2011-01-15 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / dwarf2out.c
blobca1796746327c4efd4d1c117bfb30c786c04e3a1
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, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Gary Funck (gary@intrepid.com).
6 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7 Extensively modified by Jason Merrill (jason@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
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. */
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. */
37 /* DWARF2 Abbreviation Glossary:
39 CFA = Canonical Frame Address
40 a fixed address on the stack which identifies a call frame.
41 We define it to be the value of SP just before the call insn.
42 The CFA register and offset, which may change during the course
43 of the function, are used to calculate its value at runtime.
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
51 DIE = Debugging Information Entry
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102 /* Define this macro to be a nonzero value if the directory specifications
103 which are output in the debug info should end with a separator. */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106 from generating indirect strings in DWARF2 debug information, for instance
107 if your target is stuck with an old version of GDB that is unable to
108 process them properly or uses VMS Debug. */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
115 /* ??? Poison these here until it can be done generically. They've been
116 totally replaced in this file; make sure it stays that way. */
117 #undef DWARF2_UNWIND_INFO
118 #undef DWARF2_FRAME_INFO
119 #if (GCC_VERSION >= 3000)
120 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
121 #endif
123 #ifndef INCOMING_RETURN_ADDR_RTX
124 #define INCOMING_RETURN_ADDR_RTX (gcc_unreachable (), NULL_RTX)
125 #endif
127 /* Map register numbers held in the call frame info that gcc has
128 collected using DWARF_FRAME_REGNUM to those that should be output in
129 .debug_frame and .eh_frame. */
130 #ifndef DWARF2_FRAME_REG_OUT
131 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
132 #endif
134 /* Save the result of dwarf2out_do_frame across PCH. */
135 static GTY(()) bool saved_do_cfi_asm = 0;
137 /* Decide whether we want to emit frame unwind information for the current
138 translation unit. */
141 dwarf2out_do_frame (void)
143 /* We want to emit correct CFA location expressions or lists, so we
144 have to return true if we're going to output debug info, even if
145 we're not going to output frame or unwind info. */
146 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
147 return true;
149 if (saved_do_cfi_asm)
150 return true;
152 if (targetm.debug_unwind_info () == UI_DWARF2)
153 return true;
155 if ((flag_unwind_tables || flag_exceptions)
156 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
157 return true;
159 return false;
162 /* Decide whether to emit frame unwind via assembler directives. */
165 dwarf2out_do_cfi_asm (void)
167 int enc;
169 #ifdef MIPS_DEBUGGING_INFO
170 return false;
171 #endif
172 if (saved_do_cfi_asm)
173 return true;
174 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
175 return false;
176 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
177 return false;
179 /* Make sure the personality encoding is one the assembler can support.
180 In particular, aligned addresses can't be handled. */
181 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
182 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
183 return false;
184 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
185 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
186 return false;
188 /* If we can't get the assembler to emit only .debug_frame, and we don't need
189 dwarf2 unwind info for exceptions, then emit .debug_frame by hand. */
190 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
191 && !flag_unwind_tables && !flag_exceptions
192 && targetm.except_unwind_info (&global_options) != UI_DWARF2)
193 return false;
195 saved_do_cfi_asm = true;
196 return true;
199 /* The size of the target's pointer type. */
200 #ifndef PTR_SIZE
201 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
202 #endif
204 /* Array of RTXes referenced by the debugging information, which therefore
205 must be kept around forever. */
206 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208 /* A pointer to the base of a list of incomplete types which might be
209 completed at some later time. incomplete_types_list needs to be a
210 VEC(tree,gc) because we want to tell the garbage collector about
211 it. */
212 static GTY(()) VEC(tree,gc) *incomplete_types;
214 /* A pointer to the base of a table of references to declaration
215 scopes. This table is a display which tracks the nesting
216 of declaration scopes at the current scope and containing
217 scopes. This table is used to find the proper place to
218 define type declaration DIE's. */
219 static GTY(()) VEC(tree,gc) *decl_scope_table;
221 /* Pointers to various DWARF2 sections. */
222 static GTY(()) section *debug_info_section;
223 static GTY(()) section *debug_abbrev_section;
224 static GTY(()) section *debug_aranges_section;
225 static GTY(()) section *debug_macinfo_section;
226 static GTY(()) section *debug_line_section;
227 static GTY(()) section *debug_loc_section;
228 static GTY(()) section *debug_pubnames_section;
229 static GTY(()) section *debug_pubtypes_section;
230 static GTY(()) section *debug_dcall_section;
231 static GTY(()) section *debug_vcall_section;
232 static GTY(()) section *debug_str_section;
233 static GTY(()) section *debug_ranges_section;
234 static GTY(()) section *debug_frame_section;
236 /* Personality decl of current unit. Used only when assembler does not support
237 personality CFI. */
238 static GTY(()) rtx current_unit_personality;
240 /* How to start an assembler comment. */
241 #ifndef ASM_COMMENT_START
242 #define ASM_COMMENT_START ";#"
243 #endif
245 typedef struct dw_cfi_struct *dw_cfi_ref;
246 typedef struct dw_fde_struct *dw_fde_ref;
247 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
249 /* Call frames are described using a sequence of Call Frame
250 Information instructions. The register number, offset
251 and address fields are provided as possible operands;
252 their use is selected by the opcode field. */
254 enum dw_cfi_oprnd_type {
255 dw_cfi_oprnd_unused,
256 dw_cfi_oprnd_reg_num,
257 dw_cfi_oprnd_offset,
258 dw_cfi_oprnd_addr,
259 dw_cfi_oprnd_loc
262 typedef union GTY(()) dw_cfi_oprnd_struct {
263 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
264 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
265 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
266 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
268 dw_cfi_oprnd;
270 typedef struct GTY(()) dw_cfi_struct {
271 dw_cfi_ref dw_cfi_next;
272 enum dwarf_call_frame_info dw_cfi_opc;
273 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
274 dw_cfi_oprnd1;
275 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
276 dw_cfi_oprnd2;
278 dw_cfi_node;
280 /* This is how we define the location of the CFA. We use to handle it
281 as REG + OFFSET all the time, but now it can be more complex.
282 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
283 Instead of passing around REG and OFFSET, we pass a copy
284 of this structure. */
285 typedef struct cfa_loc {
286 HOST_WIDE_INT offset;
287 HOST_WIDE_INT base_offset;
288 unsigned int reg;
289 BOOL_BITFIELD indirect : 1; /* 1 if CFA is accessed via a dereference. */
290 BOOL_BITFIELD in_use : 1; /* 1 if a saved cfa is stored here. */
291 } dw_cfa_location;
293 /* All call frame descriptions (FDE's) in the GCC generated DWARF
294 refer to a single Common Information Entry (CIE), defined at
295 the beginning of the .debug_frame section. This use of a single
296 CIE obviates the need to keep track of multiple CIE's
297 in the DWARF generation routines below. */
299 typedef struct GTY(()) dw_fde_struct {
300 tree decl;
301 const char *dw_fde_begin;
302 const char *dw_fde_current_label;
303 const char *dw_fde_end;
304 const char *dw_fde_vms_end_prologue;
305 const char *dw_fde_vms_begin_epilogue;
306 const char *dw_fde_hot_section_label;
307 const char *dw_fde_hot_section_end_label;
308 const char *dw_fde_unlikely_section_label;
309 const char *dw_fde_unlikely_section_end_label;
310 dw_cfi_ref dw_fde_cfi;
311 dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections. */
312 HOST_WIDE_INT stack_realignment;
313 unsigned funcdef_number;
314 /* Dynamic realign argument pointer register. */
315 unsigned int drap_reg;
316 /* Virtual dynamic realign argument pointer register. */
317 unsigned int vdrap_reg;
318 /* These 3 flags are copied from rtl_data in function.h. */
319 unsigned all_throwers_are_sibcalls : 1;
320 unsigned uses_eh_lsda : 1;
321 unsigned nothrow : 1;
322 /* Whether we did stack realign in this call frame. */
323 unsigned stack_realign : 1;
324 /* Whether dynamic realign argument pointer register has been saved. */
325 unsigned drap_reg_saved: 1;
326 /* True iff dw_fde_begin label is in text_section or cold_text_section. */
327 unsigned in_std_section : 1;
328 /* True iff dw_fde_unlikely_section_label is in text_section or
329 cold_text_section. */
330 unsigned cold_in_std_section : 1;
331 /* True iff switched sections. */
332 unsigned dw_fde_switched_sections : 1;
333 /* True iff switching from cold to hot section. */
334 unsigned dw_fde_switched_cold_to_hot : 1;
336 dw_fde_node;
338 /* Maximum size (in bytes) of an artificially generated label. */
339 #define MAX_ARTIFICIAL_LABEL_BYTES 30
341 /* The size of addresses as they appear in the Dwarf 2 data.
342 Some architectures use word addresses to refer to code locations,
343 but Dwarf 2 info always uses byte addresses. On such machines,
344 Dwarf 2 addresses need to be larger than the architecture's
345 pointers. */
346 #ifndef DWARF2_ADDR_SIZE
347 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
348 #endif
350 /* The size in bytes of a DWARF field indicating an offset or length
351 relative to a debug info section, specified to be 4 bytes in the
352 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
353 as PTR_SIZE. */
355 #ifndef DWARF_OFFSET_SIZE
356 #define DWARF_OFFSET_SIZE 4
357 #endif
359 /* The size in bytes of a DWARF 4 type signature. */
361 #ifndef DWARF_TYPE_SIGNATURE_SIZE
362 #define DWARF_TYPE_SIGNATURE_SIZE 8
363 #endif
365 /* According to the (draft) DWARF 3 specification, the initial length
366 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
367 bytes are 0xffffffff, followed by the length stored in the next 8
368 bytes.
370 However, the SGI/MIPS ABI uses an initial length which is equal to
371 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
373 #ifndef DWARF_INITIAL_LENGTH_SIZE
374 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
375 #endif
377 /* Round SIZE up to the nearest BOUNDARY. */
378 #define DWARF_ROUND(SIZE,BOUNDARY) \
379 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
381 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
382 #ifndef DWARF_CIE_DATA_ALIGNMENT
383 #ifdef STACK_GROWS_DOWNWARD
384 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
385 #else
386 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
387 #endif
388 #endif
390 /* CIE identifier. */
391 #if HOST_BITS_PER_WIDE_INT >= 64
392 #define DWARF_CIE_ID \
393 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
394 #else
395 #define DWARF_CIE_ID DW_CIE_ID
396 #endif
398 /* A pointer to the base of a table that contains frame description
399 information for each routine. */
400 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
402 /* Number of elements currently allocated for fde_table. */
403 static GTY(()) unsigned fde_table_allocated;
405 /* Number of elements in fde_table currently in use. */
406 static GTY(()) unsigned fde_table_in_use;
408 /* Size (in elements) of increments by which we may expand the
409 fde_table. */
410 #define FDE_TABLE_INCREMENT 256
412 /* Get the current fde_table entry we should use. */
414 static inline dw_fde_ref
415 current_fde (void)
417 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
420 /* A list of call frame insns for the CIE. */
421 static GTY(()) dw_cfi_ref cie_cfi_head;
423 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
424 attribute that accelerates the lookup of the FDE associated
425 with the subprogram. This variable holds the table index of the FDE
426 associated with the current function (body) definition. */
427 static unsigned current_funcdef_fde;
429 struct GTY(()) indirect_string_node {
430 const char *str;
431 unsigned int refcount;
432 enum dwarf_form form;
433 char *label;
436 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
438 /* True if the compilation unit has location entries that reference
439 debug strings. */
440 static GTY(()) bool debug_str_hash_forced = false;
442 static GTY(()) int dw2_string_counter;
443 static GTY(()) unsigned long dwarf2out_cfi_label_num;
445 /* True if the compilation unit places functions in more than one section. */
446 static GTY(()) bool have_multiple_function_sections = false;
448 /* Whether the default text and cold text sections have been used at all. */
450 static GTY(()) bool text_section_used = false;
451 static GTY(()) bool cold_text_section_used = false;
453 /* The default cold text section. */
454 static GTY(()) section *cold_text_section;
456 /* Forward declarations for functions defined in this file. */
458 static char *stripattributes (const char *);
459 static const char *dwarf_cfi_name (unsigned);
460 static dw_cfi_ref new_cfi (void);
461 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
462 static void add_fde_cfi (const char *, dw_cfi_ref);
463 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
464 static void lookup_cfa (dw_cfa_location *);
465 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
466 static void initial_return_save (rtx);
467 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
468 HOST_WIDE_INT);
469 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
470 static void output_cfi_directive (dw_cfi_ref);
471 static void output_call_frame_info (int);
472 static void dwarf2out_note_section_used (void);
473 static bool clobbers_queued_reg_save (const_rtx);
474 static void dwarf2out_frame_debug_expr (rtx, const char *);
476 /* Support for complex CFA locations. */
477 static void output_cfa_loc (dw_cfi_ref);
478 static void output_cfa_loc_raw (dw_cfi_ref);
479 static void get_cfa_from_loc_descr (dw_cfa_location *,
480 struct dw_loc_descr_struct *);
481 static struct dw_loc_descr_struct *build_cfa_loc
482 (dw_cfa_location *, HOST_WIDE_INT);
483 static struct dw_loc_descr_struct *build_cfa_aligned_loc
484 (HOST_WIDE_INT, HOST_WIDE_INT);
485 static void def_cfa_1 (const char *, dw_cfa_location *);
486 static struct dw_loc_descr_struct *mem_loc_descriptor
487 (rtx, enum machine_mode mode, enum var_init_status);
489 /* How to start an assembler comment. */
490 #ifndef ASM_COMMENT_START
491 #define ASM_COMMENT_START ";#"
492 #endif
494 /* Data and reference forms for relocatable data. */
495 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
496 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
498 #ifndef DEBUG_FRAME_SECTION
499 #define DEBUG_FRAME_SECTION ".debug_frame"
500 #endif
502 #ifndef FUNC_BEGIN_LABEL
503 #define FUNC_BEGIN_LABEL "LFB"
504 #endif
506 #ifndef FUNC_END_LABEL
507 #define FUNC_END_LABEL "LFE"
508 #endif
510 #ifndef PROLOGUE_END_LABEL
511 #define PROLOGUE_END_LABEL "LPE"
512 #endif
514 #ifndef EPILOGUE_BEGIN_LABEL
515 #define EPILOGUE_BEGIN_LABEL "LEB"
516 #endif
518 #ifndef FRAME_BEGIN_LABEL
519 #define FRAME_BEGIN_LABEL "Lframe"
520 #endif
521 #define CIE_AFTER_SIZE_LABEL "LSCIE"
522 #define CIE_END_LABEL "LECIE"
523 #define FDE_LABEL "LSFDE"
524 #define FDE_AFTER_SIZE_LABEL "LASFDE"
525 #define FDE_END_LABEL "LEFDE"
526 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
527 #define LINE_NUMBER_END_LABEL "LELT"
528 #define LN_PROLOG_AS_LABEL "LASLTP"
529 #define LN_PROLOG_END_LABEL "LELTP"
530 #define DIE_LABEL_PREFIX "DW"
532 /* The DWARF 2 CFA column which tracks the return address. Normally this
533 is the column for PC, or the first column after all of the hard
534 registers. */
535 #ifndef DWARF_FRAME_RETURN_COLUMN
536 #ifdef PC_REGNUM
537 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
538 #else
539 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
540 #endif
541 #endif
543 /* The mapping from gcc register number to DWARF 2 CFA column number. By
544 default, we just provide columns for all registers. */
545 #ifndef DWARF_FRAME_REGNUM
546 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
547 #endif
549 /* Match the base name of a file to the base name of a compilation unit. */
551 static int
552 matches_main_base (const char *path)
554 /* Cache the last query. */
555 static const char *last_path = NULL;
556 static int last_match = 0;
557 if (path != last_path)
559 const char *base;
560 int length = base_of_path (path, &base);
561 last_path = path;
562 last_match = (length == main_input_baselength
563 && memcmp (base, main_input_basename, length) == 0);
565 return last_match;
568 #ifdef DEBUG_DEBUG_STRUCT
570 static int
571 dump_struct_debug (tree type, enum debug_info_usage usage,
572 enum debug_struct_file criterion, int generic,
573 int matches, int result)
575 /* Find the type name. */
576 tree type_decl = TYPE_STUB_DECL (type);
577 tree t = type_decl;
578 const char *name = 0;
579 if (TREE_CODE (t) == TYPE_DECL)
580 t = DECL_NAME (t);
581 if (t)
582 name = IDENTIFIER_POINTER (t);
584 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
585 criterion,
586 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
587 matches ? "bas" : "hdr",
588 generic ? "gen" : "ord",
589 usage == DINFO_USAGE_DFN ? ";" :
590 usage == DINFO_USAGE_DIR_USE ? "." : "*",
591 result,
592 (void*) type_decl, name);
593 return result;
595 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
596 dump_struct_debug (type, usage, criterion, generic, matches, result)
598 #else
600 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
601 (result)
603 #endif
605 static bool
606 should_emit_struct_debug (tree type, enum debug_info_usage usage)
608 enum debug_struct_file criterion;
609 tree type_decl;
610 bool generic = lang_hooks.types.generic_p (type);
612 if (generic)
613 criterion = debug_struct_generic[usage];
614 else
615 criterion = debug_struct_ordinary[usage];
617 if (criterion == DINFO_STRUCT_FILE_NONE)
618 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
619 if (criterion == DINFO_STRUCT_FILE_ANY)
620 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
622 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
624 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
625 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
627 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
628 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
629 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
632 /* Hook used by __throw. */
635 expand_builtin_dwarf_sp_column (void)
637 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
638 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
641 /* Return a pointer to a copy of the section string name S with all
642 attributes stripped off, and an asterisk prepended (for assemble_name). */
644 static inline char *
645 stripattributes (const char *s)
647 char *stripped = XNEWVEC (char, strlen (s) + 2);
648 char *p = stripped;
650 *p++ = '*';
652 while (*s && *s != ',')
653 *p++ = *s++;
655 *p = '\0';
656 return stripped;
659 /* MEM is a memory reference for the register size table, each element of
660 which has mode MODE. Initialize column C as a return address column. */
662 static void
663 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
665 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
666 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
667 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
670 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
672 static inline HOST_WIDE_INT
673 div_data_align (HOST_WIDE_INT off)
675 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
676 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
677 return r;
680 /* Return true if we need a signed version of a given opcode
681 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
683 static inline bool
684 need_data_align_sf_opcode (HOST_WIDE_INT off)
686 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
689 /* Generate code to initialize the register size table. */
691 void
692 expand_builtin_init_dwarf_reg_sizes (tree address)
694 unsigned int i;
695 enum machine_mode mode = TYPE_MODE (char_type_node);
696 rtx addr = expand_normal (address);
697 rtx mem = gen_rtx_MEM (BLKmode, addr);
698 bool wrote_return_column = false;
700 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
702 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
704 if (rnum < DWARF_FRAME_REGISTERS)
706 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
707 enum machine_mode save_mode = reg_raw_mode[i];
708 HOST_WIDE_INT size;
710 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
711 save_mode = choose_hard_reg_mode (i, 1, true);
712 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
714 if (save_mode == VOIDmode)
715 continue;
716 wrote_return_column = true;
718 size = GET_MODE_SIZE (save_mode);
719 if (offset < 0)
720 continue;
722 emit_move_insn (adjust_address (mem, mode, offset),
723 gen_int_mode (size, mode));
727 if (!wrote_return_column)
728 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
730 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
731 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
732 #endif
734 targetm.init_dwarf_reg_sizes_extra (address);
737 /* Convert a DWARF call frame info. operation to its string name */
739 static const char *
740 dwarf_cfi_name (unsigned int cfi_opc)
742 switch (cfi_opc)
744 case DW_CFA_advance_loc:
745 return "DW_CFA_advance_loc";
746 case DW_CFA_offset:
747 return "DW_CFA_offset";
748 case DW_CFA_restore:
749 return "DW_CFA_restore";
750 case DW_CFA_nop:
751 return "DW_CFA_nop";
752 case DW_CFA_set_loc:
753 return "DW_CFA_set_loc";
754 case DW_CFA_advance_loc1:
755 return "DW_CFA_advance_loc1";
756 case DW_CFA_advance_loc2:
757 return "DW_CFA_advance_loc2";
758 case DW_CFA_advance_loc4:
759 return "DW_CFA_advance_loc4";
760 case DW_CFA_offset_extended:
761 return "DW_CFA_offset_extended";
762 case DW_CFA_restore_extended:
763 return "DW_CFA_restore_extended";
764 case DW_CFA_undefined:
765 return "DW_CFA_undefined";
766 case DW_CFA_same_value:
767 return "DW_CFA_same_value";
768 case DW_CFA_register:
769 return "DW_CFA_register";
770 case DW_CFA_remember_state:
771 return "DW_CFA_remember_state";
772 case DW_CFA_restore_state:
773 return "DW_CFA_restore_state";
774 case DW_CFA_def_cfa:
775 return "DW_CFA_def_cfa";
776 case DW_CFA_def_cfa_register:
777 return "DW_CFA_def_cfa_register";
778 case DW_CFA_def_cfa_offset:
779 return "DW_CFA_def_cfa_offset";
781 /* DWARF 3 */
782 case DW_CFA_def_cfa_expression:
783 return "DW_CFA_def_cfa_expression";
784 case DW_CFA_expression:
785 return "DW_CFA_expression";
786 case DW_CFA_offset_extended_sf:
787 return "DW_CFA_offset_extended_sf";
788 case DW_CFA_def_cfa_sf:
789 return "DW_CFA_def_cfa_sf";
790 case DW_CFA_def_cfa_offset_sf:
791 return "DW_CFA_def_cfa_offset_sf";
793 /* SGI/MIPS specific */
794 case DW_CFA_MIPS_advance_loc8:
795 return "DW_CFA_MIPS_advance_loc8";
797 /* GNU extensions */
798 case DW_CFA_GNU_window_save:
799 return "DW_CFA_GNU_window_save";
800 case DW_CFA_GNU_args_size:
801 return "DW_CFA_GNU_args_size";
802 case DW_CFA_GNU_negative_offset_extended:
803 return "DW_CFA_GNU_negative_offset_extended";
805 default:
806 return "DW_CFA_<unknown>";
810 /* Return a pointer to a newly allocated Call Frame Instruction. */
812 static inline dw_cfi_ref
813 new_cfi (void)
815 dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
817 cfi->dw_cfi_next = NULL;
818 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
819 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
821 return cfi;
824 /* Add a Call Frame Instruction to list of instructions. */
826 static inline void
827 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
829 dw_cfi_ref *p;
830 dw_fde_ref fde = current_fde ();
832 /* When DRAP is used, CFA is defined with an expression. Redefine
833 CFA may lead to a different CFA value. */
834 /* ??? Of course, this heuristic fails when we're annotating epilogues,
835 because of course we'll always want to redefine the CFA back to the
836 stack pointer on the way out. Where should we move this check? */
837 if (0 && fde && fde->drap_reg != INVALID_REGNUM)
838 switch (cfi->dw_cfi_opc)
840 case DW_CFA_def_cfa_register:
841 case DW_CFA_def_cfa_offset:
842 case DW_CFA_def_cfa_offset_sf:
843 case DW_CFA_def_cfa:
844 case DW_CFA_def_cfa_sf:
845 gcc_unreachable ();
847 default:
848 break;
851 /* Find the end of the chain. */
852 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
855 *p = cfi;
858 /* Generate a new label for the CFI info to refer to. FORCE is true
859 if a label needs to be output even when using .cfi_* directives. */
861 char *
862 dwarf2out_cfi_label (bool force)
864 static char label[20];
866 if (!force && dwarf2out_do_cfi_asm ())
868 /* In this case, we will be emitting the asm directive instead of
869 the label, so just return a placeholder to keep the rest of the
870 interfaces happy. */
871 strcpy (label, "<do not output>");
873 else
875 int num = dwarf2out_cfi_label_num++;
876 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
877 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
880 return label;
883 /* True if remember_state should be emitted before following CFI directive. */
884 static bool emit_cfa_remember;
886 /* True if any CFI directives were emitted at the current insn. */
887 static bool any_cfis_emitted;
889 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
890 or to the CIE if LABEL is NULL. */
892 static void
893 add_fde_cfi (const char *label, dw_cfi_ref cfi)
895 dw_cfi_ref *list_head;
897 if (emit_cfa_remember)
899 dw_cfi_ref cfi_remember;
901 /* Emit the state save. */
902 emit_cfa_remember = false;
903 cfi_remember = new_cfi ();
904 cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
905 add_fde_cfi (label, cfi_remember);
908 list_head = &cie_cfi_head;
910 if (dwarf2out_do_cfi_asm ())
912 if (label)
914 dw_fde_ref fde = current_fde ();
916 gcc_assert (fde != NULL);
918 /* We still have to add the cfi to the list so that lookup_cfa
919 works later on. When -g2 and above we even need to force
920 emitting of CFI labels and add to list a DW_CFA_set_loc for
921 convert_cfa_to_fb_loc_list purposes. If we're generating
922 DWARF3 output we use DW_OP_call_frame_cfa and so don't use
923 convert_cfa_to_fb_loc_list. */
924 if (dwarf_version == 2
925 && debug_info_level > DINFO_LEVEL_TERSE
926 && (write_symbols == DWARF2_DEBUG
927 || write_symbols == VMS_AND_DWARF2_DEBUG))
929 switch (cfi->dw_cfi_opc)
931 case DW_CFA_def_cfa_offset:
932 case DW_CFA_def_cfa_offset_sf:
933 case DW_CFA_def_cfa_register:
934 case DW_CFA_def_cfa:
935 case DW_CFA_def_cfa_sf:
936 case DW_CFA_def_cfa_expression:
937 case DW_CFA_restore_state:
938 if (*label == 0 || strcmp (label, "<do not output>") == 0)
939 label = dwarf2out_cfi_label (true);
941 if (fde->dw_fde_current_label == NULL
942 || strcmp (label, fde->dw_fde_current_label) != 0)
944 dw_cfi_ref xcfi;
946 label = xstrdup (label);
948 /* Set the location counter to the new label. */
949 xcfi = new_cfi ();
950 /* It doesn't metter whether DW_CFA_set_loc
951 or DW_CFA_advance_loc4 is added here, those aren't
952 emitted into assembly, only looked up by
953 convert_cfa_to_fb_loc_list. */
954 xcfi->dw_cfi_opc = DW_CFA_set_loc;
955 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
956 add_cfi (&fde->dw_fde_cfi, xcfi);
957 fde->dw_fde_current_label = label;
959 break;
960 default:
961 break;
965 output_cfi_directive (cfi);
967 list_head = &fde->dw_fde_cfi;
968 any_cfis_emitted = true;
970 /* ??? If this is a CFI for the CIE, we don't emit. This
971 assumes that the standard CIE contents that the assembler
972 uses matches the standard CIE contents that the compiler
973 uses. This is probably a bad assumption. I'm not quite
974 sure how to address this for now. */
976 else if (label)
978 dw_fde_ref fde = current_fde ();
980 gcc_assert (fde != NULL);
982 if (*label == 0)
983 label = dwarf2out_cfi_label (false);
985 if (fde->dw_fde_current_label == NULL
986 || strcmp (label, fde->dw_fde_current_label) != 0)
988 dw_cfi_ref xcfi;
990 label = xstrdup (label);
992 /* Set the location counter to the new label. */
993 xcfi = new_cfi ();
994 /* If we have a current label, advance from there, otherwise
995 set the location directly using set_loc. */
996 xcfi->dw_cfi_opc = fde->dw_fde_current_label
997 ? DW_CFA_advance_loc4
998 : DW_CFA_set_loc;
999 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
1000 add_cfi (&fde->dw_fde_cfi, xcfi);
1002 fde->dw_fde_current_label = label;
1005 list_head = &fde->dw_fde_cfi;
1006 any_cfis_emitted = true;
1009 add_cfi (list_head, cfi);
1012 /* Subroutine of lookup_cfa. */
1014 static void
1015 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1017 switch (cfi->dw_cfi_opc)
1019 case DW_CFA_def_cfa_offset:
1020 case DW_CFA_def_cfa_offset_sf:
1021 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1022 break;
1023 case DW_CFA_def_cfa_register:
1024 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1025 break;
1026 case DW_CFA_def_cfa:
1027 case DW_CFA_def_cfa_sf:
1028 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1029 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1030 break;
1031 case DW_CFA_def_cfa_expression:
1032 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1033 break;
1035 case DW_CFA_remember_state:
1036 gcc_assert (!remember->in_use);
1037 *remember = *loc;
1038 remember->in_use = 1;
1039 break;
1040 case DW_CFA_restore_state:
1041 gcc_assert (remember->in_use);
1042 *loc = *remember;
1043 remember->in_use = 0;
1044 break;
1046 default:
1047 break;
1051 /* Find the previous value for the CFA. */
1053 static void
1054 lookup_cfa (dw_cfa_location *loc)
1056 dw_cfi_ref cfi;
1057 dw_fde_ref fde;
1058 dw_cfa_location remember;
1060 memset (loc, 0, sizeof (*loc));
1061 loc->reg = INVALID_REGNUM;
1062 remember = *loc;
1064 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1065 lookup_cfa_1 (cfi, loc, &remember);
1067 fde = current_fde ();
1068 if (fde)
1069 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1070 lookup_cfa_1 (cfi, loc, &remember);
1073 /* The current rule for calculating the DWARF2 canonical frame address. */
1074 static dw_cfa_location cfa;
1076 /* The register used for saving registers to the stack, and its offset
1077 from the CFA. */
1078 static dw_cfa_location cfa_store;
1080 /* The current save location around an epilogue. */
1081 static dw_cfa_location cfa_remember;
1083 /* The running total of the size of arguments pushed onto the stack. */
1084 static HOST_WIDE_INT args_size;
1086 /* The last args_size we actually output. */
1087 static HOST_WIDE_INT old_args_size;
1089 /* Entry point to update the canonical frame address (CFA).
1090 LABEL is passed to add_fde_cfi. The value of CFA is now to be
1091 calculated from REG+OFFSET. */
1093 void
1094 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1096 dw_cfa_location loc;
1097 loc.indirect = 0;
1098 loc.base_offset = 0;
1099 loc.reg = reg;
1100 loc.offset = offset;
1101 def_cfa_1 (label, &loc);
1104 /* Determine if two dw_cfa_location structures define the same data. */
1106 static bool
1107 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1109 return (loc1->reg == loc2->reg
1110 && loc1->offset == loc2->offset
1111 && loc1->indirect == loc2->indirect
1112 && (loc1->indirect == 0
1113 || loc1->base_offset == loc2->base_offset));
1116 /* This routine does the actual work. The CFA is now calculated from
1117 the dw_cfa_location structure. */
1119 static void
1120 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1122 dw_cfi_ref cfi;
1123 dw_cfa_location old_cfa, loc;
1125 cfa = *loc_p;
1126 loc = *loc_p;
1128 if (cfa_store.reg == loc.reg && loc.indirect == 0)
1129 cfa_store.offset = loc.offset;
1131 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1132 lookup_cfa (&old_cfa);
1134 /* If nothing changed, no need to issue any call frame instructions. */
1135 if (cfa_equal_p (&loc, &old_cfa))
1136 return;
1138 cfi = new_cfi ();
1140 if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1142 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1143 the CFA register did not change but the offset did. The data
1144 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1145 in the assembler via the .cfi_def_cfa_offset directive. */
1146 if (loc.offset < 0)
1147 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1148 else
1149 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1150 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1153 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
1154 else if (loc.offset == old_cfa.offset
1155 && old_cfa.reg != INVALID_REGNUM
1156 && !loc.indirect
1157 && !old_cfa.indirect)
1159 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1160 indicating the CFA register has changed to <register> but the
1161 offset has not changed. */
1162 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1163 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1165 #endif
1167 else if (loc.indirect == 0)
1169 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1170 indicating the CFA register has changed to <register> with
1171 the specified offset. The data factoring for DW_CFA_def_cfa_sf
1172 happens in output_cfi, or in the assembler via the .cfi_def_cfa
1173 directive. */
1174 if (loc.offset < 0)
1175 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1176 else
1177 cfi->dw_cfi_opc = DW_CFA_def_cfa;
1178 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1179 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1181 else
1183 /* Construct a DW_CFA_def_cfa_expression instruction to
1184 calculate the CFA using a full location expression since no
1185 register-offset pair is available. */
1186 struct dw_loc_descr_struct *loc_list;
1188 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1189 loc_list = build_cfa_loc (&loc, 0);
1190 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1193 add_fde_cfi (label, cfi);
1196 /* Add the CFI for saving a register. REG is the CFA column number.
1197 LABEL is passed to add_fde_cfi.
1198 If SREG is -1, the register is saved at OFFSET from the CFA;
1199 otherwise it is saved in SREG. */
1201 static void
1202 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1204 dw_cfi_ref cfi = new_cfi ();
1205 dw_fde_ref fde = current_fde ();
1207 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1209 /* When stack is aligned, store REG using DW_CFA_expression with
1210 FP. */
1211 if (fde
1212 && fde->stack_realign
1213 && sreg == INVALID_REGNUM)
1215 cfi->dw_cfi_opc = DW_CFA_expression;
1216 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1217 cfi->dw_cfi_oprnd2.dw_cfi_loc
1218 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1220 else if (sreg == INVALID_REGNUM)
1222 if (need_data_align_sf_opcode (offset))
1223 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1224 else if (reg & ~0x3f)
1225 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1226 else
1227 cfi->dw_cfi_opc = DW_CFA_offset;
1228 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1230 else if (sreg == reg)
1231 cfi->dw_cfi_opc = DW_CFA_same_value;
1232 else
1234 cfi->dw_cfi_opc = DW_CFA_register;
1235 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1238 add_fde_cfi (label, cfi);
1241 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1242 This CFI tells the unwinder that it needs to restore the window registers
1243 from the previous frame's window save area.
1245 ??? Perhaps we should note in the CIE where windows are saved (instead of
1246 assuming 0(cfa)) and what registers are in the window. */
1248 void
1249 dwarf2out_window_save (const char *label)
1251 dw_cfi_ref cfi = new_cfi ();
1253 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1254 add_fde_cfi (label, cfi);
1257 /* Entry point for saving a register to the stack. REG is the GCC register
1258 number. LABEL and OFFSET are passed to reg_save. */
1260 void
1261 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1263 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1266 /* Entry point for saving the return address in the stack.
1267 LABEL and OFFSET are passed to reg_save. */
1269 void
1270 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1272 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1275 /* Entry point for saving the return address in a register.
1276 LABEL and SREG are passed to reg_save. */
1278 void
1279 dwarf2out_return_reg (const char *label, unsigned int sreg)
1281 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1284 /* Record the initial position of the return address. RTL is
1285 INCOMING_RETURN_ADDR_RTX. */
1287 static void
1288 initial_return_save (rtx rtl)
1290 unsigned int reg = INVALID_REGNUM;
1291 HOST_WIDE_INT offset = 0;
1293 switch (GET_CODE (rtl))
1295 case REG:
1296 /* RA is in a register. */
1297 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1298 break;
1300 case MEM:
1301 /* RA is on the stack. */
1302 rtl = XEXP (rtl, 0);
1303 switch (GET_CODE (rtl))
1305 case REG:
1306 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1307 offset = 0;
1308 break;
1310 case PLUS:
1311 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1312 offset = INTVAL (XEXP (rtl, 1));
1313 break;
1315 case MINUS:
1316 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1317 offset = -INTVAL (XEXP (rtl, 1));
1318 break;
1320 default:
1321 gcc_unreachable ();
1324 break;
1326 case PLUS:
1327 /* The return address is at some offset from any value we can
1328 actually load. For instance, on the SPARC it is in %i7+8. Just
1329 ignore the offset for now; it doesn't matter for unwinding frames. */
1330 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1331 initial_return_save (XEXP (rtl, 0));
1332 return;
1334 default:
1335 gcc_unreachable ();
1338 if (reg != DWARF_FRAME_RETURN_COLUMN)
1339 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1342 /* Given a SET, calculate the amount of stack adjustment it
1343 contains. */
1345 static HOST_WIDE_INT
1346 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1347 HOST_WIDE_INT cur_offset)
1349 const_rtx src = SET_SRC (pattern);
1350 const_rtx dest = SET_DEST (pattern);
1351 HOST_WIDE_INT offset = 0;
1352 enum rtx_code code;
1354 if (dest == stack_pointer_rtx)
1356 code = GET_CODE (src);
1358 /* Assume (set (reg sp) (reg whatever)) sets args_size
1359 level to 0. */
1360 if (code == REG && src != stack_pointer_rtx)
1362 offset = -cur_args_size;
1363 #ifndef STACK_GROWS_DOWNWARD
1364 offset = -offset;
1365 #endif
1366 return offset - cur_offset;
1369 if (! (code == PLUS || code == MINUS)
1370 || XEXP (src, 0) != stack_pointer_rtx
1371 || !CONST_INT_P (XEXP (src, 1)))
1372 return 0;
1374 /* (set (reg sp) (plus (reg sp) (const_int))) */
1375 offset = INTVAL (XEXP (src, 1));
1376 if (code == PLUS)
1377 offset = -offset;
1378 return offset;
1381 if (MEM_P (src) && !MEM_P (dest))
1382 dest = src;
1383 if (MEM_P (dest))
1385 /* (set (mem (pre_dec (reg sp))) (foo)) */
1386 src = XEXP (dest, 0);
1387 code = GET_CODE (src);
1389 switch (code)
1391 case PRE_MODIFY:
1392 case POST_MODIFY:
1393 if (XEXP (src, 0) == stack_pointer_rtx)
1395 rtx val = XEXP (XEXP (src, 1), 1);
1396 /* We handle only adjustments by constant amount. */
1397 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1398 && CONST_INT_P (val));
1399 offset = -INTVAL (val);
1400 break;
1402 return 0;
1404 case PRE_DEC:
1405 case POST_DEC:
1406 if (XEXP (src, 0) == stack_pointer_rtx)
1408 offset = GET_MODE_SIZE (GET_MODE (dest));
1409 break;
1411 return 0;
1413 case PRE_INC:
1414 case POST_INC:
1415 if (XEXP (src, 0) == stack_pointer_rtx)
1417 offset = -GET_MODE_SIZE (GET_MODE (dest));
1418 break;
1420 return 0;
1422 default:
1423 return 0;
1426 else
1427 return 0;
1429 return offset;
1432 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1433 indexed by INSN_UID. */
1435 static HOST_WIDE_INT *barrier_args_size;
1437 /* Helper function for compute_barrier_args_size. Handle one insn. */
1439 static HOST_WIDE_INT
1440 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1441 VEC (rtx, heap) **next)
1443 HOST_WIDE_INT offset = 0;
1444 int i;
1446 if (! RTX_FRAME_RELATED_P (insn))
1448 if (prologue_epilogue_contains (insn))
1449 /* Nothing */;
1450 else if (GET_CODE (PATTERN (insn)) == SET)
1451 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1452 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1453 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1455 /* There may be stack adjustments inside compound insns. Search
1456 for them. */
1457 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1458 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1459 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1460 cur_args_size, offset);
1463 else
1465 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1467 if (expr)
1469 expr = XEXP (expr, 0);
1470 if (GET_CODE (expr) == PARALLEL
1471 || GET_CODE (expr) == SEQUENCE)
1472 for (i = 1; i < XVECLEN (expr, 0); i++)
1474 rtx elem = XVECEXP (expr, 0, i);
1476 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1477 offset += stack_adjust_offset (elem, cur_args_size, offset);
1482 #ifndef STACK_GROWS_DOWNWARD
1483 offset = -offset;
1484 #endif
1486 cur_args_size += offset;
1487 if (cur_args_size < 0)
1488 cur_args_size = 0;
1490 if (JUMP_P (insn))
1492 rtx dest = JUMP_LABEL (insn);
1494 if (dest)
1496 if (barrier_args_size [INSN_UID (dest)] < 0)
1498 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1499 VEC_safe_push (rtx, heap, *next, dest);
1504 return cur_args_size;
1507 /* Walk the whole function and compute args_size on BARRIERs. */
1509 static void
1510 compute_barrier_args_size (void)
1512 int max_uid = get_max_uid (), i;
1513 rtx insn;
1514 VEC (rtx, heap) *worklist, *next, *tmp;
1516 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1517 for (i = 0; i < max_uid; i++)
1518 barrier_args_size[i] = -1;
1520 worklist = VEC_alloc (rtx, heap, 20);
1521 next = VEC_alloc (rtx, heap, 20);
1522 insn = get_insns ();
1523 barrier_args_size[INSN_UID (insn)] = 0;
1524 VEC_quick_push (rtx, worklist, insn);
1525 for (;;)
1527 while (!VEC_empty (rtx, worklist))
1529 rtx prev, body, first_insn;
1530 HOST_WIDE_INT cur_args_size;
1532 first_insn = insn = VEC_pop (rtx, worklist);
1533 cur_args_size = barrier_args_size[INSN_UID (insn)];
1534 prev = prev_nonnote_insn (insn);
1535 if (prev && BARRIER_P (prev))
1536 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1538 for (; insn; insn = NEXT_INSN (insn))
1540 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1541 continue;
1542 if (BARRIER_P (insn))
1543 break;
1545 if (LABEL_P (insn))
1547 if (insn == first_insn)
1548 continue;
1549 else if (barrier_args_size[INSN_UID (insn)] < 0)
1551 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1552 continue;
1554 else
1556 /* The insns starting with this label have been
1557 already scanned or are in the worklist. */
1558 break;
1562 body = PATTERN (insn);
1563 if (GET_CODE (body) == SEQUENCE)
1565 HOST_WIDE_INT dest_args_size = cur_args_size;
1566 for (i = 1; i < XVECLEN (body, 0); i++)
1567 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1568 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1569 dest_args_size
1570 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1571 dest_args_size, &next);
1572 else
1573 cur_args_size
1574 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1575 cur_args_size, &next);
1577 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1578 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1579 dest_args_size, &next);
1580 else
1581 cur_args_size
1582 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1583 cur_args_size, &next);
1585 else
1586 cur_args_size
1587 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1591 if (VEC_empty (rtx, next))
1592 break;
1594 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1595 tmp = next;
1596 next = worklist;
1597 worklist = tmp;
1598 VEC_truncate (rtx, next, 0);
1601 VEC_free (rtx, heap, worklist);
1602 VEC_free (rtx, heap, next);
1605 /* Add a CFI to update the running total of the size of arguments
1606 pushed onto the stack. */
1608 static void
1609 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1611 dw_cfi_ref cfi;
1613 if (size == old_args_size)
1614 return;
1616 old_args_size = size;
1618 cfi = new_cfi ();
1619 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1620 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1621 add_fde_cfi (label, cfi);
1624 /* Record a stack adjustment of OFFSET bytes. */
1626 static void
1627 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1629 if (cfa.reg == STACK_POINTER_REGNUM)
1630 cfa.offset += offset;
1632 if (cfa_store.reg == STACK_POINTER_REGNUM)
1633 cfa_store.offset += offset;
1635 if (ACCUMULATE_OUTGOING_ARGS)
1636 return;
1638 #ifndef STACK_GROWS_DOWNWARD
1639 offset = -offset;
1640 #endif
1642 args_size += offset;
1643 if (args_size < 0)
1644 args_size = 0;
1646 def_cfa_1 (label, &cfa);
1647 if (flag_asynchronous_unwind_tables)
1648 dwarf2out_args_size (label, args_size);
1651 /* Check INSN to see if it looks like a push or a stack adjustment, and
1652 make a note of it if it does. EH uses this information to find out
1653 how much extra space it needs to pop off the stack. */
1655 static void
1656 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1658 HOST_WIDE_INT offset;
1659 const char *label;
1660 int i;
1662 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1663 with this function. Proper support would require all frame-related
1664 insns to be marked, and to be able to handle saving state around
1665 epilogues textually in the middle of the function. */
1666 if (prologue_epilogue_contains (insn))
1667 return;
1669 /* If INSN is an instruction from target of an annulled branch, the
1670 effects are for the target only and so current argument size
1671 shouldn't change at all. */
1672 if (final_sequence
1673 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1674 && INSN_FROM_TARGET_P (insn))
1675 return;
1677 /* If only calls can throw, and we have a frame pointer,
1678 save up adjustments until we see the CALL_INSN. */
1679 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1681 if (CALL_P (insn) && !after_p)
1683 /* Extract the size of the args from the CALL rtx itself. */
1684 insn = PATTERN (insn);
1685 if (GET_CODE (insn) == PARALLEL)
1686 insn = XVECEXP (insn, 0, 0);
1687 if (GET_CODE (insn) == SET)
1688 insn = SET_SRC (insn);
1689 gcc_assert (GET_CODE (insn) == CALL);
1690 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1692 return;
1695 if (CALL_P (insn) && !after_p)
1697 if (!flag_asynchronous_unwind_tables)
1698 dwarf2out_args_size ("", args_size);
1699 return;
1701 else if (BARRIER_P (insn))
1703 /* Don't call compute_barrier_args_size () if the only
1704 BARRIER is at the end of function. */
1705 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1706 compute_barrier_args_size ();
1707 if (barrier_args_size == NULL)
1708 offset = 0;
1709 else
1711 offset = barrier_args_size[INSN_UID (insn)];
1712 if (offset < 0)
1713 offset = 0;
1716 offset -= args_size;
1717 #ifndef STACK_GROWS_DOWNWARD
1718 offset = -offset;
1719 #endif
1721 else if (GET_CODE (PATTERN (insn)) == SET)
1722 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1723 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1724 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1726 /* There may be stack adjustments inside compound insns. Search
1727 for them. */
1728 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1729 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1730 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1731 args_size, offset);
1733 else
1734 return;
1736 if (offset == 0)
1737 return;
1739 label = dwarf2out_cfi_label (false);
1740 dwarf2out_stack_adjust (offset, label);
1743 /* We delay emitting a register save until either (a) we reach the end
1744 of the prologue or (b) the register is clobbered. This clusters
1745 register saves so that there are fewer pc advances. */
1747 struct GTY(()) queued_reg_save {
1748 struct queued_reg_save *next;
1749 rtx reg;
1750 HOST_WIDE_INT cfa_offset;
1751 rtx saved_reg;
1754 static GTY(()) struct queued_reg_save *queued_reg_saves;
1756 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1757 struct GTY(()) reg_saved_in_data {
1758 rtx orig_reg;
1759 rtx saved_in_reg;
1762 /* A list of registers saved in other registers.
1763 The list intentionally has a small maximum capacity of 4; if your
1764 port needs more than that, you might consider implementing a
1765 more efficient data structure. */
1766 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1767 static GTY(()) size_t num_regs_saved_in_regs;
1769 static const char *last_reg_save_label;
1771 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1772 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1774 static void
1775 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1777 struct queued_reg_save *q;
1779 /* Duplicates waste space, but it's also necessary to remove them
1780 for correctness, since the queue gets output in reverse
1781 order. */
1782 for (q = queued_reg_saves; q != NULL; q = q->next)
1783 if (REGNO (q->reg) == REGNO (reg))
1784 break;
1786 if (q == NULL)
1788 q = ggc_alloc_queued_reg_save ();
1789 q->next = queued_reg_saves;
1790 queued_reg_saves = q;
1793 q->reg = reg;
1794 q->cfa_offset = offset;
1795 q->saved_reg = sreg;
1797 last_reg_save_label = label;
1800 /* Output all the entries in QUEUED_REG_SAVES. */
1802 void
1803 dwarf2out_flush_queued_reg_saves (void)
1805 struct queued_reg_save *q;
1807 for (q = queued_reg_saves; q; q = q->next)
1809 size_t i;
1810 unsigned int reg, sreg;
1812 for (i = 0; i < num_regs_saved_in_regs; i++)
1813 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1814 break;
1815 if (q->saved_reg && i == num_regs_saved_in_regs)
1817 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1818 num_regs_saved_in_regs++;
1820 if (i != num_regs_saved_in_regs)
1822 regs_saved_in_regs[i].orig_reg = q->reg;
1823 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1826 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1827 if (q->saved_reg)
1828 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1829 else
1830 sreg = INVALID_REGNUM;
1831 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1834 queued_reg_saves = NULL;
1835 last_reg_save_label = NULL;
1838 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1839 location for? Or, does it clobber a register which we've previously
1840 said that some other register is saved in, and for which we now
1841 have a new location for? */
1843 static bool
1844 clobbers_queued_reg_save (const_rtx insn)
1846 struct queued_reg_save *q;
1848 for (q = queued_reg_saves; q; q = q->next)
1850 size_t i;
1851 if (modified_in_p (q->reg, insn))
1852 return true;
1853 for (i = 0; i < num_regs_saved_in_regs; i++)
1854 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1855 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1856 return true;
1859 return false;
1862 /* Entry point for saving the first register into the second. */
1864 void
1865 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1867 size_t i;
1868 unsigned int regno, sregno;
1870 for (i = 0; i < num_regs_saved_in_regs; i++)
1871 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1872 break;
1873 if (i == num_regs_saved_in_regs)
1875 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1876 num_regs_saved_in_regs++;
1878 regs_saved_in_regs[i].orig_reg = reg;
1879 regs_saved_in_regs[i].saved_in_reg = sreg;
1881 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1882 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1883 reg_save (label, regno, sregno, 0);
1886 /* What register, if any, is currently saved in REG? */
1888 static rtx
1889 reg_saved_in (rtx reg)
1891 unsigned int regn = REGNO (reg);
1892 size_t i;
1893 struct queued_reg_save *q;
1895 for (q = queued_reg_saves; q; q = q->next)
1896 if (q->saved_reg && regn == REGNO (q->saved_reg))
1897 return q->reg;
1899 for (i = 0; i < num_regs_saved_in_regs; i++)
1900 if (regs_saved_in_regs[i].saved_in_reg
1901 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1902 return regs_saved_in_regs[i].orig_reg;
1904 return NULL_RTX;
1908 /* A temporary register holding an integral value used in adjusting SP
1909 or setting up the store_reg. The "offset" field holds the integer
1910 value, not an offset. */
1911 static dw_cfa_location cfa_temp;
1913 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1915 static void
1916 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1918 memset (&cfa, 0, sizeof (cfa));
1920 switch (GET_CODE (pat))
1922 case PLUS:
1923 cfa.reg = REGNO (XEXP (pat, 0));
1924 cfa.offset = INTVAL (XEXP (pat, 1));
1925 break;
1927 case REG:
1928 cfa.reg = REGNO (pat);
1929 break;
1931 case MEM:
1932 cfa.indirect = 1;
1933 pat = XEXP (pat, 0);
1934 if (GET_CODE (pat) == PLUS)
1936 cfa.base_offset = INTVAL (XEXP (pat, 1));
1937 pat = XEXP (pat, 0);
1939 cfa.reg = REGNO (pat);
1940 break;
1942 default:
1943 /* Recurse and define an expression. */
1944 gcc_unreachable ();
1947 def_cfa_1 (label, &cfa);
1950 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1952 static void
1953 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1955 rtx src, dest;
1957 gcc_assert (GET_CODE (pat) == SET);
1958 dest = XEXP (pat, 0);
1959 src = XEXP (pat, 1);
1961 switch (GET_CODE (src))
1963 case PLUS:
1964 gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1965 cfa.offset -= INTVAL (XEXP (src, 1));
1966 break;
1968 case REG:
1969 break;
1971 default:
1972 gcc_unreachable ();
1975 cfa.reg = REGNO (dest);
1976 gcc_assert (cfa.indirect == 0);
1978 def_cfa_1 (label, &cfa);
1981 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1983 static void
1984 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1986 HOST_WIDE_INT offset;
1987 rtx src, addr, span;
1989 src = XEXP (set, 1);
1990 addr = XEXP (set, 0);
1991 gcc_assert (MEM_P (addr));
1992 addr = XEXP (addr, 0);
1994 /* As documented, only consider extremely simple addresses. */
1995 switch (GET_CODE (addr))
1997 case REG:
1998 gcc_assert (REGNO (addr) == cfa.reg);
1999 offset = -cfa.offset;
2000 break;
2001 case PLUS:
2002 gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2003 offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2004 break;
2005 default:
2006 gcc_unreachable ();
2009 span = targetm.dwarf_register_span (src);
2011 /* ??? We'd like to use queue_reg_save, but we need to come up with
2012 a different flushing heuristic for epilogues. */
2013 if (!span)
2014 reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2015 else
2017 /* We have a PARALLEL describing where the contents of SRC live.
2018 Queue register saves for each piece of the PARALLEL. */
2019 int par_index;
2020 int limit;
2021 HOST_WIDE_INT span_offset = offset;
2023 gcc_assert (GET_CODE (span) == PARALLEL);
2025 limit = XVECLEN (span, 0);
2026 for (par_index = 0; par_index < limit; par_index++)
2028 rtx elem = XVECEXP (span, 0, par_index);
2030 reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2031 INVALID_REGNUM, span_offset);
2032 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2037 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
2039 static void
2040 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2042 rtx src, dest;
2043 unsigned sregno, dregno;
2045 src = XEXP (set, 1);
2046 dest = XEXP (set, 0);
2048 if (src == pc_rtx)
2049 sregno = DWARF_FRAME_RETURN_COLUMN;
2050 else
2051 sregno = DWARF_FRAME_REGNUM (REGNO (src));
2053 dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2055 /* ??? We'd like to use queue_reg_save, but we need to come up with
2056 a different flushing heuristic for epilogues. */
2057 reg_save (label, sregno, dregno, 0);
2060 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2062 static void
2063 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2065 rtx src, dest, span;
2066 dw_cfi_ref cfi = new_cfi ();
2068 dest = SET_DEST (set);
2069 src = SET_SRC (set);
2071 gcc_assert (REG_P (src));
2072 gcc_assert (MEM_P (dest));
2074 span = targetm.dwarf_register_span (src);
2075 gcc_assert (!span);
2077 cfi->dw_cfi_opc = DW_CFA_expression;
2078 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2079 cfi->dw_cfi_oprnd2.dw_cfi_loc
2080 = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2081 VAR_INIT_STATUS_INITIALIZED);
2083 /* ??? We'd like to use queue_reg_save, were the interface different,
2084 and, as above, we could manage flushing for epilogues. */
2085 add_fde_cfi (label, cfi);
2088 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
2090 static void
2091 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2093 dw_cfi_ref cfi = new_cfi ();
2094 unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2096 cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2097 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2099 add_fde_cfi (label, cfi);
2102 /* Record call frame debugging information for an expression EXPR,
2103 which either sets SP or FP (adjusting how we calculate the frame
2104 address) or saves a register to the stack or another register.
2105 LABEL indicates the address of EXPR.
2107 This function encodes a state machine mapping rtxes to actions on
2108 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
2109 users need not read the source code.
2111 The High-Level Picture
2113 Changes in the register we use to calculate the CFA: Currently we
2114 assume that if you copy the CFA register into another register, we
2115 should take the other one as the new CFA register; this seems to
2116 work pretty well. If it's wrong for some target, it's simple
2117 enough not to set RTX_FRAME_RELATED_P on the insn in question.
2119 Changes in the register we use for saving registers to the stack:
2120 This is usually SP, but not always. Again, we deduce that if you
2121 copy SP into another register (and SP is not the CFA register),
2122 then the new register is the one we will be using for register
2123 saves. This also seems to work.
2125 Register saves: There's not much guesswork about this one; if
2126 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2127 register save, and the register used to calculate the destination
2128 had better be the one we think we're using for this purpose.
2129 It's also assumed that a copy from a call-saved register to another
2130 register is saving that register if RTX_FRAME_RELATED_P is set on
2131 that instruction. If the copy is from a call-saved register to
2132 the *same* register, that means that the register is now the same
2133 value as in the caller.
2135 Except: If the register being saved is the CFA register, and the
2136 offset is nonzero, we are saving the CFA, so we assume we have to
2137 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
2138 the intent is to save the value of SP from the previous frame.
2140 In addition, if a register has previously been saved to a different
2141 register,
2143 Invariants / Summaries of Rules
2145 cfa current rule for calculating the CFA. It usually
2146 consists of a register and an offset.
2147 cfa_store register used by prologue code to save things to the stack
2148 cfa_store.offset is the offset from the value of
2149 cfa_store.reg to the actual CFA
2150 cfa_temp register holding an integral value. cfa_temp.offset
2151 stores the value, which will be used to adjust the
2152 stack pointer. cfa_temp is also used like cfa_store,
2153 to track stores to the stack via fp or a temp reg.
2155 Rules 1- 4: Setting a register's value to cfa.reg or an expression
2156 with cfa.reg as the first operand changes the cfa.reg and its
2157 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
2158 cfa_temp.offset.
2160 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
2161 expression yielding a constant. This sets cfa_temp.reg
2162 and cfa_temp.offset.
2164 Rule 5: Create a new register cfa_store used to save items to the
2165 stack.
2167 Rules 10-14: Save a register to the stack. Define offset as the
2168 difference of the original location and cfa_store's
2169 location (or cfa_temp's location if cfa_temp is used).
2171 Rules 16-20: If AND operation happens on sp in prologue, we assume
2172 stack is realigned. We will use a group of DW_OP_XXX
2173 expressions to represent the location of the stored
2174 register instead of CFA+offset.
2176 The Rules
2178 "{a,b}" indicates a choice of a xor b.
2179 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2181 Rule 1:
2182 (set <reg1> <reg2>:cfa.reg)
2183 effects: cfa.reg = <reg1>
2184 cfa.offset unchanged
2185 cfa_temp.reg = <reg1>
2186 cfa_temp.offset = cfa.offset
2188 Rule 2:
2189 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2190 {<const_int>,<reg>:cfa_temp.reg}))
2191 effects: cfa.reg = sp if fp used
2192 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2193 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2194 if cfa_store.reg==sp
2196 Rule 3:
2197 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2198 effects: cfa.reg = fp
2199 cfa_offset += +/- <const_int>
2201 Rule 4:
2202 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2203 constraints: <reg1> != fp
2204 <reg1> != sp
2205 effects: cfa.reg = <reg1>
2206 cfa_temp.reg = <reg1>
2207 cfa_temp.offset = cfa.offset
2209 Rule 5:
2210 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2211 constraints: <reg1> != fp
2212 <reg1> != sp
2213 effects: cfa_store.reg = <reg1>
2214 cfa_store.offset = cfa.offset - cfa_temp.offset
2216 Rule 6:
2217 (set <reg> <const_int>)
2218 effects: cfa_temp.reg = <reg>
2219 cfa_temp.offset = <const_int>
2221 Rule 7:
2222 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2223 effects: cfa_temp.reg = <reg1>
2224 cfa_temp.offset |= <const_int>
2226 Rule 8:
2227 (set <reg> (high <exp>))
2228 effects: none
2230 Rule 9:
2231 (set <reg> (lo_sum <exp> <const_int>))
2232 effects: cfa_temp.reg = <reg>
2233 cfa_temp.offset = <const_int>
2235 Rule 10:
2236 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2237 effects: cfa_store.offset -= <const_int>
2238 cfa.offset = cfa_store.offset if cfa.reg == sp
2239 cfa.reg = sp
2240 cfa.base_offset = -cfa_store.offset
2242 Rule 11:
2243 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2244 effects: cfa_store.offset += -/+ mode_size(mem)
2245 cfa.offset = cfa_store.offset if cfa.reg == sp
2246 cfa.reg = sp
2247 cfa.base_offset = -cfa_store.offset
2249 Rule 12:
2250 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2252 <reg2>)
2253 effects: cfa.reg = <reg1>
2254 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2256 Rule 13:
2257 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2258 effects: cfa.reg = <reg1>
2259 cfa.base_offset = -{cfa_store,cfa_temp}.offset
2261 Rule 14:
2262 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2263 effects: cfa.reg = <reg1>
2264 cfa.base_offset = -cfa_temp.offset
2265 cfa_temp.offset -= mode_size(mem)
2267 Rule 15:
2268 (set <reg> {unspec, unspec_volatile})
2269 effects: target-dependent
2271 Rule 16:
2272 (set sp (and: sp <const_int>))
2273 constraints: cfa_store.reg == sp
2274 effects: current_fde.stack_realign = 1
2275 cfa_store.offset = 0
2276 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2278 Rule 17:
2279 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2280 effects: cfa_store.offset += -/+ mode_size(mem)
2282 Rule 18:
2283 (set (mem ({pre_inc, pre_dec} sp)) fp)
2284 constraints: fde->stack_realign == 1
2285 effects: cfa_store.offset = 0
2286 cfa.reg != HARD_FRAME_POINTER_REGNUM
2288 Rule 19:
2289 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2290 constraints: fde->stack_realign == 1
2291 && cfa.offset == 0
2292 && cfa.indirect == 0
2293 && cfa.reg != HARD_FRAME_POINTER_REGNUM
2294 effects: Use DW_CFA_def_cfa_expression to define cfa
2295 cfa.reg == fde->drap_reg */
2297 static void
2298 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2300 rtx src, dest, span;
2301 HOST_WIDE_INT offset;
2302 dw_fde_ref fde;
2304 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2305 the PARALLEL independently. The first element is always processed if
2306 it is a SET. This is for backward compatibility. Other elements
2307 are processed only if they are SETs and the RTX_FRAME_RELATED_P
2308 flag is set in them. */
2309 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2311 int par_index;
2312 int limit = XVECLEN (expr, 0);
2313 rtx elem;
2315 /* PARALLELs have strict read-modify-write semantics, so we
2316 ought to evaluate every rvalue before changing any lvalue.
2317 It's cumbersome to do that in general, but there's an
2318 easy approximation that is enough for all current users:
2319 handle register saves before register assignments. */
2320 if (GET_CODE (expr) == PARALLEL)
2321 for (par_index = 0; par_index < limit; par_index++)
2323 elem = XVECEXP (expr, 0, par_index);
2324 if (GET_CODE (elem) == SET
2325 && MEM_P (SET_DEST (elem))
2326 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2327 dwarf2out_frame_debug_expr (elem, label);
2330 for (par_index = 0; par_index < limit; par_index++)
2332 elem = XVECEXP (expr, 0, par_index);
2333 if (GET_CODE (elem) == SET
2334 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2335 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2336 dwarf2out_frame_debug_expr (elem, label);
2337 else if (GET_CODE (elem) == SET
2338 && par_index != 0
2339 && !RTX_FRAME_RELATED_P (elem))
2341 /* Stack adjustment combining might combine some post-prologue
2342 stack adjustment into a prologue stack adjustment. */
2343 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2345 if (offset != 0)
2346 dwarf2out_stack_adjust (offset, label);
2349 return;
2352 gcc_assert (GET_CODE (expr) == SET);
2354 src = SET_SRC (expr);
2355 dest = SET_DEST (expr);
2357 if (REG_P (src))
2359 rtx rsi = reg_saved_in (src);
2360 if (rsi)
2361 src = rsi;
2364 fde = current_fde ();
2366 switch (GET_CODE (dest))
2368 case REG:
2369 switch (GET_CODE (src))
2371 /* Setting FP from SP. */
2372 case REG:
2373 if (cfa.reg == (unsigned) REGNO (src))
2375 /* Rule 1 */
2376 /* Update the CFA rule wrt SP or FP. Make sure src is
2377 relative to the current CFA register.
2379 We used to require that dest be either SP or FP, but the
2380 ARM copies SP to a temporary register, and from there to
2381 FP. So we just rely on the backends to only set
2382 RTX_FRAME_RELATED_P on appropriate insns. */
2383 cfa.reg = REGNO (dest);
2384 cfa_temp.reg = cfa.reg;
2385 cfa_temp.offset = cfa.offset;
2387 else
2389 /* Saving a register in a register. */
2390 gcc_assert (!fixed_regs [REGNO (dest)]
2391 /* For the SPARC and its register window. */
2392 || (DWARF_FRAME_REGNUM (REGNO (src))
2393 == DWARF_FRAME_RETURN_COLUMN));
2395 /* After stack is aligned, we can only save SP in FP
2396 if drap register is used. In this case, we have
2397 to restore stack pointer with the CFA value and we
2398 don't generate this DWARF information. */
2399 if (fde
2400 && fde->stack_realign
2401 && REGNO (src) == STACK_POINTER_REGNUM)
2402 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2403 && fde->drap_reg != INVALID_REGNUM
2404 && cfa.reg != REGNO (src));
2405 else
2406 queue_reg_save (label, src, dest, 0);
2408 break;
2410 case PLUS:
2411 case MINUS:
2412 case LO_SUM:
2413 if (dest == stack_pointer_rtx)
2415 /* Rule 2 */
2416 /* Adjusting SP. */
2417 switch (GET_CODE (XEXP (src, 1)))
2419 case CONST_INT:
2420 offset = INTVAL (XEXP (src, 1));
2421 break;
2422 case REG:
2423 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2424 == cfa_temp.reg);
2425 offset = cfa_temp.offset;
2426 break;
2427 default:
2428 gcc_unreachable ();
2431 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2433 /* Restoring SP from FP in the epilogue. */
2434 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2435 cfa.reg = STACK_POINTER_REGNUM;
2437 else if (GET_CODE (src) == LO_SUM)
2438 /* Assume we've set the source reg of the LO_SUM from sp. */
2440 else
2441 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2443 if (GET_CODE (src) != MINUS)
2444 offset = -offset;
2445 if (cfa.reg == STACK_POINTER_REGNUM)
2446 cfa.offset += offset;
2447 if (cfa_store.reg == STACK_POINTER_REGNUM)
2448 cfa_store.offset += offset;
2450 else if (dest == hard_frame_pointer_rtx)
2452 /* Rule 3 */
2453 /* Either setting the FP from an offset of the SP,
2454 or adjusting the FP */
2455 gcc_assert (frame_pointer_needed);
2457 gcc_assert (REG_P (XEXP (src, 0))
2458 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2459 && CONST_INT_P (XEXP (src, 1)));
2460 offset = INTVAL (XEXP (src, 1));
2461 if (GET_CODE (src) != MINUS)
2462 offset = -offset;
2463 cfa.offset += offset;
2464 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2466 else
2468 gcc_assert (GET_CODE (src) != MINUS);
2470 /* Rule 4 */
2471 if (REG_P (XEXP (src, 0))
2472 && REGNO (XEXP (src, 0)) == cfa.reg
2473 && CONST_INT_P (XEXP (src, 1)))
2475 /* Setting a temporary CFA register that will be copied
2476 into the FP later on. */
2477 offset = - INTVAL (XEXP (src, 1));
2478 cfa.offset += offset;
2479 cfa.reg = REGNO (dest);
2480 /* Or used to save regs to the stack. */
2481 cfa_temp.reg = cfa.reg;
2482 cfa_temp.offset = cfa.offset;
2485 /* Rule 5 */
2486 else if (REG_P (XEXP (src, 0))
2487 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2488 && XEXP (src, 1) == stack_pointer_rtx)
2490 /* Setting a scratch register that we will use instead
2491 of SP for saving registers to the stack. */
2492 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2493 cfa_store.reg = REGNO (dest);
2494 cfa_store.offset = cfa.offset - cfa_temp.offset;
2497 /* Rule 9 */
2498 else if (GET_CODE (src) == LO_SUM
2499 && CONST_INT_P (XEXP (src, 1)))
2501 cfa_temp.reg = REGNO (dest);
2502 cfa_temp.offset = INTVAL (XEXP (src, 1));
2504 else
2505 gcc_unreachable ();
2507 break;
2509 /* Rule 6 */
2510 case CONST_INT:
2511 cfa_temp.reg = REGNO (dest);
2512 cfa_temp.offset = INTVAL (src);
2513 break;
2515 /* Rule 7 */
2516 case IOR:
2517 gcc_assert (REG_P (XEXP (src, 0))
2518 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2519 && CONST_INT_P (XEXP (src, 1)));
2521 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2522 cfa_temp.reg = REGNO (dest);
2523 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2524 break;
2526 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2527 which will fill in all of the bits. */
2528 /* Rule 8 */
2529 case HIGH:
2530 break;
2532 /* Rule 15 */
2533 case UNSPEC:
2534 case UNSPEC_VOLATILE:
2535 gcc_assert (targetm.dwarf_handle_frame_unspec);
2536 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2537 return;
2539 /* Rule 16 */
2540 case AND:
2541 /* If this AND operation happens on stack pointer in prologue,
2542 we assume the stack is realigned and we extract the
2543 alignment. */
2544 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2546 /* We interpret reg_save differently with stack_realign set.
2547 Thus we must flush whatever we have queued first. */
2548 dwarf2out_flush_queued_reg_saves ();
2550 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2551 fde->stack_realign = 1;
2552 fde->stack_realignment = INTVAL (XEXP (src, 1));
2553 cfa_store.offset = 0;
2555 if (cfa.reg != STACK_POINTER_REGNUM
2556 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2557 fde->drap_reg = cfa.reg;
2559 return;
2561 default:
2562 gcc_unreachable ();
2565 def_cfa_1 (label, &cfa);
2566 break;
2568 case MEM:
2570 /* Saving a register to the stack. Make sure dest is relative to the
2571 CFA register. */
2572 switch (GET_CODE (XEXP (dest, 0)))
2574 /* Rule 10 */
2575 /* With a push. */
2576 case PRE_MODIFY:
2577 /* We can't handle variable size modifications. */
2578 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2579 == CONST_INT);
2580 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2582 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2583 && cfa_store.reg == STACK_POINTER_REGNUM);
2585 cfa_store.offset += offset;
2586 if (cfa.reg == STACK_POINTER_REGNUM)
2587 cfa.offset = cfa_store.offset;
2589 offset = -cfa_store.offset;
2590 break;
2592 /* Rule 11 */
2593 case PRE_INC:
2594 case PRE_DEC:
2595 offset = GET_MODE_SIZE (GET_MODE (dest));
2596 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2597 offset = -offset;
2599 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2600 == STACK_POINTER_REGNUM)
2601 && cfa_store.reg == STACK_POINTER_REGNUM);
2603 cfa_store.offset += offset;
2605 /* Rule 18: If stack is aligned, we will use FP as a
2606 reference to represent the address of the stored
2607 regiser. */
2608 if (fde
2609 && fde->stack_realign
2610 && src == hard_frame_pointer_rtx)
2612 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2613 cfa_store.offset = 0;
2616 if (cfa.reg == STACK_POINTER_REGNUM)
2617 cfa.offset = cfa_store.offset;
2619 offset = -cfa_store.offset;
2620 break;
2622 /* Rule 12 */
2623 /* With an offset. */
2624 case PLUS:
2625 case MINUS:
2626 case LO_SUM:
2628 int regno;
2630 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2631 && REG_P (XEXP (XEXP (dest, 0), 0)));
2632 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2633 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2634 offset = -offset;
2636 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2638 if (cfa.reg == (unsigned) regno)
2639 offset -= cfa.offset;
2640 else if (cfa_store.reg == (unsigned) regno)
2641 offset -= cfa_store.offset;
2642 else
2644 gcc_assert (cfa_temp.reg == (unsigned) regno);
2645 offset -= cfa_temp.offset;
2648 break;
2650 /* Rule 13 */
2651 /* Without an offset. */
2652 case REG:
2654 int regno = REGNO (XEXP (dest, 0));
2656 if (cfa.reg == (unsigned) regno)
2657 offset = -cfa.offset;
2658 else if (cfa_store.reg == (unsigned) regno)
2659 offset = -cfa_store.offset;
2660 else
2662 gcc_assert (cfa_temp.reg == (unsigned) regno);
2663 offset = -cfa_temp.offset;
2666 break;
2668 /* Rule 14 */
2669 case POST_INC:
2670 gcc_assert (cfa_temp.reg
2671 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2672 offset = -cfa_temp.offset;
2673 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2674 break;
2676 default:
2677 gcc_unreachable ();
2680 /* Rule 17 */
2681 /* If the source operand of this MEM operation is not a
2682 register, basically the source is return address. Here
2683 we only care how much stack grew and we don't save it. */
2684 if (!REG_P (src))
2685 break;
2687 if (REGNO (src) != STACK_POINTER_REGNUM
2688 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2689 && (unsigned) REGNO (src) == cfa.reg)
2691 /* We're storing the current CFA reg into the stack. */
2693 if (cfa.offset == 0)
2695 /* Rule 19 */
2696 /* If stack is aligned, putting CFA reg into stack means
2697 we can no longer use reg + offset to represent CFA.
2698 Here we use DW_CFA_def_cfa_expression instead. The
2699 result of this expression equals to the original CFA
2700 value. */
2701 if (fde
2702 && fde->stack_realign
2703 && cfa.indirect == 0
2704 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2706 dw_cfa_location cfa_exp;
2708 gcc_assert (fde->drap_reg == cfa.reg);
2710 cfa_exp.indirect = 1;
2711 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2712 cfa_exp.base_offset = offset;
2713 cfa_exp.offset = 0;
2715 fde->drap_reg_saved = 1;
2717 def_cfa_1 (label, &cfa_exp);
2718 break;
2721 /* If the source register is exactly the CFA, assume
2722 we're saving SP like any other register; this happens
2723 on the ARM. */
2724 def_cfa_1 (label, &cfa);
2725 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2726 break;
2728 else
2730 /* Otherwise, we'll need to look in the stack to
2731 calculate the CFA. */
2732 rtx x = XEXP (dest, 0);
2734 if (!REG_P (x))
2735 x = XEXP (x, 0);
2736 gcc_assert (REG_P (x));
2738 cfa.reg = REGNO (x);
2739 cfa.base_offset = offset;
2740 cfa.indirect = 1;
2741 def_cfa_1 (label, &cfa);
2742 break;
2746 def_cfa_1 (label, &cfa);
2748 span = targetm.dwarf_register_span (src);
2750 if (!span)
2751 queue_reg_save (label, src, NULL_RTX, offset);
2752 else
2754 /* We have a PARALLEL describing where the contents of SRC
2755 live. Queue register saves for each piece of the
2756 PARALLEL. */
2757 int par_index;
2758 int limit;
2759 HOST_WIDE_INT span_offset = offset;
2761 gcc_assert (GET_CODE (span) == PARALLEL);
2763 limit = XVECLEN (span, 0);
2764 for (par_index = 0; par_index < limit; par_index++)
2766 rtx elem = XVECEXP (span, 0, par_index);
2768 queue_reg_save (label, elem, NULL_RTX, span_offset);
2769 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2773 break;
2775 default:
2776 gcc_unreachable ();
2780 /* Record call frame debugging information for INSN, which either
2781 sets SP or FP (adjusting how we calculate the frame address) or saves a
2782 register to the stack. If INSN is NULL_RTX, initialize our state.
2784 If AFTER_P is false, we're being called before the insn is emitted,
2785 otherwise after. Call instructions get invoked twice. */
2787 void
2788 dwarf2out_frame_debug (rtx insn, bool after_p)
2790 const char *label;
2791 rtx note, n;
2792 bool handled_one = false;
2794 if (insn == NULL_RTX)
2796 size_t i;
2798 /* Flush any queued register saves. */
2799 dwarf2out_flush_queued_reg_saves ();
2801 /* Set up state for generating call frame debug info. */
2802 lookup_cfa (&cfa);
2803 gcc_assert (cfa.reg
2804 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2806 cfa.reg = STACK_POINTER_REGNUM;
2807 cfa_store = cfa;
2808 cfa_temp.reg = -1;
2809 cfa_temp.offset = 0;
2811 for (i = 0; i < num_regs_saved_in_regs; i++)
2813 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2814 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2816 num_regs_saved_in_regs = 0;
2818 if (barrier_args_size)
2820 XDELETEVEC (barrier_args_size);
2821 barrier_args_size = NULL;
2823 return;
2826 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2827 dwarf2out_flush_queued_reg_saves ();
2829 if (!RTX_FRAME_RELATED_P (insn))
2831 /* ??? This should be done unconditionally since stack adjustments
2832 matter if the stack pointer is not the CFA register anymore but
2833 is still used to save registers. */
2834 if (!ACCUMULATE_OUTGOING_ARGS)
2835 dwarf2out_notice_stack_adjust (insn, after_p);
2836 return;
2839 label = dwarf2out_cfi_label (false);
2840 any_cfis_emitted = false;
2842 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2843 switch (REG_NOTE_KIND (note))
2845 case REG_FRAME_RELATED_EXPR:
2846 insn = XEXP (note, 0);
2847 goto found;
2849 case REG_CFA_DEF_CFA:
2850 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2851 handled_one = true;
2852 break;
2854 case REG_CFA_ADJUST_CFA:
2855 n = XEXP (note, 0);
2856 if (n == NULL)
2858 n = PATTERN (insn);
2859 if (GET_CODE (n) == PARALLEL)
2860 n = XVECEXP (n, 0, 0);
2862 dwarf2out_frame_debug_adjust_cfa (n, label);
2863 handled_one = true;
2864 break;
2866 case REG_CFA_OFFSET:
2867 n = XEXP (note, 0);
2868 if (n == NULL)
2869 n = single_set (insn);
2870 dwarf2out_frame_debug_cfa_offset (n, label);
2871 handled_one = true;
2872 break;
2874 case REG_CFA_REGISTER:
2875 n = XEXP (note, 0);
2876 if (n == NULL)
2878 n = PATTERN (insn);
2879 if (GET_CODE (n) == PARALLEL)
2880 n = XVECEXP (n, 0, 0);
2882 dwarf2out_frame_debug_cfa_register (n, label);
2883 handled_one = true;
2884 break;
2886 case REG_CFA_EXPRESSION:
2887 n = XEXP (note, 0);
2888 if (n == NULL)
2889 n = single_set (insn);
2890 dwarf2out_frame_debug_cfa_expression (n, label);
2891 handled_one = true;
2892 break;
2894 case REG_CFA_RESTORE:
2895 n = XEXP (note, 0);
2896 if (n == NULL)
2898 n = PATTERN (insn);
2899 if (GET_CODE (n) == PARALLEL)
2900 n = XVECEXP (n, 0, 0);
2901 n = XEXP (n, 0);
2903 dwarf2out_frame_debug_cfa_restore (n, label);
2904 handled_one = true;
2905 break;
2907 case REG_CFA_SET_VDRAP:
2908 n = XEXP (note, 0);
2909 if (REG_P (n))
2911 dw_fde_ref fde = current_fde ();
2912 if (fde)
2914 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2915 if (REG_P (n))
2916 fde->vdrap_reg = REGNO (n);
2919 handled_one = true;
2920 break;
2922 default:
2923 break;
2925 if (handled_one)
2927 if (any_cfis_emitted)
2928 dwarf2out_flush_queued_reg_saves ();
2929 return;
2932 insn = PATTERN (insn);
2933 found:
2934 dwarf2out_frame_debug_expr (insn, label);
2936 /* Check again. A parallel can save and update the same register.
2937 We could probably check just once, here, but this is safer than
2938 removing the check above. */
2939 if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2940 dwarf2out_flush_queued_reg_saves ();
2943 /* Determine if we need to save and restore CFI information around this
2944 epilogue. If SIBCALL is true, then this is a sibcall epilogue. If
2945 we do need to save/restore, then emit the save now, and insert a
2946 NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream. */
2948 void
2949 dwarf2out_cfi_begin_epilogue (rtx insn)
2951 bool saw_frp = false;
2952 rtx i;
2954 /* Scan forward to the return insn, noticing if there are possible
2955 frame related insns. */
2956 for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2958 if (!INSN_P (i))
2959 continue;
2961 /* Look for both regular and sibcalls to end the block. */
2962 if (returnjump_p (i))
2963 break;
2964 if (CALL_P (i) && SIBLING_CALL_P (i))
2965 break;
2967 if (GET_CODE (PATTERN (i)) == SEQUENCE)
2969 int idx;
2970 rtx seq = PATTERN (i);
2972 if (returnjump_p (XVECEXP (seq, 0, 0)))
2973 break;
2974 if (CALL_P (XVECEXP (seq, 0, 0))
2975 && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2976 break;
2978 for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2979 if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2980 saw_frp = true;
2983 if (RTX_FRAME_RELATED_P (i))
2984 saw_frp = true;
2987 /* If the port doesn't emit epilogue unwind info, we don't need a
2988 save/restore pair. */
2989 if (!saw_frp)
2990 return;
2992 /* Otherwise, search forward to see if the return insn was the last
2993 basic block of the function. If so, we don't need save/restore. */
2994 gcc_assert (i != NULL);
2995 i = next_real_insn (i);
2996 if (i == NULL)
2997 return;
2999 /* Insert the restore before that next real insn in the stream, and before
3000 a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3001 properly nested. This should be after any label or alignment. This
3002 will be pushed into the CFI stream by the function below. */
3003 while (1)
3005 rtx p = PREV_INSN (i);
3006 if (!NOTE_P (p))
3007 break;
3008 if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3009 break;
3010 i = p;
3012 emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3014 emit_cfa_remember = true;
3016 /* And emulate the state save. */
3017 gcc_assert (!cfa_remember.in_use);
3018 cfa_remember = cfa;
3019 cfa_remember.in_use = 1;
3022 /* A "subroutine" of dwarf2out_cfi_begin_epilogue. Emit the restore
3023 required. */
3025 void
3026 dwarf2out_frame_debug_restore_state (void)
3028 dw_cfi_ref cfi = new_cfi ();
3029 const char *label = dwarf2out_cfi_label (false);
3031 cfi->dw_cfi_opc = DW_CFA_restore_state;
3032 add_fde_cfi (label, cfi);
3034 gcc_assert (cfa_remember.in_use);
3035 cfa = cfa_remember;
3036 cfa_remember.in_use = 0;
3039 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
3040 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3041 (enum dwarf_call_frame_info cfi);
3043 static enum dw_cfi_oprnd_type
3044 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3046 switch (cfi)
3048 case DW_CFA_nop:
3049 case DW_CFA_GNU_window_save:
3050 case DW_CFA_remember_state:
3051 case DW_CFA_restore_state:
3052 return dw_cfi_oprnd_unused;
3054 case DW_CFA_set_loc:
3055 case DW_CFA_advance_loc1:
3056 case DW_CFA_advance_loc2:
3057 case DW_CFA_advance_loc4:
3058 case DW_CFA_MIPS_advance_loc8:
3059 return dw_cfi_oprnd_addr;
3061 case DW_CFA_offset:
3062 case DW_CFA_offset_extended:
3063 case DW_CFA_def_cfa:
3064 case DW_CFA_offset_extended_sf:
3065 case DW_CFA_def_cfa_sf:
3066 case DW_CFA_restore:
3067 case DW_CFA_restore_extended:
3068 case DW_CFA_undefined:
3069 case DW_CFA_same_value:
3070 case DW_CFA_def_cfa_register:
3071 case DW_CFA_register:
3072 case DW_CFA_expression:
3073 return dw_cfi_oprnd_reg_num;
3075 case DW_CFA_def_cfa_offset:
3076 case DW_CFA_GNU_args_size:
3077 case DW_CFA_def_cfa_offset_sf:
3078 return dw_cfi_oprnd_offset;
3080 case DW_CFA_def_cfa_expression:
3081 return dw_cfi_oprnd_loc;
3083 default:
3084 gcc_unreachable ();
3088 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
3089 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3090 (enum dwarf_call_frame_info cfi);
3092 static enum dw_cfi_oprnd_type
3093 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3095 switch (cfi)
3097 case DW_CFA_def_cfa:
3098 case DW_CFA_def_cfa_sf:
3099 case DW_CFA_offset:
3100 case DW_CFA_offset_extended_sf:
3101 case DW_CFA_offset_extended:
3102 return dw_cfi_oprnd_offset;
3104 case DW_CFA_register:
3105 return dw_cfi_oprnd_reg_num;
3107 case DW_CFA_expression:
3108 return dw_cfi_oprnd_loc;
3110 default:
3111 return dw_cfi_oprnd_unused;
3115 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
3116 switch to the data section instead, and write out a synthetic start label
3117 for collect2 the first time around. */
3119 static void
3120 switch_to_eh_frame_section (bool back)
3122 tree label;
3124 #ifdef EH_FRAME_SECTION_NAME
3125 if (eh_frame_section == 0)
3127 int flags;
3129 if (EH_TABLES_CAN_BE_READ_ONLY)
3131 int fde_encoding;
3132 int per_encoding;
3133 int lsda_encoding;
3135 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3136 /*global=*/0);
3137 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3138 /*global=*/1);
3139 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3140 /*global=*/0);
3141 flags = ((! flag_pic
3142 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3143 && (fde_encoding & 0x70) != DW_EH_PE_aligned
3144 && (per_encoding & 0x70) != DW_EH_PE_absptr
3145 && (per_encoding & 0x70) != DW_EH_PE_aligned
3146 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3147 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3148 ? 0 : SECTION_WRITE);
3150 else
3151 flags = SECTION_WRITE;
3152 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3154 #endif /* EH_FRAME_SECTION_NAME */
3156 if (eh_frame_section)
3157 switch_to_section (eh_frame_section);
3158 else
3160 /* We have no special eh_frame section. Put the information in
3161 the data section and emit special labels to guide collect2. */
3162 switch_to_section (data_section);
3164 if (!back)
3166 label = get_file_function_name ("F");
3167 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3168 targetm.asm_out.globalize_label (asm_out_file,
3169 IDENTIFIER_POINTER (label));
3170 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3175 /* Switch [BACK] to the eh or debug frame table section, depending on
3176 FOR_EH. */
3178 static void
3179 switch_to_frame_table_section (int for_eh, bool back)
3181 if (for_eh)
3182 switch_to_eh_frame_section (back);
3183 else
3185 if (!debug_frame_section)
3186 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3187 SECTION_DEBUG, NULL);
3188 switch_to_section (debug_frame_section);
3192 /* Output a Call Frame Information opcode and its operand(s). */
3194 static void
3195 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3197 unsigned long r;
3198 HOST_WIDE_INT off;
3200 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3201 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3202 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3203 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3204 ((unsigned HOST_WIDE_INT)
3205 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3206 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3208 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3209 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3210 "DW_CFA_offset, column %#lx", r);
3211 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3212 dw2_asm_output_data_uleb128 (off, NULL);
3214 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3216 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3217 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3218 "DW_CFA_restore, column %#lx", r);
3220 else
3222 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3223 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3225 switch (cfi->dw_cfi_opc)
3227 case DW_CFA_set_loc:
3228 if (for_eh)
3229 dw2_asm_output_encoded_addr_rtx (
3230 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3231 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3232 false, NULL);
3233 else
3234 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3235 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3236 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3237 break;
3239 case DW_CFA_advance_loc1:
3240 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3241 fde->dw_fde_current_label, NULL);
3242 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3243 break;
3245 case DW_CFA_advance_loc2:
3246 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3247 fde->dw_fde_current_label, NULL);
3248 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3249 break;
3251 case DW_CFA_advance_loc4:
3252 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3253 fde->dw_fde_current_label, NULL);
3254 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3255 break;
3257 case DW_CFA_MIPS_advance_loc8:
3258 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3259 fde->dw_fde_current_label, NULL);
3260 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3261 break;
3263 case DW_CFA_offset_extended:
3264 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3265 dw2_asm_output_data_uleb128 (r, NULL);
3266 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3267 dw2_asm_output_data_uleb128 (off, NULL);
3268 break;
3270 case DW_CFA_def_cfa:
3271 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3272 dw2_asm_output_data_uleb128 (r, NULL);
3273 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3274 break;
3276 case DW_CFA_offset_extended_sf:
3277 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3278 dw2_asm_output_data_uleb128 (r, NULL);
3279 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3280 dw2_asm_output_data_sleb128 (off, NULL);
3281 break;
3283 case DW_CFA_def_cfa_sf:
3284 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3285 dw2_asm_output_data_uleb128 (r, NULL);
3286 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3287 dw2_asm_output_data_sleb128 (off, NULL);
3288 break;
3290 case DW_CFA_restore_extended:
3291 case DW_CFA_undefined:
3292 case DW_CFA_same_value:
3293 case DW_CFA_def_cfa_register:
3294 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3295 dw2_asm_output_data_uleb128 (r, NULL);
3296 break;
3298 case DW_CFA_register:
3299 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3300 dw2_asm_output_data_uleb128 (r, NULL);
3301 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3302 dw2_asm_output_data_uleb128 (r, NULL);
3303 break;
3305 case DW_CFA_def_cfa_offset:
3306 case DW_CFA_GNU_args_size:
3307 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3308 break;
3310 case DW_CFA_def_cfa_offset_sf:
3311 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3312 dw2_asm_output_data_sleb128 (off, NULL);
3313 break;
3315 case DW_CFA_GNU_window_save:
3316 break;
3318 case DW_CFA_def_cfa_expression:
3319 case DW_CFA_expression:
3320 output_cfa_loc (cfi);
3321 break;
3323 case DW_CFA_GNU_negative_offset_extended:
3324 /* Obsoleted by DW_CFA_offset_extended_sf. */
3325 gcc_unreachable ();
3327 default:
3328 break;
3333 /* Similar, but do it via assembler directives instead. */
3335 static void
3336 output_cfi_directive (dw_cfi_ref cfi)
3338 unsigned long r, r2;
3340 switch (cfi->dw_cfi_opc)
3342 case DW_CFA_advance_loc:
3343 case DW_CFA_advance_loc1:
3344 case DW_CFA_advance_loc2:
3345 case DW_CFA_advance_loc4:
3346 case DW_CFA_MIPS_advance_loc8:
3347 case DW_CFA_set_loc:
3348 /* Should only be created by add_fde_cfi in a code path not
3349 followed when emitting via directives. The assembler is
3350 going to take care of this for us. */
3351 gcc_unreachable ();
3353 case DW_CFA_offset:
3354 case DW_CFA_offset_extended:
3355 case DW_CFA_offset_extended_sf:
3356 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3357 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3358 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3359 break;
3361 case DW_CFA_restore:
3362 case DW_CFA_restore_extended:
3363 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3364 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3365 break;
3367 case DW_CFA_undefined:
3368 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3369 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3370 break;
3372 case DW_CFA_same_value:
3373 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3374 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3375 break;
3377 case DW_CFA_def_cfa:
3378 case DW_CFA_def_cfa_sf:
3379 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3380 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3381 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3382 break;
3384 case DW_CFA_def_cfa_register:
3385 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3386 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3387 break;
3389 case DW_CFA_register:
3390 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3391 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3392 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3393 break;
3395 case DW_CFA_def_cfa_offset:
3396 case DW_CFA_def_cfa_offset_sf:
3397 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3398 HOST_WIDE_INT_PRINT_DEC"\n",
3399 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3400 break;
3402 case DW_CFA_remember_state:
3403 fprintf (asm_out_file, "\t.cfi_remember_state\n");
3404 break;
3405 case DW_CFA_restore_state:
3406 fprintf (asm_out_file, "\t.cfi_restore_state\n");
3407 break;
3409 case DW_CFA_GNU_args_size:
3410 fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3411 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3412 if (flag_debug_asm)
3413 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3414 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3415 fputc ('\n', asm_out_file);
3416 break;
3418 case DW_CFA_GNU_window_save:
3419 fprintf (asm_out_file, "\t.cfi_window_save\n");
3420 break;
3422 case DW_CFA_def_cfa_expression:
3423 case DW_CFA_expression:
3424 fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3425 output_cfa_loc_raw (cfi);
3426 fputc ('\n', asm_out_file);
3427 break;
3429 default:
3430 gcc_unreachable ();
3434 DEF_VEC_P (dw_cfi_ref);
3435 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3437 /* Output CFIs to bring current FDE to the same state as after executing
3438 CFIs in CFI chain. DO_CFI_ASM is true if .cfi_* directives shall
3439 be emitted, false otherwise. If it is false, FDE and FOR_EH are the
3440 other arguments to pass to output_cfi. */
3442 static void
3443 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3445 struct dw_cfi_struct cfi_buf;
3446 dw_cfi_ref cfi2;
3447 dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3448 VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3449 unsigned int len, idx;
3451 for (;; cfi = cfi->dw_cfi_next)
3452 switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3454 case DW_CFA_advance_loc:
3455 case DW_CFA_advance_loc1:
3456 case DW_CFA_advance_loc2:
3457 case DW_CFA_advance_loc4:
3458 case DW_CFA_MIPS_advance_loc8:
3459 case DW_CFA_set_loc:
3460 /* All advances should be ignored. */
3461 break;
3462 case DW_CFA_remember_state:
3464 dw_cfi_ref args_size = cfi_args_size;
3466 /* Skip everything between .cfi_remember_state and
3467 .cfi_restore_state. */
3468 for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3469 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3470 break;
3471 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3472 args_size = cfi2;
3473 else
3474 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3476 if (cfi2 == NULL)
3477 goto flush_all;
3478 else
3480 cfi = cfi2;
3481 cfi_args_size = args_size;
3483 break;
3485 case DW_CFA_GNU_args_size:
3486 cfi_args_size = cfi;
3487 break;
3488 case DW_CFA_GNU_window_save:
3489 goto flush_all;
3490 case DW_CFA_offset:
3491 case DW_CFA_offset_extended:
3492 case DW_CFA_offset_extended_sf:
3493 case DW_CFA_restore:
3494 case DW_CFA_restore_extended:
3495 case DW_CFA_undefined:
3496 case DW_CFA_same_value:
3497 case DW_CFA_register:
3498 case DW_CFA_val_offset:
3499 case DW_CFA_val_offset_sf:
3500 case DW_CFA_expression:
3501 case DW_CFA_val_expression:
3502 case DW_CFA_GNU_negative_offset_extended:
3503 if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3504 VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3505 cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3506 VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3507 break;
3508 case DW_CFA_def_cfa:
3509 case DW_CFA_def_cfa_sf:
3510 case DW_CFA_def_cfa_expression:
3511 cfi_cfa = cfi;
3512 cfi_cfa_offset = cfi;
3513 break;
3514 case DW_CFA_def_cfa_register:
3515 cfi_cfa = cfi;
3516 break;
3517 case DW_CFA_def_cfa_offset:
3518 case DW_CFA_def_cfa_offset_sf:
3519 cfi_cfa_offset = cfi;
3520 break;
3521 case DW_CFA_nop:
3522 gcc_assert (cfi == NULL);
3523 flush_all:
3524 len = VEC_length (dw_cfi_ref, regs);
3525 for (idx = 0; idx < len; idx++)
3527 cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3528 if (cfi2 != NULL
3529 && cfi2->dw_cfi_opc != DW_CFA_restore
3530 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3532 if (do_cfi_asm)
3533 output_cfi_directive (cfi2);
3534 else
3535 output_cfi (cfi2, fde, for_eh);
3538 if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3540 gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3541 cfi_buf = *cfi_cfa;
3542 switch (cfi_cfa_offset->dw_cfi_opc)
3544 case DW_CFA_def_cfa_offset:
3545 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3546 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3547 break;
3548 case DW_CFA_def_cfa_offset_sf:
3549 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3550 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3551 break;
3552 case DW_CFA_def_cfa:
3553 case DW_CFA_def_cfa_sf:
3554 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3555 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3556 break;
3557 default:
3558 gcc_unreachable ();
3560 cfi_cfa = &cfi_buf;
3562 else if (cfi_cfa_offset)
3563 cfi_cfa = cfi_cfa_offset;
3564 if (cfi_cfa)
3566 if (do_cfi_asm)
3567 output_cfi_directive (cfi_cfa);
3568 else
3569 output_cfi (cfi_cfa, fde, for_eh);
3571 cfi_cfa = NULL;
3572 cfi_cfa_offset = NULL;
3573 if (cfi_args_size
3574 && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3576 if (do_cfi_asm)
3577 output_cfi_directive (cfi_args_size);
3578 else
3579 output_cfi (cfi_args_size, fde, for_eh);
3581 cfi_args_size = NULL;
3582 if (cfi == NULL)
3584 VEC_free (dw_cfi_ref, heap, regs);
3585 return;
3587 else if (do_cfi_asm)
3588 output_cfi_directive (cfi);
3589 else
3590 output_cfi (cfi, fde, for_eh);
3591 break;
3592 default:
3593 gcc_unreachable ();
3597 /* Output one FDE. */
3599 static void
3600 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3601 char *section_start_label, int fde_encoding, char *augmentation,
3602 bool any_lsda_needed, int lsda_encoding)
3604 const char *begin, *end;
3605 static unsigned int j;
3606 char l1[20], l2[20];
3607 dw_cfi_ref cfi;
3609 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3610 /* empty */ 0);
3611 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3612 for_eh + j);
3613 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3614 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3615 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3616 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3617 " indicating 64-bit DWARF extension");
3618 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3619 "FDE Length");
3620 ASM_OUTPUT_LABEL (asm_out_file, l1);
3622 if (for_eh)
3623 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3624 else
3625 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3626 debug_frame_section, "FDE CIE offset");
3628 if (!fde->dw_fde_switched_sections)
3630 begin = fde->dw_fde_begin;
3631 end = fde->dw_fde_end;
3633 else
3635 /* For the first section, prefer dw_fde_begin over
3636 dw_fde_{hot,cold}_section_label, as the latter
3637 might be separated from the real start of the
3638 function by alignment padding. */
3639 if (!second)
3640 begin = fde->dw_fde_begin;
3641 else if (fde->dw_fde_switched_cold_to_hot)
3642 begin = fde->dw_fde_hot_section_label;
3643 else
3644 begin = fde->dw_fde_unlikely_section_label;
3645 if (second ^ fde->dw_fde_switched_cold_to_hot)
3646 end = fde->dw_fde_unlikely_section_end_label;
3647 else
3648 end = fde->dw_fde_hot_section_end_label;
3651 if (for_eh)
3653 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3654 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3655 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3656 "FDE initial location");
3657 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3658 end, begin, "FDE address range");
3660 else
3662 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3663 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3666 if (augmentation[0])
3668 if (any_lsda_needed)
3670 int size = size_of_encoded_value (lsda_encoding);
3672 if (lsda_encoding == DW_EH_PE_aligned)
3674 int offset = ( 4 /* Length */
3675 + 4 /* CIE offset */
3676 + 2 * size_of_encoded_value (fde_encoding)
3677 + 1 /* Augmentation size */ );
3678 int pad = -offset & (PTR_SIZE - 1);
3680 size += pad;
3681 gcc_assert (size_of_uleb128 (size) == 1);
3684 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3686 if (fde->uses_eh_lsda)
3688 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3689 fde->funcdef_number);
3690 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3691 gen_rtx_SYMBOL_REF (Pmode, l1),
3692 false,
3693 "Language Specific Data Area");
3695 else
3697 if (lsda_encoding == DW_EH_PE_aligned)
3698 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3699 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3700 "Language Specific Data Area (none)");
3703 else
3704 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3707 /* Loop through the Call Frame Instructions associated with
3708 this FDE. */
3709 fde->dw_fde_current_label = begin;
3710 if (!fde->dw_fde_switched_sections)
3711 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3712 output_cfi (cfi, fde, for_eh);
3713 else if (!second)
3715 if (fde->dw_fde_switch_cfi)
3716 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3718 output_cfi (cfi, fde, for_eh);
3719 if (cfi == fde->dw_fde_switch_cfi)
3720 break;
3723 else
3725 dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3727 if (fde->dw_fde_switch_cfi)
3729 cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3730 fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3731 output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3732 fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3734 for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3735 output_cfi (cfi, fde, for_eh);
3738 /* If we are to emit a ref/link from function bodies to their frame tables,
3739 do it now. This is typically performed to make sure that tables
3740 associated with functions are dragged with them and not discarded in
3741 garbage collecting links. We need to do this on a per function basis to
3742 cope with -ffunction-sections. */
3744 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3745 /* Switch to the function section, emit the ref to the tables, and
3746 switch *back* into the table section. */
3747 switch_to_section (function_section (fde->decl));
3748 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3749 switch_to_frame_table_section (for_eh, true);
3750 #endif
3752 /* Pad the FDE out to an address sized boundary. */
3753 ASM_OUTPUT_ALIGN (asm_out_file,
3754 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3755 ASM_OUTPUT_LABEL (asm_out_file, l2);
3757 j += 2;
3760 /* Return true if frame description entry FDE is needed for EH. */
3762 static bool
3763 fde_needed_for_eh_p (dw_fde_ref fde)
3765 if (flag_asynchronous_unwind_tables)
3766 return true;
3768 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3769 return true;
3771 if (fde->uses_eh_lsda)
3772 return true;
3774 /* If exceptions are enabled, we have collected nothrow info. */
3775 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3776 return false;
3778 return true;
3781 /* Output the call frame information used to record information
3782 that relates to calculating the frame pointer, and records the
3783 location of saved registers. */
3785 static void
3786 output_call_frame_info (int for_eh)
3788 unsigned int i;
3789 dw_fde_ref fde;
3790 dw_cfi_ref cfi;
3791 char l1[20], l2[20], section_start_label[20];
3792 bool any_lsda_needed = false;
3793 char augmentation[6];
3794 int augmentation_size;
3795 int fde_encoding = DW_EH_PE_absptr;
3796 int per_encoding = DW_EH_PE_absptr;
3797 int lsda_encoding = DW_EH_PE_absptr;
3798 int return_reg;
3799 rtx personality = NULL;
3800 int dw_cie_version;
3802 /* Don't emit a CIE if there won't be any FDEs. */
3803 if (fde_table_in_use == 0)
3804 return;
3806 /* Nothing to do if the assembler's doing it all. */
3807 if (dwarf2out_do_cfi_asm ())
3808 return;
3810 /* If we don't have any functions we'll want to unwind out of, don't emit
3811 any EH unwind information. If we make FDEs linkonce, we may have to
3812 emit an empty label for an FDE that wouldn't otherwise be emitted. We
3813 want to avoid having an FDE kept around when the function it refers to
3814 is discarded. Example where this matters: a primary function template
3815 in C++ requires EH information, an explicit specialization doesn't. */
3816 if (for_eh)
3818 bool any_eh_needed = false;
3820 for (i = 0; i < fde_table_in_use; i++)
3821 if (fde_table[i].uses_eh_lsda)
3822 any_eh_needed = any_lsda_needed = true;
3823 else if (fde_needed_for_eh_p (&fde_table[i]))
3824 any_eh_needed = true;
3825 else if (TARGET_USES_WEAK_UNWIND_INFO)
3826 targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3827 1, 1);
3829 if (!any_eh_needed)
3830 return;
3833 /* We're going to be generating comments, so turn on app. */
3834 if (flag_debug_asm)
3835 app_enable ();
3837 /* Switch to the proper frame section, first time. */
3838 switch_to_frame_table_section (for_eh, false);
3840 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3841 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3843 /* Output the CIE. */
3844 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3845 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3846 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3847 dw2_asm_output_data (4, 0xffffffff,
3848 "Initial length escape value indicating 64-bit DWARF extension");
3849 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3850 "Length of Common Information Entry");
3851 ASM_OUTPUT_LABEL (asm_out_file, l1);
3853 /* Now that the CIE pointer is PC-relative for EH,
3854 use 0 to identify the CIE. */
3855 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3856 (for_eh ? 0 : DWARF_CIE_ID),
3857 "CIE Identifier Tag");
3859 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3860 use CIE version 1, unless that would produce incorrect results
3861 due to overflowing the return register column. */
3862 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3863 dw_cie_version = 1;
3864 if (return_reg >= 256 || dwarf_version > 2)
3865 dw_cie_version = 3;
3866 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3868 augmentation[0] = 0;
3869 augmentation_size = 0;
3871 personality = current_unit_personality;
3872 if (for_eh)
3874 char *p;
3876 /* Augmentation:
3877 z Indicates that a uleb128 is present to size the
3878 augmentation section.
3879 L Indicates the encoding (and thus presence) of
3880 an LSDA pointer in the FDE augmentation.
3881 R Indicates a non-default pointer encoding for
3882 FDE code pointers.
3883 P Indicates the presence of an encoding + language
3884 personality routine in the CIE augmentation. */
3886 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3887 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3888 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3890 p = augmentation + 1;
3891 if (personality)
3893 *p++ = 'P';
3894 augmentation_size += 1 + size_of_encoded_value (per_encoding);
3895 assemble_external_libcall (personality);
3897 if (any_lsda_needed)
3899 *p++ = 'L';
3900 augmentation_size += 1;
3902 if (fde_encoding != DW_EH_PE_absptr)
3904 *p++ = 'R';
3905 augmentation_size += 1;
3907 if (p > augmentation + 1)
3909 augmentation[0] = 'z';
3910 *p = '\0';
3913 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
3914 if (personality && per_encoding == DW_EH_PE_aligned)
3916 int offset = ( 4 /* Length */
3917 + 4 /* CIE Id */
3918 + 1 /* CIE version */
3919 + strlen (augmentation) + 1 /* Augmentation */
3920 + size_of_uleb128 (1) /* Code alignment */
3921 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3922 + 1 /* RA column */
3923 + 1 /* Augmentation size */
3924 + 1 /* Personality encoding */ );
3925 int pad = -offset & (PTR_SIZE - 1);
3927 augmentation_size += pad;
3929 /* Augmentations should be small, so there's scarce need to
3930 iterate for a solution. Die if we exceed one uleb128 byte. */
3931 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3935 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3936 if (dw_cie_version >= 4)
3938 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3939 dw2_asm_output_data (1, 0, "CIE Segment Size");
3941 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3942 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3943 "CIE Data Alignment Factor");
3945 if (dw_cie_version == 1)
3946 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3947 else
3948 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3950 if (augmentation[0])
3952 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3953 if (personality)
3955 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3956 eh_data_format_name (per_encoding));
3957 dw2_asm_output_encoded_addr_rtx (per_encoding,
3958 personality,
3959 true, NULL);
3962 if (any_lsda_needed)
3963 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3964 eh_data_format_name (lsda_encoding));
3966 if (fde_encoding != DW_EH_PE_absptr)
3967 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3968 eh_data_format_name (fde_encoding));
3971 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3972 output_cfi (cfi, NULL, for_eh);
3974 /* Pad the CIE out to an address sized boundary. */
3975 ASM_OUTPUT_ALIGN (asm_out_file,
3976 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3977 ASM_OUTPUT_LABEL (asm_out_file, l2);
3979 /* Loop through all of the FDE's. */
3980 for (i = 0; i < fde_table_in_use; i++)
3982 unsigned int k;
3983 fde = &fde_table[i];
3985 /* Don't emit EH unwind info for leaf functions that don't need it. */
3986 if (for_eh && !fde_needed_for_eh_p (fde))
3987 continue;
3989 for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3990 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3991 augmentation, any_lsda_needed, lsda_encoding);
3994 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3995 dw2_asm_output_data (4, 0, "End of Table");
3996 #ifdef MIPS_DEBUGGING_INFO
3997 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3998 get a value of 0. Putting .align 0 after the label fixes it. */
3999 ASM_OUTPUT_ALIGN (asm_out_file, 0);
4000 #endif
4002 /* Turn off app to make assembly quicker. */
4003 if (flag_debug_asm)
4004 app_disable ();
4007 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
4009 static void
4010 dwarf2out_do_cfi_startproc (bool second)
4012 int enc;
4013 rtx ref;
4014 rtx personality = get_personality_function (current_function_decl);
4016 fprintf (asm_out_file, "\t.cfi_startproc\n");
4018 if (personality)
4020 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4021 ref = personality;
4023 /* ??? The GAS support isn't entirely consistent. We have to
4024 handle indirect support ourselves, but PC-relative is done
4025 in the assembler. Further, the assembler can't handle any
4026 of the weirder relocation types. */
4027 if (enc & DW_EH_PE_indirect)
4028 ref = dw2_force_const_mem (ref, true);
4030 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4031 output_addr_const (asm_out_file, ref);
4032 fputc ('\n', asm_out_file);
4035 if (crtl->uses_eh_lsda)
4037 char lab[20];
4039 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4040 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4041 current_function_funcdef_no);
4042 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4043 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4045 if (enc & DW_EH_PE_indirect)
4046 ref = dw2_force_const_mem (ref, true);
4048 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4049 output_addr_const (asm_out_file, ref);
4050 fputc ('\n', asm_out_file);
4054 /* Output a marker (i.e. a label) for the beginning of a function, before
4055 the prologue. */
4057 void
4058 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4059 const char *file ATTRIBUTE_UNUSED)
4061 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4062 char * dup_label;
4063 dw_fde_ref fde;
4064 section *fnsec;
4065 bool do_frame;
4067 current_function_func_begin_label = NULL;
4069 do_frame = dwarf2out_do_frame ();
4071 /* ??? current_function_func_begin_label is also used by except.c for
4072 call-site information. We must emit this label if it might be used. */
4073 if (!do_frame
4074 && (!flag_exceptions
4075 || targetm.except_unwind_info (&global_options) != UI_TARGET))
4076 return;
4078 fnsec = function_section (current_function_decl);
4079 switch_to_section (fnsec);
4080 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4081 current_function_funcdef_no);
4082 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4083 current_function_funcdef_no);
4084 dup_label = xstrdup (label);
4085 current_function_func_begin_label = dup_label;
4087 /* We can elide the fde allocation if we're not emitting debug info. */
4088 if (!do_frame)
4089 return;
4091 /* Expand the fde table if necessary. */
4092 if (fde_table_in_use == fde_table_allocated)
4094 fde_table_allocated += FDE_TABLE_INCREMENT;
4095 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4096 memset (fde_table + fde_table_in_use, 0,
4097 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4100 /* Record the FDE associated with this function. */
4101 current_funcdef_fde = fde_table_in_use;
4103 /* Add the new FDE at the end of the fde_table. */
4104 fde = &fde_table[fde_table_in_use++];
4105 fde->decl = current_function_decl;
4106 fde->dw_fde_begin = dup_label;
4107 fde->dw_fde_current_label = dup_label;
4108 fde->dw_fde_hot_section_label = NULL;
4109 fde->dw_fde_hot_section_end_label = NULL;
4110 fde->dw_fde_unlikely_section_label = NULL;
4111 fde->dw_fde_unlikely_section_end_label = NULL;
4112 fde->dw_fde_switched_sections = 0;
4113 fde->dw_fde_switched_cold_to_hot = 0;
4114 fde->dw_fde_end = NULL;
4115 fde->dw_fde_vms_end_prologue = NULL;
4116 fde->dw_fde_vms_begin_epilogue = NULL;
4117 fde->dw_fde_cfi = NULL;
4118 fde->dw_fde_switch_cfi = NULL;
4119 fde->funcdef_number = current_function_funcdef_no;
4120 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4121 fde->uses_eh_lsda = crtl->uses_eh_lsda;
4122 fde->nothrow = crtl->nothrow;
4123 fde->drap_reg = INVALID_REGNUM;
4124 fde->vdrap_reg = INVALID_REGNUM;
4125 if (flag_reorder_blocks_and_partition)
4127 section *unlikelysec;
4128 if (first_function_block_is_cold)
4129 fde->in_std_section = 1;
4130 else
4131 fde->in_std_section
4132 = (fnsec == text_section
4133 || (cold_text_section && fnsec == cold_text_section));
4134 unlikelysec = unlikely_text_section ();
4135 fde->cold_in_std_section
4136 = (unlikelysec == text_section
4137 || (cold_text_section && unlikelysec == cold_text_section));
4139 else
4141 fde->in_std_section
4142 = (fnsec == text_section
4143 || (cold_text_section && fnsec == cold_text_section));
4144 fde->cold_in_std_section = 0;
4147 args_size = old_args_size = 0;
4149 /* We only want to output line number information for the genuine dwarf2
4150 prologue case, not the eh frame case. */
4151 #ifdef DWARF2_DEBUGGING_INFO
4152 if (file)
4153 dwarf2out_source_line (line, file, 0, true);
4154 #endif
4156 if (dwarf2out_do_cfi_asm ())
4157 dwarf2out_do_cfi_startproc (false);
4158 else
4160 rtx personality = get_personality_function (current_function_decl);
4161 if (!current_unit_personality)
4162 current_unit_personality = personality;
4164 /* We cannot keep a current personality per function as without CFI
4165 asm, at the point where we emit the CFI data, there is no current
4166 function anymore. */
4167 if (personality && current_unit_personality != personality)
4168 sorry ("multiple EH personalities are supported only with assemblers "
4169 "supporting .cfi_personality directive");
4173 /* Output a marker (i.e. a label) for the end of the generated code
4174 for a function prologue. This gets called *after* the prologue code has
4175 been generated. */
4177 void
4178 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4179 const char *file ATTRIBUTE_UNUSED)
4181 dw_fde_ref fde;
4182 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4184 /* Output a label to mark the endpoint of the code generated for this
4185 function. */
4186 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4187 current_function_funcdef_no);
4188 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4189 current_function_funcdef_no);
4190 fde = &fde_table[fde_table_in_use - 1];
4191 fde->dw_fde_vms_end_prologue = xstrdup (label);
4194 /* Output a marker (i.e. a label) for the beginning of the generated code
4195 for a function epilogue. This gets called *before* the prologue code has
4196 been generated. */
4198 void
4199 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4200 const char *file ATTRIBUTE_UNUSED)
4202 dw_fde_ref fde;
4203 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4205 fde = &fde_table[fde_table_in_use - 1];
4206 if (fde->dw_fde_vms_begin_epilogue)
4207 return;
4209 /* Output a label to mark the endpoint of the code generated for this
4210 function. */
4211 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4212 current_function_funcdef_no);
4213 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4214 current_function_funcdef_no);
4215 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4218 /* Output a marker (i.e. a label) for the absolute end of the generated code
4219 for a function definition. This gets called *after* the epilogue code has
4220 been generated. */
4222 void
4223 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4224 const char *file ATTRIBUTE_UNUSED)
4226 dw_fde_ref fde;
4227 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4229 last_var_location_insn = NULL_RTX;
4231 if (dwarf2out_do_cfi_asm ())
4232 fprintf (asm_out_file, "\t.cfi_endproc\n");
4234 /* Output a label to mark the endpoint of the code generated for this
4235 function. */
4236 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4237 current_function_funcdef_no);
4238 ASM_OUTPUT_LABEL (asm_out_file, label);
4239 fde = current_fde ();
4240 gcc_assert (fde != NULL);
4241 fde->dw_fde_end = xstrdup (label);
4244 void
4245 dwarf2out_frame_init (void)
4247 /* Allocate the initial hunk of the fde_table. */
4248 fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4249 fde_table_allocated = FDE_TABLE_INCREMENT;
4250 fde_table_in_use = 0;
4252 /* Generate the CFA instructions common to all FDE's. Do it now for the
4253 sake of lookup_cfa. */
4255 /* On entry, the Canonical Frame Address is at SP. */
4256 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4258 if (targetm.debug_unwind_info () == UI_DWARF2
4259 || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4260 initial_return_save (INCOMING_RETURN_ADDR_RTX);
4263 void
4264 dwarf2out_frame_finish (void)
4266 /* Output call frame information. */
4267 if (targetm.debug_unwind_info () == UI_DWARF2)
4268 output_call_frame_info (0);
4270 /* Output another copy for the unwinder. */
4271 if ((flag_unwind_tables || flag_exceptions)
4272 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4273 output_call_frame_info (1);
4276 /* Note that the current function section is being used for code. */
4278 static void
4279 dwarf2out_note_section_used (void)
4281 section *sec = current_function_section ();
4282 if (sec == text_section)
4283 text_section_used = true;
4284 else if (sec == cold_text_section)
4285 cold_text_section_used = true;
4288 void
4289 dwarf2out_switch_text_section (void)
4291 dw_fde_ref fde = current_fde ();
4293 gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4295 fde->dw_fde_switched_sections = 1;
4296 fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4298 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4299 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4300 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4301 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4302 have_multiple_function_sections = true;
4304 /* Reset the current label on switching text sections, so that we
4305 don't attempt to advance_loc4 between labels in different sections. */
4306 fde->dw_fde_current_label = NULL;
4308 /* There is no need to mark used sections when not debugging. */
4309 if (cold_text_section != NULL)
4310 dwarf2out_note_section_used ();
4312 if (dwarf2out_do_cfi_asm ())
4313 fprintf (asm_out_file, "\t.cfi_endproc\n");
4315 /* Now do the real section switch. */
4316 switch_to_section (current_function_section ());
4318 if (dwarf2out_do_cfi_asm ())
4320 dwarf2out_do_cfi_startproc (true);
4321 /* As this is a different FDE, insert all current CFI instructions
4322 again. */
4323 output_cfis (fde->dw_fde_cfi, true, fde, true);
4325 else
4327 dw_cfi_ref cfi = fde->dw_fde_cfi;
4329 cfi = fde->dw_fde_cfi;
4330 if (cfi)
4331 while (cfi->dw_cfi_next != NULL)
4332 cfi = cfi->dw_cfi_next;
4333 fde->dw_fde_switch_cfi = cfi;
4337 /* And now, the subset of the debugging information support code necessary
4338 for emitting location expressions. */
4340 /* Data about a single source file. */
4341 struct GTY(()) dwarf_file_data {
4342 const char * filename;
4343 int emitted_number;
4346 typedef struct dw_val_struct *dw_val_ref;
4347 typedef struct die_struct *dw_die_ref;
4348 typedef const struct die_struct *const_dw_die_ref;
4349 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4350 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4352 typedef struct GTY(()) deferred_locations_struct
4354 tree variable;
4355 dw_die_ref die;
4356 } deferred_locations;
4358 DEF_VEC_O(deferred_locations);
4359 DEF_VEC_ALLOC_O(deferred_locations,gc);
4361 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4363 DEF_VEC_P(dw_die_ref);
4364 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4366 /* Each DIE may have a series of attribute/value pairs. Values
4367 can take on several forms. The forms that are used in this
4368 implementation are listed below. */
4370 enum dw_val_class
4372 dw_val_class_addr,
4373 dw_val_class_offset,
4374 dw_val_class_loc,
4375 dw_val_class_loc_list,
4376 dw_val_class_range_list,
4377 dw_val_class_const,
4378 dw_val_class_unsigned_const,
4379 dw_val_class_const_double,
4380 dw_val_class_vec,
4381 dw_val_class_flag,
4382 dw_val_class_die_ref,
4383 dw_val_class_fde_ref,
4384 dw_val_class_lbl_id,
4385 dw_val_class_lineptr,
4386 dw_val_class_str,
4387 dw_val_class_macptr,
4388 dw_val_class_file,
4389 dw_val_class_data8,
4390 dw_val_class_decl_ref,
4391 dw_val_class_vms_delta
4394 /* Describe a floating point constant value, or a vector constant value. */
4396 typedef struct GTY(()) dw_vec_struct {
4397 unsigned char * GTY((length ("%h.length"))) array;
4398 unsigned length;
4399 unsigned elt_size;
4401 dw_vec_const;
4403 /* The dw_val_node describes an attribute's value, as it is
4404 represented internally. */
4406 typedef struct GTY(()) dw_val_struct {
4407 enum dw_val_class val_class;
4408 union dw_val_struct_union
4410 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4411 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4412 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4413 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4414 HOST_WIDE_INT GTY ((default)) val_int;
4415 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4416 double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4417 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4418 struct dw_val_die_union
4420 dw_die_ref die;
4421 int external;
4422 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4423 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4424 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4425 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4426 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4427 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4428 unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4429 tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4430 struct dw_val_vms_delta_union
4432 char * lbl1;
4433 char * lbl2;
4434 } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4436 GTY ((desc ("%1.val_class"))) v;
4438 dw_val_node;
4440 /* Locations in memory are described using a sequence of stack machine
4441 operations. */
4443 typedef struct GTY(()) dw_loc_descr_struct {
4444 dw_loc_descr_ref dw_loc_next;
4445 ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4446 /* Used to distinguish DW_OP_addr with a direct symbol relocation
4447 from DW_OP_addr with a dtp-relative symbol relocation. */
4448 unsigned int dtprel : 1;
4449 int dw_loc_addr;
4450 dw_val_node dw_loc_oprnd1;
4451 dw_val_node dw_loc_oprnd2;
4453 dw_loc_descr_node;
4455 /* Location lists are ranges + location descriptions for that range,
4456 so you can track variables that are in different places over
4457 their entire life. */
4458 typedef struct GTY(()) dw_loc_list_struct {
4459 dw_loc_list_ref dw_loc_next;
4460 const char *begin; /* Label for begin address of range */
4461 const char *end; /* Label for end address of range */
4462 char *ll_symbol; /* Label for beginning of location list.
4463 Only on head of list */
4464 const char *section; /* Section this loclist is relative to */
4465 dw_loc_descr_ref expr;
4466 hashval_t hash;
4467 bool emitted;
4468 } dw_loc_list_node;
4470 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4472 /* Convert a DWARF stack opcode into its string name. */
4474 static const char *
4475 dwarf_stack_op_name (unsigned int op)
4477 switch (op)
4479 case DW_OP_addr:
4480 return "DW_OP_addr";
4481 case DW_OP_deref:
4482 return "DW_OP_deref";
4483 case DW_OP_const1u:
4484 return "DW_OP_const1u";
4485 case DW_OP_const1s:
4486 return "DW_OP_const1s";
4487 case DW_OP_const2u:
4488 return "DW_OP_const2u";
4489 case DW_OP_const2s:
4490 return "DW_OP_const2s";
4491 case DW_OP_const4u:
4492 return "DW_OP_const4u";
4493 case DW_OP_const4s:
4494 return "DW_OP_const4s";
4495 case DW_OP_const8u:
4496 return "DW_OP_const8u";
4497 case DW_OP_const8s:
4498 return "DW_OP_const8s";
4499 case DW_OP_constu:
4500 return "DW_OP_constu";
4501 case DW_OP_consts:
4502 return "DW_OP_consts";
4503 case DW_OP_dup:
4504 return "DW_OP_dup";
4505 case DW_OP_drop:
4506 return "DW_OP_drop";
4507 case DW_OP_over:
4508 return "DW_OP_over";
4509 case DW_OP_pick:
4510 return "DW_OP_pick";
4511 case DW_OP_swap:
4512 return "DW_OP_swap";
4513 case DW_OP_rot:
4514 return "DW_OP_rot";
4515 case DW_OP_xderef:
4516 return "DW_OP_xderef";
4517 case DW_OP_abs:
4518 return "DW_OP_abs";
4519 case DW_OP_and:
4520 return "DW_OP_and";
4521 case DW_OP_div:
4522 return "DW_OP_div";
4523 case DW_OP_minus:
4524 return "DW_OP_minus";
4525 case DW_OP_mod:
4526 return "DW_OP_mod";
4527 case DW_OP_mul:
4528 return "DW_OP_mul";
4529 case DW_OP_neg:
4530 return "DW_OP_neg";
4531 case DW_OP_not:
4532 return "DW_OP_not";
4533 case DW_OP_or:
4534 return "DW_OP_or";
4535 case DW_OP_plus:
4536 return "DW_OP_plus";
4537 case DW_OP_plus_uconst:
4538 return "DW_OP_plus_uconst";
4539 case DW_OP_shl:
4540 return "DW_OP_shl";
4541 case DW_OP_shr:
4542 return "DW_OP_shr";
4543 case DW_OP_shra:
4544 return "DW_OP_shra";
4545 case DW_OP_xor:
4546 return "DW_OP_xor";
4547 case DW_OP_bra:
4548 return "DW_OP_bra";
4549 case DW_OP_eq:
4550 return "DW_OP_eq";
4551 case DW_OP_ge:
4552 return "DW_OP_ge";
4553 case DW_OP_gt:
4554 return "DW_OP_gt";
4555 case DW_OP_le:
4556 return "DW_OP_le";
4557 case DW_OP_lt:
4558 return "DW_OP_lt";
4559 case DW_OP_ne:
4560 return "DW_OP_ne";
4561 case DW_OP_skip:
4562 return "DW_OP_skip";
4563 case DW_OP_lit0:
4564 return "DW_OP_lit0";
4565 case DW_OP_lit1:
4566 return "DW_OP_lit1";
4567 case DW_OP_lit2:
4568 return "DW_OP_lit2";
4569 case DW_OP_lit3:
4570 return "DW_OP_lit3";
4571 case DW_OP_lit4:
4572 return "DW_OP_lit4";
4573 case DW_OP_lit5:
4574 return "DW_OP_lit5";
4575 case DW_OP_lit6:
4576 return "DW_OP_lit6";
4577 case DW_OP_lit7:
4578 return "DW_OP_lit7";
4579 case DW_OP_lit8:
4580 return "DW_OP_lit8";
4581 case DW_OP_lit9:
4582 return "DW_OP_lit9";
4583 case DW_OP_lit10:
4584 return "DW_OP_lit10";
4585 case DW_OP_lit11:
4586 return "DW_OP_lit11";
4587 case DW_OP_lit12:
4588 return "DW_OP_lit12";
4589 case DW_OP_lit13:
4590 return "DW_OP_lit13";
4591 case DW_OP_lit14:
4592 return "DW_OP_lit14";
4593 case DW_OP_lit15:
4594 return "DW_OP_lit15";
4595 case DW_OP_lit16:
4596 return "DW_OP_lit16";
4597 case DW_OP_lit17:
4598 return "DW_OP_lit17";
4599 case DW_OP_lit18:
4600 return "DW_OP_lit18";
4601 case DW_OP_lit19:
4602 return "DW_OP_lit19";
4603 case DW_OP_lit20:
4604 return "DW_OP_lit20";
4605 case DW_OP_lit21:
4606 return "DW_OP_lit21";
4607 case DW_OP_lit22:
4608 return "DW_OP_lit22";
4609 case DW_OP_lit23:
4610 return "DW_OP_lit23";
4611 case DW_OP_lit24:
4612 return "DW_OP_lit24";
4613 case DW_OP_lit25:
4614 return "DW_OP_lit25";
4615 case DW_OP_lit26:
4616 return "DW_OP_lit26";
4617 case DW_OP_lit27:
4618 return "DW_OP_lit27";
4619 case DW_OP_lit28:
4620 return "DW_OP_lit28";
4621 case DW_OP_lit29:
4622 return "DW_OP_lit29";
4623 case DW_OP_lit30:
4624 return "DW_OP_lit30";
4625 case DW_OP_lit31:
4626 return "DW_OP_lit31";
4627 case DW_OP_reg0:
4628 return "DW_OP_reg0";
4629 case DW_OP_reg1:
4630 return "DW_OP_reg1";
4631 case DW_OP_reg2:
4632 return "DW_OP_reg2";
4633 case DW_OP_reg3:
4634 return "DW_OP_reg3";
4635 case DW_OP_reg4:
4636 return "DW_OP_reg4";
4637 case DW_OP_reg5:
4638 return "DW_OP_reg5";
4639 case DW_OP_reg6:
4640 return "DW_OP_reg6";
4641 case DW_OP_reg7:
4642 return "DW_OP_reg7";
4643 case DW_OP_reg8:
4644 return "DW_OP_reg8";
4645 case DW_OP_reg9:
4646 return "DW_OP_reg9";
4647 case DW_OP_reg10:
4648 return "DW_OP_reg10";
4649 case DW_OP_reg11:
4650 return "DW_OP_reg11";
4651 case DW_OP_reg12:
4652 return "DW_OP_reg12";
4653 case DW_OP_reg13:
4654 return "DW_OP_reg13";
4655 case DW_OP_reg14:
4656 return "DW_OP_reg14";
4657 case DW_OP_reg15:
4658 return "DW_OP_reg15";
4659 case DW_OP_reg16:
4660 return "DW_OP_reg16";
4661 case DW_OP_reg17:
4662 return "DW_OP_reg17";
4663 case DW_OP_reg18:
4664 return "DW_OP_reg18";
4665 case DW_OP_reg19:
4666 return "DW_OP_reg19";
4667 case DW_OP_reg20:
4668 return "DW_OP_reg20";
4669 case DW_OP_reg21:
4670 return "DW_OP_reg21";
4671 case DW_OP_reg22:
4672 return "DW_OP_reg22";
4673 case DW_OP_reg23:
4674 return "DW_OP_reg23";
4675 case DW_OP_reg24:
4676 return "DW_OP_reg24";
4677 case DW_OP_reg25:
4678 return "DW_OP_reg25";
4679 case DW_OP_reg26:
4680 return "DW_OP_reg26";
4681 case DW_OP_reg27:
4682 return "DW_OP_reg27";
4683 case DW_OP_reg28:
4684 return "DW_OP_reg28";
4685 case DW_OP_reg29:
4686 return "DW_OP_reg29";
4687 case DW_OP_reg30:
4688 return "DW_OP_reg30";
4689 case DW_OP_reg31:
4690 return "DW_OP_reg31";
4691 case DW_OP_breg0:
4692 return "DW_OP_breg0";
4693 case DW_OP_breg1:
4694 return "DW_OP_breg1";
4695 case DW_OP_breg2:
4696 return "DW_OP_breg2";
4697 case DW_OP_breg3:
4698 return "DW_OP_breg3";
4699 case DW_OP_breg4:
4700 return "DW_OP_breg4";
4701 case DW_OP_breg5:
4702 return "DW_OP_breg5";
4703 case DW_OP_breg6:
4704 return "DW_OP_breg6";
4705 case DW_OP_breg7:
4706 return "DW_OP_breg7";
4707 case DW_OP_breg8:
4708 return "DW_OP_breg8";
4709 case DW_OP_breg9:
4710 return "DW_OP_breg9";
4711 case DW_OP_breg10:
4712 return "DW_OP_breg10";
4713 case DW_OP_breg11:
4714 return "DW_OP_breg11";
4715 case DW_OP_breg12:
4716 return "DW_OP_breg12";
4717 case DW_OP_breg13:
4718 return "DW_OP_breg13";
4719 case DW_OP_breg14:
4720 return "DW_OP_breg14";
4721 case DW_OP_breg15:
4722 return "DW_OP_breg15";
4723 case DW_OP_breg16:
4724 return "DW_OP_breg16";
4725 case DW_OP_breg17:
4726 return "DW_OP_breg17";
4727 case DW_OP_breg18:
4728 return "DW_OP_breg18";
4729 case DW_OP_breg19:
4730 return "DW_OP_breg19";
4731 case DW_OP_breg20:
4732 return "DW_OP_breg20";
4733 case DW_OP_breg21:
4734 return "DW_OP_breg21";
4735 case DW_OP_breg22:
4736 return "DW_OP_breg22";
4737 case DW_OP_breg23:
4738 return "DW_OP_breg23";
4739 case DW_OP_breg24:
4740 return "DW_OP_breg24";
4741 case DW_OP_breg25:
4742 return "DW_OP_breg25";
4743 case DW_OP_breg26:
4744 return "DW_OP_breg26";
4745 case DW_OP_breg27:
4746 return "DW_OP_breg27";
4747 case DW_OP_breg28:
4748 return "DW_OP_breg28";
4749 case DW_OP_breg29:
4750 return "DW_OP_breg29";
4751 case DW_OP_breg30:
4752 return "DW_OP_breg30";
4753 case DW_OP_breg31:
4754 return "DW_OP_breg31";
4755 case DW_OP_regx:
4756 return "DW_OP_regx";
4757 case DW_OP_fbreg:
4758 return "DW_OP_fbreg";
4759 case DW_OP_bregx:
4760 return "DW_OP_bregx";
4761 case DW_OP_piece:
4762 return "DW_OP_piece";
4763 case DW_OP_deref_size:
4764 return "DW_OP_deref_size";
4765 case DW_OP_xderef_size:
4766 return "DW_OP_xderef_size";
4767 case DW_OP_nop:
4768 return "DW_OP_nop";
4770 case DW_OP_push_object_address:
4771 return "DW_OP_push_object_address";
4772 case DW_OP_call2:
4773 return "DW_OP_call2";
4774 case DW_OP_call4:
4775 return "DW_OP_call4";
4776 case DW_OP_call_ref:
4777 return "DW_OP_call_ref";
4778 case DW_OP_implicit_value:
4779 return "DW_OP_implicit_value";
4780 case DW_OP_stack_value:
4781 return "DW_OP_stack_value";
4782 case DW_OP_form_tls_address:
4783 return "DW_OP_form_tls_address";
4784 case DW_OP_call_frame_cfa:
4785 return "DW_OP_call_frame_cfa";
4786 case DW_OP_bit_piece:
4787 return "DW_OP_bit_piece";
4789 case DW_OP_GNU_push_tls_address:
4790 return "DW_OP_GNU_push_tls_address";
4791 case DW_OP_GNU_uninit:
4792 return "DW_OP_GNU_uninit";
4793 case DW_OP_GNU_encoded_addr:
4794 return "DW_OP_GNU_encoded_addr";
4795 case DW_OP_GNU_implicit_pointer:
4796 return "DW_OP_GNU_implicit_pointer";
4798 default:
4799 return "OP_<unknown>";
4803 /* Return a pointer to a newly allocated location description. Location
4804 descriptions are simple expression terms that can be strung
4805 together to form more complicated location (address) descriptions. */
4807 static inline dw_loc_descr_ref
4808 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4809 unsigned HOST_WIDE_INT oprnd2)
4811 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4813 descr->dw_loc_opc = op;
4814 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4815 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4816 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4817 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4819 return descr;
4822 /* Return a pointer to a newly allocated location description for
4823 REG and OFFSET. */
4825 static inline dw_loc_descr_ref
4826 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
4828 if (reg <= 31)
4829 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4830 offset, 0);
4831 else
4832 return new_loc_descr (DW_OP_bregx, reg, offset);
4835 /* Add a location description term to a location description expression. */
4837 static inline void
4838 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4840 dw_loc_descr_ref *d;
4842 /* Find the end of the chain. */
4843 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4846 *d = descr;
4849 /* Add a constant OFFSET to a location expression. */
4851 static void
4852 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4854 dw_loc_descr_ref loc;
4855 HOST_WIDE_INT *p;
4857 gcc_assert (*list_head != NULL);
4859 if (!offset)
4860 return;
4862 /* Find the end of the chain. */
4863 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4866 p = NULL;
4867 if (loc->dw_loc_opc == DW_OP_fbreg
4868 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4869 p = &loc->dw_loc_oprnd1.v.val_int;
4870 else if (loc->dw_loc_opc == DW_OP_bregx)
4871 p = &loc->dw_loc_oprnd2.v.val_int;
4873 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4874 offset. Don't optimize if an signed integer overflow would happen. */
4875 if (p != NULL
4876 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4877 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4878 *p += offset;
4880 else if (offset > 0)
4881 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4883 else
4885 loc->dw_loc_next = int_loc_descriptor (-offset);
4886 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4890 /* Add a constant OFFSET to a location list. */
4892 static void
4893 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4895 dw_loc_list_ref d;
4896 for (d = list_head; d != NULL; d = d->dw_loc_next)
4897 loc_descr_plus_const (&d->expr, offset);
4900 #define DWARF_REF_SIZE \
4901 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4903 /* Return the size of a location descriptor. */
4905 static unsigned long
4906 size_of_loc_descr (dw_loc_descr_ref loc)
4908 unsigned long size = 1;
4910 switch (loc->dw_loc_opc)
4912 case DW_OP_addr:
4913 size += DWARF2_ADDR_SIZE;
4914 break;
4915 case DW_OP_const1u:
4916 case DW_OP_const1s:
4917 size += 1;
4918 break;
4919 case DW_OP_const2u:
4920 case DW_OP_const2s:
4921 size += 2;
4922 break;
4923 case DW_OP_const4u:
4924 case DW_OP_const4s:
4925 size += 4;
4926 break;
4927 case DW_OP_const8u:
4928 case DW_OP_const8s:
4929 size += 8;
4930 break;
4931 case DW_OP_constu:
4932 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4933 break;
4934 case DW_OP_consts:
4935 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4936 break;
4937 case DW_OP_pick:
4938 size += 1;
4939 break;
4940 case DW_OP_plus_uconst:
4941 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4942 break;
4943 case DW_OP_skip:
4944 case DW_OP_bra:
4945 size += 2;
4946 break;
4947 case DW_OP_breg0:
4948 case DW_OP_breg1:
4949 case DW_OP_breg2:
4950 case DW_OP_breg3:
4951 case DW_OP_breg4:
4952 case DW_OP_breg5:
4953 case DW_OP_breg6:
4954 case DW_OP_breg7:
4955 case DW_OP_breg8:
4956 case DW_OP_breg9:
4957 case DW_OP_breg10:
4958 case DW_OP_breg11:
4959 case DW_OP_breg12:
4960 case DW_OP_breg13:
4961 case DW_OP_breg14:
4962 case DW_OP_breg15:
4963 case DW_OP_breg16:
4964 case DW_OP_breg17:
4965 case DW_OP_breg18:
4966 case DW_OP_breg19:
4967 case DW_OP_breg20:
4968 case DW_OP_breg21:
4969 case DW_OP_breg22:
4970 case DW_OP_breg23:
4971 case DW_OP_breg24:
4972 case DW_OP_breg25:
4973 case DW_OP_breg26:
4974 case DW_OP_breg27:
4975 case DW_OP_breg28:
4976 case DW_OP_breg29:
4977 case DW_OP_breg30:
4978 case DW_OP_breg31:
4979 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4980 break;
4981 case DW_OP_regx:
4982 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4983 break;
4984 case DW_OP_fbreg:
4985 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4986 break;
4987 case DW_OP_bregx:
4988 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4989 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4990 break;
4991 case DW_OP_piece:
4992 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4993 break;
4994 case DW_OP_bit_piece:
4995 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4996 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4997 break;
4998 case DW_OP_deref_size:
4999 case DW_OP_xderef_size:
5000 size += 1;
5001 break;
5002 case DW_OP_call2:
5003 size += 2;
5004 break;
5005 case DW_OP_call4:
5006 size += 4;
5007 break;
5008 case DW_OP_call_ref:
5009 size += DWARF_REF_SIZE;
5010 break;
5011 case DW_OP_implicit_value:
5012 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5013 + loc->dw_loc_oprnd1.v.val_unsigned;
5014 break;
5015 case DW_OP_GNU_implicit_pointer:
5016 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5017 break;
5018 default:
5019 break;
5022 return size;
5025 /* Return the size of a series of location descriptors. */
5027 static unsigned long
5028 size_of_locs (dw_loc_descr_ref loc)
5030 dw_loc_descr_ref l;
5031 unsigned long size;
5033 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5034 field, to avoid writing to a PCH file. */
5035 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5037 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5038 break;
5039 size += size_of_loc_descr (l);
5041 if (! l)
5042 return size;
5044 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5046 l->dw_loc_addr = size;
5047 size += size_of_loc_descr (l);
5050 return size;
5053 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5054 static void get_ref_die_offset_label (char *, dw_die_ref);
5056 /* Output location description stack opcode's operands (if any). */
5058 static void
5059 output_loc_operands (dw_loc_descr_ref loc)
5061 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5062 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5064 switch (loc->dw_loc_opc)
5066 #ifdef DWARF2_DEBUGGING_INFO
5067 case DW_OP_const2u:
5068 case DW_OP_const2s:
5069 dw2_asm_output_data (2, val1->v.val_int, NULL);
5070 break;
5071 case DW_OP_const4u:
5072 if (loc->dtprel)
5074 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5075 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5076 val1->v.val_addr);
5077 fputc ('\n', asm_out_file);
5078 break;
5080 /* FALLTHRU */
5081 case DW_OP_const4s:
5082 dw2_asm_output_data (4, val1->v.val_int, NULL);
5083 break;
5084 case DW_OP_const8u:
5085 if (loc->dtprel)
5087 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5088 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5089 val1->v.val_addr);
5090 fputc ('\n', asm_out_file);
5091 break;
5093 /* FALLTHRU */
5094 case DW_OP_const8s:
5095 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5096 dw2_asm_output_data (8, val1->v.val_int, NULL);
5097 break;
5098 case DW_OP_skip:
5099 case DW_OP_bra:
5101 int offset;
5103 gcc_assert (val1->val_class == dw_val_class_loc);
5104 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5106 dw2_asm_output_data (2, offset, NULL);
5108 break;
5109 case DW_OP_implicit_value:
5110 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5111 switch (val2->val_class)
5113 case dw_val_class_const:
5114 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5115 break;
5116 case dw_val_class_vec:
5118 unsigned int elt_size = val2->v.val_vec.elt_size;
5119 unsigned int len = val2->v.val_vec.length;
5120 unsigned int i;
5121 unsigned char *p;
5123 if (elt_size > sizeof (HOST_WIDE_INT))
5125 elt_size /= 2;
5126 len *= 2;
5128 for (i = 0, p = val2->v.val_vec.array;
5129 i < len;
5130 i++, p += elt_size)
5131 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5132 "fp or vector constant word %u", i);
5134 break;
5135 case dw_val_class_const_double:
5137 unsigned HOST_WIDE_INT first, second;
5139 if (WORDS_BIG_ENDIAN)
5141 first = val2->v.val_double.high;
5142 second = val2->v.val_double.low;
5144 else
5146 first = val2->v.val_double.low;
5147 second = val2->v.val_double.high;
5149 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5150 first, NULL);
5151 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5152 second, NULL);
5154 break;
5155 case dw_val_class_addr:
5156 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5157 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5158 break;
5159 default:
5160 gcc_unreachable ();
5162 break;
5163 #else
5164 case DW_OP_const2u:
5165 case DW_OP_const2s:
5166 case DW_OP_const4u:
5167 case DW_OP_const4s:
5168 case DW_OP_const8u:
5169 case DW_OP_const8s:
5170 case DW_OP_skip:
5171 case DW_OP_bra:
5172 case DW_OP_implicit_value:
5173 /* We currently don't make any attempt to make sure these are
5174 aligned properly like we do for the main unwind info, so
5175 don't support emitting things larger than a byte if we're
5176 only doing unwinding. */
5177 gcc_unreachable ();
5178 #endif
5179 case DW_OP_const1u:
5180 case DW_OP_const1s:
5181 dw2_asm_output_data (1, val1->v.val_int, NULL);
5182 break;
5183 case DW_OP_constu:
5184 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5185 break;
5186 case DW_OP_consts:
5187 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5188 break;
5189 case DW_OP_pick:
5190 dw2_asm_output_data (1, val1->v.val_int, NULL);
5191 break;
5192 case DW_OP_plus_uconst:
5193 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5194 break;
5195 case DW_OP_breg0:
5196 case DW_OP_breg1:
5197 case DW_OP_breg2:
5198 case DW_OP_breg3:
5199 case DW_OP_breg4:
5200 case DW_OP_breg5:
5201 case DW_OP_breg6:
5202 case DW_OP_breg7:
5203 case DW_OP_breg8:
5204 case DW_OP_breg9:
5205 case DW_OP_breg10:
5206 case DW_OP_breg11:
5207 case DW_OP_breg12:
5208 case DW_OP_breg13:
5209 case DW_OP_breg14:
5210 case DW_OP_breg15:
5211 case DW_OP_breg16:
5212 case DW_OP_breg17:
5213 case DW_OP_breg18:
5214 case DW_OP_breg19:
5215 case DW_OP_breg20:
5216 case DW_OP_breg21:
5217 case DW_OP_breg22:
5218 case DW_OP_breg23:
5219 case DW_OP_breg24:
5220 case DW_OP_breg25:
5221 case DW_OP_breg26:
5222 case DW_OP_breg27:
5223 case DW_OP_breg28:
5224 case DW_OP_breg29:
5225 case DW_OP_breg30:
5226 case DW_OP_breg31:
5227 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5228 break;
5229 case DW_OP_regx:
5230 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5231 break;
5232 case DW_OP_fbreg:
5233 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5234 break;
5235 case DW_OP_bregx:
5236 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5237 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5238 break;
5239 case DW_OP_piece:
5240 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5241 break;
5242 case DW_OP_bit_piece:
5243 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5244 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5245 break;
5246 case DW_OP_deref_size:
5247 case DW_OP_xderef_size:
5248 dw2_asm_output_data (1, val1->v.val_int, NULL);
5249 break;
5251 case DW_OP_addr:
5252 if (loc->dtprel)
5254 if (targetm.asm_out.output_dwarf_dtprel)
5256 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5257 DWARF2_ADDR_SIZE,
5258 val1->v.val_addr);
5259 fputc ('\n', asm_out_file);
5261 else
5262 gcc_unreachable ();
5264 else
5266 #ifdef DWARF2_DEBUGGING_INFO
5267 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5268 #else
5269 gcc_unreachable ();
5270 #endif
5272 break;
5274 case DW_OP_GNU_implicit_pointer:
5276 char label[MAX_ARTIFICIAL_LABEL_BYTES
5277 + HOST_BITS_PER_WIDE_INT / 2 + 2];
5278 gcc_assert (val1->val_class == dw_val_class_die_ref);
5279 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5280 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5281 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5283 break;
5285 default:
5286 /* Other codes have no operands. */
5287 break;
5291 /* Output a sequence of location operations. */
5293 static void
5294 output_loc_sequence (dw_loc_descr_ref loc)
5296 for (; loc != NULL; loc = loc->dw_loc_next)
5298 /* Output the opcode. */
5299 dw2_asm_output_data (1, loc->dw_loc_opc,
5300 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5302 /* Output the operand(s) (if any). */
5303 output_loc_operands (loc);
5307 /* Output location description stack opcode's operands (if any).
5308 The output is single bytes on a line, suitable for .cfi_escape. */
5310 static void
5311 output_loc_operands_raw (dw_loc_descr_ref loc)
5313 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5314 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5316 switch (loc->dw_loc_opc)
5318 case DW_OP_addr:
5319 case DW_OP_implicit_value:
5320 /* We cannot output addresses in .cfi_escape, only bytes. */
5321 gcc_unreachable ();
5323 case DW_OP_const1u:
5324 case DW_OP_const1s:
5325 case DW_OP_pick:
5326 case DW_OP_deref_size:
5327 case DW_OP_xderef_size:
5328 fputc (',', asm_out_file);
5329 dw2_asm_output_data_raw (1, val1->v.val_int);
5330 break;
5332 case DW_OP_const2u:
5333 case DW_OP_const2s:
5334 fputc (',', asm_out_file);
5335 dw2_asm_output_data_raw (2, val1->v.val_int);
5336 break;
5338 case DW_OP_const4u:
5339 case DW_OP_const4s:
5340 fputc (',', asm_out_file);
5341 dw2_asm_output_data_raw (4, val1->v.val_int);
5342 break;
5344 case DW_OP_const8u:
5345 case DW_OP_const8s:
5346 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5347 fputc (',', asm_out_file);
5348 dw2_asm_output_data_raw (8, val1->v.val_int);
5349 break;
5351 case DW_OP_skip:
5352 case DW_OP_bra:
5354 int offset;
5356 gcc_assert (val1->val_class == dw_val_class_loc);
5357 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5359 fputc (',', asm_out_file);
5360 dw2_asm_output_data_raw (2, offset);
5362 break;
5364 case DW_OP_constu:
5365 case DW_OP_plus_uconst:
5366 case DW_OP_regx:
5367 case DW_OP_piece:
5368 fputc (',', asm_out_file);
5369 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5370 break;
5372 case DW_OP_bit_piece:
5373 fputc (',', asm_out_file);
5374 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5375 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5376 break;
5378 case DW_OP_consts:
5379 case DW_OP_breg0:
5380 case DW_OP_breg1:
5381 case DW_OP_breg2:
5382 case DW_OP_breg3:
5383 case DW_OP_breg4:
5384 case DW_OP_breg5:
5385 case DW_OP_breg6:
5386 case DW_OP_breg7:
5387 case DW_OP_breg8:
5388 case DW_OP_breg9:
5389 case DW_OP_breg10:
5390 case DW_OP_breg11:
5391 case DW_OP_breg12:
5392 case DW_OP_breg13:
5393 case DW_OP_breg14:
5394 case DW_OP_breg15:
5395 case DW_OP_breg16:
5396 case DW_OP_breg17:
5397 case DW_OP_breg18:
5398 case DW_OP_breg19:
5399 case DW_OP_breg20:
5400 case DW_OP_breg21:
5401 case DW_OP_breg22:
5402 case DW_OP_breg23:
5403 case DW_OP_breg24:
5404 case DW_OP_breg25:
5405 case DW_OP_breg26:
5406 case DW_OP_breg27:
5407 case DW_OP_breg28:
5408 case DW_OP_breg29:
5409 case DW_OP_breg30:
5410 case DW_OP_breg31:
5411 case DW_OP_fbreg:
5412 fputc (',', asm_out_file);
5413 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5414 break;
5416 case DW_OP_bregx:
5417 fputc (',', asm_out_file);
5418 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5419 fputc (',', asm_out_file);
5420 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5421 break;
5423 case DW_OP_GNU_implicit_pointer:
5424 gcc_unreachable ();
5425 break;
5427 default:
5428 /* Other codes have no operands. */
5429 break;
5433 static void
5434 output_loc_sequence_raw (dw_loc_descr_ref loc)
5436 while (1)
5438 /* Output the opcode. */
5439 fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5440 output_loc_operands_raw (loc);
5442 if (!loc->dw_loc_next)
5443 break;
5444 loc = loc->dw_loc_next;
5446 fputc (',', asm_out_file);
5450 /* This routine will generate the correct assembly data for a location
5451 description based on a cfi entry with a complex address. */
5453 static void
5454 output_cfa_loc (dw_cfi_ref cfi)
5456 dw_loc_descr_ref loc;
5457 unsigned long size;
5459 if (cfi->dw_cfi_opc == DW_CFA_expression)
5461 dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5462 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5464 else
5465 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5467 /* Output the size of the block. */
5468 size = size_of_locs (loc);
5469 dw2_asm_output_data_uleb128 (size, NULL);
5471 /* Now output the operations themselves. */
5472 output_loc_sequence (loc);
5475 /* Similar, but used for .cfi_escape. */
5477 static void
5478 output_cfa_loc_raw (dw_cfi_ref cfi)
5480 dw_loc_descr_ref loc;
5481 unsigned long size;
5483 if (cfi->dw_cfi_opc == DW_CFA_expression)
5485 fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5486 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5488 else
5489 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5491 /* Output the size of the block. */
5492 size = size_of_locs (loc);
5493 dw2_asm_output_data_uleb128_raw (size);
5494 fputc (',', asm_out_file);
5496 /* Now output the operations themselves. */
5497 output_loc_sequence_raw (loc);
5500 /* This function builds a dwarf location descriptor sequence from a
5501 dw_cfa_location, adding the given OFFSET to the result of the
5502 expression. */
5504 static struct dw_loc_descr_struct *
5505 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5507 struct dw_loc_descr_struct *head, *tmp;
5509 offset += cfa->offset;
5511 if (cfa->indirect)
5513 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5514 head->dw_loc_oprnd1.val_class = dw_val_class_const;
5515 tmp = new_loc_descr (DW_OP_deref, 0, 0);
5516 add_loc_descr (&head, tmp);
5517 if (offset != 0)
5519 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5520 add_loc_descr (&head, tmp);
5523 else
5524 head = new_reg_loc_descr (cfa->reg, offset);
5526 return head;
5529 /* This function builds a dwarf location descriptor sequence for
5530 the address at OFFSET from the CFA when stack is aligned to
5531 ALIGNMENT byte. */
5533 static struct dw_loc_descr_struct *
5534 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5536 struct dw_loc_descr_struct *head;
5537 unsigned int dwarf_fp
5538 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5540 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
5541 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5543 head = new_reg_loc_descr (dwarf_fp, 0);
5544 add_loc_descr (&head, int_loc_descriptor (alignment));
5545 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5546 loc_descr_plus_const (&head, offset);
5548 else
5549 head = new_reg_loc_descr (dwarf_fp, offset);
5550 return head;
5553 /* This function fills in aa dw_cfa_location structure from a dwarf location
5554 descriptor sequence. */
5556 static void
5557 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5559 struct dw_loc_descr_struct *ptr;
5560 cfa->offset = 0;
5561 cfa->base_offset = 0;
5562 cfa->indirect = 0;
5563 cfa->reg = -1;
5565 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5567 enum dwarf_location_atom op = ptr->dw_loc_opc;
5569 switch (op)
5571 case DW_OP_reg0:
5572 case DW_OP_reg1:
5573 case DW_OP_reg2:
5574 case DW_OP_reg3:
5575 case DW_OP_reg4:
5576 case DW_OP_reg5:
5577 case DW_OP_reg6:
5578 case DW_OP_reg7:
5579 case DW_OP_reg8:
5580 case DW_OP_reg9:
5581 case DW_OP_reg10:
5582 case DW_OP_reg11:
5583 case DW_OP_reg12:
5584 case DW_OP_reg13:
5585 case DW_OP_reg14:
5586 case DW_OP_reg15:
5587 case DW_OP_reg16:
5588 case DW_OP_reg17:
5589 case DW_OP_reg18:
5590 case DW_OP_reg19:
5591 case DW_OP_reg20:
5592 case DW_OP_reg21:
5593 case DW_OP_reg22:
5594 case DW_OP_reg23:
5595 case DW_OP_reg24:
5596 case DW_OP_reg25:
5597 case DW_OP_reg26:
5598 case DW_OP_reg27:
5599 case DW_OP_reg28:
5600 case DW_OP_reg29:
5601 case DW_OP_reg30:
5602 case DW_OP_reg31:
5603 cfa->reg = op - DW_OP_reg0;
5604 break;
5605 case DW_OP_regx:
5606 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5607 break;
5608 case DW_OP_breg0:
5609 case DW_OP_breg1:
5610 case DW_OP_breg2:
5611 case DW_OP_breg3:
5612 case DW_OP_breg4:
5613 case DW_OP_breg5:
5614 case DW_OP_breg6:
5615 case DW_OP_breg7:
5616 case DW_OP_breg8:
5617 case DW_OP_breg9:
5618 case DW_OP_breg10:
5619 case DW_OP_breg11:
5620 case DW_OP_breg12:
5621 case DW_OP_breg13:
5622 case DW_OP_breg14:
5623 case DW_OP_breg15:
5624 case DW_OP_breg16:
5625 case DW_OP_breg17:
5626 case DW_OP_breg18:
5627 case DW_OP_breg19:
5628 case DW_OP_breg20:
5629 case DW_OP_breg21:
5630 case DW_OP_breg22:
5631 case DW_OP_breg23:
5632 case DW_OP_breg24:
5633 case DW_OP_breg25:
5634 case DW_OP_breg26:
5635 case DW_OP_breg27:
5636 case DW_OP_breg28:
5637 case DW_OP_breg29:
5638 case DW_OP_breg30:
5639 case DW_OP_breg31:
5640 cfa->reg = op - DW_OP_breg0;
5641 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5642 break;
5643 case DW_OP_bregx:
5644 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5645 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5646 break;
5647 case DW_OP_deref:
5648 cfa->indirect = 1;
5649 break;
5650 case DW_OP_plus_uconst:
5651 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5652 break;
5653 default:
5654 internal_error ("DW_LOC_OP %s not implemented",
5655 dwarf_stack_op_name (ptr->dw_loc_opc));
5660 /* And now, the support for symbolic debugging information. */
5662 /* .debug_str support. */
5663 static int output_indirect_string (void **, void *);
5665 static void dwarf2out_init (const char *);
5666 static void dwarf2out_finish (const char *);
5667 static void dwarf2out_assembly_start (void);
5668 static void dwarf2out_define (unsigned int, const char *);
5669 static void dwarf2out_undef (unsigned int, const char *);
5670 static void dwarf2out_start_source_file (unsigned, const char *);
5671 static void dwarf2out_end_source_file (unsigned);
5672 static void dwarf2out_function_decl (tree);
5673 static void dwarf2out_begin_block (unsigned, unsigned);
5674 static void dwarf2out_end_block (unsigned, unsigned);
5675 static bool dwarf2out_ignore_block (const_tree);
5676 static void dwarf2out_global_decl (tree);
5677 static void dwarf2out_type_decl (tree, int);
5678 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5679 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5680 dw_die_ref);
5681 static void dwarf2out_abstract_function (tree);
5682 static void dwarf2out_var_location (rtx);
5683 static void dwarf2out_direct_call (tree);
5684 static void dwarf2out_virtual_call_token (tree, int);
5685 static void dwarf2out_copy_call_info (rtx, rtx);
5686 static void dwarf2out_virtual_call (int);
5687 static void dwarf2out_begin_function (tree);
5688 static void dwarf2out_set_name (tree, tree);
5690 /* The debug hooks structure. */
5692 const struct gcc_debug_hooks dwarf2_debug_hooks =
5694 dwarf2out_init,
5695 dwarf2out_finish,
5696 dwarf2out_assembly_start,
5697 dwarf2out_define,
5698 dwarf2out_undef,
5699 dwarf2out_start_source_file,
5700 dwarf2out_end_source_file,
5701 dwarf2out_begin_block,
5702 dwarf2out_end_block,
5703 dwarf2out_ignore_block,
5704 dwarf2out_source_line,
5705 dwarf2out_begin_prologue,
5706 #if VMS_DEBUGGING_INFO
5707 dwarf2out_vms_end_prologue,
5708 dwarf2out_vms_begin_epilogue,
5709 #else
5710 debug_nothing_int_charstar,
5711 debug_nothing_int_charstar,
5712 #endif
5713 dwarf2out_end_epilogue,
5714 dwarf2out_begin_function,
5715 debug_nothing_int, /* end_function */
5716 dwarf2out_function_decl, /* function_decl */
5717 dwarf2out_global_decl,
5718 dwarf2out_type_decl, /* type_decl */
5719 dwarf2out_imported_module_or_decl,
5720 debug_nothing_tree, /* deferred_inline_function */
5721 /* The DWARF 2 backend tries to reduce debugging bloat by not
5722 emitting the abstract description of inline functions until
5723 something tries to reference them. */
5724 dwarf2out_abstract_function, /* outlining_inline_function */
5725 debug_nothing_rtx, /* label */
5726 debug_nothing_int, /* handle_pch */
5727 dwarf2out_var_location,
5728 dwarf2out_switch_text_section,
5729 dwarf2out_direct_call,
5730 dwarf2out_virtual_call_token,
5731 dwarf2out_copy_call_info,
5732 dwarf2out_virtual_call,
5733 dwarf2out_set_name,
5734 1, /* start_end_main_source_file */
5735 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
5738 /* NOTE: In the comments in this file, many references are made to
5739 "Debugging Information Entries". This term is abbreviated as `DIE'
5740 throughout the remainder of this file. */
5742 /* An internal representation of the DWARF output is built, and then
5743 walked to generate the DWARF debugging info. The walk of the internal
5744 representation is done after the entire program has been compiled.
5745 The types below are used to describe the internal representation. */
5747 /* Various DIE's use offsets relative to the beginning of the
5748 .debug_info section to refer to each other. */
5750 typedef long int dw_offset;
5752 /* Define typedefs here to avoid circular dependencies. */
5754 typedef struct dw_attr_struct *dw_attr_ref;
5755 typedef struct dw_line_info_struct *dw_line_info_ref;
5756 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5757 typedef struct pubname_struct *pubname_ref;
5758 typedef struct dw_ranges_struct *dw_ranges_ref;
5759 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5760 typedef struct comdat_type_struct *comdat_type_node_ref;
5762 /* Each entry in the line_info_table maintains the file and
5763 line number associated with the label generated for that
5764 entry. The label gives the PC value associated with
5765 the line number entry. */
5767 typedef struct GTY(()) dw_line_info_struct {
5768 unsigned long dw_file_num;
5769 unsigned long dw_line_num;
5771 dw_line_info_entry;
5773 /* Line information for functions in separate sections; each one gets its
5774 own sequence. */
5775 typedef struct GTY(()) dw_separate_line_info_struct {
5776 unsigned long dw_file_num;
5777 unsigned long dw_line_num;
5778 unsigned long function;
5780 dw_separate_line_info_entry;
5782 /* Each DIE attribute has a field specifying the attribute kind,
5783 a link to the next attribute in the chain, and an attribute value.
5784 Attributes are typically linked below the DIE they modify. */
5786 typedef struct GTY(()) dw_attr_struct {
5787 enum dwarf_attribute dw_attr;
5788 dw_val_node dw_attr_val;
5790 dw_attr_node;
5792 DEF_VEC_O(dw_attr_node);
5793 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5795 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
5796 The children of each node form a circular list linked by
5797 die_sib. die_child points to the node *before* the "first" child node. */
5799 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5800 union die_symbol_or_type_node
5802 char * GTY ((tag ("0"))) die_symbol;
5803 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5805 GTY ((desc ("dwarf_version >= 4"))) die_id;
5806 VEC(dw_attr_node,gc) * die_attr;
5807 dw_die_ref die_parent;
5808 dw_die_ref die_child;
5809 dw_die_ref die_sib;
5810 dw_die_ref die_definition; /* ref from a specification to its definition */
5811 dw_offset die_offset;
5812 unsigned long die_abbrev;
5813 int die_mark;
5814 /* Die is used and must not be pruned as unused. */
5815 int die_perennial_p;
5816 unsigned int decl_id;
5817 enum dwarf_tag die_tag;
5819 die_node;
5821 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
5822 #define FOR_EACH_CHILD(die, c, expr) do { \
5823 c = die->die_child; \
5824 if (c) do { \
5825 c = c->die_sib; \
5826 expr; \
5827 } while (c != die->die_child); \
5828 } while (0)
5830 /* The pubname structure */
5832 typedef struct GTY(()) pubname_struct {
5833 dw_die_ref die;
5834 const char *name;
5836 pubname_entry;
5838 DEF_VEC_O(pubname_entry);
5839 DEF_VEC_ALLOC_O(pubname_entry, gc);
5841 struct GTY(()) dw_ranges_struct {
5842 /* If this is positive, it's a block number, otherwise it's a
5843 bitwise-negated index into dw_ranges_by_label. */
5844 int num;
5847 /* A structure to hold a macinfo entry. */
5849 typedef struct GTY(()) macinfo_struct {
5850 unsigned HOST_WIDE_INT code;
5851 unsigned HOST_WIDE_INT lineno;
5852 const char *info;
5854 macinfo_entry;
5856 DEF_VEC_O(macinfo_entry);
5857 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5859 struct GTY(()) dw_ranges_by_label_struct {
5860 const char *begin;
5861 const char *end;
5864 /* The comdat type node structure. */
5865 typedef struct GTY(()) comdat_type_struct
5867 dw_die_ref root_die;
5868 dw_die_ref type_die;
5869 char signature[DWARF_TYPE_SIGNATURE_SIZE];
5870 struct comdat_type_struct *next;
5872 comdat_type_node;
5874 /* The limbo die list structure. */
5875 typedef struct GTY(()) limbo_die_struct {
5876 dw_die_ref die;
5877 tree created_for;
5878 struct limbo_die_struct *next;
5880 limbo_die_node;
5882 typedef struct skeleton_chain_struct
5884 dw_die_ref old_die;
5885 dw_die_ref new_die;
5886 struct skeleton_chain_struct *parent;
5888 skeleton_chain_node;
5890 /* How to start an assembler comment. */
5891 #ifndef ASM_COMMENT_START
5892 #define ASM_COMMENT_START ";#"
5893 #endif
5895 /* Define a macro which returns nonzero for a TYPE_DECL which was
5896 implicitly generated for a tagged type.
5898 Note that unlike the gcc front end (which generates a NULL named
5899 TYPE_DECL node for each complete tagged type, each array type, and
5900 each function type node created) the g++ front end generates a
5901 _named_ TYPE_DECL node for each tagged type node created.
5902 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5903 generate a DW_TAG_typedef DIE for them. */
5905 #define TYPE_DECL_IS_STUB(decl) \
5906 (DECL_NAME (decl) == NULL_TREE \
5907 || (DECL_ARTIFICIAL (decl) \
5908 && is_tagged_type (TREE_TYPE (decl)) \
5909 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
5910 /* This is necessary for stub decls that \
5911 appear in nested inline functions. */ \
5912 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5913 && (decl_ultimate_origin (decl) \
5914 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5916 /* Information concerning the compilation unit's programming
5917 language, and compiler version. */
5919 /* Fixed size portion of the DWARF compilation unit header. */
5920 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5921 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5923 /* Fixed size portion of the DWARF comdat type unit header. */
5924 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5925 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5926 + DWARF_OFFSET_SIZE)
5928 /* Fixed size portion of public names info. */
5929 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5931 /* Fixed size portion of the address range info. */
5932 #define DWARF_ARANGES_HEADER_SIZE \
5933 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5934 DWARF2_ADDR_SIZE * 2) \
5935 - DWARF_INITIAL_LENGTH_SIZE)
5937 /* Size of padding portion in the address range info. It must be
5938 aligned to twice the pointer size. */
5939 #define DWARF_ARANGES_PAD_SIZE \
5940 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5941 DWARF2_ADDR_SIZE * 2) \
5942 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5944 /* Use assembler line directives if available. */
5945 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5946 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5947 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5948 #else
5949 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5950 #endif
5951 #endif
5953 /* Minimum line offset in a special line info. opcode.
5954 This value was chosen to give a reasonable range of values. */
5955 #define DWARF_LINE_BASE -10
5957 /* First special line opcode - leave room for the standard opcodes. */
5958 #define DWARF_LINE_OPCODE_BASE 10
5960 /* Range of line offsets in a special line info. opcode. */
5961 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
5963 /* Flag that indicates the initial value of the is_stmt_start flag.
5964 In the present implementation, we do not mark any lines as
5965 the beginning of a source statement, because that information
5966 is not made available by the GCC front-end. */
5967 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5969 /* Maximum number of operations per instruction bundle. */
5970 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5971 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5972 #endif
5974 /* This location is used by calc_die_sizes() to keep track
5975 the offset of each DIE within the .debug_info section. */
5976 static unsigned long next_die_offset;
5978 /* Record the root of the DIE's built for the current compilation unit. */
5979 static GTY(()) dw_die_ref single_comp_unit_die;
5981 /* A list of type DIEs that have been separated into comdat sections. */
5982 static GTY(()) comdat_type_node *comdat_type_list;
5984 /* A list of DIEs with a NULL parent waiting to be relocated. */
5985 static GTY(()) limbo_die_node *limbo_die_list;
5987 /* A list of DIEs for which we may have to generate
5988 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
5989 static GTY(()) limbo_die_node *deferred_asm_name;
5991 /* Filenames referenced by this compilation unit. */
5992 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5994 /* A hash table of references to DIE's that describe declarations.
5995 The key is a DECL_UID() which is a unique number identifying each decl. */
5996 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5998 /* A hash table of references to DIE's that describe COMMON blocks.
5999 The key is DECL_UID() ^ die_parent. */
6000 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6002 typedef struct GTY(()) die_arg_entry_struct {
6003 dw_die_ref die;
6004 tree arg;
6005 } die_arg_entry;
6007 DEF_VEC_O(die_arg_entry);
6008 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6010 /* Node of the variable location list. */
6011 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6012 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6013 EXPR_LIST chain. For small bitsizes, bitsize is encoded
6014 in mode of the EXPR_LIST node and first EXPR_LIST operand
6015 is either NOTE_INSN_VAR_LOCATION for a piece with a known
6016 location or NULL for padding. For larger bitsizes,
6017 mode is 0 and first operand is a CONCAT with bitsize
6018 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6019 NULL as second operand. */
6020 rtx GTY (()) loc;
6021 const char * GTY (()) label;
6022 struct var_loc_node * GTY (()) next;
6025 /* Variable location list. */
6026 struct GTY (()) var_loc_list_def {
6027 struct var_loc_node * GTY (()) first;
6029 /* Pointer to the last but one or last element of the
6030 chained list. If the list is empty, both first and
6031 last are NULL, if the list contains just one node
6032 or the last node certainly is not redundant, it points
6033 to the last node, otherwise points to the last but one.
6034 Do not mark it for GC because it is marked through the chain. */
6035 struct var_loc_node * GTY ((skip ("%h"))) last;
6037 /* DECL_UID of the variable decl. */
6038 unsigned int decl_id;
6040 typedef struct var_loc_list_def var_loc_list;
6043 /* Table of decl location linked lists. */
6044 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6046 /* A pointer to the base of a list of references to DIE's that
6047 are uniquely identified by their tag, presence/absence of
6048 children DIE's, and list of attribute/value pairs. */
6049 static GTY((length ("abbrev_die_table_allocated")))
6050 dw_die_ref *abbrev_die_table;
6052 /* Number of elements currently allocated for abbrev_die_table. */
6053 static GTY(()) unsigned abbrev_die_table_allocated;
6055 /* Number of elements in type_die_table currently in use. */
6056 static GTY(()) unsigned abbrev_die_table_in_use;
6058 /* Size (in elements) of increments by which we may expand the
6059 abbrev_die_table. */
6060 #define ABBREV_DIE_TABLE_INCREMENT 256
6062 /* A pointer to the base of a table that contains line information
6063 for each source code line in .text in the compilation unit. */
6064 static GTY((length ("line_info_table_allocated")))
6065 dw_line_info_ref line_info_table;
6067 /* Number of elements currently allocated for line_info_table. */
6068 static GTY(()) unsigned line_info_table_allocated;
6070 /* Number of elements in line_info_table currently in use. */
6071 static GTY(()) unsigned line_info_table_in_use;
6073 /* A pointer to the base of a table that contains line information
6074 for each source code line outside of .text in the compilation unit. */
6075 static GTY ((length ("separate_line_info_table_allocated")))
6076 dw_separate_line_info_ref separate_line_info_table;
6078 /* Number of elements currently allocated for separate_line_info_table. */
6079 static GTY(()) unsigned separate_line_info_table_allocated;
6081 /* Number of elements in separate_line_info_table currently in use. */
6082 static GTY(()) unsigned separate_line_info_table_in_use;
6084 /* Size (in elements) of increments by which we may expand the
6085 line_info_table. */
6086 #define LINE_INFO_TABLE_INCREMENT 1024
6088 /* A flag to tell pubnames/types export if there is an info section to
6089 refer to. */
6090 static bool info_section_emitted;
6092 /* A pointer to the base of a table that contains a list of publicly
6093 accessible names. */
6094 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
6096 /* A pointer to the base of a table that contains a list of publicly
6097 accessible types. */
6098 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6100 /* A pointer to the base of a table that contains a list of macro
6101 defines/undefines (and file start/end markers). */
6102 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6104 /* Array of dies for which we should generate .debug_arange info. */
6105 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6107 /* Number of elements currently allocated for arange_table. */
6108 static GTY(()) unsigned arange_table_allocated;
6110 /* Number of elements in arange_table currently in use. */
6111 static GTY(()) unsigned arange_table_in_use;
6113 /* Size (in elements) of increments by which we may expand the
6114 arange_table. */
6115 #define ARANGE_TABLE_INCREMENT 64
6117 /* Array of dies for which we should generate .debug_ranges info. */
6118 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6120 /* Number of elements currently allocated for ranges_table. */
6121 static GTY(()) unsigned ranges_table_allocated;
6123 /* Number of elements in ranges_table currently in use. */
6124 static GTY(()) unsigned ranges_table_in_use;
6126 /* Array of pairs of labels referenced in ranges_table. */
6127 static GTY ((length ("ranges_by_label_allocated")))
6128 dw_ranges_by_label_ref ranges_by_label;
6130 /* Number of elements currently allocated for ranges_by_label. */
6131 static GTY(()) unsigned ranges_by_label_allocated;
6133 /* Number of elements in ranges_by_label currently in use. */
6134 static GTY(()) unsigned ranges_by_label_in_use;
6136 /* Size (in elements) of increments by which we may expand the
6137 ranges_table. */
6138 #define RANGES_TABLE_INCREMENT 64
6140 /* Whether we have location lists that need outputting */
6141 static GTY(()) bool have_location_lists;
6143 /* Unique label counter. */
6144 static GTY(()) unsigned int loclabel_num;
6146 /* Unique label counter for point-of-call tables. */
6147 static GTY(()) unsigned int poc_label_num;
6149 /* The direct call table structure. */
6151 typedef struct GTY(()) dcall_struct {
6152 unsigned int poc_label_num;
6153 tree poc_decl;
6154 dw_die_ref targ_die;
6156 dcall_entry;
6158 DEF_VEC_O(dcall_entry);
6159 DEF_VEC_ALLOC_O(dcall_entry, gc);
6161 /* The virtual call table structure. */
6163 typedef struct GTY(()) vcall_struct {
6164 unsigned int poc_label_num;
6165 unsigned int vtable_slot;
6167 vcall_entry;
6169 DEF_VEC_O(vcall_entry);
6170 DEF_VEC_ALLOC_O(vcall_entry, gc);
6172 /* Pointers to the direct and virtual call tables. */
6173 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6174 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6176 /* A hash table to map INSN_UIDs to vtable slot indexes. */
6178 struct GTY (()) vcall_insn {
6179 int insn_uid;
6180 unsigned int vtable_slot;
6183 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6185 /* Record whether the function being analyzed contains inlined functions. */
6186 static int current_function_has_inlines;
6188 /* The last file entry emitted by maybe_emit_file(). */
6189 static GTY(()) struct dwarf_file_data * last_emitted_file;
6191 /* Number of internal labels generated by gen_internal_sym(). */
6192 static GTY(()) int label_num;
6194 /* Cached result of previous call to lookup_filename. */
6195 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6197 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6199 /* Offset from the "steady-state frame pointer" to the frame base,
6200 within the current function. */
6201 static HOST_WIDE_INT frame_pointer_fb_offset;
6203 /* Forward declarations for functions defined in this file. */
6205 static int is_pseudo_reg (const_rtx);
6206 static tree type_main_variant (tree);
6207 static int is_tagged_type (const_tree);
6208 static const char *dwarf_tag_name (unsigned);
6209 static const char *dwarf_attr_name (unsigned);
6210 static const char *dwarf_form_name (unsigned);
6211 static tree decl_ultimate_origin (const_tree);
6212 static tree decl_class_context (tree);
6213 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6214 static inline enum dw_val_class AT_class (dw_attr_ref);
6215 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6216 static inline unsigned AT_flag (dw_attr_ref);
6217 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6218 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6219 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6220 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6221 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6222 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6223 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6224 unsigned int, unsigned char *);
6225 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6226 static hashval_t debug_str_do_hash (const void *);
6227 static int debug_str_eq (const void *, const void *);
6228 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6229 static inline const char *AT_string (dw_attr_ref);
6230 static enum dwarf_form AT_string_form (dw_attr_ref);
6231 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6232 static void add_AT_specification (dw_die_ref, dw_die_ref);
6233 static inline dw_die_ref AT_ref (dw_attr_ref);
6234 static inline int AT_ref_external (dw_attr_ref);
6235 static inline void set_AT_ref_external (dw_attr_ref, int);
6236 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6237 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6238 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6239 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6240 dw_loc_list_ref);
6241 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6242 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6243 static inline rtx AT_addr (dw_attr_ref);
6244 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6245 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6246 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6247 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6248 unsigned HOST_WIDE_INT);
6249 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6250 unsigned long);
6251 static inline const char *AT_lbl (dw_attr_ref);
6252 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6253 static const char *get_AT_low_pc (dw_die_ref);
6254 static const char *get_AT_hi_pc (dw_die_ref);
6255 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6256 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6257 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6258 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6259 static bool is_cxx (void);
6260 static bool is_fortran (void);
6261 static bool is_ada (void);
6262 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6263 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6264 static void add_child_die (dw_die_ref, dw_die_ref);
6265 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6266 static dw_die_ref lookup_type_die (tree);
6267 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6268 static void equate_type_number_to_die (tree, dw_die_ref);
6269 static hashval_t decl_die_table_hash (const void *);
6270 static int decl_die_table_eq (const void *, const void *);
6271 static dw_die_ref lookup_decl_die (tree);
6272 static hashval_t common_block_die_table_hash (const void *);
6273 static int common_block_die_table_eq (const void *, const void *);
6274 static hashval_t decl_loc_table_hash (const void *);
6275 static int decl_loc_table_eq (const void *, const void *);
6276 static var_loc_list *lookup_decl_loc (const_tree);
6277 static void equate_decl_number_to_die (tree, dw_die_ref);
6278 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6279 static void print_spaces (FILE *);
6280 static void print_die (dw_die_ref, FILE *);
6281 static void print_dwarf_line_table (FILE *);
6282 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6283 static dw_die_ref pop_compile_unit (dw_die_ref);
6284 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6285 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6286 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6287 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6288 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6289 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6290 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6291 struct md5_ctx *, int *);
6292 struct checksum_attributes;
6293 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6294 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6295 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6296 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6297 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6298 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6299 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6300 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6301 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6302 static void compute_section_prefix (dw_die_ref);
6303 static int is_type_die (dw_die_ref);
6304 static int is_comdat_die (dw_die_ref);
6305 static int is_symbol_die (dw_die_ref);
6306 static void assign_symbol_names (dw_die_ref);
6307 static void break_out_includes (dw_die_ref);
6308 static int is_declaration_die (dw_die_ref);
6309 static int should_move_die_to_comdat (dw_die_ref);
6310 static dw_die_ref clone_as_declaration (dw_die_ref);
6311 static dw_die_ref clone_die (dw_die_ref);
6312 static dw_die_ref clone_tree (dw_die_ref);
6313 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6314 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6315 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6316 static dw_die_ref generate_skeleton (dw_die_ref);
6317 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6318 dw_die_ref);
6319 static void break_out_comdat_types (dw_die_ref);
6320 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6321 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6322 static void copy_decls_for_unworthy_types (dw_die_ref);
6324 static hashval_t htab_cu_hash (const void *);
6325 static int htab_cu_eq (const void *, const void *);
6326 static void htab_cu_del (void *);
6327 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6328 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6329 static void add_sibling_attributes (dw_die_ref);
6330 static void build_abbrev_table (dw_die_ref);
6331 static void output_location_lists (dw_die_ref);
6332 static int constant_size (unsigned HOST_WIDE_INT);
6333 static unsigned long size_of_die (dw_die_ref);
6334 static void calc_die_sizes (dw_die_ref);
6335 static void mark_dies (dw_die_ref);
6336 static void unmark_dies (dw_die_ref);
6337 static void unmark_all_dies (dw_die_ref);
6338 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6339 static unsigned long size_of_aranges (void);
6340 static enum dwarf_form value_format (dw_attr_ref);
6341 static void output_value_format (dw_attr_ref);
6342 static void output_abbrev_section (void);
6343 static void output_die_symbol (dw_die_ref);
6344 static void output_die (dw_die_ref);
6345 static void output_compilation_unit_header (void);
6346 static void output_comp_unit (dw_die_ref, int);
6347 static void output_comdat_type_unit (comdat_type_node *);
6348 static const char *dwarf2_name (tree, int);
6349 static void add_pubname (tree, dw_die_ref);
6350 static void add_pubname_string (const char *, dw_die_ref);
6351 static void add_pubtype (tree, dw_die_ref);
6352 static void output_pubnames (VEC (pubname_entry,gc) *);
6353 static void add_arange (tree, dw_die_ref);
6354 static void output_aranges (void);
6355 static unsigned int add_ranges_num (int);
6356 static unsigned int add_ranges (const_tree);
6357 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6358 bool *);
6359 static void output_ranges (void);
6360 static void output_line_info (void);
6361 static void output_file_names (void);
6362 static dw_die_ref base_type_die (tree);
6363 static int is_base_type (tree);
6364 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6365 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6366 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6367 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6368 static int type_is_enum (const_tree);
6369 static unsigned int dbx_reg_number (const_rtx);
6370 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6371 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6372 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6373 enum var_init_status);
6374 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6375 enum var_init_status);
6376 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6377 enum var_init_status);
6378 static int is_based_loc (const_rtx);
6379 static int resolve_one_addr (rtx *, void *);
6380 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6381 enum var_init_status);
6382 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6383 enum var_init_status);
6384 static dw_loc_list_ref loc_list_from_tree (tree, int);
6385 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6386 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6387 static tree field_type (const_tree);
6388 static unsigned int simple_type_align_in_bits (const_tree);
6389 static unsigned int simple_decl_align_in_bits (const_tree);
6390 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6391 static HOST_WIDE_INT field_byte_offset (const_tree);
6392 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6393 dw_loc_list_ref);
6394 static void add_data_member_location_attribute (dw_die_ref, tree);
6395 static bool add_const_value_attribute (dw_die_ref, rtx);
6396 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6397 static void insert_double (double_int, unsigned char *);
6398 static void insert_float (const_rtx, unsigned char *);
6399 static rtx rtl_for_decl_location (tree);
6400 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6401 enum dwarf_attribute);
6402 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6403 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6404 static void add_name_attribute (dw_die_ref, const char *);
6405 static void add_comp_dir_attribute (dw_die_ref);
6406 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6407 static void add_subscript_info (dw_die_ref, tree, bool);
6408 static void add_byte_size_attribute (dw_die_ref, tree);
6409 static void add_bit_offset_attribute (dw_die_ref, tree);
6410 static void add_bit_size_attribute (dw_die_ref, tree);
6411 static void add_prototyped_attribute (dw_die_ref, tree);
6412 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6413 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6414 static void add_src_coords_attributes (dw_die_ref, tree);
6415 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6416 static void push_decl_scope (tree);
6417 static void pop_decl_scope (void);
6418 static dw_die_ref scope_die_for (tree, dw_die_ref);
6419 static inline int local_scope_p (dw_die_ref);
6420 static inline int class_scope_p (dw_die_ref);
6421 static inline int class_or_namespace_scope_p (dw_die_ref);
6422 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6423 static void add_calling_convention_attribute (dw_die_ref, tree);
6424 static const char *type_tag (const_tree);
6425 static tree member_declared_type (const_tree);
6426 #if 0
6427 static const char *decl_start_label (tree);
6428 #endif
6429 static void gen_array_type_die (tree, dw_die_ref);
6430 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6431 #if 0
6432 static void gen_entry_point_die (tree, dw_die_ref);
6433 #endif
6434 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6435 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6436 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
6437 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6438 static void gen_formal_types_die (tree, dw_die_ref);
6439 static void gen_subprogram_die (tree, dw_die_ref);
6440 static void gen_variable_die (tree, tree, dw_die_ref);
6441 static void gen_const_die (tree, dw_die_ref);
6442 static void gen_label_die (tree, dw_die_ref);
6443 static void gen_lexical_block_die (tree, dw_die_ref, int);
6444 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6445 static void gen_field_die (tree, dw_die_ref);
6446 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6447 static dw_die_ref gen_compile_unit_die (const char *);
6448 static void gen_inheritance_die (tree, tree, dw_die_ref);
6449 static void gen_member_die (tree, dw_die_ref);
6450 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6451 enum debug_info_usage);
6452 static void gen_subroutine_type_die (tree, dw_die_ref);
6453 static void gen_typedef_die (tree, dw_die_ref);
6454 static void gen_type_die (tree, dw_die_ref);
6455 static void gen_block_die (tree, dw_die_ref, int);
6456 static void decls_for_scope (tree, dw_die_ref, int);
6457 static int is_redundant_typedef (const_tree);
6458 static bool is_naming_typedef_decl (const_tree);
6459 static inline dw_die_ref get_context_die (tree);
6460 static void gen_namespace_die (tree, dw_die_ref);
6461 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6462 static dw_die_ref force_decl_die (tree);
6463 static dw_die_ref force_type_die (tree);
6464 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6465 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6466 static struct dwarf_file_data * lookup_filename (const char *);
6467 static void retry_incomplete_types (void);
6468 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6469 static void gen_generic_params_dies (tree);
6470 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6471 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6472 static void splice_child_die (dw_die_ref, dw_die_ref);
6473 static int file_info_cmp (const void *, const void *);
6474 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6475 const char *, const char *);
6476 static void output_loc_list (dw_loc_list_ref);
6477 static char *gen_internal_sym (const char *);
6479 static void prune_unmark_dies (dw_die_ref);
6480 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6481 static void prune_unused_types_mark (dw_die_ref, int);
6482 static void prune_unused_types_walk (dw_die_ref);
6483 static void prune_unused_types_walk_attribs (dw_die_ref);
6484 static void prune_unused_types_prune (dw_die_ref);
6485 static void prune_unused_types (void);
6486 static int maybe_emit_file (struct dwarf_file_data *fd);
6487 static inline const char *AT_vms_delta1 (dw_attr_ref);
6488 static inline const char *AT_vms_delta2 (dw_attr_ref);
6489 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6490 const char *, const char *);
6491 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6492 static void gen_remaining_tmpl_value_param_die_attribute (void);
6494 /* Section names used to hold DWARF debugging information. */
6495 #ifndef DEBUG_INFO_SECTION
6496 #define DEBUG_INFO_SECTION ".debug_info"
6497 #endif
6498 #ifndef DEBUG_ABBREV_SECTION
6499 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
6500 #endif
6501 #ifndef DEBUG_ARANGES_SECTION
6502 #define DEBUG_ARANGES_SECTION ".debug_aranges"
6503 #endif
6504 #ifndef DEBUG_MACINFO_SECTION
6505 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
6506 #endif
6507 #ifndef DEBUG_LINE_SECTION
6508 #define DEBUG_LINE_SECTION ".debug_line"
6509 #endif
6510 #ifndef DEBUG_LOC_SECTION
6511 #define DEBUG_LOC_SECTION ".debug_loc"
6512 #endif
6513 #ifndef DEBUG_PUBNAMES_SECTION
6514 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
6515 #endif
6516 #ifndef DEBUG_PUBTYPES_SECTION
6517 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
6518 #endif
6519 #ifndef DEBUG_DCALL_SECTION
6520 #define DEBUG_DCALL_SECTION ".debug_dcall"
6521 #endif
6522 #ifndef DEBUG_VCALL_SECTION
6523 #define DEBUG_VCALL_SECTION ".debug_vcall"
6524 #endif
6525 #ifndef DEBUG_STR_SECTION
6526 #define DEBUG_STR_SECTION ".debug_str"
6527 #endif
6528 #ifndef DEBUG_RANGES_SECTION
6529 #define DEBUG_RANGES_SECTION ".debug_ranges"
6530 #endif
6532 /* Standard ELF section names for compiled code and data. */
6533 #ifndef TEXT_SECTION_NAME
6534 #define TEXT_SECTION_NAME ".text"
6535 #endif
6537 /* Section flags for .debug_str section. */
6538 #define DEBUG_STR_SECTION_FLAGS \
6539 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
6540 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
6541 : SECTION_DEBUG)
6543 /* Labels we insert at beginning sections we can reference instead of
6544 the section names themselves. */
6546 #ifndef TEXT_SECTION_LABEL
6547 #define TEXT_SECTION_LABEL "Ltext"
6548 #endif
6549 #ifndef COLD_TEXT_SECTION_LABEL
6550 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
6551 #endif
6552 #ifndef DEBUG_LINE_SECTION_LABEL
6553 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
6554 #endif
6555 #ifndef DEBUG_INFO_SECTION_LABEL
6556 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
6557 #endif
6558 #ifndef DEBUG_ABBREV_SECTION_LABEL
6559 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
6560 #endif
6561 #ifndef DEBUG_LOC_SECTION_LABEL
6562 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
6563 #endif
6564 #ifndef DEBUG_RANGES_SECTION_LABEL
6565 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
6566 #endif
6567 #ifndef DEBUG_MACINFO_SECTION_LABEL
6568 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
6569 #endif
6572 /* Definitions of defaults for formats and names of various special
6573 (artificial) labels which may be generated within this file (when the -g
6574 options is used and DWARF2_DEBUGGING_INFO is in effect.
6575 If necessary, these may be overridden from within the tm.h file, but
6576 typically, overriding these defaults is unnecessary. */
6578 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6579 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6580 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6581 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6582 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6583 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6584 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6585 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6586 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6587 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6589 #ifndef TEXT_END_LABEL
6590 #define TEXT_END_LABEL "Letext"
6591 #endif
6592 #ifndef COLD_END_LABEL
6593 #define COLD_END_LABEL "Letext_cold"
6594 #endif
6595 #ifndef BLOCK_BEGIN_LABEL
6596 #define BLOCK_BEGIN_LABEL "LBB"
6597 #endif
6598 #ifndef BLOCK_END_LABEL
6599 #define BLOCK_END_LABEL "LBE"
6600 #endif
6601 #ifndef LINE_CODE_LABEL
6602 #define LINE_CODE_LABEL "LM"
6603 #endif
6604 #ifndef SEPARATE_LINE_CODE_LABEL
6605 #define SEPARATE_LINE_CODE_LABEL "LSM"
6606 #endif
6609 /* Return the root of the DIE's built for the current compilation unit. */
6610 static dw_die_ref
6611 comp_unit_die (void)
6613 if (!single_comp_unit_die)
6614 single_comp_unit_die = gen_compile_unit_die (NULL);
6615 return single_comp_unit_die;
6618 /* We allow a language front-end to designate a function that is to be
6619 called to "demangle" any name before it is put into a DIE. */
6621 static const char *(*demangle_name_func) (const char *);
6623 void
6624 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6626 demangle_name_func = func;
6629 /* Test if rtl node points to a pseudo register. */
6631 static inline int
6632 is_pseudo_reg (const_rtx rtl)
6634 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6635 || (GET_CODE (rtl) == SUBREG
6636 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6639 /* Return a reference to a type, with its const and volatile qualifiers
6640 removed. */
6642 static inline tree
6643 type_main_variant (tree type)
6645 type = TYPE_MAIN_VARIANT (type);
6647 /* ??? There really should be only one main variant among any group of
6648 variants of a given type (and all of the MAIN_VARIANT values for all
6649 members of the group should point to that one type) but sometimes the C
6650 front-end messes this up for array types, so we work around that bug
6651 here. */
6652 if (TREE_CODE (type) == ARRAY_TYPE)
6653 while (type != TYPE_MAIN_VARIANT (type))
6654 type = TYPE_MAIN_VARIANT (type);
6656 return type;
6659 /* Return nonzero if the given type node represents a tagged type. */
6661 static inline int
6662 is_tagged_type (const_tree type)
6664 enum tree_code code = TREE_CODE (type);
6666 return (code == RECORD_TYPE || code == UNION_TYPE
6667 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6670 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
6672 static void
6673 get_ref_die_offset_label (char *label, dw_die_ref ref)
6675 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6678 /* Convert a DIE tag into its string name. */
6680 static const char *
6681 dwarf_tag_name (unsigned int tag)
6683 switch (tag)
6685 case DW_TAG_padding:
6686 return "DW_TAG_padding";
6687 case DW_TAG_array_type:
6688 return "DW_TAG_array_type";
6689 case DW_TAG_class_type:
6690 return "DW_TAG_class_type";
6691 case DW_TAG_entry_point:
6692 return "DW_TAG_entry_point";
6693 case DW_TAG_enumeration_type:
6694 return "DW_TAG_enumeration_type";
6695 case DW_TAG_formal_parameter:
6696 return "DW_TAG_formal_parameter";
6697 case DW_TAG_imported_declaration:
6698 return "DW_TAG_imported_declaration";
6699 case DW_TAG_label:
6700 return "DW_TAG_label";
6701 case DW_TAG_lexical_block:
6702 return "DW_TAG_lexical_block";
6703 case DW_TAG_member:
6704 return "DW_TAG_member";
6705 case DW_TAG_pointer_type:
6706 return "DW_TAG_pointer_type";
6707 case DW_TAG_reference_type:
6708 return "DW_TAG_reference_type";
6709 case DW_TAG_compile_unit:
6710 return "DW_TAG_compile_unit";
6711 case DW_TAG_string_type:
6712 return "DW_TAG_string_type";
6713 case DW_TAG_structure_type:
6714 return "DW_TAG_structure_type";
6715 case DW_TAG_subroutine_type:
6716 return "DW_TAG_subroutine_type";
6717 case DW_TAG_typedef:
6718 return "DW_TAG_typedef";
6719 case DW_TAG_union_type:
6720 return "DW_TAG_union_type";
6721 case DW_TAG_unspecified_parameters:
6722 return "DW_TAG_unspecified_parameters";
6723 case DW_TAG_variant:
6724 return "DW_TAG_variant";
6725 case DW_TAG_common_block:
6726 return "DW_TAG_common_block";
6727 case DW_TAG_common_inclusion:
6728 return "DW_TAG_common_inclusion";
6729 case DW_TAG_inheritance:
6730 return "DW_TAG_inheritance";
6731 case DW_TAG_inlined_subroutine:
6732 return "DW_TAG_inlined_subroutine";
6733 case DW_TAG_module:
6734 return "DW_TAG_module";
6735 case DW_TAG_ptr_to_member_type:
6736 return "DW_TAG_ptr_to_member_type";
6737 case DW_TAG_set_type:
6738 return "DW_TAG_set_type";
6739 case DW_TAG_subrange_type:
6740 return "DW_TAG_subrange_type";
6741 case DW_TAG_with_stmt:
6742 return "DW_TAG_with_stmt";
6743 case DW_TAG_access_declaration:
6744 return "DW_TAG_access_declaration";
6745 case DW_TAG_base_type:
6746 return "DW_TAG_base_type";
6747 case DW_TAG_catch_block:
6748 return "DW_TAG_catch_block";
6749 case DW_TAG_const_type:
6750 return "DW_TAG_const_type";
6751 case DW_TAG_constant:
6752 return "DW_TAG_constant";
6753 case DW_TAG_enumerator:
6754 return "DW_TAG_enumerator";
6755 case DW_TAG_file_type:
6756 return "DW_TAG_file_type";
6757 case DW_TAG_friend:
6758 return "DW_TAG_friend";
6759 case DW_TAG_namelist:
6760 return "DW_TAG_namelist";
6761 case DW_TAG_namelist_item:
6762 return "DW_TAG_namelist_item";
6763 case DW_TAG_packed_type:
6764 return "DW_TAG_packed_type";
6765 case DW_TAG_subprogram:
6766 return "DW_TAG_subprogram";
6767 case DW_TAG_template_type_param:
6768 return "DW_TAG_template_type_param";
6769 case DW_TAG_template_value_param:
6770 return "DW_TAG_template_value_param";
6771 case DW_TAG_thrown_type:
6772 return "DW_TAG_thrown_type";
6773 case DW_TAG_try_block:
6774 return "DW_TAG_try_block";
6775 case DW_TAG_variant_part:
6776 return "DW_TAG_variant_part";
6777 case DW_TAG_variable:
6778 return "DW_TAG_variable";
6779 case DW_TAG_volatile_type:
6780 return "DW_TAG_volatile_type";
6781 case DW_TAG_dwarf_procedure:
6782 return "DW_TAG_dwarf_procedure";
6783 case DW_TAG_restrict_type:
6784 return "DW_TAG_restrict_type";
6785 case DW_TAG_interface_type:
6786 return "DW_TAG_interface_type";
6787 case DW_TAG_namespace:
6788 return "DW_TAG_namespace";
6789 case DW_TAG_imported_module:
6790 return "DW_TAG_imported_module";
6791 case DW_TAG_unspecified_type:
6792 return "DW_TAG_unspecified_type";
6793 case DW_TAG_partial_unit:
6794 return "DW_TAG_partial_unit";
6795 case DW_TAG_imported_unit:
6796 return "DW_TAG_imported_unit";
6797 case DW_TAG_condition:
6798 return "DW_TAG_condition";
6799 case DW_TAG_shared_type:
6800 return "DW_TAG_shared_type";
6801 case DW_TAG_type_unit:
6802 return "DW_TAG_type_unit";
6803 case DW_TAG_rvalue_reference_type:
6804 return "DW_TAG_rvalue_reference_type";
6805 case DW_TAG_template_alias:
6806 return "DW_TAG_template_alias";
6807 case DW_TAG_GNU_template_parameter_pack:
6808 return "DW_TAG_GNU_template_parameter_pack";
6809 case DW_TAG_GNU_formal_parameter_pack:
6810 return "DW_TAG_GNU_formal_parameter_pack";
6811 case DW_TAG_MIPS_loop:
6812 return "DW_TAG_MIPS_loop";
6813 case DW_TAG_format_label:
6814 return "DW_TAG_format_label";
6815 case DW_TAG_function_template:
6816 return "DW_TAG_function_template";
6817 case DW_TAG_class_template:
6818 return "DW_TAG_class_template";
6819 case DW_TAG_GNU_BINCL:
6820 return "DW_TAG_GNU_BINCL";
6821 case DW_TAG_GNU_EINCL:
6822 return "DW_TAG_GNU_EINCL";
6823 case DW_TAG_GNU_template_template_param:
6824 return "DW_TAG_GNU_template_template_param";
6825 default:
6826 return "DW_TAG_<unknown>";
6830 /* Convert a DWARF attribute code into its string name. */
6832 static const char *
6833 dwarf_attr_name (unsigned int attr)
6835 switch (attr)
6837 case DW_AT_sibling:
6838 return "DW_AT_sibling";
6839 case DW_AT_location:
6840 return "DW_AT_location";
6841 case DW_AT_name:
6842 return "DW_AT_name";
6843 case DW_AT_ordering:
6844 return "DW_AT_ordering";
6845 case DW_AT_subscr_data:
6846 return "DW_AT_subscr_data";
6847 case DW_AT_byte_size:
6848 return "DW_AT_byte_size";
6849 case DW_AT_bit_offset:
6850 return "DW_AT_bit_offset";
6851 case DW_AT_bit_size:
6852 return "DW_AT_bit_size";
6853 case DW_AT_element_list:
6854 return "DW_AT_element_list";
6855 case DW_AT_stmt_list:
6856 return "DW_AT_stmt_list";
6857 case DW_AT_low_pc:
6858 return "DW_AT_low_pc";
6859 case DW_AT_high_pc:
6860 return "DW_AT_high_pc";
6861 case DW_AT_language:
6862 return "DW_AT_language";
6863 case DW_AT_member:
6864 return "DW_AT_member";
6865 case DW_AT_discr:
6866 return "DW_AT_discr";
6867 case DW_AT_discr_value:
6868 return "DW_AT_discr_value";
6869 case DW_AT_visibility:
6870 return "DW_AT_visibility";
6871 case DW_AT_import:
6872 return "DW_AT_import";
6873 case DW_AT_string_length:
6874 return "DW_AT_string_length";
6875 case DW_AT_common_reference:
6876 return "DW_AT_common_reference";
6877 case DW_AT_comp_dir:
6878 return "DW_AT_comp_dir";
6879 case DW_AT_const_value:
6880 return "DW_AT_const_value";
6881 case DW_AT_containing_type:
6882 return "DW_AT_containing_type";
6883 case DW_AT_default_value:
6884 return "DW_AT_default_value";
6885 case DW_AT_inline:
6886 return "DW_AT_inline";
6887 case DW_AT_is_optional:
6888 return "DW_AT_is_optional";
6889 case DW_AT_lower_bound:
6890 return "DW_AT_lower_bound";
6891 case DW_AT_producer:
6892 return "DW_AT_producer";
6893 case DW_AT_prototyped:
6894 return "DW_AT_prototyped";
6895 case DW_AT_return_addr:
6896 return "DW_AT_return_addr";
6897 case DW_AT_start_scope:
6898 return "DW_AT_start_scope";
6899 case DW_AT_bit_stride:
6900 return "DW_AT_bit_stride";
6901 case DW_AT_upper_bound:
6902 return "DW_AT_upper_bound";
6903 case DW_AT_abstract_origin:
6904 return "DW_AT_abstract_origin";
6905 case DW_AT_accessibility:
6906 return "DW_AT_accessibility";
6907 case DW_AT_address_class:
6908 return "DW_AT_address_class";
6909 case DW_AT_artificial:
6910 return "DW_AT_artificial";
6911 case DW_AT_base_types:
6912 return "DW_AT_base_types";
6913 case DW_AT_calling_convention:
6914 return "DW_AT_calling_convention";
6915 case DW_AT_count:
6916 return "DW_AT_count";
6917 case DW_AT_data_member_location:
6918 return "DW_AT_data_member_location";
6919 case DW_AT_decl_column:
6920 return "DW_AT_decl_column";
6921 case DW_AT_decl_file:
6922 return "DW_AT_decl_file";
6923 case DW_AT_decl_line:
6924 return "DW_AT_decl_line";
6925 case DW_AT_declaration:
6926 return "DW_AT_declaration";
6927 case DW_AT_discr_list:
6928 return "DW_AT_discr_list";
6929 case DW_AT_encoding:
6930 return "DW_AT_encoding";
6931 case DW_AT_external:
6932 return "DW_AT_external";
6933 case DW_AT_explicit:
6934 return "DW_AT_explicit";
6935 case DW_AT_frame_base:
6936 return "DW_AT_frame_base";
6937 case DW_AT_friend:
6938 return "DW_AT_friend";
6939 case DW_AT_identifier_case:
6940 return "DW_AT_identifier_case";
6941 case DW_AT_macro_info:
6942 return "DW_AT_macro_info";
6943 case DW_AT_namelist_items:
6944 return "DW_AT_namelist_items";
6945 case DW_AT_priority:
6946 return "DW_AT_priority";
6947 case DW_AT_segment:
6948 return "DW_AT_segment";
6949 case DW_AT_specification:
6950 return "DW_AT_specification";
6951 case DW_AT_static_link:
6952 return "DW_AT_static_link";
6953 case DW_AT_type:
6954 return "DW_AT_type";
6955 case DW_AT_use_location:
6956 return "DW_AT_use_location";
6957 case DW_AT_variable_parameter:
6958 return "DW_AT_variable_parameter";
6959 case DW_AT_virtuality:
6960 return "DW_AT_virtuality";
6961 case DW_AT_vtable_elem_location:
6962 return "DW_AT_vtable_elem_location";
6964 case DW_AT_allocated:
6965 return "DW_AT_allocated";
6966 case DW_AT_associated:
6967 return "DW_AT_associated";
6968 case DW_AT_data_location:
6969 return "DW_AT_data_location";
6970 case DW_AT_byte_stride:
6971 return "DW_AT_byte_stride";
6972 case DW_AT_entry_pc:
6973 return "DW_AT_entry_pc";
6974 case DW_AT_use_UTF8:
6975 return "DW_AT_use_UTF8";
6976 case DW_AT_extension:
6977 return "DW_AT_extension";
6978 case DW_AT_ranges:
6979 return "DW_AT_ranges";
6980 case DW_AT_trampoline:
6981 return "DW_AT_trampoline";
6982 case DW_AT_call_column:
6983 return "DW_AT_call_column";
6984 case DW_AT_call_file:
6985 return "DW_AT_call_file";
6986 case DW_AT_call_line:
6987 return "DW_AT_call_line";
6988 case DW_AT_object_pointer:
6989 return "DW_AT_object_pointer";
6991 case DW_AT_signature:
6992 return "DW_AT_signature";
6993 case DW_AT_main_subprogram:
6994 return "DW_AT_main_subprogram";
6995 case DW_AT_data_bit_offset:
6996 return "DW_AT_data_bit_offset";
6997 case DW_AT_const_expr:
6998 return "DW_AT_const_expr";
6999 case DW_AT_enum_class:
7000 return "DW_AT_enum_class";
7001 case DW_AT_linkage_name:
7002 return "DW_AT_linkage_name";
7004 case DW_AT_MIPS_fde:
7005 return "DW_AT_MIPS_fde";
7006 case DW_AT_MIPS_loop_begin:
7007 return "DW_AT_MIPS_loop_begin";
7008 case DW_AT_MIPS_tail_loop_begin:
7009 return "DW_AT_MIPS_tail_loop_begin";
7010 case DW_AT_MIPS_epilog_begin:
7011 return "DW_AT_MIPS_epilog_begin";
7012 #if VMS_DEBUGGING_INFO
7013 case DW_AT_HP_prologue:
7014 return "DW_AT_HP_prologue";
7015 #else
7016 case DW_AT_MIPS_loop_unroll_factor:
7017 return "DW_AT_MIPS_loop_unroll_factor";
7018 #endif
7019 case DW_AT_MIPS_software_pipeline_depth:
7020 return "DW_AT_MIPS_software_pipeline_depth";
7021 case DW_AT_MIPS_linkage_name:
7022 return "DW_AT_MIPS_linkage_name";
7023 #if VMS_DEBUGGING_INFO
7024 case DW_AT_HP_epilogue:
7025 return "DW_AT_HP_epilogue";
7026 #else
7027 case DW_AT_MIPS_stride:
7028 return "DW_AT_MIPS_stride";
7029 #endif
7030 case DW_AT_MIPS_abstract_name:
7031 return "DW_AT_MIPS_abstract_name";
7032 case DW_AT_MIPS_clone_origin:
7033 return "DW_AT_MIPS_clone_origin";
7034 case DW_AT_MIPS_has_inlines:
7035 return "DW_AT_MIPS_has_inlines";
7037 case DW_AT_sf_names:
7038 return "DW_AT_sf_names";
7039 case DW_AT_src_info:
7040 return "DW_AT_src_info";
7041 case DW_AT_mac_info:
7042 return "DW_AT_mac_info";
7043 case DW_AT_src_coords:
7044 return "DW_AT_src_coords";
7045 case DW_AT_body_begin:
7046 return "DW_AT_body_begin";
7047 case DW_AT_body_end:
7048 return "DW_AT_body_end";
7049 case DW_AT_GNU_vector:
7050 return "DW_AT_GNU_vector";
7051 case DW_AT_GNU_guarded_by:
7052 return "DW_AT_GNU_guarded_by";
7053 case DW_AT_GNU_pt_guarded_by:
7054 return "DW_AT_GNU_pt_guarded_by";
7055 case DW_AT_GNU_guarded:
7056 return "DW_AT_GNU_guarded";
7057 case DW_AT_GNU_pt_guarded:
7058 return "DW_AT_GNU_pt_guarded";
7059 case DW_AT_GNU_locks_excluded:
7060 return "DW_AT_GNU_locks_excluded";
7061 case DW_AT_GNU_exclusive_locks_required:
7062 return "DW_AT_GNU_exclusive_locks_required";
7063 case DW_AT_GNU_shared_locks_required:
7064 return "DW_AT_GNU_shared_locks_required";
7065 case DW_AT_GNU_odr_signature:
7066 return "DW_AT_GNU_odr_signature";
7067 case DW_AT_GNU_template_name:
7068 return "DW_AT_GNU_template_name";
7070 case DW_AT_VMS_rtnbeg_pd_address:
7071 return "DW_AT_VMS_rtnbeg_pd_address";
7073 default:
7074 return "DW_AT_<unknown>";
7078 /* Convert a DWARF value form code into its string name. */
7080 static const char *
7081 dwarf_form_name (unsigned int form)
7083 switch (form)
7085 case DW_FORM_addr:
7086 return "DW_FORM_addr";
7087 case DW_FORM_block2:
7088 return "DW_FORM_block2";
7089 case DW_FORM_block4:
7090 return "DW_FORM_block4";
7091 case DW_FORM_data2:
7092 return "DW_FORM_data2";
7093 case DW_FORM_data4:
7094 return "DW_FORM_data4";
7095 case DW_FORM_data8:
7096 return "DW_FORM_data8";
7097 case DW_FORM_string:
7098 return "DW_FORM_string";
7099 case DW_FORM_block:
7100 return "DW_FORM_block";
7101 case DW_FORM_block1:
7102 return "DW_FORM_block1";
7103 case DW_FORM_data1:
7104 return "DW_FORM_data1";
7105 case DW_FORM_flag:
7106 return "DW_FORM_flag";
7107 case DW_FORM_sdata:
7108 return "DW_FORM_sdata";
7109 case DW_FORM_strp:
7110 return "DW_FORM_strp";
7111 case DW_FORM_udata:
7112 return "DW_FORM_udata";
7113 case DW_FORM_ref_addr:
7114 return "DW_FORM_ref_addr";
7115 case DW_FORM_ref1:
7116 return "DW_FORM_ref1";
7117 case DW_FORM_ref2:
7118 return "DW_FORM_ref2";
7119 case DW_FORM_ref4:
7120 return "DW_FORM_ref4";
7121 case DW_FORM_ref8:
7122 return "DW_FORM_ref8";
7123 case DW_FORM_ref_udata:
7124 return "DW_FORM_ref_udata";
7125 case DW_FORM_indirect:
7126 return "DW_FORM_indirect";
7127 case DW_FORM_sec_offset:
7128 return "DW_FORM_sec_offset";
7129 case DW_FORM_exprloc:
7130 return "DW_FORM_exprloc";
7131 case DW_FORM_flag_present:
7132 return "DW_FORM_flag_present";
7133 case DW_FORM_ref_sig8:
7134 return "DW_FORM_ref_sig8";
7135 default:
7136 return "DW_FORM_<unknown>";
7140 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
7141 instance of an inlined instance of a decl which is local to an inline
7142 function, so we have to trace all of the way back through the origin chain
7143 to find out what sort of node actually served as the original seed for the
7144 given block. */
7146 static tree
7147 decl_ultimate_origin (const_tree decl)
7149 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7150 return NULL_TREE;
7152 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7153 nodes in the function to point to themselves; ignore that if
7154 we're trying to output the abstract instance of this function. */
7155 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7156 return NULL_TREE;
7158 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7159 most distant ancestor, this should never happen. */
7160 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7162 return DECL_ABSTRACT_ORIGIN (decl);
7165 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
7166 of a virtual function may refer to a base class, so we check the 'this'
7167 parameter. */
7169 static tree
7170 decl_class_context (tree decl)
7172 tree context = NULL_TREE;
7174 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7175 context = DECL_CONTEXT (decl);
7176 else
7177 context = TYPE_MAIN_VARIANT
7178 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7180 if (context && !TYPE_P (context))
7181 context = NULL_TREE;
7183 return context;
7186 /* Add an attribute/value pair to a DIE. */
7188 static inline void
7189 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7191 /* Maybe this should be an assert? */
7192 if (die == NULL)
7193 return;
7195 if (die->die_attr == NULL)
7196 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7197 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7200 static inline enum dw_val_class
7201 AT_class (dw_attr_ref a)
7203 return a->dw_attr_val.val_class;
7206 /* Add a flag value attribute to a DIE. */
7208 static inline void
7209 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7211 dw_attr_node attr;
7213 attr.dw_attr = attr_kind;
7214 attr.dw_attr_val.val_class = dw_val_class_flag;
7215 attr.dw_attr_val.v.val_flag = flag;
7216 add_dwarf_attr (die, &attr);
7219 static inline unsigned
7220 AT_flag (dw_attr_ref a)
7222 gcc_assert (a && AT_class (a) == dw_val_class_flag);
7223 return a->dw_attr_val.v.val_flag;
7226 /* Add a signed integer attribute value to a DIE. */
7228 static inline void
7229 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7231 dw_attr_node attr;
7233 attr.dw_attr = attr_kind;
7234 attr.dw_attr_val.val_class = dw_val_class_const;
7235 attr.dw_attr_val.v.val_int = int_val;
7236 add_dwarf_attr (die, &attr);
7239 static inline HOST_WIDE_INT
7240 AT_int (dw_attr_ref a)
7242 gcc_assert (a && AT_class (a) == dw_val_class_const);
7243 return a->dw_attr_val.v.val_int;
7246 /* Add an unsigned integer attribute value to a DIE. */
7248 static inline void
7249 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7250 unsigned HOST_WIDE_INT unsigned_val)
7252 dw_attr_node attr;
7254 attr.dw_attr = attr_kind;
7255 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7256 attr.dw_attr_val.v.val_unsigned = unsigned_val;
7257 add_dwarf_attr (die, &attr);
7260 static inline unsigned HOST_WIDE_INT
7261 AT_unsigned (dw_attr_ref a)
7263 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7264 return a->dw_attr_val.v.val_unsigned;
7267 /* Add an unsigned double integer attribute value to a DIE. */
7269 static inline void
7270 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7271 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7273 dw_attr_node attr;
7275 attr.dw_attr = attr_kind;
7276 attr.dw_attr_val.val_class = dw_val_class_const_double;
7277 attr.dw_attr_val.v.val_double.high = high;
7278 attr.dw_attr_val.v.val_double.low = low;
7279 add_dwarf_attr (die, &attr);
7282 /* Add a floating point attribute value to a DIE and return it. */
7284 static inline void
7285 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7286 unsigned int length, unsigned int elt_size, unsigned char *array)
7288 dw_attr_node attr;
7290 attr.dw_attr = attr_kind;
7291 attr.dw_attr_val.val_class = dw_val_class_vec;
7292 attr.dw_attr_val.v.val_vec.length = length;
7293 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7294 attr.dw_attr_val.v.val_vec.array = array;
7295 add_dwarf_attr (die, &attr);
7298 /* Add an 8-byte data attribute value to a DIE. */
7300 static inline void
7301 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7302 unsigned char data8[8])
7304 dw_attr_node attr;
7306 attr.dw_attr = attr_kind;
7307 attr.dw_attr_val.val_class = dw_val_class_data8;
7308 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7309 add_dwarf_attr (die, &attr);
7312 /* Hash and equality functions for debug_str_hash. */
7314 static hashval_t
7315 debug_str_do_hash (const void *x)
7317 return htab_hash_string (((const struct indirect_string_node *)x)->str);
7320 static int
7321 debug_str_eq (const void *x1, const void *x2)
7323 return strcmp ((((const struct indirect_string_node *)x1)->str),
7324 (const char *)x2) == 0;
7327 /* Add STR to the indirect string hash table. */
7329 static struct indirect_string_node *
7330 find_AT_string (const char *str)
7332 struct indirect_string_node *node;
7333 void **slot;
7335 if (! debug_str_hash)
7336 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7337 debug_str_eq, NULL);
7339 slot = htab_find_slot_with_hash (debug_str_hash, str,
7340 htab_hash_string (str), INSERT);
7341 if (*slot == NULL)
7343 node = ggc_alloc_cleared_indirect_string_node ();
7344 node->str = ggc_strdup (str);
7345 *slot = node;
7347 else
7348 node = (struct indirect_string_node *) *slot;
7350 node->refcount++;
7351 return node;
7354 /* Add a string attribute value to a DIE. */
7356 static inline void
7357 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7359 dw_attr_node attr;
7360 struct indirect_string_node *node;
7362 node = find_AT_string (str);
7364 attr.dw_attr = attr_kind;
7365 attr.dw_attr_val.val_class = dw_val_class_str;
7366 attr.dw_attr_val.v.val_str = node;
7367 add_dwarf_attr (die, &attr);
7370 /* Create a label for an indirect string node, ensuring it is going to
7371 be output, unless its reference count goes down to zero. */
7373 static inline void
7374 gen_label_for_indirect_string (struct indirect_string_node *node)
7376 char label[32];
7378 if (node->label)
7379 return;
7381 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7382 ++dw2_string_counter;
7383 node->label = xstrdup (label);
7386 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7387 debug string STR. */
7389 static inline rtx
7390 get_debug_string_label (const char *str)
7392 struct indirect_string_node *node = find_AT_string (str);
7394 debug_str_hash_forced = true;
7396 gen_label_for_indirect_string (node);
7398 return gen_rtx_SYMBOL_REF (Pmode, node->label);
7401 static inline const char *
7402 AT_string (dw_attr_ref a)
7404 gcc_assert (a && AT_class (a) == dw_val_class_str);
7405 return a->dw_attr_val.v.val_str->str;
7408 /* Find out whether a string should be output inline in DIE
7409 or out-of-line in .debug_str section. */
7411 static enum dwarf_form
7412 AT_string_form (dw_attr_ref a)
7414 struct indirect_string_node *node;
7415 unsigned int len;
7417 gcc_assert (a && AT_class (a) == dw_val_class_str);
7419 node = a->dw_attr_val.v.val_str;
7420 if (node->form)
7421 return node->form;
7423 len = strlen (node->str) + 1;
7425 /* If the string is shorter or equal to the size of the reference, it is
7426 always better to put it inline. */
7427 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7428 return node->form = DW_FORM_string;
7430 /* If we cannot expect the linker to merge strings in .debug_str
7431 section, only put it into .debug_str if it is worth even in this
7432 single module. */
7433 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7434 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7435 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7436 return node->form = DW_FORM_string;
7438 gen_label_for_indirect_string (node);
7440 return node->form = DW_FORM_strp;
7443 /* Add a DIE reference attribute value to a DIE. */
7445 static inline void
7446 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7448 dw_attr_node attr;
7450 #ifdef ENABLE_CHECKING
7451 gcc_assert (targ_die != NULL);
7452 #else
7453 /* With LTO we can end up trying to reference something we didn't create
7454 a DIE for. Avoid crashing later on a NULL referenced DIE. */
7455 if (targ_die == NULL)
7456 return;
7457 #endif
7459 attr.dw_attr = attr_kind;
7460 attr.dw_attr_val.val_class = dw_val_class_die_ref;
7461 attr.dw_attr_val.v.val_die_ref.die = targ_die;
7462 attr.dw_attr_val.v.val_die_ref.external = 0;
7463 add_dwarf_attr (die, &attr);
7466 /* Add an AT_specification attribute to a DIE, and also make the back
7467 pointer from the specification to the definition. */
7469 static inline void
7470 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7472 add_AT_die_ref (die, DW_AT_specification, targ_die);
7473 gcc_assert (!targ_die->die_definition);
7474 targ_die->die_definition = die;
7477 static inline dw_die_ref
7478 AT_ref (dw_attr_ref a)
7480 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7481 return a->dw_attr_val.v.val_die_ref.die;
7484 static inline int
7485 AT_ref_external (dw_attr_ref a)
7487 if (a && AT_class (a) == dw_val_class_die_ref)
7488 return a->dw_attr_val.v.val_die_ref.external;
7490 return 0;
7493 static inline void
7494 set_AT_ref_external (dw_attr_ref a, int i)
7496 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7497 a->dw_attr_val.v.val_die_ref.external = i;
7500 /* Add an FDE reference attribute value to a DIE. */
7502 static inline void
7503 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7505 dw_attr_node attr;
7507 attr.dw_attr = attr_kind;
7508 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7509 attr.dw_attr_val.v.val_fde_index = targ_fde;
7510 add_dwarf_attr (die, &attr);
7513 /* Add a location description attribute value to a DIE. */
7515 static inline void
7516 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7518 dw_attr_node attr;
7520 attr.dw_attr = attr_kind;
7521 attr.dw_attr_val.val_class = dw_val_class_loc;
7522 attr.dw_attr_val.v.val_loc = loc;
7523 add_dwarf_attr (die, &attr);
7526 static inline dw_loc_descr_ref
7527 AT_loc (dw_attr_ref a)
7529 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7530 return a->dw_attr_val.v.val_loc;
7533 static inline void
7534 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7536 dw_attr_node attr;
7538 attr.dw_attr = attr_kind;
7539 attr.dw_attr_val.val_class = dw_val_class_loc_list;
7540 attr.dw_attr_val.v.val_loc_list = loc_list;
7541 add_dwarf_attr (die, &attr);
7542 have_location_lists = true;
7545 static inline dw_loc_list_ref
7546 AT_loc_list (dw_attr_ref a)
7548 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7549 return a->dw_attr_val.v.val_loc_list;
7552 static inline dw_loc_list_ref *
7553 AT_loc_list_ptr (dw_attr_ref a)
7555 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7556 return &a->dw_attr_val.v.val_loc_list;
7559 /* Add an address constant attribute value to a DIE. */
7561 static inline void
7562 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7564 dw_attr_node attr;
7566 attr.dw_attr = attr_kind;
7567 attr.dw_attr_val.val_class = dw_val_class_addr;
7568 attr.dw_attr_val.v.val_addr = addr;
7569 add_dwarf_attr (die, &attr);
7572 /* Get the RTX from to an address DIE attribute. */
7574 static inline rtx
7575 AT_addr (dw_attr_ref a)
7577 gcc_assert (a && AT_class (a) == dw_val_class_addr);
7578 return a->dw_attr_val.v.val_addr;
7581 /* Add a file attribute value to a DIE. */
7583 static inline void
7584 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7585 struct dwarf_file_data *fd)
7587 dw_attr_node attr;
7589 attr.dw_attr = attr_kind;
7590 attr.dw_attr_val.val_class = dw_val_class_file;
7591 attr.dw_attr_val.v.val_file = fd;
7592 add_dwarf_attr (die, &attr);
7595 /* Get the dwarf_file_data from a file DIE attribute. */
7597 static inline struct dwarf_file_data *
7598 AT_file (dw_attr_ref a)
7600 gcc_assert (a && AT_class (a) == dw_val_class_file);
7601 return a->dw_attr_val.v.val_file;
7604 /* Add a vms delta attribute value to a DIE. */
7606 static inline void
7607 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7608 const char *lbl1, const char *lbl2)
7610 dw_attr_node attr;
7612 attr.dw_attr = attr_kind;
7613 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7614 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7615 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7616 add_dwarf_attr (die, &attr);
7619 /* Add a label identifier attribute value to a DIE. */
7621 static inline void
7622 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7624 dw_attr_node attr;
7626 attr.dw_attr = attr_kind;
7627 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7628 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7629 add_dwarf_attr (die, &attr);
7632 /* Add a section offset attribute value to a DIE, an offset into the
7633 debug_line section. */
7635 static inline void
7636 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7637 const char *label)
7639 dw_attr_node attr;
7641 attr.dw_attr = attr_kind;
7642 attr.dw_attr_val.val_class = dw_val_class_lineptr;
7643 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7644 add_dwarf_attr (die, &attr);
7647 /* Add a section offset attribute value to a DIE, an offset into the
7648 debug_macinfo section. */
7650 static inline void
7651 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7652 const char *label)
7654 dw_attr_node attr;
7656 attr.dw_attr = attr_kind;
7657 attr.dw_attr_val.val_class = dw_val_class_macptr;
7658 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7659 add_dwarf_attr (die, &attr);
7662 /* Add an offset attribute value to a DIE. */
7664 static inline void
7665 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7666 unsigned HOST_WIDE_INT offset)
7668 dw_attr_node attr;
7670 attr.dw_attr = attr_kind;
7671 attr.dw_attr_val.val_class = dw_val_class_offset;
7672 attr.dw_attr_val.v.val_offset = offset;
7673 add_dwarf_attr (die, &attr);
7676 /* Add an range_list attribute value to a DIE. */
7678 static void
7679 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7680 long unsigned int offset)
7682 dw_attr_node attr;
7684 attr.dw_attr = attr_kind;
7685 attr.dw_attr_val.val_class = dw_val_class_range_list;
7686 attr.dw_attr_val.v.val_offset = offset;
7687 add_dwarf_attr (die, &attr);
7690 /* Return the start label of a delta attribute. */
7692 static inline const char *
7693 AT_vms_delta1 (dw_attr_ref a)
7695 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7696 return a->dw_attr_val.v.val_vms_delta.lbl1;
7699 /* Return the end label of a delta attribute. */
7701 static inline const char *
7702 AT_vms_delta2 (dw_attr_ref a)
7704 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7705 return a->dw_attr_val.v.val_vms_delta.lbl2;
7708 static inline const char *
7709 AT_lbl (dw_attr_ref a)
7711 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7712 || AT_class (a) == dw_val_class_lineptr
7713 || AT_class (a) == dw_val_class_macptr));
7714 return a->dw_attr_val.v.val_lbl_id;
7717 /* Get the attribute of type attr_kind. */
7719 static dw_attr_ref
7720 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7722 dw_attr_ref a;
7723 unsigned ix;
7724 dw_die_ref spec = NULL;
7726 if (! die)
7727 return NULL;
7729 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7730 if (a->dw_attr == attr_kind)
7731 return a;
7732 else if (a->dw_attr == DW_AT_specification
7733 || a->dw_attr == DW_AT_abstract_origin)
7734 spec = AT_ref (a);
7736 if (spec)
7737 return get_AT (spec, attr_kind);
7739 return NULL;
7742 /* Return the "low pc" attribute value, typically associated with a subprogram
7743 DIE. Return null if the "low pc" attribute is either not present, or if it
7744 cannot be represented as an assembler label identifier. */
7746 static inline const char *
7747 get_AT_low_pc (dw_die_ref die)
7749 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7751 return a ? AT_lbl (a) : NULL;
7754 /* Return the "high pc" attribute value, typically associated with a subprogram
7755 DIE. Return null if the "high pc" attribute is either not present, or if it
7756 cannot be represented as an assembler label identifier. */
7758 static inline const char *
7759 get_AT_hi_pc (dw_die_ref die)
7761 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7763 return a ? AT_lbl (a) : NULL;
7766 /* Return the value of the string attribute designated by ATTR_KIND, or
7767 NULL if it is not present. */
7769 static inline const char *
7770 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7772 dw_attr_ref a = get_AT (die, attr_kind);
7774 return a ? AT_string (a) : NULL;
7777 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7778 if it is not present. */
7780 static inline int
7781 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7783 dw_attr_ref a = get_AT (die, attr_kind);
7785 return a ? AT_flag (a) : 0;
7788 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7789 if it is not present. */
7791 static inline unsigned
7792 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7794 dw_attr_ref a = get_AT (die, attr_kind);
7796 return a ? AT_unsigned (a) : 0;
7799 static inline dw_die_ref
7800 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7802 dw_attr_ref a = get_AT (die, attr_kind);
7804 return a ? AT_ref (a) : NULL;
7807 static inline struct dwarf_file_data *
7808 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7810 dw_attr_ref a = get_AT (die, attr_kind);
7812 return a ? AT_file (a) : NULL;
7815 /* Return TRUE if the language is C++. */
7817 static inline bool
7818 is_cxx (void)
7820 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7822 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7825 /* Return TRUE if the language is Fortran. */
7827 static inline bool
7828 is_fortran (void)
7830 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7832 return (lang == DW_LANG_Fortran77
7833 || lang == DW_LANG_Fortran90
7834 || lang == DW_LANG_Fortran95);
7837 /* Return TRUE if the language is Ada. */
7839 static inline bool
7840 is_ada (void)
7842 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7844 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7847 /* Remove the specified attribute if present. */
7849 static void
7850 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7852 dw_attr_ref a;
7853 unsigned ix;
7855 if (! die)
7856 return;
7858 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7859 if (a->dw_attr == attr_kind)
7861 if (AT_class (a) == dw_val_class_str)
7862 if (a->dw_attr_val.v.val_str->refcount)
7863 a->dw_attr_val.v.val_str->refcount--;
7865 /* VEC_ordered_remove should help reduce the number of abbrevs
7866 that are needed. */
7867 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7868 return;
7872 /* Remove CHILD from its parent. PREV must have the property that
7873 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
7875 static void
7876 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7878 gcc_assert (child->die_parent == prev->die_parent);
7879 gcc_assert (prev->die_sib == child);
7880 if (prev == child)
7882 gcc_assert (child->die_parent->die_child == child);
7883 prev = NULL;
7885 else
7886 prev->die_sib = child->die_sib;
7887 if (child->die_parent->die_child == child)
7888 child->die_parent->die_child = prev;
7891 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
7892 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
7894 static void
7895 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7897 dw_die_ref parent = old_child->die_parent;
7899 gcc_assert (parent == prev->die_parent);
7900 gcc_assert (prev->die_sib == old_child);
7902 new_child->die_parent = parent;
7903 if (prev == old_child)
7905 gcc_assert (parent->die_child == old_child);
7906 new_child->die_sib = new_child;
7908 else
7910 prev->die_sib = new_child;
7911 new_child->die_sib = old_child->die_sib;
7913 if (old_child->die_parent->die_child == old_child)
7914 old_child->die_parent->die_child = new_child;
7917 /* Move all children from OLD_PARENT to NEW_PARENT. */
7919 static void
7920 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7922 dw_die_ref c;
7923 new_parent->die_child = old_parent->die_child;
7924 old_parent->die_child = NULL;
7925 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7928 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
7929 matches TAG. */
7931 static void
7932 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7934 dw_die_ref c;
7936 c = die->die_child;
7937 if (c) do {
7938 dw_die_ref prev = c;
7939 c = c->die_sib;
7940 while (c->die_tag == tag)
7942 remove_child_with_prev (c, prev);
7943 /* Might have removed every child. */
7944 if (c == c->die_sib)
7945 return;
7946 c = c->die_sib;
7948 } while (c != die->die_child);
7951 /* Add a CHILD_DIE as the last child of DIE. */
7953 static void
7954 add_child_die (dw_die_ref die, dw_die_ref child_die)
7956 /* FIXME this should probably be an assert. */
7957 if (! die || ! child_die)
7958 return;
7959 gcc_assert (die != child_die);
7961 child_die->die_parent = die;
7962 if (die->die_child)
7964 child_die->die_sib = die->die_child->die_sib;
7965 die->die_child->die_sib = child_die;
7967 else
7968 child_die->die_sib = child_die;
7969 die->die_child = child_die;
7972 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7973 is the specification, to the end of PARENT's list of children.
7974 This is done by removing and re-adding it. */
7976 static void
7977 splice_child_die (dw_die_ref parent, dw_die_ref child)
7979 dw_die_ref p;
7981 /* We want the declaration DIE from inside the class, not the
7982 specification DIE at toplevel. */
7983 if (child->die_parent != parent)
7985 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7987 if (tmp)
7988 child = tmp;
7991 gcc_assert (child->die_parent == parent
7992 || (child->die_parent
7993 == get_AT_ref (parent, DW_AT_specification)));
7995 for (p = child->die_parent->die_child; ; p = p->die_sib)
7996 if (p->die_sib == child)
7998 remove_child_with_prev (child, p);
7999 break;
8002 add_child_die (parent, child);
8005 /* Return a pointer to a newly created DIE node. */
8007 static inline dw_die_ref
8008 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8010 dw_die_ref die = ggc_alloc_cleared_die_node ();
8012 die->die_tag = tag_value;
8014 if (parent_die != NULL)
8015 add_child_die (parent_die, die);
8016 else
8018 limbo_die_node *limbo_node;
8020 limbo_node = ggc_alloc_cleared_limbo_die_node ();
8021 limbo_node->die = die;
8022 limbo_node->created_for = t;
8023 limbo_node->next = limbo_die_list;
8024 limbo_die_list = limbo_node;
8027 return die;
8030 /* Return the DIE associated with the given type specifier. */
8032 static inline dw_die_ref
8033 lookup_type_die (tree type)
8035 return TYPE_SYMTAB_DIE (type);
8038 /* Like lookup_type_die, but if type is an anonymous type named by a
8039 typedef[1], return the DIE of the anonymous type instead the one of
8040 the naming typedef. This is because in gen_typedef_die, we did
8041 equate the anonymous struct named by the typedef with the DIE of
8042 the naming typedef. So by default, lookup_type_die on an anonymous
8043 struct yields the DIE of the naming typedef.
8045 [1]: Read the comment of is_naming_typedef_decl to learn about what
8046 a naming typedef is. */
8048 static inline dw_die_ref
8049 lookup_type_die_strip_naming_typedef (tree type)
8051 dw_die_ref die = lookup_type_die (type);
8052 if (TREE_CODE (type) == RECORD_TYPE
8053 && die->die_tag == DW_TAG_typedef
8054 && is_naming_typedef_decl (TYPE_NAME (type)))
8055 die = get_AT_ref (die, DW_AT_type);
8056 return die;
8059 /* Equate a DIE to a given type specifier. */
8061 static inline void
8062 equate_type_number_to_die (tree type, dw_die_ref type_die)
8064 TYPE_SYMTAB_DIE (type) = type_die;
8067 /* Returns a hash value for X (which really is a die_struct). */
8069 static hashval_t
8070 decl_die_table_hash (const void *x)
8072 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8075 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
8077 static int
8078 decl_die_table_eq (const void *x, const void *y)
8080 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8083 /* Return the DIE associated with a given declaration. */
8085 static inline dw_die_ref
8086 lookup_decl_die (tree decl)
8088 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8091 /* Returns a hash value for X (which really is a var_loc_list). */
8093 static hashval_t
8094 decl_loc_table_hash (const void *x)
8096 return (hashval_t) ((const var_loc_list *) x)->decl_id;
8099 /* Return nonzero if decl_id of var_loc_list X is the same as
8100 UID of decl *Y. */
8102 static int
8103 decl_loc_table_eq (const void *x, const void *y)
8105 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8108 /* Return the var_loc list associated with a given declaration. */
8110 static inline var_loc_list *
8111 lookup_decl_loc (const_tree decl)
8113 if (!decl_loc_table)
8114 return NULL;
8115 return (var_loc_list *)
8116 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8119 /* Equate a DIE to a particular declaration. */
8121 static void
8122 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8124 unsigned int decl_id = DECL_UID (decl);
8125 void **slot;
8127 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8128 *slot = decl_die;
8129 decl_die->decl_id = decl_id;
8132 /* Return how many bits covers PIECE EXPR_LIST. */
8134 static int
8135 decl_piece_bitsize (rtx piece)
8137 int ret = (int) GET_MODE (piece);
8138 if (ret)
8139 return ret;
8140 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8141 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8142 return INTVAL (XEXP (XEXP (piece, 0), 0));
8145 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
8147 static rtx *
8148 decl_piece_varloc_ptr (rtx piece)
8150 if ((int) GET_MODE (piece))
8151 return &XEXP (piece, 0);
8152 else
8153 return &XEXP (XEXP (piece, 0), 1);
8156 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8157 Next is the chain of following piece nodes. */
8159 static rtx
8160 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8162 if (bitsize <= (int) MAX_MACHINE_MODE)
8163 return alloc_EXPR_LIST (bitsize, loc_note, next);
8164 else
8165 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8166 GEN_INT (bitsize),
8167 loc_note), next);
8170 /* Return rtx that should be stored into loc field for
8171 LOC_NOTE and BITPOS/BITSIZE. */
8173 static rtx
8174 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8175 HOST_WIDE_INT bitsize)
8177 if (bitsize != -1)
8179 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8180 if (bitpos != 0)
8181 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8183 return loc_note;
8186 /* This function either modifies location piece list *DEST in
8187 place (if SRC and INNER is NULL), or copies location piece list
8188 *SRC to *DEST while modifying it. Location BITPOS is modified
8189 to contain LOC_NOTE, any pieces overlapping it are removed resp.
8190 not copied and if needed some padding around it is added.
8191 When modifying in place, DEST should point to EXPR_LIST where
8192 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8193 to the start of the whole list and INNER points to the EXPR_LIST
8194 where earlier pieces cover PIECE_BITPOS bits. */
8196 static void
8197 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8198 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8199 HOST_WIDE_INT bitsize, rtx loc_note)
8201 int diff;
8202 bool copy = inner != NULL;
8204 if (copy)
8206 /* First copy all nodes preceeding the current bitpos. */
8207 while (src != inner)
8209 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8210 decl_piece_bitsize (*src), NULL_RTX);
8211 dest = &XEXP (*dest, 1);
8212 src = &XEXP (*src, 1);
8215 /* Add padding if needed. */
8216 if (bitpos != piece_bitpos)
8218 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8219 copy ? NULL_RTX : *dest);
8220 dest = &XEXP (*dest, 1);
8222 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8224 gcc_assert (!copy);
8225 /* A piece with correct bitpos and bitsize already exist,
8226 just update the location for it and return. */
8227 *decl_piece_varloc_ptr (*dest) = loc_note;
8228 return;
8230 /* Add the piece that changed. */
8231 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8232 dest = &XEXP (*dest, 1);
8233 /* Skip over pieces that overlap it. */
8234 diff = bitpos - piece_bitpos + bitsize;
8235 if (!copy)
8236 src = dest;
8237 while (diff > 0 && *src)
8239 rtx piece = *src;
8240 diff -= decl_piece_bitsize (piece);
8241 if (copy)
8242 src = &XEXP (piece, 1);
8243 else
8245 *src = XEXP (piece, 1);
8246 free_EXPR_LIST_node (piece);
8249 /* Add padding if needed. */
8250 if (diff < 0 && *src)
8252 if (!copy)
8253 dest = src;
8254 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8255 dest = &XEXP (*dest, 1);
8257 if (!copy)
8258 return;
8259 /* Finally copy all nodes following it. */
8260 while (*src)
8262 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8263 decl_piece_bitsize (*src), NULL_RTX);
8264 dest = &XEXP (*dest, 1);
8265 src = &XEXP (*src, 1);
8269 /* Add a variable location node to the linked list for DECL. */
8271 static struct var_loc_node *
8272 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8274 unsigned int decl_id;
8275 var_loc_list *temp;
8276 void **slot;
8277 struct var_loc_node *loc = NULL;
8278 HOST_WIDE_INT bitsize = -1, bitpos = -1;
8280 if (DECL_DEBUG_EXPR_IS_FROM (decl))
8282 tree realdecl = DECL_DEBUG_EXPR (decl);
8283 if (realdecl && handled_component_p (realdecl))
8285 HOST_WIDE_INT maxsize;
8286 tree innerdecl;
8287 innerdecl
8288 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8289 if (!DECL_P (innerdecl)
8290 || DECL_IGNORED_P (innerdecl)
8291 || TREE_STATIC (innerdecl)
8292 || bitsize <= 0
8293 || bitpos + bitsize > 256
8294 || bitsize != maxsize)
8295 return NULL;
8296 decl = innerdecl;
8300 decl_id = DECL_UID (decl);
8301 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8302 if (*slot == NULL)
8304 temp = ggc_alloc_cleared_var_loc_list ();
8305 temp->decl_id = decl_id;
8306 *slot = temp;
8308 else
8309 temp = (var_loc_list *) *slot;
8311 if (temp->last)
8313 struct var_loc_node *last = temp->last, *unused = NULL;
8314 rtx *piece_loc = NULL, last_loc_note;
8315 int piece_bitpos = 0;
8316 if (last->next)
8318 last = last->next;
8319 gcc_assert (last->next == NULL);
8321 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8323 piece_loc = &last->loc;
8326 int cur_bitsize = decl_piece_bitsize (*piece_loc);
8327 if (piece_bitpos + cur_bitsize > bitpos)
8328 break;
8329 piece_bitpos += cur_bitsize;
8330 piece_loc = &XEXP (*piece_loc, 1);
8332 while (*piece_loc);
8334 /* TEMP->LAST here is either pointer to the last but one or
8335 last element in the chained list, LAST is pointer to the
8336 last element. */
8337 if (label && strcmp (last->label, label) == 0)
8339 /* For SRA optimized variables if there weren't any real
8340 insns since last note, just modify the last node. */
8341 if (piece_loc != NULL)
8343 adjust_piece_list (piece_loc, NULL, NULL,
8344 bitpos, piece_bitpos, bitsize, loc_note);
8345 return NULL;
8347 /* If the last note doesn't cover any instructions, remove it. */
8348 if (temp->last != last)
8350 temp->last->next = NULL;
8351 unused = last;
8352 last = temp->last;
8353 gcc_assert (strcmp (last->label, label) != 0);
8355 else
8357 gcc_assert (temp->first == temp->last);
8358 memset (temp->last, '\0', sizeof (*temp->last));
8359 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8360 return temp->last;
8363 if (bitsize == -1 && NOTE_P (last->loc))
8364 last_loc_note = last->loc;
8365 else if (piece_loc != NULL
8366 && *piece_loc != NULL_RTX
8367 && piece_bitpos == bitpos
8368 && decl_piece_bitsize (*piece_loc) == bitsize)
8369 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8370 else
8371 last_loc_note = NULL_RTX;
8372 /* If the current location is the same as the end of the list,
8373 and either both or neither of the locations is uninitialized,
8374 we have nothing to do. */
8375 if (last_loc_note == NULL_RTX
8376 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8377 NOTE_VAR_LOCATION_LOC (loc_note)))
8378 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8379 != NOTE_VAR_LOCATION_STATUS (loc_note))
8380 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8381 == VAR_INIT_STATUS_UNINITIALIZED)
8382 || (NOTE_VAR_LOCATION_STATUS (loc_note)
8383 == VAR_INIT_STATUS_UNINITIALIZED))))
8385 /* Add LOC to the end of list and update LAST. If the last
8386 element of the list has been removed above, reuse its
8387 memory for the new node, otherwise allocate a new one. */
8388 if (unused)
8390 loc = unused;
8391 memset (loc, '\0', sizeof (*loc));
8393 else
8394 loc = ggc_alloc_cleared_var_loc_node ();
8395 if (bitsize == -1 || piece_loc == NULL)
8396 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8397 else
8398 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8399 bitpos, piece_bitpos, bitsize, loc_note);
8400 last->next = loc;
8401 /* Ensure TEMP->LAST will point either to the new last but one
8402 element of the chain, or to the last element in it. */
8403 if (last != temp->last)
8404 temp->last = last;
8406 else if (unused)
8407 ggc_free (unused);
8409 else
8411 loc = ggc_alloc_cleared_var_loc_node ();
8412 temp->first = loc;
8413 temp->last = loc;
8414 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8416 return loc;
8419 /* Keep track of the number of spaces used to indent the
8420 output of the debugging routines that print the structure of
8421 the DIE internal representation. */
8422 static int print_indent;
8424 /* Indent the line the number of spaces given by print_indent. */
8426 static inline void
8427 print_spaces (FILE *outfile)
8429 fprintf (outfile, "%*s", print_indent, "");
8432 /* Print a type signature in hex. */
8434 static inline void
8435 print_signature (FILE *outfile, char *sig)
8437 int i;
8439 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8440 fprintf (outfile, "%02x", sig[i] & 0xff);
8443 /* Print the information associated with a given DIE, and its children.
8444 This routine is a debugging aid only. */
8446 static void
8447 print_die (dw_die_ref die, FILE *outfile)
8449 dw_attr_ref a;
8450 dw_die_ref c;
8451 unsigned ix;
8453 print_spaces (outfile);
8454 fprintf (outfile, "DIE %4ld: %s (%p)\n",
8455 die->die_offset, dwarf_tag_name (die->die_tag),
8456 (void*) die);
8457 print_spaces (outfile);
8458 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
8459 fprintf (outfile, " offset: %ld", die->die_offset);
8460 fprintf (outfile, " mark: %d\n", die->die_mark);
8462 if (dwarf_version >= 4 && die->die_id.die_type_node)
8464 print_spaces (outfile);
8465 fprintf (outfile, " signature: ");
8466 print_signature (outfile, die->die_id.die_type_node->signature);
8467 fprintf (outfile, "\n");
8470 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8472 print_spaces (outfile);
8473 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
8475 switch (AT_class (a))
8477 case dw_val_class_addr:
8478 fprintf (outfile, "address");
8479 break;
8480 case dw_val_class_offset:
8481 fprintf (outfile, "offset");
8482 break;
8483 case dw_val_class_loc:
8484 fprintf (outfile, "location descriptor");
8485 break;
8486 case dw_val_class_loc_list:
8487 fprintf (outfile, "location list -> label:%s",
8488 AT_loc_list (a)->ll_symbol);
8489 break;
8490 case dw_val_class_range_list:
8491 fprintf (outfile, "range list");
8492 break;
8493 case dw_val_class_const:
8494 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8495 break;
8496 case dw_val_class_unsigned_const:
8497 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8498 break;
8499 case dw_val_class_const_double:
8500 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8501 HOST_WIDE_INT_PRINT_UNSIGNED")",
8502 a->dw_attr_val.v.val_double.high,
8503 a->dw_attr_val.v.val_double.low);
8504 break;
8505 case dw_val_class_vec:
8506 fprintf (outfile, "floating-point or vector constant");
8507 break;
8508 case dw_val_class_flag:
8509 fprintf (outfile, "%u", AT_flag (a));
8510 break;
8511 case dw_val_class_die_ref:
8512 if (AT_ref (a) != NULL)
8514 if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8516 fprintf (outfile, "die -> signature: ");
8517 print_signature (outfile,
8518 AT_ref (a)->die_id.die_type_node->signature);
8520 else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8521 fprintf (outfile, "die -> label: %s",
8522 AT_ref (a)->die_id.die_symbol);
8523 else
8524 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8525 fprintf (outfile, " (%p)", (void *) AT_ref (a));
8527 else
8528 fprintf (outfile, "die -> <null>");
8529 break;
8530 case dw_val_class_vms_delta:
8531 fprintf (outfile, "delta: @slotcount(%s-%s)",
8532 AT_vms_delta2 (a), AT_vms_delta1 (a));
8533 break;
8534 case dw_val_class_lbl_id:
8535 case dw_val_class_lineptr:
8536 case dw_val_class_macptr:
8537 fprintf (outfile, "label: %s", AT_lbl (a));
8538 break;
8539 case dw_val_class_str:
8540 if (AT_string (a) != NULL)
8541 fprintf (outfile, "\"%s\"", AT_string (a));
8542 else
8543 fprintf (outfile, "<null>");
8544 break;
8545 case dw_val_class_file:
8546 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8547 AT_file (a)->emitted_number);
8548 break;
8549 case dw_val_class_data8:
8551 int i;
8553 for (i = 0; i < 8; i++)
8554 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8555 break;
8557 default:
8558 break;
8561 fprintf (outfile, "\n");
8564 if (die->die_child != NULL)
8566 print_indent += 4;
8567 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8568 print_indent -= 4;
8570 if (print_indent == 0)
8571 fprintf (outfile, "\n");
8574 /* Print the contents of the source code line number correspondence table.
8575 This routine is a debugging aid only. */
8577 static void
8578 print_dwarf_line_table (FILE *outfile)
8580 unsigned i;
8581 dw_line_info_ref line_info;
8583 fprintf (outfile, "\n\nDWARF source line information\n");
8584 for (i = 1; i < line_info_table_in_use; i++)
8586 line_info = &line_info_table[i];
8587 fprintf (outfile, "%5d: %4ld %6ld\n", i,
8588 line_info->dw_file_num,
8589 line_info->dw_line_num);
8592 fprintf (outfile, "\n\n");
8595 /* Print the information collected for a given DIE. */
8597 DEBUG_FUNCTION void
8598 debug_dwarf_die (dw_die_ref die)
8600 print_die (die, stderr);
8603 /* Print all DWARF information collected for the compilation unit.
8604 This routine is a debugging aid only. */
8606 DEBUG_FUNCTION void
8607 debug_dwarf (void)
8609 print_indent = 0;
8610 print_die (comp_unit_die (), stderr);
8611 if (! DWARF2_ASM_LINE_DEBUG_INFO)
8612 print_dwarf_line_table (stderr);
8615 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
8616 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
8617 DIE that marks the start of the DIEs for this include file. */
8619 static dw_die_ref
8620 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8622 const char *filename = get_AT_string (bincl_die, DW_AT_name);
8623 dw_die_ref new_unit = gen_compile_unit_die (filename);
8625 new_unit->die_sib = old_unit;
8626 return new_unit;
8629 /* Close an include-file CU and reopen the enclosing one. */
8631 static dw_die_ref
8632 pop_compile_unit (dw_die_ref old_unit)
8634 dw_die_ref new_unit = old_unit->die_sib;
8636 old_unit->die_sib = NULL;
8637 return new_unit;
8640 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8641 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8643 /* Calculate the checksum of a location expression. */
8645 static inline void
8646 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8648 int tem;
8650 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8651 CHECKSUM (tem);
8652 CHECKSUM (loc->dw_loc_oprnd1);
8653 CHECKSUM (loc->dw_loc_oprnd2);
8656 /* Calculate the checksum of an attribute. */
8658 static void
8659 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8661 dw_loc_descr_ref loc;
8662 rtx r;
8664 CHECKSUM (at->dw_attr);
8666 /* We don't care that this was compiled with a different compiler
8667 snapshot; if the output is the same, that's what matters. */
8668 if (at->dw_attr == DW_AT_producer)
8669 return;
8671 switch (AT_class (at))
8673 case dw_val_class_const:
8674 CHECKSUM (at->dw_attr_val.v.val_int);
8675 break;
8676 case dw_val_class_unsigned_const:
8677 CHECKSUM (at->dw_attr_val.v.val_unsigned);
8678 break;
8679 case dw_val_class_const_double:
8680 CHECKSUM (at->dw_attr_val.v.val_double);
8681 break;
8682 case dw_val_class_vec:
8683 CHECKSUM (at->dw_attr_val.v.val_vec);
8684 break;
8685 case dw_val_class_flag:
8686 CHECKSUM (at->dw_attr_val.v.val_flag);
8687 break;
8688 case dw_val_class_str:
8689 CHECKSUM_STRING (AT_string (at));
8690 break;
8692 case dw_val_class_addr:
8693 r = AT_addr (at);
8694 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8695 CHECKSUM_STRING (XSTR (r, 0));
8696 break;
8698 case dw_val_class_offset:
8699 CHECKSUM (at->dw_attr_val.v.val_offset);
8700 break;
8702 case dw_val_class_loc:
8703 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8704 loc_checksum (loc, ctx);
8705 break;
8707 case dw_val_class_die_ref:
8708 die_checksum (AT_ref (at), ctx, mark);
8709 break;
8711 case dw_val_class_fde_ref:
8712 case dw_val_class_vms_delta:
8713 case dw_val_class_lbl_id:
8714 case dw_val_class_lineptr:
8715 case dw_val_class_macptr:
8716 break;
8718 case dw_val_class_file:
8719 CHECKSUM_STRING (AT_file (at)->filename);
8720 break;
8722 case dw_val_class_data8:
8723 CHECKSUM (at->dw_attr_val.v.val_data8);
8724 break;
8726 default:
8727 break;
8731 /* Calculate the checksum of a DIE. */
8733 static void
8734 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8736 dw_die_ref c;
8737 dw_attr_ref a;
8738 unsigned ix;
8740 /* To avoid infinite recursion. */
8741 if (die->die_mark)
8743 CHECKSUM (die->die_mark);
8744 return;
8746 die->die_mark = ++(*mark);
8748 CHECKSUM (die->die_tag);
8750 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8751 attr_checksum (a, ctx, mark);
8753 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8756 #undef CHECKSUM
8757 #undef CHECKSUM_STRING
8759 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
8760 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8761 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8762 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8763 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8764 #define CHECKSUM_ATTR(FOO) \
8765 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8767 /* Calculate the checksum of a number in signed LEB128 format. */
8769 static void
8770 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8772 unsigned char byte;
8773 bool more;
8775 while (1)
8777 byte = (value & 0x7f);
8778 value >>= 7;
8779 more = !((value == 0 && (byte & 0x40) == 0)
8780 || (value == -1 && (byte & 0x40) != 0));
8781 if (more)
8782 byte |= 0x80;
8783 CHECKSUM (byte);
8784 if (!more)
8785 break;
8789 /* Calculate the checksum of a number in unsigned LEB128 format. */
8791 static void
8792 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8794 while (1)
8796 unsigned char byte = (value & 0x7f);
8797 value >>= 7;
8798 if (value != 0)
8799 /* More bytes to follow. */
8800 byte |= 0x80;
8801 CHECKSUM (byte);
8802 if (value == 0)
8803 break;
8807 /* Checksum the context of the DIE. This adds the names of any
8808 surrounding namespaces or structures to the checksum. */
8810 static void
8811 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8813 const char *name;
8814 dw_die_ref spec;
8815 int tag = die->die_tag;
8817 if (tag != DW_TAG_namespace
8818 && tag != DW_TAG_structure_type
8819 && tag != DW_TAG_class_type)
8820 return;
8822 name = get_AT_string (die, DW_AT_name);
8824 spec = get_AT_ref (die, DW_AT_specification);
8825 if (spec != NULL)
8826 die = spec;
8828 if (die->die_parent != NULL)
8829 checksum_die_context (die->die_parent, ctx);
8831 CHECKSUM_ULEB128 ('C');
8832 CHECKSUM_ULEB128 (tag);
8833 if (name != NULL)
8834 CHECKSUM_STRING (name);
8837 /* Calculate the checksum of a location expression. */
8839 static inline void
8840 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8842 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8843 were emitted as a DW_FORM_sdata instead of a location expression. */
8844 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8846 CHECKSUM_ULEB128 (DW_FORM_sdata);
8847 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8848 return;
8851 /* Otherwise, just checksum the raw location expression. */
8852 while (loc != NULL)
8854 CHECKSUM_ULEB128 (loc->dw_loc_opc);
8855 CHECKSUM (loc->dw_loc_oprnd1);
8856 CHECKSUM (loc->dw_loc_oprnd2);
8857 loc = loc->dw_loc_next;
8861 /* Calculate the checksum of an attribute. */
8863 static void
8864 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8865 struct md5_ctx *ctx, int *mark)
8867 dw_loc_descr_ref loc;
8868 rtx r;
8870 if (AT_class (at) == dw_val_class_die_ref)
8872 dw_die_ref target_die = AT_ref (at);
8874 /* For pointer and reference types, we checksum only the (qualified)
8875 name of the target type (if there is a name). For friend entries,
8876 we checksum only the (qualified) name of the target type or function.
8877 This allows the checksum to remain the same whether the target type
8878 is complete or not. */
8879 if ((at->dw_attr == DW_AT_type
8880 && (tag == DW_TAG_pointer_type
8881 || tag == DW_TAG_reference_type
8882 || tag == DW_TAG_rvalue_reference_type
8883 || tag == DW_TAG_ptr_to_member_type))
8884 || (at->dw_attr == DW_AT_friend
8885 && tag == DW_TAG_friend))
8887 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8889 if (name_attr != NULL)
8891 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8893 if (decl == NULL)
8894 decl = target_die;
8895 CHECKSUM_ULEB128 ('N');
8896 CHECKSUM_ULEB128 (at->dw_attr);
8897 if (decl->die_parent != NULL)
8898 checksum_die_context (decl->die_parent, ctx);
8899 CHECKSUM_ULEB128 ('E');
8900 CHECKSUM_STRING (AT_string (name_attr));
8901 return;
8905 /* For all other references to another DIE, we check to see if the
8906 target DIE has already been visited. If it has, we emit a
8907 backward reference; if not, we descend recursively. */
8908 if (target_die->die_mark > 0)
8910 CHECKSUM_ULEB128 ('R');
8911 CHECKSUM_ULEB128 (at->dw_attr);
8912 CHECKSUM_ULEB128 (target_die->die_mark);
8914 else
8916 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8918 if (decl == NULL)
8919 decl = target_die;
8920 target_die->die_mark = ++(*mark);
8921 CHECKSUM_ULEB128 ('T');
8922 CHECKSUM_ULEB128 (at->dw_attr);
8923 if (decl->die_parent != NULL)
8924 checksum_die_context (decl->die_parent, ctx);
8925 die_checksum_ordered (target_die, ctx, mark);
8927 return;
8930 CHECKSUM_ULEB128 ('A');
8931 CHECKSUM_ULEB128 (at->dw_attr);
8933 switch (AT_class (at))
8935 case dw_val_class_const:
8936 CHECKSUM_ULEB128 (DW_FORM_sdata);
8937 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8938 break;
8940 case dw_val_class_unsigned_const:
8941 CHECKSUM_ULEB128 (DW_FORM_sdata);
8942 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8943 break;
8945 case dw_val_class_const_double:
8946 CHECKSUM_ULEB128 (DW_FORM_block);
8947 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8948 CHECKSUM (at->dw_attr_val.v.val_double);
8949 break;
8951 case dw_val_class_vec:
8952 CHECKSUM_ULEB128 (DW_FORM_block);
8953 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8954 CHECKSUM (at->dw_attr_val.v.val_vec);
8955 break;
8957 case dw_val_class_flag:
8958 CHECKSUM_ULEB128 (DW_FORM_flag);
8959 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8960 break;
8962 case dw_val_class_str:
8963 CHECKSUM_ULEB128 (DW_FORM_string);
8964 CHECKSUM_STRING (AT_string (at));
8965 break;
8967 case dw_val_class_addr:
8968 r = AT_addr (at);
8969 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8970 CHECKSUM_ULEB128 (DW_FORM_string);
8971 CHECKSUM_STRING (XSTR (r, 0));
8972 break;
8974 case dw_val_class_offset:
8975 CHECKSUM_ULEB128 (DW_FORM_sdata);
8976 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8977 break;
8979 case dw_val_class_loc:
8980 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8981 loc_checksum_ordered (loc, ctx);
8982 break;
8984 case dw_val_class_fde_ref:
8985 case dw_val_class_lbl_id:
8986 case dw_val_class_lineptr:
8987 case dw_val_class_macptr:
8988 break;
8990 case dw_val_class_file:
8991 CHECKSUM_ULEB128 (DW_FORM_string);
8992 CHECKSUM_STRING (AT_file (at)->filename);
8993 break;
8995 case dw_val_class_data8:
8996 CHECKSUM (at->dw_attr_val.v.val_data8);
8997 break;
8999 default:
9000 break;
9004 struct checksum_attributes
9006 dw_attr_ref at_name;
9007 dw_attr_ref at_type;
9008 dw_attr_ref at_friend;
9009 dw_attr_ref at_accessibility;
9010 dw_attr_ref at_address_class;
9011 dw_attr_ref at_allocated;
9012 dw_attr_ref at_artificial;
9013 dw_attr_ref at_associated;
9014 dw_attr_ref at_binary_scale;
9015 dw_attr_ref at_bit_offset;
9016 dw_attr_ref at_bit_size;
9017 dw_attr_ref at_bit_stride;
9018 dw_attr_ref at_byte_size;
9019 dw_attr_ref at_byte_stride;
9020 dw_attr_ref at_const_value;
9021 dw_attr_ref at_containing_type;
9022 dw_attr_ref at_count;
9023 dw_attr_ref at_data_location;
9024 dw_attr_ref at_data_member_location;
9025 dw_attr_ref at_decimal_scale;
9026 dw_attr_ref at_decimal_sign;
9027 dw_attr_ref at_default_value;
9028 dw_attr_ref at_digit_count;
9029 dw_attr_ref at_discr;
9030 dw_attr_ref at_discr_list;
9031 dw_attr_ref at_discr_value;
9032 dw_attr_ref at_encoding;
9033 dw_attr_ref at_endianity;
9034 dw_attr_ref at_explicit;
9035 dw_attr_ref at_is_optional;
9036 dw_attr_ref at_location;
9037 dw_attr_ref at_lower_bound;
9038 dw_attr_ref at_mutable;
9039 dw_attr_ref at_ordering;
9040 dw_attr_ref at_picture_string;
9041 dw_attr_ref at_prototyped;
9042 dw_attr_ref at_small;
9043 dw_attr_ref at_segment;
9044 dw_attr_ref at_string_length;
9045 dw_attr_ref at_threads_scaled;
9046 dw_attr_ref at_upper_bound;
9047 dw_attr_ref at_use_location;
9048 dw_attr_ref at_use_UTF8;
9049 dw_attr_ref at_variable_parameter;
9050 dw_attr_ref at_virtuality;
9051 dw_attr_ref at_visibility;
9052 dw_attr_ref at_vtable_elem_location;
9055 /* Collect the attributes that we will want to use for the checksum. */
9057 static void
9058 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9060 dw_attr_ref a;
9061 unsigned ix;
9063 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9065 switch (a->dw_attr)
9067 case DW_AT_name:
9068 attrs->at_name = a;
9069 break;
9070 case DW_AT_type:
9071 attrs->at_type = a;
9072 break;
9073 case DW_AT_friend:
9074 attrs->at_friend = a;
9075 break;
9076 case DW_AT_accessibility:
9077 attrs->at_accessibility = a;
9078 break;
9079 case DW_AT_address_class:
9080 attrs->at_address_class = a;
9081 break;
9082 case DW_AT_allocated:
9083 attrs->at_allocated = a;
9084 break;
9085 case DW_AT_artificial:
9086 attrs->at_artificial = a;
9087 break;
9088 case DW_AT_associated:
9089 attrs->at_associated = a;
9090 break;
9091 case DW_AT_binary_scale:
9092 attrs->at_binary_scale = a;
9093 break;
9094 case DW_AT_bit_offset:
9095 attrs->at_bit_offset = a;
9096 break;
9097 case DW_AT_bit_size:
9098 attrs->at_bit_size = a;
9099 break;
9100 case DW_AT_bit_stride:
9101 attrs->at_bit_stride = a;
9102 break;
9103 case DW_AT_byte_size:
9104 attrs->at_byte_size = a;
9105 break;
9106 case DW_AT_byte_stride:
9107 attrs->at_byte_stride = a;
9108 break;
9109 case DW_AT_const_value:
9110 attrs->at_const_value = a;
9111 break;
9112 case DW_AT_containing_type:
9113 attrs->at_containing_type = a;
9114 break;
9115 case DW_AT_count:
9116 attrs->at_count = a;
9117 break;
9118 case DW_AT_data_location:
9119 attrs->at_data_location = a;
9120 break;
9121 case DW_AT_data_member_location:
9122 attrs->at_data_member_location = a;
9123 break;
9124 case DW_AT_decimal_scale:
9125 attrs->at_decimal_scale = a;
9126 break;
9127 case DW_AT_decimal_sign:
9128 attrs->at_decimal_sign = a;
9129 break;
9130 case DW_AT_default_value:
9131 attrs->at_default_value = a;
9132 break;
9133 case DW_AT_digit_count:
9134 attrs->at_digit_count = a;
9135 break;
9136 case DW_AT_discr:
9137 attrs->at_discr = a;
9138 break;
9139 case DW_AT_discr_list:
9140 attrs->at_discr_list = a;
9141 break;
9142 case DW_AT_discr_value:
9143 attrs->at_discr_value = a;
9144 break;
9145 case DW_AT_encoding:
9146 attrs->at_encoding = a;
9147 break;
9148 case DW_AT_endianity:
9149 attrs->at_endianity = a;
9150 break;
9151 case DW_AT_explicit:
9152 attrs->at_explicit = a;
9153 break;
9154 case DW_AT_is_optional:
9155 attrs->at_is_optional = a;
9156 break;
9157 case DW_AT_location:
9158 attrs->at_location = a;
9159 break;
9160 case DW_AT_lower_bound:
9161 attrs->at_lower_bound = a;
9162 break;
9163 case DW_AT_mutable:
9164 attrs->at_mutable = a;
9165 break;
9166 case DW_AT_ordering:
9167 attrs->at_ordering = a;
9168 break;
9169 case DW_AT_picture_string:
9170 attrs->at_picture_string = a;
9171 break;
9172 case DW_AT_prototyped:
9173 attrs->at_prototyped = a;
9174 break;
9175 case DW_AT_small:
9176 attrs->at_small = a;
9177 break;
9178 case DW_AT_segment:
9179 attrs->at_segment = a;
9180 break;
9181 case DW_AT_string_length:
9182 attrs->at_string_length = a;
9183 break;
9184 case DW_AT_threads_scaled:
9185 attrs->at_threads_scaled = a;
9186 break;
9187 case DW_AT_upper_bound:
9188 attrs->at_upper_bound = a;
9189 break;
9190 case DW_AT_use_location:
9191 attrs->at_use_location = a;
9192 break;
9193 case DW_AT_use_UTF8:
9194 attrs->at_use_UTF8 = a;
9195 break;
9196 case DW_AT_variable_parameter:
9197 attrs->at_variable_parameter = a;
9198 break;
9199 case DW_AT_virtuality:
9200 attrs->at_virtuality = a;
9201 break;
9202 case DW_AT_visibility:
9203 attrs->at_visibility = a;
9204 break;
9205 case DW_AT_vtable_elem_location:
9206 attrs->at_vtable_elem_location = a;
9207 break;
9208 default:
9209 break;
9214 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
9216 static void
9217 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9219 dw_die_ref c;
9220 dw_die_ref decl;
9221 struct checksum_attributes attrs;
9223 CHECKSUM_ULEB128 ('D');
9224 CHECKSUM_ULEB128 (die->die_tag);
9226 memset (&attrs, 0, sizeof (attrs));
9228 decl = get_AT_ref (die, DW_AT_specification);
9229 if (decl != NULL)
9230 collect_checksum_attributes (&attrs, decl);
9231 collect_checksum_attributes (&attrs, die);
9233 CHECKSUM_ATTR (attrs.at_name);
9234 CHECKSUM_ATTR (attrs.at_accessibility);
9235 CHECKSUM_ATTR (attrs.at_address_class);
9236 CHECKSUM_ATTR (attrs.at_allocated);
9237 CHECKSUM_ATTR (attrs.at_artificial);
9238 CHECKSUM_ATTR (attrs.at_associated);
9239 CHECKSUM_ATTR (attrs.at_binary_scale);
9240 CHECKSUM_ATTR (attrs.at_bit_offset);
9241 CHECKSUM_ATTR (attrs.at_bit_size);
9242 CHECKSUM_ATTR (attrs.at_bit_stride);
9243 CHECKSUM_ATTR (attrs.at_byte_size);
9244 CHECKSUM_ATTR (attrs.at_byte_stride);
9245 CHECKSUM_ATTR (attrs.at_const_value);
9246 CHECKSUM_ATTR (attrs.at_containing_type);
9247 CHECKSUM_ATTR (attrs.at_count);
9248 CHECKSUM_ATTR (attrs.at_data_location);
9249 CHECKSUM_ATTR (attrs.at_data_member_location);
9250 CHECKSUM_ATTR (attrs.at_decimal_scale);
9251 CHECKSUM_ATTR (attrs.at_decimal_sign);
9252 CHECKSUM_ATTR (attrs.at_default_value);
9253 CHECKSUM_ATTR (attrs.at_digit_count);
9254 CHECKSUM_ATTR (attrs.at_discr);
9255 CHECKSUM_ATTR (attrs.at_discr_list);
9256 CHECKSUM_ATTR (attrs.at_discr_value);
9257 CHECKSUM_ATTR (attrs.at_encoding);
9258 CHECKSUM_ATTR (attrs.at_endianity);
9259 CHECKSUM_ATTR (attrs.at_explicit);
9260 CHECKSUM_ATTR (attrs.at_is_optional);
9261 CHECKSUM_ATTR (attrs.at_location);
9262 CHECKSUM_ATTR (attrs.at_lower_bound);
9263 CHECKSUM_ATTR (attrs.at_mutable);
9264 CHECKSUM_ATTR (attrs.at_ordering);
9265 CHECKSUM_ATTR (attrs.at_picture_string);
9266 CHECKSUM_ATTR (attrs.at_prototyped);
9267 CHECKSUM_ATTR (attrs.at_small);
9268 CHECKSUM_ATTR (attrs.at_segment);
9269 CHECKSUM_ATTR (attrs.at_string_length);
9270 CHECKSUM_ATTR (attrs.at_threads_scaled);
9271 CHECKSUM_ATTR (attrs.at_upper_bound);
9272 CHECKSUM_ATTR (attrs.at_use_location);
9273 CHECKSUM_ATTR (attrs.at_use_UTF8);
9274 CHECKSUM_ATTR (attrs.at_variable_parameter);
9275 CHECKSUM_ATTR (attrs.at_virtuality);
9276 CHECKSUM_ATTR (attrs.at_visibility);
9277 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9278 CHECKSUM_ATTR (attrs.at_type);
9279 CHECKSUM_ATTR (attrs.at_friend);
9281 /* Checksum the child DIEs, except for nested types and member functions. */
9282 c = die->die_child;
9283 if (c) do {
9284 dw_attr_ref name_attr;
9286 c = c->die_sib;
9287 name_attr = get_AT (c, DW_AT_name);
9288 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9289 && name_attr != NULL)
9291 CHECKSUM_ULEB128 ('S');
9292 CHECKSUM_ULEB128 (c->die_tag);
9293 CHECKSUM_STRING (AT_string (name_attr));
9295 else
9297 /* Mark this DIE so it gets processed when unmarking. */
9298 if (c->die_mark == 0)
9299 c->die_mark = -1;
9300 die_checksum_ordered (c, ctx, mark);
9302 } while (c != die->die_child);
9304 CHECKSUM_ULEB128 (0);
9307 #undef CHECKSUM
9308 #undef CHECKSUM_STRING
9309 #undef CHECKSUM_ATTR
9310 #undef CHECKSUM_LEB128
9311 #undef CHECKSUM_ULEB128
9313 /* Generate the type signature for DIE. This is computed by generating an
9314 MD5 checksum over the DIE's tag, its relevant attributes, and its
9315 children. Attributes that are references to other DIEs are processed
9316 by recursion, using the MARK field to prevent infinite recursion.
9317 If the DIE is nested inside a namespace or another type, we also
9318 need to include that context in the signature. The lower 64 bits
9319 of the resulting MD5 checksum comprise the signature. */
9321 static void
9322 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9324 int mark;
9325 const char *name;
9326 unsigned char checksum[16];
9327 struct md5_ctx ctx;
9328 dw_die_ref decl;
9330 name = get_AT_string (die, DW_AT_name);
9331 decl = get_AT_ref (die, DW_AT_specification);
9333 /* First, compute a signature for just the type name (and its surrounding
9334 context, if any. This is stored in the type unit DIE for link-time
9335 ODR (one-definition rule) checking. */
9337 if (is_cxx() && name != NULL)
9339 md5_init_ctx (&ctx);
9341 /* Checksum the names of surrounding namespaces and structures. */
9342 if (decl != NULL && decl->die_parent != NULL)
9343 checksum_die_context (decl->die_parent, &ctx);
9345 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9346 md5_process_bytes (name, strlen (name) + 1, &ctx);
9347 md5_finish_ctx (&ctx, checksum);
9349 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9352 /* Next, compute the complete type signature. */
9354 md5_init_ctx (&ctx);
9355 mark = 1;
9356 die->die_mark = mark;
9358 /* Checksum the names of surrounding namespaces and structures. */
9359 if (decl != NULL && decl->die_parent != NULL)
9360 checksum_die_context (decl->die_parent, &ctx);
9362 /* Checksum the DIE and its children. */
9363 die_checksum_ordered (die, &ctx, &mark);
9364 unmark_all_dies (die);
9365 md5_finish_ctx (&ctx, checksum);
9367 /* Store the signature in the type node and link the type DIE and the
9368 type node together. */
9369 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9370 DWARF_TYPE_SIGNATURE_SIZE);
9371 die->die_id.die_type_node = type_node;
9372 type_node->type_die = die;
9374 /* If the DIE is a specification, link its declaration to the type node
9375 as well. */
9376 if (decl != NULL)
9377 decl->die_id.die_type_node = type_node;
9380 /* Do the location expressions look same? */
9381 static inline int
9382 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9384 return loc1->dw_loc_opc == loc2->dw_loc_opc
9385 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9386 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9389 /* Do the values look the same? */
9390 static int
9391 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9393 dw_loc_descr_ref loc1, loc2;
9394 rtx r1, r2;
9396 if (v1->val_class != v2->val_class)
9397 return 0;
9399 switch (v1->val_class)
9401 case dw_val_class_const:
9402 return v1->v.val_int == v2->v.val_int;
9403 case dw_val_class_unsigned_const:
9404 return v1->v.val_unsigned == v2->v.val_unsigned;
9405 case dw_val_class_const_double:
9406 return v1->v.val_double.high == v2->v.val_double.high
9407 && v1->v.val_double.low == v2->v.val_double.low;
9408 case dw_val_class_vec:
9409 if (v1->v.val_vec.length != v2->v.val_vec.length
9410 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9411 return 0;
9412 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9413 v1->v.val_vec.length * v1->v.val_vec.elt_size))
9414 return 0;
9415 return 1;
9416 case dw_val_class_flag:
9417 return v1->v.val_flag == v2->v.val_flag;
9418 case dw_val_class_str:
9419 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9421 case dw_val_class_addr:
9422 r1 = v1->v.val_addr;
9423 r2 = v2->v.val_addr;
9424 if (GET_CODE (r1) != GET_CODE (r2))
9425 return 0;
9426 return !rtx_equal_p (r1, r2);
9428 case dw_val_class_offset:
9429 return v1->v.val_offset == v2->v.val_offset;
9431 case dw_val_class_loc:
9432 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9433 loc1 && loc2;
9434 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9435 if (!same_loc_p (loc1, loc2, mark))
9436 return 0;
9437 return !loc1 && !loc2;
9439 case dw_val_class_die_ref:
9440 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9442 case dw_val_class_fde_ref:
9443 case dw_val_class_vms_delta:
9444 case dw_val_class_lbl_id:
9445 case dw_val_class_lineptr:
9446 case dw_val_class_macptr:
9447 return 1;
9449 case dw_val_class_file:
9450 return v1->v.val_file == v2->v.val_file;
9452 case dw_val_class_data8:
9453 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9455 default:
9456 return 1;
9460 /* Do the attributes look the same? */
9462 static int
9463 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9465 if (at1->dw_attr != at2->dw_attr)
9466 return 0;
9468 /* We don't care that this was compiled with a different compiler
9469 snapshot; if the output is the same, that's what matters. */
9470 if (at1->dw_attr == DW_AT_producer)
9471 return 1;
9473 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9476 /* Do the dies look the same? */
9478 static int
9479 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9481 dw_die_ref c1, c2;
9482 dw_attr_ref a1;
9483 unsigned ix;
9485 /* To avoid infinite recursion. */
9486 if (die1->die_mark)
9487 return die1->die_mark == die2->die_mark;
9488 die1->die_mark = die2->die_mark = ++(*mark);
9490 if (die1->die_tag != die2->die_tag)
9491 return 0;
9493 if (VEC_length (dw_attr_node, die1->die_attr)
9494 != VEC_length (dw_attr_node, die2->die_attr))
9495 return 0;
9497 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9498 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9499 return 0;
9501 c1 = die1->die_child;
9502 c2 = die2->die_child;
9503 if (! c1)
9505 if (c2)
9506 return 0;
9508 else
9509 for (;;)
9511 if (!same_die_p (c1, c2, mark))
9512 return 0;
9513 c1 = c1->die_sib;
9514 c2 = c2->die_sib;
9515 if (c1 == die1->die_child)
9517 if (c2 == die2->die_child)
9518 break;
9519 else
9520 return 0;
9524 return 1;
9527 /* Do the dies look the same? Wrapper around same_die_p. */
9529 static int
9530 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9532 int mark = 0;
9533 int ret = same_die_p (die1, die2, &mark);
9535 unmark_all_dies (die1);
9536 unmark_all_dies (die2);
9538 return ret;
9541 /* The prefix to attach to symbols on DIEs in the current comdat debug
9542 info section. */
9543 static char *comdat_symbol_id;
9545 /* The index of the current symbol within the current comdat CU. */
9546 static unsigned int comdat_symbol_number;
9548 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9549 children, and set comdat_symbol_id accordingly. */
9551 static void
9552 compute_section_prefix (dw_die_ref unit_die)
9554 const char *die_name = get_AT_string (unit_die, DW_AT_name);
9555 const char *base = die_name ? lbasename (die_name) : "anonymous";
9556 char *name = XALLOCAVEC (char, strlen (base) + 64);
9557 char *p;
9558 int i, mark;
9559 unsigned char checksum[16];
9560 struct md5_ctx ctx;
9562 /* Compute the checksum of the DIE, then append part of it as hex digits to
9563 the name filename of the unit. */
9565 md5_init_ctx (&ctx);
9566 mark = 0;
9567 die_checksum (unit_die, &ctx, &mark);
9568 unmark_all_dies (unit_die);
9569 md5_finish_ctx (&ctx, checksum);
9571 sprintf (name, "%s.", base);
9572 clean_symbol_name (name);
9574 p = name + strlen (name);
9575 for (i = 0; i < 4; i++)
9577 sprintf (p, "%.2x", checksum[i]);
9578 p += 2;
9581 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9582 comdat_symbol_number = 0;
9585 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
9587 static int
9588 is_type_die (dw_die_ref die)
9590 switch (die->die_tag)
9592 case DW_TAG_array_type:
9593 case DW_TAG_class_type:
9594 case DW_TAG_interface_type:
9595 case DW_TAG_enumeration_type:
9596 case DW_TAG_pointer_type:
9597 case DW_TAG_reference_type:
9598 case DW_TAG_rvalue_reference_type:
9599 case DW_TAG_string_type:
9600 case DW_TAG_structure_type:
9601 case DW_TAG_subroutine_type:
9602 case DW_TAG_union_type:
9603 case DW_TAG_ptr_to_member_type:
9604 case DW_TAG_set_type:
9605 case DW_TAG_subrange_type:
9606 case DW_TAG_base_type:
9607 case DW_TAG_const_type:
9608 case DW_TAG_file_type:
9609 case DW_TAG_packed_type:
9610 case DW_TAG_volatile_type:
9611 case DW_TAG_typedef:
9612 return 1;
9613 default:
9614 return 0;
9618 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9619 Basically, we want to choose the bits that are likely to be shared between
9620 compilations (types) and leave out the bits that are specific to individual
9621 compilations (functions). */
9623 static int
9624 is_comdat_die (dw_die_ref c)
9626 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9627 we do for stabs. The advantage is a greater likelihood of sharing between
9628 objects that don't include headers in the same order (and therefore would
9629 put the base types in a different comdat). jason 8/28/00 */
9631 if (c->die_tag == DW_TAG_base_type)
9632 return 0;
9634 if (c->die_tag == DW_TAG_pointer_type
9635 || c->die_tag == DW_TAG_reference_type
9636 || c->die_tag == DW_TAG_rvalue_reference_type
9637 || c->die_tag == DW_TAG_const_type
9638 || c->die_tag == DW_TAG_volatile_type)
9640 dw_die_ref t = get_AT_ref (c, DW_AT_type);
9642 return t ? is_comdat_die (t) : 0;
9645 return is_type_die (c);
9648 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9649 compilation unit. */
9651 static int
9652 is_symbol_die (dw_die_ref c)
9654 return (is_type_die (c)
9655 || is_declaration_die (c)
9656 || c->die_tag == DW_TAG_namespace
9657 || c->die_tag == DW_TAG_module);
9660 /* Returns true iff C is a compile-unit DIE. */
9662 static inline bool
9663 is_cu_die (dw_die_ref c)
9665 return c && c->die_tag == DW_TAG_compile_unit;
9668 static char *
9669 gen_internal_sym (const char *prefix)
9671 char buf[256];
9673 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9674 return xstrdup (buf);
9677 /* Assign symbols to all worthy DIEs under DIE. */
9679 static void
9680 assign_symbol_names (dw_die_ref die)
9682 dw_die_ref c;
9684 if (is_symbol_die (die))
9686 if (comdat_symbol_id)
9688 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9690 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9691 comdat_symbol_id, comdat_symbol_number++);
9692 die->die_id.die_symbol = xstrdup (p);
9694 else
9695 die->die_id.die_symbol = gen_internal_sym ("LDIE");
9698 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9701 struct cu_hash_table_entry
9703 dw_die_ref cu;
9704 unsigned min_comdat_num, max_comdat_num;
9705 struct cu_hash_table_entry *next;
9708 /* Routines to manipulate hash table of CUs. */
9709 static hashval_t
9710 htab_cu_hash (const void *of)
9712 const struct cu_hash_table_entry *const entry =
9713 (const struct cu_hash_table_entry *) of;
9715 return htab_hash_string (entry->cu->die_id.die_symbol);
9718 static int
9719 htab_cu_eq (const void *of1, const void *of2)
9721 const struct cu_hash_table_entry *const entry1 =
9722 (const struct cu_hash_table_entry *) of1;
9723 const struct die_struct *const entry2 = (const struct die_struct *) of2;
9725 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9728 static void
9729 htab_cu_del (void *what)
9731 struct cu_hash_table_entry *next,
9732 *entry = (struct cu_hash_table_entry *) what;
9734 while (entry)
9736 next = entry->next;
9737 free (entry);
9738 entry = next;
9742 /* Check whether we have already seen this CU and set up SYM_NUM
9743 accordingly. */
9744 static int
9745 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9747 struct cu_hash_table_entry dummy;
9748 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9750 dummy.max_comdat_num = 0;
9752 slot = (struct cu_hash_table_entry **)
9753 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9754 INSERT);
9755 entry = *slot;
9757 for (; entry; last = entry, entry = entry->next)
9759 if (same_die_p_wrap (cu, entry->cu))
9760 break;
9763 if (entry)
9765 *sym_num = entry->min_comdat_num;
9766 return 1;
9769 entry = XCNEW (struct cu_hash_table_entry);
9770 entry->cu = cu;
9771 entry->min_comdat_num = *sym_num = last->max_comdat_num;
9772 entry->next = *slot;
9773 *slot = entry;
9775 return 0;
9778 /* Record SYM_NUM to record of CU in HTABLE. */
9779 static void
9780 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9782 struct cu_hash_table_entry **slot, *entry;
9784 slot = (struct cu_hash_table_entry **)
9785 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9786 NO_INSERT);
9787 entry = *slot;
9789 entry->max_comdat_num = sym_num;
9792 /* Traverse the DIE (which is always comp_unit_die), and set up
9793 additional compilation units for each of the include files we see
9794 bracketed by BINCL/EINCL. */
9796 static void
9797 break_out_includes (dw_die_ref die)
9799 dw_die_ref c;
9800 dw_die_ref unit = NULL;
9801 limbo_die_node *node, **pnode;
9802 htab_t cu_hash_table;
9804 c = die->die_child;
9805 if (c) do {
9806 dw_die_ref prev = c;
9807 c = c->die_sib;
9808 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9809 || (unit && is_comdat_die (c)))
9811 dw_die_ref next = c->die_sib;
9813 /* This DIE is for a secondary CU; remove it from the main one. */
9814 remove_child_with_prev (c, prev);
9816 if (c->die_tag == DW_TAG_GNU_BINCL)
9817 unit = push_new_compile_unit (unit, c);
9818 else if (c->die_tag == DW_TAG_GNU_EINCL)
9819 unit = pop_compile_unit (unit);
9820 else
9821 add_child_die (unit, c);
9822 c = next;
9823 if (c == die->die_child)
9824 break;
9826 } while (c != die->die_child);
9828 #if 0
9829 /* We can only use this in debugging, since the frontend doesn't check
9830 to make sure that we leave every include file we enter. */
9831 gcc_assert (!unit);
9832 #endif
9834 assign_symbol_names (die);
9835 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9836 for (node = limbo_die_list, pnode = &limbo_die_list;
9837 node;
9838 node = node->next)
9840 int is_dupl;
9842 compute_section_prefix (node->die);
9843 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9844 &comdat_symbol_number);
9845 assign_symbol_names (node->die);
9846 if (is_dupl)
9847 *pnode = node->next;
9848 else
9850 pnode = &node->next;
9851 record_comdat_symbol_number (node->die, cu_hash_table,
9852 comdat_symbol_number);
9855 htab_delete (cu_hash_table);
9858 /* Return non-zero if this DIE is a declaration. */
9860 static int
9861 is_declaration_die (dw_die_ref die)
9863 dw_attr_ref a;
9864 unsigned ix;
9866 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9867 if (a->dw_attr == DW_AT_declaration)
9868 return 1;
9870 return 0;
9873 /* Return non-zero if this DIE is nested inside a subprogram. */
9875 static int
9876 is_nested_in_subprogram (dw_die_ref die)
9878 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9880 if (decl == NULL)
9881 decl = die;
9882 return local_scope_p (decl);
9885 /* Return non-zero if this is a type DIE that should be moved to a
9886 COMDAT .debug_types section. */
9888 static int
9889 should_move_die_to_comdat (dw_die_ref die)
9891 switch (die->die_tag)
9893 case DW_TAG_class_type:
9894 case DW_TAG_structure_type:
9895 case DW_TAG_enumeration_type:
9896 case DW_TAG_union_type:
9897 /* Don't move declarations, inlined instances, or types nested in a
9898 subprogram. */
9899 if (is_declaration_die (die)
9900 || get_AT (die, DW_AT_abstract_origin)
9901 || is_nested_in_subprogram (die))
9902 return 0;
9903 return 1;
9904 case DW_TAG_array_type:
9905 case DW_TAG_interface_type:
9906 case DW_TAG_pointer_type:
9907 case DW_TAG_reference_type:
9908 case DW_TAG_rvalue_reference_type:
9909 case DW_TAG_string_type:
9910 case DW_TAG_subroutine_type:
9911 case DW_TAG_ptr_to_member_type:
9912 case DW_TAG_set_type:
9913 case DW_TAG_subrange_type:
9914 case DW_TAG_base_type:
9915 case DW_TAG_const_type:
9916 case DW_TAG_file_type:
9917 case DW_TAG_packed_type:
9918 case DW_TAG_volatile_type:
9919 case DW_TAG_typedef:
9920 default:
9921 return 0;
9925 /* Make a clone of DIE. */
9927 static dw_die_ref
9928 clone_die (dw_die_ref die)
9930 dw_die_ref clone;
9931 dw_attr_ref a;
9932 unsigned ix;
9934 clone = ggc_alloc_cleared_die_node ();
9935 clone->die_tag = die->die_tag;
9937 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9938 add_dwarf_attr (clone, a);
9940 return clone;
9943 /* Make a clone of the tree rooted at DIE. */
9945 static dw_die_ref
9946 clone_tree (dw_die_ref die)
9948 dw_die_ref c;
9949 dw_die_ref clone = clone_die (die);
9951 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9953 return clone;
9956 /* Make a clone of DIE as a declaration. */
9958 static dw_die_ref
9959 clone_as_declaration (dw_die_ref die)
9961 dw_die_ref clone;
9962 dw_die_ref decl;
9963 dw_attr_ref a;
9964 unsigned ix;
9966 /* If the DIE is already a declaration, just clone it. */
9967 if (is_declaration_die (die))
9968 return clone_die (die);
9970 /* If the DIE is a specification, just clone its declaration DIE. */
9971 decl = get_AT_ref (die, DW_AT_specification);
9972 if (decl != NULL)
9973 return clone_die (decl);
9975 clone = ggc_alloc_cleared_die_node ();
9976 clone->die_tag = die->die_tag;
9978 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9980 /* We don't want to copy over all attributes.
9981 For example we don't want DW_AT_byte_size because otherwise we will no
9982 longer have a declaration and GDB will treat it as a definition. */
9984 switch (a->dw_attr)
9986 case DW_AT_artificial:
9987 case DW_AT_containing_type:
9988 case DW_AT_external:
9989 case DW_AT_name:
9990 case DW_AT_type:
9991 case DW_AT_virtuality:
9992 case DW_AT_linkage_name:
9993 case DW_AT_MIPS_linkage_name:
9994 add_dwarf_attr (clone, a);
9995 break;
9996 case DW_AT_byte_size:
9997 default:
9998 break;
10002 if (die->die_id.die_type_node)
10003 add_AT_die_ref (clone, DW_AT_signature, die);
10005 add_AT_flag (clone, DW_AT_declaration, 1);
10006 return clone;
10009 /* Copy the declaration context to the new compile unit DIE. This includes
10010 any surrounding namespace or type declarations. If the DIE has an
10011 AT_specification attribute, it also includes attributes and children
10012 attached to the specification. */
10014 static void
10015 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10017 dw_die_ref decl;
10018 dw_die_ref new_decl;
10020 decl = get_AT_ref (die, DW_AT_specification);
10021 if (decl == NULL)
10022 decl = die;
10023 else
10025 unsigned ix;
10026 dw_die_ref c;
10027 dw_attr_ref a;
10029 /* Copy the type node pointer from the new DIE to the original
10030 declaration DIE so we can forward references later. */
10031 decl->die_id.die_type_node = die->die_id.die_type_node;
10033 remove_AT (die, DW_AT_specification);
10035 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10037 if (a->dw_attr != DW_AT_name
10038 && a->dw_attr != DW_AT_declaration
10039 && a->dw_attr != DW_AT_external)
10040 add_dwarf_attr (die, a);
10043 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10046 if (decl->die_parent != NULL
10047 && decl->die_parent->die_tag != DW_TAG_compile_unit
10048 && decl->die_parent->die_tag != DW_TAG_type_unit)
10050 new_decl = copy_ancestor_tree (unit, decl, NULL);
10051 if (new_decl != NULL)
10053 remove_AT (new_decl, DW_AT_signature);
10054 add_AT_specification (die, new_decl);
10059 /* Generate the skeleton ancestor tree for the given NODE, then clone
10060 the DIE and add the clone into the tree. */
10062 static void
10063 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10065 if (node->new_die != NULL)
10066 return;
10068 node->new_die = clone_as_declaration (node->old_die);
10070 if (node->parent != NULL)
10072 generate_skeleton_ancestor_tree (node->parent);
10073 add_child_die (node->parent->new_die, node->new_die);
10077 /* Generate a skeleton tree of DIEs containing any declarations that are
10078 found in the original tree. We traverse the tree looking for declaration
10079 DIEs, and construct the skeleton from the bottom up whenever we find one. */
10081 static void
10082 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10084 skeleton_chain_node node;
10085 dw_die_ref c;
10086 dw_die_ref first;
10087 dw_die_ref prev = NULL;
10088 dw_die_ref next = NULL;
10090 node.parent = parent;
10092 first = c = parent->old_die->die_child;
10093 if (c)
10094 next = c->die_sib;
10095 if (c) do {
10096 if (prev == NULL || prev->die_sib == c)
10097 prev = c;
10098 c = next;
10099 next = (c == first ? NULL : c->die_sib);
10100 node.old_die = c;
10101 node.new_die = NULL;
10102 if (is_declaration_die (c))
10104 /* Clone the existing DIE, move the original to the skeleton
10105 tree (which is in the main CU), and put the clone, with
10106 all the original's children, where the original came from. */
10107 dw_die_ref clone = clone_die (c);
10108 move_all_children (c, clone);
10110 replace_child (c, clone, prev);
10111 generate_skeleton_ancestor_tree (parent);
10112 add_child_die (parent->new_die, c);
10113 node.new_die = c;
10114 c = clone;
10116 generate_skeleton_bottom_up (&node);
10117 } while (next != NULL);
10120 /* Wrapper function for generate_skeleton_bottom_up. */
10122 static dw_die_ref
10123 generate_skeleton (dw_die_ref die)
10125 skeleton_chain_node node;
10127 node.old_die = die;
10128 node.new_die = NULL;
10129 node.parent = NULL;
10131 /* If this type definition is nested inside another type,
10132 always leave at least a declaration in its place. */
10133 if (die->die_parent != NULL && is_type_die (die->die_parent))
10134 node.new_die = clone_as_declaration (die);
10136 generate_skeleton_bottom_up (&node);
10137 return node.new_die;
10140 /* Remove the DIE from its parent, possibly replacing it with a cloned
10141 declaration. The original DIE will be moved to a new compile unit
10142 so that existing references to it follow it to the new location. If
10143 any of the original DIE's descendants is a declaration, we need to
10144 replace the original DIE with a skeleton tree and move the
10145 declarations back into the skeleton tree. */
10147 static dw_die_ref
10148 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10150 dw_die_ref skeleton;
10152 skeleton = generate_skeleton (child);
10153 if (skeleton == NULL)
10154 remove_child_with_prev (child, prev);
10155 else
10157 skeleton->die_id.die_type_node = child->die_id.die_type_node;
10158 replace_child (child, skeleton, prev);
10161 return skeleton;
10164 /* Traverse the DIE and set up additional .debug_types sections for each
10165 type worthy of being placed in a COMDAT section. */
10167 static void
10168 break_out_comdat_types (dw_die_ref die)
10170 dw_die_ref c;
10171 dw_die_ref first;
10172 dw_die_ref prev = NULL;
10173 dw_die_ref next = NULL;
10174 dw_die_ref unit = NULL;
10176 first = c = die->die_child;
10177 if (c)
10178 next = c->die_sib;
10179 if (c) do {
10180 if (prev == NULL || prev->die_sib == c)
10181 prev = c;
10182 c = next;
10183 next = (c == first ? NULL : c->die_sib);
10184 if (should_move_die_to_comdat (c))
10186 dw_die_ref replacement;
10187 comdat_type_node_ref type_node;
10189 /* Create a new type unit DIE as the root for the new tree, and
10190 add it to the list of comdat types. */
10191 unit = new_die (DW_TAG_type_unit, NULL, NULL);
10192 add_AT_unsigned (unit, DW_AT_language,
10193 get_AT_unsigned (comp_unit_die (), DW_AT_language));
10194 type_node = ggc_alloc_cleared_comdat_type_node ();
10195 type_node->root_die = unit;
10196 type_node->next = comdat_type_list;
10197 comdat_type_list = type_node;
10199 /* Generate the type signature. */
10200 generate_type_signature (c, type_node);
10202 /* Copy the declaration context, attributes, and children of the
10203 declaration into the new compile unit DIE. */
10204 copy_declaration_context (unit, c);
10206 /* Remove this DIE from the main CU. */
10207 replacement = remove_child_or_replace_with_skeleton (c, prev);
10209 /* Break out nested types into their own type units. */
10210 break_out_comdat_types (c);
10212 /* Add the DIE to the new compunit. */
10213 add_child_die (unit, c);
10215 if (replacement != NULL)
10216 c = replacement;
10218 else if (c->die_tag == DW_TAG_namespace
10219 || c->die_tag == DW_TAG_class_type
10220 || c->die_tag == DW_TAG_structure_type
10221 || c->die_tag == DW_TAG_union_type)
10223 /* Look for nested types that can be broken out. */
10224 break_out_comdat_types (c);
10226 } while (next != NULL);
10229 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
10231 struct decl_table_entry
10233 dw_die_ref orig;
10234 dw_die_ref copy;
10237 /* Routines to manipulate hash table of copied declarations. */
10239 static hashval_t
10240 htab_decl_hash (const void *of)
10242 const struct decl_table_entry *const entry =
10243 (const struct decl_table_entry *) of;
10245 return htab_hash_pointer (entry->orig);
10248 static int
10249 htab_decl_eq (const void *of1, const void *of2)
10251 const struct decl_table_entry *const entry1 =
10252 (const struct decl_table_entry *) of1;
10253 const struct die_struct *const entry2 = (const struct die_struct *) of2;
10255 return entry1->orig == entry2;
10258 static void
10259 htab_decl_del (void *what)
10261 struct decl_table_entry *entry = (struct decl_table_entry *) what;
10263 free (entry);
10266 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10267 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
10268 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
10269 to check if the ancestor has already been copied into UNIT. */
10271 static dw_die_ref
10272 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10274 dw_die_ref parent = die->die_parent;
10275 dw_die_ref new_parent = unit;
10276 dw_die_ref copy;
10277 void **slot = NULL;
10278 struct decl_table_entry *entry = NULL;
10280 if (decl_table)
10282 /* Check if the entry has already been copied to UNIT. */
10283 slot = htab_find_slot_with_hash (decl_table, die,
10284 htab_hash_pointer (die), INSERT);
10285 if (*slot != HTAB_EMPTY_ENTRY)
10287 entry = (struct decl_table_entry *) *slot;
10288 return entry->copy;
10291 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
10292 entry = XCNEW (struct decl_table_entry);
10293 entry->orig = die;
10294 entry->copy = NULL;
10295 *slot = entry;
10298 if (parent != NULL)
10300 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10301 if (spec != NULL)
10302 parent = spec;
10303 if (parent->die_tag != DW_TAG_compile_unit
10304 && parent->die_tag != DW_TAG_type_unit)
10305 new_parent = copy_ancestor_tree (unit, parent, decl_table);
10308 copy = clone_as_declaration (die);
10309 add_child_die (new_parent, copy);
10311 if (decl_table != NULL)
10313 /* Record the pointer to the copy. */
10314 entry->copy = copy;
10317 return copy;
10320 /* Walk the DIE and its children, looking for references to incomplete
10321 or trivial types that are unmarked (i.e., that are not in the current
10322 type_unit). */
10324 static void
10325 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10327 dw_die_ref c;
10328 dw_attr_ref a;
10329 unsigned ix;
10331 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10333 if (AT_class (a) == dw_val_class_die_ref)
10335 dw_die_ref targ = AT_ref (a);
10336 comdat_type_node_ref type_node = targ->die_id.die_type_node;
10337 void **slot;
10338 struct decl_table_entry *entry;
10340 if (targ->die_mark != 0 || type_node != NULL)
10341 continue;
10343 slot = htab_find_slot_with_hash (decl_table, targ,
10344 htab_hash_pointer (targ), INSERT);
10346 if (*slot != HTAB_EMPTY_ENTRY)
10348 /* TARG has already been copied, so we just need to
10349 modify the reference to point to the copy. */
10350 entry = (struct decl_table_entry *) *slot;
10351 a->dw_attr_val.v.val_die_ref.die = entry->copy;
10353 else
10355 dw_die_ref parent = unit;
10356 dw_die_ref copy = clone_tree (targ);
10358 /* Make sure the cloned tree is marked as part of the
10359 type unit. */
10360 mark_dies (copy);
10362 /* Record in DECL_TABLE that TARG has been copied.
10363 Need to do this now, before the recursive call,
10364 because DECL_TABLE may be expanded and SLOT
10365 would no longer be a valid pointer. */
10366 entry = XCNEW (struct decl_table_entry);
10367 entry->orig = targ;
10368 entry->copy = copy;
10369 *slot = entry;
10371 /* If TARG has surrounding context, copy its ancestor tree
10372 into the new type unit. */
10373 if (targ->die_parent != NULL
10374 && targ->die_parent->die_tag != DW_TAG_compile_unit
10375 && targ->die_parent->die_tag != DW_TAG_type_unit)
10376 parent = copy_ancestor_tree (unit, targ->die_parent,
10377 decl_table);
10379 add_child_die (parent, copy);
10380 a->dw_attr_val.v.val_die_ref.die = copy;
10382 /* Make sure the newly-copied DIE is walked. If it was
10383 installed in a previously-added context, it won't
10384 get visited otherwise. */
10385 if (parent != unit)
10387 /* Find the highest point of the newly-added tree,
10388 mark each node along the way, and walk from there. */
10389 parent->die_mark = 1;
10390 while (parent->die_parent
10391 && parent->die_parent->die_mark == 0)
10393 parent = parent->die_parent;
10394 parent->die_mark = 1;
10396 copy_decls_walk (unit, parent, decl_table);
10402 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10405 /* Copy declarations for "unworthy" types into the new comdat section.
10406 Incomplete types, modified types, and certain other types aren't broken
10407 out into comdat sections of their own, so they don't have a signature,
10408 and we need to copy the declaration into the same section so that we
10409 don't have an external reference. */
10411 static void
10412 copy_decls_for_unworthy_types (dw_die_ref unit)
10414 htab_t decl_table;
10416 mark_dies (unit);
10417 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10418 copy_decls_walk (unit, unit, decl_table);
10419 htab_delete (decl_table);
10420 unmark_dies (unit);
10423 /* Traverse the DIE and add a sibling attribute if it may have the
10424 effect of speeding up access to siblings. To save some space,
10425 avoid generating sibling attributes for DIE's without children. */
10427 static void
10428 add_sibling_attributes (dw_die_ref die)
10430 dw_die_ref c;
10432 if (! die->die_child)
10433 return;
10435 if (die->die_parent && die != die->die_parent->die_child)
10436 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10438 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10441 /* Output all location lists for the DIE and its children. */
10443 static void
10444 output_location_lists (dw_die_ref die)
10446 dw_die_ref c;
10447 dw_attr_ref a;
10448 unsigned ix;
10450 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10451 if (AT_class (a) == dw_val_class_loc_list)
10452 output_loc_list (AT_loc_list (a));
10454 FOR_EACH_CHILD (die, c, output_location_lists (c));
10457 /* The format of each DIE (and its attribute value pairs) is encoded in an
10458 abbreviation table. This routine builds the abbreviation table and assigns
10459 a unique abbreviation id for each abbreviation entry. The children of each
10460 die are visited recursively. */
10462 static void
10463 build_abbrev_table (dw_die_ref die)
10465 unsigned long abbrev_id;
10466 unsigned int n_alloc;
10467 dw_die_ref c;
10468 dw_attr_ref a;
10469 unsigned ix;
10471 /* Scan the DIE references, and mark as external any that refer to
10472 DIEs from other CUs (i.e. those which are not marked). */
10473 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10474 if (AT_class (a) == dw_val_class_die_ref
10475 && AT_ref (a)->die_mark == 0)
10477 gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10478 set_AT_ref_external (a, 1);
10481 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10483 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10484 dw_attr_ref die_a, abbrev_a;
10485 unsigned ix;
10486 bool ok = true;
10488 if (abbrev->die_tag != die->die_tag)
10489 continue;
10490 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10491 continue;
10493 if (VEC_length (dw_attr_node, abbrev->die_attr)
10494 != VEC_length (dw_attr_node, die->die_attr))
10495 continue;
10497 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10499 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10500 if ((abbrev_a->dw_attr != die_a->dw_attr)
10501 || (value_format (abbrev_a) != value_format (die_a)))
10503 ok = false;
10504 break;
10507 if (ok)
10508 break;
10511 if (abbrev_id >= abbrev_die_table_in_use)
10513 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10515 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10516 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10517 n_alloc);
10519 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10520 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10521 abbrev_die_table_allocated = n_alloc;
10524 ++abbrev_die_table_in_use;
10525 abbrev_die_table[abbrev_id] = die;
10528 die->die_abbrev = abbrev_id;
10529 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10532 /* Return the power-of-two number of bytes necessary to represent VALUE. */
10534 static int
10535 constant_size (unsigned HOST_WIDE_INT value)
10537 int log;
10539 if (value == 0)
10540 log = 0;
10541 else
10542 log = floor_log2 (value);
10544 log = log / 8;
10545 log = 1 << (floor_log2 (log) + 1);
10547 return log;
10550 /* Return the size of a DIE as it is represented in the
10551 .debug_info section. */
10553 static unsigned long
10554 size_of_die (dw_die_ref die)
10556 unsigned long size = 0;
10557 dw_attr_ref a;
10558 unsigned ix;
10560 size += size_of_uleb128 (die->die_abbrev);
10561 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10563 switch (AT_class (a))
10565 case dw_val_class_addr:
10566 size += DWARF2_ADDR_SIZE;
10567 break;
10568 case dw_val_class_offset:
10569 size += DWARF_OFFSET_SIZE;
10570 break;
10571 case dw_val_class_loc:
10573 unsigned long lsize = size_of_locs (AT_loc (a));
10575 /* Block length. */
10576 if (dwarf_version >= 4)
10577 size += size_of_uleb128 (lsize);
10578 else
10579 size += constant_size (lsize);
10580 size += lsize;
10582 break;
10583 case dw_val_class_loc_list:
10584 size += DWARF_OFFSET_SIZE;
10585 break;
10586 case dw_val_class_range_list:
10587 size += DWARF_OFFSET_SIZE;
10588 break;
10589 case dw_val_class_const:
10590 size += size_of_sleb128 (AT_int (a));
10591 break;
10592 case dw_val_class_unsigned_const:
10593 size += constant_size (AT_unsigned (a));
10594 break;
10595 case dw_val_class_const_double:
10596 size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10597 if (HOST_BITS_PER_WIDE_INT >= 64)
10598 size++; /* block */
10599 break;
10600 case dw_val_class_vec:
10601 size += constant_size (a->dw_attr_val.v.val_vec.length
10602 * a->dw_attr_val.v.val_vec.elt_size)
10603 + a->dw_attr_val.v.val_vec.length
10604 * a->dw_attr_val.v.val_vec.elt_size; /* block */
10605 break;
10606 case dw_val_class_flag:
10607 if (dwarf_version >= 4)
10608 /* Currently all add_AT_flag calls pass in 1 as last argument,
10609 so DW_FORM_flag_present can be used. If that ever changes,
10610 we'll need to use DW_FORM_flag and have some optimization
10611 in build_abbrev_table that will change those to
10612 DW_FORM_flag_present if it is set to 1 in all DIEs using
10613 the same abbrev entry. */
10614 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10615 else
10616 size += 1;
10617 break;
10618 case dw_val_class_die_ref:
10619 if (AT_ref_external (a))
10621 /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10622 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
10623 is sized by target address length, whereas in DWARF3
10624 it's always sized as an offset. */
10625 if (dwarf_version >= 4)
10626 size += DWARF_TYPE_SIGNATURE_SIZE;
10627 else if (dwarf_version == 2)
10628 size += DWARF2_ADDR_SIZE;
10629 else
10630 size += DWARF_OFFSET_SIZE;
10632 else
10633 size += DWARF_OFFSET_SIZE;
10634 break;
10635 case dw_val_class_fde_ref:
10636 size += DWARF_OFFSET_SIZE;
10637 break;
10638 case dw_val_class_lbl_id:
10639 size += DWARF2_ADDR_SIZE;
10640 break;
10641 case dw_val_class_lineptr:
10642 case dw_val_class_macptr:
10643 size += DWARF_OFFSET_SIZE;
10644 break;
10645 case dw_val_class_str:
10646 if (AT_string_form (a) == DW_FORM_strp)
10647 size += DWARF_OFFSET_SIZE;
10648 else
10649 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10650 break;
10651 case dw_val_class_file:
10652 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10653 break;
10654 case dw_val_class_data8:
10655 size += 8;
10656 break;
10657 case dw_val_class_vms_delta:
10658 size += DWARF_OFFSET_SIZE;
10659 break;
10660 default:
10661 gcc_unreachable ();
10665 return size;
10668 /* Size the debugging information associated with a given DIE. Visits the
10669 DIE's children recursively. Updates the global variable next_die_offset, on
10670 each time through. Uses the current value of next_die_offset to update the
10671 die_offset field in each DIE. */
10673 static void
10674 calc_die_sizes (dw_die_ref die)
10676 dw_die_ref c;
10678 die->die_offset = next_die_offset;
10679 next_die_offset += size_of_die (die);
10681 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10683 if (die->die_child != NULL)
10684 /* Count the null byte used to terminate sibling lists. */
10685 next_die_offset += 1;
10688 /* Set the marks for a die and its children. We do this so
10689 that we know whether or not a reference needs to use FORM_ref_addr; only
10690 DIEs in the same CU will be marked. We used to clear out the offset
10691 and use that as the flag, but ran into ordering problems. */
10693 static void
10694 mark_dies (dw_die_ref die)
10696 dw_die_ref c;
10698 gcc_assert (!die->die_mark);
10700 die->die_mark = 1;
10701 FOR_EACH_CHILD (die, c, mark_dies (c));
10704 /* Clear the marks for a die and its children. */
10706 static void
10707 unmark_dies (dw_die_ref die)
10709 dw_die_ref c;
10711 if (dwarf_version < 4)
10712 gcc_assert (die->die_mark);
10714 die->die_mark = 0;
10715 FOR_EACH_CHILD (die, c, unmark_dies (c));
10718 /* Clear the marks for a die, its children and referred dies. */
10720 static void
10721 unmark_all_dies (dw_die_ref die)
10723 dw_die_ref c;
10724 dw_attr_ref a;
10725 unsigned ix;
10727 if (!die->die_mark)
10728 return;
10729 die->die_mark = 0;
10731 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10733 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10734 if (AT_class (a) == dw_val_class_die_ref)
10735 unmark_all_dies (AT_ref (a));
10738 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10739 generated for the compilation unit. */
10741 static unsigned long
10742 size_of_pubnames (VEC (pubname_entry, gc) * names)
10744 unsigned long size;
10745 unsigned i;
10746 pubname_ref p;
10748 size = DWARF_PUBNAMES_HEADER_SIZE;
10749 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10750 if (names != pubtype_table
10751 || p->die->die_offset != 0
10752 || !flag_eliminate_unused_debug_types)
10753 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10755 size += DWARF_OFFSET_SIZE;
10756 return size;
10759 /* Return the size of the information in the .debug_aranges section. */
10761 static unsigned long
10762 size_of_aranges (void)
10764 unsigned long size;
10766 size = DWARF_ARANGES_HEADER_SIZE;
10768 /* Count the address/length pair for this compilation unit. */
10769 if (text_section_used)
10770 size += 2 * DWARF2_ADDR_SIZE;
10771 if (cold_text_section_used)
10772 size += 2 * DWARF2_ADDR_SIZE;
10773 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10775 /* Count the two zero words used to terminated the address range table. */
10776 size += 2 * DWARF2_ADDR_SIZE;
10777 return size;
10780 /* Select the encoding of an attribute value. */
10782 static enum dwarf_form
10783 value_format (dw_attr_ref a)
10785 switch (a->dw_attr_val.val_class)
10787 case dw_val_class_addr:
10788 /* Only very few attributes allow DW_FORM_addr. */
10789 switch (a->dw_attr)
10791 case DW_AT_low_pc:
10792 case DW_AT_high_pc:
10793 case DW_AT_entry_pc:
10794 case DW_AT_trampoline:
10795 return DW_FORM_addr;
10796 default:
10797 break;
10799 switch (DWARF2_ADDR_SIZE)
10801 case 1:
10802 return DW_FORM_data1;
10803 case 2:
10804 return DW_FORM_data2;
10805 case 4:
10806 return DW_FORM_data4;
10807 case 8:
10808 return DW_FORM_data8;
10809 default:
10810 gcc_unreachable ();
10812 case dw_val_class_range_list:
10813 case dw_val_class_loc_list:
10814 if (dwarf_version >= 4)
10815 return DW_FORM_sec_offset;
10816 /* FALLTHRU */
10817 case dw_val_class_vms_delta:
10818 case dw_val_class_offset:
10819 switch (DWARF_OFFSET_SIZE)
10821 case 4:
10822 return DW_FORM_data4;
10823 case 8:
10824 return DW_FORM_data8;
10825 default:
10826 gcc_unreachable ();
10828 case dw_val_class_loc:
10829 if (dwarf_version >= 4)
10830 return DW_FORM_exprloc;
10831 switch (constant_size (size_of_locs (AT_loc (a))))
10833 case 1:
10834 return DW_FORM_block1;
10835 case 2:
10836 return DW_FORM_block2;
10837 default:
10838 gcc_unreachable ();
10840 case dw_val_class_const:
10841 return DW_FORM_sdata;
10842 case dw_val_class_unsigned_const:
10843 switch (constant_size (AT_unsigned (a)))
10845 case 1:
10846 return DW_FORM_data1;
10847 case 2:
10848 return DW_FORM_data2;
10849 case 4:
10850 return DW_FORM_data4;
10851 case 8:
10852 return DW_FORM_data8;
10853 default:
10854 gcc_unreachable ();
10856 case dw_val_class_const_double:
10857 switch (HOST_BITS_PER_WIDE_INT)
10859 case 8:
10860 return DW_FORM_data2;
10861 case 16:
10862 return DW_FORM_data4;
10863 case 32:
10864 return DW_FORM_data8;
10865 case 64:
10866 default:
10867 return DW_FORM_block1;
10869 case dw_val_class_vec:
10870 switch (constant_size (a->dw_attr_val.v.val_vec.length
10871 * a->dw_attr_val.v.val_vec.elt_size))
10873 case 1:
10874 return DW_FORM_block1;
10875 case 2:
10876 return DW_FORM_block2;
10877 case 4:
10878 return DW_FORM_block4;
10879 default:
10880 gcc_unreachable ();
10882 case dw_val_class_flag:
10883 if (dwarf_version >= 4)
10885 /* Currently all add_AT_flag calls pass in 1 as last argument,
10886 so DW_FORM_flag_present can be used. If that ever changes,
10887 we'll need to use DW_FORM_flag and have some optimization
10888 in build_abbrev_table that will change those to
10889 DW_FORM_flag_present if it is set to 1 in all DIEs using
10890 the same abbrev entry. */
10891 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10892 return DW_FORM_flag_present;
10894 return DW_FORM_flag;
10895 case dw_val_class_die_ref:
10896 if (AT_ref_external (a))
10897 return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10898 else
10899 return DW_FORM_ref;
10900 case dw_val_class_fde_ref:
10901 return DW_FORM_data;
10902 case dw_val_class_lbl_id:
10903 return DW_FORM_addr;
10904 case dw_val_class_lineptr:
10905 case dw_val_class_macptr:
10906 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10907 case dw_val_class_str:
10908 return AT_string_form (a);
10909 case dw_val_class_file:
10910 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10912 case 1:
10913 return DW_FORM_data1;
10914 case 2:
10915 return DW_FORM_data2;
10916 case 4:
10917 return DW_FORM_data4;
10918 default:
10919 gcc_unreachable ();
10922 case dw_val_class_data8:
10923 return DW_FORM_data8;
10925 default:
10926 gcc_unreachable ();
10930 /* Output the encoding of an attribute value. */
10932 static void
10933 output_value_format (dw_attr_ref a)
10935 enum dwarf_form form = value_format (a);
10937 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10940 /* Output the .debug_abbrev section which defines the DIE abbreviation
10941 table. */
10943 static void
10944 output_abbrev_section (void)
10946 unsigned long abbrev_id;
10948 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10950 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10951 unsigned ix;
10952 dw_attr_ref a_attr;
10954 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10955 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10956 dwarf_tag_name (abbrev->die_tag));
10958 if (abbrev->die_child != NULL)
10959 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10960 else
10961 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10963 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10964 ix++)
10966 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10967 dwarf_attr_name (a_attr->dw_attr));
10968 output_value_format (a_attr);
10971 dw2_asm_output_data (1, 0, NULL);
10972 dw2_asm_output_data (1, 0, NULL);
10975 /* Terminate the table. */
10976 dw2_asm_output_data (1, 0, NULL);
10979 /* Output a symbol we can use to refer to this DIE from another CU. */
10981 static inline void
10982 output_die_symbol (dw_die_ref die)
10984 char *sym = die->die_id.die_symbol;
10986 if (sym == 0)
10987 return;
10989 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10990 /* We make these global, not weak; if the target doesn't support
10991 .linkonce, it doesn't support combining the sections, so debugging
10992 will break. */
10993 targetm.asm_out.globalize_label (asm_out_file, sym);
10995 ASM_OUTPUT_LABEL (asm_out_file, sym);
10998 /* Return a new location list, given the begin and end range, and the
10999 expression. */
11001 static inline dw_loc_list_ref
11002 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11003 const char *section)
11005 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11007 retlist->begin = begin;
11008 retlist->end = end;
11009 retlist->expr = expr;
11010 retlist->section = section;
11012 return retlist;
11015 /* Generate a new internal symbol for this location list node, if it
11016 hasn't got one yet. */
11018 static inline void
11019 gen_llsym (dw_loc_list_ref list)
11021 gcc_assert (!list->ll_symbol);
11022 list->ll_symbol = gen_internal_sym ("LLST");
11025 /* Output the location list given to us. */
11027 static void
11028 output_loc_list (dw_loc_list_ref list_head)
11030 dw_loc_list_ref curr = list_head;
11032 if (list_head->emitted)
11033 return;
11034 list_head->emitted = true;
11036 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11038 /* Walk the location list, and output each range + expression. */
11039 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11041 unsigned long size;
11042 /* Don't output an entry that starts and ends at the same address. */
11043 if (strcmp (curr->begin, curr->end) == 0)
11044 continue;
11045 if (!have_multiple_function_sections)
11047 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11048 "Location list begin address (%s)",
11049 list_head->ll_symbol);
11050 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11051 "Location list end address (%s)",
11052 list_head->ll_symbol);
11054 else
11056 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11057 "Location list begin address (%s)",
11058 list_head->ll_symbol);
11059 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11060 "Location list end address (%s)",
11061 list_head->ll_symbol);
11063 size = size_of_locs (curr->expr);
11065 /* Output the block length for this list of location operations. */
11066 gcc_assert (size <= 0xffff);
11067 dw2_asm_output_data (2, size, "%s", "Location expression size");
11069 output_loc_sequence (curr->expr);
11072 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11073 "Location list terminator begin (%s)",
11074 list_head->ll_symbol);
11075 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11076 "Location list terminator end (%s)",
11077 list_head->ll_symbol);
11080 /* Output a type signature. */
11082 static inline void
11083 output_signature (const char *sig, const char *name)
11085 int i;
11087 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11088 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11091 /* Output the DIE and its attributes. Called recursively to generate
11092 the definitions of each child DIE. */
11094 static void
11095 output_die (dw_die_ref die)
11097 dw_attr_ref a;
11098 dw_die_ref c;
11099 unsigned long size;
11100 unsigned ix;
11102 /* If someone in another CU might refer to us, set up a symbol for
11103 them to point to. */
11104 if (dwarf_version < 4 && die->die_id.die_symbol)
11105 output_die_symbol (die);
11107 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11108 (unsigned long)die->die_offset,
11109 dwarf_tag_name (die->die_tag));
11111 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11113 const char *name = dwarf_attr_name (a->dw_attr);
11115 switch (AT_class (a))
11117 case dw_val_class_addr:
11118 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11119 break;
11121 case dw_val_class_offset:
11122 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11123 "%s", name);
11124 break;
11126 case dw_val_class_range_list:
11128 char *p = strchr (ranges_section_label, '\0');
11130 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11131 a->dw_attr_val.v.val_offset);
11132 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11133 debug_ranges_section, "%s", name);
11134 *p = '\0';
11136 break;
11138 case dw_val_class_loc:
11139 size = size_of_locs (AT_loc (a));
11141 /* Output the block length for this list of location operations. */
11142 if (dwarf_version >= 4)
11143 dw2_asm_output_data_uleb128 (size, "%s", name);
11144 else
11145 dw2_asm_output_data (constant_size (size), size, "%s", name);
11147 output_loc_sequence (AT_loc (a));
11148 break;
11150 case dw_val_class_const:
11151 /* ??? It would be slightly more efficient to use a scheme like is
11152 used for unsigned constants below, but gdb 4.x does not sign
11153 extend. Gdb 5.x does sign extend. */
11154 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11155 break;
11157 case dw_val_class_unsigned_const:
11158 dw2_asm_output_data (constant_size (AT_unsigned (a)),
11159 AT_unsigned (a), "%s", name);
11160 break;
11162 case dw_val_class_const_double:
11164 unsigned HOST_WIDE_INT first, second;
11166 if (HOST_BITS_PER_WIDE_INT >= 64)
11167 dw2_asm_output_data (1,
11168 2 * HOST_BITS_PER_WIDE_INT
11169 / HOST_BITS_PER_CHAR,
11170 NULL);
11172 if (WORDS_BIG_ENDIAN)
11174 first = a->dw_attr_val.v.val_double.high;
11175 second = a->dw_attr_val.v.val_double.low;
11177 else
11179 first = a->dw_attr_val.v.val_double.low;
11180 second = a->dw_attr_val.v.val_double.high;
11183 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11184 first, name);
11185 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11186 second, NULL);
11188 break;
11190 case dw_val_class_vec:
11192 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11193 unsigned int len = a->dw_attr_val.v.val_vec.length;
11194 unsigned int i;
11195 unsigned char *p;
11197 dw2_asm_output_data (constant_size (len * elt_size),
11198 len * elt_size, "%s", name);
11199 if (elt_size > sizeof (HOST_WIDE_INT))
11201 elt_size /= 2;
11202 len *= 2;
11204 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11205 i < len;
11206 i++, p += elt_size)
11207 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11208 "fp or vector constant word %u", i);
11209 break;
11212 case dw_val_class_flag:
11213 if (dwarf_version >= 4)
11215 /* Currently all add_AT_flag calls pass in 1 as last argument,
11216 so DW_FORM_flag_present can be used. If that ever changes,
11217 we'll need to use DW_FORM_flag and have some optimization
11218 in build_abbrev_table that will change those to
11219 DW_FORM_flag_present if it is set to 1 in all DIEs using
11220 the same abbrev entry. */
11221 gcc_assert (AT_flag (a) == 1);
11222 if (flag_debug_asm)
11223 fprintf (asm_out_file, "\t\t\t%s %s\n",
11224 ASM_COMMENT_START, name);
11225 break;
11227 dw2_asm_output_data (1, AT_flag (a), "%s", name);
11228 break;
11230 case dw_val_class_loc_list:
11232 char *sym = AT_loc_list (a)->ll_symbol;
11234 gcc_assert (sym);
11235 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11236 "%s", name);
11238 break;
11240 case dw_val_class_die_ref:
11241 if (AT_ref_external (a))
11243 if (dwarf_version >= 4)
11245 comdat_type_node_ref type_node =
11246 AT_ref (a)->die_id.die_type_node;
11248 gcc_assert (type_node);
11249 output_signature (type_node->signature, name);
11251 else
11253 char *sym = AT_ref (a)->die_id.die_symbol;
11254 int size;
11256 gcc_assert (sym);
11257 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11258 length, whereas in DWARF3 it's always sized as an
11259 offset. */
11260 if (dwarf_version == 2)
11261 size = DWARF2_ADDR_SIZE;
11262 else
11263 size = DWARF_OFFSET_SIZE;
11264 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11265 name);
11268 else
11270 gcc_assert (AT_ref (a)->die_offset);
11271 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11272 "%s", name);
11274 break;
11276 case dw_val_class_fde_ref:
11278 char l1[20];
11280 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11281 a->dw_attr_val.v.val_fde_index * 2);
11282 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11283 "%s", name);
11285 break;
11287 case dw_val_class_vms_delta:
11288 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11289 AT_vms_delta2 (a), AT_vms_delta1 (a),
11290 "%s", name);
11291 break;
11293 case dw_val_class_lbl_id:
11294 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11295 break;
11297 case dw_val_class_lineptr:
11298 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11299 debug_line_section, "%s", name);
11300 break;
11302 case dw_val_class_macptr:
11303 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11304 debug_macinfo_section, "%s", name);
11305 break;
11307 case dw_val_class_str:
11308 if (AT_string_form (a) == DW_FORM_strp)
11309 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11310 a->dw_attr_val.v.val_str->label,
11311 debug_str_section,
11312 "%s: \"%s\"", name, AT_string (a));
11313 else
11314 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11315 break;
11317 case dw_val_class_file:
11319 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11321 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11322 a->dw_attr_val.v.val_file->filename);
11323 break;
11326 case dw_val_class_data8:
11328 int i;
11330 for (i = 0; i < 8; i++)
11331 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11332 i == 0 ? "%s" : NULL, name);
11333 break;
11336 default:
11337 gcc_unreachable ();
11341 FOR_EACH_CHILD (die, c, output_die (c));
11343 /* Add null byte to terminate sibling list. */
11344 if (die->die_child != NULL)
11345 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11346 (unsigned long) die->die_offset);
11349 /* Output the compilation unit that appears at the beginning of the
11350 .debug_info section, and precedes the DIE descriptions. */
11352 static void
11353 output_compilation_unit_header (void)
11355 int ver = dwarf_version;
11357 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11358 dw2_asm_output_data (4, 0xffffffff,
11359 "Initial length escape value indicating 64-bit DWARF extension");
11360 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11361 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11362 "Length of Compilation Unit Info");
11363 dw2_asm_output_data (2, ver, "DWARF version number");
11364 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11365 debug_abbrev_section,
11366 "Offset Into Abbrev. Section");
11367 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11370 /* Output the compilation unit DIE and its children. */
11372 static void
11373 output_comp_unit (dw_die_ref die, int output_if_empty)
11375 const char *secname;
11376 char *oldsym, *tmp;
11378 /* Unless we are outputting main CU, we may throw away empty ones. */
11379 if (!output_if_empty && die->die_child == NULL)
11380 return;
11382 /* Even if there are no children of this DIE, we must output the information
11383 about the compilation unit. Otherwise, on an empty translation unit, we
11384 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11385 will then complain when examining the file. First mark all the DIEs in
11386 this CU so we know which get local refs. */
11387 mark_dies (die);
11389 build_abbrev_table (die);
11391 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11392 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11393 calc_die_sizes (die);
11395 oldsym = die->die_id.die_symbol;
11396 if (oldsym)
11398 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11400 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11401 secname = tmp;
11402 die->die_id.die_symbol = NULL;
11403 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11405 else
11407 switch_to_section (debug_info_section);
11408 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11409 info_section_emitted = true;
11412 /* Output debugging information. */
11413 output_compilation_unit_header ();
11414 output_die (die);
11416 /* Leave the marks on the main CU, so we can check them in
11417 output_pubnames. */
11418 if (oldsym)
11420 unmark_dies (die);
11421 die->die_id.die_symbol = oldsym;
11425 /* Output a comdat type unit DIE and its children. */
11427 static void
11428 output_comdat_type_unit (comdat_type_node *node)
11430 const char *secname;
11431 char *tmp;
11432 int i;
11433 #if defined (OBJECT_FORMAT_ELF)
11434 tree comdat_key;
11435 #endif
11437 /* First mark all the DIEs in this CU so we know which get local refs. */
11438 mark_dies (node->root_die);
11440 build_abbrev_table (node->root_die);
11442 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11443 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11444 calc_die_sizes (node->root_die);
11446 #if defined (OBJECT_FORMAT_ELF)
11447 secname = ".debug_types";
11448 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11449 sprintf (tmp, "wt.");
11450 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11451 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11452 comdat_key = get_identifier (tmp);
11453 targetm.asm_out.named_section (secname,
11454 SECTION_DEBUG | SECTION_LINKONCE,
11455 comdat_key);
11456 #else
11457 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11458 sprintf (tmp, ".gnu.linkonce.wt.");
11459 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11460 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11461 secname = tmp;
11462 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11463 #endif
11465 /* Output debugging information. */
11466 output_compilation_unit_header ();
11467 output_signature (node->signature, "Type Signature");
11468 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11469 "Offset to Type DIE");
11470 output_die (node->root_die);
11472 unmark_dies (node->root_die);
11475 /* Return the DWARF2/3 pubname associated with a decl. */
11477 static const char *
11478 dwarf2_name (tree decl, int scope)
11480 if (DECL_NAMELESS (decl))
11481 return NULL;
11482 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11485 /* Add a new entry to .debug_pubnames if appropriate. */
11487 static void
11488 add_pubname_string (const char *str, dw_die_ref die)
11490 if (targetm.want_debug_pub_sections)
11492 pubname_entry e;
11494 e.die = die;
11495 e.name = xstrdup (str);
11496 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11500 static void
11501 add_pubname (tree decl, dw_die_ref die)
11503 if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11505 const char *name = dwarf2_name (decl, 1);
11506 if (name)
11507 add_pubname_string (name, die);
11511 /* Add a new entry to .debug_pubtypes if appropriate. */
11513 static void
11514 add_pubtype (tree decl, dw_die_ref die)
11516 pubname_entry e;
11518 if (!targetm.want_debug_pub_sections)
11519 return;
11521 e.name = NULL;
11522 if ((TREE_PUBLIC (decl)
11523 || is_cu_die (die->die_parent))
11524 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11526 e.die = die;
11527 if (TYPE_P (decl))
11529 if (TYPE_NAME (decl))
11531 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11532 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11533 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11534 && DECL_NAME (TYPE_NAME (decl)))
11535 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11536 else
11537 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11540 else
11542 e.name = dwarf2_name (decl, 1);
11543 if (e.name)
11544 e.name = xstrdup (e.name);
11547 /* If we don't have a name for the type, there's no point in adding
11548 it to the table. */
11549 if (e.name && e.name[0] != '\0')
11550 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11554 /* Output the public names table used to speed up access to externally
11555 visible names; or the public types table used to find type definitions. */
11557 static void
11558 output_pubnames (VEC (pubname_entry, gc) * names)
11560 unsigned i;
11561 unsigned long pubnames_length = size_of_pubnames (names);
11562 pubname_ref pub;
11564 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11565 dw2_asm_output_data (4, 0xffffffff,
11566 "Initial length escape value indicating 64-bit DWARF extension");
11567 if (names == pubname_table)
11568 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11569 "Length of Public Names Info");
11570 else
11571 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11572 "Length of Public Type Names Info");
11573 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
11574 dw2_asm_output_data (2, 2, "DWARF Version");
11575 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11576 debug_info_section,
11577 "Offset of Compilation Unit Info");
11578 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11579 "Compilation Unit Length");
11581 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11583 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11584 if (names == pubname_table)
11585 gcc_assert (pub->die->die_mark);
11587 if (names != pubtype_table
11588 || pub->die->die_offset != 0
11589 || !flag_eliminate_unused_debug_types)
11591 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11592 "DIE offset");
11594 dw2_asm_output_nstring (pub->name, -1, "external name");
11598 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11601 /* Add a new entry to .debug_aranges if appropriate. */
11603 static void
11604 add_arange (tree decl, dw_die_ref die)
11606 if (! DECL_SECTION_NAME (decl))
11607 return;
11609 if (arange_table_in_use == arange_table_allocated)
11611 arange_table_allocated += ARANGE_TABLE_INCREMENT;
11612 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11613 arange_table_allocated);
11614 memset (arange_table + arange_table_in_use, 0,
11615 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11618 arange_table[arange_table_in_use++] = die;
11621 /* Output the information that goes into the .debug_aranges table.
11622 Namely, define the beginning and ending address range of the
11623 text section generated for this compilation unit. */
11625 static void
11626 output_aranges (void)
11628 unsigned i;
11629 unsigned long aranges_length = size_of_aranges ();
11631 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11632 dw2_asm_output_data (4, 0xffffffff,
11633 "Initial length escape value indicating 64-bit DWARF extension");
11634 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11635 "Length of Address Ranges Info");
11636 /* Version number for aranges is still 2, even in DWARF3. */
11637 dw2_asm_output_data (2, 2, "DWARF Version");
11638 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11639 debug_info_section,
11640 "Offset of Compilation Unit Info");
11641 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11642 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11644 /* We need to align to twice the pointer size here. */
11645 if (DWARF_ARANGES_PAD_SIZE)
11647 /* Pad using a 2 byte words so that padding is correct for any
11648 pointer size. */
11649 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11650 2 * DWARF2_ADDR_SIZE);
11651 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11652 dw2_asm_output_data (2, 0, NULL);
11655 /* It is necessary not to output these entries if the sections were
11656 not used; if the sections were not used, the length will be 0 and
11657 the address may end up as 0 if the section is discarded by ld
11658 --gc-sections, leaving an invalid (0, 0) entry that can be
11659 confused with the terminator. */
11660 if (text_section_used)
11662 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11663 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11664 text_section_label, "Length");
11666 if (cold_text_section_used)
11668 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11669 "Address");
11670 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11671 cold_text_section_label, "Length");
11674 for (i = 0; i < arange_table_in_use; i++)
11676 dw_die_ref die = arange_table[i];
11678 /* We shouldn't see aranges for DIEs outside of the main CU. */
11679 gcc_assert (die->die_mark);
11681 if (die->die_tag == DW_TAG_subprogram)
11683 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11684 "Address");
11685 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11686 get_AT_low_pc (die), "Length");
11688 else
11690 /* A static variable; extract the symbol from DW_AT_location.
11691 Note that this code isn't currently hit, as we only emit
11692 aranges for functions (jason 9/23/99). */
11693 dw_attr_ref a = get_AT (die, DW_AT_location);
11694 dw_loc_descr_ref loc;
11696 gcc_assert (a && AT_class (a) == dw_val_class_loc);
11698 loc = AT_loc (a);
11699 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11701 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11702 loc->dw_loc_oprnd1.v.val_addr, "Address");
11703 dw2_asm_output_data (DWARF2_ADDR_SIZE,
11704 get_AT_unsigned (die, DW_AT_byte_size),
11705 "Length");
11709 /* Output the terminator words. */
11710 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11711 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11714 /* Add a new entry to .debug_ranges. Return the offset at which it
11715 was placed. */
11717 static unsigned int
11718 add_ranges_num (int num)
11720 unsigned int in_use = ranges_table_in_use;
11722 if (in_use == ranges_table_allocated)
11724 ranges_table_allocated += RANGES_TABLE_INCREMENT;
11725 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11726 ranges_table_allocated);
11727 memset (ranges_table + ranges_table_in_use, 0,
11728 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11731 ranges_table[in_use].num = num;
11732 ranges_table_in_use = in_use + 1;
11734 return in_use * 2 * DWARF2_ADDR_SIZE;
11737 /* Add a new entry to .debug_ranges corresponding to a block, or a
11738 range terminator if BLOCK is NULL. */
11740 static unsigned int
11741 add_ranges (const_tree block)
11743 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11746 /* Add a new entry to .debug_ranges corresponding to a pair of
11747 labels. */
11749 static void
11750 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11751 bool *added)
11753 unsigned int in_use = ranges_by_label_in_use;
11754 unsigned int offset;
11756 if (in_use == ranges_by_label_allocated)
11758 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11759 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11760 ranges_by_label,
11761 ranges_by_label_allocated);
11762 memset (ranges_by_label + ranges_by_label_in_use, 0,
11763 RANGES_TABLE_INCREMENT
11764 * sizeof (struct dw_ranges_by_label_struct));
11767 ranges_by_label[in_use].begin = begin;
11768 ranges_by_label[in_use].end = end;
11769 ranges_by_label_in_use = in_use + 1;
11771 offset = add_ranges_num (-(int)in_use - 1);
11772 if (!*added)
11774 add_AT_range_list (die, DW_AT_ranges, offset);
11775 *added = true;
11779 static void
11780 output_ranges (void)
11782 unsigned i;
11783 static const char *const start_fmt = "Offset %#x";
11784 const char *fmt = start_fmt;
11786 for (i = 0; i < ranges_table_in_use; i++)
11788 int block_num = ranges_table[i].num;
11790 if (block_num > 0)
11792 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11793 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11795 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11796 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11798 /* If all code is in the text section, then the compilation
11799 unit base address defaults to DW_AT_low_pc, which is the
11800 base of the text section. */
11801 if (!have_multiple_function_sections)
11803 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11804 text_section_label,
11805 fmt, i * 2 * DWARF2_ADDR_SIZE);
11806 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11807 text_section_label, NULL);
11810 /* Otherwise, the compilation unit base address is zero,
11811 which allows us to use absolute addresses, and not worry
11812 about whether the target supports cross-section
11813 arithmetic. */
11814 else
11816 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11817 fmt, i * 2 * DWARF2_ADDR_SIZE);
11818 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11821 fmt = NULL;
11824 /* Negative block_num stands for an index into ranges_by_label. */
11825 else if (block_num < 0)
11827 int lab_idx = - block_num - 1;
11829 if (!have_multiple_function_sections)
11831 gcc_unreachable ();
11832 #if 0
11833 /* If we ever use add_ranges_by_labels () for a single
11834 function section, all we have to do is to take out
11835 the #if 0 above. */
11836 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11837 ranges_by_label[lab_idx].begin,
11838 text_section_label,
11839 fmt, i * 2 * DWARF2_ADDR_SIZE);
11840 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11841 ranges_by_label[lab_idx].end,
11842 text_section_label, NULL);
11843 #endif
11845 else
11847 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11848 ranges_by_label[lab_idx].begin,
11849 fmt, i * 2 * DWARF2_ADDR_SIZE);
11850 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11851 ranges_by_label[lab_idx].end,
11852 NULL);
11855 else
11857 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11858 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11859 fmt = start_fmt;
11864 /* Data structure containing information about input files. */
11865 struct file_info
11867 const char *path; /* Complete file name. */
11868 const char *fname; /* File name part. */
11869 int length; /* Length of entire string. */
11870 struct dwarf_file_data * file_idx; /* Index in input file table. */
11871 int dir_idx; /* Index in directory table. */
11874 /* Data structure containing information about directories with source
11875 files. */
11876 struct dir_info
11878 const char *path; /* Path including directory name. */
11879 int length; /* Path length. */
11880 int prefix; /* Index of directory entry which is a prefix. */
11881 int count; /* Number of files in this directory. */
11882 int dir_idx; /* Index of directory used as base. */
11885 /* Callback function for file_info comparison. We sort by looking at
11886 the directories in the path. */
11888 static int
11889 file_info_cmp (const void *p1, const void *p2)
11891 const struct file_info *const s1 = (const struct file_info *) p1;
11892 const struct file_info *const s2 = (const struct file_info *) p2;
11893 const unsigned char *cp1;
11894 const unsigned char *cp2;
11896 /* Take care of file names without directories. We need to make sure that
11897 we return consistent values to qsort since some will get confused if
11898 we return the same value when identical operands are passed in opposite
11899 orders. So if neither has a directory, return 0 and otherwise return
11900 1 or -1 depending on which one has the directory. */
11901 if ((s1->path == s1->fname || s2->path == s2->fname))
11902 return (s2->path == s2->fname) - (s1->path == s1->fname);
11904 cp1 = (const unsigned char *) s1->path;
11905 cp2 = (const unsigned char *) s2->path;
11907 while (1)
11909 ++cp1;
11910 ++cp2;
11911 /* Reached the end of the first path? If so, handle like above. */
11912 if ((cp1 == (const unsigned char *) s1->fname)
11913 || (cp2 == (const unsigned char *) s2->fname))
11914 return ((cp2 == (const unsigned char *) s2->fname)
11915 - (cp1 == (const unsigned char *) s1->fname));
11917 /* Character of current path component the same? */
11918 else if (*cp1 != *cp2)
11919 return *cp1 - *cp2;
11923 struct file_name_acquire_data
11925 struct file_info *files;
11926 int used_files;
11927 int max_files;
11930 /* Traversal function for the hash table. */
11932 static int
11933 file_name_acquire (void ** slot, void *data)
11935 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11936 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11937 struct file_info *fi;
11938 const char *f;
11940 gcc_assert (fnad->max_files >= d->emitted_number);
11942 if (! d->emitted_number)
11943 return 1;
11945 gcc_assert (fnad->max_files != fnad->used_files);
11947 fi = fnad->files + fnad->used_files++;
11949 /* Skip all leading "./". */
11950 f = d->filename;
11951 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11952 f += 2;
11954 /* Create a new array entry. */
11955 fi->path = f;
11956 fi->length = strlen (f);
11957 fi->file_idx = d;
11959 /* Search for the file name part. */
11960 f = strrchr (f, DIR_SEPARATOR);
11961 #if defined (DIR_SEPARATOR_2)
11963 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11965 if (g != NULL)
11967 if (f == NULL || f < g)
11968 f = g;
11971 #endif
11973 fi->fname = f == NULL ? fi->path : f + 1;
11974 return 1;
11977 /* Output the directory table and the file name table. We try to minimize
11978 the total amount of memory needed. A heuristic is used to avoid large
11979 slowdowns with many input files. */
11981 static void
11982 output_file_names (void)
11984 struct file_name_acquire_data fnad;
11985 int numfiles;
11986 struct file_info *files;
11987 struct dir_info *dirs;
11988 int *saved;
11989 int *savehere;
11990 int *backmap;
11991 int ndirs;
11992 int idx_offset;
11993 int i;
11995 if (!last_emitted_file)
11997 dw2_asm_output_data (1, 0, "End directory table");
11998 dw2_asm_output_data (1, 0, "End file name table");
11999 return;
12002 numfiles = last_emitted_file->emitted_number;
12004 /* Allocate the various arrays we need. */
12005 files = XALLOCAVEC (struct file_info, numfiles);
12006 dirs = XALLOCAVEC (struct dir_info, numfiles);
12008 fnad.files = files;
12009 fnad.used_files = 0;
12010 fnad.max_files = numfiles;
12011 htab_traverse (file_table, file_name_acquire, &fnad);
12012 gcc_assert (fnad.used_files == fnad.max_files);
12014 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12016 /* Find all the different directories used. */
12017 dirs[0].path = files[0].path;
12018 dirs[0].length = files[0].fname - files[0].path;
12019 dirs[0].prefix = -1;
12020 dirs[0].count = 1;
12021 dirs[0].dir_idx = 0;
12022 files[0].dir_idx = 0;
12023 ndirs = 1;
12025 for (i = 1; i < numfiles; i++)
12026 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12027 && memcmp (dirs[ndirs - 1].path, files[i].path,
12028 dirs[ndirs - 1].length) == 0)
12030 /* Same directory as last entry. */
12031 files[i].dir_idx = ndirs - 1;
12032 ++dirs[ndirs - 1].count;
12034 else
12036 int j;
12038 /* This is a new directory. */
12039 dirs[ndirs].path = files[i].path;
12040 dirs[ndirs].length = files[i].fname - files[i].path;
12041 dirs[ndirs].count = 1;
12042 dirs[ndirs].dir_idx = ndirs;
12043 files[i].dir_idx = ndirs;
12045 /* Search for a prefix. */
12046 dirs[ndirs].prefix = -1;
12047 for (j = 0; j < ndirs; j++)
12048 if (dirs[j].length < dirs[ndirs].length
12049 && dirs[j].length > 1
12050 && (dirs[ndirs].prefix == -1
12051 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12052 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12053 dirs[ndirs].prefix = j;
12055 ++ndirs;
12058 /* Now to the actual work. We have to find a subset of the directories which
12059 allow expressing the file name using references to the directory table
12060 with the least amount of characters. We do not do an exhaustive search
12061 where we would have to check out every combination of every single
12062 possible prefix. Instead we use a heuristic which provides nearly optimal
12063 results in most cases and never is much off. */
12064 saved = XALLOCAVEC (int, ndirs);
12065 savehere = XALLOCAVEC (int, ndirs);
12067 memset (saved, '\0', ndirs * sizeof (saved[0]));
12068 for (i = 0; i < ndirs; i++)
12070 int j;
12071 int total;
12073 /* We can always save some space for the current directory. But this
12074 does not mean it will be enough to justify adding the directory. */
12075 savehere[i] = dirs[i].length;
12076 total = (savehere[i] - saved[i]) * dirs[i].count;
12078 for (j = i + 1; j < ndirs; j++)
12080 savehere[j] = 0;
12081 if (saved[j] < dirs[i].length)
12083 /* Determine whether the dirs[i] path is a prefix of the
12084 dirs[j] path. */
12085 int k;
12087 k = dirs[j].prefix;
12088 while (k != -1 && k != (int) i)
12089 k = dirs[k].prefix;
12091 if (k == (int) i)
12093 /* Yes it is. We can possibly save some memory by
12094 writing the filenames in dirs[j] relative to
12095 dirs[i]. */
12096 savehere[j] = dirs[i].length;
12097 total += (savehere[j] - saved[j]) * dirs[j].count;
12102 /* Check whether we can save enough to justify adding the dirs[i]
12103 directory. */
12104 if (total > dirs[i].length + 1)
12106 /* It's worthwhile adding. */
12107 for (j = i; j < ndirs; j++)
12108 if (savehere[j] > 0)
12110 /* Remember how much we saved for this directory so far. */
12111 saved[j] = savehere[j];
12113 /* Remember the prefix directory. */
12114 dirs[j].dir_idx = i;
12119 /* Emit the directory name table. */
12120 idx_offset = dirs[0].length > 0 ? 1 : 0;
12121 for (i = 1 - idx_offset; i < ndirs; i++)
12122 dw2_asm_output_nstring (dirs[i].path,
12123 dirs[i].length
12124 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12125 "Directory Entry: %#x", i + idx_offset);
12127 dw2_asm_output_data (1, 0, "End directory table");
12129 /* We have to emit them in the order of emitted_number since that's
12130 used in the debug info generation. To do this efficiently we
12131 generate a back-mapping of the indices first. */
12132 backmap = XALLOCAVEC (int, numfiles);
12133 for (i = 0; i < numfiles; i++)
12134 backmap[files[i].file_idx->emitted_number - 1] = i;
12136 /* Now write all the file names. */
12137 for (i = 0; i < numfiles; i++)
12139 int file_idx = backmap[i];
12140 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12142 #ifdef VMS_DEBUGGING_INFO
12143 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12145 /* Setting these fields can lead to debugger miscomparisons,
12146 but VMS Debug requires them to be set correctly. */
12148 int ver;
12149 long long cdt;
12150 long siz;
12151 int maxfilelen = strlen (files[file_idx].path)
12152 + dirs[dir_idx].length
12153 + MAX_VMS_VERSION_LEN + 1;
12154 char *filebuf = XALLOCAVEC (char, maxfilelen);
12156 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12157 snprintf (filebuf, maxfilelen, "%s;%d",
12158 files[file_idx].path + dirs[dir_idx].length, ver);
12160 dw2_asm_output_nstring
12161 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12163 /* Include directory index. */
12164 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12166 /* Modification time. */
12167 dw2_asm_output_data_uleb128
12168 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12169 ? cdt : 0,
12170 NULL);
12172 /* File length in bytes. */
12173 dw2_asm_output_data_uleb128
12174 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12175 ? siz : 0,
12176 NULL);
12177 #else
12178 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12179 "File Entry: %#x", (unsigned) i + 1);
12181 /* Include directory index. */
12182 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12184 /* Modification time. */
12185 dw2_asm_output_data_uleb128 (0, NULL);
12187 /* File length in bytes. */
12188 dw2_asm_output_data_uleb128 (0, NULL);
12189 #endif /* VMS_DEBUGGING_INFO */
12192 dw2_asm_output_data (1, 0, "End file name table");
12196 /* Output the source line number correspondence information. This
12197 information goes into the .debug_line section. */
12199 static void
12200 output_line_info (void)
12202 char l1[20], l2[20], p1[20], p2[20];
12203 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12204 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12205 unsigned opc;
12206 unsigned n_op_args;
12207 unsigned long lt_index;
12208 unsigned long current_line;
12209 long line_offset;
12210 long line_delta;
12211 unsigned long current_file;
12212 unsigned long function;
12213 int ver = dwarf_version;
12215 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12216 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12217 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12218 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12220 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12221 dw2_asm_output_data (4, 0xffffffff,
12222 "Initial length escape value indicating 64-bit DWARF extension");
12223 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12224 "Length of Source Line Info");
12225 ASM_OUTPUT_LABEL (asm_out_file, l1);
12227 dw2_asm_output_data (2, ver, "DWARF Version");
12228 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12229 ASM_OUTPUT_LABEL (asm_out_file, p1);
12231 /* Define the architecture-dependent minimum instruction length (in
12232 bytes). In this implementation of DWARF, this field is used for
12233 information purposes only. Since GCC generates assembly language,
12234 we have no a priori knowledge of how many instruction bytes are
12235 generated for each source line, and therefore can use only the
12236 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12237 commands. Accordingly, we fix this as `1', which is "correct
12238 enough" for all architectures, and don't let the target override. */
12239 dw2_asm_output_data (1, 1,
12240 "Minimum Instruction Length");
12242 if (ver >= 4)
12243 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12244 "Maximum Operations Per Instruction");
12245 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12246 "Default is_stmt_start flag");
12247 dw2_asm_output_data (1, DWARF_LINE_BASE,
12248 "Line Base Value (Special Opcodes)");
12249 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12250 "Line Range Value (Special Opcodes)");
12251 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12252 "Special Opcode Base");
12254 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12256 switch (opc)
12258 case DW_LNS_advance_pc:
12259 case DW_LNS_advance_line:
12260 case DW_LNS_set_file:
12261 case DW_LNS_set_column:
12262 case DW_LNS_fixed_advance_pc:
12263 n_op_args = 1;
12264 break;
12265 default:
12266 n_op_args = 0;
12267 break;
12270 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12271 opc, n_op_args);
12274 /* Write out the information about the files we use. */
12275 output_file_names ();
12276 ASM_OUTPUT_LABEL (asm_out_file, p2);
12278 /* We used to set the address register to the first location in the text
12279 section here, but that didn't accomplish anything since we already
12280 have a line note for the opening brace of the first function. */
12282 /* Generate the line number to PC correspondence table, encoded as
12283 a series of state machine operations. */
12284 current_file = 1;
12285 current_line = 1;
12287 if (cfun && in_cold_section_p)
12288 strcpy (prev_line_label, crtl->subsections.cold_section_label);
12289 else
12290 strcpy (prev_line_label, text_section_label);
12291 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12293 dw_line_info_ref line_info = &line_info_table[lt_index];
12295 #if 0
12296 /* Disable this optimization for now; GDB wants to see two line notes
12297 at the beginning of a function so it can find the end of the
12298 prologue. */
12300 /* Don't emit anything for redundant notes. Just updating the
12301 address doesn't accomplish anything, because we already assume
12302 that anything after the last address is this line. */
12303 if (line_info->dw_line_num == current_line
12304 && line_info->dw_file_num == current_file)
12305 continue;
12306 #endif
12308 /* Emit debug info for the address of the current line.
12310 Unfortunately, we have little choice here currently, and must always
12311 use the most general form. GCC does not know the address delta
12312 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
12313 attributes which will give an upper bound on the address range. We
12314 could perhaps use length attributes to determine when it is safe to
12315 use DW_LNS_fixed_advance_pc. */
12317 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12318 if (0)
12320 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
12321 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12322 "DW_LNS_fixed_advance_pc");
12323 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12325 else
12327 /* This can handle any delta. This takes
12328 4+DWARF2_ADDR_SIZE bytes. */
12329 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12330 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12331 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12332 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12335 strcpy (prev_line_label, line_label);
12337 /* Emit debug info for the source file of the current line, if
12338 different from the previous line. */
12339 if (line_info->dw_file_num != current_file)
12341 current_file = line_info->dw_file_num;
12342 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12343 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12346 /* Emit debug info for the current line number, choosing the encoding
12347 that uses the least amount of space. */
12348 if (line_info->dw_line_num != current_line)
12350 line_offset = line_info->dw_line_num - current_line;
12351 line_delta = line_offset - DWARF_LINE_BASE;
12352 current_line = line_info->dw_line_num;
12353 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12354 /* This can handle deltas from -10 to 234, using the current
12355 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
12356 takes 1 byte. */
12357 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12358 "line %lu", current_line);
12359 else
12361 /* This can handle any delta. This takes at least 4 bytes,
12362 depending on the value being encoded. */
12363 dw2_asm_output_data (1, DW_LNS_advance_line,
12364 "advance to line %lu", current_line);
12365 dw2_asm_output_data_sleb128 (line_offset, NULL);
12366 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12369 else
12370 /* We still need to start a new row, so output a copy insn. */
12371 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12374 /* Emit debug info for the address of the end of the function. */
12375 if (0)
12377 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12378 "DW_LNS_fixed_advance_pc");
12379 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12381 else
12383 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12384 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12385 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12386 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12389 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12390 dw2_asm_output_data_uleb128 (1, NULL);
12391 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12393 function = 0;
12394 current_file = 1;
12395 current_line = 1;
12396 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12398 dw_separate_line_info_ref line_info
12399 = &separate_line_info_table[lt_index];
12401 #if 0
12402 /* Don't emit anything for redundant notes. */
12403 if (line_info->dw_line_num == current_line
12404 && line_info->dw_file_num == current_file
12405 && line_info->function == function)
12406 goto cont;
12407 #endif
12409 /* Emit debug info for the address of the current line. If this is
12410 a new function, or the first line of a function, then we need
12411 to handle it differently. */
12412 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12413 lt_index);
12414 if (function != line_info->function)
12416 function = line_info->function;
12418 /* Set the address register to the first line in the function. */
12419 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12420 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12421 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12422 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12424 else
12426 /* ??? See the DW_LNS_advance_pc comment above. */
12427 if (0)
12429 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12430 "DW_LNS_fixed_advance_pc");
12431 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12433 else
12435 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12436 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12437 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12438 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12442 strcpy (prev_line_label, line_label);
12444 /* Emit debug info for the source file of the current line, if
12445 different from the previous line. */
12446 if (line_info->dw_file_num != current_file)
12448 current_file = line_info->dw_file_num;
12449 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12450 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12453 /* Emit debug info for the current line number, choosing the encoding
12454 that uses the least amount of space. */
12455 if (line_info->dw_line_num != current_line)
12457 line_offset = line_info->dw_line_num - current_line;
12458 line_delta = line_offset - DWARF_LINE_BASE;
12459 current_line = line_info->dw_line_num;
12460 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12461 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12462 "line %lu", current_line);
12463 else
12465 dw2_asm_output_data (1, DW_LNS_advance_line,
12466 "advance to line %lu", current_line);
12467 dw2_asm_output_data_sleb128 (line_offset, NULL);
12468 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12471 else
12472 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12474 #if 0
12475 cont:
12476 #endif
12478 lt_index++;
12480 /* If we're done with a function, end its sequence. */
12481 if (lt_index == separate_line_info_table_in_use
12482 || separate_line_info_table[lt_index].function != function)
12484 current_file = 1;
12485 current_line = 1;
12487 /* Emit debug info for the address of the end of the function. */
12488 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12489 if (0)
12491 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12492 "DW_LNS_fixed_advance_pc");
12493 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12495 else
12497 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12498 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12499 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12500 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12503 /* Output the marker for the end of this sequence. */
12504 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12505 dw2_asm_output_data_uleb128 (1, NULL);
12506 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12510 /* Output the marker for the end of the line number info. */
12511 ASM_OUTPUT_LABEL (asm_out_file, l2);
12514 /* Return the size of the .debug_dcall table for the compilation unit. */
12516 static unsigned long
12517 size_of_dcall_table (void)
12519 unsigned long size;
12520 unsigned int i;
12521 dcall_entry *p;
12522 tree last_poc_decl = NULL;
12524 /* Header: version + debug info section pointer + pointer size. */
12525 size = 2 + DWARF_OFFSET_SIZE + 1;
12527 /* Each entry: code label + DIE offset. */
12528 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12530 gcc_assert (p->targ_die != NULL);
12531 /* Insert a "from" entry when the point-of-call DIE offset changes. */
12532 if (p->poc_decl != last_poc_decl)
12534 dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12535 gcc_assert (poc_die);
12536 last_poc_decl = p->poc_decl;
12537 if (poc_die)
12538 size += (DWARF_OFFSET_SIZE
12539 + size_of_uleb128 (poc_die->die_offset));
12541 size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12544 return size;
12547 /* Output the direct call table used to disambiguate PC values when
12548 identical function have been merged. */
12550 static void
12551 output_dcall_table (void)
12553 unsigned i;
12554 unsigned long dcall_length = size_of_dcall_table ();
12555 dcall_entry *p;
12556 char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12557 tree last_poc_decl = NULL;
12559 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12560 dw2_asm_output_data (4, 0xffffffff,
12561 "Initial length escape value indicating 64-bit DWARF extension");
12562 dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12563 "Length of Direct Call Table");
12564 dw2_asm_output_data (2, 4, "Version number");
12565 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12566 debug_info_section,
12567 "Offset of Compilation Unit Info");
12568 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12570 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12572 /* Insert a "from" entry when the point-of-call DIE offset changes. */
12573 if (p->poc_decl != last_poc_decl)
12575 dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12576 last_poc_decl = p->poc_decl;
12577 if (poc_die)
12579 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12580 dw2_asm_output_data_uleb128 (poc_die->die_offset,
12581 "Caller DIE offset");
12584 ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12585 dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12586 dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12587 "Callee DIE offset");
12591 /* Return the size of the .debug_vcall table for the compilation unit. */
12593 static unsigned long
12594 size_of_vcall_table (void)
12596 unsigned long size;
12597 unsigned int i;
12598 vcall_entry *p;
12600 /* Header: version + pointer size. */
12601 size = 2 + 1;
12603 /* Each entry: code label + vtable slot index. */
12604 FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12605 size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12607 return size;
12610 /* Output the virtual call table used to disambiguate PC values when
12611 identical function have been merged. */
12613 static void
12614 output_vcall_table (void)
12616 unsigned i;
12617 unsigned long vcall_length = size_of_vcall_table ();
12618 vcall_entry *p;
12619 char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12621 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12622 dw2_asm_output_data (4, 0xffffffff,
12623 "Initial length escape value indicating 64-bit DWARF extension");
12624 dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12625 "Length of Virtual Call Table");
12626 dw2_asm_output_data (2, 4, "Version number");
12627 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12629 FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12631 ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12632 dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12633 dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12637 /* Given a pointer to a tree node for some base type, return a pointer to
12638 a DIE that describes the given type.
12640 This routine must only be called for GCC type nodes that correspond to
12641 Dwarf base (fundamental) types. */
12643 static dw_die_ref
12644 base_type_die (tree type)
12646 dw_die_ref base_type_result;
12647 enum dwarf_type encoding;
12649 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12650 return 0;
12652 /* If this is a subtype that should not be emitted as a subrange type,
12653 use the base type. See subrange_type_for_debug_p. */
12654 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12655 type = TREE_TYPE (type);
12657 switch (TREE_CODE (type))
12659 case INTEGER_TYPE:
12660 if ((dwarf_version >= 4 || !dwarf_strict)
12661 && TYPE_NAME (type)
12662 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12663 && DECL_IS_BUILTIN (TYPE_NAME (type))
12664 && DECL_NAME (TYPE_NAME (type)))
12666 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12667 if (strcmp (name, "char16_t") == 0
12668 || strcmp (name, "char32_t") == 0)
12670 encoding = DW_ATE_UTF;
12671 break;
12674 if (TYPE_STRING_FLAG (type))
12676 if (TYPE_UNSIGNED (type))
12677 encoding = DW_ATE_unsigned_char;
12678 else
12679 encoding = DW_ATE_signed_char;
12681 else if (TYPE_UNSIGNED (type))
12682 encoding = DW_ATE_unsigned;
12683 else
12684 encoding = DW_ATE_signed;
12685 break;
12687 case REAL_TYPE:
12688 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12690 if (dwarf_version >= 3 || !dwarf_strict)
12691 encoding = DW_ATE_decimal_float;
12692 else
12693 encoding = DW_ATE_lo_user;
12695 else
12696 encoding = DW_ATE_float;
12697 break;
12699 case FIXED_POINT_TYPE:
12700 if (!(dwarf_version >= 3 || !dwarf_strict))
12701 encoding = DW_ATE_lo_user;
12702 else if (TYPE_UNSIGNED (type))
12703 encoding = DW_ATE_unsigned_fixed;
12704 else
12705 encoding = DW_ATE_signed_fixed;
12706 break;
12708 /* Dwarf2 doesn't know anything about complex ints, so use
12709 a user defined type for it. */
12710 case COMPLEX_TYPE:
12711 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12712 encoding = DW_ATE_complex_float;
12713 else
12714 encoding = DW_ATE_lo_user;
12715 break;
12717 case BOOLEAN_TYPE:
12718 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12719 encoding = DW_ATE_boolean;
12720 break;
12722 default:
12723 /* No other TREE_CODEs are Dwarf fundamental types. */
12724 gcc_unreachable ();
12727 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12729 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12730 int_size_in_bytes (type));
12731 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12733 return base_type_result;
12736 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12737 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12739 static inline int
12740 is_base_type (tree type)
12742 switch (TREE_CODE (type))
12744 case ERROR_MARK:
12745 case VOID_TYPE:
12746 case INTEGER_TYPE:
12747 case REAL_TYPE:
12748 case FIXED_POINT_TYPE:
12749 case COMPLEX_TYPE:
12750 case BOOLEAN_TYPE:
12751 return 1;
12753 case ARRAY_TYPE:
12754 case RECORD_TYPE:
12755 case UNION_TYPE:
12756 case QUAL_UNION_TYPE:
12757 case ENUMERAL_TYPE:
12758 case FUNCTION_TYPE:
12759 case METHOD_TYPE:
12760 case POINTER_TYPE:
12761 case REFERENCE_TYPE:
12762 case NULLPTR_TYPE:
12763 case OFFSET_TYPE:
12764 case LANG_TYPE:
12765 case VECTOR_TYPE:
12766 return 0;
12768 default:
12769 gcc_unreachable ();
12772 return 0;
12775 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12776 node, return the size in bits for the type if it is a constant, or else
12777 return the alignment for the type if the type's size is not constant, or
12778 else return BITS_PER_WORD if the type actually turns out to be an
12779 ERROR_MARK node. */
12781 static inline unsigned HOST_WIDE_INT
12782 simple_type_size_in_bits (const_tree type)
12784 if (TREE_CODE (type) == ERROR_MARK)
12785 return BITS_PER_WORD;
12786 else if (TYPE_SIZE (type) == NULL_TREE)
12787 return 0;
12788 else if (host_integerp (TYPE_SIZE (type), 1))
12789 return tree_low_cst (TYPE_SIZE (type), 1);
12790 else
12791 return TYPE_ALIGN (type);
12794 /* Similarly, but return a double_int instead of UHWI. */
12796 static inline double_int
12797 double_int_type_size_in_bits (const_tree type)
12799 if (TREE_CODE (type) == ERROR_MARK)
12800 return uhwi_to_double_int (BITS_PER_WORD);
12801 else if (TYPE_SIZE (type) == NULL_TREE)
12802 return double_int_zero;
12803 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12804 return tree_to_double_int (TYPE_SIZE (type));
12805 else
12806 return uhwi_to_double_int (TYPE_ALIGN (type));
12809 /* Given a pointer to a tree node for a subrange type, return a pointer
12810 to a DIE that describes the given type. */
12812 static dw_die_ref
12813 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12815 dw_die_ref subrange_die;
12816 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12818 if (context_die == NULL)
12819 context_die = comp_unit_die ();
12821 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12823 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12825 /* The size of the subrange type and its base type do not match,
12826 so we need to generate a size attribute for the subrange type. */
12827 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12830 if (low)
12831 add_bound_info (subrange_die, DW_AT_lower_bound, low);
12832 if (high)
12833 add_bound_info (subrange_die, DW_AT_upper_bound, high);
12835 return subrange_die;
12838 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12839 entry that chains various modifiers in front of the given type. */
12841 static dw_die_ref
12842 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12843 dw_die_ref context_die)
12845 enum tree_code code = TREE_CODE (type);
12846 dw_die_ref mod_type_die;
12847 dw_die_ref sub_die = NULL;
12848 tree item_type = NULL;
12849 tree qualified_type;
12850 tree name, low, high;
12852 if (code == ERROR_MARK)
12853 return NULL;
12855 /* See if we already have the appropriately qualified variant of
12856 this type. */
12857 qualified_type
12858 = get_qualified_type (type,
12859 ((is_const_type ? TYPE_QUAL_CONST : 0)
12860 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12862 if (qualified_type == sizetype
12863 && TYPE_NAME (qualified_type)
12864 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12866 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12868 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12869 && TYPE_PRECISION (t)
12870 == TYPE_PRECISION (qualified_type)
12871 && TYPE_UNSIGNED (t)
12872 == TYPE_UNSIGNED (qualified_type));
12873 qualified_type = t;
12876 /* If we do, then we can just use its DIE, if it exists. */
12877 if (qualified_type)
12879 mod_type_die = lookup_type_die (qualified_type);
12880 if (mod_type_die)
12881 return mod_type_die;
12884 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12886 /* Handle C typedef types. */
12887 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12888 && !DECL_ARTIFICIAL (name))
12890 tree dtype = TREE_TYPE (name);
12892 if (qualified_type == dtype)
12894 /* For a named type, use the typedef. */
12895 gen_type_die (qualified_type, context_die);
12896 return lookup_type_die (qualified_type);
12898 else if (is_const_type < TYPE_READONLY (dtype)
12899 || is_volatile_type < TYPE_VOLATILE (dtype)
12900 || (is_const_type <= TYPE_READONLY (dtype)
12901 && is_volatile_type <= TYPE_VOLATILE (dtype)
12902 && DECL_ORIGINAL_TYPE (name) != type))
12903 /* cv-unqualified version of named type. Just use the unnamed
12904 type to which it refers. */
12905 return modified_type_die (DECL_ORIGINAL_TYPE (name),
12906 is_const_type, is_volatile_type,
12907 context_die);
12908 /* Else cv-qualified version of named type; fall through. */
12911 if (is_const_type
12912 /* If both is_const_type and is_volatile_type, prefer the path
12913 which leads to a qualified type. */
12914 && (!is_volatile_type
12915 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12916 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12918 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12919 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12921 else if (is_volatile_type)
12923 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12924 sub_die = modified_type_die (type, is_const_type, 0, context_die);
12926 else if (code == POINTER_TYPE)
12928 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12929 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12930 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12931 item_type = TREE_TYPE (type);
12932 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12933 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12934 TYPE_ADDR_SPACE (item_type));
12936 else if (code == REFERENCE_TYPE)
12938 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12939 mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12940 type);
12941 else
12942 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12943 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12944 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12945 item_type = TREE_TYPE (type);
12946 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12947 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12948 TYPE_ADDR_SPACE (item_type));
12950 else if (code == INTEGER_TYPE
12951 && TREE_TYPE (type) != NULL_TREE
12952 && subrange_type_for_debug_p (type, &low, &high))
12954 mod_type_die = subrange_type_die (type, low, high, context_die);
12955 item_type = TREE_TYPE (type);
12957 else if (is_base_type (type))
12958 mod_type_die = base_type_die (type);
12959 else
12961 gen_type_die (type, context_die);
12963 /* We have to get the type_main_variant here (and pass that to the
12964 `lookup_type_die' routine) because the ..._TYPE node we have
12965 might simply be a *copy* of some original type node (where the
12966 copy was created to help us keep track of typedef names) and
12967 that copy might have a different TYPE_UID from the original
12968 ..._TYPE node. */
12969 if (TREE_CODE (type) != VECTOR_TYPE)
12970 return lookup_type_die (type_main_variant (type));
12971 else
12972 /* Vectors have the debugging information in the type,
12973 not the main variant. */
12974 return lookup_type_die (type);
12977 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12978 don't output a DW_TAG_typedef, since there isn't one in the
12979 user's program; just attach a DW_AT_name to the type.
12980 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12981 if the base type already has the same name. */
12982 if (name
12983 && ((TREE_CODE (name) != TYPE_DECL
12984 && (qualified_type == TYPE_MAIN_VARIANT (type)
12985 || (!is_const_type && !is_volatile_type)))
12986 || (TREE_CODE (name) == TYPE_DECL
12987 && TREE_TYPE (name) == qualified_type
12988 && DECL_NAME (name))))
12990 if (TREE_CODE (name) == TYPE_DECL)
12991 /* Could just call add_name_and_src_coords_attributes here,
12992 but since this is a builtin type it doesn't have any
12993 useful source coordinates anyway. */
12994 name = DECL_NAME (name);
12995 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12997 /* This probably indicates a bug. */
12998 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12999 add_name_attribute (mod_type_die, "__unknown__");
13001 if (qualified_type)
13002 equate_type_number_to_die (qualified_type, mod_type_die);
13004 if (item_type)
13005 /* We must do this after the equate_type_number_to_die call, in case
13006 this is a recursive type. This ensures that the modified_type_die
13007 recursion will terminate even if the type is recursive. Recursive
13008 types are possible in Ada. */
13009 sub_die = modified_type_die (item_type,
13010 TYPE_READONLY (item_type),
13011 TYPE_VOLATILE (item_type),
13012 context_die);
13014 if (sub_die != NULL)
13015 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13017 return mod_type_die;
13020 /* Generate DIEs for the generic parameters of T.
13021 T must be either a generic type or a generic function.
13022 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13024 static void
13025 gen_generic_params_dies (tree t)
13027 tree parms, args;
13028 int parms_num, i;
13029 dw_die_ref die = NULL;
13031 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13032 return;
13034 if (TYPE_P (t))
13035 die = lookup_type_die (t);
13036 else if (DECL_P (t))
13037 die = lookup_decl_die (t);
13039 gcc_assert (die);
13041 parms = lang_hooks.get_innermost_generic_parms (t);
13042 if (!parms)
13043 /* T has no generic parameter. It means T is neither a generic type
13044 or function. End of story. */
13045 return;
13047 parms_num = TREE_VEC_LENGTH (parms);
13048 args = lang_hooks.get_innermost_generic_args (t);
13049 for (i = 0; i < parms_num; i++)
13051 tree parm, arg, arg_pack_elems;
13053 parm = TREE_VEC_ELT (parms, i);
13054 arg = TREE_VEC_ELT (args, i);
13055 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13056 gcc_assert (parm && TREE_VALUE (parm) && arg);
13058 if (parm && TREE_VALUE (parm) && arg)
13060 /* If PARM represents a template parameter pack,
13061 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13062 by DW_TAG_template_*_parameter DIEs for the argument
13063 pack elements of ARG. Note that ARG would then be
13064 an argument pack. */
13065 if (arg_pack_elems)
13066 template_parameter_pack_die (TREE_VALUE (parm),
13067 arg_pack_elems,
13068 die);
13069 else
13070 generic_parameter_die (TREE_VALUE (parm), arg,
13071 true /* Emit DW_AT_name */, die);
13076 /* Create and return a DIE for PARM which should be
13077 the representation of a generic type parameter.
13078 For instance, in the C++ front end, PARM would be a template parameter.
13079 ARG is the argument to PARM.
13080 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13081 name of the PARM.
13082 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13083 as a child node. */
13085 static dw_die_ref
13086 generic_parameter_die (tree parm, tree arg,
13087 bool emit_name_p,
13088 dw_die_ref parent_die)
13090 dw_die_ref tmpl_die = NULL;
13091 const char *name = NULL;
13093 if (!parm || !DECL_NAME (parm) || !arg)
13094 return NULL;
13096 /* We support non-type generic parameters and arguments,
13097 type generic parameters and arguments, as well as
13098 generic generic parameters (a.k.a. template template parameters in C++)
13099 and arguments. */
13100 if (TREE_CODE (parm) == PARM_DECL)
13101 /* PARM is a nontype generic parameter */
13102 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13103 else if (TREE_CODE (parm) == TYPE_DECL)
13104 /* PARM is a type generic parameter. */
13105 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13106 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13107 /* PARM is a generic generic parameter.
13108 Its DIE is a GNU extension. It shall have a
13109 DW_AT_name attribute to represent the name of the template template
13110 parameter, and a DW_AT_GNU_template_name attribute to represent the
13111 name of the template template argument. */
13112 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13113 parent_die, parm);
13114 else
13115 gcc_unreachable ();
13117 if (tmpl_die)
13119 tree tmpl_type;
13121 /* If PARM is a generic parameter pack, it means we are
13122 emitting debug info for a template argument pack element.
13123 In other terms, ARG is a template argument pack element.
13124 In that case, we don't emit any DW_AT_name attribute for
13125 the die. */
13126 if (emit_name_p)
13128 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13129 gcc_assert (name);
13130 add_AT_string (tmpl_die, DW_AT_name, name);
13133 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13135 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13136 TMPL_DIE should have a child DW_AT_type attribute that is set
13137 to the type of the argument to PARM, which is ARG.
13138 If PARM is a type generic parameter, TMPL_DIE should have a
13139 child DW_AT_type that is set to ARG. */
13140 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13141 add_type_attribute (tmpl_die, tmpl_type, 0,
13142 TREE_THIS_VOLATILE (tmpl_type),
13143 parent_die);
13145 else
13147 /* So TMPL_DIE is a DIE representing a
13148 a generic generic template parameter, a.k.a template template
13149 parameter in C++ and arg is a template. */
13151 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13152 to the name of the argument. */
13153 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13154 if (name)
13155 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13158 if (TREE_CODE (parm) == PARM_DECL)
13159 /* So PARM is a non-type generic parameter.
13160 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13161 attribute of TMPL_DIE which value represents the value
13162 of ARG.
13163 We must be careful here:
13164 The value of ARG might reference some function decls.
13165 We might currently be emitting debug info for a generic
13166 type and types are emitted before function decls, we don't
13167 know if the function decls referenced by ARG will actually be
13168 emitted after cgraph computations.
13169 So must defer the generation of the DW_AT_const_value to
13170 after cgraph is ready. */
13171 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13174 return tmpl_die;
13177 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13178 PARM_PACK must be a template parameter pack. The returned DIE
13179 will be child DIE of PARENT_DIE. */
13181 static dw_die_ref
13182 template_parameter_pack_die (tree parm_pack,
13183 tree parm_pack_args,
13184 dw_die_ref parent_die)
13186 dw_die_ref die;
13187 int j;
13189 gcc_assert (parent_die && parm_pack);
13191 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13192 add_name_and_src_coords_attributes (die, parm_pack);
13193 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13194 generic_parameter_die (parm_pack,
13195 TREE_VEC_ELT (parm_pack_args, j),
13196 false /* Don't emit DW_AT_name */,
13197 die);
13198 return die;
13201 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13202 an enumerated type. */
13204 static inline int
13205 type_is_enum (const_tree type)
13207 return TREE_CODE (type) == ENUMERAL_TYPE;
13210 /* Return the DBX register number described by a given RTL node. */
13212 static unsigned int
13213 dbx_reg_number (const_rtx rtl)
13215 unsigned regno = REGNO (rtl);
13217 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13219 #ifdef LEAF_REG_REMAP
13220 if (current_function_uses_only_leaf_regs)
13222 int leaf_reg = LEAF_REG_REMAP (regno);
13223 if (leaf_reg != -1)
13224 regno = (unsigned) leaf_reg;
13226 #endif
13228 return DBX_REGISTER_NUMBER (regno);
13231 /* Optionally add a DW_OP_piece term to a location description expression.
13232 DW_OP_piece is only added if the location description expression already
13233 doesn't end with DW_OP_piece. */
13235 static void
13236 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13238 dw_loc_descr_ref loc;
13240 if (*list_head != NULL)
13242 /* Find the end of the chain. */
13243 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13246 if (loc->dw_loc_opc != DW_OP_piece)
13247 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13251 /* Return a location descriptor that designates a machine register or
13252 zero if there is none. */
13254 static dw_loc_descr_ref
13255 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13257 rtx regs;
13259 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13260 return 0;
13262 /* We only use "frame base" when we're sure we're talking about the
13263 post-prologue local stack frame. We do this by *not* running
13264 register elimination until this point, and recognizing the special
13265 argument pointer and soft frame pointer rtx's.
13266 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13267 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13268 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13270 dw_loc_descr_ref result = NULL;
13272 if (dwarf_version >= 4 || !dwarf_strict)
13274 result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13275 if (result)
13276 add_loc_descr (&result,
13277 new_loc_descr (DW_OP_stack_value, 0, 0));
13279 return result;
13282 regs = targetm.dwarf_register_span (rtl);
13284 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13285 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13286 else
13287 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13290 /* Return a location descriptor that designates a machine register for
13291 a given hard register number. */
13293 static dw_loc_descr_ref
13294 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13296 dw_loc_descr_ref reg_loc_descr;
13298 if (regno <= 31)
13299 reg_loc_descr
13300 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13301 else
13302 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13304 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13305 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13307 return reg_loc_descr;
13310 /* Given an RTL of a register, return a location descriptor that
13311 designates a value that spans more than one register. */
13313 static dw_loc_descr_ref
13314 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13315 enum var_init_status initialized)
13317 int nregs, size, i;
13318 unsigned reg;
13319 dw_loc_descr_ref loc_result = NULL;
13321 reg = REGNO (rtl);
13322 #ifdef LEAF_REG_REMAP
13323 if (current_function_uses_only_leaf_regs)
13325 int leaf_reg = LEAF_REG_REMAP (reg);
13326 if (leaf_reg != -1)
13327 reg = (unsigned) leaf_reg;
13329 #endif
13330 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13331 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13333 /* Simple, contiguous registers. */
13334 if (regs == NULL_RTX)
13336 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13338 loc_result = NULL;
13339 while (nregs--)
13341 dw_loc_descr_ref t;
13343 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13344 VAR_INIT_STATUS_INITIALIZED);
13345 add_loc_descr (&loc_result, t);
13346 add_loc_descr_op_piece (&loc_result, size);
13347 ++reg;
13349 return loc_result;
13352 /* Now onto stupid register sets in non contiguous locations. */
13354 gcc_assert (GET_CODE (regs) == PARALLEL);
13356 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13357 loc_result = NULL;
13359 for (i = 0; i < XVECLEN (regs, 0); ++i)
13361 dw_loc_descr_ref t;
13363 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13364 VAR_INIT_STATUS_INITIALIZED);
13365 add_loc_descr (&loc_result, t);
13366 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13367 add_loc_descr_op_piece (&loc_result, size);
13370 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13371 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13372 return loc_result;
13375 /* Return a location descriptor that designates a constant. */
13377 static dw_loc_descr_ref
13378 int_loc_descriptor (HOST_WIDE_INT i)
13380 enum dwarf_location_atom op;
13382 /* Pick the smallest representation of a constant, rather than just
13383 defaulting to the LEB encoding. */
13384 if (i >= 0)
13386 if (i <= 31)
13387 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13388 else if (i <= 0xff)
13389 op = DW_OP_const1u;
13390 else if (i <= 0xffff)
13391 op = DW_OP_const2u;
13392 else if (HOST_BITS_PER_WIDE_INT == 32
13393 || i <= 0xffffffff)
13394 op = DW_OP_const4u;
13395 else
13396 op = DW_OP_constu;
13398 else
13400 if (i >= -0x80)
13401 op = DW_OP_const1s;
13402 else if (i >= -0x8000)
13403 op = DW_OP_const2s;
13404 else if (HOST_BITS_PER_WIDE_INT == 32
13405 || i >= -0x80000000)
13406 op = DW_OP_const4s;
13407 else
13408 op = DW_OP_consts;
13411 return new_loc_descr (op, i, 0);
13414 /* Return loc description representing "address" of integer value.
13415 This can appear only as toplevel expression. */
13417 static dw_loc_descr_ref
13418 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13420 int litsize;
13421 dw_loc_descr_ref loc_result = NULL;
13423 if (!(dwarf_version >= 4 || !dwarf_strict))
13424 return NULL;
13426 if (i >= 0)
13428 if (i <= 31)
13429 litsize = 1;
13430 else if (i <= 0xff)
13431 litsize = 2;
13432 else if (i <= 0xffff)
13433 litsize = 3;
13434 else if (HOST_BITS_PER_WIDE_INT == 32
13435 || i <= 0xffffffff)
13436 litsize = 5;
13437 else
13438 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13440 else
13442 if (i >= -0x80)
13443 litsize = 2;
13444 else if (i >= -0x8000)
13445 litsize = 3;
13446 else if (HOST_BITS_PER_WIDE_INT == 32
13447 || i >= -0x80000000)
13448 litsize = 5;
13449 else
13450 litsize = 1 + size_of_sleb128 (i);
13452 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13453 is more compact. For DW_OP_stack_value we need:
13454 litsize + 1 (DW_OP_stack_value)
13455 and for DW_OP_implicit_value:
13456 1 (DW_OP_implicit_value) + 1 (length) + size. */
13457 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13459 loc_result = int_loc_descriptor (i);
13460 add_loc_descr (&loc_result,
13461 new_loc_descr (DW_OP_stack_value, 0, 0));
13462 return loc_result;
13465 loc_result = new_loc_descr (DW_OP_implicit_value,
13466 size, 0);
13467 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13468 loc_result->dw_loc_oprnd2.v.val_int = i;
13469 return loc_result;
13472 /* Return a location descriptor that designates a base+offset location. */
13474 static dw_loc_descr_ref
13475 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13476 enum var_init_status initialized)
13478 unsigned int regno;
13479 dw_loc_descr_ref result;
13480 dw_fde_ref fde = current_fde ();
13482 /* We only use "frame base" when we're sure we're talking about the
13483 post-prologue local stack frame. We do this by *not* running
13484 register elimination until this point, and recognizing the special
13485 argument pointer and soft frame pointer rtx's. */
13486 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13488 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13490 if (elim != reg)
13492 if (GET_CODE (elim) == PLUS)
13494 offset += INTVAL (XEXP (elim, 1));
13495 elim = XEXP (elim, 0);
13497 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13498 && (elim == hard_frame_pointer_rtx
13499 || elim == stack_pointer_rtx))
13500 || elim == (frame_pointer_needed
13501 ? hard_frame_pointer_rtx
13502 : stack_pointer_rtx));
13504 /* If drap register is used to align stack, use frame
13505 pointer + offset to access stack variables. If stack
13506 is aligned without drap, use stack pointer + offset to
13507 access stack variables. */
13508 if (crtl->stack_realign_tried
13509 && reg == frame_pointer_rtx)
13511 int base_reg
13512 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13513 ? HARD_FRAME_POINTER_REGNUM
13514 : STACK_POINTER_REGNUM);
13515 return new_reg_loc_descr (base_reg, offset);
13518 offset += frame_pointer_fb_offset;
13519 return new_loc_descr (DW_OP_fbreg, offset, 0);
13522 else if (!optimize
13523 && fde
13524 && (fde->drap_reg == REGNO (reg)
13525 || fde->vdrap_reg == REGNO (reg)))
13527 /* Use cfa+offset to represent the location of arguments passed
13528 on the stack when drap is used to align stack.
13529 Only do this when not optimizing, for optimized code var-tracking
13530 is supposed to track where the arguments live and the register
13531 used as vdrap or drap in some spot might be used for something
13532 else in other part of the routine. */
13533 return new_loc_descr (DW_OP_fbreg, offset, 0);
13536 regno = dbx_reg_number (reg);
13537 if (regno <= 31)
13538 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13539 offset, 0);
13540 else
13541 result = new_loc_descr (DW_OP_bregx, regno, offset);
13543 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13544 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13546 return result;
13549 /* Return true if this RTL expression describes a base+offset calculation. */
13551 static inline int
13552 is_based_loc (const_rtx rtl)
13554 return (GET_CODE (rtl) == PLUS
13555 && ((REG_P (XEXP (rtl, 0))
13556 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13557 && CONST_INT_P (XEXP (rtl, 1)))));
13560 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13561 failed. */
13563 static dw_loc_descr_ref
13564 tls_mem_loc_descriptor (rtx mem)
13566 tree base;
13567 dw_loc_descr_ref loc_result;
13569 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13570 return NULL;
13572 base = get_base_address (MEM_EXPR (mem));
13573 if (base == NULL
13574 || TREE_CODE (base) != VAR_DECL
13575 || !DECL_THREAD_LOCAL_P (base))
13576 return NULL;
13578 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13579 if (loc_result == NULL)
13580 return NULL;
13582 if (INTVAL (MEM_OFFSET (mem)))
13583 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13585 return loc_result;
13588 /* Output debug info about reason why we failed to expand expression as dwarf
13589 expression. */
13591 static void
13592 expansion_failed (tree expr, rtx rtl, char const *reason)
13594 if (dump_file && (dump_flags & TDF_DETAILS))
13596 fprintf (dump_file, "Failed to expand as dwarf: ");
13597 if (expr)
13598 print_generic_expr (dump_file, expr, dump_flags);
13599 if (rtl)
13601 fprintf (dump_file, "\n");
13602 print_rtl (dump_file, rtl);
13604 fprintf (dump_file, "\nReason: %s\n", reason);
13608 /* Helper function for const_ok_for_output, called either directly
13609 or via for_each_rtx. */
13611 static int
13612 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13614 rtx rtl = *rtlp;
13616 if (GET_CODE (rtl) == UNSPEC)
13618 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13619 we can't express it in the debug info. */
13620 #ifdef ENABLE_CHECKING
13621 /* Don't complain about TLS UNSPECs, those are just too hard to
13622 delegitimize. */
13623 if (XVECLEN (rtl, 0) != 1
13624 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13625 || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13626 || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13627 || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13628 inform (current_function_decl
13629 ? DECL_SOURCE_LOCATION (current_function_decl)
13630 : UNKNOWN_LOCATION,
13631 "non-delegitimized UNSPEC %d found in variable location",
13632 XINT (rtl, 1));
13633 #endif
13634 expansion_failed (NULL_TREE, rtl,
13635 "UNSPEC hasn't been delegitimized.\n");
13636 return 1;
13639 if (GET_CODE (rtl) != SYMBOL_REF)
13640 return 0;
13642 if (CONSTANT_POOL_ADDRESS_P (rtl))
13644 bool marked;
13645 get_pool_constant_mark (rtl, &marked);
13646 /* If all references to this pool constant were optimized away,
13647 it was not output and thus we can't represent it. */
13648 if (!marked)
13650 expansion_failed (NULL_TREE, rtl,
13651 "Constant was removed from constant pool.\n");
13652 return 1;
13656 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13657 return 1;
13659 /* Avoid references to external symbols in debug info, on several targets
13660 the linker might even refuse to link when linking a shared library,
13661 and in many other cases the relocations for .debug_info/.debug_loc are
13662 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13663 to be defined within the same shared library or executable are fine. */
13664 if (SYMBOL_REF_EXTERNAL_P (rtl))
13666 tree decl = SYMBOL_REF_DECL (rtl);
13668 if (decl == NULL || !targetm.binds_local_p (decl))
13670 expansion_failed (NULL_TREE, rtl,
13671 "Symbol not defined in current TU.\n");
13672 return 1;
13676 return 0;
13679 /* Return true if constant RTL can be emitted in DW_OP_addr or
13680 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13681 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13683 static bool
13684 const_ok_for_output (rtx rtl)
13686 if (GET_CODE (rtl) == SYMBOL_REF)
13687 return const_ok_for_output_1 (&rtl, NULL) == 0;
13689 if (GET_CODE (rtl) == CONST)
13690 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13692 return true;
13695 /* The following routine converts the RTL for a variable or parameter
13696 (resident in memory) into an equivalent Dwarf representation of a
13697 mechanism for getting the address of that same variable onto the top of a
13698 hypothetical "address evaluation" stack.
13700 When creating memory location descriptors, we are effectively transforming
13701 the RTL for a memory-resident object into its Dwarf postfix expression
13702 equivalent. This routine recursively descends an RTL tree, turning
13703 it into Dwarf postfix code as it goes.
13705 MODE is the mode of the memory reference, needed to handle some
13706 autoincrement addressing modes.
13708 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13709 location list for RTL.
13711 Return 0 if we can't represent the location. */
13713 static dw_loc_descr_ref
13714 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13715 enum var_init_status initialized)
13717 dw_loc_descr_ref mem_loc_result = NULL;
13718 enum dwarf_location_atom op;
13719 dw_loc_descr_ref op0, op1;
13721 /* Note that for a dynamically sized array, the location we will generate a
13722 description of here will be the lowest numbered location which is
13723 actually within the array. That's *not* necessarily the same as the
13724 zeroth element of the array. */
13726 rtl = targetm.delegitimize_address (rtl);
13728 switch (GET_CODE (rtl))
13730 case POST_INC:
13731 case POST_DEC:
13732 case POST_MODIFY:
13733 return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13735 case SUBREG:
13736 /* The case of a subreg may arise when we have a local (register)
13737 variable or a formal (register) parameter which doesn't quite fill
13738 up an entire register. For now, just assume that it is
13739 legitimate to make the Dwarf info refer to the whole register which
13740 contains the given subreg. */
13741 if (!subreg_lowpart_p (rtl))
13742 break;
13743 rtl = SUBREG_REG (rtl);
13744 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13745 break;
13746 if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13747 break;
13748 mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13749 break;
13751 case REG:
13752 /* Whenever a register number forms a part of the description of the
13753 method for calculating the (dynamic) address of a memory resident
13754 object, DWARF rules require the register number be referred to as
13755 a "base register". This distinction is not based in any way upon
13756 what category of register the hardware believes the given register
13757 belongs to. This is strictly DWARF terminology we're dealing with
13758 here. Note that in cases where the location of a memory-resident
13759 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13760 OP_CONST (0)) the actual DWARF location descriptor that we generate
13761 may just be OP_BASEREG (basereg). This may look deceptively like
13762 the object in question was allocated to a register (rather than in
13763 memory) so DWARF consumers need to be aware of the subtle
13764 distinction between OP_REG and OP_BASEREG. */
13765 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13766 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13767 else if (stack_realign_drap
13768 && crtl->drap_reg
13769 && crtl->args.internal_arg_pointer == rtl
13770 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13772 /* If RTL is internal_arg_pointer, which has been optimized
13773 out, use DRAP instead. */
13774 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13775 VAR_INIT_STATUS_INITIALIZED);
13777 break;
13779 case SIGN_EXTEND:
13780 case ZERO_EXTEND:
13781 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13782 VAR_INIT_STATUS_INITIALIZED);
13783 if (op0 == 0)
13784 break;
13785 else
13787 int shift = DWARF2_ADDR_SIZE
13788 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13789 shift *= BITS_PER_UNIT;
13790 if (GET_CODE (rtl) == SIGN_EXTEND)
13791 op = DW_OP_shra;
13792 else
13793 op = DW_OP_shr;
13794 mem_loc_result = op0;
13795 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13796 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13797 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13798 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13800 break;
13802 case MEM:
13803 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13804 VAR_INIT_STATUS_INITIALIZED);
13805 if (mem_loc_result == NULL)
13806 mem_loc_result = tls_mem_loc_descriptor (rtl);
13807 if (mem_loc_result != 0)
13809 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13811 expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13812 return 0;
13814 else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13815 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13816 else
13817 add_loc_descr (&mem_loc_result,
13818 new_loc_descr (DW_OP_deref_size,
13819 GET_MODE_SIZE (GET_MODE (rtl)), 0));
13821 else
13823 rtx new_rtl = avoid_constant_pool_reference (rtl);
13824 if (new_rtl != rtl)
13825 return mem_loc_descriptor (new_rtl, mode, initialized);
13827 break;
13829 case LO_SUM:
13830 rtl = XEXP (rtl, 1);
13832 /* ... fall through ... */
13834 case LABEL_REF:
13835 /* Some ports can transform a symbol ref into a label ref, because
13836 the symbol ref is too far away and has to be dumped into a constant
13837 pool. */
13838 case CONST:
13839 case SYMBOL_REF:
13840 if (GET_CODE (rtl) == SYMBOL_REF
13841 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13843 dw_loc_descr_ref temp;
13845 /* If this is not defined, we have no way to emit the data. */
13846 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13847 break;
13849 /* We used to emit DW_OP_addr here, but that's wrong, since
13850 DW_OP_addr should be relocated by the debug info consumer,
13851 while DW_OP_GNU_push_tls_address operand should not. */
13852 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13853 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13854 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13855 temp->dw_loc_oprnd1.v.val_addr = rtl;
13856 temp->dtprel = true;
13858 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13859 add_loc_descr (&mem_loc_result, temp);
13861 break;
13864 if (!const_ok_for_output (rtl))
13865 break;
13867 symref:
13868 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13869 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13870 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13871 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13872 break;
13874 case CONCAT:
13875 case CONCATN:
13876 case VAR_LOCATION:
13877 case DEBUG_IMPLICIT_PTR:
13878 expansion_failed (NULL_TREE, rtl,
13879 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13880 return 0;
13882 case PRE_MODIFY:
13883 /* Extract the PLUS expression nested inside and fall into
13884 PLUS code below. */
13885 rtl = XEXP (rtl, 1);
13886 goto plus;
13888 case PRE_INC:
13889 case PRE_DEC:
13890 /* Turn these into a PLUS expression and fall into the PLUS code
13891 below. */
13892 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13893 GEN_INT (GET_CODE (rtl) == PRE_INC
13894 ? GET_MODE_UNIT_SIZE (mode)
13895 : -GET_MODE_UNIT_SIZE (mode)));
13897 /* ... fall through ... */
13899 case PLUS:
13900 plus:
13901 if (is_based_loc (rtl))
13902 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13903 INTVAL (XEXP (rtl, 1)),
13904 VAR_INIT_STATUS_INITIALIZED);
13905 else
13907 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13908 VAR_INIT_STATUS_INITIALIZED);
13909 if (mem_loc_result == 0)
13910 break;
13912 if (CONST_INT_P (XEXP (rtl, 1)))
13913 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13914 else
13916 dw_loc_descr_ref mem_loc_result2
13917 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13918 VAR_INIT_STATUS_INITIALIZED);
13919 if (mem_loc_result2 == 0)
13920 break;
13921 add_loc_descr (&mem_loc_result, mem_loc_result2);
13922 add_loc_descr (&mem_loc_result,
13923 new_loc_descr (DW_OP_plus, 0, 0));
13926 break;
13928 /* If a pseudo-reg is optimized away, it is possible for it to
13929 be replaced with a MEM containing a multiply or shift. */
13930 case MINUS:
13931 op = DW_OP_minus;
13932 goto do_binop;
13934 case MULT:
13935 op = DW_OP_mul;
13936 goto do_binop;
13938 case DIV:
13939 op = DW_OP_div;
13940 goto do_binop;
13942 case UMOD:
13943 op = DW_OP_mod;
13944 goto do_binop;
13946 case ASHIFT:
13947 op = DW_OP_shl;
13948 goto do_binop;
13950 case ASHIFTRT:
13951 op = DW_OP_shra;
13952 goto do_binop;
13954 case LSHIFTRT:
13955 op = DW_OP_shr;
13956 goto do_binop;
13958 case AND:
13959 op = DW_OP_and;
13960 goto do_binop;
13962 case IOR:
13963 op = DW_OP_or;
13964 goto do_binop;
13966 case XOR:
13967 op = DW_OP_xor;
13968 goto do_binop;
13970 do_binop:
13971 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13972 VAR_INIT_STATUS_INITIALIZED);
13973 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13974 VAR_INIT_STATUS_INITIALIZED);
13976 if (op0 == 0 || op1 == 0)
13977 break;
13979 mem_loc_result = op0;
13980 add_loc_descr (&mem_loc_result, op1);
13981 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13982 break;
13984 case MOD:
13985 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13986 VAR_INIT_STATUS_INITIALIZED);
13987 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13988 VAR_INIT_STATUS_INITIALIZED);
13990 if (op0 == 0 || op1 == 0)
13991 break;
13993 mem_loc_result = op0;
13994 add_loc_descr (&mem_loc_result, op1);
13995 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13996 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13997 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13998 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13999 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14000 break;
14002 case NOT:
14003 op = DW_OP_not;
14004 goto do_unop;
14006 case ABS:
14007 op = DW_OP_abs;
14008 goto do_unop;
14010 case NEG:
14011 op = DW_OP_neg;
14012 goto do_unop;
14014 do_unop:
14015 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14016 VAR_INIT_STATUS_INITIALIZED);
14018 if (op0 == 0)
14019 break;
14021 mem_loc_result = op0;
14022 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14023 break;
14025 case CONST_INT:
14026 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14027 break;
14029 case EQ:
14030 op = DW_OP_eq;
14031 goto do_scompare;
14033 case GE:
14034 op = DW_OP_ge;
14035 goto do_scompare;
14037 case GT:
14038 op = DW_OP_gt;
14039 goto do_scompare;
14041 case LE:
14042 op = DW_OP_le;
14043 goto do_scompare;
14045 case LT:
14046 op = DW_OP_lt;
14047 goto do_scompare;
14049 case NE:
14050 op = DW_OP_ne;
14051 goto do_scompare;
14053 do_scompare:
14054 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14055 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14056 break;
14057 else
14059 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14061 if (op_mode == VOIDmode)
14062 op_mode = GET_MODE (XEXP (rtl, 1));
14063 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14064 break;
14066 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14067 VAR_INIT_STATUS_INITIALIZED);
14068 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14069 VAR_INIT_STATUS_INITIALIZED);
14071 if (op0 == 0 || op1 == 0)
14072 break;
14074 if (op_mode != VOIDmode
14075 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14077 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14078 shift *= BITS_PER_UNIT;
14079 /* For eq/ne, if the operands are known to be zero-extended,
14080 there is no need to do the fancy shifting up. */
14081 if (op == DW_OP_eq || op == DW_OP_ne)
14083 dw_loc_descr_ref last0, last1;
14084 for (last0 = op0;
14085 last0->dw_loc_next != NULL;
14086 last0 = last0->dw_loc_next)
14088 for (last1 = op1;
14089 last1->dw_loc_next != NULL;
14090 last1 = last1->dw_loc_next)
14092 /* deref_size zero extends, and for constants we can check
14093 whether they are zero extended or not. */
14094 if (((last0->dw_loc_opc == DW_OP_deref_size
14095 && last0->dw_loc_oprnd1.v.val_int
14096 <= GET_MODE_SIZE (op_mode))
14097 || (CONST_INT_P (XEXP (rtl, 0))
14098 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14099 == (INTVAL (XEXP (rtl, 0))
14100 & GET_MODE_MASK (op_mode))))
14101 && ((last1->dw_loc_opc == DW_OP_deref_size
14102 && last1->dw_loc_oprnd1.v.val_int
14103 <= GET_MODE_SIZE (op_mode))
14104 || (CONST_INT_P (XEXP (rtl, 1))
14105 && (unsigned HOST_WIDE_INT)
14106 INTVAL (XEXP (rtl, 1))
14107 == (INTVAL (XEXP (rtl, 1))
14108 & GET_MODE_MASK (op_mode)))))
14109 goto do_compare;
14111 add_loc_descr (&op0, int_loc_descriptor (shift));
14112 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14113 if (CONST_INT_P (XEXP (rtl, 1)))
14114 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14115 else
14117 add_loc_descr (&op1, int_loc_descriptor (shift));
14118 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14123 do_compare:
14124 mem_loc_result = op0;
14125 add_loc_descr (&mem_loc_result, op1);
14126 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14127 if (STORE_FLAG_VALUE != 1)
14129 add_loc_descr (&mem_loc_result,
14130 int_loc_descriptor (STORE_FLAG_VALUE));
14131 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14133 break;
14135 case GEU:
14136 op = DW_OP_ge;
14137 goto do_ucompare;
14139 case GTU:
14140 op = DW_OP_gt;
14141 goto do_ucompare;
14143 case LEU:
14144 op = DW_OP_le;
14145 goto do_ucompare;
14147 case LTU:
14148 op = DW_OP_lt;
14149 goto do_ucompare;
14151 do_ucompare:
14152 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14153 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14154 break;
14155 else
14157 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14159 if (op_mode == VOIDmode)
14160 op_mode = GET_MODE (XEXP (rtl, 1));
14161 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14162 break;
14164 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14165 VAR_INIT_STATUS_INITIALIZED);
14166 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14167 VAR_INIT_STATUS_INITIALIZED);
14169 if (op0 == 0 || op1 == 0)
14170 break;
14172 if (op_mode != VOIDmode
14173 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14175 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14176 dw_loc_descr_ref last0, last1;
14177 for (last0 = op0;
14178 last0->dw_loc_next != NULL;
14179 last0 = last0->dw_loc_next)
14181 for (last1 = op1;
14182 last1->dw_loc_next != NULL;
14183 last1 = last1->dw_loc_next)
14185 if (CONST_INT_P (XEXP (rtl, 0)))
14186 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14187 /* deref_size zero extends, so no need to mask it again. */
14188 else if (last0->dw_loc_opc != DW_OP_deref_size
14189 || last0->dw_loc_oprnd1.v.val_int
14190 > GET_MODE_SIZE (op_mode))
14192 add_loc_descr (&op0, int_loc_descriptor (mask));
14193 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14195 if (CONST_INT_P (XEXP (rtl, 1)))
14196 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14197 /* deref_size zero extends, so no need to mask it again. */
14198 else if (last1->dw_loc_opc != DW_OP_deref_size
14199 || last1->dw_loc_oprnd1.v.val_int
14200 > GET_MODE_SIZE (op_mode))
14202 add_loc_descr (&op1, int_loc_descriptor (mask));
14203 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14206 else
14208 HOST_WIDE_INT bias = 1;
14209 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14210 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14211 if (CONST_INT_P (XEXP (rtl, 1)))
14212 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14213 + INTVAL (XEXP (rtl, 1)));
14214 else
14215 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14216 bias, 0));
14219 goto do_compare;
14221 case SMIN:
14222 case SMAX:
14223 case UMIN:
14224 case UMAX:
14225 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14226 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14227 || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14228 break;
14230 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14231 VAR_INIT_STATUS_INITIALIZED);
14232 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14233 VAR_INIT_STATUS_INITIALIZED);
14235 if (op0 == 0 || op1 == 0)
14236 break;
14238 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14239 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14240 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14241 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14243 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14245 HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14246 add_loc_descr (&op0, int_loc_descriptor (mask));
14247 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14248 add_loc_descr (&op1, int_loc_descriptor (mask));
14249 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14251 else
14253 HOST_WIDE_INT bias = 1;
14254 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14255 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14256 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14259 else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14261 int shift = DWARF2_ADDR_SIZE
14262 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14263 shift *= BITS_PER_UNIT;
14264 add_loc_descr (&op0, int_loc_descriptor (shift));
14265 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14266 add_loc_descr (&op1, int_loc_descriptor (shift));
14267 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14270 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14271 op = DW_OP_lt;
14272 else
14273 op = DW_OP_gt;
14274 mem_loc_result = op0;
14275 add_loc_descr (&mem_loc_result, op1);
14276 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14278 dw_loc_descr_ref bra_node, drop_node;
14280 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14281 add_loc_descr (&mem_loc_result, bra_node);
14282 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14283 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14284 add_loc_descr (&mem_loc_result, drop_node);
14285 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14286 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14288 break;
14290 case ZERO_EXTRACT:
14291 case SIGN_EXTRACT:
14292 if (CONST_INT_P (XEXP (rtl, 1))
14293 && CONST_INT_P (XEXP (rtl, 2))
14294 && ((unsigned) INTVAL (XEXP (rtl, 1))
14295 + (unsigned) INTVAL (XEXP (rtl, 2))
14296 <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14297 && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14298 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14300 int shift, size;
14301 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14302 VAR_INIT_STATUS_INITIALIZED);
14303 if (op0 == 0)
14304 break;
14305 if (GET_CODE (rtl) == SIGN_EXTRACT)
14306 op = DW_OP_shra;
14307 else
14308 op = DW_OP_shr;
14309 mem_loc_result = op0;
14310 size = INTVAL (XEXP (rtl, 1));
14311 shift = INTVAL (XEXP (rtl, 2));
14312 if (BITS_BIG_ENDIAN)
14313 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14314 - shift - size;
14315 if (shift + size != (int) DWARF2_ADDR_SIZE)
14317 add_loc_descr (&mem_loc_result,
14318 int_loc_descriptor (DWARF2_ADDR_SIZE
14319 - shift - size));
14320 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14322 if (size != (int) DWARF2_ADDR_SIZE)
14324 add_loc_descr (&mem_loc_result,
14325 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14326 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14329 break;
14331 case IF_THEN_ELSE:
14333 dw_loc_descr_ref op2, bra_node, drop_node;
14334 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14335 VAR_INIT_STATUS_INITIALIZED);
14336 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14337 VAR_INIT_STATUS_INITIALIZED);
14338 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14339 VAR_INIT_STATUS_INITIALIZED);
14340 if (op0 == NULL || op1 == NULL || op2 == NULL)
14341 break;
14343 mem_loc_result = op1;
14344 add_loc_descr (&mem_loc_result, op2);
14345 add_loc_descr (&mem_loc_result, op0);
14346 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14347 add_loc_descr (&mem_loc_result, bra_node);
14348 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14349 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14350 add_loc_descr (&mem_loc_result, drop_node);
14351 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14352 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14354 break;
14356 case COMPARE:
14357 case ROTATE:
14358 case ROTATERT:
14359 case TRUNCATE:
14360 /* In theory, we could implement the above. */
14361 /* DWARF cannot represent the unsigned compare operations
14362 natively. */
14363 case SS_MULT:
14364 case US_MULT:
14365 case SS_DIV:
14366 case US_DIV:
14367 case SS_PLUS:
14368 case US_PLUS:
14369 case SS_MINUS:
14370 case US_MINUS:
14371 case SS_NEG:
14372 case US_NEG:
14373 case SS_ABS:
14374 case SS_ASHIFT:
14375 case US_ASHIFT:
14376 case SS_TRUNCATE:
14377 case US_TRUNCATE:
14378 case UDIV:
14379 case UNORDERED:
14380 case ORDERED:
14381 case UNEQ:
14382 case UNGE:
14383 case UNGT:
14384 case UNLE:
14385 case UNLT:
14386 case LTGT:
14387 case FLOAT_EXTEND:
14388 case FLOAT_TRUNCATE:
14389 case FLOAT:
14390 case UNSIGNED_FLOAT:
14391 case FIX:
14392 case UNSIGNED_FIX:
14393 case FRACT_CONVERT:
14394 case UNSIGNED_FRACT_CONVERT:
14395 case SAT_FRACT:
14396 case UNSIGNED_SAT_FRACT:
14397 case SQRT:
14398 case BSWAP:
14399 case FFS:
14400 case CLZ:
14401 case CTZ:
14402 case POPCOUNT:
14403 case PARITY:
14404 case ASM_OPERANDS:
14405 case VEC_MERGE:
14406 case VEC_SELECT:
14407 case VEC_CONCAT:
14408 case VEC_DUPLICATE:
14409 case UNSPEC:
14410 case HIGH:
14411 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14412 can't express it in the debug info. This can happen e.g. with some
14413 TLS UNSPECs. */
14414 break;
14416 case CONST_STRING:
14417 resolve_one_addr (&rtl, NULL);
14418 goto symref;
14420 default:
14421 #ifdef ENABLE_CHECKING
14422 print_rtl (stderr, rtl);
14423 gcc_unreachable ();
14424 #else
14425 break;
14426 #endif
14429 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14430 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14432 return mem_loc_result;
14435 /* Return a descriptor that describes the concatenation of two locations.
14436 This is typically a complex variable. */
14438 static dw_loc_descr_ref
14439 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14441 dw_loc_descr_ref cc_loc_result = NULL;
14442 dw_loc_descr_ref x0_ref
14443 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14444 dw_loc_descr_ref x1_ref
14445 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14447 if (x0_ref == 0 || x1_ref == 0)
14448 return 0;
14450 cc_loc_result = x0_ref;
14451 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14453 add_loc_descr (&cc_loc_result, x1_ref);
14454 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14456 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14457 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14459 return cc_loc_result;
14462 /* Return a descriptor that describes the concatenation of N
14463 locations. */
14465 static dw_loc_descr_ref
14466 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14468 unsigned int i;
14469 dw_loc_descr_ref cc_loc_result = NULL;
14470 unsigned int n = XVECLEN (concatn, 0);
14472 for (i = 0; i < n; ++i)
14474 dw_loc_descr_ref ref;
14475 rtx x = XVECEXP (concatn, 0, i);
14477 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14478 if (ref == NULL)
14479 return NULL;
14481 add_loc_descr (&cc_loc_result, ref);
14482 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14485 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14486 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14488 return cc_loc_result;
14491 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14492 for DEBUG_IMPLICIT_PTR RTL. */
14494 static dw_loc_descr_ref
14495 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14497 dw_loc_descr_ref ret;
14498 dw_die_ref ref;
14500 if (dwarf_strict)
14501 return NULL;
14502 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14503 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14504 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14505 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14506 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14507 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14508 if (ref)
14510 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14511 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14512 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14514 else
14516 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14517 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14519 return ret;
14522 /* Output a proper Dwarf location descriptor for a variable or parameter
14523 which is either allocated in a register or in a memory location. For a
14524 register, we just generate an OP_REG and the register number. For a
14525 memory location we provide a Dwarf postfix expression describing how to
14526 generate the (dynamic) address of the object onto the address stack.
14528 MODE is mode of the decl if this loc_descriptor is going to be used in
14529 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14530 allowed, VOIDmode otherwise.
14532 If we don't know how to describe it, return 0. */
14534 static dw_loc_descr_ref
14535 loc_descriptor (rtx rtl, enum machine_mode mode,
14536 enum var_init_status initialized)
14538 dw_loc_descr_ref loc_result = NULL;
14540 switch (GET_CODE (rtl))
14542 case SUBREG:
14543 /* The case of a subreg may arise when we have a local (register)
14544 variable or a formal (register) parameter which doesn't quite fill
14545 up an entire register. For now, just assume that it is
14546 legitimate to make the Dwarf info refer to the whole register which
14547 contains the given subreg. */
14548 loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14549 break;
14551 case REG:
14552 loc_result = reg_loc_descriptor (rtl, initialized);
14553 break;
14555 case MEM:
14556 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14557 initialized);
14558 if (loc_result == NULL)
14559 loc_result = tls_mem_loc_descriptor (rtl);
14560 if (loc_result == NULL)
14562 rtx new_rtl = avoid_constant_pool_reference (rtl);
14563 if (new_rtl != rtl)
14564 loc_result = loc_descriptor (new_rtl, mode, initialized);
14566 break;
14568 case CONCAT:
14569 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14570 initialized);
14571 break;
14573 case CONCATN:
14574 loc_result = concatn_loc_descriptor (rtl, initialized);
14575 break;
14577 case VAR_LOCATION:
14578 /* Single part. */
14579 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14581 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14582 if (GET_CODE (loc) == EXPR_LIST)
14583 loc = XEXP (loc, 0);
14584 loc_result = loc_descriptor (loc, mode, initialized);
14585 break;
14588 rtl = XEXP (rtl, 1);
14589 /* FALLTHRU */
14591 case PARALLEL:
14593 rtvec par_elems = XVEC (rtl, 0);
14594 int num_elem = GET_NUM_ELEM (par_elems);
14595 enum machine_mode mode;
14596 int i;
14598 /* Create the first one, so we have something to add to. */
14599 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14600 VOIDmode, initialized);
14601 if (loc_result == NULL)
14602 return NULL;
14603 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14604 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14605 for (i = 1; i < num_elem; i++)
14607 dw_loc_descr_ref temp;
14609 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14610 VOIDmode, initialized);
14611 if (temp == NULL)
14612 return NULL;
14613 add_loc_descr (&loc_result, temp);
14614 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14615 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14618 break;
14620 case CONST_INT:
14621 if (mode != VOIDmode && mode != BLKmode)
14622 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14623 INTVAL (rtl));
14624 break;
14626 case CONST_DOUBLE:
14627 if (mode == VOIDmode)
14628 mode = GET_MODE (rtl);
14630 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14632 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14634 /* Note that a CONST_DOUBLE rtx could represent either an integer
14635 or a floating-point constant. A CONST_DOUBLE is used whenever
14636 the constant requires more than one word in order to be
14637 adequately represented. We output CONST_DOUBLEs as blocks. */
14638 loc_result = new_loc_descr (DW_OP_implicit_value,
14639 GET_MODE_SIZE (mode), 0);
14640 if (SCALAR_FLOAT_MODE_P (mode))
14642 unsigned int length = GET_MODE_SIZE (mode);
14643 unsigned char *array
14644 = (unsigned char*) ggc_alloc_atomic (length);
14646 insert_float (rtl, array);
14647 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14648 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14649 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14650 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14652 else
14654 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14655 loc_result->dw_loc_oprnd2.v.val_double
14656 = rtx_to_double_int (rtl);
14659 break;
14661 case CONST_VECTOR:
14662 if (mode == VOIDmode)
14663 mode = GET_MODE (rtl);
14665 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14667 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14668 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14669 unsigned char *array = (unsigned char *)
14670 ggc_alloc_atomic (length * elt_size);
14671 unsigned int i;
14672 unsigned char *p;
14674 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14675 switch (GET_MODE_CLASS (mode))
14677 case MODE_VECTOR_INT:
14678 for (i = 0, p = array; i < length; i++, p += elt_size)
14680 rtx elt = CONST_VECTOR_ELT (rtl, i);
14681 double_int val = rtx_to_double_int (elt);
14683 if (elt_size <= sizeof (HOST_WIDE_INT))
14684 insert_int (double_int_to_shwi (val), elt_size, p);
14685 else
14687 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14688 insert_double (val, p);
14691 break;
14693 case MODE_VECTOR_FLOAT:
14694 for (i = 0, p = array; i < length; i++, p += elt_size)
14696 rtx elt = CONST_VECTOR_ELT (rtl, i);
14697 insert_float (elt, p);
14699 break;
14701 default:
14702 gcc_unreachable ();
14705 loc_result = new_loc_descr (DW_OP_implicit_value,
14706 length * elt_size, 0);
14707 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14708 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14709 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14710 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14712 break;
14714 case CONST:
14715 if (mode == VOIDmode
14716 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14717 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14718 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14720 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14721 break;
14723 /* FALLTHROUGH */
14724 case SYMBOL_REF:
14725 if (!const_ok_for_output (rtl))
14726 break;
14727 case LABEL_REF:
14728 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14729 && (dwarf_version >= 4 || !dwarf_strict))
14731 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14732 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14733 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14734 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14735 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14737 break;
14739 case DEBUG_IMPLICIT_PTR:
14740 loc_result = implicit_ptr_descriptor (rtl, 0);
14741 break;
14743 case PLUS:
14744 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14745 && CONST_INT_P (XEXP (rtl, 1)))
14747 loc_result
14748 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14749 break;
14751 /* FALLTHRU */
14752 default:
14753 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14754 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14755 && (dwarf_version >= 4 || !dwarf_strict))
14757 /* Value expression. */
14758 loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14759 if (loc_result)
14760 add_loc_descr (&loc_result,
14761 new_loc_descr (DW_OP_stack_value, 0, 0));
14763 break;
14766 return loc_result;
14769 /* We need to figure out what section we should use as the base for the
14770 address ranges where a given location is valid.
14771 1. If this particular DECL has a section associated with it, use that.
14772 2. If this function has a section associated with it, use that.
14773 3. Otherwise, use the text section.
14774 XXX: If you split a variable across multiple sections, we won't notice. */
14776 static const char *
14777 secname_for_decl (const_tree decl)
14779 const char *secname;
14781 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14783 tree sectree = DECL_SECTION_NAME (decl);
14784 secname = TREE_STRING_POINTER (sectree);
14786 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14788 tree sectree = DECL_SECTION_NAME (current_function_decl);
14789 secname = TREE_STRING_POINTER (sectree);
14791 else if (cfun && in_cold_section_p)
14792 secname = crtl->subsections.cold_section_label;
14793 else
14794 secname = text_section_label;
14796 return secname;
14799 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14801 static bool
14802 decl_by_reference_p (tree decl)
14804 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14805 || TREE_CODE (decl) == VAR_DECL)
14806 && DECL_BY_REFERENCE (decl));
14809 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14810 for VARLOC. */
14812 static dw_loc_descr_ref
14813 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14814 enum var_init_status initialized)
14816 int have_address = 0;
14817 dw_loc_descr_ref descr;
14818 enum machine_mode mode;
14820 if (want_address != 2)
14822 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14823 /* Single part. */
14824 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14826 varloc = PAT_VAR_LOCATION_LOC (varloc);
14827 if (GET_CODE (varloc) == EXPR_LIST)
14828 varloc = XEXP (varloc, 0);
14829 mode = GET_MODE (varloc);
14830 if (MEM_P (varloc))
14832 rtx addr = XEXP (varloc, 0);
14833 descr = mem_loc_descriptor (addr, mode, initialized);
14834 if (descr)
14835 have_address = 1;
14836 else
14838 rtx x = avoid_constant_pool_reference (varloc);
14839 if (x != varloc)
14840 descr = mem_loc_descriptor (x, mode, initialized);
14843 else
14844 descr = mem_loc_descriptor (varloc, mode, initialized);
14846 else
14847 return 0;
14849 else
14851 if (GET_CODE (varloc) == VAR_LOCATION)
14852 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14853 else
14854 mode = DECL_MODE (loc);
14855 descr = loc_descriptor (varloc, mode, initialized);
14856 have_address = 1;
14859 if (!descr)
14860 return 0;
14862 if (want_address == 2 && !have_address
14863 && (dwarf_version >= 4 || !dwarf_strict))
14865 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14867 expansion_failed (loc, NULL_RTX,
14868 "DWARF address size mismatch");
14869 return 0;
14871 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14872 have_address = 1;
14874 /* Show if we can't fill the request for an address. */
14875 if (want_address && !have_address)
14877 expansion_failed (loc, NULL_RTX,
14878 "Want address and only have value");
14879 return 0;
14882 /* If we've got an address and don't want one, dereference. */
14883 if (!want_address && have_address)
14885 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14886 enum dwarf_location_atom op;
14888 if (size > DWARF2_ADDR_SIZE || size == -1)
14890 expansion_failed (loc, NULL_RTX,
14891 "DWARF address size mismatch");
14892 return 0;
14894 else if (size == DWARF2_ADDR_SIZE)
14895 op = DW_OP_deref;
14896 else
14897 op = DW_OP_deref_size;
14899 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14902 return descr;
14905 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14906 if it is not possible. */
14908 static dw_loc_descr_ref
14909 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14911 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14912 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14913 else if (dwarf_version >= 3 || !dwarf_strict)
14914 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14915 else
14916 return NULL;
14919 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14920 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14922 static dw_loc_descr_ref
14923 dw_sra_loc_expr (tree decl, rtx loc)
14925 rtx p;
14926 unsigned int padsize = 0;
14927 dw_loc_descr_ref descr, *descr_tail;
14928 unsigned HOST_WIDE_INT decl_size;
14929 rtx varloc;
14930 enum var_init_status initialized;
14932 if (DECL_SIZE (decl) == NULL
14933 || !host_integerp (DECL_SIZE (decl), 1))
14934 return NULL;
14936 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14937 descr = NULL;
14938 descr_tail = &descr;
14940 for (p = loc; p; p = XEXP (p, 1))
14942 unsigned int bitsize = decl_piece_bitsize (p);
14943 rtx loc_note = *decl_piece_varloc_ptr (p);
14944 dw_loc_descr_ref cur_descr;
14945 dw_loc_descr_ref *tail, last = NULL;
14946 unsigned int opsize = 0;
14948 if (loc_note == NULL_RTX
14949 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14951 padsize += bitsize;
14952 continue;
14954 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14955 varloc = NOTE_VAR_LOCATION (loc_note);
14956 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14957 if (cur_descr == NULL)
14959 padsize += bitsize;
14960 continue;
14963 /* Check that cur_descr either doesn't use
14964 DW_OP_*piece operations, or their sum is equal
14965 to bitsize. Otherwise we can't embed it. */
14966 for (tail = &cur_descr; *tail != NULL;
14967 tail = &(*tail)->dw_loc_next)
14968 if ((*tail)->dw_loc_opc == DW_OP_piece)
14970 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14971 * BITS_PER_UNIT;
14972 last = *tail;
14974 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14976 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14977 last = *tail;
14980 if (last != NULL && opsize != bitsize)
14982 padsize += bitsize;
14983 continue;
14986 /* If there is a hole, add DW_OP_*piece after empty DWARF
14987 expression, which means that those bits are optimized out. */
14988 if (padsize)
14990 if (padsize > decl_size)
14991 return NULL;
14992 decl_size -= padsize;
14993 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14994 if (*descr_tail == NULL)
14995 return NULL;
14996 descr_tail = &(*descr_tail)->dw_loc_next;
14997 padsize = 0;
14999 *descr_tail = cur_descr;
15000 descr_tail = tail;
15001 if (bitsize > decl_size)
15002 return NULL;
15003 decl_size -= bitsize;
15004 if (last == NULL)
15006 HOST_WIDE_INT offset = 0;
15007 if (GET_CODE (varloc) == VAR_LOCATION
15008 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15010 varloc = PAT_VAR_LOCATION_LOC (varloc);
15011 if (GET_CODE (varloc) == EXPR_LIST)
15012 varloc = XEXP (varloc, 0);
15016 if (GET_CODE (varloc) == CONST
15017 || GET_CODE (varloc) == SIGN_EXTEND
15018 || GET_CODE (varloc) == ZERO_EXTEND)
15019 varloc = XEXP (varloc, 0);
15020 else if (GET_CODE (varloc) == SUBREG)
15021 varloc = SUBREG_REG (varloc);
15022 else
15023 break;
15025 while (1);
15026 /* DW_OP_bit_size offset should be zero for register
15027 or implicit location descriptions and empty location
15028 descriptions, but for memory addresses needs big endian
15029 adjustment. */
15030 if (MEM_P (varloc))
15032 unsigned HOST_WIDE_INT memsize
15033 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15034 if (memsize != bitsize)
15036 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15037 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15038 return NULL;
15039 if (memsize < bitsize)
15040 return NULL;
15041 if (BITS_BIG_ENDIAN)
15042 offset = memsize - bitsize;
15046 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15047 if (*descr_tail == NULL)
15048 return NULL;
15049 descr_tail = &(*descr_tail)->dw_loc_next;
15053 /* If there were any non-empty expressions, add padding till the end of
15054 the decl. */
15055 if (descr != NULL && decl_size != 0)
15057 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15058 if (*descr_tail == NULL)
15059 return NULL;
15061 return descr;
15064 /* Return the dwarf representation of the location list LOC_LIST of
15065 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
15066 function. */
15068 static dw_loc_list_ref
15069 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15071 const char *endname, *secname;
15072 rtx varloc;
15073 enum var_init_status initialized;
15074 struct var_loc_node *node;
15075 dw_loc_descr_ref descr;
15076 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15077 dw_loc_list_ref list = NULL;
15078 dw_loc_list_ref *listp = &list;
15080 /* Now that we know what section we are using for a base,
15081 actually construct the list of locations.
15082 The first location information is what is passed to the
15083 function that creates the location list, and the remaining
15084 locations just get added on to that list.
15085 Note that we only know the start address for a location
15086 (IE location changes), so to build the range, we use
15087 the range [current location start, next location start].
15088 This means we have to special case the last node, and generate
15089 a range of [last location start, end of function label]. */
15091 secname = secname_for_decl (decl);
15093 for (node = loc_list->first; node; node = node->next)
15094 if (GET_CODE (node->loc) == EXPR_LIST
15095 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15097 if (GET_CODE (node->loc) == EXPR_LIST)
15099 /* This requires DW_OP_{,bit_}piece, which is not usable
15100 inside DWARF expressions. */
15101 if (want_address != 2)
15102 continue;
15103 descr = dw_sra_loc_expr (decl, node->loc);
15104 if (descr == NULL)
15105 continue;
15107 else
15109 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15110 varloc = NOTE_VAR_LOCATION (node->loc);
15111 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15113 if (descr)
15115 /* The variable has a location between NODE->LABEL and
15116 NODE->NEXT->LABEL. */
15117 if (node->next)
15118 endname = node->next->label;
15119 /* If the variable has a location at the last label
15120 it keeps its location until the end of function. */
15121 else if (!current_function_decl)
15122 endname = text_end_label;
15123 else
15125 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15126 current_function_funcdef_no);
15127 endname = ggc_strdup (label_id);
15130 *listp = new_loc_list (descr, node->label, endname, secname);
15131 listp = &(*listp)->dw_loc_next;
15135 /* Try to avoid the overhead of a location list emitting a location
15136 expression instead, but only if we didn't have more than one
15137 location entry in the first place. If some entries were not
15138 representable, we don't want to pretend a single entry that was
15139 applies to the entire scope in which the variable is
15140 available. */
15141 if (list && loc_list->first->next)
15142 gen_llsym (list);
15144 return list;
15147 /* Return if the loc_list has only single element and thus can be represented
15148 as location description. */
15150 static bool
15151 single_element_loc_list_p (dw_loc_list_ref list)
15153 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15154 return !list->ll_symbol;
15157 /* To each location in list LIST add loc descr REF. */
15159 static void
15160 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15162 dw_loc_descr_ref copy;
15163 add_loc_descr (&list->expr, ref);
15164 list = list->dw_loc_next;
15165 while (list)
15167 copy = ggc_alloc_dw_loc_descr_node ();
15168 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15169 add_loc_descr (&list->expr, copy);
15170 while (copy->dw_loc_next)
15172 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15173 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15174 copy->dw_loc_next = new_copy;
15175 copy = new_copy;
15177 list = list->dw_loc_next;
15181 /* Given two lists RET and LIST
15182 produce location list that is result of adding expression in LIST
15183 to expression in RET on each possition in program.
15184 Might be destructive on both RET and LIST.
15186 TODO: We handle only simple cases of RET or LIST having at most one
15187 element. General case would inolve sorting the lists in program order
15188 and merging them that will need some additional work.
15189 Adding that will improve quality of debug info especially for SRA-ed
15190 structures. */
15192 static void
15193 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15195 if (!list)
15196 return;
15197 if (!*ret)
15199 *ret = list;
15200 return;
15202 if (!list->dw_loc_next)
15204 add_loc_descr_to_each (*ret, list->expr);
15205 return;
15207 if (!(*ret)->dw_loc_next)
15209 add_loc_descr_to_each (list, (*ret)->expr);
15210 *ret = list;
15211 return;
15213 expansion_failed (NULL_TREE, NULL_RTX,
15214 "Don't know how to merge two non-trivial"
15215 " location lists.\n");
15216 *ret = NULL;
15217 return;
15220 /* LOC is constant expression. Try a luck, look it up in constant
15221 pool and return its loc_descr of its address. */
15223 static dw_loc_descr_ref
15224 cst_pool_loc_descr (tree loc)
15226 /* Get an RTL for this, if something has been emitted. */
15227 rtx rtl = lookup_constant_def (loc);
15228 enum machine_mode mode;
15230 if (!rtl || !MEM_P (rtl))
15232 gcc_assert (!rtl);
15233 return 0;
15235 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15237 /* TODO: We might get more coverage if we was actually delaying expansion
15238 of all expressions till end of compilation when constant pools are fully
15239 populated. */
15240 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15242 expansion_failed (loc, NULL_RTX,
15243 "CST value in contant pool but not marked.");
15244 return 0;
15246 mode = GET_MODE (rtl);
15247 rtl = XEXP (rtl, 0);
15248 return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15251 /* Return dw_loc_list representing address of addr_expr LOC
15252 by looking for innder INDIRECT_REF expression and turing it
15253 into simple arithmetics. */
15255 static dw_loc_list_ref
15256 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15258 tree obj, offset;
15259 HOST_WIDE_INT bitsize, bitpos, bytepos;
15260 enum machine_mode mode;
15261 int volatilep;
15262 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15263 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15265 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15266 &bitsize, &bitpos, &offset, &mode,
15267 &unsignedp, &volatilep, false);
15268 STRIP_NOPS (obj);
15269 if (bitpos % BITS_PER_UNIT)
15271 expansion_failed (loc, NULL_RTX, "bitfield access");
15272 return 0;
15274 if (!INDIRECT_REF_P (obj))
15276 expansion_failed (obj,
15277 NULL_RTX, "no indirect ref in inner refrence");
15278 return 0;
15280 if (!offset && !bitpos)
15281 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15282 else if (toplev
15283 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15284 && (dwarf_version >= 4 || !dwarf_strict))
15286 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15287 if (!list_ret)
15288 return 0;
15289 if (offset)
15291 /* Variable offset. */
15292 list_ret1 = loc_list_from_tree (offset, 0);
15293 if (list_ret1 == 0)
15294 return 0;
15295 add_loc_list (&list_ret, list_ret1);
15296 if (!list_ret)
15297 return 0;
15298 add_loc_descr_to_each (list_ret,
15299 new_loc_descr (DW_OP_plus, 0, 0));
15301 bytepos = bitpos / BITS_PER_UNIT;
15302 if (bytepos > 0)
15303 add_loc_descr_to_each (list_ret,
15304 new_loc_descr (DW_OP_plus_uconst,
15305 bytepos, 0));
15306 else if (bytepos < 0)
15307 loc_list_plus_const (list_ret, bytepos);
15308 add_loc_descr_to_each (list_ret,
15309 new_loc_descr (DW_OP_stack_value, 0, 0));
15311 return list_ret;
15315 /* Generate Dwarf location list representing LOC.
15316 If WANT_ADDRESS is false, expression computing LOC will be computed
15317 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15318 if WANT_ADDRESS is 2, expression computing address useable in location
15319 will be returned (i.e. DW_OP_reg can be used
15320 to refer to register values). */
15322 static dw_loc_list_ref
15323 loc_list_from_tree (tree loc, int want_address)
15325 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15326 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15327 int have_address = 0;
15328 enum dwarf_location_atom op;
15330 /* ??? Most of the time we do not take proper care for sign/zero
15331 extending the values properly. Hopefully this won't be a real
15332 problem... */
15334 switch (TREE_CODE (loc))
15336 case ERROR_MARK:
15337 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15338 return 0;
15340 case PLACEHOLDER_EXPR:
15341 /* This case involves extracting fields from an object to determine the
15342 position of other fields. We don't try to encode this here. The
15343 only user of this is Ada, which encodes the needed information using
15344 the names of types. */
15345 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15346 return 0;
15348 case CALL_EXPR:
15349 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15350 /* There are no opcodes for these operations. */
15351 return 0;
15353 case PREINCREMENT_EXPR:
15354 case PREDECREMENT_EXPR:
15355 case POSTINCREMENT_EXPR:
15356 case POSTDECREMENT_EXPR:
15357 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15358 /* There are no opcodes for these operations. */
15359 return 0;
15361 case ADDR_EXPR:
15362 /* If we already want an address, see if there is INDIRECT_REF inside
15363 e.g. for &this->field. */
15364 if (want_address)
15366 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15367 (loc, want_address == 2);
15368 if (list_ret)
15369 have_address = 1;
15370 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15371 && (ret = cst_pool_loc_descr (loc)))
15372 have_address = 1;
15374 /* Otherwise, process the argument and look for the address. */
15375 if (!list_ret && !ret)
15376 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15377 else
15379 if (want_address)
15380 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15381 return NULL;
15383 break;
15385 case VAR_DECL:
15386 if (DECL_THREAD_LOCAL_P (loc))
15388 rtx rtl;
15389 enum dwarf_location_atom first_op;
15390 enum dwarf_location_atom second_op;
15391 bool dtprel = false;
15393 if (targetm.have_tls)
15395 /* If this is not defined, we have no way to emit the
15396 data. */
15397 if (!targetm.asm_out.output_dwarf_dtprel)
15398 return 0;
15400 /* The way DW_OP_GNU_push_tls_address is specified, we
15401 can only look up addresses of objects in the current
15402 module. We used DW_OP_addr as first op, but that's
15403 wrong, because DW_OP_addr is relocated by the debug
15404 info consumer, while DW_OP_GNU_push_tls_address
15405 operand shouldn't be. */
15406 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15407 return 0;
15408 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15409 dtprel = true;
15410 second_op = DW_OP_GNU_push_tls_address;
15412 else
15414 if (!targetm.emutls.debug_form_tls_address
15415 || !(dwarf_version >= 3 || !dwarf_strict))
15416 return 0;
15417 /* We stuffed the control variable into the DECL_VALUE_EXPR
15418 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15419 no longer appear in gimple code. We used the control
15420 variable in specific so that we could pick it up here. */
15421 loc = DECL_VALUE_EXPR (loc);
15422 first_op = DW_OP_addr;
15423 second_op = DW_OP_form_tls_address;
15426 rtl = rtl_for_decl_location (loc);
15427 if (rtl == NULL_RTX)
15428 return 0;
15430 if (!MEM_P (rtl))
15431 return 0;
15432 rtl = XEXP (rtl, 0);
15433 if (! CONSTANT_P (rtl))
15434 return 0;
15436 ret = new_loc_descr (first_op, 0, 0);
15437 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15438 ret->dw_loc_oprnd1.v.val_addr = rtl;
15439 ret->dtprel = dtprel;
15441 ret1 = new_loc_descr (second_op, 0, 0);
15442 add_loc_descr (&ret, ret1);
15444 have_address = 1;
15445 break;
15447 /* FALLTHRU */
15449 case PARM_DECL:
15450 case RESULT_DECL:
15451 if (DECL_HAS_VALUE_EXPR_P (loc))
15452 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15453 want_address);
15454 /* FALLTHRU */
15456 case FUNCTION_DECL:
15458 rtx rtl;
15459 var_loc_list *loc_list = lookup_decl_loc (loc);
15461 if (loc_list && loc_list->first)
15463 list_ret = dw_loc_list (loc_list, loc, want_address);
15464 have_address = want_address != 0;
15465 break;
15467 rtl = rtl_for_decl_location (loc);
15468 if (rtl == NULL_RTX)
15470 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15471 return 0;
15473 else if (CONST_INT_P (rtl))
15475 HOST_WIDE_INT val = INTVAL (rtl);
15476 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15477 val &= GET_MODE_MASK (DECL_MODE (loc));
15478 ret = int_loc_descriptor (val);
15480 else if (GET_CODE (rtl) == CONST_STRING)
15482 expansion_failed (loc, NULL_RTX, "CONST_STRING");
15483 return 0;
15485 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15487 ret = new_loc_descr (DW_OP_addr, 0, 0);
15488 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15489 ret->dw_loc_oprnd1.v.val_addr = rtl;
15491 else
15493 enum machine_mode mode;
15495 /* Certain constructs can only be represented at top-level. */
15496 if (want_address == 2)
15498 ret = loc_descriptor (rtl, VOIDmode,
15499 VAR_INIT_STATUS_INITIALIZED);
15500 have_address = 1;
15502 else
15504 mode = GET_MODE (rtl);
15505 if (MEM_P (rtl))
15507 rtl = XEXP (rtl, 0);
15508 have_address = 1;
15510 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15512 if (!ret)
15513 expansion_failed (loc, rtl,
15514 "failed to produce loc descriptor for rtl");
15517 break;
15519 case MEM_REF:
15520 /* ??? FIXME. */
15521 if (!integer_zerop (TREE_OPERAND (loc, 1)))
15522 return 0;
15523 /* Fallthru. */
15524 case INDIRECT_REF:
15525 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15526 have_address = 1;
15527 break;
15529 case COMPOUND_EXPR:
15530 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15532 CASE_CONVERT:
15533 case VIEW_CONVERT_EXPR:
15534 case SAVE_EXPR:
15535 case MODIFY_EXPR:
15536 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15538 case COMPONENT_REF:
15539 case BIT_FIELD_REF:
15540 case ARRAY_REF:
15541 case ARRAY_RANGE_REF:
15542 case REALPART_EXPR:
15543 case IMAGPART_EXPR:
15545 tree obj, offset;
15546 HOST_WIDE_INT bitsize, bitpos, bytepos;
15547 enum machine_mode mode;
15548 int volatilep;
15549 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15551 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15552 &unsignedp, &volatilep, false);
15554 gcc_assert (obj != loc);
15556 list_ret = loc_list_from_tree (obj,
15557 want_address == 2
15558 && !bitpos && !offset ? 2 : 1);
15559 /* TODO: We can extract value of the small expression via shifting even
15560 for nonzero bitpos. */
15561 if (list_ret == 0)
15562 return 0;
15563 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15565 expansion_failed (loc, NULL_RTX,
15566 "bitfield access");
15567 return 0;
15570 if (offset != NULL_TREE)
15572 /* Variable offset. */
15573 list_ret1 = loc_list_from_tree (offset, 0);
15574 if (list_ret1 == 0)
15575 return 0;
15576 add_loc_list (&list_ret, list_ret1);
15577 if (!list_ret)
15578 return 0;
15579 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15582 bytepos = bitpos / BITS_PER_UNIT;
15583 if (bytepos > 0)
15584 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15585 else if (bytepos < 0)
15586 loc_list_plus_const (list_ret, bytepos);
15588 have_address = 1;
15589 break;
15592 case INTEGER_CST:
15593 if ((want_address || !host_integerp (loc, 0))
15594 && (ret = cst_pool_loc_descr (loc)))
15595 have_address = 1;
15596 else if (want_address == 2
15597 && host_integerp (loc, 0)
15598 && (ret = address_of_int_loc_descriptor
15599 (int_size_in_bytes (TREE_TYPE (loc)),
15600 tree_low_cst (loc, 0))))
15601 have_address = 1;
15602 else if (host_integerp (loc, 0))
15603 ret = int_loc_descriptor (tree_low_cst (loc, 0));
15604 else
15606 expansion_failed (loc, NULL_RTX,
15607 "Integer operand is not host integer");
15608 return 0;
15610 break;
15612 case CONSTRUCTOR:
15613 case REAL_CST:
15614 case STRING_CST:
15615 case COMPLEX_CST:
15616 if ((ret = cst_pool_loc_descr (loc)))
15617 have_address = 1;
15618 else
15619 /* We can construct small constants here using int_loc_descriptor. */
15620 expansion_failed (loc, NULL_RTX,
15621 "constructor or constant not in constant pool");
15622 break;
15624 case TRUTH_AND_EXPR:
15625 case TRUTH_ANDIF_EXPR:
15626 case BIT_AND_EXPR:
15627 op = DW_OP_and;
15628 goto do_binop;
15630 case TRUTH_XOR_EXPR:
15631 case BIT_XOR_EXPR:
15632 op = DW_OP_xor;
15633 goto do_binop;
15635 case TRUTH_OR_EXPR:
15636 case TRUTH_ORIF_EXPR:
15637 case BIT_IOR_EXPR:
15638 op = DW_OP_or;
15639 goto do_binop;
15641 case FLOOR_DIV_EXPR:
15642 case CEIL_DIV_EXPR:
15643 case ROUND_DIV_EXPR:
15644 case TRUNC_DIV_EXPR:
15645 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15646 return 0;
15647 op = DW_OP_div;
15648 goto do_binop;
15650 case MINUS_EXPR:
15651 op = DW_OP_minus;
15652 goto do_binop;
15654 case FLOOR_MOD_EXPR:
15655 case CEIL_MOD_EXPR:
15656 case ROUND_MOD_EXPR:
15657 case TRUNC_MOD_EXPR:
15658 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15660 op = DW_OP_mod;
15661 goto do_binop;
15663 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15664 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15665 if (list_ret == 0 || list_ret1 == 0)
15666 return 0;
15668 add_loc_list (&list_ret, list_ret1);
15669 if (list_ret == 0)
15670 return 0;
15671 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15672 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15673 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15674 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15675 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15676 break;
15678 case MULT_EXPR:
15679 op = DW_OP_mul;
15680 goto do_binop;
15682 case LSHIFT_EXPR:
15683 op = DW_OP_shl;
15684 goto do_binop;
15686 case RSHIFT_EXPR:
15687 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15688 goto do_binop;
15690 case POINTER_PLUS_EXPR:
15691 case PLUS_EXPR:
15692 if (host_integerp (TREE_OPERAND (loc, 1), 0))
15694 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15695 if (list_ret == 0)
15696 return 0;
15698 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15699 break;
15702 op = DW_OP_plus;
15703 goto do_binop;
15705 case LE_EXPR:
15706 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15707 return 0;
15709 op = DW_OP_le;
15710 goto do_binop;
15712 case GE_EXPR:
15713 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15714 return 0;
15716 op = DW_OP_ge;
15717 goto do_binop;
15719 case LT_EXPR:
15720 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15721 return 0;
15723 op = DW_OP_lt;
15724 goto do_binop;
15726 case GT_EXPR:
15727 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15728 return 0;
15730 op = DW_OP_gt;
15731 goto do_binop;
15733 case EQ_EXPR:
15734 op = DW_OP_eq;
15735 goto do_binop;
15737 case NE_EXPR:
15738 op = DW_OP_ne;
15739 goto do_binop;
15741 do_binop:
15742 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15743 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15744 if (list_ret == 0 || list_ret1 == 0)
15745 return 0;
15747 add_loc_list (&list_ret, list_ret1);
15748 if (list_ret == 0)
15749 return 0;
15750 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15751 break;
15753 case TRUTH_NOT_EXPR:
15754 case BIT_NOT_EXPR:
15755 op = DW_OP_not;
15756 goto do_unop;
15758 case ABS_EXPR:
15759 op = DW_OP_abs;
15760 goto do_unop;
15762 case NEGATE_EXPR:
15763 op = DW_OP_neg;
15764 goto do_unop;
15766 do_unop:
15767 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15768 if (list_ret == 0)
15769 return 0;
15771 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15772 break;
15774 case MIN_EXPR:
15775 case MAX_EXPR:
15777 const enum tree_code code =
15778 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15780 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15781 build2 (code, integer_type_node,
15782 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15783 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15786 /* ... fall through ... */
15788 case COND_EXPR:
15790 dw_loc_descr_ref lhs
15791 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15792 dw_loc_list_ref rhs
15793 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15794 dw_loc_descr_ref bra_node, jump_node, tmp;
15796 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15797 if (list_ret == 0 || lhs == 0 || rhs == 0)
15798 return 0;
15800 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15801 add_loc_descr_to_each (list_ret, bra_node);
15803 add_loc_list (&list_ret, rhs);
15804 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15805 add_loc_descr_to_each (list_ret, jump_node);
15807 add_loc_descr_to_each (list_ret, lhs);
15808 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15809 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15811 /* ??? Need a node to point the skip at. Use a nop. */
15812 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15813 add_loc_descr_to_each (list_ret, tmp);
15814 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15815 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15817 break;
15819 case FIX_TRUNC_EXPR:
15820 return 0;
15822 default:
15823 /* Leave front-end specific codes as simply unknown. This comes
15824 up, for instance, with the C STMT_EXPR. */
15825 if ((unsigned int) TREE_CODE (loc)
15826 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15828 expansion_failed (loc, NULL_RTX,
15829 "language specific tree node");
15830 return 0;
15833 #ifdef ENABLE_CHECKING
15834 /* Otherwise this is a generic code; we should just lists all of
15835 these explicitly. We forgot one. */
15836 gcc_unreachable ();
15837 #else
15838 /* In a release build, we want to degrade gracefully: better to
15839 generate incomplete debugging information than to crash. */
15840 return NULL;
15841 #endif
15844 if (!ret && !list_ret)
15845 return 0;
15847 if (want_address == 2 && !have_address
15848 && (dwarf_version >= 4 || !dwarf_strict))
15850 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15852 expansion_failed (loc, NULL_RTX,
15853 "DWARF address size mismatch");
15854 return 0;
15856 if (ret)
15857 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15858 else
15859 add_loc_descr_to_each (list_ret,
15860 new_loc_descr (DW_OP_stack_value, 0, 0));
15861 have_address = 1;
15863 /* Show if we can't fill the request for an address. */
15864 if (want_address && !have_address)
15866 expansion_failed (loc, NULL_RTX,
15867 "Want address and only have value");
15868 return 0;
15871 gcc_assert (!ret || !list_ret);
15873 /* If we've got an address and don't want one, dereference. */
15874 if (!want_address && have_address)
15876 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15878 if (size > DWARF2_ADDR_SIZE || size == -1)
15880 expansion_failed (loc, NULL_RTX,
15881 "DWARF address size mismatch");
15882 return 0;
15884 else if (size == DWARF2_ADDR_SIZE)
15885 op = DW_OP_deref;
15886 else
15887 op = DW_OP_deref_size;
15889 if (ret)
15890 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15891 else
15892 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15894 if (ret)
15895 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15897 return list_ret;
15900 /* Same as above but return only single location expression. */
15901 static dw_loc_descr_ref
15902 loc_descriptor_from_tree (tree loc, int want_address)
15904 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15905 if (!ret)
15906 return NULL;
15907 if (ret->dw_loc_next)
15909 expansion_failed (loc, NULL_RTX,
15910 "Location list where only loc descriptor needed");
15911 return NULL;
15913 return ret->expr;
15916 /* Given a value, round it up to the lowest multiple of `boundary'
15917 which is not less than the value itself. */
15919 static inline HOST_WIDE_INT
15920 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15922 return (((value + boundary - 1) / boundary) * boundary);
15925 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15926 pointer to the declared type for the relevant field variable, or return
15927 `integer_type_node' if the given node turns out to be an
15928 ERROR_MARK node. */
15930 static inline tree
15931 field_type (const_tree decl)
15933 tree type;
15935 if (TREE_CODE (decl) == ERROR_MARK)
15936 return integer_type_node;
15938 type = DECL_BIT_FIELD_TYPE (decl);
15939 if (type == NULL_TREE)
15940 type = TREE_TYPE (decl);
15942 return type;
15945 /* Given a pointer to a tree node, return the alignment in bits for
15946 it, or else return BITS_PER_WORD if the node actually turns out to
15947 be an ERROR_MARK node. */
15949 static inline unsigned
15950 simple_type_align_in_bits (const_tree type)
15952 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15955 static inline unsigned
15956 simple_decl_align_in_bits (const_tree decl)
15958 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15961 /* Return the result of rounding T up to ALIGN. */
15963 static inline double_int
15964 round_up_to_align (double_int t, unsigned int align)
15966 double_int alignd = uhwi_to_double_int (align);
15967 t = double_int_add (t, alignd);
15968 t = double_int_add (t, double_int_minus_one);
15969 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15970 t = double_int_mul (t, alignd);
15971 return t;
15974 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15975 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15976 or return 0 if we are unable to determine what that offset is, either
15977 because the argument turns out to be a pointer to an ERROR_MARK node, or
15978 because the offset is actually variable. (We can't handle the latter case
15979 just yet). */
15981 static HOST_WIDE_INT
15982 field_byte_offset (const_tree decl)
15984 double_int object_offset_in_bits;
15985 double_int object_offset_in_bytes;
15986 double_int bitpos_int;
15988 if (TREE_CODE (decl) == ERROR_MARK)
15989 return 0;
15991 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15993 /* We cannot yet cope with fields whose positions are variable, so
15994 for now, when we see such things, we simply return 0. Someday, we may
15995 be able to handle such cases, but it will be damn difficult. */
15996 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15997 return 0;
15999 bitpos_int = tree_to_double_int (bit_position (decl));
16001 #ifdef PCC_BITFIELD_TYPE_MATTERS
16002 if (PCC_BITFIELD_TYPE_MATTERS)
16004 tree type;
16005 tree field_size_tree;
16006 double_int deepest_bitpos;
16007 double_int field_size_in_bits;
16008 unsigned int type_align_in_bits;
16009 unsigned int decl_align_in_bits;
16010 double_int type_size_in_bits;
16012 type = field_type (decl);
16013 type_size_in_bits = double_int_type_size_in_bits (type);
16014 type_align_in_bits = simple_type_align_in_bits (type);
16016 field_size_tree = DECL_SIZE (decl);
16018 /* The size could be unspecified if there was an error, or for
16019 a flexible array member. */
16020 if (!field_size_tree)
16021 field_size_tree = bitsize_zero_node;
16023 /* If the size of the field is not constant, use the type size. */
16024 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16025 field_size_in_bits = tree_to_double_int (field_size_tree);
16026 else
16027 field_size_in_bits = type_size_in_bits;
16029 decl_align_in_bits = simple_decl_align_in_bits (decl);
16031 /* The GCC front-end doesn't make any attempt to keep track of the
16032 starting bit offset (relative to the start of the containing
16033 structure type) of the hypothetical "containing object" for a
16034 bit-field. Thus, when computing the byte offset value for the
16035 start of the "containing object" of a bit-field, we must deduce
16036 this information on our own. This can be rather tricky to do in
16037 some cases. For example, handling the following structure type
16038 definition when compiling for an i386/i486 target (which only
16039 aligns long long's to 32-bit boundaries) can be very tricky:
16041 struct S { int field1; long long field2:31; };
16043 Fortunately, there is a simple rule-of-thumb which can be used
16044 in such cases. When compiling for an i386/i486, GCC will
16045 allocate 8 bytes for the structure shown above. It decides to
16046 do this based upon one simple rule for bit-field allocation.
16047 GCC allocates each "containing object" for each bit-field at
16048 the first (i.e. lowest addressed) legitimate alignment boundary
16049 (based upon the required minimum alignment for the declared
16050 type of the field) which it can possibly use, subject to the
16051 condition that there is still enough available space remaining
16052 in the containing object (when allocated at the selected point)
16053 to fully accommodate all of the bits of the bit-field itself.
16055 This simple rule makes it obvious why GCC allocates 8 bytes for
16056 each object of the structure type shown above. When looking
16057 for a place to allocate the "containing object" for `field2',
16058 the compiler simply tries to allocate a 64-bit "containing
16059 object" at each successive 32-bit boundary (starting at zero)
16060 until it finds a place to allocate that 64- bit field such that
16061 at least 31 contiguous (and previously unallocated) bits remain
16062 within that selected 64 bit field. (As it turns out, for the
16063 example above, the compiler finds it is OK to allocate the
16064 "containing object" 64-bit field at bit-offset zero within the
16065 structure type.)
16067 Here we attempt to work backwards from the limited set of facts
16068 we're given, and we try to deduce from those facts, where GCC
16069 must have believed that the containing object started (within
16070 the structure type). The value we deduce is then used (by the
16071 callers of this routine) to generate DW_AT_location and
16072 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16073 the case of DW_AT_location, regular fields as well). */
16075 /* Figure out the bit-distance from the start of the structure to
16076 the "deepest" bit of the bit-field. */
16077 deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16079 /* This is the tricky part. Use some fancy footwork to deduce
16080 where the lowest addressed bit of the containing object must
16081 be. */
16082 object_offset_in_bits
16083 = double_int_sub (deepest_bitpos, type_size_in_bits);
16085 /* Round up to type_align by default. This works best for
16086 bitfields. */
16087 object_offset_in_bits
16088 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16090 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16092 object_offset_in_bits
16093 = double_int_sub (deepest_bitpos, type_size_in_bits);
16095 /* Round up to decl_align instead. */
16096 object_offset_in_bits
16097 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16100 else
16101 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16102 object_offset_in_bits = bitpos_int;
16104 object_offset_in_bytes
16105 = double_int_div (object_offset_in_bits,
16106 uhwi_to_double_int (BITS_PER_UNIT), true,
16107 TRUNC_DIV_EXPR);
16108 return double_int_to_shwi (object_offset_in_bytes);
16111 /* The following routines define various Dwarf attributes and any data
16112 associated with them. */
16114 /* Add a location description attribute value to a DIE.
16116 This emits location attributes suitable for whole variables and
16117 whole parameters. Note that the location attributes for struct fields are
16118 generated by the routine `data_member_location_attribute' below. */
16120 static inline void
16121 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16122 dw_loc_list_ref descr)
16124 if (descr == 0)
16125 return;
16126 if (single_element_loc_list_p (descr))
16127 add_AT_loc (die, attr_kind, descr->expr);
16128 else
16129 add_AT_loc_list (die, attr_kind, descr);
16132 /* Add DW_AT_accessibility attribute to DIE if needed. */
16134 static void
16135 add_accessibility_attribute (dw_die_ref die, tree decl)
16137 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16138 children, otherwise the default is DW_ACCESS_public. In DWARF2
16139 the default has always been DW_ACCESS_public. */
16140 if (TREE_PROTECTED (decl))
16141 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16142 else if (TREE_PRIVATE (decl))
16144 if (dwarf_version == 2
16145 || die->die_parent == NULL
16146 || die->die_parent->die_tag != DW_TAG_class_type)
16147 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16149 else if (dwarf_version > 2
16150 && die->die_parent
16151 && die->die_parent->die_tag == DW_TAG_class_type)
16152 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16155 /* Attach the specialized form of location attribute used for data members of
16156 struct and union types. In the special case of a FIELD_DECL node which
16157 represents a bit-field, the "offset" part of this special location
16158 descriptor must indicate the distance in bytes from the lowest-addressed
16159 byte of the containing struct or union type to the lowest-addressed byte of
16160 the "containing object" for the bit-field. (See the `field_byte_offset'
16161 function above).
16163 For any given bit-field, the "containing object" is a hypothetical object
16164 (of some integral or enum type) within which the given bit-field lives. The
16165 type of this hypothetical "containing object" is always the same as the
16166 declared type of the individual bit-field itself (for GCC anyway... the
16167 DWARF spec doesn't actually mandate this). Note that it is the size (in
16168 bytes) of the hypothetical "containing object" which will be given in the
16169 DW_AT_byte_size attribute for this bit-field. (See the
16170 `byte_size_attribute' function below.) It is also used when calculating the
16171 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16172 function below.) */
16174 static void
16175 add_data_member_location_attribute (dw_die_ref die, tree decl)
16177 HOST_WIDE_INT offset;
16178 dw_loc_descr_ref loc_descr = 0;
16180 if (TREE_CODE (decl) == TREE_BINFO)
16182 /* We're working on the TAG_inheritance for a base class. */
16183 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16185 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16186 aren't at a fixed offset from all (sub)objects of the same
16187 type. We need to extract the appropriate offset from our
16188 vtable. The following dwarf expression means
16190 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16192 This is specific to the V3 ABI, of course. */
16194 dw_loc_descr_ref tmp;
16196 /* Make a copy of the object address. */
16197 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16198 add_loc_descr (&loc_descr, tmp);
16200 /* Extract the vtable address. */
16201 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16202 add_loc_descr (&loc_descr, tmp);
16204 /* Calculate the address of the offset. */
16205 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16206 gcc_assert (offset < 0);
16208 tmp = int_loc_descriptor (-offset);
16209 add_loc_descr (&loc_descr, tmp);
16210 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16211 add_loc_descr (&loc_descr, tmp);
16213 /* Extract the offset. */
16214 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16215 add_loc_descr (&loc_descr, tmp);
16217 /* Add it to the object address. */
16218 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16219 add_loc_descr (&loc_descr, tmp);
16221 else
16222 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16224 else
16225 offset = field_byte_offset (decl);
16227 if (! loc_descr)
16229 if (dwarf_version > 2)
16231 /* Don't need to output a location expression, just the constant. */
16232 if (offset < 0)
16233 add_AT_int (die, DW_AT_data_member_location, offset);
16234 else
16235 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16236 return;
16238 else
16240 enum dwarf_location_atom op;
16242 /* The DWARF2 standard says that we should assume that the structure
16243 address is already on the stack, so we can specify a structure
16244 field address by using DW_OP_plus_uconst. */
16246 #ifdef MIPS_DEBUGGING_INFO
16247 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16248 operator correctly. It works only if we leave the offset on the
16249 stack. */
16250 op = DW_OP_constu;
16251 #else
16252 op = DW_OP_plus_uconst;
16253 #endif
16255 loc_descr = new_loc_descr (op, offset, 0);
16259 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16262 /* Writes integer values to dw_vec_const array. */
16264 static void
16265 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16267 while (size != 0)
16269 *dest++ = val & 0xff;
16270 val >>= 8;
16271 --size;
16275 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
16277 static HOST_WIDE_INT
16278 extract_int (const unsigned char *src, unsigned int size)
16280 HOST_WIDE_INT val = 0;
16282 src += size;
16283 while (size != 0)
16285 val <<= 8;
16286 val |= *--src & 0xff;
16287 --size;
16289 return val;
16292 /* Writes double_int values to dw_vec_const array. */
16294 static void
16295 insert_double (double_int val, unsigned char *dest)
16297 unsigned char *p0 = dest;
16298 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16300 if (WORDS_BIG_ENDIAN)
16302 p0 = p1;
16303 p1 = dest;
16306 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16307 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16310 /* Writes floating point values to dw_vec_const array. */
16312 static void
16313 insert_float (const_rtx rtl, unsigned char *array)
16315 REAL_VALUE_TYPE rv;
16316 long val[4];
16317 int i;
16319 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16320 real_to_target (val, &rv, GET_MODE (rtl));
16322 /* real_to_target puts 32-bit pieces in each long. Pack them. */
16323 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16325 insert_int (val[i], 4, array);
16326 array += 4;
16330 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16331 does not have a "location" either in memory or in a register. These
16332 things can arise in GNU C when a constant is passed as an actual parameter
16333 to an inlined function. They can also arise in C++ where declared
16334 constants do not necessarily get memory "homes". */
16336 static bool
16337 add_const_value_attribute (dw_die_ref die, rtx rtl)
16339 switch (GET_CODE (rtl))
16341 case CONST_INT:
16343 HOST_WIDE_INT val = INTVAL (rtl);
16345 if (val < 0)
16346 add_AT_int (die, DW_AT_const_value, val);
16347 else
16348 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16350 return true;
16352 case CONST_DOUBLE:
16353 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16354 floating-point constant. A CONST_DOUBLE is used whenever the
16355 constant requires more than one word in order to be adequately
16356 represented. */
16358 enum machine_mode mode = GET_MODE (rtl);
16360 if (SCALAR_FLOAT_MODE_P (mode))
16362 unsigned int length = GET_MODE_SIZE (mode);
16363 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16365 insert_float (rtl, array);
16366 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16368 else
16369 add_AT_double (die, DW_AT_const_value,
16370 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16372 return true;
16374 case CONST_VECTOR:
16376 enum machine_mode mode = GET_MODE (rtl);
16377 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16378 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16379 unsigned char *array = (unsigned char *) ggc_alloc_atomic
16380 (length * elt_size);
16381 unsigned int i;
16382 unsigned char *p;
16384 switch (GET_MODE_CLASS (mode))
16386 case MODE_VECTOR_INT:
16387 for (i = 0, p = array; i < length; i++, p += elt_size)
16389 rtx elt = CONST_VECTOR_ELT (rtl, i);
16390 double_int val = rtx_to_double_int (elt);
16392 if (elt_size <= sizeof (HOST_WIDE_INT))
16393 insert_int (double_int_to_shwi (val), elt_size, p);
16394 else
16396 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16397 insert_double (val, p);
16400 break;
16402 case MODE_VECTOR_FLOAT:
16403 for (i = 0, p = array; i < length; i++, p += elt_size)
16405 rtx elt = CONST_VECTOR_ELT (rtl, i);
16406 insert_float (elt, p);
16408 break;
16410 default:
16411 gcc_unreachable ();
16414 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16416 return true;
16418 case CONST_STRING:
16419 if (dwarf_version >= 4 || !dwarf_strict)
16421 dw_loc_descr_ref loc_result;
16422 resolve_one_addr (&rtl, NULL);
16423 rtl_addr:
16424 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16425 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16426 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16427 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16428 add_AT_loc (die, DW_AT_location, loc_result);
16429 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16430 return true;
16432 return false;
16434 case CONST:
16435 if (CONSTANT_P (XEXP (rtl, 0)))
16436 return add_const_value_attribute (die, XEXP (rtl, 0));
16437 /* FALLTHROUGH */
16438 case SYMBOL_REF:
16439 if (!const_ok_for_output (rtl))
16440 return false;
16441 case LABEL_REF:
16442 if (dwarf_version >= 4 || !dwarf_strict)
16443 goto rtl_addr;
16444 return false;
16446 case PLUS:
16447 /* In cases where an inlined instance of an inline function is passed
16448 the address of an `auto' variable (which is local to the caller) we
16449 can get a situation where the DECL_RTL of the artificial local
16450 variable (for the inlining) which acts as a stand-in for the
16451 corresponding formal parameter (of the inline function) will look
16452 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
16453 exactly a compile-time constant expression, but it isn't the address
16454 of the (artificial) local variable either. Rather, it represents the
16455 *value* which the artificial local variable always has during its
16456 lifetime. We currently have no way to represent such quasi-constant
16457 values in Dwarf, so for now we just punt and generate nothing. */
16458 return false;
16460 case HIGH:
16461 case CONST_FIXED:
16462 return false;
16464 case MEM:
16465 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16466 && MEM_READONLY_P (rtl)
16467 && GET_MODE (rtl) == BLKmode)
16469 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16470 return true;
16472 return false;
16474 default:
16475 /* No other kinds of rtx should be possible here. */
16476 gcc_unreachable ();
16478 return false;
16481 /* Determine whether the evaluation of EXPR references any variables
16482 or functions which aren't otherwise used (and therefore may not be
16483 output). */
16484 static tree
16485 reference_to_unused (tree * tp, int * walk_subtrees,
16486 void * data ATTRIBUTE_UNUSED)
16488 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16489 *walk_subtrees = 0;
16491 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16492 && ! TREE_ASM_WRITTEN (*tp))
16493 return *tp;
16494 /* ??? The C++ FE emits debug information for using decls, so
16495 putting gcc_unreachable here falls over. See PR31899. For now
16496 be conservative. */
16497 else if (!cgraph_global_info_ready
16498 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16499 return *tp;
16500 else if (TREE_CODE (*tp) == VAR_DECL)
16502 struct varpool_node *node = varpool_get_node (*tp);
16503 if (!node || !node->needed)
16504 return *tp;
16506 else if (TREE_CODE (*tp) == FUNCTION_DECL
16507 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16509 /* The call graph machinery must have finished analyzing,
16510 optimizing and gimplifying the CU by now.
16511 So if *TP has no call graph node associated
16512 to it, it means *TP will not be emitted. */
16513 if (!cgraph_get_node (*tp))
16514 return *tp;
16516 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16517 return *tp;
16519 return NULL_TREE;
16522 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16523 for use in a later add_const_value_attribute call. */
16525 static rtx
16526 rtl_for_decl_init (tree init, tree type)
16528 rtx rtl = NULL_RTX;
16530 STRIP_NOPS (init);
16532 /* If a variable is initialized with a string constant without embedded
16533 zeros, build CONST_STRING. */
16534 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16536 tree enttype = TREE_TYPE (type);
16537 tree domain = TYPE_DOMAIN (type);
16538 enum machine_mode mode = TYPE_MODE (enttype);
16540 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16541 && domain
16542 && integer_zerop (TYPE_MIN_VALUE (domain))
16543 && compare_tree_int (TYPE_MAX_VALUE (domain),
16544 TREE_STRING_LENGTH (init) - 1) == 0
16545 && ((size_t) TREE_STRING_LENGTH (init)
16546 == strlen (TREE_STRING_POINTER (init)) + 1))
16548 rtl = gen_rtx_CONST_STRING (VOIDmode,
16549 ggc_strdup (TREE_STRING_POINTER (init)));
16550 rtl = gen_rtx_MEM (BLKmode, rtl);
16551 MEM_READONLY_P (rtl) = 1;
16554 /* Other aggregates, and complex values, could be represented using
16555 CONCAT: FIXME! */
16556 else if (AGGREGATE_TYPE_P (type)
16557 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16558 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16559 || TREE_CODE (type) == COMPLEX_TYPE)
16561 /* Vectors only work if their mode is supported by the target.
16562 FIXME: generic vectors ought to work too. */
16563 else if (TREE_CODE (type) == VECTOR_TYPE
16564 && !VECTOR_MODE_P (TYPE_MODE (type)))
16566 /* If the initializer is something that we know will expand into an
16567 immediate RTL constant, expand it now. We must be careful not to
16568 reference variables which won't be output. */
16569 else if (initializer_constant_valid_p (init, type)
16570 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16572 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16573 possible. */
16574 if (TREE_CODE (type) == VECTOR_TYPE)
16575 switch (TREE_CODE (init))
16577 case VECTOR_CST:
16578 break;
16579 case CONSTRUCTOR:
16580 if (TREE_CONSTANT (init))
16582 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16583 bool constant_p = true;
16584 tree value;
16585 unsigned HOST_WIDE_INT ix;
16587 /* Even when ctor is constant, it might contain non-*_CST
16588 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16589 belong into VECTOR_CST nodes. */
16590 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16591 if (!CONSTANT_CLASS_P (value))
16593 constant_p = false;
16594 break;
16597 if (constant_p)
16599 init = build_vector_from_ctor (type, elts);
16600 break;
16603 /* FALLTHRU */
16605 default:
16606 return NULL;
16609 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16611 /* If expand_expr returns a MEM, it wasn't immediate. */
16612 gcc_assert (!rtl || !MEM_P (rtl));
16615 return rtl;
16618 /* Generate RTL for the variable DECL to represent its location. */
16620 static rtx
16621 rtl_for_decl_location (tree decl)
16623 rtx rtl;
16625 /* Here we have to decide where we are going to say the parameter "lives"
16626 (as far as the debugger is concerned). We only have a couple of
16627 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16629 DECL_RTL normally indicates where the parameter lives during most of the
16630 activation of the function. If optimization is enabled however, this
16631 could be either NULL or else a pseudo-reg. Both of those cases indicate
16632 that the parameter doesn't really live anywhere (as far as the code
16633 generation parts of GCC are concerned) during most of the function's
16634 activation. That will happen (for example) if the parameter is never
16635 referenced within the function.
16637 We could just generate a location descriptor here for all non-NULL
16638 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16639 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16640 where DECL_RTL is NULL or is a pseudo-reg.
16642 Note however that we can only get away with using DECL_INCOMING_RTL as
16643 a backup substitute for DECL_RTL in certain limited cases. In cases
16644 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16645 we can be sure that the parameter was passed using the same type as it is
16646 declared to have within the function, and that its DECL_INCOMING_RTL
16647 points us to a place where a value of that type is passed.
16649 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16650 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16651 because in these cases DECL_INCOMING_RTL points us to a value of some
16652 type which is *different* from the type of the parameter itself. Thus,
16653 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16654 such cases, the debugger would end up (for example) trying to fetch a
16655 `float' from a place which actually contains the first part of a
16656 `double'. That would lead to really incorrect and confusing
16657 output at debug-time.
16659 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16660 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
16661 are a couple of exceptions however. On little-endian machines we can
16662 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16663 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16664 an integral type that is smaller than TREE_TYPE (decl). These cases arise
16665 when (on a little-endian machine) a non-prototyped function has a
16666 parameter declared to be of type `short' or `char'. In such cases,
16667 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16668 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16669 passed `int' value. If the debugger then uses that address to fetch
16670 a `short' or a `char' (on a little-endian machine) the result will be
16671 the correct data, so we allow for such exceptional cases below.
16673 Note that our goal here is to describe the place where the given formal
16674 parameter lives during most of the function's activation (i.e. between the
16675 end of the prologue and the start of the epilogue). We'll do that as best
16676 as we can. Note however that if the given formal parameter is modified
16677 sometime during the execution of the function, then a stack backtrace (at
16678 debug-time) will show the function as having been called with the *new*
16679 value rather than the value which was originally passed in. This happens
16680 rarely enough that it is not a major problem, but it *is* a problem, and
16681 I'd like to fix it.
16683 A future version of dwarf2out.c may generate two additional attributes for
16684 any given DW_TAG_formal_parameter DIE which will describe the "passed
16685 type" and the "passed location" for the given formal parameter in addition
16686 to the attributes we now generate to indicate the "declared type" and the
16687 "active location" for each parameter. This additional set of attributes
16688 could be used by debuggers for stack backtraces. Separately, note that
16689 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16690 This happens (for example) for inlined-instances of inline function formal
16691 parameters which are never referenced. This really shouldn't be
16692 happening. All PARM_DECL nodes should get valid non-NULL
16693 DECL_INCOMING_RTL values. FIXME. */
16695 /* Use DECL_RTL as the "location" unless we find something better. */
16696 rtl = DECL_RTL_IF_SET (decl);
16698 /* When generating abstract instances, ignore everything except
16699 constants, symbols living in memory, and symbols living in
16700 fixed registers. */
16701 if (! reload_completed)
16703 if (rtl
16704 && (CONSTANT_P (rtl)
16705 || (MEM_P (rtl)
16706 && CONSTANT_P (XEXP (rtl, 0)))
16707 || (REG_P (rtl)
16708 && TREE_CODE (decl) == VAR_DECL
16709 && TREE_STATIC (decl))))
16711 rtl = targetm.delegitimize_address (rtl);
16712 return rtl;
16714 rtl = NULL_RTX;
16716 else if (TREE_CODE (decl) == PARM_DECL)
16718 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16720 tree declared_type = TREE_TYPE (decl);
16721 tree passed_type = DECL_ARG_TYPE (decl);
16722 enum machine_mode dmode = TYPE_MODE (declared_type);
16723 enum machine_mode pmode = TYPE_MODE (passed_type);
16725 /* This decl represents a formal parameter which was optimized out.
16726 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16727 all cases where (rtl == NULL_RTX) just below. */
16728 if (dmode == pmode)
16729 rtl = DECL_INCOMING_RTL (decl);
16730 else if (SCALAR_INT_MODE_P (dmode)
16731 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16732 && DECL_INCOMING_RTL (decl))
16734 rtx inc = DECL_INCOMING_RTL (decl);
16735 if (REG_P (inc))
16736 rtl = inc;
16737 else if (MEM_P (inc))
16739 if (BYTES_BIG_ENDIAN)
16740 rtl = adjust_address_nv (inc, dmode,
16741 GET_MODE_SIZE (pmode)
16742 - GET_MODE_SIZE (dmode));
16743 else
16744 rtl = inc;
16749 /* If the parm was passed in registers, but lives on the stack, then
16750 make a big endian correction if the mode of the type of the
16751 parameter is not the same as the mode of the rtl. */
16752 /* ??? This is the same series of checks that are made in dbxout.c before
16753 we reach the big endian correction code there. It isn't clear if all
16754 of these checks are necessary here, but keeping them all is the safe
16755 thing to do. */
16756 else if (MEM_P (rtl)
16757 && XEXP (rtl, 0) != const0_rtx
16758 && ! CONSTANT_P (XEXP (rtl, 0))
16759 /* Not passed in memory. */
16760 && !MEM_P (DECL_INCOMING_RTL (decl))
16761 /* Not passed by invisible reference. */
16762 && (!REG_P (XEXP (rtl, 0))
16763 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16764 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16765 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16766 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16767 #endif
16769 /* Big endian correction check. */
16770 && BYTES_BIG_ENDIAN
16771 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16772 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16773 < UNITS_PER_WORD))
16775 int offset = (UNITS_PER_WORD
16776 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16778 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16779 plus_constant (XEXP (rtl, 0), offset));
16782 else if (TREE_CODE (decl) == VAR_DECL
16783 && rtl
16784 && MEM_P (rtl)
16785 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16786 && BYTES_BIG_ENDIAN)
16788 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16789 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16791 /* If a variable is declared "register" yet is smaller than
16792 a register, then if we store the variable to memory, it
16793 looks like we're storing a register-sized value, when in
16794 fact we are not. We need to adjust the offset of the
16795 storage location to reflect the actual value's bytes,
16796 else gdb will not be able to display it. */
16797 if (rsize > dsize)
16798 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16799 plus_constant (XEXP (rtl, 0), rsize-dsize));
16802 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16803 and will have been substituted directly into all expressions that use it.
16804 C does not have such a concept, but C++ and other languages do. */
16805 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16806 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16808 if (rtl)
16809 rtl = targetm.delegitimize_address (rtl);
16811 /* If we don't look past the constant pool, we risk emitting a
16812 reference to a constant pool entry that isn't referenced from
16813 code, and thus is not emitted. */
16814 if (rtl)
16815 rtl = avoid_constant_pool_reference (rtl);
16817 /* Try harder to get a rtl. If this symbol ends up not being emitted
16818 in the current CU, resolve_addr will remove the expression referencing
16819 it. */
16820 if (rtl == NULL_RTX
16821 && TREE_CODE (decl) == VAR_DECL
16822 && !DECL_EXTERNAL (decl)
16823 && TREE_STATIC (decl)
16824 && DECL_NAME (decl)
16825 && !DECL_HARD_REGISTER (decl)
16826 && DECL_MODE (decl) != VOIDmode)
16828 rtl = make_decl_rtl_for_debug (decl);
16829 if (!MEM_P (rtl)
16830 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16831 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16832 rtl = NULL_RTX;
16835 return rtl;
16838 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16839 returned. If so, the decl for the COMMON block is returned, and the
16840 value is the offset into the common block for the symbol. */
16842 static tree
16843 fortran_common (tree decl, HOST_WIDE_INT *value)
16845 tree val_expr, cvar;
16846 enum machine_mode mode;
16847 HOST_WIDE_INT bitsize, bitpos;
16848 tree offset;
16849 int volatilep = 0, unsignedp = 0;
16851 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16852 it does not have a value (the offset into the common area), or if it
16853 is thread local (as opposed to global) then it isn't common, and shouldn't
16854 be handled as such. */
16855 if (TREE_CODE (decl) != VAR_DECL
16856 || !TREE_STATIC (decl)
16857 || !DECL_HAS_VALUE_EXPR_P (decl)
16858 || !is_fortran ())
16859 return NULL_TREE;
16861 val_expr = DECL_VALUE_EXPR (decl);
16862 if (TREE_CODE (val_expr) != COMPONENT_REF)
16863 return NULL_TREE;
16865 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16866 &mode, &unsignedp, &volatilep, true);
16868 if (cvar == NULL_TREE
16869 || TREE_CODE (cvar) != VAR_DECL
16870 || DECL_ARTIFICIAL (cvar)
16871 || !TREE_PUBLIC (cvar))
16872 return NULL_TREE;
16874 *value = 0;
16875 if (offset != NULL)
16877 if (!host_integerp (offset, 0))
16878 return NULL_TREE;
16879 *value = tree_low_cst (offset, 0);
16881 if (bitpos != 0)
16882 *value += bitpos / BITS_PER_UNIT;
16884 return cvar;
16887 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16888 data attribute for a variable or a parameter. We generate the
16889 DW_AT_const_value attribute only in those cases where the given variable
16890 or parameter does not have a true "location" either in memory or in a
16891 register. This can happen (for example) when a constant is passed as an
16892 actual argument in a call to an inline function. (It's possible that
16893 these things can crop up in other ways also.) Note that one type of
16894 constant value which can be passed into an inlined function is a constant
16895 pointer. This can happen for example if an actual argument in an inlined
16896 function call evaluates to a compile-time constant address. */
16898 static bool
16899 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16900 enum dwarf_attribute attr)
16902 rtx rtl;
16903 dw_loc_list_ref list;
16904 var_loc_list *loc_list;
16906 if (TREE_CODE (decl) == ERROR_MARK)
16907 return false;
16909 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16910 || TREE_CODE (decl) == RESULT_DECL);
16912 /* Try to get some constant RTL for this decl, and use that as the value of
16913 the location. */
16915 rtl = rtl_for_decl_location (decl);
16916 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16917 && add_const_value_attribute (die, rtl))
16918 return true;
16920 /* See if we have single element location list that is equivalent to
16921 a constant value. That way we are better to use add_const_value_attribute
16922 rather than expanding constant value equivalent. */
16923 loc_list = lookup_decl_loc (decl);
16924 if (loc_list
16925 && loc_list->first
16926 && loc_list->first->next == NULL
16927 && NOTE_P (loc_list->first->loc)
16928 && NOTE_VAR_LOCATION (loc_list->first->loc)
16929 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16931 struct var_loc_node *node;
16933 node = loc_list->first;
16934 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16935 if (GET_CODE (rtl) == EXPR_LIST)
16936 rtl = XEXP (rtl, 0);
16937 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16938 && add_const_value_attribute (die, rtl))
16939 return true;
16941 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16942 if (list)
16944 add_AT_location_description (die, attr, list);
16945 return true;
16947 /* None of that worked, so it must not really have a location;
16948 try adding a constant value attribute from the DECL_INITIAL. */
16949 return tree_add_const_value_attribute_for_decl (die, decl);
16952 /* Add VARIABLE and DIE into deferred locations list. */
16954 static void
16955 defer_location (tree variable, dw_die_ref die)
16957 deferred_locations entry;
16958 entry.variable = variable;
16959 entry.die = die;
16960 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16963 /* Helper function for tree_add_const_value_attribute. Natively encode
16964 initializer INIT into an array. Return true if successful. */
16966 static bool
16967 native_encode_initializer (tree init, unsigned char *array, int size)
16969 tree type;
16971 if (init == NULL_TREE)
16972 return false;
16974 STRIP_NOPS (init);
16975 switch (TREE_CODE (init))
16977 case STRING_CST:
16978 type = TREE_TYPE (init);
16979 if (TREE_CODE (type) == ARRAY_TYPE)
16981 tree enttype = TREE_TYPE (type);
16982 enum machine_mode mode = TYPE_MODE (enttype);
16984 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16985 return false;
16986 if (int_size_in_bytes (type) != size)
16987 return false;
16988 if (size > TREE_STRING_LENGTH (init))
16990 memcpy (array, TREE_STRING_POINTER (init),
16991 TREE_STRING_LENGTH (init));
16992 memset (array + TREE_STRING_LENGTH (init),
16993 '\0', size - TREE_STRING_LENGTH (init));
16995 else
16996 memcpy (array, TREE_STRING_POINTER (init), size);
16997 return true;
16999 return false;
17000 case CONSTRUCTOR:
17001 type = TREE_TYPE (init);
17002 if (int_size_in_bytes (type) != size)
17003 return false;
17004 if (TREE_CODE (type) == ARRAY_TYPE)
17006 HOST_WIDE_INT min_index;
17007 unsigned HOST_WIDE_INT cnt;
17008 int curpos = 0, fieldsize;
17009 constructor_elt *ce;
17011 if (TYPE_DOMAIN (type) == NULL_TREE
17012 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17013 return false;
17015 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17016 if (fieldsize <= 0)
17017 return false;
17019 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17020 memset (array, '\0', size);
17021 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17023 tree val = ce->value;
17024 tree index = ce->index;
17025 int pos = curpos;
17026 if (index && TREE_CODE (index) == RANGE_EXPR)
17027 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17028 * fieldsize;
17029 else if (index)
17030 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17032 if (val)
17034 STRIP_NOPS (val);
17035 if (!native_encode_initializer (val, array + pos, fieldsize))
17036 return false;
17038 curpos = pos + fieldsize;
17039 if (index && TREE_CODE (index) == RANGE_EXPR)
17041 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17042 - tree_low_cst (TREE_OPERAND (index, 0), 0);
17043 while (count > 0)
17045 if (val)
17046 memcpy (array + curpos, array + pos, fieldsize);
17047 curpos += fieldsize;
17050 gcc_assert (curpos <= size);
17052 return true;
17054 else if (TREE_CODE (type) == RECORD_TYPE
17055 || TREE_CODE (type) == UNION_TYPE)
17057 tree field = NULL_TREE;
17058 unsigned HOST_WIDE_INT cnt;
17059 constructor_elt *ce;
17061 if (int_size_in_bytes (type) != size)
17062 return false;
17064 if (TREE_CODE (type) == RECORD_TYPE)
17065 field = TYPE_FIELDS (type);
17067 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17069 tree val = ce->value;
17070 int pos, fieldsize;
17072 if (ce->index != 0)
17073 field = ce->index;
17075 if (val)
17076 STRIP_NOPS (val);
17078 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17079 return false;
17081 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17082 && TYPE_DOMAIN (TREE_TYPE (field))
17083 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17084 return false;
17085 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17086 || !host_integerp (DECL_SIZE_UNIT (field), 0))
17087 return false;
17088 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17089 pos = int_byte_position (field);
17090 gcc_assert (pos + fieldsize <= size);
17091 if (val
17092 && !native_encode_initializer (val, array + pos, fieldsize))
17093 return false;
17095 return true;
17097 return false;
17098 case VIEW_CONVERT_EXPR:
17099 case NON_LVALUE_EXPR:
17100 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17101 default:
17102 return native_encode_expr (init, array, size) == size;
17106 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17107 attribute is the const value T. */
17109 static bool
17110 tree_add_const_value_attribute (dw_die_ref die, tree t)
17112 tree init;
17113 tree type = TREE_TYPE (t);
17114 rtx rtl;
17116 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17117 return false;
17119 init = t;
17120 gcc_assert (!DECL_P (init));
17122 rtl = rtl_for_decl_init (init, type);
17123 if (rtl)
17124 return add_const_value_attribute (die, rtl);
17125 /* If the host and target are sane, try harder. */
17126 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17127 && initializer_constant_valid_p (init, type))
17129 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17130 if (size > 0 && (int) size == size)
17132 unsigned char *array = (unsigned char *)
17133 ggc_alloc_cleared_atomic (size);
17135 if (native_encode_initializer (init, array, size))
17137 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17138 return true;
17142 return false;
17145 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17146 attribute is the const value of T, where T is an integral constant
17147 variable with static storage duration
17148 (so it can't be a PARM_DECL or a RESULT_DECL). */
17150 static bool
17151 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17154 if (!decl
17155 || (TREE_CODE (decl) != VAR_DECL
17156 && TREE_CODE (decl) != CONST_DECL))
17157 return false;
17159 if (TREE_READONLY (decl)
17160 && ! TREE_THIS_VOLATILE (decl)
17161 && DECL_INITIAL (decl))
17162 /* OK */;
17163 else
17164 return false;
17166 /* Don't add DW_AT_const_value if abstract origin already has one. */
17167 if (get_AT (var_die, DW_AT_const_value))
17168 return false;
17170 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17173 /* Convert the CFI instructions for the current function into a
17174 location list. This is used for DW_AT_frame_base when we targeting
17175 a dwarf2 consumer that does not support the dwarf3
17176 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17177 expressions. */
17179 static dw_loc_list_ref
17180 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17182 dw_fde_ref fde;
17183 dw_loc_list_ref list, *list_tail;
17184 dw_cfi_ref cfi;
17185 dw_cfa_location last_cfa, next_cfa;
17186 const char *start_label, *last_label, *section;
17187 dw_cfa_location remember;
17189 fde = current_fde ();
17190 gcc_assert (fde != NULL);
17192 section = secname_for_decl (current_function_decl);
17193 list_tail = &list;
17194 list = NULL;
17196 memset (&next_cfa, 0, sizeof (next_cfa));
17197 next_cfa.reg = INVALID_REGNUM;
17198 remember = next_cfa;
17200 start_label = fde->dw_fde_begin;
17202 /* ??? Bald assumption that the CIE opcode list does not contain
17203 advance opcodes. */
17204 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17205 lookup_cfa_1 (cfi, &next_cfa, &remember);
17207 last_cfa = next_cfa;
17208 last_label = start_label;
17210 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17211 switch (cfi->dw_cfi_opc)
17213 case DW_CFA_set_loc:
17214 case DW_CFA_advance_loc1:
17215 case DW_CFA_advance_loc2:
17216 case DW_CFA_advance_loc4:
17217 if (!cfa_equal_p (&last_cfa, &next_cfa))
17219 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17220 start_label, last_label, section);
17222 list_tail = &(*list_tail)->dw_loc_next;
17223 last_cfa = next_cfa;
17224 start_label = last_label;
17226 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17227 break;
17229 case DW_CFA_advance_loc:
17230 /* The encoding is complex enough that we should never emit this. */
17231 gcc_unreachable ();
17233 default:
17234 lookup_cfa_1 (cfi, &next_cfa, &remember);
17235 break;
17238 if (!cfa_equal_p (&last_cfa, &next_cfa))
17240 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17241 start_label, last_label, section);
17242 list_tail = &(*list_tail)->dw_loc_next;
17243 start_label = last_label;
17246 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17247 start_label, fde->dw_fde_end, section);
17249 if (list && list->dw_loc_next)
17250 gen_llsym (list);
17252 return list;
17255 /* Compute a displacement from the "steady-state frame pointer" to the
17256 frame base (often the same as the CFA), and store it in
17257 frame_pointer_fb_offset. OFFSET is added to the displacement
17258 before the latter is negated. */
17260 static void
17261 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17263 rtx reg, elim;
17265 #ifdef FRAME_POINTER_CFA_OFFSET
17266 reg = frame_pointer_rtx;
17267 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17268 #else
17269 reg = arg_pointer_rtx;
17270 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17271 #endif
17273 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17274 if (GET_CODE (elim) == PLUS)
17276 offset += INTVAL (XEXP (elim, 1));
17277 elim = XEXP (elim, 0);
17280 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17281 && (elim == hard_frame_pointer_rtx
17282 || elim == stack_pointer_rtx))
17283 || elim == (frame_pointer_needed
17284 ? hard_frame_pointer_rtx
17285 : stack_pointer_rtx));
17287 frame_pointer_fb_offset = -offset;
17290 /* Generate a DW_AT_name attribute given some string value to be included as
17291 the value of the attribute. */
17293 static void
17294 add_name_attribute (dw_die_ref die, const char *name_string)
17296 if (name_string != NULL && *name_string != 0)
17298 if (demangle_name_func)
17299 name_string = (*demangle_name_func) (name_string);
17301 add_AT_string (die, DW_AT_name, name_string);
17305 /* Generate a DW_AT_comp_dir attribute for DIE. */
17307 static void
17308 add_comp_dir_attribute (dw_die_ref die)
17310 const char *wd = get_src_pwd ();
17311 char *wd1;
17313 if (wd == NULL)
17314 return;
17316 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17318 int wdlen;
17320 wdlen = strlen (wd);
17321 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17322 strcpy (wd1, wd);
17323 wd1 [wdlen] = DIR_SEPARATOR;
17324 wd1 [wdlen + 1] = 0;
17325 wd = wd1;
17328 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17331 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17332 default. */
17334 static int
17335 lower_bound_default (void)
17337 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17339 case DW_LANG_C:
17340 case DW_LANG_C89:
17341 case DW_LANG_C99:
17342 case DW_LANG_C_plus_plus:
17343 case DW_LANG_ObjC:
17344 case DW_LANG_ObjC_plus_plus:
17345 case DW_LANG_Java:
17346 return 0;
17347 case DW_LANG_Fortran77:
17348 case DW_LANG_Fortran90:
17349 case DW_LANG_Fortran95:
17350 return 1;
17351 case DW_LANG_UPC:
17352 case DW_LANG_D:
17353 case DW_LANG_Python:
17354 return dwarf_version >= 4 ? 0 : -1;
17355 case DW_LANG_Ada95:
17356 case DW_LANG_Ada83:
17357 case DW_LANG_Cobol74:
17358 case DW_LANG_Cobol85:
17359 case DW_LANG_Pascal83:
17360 case DW_LANG_Modula2:
17361 case DW_LANG_PLI:
17362 return dwarf_version >= 4 ? 1 : -1;
17363 default:
17364 return -1;
17368 /* Given a tree node describing an array bound (either lower or upper) output
17369 a representation for that bound. */
17371 static void
17372 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17374 switch (TREE_CODE (bound))
17376 case ERROR_MARK:
17377 return;
17379 /* All fixed-bounds are represented by INTEGER_CST nodes. */
17380 case INTEGER_CST:
17382 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17383 int dflt;
17385 /* Use the default if possible. */
17386 if (bound_attr == DW_AT_lower_bound
17387 && host_integerp (bound, 0)
17388 && (dflt = lower_bound_default ()) != -1
17389 && tree_low_cst (bound, 0) == dflt)
17392 /* Otherwise represent the bound as an unsigned value with the
17393 precision of its type. The precision and signedness of the
17394 type will be necessary to re-interpret it unambiguously. */
17395 else if (prec < HOST_BITS_PER_WIDE_INT)
17397 unsigned HOST_WIDE_INT mask
17398 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17399 add_AT_unsigned (subrange_die, bound_attr,
17400 TREE_INT_CST_LOW (bound) & mask);
17402 else if (prec == HOST_BITS_PER_WIDE_INT
17403 || TREE_INT_CST_HIGH (bound) == 0)
17404 add_AT_unsigned (subrange_die, bound_attr,
17405 TREE_INT_CST_LOW (bound));
17406 else
17407 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17408 TREE_INT_CST_LOW (bound));
17410 break;
17412 CASE_CONVERT:
17413 case VIEW_CONVERT_EXPR:
17414 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17415 break;
17417 case SAVE_EXPR:
17418 break;
17420 case VAR_DECL:
17421 case PARM_DECL:
17422 case RESULT_DECL:
17424 dw_die_ref decl_die = lookup_decl_die (bound);
17426 /* ??? Can this happen, or should the variable have been bound
17427 first? Probably it can, since I imagine that we try to create
17428 the types of parameters in the order in which they exist in
17429 the list, and won't have created a forward reference to a
17430 later parameter. */
17431 if (decl_die != NULL)
17433 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17434 break;
17437 /* FALLTHRU */
17439 default:
17441 /* Otherwise try to create a stack operation procedure to
17442 evaluate the value of the array bound. */
17444 dw_die_ref ctx, decl_die;
17445 dw_loc_list_ref list;
17447 list = loc_list_from_tree (bound, 2);
17448 if (list == NULL || single_element_loc_list_p (list))
17450 /* If DW_AT_*bound is not a reference nor constant, it is
17451 a DWARF expression rather than location description.
17452 For that loc_list_from_tree (bound, 0) is needed.
17453 If that fails to give a single element list,
17454 fall back to outputting this as a reference anyway. */
17455 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17456 if (list2 && single_element_loc_list_p (list2))
17458 add_AT_loc (subrange_die, bound_attr, list2->expr);
17459 break;
17462 if (list == NULL)
17463 break;
17465 if (current_function_decl == 0)
17466 ctx = comp_unit_die ();
17467 else
17468 ctx = lookup_decl_die (current_function_decl);
17470 decl_die = new_die (DW_TAG_variable, ctx, bound);
17471 add_AT_flag (decl_die, DW_AT_artificial, 1);
17472 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17473 add_AT_location_description (decl_die, DW_AT_location, list);
17474 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17475 break;
17480 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17481 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17482 Note that the block of subscript information for an array type also
17483 includes information about the element type of the given array type. */
17485 static void
17486 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17488 unsigned dimension_number;
17489 tree lower, upper;
17490 dw_die_ref subrange_die;
17492 for (dimension_number = 0;
17493 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17494 type = TREE_TYPE (type), dimension_number++)
17496 tree domain = TYPE_DOMAIN (type);
17498 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17499 break;
17501 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17502 and (in GNU C only) variable bounds. Handle all three forms
17503 here. */
17504 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17505 if (domain)
17507 /* We have an array type with specified bounds. */
17508 lower = TYPE_MIN_VALUE (domain);
17509 upper = TYPE_MAX_VALUE (domain);
17511 /* Define the index type. */
17512 if (TREE_TYPE (domain))
17514 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17515 TREE_TYPE field. We can't emit debug info for this
17516 because it is an unnamed integral type. */
17517 if (TREE_CODE (domain) == INTEGER_TYPE
17518 && TYPE_NAME (domain) == NULL_TREE
17519 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17520 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17522 else
17523 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17524 type_die);
17527 /* ??? If upper is NULL, the array has unspecified length,
17528 but it does have a lower bound. This happens with Fortran
17529 dimension arr(N:*)
17530 Since the debugger is definitely going to need to know N
17531 to produce useful results, go ahead and output the lower
17532 bound solo, and hope the debugger can cope. */
17534 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17535 if (upper)
17536 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17539 /* Otherwise we have an array type with an unspecified length. The
17540 DWARF-2 spec does not say how to handle this; let's just leave out the
17541 bounds. */
17545 static void
17546 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17548 unsigned size;
17550 switch (TREE_CODE (tree_node))
17552 case ERROR_MARK:
17553 size = 0;
17554 break;
17555 case ENUMERAL_TYPE:
17556 case RECORD_TYPE:
17557 case UNION_TYPE:
17558 case QUAL_UNION_TYPE:
17559 size = int_size_in_bytes (tree_node);
17560 break;
17561 case FIELD_DECL:
17562 /* For a data member of a struct or union, the DW_AT_byte_size is
17563 generally given as the number of bytes normally allocated for an
17564 object of the *declared* type of the member itself. This is true
17565 even for bit-fields. */
17566 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17567 break;
17568 default:
17569 gcc_unreachable ();
17572 /* Note that `size' might be -1 when we get to this point. If it is, that
17573 indicates that the byte size of the entity in question is variable. We
17574 have no good way of expressing this fact in Dwarf at the present time,
17575 so just let the -1 pass on through. */
17576 add_AT_unsigned (die, DW_AT_byte_size, size);
17579 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17580 which specifies the distance in bits from the highest order bit of the
17581 "containing object" for the bit-field to the highest order bit of the
17582 bit-field itself.
17584 For any given bit-field, the "containing object" is a hypothetical object
17585 (of some integral or enum type) within which the given bit-field lives. The
17586 type of this hypothetical "containing object" is always the same as the
17587 declared type of the individual bit-field itself. The determination of the
17588 exact location of the "containing object" for a bit-field is rather
17589 complicated. It's handled by the `field_byte_offset' function (above).
17591 Note that it is the size (in bytes) of the hypothetical "containing object"
17592 which will be given in the DW_AT_byte_size attribute for this bit-field.
17593 (See `byte_size_attribute' above). */
17595 static inline void
17596 add_bit_offset_attribute (dw_die_ref die, tree decl)
17598 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17599 tree type = DECL_BIT_FIELD_TYPE (decl);
17600 HOST_WIDE_INT bitpos_int;
17601 HOST_WIDE_INT highest_order_object_bit_offset;
17602 HOST_WIDE_INT highest_order_field_bit_offset;
17603 HOST_WIDE_INT unsigned bit_offset;
17605 /* Must be a field and a bit field. */
17606 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17608 /* We can't yet handle bit-fields whose offsets are variable, so if we
17609 encounter such things, just return without generating any attribute
17610 whatsoever. Likewise for variable or too large size. */
17611 if (! host_integerp (bit_position (decl), 0)
17612 || ! host_integerp (DECL_SIZE (decl), 1))
17613 return;
17615 bitpos_int = int_bit_position (decl);
17617 /* Note that the bit offset is always the distance (in bits) from the
17618 highest-order bit of the "containing object" to the highest-order bit of
17619 the bit-field itself. Since the "high-order end" of any object or field
17620 is different on big-endian and little-endian machines, the computation
17621 below must take account of these differences. */
17622 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17623 highest_order_field_bit_offset = bitpos_int;
17625 if (! BYTES_BIG_ENDIAN)
17627 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17628 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17631 bit_offset
17632 = (! BYTES_BIG_ENDIAN
17633 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17634 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17636 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17639 /* For a FIELD_DECL node which represents a bit field, output an attribute
17640 which specifies the length in bits of the given field. */
17642 static inline void
17643 add_bit_size_attribute (dw_die_ref die, tree decl)
17645 /* Must be a field and a bit field. */
17646 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17647 && DECL_BIT_FIELD_TYPE (decl));
17649 if (host_integerp (DECL_SIZE (decl), 1))
17650 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17653 /* If the compiled language is ANSI C, then add a 'prototyped'
17654 attribute, if arg types are given for the parameters of a function. */
17656 static inline void
17657 add_prototyped_attribute (dw_die_ref die, tree func_type)
17659 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17660 && prototype_p (func_type))
17661 add_AT_flag (die, DW_AT_prototyped, 1);
17664 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17665 by looking in either the type declaration or object declaration
17666 equate table. */
17668 static inline dw_die_ref
17669 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17671 dw_die_ref origin_die = NULL;
17673 if (TREE_CODE (origin) != FUNCTION_DECL)
17675 /* We may have gotten separated from the block for the inlined
17676 function, if we're in an exception handler or some such; make
17677 sure that the abstract function has been written out.
17679 Doing this for nested functions is wrong, however; functions are
17680 distinct units, and our context might not even be inline. */
17681 tree fn = origin;
17683 if (TYPE_P (fn))
17684 fn = TYPE_STUB_DECL (fn);
17686 fn = decl_function_context (fn);
17687 if (fn)
17688 dwarf2out_abstract_function (fn);
17691 if (DECL_P (origin))
17692 origin_die = lookup_decl_die (origin);
17693 else if (TYPE_P (origin))
17694 origin_die = lookup_type_die (origin);
17696 /* XXX: Functions that are never lowered don't always have correct block
17697 trees (in the case of java, they simply have no block tree, in some other
17698 languages). For these functions, there is nothing we can really do to
17699 output correct debug info for inlined functions in all cases. Rather
17700 than die, we'll just produce deficient debug info now, in that we will
17701 have variables without a proper abstract origin. In the future, when all
17702 functions are lowered, we should re-add a gcc_assert (origin_die)
17703 here. */
17705 if (origin_die)
17706 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17707 return origin_die;
17710 /* We do not currently support the pure_virtual attribute. */
17712 static inline void
17713 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17715 if (DECL_VINDEX (func_decl))
17717 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17719 if (host_integerp (DECL_VINDEX (func_decl), 0))
17720 add_AT_loc (die, DW_AT_vtable_elem_location,
17721 new_loc_descr (DW_OP_constu,
17722 tree_low_cst (DECL_VINDEX (func_decl), 0),
17723 0));
17725 /* GNU extension: Record what type this method came from originally. */
17726 if (debug_info_level > DINFO_LEVEL_TERSE
17727 && DECL_CONTEXT (func_decl))
17728 add_AT_die_ref (die, DW_AT_containing_type,
17729 lookup_type_die (DECL_CONTEXT (func_decl)));
17733 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17734 given decl. This used to be a vendor extension until after DWARF 4
17735 standardized it. */
17737 static void
17738 add_linkage_attr (dw_die_ref die, tree decl)
17740 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17742 /* Mimic what assemble_name_raw does with a leading '*'. */
17743 if (name[0] == '*')
17744 name = &name[1];
17746 if (dwarf_version >= 4)
17747 add_AT_string (die, DW_AT_linkage_name, name);
17748 else
17749 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17752 /* Add source coordinate attributes for the given decl. */
17754 static void
17755 add_src_coords_attributes (dw_die_ref die, tree decl)
17757 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17759 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17760 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17763 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17765 static void
17766 add_linkage_name (dw_die_ref die, tree decl)
17768 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17769 && TREE_PUBLIC (decl)
17770 && !DECL_ABSTRACT (decl)
17771 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17772 && die->die_tag != DW_TAG_member)
17774 /* Defer until we have an assembler name set. */
17775 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17777 limbo_die_node *asm_name;
17779 asm_name = ggc_alloc_cleared_limbo_die_node ();
17780 asm_name->die = die;
17781 asm_name->created_for = decl;
17782 asm_name->next = deferred_asm_name;
17783 deferred_asm_name = asm_name;
17785 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17786 add_linkage_attr (die, decl);
17790 /* Add a DW_AT_name attribute and source coordinate attribute for the
17791 given decl, but only if it actually has a name. */
17793 static void
17794 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17796 tree decl_name;
17798 decl_name = DECL_NAME (decl);
17799 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17801 const char *name = dwarf2_name (decl, 0);
17802 if (name)
17803 add_name_attribute (die, name);
17804 if (! DECL_ARTIFICIAL (decl))
17805 add_src_coords_attributes (die, decl);
17807 add_linkage_name (die, decl);
17810 #ifdef VMS_DEBUGGING_INFO
17811 /* Get the function's name, as described by its RTL. This may be different
17812 from the DECL_NAME name used in the source file. */
17813 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17815 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17816 XEXP (DECL_RTL (decl), 0));
17817 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17819 #endif /* VMS_DEBUGGING_INFO */
17822 #ifdef VMS_DEBUGGING_INFO
17823 /* Output the debug main pointer die for VMS */
17825 void
17826 dwarf2out_vms_debug_main_pointer (void)
17828 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17829 dw_die_ref die;
17831 /* Allocate the VMS debug main subprogram die. */
17832 die = ggc_alloc_cleared_die_node ();
17833 die->die_tag = DW_TAG_subprogram;
17834 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17835 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17836 current_function_funcdef_no);
17837 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17839 /* Make it the first child of comp_unit_die (). */
17840 die->die_parent = comp_unit_die ();
17841 if (comp_unit_die ()->die_child)
17843 die->die_sib = comp_unit_die ()->die_child->die_sib;
17844 comp_unit_die ()->die_child->die_sib = die;
17846 else
17848 die->die_sib = die;
17849 comp_unit_die ()->die_child = die;
17852 #endif /* VMS_DEBUGGING_INFO */
17854 /* Push a new declaration scope. */
17856 static void
17857 push_decl_scope (tree scope)
17859 VEC_safe_push (tree, gc, decl_scope_table, scope);
17862 /* Pop a declaration scope. */
17864 static inline void
17865 pop_decl_scope (void)
17867 VEC_pop (tree, decl_scope_table);
17870 /* Return the DIE for the scope that immediately contains this type.
17871 Non-named types get global scope. Named types nested in other
17872 types get their containing scope if it's open, or global scope
17873 otherwise. All other types (i.e. function-local named types) get
17874 the current active scope. */
17876 static dw_die_ref
17877 scope_die_for (tree t, dw_die_ref context_die)
17879 dw_die_ref scope_die = NULL;
17880 tree containing_scope;
17881 int i;
17883 /* Non-types always go in the current scope. */
17884 gcc_assert (TYPE_P (t));
17886 containing_scope = TYPE_CONTEXT (t);
17888 /* Use the containing namespace if it was passed in (for a declaration). */
17889 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17891 if (context_die == lookup_decl_die (containing_scope))
17892 /* OK */;
17893 else
17894 containing_scope = NULL_TREE;
17897 /* Ignore function type "scopes" from the C frontend. They mean that
17898 a tagged type is local to a parmlist of a function declarator, but
17899 that isn't useful to DWARF. */
17900 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17901 containing_scope = NULL_TREE;
17903 if (SCOPE_FILE_SCOPE_P (containing_scope))
17904 scope_die = comp_unit_die ();
17905 else if (TYPE_P (containing_scope))
17907 /* For types, we can just look up the appropriate DIE. But
17908 first we check to see if we're in the middle of emitting it
17909 so we know where the new DIE should go. */
17910 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17911 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17912 break;
17914 if (i < 0)
17916 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17917 || TREE_ASM_WRITTEN (containing_scope));
17918 /*We are not in the middle of emitting the type
17919 CONTAINING_SCOPE. Let's see if it's emitted already. */
17920 scope_die = lookup_type_die (containing_scope);
17922 /* If none of the current dies are suitable, we get file scope. */
17923 if (scope_die == NULL)
17924 scope_die = comp_unit_die ();
17926 else
17927 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17929 else
17930 scope_die = context_die;
17932 return scope_die;
17935 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17937 static inline int
17938 local_scope_p (dw_die_ref context_die)
17940 for (; context_die; context_die = context_die->die_parent)
17941 if (context_die->die_tag == DW_TAG_inlined_subroutine
17942 || context_die->die_tag == DW_TAG_subprogram)
17943 return 1;
17945 return 0;
17948 /* Returns nonzero if CONTEXT_DIE is a class. */
17950 static inline int
17951 class_scope_p (dw_die_ref context_die)
17953 return (context_die
17954 && (context_die->die_tag == DW_TAG_structure_type
17955 || context_die->die_tag == DW_TAG_class_type
17956 || context_die->die_tag == DW_TAG_interface_type
17957 || context_die->die_tag == DW_TAG_union_type));
17960 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17961 whether or not to treat a DIE in this context as a declaration. */
17963 static inline int
17964 class_or_namespace_scope_p (dw_die_ref context_die)
17966 return (class_scope_p (context_die)
17967 || (context_die && context_die->die_tag == DW_TAG_namespace));
17970 /* Many forms of DIEs require a "type description" attribute. This
17971 routine locates the proper "type descriptor" die for the type given
17972 by 'type', and adds a DW_AT_type attribute below the given die. */
17974 static void
17975 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17976 int decl_volatile, dw_die_ref context_die)
17978 enum tree_code code = TREE_CODE (type);
17979 dw_die_ref type_die = NULL;
17981 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17982 or fixed-point type, use the inner type. This is because we have no
17983 support for unnamed types in base_type_die. This can happen if this is
17984 an Ada subrange type. Correct solution is emit a subrange type die. */
17985 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17986 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17987 type = TREE_TYPE (type), code = TREE_CODE (type);
17989 if (code == ERROR_MARK
17990 /* Handle a special case. For functions whose return type is void, we
17991 generate *no* type attribute. (Note that no object may have type
17992 `void', so this only applies to function return types). */
17993 || code == VOID_TYPE)
17994 return;
17996 type_die = modified_type_die (type,
17997 decl_const || TYPE_READONLY (type),
17998 decl_volatile || TYPE_VOLATILE (type),
17999 context_die);
18001 if (type_die != NULL)
18002 add_AT_die_ref (object_die, DW_AT_type, type_die);
18005 /* Given an object die, add the calling convention attribute for the
18006 function call type. */
18007 static void
18008 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18010 enum dwarf_calling_convention value = DW_CC_normal;
18012 value = ((enum dwarf_calling_convention)
18013 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18015 if (is_fortran ()
18016 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18018 /* DWARF 2 doesn't provide a way to identify a program's source-level
18019 entry point. DW_AT_calling_convention attributes are only meant
18020 to describe functions' calling conventions. However, lacking a
18021 better way to signal the Fortran main program, we used this for
18022 a long time, following existing custom. Now, DWARF 4 has
18023 DW_AT_main_subprogram, which we add below, but some tools still
18024 rely on the old way, which we thus keep. */
18025 value = DW_CC_program;
18027 if (dwarf_version >= 4 || !dwarf_strict)
18028 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18031 /* Only add the attribute if the backend requests it, and
18032 is not DW_CC_normal. */
18033 if (value && (value != DW_CC_normal))
18034 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18037 /* Given a tree pointer to a struct, class, union, or enum type node, return
18038 a pointer to the (string) tag name for the given type, or zero if the type
18039 was declared without a tag. */
18041 static const char *
18042 type_tag (const_tree type)
18044 const char *name = 0;
18046 if (TYPE_NAME (type) != 0)
18048 tree t = 0;
18050 /* Find the IDENTIFIER_NODE for the type name. */
18051 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18052 && !TYPE_NAMELESS (type))
18053 t = TYPE_NAME (type);
18055 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18056 a TYPE_DECL node, regardless of whether or not a `typedef' was
18057 involved. */
18058 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18059 && ! DECL_IGNORED_P (TYPE_NAME (type)))
18061 /* We want to be extra verbose. Don't call dwarf_name if
18062 DECL_NAME isn't set. The default hook for decl_printable_name
18063 doesn't like that, and in this context it's correct to return
18064 0, instead of "<anonymous>" or the like. */
18065 if (DECL_NAME (TYPE_NAME (type))
18066 && !DECL_NAMELESS (TYPE_NAME (type)))
18067 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18070 /* Now get the name as a string, or invent one. */
18071 if (!name && t != 0)
18072 name = IDENTIFIER_POINTER (t);
18075 return (name == 0 || *name == '\0') ? 0 : name;
18078 /* Return the type associated with a data member, make a special check
18079 for bit field types. */
18081 static inline tree
18082 member_declared_type (const_tree member)
18084 return (DECL_BIT_FIELD_TYPE (member)
18085 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18088 /* Get the decl's label, as described by its RTL. This may be different
18089 from the DECL_NAME name used in the source file. */
18091 #if 0
18092 static const char *
18093 decl_start_label (tree decl)
18095 rtx x;
18096 const char *fnname;
18098 x = DECL_RTL (decl);
18099 gcc_assert (MEM_P (x));
18101 x = XEXP (x, 0);
18102 gcc_assert (GET_CODE (x) == SYMBOL_REF);
18104 fnname = XSTR (x, 0);
18105 return fnname;
18107 #endif
18109 /* These routines generate the internal representation of the DIE's for
18110 the compilation unit. Debugging information is collected by walking
18111 the declaration trees passed in from dwarf2out_decl(). */
18113 static void
18114 gen_array_type_die (tree type, dw_die_ref context_die)
18116 dw_die_ref scope_die = scope_die_for (type, context_die);
18117 dw_die_ref array_die;
18119 /* GNU compilers represent multidimensional array types as sequences of one
18120 dimensional array types whose element types are themselves array types.
18121 We sometimes squish that down to a single array_type DIE with multiple
18122 subscripts in the Dwarf debugging info. The draft Dwarf specification
18123 say that we are allowed to do this kind of compression in C, because
18124 there is no difference between an array of arrays and a multidimensional
18125 array. We don't do this for Ada to remain as close as possible to the
18126 actual representation, which is especially important against the language
18127 flexibilty wrt arrays of variable size. */
18129 bool collapse_nested_arrays = !is_ada ();
18130 tree element_type;
18132 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18133 DW_TAG_string_type doesn't have DW_AT_type attribute). */
18134 if (TYPE_STRING_FLAG (type)
18135 && TREE_CODE (type) == ARRAY_TYPE
18136 && is_fortran ()
18137 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18139 HOST_WIDE_INT size;
18141 array_die = new_die (DW_TAG_string_type, scope_die, type);
18142 add_name_attribute (array_die, type_tag (type));
18143 equate_type_number_to_die (type, array_die);
18144 size = int_size_in_bytes (type);
18145 if (size >= 0)
18146 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18147 else if (TYPE_DOMAIN (type) != NULL_TREE
18148 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18149 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18151 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18152 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18154 size = int_size_in_bytes (TREE_TYPE (szdecl));
18155 if (loc && size > 0)
18157 add_AT_location_description (array_die, DW_AT_string_length, loc);
18158 if (size != DWARF2_ADDR_SIZE)
18159 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18162 return;
18165 /* ??? The SGI dwarf reader fails for array of array of enum types
18166 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18167 array type comes before the outer array type. We thus call gen_type_die
18168 before we new_die and must prevent nested array types collapsing for this
18169 target. */
18171 #ifdef MIPS_DEBUGGING_INFO
18172 gen_type_die (TREE_TYPE (type), context_die);
18173 collapse_nested_arrays = false;
18174 #endif
18176 array_die = new_die (DW_TAG_array_type, scope_die, type);
18177 add_name_attribute (array_die, type_tag (type));
18178 equate_type_number_to_die (type, array_die);
18180 if (TREE_CODE (type) == VECTOR_TYPE)
18181 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18183 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18184 if (is_fortran ()
18185 && TREE_CODE (type) == ARRAY_TYPE
18186 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18187 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18188 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18190 #if 0
18191 /* We default the array ordering. SDB will probably do
18192 the right things even if DW_AT_ordering is not present. It's not even
18193 an issue until we start to get into multidimensional arrays anyway. If
18194 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18195 then we'll have to put the DW_AT_ordering attribute back in. (But if
18196 and when we find out that we need to put these in, we will only do so
18197 for multidimensional arrays. */
18198 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18199 #endif
18201 #ifdef MIPS_DEBUGGING_INFO
18202 /* The SGI compilers handle arrays of unknown bound by setting
18203 AT_declaration and not emitting any subrange DIEs. */
18204 if (TREE_CODE (type) == ARRAY_TYPE
18205 && ! TYPE_DOMAIN (type))
18206 add_AT_flag (array_die, DW_AT_declaration, 1);
18207 else
18208 #endif
18209 if (TREE_CODE (type) == VECTOR_TYPE)
18211 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
18212 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18213 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18214 add_bound_info (subrange_die, DW_AT_upper_bound,
18215 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18217 else
18218 add_subscript_info (array_die, type, collapse_nested_arrays);
18220 /* Add representation of the type of the elements of this array type and
18221 emit the corresponding DIE if we haven't done it already. */
18222 element_type = TREE_TYPE (type);
18223 if (collapse_nested_arrays)
18224 while (TREE_CODE (element_type) == ARRAY_TYPE)
18226 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18227 break;
18228 element_type = TREE_TYPE (element_type);
18231 #ifndef MIPS_DEBUGGING_INFO
18232 gen_type_die (element_type, context_die);
18233 #endif
18235 add_type_attribute (array_die, element_type, 0, 0, context_die);
18237 if (get_AT (array_die, DW_AT_name))
18238 add_pubtype (type, array_die);
18241 static dw_loc_descr_ref
18242 descr_info_loc (tree val, tree base_decl)
18244 HOST_WIDE_INT size;
18245 dw_loc_descr_ref loc, loc2;
18246 enum dwarf_location_atom op;
18248 if (val == base_decl)
18249 return new_loc_descr (DW_OP_push_object_address, 0, 0);
18251 switch (TREE_CODE (val))
18253 CASE_CONVERT:
18254 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18255 case VAR_DECL:
18256 return loc_descriptor_from_tree (val, 0);
18257 case INTEGER_CST:
18258 if (host_integerp (val, 0))
18259 return int_loc_descriptor (tree_low_cst (val, 0));
18260 break;
18261 case INDIRECT_REF:
18262 size = int_size_in_bytes (TREE_TYPE (val));
18263 if (size < 0)
18264 break;
18265 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18266 if (!loc)
18267 break;
18268 if (size == DWARF2_ADDR_SIZE)
18269 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18270 else
18271 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18272 return loc;
18273 case POINTER_PLUS_EXPR:
18274 case PLUS_EXPR:
18275 if (host_integerp (TREE_OPERAND (val, 1), 1)
18276 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18277 < 16384)
18279 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18280 if (!loc)
18281 break;
18282 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18284 else
18286 op = DW_OP_plus;
18287 do_binop:
18288 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18289 if (!loc)
18290 break;
18291 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18292 if (!loc2)
18293 break;
18294 add_loc_descr (&loc, loc2);
18295 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18297 return loc;
18298 case MINUS_EXPR:
18299 op = DW_OP_minus;
18300 goto do_binop;
18301 case MULT_EXPR:
18302 op = DW_OP_mul;
18303 goto do_binop;
18304 case EQ_EXPR:
18305 op = DW_OP_eq;
18306 goto do_binop;
18307 case NE_EXPR:
18308 op = DW_OP_ne;
18309 goto do_binop;
18310 default:
18311 break;
18313 return NULL;
18316 static void
18317 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18318 tree val, tree base_decl)
18320 dw_loc_descr_ref loc;
18322 if (host_integerp (val, 0))
18324 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18325 return;
18328 loc = descr_info_loc (val, base_decl);
18329 if (!loc)
18330 return;
18332 add_AT_loc (die, attr, loc);
18335 /* This routine generates DIE for array with hidden descriptor, details
18336 are filled into *info by a langhook. */
18338 static void
18339 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18340 dw_die_ref context_die)
18342 dw_die_ref scope_die = scope_die_for (type, context_die);
18343 dw_die_ref array_die;
18344 int dim;
18346 array_die = new_die (DW_TAG_array_type, scope_die, type);
18347 add_name_attribute (array_die, type_tag (type));
18348 equate_type_number_to_die (type, array_die);
18350 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18351 if (is_fortran ()
18352 && info->ndimensions >= 2)
18353 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18355 if (info->data_location)
18356 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18357 info->base_decl);
18358 if (info->associated)
18359 add_descr_info_field (array_die, DW_AT_associated, info->associated,
18360 info->base_decl);
18361 if (info->allocated)
18362 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18363 info->base_decl);
18365 for (dim = 0; dim < info->ndimensions; dim++)
18367 dw_die_ref subrange_die
18368 = new_die (DW_TAG_subrange_type, array_die, NULL);
18370 if (info->dimen[dim].lower_bound)
18372 /* If it is the default value, omit it. */
18373 int dflt;
18375 if (host_integerp (info->dimen[dim].lower_bound, 0)
18376 && (dflt = lower_bound_default ()) != -1
18377 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18379 else
18380 add_descr_info_field (subrange_die, DW_AT_lower_bound,
18381 info->dimen[dim].lower_bound,
18382 info->base_decl);
18384 if (info->dimen[dim].upper_bound)
18385 add_descr_info_field (subrange_die, DW_AT_upper_bound,
18386 info->dimen[dim].upper_bound,
18387 info->base_decl);
18388 if (info->dimen[dim].stride)
18389 add_descr_info_field (subrange_die, DW_AT_byte_stride,
18390 info->dimen[dim].stride,
18391 info->base_decl);
18394 gen_type_die (info->element_type, context_die);
18395 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18397 if (get_AT (array_die, DW_AT_name))
18398 add_pubtype (type, array_die);
18401 #if 0
18402 static void
18403 gen_entry_point_die (tree decl, dw_die_ref context_die)
18405 tree origin = decl_ultimate_origin (decl);
18406 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18408 if (origin != NULL)
18409 add_abstract_origin_attribute (decl_die, origin);
18410 else
18412 add_name_and_src_coords_attributes (decl_die, decl);
18413 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18414 0, 0, context_die);
18417 if (DECL_ABSTRACT (decl))
18418 equate_decl_number_to_die (decl, decl_die);
18419 else
18420 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18422 #endif
18424 /* Walk through the list of incomplete types again, trying once more to
18425 emit full debugging info for them. */
18427 static void
18428 retry_incomplete_types (void)
18430 int i;
18432 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18433 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18434 DINFO_USAGE_DIR_USE))
18435 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18438 /* Determine what tag to use for a record type. */
18440 static enum dwarf_tag
18441 record_type_tag (tree type)
18443 if (! lang_hooks.types.classify_record)
18444 return DW_TAG_structure_type;
18446 switch (lang_hooks.types.classify_record (type))
18448 case RECORD_IS_STRUCT:
18449 return DW_TAG_structure_type;
18451 case RECORD_IS_CLASS:
18452 return DW_TAG_class_type;
18454 case RECORD_IS_INTERFACE:
18455 if (dwarf_version >= 3 || !dwarf_strict)
18456 return DW_TAG_interface_type;
18457 return DW_TAG_structure_type;
18459 default:
18460 gcc_unreachable ();
18464 /* Generate a DIE to represent an enumeration type. Note that these DIEs
18465 include all of the information about the enumeration values also. Each
18466 enumerated type name/value is listed as a child of the enumerated type
18467 DIE. */
18469 static dw_die_ref
18470 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18472 dw_die_ref type_die = lookup_type_die (type);
18474 if (type_die == NULL)
18476 type_die = new_die (DW_TAG_enumeration_type,
18477 scope_die_for (type, context_die), type);
18478 equate_type_number_to_die (type, type_die);
18479 add_name_attribute (type_die, type_tag (type));
18480 if (dwarf_version >= 4 || !dwarf_strict)
18482 if (ENUM_IS_SCOPED (type))
18483 add_AT_flag (type_die, DW_AT_enum_class, 1);
18484 if (ENUM_IS_OPAQUE (type))
18485 add_AT_flag (type_die, DW_AT_declaration, 1);
18488 else if (! TYPE_SIZE (type))
18489 return type_die;
18490 else
18491 remove_AT (type_die, DW_AT_declaration);
18493 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
18494 given enum type is incomplete, do not generate the DW_AT_byte_size
18495 attribute or the DW_AT_element_list attribute. */
18496 if (TYPE_SIZE (type))
18498 tree link;
18500 TREE_ASM_WRITTEN (type) = 1;
18501 add_byte_size_attribute (type_die, type);
18502 if (TYPE_STUB_DECL (type) != NULL_TREE)
18504 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18505 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18508 /* If the first reference to this type was as the return type of an
18509 inline function, then it may not have a parent. Fix this now. */
18510 if (type_die->die_parent == NULL)
18511 add_child_die (scope_die_for (type, context_die), type_die);
18513 for (link = TYPE_VALUES (type);
18514 link != NULL; link = TREE_CHAIN (link))
18516 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18517 tree value = TREE_VALUE (link);
18519 add_name_attribute (enum_die,
18520 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18522 if (TREE_CODE (value) == CONST_DECL)
18523 value = DECL_INITIAL (value);
18525 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18526 /* DWARF2 does not provide a way of indicating whether or
18527 not enumeration constants are signed or unsigned. GDB
18528 always assumes the values are signed, so we output all
18529 values as if they were signed. That means that
18530 enumeration constants with very large unsigned values
18531 will appear to have negative values in the debugger. */
18532 add_AT_int (enum_die, DW_AT_const_value,
18533 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18536 else
18537 add_AT_flag (type_die, DW_AT_declaration, 1);
18539 if (get_AT (type_die, DW_AT_name))
18540 add_pubtype (type, type_die);
18542 return type_die;
18545 /* Generate a DIE to represent either a real live formal parameter decl or to
18546 represent just the type of some formal parameter position in some function
18547 type.
18549 Note that this routine is a bit unusual because its argument may be a
18550 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18551 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18552 node. If it's the former then this function is being called to output a
18553 DIE to represent a formal parameter object (or some inlining thereof). If
18554 it's the latter, then this function is only being called to output a
18555 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18556 argument type of some subprogram type.
18557 If EMIT_NAME_P is true, name and source coordinate attributes
18558 are emitted. */
18560 static dw_die_ref
18561 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18562 dw_die_ref context_die)
18564 tree node_or_origin = node ? node : origin;
18565 tree ultimate_origin;
18566 dw_die_ref parm_die
18567 = new_die (DW_TAG_formal_parameter, context_die, node);
18569 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18571 case tcc_declaration:
18572 ultimate_origin = decl_ultimate_origin (node_or_origin);
18573 if (node || ultimate_origin)
18574 origin = ultimate_origin;
18575 if (origin != NULL)
18576 add_abstract_origin_attribute (parm_die, origin);
18577 else if (emit_name_p)
18578 add_name_and_src_coords_attributes (parm_die, node);
18579 if (origin == NULL
18580 || (! DECL_ABSTRACT (node_or_origin)
18581 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18582 decl_function_context
18583 (node_or_origin))))
18585 tree type = TREE_TYPE (node_or_origin);
18586 if (decl_by_reference_p (node_or_origin))
18587 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18588 context_die);
18589 else
18590 add_type_attribute (parm_die, type,
18591 TREE_READONLY (node_or_origin),
18592 TREE_THIS_VOLATILE (node_or_origin),
18593 context_die);
18595 if (origin == NULL && DECL_ARTIFICIAL (node))
18596 add_AT_flag (parm_die, DW_AT_artificial, 1);
18598 if (node && node != origin)
18599 equate_decl_number_to_die (node, parm_die);
18600 if (! DECL_ABSTRACT (node_or_origin))
18601 add_location_or_const_value_attribute (parm_die, node_or_origin,
18602 DW_AT_location);
18604 break;
18606 case tcc_type:
18607 /* We were called with some kind of a ..._TYPE node. */
18608 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18609 break;
18611 default:
18612 gcc_unreachable ();
18615 return parm_die;
18618 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18619 children DW_TAG_formal_parameter DIEs representing the arguments of the
18620 parameter pack.
18622 PARM_PACK must be a function parameter pack.
18623 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18624 must point to the subsequent arguments of the function PACK_ARG belongs to.
18625 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18626 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18627 following the last one for which a DIE was generated. */
18629 static dw_die_ref
18630 gen_formal_parameter_pack_die (tree parm_pack,
18631 tree pack_arg,
18632 dw_die_ref subr_die,
18633 tree *next_arg)
18635 tree arg;
18636 dw_die_ref parm_pack_die;
18638 gcc_assert (parm_pack
18639 && lang_hooks.function_parameter_pack_p (parm_pack)
18640 && subr_die);
18642 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18643 add_src_coords_attributes (parm_pack_die, parm_pack);
18645 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18647 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18648 parm_pack))
18649 break;
18650 gen_formal_parameter_die (arg, NULL,
18651 false /* Don't emit name attribute. */,
18652 parm_pack_die);
18654 if (next_arg)
18655 *next_arg = arg;
18656 return parm_pack_die;
18659 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18660 at the end of an (ANSI prototyped) formal parameters list. */
18662 static void
18663 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18665 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18668 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18669 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18670 parameters as specified in some function type specification (except for
18671 those which appear as part of a function *definition*). */
18673 static void
18674 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18676 tree link;
18677 tree formal_type = NULL;
18678 tree first_parm_type;
18679 tree arg;
18681 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18683 arg = DECL_ARGUMENTS (function_or_method_type);
18684 function_or_method_type = TREE_TYPE (function_or_method_type);
18686 else
18687 arg = NULL_TREE;
18689 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18691 /* Make our first pass over the list of formal parameter types and output a
18692 DW_TAG_formal_parameter DIE for each one. */
18693 for (link = first_parm_type; link; )
18695 dw_die_ref parm_die;
18697 formal_type = TREE_VALUE (link);
18698 if (formal_type == void_type_node)
18699 break;
18701 /* Output a (nameless) DIE to represent the formal parameter itself. */
18702 parm_die = gen_formal_parameter_die (formal_type, NULL,
18703 true /* Emit name attribute. */,
18704 context_die);
18705 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18706 && link == first_parm_type)
18708 add_AT_flag (parm_die, DW_AT_artificial, 1);
18709 if (dwarf_version >= 3 || !dwarf_strict)
18710 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18712 else if (arg && DECL_ARTIFICIAL (arg))
18713 add_AT_flag (parm_die, DW_AT_artificial, 1);
18715 link = TREE_CHAIN (link);
18716 if (arg)
18717 arg = DECL_CHAIN (arg);
18720 /* If this function type has an ellipsis, add a
18721 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18722 if (formal_type != void_type_node)
18723 gen_unspecified_parameters_die (function_or_method_type, context_die);
18725 /* Make our second (and final) pass over the list of formal parameter types
18726 and output DIEs to represent those types (as necessary). */
18727 for (link = TYPE_ARG_TYPES (function_or_method_type);
18728 link && TREE_VALUE (link);
18729 link = TREE_CHAIN (link))
18730 gen_type_die (TREE_VALUE (link), context_die);
18733 /* We want to generate the DIE for TYPE so that we can generate the
18734 die for MEMBER, which has been defined; we will need to refer back
18735 to the member declaration nested within TYPE. If we're trying to
18736 generate minimal debug info for TYPE, processing TYPE won't do the
18737 trick; we need to attach the member declaration by hand. */
18739 static void
18740 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18742 gen_type_die (type, context_die);
18744 /* If we're trying to avoid duplicate debug info, we may not have
18745 emitted the member decl for this function. Emit it now. */
18746 if (TYPE_STUB_DECL (type)
18747 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18748 && ! lookup_decl_die (member))
18750 dw_die_ref type_die;
18751 gcc_assert (!decl_ultimate_origin (member));
18753 push_decl_scope (type);
18754 type_die = lookup_type_die_strip_naming_typedef (type);
18755 if (TREE_CODE (member) == FUNCTION_DECL)
18756 gen_subprogram_die (member, type_die);
18757 else if (TREE_CODE (member) == FIELD_DECL)
18759 /* Ignore the nameless fields that are used to skip bits but handle
18760 C++ anonymous unions and structs. */
18761 if (DECL_NAME (member) != NULL_TREE
18762 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18763 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18765 gen_type_die (member_declared_type (member), type_die);
18766 gen_field_die (member, type_die);
18769 else
18770 gen_variable_die (member, NULL_TREE, type_die);
18772 pop_decl_scope ();
18776 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18777 may later generate inlined and/or out-of-line instances of. */
18779 static void
18780 dwarf2out_abstract_function (tree decl)
18782 dw_die_ref old_die;
18783 tree save_fn;
18784 tree context;
18785 int was_abstract;
18786 htab_t old_decl_loc_table;
18788 /* Make sure we have the actual abstract inline, not a clone. */
18789 decl = DECL_ORIGIN (decl);
18791 old_die = lookup_decl_die (decl);
18792 if (old_die && get_AT (old_die, DW_AT_inline))
18793 /* We've already generated the abstract instance. */
18794 return;
18796 /* We can be called while recursively when seeing block defining inlined subroutine
18797 DIE. Be sure to not clobber the outer location table nor use it or we would
18798 get locations in abstract instantces. */
18799 old_decl_loc_table = decl_loc_table;
18800 decl_loc_table = NULL;
18802 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18803 we don't get confused by DECL_ABSTRACT. */
18804 if (debug_info_level > DINFO_LEVEL_TERSE)
18806 context = decl_class_context (decl);
18807 if (context)
18808 gen_type_die_for_member
18809 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18812 /* Pretend we've just finished compiling this function. */
18813 save_fn = current_function_decl;
18814 current_function_decl = decl;
18815 push_cfun (DECL_STRUCT_FUNCTION (decl));
18817 was_abstract = DECL_ABSTRACT (decl);
18818 set_decl_abstract_flags (decl, 1);
18819 dwarf2out_decl (decl);
18820 if (! was_abstract)
18821 set_decl_abstract_flags (decl, 0);
18823 current_function_decl = save_fn;
18824 decl_loc_table = old_decl_loc_table;
18825 pop_cfun ();
18828 /* Helper function of premark_used_types() which gets called through
18829 htab_traverse.
18831 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18832 marked as unused by prune_unused_types. */
18834 static int
18835 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18837 tree type;
18838 dw_die_ref die;
18840 type = (tree) *slot;
18841 die = lookup_type_die (type);
18842 if (die != NULL)
18843 die->die_perennial_p = 1;
18844 return 1;
18847 /* Helper function of premark_types_used_by_global_vars which gets called
18848 through htab_traverse.
18850 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18851 marked as unused by prune_unused_types. The DIE of the type is marked
18852 only if the global variable using the type will actually be emitted. */
18854 static int
18855 premark_types_used_by_global_vars_helper (void **slot,
18856 void *data ATTRIBUTE_UNUSED)
18858 struct types_used_by_vars_entry *entry;
18859 dw_die_ref die;
18861 entry = (struct types_used_by_vars_entry *) *slot;
18862 gcc_assert (entry->type != NULL
18863 && entry->var_decl != NULL);
18864 die = lookup_type_die (entry->type);
18865 if (die)
18867 /* Ask cgraph if the global variable really is to be emitted.
18868 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18869 struct varpool_node *node = varpool_get_node (entry->var_decl);
18870 if (node && node->needed)
18872 die->die_perennial_p = 1;
18873 /* Keep the parent DIEs as well. */
18874 while ((die = die->die_parent) && die->die_perennial_p == 0)
18875 die->die_perennial_p = 1;
18878 return 1;
18881 /* Mark all members of used_types_hash as perennial. */
18883 static void
18884 premark_used_types (void)
18886 if (cfun && cfun->used_types_hash)
18887 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18890 /* Mark all members of types_used_by_vars_entry as perennial. */
18892 static void
18893 premark_types_used_by_global_vars (void)
18895 if (types_used_by_vars_hash)
18896 htab_traverse (types_used_by_vars_hash,
18897 premark_types_used_by_global_vars_helper, NULL);
18900 /* Generate a DIE to represent a declared function (either file-scope or
18901 block-local). */
18903 static void
18904 gen_subprogram_die (tree decl, dw_die_ref context_die)
18906 tree origin = decl_ultimate_origin (decl);
18907 dw_die_ref subr_die;
18908 tree outer_scope;
18909 dw_die_ref old_die = lookup_decl_die (decl);
18910 int declaration = (current_function_decl != decl
18911 || class_or_namespace_scope_p (context_die));
18913 premark_used_types ();
18915 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18916 started to generate the abstract instance of an inline, decided to output
18917 its containing class, and proceeded to emit the declaration of the inline
18918 from the member list for the class. If so, DECLARATION takes priority;
18919 we'll get back to the abstract instance when done with the class. */
18921 /* The class-scope declaration DIE must be the primary DIE. */
18922 if (origin && declaration && class_or_namespace_scope_p (context_die))
18924 origin = NULL;
18925 gcc_assert (!old_die);
18928 /* Now that the C++ front end lazily declares artificial member fns, we
18929 might need to retrofit the declaration into its class. */
18930 if (!declaration && !origin && !old_die
18931 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18932 && !class_or_namespace_scope_p (context_die)
18933 && debug_info_level > DINFO_LEVEL_TERSE)
18934 old_die = force_decl_die (decl);
18936 if (origin != NULL)
18938 gcc_assert (!declaration || local_scope_p (context_die));
18940 /* Fixup die_parent for the abstract instance of a nested
18941 inline function. */
18942 if (old_die && old_die->die_parent == NULL)
18943 add_child_die (context_die, old_die);
18945 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18946 add_abstract_origin_attribute (subr_die, origin);
18948 else if (old_die)
18950 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18951 struct dwarf_file_data * file_index = lookup_filename (s.file);
18953 if (!get_AT_flag (old_die, DW_AT_declaration)
18954 /* We can have a normal definition following an inline one in the
18955 case of redefinition of GNU C extern inlines.
18956 It seems reasonable to use AT_specification in this case. */
18957 && !get_AT (old_die, DW_AT_inline))
18959 /* Detect and ignore this case, where we are trying to output
18960 something we have already output. */
18961 return;
18964 /* If the definition comes from the same place as the declaration,
18965 maybe use the old DIE. We always want the DIE for this function
18966 that has the *_pc attributes to be under comp_unit_die so the
18967 debugger can find it. We also need to do this for abstract
18968 instances of inlines, since the spec requires the out-of-line copy
18969 to have the same parent. For local class methods, this doesn't
18970 apply; we just use the old DIE. */
18971 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18972 && (DECL_ARTIFICIAL (decl)
18973 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18974 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18975 == (unsigned) s.line))))
18977 subr_die = old_die;
18979 /* Clear out the declaration attribute and the formal parameters.
18980 Do not remove all children, because it is possible that this
18981 declaration die was forced using force_decl_die(). In such
18982 cases die that forced declaration die (e.g. TAG_imported_module)
18983 is one of the children that we do not want to remove. */
18984 remove_AT (subr_die, DW_AT_declaration);
18985 remove_AT (subr_die, DW_AT_object_pointer);
18986 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18988 else
18990 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18991 add_AT_specification (subr_die, old_die);
18992 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18993 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18994 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18995 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18998 else
19000 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19002 if (TREE_PUBLIC (decl))
19003 add_AT_flag (subr_die, DW_AT_external, 1);
19005 add_name_and_src_coords_attributes (subr_die, decl);
19006 if (debug_info_level > DINFO_LEVEL_TERSE)
19008 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19009 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19010 0, 0, context_die);
19013 add_pure_or_virtual_attribute (subr_die, decl);
19014 if (DECL_ARTIFICIAL (decl))
19015 add_AT_flag (subr_die, DW_AT_artificial, 1);
19017 add_accessibility_attribute (subr_die, decl);
19020 if (declaration)
19022 if (!old_die || !get_AT (old_die, DW_AT_inline))
19024 add_AT_flag (subr_die, DW_AT_declaration, 1);
19026 /* If this is an explicit function declaration then generate
19027 a DW_AT_explicit attribute. */
19028 if (lang_hooks.decls.function_decl_explicit_p (decl)
19029 && (dwarf_version >= 3 || !dwarf_strict))
19030 add_AT_flag (subr_die, DW_AT_explicit, 1);
19032 /* The first time we see a member function, it is in the context of
19033 the class to which it belongs. We make sure of this by emitting
19034 the class first. The next time is the definition, which is
19035 handled above. The two may come from the same source text.
19037 Note that force_decl_die() forces function declaration die. It is
19038 later reused to represent definition. */
19039 equate_decl_number_to_die (decl, subr_die);
19042 else if (DECL_ABSTRACT (decl))
19044 if (DECL_DECLARED_INLINE_P (decl))
19046 if (cgraph_function_possibly_inlined_p (decl))
19047 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19048 else
19049 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19051 else
19053 if (cgraph_function_possibly_inlined_p (decl))
19054 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19055 else
19056 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19059 if (DECL_DECLARED_INLINE_P (decl)
19060 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19061 add_AT_flag (subr_die, DW_AT_artificial, 1);
19063 equate_decl_number_to_die (decl, subr_die);
19065 else if (!DECL_EXTERNAL (decl))
19067 HOST_WIDE_INT cfa_fb_offset;
19069 if (!old_die || !get_AT (old_die, DW_AT_inline))
19070 equate_decl_number_to_die (decl, subr_die);
19072 if (!flag_reorder_blocks_and_partition)
19074 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19075 if (fde->dw_fde_begin)
19077 /* We have already generated the labels. */
19078 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19079 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19081 else
19083 /* Create start/end labels and add the range. */
19084 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19085 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19086 current_function_funcdef_no);
19087 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19088 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19089 current_function_funcdef_no);
19090 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19093 #if VMS_DEBUGGING_INFO
19094 /* HP OpenVMS Industry Standard 64: DWARF Extensions
19095 Section 2.3 Prologue and Epilogue Attributes:
19096 When a breakpoint is set on entry to a function, it is generally
19097 desirable for execution to be suspended, not on the very first
19098 instruction of the function, but rather at a point after the
19099 function's frame has been set up, after any language defined local
19100 declaration processing has been completed, and before execution of
19101 the first statement of the function begins. Debuggers generally
19102 cannot properly determine where this point is. Similarly for a
19103 breakpoint set on exit from a function. The prologue and epilogue
19104 attributes allow a compiler to communicate the location(s) to use. */
19107 if (fde->dw_fde_vms_end_prologue)
19108 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19109 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19111 if (fde->dw_fde_vms_begin_epilogue)
19112 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19113 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19115 #endif
19117 add_pubname (decl, subr_die);
19118 add_arange (decl, subr_die);
19120 else
19121 { /* Generate pubnames entries for the split function code
19122 ranges. */
19123 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19125 if (fde->dw_fde_switched_sections)
19127 if (dwarf_version >= 3 || !dwarf_strict)
19129 /* We should use ranges for non-contiguous code section
19130 addresses. Use the actual code range for the initial
19131 section, since the HOT/COLD labels might precede an
19132 alignment offset. */
19133 bool range_list_added = false;
19134 if (fde->in_std_section)
19136 add_ranges_by_labels (subr_die,
19137 fde->dw_fde_begin,
19138 fde->dw_fde_end,
19139 &range_list_added);
19140 add_ranges_by_labels (subr_die,
19141 fde->dw_fde_unlikely_section_label,
19142 fde->dw_fde_unlikely_section_end_label,
19143 &range_list_added);
19145 else
19147 add_ranges_by_labels (subr_die,
19148 fde->dw_fde_begin,
19149 fde->dw_fde_end,
19150 &range_list_added);
19151 add_ranges_by_labels (subr_die,
19152 fde->dw_fde_hot_section_label,
19153 fde->dw_fde_hot_section_end_label,
19154 &range_list_added);
19156 add_pubname (decl, subr_die);
19157 if (range_list_added)
19158 add_ranges (NULL);
19160 else
19162 /* There is no real support in DW2 for this .. so we make
19163 a work-around. First, emit the pub name for the segment
19164 containing the function label. Then make and emit a
19165 simplified subprogram DIE for the second segment with the
19166 name pre-fixed by __hot/cold_sect_of_. We use the same
19167 linkage name for the second die so that gdb will find both
19168 sections when given "b foo". */
19169 const char *name = NULL;
19170 tree decl_name = DECL_NAME (decl);
19171 dw_die_ref seg_die;
19173 /* Do the 'primary' section. */
19174 add_AT_lbl_id (subr_die, DW_AT_low_pc,
19175 fde->dw_fde_begin);
19176 add_AT_lbl_id (subr_die, DW_AT_high_pc,
19177 fde->dw_fde_end);
19178 /* Add it. */
19179 add_pubname (decl, subr_die);
19180 add_arange (decl, subr_die);
19182 /* Build a minimal DIE for the secondary section. */
19183 seg_die = new_die (DW_TAG_subprogram,
19184 subr_die->die_parent, decl);
19186 if (TREE_PUBLIC (decl))
19187 add_AT_flag (seg_die, DW_AT_external, 1);
19189 if (decl_name != NULL
19190 && IDENTIFIER_POINTER (decl_name) != NULL)
19192 name = dwarf2_name (decl, 1);
19193 if (! DECL_ARTIFICIAL (decl))
19194 add_src_coords_attributes (seg_die, decl);
19196 add_linkage_name (seg_die, decl);
19198 gcc_assert (name!=NULL);
19199 add_pure_or_virtual_attribute (seg_die, decl);
19200 if (DECL_ARTIFICIAL (decl))
19201 add_AT_flag (seg_die, DW_AT_artificial, 1);
19203 if (fde->in_std_section)
19205 name = concat ("__cold_sect_of_", name, NULL);
19206 add_AT_lbl_id (seg_die, DW_AT_low_pc,
19207 fde->dw_fde_unlikely_section_label);
19208 add_AT_lbl_id (seg_die, DW_AT_high_pc,
19209 fde->dw_fde_unlikely_section_end_label);
19211 else
19213 name = concat ("__hot_sect_of_", name, NULL);
19214 add_AT_lbl_id (seg_die, DW_AT_low_pc,
19215 fde->dw_fde_hot_section_label);
19216 add_AT_lbl_id (seg_die, DW_AT_high_pc,
19217 fde->dw_fde_hot_section_end_label);
19219 add_name_attribute (seg_die, name);
19220 add_pubname_string (name, seg_die);
19221 add_arange (decl, seg_die);
19224 else
19226 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19227 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19228 add_pubname (decl, subr_die);
19229 add_arange (decl, subr_die);
19233 #ifdef MIPS_DEBUGGING_INFO
19234 /* Add a reference to the FDE for this routine. */
19235 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19236 #endif
19238 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19240 /* We define the "frame base" as the function's CFA. This is more
19241 convenient for several reasons: (1) It's stable across the prologue
19242 and epilogue, which makes it better than just a frame pointer,
19243 (2) With dwarf3, there exists a one-byte encoding that allows us
19244 to reference the .debug_frame data by proxy, but failing that,
19245 (3) We can at least reuse the code inspection and interpretation
19246 code that determines the CFA position at various points in the
19247 function. */
19248 if (dwarf_version >= 3)
19250 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19251 add_AT_loc (subr_die, DW_AT_frame_base, op);
19253 else
19255 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19256 if (list->dw_loc_next)
19257 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19258 else
19259 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19262 /* Compute a displacement from the "steady-state frame pointer" to
19263 the CFA. The former is what all stack slots and argument slots
19264 will reference in the rtl; the later is what we've told the
19265 debugger about. We'll need to adjust all frame_base references
19266 by this displacement. */
19267 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19269 if (cfun->static_chain_decl)
19270 add_AT_location_description (subr_die, DW_AT_static_link,
19271 loc_list_from_tree (cfun->static_chain_decl, 2));
19274 /* Generate child dies for template paramaters. */
19275 if (debug_info_level > DINFO_LEVEL_TERSE)
19276 gen_generic_params_dies (decl);
19278 /* Now output descriptions of the arguments for this function. This gets
19279 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19280 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19281 `...' at the end of the formal parameter list. In order to find out if
19282 there was a trailing ellipsis or not, we must instead look at the type
19283 associated with the FUNCTION_DECL. This will be a node of type
19284 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19285 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19286 an ellipsis at the end. */
19288 /* In the case where we are describing a mere function declaration, all we
19289 need to do here (and all we *can* do here) is to describe the *types* of
19290 its formal parameters. */
19291 if (debug_info_level <= DINFO_LEVEL_TERSE)
19293 else if (declaration)
19294 gen_formal_types_die (decl, subr_die);
19295 else
19297 /* Generate DIEs to represent all known formal parameters. */
19298 tree parm = DECL_ARGUMENTS (decl);
19299 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19300 tree generic_decl_parm = generic_decl
19301 ? DECL_ARGUMENTS (generic_decl)
19302 : NULL;
19304 /* Now we want to walk the list of parameters of the function and
19305 emit their relevant DIEs.
19307 We consider the case of DECL being an instance of a generic function
19308 as well as it being a normal function.
19310 If DECL is an instance of a generic function we walk the
19311 parameters of the generic function declaration _and_ the parameters of
19312 DECL itself. This is useful because we want to emit specific DIEs for
19313 function parameter packs and those are declared as part of the
19314 generic function declaration. In that particular case,
19315 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19316 That DIE has children DIEs representing the set of arguments
19317 of the pack. Note that the set of pack arguments can be empty.
19318 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19319 children DIE.
19321 Otherwise, we just consider the parameters of DECL. */
19322 while (generic_decl_parm || parm)
19324 if (generic_decl_parm
19325 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19326 gen_formal_parameter_pack_die (generic_decl_parm,
19327 parm, subr_die,
19328 &parm);
19329 else if (parm)
19331 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19333 if (parm == DECL_ARGUMENTS (decl)
19334 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19335 && parm_die
19336 && (dwarf_version >= 3 || !dwarf_strict))
19337 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19339 parm = DECL_CHAIN (parm);
19342 if (generic_decl_parm)
19343 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19346 /* Decide whether we need an unspecified_parameters DIE at the end.
19347 There are 2 more cases to do this for: 1) the ansi ... declaration -
19348 this is detectable when the end of the arg list is not a
19349 void_type_node 2) an unprototyped function declaration (not a
19350 definition). This just means that we have no info about the
19351 parameters at all. */
19352 if (prototype_p (TREE_TYPE (decl)))
19354 /* This is the prototyped case, check for.... */
19355 if (stdarg_p (TREE_TYPE (decl)))
19356 gen_unspecified_parameters_die (decl, subr_die);
19358 else if (DECL_INITIAL (decl) == NULL_TREE)
19359 gen_unspecified_parameters_die (decl, subr_die);
19362 /* Output Dwarf info for all of the stuff within the body of the function
19363 (if it has one - it may be just a declaration). */
19364 outer_scope = DECL_INITIAL (decl);
19366 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19367 a function. This BLOCK actually represents the outermost binding contour
19368 for the function, i.e. the contour in which the function's formal
19369 parameters and labels get declared. Curiously, it appears that the front
19370 end doesn't actually put the PARM_DECL nodes for the current function onto
19371 the BLOCK_VARS list for this outer scope, but are strung off of the
19372 DECL_ARGUMENTS list for the function instead.
19374 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19375 the LABEL_DECL nodes for the function however, and we output DWARF info
19376 for those in decls_for_scope. Just within the `outer_scope' there will be
19377 a BLOCK node representing the function's outermost pair of curly braces,
19378 and any blocks used for the base and member initializers of a C++
19379 constructor function. */
19380 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19382 /* Emit a DW_TAG_variable DIE for a named return value. */
19383 if (DECL_NAME (DECL_RESULT (decl)))
19384 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19386 current_function_has_inlines = 0;
19387 decls_for_scope (outer_scope, subr_die, 0);
19389 /* Add the calling convention attribute if requested. */
19390 add_calling_convention_attribute (subr_die, decl);
19394 /* Returns a hash value for X (which really is a die_struct). */
19396 static hashval_t
19397 common_block_die_table_hash (const void *x)
19399 const_dw_die_ref d = (const_dw_die_ref) x;
19400 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19403 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19404 as decl_id and die_parent of die_struct Y. */
19406 static int
19407 common_block_die_table_eq (const void *x, const void *y)
19409 const_dw_die_ref d = (const_dw_die_ref) x;
19410 const_dw_die_ref e = (const_dw_die_ref) y;
19411 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19414 /* Generate a DIE to represent a declared data object.
19415 Either DECL or ORIGIN must be non-null. */
19417 static void
19418 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19420 HOST_WIDE_INT off;
19421 tree com_decl;
19422 tree decl_or_origin = decl ? decl : origin;
19423 tree ultimate_origin;
19424 dw_die_ref var_die;
19425 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19426 dw_die_ref origin_die;
19427 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19428 || class_or_namespace_scope_p (context_die));
19429 bool specialization_p = false;
19431 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19432 if (decl || ultimate_origin)
19433 origin = ultimate_origin;
19434 com_decl = fortran_common (decl_or_origin, &off);
19436 /* Symbol in common gets emitted as a child of the common block, in the form
19437 of a data member. */
19438 if (com_decl)
19440 dw_die_ref com_die;
19441 dw_loc_list_ref loc;
19442 die_node com_die_arg;
19444 var_die = lookup_decl_die (decl_or_origin);
19445 if (var_die)
19447 if (get_AT (var_die, DW_AT_location) == NULL)
19449 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19450 if (loc)
19452 if (off)
19454 /* Optimize the common case. */
19455 if (single_element_loc_list_p (loc)
19456 && loc->expr->dw_loc_opc == DW_OP_addr
19457 && loc->expr->dw_loc_next == NULL
19458 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19459 == SYMBOL_REF)
19460 loc->expr->dw_loc_oprnd1.v.val_addr
19461 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19462 else
19463 loc_list_plus_const (loc, off);
19465 add_AT_location_description (var_die, DW_AT_location, loc);
19466 remove_AT (var_die, DW_AT_declaration);
19469 return;
19472 if (common_block_die_table == NULL)
19473 common_block_die_table
19474 = htab_create_ggc (10, common_block_die_table_hash,
19475 common_block_die_table_eq, NULL);
19477 com_die_arg.decl_id = DECL_UID (com_decl);
19478 com_die_arg.die_parent = context_die;
19479 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19480 loc = loc_list_from_tree (com_decl, 2);
19481 if (com_die == NULL)
19483 const char *cnam
19484 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19485 void **slot;
19487 com_die = new_die (DW_TAG_common_block, context_die, decl);
19488 add_name_and_src_coords_attributes (com_die, com_decl);
19489 if (loc)
19491 add_AT_location_description (com_die, DW_AT_location, loc);
19492 /* Avoid sharing the same loc descriptor between
19493 DW_TAG_common_block and DW_TAG_variable. */
19494 loc = loc_list_from_tree (com_decl, 2);
19496 else if (DECL_EXTERNAL (decl))
19497 add_AT_flag (com_die, DW_AT_declaration, 1);
19498 add_pubname_string (cnam, com_die); /* ??? needed? */
19499 com_die->decl_id = DECL_UID (com_decl);
19500 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19501 *slot = (void *) com_die;
19503 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19505 add_AT_location_description (com_die, DW_AT_location, loc);
19506 loc = loc_list_from_tree (com_decl, 2);
19507 remove_AT (com_die, DW_AT_declaration);
19509 var_die = new_die (DW_TAG_variable, com_die, decl);
19510 add_name_and_src_coords_attributes (var_die, decl);
19511 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19512 TREE_THIS_VOLATILE (decl), context_die);
19513 add_AT_flag (var_die, DW_AT_external, 1);
19514 if (loc)
19516 if (off)
19518 /* Optimize the common case. */
19519 if (single_element_loc_list_p (loc)
19520 && loc->expr->dw_loc_opc == DW_OP_addr
19521 && loc->expr->dw_loc_next == NULL
19522 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19523 loc->expr->dw_loc_oprnd1.v.val_addr
19524 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19525 else
19526 loc_list_plus_const (loc, off);
19528 add_AT_location_description (var_die, DW_AT_location, loc);
19530 else if (DECL_EXTERNAL (decl))
19531 add_AT_flag (var_die, DW_AT_declaration, 1);
19532 equate_decl_number_to_die (decl, var_die);
19533 return;
19536 /* If the compiler emitted a definition for the DECL declaration
19537 and if we already emitted a DIE for it, don't emit a second
19538 DIE for it again. Allow re-declarations of DECLs that are
19539 inside functions, though. */
19540 if (old_die && declaration && !local_scope_p (context_die))
19541 return;
19543 /* For static data members, the declaration in the class is supposed
19544 to have DW_TAG_member tag; the specification should still be
19545 DW_TAG_variable referencing the DW_TAG_member DIE. */
19546 if (declaration && class_scope_p (context_die))
19547 var_die = new_die (DW_TAG_member, context_die, decl);
19548 else
19549 var_die = new_die (DW_TAG_variable, context_die, decl);
19551 origin_die = NULL;
19552 if (origin != NULL)
19553 origin_die = add_abstract_origin_attribute (var_die, origin);
19555 /* Loop unrolling can create multiple blocks that refer to the same
19556 static variable, so we must test for the DW_AT_declaration flag.
19558 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19559 copy decls and set the DECL_ABSTRACT flag on them instead of
19560 sharing them.
19562 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19564 ??? The declare_in_namespace support causes us to get two DIEs for one
19565 variable, both of which are declarations. We want to avoid considering
19566 one to be a specification, so we must test that this DIE is not a
19567 declaration. */
19568 else if (old_die && TREE_STATIC (decl) && ! declaration
19569 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19571 /* This is a definition of a C++ class level static. */
19572 add_AT_specification (var_die, old_die);
19573 specialization_p = true;
19574 if (DECL_NAME (decl))
19576 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19577 struct dwarf_file_data * file_index = lookup_filename (s.file);
19579 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19580 add_AT_file (var_die, DW_AT_decl_file, file_index);
19582 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19583 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19585 if (old_die->die_tag == DW_TAG_member)
19586 add_linkage_name (var_die, decl);
19589 else
19590 add_name_and_src_coords_attributes (var_die, decl);
19592 if ((origin == NULL && !specialization_p)
19593 || (origin != NULL
19594 && !DECL_ABSTRACT (decl_or_origin)
19595 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19596 decl_function_context
19597 (decl_or_origin))))
19599 tree type = TREE_TYPE (decl_or_origin);
19601 if (decl_by_reference_p (decl_or_origin))
19602 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19603 else
19604 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19605 TREE_THIS_VOLATILE (decl_or_origin), context_die);
19608 if (origin == NULL && !specialization_p)
19610 if (TREE_PUBLIC (decl))
19611 add_AT_flag (var_die, DW_AT_external, 1);
19613 if (DECL_ARTIFICIAL (decl))
19614 add_AT_flag (var_die, DW_AT_artificial, 1);
19616 add_accessibility_attribute (var_die, decl);
19619 if (declaration)
19620 add_AT_flag (var_die, DW_AT_declaration, 1);
19622 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19623 equate_decl_number_to_die (decl, var_die);
19625 if (! declaration
19626 && (! DECL_ABSTRACT (decl_or_origin)
19627 /* Local static vars are shared between all clones/inlines,
19628 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19629 already set. */
19630 || (TREE_CODE (decl_or_origin) == VAR_DECL
19631 && TREE_STATIC (decl_or_origin)
19632 && DECL_RTL_SET_P (decl_or_origin)))
19633 /* When abstract origin already has DW_AT_location attribute, no need
19634 to add it again. */
19635 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19637 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19638 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19639 defer_location (decl_or_origin, var_die);
19640 else
19641 add_location_or_const_value_attribute (var_die,
19642 decl_or_origin,
19643 DW_AT_location);
19644 add_pubname (decl_or_origin, var_die);
19646 else
19647 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19650 /* Generate a DIE to represent a named constant. */
19652 static void
19653 gen_const_die (tree decl, dw_die_ref context_die)
19655 dw_die_ref const_die;
19656 tree type = TREE_TYPE (decl);
19658 const_die = new_die (DW_TAG_constant, context_die, decl);
19659 add_name_and_src_coords_attributes (const_die, decl);
19660 add_type_attribute (const_die, type, 1, 0, context_die);
19661 if (TREE_PUBLIC (decl))
19662 add_AT_flag (const_die, DW_AT_external, 1);
19663 if (DECL_ARTIFICIAL (decl))
19664 add_AT_flag (const_die, DW_AT_artificial, 1);
19665 tree_add_const_value_attribute_for_decl (const_die, decl);
19668 /* Generate a DIE to represent a label identifier. */
19670 static void
19671 gen_label_die (tree decl, dw_die_ref context_die)
19673 tree origin = decl_ultimate_origin (decl);
19674 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19675 rtx insn;
19676 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19678 if (origin != NULL)
19679 add_abstract_origin_attribute (lbl_die, origin);
19680 else
19681 add_name_and_src_coords_attributes (lbl_die, decl);
19683 if (DECL_ABSTRACT (decl))
19684 equate_decl_number_to_die (decl, lbl_die);
19685 else
19687 insn = DECL_RTL_IF_SET (decl);
19689 /* Deleted labels are programmer specified labels which have been
19690 eliminated because of various optimizations. We still emit them
19691 here so that it is possible to put breakpoints on them. */
19692 if (insn
19693 && (LABEL_P (insn)
19694 || ((NOTE_P (insn)
19695 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19697 /* When optimization is enabled (via -O) some parts of the compiler
19698 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19699 represent source-level labels which were explicitly declared by
19700 the user. This really shouldn't be happening though, so catch
19701 it if it ever does happen. */
19702 gcc_assert (!INSN_DELETED_P (insn));
19704 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19705 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19710 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19711 attributes to the DIE for a block STMT, to describe where the inlined
19712 function was called from. This is similar to add_src_coords_attributes. */
19714 static inline void
19715 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19717 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19719 if (dwarf_version >= 3 || !dwarf_strict)
19721 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19722 add_AT_unsigned (die, DW_AT_call_line, s.line);
19727 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19728 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19730 static inline void
19731 add_high_low_attributes (tree stmt, dw_die_ref die)
19733 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19735 if (BLOCK_FRAGMENT_CHAIN (stmt)
19736 && (dwarf_version >= 3 || !dwarf_strict))
19738 tree chain;
19740 if (inlined_function_outer_scope_p (stmt))
19742 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19743 BLOCK_NUMBER (stmt));
19744 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19747 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19749 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19752 add_ranges (chain);
19753 chain = BLOCK_FRAGMENT_CHAIN (chain);
19755 while (chain);
19756 add_ranges (NULL);
19758 else
19760 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19761 BLOCK_NUMBER (stmt));
19762 add_AT_lbl_id (die, DW_AT_low_pc, label);
19763 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19764 BLOCK_NUMBER (stmt));
19765 add_AT_lbl_id (die, DW_AT_high_pc, label);
19769 /* Generate a DIE for a lexical block. */
19771 static void
19772 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19774 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19776 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19777 add_high_low_attributes (stmt, stmt_die);
19779 decls_for_scope (stmt, stmt_die, depth);
19782 /* Generate a DIE for an inlined subprogram. */
19784 static void
19785 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19787 tree decl;
19789 /* The instance of function that is effectively being inlined shall not
19790 be abstract. */
19791 gcc_assert (! BLOCK_ABSTRACT (stmt));
19793 decl = block_ultimate_origin (stmt);
19795 /* Emit info for the abstract instance first, if we haven't yet. We
19796 must emit this even if the block is abstract, otherwise when we
19797 emit the block below (or elsewhere), we may end up trying to emit
19798 a die whose origin die hasn't been emitted, and crashing. */
19799 dwarf2out_abstract_function (decl);
19801 if (! BLOCK_ABSTRACT (stmt))
19803 dw_die_ref subr_die
19804 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19806 add_abstract_origin_attribute (subr_die, decl);
19807 if (TREE_ASM_WRITTEN (stmt))
19808 add_high_low_attributes (stmt, subr_die);
19809 add_call_src_coords_attributes (stmt, subr_die);
19811 decls_for_scope (stmt, subr_die, depth);
19812 current_function_has_inlines = 1;
19816 /* Generate a DIE for a field in a record, or structure. */
19818 static void
19819 gen_field_die (tree decl, dw_die_ref context_die)
19821 dw_die_ref decl_die;
19823 if (TREE_TYPE (decl) == error_mark_node)
19824 return;
19826 decl_die = new_die (DW_TAG_member, context_die, decl);
19827 add_name_and_src_coords_attributes (decl_die, decl);
19828 add_type_attribute (decl_die, member_declared_type (decl),
19829 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19830 context_die);
19832 if (DECL_BIT_FIELD_TYPE (decl))
19834 add_byte_size_attribute (decl_die, decl);
19835 add_bit_size_attribute (decl_die, decl);
19836 add_bit_offset_attribute (decl_die, decl);
19839 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19840 add_data_member_location_attribute (decl_die, decl);
19842 if (DECL_ARTIFICIAL (decl))
19843 add_AT_flag (decl_die, DW_AT_artificial, 1);
19845 add_accessibility_attribute (decl_die, decl);
19847 /* Equate decl number to die, so that we can look up this decl later on. */
19848 equate_decl_number_to_die (decl, decl_die);
19851 #if 0
19852 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19853 Use modified_type_die instead.
19854 We keep this code here just in case these types of DIEs may be needed to
19855 represent certain things in other languages (e.g. Pascal) someday. */
19857 static void
19858 gen_pointer_type_die (tree type, dw_die_ref context_die)
19860 dw_die_ref ptr_die
19861 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19863 equate_type_number_to_die (type, ptr_die);
19864 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19865 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19868 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19869 Use modified_type_die instead.
19870 We keep this code here just in case these types of DIEs may be needed to
19871 represent certain things in other languages (e.g. Pascal) someday. */
19873 static void
19874 gen_reference_type_die (tree type, dw_die_ref context_die)
19876 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19878 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19879 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19880 else
19881 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19883 equate_type_number_to_die (type, ref_die);
19884 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19885 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19887 #endif
19889 /* Generate a DIE for a pointer to a member type. */
19891 static void
19892 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19894 dw_die_ref ptr_die
19895 = new_die (DW_TAG_ptr_to_member_type,
19896 scope_die_for (type, context_die), type);
19898 equate_type_number_to_die (type, ptr_die);
19899 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19900 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19901 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19904 /* Generate the DIE for the compilation unit. */
19906 static dw_die_ref
19907 gen_compile_unit_die (const char *filename)
19909 dw_die_ref die;
19910 char producer[250];
19911 const char *language_string = lang_hooks.name;
19912 int language;
19914 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19916 if (filename)
19918 add_name_attribute (die, filename);
19919 /* Don't add cwd for <built-in>. */
19920 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19921 add_comp_dir_attribute (die);
19924 sprintf (producer, "%s %s", language_string, version_string);
19926 #ifdef MIPS_DEBUGGING_INFO
19927 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19928 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19929 not appear in the producer string, the debugger reaches the conclusion
19930 that the object file is stripped and has no debugging information.
19931 To get the MIPS/SGI debugger to believe that there is debugging
19932 information in the object file, we add a -g to the producer string. */
19933 if (debug_info_level > DINFO_LEVEL_TERSE)
19934 strcat (producer, " -g");
19935 #endif
19937 add_AT_string (die, DW_AT_producer, producer);
19939 /* If our producer is LTO try to figure out a common language to use
19940 from the global list of translation units. */
19941 if (strcmp (language_string, "GNU GIMPLE") == 0)
19943 unsigned i;
19944 tree t;
19945 const char *common_lang = NULL;
19947 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
19949 if (!TRANSLATION_UNIT_LANGUAGE (t))
19950 continue;
19951 if (!common_lang)
19952 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19953 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19955 else if (strncmp (common_lang, "GNU C", 5) == 0
19956 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19957 /* Mixing C and C++ is ok, use C++ in that case. */
19958 common_lang = "GNU C++";
19959 else
19961 /* Fall back to C. */
19962 common_lang = NULL;
19963 break;
19967 if (common_lang)
19968 language_string = common_lang;
19971 language = DW_LANG_C89;
19972 if (strcmp (language_string, "GNU C++") == 0)
19973 language = DW_LANG_C_plus_plus;
19974 else if (strcmp (language_string, "GNU F77") == 0)
19975 language = DW_LANG_Fortran77;
19976 else if (strcmp (language_string, "GNU Pascal") == 0)
19977 language = DW_LANG_Pascal83;
19978 else if (dwarf_version >= 3 || !dwarf_strict)
19980 if (strcmp (language_string, "GNU Ada") == 0)
19981 language = DW_LANG_Ada95;
19982 else if (strcmp (language_string, "GNU Fortran") == 0)
19983 language = DW_LANG_Fortran95;
19984 else if (strcmp (language_string, "GNU Java") == 0)
19985 language = DW_LANG_Java;
19986 else if (strcmp (language_string, "GNU Objective-C") == 0)
19987 language = DW_LANG_ObjC;
19988 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19989 language = DW_LANG_ObjC_plus_plus;
19992 add_AT_unsigned (die, DW_AT_language, language);
19994 switch (language)
19996 case DW_LANG_Fortran77:
19997 case DW_LANG_Fortran90:
19998 case DW_LANG_Fortran95:
19999 /* Fortran has case insensitive identifiers and the front-end
20000 lowercases everything. */
20001 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20002 break;
20003 default:
20004 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20005 break;
20007 return die;
20010 /* Generate the DIE for a base class. */
20012 static void
20013 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20015 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20017 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20018 add_data_member_location_attribute (die, binfo);
20020 if (BINFO_VIRTUAL_P (binfo))
20021 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20023 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20024 children, otherwise the default is DW_ACCESS_public. In DWARF2
20025 the default has always been DW_ACCESS_private. */
20026 if (access == access_public_node)
20028 if (dwarf_version == 2
20029 || context_die->die_tag == DW_TAG_class_type)
20030 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20032 else if (access == access_protected_node)
20033 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20034 else if (dwarf_version > 2
20035 && context_die->die_tag != DW_TAG_class_type)
20036 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20039 /* Generate a DIE for a class member. */
20041 static void
20042 gen_member_die (tree type, dw_die_ref context_die)
20044 tree member;
20045 tree binfo = TYPE_BINFO (type);
20046 dw_die_ref child;
20048 /* If this is not an incomplete type, output descriptions of each of its
20049 members. Note that as we output the DIEs necessary to represent the
20050 members of this record or union type, we will also be trying to output
20051 DIEs to represent the *types* of those members. However the `type'
20052 function (above) will specifically avoid generating type DIEs for member
20053 types *within* the list of member DIEs for this (containing) type except
20054 for those types (of members) which are explicitly marked as also being
20055 members of this (containing) type themselves. The g++ front- end can
20056 force any given type to be treated as a member of some other (containing)
20057 type by setting the TYPE_CONTEXT of the given (member) type to point to
20058 the TREE node representing the appropriate (containing) type. */
20060 /* First output info about the base classes. */
20061 if (binfo)
20063 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20064 int i;
20065 tree base;
20067 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20068 gen_inheritance_die (base,
20069 (accesses ? VEC_index (tree, accesses, i)
20070 : access_public_node), context_die);
20073 /* Now output info about the data members and type members. */
20074 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20076 /* If we thought we were generating minimal debug info for TYPE
20077 and then changed our minds, some of the member declarations
20078 may have already been defined. Don't define them again, but
20079 do put them in the right order. */
20081 child = lookup_decl_die (member);
20082 if (child)
20083 splice_child_die (context_die, child);
20084 else
20085 gen_decl_die (member, NULL, context_die);
20088 /* Now output info about the function members (if any). */
20089 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20091 /* Don't include clones in the member list. */
20092 if (DECL_ABSTRACT_ORIGIN (member))
20093 continue;
20095 child = lookup_decl_die (member);
20096 if (child)
20097 splice_child_die (context_die, child);
20098 else
20099 gen_decl_die (member, NULL, context_die);
20103 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20104 is set, we pretend that the type was never defined, so we only get the
20105 member DIEs needed by later specification DIEs. */
20107 static void
20108 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20109 enum debug_info_usage usage)
20111 dw_die_ref type_die = lookup_type_die (type);
20112 dw_die_ref scope_die = 0;
20113 int nested = 0;
20114 int complete = (TYPE_SIZE (type)
20115 && (! TYPE_STUB_DECL (type)
20116 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20117 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20118 complete = complete && should_emit_struct_debug (type, usage);
20120 if (type_die && ! complete)
20121 return;
20123 if (TYPE_CONTEXT (type) != NULL_TREE
20124 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20125 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20126 nested = 1;
20128 scope_die = scope_die_for (type, context_die);
20130 if (! type_die || (nested && is_cu_die (scope_die)))
20131 /* First occurrence of type or toplevel definition of nested class. */
20133 dw_die_ref old_die = type_die;
20135 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20136 ? record_type_tag (type) : DW_TAG_union_type,
20137 scope_die, type);
20138 equate_type_number_to_die (type, type_die);
20139 if (old_die)
20140 add_AT_specification (type_die, old_die);
20141 else
20142 add_name_attribute (type_die, type_tag (type));
20144 else
20145 remove_AT (type_die, DW_AT_declaration);
20147 /* Generate child dies for template paramaters. */
20148 if (debug_info_level > DINFO_LEVEL_TERSE
20149 && COMPLETE_TYPE_P (type))
20150 gen_generic_params_dies (type);
20152 /* If this type has been completed, then give it a byte_size attribute and
20153 then give a list of members. */
20154 if (complete && !ns_decl)
20156 /* Prevent infinite recursion in cases where the type of some member of
20157 this type is expressed in terms of this type itself. */
20158 TREE_ASM_WRITTEN (type) = 1;
20159 add_byte_size_attribute (type_die, type);
20160 if (TYPE_STUB_DECL (type) != NULL_TREE)
20162 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20163 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20166 /* If the first reference to this type was as the return type of an
20167 inline function, then it may not have a parent. Fix this now. */
20168 if (type_die->die_parent == NULL)
20169 add_child_die (scope_die, type_die);
20171 push_decl_scope (type);
20172 gen_member_die (type, type_die);
20173 pop_decl_scope ();
20175 /* GNU extension: Record what type our vtable lives in. */
20176 if (TYPE_VFIELD (type))
20178 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20180 gen_type_die (vtype, context_die);
20181 add_AT_die_ref (type_die, DW_AT_containing_type,
20182 lookup_type_die (vtype));
20185 else
20187 add_AT_flag (type_die, DW_AT_declaration, 1);
20189 /* We don't need to do this for function-local types. */
20190 if (TYPE_STUB_DECL (type)
20191 && ! decl_function_context (TYPE_STUB_DECL (type)))
20192 VEC_safe_push (tree, gc, incomplete_types, type);
20195 if (get_AT (type_die, DW_AT_name))
20196 add_pubtype (type, type_die);
20199 /* Generate a DIE for a subroutine _type_. */
20201 static void
20202 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20204 tree return_type = TREE_TYPE (type);
20205 dw_die_ref subr_die
20206 = new_die (DW_TAG_subroutine_type,
20207 scope_die_for (type, context_die), type);
20209 equate_type_number_to_die (type, subr_die);
20210 add_prototyped_attribute (subr_die, type);
20211 add_type_attribute (subr_die, return_type, 0, 0, context_die);
20212 gen_formal_types_die (type, subr_die);
20214 if (get_AT (subr_die, DW_AT_name))
20215 add_pubtype (type, subr_die);
20218 /* Generate a DIE for a type definition. */
20220 static void
20221 gen_typedef_die (tree decl, dw_die_ref context_die)
20223 dw_die_ref type_die;
20224 tree origin;
20226 if (TREE_ASM_WRITTEN (decl))
20227 return;
20229 TREE_ASM_WRITTEN (decl) = 1;
20230 type_die = new_die (DW_TAG_typedef, context_die, decl);
20231 origin = decl_ultimate_origin (decl);
20232 if (origin != NULL)
20233 add_abstract_origin_attribute (type_die, origin);
20234 else
20236 tree type;
20238 add_name_and_src_coords_attributes (type_die, decl);
20239 if (DECL_ORIGINAL_TYPE (decl))
20241 type = DECL_ORIGINAL_TYPE (decl);
20243 gcc_assert (type != TREE_TYPE (decl));
20244 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20246 else
20248 type = TREE_TYPE (decl);
20250 if (is_naming_typedef_decl (TYPE_NAME (type)))
20252 /* Here, we are in the case of decl being a typedef naming
20253 an anonymous type, e.g:
20254 typedef struct {...} foo;
20255 In that case TREE_TYPE (decl) is not a typedef variant
20256 type and TYPE_NAME of the anonymous type is set to the
20257 TYPE_DECL of the typedef. This construct is emitted by
20258 the C++ FE.
20260 TYPE is the anonymous struct named by the typedef
20261 DECL. As we need the DW_AT_type attribute of the
20262 DW_TAG_typedef to point to the DIE of TYPE, let's
20263 generate that DIE right away. add_type_attribute
20264 called below will then pick (via lookup_type_die) that
20265 anonymous struct DIE. */
20266 if (!TREE_ASM_WRITTEN (type))
20267 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20271 add_type_attribute (type_die, type, TREE_READONLY (decl),
20272 TREE_THIS_VOLATILE (decl), context_die);
20274 if (is_naming_typedef_decl (decl))
20275 /* We want that all subsequent calls to lookup_type_die with
20276 TYPE in argument yield the DW_TAG_typedef we have just
20277 created. */
20278 equate_type_number_to_die (type, type_die);
20280 add_accessibility_attribute (type_die, decl);
20283 if (DECL_ABSTRACT (decl))
20284 equate_decl_number_to_die (decl, type_die);
20286 if (get_AT (type_die, DW_AT_name))
20287 add_pubtype (decl, type_die);
20290 /* Generate a DIE for a struct, class, enum or union type. */
20292 static void
20293 gen_tagged_type_die (tree type,
20294 dw_die_ref context_die,
20295 enum debug_info_usage usage)
20297 int need_pop;
20299 if (type == NULL_TREE
20300 || !is_tagged_type (type))
20301 return;
20303 /* If this is a nested type whose containing class hasn't been written
20304 out yet, writing it out will cover this one, too. This does not apply
20305 to instantiations of member class templates; they need to be added to
20306 the containing class as they are generated. FIXME: This hurts the
20307 idea of combining type decls from multiple TUs, since we can't predict
20308 what set of template instantiations we'll get. */
20309 if (TYPE_CONTEXT (type)
20310 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20311 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20313 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20315 if (TREE_ASM_WRITTEN (type))
20316 return;
20318 /* If that failed, attach ourselves to the stub. */
20319 push_decl_scope (TYPE_CONTEXT (type));
20320 context_die = lookup_type_die (TYPE_CONTEXT (type));
20321 need_pop = 1;
20323 else if (TYPE_CONTEXT (type) != NULL_TREE
20324 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20326 /* If this type is local to a function that hasn't been written
20327 out yet, use a NULL context for now; it will be fixed up in
20328 decls_for_scope. */
20329 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20330 /* A declaration DIE doesn't count; nested types need to go in the
20331 specification. */
20332 if (context_die && is_declaration_die (context_die))
20333 context_die = NULL;
20334 need_pop = 0;
20336 else
20338 context_die = declare_in_namespace (type, context_die);
20339 need_pop = 0;
20342 if (TREE_CODE (type) == ENUMERAL_TYPE)
20344 /* This might have been written out by the call to
20345 declare_in_namespace. */
20346 if (!TREE_ASM_WRITTEN (type))
20347 gen_enumeration_type_die (type, context_die);
20349 else
20350 gen_struct_or_union_type_die (type, context_die, usage);
20352 if (need_pop)
20353 pop_decl_scope ();
20355 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20356 it up if it is ever completed. gen_*_type_die will set it for us
20357 when appropriate. */
20360 /* Generate a type description DIE. */
20362 static void
20363 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20364 enum debug_info_usage usage)
20366 struct array_descr_info info;
20368 if (type == NULL_TREE || type == error_mark_node)
20369 return;
20371 if (TYPE_NAME (type) != NULL_TREE
20372 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20373 && is_redundant_typedef (TYPE_NAME (type))
20374 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20375 /* The DECL of this type is a typedef we don't want to emit debug
20376 info for but we want debug info for its underlying typedef.
20377 This can happen for e.g, the injected-class-name of a C++
20378 type. */
20379 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20381 /* If TYPE is a typedef type variant, let's generate debug info
20382 for the parent typedef which TYPE is a type of. */
20383 if (typedef_variant_p (type))
20385 if (TREE_ASM_WRITTEN (type))
20386 return;
20388 /* Prevent broken recursion; we can't hand off to the same type. */
20389 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20391 /* Use the DIE of the containing namespace as the parent DIE of
20392 the type description DIE we want to generate. */
20393 if (DECL_CONTEXT (TYPE_NAME (type))
20394 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20395 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20397 TREE_ASM_WRITTEN (type) = 1;
20399 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20400 return;
20403 /* If type is an anonymous tagged type named by a typedef, let's
20404 generate debug info for the typedef. */
20405 if (is_naming_typedef_decl (TYPE_NAME (type)))
20407 /* Use the DIE of the containing namespace as the parent DIE of
20408 the type description DIE we want to generate. */
20409 if (DECL_CONTEXT (TYPE_NAME (type))
20410 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20411 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20413 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20414 return;
20417 /* If this is an array type with hidden descriptor, handle it first. */
20418 if (!TREE_ASM_WRITTEN (type)
20419 && lang_hooks.types.get_array_descr_info
20420 && lang_hooks.types.get_array_descr_info (type, &info)
20421 && (dwarf_version >= 3 || !dwarf_strict))
20423 gen_descr_array_type_die (type, &info, context_die);
20424 TREE_ASM_WRITTEN (type) = 1;
20425 return;
20428 /* We are going to output a DIE to represent the unqualified version
20429 of this type (i.e. without any const or volatile qualifiers) so
20430 get the main variant (i.e. the unqualified version) of this type
20431 now. (Vectors are special because the debugging info is in the
20432 cloned type itself). */
20433 if (TREE_CODE (type) != VECTOR_TYPE)
20434 type = type_main_variant (type);
20436 if (TREE_ASM_WRITTEN (type))
20437 return;
20439 switch (TREE_CODE (type))
20441 case ERROR_MARK:
20442 break;
20444 case POINTER_TYPE:
20445 case REFERENCE_TYPE:
20446 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20447 ensures that the gen_type_die recursion will terminate even if the
20448 type is recursive. Recursive types are possible in Ada. */
20449 /* ??? We could perhaps do this for all types before the switch
20450 statement. */
20451 TREE_ASM_WRITTEN (type) = 1;
20453 /* For these types, all that is required is that we output a DIE (or a
20454 set of DIEs) to represent the "basis" type. */
20455 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20456 DINFO_USAGE_IND_USE);
20457 break;
20459 case OFFSET_TYPE:
20460 /* This code is used for C++ pointer-to-data-member types.
20461 Output a description of the relevant class type. */
20462 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20463 DINFO_USAGE_IND_USE);
20465 /* Output a description of the type of the object pointed to. */
20466 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20467 DINFO_USAGE_IND_USE);
20469 /* Now output a DIE to represent this pointer-to-data-member type
20470 itself. */
20471 gen_ptr_to_mbr_type_die (type, context_die);
20472 break;
20474 case FUNCTION_TYPE:
20475 /* Force out return type (in case it wasn't forced out already). */
20476 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20477 DINFO_USAGE_DIR_USE);
20478 gen_subroutine_type_die (type, context_die);
20479 break;
20481 case METHOD_TYPE:
20482 /* Force out return type (in case it wasn't forced out already). */
20483 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20484 DINFO_USAGE_DIR_USE);
20485 gen_subroutine_type_die (type, context_die);
20486 break;
20488 case ARRAY_TYPE:
20489 gen_array_type_die (type, context_die);
20490 break;
20492 case VECTOR_TYPE:
20493 gen_array_type_die (type, context_die);
20494 break;
20496 case ENUMERAL_TYPE:
20497 case RECORD_TYPE:
20498 case UNION_TYPE:
20499 case QUAL_UNION_TYPE:
20500 gen_tagged_type_die (type, context_die, usage);
20501 return;
20503 case VOID_TYPE:
20504 case INTEGER_TYPE:
20505 case REAL_TYPE:
20506 case FIXED_POINT_TYPE:
20507 case COMPLEX_TYPE:
20508 case BOOLEAN_TYPE:
20509 /* No DIEs needed for fundamental types. */
20510 break;
20512 case NULLPTR_TYPE:
20513 case LANG_TYPE:
20514 /* Just use DW_TAG_unspecified_type. */
20516 dw_die_ref type_die = lookup_type_die (type);
20517 if (type_die == NULL)
20519 tree name = TYPE_NAME (type);
20520 if (TREE_CODE (name) == TYPE_DECL)
20521 name = DECL_NAME (name);
20522 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20523 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20524 equate_type_number_to_die (type, type_die);
20527 break;
20529 default:
20530 gcc_unreachable ();
20533 TREE_ASM_WRITTEN (type) = 1;
20536 static void
20537 gen_type_die (tree type, dw_die_ref context_die)
20539 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20542 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20543 things which are local to the given block. */
20545 static void
20546 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20548 int must_output_die = 0;
20549 bool inlined_func;
20551 /* Ignore blocks that are NULL. */
20552 if (stmt == NULL_TREE)
20553 return;
20555 inlined_func = inlined_function_outer_scope_p (stmt);
20557 /* If the block is one fragment of a non-contiguous block, do not
20558 process the variables, since they will have been done by the
20559 origin block. Do process subblocks. */
20560 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20562 tree sub;
20564 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20565 gen_block_die (sub, context_die, depth + 1);
20567 return;
20570 /* Determine if we need to output any Dwarf DIEs at all to represent this
20571 block. */
20572 if (inlined_func)
20573 /* The outer scopes for inlinings *must* always be represented. We
20574 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20575 must_output_die = 1;
20576 else
20578 /* Determine if this block directly contains any "significant"
20579 local declarations which we will need to output DIEs for. */
20580 if (debug_info_level > DINFO_LEVEL_TERSE)
20581 /* We are not in terse mode so *any* local declaration counts
20582 as being a "significant" one. */
20583 must_output_die = ((BLOCK_VARS (stmt) != NULL
20584 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20585 && (TREE_USED (stmt)
20586 || TREE_ASM_WRITTEN (stmt)
20587 || BLOCK_ABSTRACT (stmt)));
20588 else if ((TREE_USED (stmt)
20589 || TREE_ASM_WRITTEN (stmt)
20590 || BLOCK_ABSTRACT (stmt))
20591 && !dwarf2out_ignore_block (stmt))
20592 must_output_die = 1;
20595 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20596 DIE for any block which contains no significant local declarations at
20597 all. Rather, in such cases we just call `decls_for_scope' so that any
20598 needed Dwarf info for any sub-blocks will get properly generated. Note
20599 that in terse mode, our definition of what constitutes a "significant"
20600 local declaration gets restricted to include only inlined function
20601 instances and local (nested) function definitions. */
20602 if (must_output_die)
20604 if (inlined_func)
20606 /* If STMT block is abstract, that means we have been called
20607 indirectly from dwarf2out_abstract_function.
20608 That function rightfully marks the descendent blocks (of
20609 the abstract function it is dealing with) as being abstract,
20610 precisely to prevent us from emitting any
20611 DW_TAG_inlined_subroutine DIE as a descendent
20612 of an abstract function instance. So in that case, we should
20613 not call gen_inlined_subroutine_die.
20615 Later though, when cgraph asks dwarf2out to emit info
20616 for the concrete instance of the function decl into which
20617 the concrete instance of STMT got inlined, the later will lead
20618 to the generation of a DW_TAG_inlined_subroutine DIE. */
20619 if (! BLOCK_ABSTRACT (stmt))
20620 gen_inlined_subroutine_die (stmt, context_die, depth);
20622 else
20623 gen_lexical_block_die (stmt, context_die, depth);
20625 else
20626 decls_for_scope (stmt, context_die, depth);
20629 /* Process variable DECL (or variable with origin ORIGIN) within
20630 block STMT and add it to CONTEXT_DIE. */
20631 static void
20632 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20634 dw_die_ref die;
20635 tree decl_or_origin = decl ? decl : origin;
20637 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20638 die = lookup_decl_die (decl_or_origin);
20639 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20640 && TYPE_DECL_IS_STUB (decl_or_origin))
20641 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20642 else
20643 die = NULL;
20645 if (die != NULL && die->die_parent == NULL)
20646 add_child_die (context_die, die);
20647 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20648 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20649 stmt, context_die);
20650 else
20651 gen_decl_die (decl, origin, context_die);
20654 /* Generate all of the decls declared within a given scope and (recursively)
20655 all of its sub-blocks. */
20657 static void
20658 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20660 tree decl;
20661 unsigned int i;
20662 tree subblocks;
20664 /* Ignore NULL blocks. */
20665 if (stmt == NULL_TREE)
20666 return;
20668 /* Output the DIEs to represent all of the data objects and typedefs
20669 declared directly within this block but not within any nested
20670 sub-blocks. Also, nested function and tag DIEs have been
20671 generated with a parent of NULL; fix that up now. */
20672 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20673 process_scope_var (stmt, decl, NULL_TREE, context_die);
20674 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20675 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20676 context_die);
20678 /* If we're at -g1, we're not interested in subblocks. */
20679 if (debug_info_level <= DINFO_LEVEL_TERSE)
20680 return;
20682 /* Output the DIEs to represent all sub-blocks (and the items declared
20683 therein) of this block. */
20684 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20685 subblocks != NULL;
20686 subblocks = BLOCK_CHAIN (subblocks))
20687 gen_block_die (subblocks, context_die, depth + 1);
20690 /* Is this a typedef we can avoid emitting? */
20692 static inline int
20693 is_redundant_typedef (const_tree decl)
20695 if (TYPE_DECL_IS_STUB (decl))
20696 return 1;
20698 if (DECL_ARTIFICIAL (decl)
20699 && DECL_CONTEXT (decl)
20700 && is_tagged_type (DECL_CONTEXT (decl))
20701 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20702 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20703 /* Also ignore the artificial member typedef for the class name. */
20704 return 1;
20706 return 0;
20709 /* Return TRUE if TYPE is a typedef that names a type for linkage
20710 purposes. This kind of typedefs is produced by the C++ FE for
20711 constructs like:
20713 typedef struct {...} foo;
20715 In that case, there is no typedef variant type produced for foo.
20716 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20717 struct type. */
20719 static bool
20720 is_naming_typedef_decl (const_tree decl)
20722 if (decl == NULL_TREE
20723 || TREE_CODE (decl) != TYPE_DECL
20724 || !is_tagged_type (TREE_TYPE (decl))
20725 || DECL_IS_BUILTIN (decl)
20726 || is_redundant_typedef (decl)
20727 /* It looks like Ada produces TYPE_DECLs that are very similar
20728 to C++ naming typedefs but that have different
20729 semantics. Let's be specific to c++ for now. */
20730 || !is_cxx ())
20731 return FALSE;
20733 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20734 && TYPE_NAME (TREE_TYPE (decl)) == decl
20735 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20736 != TYPE_NAME (TREE_TYPE (decl))));
20739 /* Returns the DIE for a context. */
20741 static inline dw_die_ref
20742 get_context_die (tree context)
20744 if (context)
20746 /* Find die that represents this context. */
20747 if (TYPE_P (context))
20748 return force_type_die (TYPE_MAIN_VARIANT (context));
20749 else
20750 return force_decl_die (context);
20752 return comp_unit_die ();
20755 /* Returns the DIE for decl. A DIE will always be returned. */
20757 static dw_die_ref
20758 force_decl_die (tree decl)
20760 dw_die_ref decl_die;
20761 unsigned saved_external_flag;
20762 tree save_fn = NULL_TREE;
20763 decl_die = lookup_decl_die (decl);
20764 if (!decl_die)
20766 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20768 decl_die = lookup_decl_die (decl);
20769 if (decl_die)
20770 return decl_die;
20772 switch (TREE_CODE (decl))
20774 case FUNCTION_DECL:
20775 /* Clear current_function_decl, so that gen_subprogram_die thinks
20776 that this is a declaration. At this point, we just want to force
20777 declaration die. */
20778 save_fn = current_function_decl;
20779 current_function_decl = NULL_TREE;
20780 gen_subprogram_die (decl, context_die);
20781 current_function_decl = save_fn;
20782 break;
20784 case VAR_DECL:
20785 /* Set external flag to force declaration die. Restore it after
20786 gen_decl_die() call. */
20787 saved_external_flag = DECL_EXTERNAL (decl);
20788 DECL_EXTERNAL (decl) = 1;
20789 gen_decl_die (decl, NULL, context_die);
20790 DECL_EXTERNAL (decl) = saved_external_flag;
20791 break;
20793 case NAMESPACE_DECL:
20794 if (dwarf_version >= 3 || !dwarf_strict)
20795 dwarf2out_decl (decl);
20796 else
20797 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20798 decl_die = comp_unit_die ();
20799 break;
20801 case TRANSLATION_UNIT_DECL:
20802 decl_die = comp_unit_die ();
20803 break;
20805 default:
20806 gcc_unreachable ();
20809 /* We should be able to find the DIE now. */
20810 if (!decl_die)
20811 decl_die = lookup_decl_die (decl);
20812 gcc_assert (decl_die);
20815 return decl_die;
20818 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20819 always returned. */
20821 static dw_die_ref
20822 force_type_die (tree type)
20824 dw_die_ref type_die;
20826 type_die = lookup_type_die (type);
20827 if (!type_die)
20829 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20831 type_die = modified_type_die (type, TYPE_READONLY (type),
20832 TYPE_VOLATILE (type), context_die);
20833 gcc_assert (type_die);
20835 return type_die;
20838 /* Force out any required namespaces to be able to output DECL,
20839 and return the new context_die for it, if it's changed. */
20841 static dw_die_ref
20842 setup_namespace_context (tree thing, dw_die_ref context_die)
20844 tree context = (DECL_P (thing)
20845 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20846 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20847 /* Force out the namespace. */
20848 context_die = force_decl_die (context);
20850 return context_die;
20853 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20854 type) within its namespace, if appropriate.
20856 For compatibility with older debuggers, namespace DIEs only contain
20857 declarations; all definitions are emitted at CU scope. */
20859 static dw_die_ref
20860 declare_in_namespace (tree thing, dw_die_ref context_die)
20862 dw_die_ref ns_context;
20864 if (debug_info_level <= DINFO_LEVEL_TERSE)
20865 return context_die;
20867 /* If this decl is from an inlined function, then don't try to emit it in its
20868 namespace, as we will get confused. It would have already been emitted
20869 when the abstract instance of the inline function was emitted anyways. */
20870 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20871 return context_die;
20873 ns_context = setup_namespace_context (thing, context_die);
20875 if (ns_context != context_die)
20877 if (is_fortran ())
20878 return ns_context;
20879 if (DECL_P (thing))
20880 gen_decl_die (thing, NULL, ns_context);
20881 else
20882 gen_type_die (thing, ns_context);
20884 return context_die;
20887 /* Generate a DIE for a namespace or namespace alias. */
20889 static void
20890 gen_namespace_die (tree decl, dw_die_ref context_die)
20892 dw_die_ref namespace_die;
20894 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20895 they are an alias of. */
20896 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20898 /* Output a real namespace or module. */
20899 context_die = setup_namespace_context (decl, comp_unit_die ());
20900 namespace_die = new_die (is_fortran ()
20901 ? DW_TAG_module : DW_TAG_namespace,
20902 context_die, decl);
20903 /* For Fortran modules defined in different CU don't add src coords. */
20904 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20906 const char *name = dwarf2_name (decl, 0);
20907 if (name)
20908 add_name_attribute (namespace_die, name);
20910 else
20911 add_name_and_src_coords_attributes (namespace_die, decl);
20912 if (DECL_EXTERNAL (decl))
20913 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20914 equate_decl_number_to_die (decl, namespace_die);
20916 else
20918 /* Output a namespace alias. */
20920 /* Force out the namespace we are an alias of, if necessary. */
20921 dw_die_ref origin_die
20922 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20924 if (DECL_FILE_SCOPE_P (decl)
20925 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20926 context_die = setup_namespace_context (decl, comp_unit_die ());
20927 /* Now create the namespace alias DIE. */
20928 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20929 add_name_and_src_coords_attributes (namespace_die, decl);
20930 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20931 equate_decl_number_to_die (decl, namespace_die);
20935 /* Generate Dwarf debug information for a decl described by DECL.
20936 The return value is currently only meaningful for PARM_DECLs,
20937 for all other decls it returns NULL. */
20939 static dw_die_ref
20940 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20942 tree decl_or_origin = decl ? decl : origin;
20943 tree class_origin = NULL, ultimate_origin;
20945 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20946 return NULL;
20948 switch (TREE_CODE (decl_or_origin))
20950 case ERROR_MARK:
20951 break;
20953 case CONST_DECL:
20954 if (!is_fortran () && !is_ada ())
20956 /* The individual enumerators of an enum type get output when we output
20957 the Dwarf representation of the relevant enum type itself. */
20958 break;
20961 /* Emit its type. */
20962 gen_type_die (TREE_TYPE (decl), context_die);
20964 /* And its containing namespace. */
20965 context_die = declare_in_namespace (decl, context_die);
20967 gen_const_die (decl, context_die);
20968 break;
20970 case FUNCTION_DECL:
20971 /* Don't output any DIEs to represent mere function declarations,
20972 unless they are class members or explicit block externs. */
20973 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20974 && DECL_FILE_SCOPE_P (decl_or_origin)
20975 && (current_function_decl == NULL_TREE
20976 || DECL_ARTIFICIAL (decl_or_origin)))
20977 break;
20979 #if 0
20980 /* FIXME */
20981 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20982 on local redeclarations of global functions. That seems broken. */
20983 if (current_function_decl != decl)
20984 /* This is only a declaration. */;
20985 #endif
20987 /* If we're emitting a clone, emit info for the abstract instance. */
20988 if (origin || DECL_ORIGIN (decl) != decl)
20989 dwarf2out_abstract_function (origin
20990 ? DECL_ORIGIN (origin)
20991 : DECL_ABSTRACT_ORIGIN (decl));
20993 /* If we're emitting an out-of-line copy of an inline function,
20994 emit info for the abstract instance and set up to refer to it. */
20995 else if (cgraph_function_possibly_inlined_p (decl)
20996 && ! DECL_ABSTRACT (decl)
20997 && ! class_or_namespace_scope_p (context_die)
20998 /* dwarf2out_abstract_function won't emit a die if this is just
20999 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21000 that case, because that works only if we have a die. */
21001 && DECL_INITIAL (decl) != NULL_TREE)
21003 dwarf2out_abstract_function (decl);
21004 set_decl_origin_self (decl);
21007 /* Otherwise we're emitting the primary DIE for this decl. */
21008 else if (debug_info_level > DINFO_LEVEL_TERSE)
21010 /* Before we describe the FUNCTION_DECL itself, make sure that we
21011 have its containing type. */
21012 if (!origin)
21013 origin = decl_class_context (decl);
21014 if (origin != NULL_TREE)
21015 gen_type_die (origin, context_die);
21017 /* And its return type. */
21018 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21020 /* And its virtual context. */
21021 if (DECL_VINDEX (decl) != NULL_TREE)
21022 gen_type_die (DECL_CONTEXT (decl), context_die);
21024 /* Make sure we have a member DIE for decl. */
21025 if (origin != NULL_TREE)
21026 gen_type_die_for_member (origin, decl, context_die);
21028 /* And its containing namespace. */
21029 context_die = declare_in_namespace (decl, context_die);
21032 /* Now output a DIE to represent the function itself. */
21033 if (decl)
21034 gen_subprogram_die (decl, context_die);
21035 break;
21037 case TYPE_DECL:
21038 /* If we are in terse mode, don't generate any DIEs to represent any
21039 actual typedefs. */
21040 if (debug_info_level <= DINFO_LEVEL_TERSE)
21041 break;
21043 /* In the special case of a TYPE_DECL node representing the declaration
21044 of some type tag, if the given TYPE_DECL is marked as having been
21045 instantiated from some other (original) TYPE_DECL node (e.g. one which
21046 was generated within the original definition of an inline function) we
21047 used to generate a special (abbreviated) DW_TAG_structure_type,
21048 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21049 should be actually referencing those DIEs, as variable DIEs with that
21050 type would be emitted already in the abstract origin, so it was always
21051 removed during unused type prunning. Don't add anything in this
21052 case. */
21053 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21054 break;
21056 if (is_redundant_typedef (decl))
21057 gen_type_die (TREE_TYPE (decl), context_die);
21058 else
21059 /* Output a DIE to represent the typedef itself. */
21060 gen_typedef_die (decl, context_die);
21061 break;
21063 case LABEL_DECL:
21064 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21065 gen_label_die (decl, context_die);
21066 break;
21068 case VAR_DECL:
21069 case RESULT_DECL:
21070 /* If we are in terse mode, don't generate any DIEs to represent any
21071 variable declarations or definitions. */
21072 if (debug_info_level <= DINFO_LEVEL_TERSE)
21073 break;
21075 /* Output any DIEs that are needed to specify the type of this data
21076 object. */
21077 if (decl_by_reference_p (decl_or_origin))
21078 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21079 else
21080 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21082 /* And its containing type. */
21083 class_origin = decl_class_context (decl_or_origin);
21084 if (class_origin != NULL_TREE)
21085 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21087 /* And its containing namespace. */
21088 context_die = declare_in_namespace (decl_or_origin, context_die);
21090 /* Now output the DIE to represent the data object itself. This gets
21091 complicated because of the possibility that the VAR_DECL really
21092 represents an inlined instance of a formal parameter for an inline
21093 function. */
21094 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21095 if (ultimate_origin != NULL_TREE
21096 && TREE_CODE (ultimate_origin) == PARM_DECL)
21097 gen_formal_parameter_die (decl, origin,
21098 true /* Emit name attribute. */,
21099 context_die);
21100 else
21101 gen_variable_die (decl, origin, context_die);
21102 break;
21104 case FIELD_DECL:
21105 /* Ignore the nameless fields that are used to skip bits but handle C++
21106 anonymous unions and structs. */
21107 if (DECL_NAME (decl) != NULL_TREE
21108 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21109 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21111 gen_type_die (member_declared_type (decl), context_die);
21112 gen_field_die (decl, context_die);
21114 break;
21116 case PARM_DECL:
21117 if (DECL_BY_REFERENCE (decl_or_origin))
21118 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21119 else
21120 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21121 return gen_formal_parameter_die (decl, origin,
21122 true /* Emit name attribute. */,
21123 context_die);
21125 case NAMESPACE_DECL:
21126 case IMPORTED_DECL:
21127 if (dwarf_version >= 3 || !dwarf_strict)
21128 gen_namespace_die (decl, context_die);
21129 break;
21131 default:
21132 /* Probably some frontend-internal decl. Assume we don't care. */
21133 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21134 break;
21137 return NULL;
21140 /* Output debug information for global decl DECL. Called from toplev.c after
21141 compilation proper has finished. */
21143 static void
21144 dwarf2out_global_decl (tree decl)
21146 /* Output DWARF2 information for file-scope tentative data object
21147 declarations, file-scope (extern) function declarations (which
21148 had no corresponding body) and file-scope tagged type declarations
21149 and definitions which have not yet been forced out. */
21150 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21151 dwarf2out_decl (decl);
21154 /* Output debug information for type decl DECL. Called from toplev.c
21155 and from language front ends (to record built-in types). */
21156 static void
21157 dwarf2out_type_decl (tree decl, int local)
21159 if (!local)
21160 dwarf2out_decl (decl);
21163 /* Output debug information for imported module or decl DECL.
21164 NAME is non-NULL name in the lexical block if the decl has been renamed.
21165 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21166 that DECL belongs to.
21167 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21168 static void
21169 dwarf2out_imported_module_or_decl_1 (tree decl,
21170 tree name,
21171 tree lexical_block,
21172 dw_die_ref lexical_block_die)
21174 expanded_location xloc;
21175 dw_die_ref imported_die = NULL;
21176 dw_die_ref at_import_die;
21178 if (TREE_CODE (decl) == IMPORTED_DECL)
21180 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21181 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21182 gcc_assert (decl);
21184 else
21185 xloc = expand_location (input_location);
21187 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21189 at_import_die = force_type_die (TREE_TYPE (decl));
21190 /* For namespace N { typedef void T; } using N::T; base_type_die
21191 returns NULL, but DW_TAG_imported_declaration requires
21192 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21193 if (!at_import_die)
21195 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21196 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21197 at_import_die = lookup_type_die (TREE_TYPE (decl));
21198 gcc_assert (at_import_die);
21201 else
21203 at_import_die = lookup_decl_die (decl);
21204 if (!at_import_die)
21206 /* If we're trying to avoid duplicate debug info, we may not have
21207 emitted the member decl for this field. Emit it now. */
21208 if (TREE_CODE (decl) == FIELD_DECL)
21210 tree type = DECL_CONTEXT (decl);
21212 if (TYPE_CONTEXT (type)
21213 && TYPE_P (TYPE_CONTEXT (type))
21214 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21215 DINFO_USAGE_DIR_USE))
21216 return;
21217 gen_type_die_for_member (type, decl,
21218 get_context_die (TYPE_CONTEXT (type)));
21220 at_import_die = force_decl_die (decl);
21224 if (TREE_CODE (decl) == NAMESPACE_DECL)
21226 if (dwarf_version >= 3 || !dwarf_strict)
21227 imported_die = new_die (DW_TAG_imported_module,
21228 lexical_block_die,
21229 lexical_block);
21230 else
21231 return;
21233 else
21234 imported_die = new_die (DW_TAG_imported_declaration,
21235 lexical_block_die,
21236 lexical_block);
21238 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21239 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21240 if (name)
21241 add_AT_string (imported_die, DW_AT_name,
21242 IDENTIFIER_POINTER (name));
21243 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21246 /* Output debug information for imported module or decl DECL.
21247 NAME is non-NULL name in context if the decl has been renamed.
21248 CHILD is true if decl is one of the renamed decls as part of
21249 importing whole module. */
21251 static void
21252 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21253 bool child)
21255 /* dw_die_ref at_import_die; */
21256 dw_die_ref scope_die;
21258 if (debug_info_level <= DINFO_LEVEL_TERSE)
21259 return;
21261 gcc_assert (decl);
21263 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21264 We need decl DIE for reference and scope die. First, get DIE for the decl
21265 itself. */
21267 /* Get the scope die for decl context. Use comp_unit_die for global module
21268 or decl. If die is not found for non globals, force new die. */
21269 if (context
21270 && TYPE_P (context)
21271 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21272 return;
21274 if (!(dwarf_version >= 3 || !dwarf_strict))
21275 return;
21277 scope_die = get_context_die (context);
21279 if (child)
21281 gcc_assert (scope_die->die_child);
21282 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21283 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21284 scope_die = scope_die->die_child;
21287 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21288 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21292 /* Write the debugging output for DECL. */
21294 void
21295 dwarf2out_decl (tree decl)
21297 dw_die_ref context_die = comp_unit_die ();
21299 switch (TREE_CODE (decl))
21301 case ERROR_MARK:
21302 return;
21304 case FUNCTION_DECL:
21305 /* What we would really like to do here is to filter out all mere
21306 file-scope declarations of file-scope functions which are never
21307 referenced later within this translation unit (and keep all of ones
21308 that *are* referenced later on) but we aren't clairvoyant, so we have
21309 no idea which functions will be referenced in the future (i.e. later
21310 on within the current translation unit). So here we just ignore all
21311 file-scope function declarations which are not also definitions. If
21312 and when the debugger needs to know something about these functions,
21313 it will have to hunt around and find the DWARF information associated
21314 with the definition of the function.
21316 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21317 nodes represent definitions and which ones represent mere
21318 declarations. We have to check DECL_INITIAL instead. That's because
21319 the C front-end supports some weird semantics for "extern inline"
21320 function definitions. These can get inlined within the current
21321 translation unit (and thus, we need to generate Dwarf info for their
21322 abstract instances so that the Dwarf info for the concrete inlined
21323 instances can have something to refer to) but the compiler never
21324 generates any out-of-lines instances of such things (despite the fact
21325 that they *are* definitions).
21327 The important point is that the C front-end marks these "extern
21328 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21329 them anyway. Note that the C++ front-end also plays some similar games
21330 for inline function definitions appearing within include files which
21331 also contain `#pragma interface' pragmas. */
21332 if (DECL_INITIAL (decl) == NULL_TREE)
21333 return;
21335 /* If we're a nested function, initially use a parent of NULL; if we're
21336 a plain function, this will be fixed up in decls_for_scope. If
21337 we're a method, it will be ignored, since we already have a DIE. */
21338 if (decl_function_context (decl)
21339 /* But if we're in terse mode, we don't care about scope. */
21340 && debug_info_level > DINFO_LEVEL_TERSE)
21341 context_die = NULL;
21342 break;
21344 case VAR_DECL:
21345 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21346 declaration and if the declaration was never even referenced from
21347 within this entire compilation unit. We suppress these DIEs in
21348 order to save space in the .debug section (by eliminating entries
21349 which are probably useless). Note that we must not suppress
21350 block-local extern declarations (whether used or not) because that
21351 would screw-up the debugger's name lookup mechanism and cause it to
21352 miss things which really ought to be in scope at a given point. */
21353 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21354 return;
21356 /* For local statics lookup proper context die. */
21357 if (TREE_STATIC (decl) && decl_function_context (decl))
21358 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21360 /* If we are in terse mode, don't generate any DIEs to represent any
21361 variable declarations or definitions. */
21362 if (debug_info_level <= DINFO_LEVEL_TERSE)
21363 return;
21364 break;
21366 case CONST_DECL:
21367 if (debug_info_level <= DINFO_LEVEL_TERSE)
21368 return;
21369 if (!is_fortran () && !is_ada ())
21370 return;
21371 if (TREE_STATIC (decl) && decl_function_context (decl))
21372 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21373 break;
21375 case NAMESPACE_DECL:
21376 case IMPORTED_DECL:
21377 if (debug_info_level <= DINFO_LEVEL_TERSE)
21378 return;
21379 if (lookup_decl_die (decl) != NULL)
21380 return;
21381 break;
21383 case TYPE_DECL:
21384 /* Don't emit stubs for types unless they are needed by other DIEs. */
21385 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21386 return;
21388 /* Don't bother trying to generate any DIEs to represent any of the
21389 normal built-in types for the language we are compiling. */
21390 if (DECL_IS_BUILTIN (decl))
21391 return;
21393 /* If we are in terse mode, don't generate any DIEs for types. */
21394 if (debug_info_level <= DINFO_LEVEL_TERSE)
21395 return;
21397 /* If we're a function-scope tag, initially use a parent of NULL;
21398 this will be fixed up in decls_for_scope. */
21399 if (decl_function_context (decl))
21400 context_die = NULL;
21402 break;
21404 default:
21405 return;
21408 gen_decl_die (decl, NULL, context_die);
21411 /* Write the debugging output for DECL. */
21413 static void
21414 dwarf2out_function_decl (tree decl)
21416 dwarf2out_decl (decl);
21418 htab_empty (decl_loc_table);
21421 /* Output a marker (i.e. a label) for the beginning of the generated code for
21422 a lexical block. */
21424 static void
21425 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21426 unsigned int blocknum)
21428 switch_to_section (current_function_section ());
21429 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21432 /* Output a marker (i.e. a label) for the end of the generated code for a
21433 lexical block. */
21435 static void
21436 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21438 switch_to_section (current_function_section ());
21439 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21442 /* Returns nonzero if it is appropriate not to emit any debugging
21443 information for BLOCK, because it doesn't contain any instructions.
21445 Don't allow this for blocks with nested functions or local classes
21446 as we would end up with orphans, and in the presence of scheduling
21447 we may end up calling them anyway. */
21449 static bool
21450 dwarf2out_ignore_block (const_tree block)
21452 tree decl;
21453 unsigned int i;
21455 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21456 if (TREE_CODE (decl) == FUNCTION_DECL
21457 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21458 return 0;
21459 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21461 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21462 if (TREE_CODE (decl) == FUNCTION_DECL
21463 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21464 return 0;
21467 return 1;
21470 /* Hash table routines for file_hash. */
21472 static int
21473 file_table_eq (const void *p1_p, const void *p2_p)
21475 const struct dwarf_file_data *const p1 =
21476 (const struct dwarf_file_data *) p1_p;
21477 const char *const p2 = (const char *) p2_p;
21478 return strcmp (p1->filename, p2) == 0;
21481 static hashval_t
21482 file_table_hash (const void *p_p)
21484 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21485 return htab_hash_string (p->filename);
21488 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21489 dwarf2out.c) and return its "index". The index of each (known) filename is
21490 just a unique number which is associated with only that one filename. We
21491 need such numbers for the sake of generating labels (in the .debug_sfnames
21492 section) and references to those files numbers (in the .debug_srcinfo
21493 and.debug_macinfo sections). If the filename given as an argument is not
21494 found in our current list, add it to the list and assign it the next
21495 available unique index number. In order to speed up searches, we remember
21496 the index of the filename was looked up last. This handles the majority of
21497 all searches. */
21499 static struct dwarf_file_data *
21500 lookup_filename (const char *file_name)
21502 void ** slot;
21503 struct dwarf_file_data * created;
21505 /* Check to see if the file name that was searched on the previous
21506 call matches this file name. If so, return the index. */
21507 if (file_table_last_lookup
21508 && (file_name == file_table_last_lookup->filename
21509 || strcmp (file_table_last_lookup->filename, file_name) == 0))
21510 return file_table_last_lookup;
21512 /* Didn't match the previous lookup, search the table. */
21513 slot = htab_find_slot_with_hash (file_table, file_name,
21514 htab_hash_string (file_name), INSERT);
21515 if (*slot)
21516 return (struct dwarf_file_data *) *slot;
21518 created = ggc_alloc_dwarf_file_data ();
21519 created->filename = file_name;
21520 created->emitted_number = 0;
21521 *slot = created;
21522 return created;
21525 /* If the assembler will construct the file table, then translate the compiler
21526 internal file table number into the assembler file table number, and emit
21527 a .file directive if we haven't already emitted one yet. The file table
21528 numbers are different because we prune debug info for unused variables and
21529 types, which may include filenames. */
21531 static int
21532 maybe_emit_file (struct dwarf_file_data * fd)
21534 if (! fd->emitted_number)
21536 if (last_emitted_file)
21537 fd->emitted_number = last_emitted_file->emitted_number + 1;
21538 else
21539 fd->emitted_number = 1;
21540 last_emitted_file = fd;
21542 if (DWARF2_ASM_LINE_DEBUG_INFO)
21544 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21545 output_quoted_string (asm_out_file,
21546 remap_debug_filename (fd->filename));
21547 fputc ('\n', asm_out_file);
21551 return fd->emitted_number;
21554 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21555 That generation should happen after function debug info has been
21556 generated. The value of the attribute is the constant value of ARG. */
21558 static void
21559 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21561 die_arg_entry entry;
21563 if (!die || !arg)
21564 return;
21566 if (!tmpl_value_parm_die_table)
21567 tmpl_value_parm_die_table
21568 = VEC_alloc (die_arg_entry, gc, 32);
21570 entry.die = die;
21571 entry.arg = arg;
21572 VEC_safe_push (die_arg_entry, gc,
21573 tmpl_value_parm_die_table,
21574 &entry);
21577 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21578 by append_entry_to_tmpl_value_parm_die_table. This function must
21579 be called after function DIEs have been generated. */
21581 static void
21582 gen_remaining_tmpl_value_param_die_attribute (void)
21584 if (tmpl_value_parm_die_table)
21586 unsigned i;
21587 die_arg_entry *e;
21589 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21590 tree_add_const_value_attribute (e->die, e->arg);
21595 /* Replace DW_AT_name for the decl with name. */
21597 static void
21598 dwarf2out_set_name (tree decl, tree name)
21600 dw_die_ref die;
21601 dw_attr_ref attr;
21602 const char *dname;
21604 die = TYPE_SYMTAB_DIE (decl);
21605 if (!die)
21606 return;
21608 dname = dwarf2_name (name, 0);
21609 if (!dname)
21610 return;
21612 attr = get_AT (die, DW_AT_name);
21613 if (attr)
21615 struct indirect_string_node *node;
21617 node = find_AT_string (dname);
21618 /* replace the string. */
21619 attr->dw_attr_val.v.val_str = node;
21622 else
21623 add_name_attribute (die, dname);
21626 /* Called by the final INSN scan whenever we see a direct function call.
21627 Make an entry into the direct call table, recording the point of call
21628 and a reference to the target function's debug entry. */
21630 static void
21631 dwarf2out_direct_call (tree targ)
21633 dcall_entry e;
21634 tree origin = decl_ultimate_origin (targ);
21636 /* If this is a clone, use the abstract origin as the target. */
21637 if (origin)
21638 targ = origin;
21640 e.poc_label_num = poc_label_num++;
21641 e.poc_decl = current_function_decl;
21642 e.targ_die = force_decl_die (targ);
21643 VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21645 /* Drop a label at the return point to mark the point of call. */
21646 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21649 /* Returns a hash value for X (which really is a struct vcall_insn). */
21651 static hashval_t
21652 vcall_insn_table_hash (const void *x)
21654 return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21657 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21658 insnd_uid of *Y. */
21660 static int
21661 vcall_insn_table_eq (const void *x, const void *y)
21663 return (((const struct vcall_insn *) x)->insn_uid
21664 == ((const struct vcall_insn *) y)->insn_uid);
21667 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE. */
21669 static void
21670 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21672 struct vcall_insn *item = ggc_alloc_vcall_insn ();
21673 struct vcall_insn **slot;
21675 gcc_assert (item);
21676 item->insn_uid = insn_uid;
21677 item->vtable_slot = vtable_slot;
21678 slot = (struct vcall_insn **)
21679 htab_find_slot_with_hash (vcall_insn_table, &item,
21680 (hashval_t) insn_uid, INSERT);
21681 *slot = item;
21684 /* Return the VTABLE_SLOT associated with INSN_UID. */
21686 static unsigned int
21687 lookup_vcall_insn (unsigned int insn_uid)
21689 struct vcall_insn item;
21690 struct vcall_insn *p;
21692 item.insn_uid = insn_uid;
21693 item.vtable_slot = 0;
21694 p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21695 (void *) &item,
21696 (hashval_t) insn_uid);
21697 if (p == NULL)
21698 return (unsigned int) -1;
21699 return p->vtable_slot;
21703 /* Called when lowering indirect calls to RTL. We make a note of INSN_UID
21704 and the OBJ_TYPE_REF_TOKEN from ADDR. For C++ virtual calls, the token
21705 is the vtable slot index that we will need to put in the virtual call
21706 table later. */
21708 static void
21709 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21711 if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21713 tree token = OBJ_TYPE_REF_TOKEN (addr);
21714 if (TREE_CODE (token) == INTEGER_CST)
21715 store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21719 /* Called when scheduling RTL, when a CALL_INSN is split. Copies the
21720 OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21721 with NEW_INSN. */
21723 static void
21724 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21726 unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21728 if (vtable_slot != (unsigned int) -1)
21729 store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21732 /* Called by the final INSN scan whenever we see a virtual function call.
21733 Make an entry into the virtual call table, recording the point of call
21734 and the slot index of the vtable entry used to call the virtual member
21735 function. The slot index was associated with the INSN_UID during the
21736 lowering to RTL. */
21738 static void
21739 dwarf2out_virtual_call (int insn_uid)
21741 unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21742 vcall_entry e;
21744 if (vtable_slot == (unsigned int) -1)
21745 return;
21747 e.poc_label_num = poc_label_num++;
21748 e.vtable_slot = vtable_slot;
21749 VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21751 /* Drop a label at the return point to mark the point of call. */
21752 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21755 /* Called by the final INSN scan whenever we see a var location. We
21756 use it to drop labels in the right places, and throw the location in
21757 our lookup table. */
21759 static void
21760 dwarf2out_var_location (rtx loc_note)
21762 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21763 struct var_loc_node *newloc;
21764 rtx next_real;
21765 static const char *last_label;
21766 static const char *last_postcall_label;
21767 static bool last_in_cold_section_p;
21768 tree decl;
21770 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21771 return;
21773 next_real = next_real_insn (loc_note);
21774 /* If there are no instructions which would be affected by this note,
21775 don't do anything. */
21776 if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21777 return;
21779 /* If there were any real insns between note we processed last time
21780 and this note (or if it is the first note), clear
21781 last_{,postcall_}label so that they are not reused this time. */
21782 if (last_var_location_insn == NULL_RTX
21783 || last_var_location_insn != next_real
21784 || last_in_cold_section_p != in_cold_section_p)
21786 last_label = NULL;
21787 last_postcall_label = NULL;
21790 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21791 newloc = add_var_loc_to_decl (decl, loc_note,
21792 NOTE_DURING_CALL_P (loc_note)
21793 ? last_postcall_label : last_label);
21794 if (newloc == NULL)
21795 return;
21797 /* If there were no real insns between note we processed last time
21798 and this note, use the label we emitted last time. Otherwise
21799 create a new label and emit it. */
21800 if (last_label == NULL)
21802 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21803 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21804 loclabel_num++;
21805 last_label = ggc_strdup (loclabel);
21808 if (!NOTE_DURING_CALL_P (loc_note))
21809 newloc->label = last_label;
21810 else
21812 if (!last_postcall_label)
21814 sprintf (loclabel, "%s-1", last_label);
21815 last_postcall_label = ggc_strdup (loclabel);
21817 newloc->label = last_postcall_label;
21820 last_var_location_insn = next_real;
21821 last_in_cold_section_p = in_cold_section_p;
21824 /* We need to reset the locations at the beginning of each
21825 function. We can't do this in the end_function hook, because the
21826 declarations that use the locations won't have been output when
21827 that hook is called. Also compute have_multiple_function_sections here. */
21829 static void
21830 dwarf2out_begin_function (tree fun)
21832 if (function_section (fun) != text_section)
21833 have_multiple_function_sections = true;
21834 else if (flag_reorder_blocks_and_partition && !cold_text_section)
21836 gcc_assert (current_function_decl == fun);
21837 cold_text_section = unlikely_text_section ();
21838 switch_to_section (cold_text_section);
21839 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21840 switch_to_section (current_function_section ());
21843 dwarf2out_note_section_used ();
21846 /* Output a label to mark the beginning of a source code line entry
21847 and record information relating to this source line, in
21848 'line_info_table' for later output of the .debug_line section. */
21850 static void
21851 dwarf2out_source_line (unsigned int line, const char *filename,
21852 int discriminator, bool is_stmt)
21854 static bool last_is_stmt = true;
21856 if (debug_info_level >= DINFO_LEVEL_NORMAL
21857 && line != 0)
21859 int file_num = maybe_emit_file (lookup_filename (filename));
21861 switch_to_section (current_function_section ());
21863 /* If requested, emit something human-readable. */
21864 if (flag_debug_asm)
21865 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21866 filename, line);
21868 if (DWARF2_ASM_LINE_DEBUG_INFO)
21870 /* Emit the .loc directive understood by GNU as. */
21871 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21872 if (is_stmt != last_is_stmt)
21874 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21875 last_is_stmt = is_stmt;
21877 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21878 fprintf (asm_out_file, " discriminator %d", discriminator);
21879 fputc ('\n', asm_out_file);
21881 /* Indicate that line number info exists. */
21882 line_info_table_in_use++;
21884 else if (function_section (current_function_decl) != text_section)
21886 dw_separate_line_info_ref line_info;
21887 targetm.asm_out.internal_label (asm_out_file,
21888 SEPARATE_LINE_CODE_LABEL,
21889 separate_line_info_table_in_use);
21891 /* Expand the line info table if necessary. */
21892 if (separate_line_info_table_in_use
21893 == separate_line_info_table_allocated)
21895 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21896 separate_line_info_table
21897 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21898 separate_line_info_table,
21899 separate_line_info_table_allocated);
21900 memset (separate_line_info_table
21901 + separate_line_info_table_in_use,
21903 (LINE_INFO_TABLE_INCREMENT
21904 * sizeof (dw_separate_line_info_entry)));
21907 /* Add the new entry at the end of the line_info_table. */
21908 line_info
21909 = &separate_line_info_table[separate_line_info_table_in_use++];
21910 line_info->dw_file_num = file_num;
21911 line_info->dw_line_num = line;
21912 line_info->function = current_function_funcdef_no;
21914 else
21916 dw_line_info_ref line_info;
21918 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21919 line_info_table_in_use);
21921 /* Expand the line info table if necessary. */
21922 if (line_info_table_in_use == line_info_table_allocated)
21924 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21925 line_info_table
21926 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21927 line_info_table_allocated);
21928 memset (line_info_table + line_info_table_in_use, 0,
21929 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21932 /* Add the new entry at the end of the line_info_table. */
21933 line_info = &line_info_table[line_info_table_in_use++];
21934 line_info->dw_file_num = file_num;
21935 line_info->dw_line_num = line;
21940 /* Record the beginning of a new source file. */
21942 static void
21943 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21945 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21947 /* Record the beginning of the file for break_out_includes. */
21948 dw_die_ref bincl_die;
21950 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21951 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21954 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21956 macinfo_entry e;
21957 e.code = DW_MACINFO_start_file;
21958 e.lineno = lineno;
21959 e.info = xstrdup (filename);
21960 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21964 /* Record the end of a source file. */
21966 static void
21967 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21969 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21970 /* Record the end of the file for break_out_includes. */
21971 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21973 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21975 macinfo_entry e;
21976 e.code = DW_MACINFO_end_file;
21977 e.lineno = lineno;
21978 e.info = NULL;
21979 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21983 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21984 the tail part of the directive line, i.e. the part which is past the
21985 initial whitespace, #, whitespace, directive-name, whitespace part. */
21987 static void
21988 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21989 const char *buffer ATTRIBUTE_UNUSED)
21991 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21993 macinfo_entry e;
21994 e.code = DW_MACINFO_define;
21995 e.lineno = lineno;
21996 e.info = xstrdup (buffer);;
21997 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22001 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22002 the tail part of the directive line, i.e. the part which is past the
22003 initial whitespace, #, whitespace, directive-name, whitespace part. */
22005 static void
22006 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22007 const char *buffer ATTRIBUTE_UNUSED)
22009 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22011 macinfo_entry e;
22012 e.code = DW_MACINFO_undef;
22013 e.lineno = lineno;
22014 e.info = xstrdup (buffer);;
22015 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22019 static void
22020 output_macinfo (void)
22022 unsigned i;
22023 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22024 macinfo_entry *ref;
22026 if (! length)
22027 return;
22029 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22031 switch (ref->code)
22033 case DW_MACINFO_start_file:
22035 int file_num = maybe_emit_file (lookup_filename (ref->info));
22036 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22037 dw2_asm_output_data_uleb128
22038 (ref->lineno, "Included from line number %lu",
22039 (unsigned long)ref->lineno);
22040 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22042 break;
22043 case DW_MACINFO_end_file:
22044 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22045 break;
22046 case DW_MACINFO_define:
22047 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22048 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22049 (unsigned long)ref->lineno);
22050 dw2_asm_output_nstring (ref->info, -1, "The macro");
22051 break;
22052 case DW_MACINFO_undef:
22053 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22054 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22055 (unsigned long)ref->lineno);
22056 dw2_asm_output_nstring (ref->info, -1, "The macro");
22057 break;
22058 default:
22059 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22060 ASM_COMMENT_START, (unsigned long)ref->code);
22061 break;
22066 /* Set up for Dwarf output at the start of compilation. */
22068 static void
22069 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22071 /* Allocate the file_table. */
22072 file_table = htab_create_ggc (50, file_table_hash,
22073 file_table_eq, NULL);
22075 /* Allocate the decl_die_table. */
22076 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22077 decl_die_table_eq, NULL);
22079 /* Allocate the decl_loc_table. */
22080 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22081 decl_loc_table_eq, NULL);
22083 /* Allocate the initial hunk of the decl_scope_table. */
22084 decl_scope_table = VEC_alloc (tree, gc, 256);
22086 /* Allocate the initial hunk of the abbrev_die_table. */
22087 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22088 (ABBREV_DIE_TABLE_INCREMENT);
22089 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22090 /* Zero-th entry is allocated, but unused. */
22091 abbrev_die_table_in_use = 1;
22093 /* Allocate the initial hunk of the line_info_table. */
22094 line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22095 (LINE_INFO_TABLE_INCREMENT);
22096 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22098 /* Zero-th entry is allocated, but unused. */
22099 line_info_table_in_use = 1;
22101 /* Allocate the pubtypes and pubnames vectors. */
22102 pubname_table = VEC_alloc (pubname_entry, gc, 32);
22103 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22105 /* Allocate the table that maps insn UIDs to vtable slot indexes. */
22106 vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
22107 vcall_insn_table_eq, NULL);
22109 incomplete_types = VEC_alloc (tree, gc, 64);
22111 used_rtx_array = VEC_alloc (rtx, gc, 32);
22113 debug_info_section = get_section (DEBUG_INFO_SECTION,
22114 SECTION_DEBUG, NULL);
22115 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22116 SECTION_DEBUG, NULL);
22117 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22118 SECTION_DEBUG, NULL);
22119 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22120 SECTION_DEBUG, NULL);
22121 debug_line_section = get_section (DEBUG_LINE_SECTION,
22122 SECTION_DEBUG, NULL);
22123 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22124 SECTION_DEBUG, NULL);
22125 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22126 SECTION_DEBUG, NULL);
22127 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22128 SECTION_DEBUG, NULL);
22129 debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
22130 SECTION_DEBUG, NULL);
22131 debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
22132 SECTION_DEBUG, NULL);
22133 debug_str_section = get_section (DEBUG_STR_SECTION,
22134 DEBUG_STR_SECTION_FLAGS, NULL);
22135 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22136 SECTION_DEBUG, NULL);
22137 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22138 SECTION_DEBUG, NULL);
22140 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22141 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22142 DEBUG_ABBREV_SECTION_LABEL, 0);
22143 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22144 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22145 COLD_TEXT_SECTION_LABEL, 0);
22146 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22148 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22149 DEBUG_INFO_SECTION_LABEL, 0);
22150 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22151 DEBUG_LINE_SECTION_LABEL, 0);
22152 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22153 DEBUG_RANGES_SECTION_LABEL, 0);
22154 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22155 DEBUG_MACINFO_SECTION_LABEL, 0);
22157 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22158 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22160 switch_to_section (text_section);
22161 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22164 /* Called before cgraph_optimize starts outputtting functions, variables
22165 and toplevel asms into assembly. */
22167 static void
22168 dwarf2out_assembly_start (void)
22170 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22171 && dwarf2out_do_cfi_asm ()
22172 && (!(flag_unwind_tables || flag_exceptions)
22173 || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22174 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22177 /* A helper function for dwarf2out_finish called through
22178 htab_traverse. Emit one queued .debug_str string. */
22180 static int
22181 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22183 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22185 if (node->label && node->refcount)
22187 switch_to_section (debug_str_section);
22188 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22189 assemble_string (node->str, strlen (node->str) + 1);
22192 return 1;
22195 #if ENABLE_ASSERT_CHECKING
22196 /* Verify that all marks are clear. */
22198 static void
22199 verify_marks_clear (dw_die_ref die)
22201 dw_die_ref c;
22203 gcc_assert (! die->die_mark);
22204 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22206 #endif /* ENABLE_ASSERT_CHECKING */
22208 /* Clear the marks for a die and its children.
22209 Be cool if the mark isn't set. */
22211 static void
22212 prune_unmark_dies (dw_die_ref die)
22214 dw_die_ref c;
22216 if (die->die_mark)
22217 die->die_mark = 0;
22218 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22221 /* Given DIE that we're marking as used, find any other dies
22222 it references as attributes and mark them as used. */
22224 static void
22225 prune_unused_types_walk_attribs (dw_die_ref die)
22227 dw_attr_ref a;
22228 unsigned ix;
22230 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22232 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22234 /* A reference to another DIE.
22235 Make sure that it will get emitted.
22236 If it was broken out into a comdat group, don't follow it. */
22237 if (dwarf_version < 4
22238 || a->dw_attr == DW_AT_specification
22239 || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22240 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22242 /* Set the string's refcount to 0 so that prune_unused_types_mark
22243 accounts properly for it. */
22244 if (AT_class (a) == dw_val_class_str)
22245 a->dw_attr_val.v.val_str->refcount = 0;
22249 /* Mark the generic parameters and arguments children DIEs of DIE. */
22251 static void
22252 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22254 dw_die_ref c;
22256 if (die == NULL || die->die_child == NULL)
22257 return;
22258 c = die->die_child;
22261 switch (c->die_tag)
22263 case DW_TAG_template_type_param:
22264 case DW_TAG_template_value_param:
22265 case DW_TAG_GNU_template_template_param:
22266 case DW_TAG_GNU_template_parameter_pack:
22267 prune_unused_types_mark (c, 1);
22268 break;
22269 default:
22270 break;
22272 c = c->die_sib;
22273 } while (c && c != die->die_child);
22276 /* Mark DIE as being used. If DOKIDS is true, then walk down
22277 to DIE's children. */
22279 static void
22280 prune_unused_types_mark (dw_die_ref die, int dokids)
22282 dw_die_ref c;
22284 if (die->die_mark == 0)
22286 /* We haven't done this node yet. Mark it as used. */
22287 die->die_mark = 1;
22288 /* If this is the DIE of a generic type instantiation,
22289 mark the children DIEs that describe its generic parms and
22290 args. */
22291 prune_unused_types_mark_generic_parms_dies (die);
22293 /* We also have to mark its parents as used.
22294 (But we don't want to mark our parents' kids due to this.) */
22295 if (die->die_parent)
22296 prune_unused_types_mark (die->die_parent, 0);
22298 /* Mark any referenced nodes. */
22299 prune_unused_types_walk_attribs (die);
22301 /* If this node is a specification,
22302 also mark the definition, if it exists. */
22303 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22304 prune_unused_types_mark (die->die_definition, 1);
22307 if (dokids && die->die_mark != 2)
22309 /* We need to walk the children, but haven't done so yet.
22310 Remember that we've walked the kids. */
22311 die->die_mark = 2;
22313 /* If this is an array type, we need to make sure our
22314 kids get marked, even if they're types. If we're
22315 breaking out types into comdat sections, do this
22316 for all type definitions. */
22317 if (die->die_tag == DW_TAG_array_type
22318 || (dwarf_version >= 4
22319 && is_type_die (die) && ! is_declaration_die (die)))
22320 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22321 else
22322 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22326 /* For local classes, look if any static member functions were emitted
22327 and if so, mark them. */
22329 static void
22330 prune_unused_types_walk_local_classes (dw_die_ref die)
22332 dw_die_ref c;
22334 if (die->die_mark == 2)
22335 return;
22337 switch (die->die_tag)
22339 case DW_TAG_structure_type:
22340 case DW_TAG_union_type:
22341 case DW_TAG_class_type:
22342 break;
22344 case DW_TAG_subprogram:
22345 if (!get_AT_flag (die, DW_AT_declaration)
22346 || die->die_definition != NULL)
22347 prune_unused_types_mark (die, 1);
22348 return;
22350 default:
22351 return;
22354 /* Mark children. */
22355 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22358 /* Walk the tree DIE and mark types that we actually use. */
22360 static void
22361 prune_unused_types_walk (dw_die_ref die)
22363 dw_die_ref c;
22365 /* Don't do anything if this node is already marked and
22366 children have been marked as well. */
22367 if (die->die_mark == 2)
22368 return;
22370 switch (die->die_tag)
22372 case DW_TAG_structure_type:
22373 case DW_TAG_union_type:
22374 case DW_TAG_class_type:
22375 if (die->die_perennial_p)
22376 break;
22378 for (c = die->die_parent; c; c = c->die_parent)
22379 if (c->die_tag == DW_TAG_subprogram)
22380 break;
22382 /* Finding used static member functions inside of classes
22383 is needed just for local classes, because for other classes
22384 static member function DIEs with DW_AT_specification
22385 are emitted outside of the DW_TAG_*_type. If we ever change
22386 it, we'd need to call this even for non-local classes. */
22387 if (c)
22388 prune_unused_types_walk_local_classes (die);
22390 /* It's a type node --- don't mark it. */
22391 return;
22393 case DW_TAG_const_type:
22394 case DW_TAG_packed_type:
22395 case DW_TAG_pointer_type:
22396 case DW_TAG_reference_type:
22397 case DW_TAG_rvalue_reference_type:
22398 case DW_TAG_volatile_type:
22399 case DW_TAG_typedef:
22400 case DW_TAG_array_type:
22401 case DW_TAG_interface_type:
22402 case DW_TAG_friend:
22403 case DW_TAG_variant_part:
22404 case DW_TAG_enumeration_type:
22405 case DW_TAG_subroutine_type:
22406 case DW_TAG_string_type:
22407 case DW_TAG_set_type:
22408 case DW_TAG_subrange_type:
22409 case DW_TAG_ptr_to_member_type:
22410 case DW_TAG_file_type:
22411 if (die->die_perennial_p)
22412 break;
22414 /* It's a type node --- don't mark it. */
22415 return;
22417 default:
22418 /* Mark everything else. */
22419 break;
22422 if (die->die_mark == 0)
22424 die->die_mark = 1;
22426 /* Now, mark any dies referenced from here. */
22427 prune_unused_types_walk_attribs (die);
22430 die->die_mark = 2;
22432 /* Mark children. */
22433 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22436 /* Increment the string counts on strings referred to from DIE's
22437 attributes. */
22439 static void
22440 prune_unused_types_update_strings (dw_die_ref die)
22442 dw_attr_ref a;
22443 unsigned ix;
22445 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22446 if (AT_class (a) == dw_val_class_str)
22448 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22449 s->refcount++;
22450 /* Avoid unnecessarily putting strings that are used less than
22451 twice in the hash table. */
22452 if (s->refcount
22453 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22455 void ** slot;
22456 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22457 htab_hash_string (s->str),
22458 INSERT);
22459 gcc_assert (*slot == NULL);
22460 *slot = s;
22465 /* Remove from the tree DIE any dies that aren't marked. */
22467 static void
22468 prune_unused_types_prune (dw_die_ref die)
22470 dw_die_ref c;
22472 gcc_assert (die->die_mark);
22473 prune_unused_types_update_strings (die);
22475 if (! die->die_child)
22476 return;
22478 c = die->die_child;
22479 do {
22480 dw_die_ref prev = c;
22481 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22482 if (c == die->die_child)
22484 /* No marked children between 'prev' and the end of the list. */
22485 if (prev == c)
22486 /* No marked children at all. */
22487 die->die_child = NULL;
22488 else
22490 prev->die_sib = c->die_sib;
22491 die->die_child = prev;
22493 return;
22496 if (c != prev->die_sib)
22497 prev->die_sib = c;
22498 prune_unused_types_prune (c);
22499 } while (c != die->die_child);
22502 /* A helper function for dwarf2out_finish called through
22503 htab_traverse. Clear .debug_str strings that we haven't already
22504 decided to emit. */
22506 static int
22507 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22509 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22511 if (!node->label || !node->refcount)
22512 htab_clear_slot (debug_str_hash, h);
22514 return 1;
22517 /* Remove dies representing declarations that we never use. */
22519 static void
22520 prune_unused_types (void)
22522 unsigned int i;
22523 limbo_die_node *node;
22524 comdat_type_node *ctnode;
22525 pubname_ref pub;
22526 dcall_entry *dcall;
22528 #if ENABLE_ASSERT_CHECKING
22529 /* All the marks should already be clear. */
22530 verify_marks_clear (comp_unit_die ());
22531 for (node = limbo_die_list; node; node = node->next)
22532 verify_marks_clear (node->die);
22533 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22534 verify_marks_clear (ctnode->root_die);
22535 #endif /* ENABLE_ASSERT_CHECKING */
22537 /* Mark types that are used in global variables. */
22538 premark_types_used_by_global_vars ();
22540 /* Set the mark on nodes that are actually used. */
22541 prune_unused_types_walk (comp_unit_die ());
22542 for (node = limbo_die_list; node; node = node->next)
22543 prune_unused_types_walk (node->die);
22544 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22546 prune_unused_types_walk (ctnode->root_die);
22547 prune_unused_types_mark (ctnode->type_die, 1);
22550 /* Also set the mark on nodes referenced from the
22551 pubname_table or arange_table. */
22552 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22553 prune_unused_types_mark (pub->die, 1);
22554 for (i = 0; i < arange_table_in_use; i++)
22555 prune_unused_types_mark (arange_table[i], 1);
22557 /* Mark nodes referenced from the direct call table. */
22558 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22559 prune_unused_types_mark (dcall->targ_die, 1);
22561 /* Get rid of nodes that aren't marked; and update the string counts. */
22562 if (debug_str_hash && debug_str_hash_forced)
22563 htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22564 else if (debug_str_hash)
22565 htab_empty (debug_str_hash);
22566 prune_unused_types_prune (comp_unit_die ());
22567 for (node = limbo_die_list; node; node = node->next)
22568 prune_unused_types_prune (node->die);
22569 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22570 prune_unused_types_prune (ctnode->root_die);
22572 /* Leave the marks clear. */
22573 prune_unmark_dies (comp_unit_die ());
22574 for (node = limbo_die_list; node; node = node->next)
22575 prune_unmark_dies (node->die);
22576 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22577 prune_unmark_dies (ctnode->root_die);
22580 /* Set the parameter to true if there are any relative pathnames in
22581 the file table. */
22582 static int
22583 file_table_relative_p (void ** slot, void *param)
22585 bool *p = (bool *) param;
22586 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22587 if (!IS_ABSOLUTE_PATH (d->filename))
22589 *p = true;
22590 return 0;
22592 return 1;
22595 /* Routines to manipulate hash table of comdat type units. */
22597 static hashval_t
22598 htab_ct_hash (const void *of)
22600 hashval_t h;
22601 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22603 memcpy (&h, type_node->signature, sizeof (h));
22604 return h;
22607 static int
22608 htab_ct_eq (const void *of1, const void *of2)
22610 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22611 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22613 return (! memcmp (type_node_1->signature, type_node_2->signature,
22614 DWARF_TYPE_SIGNATURE_SIZE));
22617 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22618 to the location it would have been added, should we know its
22619 DECL_ASSEMBLER_NAME when we added other attributes. This will
22620 probably improve compactness of debug info, removing equivalent
22621 abbrevs, and hide any differences caused by deferring the
22622 computation of the assembler name, triggered by e.g. PCH. */
22624 static inline void
22625 move_linkage_attr (dw_die_ref die)
22627 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22628 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22630 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22631 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22633 while (--ix > 0)
22635 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22637 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22638 break;
22641 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22643 VEC_pop (dw_attr_node, die->die_attr);
22644 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22648 /* Helper function for resolve_addr, attempt to resolve
22649 one CONST_STRING, return non-zero if not successful. Similarly verify that
22650 SYMBOL_REFs refer to variables emitted in the current CU. */
22652 static int
22653 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22655 rtx rtl = *addr;
22657 if (GET_CODE (rtl) == CONST_STRING)
22659 size_t len = strlen (XSTR (rtl, 0)) + 1;
22660 tree t = build_string (len, XSTR (rtl, 0));
22661 tree tlen = build_int_cst (NULL_TREE, len - 1);
22662 TREE_TYPE (t)
22663 = build_array_type (char_type_node, build_index_type (tlen));
22664 rtl = lookup_constant_def (t);
22665 if (!rtl || !MEM_P (rtl))
22666 return 1;
22667 rtl = XEXP (rtl, 0);
22668 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22669 *addr = rtl;
22670 return 0;
22673 if (GET_CODE (rtl) == SYMBOL_REF
22674 && SYMBOL_REF_DECL (rtl)
22675 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22676 return 1;
22678 if (GET_CODE (rtl) == CONST
22679 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22680 return 1;
22682 return 0;
22685 /* Helper function for resolve_addr, handle one location
22686 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22687 the location list couldn't be resolved. */
22689 static bool
22690 resolve_addr_in_expr (dw_loc_descr_ref loc)
22692 for (; loc; loc = loc->dw_loc_next)
22693 if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22694 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22695 || (loc->dw_loc_opc == DW_OP_implicit_value
22696 && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22697 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22698 return false;
22699 else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22700 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22702 dw_die_ref ref
22703 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22704 if (ref == NULL)
22705 return false;
22706 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22707 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22708 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22710 return true;
22713 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22714 an address in .rodata section if the string literal is emitted there,
22715 or remove the containing location list or replace DW_AT_const_value
22716 with DW_AT_location and empty location expression, if it isn't found
22717 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22718 to something that has been emitted in the current CU. */
22720 static void
22721 resolve_addr (dw_die_ref die)
22723 dw_die_ref c;
22724 dw_attr_ref a;
22725 dw_loc_list_ref *curr;
22726 unsigned ix;
22728 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22729 switch (AT_class (a))
22731 case dw_val_class_loc_list:
22732 curr = AT_loc_list_ptr (a);
22733 while (*curr)
22735 if (!resolve_addr_in_expr ((*curr)->expr))
22737 dw_loc_list_ref next = (*curr)->dw_loc_next;
22738 if (next && (*curr)->ll_symbol)
22740 gcc_assert (!next->ll_symbol);
22741 next->ll_symbol = (*curr)->ll_symbol;
22743 *curr = next;
22745 else
22746 curr = &(*curr)->dw_loc_next;
22748 if (!AT_loc_list (a))
22750 remove_AT (die, a->dw_attr);
22751 ix--;
22753 break;
22754 case dw_val_class_loc:
22755 if (!resolve_addr_in_expr (AT_loc (a)))
22757 remove_AT (die, a->dw_attr);
22758 ix--;
22760 break;
22761 case dw_val_class_addr:
22762 if (a->dw_attr == DW_AT_const_value
22763 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22765 remove_AT (die, a->dw_attr);
22766 ix--;
22768 break;
22769 default:
22770 break;
22773 FOR_EACH_CHILD (die, c, resolve_addr (c));
22776 /* Helper routines for optimize_location_lists.
22777 This pass tries to share identical local lists in .debug_loc
22778 section. */
22780 /* Iteratively hash operands of LOC opcode. */
22782 static inline hashval_t
22783 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22785 dw_val_ref val1 = &loc->dw_loc_oprnd1;
22786 dw_val_ref val2 = &loc->dw_loc_oprnd2;
22788 switch (loc->dw_loc_opc)
22790 case DW_OP_const4u:
22791 case DW_OP_const8u:
22792 if (loc->dtprel)
22793 goto hash_addr;
22794 /* FALLTHRU */
22795 case DW_OP_const1u:
22796 case DW_OP_const1s:
22797 case DW_OP_const2u:
22798 case DW_OP_const2s:
22799 case DW_OP_const4s:
22800 case DW_OP_const8s:
22801 case DW_OP_constu:
22802 case DW_OP_consts:
22803 case DW_OP_pick:
22804 case DW_OP_plus_uconst:
22805 case DW_OP_breg0:
22806 case DW_OP_breg1:
22807 case DW_OP_breg2:
22808 case DW_OP_breg3:
22809 case DW_OP_breg4:
22810 case DW_OP_breg5:
22811 case DW_OP_breg6:
22812 case DW_OP_breg7:
22813 case DW_OP_breg8:
22814 case DW_OP_breg9:
22815 case DW_OP_breg10:
22816 case DW_OP_breg11:
22817 case DW_OP_breg12:
22818 case DW_OP_breg13:
22819 case DW_OP_breg14:
22820 case DW_OP_breg15:
22821 case DW_OP_breg16:
22822 case DW_OP_breg17:
22823 case DW_OP_breg18:
22824 case DW_OP_breg19:
22825 case DW_OP_breg20:
22826 case DW_OP_breg21:
22827 case DW_OP_breg22:
22828 case DW_OP_breg23:
22829 case DW_OP_breg24:
22830 case DW_OP_breg25:
22831 case DW_OP_breg26:
22832 case DW_OP_breg27:
22833 case DW_OP_breg28:
22834 case DW_OP_breg29:
22835 case DW_OP_breg30:
22836 case DW_OP_breg31:
22837 case DW_OP_regx:
22838 case DW_OP_fbreg:
22839 case DW_OP_piece:
22840 case DW_OP_deref_size:
22841 case DW_OP_xderef_size:
22842 hash = iterative_hash_object (val1->v.val_int, hash);
22843 break;
22844 case DW_OP_skip:
22845 case DW_OP_bra:
22847 int offset;
22849 gcc_assert (val1->val_class == dw_val_class_loc);
22850 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22851 hash = iterative_hash_object (offset, hash);
22853 break;
22854 case DW_OP_implicit_value:
22855 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22856 switch (val2->val_class)
22858 case dw_val_class_const:
22859 hash = iterative_hash_object (val2->v.val_int, hash);
22860 break;
22861 case dw_val_class_vec:
22863 unsigned int elt_size = val2->v.val_vec.elt_size;
22864 unsigned int len = val2->v.val_vec.length;
22866 hash = iterative_hash_object (elt_size, hash);
22867 hash = iterative_hash_object (len, hash);
22868 hash = iterative_hash (val2->v.val_vec.array,
22869 len * elt_size, hash);
22871 break;
22872 case dw_val_class_const_double:
22873 hash = iterative_hash_object (val2->v.val_double.low, hash);
22874 hash = iterative_hash_object (val2->v.val_double.high, hash);
22875 break;
22876 case dw_val_class_addr:
22877 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22878 break;
22879 default:
22880 gcc_unreachable ();
22882 break;
22883 case DW_OP_bregx:
22884 case DW_OP_bit_piece:
22885 hash = iterative_hash_object (val1->v.val_int, hash);
22886 hash = iterative_hash_object (val2->v.val_int, hash);
22887 break;
22888 case DW_OP_addr:
22889 hash_addr:
22890 if (loc->dtprel)
22892 unsigned char dtprel = 0xd1;
22893 hash = iterative_hash_object (dtprel, hash);
22895 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22896 break;
22897 case DW_OP_GNU_implicit_pointer:
22898 hash = iterative_hash_object (val2->v.val_int, hash);
22899 break;
22901 default:
22902 /* Other codes have no operands. */
22903 break;
22905 return hash;
22908 /* Iteratively hash the whole DWARF location expression LOC. */
22910 static inline hashval_t
22911 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22913 dw_loc_descr_ref l;
22914 bool sizes_computed = false;
22915 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
22916 size_of_locs (loc);
22918 for (l = loc; l != NULL; l = l->dw_loc_next)
22920 enum dwarf_location_atom opc = l->dw_loc_opc;
22921 hash = iterative_hash_object (opc, hash);
22922 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22924 size_of_locs (loc);
22925 sizes_computed = true;
22927 hash = hash_loc_operands (l, hash);
22929 return hash;
22932 /* Compute hash of the whole location list LIST_HEAD. */
22934 static inline void
22935 hash_loc_list (dw_loc_list_ref list_head)
22937 dw_loc_list_ref curr = list_head;
22938 hashval_t hash = 0;
22940 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22942 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22943 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22944 if (curr->section)
22945 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22946 hash);
22947 hash = hash_locs (curr->expr, hash);
22949 list_head->hash = hash;
22952 /* Return true if X and Y opcodes have the same operands. */
22954 static inline bool
22955 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22957 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22958 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22959 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22960 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22962 switch (x->dw_loc_opc)
22964 case DW_OP_const4u:
22965 case DW_OP_const8u:
22966 if (x->dtprel)
22967 goto hash_addr;
22968 /* FALLTHRU */
22969 case DW_OP_const1u:
22970 case DW_OP_const1s:
22971 case DW_OP_const2u:
22972 case DW_OP_const2s:
22973 case DW_OP_const4s:
22974 case DW_OP_const8s:
22975 case DW_OP_constu:
22976 case DW_OP_consts:
22977 case DW_OP_pick:
22978 case DW_OP_plus_uconst:
22979 case DW_OP_breg0:
22980 case DW_OP_breg1:
22981 case DW_OP_breg2:
22982 case DW_OP_breg3:
22983 case DW_OP_breg4:
22984 case DW_OP_breg5:
22985 case DW_OP_breg6:
22986 case DW_OP_breg7:
22987 case DW_OP_breg8:
22988 case DW_OP_breg9:
22989 case DW_OP_breg10:
22990 case DW_OP_breg11:
22991 case DW_OP_breg12:
22992 case DW_OP_breg13:
22993 case DW_OP_breg14:
22994 case DW_OP_breg15:
22995 case DW_OP_breg16:
22996 case DW_OP_breg17:
22997 case DW_OP_breg18:
22998 case DW_OP_breg19:
22999 case DW_OP_breg20:
23000 case DW_OP_breg21:
23001 case DW_OP_breg22:
23002 case DW_OP_breg23:
23003 case DW_OP_breg24:
23004 case DW_OP_breg25:
23005 case DW_OP_breg26:
23006 case DW_OP_breg27:
23007 case DW_OP_breg28:
23008 case DW_OP_breg29:
23009 case DW_OP_breg30:
23010 case DW_OP_breg31:
23011 case DW_OP_regx:
23012 case DW_OP_fbreg:
23013 case DW_OP_piece:
23014 case DW_OP_deref_size:
23015 case DW_OP_xderef_size:
23016 return valx1->v.val_int == valy1->v.val_int;
23017 case DW_OP_skip:
23018 case DW_OP_bra:
23019 gcc_assert (valx1->val_class == dw_val_class_loc
23020 && valy1->val_class == dw_val_class_loc
23021 && x->dw_loc_addr == y->dw_loc_addr);
23022 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23023 case DW_OP_implicit_value:
23024 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23025 || valx2->val_class != valy2->val_class)
23026 return false;
23027 switch (valx2->val_class)
23029 case dw_val_class_const:
23030 return valx2->v.val_int == valy2->v.val_int;
23031 case dw_val_class_vec:
23032 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23033 && valx2->v.val_vec.length == valy2->v.val_vec.length
23034 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23035 valx2->v.val_vec.elt_size
23036 * valx2->v.val_vec.length) == 0;
23037 case dw_val_class_const_double:
23038 return valx2->v.val_double.low == valy2->v.val_double.low
23039 && valx2->v.val_double.high == valy2->v.val_double.high;
23040 case dw_val_class_addr:
23041 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23042 default:
23043 gcc_unreachable ();
23045 case DW_OP_bregx:
23046 case DW_OP_bit_piece:
23047 return valx1->v.val_int == valy1->v.val_int
23048 && valx2->v.val_int == valy2->v.val_int;
23049 case DW_OP_addr:
23050 hash_addr:
23051 return rtx_equal_p (valx1->v.val_addr, valx2->v.val_addr);
23052 case DW_OP_GNU_implicit_pointer:
23053 return valx1->val_class == dw_val_class_die_ref
23054 && valx1->val_class == valy1->val_class
23055 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23056 && valx2->v.val_int == valy2->v.val_int;
23057 default:
23058 /* Other codes have no operands. */
23059 return true;
23063 /* Return true if DWARF location expressions X and Y are the same. */
23065 static inline bool
23066 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23068 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23069 if (x->dw_loc_opc != y->dw_loc_opc
23070 || x->dtprel != y->dtprel
23071 || !compare_loc_operands (x, y))
23072 break;
23073 return x == NULL && y == NULL;
23076 /* Return precomputed hash of location list X. */
23078 static hashval_t
23079 loc_list_hash (const void *x)
23081 return ((const struct dw_loc_list_struct *) x)->hash;
23084 /* Return 1 if location lists X and Y are the same. */
23086 static int
23087 loc_list_eq (const void *x, const void *y)
23089 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23090 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23091 if (a == b)
23092 return 1;
23093 if (a->hash != b->hash)
23094 return 0;
23095 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23096 if (strcmp (a->begin, b->begin) != 0
23097 || strcmp (a->end, b->end) != 0
23098 || (a->section == NULL) != (b->section == NULL)
23099 || (a->section && strcmp (a->section, b->section) != 0)
23100 || !compare_locs (a->expr, b->expr))
23101 break;
23102 return a == NULL && b == NULL;
23105 /* Recursively optimize location lists referenced from DIE
23106 children and share them whenever possible. */
23108 static void
23109 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23111 dw_die_ref c;
23112 dw_attr_ref a;
23113 unsigned ix;
23114 void **slot;
23116 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23117 if (AT_class (a) == dw_val_class_loc_list)
23119 dw_loc_list_ref list = AT_loc_list (a);
23120 /* TODO: perform some optimizations here, before hashing
23121 it and storing into the hash table. */
23122 hash_loc_list (list);
23123 slot = htab_find_slot_with_hash (htab, list, list->hash,
23124 INSERT);
23125 if (*slot == NULL)
23126 *slot = (void *) list;
23127 else
23128 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23131 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23134 /* Optimize location lists referenced from DIE
23135 children and share them whenever possible. */
23137 static void
23138 optimize_location_lists (dw_die_ref die)
23140 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23141 optimize_location_lists_1 (die, htab);
23142 htab_delete (htab);
23145 /* Output stuff that dwarf requires at the end of every file,
23146 and generate the DWARF-2 debugging info. */
23148 static void
23149 dwarf2out_finish (const char *filename)
23151 limbo_die_node *node, *next_node;
23152 comdat_type_node *ctnode;
23153 htab_t comdat_type_table;
23154 unsigned int i;
23156 gen_remaining_tmpl_value_param_die_attribute ();
23158 /* Add the name for the main input file now. We delayed this from
23159 dwarf2out_init to avoid complications with PCH. */
23160 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23161 if (!IS_ABSOLUTE_PATH (filename))
23162 add_comp_dir_attribute (comp_unit_die ());
23163 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23165 bool p = false;
23166 htab_traverse (file_table, file_table_relative_p, &p);
23167 if (p)
23168 add_comp_dir_attribute (comp_unit_die ());
23171 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23173 add_location_or_const_value_attribute (
23174 VEC_index (deferred_locations, deferred_locations_list, i)->die,
23175 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23176 DW_AT_location);
23179 /* Traverse the limbo die list, and add parent/child links. The only
23180 dies without parents that should be here are concrete instances of
23181 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23182 For concrete instances, we can get the parent die from the abstract
23183 instance. */
23184 for (node = limbo_die_list; node; node = next_node)
23186 dw_die_ref die = node->die;
23187 next_node = node->next;
23189 if (die->die_parent == NULL)
23191 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23193 if (origin)
23194 add_child_die (origin->die_parent, die);
23195 else if (is_cu_die (die))
23197 else if (seen_error ())
23198 /* It's OK to be confused by errors in the input. */
23199 add_child_die (comp_unit_die (), die);
23200 else
23202 /* In certain situations, the lexical block containing a
23203 nested function can be optimized away, which results
23204 in the nested function die being orphaned. Likewise
23205 with the return type of that nested function. Force
23206 this to be a child of the containing function.
23208 It may happen that even the containing function got fully
23209 inlined and optimized out. In that case we are lost and
23210 assign the empty child. This should not be big issue as
23211 the function is likely unreachable too. */
23212 tree context = NULL_TREE;
23214 gcc_assert (node->created_for);
23216 if (DECL_P (node->created_for))
23217 context = DECL_CONTEXT (node->created_for);
23218 else if (TYPE_P (node->created_for))
23219 context = TYPE_CONTEXT (node->created_for);
23221 gcc_assert (context
23222 && (TREE_CODE (context) == FUNCTION_DECL
23223 || TREE_CODE (context) == NAMESPACE_DECL));
23225 origin = lookup_decl_die (context);
23226 if (origin)
23227 add_child_die (origin, die);
23228 else
23229 add_child_die (comp_unit_die (), die);
23234 limbo_die_list = NULL;
23236 resolve_addr (comp_unit_die ());
23238 for (node = deferred_asm_name; node; node = node->next)
23240 tree decl = node->created_for;
23241 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23243 add_linkage_attr (node->die, decl);
23244 move_linkage_attr (node->die);
23248 deferred_asm_name = NULL;
23250 /* Walk through the list of incomplete types again, trying once more to
23251 emit full debugging info for them. */
23252 retry_incomplete_types ();
23254 if (flag_eliminate_unused_debug_types)
23255 prune_unused_types ();
23257 /* Generate separate CUs for each of the include files we've seen.
23258 They will go into limbo_die_list. */
23259 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23260 break_out_includes (comp_unit_die ());
23262 /* Generate separate COMDAT sections for type DIEs. */
23263 if (dwarf_version >= 4)
23265 break_out_comdat_types (comp_unit_die ());
23267 /* Each new type_unit DIE was added to the limbo die list when created.
23268 Since these have all been added to comdat_type_list, clear the
23269 limbo die list. */
23270 limbo_die_list = NULL;
23272 /* For each new comdat type unit, copy declarations for incomplete
23273 types to make the new unit self-contained (i.e., no direct
23274 references to the main compile unit). */
23275 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23276 copy_decls_for_unworthy_types (ctnode->root_die);
23277 copy_decls_for_unworthy_types (comp_unit_die ());
23279 /* In the process of copying declarations from one unit to another,
23280 we may have left some declarations behind that are no longer
23281 referenced. Prune them. */
23282 prune_unused_types ();
23285 /* Traverse the DIE's and add add sibling attributes to those DIE's
23286 that have children. */
23287 add_sibling_attributes (comp_unit_die ());
23288 for (node = limbo_die_list; node; node = node->next)
23289 add_sibling_attributes (node->die);
23290 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23291 add_sibling_attributes (ctnode->root_die);
23293 /* Output a terminator label for the .text section. */
23294 switch_to_section (text_section);
23295 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23296 if (cold_text_section)
23298 switch_to_section (cold_text_section);
23299 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23302 /* We can only use the low/high_pc attributes if all of the code was
23303 in .text. */
23304 if (!have_multiple_function_sections
23305 || (dwarf_version < 3 && dwarf_strict))
23307 add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23308 add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23311 else
23313 unsigned fde_idx = 0;
23314 bool range_list_added = false;
23316 /* We need to give .debug_loc and .debug_ranges an appropriate
23317 "base address". Use zero so that these addresses become
23318 absolute. Historically, we've emitted the unexpected
23319 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23320 Emit both to give time for other tools to adapt. */
23321 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23322 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23324 if (text_section_used)
23325 add_ranges_by_labels (comp_unit_die (), text_section_label,
23326 text_end_label, &range_list_added);
23327 if (flag_reorder_blocks_and_partition && cold_text_section_used)
23328 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23329 cold_end_label, &range_list_added);
23331 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23333 dw_fde_ref fde = &fde_table[fde_idx];
23335 if (fde->dw_fde_switched_sections)
23337 if (!fde->in_std_section)
23338 add_ranges_by_labels (comp_unit_die (),
23339 fde->dw_fde_hot_section_label,
23340 fde->dw_fde_hot_section_end_label,
23341 &range_list_added);
23342 if (!fde->cold_in_std_section)
23343 add_ranges_by_labels (comp_unit_die (),
23344 fde->dw_fde_unlikely_section_label,
23345 fde->dw_fde_unlikely_section_end_label,
23346 &range_list_added);
23348 else if (!fde->in_std_section)
23349 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23350 fde->dw_fde_end, &range_list_added);
23353 if (range_list_added)
23354 add_ranges (NULL);
23357 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23358 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23359 debug_line_section_label);
23361 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23362 add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23364 if (have_location_lists)
23365 optimize_location_lists (comp_unit_die ());
23367 /* Output all of the compilation units. We put the main one last so that
23368 the offsets are available to output_pubnames. */
23369 for (node = limbo_die_list; node; node = node->next)
23370 output_comp_unit (node->die, 0);
23372 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23373 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23375 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23377 /* Don't output duplicate types. */
23378 if (*slot != HTAB_EMPTY_ENTRY)
23379 continue;
23381 /* Add a pointer to the line table for the main compilation unit
23382 so that the debugger can make sense of DW_AT_decl_file
23383 attributes. */
23384 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23385 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23386 debug_line_section_label);
23388 output_comdat_type_unit (ctnode);
23389 *slot = ctnode;
23391 htab_delete (comdat_type_table);
23393 /* Output the main compilation unit if non-empty or if .debug_macinfo
23394 will be emitted. */
23395 output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23397 /* Output the abbreviation table. */
23398 switch_to_section (debug_abbrev_section);
23399 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23400 output_abbrev_section ();
23402 /* Output location list section if necessary. */
23403 if (have_location_lists)
23405 /* Output the location lists info. */
23406 switch_to_section (debug_loc_section);
23407 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23408 DEBUG_LOC_SECTION_LABEL, 0);
23409 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23410 output_location_lists (comp_unit_die ());
23413 /* Output public names table if necessary. */
23414 if (!VEC_empty (pubname_entry, pubname_table))
23416 gcc_assert (info_section_emitted);
23417 switch_to_section (debug_pubnames_section);
23418 output_pubnames (pubname_table);
23421 /* Output public types table if necessary. */
23422 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23423 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23424 simply won't look for the section. */
23425 if (!VEC_empty (pubname_entry, pubtype_table))
23427 bool empty = false;
23429 if (flag_eliminate_unused_debug_types)
23431 /* The pubtypes table might be emptied by pruning unused items. */
23432 unsigned i;
23433 pubname_ref p;
23434 empty = true;
23435 FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23436 if (p->die->die_offset != 0)
23438 empty = false;
23439 break;
23442 if (!empty)
23444 gcc_assert (info_section_emitted);
23445 switch_to_section (debug_pubtypes_section);
23446 output_pubnames (pubtype_table);
23450 /* Output direct and virtual call tables if necessary. */
23451 if (!VEC_empty (dcall_entry, dcall_table))
23453 switch_to_section (debug_dcall_section);
23454 output_dcall_table ();
23456 if (!VEC_empty (vcall_entry, vcall_table))
23458 switch_to_section (debug_vcall_section);
23459 output_vcall_table ();
23462 /* Output the address range information. We only put functions in the arange
23463 table, so don't write it out if we don't have any. */
23464 if (arange_table_in_use)
23466 switch_to_section (debug_aranges_section);
23467 output_aranges ();
23470 /* Output ranges section if necessary. */
23471 if (ranges_table_in_use)
23473 switch_to_section (debug_ranges_section);
23474 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23475 output_ranges ();
23478 /* Output the source line correspondence table. We must do this
23479 even if there is no line information. Otherwise, on an empty
23480 translation unit, we will generate a present, but empty,
23481 .debug_info section. IRIX 6.5 `nm' will then complain when
23482 examining the file. This is done late so that any filenames
23483 used by the debug_info section are marked as 'used'. */
23484 switch_to_section (debug_line_section);
23485 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23486 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23487 output_line_info ();
23489 /* Have to end the macro section. */
23490 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23492 switch_to_section (debug_macinfo_section);
23493 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23494 if (!VEC_empty (macinfo_entry, macinfo_table))
23495 output_macinfo ();
23496 dw2_asm_output_data (1, 0, "End compilation unit");
23499 /* If we emitted any DW_FORM_strp form attribute, output the string
23500 table too. */
23501 if (debug_str_hash)
23502 htab_traverse (debug_str_hash, output_indirect_string, NULL);
23505 #include "gt-dwarf2out.h"