Introduce -fno-debug-types-section flag.
[official-gcc.git] / gcc / dwarf2out.c
blob91be9a4b624eee9274c4694477e487d5fb38f458
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, 2011
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"
95 #include "cfglayout.h"
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX (gcc_unreachable (), NULL_RTX)
126 #endif
128 /* Map register numbers held in the call frame info that gcc has
129 collected using DWARF_FRAME_REGNUM to those that should be output in
130 .debug_frame and .eh_frame. */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
135 /* Save the result of dwarf2out_do_frame across PCH. */
136 static GTY(()) bool saved_do_cfi_asm = 0;
138 /* Decide whether we want to emit frame unwind information for the current
139 translation unit. */
142 dwarf2out_do_frame (void)
144 /* We want to emit correct CFA location expressions or lists, so we
145 have to return true if we're going to output debug info, even if
146 we're not going to output frame or unwind info. */
147 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148 return true;
150 if (saved_do_cfi_asm)
151 return true;
153 if (targetm.debug_unwind_info () == UI_DWARF2)
154 return true;
156 if ((flag_unwind_tables || flag_exceptions)
157 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158 return true;
160 return false;
163 /* Decide whether to emit frame unwind via assembler directives. */
166 dwarf2out_do_cfi_asm (void)
168 int enc;
170 #ifdef MIPS_DEBUGGING_INFO
171 return false;
172 #endif
173 if (saved_do_cfi_asm)
174 return true;
175 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176 return false;
177 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178 return false;
180 /* Make sure the personality encoding is one the assembler can support.
181 In particular, aligned addresses can't be handled. */
182 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184 return false;
185 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187 return false;
189 /* If we can't get the assembler to emit only .debug_frame, and we don't need
190 dwarf2 unwind info for exceptions, then emit .debug_frame by hand. */
191 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192 && !flag_unwind_tables && !flag_exceptions
193 && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194 return false;
196 saved_do_cfi_asm = true;
197 return true;
200 /* The size of the target's pointer type. */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
205 /* Array of RTXes referenced by the debugging information, which therefore
206 must be kept around forever. */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
209 /* A pointer to the base of a list of incomplete types which might be
210 completed at some later time. incomplete_types_list needs to be a
211 VEC(tree,gc) because we want to tell the garbage collector about
212 it. */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
215 /* A pointer to the base of a table of references to declaration
216 scopes. This table is a display which tracks the nesting
217 of declaration scopes at the current scope and containing
218 scopes. This table is used to find the proper place to
219 define type declaration DIE's. */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
222 /* Pointers to various DWARF2 sections. */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
235 /* Personality decl of current unit. Used only when assembler does not support
236 personality CFI. */
237 static GTY(()) rtx current_unit_personality;
239 /* How to start an assembler comment. */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
248 /* Call frames are described using a sequence of Call Frame
249 Information instructions. The register number, offset
250 and address fields are provided as possible operands;
251 their use is selected by the opcode field. */
253 enum dw_cfi_oprnd_type {
254 dw_cfi_oprnd_unused,
255 dw_cfi_oprnd_reg_num,
256 dw_cfi_oprnd_offset,
257 dw_cfi_oprnd_addr,
258 dw_cfi_oprnd_loc
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
267 dw_cfi_oprnd;
269 typedef struct GTY(()) dw_cfi_struct {
270 dw_cfi_ref dw_cfi_next;
271 enum dwarf_call_frame_info dw_cfi_opc;
272 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
273 dw_cfi_oprnd1;
274 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
275 dw_cfi_oprnd2;
277 dw_cfi_node;
279 /* This is how we define the location of the CFA. We use to handle it
280 as REG + OFFSET all the time, but now it can be more complex.
281 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
282 Instead of passing around REG and OFFSET, we pass a copy
283 of this structure. */
284 typedef struct cfa_loc {
285 HOST_WIDE_INT offset;
286 HOST_WIDE_INT base_offset;
287 unsigned int reg;
288 BOOL_BITFIELD indirect : 1; /* 1 if CFA is accessed via a dereference. */
289 BOOL_BITFIELD in_use : 1; /* 1 if a saved cfa is stored here. */
290 } dw_cfa_location;
292 /* All call frame descriptions (FDE's) in the GCC generated DWARF
293 refer to a single Common Information Entry (CIE), defined at
294 the beginning of the .debug_frame section. This use of a single
295 CIE obviates the need to keep track of multiple CIE's
296 in the DWARF generation routines below. */
298 typedef struct GTY(()) dw_fde_struct {
299 tree decl;
300 const char *dw_fde_begin;
301 const char *dw_fde_current_label;
302 const char *dw_fde_end;
303 const char *dw_fde_vms_end_prologue;
304 const char *dw_fde_vms_begin_epilogue;
305 const char *dw_fde_second_begin;
306 const char *dw_fde_second_end;
307 dw_cfi_ref dw_fde_cfi;
308 dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections. */
309 HOST_WIDE_INT stack_realignment;
310 unsigned funcdef_number;
311 /* Dynamic realign argument pointer register. */
312 unsigned int drap_reg;
313 /* Virtual dynamic realign argument pointer register. */
314 unsigned int vdrap_reg;
315 /* These 3 flags are copied from rtl_data in function.h. */
316 unsigned all_throwers_are_sibcalls : 1;
317 unsigned uses_eh_lsda : 1;
318 unsigned nothrow : 1;
319 /* Whether we did stack realign in this call frame. */
320 unsigned stack_realign : 1;
321 /* Whether dynamic realign argument pointer register has been saved. */
322 unsigned drap_reg_saved: 1;
323 /* True iff dw_fde_begin label is in text_section or cold_text_section. */
324 unsigned in_std_section : 1;
325 /* True iff dw_fde_second_begin label is in text_section or
326 cold_text_section. */
327 unsigned second_in_std_section : 1;
329 dw_fde_node;
331 /* Maximum size (in bytes) of an artificially generated label. */
332 #define MAX_ARTIFICIAL_LABEL_BYTES 30
334 /* The size of addresses as they appear in the Dwarf 2 data.
335 Some architectures use word addresses to refer to code locations,
336 but Dwarf 2 info always uses byte addresses. On such machines,
337 Dwarf 2 addresses need to be larger than the architecture's
338 pointers. */
339 #ifndef DWARF2_ADDR_SIZE
340 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
341 #endif
343 /* The size in bytes of a DWARF field indicating an offset or length
344 relative to a debug info section, specified to be 4 bytes in the
345 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
346 as PTR_SIZE. */
348 #ifndef DWARF_OFFSET_SIZE
349 #define DWARF_OFFSET_SIZE 4
350 #endif
352 /* The size in bytes of a DWARF 4 type signature. */
354 #ifndef DWARF_TYPE_SIGNATURE_SIZE
355 #define DWARF_TYPE_SIGNATURE_SIZE 8
356 #endif
358 /* According to the (draft) DWARF 3 specification, the initial length
359 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
360 bytes are 0xffffffff, followed by the length stored in the next 8
361 bytes.
363 However, the SGI/MIPS ABI uses an initial length which is equal to
364 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
366 #ifndef DWARF_INITIAL_LENGTH_SIZE
367 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
368 #endif
370 /* Round SIZE up to the nearest BOUNDARY. */
371 #define DWARF_ROUND(SIZE,BOUNDARY) \
372 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
374 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
375 #ifndef DWARF_CIE_DATA_ALIGNMENT
376 #ifdef STACK_GROWS_DOWNWARD
377 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
378 #else
379 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
380 #endif
381 #endif
383 /* CIE identifier. */
384 #if HOST_BITS_PER_WIDE_INT >= 64
385 #define DWARF_CIE_ID \
386 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
387 #else
388 #define DWARF_CIE_ID DW_CIE_ID
389 #endif
391 /* A pointer to the base of a table that contains frame description
392 information for each routine. */
393 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
395 /* Number of elements currently allocated for fde_table. */
396 static GTY(()) unsigned fde_table_allocated;
398 /* Number of elements in fde_table currently in use. */
399 static GTY(()) unsigned fde_table_in_use;
401 /* Size (in elements) of increments by which we may expand the
402 fde_table. */
403 #define FDE_TABLE_INCREMENT 256
405 /* Get the current fde_table entry we should use. */
407 static inline dw_fde_ref
408 current_fde (void)
410 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
413 /* A list of call frame insns for the CIE. */
414 static GTY(()) dw_cfi_ref cie_cfi_head;
416 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
417 attribute that accelerates the lookup of the FDE associated
418 with the subprogram. This variable holds the table index of the FDE
419 associated with the current function (body) definition. */
420 static unsigned current_funcdef_fde;
422 struct GTY(()) indirect_string_node {
423 const char *str;
424 unsigned int refcount;
425 enum dwarf_form form;
426 char *label;
429 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
431 /* True if the compilation unit has location entries that reference
432 debug strings. */
433 static GTY(()) bool debug_str_hash_forced = false;
435 static GTY(()) int dw2_string_counter;
436 static GTY(()) unsigned long dwarf2out_cfi_label_num;
438 /* True if the compilation unit places functions in more than one section. */
439 static GTY(()) bool have_multiple_function_sections = false;
441 /* Whether the default text and cold text sections have been used at all. */
443 static GTY(()) bool text_section_used = false;
444 static GTY(()) bool cold_text_section_used = false;
446 /* The default cold text section. */
447 static GTY(()) section *cold_text_section;
449 /* Forward declarations for functions defined in this file. */
451 static char *stripattributes (const char *);
452 static const char *dwarf_cfi_name (unsigned);
453 static dw_cfi_ref new_cfi (void);
454 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
455 static void add_fde_cfi (const char *, dw_cfi_ref);
456 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
457 static void lookup_cfa (dw_cfa_location *);
458 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
459 static void initial_return_save (rtx);
460 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
461 HOST_WIDE_INT);
462 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
463 static void output_cfi_directive (dw_cfi_ref);
464 static void output_call_frame_info (int);
465 static void dwarf2out_note_section_used (void);
466 static bool clobbers_queued_reg_save (const_rtx);
467 static void dwarf2out_frame_debug_expr (rtx, const char *);
469 /* Support for complex CFA locations. */
470 static void output_cfa_loc (dw_cfi_ref, int);
471 static void output_cfa_loc_raw (dw_cfi_ref);
472 static void get_cfa_from_loc_descr (dw_cfa_location *,
473 struct dw_loc_descr_struct *);
474 static struct dw_loc_descr_struct *build_cfa_loc
475 (dw_cfa_location *, HOST_WIDE_INT);
476 static struct dw_loc_descr_struct *build_cfa_aligned_loc
477 (HOST_WIDE_INT, HOST_WIDE_INT);
478 static void def_cfa_1 (const char *, dw_cfa_location *);
479 static struct dw_loc_descr_struct *mem_loc_descriptor
480 (rtx, enum machine_mode mode, enum var_init_status);
482 /* How to start an assembler comment. */
483 #ifndef ASM_COMMENT_START
484 #define ASM_COMMENT_START ";#"
485 #endif
487 /* Data and reference forms for relocatable data. */
488 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
489 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
491 #ifndef DEBUG_FRAME_SECTION
492 #define DEBUG_FRAME_SECTION ".debug_frame"
493 #endif
495 #ifndef FUNC_BEGIN_LABEL
496 #define FUNC_BEGIN_LABEL "LFB"
497 #endif
499 #ifndef FUNC_END_LABEL
500 #define FUNC_END_LABEL "LFE"
501 #endif
503 #ifndef PROLOGUE_END_LABEL
504 #define PROLOGUE_END_LABEL "LPE"
505 #endif
507 #ifndef EPILOGUE_BEGIN_LABEL
508 #define EPILOGUE_BEGIN_LABEL "LEB"
509 #endif
511 #ifndef FRAME_BEGIN_LABEL
512 #define FRAME_BEGIN_LABEL "Lframe"
513 #endif
514 #define CIE_AFTER_SIZE_LABEL "LSCIE"
515 #define CIE_END_LABEL "LECIE"
516 #define FDE_LABEL "LSFDE"
517 #define FDE_AFTER_SIZE_LABEL "LASFDE"
518 #define FDE_END_LABEL "LEFDE"
519 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
520 #define LINE_NUMBER_END_LABEL "LELT"
521 #define LN_PROLOG_AS_LABEL "LASLTP"
522 #define LN_PROLOG_END_LABEL "LELTP"
523 #define DIE_LABEL_PREFIX "DW"
525 /* The DWARF 2 CFA column which tracks the return address. Normally this
526 is the column for PC, or the first column after all of the hard
527 registers. */
528 #ifndef DWARF_FRAME_RETURN_COLUMN
529 #ifdef PC_REGNUM
530 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
531 #else
532 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
533 #endif
534 #endif
536 /* The mapping from gcc register number to DWARF 2 CFA column number. By
537 default, we just provide columns for all registers. */
538 #ifndef DWARF_FRAME_REGNUM
539 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
540 #endif
542 /* Match the base name of a file to the base name of a compilation unit. */
544 static int
545 matches_main_base (const char *path)
547 /* Cache the last query. */
548 static const char *last_path = NULL;
549 static int last_match = 0;
550 if (path != last_path)
552 const char *base;
553 int length = base_of_path (path, &base);
554 last_path = path;
555 last_match = (length == main_input_baselength
556 && memcmp (base, main_input_basename, length) == 0);
558 return last_match;
561 #ifdef DEBUG_DEBUG_STRUCT
563 static int
564 dump_struct_debug (tree type, enum debug_info_usage usage,
565 enum debug_struct_file criterion, int generic,
566 int matches, int result)
568 /* Find the type name. */
569 tree type_decl = TYPE_STUB_DECL (type);
570 tree t = type_decl;
571 const char *name = 0;
572 if (TREE_CODE (t) == TYPE_DECL)
573 t = DECL_NAME (t);
574 if (t)
575 name = IDENTIFIER_POINTER (t);
577 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
578 criterion,
579 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
580 matches ? "bas" : "hdr",
581 generic ? "gen" : "ord",
582 usage == DINFO_USAGE_DFN ? ";" :
583 usage == DINFO_USAGE_DIR_USE ? "." : "*",
584 result,
585 (void*) type_decl, name);
586 return result;
588 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
589 dump_struct_debug (type, usage, criterion, generic, matches, result)
591 #else
593 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
594 (result)
596 #endif
598 static bool
599 should_emit_struct_debug (tree type, enum debug_info_usage usage)
601 enum debug_struct_file criterion;
602 tree type_decl;
603 bool generic = lang_hooks.types.generic_p (type);
605 if (generic)
606 criterion = debug_struct_generic[usage];
607 else
608 criterion = debug_struct_ordinary[usage];
610 if (criterion == DINFO_STRUCT_FILE_NONE)
611 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
612 if (criterion == DINFO_STRUCT_FILE_ANY)
613 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
615 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
617 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
618 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
620 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
621 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
622 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
625 /* Hook used by __throw. */
628 expand_builtin_dwarf_sp_column (void)
630 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
631 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
634 /* Return a pointer to a copy of the section string name S with all
635 attributes stripped off, and an asterisk prepended (for assemble_name). */
637 static inline char *
638 stripattributes (const char *s)
640 char *stripped = XNEWVEC (char, strlen (s) + 2);
641 char *p = stripped;
643 *p++ = '*';
645 while (*s && *s != ',')
646 *p++ = *s++;
648 *p = '\0';
649 return stripped;
652 /* MEM is a memory reference for the register size table, each element of
653 which has mode MODE. Initialize column C as a return address column. */
655 static void
656 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
658 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
659 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
660 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
663 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
665 static inline HOST_WIDE_INT
666 div_data_align (HOST_WIDE_INT off)
668 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
669 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
670 return r;
673 /* Return true if we need a signed version of a given opcode
674 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
676 static inline bool
677 need_data_align_sf_opcode (HOST_WIDE_INT off)
679 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
682 /* Generate code to initialize the register size table. */
684 void
685 expand_builtin_init_dwarf_reg_sizes (tree address)
687 unsigned int i;
688 enum machine_mode mode = TYPE_MODE (char_type_node);
689 rtx addr = expand_normal (address);
690 rtx mem = gen_rtx_MEM (BLKmode, addr);
691 bool wrote_return_column = false;
693 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
695 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
697 if (rnum < DWARF_FRAME_REGISTERS)
699 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
700 enum machine_mode save_mode = reg_raw_mode[i];
701 HOST_WIDE_INT size;
703 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
704 save_mode = choose_hard_reg_mode (i, 1, true);
705 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
707 if (save_mode == VOIDmode)
708 continue;
709 wrote_return_column = true;
711 size = GET_MODE_SIZE (save_mode);
712 if (offset < 0)
713 continue;
715 emit_move_insn (adjust_address (mem, mode, offset),
716 gen_int_mode (size, mode));
720 if (!wrote_return_column)
721 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
723 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
724 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
725 #endif
727 targetm.init_dwarf_reg_sizes_extra (address);
730 /* Convert a DWARF call frame info. operation to its string name */
732 static const char *
733 dwarf_cfi_name (unsigned int cfi_opc)
735 switch (cfi_opc)
737 case DW_CFA_advance_loc:
738 return "DW_CFA_advance_loc";
739 case DW_CFA_offset:
740 return "DW_CFA_offset";
741 case DW_CFA_restore:
742 return "DW_CFA_restore";
743 case DW_CFA_nop:
744 return "DW_CFA_nop";
745 case DW_CFA_set_loc:
746 return "DW_CFA_set_loc";
747 case DW_CFA_advance_loc1:
748 return "DW_CFA_advance_loc1";
749 case DW_CFA_advance_loc2:
750 return "DW_CFA_advance_loc2";
751 case DW_CFA_advance_loc4:
752 return "DW_CFA_advance_loc4";
753 case DW_CFA_offset_extended:
754 return "DW_CFA_offset_extended";
755 case DW_CFA_restore_extended:
756 return "DW_CFA_restore_extended";
757 case DW_CFA_undefined:
758 return "DW_CFA_undefined";
759 case DW_CFA_same_value:
760 return "DW_CFA_same_value";
761 case DW_CFA_register:
762 return "DW_CFA_register";
763 case DW_CFA_remember_state:
764 return "DW_CFA_remember_state";
765 case DW_CFA_restore_state:
766 return "DW_CFA_restore_state";
767 case DW_CFA_def_cfa:
768 return "DW_CFA_def_cfa";
769 case DW_CFA_def_cfa_register:
770 return "DW_CFA_def_cfa_register";
771 case DW_CFA_def_cfa_offset:
772 return "DW_CFA_def_cfa_offset";
774 /* DWARF 3 */
775 case DW_CFA_def_cfa_expression:
776 return "DW_CFA_def_cfa_expression";
777 case DW_CFA_expression:
778 return "DW_CFA_expression";
779 case DW_CFA_offset_extended_sf:
780 return "DW_CFA_offset_extended_sf";
781 case DW_CFA_def_cfa_sf:
782 return "DW_CFA_def_cfa_sf";
783 case DW_CFA_def_cfa_offset_sf:
784 return "DW_CFA_def_cfa_offset_sf";
786 /* SGI/MIPS specific */
787 case DW_CFA_MIPS_advance_loc8:
788 return "DW_CFA_MIPS_advance_loc8";
790 /* GNU extensions */
791 case DW_CFA_GNU_window_save:
792 return "DW_CFA_GNU_window_save";
793 case DW_CFA_GNU_args_size:
794 return "DW_CFA_GNU_args_size";
795 case DW_CFA_GNU_negative_offset_extended:
796 return "DW_CFA_GNU_negative_offset_extended";
798 default:
799 return "DW_CFA_<unknown>";
803 /* Return a pointer to a newly allocated Call Frame Instruction. */
805 static inline dw_cfi_ref
806 new_cfi (void)
808 dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
810 cfi->dw_cfi_next = NULL;
811 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
812 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
814 return cfi;
817 /* Add a Call Frame Instruction to list of instructions. */
819 static inline void
820 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
822 dw_cfi_ref *p;
823 dw_fde_ref fde = current_fde ();
825 /* When DRAP is used, CFA is defined with an expression. Redefine
826 CFA may lead to a different CFA value. */
827 /* ??? Of course, this heuristic fails when we're annotating epilogues,
828 because of course we'll always want to redefine the CFA back to the
829 stack pointer on the way out. Where should we move this check? */
830 if (0 && fde && fde->drap_reg != INVALID_REGNUM)
831 switch (cfi->dw_cfi_opc)
833 case DW_CFA_def_cfa_register:
834 case DW_CFA_def_cfa_offset:
835 case DW_CFA_def_cfa_offset_sf:
836 case DW_CFA_def_cfa:
837 case DW_CFA_def_cfa_sf:
838 gcc_unreachable ();
840 default:
841 break;
844 /* Find the end of the chain. */
845 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
848 *p = cfi;
851 /* Generate a new label for the CFI info to refer to. FORCE is true
852 if a label needs to be output even when using .cfi_* directives. */
854 char *
855 dwarf2out_cfi_label (bool force)
857 static char label[20];
859 if (!force && dwarf2out_do_cfi_asm ())
861 /* In this case, we will be emitting the asm directive instead of
862 the label, so just return a placeholder to keep the rest of the
863 interfaces happy. */
864 strcpy (label, "<do not output>");
866 else
868 int num = dwarf2out_cfi_label_num++;
869 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
870 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
873 return label;
876 /* True if remember_state should be emitted before following CFI directive. */
877 static bool emit_cfa_remember;
879 /* True if any CFI directives were emitted at the current insn. */
880 static bool any_cfis_emitted;
882 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
883 or to the CIE if LABEL is NULL. */
885 static void
886 add_fde_cfi (const char *label, dw_cfi_ref cfi)
888 dw_cfi_ref *list_head;
890 if (emit_cfa_remember)
892 dw_cfi_ref cfi_remember;
894 /* Emit the state save. */
895 emit_cfa_remember = false;
896 cfi_remember = new_cfi ();
897 cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
898 add_fde_cfi (label, cfi_remember);
901 list_head = &cie_cfi_head;
903 if (dwarf2out_do_cfi_asm ())
905 if (label)
907 dw_fde_ref fde = current_fde ();
909 gcc_assert (fde != NULL);
911 /* We still have to add the cfi to the list so that lookup_cfa
912 works later on. When -g2 and above we even need to force
913 emitting of CFI labels and add to list a DW_CFA_set_loc for
914 convert_cfa_to_fb_loc_list purposes. If we're generating
915 DWARF3 output we use DW_OP_call_frame_cfa and so don't use
916 convert_cfa_to_fb_loc_list. */
917 if (dwarf_version == 2
918 && debug_info_level > DINFO_LEVEL_TERSE
919 && (write_symbols == DWARF2_DEBUG
920 || write_symbols == VMS_AND_DWARF2_DEBUG))
922 switch (cfi->dw_cfi_opc)
924 case DW_CFA_def_cfa_offset:
925 case DW_CFA_def_cfa_offset_sf:
926 case DW_CFA_def_cfa_register:
927 case DW_CFA_def_cfa:
928 case DW_CFA_def_cfa_sf:
929 case DW_CFA_def_cfa_expression:
930 case DW_CFA_restore_state:
931 if (*label == 0 || strcmp (label, "<do not output>") == 0)
932 label = dwarf2out_cfi_label (true);
934 if (fde->dw_fde_current_label == NULL
935 || strcmp (label, fde->dw_fde_current_label) != 0)
937 dw_cfi_ref xcfi;
939 label = xstrdup (label);
941 /* Set the location counter to the new label. */
942 xcfi = new_cfi ();
943 /* It doesn't metter whether DW_CFA_set_loc
944 or DW_CFA_advance_loc4 is added here, those aren't
945 emitted into assembly, only looked up by
946 convert_cfa_to_fb_loc_list. */
947 xcfi->dw_cfi_opc = DW_CFA_set_loc;
948 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
949 add_cfi (&fde->dw_fde_cfi, xcfi);
950 fde->dw_fde_current_label = label;
952 break;
953 default:
954 break;
958 output_cfi_directive (cfi);
960 list_head = &fde->dw_fde_cfi;
961 any_cfis_emitted = true;
963 /* ??? If this is a CFI for the CIE, we don't emit. This
964 assumes that the standard CIE contents that the assembler
965 uses matches the standard CIE contents that the compiler
966 uses. This is probably a bad assumption. I'm not quite
967 sure how to address this for now. */
969 else if (label)
971 dw_fde_ref fde = current_fde ();
973 gcc_assert (fde != NULL);
975 if (*label == 0)
976 label = dwarf2out_cfi_label (false);
978 if (fde->dw_fde_current_label == NULL
979 || strcmp (label, fde->dw_fde_current_label) != 0)
981 dw_cfi_ref xcfi;
983 label = xstrdup (label);
985 /* Set the location counter to the new label. */
986 xcfi = new_cfi ();
987 /* If we have a current label, advance from there, otherwise
988 set the location directly using set_loc. */
989 xcfi->dw_cfi_opc = fde->dw_fde_current_label
990 ? DW_CFA_advance_loc4
991 : DW_CFA_set_loc;
992 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
993 add_cfi (&fde->dw_fde_cfi, xcfi);
995 fde->dw_fde_current_label = label;
998 list_head = &fde->dw_fde_cfi;
999 any_cfis_emitted = true;
1002 add_cfi (list_head, cfi);
1005 /* Subroutine of lookup_cfa. */
1007 static void
1008 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1010 switch (cfi->dw_cfi_opc)
1012 case DW_CFA_def_cfa_offset:
1013 case DW_CFA_def_cfa_offset_sf:
1014 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1015 break;
1016 case DW_CFA_def_cfa_register:
1017 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1018 break;
1019 case DW_CFA_def_cfa:
1020 case DW_CFA_def_cfa_sf:
1021 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1022 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1023 break;
1024 case DW_CFA_def_cfa_expression:
1025 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1026 break;
1028 case DW_CFA_remember_state:
1029 gcc_assert (!remember->in_use);
1030 *remember = *loc;
1031 remember->in_use = 1;
1032 break;
1033 case DW_CFA_restore_state:
1034 gcc_assert (remember->in_use);
1035 *loc = *remember;
1036 remember->in_use = 0;
1037 break;
1039 default:
1040 break;
1044 /* Find the previous value for the CFA. */
1046 static void
1047 lookup_cfa (dw_cfa_location *loc)
1049 dw_cfi_ref cfi;
1050 dw_fde_ref fde;
1051 dw_cfa_location remember;
1053 memset (loc, 0, sizeof (*loc));
1054 loc->reg = INVALID_REGNUM;
1055 remember = *loc;
1057 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1058 lookup_cfa_1 (cfi, loc, &remember);
1060 fde = current_fde ();
1061 if (fde)
1062 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1063 lookup_cfa_1 (cfi, loc, &remember);
1066 /* The current rule for calculating the DWARF2 canonical frame address. */
1067 static dw_cfa_location cfa;
1069 /* The register used for saving registers to the stack, and its offset
1070 from the CFA. */
1071 static dw_cfa_location cfa_store;
1073 /* The current save location around an epilogue. */
1074 static dw_cfa_location cfa_remember;
1076 /* The running total of the size of arguments pushed onto the stack. */
1077 static HOST_WIDE_INT args_size;
1079 /* The last args_size we actually output. */
1080 static HOST_WIDE_INT old_args_size;
1082 /* Entry point to update the canonical frame address (CFA).
1083 LABEL is passed to add_fde_cfi. The value of CFA is now to be
1084 calculated from REG+OFFSET. */
1086 void
1087 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1089 dw_cfa_location loc;
1090 loc.indirect = 0;
1091 loc.base_offset = 0;
1092 loc.reg = reg;
1093 loc.offset = offset;
1094 def_cfa_1 (label, &loc);
1097 /* Determine if two dw_cfa_location structures define the same data. */
1099 static bool
1100 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1102 return (loc1->reg == loc2->reg
1103 && loc1->offset == loc2->offset
1104 && loc1->indirect == loc2->indirect
1105 && (loc1->indirect == 0
1106 || loc1->base_offset == loc2->base_offset));
1109 /* This routine does the actual work. The CFA is now calculated from
1110 the dw_cfa_location structure. */
1112 static void
1113 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1115 dw_cfi_ref cfi;
1116 dw_cfa_location old_cfa, loc;
1118 cfa = *loc_p;
1119 loc = *loc_p;
1121 if (cfa_store.reg == loc.reg && loc.indirect == 0)
1122 cfa_store.offset = loc.offset;
1124 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1125 lookup_cfa (&old_cfa);
1127 /* If nothing changed, no need to issue any call frame instructions. */
1128 if (cfa_equal_p (&loc, &old_cfa))
1129 return;
1131 cfi = new_cfi ();
1133 if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1135 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1136 the CFA register did not change but the offset did. The data
1137 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1138 in the assembler via the .cfi_def_cfa_offset directive. */
1139 if (loc.offset < 0)
1140 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1141 else
1142 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1143 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1146 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
1147 else if (loc.offset == old_cfa.offset
1148 && old_cfa.reg != INVALID_REGNUM
1149 && !loc.indirect
1150 && !old_cfa.indirect)
1152 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1153 indicating the CFA register has changed to <register> but the
1154 offset has not changed. */
1155 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1156 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1158 #endif
1160 else if (loc.indirect == 0)
1162 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1163 indicating the CFA register has changed to <register> with
1164 the specified offset. The data factoring for DW_CFA_def_cfa_sf
1165 happens in output_cfi, or in the assembler via the .cfi_def_cfa
1166 directive. */
1167 if (loc.offset < 0)
1168 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1169 else
1170 cfi->dw_cfi_opc = DW_CFA_def_cfa;
1171 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1172 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1174 else
1176 /* Construct a DW_CFA_def_cfa_expression instruction to
1177 calculate the CFA using a full location expression since no
1178 register-offset pair is available. */
1179 struct dw_loc_descr_struct *loc_list;
1181 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1182 loc_list = build_cfa_loc (&loc, 0);
1183 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1186 add_fde_cfi (label, cfi);
1189 /* Add the CFI for saving a register. REG is the CFA column number.
1190 LABEL is passed to add_fde_cfi.
1191 If SREG is -1, the register is saved at OFFSET from the CFA;
1192 otherwise it is saved in SREG. */
1194 static void
1195 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1197 dw_cfi_ref cfi = new_cfi ();
1198 dw_fde_ref fde = current_fde ();
1200 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1202 /* When stack is aligned, store REG using DW_CFA_expression with
1203 FP. */
1204 if (fde
1205 && fde->stack_realign
1206 && sreg == INVALID_REGNUM)
1208 cfi->dw_cfi_opc = DW_CFA_expression;
1209 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1210 cfi->dw_cfi_oprnd2.dw_cfi_loc
1211 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1213 else if (sreg == INVALID_REGNUM)
1215 if (need_data_align_sf_opcode (offset))
1216 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1217 else if (reg & ~0x3f)
1218 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1219 else
1220 cfi->dw_cfi_opc = DW_CFA_offset;
1221 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1223 else if (sreg == reg)
1224 cfi->dw_cfi_opc = DW_CFA_same_value;
1225 else
1227 cfi->dw_cfi_opc = DW_CFA_register;
1228 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1231 add_fde_cfi (label, cfi);
1234 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1235 This CFI tells the unwinder that it needs to restore the window registers
1236 from the previous frame's window save area.
1238 ??? Perhaps we should note in the CIE where windows are saved (instead of
1239 assuming 0(cfa)) and what registers are in the window. */
1241 void
1242 dwarf2out_window_save (const char *label)
1244 dw_cfi_ref cfi = new_cfi ();
1246 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1247 add_fde_cfi (label, cfi);
1250 /* Entry point for saving a register to the stack. REG is the GCC register
1251 number. LABEL and OFFSET are passed to reg_save. */
1253 void
1254 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1256 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1259 /* Entry point for saving the return address in the stack.
1260 LABEL and OFFSET are passed to reg_save. */
1262 void
1263 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1265 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1268 /* Entry point for saving the return address in a register.
1269 LABEL and SREG are passed to reg_save. */
1271 void
1272 dwarf2out_return_reg (const char *label, unsigned int sreg)
1274 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1277 /* Record the initial position of the return address. RTL is
1278 INCOMING_RETURN_ADDR_RTX. */
1280 static void
1281 initial_return_save (rtx rtl)
1283 unsigned int reg = INVALID_REGNUM;
1284 HOST_WIDE_INT offset = 0;
1286 switch (GET_CODE (rtl))
1288 case REG:
1289 /* RA is in a register. */
1290 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1291 break;
1293 case MEM:
1294 /* RA is on the stack. */
1295 rtl = XEXP (rtl, 0);
1296 switch (GET_CODE (rtl))
1298 case REG:
1299 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1300 offset = 0;
1301 break;
1303 case PLUS:
1304 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1305 offset = INTVAL (XEXP (rtl, 1));
1306 break;
1308 case MINUS:
1309 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1310 offset = -INTVAL (XEXP (rtl, 1));
1311 break;
1313 default:
1314 gcc_unreachable ();
1317 break;
1319 case PLUS:
1320 /* The return address is at some offset from any value we can
1321 actually load. For instance, on the SPARC it is in %i7+8. Just
1322 ignore the offset for now; it doesn't matter for unwinding frames. */
1323 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1324 initial_return_save (XEXP (rtl, 0));
1325 return;
1327 default:
1328 gcc_unreachable ();
1331 if (reg != DWARF_FRAME_RETURN_COLUMN)
1332 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1335 /* Given a SET, calculate the amount of stack adjustment it
1336 contains. */
1338 static HOST_WIDE_INT
1339 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1340 HOST_WIDE_INT cur_offset)
1342 const_rtx src = SET_SRC (pattern);
1343 const_rtx dest = SET_DEST (pattern);
1344 HOST_WIDE_INT offset = 0;
1345 enum rtx_code code;
1347 if (dest == stack_pointer_rtx)
1349 code = GET_CODE (src);
1351 /* Assume (set (reg sp) (reg whatever)) sets args_size
1352 level to 0. */
1353 if (code == REG && src != stack_pointer_rtx)
1355 offset = -cur_args_size;
1356 #ifndef STACK_GROWS_DOWNWARD
1357 offset = -offset;
1358 #endif
1359 return offset - cur_offset;
1362 if (! (code == PLUS || code == MINUS)
1363 || XEXP (src, 0) != stack_pointer_rtx
1364 || !CONST_INT_P (XEXP (src, 1)))
1365 return 0;
1367 /* (set (reg sp) (plus (reg sp) (const_int))) */
1368 offset = INTVAL (XEXP (src, 1));
1369 if (code == PLUS)
1370 offset = -offset;
1371 return offset;
1374 if (MEM_P (src) && !MEM_P (dest))
1375 dest = src;
1376 if (MEM_P (dest))
1378 /* (set (mem (pre_dec (reg sp))) (foo)) */
1379 src = XEXP (dest, 0);
1380 code = GET_CODE (src);
1382 switch (code)
1384 case PRE_MODIFY:
1385 case POST_MODIFY:
1386 if (XEXP (src, 0) == stack_pointer_rtx)
1388 rtx val = XEXP (XEXP (src, 1), 1);
1389 /* We handle only adjustments by constant amount. */
1390 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1391 && CONST_INT_P (val));
1392 offset = -INTVAL (val);
1393 break;
1395 return 0;
1397 case PRE_DEC:
1398 case POST_DEC:
1399 if (XEXP (src, 0) == stack_pointer_rtx)
1401 offset = GET_MODE_SIZE (GET_MODE (dest));
1402 break;
1404 return 0;
1406 case PRE_INC:
1407 case POST_INC:
1408 if (XEXP (src, 0) == stack_pointer_rtx)
1410 offset = -GET_MODE_SIZE (GET_MODE (dest));
1411 break;
1413 return 0;
1415 default:
1416 return 0;
1419 else
1420 return 0;
1422 return offset;
1425 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1426 indexed by INSN_UID. */
1428 static HOST_WIDE_INT *barrier_args_size;
1430 /* Helper function for compute_barrier_args_size. Handle one insn. */
1432 static HOST_WIDE_INT
1433 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1434 VEC (rtx, heap) **next)
1436 HOST_WIDE_INT offset = 0;
1437 int i;
1439 if (! RTX_FRAME_RELATED_P (insn))
1441 if (prologue_epilogue_contains (insn))
1442 /* Nothing */;
1443 else if (GET_CODE (PATTERN (insn)) == SET)
1444 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1445 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1446 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1448 /* There may be stack adjustments inside compound insns. Search
1449 for them. */
1450 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1451 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1452 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1453 cur_args_size, offset);
1456 else
1458 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1460 if (expr)
1462 expr = XEXP (expr, 0);
1463 if (GET_CODE (expr) == PARALLEL
1464 || GET_CODE (expr) == SEQUENCE)
1465 for (i = 1; i < XVECLEN (expr, 0); i++)
1467 rtx elem = XVECEXP (expr, 0, i);
1469 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1470 offset += stack_adjust_offset (elem, cur_args_size, offset);
1475 #ifndef STACK_GROWS_DOWNWARD
1476 offset = -offset;
1477 #endif
1479 cur_args_size += offset;
1480 if (cur_args_size < 0)
1481 cur_args_size = 0;
1483 if (JUMP_P (insn))
1485 rtx dest = JUMP_LABEL (insn);
1487 if (dest)
1489 if (barrier_args_size [INSN_UID (dest)] < 0)
1491 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1492 VEC_safe_push (rtx, heap, *next, dest);
1497 return cur_args_size;
1500 /* Walk the whole function and compute args_size on BARRIERs. */
1502 static void
1503 compute_barrier_args_size (void)
1505 int max_uid = get_max_uid (), i;
1506 rtx insn;
1507 VEC (rtx, heap) *worklist, *next, *tmp;
1509 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1510 for (i = 0; i < max_uid; i++)
1511 barrier_args_size[i] = -1;
1513 worklist = VEC_alloc (rtx, heap, 20);
1514 next = VEC_alloc (rtx, heap, 20);
1515 insn = get_insns ();
1516 barrier_args_size[INSN_UID (insn)] = 0;
1517 VEC_quick_push (rtx, worklist, insn);
1518 for (;;)
1520 while (!VEC_empty (rtx, worklist))
1522 rtx prev, body, first_insn;
1523 HOST_WIDE_INT cur_args_size;
1525 first_insn = insn = VEC_pop (rtx, worklist);
1526 cur_args_size = barrier_args_size[INSN_UID (insn)];
1527 prev = prev_nonnote_insn (insn);
1528 if (prev && BARRIER_P (prev))
1529 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1531 for (; insn; insn = NEXT_INSN (insn))
1533 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1534 continue;
1535 if (BARRIER_P (insn))
1536 break;
1538 if (LABEL_P (insn))
1540 if (insn == first_insn)
1541 continue;
1542 else if (barrier_args_size[INSN_UID (insn)] < 0)
1544 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1545 continue;
1547 else
1549 /* The insns starting with this label have been
1550 already scanned or are in the worklist. */
1551 break;
1555 body = PATTERN (insn);
1556 if (GET_CODE (body) == SEQUENCE)
1558 HOST_WIDE_INT dest_args_size = cur_args_size;
1559 for (i = 1; i < XVECLEN (body, 0); i++)
1560 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1561 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1562 dest_args_size
1563 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1564 dest_args_size, &next);
1565 else
1566 cur_args_size
1567 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1568 cur_args_size, &next);
1570 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1571 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1572 dest_args_size, &next);
1573 else
1574 cur_args_size
1575 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1576 cur_args_size, &next);
1578 else
1579 cur_args_size
1580 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1584 if (VEC_empty (rtx, next))
1585 break;
1587 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1588 tmp = next;
1589 next = worklist;
1590 worklist = tmp;
1591 VEC_truncate (rtx, next, 0);
1594 VEC_free (rtx, heap, worklist);
1595 VEC_free (rtx, heap, next);
1598 /* Add a CFI to update the running total of the size of arguments
1599 pushed onto the stack. */
1601 static void
1602 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1604 dw_cfi_ref cfi;
1606 if (size == old_args_size)
1607 return;
1609 old_args_size = size;
1611 cfi = new_cfi ();
1612 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1613 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1614 add_fde_cfi (label, cfi);
1617 /* Record a stack adjustment of OFFSET bytes. */
1619 static void
1620 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1622 if (cfa.reg == STACK_POINTER_REGNUM)
1623 cfa.offset += offset;
1625 if (cfa_store.reg == STACK_POINTER_REGNUM)
1626 cfa_store.offset += offset;
1628 if (ACCUMULATE_OUTGOING_ARGS)
1629 return;
1631 #ifndef STACK_GROWS_DOWNWARD
1632 offset = -offset;
1633 #endif
1635 args_size += offset;
1636 if (args_size < 0)
1637 args_size = 0;
1639 def_cfa_1 (label, &cfa);
1640 if (flag_asynchronous_unwind_tables)
1641 dwarf2out_args_size (label, args_size);
1644 /* Check INSN to see if it looks like a push or a stack adjustment, and
1645 make a note of it if it does. EH uses this information to find out
1646 how much extra space it needs to pop off the stack. */
1648 static void
1649 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1651 HOST_WIDE_INT offset;
1652 const char *label;
1653 int i;
1655 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1656 with this function. Proper support would require all frame-related
1657 insns to be marked, and to be able to handle saving state around
1658 epilogues textually in the middle of the function. */
1659 if (prologue_epilogue_contains (insn))
1660 return;
1662 /* If INSN is an instruction from target of an annulled branch, the
1663 effects are for the target only and so current argument size
1664 shouldn't change at all. */
1665 if (final_sequence
1666 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1667 && INSN_FROM_TARGET_P (insn))
1668 return;
1670 /* If only calls can throw, and we have a frame pointer,
1671 save up adjustments until we see the CALL_INSN. */
1672 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1674 if (CALL_P (insn) && !after_p)
1676 /* Extract the size of the args from the CALL rtx itself. */
1677 insn = PATTERN (insn);
1678 if (GET_CODE (insn) == PARALLEL)
1679 insn = XVECEXP (insn, 0, 0);
1680 if (GET_CODE (insn) == SET)
1681 insn = SET_SRC (insn);
1682 gcc_assert (GET_CODE (insn) == CALL);
1683 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1685 return;
1688 if (CALL_P (insn) && !after_p)
1690 if (!flag_asynchronous_unwind_tables)
1691 dwarf2out_args_size ("", args_size);
1692 return;
1694 else if (BARRIER_P (insn))
1696 /* Don't call compute_barrier_args_size () if the only
1697 BARRIER is at the end of function. */
1698 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1699 compute_barrier_args_size ();
1700 if (barrier_args_size == NULL)
1701 offset = 0;
1702 else
1704 offset = barrier_args_size[INSN_UID (insn)];
1705 if (offset < 0)
1706 offset = 0;
1709 offset -= args_size;
1710 #ifndef STACK_GROWS_DOWNWARD
1711 offset = -offset;
1712 #endif
1714 else if (GET_CODE (PATTERN (insn)) == SET)
1715 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1716 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1717 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1719 /* There may be stack adjustments inside compound insns. Search
1720 for them. */
1721 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1722 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1723 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1724 args_size, offset);
1726 else
1727 return;
1729 if (offset == 0)
1730 return;
1732 label = dwarf2out_cfi_label (false);
1733 dwarf2out_stack_adjust (offset, label);
1736 /* We delay emitting a register save until either (a) we reach the end
1737 of the prologue or (b) the register is clobbered. This clusters
1738 register saves so that there are fewer pc advances. */
1740 struct GTY(()) queued_reg_save {
1741 struct queued_reg_save *next;
1742 rtx reg;
1743 HOST_WIDE_INT cfa_offset;
1744 rtx saved_reg;
1747 static GTY(()) struct queued_reg_save *queued_reg_saves;
1749 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1750 struct GTY(()) reg_saved_in_data {
1751 rtx orig_reg;
1752 rtx saved_in_reg;
1755 /* A list of registers saved in other registers.
1756 The list intentionally has a small maximum capacity of 4; if your
1757 port needs more than that, you might consider implementing a
1758 more efficient data structure. */
1759 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1760 static GTY(()) size_t num_regs_saved_in_regs;
1762 static const char *last_reg_save_label;
1764 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1765 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1767 static void
1768 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1770 struct queued_reg_save *q;
1772 /* Duplicates waste space, but it's also necessary to remove them
1773 for correctness, since the queue gets output in reverse
1774 order. */
1775 for (q = queued_reg_saves; q != NULL; q = q->next)
1776 if (REGNO (q->reg) == REGNO (reg))
1777 break;
1779 if (q == NULL)
1781 q = ggc_alloc_queued_reg_save ();
1782 q->next = queued_reg_saves;
1783 queued_reg_saves = q;
1786 q->reg = reg;
1787 q->cfa_offset = offset;
1788 q->saved_reg = sreg;
1790 last_reg_save_label = label;
1793 /* Output all the entries in QUEUED_REG_SAVES. */
1795 void
1796 dwarf2out_flush_queued_reg_saves (void)
1798 struct queued_reg_save *q;
1800 for (q = queued_reg_saves; q; q = q->next)
1802 size_t i;
1803 unsigned int reg, sreg;
1805 for (i = 0; i < num_regs_saved_in_regs; i++)
1806 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1807 break;
1808 if (q->saved_reg && i == num_regs_saved_in_regs)
1810 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1811 num_regs_saved_in_regs++;
1813 if (i != num_regs_saved_in_regs)
1815 regs_saved_in_regs[i].orig_reg = q->reg;
1816 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1819 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1820 if (q->saved_reg)
1821 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1822 else
1823 sreg = INVALID_REGNUM;
1824 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1827 queued_reg_saves = NULL;
1828 last_reg_save_label = NULL;
1831 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1832 location for? Or, does it clobber a register which we've previously
1833 said that some other register is saved in, and for which we now
1834 have a new location for? */
1836 static bool
1837 clobbers_queued_reg_save (const_rtx insn)
1839 struct queued_reg_save *q;
1841 for (q = queued_reg_saves; q; q = q->next)
1843 size_t i;
1844 if (modified_in_p (q->reg, insn))
1845 return true;
1846 for (i = 0; i < num_regs_saved_in_regs; i++)
1847 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1848 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1849 return true;
1852 return false;
1855 /* Entry point for saving the first register into the second. */
1857 void
1858 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1860 size_t i;
1861 unsigned int regno, sregno;
1863 for (i = 0; i < num_regs_saved_in_regs; i++)
1864 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1865 break;
1866 if (i == num_regs_saved_in_regs)
1868 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1869 num_regs_saved_in_regs++;
1871 regs_saved_in_regs[i].orig_reg = reg;
1872 regs_saved_in_regs[i].saved_in_reg = sreg;
1874 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1875 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1876 reg_save (label, regno, sregno, 0);
1879 /* What register, if any, is currently saved in REG? */
1881 static rtx
1882 reg_saved_in (rtx reg)
1884 unsigned int regn = REGNO (reg);
1885 size_t i;
1886 struct queued_reg_save *q;
1888 for (q = queued_reg_saves; q; q = q->next)
1889 if (q->saved_reg && regn == REGNO (q->saved_reg))
1890 return q->reg;
1892 for (i = 0; i < num_regs_saved_in_regs; i++)
1893 if (regs_saved_in_regs[i].saved_in_reg
1894 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1895 return regs_saved_in_regs[i].orig_reg;
1897 return NULL_RTX;
1901 /* A temporary register holding an integral value used in adjusting SP
1902 or setting up the store_reg. The "offset" field holds the integer
1903 value, not an offset. */
1904 static dw_cfa_location cfa_temp;
1906 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1908 static void
1909 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1911 memset (&cfa, 0, sizeof (cfa));
1913 switch (GET_CODE (pat))
1915 case PLUS:
1916 cfa.reg = REGNO (XEXP (pat, 0));
1917 cfa.offset = INTVAL (XEXP (pat, 1));
1918 break;
1920 case REG:
1921 cfa.reg = REGNO (pat);
1922 break;
1924 case MEM:
1925 cfa.indirect = 1;
1926 pat = XEXP (pat, 0);
1927 if (GET_CODE (pat) == PLUS)
1929 cfa.base_offset = INTVAL (XEXP (pat, 1));
1930 pat = XEXP (pat, 0);
1932 cfa.reg = REGNO (pat);
1933 break;
1935 default:
1936 /* Recurse and define an expression. */
1937 gcc_unreachable ();
1940 def_cfa_1 (label, &cfa);
1943 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1945 static void
1946 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1948 rtx src, dest;
1950 gcc_assert (GET_CODE (pat) == SET);
1951 dest = XEXP (pat, 0);
1952 src = XEXP (pat, 1);
1954 switch (GET_CODE (src))
1956 case PLUS:
1957 gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1958 cfa.offset -= INTVAL (XEXP (src, 1));
1959 break;
1961 case REG:
1962 break;
1964 default:
1965 gcc_unreachable ();
1968 cfa.reg = REGNO (dest);
1969 gcc_assert (cfa.indirect == 0);
1971 def_cfa_1 (label, &cfa);
1974 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1976 static void
1977 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1979 HOST_WIDE_INT offset;
1980 rtx src, addr, span;
1982 src = XEXP (set, 1);
1983 addr = XEXP (set, 0);
1984 gcc_assert (MEM_P (addr));
1985 addr = XEXP (addr, 0);
1987 /* As documented, only consider extremely simple addresses. */
1988 switch (GET_CODE (addr))
1990 case REG:
1991 gcc_assert (REGNO (addr) == cfa.reg);
1992 offset = -cfa.offset;
1993 break;
1994 case PLUS:
1995 gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1996 offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1997 break;
1998 default:
1999 gcc_unreachable ();
2002 span = targetm.dwarf_register_span (src);
2004 /* ??? We'd like to use queue_reg_save, but we need to come up with
2005 a different flushing heuristic for epilogues. */
2006 if (!span)
2007 reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2008 else
2010 /* We have a PARALLEL describing where the contents of SRC live.
2011 Queue register saves for each piece of the PARALLEL. */
2012 int par_index;
2013 int limit;
2014 HOST_WIDE_INT span_offset = offset;
2016 gcc_assert (GET_CODE (span) == PARALLEL);
2018 limit = XVECLEN (span, 0);
2019 for (par_index = 0; par_index < limit; par_index++)
2021 rtx elem = XVECEXP (span, 0, par_index);
2023 reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2024 INVALID_REGNUM, span_offset);
2025 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2030 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
2032 static void
2033 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2035 rtx src, dest;
2036 unsigned sregno, dregno;
2038 src = XEXP (set, 1);
2039 dest = XEXP (set, 0);
2041 if (src == pc_rtx)
2042 sregno = DWARF_FRAME_RETURN_COLUMN;
2043 else
2044 sregno = DWARF_FRAME_REGNUM (REGNO (src));
2046 dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2048 /* ??? We'd like to use queue_reg_save, but we need to come up with
2049 a different flushing heuristic for epilogues. */
2050 reg_save (label, sregno, dregno, 0);
2053 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2055 static void
2056 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2058 rtx src, dest, span;
2059 dw_cfi_ref cfi = new_cfi ();
2061 dest = SET_DEST (set);
2062 src = SET_SRC (set);
2064 gcc_assert (REG_P (src));
2065 gcc_assert (MEM_P (dest));
2067 span = targetm.dwarf_register_span (src);
2068 gcc_assert (!span);
2070 cfi->dw_cfi_opc = DW_CFA_expression;
2071 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2072 cfi->dw_cfi_oprnd2.dw_cfi_loc
2073 = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2074 VAR_INIT_STATUS_INITIALIZED);
2076 /* ??? We'd like to use queue_reg_save, were the interface different,
2077 and, as above, we could manage flushing for epilogues. */
2078 add_fde_cfi (label, cfi);
2081 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
2083 static void
2084 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2086 dw_cfi_ref cfi = new_cfi ();
2087 unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2089 cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2090 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2092 add_fde_cfi (label, cfi);
2095 /* Record call frame debugging information for an expression EXPR,
2096 which either sets SP or FP (adjusting how we calculate the frame
2097 address) or saves a register to the stack or another register.
2098 LABEL indicates the address of EXPR.
2100 This function encodes a state machine mapping rtxes to actions on
2101 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
2102 users need not read the source code.
2104 The High-Level Picture
2106 Changes in the register we use to calculate the CFA: Currently we
2107 assume that if you copy the CFA register into another register, we
2108 should take the other one as the new CFA register; this seems to
2109 work pretty well. If it's wrong for some target, it's simple
2110 enough not to set RTX_FRAME_RELATED_P on the insn in question.
2112 Changes in the register we use for saving registers to the stack:
2113 This is usually SP, but not always. Again, we deduce that if you
2114 copy SP into another register (and SP is not the CFA register),
2115 then the new register is the one we will be using for register
2116 saves. This also seems to work.
2118 Register saves: There's not much guesswork about this one; if
2119 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2120 register save, and the register used to calculate the destination
2121 had better be the one we think we're using for this purpose.
2122 It's also assumed that a copy from a call-saved register to another
2123 register is saving that register if RTX_FRAME_RELATED_P is set on
2124 that instruction. If the copy is from a call-saved register to
2125 the *same* register, that means that the register is now the same
2126 value as in the caller.
2128 Except: If the register being saved is the CFA register, and the
2129 offset is nonzero, we are saving the CFA, so we assume we have to
2130 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
2131 the intent is to save the value of SP from the previous frame.
2133 In addition, if a register has previously been saved to a different
2134 register,
2136 Invariants / Summaries of Rules
2138 cfa current rule for calculating the CFA. It usually
2139 consists of a register and an offset.
2140 cfa_store register used by prologue code to save things to the stack
2141 cfa_store.offset is the offset from the value of
2142 cfa_store.reg to the actual CFA
2143 cfa_temp register holding an integral value. cfa_temp.offset
2144 stores the value, which will be used to adjust the
2145 stack pointer. cfa_temp is also used like cfa_store,
2146 to track stores to the stack via fp or a temp reg.
2148 Rules 1- 4: Setting a register's value to cfa.reg or an expression
2149 with cfa.reg as the first operand changes the cfa.reg and its
2150 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
2151 cfa_temp.offset.
2153 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
2154 expression yielding a constant. This sets cfa_temp.reg
2155 and cfa_temp.offset.
2157 Rule 5: Create a new register cfa_store used to save items to the
2158 stack.
2160 Rules 10-14: Save a register to the stack. Define offset as the
2161 difference of the original location and cfa_store's
2162 location (or cfa_temp's location if cfa_temp is used).
2164 Rules 16-20: If AND operation happens on sp in prologue, we assume
2165 stack is realigned. We will use a group of DW_OP_XXX
2166 expressions to represent the location of the stored
2167 register instead of CFA+offset.
2169 The Rules
2171 "{a,b}" indicates a choice of a xor b.
2172 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2174 Rule 1:
2175 (set <reg1> <reg2>:cfa.reg)
2176 effects: cfa.reg = <reg1>
2177 cfa.offset unchanged
2178 cfa_temp.reg = <reg1>
2179 cfa_temp.offset = cfa.offset
2181 Rule 2:
2182 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2183 {<const_int>,<reg>:cfa_temp.reg}))
2184 effects: cfa.reg = sp if fp used
2185 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2186 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2187 if cfa_store.reg==sp
2189 Rule 3:
2190 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2191 effects: cfa.reg = fp
2192 cfa_offset += +/- <const_int>
2194 Rule 4:
2195 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2196 constraints: <reg1> != fp
2197 <reg1> != sp
2198 effects: cfa.reg = <reg1>
2199 cfa_temp.reg = <reg1>
2200 cfa_temp.offset = cfa.offset
2202 Rule 5:
2203 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2204 constraints: <reg1> != fp
2205 <reg1> != sp
2206 effects: cfa_store.reg = <reg1>
2207 cfa_store.offset = cfa.offset - cfa_temp.offset
2209 Rule 6:
2210 (set <reg> <const_int>)
2211 effects: cfa_temp.reg = <reg>
2212 cfa_temp.offset = <const_int>
2214 Rule 7:
2215 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2216 effects: cfa_temp.reg = <reg1>
2217 cfa_temp.offset |= <const_int>
2219 Rule 8:
2220 (set <reg> (high <exp>))
2221 effects: none
2223 Rule 9:
2224 (set <reg> (lo_sum <exp> <const_int>))
2225 effects: cfa_temp.reg = <reg>
2226 cfa_temp.offset = <const_int>
2228 Rule 10:
2229 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2230 effects: cfa_store.offset -= <const_int>
2231 cfa.offset = cfa_store.offset if cfa.reg == sp
2232 cfa.reg = sp
2233 cfa.base_offset = -cfa_store.offset
2235 Rule 11:
2236 (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2237 effects: cfa_store.offset += -/+ mode_size(mem)
2238 cfa.offset = cfa_store.offset if cfa.reg == sp
2239 cfa.reg = sp
2240 cfa.base_offset = -cfa_store.offset
2242 Rule 12:
2243 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2245 <reg2>)
2246 effects: cfa.reg = <reg1>
2247 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2249 Rule 13:
2250 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2251 effects: cfa.reg = <reg1>
2252 cfa.base_offset = -{cfa_store,cfa_temp}.offset
2254 Rule 14:
2255 (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2256 effects: cfa.reg = <reg1>
2257 cfa.base_offset = -cfa_temp.offset
2258 cfa_temp.offset -= mode_size(mem)
2260 Rule 15:
2261 (set <reg> {unspec, unspec_volatile})
2262 effects: target-dependent
2264 Rule 16:
2265 (set sp (and: sp <const_int>))
2266 constraints: cfa_store.reg == sp
2267 effects: current_fde.stack_realign = 1
2268 cfa_store.offset = 0
2269 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2271 Rule 17:
2272 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2273 effects: cfa_store.offset += -/+ mode_size(mem)
2275 Rule 18:
2276 (set (mem ({pre_inc, pre_dec} sp)) fp)
2277 constraints: fde->stack_realign == 1
2278 effects: cfa_store.offset = 0
2279 cfa.reg != HARD_FRAME_POINTER_REGNUM
2281 Rule 19:
2282 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2283 constraints: fde->stack_realign == 1
2284 && cfa.offset == 0
2285 && cfa.indirect == 0
2286 && cfa.reg != HARD_FRAME_POINTER_REGNUM
2287 effects: Use DW_CFA_def_cfa_expression to define cfa
2288 cfa.reg == fde->drap_reg */
2290 static void
2291 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2293 rtx src, dest, span;
2294 HOST_WIDE_INT offset;
2295 dw_fde_ref fde;
2297 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2298 the PARALLEL independently. The first element is always processed if
2299 it is a SET. This is for backward compatibility. Other elements
2300 are processed only if they are SETs and the RTX_FRAME_RELATED_P
2301 flag is set in them. */
2302 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2304 int par_index;
2305 int limit = XVECLEN (expr, 0);
2306 rtx elem;
2308 /* PARALLELs have strict read-modify-write semantics, so we
2309 ought to evaluate every rvalue before changing any lvalue.
2310 It's cumbersome to do that in general, but there's an
2311 easy approximation that is enough for all current users:
2312 handle register saves before register assignments. */
2313 if (GET_CODE (expr) == PARALLEL)
2314 for (par_index = 0; par_index < limit; par_index++)
2316 elem = XVECEXP (expr, 0, par_index);
2317 if (GET_CODE (elem) == SET
2318 && MEM_P (SET_DEST (elem))
2319 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2320 dwarf2out_frame_debug_expr (elem, label);
2323 for (par_index = 0; par_index < limit; par_index++)
2325 elem = XVECEXP (expr, 0, par_index);
2326 if (GET_CODE (elem) == SET
2327 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2328 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2329 dwarf2out_frame_debug_expr (elem, label);
2330 else if (GET_CODE (elem) == SET
2331 && par_index != 0
2332 && !RTX_FRAME_RELATED_P (elem))
2334 /* Stack adjustment combining might combine some post-prologue
2335 stack adjustment into a prologue stack adjustment. */
2336 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2338 if (offset != 0)
2339 dwarf2out_stack_adjust (offset, label);
2342 return;
2345 gcc_assert (GET_CODE (expr) == SET);
2347 src = SET_SRC (expr);
2348 dest = SET_DEST (expr);
2350 if (REG_P (src))
2352 rtx rsi = reg_saved_in (src);
2353 if (rsi)
2354 src = rsi;
2357 fde = current_fde ();
2359 switch (GET_CODE (dest))
2361 case REG:
2362 switch (GET_CODE (src))
2364 /* Setting FP from SP. */
2365 case REG:
2366 if (cfa.reg == (unsigned) REGNO (src))
2368 /* Rule 1 */
2369 /* Update the CFA rule wrt SP or FP. Make sure src is
2370 relative to the current CFA register.
2372 We used to require that dest be either SP or FP, but the
2373 ARM copies SP to a temporary register, and from there to
2374 FP. So we just rely on the backends to only set
2375 RTX_FRAME_RELATED_P on appropriate insns. */
2376 cfa.reg = REGNO (dest);
2377 cfa_temp.reg = cfa.reg;
2378 cfa_temp.offset = cfa.offset;
2380 else
2382 /* Saving a register in a register. */
2383 gcc_assert (!fixed_regs [REGNO (dest)]
2384 /* For the SPARC and its register window. */
2385 || (DWARF_FRAME_REGNUM (REGNO (src))
2386 == DWARF_FRAME_RETURN_COLUMN));
2388 /* After stack is aligned, we can only save SP in FP
2389 if drap register is used. In this case, we have
2390 to restore stack pointer with the CFA value and we
2391 don't generate this DWARF information. */
2392 if (fde
2393 && fde->stack_realign
2394 && REGNO (src) == STACK_POINTER_REGNUM)
2395 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2396 && fde->drap_reg != INVALID_REGNUM
2397 && cfa.reg != REGNO (src));
2398 else
2399 queue_reg_save (label, src, dest, 0);
2401 break;
2403 case PLUS:
2404 case MINUS:
2405 case LO_SUM:
2406 if (dest == stack_pointer_rtx)
2408 /* Rule 2 */
2409 /* Adjusting SP. */
2410 switch (GET_CODE (XEXP (src, 1)))
2412 case CONST_INT:
2413 offset = INTVAL (XEXP (src, 1));
2414 break;
2415 case REG:
2416 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2417 == cfa_temp.reg);
2418 offset = cfa_temp.offset;
2419 break;
2420 default:
2421 gcc_unreachable ();
2424 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2426 /* Restoring SP from FP in the epilogue. */
2427 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2428 cfa.reg = STACK_POINTER_REGNUM;
2430 else if (GET_CODE (src) == LO_SUM)
2431 /* Assume we've set the source reg of the LO_SUM from sp. */
2433 else
2434 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2436 if (GET_CODE (src) != MINUS)
2437 offset = -offset;
2438 if (cfa.reg == STACK_POINTER_REGNUM)
2439 cfa.offset += offset;
2440 if (cfa_store.reg == STACK_POINTER_REGNUM)
2441 cfa_store.offset += offset;
2443 else if (dest == hard_frame_pointer_rtx)
2445 /* Rule 3 */
2446 /* Either setting the FP from an offset of the SP,
2447 or adjusting the FP */
2448 gcc_assert (frame_pointer_needed);
2450 gcc_assert (REG_P (XEXP (src, 0))
2451 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2452 && CONST_INT_P (XEXP (src, 1)));
2453 offset = INTVAL (XEXP (src, 1));
2454 if (GET_CODE (src) != MINUS)
2455 offset = -offset;
2456 cfa.offset += offset;
2457 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2459 else
2461 gcc_assert (GET_CODE (src) != MINUS);
2463 /* Rule 4 */
2464 if (REG_P (XEXP (src, 0))
2465 && REGNO (XEXP (src, 0)) == cfa.reg
2466 && CONST_INT_P (XEXP (src, 1)))
2468 /* Setting a temporary CFA register that will be copied
2469 into the FP later on. */
2470 offset = - INTVAL (XEXP (src, 1));
2471 cfa.offset += offset;
2472 cfa.reg = REGNO (dest);
2473 /* Or used to save regs to the stack. */
2474 cfa_temp.reg = cfa.reg;
2475 cfa_temp.offset = cfa.offset;
2478 /* Rule 5 */
2479 else if (REG_P (XEXP (src, 0))
2480 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2481 && XEXP (src, 1) == stack_pointer_rtx)
2483 /* Setting a scratch register that we will use instead
2484 of SP for saving registers to the stack. */
2485 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2486 cfa_store.reg = REGNO (dest);
2487 cfa_store.offset = cfa.offset - cfa_temp.offset;
2490 /* Rule 9 */
2491 else if (GET_CODE (src) == LO_SUM
2492 && CONST_INT_P (XEXP (src, 1)))
2494 cfa_temp.reg = REGNO (dest);
2495 cfa_temp.offset = INTVAL (XEXP (src, 1));
2497 else
2498 gcc_unreachable ();
2500 break;
2502 /* Rule 6 */
2503 case CONST_INT:
2504 cfa_temp.reg = REGNO (dest);
2505 cfa_temp.offset = INTVAL (src);
2506 break;
2508 /* Rule 7 */
2509 case IOR:
2510 gcc_assert (REG_P (XEXP (src, 0))
2511 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2512 && CONST_INT_P (XEXP (src, 1)));
2514 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2515 cfa_temp.reg = REGNO (dest);
2516 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2517 break;
2519 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2520 which will fill in all of the bits. */
2521 /* Rule 8 */
2522 case HIGH:
2523 break;
2525 /* Rule 15 */
2526 case UNSPEC:
2527 case UNSPEC_VOLATILE:
2528 gcc_assert (targetm.dwarf_handle_frame_unspec);
2529 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2530 return;
2532 /* Rule 16 */
2533 case AND:
2534 /* If this AND operation happens on stack pointer in prologue,
2535 we assume the stack is realigned and we extract the
2536 alignment. */
2537 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2539 /* We interpret reg_save differently with stack_realign set.
2540 Thus we must flush whatever we have queued first. */
2541 dwarf2out_flush_queued_reg_saves ();
2543 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2544 fde->stack_realign = 1;
2545 fde->stack_realignment = INTVAL (XEXP (src, 1));
2546 cfa_store.offset = 0;
2548 if (cfa.reg != STACK_POINTER_REGNUM
2549 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2550 fde->drap_reg = cfa.reg;
2552 return;
2554 default:
2555 gcc_unreachable ();
2558 def_cfa_1 (label, &cfa);
2559 break;
2561 case MEM:
2563 /* Saving a register to the stack. Make sure dest is relative to the
2564 CFA register. */
2565 switch (GET_CODE (XEXP (dest, 0)))
2567 /* Rule 10 */
2568 /* With a push. */
2569 case PRE_MODIFY:
2570 /* We can't handle variable size modifications. */
2571 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2572 == CONST_INT);
2573 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2575 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2576 && cfa_store.reg == STACK_POINTER_REGNUM);
2578 cfa_store.offset += offset;
2579 if (cfa.reg == STACK_POINTER_REGNUM)
2580 cfa.offset = cfa_store.offset;
2582 offset = -cfa_store.offset;
2583 break;
2585 /* Rule 11 */
2586 case PRE_INC:
2587 case PRE_DEC:
2588 case POST_DEC:
2589 offset = GET_MODE_SIZE (GET_MODE (dest));
2590 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2591 offset = -offset;
2593 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2594 == STACK_POINTER_REGNUM)
2595 && cfa_store.reg == STACK_POINTER_REGNUM);
2597 cfa_store.offset += offset;
2599 /* Rule 18: If stack is aligned, we will use FP as a
2600 reference to represent the address of the stored
2601 regiser. */
2602 if (fde
2603 && fde->stack_realign
2604 && src == hard_frame_pointer_rtx)
2606 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2607 cfa_store.offset = 0;
2610 if (cfa.reg == STACK_POINTER_REGNUM)
2611 cfa.offset = cfa_store.offset;
2613 if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2614 offset += -cfa_store.offset;
2615 else
2616 offset = -cfa_store.offset;
2617 break;
2619 /* Rule 12 */
2620 /* With an offset. */
2621 case PLUS:
2622 case MINUS:
2623 case LO_SUM:
2625 int regno;
2627 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2628 && REG_P (XEXP (XEXP (dest, 0), 0)));
2629 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2630 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2631 offset = -offset;
2633 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2635 if (cfa.reg == (unsigned) regno)
2636 offset -= cfa.offset;
2637 else if (cfa_store.reg == (unsigned) regno)
2638 offset -= cfa_store.offset;
2639 else
2641 gcc_assert (cfa_temp.reg == (unsigned) regno);
2642 offset -= cfa_temp.offset;
2645 break;
2647 /* Rule 13 */
2648 /* Without an offset. */
2649 case REG:
2651 int regno = REGNO (XEXP (dest, 0));
2653 if (cfa.reg == (unsigned) regno)
2654 offset = -cfa.offset;
2655 else if (cfa_store.reg == (unsigned) regno)
2656 offset = -cfa_store.offset;
2657 else
2659 gcc_assert (cfa_temp.reg == (unsigned) regno);
2660 offset = -cfa_temp.offset;
2663 break;
2665 /* Rule 14 */
2666 case POST_INC:
2667 gcc_assert (cfa_temp.reg
2668 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2669 offset = -cfa_temp.offset;
2670 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2671 break;
2673 default:
2674 gcc_unreachable ();
2677 /* Rule 17 */
2678 /* If the source operand of this MEM operation is not a
2679 register, basically the source is return address. Here
2680 we only care how much stack grew and we don't save it. */
2681 if (!REG_P (src))
2682 break;
2684 if (REGNO (src) != STACK_POINTER_REGNUM
2685 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2686 && (unsigned) REGNO (src) == cfa.reg)
2688 /* We're storing the current CFA reg into the stack. */
2690 if (cfa.offset == 0)
2692 /* Rule 19 */
2693 /* If stack is aligned, putting CFA reg into stack means
2694 we can no longer use reg + offset to represent CFA.
2695 Here we use DW_CFA_def_cfa_expression instead. The
2696 result of this expression equals to the original CFA
2697 value. */
2698 if (fde
2699 && fde->stack_realign
2700 && cfa.indirect == 0
2701 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2703 dw_cfa_location cfa_exp;
2705 gcc_assert (fde->drap_reg == cfa.reg);
2707 cfa_exp.indirect = 1;
2708 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2709 cfa_exp.base_offset = offset;
2710 cfa_exp.offset = 0;
2712 fde->drap_reg_saved = 1;
2714 def_cfa_1 (label, &cfa_exp);
2715 break;
2718 /* If the source register is exactly the CFA, assume
2719 we're saving SP like any other register; this happens
2720 on the ARM. */
2721 def_cfa_1 (label, &cfa);
2722 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2723 break;
2725 else
2727 /* Otherwise, we'll need to look in the stack to
2728 calculate the CFA. */
2729 rtx x = XEXP (dest, 0);
2731 if (!REG_P (x))
2732 x = XEXP (x, 0);
2733 gcc_assert (REG_P (x));
2735 cfa.reg = REGNO (x);
2736 cfa.base_offset = offset;
2737 cfa.indirect = 1;
2738 def_cfa_1 (label, &cfa);
2739 break;
2743 def_cfa_1 (label, &cfa);
2745 span = targetm.dwarf_register_span (src);
2747 if (!span)
2748 queue_reg_save (label, src, NULL_RTX, offset);
2749 else
2751 /* We have a PARALLEL describing where the contents of SRC
2752 live. Queue register saves for each piece of the
2753 PARALLEL. */
2754 int par_index;
2755 int limit;
2756 HOST_WIDE_INT span_offset = offset;
2758 gcc_assert (GET_CODE (span) == PARALLEL);
2760 limit = XVECLEN (span, 0);
2761 for (par_index = 0; par_index < limit; par_index++)
2763 rtx elem = XVECEXP (span, 0, par_index);
2765 queue_reg_save (label, elem, NULL_RTX, span_offset);
2766 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2770 break;
2772 default:
2773 gcc_unreachable ();
2777 /* Record call frame debugging information for INSN, which either
2778 sets SP or FP (adjusting how we calculate the frame address) or saves a
2779 register to the stack. If INSN is NULL_RTX, initialize our state.
2781 If AFTER_P is false, we're being called before the insn is emitted,
2782 otherwise after. Call instructions get invoked twice. */
2784 void
2785 dwarf2out_frame_debug (rtx insn, bool after_p)
2787 const char *label;
2788 rtx note, n;
2789 bool handled_one = false;
2791 if (insn == NULL_RTX)
2793 size_t i;
2795 /* Flush any queued register saves. */
2796 dwarf2out_flush_queued_reg_saves ();
2798 /* Set up state for generating call frame debug info. */
2799 lookup_cfa (&cfa);
2800 gcc_assert (cfa.reg
2801 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2803 cfa.reg = STACK_POINTER_REGNUM;
2804 cfa_store = cfa;
2805 cfa_temp.reg = -1;
2806 cfa_temp.offset = 0;
2808 for (i = 0; i < num_regs_saved_in_regs; i++)
2810 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2811 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2813 num_regs_saved_in_regs = 0;
2815 if (barrier_args_size)
2817 XDELETEVEC (barrier_args_size);
2818 barrier_args_size = NULL;
2820 return;
2823 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2824 dwarf2out_flush_queued_reg_saves ();
2826 if (!RTX_FRAME_RELATED_P (insn))
2828 /* ??? This should be done unconditionally since stack adjustments
2829 matter if the stack pointer is not the CFA register anymore but
2830 is still used to save registers. */
2831 if (!ACCUMULATE_OUTGOING_ARGS)
2832 dwarf2out_notice_stack_adjust (insn, after_p);
2833 return;
2836 label = dwarf2out_cfi_label (false);
2837 any_cfis_emitted = false;
2839 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2840 switch (REG_NOTE_KIND (note))
2842 case REG_FRAME_RELATED_EXPR:
2843 insn = XEXP (note, 0);
2844 goto found;
2846 case REG_CFA_DEF_CFA:
2847 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2848 handled_one = true;
2849 break;
2851 case REG_CFA_ADJUST_CFA:
2852 n = XEXP (note, 0);
2853 if (n == NULL)
2855 n = PATTERN (insn);
2856 if (GET_CODE (n) == PARALLEL)
2857 n = XVECEXP (n, 0, 0);
2859 dwarf2out_frame_debug_adjust_cfa (n, label);
2860 handled_one = true;
2861 break;
2863 case REG_CFA_OFFSET:
2864 n = XEXP (note, 0);
2865 if (n == NULL)
2866 n = single_set (insn);
2867 dwarf2out_frame_debug_cfa_offset (n, label);
2868 handled_one = true;
2869 break;
2871 case REG_CFA_REGISTER:
2872 n = XEXP (note, 0);
2873 if (n == NULL)
2875 n = PATTERN (insn);
2876 if (GET_CODE (n) == PARALLEL)
2877 n = XVECEXP (n, 0, 0);
2879 dwarf2out_frame_debug_cfa_register (n, label);
2880 handled_one = true;
2881 break;
2883 case REG_CFA_EXPRESSION:
2884 n = XEXP (note, 0);
2885 if (n == NULL)
2886 n = single_set (insn);
2887 dwarf2out_frame_debug_cfa_expression (n, label);
2888 handled_one = true;
2889 break;
2891 case REG_CFA_RESTORE:
2892 n = XEXP (note, 0);
2893 if (n == NULL)
2895 n = PATTERN (insn);
2896 if (GET_CODE (n) == PARALLEL)
2897 n = XVECEXP (n, 0, 0);
2898 n = XEXP (n, 0);
2900 dwarf2out_frame_debug_cfa_restore (n, label);
2901 handled_one = true;
2902 break;
2904 case REG_CFA_SET_VDRAP:
2905 n = XEXP (note, 0);
2906 if (REG_P (n))
2908 dw_fde_ref fde = current_fde ();
2909 if (fde)
2911 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2912 if (REG_P (n))
2913 fde->vdrap_reg = REGNO (n);
2916 handled_one = true;
2917 break;
2919 default:
2920 break;
2922 if (handled_one)
2924 if (any_cfis_emitted)
2925 dwarf2out_flush_queued_reg_saves ();
2926 return;
2929 insn = PATTERN (insn);
2930 found:
2931 dwarf2out_frame_debug_expr (insn, label);
2933 /* Check again. A parallel can save and update the same register.
2934 We could probably check just once, here, but this is safer than
2935 removing the check above. */
2936 if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2937 dwarf2out_flush_queued_reg_saves ();
2940 /* Determine if we need to save and restore CFI information around this
2941 epilogue. If SIBCALL is true, then this is a sibcall epilogue. If
2942 we do need to save/restore, then emit the save now, and insert a
2943 NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream. */
2945 void
2946 dwarf2out_cfi_begin_epilogue (rtx insn)
2948 bool saw_frp = false;
2949 rtx i;
2951 /* Scan forward to the return insn, noticing if there are possible
2952 frame related insns. */
2953 for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2955 if (!INSN_P (i))
2956 continue;
2958 /* Look for both regular and sibcalls to end the block. */
2959 if (returnjump_p (i))
2960 break;
2961 if (CALL_P (i) && SIBLING_CALL_P (i))
2962 break;
2964 if (GET_CODE (PATTERN (i)) == SEQUENCE)
2966 int idx;
2967 rtx seq = PATTERN (i);
2969 if (returnjump_p (XVECEXP (seq, 0, 0)))
2970 break;
2971 if (CALL_P (XVECEXP (seq, 0, 0))
2972 && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2973 break;
2975 for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2976 if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2977 saw_frp = true;
2980 if (RTX_FRAME_RELATED_P (i))
2981 saw_frp = true;
2984 /* If the port doesn't emit epilogue unwind info, we don't need a
2985 save/restore pair. */
2986 if (!saw_frp)
2987 return;
2989 /* Otherwise, search forward to see if the return insn was the last
2990 basic block of the function. If so, we don't need save/restore. */
2991 gcc_assert (i != NULL);
2992 i = next_real_insn (i);
2993 if (i == NULL)
2994 return;
2996 /* Insert the restore before that next real insn in the stream, and before
2997 a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2998 properly nested. This should be after any label or alignment. This
2999 will be pushed into the CFI stream by the function below. */
3000 while (1)
3002 rtx p = PREV_INSN (i);
3003 if (!NOTE_P (p))
3004 break;
3005 if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3006 break;
3007 i = p;
3009 emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3011 emit_cfa_remember = true;
3013 /* And emulate the state save. */
3014 gcc_assert (!cfa_remember.in_use);
3015 cfa_remember = cfa;
3016 cfa_remember.in_use = 1;
3019 /* A "subroutine" of dwarf2out_cfi_begin_epilogue. Emit the restore
3020 required. */
3022 void
3023 dwarf2out_frame_debug_restore_state (void)
3025 dw_cfi_ref cfi = new_cfi ();
3026 const char *label = dwarf2out_cfi_label (false);
3028 cfi->dw_cfi_opc = DW_CFA_restore_state;
3029 add_fde_cfi (label, cfi);
3031 gcc_assert (cfa_remember.in_use);
3032 cfa = cfa_remember;
3033 cfa_remember.in_use = 0;
3036 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
3037 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3038 (enum dwarf_call_frame_info cfi);
3040 static enum dw_cfi_oprnd_type
3041 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3043 switch (cfi)
3045 case DW_CFA_nop:
3046 case DW_CFA_GNU_window_save:
3047 case DW_CFA_remember_state:
3048 case DW_CFA_restore_state:
3049 return dw_cfi_oprnd_unused;
3051 case DW_CFA_set_loc:
3052 case DW_CFA_advance_loc1:
3053 case DW_CFA_advance_loc2:
3054 case DW_CFA_advance_loc4:
3055 case DW_CFA_MIPS_advance_loc8:
3056 return dw_cfi_oprnd_addr;
3058 case DW_CFA_offset:
3059 case DW_CFA_offset_extended:
3060 case DW_CFA_def_cfa:
3061 case DW_CFA_offset_extended_sf:
3062 case DW_CFA_def_cfa_sf:
3063 case DW_CFA_restore:
3064 case DW_CFA_restore_extended:
3065 case DW_CFA_undefined:
3066 case DW_CFA_same_value:
3067 case DW_CFA_def_cfa_register:
3068 case DW_CFA_register:
3069 case DW_CFA_expression:
3070 return dw_cfi_oprnd_reg_num;
3072 case DW_CFA_def_cfa_offset:
3073 case DW_CFA_GNU_args_size:
3074 case DW_CFA_def_cfa_offset_sf:
3075 return dw_cfi_oprnd_offset;
3077 case DW_CFA_def_cfa_expression:
3078 return dw_cfi_oprnd_loc;
3080 default:
3081 gcc_unreachable ();
3085 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
3086 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3087 (enum dwarf_call_frame_info cfi);
3089 static enum dw_cfi_oprnd_type
3090 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3092 switch (cfi)
3094 case DW_CFA_def_cfa:
3095 case DW_CFA_def_cfa_sf:
3096 case DW_CFA_offset:
3097 case DW_CFA_offset_extended_sf:
3098 case DW_CFA_offset_extended:
3099 return dw_cfi_oprnd_offset;
3101 case DW_CFA_register:
3102 return dw_cfi_oprnd_reg_num;
3104 case DW_CFA_expression:
3105 return dw_cfi_oprnd_loc;
3107 default:
3108 return dw_cfi_oprnd_unused;
3112 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
3113 switch to the data section instead, and write out a synthetic start label
3114 for collect2 the first time around. */
3116 static void
3117 switch_to_eh_frame_section (bool back)
3119 tree label;
3121 #ifdef EH_FRAME_SECTION_NAME
3122 if (eh_frame_section == 0)
3124 int flags;
3126 if (EH_TABLES_CAN_BE_READ_ONLY)
3128 int fde_encoding;
3129 int per_encoding;
3130 int lsda_encoding;
3132 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3133 /*global=*/0);
3134 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3135 /*global=*/1);
3136 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3137 /*global=*/0);
3138 flags = ((! flag_pic
3139 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3140 && (fde_encoding & 0x70) != DW_EH_PE_aligned
3141 && (per_encoding & 0x70) != DW_EH_PE_absptr
3142 && (per_encoding & 0x70) != DW_EH_PE_aligned
3143 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3144 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3145 ? 0 : SECTION_WRITE);
3147 else
3148 flags = SECTION_WRITE;
3149 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3151 #endif /* EH_FRAME_SECTION_NAME */
3153 if (eh_frame_section)
3154 switch_to_section (eh_frame_section);
3155 else
3157 /* We have no special eh_frame section. Put the information in
3158 the data section and emit special labels to guide collect2. */
3159 switch_to_section (data_section);
3161 if (!back)
3163 label = get_file_function_name ("F");
3164 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3165 targetm.asm_out.globalize_label (asm_out_file,
3166 IDENTIFIER_POINTER (label));
3167 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3172 /* Switch [BACK] to the eh or debug frame table section, depending on
3173 FOR_EH. */
3175 static void
3176 switch_to_frame_table_section (int for_eh, bool back)
3178 if (for_eh)
3179 switch_to_eh_frame_section (back);
3180 else
3182 if (!debug_frame_section)
3183 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3184 SECTION_DEBUG, NULL);
3185 switch_to_section (debug_frame_section);
3189 /* Output a Call Frame Information opcode and its operand(s). */
3191 static void
3192 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3194 unsigned long r;
3195 HOST_WIDE_INT off;
3197 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3198 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3199 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3200 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3201 ((unsigned HOST_WIDE_INT)
3202 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3203 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3205 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3206 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3207 "DW_CFA_offset, column %#lx", r);
3208 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3209 dw2_asm_output_data_uleb128 (off, NULL);
3211 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3213 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3214 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3215 "DW_CFA_restore, column %#lx", r);
3217 else
3219 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3220 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3222 switch (cfi->dw_cfi_opc)
3224 case DW_CFA_set_loc:
3225 if (for_eh)
3226 dw2_asm_output_encoded_addr_rtx (
3227 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3228 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3229 false, NULL);
3230 else
3231 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3232 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3233 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3234 break;
3236 case DW_CFA_advance_loc1:
3237 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3238 fde->dw_fde_current_label, NULL);
3239 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3240 break;
3242 case DW_CFA_advance_loc2:
3243 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3244 fde->dw_fde_current_label, NULL);
3245 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3246 break;
3248 case DW_CFA_advance_loc4:
3249 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3250 fde->dw_fde_current_label, NULL);
3251 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3252 break;
3254 case DW_CFA_MIPS_advance_loc8:
3255 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3256 fde->dw_fde_current_label, NULL);
3257 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3258 break;
3260 case DW_CFA_offset_extended:
3261 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3262 dw2_asm_output_data_uleb128 (r, NULL);
3263 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3264 dw2_asm_output_data_uleb128 (off, NULL);
3265 break;
3267 case DW_CFA_def_cfa:
3268 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3269 dw2_asm_output_data_uleb128 (r, NULL);
3270 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3271 break;
3273 case DW_CFA_offset_extended_sf:
3274 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3275 dw2_asm_output_data_uleb128 (r, NULL);
3276 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3277 dw2_asm_output_data_sleb128 (off, NULL);
3278 break;
3280 case DW_CFA_def_cfa_sf:
3281 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3282 dw2_asm_output_data_uleb128 (r, NULL);
3283 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3284 dw2_asm_output_data_sleb128 (off, NULL);
3285 break;
3287 case DW_CFA_restore_extended:
3288 case DW_CFA_undefined:
3289 case DW_CFA_same_value:
3290 case DW_CFA_def_cfa_register:
3291 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3292 dw2_asm_output_data_uleb128 (r, NULL);
3293 break;
3295 case DW_CFA_register:
3296 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3297 dw2_asm_output_data_uleb128 (r, NULL);
3298 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3299 dw2_asm_output_data_uleb128 (r, NULL);
3300 break;
3302 case DW_CFA_def_cfa_offset:
3303 case DW_CFA_GNU_args_size:
3304 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3305 break;
3307 case DW_CFA_def_cfa_offset_sf:
3308 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3309 dw2_asm_output_data_sleb128 (off, NULL);
3310 break;
3312 case DW_CFA_GNU_window_save:
3313 break;
3315 case DW_CFA_def_cfa_expression:
3316 case DW_CFA_expression:
3317 output_cfa_loc (cfi, for_eh);
3318 break;
3320 case DW_CFA_GNU_negative_offset_extended:
3321 /* Obsoleted by DW_CFA_offset_extended_sf. */
3322 gcc_unreachable ();
3324 default:
3325 break;
3330 /* Similar, but do it via assembler directives instead. */
3332 static void
3333 output_cfi_directive (dw_cfi_ref cfi)
3335 unsigned long r, r2;
3337 switch (cfi->dw_cfi_opc)
3339 case DW_CFA_advance_loc:
3340 case DW_CFA_advance_loc1:
3341 case DW_CFA_advance_loc2:
3342 case DW_CFA_advance_loc4:
3343 case DW_CFA_MIPS_advance_loc8:
3344 case DW_CFA_set_loc:
3345 /* Should only be created by add_fde_cfi in a code path not
3346 followed when emitting via directives. The assembler is
3347 going to take care of this for us. */
3348 gcc_unreachable ();
3350 case DW_CFA_offset:
3351 case DW_CFA_offset_extended:
3352 case DW_CFA_offset_extended_sf:
3353 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3354 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3355 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3356 break;
3358 case DW_CFA_restore:
3359 case DW_CFA_restore_extended:
3360 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3361 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3362 break;
3364 case DW_CFA_undefined:
3365 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3366 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3367 break;
3369 case DW_CFA_same_value:
3370 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3371 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3372 break;
3374 case DW_CFA_def_cfa:
3375 case DW_CFA_def_cfa_sf:
3376 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3377 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3378 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3379 break;
3381 case DW_CFA_def_cfa_register:
3382 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3383 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3384 break;
3386 case DW_CFA_register:
3387 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3388 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3389 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3390 break;
3392 case DW_CFA_def_cfa_offset:
3393 case DW_CFA_def_cfa_offset_sf:
3394 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3395 HOST_WIDE_INT_PRINT_DEC"\n",
3396 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3397 break;
3399 case DW_CFA_remember_state:
3400 fprintf (asm_out_file, "\t.cfi_remember_state\n");
3401 break;
3402 case DW_CFA_restore_state:
3403 fprintf (asm_out_file, "\t.cfi_restore_state\n");
3404 break;
3406 case DW_CFA_GNU_args_size:
3407 fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3408 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3409 if (flag_debug_asm)
3410 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3411 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3412 fputc ('\n', asm_out_file);
3413 break;
3415 case DW_CFA_GNU_window_save:
3416 fprintf (asm_out_file, "\t.cfi_window_save\n");
3417 break;
3419 case DW_CFA_def_cfa_expression:
3420 case DW_CFA_expression:
3421 fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3422 output_cfa_loc_raw (cfi);
3423 fputc ('\n', asm_out_file);
3424 break;
3426 default:
3427 gcc_unreachable ();
3431 DEF_VEC_P (dw_cfi_ref);
3432 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3434 /* Output CFIs to bring current FDE to the same state as after executing
3435 CFIs in CFI chain. DO_CFI_ASM is true if .cfi_* directives shall
3436 be emitted, false otherwise. If it is false, FDE and FOR_EH are the
3437 other arguments to pass to output_cfi. */
3439 static void
3440 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3442 struct dw_cfi_struct cfi_buf;
3443 dw_cfi_ref cfi2;
3444 dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3445 VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3446 unsigned int len, idx;
3448 for (;; cfi = cfi->dw_cfi_next)
3449 switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3451 case DW_CFA_advance_loc:
3452 case DW_CFA_advance_loc1:
3453 case DW_CFA_advance_loc2:
3454 case DW_CFA_advance_loc4:
3455 case DW_CFA_MIPS_advance_loc8:
3456 case DW_CFA_set_loc:
3457 /* All advances should be ignored. */
3458 break;
3459 case DW_CFA_remember_state:
3461 dw_cfi_ref args_size = cfi_args_size;
3463 /* Skip everything between .cfi_remember_state and
3464 .cfi_restore_state. */
3465 for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3466 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3467 break;
3468 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3469 args_size = cfi2;
3470 else
3471 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3473 if (cfi2 == NULL)
3474 goto flush_all;
3475 else
3477 cfi = cfi2;
3478 cfi_args_size = args_size;
3480 break;
3482 case DW_CFA_GNU_args_size:
3483 cfi_args_size = cfi;
3484 break;
3485 case DW_CFA_GNU_window_save:
3486 goto flush_all;
3487 case DW_CFA_offset:
3488 case DW_CFA_offset_extended:
3489 case DW_CFA_offset_extended_sf:
3490 case DW_CFA_restore:
3491 case DW_CFA_restore_extended:
3492 case DW_CFA_undefined:
3493 case DW_CFA_same_value:
3494 case DW_CFA_register:
3495 case DW_CFA_val_offset:
3496 case DW_CFA_val_offset_sf:
3497 case DW_CFA_expression:
3498 case DW_CFA_val_expression:
3499 case DW_CFA_GNU_negative_offset_extended:
3500 if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3501 VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3502 cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3503 VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3504 break;
3505 case DW_CFA_def_cfa:
3506 case DW_CFA_def_cfa_sf:
3507 case DW_CFA_def_cfa_expression:
3508 cfi_cfa = cfi;
3509 cfi_cfa_offset = cfi;
3510 break;
3511 case DW_CFA_def_cfa_register:
3512 cfi_cfa = cfi;
3513 break;
3514 case DW_CFA_def_cfa_offset:
3515 case DW_CFA_def_cfa_offset_sf:
3516 cfi_cfa_offset = cfi;
3517 break;
3518 case DW_CFA_nop:
3519 gcc_assert (cfi == NULL);
3520 flush_all:
3521 len = VEC_length (dw_cfi_ref, regs);
3522 for (idx = 0; idx < len; idx++)
3524 cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3525 if (cfi2 != NULL
3526 && cfi2->dw_cfi_opc != DW_CFA_restore
3527 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3529 if (do_cfi_asm)
3530 output_cfi_directive (cfi2);
3531 else
3532 output_cfi (cfi2, fde, for_eh);
3535 if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3537 gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3538 cfi_buf = *cfi_cfa;
3539 switch (cfi_cfa_offset->dw_cfi_opc)
3541 case DW_CFA_def_cfa_offset:
3542 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3543 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3544 break;
3545 case DW_CFA_def_cfa_offset_sf:
3546 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3547 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3548 break;
3549 case DW_CFA_def_cfa:
3550 case DW_CFA_def_cfa_sf:
3551 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3552 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3553 break;
3554 default:
3555 gcc_unreachable ();
3557 cfi_cfa = &cfi_buf;
3559 else if (cfi_cfa_offset)
3560 cfi_cfa = cfi_cfa_offset;
3561 if (cfi_cfa)
3563 if (do_cfi_asm)
3564 output_cfi_directive (cfi_cfa);
3565 else
3566 output_cfi (cfi_cfa, fde, for_eh);
3568 cfi_cfa = NULL;
3569 cfi_cfa_offset = NULL;
3570 if (cfi_args_size
3571 && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3573 if (do_cfi_asm)
3574 output_cfi_directive (cfi_args_size);
3575 else
3576 output_cfi (cfi_args_size, fde, for_eh);
3578 cfi_args_size = NULL;
3579 if (cfi == NULL)
3581 VEC_free (dw_cfi_ref, heap, regs);
3582 return;
3584 else if (do_cfi_asm)
3585 output_cfi_directive (cfi);
3586 else
3587 output_cfi (cfi, fde, for_eh);
3588 break;
3589 default:
3590 gcc_unreachable ();
3594 /* Output one FDE. */
3596 static void
3597 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3598 char *section_start_label, int fde_encoding, char *augmentation,
3599 bool any_lsda_needed, int lsda_encoding)
3601 const char *begin, *end;
3602 static unsigned int j;
3603 char l1[20], l2[20];
3604 dw_cfi_ref cfi;
3606 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3607 /* empty */ 0);
3608 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3609 for_eh + j);
3610 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3611 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3612 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3613 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3614 " indicating 64-bit DWARF extension");
3615 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3616 "FDE Length");
3617 ASM_OUTPUT_LABEL (asm_out_file, l1);
3619 if (for_eh)
3620 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3621 else
3622 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3623 debug_frame_section, "FDE CIE offset");
3625 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3626 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3628 if (for_eh)
3630 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3631 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3632 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3633 "FDE initial location");
3634 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3635 end, begin, "FDE address range");
3637 else
3639 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3640 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3643 if (augmentation[0])
3645 if (any_lsda_needed)
3647 int size = size_of_encoded_value (lsda_encoding);
3649 if (lsda_encoding == DW_EH_PE_aligned)
3651 int offset = ( 4 /* Length */
3652 + 4 /* CIE offset */
3653 + 2 * size_of_encoded_value (fde_encoding)
3654 + 1 /* Augmentation size */ );
3655 int pad = -offset & (PTR_SIZE - 1);
3657 size += pad;
3658 gcc_assert (size_of_uleb128 (size) == 1);
3661 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3663 if (fde->uses_eh_lsda)
3665 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3666 fde->funcdef_number);
3667 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3668 gen_rtx_SYMBOL_REF (Pmode, l1),
3669 false,
3670 "Language Specific Data Area");
3672 else
3674 if (lsda_encoding == DW_EH_PE_aligned)
3675 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3676 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3677 "Language Specific Data Area (none)");
3680 else
3681 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3684 /* Loop through the Call Frame Instructions associated with
3685 this FDE. */
3686 fde->dw_fde_current_label = begin;
3687 if (fde->dw_fde_second_begin == NULL)
3688 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3689 output_cfi (cfi, fde, for_eh);
3690 else if (!second)
3692 if (fde->dw_fde_switch_cfi)
3693 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3695 output_cfi (cfi, fde, for_eh);
3696 if (cfi == fde->dw_fde_switch_cfi)
3697 break;
3700 else
3702 dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3704 if (fde->dw_fde_switch_cfi)
3706 cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3707 fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3708 output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3709 fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3711 for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3712 output_cfi (cfi, fde, for_eh);
3715 /* If we are to emit a ref/link from function bodies to their frame tables,
3716 do it now. This is typically performed to make sure that tables
3717 associated with functions are dragged with them and not discarded in
3718 garbage collecting links. We need to do this on a per function basis to
3719 cope with -ffunction-sections. */
3721 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3722 /* Switch to the function section, emit the ref to the tables, and
3723 switch *back* into the table section. */
3724 switch_to_section (function_section (fde->decl));
3725 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3726 switch_to_frame_table_section (for_eh, true);
3727 #endif
3729 /* Pad the FDE out to an address sized boundary. */
3730 ASM_OUTPUT_ALIGN (asm_out_file,
3731 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3732 ASM_OUTPUT_LABEL (asm_out_file, l2);
3734 j += 2;
3737 /* Return true if frame description entry FDE is needed for EH. */
3739 static bool
3740 fde_needed_for_eh_p (dw_fde_ref fde)
3742 if (flag_asynchronous_unwind_tables)
3743 return true;
3745 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3746 return true;
3748 if (fde->uses_eh_lsda)
3749 return true;
3751 /* If exceptions are enabled, we have collected nothrow info. */
3752 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3753 return false;
3755 return true;
3758 /* Output the call frame information used to record information
3759 that relates to calculating the frame pointer, and records the
3760 location of saved registers. */
3762 static void
3763 output_call_frame_info (int for_eh)
3765 unsigned int i;
3766 dw_fde_ref fde;
3767 dw_cfi_ref cfi;
3768 char l1[20], l2[20], section_start_label[20];
3769 bool any_lsda_needed = false;
3770 char augmentation[6];
3771 int augmentation_size;
3772 int fde_encoding = DW_EH_PE_absptr;
3773 int per_encoding = DW_EH_PE_absptr;
3774 int lsda_encoding = DW_EH_PE_absptr;
3775 int return_reg;
3776 rtx personality = NULL;
3777 int dw_cie_version;
3779 /* Don't emit a CIE if there won't be any FDEs. */
3780 if (fde_table_in_use == 0)
3781 return;
3783 /* Nothing to do if the assembler's doing it all. */
3784 if (dwarf2out_do_cfi_asm ())
3785 return;
3787 /* If we don't have any functions we'll want to unwind out of, don't emit
3788 any EH unwind information. If we make FDEs linkonce, we may have to
3789 emit an empty label for an FDE that wouldn't otherwise be emitted. We
3790 want to avoid having an FDE kept around when the function it refers to
3791 is discarded. Example where this matters: a primary function template
3792 in C++ requires EH information, an explicit specialization doesn't. */
3793 if (for_eh)
3795 bool any_eh_needed = false;
3797 for (i = 0; i < fde_table_in_use; i++)
3798 if (fde_table[i].uses_eh_lsda)
3799 any_eh_needed = any_lsda_needed = true;
3800 else if (fde_needed_for_eh_p (&fde_table[i]))
3801 any_eh_needed = true;
3802 else if (TARGET_USES_WEAK_UNWIND_INFO)
3803 targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3804 1, 1);
3806 if (!any_eh_needed)
3807 return;
3810 /* We're going to be generating comments, so turn on app. */
3811 if (flag_debug_asm)
3812 app_enable ();
3814 /* Switch to the proper frame section, first time. */
3815 switch_to_frame_table_section (for_eh, false);
3817 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3818 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3820 /* Output the CIE. */
3821 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3822 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3823 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3824 dw2_asm_output_data (4, 0xffffffff,
3825 "Initial length escape value indicating 64-bit DWARF extension");
3826 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3827 "Length of Common Information Entry");
3828 ASM_OUTPUT_LABEL (asm_out_file, l1);
3830 /* Now that the CIE pointer is PC-relative for EH,
3831 use 0 to identify the CIE. */
3832 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3833 (for_eh ? 0 : DWARF_CIE_ID),
3834 "CIE Identifier Tag");
3836 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3837 use CIE version 1, unless that would produce incorrect results
3838 due to overflowing the return register column. */
3839 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3840 dw_cie_version = 1;
3841 if (return_reg >= 256 || dwarf_version > 2)
3842 dw_cie_version = 3;
3843 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3845 augmentation[0] = 0;
3846 augmentation_size = 0;
3848 personality = current_unit_personality;
3849 if (for_eh)
3851 char *p;
3853 /* Augmentation:
3854 z Indicates that a uleb128 is present to size the
3855 augmentation section.
3856 L Indicates the encoding (and thus presence) of
3857 an LSDA pointer in the FDE augmentation.
3858 R Indicates a non-default pointer encoding for
3859 FDE code pointers.
3860 P Indicates the presence of an encoding + language
3861 personality routine in the CIE augmentation. */
3863 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3864 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3865 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3867 p = augmentation + 1;
3868 if (personality)
3870 *p++ = 'P';
3871 augmentation_size += 1 + size_of_encoded_value (per_encoding);
3872 assemble_external_libcall (personality);
3874 if (any_lsda_needed)
3876 *p++ = 'L';
3877 augmentation_size += 1;
3879 if (fde_encoding != DW_EH_PE_absptr)
3881 *p++ = 'R';
3882 augmentation_size += 1;
3884 if (p > augmentation + 1)
3886 augmentation[0] = 'z';
3887 *p = '\0';
3890 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
3891 if (personality && per_encoding == DW_EH_PE_aligned)
3893 int offset = ( 4 /* Length */
3894 + 4 /* CIE Id */
3895 + 1 /* CIE version */
3896 + strlen (augmentation) + 1 /* Augmentation */
3897 + size_of_uleb128 (1) /* Code alignment */
3898 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3899 + 1 /* RA column */
3900 + 1 /* Augmentation size */
3901 + 1 /* Personality encoding */ );
3902 int pad = -offset & (PTR_SIZE - 1);
3904 augmentation_size += pad;
3906 /* Augmentations should be small, so there's scarce need to
3907 iterate for a solution. Die if we exceed one uleb128 byte. */
3908 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3912 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3913 if (dw_cie_version >= 4)
3915 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3916 dw2_asm_output_data (1, 0, "CIE Segment Size");
3918 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3919 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3920 "CIE Data Alignment Factor");
3922 if (dw_cie_version == 1)
3923 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3924 else
3925 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3927 if (augmentation[0])
3929 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3930 if (personality)
3932 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3933 eh_data_format_name (per_encoding));
3934 dw2_asm_output_encoded_addr_rtx (per_encoding,
3935 personality,
3936 true, NULL);
3939 if (any_lsda_needed)
3940 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3941 eh_data_format_name (lsda_encoding));
3943 if (fde_encoding != DW_EH_PE_absptr)
3944 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3945 eh_data_format_name (fde_encoding));
3948 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3949 output_cfi (cfi, NULL, for_eh);
3951 /* Pad the CIE out to an address sized boundary. */
3952 ASM_OUTPUT_ALIGN (asm_out_file,
3953 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3954 ASM_OUTPUT_LABEL (asm_out_file, l2);
3956 /* Loop through all of the FDE's. */
3957 for (i = 0; i < fde_table_in_use; i++)
3959 unsigned int k;
3960 fde = &fde_table[i];
3962 /* Don't emit EH unwind info for leaf functions that don't need it. */
3963 if (for_eh && !fde_needed_for_eh_p (fde))
3964 continue;
3966 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
3967 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3968 augmentation, any_lsda_needed, lsda_encoding);
3971 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3972 dw2_asm_output_data (4, 0, "End of Table");
3973 #ifdef MIPS_DEBUGGING_INFO
3974 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3975 get a value of 0. Putting .align 0 after the label fixes it. */
3976 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3977 #endif
3979 /* Turn off app to make assembly quicker. */
3980 if (flag_debug_asm)
3981 app_disable ();
3984 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
3986 static void
3987 dwarf2out_do_cfi_startproc (bool second)
3989 int enc;
3990 rtx ref;
3991 rtx personality = get_personality_function (current_function_decl);
3993 fprintf (asm_out_file, "\t.cfi_startproc\n");
3995 if (personality)
3997 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3998 ref = personality;
4000 /* ??? The GAS support isn't entirely consistent. We have to
4001 handle indirect support ourselves, but PC-relative is done
4002 in the assembler. Further, the assembler can't handle any
4003 of the weirder relocation types. */
4004 if (enc & DW_EH_PE_indirect)
4005 ref = dw2_force_const_mem (ref, true);
4007 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4008 output_addr_const (asm_out_file, ref);
4009 fputc ('\n', asm_out_file);
4012 if (crtl->uses_eh_lsda)
4014 char lab[20];
4016 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4017 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4018 current_function_funcdef_no);
4019 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4020 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4022 if (enc & DW_EH_PE_indirect)
4023 ref = dw2_force_const_mem (ref, true);
4025 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4026 output_addr_const (asm_out_file, ref);
4027 fputc ('\n', asm_out_file);
4031 /* Output a marker (i.e. a label) for the beginning of a function, before
4032 the prologue. */
4034 void
4035 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4036 const char *file ATTRIBUTE_UNUSED)
4038 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4039 char * dup_label;
4040 dw_fde_ref fde;
4041 section *fnsec;
4042 bool do_frame;
4044 current_function_func_begin_label = NULL;
4046 do_frame = dwarf2out_do_frame ();
4048 /* ??? current_function_func_begin_label is also used by except.c for
4049 call-site information. We must emit this label if it might be used. */
4050 if (!do_frame
4051 && (!flag_exceptions
4052 || targetm.except_unwind_info (&global_options) != UI_TARGET))
4053 return;
4055 fnsec = function_section (current_function_decl);
4056 switch_to_section (fnsec);
4057 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4058 current_function_funcdef_no);
4059 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4060 current_function_funcdef_no);
4061 dup_label = xstrdup (label);
4062 current_function_func_begin_label = dup_label;
4064 /* We can elide the fde allocation if we're not emitting debug info. */
4065 if (!do_frame)
4066 return;
4068 /* Expand the fde table if necessary. */
4069 if (fde_table_in_use == fde_table_allocated)
4071 fde_table_allocated += FDE_TABLE_INCREMENT;
4072 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4073 memset (fde_table + fde_table_in_use, 0,
4074 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4077 /* Record the FDE associated with this function. */
4078 current_funcdef_fde = fde_table_in_use;
4080 /* Add the new FDE at the end of the fde_table. */
4081 fde = &fde_table[fde_table_in_use++];
4082 fde->decl = current_function_decl;
4083 fde->dw_fde_begin = dup_label;
4084 fde->dw_fde_end = NULL;
4085 fde->dw_fde_current_label = dup_label;
4086 fde->dw_fde_second_begin = NULL;
4087 fde->dw_fde_second_end = NULL;
4088 fde->dw_fde_vms_end_prologue = NULL;
4089 fde->dw_fde_vms_begin_epilogue = NULL;
4090 fde->dw_fde_cfi = NULL;
4091 fde->dw_fde_switch_cfi = NULL;
4092 fde->funcdef_number = current_function_funcdef_no;
4093 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4094 fde->uses_eh_lsda = crtl->uses_eh_lsda;
4095 fde->nothrow = crtl->nothrow;
4096 fde->drap_reg = INVALID_REGNUM;
4097 fde->vdrap_reg = INVALID_REGNUM;
4098 fde->in_std_section = (fnsec == text_section
4099 || (cold_text_section && fnsec == cold_text_section));
4100 fde->second_in_std_section = 0;
4102 args_size = old_args_size = 0;
4104 /* We only want to output line number information for the genuine dwarf2
4105 prologue case, not the eh frame case. */
4106 #ifdef DWARF2_DEBUGGING_INFO
4107 if (file)
4108 dwarf2out_source_line (line, file, 0, true);
4109 #endif
4111 if (dwarf2out_do_cfi_asm ())
4112 dwarf2out_do_cfi_startproc (false);
4113 else
4115 rtx personality = get_personality_function (current_function_decl);
4116 if (!current_unit_personality)
4117 current_unit_personality = personality;
4119 /* We cannot keep a current personality per function as without CFI
4120 asm, at the point where we emit the CFI data, there is no current
4121 function anymore. */
4122 if (personality && current_unit_personality != personality)
4123 sorry ("multiple EH personalities are supported only with assemblers "
4124 "supporting .cfi_personality directive");
4128 /* Output a marker (i.e. a label) for the end of the generated code
4129 for a function prologue. This gets called *after* the prologue code has
4130 been generated. */
4132 void
4133 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4134 const char *file ATTRIBUTE_UNUSED)
4136 dw_fde_ref fde;
4137 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4139 /* Output a label to mark the endpoint of the code generated for this
4140 function. */
4141 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4142 current_function_funcdef_no);
4143 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4144 current_function_funcdef_no);
4145 fde = &fde_table[fde_table_in_use - 1];
4146 fde->dw_fde_vms_end_prologue = xstrdup (label);
4149 /* Output a marker (i.e. a label) for the beginning of the generated code
4150 for a function epilogue. This gets called *before* the prologue code has
4151 been generated. */
4153 void
4154 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4155 const char *file ATTRIBUTE_UNUSED)
4157 dw_fde_ref fde;
4158 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4160 fde = &fde_table[fde_table_in_use - 1];
4161 if (fde->dw_fde_vms_begin_epilogue)
4162 return;
4164 /* Output a label to mark the endpoint of the code generated for this
4165 function. */
4166 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4167 current_function_funcdef_no);
4168 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4169 current_function_funcdef_no);
4170 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4173 /* Output a marker (i.e. a label) for the absolute end of the generated code
4174 for a function definition. This gets called *after* the epilogue code has
4175 been generated. */
4177 void
4178 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4179 const char *file ATTRIBUTE_UNUSED)
4181 dw_fde_ref fde;
4182 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4184 last_var_location_insn = NULL_RTX;
4186 if (dwarf2out_do_cfi_asm ())
4187 fprintf (asm_out_file, "\t.cfi_endproc\n");
4189 /* Output a label to mark the endpoint of the code generated for this
4190 function. */
4191 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4192 current_function_funcdef_no);
4193 ASM_OUTPUT_LABEL (asm_out_file, label);
4194 fde = current_fde ();
4195 gcc_assert (fde != NULL);
4196 if (fde->dw_fde_second_begin == NULL)
4197 fde->dw_fde_end = xstrdup (label);
4200 void
4201 dwarf2out_frame_init (void)
4203 /* Allocate the initial hunk of the fde_table. */
4204 fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4205 fde_table_allocated = FDE_TABLE_INCREMENT;
4206 fde_table_in_use = 0;
4208 /* Generate the CFA instructions common to all FDE's. Do it now for the
4209 sake of lookup_cfa. */
4211 /* On entry, the Canonical Frame Address is at SP. */
4212 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4214 if (targetm.debug_unwind_info () == UI_DWARF2
4215 || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4216 initial_return_save (INCOMING_RETURN_ADDR_RTX);
4219 void
4220 dwarf2out_frame_finish (void)
4222 /* Output call frame information. */
4223 if (targetm.debug_unwind_info () == UI_DWARF2)
4224 output_call_frame_info (0);
4226 /* Output another copy for the unwinder. */
4227 if ((flag_unwind_tables || flag_exceptions)
4228 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4229 output_call_frame_info (1);
4232 /* Note that the current function section is being used for code. */
4234 static void
4235 dwarf2out_note_section_used (void)
4237 section *sec = current_function_section ();
4238 if (sec == text_section)
4239 text_section_used = true;
4240 else if (sec == cold_text_section)
4241 cold_text_section_used = true;
4244 static void var_location_switch_text_section (void);
4246 void
4247 dwarf2out_switch_text_section (void)
4249 section *sect;
4250 dw_fde_ref fde = current_fde ();
4251 dw_cfi_ref cfi;
4253 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4255 if (!in_cold_section_p)
4257 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4258 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4259 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4261 else
4263 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4264 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4265 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4267 have_multiple_function_sections = true;
4269 /* Reset the current label on switching text sections, so that we
4270 don't attempt to advance_loc4 between labels in different sections. */
4271 fde->dw_fde_current_label = NULL;
4273 /* There is no need to mark used sections when not debugging. */
4274 if (cold_text_section != NULL)
4275 dwarf2out_note_section_used ();
4277 if (dwarf2out_do_cfi_asm ())
4278 fprintf (asm_out_file, "\t.cfi_endproc\n");
4280 /* Now do the real section switch. */
4281 sect = current_function_section ();
4282 switch_to_section (sect);
4284 fde->second_in_std_section
4285 = (sect == text_section
4286 || (cold_text_section && sect == cold_text_section));
4288 if (dwarf2out_do_cfi_asm ())
4290 dwarf2out_do_cfi_startproc (true);
4291 /* As this is a different FDE, insert all current CFI instructions
4292 again. */
4293 output_cfis (fde->dw_fde_cfi, true, fde, true);
4295 cfi = fde->dw_fde_cfi;
4296 if (cfi)
4297 while (cfi->dw_cfi_next != NULL)
4298 cfi = cfi->dw_cfi_next;
4299 fde->dw_fde_switch_cfi = cfi;
4300 var_location_switch_text_section ();
4303 /* And now, the subset of the debugging information support code necessary
4304 for emitting location expressions. */
4306 /* Data about a single source file. */
4307 struct GTY(()) dwarf_file_data {
4308 const char * filename;
4309 int emitted_number;
4312 typedef struct dw_val_struct *dw_val_ref;
4313 typedef struct die_struct *dw_die_ref;
4314 typedef const struct die_struct *const_dw_die_ref;
4315 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4316 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4318 typedef struct GTY(()) deferred_locations_struct
4320 tree variable;
4321 dw_die_ref die;
4322 } deferred_locations;
4324 DEF_VEC_O(deferred_locations);
4325 DEF_VEC_ALLOC_O(deferred_locations,gc);
4327 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4329 DEF_VEC_P(dw_die_ref);
4330 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4332 /* Each DIE may have a series of attribute/value pairs. Values
4333 can take on several forms. The forms that are used in this
4334 implementation are listed below. */
4336 enum dw_val_class
4338 dw_val_class_addr,
4339 dw_val_class_offset,
4340 dw_val_class_loc,
4341 dw_val_class_loc_list,
4342 dw_val_class_range_list,
4343 dw_val_class_const,
4344 dw_val_class_unsigned_const,
4345 dw_val_class_const_double,
4346 dw_val_class_vec,
4347 dw_val_class_flag,
4348 dw_val_class_die_ref,
4349 dw_val_class_fde_ref,
4350 dw_val_class_lbl_id,
4351 dw_val_class_lineptr,
4352 dw_val_class_str,
4353 dw_val_class_macptr,
4354 dw_val_class_file,
4355 dw_val_class_data8,
4356 dw_val_class_decl_ref,
4357 dw_val_class_vms_delta
4360 /* Describe a floating point constant value, or a vector constant value. */
4362 typedef struct GTY(()) dw_vec_struct {
4363 unsigned char * GTY((length ("%h.length"))) array;
4364 unsigned length;
4365 unsigned elt_size;
4367 dw_vec_const;
4369 /* The dw_val_node describes an attribute's value, as it is
4370 represented internally. */
4372 typedef struct GTY(()) dw_val_struct {
4373 enum dw_val_class val_class;
4374 union dw_val_struct_union
4376 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4377 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4378 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4379 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4380 HOST_WIDE_INT GTY ((default)) val_int;
4381 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4382 double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4383 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4384 struct dw_val_die_union
4386 dw_die_ref die;
4387 int external;
4388 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4389 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4390 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4391 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4392 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4393 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4394 unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4395 tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4396 struct dw_val_vms_delta_union
4398 char * lbl1;
4399 char * lbl2;
4400 } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4402 GTY ((desc ("%1.val_class"))) v;
4404 dw_val_node;
4406 /* Locations in memory are described using a sequence of stack machine
4407 operations. */
4409 typedef struct GTY(()) dw_loc_descr_struct {
4410 dw_loc_descr_ref dw_loc_next;
4411 ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4412 /* Used to distinguish DW_OP_addr with a direct symbol relocation
4413 from DW_OP_addr with a dtp-relative symbol relocation. */
4414 unsigned int dtprel : 1;
4415 int dw_loc_addr;
4416 dw_val_node dw_loc_oprnd1;
4417 dw_val_node dw_loc_oprnd2;
4419 dw_loc_descr_node;
4421 /* Location lists are ranges + location descriptions for that range,
4422 so you can track variables that are in different places over
4423 their entire life. */
4424 typedef struct GTY(()) dw_loc_list_struct {
4425 dw_loc_list_ref dw_loc_next;
4426 const char *begin; /* Label for begin address of range */
4427 const char *end; /* Label for end address of range */
4428 char *ll_symbol; /* Label for beginning of location list.
4429 Only on head of list */
4430 const char *section; /* Section this loclist is relative to */
4431 dw_loc_descr_ref expr;
4432 hashval_t hash;
4433 /* True if all addresses in this and subsequent lists are known to be
4434 resolved. */
4435 bool resolved_addr;
4436 /* True if this list has been replaced by dw_loc_next. */
4437 bool replaced;
4438 bool emitted;
4439 } dw_loc_list_node;
4441 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4443 /* Convert a DWARF stack opcode into its string name. */
4445 static const char *
4446 dwarf_stack_op_name (unsigned int op)
4448 switch (op)
4450 case DW_OP_addr:
4451 return "DW_OP_addr";
4452 case DW_OP_deref:
4453 return "DW_OP_deref";
4454 case DW_OP_const1u:
4455 return "DW_OP_const1u";
4456 case DW_OP_const1s:
4457 return "DW_OP_const1s";
4458 case DW_OP_const2u:
4459 return "DW_OP_const2u";
4460 case DW_OP_const2s:
4461 return "DW_OP_const2s";
4462 case DW_OP_const4u:
4463 return "DW_OP_const4u";
4464 case DW_OP_const4s:
4465 return "DW_OP_const4s";
4466 case DW_OP_const8u:
4467 return "DW_OP_const8u";
4468 case DW_OP_const8s:
4469 return "DW_OP_const8s";
4470 case DW_OP_constu:
4471 return "DW_OP_constu";
4472 case DW_OP_consts:
4473 return "DW_OP_consts";
4474 case DW_OP_dup:
4475 return "DW_OP_dup";
4476 case DW_OP_drop:
4477 return "DW_OP_drop";
4478 case DW_OP_over:
4479 return "DW_OP_over";
4480 case DW_OP_pick:
4481 return "DW_OP_pick";
4482 case DW_OP_swap:
4483 return "DW_OP_swap";
4484 case DW_OP_rot:
4485 return "DW_OP_rot";
4486 case DW_OP_xderef:
4487 return "DW_OP_xderef";
4488 case DW_OP_abs:
4489 return "DW_OP_abs";
4490 case DW_OP_and:
4491 return "DW_OP_and";
4492 case DW_OP_div:
4493 return "DW_OP_div";
4494 case DW_OP_minus:
4495 return "DW_OP_minus";
4496 case DW_OP_mod:
4497 return "DW_OP_mod";
4498 case DW_OP_mul:
4499 return "DW_OP_mul";
4500 case DW_OP_neg:
4501 return "DW_OP_neg";
4502 case DW_OP_not:
4503 return "DW_OP_not";
4504 case DW_OP_or:
4505 return "DW_OP_or";
4506 case DW_OP_plus:
4507 return "DW_OP_plus";
4508 case DW_OP_plus_uconst:
4509 return "DW_OP_plus_uconst";
4510 case DW_OP_shl:
4511 return "DW_OP_shl";
4512 case DW_OP_shr:
4513 return "DW_OP_shr";
4514 case DW_OP_shra:
4515 return "DW_OP_shra";
4516 case DW_OP_xor:
4517 return "DW_OP_xor";
4518 case DW_OP_bra:
4519 return "DW_OP_bra";
4520 case DW_OP_eq:
4521 return "DW_OP_eq";
4522 case DW_OP_ge:
4523 return "DW_OP_ge";
4524 case DW_OP_gt:
4525 return "DW_OP_gt";
4526 case DW_OP_le:
4527 return "DW_OP_le";
4528 case DW_OP_lt:
4529 return "DW_OP_lt";
4530 case DW_OP_ne:
4531 return "DW_OP_ne";
4532 case DW_OP_skip:
4533 return "DW_OP_skip";
4534 case DW_OP_lit0:
4535 return "DW_OP_lit0";
4536 case DW_OP_lit1:
4537 return "DW_OP_lit1";
4538 case DW_OP_lit2:
4539 return "DW_OP_lit2";
4540 case DW_OP_lit3:
4541 return "DW_OP_lit3";
4542 case DW_OP_lit4:
4543 return "DW_OP_lit4";
4544 case DW_OP_lit5:
4545 return "DW_OP_lit5";
4546 case DW_OP_lit6:
4547 return "DW_OP_lit6";
4548 case DW_OP_lit7:
4549 return "DW_OP_lit7";
4550 case DW_OP_lit8:
4551 return "DW_OP_lit8";
4552 case DW_OP_lit9:
4553 return "DW_OP_lit9";
4554 case DW_OP_lit10:
4555 return "DW_OP_lit10";
4556 case DW_OP_lit11:
4557 return "DW_OP_lit11";
4558 case DW_OP_lit12:
4559 return "DW_OP_lit12";
4560 case DW_OP_lit13:
4561 return "DW_OP_lit13";
4562 case DW_OP_lit14:
4563 return "DW_OP_lit14";
4564 case DW_OP_lit15:
4565 return "DW_OP_lit15";
4566 case DW_OP_lit16:
4567 return "DW_OP_lit16";
4568 case DW_OP_lit17:
4569 return "DW_OP_lit17";
4570 case DW_OP_lit18:
4571 return "DW_OP_lit18";
4572 case DW_OP_lit19:
4573 return "DW_OP_lit19";
4574 case DW_OP_lit20:
4575 return "DW_OP_lit20";
4576 case DW_OP_lit21:
4577 return "DW_OP_lit21";
4578 case DW_OP_lit22:
4579 return "DW_OP_lit22";
4580 case DW_OP_lit23:
4581 return "DW_OP_lit23";
4582 case DW_OP_lit24:
4583 return "DW_OP_lit24";
4584 case DW_OP_lit25:
4585 return "DW_OP_lit25";
4586 case DW_OP_lit26:
4587 return "DW_OP_lit26";
4588 case DW_OP_lit27:
4589 return "DW_OP_lit27";
4590 case DW_OP_lit28:
4591 return "DW_OP_lit28";
4592 case DW_OP_lit29:
4593 return "DW_OP_lit29";
4594 case DW_OP_lit30:
4595 return "DW_OP_lit30";
4596 case DW_OP_lit31:
4597 return "DW_OP_lit31";
4598 case DW_OP_reg0:
4599 return "DW_OP_reg0";
4600 case DW_OP_reg1:
4601 return "DW_OP_reg1";
4602 case DW_OP_reg2:
4603 return "DW_OP_reg2";
4604 case DW_OP_reg3:
4605 return "DW_OP_reg3";
4606 case DW_OP_reg4:
4607 return "DW_OP_reg4";
4608 case DW_OP_reg5:
4609 return "DW_OP_reg5";
4610 case DW_OP_reg6:
4611 return "DW_OP_reg6";
4612 case DW_OP_reg7:
4613 return "DW_OP_reg7";
4614 case DW_OP_reg8:
4615 return "DW_OP_reg8";
4616 case DW_OP_reg9:
4617 return "DW_OP_reg9";
4618 case DW_OP_reg10:
4619 return "DW_OP_reg10";
4620 case DW_OP_reg11:
4621 return "DW_OP_reg11";
4622 case DW_OP_reg12:
4623 return "DW_OP_reg12";
4624 case DW_OP_reg13:
4625 return "DW_OP_reg13";
4626 case DW_OP_reg14:
4627 return "DW_OP_reg14";
4628 case DW_OP_reg15:
4629 return "DW_OP_reg15";
4630 case DW_OP_reg16:
4631 return "DW_OP_reg16";
4632 case DW_OP_reg17:
4633 return "DW_OP_reg17";
4634 case DW_OP_reg18:
4635 return "DW_OP_reg18";
4636 case DW_OP_reg19:
4637 return "DW_OP_reg19";
4638 case DW_OP_reg20:
4639 return "DW_OP_reg20";
4640 case DW_OP_reg21:
4641 return "DW_OP_reg21";
4642 case DW_OP_reg22:
4643 return "DW_OP_reg22";
4644 case DW_OP_reg23:
4645 return "DW_OP_reg23";
4646 case DW_OP_reg24:
4647 return "DW_OP_reg24";
4648 case DW_OP_reg25:
4649 return "DW_OP_reg25";
4650 case DW_OP_reg26:
4651 return "DW_OP_reg26";
4652 case DW_OP_reg27:
4653 return "DW_OP_reg27";
4654 case DW_OP_reg28:
4655 return "DW_OP_reg28";
4656 case DW_OP_reg29:
4657 return "DW_OP_reg29";
4658 case DW_OP_reg30:
4659 return "DW_OP_reg30";
4660 case DW_OP_reg31:
4661 return "DW_OP_reg31";
4662 case DW_OP_breg0:
4663 return "DW_OP_breg0";
4664 case DW_OP_breg1:
4665 return "DW_OP_breg1";
4666 case DW_OP_breg2:
4667 return "DW_OP_breg2";
4668 case DW_OP_breg3:
4669 return "DW_OP_breg3";
4670 case DW_OP_breg4:
4671 return "DW_OP_breg4";
4672 case DW_OP_breg5:
4673 return "DW_OP_breg5";
4674 case DW_OP_breg6:
4675 return "DW_OP_breg6";
4676 case DW_OP_breg7:
4677 return "DW_OP_breg7";
4678 case DW_OP_breg8:
4679 return "DW_OP_breg8";
4680 case DW_OP_breg9:
4681 return "DW_OP_breg9";
4682 case DW_OP_breg10:
4683 return "DW_OP_breg10";
4684 case DW_OP_breg11:
4685 return "DW_OP_breg11";
4686 case DW_OP_breg12:
4687 return "DW_OP_breg12";
4688 case DW_OP_breg13:
4689 return "DW_OP_breg13";
4690 case DW_OP_breg14:
4691 return "DW_OP_breg14";
4692 case DW_OP_breg15:
4693 return "DW_OP_breg15";
4694 case DW_OP_breg16:
4695 return "DW_OP_breg16";
4696 case DW_OP_breg17:
4697 return "DW_OP_breg17";
4698 case DW_OP_breg18:
4699 return "DW_OP_breg18";
4700 case DW_OP_breg19:
4701 return "DW_OP_breg19";
4702 case DW_OP_breg20:
4703 return "DW_OP_breg20";
4704 case DW_OP_breg21:
4705 return "DW_OP_breg21";
4706 case DW_OP_breg22:
4707 return "DW_OP_breg22";
4708 case DW_OP_breg23:
4709 return "DW_OP_breg23";
4710 case DW_OP_breg24:
4711 return "DW_OP_breg24";
4712 case DW_OP_breg25:
4713 return "DW_OP_breg25";
4714 case DW_OP_breg26:
4715 return "DW_OP_breg26";
4716 case DW_OP_breg27:
4717 return "DW_OP_breg27";
4718 case DW_OP_breg28:
4719 return "DW_OP_breg28";
4720 case DW_OP_breg29:
4721 return "DW_OP_breg29";
4722 case DW_OP_breg30:
4723 return "DW_OP_breg30";
4724 case DW_OP_breg31:
4725 return "DW_OP_breg31";
4726 case DW_OP_regx:
4727 return "DW_OP_regx";
4728 case DW_OP_fbreg:
4729 return "DW_OP_fbreg";
4730 case DW_OP_bregx:
4731 return "DW_OP_bregx";
4732 case DW_OP_piece:
4733 return "DW_OP_piece";
4734 case DW_OP_deref_size:
4735 return "DW_OP_deref_size";
4736 case DW_OP_xderef_size:
4737 return "DW_OP_xderef_size";
4738 case DW_OP_nop:
4739 return "DW_OP_nop";
4741 case DW_OP_push_object_address:
4742 return "DW_OP_push_object_address";
4743 case DW_OP_call2:
4744 return "DW_OP_call2";
4745 case DW_OP_call4:
4746 return "DW_OP_call4";
4747 case DW_OP_call_ref:
4748 return "DW_OP_call_ref";
4749 case DW_OP_implicit_value:
4750 return "DW_OP_implicit_value";
4751 case DW_OP_stack_value:
4752 return "DW_OP_stack_value";
4753 case DW_OP_form_tls_address:
4754 return "DW_OP_form_tls_address";
4755 case DW_OP_call_frame_cfa:
4756 return "DW_OP_call_frame_cfa";
4757 case DW_OP_bit_piece:
4758 return "DW_OP_bit_piece";
4760 case DW_OP_GNU_push_tls_address:
4761 return "DW_OP_GNU_push_tls_address";
4762 case DW_OP_GNU_uninit:
4763 return "DW_OP_GNU_uninit";
4764 case DW_OP_GNU_encoded_addr:
4765 return "DW_OP_GNU_encoded_addr";
4766 case DW_OP_GNU_implicit_pointer:
4767 return "DW_OP_GNU_implicit_pointer";
4768 case DW_OP_GNU_entry_value:
4769 return "DW_OP_GNU_entry_value";
4771 default:
4772 return "OP_<unknown>";
4776 /* Return a pointer to a newly allocated location description. Location
4777 descriptions are simple expression terms that can be strung
4778 together to form more complicated location (address) descriptions. */
4780 static inline dw_loc_descr_ref
4781 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4782 unsigned HOST_WIDE_INT oprnd2)
4784 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4786 descr->dw_loc_opc = op;
4787 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4788 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4789 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4790 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4792 return descr;
4795 /* Return a pointer to a newly allocated location description for
4796 REG and OFFSET. */
4798 static inline dw_loc_descr_ref
4799 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
4801 if (reg <= 31)
4802 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4803 offset, 0);
4804 else
4805 return new_loc_descr (DW_OP_bregx, reg, offset);
4808 /* Add a location description term to a location description expression. */
4810 static inline void
4811 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4813 dw_loc_descr_ref *d;
4815 /* Find the end of the chain. */
4816 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4819 *d = descr;
4822 /* Add a constant OFFSET to a location expression. */
4824 static void
4825 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4827 dw_loc_descr_ref loc;
4828 HOST_WIDE_INT *p;
4830 gcc_assert (*list_head != NULL);
4832 if (!offset)
4833 return;
4835 /* Find the end of the chain. */
4836 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4839 p = NULL;
4840 if (loc->dw_loc_opc == DW_OP_fbreg
4841 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4842 p = &loc->dw_loc_oprnd1.v.val_int;
4843 else if (loc->dw_loc_opc == DW_OP_bregx)
4844 p = &loc->dw_loc_oprnd2.v.val_int;
4846 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4847 offset. Don't optimize if an signed integer overflow would happen. */
4848 if (p != NULL
4849 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4850 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4851 *p += offset;
4853 else if (offset > 0)
4854 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4856 else
4858 loc->dw_loc_next = int_loc_descriptor (-offset);
4859 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4863 /* Add a constant OFFSET to a location list. */
4865 static void
4866 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4868 dw_loc_list_ref d;
4869 for (d = list_head; d != NULL; d = d->dw_loc_next)
4870 loc_descr_plus_const (&d->expr, offset);
4873 #define DWARF_REF_SIZE \
4874 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4876 static unsigned long size_of_locs (dw_loc_descr_ref);
4878 /* Return the size of a location descriptor. */
4880 static unsigned long
4881 size_of_loc_descr (dw_loc_descr_ref loc)
4883 unsigned long size = 1;
4885 switch (loc->dw_loc_opc)
4887 case DW_OP_addr:
4888 size += DWARF2_ADDR_SIZE;
4889 break;
4890 case DW_OP_const1u:
4891 case DW_OP_const1s:
4892 size += 1;
4893 break;
4894 case DW_OP_const2u:
4895 case DW_OP_const2s:
4896 size += 2;
4897 break;
4898 case DW_OP_const4u:
4899 case DW_OP_const4s:
4900 size += 4;
4901 break;
4902 case DW_OP_const8u:
4903 case DW_OP_const8s:
4904 size += 8;
4905 break;
4906 case DW_OP_constu:
4907 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4908 break;
4909 case DW_OP_consts:
4910 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4911 break;
4912 case DW_OP_pick:
4913 size += 1;
4914 break;
4915 case DW_OP_plus_uconst:
4916 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4917 break;
4918 case DW_OP_skip:
4919 case DW_OP_bra:
4920 size += 2;
4921 break;
4922 case DW_OP_breg0:
4923 case DW_OP_breg1:
4924 case DW_OP_breg2:
4925 case DW_OP_breg3:
4926 case DW_OP_breg4:
4927 case DW_OP_breg5:
4928 case DW_OP_breg6:
4929 case DW_OP_breg7:
4930 case DW_OP_breg8:
4931 case DW_OP_breg9:
4932 case DW_OP_breg10:
4933 case DW_OP_breg11:
4934 case DW_OP_breg12:
4935 case DW_OP_breg13:
4936 case DW_OP_breg14:
4937 case DW_OP_breg15:
4938 case DW_OP_breg16:
4939 case DW_OP_breg17:
4940 case DW_OP_breg18:
4941 case DW_OP_breg19:
4942 case DW_OP_breg20:
4943 case DW_OP_breg21:
4944 case DW_OP_breg22:
4945 case DW_OP_breg23:
4946 case DW_OP_breg24:
4947 case DW_OP_breg25:
4948 case DW_OP_breg26:
4949 case DW_OP_breg27:
4950 case DW_OP_breg28:
4951 case DW_OP_breg29:
4952 case DW_OP_breg30:
4953 case DW_OP_breg31:
4954 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4955 break;
4956 case DW_OP_regx:
4957 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4958 break;
4959 case DW_OP_fbreg:
4960 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4961 break;
4962 case DW_OP_bregx:
4963 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4964 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4965 break;
4966 case DW_OP_piece:
4967 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4968 break;
4969 case DW_OP_bit_piece:
4970 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4971 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4972 break;
4973 case DW_OP_deref_size:
4974 case DW_OP_xderef_size:
4975 size += 1;
4976 break;
4977 case DW_OP_call2:
4978 size += 2;
4979 break;
4980 case DW_OP_call4:
4981 size += 4;
4982 break;
4983 case DW_OP_call_ref:
4984 size += DWARF_REF_SIZE;
4985 break;
4986 case DW_OP_implicit_value:
4987 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4988 + loc->dw_loc_oprnd1.v.val_unsigned;
4989 break;
4990 case DW_OP_GNU_implicit_pointer:
4991 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4992 break;
4993 case DW_OP_GNU_entry_value:
4995 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
4996 size += size_of_uleb128 (op_size) + op_size;
4997 break;
4999 default:
5000 break;
5003 return size;
5006 /* Return the size of a series of location descriptors. */
5008 static unsigned long
5009 size_of_locs (dw_loc_descr_ref loc)
5011 dw_loc_descr_ref l;
5012 unsigned long size;
5014 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5015 field, to avoid writing to a PCH file. */
5016 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5018 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5019 break;
5020 size += size_of_loc_descr (l);
5022 if (! l)
5023 return size;
5025 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5027 l->dw_loc_addr = size;
5028 size += size_of_loc_descr (l);
5031 return size;
5034 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5035 static void get_ref_die_offset_label (char *, dw_die_ref);
5036 static void output_loc_sequence (dw_loc_descr_ref, int);
5038 /* Output location description stack opcode's operands (if any).
5039 The for_eh_or_skip parameter controls whether register numbers are
5040 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5041 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5042 info). This should be suppressed for the cases that have not been converted
5043 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
5045 static void
5046 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5048 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5049 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5051 switch (loc->dw_loc_opc)
5053 #ifdef DWARF2_DEBUGGING_INFO
5054 case DW_OP_const2u:
5055 case DW_OP_const2s:
5056 dw2_asm_output_data (2, val1->v.val_int, NULL);
5057 break;
5058 case DW_OP_const4u:
5059 if (loc->dtprel)
5061 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5062 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5063 val1->v.val_addr);
5064 fputc ('\n', asm_out_file);
5065 break;
5067 /* FALLTHRU */
5068 case DW_OP_const4s:
5069 dw2_asm_output_data (4, val1->v.val_int, NULL);
5070 break;
5071 case DW_OP_const8u:
5072 if (loc->dtprel)
5074 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5075 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5076 val1->v.val_addr);
5077 fputc ('\n', asm_out_file);
5078 break;
5080 /* FALLTHRU */
5081 case DW_OP_const8s:
5082 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5083 dw2_asm_output_data (8, val1->v.val_int, NULL);
5084 break;
5085 case DW_OP_skip:
5086 case DW_OP_bra:
5088 int offset;
5090 gcc_assert (val1->val_class == dw_val_class_loc);
5091 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5093 dw2_asm_output_data (2, offset, NULL);
5095 break;
5096 case DW_OP_implicit_value:
5097 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5098 switch (val2->val_class)
5100 case dw_val_class_const:
5101 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5102 break;
5103 case dw_val_class_vec:
5105 unsigned int elt_size = val2->v.val_vec.elt_size;
5106 unsigned int len = val2->v.val_vec.length;
5107 unsigned int i;
5108 unsigned char *p;
5110 if (elt_size > sizeof (HOST_WIDE_INT))
5112 elt_size /= 2;
5113 len *= 2;
5115 for (i = 0, p = val2->v.val_vec.array;
5116 i < len;
5117 i++, p += elt_size)
5118 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5119 "fp or vector constant word %u", i);
5121 break;
5122 case dw_val_class_const_double:
5124 unsigned HOST_WIDE_INT first, second;
5126 if (WORDS_BIG_ENDIAN)
5128 first = val2->v.val_double.high;
5129 second = val2->v.val_double.low;
5131 else
5133 first = val2->v.val_double.low;
5134 second = val2->v.val_double.high;
5136 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5137 first, NULL);
5138 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5139 second, NULL);
5141 break;
5142 case dw_val_class_addr:
5143 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5144 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5145 break;
5146 default:
5147 gcc_unreachable ();
5149 break;
5150 #else
5151 case DW_OP_const2u:
5152 case DW_OP_const2s:
5153 case DW_OP_const4u:
5154 case DW_OP_const4s:
5155 case DW_OP_const8u:
5156 case DW_OP_const8s:
5157 case DW_OP_skip:
5158 case DW_OP_bra:
5159 case DW_OP_implicit_value:
5160 /* We currently don't make any attempt to make sure these are
5161 aligned properly like we do for the main unwind info, so
5162 don't support emitting things larger than a byte if we're
5163 only doing unwinding. */
5164 gcc_unreachable ();
5165 #endif
5166 case DW_OP_const1u:
5167 case DW_OP_const1s:
5168 dw2_asm_output_data (1, val1->v.val_int, NULL);
5169 break;
5170 case DW_OP_constu:
5171 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5172 break;
5173 case DW_OP_consts:
5174 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5175 break;
5176 case DW_OP_pick:
5177 dw2_asm_output_data (1, val1->v.val_int, NULL);
5178 break;
5179 case DW_OP_plus_uconst:
5180 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5181 break;
5182 case DW_OP_breg0:
5183 case DW_OP_breg1:
5184 case DW_OP_breg2:
5185 case DW_OP_breg3:
5186 case DW_OP_breg4:
5187 case DW_OP_breg5:
5188 case DW_OP_breg6:
5189 case DW_OP_breg7:
5190 case DW_OP_breg8:
5191 case DW_OP_breg9:
5192 case DW_OP_breg10:
5193 case DW_OP_breg11:
5194 case DW_OP_breg12:
5195 case DW_OP_breg13:
5196 case DW_OP_breg14:
5197 case DW_OP_breg15:
5198 case DW_OP_breg16:
5199 case DW_OP_breg17:
5200 case DW_OP_breg18:
5201 case DW_OP_breg19:
5202 case DW_OP_breg20:
5203 case DW_OP_breg21:
5204 case DW_OP_breg22:
5205 case DW_OP_breg23:
5206 case DW_OP_breg24:
5207 case DW_OP_breg25:
5208 case DW_OP_breg26:
5209 case DW_OP_breg27:
5210 case DW_OP_breg28:
5211 case DW_OP_breg29:
5212 case DW_OP_breg30:
5213 case DW_OP_breg31:
5214 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5215 break;
5216 case DW_OP_regx:
5218 unsigned r = val1->v.val_unsigned;
5219 if (for_eh_or_skip >= 0)
5220 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5221 gcc_assert (size_of_uleb128 (r)
5222 == size_of_uleb128 (val1->v.val_unsigned));
5223 dw2_asm_output_data_uleb128 (r, NULL);
5225 break;
5226 case DW_OP_fbreg:
5227 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5228 break;
5229 case DW_OP_bregx:
5231 unsigned r = val1->v.val_unsigned;
5232 if (for_eh_or_skip >= 0)
5233 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5234 gcc_assert (size_of_uleb128 (r)
5235 == size_of_uleb128 (val1->v.val_unsigned));
5236 dw2_asm_output_data_uleb128 (r, NULL);
5237 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5239 break;
5240 case DW_OP_piece:
5241 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5242 break;
5243 case DW_OP_bit_piece:
5244 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5245 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5246 break;
5247 case DW_OP_deref_size:
5248 case DW_OP_xderef_size:
5249 dw2_asm_output_data (1, val1->v.val_int, NULL);
5250 break;
5252 case DW_OP_addr:
5253 if (loc->dtprel)
5255 if (targetm.asm_out.output_dwarf_dtprel)
5257 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5258 DWARF2_ADDR_SIZE,
5259 val1->v.val_addr);
5260 fputc ('\n', asm_out_file);
5262 else
5263 gcc_unreachable ();
5265 else
5267 #ifdef DWARF2_DEBUGGING_INFO
5268 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5269 #else
5270 gcc_unreachable ();
5271 #endif
5273 break;
5275 case DW_OP_GNU_implicit_pointer:
5277 char label[MAX_ARTIFICIAL_LABEL_BYTES
5278 + HOST_BITS_PER_WIDE_INT / 2 + 2];
5279 gcc_assert (val1->val_class == dw_val_class_die_ref);
5280 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5281 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5282 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5284 break;
5286 case DW_OP_GNU_entry_value:
5287 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5288 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5289 break;
5291 default:
5292 /* Other codes have no operands. */
5293 break;
5297 /* Output a sequence of location operations.
5298 The for_eh_or_skip parameter controls whether register numbers are
5299 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5300 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5301 info). This should be suppressed for the cases that have not been converted
5302 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
5304 static void
5305 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5307 for (; loc != NULL; loc = loc->dw_loc_next)
5309 enum dwarf_location_atom opc = loc->dw_loc_opc;
5310 /* Output the opcode. */
5311 if (for_eh_or_skip >= 0
5312 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5314 unsigned r = (opc - DW_OP_breg0);
5315 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5316 gcc_assert (r <= 31);
5317 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5319 else if (for_eh_or_skip >= 0
5320 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5322 unsigned r = (opc - DW_OP_reg0);
5323 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5324 gcc_assert (r <= 31);
5325 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5328 dw2_asm_output_data (1, opc,
5329 "%s", dwarf_stack_op_name (opc));
5331 /* Output the operand(s) (if any). */
5332 output_loc_operands (loc, for_eh_or_skip);
5336 /* Output location description stack opcode's operands (if any).
5337 The output is single bytes on a line, suitable for .cfi_escape. */
5339 static void
5340 output_loc_operands_raw (dw_loc_descr_ref loc)
5342 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5343 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5345 switch (loc->dw_loc_opc)
5347 case DW_OP_addr:
5348 case DW_OP_implicit_value:
5349 /* We cannot output addresses in .cfi_escape, only bytes. */
5350 gcc_unreachable ();
5352 case DW_OP_const1u:
5353 case DW_OP_const1s:
5354 case DW_OP_pick:
5355 case DW_OP_deref_size:
5356 case DW_OP_xderef_size:
5357 fputc (',', asm_out_file);
5358 dw2_asm_output_data_raw (1, val1->v.val_int);
5359 break;
5361 case DW_OP_const2u:
5362 case DW_OP_const2s:
5363 fputc (',', asm_out_file);
5364 dw2_asm_output_data_raw (2, val1->v.val_int);
5365 break;
5367 case DW_OP_const4u:
5368 case DW_OP_const4s:
5369 fputc (',', asm_out_file);
5370 dw2_asm_output_data_raw (4, val1->v.val_int);
5371 break;
5373 case DW_OP_const8u:
5374 case DW_OP_const8s:
5375 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5376 fputc (',', asm_out_file);
5377 dw2_asm_output_data_raw (8, val1->v.val_int);
5378 break;
5380 case DW_OP_skip:
5381 case DW_OP_bra:
5383 int offset;
5385 gcc_assert (val1->val_class == dw_val_class_loc);
5386 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5388 fputc (',', asm_out_file);
5389 dw2_asm_output_data_raw (2, offset);
5391 break;
5393 case DW_OP_regx:
5395 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5396 gcc_assert (size_of_uleb128 (r)
5397 == size_of_uleb128 (val1->v.val_unsigned));
5398 fputc (',', asm_out_file);
5399 dw2_asm_output_data_uleb128_raw (r);
5401 break;
5403 case DW_OP_constu:
5404 case DW_OP_plus_uconst:
5405 case DW_OP_piece:
5406 fputc (',', asm_out_file);
5407 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5408 break;
5410 case DW_OP_bit_piece:
5411 fputc (',', asm_out_file);
5412 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5413 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5414 break;
5416 case DW_OP_consts:
5417 case DW_OP_breg0:
5418 case DW_OP_breg1:
5419 case DW_OP_breg2:
5420 case DW_OP_breg3:
5421 case DW_OP_breg4:
5422 case DW_OP_breg5:
5423 case DW_OP_breg6:
5424 case DW_OP_breg7:
5425 case DW_OP_breg8:
5426 case DW_OP_breg9:
5427 case DW_OP_breg10:
5428 case DW_OP_breg11:
5429 case DW_OP_breg12:
5430 case DW_OP_breg13:
5431 case DW_OP_breg14:
5432 case DW_OP_breg15:
5433 case DW_OP_breg16:
5434 case DW_OP_breg17:
5435 case DW_OP_breg18:
5436 case DW_OP_breg19:
5437 case DW_OP_breg20:
5438 case DW_OP_breg21:
5439 case DW_OP_breg22:
5440 case DW_OP_breg23:
5441 case DW_OP_breg24:
5442 case DW_OP_breg25:
5443 case DW_OP_breg26:
5444 case DW_OP_breg27:
5445 case DW_OP_breg28:
5446 case DW_OP_breg29:
5447 case DW_OP_breg30:
5448 case DW_OP_breg31:
5449 case DW_OP_fbreg:
5450 fputc (',', asm_out_file);
5451 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5452 break;
5454 case DW_OP_bregx:
5456 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5457 gcc_assert (size_of_uleb128 (r)
5458 == size_of_uleb128 (val1->v.val_unsigned));
5459 fputc (',', asm_out_file);
5460 dw2_asm_output_data_uleb128_raw (r);
5461 fputc (',', asm_out_file);
5462 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5464 break;
5466 case DW_OP_GNU_implicit_pointer:
5467 case DW_OP_GNU_entry_value:
5468 gcc_unreachable ();
5469 break;
5471 default:
5472 /* Other codes have no operands. */
5473 break;
5477 static void
5478 output_loc_sequence_raw (dw_loc_descr_ref loc)
5480 while (1)
5482 enum dwarf_location_atom opc = loc->dw_loc_opc;
5483 /* Output the opcode. */
5484 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5486 unsigned r = (opc - DW_OP_breg0);
5487 r = DWARF2_FRAME_REG_OUT (r, 1);
5488 gcc_assert (r <= 31);
5489 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5491 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5493 unsigned r = (opc - DW_OP_reg0);
5494 r = DWARF2_FRAME_REG_OUT (r, 1);
5495 gcc_assert (r <= 31);
5496 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5498 /* Output the opcode. */
5499 fprintf (asm_out_file, "%#x", opc);
5500 output_loc_operands_raw (loc);
5502 if (!loc->dw_loc_next)
5503 break;
5504 loc = loc->dw_loc_next;
5506 fputc (',', asm_out_file);
5510 /* This routine will generate the correct assembly data for a location
5511 description based on a cfi entry with a complex address. */
5513 static void
5514 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5516 dw_loc_descr_ref loc;
5517 unsigned long size;
5519 if (cfi->dw_cfi_opc == DW_CFA_expression)
5521 unsigned r =
5522 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5523 dw2_asm_output_data (1, r, NULL);
5524 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5526 else
5527 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5529 /* Output the size of the block. */
5530 size = size_of_locs (loc);
5531 dw2_asm_output_data_uleb128 (size, NULL);
5533 /* Now output the operations themselves. */
5534 output_loc_sequence (loc, for_eh);
5537 /* Similar, but used for .cfi_escape. */
5539 static void
5540 output_cfa_loc_raw (dw_cfi_ref cfi)
5542 dw_loc_descr_ref loc;
5543 unsigned long size;
5545 if (cfi->dw_cfi_opc == DW_CFA_expression)
5547 unsigned r =
5548 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5549 fprintf (asm_out_file, "%#x,", r);
5550 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5552 else
5553 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5555 /* Output the size of the block. */
5556 size = size_of_locs (loc);
5557 dw2_asm_output_data_uleb128_raw (size);
5558 fputc (',', asm_out_file);
5560 /* Now output the operations themselves. */
5561 output_loc_sequence_raw (loc);
5564 /* This function builds a dwarf location descriptor sequence from a
5565 dw_cfa_location, adding the given OFFSET to the result of the
5566 expression. */
5568 static struct dw_loc_descr_struct *
5569 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5571 struct dw_loc_descr_struct *head, *tmp;
5573 offset += cfa->offset;
5575 if (cfa->indirect)
5577 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5578 head->dw_loc_oprnd1.val_class = dw_val_class_const;
5579 tmp = new_loc_descr (DW_OP_deref, 0, 0);
5580 add_loc_descr (&head, tmp);
5581 if (offset != 0)
5583 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5584 add_loc_descr (&head, tmp);
5587 else
5588 head = new_reg_loc_descr (cfa->reg, offset);
5590 return head;
5593 /* This function builds a dwarf location descriptor sequence for
5594 the address at OFFSET from the CFA when stack is aligned to
5595 ALIGNMENT byte. */
5597 static struct dw_loc_descr_struct *
5598 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5600 struct dw_loc_descr_struct *head;
5601 unsigned int dwarf_fp
5602 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5604 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
5605 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5607 head = new_reg_loc_descr (dwarf_fp, 0);
5608 add_loc_descr (&head, int_loc_descriptor (alignment));
5609 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5610 loc_descr_plus_const (&head, offset);
5612 else
5613 head = new_reg_loc_descr (dwarf_fp, offset);
5614 return head;
5617 /* This function fills in aa dw_cfa_location structure from a dwarf location
5618 descriptor sequence. */
5620 static void
5621 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5623 struct dw_loc_descr_struct *ptr;
5624 cfa->offset = 0;
5625 cfa->base_offset = 0;
5626 cfa->indirect = 0;
5627 cfa->reg = -1;
5629 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5631 enum dwarf_location_atom op = ptr->dw_loc_opc;
5633 switch (op)
5635 case DW_OP_reg0:
5636 case DW_OP_reg1:
5637 case DW_OP_reg2:
5638 case DW_OP_reg3:
5639 case DW_OP_reg4:
5640 case DW_OP_reg5:
5641 case DW_OP_reg6:
5642 case DW_OP_reg7:
5643 case DW_OP_reg8:
5644 case DW_OP_reg9:
5645 case DW_OP_reg10:
5646 case DW_OP_reg11:
5647 case DW_OP_reg12:
5648 case DW_OP_reg13:
5649 case DW_OP_reg14:
5650 case DW_OP_reg15:
5651 case DW_OP_reg16:
5652 case DW_OP_reg17:
5653 case DW_OP_reg18:
5654 case DW_OP_reg19:
5655 case DW_OP_reg20:
5656 case DW_OP_reg21:
5657 case DW_OP_reg22:
5658 case DW_OP_reg23:
5659 case DW_OP_reg24:
5660 case DW_OP_reg25:
5661 case DW_OP_reg26:
5662 case DW_OP_reg27:
5663 case DW_OP_reg28:
5664 case DW_OP_reg29:
5665 case DW_OP_reg30:
5666 case DW_OP_reg31:
5667 cfa->reg = op - DW_OP_reg0;
5668 break;
5669 case DW_OP_regx:
5670 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5671 break;
5672 case DW_OP_breg0:
5673 case DW_OP_breg1:
5674 case DW_OP_breg2:
5675 case DW_OP_breg3:
5676 case DW_OP_breg4:
5677 case DW_OP_breg5:
5678 case DW_OP_breg6:
5679 case DW_OP_breg7:
5680 case DW_OP_breg8:
5681 case DW_OP_breg9:
5682 case DW_OP_breg10:
5683 case DW_OP_breg11:
5684 case DW_OP_breg12:
5685 case DW_OP_breg13:
5686 case DW_OP_breg14:
5687 case DW_OP_breg15:
5688 case DW_OP_breg16:
5689 case DW_OP_breg17:
5690 case DW_OP_breg18:
5691 case DW_OP_breg19:
5692 case DW_OP_breg20:
5693 case DW_OP_breg21:
5694 case DW_OP_breg22:
5695 case DW_OP_breg23:
5696 case DW_OP_breg24:
5697 case DW_OP_breg25:
5698 case DW_OP_breg26:
5699 case DW_OP_breg27:
5700 case DW_OP_breg28:
5701 case DW_OP_breg29:
5702 case DW_OP_breg30:
5703 case DW_OP_breg31:
5704 cfa->reg = op - DW_OP_breg0;
5705 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5706 break;
5707 case DW_OP_bregx:
5708 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5709 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5710 break;
5711 case DW_OP_deref:
5712 cfa->indirect = 1;
5713 break;
5714 case DW_OP_plus_uconst:
5715 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5716 break;
5717 default:
5718 internal_error ("DW_LOC_OP %s not implemented",
5719 dwarf_stack_op_name (ptr->dw_loc_opc));
5724 /* And now, the support for symbolic debugging information. */
5726 /* .debug_str support. */
5727 static int output_indirect_string (void **, void *);
5729 static void dwarf2out_init (const char *);
5730 static void dwarf2out_finish (const char *);
5731 static void dwarf2out_assembly_start (void);
5732 static void dwarf2out_define (unsigned int, const char *);
5733 static void dwarf2out_undef (unsigned int, const char *);
5734 static void dwarf2out_start_source_file (unsigned, const char *);
5735 static void dwarf2out_end_source_file (unsigned);
5736 static void dwarf2out_function_decl (tree);
5737 static void dwarf2out_begin_block (unsigned, unsigned);
5738 static void dwarf2out_end_block (unsigned, unsigned);
5739 static bool dwarf2out_ignore_block (const_tree);
5740 static void dwarf2out_global_decl (tree);
5741 static void dwarf2out_type_decl (tree, int);
5742 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5743 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5744 dw_die_ref);
5745 static void dwarf2out_abstract_function (tree);
5746 static void dwarf2out_var_location (rtx);
5747 static void dwarf2out_begin_function (tree);
5748 static void dwarf2out_set_name (tree, tree);
5750 /* The debug hooks structure. */
5752 const struct gcc_debug_hooks dwarf2_debug_hooks =
5754 dwarf2out_init,
5755 dwarf2out_finish,
5756 dwarf2out_assembly_start,
5757 dwarf2out_define,
5758 dwarf2out_undef,
5759 dwarf2out_start_source_file,
5760 dwarf2out_end_source_file,
5761 dwarf2out_begin_block,
5762 dwarf2out_end_block,
5763 dwarf2out_ignore_block,
5764 dwarf2out_source_line,
5765 dwarf2out_begin_prologue,
5766 #if VMS_DEBUGGING_INFO
5767 dwarf2out_vms_end_prologue,
5768 dwarf2out_vms_begin_epilogue,
5769 #else
5770 debug_nothing_int_charstar,
5771 debug_nothing_int_charstar,
5772 #endif
5773 dwarf2out_end_epilogue,
5774 dwarf2out_begin_function,
5775 debug_nothing_int, /* end_function */
5776 dwarf2out_function_decl, /* function_decl */
5777 dwarf2out_global_decl,
5778 dwarf2out_type_decl, /* type_decl */
5779 dwarf2out_imported_module_or_decl,
5780 debug_nothing_tree, /* deferred_inline_function */
5781 /* The DWARF 2 backend tries to reduce debugging bloat by not
5782 emitting the abstract description of inline functions until
5783 something tries to reference them. */
5784 dwarf2out_abstract_function, /* outlining_inline_function */
5785 debug_nothing_rtx, /* label */
5786 debug_nothing_int, /* handle_pch */
5787 dwarf2out_var_location,
5788 dwarf2out_switch_text_section,
5789 dwarf2out_set_name,
5790 1, /* start_end_main_source_file */
5791 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
5794 /* NOTE: In the comments in this file, many references are made to
5795 "Debugging Information Entries". This term is abbreviated as `DIE'
5796 throughout the remainder of this file. */
5798 /* An internal representation of the DWARF output is built, and then
5799 walked to generate the DWARF debugging info. The walk of the internal
5800 representation is done after the entire program has been compiled.
5801 The types below are used to describe the internal representation. */
5803 /* Whether to put type DIEs into their own section .debug_types instead
5804 of making them part of the .debug_info section. Only supported for
5805 Dwarf V4 or higher and the user didn't disable them through
5806 -fno-debug-types-section. It is more efficient to put them in a
5807 separate comdat sections since the linker will then be able to
5808 remove duplicates. But not all tools support .debug_types sections
5809 yet. */
5811 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
5813 /* Various DIE's use offsets relative to the beginning of the
5814 .debug_info section to refer to each other. */
5816 typedef long int dw_offset;
5818 /* Define typedefs here to avoid circular dependencies. */
5820 typedef struct dw_attr_struct *dw_attr_ref;
5821 typedef struct dw_line_info_struct *dw_line_info_ref;
5822 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5823 typedef struct pubname_struct *pubname_ref;
5824 typedef struct dw_ranges_struct *dw_ranges_ref;
5825 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5826 typedef struct comdat_type_struct *comdat_type_node_ref;
5828 /* Each entry in the line_info_table maintains the file and
5829 line number associated with the label generated for that
5830 entry. The label gives the PC value associated with
5831 the line number entry. */
5833 typedef struct GTY(()) dw_line_info_struct {
5834 unsigned long dw_file_num;
5835 unsigned long dw_line_num;
5837 dw_line_info_entry;
5839 /* Line information for functions in separate sections; each one gets its
5840 own sequence. */
5841 typedef struct GTY(()) dw_separate_line_info_struct {
5842 unsigned long dw_file_num;
5843 unsigned long dw_line_num;
5844 unsigned long function;
5846 dw_separate_line_info_entry;
5848 /* Each DIE attribute has a field specifying the attribute kind,
5849 a link to the next attribute in the chain, and an attribute value.
5850 Attributes are typically linked below the DIE they modify. */
5852 typedef struct GTY(()) dw_attr_struct {
5853 enum dwarf_attribute dw_attr;
5854 dw_val_node dw_attr_val;
5856 dw_attr_node;
5858 DEF_VEC_O(dw_attr_node);
5859 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5861 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
5862 The children of each node form a circular list linked by
5863 die_sib. die_child points to the node *before* the "first" child node. */
5865 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5866 union die_symbol_or_type_node
5868 char * GTY ((tag ("0"))) die_symbol;
5869 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5871 GTY ((desc ("use_debug_types"))) die_id;
5872 VEC(dw_attr_node,gc) * die_attr;
5873 dw_die_ref die_parent;
5874 dw_die_ref die_child;
5875 dw_die_ref die_sib;
5876 dw_die_ref die_definition; /* ref from a specification to its definition */
5877 dw_offset die_offset;
5878 unsigned long die_abbrev;
5879 int die_mark;
5880 /* Die is used and must not be pruned as unused. */
5881 int die_perennial_p;
5882 unsigned int decl_id;
5883 enum dwarf_tag die_tag;
5885 die_node;
5887 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
5888 #define FOR_EACH_CHILD(die, c, expr) do { \
5889 c = die->die_child; \
5890 if (c) do { \
5891 c = c->die_sib; \
5892 expr; \
5893 } while (c != die->die_child); \
5894 } while (0)
5896 /* The pubname structure */
5898 typedef struct GTY(()) pubname_struct {
5899 dw_die_ref die;
5900 const char *name;
5902 pubname_entry;
5904 DEF_VEC_O(pubname_entry);
5905 DEF_VEC_ALLOC_O(pubname_entry, gc);
5907 struct GTY(()) dw_ranges_struct {
5908 /* If this is positive, it's a block number, otherwise it's a
5909 bitwise-negated index into dw_ranges_by_label. */
5910 int num;
5913 /* A structure to hold a macinfo entry. */
5915 typedef struct GTY(()) macinfo_struct {
5916 unsigned HOST_WIDE_INT code;
5917 unsigned HOST_WIDE_INT lineno;
5918 const char *info;
5920 macinfo_entry;
5922 DEF_VEC_O(macinfo_entry);
5923 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5925 struct GTY(()) dw_ranges_by_label_struct {
5926 const char *begin;
5927 const char *end;
5930 /* The comdat type node structure. */
5931 typedef struct GTY(()) comdat_type_struct
5933 dw_die_ref root_die;
5934 dw_die_ref type_die;
5935 char signature[DWARF_TYPE_SIGNATURE_SIZE];
5936 struct comdat_type_struct *next;
5938 comdat_type_node;
5940 /* The limbo die list structure. */
5941 typedef struct GTY(()) limbo_die_struct {
5942 dw_die_ref die;
5943 tree created_for;
5944 struct limbo_die_struct *next;
5946 limbo_die_node;
5948 typedef struct skeleton_chain_struct
5950 dw_die_ref old_die;
5951 dw_die_ref new_die;
5952 struct skeleton_chain_struct *parent;
5954 skeleton_chain_node;
5956 /* How to start an assembler comment. */
5957 #ifndef ASM_COMMENT_START
5958 #define ASM_COMMENT_START ";#"
5959 #endif
5961 /* Define a macro which returns nonzero for a TYPE_DECL which was
5962 implicitly generated for a tagged type.
5964 Note that unlike the gcc front end (which generates a NULL named
5965 TYPE_DECL node for each complete tagged type, each array type, and
5966 each function type node created) the g++ front end generates a
5967 _named_ TYPE_DECL node for each tagged type node created.
5968 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5969 generate a DW_TAG_typedef DIE for them. */
5971 #define TYPE_DECL_IS_STUB(decl) \
5972 (DECL_NAME (decl) == NULL_TREE \
5973 || (DECL_ARTIFICIAL (decl) \
5974 && is_tagged_type (TREE_TYPE (decl)) \
5975 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
5976 /* This is necessary for stub decls that \
5977 appear in nested inline functions. */ \
5978 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5979 && (decl_ultimate_origin (decl) \
5980 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5982 /* Information concerning the compilation unit's programming
5983 language, and compiler version. */
5985 /* Fixed size portion of the DWARF compilation unit header. */
5986 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5987 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5989 /* Fixed size portion of the DWARF comdat type unit header. */
5990 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5991 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5992 + DWARF_OFFSET_SIZE)
5994 /* Fixed size portion of public names info. */
5995 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5997 /* Fixed size portion of the address range info. */
5998 #define DWARF_ARANGES_HEADER_SIZE \
5999 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6000 DWARF2_ADDR_SIZE * 2) \
6001 - DWARF_INITIAL_LENGTH_SIZE)
6003 /* Size of padding portion in the address range info. It must be
6004 aligned to twice the pointer size. */
6005 #define DWARF_ARANGES_PAD_SIZE \
6006 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6007 DWARF2_ADDR_SIZE * 2) \
6008 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6010 /* Use assembler line directives if available. */
6011 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6012 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6013 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6014 #else
6015 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6016 #endif
6017 #endif
6019 /* Minimum line offset in a special line info. opcode.
6020 This value was chosen to give a reasonable range of values. */
6021 #define DWARF_LINE_BASE -10
6023 /* First special line opcode - leave room for the standard opcodes. */
6024 #define DWARF_LINE_OPCODE_BASE 10
6026 /* Range of line offsets in a special line info. opcode. */
6027 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
6029 /* Flag that indicates the initial value of the is_stmt_start flag.
6030 In the present implementation, we do not mark any lines as
6031 the beginning of a source statement, because that information
6032 is not made available by the GCC front-end. */
6033 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6035 /* Maximum number of operations per instruction bundle. */
6036 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6037 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6038 #endif
6040 /* This location is used by calc_die_sizes() to keep track
6041 the offset of each DIE within the .debug_info section. */
6042 static unsigned long next_die_offset;
6044 /* Record the root of the DIE's built for the current compilation unit. */
6045 static GTY(()) dw_die_ref single_comp_unit_die;
6047 /* A list of type DIEs that have been separated into comdat sections. */
6048 static GTY(()) comdat_type_node *comdat_type_list;
6050 /* A list of DIEs with a NULL parent waiting to be relocated. */
6051 static GTY(()) limbo_die_node *limbo_die_list;
6053 /* A list of DIEs for which we may have to generate
6054 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
6055 static GTY(()) limbo_die_node *deferred_asm_name;
6057 /* Filenames referenced by this compilation unit. */
6058 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6060 /* A hash table of references to DIE's that describe declarations.
6061 The key is a DECL_UID() which is a unique number identifying each decl. */
6062 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6064 /* A hash table of references to DIE's that describe COMMON blocks.
6065 The key is DECL_UID() ^ die_parent. */
6066 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6068 typedef struct GTY(()) die_arg_entry_struct {
6069 dw_die_ref die;
6070 tree arg;
6071 } die_arg_entry;
6073 DEF_VEC_O(die_arg_entry);
6074 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6076 /* Node of the variable location list. */
6077 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6078 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6079 EXPR_LIST chain. For small bitsizes, bitsize is encoded
6080 in mode of the EXPR_LIST node and first EXPR_LIST operand
6081 is either NOTE_INSN_VAR_LOCATION for a piece with a known
6082 location or NULL for padding. For larger bitsizes,
6083 mode is 0 and first operand is a CONCAT with bitsize
6084 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6085 NULL as second operand. */
6086 rtx GTY (()) loc;
6087 const char * GTY (()) label;
6088 struct var_loc_node * GTY (()) next;
6091 /* Variable location list. */
6092 struct GTY (()) var_loc_list_def {
6093 struct var_loc_node * GTY (()) first;
6095 /* Pointer to the last but one or last element of the
6096 chained list. If the list is empty, both first and
6097 last are NULL, if the list contains just one node
6098 or the last node certainly is not redundant, it points
6099 to the last node, otherwise points to the last but one.
6100 Do not mark it for GC because it is marked through the chain. */
6101 struct var_loc_node * GTY ((skip ("%h"))) last;
6103 /* Pointer to the last element before section switch,
6104 if NULL, either sections weren't switched or first
6105 is after section switch. */
6106 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6108 /* DECL_UID of the variable decl. */
6109 unsigned int decl_id;
6111 typedef struct var_loc_list_def var_loc_list;
6113 /* Call argument location list. */
6114 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6115 rtx GTY (()) call_arg_loc_note;
6116 const char * GTY (()) label;
6117 tree GTY (()) block;
6118 bool tail_call_p;
6119 rtx GTY (()) symbol_ref;
6120 struct call_arg_loc_node * GTY (()) next;
6124 /* Table of decl location linked lists. */
6125 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6127 /* Head and tail of call_arg_loc chain. */
6128 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6129 static struct call_arg_loc_node *call_arg_loc_last;
6131 /* Number of call sites in the current function. */
6132 static int call_site_count = -1;
6133 /* Number of tail call sites in the current function. */
6134 static int tail_call_site_count = -1;
6136 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6137 DIEs. */
6138 static VEC (dw_die_ref, heap) *block_map;
6140 /* A cached location list. */
6141 struct GTY (()) cached_dw_loc_list_def {
6142 /* The DECL_UID of the decl that this entry describes. */
6143 unsigned int decl_id;
6145 /* The cached location list. */
6146 dw_loc_list_ref loc_list;
6148 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6150 /* Table of cached location lists. */
6151 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6153 /* A pointer to the base of a list of references to DIE's that
6154 are uniquely identified by their tag, presence/absence of
6155 children DIE's, and list of attribute/value pairs. */
6156 static GTY((length ("abbrev_die_table_allocated")))
6157 dw_die_ref *abbrev_die_table;
6159 /* Number of elements currently allocated for abbrev_die_table. */
6160 static GTY(()) unsigned abbrev_die_table_allocated;
6162 /* Number of elements in type_die_table currently in use. */
6163 static GTY(()) unsigned abbrev_die_table_in_use;
6165 /* Size (in elements) of increments by which we may expand the
6166 abbrev_die_table. */
6167 #define ABBREV_DIE_TABLE_INCREMENT 256
6169 /* A pointer to the base of a table that contains line information
6170 for each source code line in .text in the compilation unit. */
6171 static GTY((length ("line_info_table_allocated")))
6172 dw_line_info_ref line_info_table;
6174 /* Number of elements currently allocated for line_info_table. */
6175 static GTY(()) unsigned line_info_table_allocated;
6177 /* Number of elements in line_info_table currently in use. */
6178 static GTY(()) unsigned line_info_table_in_use;
6180 /* A pointer to the base of a table that contains line information
6181 for each source code line outside of .text in the compilation unit. */
6182 static GTY ((length ("separate_line_info_table_allocated")))
6183 dw_separate_line_info_ref separate_line_info_table;
6185 /* Number of elements currently allocated for separate_line_info_table. */
6186 static GTY(()) unsigned separate_line_info_table_allocated;
6188 /* Number of elements in separate_line_info_table currently in use. */
6189 static GTY(()) unsigned separate_line_info_table_in_use;
6191 /* Size (in elements) of increments by which we may expand the
6192 line_info_table. */
6193 #define LINE_INFO_TABLE_INCREMENT 1024
6195 /* A flag to tell pubnames/types export if there is an info section to
6196 refer to. */
6197 static bool info_section_emitted;
6199 /* A pointer to the base of a table that contains a list of publicly
6200 accessible names. */
6201 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
6203 /* A pointer to the base of a table that contains a list of publicly
6204 accessible types. */
6205 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6207 /* A pointer to the base of a table that contains a list of macro
6208 defines/undefines (and file start/end markers). */
6209 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6211 /* Array of dies for which we should generate .debug_ranges info. */
6212 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6214 /* Number of elements currently allocated for ranges_table. */
6215 static GTY(()) unsigned ranges_table_allocated;
6217 /* Number of elements in ranges_table currently in use. */
6218 static GTY(()) unsigned ranges_table_in_use;
6220 /* Array of pairs of labels referenced in ranges_table. */
6221 static GTY ((length ("ranges_by_label_allocated")))
6222 dw_ranges_by_label_ref ranges_by_label;
6224 /* Number of elements currently allocated for ranges_by_label. */
6225 static GTY(()) unsigned ranges_by_label_allocated;
6227 /* Number of elements in ranges_by_label currently in use. */
6228 static GTY(()) unsigned ranges_by_label_in_use;
6230 /* Size (in elements) of increments by which we may expand the
6231 ranges_table. */
6232 #define RANGES_TABLE_INCREMENT 64
6234 /* Whether we have location lists that need outputting */
6235 static GTY(()) bool have_location_lists;
6237 /* Unique label counter. */
6238 static GTY(()) unsigned int loclabel_num;
6240 /* Unique label counter for point-of-call tables. */
6241 static GTY(()) unsigned int poc_label_num;
6243 /* Record whether the function being analyzed contains inlined functions. */
6244 static int current_function_has_inlines;
6246 /* The last file entry emitted by maybe_emit_file(). */
6247 static GTY(()) struct dwarf_file_data * last_emitted_file;
6249 /* Number of internal labels generated by gen_internal_sym(). */
6250 static GTY(()) int label_num;
6252 /* Cached result of previous call to lookup_filename. */
6253 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6255 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6257 /* Instances of generic types for which we need to generate debug
6258 info that describe their generic parameters and arguments. That
6259 generation needs to happen once all types are properly laid out so
6260 we do it at the end of compilation. */
6261 static GTY(()) VEC(tree,gc) *generic_type_instances;
6263 /* Offset from the "steady-state frame pointer" to the frame base,
6264 within the current function. */
6265 static HOST_WIDE_INT frame_pointer_fb_offset;
6267 /* Forward declarations for functions defined in this file. */
6269 static int is_pseudo_reg (const_rtx);
6270 static tree type_main_variant (tree);
6271 static int is_tagged_type (const_tree);
6272 static const char *dwarf_tag_name (unsigned);
6273 static const char *dwarf_attr_name (unsigned);
6274 static const char *dwarf_form_name (unsigned);
6275 static tree decl_ultimate_origin (const_tree);
6276 static tree decl_class_context (tree);
6277 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6278 static inline enum dw_val_class AT_class (dw_attr_ref);
6279 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6280 static inline unsigned AT_flag (dw_attr_ref);
6281 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6282 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6283 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6284 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6285 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6286 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6287 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6288 unsigned int, unsigned char *);
6289 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6290 static hashval_t debug_str_do_hash (const void *);
6291 static int debug_str_eq (const void *, const void *);
6292 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6293 static inline const char *AT_string (dw_attr_ref);
6294 static enum dwarf_form AT_string_form (dw_attr_ref);
6295 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6296 static void add_AT_specification (dw_die_ref, dw_die_ref);
6297 static inline dw_die_ref AT_ref (dw_attr_ref);
6298 static inline int AT_ref_external (dw_attr_ref);
6299 static inline void set_AT_ref_external (dw_attr_ref, int);
6300 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6301 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6302 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6303 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6304 dw_loc_list_ref);
6305 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6306 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6307 static inline rtx AT_addr (dw_attr_ref);
6308 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6309 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6310 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6311 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6312 unsigned HOST_WIDE_INT);
6313 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6314 unsigned long);
6315 static inline const char *AT_lbl (dw_attr_ref);
6316 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6317 static const char *get_AT_low_pc (dw_die_ref);
6318 static const char *get_AT_hi_pc (dw_die_ref);
6319 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6320 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6321 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6322 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6323 static bool is_cxx (void);
6324 static bool is_fortran (void);
6325 static bool is_ada (void);
6326 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6327 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6328 static void add_child_die (dw_die_ref, dw_die_ref);
6329 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6330 static dw_die_ref lookup_type_die (tree);
6331 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6332 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6333 static void equate_type_number_to_die (tree, dw_die_ref);
6334 static hashval_t decl_die_table_hash (const void *);
6335 static int decl_die_table_eq (const void *, const void *);
6336 static dw_die_ref lookup_decl_die (tree);
6337 static hashval_t common_block_die_table_hash (const void *);
6338 static int common_block_die_table_eq (const void *, const void *);
6339 static hashval_t decl_loc_table_hash (const void *);
6340 static int decl_loc_table_eq (const void *, const void *);
6341 static var_loc_list *lookup_decl_loc (const_tree);
6342 static void equate_decl_number_to_die (tree, dw_die_ref);
6343 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6344 static void print_spaces (FILE *);
6345 static void print_die (dw_die_ref, FILE *);
6346 static void print_dwarf_line_table (FILE *);
6347 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6348 static dw_die_ref pop_compile_unit (dw_die_ref);
6349 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6350 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6351 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6352 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6353 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6354 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6355 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6356 struct md5_ctx *, int *);
6357 struct checksum_attributes;
6358 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6359 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6360 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6361 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6362 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6363 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6364 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6365 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6366 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6367 static void compute_section_prefix (dw_die_ref);
6368 static int is_type_die (dw_die_ref);
6369 static int is_comdat_die (dw_die_ref);
6370 static int is_symbol_die (dw_die_ref);
6371 static void assign_symbol_names (dw_die_ref);
6372 static void break_out_includes (dw_die_ref);
6373 static int is_declaration_die (dw_die_ref);
6374 static int should_move_die_to_comdat (dw_die_ref);
6375 static dw_die_ref clone_as_declaration (dw_die_ref);
6376 static dw_die_ref clone_die (dw_die_ref);
6377 static dw_die_ref clone_tree (dw_die_ref);
6378 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6379 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6380 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6381 static dw_die_ref generate_skeleton (dw_die_ref);
6382 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6383 dw_die_ref);
6384 static void break_out_comdat_types (dw_die_ref);
6385 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6386 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6387 static void copy_decls_for_unworthy_types (dw_die_ref);
6389 static hashval_t htab_cu_hash (const void *);
6390 static int htab_cu_eq (const void *, const void *);
6391 static void htab_cu_del (void *);
6392 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6393 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6394 static void add_sibling_attributes (dw_die_ref);
6395 static void build_abbrev_table (dw_die_ref);
6396 static void output_location_lists (dw_die_ref);
6397 static int constant_size (unsigned HOST_WIDE_INT);
6398 static unsigned long size_of_die (dw_die_ref);
6399 static void calc_die_sizes (dw_die_ref);
6400 static void mark_dies (dw_die_ref);
6401 static void unmark_dies (dw_die_ref);
6402 static void unmark_all_dies (dw_die_ref);
6403 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6404 static unsigned long size_of_aranges (void);
6405 static enum dwarf_form value_format (dw_attr_ref);
6406 static void output_value_format (dw_attr_ref);
6407 static void output_abbrev_section (void);
6408 static void output_die_symbol (dw_die_ref);
6409 static void output_die (dw_die_ref);
6410 static void output_compilation_unit_header (void);
6411 static void output_comp_unit (dw_die_ref, int);
6412 static void output_comdat_type_unit (comdat_type_node *);
6413 static const char *dwarf2_name (tree, int);
6414 static void add_pubname (tree, dw_die_ref);
6415 static void add_pubname_string (const char *, dw_die_ref);
6416 static void add_pubtype (tree, dw_die_ref);
6417 static void output_pubnames (VEC (pubname_entry,gc) *);
6418 static void output_aranges (unsigned long);
6419 static unsigned int add_ranges_num (int);
6420 static unsigned int add_ranges (const_tree);
6421 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6422 bool *);
6423 static void output_ranges (void);
6424 static void output_line_info (void);
6425 static void output_file_names (void);
6426 static dw_die_ref base_type_die (tree);
6427 static int is_base_type (tree);
6428 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6429 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6430 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6431 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6432 static int type_is_enum (const_tree);
6433 static unsigned int dbx_reg_number (const_rtx);
6434 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6435 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6436 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6437 enum var_init_status);
6438 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6439 enum var_init_status);
6440 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6441 enum var_init_status);
6442 static int is_based_loc (const_rtx);
6443 static int resolve_one_addr (rtx *, void *);
6444 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6445 enum var_init_status);
6446 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6447 enum var_init_status);
6448 static dw_loc_list_ref loc_list_from_tree (tree, int);
6449 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6450 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6451 static tree field_type (const_tree);
6452 static unsigned int simple_type_align_in_bits (const_tree);
6453 static unsigned int simple_decl_align_in_bits (const_tree);
6454 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6455 static HOST_WIDE_INT field_byte_offset (const_tree);
6456 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6457 dw_loc_list_ref);
6458 static void add_data_member_location_attribute (dw_die_ref, tree);
6459 static bool add_const_value_attribute (dw_die_ref, rtx);
6460 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6461 static void insert_double (double_int, unsigned char *);
6462 static void insert_float (const_rtx, unsigned char *);
6463 static rtx rtl_for_decl_location (tree);
6464 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6465 enum dwarf_attribute);
6466 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6467 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6468 static void add_name_attribute (dw_die_ref, const char *);
6469 static void add_comp_dir_attribute (dw_die_ref);
6470 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6471 static void add_subscript_info (dw_die_ref, tree, bool);
6472 static void add_byte_size_attribute (dw_die_ref, tree);
6473 static void add_bit_offset_attribute (dw_die_ref, tree);
6474 static void add_bit_size_attribute (dw_die_ref, tree);
6475 static void add_prototyped_attribute (dw_die_ref, tree);
6476 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6477 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6478 static void add_src_coords_attributes (dw_die_ref, tree);
6479 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6480 static void push_decl_scope (tree);
6481 static void pop_decl_scope (void);
6482 static dw_die_ref scope_die_for (tree, dw_die_ref);
6483 static inline int local_scope_p (dw_die_ref);
6484 static inline int class_scope_p (dw_die_ref);
6485 static inline int class_or_namespace_scope_p (dw_die_ref);
6486 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6487 static void add_calling_convention_attribute (dw_die_ref, tree);
6488 static const char *type_tag (const_tree);
6489 static tree member_declared_type (const_tree);
6490 #if 0
6491 static const char *decl_start_label (tree);
6492 #endif
6493 static void gen_array_type_die (tree, dw_die_ref);
6494 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6495 #if 0
6496 static void gen_entry_point_die (tree, dw_die_ref);
6497 #endif
6498 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6499 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6500 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
6501 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6502 static void gen_formal_types_die (tree, dw_die_ref);
6503 static void gen_subprogram_die (tree, dw_die_ref);
6504 static void gen_variable_die (tree, tree, dw_die_ref);
6505 static void gen_const_die (tree, dw_die_ref);
6506 static void gen_label_die (tree, dw_die_ref);
6507 static void gen_lexical_block_die (tree, dw_die_ref, int);
6508 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6509 static void gen_field_die (tree, dw_die_ref);
6510 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6511 static dw_die_ref gen_compile_unit_die (const char *);
6512 static void gen_inheritance_die (tree, tree, dw_die_ref);
6513 static void gen_member_die (tree, dw_die_ref);
6514 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6515 enum debug_info_usage);
6516 static void gen_subroutine_type_die (tree, dw_die_ref);
6517 static void gen_typedef_die (tree, dw_die_ref);
6518 static void gen_type_die (tree, dw_die_ref);
6519 static void gen_block_die (tree, dw_die_ref, int);
6520 static void decls_for_scope (tree, dw_die_ref, int);
6521 static int is_redundant_typedef (const_tree);
6522 static bool is_naming_typedef_decl (const_tree);
6523 static inline dw_die_ref get_context_die (tree);
6524 static void gen_namespace_die (tree, dw_die_ref);
6525 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6526 static dw_die_ref force_decl_die (tree);
6527 static dw_die_ref force_type_die (tree);
6528 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6529 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6530 static struct dwarf_file_data * lookup_filename (const char *);
6531 static void retry_incomplete_types (void);
6532 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6533 static void gen_generic_params_dies (tree);
6534 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6535 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6536 static void splice_child_die (dw_die_ref, dw_die_ref);
6537 static int file_info_cmp (const void *, const void *);
6538 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6539 const char *, const char *);
6540 static void output_loc_list (dw_loc_list_ref);
6541 static char *gen_internal_sym (const char *);
6543 static void prune_unmark_dies (dw_die_ref);
6544 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6545 static void prune_unused_types_mark (dw_die_ref, int);
6546 static void prune_unused_types_walk (dw_die_ref);
6547 static void prune_unused_types_walk_attribs (dw_die_ref);
6548 static void prune_unused_types_prune (dw_die_ref);
6549 static void prune_unused_types (void);
6550 static int maybe_emit_file (struct dwarf_file_data *fd);
6551 static inline const char *AT_vms_delta1 (dw_attr_ref);
6552 static inline const char *AT_vms_delta2 (dw_attr_ref);
6553 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6554 const char *, const char *);
6555 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6556 static void gen_remaining_tmpl_value_param_die_attribute (void);
6557 static bool generic_type_p (tree);
6558 static void schedule_generic_params_dies_gen (tree t);
6559 static void gen_scheduled_generic_parms_dies (void);
6561 /* Section names used to hold DWARF debugging information. */
6562 #ifndef DEBUG_INFO_SECTION
6563 #define DEBUG_INFO_SECTION ".debug_info"
6564 #endif
6565 #ifndef DEBUG_ABBREV_SECTION
6566 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
6567 #endif
6568 #ifndef DEBUG_ARANGES_SECTION
6569 #define DEBUG_ARANGES_SECTION ".debug_aranges"
6570 #endif
6571 #ifndef DEBUG_MACINFO_SECTION
6572 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
6573 #endif
6574 #ifndef DEBUG_LINE_SECTION
6575 #define DEBUG_LINE_SECTION ".debug_line"
6576 #endif
6577 #ifndef DEBUG_LOC_SECTION
6578 #define DEBUG_LOC_SECTION ".debug_loc"
6579 #endif
6580 #ifndef DEBUG_PUBNAMES_SECTION
6581 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
6582 #endif
6583 #ifndef DEBUG_PUBTYPES_SECTION
6584 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
6585 #endif
6586 #ifndef DEBUG_STR_SECTION
6587 #define DEBUG_STR_SECTION ".debug_str"
6588 #endif
6589 #ifndef DEBUG_RANGES_SECTION
6590 #define DEBUG_RANGES_SECTION ".debug_ranges"
6591 #endif
6593 /* Standard ELF section names for compiled code and data. */
6594 #ifndef TEXT_SECTION_NAME
6595 #define TEXT_SECTION_NAME ".text"
6596 #endif
6598 /* Section flags for .debug_str section. */
6599 #define DEBUG_STR_SECTION_FLAGS \
6600 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
6601 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
6602 : SECTION_DEBUG)
6604 /* Labels we insert at beginning sections we can reference instead of
6605 the section names themselves. */
6607 #ifndef TEXT_SECTION_LABEL
6608 #define TEXT_SECTION_LABEL "Ltext"
6609 #endif
6610 #ifndef COLD_TEXT_SECTION_LABEL
6611 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
6612 #endif
6613 #ifndef DEBUG_LINE_SECTION_LABEL
6614 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
6615 #endif
6616 #ifndef DEBUG_INFO_SECTION_LABEL
6617 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
6618 #endif
6619 #ifndef DEBUG_ABBREV_SECTION_LABEL
6620 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
6621 #endif
6622 #ifndef DEBUG_LOC_SECTION_LABEL
6623 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
6624 #endif
6625 #ifndef DEBUG_RANGES_SECTION_LABEL
6626 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
6627 #endif
6628 #ifndef DEBUG_MACINFO_SECTION_LABEL
6629 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
6630 #endif
6633 /* Definitions of defaults for formats and names of various special
6634 (artificial) labels which may be generated within this file (when the -g
6635 options is used and DWARF2_DEBUGGING_INFO is in effect.
6636 If necessary, these may be overridden from within the tm.h file, but
6637 typically, overriding these defaults is unnecessary. */
6639 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6640 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6641 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6642 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6643 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6644 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6645 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6646 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6647 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6648 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6650 #ifndef TEXT_END_LABEL
6651 #define TEXT_END_LABEL "Letext"
6652 #endif
6653 #ifndef COLD_END_LABEL
6654 #define COLD_END_LABEL "Letext_cold"
6655 #endif
6656 #ifndef BLOCK_BEGIN_LABEL
6657 #define BLOCK_BEGIN_LABEL "LBB"
6658 #endif
6659 #ifndef BLOCK_END_LABEL
6660 #define BLOCK_END_LABEL "LBE"
6661 #endif
6662 #ifndef LINE_CODE_LABEL
6663 #define LINE_CODE_LABEL "LM"
6664 #endif
6665 #ifndef SEPARATE_LINE_CODE_LABEL
6666 #define SEPARATE_LINE_CODE_LABEL "LSM"
6667 #endif
6670 /* Return the root of the DIE's built for the current compilation unit. */
6671 static dw_die_ref
6672 comp_unit_die (void)
6674 if (!single_comp_unit_die)
6675 single_comp_unit_die = gen_compile_unit_die (NULL);
6676 return single_comp_unit_die;
6679 /* We allow a language front-end to designate a function that is to be
6680 called to "demangle" any name before it is put into a DIE. */
6682 static const char *(*demangle_name_func) (const char *);
6684 void
6685 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6687 demangle_name_func = func;
6690 /* Test if rtl node points to a pseudo register. */
6692 static inline int
6693 is_pseudo_reg (const_rtx rtl)
6695 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6696 || (GET_CODE (rtl) == SUBREG
6697 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6700 /* Return a reference to a type, with its const and volatile qualifiers
6701 removed. */
6703 static inline tree
6704 type_main_variant (tree type)
6706 type = TYPE_MAIN_VARIANT (type);
6708 /* ??? There really should be only one main variant among any group of
6709 variants of a given type (and all of the MAIN_VARIANT values for all
6710 members of the group should point to that one type) but sometimes the C
6711 front-end messes this up for array types, so we work around that bug
6712 here. */
6713 if (TREE_CODE (type) == ARRAY_TYPE)
6714 while (type != TYPE_MAIN_VARIANT (type))
6715 type = TYPE_MAIN_VARIANT (type);
6717 return type;
6720 /* Return nonzero if the given type node represents a tagged type. */
6722 static inline int
6723 is_tagged_type (const_tree type)
6725 enum tree_code code = TREE_CODE (type);
6727 return (code == RECORD_TYPE || code == UNION_TYPE
6728 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6731 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
6733 static void
6734 get_ref_die_offset_label (char *label, dw_die_ref ref)
6736 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6739 /* Convert a DIE tag into its string name. */
6741 static const char *
6742 dwarf_tag_name (unsigned int tag)
6744 switch (tag)
6746 case DW_TAG_padding:
6747 return "DW_TAG_padding";
6748 case DW_TAG_array_type:
6749 return "DW_TAG_array_type";
6750 case DW_TAG_class_type:
6751 return "DW_TAG_class_type";
6752 case DW_TAG_entry_point:
6753 return "DW_TAG_entry_point";
6754 case DW_TAG_enumeration_type:
6755 return "DW_TAG_enumeration_type";
6756 case DW_TAG_formal_parameter:
6757 return "DW_TAG_formal_parameter";
6758 case DW_TAG_imported_declaration:
6759 return "DW_TAG_imported_declaration";
6760 case DW_TAG_label:
6761 return "DW_TAG_label";
6762 case DW_TAG_lexical_block:
6763 return "DW_TAG_lexical_block";
6764 case DW_TAG_member:
6765 return "DW_TAG_member";
6766 case DW_TAG_pointer_type:
6767 return "DW_TAG_pointer_type";
6768 case DW_TAG_reference_type:
6769 return "DW_TAG_reference_type";
6770 case DW_TAG_compile_unit:
6771 return "DW_TAG_compile_unit";
6772 case DW_TAG_string_type:
6773 return "DW_TAG_string_type";
6774 case DW_TAG_structure_type:
6775 return "DW_TAG_structure_type";
6776 case DW_TAG_subroutine_type:
6777 return "DW_TAG_subroutine_type";
6778 case DW_TAG_typedef:
6779 return "DW_TAG_typedef";
6780 case DW_TAG_union_type:
6781 return "DW_TAG_union_type";
6782 case DW_TAG_unspecified_parameters:
6783 return "DW_TAG_unspecified_parameters";
6784 case DW_TAG_variant:
6785 return "DW_TAG_variant";
6786 case DW_TAG_common_block:
6787 return "DW_TAG_common_block";
6788 case DW_TAG_common_inclusion:
6789 return "DW_TAG_common_inclusion";
6790 case DW_TAG_inheritance:
6791 return "DW_TAG_inheritance";
6792 case DW_TAG_inlined_subroutine:
6793 return "DW_TAG_inlined_subroutine";
6794 case DW_TAG_module:
6795 return "DW_TAG_module";
6796 case DW_TAG_ptr_to_member_type:
6797 return "DW_TAG_ptr_to_member_type";
6798 case DW_TAG_set_type:
6799 return "DW_TAG_set_type";
6800 case DW_TAG_subrange_type:
6801 return "DW_TAG_subrange_type";
6802 case DW_TAG_with_stmt:
6803 return "DW_TAG_with_stmt";
6804 case DW_TAG_access_declaration:
6805 return "DW_TAG_access_declaration";
6806 case DW_TAG_base_type:
6807 return "DW_TAG_base_type";
6808 case DW_TAG_catch_block:
6809 return "DW_TAG_catch_block";
6810 case DW_TAG_const_type:
6811 return "DW_TAG_const_type";
6812 case DW_TAG_constant:
6813 return "DW_TAG_constant";
6814 case DW_TAG_enumerator:
6815 return "DW_TAG_enumerator";
6816 case DW_TAG_file_type:
6817 return "DW_TAG_file_type";
6818 case DW_TAG_friend:
6819 return "DW_TAG_friend";
6820 case DW_TAG_namelist:
6821 return "DW_TAG_namelist";
6822 case DW_TAG_namelist_item:
6823 return "DW_TAG_namelist_item";
6824 case DW_TAG_packed_type:
6825 return "DW_TAG_packed_type";
6826 case DW_TAG_subprogram:
6827 return "DW_TAG_subprogram";
6828 case DW_TAG_template_type_param:
6829 return "DW_TAG_template_type_param";
6830 case DW_TAG_template_value_param:
6831 return "DW_TAG_template_value_param";
6832 case DW_TAG_thrown_type:
6833 return "DW_TAG_thrown_type";
6834 case DW_TAG_try_block:
6835 return "DW_TAG_try_block";
6836 case DW_TAG_variant_part:
6837 return "DW_TAG_variant_part";
6838 case DW_TAG_variable:
6839 return "DW_TAG_variable";
6840 case DW_TAG_volatile_type:
6841 return "DW_TAG_volatile_type";
6842 case DW_TAG_dwarf_procedure:
6843 return "DW_TAG_dwarf_procedure";
6844 case DW_TAG_restrict_type:
6845 return "DW_TAG_restrict_type";
6846 case DW_TAG_interface_type:
6847 return "DW_TAG_interface_type";
6848 case DW_TAG_namespace:
6849 return "DW_TAG_namespace";
6850 case DW_TAG_imported_module:
6851 return "DW_TAG_imported_module";
6852 case DW_TAG_unspecified_type:
6853 return "DW_TAG_unspecified_type";
6854 case DW_TAG_partial_unit:
6855 return "DW_TAG_partial_unit";
6856 case DW_TAG_imported_unit:
6857 return "DW_TAG_imported_unit";
6858 case DW_TAG_condition:
6859 return "DW_TAG_condition";
6860 case DW_TAG_shared_type:
6861 return "DW_TAG_shared_type";
6862 case DW_TAG_type_unit:
6863 return "DW_TAG_type_unit";
6864 case DW_TAG_rvalue_reference_type:
6865 return "DW_TAG_rvalue_reference_type";
6866 case DW_TAG_template_alias:
6867 return "DW_TAG_template_alias";
6868 case DW_TAG_GNU_template_parameter_pack:
6869 return "DW_TAG_GNU_template_parameter_pack";
6870 case DW_TAG_GNU_formal_parameter_pack:
6871 return "DW_TAG_GNU_formal_parameter_pack";
6872 case DW_TAG_MIPS_loop:
6873 return "DW_TAG_MIPS_loop";
6874 case DW_TAG_format_label:
6875 return "DW_TAG_format_label";
6876 case DW_TAG_function_template:
6877 return "DW_TAG_function_template";
6878 case DW_TAG_class_template:
6879 return "DW_TAG_class_template";
6880 case DW_TAG_GNU_BINCL:
6881 return "DW_TAG_GNU_BINCL";
6882 case DW_TAG_GNU_EINCL:
6883 return "DW_TAG_GNU_EINCL";
6884 case DW_TAG_GNU_template_template_param:
6885 return "DW_TAG_GNU_template_template_param";
6886 case DW_TAG_GNU_call_site:
6887 return "DW_TAG_GNU_call_site";
6888 case DW_TAG_GNU_call_site_parameter:
6889 return "DW_TAG_GNU_call_site_parameter";
6890 default:
6891 return "DW_TAG_<unknown>";
6895 /* Convert a DWARF attribute code into its string name. */
6897 static const char *
6898 dwarf_attr_name (unsigned int attr)
6900 switch (attr)
6902 case DW_AT_sibling:
6903 return "DW_AT_sibling";
6904 case DW_AT_location:
6905 return "DW_AT_location";
6906 case DW_AT_name:
6907 return "DW_AT_name";
6908 case DW_AT_ordering:
6909 return "DW_AT_ordering";
6910 case DW_AT_subscr_data:
6911 return "DW_AT_subscr_data";
6912 case DW_AT_byte_size:
6913 return "DW_AT_byte_size";
6914 case DW_AT_bit_offset:
6915 return "DW_AT_bit_offset";
6916 case DW_AT_bit_size:
6917 return "DW_AT_bit_size";
6918 case DW_AT_element_list:
6919 return "DW_AT_element_list";
6920 case DW_AT_stmt_list:
6921 return "DW_AT_stmt_list";
6922 case DW_AT_low_pc:
6923 return "DW_AT_low_pc";
6924 case DW_AT_high_pc:
6925 return "DW_AT_high_pc";
6926 case DW_AT_language:
6927 return "DW_AT_language";
6928 case DW_AT_member:
6929 return "DW_AT_member";
6930 case DW_AT_discr:
6931 return "DW_AT_discr";
6932 case DW_AT_discr_value:
6933 return "DW_AT_discr_value";
6934 case DW_AT_visibility:
6935 return "DW_AT_visibility";
6936 case DW_AT_import:
6937 return "DW_AT_import";
6938 case DW_AT_string_length:
6939 return "DW_AT_string_length";
6940 case DW_AT_common_reference:
6941 return "DW_AT_common_reference";
6942 case DW_AT_comp_dir:
6943 return "DW_AT_comp_dir";
6944 case DW_AT_const_value:
6945 return "DW_AT_const_value";
6946 case DW_AT_containing_type:
6947 return "DW_AT_containing_type";
6948 case DW_AT_default_value:
6949 return "DW_AT_default_value";
6950 case DW_AT_inline:
6951 return "DW_AT_inline";
6952 case DW_AT_is_optional:
6953 return "DW_AT_is_optional";
6954 case DW_AT_lower_bound:
6955 return "DW_AT_lower_bound";
6956 case DW_AT_producer:
6957 return "DW_AT_producer";
6958 case DW_AT_prototyped:
6959 return "DW_AT_prototyped";
6960 case DW_AT_return_addr:
6961 return "DW_AT_return_addr";
6962 case DW_AT_start_scope:
6963 return "DW_AT_start_scope";
6964 case DW_AT_bit_stride:
6965 return "DW_AT_bit_stride";
6966 case DW_AT_upper_bound:
6967 return "DW_AT_upper_bound";
6968 case DW_AT_abstract_origin:
6969 return "DW_AT_abstract_origin";
6970 case DW_AT_accessibility:
6971 return "DW_AT_accessibility";
6972 case DW_AT_address_class:
6973 return "DW_AT_address_class";
6974 case DW_AT_artificial:
6975 return "DW_AT_artificial";
6976 case DW_AT_base_types:
6977 return "DW_AT_base_types";
6978 case DW_AT_calling_convention:
6979 return "DW_AT_calling_convention";
6980 case DW_AT_count:
6981 return "DW_AT_count";
6982 case DW_AT_data_member_location:
6983 return "DW_AT_data_member_location";
6984 case DW_AT_decl_column:
6985 return "DW_AT_decl_column";
6986 case DW_AT_decl_file:
6987 return "DW_AT_decl_file";
6988 case DW_AT_decl_line:
6989 return "DW_AT_decl_line";
6990 case DW_AT_declaration:
6991 return "DW_AT_declaration";
6992 case DW_AT_discr_list:
6993 return "DW_AT_discr_list";
6994 case DW_AT_encoding:
6995 return "DW_AT_encoding";
6996 case DW_AT_external:
6997 return "DW_AT_external";
6998 case DW_AT_explicit:
6999 return "DW_AT_explicit";
7000 case DW_AT_frame_base:
7001 return "DW_AT_frame_base";
7002 case DW_AT_friend:
7003 return "DW_AT_friend";
7004 case DW_AT_identifier_case:
7005 return "DW_AT_identifier_case";
7006 case DW_AT_macro_info:
7007 return "DW_AT_macro_info";
7008 case DW_AT_namelist_items:
7009 return "DW_AT_namelist_items";
7010 case DW_AT_priority:
7011 return "DW_AT_priority";
7012 case DW_AT_segment:
7013 return "DW_AT_segment";
7014 case DW_AT_specification:
7015 return "DW_AT_specification";
7016 case DW_AT_static_link:
7017 return "DW_AT_static_link";
7018 case DW_AT_type:
7019 return "DW_AT_type";
7020 case DW_AT_use_location:
7021 return "DW_AT_use_location";
7022 case DW_AT_variable_parameter:
7023 return "DW_AT_variable_parameter";
7024 case DW_AT_virtuality:
7025 return "DW_AT_virtuality";
7026 case DW_AT_vtable_elem_location:
7027 return "DW_AT_vtable_elem_location";
7029 case DW_AT_allocated:
7030 return "DW_AT_allocated";
7031 case DW_AT_associated:
7032 return "DW_AT_associated";
7033 case DW_AT_data_location:
7034 return "DW_AT_data_location";
7035 case DW_AT_byte_stride:
7036 return "DW_AT_byte_stride";
7037 case DW_AT_entry_pc:
7038 return "DW_AT_entry_pc";
7039 case DW_AT_use_UTF8:
7040 return "DW_AT_use_UTF8";
7041 case DW_AT_extension:
7042 return "DW_AT_extension";
7043 case DW_AT_ranges:
7044 return "DW_AT_ranges";
7045 case DW_AT_trampoline:
7046 return "DW_AT_trampoline";
7047 case DW_AT_call_column:
7048 return "DW_AT_call_column";
7049 case DW_AT_call_file:
7050 return "DW_AT_call_file";
7051 case DW_AT_call_line:
7052 return "DW_AT_call_line";
7053 case DW_AT_object_pointer:
7054 return "DW_AT_object_pointer";
7056 case DW_AT_signature:
7057 return "DW_AT_signature";
7058 case DW_AT_main_subprogram:
7059 return "DW_AT_main_subprogram";
7060 case DW_AT_data_bit_offset:
7061 return "DW_AT_data_bit_offset";
7062 case DW_AT_const_expr:
7063 return "DW_AT_const_expr";
7064 case DW_AT_enum_class:
7065 return "DW_AT_enum_class";
7066 case DW_AT_linkage_name:
7067 return "DW_AT_linkage_name";
7069 case DW_AT_MIPS_fde:
7070 return "DW_AT_MIPS_fde";
7071 case DW_AT_MIPS_loop_begin:
7072 return "DW_AT_MIPS_loop_begin";
7073 case DW_AT_MIPS_tail_loop_begin:
7074 return "DW_AT_MIPS_tail_loop_begin";
7075 case DW_AT_MIPS_epilog_begin:
7076 return "DW_AT_MIPS_epilog_begin";
7077 #if VMS_DEBUGGING_INFO
7078 case DW_AT_HP_prologue:
7079 return "DW_AT_HP_prologue";
7080 #else
7081 case DW_AT_MIPS_loop_unroll_factor:
7082 return "DW_AT_MIPS_loop_unroll_factor";
7083 #endif
7084 case DW_AT_MIPS_software_pipeline_depth:
7085 return "DW_AT_MIPS_software_pipeline_depth";
7086 case DW_AT_MIPS_linkage_name:
7087 return "DW_AT_MIPS_linkage_name";
7088 #if VMS_DEBUGGING_INFO
7089 case DW_AT_HP_epilogue:
7090 return "DW_AT_HP_epilogue";
7091 #else
7092 case DW_AT_MIPS_stride:
7093 return "DW_AT_MIPS_stride";
7094 #endif
7095 case DW_AT_MIPS_abstract_name:
7096 return "DW_AT_MIPS_abstract_name";
7097 case DW_AT_MIPS_clone_origin:
7098 return "DW_AT_MIPS_clone_origin";
7099 case DW_AT_MIPS_has_inlines:
7100 return "DW_AT_MIPS_has_inlines";
7102 case DW_AT_sf_names:
7103 return "DW_AT_sf_names";
7104 case DW_AT_src_info:
7105 return "DW_AT_src_info";
7106 case DW_AT_mac_info:
7107 return "DW_AT_mac_info";
7108 case DW_AT_src_coords:
7109 return "DW_AT_src_coords";
7110 case DW_AT_body_begin:
7111 return "DW_AT_body_begin";
7112 case DW_AT_body_end:
7113 return "DW_AT_body_end";
7114 case DW_AT_GNU_vector:
7115 return "DW_AT_GNU_vector";
7116 case DW_AT_GNU_guarded_by:
7117 return "DW_AT_GNU_guarded_by";
7118 case DW_AT_GNU_pt_guarded_by:
7119 return "DW_AT_GNU_pt_guarded_by";
7120 case DW_AT_GNU_guarded:
7121 return "DW_AT_GNU_guarded";
7122 case DW_AT_GNU_pt_guarded:
7123 return "DW_AT_GNU_pt_guarded";
7124 case DW_AT_GNU_locks_excluded:
7125 return "DW_AT_GNU_locks_excluded";
7126 case DW_AT_GNU_exclusive_locks_required:
7127 return "DW_AT_GNU_exclusive_locks_required";
7128 case DW_AT_GNU_shared_locks_required:
7129 return "DW_AT_GNU_shared_locks_required";
7130 case DW_AT_GNU_odr_signature:
7131 return "DW_AT_GNU_odr_signature";
7132 case DW_AT_GNU_template_name:
7133 return "DW_AT_GNU_template_name";
7134 case DW_AT_GNU_call_site_value:
7135 return "DW_AT_GNU_call_site_value";
7136 case DW_AT_GNU_call_site_data_value:
7137 return "DW_AT_GNU_call_site_data_value";
7138 case DW_AT_GNU_call_site_target:
7139 return "DW_AT_GNU_call_site_target";
7140 case DW_AT_GNU_call_site_target_clobbered:
7141 return "DW_AT_GNU_call_site_target_clobbered";
7142 case DW_AT_GNU_tail_call:
7143 return "DW_AT_GNU_tail_call";
7144 case DW_AT_GNU_all_tail_call_sites:
7145 return "DW_AT_GNU_all_tail_call_sites";
7146 case DW_AT_GNU_all_call_sites:
7147 return "DW_AT_GNU_all_call_sites";
7148 case DW_AT_GNU_all_source_call_sites:
7149 return "DW_AT_GNU_all_source_call_sites";
7151 case DW_AT_VMS_rtnbeg_pd_address:
7152 return "DW_AT_VMS_rtnbeg_pd_address";
7154 default:
7155 return "DW_AT_<unknown>";
7159 /* Convert a DWARF value form code into its string name. */
7161 static const char *
7162 dwarf_form_name (unsigned int form)
7164 switch (form)
7166 case DW_FORM_addr:
7167 return "DW_FORM_addr";
7168 case DW_FORM_block2:
7169 return "DW_FORM_block2";
7170 case DW_FORM_block4:
7171 return "DW_FORM_block4";
7172 case DW_FORM_data2:
7173 return "DW_FORM_data2";
7174 case DW_FORM_data4:
7175 return "DW_FORM_data4";
7176 case DW_FORM_data8:
7177 return "DW_FORM_data8";
7178 case DW_FORM_string:
7179 return "DW_FORM_string";
7180 case DW_FORM_block:
7181 return "DW_FORM_block";
7182 case DW_FORM_block1:
7183 return "DW_FORM_block1";
7184 case DW_FORM_data1:
7185 return "DW_FORM_data1";
7186 case DW_FORM_flag:
7187 return "DW_FORM_flag";
7188 case DW_FORM_sdata:
7189 return "DW_FORM_sdata";
7190 case DW_FORM_strp:
7191 return "DW_FORM_strp";
7192 case DW_FORM_udata:
7193 return "DW_FORM_udata";
7194 case DW_FORM_ref_addr:
7195 return "DW_FORM_ref_addr";
7196 case DW_FORM_ref1:
7197 return "DW_FORM_ref1";
7198 case DW_FORM_ref2:
7199 return "DW_FORM_ref2";
7200 case DW_FORM_ref4:
7201 return "DW_FORM_ref4";
7202 case DW_FORM_ref8:
7203 return "DW_FORM_ref8";
7204 case DW_FORM_ref_udata:
7205 return "DW_FORM_ref_udata";
7206 case DW_FORM_indirect:
7207 return "DW_FORM_indirect";
7208 case DW_FORM_sec_offset:
7209 return "DW_FORM_sec_offset";
7210 case DW_FORM_exprloc:
7211 return "DW_FORM_exprloc";
7212 case DW_FORM_flag_present:
7213 return "DW_FORM_flag_present";
7214 case DW_FORM_ref_sig8:
7215 return "DW_FORM_ref_sig8";
7216 default:
7217 return "DW_FORM_<unknown>";
7221 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
7222 instance of an inlined instance of a decl which is local to an inline
7223 function, so we have to trace all of the way back through the origin chain
7224 to find out what sort of node actually served as the original seed for the
7225 given block. */
7227 static tree
7228 decl_ultimate_origin (const_tree decl)
7230 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7231 return NULL_TREE;
7233 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7234 nodes in the function to point to themselves; ignore that if
7235 we're trying to output the abstract instance of this function. */
7236 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7237 return NULL_TREE;
7239 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7240 most distant ancestor, this should never happen. */
7241 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7243 return DECL_ABSTRACT_ORIGIN (decl);
7246 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
7247 of a virtual function may refer to a base class, so we check the 'this'
7248 parameter. */
7250 static tree
7251 decl_class_context (tree decl)
7253 tree context = NULL_TREE;
7255 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7256 context = DECL_CONTEXT (decl);
7257 else
7258 context = TYPE_MAIN_VARIANT
7259 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7261 if (context && !TYPE_P (context))
7262 context = NULL_TREE;
7264 return context;
7267 /* Add an attribute/value pair to a DIE. */
7269 static inline void
7270 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7272 /* Maybe this should be an assert? */
7273 if (die == NULL)
7274 return;
7276 if (die->die_attr == NULL)
7277 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7278 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7281 static inline enum dw_val_class
7282 AT_class (dw_attr_ref a)
7284 return a->dw_attr_val.val_class;
7287 /* Add a flag value attribute to a DIE. */
7289 static inline void
7290 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7292 dw_attr_node attr;
7294 attr.dw_attr = attr_kind;
7295 attr.dw_attr_val.val_class = dw_val_class_flag;
7296 attr.dw_attr_val.v.val_flag = flag;
7297 add_dwarf_attr (die, &attr);
7300 static inline unsigned
7301 AT_flag (dw_attr_ref a)
7303 gcc_assert (a && AT_class (a) == dw_val_class_flag);
7304 return a->dw_attr_val.v.val_flag;
7307 /* Add a signed integer attribute value to a DIE. */
7309 static inline void
7310 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7312 dw_attr_node attr;
7314 attr.dw_attr = attr_kind;
7315 attr.dw_attr_val.val_class = dw_val_class_const;
7316 attr.dw_attr_val.v.val_int = int_val;
7317 add_dwarf_attr (die, &attr);
7320 static inline HOST_WIDE_INT
7321 AT_int (dw_attr_ref a)
7323 gcc_assert (a && AT_class (a) == dw_val_class_const);
7324 return a->dw_attr_val.v.val_int;
7327 /* Add an unsigned integer attribute value to a DIE. */
7329 static inline void
7330 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7331 unsigned HOST_WIDE_INT unsigned_val)
7333 dw_attr_node attr;
7335 attr.dw_attr = attr_kind;
7336 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7337 attr.dw_attr_val.v.val_unsigned = unsigned_val;
7338 add_dwarf_attr (die, &attr);
7341 static inline unsigned HOST_WIDE_INT
7342 AT_unsigned (dw_attr_ref a)
7344 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7345 return a->dw_attr_val.v.val_unsigned;
7348 /* Add an unsigned double integer attribute value to a DIE. */
7350 static inline void
7351 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7352 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7354 dw_attr_node attr;
7356 attr.dw_attr = attr_kind;
7357 attr.dw_attr_val.val_class = dw_val_class_const_double;
7358 attr.dw_attr_val.v.val_double.high = high;
7359 attr.dw_attr_val.v.val_double.low = low;
7360 add_dwarf_attr (die, &attr);
7363 /* Add a floating point attribute value to a DIE and return it. */
7365 static inline void
7366 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7367 unsigned int length, unsigned int elt_size, unsigned char *array)
7369 dw_attr_node attr;
7371 attr.dw_attr = attr_kind;
7372 attr.dw_attr_val.val_class = dw_val_class_vec;
7373 attr.dw_attr_val.v.val_vec.length = length;
7374 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7375 attr.dw_attr_val.v.val_vec.array = array;
7376 add_dwarf_attr (die, &attr);
7379 /* Add an 8-byte data attribute value to a DIE. */
7381 static inline void
7382 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7383 unsigned char data8[8])
7385 dw_attr_node attr;
7387 attr.dw_attr = attr_kind;
7388 attr.dw_attr_val.val_class = dw_val_class_data8;
7389 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7390 add_dwarf_attr (die, &attr);
7393 /* Hash and equality functions for debug_str_hash. */
7395 static hashval_t
7396 debug_str_do_hash (const void *x)
7398 return htab_hash_string (((const struct indirect_string_node *)x)->str);
7401 static int
7402 debug_str_eq (const void *x1, const void *x2)
7404 return strcmp ((((const struct indirect_string_node *)x1)->str),
7405 (const char *)x2) == 0;
7408 /* Add STR to the indirect string hash table. */
7410 static struct indirect_string_node *
7411 find_AT_string (const char *str)
7413 struct indirect_string_node *node;
7414 void **slot;
7416 if (! debug_str_hash)
7417 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7418 debug_str_eq, NULL);
7420 slot = htab_find_slot_with_hash (debug_str_hash, str,
7421 htab_hash_string (str), INSERT);
7422 if (*slot == NULL)
7424 node = ggc_alloc_cleared_indirect_string_node ();
7425 node->str = ggc_strdup (str);
7426 *slot = node;
7428 else
7429 node = (struct indirect_string_node *) *slot;
7431 node->refcount++;
7432 return node;
7435 /* Add a string attribute value to a DIE. */
7437 static inline void
7438 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7440 dw_attr_node attr;
7441 struct indirect_string_node *node;
7443 node = find_AT_string (str);
7445 attr.dw_attr = attr_kind;
7446 attr.dw_attr_val.val_class = dw_val_class_str;
7447 attr.dw_attr_val.v.val_str = node;
7448 add_dwarf_attr (die, &attr);
7451 /* Create a label for an indirect string node, ensuring it is going to
7452 be output, unless its reference count goes down to zero. */
7454 static inline void
7455 gen_label_for_indirect_string (struct indirect_string_node *node)
7457 char label[32];
7459 if (node->label)
7460 return;
7462 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7463 ++dw2_string_counter;
7464 node->label = xstrdup (label);
7467 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7468 debug string STR. */
7470 static inline rtx
7471 get_debug_string_label (const char *str)
7473 struct indirect_string_node *node = find_AT_string (str);
7475 debug_str_hash_forced = true;
7477 gen_label_for_indirect_string (node);
7479 return gen_rtx_SYMBOL_REF (Pmode, node->label);
7482 static inline const char *
7483 AT_string (dw_attr_ref a)
7485 gcc_assert (a && AT_class (a) == dw_val_class_str);
7486 return a->dw_attr_val.v.val_str->str;
7489 /* Find out whether a string should be output inline in DIE
7490 or out-of-line in .debug_str section. */
7492 static enum dwarf_form
7493 AT_string_form (dw_attr_ref a)
7495 struct indirect_string_node *node;
7496 unsigned int len;
7498 gcc_assert (a && AT_class (a) == dw_val_class_str);
7500 node = a->dw_attr_val.v.val_str;
7501 if (node->form)
7502 return node->form;
7504 len = strlen (node->str) + 1;
7506 /* If the string is shorter or equal to the size of the reference, it is
7507 always better to put it inline. */
7508 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7509 return node->form = DW_FORM_string;
7511 /* If we cannot expect the linker to merge strings in .debug_str
7512 section, only put it into .debug_str if it is worth even in this
7513 single module. */
7514 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7515 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7516 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7517 return node->form = DW_FORM_string;
7519 gen_label_for_indirect_string (node);
7521 return node->form = DW_FORM_strp;
7524 /* Add a DIE reference attribute value to a DIE. */
7526 static inline void
7527 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7529 dw_attr_node attr;
7531 #ifdef ENABLE_CHECKING
7532 gcc_assert (targ_die != NULL);
7533 #else
7534 /* With LTO we can end up trying to reference something we didn't create
7535 a DIE for. Avoid crashing later on a NULL referenced DIE. */
7536 if (targ_die == NULL)
7537 return;
7538 #endif
7540 attr.dw_attr = attr_kind;
7541 attr.dw_attr_val.val_class = dw_val_class_die_ref;
7542 attr.dw_attr_val.v.val_die_ref.die = targ_die;
7543 attr.dw_attr_val.v.val_die_ref.external = 0;
7544 add_dwarf_attr (die, &attr);
7547 /* Add an AT_specification attribute to a DIE, and also make the back
7548 pointer from the specification to the definition. */
7550 static inline void
7551 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7553 add_AT_die_ref (die, DW_AT_specification, targ_die);
7554 gcc_assert (!targ_die->die_definition);
7555 targ_die->die_definition = die;
7558 static inline dw_die_ref
7559 AT_ref (dw_attr_ref a)
7561 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7562 return a->dw_attr_val.v.val_die_ref.die;
7565 static inline int
7566 AT_ref_external (dw_attr_ref a)
7568 if (a && AT_class (a) == dw_val_class_die_ref)
7569 return a->dw_attr_val.v.val_die_ref.external;
7571 return 0;
7574 static inline void
7575 set_AT_ref_external (dw_attr_ref a, int i)
7577 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7578 a->dw_attr_val.v.val_die_ref.external = i;
7581 /* Add an FDE reference attribute value to a DIE. */
7583 static inline void
7584 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7586 dw_attr_node attr;
7588 attr.dw_attr = attr_kind;
7589 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7590 attr.dw_attr_val.v.val_fde_index = targ_fde;
7591 add_dwarf_attr (die, &attr);
7594 /* Add a location description attribute value to a DIE. */
7596 static inline void
7597 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7599 dw_attr_node attr;
7601 attr.dw_attr = attr_kind;
7602 attr.dw_attr_val.val_class = dw_val_class_loc;
7603 attr.dw_attr_val.v.val_loc = loc;
7604 add_dwarf_attr (die, &attr);
7607 static inline dw_loc_descr_ref
7608 AT_loc (dw_attr_ref a)
7610 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7611 return a->dw_attr_val.v.val_loc;
7614 static inline void
7615 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7617 dw_attr_node attr;
7619 attr.dw_attr = attr_kind;
7620 attr.dw_attr_val.val_class = dw_val_class_loc_list;
7621 attr.dw_attr_val.v.val_loc_list = loc_list;
7622 add_dwarf_attr (die, &attr);
7623 have_location_lists = true;
7626 static inline dw_loc_list_ref
7627 AT_loc_list (dw_attr_ref a)
7629 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7630 return a->dw_attr_val.v.val_loc_list;
7633 static inline dw_loc_list_ref *
7634 AT_loc_list_ptr (dw_attr_ref a)
7636 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7637 return &a->dw_attr_val.v.val_loc_list;
7640 /* Add an address constant attribute value to a DIE. */
7642 static inline void
7643 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7645 dw_attr_node attr;
7647 attr.dw_attr = attr_kind;
7648 attr.dw_attr_val.val_class = dw_val_class_addr;
7649 attr.dw_attr_val.v.val_addr = addr;
7650 add_dwarf_attr (die, &attr);
7653 /* Get the RTX from to an address DIE attribute. */
7655 static inline rtx
7656 AT_addr (dw_attr_ref a)
7658 gcc_assert (a && AT_class (a) == dw_val_class_addr);
7659 return a->dw_attr_val.v.val_addr;
7662 /* Add a file attribute value to a DIE. */
7664 static inline void
7665 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7666 struct dwarf_file_data *fd)
7668 dw_attr_node attr;
7670 attr.dw_attr = attr_kind;
7671 attr.dw_attr_val.val_class = dw_val_class_file;
7672 attr.dw_attr_val.v.val_file = fd;
7673 add_dwarf_attr (die, &attr);
7676 /* Get the dwarf_file_data from a file DIE attribute. */
7678 static inline struct dwarf_file_data *
7679 AT_file (dw_attr_ref a)
7681 gcc_assert (a && AT_class (a) == dw_val_class_file);
7682 return a->dw_attr_val.v.val_file;
7685 /* Add a vms delta attribute value to a DIE. */
7687 static inline void
7688 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7689 const char *lbl1, const char *lbl2)
7691 dw_attr_node attr;
7693 attr.dw_attr = attr_kind;
7694 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7695 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7696 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7697 add_dwarf_attr (die, &attr);
7700 /* Add a label identifier attribute value to a DIE. */
7702 static inline void
7703 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7705 dw_attr_node attr;
7707 attr.dw_attr = attr_kind;
7708 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7709 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7710 add_dwarf_attr (die, &attr);
7713 /* Add a section offset attribute value to a DIE, an offset into the
7714 debug_line section. */
7716 static inline void
7717 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7718 const char *label)
7720 dw_attr_node attr;
7722 attr.dw_attr = attr_kind;
7723 attr.dw_attr_val.val_class = dw_val_class_lineptr;
7724 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7725 add_dwarf_attr (die, &attr);
7728 /* Add a section offset attribute value to a DIE, an offset into the
7729 debug_macinfo section. */
7731 static inline void
7732 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7733 const char *label)
7735 dw_attr_node attr;
7737 attr.dw_attr = attr_kind;
7738 attr.dw_attr_val.val_class = dw_val_class_macptr;
7739 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7740 add_dwarf_attr (die, &attr);
7743 /* Add an offset attribute value to a DIE. */
7745 static inline void
7746 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7747 unsigned HOST_WIDE_INT offset)
7749 dw_attr_node attr;
7751 attr.dw_attr = attr_kind;
7752 attr.dw_attr_val.val_class = dw_val_class_offset;
7753 attr.dw_attr_val.v.val_offset = offset;
7754 add_dwarf_attr (die, &attr);
7757 /* Add an range_list attribute value to a DIE. */
7759 static void
7760 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7761 long unsigned int offset)
7763 dw_attr_node attr;
7765 attr.dw_attr = attr_kind;
7766 attr.dw_attr_val.val_class = dw_val_class_range_list;
7767 attr.dw_attr_val.v.val_offset = offset;
7768 add_dwarf_attr (die, &attr);
7771 /* Return the start label of a delta attribute. */
7773 static inline const char *
7774 AT_vms_delta1 (dw_attr_ref a)
7776 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7777 return a->dw_attr_val.v.val_vms_delta.lbl1;
7780 /* Return the end label of a delta attribute. */
7782 static inline const char *
7783 AT_vms_delta2 (dw_attr_ref a)
7785 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7786 return a->dw_attr_val.v.val_vms_delta.lbl2;
7789 static inline const char *
7790 AT_lbl (dw_attr_ref a)
7792 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7793 || AT_class (a) == dw_val_class_lineptr
7794 || AT_class (a) == dw_val_class_macptr));
7795 return a->dw_attr_val.v.val_lbl_id;
7798 /* Get the attribute of type attr_kind. */
7800 static dw_attr_ref
7801 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7803 dw_attr_ref a;
7804 unsigned ix;
7805 dw_die_ref spec = NULL;
7807 if (! die)
7808 return NULL;
7810 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7811 if (a->dw_attr == attr_kind)
7812 return a;
7813 else if (a->dw_attr == DW_AT_specification
7814 || a->dw_attr == DW_AT_abstract_origin)
7815 spec = AT_ref (a);
7817 if (spec)
7818 return get_AT (spec, attr_kind);
7820 return NULL;
7823 /* Return the "low pc" attribute value, typically associated with a subprogram
7824 DIE. Return null if the "low pc" attribute is either not present, or if it
7825 cannot be represented as an assembler label identifier. */
7827 static inline const char *
7828 get_AT_low_pc (dw_die_ref die)
7830 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7832 return a ? AT_lbl (a) : NULL;
7835 /* Return the "high pc" attribute value, typically associated with a subprogram
7836 DIE. Return null if the "high pc" attribute is either not present, or if it
7837 cannot be represented as an assembler label identifier. */
7839 static inline const char *
7840 get_AT_hi_pc (dw_die_ref die)
7842 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7844 return a ? AT_lbl (a) : NULL;
7847 /* Return the value of the string attribute designated by ATTR_KIND, or
7848 NULL if it is not present. */
7850 static inline const char *
7851 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7853 dw_attr_ref a = get_AT (die, attr_kind);
7855 return a ? AT_string (a) : NULL;
7858 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7859 if it is not present. */
7861 static inline int
7862 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7864 dw_attr_ref a = get_AT (die, attr_kind);
7866 return a ? AT_flag (a) : 0;
7869 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7870 if it is not present. */
7872 static inline unsigned
7873 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7875 dw_attr_ref a = get_AT (die, attr_kind);
7877 return a ? AT_unsigned (a) : 0;
7880 static inline dw_die_ref
7881 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7883 dw_attr_ref a = get_AT (die, attr_kind);
7885 return a ? AT_ref (a) : NULL;
7888 static inline struct dwarf_file_data *
7889 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7891 dw_attr_ref a = get_AT (die, attr_kind);
7893 return a ? AT_file (a) : NULL;
7896 /* Return TRUE if the language is C++. */
7898 static inline bool
7899 is_cxx (void)
7901 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7903 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7906 /* Return TRUE if the language is Fortran. */
7908 static inline bool
7909 is_fortran (void)
7911 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7913 return (lang == DW_LANG_Fortran77
7914 || lang == DW_LANG_Fortran90
7915 || lang == DW_LANG_Fortran95);
7918 /* Return TRUE if the language is Ada. */
7920 static inline bool
7921 is_ada (void)
7923 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7925 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7928 /* Remove the specified attribute if present. */
7930 static void
7931 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7933 dw_attr_ref a;
7934 unsigned ix;
7936 if (! die)
7937 return;
7939 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7940 if (a->dw_attr == attr_kind)
7942 if (AT_class (a) == dw_val_class_str)
7943 if (a->dw_attr_val.v.val_str->refcount)
7944 a->dw_attr_val.v.val_str->refcount--;
7946 /* VEC_ordered_remove should help reduce the number of abbrevs
7947 that are needed. */
7948 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7949 return;
7953 /* Remove CHILD from its parent. PREV must have the property that
7954 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
7956 static void
7957 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7959 gcc_assert (child->die_parent == prev->die_parent);
7960 gcc_assert (prev->die_sib == child);
7961 if (prev == child)
7963 gcc_assert (child->die_parent->die_child == child);
7964 prev = NULL;
7966 else
7967 prev->die_sib = child->die_sib;
7968 if (child->die_parent->die_child == child)
7969 child->die_parent->die_child = prev;
7972 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
7973 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
7975 static void
7976 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7978 dw_die_ref parent = old_child->die_parent;
7980 gcc_assert (parent == prev->die_parent);
7981 gcc_assert (prev->die_sib == old_child);
7983 new_child->die_parent = parent;
7984 if (prev == old_child)
7986 gcc_assert (parent->die_child == old_child);
7987 new_child->die_sib = new_child;
7989 else
7991 prev->die_sib = new_child;
7992 new_child->die_sib = old_child->die_sib;
7994 if (old_child->die_parent->die_child == old_child)
7995 old_child->die_parent->die_child = new_child;
7998 /* Move all children from OLD_PARENT to NEW_PARENT. */
8000 static void
8001 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8003 dw_die_ref c;
8004 new_parent->die_child = old_parent->die_child;
8005 old_parent->die_child = NULL;
8006 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8009 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
8010 matches TAG. */
8012 static void
8013 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8015 dw_die_ref c;
8017 c = die->die_child;
8018 if (c) do {
8019 dw_die_ref prev = c;
8020 c = c->die_sib;
8021 while (c->die_tag == tag)
8023 remove_child_with_prev (c, prev);
8024 /* Might have removed every child. */
8025 if (c == c->die_sib)
8026 return;
8027 c = c->die_sib;
8029 } while (c != die->die_child);
8032 /* Add a CHILD_DIE as the last child of DIE. */
8034 static void
8035 add_child_die (dw_die_ref die, dw_die_ref child_die)
8037 /* FIXME this should probably be an assert. */
8038 if (! die || ! child_die)
8039 return;
8040 gcc_assert (die != child_die);
8042 child_die->die_parent = die;
8043 if (die->die_child)
8045 child_die->die_sib = die->die_child->die_sib;
8046 die->die_child->die_sib = child_die;
8048 else
8049 child_die->die_sib = child_die;
8050 die->die_child = child_die;
8053 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8054 is the specification, to the end of PARENT's list of children.
8055 This is done by removing and re-adding it. */
8057 static void
8058 splice_child_die (dw_die_ref parent, dw_die_ref child)
8060 dw_die_ref p;
8062 /* We want the declaration DIE from inside the class, not the
8063 specification DIE at toplevel. */
8064 if (child->die_parent != parent)
8066 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8068 if (tmp)
8069 child = tmp;
8072 gcc_assert (child->die_parent == parent
8073 || (child->die_parent
8074 == get_AT_ref (parent, DW_AT_specification)));
8076 for (p = child->die_parent->die_child; ; p = p->die_sib)
8077 if (p->die_sib == child)
8079 remove_child_with_prev (child, p);
8080 break;
8083 add_child_die (parent, child);
8086 /* Return a pointer to a newly created DIE node. */
8088 static inline dw_die_ref
8089 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8091 dw_die_ref die = ggc_alloc_cleared_die_node ();
8093 die->die_tag = tag_value;
8095 if (parent_die != NULL)
8096 add_child_die (parent_die, die);
8097 else
8099 limbo_die_node *limbo_node;
8101 limbo_node = ggc_alloc_cleared_limbo_die_node ();
8102 limbo_node->die = die;
8103 limbo_node->created_for = t;
8104 limbo_node->next = limbo_die_list;
8105 limbo_die_list = limbo_node;
8108 return die;
8111 /* Return the DIE associated with the given type specifier. */
8113 static inline dw_die_ref
8114 lookup_type_die (tree type)
8116 return TYPE_SYMTAB_DIE (type);
8119 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8120 anonymous type named by the typedef TYPE_DIE, return the DIE of the
8121 anonymous type instead the one of the naming typedef. */
8123 static inline dw_die_ref
8124 strip_naming_typedef (tree type, dw_die_ref type_die)
8126 if (type
8127 && TREE_CODE (type) == RECORD_TYPE
8128 && type_die
8129 && type_die->die_tag == DW_TAG_typedef
8130 && is_naming_typedef_decl (TYPE_NAME (type)))
8131 type_die = get_AT_ref (type_die, DW_AT_type);
8132 return type_die;
8135 /* Like lookup_type_die, but if type is an anonymous type named by a
8136 typedef[1], return the DIE of the anonymous type instead the one of
8137 the naming typedef. This is because in gen_typedef_die, we did
8138 equate the anonymous struct named by the typedef with the DIE of
8139 the naming typedef. So by default, lookup_type_die on an anonymous
8140 struct yields the DIE of the naming typedef.
8142 [1]: Read the comment of is_naming_typedef_decl to learn about what
8143 a naming typedef is. */
8145 static inline dw_die_ref
8146 lookup_type_die_strip_naming_typedef (tree type)
8148 dw_die_ref die = lookup_type_die (type);
8149 return strip_naming_typedef (type, die);
8152 /* Equate a DIE to a given type specifier. */
8154 static inline void
8155 equate_type_number_to_die (tree type, dw_die_ref type_die)
8157 TYPE_SYMTAB_DIE (type) = type_die;
8160 /* Returns a hash value for X (which really is a die_struct). */
8162 static hashval_t
8163 decl_die_table_hash (const void *x)
8165 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8168 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
8170 static int
8171 decl_die_table_eq (const void *x, const void *y)
8173 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8176 /* Return the DIE associated with a given declaration. */
8178 static inline dw_die_ref
8179 lookup_decl_die (tree decl)
8181 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8184 /* Returns a hash value for X (which really is a var_loc_list). */
8186 static hashval_t
8187 decl_loc_table_hash (const void *x)
8189 return (hashval_t) ((const var_loc_list *) x)->decl_id;
8192 /* Return nonzero if decl_id of var_loc_list X is the same as
8193 UID of decl *Y. */
8195 static int
8196 decl_loc_table_eq (const void *x, const void *y)
8198 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8201 /* Return the var_loc list associated with a given declaration. */
8203 static inline var_loc_list *
8204 lookup_decl_loc (const_tree decl)
8206 if (!decl_loc_table)
8207 return NULL;
8208 return (var_loc_list *)
8209 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8212 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
8214 static hashval_t
8215 cached_dw_loc_list_table_hash (const void *x)
8217 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8220 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8221 UID of decl *Y. */
8223 static int
8224 cached_dw_loc_list_table_eq (const void *x, const void *y)
8226 return (((const cached_dw_loc_list *) x)->decl_id
8227 == DECL_UID ((const_tree) y));
8230 /* Equate a DIE to a particular declaration. */
8232 static void
8233 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8235 unsigned int decl_id = DECL_UID (decl);
8236 void **slot;
8238 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8239 *slot = decl_die;
8240 decl_die->decl_id = decl_id;
8243 /* Return how many bits covers PIECE EXPR_LIST. */
8245 static int
8246 decl_piece_bitsize (rtx piece)
8248 int ret = (int) GET_MODE (piece);
8249 if (ret)
8250 return ret;
8251 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8252 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8253 return INTVAL (XEXP (XEXP (piece, 0), 0));
8256 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
8258 static rtx *
8259 decl_piece_varloc_ptr (rtx piece)
8261 if ((int) GET_MODE (piece))
8262 return &XEXP (piece, 0);
8263 else
8264 return &XEXP (XEXP (piece, 0), 1);
8267 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8268 Next is the chain of following piece nodes. */
8270 static rtx
8271 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8273 if (bitsize <= (int) MAX_MACHINE_MODE)
8274 return alloc_EXPR_LIST (bitsize, loc_note, next);
8275 else
8276 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8277 GEN_INT (bitsize),
8278 loc_note), next);
8281 /* Return rtx that should be stored into loc field for
8282 LOC_NOTE and BITPOS/BITSIZE. */
8284 static rtx
8285 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8286 HOST_WIDE_INT bitsize)
8288 if (bitsize != -1)
8290 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8291 if (bitpos != 0)
8292 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8294 return loc_note;
8297 /* This function either modifies location piece list *DEST in
8298 place (if SRC and INNER is NULL), or copies location piece list
8299 *SRC to *DEST while modifying it. Location BITPOS is modified
8300 to contain LOC_NOTE, any pieces overlapping it are removed resp.
8301 not copied and if needed some padding around it is added.
8302 When modifying in place, DEST should point to EXPR_LIST where
8303 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8304 to the start of the whole list and INNER points to the EXPR_LIST
8305 where earlier pieces cover PIECE_BITPOS bits. */
8307 static void
8308 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8309 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8310 HOST_WIDE_INT bitsize, rtx loc_note)
8312 int diff;
8313 bool copy = inner != NULL;
8315 if (copy)
8317 /* First copy all nodes preceeding the current bitpos. */
8318 while (src != inner)
8320 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8321 decl_piece_bitsize (*src), NULL_RTX);
8322 dest = &XEXP (*dest, 1);
8323 src = &XEXP (*src, 1);
8326 /* Add padding if needed. */
8327 if (bitpos != piece_bitpos)
8329 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8330 copy ? NULL_RTX : *dest);
8331 dest = &XEXP (*dest, 1);
8333 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8335 gcc_assert (!copy);
8336 /* A piece with correct bitpos and bitsize already exist,
8337 just update the location for it and return. */
8338 *decl_piece_varloc_ptr (*dest) = loc_note;
8339 return;
8341 /* Add the piece that changed. */
8342 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8343 dest = &XEXP (*dest, 1);
8344 /* Skip over pieces that overlap it. */
8345 diff = bitpos - piece_bitpos + bitsize;
8346 if (!copy)
8347 src = dest;
8348 while (diff > 0 && *src)
8350 rtx piece = *src;
8351 diff -= decl_piece_bitsize (piece);
8352 if (copy)
8353 src = &XEXP (piece, 1);
8354 else
8356 *src = XEXP (piece, 1);
8357 free_EXPR_LIST_node (piece);
8360 /* Add padding if needed. */
8361 if (diff < 0 && *src)
8363 if (!copy)
8364 dest = src;
8365 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8366 dest = &XEXP (*dest, 1);
8368 if (!copy)
8369 return;
8370 /* Finally copy all nodes following it. */
8371 while (*src)
8373 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8374 decl_piece_bitsize (*src), NULL_RTX);
8375 dest = &XEXP (*dest, 1);
8376 src = &XEXP (*src, 1);
8380 /* Add a variable location node to the linked list for DECL. */
8382 static struct var_loc_node *
8383 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8385 unsigned int decl_id;
8386 var_loc_list *temp;
8387 void **slot;
8388 struct var_loc_node *loc = NULL;
8389 HOST_WIDE_INT bitsize = -1, bitpos = -1;
8391 if (DECL_DEBUG_EXPR_IS_FROM (decl))
8393 tree realdecl = DECL_DEBUG_EXPR (decl);
8394 if (realdecl && handled_component_p (realdecl))
8396 HOST_WIDE_INT maxsize;
8397 tree innerdecl;
8398 innerdecl
8399 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8400 if (!DECL_P (innerdecl)
8401 || DECL_IGNORED_P (innerdecl)
8402 || TREE_STATIC (innerdecl)
8403 || bitsize <= 0
8404 || bitpos + bitsize > 256
8405 || bitsize != maxsize)
8406 return NULL;
8407 decl = innerdecl;
8411 decl_id = DECL_UID (decl);
8412 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8413 if (*slot == NULL)
8415 temp = ggc_alloc_cleared_var_loc_list ();
8416 temp->decl_id = decl_id;
8417 *slot = temp;
8419 else
8420 temp = (var_loc_list *) *slot;
8422 if (temp->last)
8424 struct var_loc_node *last = temp->last, *unused = NULL;
8425 rtx *piece_loc = NULL, last_loc_note;
8426 int piece_bitpos = 0;
8427 if (last->next)
8429 last = last->next;
8430 gcc_assert (last->next == NULL);
8432 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8434 piece_loc = &last->loc;
8437 int cur_bitsize = decl_piece_bitsize (*piece_loc);
8438 if (piece_bitpos + cur_bitsize > bitpos)
8439 break;
8440 piece_bitpos += cur_bitsize;
8441 piece_loc = &XEXP (*piece_loc, 1);
8443 while (*piece_loc);
8445 /* TEMP->LAST here is either pointer to the last but one or
8446 last element in the chained list, LAST is pointer to the
8447 last element. */
8448 if (label && strcmp (last->label, label) == 0)
8450 /* For SRA optimized variables if there weren't any real
8451 insns since last note, just modify the last node. */
8452 if (piece_loc != NULL)
8454 adjust_piece_list (piece_loc, NULL, NULL,
8455 bitpos, piece_bitpos, bitsize, loc_note);
8456 return NULL;
8458 /* If the last note doesn't cover any instructions, remove it. */
8459 if (temp->last != last)
8461 temp->last->next = NULL;
8462 unused = last;
8463 last = temp->last;
8464 gcc_assert (strcmp (last->label, label) != 0);
8466 else
8468 gcc_assert (temp->first == temp->last);
8469 memset (temp->last, '\0', sizeof (*temp->last));
8470 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8471 return temp->last;
8474 if (bitsize == -1 && NOTE_P (last->loc))
8475 last_loc_note = last->loc;
8476 else if (piece_loc != NULL
8477 && *piece_loc != NULL_RTX
8478 && piece_bitpos == bitpos
8479 && decl_piece_bitsize (*piece_loc) == bitsize)
8480 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8481 else
8482 last_loc_note = NULL_RTX;
8483 /* If the current location is the same as the end of the list,
8484 and either both or neither of the locations is uninitialized,
8485 we have nothing to do. */
8486 if (last_loc_note == NULL_RTX
8487 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8488 NOTE_VAR_LOCATION_LOC (loc_note)))
8489 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8490 != NOTE_VAR_LOCATION_STATUS (loc_note))
8491 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8492 == VAR_INIT_STATUS_UNINITIALIZED)
8493 || (NOTE_VAR_LOCATION_STATUS (loc_note)
8494 == VAR_INIT_STATUS_UNINITIALIZED))))
8496 /* Add LOC to the end of list and update LAST. If the last
8497 element of the list has been removed above, reuse its
8498 memory for the new node, otherwise allocate a new one. */
8499 if (unused)
8501 loc = unused;
8502 memset (loc, '\0', sizeof (*loc));
8504 else
8505 loc = ggc_alloc_cleared_var_loc_node ();
8506 if (bitsize == -1 || piece_loc == NULL)
8507 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8508 else
8509 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8510 bitpos, piece_bitpos, bitsize, loc_note);
8511 last->next = loc;
8512 /* Ensure TEMP->LAST will point either to the new last but one
8513 element of the chain, or to the last element in it. */
8514 if (last != temp->last)
8515 temp->last = last;
8517 else if (unused)
8518 ggc_free (unused);
8520 else
8522 loc = ggc_alloc_cleared_var_loc_node ();
8523 temp->first = loc;
8524 temp->last = loc;
8525 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8527 return loc;
8530 /* Keep track of the number of spaces used to indent the
8531 output of the debugging routines that print the structure of
8532 the DIE internal representation. */
8533 static int print_indent;
8535 /* Indent the line the number of spaces given by print_indent. */
8537 static inline void
8538 print_spaces (FILE *outfile)
8540 fprintf (outfile, "%*s", print_indent, "");
8543 /* Print a type signature in hex. */
8545 static inline void
8546 print_signature (FILE *outfile, char *sig)
8548 int i;
8550 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8551 fprintf (outfile, "%02x", sig[i] & 0xff);
8554 /* Print the information associated with a given DIE, and its children.
8555 This routine is a debugging aid only. */
8557 static void
8558 print_die (dw_die_ref die, FILE *outfile)
8560 dw_attr_ref a;
8561 dw_die_ref c;
8562 unsigned ix;
8564 print_spaces (outfile);
8565 fprintf (outfile, "DIE %4ld: %s (%p)\n",
8566 die->die_offset, dwarf_tag_name (die->die_tag),
8567 (void*) die);
8568 print_spaces (outfile);
8569 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
8570 fprintf (outfile, " offset: %ld", die->die_offset);
8571 fprintf (outfile, " mark: %d\n", die->die_mark);
8573 if (use_debug_types && die->die_id.die_type_node)
8575 print_spaces (outfile);
8576 fprintf (outfile, " signature: ");
8577 print_signature (outfile, die->die_id.die_type_node->signature);
8578 fprintf (outfile, "\n");
8581 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8583 print_spaces (outfile);
8584 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
8586 switch (AT_class (a))
8588 case dw_val_class_addr:
8589 fprintf (outfile, "address");
8590 break;
8591 case dw_val_class_offset:
8592 fprintf (outfile, "offset");
8593 break;
8594 case dw_val_class_loc:
8595 fprintf (outfile, "location descriptor");
8596 break;
8597 case dw_val_class_loc_list:
8598 fprintf (outfile, "location list -> label:%s",
8599 AT_loc_list (a)->ll_symbol);
8600 break;
8601 case dw_val_class_range_list:
8602 fprintf (outfile, "range list");
8603 break;
8604 case dw_val_class_const:
8605 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8606 break;
8607 case dw_val_class_unsigned_const:
8608 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8609 break;
8610 case dw_val_class_const_double:
8611 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8612 HOST_WIDE_INT_PRINT_UNSIGNED")",
8613 a->dw_attr_val.v.val_double.high,
8614 a->dw_attr_val.v.val_double.low);
8615 break;
8616 case dw_val_class_vec:
8617 fprintf (outfile, "floating-point or vector constant");
8618 break;
8619 case dw_val_class_flag:
8620 fprintf (outfile, "%u", AT_flag (a));
8621 break;
8622 case dw_val_class_die_ref:
8623 if (AT_ref (a) != NULL)
8625 if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8627 fprintf (outfile, "die -> signature: ");
8628 print_signature (outfile,
8629 AT_ref (a)->die_id.die_type_node->signature);
8631 else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8632 fprintf (outfile, "die -> label: %s",
8633 AT_ref (a)->die_id.die_symbol);
8634 else
8635 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8636 fprintf (outfile, " (%p)", (void *) AT_ref (a));
8638 else
8639 fprintf (outfile, "die -> <null>");
8640 break;
8641 case dw_val_class_vms_delta:
8642 fprintf (outfile, "delta: @slotcount(%s-%s)",
8643 AT_vms_delta2 (a), AT_vms_delta1 (a));
8644 break;
8645 case dw_val_class_lbl_id:
8646 case dw_val_class_lineptr:
8647 case dw_val_class_macptr:
8648 fprintf (outfile, "label: %s", AT_lbl (a));
8649 break;
8650 case dw_val_class_str:
8651 if (AT_string (a) != NULL)
8652 fprintf (outfile, "\"%s\"", AT_string (a));
8653 else
8654 fprintf (outfile, "<null>");
8655 break;
8656 case dw_val_class_file:
8657 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8658 AT_file (a)->emitted_number);
8659 break;
8660 case dw_val_class_data8:
8662 int i;
8664 for (i = 0; i < 8; i++)
8665 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8666 break;
8668 default:
8669 break;
8672 fprintf (outfile, "\n");
8675 if (die->die_child != NULL)
8677 print_indent += 4;
8678 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8679 print_indent -= 4;
8681 if (print_indent == 0)
8682 fprintf (outfile, "\n");
8685 /* Print the contents of the source code line number correspondence table.
8686 This routine is a debugging aid only. */
8688 static void
8689 print_dwarf_line_table (FILE *outfile)
8691 unsigned i;
8692 dw_line_info_ref line_info;
8694 fprintf (outfile, "\n\nDWARF source line information\n");
8695 for (i = 1; i < line_info_table_in_use; i++)
8697 line_info = &line_info_table[i];
8698 fprintf (outfile, "%5d: %4ld %6ld\n", i,
8699 line_info->dw_file_num,
8700 line_info->dw_line_num);
8703 fprintf (outfile, "\n\n");
8706 /* Print the information collected for a given DIE. */
8708 DEBUG_FUNCTION void
8709 debug_dwarf_die (dw_die_ref die)
8711 print_die (die, stderr);
8714 /* Print all DWARF information collected for the compilation unit.
8715 This routine is a debugging aid only. */
8717 DEBUG_FUNCTION void
8718 debug_dwarf (void)
8720 print_indent = 0;
8721 print_die (comp_unit_die (), stderr);
8722 if (! DWARF2_ASM_LINE_DEBUG_INFO)
8723 print_dwarf_line_table (stderr);
8726 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
8727 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
8728 DIE that marks the start of the DIEs for this include file. */
8730 static dw_die_ref
8731 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8733 const char *filename = get_AT_string (bincl_die, DW_AT_name);
8734 dw_die_ref new_unit = gen_compile_unit_die (filename);
8736 new_unit->die_sib = old_unit;
8737 return new_unit;
8740 /* Close an include-file CU and reopen the enclosing one. */
8742 static dw_die_ref
8743 pop_compile_unit (dw_die_ref old_unit)
8745 dw_die_ref new_unit = old_unit->die_sib;
8747 old_unit->die_sib = NULL;
8748 return new_unit;
8751 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8752 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8754 /* Calculate the checksum of a location expression. */
8756 static inline void
8757 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8759 int tem;
8761 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8762 CHECKSUM (tem);
8763 CHECKSUM (loc->dw_loc_oprnd1);
8764 CHECKSUM (loc->dw_loc_oprnd2);
8767 /* Calculate the checksum of an attribute. */
8769 static void
8770 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8772 dw_loc_descr_ref loc;
8773 rtx r;
8775 CHECKSUM (at->dw_attr);
8777 /* We don't care that this was compiled with a different compiler
8778 snapshot; if the output is the same, that's what matters. */
8779 if (at->dw_attr == DW_AT_producer)
8780 return;
8782 switch (AT_class (at))
8784 case dw_val_class_const:
8785 CHECKSUM (at->dw_attr_val.v.val_int);
8786 break;
8787 case dw_val_class_unsigned_const:
8788 CHECKSUM (at->dw_attr_val.v.val_unsigned);
8789 break;
8790 case dw_val_class_const_double:
8791 CHECKSUM (at->dw_attr_val.v.val_double);
8792 break;
8793 case dw_val_class_vec:
8794 CHECKSUM (at->dw_attr_val.v.val_vec);
8795 break;
8796 case dw_val_class_flag:
8797 CHECKSUM (at->dw_attr_val.v.val_flag);
8798 break;
8799 case dw_val_class_str:
8800 CHECKSUM_STRING (AT_string (at));
8801 break;
8803 case dw_val_class_addr:
8804 r = AT_addr (at);
8805 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8806 CHECKSUM_STRING (XSTR (r, 0));
8807 break;
8809 case dw_val_class_offset:
8810 CHECKSUM (at->dw_attr_val.v.val_offset);
8811 break;
8813 case dw_val_class_loc:
8814 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8815 loc_checksum (loc, ctx);
8816 break;
8818 case dw_val_class_die_ref:
8819 die_checksum (AT_ref (at), ctx, mark);
8820 break;
8822 case dw_val_class_fde_ref:
8823 case dw_val_class_vms_delta:
8824 case dw_val_class_lbl_id:
8825 case dw_val_class_lineptr:
8826 case dw_val_class_macptr:
8827 break;
8829 case dw_val_class_file:
8830 CHECKSUM_STRING (AT_file (at)->filename);
8831 break;
8833 case dw_val_class_data8:
8834 CHECKSUM (at->dw_attr_val.v.val_data8);
8835 break;
8837 default:
8838 break;
8842 /* Calculate the checksum of a DIE. */
8844 static void
8845 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8847 dw_die_ref c;
8848 dw_attr_ref a;
8849 unsigned ix;
8851 /* To avoid infinite recursion. */
8852 if (die->die_mark)
8854 CHECKSUM (die->die_mark);
8855 return;
8857 die->die_mark = ++(*mark);
8859 CHECKSUM (die->die_tag);
8861 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8862 attr_checksum (a, ctx, mark);
8864 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8867 #undef CHECKSUM
8868 #undef CHECKSUM_STRING
8870 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
8871 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8872 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8873 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8874 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8875 #define CHECKSUM_ATTR(FOO) \
8876 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8878 /* Calculate the checksum of a number in signed LEB128 format. */
8880 static void
8881 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8883 unsigned char byte;
8884 bool more;
8886 while (1)
8888 byte = (value & 0x7f);
8889 value >>= 7;
8890 more = !((value == 0 && (byte & 0x40) == 0)
8891 || (value == -1 && (byte & 0x40) != 0));
8892 if (more)
8893 byte |= 0x80;
8894 CHECKSUM (byte);
8895 if (!more)
8896 break;
8900 /* Calculate the checksum of a number in unsigned LEB128 format. */
8902 static void
8903 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8905 while (1)
8907 unsigned char byte = (value & 0x7f);
8908 value >>= 7;
8909 if (value != 0)
8910 /* More bytes to follow. */
8911 byte |= 0x80;
8912 CHECKSUM (byte);
8913 if (value == 0)
8914 break;
8918 /* Checksum the context of the DIE. This adds the names of any
8919 surrounding namespaces or structures to the checksum. */
8921 static void
8922 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8924 const char *name;
8925 dw_die_ref spec;
8926 int tag = die->die_tag;
8928 if (tag != DW_TAG_namespace
8929 && tag != DW_TAG_structure_type
8930 && tag != DW_TAG_class_type)
8931 return;
8933 name = get_AT_string (die, DW_AT_name);
8935 spec = get_AT_ref (die, DW_AT_specification);
8936 if (spec != NULL)
8937 die = spec;
8939 if (die->die_parent != NULL)
8940 checksum_die_context (die->die_parent, ctx);
8942 CHECKSUM_ULEB128 ('C');
8943 CHECKSUM_ULEB128 (tag);
8944 if (name != NULL)
8945 CHECKSUM_STRING (name);
8948 /* Calculate the checksum of a location expression. */
8950 static inline void
8951 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8953 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8954 were emitted as a DW_FORM_sdata instead of a location expression. */
8955 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8957 CHECKSUM_ULEB128 (DW_FORM_sdata);
8958 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8959 return;
8962 /* Otherwise, just checksum the raw location expression. */
8963 while (loc != NULL)
8965 CHECKSUM_ULEB128 (loc->dw_loc_opc);
8966 CHECKSUM (loc->dw_loc_oprnd1);
8967 CHECKSUM (loc->dw_loc_oprnd2);
8968 loc = loc->dw_loc_next;
8972 /* Calculate the checksum of an attribute. */
8974 static void
8975 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8976 struct md5_ctx *ctx, int *mark)
8978 dw_loc_descr_ref loc;
8979 rtx r;
8981 if (AT_class (at) == dw_val_class_die_ref)
8983 dw_die_ref target_die = AT_ref (at);
8985 /* For pointer and reference types, we checksum only the (qualified)
8986 name of the target type (if there is a name). For friend entries,
8987 we checksum only the (qualified) name of the target type or function.
8988 This allows the checksum to remain the same whether the target type
8989 is complete or not. */
8990 if ((at->dw_attr == DW_AT_type
8991 && (tag == DW_TAG_pointer_type
8992 || tag == DW_TAG_reference_type
8993 || tag == DW_TAG_rvalue_reference_type
8994 || tag == DW_TAG_ptr_to_member_type))
8995 || (at->dw_attr == DW_AT_friend
8996 && tag == DW_TAG_friend))
8998 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9000 if (name_attr != NULL)
9002 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9004 if (decl == NULL)
9005 decl = target_die;
9006 CHECKSUM_ULEB128 ('N');
9007 CHECKSUM_ULEB128 (at->dw_attr);
9008 if (decl->die_parent != NULL)
9009 checksum_die_context (decl->die_parent, ctx);
9010 CHECKSUM_ULEB128 ('E');
9011 CHECKSUM_STRING (AT_string (name_attr));
9012 return;
9016 /* For all other references to another DIE, we check to see if the
9017 target DIE has already been visited. If it has, we emit a
9018 backward reference; if not, we descend recursively. */
9019 if (target_die->die_mark > 0)
9021 CHECKSUM_ULEB128 ('R');
9022 CHECKSUM_ULEB128 (at->dw_attr);
9023 CHECKSUM_ULEB128 (target_die->die_mark);
9025 else
9027 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9029 if (decl == NULL)
9030 decl = target_die;
9031 target_die->die_mark = ++(*mark);
9032 CHECKSUM_ULEB128 ('T');
9033 CHECKSUM_ULEB128 (at->dw_attr);
9034 if (decl->die_parent != NULL)
9035 checksum_die_context (decl->die_parent, ctx);
9036 die_checksum_ordered (target_die, ctx, mark);
9038 return;
9041 CHECKSUM_ULEB128 ('A');
9042 CHECKSUM_ULEB128 (at->dw_attr);
9044 switch (AT_class (at))
9046 case dw_val_class_const:
9047 CHECKSUM_ULEB128 (DW_FORM_sdata);
9048 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9049 break;
9051 case dw_val_class_unsigned_const:
9052 CHECKSUM_ULEB128 (DW_FORM_sdata);
9053 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9054 break;
9056 case dw_val_class_const_double:
9057 CHECKSUM_ULEB128 (DW_FORM_block);
9058 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9059 CHECKSUM (at->dw_attr_val.v.val_double);
9060 break;
9062 case dw_val_class_vec:
9063 CHECKSUM_ULEB128 (DW_FORM_block);
9064 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9065 CHECKSUM (at->dw_attr_val.v.val_vec);
9066 break;
9068 case dw_val_class_flag:
9069 CHECKSUM_ULEB128 (DW_FORM_flag);
9070 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9071 break;
9073 case dw_val_class_str:
9074 CHECKSUM_ULEB128 (DW_FORM_string);
9075 CHECKSUM_STRING (AT_string (at));
9076 break;
9078 case dw_val_class_addr:
9079 r = AT_addr (at);
9080 gcc_assert (GET_CODE (r) == SYMBOL_REF);
9081 CHECKSUM_ULEB128 (DW_FORM_string);
9082 CHECKSUM_STRING (XSTR (r, 0));
9083 break;
9085 case dw_val_class_offset:
9086 CHECKSUM_ULEB128 (DW_FORM_sdata);
9087 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9088 break;
9090 case dw_val_class_loc:
9091 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9092 loc_checksum_ordered (loc, ctx);
9093 break;
9095 case dw_val_class_fde_ref:
9096 case dw_val_class_lbl_id:
9097 case dw_val_class_lineptr:
9098 case dw_val_class_macptr:
9099 break;
9101 case dw_val_class_file:
9102 CHECKSUM_ULEB128 (DW_FORM_string);
9103 CHECKSUM_STRING (AT_file (at)->filename);
9104 break;
9106 case dw_val_class_data8:
9107 CHECKSUM (at->dw_attr_val.v.val_data8);
9108 break;
9110 default:
9111 break;
9115 struct checksum_attributes
9117 dw_attr_ref at_name;
9118 dw_attr_ref at_type;
9119 dw_attr_ref at_friend;
9120 dw_attr_ref at_accessibility;
9121 dw_attr_ref at_address_class;
9122 dw_attr_ref at_allocated;
9123 dw_attr_ref at_artificial;
9124 dw_attr_ref at_associated;
9125 dw_attr_ref at_binary_scale;
9126 dw_attr_ref at_bit_offset;
9127 dw_attr_ref at_bit_size;
9128 dw_attr_ref at_bit_stride;
9129 dw_attr_ref at_byte_size;
9130 dw_attr_ref at_byte_stride;
9131 dw_attr_ref at_const_value;
9132 dw_attr_ref at_containing_type;
9133 dw_attr_ref at_count;
9134 dw_attr_ref at_data_location;
9135 dw_attr_ref at_data_member_location;
9136 dw_attr_ref at_decimal_scale;
9137 dw_attr_ref at_decimal_sign;
9138 dw_attr_ref at_default_value;
9139 dw_attr_ref at_digit_count;
9140 dw_attr_ref at_discr;
9141 dw_attr_ref at_discr_list;
9142 dw_attr_ref at_discr_value;
9143 dw_attr_ref at_encoding;
9144 dw_attr_ref at_endianity;
9145 dw_attr_ref at_explicit;
9146 dw_attr_ref at_is_optional;
9147 dw_attr_ref at_location;
9148 dw_attr_ref at_lower_bound;
9149 dw_attr_ref at_mutable;
9150 dw_attr_ref at_ordering;
9151 dw_attr_ref at_picture_string;
9152 dw_attr_ref at_prototyped;
9153 dw_attr_ref at_small;
9154 dw_attr_ref at_segment;
9155 dw_attr_ref at_string_length;
9156 dw_attr_ref at_threads_scaled;
9157 dw_attr_ref at_upper_bound;
9158 dw_attr_ref at_use_location;
9159 dw_attr_ref at_use_UTF8;
9160 dw_attr_ref at_variable_parameter;
9161 dw_attr_ref at_virtuality;
9162 dw_attr_ref at_visibility;
9163 dw_attr_ref at_vtable_elem_location;
9166 /* Collect the attributes that we will want to use for the checksum. */
9168 static void
9169 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9171 dw_attr_ref a;
9172 unsigned ix;
9174 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9176 switch (a->dw_attr)
9178 case DW_AT_name:
9179 attrs->at_name = a;
9180 break;
9181 case DW_AT_type:
9182 attrs->at_type = a;
9183 break;
9184 case DW_AT_friend:
9185 attrs->at_friend = a;
9186 break;
9187 case DW_AT_accessibility:
9188 attrs->at_accessibility = a;
9189 break;
9190 case DW_AT_address_class:
9191 attrs->at_address_class = a;
9192 break;
9193 case DW_AT_allocated:
9194 attrs->at_allocated = a;
9195 break;
9196 case DW_AT_artificial:
9197 attrs->at_artificial = a;
9198 break;
9199 case DW_AT_associated:
9200 attrs->at_associated = a;
9201 break;
9202 case DW_AT_binary_scale:
9203 attrs->at_binary_scale = a;
9204 break;
9205 case DW_AT_bit_offset:
9206 attrs->at_bit_offset = a;
9207 break;
9208 case DW_AT_bit_size:
9209 attrs->at_bit_size = a;
9210 break;
9211 case DW_AT_bit_stride:
9212 attrs->at_bit_stride = a;
9213 break;
9214 case DW_AT_byte_size:
9215 attrs->at_byte_size = a;
9216 break;
9217 case DW_AT_byte_stride:
9218 attrs->at_byte_stride = a;
9219 break;
9220 case DW_AT_const_value:
9221 attrs->at_const_value = a;
9222 break;
9223 case DW_AT_containing_type:
9224 attrs->at_containing_type = a;
9225 break;
9226 case DW_AT_count:
9227 attrs->at_count = a;
9228 break;
9229 case DW_AT_data_location:
9230 attrs->at_data_location = a;
9231 break;
9232 case DW_AT_data_member_location:
9233 attrs->at_data_member_location = a;
9234 break;
9235 case DW_AT_decimal_scale:
9236 attrs->at_decimal_scale = a;
9237 break;
9238 case DW_AT_decimal_sign:
9239 attrs->at_decimal_sign = a;
9240 break;
9241 case DW_AT_default_value:
9242 attrs->at_default_value = a;
9243 break;
9244 case DW_AT_digit_count:
9245 attrs->at_digit_count = a;
9246 break;
9247 case DW_AT_discr:
9248 attrs->at_discr = a;
9249 break;
9250 case DW_AT_discr_list:
9251 attrs->at_discr_list = a;
9252 break;
9253 case DW_AT_discr_value:
9254 attrs->at_discr_value = a;
9255 break;
9256 case DW_AT_encoding:
9257 attrs->at_encoding = a;
9258 break;
9259 case DW_AT_endianity:
9260 attrs->at_endianity = a;
9261 break;
9262 case DW_AT_explicit:
9263 attrs->at_explicit = a;
9264 break;
9265 case DW_AT_is_optional:
9266 attrs->at_is_optional = a;
9267 break;
9268 case DW_AT_location:
9269 attrs->at_location = a;
9270 break;
9271 case DW_AT_lower_bound:
9272 attrs->at_lower_bound = a;
9273 break;
9274 case DW_AT_mutable:
9275 attrs->at_mutable = a;
9276 break;
9277 case DW_AT_ordering:
9278 attrs->at_ordering = a;
9279 break;
9280 case DW_AT_picture_string:
9281 attrs->at_picture_string = a;
9282 break;
9283 case DW_AT_prototyped:
9284 attrs->at_prototyped = a;
9285 break;
9286 case DW_AT_small:
9287 attrs->at_small = a;
9288 break;
9289 case DW_AT_segment:
9290 attrs->at_segment = a;
9291 break;
9292 case DW_AT_string_length:
9293 attrs->at_string_length = a;
9294 break;
9295 case DW_AT_threads_scaled:
9296 attrs->at_threads_scaled = a;
9297 break;
9298 case DW_AT_upper_bound:
9299 attrs->at_upper_bound = a;
9300 break;
9301 case DW_AT_use_location:
9302 attrs->at_use_location = a;
9303 break;
9304 case DW_AT_use_UTF8:
9305 attrs->at_use_UTF8 = a;
9306 break;
9307 case DW_AT_variable_parameter:
9308 attrs->at_variable_parameter = a;
9309 break;
9310 case DW_AT_virtuality:
9311 attrs->at_virtuality = a;
9312 break;
9313 case DW_AT_visibility:
9314 attrs->at_visibility = a;
9315 break;
9316 case DW_AT_vtable_elem_location:
9317 attrs->at_vtable_elem_location = a;
9318 break;
9319 default:
9320 break;
9325 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
9327 static void
9328 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9330 dw_die_ref c;
9331 dw_die_ref decl;
9332 struct checksum_attributes attrs;
9334 CHECKSUM_ULEB128 ('D');
9335 CHECKSUM_ULEB128 (die->die_tag);
9337 memset (&attrs, 0, sizeof (attrs));
9339 decl = get_AT_ref (die, DW_AT_specification);
9340 if (decl != NULL)
9341 collect_checksum_attributes (&attrs, decl);
9342 collect_checksum_attributes (&attrs, die);
9344 CHECKSUM_ATTR (attrs.at_name);
9345 CHECKSUM_ATTR (attrs.at_accessibility);
9346 CHECKSUM_ATTR (attrs.at_address_class);
9347 CHECKSUM_ATTR (attrs.at_allocated);
9348 CHECKSUM_ATTR (attrs.at_artificial);
9349 CHECKSUM_ATTR (attrs.at_associated);
9350 CHECKSUM_ATTR (attrs.at_binary_scale);
9351 CHECKSUM_ATTR (attrs.at_bit_offset);
9352 CHECKSUM_ATTR (attrs.at_bit_size);
9353 CHECKSUM_ATTR (attrs.at_bit_stride);
9354 CHECKSUM_ATTR (attrs.at_byte_size);
9355 CHECKSUM_ATTR (attrs.at_byte_stride);
9356 CHECKSUM_ATTR (attrs.at_const_value);
9357 CHECKSUM_ATTR (attrs.at_containing_type);
9358 CHECKSUM_ATTR (attrs.at_count);
9359 CHECKSUM_ATTR (attrs.at_data_location);
9360 CHECKSUM_ATTR (attrs.at_data_member_location);
9361 CHECKSUM_ATTR (attrs.at_decimal_scale);
9362 CHECKSUM_ATTR (attrs.at_decimal_sign);
9363 CHECKSUM_ATTR (attrs.at_default_value);
9364 CHECKSUM_ATTR (attrs.at_digit_count);
9365 CHECKSUM_ATTR (attrs.at_discr);
9366 CHECKSUM_ATTR (attrs.at_discr_list);
9367 CHECKSUM_ATTR (attrs.at_discr_value);
9368 CHECKSUM_ATTR (attrs.at_encoding);
9369 CHECKSUM_ATTR (attrs.at_endianity);
9370 CHECKSUM_ATTR (attrs.at_explicit);
9371 CHECKSUM_ATTR (attrs.at_is_optional);
9372 CHECKSUM_ATTR (attrs.at_location);
9373 CHECKSUM_ATTR (attrs.at_lower_bound);
9374 CHECKSUM_ATTR (attrs.at_mutable);
9375 CHECKSUM_ATTR (attrs.at_ordering);
9376 CHECKSUM_ATTR (attrs.at_picture_string);
9377 CHECKSUM_ATTR (attrs.at_prototyped);
9378 CHECKSUM_ATTR (attrs.at_small);
9379 CHECKSUM_ATTR (attrs.at_segment);
9380 CHECKSUM_ATTR (attrs.at_string_length);
9381 CHECKSUM_ATTR (attrs.at_threads_scaled);
9382 CHECKSUM_ATTR (attrs.at_upper_bound);
9383 CHECKSUM_ATTR (attrs.at_use_location);
9384 CHECKSUM_ATTR (attrs.at_use_UTF8);
9385 CHECKSUM_ATTR (attrs.at_variable_parameter);
9386 CHECKSUM_ATTR (attrs.at_virtuality);
9387 CHECKSUM_ATTR (attrs.at_visibility);
9388 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9389 CHECKSUM_ATTR (attrs.at_type);
9390 CHECKSUM_ATTR (attrs.at_friend);
9392 /* Checksum the child DIEs, except for nested types and member functions. */
9393 c = die->die_child;
9394 if (c) do {
9395 dw_attr_ref name_attr;
9397 c = c->die_sib;
9398 name_attr = get_AT (c, DW_AT_name);
9399 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9400 && name_attr != NULL)
9402 CHECKSUM_ULEB128 ('S');
9403 CHECKSUM_ULEB128 (c->die_tag);
9404 CHECKSUM_STRING (AT_string (name_attr));
9406 else
9408 /* Mark this DIE so it gets processed when unmarking. */
9409 if (c->die_mark == 0)
9410 c->die_mark = -1;
9411 die_checksum_ordered (c, ctx, mark);
9413 } while (c != die->die_child);
9415 CHECKSUM_ULEB128 (0);
9418 #undef CHECKSUM
9419 #undef CHECKSUM_STRING
9420 #undef CHECKSUM_ATTR
9421 #undef CHECKSUM_LEB128
9422 #undef CHECKSUM_ULEB128
9424 /* Generate the type signature for DIE. This is computed by generating an
9425 MD5 checksum over the DIE's tag, its relevant attributes, and its
9426 children. Attributes that are references to other DIEs are processed
9427 by recursion, using the MARK field to prevent infinite recursion.
9428 If the DIE is nested inside a namespace or another type, we also
9429 need to include that context in the signature. The lower 64 bits
9430 of the resulting MD5 checksum comprise the signature. */
9432 static void
9433 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9435 int mark;
9436 const char *name;
9437 unsigned char checksum[16];
9438 struct md5_ctx ctx;
9439 dw_die_ref decl;
9441 name = get_AT_string (die, DW_AT_name);
9442 decl = get_AT_ref (die, DW_AT_specification);
9444 /* First, compute a signature for just the type name (and its surrounding
9445 context, if any. This is stored in the type unit DIE for link-time
9446 ODR (one-definition rule) checking. */
9448 if (is_cxx() && name != NULL)
9450 md5_init_ctx (&ctx);
9452 /* Checksum the names of surrounding namespaces and structures. */
9453 if (decl != NULL && decl->die_parent != NULL)
9454 checksum_die_context (decl->die_parent, &ctx);
9456 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9457 md5_process_bytes (name, strlen (name) + 1, &ctx);
9458 md5_finish_ctx (&ctx, checksum);
9460 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9463 /* Next, compute the complete type signature. */
9465 md5_init_ctx (&ctx);
9466 mark = 1;
9467 die->die_mark = mark;
9469 /* Checksum the names of surrounding namespaces and structures. */
9470 if (decl != NULL && decl->die_parent != NULL)
9471 checksum_die_context (decl->die_parent, &ctx);
9473 /* Checksum the DIE and its children. */
9474 die_checksum_ordered (die, &ctx, &mark);
9475 unmark_all_dies (die);
9476 md5_finish_ctx (&ctx, checksum);
9478 /* Store the signature in the type node and link the type DIE and the
9479 type node together. */
9480 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9481 DWARF_TYPE_SIGNATURE_SIZE);
9482 die->die_id.die_type_node = type_node;
9483 type_node->type_die = die;
9485 /* If the DIE is a specification, link its declaration to the type node
9486 as well. */
9487 if (decl != NULL)
9488 decl->die_id.die_type_node = type_node;
9491 /* Do the location expressions look same? */
9492 static inline int
9493 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9495 return loc1->dw_loc_opc == loc2->dw_loc_opc
9496 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9497 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9500 /* Do the values look the same? */
9501 static int
9502 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9504 dw_loc_descr_ref loc1, loc2;
9505 rtx r1, r2;
9507 if (v1->val_class != v2->val_class)
9508 return 0;
9510 switch (v1->val_class)
9512 case dw_val_class_const:
9513 return v1->v.val_int == v2->v.val_int;
9514 case dw_val_class_unsigned_const:
9515 return v1->v.val_unsigned == v2->v.val_unsigned;
9516 case dw_val_class_const_double:
9517 return v1->v.val_double.high == v2->v.val_double.high
9518 && v1->v.val_double.low == v2->v.val_double.low;
9519 case dw_val_class_vec:
9520 if (v1->v.val_vec.length != v2->v.val_vec.length
9521 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9522 return 0;
9523 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9524 v1->v.val_vec.length * v1->v.val_vec.elt_size))
9525 return 0;
9526 return 1;
9527 case dw_val_class_flag:
9528 return v1->v.val_flag == v2->v.val_flag;
9529 case dw_val_class_str:
9530 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9532 case dw_val_class_addr:
9533 r1 = v1->v.val_addr;
9534 r2 = v2->v.val_addr;
9535 if (GET_CODE (r1) != GET_CODE (r2))
9536 return 0;
9537 return !rtx_equal_p (r1, r2);
9539 case dw_val_class_offset:
9540 return v1->v.val_offset == v2->v.val_offset;
9542 case dw_val_class_loc:
9543 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9544 loc1 && loc2;
9545 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9546 if (!same_loc_p (loc1, loc2, mark))
9547 return 0;
9548 return !loc1 && !loc2;
9550 case dw_val_class_die_ref:
9551 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9553 case dw_val_class_fde_ref:
9554 case dw_val_class_vms_delta:
9555 case dw_val_class_lbl_id:
9556 case dw_val_class_lineptr:
9557 case dw_val_class_macptr:
9558 return 1;
9560 case dw_val_class_file:
9561 return v1->v.val_file == v2->v.val_file;
9563 case dw_val_class_data8:
9564 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9566 default:
9567 return 1;
9571 /* Do the attributes look the same? */
9573 static int
9574 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9576 if (at1->dw_attr != at2->dw_attr)
9577 return 0;
9579 /* We don't care that this was compiled with a different compiler
9580 snapshot; if the output is the same, that's what matters. */
9581 if (at1->dw_attr == DW_AT_producer)
9582 return 1;
9584 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9587 /* Do the dies look the same? */
9589 static int
9590 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9592 dw_die_ref c1, c2;
9593 dw_attr_ref a1;
9594 unsigned ix;
9596 /* To avoid infinite recursion. */
9597 if (die1->die_mark)
9598 return die1->die_mark == die2->die_mark;
9599 die1->die_mark = die2->die_mark = ++(*mark);
9601 if (die1->die_tag != die2->die_tag)
9602 return 0;
9604 if (VEC_length (dw_attr_node, die1->die_attr)
9605 != VEC_length (dw_attr_node, die2->die_attr))
9606 return 0;
9608 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9609 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9610 return 0;
9612 c1 = die1->die_child;
9613 c2 = die2->die_child;
9614 if (! c1)
9616 if (c2)
9617 return 0;
9619 else
9620 for (;;)
9622 if (!same_die_p (c1, c2, mark))
9623 return 0;
9624 c1 = c1->die_sib;
9625 c2 = c2->die_sib;
9626 if (c1 == die1->die_child)
9628 if (c2 == die2->die_child)
9629 break;
9630 else
9631 return 0;
9635 return 1;
9638 /* Do the dies look the same? Wrapper around same_die_p. */
9640 static int
9641 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9643 int mark = 0;
9644 int ret = same_die_p (die1, die2, &mark);
9646 unmark_all_dies (die1);
9647 unmark_all_dies (die2);
9649 return ret;
9652 /* The prefix to attach to symbols on DIEs in the current comdat debug
9653 info section. */
9654 static char *comdat_symbol_id;
9656 /* The index of the current symbol within the current comdat CU. */
9657 static unsigned int comdat_symbol_number;
9659 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9660 children, and set comdat_symbol_id accordingly. */
9662 static void
9663 compute_section_prefix (dw_die_ref unit_die)
9665 const char *die_name = get_AT_string (unit_die, DW_AT_name);
9666 const char *base = die_name ? lbasename (die_name) : "anonymous";
9667 char *name = XALLOCAVEC (char, strlen (base) + 64);
9668 char *p;
9669 int i, mark;
9670 unsigned char checksum[16];
9671 struct md5_ctx ctx;
9673 /* Compute the checksum of the DIE, then append part of it as hex digits to
9674 the name filename of the unit. */
9676 md5_init_ctx (&ctx);
9677 mark = 0;
9678 die_checksum (unit_die, &ctx, &mark);
9679 unmark_all_dies (unit_die);
9680 md5_finish_ctx (&ctx, checksum);
9682 sprintf (name, "%s.", base);
9683 clean_symbol_name (name);
9685 p = name + strlen (name);
9686 for (i = 0; i < 4; i++)
9688 sprintf (p, "%.2x", checksum[i]);
9689 p += 2;
9692 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9693 comdat_symbol_number = 0;
9696 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
9698 static int
9699 is_type_die (dw_die_ref die)
9701 switch (die->die_tag)
9703 case DW_TAG_array_type:
9704 case DW_TAG_class_type:
9705 case DW_TAG_interface_type:
9706 case DW_TAG_enumeration_type:
9707 case DW_TAG_pointer_type:
9708 case DW_TAG_reference_type:
9709 case DW_TAG_rvalue_reference_type:
9710 case DW_TAG_string_type:
9711 case DW_TAG_structure_type:
9712 case DW_TAG_subroutine_type:
9713 case DW_TAG_union_type:
9714 case DW_TAG_ptr_to_member_type:
9715 case DW_TAG_set_type:
9716 case DW_TAG_subrange_type:
9717 case DW_TAG_base_type:
9718 case DW_TAG_const_type:
9719 case DW_TAG_file_type:
9720 case DW_TAG_packed_type:
9721 case DW_TAG_volatile_type:
9722 case DW_TAG_typedef:
9723 return 1;
9724 default:
9725 return 0;
9729 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9730 Basically, we want to choose the bits that are likely to be shared between
9731 compilations (types) and leave out the bits that are specific to individual
9732 compilations (functions). */
9734 static int
9735 is_comdat_die (dw_die_ref c)
9737 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9738 we do for stabs. The advantage is a greater likelihood of sharing between
9739 objects that don't include headers in the same order (and therefore would
9740 put the base types in a different comdat). jason 8/28/00 */
9742 if (c->die_tag == DW_TAG_base_type)
9743 return 0;
9745 if (c->die_tag == DW_TAG_pointer_type
9746 || c->die_tag == DW_TAG_reference_type
9747 || c->die_tag == DW_TAG_rvalue_reference_type
9748 || c->die_tag == DW_TAG_const_type
9749 || c->die_tag == DW_TAG_volatile_type)
9751 dw_die_ref t = get_AT_ref (c, DW_AT_type);
9753 return t ? is_comdat_die (t) : 0;
9756 return is_type_die (c);
9759 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9760 compilation unit. */
9762 static int
9763 is_symbol_die (dw_die_ref c)
9765 return (is_type_die (c)
9766 || is_declaration_die (c)
9767 || c->die_tag == DW_TAG_namespace
9768 || c->die_tag == DW_TAG_module);
9771 /* Returns true iff C is a compile-unit DIE. */
9773 static inline bool
9774 is_cu_die (dw_die_ref c)
9776 return c && c->die_tag == DW_TAG_compile_unit;
9779 static char *
9780 gen_internal_sym (const char *prefix)
9782 char buf[256];
9784 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9785 return xstrdup (buf);
9788 /* Assign symbols to all worthy DIEs under DIE. */
9790 static void
9791 assign_symbol_names (dw_die_ref die)
9793 dw_die_ref c;
9795 if (is_symbol_die (die))
9797 if (comdat_symbol_id)
9799 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9801 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9802 comdat_symbol_id, comdat_symbol_number++);
9803 die->die_id.die_symbol = xstrdup (p);
9805 else
9806 die->die_id.die_symbol = gen_internal_sym ("LDIE");
9809 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9812 struct cu_hash_table_entry
9814 dw_die_ref cu;
9815 unsigned min_comdat_num, max_comdat_num;
9816 struct cu_hash_table_entry *next;
9819 /* Routines to manipulate hash table of CUs. */
9820 static hashval_t
9821 htab_cu_hash (const void *of)
9823 const struct cu_hash_table_entry *const entry =
9824 (const struct cu_hash_table_entry *) of;
9826 return htab_hash_string (entry->cu->die_id.die_symbol);
9829 static int
9830 htab_cu_eq (const void *of1, const void *of2)
9832 const struct cu_hash_table_entry *const entry1 =
9833 (const struct cu_hash_table_entry *) of1;
9834 const struct die_struct *const entry2 = (const struct die_struct *) of2;
9836 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9839 static void
9840 htab_cu_del (void *what)
9842 struct cu_hash_table_entry *next,
9843 *entry = (struct cu_hash_table_entry *) what;
9845 while (entry)
9847 next = entry->next;
9848 free (entry);
9849 entry = next;
9853 /* Check whether we have already seen this CU and set up SYM_NUM
9854 accordingly. */
9855 static int
9856 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9858 struct cu_hash_table_entry dummy;
9859 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9861 dummy.max_comdat_num = 0;
9863 slot = (struct cu_hash_table_entry **)
9864 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9865 INSERT);
9866 entry = *slot;
9868 for (; entry; last = entry, entry = entry->next)
9870 if (same_die_p_wrap (cu, entry->cu))
9871 break;
9874 if (entry)
9876 *sym_num = entry->min_comdat_num;
9877 return 1;
9880 entry = XCNEW (struct cu_hash_table_entry);
9881 entry->cu = cu;
9882 entry->min_comdat_num = *sym_num = last->max_comdat_num;
9883 entry->next = *slot;
9884 *slot = entry;
9886 return 0;
9889 /* Record SYM_NUM to record of CU in HTABLE. */
9890 static void
9891 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9893 struct cu_hash_table_entry **slot, *entry;
9895 slot = (struct cu_hash_table_entry **)
9896 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9897 NO_INSERT);
9898 entry = *slot;
9900 entry->max_comdat_num = sym_num;
9903 /* Traverse the DIE (which is always comp_unit_die), and set up
9904 additional compilation units for each of the include files we see
9905 bracketed by BINCL/EINCL. */
9907 static void
9908 break_out_includes (dw_die_ref die)
9910 dw_die_ref c;
9911 dw_die_ref unit = NULL;
9912 limbo_die_node *node, **pnode;
9913 htab_t cu_hash_table;
9915 c = die->die_child;
9916 if (c) do {
9917 dw_die_ref prev = c;
9918 c = c->die_sib;
9919 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9920 || (unit && is_comdat_die (c)))
9922 dw_die_ref next = c->die_sib;
9924 /* This DIE is for a secondary CU; remove it from the main one. */
9925 remove_child_with_prev (c, prev);
9927 if (c->die_tag == DW_TAG_GNU_BINCL)
9928 unit = push_new_compile_unit (unit, c);
9929 else if (c->die_tag == DW_TAG_GNU_EINCL)
9930 unit = pop_compile_unit (unit);
9931 else
9932 add_child_die (unit, c);
9933 c = next;
9934 if (c == die->die_child)
9935 break;
9937 } while (c != die->die_child);
9939 #if 0
9940 /* We can only use this in debugging, since the frontend doesn't check
9941 to make sure that we leave every include file we enter. */
9942 gcc_assert (!unit);
9943 #endif
9945 assign_symbol_names (die);
9946 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9947 for (node = limbo_die_list, pnode = &limbo_die_list;
9948 node;
9949 node = node->next)
9951 int is_dupl;
9953 compute_section_prefix (node->die);
9954 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9955 &comdat_symbol_number);
9956 assign_symbol_names (node->die);
9957 if (is_dupl)
9958 *pnode = node->next;
9959 else
9961 pnode = &node->next;
9962 record_comdat_symbol_number (node->die, cu_hash_table,
9963 comdat_symbol_number);
9966 htab_delete (cu_hash_table);
9969 /* Return non-zero if this DIE is a declaration. */
9971 static int
9972 is_declaration_die (dw_die_ref die)
9974 dw_attr_ref a;
9975 unsigned ix;
9977 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9978 if (a->dw_attr == DW_AT_declaration)
9979 return 1;
9981 return 0;
9984 /* Return non-zero if this DIE is nested inside a subprogram. */
9986 static int
9987 is_nested_in_subprogram (dw_die_ref die)
9989 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9991 if (decl == NULL)
9992 decl = die;
9993 return local_scope_p (decl);
9996 /* Return non-zero if this is a type DIE that should be moved to a
9997 COMDAT .debug_types section. */
9999 static int
10000 should_move_die_to_comdat (dw_die_ref die)
10002 switch (die->die_tag)
10004 case DW_TAG_class_type:
10005 case DW_TAG_structure_type:
10006 case DW_TAG_enumeration_type:
10007 case DW_TAG_union_type:
10008 /* Don't move declarations, inlined instances, or types nested in a
10009 subprogram. */
10010 if (is_declaration_die (die)
10011 || get_AT (die, DW_AT_abstract_origin)
10012 || is_nested_in_subprogram (die))
10013 return 0;
10014 return 1;
10015 case DW_TAG_array_type:
10016 case DW_TAG_interface_type:
10017 case DW_TAG_pointer_type:
10018 case DW_TAG_reference_type:
10019 case DW_TAG_rvalue_reference_type:
10020 case DW_TAG_string_type:
10021 case DW_TAG_subroutine_type:
10022 case DW_TAG_ptr_to_member_type:
10023 case DW_TAG_set_type:
10024 case DW_TAG_subrange_type:
10025 case DW_TAG_base_type:
10026 case DW_TAG_const_type:
10027 case DW_TAG_file_type:
10028 case DW_TAG_packed_type:
10029 case DW_TAG_volatile_type:
10030 case DW_TAG_typedef:
10031 default:
10032 return 0;
10036 /* Make a clone of DIE. */
10038 static dw_die_ref
10039 clone_die (dw_die_ref die)
10041 dw_die_ref clone;
10042 dw_attr_ref a;
10043 unsigned ix;
10045 clone = ggc_alloc_cleared_die_node ();
10046 clone->die_tag = die->die_tag;
10048 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10049 add_dwarf_attr (clone, a);
10051 return clone;
10054 /* Make a clone of the tree rooted at DIE. */
10056 static dw_die_ref
10057 clone_tree (dw_die_ref die)
10059 dw_die_ref c;
10060 dw_die_ref clone = clone_die (die);
10062 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10064 return clone;
10067 /* Make a clone of DIE as a declaration. */
10069 static dw_die_ref
10070 clone_as_declaration (dw_die_ref die)
10072 dw_die_ref clone;
10073 dw_die_ref decl;
10074 dw_attr_ref a;
10075 unsigned ix;
10077 /* If the DIE is already a declaration, just clone it. */
10078 if (is_declaration_die (die))
10079 return clone_die (die);
10081 /* If the DIE is a specification, just clone its declaration DIE. */
10082 decl = get_AT_ref (die, DW_AT_specification);
10083 if (decl != NULL)
10084 return clone_die (decl);
10086 clone = ggc_alloc_cleared_die_node ();
10087 clone->die_tag = die->die_tag;
10089 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10091 /* We don't want to copy over all attributes.
10092 For example we don't want DW_AT_byte_size because otherwise we will no
10093 longer have a declaration and GDB will treat it as a definition. */
10095 switch (a->dw_attr)
10097 case DW_AT_artificial:
10098 case DW_AT_containing_type:
10099 case DW_AT_external:
10100 case DW_AT_name:
10101 case DW_AT_type:
10102 case DW_AT_virtuality:
10103 case DW_AT_linkage_name:
10104 case DW_AT_MIPS_linkage_name:
10105 add_dwarf_attr (clone, a);
10106 break;
10107 case DW_AT_byte_size:
10108 default:
10109 break;
10113 if (die->die_id.die_type_node)
10114 add_AT_die_ref (clone, DW_AT_signature, die);
10116 add_AT_flag (clone, DW_AT_declaration, 1);
10117 return clone;
10120 /* Copy the declaration context to the new compile unit DIE. This includes
10121 any surrounding namespace or type declarations. If the DIE has an
10122 AT_specification attribute, it also includes attributes and children
10123 attached to the specification. */
10125 static void
10126 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10128 dw_die_ref decl;
10129 dw_die_ref new_decl;
10131 decl = get_AT_ref (die, DW_AT_specification);
10132 if (decl == NULL)
10133 decl = die;
10134 else
10136 unsigned ix;
10137 dw_die_ref c;
10138 dw_attr_ref a;
10140 /* Copy the type node pointer from the new DIE to the original
10141 declaration DIE so we can forward references later. */
10142 decl->die_id.die_type_node = die->die_id.die_type_node;
10144 remove_AT (die, DW_AT_specification);
10146 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10148 if (a->dw_attr != DW_AT_name
10149 && a->dw_attr != DW_AT_declaration
10150 && a->dw_attr != DW_AT_external)
10151 add_dwarf_attr (die, a);
10154 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10157 if (decl->die_parent != NULL
10158 && decl->die_parent->die_tag != DW_TAG_compile_unit
10159 && decl->die_parent->die_tag != DW_TAG_type_unit)
10161 new_decl = copy_ancestor_tree (unit, decl, NULL);
10162 if (new_decl != NULL)
10164 remove_AT (new_decl, DW_AT_signature);
10165 add_AT_specification (die, new_decl);
10170 /* Generate the skeleton ancestor tree for the given NODE, then clone
10171 the DIE and add the clone into the tree. */
10173 static void
10174 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10176 if (node->new_die != NULL)
10177 return;
10179 node->new_die = clone_as_declaration (node->old_die);
10181 if (node->parent != NULL)
10183 generate_skeleton_ancestor_tree (node->parent);
10184 add_child_die (node->parent->new_die, node->new_die);
10188 /* Generate a skeleton tree of DIEs containing any declarations that are
10189 found in the original tree. We traverse the tree looking for declaration
10190 DIEs, and construct the skeleton from the bottom up whenever we find one. */
10192 static void
10193 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10195 skeleton_chain_node node;
10196 dw_die_ref c;
10197 dw_die_ref first;
10198 dw_die_ref prev = NULL;
10199 dw_die_ref next = NULL;
10201 node.parent = parent;
10203 first = c = parent->old_die->die_child;
10204 if (c)
10205 next = c->die_sib;
10206 if (c) do {
10207 if (prev == NULL || prev->die_sib == c)
10208 prev = c;
10209 c = next;
10210 next = (c == first ? NULL : c->die_sib);
10211 node.old_die = c;
10212 node.new_die = NULL;
10213 if (is_declaration_die (c))
10215 /* Clone the existing DIE, move the original to the skeleton
10216 tree (which is in the main CU), and put the clone, with
10217 all the original's children, where the original came from. */
10218 dw_die_ref clone = clone_die (c);
10219 move_all_children (c, clone);
10221 replace_child (c, clone, prev);
10222 generate_skeleton_ancestor_tree (parent);
10223 add_child_die (parent->new_die, c);
10224 node.new_die = c;
10225 c = clone;
10227 generate_skeleton_bottom_up (&node);
10228 } while (next != NULL);
10231 /* Wrapper function for generate_skeleton_bottom_up. */
10233 static dw_die_ref
10234 generate_skeleton (dw_die_ref die)
10236 skeleton_chain_node node;
10238 node.old_die = die;
10239 node.new_die = NULL;
10240 node.parent = NULL;
10242 /* If this type definition is nested inside another type,
10243 always leave at least a declaration in its place. */
10244 if (die->die_parent != NULL && is_type_die (die->die_parent))
10245 node.new_die = clone_as_declaration (die);
10247 generate_skeleton_bottom_up (&node);
10248 return node.new_die;
10251 /* Remove the DIE from its parent, possibly replacing it with a cloned
10252 declaration. The original DIE will be moved to a new compile unit
10253 so that existing references to it follow it to the new location. If
10254 any of the original DIE's descendants is a declaration, we need to
10255 replace the original DIE with a skeleton tree and move the
10256 declarations back into the skeleton tree. */
10258 static dw_die_ref
10259 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10261 dw_die_ref skeleton;
10263 skeleton = generate_skeleton (child);
10264 if (skeleton == NULL)
10265 remove_child_with_prev (child, prev);
10266 else
10268 skeleton->die_id.die_type_node = child->die_id.die_type_node;
10269 replace_child (child, skeleton, prev);
10272 return skeleton;
10275 /* Traverse the DIE and set up additional .debug_types sections for each
10276 type worthy of being placed in a COMDAT section. */
10278 static void
10279 break_out_comdat_types (dw_die_ref die)
10281 dw_die_ref c;
10282 dw_die_ref first;
10283 dw_die_ref prev = NULL;
10284 dw_die_ref next = NULL;
10285 dw_die_ref unit = NULL;
10287 first = c = die->die_child;
10288 if (c)
10289 next = c->die_sib;
10290 if (c) do {
10291 if (prev == NULL || prev->die_sib == c)
10292 prev = c;
10293 c = next;
10294 next = (c == first ? NULL : c->die_sib);
10295 if (should_move_die_to_comdat (c))
10297 dw_die_ref replacement;
10298 comdat_type_node_ref type_node;
10300 /* Create a new type unit DIE as the root for the new tree, and
10301 add it to the list of comdat types. */
10302 unit = new_die (DW_TAG_type_unit, NULL, NULL);
10303 add_AT_unsigned (unit, DW_AT_language,
10304 get_AT_unsigned (comp_unit_die (), DW_AT_language));
10305 type_node = ggc_alloc_cleared_comdat_type_node ();
10306 type_node->root_die = unit;
10307 type_node->next = comdat_type_list;
10308 comdat_type_list = type_node;
10310 /* Generate the type signature. */
10311 generate_type_signature (c, type_node);
10313 /* Copy the declaration context, attributes, and children of the
10314 declaration into the new compile unit DIE. */
10315 copy_declaration_context (unit, c);
10317 /* Remove this DIE from the main CU. */
10318 replacement = remove_child_or_replace_with_skeleton (c, prev);
10320 /* Break out nested types into their own type units. */
10321 break_out_comdat_types (c);
10323 /* Add the DIE to the new compunit. */
10324 add_child_die (unit, c);
10326 if (replacement != NULL)
10327 c = replacement;
10329 else if (c->die_tag == DW_TAG_namespace
10330 || c->die_tag == DW_TAG_class_type
10331 || c->die_tag == DW_TAG_structure_type
10332 || c->die_tag == DW_TAG_union_type)
10334 /* Look for nested types that can be broken out. */
10335 break_out_comdat_types (c);
10337 } while (next != NULL);
10340 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
10342 struct decl_table_entry
10344 dw_die_ref orig;
10345 dw_die_ref copy;
10348 /* Routines to manipulate hash table of copied declarations. */
10350 static hashval_t
10351 htab_decl_hash (const void *of)
10353 const struct decl_table_entry *const entry =
10354 (const struct decl_table_entry *) of;
10356 return htab_hash_pointer (entry->orig);
10359 static int
10360 htab_decl_eq (const void *of1, const void *of2)
10362 const struct decl_table_entry *const entry1 =
10363 (const struct decl_table_entry *) of1;
10364 const struct die_struct *const entry2 = (const struct die_struct *) of2;
10366 return entry1->orig == entry2;
10369 static void
10370 htab_decl_del (void *what)
10372 struct decl_table_entry *entry = (struct decl_table_entry *) what;
10374 free (entry);
10377 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10378 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
10379 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
10380 to check if the ancestor has already been copied into UNIT. */
10382 static dw_die_ref
10383 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10385 dw_die_ref parent = die->die_parent;
10386 dw_die_ref new_parent = unit;
10387 dw_die_ref copy;
10388 void **slot = NULL;
10389 struct decl_table_entry *entry = NULL;
10391 if (decl_table)
10393 /* Check if the entry has already been copied to UNIT. */
10394 slot = htab_find_slot_with_hash (decl_table, die,
10395 htab_hash_pointer (die), INSERT);
10396 if (*slot != HTAB_EMPTY_ENTRY)
10398 entry = (struct decl_table_entry *) *slot;
10399 return entry->copy;
10402 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
10403 entry = XCNEW (struct decl_table_entry);
10404 entry->orig = die;
10405 entry->copy = NULL;
10406 *slot = entry;
10409 if (parent != NULL)
10411 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10412 if (spec != NULL)
10413 parent = spec;
10414 if (parent->die_tag != DW_TAG_compile_unit
10415 && parent->die_tag != DW_TAG_type_unit)
10416 new_parent = copy_ancestor_tree (unit, parent, decl_table);
10419 copy = clone_as_declaration (die);
10420 add_child_die (new_parent, copy);
10422 if (decl_table != NULL)
10424 /* Record the pointer to the copy. */
10425 entry->copy = copy;
10428 return copy;
10431 /* Walk the DIE and its children, looking for references to incomplete
10432 or trivial types that are unmarked (i.e., that are not in the current
10433 type_unit). */
10435 static void
10436 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10438 dw_die_ref c;
10439 dw_attr_ref a;
10440 unsigned ix;
10442 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10444 if (AT_class (a) == dw_val_class_die_ref)
10446 dw_die_ref targ = AT_ref (a);
10447 comdat_type_node_ref type_node = targ->die_id.die_type_node;
10448 void **slot;
10449 struct decl_table_entry *entry;
10451 if (targ->die_mark != 0 || type_node != NULL)
10452 continue;
10454 slot = htab_find_slot_with_hash (decl_table, targ,
10455 htab_hash_pointer (targ), INSERT);
10457 if (*slot != HTAB_EMPTY_ENTRY)
10459 /* TARG has already been copied, so we just need to
10460 modify the reference to point to the copy. */
10461 entry = (struct decl_table_entry *) *slot;
10462 a->dw_attr_val.v.val_die_ref.die = entry->copy;
10464 else
10466 dw_die_ref parent = unit;
10467 dw_die_ref copy = clone_tree (targ);
10469 /* Make sure the cloned tree is marked as part of the
10470 type unit. */
10471 mark_dies (copy);
10473 /* Record in DECL_TABLE that TARG has been copied.
10474 Need to do this now, before the recursive call,
10475 because DECL_TABLE may be expanded and SLOT
10476 would no longer be a valid pointer. */
10477 entry = XCNEW (struct decl_table_entry);
10478 entry->orig = targ;
10479 entry->copy = copy;
10480 *slot = entry;
10482 /* If TARG has surrounding context, copy its ancestor tree
10483 into the new type unit. */
10484 if (targ->die_parent != NULL
10485 && targ->die_parent->die_tag != DW_TAG_compile_unit
10486 && targ->die_parent->die_tag != DW_TAG_type_unit)
10487 parent = copy_ancestor_tree (unit, targ->die_parent,
10488 decl_table);
10490 add_child_die (parent, copy);
10491 a->dw_attr_val.v.val_die_ref.die = copy;
10493 /* Make sure the newly-copied DIE is walked. If it was
10494 installed in a previously-added context, it won't
10495 get visited otherwise. */
10496 if (parent != unit)
10498 /* Find the highest point of the newly-added tree,
10499 mark each node along the way, and walk from there. */
10500 parent->die_mark = 1;
10501 while (parent->die_parent
10502 && parent->die_parent->die_mark == 0)
10504 parent = parent->die_parent;
10505 parent->die_mark = 1;
10507 copy_decls_walk (unit, parent, decl_table);
10513 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10516 /* Copy declarations for "unworthy" types into the new comdat section.
10517 Incomplete types, modified types, and certain other types aren't broken
10518 out into comdat sections of their own, so they don't have a signature,
10519 and we need to copy the declaration into the same section so that we
10520 don't have an external reference. */
10522 static void
10523 copy_decls_for_unworthy_types (dw_die_ref unit)
10525 htab_t decl_table;
10527 mark_dies (unit);
10528 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10529 copy_decls_walk (unit, unit, decl_table);
10530 htab_delete (decl_table);
10531 unmark_dies (unit);
10534 /* Traverse the DIE and add a sibling attribute if it may have the
10535 effect of speeding up access to siblings. To save some space,
10536 avoid generating sibling attributes for DIE's without children. */
10538 static void
10539 add_sibling_attributes (dw_die_ref die)
10541 dw_die_ref c;
10543 if (! die->die_child)
10544 return;
10546 if (die->die_parent && die != die->die_parent->die_child)
10547 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10549 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10552 /* Output all location lists for the DIE and its children. */
10554 static void
10555 output_location_lists (dw_die_ref die)
10557 dw_die_ref c;
10558 dw_attr_ref a;
10559 unsigned ix;
10561 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10562 if (AT_class (a) == dw_val_class_loc_list)
10563 output_loc_list (AT_loc_list (a));
10565 FOR_EACH_CHILD (die, c, output_location_lists (c));
10568 /* The format of each DIE (and its attribute value pairs) is encoded in an
10569 abbreviation table. This routine builds the abbreviation table and assigns
10570 a unique abbreviation id for each abbreviation entry. The children of each
10571 die are visited recursively. */
10573 static void
10574 build_abbrev_table (dw_die_ref die)
10576 unsigned long abbrev_id;
10577 unsigned int n_alloc;
10578 dw_die_ref c;
10579 dw_attr_ref a;
10580 unsigned ix;
10582 /* Scan the DIE references, and mark as external any that refer to
10583 DIEs from other CUs (i.e. those which are not marked). */
10584 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10585 if (AT_class (a) == dw_val_class_die_ref
10586 && AT_ref (a)->die_mark == 0)
10588 gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10589 set_AT_ref_external (a, 1);
10592 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10594 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10595 dw_attr_ref die_a, abbrev_a;
10596 unsigned ix;
10597 bool ok = true;
10599 if (abbrev->die_tag != die->die_tag)
10600 continue;
10601 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10602 continue;
10604 if (VEC_length (dw_attr_node, abbrev->die_attr)
10605 != VEC_length (dw_attr_node, die->die_attr))
10606 continue;
10608 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10610 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10611 if ((abbrev_a->dw_attr != die_a->dw_attr)
10612 || (value_format (abbrev_a) != value_format (die_a)))
10614 ok = false;
10615 break;
10618 if (ok)
10619 break;
10622 if (abbrev_id >= abbrev_die_table_in_use)
10624 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10626 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10627 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10628 n_alloc);
10630 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10631 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10632 abbrev_die_table_allocated = n_alloc;
10635 ++abbrev_die_table_in_use;
10636 abbrev_die_table[abbrev_id] = die;
10639 die->die_abbrev = abbrev_id;
10640 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10643 /* Return the power-of-two number of bytes necessary to represent VALUE. */
10645 static int
10646 constant_size (unsigned HOST_WIDE_INT value)
10648 int log;
10650 if (value == 0)
10651 log = 0;
10652 else
10653 log = floor_log2 (value);
10655 log = log / 8;
10656 log = 1 << (floor_log2 (log) + 1);
10658 return log;
10661 /* Return the size of a DIE as it is represented in the
10662 .debug_info section. */
10664 static unsigned long
10665 size_of_die (dw_die_ref die)
10667 unsigned long size = 0;
10668 dw_attr_ref a;
10669 unsigned ix;
10671 size += size_of_uleb128 (die->die_abbrev);
10672 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10674 switch (AT_class (a))
10676 case dw_val_class_addr:
10677 size += DWARF2_ADDR_SIZE;
10678 break;
10679 case dw_val_class_offset:
10680 size += DWARF_OFFSET_SIZE;
10681 break;
10682 case dw_val_class_loc:
10684 unsigned long lsize = size_of_locs (AT_loc (a));
10686 /* Block length. */
10687 if (dwarf_version >= 4)
10688 size += size_of_uleb128 (lsize);
10689 else
10690 size += constant_size (lsize);
10691 size += lsize;
10693 break;
10694 case dw_val_class_loc_list:
10695 size += DWARF_OFFSET_SIZE;
10696 break;
10697 case dw_val_class_range_list:
10698 size += DWARF_OFFSET_SIZE;
10699 break;
10700 case dw_val_class_const:
10701 size += size_of_sleb128 (AT_int (a));
10702 break;
10703 case dw_val_class_unsigned_const:
10704 size += constant_size (AT_unsigned (a));
10705 break;
10706 case dw_val_class_const_double:
10707 size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10708 if (HOST_BITS_PER_WIDE_INT >= 64)
10709 size++; /* block */
10710 break;
10711 case dw_val_class_vec:
10712 size += constant_size (a->dw_attr_val.v.val_vec.length
10713 * a->dw_attr_val.v.val_vec.elt_size)
10714 + a->dw_attr_val.v.val_vec.length
10715 * a->dw_attr_val.v.val_vec.elt_size; /* block */
10716 break;
10717 case dw_val_class_flag:
10718 if (dwarf_version >= 4)
10719 /* Currently all add_AT_flag calls pass in 1 as last argument,
10720 so DW_FORM_flag_present can be used. If that ever changes,
10721 we'll need to use DW_FORM_flag and have some optimization
10722 in build_abbrev_table that will change those to
10723 DW_FORM_flag_present if it is set to 1 in all DIEs using
10724 the same abbrev entry. */
10725 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10726 else
10727 size += 1;
10728 break;
10729 case dw_val_class_die_ref:
10730 if (AT_ref_external (a))
10732 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10733 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
10734 is sized by target address length, whereas in DWARF3
10735 it's always sized as an offset. */
10736 if (use_debug_types)
10737 size += DWARF_TYPE_SIGNATURE_SIZE;
10738 else if (dwarf_version == 2)
10739 size += DWARF2_ADDR_SIZE;
10740 else
10741 size += DWARF_OFFSET_SIZE;
10743 else
10744 size += DWARF_OFFSET_SIZE;
10745 break;
10746 case dw_val_class_fde_ref:
10747 size += DWARF_OFFSET_SIZE;
10748 break;
10749 case dw_val_class_lbl_id:
10750 size += DWARF2_ADDR_SIZE;
10751 break;
10752 case dw_val_class_lineptr:
10753 case dw_val_class_macptr:
10754 size += DWARF_OFFSET_SIZE;
10755 break;
10756 case dw_val_class_str:
10757 if (AT_string_form (a) == DW_FORM_strp)
10758 size += DWARF_OFFSET_SIZE;
10759 else
10760 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10761 break;
10762 case dw_val_class_file:
10763 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10764 break;
10765 case dw_val_class_data8:
10766 size += 8;
10767 break;
10768 case dw_val_class_vms_delta:
10769 size += DWARF_OFFSET_SIZE;
10770 break;
10771 default:
10772 gcc_unreachable ();
10776 return size;
10779 /* Size the debugging information associated with a given DIE. Visits the
10780 DIE's children recursively. Updates the global variable next_die_offset, on
10781 each time through. Uses the current value of next_die_offset to update the
10782 die_offset field in each DIE. */
10784 static void
10785 calc_die_sizes (dw_die_ref die)
10787 dw_die_ref c;
10789 die->die_offset = next_die_offset;
10790 next_die_offset += size_of_die (die);
10792 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10794 if (die->die_child != NULL)
10795 /* Count the null byte used to terminate sibling lists. */
10796 next_die_offset += 1;
10799 /* Set the marks for a die and its children. We do this so
10800 that we know whether or not a reference needs to use FORM_ref_addr; only
10801 DIEs in the same CU will be marked. We used to clear out the offset
10802 and use that as the flag, but ran into ordering problems. */
10804 static void
10805 mark_dies (dw_die_ref die)
10807 dw_die_ref c;
10809 gcc_assert (!die->die_mark);
10811 die->die_mark = 1;
10812 FOR_EACH_CHILD (die, c, mark_dies (c));
10815 /* Clear the marks for a die and its children. */
10817 static void
10818 unmark_dies (dw_die_ref die)
10820 dw_die_ref c;
10822 if (! use_debug_types)
10823 gcc_assert (die->die_mark);
10825 die->die_mark = 0;
10826 FOR_EACH_CHILD (die, c, unmark_dies (c));
10829 /* Clear the marks for a die, its children and referred dies. */
10831 static void
10832 unmark_all_dies (dw_die_ref die)
10834 dw_die_ref c;
10835 dw_attr_ref a;
10836 unsigned ix;
10838 if (!die->die_mark)
10839 return;
10840 die->die_mark = 0;
10842 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10844 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10845 if (AT_class (a) == dw_val_class_die_ref)
10846 unmark_all_dies (AT_ref (a));
10849 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10850 generated for the compilation unit. */
10852 static unsigned long
10853 size_of_pubnames (VEC (pubname_entry, gc) * names)
10855 unsigned long size;
10856 unsigned i;
10857 pubname_ref p;
10859 size = DWARF_PUBNAMES_HEADER_SIZE;
10860 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10861 if (names != pubtype_table
10862 || p->die->die_offset != 0
10863 || !flag_eliminate_unused_debug_types)
10864 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10866 size += DWARF_OFFSET_SIZE;
10867 return size;
10870 /* Return the size of the information in the .debug_aranges section. */
10872 static unsigned long
10873 size_of_aranges (void)
10875 unsigned long size;
10877 size = DWARF_ARANGES_HEADER_SIZE;
10879 /* Count the address/length pair for this compilation unit. */
10880 if (text_section_used)
10881 size += 2 * DWARF2_ADDR_SIZE;
10882 if (cold_text_section_used)
10883 size += 2 * DWARF2_ADDR_SIZE;
10884 if (have_multiple_function_sections)
10886 unsigned fde_idx = 0;
10888 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
10890 dw_fde_ref fde = &fde_table[fde_idx];
10892 if (!fde->in_std_section)
10893 size += 2 * DWARF2_ADDR_SIZE;
10894 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10895 size += 2 * DWARF2_ADDR_SIZE;
10899 /* Count the two zero words used to terminated the address range table. */
10900 size += 2 * DWARF2_ADDR_SIZE;
10901 return size;
10904 /* Select the encoding of an attribute value. */
10906 static enum dwarf_form
10907 value_format (dw_attr_ref a)
10909 switch (a->dw_attr_val.val_class)
10911 case dw_val_class_addr:
10912 /* Only very few attributes allow DW_FORM_addr. */
10913 switch (a->dw_attr)
10915 case DW_AT_low_pc:
10916 case DW_AT_high_pc:
10917 case DW_AT_entry_pc:
10918 case DW_AT_trampoline:
10919 return DW_FORM_addr;
10920 default:
10921 break;
10923 switch (DWARF2_ADDR_SIZE)
10925 case 1:
10926 return DW_FORM_data1;
10927 case 2:
10928 return DW_FORM_data2;
10929 case 4:
10930 return DW_FORM_data4;
10931 case 8:
10932 return DW_FORM_data8;
10933 default:
10934 gcc_unreachable ();
10936 case dw_val_class_range_list:
10937 case dw_val_class_loc_list:
10938 if (dwarf_version >= 4)
10939 return DW_FORM_sec_offset;
10940 /* FALLTHRU */
10941 case dw_val_class_vms_delta:
10942 case dw_val_class_offset:
10943 switch (DWARF_OFFSET_SIZE)
10945 case 4:
10946 return DW_FORM_data4;
10947 case 8:
10948 return DW_FORM_data8;
10949 default:
10950 gcc_unreachable ();
10952 case dw_val_class_loc:
10953 if (dwarf_version >= 4)
10954 return DW_FORM_exprloc;
10955 switch (constant_size (size_of_locs (AT_loc (a))))
10957 case 1:
10958 return DW_FORM_block1;
10959 case 2:
10960 return DW_FORM_block2;
10961 default:
10962 gcc_unreachable ();
10964 case dw_val_class_const:
10965 return DW_FORM_sdata;
10966 case dw_val_class_unsigned_const:
10967 switch (constant_size (AT_unsigned (a)))
10969 case 1:
10970 return DW_FORM_data1;
10971 case 2:
10972 return DW_FORM_data2;
10973 case 4:
10974 return DW_FORM_data4;
10975 case 8:
10976 return DW_FORM_data8;
10977 default:
10978 gcc_unreachable ();
10980 case dw_val_class_const_double:
10981 switch (HOST_BITS_PER_WIDE_INT)
10983 case 8:
10984 return DW_FORM_data2;
10985 case 16:
10986 return DW_FORM_data4;
10987 case 32:
10988 return DW_FORM_data8;
10989 case 64:
10990 default:
10991 return DW_FORM_block1;
10993 case dw_val_class_vec:
10994 switch (constant_size (a->dw_attr_val.v.val_vec.length
10995 * a->dw_attr_val.v.val_vec.elt_size))
10997 case 1:
10998 return DW_FORM_block1;
10999 case 2:
11000 return DW_FORM_block2;
11001 case 4:
11002 return DW_FORM_block4;
11003 default:
11004 gcc_unreachable ();
11006 case dw_val_class_flag:
11007 if (dwarf_version >= 4)
11009 /* Currently all add_AT_flag calls pass in 1 as last argument,
11010 so DW_FORM_flag_present can be used. If that ever changes,
11011 we'll need to use DW_FORM_flag and have some optimization
11012 in build_abbrev_table that will change those to
11013 DW_FORM_flag_present if it is set to 1 in all DIEs using
11014 the same abbrev entry. */
11015 gcc_assert (a->dw_attr_val.v.val_flag == 1);
11016 return DW_FORM_flag_present;
11018 return DW_FORM_flag;
11019 case dw_val_class_die_ref:
11020 if (AT_ref_external (a))
11021 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11022 else
11023 return DW_FORM_ref;
11024 case dw_val_class_fde_ref:
11025 return DW_FORM_data;
11026 case dw_val_class_lbl_id:
11027 return DW_FORM_addr;
11028 case dw_val_class_lineptr:
11029 case dw_val_class_macptr:
11030 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11031 case dw_val_class_str:
11032 return AT_string_form (a);
11033 case dw_val_class_file:
11034 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11036 case 1:
11037 return DW_FORM_data1;
11038 case 2:
11039 return DW_FORM_data2;
11040 case 4:
11041 return DW_FORM_data4;
11042 default:
11043 gcc_unreachable ();
11046 case dw_val_class_data8:
11047 return DW_FORM_data8;
11049 default:
11050 gcc_unreachable ();
11054 /* Output the encoding of an attribute value. */
11056 static void
11057 output_value_format (dw_attr_ref a)
11059 enum dwarf_form form = value_format (a);
11061 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11064 /* Output the .debug_abbrev section which defines the DIE abbreviation
11065 table. */
11067 static void
11068 output_abbrev_section (void)
11070 unsigned long abbrev_id;
11072 if (abbrev_die_table_in_use == 1)
11073 return;
11075 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11077 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11078 unsigned ix;
11079 dw_attr_ref a_attr;
11081 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11082 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11083 dwarf_tag_name (abbrev->die_tag));
11085 if (abbrev->die_child != NULL)
11086 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11087 else
11088 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11090 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11091 ix++)
11093 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11094 dwarf_attr_name (a_attr->dw_attr));
11095 output_value_format (a_attr);
11098 dw2_asm_output_data (1, 0, NULL);
11099 dw2_asm_output_data (1, 0, NULL);
11102 /* Terminate the table. */
11103 dw2_asm_output_data (1, 0, NULL);
11106 /* Output a symbol we can use to refer to this DIE from another CU. */
11108 static inline void
11109 output_die_symbol (dw_die_ref die)
11111 char *sym = die->die_id.die_symbol;
11113 if (sym == 0)
11114 return;
11116 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11117 /* We make these global, not weak; if the target doesn't support
11118 .linkonce, it doesn't support combining the sections, so debugging
11119 will break. */
11120 targetm.asm_out.globalize_label (asm_out_file, sym);
11122 ASM_OUTPUT_LABEL (asm_out_file, sym);
11125 /* Return a new location list, given the begin and end range, and the
11126 expression. */
11128 static inline dw_loc_list_ref
11129 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11130 const char *section)
11132 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11134 retlist->begin = begin;
11135 retlist->end = end;
11136 retlist->expr = expr;
11137 retlist->section = section;
11139 return retlist;
11142 /* Generate a new internal symbol for this location list node, if it
11143 hasn't got one yet. */
11145 static inline void
11146 gen_llsym (dw_loc_list_ref list)
11148 gcc_assert (!list->ll_symbol);
11149 list->ll_symbol = gen_internal_sym ("LLST");
11152 /* Output the location list given to us. */
11154 static void
11155 output_loc_list (dw_loc_list_ref list_head)
11157 dw_loc_list_ref curr = list_head;
11159 if (list_head->emitted)
11160 return;
11161 list_head->emitted = true;
11163 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11165 /* Walk the location list, and output each range + expression. */
11166 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11168 unsigned long size;
11169 /* Don't output an entry that starts and ends at the same address. */
11170 if (strcmp (curr->begin, curr->end) == 0)
11171 continue;
11172 if (!have_multiple_function_sections)
11174 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11175 "Location list begin address (%s)",
11176 list_head->ll_symbol);
11177 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11178 "Location list end address (%s)",
11179 list_head->ll_symbol);
11181 else
11183 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11184 "Location list begin address (%s)",
11185 list_head->ll_symbol);
11186 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11187 "Location list end address (%s)",
11188 list_head->ll_symbol);
11190 size = size_of_locs (curr->expr);
11192 /* Output the block length for this list of location operations. */
11193 gcc_assert (size <= 0xffff);
11194 dw2_asm_output_data (2, size, "%s", "Location expression size");
11196 output_loc_sequence (curr->expr, -1);
11199 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11200 "Location list terminator begin (%s)",
11201 list_head->ll_symbol);
11202 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11203 "Location list terminator end (%s)",
11204 list_head->ll_symbol);
11207 /* Output a type signature. */
11209 static inline void
11210 output_signature (const char *sig, const char *name)
11212 int i;
11214 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11215 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11218 /* Output the DIE and its attributes. Called recursively to generate
11219 the definitions of each child DIE. */
11221 static void
11222 output_die (dw_die_ref die)
11224 dw_attr_ref a;
11225 dw_die_ref c;
11226 unsigned long size;
11227 unsigned ix;
11229 /* If someone in another CU might refer to us, set up a symbol for
11230 them to point to. */
11231 if (! use_debug_types && die->die_id.die_symbol)
11232 output_die_symbol (die);
11234 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11235 (unsigned long)die->die_offset,
11236 dwarf_tag_name (die->die_tag));
11238 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11240 const char *name = dwarf_attr_name (a->dw_attr);
11242 switch (AT_class (a))
11244 case dw_val_class_addr:
11245 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11246 break;
11248 case dw_val_class_offset:
11249 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11250 "%s", name);
11251 break;
11253 case dw_val_class_range_list:
11255 char *p = strchr (ranges_section_label, '\0');
11257 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11258 a->dw_attr_val.v.val_offset);
11259 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11260 debug_ranges_section, "%s", name);
11261 *p = '\0';
11263 break;
11265 case dw_val_class_loc:
11266 size = size_of_locs (AT_loc (a));
11268 /* Output the block length for this list of location operations. */
11269 if (dwarf_version >= 4)
11270 dw2_asm_output_data_uleb128 (size, "%s", name);
11271 else
11272 dw2_asm_output_data (constant_size (size), size, "%s", name);
11274 output_loc_sequence (AT_loc (a), -1);
11275 break;
11277 case dw_val_class_const:
11278 /* ??? It would be slightly more efficient to use a scheme like is
11279 used for unsigned constants below, but gdb 4.x does not sign
11280 extend. Gdb 5.x does sign extend. */
11281 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11282 break;
11284 case dw_val_class_unsigned_const:
11285 dw2_asm_output_data (constant_size (AT_unsigned (a)),
11286 AT_unsigned (a), "%s", name);
11287 break;
11289 case dw_val_class_const_double:
11291 unsigned HOST_WIDE_INT first, second;
11293 if (HOST_BITS_PER_WIDE_INT >= 64)
11294 dw2_asm_output_data (1,
11295 2 * HOST_BITS_PER_WIDE_INT
11296 / HOST_BITS_PER_CHAR,
11297 NULL);
11299 if (WORDS_BIG_ENDIAN)
11301 first = a->dw_attr_val.v.val_double.high;
11302 second = a->dw_attr_val.v.val_double.low;
11304 else
11306 first = a->dw_attr_val.v.val_double.low;
11307 second = a->dw_attr_val.v.val_double.high;
11310 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11311 first, name);
11312 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11313 second, NULL);
11315 break;
11317 case dw_val_class_vec:
11319 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11320 unsigned int len = a->dw_attr_val.v.val_vec.length;
11321 unsigned int i;
11322 unsigned char *p;
11324 dw2_asm_output_data (constant_size (len * elt_size),
11325 len * elt_size, "%s", name);
11326 if (elt_size > sizeof (HOST_WIDE_INT))
11328 elt_size /= 2;
11329 len *= 2;
11331 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11332 i < len;
11333 i++, p += elt_size)
11334 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11335 "fp or vector constant word %u", i);
11336 break;
11339 case dw_val_class_flag:
11340 if (dwarf_version >= 4)
11342 /* Currently all add_AT_flag calls pass in 1 as last argument,
11343 so DW_FORM_flag_present can be used. If that ever changes,
11344 we'll need to use DW_FORM_flag and have some optimization
11345 in build_abbrev_table that will change those to
11346 DW_FORM_flag_present if it is set to 1 in all DIEs using
11347 the same abbrev entry. */
11348 gcc_assert (AT_flag (a) == 1);
11349 if (flag_debug_asm)
11350 fprintf (asm_out_file, "\t\t\t%s %s\n",
11351 ASM_COMMENT_START, name);
11352 break;
11354 dw2_asm_output_data (1, AT_flag (a), "%s", name);
11355 break;
11357 case dw_val_class_loc_list:
11359 char *sym = AT_loc_list (a)->ll_symbol;
11361 gcc_assert (sym);
11362 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11363 "%s", name);
11365 break;
11367 case dw_val_class_die_ref:
11368 if (AT_ref_external (a))
11370 if (use_debug_types)
11372 comdat_type_node_ref type_node =
11373 AT_ref (a)->die_id.die_type_node;
11375 gcc_assert (type_node);
11376 output_signature (type_node->signature, name);
11378 else
11380 char *sym = AT_ref (a)->die_id.die_symbol;
11381 int size;
11383 gcc_assert (sym);
11384 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11385 length, whereas in DWARF3 it's always sized as an
11386 offset. */
11387 if (dwarf_version == 2)
11388 size = DWARF2_ADDR_SIZE;
11389 else
11390 size = DWARF_OFFSET_SIZE;
11391 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11392 name);
11395 else
11397 gcc_assert (AT_ref (a)->die_offset);
11398 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11399 "%s", name);
11401 break;
11403 case dw_val_class_fde_ref:
11405 char l1[20];
11407 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11408 a->dw_attr_val.v.val_fde_index * 2);
11409 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11410 "%s", name);
11412 break;
11414 case dw_val_class_vms_delta:
11415 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11416 AT_vms_delta2 (a), AT_vms_delta1 (a),
11417 "%s", name);
11418 break;
11420 case dw_val_class_lbl_id:
11421 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11422 break;
11424 case dw_val_class_lineptr:
11425 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11426 debug_line_section, "%s", name);
11427 break;
11429 case dw_val_class_macptr:
11430 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11431 debug_macinfo_section, "%s", name);
11432 break;
11434 case dw_val_class_str:
11435 if (AT_string_form (a) == DW_FORM_strp)
11436 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11437 a->dw_attr_val.v.val_str->label,
11438 debug_str_section,
11439 "%s: \"%s\"", name, AT_string (a));
11440 else
11441 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11442 break;
11444 case dw_val_class_file:
11446 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11448 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11449 a->dw_attr_val.v.val_file->filename);
11450 break;
11453 case dw_val_class_data8:
11455 int i;
11457 for (i = 0; i < 8; i++)
11458 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11459 i == 0 ? "%s" : NULL, name);
11460 break;
11463 default:
11464 gcc_unreachable ();
11468 FOR_EACH_CHILD (die, c, output_die (c));
11470 /* Add null byte to terminate sibling list. */
11471 if (die->die_child != NULL)
11472 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11473 (unsigned long) die->die_offset);
11476 /* Output the compilation unit that appears at the beginning of the
11477 .debug_info section, and precedes the DIE descriptions. */
11479 static void
11480 output_compilation_unit_header (void)
11482 int ver = dwarf_version;
11484 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11485 dw2_asm_output_data (4, 0xffffffff,
11486 "Initial length escape value indicating 64-bit DWARF extension");
11487 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11488 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11489 "Length of Compilation Unit Info");
11490 dw2_asm_output_data (2, ver, "DWARF version number");
11491 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11492 debug_abbrev_section,
11493 "Offset Into Abbrev. Section");
11494 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11497 /* Output the compilation unit DIE and its children. */
11499 static void
11500 output_comp_unit (dw_die_ref die, int output_if_empty)
11502 const char *secname;
11503 char *oldsym, *tmp;
11505 /* Unless we are outputting main CU, we may throw away empty ones. */
11506 if (!output_if_empty && die->die_child == NULL)
11507 return;
11509 /* Even if there are no children of this DIE, we must output the information
11510 about the compilation unit. Otherwise, on an empty translation unit, we
11511 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11512 will then complain when examining the file. First mark all the DIEs in
11513 this CU so we know which get local refs. */
11514 mark_dies (die);
11516 build_abbrev_table (die);
11518 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11519 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11520 calc_die_sizes (die);
11522 oldsym = die->die_id.die_symbol;
11523 if (oldsym)
11525 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11527 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11528 secname = tmp;
11529 die->die_id.die_symbol = NULL;
11530 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11532 else
11534 switch_to_section (debug_info_section);
11535 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11536 info_section_emitted = true;
11539 /* Output debugging information. */
11540 output_compilation_unit_header ();
11541 output_die (die);
11543 /* Leave the marks on the main CU, so we can check them in
11544 output_pubnames. */
11545 if (oldsym)
11547 unmark_dies (die);
11548 die->die_id.die_symbol = oldsym;
11552 /* Output a comdat type unit DIE and its children. */
11554 static void
11555 output_comdat_type_unit (comdat_type_node *node)
11557 const char *secname;
11558 char *tmp;
11559 int i;
11560 #if defined (OBJECT_FORMAT_ELF)
11561 tree comdat_key;
11562 #endif
11564 /* First mark all the DIEs in this CU so we know which get local refs. */
11565 mark_dies (node->root_die);
11567 build_abbrev_table (node->root_die);
11569 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11570 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11571 calc_die_sizes (node->root_die);
11573 #if defined (OBJECT_FORMAT_ELF)
11574 secname = ".debug_types";
11575 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11576 sprintf (tmp, "wt.");
11577 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11578 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11579 comdat_key = get_identifier (tmp);
11580 targetm.asm_out.named_section (secname,
11581 SECTION_DEBUG | SECTION_LINKONCE,
11582 comdat_key);
11583 #else
11584 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11585 sprintf (tmp, ".gnu.linkonce.wt.");
11586 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11587 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11588 secname = tmp;
11589 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11590 #endif
11592 /* Output debugging information. */
11593 output_compilation_unit_header ();
11594 output_signature (node->signature, "Type Signature");
11595 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11596 "Offset to Type DIE");
11597 output_die (node->root_die);
11599 unmark_dies (node->root_die);
11602 /* Return the DWARF2/3 pubname associated with a decl. */
11604 static const char *
11605 dwarf2_name (tree decl, int scope)
11607 if (DECL_NAMELESS (decl))
11608 return NULL;
11609 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11612 /* Add a new entry to .debug_pubnames if appropriate. */
11614 static void
11615 add_pubname_string (const char *str, dw_die_ref die)
11617 if (targetm.want_debug_pub_sections)
11619 pubname_entry e;
11621 e.die = die;
11622 e.name = xstrdup (str);
11623 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11627 static void
11628 add_pubname (tree decl, dw_die_ref die)
11630 if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11632 const char *name = dwarf2_name (decl, 1);
11633 if (name)
11634 add_pubname_string (name, die);
11638 /* Add a new entry to .debug_pubtypes if appropriate. */
11640 static void
11641 add_pubtype (tree decl, dw_die_ref die)
11643 pubname_entry e;
11645 if (!targetm.want_debug_pub_sections)
11646 return;
11648 e.name = NULL;
11649 if ((TREE_PUBLIC (decl)
11650 || is_cu_die (die->die_parent))
11651 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11653 e.die = die;
11654 if (TYPE_P (decl))
11656 if (TYPE_NAME (decl))
11658 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11659 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11660 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11661 && DECL_NAME (TYPE_NAME (decl)))
11662 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11663 else
11664 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11667 else
11669 e.name = dwarf2_name (decl, 1);
11670 if (e.name)
11671 e.name = xstrdup (e.name);
11674 /* If we don't have a name for the type, there's no point in adding
11675 it to the table. */
11676 if (e.name && e.name[0] != '\0')
11677 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11681 /* Output the public names table used to speed up access to externally
11682 visible names; or the public types table used to find type definitions. */
11684 static void
11685 output_pubnames (VEC (pubname_entry, gc) * names)
11687 unsigned i;
11688 unsigned long pubnames_length = size_of_pubnames (names);
11689 pubname_ref pub;
11691 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11692 dw2_asm_output_data (4, 0xffffffff,
11693 "Initial length escape value indicating 64-bit DWARF extension");
11694 if (names == pubname_table)
11695 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11696 "Length of Public Names Info");
11697 else
11698 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11699 "Length of Public Type Names Info");
11700 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
11701 dw2_asm_output_data (2, 2, "DWARF Version");
11702 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11703 debug_info_section,
11704 "Offset of Compilation Unit Info");
11705 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11706 "Compilation Unit Length");
11708 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11710 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11711 if (names == pubname_table)
11712 gcc_assert (pub->die->die_mark);
11714 if (names != pubtype_table
11715 || pub->die->die_offset != 0
11716 || !flag_eliminate_unused_debug_types)
11718 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11719 "DIE offset");
11721 dw2_asm_output_nstring (pub->name, -1, "external name");
11725 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11728 /* Output the information that goes into the .debug_aranges table.
11729 Namely, define the beginning and ending address range of the
11730 text section generated for this compilation unit. */
11732 static void
11733 output_aranges (unsigned long aranges_length)
11735 unsigned i;
11737 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11738 dw2_asm_output_data (4, 0xffffffff,
11739 "Initial length escape value indicating 64-bit DWARF extension");
11740 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11741 "Length of Address Ranges Info");
11742 /* Version number for aranges is still 2, even in DWARF3. */
11743 dw2_asm_output_data (2, 2, "DWARF Version");
11744 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11745 debug_info_section,
11746 "Offset of Compilation Unit Info");
11747 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11748 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11750 /* We need to align to twice the pointer size here. */
11751 if (DWARF_ARANGES_PAD_SIZE)
11753 /* Pad using a 2 byte words so that padding is correct for any
11754 pointer size. */
11755 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11756 2 * DWARF2_ADDR_SIZE);
11757 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11758 dw2_asm_output_data (2, 0, NULL);
11761 /* It is necessary not to output these entries if the sections were
11762 not used; if the sections were not used, the length will be 0 and
11763 the address may end up as 0 if the section is discarded by ld
11764 --gc-sections, leaving an invalid (0, 0) entry that can be
11765 confused with the terminator. */
11766 if (text_section_used)
11768 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11769 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11770 text_section_label, "Length");
11772 if (cold_text_section_used)
11774 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11775 "Address");
11776 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11777 cold_text_section_label, "Length");
11780 if (have_multiple_function_sections)
11782 unsigned fde_idx = 0;
11784 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11786 dw_fde_ref fde = &fde_table[fde_idx];
11788 if (!fde->in_std_section)
11790 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11791 "Address");
11792 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11793 fde->dw_fde_begin, "Length");
11795 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11797 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11798 "Address");
11799 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11800 fde->dw_fde_second_begin, "Length");
11805 /* Output the terminator words. */
11806 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11807 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11810 /* Add a new entry to .debug_ranges. Return the offset at which it
11811 was placed. */
11813 static unsigned int
11814 add_ranges_num (int num)
11816 unsigned int in_use = ranges_table_in_use;
11818 if (in_use == ranges_table_allocated)
11820 ranges_table_allocated += RANGES_TABLE_INCREMENT;
11821 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11822 ranges_table_allocated);
11823 memset (ranges_table + ranges_table_in_use, 0,
11824 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11827 ranges_table[in_use].num = num;
11828 ranges_table_in_use = in_use + 1;
11830 return in_use * 2 * DWARF2_ADDR_SIZE;
11833 /* Add a new entry to .debug_ranges corresponding to a block, or a
11834 range terminator if BLOCK is NULL. */
11836 static unsigned int
11837 add_ranges (const_tree block)
11839 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11842 /* Add a new entry to .debug_ranges corresponding to a pair of
11843 labels. */
11845 static void
11846 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11847 bool *added)
11849 unsigned int in_use = ranges_by_label_in_use;
11850 unsigned int offset;
11852 if (in_use == ranges_by_label_allocated)
11854 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11855 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11856 ranges_by_label,
11857 ranges_by_label_allocated);
11858 memset (ranges_by_label + ranges_by_label_in_use, 0,
11859 RANGES_TABLE_INCREMENT
11860 * sizeof (struct dw_ranges_by_label_struct));
11863 ranges_by_label[in_use].begin = begin;
11864 ranges_by_label[in_use].end = end;
11865 ranges_by_label_in_use = in_use + 1;
11867 offset = add_ranges_num (-(int)in_use - 1);
11868 if (!*added)
11870 add_AT_range_list (die, DW_AT_ranges, offset);
11871 *added = true;
11875 static void
11876 output_ranges (void)
11878 unsigned i;
11879 static const char *const start_fmt = "Offset %#x";
11880 const char *fmt = start_fmt;
11882 for (i = 0; i < ranges_table_in_use; i++)
11884 int block_num = ranges_table[i].num;
11886 if (block_num > 0)
11888 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11889 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11891 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11892 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11894 /* If all code is in the text section, then the compilation
11895 unit base address defaults to DW_AT_low_pc, which is the
11896 base of the text section. */
11897 if (!have_multiple_function_sections)
11899 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11900 text_section_label,
11901 fmt, i * 2 * DWARF2_ADDR_SIZE);
11902 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11903 text_section_label, NULL);
11906 /* Otherwise, the compilation unit base address is zero,
11907 which allows us to use absolute addresses, and not worry
11908 about whether the target supports cross-section
11909 arithmetic. */
11910 else
11912 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11913 fmt, i * 2 * DWARF2_ADDR_SIZE);
11914 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11917 fmt = NULL;
11920 /* Negative block_num stands for an index into ranges_by_label. */
11921 else if (block_num < 0)
11923 int lab_idx = - block_num - 1;
11925 if (!have_multiple_function_sections)
11927 gcc_unreachable ();
11928 #if 0
11929 /* If we ever use add_ranges_by_labels () for a single
11930 function section, all we have to do is to take out
11931 the #if 0 above. */
11932 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11933 ranges_by_label[lab_idx].begin,
11934 text_section_label,
11935 fmt, i * 2 * DWARF2_ADDR_SIZE);
11936 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11937 ranges_by_label[lab_idx].end,
11938 text_section_label, NULL);
11939 #endif
11941 else
11943 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11944 ranges_by_label[lab_idx].begin,
11945 fmt, i * 2 * DWARF2_ADDR_SIZE);
11946 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11947 ranges_by_label[lab_idx].end,
11948 NULL);
11951 else
11953 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11954 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11955 fmt = start_fmt;
11960 /* Data structure containing information about input files. */
11961 struct file_info
11963 const char *path; /* Complete file name. */
11964 const char *fname; /* File name part. */
11965 int length; /* Length of entire string. */
11966 struct dwarf_file_data * file_idx; /* Index in input file table. */
11967 int dir_idx; /* Index in directory table. */
11970 /* Data structure containing information about directories with source
11971 files. */
11972 struct dir_info
11974 const char *path; /* Path including directory name. */
11975 int length; /* Path length. */
11976 int prefix; /* Index of directory entry which is a prefix. */
11977 int count; /* Number of files in this directory. */
11978 int dir_idx; /* Index of directory used as base. */
11981 /* Callback function for file_info comparison. We sort by looking at
11982 the directories in the path. */
11984 static int
11985 file_info_cmp (const void *p1, const void *p2)
11987 const struct file_info *const s1 = (const struct file_info *) p1;
11988 const struct file_info *const s2 = (const struct file_info *) p2;
11989 const unsigned char *cp1;
11990 const unsigned char *cp2;
11992 /* Take care of file names without directories. We need to make sure that
11993 we return consistent values to qsort since some will get confused if
11994 we return the same value when identical operands are passed in opposite
11995 orders. So if neither has a directory, return 0 and otherwise return
11996 1 or -1 depending on which one has the directory. */
11997 if ((s1->path == s1->fname || s2->path == s2->fname))
11998 return (s2->path == s2->fname) - (s1->path == s1->fname);
12000 cp1 = (const unsigned char *) s1->path;
12001 cp2 = (const unsigned char *) s2->path;
12003 while (1)
12005 ++cp1;
12006 ++cp2;
12007 /* Reached the end of the first path? If so, handle like above. */
12008 if ((cp1 == (const unsigned char *) s1->fname)
12009 || (cp2 == (const unsigned char *) s2->fname))
12010 return ((cp2 == (const unsigned char *) s2->fname)
12011 - (cp1 == (const unsigned char *) s1->fname));
12013 /* Character of current path component the same? */
12014 else if (*cp1 != *cp2)
12015 return *cp1 - *cp2;
12019 struct file_name_acquire_data
12021 struct file_info *files;
12022 int used_files;
12023 int max_files;
12026 /* Traversal function for the hash table. */
12028 static int
12029 file_name_acquire (void ** slot, void *data)
12031 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12032 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12033 struct file_info *fi;
12034 const char *f;
12036 gcc_assert (fnad->max_files >= d->emitted_number);
12038 if (! d->emitted_number)
12039 return 1;
12041 gcc_assert (fnad->max_files != fnad->used_files);
12043 fi = fnad->files + fnad->used_files++;
12045 /* Skip all leading "./". */
12046 f = d->filename;
12047 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12048 f += 2;
12050 /* Create a new array entry. */
12051 fi->path = f;
12052 fi->length = strlen (f);
12053 fi->file_idx = d;
12055 /* Search for the file name part. */
12056 f = strrchr (f, DIR_SEPARATOR);
12057 #if defined (DIR_SEPARATOR_2)
12059 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12061 if (g != NULL)
12063 if (f == NULL || f < g)
12064 f = g;
12067 #endif
12069 fi->fname = f == NULL ? fi->path : f + 1;
12070 return 1;
12073 /* Output the directory table and the file name table. We try to minimize
12074 the total amount of memory needed. A heuristic is used to avoid large
12075 slowdowns with many input files. */
12077 static void
12078 output_file_names (void)
12080 struct file_name_acquire_data fnad;
12081 int numfiles;
12082 struct file_info *files;
12083 struct dir_info *dirs;
12084 int *saved;
12085 int *savehere;
12086 int *backmap;
12087 int ndirs;
12088 int idx_offset;
12089 int i;
12091 if (!last_emitted_file)
12093 dw2_asm_output_data (1, 0, "End directory table");
12094 dw2_asm_output_data (1, 0, "End file name table");
12095 return;
12098 numfiles = last_emitted_file->emitted_number;
12100 /* Allocate the various arrays we need. */
12101 files = XALLOCAVEC (struct file_info, numfiles);
12102 dirs = XALLOCAVEC (struct dir_info, numfiles);
12104 fnad.files = files;
12105 fnad.used_files = 0;
12106 fnad.max_files = numfiles;
12107 htab_traverse (file_table, file_name_acquire, &fnad);
12108 gcc_assert (fnad.used_files == fnad.max_files);
12110 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12112 /* Find all the different directories used. */
12113 dirs[0].path = files[0].path;
12114 dirs[0].length = files[0].fname - files[0].path;
12115 dirs[0].prefix = -1;
12116 dirs[0].count = 1;
12117 dirs[0].dir_idx = 0;
12118 files[0].dir_idx = 0;
12119 ndirs = 1;
12121 for (i = 1; i < numfiles; i++)
12122 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12123 && memcmp (dirs[ndirs - 1].path, files[i].path,
12124 dirs[ndirs - 1].length) == 0)
12126 /* Same directory as last entry. */
12127 files[i].dir_idx = ndirs - 1;
12128 ++dirs[ndirs - 1].count;
12130 else
12132 int j;
12134 /* This is a new directory. */
12135 dirs[ndirs].path = files[i].path;
12136 dirs[ndirs].length = files[i].fname - files[i].path;
12137 dirs[ndirs].count = 1;
12138 dirs[ndirs].dir_idx = ndirs;
12139 files[i].dir_idx = ndirs;
12141 /* Search for a prefix. */
12142 dirs[ndirs].prefix = -1;
12143 for (j = 0; j < ndirs; j++)
12144 if (dirs[j].length < dirs[ndirs].length
12145 && dirs[j].length > 1
12146 && (dirs[ndirs].prefix == -1
12147 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12148 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12149 dirs[ndirs].prefix = j;
12151 ++ndirs;
12154 /* Now to the actual work. We have to find a subset of the directories which
12155 allow expressing the file name using references to the directory table
12156 with the least amount of characters. We do not do an exhaustive search
12157 where we would have to check out every combination of every single
12158 possible prefix. Instead we use a heuristic which provides nearly optimal
12159 results in most cases and never is much off. */
12160 saved = XALLOCAVEC (int, ndirs);
12161 savehere = XALLOCAVEC (int, ndirs);
12163 memset (saved, '\0', ndirs * sizeof (saved[0]));
12164 for (i = 0; i < ndirs; i++)
12166 int j;
12167 int total;
12169 /* We can always save some space for the current directory. But this
12170 does not mean it will be enough to justify adding the directory. */
12171 savehere[i] = dirs[i].length;
12172 total = (savehere[i] - saved[i]) * dirs[i].count;
12174 for (j = i + 1; j < ndirs; j++)
12176 savehere[j] = 0;
12177 if (saved[j] < dirs[i].length)
12179 /* Determine whether the dirs[i] path is a prefix of the
12180 dirs[j] path. */
12181 int k;
12183 k = dirs[j].prefix;
12184 while (k != -1 && k != (int) i)
12185 k = dirs[k].prefix;
12187 if (k == (int) i)
12189 /* Yes it is. We can possibly save some memory by
12190 writing the filenames in dirs[j] relative to
12191 dirs[i]. */
12192 savehere[j] = dirs[i].length;
12193 total += (savehere[j] - saved[j]) * dirs[j].count;
12198 /* Check whether we can save enough to justify adding the dirs[i]
12199 directory. */
12200 if (total > dirs[i].length + 1)
12202 /* It's worthwhile adding. */
12203 for (j = i; j < ndirs; j++)
12204 if (savehere[j] > 0)
12206 /* Remember how much we saved for this directory so far. */
12207 saved[j] = savehere[j];
12209 /* Remember the prefix directory. */
12210 dirs[j].dir_idx = i;
12215 /* Emit the directory name table. */
12216 idx_offset = dirs[0].length > 0 ? 1 : 0;
12217 for (i = 1 - idx_offset; i < ndirs; i++)
12218 dw2_asm_output_nstring (dirs[i].path,
12219 dirs[i].length
12220 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12221 "Directory Entry: %#x", i + idx_offset);
12223 dw2_asm_output_data (1, 0, "End directory table");
12225 /* We have to emit them in the order of emitted_number since that's
12226 used in the debug info generation. To do this efficiently we
12227 generate a back-mapping of the indices first. */
12228 backmap = XALLOCAVEC (int, numfiles);
12229 for (i = 0; i < numfiles; i++)
12230 backmap[files[i].file_idx->emitted_number - 1] = i;
12232 /* Now write all the file names. */
12233 for (i = 0; i < numfiles; i++)
12235 int file_idx = backmap[i];
12236 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12238 #ifdef VMS_DEBUGGING_INFO
12239 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12241 /* Setting these fields can lead to debugger miscomparisons,
12242 but VMS Debug requires them to be set correctly. */
12244 int ver;
12245 long long cdt;
12246 long siz;
12247 int maxfilelen = strlen (files[file_idx].path)
12248 + dirs[dir_idx].length
12249 + MAX_VMS_VERSION_LEN + 1;
12250 char *filebuf = XALLOCAVEC (char, maxfilelen);
12252 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12253 snprintf (filebuf, maxfilelen, "%s;%d",
12254 files[file_idx].path + dirs[dir_idx].length, ver);
12256 dw2_asm_output_nstring
12257 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12259 /* Include directory index. */
12260 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12262 /* Modification time. */
12263 dw2_asm_output_data_uleb128
12264 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12265 ? cdt : 0,
12266 NULL);
12268 /* File length in bytes. */
12269 dw2_asm_output_data_uleb128
12270 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12271 ? siz : 0,
12272 NULL);
12273 #else
12274 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12275 "File Entry: %#x", (unsigned) i + 1);
12277 /* Include directory index. */
12278 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12280 /* Modification time. */
12281 dw2_asm_output_data_uleb128 (0, NULL);
12283 /* File length in bytes. */
12284 dw2_asm_output_data_uleb128 (0, NULL);
12285 #endif /* VMS_DEBUGGING_INFO */
12288 dw2_asm_output_data (1, 0, "End file name table");
12292 /* Output the source line number correspondence information. This
12293 information goes into the .debug_line section. */
12295 static void
12296 output_line_info (void)
12298 char l1[20], l2[20], p1[20], p2[20];
12299 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12300 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12301 unsigned opc;
12302 unsigned n_op_args;
12303 unsigned long lt_index;
12304 unsigned long current_line;
12305 long line_offset;
12306 long line_delta;
12307 unsigned long current_file;
12308 unsigned long function;
12309 int ver = dwarf_version;
12311 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12312 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12313 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12314 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12316 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12317 dw2_asm_output_data (4, 0xffffffff,
12318 "Initial length escape value indicating 64-bit DWARF extension");
12319 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12320 "Length of Source Line Info");
12321 ASM_OUTPUT_LABEL (asm_out_file, l1);
12323 dw2_asm_output_data (2, ver, "DWARF Version");
12324 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12325 ASM_OUTPUT_LABEL (asm_out_file, p1);
12327 /* Define the architecture-dependent minimum instruction length (in
12328 bytes). In this implementation of DWARF, this field is used for
12329 information purposes only. Since GCC generates assembly language,
12330 we have no a priori knowledge of how many instruction bytes are
12331 generated for each source line, and therefore can use only the
12332 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12333 commands. Accordingly, we fix this as `1', which is "correct
12334 enough" for all architectures, and don't let the target override. */
12335 dw2_asm_output_data (1, 1,
12336 "Minimum Instruction Length");
12338 if (ver >= 4)
12339 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12340 "Maximum Operations Per Instruction");
12341 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12342 "Default is_stmt_start flag");
12343 dw2_asm_output_data (1, DWARF_LINE_BASE,
12344 "Line Base Value (Special Opcodes)");
12345 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12346 "Line Range Value (Special Opcodes)");
12347 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12348 "Special Opcode Base");
12350 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12352 switch (opc)
12354 case DW_LNS_advance_pc:
12355 case DW_LNS_advance_line:
12356 case DW_LNS_set_file:
12357 case DW_LNS_set_column:
12358 case DW_LNS_fixed_advance_pc:
12359 n_op_args = 1;
12360 break;
12361 default:
12362 n_op_args = 0;
12363 break;
12366 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12367 opc, n_op_args);
12370 /* Write out the information about the files we use. */
12371 output_file_names ();
12372 ASM_OUTPUT_LABEL (asm_out_file, p2);
12374 /* We used to set the address register to the first location in the text
12375 section here, but that didn't accomplish anything since we already
12376 have a line note for the opening brace of the first function. */
12378 /* Generate the line number to PC correspondence table, encoded as
12379 a series of state machine operations. */
12380 current_file = 1;
12381 current_line = 1;
12383 if (cfun && in_cold_section_p)
12384 strcpy (prev_line_label, crtl->subsections.cold_section_label);
12385 else
12386 strcpy (prev_line_label, text_section_label);
12387 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12389 dw_line_info_ref line_info = &line_info_table[lt_index];
12391 #if 0
12392 /* Disable this optimization for now; GDB wants to see two line notes
12393 at the beginning of a function so it can find the end of the
12394 prologue. */
12396 /* Don't emit anything for redundant notes. Just updating the
12397 address doesn't accomplish anything, because we already assume
12398 that anything after the last address is this line. */
12399 if (line_info->dw_line_num == current_line
12400 && line_info->dw_file_num == current_file)
12401 continue;
12402 #endif
12404 /* Emit debug info for the address of the current line.
12406 Unfortunately, we have little choice here currently, and must always
12407 use the most general form. GCC does not know the address delta
12408 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
12409 attributes which will give an upper bound on the address range. We
12410 could perhaps use length attributes to determine when it is safe to
12411 use DW_LNS_fixed_advance_pc. */
12413 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12414 if (0)
12416 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
12417 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12418 "DW_LNS_fixed_advance_pc");
12419 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12421 else
12423 /* This can handle any delta. This takes
12424 4+DWARF2_ADDR_SIZE bytes. */
12425 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12426 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12427 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12428 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12431 strcpy (prev_line_label, line_label);
12433 /* Emit debug info for the source file of the current line, if
12434 different from the previous line. */
12435 if (line_info->dw_file_num != current_file)
12437 current_file = line_info->dw_file_num;
12438 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12439 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12442 /* Emit debug info for the current line number, choosing the encoding
12443 that uses the least amount of space. */
12444 if (line_info->dw_line_num != current_line)
12446 line_offset = line_info->dw_line_num - current_line;
12447 line_delta = line_offset - DWARF_LINE_BASE;
12448 current_line = line_info->dw_line_num;
12449 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12450 /* This can handle deltas from -10 to 234, using the current
12451 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
12452 takes 1 byte. */
12453 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12454 "line %lu", current_line);
12455 else
12457 /* This can handle any delta. This takes at least 4 bytes,
12458 depending on the value being encoded. */
12459 dw2_asm_output_data (1, DW_LNS_advance_line,
12460 "advance to line %lu", current_line);
12461 dw2_asm_output_data_sleb128 (line_offset, NULL);
12462 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12465 else
12466 /* We still need to start a new row, so output a copy insn. */
12467 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12470 /* Emit debug info for the address of the end of the function. */
12471 if (0)
12473 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12474 "DW_LNS_fixed_advance_pc");
12475 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12477 else
12479 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12480 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12481 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12482 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12485 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12486 dw2_asm_output_data_uleb128 (1, NULL);
12487 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12489 function = 0;
12490 current_file = 1;
12491 current_line = 1;
12492 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12494 dw_separate_line_info_ref line_info
12495 = &separate_line_info_table[lt_index];
12497 #if 0
12498 /* Don't emit anything for redundant notes. */
12499 if (line_info->dw_line_num == current_line
12500 && line_info->dw_file_num == current_file
12501 && line_info->function == function)
12502 goto cont;
12503 #endif
12505 /* Emit debug info for the address of the current line. If this is
12506 a new function, or the first line of a function, then we need
12507 to handle it differently. */
12508 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12509 lt_index);
12510 if (function != line_info->function)
12512 function = line_info->function;
12514 /* Set the address register to the first line in the function. */
12515 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12516 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12517 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12518 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12520 else
12522 /* ??? See the DW_LNS_advance_pc comment above. */
12523 if (0)
12525 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12526 "DW_LNS_fixed_advance_pc");
12527 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12529 else
12531 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12532 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12533 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12534 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12538 strcpy (prev_line_label, line_label);
12540 /* Emit debug info for the source file of the current line, if
12541 different from the previous line. */
12542 if (line_info->dw_file_num != current_file)
12544 current_file = line_info->dw_file_num;
12545 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12546 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12549 /* Emit debug info for the current line number, choosing the encoding
12550 that uses the least amount of space. */
12551 if (line_info->dw_line_num != current_line)
12553 line_offset = line_info->dw_line_num - current_line;
12554 line_delta = line_offset - DWARF_LINE_BASE;
12555 current_line = line_info->dw_line_num;
12556 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12557 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12558 "line %lu", current_line);
12559 else
12561 dw2_asm_output_data (1, DW_LNS_advance_line,
12562 "advance to line %lu", current_line);
12563 dw2_asm_output_data_sleb128 (line_offset, NULL);
12564 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12567 else
12568 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12570 #if 0
12571 cont:
12572 #endif
12574 lt_index++;
12576 /* If we're done with a function, end its sequence. */
12577 if (lt_index == separate_line_info_table_in_use
12578 || separate_line_info_table[lt_index].function != function)
12580 current_file = 1;
12581 current_line = 1;
12583 /* Emit debug info for the address of the end of the function. */
12584 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12585 if (0)
12587 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12588 "DW_LNS_fixed_advance_pc");
12589 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12591 else
12593 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12594 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12595 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12596 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12599 /* Output the marker for the end of this sequence. */
12600 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12601 dw2_asm_output_data_uleb128 (1, NULL);
12602 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12606 /* Output the marker for the end of the line number info. */
12607 ASM_OUTPUT_LABEL (asm_out_file, l2);
12610 /* Given a pointer to a tree node for some base type, return a pointer to
12611 a DIE that describes the given type.
12613 This routine must only be called for GCC type nodes that correspond to
12614 Dwarf base (fundamental) types. */
12616 static dw_die_ref
12617 base_type_die (tree type)
12619 dw_die_ref base_type_result;
12620 enum dwarf_type encoding;
12622 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12623 return 0;
12625 /* If this is a subtype that should not be emitted as a subrange type,
12626 use the base type. See subrange_type_for_debug_p. */
12627 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12628 type = TREE_TYPE (type);
12630 switch (TREE_CODE (type))
12632 case INTEGER_TYPE:
12633 if ((dwarf_version >= 4 || !dwarf_strict)
12634 && TYPE_NAME (type)
12635 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12636 && DECL_IS_BUILTIN (TYPE_NAME (type))
12637 && DECL_NAME (TYPE_NAME (type)))
12639 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12640 if (strcmp (name, "char16_t") == 0
12641 || strcmp (name, "char32_t") == 0)
12643 encoding = DW_ATE_UTF;
12644 break;
12647 if (TYPE_STRING_FLAG (type))
12649 if (TYPE_UNSIGNED (type))
12650 encoding = DW_ATE_unsigned_char;
12651 else
12652 encoding = DW_ATE_signed_char;
12654 else if (TYPE_UNSIGNED (type))
12655 encoding = DW_ATE_unsigned;
12656 else
12657 encoding = DW_ATE_signed;
12658 break;
12660 case REAL_TYPE:
12661 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12663 if (dwarf_version >= 3 || !dwarf_strict)
12664 encoding = DW_ATE_decimal_float;
12665 else
12666 encoding = DW_ATE_lo_user;
12668 else
12669 encoding = DW_ATE_float;
12670 break;
12672 case FIXED_POINT_TYPE:
12673 if (!(dwarf_version >= 3 || !dwarf_strict))
12674 encoding = DW_ATE_lo_user;
12675 else if (TYPE_UNSIGNED (type))
12676 encoding = DW_ATE_unsigned_fixed;
12677 else
12678 encoding = DW_ATE_signed_fixed;
12679 break;
12681 /* Dwarf2 doesn't know anything about complex ints, so use
12682 a user defined type for it. */
12683 case COMPLEX_TYPE:
12684 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12685 encoding = DW_ATE_complex_float;
12686 else
12687 encoding = DW_ATE_lo_user;
12688 break;
12690 case BOOLEAN_TYPE:
12691 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12692 encoding = DW_ATE_boolean;
12693 break;
12695 default:
12696 /* No other TREE_CODEs are Dwarf fundamental types. */
12697 gcc_unreachable ();
12700 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12702 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12703 int_size_in_bytes (type));
12704 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12706 return base_type_result;
12709 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12710 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12712 static inline int
12713 is_base_type (tree type)
12715 switch (TREE_CODE (type))
12717 case ERROR_MARK:
12718 case VOID_TYPE:
12719 case INTEGER_TYPE:
12720 case REAL_TYPE:
12721 case FIXED_POINT_TYPE:
12722 case COMPLEX_TYPE:
12723 case BOOLEAN_TYPE:
12724 return 1;
12726 case ARRAY_TYPE:
12727 case RECORD_TYPE:
12728 case UNION_TYPE:
12729 case QUAL_UNION_TYPE:
12730 case ENUMERAL_TYPE:
12731 case FUNCTION_TYPE:
12732 case METHOD_TYPE:
12733 case POINTER_TYPE:
12734 case REFERENCE_TYPE:
12735 case NULLPTR_TYPE:
12736 case OFFSET_TYPE:
12737 case LANG_TYPE:
12738 case VECTOR_TYPE:
12739 return 0;
12741 default:
12742 gcc_unreachable ();
12745 return 0;
12748 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12749 node, return the size in bits for the type if it is a constant, or else
12750 return the alignment for the type if the type's size is not constant, or
12751 else return BITS_PER_WORD if the type actually turns out to be an
12752 ERROR_MARK node. */
12754 static inline unsigned HOST_WIDE_INT
12755 simple_type_size_in_bits (const_tree type)
12757 if (TREE_CODE (type) == ERROR_MARK)
12758 return BITS_PER_WORD;
12759 else if (TYPE_SIZE (type) == NULL_TREE)
12760 return 0;
12761 else if (host_integerp (TYPE_SIZE (type), 1))
12762 return tree_low_cst (TYPE_SIZE (type), 1);
12763 else
12764 return TYPE_ALIGN (type);
12767 /* Similarly, but return a double_int instead of UHWI. */
12769 static inline double_int
12770 double_int_type_size_in_bits (const_tree type)
12772 if (TREE_CODE (type) == ERROR_MARK)
12773 return uhwi_to_double_int (BITS_PER_WORD);
12774 else if (TYPE_SIZE (type) == NULL_TREE)
12775 return double_int_zero;
12776 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12777 return tree_to_double_int (TYPE_SIZE (type));
12778 else
12779 return uhwi_to_double_int (TYPE_ALIGN (type));
12782 /* Given a pointer to a tree node for a subrange type, return a pointer
12783 to a DIE that describes the given type. */
12785 static dw_die_ref
12786 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12788 dw_die_ref subrange_die;
12789 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12791 if (context_die == NULL)
12792 context_die = comp_unit_die ();
12794 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12796 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12798 /* The size of the subrange type and its base type do not match,
12799 so we need to generate a size attribute for the subrange type. */
12800 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12803 if (low)
12804 add_bound_info (subrange_die, DW_AT_lower_bound, low);
12805 if (high)
12806 add_bound_info (subrange_die, DW_AT_upper_bound, high);
12808 return subrange_die;
12811 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12812 entry that chains various modifiers in front of the given type. */
12814 static dw_die_ref
12815 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12816 dw_die_ref context_die)
12818 enum tree_code code = TREE_CODE (type);
12819 dw_die_ref mod_type_die;
12820 dw_die_ref sub_die = NULL;
12821 tree item_type = NULL;
12822 tree qualified_type;
12823 tree name, low, high;
12825 if (code == ERROR_MARK)
12826 return NULL;
12828 /* See if we already have the appropriately qualified variant of
12829 this type. */
12830 qualified_type
12831 = get_qualified_type (type,
12832 ((is_const_type ? TYPE_QUAL_CONST : 0)
12833 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12835 if (qualified_type == sizetype
12836 && TYPE_NAME (qualified_type)
12837 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12839 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12841 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12842 && TYPE_PRECISION (t)
12843 == TYPE_PRECISION (qualified_type)
12844 && TYPE_UNSIGNED (t)
12845 == TYPE_UNSIGNED (qualified_type));
12846 qualified_type = t;
12849 /* If we do, then we can just use its DIE, if it exists. */
12850 if (qualified_type)
12852 mod_type_die = lookup_type_die (qualified_type);
12853 if (mod_type_die)
12854 return mod_type_die;
12857 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12859 /* Handle C typedef types. */
12860 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12861 && !DECL_ARTIFICIAL (name))
12863 tree dtype = TREE_TYPE (name);
12865 if (qualified_type == dtype)
12867 /* For a named type, use the typedef. */
12868 gen_type_die (qualified_type, context_die);
12869 return lookup_type_die (qualified_type);
12871 else if (is_const_type < TYPE_READONLY (dtype)
12872 || is_volatile_type < TYPE_VOLATILE (dtype)
12873 || (is_const_type <= TYPE_READONLY (dtype)
12874 && is_volatile_type <= TYPE_VOLATILE (dtype)
12875 && DECL_ORIGINAL_TYPE (name) != type))
12876 /* cv-unqualified version of named type. Just use the unnamed
12877 type to which it refers. */
12878 return modified_type_die (DECL_ORIGINAL_TYPE (name),
12879 is_const_type, is_volatile_type,
12880 context_die);
12881 /* Else cv-qualified version of named type; fall through. */
12884 if (is_const_type
12885 /* If both is_const_type and is_volatile_type, prefer the path
12886 which leads to a qualified type. */
12887 && (!is_volatile_type
12888 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12889 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12891 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12892 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12894 else if (is_volatile_type)
12896 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12897 sub_die = modified_type_die (type, is_const_type, 0, context_die);
12899 else if (code == POINTER_TYPE)
12901 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12902 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12903 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12904 item_type = TREE_TYPE (type);
12905 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12906 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12907 TYPE_ADDR_SPACE (item_type));
12909 else if (code == REFERENCE_TYPE)
12911 if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
12912 mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12913 type);
12914 else
12915 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12916 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12917 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12918 item_type = TREE_TYPE (type);
12919 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12920 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12921 TYPE_ADDR_SPACE (item_type));
12923 else if (code == INTEGER_TYPE
12924 && TREE_TYPE (type) != NULL_TREE
12925 && subrange_type_for_debug_p (type, &low, &high))
12927 mod_type_die = subrange_type_die (type, low, high, context_die);
12928 item_type = TREE_TYPE (type);
12930 else if (is_base_type (type))
12931 mod_type_die = base_type_die (type);
12932 else
12934 gen_type_die (type, context_die);
12936 /* We have to get the type_main_variant here (and pass that to the
12937 `lookup_type_die' routine) because the ..._TYPE node we have
12938 might simply be a *copy* of some original type node (where the
12939 copy was created to help us keep track of typedef names) and
12940 that copy might have a different TYPE_UID from the original
12941 ..._TYPE node. */
12942 if (TREE_CODE (type) != VECTOR_TYPE)
12943 return lookup_type_die (type_main_variant (type));
12944 else
12945 /* Vectors have the debugging information in the type,
12946 not the main variant. */
12947 return lookup_type_die (type);
12950 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12951 don't output a DW_TAG_typedef, since there isn't one in the
12952 user's program; just attach a DW_AT_name to the type.
12953 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12954 if the base type already has the same name. */
12955 if (name
12956 && ((TREE_CODE (name) != TYPE_DECL
12957 && (qualified_type == TYPE_MAIN_VARIANT (type)
12958 || (!is_const_type && !is_volatile_type)))
12959 || (TREE_CODE (name) == TYPE_DECL
12960 && TREE_TYPE (name) == qualified_type
12961 && DECL_NAME (name))))
12963 if (TREE_CODE (name) == TYPE_DECL)
12964 /* Could just call add_name_and_src_coords_attributes here,
12965 but since this is a builtin type it doesn't have any
12966 useful source coordinates anyway. */
12967 name = DECL_NAME (name);
12968 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12970 /* This probably indicates a bug. */
12971 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12972 add_name_attribute (mod_type_die, "__unknown__");
12974 if (qualified_type)
12975 equate_type_number_to_die (qualified_type, mod_type_die);
12977 if (item_type)
12978 /* We must do this after the equate_type_number_to_die call, in case
12979 this is a recursive type. This ensures that the modified_type_die
12980 recursion will terminate even if the type is recursive. Recursive
12981 types are possible in Ada. */
12982 sub_die = modified_type_die (item_type,
12983 TYPE_READONLY (item_type),
12984 TYPE_VOLATILE (item_type),
12985 context_die);
12987 if (sub_die != NULL)
12988 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12990 return mod_type_die;
12993 /* Generate DIEs for the generic parameters of T.
12994 T must be either a generic type or a generic function.
12995 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12997 static void
12998 gen_generic_params_dies (tree t)
13000 tree parms, args;
13001 int parms_num, i;
13002 dw_die_ref die = NULL;
13004 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13005 return;
13007 if (TYPE_P (t))
13008 die = lookup_type_die (t);
13009 else if (DECL_P (t))
13010 die = lookup_decl_die (t);
13012 gcc_assert (die);
13014 parms = lang_hooks.get_innermost_generic_parms (t);
13015 if (!parms)
13016 /* T has no generic parameter. It means T is neither a generic type
13017 or function. End of story. */
13018 return;
13020 parms_num = TREE_VEC_LENGTH (parms);
13021 args = lang_hooks.get_innermost_generic_args (t);
13022 for (i = 0; i < parms_num; i++)
13024 tree parm, arg, arg_pack_elems;
13026 parm = TREE_VEC_ELT (parms, i);
13027 arg = TREE_VEC_ELT (args, i);
13028 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13029 gcc_assert (parm && TREE_VALUE (parm) && arg);
13031 if (parm && TREE_VALUE (parm) && arg)
13033 /* If PARM represents a template parameter pack,
13034 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13035 by DW_TAG_template_*_parameter DIEs for the argument
13036 pack elements of ARG. Note that ARG would then be
13037 an argument pack. */
13038 if (arg_pack_elems)
13039 template_parameter_pack_die (TREE_VALUE (parm),
13040 arg_pack_elems,
13041 die);
13042 else
13043 generic_parameter_die (TREE_VALUE (parm), arg,
13044 true /* Emit DW_AT_name */, die);
13049 /* Create and return a DIE for PARM which should be
13050 the representation of a generic type parameter.
13051 For instance, in the C++ front end, PARM would be a template parameter.
13052 ARG is the argument to PARM.
13053 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13054 name of the PARM.
13055 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13056 as a child node. */
13058 static dw_die_ref
13059 generic_parameter_die (tree parm, tree arg,
13060 bool emit_name_p,
13061 dw_die_ref parent_die)
13063 dw_die_ref tmpl_die = NULL;
13064 const char *name = NULL;
13066 if (!parm || !DECL_NAME (parm) || !arg)
13067 return NULL;
13069 /* We support non-type generic parameters and arguments,
13070 type generic parameters and arguments, as well as
13071 generic generic parameters (a.k.a. template template parameters in C++)
13072 and arguments. */
13073 if (TREE_CODE (parm) == PARM_DECL)
13074 /* PARM is a nontype generic parameter */
13075 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13076 else if (TREE_CODE (parm) == TYPE_DECL)
13077 /* PARM is a type generic parameter. */
13078 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13079 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13080 /* PARM is a generic generic parameter.
13081 Its DIE is a GNU extension. It shall have a
13082 DW_AT_name attribute to represent the name of the template template
13083 parameter, and a DW_AT_GNU_template_name attribute to represent the
13084 name of the template template argument. */
13085 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13086 parent_die, parm);
13087 else
13088 gcc_unreachable ();
13090 if (tmpl_die)
13092 tree tmpl_type;
13094 /* If PARM is a generic parameter pack, it means we are
13095 emitting debug info for a template argument pack element.
13096 In other terms, ARG is a template argument pack element.
13097 In that case, we don't emit any DW_AT_name attribute for
13098 the die. */
13099 if (emit_name_p)
13101 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13102 gcc_assert (name);
13103 add_AT_string (tmpl_die, DW_AT_name, name);
13106 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13108 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13109 TMPL_DIE should have a child DW_AT_type attribute that is set
13110 to the type of the argument to PARM, which is ARG.
13111 If PARM is a type generic parameter, TMPL_DIE should have a
13112 child DW_AT_type that is set to ARG. */
13113 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13114 add_type_attribute (tmpl_die, tmpl_type, 0,
13115 TREE_THIS_VOLATILE (tmpl_type),
13116 parent_die);
13118 else
13120 /* So TMPL_DIE is a DIE representing a
13121 a generic generic template parameter, a.k.a template template
13122 parameter in C++ and arg is a template. */
13124 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13125 to the name of the argument. */
13126 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13127 if (name)
13128 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13131 if (TREE_CODE (parm) == PARM_DECL)
13132 /* So PARM is a non-type generic parameter.
13133 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13134 attribute of TMPL_DIE which value represents the value
13135 of ARG.
13136 We must be careful here:
13137 The value of ARG might reference some function decls.
13138 We might currently be emitting debug info for a generic
13139 type and types are emitted before function decls, we don't
13140 know if the function decls referenced by ARG will actually be
13141 emitted after cgraph computations.
13142 So must defer the generation of the DW_AT_const_value to
13143 after cgraph is ready. */
13144 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13147 return tmpl_die;
13150 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13151 PARM_PACK must be a template parameter pack. The returned DIE
13152 will be child DIE of PARENT_DIE. */
13154 static dw_die_ref
13155 template_parameter_pack_die (tree parm_pack,
13156 tree parm_pack_args,
13157 dw_die_ref parent_die)
13159 dw_die_ref die;
13160 int j;
13162 gcc_assert (parent_die && parm_pack);
13164 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13165 add_name_and_src_coords_attributes (die, parm_pack);
13166 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13167 generic_parameter_die (parm_pack,
13168 TREE_VEC_ELT (parm_pack_args, j),
13169 false /* Don't emit DW_AT_name */,
13170 die);
13171 return die;
13174 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13175 an enumerated type. */
13177 static inline int
13178 type_is_enum (const_tree type)
13180 return TREE_CODE (type) == ENUMERAL_TYPE;
13183 /* Return the DBX register number described by a given RTL node. */
13185 static unsigned int
13186 dbx_reg_number (const_rtx rtl)
13188 unsigned regno = REGNO (rtl);
13190 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13192 #ifdef LEAF_REG_REMAP
13193 if (current_function_uses_only_leaf_regs)
13195 int leaf_reg = LEAF_REG_REMAP (regno);
13196 if (leaf_reg != -1)
13197 regno = (unsigned) leaf_reg;
13199 #endif
13201 return DBX_REGISTER_NUMBER (regno);
13204 /* Optionally add a DW_OP_piece term to a location description expression.
13205 DW_OP_piece is only added if the location description expression already
13206 doesn't end with DW_OP_piece. */
13208 static void
13209 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13211 dw_loc_descr_ref loc;
13213 if (*list_head != NULL)
13215 /* Find the end of the chain. */
13216 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13219 if (loc->dw_loc_opc != DW_OP_piece)
13220 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13224 /* Return a location descriptor that designates a machine register or
13225 zero if there is none. */
13227 static dw_loc_descr_ref
13228 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13230 rtx regs;
13232 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13233 return 0;
13235 /* We only use "frame base" when we're sure we're talking about the
13236 post-prologue local stack frame. We do this by *not* running
13237 register elimination until this point, and recognizing the special
13238 argument pointer and soft frame pointer rtx's.
13239 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13240 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13241 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13243 dw_loc_descr_ref result = NULL;
13245 if (dwarf_version >= 4 || !dwarf_strict)
13247 result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13248 if (result)
13249 add_loc_descr (&result,
13250 new_loc_descr (DW_OP_stack_value, 0, 0));
13252 return result;
13255 regs = targetm.dwarf_register_span (rtl);
13257 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13258 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13259 else
13260 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13263 /* Return a location descriptor that designates a machine register for
13264 a given hard register number. */
13266 static dw_loc_descr_ref
13267 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13269 dw_loc_descr_ref reg_loc_descr;
13271 if (regno <= 31)
13272 reg_loc_descr
13273 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13274 else
13275 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13277 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13278 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13280 return reg_loc_descr;
13283 /* Given an RTL of a register, return a location descriptor that
13284 designates a value that spans more than one register. */
13286 static dw_loc_descr_ref
13287 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13288 enum var_init_status initialized)
13290 int nregs, size, i;
13291 unsigned reg;
13292 dw_loc_descr_ref loc_result = NULL;
13294 reg = REGNO (rtl);
13295 #ifdef LEAF_REG_REMAP
13296 if (current_function_uses_only_leaf_regs)
13298 int leaf_reg = LEAF_REG_REMAP (reg);
13299 if (leaf_reg != -1)
13300 reg = (unsigned) leaf_reg;
13302 #endif
13303 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13304 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13306 /* Simple, contiguous registers. */
13307 if (regs == NULL_RTX)
13309 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13311 loc_result = NULL;
13312 while (nregs--)
13314 dw_loc_descr_ref t;
13316 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13317 VAR_INIT_STATUS_INITIALIZED);
13318 add_loc_descr (&loc_result, t);
13319 add_loc_descr_op_piece (&loc_result, size);
13320 ++reg;
13322 return loc_result;
13325 /* Now onto stupid register sets in non contiguous locations. */
13327 gcc_assert (GET_CODE (regs) == PARALLEL);
13329 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13330 loc_result = NULL;
13332 for (i = 0; i < XVECLEN (regs, 0); ++i)
13334 dw_loc_descr_ref t;
13336 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13337 VAR_INIT_STATUS_INITIALIZED);
13338 add_loc_descr (&loc_result, t);
13339 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13340 add_loc_descr_op_piece (&loc_result, size);
13343 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13344 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13345 return loc_result;
13348 /* Return a location descriptor that designates a constant. */
13350 static dw_loc_descr_ref
13351 int_loc_descriptor (HOST_WIDE_INT i)
13353 enum dwarf_location_atom op;
13355 /* Pick the smallest representation of a constant, rather than just
13356 defaulting to the LEB encoding. */
13357 if (i >= 0)
13359 if (i <= 31)
13360 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13361 else if (i <= 0xff)
13362 op = DW_OP_const1u;
13363 else if (i <= 0xffff)
13364 op = DW_OP_const2u;
13365 else if (HOST_BITS_PER_WIDE_INT == 32
13366 || i <= 0xffffffff)
13367 op = DW_OP_const4u;
13368 else
13369 op = DW_OP_constu;
13371 else
13373 if (i >= -0x80)
13374 op = DW_OP_const1s;
13375 else if (i >= -0x8000)
13376 op = DW_OP_const2s;
13377 else if (HOST_BITS_PER_WIDE_INT == 32
13378 || i >= -0x80000000)
13379 op = DW_OP_const4s;
13380 else
13381 op = DW_OP_consts;
13384 return new_loc_descr (op, i, 0);
13387 /* Return loc description representing "address" of integer value.
13388 This can appear only as toplevel expression. */
13390 static dw_loc_descr_ref
13391 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13393 int litsize;
13394 dw_loc_descr_ref loc_result = NULL;
13396 if (!(dwarf_version >= 4 || !dwarf_strict))
13397 return NULL;
13399 if (i >= 0)
13401 if (i <= 31)
13402 litsize = 1;
13403 else if (i <= 0xff)
13404 litsize = 2;
13405 else if (i <= 0xffff)
13406 litsize = 3;
13407 else if (HOST_BITS_PER_WIDE_INT == 32
13408 || i <= 0xffffffff)
13409 litsize = 5;
13410 else
13411 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13413 else
13415 if (i >= -0x80)
13416 litsize = 2;
13417 else if (i >= -0x8000)
13418 litsize = 3;
13419 else if (HOST_BITS_PER_WIDE_INT == 32
13420 || i >= -0x80000000)
13421 litsize = 5;
13422 else
13423 litsize = 1 + size_of_sleb128 (i);
13425 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13426 is more compact. For DW_OP_stack_value we need:
13427 litsize + 1 (DW_OP_stack_value)
13428 and for DW_OP_implicit_value:
13429 1 (DW_OP_implicit_value) + 1 (length) + size. */
13430 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13432 loc_result = int_loc_descriptor (i);
13433 add_loc_descr (&loc_result,
13434 new_loc_descr (DW_OP_stack_value, 0, 0));
13435 return loc_result;
13438 loc_result = new_loc_descr (DW_OP_implicit_value,
13439 size, 0);
13440 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13441 loc_result->dw_loc_oprnd2.v.val_int = i;
13442 return loc_result;
13445 /* Return a location descriptor that designates a base+offset location. */
13447 static dw_loc_descr_ref
13448 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13449 enum var_init_status initialized)
13451 unsigned int regno;
13452 dw_loc_descr_ref result;
13453 dw_fde_ref fde = current_fde ();
13455 /* We only use "frame base" when we're sure we're talking about the
13456 post-prologue local stack frame. We do this by *not* running
13457 register elimination until this point, and recognizing the special
13458 argument pointer and soft frame pointer rtx's. */
13459 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13461 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13463 if (elim != reg)
13465 if (GET_CODE (elim) == PLUS)
13467 offset += INTVAL (XEXP (elim, 1));
13468 elim = XEXP (elim, 0);
13470 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13471 && (elim == hard_frame_pointer_rtx
13472 || elim == stack_pointer_rtx))
13473 || elim == (frame_pointer_needed
13474 ? hard_frame_pointer_rtx
13475 : stack_pointer_rtx));
13477 /* If drap register is used to align stack, use frame
13478 pointer + offset to access stack variables. If stack
13479 is aligned without drap, use stack pointer + offset to
13480 access stack variables. */
13481 if (crtl->stack_realign_tried
13482 && reg == frame_pointer_rtx)
13484 int base_reg
13485 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13486 ? HARD_FRAME_POINTER_REGNUM
13487 : STACK_POINTER_REGNUM);
13488 return new_reg_loc_descr (base_reg, offset);
13491 offset += frame_pointer_fb_offset;
13492 return new_loc_descr (DW_OP_fbreg, offset, 0);
13495 else if (!optimize
13496 && fde
13497 && (fde->drap_reg == REGNO (reg)
13498 || fde->vdrap_reg == REGNO (reg)))
13500 /* Use cfa+offset to represent the location of arguments passed
13501 on the stack when drap is used to align stack.
13502 Only do this when not optimizing, for optimized code var-tracking
13503 is supposed to track where the arguments live and the register
13504 used as vdrap or drap in some spot might be used for something
13505 else in other part of the routine. */
13506 return new_loc_descr (DW_OP_fbreg, offset, 0);
13509 regno = dbx_reg_number (reg);
13510 if (regno <= 31)
13511 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13512 offset, 0);
13513 else
13514 result = new_loc_descr (DW_OP_bregx, regno, offset);
13516 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13517 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13519 return result;
13522 /* Return true if this RTL expression describes a base+offset calculation. */
13524 static inline int
13525 is_based_loc (const_rtx rtl)
13527 return (GET_CODE (rtl) == PLUS
13528 && ((REG_P (XEXP (rtl, 0))
13529 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13530 && CONST_INT_P (XEXP (rtl, 1)))));
13533 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13534 failed. */
13536 static dw_loc_descr_ref
13537 tls_mem_loc_descriptor (rtx mem)
13539 tree base;
13540 dw_loc_descr_ref loc_result;
13542 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13543 return NULL;
13545 base = get_base_address (MEM_EXPR (mem));
13546 if (base == NULL
13547 || TREE_CODE (base) != VAR_DECL
13548 || !DECL_THREAD_LOCAL_P (base))
13549 return NULL;
13551 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13552 if (loc_result == NULL)
13553 return NULL;
13555 if (INTVAL (MEM_OFFSET (mem)))
13556 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13558 return loc_result;
13561 /* Output debug info about reason why we failed to expand expression as dwarf
13562 expression. */
13564 static void
13565 expansion_failed (tree expr, rtx rtl, char const *reason)
13567 if (dump_file && (dump_flags & TDF_DETAILS))
13569 fprintf (dump_file, "Failed to expand as dwarf: ");
13570 if (expr)
13571 print_generic_expr (dump_file, expr, dump_flags);
13572 if (rtl)
13574 fprintf (dump_file, "\n");
13575 print_rtl (dump_file, rtl);
13577 fprintf (dump_file, "\nReason: %s\n", reason);
13581 /* Helper function for const_ok_for_output, called either directly
13582 or via for_each_rtx. */
13584 static int
13585 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13587 rtx rtl = *rtlp;
13589 if (GET_CODE (rtl) == UNSPEC)
13591 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13592 we can't express it in the debug info. */
13593 #ifdef ENABLE_CHECKING
13594 /* Don't complain about TLS UNSPECs, those are just too hard to
13595 delegitimize. */
13596 if (XVECLEN (rtl, 0) != 1
13597 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13598 || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13599 || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13600 || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13601 inform (current_function_decl
13602 ? DECL_SOURCE_LOCATION (current_function_decl)
13603 : UNKNOWN_LOCATION,
13604 #if NUM_UNSPEC_VALUES > 0
13605 "non-delegitimized UNSPEC %s (%d) found in variable location",
13606 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13607 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13608 XINT (rtl, 1));
13609 #else
13610 "non-delegitimized UNSPEC %d found in variable location",
13611 XINT (rtl, 1));
13612 #endif
13613 #endif
13614 expansion_failed (NULL_TREE, rtl,
13615 "UNSPEC hasn't been delegitimized.\n");
13616 return 1;
13619 if (GET_CODE (rtl) != SYMBOL_REF)
13620 return 0;
13622 if (CONSTANT_POOL_ADDRESS_P (rtl))
13624 bool marked;
13625 get_pool_constant_mark (rtl, &marked);
13626 /* If all references to this pool constant were optimized away,
13627 it was not output and thus we can't represent it. */
13628 if (!marked)
13630 expansion_failed (NULL_TREE, rtl,
13631 "Constant was removed from constant pool.\n");
13632 return 1;
13636 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13637 return 1;
13639 /* Avoid references to external symbols in debug info, on several targets
13640 the linker might even refuse to link when linking a shared library,
13641 and in many other cases the relocations for .debug_info/.debug_loc are
13642 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13643 to be defined within the same shared library or executable are fine. */
13644 if (SYMBOL_REF_EXTERNAL_P (rtl))
13646 tree decl = SYMBOL_REF_DECL (rtl);
13648 if (decl == NULL || !targetm.binds_local_p (decl))
13650 expansion_failed (NULL_TREE, rtl,
13651 "Symbol not defined in current TU.\n");
13652 return 1;
13656 return 0;
13659 /* Return true if constant RTL can be emitted in DW_OP_addr or
13660 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13661 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13663 static bool
13664 const_ok_for_output (rtx rtl)
13666 if (GET_CODE (rtl) == SYMBOL_REF)
13667 return const_ok_for_output_1 (&rtl, NULL) == 0;
13669 if (GET_CODE (rtl) == CONST)
13670 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13672 return true;
13675 /* The following routine converts the RTL for a variable or parameter
13676 (resident in memory) into an equivalent Dwarf representation of a
13677 mechanism for getting the address of that same variable onto the top of a
13678 hypothetical "address evaluation" stack.
13680 When creating memory location descriptors, we are effectively transforming
13681 the RTL for a memory-resident object into its Dwarf postfix expression
13682 equivalent. This routine recursively descends an RTL tree, turning
13683 it into Dwarf postfix code as it goes.
13685 MODE is the mode of the memory reference, needed to handle some
13686 autoincrement addressing modes.
13688 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13689 location list for RTL.
13691 Return 0 if we can't represent the location. */
13693 static dw_loc_descr_ref
13694 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13695 enum var_init_status initialized)
13697 dw_loc_descr_ref mem_loc_result = NULL;
13698 enum dwarf_location_atom op;
13699 dw_loc_descr_ref op0, op1;
13701 /* Note that for a dynamically sized array, the location we will generate a
13702 description of here will be the lowest numbered location which is
13703 actually within the array. That's *not* necessarily the same as the
13704 zeroth element of the array. */
13706 rtl = targetm.delegitimize_address (rtl);
13708 switch (GET_CODE (rtl))
13710 case POST_INC:
13711 case POST_DEC:
13712 case POST_MODIFY:
13713 return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13715 case SUBREG:
13716 /* The case of a subreg may arise when we have a local (register)
13717 variable or a formal (register) parameter which doesn't quite fill
13718 up an entire register. For now, just assume that it is
13719 legitimate to make the Dwarf info refer to the whole register which
13720 contains the given subreg. */
13721 if (!subreg_lowpart_p (rtl))
13722 break;
13723 rtl = SUBREG_REG (rtl);
13724 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13725 break;
13726 if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13727 break;
13728 mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13729 break;
13731 case REG:
13732 /* Whenever a register number forms a part of the description of the
13733 method for calculating the (dynamic) address of a memory resident
13734 object, DWARF rules require the register number be referred to as
13735 a "base register". This distinction is not based in any way upon
13736 what category of register the hardware believes the given register
13737 belongs to. This is strictly DWARF terminology we're dealing with
13738 here. Note that in cases where the location of a memory-resident
13739 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13740 OP_CONST (0)) the actual DWARF location descriptor that we generate
13741 may just be OP_BASEREG (basereg). This may look deceptively like
13742 the object in question was allocated to a register (rather than in
13743 memory) so DWARF consumers need to be aware of the subtle
13744 distinction between OP_REG and OP_BASEREG. */
13745 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13746 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13747 else if (stack_realign_drap
13748 && crtl->drap_reg
13749 && crtl->args.internal_arg_pointer == rtl
13750 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13752 /* If RTL is internal_arg_pointer, which has been optimized
13753 out, use DRAP instead. */
13754 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13755 VAR_INIT_STATUS_INITIALIZED);
13757 break;
13759 case SIGN_EXTEND:
13760 case ZERO_EXTEND:
13761 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13762 VAR_INIT_STATUS_INITIALIZED);
13763 if (op0 == 0)
13764 break;
13765 else
13767 int shift = DWARF2_ADDR_SIZE
13768 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13769 shift *= BITS_PER_UNIT;
13770 if (GET_CODE (rtl) == SIGN_EXTEND)
13771 op = DW_OP_shra;
13772 else
13773 op = DW_OP_shr;
13774 mem_loc_result = op0;
13775 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13776 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13777 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13778 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13780 break;
13782 case MEM:
13783 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13784 VAR_INIT_STATUS_INITIALIZED);
13785 if (mem_loc_result == NULL)
13786 mem_loc_result = tls_mem_loc_descriptor (rtl);
13787 if (mem_loc_result != 0)
13789 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13791 expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13792 return 0;
13794 else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13795 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13796 else
13797 add_loc_descr (&mem_loc_result,
13798 new_loc_descr (DW_OP_deref_size,
13799 GET_MODE_SIZE (GET_MODE (rtl)), 0));
13801 else
13803 rtx new_rtl = avoid_constant_pool_reference (rtl);
13804 if (new_rtl != rtl)
13805 return mem_loc_descriptor (new_rtl, mode, initialized);
13807 break;
13809 case LO_SUM:
13810 rtl = XEXP (rtl, 1);
13812 /* ... fall through ... */
13814 case LABEL_REF:
13815 /* Some ports can transform a symbol ref into a label ref, because
13816 the symbol ref is too far away and has to be dumped into a constant
13817 pool. */
13818 case CONST:
13819 case SYMBOL_REF:
13820 if (GET_CODE (rtl) == SYMBOL_REF
13821 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13823 dw_loc_descr_ref temp;
13825 /* If this is not defined, we have no way to emit the data. */
13826 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13827 break;
13829 /* We used to emit DW_OP_addr here, but that's wrong, since
13830 DW_OP_addr should be relocated by the debug info consumer,
13831 while DW_OP_GNU_push_tls_address operand should not. */
13832 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13833 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13834 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13835 temp->dw_loc_oprnd1.v.val_addr = rtl;
13836 temp->dtprel = true;
13838 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13839 add_loc_descr (&mem_loc_result, temp);
13841 break;
13844 if (!const_ok_for_output (rtl))
13845 break;
13847 symref:
13848 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13849 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13850 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13851 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13852 break;
13854 case CONCAT:
13855 case CONCATN:
13856 case VAR_LOCATION:
13857 case DEBUG_IMPLICIT_PTR:
13858 expansion_failed (NULL_TREE, rtl,
13859 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13860 return 0;
13862 case ENTRY_VALUE:
13863 if (dwarf_strict)
13864 return NULL;
13865 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13866 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13867 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13868 mem_loc_result->dw_loc_oprnd1.v.val_loc
13869 = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13870 VAR_INIT_STATUS_INITIALIZED);
13871 else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13873 dw_loc_descr_ref ref
13874 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13875 VAR_INIT_STATUS_INITIALIZED);
13876 if (ref == NULL || ref->dw_loc_opc == DW_OP_fbreg)
13877 return NULL;
13878 mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13880 else
13881 gcc_unreachable ();
13882 return mem_loc_result;
13884 case PRE_MODIFY:
13885 /* Extract the PLUS expression nested inside and fall into
13886 PLUS code below. */
13887 rtl = XEXP (rtl, 1);
13888 goto plus;
13890 case PRE_INC:
13891 case PRE_DEC:
13892 /* Turn these into a PLUS expression and fall into the PLUS code
13893 below. */
13894 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13895 GEN_INT (GET_CODE (rtl) == PRE_INC
13896 ? GET_MODE_UNIT_SIZE (mode)
13897 : -GET_MODE_UNIT_SIZE (mode)));
13899 /* ... fall through ... */
13901 case PLUS:
13902 plus:
13903 if (is_based_loc (rtl))
13904 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13905 INTVAL (XEXP (rtl, 1)),
13906 VAR_INIT_STATUS_INITIALIZED);
13907 else
13909 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13910 VAR_INIT_STATUS_INITIALIZED);
13911 if (mem_loc_result == 0)
13912 break;
13914 if (CONST_INT_P (XEXP (rtl, 1)))
13915 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13916 else
13918 dw_loc_descr_ref mem_loc_result2
13919 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13920 VAR_INIT_STATUS_INITIALIZED);
13921 if (mem_loc_result2 == 0)
13922 break;
13923 add_loc_descr (&mem_loc_result, mem_loc_result2);
13924 add_loc_descr (&mem_loc_result,
13925 new_loc_descr (DW_OP_plus, 0, 0));
13928 break;
13930 /* If a pseudo-reg is optimized away, it is possible for it to
13931 be replaced with a MEM containing a multiply or shift. */
13932 case MINUS:
13933 op = DW_OP_minus;
13934 goto do_binop;
13936 case MULT:
13937 op = DW_OP_mul;
13938 goto do_binop;
13940 case DIV:
13941 op = DW_OP_div;
13942 goto do_binop;
13944 case UMOD:
13945 op = DW_OP_mod;
13946 goto do_binop;
13948 case ASHIFT:
13949 op = DW_OP_shl;
13950 goto do_binop;
13952 case ASHIFTRT:
13953 op = DW_OP_shra;
13954 goto do_binop;
13956 case LSHIFTRT:
13957 op = DW_OP_shr;
13958 goto do_binop;
13960 case AND:
13961 op = DW_OP_and;
13962 goto do_binop;
13964 case IOR:
13965 op = DW_OP_or;
13966 goto do_binop;
13968 case XOR:
13969 op = DW_OP_xor;
13970 goto do_binop;
13972 do_binop:
13973 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13974 VAR_INIT_STATUS_INITIALIZED);
13975 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13976 VAR_INIT_STATUS_INITIALIZED);
13978 if (op0 == 0 || op1 == 0)
13979 break;
13981 mem_loc_result = op0;
13982 add_loc_descr (&mem_loc_result, op1);
13983 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13984 break;
13986 case MOD:
13987 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13988 VAR_INIT_STATUS_INITIALIZED);
13989 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13990 VAR_INIT_STATUS_INITIALIZED);
13992 if (op0 == 0 || op1 == 0)
13993 break;
13995 mem_loc_result = op0;
13996 add_loc_descr (&mem_loc_result, op1);
13997 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13998 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13999 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14000 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14001 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14002 break;
14004 case NOT:
14005 op = DW_OP_not;
14006 goto do_unop;
14008 case ABS:
14009 op = DW_OP_abs;
14010 goto do_unop;
14012 case NEG:
14013 op = DW_OP_neg;
14014 goto do_unop;
14016 do_unop:
14017 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14018 VAR_INIT_STATUS_INITIALIZED);
14020 if (op0 == 0)
14021 break;
14023 mem_loc_result = op0;
14024 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14025 break;
14027 case CONST_INT:
14028 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14029 break;
14031 case EQ:
14032 op = DW_OP_eq;
14033 goto do_scompare;
14035 case GE:
14036 op = DW_OP_ge;
14037 goto do_scompare;
14039 case GT:
14040 op = DW_OP_gt;
14041 goto do_scompare;
14043 case LE:
14044 op = DW_OP_le;
14045 goto do_scompare;
14047 case LT:
14048 op = DW_OP_lt;
14049 goto do_scompare;
14051 case NE:
14052 op = DW_OP_ne;
14053 goto do_scompare;
14055 do_scompare:
14056 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14057 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14058 break;
14059 else
14061 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14063 if (op_mode == VOIDmode)
14064 op_mode = GET_MODE (XEXP (rtl, 1));
14065 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14066 break;
14068 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14069 VAR_INIT_STATUS_INITIALIZED);
14070 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14071 VAR_INIT_STATUS_INITIALIZED);
14073 if (op0 == 0 || op1 == 0)
14074 break;
14076 if (op_mode != VOIDmode
14077 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14079 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14080 shift *= BITS_PER_UNIT;
14081 /* For eq/ne, if the operands are known to be zero-extended,
14082 there is no need to do the fancy shifting up. */
14083 if (op == DW_OP_eq || op == DW_OP_ne)
14085 dw_loc_descr_ref last0, last1;
14086 for (last0 = op0;
14087 last0->dw_loc_next != NULL;
14088 last0 = last0->dw_loc_next)
14090 for (last1 = op1;
14091 last1->dw_loc_next != NULL;
14092 last1 = last1->dw_loc_next)
14094 /* deref_size zero extends, and for constants we can check
14095 whether they are zero extended or not. */
14096 if (((last0->dw_loc_opc == DW_OP_deref_size
14097 && last0->dw_loc_oprnd1.v.val_int
14098 <= GET_MODE_SIZE (op_mode))
14099 || (CONST_INT_P (XEXP (rtl, 0))
14100 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14101 == (INTVAL (XEXP (rtl, 0))
14102 & GET_MODE_MASK (op_mode))))
14103 && ((last1->dw_loc_opc == DW_OP_deref_size
14104 && last1->dw_loc_oprnd1.v.val_int
14105 <= GET_MODE_SIZE (op_mode))
14106 || (CONST_INT_P (XEXP (rtl, 1))
14107 && (unsigned HOST_WIDE_INT)
14108 INTVAL (XEXP (rtl, 1))
14109 == (INTVAL (XEXP (rtl, 1))
14110 & GET_MODE_MASK (op_mode)))))
14111 goto do_compare;
14113 add_loc_descr (&op0, int_loc_descriptor (shift));
14114 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14115 if (CONST_INT_P (XEXP (rtl, 1)))
14116 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14117 else
14119 add_loc_descr (&op1, int_loc_descriptor (shift));
14120 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14125 do_compare:
14126 mem_loc_result = op0;
14127 add_loc_descr (&mem_loc_result, op1);
14128 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14129 if (STORE_FLAG_VALUE != 1)
14131 add_loc_descr (&mem_loc_result,
14132 int_loc_descriptor (STORE_FLAG_VALUE));
14133 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14135 break;
14137 case GEU:
14138 op = DW_OP_ge;
14139 goto do_ucompare;
14141 case GTU:
14142 op = DW_OP_gt;
14143 goto do_ucompare;
14145 case LEU:
14146 op = DW_OP_le;
14147 goto do_ucompare;
14149 case LTU:
14150 op = DW_OP_lt;
14151 goto do_ucompare;
14153 do_ucompare:
14154 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14155 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14156 break;
14157 else
14159 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14161 if (op_mode == VOIDmode)
14162 op_mode = GET_MODE (XEXP (rtl, 1));
14163 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14164 break;
14166 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14167 VAR_INIT_STATUS_INITIALIZED);
14168 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14169 VAR_INIT_STATUS_INITIALIZED);
14171 if (op0 == 0 || op1 == 0)
14172 break;
14174 if (op_mode != VOIDmode
14175 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14177 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14178 dw_loc_descr_ref last0, last1;
14179 for (last0 = op0;
14180 last0->dw_loc_next != NULL;
14181 last0 = last0->dw_loc_next)
14183 for (last1 = op1;
14184 last1->dw_loc_next != NULL;
14185 last1 = last1->dw_loc_next)
14187 if (CONST_INT_P (XEXP (rtl, 0)))
14188 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14189 /* deref_size zero extends, so no need to mask it again. */
14190 else if (last0->dw_loc_opc != DW_OP_deref_size
14191 || last0->dw_loc_oprnd1.v.val_int
14192 > GET_MODE_SIZE (op_mode))
14194 add_loc_descr (&op0, int_loc_descriptor (mask));
14195 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14197 if (CONST_INT_P (XEXP (rtl, 1)))
14198 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14199 /* deref_size zero extends, so no need to mask it again. */
14200 else if (last1->dw_loc_opc != DW_OP_deref_size
14201 || last1->dw_loc_oprnd1.v.val_int
14202 > GET_MODE_SIZE (op_mode))
14204 add_loc_descr (&op1, int_loc_descriptor (mask));
14205 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14208 else
14210 HOST_WIDE_INT bias = 1;
14211 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14212 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14213 if (CONST_INT_P (XEXP (rtl, 1)))
14214 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14215 + INTVAL (XEXP (rtl, 1)));
14216 else
14217 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14218 bias, 0));
14221 goto do_compare;
14223 case SMIN:
14224 case SMAX:
14225 case UMIN:
14226 case UMAX:
14227 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14228 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14229 || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14230 break;
14232 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14233 VAR_INIT_STATUS_INITIALIZED);
14234 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14235 VAR_INIT_STATUS_INITIALIZED);
14237 if (op0 == 0 || op1 == 0)
14238 break;
14240 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14241 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14242 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14243 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14245 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14247 HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14248 add_loc_descr (&op0, int_loc_descriptor (mask));
14249 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14250 add_loc_descr (&op1, int_loc_descriptor (mask));
14251 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14253 else
14255 HOST_WIDE_INT bias = 1;
14256 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14257 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14258 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14261 else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14263 int shift = DWARF2_ADDR_SIZE
14264 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14265 shift *= BITS_PER_UNIT;
14266 add_loc_descr (&op0, int_loc_descriptor (shift));
14267 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14268 add_loc_descr (&op1, int_loc_descriptor (shift));
14269 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14272 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14273 op = DW_OP_lt;
14274 else
14275 op = DW_OP_gt;
14276 mem_loc_result = op0;
14277 add_loc_descr (&mem_loc_result, op1);
14278 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14280 dw_loc_descr_ref bra_node, drop_node;
14282 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14283 add_loc_descr (&mem_loc_result, bra_node);
14284 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14285 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14286 add_loc_descr (&mem_loc_result, drop_node);
14287 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14288 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14290 break;
14292 case ZERO_EXTRACT:
14293 case SIGN_EXTRACT:
14294 if (CONST_INT_P (XEXP (rtl, 1))
14295 && CONST_INT_P (XEXP (rtl, 2))
14296 && ((unsigned) INTVAL (XEXP (rtl, 1))
14297 + (unsigned) INTVAL (XEXP (rtl, 2))
14298 <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14299 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14300 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14302 int shift, size;
14303 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14304 VAR_INIT_STATUS_INITIALIZED);
14305 if (op0 == 0)
14306 break;
14307 if (GET_CODE (rtl) == SIGN_EXTRACT)
14308 op = DW_OP_shra;
14309 else
14310 op = DW_OP_shr;
14311 mem_loc_result = op0;
14312 size = INTVAL (XEXP (rtl, 1));
14313 shift = INTVAL (XEXP (rtl, 2));
14314 if (BITS_BIG_ENDIAN)
14315 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14316 - shift - size;
14317 if (shift + size != (int) DWARF2_ADDR_SIZE)
14319 add_loc_descr (&mem_loc_result,
14320 int_loc_descriptor (DWARF2_ADDR_SIZE
14321 - shift - size));
14322 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14324 if (size != (int) DWARF2_ADDR_SIZE)
14326 add_loc_descr (&mem_loc_result,
14327 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14328 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14331 break;
14333 case IF_THEN_ELSE:
14335 dw_loc_descr_ref op2, bra_node, drop_node;
14336 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14337 VAR_INIT_STATUS_INITIALIZED);
14338 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14339 VAR_INIT_STATUS_INITIALIZED);
14340 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14341 VAR_INIT_STATUS_INITIALIZED);
14342 if (op0 == NULL || op1 == NULL || op2 == NULL)
14343 break;
14345 mem_loc_result = op1;
14346 add_loc_descr (&mem_loc_result, op2);
14347 add_loc_descr (&mem_loc_result, op0);
14348 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14349 add_loc_descr (&mem_loc_result, bra_node);
14350 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14351 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14352 add_loc_descr (&mem_loc_result, drop_node);
14353 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14354 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14356 break;
14358 case COMPARE:
14359 case ROTATE:
14360 case ROTATERT:
14361 case TRUNCATE:
14362 /* In theory, we could implement the above. */
14363 /* DWARF cannot represent the unsigned compare operations
14364 natively. */
14365 case SS_MULT:
14366 case US_MULT:
14367 case SS_DIV:
14368 case US_DIV:
14369 case SS_PLUS:
14370 case US_PLUS:
14371 case SS_MINUS:
14372 case US_MINUS:
14373 case SS_NEG:
14374 case US_NEG:
14375 case SS_ABS:
14376 case SS_ASHIFT:
14377 case US_ASHIFT:
14378 case SS_TRUNCATE:
14379 case US_TRUNCATE:
14380 case UDIV:
14381 case UNORDERED:
14382 case ORDERED:
14383 case UNEQ:
14384 case UNGE:
14385 case UNGT:
14386 case UNLE:
14387 case UNLT:
14388 case LTGT:
14389 case FLOAT_EXTEND:
14390 case FLOAT_TRUNCATE:
14391 case FLOAT:
14392 case UNSIGNED_FLOAT:
14393 case FIX:
14394 case UNSIGNED_FIX:
14395 case FRACT_CONVERT:
14396 case UNSIGNED_FRACT_CONVERT:
14397 case SAT_FRACT:
14398 case UNSIGNED_SAT_FRACT:
14399 case SQRT:
14400 case BSWAP:
14401 case FFS:
14402 case CLZ:
14403 case CTZ:
14404 case POPCOUNT:
14405 case PARITY:
14406 case ASM_OPERANDS:
14407 case VEC_MERGE:
14408 case VEC_SELECT:
14409 case VEC_CONCAT:
14410 case VEC_DUPLICATE:
14411 case UNSPEC:
14412 case HIGH:
14413 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14414 can't express it in the debug info. This can happen e.g. with some
14415 TLS UNSPECs. */
14416 break;
14418 case CONST_STRING:
14419 resolve_one_addr (&rtl, NULL);
14420 goto symref;
14422 default:
14423 #ifdef ENABLE_CHECKING
14424 print_rtl (stderr, rtl);
14425 gcc_unreachable ();
14426 #else
14427 break;
14428 #endif
14431 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14432 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14434 return mem_loc_result;
14437 /* Return a descriptor that describes the concatenation of two locations.
14438 This is typically a complex variable. */
14440 static dw_loc_descr_ref
14441 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14443 dw_loc_descr_ref cc_loc_result = NULL;
14444 dw_loc_descr_ref x0_ref
14445 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14446 dw_loc_descr_ref x1_ref
14447 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14449 if (x0_ref == 0 || x1_ref == 0)
14450 return 0;
14452 cc_loc_result = x0_ref;
14453 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14455 add_loc_descr (&cc_loc_result, x1_ref);
14456 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14458 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14459 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14461 return cc_loc_result;
14464 /* Return a descriptor that describes the concatenation of N
14465 locations. */
14467 static dw_loc_descr_ref
14468 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14470 unsigned int i;
14471 dw_loc_descr_ref cc_loc_result = NULL;
14472 unsigned int n = XVECLEN (concatn, 0);
14474 for (i = 0; i < n; ++i)
14476 dw_loc_descr_ref ref;
14477 rtx x = XVECEXP (concatn, 0, i);
14479 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14480 if (ref == NULL)
14481 return NULL;
14483 add_loc_descr (&cc_loc_result, ref);
14484 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14487 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14488 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14490 return cc_loc_result;
14493 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14494 for DEBUG_IMPLICIT_PTR RTL. */
14496 static dw_loc_descr_ref
14497 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14499 dw_loc_descr_ref ret;
14500 dw_die_ref ref;
14502 if (dwarf_strict)
14503 return NULL;
14504 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14505 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14506 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14507 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14508 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14509 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14510 if (ref)
14512 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14513 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14514 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14516 else
14518 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14519 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14521 return ret;
14524 /* Output a proper Dwarf location descriptor for a variable or parameter
14525 which is either allocated in a register or in a memory location. For a
14526 register, we just generate an OP_REG and the register number. For a
14527 memory location we provide a Dwarf postfix expression describing how to
14528 generate the (dynamic) address of the object onto the address stack.
14530 MODE is mode of the decl if this loc_descriptor is going to be used in
14531 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14532 allowed, VOIDmode otherwise.
14534 If we don't know how to describe it, return 0. */
14536 static dw_loc_descr_ref
14537 loc_descriptor (rtx rtl, enum machine_mode mode,
14538 enum var_init_status initialized)
14540 dw_loc_descr_ref loc_result = NULL;
14542 switch (GET_CODE (rtl))
14544 case SUBREG:
14545 /* The case of a subreg may arise when we have a local (register)
14546 variable or a formal (register) parameter which doesn't quite fill
14547 up an entire register. For now, just assume that it is
14548 legitimate to make the Dwarf info refer to the whole register which
14549 contains the given subreg. */
14550 loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14551 break;
14553 case REG:
14554 loc_result = reg_loc_descriptor (rtl, initialized);
14555 break;
14557 case MEM:
14558 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14559 initialized);
14560 if (loc_result == NULL)
14561 loc_result = tls_mem_loc_descriptor (rtl);
14562 if (loc_result == NULL)
14564 rtx new_rtl = avoid_constant_pool_reference (rtl);
14565 if (new_rtl != rtl)
14566 loc_result = loc_descriptor (new_rtl, mode, initialized);
14568 break;
14570 case CONCAT:
14571 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14572 initialized);
14573 break;
14575 case CONCATN:
14576 loc_result = concatn_loc_descriptor (rtl, initialized);
14577 break;
14579 case VAR_LOCATION:
14580 /* Single part. */
14581 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14583 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14584 if (GET_CODE (loc) == EXPR_LIST)
14585 loc = XEXP (loc, 0);
14586 loc_result = loc_descriptor (loc, mode, initialized);
14587 break;
14590 rtl = XEXP (rtl, 1);
14591 /* FALLTHRU */
14593 case PARALLEL:
14595 rtvec par_elems = XVEC (rtl, 0);
14596 int num_elem = GET_NUM_ELEM (par_elems);
14597 enum machine_mode mode;
14598 int i;
14600 /* Create the first one, so we have something to add to. */
14601 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14602 VOIDmode, initialized);
14603 if (loc_result == NULL)
14604 return NULL;
14605 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14606 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14607 for (i = 1; i < num_elem; i++)
14609 dw_loc_descr_ref temp;
14611 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14612 VOIDmode, initialized);
14613 if (temp == NULL)
14614 return NULL;
14615 add_loc_descr (&loc_result, temp);
14616 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14617 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14620 break;
14622 case CONST_INT:
14623 if (mode != VOIDmode && mode != BLKmode)
14624 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14625 INTVAL (rtl));
14626 break;
14628 case CONST_DOUBLE:
14629 if (mode == VOIDmode)
14630 mode = GET_MODE (rtl);
14632 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14634 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14636 /* Note that a CONST_DOUBLE rtx could represent either an integer
14637 or a floating-point constant. A CONST_DOUBLE is used whenever
14638 the constant requires more than one word in order to be
14639 adequately represented. We output CONST_DOUBLEs as blocks. */
14640 loc_result = new_loc_descr (DW_OP_implicit_value,
14641 GET_MODE_SIZE (mode), 0);
14642 if (SCALAR_FLOAT_MODE_P (mode))
14644 unsigned int length = GET_MODE_SIZE (mode);
14645 unsigned char *array
14646 = (unsigned char*) ggc_alloc_atomic (length);
14648 insert_float (rtl, array);
14649 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14650 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14651 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14652 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14654 else
14656 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14657 loc_result->dw_loc_oprnd2.v.val_double
14658 = rtx_to_double_int (rtl);
14661 break;
14663 case CONST_VECTOR:
14664 if (mode == VOIDmode)
14665 mode = GET_MODE (rtl);
14667 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14669 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14670 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14671 unsigned char *array = (unsigned char *)
14672 ggc_alloc_atomic (length * elt_size);
14673 unsigned int i;
14674 unsigned char *p;
14676 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14677 switch (GET_MODE_CLASS (mode))
14679 case MODE_VECTOR_INT:
14680 for (i = 0, p = array; i < length; i++, p += elt_size)
14682 rtx elt = CONST_VECTOR_ELT (rtl, i);
14683 double_int val = rtx_to_double_int (elt);
14685 if (elt_size <= sizeof (HOST_WIDE_INT))
14686 insert_int (double_int_to_shwi (val), elt_size, p);
14687 else
14689 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14690 insert_double (val, p);
14693 break;
14695 case MODE_VECTOR_FLOAT:
14696 for (i = 0, p = array; i < length; i++, p += elt_size)
14698 rtx elt = CONST_VECTOR_ELT (rtl, i);
14699 insert_float (elt, p);
14701 break;
14703 default:
14704 gcc_unreachable ();
14707 loc_result = new_loc_descr (DW_OP_implicit_value,
14708 length * elt_size, 0);
14709 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14710 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14711 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14712 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14714 break;
14716 case CONST:
14717 if (mode == VOIDmode
14718 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14719 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14720 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14722 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14723 break;
14725 /* FALLTHROUGH */
14726 case SYMBOL_REF:
14727 if (!const_ok_for_output (rtl))
14728 break;
14729 case LABEL_REF:
14730 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14731 && (dwarf_version >= 4 || !dwarf_strict))
14733 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14734 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14735 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14736 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14737 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14739 break;
14741 case DEBUG_IMPLICIT_PTR:
14742 loc_result = implicit_ptr_descriptor (rtl, 0);
14743 break;
14745 case PLUS:
14746 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14747 && CONST_INT_P (XEXP (rtl, 1)))
14749 loc_result
14750 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14751 break;
14753 /* FALLTHRU */
14754 default:
14755 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14756 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14757 && (dwarf_version >= 4 || !dwarf_strict))
14759 /* Value expression. */
14760 loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14761 if (loc_result)
14762 add_loc_descr (&loc_result,
14763 new_loc_descr (DW_OP_stack_value, 0, 0));
14765 break;
14768 return loc_result;
14771 /* We need to figure out what section we should use as the base for the
14772 address ranges where a given location is valid.
14773 1. If this particular DECL has a section associated with it, use that.
14774 2. If this function has a section associated with it, use that.
14775 3. Otherwise, use the text section.
14776 XXX: If you split a variable across multiple sections, we won't notice. */
14778 static const char *
14779 secname_for_decl (const_tree decl)
14781 const char *secname;
14783 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14785 tree sectree = DECL_SECTION_NAME (decl);
14786 secname = TREE_STRING_POINTER (sectree);
14788 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14790 tree sectree = DECL_SECTION_NAME (current_function_decl);
14791 secname = TREE_STRING_POINTER (sectree);
14793 else if (cfun && in_cold_section_p)
14794 secname = crtl->subsections.cold_section_label;
14795 else
14796 secname = text_section_label;
14798 return secname;
14801 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14803 static bool
14804 decl_by_reference_p (tree decl)
14806 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14807 || TREE_CODE (decl) == VAR_DECL)
14808 && DECL_BY_REFERENCE (decl));
14811 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14812 for VARLOC. */
14814 static dw_loc_descr_ref
14815 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14816 enum var_init_status initialized)
14818 int have_address = 0;
14819 dw_loc_descr_ref descr;
14820 enum machine_mode mode;
14822 if (want_address != 2)
14824 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14825 /* Single part. */
14826 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14828 varloc = PAT_VAR_LOCATION_LOC (varloc);
14829 if (GET_CODE (varloc) == EXPR_LIST)
14830 varloc = XEXP (varloc, 0);
14831 mode = GET_MODE (varloc);
14832 if (MEM_P (varloc))
14834 rtx addr = XEXP (varloc, 0);
14835 descr = mem_loc_descriptor (addr, mode, initialized);
14836 if (descr)
14837 have_address = 1;
14838 else
14840 rtx x = avoid_constant_pool_reference (varloc);
14841 if (x != varloc)
14842 descr = mem_loc_descriptor (x, mode, initialized);
14845 else
14846 descr = mem_loc_descriptor (varloc, mode, initialized);
14848 else
14849 return 0;
14851 else
14853 if (GET_CODE (varloc) == VAR_LOCATION)
14854 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14855 else
14856 mode = DECL_MODE (loc);
14857 descr = loc_descriptor (varloc, mode, initialized);
14858 have_address = 1;
14861 if (!descr)
14862 return 0;
14864 if (want_address == 2 && !have_address
14865 && (dwarf_version >= 4 || !dwarf_strict))
14867 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14869 expansion_failed (loc, NULL_RTX,
14870 "DWARF address size mismatch");
14871 return 0;
14873 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14874 have_address = 1;
14876 /* Show if we can't fill the request for an address. */
14877 if (want_address && !have_address)
14879 expansion_failed (loc, NULL_RTX,
14880 "Want address and only have value");
14881 return 0;
14884 /* If we've got an address and don't want one, dereference. */
14885 if (!want_address && have_address)
14887 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14888 enum dwarf_location_atom op;
14890 if (size > DWARF2_ADDR_SIZE || size == -1)
14892 expansion_failed (loc, NULL_RTX,
14893 "DWARF address size mismatch");
14894 return 0;
14896 else if (size == DWARF2_ADDR_SIZE)
14897 op = DW_OP_deref;
14898 else
14899 op = DW_OP_deref_size;
14901 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14904 return descr;
14907 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14908 if it is not possible. */
14910 static dw_loc_descr_ref
14911 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14913 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14914 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14915 else if (dwarf_version >= 3 || !dwarf_strict)
14916 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14917 else
14918 return NULL;
14921 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14922 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14924 static dw_loc_descr_ref
14925 dw_sra_loc_expr (tree decl, rtx loc)
14927 rtx p;
14928 unsigned int padsize = 0;
14929 dw_loc_descr_ref descr, *descr_tail;
14930 unsigned HOST_WIDE_INT decl_size;
14931 rtx varloc;
14932 enum var_init_status initialized;
14934 if (DECL_SIZE (decl) == NULL
14935 || !host_integerp (DECL_SIZE (decl), 1))
14936 return NULL;
14938 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14939 descr = NULL;
14940 descr_tail = &descr;
14942 for (p = loc; p; p = XEXP (p, 1))
14944 unsigned int bitsize = decl_piece_bitsize (p);
14945 rtx loc_note = *decl_piece_varloc_ptr (p);
14946 dw_loc_descr_ref cur_descr;
14947 dw_loc_descr_ref *tail, last = NULL;
14948 unsigned int opsize = 0;
14950 if (loc_note == NULL_RTX
14951 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14953 padsize += bitsize;
14954 continue;
14956 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14957 varloc = NOTE_VAR_LOCATION (loc_note);
14958 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14959 if (cur_descr == NULL)
14961 padsize += bitsize;
14962 continue;
14965 /* Check that cur_descr either doesn't use
14966 DW_OP_*piece operations, or their sum is equal
14967 to bitsize. Otherwise we can't embed it. */
14968 for (tail = &cur_descr; *tail != NULL;
14969 tail = &(*tail)->dw_loc_next)
14970 if ((*tail)->dw_loc_opc == DW_OP_piece)
14972 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14973 * BITS_PER_UNIT;
14974 last = *tail;
14976 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14978 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14979 last = *tail;
14982 if (last != NULL && opsize != bitsize)
14984 padsize += bitsize;
14985 continue;
14988 /* If there is a hole, add DW_OP_*piece after empty DWARF
14989 expression, which means that those bits are optimized out. */
14990 if (padsize)
14992 if (padsize > decl_size)
14993 return NULL;
14994 decl_size -= padsize;
14995 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14996 if (*descr_tail == NULL)
14997 return NULL;
14998 descr_tail = &(*descr_tail)->dw_loc_next;
14999 padsize = 0;
15001 *descr_tail = cur_descr;
15002 descr_tail = tail;
15003 if (bitsize > decl_size)
15004 return NULL;
15005 decl_size -= bitsize;
15006 if (last == NULL)
15008 HOST_WIDE_INT offset = 0;
15009 if (GET_CODE (varloc) == VAR_LOCATION
15010 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15012 varloc = PAT_VAR_LOCATION_LOC (varloc);
15013 if (GET_CODE (varloc) == EXPR_LIST)
15014 varloc = XEXP (varloc, 0);
15018 if (GET_CODE (varloc) == CONST
15019 || GET_CODE (varloc) == SIGN_EXTEND
15020 || GET_CODE (varloc) == ZERO_EXTEND)
15021 varloc = XEXP (varloc, 0);
15022 else if (GET_CODE (varloc) == SUBREG)
15023 varloc = SUBREG_REG (varloc);
15024 else
15025 break;
15027 while (1);
15028 /* DW_OP_bit_size offset should be zero for register
15029 or implicit location descriptions and empty location
15030 descriptions, but for memory addresses needs big endian
15031 adjustment. */
15032 if (MEM_P (varloc))
15034 unsigned HOST_WIDE_INT memsize
15035 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15036 if (memsize != bitsize)
15038 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15039 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15040 return NULL;
15041 if (memsize < bitsize)
15042 return NULL;
15043 if (BITS_BIG_ENDIAN)
15044 offset = memsize - bitsize;
15048 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15049 if (*descr_tail == NULL)
15050 return NULL;
15051 descr_tail = &(*descr_tail)->dw_loc_next;
15055 /* If there were any non-empty expressions, add padding till the end of
15056 the decl. */
15057 if (descr != NULL && decl_size != 0)
15059 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15060 if (*descr_tail == NULL)
15061 return NULL;
15063 return descr;
15066 /* Return the dwarf representation of the location list LOC_LIST of
15067 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
15068 function. */
15070 static dw_loc_list_ref
15071 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15073 const char *endname, *secname;
15074 rtx varloc;
15075 enum var_init_status initialized;
15076 struct var_loc_node *node;
15077 dw_loc_descr_ref descr;
15078 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15079 dw_loc_list_ref list = NULL;
15080 dw_loc_list_ref *listp = &list;
15082 /* Now that we know what section we are using for a base,
15083 actually construct the list of locations.
15084 The first location information is what is passed to the
15085 function that creates the location list, and the remaining
15086 locations just get added on to that list.
15087 Note that we only know the start address for a location
15088 (IE location changes), so to build the range, we use
15089 the range [current location start, next location start].
15090 This means we have to special case the last node, and generate
15091 a range of [last location start, end of function label]. */
15093 secname = secname_for_decl (decl);
15095 for (node = loc_list->first; node; node = node->next)
15096 if (GET_CODE (node->loc) == EXPR_LIST
15097 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15099 if (GET_CODE (node->loc) == EXPR_LIST)
15101 /* This requires DW_OP_{,bit_}piece, which is not usable
15102 inside DWARF expressions. */
15103 if (want_address != 2)
15104 continue;
15105 descr = dw_sra_loc_expr (decl, node->loc);
15106 if (descr == NULL)
15107 continue;
15109 else
15111 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15112 varloc = NOTE_VAR_LOCATION (node->loc);
15113 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15115 if (descr)
15117 bool range_across_switch = false;
15118 /* If section switch happens in between node->label
15119 and node->next->label (or end of function) and
15120 we can't emit it as a single entry list,
15121 emit two ranges, first one ending at the end
15122 of first partition and second one starting at the
15123 beginning of second partition. */
15124 if (node == loc_list->last_before_switch
15125 && (node != loc_list->first || loc_list->first->next)
15126 && current_function_decl)
15128 endname = current_fde ()->dw_fde_end;
15129 range_across_switch = true;
15131 /* The variable has a location between NODE->LABEL and
15132 NODE->NEXT->LABEL. */
15133 else if (node->next)
15134 endname = node->next->label;
15135 /* If the variable has a location at the last label
15136 it keeps its location until the end of function. */
15137 else if (!current_function_decl)
15138 endname = text_end_label;
15139 else
15141 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15142 current_function_funcdef_no);
15143 endname = ggc_strdup (label_id);
15146 *listp = new_loc_list (descr, node->label, endname, secname);
15147 listp = &(*listp)->dw_loc_next;
15149 if (range_across_switch)
15151 if (GET_CODE (node->loc) == EXPR_LIST)
15152 descr = dw_sra_loc_expr (decl, node->loc);
15153 else
15155 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15156 varloc = NOTE_VAR_LOCATION (node->loc);
15157 descr = dw_loc_list_1 (decl, varloc, want_address,
15158 initialized);
15160 gcc_assert (descr);
15161 /* The variable has a location between NODE->LABEL and
15162 NODE->NEXT->LABEL. */
15163 if (node->next)
15164 endname = node->next->label;
15165 else
15166 endname = current_fde ()->dw_fde_second_end;
15167 *listp = new_loc_list (descr,
15168 current_fde ()->dw_fde_second_begin,
15169 endname, secname);
15170 listp = &(*listp)->dw_loc_next;
15175 /* Try to avoid the overhead of a location list emitting a location
15176 expression instead, but only if we didn't have more than one
15177 location entry in the first place. If some entries were not
15178 representable, we don't want to pretend a single entry that was
15179 applies to the entire scope in which the variable is
15180 available. */
15181 if (list && loc_list->first->next)
15182 gen_llsym (list);
15184 return list;
15187 /* Return if the loc_list has only single element and thus can be represented
15188 as location description. */
15190 static bool
15191 single_element_loc_list_p (dw_loc_list_ref list)
15193 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15194 return !list->ll_symbol;
15197 /* To each location in list LIST add loc descr REF. */
15199 static void
15200 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15202 dw_loc_descr_ref copy;
15203 add_loc_descr (&list->expr, ref);
15204 list = list->dw_loc_next;
15205 while (list)
15207 copy = ggc_alloc_dw_loc_descr_node ();
15208 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15209 add_loc_descr (&list->expr, copy);
15210 while (copy->dw_loc_next)
15212 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15213 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15214 copy->dw_loc_next = new_copy;
15215 copy = new_copy;
15217 list = list->dw_loc_next;
15221 /* Given two lists RET and LIST
15222 produce location list that is result of adding expression in LIST
15223 to expression in RET on each possition in program.
15224 Might be destructive on both RET and LIST.
15226 TODO: We handle only simple cases of RET or LIST having at most one
15227 element. General case would inolve sorting the lists in program order
15228 and merging them that will need some additional work.
15229 Adding that will improve quality of debug info especially for SRA-ed
15230 structures. */
15232 static void
15233 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15235 if (!list)
15236 return;
15237 if (!*ret)
15239 *ret = list;
15240 return;
15242 if (!list->dw_loc_next)
15244 add_loc_descr_to_each (*ret, list->expr);
15245 return;
15247 if (!(*ret)->dw_loc_next)
15249 add_loc_descr_to_each (list, (*ret)->expr);
15250 *ret = list;
15251 return;
15253 expansion_failed (NULL_TREE, NULL_RTX,
15254 "Don't know how to merge two non-trivial"
15255 " location lists.\n");
15256 *ret = NULL;
15257 return;
15260 /* LOC is constant expression. Try a luck, look it up in constant
15261 pool and return its loc_descr of its address. */
15263 static dw_loc_descr_ref
15264 cst_pool_loc_descr (tree loc)
15266 /* Get an RTL for this, if something has been emitted. */
15267 rtx rtl = lookup_constant_def (loc);
15268 enum machine_mode mode;
15270 if (!rtl || !MEM_P (rtl))
15272 gcc_assert (!rtl);
15273 return 0;
15275 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15277 /* TODO: We might get more coverage if we was actually delaying expansion
15278 of all expressions till end of compilation when constant pools are fully
15279 populated. */
15280 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15282 expansion_failed (loc, NULL_RTX,
15283 "CST value in contant pool but not marked.");
15284 return 0;
15286 mode = GET_MODE (rtl);
15287 rtl = XEXP (rtl, 0);
15288 return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15291 /* Return dw_loc_list representing address of addr_expr LOC
15292 by looking for innder INDIRECT_REF expression and turing it
15293 into simple arithmetics. */
15295 static dw_loc_list_ref
15296 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15298 tree obj, offset;
15299 HOST_WIDE_INT bitsize, bitpos, bytepos;
15300 enum machine_mode mode;
15301 int volatilep;
15302 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15303 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15305 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15306 &bitsize, &bitpos, &offset, &mode,
15307 &unsignedp, &volatilep, false);
15308 STRIP_NOPS (obj);
15309 if (bitpos % BITS_PER_UNIT)
15311 expansion_failed (loc, NULL_RTX, "bitfield access");
15312 return 0;
15314 if (!INDIRECT_REF_P (obj))
15316 expansion_failed (obj,
15317 NULL_RTX, "no indirect ref in inner refrence");
15318 return 0;
15320 if (!offset && !bitpos)
15321 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15322 else if (toplev
15323 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15324 && (dwarf_version >= 4 || !dwarf_strict))
15326 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15327 if (!list_ret)
15328 return 0;
15329 if (offset)
15331 /* Variable offset. */
15332 list_ret1 = loc_list_from_tree (offset, 0);
15333 if (list_ret1 == 0)
15334 return 0;
15335 add_loc_list (&list_ret, list_ret1);
15336 if (!list_ret)
15337 return 0;
15338 add_loc_descr_to_each (list_ret,
15339 new_loc_descr (DW_OP_plus, 0, 0));
15341 bytepos = bitpos / BITS_PER_UNIT;
15342 if (bytepos > 0)
15343 add_loc_descr_to_each (list_ret,
15344 new_loc_descr (DW_OP_plus_uconst,
15345 bytepos, 0));
15346 else if (bytepos < 0)
15347 loc_list_plus_const (list_ret, bytepos);
15348 add_loc_descr_to_each (list_ret,
15349 new_loc_descr (DW_OP_stack_value, 0, 0));
15351 return list_ret;
15355 /* Generate Dwarf location list representing LOC.
15356 If WANT_ADDRESS is false, expression computing LOC will be computed
15357 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15358 if WANT_ADDRESS is 2, expression computing address useable in location
15359 will be returned (i.e. DW_OP_reg can be used
15360 to refer to register values). */
15362 static dw_loc_list_ref
15363 loc_list_from_tree (tree loc, int want_address)
15365 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15366 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15367 int have_address = 0;
15368 enum dwarf_location_atom op;
15370 /* ??? Most of the time we do not take proper care for sign/zero
15371 extending the values properly. Hopefully this won't be a real
15372 problem... */
15374 switch (TREE_CODE (loc))
15376 case ERROR_MARK:
15377 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15378 return 0;
15380 case PLACEHOLDER_EXPR:
15381 /* This case involves extracting fields from an object to determine the
15382 position of other fields. We don't try to encode this here. The
15383 only user of this is Ada, which encodes the needed information using
15384 the names of types. */
15385 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15386 return 0;
15388 case CALL_EXPR:
15389 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15390 /* There are no opcodes for these operations. */
15391 return 0;
15393 case PREINCREMENT_EXPR:
15394 case PREDECREMENT_EXPR:
15395 case POSTINCREMENT_EXPR:
15396 case POSTDECREMENT_EXPR:
15397 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15398 /* There are no opcodes for these operations. */
15399 return 0;
15401 case ADDR_EXPR:
15402 /* If we already want an address, see if there is INDIRECT_REF inside
15403 e.g. for &this->field. */
15404 if (want_address)
15406 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15407 (loc, want_address == 2);
15408 if (list_ret)
15409 have_address = 1;
15410 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15411 && (ret = cst_pool_loc_descr (loc)))
15412 have_address = 1;
15414 /* Otherwise, process the argument and look for the address. */
15415 if (!list_ret && !ret)
15416 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15417 else
15419 if (want_address)
15420 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15421 return NULL;
15423 break;
15425 case VAR_DECL:
15426 if (DECL_THREAD_LOCAL_P (loc))
15428 rtx rtl;
15429 enum dwarf_location_atom first_op;
15430 enum dwarf_location_atom second_op;
15431 bool dtprel = false;
15433 if (targetm.have_tls)
15435 /* If this is not defined, we have no way to emit the
15436 data. */
15437 if (!targetm.asm_out.output_dwarf_dtprel)
15438 return 0;
15440 /* The way DW_OP_GNU_push_tls_address is specified, we
15441 can only look up addresses of objects in the current
15442 module. We used DW_OP_addr as first op, but that's
15443 wrong, because DW_OP_addr is relocated by the debug
15444 info consumer, while DW_OP_GNU_push_tls_address
15445 operand shouldn't be. */
15446 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15447 return 0;
15448 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15449 dtprel = true;
15450 second_op = DW_OP_GNU_push_tls_address;
15452 else
15454 if (!targetm.emutls.debug_form_tls_address
15455 || !(dwarf_version >= 3 || !dwarf_strict))
15456 return 0;
15457 /* We stuffed the control variable into the DECL_VALUE_EXPR
15458 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15459 no longer appear in gimple code. We used the control
15460 variable in specific so that we could pick it up here. */
15461 loc = DECL_VALUE_EXPR (loc);
15462 first_op = DW_OP_addr;
15463 second_op = DW_OP_form_tls_address;
15466 rtl = rtl_for_decl_location (loc);
15467 if (rtl == NULL_RTX)
15468 return 0;
15470 if (!MEM_P (rtl))
15471 return 0;
15472 rtl = XEXP (rtl, 0);
15473 if (! CONSTANT_P (rtl))
15474 return 0;
15476 ret = new_loc_descr (first_op, 0, 0);
15477 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15478 ret->dw_loc_oprnd1.v.val_addr = rtl;
15479 ret->dtprel = dtprel;
15481 ret1 = new_loc_descr (second_op, 0, 0);
15482 add_loc_descr (&ret, ret1);
15484 have_address = 1;
15485 break;
15487 /* FALLTHRU */
15489 case PARM_DECL:
15490 case RESULT_DECL:
15491 if (DECL_HAS_VALUE_EXPR_P (loc))
15492 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15493 want_address);
15494 /* FALLTHRU */
15496 case FUNCTION_DECL:
15498 rtx rtl;
15499 var_loc_list *loc_list = lookup_decl_loc (loc);
15501 if (loc_list && loc_list->first)
15503 list_ret = dw_loc_list (loc_list, loc, want_address);
15504 have_address = want_address != 0;
15505 break;
15507 rtl = rtl_for_decl_location (loc);
15508 if (rtl == NULL_RTX)
15510 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15511 return 0;
15513 else if (CONST_INT_P (rtl))
15515 HOST_WIDE_INT val = INTVAL (rtl);
15516 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15517 val &= GET_MODE_MASK (DECL_MODE (loc));
15518 ret = int_loc_descriptor (val);
15520 else if (GET_CODE (rtl) == CONST_STRING)
15522 expansion_failed (loc, NULL_RTX, "CONST_STRING");
15523 return 0;
15525 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15527 ret = new_loc_descr (DW_OP_addr, 0, 0);
15528 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15529 ret->dw_loc_oprnd1.v.val_addr = rtl;
15531 else
15533 enum machine_mode mode;
15535 /* Certain constructs can only be represented at top-level. */
15536 if (want_address == 2)
15538 ret = loc_descriptor (rtl, VOIDmode,
15539 VAR_INIT_STATUS_INITIALIZED);
15540 have_address = 1;
15542 else
15544 mode = GET_MODE (rtl);
15545 if (MEM_P (rtl))
15547 rtl = XEXP (rtl, 0);
15548 have_address = 1;
15550 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15552 if (!ret)
15553 expansion_failed (loc, rtl,
15554 "failed to produce loc descriptor for rtl");
15557 break;
15559 case MEM_REF:
15560 /* ??? FIXME. */
15561 if (!integer_zerop (TREE_OPERAND (loc, 1)))
15562 return 0;
15563 /* Fallthru. */
15564 case INDIRECT_REF:
15565 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15566 have_address = 1;
15567 break;
15569 case COMPOUND_EXPR:
15570 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15572 CASE_CONVERT:
15573 case VIEW_CONVERT_EXPR:
15574 case SAVE_EXPR:
15575 case MODIFY_EXPR:
15576 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15578 case COMPONENT_REF:
15579 case BIT_FIELD_REF:
15580 case ARRAY_REF:
15581 case ARRAY_RANGE_REF:
15582 case REALPART_EXPR:
15583 case IMAGPART_EXPR:
15585 tree obj, offset;
15586 HOST_WIDE_INT bitsize, bitpos, bytepos;
15587 enum machine_mode mode;
15588 int volatilep;
15589 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15591 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15592 &unsignedp, &volatilep, false);
15594 gcc_assert (obj != loc);
15596 list_ret = loc_list_from_tree (obj,
15597 want_address == 2
15598 && !bitpos && !offset ? 2 : 1);
15599 /* TODO: We can extract value of the small expression via shifting even
15600 for nonzero bitpos. */
15601 if (list_ret == 0)
15602 return 0;
15603 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15605 expansion_failed (loc, NULL_RTX,
15606 "bitfield access");
15607 return 0;
15610 if (offset != NULL_TREE)
15612 /* Variable offset. */
15613 list_ret1 = loc_list_from_tree (offset, 0);
15614 if (list_ret1 == 0)
15615 return 0;
15616 add_loc_list (&list_ret, list_ret1);
15617 if (!list_ret)
15618 return 0;
15619 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15622 bytepos = bitpos / BITS_PER_UNIT;
15623 if (bytepos > 0)
15624 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15625 else if (bytepos < 0)
15626 loc_list_plus_const (list_ret, bytepos);
15628 have_address = 1;
15629 break;
15632 case INTEGER_CST:
15633 if ((want_address || !host_integerp (loc, 0))
15634 && (ret = cst_pool_loc_descr (loc)))
15635 have_address = 1;
15636 else if (want_address == 2
15637 && host_integerp (loc, 0)
15638 && (ret = address_of_int_loc_descriptor
15639 (int_size_in_bytes (TREE_TYPE (loc)),
15640 tree_low_cst (loc, 0))))
15641 have_address = 1;
15642 else if (host_integerp (loc, 0))
15643 ret = int_loc_descriptor (tree_low_cst (loc, 0));
15644 else
15646 expansion_failed (loc, NULL_RTX,
15647 "Integer operand is not host integer");
15648 return 0;
15650 break;
15652 case CONSTRUCTOR:
15653 case REAL_CST:
15654 case STRING_CST:
15655 case COMPLEX_CST:
15656 if ((ret = cst_pool_loc_descr (loc)))
15657 have_address = 1;
15658 else
15659 /* We can construct small constants here using int_loc_descriptor. */
15660 expansion_failed (loc, NULL_RTX,
15661 "constructor or constant not in constant pool");
15662 break;
15664 case TRUTH_AND_EXPR:
15665 case TRUTH_ANDIF_EXPR:
15666 case BIT_AND_EXPR:
15667 op = DW_OP_and;
15668 goto do_binop;
15670 case TRUTH_XOR_EXPR:
15671 case BIT_XOR_EXPR:
15672 op = DW_OP_xor;
15673 goto do_binop;
15675 case TRUTH_OR_EXPR:
15676 case TRUTH_ORIF_EXPR:
15677 case BIT_IOR_EXPR:
15678 op = DW_OP_or;
15679 goto do_binop;
15681 case FLOOR_DIV_EXPR:
15682 case CEIL_DIV_EXPR:
15683 case ROUND_DIV_EXPR:
15684 case TRUNC_DIV_EXPR:
15685 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15686 return 0;
15687 op = DW_OP_div;
15688 goto do_binop;
15690 case MINUS_EXPR:
15691 op = DW_OP_minus;
15692 goto do_binop;
15694 case FLOOR_MOD_EXPR:
15695 case CEIL_MOD_EXPR:
15696 case ROUND_MOD_EXPR:
15697 case TRUNC_MOD_EXPR:
15698 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15700 op = DW_OP_mod;
15701 goto do_binop;
15703 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15704 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15705 if (list_ret == 0 || list_ret1 == 0)
15706 return 0;
15708 add_loc_list (&list_ret, list_ret1);
15709 if (list_ret == 0)
15710 return 0;
15711 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15712 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15713 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15714 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15715 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15716 break;
15718 case MULT_EXPR:
15719 op = DW_OP_mul;
15720 goto do_binop;
15722 case LSHIFT_EXPR:
15723 op = DW_OP_shl;
15724 goto do_binop;
15726 case RSHIFT_EXPR:
15727 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15728 goto do_binop;
15730 case POINTER_PLUS_EXPR:
15731 case PLUS_EXPR:
15732 if (host_integerp (TREE_OPERAND (loc, 1), 0))
15734 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15735 if (list_ret == 0)
15736 return 0;
15738 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15739 break;
15742 op = DW_OP_plus;
15743 goto do_binop;
15745 case LE_EXPR:
15746 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15747 return 0;
15749 op = DW_OP_le;
15750 goto do_binop;
15752 case GE_EXPR:
15753 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15754 return 0;
15756 op = DW_OP_ge;
15757 goto do_binop;
15759 case LT_EXPR:
15760 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15761 return 0;
15763 op = DW_OP_lt;
15764 goto do_binop;
15766 case GT_EXPR:
15767 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15768 return 0;
15770 op = DW_OP_gt;
15771 goto do_binop;
15773 case EQ_EXPR:
15774 op = DW_OP_eq;
15775 goto do_binop;
15777 case NE_EXPR:
15778 op = DW_OP_ne;
15779 goto do_binop;
15781 do_binop:
15782 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15783 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15784 if (list_ret == 0 || list_ret1 == 0)
15785 return 0;
15787 add_loc_list (&list_ret, list_ret1);
15788 if (list_ret == 0)
15789 return 0;
15790 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15791 break;
15793 case TRUTH_NOT_EXPR:
15794 case BIT_NOT_EXPR:
15795 op = DW_OP_not;
15796 goto do_unop;
15798 case ABS_EXPR:
15799 op = DW_OP_abs;
15800 goto do_unop;
15802 case NEGATE_EXPR:
15803 op = DW_OP_neg;
15804 goto do_unop;
15806 do_unop:
15807 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15808 if (list_ret == 0)
15809 return 0;
15811 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15812 break;
15814 case MIN_EXPR:
15815 case MAX_EXPR:
15817 const enum tree_code code =
15818 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15820 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15821 build2 (code, integer_type_node,
15822 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15823 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15826 /* ... fall through ... */
15828 case COND_EXPR:
15830 dw_loc_descr_ref lhs
15831 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15832 dw_loc_list_ref rhs
15833 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15834 dw_loc_descr_ref bra_node, jump_node, tmp;
15836 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15837 if (list_ret == 0 || lhs == 0 || rhs == 0)
15838 return 0;
15840 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15841 add_loc_descr_to_each (list_ret, bra_node);
15843 add_loc_list (&list_ret, rhs);
15844 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15845 add_loc_descr_to_each (list_ret, jump_node);
15847 add_loc_descr_to_each (list_ret, lhs);
15848 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15849 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15851 /* ??? Need a node to point the skip at. Use a nop. */
15852 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15853 add_loc_descr_to_each (list_ret, tmp);
15854 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15855 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15857 break;
15859 case FIX_TRUNC_EXPR:
15860 return 0;
15862 default:
15863 /* Leave front-end specific codes as simply unknown. This comes
15864 up, for instance, with the C STMT_EXPR. */
15865 if ((unsigned int) TREE_CODE (loc)
15866 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15868 expansion_failed (loc, NULL_RTX,
15869 "language specific tree node");
15870 return 0;
15873 #ifdef ENABLE_CHECKING
15874 /* Otherwise this is a generic code; we should just lists all of
15875 these explicitly. We forgot one. */
15876 gcc_unreachable ();
15877 #else
15878 /* In a release build, we want to degrade gracefully: better to
15879 generate incomplete debugging information than to crash. */
15880 return NULL;
15881 #endif
15884 if (!ret && !list_ret)
15885 return 0;
15887 if (want_address == 2 && !have_address
15888 && (dwarf_version >= 4 || !dwarf_strict))
15890 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15892 expansion_failed (loc, NULL_RTX,
15893 "DWARF address size mismatch");
15894 return 0;
15896 if (ret)
15897 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15898 else
15899 add_loc_descr_to_each (list_ret,
15900 new_loc_descr (DW_OP_stack_value, 0, 0));
15901 have_address = 1;
15903 /* Show if we can't fill the request for an address. */
15904 if (want_address && !have_address)
15906 expansion_failed (loc, NULL_RTX,
15907 "Want address and only have value");
15908 return 0;
15911 gcc_assert (!ret || !list_ret);
15913 /* If we've got an address and don't want one, dereference. */
15914 if (!want_address && have_address)
15916 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15918 if (size > DWARF2_ADDR_SIZE || size == -1)
15920 expansion_failed (loc, NULL_RTX,
15921 "DWARF address size mismatch");
15922 return 0;
15924 else if (size == DWARF2_ADDR_SIZE)
15925 op = DW_OP_deref;
15926 else
15927 op = DW_OP_deref_size;
15929 if (ret)
15930 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15931 else
15932 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15934 if (ret)
15935 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15937 return list_ret;
15940 /* Same as above but return only single location expression. */
15941 static dw_loc_descr_ref
15942 loc_descriptor_from_tree (tree loc, int want_address)
15944 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15945 if (!ret)
15946 return NULL;
15947 if (ret->dw_loc_next)
15949 expansion_failed (loc, NULL_RTX,
15950 "Location list where only loc descriptor needed");
15951 return NULL;
15953 return ret->expr;
15956 /* Given a value, round it up to the lowest multiple of `boundary'
15957 which is not less than the value itself. */
15959 static inline HOST_WIDE_INT
15960 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15962 return (((value + boundary - 1) / boundary) * boundary);
15965 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15966 pointer to the declared type for the relevant field variable, or return
15967 `integer_type_node' if the given node turns out to be an
15968 ERROR_MARK node. */
15970 static inline tree
15971 field_type (const_tree decl)
15973 tree type;
15975 if (TREE_CODE (decl) == ERROR_MARK)
15976 return integer_type_node;
15978 type = DECL_BIT_FIELD_TYPE (decl);
15979 if (type == NULL_TREE)
15980 type = TREE_TYPE (decl);
15982 return type;
15985 /* Given a pointer to a tree node, return the alignment in bits for
15986 it, or else return BITS_PER_WORD if the node actually turns out to
15987 be an ERROR_MARK node. */
15989 static inline unsigned
15990 simple_type_align_in_bits (const_tree type)
15992 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15995 static inline unsigned
15996 simple_decl_align_in_bits (const_tree decl)
15998 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16001 /* Return the result of rounding T up to ALIGN. */
16003 static inline double_int
16004 round_up_to_align (double_int t, unsigned int align)
16006 double_int alignd = uhwi_to_double_int (align);
16007 t = double_int_add (t, alignd);
16008 t = double_int_add (t, double_int_minus_one);
16009 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16010 t = double_int_mul (t, alignd);
16011 return t;
16014 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16015 lowest addressed byte of the "containing object" for the given FIELD_DECL,
16016 or return 0 if we are unable to determine what that offset is, either
16017 because the argument turns out to be a pointer to an ERROR_MARK node, or
16018 because the offset is actually variable. (We can't handle the latter case
16019 just yet). */
16021 static HOST_WIDE_INT
16022 field_byte_offset (const_tree decl)
16024 double_int object_offset_in_bits;
16025 double_int object_offset_in_bytes;
16026 double_int bitpos_int;
16028 if (TREE_CODE (decl) == ERROR_MARK)
16029 return 0;
16031 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16033 /* We cannot yet cope with fields whose positions are variable, so
16034 for now, when we see such things, we simply return 0. Someday, we may
16035 be able to handle such cases, but it will be damn difficult. */
16036 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16037 return 0;
16039 bitpos_int = tree_to_double_int (bit_position (decl));
16041 #ifdef PCC_BITFIELD_TYPE_MATTERS
16042 if (PCC_BITFIELD_TYPE_MATTERS)
16044 tree type;
16045 tree field_size_tree;
16046 double_int deepest_bitpos;
16047 double_int field_size_in_bits;
16048 unsigned int type_align_in_bits;
16049 unsigned int decl_align_in_bits;
16050 double_int type_size_in_bits;
16052 type = field_type (decl);
16053 type_size_in_bits = double_int_type_size_in_bits (type);
16054 type_align_in_bits = simple_type_align_in_bits (type);
16056 field_size_tree = DECL_SIZE (decl);
16058 /* The size could be unspecified if there was an error, or for
16059 a flexible array member. */
16060 if (!field_size_tree)
16061 field_size_tree = bitsize_zero_node;
16063 /* If the size of the field is not constant, use the type size. */
16064 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16065 field_size_in_bits = tree_to_double_int (field_size_tree);
16066 else
16067 field_size_in_bits = type_size_in_bits;
16069 decl_align_in_bits = simple_decl_align_in_bits (decl);
16071 /* The GCC front-end doesn't make any attempt to keep track of the
16072 starting bit offset (relative to the start of the containing
16073 structure type) of the hypothetical "containing object" for a
16074 bit-field. Thus, when computing the byte offset value for the
16075 start of the "containing object" of a bit-field, we must deduce
16076 this information on our own. This can be rather tricky to do in
16077 some cases. For example, handling the following structure type
16078 definition when compiling for an i386/i486 target (which only
16079 aligns long long's to 32-bit boundaries) can be very tricky:
16081 struct S { int field1; long long field2:31; };
16083 Fortunately, there is a simple rule-of-thumb which can be used
16084 in such cases. When compiling for an i386/i486, GCC will
16085 allocate 8 bytes for the structure shown above. It decides to
16086 do this based upon one simple rule for bit-field allocation.
16087 GCC allocates each "containing object" for each bit-field at
16088 the first (i.e. lowest addressed) legitimate alignment boundary
16089 (based upon the required minimum alignment for the declared
16090 type of the field) which it can possibly use, subject to the
16091 condition that there is still enough available space remaining
16092 in the containing object (when allocated at the selected point)
16093 to fully accommodate all of the bits of the bit-field itself.
16095 This simple rule makes it obvious why GCC allocates 8 bytes for
16096 each object of the structure type shown above. When looking
16097 for a place to allocate the "containing object" for `field2',
16098 the compiler simply tries to allocate a 64-bit "containing
16099 object" at each successive 32-bit boundary (starting at zero)
16100 until it finds a place to allocate that 64- bit field such that
16101 at least 31 contiguous (and previously unallocated) bits remain
16102 within that selected 64 bit field. (As it turns out, for the
16103 example above, the compiler finds it is OK to allocate the
16104 "containing object" 64-bit field at bit-offset zero within the
16105 structure type.)
16107 Here we attempt to work backwards from the limited set of facts
16108 we're given, and we try to deduce from those facts, where GCC
16109 must have believed that the containing object started (within
16110 the structure type). The value we deduce is then used (by the
16111 callers of this routine) to generate DW_AT_location and
16112 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16113 the case of DW_AT_location, regular fields as well). */
16115 /* Figure out the bit-distance from the start of the structure to
16116 the "deepest" bit of the bit-field. */
16117 deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16119 /* This is the tricky part. Use some fancy footwork to deduce
16120 where the lowest addressed bit of the containing object must
16121 be. */
16122 object_offset_in_bits
16123 = double_int_sub (deepest_bitpos, type_size_in_bits);
16125 /* Round up to type_align by default. This works best for
16126 bitfields. */
16127 object_offset_in_bits
16128 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16130 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16132 object_offset_in_bits
16133 = double_int_sub (deepest_bitpos, type_size_in_bits);
16135 /* Round up to decl_align instead. */
16136 object_offset_in_bits
16137 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16140 else
16141 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16142 object_offset_in_bits = bitpos_int;
16144 object_offset_in_bytes
16145 = double_int_div (object_offset_in_bits,
16146 uhwi_to_double_int (BITS_PER_UNIT), true,
16147 TRUNC_DIV_EXPR);
16148 return double_int_to_shwi (object_offset_in_bytes);
16151 /* The following routines define various Dwarf attributes and any data
16152 associated with them. */
16154 /* Add a location description attribute value to a DIE.
16156 This emits location attributes suitable for whole variables and
16157 whole parameters. Note that the location attributes for struct fields are
16158 generated by the routine `data_member_location_attribute' below. */
16160 static inline void
16161 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16162 dw_loc_list_ref descr)
16164 if (descr == 0)
16165 return;
16166 if (single_element_loc_list_p (descr))
16167 add_AT_loc (die, attr_kind, descr->expr);
16168 else
16169 add_AT_loc_list (die, attr_kind, descr);
16172 /* Add DW_AT_accessibility attribute to DIE if needed. */
16174 static void
16175 add_accessibility_attribute (dw_die_ref die, tree decl)
16177 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16178 children, otherwise the default is DW_ACCESS_public. In DWARF2
16179 the default has always been DW_ACCESS_public. */
16180 if (TREE_PROTECTED (decl))
16181 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16182 else if (TREE_PRIVATE (decl))
16184 if (dwarf_version == 2
16185 || die->die_parent == NULL
16186 || die->die_parent->die_tag != DW_TAG_class_type)
16187 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16189 else if (dwarf_version > 2
16190 && die->die_parent
16191 && die->die_parent->die_tag == DW_TAG_class_type)
16192 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16195 /* Attach the specialized form of location attribute used for data members of
16196 struct and union types. In the special case of a FIELD_DECL node which
16197 represents a bit-field, the "offset" part of this special location
16198 descriptor must indicate the distance in bytes from the lowest-addressed
16199 byte of the containing struct or union type to the lowest-addressed byte of
16200 the "containing object" for the bit-field. (See the `field_byte_offset'
16201 function above).
16203 For any given bit-field, the "containing object" is a hypothetical object
16204 (of some integral or enum type) within which the given bit-field lives. The
16205 type of this hypothetical "containing object" is always the same as the
16206 declared type of the individual bit-field itself (for GCC anyway... the
16207 DWARF spec doesn't actually mandate this). Note that it is the size (in
16208 bytes) of the hypothetical "containing object" which will be given in the
16209 DW_AT_byte_size attribute for this bit-field. (See the
16210 `byte_size_attribute' function below.) It is also used when calculating the
16211 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16212 function below.) */
16214 static void
16215 add_data_member_location_attribute (dw_die_ref die, tree decl)
16217 HOST_WIDE_INT offset;
16218 dw_loc_descr_ref loc_descr = 0;
16220 if (TREE_CODE (decl) == TREE_BINFO)
16222 /* We're working on the TAG_inheritance for a base class. */
16223 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16225 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16226 aren't at a fixed offset from all (sub)objects of the same
16227 type. We need to extract the appropriate offset from our
16228 vtable. The following dwarf expression means
16230 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16232 This is specific to the V3 ABI, of course. */
16234 dw_loc_descr_ref tmp;
16236 /* Make a copy of the object address. */
16237 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16238 add_loc_descr (&loc_descr, tmp);
16240 /* Extract the vtable address. */
16241 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16242 add_loc_descr (&loc_descr, tmp);
16244 /* Calculate the address of the offset. */
16245 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16246 gcc_assert (offset < 0);
16248 tmp = int_loc_descriptor (-offset);
16249 add_loc_descr (&loc_descr, tmp);
16250 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16251 add_loc_descr (&loc_descr, tmp);
16253 /* Extract the offset. */
16254 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16255 add_loc_descr (&loc_descr, tmp);
16257 /* Add it to the object address. */
16258 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16259 add_loc_descr (&loc_descr, tmp);
16261 else
16262 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16264 else
16265 offset = field_byte_offset (decl);
16267 if (! loc_descr)
16269 if (dwarf_version > 2)
16271 /* Don't need to output a location expression, just the constant. */
16272 if (offset < 0)
16273 add_AT_int (die, DW_AT_data_member_location, offset);
16274 else
16275 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16276 return;
16278 else
16280 enum dwarf_location_atom op;
16282 /* The DWARF2 standard says that we should assume that the structure
16283 address is already on the stack, so we can specify a structure
16284 field address by using DW_OP_plus_uconst. */
16286 #ifdef MIPS_DEBUGGING_INFO
16287 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16288 operator correctly. It works only if we leave the offset on the
16289 stack. */
16290 op = DW_OP_constu;
16291 #else
16292 op = DW_OP_plus_uconst;
16293 #endif
16295 loc_descr = new_loc_descr (op, offset, 0);
16299 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16302 /* Writes integer values to dw_vec_const array. */
16304 static void
16305 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16307 while (size != 0)
16309 *dest++ = val & 0xff;
16310 val >>= 8;
16311 --size;
16315 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
16317 static HOST_WIDE_INT
16318 extract_int (const unsigned char *src, unsigned int size)
16320 HOST_WIDE_INT val = 0;
16322 src += size;
16323 while (size != 0)
16325 val <<= 8;
16326 val |= *--src & 0xff;
16327 --size;
16329 return val;
16332 /* Writes double_int values to dw_vec_const array. */
16334 static void
16335 insert_double (double_int val, unsigned char *dest)
16337 unsigned char *p0 = dest;
16338 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16340 if (WORDS_BIG_ENDIAN)
16342 p0 = p1;
16343 p1 = dest;
16346 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16347 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16350 /* Writes floating point values to dw_vec_const array. */
16352 static void
16353 insert_float (const_rtx rtl, unsigned char *array)
16355 REAL_VALUE_TYPE rv;
16356 long val[4];
16357 int i;
16359 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16360 real_to_target (val, &rv, GET_MODE (rtl));
16362 /* real_to_target puts 32-bit pieces in each long. Pack them. */
16363 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16365 insert_int (val[i], 4, array);
16366 array += 4;
16370 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16371 does not have a "location" either in memory or in a register. These
16372 things can arise in GNU C when a constant is passed as an actual parameter
16373 to an inlined function. They can also arise in C++ where declared
16374 constants do not necessarily get memory "homes". */
16376 static bool
16377 add_const_value_attribute (dw_die_ref die, rtx rtl)
16379 switch (GET_CODE (rtl))
16381 case CONST_INT:
16383 HOST_WIDE_INT val = INTVAL (rtl);
16385 if (val < 0)
16386 add_AT_int (die, DW_AT_const_value, val);
16387 else
16388 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16390 return true;
16392 case CONST_DOUBLE:
16393 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16394 floating-point constant. A CONST_DOUBLE is used whenever the
16395 constant requires more than one word in order to be adequately
16396 represented. */
16398 enum machine_mode mode = GET_MODE (rtl);
16400 if (SCALAR_FLOAT_MODE_P (mode))
16402 unsigned int length = GET_MODE_SIZE (mode);
16403 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16405 insert_float (rtl, array);
16406 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16408 else
16409 add_AT_double (die, DW_AT_const_value,
16410 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16412 return true;
16414 case CONST_VECTOR:
16416 enum machine_mode mode = GET_MODE (rtl);
16417 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16418 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16419 unsigned char *array = (unsigned char *) ggc_alloc_atomic
16420 (length * elt_size);
16421 unsigned int i;
16422 unsigned char *p;
16424 switch (GET_MODE_CLASS (mode))
16426 case MODE_VECTOR_INT:
16427 for (i = 0, p = array; i < length; i++, p += elt_size)
16429 rtx elt = CONST_VECTOR_ELT (rtl, i);
16430 double_int val = rtx_to_double_int (elt);
16432 if (elt_size <= sizeof (HOST_WIDE_INT))
16433 insert_int (double_int_to_shwi (val), elt_size, p);
16434 else
16436 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16437 insert_double (val, p);
16440 break;
16442 case MODE_VECTOR_FLOAT:
16443 for (i = 0, p = array; i < length; i++, p += elt_size)
16445 rtx elt = CONST_VECTOR_ELT (rtl, i);
16446 insert_float (elt, p);
16448 break;
16450 default:
16451 gcc_unreachable ();
16454 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16456 return true;
16458 case CONST_STRING:
16459 if (dwarf_version >= 4 || !dwarf_strict)
16461 dw_loc_descr_ref loc_result;
16462 resolve_one_addr (&rtl, NULL);
16463 rtl_addr:
16464 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16465 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16466 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16467 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16468 add_AT_loc (die, DW_AT_location, loc_result);
16469 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16470 return true;
16472 return false;
16474 case CONST:
16475 if (CONSTANT_P (XEXP (rtl, 0)))
16476 return add_const_value_attribute (die, XEXP (rtl, 0));
16477 /* FALLTHROUGH */
16478 case SYMBOL_REF:
16479 if (!const_ok_for_output (rtl))
16480 return false;
16481 case LABEL_REF:
16482 if (dwarf_version >= 4 || !dwarf_strict)
16483 goto rtl_addr;
16484 return false;
16486 case PLUS:
16487 /* In cases where an inlined instance of an inline function is passed
16488 the address of an `auto' variable (which is local to the caller) we
16489 can get a situation where the DECL_RTL of the artificial local
16490 variable (for the inlining) which acts as a stand-in for the
16491 corresponding formal parameter (of the inline function) will look
16492 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
16493 exactly a compile-time constant expression, but it isn't the address
16494 of the (artificial) local variable either. Rather, it represents the
16495 *value* which the artificial local variable always has during its
16496 lifetime. We currently have no way to represent such quasi-constant
16497 values in Dwarf, so for now we just punt and generate nothing. */
16498 return false;
16500 case HIGH:
16501 case CONST_FIXED:
16502 return false;
16504 case MEM:
16505 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16506 && MEM_READONLY_P (rtl)
16507 && GET_MODE (rtl) == BLKmode)
16509 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16510 return true;
16512 return false;
16514 default:
16515 /* No other kinds of rtx should be possible here. */
16516 gcc_unreachable ();
16518 return false;
16521 /* Determine whether the evaluation of EXPR references any variables
16522 or functions which aren't otherwise used (and therefore may not be
16523 output). */
16524 static tree
16525 reference_to_unused (tree * tp, int * walk_subtrees,
16526 void * data ATTRIBUTE_UNUSED)
16528 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16529 *walk_subtrees = 0;
16531 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16532 && ! TREE_ASM_WRITTEN (*tp))
16533 return *tp;
16534 /* ??? The C++ FE emits debug information for using decls, so
16535 putting gcc_unreachable here falls over. See PR31899. For now
16536 be conservative. */
16537 else if (!cgraph_global_info_ready
16538 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16539 return *tp;
16540 else if (TREE_CODE (*tp) == VAR_DECL)
16542 struct varpool_node *node = varpool_get_node (*tp);
16543 if (!node || !node->needed)
16544 return *tp;
16546 else if (TREE_CODE (*tp) == FUNCTION_DECL
16547 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16549 /* The call graph machinery must have finished analyzing,
16550 optimizing and gimplifying the CU by now.
16551 So if *TP has no call graph node associated
16552 to it, it means *TP will not be emitted. */
16553 if (!cgraph_get_node (*tp))
16554 return *tp;
16556 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16557 return *tp;
16559 return NULL_TREE;
16562 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16563 for use in a later add_const_value_attribute call. */
16565 static rtx
16566 rtl_for_decl_init (tree init, tree type)
16568 rtx rtl = NULL_RTX;
16570 STRIP_NOPS (init);
16572 /* If a variable is initialized with a string constant without embedded
16573 zeros, build CONST_STRING. */
16574 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16576 tree enttype = TREE_TYPE (type);
16577 tree domain = TYPE_DOMAIN (type);
16578 enum machine_mode mode = TYPE_MODE (enttype);
16580 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16581 && domain
16582 && integer_zerop (TYPE_MIN_VALUE (domain))
16583 && compare_tree_int (TYPE_MAX_VALUE (domain),
16584 TREE_STRING_LENGTH (init) - 1) == 0
16585 && ((size_t) TREE_STRING_LENGTH (init)
16586 == strlen (TREE_STRING_POINTER (init)) + 1))
16588 rtl = gen_rtx_CONST_STRING (VOIDmode,
16589 ggc_strdup (TREE_STRING_POINTER (init)));
16590 rtl = gen_rtx_MEM (BLKmode, rtl);
16591 MEM_READONLY_P (rtl) = 1;
16594 /* Other aggregates, and complex values, could be represented using
16595 CONCAT: FIXME! */
16596 else if (AGGREGATE_TYPE_P (type)
16597 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16598 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16599 || TREE_CODE (type) == COMPLEX_TYPE)
16601 /* Vectors only work if their mode is supported by the target.
16602 FIXME: generic vectors ought to work too. */
16603 else if (TREE_CODE (type) == VECTOR_TYPE
16604 && !VECTOR_MODE_P (TYPE_MODE (type)))
16606 /* If the initializer is something that we know will expand into an
16607 immediate RTL constant, expand it now. We must be careful not to
16608 reference variables which won't be output. */
16609 else if (initializer_constant_valid_p (init, type)
16610 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16612 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16613 possible. */
16614 if (TREE_CODE (type) == VECTOR_TYPE)
16615 switch (TREE_CODE (init))
16617 case VECTOR_CST:
16618 break;
16619 case CONSTRUCTOR:
16620 if (TREE_CONSTANT (init))
16622 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16623 bool constant_p = true;
16624 tree value;
16625 unsigned HOST_WIDE_INT ix;
16627 /* Even when ctor is constant, it might contain non-*_CST
16628 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16629 belong into VECTOR_CST nodes. */
16630 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16631 if (!CONSTANT_CLASS_P (value))
16633 constant_p = false;
16634 break;
16637 if (constant_p)
16639 init = build_vector_from_ctor (type, elts);
16640 break;
16643 /* FALLTHRU */
16645 default:
16646 return NULL;
16649 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16651 /* If expand_expr returns a MEM, it wasn't immediate. */
16652 gcc_assert (!rtl || !MEM_P (rtl));
16655 return rtl;
16658 /* Generate RTL for the variable DECL to represent its location. */
16660 static rtx
16661 rtl_for_decl_location (tree decl)
16663 rtx rtl;
16665 /* Here we have to decide where we are going to say the parameter "lives"
16666 (as far as the debugger is concerned). We only have a couple of
16667 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16669 DECL_RTL normally indicates where the parameter lives during most of the
16670 activation of the function. If optimization is enabled however, this
16671 could be either NULL or else a pseudo-reg. Both of those cases indicate
16672 that the parameter doesn't really live anywhere (as far as the code
16673 generation parts of GCC are concerned) during most of the function's
16674 activation. That will happen (for example) if the parameter is never
16675 referenced within the function.
16677 We could just generate a location descriptor here for all non-NULL
16678 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16679 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16680 where DECL_RTL is NULL or is a pseudo-reg.
16682 Note however that we can only get away with using DECL_INCOMING_RTL as
16683 a backup substitute for DECL_RTL in certain limited cases. In cases
16684 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16685 we can be sure that the parameter was passed using the same type as it is
16686 declared to have within the function, and that its DECL_INCOMING_RTL
16687 points us to a place where a value of that type is passed.
16689 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16690 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16691 because in these cases DECL_INCOMING_RTL points us to a value of some
16692 type which is *different* from the type of the parameter itself. Thus,
16693 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16694 such cases, the debugger would end up (for example) trying to fetch a
16695 `float' from a place which actually contains the first part of a
16696 `double'. That would lead to really incorrect and confusing
16697 output at debug-time.
16699 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16700 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
16701 are a couple of exceptions however. On little-endian machines we can
16702 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16703 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16704 an integral type that is smaller than TREE_TYPE (decl). These cases arise
16705 when (on a little-endian machine) a non-prototyped function has a
16706 parameter declared to be of type `short' or `char'. In such cases,
16707 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16708 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16709 passed `int' value. If the debugger then uses that address to fetch
16710 a `short' or a `char' (on a little-endian machine) the result will be
16711 the correct data, so we allow for such exceptional cases below.
16713 Note that our goal here is to describe the place where the given formal
16714 parameter lives during most of the function's activation (i.e. between the
16715 end of the prologue and the start of the epilogue). We'll do that as best
16716 as we can. Note however that if the given formal parameter is modified
16717 sometime during the execution of the function, then a stack backtrace (at
16718 debug-time) will show the function as having been called with the *new*
16719 value rather than the value which was originally passed in. This happens
16720 rarely enough that it is not a major problem, but it *is* a problem, and
16721 I'd like to fix it.
16723 A future version of dwarf2out.c may generate two additional attributes for
16724 any given DW_TAG_formal_parameter DIE which will describe the "passed
16725 type" and the "passed location" for the given formal parameter in addition
16726 to the attributes we now generate to indicate the "declared type" and the
16727 "active location" for each parameter. This additional set of attributes
16728 could be used by debuggers for stack backtraces. Separately, note that
16729 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16730 This happens (for example) for inlined-instances of inline function formal
16731 parameters which are never referenced. This really shouldn't be
16732 happening. All PARM_DECL nodes should get valid non-NULL
16733 DECL_INCOMING_RTL values. FIXME. */
16735 /* Use DECL_RTL as the "location" unless we find something better. */
16736 rtl = DECL_RTL_IF_SET (decl);
16738 /* When generating abstract instances, ignore everything except
16739 constants, symbols living in memory, and symbols living in
16740 fixed registers. */
16741 if (! reload_completed)
16743 if (rtl
16744 && (CONSTANT_P (rtl)
16745 || (MEM_P (rtl)
16746 && CONSTANT_P (XEXP (rtl, 0)))
16747 || (REG_P (rtl)
16748 && TREE_CODE (decl) == VAR_DECL
16749 && TREE_STATIC (decl))))
16751 rtl = targetm.delegitimize_address (rtl);
16752 return rtl;
16754 rtl = NULL_RTX;
16756 else if (TREE_CODE (decl) == PARM_DECL)
16758 if (rtl == NULL_RTX
16759 || is_pseudo_reg (rtl)
16760 || (MEM_P (rtl)
16761 && is_pseudo_reg (XEXP (rtl, 0))
16762 && DECL_INCOMING_RTL (decl)
16763 && MEM_P (DECL_INCOMING_RTL (decl))
16764 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16766 tree declared_type = TREE_TYPE (decl);
16767 tree passed_type = DECL_ARG_TYPE (decl);
16768 enum machine_mode dmode = TYPE_MODE (declared_type);
16769 enum machine_mode pmode = TYPE_MODE (passed_type);
16771 /* This decl represents a formal parameter which was optimized out.
16772 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16773 all cases where (rtl == NULL_RTX) just below. */
16774 if (dmode == pmode)
16775 rtl = DECL_INCOMING_RTL (decl);
16776 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16777 && SCALAR_INT_MODE_P (dmode)
16778 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16779 && DECL_INCOMING_RTL (decl))
16781 rtx inc = DECL_INCOMING_RTL (decl);
16782 if (REG_P (inc))
16783 rtl = inc;
16784 else if (MEM_P (inc))
16786 if (BYTES_BIG_ENDIAN)
16787 rtl = adjust_address_nv (inc, dmode,
16788 GET_MODE_SIZE (pmode)
16789 - GET_MODE_SIZE (dmode));
16790 else
16791 rtl = inc;
16796 /* If the parm was passed in registers, but lives on the stack, then
16797 make a big endian correction if the mode of the type of the
16798 parameter is not the same as the mode of the rtl. */
16799 /* ??? This is the same series of checks that are made in dbxout.c before
16800 we reach the big endian correction code there. It isn't clear if all
16801 of these checks are necessary here, but keeping them all is the safe
16802 thing to do. */
16803 else if (MEM_P (rtl)
16804 && XEXP (rtl, 0) != const0_rtx
16805 && ! CONSTANT_P (XEXP (rtl, 0))
16806 /* Not passed in memory. */
16807 && !MEM_P (DECL_INCOMING_RTL (decl))
16808 /* Not passed by invisible reference. */
16809 && (!REG_P (XEXP (rtl, 0))
16810 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16811 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16812 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16813 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16814 #endif
16816 /* Big endian correction check. */
16817 && BYTES_BIG_ENDIAN
16818 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16819 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16820 < UNITS_PER_WORD))
16822 int offset = (UNITS_PER_WORD
16823 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16825 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16826 plus_constant (XEXP (rtl, 0), offset));
16829 else if (TREE_CODE (decl) == VAR_DECL
16830 && rtl
16831 && MEM_P (rtl)
16832 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16833 && BYTES_BIG_ENDIAN)
16835 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16836 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16838 /* If a variable is declared "register" yet is smaller than
16839 a register, then if we store the variable to memory, it
16840 looks like we're storing a register-sized value, when in
16841 fact we are not. We need to adjust the offset of the
16842 storage location to reflect the actual value's bytes,
16843 else gdb will not be able to display it. */
16844 if (rsize > dsize)
16845 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16846 plus_constant (XEXP (rtl, 0), rsize-dsize));
16849 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16850 and will have been substituted directly into all expressions that use it.
16851 C does not have such a concept, but C++ and other languages do. */
16852 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16853 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16855 if (rtl)
16856 rtl = targetm.delegitimize_address (rtl);
16858 /* If we don't look past the constant pool, we risk emitting a
16859 reference to a constant pool entry that isn't referenced from
16860 code, and thus is not emitted. */
16861 if (rtl)
16862 rtl = avoid_constant_pool_reference (rtl);
16864 /* Try harder to get a rtl. If this symbol ends up not being emitted
16865 in the current CU, resolve_addr will remove the expression referencing
16866 it. */
16867 if (rtl == NULL_RTX
16868 && TREE_CODE (decl) == VAR_DECL
16869 && !DECL_EXTERNAL (decl)
16870 && TREE_STATIC (decl)
16871 && DECL_NAME (decl)
16872 && !DECL_HARD_REGISTER (decl)
16873 && DECL_MODE (decl) != VOIDmode)
16875 rtl = make_decl_rtl_for_debug (decl);
16876 if (!MEM_P (rtl)
16877 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16878 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16879 rtl = NULL_RTX;
16882 return rtl;
16885 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16886 returned. If so, the decl for the COMMON block is returned, and the
16887 value is the offset into the common block for the symbol. */
16889 static tree
16890 fortran_common (tree decl, HOST_WIDE_INT *value)
16892 tree val_expr, cvar;
16893 enum machine_mode mode;
16894 HOST_WIDE_INT bitsize, bitpos;
16895 tree offset;
16896 int volatilep = 0, unsignedp = 0;
16898 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16899 it does not have a value (the offset into the common area), or if it
16900 is thread local (as opposed to global) then it isn't common, and shouldn't
16901 be handled as such. */
16902 if (TREE_CODE (decl) != VAR_DECL
16903 || !TREE_STATIC (decl)
16904 || !DECL_HAS_VALUE_EXPR_P (decl)
16905 || !is_fortran ())
16906 return NULL_TREE;
16908 val_expr = DECL_VALUE_EXPR (decl);
16909 if (TREE_CODE (val_expr) != COMPONENT_REF)
16910 return NULL_TREE;
16912 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16913 &mode, &unsignedp, &volatilep, true);
16915 if (cvar == NULL_TREE
16916 || TREE_CODE (cvar) != VAR_DECL
16917 || DECL_ARTIFICIAL (cvar)
16918 || !TREE_PUBLIC (cvar))
16919 return NULL_TREE;
16921 *value = 0;
16922 if (offset != NULL)
16924 if (!host_integerp (offset, 0))
16925 return NULL_TREE;
16926 *value = tree_low_cst (offset, 0);
16928 if (bitpos != 0)
16929 *value += bitpos / BITS_PER_UNIT;
16931 return cvar;
16934 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16935 data attribute for a variable or a parameter. We generate the
16936 DW_AT_const_value attribute only in those cases where the given variable
16937 or parameter does not have a true "location" either in memory or in a
16938 register. This can happen (for example) when a constant is passed as an
16939 actual argument in a call to an inline function. (It's possible that
16940 these things can crop up in other ways also.) Note that one type of
16941 constant value which can be passed into an inlined function is a constant
16942 pointer. This can happen for example if an actual argument in an inlined
16943 function call evaluates to a compile-time constant address.
16945 CACHE_P is true if it is worth caching the location list for DECL,
16946 so that future calls can reuse it rather than regenerate it from scratch.
16947 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16948 since we will need to refer to them each time the function is inlined. */
16950 static bool
16951 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16952 enum dwarf_attribute attr)
16954 rtx rtl;
16955 dw_loc_list_ref list;
16956 var_loc_list *loc_list;
16957 cached_dw_loc_list *cache;
16958 void **slot;
16960 if (TREE_CODE (decl) == ERROR_MARK)
16961 return false;
16963 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16964 || TREE_CODE (decl) == RESULT_DECL);
16966 /* Try to get some constant RTL for this decl, and use that as the value of
16967 the location. */
16969 rtl = rtl_for_decl_location (decl);
16970 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16971 && add_const_value_attribute (die, rtl))
16972 return true;
16974 /* See if we have single element location list that is equivalent to
16975 a constant value. That way we are better to use add_const_value_attribute
16976 rather than expanding constant value equivalent. */
16977 loc_list = lookup_decl_loc (decl);
16978 if (loc_list
16979 && loc_list->first
16980 && loc_list->first->next == NULL
16981 && NOTE_P (loc_list->first->loc)
16982 && NOTE_VAR_LOCATION (loc_list->first->loc)
16983 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16985 struct var_loc_node *node;
16987 node = loc_list->first;
16988 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16989 if (GET_CODE (rtl) == EXPR_LIST)
16990 rtl = XEXP (rtl, 0);
16991 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16992 && add_const_value_attribute (die, rtl))
16993 return true;
16995 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16996 list several times. See if we've already cached the contents. */
16997 list = NULL;
16998 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16999 cache_p = false;
17000 if (cache_p)
17002 cache = (cached_dw_loc_list *)
17003 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
17004 if (cache)
17005 list = cache->loc_list;
17007 if (list == NULL)
17009 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17010 /* It is usually worth caching this result if the decl is from
17011 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17012 if (cache_p && list && list->dw_loc_next)
17014 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
17015 DECL_UID (decl), INSERT);
17016 cache = ggc_alloc_cleared_cached_dw_loc_list ();
17017 cache->decl_id = DECL_UID (decl);
17018 cache->loc_list = list;
17019 *slot = cache;
17022 if (list)
17024 add_AT_location_description (die, attr, list);
17025 return true;
17027 /* None of that worked, so it must not really have a location;
17028 try adding a constant value attribute from the DECL_INITIAL. */
17029 return tree_add_const_value_attribute_for_decl (die, decl);
17032 /* Add VARIABLE and DIE into deferred locations list. */
17034 static void
17035 defer_location (tree variable, dw_die_ref die)
17037 deferred_locations entry;
17038 entry.variable = variable;
17039 entry.die = die;
17040 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17043 /* Helper function for tree_add_const_value_attribute. Natively encode
17044 initializer INIT into an array. Return true if successful. */
17046 static bool
17047 native_encode_initializer (tree init, unsigned char *array, int size)
17049 tree type;
17051 if (init == NULL_TREE)
17052 return false;
17054 STRIP_NOPS (init);
17055 switch (TREE_CODE (init))
17057 case STRING_CST:
17058 type = TREE_TYPE (init);
17059 if (TREE_CODE (type) == ARRAY_TYPE)
17061 tree enttype = TREE_TYPE (type);
17062 enum machine_mode mode = TYPE_MODE (enttype);
17064 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17065 return false;
17066 if (int_size_in_bytes (type) != size)
17067 return false;
17068 if (size > TREE_STRING_LENGTH (init))
17070 memcpy (array, TREE_STRING_POINTER (init),
17071 TREE_STRING_LENGTH (init));
17072 memset (array + TREE_STRING_LENGTH (init),
17073 '\0', size - TREE_STRING_LENGTH (init));
17075 else
17076 memcpy (array, TREE_STRING_POINTER (init), size);
17077 return true;
17079 return false;
17080 case CONSTRUCTOR:
17081 type = TREE_TYPE (init);
17082 if (int_size_in_bytes (type) != size)
17083 return false;
17084 if (TREE_CODE (type) == ARRAY_TYPE)
17086 HOST_WIDE_INT min_index;
17087 unsigned HOST_WIDE_INT cnt;
17088 int curpos = 0, fieldsize;
17089 constructor_elt *ce;
17091 if (TYPE_DOMAIN (type) == NULL_TREE
17092 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17093 return false;
17095 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17096 if (fieldsize <= 0)
17097 return false;
17099 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17100 memset (array, '\0', size);
17101 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17103 tree val = ce->value;
17104 tree index = ce->index;
17105 int pos = curpos;
17106 if (index && TREE_CODE (index) == RANGE_EXPR)
17107 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17108 * fieldsize;
17109 else if (index)
17110 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17112 if (val)
17114 STRIP_NOPS (val);
17115 if (!native_encode_initializer (val, array + pos, fieldsize))
17116 return false;
17118 curpos = pos + fieldsize;
17119 if (index && TREE_CODE (index) == RANGE_EXPR)
17121 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17122 - tree_low_cst (TREE_OPERAND (index, 0), 0);
17123 while (count > 0)
17125 if (val)
17126 memcpy (array + curpos, array + pos, fieldsize);
17127 curpos += fieldsize;
17130 gcc_assert (curpos <= size);
17132 return true;
17134 else if (TREE_CODE (type) == RECORD_TYPE
17135 || TREE_CODE (type) == UNION_TYPE)
17137 tree field = NULL_TREE;
17138 unsigned HOST_WIDE_INT cnt;
17139 constructor_elt *ce;
17141 if (int_size_in_bytes (type) != size)
17142 return false;
17144 if (TREE_CODE (type) == RECORD_TYPE)
17145 field = TYPE_FIELDS (type);
17147 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17149 tree val = ce->value;
17150 int pos, fieldsize;
17152 if (ce->index != 0)
17153 field = ce->index;
17155 if (val)
17156 STRIP_NOPS (val);
17158 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17159 return false;
17161 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17162 && TYPE_DOMAIN (TREE_TYPE (field))
17163 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17164 return false;
17165 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17166 || !host_integerp (DECL_SIZE_UNIT (field), 0))
17167 return false;
17168 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17169 pos = int_byte_position (field);
17170 gcc_assert (pos + fieldsize <= size);
17171 if (val
17172 && !native_encode_initializer (val, array + pos, fieldsize))
17173 return false;
17175 return true;
17177 return false;
17178 case VIEW_CONVERT_EXPR:
17179 case NON_LVALUE_EXPR:
17180 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17181 default:
17182 return native_encode_expr (init, array, size) == size;
17186 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17187 attribute is the const value T. */
17189 static bool
17190 tree_add_const_value_attribute (dw_die_ref die, tree t)
17192 tree init;
17193 tree type = TREE_TYPE (t);
17194 rtx rtl;
17196 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17197 return false;
17199 init = t;
17200 gcc_assert (!DECL_P (init));
17202 rtl = rtl_for_decl_init (init, type);
17203 if (rtl)
17204 return add_const_value_attribute (die, rtl);
17205 /* If the host and target are sane, try harder. */
17206 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17207 && initializer_constant_valid_p (init, type))
17209 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17210 if (size > 0 && (int) size == size)
17212 unsigned char *array = (unsigned char *)
17213 ggc_alloc_cleared_atomic (size);
17215 if (native_encode_initializer (init, array, size))
17217 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17218 return true;
17222 return false;
17225 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17226 attribute is the const value of T, where T is an integral constant
17227 variable with static storage duration
17228 (so it can't be a PARM_DECL or a RESULT_DECL). */
17230 static bool
17231 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17234 if (!decl
17235 || (TREE_CODE (decl) != VAR_DECL
17236 && TREE_CODE (decl) != CONST_DECL))
17237 return false;
17239 if (TREE_READONLY (decl)
17240 && ! TREE_THIS_VOLATILE (decl)
17241 && DECL_INITIAL (decl))
17242 /* OK */;
17243 else
17244 return false;
17246 /* Don't add DW_AT_const_value if abstract origin already has one. */
17247 if (get_AT (var_die, DW_AT_const_value))
17248 return false;
17250 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17253 /* Convert the CFI instructions for the current function into a
17254 location list. This is used for DW_AT_frame_base when we targeting
17255 a dwarf2 consumer that does not support the dwarf3
17256 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17257 expressions. */
17259 static dw_loc_list_ref
17260 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17262 dw_fde_ref fde;
17263 dw_loc_list_ref list, *list_tail;
17264 dw_cfi_ref cfi;
17265 dw_cfa_location last_cfa, next_cfa;
17266 const char *start_label, *last_label, *section;
17267 dw_cfa_location remember;
17269 fde = current_fde ();
17270 gcc_assert (fde != NULL);
17272 section = secname_for_decl (current_function_decl);
17273 list_tail = &list;
17274 list = NULL;
17276 memset (&next_cfa, 0, sizeof (next_cfa));
17277 next_cfa.reg = INVALID_REGNUM;
17278 remember = next_cfa;
17280 start_label = fde->dw_fde_begin;
17282 /* ??? Bald assumption that the CIE opcode list does not contain
17283 advance opcodes. */
17284 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17285 lookup_cfa_1 (cfi, &next_cfa, &remember);
17287 last_cfa = next_cfa;
17288 last_label = start_label;
17290 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi == NULL)
17292 /* If the first partition contained no CFI adjustments, the
17293 CIE opcodes apply to the whole first partition. */
17294 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17295 fde->dw_fde_begin, fde->dw_fde_end, section);
17296 list_tail =&(*list_tail)->dw_loc_next;
17297 start_label = last_label = fde->dw_fde_second_begin;
17300 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17302 switch (cfi->dw_cfi_opc)
17304 case DW_CFA_set_loc:
17305 case DW_CFA_advance_loc1:
17306 case DW_CFA_advance_loc2:
17307 case DW_CFA_advance_loc4:
17308 if (!cfa_equal_p (&last_cfa, &next_cfa))
17310 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17311 start_label, last_label, section);
17313 list_tail = &(*list_tail)->dw_loc_next;
17314 last_cfa = next_cfa;
17315 start_label = last_label;
17317 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17318 break;
17320 case DW_CFA_advance_loc:
17321 /* The encoding is complex enough that we should never emit this. */
17322 gcc_unreachable ();
17324 default:
17325 lookup_cfa_1 (cfi, &next_cfa, &remember);
17326 break;
17328 if (cfi == fde->dw_fde_switch_cfi)
17330 if (!cfa_equal_p (&last_cfa, &next_cfa))
17332 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17333 start_label, last_label, section);
17335 list_tail = &(*list_tail)->dw_loc_next;
17336 last_cfa = next_cfa;
17337 start_label = last_label;
17339 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17340 start_label, fde->dw_fde_end, section);
17341 list_tail = &(*list_tail)->dw_loc_next;
17342 start_label = last_label = fde->dw_fde_second_begin;
17346 if (!cfa_equal_p (&last_cfa, &next_cfa))
17348 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17349 start_label, last_label, section);
17350 list_tail = &(*list_tail)->dw_loc_next;
17351 start_label = last_label;
17354 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17355 start_label,
17356 fde->dw_fde_second_begin
17357 ? fde->dw_fde_second_end : fde->dw_fde_end,
17358 section);
17360 if (list && list->dw_loc_next)
17361 gen_llsym (list);
17363 return list;
17366 /* Compute a displacement from the "steady-state frame pointer" to the
17367 frame base (often the same as the CFA), and store it in
17368 frame_pointer_fb_offset. OFFSET is added to the displacement
17369 before the latter is negated. */
17371 static void
17372 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17374 rtx reg, elim;
17376 #ifdef FRAME_POINTER_CFA_OFFSET
17377 reg = frame_pointer_rtx;
17378 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17379 #else
17380 reg = arg_pointer_rtx;
17381 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17382 #endif
17384 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17385 if (GET_CODE (elim) == PLUS)
17387 offset += INTVAL (XEXP (elim, 1));
17388 elim = XEXP (elim, 0);
17391 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17392 && (elim == hard_frame_pointer_rtx
17393 || elim == stack_pointer_rtx))
17394 || elim == (frame_pointer_needed
17395 ? hard_frame_pointer_rtx
17396 : stack_pointer_rtx));
17398 frame_pointer_fb_offset = -offset;
17401 /* Generate a DW_AT_name attribute given some string value to be included as
17402 the value of the attribute. */
17404 static void
17405 add_name_attribute (dw_die_ref die, const char *name_string)
17407 if (name_string != NULL && *name_string != 0)
17409 if (demangle_name_func)
17410 name_string = (*demangle_name_func) (name_string);
17412 add_AT_string (die, DW_AT_name, name_string);
17416 /* Generate a DW_AT_comp_dir attribute for DIE. */
17418 static void
17419 add_comp_dir_attribute (dw_die_ref die)
17421 const char *wd = get_src_pwd ();
17422 char *wd1;
17424 if (wd == NULL)
17425 return;
17427 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17429 int wdlen;
17431 wdlen = strlen (wd);
17432 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17433 strcpy (wd1, wd);
17434 wd1 [wdlen] = DIR_SEPARATOR;
17435 wd1 [wdlen + 1] = 0;
17436 wd = wd1;
17439 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17442 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17443 default. */
17445 static int
17446 lower_bound_default (void)
17448 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17450 case DW_LANG_C:
17451 case DW_LANG_C89:
17452 case DW_LANG_C99:
17453 case DW_LANG_C_plus_plus:
17454 case DW_LANG_ObjC:
17455 case DW_LANG_ObjC_plus_plus:
17456 case DW_LANG_Java:
17457 return 0;
17458 case DW_LANG_Fortran77:
17459 case DW_LANG_Fortran90:
17460 case DW_LANG_Fortran95:
17461 return 1;
17462 case DW_LANG_UPC:
17463 case DW_LANG_D:
17464 case DW_LANG_Python:
17465 return dwarf_version >= 4 ? 0 : -1;
17466 case DW_LANG_Ada95:
17467 case DW_LANG_Ada83:
17468 case DW_LANG_Cobol74:
17469 case DW_LANG_Cobol85:
17470 case DW_LANG_Pascal83:
17471 case DW_LANG_Modula2:
17472 case DW_LANG_PLI:
17473 return dwarf_version >= 4 ? 1 : -1;
17474 default:
17475 return -1;
17479 /* Given a tree node describing an array bound (either lower or upper) output
17480 a representation for that bound. */
17482 static void
17483 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17485 switch (TREE_CODE (bound))
17487 case ERROR_MARK:
17488 return;
17490 /* All fixed-bounds are represented by INTEGER_CST nodes. */
17491 case INTEGER_CST:
17493 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17494 int dflt;
17496 /* Use the default if possible. */
17497 if (bound_attr == DW_AT_lower_bound
17498 && host_integerp (bound, 0)
17499 && (dflt = lower_bound_default ()) != -1
17500 && tree_low_cst (bound, 0) == dflt)
17503 /* Otherwise represent the bound as an unsigned value with the
17504 precision of its type. The precision and signedness of the
17505 type will be necessary to re-interpret it unambiguously. */
17506 else if (prec < HOST_BITS_PER_WIDE_INT)
17508 unsigned HOST_WIDE_INT mask
17509 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17510 add_AT_unsigned (subrange_die, bound_attr,
17511 TREE_INT_CST_LOW (bound) & mask);
17513 else if (prec == HOST_BITS_PER_WIDE_INT
17514 || TREE_INT_CST_HIGH (bound) == 0)
17515 add_AT_unsigned (subrange_die, bound_attr,
17516 TREE_INT_CST_LOW (bound));
17517 else
17518 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17519 TREE_INT_CST_LOW (bound));
17521 break;
17523 CASE_CONVERT:
17524 case VIEW_CONVERT_EXPR:
17525 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17526 break;
17528 case SAVE_EXPR:
17529 break;
17531 case VAR_DECL:
17532 case PARM_DECL:
17533 case RESULT_DECL:
17535 dw_die_ref decl_die = lookup_decl_die (bound);
17537 /* ??? Can this happen, or should the variable have been bound
17538 first? Probably it can, since I imagine that we try to create
17539 the types of parameters in the order in which they exist in
17540 the list, and won't have created a forward reference to a
17541 later parameter. */
17542 if (decl_die != NULL)
17544 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17545 break;
17548 /* FALLTHRU */
17550 default:
17552 /* Otherwise try to create a stack operation procedure to
17553 evaluate the value of the array bound. */
17555 dw_die_ref ctx, decl_die;
17556 dw_loc_list_ref list;
17558 list = loc_list_from_tree (bound, 2);
17559 if (list == NULL || single_element_loc_list_p (list))
17561 /* If DW_AT_*bound is not a reference nor constant, it is
17562 a DWARF expression rather than location description.
17563 For that loc_list_from_tree (bound, 0) is needed.
17564 If that fails to give a single element list,
17565 fall back to outputting this as a reference anyway. */
17566 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17567 if (list2 && single_element_loc_list_p (list2))
17569 add_AT_loc (subrange_die, bound_attr, list2->expr);
17570 break;
17573 if (list == NULL)
17574 break;
17576 if (current_function_decl == 0)
17577 ctx = comp_unit_die ();
17578 else
17579 ctx = lookup_decl_die (current_function_decl);
17581 decl_die = new_die (DW_TAG_variable, ctx, bound);
17582 add_AT_flag (decl_die, DW_AT_artificial, 1);
17583 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17584 add_AT_location_description (decl_die, DW_AT_location, list);
17585 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17586 break;
17591 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17592 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17593 Note that the block of subscript information for an array type also
17594 includes information about the element type of the given array type. */
17596 static void
17597 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17599 unsigned dimension_number;
17600 tree lower, upper;
17601 dw_die_ref subrange_die;
17603 for (dimension_number = 0;
17604 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17605 type = TREE_TYPE (type), dimension_number++)
17607 tree domain = TYPE_DOMAIN (type);
17609 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17610 break;
17612 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17613 and (in GNU C only) variable bounds. Handle all three forms
17614 here. */
17615 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17616 if (domain)
17618 /* We have an array type with specified bounds. */
17619 lower = TYPE_MIN_VALUE (domain);
17620 upper = TYPE_MAX_VALUE (domain);
17622 /* Define the index type. */
17623 if (TREE_TYPE (domain))
17625 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17626 TREE_TYPE field. We can't emit debug info for this
17627 because it is an unnamed integral type. */
17628 if (TREE_CODE (domain) == INTEGER_TYPE
17629 && TYPE_NAME (domain) == NULL_TREE
17630 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17631 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17633 else
17634 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17635 type_die);
17638 /* ??? If upper is NULL, the array has unspecified length,
17639 but it does have a lower bound. This happens with Fortran
17640 dimension arr(N:*)
17641 Since the debugger is definitely going to need to know N
17642 to produce useful results, go ahead and output the lower
17643 bound solo, and hope the debugger can cope. */
17645 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17646 if (upper)
17647 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17650 /* Otherwise we have an array type with an unspecified length. The
17651 DWARF-2 spec does not say how to handle this; let's just leave out the
17652 bounds. */
17656 static void
17657 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17659 unsigned size;
17661 switch (TREE_CODE (tree_node))
17663 case ERROR_MARK:
17664 size = 0;
17665 break;
17666 case ENUMERAL_TYPE:
17667 case RECORD_TYPE:
17668 case UNION_TYPE:
17669 case QUAL_UNION_TYPE:
17670 size = int_size_in_bytes (tree_node);
17671 break;
17672 case FIELD_DECL:
17673 /* For a data member of a struct or union, the DW_AT_byte_size is
17674 generally given as the number of bytes normally allocated for an
17675 object of the *declared* type of the member itself. This is true
17676 even for bit-fields. */
17677 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17678 break;
17679 default:
17680 gcc_unreachable ();
17683 /* Note that `size' might be -1 when we get to this point. If it is, that
17684 indicates that the byte size of the entity in question is variable. We
17685 have no good way of expressing this fact in Dwarf at the present time,
17686 so just let the -1 pass on through. */
17687 add_AT_unsigned (die, DW_AT_byte_size, size);
17690 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17691 which specifies the distance in bits from the highest order bit of the
17692 "containing object" for the bit-field to the highest order bit of the
17693 bit-field itself.
17695 For any given bit-field, the "containing object" is a hypothetical object
17696 (of some integral or enum type) within which the given bit-field lives. The
17697 type of this hypothetical "containing object" is always the same as the
17698 declared type of the individual bit-field itself. The determination of the
17699 exact location of the "containing object" for a bit-field is rather
17700 complicated. It's handled by the `field_byte_offset' function (above).
17702 Note that it is the size (in bytes) of the hypothetical "containing object"
17703 which will be given in the DW_AT_byte_size attribute for this bit-field.
17704 (See `byte_size_attribute' above). */
17706 static inline void
17707 add_bit_offset_attribute (dw_die_ref die, tree decl)
17709 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17710 tree type = DECL_BIT_FIELD_TYPE (decl);
17711 HOST_WIDE_INT bitpos_int;
17712 HOST_WIDE_INT highest_order_object_bit_offset;
17713 HOST_WIDE_INT highest_order_field_bit_offset;
17714 HOST_WIDE_INT bit_offset;
17716 /* Must be a field and a bit field. */
17717 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17719 /* We can't yet handle bit-fields whose offsets are variable, so if we
17720 encounter such things, just return without generating any attribute
17721 whatsoever. Likewise for variable or too large size. */
17722 if (! host_integerp (bit_position (decl), 0)
17723 || ! host_integerp (DECL_SIZE (decl), 1))
17724 return;
17726 bitpos_int = int_bit_position (decl);
17728 /* Note that the bit offset is always the distance (in bits) from the
17729 highest-order bit of the "containing object" to the highest-order bit of
17730 the bit-field itself. Since the "high-order end" of any object or field
17731 is different on big-endian and little-endian machines, the computation
17732 below must take account of these differences. */
17733 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17734 highest_order_field_bit_offset = bitpos_int;
17736 if (! BYTES_BIG_ENDIAN)
17738 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17739 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17742 bit_offset
17743 = (! BYTES_BIG_ENDIAN
17744 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17745 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17747 if (bit_offset < 0)
17748 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17749 else
17750 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17753 /* For a FIELD_DECL node which represents a bit field, output an attribute
17754 which specifies the length in bits of the given field. */
17756 static inline void
17757 add_bit_size_attribute (dw_die_ref die, tree decl)
17759 /* Must be a field and a bit field. */
17760 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17761 && DECL_BIT_FIELD_TYPE (decl));
17763 if (host_integerp (DECL_SIZE (decl), 1))
17764 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17767 /* If the compiled language is ANSI C, then add a 'prototyped'
17768 attribute, if arg types are given for the parameters of a function. */
17770 static inline void
17771 add_prototyped_attribute (dw_die_ref die, tree func_type)
17773 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17774 && prototype_p (func_type))
17775 add_AT_flag (die, DW_AT_prototyped, 1);
17778 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17779 by looking in either the type declaration or object declaration
17780 equate table. */
17782 static inline dw_die_ref
17783 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17785 dw_die_ref origin_die = NULL;
17787 if (TREE_CODE (origin) != FUNCTION_DECL)
17789 /* We may have gotten separated from the block for the inlined
17790 function, if we're in an exception handler or some such; make
17791 sure that the abstract function has been written out.
17793 Doing this for nested functions is wrong, however; functions are
17794 distinct units, and our context might not even be inline. */
17795 tree fn = origin;
17797 if (TYPE_P (fn))
17798 fn = TYPE_STUB_DECL (fn);
17800 fn = decl_function_context (fn);
17801 if (fn)
17802 dwarf2out_abstract_function (fn);
17805 if (DECL_P (origin))
17806 origin_die = lookup_decl_die (origin);
17807 else if (TYPE_P (origin))
17808 origin_die = lookup_type_die (origin);
17810 /* XXX: Functions that are never lowered don't always have correct block
17811 trees (in the case of java, they simply have no block tree, in some other
17812 languages). For these functions, there is nothing we can really do to
17813 output correct debug info for inlined functions in all cases. Rather
17814 than die, we'll just produce deficient debug info now, in that we will
17815 have variables without a proper abstract origin. In the future, when all
17816 functions are lowered, we should re-add a gcc_assert (origin_die)
17817 here. */
17819 if (origin_die)
17820 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17821 return origin_die;
17824 /* We do not currently support the pure_virtual attribute. */
17826 static inline void
17827 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17829 if (DECL_VINDEX (func_decl))
17831 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17833 if (host_integerp (DECL_VINDEX (func_decl), 0))
17834 add_AT_loc (die, DW_AT_vtable_elem_location,
17835 new_loc_descr (DW_OP_constu,
17836 tree_low_cst (DECL_VINDEX (func_decl), 0),
17837 0));
17839 /* GNU extension: Record what type this method came from originally. */
17840 if (debug_info_level > DINFO_LEVEL_TERSE
17841 && DECL_CONTEXT (func_decl))
17842 add_AT_die_ref (die, DW_AT_containing_type,
17843 lookup_type_die (DECL_CONTEXT (func_decl)));
17847 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17848 given decl. This used to be a vendor extension until after DWARF 4
17849 standardized it. */
17851 static void
17852 add_linkage_attr (dw_die_ref die, tree decl)
17854 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17856 /* Mimic what assemble_name_raw does with a leading '*'. */
17857 if (name[0] == '*')
17858 name = &name[1];
17860 if (dwarf_version >= 4)
17861 add_AT_string (die, DW_AT_linkage_name, name);
17862 else
17863 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17866 /* Add source coordinate attributes for the given decl. */
17868 static void
17869 add_src_coords_attributes (dw_die_ref die, tree decl)
17871 expanded_location s;
17873 if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17874 return;
17875 s = expand_location (DECL_SOURCE_LOCATION (decl));
17876 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17877 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17880 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17882 static void
17883 add_linkage_name (dw_die_ref die, tree decl)
17885 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17886 && TREE_PUBLIC (decl)
17887 && !DECL_ABSTRACT (decl)
17888 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17889 && die->die_tag != DW_TAG_member)
17891 /* Defer until we have an assembler name set. */
17892 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17894 limbo_die_node *asm_name;
17896 asm_name = ggc_alloc_cleared_limbo_die_node ();
17897 asm_name->die = die;
17898 asm_name->created_for = decl;
17899 asm_name->next = deferred_asm_name;
17900 deferred_asm_name = asm_name;
17902 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17903 add_linkage_attr (die, decl);
17907 /* Add a DW_AT_name attribute and source coordinate attribute for the
17908 given decl, but only if it actually has a name. */
17910 static void
17911 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17913 tree decl_name;
17915 decl_name = DECL_NAME (decl);
17916 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17918 const char *name = dwarf2_name (decl, 0);
17919 if (name)
17920 add_name_attribute (die, name);
17921 if (! DECL_ARTIFICIAL (decl))
17922 add_src_coords_attributes (die, decl);
17924 add_linkage_name (die, decl);
17927 #ifdef VMS_DEBUGGING_INFO
17928 /* Get the function's name, as described by its RTL. This may be different
17929 from the DECL_NAME name used in the source file. */
17930 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17932 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17933 XEXP (DECL_RTL (decl), 0));
17934 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17936 #endif /* VMS_DEBUGGING_INFO */
17939 #ifdef VMS_DEBUGGING_INFO
17940 /* Output the debug main pointer die for VMS */
17942 void
17943 dwarf2out_vms_debug_main_pointer (void)
17945 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17946 dw_die_ref die;
17948 /* Allocate the VMS debug main subprogram die. */
17949 die = ggc_alloc_cleared_die_node ();
17950 die->die_tag = DW_TAG_subprogram;
17951 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17952 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17953 current_function_funcdef_no);
17954 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17956 /* Make it the first child of comp_unit_die (). */
17957 die->die_parent = comp_unit_die ();
17958 if (comp_unit_die ()->die_child)
17960 die->die_sib = comp_unit_die ()->die_child->die_sib;
17961 comp_unit_die ()->die_child->die_sib = die;
17963 else
17965 die->die_sib = die;
17966 comp_unit_die ()->die_child = die;
17969 #endif /* VMS_DEBUGGING_INFO */
17971 /* Push a new declaration scope. */
17973 static void
17974 push_decl_scope (tree scope)
17976 VEC_safe_push (tree, gc, decl_scope_table, scope);
17979 /* Pop a declaration scope. */
17981 static inline void
17982 pop_decl_scope (void)
17984 VEC_pop (tree, decl_scope_table);
17987 /* Return the DIE for the scope that immediately contains this type.
17988 Non-named types get global scope. Named types nested in other
17989 types get their containing scope if it's open, or global scope
17990 otherwise. All other types (i.e. function-local named types) get
17991 the current active scope. */
17993 static dw_die_ref
17994 scope_die_for (tree t, dw_die_ref context_die)
17996 dw_die_ref scope_die = NULL;
17997 tree containing_scope;
17998 int i;
18000 /* Non-types always go in the current scope. */
18001 gcc_assert (TYPE_P (t));
18003 containing_scope = TYPE_CONTEXT (t);
18005 /* Use the containing namespace if it was passed in (for a declaration). */
18006 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18008 if (context_die == lookup_decl_die (containing_scope))
18009 /* OK */;
18010 else
18011 containing_scope = NULL_TREE;
18014 /* Ignore function type "scopes" from the C frontend. They mean that
18015 a tagged type is local to a parmlist of a function declarator, but
18016 that isn't useful to DWARF. */
18017 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18018 containing_scope = NULL_TREE;
18020 if (SCOPE_FILE_SCOPE_P (containing_scope))
18021 scope_die = comp_unit_die ();
18022 else if (TYPE_P (containing_scope))
18024 /* For types, we can just look up the appropriate DIE. But
18025 first we check to see if we're in the middle of emitting it
18026 so we know where the new DIE should go. */
18027 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
18028 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
18029 break;
18031 if (i < 0)
18033 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
18034 || TREE_ASM_WRITTEN (containing_scope));
18035 /*We are not in the middle of emitting the type
18036 CONTAINING_SCOPE. Let's see if it's emitted already. */
18037 scope_die = lookup_type_die (containing_scope);
18039 /* If none of the current dies are suitable, we get file scope. */
18040 if (scope_die == NULL)
18041 scope_die = comp_unit_die ();
18043 else
18044 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18046 else
18047 scope_die = context_die;
18049 return scope_die;
18052 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
18054 static inline int
18055 local_scope_p (dw_die_ref context_die)
18057 for (; context_die; context_die = context_die->die_parent)
18058 if (context_die->die_tag == DW_TAG_inlined_subroutine
18059 || context_die->die_tag == DW_TAG_subprogram)
18060 return 1;
18062 return 0;
18065 /* Returns nonzero if CONTEXT_DIE is a class. */
18067 static inline int
18068 class_scope_p (dw_die_ref context_die)
18070 return (context_die
18071 && (context_die->die_tag == DW_TAG_structure_type
18072 || context_die->die_tag == DW_TAG_class_type
18073 || context_die->die_tag == DW_TAG_interface_type
18074 || context_die->die_tag == DW_TAG_union_type));
18077 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18078 whether or not to treat a DIE in this context as a declaration. */
18080 static inline int
18081 class_or_namespace_scope_p (dw_die_ref context_die)
18083 return (class_scope_p (context_die)
18084 || (context_die && context_die->die_tag == DW_TAG_namespace));
18087 /* Many forms of DIEs require a "type description" attribute. This
18088 routine locates the proper "type descriptor" die for the type given
18089 by 'type', and adds a DW_AT_type attribute below the given die. */
18091 static void
18092 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18093 int decl_volatile, dw_die_ref context_die)
18095 enum tree_code code = TREE_CODE (type);
18096 dw_die_ref type_die = NULL;
18098 /* ??? If this type is an unnamed subrange type of an integral, floating-point
18099 or fixed-point type, use the inner type. This is because we have no
18100 support for unnamed types in base_type_die. This can happen if this is
18101 an Ada subrange type. Correct solution is emit a subrange type die. */
18102 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18103 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18104 type = TREE_TYPE (type), code = TREE_CODE (type);
18106 if (code == ERROR_MARK
18107 /* Handle a special case. For functions whose return type is void, we
18108 generate *no* type attribute. (Note that no object may have type
18109 `void', so this only applies to function return types). */
18110 || code == VOID_TYPE)
18111 return;
18113 type_die = modified_type_die (type,
18114 decl_const || TYPE_READONLY (type),
18115 decl_volatile || TYPE_VOLATILE (type),
18116 context_die);
18118 if (type_die != NULL)
18119 add_AT_die_ref (object_die, DW_AT_type, type_die);
18122 /* Given an object die, add the calling convention attribute for the
18123 function call type. */
18124 static void
18125 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18127 enum dwarf_calling_convention value = DW_CC_normal;
18129 value = ((enum dwarf_calling_convention)
18130 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18132 if (is_fortran ()
18133 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18135 /* DWARF 2 doesn't provide a way to identify a program's source-level
18136 entry point. DW_AT_calling_convention attributes are only meant
18137 to describe functions' calling conventions. However, lacking a
18138 better way to signal the Fortran main program, we used this for
18139 a long time, following existing custom. Now, DWARF 4 has
18140 DW_AT_main_subprogram, which we add below, but some tools still
18141 rely on the old way, which we thus keep. */
18142 value = DW_CC_program;
18144 if (dwarf_version >= 4 || !dwarf_strict)
18145 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18148 /* Only add the attribute if the backend requests it, and
18149 is not DW_CC_normal. */
18150 if (value && (value != DW_CC_normal))
18151 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18154 /* Given a tree pointer to a struct, class, union, or enum type node, return
18155 a pointer to the (string) tag name for the given type, or zero if the type
18156 was declared without a tag. */
18158 static const char *
18159 type_tag (const_tree type)
18161 const char *name = 0;
18163 if (TYPE_NAME (type) != 0)
18165 tree t = 0;
18167 /* Find the IDENTIFIER_NODE for the type name. */
18168 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18169 && !TYPE_NAMELESS (type))
18170 t = TYPE_NAME (type);
18172 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18173 a TYPE_DECL node, regardless of whether or not a `typedef' was
18174 involved. */
18175 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18176 && ! DECL_IGNORED_P (TYPE_NAME (type)))
18178 /* We want to be extra verbose. Don't call dwarf_name if
18179 DECL_NAME isn't set. The default hook for decl_printable_name
18180 doesn't like that, and in this context it's correct to return
18181 0, instead of "<anonymous>" or the like. */
18182 if (DECL_NAME (TYPE_NAME (type))
18183 && !DECL_NAMELESS (TYPE_NAME (type)))
18184 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18187 /* Now get the name as a string, or invent one. */
18188 if (!name && t != 0)
18189 name = IDENTIFIER_POINTER (t);
18192 return (name == 0 || *name == '\0') ? 0 : name;
18195 /* Return the type associated with a data member, make a special check
18196 for bit field types. */
18198 static inline tree
18199 member_declared_type (const_tree member)
18201 return (DECL_BIT_FIELD_TYPE (member)
18202 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18205 /* Get the decl's label, as described by its RTL. This may be different
18206 from the DECL_NAME name used in the source file. */
18208 #if 0
18209 static const char *
18210 decl_start_label (tree decl)
18212 rtx x;
18213 const char *fnname;
18215 x = DECL_RTL (decl);
18216 gcc_assert (MEM_P (x));
18218 x = XEXP (x, 0);
18219 gcc_assert (GET_CODE (x) == SYMBOL_REF);
18221 fnname = XSTR (x, 0);
18222 return fnname;
18224 #endif
18226 /* These routines generate the internal representation of the DIE's for
18227 the compilation unit. Debugging information is collected by walking
18228 the declaration trees passed in from dwarf2out_decl(). */
18230 static void
18231 gen_array_type_die (tree type, dw_die_ref context_die)
18233 dw_die_ref scope_die = scope_die_for (type, context_die);
18234 dw_die_ref array_die;
18236 /* GNU compilers represent multidimensional array types as sequences of one
18237 dimensional array types whose element types are themselves array types.
18238 We sometimes squish that down to a single array_type DIE with multiple
18239 subscripts in the Dwarf debugging info. The draft Dwarf specification
18240 say that we are allowed to do this kind of compression in C, because
18241 there is no difference between an array of arrays and a multidimensional
18242 array. We don't do this for Ada to remain as close as possible to the
18243 actual representation, which is especially important against the language
18244 flexibilty wrt arrays of variable size. */
18246 bool collapse_nested_arrays = !is_ada ();
18247 tree element_type;
18249 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18250 DW_TAG_string_type doesn't have DW_AT_type attribute). */
18251 if (TYPE_STRING_FLAG (type)
18252 && TREE_CODE (type) == ARRAY_TYPE
18253 && is_fortran ()
18254 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18256 HOST_WIDE_INT size;
18258 array_die = new_die (DW_TAG_string_type, scope_die, type);
18259 add_name_attribute (array_die, type_tag (type));
18260 equate_type_number_to_die (type, array_die);
18261 size = int_size_in_bytes (type);
18262 if (size >= 0)
18263 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18264 else if (TYPE_DOMAIN (type) != NULL_TREE
18265 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18266 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18268 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18269 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18271 size = int_size_in_bytes (TREE_TYPE (szdecl));
18272 if (loc && size > 0)
18274 add_AT_location_description (array_die, DW_AT_string_length, loc);
18275 if (size != DWARF2_ADDR_SIZE)
18276 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18279 return;
18282 /* ??? The SGI dwarf reader fails for array of array of enum types
18283 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18284 array type comes before the outer array type. We thus call gen_type_die
18285 before we new_die and must prevent nested array types collapsing for this
18286 target. */
18288 #ifdef MIPS_DEBUGGING_INFO
18289 gen_type_die (TREE_TYPE (type), context_die);
18290 collapse_nested_arrays = false;
18291 #endif
18293 array_die = new_die (DW_TAG_array_type, scope_die, type);
18294 add_name_attribute (array_die, type_tag (type));
18295 equate_type_number_to_die (type, array_die);
18297 if (TREE_CODE (type) == VECTOR_TYPE)
18298 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18300 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18301 if (is_fortran ()
18302 && TREE_CODE (type) == ARRAY_TYPE
18303 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18304 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18305 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18307 #if 0
18308 /* We default the array ordering. SDB will probably do
18309 the right things even if DW_AT_ordering is not present. It's not even
18310 an issue until we start to get into multidimensional arrays anyway. If
18311 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18312 then we'll have to put the DW_AT_ordering attribute back in. (But if
18313 and when we find out that we need to put these in, we will only do so
18314 for multidimensional arrays. */
18315 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18316 #endif
18318 #ifdef MIPS_DEBUGGING_INFO
18319 /* The SGI compilers handle arrays of unknown bound by setting
18320 AT_declaration and not emitting any subrange DIEs. */
18321 if (TREE_CODE (type) == ARRAY_TYPE
18322 && ! TYPE_DOMAIN (type))
18323 add_AT_flag (array_die, DW_AT_declaration, 1);
18324 else
18325 #endif
18326 if (TREE_CODE (type) == VECTOR_TYPE)
18328 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
18329 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18330 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18331 add_bound_info (subrange_die, DW_AT_upper_bound,
18332 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18334 else
18335 add_subscript_info (array_die, type, collapse_nested_arrays);
18337 /* Add representation of the type of the elements of this array type and
18338 emit the corresponding DIE if we haven't done it already. */
18339 element_type = TREE_TYPE (type);
18340 if (collapse_nested_arrays)
18341 while (TREE_CODE (element_type) == ARRAY_TYPE)
18343 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18344 break;
18345 element_type = TREE_TYPE (element_type);
18348 #ifndef MIPS_DEBUGGING_INFO
18349 gen_type_die (element_type, context_die);
18350 #endif
18352 add_type_attribute (array_die, element_type, 0, 0, context_die);
18354 if (get_AT (array_die, DW_AT_name))
18355 add_pubtype (type, array_die);
18358 static dw_loc_descr_ref
18359 descr_info_loc (tree val, tree base_decl)
18361 HOST_WIDE_INT size;
18362 dw_loc_descr_ref loc, loc2;
18363 enum dwarf_location_atom op;
18365 if (val == base_decl)
18366 return new_loc_descr (DW_OP_push_object_address, 0, 0);
18368 switch (TREE_CODE (val))
18370 CASE_CONVERT:
18371 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18372 case VAR_DECL:
18373 return loc_descriptor_from_tree (val, 0);
18374 case INTEGER_CST:
18375 if (host_integerp (val, 0))
18376 return int_loc_descriptor (tree_low_cst (val, 0));
18377 break;
18378 case INDIRECT_REF:
18379 size = int_size_in_bytes (TREE_TYPE (val));
18380 if (size < 0)
18381 break;
18382 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18383 if (!loc)
18384 break;
18385 if (size == DWARF2_ADDR_SIZE)
18386 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18387 else
18388 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18389 return loc;
18390 case POINTER_PLUS_EXPR:
18391 case PLUS_EXPR:
18392 if (host_integerp (TREE_OPERAND (val, 1), 1)
18393 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18394 < 16384)
18396 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18397 if (!loc)
18398 break;
18399 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18401 else
18403 op = DW_OP_plus;
18404 do_binop:
18405 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18406 if (!loc)
18407 break;
18408 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18409 if (!loc2)
18410 break;
18411 add_loc_descr (&loc, loc2);
18412 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18414 return loc;
18415 case MINUS_EXPR:
18416 op = DW_OP_minus;
18417 goto do_binop;
18418 case MULT_EXPR:
18419 op = DW_OP_mul;
18420 goto do_binop;
18421 case EQ_EXPR:
18422 op = DW_OP_eq;
18423 goto do_binop;
18424 case NE_EXPR:
18425 op = DW_OP_ne;
18426 goto do_binop;
18427 default:
18428 break;
18430 return NULL;
18433 static void
18434 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18435 tree val, tree base_decl)
18437 dw_loc_descr_ref loc;
18439 if (host_integerp (val, 0))
18441 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18442 return;
18445 loc = descr_info_loc (val, base_decl);
18446 if (!loc)
18447 return;
18449 add_AT_loc (die, attr, loc);
18452 /* This routine generates DIE for array with hidden descriptor, details
18453 are filled into *info by a langhook. */
18455 static void
18456 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18457 dw_die_ref context_die)
18459 dw_die_ref scope_die = scope_die_for (type, context_die);
18460 dw_die_ref array_die;
18461 int dim;
18463 array_die = new_die (DW_TAG_array_type, scope_die, type);
18464 add_name_attribute (array_die, type_tag (type));
18465 equate_type_number_to_die (type, array_die);
18467 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18468 if (is_fortran ()
18469 && info->ndimensions >= 2)
18470 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18472 if (info->data_location)
18473 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18474 info->base_decl);
18475 if (info->associated)
18476 add_descr_info_field (array_die, DW_AT_associated, info->associated,
18477 info->base_decl);
18478 if (info->allocated)
18479 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18480 info->base_decl);
18482 for (dim = 0; dim < info->ndimensions; dim++)
18484 dw_die_ref subrange_die
18485 = new_die (DW_TAG_subrange_type, array_die, NULL);
18487 if (info->dimen[dim].lower_bound)
18489 /* If it is the default value, omit it. */
18490 int dflt;
18492 if (host_integerp (info->dimen[dim].lower_bound, 0)
18493 && (dflt = lower_bound_default ()) != -1
18494 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18496 else
18497 add_descr_info_field (subrange_die, DW_AT_lower_bound,
18498 info->dimen[dim].lower_bound,
18499 info->base_decl);
18501 if (info->dimen[dim].upper_bound)
18502 add_descr_info_field (subrange_die, DW_AT_upper_bound,
18503 info->dimen[dim].upper_bound,
18504 info->base_decl);
18505 if (info->dimen[dim].stride)
18506 add_descr_info_field (subrange_die, DW_AT_byte_stride,
18507 info->dimen[dim].stride,
18508 info->base_decl);
18511 gen_type_die (info->element_type, context_die);
18512 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18514 if (get_AT (array_die, DW_AT_name))
18515 add_pubtype (type, array_die);
18518 #if 0
18519 static void
18520 gen_entry_point_die (tree decl, dw_die_ref context_die)
18522 tree origin = decl_ultimate_origin (decl);
18523 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18525 if (origin != NULL)
18526 add_abstract_origin_attribute (decl_die, origin);
18527 else
18529 add_name_and_src_coords_attributes (decl_die, decl);
18530 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18531 0, 0, context_die);
18534 if (DECL_ABSTRACT (decl))
18535 equate_decl_number_to_die (decl, decl_die);
18536 else
18537 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18539 #endif
18541 /* Walk through the list of incomplete types again, trying once more to
18542 emit full debugging info for them. */
18544 static void
18545 retry_incomplete_types (void)
18547 int i;
18549 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18550 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18551 DINFO_USAGE_DIR_USE))
18552 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18555 /* Determine what tag to use for a record type. */
18557 static enum dwarf_tag
18558 record_type_tag (tree type)
18560 if (! lang_hooks.types.classify_record)
18561 return DW_TAG_structure_type;
18563 switch (lang_hooks.types.classify_record (type))
18565 case RECORD_IS_STRUCT:
18566 return DW_TAG_structure_type;
18568 case RECORD_IS_CLASS:
18569 return DW_TAG_class_type;
18571 case RECORD_IS_INTERFACE:
18572 if (dwarf_version >= 3 || !dwarf_strict)
18573 return DW_TAG_interface_type;
18574 return DW_TAG_structure_type;
18576 default:
18577 gcc_unreachable ();
18581 /* Generate a DIE to represent an enumeration type. Note that these DIEs
18582 include all of the information about the enumeration values also. Each
18583 enumerated type name/value is listed as a child of the enumerated type
18584 DIE. */
18586 static dw_die_ref
18587 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18589 dw_die_ref type_die = lookup_type_die (type);
18591 if (type_die == NULL)
18593 type_die = new_die (DW_TAG_enumeration_type,
18594 scope_die_for (type, context_die), type);
18595 equate_type_number_to_die (type, type_die);
18596 add_name_attribute (type_die, type_tag (type));
18597 if (dwarf_version >= 4 || !dwarf_strict)
18599 if (ENUM_IS_SCOPED (type))
18600 add_AT_flag (type_die, DW_AT_enum_class, 1);
18601 if (ENUM_IS_OPAQUE (type))
18602 add_AT_flag (type_die, DW_AT_declaration, 1);
18605 else if (! TYPE_SIZE (type))
18606 return type_die;
18607 else
18608 remove_AT (type_die, DW_AT_declaration);
18610 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
18611 given enum type is incomplete, do not generate the DW_AT_byte_size
18612 attribute or the DW_AT_element_list attribute. */
18613 if (TYPE_SIZE (type))
18615 tree link;
18617 TREE_ASM_WRITTEN (type) = 1;
18618 add_byte_size_attribute (type_die, type);
18619 if (TYPE_STUB_DECL (type) != NULL_TREE)
18621 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18622 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18625 /* If the first reference to this type was as the return type of an
18626 inline function, then it may not have a parent. Fix this now. */
18627 if (type_die->die_parent == NULL)
18628 add_child_die (scope_die_for (type, context_die), type_die);
18630 for (link = TYPE_VALUES (type);
18631 link != NULL; link = TREE_CHAIN (link))
18633 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18634 tree value = TREE_VALUE (link);
18636 add_name_attribute (enum_die,
18637 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18639 if (TREE_CODE (value) == CONST_DECL)
18640 value = DECL_INITIAL (value);
18642 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18643 /* DWARF2 does not provide a way of indicating whether or
18644 not enumeration constants are signed or unsigned. GDB
18645 always assumes the values are signed, so we output all
18646 values as if they were signed. That means that
18647 enumeration constants with very large unsigned values
18648 will appear to have negative values in the debugger. */
18649 add_AT_int (enum_die, DW_AT_const_value,
18650 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18653 else
18654 add_AT_flag (type_die, DW_AT_declaration, 1);
18656 if (get_AT (type_die, DW_AT_name))
18657 add_pubtype (type, type_die);
18659 return type_die;
18662 /* Generate a DIE to represent either a real live formal parameter decl or to
18663 represent just the type of some formal parameter position in some function
18664 type.
18666 Note that this routine is a bit unusual because its argument may be a
18667 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18668 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18669 node. If it's the former then this function is being called to output a
18670 DIE to represent a formal parameter object (or some inlining thereof). If
18671 it's the latter, then this function is only being called to output a
18672 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18673 argument type of some subprogram type.
18674 If EMIT_NAME_P is true, name and source coordinate attributes
18675 are emitted. */
18677 static dw_die_ref
18678 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18679 dw_die_ref context_die)
18681 tree node_or_origin = node ? node : origin;
18682 tree ultimate_origin;
18683 dw_die_ref parm_die
18684 = new_die (DW_TAG_formal_parameter, context_die, node);
18686 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18688 case tcc_declaration:
18689 ultimate_origin = decl_ultimate_origin (node_or_origin);
18690 if (node || ultimate_origin)
18691 origin = ultimate_origin;
18692 if (origin != NULL)
18693 add_abstract_origin_attribute (parm_die, origin);
18694 else if (emit_name_p)
18695 add_name_and_src_coords_attributes (parm_die, node);
18696 if (origin == NULL
18697 || (! DECL_ABSTRACT (node_or_origin)
18698 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18699 decl_function_context
18700 (node_or_origin))))
18702 tree type = TREE_TYPE (node_or_origin);
18703 if (decl_by_reference_p (node_or_origin))
18704 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18705 context_die);
18706 else
18707 add_type_attribute (parm_die, type,
18708 TREE_READONLY (node_or_origin),
18709 TREE_THIS_VOLATILE (node_or_origin),
18710 context_die);
18712 if (origin == NULL && DECL_ARTIFICIAL (node))
18713 add_AT_flag (parm_die, DW_AT_artificial, 1);
18715 if (node && node != origin)
18716 equate_decl_number_to_die (node, parm_die);
18717 if (! DECL_ABSTRACT (node_or_origin))
18718 add_location_or_const_value_attribute (parm_die, node_or_origin,
18719 node == NULL, DW_AT_location);
18721 break;
18723 case tcc_type:
18724 /* We were called with some kind of a ..._TYPE node. */
18725 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18726 break;
18728 default:
18729 gcc_unreachable ();
18732 return parm_die;
18735 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18736 children DW_TAG_formal_parameter DIEs representing the arguments of the
18737 parameter pack.
18739 PARM_PACK must be a function parameter pack.
18740 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18741 must point to the subsequent arguments of the function PACK_ARG belongs to.
18742 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18743 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18744 following the last one for which a DIE was generated. */
18746 static dw_die_ref
18747 gen_formal_parameter_pack_die (tree parm_pack,
18748 tree pack_arg,
18749 dw_die_ref subr_die,
18750 tree *next_arg)
18752 tree arg;
18753 dw_die_ref parm_pack_die;
18755 gcc_assert (parm_pack
18756 && lang_hooks.function_parameter_pack_p (parm_pack)
18757 && subr_die);
18759 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18760 add_src_coords_attributes (parm_pack_die, parm_pack);
18762 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18764 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18765 parm_pack))
18766 break;
18767 gen_formal_parameter_die (arg, NULL,
18768 false /* Don't emit name attribute. */,
18769 parm_pack_die);
18771 if (next_arg)
18772 *next_arg = arg;
18773 return parm_pack_die;
18776 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18777 at the end of an (ANSI prototyped) formal parameters list. */
18779 static void
18780 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18782 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18785 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18786 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18787 parameters as specified in some function type specification (except for
18788 those which appear as part of a function *definition*). */
18790 static void
18791 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18793 tree link;
18794 tree formal_type = NULL;
18795 tree first_parm_type;
18796 tree arg;
18798 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18800 arg = DECL_ARGUMENTS (function_or_method_type);
18801 function_or_method_type = TREE_TYPE (function_or_method_type);
18803 else
18804 arg = NULL_TREE;
18806 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18808 /* Make our first pass over the list of formal parameter types and output a
18809 DW_TAG_formal_parameter DIE for each one. */
18810 for (link = first_parm_type; link; )
18812 dw_die_ref parm_die;
18814 formal_type = TREE_VALUE (link);
18815 if (formal_type == void_type_node)
18816 break;
18818 /* Output a (nameless) DIE to represent the formal parameter itself. */
18819 parm_die = gen_formal_parameter_die (formal_type, NULL,
18820 true /* Emit name attribute. */,
18821 context_die);
18822 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18823 && link == first_parm_type)
18825 add_AT_flag (parm_die, DW_AT_artificial, 1);
18826 if (dwarf_version >= 3 || !dwarf_strict)
18827 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18829 else if (arg && DECL_ARTIFICIAL (arg))
18830 add_AT_flag (parm_die, DW_AT_artificial, 1);
18832 link = TREE_CHAIN (link);
18833 if (arg)
18834 arg = DECL_CHAIN (arg);
18837 /* If this function type has an ellipsis, add a
18838 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18839 if (formal_type != void_type_node)
18840 gen_unspecified_parameters_die (function_or_method_type, context_die);
18842 /* Make our second (and final) pass over the list of formal parameter types
18843 and output DIEs to represent those types (as necessary). */
18844 for (link = TYPE_ARG_TYPES (function_or_method_type);
18845 link && TREE_VALUE (link);
18846 link = TREE_CHAIN (link))
18847 gen_type_die (TREE_VALUE (link), context_die);
18850 /* We want to generate the DIE for TYPE so that we can generate the
18851 die for MEMBER, which has been defined; we will need to refer back
18852 to the member declaration nested within TYPE. If we're trying to
18853 generate minimal debug info for TYPE, processing TYPE won't do the
18854 trick; we need to attach the member declaration by hand. */
18856 static void
18857 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18859 gen_type_die (type, context_die);
18861 /* If we're trying to avoid duplicate debug info, we may not have
18862 emitted the member decl for this function. Emit it now. */
18863 if (TYPE_STUB_DECL (type)
18864 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18865 && ! lookup_decl_die (member))
18867 dw_die_ref type_die;
18868 gcc_assert (!decl_ultimate_origin (member));
18870 push_decl_scope (type);
18871 type_die = lookup_type_die_strip_naming_typedef (type);
18872 if (TREE_CODE (member) == FUNCTION_DECL)
18873 gen_subprogram_die (member, type_die);
18874 else if (TREE_CODE (member) == FIELD_DECL)
18876 /* Ignore the nameless fields that are used to skip bits but handle
18877 C++ anonymous unions and structs. */
18878 if (DECL_NAME (member) != NULL_TREE
18879 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18880 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18882 gen_type_die (member_declared_type (member), type_die);
18883 gen_field_die (member, type_die);
18886 else
18887 gen_variable_die (member, NULL_TREE, type_die);
18889 pop_decl_scope ();
18893 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18894 may later generate inlined and/or out-of-line instances of. */
18896 static void
18897 dwarf2out_abstract_function (tree decl)
18899 dw_die_ref old_die;
18900 tree save_fn;
18901 tree context;
18902 int was_abstract;
18903 htab_t old_decl_loc_table;
18904 htab_t old_cached_dw_loc_list_table;
18905 int old_call_site_count, old_tail_call_site_count;
18906 struct call_arg_loc_node *old_call_arg_locations;
18908 /* Make sure we have the actual abstract inline, not a clone. */
18909 decl = DECL_ORIGIN (decl);
18911 old_die = lookup_decl_die (decl);
18912 if (old_die && get_AT (old_die, DW_AT_inline))
18913 /* We've already generated the abstract instance. */
18914 return;
18916 /* We can be called while recursively when seeing block defining inlined subroutine
18917 DIE. Be sure to not clobber the outer location table nor use it or we would
18918 get locations in abstract instantces. */
18919 old_decl_loc_table = decl_loc_table;
18920 decl_loc_table = NULL;
18921 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18922 cached_dw_loc_list_table = NULL;
18923 old_call_arg_locations = call_arg_locations;
18924 call_arg_locations = NULL;
18925 old_call_site_count = call_site_count;
18926 call_site_count = -1;
18927 old_tail_call_site_count = tail_call_site_count;
18928 tail_call_site_count = -1;
18930 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18931 we don't get confused by DECL_ABSTRACT. */
18932 if (debug_info_level > DINFO_LEVEL_TERSE)
18934 context = decl_class_context (decl);
18935 if (context)
18936 gen_type_die_for_member
18937 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18940 /* Pretend we've just finished compiling this function. */
18941 save_fn = current_function_decl;
18942 current_function_decl = decl;
18943 push_cfun (DECL_STRUCT_FUNCTION (decl));
18945 was_abstract = DECL_ABSTRACT (decl);
18946 set_decl_abstract_flags (decl, 1);
18947 dwarf2out_decl (decl);
18948 if (! was_abstract)
18949 set_decl_abstract_flags (decl, 0);
18951 current_function_decl = save_fn;
18952 decl_loc_table = old_decl_loc_table;
18953 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18954 call_arg_locations = old_call_arg_locations;
18955 call_site_count = old_call_site_count;
18956 tail_call_site_count = old_tail_call_site_count;
18957 pop_cfun ();
18960 /* Helper function of premark_used_types() which gets called through
18961 htab_traverse.
18963 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18964 marked as unused by prune_unused_types. */
18966 static int
18967 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18969 tree type;
18970 dw_die_ref die;
18972 type = (tree) *slot;
18973 die = lookup_type_die (type);
18974 if (die != NULL)
18975 die->die_perennial_p = 1;
18976 return 1;
18979 /* Helper function of premark_types_used_by_global_vars which gets called
18980 through htab_traverse.
18982 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18983 marked as unused by prune_unused_types. The DIE of the type is marked
18984 only if the global variable using the type will actually be emitted. */
18986 static int
18987 premark_types_used_by_global_vars_helper (void **slot,
18988 void *data ATTRIBUTE_UNUSED)
18990 struct types_used_by_vars_entry *entry;
18991 dw_die_ref die;
18993 entry = (struct types_used_by_vars_entry *) *slot;
18994 gcc_assert (entry->type != NULL
18995 && entry->var_decl != NULL);
18996 die = lookup_type_die (entry->type);
18997 if (die)
18999 /* Ask cgraph if the global variable really is to be emitted.
19000 If yes, then we'll keep the DIE of ENTRY->TYPE. */
19001 struct varpool_node *node = varpool_get_node (entry->var_decl);
19002 if (node && node->needed)
19004 die->die_perennial_p = 1;
19005 /* Keep the parent DIEs as well. */
19006 while ((die = die->die_parent) && die->die_perennial_p == 0)
19007 die->die_perennial_p = 1;
19010 return 1;
19013 /* Mark all members of used_types_hash as perennial. */
19015 static void
19016 premark_used_types (void)
19018 if (cfun && cfun->used_types_hash)
19019 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
19022 /* Mark all members of types_used_by_vars_entry as perennial. */
19024 static void
19025 premark_types_used_by_global_vars (void)
19027 if (types_used_by_vars_hash)
19028 htab_traverse (types_used_by_vars_hash,
19029 premark_types_used_by_global_vars_helper, NULL);
19032 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
19033 for CA_LOC call arg loc node. */
19035 static dw_die_ref
19036 gen_call_site_die (tree decl, dw_die_ref subr_die,
19037 struct call_arg_loc_node *ca_loc)
19039 dw_die_ref stmt_die = NULL, die;
19040 tree block = ca_loc->block;
19042 while (block
19043 && block != DECL_INITIAL (decl)
19044 && TREE_CODE (block) == BLOCK)
19046 if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
19047 stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
19048 if (stmt_die)
19049 break;
19050 block = BLOCK_SUPERCONTEXT (block);
19052 if (stmt_die == NULL)
19053 stmt_die = subr_die;
19054 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
19055 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
19056 if (ca_loc->tail_call_p)
19057 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19058 if (ca_loc->symbol_ref)
19060 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19061 if (tdie)
19062 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19063 else
19064 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
19066 return die;
19069 /* Generate a DIE to represent a declared function (either file-scope or
19070 block-local). */
19072 static void
19073 gen_subprogram_die (tree decl, dw_die_ref context_die)
19075 tree origin = decl_ultimate_origin (decl);
19076 dw_die_ref subr_die;
19077 tree outer_scope;
19078 dw_die_ref old_die = lookup_decl_die (decl);
19079 int declaration = (current_function_decl != decl
19080 || class_or_namespace_scope_p (context_die));
19082 premark_used_types ();
19084 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19085 started to generate the abstract instance of an inline, decided to output
19086 its containing class, and proceeded to emit the declaration of the inline
19087 from the member list for the class. If so, DECLARATION takes priority;
19088 we'll get back to the abstract instance when done with the class. */
19090 /* The class-scope declaration DIE must be the primary DIE. */
19091 if (origin && declaration && class_or_namespace_scope_p (context_die))
19093 origin = NULL;
19094 gcc_assert (!old_die);
19097 /* Now that the C++ front end lazily declares artificial member fns, we
19098 might need to retrofit the declaration into its class. */
19099 if (!declaration && !origin && !old_die
19100 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19101 && !class_or_namespace_scope_p (context_die)
19102 && debug_info_level > DINFO_LEVEL_TERSE)
19103 old_die = force_decl_die (decl);
19105 if (origin != NULL)
19107 gcc_assert (!declaration || local_scope_p (context_die));
19109 /* Fixup die_parent for the abstract instance of a nested
19110 inline function. */
19111 if (old_die && old_die->die_parent == NULL)
19112 add_child_die (context_die, old_die);
19114 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19115 add_abstract_origin_attribute (subr_die, origin);
19117 else if (old_die)
19119 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19120 struct dwarf_file_data * file_index = lookup_filename (s.file);
19122 if (!get_AT_flag (old_die, DW_AT_declaration)
19123 /* We can have a normal definition following an inline one in the
19124 case of redefinition of GNU C extern inlines.
19125 It seems reasonable to use AT_specification in this case. */
19126 && !get_AT (old_die, DW_AT_inline))
19128 /* Detect and ignore this case, where we are trying to output
19129 something we have already output. */
19130 return;
19133 /* If the definition comes from the same place as the declaration,
19134 maybe use the old DIE. We always want the DIE for this function
19135 that has the *_pc attributes to be under comp_unit_die so the
19136 debugger can find it. We also need to do this for abstract
19137 instances of inlines, since the spec requires the out-of-line copy
19138 to have the same parent. For local class methods, this doesn't
19139 apply; we just use the old DIE. */
19140 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19141 && (DECL_ARTIFICIAL (decl)
19142 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19143 && (get_AT_unsigned (old_die, DW_AT_decl_line)
19144 == (unsigned) s.line))))
19146 subr_die = old_die;
19148 /* Clear out the declaration attribute and the formal parameters.
19149 Do not remove all children, because it is possible that this
19150 declaration die was forced using force_decl_die(). In such
19151 cases die that forced declaration die (e.g. TAG_imported_module)
19152 is one of the children that we do not want to remove. */
19153 remove_AT (subr_die, DW_AT_declaration);
19154 remove_AT (subr_die, DW_AT_object_pointer);
19155 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19157 else
19159 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19160 add_AT_specification (subr_die, old_die);
19161 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19162 add_AT_file (subr_die, DW_AT_decl_file, file_index);
19163 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19164 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19167 else
19169 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19171 if (TREE_PUBLIC (decl))
19172 add_AT_flag (subr_die, DW_AT_external, 1);
19174 add_name_and_src_coords_attributes (subr_die, decl);
19175 if (debug_info_level > DINFO_LEVEL_TERSE)
19177 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19178 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19179 0, 0, context_die);
19182 add_pure_or_virtual_attribute (subr_die, decl);
19183 if (DECL_ARTIFICIAL (decl))
19184 add_AT_flag (subr_die, DW_AT_artificial, 1);
19186 add_accessibility_attribute (subr_die, decl);
19189 if (declaration)
19191 if (!old_die || !get_AT (old_die, DW_AT_inline))
19193 add_AT_flag (subr_die, DW_AT_declaration, 1);
19195 /* If this is an explicit function declaration then generate
19196 a DW_AT_explicit attribute. */
19197 if (lang_hooks.decls.function_decl_explicit_p (decl)
19198 && (dwarf_version >= 3 || !dwarf_strict))
19199 add_AT_flag (subr_die, DW_AT_explicit, 1);
19201 /* The first time we see a member function, it is in the context of
19202 the class to which it belongs. We make sure of this by emitting
19203 the class first. The next time is the definition, which is
19204 handled above. The two may come from the same source text.
19206 Note that force_decl_die() forces function declaration die. It is
19207 later reused to represent definition. */
19208 equate_decl_number_to_die (decl, subr_die);
19211 else if (DECL_ABSTRACT (decl))
19213 if (DECL_DECLARED_INLINE_P (decl))
19215 if (cgraph_function_possibly_inlined_p (decl))
19216 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19217 else
19218 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19220 else
19222 if (cgraph_function_possibly_inlined_p (decl))
19223 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19224 else
19225 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19228 if (DECL_DECLARED_INLINE_P (decl)
19229 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19230 add_AT_flag (subr_die, DW_AT_artificial, 1);
19232 equate_decl_number_to_die (decl, subr_die);
19234 else if (!DECL_EXTERNAL (decl))
19236 HOST_WIDE_INT cfa_fb_offset;
19238 if (!old_die || !get_AT (old_die, DW_AT_inline))
19239 equate_decl_number_to_die (decl, subr_die);
19241 if (!flag_reorder_blocks_and_partition)
19243 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19244 if (fde->dw_fde_begin)
19246 /* We have already generated the labels. */
19247 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19248 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19250 else
19252 /* Create start/end labels and add the range. */
19253 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19254 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19255 current_function_funcdef_no);
19256 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19257 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19258 current_function_funcdef_no);
19259 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19262 #if VMS_DEBUGGING_INFO
19263 /* HP OpenVMS Industry Standard 64: DWARF Extensions
19264 Section 2.3 Prologue and Epilogue Attributes:
19265 When a breakpoint is set on entry to a function, it is generally
19266 desirable for execution to be suspended, not on the very first
19267 instruction of the function, but rather at a point after the
19268 function's frame has been set up, after any language defined local
19269 declaration processing has been completed, and before execution of
19270 the first statement of the function begins. Debuggers generally
19271 cannot properly determine where this point is. Similarly for a
19272 breakpoint set on exit from a function. The prologue and epilogue
19273 attributes allow a compiler to communicate the location(s) to use. */
19276 if (fde->dw_fde_vms_end_prologue)
19277 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19278 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19280 if (fde->dw_fde_vms_begin_epilogue)
19281 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19282 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19284 #endif
19286 add_pubname (decl, subr_die);
19288 else
19289 { /* Generate pubnames entries for the split function code
19290 ranges. */
19291 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19293 if (fde->dw_fde_second_begin)
19295 if (dwarf_version >= 3 || !dwarf_strict)
19297 /* We should use ranges for non-contiguous code section
19298 addresses. Use the actual code range for the initial
19299 section, since the HOT/COLD labels might precede an
19300 alignment offset. */
19301 bool range_list_added = false;
19302 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19303 fde->dw_fde_end, &range_list_added);
19304 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19305 fde->dw_fde_second_end,
19306 &range_list_added);
19307 add_pubname (decl, subr_die);
19308 if (range_list_added)
19309 add_ranges (NULL);
19311 else
19313 /* There is no real support in DW2 for this .. so we make
19314 a work-around. First, emit the pub name for the segment
19315 containing the function label. Then make and emit a
19316 simplified subprogram DIE for the second segment with the
19317 name pre-fixed by __hot/cold_sect_of_. We use the same
19318 linkage name for the second die so that gdb will find both
19319 sections when given "b foo". */
19320 const char *name = NULL;
19321 tree decl_name = DECL_NAME (decl);
19322 dw_die_ref seg_die;
19324 /* Do the 'primary' section. */
19325 add_AT_lbl_id (subr_die, DW_AT_low_pc,
19326 fde->dw_fde_begin);
19327 add_AT_lbl_id (subr_die, DW_AT_high_pc,
19328 fde->dw_fde_end);
19329 /* Add it. */
19330 add_pubname (decl, subr_die);
19332 /* Build a minimal DIE for the secondary section. */
19333 seg_die = new_die (DW_TAG_subprogram,
19334 subr_die->die_parent, decl);
19336 if (TREE_PUBLIC (decl))
19337 add_AT_flag (seg_die, DW_AT_external, 1);
19339 if (decl_name != NULL
19340 && IDENTIFIER_POINTER (decl_name) != NULL)
19342 name = dwarf2_name (decl, 1);
19343 if (! DECL_ARTIFICIAL (decl))
19344 add_src_coords_attributes (seg_die, decl);
19346 add_linkage_name (seg_die, decl);
19348 gcc_assert (name != NULL);
19349 add_pure_or_virtual_attribute (seg_die, decl);
19350 if (DECL_ARTIFICIAL (decl))
19351 add_AT_flag (seg_die, DW_AT_artificial, 1);
19353 name = concat ("__second_sect_of_", name, NULL);
19354 add_AT_lbl_id (seg_die, DW_AT_low_pc,
19355 fde->dw_fde_second_begin);
19356 add_AT_lbl_id (seg_die, DW_AT_high_pc,
19357 fde->dw_fde_second_end);
19358 add_name_attribute (seg_die, name);
19359 add_pubname_string (name, seg_die);
19362 else
19364 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19365 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19366 add_pubname (decl, subr_die);
19370 #ifdef MIPS_DEBUGGING_INFO
19371 /* Add a reference to the FDE for this routine. */
19372 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19373 #endif
19375 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19377 /* We define the "frame base" as the function's CFA. This is more
19378 convenient for several reasons: (1) It's stable across the prologue
19379 and epilogue, which makes it better than just a frame pointer,
19380 (2) With dwarf3, there exists a one-byte encoding that allows us
19381 to reference the .debug_frame data by proxy, but failing that,
19382 (3) We can at least reuse the code inspection and interpretation
19383 code that determines the CFA position at various points in the
19384 function. */
19385 if (dwarf_version >= 3)
19387 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19388 add_AT_loc (subr_die, DW_AT_frame_base, op);
19390 else
19392 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19393 if (list->dw_loc_next)
19394 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19395 else
19396 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19399 /* Compute a displacement from the "steady-state frame pointer" to
19400 the CFA. The former is what all stack slots and argument slots
19401 will reference in the rtl; the later is what we've told the
19402 debugger about. We'll need to adjust all frame_base references
19403 by this displacement. */
19404 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19406 if (cfun->static_chain_decl)
19407 add_AT_location_description (subr_die, DW_AT_static_link,
19408 loc_list_from_tree (cfun->static_chain_decl, 2));
19411 /* Generate child dies for template paramaters. */
19412 if (debug_info_level > DINFO_LEVEL_TERSE)
19413 gen_generic_params_dies (decl);
19415 /* Now output descriptions of the arguments for this function. This gets
19416 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19417 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19418 `...' at the end of the formal parameter list. In order to find out if
19419 there was a trailing ellipsis or not, we must instead look at the type
19420 associated with the FUNCTION_DECL. This will be a node of type
19421 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19422 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19423 an ellipsis at the end. */
19425 /* In the case where we are describing a mere function declaration, all we
19426 need to do here (and all we *can* do here) is to describe the *types* of
19427 its formal parameters. */
19428 if (debug_info_level <= DINFO_LEVEL_TERSE)
19430 else if (declaration)
19431 gen_formal_types_die (decl, subr_die);
19432 else
19434 /* Generate DIEs to represent all known formal parameters. */
19435 tree parm = DECL_ARGUMENTS (decl);
19436 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19437 tree generic_decl_parm = generic_decl
19438 ? DECL_ARGUMENTS (generic_decl)
19439 : NULL;
19441 /* Now we want to walk the list of parameters of the function and
19442 emit their relevant DIEs.
19444 We consider the case of DECL being an instance of a generic function
19445 as well as it being a normal function.
19447 If DECL is an instance of a generic function we walk the
19448 parameters of the generic function declaration _and_ the parameters of
19449 DECL itself. This is useful because we want to emit specific DIEs for
19450 function parameter packs and those are declared as part of the
19451 generic function declaration. In that particular case,
19452 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19453 That DIE has children DIEs representing the set of arguments
19454 of the pack. Note that the set of pack arguments can be empty.
19455 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19456 children DIE.
19458 Otherwise, we just consider the parameters of DECL. */
19459 while (generic_decl_parm || parm)
19461 if (generic_decl_parm
19462 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19463 gen_formal_parameter_pack_die (generic_decl_parm,
19464 parm, subr_die,
19465 &parm);
19466 else if (parm)
19468 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19470 if (parm == DECL_ARGUMENTS (decl)
19471 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19472 && parm_die
19473 && (dwarf_version >= 3 || !dwarf_strict))
19474 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19476 parm = DECL_CHAIN (parm);
19479 if (generic_decl_parm)
19480 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19483 /* Decide whether we need an unspecified_parameters DIE at the end.
19484 There are 2 more cases to do this for: 1) the ansi ... declaration -
19485 this is detectable when the end of the arg list is not a
19486 void_type_node 2) an unprototyped function declaration (not a
19487 definition). This just means that we have no info about the
19488 parameters at all. */
19489 if (prototype_p (TREE_TYPE (decl)))
19491 /* This is the prototyped case, check for.... */
19492 if (stdarg_p (TREE_TYPE (decl)))
19493 gen_unspecified_parameters_die (decl, subr_die);
19495 else if (DECL_INITIAL (decl) == NULL_TREE)
19496 gen_unspecified_parameters_die (decl, subr_die);
19499 /* Output Dwarf info for all of the stuff within the body of the function
19500 (if it has one - it may be just a declaration). */
19501 outer_scope = DECL_INITIAL (decl);
19503 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19504 a function. This BLOCK actually represents the outermost binding contour
19505 for the function, i.e. the contour in which the function's formal
19506 parameters and labels get declared. Curiously, it appears that the front
19507 end doesn't actually put the PARM_DECL nodes for the current function onto
19508 the BLOCK_VARS list for this outer scope, but are strung off of the
19509 DECL_ARGUMENTS list for the function instead.
19511 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19512 the LABEL_DECL nodes for the function however, and we output DWARF info
19513 for those in decls_for_scope. Just within the `outer_scope' there will be
19514 a BLOCK node representing the function's outermost pair of curly braces,
19515 and any blocks used for the base and member initializers of a C++
19516 constructor function. */
19517 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19519 int call_site_note_count = 0;
19520 int tail_call_site_note_count = 0;
19522 /* Emit a DW_TAG_variable DIE for a named return value. */
19523 if (DECL_NAME (DECL_RESULT (decl)))
19524 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19526 current_function_has_inlines = 0;
19527 decls_for_scope (outer_scope, subr_die, 0);
19529 if (call_arg_locations && !dwarf_strict)
19531 struct call_arg_loc_node *ca_loc;
19532 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19534 dw_die_ref die = NULL;
19535 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19536 rtx arg, next_arg;
19538 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19539 arg; arg = next_arg)
19541 dw_loc_descr_ref reg, val;
19542 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19543 dw_die_ref cdie;
19545 next_arg = XEXP (arg, 1);
19546 if (REG_P (XEXP (XEXP (arg, 0), 0))
19547 && next_arg
19548 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19549 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19550 && REGNO (XEXP (XEXP (arg, 0), 0))
19551 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19552 next_arg = XEXP (next_arg, 1);
19553 if (mode == VOIDmode)
19555 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19556 if (mode == VOIDmode)
19557 mode = GET_MODE (XEXP (arg, 0));
19559 if (GET_MODE_CLASS (mode) != MODE_INT
19560 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19561 continue;
19562 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19564 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19565 tloc = XEXP (XEXP (arg, 0), 1);
19566 continue;
19568 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19569 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19571 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19572 tlocc = XEXP (XEXP (arg, 0), 1);
19573 continue;
19575 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19576 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19577 VAR_INIT_STATUS_INITIALIZED);
19578 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19579 reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19580 0), 0), mode,
19581 VAR_INIT_STATUS_INITIALIZED);
19582 else
19583 continue;
19584 if (reg == NULL)
19585 continue;
19586 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19587 VAR_INIT_STATUS_INITIALIZED);
19588 if (val == NULL)
19589 continue;
19590 if (die == NULL)
19591 die = gen_call_site_die (decl, subr_die, ca_loc);
19592 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19593 NULL_TREE);
19594 add_AT_loc (cdie, DW_AT_location, reg);
19595 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19596 if (next_arg != XEXP (arg, 1))
19598 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19599 0), 1), VOIDmode,
19600 VAR_INIT_STATUS_INITIALIZED);
19601 if (val != NULL)
19602 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19605 if (die == NULL
19606 && (ca_loc->symbol_ref || tloc))
19607 die = gen_call_site_die (decl, subr_die, ca_loc);
19608 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19610 dw_loc_descr_ref tval = NULL;
19612 if (tloc != NULL_RTX)
19613 tval = mem_loc_descriptor (tloc, VOIDmode,
19614 VAR_INIT_STATUS_INITIALIZED);
19615 if (tval)
19616 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19617 else if (tlocc != NULL_RTX)
19619 tval = mem_loc_descriptor (tlocc, VOIDmode,
19620 VAR_INIT_STATUS_INITIALIZED);
19621 if (tval)
19622 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19623 tval);
19626 if (die != NULL)
19628 call_site_note_count++;
19629 if (ca_loc->tail_call_p)
19630 tail_call_site_note_count++;
19634 call_arg_locations = NULL;
19635 call_arg_loc_last = NULL;
19636 if (tail_call_site_count >= 0
19637 && tail_call_site_count == tail_call_site_note_count
19638 && !dwarf_strict)
19640 if (call_site_count >= 0
19641 && call_site_count == call_site_note_count)
19642 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19643 else
19644 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19646 call_site_count = -1;
19647 tail_call_site_count = -1;
19649 /* Add the calling convention attribute if requested. */
19650 add_calling_convention_attribute (subr_die, decl);
19654 /* Returns a hash value for X (which really is a die_struct). */
19656 static hashval_t
19657 common_block_die_table_hash (const void *x)
19659 const_dw_die_ref d = (const_dw_die_ref) x;
19660 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19663 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19664 as decl_id and die_parent of die_struct Y. */
19666 static int
19667 common_block_die_table_eq (const void *x, const void *y)
19669 const_dw_die_ref d = (const_dw_die_ref) x;
19670 const_dw_die_ref e = (const_dw_die_ref) y;
19671 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19674 /* Generate a DIE to represent a declared data object.
19675 Either DECL or ORIGIN must be non-null. */
19677 static void
19678 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19680 HOST_WIDE_INT off;
19681 tree com_decl;
19682 tree decl_or_origin = decl ? decl : origin;
19683 tree ultimate_origin;
19684 dw_die_ref var_die;
19685 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19686 dw_die_ref origin_die;
19687 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19688 || class_or_namespace_scope_p (context_die));
19689 bool specialization_p = false;
19691 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19692 if (decl || ultimate_origin)
19693 origin = ultimate_origin;
19694 com_decl = fortran_common (decl_or_origin, &off);
19696 /* Symbol in common gets emitted as a child of the common block, in the form
19697 of a data member. */
19698 if (com_decl)
19700 dw_die_ref com_die;
19701 dw_loc_list_ref loc;
19702 die_node com_die_arg;
19704 var_die = lookup_decl_die (decl_or_origin);
19705 if (var_die)
19707 if (get_AT (var_die, DW_AT_location) == NULL)
19709 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19710 if (loc)
19712 if (off)
19714 /* Optimize the common case. */
19715 if (single_element_loc_list_p (loc)
19716 && loc->expr->dw_loc_opc == DW_OP_addr
19717 && loc->expr->dw_loc_next == NULL
19718 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19719 == SYMBOL_REF)
19720 loc->expr->dw_loc_oprnd1.v.val_addr
19721 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19722 else
19723 loc_list_plus_const (loc, off);
19725 add_AT_location_description (var_die, DW_AT_location, loc);
19726 remove_AT (var_die, DW_AT_declaration);
19729 return;
19732 if (common_block_die_table == NULL)
19733 common_block_die_table
19734 = htab_create_ggc (10, common_block_die_table_hash,
19735 common_block_die_table_eq, NULL);
19737 com_die_arg.decl_id = DECL_UID (com_decl);
19738 com_die_arg.die_parent = context_die;
19739 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19740 loc = loc_list_from_tree (com_decl, 2);
19741 if (com_die == NULL)
19743 const char *cnam
19744 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19745 void **slot;
19747 com_die = new_die (DW_TAG_common_block, context_die, decl);
19748 add_name_and_src_coords_attributes (com_die, com_decl);
19749 if (loc)
19751 add_AT_location_description (com_die, DW_AT_location, loc);
19752 /* Avoid sharing the same loc descriptor between
19753 DW_TAG_common_block and DW_TAG_variable. */
19754 loc = loc_list_from_tree (com_decl, 2);
19756 else if (DECL_EXTERNAL (decl))
19757 add_AT_flag (com_die, DW_AT_declaration, 1);
19758 add_pubname_string (cnam, com_die); /* ??? needed? */
19759 com_die->decl_id = DECL_UID (com_decl);
19760 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19761 *slot = (void *) com_die;
19763 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19765 add_AT_location_description (com_die, DW_AT_location, loc);
19766 loc = loc_list_from_tree (com_decl, 2);
19767 remove_AT (com_die, DW_AT_declaration);
19769 var_die = new_die (DW_TAG_variable, com_die, decl);
19770 add_name_and_src_coords_attributes (var_die, decl);
19771 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19772 TREE_THIS_VOLATILE (decl), context_die);
19773 add_AT_flag (var_die, DW_AT_external, 1);
19774 if (loc)
19776 if (off)
19778 /* Optimize the common case. */
19779 if (single_element_loc_list_p (loc)
19780 && loc->expr->dw_loc_opc == DW_OP_addr
19781 && loc->expr->dw_loc_next == NULL
19782 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19783 loc->expr->dw_loc_oprnd1.v.val_addr
19784 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19785 else
19786 loc_list_plus_const (loc, off);
19788 add_AT_location_description (var_die, DW_AT_location, loc);
19790 else if (DECL_EXTERNAL (decl))
19791 add_AT_flag (var_die, DW_AT_declaration, 1);
19792 equate_decl_number_to_die (decl, var_die);
19793 return;
19796 /* If the compiler emitted a definition for the DECL declaration
19797 and if we already emitted a DIE for it, don't emit a second
19798 DIE for it again. Allow re-declarations of DECLs that are
19799 inside functions, though. */
19800 if (old_die && declaration && !local_scope_p (context_die))
19801 return;
19803 /* For static data members, the declaration in the class is supposed
19804 to have DW_TAG_member tag; the specification should still be
19805 DW_TAG_variable referencing the DW_TAG_member DIE. */
19806 if (declaration && class_scope_p (context_die))
19807 var_die = new_die (DW_TAG_member, context_die, decl);
19808 else
19809 var_die = new_die (DW_TAG_variable, context_die, decl);
19811 origin_die = NULL;
19812 if (origin != NULL)
19813 origin_die = add_abstract_origin_attribute (var_die, origin);
19815 /* Loop unrolling can create multiple blocks that refer to the same
19816 static variable, so we must test for the DW_AT_declaration flag.
19818 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19819 copy decls and set the DECL_ABSTRACT flag on them instead of
19820 sharing them.
19822 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19824 ??? The declare_in_namespace support causes us to get two DIEs for one
19825 variable, both of which are declarations. We want to avoid considering
19826 one to be a specification, so we must test that this DIE is not a
19827 declaration. */
19828 else if (old_die && TREE_STATIC (decl) && ! declaration
19829 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19831 /* This is a definition of a C++ class level static. */
19832 add_AT_specification (var_die, old_die);
19833 specialization_p = true;
19834 if (DECL_NAME (decl))
19836 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19837 struct dwarf_file_data * file_index = lookup_filename (s.file);
19839 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19840 add_AT_file (var_die, DW_AT_decl_file, file_index);
19842 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19843 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19845 if (old_die->die_tag == DW_TAG_member)
19846 add_linkage_name (var_die, decl);
19849 else
19850 add_name_and_src_coords_attributes (var_die, decl);
19852 if ((origin == NULL && !specialization_p)
19853 || (origin != NULL
19854 && !DECL_ABSTRACT (decl_or_origin)
19855 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19856 decl_function_context
19857 (decl_or_origin))))
19859 tree type = TREE_TYPE (decl_or_origin);
19861 if (decl_by_reference_p (decl_or_origin))
19862 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19863 else
19864 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19865 TREE_THIS_VOLATILE (decl_or_origin), context_die);
19868 if (origin == NULL && !specialization_p)
19870 if (TREE_PUBLIC (decl))
19871 add_AT_flag (var_die, DW_AT_external, 1);
19873 if (DECL_ARTIFICIAL (decl))
19874 add_AT_flag (var_die, DW_AT_artificial, 1);
19876 add_accessibility_attribute (var_die, decl);
19879 if (declaration)
19880 add_AT_flag (var_die, DW_AT_declaration, 1);
19882 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19883 equate_decl_number_to_die (decl, var_die);
19885 if (! declaration
19886 && (! DECL_ABSTRACT (decl_or_origin)
19887 /* Local static vars are shared between all clones/inlines,
19888 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19889 already set. */
19890 || (TREE_CODE (decl_or_origin) == VAR_DECL
19891 && TREE_STATIC (decl_or_origin)
19892 && DECL_RTL_SET_P (decl_or_origin)))
19893 /* When abstract origin already has DW_AT_location attribute, no need
19894 to add it again. */
19895 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19897 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19898 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19899 defer_location (decl_or_origin, var_die);
19900 else
19901 add_location_or_const_value_attribute (var_die, decl_or_origin,
19902 decl == NULL, DW_AT_location);
19903 add_pubname (decl_or_origin, var_die);
19905 else
19906 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19909 /* Generate a DIE to represent a named constant. */
19911 static void
19912 gen_const_die (tree decl, dw_die_ref context_die)
19914 dw_die_ref const_die;
19915 tree type = TREE_TYPE (decl);
19917 const_die = new_die (DW_TAG_constant, context_die, decl);
19918 add_name_and_src_coords_attributes (const_die, decl);
19919 add_type_attribute (const_die, type, 1, 0, context_die);
19920 if (TREE_PUBLIC (decl))
19921 add_AT_flag (const_die, DW_AT_external, 1);
19922 if (DECL_ARTIFICIAL (decl))
19923 add_AT_flag (const_die, DW_AT_artificial, 1);
19924 tree_add_const_value_attribute_for_decl (const_die, decl);
19927 /* Generate a DIE to represent a label identifier. */
19929 static void
19930 gen_label_die (tree decl, dw_die_ref context_die)
19932 tree origin = decl_ultimate_origin (decl);
19933 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19934 rtx insn;
19935 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19937 if (origin != NULL)
19938 add_abstract_origin_attribute (lbl_die, origin);
19939 else
19940 add_name_and_src_coords_attributes (lbl_die, decl);
19942 if (DECL_ABSTRACT (decl))
19943 equate_decl_number_to_die (decl, lbl_die);
19944 else
19946 insn = DECL_RTL_IF_SET (decl);
19948 /* Deleted labels are programmer specified labels which have been
19949 eliminated because of various optimizations. We still emit them
19950 here so that it is possible to put breakpoints on them. */
19951 if (insn
19952 && (LABEL_P (insn)
19953 || ((NOTE_P (insn)
19954 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19956 /* When optimization is enabled (via -O) some parts of the compiler
19957 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19958 represent source-level labels which were explicitly declared by
19959 the user. This really shouldn't be happening though, so catch
19960 it if it ever does happen. */
19961 gcc_assert (!INSN_DELETED_P (insn));
19963 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19964 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19969 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19970 attributes to the DIE for a block STMT, to describe where the inlined
19971 function was called from. This is similar to add_src_coords_attributes. */
19973 static inline void
19974 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19976 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19978 if (dwarf_version >= 3 || !dwarf_strict)
19980 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19981 add_AT_unsigned (die, DW_AT_call_line, s.line);
19986 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19987 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19989 static inline void
19990 add_high_low_attributes (tree stmt, dw_die_ref die)
19992 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19994 if (BLOCK_FRAGMENT_CHAIN (stmt)
19995 && (dwarf_version >= 3 || !dwarf_strict))
19997 tree chain;
19999 if (inlined_function_outer_scope_p (stmt))
20001 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20002 BLOCK_NUMBER (stmt));
20003 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20006 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
20008 chain = BLOCK_FRAGMENT_CHAIN (stmt);
20011 add_ranges (chain);
20012 chain = BLOCK_FRAGMENT_CHAIN (chain);
20014 while (chain);
20015 add_ranges (NULL);
20017 else
20019 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20020 BLOCK_NUMBER (stmt));
20021 add_AT_lbl_id (die, DW_AT_low_pc, label);
20022 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
20023 BLOCK_NUMBER (stmt));
20024 add_AT_lbl_id (die, DW_AT_high_pc, label);
20028 /* Generate a DIE for a lexical block. */
20030 static void
20031 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
20033 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
20035 if (call_arg_locations)
20037 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20038 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20039 BLOCK_NUMBER (stmt) + 1);
20040 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
20043 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
20044 add_high_low_attributes (stmt, stmt_die);
20046 decls_for_scope (stmt, stmt_die, depth);
20049 /* Generate a DIE for an inlined subprogram. */
20051 static void
20052 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
20054 tree decl;
20056 /* The instance of function that is effectively being inlined shall not
20057 be abstract. */
20058 gcc_assert (! BLOCK_ABSTRACT (stmt));
20060 decl = block_ultimate_origin (stmt);
20062 /* Emit info for the abstract instance first, if we haven't yet. We
20063 must emit this even if the block is abstract, otherwise when we
20064 emit the block below (or elsewhere), we may end up trying to emit
20065 a die whose origin die hasn't been emitted, and crashing. */
20066 dwarf2out_abstract_function (decl);
20068 if (! BLOCK_ABSTRACT (stmt))
20070 dw_die_ref subr_die
20071 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20073 if (call_arg_locations)
20075 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20076 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20077 BLOCK_NUMBER (stmt) + 1);
20078 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20080 add_abstract_origin_attribute (subr_die, decl);
20081 if (TREE_ASM_WRITTEN (stmt))
20082 add_high_low_attributes (stmt, subr_die);
20083 add_call_src_coords_attributes (stmt, subr_die);
20085 decls_for_scope (stmt, subr_die, depth);
20086 current_function_has_inlines = 1;
20090 /* Generate a DIE for a field in a record, or structure. */
20092 static void
20093 gen_field_die (tree decl, dw_die_ref context_die)
20095 dw_die_ref decl_die;
20097 if (TREE_TYPE (decl) == error_mark_node)
20098 return;
20100 decl_die = new_die (DW_TAG_member, context_die, decl);
20101 add_name_and_src_coords_attributes (decl_die, decl);
20102 add_type_attribute (decl_die, member_declared_type (decl),
20103 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20104 context_die);
20106 if (DECL_BIT_FIELD_TYPE (decl))
20108 add_byte_size_attribute (decl_die, decl);
20109 add_bit_size_attribute (decl_die, decl);
20110 add_bit_offset_attribute (decl_die, decl);
20113 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20114 add_data_member_location_attribute (decl_die, decl);
20116 if (DECL_ARTIFICIAL (decl))
20117 add_AT_flag (decl_die, DW_AT_artificial, 1);
20119 add_accessibility_attribute (decl_die, decl);
20121 /* Equate decl number to die, so that we can look up this decl later on. */
20122 equate_decl_number_to_die (decl, decl_die);
20125 #if 0
20126 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20127 Use modified_type_die instead.
20128 We keep this code here just in case these types of DIEs may be needed to
20129 represent certain things in other languages (e.g. Pascal) someday. */
20131 static void
20132 gen_pointer_type_die (tree type, dw_die_ref context_die)
20134 dw_die_ref ptr_die
20135 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20137 equate_type_number_to_die (type, ptr_die);
20138 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20139 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20142 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20143 Use modified_type_die instead.
20144 We keep this code here just in case these types of DIEs may be needed to
20145 represent certain things in other languages (e.g. Pascal) someday. */
20147 static void
20148 gen_reference_type_die (tree type, dw_die_ref context_die)
20150 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20152 if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
20153 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20154 else
20155 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20157 equate_type_number_to_die (type, ref_die);
20158 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20159 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20161 #endif
20163 /* Generate a DIE for a pointer to a member type. */
20165 static void
20166 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20168 dw_die_ref ptr_die
20169 = new_die (DW_TAG_ptr_to_member_type,
20170 scope_die_for (type, context_die), type);
20172 equate_type_number_to_die (type, ptr_die);
20173 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20174 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20175 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20178 /* Generate the DIE for the compilation unit. */
20180 static dw_die_ref
20181 gen_compile_unit_die (const char *filename)
20183 dw_die_ref die;
20184 char producer[250];
20185 const char *language_string = lang_hooks.name;
20186 int language;
20188 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20190 if (filename)
20192 add_name_attribute (die, filename);
20193 /* Don't add cwd for <built-in>. */
20194 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20195 add_comp_dir_attribute (die);
20198 sprintf (producer, "%s %s", language_string, version_string);
20200 #ifdef MIPS_DEBUGGING_INFO
20201 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20202 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20203 not appear in the producer string, the debugger reaches the conclusion
20204 that the object file is stripped and has no debugging information.
20205 To get the MIPS/SGI debugger to believe that there is debugging
20206 information in the object file, we add a -g to the producer string. */
20207 if (debug_info_level > DINFO_LEVEL_TERSE)
20208 strcat (producer, " -g");
20209 #endif
20211 add_AT_string (die, DW_AT_producer, producer);
20213 /* If our producer is LTO try to figure out a common language to use
20214 from the global list of translation units. */
20215 if (strcmp (language_string, "GNU GIMPLE") == 0)
20217 unsigned i;
20218 tree t;
20219 const char *common_lang = NULL;
20221 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20223 if (!TRANSLATION_UNIT_LANGUAGE (t))
20224 continue;
20225 if (!common_lang)
20226 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20227 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20229 else if (strncmp (common_lang, "GNU C", 5) == 0
20230 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20231 /* Mixing C and C++ is ok, use C++ in that case. */
20232 common_lang = "GNU C++";
20233 else
20235 /* Fall back to C. */
20236 common_lang = NULL;
20237 break;
20241 if (common_lang)
20242 language_string = common_lang;
20245 language = DW_LANG_C89;
20246 if (strcmp (language_string, "GNU C++") == 0)
20247 language = DW_LANG_C_plus_plus;
20248 else if (strcmp (language_string, "GNU F77") == 0)
20249 language = DW_LANG_Fortran77;
20250 else if (strcmp (language_string, "GNU Pascal") == 0)
20251 language = DW_LANG_Pascal83;
20252 else if (dwarf_version >= 3 || !dwarf_strict)
20254 if (strcmp (language_string, "GNU Ada") == 0)
20255 language = DW_LANG_Ada95;
20256 else if (strcmp (language_string, "GNU Fortran") == 0)
20257 language = DW_LANG_Fortran95;
20258 else if (strcmp (language_string, "GNU Java") == 0)
20259 language = DW_LANG_Java;
20260 else if (strcmp (language_string, "GNU Objective-C") == 0)
20261 language = DW_LANG_ObjC;
20262 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20263 language = DW_LANG_ObjC_plus_plus;
20266 add_AT_unsigned (die, DW_AT_language, language);
20268 switch (language)
20270 case DW_LANG_Fortran77:
20271 case DW_LANG_Fortran90:
20272 case DW_LANG_Fortran95:
20273 /* Fortran has case insensitive identifiers and the front-end
20274 lowercases everything. */
20275 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20276 break;
20277 default:
20278 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20279 break;
20281 return die;
20284 /* Generate the DIE for a base class. */
20286 static void
20287 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20289 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20291 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20292 add_data_member_location_attribute (die, binfo);
20294 if (BINFO_VIRTUAL_P (binfo))
20295 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20297 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20298 children, otherwise the default is DW_ACCESS_public. In DWARF2
20299 the default has always been DW_ACCESS_private. */
20300 if (access == access_public_node)
20302 if (dwarf_version == 2
20303 || context_die->die_tag == DW_TAG_class_type)
20304 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20306 else if (access == access_protected_node)
20307 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20308 else if (dwarf_version > 2
20309 && context_die->die_tag != DW_TAG_class_type)
20310 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20313 /* Generate a DIE for a class member. */
20315 static void
20316 gen_member_die (tree type, dw_die_ref context_die)
20318 tree member;
20319 tree binfo = TYPE_BINFO (type);
20320 dw_die_ref child;
20322 /* If this is not an incomplete type, output descriptions of each of its
20323 members. Note that as we output the DIEs necessary to represent the
20324 members of this record or union type, we will also be trying to output
20325 DIEs to represent the *types* of those members. However the `type'
20326 function (above) will specifically avoid generating type DIEs for member
20327 types *within* the list of member DIEs for this (containing) type except
20328 for those types (of members) which are explicitly marked as also being
20329 members of this (containing) type themselves. The g++ front- end can
20330 force any given type to be treated as a member of some other (containing)
20331 type by setting the TYPE_CONTEXT of the given (member) type to point to
20332 the TREE node representing the appropriate (containing) type. */
20334 /* First output info about the base classes. */
20335 if (binfo)
20337 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20338 int i;
20339 tree base;
20341 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20342 gen_inheritance_die (base,
20343 (accesses ? VEC_index (tree, accesses, i)
20344 : access_public_node), context_die);
20347 /* Now output info about the data members and type members. */
20348 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20350 /* If we thought we were generating minimal debug info for TYPE
20351 and then changed our minds, some of the member declarations
20352 may have already been defined. Don't define them again, but
20353 do put them in the right order. */
20355 child = lookup_decl_die (member);
20356 if (child)
20357 splice_child_die (context_die, child);
20358 else
20359 gen_decl_die (member, NULL, context_die);
20362 /* Now output info about the function members (if any). */
20363 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20365 /* Don't include clones in the member list. */
20366 if (DECL_ABSTRACT_ORIGIN (member))
20367 continue;
20369 child = lookup_decl_die (member);
20370 if (child)
20371 splice_child_die (context_die, child);
20372 else
20373 gen_decl_die (member, NULL, context_die);
20377 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20378 is set, we pretend that the type was never defined, so we only get the
20379 member DIEs needed by later specification DIEs. */
20381 static void
20382 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20383 enum debug_info_usage usage)
20385 dw_die_ref type_die = lookup_type_die (type);
20386 dw_die_ref scope_die = 0;
20387 int nested = 0;
20388 int complete = (TYPE_SIZE (type)
20389 && (! TYPE_STUB_DECL (type)
20390 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20391 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20392 complete = complete && should_emit_struct_debug (type, usage);
20394 if (type_die && ! complete)
20395 return;
20397 if (TYPE_CONTEXT (type) != NULL_TREE
20398 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20399 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20400 nested = 1;
20402 scope_die = scope_die_for (type, context_die);
20404 if (! type_die || (nested && is_cu_die (scope_die)))
20405 /* First occurrence of type or toplevel definition of nested class. */
20407 dw_die_ref old_die = type_die;
20409 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20410 ? record_type_tag (type) : DW_TAG_union_type,
20411 scope_die, type);
20412 equate_type_number_to_die (type, type_die);
20413 if (old_die)
20414 add_AT_specification (type_die, old_die);
20415 else
20416 add_name_attribute (type_die, type_tag (type));
20418 else
20419 remove_AT (type_die, DW_AT_declaration);
20421 /* Generate child dies for template paramaters. */
20422 if (debug_info_level > DINFO_LEVEL_TERSE
20423 && COMPLETE_TYPE_P (type))
20424 schedule_generic_params_dies_gen (type);
20426 /* If this type has been completed, then give it a byte_size attribute and
20427 then give a list of members. */
20428 if (complete && !ns_decl)
20430 /* Prevent infinite recursion in cases where the type of some member of
20431 this type is expressed in terms of this type itself. */
20432 TREE_ASM_WRITTEN (type) = 1;
20433 add_byte_size_attribute (type_die, type);
20434 if (TYPE_STUB_DECL (type) != NULL_TREE)
20436 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20437 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20440 /* If the first reference to this type was as the return type of an
20441 inline function, then it may not have a parent. Fix this now. */
20442 if (type_die->die_parent == NULL)
20443 add_child_die (scope_die, type_die);
20445 push_decl_scope (type);
20446 gen_member_die (type, type_die);
20447 pop_decl_scope ();
20449 /* GNU extension: Record what type our vtable lives in. */
20450 if (TYPE_VFIELD (type))
20452 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20454 gen_type_die (vtype, context_die);
20455 add_AT_die_ref (type_die, DW_AT_containing_type,
20456 lookup_type_die (vtype));
20459 else
20461 add_AT_flag (type_die, DW_AT_declaration, 1);
20463 /* We don't need to do this for function-local types. */
20464 if (TYPE_STUB_DECL (type)
20465 && ! decl_function_context (TYPE_STUB_DECL (type)))
20466 VEC_safe_push (tree, gc, incomplete_types, type);
20469 if (get_AT (type_die, DW_AT_name))
20470 add_pubtype (type, type_die);
20473 /* Generate a DIE for a subroutine _type_. */
20475 static void
20476 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20478 tree return_type = TREE_TYPE (type);
20479 dw_die_ref subr_die
20480 = new_die (DW_TAG_subroutine_type,
20481 scope_die_for (type, context_die), type);
20483 equate_type_number_to_die (type, subr_die);
20484 add_prototyped_attribute (subr_die, type);
20485 add_type_attribute (subr_die, return_type, 0, 0, context_die);
20486 gen_formal_types_die (type, subr_die);
20488 if (get_AT (subr_die, DW_AT_name))
20489 add_pubtype (type, subr_die);
20492 /* Generate a DIE for a type definition. */
20494 static void
20495 gen_typedef_die (tree decl, dw_die_ref context_die)
20497 dw_die_ref type_die;
20498 tree origin;
20500 if (TREE_ASM_WRITTEN (decl))
20501 return;
20503 TREE_ASM_WRITTEN (decl) = 1;
20504 type_die = new_die (DW_TAG_typedef, context_die, decl);
20505 origin = decl_ultimate_origin (decl);
20506 if (origin != NULL)
20507 add_abstract_origin_attribute (type_die, origin);
20508 else
20510 tree type;
20512 add_name_and_src_coords_attributes (type_die, decl);
20513 if (DECL_ORIGINAL_TYPE (decl))
20515 type = DECL_ORIGINAL_TYPE (decl);
20517 gcc_assert (type != TREE_TYPE (decl));
20518 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20520 else
20522 type = TREE_TYPE (decl);
20524 if (is_naming_typedef_decl (TYPE_NAME (type)))
20526 /* Here, we are in the case of decl being a typedef naming
20527 an anonymous type, e.g:
20528 typedef struct {...} foo;
20529 In that case TREE_TYPE (decl) is not a typedef variant
20530 type and TYPE_NAME of the anonymous type is set to the
20531 TYPE_DECL of the typedef. This construct is emitted by
20532 the C++ FE.
20534 TYPE is the anonymous struct named by the typedef
20535 DECL. As we need the DW_AT_type attribute of the
20536 DW_TAG_typedef to point to the DIE of TYPE, let's
20537 generate that DIE right away. add_type_attribute
20538 called below will then pick (via lookup_type_die) that
20539 anonymous struct DIE. */
20540 if (!TREE_ASM_WRITTEN (type))
20541 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20543 /* This is a GNU Extension. We are adding a
20544 DW_AT_linkage_name attribute to the DIE of the
20545 anonymous struct TYPE. The value of that attribute
20546 is the name of the typedef decl naming the anonymous
20547 struct. This greatly eases the work of consumers of
20548 this debug info. */
20549 add_linkage_attr (lookup_type_die (type), decl);
20553 add_type_attribute (type_die, type, TREE_READONLY (decl),
20554 TREE_THIS_VOLATILE (decl), context_die);
20556 if (is_naming_typedef_decl (decl))
20557 /* We want that all subsequent calls to lookup_type_die with
20558 TYPE in argument yield the DW_TAG_typedef we have just
20559 created. */
20560 equate_type_number_to_die (type, type_die);
20562 add_accessibility_attribute (type_die, decl);
20565 if (DECL_ABSTRACT (decl))
20566 equate_decl_number_to_die (decl, type_die);
20568 if (get_AT (type_die, DW_AT_name))
20569 add_pubtype (decl, type_die);
20572 /* Generate a DIE for a struct, class, enum or union type. */
20574 static void
20575 gen_tagged_type_die (tree type,
20576 dw_die_ref context_die,
20577 enum debug_info_usage usage)
20579 int need_pop;
20581 if (type == NULL_TREE
20582 || !is_tagged_type (type))
20583 return;
20585 /* If this is a nested type whose containing class hasn't been written
20586 out yet, writing it out will cover this one, too. This does not apply
20587 to instantiations of member class templates; they need to be added to
20588 the containing class as they are generated. FIXME: This hurts the
20589 idea of combining type decls from multiple TUs, since we can't predict
20590 what set of template instantiations we'll get. */
20591 if (TYPE_CONTEXT (type)
20592 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20593 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20595 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20597 if (TREE_ASM_WRITTEN (type))
20598 return;
20600 /* If that failed, attach ourselves to the stub. */
20601 push_decl_scope (TYPE_CONTEXT (type));
20602 context_die = lookup_type_die (TYPE_CONTEXT (type));
20603 need_pop = 1;
20605 else if (TYPE_CONTEXT (type) != NULL_TREE
20606 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20608 /* If this type is local to a function that hasn't been written
20609 out yet, use a NULL context for now; it will be fixed up in
20610 decls_for_scope. */
20611 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20612 /* A declaration DIE doesn't count; nested types need to go in the
20613 specification. */
20614 if (context_die && is_declaration_die (context_die))
20615 context_die = NULL;
20616 need_pop = 0;
20618 else
20620 context_die = declare_in_namespace (type, context_die);
20621 need_pop = 0;
20624 if (TREE_CODE (type) == ENUMERAL_TYPE)
20626 /* This might have been written out by the call to
20627 declare_in_namespace. */
20628 if (!TREE_ASM_WRITTEN (type))
20629 gen_enumeration_type_die (type, context_die);
20631 else
20632 gen_struct_or_union_type_die (type, context_die, usage);
20634 if (need_pop)
20635 pop_decl_scope ();
20637 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20638 it up if it is ever completed. gen_*_type_die will set it for us
20639 when appropriate. */
20642 /* Generate a type description DIE. */
20644 static void
20645 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20646 enum debug_info_usage usage)
20648 struct array_descr_info info;
20650 if (type == NULL_TREE || type == error_mark_node)
20651 return;
20653 if (TYPE_NAME (type) != NULL_TREE
20654 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20655 && is_redundant_typedef (TYPE_NAME (type))
20656 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20657 /* The DECL of this type is a typedef we don't want to emit debug
20658 info for but we want debug info for its underlying typedef.
20659 This can happen for e.g, the injected-class-name of a C++
20660 type. */
20661 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20663 /* If TYPE is a typedef type variant, let's generate debug info
20664 for the parent typedef which TYPE is a type of. */
20665 if (typedef_variant_p (type))
20667 if (TREE_ASM_WRITTEN (type))
20668 return;
20670 /* Prevent broken recursion; we can't hand off to the same type. */
20671 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20673 /* Use the DIE of the containing namespace as the parent DIE of
20674 the type description DIE we want to generate. */
20675 if (DECL_CONTEXT (TYPE_NAME (type))
20676 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20677 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20679 TREE_ASM_WRITTEN (type) = 1;
20681 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20682 return;
20685 /* If type is an anonymous tagged type named by a typedef, let's
20686 generate debug info for the typedef. */
20687 if (is_naming_typedef_decl (TYPE_NAME (type)))
20689 /* Use the DIE of the containing namespace as the parent DIE of
20690 the type description DIE we want to generate. */
20691 if (DECL_CONTEXT (TYPE_NAME (type))
20692 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20693 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20695 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20696 return;
20699 /* If this is an array type with hidden descriptor, handle it first. */
20700 if (!TREE_ASM_WRITTEN (type)
20701 && lang_hooks.types.get_array_descr_info
20702 && lang_hooks.types.get_array_descr_info (type, &info)
20703 && (dwarf_version >= 3 || !dwarf_strict))
20705 gen_descr_array_type_die (type, &info, context_die);
20706 TREE_ASM_WRITTEN (type) = 1;
20707 return;
20710 /* We are going to output a DIE to represent the unqualified version
20711 of this type (i.e. without any const or volatile qualifiers) so
20712 get the main variant (i.e. the unqualified version) of this type
20713 now. (Vectors are special because the debugging info is in the
20714 cloned type itself). */
20715 if (TREE_CODE (type) != VECTOR_TYPE)
20716 type = type_main_variant (type);
20718 if (TREE_ASM_WRITTEN (type))
20719 return;
20721 switch (TREE_CODE (type))
20723 case ERROR_MARK:
20724 break;
20726 case POINTER_TYPE:
20727 case REFERENCE_TYPE:
20728 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20729 ensures that the gen_type_die recursion will terminate even if the
20730 type is recursive. Recursive types are possible in Ada. */
20731 /* ??? We could perhaps do this for all types before the switch
20732 statement. */
20733 TREE_ASM_WRITTEN (type) = 1;
20735 /* For these types, all that is required is that we output a DIE (or a
20736 set of DIEs) to represent the "basis" type. */
20737 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20738 DINFO_USAGE_IND_USE);
20739 break;
20741 case OFFSET_TYPE:
20742 /* This code is used for C++ pointer-to-data-member types.
20743 Output a description of the relevant class type. */
20744 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20745 DINFO_USAGE_IND_USE);
20747 /* Output a description of the type of the object pointed to. */
20748 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20749 DINFO_USAGE_IND_USE);
20751 /* Now output a DIE to represent this pointer-to-data-member type
20752 itself. */
20753 gen_ptr_to_mbr_type_die (type, context_die);
20754 break;
20756 case FUNCTION_TYPE:
20757 /* Force out return type (in case it wasn't forced out already). */
20758 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20759 DINFO_USAGE_DIR_USE);
20760 gen_subroutine_type_die (type, context_die);
20761 break;
20763 case METHOD_TYPE:
20764 /* Force out return type (in case it wasn't forced out already). */
20765 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20766 DINFO_USAGE_DIR_USE);
20767 gen_subroutine_type_die (type, context_die);
20768 break;
20770 case ARRAY_TYPE:
20771 gen_array_type_die (type, context_die);
20772 break;
20774 case VECTOR_TYPE:
20775 gen_array_type_die (type, context_die);
20776 break;
20778 case ENUMERAL_TYPE:
20779 case RECORD_TYPE:
20780 case UNION_TYPE:
20781 case QUAL_UNION_TYPE:
20782 gen_tagged_type_die (type, context_die, usage);
20783 return;
20785 case VOID_TYPE:
20786 case INTEGER_TYPE:
20787 case REAL_TYPE:
20788 case FIXED_POINT_TYPE:
20789 case COMPLEX_TYPE:
20790 case BOOLEAN_TYPE:
20791 /* No DIEs needed for fundamental types. */
20792 break;
20794 case NULLPTR_TYPE:
20795 case LANG_TYPE:
20796 /* Just use DW_TAG_unspecified_type. */
20798 dw_die_ref type_die = lookup_type_die (type);
20799 if (type_die == NULL)
20801 tree name = TYPE_NAME (type);
20802 if (TREE_CODE (name) == TYPE_DECL)
20803 name = DECL_NAME (name);
20804 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20805 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20806 equate_type_number_to_die (type, type_die);
20809 break;
20811 default:
20812 gcc_unreachable ();
20815 TREE_ASM_WRITTEN (type) = 1;
20818 static void
20819 gen_type_die (tree type, dw_die_ref context_die)
20821 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20824 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20825 things which are local to the given block. */
20827 static void
20828 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20830 int must_output_die = 0;
20831 bool inlined_func;
20833 /* Ignore blocks that are NULL. */
20834 if (stmt == NULL_TREE)
20835 return;
20837 inlined_func = inlined_function_outer_scope_p (stmt);
20839 /* If the block is one fragment of a non-contiguous block, do not
20840 process the variables, since they will have been done by the
20841 origin block. Do process subblocks. */
20842 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20844 tree sub;
20846 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20847 gen_block_die (sub, context_die, depth + 1);
20849 return;
20852 /* Determine if we need to output any Dwarf DIEs at all to represent this
20853 block. */
20854 if (inlined_func)
20855 /* The outer scopes for inlinings *must* always be represented. We
20856 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20857 must_output_die = 1;
20858 else
20860 /* Determine if this block directly contains any "significant"
20861 local declarations which we will need to output DIEs for. */
20862 if (debug_info_level > DINFO_LEVEL_TERSE)
20863 /* We are not in terse mode so *any* local declaration counts
20864 as being a "significant" one. */
20865 must_output_die = ((BLOCK_VARS (stmt) != NULL
20866 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20867 && (TREE_USED (stmt)
20868 || TREE_ASM_WRITTEN (stmt)
20869 || BLOCK_ABSTRACT (stmt)));
20870 else if ((TREE_USED (stmt)
20871 || TREE_ASM_WRITTEN (stmt)
20872 || BLOCK_ABSTRACT (stmt))
20873 && !dwarf2out_ignore_block (stmt))
20874 must_output_die = 1;
20877 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20878 DIE for any block which contains no significant local declarations at
20879 all. Rather, in such cases we just call `decls_for_scope' so that any
20880 needed Dwarf info for any sub-blocks will get properly generated. Note
20881 that in terse mode, our definition of what constitutes a "significant"
20882 local declaration gets restricted to include only inlined function
20883 instances and local (nested) function definitions. */
20884 if (must_output_die)
20886 if (inlined_func)
20888 /* If STMT block is abstract, that means we have been called
20889 indirectly from dwarf2out_abstract_function.
20890 That function rightfully marks the descendent blocks (of
20891 the abstract function it is dealing with) as being abstract,
20892 precisely to prevent us from emitting any
20893 DW_TAG_inlined_subroutine DIE as a descendent
20894 of an abstract function instance. So in that case, we should
20895 not call gen_inlined_subroutine_die.
20897 Later though, when cgraph asks dwarf2out to emit info
20898 for the concrete instance of the function decl into which
20899 the concrete instance of STMT got inlined, the later will lead
20900 to the generation of a DW_TAG_inlined_subroutine DIE. */
20901 if (! BLOCK_ABSTRACT (stmt))
20902 gen_inlined_subroutine_die (stmt, context_die, depth);
20904 else
20905 gen_lexical_block_die (stmt, context_die, depth);
20907 else
20908 decls_for_scope (stmt, context_die, depth);
20911 /* Process variable DECL (or variable with origin ORIGIN) within
20912 block STMT and add it to CONTEXT_DIE. */
20913 static void
20914 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20916 dw_die_ref die;
20917 tree decl_or_origin = decl ? decl : origin;
20919 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20920 die = lookup_decl_die (decl_or_origin);
20921 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20922 && TYPE_DECL_IS_STUB (decl_or_origin))
20923 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20924 else
20925 die = NULL;
20927 if (die != NULL && die->die_parent == NULL)
20928 add_child_die (context_die, die);
20929 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20930 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20931 stmt, context_die);
20932 else
20933 gen_decl_die (decl, origin, context_die);
20936 /* Generate all of the decls declared within a given scope and (recursively)
20937 all of its sub-blocks. */
20939 static void
20940 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20942 tree decl;
20943 unsigned int i;
20944 tree subblocks;
20946 /* Ignore NULL blocks. */
20947 if (stmt == NULL_TREE)
20948 return;
20950 /* Output the DIEs to represent all of the data objects and typedefs
20951 declared directly within this block but not within any nested
20952 sub-blocks. Also, nested function and tag DIEs have been
20953 generated with a parent of NULL; fix that up now. */
20954 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20955 process_scope_var (stmt, decl, NULL_TREE, context_die);
20956 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20957 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20958 context_die);
20960 /* If we're at -g1, we're not interested in subblocks. */
20961 if (debug_info_level <= DINFO_LEVEL_TERSE)
20962 return;
20964 /* Output the DIEs to represent all sub-blocks (and the items declared
20965 therein) of this block. */
20966 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20967 subblocks != NULL;
20968 subblocks = BLOCK_CHAIN (subblocks))
20969 gen_block_die (subblocks, context_die, depth + 1);
20972 /* Is this a typedef we can avoid emitting? */
20974 static inline int
20975 is_redundant_typedef (const_tree decl)
20977 if (TYPE_DECL_IS_STUB (decl))
20978 return 1;
20980 if (DECL_ARTIFICIAL (decl)
20981 && DECL_CONTEXT (decl)
20982 && is_tagged_type (DECL_CONTEXT (decl))
20983 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20984 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20985 /* Also ignore the artificial member typedef for the class name. */
20986 return 1;
20988 return 0;
20991 /* Return TRUE if TYPE is a typedef that names a type for linkage
20992 purposes. This kind of typedefs is produced by the C++ FE for
20993 constructs like:
20995 typedef struct {...} foo;
20997 In that case, there is no typedef variant type produced for foo.
20998 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20999 struct type. */
21001 static bool
21002 is_naming_typedef_decl (const_tree decl)
21004 if (decl == NULL_TREE
21005 || TREE_CODE (decl) != TYPE_DECL
21006 || !is_tagged_type (TREE_TYPE (decl))
21007 || DECL_IS_BUILTIN (decl)
21008 || is_redundant_typedef (decl)
21009 /* It looks like Ada produces TYPE_DECLs that are very similar
21010 to C++ naming typedefs but that have different
21011 semantics. Let's be specific to c++ for now. */
21012 || !is_cxx ())
21013 return FALSE;
21015 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21016 && TYPE_NAME (TREE_TYPE (decl)) == decl
21017 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21018 != TYPE_NAME (TREE_TYPE (decl))));
21021 /* Returns the DIE for a context. */
21023 static inline dw_die_ref
21024 get_context_die (tree context)
21026 if (context)
21028 /* Find die that represents this context. */
21029 if (TYPE_P (context))
21031 context = TYPE_MAIN_VARIANT (context);
21032 return strip_naming_typedef (context, force_type_die (context));
21034 else
21035 return force_decl_die (context);
21037 return comp_unit_die ();
21040 /* Returns the DIE for decl. A DIE will always be returned. */
21042 static dw_die_ref
21043 force_decl_die (tree decl)
21045 dw_die_ref decl_die;
21046 unsigned saved_external_flag;
21047 tree save_fn = NULL_TREE;
21048 decl_die = lookup_decl_die (decl);
21049 if (!decl_die)
21051 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21053 decl_die = lookup_decl_die (decl);
21054 if (decl_die)
21055 return decl_die;
21057 switch (TREE_CODE (decl))
21059 case FUNCTION_DECL:
21060 /* Clear current_function_decl, so that gen_subprogram_die thinks
21061 that this is a declaration. At this point, we just want to force
21062 declaration die. */
21063 save_fn = current_function_decl;
21064 current_function_decl = NULL_TREE;
21065 gen_subprogram_die (decl, context_die);
21066 current_function_decl = save_fn;
21067 break;
21069 case VAR_DECL:
21070 /* Set external flag to force declaration die. Restore it after
21071 gen_decl_die() call. */
21072 saved_external_flag = DECL_EXTERNAL (decl);
21073 DECL_EXTERNAL (decl) = 1;
21074 gen_decl_die (decl, NULL, context_die);
21075 DECL_EXTERNAL (decl) = saved_external_flag;
21076 break;
21078 case NAMESPACE_DECL:
21079 if (dwarf_version >= 3 || !dwarf_strict)
21080 dwarf2out_decl (decl);
21081 else
21082 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21083 decl_die = comp_unit_die ();
21084 break;
21086 case TRANSLATION_UNIT_DECL:
21087 decl_die = comp_unit_die ();
21088 break;
21090 default:
21091 gcc_unreachable ();
21094 /* We should be able to find the DIE now. */
21095 if (!decl_die)
21096 decl_die = lookup_decl_die (decl);
21097 gcc_assert (decl_die);
21100 return decl_die;
21103 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21104 always returned. */
21106 static dw_die_ref
21107 force_type_die (tree type)
21109 dw_die_ref type_die;
21111 type_die = lookup_type_die (type);
21112 if (!type_die)
21114 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21116 type_die = modified_type_die (type, TYPE_READONLY (type),
21117 TYPE_VOLATILE (type), context_die);
21118 gcc_assert (type_die);
21120 return type_die;
21123 /* Force out any required namespaces to be able to output DECL,
21124 and return the new context_die for it, if it's changed. */
21126 static dw_die_ref
21127 setup_namespace_context (tree thing, dw_die_ref context_die)
21129 tree context = (DECL_P (thing)
21130 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21131 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21132 /* Force out the namespace. */
21133 context_die = force_decl_die (context);
21135 return context_die;
21138 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21139 type) within its namespace, if appropriate.
21141 For compatibility with older debuggers, namespace DIEs only contain
21142 declarations; all definitions are emitted at CU scope. */
21144 static dw_die_ref
21145 declare_in_namespace (tree thing, dw_die_ref context_die)
21147 dw_die_ref ns_context;
21149 if (debug_info_level <= DINFO_LEVEL_TERSE)
21150 return context_die;
21152 /* If this decl is from an inlined function, then don't try to emit it in its
21153 namespace, as we will get confused. It would have already been emitted
21154 when the abstract instance of the inline function was emitted anyways. */
21155 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21156 return context_die;
21158 ns_context = setup_namespace_context (thing, context_die);
21160 if (ns_context != context_die)
21162 if (is_fortran ())
21163 return ns_context;
21164 if (DECL_P (thing))
21165 gen_decl_die (thing, NULL, ns_context);
21166 else
21167 gen_type_die (thing, ns_context);
21169 return context_die;
21172 /* Generate a DIE for a namespace or namespace alias. */
21174 static void
21175 gen_namespace_die (tree decl, dw_die_ref context_die)
21177 dw_die_ref namespace_die;
21179 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21180 they are an alias of. */
21181 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21183 /* Output a real namespace or module. */
21184 context_die = setup_namespace_context (decl, comp_unit_die ());
21185 namespace_die = new_die (is_fortran ()
21186 ? DW_TAG_module : DW_TAG_namespace,
21187 context_die, decl);
21188 /* For Fortran modules defined in different CU don't add src coords. */
21189 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21191 const char *name = dwarf2_name (decl, 0);
21192 if (name)
21193 add_name_attribute (namespace_die, name);
21195 else
21196 add_name_and_src_coords_attributes (namespace_die, decl);
21197 if (DECL_EXTERNAL (decl))
21198 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21199 equate_decl_number_to_die (decl, namespace_die);
21201 else
21203 /* Output a namespace alias. */
21205 /* Force out the namespace we are an alias of, if necessary. */
21206 dw_die_ref origin_die
21207 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21209 if (DECL_FILE_SCOPE_P (decl)
21210 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21211 context_die = setup_namespace_context (decl, comp_unit_die ());
21212 /* Now create the namespace alias DIE. */
21213 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21214 add_name_and_src_coords_attributes (namespace_die, decl);
21215 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21216 equate_decl_number_to_die (decl, namespace_die);
21220 /* Generate Dwarf debug information for a decl described by DECL.
21221 The return value is currently only meaningful for PARM_DECLs,
21222 for all other decls it returns NULL. */
21224 static dw_die_ref
21225 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21227 tree decl_or_origin = decl ? decl : origin;
21228 tree class_origin = NULL, ultimate_origin;
21230 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21231 return NULL;
21233 switch (TREE_CODE (decl_or_origin))
21235 case ERROR_MARK:
21236 break;
21238 case CONST_DECL:
21239 if (!is_fortran () && !is_ada ())
21241 /* The individual enumerators of an enum type get output when we output
21242 the Dwarf representation of the relevant enum type itself. */
21243 break;
21246 /* Emit its type. */
21247 gen_type_die (TREE_TYPE (decl), context_die);
21249 /* And its containing namespace. */
21250 context_die = declare_in_namespace (decl, context_die);
21252 gen_const_die (decl, context_die);
21253 break;
21255 case FUNCTION_DECL:
21256 /* Don't output any DIEs to represent mere function declarations,
21257 unless they are class members or explicit block externs. */
21258 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21259 && DECL_FILE_SCOPE_P (decl_or_origin)
21260 && (current_function_decl == NULL_TREE
21261 || DECL_ARTIFICIAL (decl_or_origin)))
21262 break;
21264 #if 0
21265 /* FIXME */
21266 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21267 on local redeclarations of global functions. That seems broken. */
21268 if (current_function_decl != decl)
21269 /* This is only a declaration. */;
21270 #endif
21272 /* If we're emitting a clone, emit info for the abstract instance. */
21273 if (origin || DECL_ORIGIN (decl) != decl)
21274 dwarf2out_abstract_function (origin
21275 ? DECL_ORIGIN (origin)
21276 : DECL_ABSTRACT_ORIGIN (decl));
21278 /* If we're emitting an out-of-line copy of an inline function,
21279 emit info for the abstract instance and set up to refer to it. */
21280 else if (cgraph_function_possibly_inlined_p (decl)
21281 && ! DECL_ABSTRACT (decl)
21282 && ! class_or_namespace_scope_p (context_die)
21283 /* dwarf2out_abstract_function won't emit a die if this is just
21284 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21285 that case, because that works only if we have a die. */
21286 && DECL_INITIAL (decl) != NULL_TREE)
21288 dwarf2out_abstract_function (decl);
21289 set_decl_origin_self (decl);
21292 /* Otherwise we're emitting the primary DIE for this decl. */
21293 else if (debug_info_level > DINFO_LEVEL_TERSE)
21295 /* Before we describe the FUNCTION_DECL itself, make sure that we
21296 have its containing type. */
21297 if (!origin)
21298 origin = decl_class_context (decl);
21299 if (origin != NULL_TREE)
21300 gen_type_die (origin, context_die);
21302 /* And its return type. */
21303 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21305 /* And its virtual context. */
21306 if (DECL_VINDEX (decl) != NULL_TREE)
21307 gen_type_die (DECL_CONTEXT (decl), context_die);
21309 /* Make sure we have a member DIE for decl. */
21310 if (origin != NULL_TREE)
21311 gen_type_die_for_member (origin, decl, context_die);
21313 /* And its containing namespace. */
21314 context_die = declare_in_namespace (decl, context_die);
21317 /* Now output a DIE to represent the function itself. */
21318 if (decl)
21319 gen_subprogram_die (decl, context_die);
21320 break;
21322 case TYPE_DECL:
21323 /* If we are in terse mode, don't generate any DIEs to represent any
21324 actual typedefs. */
21325 if (debug_info_level <= DINFO_LEVEL_TERSE)
21326 break;
21328 /* In the special case of a TYPE_DECL node representing the declaration
21329 of some type tag, if the given TYPE_DECL is marked as having been
21330 instantiated from some other (original) TYPE_DECL node (e.g. one which
21331 was generated within the original definition of an inline function) we
21332 used to generate a special (abbreviated) DW_TAG_structure_type,
21333 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21334 should be actually referencing those DIEs, as variable DIEs with that
21335 type would be emitted already in the abstract origin, so it was always
21336 removed during unused type prunning. Don't add anything in this
21337 case. */
21338 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21339 break;
21341 if (is_redundant_typedef (decl))
21342 gen_type_die (TREE_TYPE (decl), context_die);
21343 else
21344 /* Output a DIE to represent the typedef itself. */
21345 gen_typedef_die (decl, context_die);
21346 break;
21348 case LABEL_DECL:
21349 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21350 gen_label_die (decl, context_die);
21351 break;
21353 case VAR_DECL:
21354 case RESULT_DECL:
21355 /* If we are in terse mode, don't generate any DIEs to represent any
21356 variable declarations or definitions. */
21357 if (debug_info_level <= DINFO_LEVEL_TERSE)
21358 break;
21360 /* Output any DIEs that are needed to specify the type of this data
21361 object. */
21362 if (decl_by_reference_p (decl_or_origin))
21363 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21364 else
21365 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21367 /* And its containing type. */
21368 class_origin = decl_class_context (decl_or_origin);
21369 if (class_origin != NULL_TREE)
21370 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21372 /* And its containing namespace. */
21373 context_die = declare_in_namespace (decl_or_origin, context_die);
21375 /* Now output the DIE to represent the data object itself. This gets
21376 complicated because of the possibility that the VAR_DECL really
21377 represents an inlined instance of a formal parameter for an inline
21378 function. */
21379 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21380 if (ultimate_origin != NULL_TREE
21381 && TREE_CODE (ultimate_origin) == PARM_DECL)
21382 gen_formal_parameter_die (decl, origin,
21383 true /* Emit name attribute. */,
21384 context_die);
21385 else
21386 gen_variable_die (decl, origin, context_die);
21387 break;
21389 case FIELD_DECL:
21390 /* Ignore the nameless fields that are used to skip bits but handle C++
21391 anonymous unions and structs. */
21392 if (DECL_NAME (decl) != NULL_TREE
21393 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21394 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21396 gen_type_die (member_declared_type (decl), context_die);
21397 gen_field_die (decl, context_die);
21399 break;
21401 case PARM_DECL:
21402 if (DECL_BY_REFERENCE (decl_or_origin))
21403 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21404 else
21405 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21406 return gen_formal_parameter_die (decl, origin,
21407 true /* Emit name attribute. */,
21408 context_die);
21410 case NAMESPACE_DECL:
21411 case IMPORTED_DECL:
21412 if (dwarf_version >= 3 || !dwarf_strict)
21413 gen_namespace_die (decl, context_die);
21414 break;
21416 default:
21417 /* Probably some frontend-internal decl. Assume we don't care. */
21418 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21419 break;
21422 return NULL;
21425 /* Output debug information for global decl DECL. Called from toplev.c after
21426 compilation proper has finished. */
21428 static void
21429 dwarf2out_global_decl (tree decl)
21431 /* Output DWARF2 information for file-scope tentative data object
21432 declarations, file-scope (extern) function declarations (which
21433 had no corresponding body) and file-scope tagged type declarations
21434 and definitions which have not yet been forced out. */
21435 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21436 dwarf2out_decl (decl);
21439 /* Output debug information for type decl DECL. Called from toplev.c
21440 and from language front ends (to record built-in types). */
21441 static void
21442 dwarf2out_type_decl (tree decl, int local)
21444 if (!local)
21445 dwarf2out_decl (decl);
21448 /* Output debug information for imported module or decl DECL.
21449 NAME is non-NULL name in the lexical block if the decl has been renamed.
21450 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21451 that DECL belongs to.
21452 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21453 static void
21454 dwarf2out_imported_module_or_decl_1 (tree decl,
21455 tree name,
21456 tree lexical_block,
21457 dw_die_ref lexical_block_die)
21459 expanded_location xloc;
21460 dw_die_ref imported_die = NULL;
21461 dw_die_ref at_import_die;
21463 if (TREE_CODE (decl) == IMPORTED_DECL)
21465 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21466 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21467 gcc_assert (decl);
21469 else
21470 xloc = expand_location (input_location);
21472 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21474 at_import_die = force_type_die (TREE_TYPE (decl));
21475 /* For namespace N { typedef void T; } using N::T; base_type_die
21476 returns NULL, but DW_TAG_imported_declaration requires
21477 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21478 if (!at_import_die)
21480 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21481 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21482 at_import_die = lookup_type_die (TREE_TYPE (decl));
21483 gcc_assert (at_import_die);
21486 else
21488 at_import_die = lookup_decl_die (decl);
21489 if (!at_import_die)
21491 /* If we're trying to avoid duplicate debug info, we may not have
21492 emitted the member decl for this field. Emit it now. */
21493 if (TREE_CODE (decl) == FIELD_DECL)
21495 tree type = DECL_CONTEXT (decl);
21497 if (TYPE_CONTEXT (type)
21498 && TYPE_P (TYPE_CONTEXT (type))
21499 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21500 DINFO_USAGE_DIR_USE))
21501 return;
21502 gen_type_die_for_member (type, decl,
21503 get_context_die (TYPE_CONTEXT (type)));
21505 at_import_die = force_decl_die (decl);
21509 if (TREE_CODE (decl) == NAMESPACE_DECL)
21511 if (dwarf_version >= 3 || !dwarf_strict)
21512 imported_die = new_die (DW_TAG_imported_module,
21513 lexical_block_die,
21514 lexical_block);
21515 else
21516 return;
21518 else
21519 imported_die = new_die (DW_TAG_imported_declaration,
21520 lexical_block_die,
21521 lexical_block);
21523 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21524 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21525 if (name)
21526 add_AT_string (imported_die, DW_AT_name,
21527 IDENTIFIER_POINTER (name));
21528 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21531 /* Output debug information for imported module or decl DECL.
21532 NAME is non-NULL name in context if the decl has been renamed.
21533 CHILD is true if decl is one of the renamed decls as part of
21534 importing whole module. */
21536 static void
21537 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21538 bool child)
21540 /* dw_die_ref at_import_die; */
21541 dw_die_ref scope_die;
21543 if (debug_info_level <= DINFO_LEVEL_TERSE)
21544 return;
21546 gcc_assert (decl);
21548 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21549 We need decl DIE for reference and scope die. First, get DIE for the decl
21550 itself. */
21552 /* Get the scope die for decl context. Use comp_unit_die for global module
21553 or decl. If die is not found for non globals, force new die. */
21554 if (context
21555 && TYPE_P (context)
21556 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21557 return;
21559 if (!(dwarf_version >= 3 || !dwarf_strict))
21560 return;
21562 scope_die = get_context_die (context);
21564 if (child)
21566 gcc_assert (scope_die->die_child);
21567 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21568 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21569 scope_die = scope_die->die_child;
21572 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21573 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21577 /* Write the debugging output for DECL. */
21579 void
21580 dwarf2out_decl (tree decl)
21582 dw_die_ref context_die = comp_unit_die ();
21584 switch (TREE_CODE (decl))
21586 case ERROR_MARK:
21587 return;
21589 case FUNCTION_DECL:
21590 /* What we would really like to do here is to filter out all mere
21591 file-scope declarations of file-scope functions which are never
21592 referenced later within this translation unit (and keep all of ones
21593 that *are* referenced later on) but we aren't clairvoyant, so we have
21594 no idea which functions will be referenced in the future (i.e. later
21595 on within the current translation unit). So here we just ignore all
21596 file-scope function declarations which are not also definitions. If
21597 and when the debugger needs to know something about these functions,
21598 it will have to hunt around and find the DWARF information associated
21599 with the definition of the function.
21601 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21602 nodes represent definitions and which ones represent mere
21603 declarations. We have to check DECL_INITIAL instead. That's because
21604 the C front-end supports some weird semantics for "extern inline"
21605 function definitions. These can get inlined within the current
21606 translation unit (and thus, we need to generate Dwarf info for their
21607 abstract instances so that the Dwarf info for the concrete inlined
21608 instances can have something to refer to) but the compiler never
21609 generates any out-of-lines instances of such things (despite the fact
21610 that they *are* definitions).
21612 The important point is that the C front-end marks these "extern
21613 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21614 them anyway. Note that the C++ front-end also plays some similar games
21615 for inline function definitions appearing within include files which
21616 also contain `#pragma interface' pragmas. */
21617 if (DECL_INITIAL (decl) == NULL_TREE)
21618 return;
21620 /* If we're a nested function, initially use a parent of NULL; if we're
21621 a plain function, this will be fixed up in decls_for_scope. If
21622 we're a method, it will be ignored, since we already have a DIE. */
21623 if (decl_function_context (decl)
21624 /* But if we're in terse mode, we don't care about scope. */
21625 && debug_info_level > DINFO_LEVEL_TERSE)
21626 context_die = NULL;
21627 break;
21629 case VAR_DECL:
21630 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21631 declaration and if the declaration was never even referenced from
21632 within this entire compilation unit. We suppress these DIEs in
21633 order to save space in the .debug section (by eliminating entries
21634 which are probably useless). Note that we must not suppress
21635 block-local extern declarations (whether used or not) because that
21636 would screw-up the debugger's name lookup mechanism and cause it to
21637 miss things which really ought to be in scope at a given point. */
21638 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21639 return;
21641 /* For local statics lookup proper context die. */
21642 if (TREE_STATIC (decl) && decl_function_context (decl))
21643 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21645 /* If we are in terse mode, don't generate any DIEs to represent any
21646 variable declarations or definitions. */
21647 if (debug_info_level <= DINFO_LEVEL_TERSE)
21648 return;
21649 break;
21651 case CONST_DECL:
21652 if (debug_info_level <= DINFO_LEVEL_TERSE)
21653 return;
21654 if (!is_fortran () && !is_ada ())
21655 return;
21656 if (TREE_STATIC (decl) && decl_function_context (decl))
21657 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21658 break;
21660 case NAMESPACE_DECL:
21661 case IMPORTED_DECL:
21662 if (debug_info_level <= DINFO_LEVEL_TERSE)
21663 return;
21664 if (lookup_decl_die (decl) != NULL)
21665 return;
21666 break;
21668 case TYPE_DECL:
21669 /* Don't emit stubs for types unless they are needed by other DIEs. */
21670 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21671 return;
21673 /* Don't bother trying to generate any DIEs to represent any of the
21674 normal built-in types for the language we are compiling. */
21675 if (DECL_IS_BUILTIN (decl))
21676 return;
21678 /* If we are in terse mode, don't generate any DIEs for types. */
21679 if (debug_info_level <= DINFO_LEVEL_TERSE)
21680 return;
21682 /* If we're a function-scope tag, initially use a parent of NULL;
21683 this will be fixed up in decls_for_scope. */
21684 if (decl_function_context (decl))
21685 context_die = NULL;
21687 break;
21689 default:
21690 return;
21693 gen_decl_die (decl, NULL, context_die);
21696 /* Write the debugging output for DECL. */
21698 static void
21699 dwarf2out_function_decl (tree decl)
21701 dwarf2out_decl (decl);
21702 call_arg_locations = NULL;
21703 call_arg_loc_last = NULL;
21704 call_site_count = -1;
21705 tail_call_site_count = -1;
21706 VEC_free (dw_die_ref, heap, block_map);
21707 htab_empty (decl_loc_table);
21708 htab_empty (cached_dw_loc_list_table);
21711 /* Output a marker (i.e. a label) for the beginning of the generated code for
21712 a lexical block. */
21714 static void
21715 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21716 unsigned int blocknum)
21718 switch_to_section (current_function_section ());
21719 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21722 /* Output a marker (i.e. a label) for the end of the generated code for a
21723 lexical block. */
21725 static void
21726 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21728 switch_to_section (current_function_section ());
21729 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21732 /* Returns nonzero if it is appropriate not to emit any debugging
21733 information for BLOCK, because it doesn't contain any instructions.
21735 Don't allow this for blocks with nested functions or local classes
21736 as we would end up with orphans, and in the presence of scheduling
21737 we may end up calling them anyway. */
21739 static bool
21740 dwarf2out_ignore_block (const_tree block)
21742 tree decl;
21743 unsigned int i;
21745 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21746 if (TREE_CODE (decl) == FUNCTION_DECL
21747 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21748 return 0;
21749 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21751 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21752 if (TREE_CODE (decl) == FUNCTION_DECL
21753 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21754 return 0;
21757 return 1;
21760 /* Hash table routines for file_hash. */
21762 static int
21763 file_table_eq (const void *p1_p, const void *p2_p)
21765 const struct dwarf_file_data *const p1 =
21766 (const struct dwarf_file_data *) p1_p;
21767 const char *const p2 = (const char *) p2_p;
21768 return filename_cmp (p1->filename, p2) == 0;
21771 static hashval_t
21772 file_table_hash (const void *p_p)
21774 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21775 return htab_hash_string (p->filename);
21778 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21779 dwarf2out.c) and return its "index". The index of each (known) filename is
21780 just a unique number which is associated with only that one filename. We
21781 need such numbers for the sake of generating labels (in the .debug_sfnames
21782 section) and references to those files numbers (in the .debug_srcinfo
21783 and.debug_macinfo sections). If the filename given as an argument is not
21784 found in our current list, add it to the list and assign it the next
21785 available unique index number. In order to speed up searches, we remember
21786 the index of the filename was looked up last. This handles the majority of
21787 all searches. */
21789 static struct dwarf_file_data *
21790 lookup_filename (const char *file_name)
21792 void ** slot;
21793 struct dwarf_file_data * created;
21795 /* Check to see if the file name that was searched on the previous
21796 call matches this file name. If so, return the index. */
21797 if (file_table_last_lookup
21798 && (file_name == file_table_last_lookup->filename
21799 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21800 return file_table_last_lookup;
21802 /* Didn't match the previous lookup, search the table. */
21803 slot = htab_find_slot_with_hash (file_table, file_name,
21804 htab_hash_string (file_name), INSERT);
21805 if (*slot)
21806 return (struct dwarf_file_data *) *slot;
21808 created = ggc_alloc_dwarf_file_data ();
21809 created->filename = file_name;
21810 created->emitted_number = 0;
21811 *slot = created;
21812 return created;
21815 /* If the assembler will construct the file table, then translate the compiler
21816 internal file table number into the assembler file table number, and emit
21817 a .file directive if we haven't already emitted one yet. The file table
21818 numbers are different because we prune debug info for unused variables and
21819 types, which may include filenames. */
21821 static int
21822 maybe_emit_file (struct dwarf_file_data * fd)
21824 if (! fd->emitted_number)
21826 if (last_emitted_file)
21827 fd->emitted_number = last_emitted_file->emitted_number + 1;
21828 else
21829 fd->emitted_number = 1;
21830 last_emitted_file = fd;
21832 if (DWARF2_ASM_LINE_DEBUG_INFO)
21834 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21835 output_quoted_string (asm_out_file,
21836 remap_debug_filename (fd->filename));
21837 fputc ('\n', asm_out_file);
21841 return fd->emitted_number;
21844 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21845 That generation should happen after function debug info has been
21846 generated. The value of the attribute is the constant value of ARG. */
21848 static void
21849 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21851 die_arg_entry entry;
21853 if (!die || !arg)
21854 return;
21856 if (!tmpl_value_parm_die_table)
21857 tmpl_value_parm_die_table
21858 = VEC_alloc (die_arg_entry, gc, 32);
21860 entry.die = die;
21861 entry.arg = arg;
21862 VEC_safe_push (die_arg_entry, gc,
21863 tmpl_value_parm_die_table,
21864 &entry);
21867 /* Return TRUE if T is an instance of generic type, FALSE
21868 otherwise. */
21870 static bool
21871 generic_type_p (tree t)
21873 if (t == NULL_TREE || !TYPE_P (t))
21874 return false;
21875 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21878 /* Schedule the generation of the generic parameter dies for the
21879 instance of generic type T. The proper generation itself is later
21880 done by gen_scheduled_generic_parms_dies. */
21882 static void
21883 schedule_generic_params_dies_gen (tree t)
21885 if (!generic_type_p (t))
21886 return;
21888 if (generic_type_instances == NULL)
21889 generic_type_instances = VEC_alloc (tree, gc, 256);
21891 VEC_safe_push (tree, gc, generic_type_instances, t);
21894 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21895 by append_entry_to_tmpl_value_parm_die_table. This function must
21896 be called after function DIEs have been generated. */
21898 static void
21899 gen_remaining_tmpl_value_param_die_attribute (void)
21901 if (tmpl_value_parm_die_table)
21903 unsigned i;
21904 die_arg_entry *e;
21906 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21907 tree_add_const_value_attribute (e->die, e->arg);
21911 /* Generate generic parameters DIEs for instances of generic types
21912 that have been previously scheduled by
21913 schedule_generic_params_dies_gen. This function must be called
21914 after all the types of the CU have been laid out. */
21916 static void
21917 gen_scheduled_generic_parms_dies (void)
21919 unsigned i;
21920 tree t;
21922 if (generic_type_instances == NULL)
21923 return;
21925 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21926 gen_generic_params_dies (t);
21930 /* Replace DW_AT_name for the decl with name. */
21932 static void
21933 dwarf2out_set_name (tree decl, tree name)
21935 dw_die_ref die;
21936 dw_attr_ref attr;
21937 const char *dname;
21939 die = TYPE_SYMTAB_DIE (decl);
21940 if (!die)
21941 return;
21943 dname = dwarf2_name (name, 0);
21944 if (!dname)
21945 return;
21947 attr = get_AT (die, DW_AT_name);
21948 if (attr)
21950 struct indirect_string_node *node;
21952 node = find_AT_string (dname);
21953 /* replace the string. */
21954 attr->dw_attr_val.v.val_str = node;
21957 else
21958 add_name_attribute (die, dname);
21961 /* Called by the final INSN scan whenever we see a var location. We
21962 use it to drop labels in the right places, and throw the location in
21963 our lookup table. */
21965 static void
21966 dwarf2out_var_location (rtx loc_note)
21968 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21969 struct var_loc_node *newloc;
21970 rtx next_real;
21971 static const char *last_label;
21972 static const char *last_postcall_label;
21973 static bool last_in_cold_section_p;
21974 tree decl;
21975 bool var_loc_p;
21977 if (!NOTE_P (loc_note))
21979 if (CALL_P (loc_note))
21981 call_site_count++;
21982 if (SIBLING_CALL_P (loc_note))
21983 tail_call_site_count++;
21985 return;
21988 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21989 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21990 return;
21992 next_real = next_real_insn (loc_note);
21994 /* If there are no instructions which would be affected by this note,
21995 don't do anything. */
21996 if (var_loc_p
21997 && next_real == NULL_RTX
21998 && !NOTE_DURING_CALL_P (loc_note))
21999 return;
22001 if (next_real == NULL_RTX)
22002 next_real = get_last_insn ();
22004 /* If there were any real insns between note we processed last time
22005 and this note (or if it is the first note), clear
22006 last_{,postcall_}label so that they are not reused this time. */
22007 if (last_var_location_insn == NULL_RTX
22008 || last_var_location_insn != next_real
22009 || last_in_cold_section_p != in_cold_section_p)
22011 last_label = NULL;
22012 last_postcall_label = NULL;
22015 if (var_loc_p)
22017 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22018 newloc = add_var_loc_to_decl (decl, loc_note,
22019 NOTE_DURING_CALL_P (loc_note)
22020 ? last_postcall_label : last_label);
22021 if (newloc == NULL)
22022 return;
22024 else
22026 decl = NULL_TREE;
22027 newloc = NULL;
22030 /* If there were no real insns between note we processed last time
22031 and this note, use the label we emitted last time. Otherwise
22032 create a new label and emit it. */
22033 if (last_label == NULL)
22035 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22036 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22037 loclabel_num++;
22038 last_label = ggc_strdup (loclabel);
22041 if (!var_loc_p)
22043 struct call_arg_loc_node *ca_loc
22044 = ggc_alloc_cleared_call_arg_loc_node ();
22045 rtx prev = prev_real_insn (loc_note), x;
22046 ca_loc->call_arg_loc_note = loc_note;
22047 ca_loc->next = NULL;
22048 ca_loc->label = last_label;
22049 gcc_assert (prev
22050 && (CALL_P (prev)
22051 || (NONJUMP_INSN_P (prev)
22052 && GET_CODE (PATTERN (prev)) == SEQUENCE
22053 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22054 if (!CALL_P (prev))
22055 prev = XVECEXP (PATTERN (prev), 0, 0);
22056 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22057 x = PATTERN (prev);
22058 if (GET_CODE (x) == PARALLEL)
22059 x = XVECEXP (x, 0, 0);
22060 if (GET_CODE (x) == SET)
22061 x = SET_SRC (x);
22062 if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22064 x = XEXP (XEXP (x, 0), 0);
22065 if (GET_CODE (x) == SYMBOL_REF
22066 && SYMBOL_REF_DECL (x)
22067 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22068 ca_loc->symbol_ref = x;
22070 ca_loc->block = insn_scope (prev);
22071 if (call_arg_locations)
22072 call_arg_loc_last->next = ca_loc;
22073 else
22074 call_arg_locations = ca_loc;
22075 call_arg_loc_last = ca_loc;
22077 else if (!NOTE_DURING_CALL_P (loc_note))
22078 newloc->label = last_label;
22079 else
22081 if (!last_postcall_label)
22083 sprintf (loclabel, "%s-1", last_label);
22084 last_postcall_label = ggc_strdup (loclabel);
22086 newloc->label = last_postcall_label;
22089 last_var_location_insn = next_real;
22090 last_in_cold_section_p = in_cold_section_p;
22093 /* Note in one location list that text section has changed. */
22095 static int
22096 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
22098 var_loc_list *list = (var_loc_list *) *slot;
22099 if (list->first)
22100 list->last_before_switch
22101 = list->last->next ? list->last->next : list->last;
22102 return 1;
22105 /* Note in all location lists that text section has changed. */
22107 static void
22108 var_location_switch_text_section (void)
22110 if (decl_loc_table == NULL)
22111 return;
22113 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22116 /* We need to reset the locations at the beginning of each
22117 function. We can't do this in the end_function hook, because the
22118 declarations that use the locations won't have been output when
22119 that hook is called. Also compute have_multiple_function_sections here. */
22121 static void
22122 dwarf2out_begin_function (tree fun)
22124 if (function_section (fun) != text_section)
22125 have_multiple_function_sections = true;
22126 if (flag_reorder_blocks_and_partition && !cold_text_section)
22128 gcc_assert (current_function_decl == fun);
22129 cold_text_section = unlikely_text_section ();
22130 switch_to_section (cold_text_section);
22131 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22132 switch_to_section (current_function_section ());
22135 dwarf2out_note_section_used ();
22136 call_site_count = 0;
22137 tail_call_site_count = 0;
22140 /* Output a label to mark the beginning of a source code line entry
22141 and record information relating to this source line, in
22142 'line_info_table' for later output of the .debug_line section. */
22144 static void
22145 dwarf2out_source_line (unsigned int line, const char *filename,
22146 int discriminator, bool is_stmt)
22148 static bool last_is_stmt = true;
22150 if (debug_info_level >= DINFO_LEVEL_NORMAL
22151 && line != 0)
22153 int file_num = maybe_emit_file (lookup_filename (filename));
22155 switch_to_section (current_function_section ());
22157 /* If requested, emit something human-readable. */
22158 if (flag_debug_asm)
22159 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
22160 filename, line);
22162 if (DWARF2_ASM_LINE_DEBUG_INFO)
22164 /* Emit the .loc directive understood by GNU as. */
22165 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22166 if (is_stmt != last_is_stmt)
22168 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22169 last_is_stmt = is_stmt;
22171 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22172 fprintf (asm_out_file, " discriminator %d", discriminator);
22173 fputc ('\n', asm_out_file);
22175 /* Indicate that line number info exists. */
22176 line_info_table_in_use++;
22178 else if (function_section (current_function_decl) != text_section)
22180 dw_separate_line_info_ref line_info;
22181 targetm.asm_out.internal_label (asm_out_file,
22182 SEPARATE_LINE_CODE_LABEL,
22183 separate_line_info_table_in_use);
22185 /* Expand the line info table if necessary. */
22186 if (separate_line_info_table_in_use
22187 == separate_line_info_table_allocated)
22189 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22190 separate_line_info_table
22191 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22192 separate_line_info_table,
22193 separate_line_info_table_allocated);
22194 memset (separate_line_info_table
22195 + separate_line_info_table_in_use,
22197 (LINE_INFO_TABLE_INCREMENT
22198 * sizeof (dw_separate_line_info_entry)));
22201 /* Add the new entry at the end of the line_info_table. */
22202 line_info
22203 = &separate_line_info_table[separate_line_info_table_in_use++];
22204 line_info->dw_file_num = file_num;
22205 line_info->dw_line_num = line;
22206 line_info->function = current_function_funcdef_no;
22208 else
22210 dw_line_info_ref line_info;
22212 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22213 line_info_table_in_use);
22215 /* Expand the line info table if necessary. */
22216 if (line_info_table_in_use == line_info_table_allocated)
22218 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22219 line_info_table
22220 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22221 line_info_table_allocated);
22222 memset (line_info_table + line_info_table_in_use, 0,
22223 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22226 /* Add the new entry at the end of the line_info_table. */
22227 line_info = &line_info_table[line_info_table_in_use++];
22228 line_info->dw_file_num = file_num;
22229 line_info->dw_line_num = line;
22234 /* Record the beginning of a new source file. */
22236 static void
22237 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22239 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22241 /* Record the beginning of the file for break_out_includes. */
22242 dw_die_ref bincl_die;
22244 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22245 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22248 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22250 macinfo_entry e;
22251 e.code = DW_MACINFO_start_file;
22252 e.lineno = lineno;
22253 e.info = xstrdup (filename);
22254 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22258 /* Record the end of a source file. */
22260 static void
22261 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22263 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22264 /* Record the end of the file for break_out_includes. */
22265 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22267 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22269 macinfo_entry e;
22270 e.code = DW_MACINFO_end_file;
22271 e.lineno = lineno;
22272 e.info = NULL;
22273 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22277 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22278 the tail part of the directive line, i.e. the part which is past the
22279 initial whitespace, #, whitespace, directive-name, whitespace part. */
22281 static void
22282 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22283 const char *buffer ATTRIBUTE_UNUSED)
22285 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22287 macinfo_entry e;
22288 e.code = DW_MACINFO_define;
22289 e.lineno = lineno;
22290 e.info = xstrdup (buffer);;
22291 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22295 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22296 the tail part of the directive line, i.e. the part which is past the
22297 initial whitespace, #, whitespace, directive-name, whitespace part. */
22299 static void
22300 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22301 const char *buffer ATTRIBUTE_UNUSED)
22303 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22305 macinfo_entry e;
22306 e.code = DW_MACINFO_undef;
22307 e.lineno = lineno;
22308 e.info = xstrdup (buffer);;
22309 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22313 static void
22314 output_macinfo (void)
22316 unsigned i;
22317 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22318 macinfo_entry *ref;
22320 if (! length)
22321 return;
22323 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22325 switch (ref->code)
22327 case DW_MACINFO_start_file:
22329 int file_num = maybe_emit_file (lookup_filename (ref->info));
22330 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22331 dw2_asm_output_data_uleb128
22332 (ref->lineno, "Included from line number %lu",
22333 (unsigned long)ref->lineno);
22334 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22336 break;
22337 case DW_MACINFO_end_file:
22338 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22339 break;
22340 case DW_MACINFO_define:
22341 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22342 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22343 (unsigned long)ref->lineno);
22344 dw2_asm_output_nstring (ref->info, -1, "The macro");
22345 break;
22346 case DW_MACINFO_undef:
22347 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22348 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22349 (unsigned long)ref->lineno);
22350 dw2_asm_output_nstring (ref->info, -1, "The macro");
22351 break;
22352 default:
22353 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22354 ASM_COMMENT_START, (unsigned long)ref->code);
22355 break;
22360 /* Set up for Dwarf output at the start of compilation. */
22362 static void
22363 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22365 /* Allocate the file_table. */
22366 file_table = htab_create_ggc (50, file_table_hash,
22367 file_table_eq, NULL);
22369 /* Allocate the decl_die_table. */
22370 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22371 decl_die_table_eq, NULL);
22373 /* Allocate the decl_loc_table. */
22374 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22375 decl_loc_table_eq, NULL);
22377 /* Allocate the cached_dw_loc_list_table. */
22378 cached_dw_loc_list_table
22379 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22380 cached_dw_loc_list_table_eq, NULL);
22382 /* Allocate the initial hunk of the decl_scope_table. */
22383 decl_scope_table = VEC_alloc (tree, gc, 256);
22385 /* Allocate the initial hunk of the abbrev_die_table. */
22386 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22387 (ABBREV_DIE_TABLE_INCREMENT);
22388 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22389 /* Zero-th entry is allocated, but unused. */
22390 abbrev_die_table_in_use = 1;
22392 /* Allocate the initial hunk of the line_info_table. */
22393 line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22394 (LINE_INFO_TABLE_INCREMENT);
22395 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22397 /* Zero-th entry is allocated, but unused. */
22398 line_info_table_in_use = 1;
22400 /* Allocate the pubtypes and pubnames vectors. */
22401 pubname_table = VEC_alloc (pubname_entry, gc, 32);
22402 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22404 incomplete_types = VEC_alloc (tree, gc, 64);
22406 used_rtx_array = VEC_alloc (rtx, gc, 32);
22408 debug_info_section = get_section (DEBUG_INFO_SECTION,
22409 SECTION_DEBUG, NULL);
22410 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22411 SECTION_DEBUG, NULL);
22412 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22413 SECTION_DEBUG, NULL);
22414 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22415 SECTION_DEBUG, NULL);
22416 debug_line_section = get_section (DEBUG_LINE_SECTION,
22417 SECTION_DEBUG, NULL);
22418 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22419 SECTION_DEBUG, NULL);
22420 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22421 SECTION_DEBUG, NULL);
22422 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22423 SECTION_DEBUG, NULL);
22424 debug_str_section = get_section (DEBUG_STR_SECTION,
22425 DEBUG_STR_SECTION_FLAGS, NULL);
22426 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22427 SECTION_DEBUG, NULL);
22428 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22429 SECTION_DEBUG, NULL);
22431 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22432 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22433 DEBUG_ABBREV_SECTION_LABEL, 0);
22434 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22435 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22436 COLD_TEXT_SECTION_LABEL, 0);
22437 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22439 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22440 DEBUG_INFO_SECTION_LABEL, 0);
22441 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22442 DEBUG_LINE_SECTION_LABEL, 0);
22443 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22444 DEBUG_RANGES_SECTION_LABEL, 0);
22445 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22446 DEBUG_MACINFO_SECTION_LABEL, 0);
22448 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22449 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22451 switch_to_section (text_section);
22452 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22455 /* Called before cgraph_optimize starts outputtting functions, variables
22456 and toplevel asms into assembly. */
22458 static void
22459 dwarf2out_assembly_start (void)
22461 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22462 && dwarf2out_do_cfi_asm ()
22463 && (!(flag_unwind_tables || flag_exceptions)
22464 || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22465 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22468 /* A helper function for dwarf2out_finish called through
22469 htab_traverse. Emit one queued .debug_str string. */
22471 static int
22472 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22474 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22476 if (node->label && node->refcount)
22478 switch_to_section (debug_str_section);
22479 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22480 assemble_string (node->str, strlen (node->str) + 1);
22483 return 1;
22486 #if ENABLE_ASSERT_CHECKING
22487 /* Verify that all marks are clear. */
22489 static void
22490 verify_marks_clear (dw_die_ref die)
22492 dw_die_ref c;
22494 gcc_assert (! die->die_mark);
22495 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22497 #endif /* ENABLE_ASSERT_CHECKING */
22499 /* Clear the marks for a die and its children.
22500 Be cool if the mark isn't set. */
22502 static void
22503 prune_unmark_dies (dw_die_ref die)
22505 dw_die_ref c;
22507 if (die->die_mark)
22508 die->die_mark = 0;
22509 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22512 /* Given DIE that we're marking as used, find any other dies
22513 it references as attributes and mark them as used. */
22515 static void
22516 prune_unused_types_walk_attribs (dw_die_ref die)
22518 dw_attr_ref a;
22519 unsigned ix;
22521 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22523 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22525 /* A reference to another DIE.
22526 Make sure that it will get emitted.
22527 If it was broken out into a comdat group, don't follow it. */
22528 if (! use_debug_types
22529 || a->dw_attr == DW_AT_specification
22530 || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22531 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22533 /* Set the string's refcount to 0 so that prune_unused_types_mark
22534 accounts properly for it. */
22535 if (AT_class (a) == dw_val_class_str)
22536 a->dw_attr_val.v.val_str->refcount = 0;
22540 /* Mark the generic parameters and arguments children DIEs of DIE. */
22542 static void
22543 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22545 dw_die_ref c;
22547 if (die == NULL || die->die_child == NULL)
22548 return;
22549 c = die->die_child;
22552 switch (c->die_tag)
22554 case DW_TAG_template_type_param:
22555 case DW_TAG_template_value_param:
22556 case DW_TAG_GNU_template_template_param:
22557 case DW_TAG_GNU_template_parameter_pack:
22558 prune_unused_types_mark (c, 1);
22559 break;
22560 default:
22561 break;
22563 c = c->die_sib;
22564 } while (c && c != die->die_child);
22567 /* Mark DIE as being used. If DOKIDS is true, then walk down
22568 to DIE's children. */
22570 static void
22571 prune_unused_types_mark (dw_die_ref die, int dokids)
22573 dw_die_ref c;
22575 if (die->die_mark == 0)
22577 /* We haven't done this node yet. Mark it as used. */
22578 die->die_mark = 1;
22579 /* If this is the DIE of a generic type instantiation,
22580 mark the children DIEs that describe its generic parms and
22581 args. */
22582 prune_unused_types_mark_generic_parms_dies (die);
22584 /* We also have to mark its parents as used.
22585 (But we don't want to mark our parents' kids due to this.) */
22586 if (die->die_parent)
22587 prune_unused_types_mark (die->die_parent, 0);
22589 /* Mark any referenced nodes. */
22590 prune_unused_types_walk_attribs (die);
22592 /* If this node is a specification,
22593 also mark the definition, if it exists. */
22594 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22595 prune_unused_types_mark (die->die_definition, 1);
22598 if (dokids && die->die_mark != 2)
22600 /* We need to walk the children, but haven't done so yet.
22601 Remember that we've walked the kids. */
22602 die->die_mark = 2;
22604 /* If this is an array type, we need to make sure our
22605 kids get marked, even if they're types. If we're
22606 breaking out types into comdat sections, do this
22607 for all type definitions. */
22608 if (die->die_tag == DW_TAG_array_type
22609 || (use_debug_types
22610 && is_type_die (die) && ! is_declaration_die (die)))
22611 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22612 else
22613 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22617 /* For local classes, look if any static member functions were emitted
22618 and if so, mark them. */
22620 static void
22621 prune_unused_types_walk_local_classes (dw_die_ref die)
22623 dw_die_ref c;
22625 if (die->die_mark == 2)
22626 return;
22628 switch (die->die_tag)
22630 case DW_TAG_structure_type:
22631 case DW_TAG_union_type:
22632 case DW_TAG_class_type:
22633 break;
22635 case DW_TAG_subprogram:
22636 if (!get_AT_flag (die, DW_AT_declaration)
22637 || die->die_definition != NULL)
22638 prune_unused_types_mark (die, 1);
22639 return;
22641 default:
22642 return;
22645 /* Mark children. */
22646 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22649 /* Walk the tree DIE and mark types that we actually use. */
22651 static void
22652 prune_unused_types_walk (dw_die_ref die)
22654 dw_die_ref c;
22656 /* Don't do anything if this node is already marked and
22657 children have been marked as well. */
22658 if (die->die_mark == 2)
22659 return;
22661 switch (die->die_tag)
22663 case DW_TAG_structure_type:
22664 case DW_TAG_union_type:
22665 case DW_TAG_class_type:
22666 if (die->die_perennial_p)
22667 break;
22669 for (c = die->die_parent; c; c = c->die_parent)
22670 if (c->die_tag == DW_TAG_subprogram)
22671 break;
22673 /* Finding used static member functions inside of classes
22674 is needed just for local classes, because for other classes
22675 static member function DIEs with DW_AT_specification
22676 are emitted outside of the DW_TAG_*_type. If we ever change
22677 it, we'd need to call this even for non-local classes. */
22678 if (c)
22679 prune_unused_types_walk_local_classes (die);
22681 /* It's a type node --- don't mark it. */
22682 return;
22684 case DW_TAG_const_type:
22685 case DW_TAG_packed_type:
22686 case DW_TAG_pointer_type:
22687 case DW_TAG_reference_type:
22688 case DW_TAG_rvalue_reference_type:
22689 case DW_TAG_volatile_type:
22690 case DW_TAG_typedef:
22691 case DW_TAG_array_type:
22692 case DW_TAG_interface_type:
22693 case DW_TAG_friend:
22694 case DW_TAG_variant_part:
22695 case DW_TAG_enumeration_type:
22696 case DW_TAG_subroutine_type:
22697 case DW_TAG_string_type:
22698 case DW_TAG_set_type:
22699 case DW_TAG_subrange_type:
22700 case DW_TAG_ptr_to_member_type:
22701 case DW_TAG_file_type:
22702 if (die->die_perennial_p)
22703 break;
22705 /* It's a type node --- don't mark it. */
22706 return;
22708 default:
22709 /* Mark everything else. */
22710 break;
22713 if (die->die_mark == 0)
22715 die->die_mark = 1;
22717 /* Now, mark any dies referenced from here. */
22718 prune_unused_types_walk_attribs (die);
22721 die->die_mark = 2;
22723 /* Mark children. */
22724 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22727 /* Increment the string counts on strings referred to from DIE's
22728 attributes. */
22730 static void
22731 prune_unused_types_update_strings (dw_die_ref die)
22733 dw_attr_ref a;
22734 unsigned ix;
22736 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22737 if (AT_class (a) == dw_val_class_str)
22739 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22740 s->refcount++;
22741 /* Avoid unnecessarily putting strings that are used less than
22742 twice in the hash table. */
22743 if (s->refcount
22744 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22746 void ** slot;
22747 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22748 htab_hash_string (s->str),
22749 INSERT);
22750 gcc_assert (*slot == NULL);
22751 *slot = s;
22756 /* Remove from the tree DIE any dies that aren't marked. */
22758 static void
22759 prune_unused_types_prune (dw_die_ref die)
22761 dw_die_ref c;
22763 gcc_assert (die->die_mark);
22764 prune_unused_types_update_strings (die);
22766 if (! die->die_child)
22767 return;
22769 c = die->die_child;
22770 do {
22771 dw_die_ref prev = c;
22772 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22773 if (c == die->die_child)
22775 /* No marked children between 'prev' and the end of the list. */
22776 if (prev == c)
22777 /* No marked children at all. */
22778 die->die_child = NULL;
22779 else
22781 prev->die_sib = c->die_sib;
22782 die->die_child = prev;
22784 return;
22787 if (c != prev->die_sib)
22788 prev->die_sib = c;
22789 prune_unused_types_prune (c);
22790 } while (c != die->die_child);
22793 /* A helper function for dwarf2out_finish called through
22794 htab_traverse. Clear .debug_str strings that we haven't already
22795 decided to emit. */
22797 static int
22798 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22800 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22802 if (!node->label || !node->refcount)
22803 htab_clear_slot (debug_str_hash, h);
22805 return 1;
22808 /* Remove dies representing declarations that we never use. */
22810 static void
22811 prune_unused_types (void)
22813 unsigned int i;
22814 limbo_die_node *node;
22815 comdat_type_node *ctnode;
22816 pubname_ref pub;
22818 #if ENABLE_ASSERT_CHECKING
22819 /* All the marks should already be clear. */
22820 verify_marks_clear (comp_unit_die ());
22821 for (node = limbo_die_list; node; node = node->next)
22822 verify_marks_clear (node->die);
22823 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22824 verify_marks_clear (ctnode->root_die);
22825 #endif /* ENABLE_ASSERT_CHECKING */
22827 /* Mark types that are used in global variables. */
22828 premark_types_used_by_global_vars ();
22830 /* Set the mark on nodes that are actually used. */
22831 prune_unused_types_walk (comp_unit_die ());
22832 for (node = limbo_die_list; node; node = node->next)
22833 prune_unused_types_walk (node->die);
22834 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22836 prune_unused_types_walk (ctnode->root_die);
22837 prune_unused_types_mark (ctnode->type_die, 1);
22840 /* Also set the mark on nodes referenced from the
22841 pubname_table. */
22842 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22843 prune_unused_types_mark (pub->die, 1);
22845 /* Get rid of nodes that aren't marked; and update the string counts. */
22846 if (debug_str_hash && debug_str_hash_forced)
22847 htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22848 else if (debug_str_hash)
22849 htab_empty (debug_str_hash);
22850 prune_unused_types_prune (comp_unit_die ());
22851 for (node = limbo_die_list; node; node = node->next)
22852 prune_unused_types_prune (node->die);
22853 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22854 prune_unused_types_prune (ctnode->root_die);
22856 /* Leave the marks clear. */
22857 prune_unmark_dies (comp_unit_die ());
22858 for (node = limbo_die_list; node; node = node->next)
22859 prune_unmark_dies (node->die);
22860 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22861 prune_unmark_dies (ctnode->root_die);
22864 /* Set the parameter to true if there are any relative pathnames in
22865 the file table. */
22866 static int
22867 file_table_relative_p (void ** slot, void *param)
22869 bool *p = (bool *) param;
22870 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22871 if (!IS_ABSOLUTE_PATH (d->filename))
22873 *p = true;
22874 return 0;
22876 return 1;
22879 /* Routines to manipulate hash table of comdat type units. */
22881 static hashval_t
22882 htab_ct_hash (const void *of)
22884 hashval_t h;
22885 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22887 memcpy (&h, type_node->signature, sizeof (h));
22888 return h;
22891 static int
22892 htab_ct_eq (const void *of1, const void *of2)
22894 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22895 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22897 return (! memcmp (type_node_1->signature, type_node_2->signature,
22898 DWARF_TYPE_SIGNATURE_SIZE));
22901 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22902 to the location it would have been added, should we know its
22903 DECL_ASSEMBLER_NAME when we added other attributes. This will
22904 probably improve compactness of debug info, removing equivalent
22905 abbrevs, and hide any differences caused by deferring the
22906 computation of the assembler name, triggered by e.g. PCH. */
22908 static inline void
22909 move_linkage_attr (dw_die_ref die)
22911 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22912 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22914 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22915 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22917 while (--ix > 0)
22919 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22921 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22922 break;
22925 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22927 VEC_pop (dw_attr_node, die->die_attr);
22928 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22932 /* Helper function for resolve_addr, attempt to resolve
22933 one CONST_STRING, return non-zero if not successful. Similarly verify that
22934 SYMBOL_REFs refer to variables emitted in the current CU. */
22936 static int
22937 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22939 rtx rtl = *addr;
22941 if (GET_CODE (rtl) == CONST_STRING)
22943 size_t len = strlen (XSTR (rtl, 0)) + 1;
22944 tree t = build_string (len, XSTR (rtl, 0));
22945 tree tlen = build_int_cst (NULL_TREE, len - 1);
22946 TREE_TYPE (t)
22947 = build_array_type (char_type_node, build_index_type (tlen));
22948 rtl = lookup_constant_def (t);
22949 if (!rtl || !MEM_P (rtl))
22950 return 1;
22951 rtl = XEXP (rtl, 0);
22952 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22953 *addr = rtl;
22954 return 0;
22957 if (GET_CODE (rtl) == SYMBOL_REF
22958 && SYMBOL_REF_DECL (rtl))
22960 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22962 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22963 return 1;
22965 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22966 return 1;
22969 if (GET_CODE (rtl) == CONST
22970 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22971 return 1;
22973 return 0;
22976 /* Helper function for resolve_addr, handle one location
22977 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22978 the location list couldn't be resolved. */
22980 static bool
22981 resolve_addr_in_expr (dw_loc_descr_ref loc)
22983 for (; loc; loc = loc->dw_loc_next)
22984 if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22985 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22986 || (loc->dw_loc_opc == DW_OP_implicit_value
22987 && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22988 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22989 return false;
22990 else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22991 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22993 dw_die_ref ref
22994 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22995 if (ref == NULL)
22996 return false;
22997 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22998 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22999 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23001 return true;
23004 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23005 an address in .rodata section if the string literal is emitted there,
23006 or remove the containing location list or replace DW_AT_const_value
23007 with DW_AT_location and empty location expression, if it isn't found
23008 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23009 to something that has been emitted in the current CU. */
23011 static void
23012 resolve_addr (dw_die_ref die)
23014 dw_die_ref c;
23015 dw_attr_ref a;
23016 dw_loc_list_ref *curr, *start, loc;
23017 unsigned ix;
23019 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23020 switch (AT_class (a))
23022 case dw_val_class_loc_list:
23023 start = curr = AT_loc_list_ptr (a);
23024 loc = *curr;
23025 gcc_assert (loc);
23026 /* The same list can be referenced more than once. See if we have
23027 already recorded the result from a previous pass. */
23028 if (loc->replaced)
23029 *curr = loc->dw_loc_next;
23030 else if (!loc->resolved_addr)
23032 /* As things stand, we do not expect or allow one die to
23033 reference a suffix of another die's location list chain.
23034 References must be identical or completely separate.
23035 There is therefore no need to cache the result of this
23036 pass on any list other than the first; doing so
23037 would lead to unnecessary writes. */
23038 while (*curr)
23040 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23041 if (!resolve_addr_in_expr ((*curr)->expr))
23043 dw_loc_list_ref next = (*curr)->dw_loc_next;
23044 if (next && (*curr)->ll_symbol)
23046 gcc_assert (!next->ll_symbol);
23047 next->ll_symbol = (*curr)->ll_symbol;
23049 *curr = next;
23051 else
23052 curr = &(*curr)->dw_loc_next;
23054 if (loc == *start)
23055 loc->resolved_addr = 1;
23056 else
23058 loc->replaced = 1;
23059 loc->dw_loc_next = *start;
23062 if (!*start)
23064 remove_AT (die, a->dw_attr);
23065 ix--;
23067 break;
23068 case dw_val_class_loc:
23069 if (!resolve_addr_in_expr (AT_loc (a)))
23071 remove_AT (die, a->dw_attr);
23072 ix--;
23074 break;
23075 case dw_val_class_addr:
23076 if (a->dw_attr == DW_AT_const_value
23077 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23079 remove_AT (die, a->dw_attr);
23080 ix--;
23082 if (die->die_tag == DW_TAG_GNU_call_site
23083 && a->dw_attr == DW_AT_abstract_origin)
23085 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23086 dw_die_ref tdie = lookup_decl_die (tdecl);
23087 if (tdie == NULL && DECL_EXTERNAL (tdecl))
23089 force_decl_die (tdecl);
23090 tdie = lookup_decl_die (tdecl);
23092 if (tdie)
23094 a->dw_attr_val.val_class = dw_val_class_die_ref;
23095 a->dw_attr_val.v.val_die_ref.die = tdie;
23096 a->dw_attr_val.v.val_die_ref.external = 0;
23098 else
23100 remove_AT (die, a->dw_attr);
23101 ix--;
23104 break;
23105 default:
23106 break;
23109 FOR_EACH_CHILD (die, c, resolve_addr (c));
23112 /* Helper routines for optimize_location_lists.
23113 This pass tries to share identical local lists in .debug_loc
23114 section. */
23116 /* Iteratively hash operands of LOC opcode. */
23118 static inline hashval_t
23119 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23121 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23122 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23124 switch (loc->dw_loc_opc)
23126 case DW_OP_const4u:
23127 case DW_OP_const8u:
23128 if (loc->dtprel)
23129 goto hash_addr;
23130 /* FALLTHRU */
23131 case DW_OP_const1u:
23132 case DW_OP_const1s:
23133 case DW_OP_const2u:
23134 case DW_OP_const2s:
23135 case DW_OP_const4s:
23136 case DW_OP_const8s:
23137 case DW_OP_constu:
23138 case DW_OP_consts:
23139 case DW_OP_pick:
23140 case DW_OP_plus_uconst:
23141 case DW_OP_breg0:
23142 case DW_OP_breg1:
23143 case DW_OP_breg2:
23144 case DW_OP_breg3:
23145 case DW_OP_breg4:
23146 case DW_OP_breg5:
23147 case DW_OP_breg6:
23148 case DW_OP_breg7:
23149 case DW_OP_breg8:
23150 case DW_OP_breg9:
23151 case DW_OP_breg10:
23152 case DW_OP_breg11:
23153 case DW_OP_breg12:
23154 case DW_OP_breg13:
23155 case DW_OP_breg14:
23156 case DW_OP_breg15:
23157 case DW_OP_breg16:
23158 case DW_OP_breg17:
23159 case DW_OP_breg18:
23160 case DW_OP_breg19:
23161 case DW_OP_breg20:
23162 case DW_OP_breg21:
23163 case DW_OP_breg22:
23164 case DW_OP_breg23:
23165 case DW_OP_breg24:
23166 case DW_OP_breg25:
23167 case DW_OP_breg26:
23168 case DW_OP_breg27:
23169 case DW_OP_breg28:
23170 case DW_OP_breg29:
23171 case DW_OP_breg30:
23172 case DW_OP_breg31:
23173 case DW_OP_regx:
23174 case DW_OP_fbreg:
23175 case DW_OP_piece:
23176 case DW_OP_deref_size:
23177 case DW_OP_xderef_size:
23178 hash = iterative_hash_object (val1->v.val_int, hash);
23179 break;
23180 case DW_OP_skip:
23181 case DW_OP_bra:
23183 int offset;
23185 gcc_assert (val1->val_class == dw_val_class_loc);
23186 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23187 hash = iterative_hash_object (offset, hash);
23189 break;
23190 case DW_OP_implicit_value:
23191 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23192 switch (val2->val_class)
23194 case dw_val_class_const:
23195 hash = iterative_hash_object (val2->v.val_int, hash);
23196 break;
23197 case dw_val_class_vec:
23199 unsigned int elt_size = val2->v.val_vec.elt_size;
23200 unsigned int len = val2->v.val_vec.length;
23202 hash = iterative_hash_object (elt_size, hash);
23203 hash = iterative_hash_object (len, hash);
23204 hash = iterative_hash (val2->v.val_vec.array,
23205 len * elt_size, hash);
23207 break;
23208 case dw_val_class_const_double:
23209 hash = iterative_hash_object (val2->v.val_double.low, hash);
23210 hash = iterative_hash_object (val2->v.val_double.high, hash);
23211 break;
23212 case dw_val_class_addr:
23213 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23214 break;
23215 default:
23216 gcc_unreachable ();
23218 break;
23219 case DW_OP_bregx:
23220 case DW_OP_bit_piece:
23221 hash = iterative_hash_object (val1->v.val_int, hash);
23222 hash = iterative_hash_object (val2->v.val_int, hash);
23223 break;
23224 case DW_OP_addr:
23225 hash_addr:
23226 if (loc->dtprel)
23228 unsigned char dtprel = 0xd1;
23229 hash = iterative_hash_object (dtprel, hash);
23231 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23232 break;
23233 case DW_OP_GNU_implicit_pointer:
23234 hash = iterative_hash_object (val2->v.val_int, hash);
23235 break;
23236 case DW_OP_GNU_entry_value:
23237 hash = hash_loc_operands (val1->v.val_loc, hash);
23238 break;
23240 default:
23241 /* Other codes have no operands. */
23242 break;
23244 return hash;
23247 /* Iteratively hash the whole DWARF location expression LOC. */
23249 static inline hashval_t
23250 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23252 dw_loc_descr_ref l;
23253 bool sizes_computed = false;
23254 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23255 size_of_locs (loc);
23257 for (l = loc; l != NULL; l = l->dw_loc_next)
23259 enum dwarf_location_atom opc = l->dw_loc_opc;
23260 hash = iterative_hash_object (opc, hash);
23261 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23263 size_of_locs (loc);
23264 sizes_computed = true;
23266 hash = hash_loc_operands (l, hash);
23268 return hash;
23271 /* Compute hash of the whole location list LIST_HEAD. */
23273 static inline void
23274 hash_loc_list (dw_loc_list_ref list_head)
23276 dw_loc_list_ref curr = list_head;
23277 hashval_t hash = 0;
23279 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23281 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23282 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23283 if (curr->section)
23284 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23285 hash);
23286 hash = hash_locs (curr->expr, hash);
23288 list_head->hash = hash;
23291 /* Return true if X and Y opcodes have the same operands. */
23293 static inline bool
23294 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23296 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23297 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23298 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23299 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23301 switch (x->dw_loc_opc)
23303 case DW_OP_const4u:
23304 case DW_OP_const8u:
23305 if (x->dtprel)
23306 goto hash_addr;
23307 /* FALLTHRU */
23308 case DW_OP_const1u:
23309 case DW_OP_const1s:
23310 case DW_OP_const2u:
23311 case DW_OP_const2s:
23312 case DW_OP_const4s:
23313 case DW_OP_const8s:
23314 case DW_OP_constu:
23315 case DW_OP_consts:
23316 case DW_OP_pick:
23317 case DW_OP_plus_uconst:
23318 case DW_OP_breg0:
23319 case DW_OP_breg1:
23320 case DW_OP_breg2:
23321 case DW_OP_breg3:
23322 case DW_OP_breg4:
23323 case DW_OP_breg5:
23324 case DW_OP_breg6:
23325 case DW_OP_breg7:
23326 case DW_OP_breg8:
23327 case DW_OP_breg9:
23328 case DW_OP_breg10:
23329 case DW_OP_breg11:
23330 case DW_OP_breg12:
23331 case DW_OP_breg13:
23332 case DW_OP_breg14:
23333 case DW_OP_breg15:
23334 case DW_OP_breg16:
23335 case DW_OP_breg17:
23336 case DW_OP_breg18:
23337 case DW_OP_breg19:
23338 case DW_OP_breg20:
23339 case DW_OP_breg21:
23340 case DW_OP_breg22:
23341 case DW_OP_breg23:
23342 case DW_OP_breg24:
23343 case DW_OP_breg25:
23344 case DW_OP_breg26:
23345 case DW_OP_breg27:
23346 case DW_OP_breg28:
23347 case DW_OP_breg29:
23348 case DW_OP_breg30:
23349 case DW_OP_breg31:
23350 case DW_OP_regx:
23351 case DW_OP_fbreg:
23352 case DW_OP_piece:
23353 case DW_OP_deref_size:
23354 case DW_OP_xderef_size:
23355 return valx1->v.val_int == valy1->v.val_int;
23356 case DW_OP_skip:
23357 case DW_OP_bra:
23358 gcc_assert (valx1->val_class == dw_val_class_loc
23359 && valy1->val_class == dw_val_class_loc
23360 && x->dw_loc_addr == y->dw_loc_addr);
23361 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23362 case DW_OP_implicit_value:
23363 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23364 || valx2->val_class != valy2->val_class)
23365 return false;
23366 switch (valx2->val_class)
23368 case dw_val_class_const:
23369 return valx2->v.val_int == valy2->v.val_int;
23370 case dw_val_class_vec:
23371 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23372 && valx2->v.val_vec.length == valy2->v.val_vec.length
23373 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23374 valx2->v.val_vec.elt_size
23375 * valx2->v.val_vec.length) == 0;
23376 case dw_val_class_const_double:
23377 return valx2->v.val_double.low == valy2->v.val_double.low
23378 && valx2->v.val_double.high == valy2->v.val_double.high;
23379 case dw_val_class_addr:
23380 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23381 default:
23382 gcc_unreachable ();
23384 case DW_OP_bregx:
23385 case DW_OP_bit_piece:
23386 return valx1->v.val_int == valy1->v.val_int
23387 && valx2->v.val_int == valy2->v.val_int;
23388 case DW_OP_addr:
23389 hash_addr:
23390 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23391 case DW_OP_GNU_implicit_pointer:
23392 return valx1->val_class == dw_val_class_die_ref
23393 && valx1->val_class == valy1->val_class
23394 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23395 && valx2->v.val_int == valy2->v.val_int;
23396 case DW_OP_GNU_entry_value:
23397 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23398 default:
23399 /* Other codes have no operands. */
23400 return true;
23404 /* Return true if DWARF location expressions X and Y are the same. */
23406 static inline bool
23407 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23409 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23410 if (x->dw_loc_opc != y->dw_loc_opc
23411 || x->dtprel != y->dtprel
23412 || !compare_loc_operands (x, y))
23413 break;
23414 return x == NULL && y == NULL;
23417 /* Return precomputed hash of location list X. */
23419 static hashval_t
23420 loc_list_hash (const void *x)
23422 return ((const struct dw_loc_list_struct *) x)->hash;
23425 /* Return 1 if location lists X and Y are the same. */
23427 static int
23428 loc_list_eq (const void *x, const void *y)
23430 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23431 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23432 if (a == b)
23433 return 1;
23434 if (a->hash != b->hash)
23435 return 0;
23436 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23437 if (strcmp (a->begin, b->begin) != 0
23438 || strcmp (a->end, b->end) != 0
23439 || (a->section == NULL) != (b->section == NULL)
23440 || (a->section && strcmp (a->section, b->section) != 0)
23441 || !compare_locs (a->expr, b->expr))
23442 break;
23443 return a == NULL && b == NULL;
23446 /* Recursively optimize location lists referenced from DIE
23447 children and share them whenever possible. */
23449 static void
23450 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23452 dw_die_ref c;
23453 dw_attr_ref a;
23454 unsigned ix;
23455 void **slot;
23457 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23458 if (AT_class (a) == dw_val_class_loc_list)
23460 dw_loc_list_ref list = AT_loc_list (a);
23461 /* TODO: perform some optimizations here, before hashing
23462 it and storing into the hash table. */
23463 hash_loc_list (list);
23464 slot = htab_find_slot_with_hash (htab, list, list->hash,
23465 INSERT);
23466 if (*slot == NULL)
23467 *slot = (void *) list;
23468 else
23469 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23472 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23475 /* Optimize location lists referenced from DIE
23476 children and share them whenever possible. */
23478 static void
23479 optimize_location_lists (dw_die_ref die)
23481 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23482 optimize_location_lists_1 (die, htab);
23483 htab_delete (htab);
23486 /* Output stuff that dwarf requires at the end of every file,
23487 and generate the DWARF-2 debugging info. */
23489 static void
23490 dwarf2out_finish (const char *filename)
23492 limbo_die_node *node, *next_node;
23493 comdat_type_node *ctnode;
23494 htab_t comdat_type_table;
23495 unsigned int i;
23497 gen_scheduled_generic_parms_dies ();
23498 gen_remaining_tmpl_value_param_die_attribute ();
23500 /* Add the name for the main input file now. We delayed this from
23501 dwarf2out_init to avoid complications with PCH. */
23502 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23503 if (!IS_ABSOLUTE_PATH (filename))
23504 add_comp_dir_attribute (comp_unit_die ());
23505 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23507 bool p = false;
23508 htab_traverse (file_table, file_table_relative_p, &p);
23509 if (p)
23510 add_comp_dir_attribute (comp_unit_die ());
23513 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23515 add_location_or_const_value_attribute (
23516 VEC_index (deferred_locations, deferred_locations_list, i)->die,
23517 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23518 false,
23519 DW_AT_location);
23522 /* Traverse the limbo die list, and add parent/child links. The only
23523 dies without parents that should be here are concrete instances of
23524 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23525 For concrete instances, we can get the parent die from the abstract
23526 instance. */
23527 for (node = limbo_die_list; node; node = next_node)
23529 dw_die_ref die = node->die;
23530 next_node = node->next;
23532 if (die->die_parent == NULL)
23534 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23536 if (origin)
23537 add_child_die (origin->die_parent, die);
23538 else if (is_cu_die (die))
23540 else if (seen_error ())
23541 /* It's OK to be confused by errors in the input. */
23542 add_child_die (comp_unit_die (), die);
23543 else
23545 /* In certain situations, the lexical block containing a
23546 nested function can be optimized away, which results
23547 in the nested function die being orphaned. Likewise
23548 with the return type of that nested function. Force
23549 this to be a child of the containing function.
23551 It may happen that even the containing function got fully
23552 inlined and optimized out. In that case we are lost and
23553 assign the empty child. This should not be big issue as
23554 the function is likely unreachable too. */
23555 tree context = NULL_TREE;
23557 gcc_assert (node->created_for);
23559 if (DECL_P (node->created_for))
23560 context = DECL_CONTEXT (node->created_for);
23561 else if (TYPE_P (node->created_for))
23562 context = TYPE_CONTEXT (node->created_for);
23564 gcc_assert (context
23565 && (TREE_CODE (context) == FUNCTION_DECL
23566 || TREE_CODE (context) == NAMESPACE_DECL));
23568 origin = lookup_decl_die (context);
23569 if (origin)
23570 add_child_die (origin, die);
23571 else
23572 add_child_die (comp_unit_die (), die);
23577 limbo_die_list = NULL;
23579 resolve_addr (comp_unit_die ());
23581 for (node = deferred_asm_name; node; node = node->next)
23583 tree decl = node->created_for;
23584 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23586 add_linkage_attr (node->die, decl);
23587 move_linkage_attr (node->die);
23591 deferred_asm_name = NULL;
23593 /* Walk through the list of incomplete types again, trying once more to
23594 emit full debugging info for them. */
23595 retry_incomplete_types ();
23597 if (flag_eliminate_unused_debug_types)
23598 prune_unused_types ();
23600 /* Generate separate CUs for each of the include files we've seen.
23601 They will go into limbo_die_list. */
23602 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23603 break_out_includes (comp_unit_die ());
23605 /* Generate separate COMDAT sections for type DIEs. */
23606 if (use_debug_types)
23608 break_out_comdat_types (comp_unit_die ());
23610 /* Each new type_unit DIE was added to the limbo die list when created.
23611 Since these have all been added to comdat_type_list, clear the
23612 limbo die list. */
23613 limbo_die_list = NULL;
23615 /* For each new comdat type unit, copy declarations for incomplete
23616 types to make the new unit self-contained (i.e., no direct
23617 references to the main compile unit). */
23618 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23619 copy_decls_for_unworthy_types (ctnode->root_die);
23620 copy_decls_for_unworthy_types (comp_unit_die ());
23622 /* In the process of copying declarations from one unit to another,
23623 we may have left some declarations behind that are no longer
23624 referenced. Prune them. */
23625 prune_unused_types ();
23628 /* Traverse the DIE's and add add sibling attributes to those DIE's
23629 that have children. */
23630 add_sibling_attributes (comp_unit_die ());
23631 for (node = limbo_die_list; node; node = node->next)
23632 add_sibling_attributes (node->die);
23633 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23634 add_sibling_attributes (ctnode->root_die);
23636 /* Output a terminator label for the .text section. */
23637 switch_to_section (text_section);
23638 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23639 if (cold_text_section)
23641 switch_to_section (cold_text_section);
23642 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23645 /* We can only use the low/high_pc attributes if all of the code was
23646 in .text. */
23647 if (!have_multiple_function_sections
23648 || (dwarf_version < 3 && dwarf_strict))
23650 add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23651 add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23654 else
23656 unsigned fde_idx = 0;
23657 bool range_list_added = false;
23659 /* We need to give .debug_loc and .debug_ranges an appropriate
23660 "base address". Use zero so that these addresses become
23661 absolute. Historically, we've emitted the unexpected
23662 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23663 Emit both to give time for other tools to adapt. */
23664 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23665 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23667 if (text_section_used)
23668 add_ranges_by_labels (comp_unit_die (), text_section_label,
23669 text_end_label, &range_list_added);
23670 if (cold_text_section_used)
23671 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23672 cold_end_label, &range_list_added);
23674 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23676 dw_fde_ref fde = &fde_table[fde_idx];
23678 if (!fde->in_std_section)
23679 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23680 fde->dw_fde_end, &range_list_added);
23681 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23682 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
23683 fde->dw_fde_second_end, &range_list_added);
23686 if (range_list_added)
23687 add_ranges (NULL);
23690 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23691 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23692 debug_line_section_label);
23694 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23695 add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23697 if (have_location_lists)
23698 optimize_location_lists (comp_unit_die ());
23700 /* Output all of the compilation units. We put the main one last so that
23701 the offsets are available to output_pubnames. */
23702 for (node = limbo_die_list; node; node = node->next)
23703 output_comp_unit (node->die, 0);
23705 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23706 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23708 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23710 /* Don't output duplicate types. */
23711 if (*slot != HTAB_EMPTY_ENTRY)
23712 continue;
23714 /* Add a pointer to the line table for the main compilation unit
23715 so that the debugger can make sense of DW_AT_decl_file
23716 attributes. */
23717 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23718 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23719 debug_line_section_label);
23721 output_comdat_type_unit (ctnode);
23722 *slot = ctnode;
23724 htab_delete (comdat_type_table);
23726 /* Output the main compilation unit if non-empty or if .debug_macinfo
23727 will be emitted. */
23728 output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23730 /* Output the abbreviation table. */
23731 switch_to_section (debug_abbrev_section);
23732 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23733 output_abbrev_section ();
23735 /* Output location list section if necessary. */
23736 if (have_location_lists)
23738 /* Output the location lists info. */
23739 switch_to_section (debug_loc_section);
23740 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23741 DEBUG_LOC_SECTION_LABEL, 0);
23742 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23743 output_location_lists (comp_unit_die ());
23746 /* Output public names table if necessary. */
23747 if (!VEC_empty (pubname_entry, pubname_table))
23749 gcc_assert (info_section_emitted);
23750 switch_to_section (debug_pubnames_section);
23751 output_pubnames (pubname_table);
23754 /* Output public types table if necessary. */
23755 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23756 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23757 simply won't look for the section. */
23758 if (!VEC_empty (pubname_entry, pubtype_table))
23760 bool empty = false;
23762 if (flag_eliminate_unused_debug_types)
23764 /* The pubtypes table might be emptied by pruning unused items. */
23765 unsigned i;
23766 pubname_ref p;
23767 empty = true;
23768 FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23769 if (p->die->die_offset != 0)
23771 empty = false;
23772 break;
23775 if (!empty)
23777 gcc_assert (info_section_emitted);
23778 switch_to_section (debug_pubtypes_section);
23779 output_pubnames (pubtype_table);
23783 /* Output the address range information. We only put functions in the
23784 arange table, so don't write it out if we don't have any. */
23785 if (info_section_emitted)
23787 unsigned long aranges_length = size_of_aranges ();
23789 /* Empty .debug_aranges would contain just header and
23790 terminating 0,0. */
23791 if (aranges_length
23792 != (unsigned long) (DWARF_ARANGES_HEADER_SIZE
23793 + 2 * DWARF2_ADDR_SIZE))
23795 switch_to_section (debug_aranges_section);
23796 output_aranges (aranges_length);
23800 /* Output ranges section if necessary. */
23801 if (ranges_table_in_use)
23803 switch_to_section (debug_ranges_section);
23804 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23805 output_ranges ();
23808 /* Output the source line correspondence table. We must do this
23809 even if there is no line information. Otherwise, on an empty
23810 translation unit, we will generate a present, but empty,
23811 .debug_info section. IRIX 6.5 `nm' will then complain when
23812 examining the file. This is done late so that any filenames
23813 used by the debug_info section are marked as 'used'. */
23814 switch_to_section (debug_line_section);
23815 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23816 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23817 output_line_info ();
23819 /* Have to end the macro section. */
23820 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23822 switch_to_section (debug_macinfo_section);
23823 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23824 if (!VEC_empty (macinfo_entry, macinfo_table))
23825 output_macinfo ();
23826 dw2_asm_output_data (1, 0, "End compilation unit");
23829 /* If we emitted any DW_FORM_strp form attribute, output the string
23830 table too. */
23831 if (debug_str_hash)
23832 htab_traverse (debug_str_hash, output_indirect_string, NULL);
23835 #include "gt-dwarf2out.h"