Fix PR47707
[official-gcc.git] / gcc / dwarf2out.c
blob2309297d2e1ac30658de45b081b9a0f7f57eeba9
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Gary Funck (gary@intrepid.com).
6 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7 Extensively modified by Jason Merrill (jason@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 /* DWARF2 Abbreviation Glossary:
39 CFA = Canonical Frame Address
40 a fixed address on the stack which identifies a call frame.
41 We define it to be the value of SP just before the call insn.
42 The CFA register and offset, which may change during the course
43 of the function, are used to calculate its value at runtime.
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
51 DIE = Debugging Information Entry
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102 /* Define this macro to be a nonzero value if the directory specifications
103 which are output in the debug info should end with a separator. */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106 from generating indirect strings in DWARF2 debug information, for instance
107 if your target is stuck with an old version of GDB that is unable to
108 process them properly or uses VMS Debug. */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
115 /* ??? Poison these here until it can be done generically. They've been
116 totally replaced in this file; make sure it stays that way. */
117 #undef DWARF2_UNWIND_INFO
118 #undef DWARF2_FRAME_INFO
119 #if (GCC_VERSION >= 3000)
120 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
121 #endif
123 #ifndef INCOMING_RETURN_ADDR_RTX
124 #define INCOMING_RETURN_ADDR_RTX (gcc_unreachable (), NULL_RTX)
125 #endif
127 /* Map register numbers held in the call frame info that gcc has
128 collected using DWARF_FRAME_REGNUM to those that should be output in
129 .debug_frame and .eh_frame. */
130 #ifndef DWARF2_FRAME_REG_OUT
131 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
132 #endif
134 /* Save the result of dwarf2out_do_frame across PCH. */
135 static GTY(()) bool saved_do_cfi_asm = 0;
137 /* Decide whether we want to emit frame unwind information for the current
138 translation unit. */
141 dwarf2out_do_frame (void)
143 /* We want to emit correct CFA location expressions or lists, so we
144 have to return true if we're going to output debug info, even if
145 we're not going to output frame or unwind info. */
146 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
147 return true;
149 if (saved_do_cfi_asm)
150 return true;
152 if (targetm.debug_unwind_info () == UI_DWARF2)
153 return true;
155 if ((flag_unwind_tables || flag_exceptions)
156 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
157 return true;
159 return false;
162 /* Decide whether to emit frame unwind via assembler directives. */
165 dwarf2out_do_cfi_asm (void)
167 int enc;
169 #ifdef MIPS_DEBUGGING_INFO
170 return false;
171 #endif
172 if (saved_do_cfi_asm)
173 return true;
174 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
175 return false;
176 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
177 return false;
179 /* Make sure the personality encoding is one the assembler can support.
180 In particular, aligned addresses can't be handled. */
181 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
182 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
183 return false;
184 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
185 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
186 return false;
188 /* If we can't get the assembler to emit only .debug_frame, and we don't need
189 dwarf2 unwind info for exceptions, then emit .debug_frame by hand. */
190 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
191 && !flag_unwind_tables && !flag_exceptions
192 && targetm.except_unwind_info (&global_options) != UI_DWARF2)
193 return false;
195 saved_do_cfi_asm = true;
196 return true;
199 /* The size of the target's pointer type. */
200 #ifndef PTR_SIZE
201 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
202 #endif
204 /* Array of RTXes referenced by the debugging information, which therefore
205 must be kept around forever. */
206 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208 /* A pointer to the base of a list of incomplete types which might be
209 completed at some later time. incomplete_types_list needs to be a
210 VEC(tree,gc) because we want to tell the garbage collector about
211 it. */
212 static GTY(()) VEC(tree,gc) *incomplete_types;
214 /* A pointer to the base of a table of references to declaration
215 scopes. This table is a display which tracks the nesting
216 of declaration scopes at the current scope and containing
217 scopes. This table is used to find the proper place to
218 define type declaration DIE's. */
219 static GTY(()) VEC(tree,gc) *decl_scope_table;
221 /* Pointers to various DWARF2 sections. */
222 static GTY(()) section *debug_info_section;
223 static GTY(()) section *debug_abbrev_section;
224 static GTY(()) section *debug_aranges_section;
225 static GTY(()) section *debug_macinfo_section;
226 static GTY(()) section *debug_line_section;
227 static GTY(()) section *debug_loc_section;
228 static GTY(()) section *debug_pubnames_section;
229 static GTY(()) section *debug_pubtypes_section;
230 static GTY(()) section *debug_dcall_section;
231 static GTY(()) section *debug_vcall_section;
232 static GTY(()) section *debug_str_section;
233 static GTY(()) section *debug_ranges_section;
234 static GTY(()) section *debug_frame_section;
236 /* Personality decl of current unit. Used only when assembler does not support
237 personality CFI. */
238 static GTY(()) rtx current_unit_personality;
240 /* How to start an assembler comment. */
241 #ifndef ASM_COMMENT_START
242 #define ASM_COMMENT_START ";#"
243 #endif
245 typedef struct dw_cfi_struct *dw_cfi_ref;
246 typedef struct dw_fde_struct *dw_fde_ref;
247 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
249 /* Call frames are described using a sequence of Call Frame
250 Information instructions. The register number, offset
251 and address fields are provided as possible operands;
252 their use is selected by the opcode field. */
254 enum dw_cfi_oprnd_type {
255 dw_cfi_oprnd_unused,
256 dw_cfi_oprnd_reg_num,
257 dw_cfi_oprnd_offset,
258 dw_cfi_oprnd_addr,
259 dw_cfi_oprnd_loc
262 typedef union GTY(()) dw_cfi_oprnd_struct {
263 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
264 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
265 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
266 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
268 dw_cfi_oprnd;
270 typedef struct GTY(()) dw_cfi_struct {
271 dw_cfi_ref dw_cfi_next;
272 enum dwarf_call_frame_info dw_cfi_opc;
273 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
274 dw_cfi_oprnd1;
275 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
276 dw_cfi_oprnd2;
278 dw_cfi_node;
280 /* This is how we define the location of the CFA. We use to handle it
281 as REG + OFFSET all the time, but now it can be more complex.
282 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
283 Instead of passing around REG and OFFSET, we pass a copy
284 of this structure. */
285 typedef struct cfa_loc {
286 HOST_WIDE_INT offset;
287 HOST_WIDE_INT base_offset;
288 unsigned int reg;
289 BOOL_BITFIELD indirect : 1; /* 1 if CFA is accessed via a dereference. */
290 BOOL_BITFIELD in_use : 1; /* 1 if a saved cfa is stored here. */
291 } dw_cfa_location;
293 /* All call frame descriptions (FDE's) in the GCC generated DWARF
294 refer to a single Common Information Entry (CIE), defined at
295 the beginning of the .debug_frame section. This use of a single
296 CIE obviates the need to keep track of multiple CIE's
297 in the DWARF generation routines below. */
299 typedef struct GTY(()) dw_fde_struct {
300 tree decl;
301 const char *dw_fde_begin;
302 const char *dw_fde_current_label;
303 const char *dw_fde_end;
304 const char *dw_fde_vms_end_prologue;
305 const char *dw_fde_vms_begin_epilogue;
306 const char *dw_fde_hot_section_label;
307 const char *dw_fde_hot_section_end_label;
308 const char *dw_fde_unlikely_section_label;
309 const char *dw_fde_unlikely_section_end_label;
310 dw_cfi_ref dw_fde_cfi;
311 dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections. */
312 HOST_WIDE_INT stack_realignment;
313 unsigned funcdef_number;
314 /* Dynamic realign argument pointer register. */
315 unsigned int drap_reg;
316 /* Virtual dynamic realign argument pointer register. */
317 unsigned int vdrap_reg;
318 /* These 3 flags are copied from rtl_data in function.h. */
319 unsigned all_throwers_are_sibcalls : 1;
320 unsigned uses_eh_lsda : 1;
321 unsigned nothrow : 1;
322 /* Whether we did stack realign in this call frame. */
323 unsigned stack_realign : 1;
324 /* Whether dynamic realign argument pointer register has been saved. */
325 unsigned drap_reg_saved: 1;
326 /* True iff dw_fde_begin label is in text_section or cold_text_section. */
327 unsigned in_std_section : 1;
328 /* True iff dw_fde_unlikely_section_label is in text_section or
329 cold_text_section. */
330 unsigned cold_in_std_section : 1;
331 /* True iff switched sections. */
332 unsigned dw_fde_switched_sections : 1;
333 /* True iff switching from cold to hot section. */
334 unsigned dw_fde_switched_cold_to_hot : 1;
336 dw_fde_node;
338 /* Maximum size (in bytes) of an artificially generated label. */
339 #define MAX_ARTIFICIAL_LABEL_BYTES 30
341 /* The size of addresses as they appear in the Dwarf 2 data.
342 Some architectures use word addresses to refer to code locations,
343 but Dwarf 2 info always uses byte addresses. On such machines,
344 Dwarf 2 addresses need to be larger than the architecture's
345 pointers. */
346 #ifndef DWARF2_ADDR_SIZE
347 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
348 #endif
350 /* The size in bytes of a DWARF field indicating an offset or length
351 relative to a debug info section, specified to be 4 bytes in the
352 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
353 as PTR_SIZE. */
355 #ifndef DWARF_OFFSET_SIZE
356 #define DWARF_OFFSET_SIZE 4
357 #endif
359 /* The size in bytes of a DWARF 4 type signature. */
361 #ifndef DWARF_TYPE_SIGNATURE_SIZE
362 #define DWARF_TYPE_SIGNATURE_SIZE 8
363 #endif
365 /* According to the (draft) DWARF 3 specification, the initial length
366 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
367 bytes are 0xffffffff, followed by the length stored in the next 8
368 bytes.
370 However, the SGI/MIPS ABI uses an initial length which is equal to
371 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
373 #ifndef DWARF_INITIAL_LENGTH_SIZE
374 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
375 #endif
377 /* Round SIZE up to the nearest BOUNDARY. */
378 #define DWARF_ROUND(SIZE,BOUNDARY) \
379 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
381 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
382 #ifndef DWARF_CIE_DATA_ALIGNMENT
383 #ifdef STACK_GROWS_DOWNWARD
384 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
385 #else
386 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
387 #endif
388 #endif
390 /* CIE identifier. */
391 #if HOST_BITS_PER_WIDE_INT >= 64
392 #define DWARF_CIE_ID \
393 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
394 #else
395 #define DWARF_CIE_ID DW_CIE_ID
396 #endif
398 /* A pointer to the base of a table that contains frame description
399 information for each routine. */
400 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
402 /* Number of elements currently allocated for fde_table. */
403 static GTY(()) unsigned fde_table_allocated;
405 /* Number of elements in fde_table currently in use. */
406 static GTY(()) unsigned fde_table_in_use;
408 /* Size (in elements) of increments by which we may expand the
409 fde_table. */
410 #define FDE_TABLE_INCREMENT 256
412 /* Get the current fde_table entry we should use. */
414 static inline dw_fde_ref
415 current_fde (void)
417 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
420 /* A list of call frame insns for the CIE. */
421 static GTY(()) dw_cfi_ref cie_cfi_head;
423 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
424 attribute that accelerates the lookup of the FDE associated
425 with the subprogram. This variable holds the table index of the FDE
426 associated with the current function (body) definition. */
427 static unsigned current_funcdef_fde;
429 struct GTY(()) indirect_string_node {
430 const char *str;
431 unsigned int refcount;
432 enum dwarf_form form;
433 char *label;
436 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
438 /* True if the compilation unit has location entries that reference
439 debug strings. */
440 static GTY(()) bool debug_str_hash_forced = false;
442 static GTY(()) int dw2_string_counter;
443 static GTY(()) unsigned long dwarf2out_cfi_label_num;
445 /* True if the compilation unit places functions in more than one section. */
446 static GTY(()) bool have_multiple_function_sections = false;
448 /* Whether the default text and cold text sections have been used at all. */
450 static GTY(()) bool text_section_used = false;
451 static GTY(()) bool cold_text_section_used = false;
453 /* The default cold text section. */
454 static GTY(()) section *cold_text_section;
456 /* Forward declarations for functions defined in this file. */
458 static char *stripattributes (const char *);
459 static const char *dwarf_cfi_name (unsigned);
460 static dw_cfi_ref new_cfi (void);
461 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
462 static void add_fde_cfi (const char *, dw_cfi_ref);
463 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
464 static void lookup_cfa (dw_cfa_location *);
465 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
466 static void initial_return_save (rtx);
467 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
468 HOST_WIDE_INT);
469 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
470 static void output_cfi_directive (dw_cfi_ref);
471 static void output_call_frame_info (int);
472 static void dwarf2out_note_section_used (void);
473 static bool clobbers_queued_reg_save (const_rtx);
474 static void dwarf2out_frame_debug_expr (rtx, const char *);
476 /* Support for complex CFA locations. */
477 static void output_cfa_loc (dw_cfi_ref);
478 static void output_cfa_loc_raw (dw_cfi_ref);
479 static void get_cfa_from_loc_descr (dw_cfa_location *,
480 struct dw_loc_descr_struct *);
481 static struct dw_loc_descr_struct *build_cfa_loc
482 (dw_cfa_location *, HOST_WIDE_INT);
483 static struct dw_loc_descr_struct *build_cfa_aligned_loc
484 (HOST_WIDE_INT, HOST_WIDE_INT);
485 static void def_cfa_1 (const char *, dw_cfa_location *);
486 static struct dw_loc_descr_struct *mem_loc_descriptor
487 (rtx, enum machine_mode mode, enum var_init_status);
489 /* How to start an assembler comment. */
490 #ifndef ASM_COMMENT_START
491 #define ASM_COMMENT_START ";#"
492 #endif
494 /* Data and reference forms for relocatable data. */
495 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
496 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
498 #ifndef DEBUG_FRAME_SECTION
499 #define DEBUG_FRAME_SECTION ".debug_frame"
500 #endif
502 #ifndef FUNC_BEGIN_LABEL
503 #define FUNC_BEGIN_LABEL "LFB"
504 #endif
506 #ifndef FUNC_END_LABEL
507 #define FUNC_END_LABEL "LFE"
508 #endif
510 #ifndef PROLOGUE_END_LABEL
511 #define PROLOGUE_END_LABEL "LPE"
512 #endif
514 #ifndef EPILOGUE_BEGIN_LABEL
515 #define EPILOGUE_BEGIN_LABEL "LEB"
516 #endif
518 #ifndef FRAME_BEGIN_LABEL
519 #define FRAME_BEGIN_LABEL "Lframe"
520 #endif
521 #define CIE_AFTER_SIZE_LABEL "LSCIE"
522 #define CIE_END_LABEL "LECIE"
523 #define FDE_LABEL "LSFDE"
524 #define FDE_AFTER_SIZE_LABEL "LASFDE"
525 #define FDE_END_LABEL "LEFDE"
526 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
527 #define LINE_NUMBER_END_LABEL "LELT"
528 #define LN_PROLOG_AS_LABEL "LASLTP"
529 #define LN_PROLOG_END_LABEL "LELTP"
530 #define DIE_LABEL_PREFIX "DW"
532 /* The DWARF 2 CFA column which tracks the return address. Normally this
533 is the column for PC, or the first column after all of the hard
534 registers. */
535 #ifndef DWARF_FRAME_RETURN_COLUMN
536 #ifdef PC_REGNUM
537 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
538 #else
539 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
540 #endif
541 #endif
543 /* The mapping from gcc register number to DWARF 2 CFA column number. By
544 default, we just provide columns for all registers. */
545 #ifndef DWARF_FRAME_REGNUM
546 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
547 #endif
549 /* Match the base name of a file to the base name of a compilation unit. */
551 static int
552 matches_main_base (const char *path)
554 /* Cache the last query. */
555 static const char *last_path = NULL;
556 static int last_match = 0;
557 if (path != last_path)
559 const char *base;
560 int length = base_of_path (path, &base);
561 last_path = path;
562 last_match = (length == main_input_baselength
563 && memcmp (base, main_input_basename, length) == 0);
565 return last_match;
568 #ifdef DEBUG_DEBUG_STRUCT
570 static int
571 dump_struct_debug (tree type, enum debug_info_usage usage,
572 enum debug_struct_file criterion, int generic,
573 int matches, int result)
575 /* Find the type name. */
576 tree type_decl = TYPE_STUB_DECL (type);
577 tree t = type_decl;
578 const char *name = 0;
579 if (TREE_CODE (t) == TYPE_DECL)
580 t = DECL_NAME (t);
581 if (t)
582 name = IDENTIFIER_POINTER (t);
584 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
585 criterion,
586 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
587 matches ? "bas" : "hdr",
588 generic ? "gen" : "ord",
589 usage == DINFO_USAGE_DFN ? ";" :
590 usage == DINFO_USAGE_DIR_USE ? "." : "*",
591 result,
592 (void*) type_decl, name);
593 return result;
595 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
596 dump_struct_debug (type, usage, criterion, generic, matches, result)
598 #else
600 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
601 (result)
603 #endif
605 static bool
606 should_emit_struct_debug (tree type, enum debug_info_usage usage)
608 enum debug_struct_file criterion;
609 tree type_decl;
610 bool generic = lang_hooks.types.generic_p (type);
612 if (generic)
613 criterion = debug_struct_generic[usage];
614 else
615 criterion = debug_struct_ordinary[usage];
617 if (criterion == DINFO_STRUCT_FILE_NONE)
618 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
619 if (criterion == DINFO_STRUCT_FILE_ANY)
620 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
622 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
624 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
625 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
627 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
628 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
629 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
632 /* Hook used by __throw. */
635 expand_builtin_dwarf_sp_column (void)
637 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
638 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
641 /* Return a pointer to a copy of the section string name S with all
642 attributes stripped off, and an asterisk prepended (for assemble_name). */
644 static inline char *
645 stripattributes (const char *s)
647 char *stripped = XNEWVEC (char, strlen (s) + 2);
648 char *p = stripped;
650 *p++ = '*';
652 while (*s && *s != ',')
653 *p++ = *s++;
655 *p = '\0';
656 return stripped;
659 /* MEM is a memory reference for the register size table, each element of
660 which has mode MODE. Initialize column C as a return address column. */
662 static void
663 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
665 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
666 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
667 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
670 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
672 static inline HOST_WIDE_INT
673 div_data_align (HOST_WIDE_INT off)
675 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
676 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
677 return r;
680 /* Return true if we need a signed version of a given opcode
681 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
683 static inline bool
684 need_data_align_sf_opcode (HOST_WIDE_INT off)
686 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
689 /* Generate code to initialize the register size table. */
691 void
692 expand_builtin_init_dwarf_reg_sizes (tree address)
694 unsigned int i;
695 enum machine_mode mode = TYPE_MODE (char_type_node);
696 rtx addr = expand_normal (address);
697 rtx mem = gen_rtx_MEM (BLKmode, addr);
698 bool wrote_return_column = false;
700 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
702 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
704 if (rnum < DWARF_FRAME_REGISTERS)
706 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
707 enum machine_mode save_mode = reg_raw_mode[i];
708 HOST_WIDE_INT size;
710 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
711 save_mode = choose_hard_reg_mode (i, 1, true);
712 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
714 if (save_mode == VOIDmode)
715 continue;
716 wrote_return_column = true;
718 size = GET_MODE_SIZE (save_mode);
719 if (offset < 0)
720 continue;
722 emit_move_insn (adjust_address (mem, mode, offset),
723 gen_int_mode (size, mode));
727 if (!wrote_return_column)
728 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
730 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
731 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
732 #endif
734 targetm.init_dwarf_reg_sizes_extra (address);
737 /* Convert a DWARF call frame info. operation to its string name */
739 static const char *
740 dwarf_cfi_name (unsigned int cfi_opc)
742 switch (cfi_opc)
744 case DW_CFA_advance_loc:
745 return "DW_CFA_advance_loc";
746 case DW_CFA_offset:
747 return "DW_CFA_offset";
748 case DW_CFA_restore:
749 return "DW_CFA_restore";
750 case DW_CFA_nop:
751 return "DW_CFA_nop";
752 case DW_CFA_set_loc:
753 return "DW_CFA_set_loc";
754 case DW_CFA_advance_loc1:
755 return "DW_CFA_advance_loc1";
756 case DW_CFA_advance_loc2:
757 return "DW_CFA_advance_loc2";
758 case DW_CFA_advance_loc4:
759 return "DW_CFA_advance_loc4";
760 case DW_CFA_offset_extended:
761 return "DW_CFA_offset_extended";
762 case DW_CFA_restore_extended:
763 return "DW_CFA_restore_extended";
764 case DW_CFA_undefined:
765 return "DW_CFA_undefined";
766 case DW_CFA_same_value:
767 return "DW_CFA_same_value";
768 case DW_CFA_register:
769 return "DW_CFA_register";
770 case DW_CFA_remember_state:
771 return "DW_CFA_remember_state";
772 case DW_CFA_restore_state:
773 return "DW_CFA_restore_state";
774 case DW_CFA_def_cfa:
775 return "DW_CFA_def_cfa";
776 case DW_CFA_def_cfa_register:
777 return "DW_CFA_def_cfa_register";
778 case DW_CFA_def_cfa_offset:
779 return "DW_CFA_def_cfa_offset";
781 /* DWARF 3 */
782 case DW_CFA_def_cfa_expression:
783 return "DW_CFA_def_cfa_expression";
784 case DW_CFA_expression:
785 return "DW_CFA_expression";
786 case DW_CFA_offset_extended_sf:
787 return "DW_CFA_offset_extended_sf";
788 case DW_CFA_def_cfa_sf:
789 return "DW_CFA_def_cfa_sf";
790 case DW_CFA_def_cfa_offset_sf:
791 return "DW_CFA_def_cfa_offset_sf";
793 /* SGI/MIPS specific */
794 case DW_CFA_MIPS_advance_loc8:
795 return "DW_CFA_MIPS_advance_loc8";
797 /* GNU extensions */
798 case DW_CFA_GNU_window_save:
799 return "DW_CFA_GNU_window_save";
800 case DW_CFA_GNU_args_size:
801 return "DW_CFA_GNU_args_size";
802 case DW_CFA_GNU_negative_offset_extended:
803 return "DW_CFA_GNU_negative_offset_extended";
805 default:
806 return "DW_CFA_<unknown>";
810 /* Return a pointer to a newly allocated Call Frame Instruction. */
812 static inline dw_cfi_ref
813 new_cfi (void)
815 dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
817 cfi->dw_cfi_next = NULL;
818 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
819 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
821 return cfi;
824 /* Add a Call Frame Instruction to list of instructions. */
826 static inline void
827 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
829 dw_cfi_ref *p;
830 dw_fde_ref fde = current_fde ();
832 /* When DRAP is used, CFA is defined with an expression. Redefine
833 CFA may lead to a different CFA value. */
834 /* ??? Of course, this heuristic fails when we're annotating epilogues,
835 because of course we'll always want to redefine the CFA back to the
836 stack pointer on the way out. Where should we move this check? */
837 if (0 && fde && fde->drap_reg != INVALID_REGNUM)
838 switch (cfi->dw_cfi_opc)
840 case DW_CFA_def_cfa_register:
841 case DW_CFA_def_cfa_offset:
842 case DW_CFA_def_cfa_offset_sf:
843 case DW_CFA_def_cfa:
844 case DW_CFA_def_cfa_sf:
845 gcc_unreachable ();
847 default:
848 break;
851 /* Find the end of the chain. */
852 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
855 *p = cfi;
858 /* Generate a new label for the CFI info to refer to. FORCE is true
859 if a label needs to be output even when using .cfi_* directives. */
861 char *
862 dwarf2out_cfi_label (bool force)
864 static char label[20];
866 if (!force && dwarf2out_do_cfi_asm ())
868 /* In this case, we will be emitting the asm directive instead of
869 the label, so just return a placeholder to keep the rest of the
870 interfaces happy. */
871 strcpy (label, "<do not output>");
873 else
875 int num = dwarf2out_cfi_label_num++;
876 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
877 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
880 return label;
883 /* True if remember_state should be emitted before following CFI directive. */
884 static bool emit_cfa_remember;
886 /* True if any CFI directives were emitted at the current insn. */
887 static bool any_cfis_emitted;
889 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
890 or to the CIE if LABEL is NULL. */
892 static void
893 add_fde_cfi (const char *label, dw_cfi_ref cfi)
895 dw_cfi_ref *list_head;
897 if (emit_cfa_remember)
899 dw_cfi_ref cfi_remember;
901 /* Emit the state save. */
902 emit_cfa_remember = false;
903 cfi_remember = new_cfi ();
904 cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
905 add_fde_cfi (label, cfi_remember);
908 list_head = &cie_cfi_head;
910 if (dwarf2out_do_cfi_asm ())
912 if (label)
914 dw_fde_ref fde = current_fde ();
916 gcc_assert (fde != NULL);
918 /* We still have to add the cfi to the list so that lookup_cfa
919 works later on. When -g2 and above we even need to force
920 emitting of CFI labels and add to list a DW_CFA_set_loc for
921 convert_cfa_to_fb_loc_list purposes. If we're generating
922 DWARF3 output we use DW_OP_call_frame_cfa and so don't use
923 convert_cfa_to_fb_loc_list. */
924 if (dwarf_version == 2
925 && debug_info_level > DINFO_LEVEL_TERSE
926 && (write_symbols == DWARF2_DEBUG
927 || write_symbols == VMS_AND_DWARF2_DEBUG))
929 switch (cfi->dw_cfi_opc)
931 case DW_CFA_def_cfa_offset:
932 case DW_CFA_def_cfa_offset_sf:
933 case DW_CFA_def_cfa_register:
934 case DW_CFA_def_cfa:
935 case DW_CFA_def_cfa_sf:
936 case DW_CFA_def_cfa_expression:
937 case DW_CFA_restore_state:
938 if (*label == 0 || strcmp (label, "<do not output>") == 0)
939 label = dwarf2out_cfi_label (true);
941 if (fde->dw_fde_current_label == NULL
942 || strcmp (label, fde->dw_fde_current_label) != 0)
944 dw_cfi_ref xcfi;
946 label = xstrdup (label);
948 /* Set the location counter to the new label. */
949 xcfi = new_cfi ();
950 /* It doesn't metter whether DW_CFA_set_loc
951 or DW_CFA_advance_loc4 is added here, those aren't
952 emitted into assembly, only looked up by
953 convert_cfa_to_fb_loc_list. */
954 xcfi->dw_cfi_opc = DW_CFA_set_loc;
955 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
956 add_cfi (&fde->dw_fde_cfi, xcfi);
957 fde->dw_fde_current_label = label;
959 break;
960 default:
961 break;
965 output_cfi_directive (cfi);
967 list_head = &fde->dw_fde_cfi;
968 any_cfis_emitted = true;
970 /* ??? If this is a CFI for the CIE, we don't emit. This
971 assumes that the standard CIE contents that the assembler
972 uses matches the standard CIE contents that the compiler
973 uses. This is probably a bad assumption. I'm not quite
974 sure how to address this for now. */
976 else if (label)
978 dw_fde_ref fde = current_fde ();
980 gcc_assert (fde != NULL);
982 if (*label == 0)
983 label = dwarf2out_cfi_label (false);
985 if (fde->dw_fde_current_label == NULL
986 || strcmp (label, fde->dw_fde_current_label) != 0)
988 dw_cfi_ref xcfi;
990 label = xstrdup (label);
992 /* Set the location counter to the new label. */
993 xcfi = new_cfi ();
994 /* If we have a current label, advance from there, otherwise
995 set the location directly using set_loc. */
996 xcfi->dw_cfi_opc = fde->dw_fde_current_label
997 ? DW_CFA_advance_loc4
998 : DW_CFA_set_loc;
999 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
1000 add_cfi (&fde->dw_fde_cfi, xcfi);
1002 fde->dw_fde_current_label = label;
1005 list_head = &fde->dw_fde_cfi;
1006 any_cfis_emitted = true;
1009 add_cfi (list_head, cfi);
1012 /* Subroutine of lookup_cfa. */
1014 static void
1015 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1017 switch (cfi->dw_cfi_opc)
1019 case DW_CFA_def_cfa_offset:
1020 case DW_CFA_def_cfa_offset_sf:
1021 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1022 break;
1023 case DW_CFA_def_cfa_register:
1024 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1025 break;
1026 case DW_CFA_def_cfa:
1027 case DW_CFA_def_cfa_sf:
1028 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1029 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1030 break;
1031 case DW_CFA_def_cfa_expression:
1032 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1033 break;
1035 case DW_CFA_remember_state:
1036 gcc_assert (!remember->in_use);
1037 *remember = *loc;
1038 remember->in_use = 1;
1039 break;
1040 case DW_CFA_restore_state:
1041 gcc_assert (remember->in_use);
1042 *loc = *remember;
1043 remember->in_use = 0;
1044 break;
1046 default:
1047 break;
1051 /* Find the previous value for the CFA. */
1053 static void
1054 lookup_cfa (dw_cfa_location *loc)
1056 dw_cfi_ref cfi;
1057 dw_fde_ref fde;
1058 dw_cfa_location remember;
1060 memset (loc, 0, sizeof (*loc));
1061 loc->reg = INVALID_REGNUM;
1062 remember = *loc;
1064 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1065 lookup_cfa_1 (cfi, loc, &remember);
1067 fde = current_fde ();
1068 if (fde)
1069 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1070 lookup_cfa_1 (cfi, loc, &remember);
1073 /* The current rule for calculating the DWARF2 canonical frame address. */
1074 static dw_cfa_location cfa;
1076 /* The register used for saving registers to the stack, and its offset
1077 from the CFA. */
1078 static dw_cfa_location cfa_store;
1080 /* The current save location around an epilogue. */
1081 static dw_cfa_location cfa_remember;
1083 /* The running total of the size of arguments pushed onto the stack. */
1084 static HOST_WIDE_INT args_size;
1086 /* The last args_size we actually output. */
1087 static HOST_WIDE_INT old_args_size;
1089 /* Entry point to update the canonical frame address (CFA).
1090 LABEL is passed to add_fde_cfi. The value of CFA is now to be
1091 calculated from REG+OFFSET. */
1093 void
1094 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1096 dw_cfa_location loc;
1097 loc.indirect = 0;
1098 loc.base_offset = 0;
1099 loc.reg = reg;
1100 loc.offset = offset;
1101 def_cfa_1 (label, &loc);
1104 /* Determine if two dw_cfa_location structures define the same data. */
1106 static bool
1107 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1109 return (loc1->reg == loc2->reg
1110 && loc1->offset == loc2->offset
1111 && loc1->indirect == loc2->indirect
1112 && (loc1->indirect == 0
1113 || loc1->base_offset == loc2->base_offset));
1116 /* This routine does the actual work. The CFA is now calculated from
1117 the dw_cfa_location structure. */
1119 static void
1120 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1122 dw_cfi_ref cfi;
1123 dw_cfa_location old_cfa, loc;
1125 cfa = *loc_p;
1126 loc = *loc_p;
1128 if (cfa_store.reg == loc.reg && loc.indirect == 0)
1129 cfa_store.offset = loc.offset;
1131 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1132 lookup_cfa (&old_cfa);
1134 /* If nothing changed, no need to issue any call frame instructions. */
1135 if (cfa_equal_p (&loc, &old_cfa))
1136 return;
1138 cfi = new_cfi ();
1140 if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1142 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1143 the CFA register did not change but the offset did. The data
1144 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1145 in the assembler via the .cfi_def_cfa_offset directive. */
1146 if (loc.offset < 0)
1147 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1148 else
1149 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1150 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1153 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
1154 else if (loc.offset == old_cfa.offset
1155 && old_cfa.reg != INVALID_REGNUM
1156 && !loc.indirect
1157 && !old_cfa.indirect)
1159 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1160 indicating the CFA register has changed to <register> but the
1161 offset has not changed. */
1162 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1163 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1165 #endif
1167 else if (loc.indirect == 0)
1169 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1170 indicating the CFA register has changed to <register> with
1171 the specified offset. The data factoring for DW_CFA_def_cfa_sf
1172 happens in output_cfi, or in the assembler via the .cfi_def_cfa
1173 directive. */
1174 if (loc.offset < 0)
1175 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1176 else
1177 cfi->dw_cfi_opc = DW_CFA_def_cfa;
1178 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1179 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1181 else
1183 /* Construct a DW_CFA_def_cfa_expression instruction to
1184 calculate the CFA using a full location expression since no
1185 register-offset pair is available. */
1186 struct dw_loc_descr_struct *loc_list;
1188 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1189 loc_list = build_cfa_loc (&loc, 0);
1190 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1193 add_fde_cfi (label, cfi);
1196 /* Add the CFI for saving a register. REG is the CFA column number.
1197 LABEL is passed to add_fde_cfi.
1198 If SREG is -1, the register is saved at OFFSET from the CFA;
1199 otherwise it is saved in SREG. */
1201 static void
1202 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1204 dw_cfi_ref cfi = new_cfi ();
1205 dw_fde_ref fde = current_fde ();
1207 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1209 /* When stack is aligned, store REG using DW_CFA_expression with
1210 FP. */
1211 if (fde
1212 && fde->stack_realign
1213 && sreg == INVALID_REGNUM)
1215 cfi->dw_cfi_opc = DW_CFA_expression;
1216 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1217 cfi->dw_cfi_oprnd2.dw_cfi_loc
1218 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1220 else if (sreg == INVALID_REGNUM)
1222 if (need_data_align_sf_opcode (offset))
1223 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1224 else if (reg & ~0x3f)
1225 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1226 else
1227 cfi->dw_cfi_opc = DW_CFA_offset;
1228 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1230 else if (sreg == reg)
1231 cfi->dw_cfi_opc = DW_CFA_same_value;
1232 else
1234 cfi->dw_cfi_opc = DW_CFA_register;
1235 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1238 add_fde_cfi (label, cfi);
1241 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1242 This CFI tells the unwinder that it needs to restore the window registers
1243 from the previous frame's window save area.
1245 ??? Perhaps we should note in the CIE where windows are saved (instead of
1246 assuming 0(cfa)) and what registers are in the window. */
1248 void
1249 dwarf2out_window_save (const char *label)
1251 dw_cfi_ref cfi = new_cfi ();
1253 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1254 add_fde_cfi (label, cfi);
1257 /* Entry point for saving a register to the stack. REG is the GCC register
1258 number. LABEL and OFFSET are passed to reg_save. */
1260 void
1261 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1263 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1266 /* Entry point for saving the return address in the stack.
1267 LABEL and OFFSET are passed to reg_save. */
1269 void
1270 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1272 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1275 /* Entry point for saving the return address in a register.
1276 LABEL and SREG are passed to reg_save. */
1278 void
1279 dwarf2out_return_reg (const char *label, unsigned int sreg)
1281 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1284 /* Record the initial position of the return address. RTL is
1285 INCOMING_RETURN_ADDR_RTX. */
1287 static void
1288 initial_return_save (rtx rtl)
1290 unsigned int reg = INVALID_REGNUM;
1291 HOST_WIDE_INT offset = 0;
1293 switch (GET_CODE (rtl))
1295 case REG:
1296 /* RA is in a register. */
1297 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1298 break;
1300 case MEM:
1301 /* RA is on the stack. */
1302 rtl = XEXP (rtl, 0);
1303 switch (GET_CODE (rtl))
1305 case REG:
1306 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1307 offset = 0;
1308 break;
1310 case PLUS:
1311 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1312 offset = INTVAL (XEXP (rtl, 1));
1313 break;
1315 case MINUS:
1316 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1317 offset = -INTVAL (XEXP (rtl, 1));
1318 break;
1320 default:
1321 gcc_unreachable ();
1324 break;
1326 case PLUS:
1327 /* The return address is at some offset from any value we can
1328 actually load. For instance, on the SPARC it is in %i7+8. Just
1329 ignore the offset for now; it doesn't matter for unwinding frames. */
1330 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1331 initial_return_save (XEXP (rtl, 0));
1332 return;
1334 default:
1335 gcc_unreachable ();
1338 if (reg != DWARF_FRAME_RETURN_COLUMN)
1339 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1342 /* Given a SET, calculate the amount of stack adjustment it
1343 contains. */
1345 static HOST_WIDE_INT
1346 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1347 HOST_WIDE_INT cur_offset)
1349 const_rtx src = SET_SRC (pattern);
1350 const_rtx dest = SET_DEST (pattern);
1351 HOST_WIDE_INT offset = 0;
1352 enum rtx_code code;
1354 if (dest == stack_pointer_rtx)
1356 code = GET_CODE (src);
1358 /* Assume (set (reg sp) (reg whatever)) sets args_size
1359 level to 0. */
1360 if (code == REG && src != stack_pointer_rtx)
1362 offset = -cur_args_size;
1363 #ifndef STACK_GROWS_DOWNWARD
1364 offset = -offset;
1365 #endif
1366 return offset - cur_offset;
1369 if (! (code == PLUS || code == MINUS)
1370 || XEXP (src, 0) != stack_pointer_rtx
1371 || !CONST_INT_P (XEXP (src, 1)))
1372 return 0;
1374 /* (set (reg sp) (plus (reg sp) (const_int))) */
1375 offset = INTVAL (XEXP (src, 1));
1376 if (code == PLUS)
1377 offset = -offset;
1378 return offset;
1381 if (MEM_P (src) && !MEM_P (dest))
1382 dest = src;
1383 if (MEM_P (dest))
1385 /* (set (mem (pre_dec (reg sp))) (foo)) */
1386 src = XEXP (dest, 0);
1387 code = GET_CODE (src);
1389 switch (code)
1391 case PRE_MODIFY:
1392 case POST_MODIFY:
1393 if (XEXP (src, 0) == stack_pointer_rtx)
1395 rtx val = XEXP (XEXP (src, 1), 1);
1396 /* We handle only adjustments by constant amount. */
1397 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1398 && CONST_INT_P (val));
1399 offset = -INTVAL (val);
1400 break;
1402 return 0;
1404 case PRE_DEC:
1405 case POST_DEC:
1406 if (XEXP (src, 0) == stack_pointer_rtx)
1408 offset = GET_MODE_SIZE (GET_MODE (dest));
1409 break;
1411 return 0;
1413 case PRE_INC:
1414 case POST_INC:
1415 if (XEXP (src, 0) == stack_pointer_rtx)
1417 offset = -GET_MODE_SIZE (GET_MODE (dest));
1418 break;
1420 return 0;
1422 default:
1423 return 0;
1426 else
1427 return 0;
1429 return offset;
1432 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1433 indexed by INSN_UID. */
1435 static HOST_WIDE_INT *barrier_args_size;
1437 /* Helper function for compute_barrier_args_size. Handle one insn. */
1439 static HOST_WIDE_INT
1440 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1441 VEC (rtx, heap) **next)
1443 HOST_WIDE_INT offset = 0;
1444 int i;
1446 if (! RTX_FRAME_RELATED_P (insn))
1448 if (prologue_epilogue_contains (insn))
1449 /* Nothing */;
1450 else if (GET_CODE (PATTERN (insn)) == SET)
1451 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1452 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1453 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1455 /* There may be stack adjustments inside compound insns. Search
1456 for them. */
1457 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1458 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1459 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1460 cur_args_size, offset);
1463 else
1465 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1467 if (expr)
1469 expr = XEXP (expr, 0);
1470 if (GET_CODE (expr) == PARALLEL
1471 || GET_CODE (expr) == SEQUENCE)
1472 for (i = 1; i < XVECLEN (expr, 0); i++)
1474 rtx elem = XVECEXP (expr, 0, i);
1476 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1477 offset += stack_adjust_offset (elem, cur_args_size, offset);
1482 #ifndef STACK_GROWS_DOWNWARD
1483 offset = -offset;
1484 #endif
1486 cur_args_size += offset;
1487 if (cur_args_size < 0)
1488 cur_args_size = 0;
1490 if (JUMP_P (insn))
1492 rtx dest = JUMP_LABEL (insn);
1494 if (dest)
1496 if (barrier_args_size [INSN_UID (dest)] < 0)
1498 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1499 VEC_safe_push (rtx, heap, *next, dest);
1504 return cur_args_size;
1507 /* Walk the whole function and compute args_size on BARRIERs. */
1509 static void
1510 compute_barrier_args_size (void)
1512 int max_uid = get_max_uid (), i;
1513 rtx insn;
1514 VEC (rtx, heap) *worklist, *next, *tmp;
1516 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1517 for (i = 0; i < max_uid; i++)
1518 barrier_args_size[i] = -1;
1520 worklist = VEC_alloc (rtx, heap, 20);
1521 next = VEC_alloc (rtx, heap, 20);
1522 insn = get_insns ();
1523 barrier_args_size[INSN_UID (insn)] = 0;
1524 VEC_quick_push (rtx, worklist, insn);
1525 for (;;)
1527 while (!VEC_empty (rtx, worklist))
1529 rtx prev, body, first_insn;
1530 HOST_WIDE_INT cur_args_size;
1532 first_insn = insn = VEC_pop (rtx, worklist);
1533 cur_args_size = barrier_args_size[INSN_UID (insn)];
1534 prev = prev_nonnote_insn (insn);
1535 if (prev && BARRIER_P (prev))
1536 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1538 for (; insn; insn = NEXT_INSN (insn))
1540 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1541 continue;
1542 if (BARRIER_P (insn))
1543 break;
1545 if (LABEL_P (insn))
1547 if (insn == first_insn)
1548 continue;
1549 else if (barrier_args_size[INSN_UID (insn)] < 0)
1551 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1552 continue;
1554 else
1556 /* The insns starting with this label have been
1557 already scanned or are in the worklist. */
1558 break;
1562 body = PATTERN (insn);
1563 if (GET_CODE (body) == SEQUENCE)
1565 HOST_WIDE_INT dest_args_size = cur_args_size;
1566 for (i = 1; i < XVECLEN (body, 0); i++)
1567 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1568 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1569 dest_args_size
1570 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1571 dest_args_size, &next);
1572 else
1573 cur_args_size
1574 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1575 cur_args_size, &next);
1577 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1578 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1579 dest_args_size, &next);
1580 else
1581 cur_args_size
1582 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1583 cur_args_size, &next);
1585 else
1586 cur_args_size
1587 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1591 if (VEC_empty (rtx, next))
1592 break;
1594 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1595 tmp = next;
1596 next = worklist;
1597 worklist = tmp;
1598 VEC_truncate (rtx, next, 0);
1601 VEC_free (rtx, heap, worklist);
1602 VEC_free (rtx, heap, next);
1605 /* Add a CFI to update the running total of the size of arguments
1606 pushed onto the stack. */
1608 static void
1609 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1611 dw_cfi_ref cfi;
1613 if (size == old_args_size)
1614 return;
1616 old_args_size = size;
1618 cfi = new_cfi ();
1619 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1620 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1621 add_fde_cfi (label, cfi);
1624 /* Record a stack adjustment of OFFSET bytes. */
1626 static void
1627 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1629 if (cfa.reg == STACK_POINTER_REGNUM)
1630 cfa.offset += offset;
1632 if (cfa_store.reg == STACK_POINTER_REGNUM)
1633 cfa_store.offset += offset;
1635 if (ACCUMULATE_OUTGOING_ARGS)
1636 return;
1638 #ifndef STACK_GROWS_DOWNWARD
1639 offset = -offset;
1640 #endif
1642 args_size += offset;
1643 if (args_size < 0)
1644 args_size = 0;
1646 def_cfa_1 (label, &cfa);
1647 if (flag_asynchronous_unwind_tables)
1648 dwarf2out_args_size (label, args_size);
1651 /* Check INSN to see if it looks like a push or a stack adjustment, and
1652 make a note of it if it does. EH uses this information to find out
1653 how much extra space it needs to pop off the stack. */
1655 static void
1656 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1658 HOST_WIDE_INT offset;
1659 const char *label;
1660 int i;
1662 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1663 with this function. Proper support would require all frame-related
1664 insns to be marked, and to be able to handle saving state around
1665 epilogues textually in the middle of the function. */
1666 if (prologue_epilogue_contains (insn))
1667 return;
1669 /* If INSN is an instruction from target of an annulled branch, the
1670 effects are for the target only and so current argument size
1671 shouldn't change at all. */
1672 if (final_sequence
1673 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1674 && INSN_FROM_TARGET_P (insn))
1675 return;
1677 /* If only calls can throw, and we have a frame pointer,
1678 save up adjustments until we see the CALL_INSN. */
1679 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1681 if (CALL_P (insn) && !after_p)
1683 /* Extract the size of the args from the CALL rtx itself. */
1684 insn = PATTERN (insn);
1685 if (GET_CODE (insn) == PARALLEL)
1686 insn = XVECEXP (insn, 0, 0);
1687 if (GET_CODE (insn) == SET)
1688 insn = SET_SRC (insn);
1689 gcc_assert (GET_CODE (insn) == CALL);
1690 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1692 return;
1695 if (CALL_P (insn) && !after_p)
1697 if (!flag_asynchronous_unwind_tables)
1698 dwarf2out_args_size ("", args_size);
1699 return;
1701 else if (BARRIER_P (insn))
1703 /* Don't call compute_barrier_args_size () if the only
1704 BARRIER is at the end of function. */
1705 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1706 compute_barrier_args_size ();
1707 if (barrier_args_size == NULL)
1708 offset = 0;
1709 else
1711 offset = barrier_args_size[INSN_UID (insn)];
1712 if (offset < 0)
1713 offset = 0;
1716 offset -= args_size;
1717 #ifndef STACK_GROWS_DOWNWARD
1718 offset = -offset;
1719 #endif
1721 else if (GET_CODE (PATTERN (insn)) == SET)
1722 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1723 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1724 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1726 /* There may be stack adjustments inside compound insns. Search
1727 for them. */
1728 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1729 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1730 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1731 args_size, offset);
1733 else
1734 return;
1736 if (offset == 0)
1737 return;
1739 label = dwarf2out_cfi_label (false);
1740 dwarf2out_stack_adjust (offset, label);
1743 /* We delay emitting a register save until either (a) we reach the end
1744 of the prologue or (b) the register is clobbered. This clusters
1745 register saves so that there are fewer pc advances. */
1747 struct GTY(()) queued_reg_save {
1748 struct queued_reg_save *next;
1749 rtx reg;
1750 HOST_WIDE_INT cfa_offset;
1751 rtx saved_reg;
1754 static GTY(()) struct queued_reg_save *queued_reg_saves;
1756 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1757 struct GTY(()) reg_saved_in_data {
1758 rtx orig_reg;
1759 rtx saved_in_reg;
1762 /* A list of registers saved in other registers.
1763 The list intentionally has a small maximum capacity of 4; if your
1764 port needs more than that, you might consider implementing a
1765 more efficient data structure. */
1766 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1767 static GTY(()) size_t num_regs_saved_in_regs;
1769 static const char *last_reg_save_label;
1771 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1772 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1774 static void
1775 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1777 struct queued_reg_save *q;
1779 /* Duplicates waste space, but it's also necessary to remove them
1780 for correctness, since the queue gets output in reverse
1781 order. */
1782 for (q = queued_reg_saves; q != NULL; q = q->next)
1783 if (REGNO (q->reg) == REGNO (reg))
1784 break;
1786 if (q == NULL)
1788 q = ggc_alloc_queued_reg_save ();
1789 q->next = queued_reg_saves;
1790 queued_reg_saves = q;
1793 q->reg = reg;
1794 q->cfa_offset = offset;
1795 q->saved_reg = sreg;
1797 last_reg_save_label = label;
1800 /* Output all the entries in QUEUED_REG_SAVES. */
1802 void
1803 dwarf2out_flush_queued_reg_saves (void)
1805 struct queued_reg_save *q;
1807 for (q = queued_reg_saves; q; q = q->next)
1809 size_t i;
1810 unsigned int reg, sreg;
1812 for (i = 0; i < num_regs_saved_in_regs; i++)
1813 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1814 break;
1815 if (q->saved_reg && i == num_regs_saved_in_regs)
1817 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1818 num_regs_saved_in_regs++;
1820 if (i != num_regs_saved_in_regs)
1822 regs_saved_in_regs[i].orig_reg = q->reg;
1823 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1826 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1827 if (q->saved_reg)
1828 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1829 else
1830 sreg = INVALID_REGNUM;
1831 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1834 queued_reg_saves = NULL;
1835 last_reg_save_label = NULL;
1838 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1839 location for? Or, does it clobber a register which we've previously
1840 said that some other register is saved in, and for which we now
1841 have a new location for? */
1843 static bool
1844 clobbers_queued_reg_save (const_rtx insn)
1846 struct queued_reg_save *q;
1848 for (q = queued_reg_saves; q; q = q->next)
1850 size_t i;
1851 if (modified_in_p (q->reg, insn))
1852 return true;
1853 for (i = 0; i < num_regs_saved_in_regs; i++)
1854 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1855 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1856 return true;
1859 return false;
1862 /* Entry point for saving the first register into the second. */
1864 void
1865 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1867 size_t i;
1868 unsigned int regno, sregno;
1870 for (i = 0; i < num_regs_saved_in_regs; i++)
1871 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1872 break;
1873 if (i == num_regs_saved_in_regs)
1875 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1876 num_regs_saved_in_regs++;
1878 regs_saved_in_regs[i].orig_reg = reg;
1879 regs_saved_in_regs[i].saved_in_reg = sreg;
1881 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1882 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1883 reg_save (label, regno, sregno, 0);
1886 /* What register, if any, is currently saved in REG? */
1888 static rtx
1889 reg_saved_in (rtx reg)
1891 unsigned int regn = REGNO (reg);
1892 size_t i;
1893 struct queued_reg_save *q;
1895 for (q = queued_reg_saves; q; q = q->next)
1896 if (q->saved_reg && regn == REGNO (q->saved_reg))
1897 return q->reg;
1899 for (i = 0; i < num_regs_saved_in_regs; i++)
1900 if (regs_saved_in_regs[i].saved_in_reg
1901 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1902 return regs_saved_in_regs[i].orig_reg;
1904 return NULL_RTX;
1908 /* A temporary register holding an integral value used in adjusting SP
1909 or setting up the store_reg. The "offset" field holds the integer
1910 value, not an offset. */
1911 static dw_cfa_location cfa_temp;
1913 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1915 static void
1916 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1918 memset (&cfa, 0, sizeof (cfa));
1920 switch (GET_CODE (pat))
1922 case PLUS:
1923 cfa.reg = REGNO (XEXP (pat, 0));
1924 cfa.offset = INTVAL (XEXP (pat, 1));
1925 break;
1927 case REG:
1928 cfa.reg = REGNO (pat);
1929 break;
1931 case MEM:
1932 cfa.indirect = 1;
1933 pat = XEXP (pat, 0);
1934 if (GET_CODE (pat) == PLUS)
1936 cfa.base_offset = INTVAL (XEXP (pat, 1));
1937 pat = XEXP (pat, 0);
1939 cfa.reg = REGNO (pat);
1940 break;
1942 default:
1943 /* Recurse and define an expression. */
1944 gcc_unreachable ();
1947 def_cfa_1 (label, &cfa);
1950 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1952 static void
1953 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1955 rtx src, dest;
1957 gcc_assert (GET_CODE (pat) == SET);
1958 dest = XEXP (pat, 0);
1959 src = XEXP (pat, 1);
1961 switch (GET_CODE (src))
1963 case PLUS:
1964 gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1965 cfa.offset -= INTVAL (XEXP (src, 1));
1966 break;
1968 case REG:
1969 break;
1971 default:
1972 gcc_unreachable ();
1975 cfa.reg = REGNO (dest);
1976 gcc_assert (cfa.indirect == 0);
1978 def_cfa_1 (label, &cfa);
1981 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1983 static void
1984 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1986 HOST_WIDE_INT offset;
1987 rtx src, addr, span;
1989 src = XEXP (set, 1);
1990 addr = XEXP (set, 0);
1991 gcc_assert (MEM_P (addr));
1992 addr = XEXP (addr, 0);
1994 /* As documented, only consider extremely simple addresses. */
1995 switch (GET_CODE (addr))
1997 case REG:
1998 gcc_assert (REGNO (addr) == cfa.reg);
1999 offset = -cfa.offset;
2000 break;
2001 case PLUS:
2002 gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2003 offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2004 break;
2005 default:
2006 gcc_unreachable ();
2009 span = targetm.dwarf_register_span (src);
2011 /* ??? We'd like to use queue_reg_save, but we need to come up with
2012 a different flushing heuristic for epilogues. */
2013 if (!span)
2014 reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2015 else
2017 /* We have a PARALLEL describing where the contents of SRC live.
2018 Queue register saves for each piece of the PARALLEL. */
2019 int par_index;
2020 int limit;
2021 HOST_WIDE_INT span_offset = offset;
2023 gcc_assert (GET_CODE (span) == PARALLEL);
2025 limit = XVECLEN (span, 0);
2026 for (par_index = 0; par_index < limit; par_index++)
2028 rtx elem = XVECEXP (span, 0, par_index);
2030 reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2031 INVALID_REGNUM, span_offset);
2032 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2037 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
2039 static void
2040 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2042 rtx src, dest;
2043 unsigned sregno, dregno;
2045 src = XEXP (set, 1);
2046 dest = XEXP (set, 0);
2048 if (src == pc_rtx)
2049 sregno = DWARF_FRAME_RETURN_COLUMN;
2050 else
2051 sregno = DWARF_FRAME_REGNUM (REGNO (src));
2053 dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2055 /* ??? We'd like to use queue_reg_save, but we need to come up with
2056 a different flushing heuristic for epilogues. */
2057 reg_save (label, sregno, dregno, 0);
2060 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2062 static void
2063 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2065 rtx src, dest, span;
2066 dw_cfi_ref cfi = new_cfi ();
2068 dest = SET_DEST (set);
2069 src = SET_SRC (set);
2071 gcc_assert (REG_P (src));
2072 gcc_assert (MEM_P (dest));
2074 span = targetm.dwarf_register_span (src);
2075 gcc_assert (!span);
2077 cfi->dw_cfi_opc = DW_CFA_expression;
2078 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2079 cfi->dw_cfi_oprnd2.dw_cfi_loc
2080 = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2081 VAR_INIT_STATUS_INITIALIZED);
2083 /* ??? We'd like to use queue_reg_save, were the interface different,
2084 and, as above, we could manage flushing for epilogues. */
2085 add_fde_cfi (label, cfi);
2088 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
2090 static void
2091 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2093 dw_cfi_ref cfi = new_cfi ();
2094 unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2096 cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2097 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2099 add_fde_cfi (label, cfi);
2102 /* Record call frame debugging information for an expression EXPR,
2103 which either sets SP or FP (adjusting how we calculate the frame
2104 address) or saves a register to the stack or another register.
2105 LABEL indicates the address of EXPR.
2107 This function encodes a state machine mapping rtxes to actions on
2108 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
2109 users need not read the source code.
2111 The High-Level Picture
2113 Changes in the register we use to calculate the CFA: Currently we
2114 assume that if you copy the CFA register into another register, we
2115 should take the other one as the new CFA register; this seems to
2116 work pretty well. If it's wrong for some target, it's simple
2117 enough not to set RTX_FRAME_RELATED_P on the insn in question.
2119 Changes in the register we use for saving registers to the stack:
2120 This is usually SP, but not always. Again, we deduce that if you
2121 copy SP into another register (and SP is not the CFA register),
2122 then the new register is the one we will be using for register
2123 saves. This also seems to work.
2125 Register saves: There's not much guesswork about this one; if
2126 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2127 register save, and the register used to calculate the destination
2128 had better be the one we think we're using for this purpose.
2129 It's also assumed that a copy from a call-saved register to another
2130 register is saving that register if RTX_FRAME_RELATED_P is set on
2131 that instruction. If the copy is from a call-saved register to
2132 the *same* register, that means that the register is now the same
2133 value as in the caller.
2135 Except: If the register being saved is the CFA register, and the
2136 offset is nonzero, we are saving the CFA, so we assume we have to
2137 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
2138 the intent is to save the value of SP from the previous frame.
2140 In addition, if a register has previously been saved to a different
2141 register,
2143 Invariants / Summaries of Rules
2145 cfa current rule for calculating the CFA. It usually
2146 consists of a register and an offset.
2147 cfa_store register used by prologue code to save things to the stack
2148 cfa_store.offset is the offset from the value of
2149 cfa_store.reg to the actual CFA
2150 cfa_temp register holding an integral value. cfa_temp.offset
2151 stores the value, which will be used to adjust the
2152 stack pointer. cfa_temp is also used like cfa_store,
2153 to track stores to the stack via fp or a temp reg.
2155 Rules 1- 4: Setting a register's value to cfa.reg or an expression
2156 with cfa.reg as the first operand changes the cfa.reg and its
2157 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
2158 cfa_temp.offset.
2160 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
2161 expression yielding a constant. This sets cfa_temp.reg
2162 and cfa_temp.offset.
2164 Rule 5: Create a new register cfa_store used to save items to the
2165 stack.
2167 Rules 10-14: Save a register to the stack. Define offset as the
2168 difference of the original location and cfa_store's
2169 location (or cfa_temp's location if cfa_temp is used).
2171 Rules 16-20: If AND operation happens on sp in prologue, we assume
2172 stack is realigned. We will use a group of DW_OP_XXX
2173 expressions to represent the location of the stored
2174 register instead of CFA+offset.
2176 The Rules
2178 "{a,b}" indicates a choice of a xor b.
2179 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2181 Rule 1:
2182 (set <reg1> <reg2>:cfa.reg)
2183 effects: cfa.reg = <reg1>
2184 cfa.offset unchanged
2185 cfa_temp.reg = <reg1>
2186 cfa_temp.offset = cfa.offset
2188 Rule 2:
2189 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2190 {<const_int>,<reg>:cfa_temp.reg}))
2191 effects: cfa.reg = sp if fp used
2192 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2193 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2194 if cfa_store.reg==sp
2196 Rule 3:
2197 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2198 effects: cfa.reg = fp
2199 cfa_offset += +/- <const_int>
2201 Rule 4:
2202 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2203 constraints: <reg1> != fp
2204 <reg1> != sp
2205 effects: cfa.reg = <reg1>
2206 cfa_temp.reg = <reg1>
2207 cfa_temp.offset = cfa.offset
2209 Rule 5:
2210 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2211 constraints: <reg1> != fp
2212 <reg1> != sp
2213 effects: cfa_store.reg = <reg1>
2214 cfa_store.offset = cfa.offset - cfa_temp.offset
2216 Rule 6:
2217 (set <reg> <const_int>)
2218 effects: cfa_temp.reg = <reg>
2219 cfa_temp.offset = <const_int>
2221 Rule 7:
2222 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2223 effects: cfa_temp.reg = <reg1>
2224 cfa_temp.offset |= <const_int>
2226 Rule 8:
2227 (set <reg> (high <exp>))
2228 effects: none
2230 Rule 9:
2231 (set <reg> (lo_sum <exp> <const_int>))
2232 effects: cfa_temp.reg = <reg>
2233 cfa_temp.offset = <const_int>
2235 Rule 10:
2236 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2237 effects: cfa_store.offset -= <const_int>
2238 cfa.offset = cfa_store.offset if cfa.reg == sp
2239 cfa.reg = sp
2240 cfa.base_offset = -cfa_store.offset
2242 Rule 11:
2243 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2244 effects: cfa_store.offset += -/+ mode_size(mem)
2245 cfa.offset = cfa_store.offset if cfa.reg == sp
2246 cfa.reg = sp
2247 cfa.base_offset = -cfa_store.offset
2249 Rule 12:
2250 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2252 <reg2>)
2253 effects: cfa.reg = <reg1>
2254 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2256 Rule 13:
2257 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2258 effects: cfa.reg = <reg1>
2259 cfa.base_offset = -{cfa_store,cfa_temp}.offset
2261 Rule 14:
2262 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2263 effects: cfa.reg = <reg1>
2264 cfa.base_offset = -cfa_temp.offset
2265 cfa_temp.offset -= mode_size(mem)
2267 Rule 15:
2268 (set <reg> {unspec, unspec_volatile})
2269 effects: target-dependent
2271 Rule 16:
2272 (set sp (and: sp <const_int>))
2273 constraints: cfa_store.reg == sp
2274 effects: current_fde.stack_realign = 1
2275 cfa_store.offset = 0
2276 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2278 Rule 17:
2279 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2280 effects: cfa_store.offset += -/+ mode_size(mem)
2282 Rule 18:
2283 (set (mem ({pre_inc, pre_dec} sp)) fp)
2284 constraints: fde->stack_realign == 1
2285 effects: cfa_store.offset = 0
2286 cfa.reg != HARD_FRAME_POINTER_REGNUM
2288 Rule 19:
2289 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2290 constraints: fde->stack_realign == 1
2291 && cfa.offset == 0
2292 && cfa.indirect == 0
2293 && cfa.reg != HARD_FRAME_POINTER_REGNUM
2294 effects: Use DW_CFA_def_cfa_expression to define cfa
2295 cfa.reg == fde->drap_reg */
2297 static void
2298 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2300 rtx src, dest, span;
2301 HOST_WIDE_INT offset;
2302 dw_fde_ref fde;
2304 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2305 the PARALLEL independently. The first element is always processed if
2306 it is a SET. This is for backward compatibility. Other elements
2307 are processed only if they are SETs and the RTX_FRAME_RELATED_P
2308 flag is set in them. */
2309 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2311 int par_index;
2312 int limit = XVECLEN (expr, 0);
2313 rtx elem;
2315 /* PARALLELs have strict read-modify-write semantics, so we
2316 ought to evaluate every rvalue before changing any lvalue.
2317 It's cumbersome to do that in general, but there's an
2318 easy approximation that is enough for all current users:
2319 handle register saves before register assignments. */
2320 if (GET_CODE (expr) == PARALLEL)
2321 for (par_index = 0; par_index < limit; par_index++)
2323 elem = XVECEXP (expr, 0, par_index);
2324 if (GET_CODE (elem) == SET
2325 && MEM_P (SET_DEST (elem))
2326 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2327 dwarf2out_frame_debug_expr (elem, label);
2330 for (par_index = 0; par_index < limit; par_index++)
2332 elem = XVECEXP (expr, 0, par_index);
2333 if (GET_CODE (elem) == SET
2334 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2335 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2336 dwarf2out_frame_debug_expr (elem, label);
2337 else if (GET_CODE (elem) == SET
2338 && par_index != 0
2339 && !RTX_FRAME_RELATED_P (elem))
2341 /* Stack adjustment combining might combine some post-prologue
2342 stack adjustment into a prologue stack adjustment. */
2343 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2345 if (offset != 0)
2346 dwarf2out_stack_adjust (offset, label);
2349 return;
2352 gcc_assert (GET_CODE (expr) == SET);
2354 src = SET_SRC (expr);
2355 dest = SET_DEST (expr);
2357 if (REG_P (src))
2359 rtx rsi = reg_saved_in (src);
2360 if (rsi)
2361 src = rsi;
2364 fde = current_fde ();
2366 switch (GET_CODE (dest))
2368 case REG:
2369 switch (GET_CODE (src))
2371 /* Setting FP from SP. */
2372 case REG:
2373 if (cfa.reg == (unsigned) REGNO (src))
2375 /* Rule 1 */
2376 /* Update the CFA rule wrt SP or FP. Make sure src is
2377 relative to the current CFA register.
2379 We used to require that dest be either SP or FP, but the
2380 ARM copies SP to a temporary register, and from there to
2381 FP. So we just rely on the backends to only set
2382 RTX_FRAME_RELATED_P on appropriate insns. */
2383 cfa.reg = REGNO (dest);
2384 cfa_temp.reg = cfa.reg;
2385 cfa_temp.offset = cfa.offset;
2387 else
2389 /* Saving a register in a register. */
2390 gcc_assert (!fixed_regs [REGNO (dest)]
2391 /* For the SPARC and its register window. */
2392 || (DWARF_FRAME_REGNUM (REGNO (src))
2393 == DWARF_FRAME_RETURN_COLUMN));
2395 /* After stack is aligned, we can only save SP in FP
2396 if drap register is used. In this case, we have
2397 to restore stack pointer with the CFA value and we
2398 don't generate this DWARF information. */
2399 if (fde
2400 && fde->stack_realign
2401 && REGNO (src) == STACK_POINTER_REGNUM)
2402 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2403 && fde->drap_reg != INVALID_REGNUM
2404 && cfa.reg != REGNO (src));
2405 else
2406 queue_reg_save (label, src, dest, 0);
2408 break;
2410 case PLUS:
2411 case MINUS:
2412 case LO_SUM:
2413 if (dest == stack_pointer_rtx)
2415 /* Rule 2 */
2416 /* Adjusting SP. */
2417 switch (GET_CODE (XEXP (src, 1)))
2419 case CONST_INT:
2420 offset = INTVAL (XEXP (src, 1));
2421 break;
2422 case REG:
2423 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2424 == cfa_temp.reg);
2425 offset = cfa_temp.offset;
2426 break;
2427 default:
2428 gcc_unreachable ();
2431 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2433 /* Restoring SP from FP in the epilogue. */
2434 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2435 cfa.reg = STACK_POINTER_REGNUM;
2437 else if (GET_CODE (src) == LO_SUM)
2438 /* Assume we've set the source reg of the LO_SUM from sp. */
2440 else
2441 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2443 if (GET_CODE (src) != MINUS)
2444 offset = -offset;
2445 if (cfa.reg == STACK_POINTER_REGNUM)
2446 cfa.offset += offset;
2447 if (cfa_store.reg == STACK_POINTER_REGNUM)
2448 cfa_store.offset += offset;
2450 else if (dest == hard_frame_pointer_rtx)
2452 /* Rule 3 */
2453 /* Either setting the FP from an offset of the SP,
2454 or adjusting the FP */
2455 gcc_assert (frame_pointer_needed);
2457 gcc_assert (REG_P (XEXP (src, 0))
2458 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2459 && CONST_INT_P (XEXP (src, 1)));
2460 offset = INTVAL (XEXP (src, 1));
2461 if (GET_CODE (src) != MINUS)
2462 offset = -offset;
2463 cfa.offset += offset;
2464 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2466 else
2468 gcc_assert (GET_CODE (src) != MINUS);
2470 /* Rule 4 */
2471 if (REG_P (XEXP (src, 0))
2472 && REGNO (XEXP (src, 0)) == cfa.reg
2473 && CONST_INT_P (XEXP (src, 1)))
2475 /* Setting a temporary CFA register that will be copied
2476 into the FP later on. */
2477 offset = - INTVAL (XEXP (src, 1));
2478 cfa.offset += offset;
2479 cfa.reg = REGNO (dest);
2480 /* Or used to save regs to the stack. */
2481 cfa_temp.reg = cfa.reg;
2482 cfa_temp.offset = cfa.offset;
2485 /* Rule 5 */
2486 else if (REG_P (XEXP (src, 0))
2487 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2488 && XEXP (src, 1) == stack_pointer_rtx)
2490 /* Setting a scratch register that we will use instead
2491 of SP for saving registers to the stack. */
2492 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2493 cfa_store.reg = REGNO (dest);
2494 cfa_store.offset = cfa.offset - cfa_temp.offset;
2497 /* Rule 9 */
2498 else if (GET_CODE (src) == LO_SUM
2499 && CONST_INT_P (XEXP (src, 1)))
2501 cfa_temp.reg = REGNO (dest);
2502 cfa_temp.offset = INTVAL (XEXP (src, 1));
2504 else
2505 gcc_unreachable ();
2507 break;
2509 /* Rule 6 */
2510 case CONST_INT:
2511 cfa_temp.reg = REGNO (dest);
2512 cfa_temp.offset = INTVAL (src);
2513 break;
2515 /* Rule 7 */
2516 case IOR:
2517 gcc_assert (REG_P (XEXP (src, 0))
2518 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2519 && CONST_INT_P (XEXP (src, 1)));
2521 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2522 cfa_temp.reg = REGNO (dest);
2523 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2524 break;
2526 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2527 which will fill in all of the bits. */
2528 /* Rule 8 */
2529 case HIGH:
2530 break;
2532 /* Rule 15 */
2533 case UNSPEC:
2534 case UNSPEC_VOLATILE:
2535 gcc_assert (targetm.dwarf_handle_frame_unspec);
2536 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2537 return;
2539 /* Rule 16 */
2540 case AND:
2541 /* If this AND operation happens on stack pointer in prologue,
2542 we assume the stack is realigned and we extract the
2543 alignment. */
2544 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2546 /* We interpret reg_save differently with stack_realign set.
2547 Thus we must flush whatever we have queued first. */
2548 dwarf2out_flush_queued_reg_saves ();
2550 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2551 fde->stack_realign = 1;
2552 fde->stack_realignment = INTVAL (XEXP (src, 1));
2553 cfa_store.offset = 0;
2555 if (cfa.reg != STACK_POINTER_REGNUM
2556 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2557 fde->drap_reg = cfa.reg;
2559 return;
2561 default:
2562 gcc_unreachable ();
2565 def_cfa_1 (label, &cfa);
2566 break;
2568 case MEM:
2570 /* Saving a register to the stack. Make sure dest is relative to the
2571 CFA register. */
2572 switch (GET_CODE (XEXP (dest, 0)))
2574 /* Rule 10 */
2575 /* With a push. */
2576 case PRE_MODIFY:
2577 /* We can't handle variable size modifications. */
2578 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2579 == CONST_INT);
2580 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2582 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2583 && cfa_store.reg == STACK_POINTER_REGNUM);
2585 cfa_store.offset += offset;
2586 if (cfa.reg == STACK_POINTER_REGNUM)
2587 cfa.offset = cfa_store.offset;
2589 offset = -cfa_store.offset;
2590 break;
2592 /* Rule 11 */
2593 case PRE_INC:
2594 case PRE_DEC:
2595 offset = GET_MODE_SIZE (GET_MODE (dest));
2596 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2597 offset = -offset;
2599 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2600 == STACK_POINTER_REGNUM)
2601 && cfa_store.reg == STACK_POINTER_REGNUM);
2603 cfa_store.offset += offset;
2605 /* Rule 18: If stack is aligned, we will use FP as a
2606 reference to represent the address of the stored
2607 regiser. */
2608 if (fde
2609 && fde->stack_realign
2610 && src == hard_frame_pointer_rtx)
2612 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2613 cfa_store.offset = 0;
2616 if (cfa.reg == STACK_POINTER_REGNUM)
2617 cfa.offset = cfa_store.offset;
2619 offset = -cfa_store.offset;
2620 break;
2622 /* Rule 12 */
2623 /* With an offset. */
2624 case PLUS:
2625 case MINUS:
2626 case LO_SUM:
2628 int regno;
2630 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2631 && REG_P (XEXP (XEXP (dest, 0), 0)));
2632 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2633 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2634 offset = -offset;
2636 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2638 if (cfa.reg == (unsigned) regno)
2639 offset -= cfa.offset;
2640 else if (cfa_store.reg == (unsigned) regno)
2641 offset -= cfa_store.offset;
2642 else
2644 gcc_assert (cfa_temp.reg == (unsigned) regno);
2645 offset -= cfa_temp.offset;
2648 break;
2650 /* Rule 13 */
2651 /* Without an offset. */
2652 case REG:
2654 int regno = REGNO (XEXP (dest, 0));
2656 if (cfa.reg == (unsigned) regno)
2657 offset = -cfa.offset;
2658 else if (cfa_store.reg == (unsigned) regno)
2659 offset = -cfa_store.offset;
2660 else
2662 gcc_assert (cfa_temp.reg == (unsigned) regno);
2663 offset = -cfa_temp.offset;
2666 break;
2668 /* Rule 14 */
2669 case POST_INC:
2670 gcc_assert (cfa_temp.reg
2671 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2672 offset = -cfa_temp.offset;
2673 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2674 break;
2676 default:
2677 gcc_unreachable ();
2680 /* Rule 17 */
2681 /* If the source operand of this MEM operation is not a
2682 register, basically the source is return address. Here
2683 we only care how much stack grew and we don't save it. */
2684 if (!REG_P (src))
2685 break;
2687 if (REGNO (src) != STACK_POINTER_REGNUM
2688 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2689 && (unsigned) REGNO (src) == cfa.reg)
2691 /* We're storing the current CFA reg into the stack. */
2693 if (cfa.offset == 0)
2695 /* Rule 19 */
2696 /* If stack is aligned, putting CFA reg into stack means
2697 we can no longer use reg + offset to represent CFA.
2698 Here we use DW_CFA_def_cfa_expression instead. The
2699 result of this expression equals to the original CFA
2700 value. */
2701 if (fde
2702 && fde->stack_realign
2703 && cfa.indirect == 0
2704 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2706 dw_cfa_location cfa_exp;
2708 gcc_assert (fde->drap_reg == cfa.reg);
2710 cfa_exp.indirect = 1;
2711 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2712 cfa_exp.base_offset = offset;
2713 cfa_exp.offset = 0;
2715 fde->drap_reg_saved = 1;
2717 def_cfa_1 (label, &cfa_exp);
2718 break;
2721 /* If the source register is exactly the CFA, assume
2722 we're saving SP like any other register; this happens
2723 on the ARM. */
2724 def_cfa_1 (label, &cfa);
2725 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2726 break;
2728 else
2730 /* Otherwise, we'll need to look in the stack to
2731 calculate the CFA. */
2732 rtx x = XEXP (dest, 0);
2734 if (!REG_P (x))
2735 x = XEXP (x, 0);
2736 gcc_assert (REG_P (x));
2738 cfa.reg = REGNO (x);
2739 cfa.base_offset = offset;
2740 cfa.indirect = 1;
2741 def_cfa_1 (label, &cfa);
2742 break;
2746 def_cfa_1 (label, &cfa);
2748 span = targetm.dwarf_register_span (src);
2750 if (!span)
2751 queue_reg_save (label, src, NULL_RTX, offset);
2752 else
2754 /* We have a PARALLEL describing where the contents of SRC
2755 live. Queue register saves for each piece of the
2756 PARALLEL. */
2757 int par_index;
2758 int limit;
2759 HOST_WIDE_INT span_offset = offset;
2761 gcc_assert (GET_CODE (span) == PARALLEL);
2763 limit = XVECLEN (span, 0);
2764 for (par_index = 0; par_index < limit; par_index++)
2766 rtx elem = XVECEXP (span, 0, par_index);
2768 queue_reg_save (label, elem, NULL_RTX, span_offset);
2769 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2773 break;
2775 default:
2776 gcc_unreachable ();
2780 /* Record call frame debugging information for INSN, which either
2781 sets SP or FP (adjusting how we calculate the frame address) or saves a
2782 register to the stack. If INSN is NULL_RTX, initialize our state.
2784 If AFTER_P is false, we're being called before the insn is emitted,
2785 otherwise after. Call instructions get invoked twice. */
2787 void
2788 dwarf2out_frame_debug (rtx insn, bool after_p)
2790 const char *label;
2791 rtx note, n;
2792 bool handled_one = false;
2794 if (insn == NULL_RTX)
2796 size_t i;
2798 /* Flush any queued register saves. */
2799 dwarf2out_flush_queued_reg_saves ();
2801 /* Set up state for generating call frame debug info. */
2802 lookup_cfa (&cfa);
2803 gcc_assert (cfa.reg
2804 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2806 cfa.reg = STACK_POINTER_REGNUM;
2807 cfa_store = cfa;
2808 cfa_temp.reg = -1;
2809 cfa_temp.offset = 0;
2811 for (i = 0; i < num_regs_saved_in_regs; i++)
2813 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2814 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2816 num_regs_saved_in_regs = 0;
2818 if (barrier_args_size)
2820 XDELETEVEC (barrier_args_size);
2821 barrier_args_size = NULL;
2823 return;
2826 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2827 dwarf2out_flush_queued_reg_saves ();
2829 if (!RTX_FRAME_RELATED_P (insn))
2831 /* ??? This should be done unconditionally since stack adjustments
2832 matter if the stack pointer is not the CFA register anymore but
2833 is still used to save registers. */
2834 if (!ACCUMULATE_OUTGOING_ARGS)
2835 dwarf2out_notice_stack_adjust (insn, after_p);
2836 return;
2839 label = dwarf2out_cfi_label (false);
2840 any_cfis_emitted = false;
2842 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2843 switch (REG_NOTE_KIND (note))
2845 case REG_FRAME_RELATED_EXPR:
2846 insn = XEXP (note, 0);
2847 goto found;
2849 case REG_CFA_DEF_CFA:
2850 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2851 handled_one = true;
2852 break;
2854 case REG_CFA_ADJUST_CFA:
2855 n = XEXP (note, 0);
2856 if (n == NULL)
2858 n = PATTERN (insn);
2859 if (GET_CODE (n) == PARALLEL)
2860 n = XVECEXP (n, 0, 0);
2862 dwarf2out_frame_debug_adjust_cfa (n, label);
2863 handled_one = true;
2864 break;
2866 case REG_CFA_OFFSET:
2867 n = XEXP (note, 0);
2868 if (n == NULL)
2869 n = single_set (insn);
2870 dwarf2out_frame_debug_cfa_offset (n, label);
2871 handled_one = true;
2872 break;
2874 case REG_CFA_REGISTER:
2875 n = XEXP (note, 0);
2876 if (n == NULL)
2878 n = PATTERN (insn);
2879 if (GET_CODE (n) == PARALLEL)
2880 n = XVECEXP (n, 0, 0);
2882 dwarf2out_frame_debug_cfa_register (n, label);
2883 handled_one = true;
2884 break;
2886 case REG_CFA_EXPRESSION:
2887 n = XEXP (note, 0);
2888 if (n == NULL)
2889 n = single_set (insn);
2890 dwarf2out_frame_debug_cfa_expression (n, label);
2891 handled_one = true;
2892 break;
2894 case REG_CFA_RESTORE:
2895 n = XEXP (note, 0);
2896 if (n == NULL)
2898 n = PATTERN (insn);
2899 if (GET_CODE (n) == PARALLEL)
2900 n = XVECEXP (n, 0, 0);
2901 n = XEXP (n, 0);
2903 dwarf2out_frame_debug_cfa_restore (n, label);
2904 handled_one = true;
2905 break;
2907 case REG_CFA_SET_VDRAP:
2908 n = XEXP (note, 0);
2909 if (REG_P (n))
2911 dw_fde_ref fde = current_fde ();
2912 if (fde)
2914 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2915 if (REG_P (n))
2916 fde->vdrap_reg = REGNO (n);
2919 handled_one = true;
2920 break;
2922 default:
2923 break;
2925 if (handled_one)
2927 if (any_cfis_emitted)
2928 dwarf2out_flush_queued_reg_saves ();
2929 return;
2932 insn = PATTERN (insn);
2933 found:
2934 dwarf2out_frame_debug_expr (insn, label);
2936 /* Check again. A parallel can save and update the same register.
2937 We could probably check just once, here, but this is safer than
2938 removing the check above. */
2939 if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2940 dwarf2out_flush_queued_reg_saves ();
2943 /* Determine if we need to save and restore CFI information around this
2944 epilogue. If SIBCALL is true, then this is a sibcall epilogue. If
2945 we do need to save/restore, then emit the save now, and insert a
2946 NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream. */
2948 void
2949 dwarf2out_cfi_begin_epilogue (rtx insn)
2951 bool saw_frp = false;
2952 rtx i;
2954 /* Scan forward to the return insn, noticing if there are possible
2955 frame related insns. */
2956 for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2958 if (!INSN_P (i))
2959 continue;
2961 /* Look for both regular and sibcalls to end the block. */
2962 if (returnjump_p (i))
2963 break;
2964 if (CALL_P (i) && SIBLING_CALL_P (i))
2965 break;
2967 if (GET_CODE (PATTERN (i)) == SEQUENCE)
2969 int idx;
2970 rtx seq = PATTERN (i);
2972 if (returnjump_p (XVECEXP (seq, 0, 0)))
2973 break;
2974 if (CALL_P (XVECEXP (seq, 0, 0))
2975 && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2976 break;
2978 for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2979 if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2980 saw_frp = true;
2983 if (RTX_FRAME_RELATED_P (i))
2984 saw_frp = true;
2987 /* If the port doesn't emit epilogue unwind info, we don't need a
2988 save/restore pair. */
2989 if (!saw_frp)
2990 return;
2992 /* Otherwise, search forward to see if the return insn was the last
2993 basic block of the function. If so, we don't need save/restore. */
2994 gcc_assert (i != NULL);
2995 i = next_real_insn (i);
2996 if (i == NULL)
2997 return;
2999 /* Insert the restore before that next real insn in the stream, and before
3000 a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3001 properly nested. This should be after any label or alignment. This
3002 will be pushed into the CFI stream by the function below. */
3003 while (1)
3005 rtx p = PREV_INSN (i);
3006 if (!NOTE_P (p))
3007 break;
3008 if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3009 break;
3010 i = p;
3012 emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3014 emit_cfa_remember = true;
3016 /* And emulate the state save. */
3017 gcc_assert (!cfa_remember.in_use);
3018 cfa_remember = cfa;
3019 cfa_remember.in_use = 1;
3022 /* A "subroutine" of dwarf2out_cfi_begin_epilogue. Emit the restore
3023 required. */
3025 void
3026 dwarf2out_frame_debug_restore_state (void)
3028 dw_cfi_ref cfi = new_cfi ();
3029 const char *label = dwarf2out_cfi_label (false);
3031 cfi->dw_cfi_opc = DW_CFA_restore_state;
3032 add_fde_cfi (label, cfi);
3034 gcc_assert (cfa_remember.in_use);
3035 cfa = cfa_remember;
3036 cfa_remember.in_use = 0;
3039 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
3040 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3041 (enum dwarf_call_frame_info cfi);
3043 static enum dw_cfi_oprnd_type
3044 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3046 switch (cfi)
3048 case DW_CFA_nop:
3049 case DW_CFA_GNU_window_save:
3050 case DW_CFA_remember_state:
3051 case DW_CFA_restore_state:
3052 return dw_cfi_oprnd_unused;
3054 case DW_CFA_set_loc:
3055 case DW_CFA_advance_loc1:
3056 case DW_CFA_advance_loc2:
3057 case DW_CFA_advance_loc4:
3058 case DW_CFA_MIPS_advance_loc8:
3059 return dw_cfi_oprnd_addr;
3061 case DW_CFA_offset:
3062 case DW_CFA_offset_extended:
3063 case DW_CFA_def_cfa:
3064 case DW_CFA_offset_extended_sf:
3065 case DW_CFA_def_cfa_sf:
3066 case DW_CFA_restore:
3067 case DW_CFA_restore_extended:
3068 case DW_CFA_undefined:
3069 case DW_CFA_same_value:
3070 case DW_CFA_def_cfa_register:
3071 case DW_CFA_register:
3072 case DW_CFA_expression:
3073 return dw_cfi_oprnd_reg_num;
3075 case DW_CFA_def_cfa_offset:
3076 case DW_CFA_GNU_args_size:
3077 case DW_CFA_def_cfa_offset_sf:
3078 return dw_cfi_oprnd_offset;
3080 case DW_CFA_def_cfa_expression:
3081 return dw_cfi_oprnd_loc;
3083 default:
3084 gcc_unreachable ();
3088 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
3089 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3090 (enum dwarf_call_frame_info cfi);
3092 static enum dw_cfi_oprnd_type
3093 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3095 switch (cfi)
3097 case DW_CFA_def_cfa:
3098 case DW_CFA_def_cfa_sf:
3099 case DW_CFA_offset:
3100 case DW_CFA_offset_extended_sf:
3101 case DW_CFA_offset_extended:
3102 return dw_cfi_oprnd_offset;
3104 case DW_CFA_register:
3105 return dw_cfi_oprnd_reg_num;
3107 case DW_CFA_expression:
3108 return dw_cfi_oprnd_loc;
3110 default:
3111 return dw_cfi_oprnd_unused;
3115 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
3116 switch to the data section instead, and write out a synthetic start label
3117 for collect2 the first time around. */
3119 static void
3120 switch_to_eh_frame_section (bool back)
3122 tree label;
3124 #ifdef EH_FRAME_SECTION_NAME
3125 if (eh_frame_section == 0)
3127 int flags;
3129 if (EH_TABLES_CAN_BE_READ_ONLY)
3131 int fde_encoding;
3132 int per_encoding;
3133 int lsda_encoding;
3135 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3136 /*global=*/0);
3137 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3138 /*global=*/1);
3139 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3140 /*global=*/0);
3141 flags = ((! flag_pic
3142 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3143 && (fde_encoding & 0x70) != DW_EH_PE_aligned
3144 && (per_encoding & 0x70) != DW_EH_PE_absptr
3145 && (per_encoding & 0x70) != DW_EH_PE_aligned
3146 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3147 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3148 ? 0 : SECTION_WRITE);
3150 else
3151 flags = SECTION_WRITE;
3152 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3154 #endif /* EH_FRAME_SECTION_NAME */
3156 if (eh_frame_section)
3157 switch_to_section (eh_frame_section);
3158 else
3160 /* We have no special eh_frame section. Put the information in
3161 the data section and emit special labels to guide collect2. */
3162 switch_to_section (data_section);
3164 if (!back)
3166 label = get_file_function_name ("F");
3167 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3168 targetm.asm_out.globalize_label (asm_out_file,
3169 IDENTIFIER_POINTER (label));
3170 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3175 /* Switch [BACK] to the eh or debug frame table section, depending on
3176 FOR_EH. */
3178 static void
3179 switch_to_frame_table_section (int for_eh, bool back)
3181 if (for_eh)
3182 switch_to_eh_frame_section (back);
3183 else
3185 if (!debug_frame_section)
3186 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3187 SECTION_DEBUG, NULL);
3188 switch_to_section (debug_frame_section);
3192 /* Output a Call Frame Information opcode and its operand(s). */
3194 static void
3195 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3197 unsigned long r;
3198 HOST_WIDE_INT off;
3200 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3201 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3202 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3203 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3204 ((unsigned HOST_WIDE_INT)
3205 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3206 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3208 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3209 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3210 "DW_CFA_offset, column %#lx", r);
3211 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3212 dw2_asm_output_data_uleb128 (off, NULL);
3214 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3216 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3217 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3218 "DW_CFA_restore, column %#lx", r);
3220 else
3222 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3223 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3225 switch (cfi->dw_cfi_opc)
3227 case DW_CFA_set_loc:
3228 if (for_eh)
3229 dw2_asm_output_encoded_addr_rtx (
3230 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3231 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3232 false, NULL);
3233 else
3234 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3235 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3236 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3237 break;
3239 case DW_CFA_advance_loc1:
3240 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3241 fde->dw_fde_current_label, NULL);
3242 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3243 break;
3245 case DW_CFA_advance_loc2:
3246 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3247 fde->dw_fde_current_label, NULL);
3248 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3249 break;
3251 case DW_CFA_advance_loc4:
3252 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3253 fde->dw_fde_current_label, NULL);
3254 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3255 break;
3257 case DW_CFA_MIPS_advance_loc8:
3258 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3259 fde->dw_fde_current_label, NULL);
3260 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3261 break;
3263 case DW_CFA_offset_extended:
3264 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3265 dw2_asm_output_data_uleb128 (r, NULL);
3266 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3267 dw2_asm_output_data_uleb128 (off, NULL);
3268 break;
3270 case DW_CFA_def_cfa:
3271 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3272 dw2_asm_output_data_uleb128 (r, NULL);
3273 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3274 break;
3276 case DW_CFA_offset_extended_sf:
3277 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3278 dw2_asm_output_data_uleb128 (r, NULL);
3279 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3280 dw2_asm_output_data_sleb128 (off, NULL);
3281 break;
3283 case DW_CFA_def_cfa_sf:
3284 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3285 dw2_asm_output_data_uleb128 (r, NULL);
3286 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3287 dw2_asm_output_data_sleb128 (off, NULL);
3288 break;
3290 case DW_CFA_restore_extended:
3291 case DW_CFA_undefined:
3292 case DW_CFA_same_value:
3293 case DW_CFA_def_cfa_register:
3294 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3295 dw2_asm_output_data_uleb128 (r, NULL);
3296 break;
3298 case DW_CFA_register:
3299 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3300 dw2_asm_output_data_uleb128 (r, NULL);
3301 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3302 dw2_asm_output_data_uleb128 (r, NULL);
3303 break;
3305 case DW_CFA_def_cfa_offset:
3306 case DW_CFA_GNU_args_size:
3307 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3308 break;
3310 case DW_CFA_def_cfa_offset_sf:
3311 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3312 dw2_asm_output_data_sleb128 (off, NULL);
3313 break;
3315 case DW_CFA_GNU_window_save:
3316 break;
3318 case DW_CFA_def_cfa_expression:
3319 case DW_CFA_expression:
3320 output_cfa_loc (cfi);
3321 break;
3323 case DW_CFA_GNU_negative_offset_extended:
3324 /* Obsoleted by DW_CFA_offset_extended_sf. */
3325 gcc_unreachable ();
3327 default:
3328 break;
3333 /* Similar, but do it via assembler directives instead. */
3335 static void
3336 output_cfi_directive (dw_cfi_ref cfi)
3338 unsigned long r, r2;
3340 switch (cfi->dw_cfi_opc)
3342 case DW_CFA_advance_loc:
3343 case DW_CFA_advance_loc1:
3344 case DW_CFA_advance_loc2:
3345 case DW_CFA_advance_loc4:
3346 case DW_CFA_MIPS_advance_loc8:
3347 case DW_CFA_set_loc:
3348 /* Should only be created by add_fde_cfi in a code path not
3349 followed when emitting via directives. The assembler is
3350 going to take care of this for us. */
3351 gcc_unreachable ();
3353 case DW_CFA_offset:
3354 case DW_CFA_offset_extended:
3355 case DW_CFA_offset_extended_sf:
3356 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3357 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3358 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3359 break;
3361 case DW_CFA_restore:
3362 case DW_CFA_restore_extended:
3363 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3364 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3365 break;
3367 case DW_CFA_undefined:
3368 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3369 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3370 break;
3372 case DW_CFA_same_value:
3373 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3374 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3375 break;
3377 case DW_CFA_def_cfa:
3378 case DW_CFA_def_cfa_sf:
3379 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3380 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3381 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3382 break;
3384 case DW_CFA_def_cfa_register:
3385 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3386 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3387 break;
3389 case DW_CFA_register:
3390 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3391 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3392 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3393 break;
3395 case DW_CFA_def_cfa_offset:
3396 case DW_CFA_def_cfa_offset_sf:
3397 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3398 HOST_WIDE_INT_PRINT_DEC"\n",
3399 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3400 break;
3402 case DW_CFA_remember_state:
3403 fprintf (asm_out_file, "\t.cfi_remember_state\n");
3404 break;
3405 case DW_CFA_restore_state:
3406 fprintf (asm_out_file, "\t.cfi_restore_state\n");
3407 break;
3409 case DW_CFA_GNU_args_size:
3410 fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3411 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3412 if (flag_debug_asm)
3413 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3414 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3415 fputc ('\n', asm_out_file);
3416 break;
3418 case DW_CFA_GNU_window_save:
3419 fprintf (asm_out_file, "\t.cfi_window_save\n");
3420 break;
3422 case DW_CFA_def_cfa_expression:
3423 case DW_CFA_expression:
3424 fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3425 output_cfa_loc_raw (cfi);
3426 fputc ('\n', asm_out_file);
3427 break;
3429 default:
3430 gcc_unreachable ();
3434 DEF_VEC_P (dw_cfi_ref);
3435 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3437 /* Output CFIs to bring current FDE to the same state as after executing
3438 CFIs in CFI chain. DO_CFI_ASM is true if .cfi_* directives shall
3439 be emitted, false otherwise. If it is false, FDE and FOR_EH are the
3440 other arguments to pass to output_cfi. */
3442 static void
3443 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3445 struct dw_cfi_struct cfi_buf;
3446 dw_cfi_ref cfi2;
3447 dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3448 VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3449 unsigned int len, idx;
3451 for (;; cfi = cfi->dw_cfi_next)
3452 switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3454 case DW_CFA_advance_loc:
3455 case DW_CFA_advance_loc1:
3456 case DW_CFA_advance_loc2:
3457 case DW_CFA_advance_loc4:
3458 case DW_CFA_MIPS_advance_loc8:
3459 case DW_CFA_set_loc:
3460 /* All advances should be ignored. */
3461 break;
3462 case DW_CFA_remember_state:
3464 dw_cfi_ref args_size = cfi_args_size;
3466 /* Skip everything between .cfi_remember_state and
3467 .cfi_restore_state. */
3468 for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3469 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3470 break;
3471 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3472 args_size = cfi2;
3473 else
3474 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3476 if (cfi2 == NULL)
3477 goto flush_all;
3478 else
3480 cfi = cfi2;
3481 cfi_args_size = args_size;
3483 break;
3485 case DW_CFA_GNU_args_size:
3486 cfi_args_size = cfi;
3487 break;
3488 case DW_CFA_GNU_window_save:
3489 goto flush_all;
3490 case DW_CFA_offset:
3491 case DW_CFA_offset_extended:
3492 case DW_CFA_offset_extended_sf:
3493 case DW_CFA_restore:
3494 case DW_CFA_restore_extended:
3495 case DW_CFA_undefined:
3496 case DW_CFA_same_value:
3497 case DW_CFA_register:
3498 case DW_CFA_val_offset:
3499 case DW_CFA_val_offset_sf:
3500 case DW_CFA_expression:
3501 case DW_CFA_val_expression:
3502 case DW_CFA_GNU_negative_offset_extended:
3503 if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3504 VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3505 cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3506 VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3507 break;
3508 case DW_CFA_def_cfa:
3509 case DW_CFA_def_cfa_sf:
3510 case DW_CFA_def_cfa_expression:
3511 cfi_cfa = cfi;
3512 cfi_cfa_offset = cfi;
3513 break;
3514 case DW_CFA_def_cfa_register:
3515 cfi_cfa = cfi;
3516 break;
3517 case DW_CFA_def_cfa_offset:
3518 case DW_CFA_def_cfa_offset_sf:
3519 cfi_cfa_offset = cfi;
3520 break;
3521 case DW_CFA_nop:
3522 gcc_assert (cfi == NULL);
3523 flush_all:
3524 len = VEC_length (dw_cfi_ref, regs);
3525 for (idx = 0; idx < len; idx++)
3527 cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3528 if (cfi2 != NULL
3529 && cfi2->dw_cfi_opc != DW_CFA_restore
3530 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3532 if (do_cfi_asm)
3533 output_cfi_directive (cfi2);
3534 else
3535 output_cfi (cfi2, fde, for_eh);
3538 if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3540 gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3541 cfi_buf = *cfi_cfa;
3542 switch (cfi_cfa_offset->dw_cfi_opc)
3544 case DW_CFA_def_cfa_offset:
3545 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3546 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3547 break;
3548 case DW_CFA_def_cfa_offset_sf:
3549 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3550 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3551 break;
3552 case DW_CFA_def_cfa:
3553 case DW_CFA_def_cfa_sf:
3554 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3555 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3556 break;
3557 default:
3558 gcc_unreachable ();
3560 cfi_cfa = &cfi_buf;
3562 else if (cfi_cfa_offset)
3563 cfi_cfa = cfi_cfa_offset;
3564 if (cfi_cfa)
3566 if (do_cfi_asm)
3567 output_cfi_directive (cfi_cfa);
3568 else
3569 output_cfi (cfi_cfa, fde, for_eh);
3571 cfi_cfa = NULL;
3572 cfi_cfa_offset = NULL;
3573 if (cfi_args_size
3574 && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3576 if (do_cfi_asm)
3577 output_cfi_directive (cfi_args_size);
3578 else
3579 output_cfi (cfi_args_size, fde, for_eh);
3581 cfi_args_size = NULL;
3582 if (cfi == NULL)
3584 VEC_free (dw_cfi_ref, heap, regs);
3585 return;
3587 else if (do_cfi_asm)
3588 output_cfi_directive (cfi);
3589 else
3590 output_cfi (cfi, fde, for_eh);
3591 break;
3592 default:
3593 gcc_unreachable ();
3597 /* Output one FDE. */
3599 static void
3600 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3601 char *section_start_label, int fde_encoding, char *augmentation,
3602 bool any_lsda_needed, int lsda_encoding)
3604 const char *begin, *end;
3605 static unsigned int j;
3606 char l1[20], l2[20];
3607 dw_cfi_ref cfi;
3609 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3610 /* empty */ 0);
3611 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3612 for_eh + j);
3613 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3614 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3615 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3616 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3617 " indicating 64-bit DWARF extension");
3618 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3619 "FDE Length");
3620 ASM_OUTPUT_LABEL (asm_out_file, l1);
3622 if (for_eh)
3623 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3624 else
3625 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3626 debug_frame_section, "FDE CIE offset");
3628 if (!fde->dw_fde_switched_sections)
3630 begin = fde->dw_fde_begin;
3631 end = fde->dw_fde_end;
3633 else
3635 /* For the first section, prefer dw_fde_begin over
3636 dw_fde_{hot,cold}_section_label, as the latter
3637 might be separated from the real start of the
3638 function by alignment padding. */
3639 if (!second)
3640 begin = fde->dw_fde_begin;
3641 else if (fde->dw_fde_switched_cold_to_hot)
3642 begin = fde->dw_fde_hot_section_label;
3643 else
3644 begin = fde->dw_fde_unlikely_section_label;
3645 if (second ^ fde->dw_fde_switched_cold_to_hot)
3646 end = fde->dw_fde_unlikely_section_end_label;
3647 else
3648 end = fde->dw_fde_hot_section_end_label;
3651 if (for_eh)
3653 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3654 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3655 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3656 "FDE initial location");
3657 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3658 end, begin, "FDE address range");
3660 else
3662 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3663 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3666 if (augmentation[0])
3668 if (any_lsda_needed)
3670 int size = size_of_encoded_value (lsda_encoding);
3672 if (lsda_encoding == DW_EH_PE_aligned)
3674 int offset = ( 4 /* Length */
3675 + 4 /* CIE offset */
3676 + 2 * size_of_encoded_value (fde_encoding)
3677 + 1 /* Augmentation size */ );
3678 int pad = -offset & (PTR_SIZE - 1);
3680 size += pad;
3681 gcc_assert (size_of_uleb128 (size) == 1);
3684 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3686 if (fde->uses_eh_lsda)
3688 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3689 fde->funcdef_number);
3690 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3691 gen_rtx_SYMBOL_REF (Pmode, l1),
3692 false,
3693 "Language Specific Data Area");
3695 else
3697 if (lsda_encoding == DW_EH_PE_aligned)
3698 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3699 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3700 "Language Specific Data Area (none)");
3703 else
3704 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3707 /* Loop through the Call Frame Instructions associated with
3708 this FDE. */
3709 fde->dw_fde_current_label = begin;
3710 if (!fde->dw_fde_switched_sections)
3711 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3712 output_cfi (cfi, fde, for_eh);
3713 else if (!second)
3715 if (fde->dw_fde_switch_cfi)
3716 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3718 output_cfi (cfi, fde, for_eh);
3719 if (cfi == fde->dw_fde_switch_cfi)
3720 break;
3723 else
3725 dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3727 if (fde->dw_fde_switch_cfi)
3729 cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3730 fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3731 output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3732 fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3734 for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3735 output_cfi (cfi, fde, for_eh);
3738 /* If we are to emit a ref/link from function bodies to their frame tables,
3739 do it now. This is typically performed to make sure that tables
3740 associated with functions are dragged with them and not discarded in
3741 garbage collecting links. We need to do this on a per function basis to
3742 cope with -ffunction-sections. */
3744 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3745 /* Switch to the function section, emit the ref to the tables, and
3746 switch *back* into the table section. */
3747 switch_to_section (function_section (fde->decl));
3748 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3749 switch_to_frame_table_section (for_eh, true);
3750 #endif
3752 /* Pad the FDE out to an address sized boundary. */
3753 ASM_OUTPUT_ALIGN (asm_out_file,
3754 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3755 ASM_OUTPUT_LABEL (asm_out_file, l2);
3757 j += 2;
3760 /* Return true if frame description entry FDE is needed for EH. */
3762 static bool
3763 fde_needed_for_eh_p (dw_fde_ref fde)
3765 if (flag_asynchronous_unwind_tables)
3766 return true;
3768 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3769 return true;
3771 if (fde->uses_eh_lsda)
3772 return true;
3774 /* If exceptions are enabled, we have collected nothrow info. */
3775 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3776 return false;
3778 return true;
3781 /* Output the call frame information used to record information
3782 that relates to calculating the frame pointer, and records the
3783 location of saved registers. */
3785 static void
3786 output_call_frame_info (int for_eh)
3788 unsigned int i;
3789 dw_fde_ref fde;
3790 dw_cfi_ref cfi;
3791 char l1[20], l2[20], section_start_label[20];
3792 bool any_lsda_needed = false;
3793 char augmentation[6];
3794 int augmentation_size;
3795 int fde_encoding = DW_EH_PE_absptr;
3796 int per_encoding = DW_EH_PE_absptr;
3797 int lsda_encoding = DW_EH_PE_absptr;
3798 int return_reg;
3799 rtx personality = NULL;
3800 int dw_cie_version;
3802 /* Don't emit a CIE if there won't be any FDEs. */
3803 if (fde_table_in_use == 0)
3804 return;
3806 /* Nothing to do if the assembler's doing it all. */
3807 if (dwarf2out_do_cfi_asm ())
3808 return;
3810 /* If we don't have any functions we'll want to unwind out of, don't emit
3811 any EH unwind information. If we make FDEs linkonce, we may have to
3812 emit an empty label for an FDE that wouldn't otherwise be emitted. We
3813 want to avoid having an FDE kept around when the function it refers to
3814 is discarded. Example where this matters: a primary function template
3815 in C++ requires EH information, an explicit specialization doesn't. */
3816 if (for_eh)
3818 bool any_eh_needed = false;
3820 for (i = 0; i < fde_table_in_use; i++)
3821 if (fde_table[i].uses_eh_lsda)
3822 any_eh_needed = any_lsda_needed = true;
3823 else if (fde_needed_for_eh_p (&fde_table[i]))
3824 any_eh_needed = true;
3825 else if (TARGET_USES_WEAK_UNWIND_INFO)
3826 targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3827 1, 1);
3829 if (!any_eh_needed)
3830 return;
3833 /* We're going to be generating comments, so turn on app. */
3834 if (flag_debug_asm)
3835 app_enable ();
3837 /* Switch to the proper frame section, first time. */
3838 switch_to_frame_table_section (for_eh, false);
3840 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3841 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3843 /* Output the CIE. */
3844 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3845 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3846 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3847 dw2_asm_output_data (4, 0xffffffff,
3848 "Initial length escape value indicating 64-bit DWARF extension");
3849 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3850 "Length of Common Information Entry");
3851 ASM_OUTPUT_LABEL (asm_out_file, l1);
3853 /* Now that the CIE pointer is PC-relative for EH,
3854 use 0 to identify the CIE. */
3855 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3856 (for_eh ? 0 : DWARF_CIE_ID),
3857 "CIE Identifier Tag");
3859 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3860 use CIE version 1, unless that would produce incorrect results
3861 due to overflowing the return register column. */
3862 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3863 dw_cie_version = 1;
3864 if (return_reg >= 256 || dwarf_version > 2)
3865 dw_cie_version = 3;
3866 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3868 augmentation[0] = 0;
3869 augmentation_size = 0;
3871 personality = current_unit_personality;
3872 if (for_eh)
3874 char *p;
3876 /* Augmentation:
3877 z Indicates that a uleb128 is present to size the
3878 augmentation section.
3879 L Indicates the encoding (and thus presence) of
3880 an LSDA pointer in the FDE augmentation.
3881 R Indicates a non-default pointer encoding for
3882 FDE code pointers.
3883 P Indicates the presence of an encoding + language
3884 personality routine in the CIE augmentation. */
3886 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3887 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3888 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3890 p = augmentation + 1;
3891 if (personality)
3893 *p++ = 'P';
3894 augmentation_size += 1 + size_of_encoded_value (per_encoding);
3895 assemble_external_libcall (personality);
3897 if (any_lsda_needed)
3899 *p++ = 'L';
3900 augmentation_size += 1;
3902 if (fde_encoding != DW_EH_PE_absptr)
3904 *p++ = 'R';
3905 augmentation_size += 1;
3907 if (p > augmentation + 1)
3909 augmentation[0] = 'z';
3910 *p = '\0';
3913 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
3914 if (personality && per_encoding == DW_EH_PE_aligned)
3916 int offset = ( 4 /* Length */
3917 + 4 /* CIE Id */
3918 + 1 /* CIE version */
3919 + strlen (augmentation) + 1 /* Augmentation */
3920 + size_of_uleb128 (1) /* Code alignment */
3921 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3922 + 1 /* RA column */
3923 + 1 /* Augmentation size */
3924 + 1 /* Personality encoding */ );
3925 int pad = -offset & (PTR_SIZE - 1);
3927 augmentation_size += pad;
3929 /* Augmentations should be small, so there's scarce need to
3930 iterate for a solution. Die if we exceed one uleb128 byte. */
3931 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3935 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3936 if (dw_cie_version >= 4)
3938 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3939 dw2_asm_output_data (1, 0, "CIE Segment Size");
3941 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3942 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3943 "CIE Data Alignment Factor");
3945 if (dw_cie_version == 1)
3946 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3947 else
3948 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3950 if (augmentation[0])
3952 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3953 if (personality)
3955 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3956 eh_data_format_name (per_encoding));
3957 dw2_asm_output_encoded_addr_rtx (per_encoding,
3958 personality,
3959 true, NULL);
3962 if (any_lsda_needed)
3963 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3964 eh_data_format_name (lsda_encoding));
3966 if (fde_encoding != DW_EH_PE_absptr)
3967 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3968 eh_data_format_name (fde_encoding));
3971 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3972 output_cfi (cfi, NULL, for_eh);
3974 /* Pad the CIE out to an address sized boundary. */
3975 ASM_OUTPUT_ALIGN (asm_out_file,
3976 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3977 ASM_OUTPUT_LABEL (asm_out_file, l2);
3979 /* Loop through all of the FDE's. */
3980 for (i = 0; i < fde_table_in_use; i++)
3982 unsigned int k;
3983 fde = &fde_table[i];
3985 /* Don't emit EH unwind info for leaf functions that don't need it. */
3986 if (for_eh && !fde_needed_for_eh_p (fde))
3987 continue;
3989 for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3990 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3991 augmentation, any_lsda_needed, lsda_encoding);
3994 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3995 dw2_asm_output_data (4, 0, "End of Table");
3996 #ifdef MIPS_DEBUGGING_INFO
3997 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3998 get a value of 0. Putting .align 0 after the label fixes it. */
3999 ASM_OUTPUT_ALIGN (asm_out_file, 0);
4000 #endif
4002 /* Turn off app to make assembly quicker. */
4003 if (flag_debug_asm)
4004 app_disable ();
4007 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
4009 static void
4010 dwarf2out_do_cfi_startproc (bool second)
4012 int enc;
4013 rtx ref;
4014 rtx personality = get_personality_function (current_function_decl);
4016 fprintf (asm_out_file, "\t.cfi_startproc\n");
4018 if (personality)
4020 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4021 ref = personality;
4023 /* ??? The GAS support isn't entirely consistent. We have to
4024 handle indirect support ourselves, but PC-relative is done
4025 in the assembler. Further, the assembler can't handle any
4026 of the weirder relocation types. */
4027 if (enc & DW_EH_PE_indirect)
4028 ref = dw2_force_const_mem (ref, true);
4030 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4031 output_addr_const (asm_out_file, ref);
4032 fputc ('\n', asm_out_file);
4035 if (crtl->uses_eh_lsda)
4037 char lab[20];
4039 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4040 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4041 current_function_funcdef_no);
4042 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4043 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4045 if (enc & DW_EH_PE_indirect)
4046 ref = dw2_force_const_mem (ref, true);
4048 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4049 output_addr_const (asm_out_file, ref);
4050 fputc ('\n', asm_out_file);
4054 /* Output a marker (i.e. a label) for the beginning of a function, before
4055 the prologue. */
4057 void
4058 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4059 const char *file ATTRIBUTE_UNUSED)
4061 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4062 char * dup_label;
4063 dw_fde_ref fde;
4064 section *fnsec;
4065 bool do_frame;
4067 current_function_func_begin_label = NULL;
4069 do_frame = dwarf2out_do_frame ();
4071 /* ??? current_function_func_begin_label is also used by except.c for
4072 call-site information. We must emit this label if it might be used. */
4073 if (!do_frame
4074 && (!flag_exceptions
4075 || targetm.except_unwind_info (&global_options) != UI_TARGET))
4076 return;
4078 fnsec = function_section (current_function_decl);
4079 switch_to_section (fnsec);
4080 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4081 current_function_funcdef_no);
4082 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4083 current_function_funcdef_no);
4084 dup_label = xstrdup (label);
4085 current_function_func_begin_label = dup_label;
4087 /* We can elide the fde allocation if we're not emitting debug info. */
4088 if (!do_frame)
4089 return;
4091 /* Expand the fde table if necessary. */
4092 if (fde_table_in_use == fde_table_allocated)
4094 fde_table_allocated += FDE_TABLE_INCREMENT;
4095 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4096 memset (fde_table + fde_table_in_use, 0,
4097 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4100 /* Record the FDE associated with this function. */
4101 current_funcdef_fde = fde_table_in_use;
4103 /* Add the new FDE at the end of the fde_table. */
4104 fde = &fde_table[fde_table_in_use++];
4105 fde->decl = current_function_decl;
4106 fde->dw_fde_begin = dup_label;
4107 fde->dw_fde_current_label = dup_label;
4108 fde->dw_fde_hot_section_label = NULL;
4109 fde->dw_fde_hot_section_end_label = NULL;
4110 fde->dw_fde_unlikely_section_label = NULL;
4111 fde->dw_fde_unlikely_section_end_label = NULL;
4112 fde->dw_fde_switched_sections = 0;
4113 fde->dw_fde_switched_cold_to_hot = 0;
4114 fde->dw_fde_end = NULL;
4115 fde->dw_fde_vms_end_prologue = NULL;
4116 fde->dw_fde_vms_begin_epilogue = NULL;
4117 fde->dw_fde_cfi = NULL;
4118 fde->dw_fde_switch_cfi = NULL;
4119 fde->funcdef_number = current_function_funcdef_no;
4120 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4121 fde->uses_eh_lsda = crtl->uses_eh_lsda;
4122 fde->nothrow = crtl->nothrow;
4123 fde->drap_reg = INVALID_REGNUM;
4124 fde->vdrap_reg = INVALID_REGNUM;
4125 if (flag_reorder_blocks_and_partition)
4127 section *unlikelysec;
4128 if (first_function_block_is_cold)
4129 fde->in_std_section = 1;
4130 else
4131 fde->in_std_section
4132 = (fnsec == text_section
4133 || (cold_text_section && fnsec == cold_text_section));
4134 unlikelysec = unlikely_text_section ();
4135 fde->cold_in_std_section
4136 = (unlikelysec == text_section
4137 || (cold_text_section && unlikelysec == cold_text_section));
4139 else
4141 fde->in_std_section
4142 = (fnsec == text_section
4143 || (cold_text_section && fnsec == cold_text_section));
4144 fde->cold_in_std_section = 0;
4147 args_size = old_args_size = 0;
4149 /* We only want to output line number information for the genuine dwarf2
4150 prologue case, not the eh frame case. */
4151 #ifdef DWARF2_DEBUGGING_INFO
4152 if (file)
4153 dwarf2out_source_line (line, file, 0, true);
4154 #endif
4156 if (dwarf2out_do_cfi_asm ())
4157 dwarf2out_do_cfi_startproc (false);
4158 else
4160 rtx personality = get_personality_function (current_function_decl);
4161 if (!current_unit_personality)
4162 current_unit_personality = personality;
4164 /* We cannot keep a current personality per function as without CFI
4165 asm, at the point where we emit the CFI data, there is no current
4166 function anymore. */
4167 if (personality && current_unit_personality != personality)
4168 sorry ("multiple EH personalities are supported only with assemblers "
4169 "supporting .cfi_personality directive");
4173 /* Output a marker (i.e. a label) for the end of the generated code
4174 for a function prologue. This gets called *after* the prologue code has
4175 been generated. */
4177 void
4178 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4179 const char *file ATTRIBUTE_UNUSED)
4181 dw_fde_ref fde;
4182 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4184 /* Output a label to mark the endpoint of the code generated for this
4185 function. */
4186 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4187 current_function_funcdef_no);
4188 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4189 current_function_funcdef_no);
4190 fde = &fde_table[fde_table_in_use - 1];
4191 fde->dw_fde_vms_end_prologue = xstrdup (label);
4194 /* Output a marker (i.e. a label) for the beginning of the generated code
4195 for a function epilogue. This gets called *before* the prologue code has
4196 been generated. */
4198 void
4199 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4200 const char *file ATTRIBUTE_UNUSED)
4202 dw_fde_ref fde;
4203 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4205 fde = &fde_table[fde_table_in_use - 1];
4206 if (fde->dw_fde_vms_begin_epilogue)
4207 return;
4209 /* Output a label to mark the endpoint of the code generated for this
4210 function. */
4211 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4212 current_function_funcdef_no);
4213 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4214 current_function_funcdef_no);
4215 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4218 /* Output a marker (i.e. a label) for the absolute end of the generated code
4219 for a function definition. This gets called *after* the epilogue code has
4220 been generated. */
4222 void
4223 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4224 const char *file ATTRIBUTE_UNUSED)
4226 dw_fde_ref fde;
4227 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4229 last_var_location_insn = NULL_RTX;
4231 if (dwarf2out_do_cfi_asm ())
4232 fprintf (asm_out_file, "\t.cfi_endproc\n");
4234 /* Output a label to mark the endpoint of the code generated for this
4235 function. */
4236 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4237 current_function_funcdef_no);
4238 ASM_OUTPUT_LABEL (asm_out_file, label);
4239 fde = current_fde ();
4240 gcc_assert (fde != NULL);
4241 fde->dw_fde_end = xstrdup (label);
4244 void
4245 dwarf2out_frame_init (void)
4247 /* Allocate the initial hunk of the fde_table. */
4248 fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4249 fde_table_allocated = FDE_TABLE_INCREMENT;
4250 fde_table_in_use = 0;
4252 /* Generate the CFA instructions common to all FDE's. Do it now for the
4253 sake of lookup_cfa. */
4255 /* On entry, the Canonical Frame Address is at SP. */
4256 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4258 if (targetm.debug_unwind_info () == UI_DWARF2
4259 || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4260 initial_return_save (INCOMING_RETURN_ADDR_RTX);
4263 void
4264 dwarf2out_frame_finish (void)
4266 /* Output call frame information. */
4267 if (targetm.debug_unwind_info () == UI_DWARF2)
4268 output_call_frame_info (0);
4270 /* Output another copy for the unwinder. */
4271 if ((flag_unwind_tables || flag_exceptions)
4272 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4273 output_call_frame_info (1);
4276 /* Note that the current function section is being used for code. */
4278 static void
4279 dwarf2out_note_section_used (void)
4281 section *sec = current_function_section ();
4282 if (sec == text_section)
4283 text_section_used = true;
4284 else if (sec == cold_text_section)
4285 cold_text_section_used = true;
4288 void
4289 dwarf2out_switch_text_section (void)
4291 dw_fde_ref fde = current_fde ();
4293 gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4295 fde->dw_fde_switched_sections = 1;
4296 fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4298 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4299 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4300 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4301 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4302 have_multiple_function_sections = true;
4304 /* Reset the current label on switching text sections, so that we
4305 don't attempt to advance_loc4 between labels in different sections. */
4306 fde->dw_fde_current_label = NULL;
4308 /* There is no need to mark used sections when not debugging. */
4309 if (cold_text_section != NULL)
4310 dwarf2out_note_section_used ();
4312 if (dwarf2out_do_cfi_asm ())
4313 fprintf (asm_out_file, "\t.cfi_endproc\n");
4315 /* Now do the real section switch. */
4316 switch_to_section (current_function_section ());
4318 if (dwarf2out_do_cfi_asm ())
4320 dwarf2out_do_cfi_startproc (true);
4321 /* As this is a different FDE, insert all current CFI instructions
4322 again. */
4323 output_cfis (fde->dw_fde_cfi, true, fde, true);
4325 else
4327 dw_cfi_ref cfi = fde->dw_fde_cfi;
4329 cfi = fde->dw_fde_cfi;
4330 if (cfi)
4331 while (cfi->dw_cfi_next != NULL)
4332 cfi = cfi->dw_cfi_next;
4333 fde->dw_fde_switch_cfi = cfi;
4337 /* And now, the subset of the debugging information support code necessary
4338 for emitting location expressions. */
4340 /* Data about a single source file. */
4341 struct GTY(()) dwarf_file_data {
4342 const char * filename;
4343 int emitted_number;
4346 typedef struct dw_val_struct *dw_val_ref;
4347 typedef struct die_struct *dw_die_ref;
4348 typedef const struct die_struct *const_dw_die_ref;
4349 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4350 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4352 typedef struct GTY(()) deferred_locations_struct
4354 tree variable;
4355 dw_die_ref die;
4356 } deferred_locations;
4358 DEF_VEC_O(deferred_locations);
4359 DEF_VEC_ALLOC_O(deferred_locations,gc);
4361 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4363 DEF_VEC_P(dw_die_ref);
4364 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4366 /* Each DIE may have a series of attribute/value pairs. Values
4367 can take on several forms. The forms that are used in this
4368 implementation are listed below. */
4370 enum dw_val_class
4372 dw_val_class_addr,
4373 dw_val_class_offset,
4374 dw_val_class_loc,
4375 dw_val_class_loc_list,
4376 dw_val_class_range_list,
4377 dw_val_class_const,
4378 dw_val_class_unsigned_const,
4379 dw_val_class_const_double,
4380 dw_val_class_vec,
4381 dw_val_class_flag,
4382 dw_val_class_die_ref,
4383 dw_val_class_fde_ref,
4384 dw_val_class_lbl_id,
4385 dw_val_class_lineptr,
4386 dw_val_class_str,
4387 dw_val_class_macptr,
4388 dw_val_class_file,
4389 dw_val_class_data8,
4390 dw_val_class_decl_ref,
4391 dw_val_class_vms_delta
4394 /* Describe a floating point constant value, or a vector constant value. */
4396 typedef struct GTY(()) dw_vec_struct {
4397 unsigned char * GTY((length ("%h.length"))) array;
4398 unsigned length;
4399 unsigned elt_size;
4401 dw_vec_const;
4403 /* The dw_val_node describes an attribute's value, as it is
4404 represented internally. */
4406 typedef struct GTY(()) dw_val_struct {
4407 enum dw_val_class val_class;
4408 union dw_val_struct_union
4410 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4411 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4412 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4413 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4414 HOST_WIDE_INT GTY ((default)) val_int;
4415 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4416 double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4417 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4418 struct dw_val_die_union
4420 dw_die_ref die;
4421 int external;
4422 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4423 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4424 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4425 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4426 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4427 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4428 unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4429 tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4430 struct dw_val_vms_delta_union
4432 char * lbl1;
4433 char * lbl2;
4434 } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4436 GTY ((desc ("%1.val_class"))) v;
4438 dw_val_node;
4440 /* Locations in memory are described using a sequence of stack machine
4441 operations. */
4443 typedef struct GTY(()) dw_loc_descr_struct {
4444 dw_loc_descr_ref dw_loc_next;
4445 ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4446 /* Used to distinguish DW_OP_addr with a direct symbol relocation
4447 from DW_OP_addr with a dtp-relative symbol relocation. */
4448 unsigned int dtprel : 1;
4449 int dw_loc_addr;
4450 dw_val_node dw_loc_oprnd1;
4451 dw_val_node dw_loc_oprnd2;
4453 dw_loc_descr_node;
4455 /* Location lists are ranges + location descriptions for that range,
4456 so you can track variables that are in different places over
4457 their entire life. */
4458 typedef struct GTY(()) dw_loc_list_struct {
4459 dw_loc_list_ref dw_loc_next;
4460 const char *begin; /* Label for begin address of range */
4461 const char *end; /* Label for end address of range */
4462 char *ll_symbol; /* Label for beginning of location list.
4463 Only on head of list */
4464 const char *section; /* Section this loclist is relative to */
4465 dw_loc_descr_ref expr;
4466 hashval_t hash;
4467 bool emitted;
4468 } dw_loc_list_node;
4470 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4472 /* Convert a DWARF stack opcode into its string name. */
4474 static const char *
4475 dwarf_stack_op_name (unsigned int op)
4477 switch (op)
4479 case DW_OP_addr:
4480 return "DW_OP_addr";
4481 case DW_OP_deref:
4482 return "DW_OP_deref";
4483 case DW_OP_const1u:
4484 return "DW_OP_const1u";
4485 case DW_OP_const1s:
4486 return "DW_OP_const1s";
4487 case DW_OP_const2u:
4488 return "DW_OP_const2u";
4489 case DW_OP_const2s:
4490 return "DW_OP_const2s";
4491 case DW_OP_const4u:
4492 return "DW_OP_const4u";
4493 case DW_OP_const4s:
4494 return "DW_OP_const4s";
4495 case DW_OP_const8u:
4496 return "DW_OP_const8u";
4497 case DW_OP_const8s:
4498 return "DW_OP_const8s";
4499 case DW_OP_constu:
4500 return "DW_OP_constu";
4501 case DW_OP_consts:
4502 return "DW_OP_consts";
4503 case DW_OP_dup:
4504 return "DW_OP_dup";
4505 case DW_OP_drop:
4506 return "DW_OP_drop";
4507 case DW_OP_over:
4508 return "DW_OP_over";
4509 case DW_OP_pick:
4510 return "DW_OP_pick";
4511 case DW_OP_swap:
4512 return "DW_OP_swap";
4513 case DW_OP_rot:
4514 return "DW_OP_rot";
4515 case DW_OP_xderef:
4516 return "DW_OP_xderef";
4517 case DW_OP_abs:
4518 return "DW_OP_abs";
4519 case DW_OP_and:
4520 return "DW_OP_and";
4521 case DW_OP_div:
4522 return "DW_OP_div";
4523 case DW_OP_minus:
4524 return "DW_OP_minus";
4525 case DW_OP_mod:
4526 return "DW_OP_mod";
4527 case DW_OP_mul:
4528 return "DW_OP_mul";
4529 case DW_OP_neg:
4530 return "DW_OP_neg";
4531 case DW_OP_not:
4532 return "DW_OP_not";
4533 case DW_OP_or:
4534 return "DW_OP_or";
4535 case DW_OP_plus:
4536 return "DW_OP_plus";
4537 case DW_OP_plus_uconst:
4538 return "DW_OP_plus_uconst";
4539 case DW_OP_shl:
4540 return "DW_OP_shl";
4541 case DW_OP_shr:
4542 return "DW_OP_shr";
4543 case DW_OP_shra:
4544 return "DW_OP_shra";
4545 case DW_OP_xor:
4546 return "DW_OP_xor";
4547 case DW_OP_bra:
4548 return "DW_OP_bra";
4549 case DW_OP_eq:
4550 return "DW_OP_eq";
4551 case DW_OP_ge:
4552 return "DW_OP_ge";
4553 case DW_OP_gt:
4554 return "DW_OP_gt";
4555 case DW_OP_le:
4556 return "DW_OP_le";
4557 case DW_OP_lt:
4558 return "DW_OP_lt";
4559 case DW_OP_ne:
4560 return "DW_OP_ne";
4561 case DW_OP_skip:
4562 return "DW_OP_skip";
4563 case DW_OP_lit0:
4564 return "DW_OP_lit0";
4565 case DW_OP_lit1:
4566 return "DW_OP_lit1";
4567 case DW_OP_lit2:
4568 return "DW_OP_lit2";
4569 case DW_OP_lit3:
4570 return "DW_OP_lit3";
4571 case DW_OP_lit4:
4572 return "DW_OP_lit4";
4573 case DW_OP_lit5:
4574 return "DW_OP_lit5";
4575 case DW_OP_lit6:
4576 return "DW_OP_lit6";
4577 case DW_OP_lit7:
4578 return "DW_OP_lit7";
4579 case DW_OP_lit8:
4580 return "DW_OP_lit8";
4581 case DW_OP_lit9:
4582 return "DW_OP_lit9";
4583 case DW_OP_lit10:
4584 return "DW_OP_lit10";
4585 case DW_OP_lit11:
4586 return "DW_OP_lit11";
4587 case DW_OP_lit12:
4588 return "DW_OP_lit12";
4589 case DW_OP_lit13:
4590 return "DW_OP_lit13";
4591 case DW_OP_lit14:
4592 return "DW_OP_lit14";
4593 case DW_OP_lit15:
4594 return "DW_OP_lit15";
4595 case DW_OP_lit16:
4596 return "DW_OP_lit16";
4597 case DW_OP_lit17:
4598 return "DW_OP_lit17";
4599 case DW_OP_lit18:
4600 return "DW_OP_lit18";
4601 case DW_OP_lit19:
4602 return "DW_OP_lit19";
4603 case DW_OP_lit20:
4604 return "DW_OP_lit20";
4605 case DW_OP_lit21:
4606 return "DW_OP_lit21";
4607 case DW_OP_lit22:
4608 return "DW_OP_lit22";
4609 case DW_OP_lit23:
4610 return "DW_OP_lit23";
4611 case DW_OP_lit24:
4612 return "DW_OP_lit24";
4613 case DW_OP_lit25:
4614 return "DW_OP_lit25";
4615 case DW_OP_lit26:
4616 return "DW_OP_lit26";
4617 case DW_OP_lit27:
4618 return "DW_OP_lit27";
4619 case DW_OP_lit28:
4620 return "DW_OP_lit28";
4621 case DW_OP_lit29:
4622 return "DW_OP_lit29";
4623 case DW_OP_lit30:
4624 return "DW_OP_lit30";
4625 case DW_OP_lit31:
4626 return "DW_OP_lit31";
4627 case DW_OP_reg0:
4628 return "DW_OP_reg0";
4629 case DW_OP_reg1:
4630 return "DW_OP_reg1";
4631 case DW_OP_reg2:
4632 return "DW_OP_reg2";
4633 case DW_OP_reg3:
4634 return "DW_OP_reg3";
4635 case DW_OP_reg4:
4636 return "DW_OP_reg4";
4637 case DW_OP_reg5:
4638 return "DW_OP_reg5";
4639 case DW_OP_reg6:
4640 return "DW_OP_reg6";
4641 case DW_OP_reg7:
4642 return "DW_OP_reg7";
4643 case DW_OP_reg8:
4644 return "DW_OP_reg8";
4645 case DW_OP_reg9:
4646 return "DW_OP_reg9";
4647 case DW_OP_reg10:
4648 return "DW_OP_reg10";
4649 case DW_OP_reg11:
4650 return "DW_OP_reg11";
4651 case DW_OP_reg12:
4652 return "DW_OP_reg12";
4653 case DW_OP_reg13:
4654 return "DW_OP_reg13";
4655 case DW_OP_reg14:
4656 return "DW_OP_reg14";
4657 case DW_OP_reg15:
4658 return "DW_OP_reg15";
4659 case DW_OP_reg16:
4660 return "DW_OP_reg16";
4661 case DW_OP_reg17:
4662 return "DW_OP_reg17";
4663 case DW_OP_reg18:
4664 return "DW_OP_reg18";
4665 case DW_OP_reg19:
4666 return "DW_OP_reg19";
4667 case DW_OP_reg20:
4668 return "DW_OP_reg20";
4669 case DW_OP_reg21:
4670 return "DW_OP_reg21";
4671 case DW_OP_reg22:
4672 return "DW_OP_reg22";
4673 case DW_OP_reg23:
4674 return "DW_OP_reg23";
4675 case DW_OP_reg24:
4676 return "DW_OP_reg24";
4677 case DW_OP_reg25:
4678 return "DW_OP_reg25";
4679 case DW_OP_reg26:
4680 return "DW_OP_reg26";
4681 case DW_OP_reg27:
4682 return "DW_OP_reg27";
4683 case DW_OP_reg28:
4684 return "DW_OP_reg28";
4685 case DW_OP_reg29:
4686 return "DW_OP_reg29";
4687 case DW_OP_reg30:
4688 return "DW_OP_reg30";
4689 case DW_OP_reg31:
4690 return "DW_OP_reg31";
4691 case DW_OP_breg0:
4692 return "DW_OP_breg0";
4693 case DW_OP_breg1:
4694 return "DW_OP_breg1";
4695 case DW_OP_breg2:
4696 return "DW_OP_breg2";
4697 case DW_OP_breg3:
4698 return "DW_OP_breg3";
4699 case DW_OP_breg4:
4700 return "DW_OP_breg4";
4701 case DW_OP_breg5:
4702 return "DW_OP_breg5";
4703 case DW_OP_breg6:
4704 return "DW_OP_breg6";
4705 case DW_OP_breg7:
4706 return "DW_OP_breg7";
4707 case DW_OP_breg8:
4708 return "DW_OP_breg8";
4709 case DW_OP_breg9:
4710 return "DW_OP_breg9";
4711 case DW_OP_breg10:
4712 return "DW_OP_breg10";
4713 case DW_OP_breg11:
4714 return "DW_OP_breg11";
4715 case DW_OP_breg12:
4716 return "DW_OP_breg12";
4717 case DW_OP_breg13:
4718 return "DW_OP_breg13";
4719 case DW_OP_breg14:
4720 return "DW_OP_breg14";
4721 case DW_OP_breg15:
4722 return "DW_OP_breg15";
4723 case DW_OP_breg16:
4724 return "DW_OP_breg16";
4725 case DW_OP_breg17:
4726 return "DW_OP_breg17";
4727 case DW_OP_breg18:
4728 return "DW_OP_breg18";
4729 case DW_OP_breg19:
4730 return "DW_OP_breg19";
4731 case DW_OP_breg20:
4732 return "DW_OP_breg20";
4733 case DW_OP_breg21:
4734 return "DW_OP_breg21";
4735 case DW_OP_breg22:
4736 return "DW_OP_breg22";
4737 case DW_OP_breg23:
4738 return "DW_OP_breg23";
4739 case DW_OP_breg24:
4740 return "DW_OP_breg24";
4741 case DW_OP_breg25:
4742 return "DW_OP_breg25";
4743 case DW_OP_breg26:
4744 return "DW_OP_breg26";
4745 case DW_OP_breg27:
4746 return "DW_OP_breg27";
4747 case DW_OP_breg28:
4748 return "DW_OP_breg28";
4749 case DW_OP_breg29:
4750 return "DW_OP_breg29";
4751 case DW_OP_breg30:
4752 return "DW_OP_breg30";
4753 case DW_OP_breg31:
4754 return "DW_OP_breg31";
4755 case DW_OP_regx:
4756 return "DW_OP_regx";
4757 case DW_OP_fbreg:
4758 return "DW_OP_fbreg";
4759 case DW_OP_bregx:
4760 return "DW_OP_bregx";
4761 case DW_OP_piece:
4762 return "DW_OP_piece";
4763 case DW_OP_deref_size:
4764 return "DW_OP_deref_size";
4765 case DW_OP_xderef_size:
4766 return "DW_OP_xderef_size";
4767 case DW_OP_nop:
4768 return "DW_OP_nop";
4770 case DW_OP_push_object_address:
4771 return "DW_OP_push_object_address";
4772 case DW_OP_call2:
4773 return "DW_OP_call2";
4774 case DW_OP_call4:
4775 return "DW_OP_call4";
4776 case DW_OP_call_ref:
4777 return "DW_OP_call_ref";
4778 case DW_OP_implicit_value:
4779 return "DW_OP_implicit_value";
4780 case DW_OP_stack_value:
4781 return "DW_OP_stack_value";
4782 case DW_OP_form_tls_address:
4783 return "DW_OP_form_tls_address";
4784 case DW_OP_call_frame_cfa:
4785 return "DW_OP_call_frame_cfa";
4786 case DW_OP_bit_piece:
4787 return "DW_OP_bit_piece";
4789 case DW_OP_GNU_push_tls_address:
4790 return "DW_OP_GNU_push_tls_address";
4791 case DW_OP_GNU_uninit:
4792 return "DW_OP_GNU_uninit";
4793 case DW_OP_GNU_encoded_addr:
4794 return "DW_OP_GNU_encoded_addr";
4795 case DW_OP_GNU_implicit_pointer:
4796 return "DW_OP_GNU_implicit_pointer";
4798 default:
4799 return "OP_<unknown>";
4803 /* Return a pointer to a newly allocated location description. Location
4804 descriptions are simple expression terms that can be strung
4805 together to form more complicated location (address) descriptions. */
4807 static inline dw_loc_descr_ref
4808 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4809 unsigned HOST_WIDE_INT oprnd2)
4811 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4813 descr->dw_loc_opc = op;
4814 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4815 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4816 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4817 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4819 return descr;
4822 /* Return a pointer to a newly allocated location description for
4823 REG and OFFSET. */
4825 static inline dw_loc_descr_ref
4826 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
4828 if (reg <= 31)
4829 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4830 offset, 0);
4831 else
4832 return new_loc_descr (DW_OP_bregx, reg, offset);
4835 /* Add a location description term to a location description expression. */
4837 static inline void
4838 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4840 dw_loc_descr_ref *d;
4842 /* Find the end of the chain. */
4843 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4846 *d = descr;
4849 /* Add a constant OFFSET to a location expression. */
4851 static void
4852 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4854 dw_loc_descr_ref loc;
4855 HOST_WIDE_INT *p;
4857 gcc_assert (*list_head != NULL);
4859 if (!offset)
4860 return;
4862 /* Find the end of the chain. */
4863 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4866 p = NULL;
4867 if (loc->dw_loc_opc == DW_OP_fbreg
4868 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4869 p = &loc->dw_loc_oprnd1.v.val_int;
4870 else if (loc->dw_loc_opc == DW_OP_bregx)
4871 p = &loc->dw_loc_oprnd2.v.val_int;
4873 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4874 offset. Don't optimize if an signed integer overflow would happen. */
4875 if (p != NULL
4876 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4877 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4878 *p += offset;
4880 else if (offset > 0)
4881 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4883 else
4885 loc->dw_loc_next = int_loc_descriptor (-offset);
4886 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4890 /* Add a constant OFFSET to a location list. */
4892 static void
4893 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4895 dw_loc_list_ref d;
4896 for (d = list_head; d != NULL; d = d->dw_loc_next)
4897 loc_descr_plus_const (&d->expr, offset);
4900 #define DWARF_REF_SIZE \
4901 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4903 /* Return the size of a location descriptor. */
4905 static unsigned long
4906 size_of_loc_descr (dw_loc_descr_ref loc)
4908 unsigned long size = 1;
4910 switch (loc->dw_loc_opc)
4912 case DW_OP_addr:
4913 size += DWARF2_ADDR_SIZE;
4914 break;
4915 case DW_OP_const1u:
4916 case DW_OP_const1s:
4917 size += 1;
4918 break;
4919 case DW_OP_const2u:
4920 case DW_OP_const2s:
4921 size += 2;
4922 break;
4923 case DW_OP_const4u:
4924 case DW_OP_const4s:
4925 size += 4;
4926 break;
4927 case DW_OP_const8u:
4928 case DW_OP_const8s:
4929 size += 8;
4930 break;
4931 case DW_OP_constu:
4932 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4933 break;
4934 case DW_OP_consts:
4935 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4936 break;
4937 case DW_OP_pick:
4938 size += 1;
4939 break;
4940 case DW_OP_plus_uconst:
4941 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4942 break;
4943 case DW_OP_skip:
4944 case DW_OP_bra:
4945 size += 2;
4946 break;
4947 case DW_OP_breg0:
4948 case DW_OP_breg1:
4949 case DW_OP_breg2:
4950 case DW_OP_breg3:
4951 case DW_OP_breg4:
4952 case DW_OP_breg5:
4953 case DW_OP_breg6:
4954 case DW_OP_breg7:
4955 case DW_OP_breg8:
4956 case DW_OP_breg9:
4957 case DW_OP_breg10:
4958 case DW_OP_breg11:
4959 case DW_OP_breg12:
4960 case DW_OP_breg13:
4961 case DW_OP_breg14:
4962 case DW_OP_breg15:
4963 case DW_OP_breg16:
4964 case DW_OP_breg17:
4965 case DW_OP_breg18:
4966 case DW_OP_breg19:
4967 case DW_OP_breg20:
4968 case DW_OP_breg21:
4969 case DW_OP_breg22:
4970 case DW_OP_breg23:
4971 case DW_OP_breg24:
4972 case DW_OP_breg25:
4973 case DW_OP_breg26:
4974 case DW_OP_breg27:
4975 case DW_OP_breg28:
4976 case DW_OP_breg29:
4977 case DW_OP_breg30:
4978 case DW_OP_breg31:
4979 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4980 break;
4981 case DW_OP_regx:
4982 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4983 break;
4984 case DW_OP_fbreg:
4985 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4986 break;
4987 case DW_OP_bregx:
4988 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4989 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4990 break;
4991 case DW_OP_piece:
4992 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4993 break;
4994 case DW_OP_bit_piece:
4995 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4996 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4997 break;
4998 case DW_OP_deref_size:
4999 case DW_OP_xderef_size:
5000 size += 1;
5001 break;
5002 case DW_OP_call2:
5003 size += 2;
5004 break;
5005 case DW_OP_call4:
5006 size += 4;
5007 break;
5008 case DW_OP_call_ref:
5009 size += DWARF_REF_SIZE;
5010 break;
5011 case DW_OP_implicit_value:
5012 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5013 + loc->dw_loc_oprnd1.v.val_unsigned;
5014 break;
5015 case DW_OP_GNU_implicit_pointer:
5016 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5017 break;
5018 default:
5019 break;
5022 return size;
5025 /* Return the size of a series of location descriptors. */
5027 static unsigned long
5028 size_of_locs (dw_loc_descr_ref loc)
5030 dw_loc_descr_ref l;
5031 unsigned long size;
5033 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5034 field, to avoid writing to a PCH file. */
5035 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5037 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5038 break;
5039 size += size_of_loc_descr (l);
5041 if (! l)
5042 return size;
5044 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5046 l->dw_loc_addr = size;
5047 size += size_of_loc_descr (l);
5050 return size;
5053 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5054 static void get_ref_die_offset_label (char *, dw_die_ref);
5056 /* Output location description stack opcode's operands (if any). */
5058 static void
5059 output_loc_operands (dw_loc_descr_ref loc)
5061 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5062 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5064 switch (loc->dw_loc_opc)
5066 #ifdef DWARF2_DEBUGGING_INFO
5067 case DW_OP_const2u:
5068 case DW_OP_const2s:
5069 dw2_asm_output_data (2, val1->v.val_int, NULL);
5070 break;
5071 case DW_OP_const4u:
5072 if (loc->dtprel)
5074 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5075 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5076 val1->v.val_addr);
5077 fputc ('\n', asm_out_file);
5078 break;
5080 /* FALLTHRU */
5081 case DW_OP_const4s:
5082 dw2_asm_output_data (4, val1->v.val_int, NULL);
5083 break;
5084 case DW_OP_const8u:
5085 if (loc->dtprel)
5087 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5088 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5089 val1->v.val_addr);
5090 fputc ('\n', asm_out_file);
5091 break;
5093 /* FALLTHRU */
5094 case DW_OP_const8s:
5095 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5096 dw2_asm_output_data (8, val1->v.val_int, NULL);
5097 break;
5098 case DW_OP_skip:
5099 case DW_OP_bra:
5101 int offset;
5103 gcc_assert (val1->val_class == dw_val_class_loc);
5104 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5106 dw2_asm_output_data (2, offset, NULL);
5108 break;
5109 case DW_OP_implicit_value:
5110 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5111 switch (val2->val_class)
5113 case dw_val_class_const:
5114 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5115 break;
5116 case dw_val_class_vec:
5118 unsigned int elt_size = val2->v.val_vec.elt_size;
5119 unsigned int len = val2->v.val_vec.length;
5120 unsigned int i;
5121 unsigned char *p;
5123 if (elt_size > sizeof (HOST_WIDE_INT))
5125 elt_size /= 2;
5126 len *= 2;
5128 for (i = 0, p = val2->v.val_vec.array;
5129 i < len;
5130 i++, p += elt_size)
5131 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5132 "fp or vector constant word %u", i);
5134 break;
5135 case dw_val_class_const_double:
5137 unsigned HOST_WIDE_INT first, second;
5139 if (WORDS_BIG_ENDIAN)
5141 first = val2->v.val_double.high;
5142 second = val2->v.val_double.low;
5144 else
5146 first = val2->v.val_double.low;
5147 second = val2->v.val_double.high;
5149 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5150 first, NULL);
5151 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5152 second, NULL);
5154 break;
5155 case dw_val_class_addr:
5156 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5157 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5158 break;
5159 default:
5160 gcc_unreachable ();
5162 break;
5163 #else
5164 case DW_OP_const2u:
5165 case DW_OP_const2s:
5166 case DW_OP_const4u:
5167 case DW_OP_const4s:
5168 case DW_OP_const8u:
5169 case DW_OP_const8s:
5170 case DW_OP_skip:
5171 case DW_OP_bra:
5172 case DW_OP_implicit_value:
5173 /* We currently don't make any attempt to make sure these are
5174 aligned properly like we do for the main unwind info, so
5175 don't support emitting things larger than a byte if we're
5176 only doing unwinding. */
5177 gcc_unreachable ();
5178 #endif
5179 case DW_OP_const1u:
5180 case DW_OP_const1s:
5181 dw2_asm_output_data (1, val1->v.val_int, NULL);
5182 break;
5183 case DW_OP_constu:
5184 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5185 break;
5186 case DW_OP_consts:
5187 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5188 break;
5189 case DW_OP_pick:
5190 dw2_asm_output_data (1, val1->v.val_int, NULL);
5191 break;
5192 case DW_OP_plus_uconst:
5193 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5194 break;
5195 case DW_OP_breg0:
5196 case DW_OP_breg1:
5197 case DW_OP_breg2:
5198 case DW_OP_breg3:
5199 case DW_OP_breg4:
5200 case DW_OP_breg5:
5201 case DW_OP_breg6:
5202 case DW_OP_breg7:
5203 case DW_OP_breg8:
5204 case DW_OP_breg9:
5205 case DW_OP_breg10:
5206 case DW_OP_breg11:
5207 case DW_OP_breg12:
5208 case DW_OP_breg13:
5209 case DW_OP_breg14:
5210 case DW_OP_breg15:
5211 case DW_OP_breg16:
5212 case DW_OP_breg17:
5213 case DW_OP_breg18:
5214 case DW_OP_breg19:
5215 case DW_OP_breg20:
5216 case DW_OP_breg21:
5217 case DW_OP_breg22:
5218 case DW_OP_breg23:
5219 case DW_OP_breg24:
5220 case DW_OP_breg25:
5221 case DW_OP_breg26:
5222 case DW_OP_breg27:
5223 case DW_OP_breg28:
5224 case DW_OP_breg29:
5225 case DW_OP_breg30:
5226 case DW_OP_breg31:
5227 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5228 break;
5229 case DW_OP_regx:
5230 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5231 break;
5232 case DW_OP_fbreg:
5233 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5234 break;
5235 case DW_OP_bregx:
5236 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5237 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5238 break;
5239 case DW_OP_piece:
5240 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5241 break;
5242 case DW_OP_bit_piece:
5243 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5244 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5245 break;
5246 case DW_OP_deref_size:
5247 case DW_OP_xderef_size:
5248 dw2_asm_output_data (1, val1->v.val_int, NULL);
5249 break;
5251 case DW_OP_addr:
5252 if (loc->dtprel)
5254 if (targetm.asm_out.output_dwarf_dtprel)
5256 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5257 DWARF2_ADDR_SIZE,
5258 val1->v.val_addr);
5259 fputc ('\n', asm_out_file);
5261 else
5262 gcc_unreachable ();
5264 else
5266 #ifdef DWARF2_DEBUGGING_INFO
5267 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5268 #else
5269 gcc_unreachable ();
5270 #endif
5272 break;
5274 case DW_OP_GNU_implicit_pointer:
5276 char label[MAX_ARTIFICIAL_LABEL_BYTES
5277 + HOST_BITS_PER_WIDE_INT / 2 + 2];
5278 gcc_assert (val1->val_class == dw_val_class_die_ref);
5279 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5280 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5281 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5283 break;
5285 default:
5286 /* Other codes have no operands. */
5287 break;
5291 /* Output a sequence of location operations. */
5293 static void
5294 output_loc_sequence (dw_loc_descr_ref loc)
5296 for (; loc != NULL; loc = loc->dw_loc_next)
5298 /* Output the opcode. */
5299 dw2_asm_output_data (1, loc->dw_loc_opc,
5300 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5302 /* Output the operand(s) (if any). */
5303 output_loc_operands (loc);
5307 /* Output location description stack opcode's operands (if any).
5308 The output is single bytes on a line, suitable for .cfi_escape. */
5310 static void
5311 output_loc_operands_raw (dw_loc_descr_ref loc)
5313 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5314 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5316 switch (loc->dw_loc_opc)
5318 case DW_OP_addr:
5319 case DW_OP_implicit_value:
5320 /* We cannot output addresses in .cfi_escape, only bytes. */
5321 gcc_unreachable ();
5323 case DW_OP_const1u:
5324 case DW_OP_const1s:
5325 case DW_OP_pick:
5326 case DW_OP_deref_size:
5327 case DW_OP_xderef_size:
5328 fputc (',', asm_out_file);
5329 dw2_asm_output_data_raw (1, val1->v.val_int);
5330 break;
5332 case DW_OP_const2u:
5333 case DW_OP_const2s:
5334 fputc (',', asm_out_file);
5335 dw2_asm_output_data_raw (2, val1->v.val_int);
5336 break;
5338 case DW_OP_const4u:
5339 case DW_OP_const4s:
5340 fputc (',', asm_out_file);
5341 dw2_asm_output_data_raw (4, val1->v.val_int);
5342 break;
5344 case DW_OP_const8u:
5345 case DW_OP_const8s:
5346 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5347 fputc (',', asm_out_file);
5348 dw2_asm_output_data_raw (8, val1->v.val_int);
5349 break;
5351 case DW_OP_skip:
5352 case DW_OP_bra:
5354 int offset;
5356 gcc_assert (val1->val_class == dw_val_class_loc);
5357 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5359 fputc (',', asm_out_file);
5360 dw2_asm_output_data_raw (2, offset);
5362 break;
5364 case DW_OP_constu:
5365 case DW_OP_plus_uconst:
5366 case DW_OP_regx:
5367 case DW_OP_piece:
5368 fputc (',', asm_out_file);
5369 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5370 break;
5372 case DW_OP_bit_piece:
5373 fputc (',', asm_out_file);
5374 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5375 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5376 break;
5378 case DW_OP_consts:
5379 case DW_OP_breg0:
5380 case DW_OP_breg1:
5381 case DW_OP_breg2:
5382 case DW_OP_breg3:
5383 case DW_OP_breg4:
5384 case DW_OP_breg5:
5385 case DW_OP_breg6:
5386 case DW_OP_breg7:
5387 case DW_OP_breg8:
5388 case DW_OP_breg9:
5389 case DW_OP_breg10:
5390 case DW_OP_breg11:
5391 case DW_OP_breg12:
5392 case DW_OP_breg13:
5393 case DW_OP_breg14:
5394 case DW_OP_breg15:
5395 case DW_OP_breg16:
5396 case DW_OP_breg17:
5397 case DW_OP_breg18:
5398 case DW_OP_breg19:
5399 case DW_OP_breg20:
5400 case DW_OP_breg21:
5401 case DW_OP_breg22:
5402 case DW_OP_breg23:
5403 case DW_OP_breg24:
5404 case DW_OP_breg25:
5405 case DW_OP_breg26:
5406 case DW_OP_breg27:
5407 case DW_OP_breg28:
5408 case DW_OP_breg29:
5409 case DW_OP_breg30:
5410 case DW_OP_breg31:
5411 case DW_OP_fbreg:
5412 fputc (',', asm_out_file);
5413 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5414 break;
5416 case DW_OP_bregx:
5417 fputc (',', asm_out_file);
5418 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5419 fputc (',', asm_out_file);
5420 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5421 break;
5423 case DW_OP_GNU_implicit_pointer:
5424 gcc_unreachable ();
5425 break;
5427 default:
5428 /* Other codes have no operands. */
5429 break;
5433 static void
5434 output_loc_sequence_raw (dw_loc_descr_ref loc)
5436 while (1)
5438 /* Output the opcode. */
5439 fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5440 output_loc_operands_raw (loc);
5442 if (!loc->dw_loc_next)
5443 break;
5444 loc = loc->dw_loc_next;
5446 fputc (',', asm_out_file);
5450 /* This routine will generate the correct assembly data for a location
5451 description based on a cfi entry with a complex address. */
5453 static void
5454 output_cfa_loc (dw_cfi_ref cfi)
5456 dw_loc_descr_ref loc;
5457 unsigned long size;
5459 if (cfi->dw_cfi_opc == DW_CFA_expression)
5461 dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5462 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5464 else
5465 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5467 /* Output the size of the block. */
5468 size = size_of_locs (loc);
5469 dw2_asm_output_data_uleb128 (size, NULL);
5471 /* Now output the operations themselves. */
5472 output_loc_sequence (loc);
5475 /* Similar, but used for .cfi_escape. */
5477 static void
5478 output_cfa_loc_raw (dw_cfi_ref cfi)
5480 dw_loc_descr_ref loc;
5481 unsigned long size;
5483 if (cfi->dw_cfi_opc == DW_CFA_expression)
5485 fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5486 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5488 else
5489 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5491 /* Output the size of the block. */
5492 size = size_of_locs (loc);
5493 dw2_asm_output_data_uleb128_raw (size);
5494 fputc (',', asm_out_file);
5496 /* Now output the operations themselves. */
5497 output_loc_sequence_raw (loc);
5500 /* This function builds a dwarf location descriptor sequence from a
5501 dw_cfa_location, adding the given OFFSET to the result of the
5502 expression. */
5504 static struct dw_loc_descr_struct *
5505 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5507 struct dw_loc_descr_struct *head, *tmp;
5509 offset += cfa->offset;
5511 if (cfa->indirect)
5513 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5514 head->dw_loc_oprnd1.val_class = dw_val_class_const;
5515 tmp = new_loc_descr (DW_OP_deref, 0, 0);
5516 add_loc_descr (&head, tmp);
5517 if (offset != 0)
5519 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5520 add_loc_descr (&head, tmp);
5523 else
5524 head = new_reg_loc_descr (cfa->reg, offset);
5526 return head;
5529 /* This function builds a dwarf location descriptor sequence for
5530 the address at OFFSET from the CFA when stack is aligned to
5531 ALIGNMENT byte. */
5533 static struct dw_loc_descr_struct *
5534 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5536 struct dw_loc_descr_struct *head;
5537 unsigned int dwarf_fp
5538 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5540 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
5541 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5543 head = new_reg_loc_descr (dwarf_fp, 0);
5544 add_loc_descr (&head, int_loc_descriptor (alignment));
5545 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5546 loc_descr_plus_const (&head, offset);
5548 else
5549 head = new_reg_loc_descr (dwarf_fp, offset);
5550 return head;
5553 /* This function fills in aa dw_cfa_location structure from a dwarf location
5554 descriptor sequence. */
5556 static void
5557 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5559 struct dw_loc_descr_struct *ptr;
5560 cfa->offset = 0;
5561 cfa->base_offset = 0;
5562 cfa->indirect = 0;
5563 cfa->reg = -1;
5565 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5567 enum dwarf_location_atom op = ptr->dw_loc_opc;
5569 switch (op)
5571 case DW_OP_reg0:
5572 case DW_OP_reg1:
5573 case DW_OP_reg2:
5574 case DW_OP_reg3:
5575 case DW_OP_reg4:
5576 case DW_OP_reg5:
5577 case DW_OP_reg6:
5578 case DW_OP_reg7:
5579 case DW_OP_reg8:
5580 case DW_OP_reg9:
5581 case DW_OP_reg10:
5582 case DW_OP_reg11:
5583 case DW_OP_reg12:
5584 case DW_OP_reg13:
5585 case DW_OP_reg14:
5586 case DW_OP_reg15:
5587 case DW_OP_reg16:
5588 case DW_OP_reg17:
5589 case DW_OP_reg18:
5590 case DW_OP_reg19:
5591 case DW_OP_reg20:
5592 case DW_OP_reg21:
5593 case DW_OP_reg22:
5594 case DW_OP_reg23:
5595 case DW_OP_reg24:
5596 case DW_OP_reg25:
5597 case DW_OP_reg26:
5598 case DW_OP_reg27:
5599 case DW_OP_reg28:
5600 case DW_OP_reg29:
5601 case DW_OP_reg30:
5602 case DW_OP_reg31:
5603 cfa->reg = op - DW_OP_reg0;
5604 break;
5605 case DW_OP_regx:
5606 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5607 break;
5608 case DW_OP_breg0:
5609 case DW_OP_breg1:
5610 case DW_OP_breg2:
5611 case DW_OP_breg3:
5612 case DW_OP_breg4:
5613 case DW_OP_breg5:
5614 case DW_OP_breg6:
5615 case DW_OP_breg7:
5616 case DW_OP_breg8:
5617 case DW_OP_breg9:
5618 case DW_OP_breg10:
5619 case DW_OP_breg11:
5620 case DW_OP_breg12:
5621 case DW_OP_breg13:
5622 case DW_OP_breg14:
5623 case DW_OP_breg15:
5624 case DW_OP_breg16:
5625 case DW_OP_breg17:
5626 case DW_OP_breg18:
5627 case DW_OP_breg19:
5628 case DW_OP_breg20:
5629 case DW_OP_breg21:
5630 case DW_OP_breg22:
5631 case DW_OP_breg23:
5632 case DW_OP_breg24:
5633 case DW_OP_breg25:
5634 case DW_OP_breg26:
5635 case DW_OP_breg27:
5636 case DW_OP_breg28:
5637 case DW_OP_breg29:
5638 case DW_OP_breg30:
5639 case DW_OP_breg31:
5640 cfa->reg = op - DW_OP_breg0;
5641 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5642 break;
5643 case DW_OP_bregx:
5644 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5645 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5646 break;
5647 case DW_OP_deref:
5648 cfa->indirect = 1;
5649 break;
5650 case DW_OP_plus_uconst:
5651 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5652 break;
5653 default:
5654 internal_error ("DW_LOC_OP %s not implemented",
5655 dwarf_stack_op_name (ptr->dw_loc_opc));
5660 /* And now, the support for symbolic debugging information. */
5662 /* .debug_str support. */
5663 static int output_indirect_string (void **, void *);
5665 static void dwarf2out_init (const char *);
5666 static void dwarf2out_finish (const char *);
5667 static void dwarf2out_assembly_start (void);
5668 static void dwarf2out_define (unsigned int, const char *);
5669 static void dwarf2out_undef (unsigned int, const char *);
5670 static void dwarf2out_start_source_file (unsigned, const char *);
5671 static void dwarf2out_end_source_file (unsigned);
5672 static void dwarf2out_function_decl (tree);
5673 static void dwarf2out_begin_block (unsigned, unsigned);
5674 static void dwarf2out_end_block (unsigned, unsigned);
5675 static bool dwarf2out_ignore_block (const_tree);
5676 static void dwarf2out_global_decl (tree);
5677 static void dwarf2out_type_decl (tree, int);
5678 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5679 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5680 dw_die_ref);
5681 static void dwarf2out_abstract_function (tree);
5682 static void dwarf2out_var_location (rtx);
5683 static void dwarf2out_direct_call (tree);
5684 static void dwarf2out_virtual_call_token (tree, int);
5685 static void dwarf2out_copy_call_info (rtx, rtx);
5686 static void dwarf2out_virtual_call (int);
5687 static void dwarf2out_begin_function (tree);
5688 static void dwarf2out_set_name (tree, tree);
5690 /* The debug hooks structure. */
5692 const struct gcc_debug_hooks dwarf2_debug_hooks =
5694 dwarf2out_init,
5695 dwarf2out_finish,
5696 dwarf2out_assembly_start,
5697 dwarf2out_define,
5698 dwarf2out_undef,
5699 dwarf2out_start_source_file,
5700 dwarf2out_end_source_file,
5701 dwarf2out_begin_block,
5702 dwarf2out_end_block,
5703 dwarf2out_ignore_block,
5704 dwarf2out_source_line,
5705 dwarf2out_begin_prologue,
5706 #if VMS_DEBUGGING_INFO
5707 dwarf2out_vms_end_prologue,
5708 dwarf2out_vms_begin_epilogue,
5709 #else
5710 debug_nothing_int_charstar,
5711 debug_nothing_int_charstar,
5712 #endif
5713 dwarf2out_end_epilogue,
5714 dwarf2out_begin_function,
5715 debug_nothing_int, /* end_function */
5716 dwarf2out_function_decl, /* function_decl */
5717 dwarf2out_global_decl,
5718 dwarf2out_type_decl, /* type_decl */
5719 dwarf2out_imported_module_or_decl,
5720 debug_nothing_tree, /* deferred_inline_function */
5721 /* The DWARF 2 backend tries to reduce debugging bloat by not
5722 emitting the abstract description of inline functions until
5723 something tries to reference them. */
5724 dwarf2out_abstract_function, /* outlining_inline_function */
5725 debug_nothing_rtx, /* label */
5726 debug_nothing_int, /* handle_pch */
5727 dwarf2out_var_location,
5728 dwarf2out_switch_text_section,
5729 dwarf2out_direct_call,
5730 dwarf2out_virtual_call_token,
5731 dwarf2out_copy_call_info,
5732 dwarf2out_virtual_call,
5733 dwarf2out_set_name,
5734 1, /* start_end_main_source_file */
5735 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
5738 /* NOTE: In the comments in this file, many references are made to
5739 "Debugging Information Entries". This term is abbreviated as `DIE'
5740 throughout the remainder of this file. */
5742 /* An internal representation of the DWARF output is built, and then
5743 walked to generate the DWARF debugging info. The walk of the internal
5744 representation is done after the entire program has been compiled.
5745 The types below are used to describe the internal representation. */
5747 /* Various DIE's use offsets relative to the beginning of the
5748 .debug_info section to refer to each other. */
5750 typedef long int dw_offset;
5752 /* Define typedefs here to avoid circular dependencies. */
5754 typedef struct dw_attr_struct *dw_attr_ref;
5755 typedef struct dw_line_info_struct *dw_line_info_ref;
5756 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5757 typedef struct pubname_struct *pubname_ref;
5758 typedef struct dw_ranges_struct *dw_ranges_ref;
5759 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5760 typedef struct comdat_type_struct *comdat_type_node_ref;
5762 /* Each entry in the line_info_table maintains the file and
5763 line number associated with the label generated for that
5764 entry. The label gives the PC value associated with
5765 the line number entry. */
5767 typedef struct GTY(()) dw_line_info_struct {
5768 unsigned long dw_file_num;
5769 unsigned long dw_line_num;
5771 dw_line_info_entry;
5773 /* Line information for functions in separate sections; each one gets its
5774 own sequence. */
5775 typedef struct GTY(()) dw_separate_line_info_struct {
5776 unsigned long dw_file_num;
5777 unsigned long dw_line_num;
5778 unsigned long function;
5780 dw_separate_line_info_entry;
5782 /* Each DIE attribute has a field specifying the attribute kind,
5783 a link to the next attribute in the chain, and an attribute value.
5784 Attributes are typically linked below the DIE they modify. */
5786 typedef struct GTY(()) dw_attr_struct {
5787 enum dwarf_attribute dw_attr;
5788 dw_val_node dw_attr_val;
5790 dw_attr_node;
5792 DEF_VEC_O(dw_attr_node);
5793 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5795 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
5796 The children of each node form a circular list linked by
5797 die_sib. die_child points to the node *before* the "first" child node. */
5799 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5800 union die_symbol_or_type_node
5802 char * GTY ((tag ("0"))) die_symbol;
5803 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5805 GTY ((desc ("dwarf_version >= 4"))) die_id;
5806 VEC(dw_attr_node,gc) * die_attr;
5807 dw_die_ref die_parent;
5808 dw_die_ref die_child;
5809 dw_die_ref die_sib;
5810 dw_die_ref die_definition; /* ref from a specification to its definition */
5811 dw_offset die_offset;
5812 unsigned long die_abbrev;
5813 int die_mark;
5814 /* Die is used and must not be pruned as unused. */
5815 int die_perennial_p;
5816 unsigned int decl_id;
5817 enum dwarf_tag die_tag;
5819 die_node;
5821 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
5822 #define FOR_EACH_CHILD(die, c, expr) do { \
5823 c = die->die_child; \
5824 if (c) do { \
5825 c = c->die_sib; \
5826 expr; \
5827 } while (c != die->die_child); \
5828 } while (0)
5830 /* The pubname structure */
5832 typedef struct GTY(()) pubname_struct {
5833 dw_die_ref die;
5834 const char *name;
5836 pubname_entry;
5838 DEF_VEC_O(pubname_entry);
5839 DEF_VEC_ALLOC_O(pubname_entry, gc);
5841 struct GTY(()) dw_ranges_struct {
5842 /* If this is positive, it's a block number, otherwise it's a
5843 bitwise-negated index into dw_ranges_by_label. */
5844 int num;
5847 /* A structure to hold a macinfo entry. */
5849 typedef struct GTY(()) macinfo_struct {
5850 unsigned HOST_WIDE_INT code;
5851 unsigned HOST_WIDE_INT lineno;
5852 const char *info;
5854 macinfo_entry;
5856 DEF_VEC_O(macinfo_entry);
5857 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5859 struct GTY(()) dw_ranges_by_label_struct {
5860 const char *begin;
5861 const char *end;
5864 /* The comdat type node structure. */
5865 typedef struct GTY(()) comdat_type_struct
5867 dw_die_ref root_die;
5868 dw_die_ref type_die;
5869 char signature[DWARF_TYPE_SIGNATURE_SIZE];
5870 struct comdat_type_struct *next;
5872 comdat_type_node;
5874 /* The limbo die list structure. */
5875 typedef struct GTY(()) limbo_die_struct {
5876 dw_die_ref die;
5877 tree created_for;
5878 struct limbo_die_struct *next;
5880 limbo_die_node;
5882 typedef struct skeleton_chain_struct
5884 dw_die_ref old_die;
5885 dw_die_ref new_die;
5886 struct skeleton_chain_struct *parent;
5888 skeleton_chain_node;
5890 /* How to start an assembler comment. */
5891 #ifndef ASM_COMMENT_START
5892 #define ASM_COMMENT_START ";#"
5893 #endif
5895 /* Define a macro which returns nonzero for a TYPE_DECL which was
5896 implicitly generated for a tagged type.
5898 Note that unlike the gcc front end (which generates a NULL named
5899 TYPE_DECL node for each complete tagged type, each array type, and
5900 each function type node created) the g++ front end generates a
5901 _named_ TYPE_DECL node for each tagged type node created.
5902 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5903 generate a DW_TAG_typedef DIE for them. */
5905 #define TYPE_DECL_IS_STUB(decl) \
5906 (DECL_NAME (decl) == NULL_TREE \
5907 || (DECL_ARTIFICIAL (decl) \
5908 && is_tagged_type (TREE_TYPE (decl)) \
5909 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
5910 /* This is necessary for stub decls that \
5911 appear in nested inline functions. */ \
5912 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5913 && (decl_ultimate_origin (decl) \
5914 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5916 /* Information concerning the compilation unit's programming
5917 language, and compiler version. */
5919 /* Fixed size portion of the DWARF compilation unit header. */
5920 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5921 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5923 /* Fixed size portion of the DWARF comdat type unit header. */
5924 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5925 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5926 + DWARF_OFFSET_SIZE)
5928 /* Fixed size portion of public names info. */
5929 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5931 /* Fixed size portion of the address range info. */
5932 #define DWARF_ARANGES_HEADER_SIZE \
5933 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5934 DWARF2_ADDR_SIZE * 2) \
5935 - DWARF_INITIAL_LENGTH_SIZE)
5937 /* Size of padding portion in the address range info. It must be
5938 aligned to twice the pointer size. */
5939 #define DWARF_ARANGES_PAD_SIZE \
5940 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5941 DWARF2_ADDR_SIZE * 2) \
5942 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5944 /* Use assembler line directives if available. */
5945 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5946 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5947 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5948 #else
5949 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5950 #endif
5951 #endif
5953 /* Minimum line offset in a special line info. opcode.
5954 This value was chosen to give a reasonable range of values. */
5955 #define DWARF_LINE_BASE -10
5957 /* First special line opcode - leave room for the standard opcodes. */
5958 #define DWARF_LINE_OPCODE_BASE 10
5960 /* Range of line offsets in a special line info. opcode. */
5961 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
5963 /* Flag that indicates the initial value of the is_stmt_start flag.
5964 In the present implementation, we do not mark any lines as
5965 the beginning of a source statement, because that information
5966 is not made available by the GCC front-end. */
5967 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5969 /* Maximum number of operations per instruction bundle. */
5970 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5971 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5972 #endif
5974 /* This location is used by calc_die_sizes() to keep track
5975 the offset of each DIE within the .debug_info section. */
5976 static unsigned long next_die_offset;
5978 /* Record the root of the DIE's built for the current compilation unit. */
5979 static GTY(()) dw_die_ref single_comp_unit_die;
5981 /* A list of type DIEs that have been separated into comdat sections. */
5982 static GTY(()) comdat_type_node *comdat_type_list;
5984 /* A list of DIEs with a NULL parent waiting to be relocated. */
5985 static GTY(()) limbo_die_node *limbo_die_list;
5987 /* A list of DIEs for which we may have to generate
5988 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
5989 static GTY(()) limbo_die_node *deferred_asm_name;
5991 /* Filenames referenced by this compilation unit. */
5992 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5994 /* A hash table of references to DIE's that describe declarations.
5995 The key is a DECL_UID() which is a unique number identifying each decl. */
5996 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5998 /* A hash table of references to DIE's that describe COMMON blocks.
5999 The key is DECL_UID() ^ die_parent. */
6000 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6002 typedef struct GTY(()) die_arg_entry_struct {
6003 dw_die_ref die;
6004 tree arg;
6005 } die_arg_entry;
6007 DEF_VEC_O(die_arg_entry);
6008 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6010 /* Node of the variable location list. */
6011 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6012 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6013 EXPR_LIST chain. For small bitsizes, bitsize is encoded
6014 in mode of the EXPR_LIST node and first EXPR_LIST operand
6015 is either NOTE_INSN_VAR_LOCATION for a piece with a known
6016 location or NULL for padding. For larger bitsizes,
6017 mode is 0 and first operand is a CONCAT with bitsize
6018 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6019 NULL as second operand. */
6020 rtx GTY (()) loc;
6021 const char * GTY (()) label;
6022 struct var_loc_node * GTY (()) next;
6025 /* Variable location list. */
6026 struct GTY (()) var_loc_list_def {
6027 struct var_loc_node * GTY (()) first;
6029 /* Pointer to the last but one or last element of the
6030 chained list. If the list is empty, both first and
6031 last are NULL, if the list contains just one node
6032 or the last node certainly is not redundant, it points
6033 to the last node, otherwise points to the last but one.
6034 Do not mark it for GC because it is marked through the chain. */
6035 struct var_loc_node * GTY ((skip ("%h"))) last;
6037 /* DECL_UID of the variable decl. */
6038 unsigned int decl_id;
6040 typedef struct var_loc_list_def var_loc_list;
6043 /* Table of decl location linked lists. */
6044 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6046 /* A pointer to the base of a list of references to DIE's that
6047 are uniquely identified by their tag, presence/absence of
6048 children DIE's, and list of attribute/value pairs. */
6049 static GTY((length ("abbrev_die_table_allocated")))
6050 dw_die_ref *abbrev_die_table;
6052 /* Number of elements currently allocated for abbrev_die_table. */
6053 static GTY(()) unsigned abbrev_die_table_allocated;
6055 /* Number of elements in type_die_table currently in use. */
6056 static GTY(()) unsigned abbrev_die_table_in_use;
6058 /* Size (in elements) of increments by which we may expand the
6059 abbrev_die_table. */
6060 #define ABBREV_DIE_TABLE_INCREMENT 256
6062 /* A pointer to the base of a table that contains line information
6063 for each source code line in .text in the compilation unit. */
6064 static GTY((length ("line_info_table_allocated")))
6065 dw_line_info_ref line_info_table;
6067 /* Number of elements currently allocated for line_info_table. */
6068 static GTY(()) unsigned line_info_table_allocated;
6070 /* Number of elements in line_info_table currently in use. */
6071 static GTY(()) unsigned line_info_table_in_use;
6073 /* A pointer to the base of a table that contains line information
6074 for each source code line outside of .text in the compilation unit. */
6075 static GTY ((length ("separate_line_info_table_allocated")))
6076 dw_separate_line_info_ref separate_line_info_table;
6078 /* Number of elements currently allocated for separate_line_info_table. */
6079 static GTY(()) unsigned separate_line_info_table_allocated;
6081 /* Number of elements in separate_line_info_table currently in use. */
6082 static GTY(()) unsigned separate_line_info_table_in_use;
6084 /* Size (in elements) of increments by which we may expand the
6085 line_info_table. */
6086 #define LINE_INFO_TABLE_INCREMENT 1024
6088 /* A flag to tell pubnames/types export if there is an info section to
6089 refer to. */
6090 static bool info_section_emitted;
6092 /* A pointer to the base of a table that contains a list of publicly
6093 accessible names. */
6094 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
6096 /* A pointer to the base of a table that contains a list of publicly
6097 accessible types. */
6098 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6100 /* A pointer to the base of a table that contains a list of macro
6101 defines/undefines (and file start/end markers). */
6102 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6104 /* Array of dies for which we should generate .debug_arange info. */
6105 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6107 /* Number of elements currently allocated for arange_table. */
6108 static GTY(()) unsigned arange_table_allocated;
6110 /* Number of elements in arange_table currently in use. */
6111 static GTY(()) unsigned arange_table_in_use;
6113 /* Size (in elements) of increments by which we may expand the
6114 arange_table. */
6115 #define ARANGE_TABLE_INCREMENT 64
6117 /* Array of dies for which we should generate .debug_ranges info. */
6118 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6120 /* Number of elements currently allocated for ranges_table. */
6121 static GTY(()) unsigned ranges_table_allocated;
6123 /* Number of elements in ranges_table currently in use. */
6124 static GTY(()) unsigned ranges_table_in_use;
6126 /* Array of pairs of labels referenced in ranges_table. */
6127 static GTY ((length ("ranges_by_label_allocated")))
6128 dw_ranges_by_label_ref ranges_by_label;
6130 /* Number of elements currently allocated for ranges_by_label. */
6131 static GTY(()) unsigned ranges_by_label_allocated;
6133 /* Number of elements in ranges_by_label currently in use. */
6134 static GTY(()) unsigned ranges_by_label_in_use;
6136 /* Size (in elements) of increments by which we may expand the
6137 ranges_table. */
6138 #define RANGES_TABLE_INCREMENT 64
6140 /* Whether we have location lists that need outputting */
6141 static GTY(()) bool have_location_lists;
6143 /* Unique label counter. */
6144 static GTY(()) unsigned int loclabel_num;
6146 /* Unique label counter for point-of-call tables. */
6147 static GTY(()) unsigned int poc_label_num;
6149 /* The direct call table structure. */
6151 typedef struct GTY(()) dcall_struct {
6152 unsigned int poc_label_num;
6153 tree poc_decl;
6154 dw_die_ref targ_die;
6156 dcall_entry;
6158 DEF_VEC_O(dcall_entry);
6159 DEF_VEC_ALLOC_O(dcall_entry, gc);
6161 /* The virtual call table structure. */
6163 typedef struct GTY(()) vcall_struct {
6164 unsigned int poc_label_num;
6165 unsigned int vtable_slot;
6167 vcall_entry;
6169 DEF_VEC_O(vcall_entry);
6170 DEF_VEC_ALLOC_O(vcall_entry, gc);
6172 /* Pointers to the direct and virtual call tables. */
6173 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6174 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6176 /* A hash table to map INSN_UIDs to vtable slot indexes. */
6178 struct GTY (()) vcall_insn {
6179 int insn_uid;
6180 unsigned int vtable_slot;
6183 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6185 /* Record whether the function being analyzed contains inlined functions. */
6186 static int current_function_has_inlines;
6188 /* The last file entry emitted by maybe_emit_file(). */
6189 static GTY(()) struct dwarf_file_data * last_emitted_file;
6191 /* Number of internal labels generated by gen_internal_sym(). */
6192 static GTY(()) int label_num;
6194 /* Cached result of previous call to lookup_filename. */
6195 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6197 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6199 /* Instances of generic types for which we need to generate debug
6200 info that describe their generic parameters and arguments. That
6201 generation needs to happen once all types are properly laid out so
6202 we do it at the end of compilation. */
6203 static GTY(()) VEC(tree,gc) *generic_type_instances;
6205 /* Offset from the "steady-state frame pointer" to the frame base,
6206 within the current function. */
6207 static HOST_WIDE_INT frame_pointer_fb_offset;
6209 /* Forward declarations for functions defined in this file. */
6211 static int is_pseudo_reg (const_rtx);
6212 static tree type_main_variant (tree);
6213 static int is_tagged_type (const_tree);
6214 static const char *dwarf_tag_name (unsigned);
6215 static const char *dwarf_attr_name (unsigned);
6216 static const char *dwarf_form_name (unsigned);
6217 static tree decl_ultimate_origin (const_tree);
6218 static tree decl_class_context (tree);
6219 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6220 static inline enum dw_val_class AT_class (dw_attr_ref);
6221 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6222 static inline unsigned AT_flag (dw_attr_ref);
6223 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6224 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6225 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6226 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6227 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6228 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6229 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6230 unsigned int, unsigned char *);
6231 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6232 static hashval_t debug_str_do_hash (const void *);
6233 static int debug_str_eq (const void *, const void *);
6234 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6235 static inline const char *AT_string (dw_attr_ref);
6236 static enum dwarf_form AT_string_form (dw_attr_ref);
6237 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6238 static void add_AT_specification (dw_die_ref, dw_die_ref);
6239 static inline dw_die_ref AT_ref (dw_attr_ref);
6240 static inline int AT_ref_external (dw_attr_ref);
6241 static inline void set_AT_ref_external (dw_attr_ref, int);
6242 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6243 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6244 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6245 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6246 dw_loc_list_ref);
6247 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6248 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6249 static inline rtx AT_addr (dw_attr_ref);
6250 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6251 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6252 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6253 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6254 unsigned HOST_WIDE_INT);
6255 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6256 unsigned long);
6257 static inline const char *AT_lbl (dw_attr_ref);
6258 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6259 static const char *get_AT_low_pc (dw_die_ref);
6260 static const char *get_AT_hi_pc (dw_die_ref);
6261 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6262 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6263 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6264 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6265 static bool is_cxx (void);
6266 static bool is_fortran (void);
6267 static bool is_ada (void);
6268 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6269 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6270 static void add_child_die (dw_die_ref, dw_die_ref);
6271 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6272 static dw_die_ref lookup_type_die (tree);
6273 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6274 static void equate_type_number_to_die (tree, dw_die_ref);
6275 static hashval_t decl_die_table_hash (const void *);
6276 static int decl_die_table_eq (const void *, const void *);
6277 static dw_die_ref lookup_decl_die (tree);
6278 static hashval_t common_block_die_table_hash (const void *);
6279 static int common_block_die_table_eq (const void *, const void *);
6280 static hashval_t decl_loc_table_hash (const void *);
6281 static int decl_loc_table_eq (const void *, const void *);
6282 static var_loc_list *lookup_decl_loc (const_tree);
6283 static void equate_decl_number_to_die (tree, dw_die_ref);
6284 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6285 static void print_spaces (FILE *);
6286 static void print_die (dw_die_ref, FILE *);
6287 static void print_dwarf_line_table (FILE *);
6288 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6289 static dw_die_ref pop_compile_unit (dw_die_ref);
6290 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6291 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6292 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6293 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6294 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6295 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6296 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6297 struct md5_ctx *, int *);
6298 struct checksum_attributes;
6299 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6300 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6301 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6302 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6303 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6304 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6305 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6306 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6307 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6308 static void compute_section_prefix (dw_die_ref);
6309 static int is_type_die (dw_die_ref);
6310 static int is_comdat_die (dw_die_ref);
6311 static int is_symbol_die (dw_die_ref);
6312 static void assign_symbol_names (dw_die_ref);
6313 static void break_out_includes (dw_die_ref);
6314 static int is_declaration_die (dw_die_ref);
6315 static int should_move_die_to_comdat (dw_die_ref);
6316 static dw_die_ref clone_as_declaration (dw_die_ref);
6317 static dw_die_ref clone_die (dw_die_ref);
6318 static dw_die_ref clone_tree (dw_die_ref);
6319 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6320 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6321 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6322 static dw_die_ref generate_skeleton (dw_die_ref);
6323 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6324 dw_die_ref);
6325 static void break_out_comdat_types (dw_die_ref);
6326 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6327 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6328 static void copy_decls_for_unworthy_types (dw_die_ref);
6330 static hashval_t htab_cu_hash (const void *);
6331 static int htab_cu_eq (const void *, const void *);
6332 static void htab_cu_del (void *);
6333 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6334 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6335 static void add_sibling_attributes (dw_die_ref);
6336 static void build_abbrev_table (dw_die_ref);
6337 static void output_location_lists (dw_die_ref);
6338 static int constant_size (unsigned HOST_WIDE_INT);
6339 static unsigned long size_of_die (dw_die_ref);
6340 static void calc_die_sizes (dw_die_ref);
6341 static void mark_dies (dw_die_ref);
6342 static void unmark_dies (dw_die_ref);
6343 static void unmark_all_dies (dw_die_ref);
6344 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6345 static unsigned long size_of_aranges (void);
6346 static enum dwarf_form value_format (dw_attr_ref);
6347 static void output_value_format (dw_attr_ref);
6348 static void output_abbrev_section (void);
6349 static void output_die_symbol (dw_die_ref);
6350 static void output_die (dw_die_ref);
6351 static void output_compilation_unit_header (void);
6352 static void output_comp_unit (dw_die_ref, int);
6353 static void output_comdat_type_unit (comdat_type_node *);
6354 static const char *dwarf2_name (tree, int);
6355 static void add_pubname (tree, dw_die_ref);
6356 static void add_pubname_string (const char *, dw_die_ref);
6357 static void add_pubtype (tree, dw_die_ref);
6358 static void output_pubnames (VEC (pubname_entry,gc) *);
6359 static void add_arange (tree, dw_die_ref);
6360 static void output_aranges (void);
6361 static unsigned int add_ranges_num (int);
6362 static unsigned int add_ranges (const_tree);
6363 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6364 bool *);
6365 static void output_ranges (void);
6366 static void output_line_info (void);
6367 static void output_file_names (void);
6368 static dw_die_ref base_type_die (tree);
6369 static int is_base_type (tree);
6370 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6371 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6372 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6373 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6374 static int type_is_enum (const_tree);
6375 static unsigned int dbx_reg_number (const_rtx);
6376 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6377 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6378 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6379 enum var_init_status);
6380 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6381 enum var_init_status);
6382 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6383 enum var_init_status);
6384 static int is_based_loc (const_rtx);
6385 static int resolve_one_addr (rtx *, void *);
6386 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6387 enum var_init_status);
6388 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6389 enum var_init_status);
6390 static dw_loc_list_ref loc_list_from_tree (tree, int);
6391 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6392 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6393 static tree field_type (const_tree);
6394 static unsigned int simple_type_align_in_bits (const_tree);
6395 static unsigned int simple_decl_align_in_bits (const_tree);
6396 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6397 static HOST_WIDE_INT field_byte_offset (const_tree);
6398 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6399 dw_loc_list_ref);
6400 static void add_data_member_location_attribute (dw_die_ref, tree);
6401 static bool add_const_value_attribute (dw_die_ref, rtx);
6402 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6403 static void insert_double (double_int, unsigned char *);
6404 static void insert_float (const_rtx, unsigned char *);
6405 static rtx rtl_for_decl_location (tree);
6406 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6407 enum dwarf_attribute);
6408 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6409 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6410 static void add_name_attribute (dw_die_ref, const char *);
6411 static void add_comp_dir_attribute (dw_die_ref);
6412 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6413 static void add_subscript_info (dw_die_ref, tree, bool);
6414 static void add_byte_size_attribute (dw_die_ref, tree);
6415 static void add_bit_offset_attribute (dw_die_ref, tree);
6416 static void add_bit_size_attribute (dw_die_ref, tree);
6417 static void add_prototyped_attribute (dw_die_ref, tree);
6418 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6419 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6420 static void add_src_coords_attributes (dw_die_ref, tree);
6421 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6422 static void push_decl_scope (tree);
6423 static void pop_decl_scope (void);
6424 static dw_die_ref scope_die_for (tree, dw_die_ref);
6425 static inline int local_scope_p (dw_die_ref);
6426 static inline int class_scope_p (dw_die_ref);
6427 static inline int class_or_namespace_scope_p (dw_die_ref);
6428 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6429 static void add_calling_convention_attribute (dw_die_ref, tree);
6430 static const char *type_tag (const_tree);
6431 static tree member_declared_type (const_tree);
6432 #if 0
6433 static const char *decl_start_label (tree);
6434 #endif
6435 static void gen_array_type_die (tree, dw_die_ref);
6436 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6437 #if 0
6438 static void gen_entry_point_die (tree, dw_die_ref);
6439 #endif
6440 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6441 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6442 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
6443 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6444 static void gen_formal_types_die (tree, dw_die_ref);
6445 static void gen_subprogram_die (tree, dw_die_ref);
6446 static void gen_variable_die (tree, tree, dw_die_ref);
6447 static void gen_const_die (tree, dw_die_ref);
6448 static void gen_label_die (tree, dw_die_ref);
6449 static void gen_lexical_block_die (tree, dw_die_ref, int);
6450 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6451 static void gen_field_die (tree, dw_die_ref);
6452 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6453 static dw_die_ref gen_compile_unit_die (const char *);
6454 static void gen_inheritance_die (tree, tree, dw_die_ref);
6455 static void gen_member_die (tree, dw_die_ref);
6456 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6457 enum debug_info_usage);
6458 static void gen_subroutine_type_die (tree, dw_die_ref);
6459 static void gen_typedef_die (tree, dw_die_ref);
6460 static void gen_type_die (tree, dw_die_ref);
6461 static void gen_block_die (tree, dw_die_ref, int);
6462 static void decls_for_scope (tree, dw_die_ref, int);
6463 static int is_redundant_typedef (const_tree);
6464 static bool is_naming_typedef_decl (const_tree);
6465 static inline dw_die_ref get_context_die (tree);
6466 static void gen_namespace_die (tree, dw_die_ref);
6467 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6468 static dw_die_ref force_decl_die (tree);
6469 static dw_die_ref force_type_die (tree);
6470 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6471 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6472 static struct dwarf_file_data * lookup_filename (const char *);
6473 static void retry_incomplete_types (void);
6474 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6475 static void gen_generic_params_dies (tree);
6476 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6477 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6478 static void splice_child_die (dw_die_ref, dw_die_ref);
6479 static int file_info_cmp (const void *, const void *);
6480 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6481 const char *, const char *);
6482 static void output_loc_list (dw_loc_list_ref);
6483 static char *gen_internal_sym (const char *);
6485 static void prune_unmark_dies (dw_die_ref);
6486 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6487 static void prune_unused_types_mark (dw_die_ref, int);
6488 static void prune_unused_types_walk (dw_die_ref);
6489 static void prune_unused_types_walk_attribs (dw_die_ref);
6490 static void prune_unused_types_prune (dw_die_ref);
6491 static void prune_unused_types (void);
6492 static int maybe_emit_file (struct dwarf_file_data *fd);
6493 static inline const char *AT_vms_delta1 (dw_attr_ref);
6494 static inline const char *AT_vms_delta2 (dw_attr_ref);
6495 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6496 const char *, const char *);
6497 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6498 static void gen_remaining_tmpl_value_param_die_attribute (void);
6499 static bool generic_type_p (tree);
6500 static void schedule_generic_params_dies_gen (tree t);
6501 static void gen_scheduled_generic_parms_dies (void);
6503 /* Section names used to hold DWARF debugging information. */
6504 #ifndef DEBUG_INFO_SECTION
6505 #define DEBUG_INFO_SECTION ".debug_info"
6506 #endif
6507 #ifndef DEBUG_ABBREV_SECTION
6508 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
6509 #endif
6510 #ifndef DEBUG_ARANGES_SECTION
6511 #define DEBUG_ARANGES_SECTION ".debug_aranges"
6512 #endif
6513 #ifndef DEBUG_MACINFO_SECTION
6514 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
6515 #endif
6516 #ifndef DEBUG_LINE_SECTION
6517 #define DEBUG_LINE_SECTION ".debug_line"
6518 #endif
6519 #ifndef DEBUG_LOC_SECTION
6520 #define DEBUG_LOC_SECTION ".debug_loc"
6521 #endif
6522 #ifndef DEBUG_PUBNAMES_SECTION
6523 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
6524 #endif
6525 #ifndef DEBUG_PUBTYPES_SECTION
6526 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
6527 #endif
6528 #ifndef DEBUG_DCALL_SECTION
6529 #define DEBUG_DCALL_SECTION ".debug_dcall"
6530 #endif
6531 #ifndef DEBUG_VCALL_SECTION
6532 #define DEBUG_VCALL_SECTION ".debug_vcall"
6533 #endif
6534 #ifndef DEBUG_STR_SECTION
6535 #define DEBUG_STR_SECTION ".debug_str"
6536 #endif
6537 #ifndef DEBUG_RANGES_SECTION
6538 #define DEBUG_RANGES_SECTION ".debug_ranges"
6539 #endif
6541 /* Standard ELF section names for compiled code and data. */
6542 #ifndef TEXT_SECTION_NAME
6543 #define TEXT_SECTION_NAME ".text"
6544 #endif
6546 /* Section flags for .debug_str section. */
6547 #define DEBUG_STR_SECTION_FLAGS \
6548 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
6549 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
6550 : SECTION_DEBUG)
6552 /* Labels we insert at beginning sections we can reference instead of
6553 the section names themselves. */
6555 #ifndef TEXT_SECTION_LABEL
6556 #define TEXT_SECTION_LABEL "Ltext"
6557 #endif
6558 #ifndef COLD_TEXT_SECTION_LABEL
6559 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
6560 #endif
6561 #ifndef DEBUG_LINE_SECTION_LABEL
6562 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
6563 #endif
6564 #ifndef DEBUG_INFO_SECTION_LABEL
6565 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
6566 #endif
6567 #ifndef DEBUG_ABBREV_SECTION_LABEL
6568 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
6569 #endif
6570 #ifndef DEBUG_LOC_SECTION_LABEL
6571 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
6572 #endif
6573 #ifndef DEBUG_RANGES_SECTION_LABEL
6574 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
6575 #endif
6576 #ifndef DEBUG_MACINFO_SECTION_LABEL
6577 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
6578 #endif
6581 /* Definitions of defaults for formats and names of various special
6582 (artificial) labels which may be generated within this file (when the -g
6583 options is used and DWARF2_DEBUGGING_INFO is in effect.
6584 If necessary, these may be overridden from within the tm.h file, but
6585 typically, overriding these defaults is unnecessary. */
6587 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6588 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6589 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6590 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6591 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6592 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6593 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6594 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6595 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6596 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6598 #ifndef TEXT_END_LABEL
6599 #define TEXT_END_LABEL "Letext"
6600 #endif
6601 #ifndef COLD_END_LABEL
6602 #define COLD_END_LABEL "Letext_cold"
6603 #endif
6604 #ifndef BLOCK_BEGIN_LABEL
6605 #define BLOCK_BEGIN_LABEL "LBB"
6606 #endif
6607 #ifndef BLOCK_END_LABEL
6608 #define BLOCK_END_LABEL "LBE"
6609 #endif
6610 #ifndef LINE_CODE_LABEL
6611 #define LINE_CODE_LABEL "LM"
6612 #endif
6613 #ifndef SEPARATE_LINE_CODE_LABEL
6614 #define SEPARATE_LINE_CODE_LABEL "LSM"
6615 #endif
6618 /* Return the root of the DIE's built for the current compilation unit. */
6619 static dw_die_ref
6620 comp_unit_die (void)
6622 if (!single_comp_unit_die)
6623 single_comp_unit_die = gen_compile_unit_die (NULL);
6624 return single_comp_unit_die;
6627 /* We allow a language front-end to designate a function that is to be
6628 called to "demangle" any name before it is put into a DIE. */
6630 static const char *(*demangle_name_func) (const char *);
6632 void
6633 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6635 demangle_name_func = func;
6638 /* Test if rtl node points to a pseudo register. */
6640 static inline int
6641 is_pseudo_reg (const_rtx rtl)
6643 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6644 || (GET_CODE (rtl) == SUBREG
6645 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6648 /* Return a reference to a type, with its const and volatile qualifiers
6649 removed. */
6651 static inline tree
6652 type_main_variant (tree type)
6654 type = TYPE_MAIN_VARIANT (type);
6656 /* ??? There really should be only one main variant among any group of
6657 variants of a given type (and all of the MAIN_VARIANT values for all
6658 members of the group should point to that one type) but sometimes the C
6659 front-end messes this up for array types, so we work around that bug
6660 here. */
6661 if (TREE_CODE (type) == ARRAY_TYPE)
6662 while (type != TYPE_MAIN_VARIANT (type))
6663 type = TYPE_MAIN_VARIANT (type);
6665 return type;
6668 /* Return nonzero if the given type node represents a tagged type. */
6670 static inline int
6671 is_tagged_type (const_tree type)
6673 enum tree_code code = TREE_CODE (type);
6675 return (code == RECORD_TYPE || code == UNION_TYPE
6676 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6679 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
6681 static void
6682 get_ref_die_offset_label (char *label, dw_die_ref ref)
6684 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6687 /* Convert a DIE tag into its string name. */
6689 static const char *
6690 dwarf_tag_name (unsigned int tag)
6692 switch (tag)
6694 case DW_TAG_padding:
6695 return "DW_TAG_padding";
6696 case DW_TAG_array_type:
6697 return "DW_TAG_array_type";
6698 case DW_TAG_class_type:
6699 return "DW_TAG_class_type";
6700 case DW_TAG_entry_point:
6701 return "DW_TAG_entry_point";
6702 case DW_TAG_enumeration_type:
6703 return "DW_TAG_enumeration_type";
6704 case DW_TAG_formal_parameter:
6705 return "DW_TAG_formal_parameter";
6706 case DW_TAG_imported_declaration:
6707 return "DW_TAG_imported_declaration";
6708 case DW_TAG_label:
6709 return "DW_TAG_label";
6710 case DW_TAG_lexical_block:
6711 return "DW_TAG_lexical_block";
6712 case DW_TAG_member:
6713 return "DW_TAG_member";
6714 case DW_TAG_pointer_type:
6715 return "DW_TAG_pointer_type";
6716 case DW_TAG_reference_type:
6717 return "DW_TAG_reference_type";
6718 case DW_TAG_compile_unit:
6719 return "DW_TAG_compile_unit";
6720 case DW_TAG_string_type:
6721 return "DW_TAG_string_type";
6722 case DW_TAG_structure_type:
6723 return "DW_TAG_structure_type";
6724 case DW_TAG_subroutine_type:
6725 return "DW_TAG_subroutine_type";
6726 case DW_TAG_typedef:
6727 return "DW_TAG_typedef";
6728 case DW_TAG_union_type:
6729 return "DW_TAG_union_type";
6730 case DW_TAG_unspecified_parameters:
6731 return "DW_TAG_unspecified_parameters";
6732 case DW_TAG_variant:
6733 return "DW_TAG_variant";
6734 case DW_TAG_common_block:
6735 return "DW_TAG_common_block";
6736 case DW_TAG_common_inclusion:
6737 return "DW_TAG_common_inclusion";
6738 case DW_TAG_inheritance:
6739 return "DW_TAG_inheritance";
6740 case DW_TAG_inlined_subroutine:
6741 return "DW_TAG_inlined_subroutine";
6742 case DW_TAG_module:
6743 return "DW_TAG_module";
6744 case DW_TAG_ptr_to_member_type:
6745 return "DW_TAG_ptr_to_member_type";
6746 case DW_TAG_set_type:
6747 return "DW_TAG_set_type";
6748 case DW_TAG_subrange_type:
6749 return "DW_TAG_subrange_type";
6750 case DW_TAG_with_stmt:
6751 return "DW_TAG_with_stmt";
6752 case DW_TAG_access_declaration:
6753 return "DW_TAG_access_declaration";
6754 case DW_TAG_base_type:
6755 return "DW_TAG_base_type";
6756 case DW_TAG_catch_block:
6757 return "DW_TAG_catch_block";
6758 case DW_TAG_const_type:
6759 return "DW_TAG_const_type";
6760 case DW_TAG_constant:
6761 return "DW_TAG_constant";
6762 case DW_TAG_enumerator:
6763 return "DW_TAG_enumerator";
6764 case DW_TAG_file_type:
6765 return "DW_TAG_file_type";
6766 case DW_TAG_friend:
6767 return "DW_TAG_friend";
6768 case DW_TAG_namelist:
6769 return "DW_TAG_namelist";
6770 case DW_TAG_namelist_item:
6771 return "DW_TAG_namelist_item";
6772 case DW_TAG_packed_type:
6773 return "DW_TAG_packed_type";
6774 case DW_TAG_subprogram:
6775 return "DW_TAG_subprogram";
6776 case DW_TAG_template_type_param:
6777 return "DW_TAG_template_type_param";
6778 case DW_TAG_template_value_param:
6779 return "DW_TAG_template_value_param";
6780 case DW_TAG_thrown_type:
6781 return "DW_TAG_thrown_type";
6782 case DW_TAG_try_block:
6783 return "DW_TAG_try_block";
6784 case DW_TAG_variant_part:
6785 return "DW_TAG_variant_part";
6786 case DW_TAG_variable:
6787 return "DW_TAG_variable";
6788 case DW_TAG_volatile_type:
6789 return "DW_TAG_volatile_type";
6790 case DW_TAG_dwarf_procedure:
6791 return "DW_TAG_dwarf_procedure";
6792 case DW_TAG_restrict_type:
6793 return "DW_TAG_restrict_type";
6794 case DW_TAG_interface_type:
6795 return "DW_TAG_interface_type";
6796 case DW_TAG_namespace:
6797 return "DW_TAG_namespace";
6798 case DW_TAG_imported_module:
6799 return "DW_TAG_imported_module";
6800 case DW_TAG_unspecified_type:
6801 return "DW_TAG_unspecified_type";
6802 case DW_TAG_partial_unit:
6803 return "DW_TAG_partial_unit";
6804 case DW_TAG_imported_unit:
6805 return "DW_TAG_imported_unit";
6806 case DW_TAG_condition:
6807 return "DW_TAG_condition";
6808 case DW_TAG_shared_type:
6809 return "DW_TAG_shared_type";
6810 case DW_TAG_type_unit:
6811 return "DW_TAG_type_unit";
6812 case DW_TAG_rvalue_reference_type:
6813 return "DW_TAG_rvalue_reference_type";
6814 case DW_TAG_template_alias:
6815 return "DW_TAG_template_alias";
6816 case DW_TAG_GNU_template_parameter_pack:
6817 return "DW_TAG_GNU_template_parameter_pack";
6818 case DW_TAG_GNU_formal_parameter_pack:
6819 return "DW_TAG_GNU_formal_parameter_pack";
6820 case DW_TAG_MIPS_loop:
6821 return "DW_TAG_MIPS_loop";
6822 case DW_TAG_format_label:
6823 return "DW_TAG_format_label";
6824 case DW_TAG_function_template:
6825 return "DW_TAG_function_template";
6826 case DW_TAG_class_template:
6827 return "DW_TAG_class_template";
6828 case DW_TAG_GNU_BINCL:
6829 return "DW_TAG_GNU_BINCL";
6830 case DW_TAG_GNU_EINCL:
6831 return "DW_TAG_GNU_EINCL";
6832 case DW_TAG_GNU_template_template_param:
6833 return "DW_TAG_GNU_template_template_param";
6834 default:
6835 return "DW_TAG_<unknown>";
6839 /* Convert a DWARF attribute code into its string name. */
6841 static const char *
6842 dwarf_attr_name (unsigned int attr)
6844 switch (attr)
6846 case DW_AT_sibling:
6847 return "DW_AT_sibling";
6848 case DW_AT_location:
6849 return "DW_AT_location";
6850 case DW_AT_name:
6851 return "DW_AT_name";
6852 case DW_AT_ordering:
6853 return "DW_AT_ordering";
6854 case DW_AT_subscr_data:
6855 return "DW_AT_subscr_data";
6856 case DW_AT_byte_size:
6857 return "DW_AT_byte_size";
6858 case DW_AT_bit_offset:
6859 return "DW_AT_bit_offset";
6860 case DW_AT_bit_size:
6861 return "DW_AT_bit_size";
6862 case DW_AT_element_list:
6863 return "DW_AT_element_list";
6864 case DW_AT_stmt_list:
6865 return "DW_AT_stmt_list";
6866 case DW_AT_low_pc:
6867 return "DW_AT_low_pc";
6868 case DW_AT_high_pc:
6869 return "DW_AT_high_pc";
6870 case DW_AT_language:
6871 return "DW_AT_language";
6872 case DW_AT_member:
6873 return "DW_AT_member";
6874 case DW_AT_discr:
6875 return "DW_AT_discr";
6876 case DW_AT_discr_value:
6877 return "DW_AT_discr_value";
6878 case DW_AT_visibility:
6879 return "DW_AT_visibility";
6880 case DW_AT_import:
6881 return "DW_AT_import";
6882 case DW_AT_string_length:
6883 return "DW_AT_string_length";
6884 case DW_AT_common_reference:
6885 return "DW_AT_common_reference";
6886 case DW_AT_comp_dir:
6887 return "DW_AT_comp_dir";
6888 case DW_AT_const_value:
6889 return "DW_AT_const_value";
6890 case DW_AT_containing_type:
6891 return "DW_AT_containing_type";
6892 case DW_AT_default_value:
6893 return "DW_AT_default_value";
6894 case DW_AT_inline:
6895 return "DW_AT_inline";
6896 case DW_AT_is_optional:
6897 return "DW_AT_is_optional";
6898 case DW_AT_lower_bound:
6899 return "DW_AT_lower_bound";
6900 case DW_AT_producer:
6901 return "DW_AT_producer";
6902 case DW_AT_prototyped:
6903 return "DW_AT_prototyped";
6904 case DW_AT_return_addr:
6905 return "DW_AT_return_addr";
6906 case DW_AT_start_scope:
6907 return "DW_AT_start_scope";
6908 case DW_AT_bit_stride:
6909 return "DW_AT_bit_stride";
6910 case DW_AT_upper_bound:
6911 return "DW_AT_upper_bound";
6912 case DW_AT_abstract_origin:
6913 return "DW_AT_abstract_origin";
6914 case DW_AT_accessibility:
6915 return "DW_AT_accessibility";
6916 case DW_AT_address_class:
6917 return "DW_AT_address_class";
6918 case DW_AT_artificial:
6919 return "DW_AT_artificial";
6920 case DW_AT_base_types:
6921 return "DW_AT_base_types";
6922 case DW_AT_calling_convention:
6923 return "DW_AT_calling_convention";
6924 case DW_AT_count:
6925 return "DW_AT_count";
6926 case DW_AT_data_member_location:
6927 return "DW_AT_data_member_location";
6928 case DW_AT_decl_column:
6929 return "DW_AT_decl_column";
6930 case DW_AT_decl_file:
6931 return "DW_AT_decl_file";
6932 case DW_AT_decl_line:
6933 return "DW_AT_decl_line";
6934 case DW_AT_declaration:
6935 return "DW_AT_declaration";
6936 case DW_AT_discr_list:
6937 return "DW_AT_discr_list";
6938 case DW_AT_encoding:
6939 return "DW_AT_encoding";
6940 case DW_AT_external:
6941 return "DW_AT_external";
6942 case DW_AT_explicit:
6943 return "DW_AT_explicit";
6944 case DW_AT_frame_base:
6945 return "DW_AT_frame_base";
6946 case DW_AT_friend:
6947 return "DW_AT_friend";
6948 case DW_AT_identifier_case:
6949 return "DW_AT_identifier_case";
6950 case DW_AT_macro_info:
6951 return "DW_AT_macro_info";
6952 case DW_AT_namelist_items:
6953 return "DW_AT_namelist_items";
6954 case DW_AT_priority:
6955 return "DW_AT_priority";
6956 case DW_AT_segment:
6957 return "DW_AT_segment";
6958 case DW_AT_specification:
6959 return "DW_AT_specification";
6960 case DW_AT_static_link:
6961 return "DW_AT_static_link";
6962 case DW_AT_type:
6963 return "DW_AT_type";
6964 case DW_AT_use_location:
6965 return "DW_AT_use_location";
6966 case DW_AT_variable_parameter:
6967 return "DW_AT_variable_parameter";
6968 case DW_AT_virtuality:
6969 return "DW_AT_virtuality";
6970 case DW_AT_vtable_elem_location:
6971 return "DW_AT_vtable_elem_location";
6973 case DW_AT_allocated:
6974 return "DW_AT_allocated";
6975 case DW_AT_associated:
6976 return "DW_AT_associated";
6977 case DW_AT_data_location:
6978 return "DW_AT_data_location";
6979 case DW_AT_byte_stride:
6980 return "DW_AT_byte_stride";
6981 case DW_AT_entry_pc:
6982 return "DW_AT_entry_pc";
6983 case DW_AT_use_UTF8:
6984 return "DW_AT_use_UTF8";
6985 case DW_AT_extension:
6986 return "DW_AT_extension";
6987 case DW_AT_ranges:
6988 return "DW_AT_ranges";
6989 case DW_AT_trampoline:
6990 return "DW_AT_trampoline";
6991 case DW_AT_call_column:
6992 return "DW_AT_call_column";
6993 case DW_AT_call_file:
6994 return "DW_AT_call_file";
6995 case DW_AT_call_line:
6996 return "DW_AT_call_line";
6997 case DW_AT_object_pointer:
6998 return "DW_AT_object_pointer";
7000 case DW_AT_signature:
7001 return "DW_AT_signature";
7002 case DW_AT_main_subprogram:
7003 return "DW_AT_main_subprogram";
7004 case DW_AT_data_bit_offset:
7005 return "DW_AT_data_bit_offset";
7006 case DW_AT_const_expr:
7007 return "DW_AT_const_expr";
7008 case DW_AT_enum_class:
7009 return "DW_AT_enum_class";
7010 case DW_AT_linkage_name:
7011 return "DW_AT_linkage_name";
7013 case DW_AT_MIPS_fde:
7014 return "DW_AT_MIPS_fde";
7015 case DW_AT_MIPS_loop_begin:
7016 return "DW_AT_MIPS_loop_begin";
7017 case DW_AT_MIPS_tail_loop_begin:
7018 return "DW_AT_MIPS_tail_loop_begin";
7019 case DW_AT_MIPS_epilog_begin:
7020 return "DW_AT_MIPS_epilog_begin";
7021 #if VMS_DEBUGGING_INFO
7022 case DW_AT_HP_prologue:
7023 return "DW_AT_HP_prologue";
7024 #else
7025 case DW_AT_MIPS_loop_unroll_factor:
7026 return "DW_AT_MIPS_loop_unroll_factor";
7027 #endif
7028 case DW_AT_MIPS_software_pipeline_depth:
7029 return "DW_AT_MIPS_software_pipeline_depth";
7030 case DW_AT_MIPS_linkage_name:
7031 return "DW_AT_MIPS_linkage_name";
7032 #if VMS_DEBUGGING_INFO
7033 case DW_AT_HP_epilogue:
7034 return "DW_AT_HP_epilogue";
7035 #else
7036 case DW_AT_MIPS_stride:
7037 return "DW_AT_MIPS_stride";
7038 #endif
7039 case DW_AT_MIPS_abstract_name:
7040 return "DW_AT_MIPS_abstract_name";
7041 case DW_AT_MIPS_clone_origin:
7042 return "DW_AT_MIPS_clone_origin";
7043 case DW_AT_MIPS_has_inlines:
7044 return "DW_AT_MIPS_has_inlines";
7046 case DW_AT_sf_names:
7047 return "DW_AT_sf_names";
7048 case DW_AT_src_info:
7049 return "DW_AT_src_info";
7050 case DW_AT_mac_info:
7051 return "DW_AT_mac_info";
7052 case DW_AT_src_coords:
7053 return "DW_AT_src_coords";
7054 case DW_AT_body_begin:
7055 return "DW_AT_body_begin";
7056 case DW_AT_body_end:
7057 return "DW_AT_body_end";
7058 case DW_AT_GNU_vector:
7059 return "DW_AT_GNU_vector";
7060 case DW_AT_GNU_guarded_by:
7061 return "DW_AT_GNU_guarded_by";
7062 case DW_AT_GNU_pt_guarded_by:
7063 return "DW_AT_GNU_pt_guarded_by";
7064 case DW_AT_GNU_guarded:
7065 return "DW_AT_GNU_guarded";
7066 case DW_AT_GNU_pt_guarded:
7067 return "DW_AT_GNU_pt_guarded";
7068 case DW_AT_GNU_locks_excluded:
7069 return "DW_AT_GNU_locks_excluded";
7070 case DW_AT_GNU_exclusive_locks_required:
7071 return "DW_AT_GNU_exclusive_locks_required";
7072 case DW_AT_GNU_shared_locks_required:
7073 return "DW_AT_GNU_shared_locks_required";
7074 case DW_AT_GNU_odr_signature:
7075 return "DW_AT_GNU_odr_signature";
7076 case DW_AT_GNU_template_name:
7077 return "DW_AT_GNU_template_name";
7079 case DW_AT_VMS_rtnbeg_pd_address:
7080 return "DW_AT_VMS_rtnbeg_pd_address";
7082 default:
7083 return "DW_AT_<unknown>";
7087 /* Convert a DWARF value form code into its string name. */
7089 static const char *
7090 dwarf_form_name (unsigned int form)
7092 switch (form)
7094 case DW_FORM_addr:
7095 return "DW_FORM_addr";
7096 case DW_FORM_block2:
7097 return "DW_FORM_block2";
7098 case DW_FORM_block4:
7099 return "DW_FORM_block4";
7100 case DW_FORM_data2:
7101 return "DW_FORM_data2";
7102 case DW_FORM_data4:
7103 return "DW_FORM_data4";
7104 case DW_FORM_data8:
7105 return "DW_FORM_data8";
7106 case DW_FORM_string:
7107 return "DW_FORM_string";
7108 case DW_FORM_block:
7109 return "DW_FORM_block";
7110 case DW_FORM_block1:
7111 return "DW_FORM_block1";
7112 case DW_FORM_data1:
7113 return "DW_FORM_data1";
7114 case DW_FORM_flag:
7115 return "DW_FORM_flag";
7116 case DW_FORM_sdata:
7117 return "DW_FORM_sdata";
7118 case DW_FORM_strp:
7119 return "DW_FORM_strp";
7120 case DW_FORM_udata:
7121 return "DW_FORM_udata";
7122 case DW_FORM_ref_addr:
7123 return "DW_FORM_ref_addr";
7124 case DW_FORM_ref1:
7125 return "DW_FORM_ref1";
7126 case DW_FORM_ref2:
7127 return "DW_FORM_ref2";
7128 case DW_FORM_ref4:
7129 return "DW_FORM_ref4";
7130 case DW_FORM_ref8:
7131 return "DW_FORM_ref8";
7132 case DW_FORM_ref_udata:
7133 return "DW_FORM_ref_udata";
7134 case DW_FORM_indirect:
7135 return "DW_FORM_indirect";
7136 case DW_FORM_sec_offset:
7137 return "DW_FORM_sec_offset";
7138 case DW_FORM_exprloc:
7139 return "DW_FORM_exprloc";
7140 case DW_FORM_flag_present:
7141 return "DW_FORM_flag_present";
7142 case DW_FORM_ref_sig8:
7143 return "DW_FORM_ref_sig8";
7144 default:
7145 return "DW_FORM_<unknown>";
7149 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
7150 instance of an inlined instance of a decl which is local to an inline
7151 function, so we have to trace all of the way back through the origin chain
7152 to find out what sort of node actually served as the original seed for the
7153 given block. */
7155 static tree
7156 decl_ultimate_origin (const_tree decl)
7158 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7159 return NULL_TREE;
7161 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7162 nodes in the function to point to themselves; ignore that if
7163 we're trying to output the abstract instance of this function. */
7164 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7165 return NULL_TREE;
7167 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7168 most distant ancestor, this should never happen. */
7169 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7171 return DECL_ABSTRACT_ORIGIN (decl);
7174 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
7175 of a virtual function may refer to a base class, so we check the 'this'
7176 parameter. */
7178 static tree
7179 decl_class_context (tree decl)
7181 tree context = NULL_TREE;
7183 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7184 context = DECL_CONTEXT (decl);
7185 else
7186 context = TYPE_MAIN_VARIANT
7187 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7189 if (context && !TYPE_P (context))
7190 context = NULL_TREE;
7192 return context;
7195 /* Add an attribute/value pair to a DIE. */
7197 static inline void
7198 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7200 /* Maybe this should be an assert? */
7201 if (die == NULL)
7202 return;
7204 if (die->die_attr == NULL)
7205 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7206 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7209 static inline enum dw_val_class
7210 AT_class (dw_attr_ref a)
7212 return a->dw_attr_val.val_class;
7215 /* Add a flag value attribute to a DIE. */
7217 static inline void
7218 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7220 dw_attr_node attr;
7222 attr.dw_attr = attr_kind;
7223 attr.dw_attr_val.val_class = dw_val_class_flag;
7224 attr.dw_attr_val.v.val_flag = flag;
7225 add_dwarf_attr (die, &attr);
7228 static inline unsigned
7229 AT_flag (dw_attr_ref a)
7231 gcc_assert (a && AT_class (a) == dw_val_class_flag);
7232 return a->dw_attr_val.v.val_flag;
7235 /* Add a signed integer attribute value to a DIE. */
7237 static inline void
7238 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7240 dw_attr_node attr;
7242 attr.dw_attr = attr_kind;
7243 attr.dw_attr_val.val_class = dw_val_class_const;
7244 attr.dw_attr_val.v.val_int = int_val;
7245 add_dwarf_attr (die, &attr);
7248 static inline HOST_WIDE_INT
7249 AT_int (dw_attr_ref a)
7251 gcc_assert (a && AT_class (a) == dw_val_class_const);
7252 return a->dw_attr_val.v.val_int;
7255 /* Add an unsigned integer attribute value to a DIE. */
7257 static inline void
7258 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7259 unsigned HOST_WIDE_INT unsigned_val)
7261 dw_attr_node attr;
7263 attr.dw_attr = attr_kind;
7264 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7265 attr.dw_attr_val.v.val_unsigned = unsigned_val;
7266 add_dwarf_attr (die, &attr);
7269 static inline unsigned HOST_WIDE_INT
7270 AT_unsigned (dw_attr_ref a)
7272 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7273 return a->dw_attr_val.v.val_unsigned;
7276 /* Add an unsigned double integer attribute value to a DIE. */
7278 static inline void
7279 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7280 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7282 dw_attr_node attr;
7284 attr.dw_attr = attr_kind;
7285 attr.dw_attr_val.val_class = dw_val_class_const_double;
7286 attr.dw_attr_val.v.val_double.high = high;
7287 attr.dw_attr_val.v.val_double.low = low;
7288 add_dwarf_attr (die, &attr);
7291 /* Add a floating point attribute value to a DIE and return it. */
7293 static inline void
7294 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7295 unsigned int length, unsigned int elt_size, unsigned char *array)
7297 dw_attr_node attr;
7299 attr.dw_attr = attr_kind;
7300 attr.dw_attr_val.val_class = dw_val_class_vec;
7301 attr.dw_attr_val.v.val_vec.length = length;
7302 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7303 attr.dw_attr_val.v.val_vec.array = array;
7304 add_dwarf_attr (die, &attr);
7307 /* Add an 8-byte data attribute value to a DIE. */
7309 static inline void
7310 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7311 unsigned char data8[8])
7313 dw_attr_node attr;
7315 attr.dw_attr = attr_kind;
7316 attr.dw_attr_val.val_class = dw_val_class_data8;
7317 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7318 add_dwarf_attr (die, &attr);
7321 /* Hash and equality functions for debug_str_hash. */
7323 static hashval_t
7324 debug_str_do_hash (const void *x)
7326 return htab_hash_string (((const struct indirect_string_node *)x)->str);
7329 static int
7330 debug_str_eq (const void *x1, const void *x2)
7332 return strcmp ((((const struct indirect_string_node *)x1)->str),
7333 (const char *)x2) == 0;
7336 /* Add STR to the indirect string hash table. */
7338 static struct indirect_string_node *
7339 find_AT_string (const char *str)
7341 struct indirect_string_node *node;
7342 void **slot;
7344 if (! debug_str_hash)
7345 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7346 debug_str_eq, NULL);
7348 slot = htab_find_slot_with_hash (debug_str_hash, str,
7349 htab_hash_string (str), INSERT);
7350 if (*slot == NULL)
7352 node = ggc_alloc_cleared_indirect_string_node ();
7353 node->str = ggc_strdup (str);
7354 *slot = node;
7356 else
7357 node = (struct indirect_string_node *) *slot;
7359 node->refcount++;
7360 return node;
7363 /* Add a string attribute value to a DIE. */
7365 static inline void
7366 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7368 dw_attr_node attr;
7369 struct indirect_string_node *node;
7371 node = find_AT_string (str);
7373 attr.dw_attr = attr_kind;
7374 attr.dw_attr_val.val_class = dw_val_class_str;
7375 attr.dw_attr_val.v.val_str = node;
7376 add_dwarf_attr (die, &attr);
7379 /* Create a label for an indirect string node, ensuring it is going to
7380 be output, unless its reference count goes down to zero. */
7382 static inline void
7383 gen_label_for_indirect_string (struct indirect_string_node *node)
7385 char label[32];
7387 if (node->label)
7388 return;
7390 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7391 ++dw2_string_counter;
7392 node->label = xstrdup (label);
7395 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7396 debug string STR. */
7398 static inline rtx
7399 get_debug_string_label (const char *str)
7401 struct indirect_string_node *node = find_AT_string (str);
7403 debug_str_hash_forced = true;
7405 gen_label_for_indirect_string (node);
7407 return gen_rtx_SYMBOL_REF (Pmode, node->label);
7410 static inline const char *
7411 AT_string (dw_attr_ref a)
7413 gcc_assert (a && AT_class (a) == dw_val_class_str);
7414 return a->dw_attr_val.v.val_str->str;
7417 /* Find out whether a string should be output inline in DIE
7418 or out-of-line in .debug_str section. */
7420 static enum dwarf_form
7421 AT_string_form (dw_attr_ref a)
7423 struct indirect_string_node *node;
7424 unsigned int len;
7426 gcc_assert (a && AT_class (a) == dw_val_class_str);
7428 node = a->dw_attr_val.v.val_str;
7429 if (node->form)
7430 return node->form;
7432 len = strlen (node->str) + 1;
7434 /* If the string is shorter or equal to the size of the reference, it is
7435 always better to put it inline. */
7436 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7437 return node->form = DW_FORM_string;
7439 /* If we cannot expect the linker to merge strings in .debug_str
7440 section, only put it into .debug_str if it is worth even in this
7441 single module. */
7442 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7443 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7444 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7445 return node->form = DW_FORM_string;
7447 gen_label_for_indirect_string (node);
7449 return node->form = DW_FORM_strp;
7452 /* Add a DIE reference attribute value to a DIE. */
7454 static inline void
7455 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7457 dw_attr_node attr;
7459 #ifdef ENABLE_CHECKING
7460 gcc_assert (targ_die != NULL);
7461 #else
7462 /* With LTO we can end up trying to reference something we didn't create
7463 a DIE for. Avoid crashing later on a NULL referenced DIE. */
7464 if (targ_die == NULL)
7465 return;
7466 #endif
7468 attr.dw_attr = attr_kind;
7469 attr.dw_attr_val.val_class = dw_val_class_die_ref;
7470 attr.dw_attr_val.v.val_die_ref.die = targ_die;
7471 attr.dw_attr_val.v.val_die_ref.external = 0;
7472 add_dwarf_attr (die, &attr);
7475 /* Add an AT_specification attribute to a DIE, and also make the back
7476 pointer from the specification to the definition. */
7478 static inline void
7479 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7481 add_AT_die_ref (die, DW_AT_specification, targ_die);
7482 gcc_assert (!targ_die->die_definition);
7483 targ_die->die_definition = die;
7486 static inline dw_die_ref
7487 AT_ref (dw_attr_ref a)
7489 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7490 return a->dw_attr_val.v.val_die_ref.die;
7493 static inline int
7494 AT_ref_external (dw_attr_ref a)
7496 if (a && AT_class (a) == dw_val_class_die_ref)
7497 return a->dw_attr_val.v.val_die_ref.external;
7499 return 0;
7502 static inline void
7503 set_AT_ref_external (dw_attr_ref a, int i)
7505 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7506 a->dw_attr_val.v.val_die_ref.external = i;
7509 /* Add an FDE reference attribute value to a DIE. */
7511 static inline void
7512 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7514 dw_attr_node attr;
7516 attr.dw_attr = attr_kind;
7517 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7518 attr.dw_attr_val.v.val_fde_index = targ_fde;
7519 add_dwarf_attr (die, &attr);
7522 /* Add a location description attribute value to a DIE. */
7524 static inline void
7525 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7527 dw_attr_node attr;
7529 attr.dw_attr = attr_kind;
7530 attr.dw_attr_val.val_class = dw_val_class_loc;
7531 attr.dw_attr_val.v.val_loc = loc;
7532 add_dwarf_attr (die, &attr);
7535 static inline dw_loc_descr_ref
7536 AT_loc (dw_attr_ref a)
7538 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7539 return a->dw_attr_val.v.val_loc;
7542 static inline void
7543 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7545 dw_attr_node attr;
7547 attr.dw_attr = attr_kind;
7548 attr.dw_attr_val.val_class = dw_val_class_loc_list;
7549 attr.dw_attr_val.v.val_loc_list = loc_list;
7550 add_dwarf_attr (die, &attr);
7551 have_location_lists = true;
7554 static inline dw_loc_list_ref
7555 AT_loc_list (dw_attr_ref a)
7557 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7558 return a->dw_attr_val.v.val_loc_list;
7561 static inline dw_loc_list_ref *
7562 AT_loc_list_ptr (dw_attr_ref a)
7564 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7565 return &a->dw_attr_val.v.val_loc_list;
7568 /* Add an address constant attribute value to a DIE. */
7570 static inline void
7571 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7573 dw_attr_node attr;
7575 attr.dw_attr = attr_kind;
7576 attr.dw_attr_val.val_class = dw_val_class_addr;
7577 attr.dw_attr_val.v.val_addr = addr;
7578 add_dwarf_attr (die, &attr);
7581 /* Get the RTX from to an address DIE attribute. */
7583 static inline rtx
7584 AT_addr (dw_attr_ref a)
7586 gcc_assert (a && AT_class (a) == dw_val_class_addr);
7587 return a->dw_attr_val.v.val_addr;
7590 /* Add a file attribute value to a DIE. */
7592 static inline void
7593 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7594 struct dwarf_file_data *fd)
7596 dw_attr_node attr;
7598 attr.dw_attr = attr_kind;
7599 attr.dw_attr_val.val_class = dw_val_class_file;
7600 attr.dw_attr_val.v.val_file = fd;
7601 add_dwarf_attr (die, &attr);
7604 /* Get the dwarf_file_data from a file DIE attribute. */
7606 static inline struct dwarf_file_data *
7607 AT_file (dw_attr_ref a)
7609 gcc_assert (a && AT_class (a) == dw_val_class_file);
7610 return a->dw_attr_val.v.val_file;
7613 /* Add a vms delta attribute value to a DIE. */
7615 static inline void
7616 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7617 const char *lbl1, const char *lbl2)
7619 dw_attr_node attr;
7621 attr.dw_attr = attr_kind;
7622 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7623 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7624 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7625 add_dwarf_attr (die, &attr);
7628 /* Add a label identifier attribute value to a DIE. */
7630 static inline void
7631 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7633 dw_attr_node attr;
7635 attr.dw_attr = attr_kind;
7636 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7637 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7638 add_dwarf_attr (die, &attr);
7641 /* Add a section offset attribute value to a DIE, an offset into the
7642 debug_line section. */
7644 static inline void
7645 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7646 const char *label)
7648 dw_attr_node attr;
7650 attr.dw_attr = attr_kind;
7651 attr.dw_attr_val.val_class = dw_val_class_lineptr;
7652 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7653 add_dwarf_attr (die, &attr);
7656 /* Add a section offset attribute value to a DIE, an offset into the
7657 debug_macinfo section. */
7659 static inline void
7660 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7661 const char *label)
7663 dw_attr_node attr;
7665 attr.dw_attr = attr_kind;
7666 attr.dw_attr_val.val_class = dw_val_class_macptr;
7667 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7668 add_dwarf_attr (die, &attr);
7671 /* Add an offset attribute value to a DIE. */
7673 static inline void
7674 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7675 unsigned HOST_WIDE_INT offset)
7677 dw_attr_node attr;
7679 attr.dw_attr = attr_kind;
7680 attr.dw_attr_val.val_class = dw_val_class_offset;
7681 attr.dw_attr_val.v.val_offset = offset;
7682 add_dwarf_attr (die, &attr);
7685 /* Add an range_list attribute value to a DIE. */
7687 static void
7688 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7689 long unsigned int offset)
7691 dw_attr_node attr;
7693 attr.dw_attr = attr_kind;
7694 attr.dw_attr_val.val_class = dw_val_class_range_list;
7695 attr.dw_attr_val.v.val_offset = offset;
7696 add_dwarf_attr (die, &attr);
7699 /* Return the start label of a delta attribute. */
7701 static inline const char *
7702 AT_vms_delta1 (dw_attr_ref a)
7704 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7705 return a->dw_attr_val.v.val_vms_delta.lbl1;
7708 /* Return the end label of a delta attribute. */
7710 static inline const char *
7711 AT_vms_delta2 (dw_attr_ref a)
7713 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7714 return a->dw_attr_val.v.val_vms_delta.lbl2;
7717 static inline const char *
7718 AT_lbl (dw_attr_ref a)
7720 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7721 || AT_class (a) == dw_val_class_lineptr
7722 || AT_class (a) == dw_val_class_macptr));
7723 return a->dw_attr_val.v.val_lbl_id;
7726 /* Get the attribute of type attr_kind. */
7728 static dw_attr_ref
7729 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7731 dw_attr_ref a;
7732 unsigned ix;
7733 dw_die_ref spec = NULL;
7735 if (! die)
7736 return NULL;
7738 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7739 if (a->dw_attr == attr_kind)
7740 return a;
7741 else if (a->dw_attr == DW_AT_specification
7742 || a->dw_attr == DW_AT_abstract_origin)
7743 spec = AT_ref (a);
7745 if (spec)
7746 return get_AT (spec, attr_kind);
7748 return NULL;
7751 /* Return the "low pc" attribute value, typically associated with a subprogram
7752 DIE. Return null if the "low pc" attribute is either not present, or if it
7753 cannot be represented as an assembler label identifier. */
7755 static inline const char *
7756 get_AT_low_pc (dw_die_ref die)
7758 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7760 return a ? AT_lbl (a) : NULL;
7763 /* Return the "high pc" attribute value, typically associated with a subprogram
7764 DIE. Return null if the "high pc" attribute is either not present, or if it
7765 cannot be represented as an assembler label identifier. */
7767 static inline const char *
7768 get_AT_hi_pc (dw_die_ref die)
7770 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7772 return a ? AT_lbl (a) : NULL;
7775 /* Return the value of the string attribute designated by ATTR_KIND, or
7776 NULL if it is not present. */
7778 static inline const char *
7779 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7781 dw_attr_ref a = get_AT (die, attr_kind);
7783 return a ? AT_string (a) : NULL;
7786 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7787 if it is not present. */
7789 static inline int
7790 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7792 dw_attr_ref a = get_AT (die, attr_kind);
7794 return a ? AT_flag (a) : 0;
7797 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7798 if it is not present. */
7800 static inline unsigned
7801 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7803 dw_attr_ref a = get_AT (die, attr_kind);
7805 return a ? AT_unsigned (a) : 0;
7808 static inline dw_die_ref
7809 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7811 dw_attr_ref a = get_AT (die, attr_kind);
7813 return a ? AT_ref (a) : NULL;
7816 static inline struct dwarf_file_data *
7817 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7819 dw_attr_ref a = get_AT (die, attr_kind);
7821 return a ? AT_file (a) : NULL;
7824 /* Return TRUE if the language is C++. */
7826 static inline bool
7827 is_cxx (void)
7829 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7831 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7834 /* Return TRUE if the language is Fortran. */
7836 static inline bool
7837 is_fortran (void)
7839 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7841 return (lang == DW_LANG_Fortran77
7842 || lang == DW_LANG_Fortran90
7843 || lang == DW_LANG_Fortran95);
7846 /* Return TRUE if the language is Ada. */
7848 static inline bool
7849 is_ada (void)
7851 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7853 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7856 /* Remove the specified attribute if present. */
7858 static void
7859 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7861 dw_attr_ref a;
7862 unsigned ix;
7864 if (! die)
7865 return;
7867 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7868 if (a->dw_attr == attr_kind)
7870 if (AT_class (a) == dw_val_class_str)
7871 if (a->dw_attr_val.v.val_str->refcount)
7872 a->dw_attr_val.v.val_str->refcount--;
7874 /* VEC_ordered_remove should help reduce the number of abbrevs
7875 that are needed. */
7876 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7877 return;
7881 /* Remove CHILD from its parent. PREV must have the property that
7882 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
7884 static void
7885 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7887 gcc_assert (child->die_parent == prev->die_parent);
7888 gcc_assert (prev->die_sib == child);
7889 if (prev == child)
7891 gcc_assert (child->die_parent->die_child == child);
7892 prev = NULL;
7894 else
7895 prev->die_sib = child->die_sib;
7896 if (child->die_parent->die_child == child)
7897 child->die_parent->die_child = prev;
7900 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
7901 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
7903 static void
7904 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7906 dw_die_ref parent = old_child->die_parent;
7908 gcc_assert (parent == prev->die_parent);
7909 gcc_assert (prev->die_sib == old_child);
7911 new_child->die_parent = parent;
7912 if (prev == old_child)
7914 gcc_assert (parent->die_child == old_child);
7915 new_child->die_sib = new_child;
7917 else
7919 prev->die_sib = new_child;
7920 new_child->die_sib = old_child->die_sib;
7922 if (old_child->die_parent->die_child == old_child)
7923 old_child->die_parent->die_child = new_child;
7926 /* Move all children from OLD_PARENT to NEW_PARENT. */
7928 static void
7929 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7931 dw_die_ref c;
7932 new_parent->die_child = old_parent->die_child;
7933 old_parent->die_child = NULL;
7934 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7937 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
7938 matches TAG. */
7940 static void
7941 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7943 dw_die_ref c;
7945 c = die->die_child;
7946 if (c) do {
7947 dw_die_ref prev = c;
7948 c = c->die_sib;
7949 while (c->die_tag == tag)
7951 remove_child_with_prev (c, prev);
7952 /* Might have removed every child. */
7953 if (c == c->die_sib)
7954 return;
7955 c = c->die_sib;
7957 } while (c != die->die_child);
7960 /* Add a CHILD_DIE as the last child of DIE. */
7962 static void
7963 add_child_die (dw_die_ref die, dw_die_ref child_die)
7965 /* FIXME this should probably be an assert. */
7966 if (! die || ! child_die)
7967 return;
7968 gcc_assert (die != child_die);
7970 child_die->die_parent = die;
7971 if (die->die_child)
7973 child_die->die_sib = die->die_child->die_sib;
7974 die->die_child->die_sib = child_die;
7976 else
7977 child_die->die_sib = child_die;
7978 die->die_child = child_die;
7981 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7982 is the specification, to the end of PARENT's list of children.
7983 This is done by removing and re-adding it. */
7985 static void
7986 splice_child_die (dw_die_ref parent, dw_die_ref child)
7988 dw_die_ref p;
7990 /* We want the declaration DIE from inside the class, not the
7991 specification DIE at toplevel. */
7992 if (child->die_parent != parent)
7994 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7996 if (tmp)
7997 child = tmp;
8000 gcc_assert (child->die_parent == parent
8001 || (child->die_parent
8002 == get_AT_ref (parent, DW_AT_specification)));
8004 for (p = child->die_parent->die_child; ; p = p->die_sib)
8005 if (p->die_sib == child)
8007 remove_child_with_prev (child, p);
8008 break;
8011 add_child_die (parent, child);
8014 /* Return a pointer to a newly created DIE node. */
8016 static inline dw_die_ref
8017 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8019 dw_die_ref die = ggc_alloc_cleared_die_node ();
8021 die->die_tag = tag_value;
8023 if (parent_die != NULL)
8024 add_child_die (parent_die, die);
8025 else
8027 limbo_die_node *limbo_node;
8029 limbo_node = ggc_alloc_cleared_limbo_die_node ();
8030 limbo_node->die = die;
8031 limbo_node->created_for = t;
8032 limbo_node->next = limbo_die_list;
8033 limbo_die_list = limbo_node;
8036 return die;
8039 /* Return the DIE associated with the given type specifier. */
8041 static inline dw_die_ref
8042 lookup_type_die (tree type)
8044 return TYPE_SYMTAB_DIE (type);
8047 /* Like lookup_type_die, but if type is an anonymous type named by a
8048 typedef[1], return the DIE of the anonymous type instead the one of
8049 the naming typedef. This is because in gen_typedef_die, we did
8050 equate the anonymous struct named by the typedef with the DIE of
8051 the naming typedef. So by default, lookup_type_die on an anonymous
8052 struct yields the DIE of the naming typedef.
8054 [1]: Read the comment of is_naming_typedef_decl to learn about what
8055 a naming typedef is. */
8057 static inline dw_die_ref
8058 lookup_type_die_strip_naming_typedef (tree type)
8060 dw_die_ref die = lookup_type_die (type);
8061 if (TREE_CODE (type) == RECORD_TYPE
8062 && die->die_tag == DW_TAG_typedef
8063 && is_naming_typedef_decl (TYPE_NAME (type)))
8064 die = get_AT_ref (die, DW_AT_type);
8065 return die;
8068 /* Equate a DIE to a given type specifier. */
8070 static inline void
8071 equate_type_number_to_die (tree type, dw_die_ref type_die)
8073 TYPE_SYMTAB_DIE (type) = type_die;
8076 /* Returns a hash value for X (which really is a die_struct). */
8078 static hashval_t
8079 decl_die_table_hash (const void *x)
8081 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8084 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
8086 static int
8087 decl_die_table_eq (const void *x, const void *y)
8089 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8092 /* Return the DIE associated with a given declaration. */
8094 static inline dw_die_ref
8095 lookup_decl_die (tree decl)
8097 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8100 /* Returns a hash value for X (which really is a var_loc_list). */
8102 static hashval_t
8103 decl_loc_table_hash (const void *x)
8105 return (hashval_t) ((const var_loc_list *) x)->decl_id;
8108 /* Return nonzero if decl_id of var_loc_list X is the same as
8109 UID of decl *Y. */
8111 static int
8112 decl_loc_table_eq (const void *x, const void *y)
8114 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8117 /* Return the var_loc list associated with a given declaration. */
8119 static inline var_loc_list *
8120 lookup_decl_loc (const_tree decl)
8122 if (!decl_loc_table)
8123 return NULL;
8124 return (var_loc_list *)
8125 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8128 /* Equate a DIE to a particular declaration. */
8130 static void
8131 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8133 unsigned int decl_id = DECL_UID (decl);
8134 void **slot;
8136 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8137 *slot = decl_die;
8138 decl_die->decl_id = decl_id;
8141 /* Return how many bits covers PIECE EXPR_LIST. */
8143 static int
8144 decl_piece_bitsize (rtx piece)
8146 int ret = (int) GET_MODE (piece);
8147 if (ret)
8148 return ret;
8149 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8150 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8151 return INTVAL (XEXP (XEXP (piece, 0), 0));
8154 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
8156 static rtx *
8157 decl_piece_varloc_ptr (rtx piece)
8159 if ((int) GET_MODE (piece))
8160 return &XEXP (piece, 0);
8161 else
8162 return &XEXP (XEXP (piece, 0), 1);
8165 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8166 Next is the chain of following piece nodes. */
8168 static rtx
8169 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8171 if (bitsize <= (int) MAX_MACHINE_MODE)
8172 return alloc_EXPR_LIST (bitsize, loc_note, next);
8173 else
8174 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8175 GEN_INT (bitsize),
8176 loc_note), next);
8179 /* Return rtx that should be stored into loc field for
8180 LOC_NOTE and BITPOS/BITSIZE. */
8182 static rtx
8183 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8184 HOST_WIDE_INT bitsize)
8186 if (bitsize != -1)
8188 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8189 if (bitpos != 0)
8190 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8192 return loc_note;
8195 /* This function either modifies location piece list *DEST in
8196 place (if SRC and INNER is NULL), or copies location piece list
8197 *SRC to *DEST while modifying it. Location BITPOS is modified
8198 to contain LOC_NOTE, any pieces overlapping it are removed resp.
8199 not copied and if needed some padding around it is added.
8200 When modifying in place, DEST should point to EXPR_LIST where
8201 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8202 to the start of the whole list and INNER points to the EXPR_LIST
8203 where earlier pieces cover PIECE_BITPOS bits. */
8205 static void
8206 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8207 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8208 HOST_WIDE_INT bitsize, rtx loc_note)
8210 int diff;
8211 bool copy = inner != NULL;
8213 if (copy)
8215 /* First copy all nodes preceeding the current bitpos. */
8216 while (src != inner)
8218 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8219 decl_piece_bitsize (*src), NULL_RTX);
8220 dest = &XEXP (*dest, 1);
8221 src = &XEXP (*src, 1);
8224 /* Add padding if needed. */
8225 if (bitpos != piece_bitpos)
8227 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8228 copy ? NULL_RTX : *dest);
8229 dest = &XEXP (*dest, 1);
8231 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8233 gcc_assert (!copy);
8234 /* A piece with correct bitpos and bitsize already exist,
8235 just update the location for it and return. */
8236 *decl_piece_varloc_ptr (*dest) = loc_note;
8237 return;
8239 /* Add the piece that changed. */
8240 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8241 dest = &XEXP (*dest, 1);
8242 /* Skip over pieces that overlap it. */
8243 diff = bitpos - piece_bitpos + bitsize;
8244 if (!copy)
8245 src = dest;
8246 while (diff > 0 && *src)
8248 rtx piece = *src;
8249 diff -= decl_piece_bitsize (piece);
8250 if (copy)
8251 src = &XEXP (piece, 1);
8252 else
8254 *src = XEXP (piece, 1);
8255 free_EXPR_LIST_node (piece);
8258 /* Add padding if needed. */
8259 if (diff < 0 && *src)
8261 if (!copy)
8262 dest = src;
8263 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8264 dest = &XEXP (*dest, 1);
8266 if (!copy)
8267 return;
8268 /* Finally copy all nodes following it. */
8269 while (*src)
8271 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8272 decl_piece_bitsize (*src), NULL_RTX);
8273 dest = &XEXP (*dest, 1);
8274 src = &XEXP (*src, 1);
8278 /* Add a variable location node to the linked list for DECL. */
8280 static struct var_loc_node *
8281 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8283 unsigned int decl_id;
8284 var_loc_list *temp;
8285 void **slot;
8286 struct var_loc_node *loc = NULL;
8287 HOST_WIDE_INT bitsize = -1, bitpos = -1;
8289 if (DECL_DEBUG_EXPR_IS_FROM (decl))
8291 tree realdecl = DECL_DEBUG_EXPR (decl);
8292 if (realdecl && handled_component_p (realdecl))
8294 HOST_WIDE_INT maxsize;
8295 tree innerdecl;
8296 innerdecl
8297 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8298 if (!DECL_P (innerdecl)
8299 || DECL_IGNORED_P (innerdecl)
8300 || TREE_STATIC (innerdecl)
8301 || bitsize <= 0
8302 || bitpos + bitsize > 256
8303 || bitsize != maxsize)
8304 return NULL;
8305 decl = innerdecl;
8309 decl_id = DECL_UID (decl);
8310 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8311 if (*slot == NULL)
8313 temp = ggc_alloc_cleared_var_loc_list ();
8314 temp->decl_id = decl_id;
8315 *slot = temp;
8317 else
8318 temp = (var_loc_list *) *slot;
8320 if (temp->last)
8322 struct var_loc_node *last = temp->last, *unused = NULL;
8323 rtx *piece_loc = NULL, last_loc_note;
8324 int piece_bitpos = 0;
8325 if (last->next)
8327 last = last->next;
8328 gcc_assert (last->next == NULL);
8330 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8332 piece_loc = &last->loc;
8335 int cur_bitsize = decl_piece_bitsize (*piece_loc);
8336 if (piece_bitpos + cur_bitsize > bitpos)
8337 break;
8338 piece_bitpos += cur_bitsize;
8339 piece_loc = &XEXP (*piece_loc, 1);
8341 while (*piece_loc);
8343 /* TEMP->LAST here is either pointer to the last but one or
8344 last element in the chained list, LAST is pointer to the
8345 last element. */
8346 if (label && strcmp (last->label, label) == 0)
8348 /* For SRA optimized variables if there weren't any real
8349 insns since last note, just modify the last node. */
8350 if (piece_loc != NULL)
8352 adjust_piece_list (piece_loc, NULL, NULL,
8353 bitpos, piece_bitpos, bitsize, loc_note);
8354 return NULL;
8356 /* If the last note doesn't cover any instructions, remove it. */
8357 if (temp->last != last)
8359 temp->last->next = NULL;
8360 unused = last;
8361 last = temp->last;
8362 gcc_assert (strcmp (last->label, label) != 0);
8364 else
8366 gcc_assert (temp->first == temp->last);
8367 memset (temp->last, '\0', sizeof (*temp->last));
8368 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8369 return temp->last;
8372 if (bitsize == -1 && NOTE_P (last->loc))
8373 last_loc_note = last->loc;
8374 else if (piece_loc != NULL
8375 && *piece_loc != NULL_RTX
8376 && piece_bitpos == bitpos
8377 && decl_piece_bitsize (*piece_loc) == bitsize)
8378 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8379 else
8380 last_loc_note = NULL_RTX;
8381 /* If the current location is the same as the end of the list,
8382 and either both or neither of the locations is uninitialized,
8383 we have nothing to do. */
8384 if (last_loc_note == NULL_RTX
8385 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8386 NOTE_VAR_LOCATION_LOC (loc_note)))
8387 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8388 != NOTE_VAR_LOCATION_STATUS (loc_note))
8389 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8390 == VAR_INIT_STATUS_UNINITIALIZED)
8391 || (NOTE_VAR_LOCATION_STATUS (loc_note)
8392 == VAR_INIT_STATUS_UNINITIALIZED))))
8394 /* Add LOC to the end of list and update LAST. If the last
8395 element of the list has been removed above, reuse its
8396 memory for the new node, otherwise allocate a new one. */
8397 if (unused)
8399 loc = unused;
8400 memset (loc, '\0', sizeof (*loc));
8402 else
8403 loc = ggc_alloc_cleared_var_loc_node ();
8404 if (bitsize == -1 || piece_loc == NULL)
8405 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8406 else
8407 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8408 bitpos, piece_bitpos, bitsize, loc_note);
8409 last->next = loc;
8410 /* Ensure TEMP->LAST will point either to the new last but one
8411 element of the chain, or to the last element in it. */
8412 if (last != temp->last)
8413 temp->last = last;
8415 else if (unused)
8416 ggc_free (unused);
8418 else
8420 loc = ggc_alloc_cleared_var_loc_node ();
8421 temp->first = loc;
8422 temp->last = loc;
8423 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8425 return loc;
8428 /* Keep track of the number of spaces used to indent the
8429 output of the debugging routines that print the structure of
8430 the DIE internal representation. */
8431 static int print_indent;
8433 /* Indent the line the number of spaces given by print_indent. */
8435 static inline void
8436 print_spaces (FILE *outfile)
8438 fprintf (outfile, "%*s", print_indent, "");
8441 /* Print a type signature in hex. */
8443 static inline void
8444 print_signature (FILE *outfile, char *sig)
8446 int i;
8448 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8449 fprintf (outfile, "%02x", sig[i] & 0xff);
8452 /* Print the information associated with a given DIE, and its children.
8453 This routine is a debugging aid only. */
8455 static void
8456 print_die (dw_die_ref die, FILE *outfile)
8458 dw_attr_ref a;
8459 dw_die_ref c;
8460 unsigned ix;
8462 print_spaces (outfile);
8463 fprintf (outfile, "DIE %4ld: %s (%p)\n",
8464 die->die_offset, dwarf_tag_name (die->die_tag),
8465 (void*) die);
8466 print_spaces (outfile);
8467 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
8468 fprintf (outfile, " offset: %ld", die->die_offset);
8469 fprintf (outfile, " mark: %d\n", die->die_mark);
8471 if (dwarf_version >= 4 && die->die_id.die_type_node)
8473 print_spaces (outfile);
8474 fprintf (outfile, " signature: ");
8475 print_signature (outfile, die->die_id.die_type_node->signature);
8476 fprintf (outfile, "\n");
8479 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8481 print_spaces (outfile);
8482 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
8484 switch (AT_class (a))
8486 case dw_val_class_addr:
8487 fprintf (outfile, "address");
8488 break;
8489 case dw_val_class_offset:
8490 fprintf (outfile, "offset");
8491 break;
8492 case dw_val_class_loc:
8493 fprintf (outfile, "location descriptor");
8494 break;
8495 case dw_val_class_loc_list:
8496 fprintf (outfile, "location list -> label:%s",
8497 AT_loc_list (a)->ll_symbol);
8498 break;
8499 case dw_val_class_range_list:
8500 fprintf (outfile, "range list");
8501 break;
8502 case dw_val_class_const:
8503 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8504 break;
8505 case dw_val_class_unsigned_const:
8506 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8507 break;
8508 case dw_val_class_const_double:
8509 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8510 HOST_WIDE_INT_PRINT_UNSIGNED")",
8511 a->dw_attr_val.v.val_double.high,
8512 a->dw_attr_val.v.val_double.low);
8513 break;
8514 case dw_val_class_vec:
8515 fprintf (outfile, "floating-point or vector constant");
8516 break;
8517 case dw_val_class_flag:
8518 fprintf (outfile, "%u", AT_flag (a));
8519 break;
8520 case dw_val_class_die_ref:
8521 if (AT_ref (a) != NULL)
8523 if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8525 fprintf (outfile, "die -> signature: ");
8526 print_signature (outfile,
8527 AT_ref (a)->die_id.die_type_node->signature);
8529 else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8530 fprintf (outfile, "die -> label: %s",
8531 AT_ref (a)->die_id.die_symbol);
8532 else
8533 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8534 fprintf (outfile, " (%p)", (void *) AT_ref (a));
8536 else
8537 fprintf (outfile, "die -> <null>");
8538 break;
8539 case dw_val_class_vms_delta:
8540 fprintf (outfile, "delta: @slotcount(%s-%s)",
8541 AT_vms_delta2 (a), AT_vms_delta1 (a));
8542 break;
8543 case dw_val_class_lbl_id:
8544 case dw_val_class_lineptr:
8545 case dw_val_class_macptr:
8546 fprintf (outfile, "label: %s", AT_lbl (a));
8547 break;
8548 case dw_val_class_str:
8549 if (AT_string (a) != NULL)
8550 fprintf (outfile, "\"%s\"", AT_string (a));
8551 else
8552 fprintf (outfile, "<null>");
8553 break;
8554 case dw_val_class_file:
8555 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8556 AT_file (a)->emitted_number);
8557 break;
8558 case dw_val_class_data8:
8560 int i;
8562 for (i = 0; i < 8; i++)
8563 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8564 break;
8566 default:
8567 break;
8570 fprintf (outfile, "\n");
8573 if (die->die_child != NULL)
8575 print_indent += 4;
8576 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8577 print_indent -= 4;
8579 if (print_indent == 0)
8580 fprintf (outfile, "\n");
8583 /* Print the contents of the source code line number correspondence table.
8584 This routine is a debugging aid only. */
8586 static void
8587 print_dwarf_line_table (FILE *outfile)
8589 unsigned i;
8590 dw_line_info_ref line_info;
8592 fprintf (outfile, "\n\nDWARF source line information\n");
8593 for (i = 1; i < line_info_table_in_use; i++)
8595 line_info = &line_info_table[i];
8596 fprintf (outfile, "%5d: %4ld %6ld\n", i,
8597 line_info->dw_file_num,
8598 line_info->dw_line_num);
8601 fprintf (outfile, "\n\n");
8604 /* Print the information collected for a given DIE. */
8606 DEBUG_FUNCTION void
8607 debug_dwarf_die (dw_die_ref die)
8609 print_die (die, stderr);
8612 /* Print all DWARF information collected for the compilation unit.
8613 This routine is a debugging aid only. */
8615 DEBUG_FUNCTION void
8616 debug_dwarf (void)
8618 print_indent = 0;
8619 print_die (comp_unit_die (), stderr);
8620 if (! DWARF2_ASM_LINE_DEBUG_INFO)
8621 print_dwarf_line_table (stderr);
8624 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
8625 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
8626 DIE that marks the start of the DIEs for this include file. */
8628 static dw_die_ref
8629 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8631 const char *filename = get_AT_string (bincl_die, DW_AT_name);
8632 dw_die_ref new_unit = gen_compile_unit_die (filename);
8634 new_unit->die_sib = old_unit;
8635 return new_unit;
8638 /* Close an include-file CU and reopen the enclosing one. */
8640 static dw_die_ref
8641 pop_compile_unit (dw_die_ref old_unit)
8643 dw_die_ref new_unit = old_unit->die_sib;
8645 old_unit->die_sib = NULL;
8646 return new_unit;
8649 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8650 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8652 /* Calculate the checksum of a location expression. */
8654 static inline void
8655 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8657 int tem;
8659 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8660 CHECKSUM (tem);
8661 CHECKSUM (loc->dw_loc_oprnd1);
8662 CHECKSUM (loc->dw_loc_oprnd2);
8665 /* Calculate the checksum of an attribute. */
8667 static void
8668 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8670 dw_loc_descr_ref loc;
8671 rtx r;
8673 CHECKSUM (at->dw_attr);
8675 /* We don't care that this was compiled with a different compiler
8676 snapshot; if the output is the same, that's what matters. */
8677 if (at->dw_attr == DW_AT_producer)
8678 return;
8680 switch (AT_class (at))
8682 case dw_val_class_const:
8683 CHECKSUM (at->dw_attr_val.v.val_int);
8684 break;
8685 case dw_val_class_unsigned_const:
8686 CHECKSUM (at->dw_attr_val.v.val_unsigned);
8687 break;
8688 case dw_val_class_const_double:
8689 CHECKSUM (at->dw_attr_val.v.val_double);
8690 break;
8691 case dw_val_class_vec:
8692 CHECKSUM (at->dw_attr_val.v.val_vec);
8693 break;
8694 case dw_val_class_flag:
8695 CHECKSUM (at->dw_attr_val.v.val_flag);
8696 break;
8697 case dw_val_class_str:
8698 CHECKSUM_STRING (AT_string (at));
8699 break;
8701 case dw_val_class_addr:
8702 r = AT_addr (at);
8703 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8704 CHECKSUM_STRING (XSTR (r, 0));
8705 break;
8707 case dw_val_class_offset:
8708 CHECKSUM (at->dw_attr_val.v.val_offset);
8709 break;
8711 case dw_val_class_loc:
8712 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8713 loc_checksum (loc, ctx);
8714 break;
8716 case dw_val_class_die_ref:
8717 die_checksum (AT_ref (at), ctx, mark);
8718 break;
8720 case dw_val_class_fde_ref:
8721 case dw_val_class_vms_delta:
8722 case dw_val_class_lbl_id:
8723 case dw_val_class_lineptr:
8724 case dw_val_class_macptr:
8725 break;
8727 case dw_val_class_file:
8728 CHECKSUM_STRING (AT_file (at)->filename);
8729 break;
8731 case dw_val_class_data8:
8732 CHECKSUM (at->dw_attr_val.v.val_data8);
8733 break;
8735 default:
8736 break;
8740 /* Calculate the checksum of a DIE. */
8742 static void
8743 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8745 dw_die_ref c;
8746 dw_attr_ref a;
8747 unsigned ix;
8749 /* To avoid infinite recursion. */
8750 if (die->die_mark)
8752 CHECKSUM (die->die_mark);
8753 return;
8755 die->die_mark = ++(*mark);
8757 CHECKSUM (die->die_tag);
8759 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8760 attr_checksum (a, ctx, mark);
8762 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8765 #undef CHECKSUM
8766 #undef CHECKSUM_STRING
8768 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
8769 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8770 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8771 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8772 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8773 #define CHECKSUM_ATTR(FOO) \
8774 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8776 /* Calculate the checksum of a number in signed LEB128 format. */
8778 static void
8779 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8781 unsigned char byte;
8782 bool more;
8784 while (1)
8786 byte = (value & 0x7f);
8787 value >>= 7;
8788 more = !((value == 0 && (byte & 0x40) == 0)
8789 || (value == -1 && (byte & 0x40) != 0));
8790 if (more)
8791 byte |= 0x80;
8792 CHECKSUM (byte);
8793 if (!more)
8794 break;
8798 /* Calculate the checksum of a number in unsigned LEB128 format. */
8800 static void
8801 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8803 while (1)
8805 unsigned char byte = (value & 0x7f);
8806 value >>= 7;
8807 if (value != 0)
8808 /* More bytes to follow. */
8809 byte |= 0x80;
8810 CHECKSUM (byte);
8811 if (value == 0)
8812 break;
8816 /* Checksum the context of the DIE. This adds the names of any
8817 surrounding namespaces or structures to the checksum. */
8819 static void
8820 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8822 const char *name;
8823 dw_die_ref spec;
8824 int tag = die->die_tag;
8826 if (tag != DW_TAG_namespace
8827 && tag != DW_TAG_structure_type
8828 && tag != DW_TAG_class_type)
8829 return;
8831 name = get_AT_string (die, DW_AT_name);
8833 spec = get_AT_ref (die, DW_AT_specification);
8834 if (spec != NULL)
8835 die = spec;
8837 if (die->die_parent != NULL)
8838 checksum_die_context (die->die_parent, ctx);
8840 CHECKSUM_ULEB128 ('C');
8841 CHECKSUM_ULEB128 (tag);
8842 if (name != NULL)
8843 CHECKSUM_STRING (name);
8846 /* Calculate the checksum of a location expression. */
8848 static inline void
8849 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8851 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8852 were emitted as a DW_FORM_sdata instead of a location expression. */
8853 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8855 CHECKSUM_ULEB128 (DW_FORM_sdata);
8856 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8857 return;
8860 /* Otherwise, just checksum the raw location expression. */
8861 while (loc != NULL)
8863 CHECKSUM_ULEB128 (loc->dw_loc_opc);
8864 CHECKSUM (loc->dw_loc_oprnd1);
8865 CHECKSUM (loc->dw_loc_oprnd2);
8866 loc = loc->dw_loc_next;
8870 /* Calculate the checksum of an attribute. */
8872 static void
8873 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8874 struct md5_ctx *ctx, int *mark)
8876 dw_loc_descr_ref loc;
8877 rtx r;
8879 if (AT_class (at) == dw_val_class_die_ref)
8881 dw_die_ref target_die = AT_ref (at);
8883 /* For pointer and reference types, we checksum only the (qualified)
8884 name of the target type (if there is a name). For friend entries,
8885 we checksum only the (qualified) name of the target type or function.
8886 This allows the checksum to remain the same whether the target type
8887 is complete or not. */
8888 if ((at->dw_attr == DW_AT_type
8889 && (tag == DW_TAG_pointer_type
8890 || tag == DW_TAG_reference_type
8891 || tag == DW_TAG_rvalue_reference_type
8892 || tag == DW_TAG_ptr_to_member_type))
8893 || (at->dw_attr == DW_AT_friend
8894 && tag == DW_TAG_friend))
8896 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8898 if (name_attr != NULL)
8900 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8902 if (decl == NULL)
8903 decl = target_die;
8904 CHECKSUM_ULEB128 ('N');
8905 CHECKSUM_ULEB128 (at->dw_attr);
8906 if (decl->die_parent != NULL)
8907 checksum_die_context (decl->die_parent, ctx);
8908 CHECKSUM_ULEB128 ('E');
8909 CHECKSUM_STRING (AT_string (name_attr));
8910 return;
8914 /* For all other references to another DIE, we check to see if the
8915 target DIE has already been visited. If it has, we emit a
8916 backward reference; if not, we descend recursively. */
8917 if (target_die->die_mark > 0)
8919 CHECKSUM_ULEB128 ('R');
8920 CHECKSUM_ULEB128 (at->dw_attr);
8921 CHECKSUM_ULEB128 (target_die->die_mark);
8923 else
8925 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8927 if (decl == NULL)
8928 decl = target_die;
8929 target_die->die_mark = ++(*mark);
8930 CHECKSUM_ULEB128 ('T');
8931 CHECKSUM_ULEB128 (at->dw_attr);
8932 if (decl->die_parent != NULL)
8933 checksum_die_context (decl->die_parent, ctx);
8934 die_checksum_ordered (target_die, ctx, mark);
8936 return;
8939 CHECKSUM_ULEB128 ('A');
8940 CHECKSUM_ULEB128 (at->dw_attr);
8942 switch (AT_class (at))
8944 case dw_val_class_const:
8945 CHECKSUM_ULEB128 (DW_FORM_sdata);
8946 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8947 break;
8949 case dw_val_class_unsigned_const:
8950 CHECKSUM_ULEB128 (DW_FORM_sdata);
8951 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8952 break;
8954 case dw_val_class_const_double:
8955 CHECKSUM_ULEB128 (DW_FORM_block);
8956 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8957 CHECKSUM (at->dw_attr_val.v.val_double);
8958 break;
8960 case dw_val_class_vec:
8961 CHECKSUM_ULEB128 (DW_FORM_block);
8962 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8963 CHECKSUM (at->dw_attr_val.v.val_vec);
8964 break;
8966 case dw_val_class_flag:
8967 CHECKSUM_ULEB128 (DW_FORM_flag);
8968 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8969 break;
8971 case dw_val_class_str:
8972 CHECKSUM_ULEB128 (DW_FORM_string);
8973 CHECKSUM_STRING (AT_string (at));
8974 break;
8976 case dw_val_class_addr:
8977 r = AT_addr (at);
8978 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8979 CHECKSUM_ULEB128 (DW_FORM_string);
8980 CHECKSUM_STRING (XSTR (r, 0));
8981 break;
8983 case dw_val_class_offset:
8984 CHECKSUM_ULEB128 (DW_FORM_sdata);
8985 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8986 break;
8988 case dw_val_class_loc:
8989 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8990 loc_checksum_ordered (loc, ctx);
8991 break;
8993 case dw_val_class_fde_ref:
8994 case dw_val_class_lbl_id:
8995 case dw_val_class_lineptr:
8996 case dw_val_class_macptr:
8997 break;
8999 case dw_val_class_file:
9000 CHECKSUM_ULEB128 (DW_FORM_string);
9001 CHECKSUM_STRING (AT_file (at)->filename);
9002 break;
9004 case dw_val_class_data8:
9005 CHECKSUM (at->dw_attr_val.v.val_data8);
9006 break;
9008 default:
9009 break;
9013 struct checksum_attributes
9015 dw_attr_ref at_name;
9016 dw_attr_ref at_type;
9017 dw_attr_ref at_friend;
9018 dw_attr_ref at_accessibility;
9019 dw_attr_ref at_address_class;
9020 dw_attr_ref at_allocated;
9021 dw_attr_ref at_artificial;
9022 dw_attr_ref at_associated;
9023 dw_attr_ref at_binary_scale;
9024 dw_attr_ref at_bit_offset;
9025 dw_attr_ref at_bit_size;
9026 dw_attr_ref at_bit_stride;
9027 dw_attr_ref at_byte_size;
9028 dw_attr_ref at_byte_stride;
9029 dw_attr_ref at_const_value;
9030 dw_attr_ref at_containing_type;
9031 dw_attr_ref at_count;
9032 dw_attr_ref at_data_location;
9033 dw_attr_ref at_data_member_location;
9034 dw_attr_ref at_decimal_scale;
9035 dw_attr_ref at_decimal_sign;
9036 dw_attr_ref at_default_value;
9037 dw_attr_ref at_digit_count;
9038 dw_attr_ref at_discr;
9039 dw_attr_ref at_discr_list;
9040 dw_attr_ref at_discr_value;
9041 dw_attr_ref at_encoding;
9042 dw_attr_ref at_endianity;
9043 dw_attr_ref at_explicit;
9044 dw_attr_ref at_is_optional;
9045 dw_attr_ref at_location;
9046 dw_attr_ref at_lower_bound;
9047 dw_attr_ref at_mutable;
9048 dw_attr_ref at_ordering;
9049 dw_attr_ref at_picture_string;
9050 dw_attr_ref at_prototyped;
9051 dw_attr_ref at_small;
9052 dw_attr_ref at_segment;
9053 dw_attr_ref at_string_length;
9054 dw_attr_ref at_threads_scaled;
9055 dw_attr_ref at_upper_bound;
9056 dw_attr_ref at_use_location;
9057 dw_attr_ref at_use_UTF8;
9058 dw_attr_ref at_variable_parameter;
9059 dw_attr_ref at_virtuality;
9060 dw_attr_ref at_visibility;
9061 dw_attr_ref at_vtable_elem_location;
9064 /* Collect the attributes that we will want to use for the checksum. */
9066 static void
9067 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9069 dw_attr_ref a;
9070 unsigned ix;
9072 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9074 switch (a->dw_attr)
9076 case DW_AT_name:
9077 attrs->at_name = a;
9078 break;
9079 case DW_AT_type:
9080 attrs->at_type = a;
9081 break;
9082 case DW_AT_friend:
9083 attrs->at_friend = a;
9084 break;
9085 case DW_AT_accessibility:
9086 attrs->at_accessibility = a;
9087 break;
9088 case DW_AT_address_class:
9089 attrs->at_address_class = a;
9090 break;
9091 case DW_AT_allocated:
9092 attrs->at_allocated = a;
9093 break;
9094 case DW_AT_artificial:
9095 attrs->at_artificial = a;
9096 break;
9097 case DW_AT_associated:
9098 attrs->at_associated = a;
9099 break;
9100 case DW_AT_binary_scale:
9101 attrs->at_binary_scale = a;
9102 break;
9103 case DW_AT_bit_offset:
9104 attrs->at_bit_offset = a;
9105 break;
9106 case DW_AT_bit_size:
9107 attrs->at_bit_size = a;
9108 break;
9109 case DW_AT_bit_stride:
9110 attrs->at_bit_stride = a;
9111 break;
9112 case DW_AT_byte_size:
9113 attrs->at_byte_size = a;
9114 break;
9115 case DW_AT_byte_stride:
9116 attrs->at_byte_stride = a;
9117 break;
9118 case DW_AT_const_value:
9119 attrs->at_const_value = a;
9120 break;
9121 case DW_AT_containing_type:
9122 attrs->at_containing_type = a;
9123 break;
9124 case DW_AT_count:
9125 attrs->at_count = a;
9126 break;
9127 case DW_AT_data_location:
9128 attrs->at_data_location = a;
9129 break;
9130 case DW_AT_data_member_location:
9131 attrs->at_data_member_location = a;
9132 break;
9133 case DW_AT_decimal_scale:
9134 attrs->at_decimal_scale = a;
9135 break;
9136 case DW_AT_decimal_sign:
9137 attrs->at_decimal_sign = a;
9138 break;
9139 case DW_AT_default_value:
9140 attrs->at_default_value = a;
9141 break;
9142 case DW_AT_digit_count:
9143 attrs->at_digit_count = a;
9144 break;
9145 case DW_AT_discr:
9146 attrs->at_discr = a;
9147 break;
9148 case DW_AT_discr_list:
9149 attrs->at_discr_list = a;
9150 break;
9151 case DW_AT_discr_value:
9152 attrs->at_discr_value = a;
9153 break;
9154 case DW_AT_encoding:
9155 attrs->at_encoding = a;
9156 break;
9157 case DW_AT_endianity:
9158 attrs->at_endianity = a;
9159 break;
9160 case DW_AT_explicit:
9161 attrs->at_explicit = a;
9162 break;
9163 case DW_AT_is_optional:
9164 attrs->at_is_optional = a;
9165 break;
9166 case DW_AT_location:
9167 attrs->at_location = a;
9168 break;
9169 case DW_AT_lower_bound:
9170 attrs->at_lower_bound = a;
9171 break;
9172 case DW_AT_mutable:
9173 attrs->at_mutable = a;
9174 break;
9175 case DW_AT_ordering:
9176 attrs->at_ordering = a;
9177 break;
9178 case DW_AT_picture_string:
9179 attrs->at_picture_string = a;
9180 break;
9181 case DW_AT_prototyped:
9182 attrs->at_prototyped = a;
9183 break;
9184 case DW_AT_small:
9185 attrs->at_small = a;
9186 break;
9187 case DW_AT_segment:
9188 attrs->at_segment = a;
9189 break;
9190 case DW_AT_string_length:
9191 attrs->at_string_length = a;
9192 break;
9193 case DW_AT_threads_scaled:
9194 attrs->at_threads_scaled = a;
9195 break;
9196 case DW_AT_upper_bound:
9197 attrs->at_upper_bound = a;
9198 break;
9199 case DW_AT_use_location:
9200 attrs->at_use_location = a;
9201 break;
9202 case DW_AT_use_UTF8:
9203 attrs->at_use_UTF8 = a;
9204 break;
9205 case DW_AT_variable_parameter:
9206 attrs->at_variable_parameter = a;
9207 break;
9208 case DW_AT_virtuality:
9209 attrs->at_virtuality = a;
9210 break;
9211 case DW_AT_visibility:
9212 attrs->at_visibility = a;
9213 break;
9214 case DW_AT_vtable_elem_location:
9215 attrs->at_vtable_elem_location = a;
9216 break;
9217 default:
9218 break;
9223 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
9225 static void
9226 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9228 dw_die_ref c;
9229 dw_die_ref decl;
9230 struct checksum_attributes attrs;
9232 CHECKSUM_ULEB128 ('D');
9233 CHECKSUM_ULEB128 (die->die_tag);
9235 memset (&attrs, 0, sizeof (attrs));
9237 decl = get_AT_ref (die, DW_AT_specification);
9238 if (decl != NULL)
9239 collect_checksum_attributes (&attrs, decl);
9240 collect_checksum_attributes (&attrs, die);
9242 CHECKSUM_ATTR (attrs.at_name);
9243 CHECKSUM_ATTR (attrs.at_accessibility);
9244 CHECKSUM_ATTR (attrs.at_address_class);
9245 CHECKSUM_ATTR (attrs.at_allocated);
9246 CHECKSUM_ATTR (attrs.at_artificial);
9247 CHECKSUM_ATTR (attrs.at_associated);
9248 CHECKSUM_ATTR (attrs.at_binary_scale);
9249 CHECKSUM_ATTR (attrs.at_bit_offset);
9250 CHECKSUM_ATTR (attrs.at_bit_size);
9251 CHECKSUM_ATTR (attrs.at_bit_stride);
9252 CHECKSUM_ATTR (attrs.at_byte_size);
9253 CHECKSUM_ATTR (attrs.at_byte_stride);
9254 CHECKSUM_ATTR (attrs.at_const_value);
9255 CHECKSUM_ATTR (attrs.at_containing_type);
9256 CHECKSUM_ATTR (attrs.at_count);
9257 CHECKSUM_ATTR (attrs.at_data_location);
9258 CHECKSUM_ATTR (attrs.at_data_member_location);
9259 CHECKSUM_ATTR (attrs.at_decimal_scale);
9260 CHECKSUM_ATTR (attrs.at_decimal_sign);
9261 CHECKSUM_ATTR (attrs.at_default_value);
9262 CHECKSUM_ATTR (attrs.at_digit_count);
9263 CHECKSUM_ATTR (attrs.at_discr);
9264 CHECKSUM_ATTR (attrs.at_discr_list);
9265 CHECKSUM_ATTR (attrs.at_discr_value);
9266 CHECKSUM_ATTR (attrs.at_encoding);
9267 CHECKSUM_ATTR (attrs.at_endianity);
9268 CHECKSUM_ATTR (attrs.at_explicit);
9269 CHECKSUM_ATTR (attrs.at_is_optional);
9270 CHECKSUM_ATTR (attrs.at_location);
9271 CHECKSUM_ATTR (attrs.at_lower_bound);
9272 CHECKSUM_ATTR (attrs.at_mutable);
9273 CHECKSUM_ATTR (attrs.at_ordering);
9274 CHECKSUM_ATTR (attrs.at_picture_string);
9275 CHECKSUM_ATTR (attrs.at_prototyped);
9276 CHECKSUM_ATTR (attrs.at_small);
9277 CHECKSUM_ATTR (attrs.at_segment);
9278 CHECKSUM_ATTR (attrs.at_string_length);
9279 CHECKSUM_ATTR (attrs.at_threads_scaled);
9280 CHECKSUM_ATTR (attrs.at_upper_bound);
9281 CHECKSUM_ATTR (attrs.at_use_location);
9282 CHECKSUM_ATTR (attrs.at_use_UTF8);
9283 CHECKSUM_ATTR (attrs.at_variable_parameter);
9284 CHECKSUM_ATTR (attrs.at_virtuality);
9285 CHECKSUM_ATTR (attrs.at_visibility);
9286 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9287 CHECKSUM_ATTR (attrs.at_type);
9288 CHECKSUM_ATTR (attrs.at_friend);
9290 /* Checksum the child DIEs, except for nested types and member functions. */
9291 c = die->die_child;
9292 if (c) do {
9293 dw_attr_ref name_attr;
9295 c = c->die_sib;
9296 name_attr = get_AT (c, DW_AT_name);
9297 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9298 && name_attr != NULL)
9300 CHECKSUM_ULEB128 ('S');
9301 CHECKSUM_ULEB128 (c->die_tag);
9302 CHECKSUM_STRING (AT_string (name_attr));
9304 else
9306 /* Mark this DIE so it gets processed when unmarking. */
9307 if (c->die_mark == 0)
9308 c->die_mark = -1;
9309 die_checksum_ordered (c, ctx, mark);
9311 } while (c != die->die_child);
9313 CHECKSUM_ULEB128 (0);
9316 #undef CHECKSUM
9317 #undef CHECKSUM_STRING
9318 #undef CHECKSUM_ATTR
9319 #undef CHECKSUM_LEB128
9320 #undef CHECKSUM_ULEB128
9322 /* Generate the type signature for DIE. This is computed by generating an
9323 MD5 checksum over the DIE's tag, its relevant attributes, and its
9324 children. Attributes that are references to other DIEs are processed
9325 by recursion, using the MARK field to prevent infinite recursion.
9326 If the DIE is nested inside a namespace or another type, we also
9327 need to include that context in the signature. The lower 64 bits
9328 of the resulting MD5 checksum comprise the signature. */
9330 static void
9331 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9333 int mark;
9334 const char *name;
9335 unsigned char checksum[16];
9336 struct md5_ctx ctx;
9337 dw_die_ref decl;
9339 name = get_AT_string (die, DW_AT_name);
9340 decl = get_AT_ref (die, DW_AT_specification);
9342 /* First, compute a signature for just the type name (and its surrounding
9343 context, if any. This is stored in the type unit DIE for link-time
9344 ODR (one-definition rule) checking. */
9346 if (is_cxx() && name != NULL)
9348 md5_init_ctx (&ctx);
9350 /* Checksum the names of surrounding namespaces and structures. */
9351 if (decl != NULL && decl->die_parent != NULL)
9352 checksum_die_context (decl->die_parent, &ctx);
9354 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9355 md5_process_bytes (name, strlen (name) + 1, &ctx);
9356 md5_finish_ctx (&ctx, checksum);
9358 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9361 /* Next, compute the complete type signature. */
9363 md5_init_ctx (&ctx);
9364 mark = 1;
9365 die->die_mark = mark;
9367 /* Checksum the names of surrounding namespaces and structures. */
9368 if (decl != NULL && decl->die_parent != NULL)
9369 checksum_die_context (decl->die_parent, &ctx);
9371 /* Checksum the DIE and its children. */
9372 die_checksum_ordered (die, &ctx, &mark);
9373 unmark_all_dies (die);
9374 md5_finish_ctx (&ctx, checksum);
9376 /* Store the signature in the type node and link the type DIE and the
9377 type node together. */
9378 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9379 DWARF_TYPE_SIGNATURE_SIZE);
9380 die->die_id.die_type_node = type_node;
9381 type_node->type_die = die;
9383 /* If the DIE is a specification, link its declaration to the type node
9384 as well. */
9385 if (decl != NULL)
9386 decl->die_id.die_type_node = type_node;
9389 /* Do the location expressions look same? */
9390 static inline int
9391 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9393 return loc1->dw_loc_opc == loc2->dw_loc_opc
9394 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9395 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9398 /* Do the values look the same? */
9399 static int
9400 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9402 dw_loc_descr_ref loc1, loc2;
9403 rtx r1, r2;
9405 if (v1->val_class != v2->val_class)
9406 return 0;
9408 switch (v1->val_class)
9410 case dw_val_class_const:
9411 return v1->v.val_int == v2->v.val_int;
9412 case dw_val_class_unsigned_const:
9413 return v1->v.val_unsigned == v2->v.val_unsigned;
9414 case dw_val_class_const_double:
9415 return v1->v.val_double.high == v2->v.val_double.high
9416 && v1->v.val_double.low == v2->v.val_double.low;
9417 case dw_val_class_vec:
9418 if (v1->v.val_vec.length != v2->v.val_vec.length
9419 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9420 return 0;
9421 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9422 v1->v.val_vec.length * v1->v.val_vec.elt_size))
9423 return 0;
9424 return 1;
9425 case dw_val_class_flag:
9426 return v1->v.val_flag == v2->v.val_flag;
9427 case dw_val_class_str:
9428 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9430 case dw_val_class_addr:
9431 r1 = v1->v.val_addr;
9432 r2 = v2->v.val_addr;
9433 if (GET_CODE (r1) != GET_CODE (r2))
9434 return 0;
9435 return !rtx_equal_p (r1, r2);
9437 case dw_val_class_offset:
9438 return v1->v.val_offset == v2->v.val_offset;
9440 case dw_val_class_loc:
9441 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9442 loc1 && loc2;
9443 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9444 if (!same_loc_p (loc1, loc2, mark))
9445 return 0;
9446 return !loc1 && !loc2;
9448 case dw_val_class_die_ref:
9449 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9451 case dw_val_class_fde_ref:
9452 case dw_val_class_vms_delta:
9453 case dw_val_class_lbl_id:
9454 case dw_val_class_lineptr:
9455 case dw_val_class_macptr:
9456 return 1;
9458 case dw_val_class_file:
9459 return v1->v.val_file == v2->v.val_file;
9461 case dw_val_class_data8:
9462 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9464 default:
9465 return 1;
9469 /* Do the attributes look the same? */
9471 static int
9472 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9474 if (at1->dw_attr != at2->dw_attr)
9475 return 0;
9477 /* We don't care that this was compiled with a different compiler
9478 snapshot; if the output is the same, that's what matters. */
9479 if (at1->dw_attr == DW_AT_producer)
9480 return 1;
9482 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9485 /* Do the dies look the same? */
9487 static int
9488 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9490 dw_die_ref c1, c2;
9491 dw_attr_ref a1;
9492 unsigned ix;
9494 /* To avoid infinite recursion. */
9495 if (die1->die_mark)
9496 return die1->die_mark == die2->die_mark;
9497 die1->die_mark = die2->die_mark = ++(*mark);
9499 if (die1->die_tag != die2->die_tag)
9500 return 0;
9502 if (VEC_length (dw_attr_node, die1->die_attr)
9503 != VEC_length (dw_attr_node, die2->die_attr))
9504 return 0;
9506 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9507 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9508 return 0;
9510 c1 = die1->die_child;
9511 c2 = die2->die_child;
9512 if (! c1)
9514 if (c2)
9515 return 0;
9517 else
9518 for (;;)
9520 if (!same_die_p (c1, c2, mark))
9521 return 0;
9522 c1 = c1->die_sib;
9523 c2 = c2->die_sib;
9524 if (c1 == die1->die_child)
9526 if (c2 == die2->die_child)
9527 break;
9528 else
9529 return 0;
9533 return 1;
9536 /* Do the dies look the same? Wrapper around same_die_p. */
9538 static int
9539 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9541 int mark = 0;
9542 int ret = same_die_p (die1, die2, &mark);
9544 unmark_all_dies (die1);
9545 unmark_all_dies (die2);
9547 return ret;
9550 /* The prefix to attach to symbols on DIEs in the current comdat debug
9551 info section. */
9552 static char *comdat_symbol_id;
9554 /* The index of the current symbol within the current comdat CU. */
9555 static unsigned int comdat_symbol_number;
9557 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9558 children, and set comdat_symbol_id accordingly. */
9560 static void
9561 compute_section_prefix (dw_die_ref unit_die)
9563 const char *die_name = get_AT_string (unit_die, DW_AT_name);
9564 const char *base = die_name ? lbasename (die_name) : "anonymous";
9565 char *name = XALLOCAVEC (char, strlen (base) + 64);
9566 char *p;
9567 int i, mark;
9568 unsigned char checksum[16];
9569 struct md5_ctx ctx;
9571 /* Compute the checksum of the DIE, then append part of it as hex digits to
9572 the name filename of the unit. */
9574 md5_init_ctx (&ctx);
9575 mark = 0;
9576 die_checksum (unit_die, &ctx, &mark);
9577 unmark_all_dies (unit_die);
9578 md5_finish_ctx (&ctx, checksum);
9580 sprintf (name, "%s.", base);
9581 clean_symbol_name (name);
9583 p = name + strlen (name);
9584 for (i = 0; i < 4; i++)
9586 sprintf (p, "%.2x", checksum[i]);
9587 p += 2;
9590 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9591 comdat_symbol_number = 0;
9594 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
9596 static int
9597 is_type_die (dw_die_ref die)
9599 switch (die->die_tag)
9601 case DW_TAG_array_type:
9602 case DW_TAG_class_type:
9603 case DW_TAG_interface_type:
9604 case DW_TAG_enumeration_type:
9605 case DW_TAG_pointer_type:
9606 case DW_TAG_reference_type:
9607 case DW_TAG_rvalue_reference_type:
9608 case DW_TAG_string_type:
9609 case DW_TAG_structure_type:
9610 case DW_TAG_subroutine_type:
9611 case DW_TAG_union_type:
9612 case DW_TAG_ptr_to_member_type:
9613 case DW_TAG_set_type:
9614 case DW_TAG_subrange_type:
9615 case DW_TAG_base_type:
9616 case DW_TAG_const_type:
9617 case DW_TAG_file_type:
9618 case DW_TAG_packed_type:
9619 case DW_TAG_volatile_type:
9620 case DW_TAG_typedef:
9621 return 1;
9622 default:
9623 return 0;
9627 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9628 Basically, we want to choose the bits that are likely to be shared between
9629 compilations (types) and leave out the bits that are specific to individual
9630 compilations (functions). */
9632 static int
9633 is_comdat_die (dw_die_ref c)
9635 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9636 we do for stabs. The advantage is a greater likelihood of sharing between
9637 objects that don't include headers in the same order (and therefore would
9638 put the base types in a different comdat). jason 8/28/00 */
9640 if (c->die_tag == DW_TAG_base_type)
9641 return 0;
9643 if (c->die_tag == DW_TAG_pointer_type
9644 || c->die_tag == DW_TAG_reference_type
9645 || c->die_tag == DW_TAG_rvalue_reference_type
9646 || c->die_tag == DW_TAG_const_type
9647 || c->die_tag == DW_TAG_volatile_type)
9649 dw_die_ref t = get_AT_ref (c, DW_AT_type);
9651 return t ? is_comdat_die (t) : 0;
9654 return is_type_die (c);
9657 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9658 compilation unit. */
9660 static int
9661 is_symbol_die (dw_die_ref c)
9663 return (is_type_die (c)
9664 || is_declaration_die (c)
9665 || c->die_tag == DW_TAG_namespace
9666 || c->die_tag == DW_TAG_module);
9669 /* Returns true iff C is a compile-unit DIE. */
9671 static inline bool
9672 is_cu_die (dw_die_ref c)
9674 return c && c->die_tag == DW_TAG_compile_unit;
9677 static char *
9678 gen_internal_sym (const char *prefix)
9680 char buf[256];
9682 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9683 return xstrdup (buf);
9686 /* Assign symbols to all worthy DIEs under DIE. */
9688 static void
9689 assign_symbol_names (dw_die_ref die)
9691 dw_die_ref c;
9693 if (is_symbol_die (die))
9695 if (comdat_symbol_id)
9697 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9699 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9700 comdat_symbol_id, comdat_symbol_number++);
9701 die->die_id.die_symbol = xstrdup (p);
9703 else
9704 die->die_id.die_symbol = gen_internal_sym ("LDIE");
9707 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9710 struct cu_hash_table_entry
9712 dw_die_ref cu;
9713 unsigned min_comdat_num, max_comdat_num;
9714 struct cu_hash_table_entry *next;
9717 /* Routines to manipulate hash table of CUs. */
9718 static hashval_t
9719 htab_cu_hash (const void *of)
9721 const struct cu_hash_table_entry *const entry =
9722 (const struct cu_hash_table_entry *) of;
9724 return htab_hash_string (entry->cu->die_id.die_symbol);
9727 static int
9728 htab_cu_eq (const void *of1, const void *of2)
9730 const struct cu_hash_table_entry *const entry1 =
9731 (const struct cu_hash_table_entry *) of1;
9732 const struct die_struct *const entry2 = (const struct die_struct *) of2;
9734 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9737 static void
9738 htab_cu_del (void *what)
9740 struct cu_hash_table_entry *next,
9741 *entry = (struct cu_hash_table_entry *) what;
9743 while (entry)
9745 next = entry->next;
9746 free (entry);
9747 entry = next;
9751 /* Check whether we have already seen this CU and set up SYM_NUM
9752 accordingly. */
9753 static int
9754 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9756 struct cu_hash_table_entry dummy;
9757 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9759 dummy.max_comdat_num = 0;
9761 slot = (struct cu_hash_table_entry **)
9762 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9763 INSERT);
9764 entry = *slot;
9766 for (; entry; last = entry, entry = entry->next)
9768 if (same_die_p_wrap (cu, entry->cu))
9769 break;
9772 if (entry)
9774 *sym_num = entry->min_comdat_num;
9775 return 1;
9778 entry = XCNEW (struct cu_hash_table_entry);
9779 entry->cu = cu;
9780 entry->min_comdat_num = *sym_num = last->max_comdat_num;
9781 entry->next = *slot;
9782 *slot = entry;
9784 return 0;
9787 /* Record SYM_NUM to record of CU in HTABLE. */
9788 static void
9789 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9791 struct cu_hash_table_entry **slot, *entry;
9793 slot = (struct cu_hash_table_entry **)
9794 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9795 NO_INSERT);
9796 entry = *slot;
9798 entry->max_comdat_num = sym_num;
9801 /* Traverse the DIE (which is always comp_unit_die), and set up
9802 additional compilation units for each of the include files we see
9803 bracketed by BINCL/EINCL. */
9805 static void
9806 break_out_includes (dw_die_ref die)
9808 dw_die_ref c;
9809 dw_die_ref unit = NULL;
9810 limbo_die_node *node, **pnode;
9811 htab_t cu_hash_table;
9813 c = die->die_child;
9814 if (c) do {
9815 dw_die_ref prev = c;
9816 c = c->die_sib;
9817 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9818 || (unit && is_comdat_die (c)))
9820 dw_die_ref next = c->die_sib;
9822 /* This DIE is for a secondary CU; remove it from the main one. */
9823 remove_child_with_prev (c, prev);
9825 if (c->die_tag == DW_TAG_GNU_BINCL)
9826 unit = push_new_compile_unit (unit, c);
9827 else if (c->die_tag == DW_TAG_GNU_EINCL)
9828 unit = pop_compile_unit (unit);
9829 else
9830 add_child_die (unit, c);
9831 c = next;
9832 if (c == die->die_child)
9833 break;
9835 } while (c != die->die_child);
9837 #if 0
9838 /* We can only use this in debugging, since the frontend doesn't check
9839 to make sure that we leave every include file we enter. */
9840 gcc_assert (!unit);
9841 #endif
9843 assign_symbol_names (die);
9844 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9845 for (node = limbo_die_list, pnode = &limbo_die_list;
9846 node;
9847 node = node->next)
9849 int is_dupl;
9851 compute_section_prefix (node->die);
9852 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9853 &comdat_symbol_number);
9854 assign_symbol_names (node->die);
9855 if (is_dupl)
9856 *pnode = node->next;
9857 else
9859 pnode = &node->next;
9860 record_comdat_symbol_number (node->die, cu_hash_table,
9861 comdat_symbol_number);
9864 htab_delete (cu_hash_table);
9867 /* Return non-zero if this DIE is a declaration. */
9869 static int
9870 is_declaration_die (dw_die_ref die)
9872 dw_attr_ref a;
9873 unsigned ix;
9875 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9876 if (a->dw_attr == DW_AT_declaration)
9877 return 1;
9879 return 0;
9882 /* Return non-zero if this DIE is nested inside a subprogram. */
9884 static int
9885 is_nested_in_subprogram (dw_die_ref die)
9887 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9889 if (decl == NULL)
9890 decl = die;
9891 return local_scope_p (decl);
9894 /* Return non-zero if this is a type DIE that should be moved to a
9895 COMDAT .debug_types section. */
9897 static int
9898 should_move_die_to_comdat (dw_die_ref die)
9900 switch (die->die_tag)
9902 case DW_TAG_class_type:
9903 case DW_TAG_structure_type:
9904 case DW_TAG_enumeration_type:
9905 case DW_TAG_union_type:
9906 /* Don't move declarations, inlined instances, or types nested in a
9907 subprogram. */
9908 if (is_declaration_die (die)
9909 || get_AT (die, DW_AT_abstract_origin)
9910 || is_nested_in_subprogram (die))
9911 return 0;
9912 return 1;
9913 case DW_TAG_array_type:
9914 case DW_TAG_interface_type:
9915 case DW_TAG_pointer_type:
9916 case DW_TAG_reference_type:
9917 case DW_TAG_rvalue_reference_type:
9918 case DW_TAG_string_type:
9919 case DW_TAG_subroutine_type:
9920 case DW_TAG_ptr_to_member_type:
9921 case DW_TAG_set_type:
9922 case DW_TAG_subrange_type:
9923 case DW_TAG_base_type:
9924 case DW_TAG_const_type:
9925 case DW_TAG_file_type:
9926 case DW_TAG_packed_type:
9927 case DW_TAG_volatile_type:
9928 case DW_TAG_typedef:
9929 default:
9930 return 0;
9934 /* Make a clone of DIE. */
9936 static dw_die_ref
9937 clone_die (dw_die_ref die)
9939 dw_die_ref clone;
9940 dw_attr_ref a;
9941 unsigned ix;
9943 clone = ggc_alloc_cleared_die_node ();
9944 clone->die_tag = die->die_tag;
9946 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9947 add_dwarf_attr (clone, a);
9949 return clone;
9952 /* Make a clone of the tree rooted at DIE. */
9954 static dw_die_ref
9955 clone_tree (dw_die_ref die)
9957 dw_die_ref c;
9958 dw_die_ref clone = clone_die (die);
9960 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9962 return clone;
9965 /* Make a clone of DIE as a declaration. */
9967 static dw_die_ref
9968 clone_as_declaration (dw_die_ref die)
9970 dw_die_ref clone;
9971 dw_die_ref decl;
9972 dw_attr_ref a;
9973 unsigned ix;
9975 /* If the DIE is already a declaration, just clone it. */
9976 if (is_declaration_die (die))
9977 return clone_die (die);
9979 /* If the DIE is a specification, just clone its declaration DIE. */
9980 decl = get_AT_ref (die, DW_AT_specification);
9981 if (decl != NULL)
9982 return clone_die (decl);
9984 clone = ggc_alloc_cleared_die_node ();
9985 clone->die_tag = die->die_tag;
9987 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9989 /* We don't want to copy over all attributes.
9990 For example we don't want DW_AT_byte_size because otherwise we will no
9991 longer have a declaration and GDB will treat it as a definition. */
9993 switch (a->dw_attr)
9995 case DW_AT_artificial:
9996 case DW_AT_containing_type:
9997 case DW_AT_external:
9998 case DW_AT_name:
9999 case DW_AT_type:
10000 case DW_AT_virtuality:
10001 case DW_AT_linkage_name:
10002 case DW_AT_MIPS_linkage_name:
10003 add_dwarf_attr (clone, a);
10004 break;
10005 case DW_AT_byte_size:
10006 default:
10007 break;
10011 if (die->die_id.die_type_node)
10012 add_AT_die_ref (clone, DW_AT_signature, die);
10014 add_AT_flag (clone, DW_AT_declaration, 1);
10015 return clone;
10018 /* Copy the declaration context to the new compile unit DIE. This includes
10019 any surrounding namespace or type declarations. If the DIE has an
10020 AT_specification attribute, it also includes attributes and children
10021 attached to the specification. */
10023 static void
10024 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10026 dw_die_ref decl;
10027 dw_die_ref new_decl;
10029 decl = get_AT_ref (die, DW_AT_specification);
10030 if (decl == NULL)
10031 decl = die;
10032 else
10034 unsigned ix;
10035 dw_die_ref c;
10036 dw_attr_ref a;
10038 /* Copy the type node pointer from the new DIE to the original
10039 declaration DIE so we can forward references later. */
10040 decl->die_id.die_type_node = die->die_id.die_type_node;
10042 remove_AT (die, DW_AT_specification);
10044 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10046 if (a->dw_attr != DW_AT_name
10047 && a->dw_attr != DW_AT_declaration
10048 && a->dw_attr != DW_AT_external)
10049 add_dwarf_attr (die, a);
10052 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10055 if (decl->die_parent != NULL
10056 && decl->die_parent->die_tag != DW_TAG_compile_unit
10057 && decl->die_parent->die_tag != DW_TAG_type_unit)
10059 new_decl = copy_ancestor_tree (unit, decl, NULL);
10060 if (new_decl != NULL)
10062 remove_AT (new_decl, DW_AT_signature);
10063 add_AT_specification (die, new_decl);
10068 /* Generate the skeleton ancestor tree for the given NODE, then clone
10069 the DIE and add the clone into the tree. */
10071 static void
10072 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10074 if (node->new_die != NULL)
10075 return;
10077 node->new_die = clone_as_declaration (node->old_die);
10079 if (node->parent != NULL)
10081 generate_skeleton_ancestor_tree (node->parent);
10082 add_child_die (node->parent->new_die, node->new_die);
10086 /* Generate a skeleton tree of DIEs containing any declarations that are
10087 found in the original tree. We traverse the tree looking for declaration
10088 DIEs, and construct the skeleton from the bottom up whenever we find one. */
10090 static void
10091 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10093 skeleton_chain_node node;
10094 dw_die_ref c;
10095 dw_die_ref first;
10096 dw_die_ref prev = NULL;
10097 dw_die_ref next = NULL;
10099 node.parent = parent;
10101 first = c = parent->old_die->die_child;
10102 if (c)
10103 next = c->die_sib;
10104 if (c) do {
10105 if (prev == NULL || prev->die_sib == c)
10106 prev = c;
10107 c = next;
10108 next = (c == first ? NULL : c->die_sib);
10109 node.old_die = c;
10110 node.new_die = NULL;
10111 if (is_declaration_die (c))
10113 /* Clone the existing DIE, move the original to the skeleton
10114 tree (which is in the main CU), and put the clone, with
10115 all the original's children, where the original came from. */
10116 dw_die_ref clone = clone_die (c);
10117 move_all_children (c, clone);
10119 replace_child (c, clone, prev);
10120 generate_skeleton_ancestor_tree (parent);
10121 add_child_die (parent->new_die, c);
10122 node.new_die = c;
10123 c = clone;
10125 generate_skeleton_bottom_up (&node);
10126 } while (next != NULL);
10129 /* Wrapper function for generate_skeleton_bottom_up. */
10131 static dw_die_ref
10132 generate_skeleton (dw_die_ref die)
10134 skeleton_chain_node node;
10136 node.old_die = die;
10137 node.new_die = NULL;
10138 node.parent = NULL;
10140 /* If this type definition is nested inside another type,
10141 always leave at least a declaration in its place. */
10142 if (die->die_parent != NULL && is_type_die (die->die_parent))
10143 node.new_die = clone_as_declaration (die);
10145 generate_skeleton_bottom_up (&node);
10146 return node.new_die;
10149 /* Remove the DIE from its parent, possibly replacing it with a cloned
10150 declaration. The original DIE will be moved to a new compile unit
10151 so that existing references to it follow it to the new location. If
10152 any of the original DIE's descendants is a declaration, we need to
10153 replace the original DIE with a skeleton tree and move the
10154 declarations back into the skeleton tree. */
10156 static dw_die_ref
10157 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10159 dw_die_ref skeleton;
10161 skeleton = generate_skeleton (child);
10162 if (skeleton == NULL)
10163 remove_child_with_prev (child, prev);
10164 else
10166 skeleton->die_id.die_type_node = child->die_id.die_type_node;
10167 replace_child (child, skeleton, prev);
10170 return skeleton;
10173 /* Traverse the DIE and set up additional .debug_types sections for each
10174 type worthy of being placed in a COMDAT section. */
10176 static void
10177 break_out_comdat_types (dw_die_ref die)
10179 dw_die_ref c;
10180 dw_die_ref first;
10181 dw_die_ref prev = NULL;
10182 dw_die_ref next = NULL;
10183 dw_die_ref unit = NULL;
10185 first = c = die->die_child;
10186 if (c)
10187 next = c->die_sib;
10188 if (c) do {
10189 if (prev == NULL || prev->die_sib == c)
10190 prev = c;
10191 c = next;
10192 next = (c == first ? NULL : c->die_sib);
10193 if (should_move_die_to_comdat (c))
10195 dw_die_ref replacement;
10196 comdat_type_node_ref type_node;
10198 /* Create a new type unit DIE as the root for the new tree, and
10199 add it to the list of comdat types. */
10200 unit = new_die (DW_TAG_type_unit, NULL, NULL);
10201 add_AT_unsigned (unit, DW_AT_language,
10202 get_AT_unsigned (comp_unit_die (), DW_AT_language));
10203 type_node = ggc_alloc_cleared_comdat_type_node ();
10204 type_node->root_die = unit;
10205 type_node->next = comdat_type_list;
10206 comdat_type_list = type_node;
10208 /* Generate the type signature. */
10209 generate_type_signature (c, type_node);
10211 /* Copy the declaration context, attributes, and children of the
10212 declaration into the new compile unit DIE. */
10213 copy_declaration_context (unit, c);
10215 /* Remove this DIE from the main CU. */
10216 replacement = remove_child_or_replace_with_skeleton (c, prev);
10218 /* Break out nested types into their own type units. */
10219 break_out_comdat_types (c);
10221 /* Add the DIE to the new compunit. */
10222 add_child_die (unit, c);
10224 if (replacement != NULL)
10225 c = replacement;
10227 else if (c->die_tag == DW_TAG_namespace
10228 || c->die_tag == DW_TAG_class_type
10229 || c->die_tag == DW_TAG_structure_type
10230 || c->die_tag == DW_TAG_union_type)
10232 /* Look for nested types that can be broken out. */
10233 break_out_comdat_types (c);
10235 } while (next != NULL);
10238 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
10240 struct decl_table_entry
10242 dw_die_ref orig;
10243 dw_die_ref copy;
10246 /* Routines to manipulate hash table of copied declarations. */
10248 static hashval_t
10249 htab_decl_hash (const void *of)
10251 const struct decl_table_entry *const entry =
10252 (const struct decl_table_entry *) of;
10254 return htab_hash_pointer (entry->orig);
10257 static int
10258 htab_decl_eq (const void *of1, const void *of2)
10260 const struct decl_table_entry *const entry1 =
10261 (const struct decl_table_entry *) of1;
10262 const struct die_struct *const entry2 = (const struct die_struct *) of2;
10264 return entry1->orig == entry2;
10267 static void
10268 htab_decl_del (void *what)
10270 struct decl_table_entry *entry = (struct decl_table_entry *) what;
10272 free (entry);
10275 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10276 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
10277 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
10278 to check if the ancestor has already been copied into UNIT. */
10280 static dw_die_ref
10281 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10283 dw_die_ref parent = die->die_parent;
10284 dw_die_ref new_parent = unit;
10285 dw_die_ref copy;
10286 void **slot = NULL;
10287 struct decl_table_entry *entry = NULL;
10289 if (decl_table)
10291 /* Check if the entry has already been copied to UNIT. */
10292 slot = htab_find_slot_with_hash (decl_table, die,
10293 htab_hash_pointer (die), INSERT);
10294 if (*slot != HTAB_EMPTY_ENTRY)
10296 entry = (struct decl_table_entry *) *slot;
10297 return entry->copy;
10300 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
10301 entry = XCNEW (struct decl_table_entry);
10302 entry->orig = die;
10303 entry->copy = NULL;
10304 *slot = entry;
10307 if (parent != NULL)
10309 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10310 if (spec != NULL)
10311 parent = spec;
10312 if (parent->die_tag != DW_TAG_compile_unit
10313 && parent->die_tag != DW_TAG_type_unit)
10314 new_parent = copy_ancestor_tree (unit, parent, decl_table);
10317 copy = clone_as_declaration (die);
10318 add_child_die (new_parent, copy);
10320 if (decl_table != NULL)
10322 /* Record the pointer to the copy. */
10323 entry->copy = copy;
10326 return copy;
10329 /* Walk the DIE and its children, looking for references to incomplete
10330 or trivial types that are unmarked (i.e., that are not in the current
10331 type_unit). */
10333 static void
10334 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10336 dw_die_ref c;
10337 dw_attr_ref a;
10338 unsigned ix;
10340 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10342 if (AT_class (a) == dw_val_class_die_ref)
10344 dw_die_ref targ = AT_ref (a);
10345 comdat_type_node_ref type_node = targ->die_id.die_type_node;
10346 void **slot;
10347 struct decl_table_entry *entry;
10349 if (targ->die_mark != 0 || type_node != NULL)
10350 continue;
10352 slot = htab_find_slot_with_hash (decl_table, targ,
10353 htab_hash_pointer (targ), INSERT);
10355 if (*slot != HTAB_EMPTY_ENTRY)
10357 /* TARG has already been copied, so we just need to
10358 modify the reference to point to the copy. */
10359 entry = (struct decl_table_entry *) *slot;
10360 a->dw_attr_val.v.val_die_ref.die = entry->copy;
10362 else
10364 dw_die_ref parent = unit;
10365 dw_die_ref copy = clone_tree (targ);
10367 /* Make sure the cloned tree is marked as part of the
10368 type unit. */
10369 mark_dies (copy);
10371 /* Record in DECL_TABLE that TARG has been copied.
10372 Need to do this now, before the recursive call,
10373 because DECL_TABLE may be expanded and SLOT
10374 would no longer be a valid pointer. */
10375 entry = XCNEW (struct decl_table_entry);
10376 entry->orig = targ;
10377 entry->copy = copy;
10378 *slot = entry;
10380 /* If TARG has surrounding context, copy its ancestor tree
10381 into the new type unit. */
10382 if (targ->die_parent != NULL
10383 && targ->die_parent->die_tag != DW_TAG_compile_unit
10384 && targ->die_parent->die_tag != DW_TAG_type_unit)
10385 parent = copy_ancestor_tree (unit, targ->die_parent,
10386 decl_table);
10388 add_child_die (parent, copy);
10389 a->dw_attr_val.v.val_die_ref.die = copy;
10391 /* Make sure the newly-copied DIE is walked. If it was
10392 installed in a previously-added context, it won't
10393 get visited otherwise. */
10394 if (parent != unit)
10396 /* Find the highest point of the newly-added tree,
10397 mark each node along the way, and walk from there. */
10398 parent->die_mark = 1;
10399 while (parent->die_parent
10400 && parent->die_parent->die_mark == 0)
10402 parent = parent->die_parent;
10403 parent->die_mark = 1;
10405 copy_decls_walk (unit, parent, decl_table);
10411 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10414 /* Copy declarations for "unworthy" types into the new comdat section.
10415 Incomplete types, modified types, and certain other types aren't broken
10416 out into comdat sections of their own, so they don't have a signature,
10417 and we need to copy the declaration into the same section so that we
10418 don't have an external reference. */
10420 static void
10421 copy_decls_for_unworthy_types (dw_die_ref unit)
10423 htab_t decl_table;
10425 mark_dies (unit);
10426 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10427 copy_decls_walk (unit, unit, decl_table);
10428 htab_delete (decl_table);
10429 unmark_dies (unit);
10432 /* Traverse the DIE and add a sibling attribute if it may have the
10433 effect of speeding up access to siblings. To save some space,
10434 avoid generating sibling attributes for DIE's without children. */
10436 static void
10437 add_sibling_attributes (dw_die_ref die)
10439 dw_die_ref c;
10441 if (! die->die_child)
10442 return;
10444 if (die->die_parent && die != die->die_parent->die_child)
10445 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10447 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10450 /* Output all location lists for the DIE and its children. */
10452 static void
10453 output_location_lists (dw_die_ref die)
10455 dw_die_ref c;
10456 dw_attr_ref a;
10457 unsigned ix;
10459 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10460 if (AT_class (a) == dw_val_class_loc_list)
10461 output_loc_list (AT_loc_list (a));
10463 FOR_EACH_CHILD (die, c, output_location_lists (c));
10466 /* The format of each DIE (and its attribute value pairs) is encoded in an
10467 abbreviation table. This routine builds the abbreviation table and assigns
10468 a unique abbreviation id for each abbreviation entry. The children of each
10469 die are visited recursively. */
10471 static void
10472 build_abbrev_table (dw_die_ref die)
10474 unsigned long abbrev_id;
10475 unsigned int n_alloc;
10476 dw_die_ref c;
10477 dw_attr_ref a;
10478 unsigned ix;
10480 /* Scan the DIE references, and mark as external any that refer to
10481 DIEs from other CUs (i.e. those which are not marked). */
10482 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10483 if (AT_class (a) == dw_val_class_die_ref
10484 && AT_ref (a)->die_mark == 0)
10486 gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10487 set_AT_ref_external (a, 1);
10490 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10492 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10493 dw_attr_ref die_a, abbrev_a;
10494 unsigned ix;
10495 bool ok = true;
10497 if (abbrev->die_tag != die->die_tag)
10498 continue;
10499 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10500 continue;
10502 if (VEC_length (dw_attr_node, abbrev->die_attr)
10503 != VEC_length (dw_attr_node, die->die_attr))
10504 continue;
10506 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10508 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10509 if ((abbrev_a->dw_attr != die_a->dw_attr)
10510 || (value_format (abbrev_a) != value_format (die_a)))
10512 ok = false;
10513 break;
10516 if (ok)
10517 break;
10520 if (abbrev_id >= abbrev_die_table_in_use)
10522 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10524 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10525 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10526 n_alloc);
10528 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10529 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10530 abbrev_die_table_allocated = n_alloc;
10533 ++abbrev_die_table_in_use;
10534 abbrev_die_table[abbrev_id] = die;
10537 die->die_abbrev = abbrev_id;
10538 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10541 /* Return the power-of-two number of bytes necessary to represent VALUE. */
10543 static int
10544 constant_size (unsigned HOST_WIDE_INT value)
10546 int log;
10548 if (value == 0)
10549 log = 0;
10550 else
10551 log = floor_log2 (value);
10553 log = log / 8;
10554 log = 1 << (floor_log2 (log) + 1);
10556 return log;
10559 /* Return the size of a DIE as it is represented in the
10560 .debug_info section. */
10562 static unsigned long
10563 size_of_die (dw_die_ref die)
10565 unsigned long size = 0;
10566 dw_attr_ref a;
10567 unsigned ix;
10569 size += size_of_uleb128 (die->die_abbrev);
10570 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10572 switch (AT_class (a))
10574 case dw_val_class_addr:
10575 size += DWARF2_ADDR_SIZE;
10576 break;
10577 case dw_val_class_offset:
10578 size += DWARF_OFFSET_SIZE;
10579 break;
10580 case dw_val_class_loc:
10582 unsigned long lsize = size_of_locs (AT_loc (a));
10584 /* Block length. */
10585 if (dwarf_version >= 4)
10586 size += size_of_uleb128 (lsize);
10587 else
10588 size += constant_size (lsize);
10589 size += lsize;
10591 break;
10592 case dw_val_class_loc_list:
10593 size += DWARF_OFFSET_SIZE;
10594 break;
10595 case dw_val_class_range_list:
10596 size += DWARF_OFFSET_SIZE;
10597 break;
10598 case dw_val_class_const:
10599 size += size_of_sleb128 (AT_int (a));
10600 break;
10601 case dw_val_class_unsigned_const:
10602 size += constant_size (AT_unsigned (a));
10603 break;
10604 case dw_val_class_const_double:
10605 size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10606 if (HOST_BITS_PER_WIDE_INT >= 64)
10607 size++; /* block */
10608 break;
10609 case dw_val_class_vec:
10610 size += constant_size (a->dw_attr_val.v.val_vec.length
10611 * a->dw_attr_val.v.val_vec.elt_size)
10612 + a->dw_attr_val.v.val_vec.length
10613 * a->dw_attr_val.v.val_vec.elt_size; /* block */
10614 break;
10615 case dw_val_class_flag:
10616 if (dwarf_version >= 4)
10617 /* Currently all add_AT_flag calls pass in 1 as last argument,
10618 so DW_FORM_flag_present can be used. If that ever changes,
10619 we'll need to use DW_FORM_flag and have some optimization
10620 in build_abbrev_table that will change those to
10621 DW_FORM_flag_present if it is set to 1 in all DIEs using
10622 the same abbrev entry. */
10623 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10624 else
10625 size += 1;
10626 break;
10627 case dw_val_class_die_ref:
10628 if (AT_ref_external (a))
10630 /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10631 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
10632 is sized by target address length, whereas in DWARF3
10633 it's always sized as an offset. */
10634 if (dwarf_version >= 4)
10635 size += DWARF_TYPE_SIGNATURE_SIZE;
10636 else if (dwarf_version == 2)
10637 size += DWARF2_ADDR_SIZE;
10638 else
10639 size += DWARF_OFFSET_SIZE;
10641 else
10642 size += DWARF_OFFSET_SIZE;
10643 break;
10644 case dw_val_class_fde_ref:
10645 size += DWARF_OFFSET_SIZE;
10646 break;
10647 case dw_val_class_lbl_id:
10648 size += DWARF2_ADDR_SIZE;
10649 break;
10650 case dw_val_class_lineptr:
10651 case dw_val_class_macptr:
10652 size += DWARF_OFFSET_SIZE;
10653 break;
10654 case dw_val_class_str:
10655 if (AT_string_form (a) == DW_FORM_strp)
10656 size += DWARF_OFFSET_SIZE;
10657 else
10658 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10659 break;
10660 case dw_val_class_file:
10661 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10662 break;
10663 case dw_val_class_data8:
10664 size += 8;
10665 break;
10666 case dw_val_class_vms_delta:
10667 size += DWARF_OFFSET_SIZE;
10668 break;
10669 default:
10670 gcc_unreachable ();
10674 return size;
10677 /* Size the debugging information associated with a given DIE. Visits the
10678 DIE's children recursively. Updates the global variable next_die_offset, on
10679 each time through. Uses the current value of next_die_offset to update the
10680 die_offset field in each DIE. */
10682 static void
10683 calc_die_sizes (dw_die_ref die)
10685 dw_die_ref c;
10687 die->die_offset = next_die_offset;
10688 next_die_offset += size_of_die (die);
10690 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10692 if (die->die_child != NULL)
10693 /* Count the null byte used to terminate sibling lists. */
10694 next_die_offset += 1;
10697 /* Set the marks for a die and its children. We do this so
10698 that we know whether or not a reference needs to use FORM_ref_addr; only
10699 DIEs in the same CU will be marked. We used to clear out the offset
10700 and use that as the flag, but ran into ordering problems. */
10702 static void
10703 mark_dies (dw_die_ref die)
10705 dw_die_ref c;
10707 gcc_assert (!die->die_mark);
10709 die->die_mark = 1;
10710 FOR_EACH_CHILD (die, c, mark_dies (c));
10713 /* Clear the marks for a die and its children. */
10715 static void
10716 unmark_dies (dw_die_ref die)
10718 dw_die_ref c;
10720 if (dwarf_version < 4)
10721 gcc_assert (die->die_mark);
10723 die->die_mark = 0;
10724 FOR_EACH_CHILD (die, c, unmark_dies (c));
10727 /* Clear the marks for a die, its children and referred dies. */
10729 static void
10730 unmark_all_dies (dw_die_ref die)
10732 dw_die_ref c;
10733 dw_attr_ref a;
10734 unsigned ix;
10736 if (!die->die_mark)
10737 return;
10738 die->die_mark = 0;
10740 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10742 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10743 if (AT_class (a) == dw_val_class_die_ref)
10744 unmark_all_dies (AT_ref (a));
10747 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10748 generated for the compilation unit. */
10750 static unsigned long
10751 size_of_pubnames (VEC (pubname_entry, gc) * names)
10753 unsigned long size;
10754 unsigned i;
10755 pubname_ref p;
10757 size = DWARF_PUBNAMES_HEADER_SIZE;
10758 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10759 if (names != pubtype_table
10760 || p->die->die_offset != 0
10761 || !flag_eliminate_unused_debug_types)
10762 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10764 size += DWARF_OFFSET_SIZE;
10765 return size;
10768 /* Return the size of the information in the .debug_aranges section. */
10770 static unsigned long
10771 size_of_aranges (void)
10773 unsigned long size;
10775 size = DWARF_ARANGES_HEADER_SIZE;
10777 /* Count the address/length pair for this compilation unit. */
10778 if (text_section_used)
10779 size += 2 * DWARF2_ADDR_SIZE;
10780 if (cold_text_section_used)
10781 size += 2 * DWARF2_ADDR_SIZE;
10782 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10784 /* Count the two zero words used to terminated the address range table. */
10785 size += 2 * DWARF2_ADDR_SIZE;
10786 return size;
10789 /* Select the encoding of an attribute value. */
10791 static enum dwarf_form
10792 value_format (dw_attr_ref a)
10794 switch (a->dw_attr_val.val_class)
10796 case dw_val_class_addr:
10797 /* Only very few attributes allow DW_FORM_addr. */
10798 switch (a->dw_attr)
10800 case DW_AT_low_pc:
10801 case DW_AT_high_pc:
10802 case DW_AT_entry_pc:
10803 case DW_AT_trampoline:
10804 return DW_FORM_addr;
10805 default:
10806 break;
10808 switch (DWARF2_ADDR_SIZE)
10810 case 1:
10811 return DW_FORM_data1;
10812 case 2:
10813 return DW_FORM_data2;
10814 case 4:
10815 return DW_FORM_data4;
10816 case 8:
10817 return DW_FORM_data8;
10818 default:
10819 gcc_unreachable ();
10821 case dw_val_class_range_list:
10822 case dw_val_class_loc_list:
10823 if (dwarf_version >= 4)
10824 return DW_FORM_sec_offset;
10825 /* FALLTHRU */
10826 case dw_val_class_vms_delta:
10827 case dw_val_class_offset:
10828 switch (DWARF_OFFSET_SIZE)
10830 case 4:
10831 return DW_FORM_data4;
10832 case 8:
10833 return DW_FORM_data8;
10834 default:
10835 gcc_unreachable ();
10837 case dw_val_class_loc:
10838 if (dwarf_version >= 4)
10839 return DW_FORM_exprloc;
10840 switch (constant_size (size_of_locs (AT_loc (a))))
10842 case 1:
10843 return DW_FORM_block1;
10844 case 2:
10845 return DW_FORM_block2;
10846 default:
10847 gcc_unreachable ();
10849 case dw_val_class_const:
10850 return DW_FORM_sdata;
10851 case dw_val_class_unsigned_const:
10852 switch (constant_size (AT_unsigned (a)))
10854 case 1:
10855 return DW_FORM_data1;
10856 case 2:
10857 return DW_FORM_data2;
10858 case 4:
10859 return DW_FORM_data4;
10860 case 8:
10861 return DW_FORM_data8;
10862 default:
10863 gcc_unreachable ();
10865 case dw_val_class_const_double:
10866 switch (HOST_BITS_PER_WIDE_INT)
10868 case 8:
10869 return DW_FORM_data2;
10870 case 16:
10871 return DW_FORM_data4;
10872 case 32:
10873 return DW_FORM_data8;
10874 case 64:
10875 default:
10876 return DW_FORM_block1;
10878 case dw_val_class_vec:
10879 switch (constant_size (a->dw_attr_val.v.val_vec.length
10880 * a->dw_attr_val.v.val_vec.elt_size))
10882 case 1:
10883 return DW_FORM_block1;
10884 case 2:
10885 return DW_FORM_block2;
10886 case 4:
10887 return DW_FORM_block4;
10888 default:
10889 gcc_unreachable ();
10891 case dw_val_class_flag:
10892 if (dwarf_version >= 4)
10894 /* Currently all add_AT_flag calls pass in 1 as last argument,
10895 so DW_FORM_flag_present can be used. If that ever changes,
10896 we'll need to use DW_FORM_flag and have some optimization
10897 in build_abbrev_table that will change those to
10898 DW_FORM_flag_present if it is set to 1 in all DIEs using
10899 the same abbrev entry. */
10900 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10901 return DW_FORM_flag_present;
10903 return DW_FORM_flag;
10904 case dw_val_class_die_ref:
10905 if (AT_ref_external (a))
10906 return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10907 else
10908 return DW_FORM_ref;
10909 case dw_val_class_fde_ref:
10910 return DW_FORM_data;
10911 case dw_val_class_lbl_id:
10912 return DW_FORM_addr;
10913 case dw_val_class_lineptr:
10914 case dw_val_class_macptr:
10915 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10916 case dw_val_class_str:
10917 return AT_string_form (a);
10918 case dw_val_class_file:
10919 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10921 case 1:
10922 return DW_FORM_data1;
10923 case 2:
10924 return DW_FORM_data2;
10925 case 4:
10926 return DW_FORM_data4;
10927 default:
10928 gcc_unreachable ();
10931 case dw_val_class_data8:
10932 return DW_FORM_data8;
10934 default:
10935 gcc_unreachable ();
10939 /* Output the encoding of an attribute value. */
10941 static void
10942 output_value_format (dw_attr_ref a)
10944 enum dwarf_form form = value_format (a);
10946 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10949 /* Output the .debug_abbrev section which defines the DIE abbreviation
10950 table. */
10952 static void
10953 output_abbrev_section (void)
10955 unsigned long abbrev_id;
10957 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10959 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10960 unsigned ix;
10961 dw_attr_ref a_attr;
10963 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10964 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10965 dwarf_tag_name (abbrev->die_tag));
10967 if (abbrev->die_child != NULL)
10968 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10969 else
10970 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10972 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10973 ix++)
10975 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10976 dwarf_attr_name (a_attr->dw_attr));
10977 output_value_format (a_attr);
10980 dw2_asm_output_data (1, 0, NULL);
10981 dw2_asm_output_data (1, 0, NULL);
10984 /* Terminate the table. */
10985 dw2_asm_output_data (1, 0, NULL);
10988 /* Output a symbol we can use to refer to this DIE from another CU. */
10990 static inline void
10991 output_die_symbol (dw_die_ref die)
10993 char *sym = die->die_id.die_symbol;
10995 if (sym == 0)
10996 return;
10998 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10999 /* We make these global, not weak; if the target doesn't support
11000 .linkonce, it doesn't support combining the sections, so debugging
11001 will break. */
11002 targetm.asm_out.globalize_label (asm_out_file, sym);
11004 ASM_OUTPUT_LABEL (asm_out_file, sym);
11007 /* Return a new location list, given the begin and end range, and the
11008 expression. */
11010 static inline dw_loc_list_ref
11011 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11012 const char *section)
11014 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11016 retlist->begin = begin;
11017 retlist->end = end;
11018 retlist->expr = expr;
11019 retlist->section = section;
11021 return retlist;
11024 /* Generate a new internal symbol for this location list node, if it
11025 hasn't got one yet. */
11027 static inline void
11028 gen_llsym (dw_loc_list_ref list)
11030 gcc_assert (!list->ll_symbol);
11031 list->ll_symbol = gen_internal_sym ("LLST");
11034 /* Output the location list given to us. */
11036 static void
11037 output_loc_list (dw_loc_list_ref list_head)
11039 dw_loc_list_ref curr = list_head;
11041 if (list_head->emitted)
11042 return;
11043 list_head->emitted = true;
11045 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11047 /* Walk the location list, and output each range + expression. */
11048 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11050 unsigned long size;
11051 /* Don't output an entry that starts and ends at the same address. */
11052 if (strcmp (curr->begin, curr->end) == 0)
11053 continue;
11054 if (!have_multiple_function_sections)
11056 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11057 "Location list begin address (%s)",
11058 list_head->ll_symbol);
11059 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11060 "Location list end address (%s)",
11061 list_head->ll_symbol);
11063 else
11065 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11066 "Location list begin address (%s)",
11067 list_head->ll_symbol);
11068 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11069 "Location list end address (%s)",
11070 list_head->ll_symbol);
11072 size = size_of_locs (curr->expr);
11074 /* Output the block length for this list of location operations. */
11075 gcc_assert (size <= 0xffff);
11076 dw2_asm_output_data (2, size, "%s", "Location expression size");
11078 output_loc_sequence (curr->expr);
11081 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11082 "Location list terminator begin (%s)",
11083 list_head->ll_symbol);
11084 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11085 "Location list terminator end (%s)",
11086 list_head->ll_symbol);
11089 /* Output a type signature. */
11091 static inline void
11092 output_signature (const char *sig, const char *name)
11094 int i;
11096 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11097 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11100 /* Output the DIE and its attributes. Called recursively to generate
11101 the definitions of each child DIE. */
11103 static void
11104 output_die (dw_die_ref die)
11106 dw_attr_ref a;
11107 dw_die_ref c;
11108 unsigned long size;
11109 unsigned ix;
11111 /* If someone in another CU might refer to us, set up a symbol for
11112 them to point to. */
11113 if (dwarf_version < 4 && die->die_id.die_symbol)
11114 output_die_symbol (die);
11116 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11117 (unsigned long)die->die_offset,
11118 dwarf_tag_name (die->die_tag));
11120 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11122 const char *name = dwarf_attr_name (a->dw_attr);
11124 switch (AT_class (a))
11126 case dw_val_class_addr:
11127 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11128 break;
11130 case dw_val_class_offset:
11131 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11132 "%s", name);
11133 break;
11135 case dw_val_class_range_list:
11137 char *p = strchr (ranges_section_label, '\0');
11139 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11140 a->dw_attr_val.v.val_offset);
11141 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11142 debug_ranges_section, "%s", name);
11143 *p = '\0';
11145 break;
11147 case dw_val_class_loc:
11148 size = size_of_locs (AT_loc (a));
11150 /* Output the block length for this list of location operations. */
11151 if (dwarf_version >= 4)
11152 dw2_asm_output_data_uleb128 (size, "%s", name);
11153 else
11154 dw2_asm_output_data (constant_size (size), size, "%s", name);
11156 output_loc_sequence (AT_loc (a));
11157 break;
11159 case dw_val_class_const:
11160 /* ??? It would be slightly more efficient to use a scheme like is
11161 used for unsigned constants below, but gdb 4.x does not sign
11162 extend. Gdb 5.x does sign extend. */
11163 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11164 break;
11166 case dw_val_class_unsigned_const:
11167 dw2_asm_output_data (constant_size (AT_unsigned (a)),
11168 AT_unsigned (a), "%s", name);
11169 break;
11171 case dw_val_class_const_double:
11173 unsigned HOST_WIDE_INT first, second;
11175 if (HOST_BITS_PER_WIDE_INT >= 64)
11176 dw2_asm_output_data (1,
11177 2 * HOST_BITS_PER_WIDE_INT
11178 / HOST_BITS_PER_CHAR,
11179 NULL);
11181 if (WORDS_BIG_ENDIAN)
11183 first = a->dw_attr_val.v.val_double.high;
11184 second = a->dw_attr_val.v.val_double.low;
11186 else
11188 first = a->dw_attr_val.v.val_double.low;
11189 second = a->dw_attr_val.v.val_double.high;
11192 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11193 first, name);
11194 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11195 second, NULL);
11197 break;
11199 case dw_val_class_vec:
11201 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11202 unsigned int len = a->dw_attr_val.v.val_vec.length;
11203 unsigned int i;
11204 unsigned char *p;
11206 dw2_asm_output_data (constant_size (len * elt_size),
11207 len * elt_size, "%s", name);
11208 if (elt_size > sizeof (HOST_WIDE_INT))
11210 elt_size /= 2;
11211 len *= 2;
11213 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11214 i < len;
11215 i++, p += elt_size)
11216 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11217 "fp or vector constant word %u", i);
11218 break;
11221 case dw_val_class_flag:
11222 if (dwarf_version >= 4)
11224 /* Currently all add_AT_flag calls pass in 1 as last argument,
11225 so DW_FORM_flag_present can be used. If that ever changes,
11226 we'll need to use DW_FORM_flag and have some optimization
11227 in build_abbrev_table that will change those to
11228 DW_FORM_flag_present if it is set to 1 in all DIEs using
11229 the same abbrev entry. */
11230 gcc_assert (AT_flag (a) == 1);
11231 if (flag_debug_asm)
11232 fprintf (asm_out_file, "\t\t\t%s %s\n",
11233 ASM_COMMENT_START, name);
11234 break;
11236 dw2_asm_output_data (1, AT_flag (a), "%s", name);
11237 break;
11239 case dw_val_class_loc_list:
11241 char *sym = AT_loc_list (a)->ll_symbol;
11243 gcc_assert (sym);
11244 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11245 "%s", name);
11247 break;
11249 case dw_val_class_die_ref:
11250 if (AT_ref_external (a))
11252 if (dwarf_version >= 4)
11254 comdat_type_node_ref type_node =
11255 AT_ref (a)->die_id.die_type_node;
11257 gcc_assert (type_node);
11258 output_signature (type_node->signature, name);
11260 else
11262 char *sym = AT_ref (a)->die_id.die_symbol;
11263 int size;
11265 gcc_assert (sym);
11266 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11267 length, whereas in DWARF3 it's always sized as an
11268 offset. */
11269 if (dwarf_version == 2)
11270 size = DWARF2_ADDR_SIZE;
11271 else
11272 size = DWARF_OFFSET_SIZE;
11273 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11274 name);
11277 else
11279 gcc_assert (AT_ref (a)->die_offset);
11280 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11281 "%s", name);
11283 break;
11285 case dw_val_class_fde_ref:
11287 char l1[20];
11289 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11290 a->dw_attr_val.v.val_fde_index * 2);
11291 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11292 "%s", name);
11294 break;
11296 case dw_val_class_vms_delta:
11297 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11298 AT_vms_delta2 (a), AT_vms_delta1 (a),
11299 "%s", name);
11300 break;
11302 case dw_val_class_lbl_id:
11303 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11304 break;
11306 case dw_val_class_lineptr:
11307 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11308 debug_line_section, "%s", name);
11309 break;
11311 case dw_val_class_macptr:
11312 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11313 debug_macinfo_section, "%s", name);
11314 break;
11316 case dw_val_class_str:
11317 if (AT_string_form (a) == DW_FORM_strp)
11318 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11319 a->dw_attr_val.v.val_str->label,
11320 debug_str_section,
11321 "%s: \"%s\"", name, AT_string (a));
11322 else
11323 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11324 break;
11326 case dw_val_class_file:
11328 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11330 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11331 a->dw_attr_val.v.val_file->filename);
11332 break;
11335 case dw_val_class_data8:
11337 int i;
11339 for (i = 0; i < 8; i++)
11340 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11341 i == 0 ? "%s" : NULL, name);
11342 break;
11345 default:
11346 gcc_unreachable ();
11350 FOR_EACH_CHILD (die, c, output_die (c));
11352 /* Add null byte to terminate sibling list. */
11353 if (die->die_child != NULL)
11354 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11355 (unsigned long) die->die_offset);
11358 /* Output the compilation unit that appears at the beginning of the
11359 .debug_info section, and precedes the DIE descriptions. */
11361 static void
11362 output_compilation_unit_header (void)
11364 int ver = dwarf_version;
11366 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11367 dw2_asm_output_data (4, 0xffffffff,
11368 "Initial length escape value indicating 64-bit DWARF extension");
11369 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11370 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11371 "Length of Compilation Unit Info");
11372 dw2_asm_output_data (2, ver, "DWARF version number");
11373 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11374 debug_abbrev_section,
11375 "Offset Into Abbrev. Section");
11376 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11379 /* Output the compilation unit DIE and its children. */
11381 static void
11382 output_comp_unit (dw_die_ref die, int output_if_empty)
11384 const char *secname;
11385 char *oldsym, *tmp;
11387 /* Unless we are outputting main CU, we may throw away empty ones. */
11388 if (!output_if_empty && die->die_child == NULL)
11389 return;
11391 /* Even if there are no children of this DIE, we must output the information
11392 about the compilation unit. Otherwise, on an empty translation unit, we
11393 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11394 will then complain when examining the file. First mark all the DIEs in
11395 this CU so we know which get local refs. */
11396 mark_dies (die);
11398 build_abbrev_table (die);
11400 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11401 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11402 calc_die_sizes (die);
11404 oldsym = die->die_id.die_symbol;
11405 if (oldsym)
11407 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11409 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11410 secname = tmp;
11411 die->die_id.die_symbol = NULL;
11412 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11414 else
11416 switch_to_section (debug_info_section);
11417 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11418 info_section_emitted = true;
11421 /* Output debugging information. */
11422 output_compilation_unit_header ();
11423 output_die (die);
11425 /* Leave the marks on the main CU, so we can check them in
11426 output_pubnames. */
11427 if (oldsym)
11429 unmark_dies (die);
11430 die->die_id.die_symbol = oldsym;
11434 /* Output a comdat type unit DIE and its children. */
11436 static void
11437 output_comdat_type_unit (comdat_type_node *node)
11439 const char *secname;
11440 char *tmp;
11441 int i;
11442 #if defined (OBJECT_FORMAT_ELF)
11443 tree comdat_key;
11444 #endif
11446 /* First mark all the DIEs in this CU so we know which get local refs. */
11447 mark_dies (node->root_die);
11449 build_abbrev_table (node->root_die);
11451 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11452 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11453 calc_die_sizes (node->root_die);
11455 #if defined (OBJECT_FORMAT_ELF)
11456 secname = ".debug_types";
11457 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11458 sprintf (tmp, "wt.");
11459 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11460 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11461 comdat_key = get_identifier (tmp);
11462 targetm.asm_out.named_section (secname,
11463 SECTION_DEBUG | SECTION_LINKONCE,
11464 comdat_key);
11465 #else
11466 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11467 sprintf (tmp, ".gnu.linkonce.wt.");
11468 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11469 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11470 secname = tmp;
11471 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11472 #endif
11474 /* Output debugging information. */
11475 output_compilation_unit_header ();
11476 output_signature (node->signature, "Type Signature");
11477 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11478 "Offset to Type DIE");
11479 output_die (node->root_die);
11481 unmark_dies (node->root_die);
11484 /* Return the DWARF2/3 pubname associated with a decl. */
11486 static const char *
11487 dwarf2_name (tree decl, int scope)
11489 if (DECL_NAMELESS (decl))
11490 return NULL;
11491 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11494 /* Add a new entry to .debug_pubnames if appropriate. */
11496 static void
11497 add_pubname_string (const char *str, dw_die_ref die)
11499 if (targetm.want_debug_pub_sections)
11501 pubname_entry e;
11503 e.die = die;
11504 e.name = xstrdup (str);
11505 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11509 static void
11510 add_pubname (tree decl, dw_die_ref die)
11512 if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11514 const char *name = dwarf2_name (decl, 1);
11515 if (name)
11516 add_pubname_string (name, die);
11520 /* Add a new entry to .debug_pubtypes if appropriate. */
11522 static void
11523 add_pubtype (tree decl, dw_die_ref die)
11525 pubname_entry e;
11527 if (!targetm.want_debug_pub_sections)
11528 return;
11530 e.name = NULL;
11531 if ((TREE_PUBLIC (decl)
11532 || is_cu_die (die->die_parent))
11533 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11535 e.die = die;
11536 if (TYPE_P (decl))
11538 if (TYPE_NAME (decl))
11540 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11541 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11542 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11543 && DECL_NAME (TYPE_NAME (decl)))
11544 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11545 else
11546 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11549 else
11551 e.name = dwarf2_name (decl, 1);
11552 if (e.name)
11553 e.name = xstrdup (e.name);
11556 /* If we don't have a name for the type, there's no point in adding
11557 it to the table. */
11558 if (e.name && e.name[0] != '\0')
11559 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11563 /* Output the public names table used to speed up access to externally
11564 visible names; or the public types table used to find type definitions. */
11566 static void
11567 output_pubnames (VEC (pubname_entry, gc) * names)
11569 unsigned i;
11570 unsigned long pubnames_length = size_of_pubnames (names);
11571 pubname_ref pub;
11573 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11574 dw2_asm_output_data (4, 0xffffffff,
11575 "Initial length escape value indicating 64-bit DWARF extension");
11576 if (names == pubname_table)
11577 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11578 "Length of Public Names Info");
11579 else
11580 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11581 "Length of Public Type Names Info");
11582 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
11583 dw2_asm_output_data (2, 2, "DWARF Version");
11584 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11585 debug_info_section,
11586 "Offset of Compilation Unit Info");
11587 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11588 "Compilation Unit Length");
11590 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11592 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11593 if (names == pubname_table)
11594 gcc_assert (pub->die->die_mark);
11596 if (names != pubtype_table
11597 || pub->die->die_offset != 0
11598 || !flag_eliminate_unused_debug_types)
11600 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11601 "DIE offset");
11603 dw2_asm_output_nstring (pub->name, -1, "external name");
11607 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11610 /* Add a new entry to .debug_aranges if appropriate. */
11612 static void
11613 add_arange (tree decl, dw_die_ref die)
11615 if (! DECL_SECTION_NAME (decl))
11616 return;
11618 if (arange_table_in_use == arange_table_allocated)
11620 arange_table_allocated += ARANGE_TABLE_INCREMENT;
11621 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11622 arange_table_allocated);
11623 memset (arange_table + arange_table_in_use, 0,
11624 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11627 arange_table[arange_table_in_use++] = die;
11630 /* Output the information that goes into the .debug_aranges table.
11631 Namely, define the beginning and ending address range of the
11632 text section generated for this compilation unit. */
11634 static void
11635 output_aranges (void)
11637 unsigned i;
11638 unsigned long aranges_length = size_of_aranges ();
11640 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11641 dw2_asm_output_data (4, 0xffffffff,
11642 "Initial length escape value indicating 64-bit DWARF extension");
11643 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11644 "Length of Address Ranges Info");
11645 /* Version number for aranges is still 2, even in DWARF3. */
11646 dw2_asm_output_data (2, 2, "DWARF Version");
11647 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11648 debug_info_section,
11649 "Offset of Compilation Unit Info");
11650 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11651 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11653 /* We need to align to twice the pointer size here. */
11654 if (DWARF_ARANGES_PAD_SIZE)
11656 /* Pad using a 2 byte words so that padding is correct for any
11657 pointer size. */
11658 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11659 2 * DWARF2_ADDR_SIZE);
11660 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11661 dw2_asm_output_data (2, 0, NULL);
11664 /* It is necessary not to output these entries if the sections were
11665 not used; if the sections were not used, the length will be 0 and
11666 the address may end up as 0 if the section is discarded by ld
11667 --gc-sections, leaving an invalid (0, 0) entry that can be
11668 confused with the terminator. */
11669 if (text_section_used)
11671 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11672 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11673 text_section_label, "Length");
11675 if (cold_text_section_used)
11677 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11678 "Address");
11679 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11680 cold_text_section_label, "Length");
11683 for (i = 0; i < arange_table_in_use; i++)
11685 dw_die_ref die = arange_table[i];
11687 /* We shouldn't see aranges for DIEs outside of the main CU. */
11688 gcc_assert (die->die_mark);
11690 if (die->die_tag == DW_TAG_subprogram)
11692 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11693 "Address");
11694 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11695 get_AT_low_pc (die), "Length");
11697 else
11699 /* A static variable; extract the symbol from DW_AT_location.
11700 Note that this code isn't currently hit, as we only emit
11701 aranges for functions (jason 9/23/99). */
11702 dw_attr_ref a = get_AT (die, DW_AT_location);
11703 dw_loc_descr_ref loc;
11705 gcc_assert (a && AT_class (a) == dw_val_class_loc);
11707 loc = AT_loc (a);
11708 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11710 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11711 loc->dw_loc_oprnd1.v.val_addr, "Address");
11712 dw2_asm_output_data (DWARF2_ADDR_SIZE,
11713 get_AT_unsigned (die, DW_AT_byte_size),
11714 "Length");
11718 /* Output the terminator words. */
11719 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11720 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11723 /* Add a new entry to .debug_ranges. Return the offset at which it
11724 was placed. */
11726 static unsigned int
11727 add_ranges_num (int num)
11729 unsigned int in_use = ranges_table_in_use;
11731 if (in_use == ranges_table_allocated)
11733 ranges_table_allocated += RANGES_TABLE_INCREMENT;
11734 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11735 ranges_table_allocated);
11736 memset (ranges_table + ranges_table_in_use, 0,
11737 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11740 ranges_table[in_use].num = num;
11741 ranges_table_in_use = in_use + 1;
11743 return in_use * 2 * DWARF2_ADDR_SIZE;
11746 /* Add a new entry to .debug_ranges corresponding to a block, or a
11747 range terminator if BLOCK is NULL. */
11749 static unsigned int
11750 add_ranges (const_tree block)
11752 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11755 /* Add a new entry to .debug_ranges corresponding to a pair of
11756 labels. */
11758 static void
11759 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11760 bool *added)
11762 unsigned int in_use = ranges_by_label_in_use;
11763 unsigned int offset;
11765 if (in_use == ranges_by_label_allocated)
11767 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11768 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11769 ranges_by_label,
11770 ranges_by_label_allocated);
11771 memset (ranges_by_label + ranges_by_label_in_use, 0,
11772 RANGES_TABLE_INCREMENT
11773 * sizeof (struct dw_ranges_by_label_struct));
11776 ranges_by_label[in_use].begin = begin;
11777 ranges_by_label[in_use].end = end;
11778 ranges_by_label_in_use = in_use + 1;
11780 offset = add_ranges_num (-(int)in_use - 1);
11781 if (!*added)
11783 add_AT_range_list (die, DW_AT_ranges, offset);
11784 *added = true;
11788 static void
11789 output_ranges (void)
11791 unsigned i;
11792 static const char *const start_fmt = "Offset %#x";
11793 const char *fmt = start_fmt;
11795 for (i = 0; i < ranges_table_in_use; i++)
11797 int block_num = ranges_table[i].num;
11799 if (block_num > 0)
11801 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11802 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11804 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11805 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11807 /* If all code is in the text section, then the compilation
11808 unit base address defaults to DW_AT_low_pc, which is the
11809 base of the text section. */
11810 if (!have_multiple_function_sections)
11812 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11813 text_section_label,
11814 fmt, i * 2 * DWARF2_ADDR_SIZE);
11815 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11816 text_section_label, NULL);
11819 /* Otherwise, the compilation unit base address is zero,
11820 which allows us to use absolute addresses, and not worry
11821 about whether the target supports cross-section
11822 arithmetic. */
11823 else
11825 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11826 fmt, i * 2 * DWARF2_ADDR_SIZE);
11827 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11830 fmt = NULL;
11833 /* Negative block_num stands for an index into ranges_by_label. */
11834 else if (block_num < 0)
11836 int lab_idx = - block_num - 1;
11838 if (!have_multiple_function_sections)
11840 gcc_unreachable ();
11841 #if 0
11842 /* If we ever use add_ranges_by_labels () for a single
11843 function section, all we have to do is to take out
11844 the #if 0 above. */
11845 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11846 ranges_by_label[lab_idx].begin,
11847 text_section_label,
11848 fmt, i * 2 * DWARF2_ADDR_SIZE);
11849 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11850 ranges_by_label[lab_idx].end,
11851 text_section_label, NULL);
11852 #endif
11854 else
11856 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11857 ranges_by_label[lab_idx].begin,
11858 fmt, i * 2 * DWARF2_ADDR_SIZE);
11859 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11860 ranges_by_label[lab_idx].end,
11861 NULL);
11864 else
11866 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11867 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11868 fmt = start_fmt;
11873 /* Data structure containing information about input files. */
11874 struct file_info
11876 const char *path; /* Complete file name. */
11877 const char *fname; /* File name part. */
11878 int length; /* Length of entire string. */
11879 struct dwarf_file_data * file_idx; /* Index in input file table. */
11880 int dir_idx; /* Index in directory table. */
11883 /* Data structure containing information about directories with source
11884 files. */
11885 struct dir_info
11887 const char *path; /* Path including directory name. */
11888 int length; /* Path length. */
11889 int prefix; /* Index of directory entry which is a prefix. */
11890 int count; /* Number of files in this directory. */
11891 int dir_idx; /* Index of directory used as base. */
11894 /* Callback function for file_info comparison. We sort by looking at
11895 the directories in the path. */
11897 static int
11898 file_info_cmp (const void *p1, const void *p2)
11900 const struct file_info *const s1 = (const struct file_info *) p1;
11901 const struct file_info *const s2 = (const struct file_info *) p2;
11902 const unsigned char *cp1;
11903 const unsigned char *cp2;
11905 /* Take care of file names without directories. We need to make sure that
11906 we return consistent values to qsort since some will get confused if
11907 we return the same value when identical operands are passed in opposite
11908 orders. So if neither has a directory, return 0 and otherwise return
11909 1 or -1 depending on which one has the directory. */
11910 if ((s1->path == s1->fname || s2->path == s2->fname))
11911 return (s2->path == s2->fname) - (s1->path == s1->fname);
11913 cp1 = (const unsigned char *) s1->path;
11914 cp2 = (const unsigned char *) s2->path;
11916 while (1)
11918 ++cp1;
11919 ++cp2;
11920 /* Reached the end of the first path? If so, handle like above. */
11921 if ((cp1 == (const unsigned char *) s1->fname)
11922 || (cp2 == (const unsigned char *) s2->fname))
11923 return ((cp2 == (const unsigned char *) s2->fname)
11924 - (cp1 == (const unsigned char *) s1->fname));
11926 /* Character of current path component the same? */
11927 else if (*cp1 != *cp2)
11928 return *cp1 - *cp2;
11932 struct file_name_acquire_data
11934 struct file_info *files;
11935 int used_files;
11936 int max_files;
11939 /* Traversal function for the hash table. */
11941 static int
11942 file_name_acquire (void ** slot, void *data)
11944 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11945 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11946 struct file_info *fi;
11947 const char *f;
11949 gcc_assert (fnad->max_files >= d->emitted_number);
11951 if (! d->emitted_number)
11952 return 1;
11954 gcc_assert (fnad->max_files != fnad->used_files);
11956 fi = fnad->files + fnad->used_files++;
11958 /* Skip all leading "./". */
11959 f = d->filename;
11960 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11961 f += 2;
11963 /* Create a new array entry. */
11964 fi->path = f;
11965 fi->length = strlen (f);
11966 fi->file_idx = d;
11968 /* Search for the file name part. */
11969 f = strrchr (f, DIR_SEPARATOR);
11970 #if defined (DIR_SEPARATOR_2)
11972 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11974 if (g != NULL)
11976 if (f == NULL || f < g)
11977 f = g;
11980 #endif
11982 fi->fname = f == NULL ? fi->path : f + 1;
11983 return 1;
11986 /* Output the directory table and the file name table. We try to minimize
11987 the total amount of memory needed. A heuristic is used to avoid large
11988 slowdowns with many input files. */
11990 static void
11991 output_file_names (void)
11993 struct file_name_acquire_data fnad;
11994 int numfiles;
11995 struct file_info *files;
11996 struct dir_info *dirs;
11997 int *saved;
11998 int *savehere;
11999 int *backmap;
12000 int ndirs;
12001 int idx_offset;
12002 int i;
12004 if (!last_emitted_file)
12006 dw2_asm_output_data (1, 0, "End directory table");
12007 dw2_asm_output_data (1, 0, "End file name table");
12008 return;
12011 numfiles = last_emitted_file->emitted_number;
12013 /* Allocate the various arrays we need. */
12014 files = XALLOCAVEC (struct file_info, numfiles);
12015 dirs = XALLOCAVEC (struct dir_info, numfiles);
12017 fnad.files = files;
12018 fnad.used_files = 0;
12019 fnad.max_files = numfiles;
12020 htab_traverse (file_table, file_name_acquire, &fnad);
12021 gcc_assert (fnad.used_files == fnad.max_files);
12023 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12025 /* Find all the different directories used. */
12026 dirs[0].path = files[0].path;
12027 dirs[0].length = files[0].fname - files[0].path;
12028 dirs[0].prefix = -1;
12029 dirs[0].count = 1;
12030 dirs[0].dir_idx = 0;
12031 files[0].dir_idx = 0;
12032 ndirs = 1;
12034 for (i = 1; i < numfiles; i++)
12035 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12036 && memcmp (dirs[ndirs - 1].path, files[i].path,
12037 dirs[ndirs - 1].length) == 0)
12039 /* Same directory as last entry. */
12040 files[i].dir_idx = ndirs - 1;
12041 ++dirs[ndirs - 1].count;
12043 else
12045 int j;
12047 /* This is a new directory. */
12048 dirs[ndirs].path = files[i].path;
12049 dirs[ndirs].length = files[i].fname - files[i].path;
12050 dirs[ndirs].count = 1;
12051 dirs[ndirs].dir_idx = ndirs;
12052 files[i].dir_idx = ndirs;
12054 /* Search for a prefix. */
12055 dirs[ndirs].prefix = -1;
12056 for (j = 0; j < ndirs; j++)
12057 if (dirs[j].length < dirs[ndirs].length
12058 && dirs[j].length > 1
12059 && (dirs[ndirs].prefix == -1
12060 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12061 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12062 dirs[ndirs].prefix = j;
12064 ++ndirs;
12067 /* Now to the actual work. We have to find a subset of the directories which
12068 allow expressing the file name using references to the directory table
12069 with the least amount of characters. We do not do an exhaustive search
12070 where we would have to check out every combination of every single
12071 possible prefix. Instead we use a heuristic which provides nearly optimal
12072 results in most cases and never is much off. */
12073 saved = XALLOCAVEC (int, ndirs);
12074 savehere = XALLOCAVEC (int, ndirs);
12076 memset (saved, '\0', ndirs * sizeof (saved[0]));
12077 for (i = 0; i < ndirs; i++)
12079 int j;
12080 int total;
12082 /* We can always save some space for the current directory. But this
12083 does not mean it will be enough to justify adding the directory. */
12084 savehere[i] = dirs[i].length;
12085 total = (savehere[i] - saved[i]) * dirs[i].count;
12087 for (j = i + 1; j < ndirs; j++)
12089 savehere[j] = 0;
12090 if (saved[j] < dirs[i].length)
12092 /* Determine whether the dirs[i] path is a prefix of the
12093 dirs[j] path. */
12094 int k;
12096 k = dirs[j].prefix;
12097 while (k != -1 && k != (int) i)
12098 k = dirs[k].prefix;
12100 if (k == (int) i)
12102 /* Yes it is. We can possibly save some memory by
12103 writing the filenames in dirs[j] relative to
12104 dirs[i]. */
12105 savehere[j] = dirs[i].length;
12106 total += (savehere[j] - saved[j]) * dirs[j].count;
12111 /* Check whether we can save enough to justify adding the dirs[i]
12112 directory. */
12113 if (total > dirs[i].length + 1)
12115 /* It's worthwhile adding. */
12116 for (j = i; j < ndirs; j++)
12117 if (savehere[j] > 0)
12119 /* Remember how much we saved for this directory so far. */
12120 saved[j] = savehere[j];
12122 /* Remember the prefix directory. */
12123 dirs[j].dir_idx = i;
12128 /* Emit the directory name table. */
12129 idx_offset = dirs[0].length > 0 ? 1 : 0;
12130 for (i = 1 - idx_offset; i < ndirs; i++)
12131 dw2_asm_output_nstring (dirs[i].path,
12132 dirs[i].length
12133 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12134 "Directory Entry: %#x", i + idx_offset);
12136 dw2_asm_output_data (1, 0, "End directory table");
12138 /* We have to emit them in the order of emitted_number since that's
12139 used in the debug info generation. To do this efficiently we
12140 generate a back-mapping of the indices first. */
12141 backmap = XALLOCAVEC (int, numfiles);
12142 for (i = 0; i < numfiles; i++)
12143 backmap[files[i].file_idx->emitted_number - 1] = i;
12145 /* Now write all the file names. */
12146 for (i = 0; i < numfiles; i++)
12148 int file_idx = backmap[i];
12149 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12151 #ifdef VMS_DEBUGGING_INFO
12152 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12154 /* Setting these fields can lead to debugger miscomparisons,
12155 but VMS Debug requires them to be set correctly. */
12157 int ver;
12158 long long cdt;
12159 long siz;
12160 int maxfilelen = strlen (files[file_idx].path)
12161 + dirs[dir_idx].length
12162 + MAX_VMS_VERSION_LEN + 1;
12163 char *filebuf = XALLOCAVEC (char, maxfilelen);
12165 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12166 snprintf (filebuf, maxfilelen, "%s;%d",
12167 files[file_idx].path + dirs[dir_idx].length, ver);
12169 dw2_asm_output_nstring
12170 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12172 /* Include directory index. */
12173 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12175 /* Modification time. */
12176 dw2_asm_output_data_uleb128
12177 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12178 ? cdt : 0,
12179 NULL);
12181 /* File length in bytes. */
12182 dw2_asm_output_data_uleb128
12183 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12184 ? siz : 0,
12185 NULL);
12186 #else
12187 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12188 "File Entry: %#x", (unsigned) i + 1);
12190 /* Include directory index. */
12191 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12193 /* Modification time. */
12194 dw2_asm_output_data_uleb128 (0, NULL);
12196 /* File length in bytes. */
12197 dw2_asm_output_data_uleb128 (0, NULL);
12198 #endif /* VMS_DEBUGGING_INFO */
12201 dw2_asm_output_data (1, 0, "End file name table");
12205 /* Output the source line number correspondence information. This
12206 information goes into the .debug_line section. */
12208 static void
12209 output_line_info (void)
12211 char l1[20], l2[20], p1[20], p2[20];
12212 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12213 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12214 unsigned opc;
12215 unsigned n_op_args;
12216 unsigned long lt_index;
12217 unsigned long current_line;
12218 long line_offset;
12219 long line_delta;
12220 unsigned long current_file;
12221 unsigned long function;
12222 int ver = dwarf_version;
12224 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12225 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12226 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12227 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12229 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12230 dw2_asm_output_data (4, 0xffffffff,
12231 "Initial length escape value indicating 64-bit DWARF extension");
12232 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12233 "Length of Source Line Info");
12234 ASM_OUTPUT_LABEL (asm_out_file, l1);
12236 dw2_asm_output_data (2, ver, "DWARF Version");
12237 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12238 ASM_OUTPUT_LABEL (asm_out_file, p1);
12240 /* Define the architecture-dependent minimum instruction length (in
12241 bytes). In this implementation of DWARF, this field is used for
12242 information purposes only. Since GCC generates assembly language,
12243 we have no a priori knowledge of how many instruction bytes are
12244 generated for each source line, and therefore can use only the
12245 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12246 commands. Accordingly, we fix this as `1', which is "correct
12247 enough" for all architectures, and don't let the target override. */
12248 dw2_asm_output_data (1, 1,
12249 "Minimum Instruction Length");
12251 if (ver >= 4)
12252 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12253 "Maximum Operations Per Instruction");
12254 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12255 "Default is_stmt_start flag");
12256 dw2_asm_output_data (1, DWARF_LINE_BASE,
12257 "Line Base Value (Special Opcodes)");
12258 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12259 "Line Range Value (Special Opcodes)");
12260 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12261 "Special Opcode Base");
12263 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12265 switch (opc)
12267 case DW_LNS_advance_pc:
12268 case DW_LNS_advance_line:
12269 case DW_LNS_set_file:
12270 case DW_LNS_set_column:
12271 case DW_LNS_fixed_advance_pc:
12272 n_op_args = 1;
12273 break;
12274 default:
12275 n_op_args = 0;
12276 break;
12279 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12280 opc, n_op_args);
12283 /* Write out the information about the files we use. */
12284 output_file_names ();
12285 ASM_OUTPUT_LABEL (asm_out_file, p2);
12287 /* We used to set the address register to the first location in the text
12288 section here, but that didn't accomplish anything since we already
12289 have a line note for the opening brace of the first function. */
12291 /* Generate the line number to PC correspondence table, encoded as
12292 a series of state machine operations. */
12293 current_file = 1;
12294 current_line = 1;
12296 if (cfun && in_cold_section_p)
12297 strcpy (prev_line_label, crtl->subsections.cold_section_label);
12298 else
12299 strcpy (prev_line_label, text_section_label);
12300 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12302 dw_line_info_ref line_info = &line_info_table[lt_index];
12304 #if 0
12305 /* Disable this optimization for now; GDB wants to see two line notes
12306 at the beginning of a function so it can find the end of the
12307 prologue. */
12309 /* Don't emit anything for redundant notes. Just updating the
12310 address doesn't accomplish anything, because we already assume
12311 that anything after the last address is this line. */
12312 if (line_info->dw_line_num == current_line
12313 && line_info->dw_file_num == current_file)
12314 continue;
12315 #endif
12317 /* Emit debug info for the address of the current line.
12319 Unfortunately, we have little choice here currently, and must always
12320 use the most general form. GCC does not know the address delta
12321 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
12322 attributes which will give an upper bound on the address range. We
12323 could perhaps use length attributes to determine when it is safe to
12324 use DW_LNS_fixed_advance_pc. */
12326 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12327 if (0)
12329 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
12330 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12331 "DW_LNS_fixed_advance_pc");
12332 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12334 else
12336 /* This can handle any delta. This takes
12337 4+DWARF2_ADDR_SIZE bytes. */
12338 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12339 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12340 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12341 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12344 strcpy (prev_line_label, line_label);
12346 /* Emit debug info for the source file of the current line, if
12347 different from the previous line. */
12348 if (line_info->dw_file_num != current_file)
12350 current_file = line_info->dw_file_num;
12351 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12352 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12355 /* Emit debug info for the current line number, choosing the encoding
12356 that uses the least amount of space. */
12357 if (line_info->dw_line_num != current_line)
12359 line_offset = line_info->dw_line_num - current_line;
12360 line_delta = line_offset - DWARF_LINE_BASE;
12361 current_line = line_info->dw_line_num;
12362 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12363 /* This can handle deltas from -10 to 234, using the current
12364 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
12365 takes 1 byte. */
12366 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12367 "line %lu", current_line);
12368 else
12370 /* This can handle any delta. This takes at least 4 bytes,
12371 depending on the value being encoded. */
12372 dw2_asm_output_data (1, DW_LNS_advance_line,
12373 "advance to line %lu", current_line);
12374 dw2_asm_output_data_sleb128 (line_offset, NULL);
12375 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12378 else
12379 /* We still need to start a new row, so output a copy insn. */
12380 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12383 /* Emit debug info for the address of the end of the function. */
12384 if (0)
12386 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12387 "DW_LNS_fixed_advance_pc");
12388 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12390 else
12392 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12393 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12394 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12395 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12398 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12399 dw2_asm_output_data_uleb128 (1, NULL);
12400 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12402 function = 0;
12403 current_file = 1;
12404 current_line = 1;
12405 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12407 dw_separate_line_info_ref line_info
12408 = &separate_line_info_table[lt_index];
12410 #if 0
12411 /* Don't emit anything for redundant notes. */
12412 if (line_info->dw_line_num == current_line
12413 && line_info->dw_file_num == current_file
12414 && line_info->function == function)
12415 goto cont;
12416 #endif
12418 /* Emit debug info for the address of the current line. If this is
12419 a new function, or the first line of a function, then we need
12420 to handle it differently. */
12421 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12422 lt_index);
12423 if (function != line_info->function)
12425 function = line_info->function;
12427 /* Set the address register to the first line in the function. */
12428 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12429 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12430 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12431 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12433 else
12435 /* ??? See the DW_LNS_advance_pc comment above. */
12436 if (0)
12438 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12439 "DW_LNS_fixed_advance_pc");
12440 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12442 else
12444 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12445 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12446 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12447 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12451 strcpy (prev_line_label, line_label);
12453 /* Emit debug info for the source file of the current line, if
12454 different from the previous line. */
12455 if (line_info->dw_file_num != current_file)
12457 current_file = line_info->dw_file_num;
12458 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12459 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12462 /* Emit debug info for the current line number, choosing the encoding
12463 that uses the least amount of space. */
12464 if (line_info->dw_line_num != current_line)
12466 line_offset = line_info->dw_line_num - current_line;
12467 line_delta = line_offset - DWARF_LINE_BASE;
12468 current_line = line_info->dw_line_num;
12469 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12470 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12471 "line %lu", current_line);
12472 else
12474 dw2_asm_output_data (1, DW_LNS_advance_line,
12475 "advance to line %lu", current_line);
12476 dw2_asm_output_data_sleb128 (line_offset, NULL);
12477 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12480 else
12481 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12483 #if 0
12484 cont:
12485 #endif
12487 lt_index++;
12489 /* If we're done with a function, end its sequence. */
12490 if (lt_index == separate_line_info_table_in_use
12491 || separate_line_info_table[lt_index].function != function)
12493 current_file = 1;
12494 current_line = 1;
12496 /* Emit debug info for the address of the end of the function. */
12497 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12498 if (0)
12500 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12501 "DW_LNS_fixed_advance_pc");
12502 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12504 else
12506 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12507 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12508 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12509 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12512 /* Output the marker for the end of this sequence. */
12513 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12514 dw2_asm_output_data_uleb128 (1, NULL);
12515 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12519 /* Output the marker for the end of the line number info. */
12520 ASM_OUTPUT_LABEL (asm_out_file, l2);
12523 /* Return the size of the .debug_dcall table for the compilation unit. */
12525 static unsigned long
12526 size_of_dcall_table (void)
12528 unsigned long size;
12529 unsigned int i;
12530 dcall_entry *p;
12531 tree last_poc_decl = NULL;
12533 /* Header: version + debug info section pointer + pointer size. */
12534 size = 2 + DWARF_OFFSET_SIZE + 1;
12536 /* Each entry: code label + DIE offset. */
12537 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12539 gcc_assert (p->targ_die != NULL);
12540 /* Insert a "from" entry when the point-of-call DIE offset changes. */
12541 if (p->poc_decl != last_poc_decl)
12543 dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12544 gcc_assert (poc_die);
12545 last_poc_decl = p->poc_decl;
12546 if (poc_die)
12547 size += (DWARF_OFFSET_SIZE
12548 + size_of_uleb128 (poc_die->die_offset));
12550 size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12553 return size;
12556 /* Output the direct call table used to disambiguate PC values when
12557 identical function have been merged. */
12559 static void
12560 output_dcall_table (void)
12562 unsigned i;
12563 unsigned long dcall_length = size_of_dcall_table ();
12564 dcall_entry *p;
12565 char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12566 tree last_poc_decl = NULL;
12568 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12569 dw2_asm_output_data (4, 0xffffffff,
12570 "Initial length escape value indicating 64-bit DWARF extension");
12571 dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12572 "Length of Direct Call Table");
12573 dw2_asm_output_data (2, 4, "Version number");
12574 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12575 debug_info_section,
12576 "Offset of Compilation Unit Info");
12577 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12579 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12581 /* Insert a "from" entry when the point-of-call DIE offset changes. */
12582 if (p->poc_decl != last_poc_decl)
12584 dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12585 last_poc_decl = p->poc_decl;
12586 if (poc_die)
12588 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12589 dw2_asm_output_data_uleb128 (poc_die->die_offset,
12590 "Caller DIE offset");
12593 ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12594 dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12595 dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12596 "Callee DIE offset");
12600 /* Return the size of the .debug_vcall table for the compilation unit. */
12602 static unsigned long
12603 size_of_vcall_table (void)
12605 unsigned long size;
12606 unsigned int i;
12607 vcall_entry *p;
12609 /* Header: version + pointer size. */
12610 size = 2 + 1;
12612 /* Each entry: code label + vtable slot index. */
12613 FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12614 size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12616 return size;
12619 /* Output the virtual call table used to disambiguate PC values when
12620 identical function have been merged. */
12622 static void
12623 output_vcall_table (void)
12625 unsigned i;
12626 unsigned long vcall_length = size_of_vcall_table ();
12627 vcall_entry *p;
12628 char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12630 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12631 dw2_asm_output_data (4, 0xffffffff,
12632 "Initial length escape value indicating 64-bit DWARF extension");
12633 dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12634 "Length of Virtual Call Table");
12635 dw2_asm_output_data (2, 4, "Version number");
12636 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12638 FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12640 ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12641 dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12642 dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12646 /* Given a pointer to a tree node for some base type, return a pointer to
12647 a DIE that describes the given type.
12649 This routine must only be called for GCC type nodes that correspond to
12650 Dwarf base (fundamental) types. */
12652 static dw_die_ref
12653 base_type_die (tree type)
12655 dw_die_ref base_type_result;
12656 enum dwarf_type encoding;
12658 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12659 return 0;
12661 /* If this is a subtype that should not be emitted as a subrange type,
12662 use the base type. See subrange_type_for_debug_p. */
12663 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12664 type = TREE_TYPE (type);
12666 switch (TREE_CODE (type))
12668 case INTEGER_TYPE:
12669 if ((dwarf_version >= 4 || !dwarf_strict)
12670 && TYPE_NAME (type)
12671 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12672 && DECL_IS_BUILTIN (TYPE_NAME (type))
12673 && DECL_NAME (TYPE_NAME (type)))
12675 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12676 if (strcmp (name, "char16_t") == 0
12677 || strcmp (name, "char32_t") == 0)
12679 encoding = DW_ATE_UTF;
12680 break;
12683 if (TYPE_STRING_FLAG (type))
12685 if (TYPE_UNSIGNED (type))
12686 encoding = DW_ATE_unsigned_char;
12687 else
12688 encoding = DW_ATE_signed_char;
12690 else if (TYPE_UNSIGNED (type))
12691 encoding = DW_ATE_unsigned;
12692 else
12693 encoding = DW_ATE_signed;
12694 break;
12696 case REAL_TYPE:
12697 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12699 if (dwarf_version >= 3 || !dwarf_strict)
12700 encoding = DW_ATE_decimal_float;
12701 else
12702 encoding = DW_ATE_lo_user;
12704 else
12705 encoding = DW_ATE_float;
12706 break;
12708 case FIXED_POINT_TYPE:
12709 if (!(dwarf_version >= 3 || !dwarf_strict))
12710 encoding = DW_ATE_lo_user;
12711 else if (TYPE_UNSIGNED (type))
12712 encoding = DW_ATE_unsigned_fixed;
12713 else
12714 encoding = DW_ATE_signed_fixed;
12715 break;
12717 /* Dwarf2 doesn't know anything about complex ints, so use
12718 a user defined type for it. */
12719 case COMPLEX_TYPE:
12720 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12721 encoding = DW_ATE_complex_float;
12722 else
12723 encoding = DW_ATE_lo_user;
12724 break;
12726 case BOOLEAN_TYPE:
12727 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12728 encoding = DW_ATE_boolean;
12729 break;
12731 default:
12732 /* No other TREE_CODEs are Dwarf fundamental types. */
12733 gcc_unreachable ();
12736 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12738 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12739 int_size_in_bytes (type));
12740 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12742 return base_type_result;
12745 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12746 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12748 static inline int
12749 is_base_type (tree type)
12751 switch (TREE_CODE (type))
12753 case ERROR_MARK:
12754 case VOID_TYPE:
12755 case INTEGER_TYPE:
12756 case REAL_TYPE:
12757 case FIXED_POINT_TYPE:
12758 case COMPLEX_TYPE:
12759 case BOOLEAN_TYPE:
12760 return 1;
12762 case ARRAY_TYPE:
12763 case RECORD_TYPE:
12764 case UNION_TYPE:
12765 case QUAL_UNION_TYPE:
12766 case ENUMERAL_TYPE:
12767 case FUNCTION_TYPE:
12768 case METHOD_TYPE:
12769 case POINTER_TYPE:
12770 case REFERENCE_TYPE:
12771 case NULLPTR_TYPE:
12772 case OFFSET_TYPE:
12773 case LANG_TYPE:
12774 case VECTOR_TYPE:
12775 return 0;
12777 default:
12778 gcc_unreachable ();
12781 return 0;
12784 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12785 node, return the size in bits for the type if it is a constant, or else
12786 return the alignment for the type if the type's size is not constant, or
12787 else return BITS_PER_WORD if the type actually turns out to be an
12788 ERROR_MARK node. */
12790 static inline unsigned HOST_WIDE_INT
12791 simple_type_size_in_bits (const_tree type)
12793 if (TREE_CODE (type) == ERROR_MARK)
12794 return BITS_PER_WORD;
12795 else if (TYPE_SIZE (type) == NULL_TREE)
12796 return 0;
12797 else if (host_integerp (TYPE_SIZE (type), 1))
12798 return tree_low_cst (TYPE_SIZE (type), 1);
12799 else
12800 return TYPE_ALIGN (type);
12803 /* Similarly, but return a double_int instead of UHWI. */
12805 static inline double_int
12806 double_int_type_size_in_bits (const_tree type)
12808 if (TREE_CODE (type) == ERROR_MARK)
12809 return uhwi_to_double_int (BITS_PER_WORD);
12810 else if (TYPE_SIZE (type) == NULL_TREE)
12811 return double_int_zero;
12812 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12813 return tree_to_double_int (TYPE_SIZE (type));
12814 else
12815 return uhwi_to_double_int (TYPE_ALIGN (type));
12818 /* Given a pointer to a tree node for a subrange type, return a pointer
12819 to a DIE that describes the given type. */
12821 static dw_die_ref
12822 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12824 dw_die_ref subrange_die;
12825 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12827 if (context_die == NULL)
12828 context_die = comp_unit_die ();
12830 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12832 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12834 /* The size of the subrange type and its base type do not match,
12835 so we need to generate a size attribute for the subrange type. */
12836 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12839 if (low)
12840 add_bound_info (subrange_die, DW_AT_lower_bound, low);
12841 if (high)
12842 add_bound_info (subrange_die, DW_AT_upper_bound, high);
12844 return subrange_die;
12847 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12848 entry that chains various modifiers in front of the given type. */
12850 static dw_die_ref
12851 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12852 dw_die_ref context_die)
12854 enum tree_code code = TREE_CODE (type);
12855 dw_die_ref mod_type_die;
12856 dw_die_ref sub_die = NULL;
12857 tree item_type = NULL;
12858 tree qualified_type;
12859 tree name, low, high;
12861 if (code == ERROR_MARK)
12862 return NULL;
12864 /* See if we already have the appropriately qualified variant of
12865 this type. */
12866 qualified_type
12867 = get_qualified_type (type,
12868 ((is_const_type ? TYPE_QUAL_CONST : 0)
12869 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12871 if (qualified_type == sizetype
12872 && TYPE_NAME (qualified_type)
12873 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12875 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12877 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12878 && TYPE_PRECISION (t)
12879 == TYPE_PRECISION (qualified_type)
12880 && TYPE_UNSIGNED (t)
12881 == TYPE_UNSIGNED (qualified_type));
12882 qualified_type = t;
12885 /* If we do, then we can just use its DIE, if it exists. */
12886 if (qualified_type)
12888 mod_type_die = lookup_type_die (qualified_type);
12889 if (mod_type_die)
12890 return mod_type_die;
12893 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12895 /* Handle C typedef types. */
12896 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12897 && !DECL_ARTIFICIAL (name))
12899 tree dtype = TREE_TYPE (name);
12901 if (qualified_type == dtype)
12903 /* For a named type, use the typedef. */
12904 gen_type_die (qualified_type, context_die);
12905 return lookup_type_die (qualified_type);
12907 else if (is_const_type < TYPE_READONLY (dtype)
12908 || is_volatile_type < TYPE_VOLATILE (dtype)
12909 || (is_const_type <= TYPE_READONLY (dtype)
12910 && is_volatile_type <= TYPE_VOLATILE (dtype)
12911 && DECL_ORIGINAL_TYPE (name) != type))
12912 /* cv-unqualified version of named type. Just use the unnamed
12913 type to which it refers. */
12914 return modified_type_die (DECL_ORIGINAL_TYPE (name),
12915 is_const_type, is_volatile_type,
12916 context_die);
12917 /* Else cv-qualified version of named type; fall through. */
12920 if (is_const_type
12921 /* If both is_const_type and is_volatile_type, prefer the path
12922 which leads to a qualified type. */
12923 && (!is_volatile_type
12924 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12925 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12927 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12928 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12930 else if (is_volatile_type)
12932 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12933 sub_die = modified_type_die (type, is_const_type, 0, context_die);
12935 else if (code == POINTER_TYPE)
12937 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12938 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12939 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12940 item_type = TREE_TYPE (type);
12941 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12942 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12943 TYPE_ADDR_SPACE (item_type));
12945 else if (code == REFERENCE_TYPE)
12947 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12948 mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12949 type);
12950 else
12951 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12952 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12953 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12954 item_type = TREE_TYPE (type);
12955 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12956 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12957 TYPE_ADDR_SPACE (item_type));
12959 else if (code == INTEGER_TYPE
12960 && TREE_TYPE (type) != NULL_TREE
12961 && subrange_type_for_debug_p (type, &low, &high))
12963 mod_type_die = subrange_type_die (type, low, high, context_die);
12964 item_type = TREE_TYPE (type);
12966 else if (is_base_type (type))
12967 mod_type_die = base_type_die (type);
12968 else
12970 gen_type_die (type, context_die);
12972 /* We have to get the type_main_variant here (and pass that to the
12973 `lookup_type_die' routine) because the ..._TYPE node we have
12974 might simply be a *copy* of some original type node (where the
12975 copy was created to help us keep track of typedef names) and
12976 that copy might have a different TYPE_UID from the original
12977 ..._TYPE node. */
12978 if (TREE_CODE (type) != VECTOR_TYPE)
12979 return lookup_type_die (type_main_variant (type));
12980 else
12981 /* Vectors have the debugging information in the type,
12982 not the main variant. */
12983 return lookup_type_die (type);
12986 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12987 don't output a DW_TAG_typedef, since there isn't one in the
12988 user's program; just attach a DW_AT_name to the type.
12989 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12990 if the base type already has the same name. */
12991 if (name
12992 && ((TREE_CODE (name) != TYPE_DECL
12993 && (qualified_type == TYPE_MAIN_VARIANT (type)
12994 || (!is_const_type && !is_volatile_type)))
12995 || (TREE_CODE (name) == TYPE_DECL
12996 && TREE_TYPE (name) == qualified_type
12997 && DECL_NAME (name))))
12999 if (TREE_CODE (name) == TYPE_DECL)
13000 /* Could just call add_name_and_src_coords_attributes here,
13001 but since this is a builtin type it doesn't have any
13002 useful source coordinates anyway. */
13003 name = DECL_NAME (name);
13004 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13006 /* This probably indicates a bug. */
13007 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13008 add_name_attribute (mod_type_die, "__unknown__");
13010 if (qualified_type)
13011 equate_type_number_to_die (qualified_type, mod_type_die);
13013 if (item_type)
13014 /* We must do this after the equate_type_number_to_die call, in case
13015 this is a recursive type. This ensures that the modified_type_die
13016 recursion will terminate even if the type is recursive. Recursive
13017 types are possible in Ada. */
13018 sub_die = modified_type_die (item_type,
13019 TYPE_READONLY (item_type),
13020 TYPE_VOLATILE (item_type),
13021 context_die);
13023 if (sub_die != NULL)
13024 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13026 return mod_type_die;
13029 /* Generate DIEs for the generic parameters of T.
13030 T must be either a generic type or a generic function.
13031 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13033 static void
13034 gen_generic_params_dies (tree t)
13036 tree parms, args;
13037 int parms_num, i;
13038 dw_die_ref die = NULL;
13040 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13041 return;
13043 if (TYPE_P (t))
13044 die = lookup_type_die (t);
13045 else if (DECL_P (t))
13046 die = lookup_decl_die (t);
13048 gcc_assert (die);
13050 parms = lang_hooks.get_innermost_generic_parms (t);
13051 if (!parms)
13052 /* T has no generic parameter. It means T is neither a generic type
13053 or function. End of story. */
13054 return;
13056 parms_num = TREE_VEC_LENGTH (parms);
13057 args = lang_hooks.get_innermost_generic_args (t);
13058 for (i = 0; i < parms_num; i++)
13060 tree parm, arg, arg_pack_elems;
13062 parm = TREE_VEC_ELT (parms, i);
13063 arg = TREE_VEC_ELT (args, i);
13064 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13065 gcc_assert (parm && TREE_VALUE (parm) && arg);
13067 if (parm && TREE_VALUE (parm) && arg)
13069 /* If PARM represents a template parameter pack,
13070 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13071 by DW_TAG_template_*_parameter DIEs for the argument
13072 pack elements of ARG. Note that ARG would then be
13073 an argument pack. */
13074 if (arg_pack_elems)
13075 template_parameter_pack_die (TREE_VALUE (parm),
13076 arg_pack_elems,
13077 die);
13078 else
13079 generic_parameter_die (TREE_VALUE (parm), arg,
13080 true /* Emit DW_AT_name */, die);
13085 /* Create and return a DIE for PARM which should be
13086 the representation of a generic type parameter.
13087 For instance, in the C++ front end, PARM would be a template parameter.
13088 ARG is the argument to PARM.
13089 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13090 name of the PARM.
13091 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13092 as a child node. */
13094 static dw_die_ref
13095 generic_parameter_die (tree parm, tree arg,
13096 bool emit_name_p,
13097 dw_die_ref parent_die)
13099 dw_die_ref tmpl_die = NULL;
13100 const char *name = NULL;
13102 if (!parm || !DECL_NAME (parm) || !arg)
13103 return NULL;
13105 /* We support non-type generic parameters and arguments,
13106 type generic parameters and arguments, as well as
13107 generic generic parameters (a.k.a. template template parameters in C++)
13108 and arguments. */
13109 if (TREE_CODE (parm) == PARM_DECL)
13110 /* PARM is a nontype generic parameter */
13111 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13112 else if (TREE_CODE (parm) == TYPE_DECL)
13113 /* PARM is a type generic parameter. */
13114 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13115 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13116 /* PARM is a generic generic parameter.
13117 Its DIE is a GNU extension. It shall have a
13118 DW_AT_name attribute to represent the name of the template template
13119 parameter, and a DW_AT_GNU_template_name attribute to represent the
13120 name of the template template argument. */
13121 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13122 parent_die, parm);
13123 else
13124 gcc_unreachable ();
13126 if (tmpl_die)
13128 tree tmpl_type;
13130 /* If PARM is a generic parameter pack, it means we are
13131 emitting debug info for a template argument pack element.
13132 In other terms, ARG is a template argument pack element.
13133 In that case, we don't emit any DW_AT_name attribute for
13134 the die. */
13135 if (emit_name_p)
13137 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13138 gcc_assert (name);
13139 add_AT_string (tmpl_die, DW_AT_name, name);
13142 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13144 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13145 TMPL_DIE should have a child DW_AT_type attribute that is set
13146 to the type of the argument to PARM, which is ARG.
13147 If PARM is a type generic parameter, TMPL_DIE should have a
13148 child DW_AT_type that is set to ARG. */
13149 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13150 add_type_attribute (tmpl_die, tmpl_type, 0,
13151 TREE_THIS_VOLATILE (tmpl_type),
13152 parent_die);
13154 else
13156 /* So TMPL_DIE is a DIE representing a
13157 a generic generic template parameter, a.k.a template template
13158 parameter in C++ and arg is a template. */
13160 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13161 to the name of the argument. */
13162 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13163 if (name)
13164 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13167 if (TREE_CODE (parm) == PARM_DECL)
13168 /* So PARM is a non-type generic parameter.
13169 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13170 attribute of TMPL_DIE which value represents the value
13171 of ARG.
13172 We must be careful here:
13173 The value of ARG might reference some function decls.
13174 We might currently be emitting debug info for a generic
13175 type and types are emitted before function decls, we don't
13176 know if the function decls referenced by ARG will actually be
13177 emitted after cgraph computations.
13178 So must defer the generation of the DW_AT_const_value to
13179 after cgraph is ready. */
13180 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13183 return tmpl_die;
13186 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13187 PARM_PACK must be a template parameter pack. The returned DIE
13188 will be child DIE of PARENT_DIE. */
13190 static dw_die_ref
13191 template_parameter_pack_die (tree parm_pack,
13192 tree parm_pack_args,
13193 dw_die_ref parent_die)
13195 dw_die_ref die;
13196 int j;
13198 gcc_assert (parent_die && parm_pack);
13200 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13201 add_name_and_src_coords_attributes (die, parm_pack);
13202 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13203 generic_parameter_die (parm_pack,
13204 TREE_VEC_ELT (parm_pack_args, j),
13205 false /* Don't emit DW_AT_name */,
13206 die);
13207 return die;
13210 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13211 an enumerated type. */
13213 static inline int
13214 type_is_enum (const_tree type)
13216 return TREE_CODE (type) == ENUMERAL_TYPE;
13219 /* Return the DBX register number described by a given RTL node. */
13221 static unsigned int
13222 dbx_reg_number (const_rtx rtl)
13224 unsigned regno = REGNO (rtl);
13226 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13228 #ifdef LEAF_REG_REMAP
13229 if (current_function_uses_only_leaf_regs)
13231 int leaf_reg = LEAF_REG_REMAP (regno);
13232 if (leaf_reg != -1)
13233 regno = (unsigned) leaf_reg;
13235 #endif
13237 return DBX_REGISTER_NUMBER (regno);
13240 /* Optionally add a DW_OP_piece term to a location description expression.
13241 DW_OP_piece is only added if the location description expression already
13242 doesn't end with DW_OP_piece. */
13244 static void
13245 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13247 dw_loc_descr_ref loc;
13249 if (*list_head != NULL)
13251 /* Find the end of the chain. */
13252 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13255 if (loc->dw_loc_opc != DW_OP_piece)
13256 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13260 /* Return a location descriptor that designates a machine register or
13261 zero if there is none. */
13263 static dw_loc_descr_ref
13264 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13266 rtx regs;
13268 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13269 return 0;
13271 /* We only use "frame base" when we're sure we're talking about the
13272 post-prologue local stack frame. We do this by *not* running
13273 register elimination until this point, and recognizing the special
13274 argument pointer and soft frame pointer rtx's.
13275 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13276 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13277 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13279 dw_loc_descr_ref result = NULL;
13281 if (dwarf_version >= 4 || !dwarf_strict)
13283 result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13284 if (result)
13285 add_loc_descr (&result,
13286 new_loc_descr (DW_OP_stack_value, 0, 0));
13288 return result;
13291 regs = targetm.dwarf_register_span (rtl);
13293 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13294 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13295 else
13296 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13299 /* Return a location descriptor that designates a machine register for
13300 a given hard register number. */
13302 static dw_loc_descr_ref
13303 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13305 dw_loc_descr_ref reg_loc_descr;
13307 if (regno <= 31)
13308 reg_loc_descr
13309 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13310 else
13311 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13313 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13314 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13316 return reg_loc_descr;
13319 /* Given an RTL of a register, return a location descriptor that
13320 designates a value that spans more than one register. */
13322 static dw_loc_descr_ref
13323 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13324 enum var_init_status initialized)
13326 int nregs, size, i;
13327 unsigned reg;
13328 dw_loc_descr_ref loc_result = NULL;
13330 reg = REGNO (rtl);
13331 #ifdef LEAF_REG_REMAP
13332 if (current_function_uses_only_leaf_regs)
13334 int leaf_reg = LEAF_REG_REMAP (reg);
13335 if (leaf_reg != -1)
13336 reg = (unsigned) leaf_reg;
13338 #endif
13339 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13340 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13342 /* Simple, contiguous registers. */
13343 if (regs == NULL_RTX)
13345 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13347 loc_result = NULL;
13348 while (nregs--)
13350 dw_loc_descr_ref t;
13352 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13353 VAR_INIT_STATUS_INITIALIZED);
13354 add_loc_descr (&loc_result, t);
13355 add_loc_descr_op_piece (&loc_result, size);
13356 ++reg;
13358 return loc_result;
13361 /* Now onto stupid register sets in non contiguous locations. */
13363 gcc_assert (GET_CODE (regs) == PARALLEL);
13365 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13366 loc_result = NULL;
13368 for (i = 0; i < XVECLEN (regs, 0); ++i)
13370 dw_loc_descr_ref t;
13372 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13373 VAR_INIT_STATUS_INITIALIZED);
13374 add_loc_descr (&loc_result, t);
13375 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13376 add_loc_descr_op_piece (&loc_result, size);
13379 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13380 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13381 return loc_result;
13384 /* Return a location descriptor that designates a constant. */
13386 static dw_loc_descr_ref
13387 int_loc_descriptor (HOST_WIDE_INT i)
13389 enum dwarf_location_atom op;
13391 /* Pick the smallest representation of a constant, rather than just
13392 defaulting to the LEB encoding. */
13393 if (i >= 0)
13395 if (i <= 31)
13396 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13397 else if (i <= 0xff)
13398 op = DW_OP_const1u;
13399 else if (i <= 0xffff)
13400 op = DW_OP_const2u;
13401 else if (HOST_BITS_PER_WIDE_INT == 32
13402 || i <= 0xffffffff)
13403 op = DW_OP_const4u;
13404 else
13405 op = DW_OP_constu;
13407 else
13409 if (i >= -0x80)
13410 op = DW_OP_const1s;
13411 else if (i >= -0x8000)
13412 op = DW_OP_const2s;
13413 else if (HOST_BITS_PER_WIDE_INT == 32
13414 || i >= -0x80000000)
13415 op = DW_OP_const4s;
13416 else
13417 op = DW_OP_consts;
13420 return new_loc_descr (op, i, 0);
13423 /* Return loc description representing "address" of integer value.
13424 This can appear only as toplevel expression. */
13426 static dw_loc_descr_ref
13427 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13429 int litsize;
13430 dw_loc_descr_ref loc_result = NULL;
13432 if (!(dwarf_version >= 4 || !dwarf_strict))
13433 return NULL;
13435 if (i >= 0)
13437 if (i <= 31)
13438 litsize = 1;
13439 else if (i <= 0xff)
13440 litsize = 2;
13441 else if (i <= 0xffff)
13442 litsize = 3;
13443 else if (HOST_BITS_PER_WIDE_INT == 32
13444 || i <= 0xffffffff)
13445 litsize = 5;
13446 else
13447 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13449 else
13451 if (i >= -0x80)
13452 litsize = 2;
13453 else if (i >= -0x8000)
13454 litsize = 3;
13455 else if (HOST_BITS_PER_WIDE_INT == 32
13456 || i >= -0x80000000)
13457 litsize = 5;
13458 else
13459 litsize = 1 + size_of_sleb128 (i);
13461 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13462 is more compact. For DW_OP_stack_value we need:
13463 litsize + 1 (DW_OP_stack_value)
13464 and for DW_OP_implicit_value:
13465 1 (DW_OP_implicit_value) + 1 (length) + size. */
13466 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13468 loc_result = int_loc_descriptor (i);
13469 add_loc_descr (&loc_result,
13470 new_loc_descr (DW_OP_stack_value, 0, 0));
13471 return loc_result;
13474 loc_result = new_loc_descr (DW_OP_implicit_value,
13475 size, 0);
13476 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13477 loc_result->dw_loc_oprnd2.v.val_int = i;
13478 return loc_result;
13481 /* Return a location descriptor that designates a base+offset location. */
13483 static dw_loc_descr_ref
13484 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13485 enum var_init_status initialized)
13487 unsigned int regno;
13488 dw_loc_descr_ref result;
13489 dw_fde_ref fde = current_fde ();
13491 /* We only use "frame base" when we're sure we're talking about the
13492 post-prologue local stack frame. We do this by *not* running
13493 register elimination until this point, and recognizing the special
13494 argument pointer and soft frame pointer rtx's. */
13495 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13497 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13499 if (elim != reg)
13501 if (GET_CODE (elim) == PLUS)
13503 offset += INTVAL (XEXP (elim, 1));
13504 elim = XEXP (elim, 0);
13506 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13507 && (elim == hard_frame_pointer_rtx
13508 || elim == stack_pointer_rtx))
13509 || elim == (frame_pointer_needed
13510 ? hard_frame_pointer_rtx
13511 : stack_pointer_rtx));
13513 /* If drap register is used to align stack, use frame
13514 pointer + offset to access stack variables. If stack
13515 is aligned without drap, use stack pointer + offset to
13516 access stack variables. */
13517 if (crtl->stack_realign_tried
13518 && reg == frame_pointer_rtx)
13520 int base_reg
13521 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13522 ? HARD_FRAME_POINTER_REGNUM
13523 : STACK_POINTER_REGNUM);
13524 return new_reg_loc_descr (base_reg, offset);
13527 offset += frame_pointer_fb_offset;
13528 return new_loc_descr (DW_OP_fbreg, offset, 0);
13531 else if (!optimize
13532 && fde
13533 && (fde->drap_reg == REGNO (reg)
13534 || fde->vdrap_reg == REGNO (reg)))
13536 /* Use cfa+offset to represent the location of arguments passed
13537 on the stack when drap is used to align stack.
13538 Only do this when not optimizing, for optimized code var-tracking
13539 is supposed to track where the arguments live and the register
13540 used as vdrap or drap in some spot might be used for something
13541 else in other part of the routine. */
13542 return new_loc_descr (DW_OP_fbreg, offset, 0);
13545 regno = dbx_reg_number (reg);
13546 if (regno <= 31)
13547 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13548 offset, 0);
13549 else
13550 result = new_loc_descr (DW_OP_bregx, regno, offset);
13552 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13553 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13555 return result;
13558 /* Return true if this RTL expression describes a base+offset calculation. */
13560 static inline int
13561 is_based_loc (const_rtx rtl)
13563 return (GET_CODE (rtl) == PLUS
13564 && ((REG_P (XEXP (rtl, 0))
13565 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13566 && CONST_INT_P (XEXP (rtl, 1)))));
13569 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13570 failed. */
13572 static dw_loc_descr_ref
13573 tls_mem_loc_descriptor (rtx mem)
13575 tree base;
13576 dw_loc_descr_ref loc_result;
13578 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13579 return NULL;
13581 base = get_base_address (MEM_EXPR (mem));
13582 if (base == NULL
13583 || TREE_CODE (base) != VAR_DECL
13584 || !DECL_THREAD_LOCAL_P (base))
13585 return NULL;
13587 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13588 if (loc_result == NULL)
13589 return NULL;
13591 if (INTVAL (MEM_OFFSET (mem)))
13592 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13594 return loc_result;
13597 /* Output debug info about reason why we failed to expand expression as dwarf
13598 expression. */
13600 static void
13601 expansion_failed (tree expr, rtx rtl, char const *reason)
13603 if (dump_file && (dump_flags & TDF_DETAILS))
13605 fprintf (dump_file, "Failed to expand as dwarf: ");
13606 if (expr)
13607 print_generic_expr (dump_file, expr, dump_flags);
13608 if (rtl)
13610 fprintf (dump_file, "\n");
13611 print_rtl (dump_file, rtl);
13613 fprintf (dump_file, "\nReason: %s\n", reason);
13617 /* Helper function for const_ok_for_output, called either directly
13618 or via for_each_rtx. */
13620 static int
13621 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13623 rtx rtl = *rtlp;
13625 if (GET_CODE (rtl) == UNSPEC)
13627 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13628 we can't express it in the debug info. */
13629 #ifdef ENABLE_CHECKING
13630 /* Don't complain about TLS UNSPECs, those are just too hard to
13631 delegitimize. */
13632 if (XVECLEN (rtl, 0) != 1
13633 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13634 || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13635 || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13636 || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13637 inform (current_function_decl
13638 ? DECL_SOURCE_LOCATION (current_function_decl)
13639 : UNKNOWN_LOCATION,
13640 "non-delegitimized UNSPEC %d found in variable location",
13641 XINT (rtl, 1));
13642 #endif
13643 expansion_failed (NULL_TREE, rtl,
13644 "UNSPEC hasn't been delegitimized.\n");
13645 return 1;
13648 if (GET_CODE (rtl) != SYMBOL_REF)
13649 return 0;
13651 if (CONSTANT_POOL_ADDRESS_P (rtl))
13653 bool marked;
13654 get_pool_constant_mark (rtl, &marked);
13655 /* If all references to this pool constant were optimized away,
13656 it was not output and thus we can't represent it. */
13657 if (!marked)
13659 expansion_failed (NULL_TREE, rtl,
13660 "Constant was removed from constant pool.\n");
13661 return 1;
13665 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13666 return 1;
13668 /* Avoid references to external symbols in debug info, on several targets
13669 the linker might even refuse to link when linking a shared library,
13670 and in many other cases the relocations for .debug_info/.debug_loc are
13671 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13672 to be defined within the same shared library or executable are fine. */
13673 if (SYMBOL_REF_EXTERNAL_P (rtl))
13675 tree decl = SYMBOL_REF_DECL (rtl);
13677 if (decl == NULL || !targetm.binds_local_p (decl))
13679 expansion_failed (NULL_TREE, rtl,
13680 "Symbol not defined in current TU.\n");
13681 return 1;
13685 return 0;
13688 /* Return true if constant RTL can be emitted in DW_OP_addr or
13689 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13690 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13692 static bool
13693 const_ok_for_output (rtx rtl)
13695 if (GET_CODE (rtl) == SYMBOL_REF)
13696 return const_ok_for_output_1 (&rtl, NULL) == 0;
13698 if (GET_CODE (rtl) == CONST)
13699 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13701 return true;
13704 /* The following routine converts the RTL for a variable or parameter
13705 (resident in memory) into an equivalent Dwarf representation of a
13706 mechanism for getting the address of that same variable onto the top of a
13707 hypothetical "address evaluation" stack.
13709 When creating memory location descriptors, we are effectively transforming
13710 the RTL for a memory-resident object into its Dwarf postfix expression
13711 equivalent. This routine recursively descends an RTL tree, turning
13712 it into Dwarf postfix code as it goes.
13714 MODE is the mode of the memory reference, needed to handle some
13715 autoincrement addressing modes.
13717 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13718 location list for RTL.
13720 Return 0 if we can't represent the location. */
13722 static dw_loc_descr_ref
13723 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13724 enum var_init_status initialized)
13726 dw_loc_descr_ref mem_loc_result = NULL;
13727 enum dwarf_location_atom op;
13728 dw_loc_descr_ref op0, op1;
13730 /* Note that for a dynamically sized array, the location we will generate a
13731 description of here will be the lowest numbered location which is
13732 actually within the array. That's *not* necessarily the same as the
13733 zeroth element of the array. */
13735 rtl = targetm.delegitimize_address (rtl);
13737 switch (GET_CODE (rtl))
13739 case POST_INC:
13740 case POST_DEC:
13741 case POST_MODIFY:
13742 return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13744 case SUBREG:
13745 /* The case of a subreg may arise when we have a local (register)
13746 variable or a formal (register) parameter which doesn't quite fill
13747 up an entire register. For now, just assume that it is
13748 legitimate to make the Dwarf info refer to the whole register which
13749 contains the given subreg. */
13750 if (!subreg_lowpart_p (rtl))
13751 break;
13752 rtl = SUBREG_REG (rtl);
13753 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13754 break;
13755 if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13756 break;
13757 mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13758 break;
13760 case REG:
13761 /* Whenever a register number forms a part of the description of the
13762 method for calculating the (dynamic) address of a memory resident
13763 object, DWARF rules require the register number be referred to as
13764 a "base register". This distinction is not based in any way upon
13765 what category of register the hardware believes the given register
13766 belongs to. This is strictly DWARF terminology we're dealing with
13767 here. Note that in cases where the location of a memory-resident
13768 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13769 OP_CONST (0)) the actual DWARF location descriptor that we generate
13770 may just be OP_BASEREG (basereg). This may look deceptively like
13771 the object in question was allocated to a register (rather than in
13772 memory) so DWARF consumers need to be aware of the subtle
13773 distinction between OP_REG and OP_BASEREG. */
13774 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13775 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13776 else if (stack_realign_drap
13777 && crtl->drap_reg
13778 && crtl->args.internal_arg_pointer == rtl
13779 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13781 /* If RTL is internal_arg_pointer, which has been optimized
13782 out, use DRAP instead. */
13783 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13784 VAR_INIT_STATUS_INITIALIZED);
13786 break;
13788 case SIGN_EXTEND:
13789 case ZERO_EXTEND:
13790 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13791 VAR_INIT_STATUS_INITIALIZED);
13792 if (op0 == 0)
13793 break;
13794 else
13796 int shift = DWARF2_ADDR_SIZE
13797 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13798 shift *= BITS_PER_UNIT;
13799 if (GET_CODE (rtl) == SIGN_EXTEND)
13800 op = DW_OP_shra;
13801 else
13802 op = DW_OP_shr;
13803 mem_loc_result = op0;
13804 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13805 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13806 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13807 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13809 break;
13811 case MEM:
13812 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13813 VAR_INIT_STATUS_INITIALIZED);
13814 if (mem_loc_result == NULL)
13815 mem_loc_result = tls_mem_loc_descriptor (rtl);
13816 if (mem_loc_result != 0)
13818 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13820 expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13821 return 0;
13823 else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13824 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13825 else
13826 add_loc_descr (&mem_loc_result,
13827 new_loc_descr (DW_OP_deref_size,
13828 GET_MODE_SIZE (GET_MODE (rtl)), 0));
13830 else
13832 rtx new_rtl = avoid_constant_pool_reference (rtl);
13833 if (new_rtl != rtl)
13834 return mem_loc_descriptor (new_rtl, mode, initialized);
13836 break;
13838 case LO_SUM:
13839 rtl = XEXP (rtl, 1);
13841 /* ... fall through ... */
13843 case LABEL_REF:
13844 /* Some ports can transform a symbol ref into a label ref, because
13845 the symbol ref is too far away and has to be dumped into a constant
13846 pool. */
13847 case CONST:
13848 case SYMBOL_REF:
13849 if (GET_CODE (rtl) == SYMBOL_REF
13850 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13852 dw_loc_descr_ref temp;
13854 /* If this is not defined, we have no way to emit the data. */
13855 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13856 break;
13858 /* We used to emit DW_OP_addr here, but that's wrong, since
13859 DW_OP_addr should be relocated by the debug info consumer,
13860 while DW_OP_GNU_push_tls_address operand should not. */
13861 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13862 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13863 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13864 temp->dw_loc_oprnd1.v.val_addr = rtl;
13865 temp->dtprel = true;
13867 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13868 add_loc_descr (&mem_loc_result, temp);
13870 break;
13873 if (!const_ok_for_output (rtl))
13874 break;
13876 symref:
13877 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13878 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13879 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13880 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13881 break;
13883 case CONCAT:
13884 case CONCATN:
13885 case VAR_LOCATION:
13886 case DEBUG_IMPLICIT_PTR:
13887 expansion_failed (NULL_TREE, rtl,
13888 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13889 return 0;
13891 case PRE_MODIFY:
13892 /* Extract the PLUS expression nested inside and fall into
13893 PLUS code below. */
13894 rtl = XEXP (rtl, 1);
13895 goto plus;
13897 case PRE_INC:
13898 case PRE_DEC:
13899 /* Turn these into a PLUS expression and fall into the PLUS code
13900 below. */
13901 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13902 GEN_INT (GET_CODE (rtl) == PRE_INC
13903 ? GET_MODE_UNIT_SIZE (mode)
13904 : -GET_MODE_UNIT_SIZE (mode)));
13906 /* ... fall through ... */
13908 case PLUS:
13909 plus:
13910 if (is_based_loc (rtl))
13911 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13912 INTVAL (XEXP (rtl, 1)),
13913 VAR_INIT_STATUS_INITIALIZED);
13914 else
13916 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13917 VAR_INIT_STATUS_INITIALIZED);
13918 if (mem_loc_result == 0)
13919 break;
13921 if (CONST_INT_P (XEXP (rtl, 1)))
13922 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13923 else
13925 dw_loc_descr_ref mem_loc_result2
13926 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13927 VAR_INIT_STATUS_INITIALIZED);
13928 if (mem_loc_result2 == 0)
13929 break;
13930 add_loc_descr (&mem_loc_result, mem_loc_result2);
13931 add_loc_descr (&mem_loc_result,
13932 new_loc_descr (DW_OP_plus, 0, 0));
13935 break;
13937 /* If a pseudo-reg is optimized away, it is possible for it to
13938 be replaced with a MEM containing a multiply or shift. */
13939 case MINUS:
13940 op = DW_OP_minus;
13941 goto do_binop;
13943 case MULT:
13944 op = DW_OP_mul;
13945 goto do_binop;
13947 case DIV:
13948 op = DW_OP_div;
13949 goto do_binop;
13951 case UMOD:
13952 op = DW_OP_mod;
13953 goto do_binop;
13955 case ASHIFT:
13956 op = DW_OP_shl;
13957 goto do_binop;
13959 case ASHIFTRT:
13960 op = DW_OP_shra;
13961 goto do_binop;
13963 case LSHIFTRT:
13964 op = DW_OP_shr;
13965 goto do_binop;
13967 case AND:
13968 op = DW_OP_and;
13969 goto do_binop;
13971 case IOR:
13972 op = DW_OP_or;
13973 goto do_binop;
13975 case XOR:
13976 op = DW_OP_xor;
13977 goto do_binop;
13979 do_binop:
13980 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13981 VAR_INIT_STATUS_INITIALIZED);
13982 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13983 VAR_INIT_STATUS_INITIALIZED);
13985 if (op0 == 0 || op1 == 0)
13986 break;
13988 mem_loc_result = op0;
13989 add_loc_descr (&mem_loc_result, op1);
13990 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13991 break;
13993 case MOD:
13994 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13995 VAR_INIT_STATUS_INITIALIZED);
13996 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13997 VAR_INIT_STATUS_INITIALIZED);
13999 if (op0 == 0 || op1 == 0)
14000 break;
14002 mem_loc_result = op0;
14003 add_loc_descr (&mem_loc_result, op1);
14004 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14005 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14006 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14007 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14008 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14009 break;
14011 case NOT:
14012 op = DW_OP_not;
14013 goto do_unop;
14015 case ABS:
14016 op = DW_OP_abs;
14017 goto do_unop;
14019 case NEG:
14020 op = DW_OP_neg;
14021 goto do_unop;
14023 do_unop:
14024 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14025 VAR_INIT_STATUS_INITIALIZED);
14027 if (op0 == 0)
14028 break;
14030 mem_loc_result = op0;
14031 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14032 break;
14034 case CONST_INT:
14035 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14036 break;
14038 case EQ:
14039 op = DW_OP_eq;
14040 goto do_scompare;
14042 case GE:
14043 op = DW_OP_ge;
14044 goto do_scompare;
14046 case GT:
14047 op = DW_OP_gt;
14048 goto do_scompare;
14050 case LE:
14051 op = DW_OP_le;
14052 goto do_scompare;
14054 case LT:
14055 op = DW_OP_lt;
14056 goto do_scompare;
14058 case NE:
14059 op = DW_OP_ne;
14060 goto do_scompare;
14062 do_scompare:
14063 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14064 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14065 break;
14066 else
14068 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14070 if (op_mode == VOIDmode)
14071 op_mode = GET_MODE (XEXP (rtl, 1));
14072 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14073 break;
14075 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14076 VAR_INIT_STATUS_INITIALIZED);
14077 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14078 VAR_INIT_STATUS_INITIALIZED);
14080 if (op0 == 0 || op1 == 0)
14081 break;
14083 if (op_mode != VOIDmode
14084 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14086 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14087 shift *= BITS_PER_UNIT;
14088 /* For eq/ne, if the operands are known to be zero-extended,
14089 there is no need to do the fancy shifting up. */
14090 if (op == DW_OP_eq || op == DW_OP_ne)
14092 dw_loc_descr_ref last0, last1;
14093 for (last0 = op0;
14094 last0->dw_loc_next != NULL;
14095 last0 = last0->dw_loc_next)
14097 for (last1 = op1;
14098 last1->dw_loc_next != NULL;
14099 last1 = last1->dw_loc_next)
14101 /* deref_size zero extends, and for constants we can check
14102 whether they are zero extended or not. */
14103 if (((last0->dw_loc_opc == DW_OP_deref_size
14104 && last0->dw_loc_oprnd1.v.val_int
14105 <= GET_MODE_SIZE (op_mode))
14106 || (CONST_INT_P (XEXP (rtl, 0))
14107 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14108 == (INTVAL (XEXP (rtl, 0))
14109 & GET_MODE_MASK (op_mode))))
14110 && ((last1->dw_loc_opc == DW_OP_deref_size
14111 && last1->dw_loc_oprnd1.v.val_int
14112 <= GET_MODE_SIZE (op_mode))
14113 || (CONST_INT_P (XEXP (rtl, 1))
14114 && (unsigned HOST_WIDE_INT)
14115 INTVAL (XEXP (rtl, 1))
14116 == (INTVAL (XEXP (rtl, 1))
14117 & GET_MODE_MASK (op_mode)))))
14118 goto do_compare;
14120 add_loc_descr (&op0, int_loc_descriptor (shift));
14121 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14122 if (CONST_INT_P (XEXP (rtl, 1)))
14123 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14124 else
14126 add_loc_descr (&op1, int_loc_descriptor (shift));
14127 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14132 do_compare:
14133 mem_loc_result = op0;
14134 add_loc_descr (&mem_loc_result, op1);
14135 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14136 if (STORE_FLAG_VALUE != 1)
14138 add_loc_descr (&mem_loc_result,
14139 int_loc_descriptor (STORE_FLAG_VALUE));
14140 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14142 break;
14144 case GEU:
14145 op = DW_OP_ge;
14146 goto do_ucompare;
14148 case GTU:
14149 op = DW_OP_gt;
14150 goto do_ucompare;
14152 case LEU:
14153 op = DW_OP_le;
14154 goto do_ucompare;
14156 case LTU:
14157 op = DW_OP_lt;
14158 goto do_ucompare;
14160 do_ucompare:
14161 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14162 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14163 break;
14164 else
14166 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14168 if (op_mode == VOIDmode)
14169 op_mode = GET_MODE (XEXP (rtl, 1));
14170 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14171 break;
14173 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14174 VAR_INIT_STATUS_INITIALIZED);
14175 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14176 VAR_INIT_STATUS_INITIALIZED);
14178 if (op0 == 0 || op1 == 0)
14179 break;
14181 if (op_mode != VOIDmode
14182 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14184 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14185 dw_loc_descr_ref last0, last1;
14186 for (last0 = op0;
14187 last0->dw_loc_next != NULL;
14188 last0 = last0->dw_loc_next)
14190 for (last1 = op1;
14191 last1->dw_loc_next != NULL;
14192 last1 = last1->dw_loc_next)
14194 if (CONST_INT_P (XEXP (rtl, 0)))
14195 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14196 /* deref_size zero extends, so no need to mask it again. */
14197 else if (last0->dw_loc_opc != DW_OP_deref_size
14198 || last0->dw_loc_oprnd1.v.val_int
14199 > GET_MODE_SIZE (op_mode))
14201 add_loc_descr (&op0, int_loc_descriptor (mask));
14202 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14204 if (CONST_INT_P (XEXP (rtl, 1)))
14205 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14206 /* deref_size zero extends, so no need to mask it again. */
14207 else if (last1->dw_loc_opc != DW_OP_deref_size
14208 || last1->dw_loc_oprnd1.v.val_int
14209 > GET_MODE_SIZE (op_mode))
14211 add_loc_descr (&op1, int_loc_descriptor (mask));
14212 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14215 else
14217 HOST_WIDE_INT bias = 1;
14218 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14219 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14220 if (CONST_INT_P (XEXP (rtl, 1)))
14221 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14222 + INTVAL (XEXP (rtl, 1)));
14223 else
14224 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14225 bias, 0));
14228 goto do_compare;
14230 case SMIN:
14231 case SMAX:
14232 case UMIN:
14233 case UMAX:
14234 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14235 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14236 || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14237 break;
14239 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14240 VAR_INIT_STATUS_INITIALIZED);
14241 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14242 VAR_INIT_STATUS_INITIALIZED);
14244 if (op0 == 0 || op1 == 0)
14245 break;
14247 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14248 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14249 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14250 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14252 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14254 HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14255 add_loc_descr (&op0, int_loc_descriptor (mask));
14256 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14257 add_loc_descr (&op1, int_loc_descriptor (mask));
14258 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14260 else
14262 HOST_WIDE_INT bias = 1;
14263 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14264 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14265 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14268 else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14270 int shift = DWARF2_ADDR_SIZE
14271 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14272 shift *= BITS_PER_UNIT;
14273 add_loc_descr (&op0, int_loc_descriptor (shift));
14274 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14275 add_loc_descr (&op1, int_loc_descriptor (shift));
14276 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14279 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14280 op = DW_OP_lt;
14281 else
14282 op = DW_OP_gt;
14283 mem_loc_result = op0;
14284 add_loc_descr (&mem_loc_result, op1);
14285 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14287 dw_loc_descr_ref bra_node, drop_node;
14289 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14290 add_loc_descr (&mem_loc_result, bra_node);
14291 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14292 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14293 add_loc_descr (&mem_loc_result, drop_node);
14294 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14295 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14297 break;
14299 case ZERO_EXTRACT:
14300 case SIGN_EXTRACT:
14301 if (CONST_INT_P (XEXP (rtl, 1))
14302 && CONST_INT_P (XEXP (rtl, 2))
14303 && ((unsigned) INTVAL (XEXP (rtl, 1))
14304 + (unsigned) INTVAL (XEXP (rtl, 2))
14305 <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14306 && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14307 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14309 int shift, size;
14310 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14311 VAR_INIT_STATUS_INITIALIZED);
14312 if (op0 == 0)
14313 break;
14314 if (GET_CODE (rtl) == SIGN_EXTRACT)
14315 op = DW_OP_shra;
14316 else
14317 op = DW_OP_shr;
14318 mem_loc_result = op0;
14319 size = INTVAL (XEXP (rtl, 1));
14320 shift = INTVAL (XEXP (rtl, 2));
14321 if (BITS_BIG_ENDIAN)
14322 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14323 - shift - size;
14324 if (shift + size != (int) DWARF2_ADDR_SIZE)
14326 add_loc_descr (&mem_loc_result,
14327 int_loc_descriptor (DWARF2_ADDR_SIZE
14328 - shift - size));
14329 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14331 if (size != (int) DWARF2_ADDR_SIZE)
14333 add_loc_descr (&mem_loc_result,
14334 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14335 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14338 break;
14340 case IF_THEN_ELSE:
14342 dw_loc_descr_ref op2, bra_node, drop_node;
14343 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14344 VAR_INIT_STATUS_INITIALIZED);
14345 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14346 VAR_INIT_STATUS_INITIALIZED);
14347 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14348 VAR_INIT_STATUS_INITIALIZED);
14349 if (op0 == NULL || op1 == NULL || op2 == NULL)
14350 break;
14352 mem_loc_result = op1;
14353 add_loc_descr (&mem_loc_result, op2);
14354 add_loc_descr (&mem_loc_result, op0);
14355 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14356 add_loc_descr (&mem_loc_result, bra_node);
14357 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14358 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14359 add_loc_descr (&mem_loc_result, drop_node);
14360 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14361 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14363 break;
14365 case COMPARE:
14366 case ROTATE:
14367 case ROTATERT:
14368 case TRUNCATE:
14369 /* In theory, we could implement the above. */
14370 /* DWARF cannot represent the unsigned compare operations
14371 natively. */
14372 case SS_MULT:
14373 case US_MULT:
14374 case SS_DIV:
14375 case US_DIV:
14376 case SS_PLUS:
14377 case US_PLUS:
14378 case SS_MINUS:
14379 case US_MINUS:
14380 case SS_NEG:
14381 case US_NEG:
14382 case SS_ABS:
14383 case SS_ASHIFT:
14384 case US_ASHIFT:
14385 case SS_TRUNCATE:
14386 case US_TRUNCATE:
14387 case UDIV:
14388 case UNORDERED:
14389 case ORDERED:
14390 case UNEQ:
14391 case UNGE:
14392 case UNGT:
14393 case UNLE:
14394 case UNLT:
14395 case LTGT:
14396 case FLOAT_EXTEND:
14397 case FLOAT_TRUNCATE:
14398 case FLOAT:
14399 case UNSIGNED_FLOAT:
14400 case FIX:
14401 case UNSIGNED_FIX:
14402 case FRACT_CONVERT:
14403 case UNSIGNED_FRACT_CONVERT:
14404 case SAT_FRACT:
14405 case UNSIGNED_SAT_FRACT:
14406 case SQRT:
14407 case BSWAP:
14408 case FFS:
14409 case CLZ:
14410 case CTZ:
14411 case POPCOUNT:
14412 case PARITY:
14413 case ASM_OPERANDS:
14414 case VEC_MERGE:
14415 case VEC_SELECT:
14416 case VEC_CONCAT:
14417 case VEC_DUPLICATE:
14418 case UNSPEC:
14419 case HIGH:
14420 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14421 can't express it in the debug info. This can happen e.g. with some
14422 TLS UNSPECs. */
14423 break;
14425 case CONST_STRING:
14426 resolve_one_addr (&rtl, NULL);
14427 goto symref;
14429 default:
14430 #ifdef ENABLE_CHECKING
14431 print_rtl (stderr, rtl);
14432 gcc_unreachable ();
14433 #else
14434 break;
14435 #endif
14438 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14439 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14441 return mem_loc_result;
14444 /* Return a descriptor that describes the concatenation of two locations.
14445 This is typically a complex variable. */
14447 static dw_loc_descr_ref
14448 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14450 dw_loc_descr_ref cc_loc_result = NULL;
14451 dw_loc_descr_ref x0_ref
14452 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14453 dw_loc_descr_ref x1_ref
14454 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14456 if (x0_ref == 0 || x1_ref == 0)
14457 return 0;
14459 cc_loc_result = x0_ref;
14460 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14462 add_loc_descr (&cc_loc_result, x1_ref);
14463 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14465 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14466 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14468 return cc_loc_result;
14471 /* Return a descriptor that describes the concatenation of N
14472 locations. */
14474 static dw_loc_descr_ref
14475 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14477 unsigned int i;
14478 dw_loc_descr_ref cc_loc_result = NULL;
14479 unsigned int n = XVECLEN (concatn, 0);
14481 for (i = 0; i < n; ++i)
14483 dw_loc_descr_ref ref;
14484 rtx x = XVECEXP (concatn, 0, i);
14486 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14487 if (ref == NULL)
14488 return NULL;
14490 add_loc_descr (&cc_loc_result, ref);
14491 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14494 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14495 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14497 return cc_loc_result;
14500 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14501 for DEBUG_IMPLICIT_PTR RTL. */
14503 static dw_loc_descr_ref
14504 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14506 dw_loc_descr_ref ret;
14507 dw_die_ref ref;
14509 if (dwarf_strict)
14510 return NULL;
14511 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14512 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14513 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14514 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14515 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14516 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14517 if (ref)
14519 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14520 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14521 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14523 else
14525 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14526 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14528 return ret;
14531 /* Output a proper Dwarf location descriptor for a variable or parameter
14532 which is either allocated in a register or in a memory location. For a
14533 register, we just generate an OP_REG and the register number. For a
14534 memory location we provide a Dwarf postfix expression describing how to
14535 generate the (dynamic) address of the object onto the address stack.
14537 MODE is mode of the decl if this loc_descriptor is going to be used in
14538 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14539 allowed, VOIDmode otherwise.
14541 If we don't know how to describe it, return 0. */
14543 static dw_loc_descr_ref
14544 loc_descriptor (rtx rtl, enum machine_mode mode,
14545 enum var_init_status initialized)
14547 dw_loc_descr_ref loc_result = NULL;
14549 switch (GET_CODE (rtl))
14551 case SUBREG:
14552 /* The case of a subreg may arise when we have a local (register)
14553 variable or a formal (register) parameter which doesn't quite fill
14554 up an entire register. For now, just assume that it is
14555 legitimate to make the Dwarf info refer to the whole register which
14556 contains the given subreg. */
14557 loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14558 break;
14560 case REG:
14561 loc_result = reg_loc_descriptor (rtl, initialized);
14562 break;
14564 case MEM:
14565 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14566 initialized);
14567 if (loc_result == NULL)
14568 loc_result = tls_mem_loc_descriptor (rtl);
14569 if (loc_result == NULL)
14571 rtx new_rtl = avoid_constant_pool_reference (rtl);
14572 if (new_rtl != rtl)
14573 loc_result = loc_descriptor (new_rtl, mode, initialized);
14575 break;
14577 case CONCAT:
14578 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14579 initialized);
14580 break;
14582 case CONCATN:
14583 loc_result = concatn_loc_descriptor (rtl, initialized);
14584 break;
14586 case VAR_LOCATION:
14587 /* Single part. */
14588 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14590 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14591 if (GET_CODE (loc) == EXPR_LIST)
14592 loc = XEXP (loc, 0);
14593 loc_result = loc_descriptor (loc, mode, initialized);
14594 break;
14597 rtl = XEXP (rtl, 1);
14598 /* FALLTHRU */
14600 case PARALLEL:
14602 rtvec par_elems = XVEC (rtl, 0);
14603 int num_elem = GET_NUM_ELEM (par_elems);
14604 enum machine_mode mode;
14605 int i;
14607 /* Create the first one, so we have something to add to. */
14608 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14609 VOIDmode, initialized);
14610 if (loc_result == NULL)
14611 return NULL;
14612 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14613 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14614 for (i = 1; i < num_elem; i++)
14616 dw_loc_descr_ref temp;
14618 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14619 VOIDmode, initialized);
14620 if (temp == NULL)
14621 return NULL;
14622 add_loc_descr (&loc_result, temp);
14623 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14624 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14627 break;
14629 case CONST_INT:
14630 if (mode != VOIDmode && mode != BLKmode)
14631 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14632 INTVAL (rtl));
14633 break;
14635 case CONST_DOUBLE:
14636 if (mode == VOIDmode)
14637 mode = GET_MODE (rtl);
14639 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14641 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14643 /* Note that a CONST_DOUBLE rtx could represent either an integer
14644 or a floating-point constant. A CONST_DOUBLE is used whenever
14645 the constant requires more than one word in order to be
14646 adequately represented. We output CONST_DOUBLEs as blocks. */
14647 loc_result = new_loc_descr (DW_OP_implicit_value,
14648 GET_MODE_SIZE (mode), 0);
14649 if (SCALAR_FLOAT_MODE_P (mode))
14651 unsigned int length = GET_MODE_SIZE (mode);
14652 unsigned char *array
14653 = (unsigned char*) ggc_alloc_atomic (length);
14655 insert_float (rtl, array);
14656 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14657 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14658 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14659 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14661 else
14663 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14664 loc_result->dw_loc_oprnd2.v.val_double
14665 = rtx_to_double_int (rtl);
14668 break;
14670 case CONST_VECTOR:
14671 if (mode == VOIDmode)
14672 mode = GET_MODE (rtl);
14674 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14676 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14677 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14678 unsigned char *array = (unsigned char *)
14679 ggc_alloc_atomic (length * elt_size);
14680 unsigned int i;
14681 unsigned char *p;
14683 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14684 switch (GET_MODE_CLASS (mode))
14686 case MODE_VECTOR_INT:
14687 for (i = 0, p = array; i < length; i++, p += elt_size)
14689 rtx elt = CONST_VECTOR_ELT (rtl, i);
14690 double_int val = rtx_to_double_int (elt);
14692 if (elt_size <= sizeof (HOST_WIDE_INT))
14693 insert_int (double_int_to_shwi (val), elt_size, p);
14694 else
14696 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14697 insert_double (val, p);
14700 break;
14702 case MODE_VECTOR_FLOAT:
14703 for (i = 0, p = array; i < length; i++, p += elt_size)
14705 rtx elt = CONST_VECTOR_ELT (rtl, i);
14706 insert_float (elt, p);
14708 break;
14710 default:
14711 gcc_unreachable ();
14714 loc_result = new_loc_descr (DW_OP_implicit_value,
14715 length * elt_size, 0);
14716 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14717 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14718 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14719 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14721 break;
14723 case CONST:
14724 if (mode == VOIDmode
14725 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14726 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14727 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14729 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14730 break;
14732 /* FALLTHROUGH */
14733 case SYMBOL_REF:
14734 if (!const_ok_for_output (rtl))
14735 break;
14736 case LABEL_REF:
14737 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14738 && (dwarf_version >= 4 || !dwarf_strict))
14740 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14741 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14742 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14743 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14744 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14746 break;
14748 case DEBUG_IMPLICIT_PTR:
14749 loc_result = implicit_ptr_descriptor (rtl, 0);
14750 break;
14752 case PLUS:
14753 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14754 && CONST_INT_P (XEXP (rtl, 1)))
14756 loc_result
14757 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14758 break;
14760 /* FALLTHRU */
14761 default:
14762 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14763 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14764 && (dwarf_version >= 4 || !dwarf_strict))
14766 /* Value expression. */
14767 loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14768 if (loc_result)
14769 add_loc_descr (&loc_result,
14770 new_loc_descr (DW_OP_stack_value, 0, 0));
14772 break;
14775 return loc_result;
14778 /* We need to figure out what section we should use as the base for the
14779 address ranges where a given location is valid.
14780 1. If this particular DECL has a section associated with it, use that.
14781 2. If this function has a section associated with it, use that.
14782 3. Otherwise, use the text section.
14783 XXX: If you split a variable across multiple sections, we won't notice. */
14785 static const char *
14786 secname_for_decl (const_tree decl)
14788 const char *secname;
14790 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14792 tree sectree = DECL_SECTION_NAME (decl);
14793 secname = TREE_STRING_POINTER (sectree);
14795 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14797 tree sectree = DECL_SECTION_NAME (current_function_decl);
14798 secname = TREE_STRING_POINTER (sectree);
14800 else if (cfun && in_cold_section_p)
14801 secname = crtl->subsections.cold_section_label;
14802 else
14803 secname = text_section_label;
14805 return secname;
14808 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14810 static bool
14811 decl_by_reference_p (tree decl)
14813 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14814 || TREE_CODE (decl) == VAR_DECL)
14815 && DECL_BY_REFERENCE (decl));
14818 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14819 for VARLOC. */
14821 static dw_loc_descr_ref
14822 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14823 enum var_init_status initialized)
14825 int have_address = 0;
14826 dw_loc_descr_ref descr;
14827 enum machine_mode mode;
14829 if (want_address != 2)
14831 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14832 /* Single part. */
14833 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14835 varloc = PAT_VAR_LOCATION_LOC (varloc);
14836 if (GET_CODE (varloc) == EXPR_LIST)
14837 varloc = XEXP (varloc, 0);
14838 mode = GET_MODE (varloc);
14839 if (MEM_P (varloc))
14841 rtx addr = XEXP (varloc, 0);
14842 descr = mem_loc_descriptor (addr, mode, initialized);
14843 if (descr)
14844 have_address = 1;
14845 else
14847 rtx x = avoid_constant_pool_reference (varloc);
14848 if (x != varloc)
14849 descr = mem_loc_descriptor (x, mode, initialized);
14852 else
14853 descr = mem_loc_descriptor (varloc, mode, initialized);
14855 else
14856 return 0;
14858 else
14860 if (GET_CODE (varloc) == VAR_LOCATION)
14861 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14862 else
14863 mode = DECL_MODE (loc);
14864 descr = loc_descriptor (varloc, mode, initialized);
14865 have_address = 1;
14868 if (!descr)
14869 return 0;
14871 if (want_address == 2 && !have_address
14872 && (dwarf_version >= 4 || !dwarf_strict))
14874 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14876 expansion_failed (loc, NULL_RTX,
14877 "DWARF address size mismatch");
14878 return 0;
14880 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14881 have_address = 1;
14883 /* Show if we can't fill the request for an address. */
14884 if (want_address && !have_address)
14886 expansion_failed (loc, NULL_RTX,
14887 "Want address and only have value");
14888 return 0;
14891 /* If we've got an address and don't want one, dereference. */
14892 if (!want_address && have_address)
14894 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14895 enum dwarf_location_atom op;
14897 if (size > DWARF2_ADDR_SIZE || size == -1)
14899 expansion_failed (loc, NULL_RTX,
14900 "DWARF address size mismatch");
14901 return 0;
14903 else if (size == DWARF2_ADDR_SIZE)
14904 op = DW_OP_deref;
14905 else
14906 op = DW_OP_deref_size;
14908 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14911 return descr;
14914 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14915 if it is not possible. */
14917 static dw_loc_descr_ref
14918 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14920 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14921 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14922 else if (dwarf_version >= 3 || !dwarf_strict)
14923 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14924 else
14925 return NULL;
14928 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14929 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14931 static dw_loc_descr_ref
14932 dw_sra_loc_expr (tree decl, rtx loc)
14934 rtx p;
14935 unsigned int padsize = 0;
14936 dw_loc_descr_ref descr, *descr_tail;
14937 unsigned HOST_WIDE_INT decl_size;
14938 rtx varloc;
14939 enum var_init_status initialized;
14941 if (DECL_SIZE (decl) == NULL
14942 || !host_integerp (DECL_SIZE (decl), 1))
14943 return NULL;
14945 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14946 descr = NULL;
14947 descr_tail = &descr;
14949 for (p = loc; p; p = XEXP (p, 1))
14951 unsigned int bitsize = decl_piece_bitsize (p);
14952 rtx loc_note = *decl_piece_varloc_ptr (p);
14953 dw_loc_descr_ref cur_descr;
14954 dw_loc_descr_ref *tail, last = NULL;
14955 unsigned int opsize = 0;
14957 if (loc_note == NULL_RTX
14958 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14960 padsize += bitsize;
14961 continue;
14963 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14964 varloc = NOTE_VAR_LOCATION (loc_note);
14965 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14966 if (cur_descr == NULL)
14968 padsize += bitsize;
14969 continue;
14972 /* Check that cur_descr either doesn't use
14973 DW_OP_*piece operations, or their sum is equal
14974 to bitsize. Otherwise we can't embed it. */
14975 for (tail = &cur_descr; *tail != NULL;
14976 tail = &(*tail)->dw_loc_next)
14977 if ((*tail)->dw_loc_opc == DW_OP_piece)
14979 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14980 * BITS_PER_UNIT;
14981 last = *tail;
14983 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14985 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14986 last = *tail;
14989 if (last != NULL && opsize != bitsize)
14991 padsize += bitsize;
14992 continue;
14995 /* If there is a hole, add DW_OP_*piece after empty DWARF
14996 expression, which means that those bits are optimized out. */
14997 if (padsize)
14999 if (padsize > decl_size)
15000 return NULL;
15001 decl_size -= padsize;
15002 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15003 if (*descr_tail == NULL)
15004 return NULL;
15005 descr_tail = &(*descr_tail)->dw_loc_next;
15006 padsize = 0;
15008 *descr_tail = cur_descr;
15009 descr_tail = tail;
15010 if (bitsize > decl_size)
15011 return NULL;
15012 decl_size -= bitsize;
15013 if (last == NULL)
15015 HOST_WIDE_INT offset = 0;
15016 if (GET_CODE (varloc) == VAR_LOCATION
15017 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15019 varloc = PAT_VAR_LOCATION_LOC (varloc);
15020 if (GET_CODE (varloc) == EXPR_LIST)
15021 varloc = XEXP (varloc, 0);
15025 if (GET_CODE (varloc) == CONST
15026 || GET_CODE (varloc) == SIGN_EXTEND
15027 || GET_CODE (varloc) == ZERO_EXTEND)
15028 varloc = XEXP (varloc, 0);
15029 else if (GET_CODE (varloc) == SUBREG)
15030 varloc = SUBREG_REG (varloc);
15031 else
15032 break;
15034 while (1);
15035 /* DW_OP_bit_size offset should be zero for register
15036 or implicit location descriptions and empty location
15037 descriptions, but for memory addresses needs big endian
15038 adjustment. */
15039 if (MEM_P (varloc))
15041 unsigned HOST_WIDE_INT memsize
15042 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15043 if (memsize != bitsize)
15045 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15046 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15047 return NULL;
15048 if (memsize < bitsize)
15049 return NULL;
15050 if (BITS_BIG_ENDIAN)
15051 offset = memsize - bitsize;
15055 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15056 if (*descr_tail == NULL)
15057 return NULL;
15058 descr_tail = &(*descr_tail)->dw_loc_next;
15062 /* If there were any non-empty expressions, add padding till the end of
15063 the decl. */
15064 if (descr != NULL && decl_size != 0)
15066 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15067 if (*descr_tail == NULL)
15068 return NULL;
15070 return descr;
15073 /* Return the dwarf representation of the location list LOC_LIST of
15074 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
15075 function. */
15077 static dw_loc_list_ref
15078 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15080 const char *endname, *secname;
15081 rtx varloc;
15082 enum var_init_status initialized;
15083 struct var_loc_node *node;
15084 dw_loc_descr_ref descr;
15085 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15086 dw_loc_list_ref list = NULL;
15087 dw_loc_list_ref *listp = &list;
15089 /* Now that we know what section we are using for a base,
15090 actually construct the list of locations.
15091 The first location information is what is passed to the
15092 function that creates the location list, and the remaining
15093 locations just get added on to that list.
15094 Note that we only know the start address for a location
15095 (IE location changes), so to build the range, we use
15096 the range [current location start, next location start].
15097 This means we have to special case the last node, and generate
15098 a range of [last location start, end of function label]. */
15100 secname = secname_for_decl (decl);
15102 for (node = loc_list->first; node; node = node->next)
15103 if (GET_CODE (node->loc) == EXPR_LIST
15104 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15106 if (GET_CODE (node->loc) == EXPR_LIST)
15108 /* This requires DW_OP_{,bit_}piece, which is not usable
15109 inside DWARF expressions. */
15110 if (want_address != 2)
15111 continue;
15112 descr = dw_sra_loc_expr (decl, node->loc);
15113 if (descr == NULL)
15114 continue;
15116 else
15118 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15119 varloc = NOTE_VAR_LOCATION (node->loc);
15120 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15122 if (descr)
15124 /* The variable has a location between NODE->LABEL and
15125 NODE->NEXT->LABEL. */
15126 if (node->next)
15127 endname = node->next->label;
15128 /* If the variable has a location at the last label
15129 it keeps its location until the end of function. */
15130 else if (!current_function_decl)
15131 endname = text_end_label;
15132 else
15134 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15135 current_function_funcdef_no);
15136 endname = ggc_strdup (label_id);
15139 *listp = new_loc_list (descr, node->label, endname, secname);
15140 listp = &(*listp)->dw_loc_next;
15144 /* Try to avoid the overhead of a location list emitting a location
15145 expression instead, but only if we didn't have more than one
15146 location entry in the first place. If some entries were not
15147 representable, we don't want to pretend a single entry that was
15148 applies to the entire scope in which the variable is
15149 available. */
15150 if (list && loc_list->first->next)
15151 gen_llsym (list);
15153 return list;
15156 /* Return if the loc_list has only single element and thus can be represented
15157 as location description. */
15159 static bool
15160 single_element_loc_list_p (dw_loc_list_ref list)
15162 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15163 return !list->ll_symbol;
15166 /* To each location in list LIST add loc descr REF. */
15168 static void
15169 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15171 dw_loc_descr_ref copy;
15172 add_loc_descr (&list->expr, ref);
15173 list = list->dw_loc_next;
15174 while (list)
15176 copy = ggc_alloc_dw_loc_descr_node ();
15177 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15178 add_loc_descr (&list->expr, copy);
15179 while (copy->dw_loc_next)
15181 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15182 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15183 copy->dw_loc_next = new_copy;
15184 copy = new_copy;
15186 list = list->dw_loc_next;
15190 /* Given two lists RET and LIST
15191 produce location list that is result of adding expression in LIST
15192 to expression in RET on each possition in program.
15193 Might be destructive on both RET and LIST.
15195 TODO: We handle only simple cases of RET or LIST having at most one
15196 element. General case would inolve sorting the lists in program order
15197 and merging them that will need some additional work.
15198 Adding that will improve quality of debug info especially for SRA-ed
15199 structures. */
15201 static void
15202 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15204 if (!list)
15205 return;
15206 if (!*ret)
15208 *ret = list;
15209 return;
15211 if (!list->dw_loc_next)
15213 add_loc_descr_to_each (*ret, list->expr);
15214 return;
15216 if (!(*ret)->dw_loc_next)
15218 add_loc_descr_to_each (list, (*ret)->expr);
15219 *ret = list;
15220 return;
15222 expansion_failed (NULL_TREE, NULL_RTX,
15223 "Don't know how to merge two non-trivial"
15224 " location lists.\n");
15225 *ret = NULL;
15226 return;
15229 /* LOC is constant expression. Try a luck, look it up in constant
15230 pool and return its loc_descr of its address. */
15232 static dw_loc_descr_ref
15233 cst_pool_loc_descr (tree loc)
15235 /* Get an RTL for this, if something has been emitted. */
15236 rtx rtl = lookup_constant_def (loc);
15237 enum machine_mode mode;
15239 if (!rtl || !MEM_P (rtl))
15241 gcc_assert (!rtl);
15242 return 0;
15244 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15246 /* TODO: We might get more coverage if we was actually delaying expansion
15247 of all expressions till end of compilation when constant pools are fully
15248 populated. */
15249 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15251 expansion_failed (loc, NULL_RTX,
15252 "CST value in contant pool but not marked.");
15253 return 0;
15255 mode = GET_MODE (rtl);
15256 rtl = XEXP (rtl, 0);
15257 return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15260 /* Return dw_loc_list representing address of addr_expr LOC
15261 by looking for innder INDIRECT_REF expression and turing it
15262 into simple arithmetics. */
15264 static dw_loc_list_ref
15265 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15267 tree obj, offset;
15268 HOST_WIDE_INT bitsize, bitpos, bytepos;
15269 enum machine_mode mode;
15270 int volatilep;
15271 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15272 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15274 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15275 &bitsize, &bitpos, &offset, &mode,
15276 &unsignedp, &volatilep, false);
15277 STRIP_NOPS (obj);
15278 if (bitpos % BITS_PER_UNIT)
15280 expansion_failed (loc, NULL_RTX, "bitfield access");
15281 return 0;
15283 if (!INDIRECT_REF_P (obj))
15285 expansion_failed (obj,
15286 NULL_RTX, "no indirect ref in inner refrence");
15287 return 0;
15289 if (!offset && !bitpos)
15290 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15291 else if (toplev
15292 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15293 && (dwarf_version >= 4 || !dwarf_strict))
15295 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15296 if (!list_ret)
15297 return 0;
15298 if (offset)
15300 /* Variable offset. */
15301 list_ret1 = loc_list_from_tree (offset, 0);
15302 if (list_ret1 == 0)
15303 return 0;
15304 add_loc_list (&list_ret, list_ret1);
15305 if (!list_ret)
15306 return 0;
15307 add_loc_descr_to_each (list_ret,
15308 new_loc_descr (DW_OP_plus, 0, 0));
15310 bytepos = bitpos / BITS_PER_UNIT;
15311 if (bytepos > 0)
15312 add_loc_descr_to_each (list_ret,
15313 new_loc_descr (DW_OP_plus_uconst,
15314 bytepos, 0));
15315 else if (bytepos < 0)
15316 loc_list_plus_const (list_ret, bytepos);
15317 add_loc_descr_to_each (list_ret,
15318 new_loc_descr (DW_OP_stack_value, 0, 0));
15320 return list_ret;
15324 /* Generate Dwarf location list representing LOC.
15325 If WANT_ADDRESS is false, expression computing LOC will be computed
15326 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15327 if WANT_ADDRESS is 2, expression computing address useable in location
15328 will be returned (i.e. DW_OP_reg can be used
15329 to refer to register values). */
15331 static dw_loc_list_ref
15332 loc_list_from_tree (tree loc, int want_address)
15334 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15335 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15336 int have_address = 0;
15337 enum dwarf_location_atom op;
15339 /* ??? Most of the time we do not take proper care for sign/zero
15340 extending the values properly. Hopefully this won't be a real
15341 problem... */
15343 switch (TREE_CODE (loc))
15345 case ERROR_MARK:
15346 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15347 return 0;
15349 case PLACEHOLDER_EXPR:
15350 /* This case involves extracting fields from an object to determine the
15351 position of other fields. We don't try to encode this here. The
15352 only user of this is Ada, which encodes the needed information using
15353 the names of types. */
15354 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15355 return 0;
15357 case CALL_EXPR:
15358 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15359 /* There are no opcodes for these operations. */
15360 return 0;
15362 case PREINCREMENT_EXPR:
15363 case PREDECREMENT_EXPR:
15364 case POSTINCREMENT_EXPR:
15365 case POSTDECREMENT_EXPR:
15366 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15367 /* There are no opcodes for these operations. */
15368 return 0;
15370 case ADDR_EXPR:
15371 /* If we already want an address, see if there is INDIRECT_REF inside
15372 e.g. for &this->field. */
15373 if (want_address)
15375 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15376 (loc, want_address == 2);
15377 if (list_ret)
15378 have_address = 1;
15379 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15380 && (ret = cst_pool_loc_descr (loc)))
15381 have_address = 1;
15383 /* Otherwise, process the argument and look for the address. */
15384 if (!list_ret && !ret)
15385 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15386 else
15388 if (want_address)
15389 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15390 return NULL;
15392 break;
15394 case VAR_DECL:
15395 if (DECL_THREAD_LOCAL_P (loc))
15397 rtx rtl;
15398 enum dwarf_location_atom first_op;
15399 enum dwarf_location_atom second_op;
15400 bool dtprel = false;
15402 if (targetm.have_tls)
15404 /* If this is not defined, we have no way to emit the
15405 data. */
15406 if (!targetm.asm_out.output_dwarf_dtprel)
15407 return 0;
15409 /* The way DW_OP_GNU_push_tls_address is specified, we
15410 can only look up addresses of objects in the current
15411 module. We used DW_OP_addr as first op, but that's
15412 wrong, because DW_OP_addr is relocated by the debug
15413 info consumer, while DW_OP_GNU_push_tls_address
15414 operand shouldn't be. */
15415 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15416 return 0;
15417 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15418 dtprel = true;
15419 second_op = DW_OP_GNU_push_tls_address;
15421 else
15423 if (!targetm.emutls.debug_form_tls_address
15424 || !(dwarf_version >= 3 || !dwarf_strict))
15425 return 0;
15426 /* We stuffed the control variable into the DECL_VALUE_EXPR
15427 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15428 no longer appear in gimple code. We used the control
15429 variable in specific so that we could pick it up here. */
15430 loc = DECL_VALUE_EXPR (loc);
15431 first_op = DW_OP_addr;
15432 second_op = DW_OP_form_tls_address;
15435 rtl = rtl_for_decl_location (loc);
15436 if (rtl == NULL_RTX)
15437 return 0;
15439 if (!MEM_P (rtl))
15440 return 0;
15441 rtl = XEXP (rtl, 0);
15442 if (! CONSTANT_P (rtl))
15443 return 0;
15445 ret = new_loc_descr (first_op, 0, 0);
15446 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15447 ret->dw_loc_oprnd1.v.val_addr = rtl;
15448 ret->dtprel = dtprel;
15450 ret1 = new_loc_descr (second_op, 0, 0);
15451 add_loc_descr (&ret, ret1);
15453 have_address = 1;
15454 break;
15456 /* FALLTHRU */
15458 case PARM_DECL:
15459 case RESULT_DECL:
15460 if (DECL_HAS_VALUE_EXPR_P (loc))
15461 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15462 want_address);
15463 /* FALLTHRU */
15465 case FUNCTION_DECL:
15467 rtx rtl;
15468 var_loc_list *loc_list = lookup_decl_loc (loc);
15470 if (loc_list && loc_list->first)
15472 list_ret = dw_loc_list (loc_list, loc, want_address);
15473 have_address = want_address != 0;
15474 break;
15476 rtl = rtl_for_decl_location (loc);
15477 if (rtl == NULL_RTX)
15479 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15480 return 0;
15482 else if (CONST_INT_P (rtl))
15484 HOST_WIDE_INT val = INTVAL (rtl);
15485 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15486 val &= GET_MODE_MASK (DECL_MODE (loc));
15487 ret = int_loc_descriptor (val);
15489 else if (GET_CODE (rtl) == CONST_STRING)
15491 expansion_failed (loc, NULL_RTX, "CONST_STRING");
15492 return 0;
15494 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15496 ret = new_loc_descr (DW_OP_addr, 0, 0);
15497 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15498 ret->dw_loc_oprnd1.v.val_addr = rtl;
15500 else
15502 enum machine_mode mode;
15504 /* Certain constructs can only be represented at top-level. */
15505 if (want_address == 2)
15507 ret = loc_descriptor (rtl, VOIDmode,
15508 VAR_INIT_STATUS_INITIALIZED);
15509 have_address = 1;
15511 else
15513 mode = GET_MODE (rtl);
15514 if (MEM_P (rtl))
15516 rtl = XEXP (rtl, 0);
15517 have_address = 1;
15519 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15521 if (!ret)
15522 expansion_failed (loc, rtl,
15523 "failed to produce loc descriptor for rtl");
15526 break;
15528 case MEM_REF:
15529 /* ??? FIXME. */
15530 if (!integer_zerop (TREE_OPERAND (loc, 1)))
15531 return 0;
15532 /* Fallthru. */
15533 case INDIRECT_REF:
15534 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15535 have_address = 1;
15536 break;
15538 case COMPOUND_EXPR:
15539 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15541 CASE_CONVERT:
15542 case VIEW_CONVERT_EXPR:
15543 case SAVE_EXPR:
15544 case MODIFY_EXPR:
15545 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15547 case COMPONENT_REF:
15548 case BIT_FIELD_REF:
15549 case ARRAY_REF:
15550 case ARRAY_RANGE_REF:
15551 case REALPART_EXPR:
15552 case IMAGPART_EXPR:
15554 tree obj, offset;
15555 HOST_WIDE_INT bitsize, bitpos, bytepos;
15556 enum machine_mode mode;
15557 int volatilep;
15558 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15560 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15561 &unsignedp, &volatilep, false);
15563 gcc_assert (obj != loc);
15565 list_ret = loc_list_from_tree (obj,
15566 want_address == 2
15567 && !bitpos && !offset ? 2 : 1);
15568 /* TODO: We can extract value of the small expression via shifting even
15569 for nonzero bitpos. */
15570 if (list_ret == 0)
15571 return 0;
15572 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15574 expansion_failed (loc, NULL_RTX,
15575 "bitfield access");
15576 return 0;
15579 if (offset != NULL_TREE)
15581 /* Variable offset. */
15582 list_ret1 = loc_list_from_tree (offset, 0);
15583 if (list_ret1 == 0)
15584 return 0;
15585 add_loc_list (&list_ret, list_ret1);
15586 if (!list_ret)
15587 return 0;
15588 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15591 bytepos = bitpos / BITS_PER_UNIT;
15592 if (bytepos > 0)
15593 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15594 else if (bytepos < 0)
15595 loc_list_plus_const (list_ret, bytepos);
15597 have_address = 1;
15598 break;
15601 case INTEGER_CST:
15602 if ((want_address || !host_integerp (loc, 0))
15603 && (ret = cst_pool_loc_descr (loc)))
15604 have_address = 1;
15605 else if (want_address == 2
15606 && host_integerp (loc, 0)
15607 && (ret = address_of_int_loc_descriptor
15608 (int_size_in_bytes (TREE_TYPE (loc)),
15609 tree_low_cst (loc, 0))))
15610 have_address = 1;
15611 else if (host_integerp (loc, 0))
15612 ret = int_loc_descriptor (tree_low_cst (loc, 0));
15613 else
15615 expansion_failed (loc, NULL_RTX,
15616 "Integer operand is not host integer");
15617 return 0;
15619 break;
15621 case CONSTRUCTOR:
15622 case REAL_CST:
15623 case STRING_CST:
15624 case COMPLEX_CST:
15625 if ((ret = cst_pool_loc_descr (loc)))
15626 have_address = 1;
15627 else
15628 /* We can construct small constants here using int_loc_descriptor. */
15629 expansion_failed (loc, NULL_RTX,
15630 "constructor or constant not in constant pool");
15631 break;
15633 case TRUTH_AND_EXPR:
15634 case TRUTH_ANDIF_EXPR:
15635 case BIT_AND_EXPR:
15636 op = DW_OP_and;
15637 goto do_binop;
15639 case TRUTH_XOR_EXPR:
15640 case BIT_XOR_EXPR:
15641 op = DW_OP_xor;
15642 goto do_binop;
15644 case TRUTH_OR_EXPR:
15645 case TRUTH_ORIF_EXPR:
15646 case BIT_IOR_EXPR:
15647 op = DW_OP_or;
15648 goto do_binop;
15650 case FLOOR_DIV_EXPR:
15651 case CEIL_DIV_EXPR:
15652 case ROUND_DIV_EXPR:
15653 case TRUNC_DIV_EXPR:
15654 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15655 return 0;
15656 op = DW_OP_div;
15657 goto do_binop;
15659 case MINUS_EXPR:
15660 op = DW_OP_minus;
15661 goto do_binop;
15663 case FLOOR_MOD_EXPR:
15664 case CEIL_MOD_EXPR:
15665 case ROUND_MOD_EXPR:
15666 case TRUNC_MOD_EXPR:
15667 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15669 op = DW_OP_mod;
15670 goto do_binop;
15672 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15673 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15674 if (list_ret == 0 || list_ret1 == 0)
15675 return 0;
15677 add_loc_list (&list_ret, list_ret1);
15678 if (list_ret == 0)
15679 return 0;
15680 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15681 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15682 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15683 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15684 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15685 break;
15687 case MULT_EXPR:
15688 op = DW_OP_mul;
15689 goto do_binop;
15691 case LSHIFT_EXPR:
15692 op = DW_OP_shl;
15693 goto do_binop;
15695 case RSHIFT_EXPR:
15696 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15697 goto do_binop;
15699 case POINTER_PLUS_EXPR:
15700 case PLUS_EXPR:
15701 if (host_integerp (TREE_OPERAND (loc, 1), 0))
15703 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15704 if (list_ret == 0)
15705 return 0;
15707 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15708 break;
15711 op = DW_OP_plus;
15712 goto do_binop;
15714 case LE_EXPR:
15715 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15716 return 0;
15718 op = DW_OP_le;
15719 goto do_binop;
15721 case GE_EXPR:
15722 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15723 return 0;
15725 op = DW_OP_ge;
15726 goto do_binop;
15728 case LT_EXPR:
15729 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15730 return 0;
15732 op = DW_OP_lt;
15733 goto do_binop;
15735 case GT_EXPR:
15736 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15737 return 0;
15739 op = DW_OP_gt;
15740 goto do_binop;
15742 case EQ_EXPR:
15743 op = DW_OP_eq;
15744 goto do_binop;
15746 case NE_EXPR:
15747 op = DW_OP_ne;
15748 goto do_binop;
15750 do_binop:
15751 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15752 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15753 if (list_ret == 0 || list_ret1 == 0)
15754 return 0;
15756 add_loc_list (&list_ret, list_ret1);
15757 if (list_ret == 0)
15758 return 0;
15759 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15760 break;
15762 case TRUTH_NOT_EXPR:
15763 case BIT_NOT_EXPR:
15764 op = DW_OP_not;
15765 goto do_unop;
15767 case ABS_EXPR:
15768 op = DW_OP_abs;
15769 goto do_unop;
15771 case NEGATE_EXPR:
15772 op = DW_OP_neg;
15773 goto do_unop;
15775 do_unop:
15776 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15777 if (list_ret == 0)
15778 return 0;
15780 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15781 break;
15783 case MIN_EXPR:
15784 case MAX_EXPR:
15786 const enum tree_code code =
15787 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15789 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15790 build2 (code, integer_type_node,
15791 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15792 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15795 /* ... fall through ... */
15797 case COND_EXPR:
15799 dw_loc_descr_ref lhs
15800 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15801 dw_loc_list_ref rhs
15802 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15803 dw_loc_descr_ref bra_node, jump_node, tmp;
15805 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15806 if (list_ret == 0 || lhs == 0 || rhs == 0)
15807 return 0;
15809 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15810 add_loc_descr_to_each (list_ret, bra_node);
15812 add_loc_list (&list_ret, rhs);
15813 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15814 add_loc_descr_to_each (list_ret, jump_node);
15816 add_loc_descr_to_each (list_ret, lhs);
15817 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15818 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15820 /* ??? Need a node to point the skip at. Use a nop. */
15821 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15822 add_loc_descr_to_each (list_ret, tmp);
15823 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15824 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15826 break;
15828 case FIX_TRUNC_EXPR:
15829 return 0;
15831 default:
15832 /* Leave front-end specific codes as simply unknown. This comes
15833 up, for instance, with the C STMT_EXPR. */
15834 if ((unsigned int) TREE_CODE (loc)
15835 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15837 expansion_failed (loc, NULL_RTX,
15838 "language specific tree node");
15839 return 0;
15842 #ifdef ENABLE_CHECKING
15843 /* Otherwise this is a generic code; we should just lists all of
15844 these explicitly. We forgot one. */
15845 gcc_unreachable ();
15846 #else
15847 /* In a release build, we want to degrade gracefully: better to
15848 generate incomplete debugging information than to crash. */
15849 return NULL;
15850 #endif
15853 if (!ret && !list_ret)
15854 return 0;
15856 if (want_address == 2 && !have_address
15857 && (dwarf_version >= 4 || !dwarf_strict))
15859 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15861 expansion_failed (loc, NULL_RTX,
15862 "DWARF address size mismatch");
15863 return 0;
15865 if (ret)
15866 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15867 else
15868 add_loc_descr_to_each (list_ret,
15869 new_loc_descr (DW_OP_stack_value, 0, 0));
15870 have_address = 1;
15872 /* Show if we can't fill the request for an address. */
15873 if (want_address && !have_address)
15875 expansion_failed (loc, NULL_RTX,
15876 "Want address and only have value");
15877 return 0;
15880 gcc_assert (!ret || !list_ret);
15882 /* If we've got an address and don't want one, dereference. */
15883 if (!want_address && have_address)
15885 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15887 if (size > DWARF2_ADDR_SIZE || size == -1)
15889 expansion_failed (loc, NULL_RTX,
15890 "DWARF address size mismatch");
15891 return 0;
15893 else if (size == DWARF2_ADDR_SIZE)
15894 op = DW_OP_deref;
15895 else
15896 op = DW_OP_deref_size;
15898 if (ret)
15899 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15900 else
15901 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15903 if (ret)
15904 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15906 return list_ret;
15909 /* Same as above but return only single location expression. */
15910 static dw_loc_descr_ref
15911 loc_descriptor_from_tree (tree loc, int want_address)
15913 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15914 if (!ret)
15915 return NULL;
15916 if (ret->dw_loc_next)
15918 expansion_failed (loc, NULL_RTX,
15919 "Location list where only loc descriptor needed");
15920 return NULL;
15922 return ret->expr;
15925 /* Given a value, round it up to the lowest multiple of `boundary'
15926 which is not less than the value itself. */
15928 static inline HOST_WIDE_INT
15929 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15931 return (((value + boundary - 1) / boundary) * boundary);
15934 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15935 pointer to the declared type for the relevant field variable, or return
15936 `integer_type_node' if the given node turns out to be an
15937 ERROR_MARK node. */
15939 static inline tree
15940 field_type (const_tree decl)
15942 tree type;
15944 if (TREE_CODE (decl) == ERROR_MARK)
15945 return integer_type_node;
15947 type = DECL_BIT_FIELD_TYPE (decl);
15948 if (type == NULL_TREE)
15949 type = TREE_TYPE (decl);
15951 return type;
15954 /* Given a pointer to a tree node, return the alignment in bits for
15955 it, or else return BITS_PER_WORD if the node actually turns out to
15956 be an ERROR_MARK node. */
15958 static inline unsigned
15959 simple_type_align_in_bits (const_tree type)
15961 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15964 static inline unsigned
15965 simple_decl_align_in_bits (const_tree decl)
15967 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15970 /* Return the result of rounding T up to ALIGN. */
15972 static inline double_int
15973 round_up_to_align (double_int t, unsigned int align)
15975 double_int alignd = uhwi_to_double_int (align);
15976 t = double_int_add (t, alignd);
15977 t = double_int_add (t, double_int_minus_one);
15978 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15979 t = double_int_mul (t, alignd);
15980 return t;
15983 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15984 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15985 or return 0 if we are unable to determine what that offset is, either
15986 because the argument turns out to be a pointer to an ERROR_MARK node, or
15987 because the offset is actually variable. (We can't handle the latter case
15988 just yet). */
15990 static HOST_WIDE_INT
15991 field_byte_offset (const_tree decl)
15993 double_int object_offset_in_bits;
15994 double_int object_offset_in_bytes;
15995 double_int bitpos_int;
15997 if (TREE_CODE (decl) == ERROR_MARK)
15998 return 0;
16000 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16002 /* We cannot yet cope with fields whose positions are variable, so
16003 for now, when we see such things, we simply return 0. Someday, we may
16004 be able to handle such cases, but it will be damn difficult. */
16005 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16006 return 0;
16008 bitpos_int = tree_to_double_int (bit_position (decl));
16010 #ifdef PCC_BITFIELD_TYPE_MATTERS
16011 if (PCC_BITFIELD_TYPE_MATTERS)
16013 tree type;
16014 tree field_size_tree;
16015 double_int deepest_bitpos;
16016 double_int field_size_in_bits;
16017 unsigned int type_align_in_bits;
16018 unsigned int decl_align_in_bits;
16019 double_int type_size_in_bits;
16021 type = field_type (decl);
16022 type_size_in_bits = double_int_type_size_in_bits (type);
16023 type_align_in_bits = simple_type_align_in_bits (type);
16025 field_size_tree = DECL_SIZE (decl);
16027 /* The size could be unspecified if there was an error, or for
16028 a flexible array member. */
16029 if (!field_size_tree)
16030 field_size_tree = bitsize_zero_node;
16032 /* If the size of the field is not constant, use the type size. */
16033 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16034 field_size_in_bits = tree_to_double_int (field_size_tree);
16035 else
16036 field_size_in_bits = type_size_in_bits;
16038 decl_align_in_bits = simple_decl_align_in_bits (decl);
16040 /* The GCC front-end doesn't make any attempt to keep track of the
16041 starting bit offset (relative to the start of the containing
16042 structure type) of the hypothetical "containing object" for a
16043 bit-field. Thus, when computing the byte offset value for the
16044 start of the "containing object" of a bit-field, we must deduce
16045 this information on our own. This can be rather tricky to do in
16046 some cases. For example, handling the following structure type
16047 definition when compiling for an i386/i486 target (which only
16048 aligns long long's to 32-bit boundaries) can be very tricky:
16050 struct S { int field1; long long field2:31; };
16052 Fortunately, there is a simple rule-of-thumb which can be used
16053 in such cases. When compiling for an i386/i486, GCC will
16054 allocate 8 bytes for the structure shown above. It decides to
16055 do this based upon one simple rule for bit-field allocation.
16056 GCC allocates each "containing object" for each bit-field at
16057 the first (i.e. lowest addressed) legitimate alignment boundary
16058 (based upon the required minimum alignment for the declared
16059 type of the field) which it can possibly use, subject to the
16060 condition that there is still enough available space remaining
16061 in the containing object (when allocated at the selected point)
16062 to fully accommodate all of the bits of the bit-field itself.
16064 This simple rule makes it obvious why GCC allocates 8 bytes for
16065 each object of the structure type shown above. When looking
16066 for a place to allocate the "containing object" for `field2',
16067 the compiler simply tries to allocate a 64-bit "containing
16068 object" at each successive 32-bit boundary (starting at zero)
16069 until it finds a place to allocate that 64- bit field such that
16070 at least 31 contiguous (and previously unallocated) bits remain
16071 within that selected 64 bit field. (As it turns out, for the
16072 example above, the compiler finds it is OK to allocate the
16073 "containing object" 64-bit field at bit-offset zero within the
16074 structure type.)
16076 Here we attempt to work backwards from the limited set of facts
16077 we're given, and we try to deduce from those facts, where GCC
16078 must have believed that the containing object started (within
16079 the structure type). The value we deduce is then used (by the
16080 callers of this routine) to generate DW_AT_location and
16081 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16082 the case of DW_AT_location, regular fields as well). */
16084 /* Figure out the bit-distance from the start of the structure to
16085 the "deepest" bit of the bit-field. */
16086 deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16088 /* This is the tricky part. Use some fancy footwork to deduce
16089 where the lowest addressed bit of the containing object must
16090 be. */
16091 object_offset_in_bits
16092 = double_int_sub (deepest_bitpos, type_size_in_bits);
16094 /* Round up to type_align by default. This works best for
16095 bitfields. */
16096 object_offset_in_bits
16097 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16099 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16101 object_offset_in_bits
16102 = double_int_sub (deepest_bitpos, type_size_in_bits);
16104 /* Round up to decl_align instead. */
16105 object_offset_in_bits
16106 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16109 else
16110 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16111 object_offset_in_bits = bitpos_int;
16113 object_offset_in_bytes
16114 = double_int_div (object_offset_in_bits,
16115 uhwi_to_double_int (BITS_PER_UNIT), true,
16116 TRUNC_DIV_EXPR);
16117 return double_int_to_shwi (object_offset_in_bytes);
16120 /* The following routines define various Dwarf attributes and any data
16121 associated with them. */
16123 /* Add a location description attribute value to a DIE.
16125 This emits location attributes suitable for whole variables and
16126 whole parameters. Note that the location attributes for struct fields are
16127 generated by the routine `data_member_location_attribute' below. */
16129 static inline void
16130 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16131 dw_loc_list_ref descr)
16133 if (descr == 0)
16134 return;
16135 if (single_element_loc_list_p (descr))
16136 add_AT_loc (die, attr_kind, descr->expr);
16137 else
16138 add_AT_loc_list (die, attr_kind, descr);
16141 /* Add DW_AT_accessibility attribute to DIE if needed. */
16143 static void
16144 add_accessibility_attribute (dw_die_ref die, tree decl)
16146 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16147 children, otherwise the default is DW_ACCESS_public. In DWARF2
16148 the default has always been DW_ACCESS_public. */
16149 if (TREE_PROTECTED (decl))
16150 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16151 else if (TREE_PRIVATE (decl))
16153 if (dwarf_version == 2
16154 || die->die_parent == NULL
16155 || die->die_parent->die_tag != DW_TAG_class_type)
16156 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16158 else if (dwarf_version > 2
16159 && die->die_parent
16160 && die->die_parent->die_tag == DW_TAG_class_type)
16161 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16164 /* Attach the specialized form of location attribute used for data members of
16165 struct and union types. In the special case of a FIELD_DECL node which
16166 represents a bit-field, the "offset" part of this special location
16167 descriptor must indicate the distance in bytes from the lowest-addressed
16168 byte of the containing struct or union type to the lowest-addressed byte of
16169 the "containing object" for the bit-field. (See the `field_byte_offset'
16170 function above).
16172 For any given bit-field, the "containing object" is a hypothetical object
16173 (of some integral or enum type) within which the given bit-field lives. The
16174 type of this hypothetical "containing object" is always the same as the
16175 declared type of the individual bit-field itself (for GCC anyway... the
16176 DWARF spec doesn't actually mandate this). Note that it is the size (in
16177 bytes) of the hypothetical "containing object" which will be given in the
16178 DW_AT_byte_size attribute for this bit-field. (See the
16179 `byte_size_attribute' function below.) It is also used when calculating the
16180 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16181 function below.) */
16183 static void
16184 add_data_member_location_attribute (dw_die_ref die, tree decl)
16186 HOST_WIDE_INT offset;
16187 dw_loc_descr_ref loc_descr = 0;
16189 if (TREE_CODE (decl) == TREE_BINFO)
16191 /* We're working on the TAG_inheritance for a base class. */
16192 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16194 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16195 aren't at a fixed offset from all (sub)objects of the same
16196 type. We need to extract the appropriate offset from our
16197 vtable. The following dwarf expression means
16199 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16201 This is specific to the V3 ABI, of course. */
16203 dw_loc_descr_ref tmp;
16205 /* Make a copy of the object address. */
16206 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16207 add_loc_descr (&loc_descr, tmp);
16209 /* Extract the vtable address. */
16210 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16211 add_loc_descr (&loc_descr, tmp);
16213 /* Calculate the address of the offset. */
16214 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16215 gcc_assert (offset < 0);
16217 tmp = int_loc_descriptor (-offset);
16218 add_loc_descr (&loc_descr, tmp);
16219 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16220 add_loc_descr (&loc_descr, tmp);
16222 /* Extract the offset. */
16223 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16224 add_loc_descr (&loc_descr, tmp);
16226 /* Add it to the object address. */
16227 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16228 add_loc_descr (&loc_descr, tmp);
16230 else
16231 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16233 else
16234 offset = field_byte_offset (decl);
16236 if (! loc_descr)
16238 if (dwarf_version > 2)
16240 /* Don't need to output a location expression, just the constant. */
16241 if (offset < 0)
16242 add_AT_int (die, DW_AT_data_member_location, offset);
16243 else
16244 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16245 return;
16247 else
16249 enum dwarf_location_atom op;
16251 /* The DWARF2 standard says that we should assume that the structure
16252 address is already on the stack, so we can specify a structure
16253 field address by using DW_OP_plus_uconst. */
16255 #ifdef MIPS_DEBUGGING_INFO
16256 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16257 operator correctly. It works only if we leave the offset on the
16258 stack. */
16259 op = DW_OP_constu;
16260 #else
16261 op = DW_OP_plus_uconst;
16262 #endif
16264 loc_descr = new_loc_descr (op, offset, 0);
16268 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16271 /* Writes integer values to dw_vec_const array. */
16273 static void
16274 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16276 while (size != 0)
16278 *dest++ = val & 0xff;
16279 val >>= 8;
16280 --size;
16284 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
16286 static HOST_WIDE_INT
16287 extract_int (const unsigned char *src, unsigned int size)
16289 HOST_WIDE_INT val = 0;
16291 src += size;
16292 while (size != 0)
16294 val <<= 8;
16295 val |= *--src & 0xff;
16296 --size;
16298 return val;
16301 /* Writes double_int values to dw_vec_const array. */
16303 static void
16304 insert_double (double_int val, unsigned char *dest)
16306 unsigned char *p0 = dest;
16307 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16309 if (WORDS_BIG_ENDIAN)
16311 p0 = p1;
16312 p1 = dest;
16315 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16316 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16319 /* Writes floating point values to dw_vec_const array. */
16321 static void
16322 insert_float (const_rtx rtl, unsigned char *array)
16324 REAL_VALUE_TYPE rv;
16325 long val[4];
16326 int i;
16328 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16329 real_to_target (val, &rv, GET_MODE (rtl));
16331 /* real_to_target puts 32-bit pieces in each long. Pack them. */
16332 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16334 insert_int (val[i], 4, array);
16335 array += 4;
16339 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16340 does not have a "location" either in memory or in a register. These
16341 things can arise in GNU C when a constant is passed as an actual parameter
16342 to an inlined function. They can also arise in C++ where declared
16343 constants do not necessarily get memory "homes". */
16345 static bool
16346 add_const_value_attribute (dw_die_ref die, rtx rtl)
16348 switch (GET_CODE (rtl))
16350 case CONST_INT:
16352 HOST_WIDE_INT val = INTVAL (rtl);
16354 if (val < 0)
16355 add_AT_int (die, DW_AT_const_value, val);
16356 else
16357 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16359 return true;
16361 case CONST_DOUBLE:
16362 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16363 floating-point constant. A CONST_DOUBLE is used whenever the
16364 constant requires more than one word in order to be adequately
16365 represented. */
16367 enum machine_mode mode = GET_MODE (rtl);
16369 if (SCALAR_FLOAT_MODE_P (mode))
16371 unsigned int length = GET_MODE_SIZE (mode);
16372 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16374 insert_float (rtl, array);
16375 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16377 else
16378 add_AT_double (die, DW_AT_const_value,
16379 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16381 return true;
16383 case CONST_VECTOR:
16385 enum machine_mode mode = GET_MODE (rtl);
16386 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16387 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16388 unsigned char *array = (unsigned char *) ggc_alloc_atomic
16389 (length * elt_size);
16390 unsigned int i;
16391 unsigned char *p;
16393 switch (GET_MODE_CLASS (mode))
16395 case MODE_VECTOR_INT:
16396 for (i = 0, p = array; i < length; i++, p += elt_size)
16398 rtx elt = CONST_VECTOR_ELT (rtl, i);
16399 double_int val = rtx_to_double_int (elt);
16401 if (elt_size <= sizeof (HOST_WIDE_INT))
16402 insert_int (double_int_to_shwi (val), elt_size, p);
16403 else
16405 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16406 insert_double (val, p);
16409 break;
16411 case MODE_VECTOR_FLOAT:
16412 for (i = 0, p = array; i < length; i++, p += elt_size)
16414 rtx elt = CONST_VECTOR_ELT (rtl, i);
16415 insert_float (elt, p);
16417 break;
16419 default:
16420 gcc_unreachable ();
16423 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16425 return true;
16427 case CONST_STRING:
16428 if (dwarf_version >= 4 || !dwarf_strict)
16430 dw_loc_descr_ref loc_result;
16431 resolve_one_addr (&rtl, NULL);
16432 rtl_addr:
16433 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16434 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16435 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16436 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16437 add_AT_loc (die, DW_AT_location, loc_result);
16438 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16439 return true;
16441 return false;
16443 case CONST:
16444 if (CONSTANT_P (XEXP (rtl, 0)))
16445 return add_const_value_attribute (die, XEXP (rtl, 0));
16446 /* FALLTHROUGH */
16447 case SYMBOL_REF:
16448 if (!const_ok_for_output (rtl))
16449 return false;
16450 case LABEL_REF:
16451 if (dwarf_version >= 4 || !dwarf_strict)
16452 goto rtl_addr;
16453 return false;
16455 case PLUS:
16456 /* In cases where an inlined instance of an inline function is passed
16457 the address of an `auto' variable (which is local to the caller) we
16458 can get a situation where the DECL_RTL of the artificial local
16459 variable (for the inlining) which acts as a stand-in for the
16460 corresponding formal parameter (of the inline function) will look
16461 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
16462 exactly a compile-time constant expression, but it isn't the address
16463 of the (artificial) local variable either. Rather, it represents the
16464 *value* which the artificial local variable always has during its
16465 lifetime. We currently have no way to represent such quasi-constant
16466 values in Dwarf, so for now we just punt and generate nothing. */
16467 return false;
16469 case HIGH:
16470 case CONST_FIXED:
16471 return false;
16473 case MEM:
16474 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16475 && MEM_READONLY_P (rtl)
16476 && GET_MODE (rtl) == BLKmode)
16478 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16479 return true;
16481 return false;
16483 default:
16484 /* No other kinds of rtx should be possible here. */
16485 gcc_unreachable ();
16487 return false;
16490 /* Determine whether the evaluation of EXPR references any variables
16491 or functions which aren't otherwise used (and therefore may not be
16492 output). */
16493 static tree
16494 reference_to_unused (tree * tp, int * walk_subtrees,
16495 void * data ATTRIBUTE_UNUSED)
16497 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16498 *walk_subtrees = 0;
16500 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16501 && ! TREE_ASM_WRITTEN (*tp))
16502 return *tp;
16503 /* ??? The C++ FE emits debug information for using decls, so
16504 putting gcc_unreachable here falls over. See PR31899. For now
16505 be conservative. */
16506 else if (!cgraph_global_info_ready
16507 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16508 return *tp;
16509 else if (TREE_CODE (*tp) == VAR_DECL)
16511 struct varpool_node *node = varpool_get_node (*tp);
16512 if (!node || !node->needed)
16513 return *tp;
16515 else if (TREE_CODE (*tp) == FUNCTION_DECL
16516 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16518 /* The call graph machinery must have finished analyzing,
16519 optimizing and gimplifying the CU by now.
16520 So if *TP has no call graph node associated
16521 to it, it means *TP will not be emitted. */
16522 if (!cgraph_get_node (*tp))
16523 return *tp;
16525 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16526 return *tp;
16528 return NULL_TREE;
16531 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16532 for use in a later add_const_value_attribute call. */
16534 static rtx
16535 rtl_for_decl_init (tree init, tree type)
16537 rtx rtl = NULL_RTX;
16539 STRIP_NOPS (init);
16541 /* If a variable is initialized with a string constant without embedded
16542 zeros, build CONST_STRING. */
16543 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16545 tree enttype = TREE_TYPE (type);
16546 tree domain = TYPE_DOMAIN (type);
16547 enum machine_mode mode = TYPE_MODE (enttype);
16549 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16550 && domain
16551 && integer_zerop (TYPE_MIN_VALUE (domain))
16552 && compare_tree_int (TYPE_MAX_VALUE (domain),
16553 TREE_STRING_LENGTH (init) - 1) == 0
16554 && ((size_t) TREE_STRING_LENGTH (init)
16555 == strlen (TREE_STRING_POINTER (init)) + 1))
16557 rtl = gen_rtx_CONST_STRING (VOIDmode,
16558 ggc_strdup (TREE_STRING_POINTER (init)));
16559 rtl = gen_rtx_MEM (BLKmode, rtl);
16560 MEM_READONLY_P (rtl) = 1;
16563 /* Other aggregates, and complex values, could be represented using
16564 CONCAT: FIXME! */
16565 else if (AGGREGATE_TYPE_P (type)
16566 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16567 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16568 || TREE_CODE (type) == COMPLEX_TYPE)
16570 /* Vectors only work if their mode is supported by the target.
16571 FIXME: generic vectors ought to work too. */
16572 else if (TREE_CODE (type) == VECTOR_TYPE
16573 && !VECTOR_MODE_P (TYPE_MODE (type)))
16575 /* If the initializer is something that we know will expand into an
16576 immediate RTL constant, expand it now. We must be careful not to
16577 reference variables which won't be output. */
16578 else if (initializer_constant_valid_p (init, type)
16579 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16581 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16582 possible. */
16583 if (TREE_CODE (type) == VECTOR_TYPE)
16584 switch (TREE_CODE (init))
16586 case VECTOR_CST:
16587 break;
16588 case CONSTRUCTOR:
16589 if (TREE_CONSTANT (init))
16591 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16592 bool constant_p = true;
16593 tree value;
16594 unsigned HOST_WIDE_INT ix;
16596 /* Even when ctor is constant, it might contain non-*_CST
16597 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16598 belong into VECTOR_CST nodes. */
16599 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16600 if (!CONSTANT_CLASS_P (value))
16602 constant_p = false;
16603 break;
16606 if (constant_p)
16608 init = build_vector_from_ctor (type, elts);
16609 break;
16612 /* FALLTHRU */
16614 default:
16615 return NULL;
16618 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16620 /* If expand_expr returns a MEM, it wasn't immediate. */
16621 gcc_assert (!rtl || !MEM_P (rtl));
16624 return rtl;
16627 /* Generate RTL for the variable DECL to represent its location. */
16629 static rtx
16630 rtl_for_decl_location (tree decl)
16632 rtx rtl;
16634 /* Here we have to decide where we are going to say the parameter "lives"
16635 (as far as the debugger is concerned). We only have a couple of
16636 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16638 DECL_RTL normally indicates where the parameter lives during most of the
16639 activation of the function. If optimization is enabled however, this
16640 could be either NULL or else a pseudo-reg. Both of those cases indicate
16641 that the parameter doesn't really live anywhere (as far as the code
16642 generation parts of GCC are concerned) during most of the function's
16643 activation. That will happen (for example) if the parameter is never
16644 referenced within the function.
16646 We could just generate a location descriptor here for all non-NULL
16647 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16648 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16649 where DECL_RTL is NULL or is a pseudo-reg.
16651 Note however that we can only get away with using DECL_INCOMING_RTL as
16652 a backup substitute for DECL_RTL in certain limited cases. In cases
16653 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16654 we can be sure that the parameter was passed using the same type as it is
16655 declared to have within the function, and that its DECL_INCOMING_RTL
16656 points us to a place where a value of that type is passed.
16658 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16659 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16660 because in these cases DECL_INCOMING_RTL points us to a value of some
16661 type which is *different* from the type of the parameter itself. Thus,
16662 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16663 such cases, the debugger would end up (for example) trying to fetch a
16664 `float' from a place which actually contains the first part of a
16665 `double'. That would lead to really incorrect and confusing
16666 output at debug-time.
16668 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16669 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
16670 are a couple of exceptions however. On little-endian machines we can
16671 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16672 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16673 an integral type that is smaller than TREE_TYPE (decl). These cases arise
16674 when (on a little-endian machine) a non-prototyped function has a
16675 parameter declared to be of type `short' or `char'. In such cases,
16676 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16677 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16678 passed `int' value. If the debugger then uses that address to fetch
16679 a `short' or a `char' (on a little-endian machine) the result will be
16680 the correct data, so we allow for such exceptional cases below.
16682 Note that our goal here is to describe the place where the given formal
16683 parameter lives during most of the function's activation (i.e. between the
16684 end of the prologue and the start of the epilogue). We'll do that as best
16685 as we can. Note however that if the given formal parameter is modified
16686 sometime during the execution of the function, then a stack backtrace (at
16687 debug-time) will show the function as having been called with the *new*
16688 value rather than the value which was originally passed in. This happens
16689 rarely enough that it is not a major problem, but it *is* a problem, and
16690 I'd like to fix it.
16692 A future version of dwarf2out.c may generate two additional attributes for
16693 any given DW_TAG_formal_parameter DIE which will describe the "passed
16694 type" and the "passed location" for the given formal parameter in addition
16695 to the attributes we now generate to indicate the "declared type" and the
16696 "active location" for each parameter. This additional set of attributes
16697 could be used by debuggers for stack backtraces. Separately, note that
16698 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16699 This happens (for example) for inlined-instances of inline function formal
16700 parameters which are never referenced. This really shouldn't be
16701 happening. All PARM_DECL nodes should get valid non-NULL
16702 DECL_INCOMING_RTL values. FIXME. */
16704 /* Use DECL_RTL as the "location" unless we find something better. */
16705 rtl = DECL_RTL_IF_SET (decl);
16707 /* When generating abstract instances, ignore everything except
16708 constants, symbols living in memory, and symbols living in
16709 fixed registers. */
16710 if (! reload_completed)
16712 if (rtl
16713 && (CONSTANT_P (rtl)
16714 || (MEM_P (rtl)
16715 && CONSTANT_P (XEXP (rtl, 0)))
16716 || (REG_P (rtl)
16717 && TREE_CODE (decl) == VAR_DECL
16718 && TREE_STATIC (decl))))
16720 rtl = targetm.delegitimize_address (rtl);
16721 return rtl;
16723 rtl = NULL_RTX;
16725 else if (TREE_CODE (decl) == PARM_DECL)
16727 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16729 tree declared_type = TREE_TYPE (decl);
16730 tree passed_type = DECL_ARG_TYPE (decl);
16731 enum machine_mode dmode = TYPE_MODE (declared_type);
16732 enum machine_mode pmode = TYPE_MODE (passed_type);
16734 /* This decl represents a formal parameter which was optimized out.
16735 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16736 all cases where (rtl == NULL_RTX) just below. */
16737 if (dmode == pmode)
16738 rtl = DECL_INCOMING_RTL (decl);
16739 else if (SCALAR_INT_MODE_P (dmode)
16740 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16741 && DECL_INCOMING_RTL (decl))
16743 rtx inc = DECL_INCOMING_RTL (decl);
16744 if (REG_P (inc))
16745 rtl = inc;
16746 else if (MEM_P (inc))
16748 if (BYTES_BIG_ENDIAN)
16749 rtl = adjust_address_nv (inc, dmode,
16750 GET_MODE_SIZE (pmode)
16751 - GET_MODE_SIZE (dmode));
16752 else
16753 rtl = inc;
16758 /* If the parm was passed in registers, but lives on the stack, then
16759 make a big endian correction if the mode of the type of the
16760 parameter is not the same as the mode of the rtl. */
16761 /* ??? This is the same series of checks that are made in dbxout.c before
16762 we reach the big endian correction code there. It isn't clear if all
16763 of these checks are necessary here, but keeping them all is the safe
16764 thing to do. */
16765 else if (MEM_P (rtl)
16766 && XEXP (rtl, 0) != const0_rtx
16767 && ! CONSTANT_P (XEXP (rtl, 0))
16768 /* Not passed in memory. */
16769 && !MEM_P (DECL_INCOMING_RTL (decl))
16770 /* Not passed by invisible reference. */
16771 && (!REG_P (XEXP (rtl, 0))
16772 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16773 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16774 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16775 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16776 #endif
16778 /* Big endian correction check. */
16779 && BYTES_BIG_ENDIAN
16780 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16781 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16782 < UNITS_PER_WORD))
16784 int offset = (UNITS_PER_WORD
16785 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16787 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16788 plus_constant (XEXP (rtl, 0), offset));
16791 else if (TREE_CODE (decl) == VAR_DECL
16792 && rtl
16793 && MEM_P (rtl)
16794 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16795 && BYTES_BIG_ENDIAN)
16797 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16798 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16800 /* If a variable is declared "register" yet is smaller than
16801 a register, then if we store the variable to memory, it
16802 looks like we're storing a register-sized value, when in
16803 fact we are not. We need to adjust the offset of the
16804 storage location to reflect the actual value's bytes,
16805 else gdb will not be able to display it. */
16806 if (rsize > dsize)
16807 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16808 plus_constant (XEXP (rtl, 0), rsize-dsize));
16811 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16812 and will have been substituted directly into all expressions that use it.
16813 C does not have such a concept, but C++ and other languages do. */
16814 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16815 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16817 if (rtl)
16818 rtl = targetm.delegitimize_address (rtl);
16820 /* If we don't look past the constant pool, we risk emitting a
16821 reference to a constant pool entry that isn't referenced from
16822 code, and thus is not emitted. */
16823 if (rtl)
16824 rtl = avoid_constant_pool_reference (rtl);
16826 /* Try harder to get a rtl. If this symbol ends up not being emitted
16827 in the current CU, resolve_addr will remove the expression referencing
16828 it. */
16829 if (rtl == NULL_RTX
16830 && TREE_CODE (decl) == VAR_DECL
16831 && !DECL_EXTERNAL (decl)
16832 && TREE_STATIC (decl)
16833 && DECL_NAME (decl)
16834 && !DECL_HARD_REGISTER (decl)
16835 && DECL_MODE (decl) != VOIDmode)
16837 rtl = make_decl_rtl_for_debug (decl);
16838 if (!MEM_P (rtl)
16839 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16840 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16841 rtl = NULL_RTX;
16844 return rtl;
16847 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16848 returned. If so, the decl for the COMMON block is returned, and the
16849 value is the offset into the common block for the symbol. */
16851 static tree
16852 fortran_common (tree decl, HOST_WIDE_INT *value)
16854 tree val_expr, cvar;
16855 enum machine_mode mode;
16856 HOST_WIDE_INT bitsize, bitpos;
16857 tree offset;
16858 int volatilep = 0, unsignedp = 0;
16860 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16861 it does not have a value (the offset into the common area), or if it
16862 is thread local (as opposed to global) then it isn't common, and shouldn't
16863 be handled as such. */
16864 if (TREE_CODE (decl) != VAR_DECL
16865 || !TREE_STATIC (decl)
16866 || !DECL_HAS_VALUE_EXPR_P (decl)
16867 || !is_fortran ())
16868 return NULL_TREE;
16870 val_expr = DECL_VALUE_EXPR (decl);
16871 if (TREE_CODE (val_expr) != COMPONENT_REF)
16872 return NULL_TREE;
16874 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16875 &mode, &unsignedp, &volatilep, true);
16877 if (cvar == NULL_TREE
16878 || TREE_CODE (cvar) != VAR_DECL
16879 || DECL_ARTIFICIAL (cvar)
16880 || !TREE_PUBLIC (cvar))
16881 return NULL_TREE;
16883 *value = 0;
16884 if (offset != NULL)
16886 if (!host_integerp (offset, 0))
16887 return NULL_TREE;
16888 *value = tree_low_cst (offset, 0);
16890 if (bitpos != 0)
16891 *value += bitpos / BITS_PER_UNIT;
16893 return cvar;
16896 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16897 data attribute for a variable or a parameter. We generate the
16898 DW_AT_const_value attribute only in those cases where the given variable
16899 or parameter does not have a true "location" either in memory or in a
16900 register. This can happen (for example) when a constant is passed as an
16901 actual argument in a call to an inline function. (It's possible that
16902 these things can crop up in other ways also.) Note that one type of
16903 constant value which can be passed into an inlined function is a constant
16904 pointer. This can happen for example if an actual argument in an inlined
16905 function call evaluates to a compile-time constant address. */
16907 static bool
16908 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16909 enum dwarf_attribute attr)
16911 rtx rtl;
16912 dw_loc_list_ref list;
16913 var_loc_list *loc_list;
16915 if (TREE_CODE (decl) == ERROR_MARK)
16916 return false;
16918 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16919 || TREE_CODE (decl) == RESULT_DECL);
16921 /* Try to get some constant RTL for this decl, and use that as the value of
16922 the location. */
16924 rtl = rtl_for_decl_location (decl);
16925 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16926 && add_const_value_attribute (die, rtl))
16927 return true;
16929 /* See if we have single element location list that is equivalent to
16930 a constant value. That way we are better to use add_const_value_attribute
16931 rather than expanding constant value equivalent. */
16932 loc_list = lookup_decl_loc (decl);
16933 if (loc_list
16934 && loc_list->first
16935 && loc_list->first->next == NULL
16936 && NOTE_P (loc_list->first->loc)
16937 && NOTE_VAR_LOCATION (loc_list->first->loc)
16938 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16940 struct var_loc_node *node;
16942 node = loc_list->first;
16943 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16944 if (GET_CODE (rtl) == EXPR_LIST)
16945 rtl = XEXP (rtl, 0);
16946 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16947 && add_const_value_attribute (die, rtl))
16948 return true;
16950 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16951 if (list)
16953 add_AT_location_description (die, attr, list);
16954 return true;
16956 /* None of that worked, so it must not really have a location;
16957 try adding a constant value attribute from the DECL_INITIAL. */
16958 return tree_add_const_value_attribute_for_decl (die, decl);
16961 /* Add VARIABLE and DIE into deferred locations list. */
16963 static void
16964 defer_location (tree variable, dw_die_ref die)
16966 deferred_locations entry;
16967 entry.variable = variable;
16968 entry.die = die;
16969 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16972 /* Helper function for tree_add_const_value_attribute. Natively encode
16973 initializer INIT into an array. Return true if successful. */
16975 static bool
16976 native_encode_initializer (tree init, unsigned char *array, int size)
16978 tree type;
16980 if (init == NULL_TREE)
16981 return false;
16983 STRIP_NOPS (init);
16984 switch (TREE_CODE (init))
16986 case STRING_CST:
16987 type = TREE_TYPE (init);
16988 if (TREE_CODE (type) == ARRAY_TYPE)
16990 tree enttype = TREE_TYPE (type);
16991 enum machine_mode mode = TYPE_MODE (enttype);
16993 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16994 return false;
16995 if (int_size_in_bytes (type) != size)
16996 return false;
16997 if (size > TREE_STRING_LENGTH (init))
16999 memcpy (array, TREE_STRING_POINTER (init),
17000 TREE_STRING_LENGTH (init));
17001 memset (array + TREE_STRING_LENGTH (init),
17002 '\0', size - TREE_STRING_LENGTH (init));
17004 else
17005 memcpy (array, TREE_STRING_POINTER (init), size);
17006 return true;
17008 return false;
17009 case CONSTRUCTOR:
17010 type = TREE_TYPE (init);
17011 if (int_size_in_bytes (type) != size)
17012 return false;
17013 if (TREE_CODE (type) == ARRAY_TYPE)
17015 HOST_WIDE_INT min_index;
17016 unsigned HOST_WIDE_INT cnt;
17017 int curpos = 0, fieldsize;
17018 constructor_elt *ce;
17020 if (TYPE_DOMAIN (type) == NULL_TREE
17021 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17022 return false;
17024 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17025 if (fieldsize <= 0)
17026 return false;
17028 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17029 memset (array, '\0', size);
17030 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17032 tree val = ce->value;
17033 tree index = ce->index;
17034 int pos = curpos;
17035 if (index && TREE_CODE (index) == RANGE_EXPR)
17036 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17037 * fieldsize;
17038 else if (index)
17039 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17041 if (val)
17043 STRIP_NOPS (val);
17044 if (!native_encode_initializer (val, array + pos, fieldsize))
17045 return false;
17047 curpos = pos + fieldsize;
17048 if (index && TREE_CODE (index) == RANGE_EXPR)
17050 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17051 - tree_low_cst (TREE_OPERAND (index, 0), 0);
17052 while (count > 0)
17054 if (val)
17055 memcpy (array + curpos, array + pos, fieldsize);
17056 curpos += fieldsize;
17059 gcc_assert (curpos <= size);
17061 return true;
17063 else if (TREE_CODE (type) == RECORD_TYPE
17064 || TREE_CODE (type) == UNION_TYPE)
17066 tree field = NULL_TREE;
17067 unsigned HOST_WIDE_INT cnt;
17068 constructor_elt *ce;
17070 if (int_size_in_bytes (type) != size)
17071 return false;
17073 if (TREE_CODE (type) == RECORD_TYPE)
17074 field = TYPE_FIELDS (type);
17076 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17078 tree val = ce->value;
17079 int pos, fieldsize;
17081 if (ce->index != 0)
17082 field = ce->index;
17084 if (val)
17085 STRIP_NOPS (val);
17087 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17088 return false;
17090 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17091 && TYPE_DOMAIN (TREE_TYPE (field))
17092 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17093 return false;
17094 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17095 || !host_integerp (DECL_SIZE_UNIT (field), 0))
17096 return false;
17097 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17098 pos = int_byte_position (field);
17099 gcc_assert (pos + fieldsize <= size);
17100 if (val
17101 && !native_encode_initializer (val, array + pos, fieldsize))
17102 return false;
17104 return true;
17106 return false;
17107 case VIEW_CONVERT_EXPR:
17108 case NON_LVALUE_EXPR:
17109 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17110 default:
17111 return native_encode_expr (init, array, size) == size;
17115 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17116 attribute is the const value T. */
17118 static bool
17119 tree_add_const_value_attribute (dw_die_ref die, tree t)
17121 tree init;
17122 tree type = TREE_TYPE (t);
17123 rtx rtl;
17125 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17126 return false;
17128 init = t;
17129 gcc_assert (!DECL_P (init));
17131 rtl = rtl_for_decl_init (init, type);
17132 if (rtl)
17133 return add_const_value_attribute (die, rtl);
17134 /* If the host and target are sane, try harder. */
17135 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17136 && initializer_constant_valid_p (init, type))
17138 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17139 if (size > 0 && (int) size == size)
17141 unsigned char *array = (unsigned char *)
17142 ggc_alloc_cleared_atomic (size);
17144 if (native_encode_initializer (init, array, size))
17146 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17147 return true;
17151 return false;
17154 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17155 attribute is the const value of T, where T is an integral constant
17156 variable with static storage duration
17157 (so it can't be a PARM_DECL or a RESULT_DECL). */
17159 static bool
17160 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17163 if (!decl
17164 || (TREE_CODE (decl) != VAR_DECL
17165 && TREE_CODE (decl) != CONST_DECL))
17166 return false;
17168 if (TREE_READONLY (decl)
17169 && ! TREE_THIS_VOLATILE (decl)
17170 && DECL_INITIAL (decl))
17171 /* OK */;
17172 else
17173 return false;
17175 /* Don't add DW_AT_const_value if abstract origin already has one. */
17176 if (get_AT (var_die, DW_AT_const_value))
17177 return false;
17179 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17182 /* Convert the CFI instructions for the current function into a
17183 location list. This is used for DW_AT_frame_base when we targeting
17184 a dwarf2 consumer that does not support the dwarf3
17185 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17186 expressions. */
17188 static dw_loc_list_ref
17189 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17191 dw_fde_ref fde;
17192 dw_loc_list_ref list, *list_tail;
17193 dw_cfi_ref cfi;
17194 dw_cfa_location last_cfa, next_cfa;
17195 const char *start_label, *last_label, *section;
17196 dw_cfa_location remember;
17198 fde = current_fde ();
17199 gcc_assert (fde != NULL);
17201 section = secname_for_decl (current_function_decl);
17202 list_tail = &list;
17203 list = NULL;
17205 memset (&next_cfa, 0, sizeof (next_cfa));
17206 next_cfa.reg = INVALID_REGNUM;
17207 remember = next_cfa;
17209 start_label = fde->dw_fde_begin;
17211 /* ??? Bald assumption that the CIE opcode list does not contain
17212 advance opcodes. */
17213 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17214 lookup_cfa_1 (cfi, &next_cfa, &remember);
17216 last_cfa = next_cfa;
17217 last_label = start_label;
17219 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17220 switch (cfi->dw_cfi_opc)
17222 case DW_CFA_set_loc:
17223 case DW_CFA_advance_loc1:
17224 case DW_CFA_advance_loc2:
17225 case DW_CFA_advance_loc4:
17226 if (!cfa_equal_p (&last_cfa, &next_cfa))
17228 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17229 start_label, last_label, section);
17231 list_tail = &(*list_tail)->dw_loc_next;
17232 last_cfa = next_cfa;
17233 start_label = last_label;
17235 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17236 break;
17238 case DW_CFA_advance_loc:
17239 /* The encoding is complex enough that we should never emit this. */
17240 gcc_unreachable ();
17242 default:
17243 lookup_cfa_1 (cfi, &next_cfa, &remember);
17244 break;
17247 if (!cfa_equal_p (&last_cfa, &next_cfa))
17249 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17250 start_label, last_label, section);
17251 list_tail = &(*list_tail)->dw_loc_next;
17252 start_label = last_label;
17255 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17256 start_label, fde->dw_fde_end, section);
17258 if (list && list->dw_loc_next)
17259 gen_llsym (list);
17261 return list;
17264 /* Compute a displacement from the "steady-state frame pointer" to the
17265 frame base (often the same as the CFA), and store it in
17266 frame_pointer_fb_offset. OFFSET is added to the displacement
17267 before the latter is negated. */
17269 static void
17270 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17272 rtx reg, elim;
17274 #ifdef FRAME_POINTER_CFA_OFFSET
17275 reg = frame_pointer_rtx;
17276 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17277 #else
17278 reg = arg_pointer_rtx;
17279 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17280 #endif
17282 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17283 if (GET_CODE (elim) == PLUS)
17285 offset += INTVAL (XEXP (elim, 1));
17286 elim = XEXP (elim, 0);
17289 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17290 && (elim == hard_frame_pointer_rtx
17291 || elim == stack_pointer_rtx))
17292 || elim == (frame_pointer_needed
17293 ? hard_frame_pointer_rtx
17294 : stack_pointer_rtx));
17296 frame_pointer_fb_offset = -offset;
17299 /* Generate a DW_AT_name attribute given some string value to be included as
17300 the value of the attribute. */
17302 static void
17303 add_name_attribute (dw_die_ref die, const char *name_string)
17305 if (name_string != NULL && *name_string != 0)
17307 if (demangle_name_func)
17308 name_string = (*demangle_name_func) (name_string);
17310 add_AT_string (die, DW_AT_name, name_string);
17314 /* Generate a DW_AT_comp_dir attribute for DIE. */
17316 static void
17317 add_comp_dir_attribute (dw_die_ref die)
17319 const char *wd = get_src_pwd ();
17320 char *wd1;
17322 if (wd == NULL)
17323 return;
17325 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17327 int wdlen;
17329 wdlen = strlen (wd);
17330 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17331 strcpy (wd1, wd);
17332 wd1 [wdlen] = DIR_SEPARATOR;
17333 wd1 [wdlen + 1] = 0;
17334 wd = wd1;
17337 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17340 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17341 default. */
17343 static int
17344 lower_bound_default (void)
17346 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17348 case DW_LANG_C:
17349 case DW_LANG_C89:
17350 case DW_LANG_C99:
17351 case DW_LANG_C_plus_plus:
17352 case DW_LANG_ObjC:
17353 case DW_LANG_ObjC_plus_plus:
17354 case DW_LANG_Java:
17355 return 0;
17356 case DW_LANG_Fortran77:
17357 case DW_LANG_Fortran90:
17358 case DW_LANG_Fortran95:
17359 return 1;
17360 case DW_LANG_UPC:
17361 case DW_LANG_D:
17362 case DW_LANG_Python:
17363 return dwarf_version >= 4 ? 0 : -1;
17364 case DW_LANG_Ada95:
17365 case DW_LANG_Ada83:
17366 case DW_LANG_Cobol74:
17367 case DW_LANG_Cobol85:
17368 case DW_LANG_Pascal83:
17369 case DW_LANG_Modula2:
17370 case DW_LANG_PLI:
17371 return dwarf_version >= 4 ? 1 : -1;
17372 default:
17373 return -1;
17377 /* Given a tree node describing an array bound (either lower or upper) output
17378 a representation for that bound. */
17380 static void
17381 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17383 switch (TREE_CODE (bound))
17385 case ERROR_MARK:
17386 return;
17388 /* All fixed-bounds are represented by INTEGER_CST nodes. */
17389 case INTEGER_CST:
17391 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17392 int dflt;
17394 /* Use the default if possible. */
17395 if (bound_attr == DW_AT_lower_bound
17396 && host_integerp (bound, 0)
17397 && (dflt = lower_bound_default ()) != -1
17398 && tree_low_cst (bound, 0) == dflt)
17401 /* Otherwise represent the bound as an unsigned value with the
17402 precision of its type. The precision and signedness of the
17403 type will be necessary to re-interpret it unambiguously. */
17404 else if (prec < HOST_BITS_PER_WIDE_INT)
17406 unsigned HOST_WIDE_INT mask
17407 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17408 add_AT_unsigned (subrange_die, bound_attr,
17409 TREE_INT_CST_LOW (bound) & mask);
17411 else if (prec == HOST_BITS_PER_WIDE_INT
17412 || TREE_INT_CST_HIGH (bound) == 0)
17413 add_AT_unsigned (subrange_die, bound_attr,
17414 TREE_INT_CST_LOW (bound));
17415 else
17416 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17417 TREE_INT_CST_LOW (bound));
17419 break;
17421 CASE_CONVERT:
17422 case VIEW_CONVERT_EXPR:
17423 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17424 break;
17426 case SAVE_EXPR:
17427 break;
17429 case VAR_DECL:
17430 case PARM_DECL:
17431 case RESULT_DECL:
17433 dw_die_ref decl_die = lookup_decl_die (bound);
17435 /* ??? Can this happen, or should the variable have been bound
17436 first? Probably it can, since I imagine that we try to create
17437 the types of parameters in the order in which they exist in
17438 the list, and won't have created a forward reference to a
17439 later parameter. */
17440 if (decl_die != NULL)
17442 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17443 break;
17446 /* FALLTHRU */
17448 default:
17450 /* Otherwise try to create a stack operation procedure to
17451 evaluate the value of the array bound. */
17453 dw_die_ref ctx, decl_die;
17454 dw_loc_list_ref list;
17456 list = loc_list_from_tree (bound, 2);
17457 if (list == NULL || single_element_loc_list_p (list))
17459 /* If DW_AT_*bound is not a reference nor constant, it is
17460 a DWARF expression rather than location description.
17461 For that loc_list_from_tree (bound, 0) is needed.
17462 If that fails to give a single element list,
17463 fall back to outputting this as a reference anyway. */
17464 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17465 if (list2 && single_element_loc_list_p (list2))
17467 add_AT_loc (subrange_die, bound_attr, list2->expr);
17468 break;
17471 if (list == NULL)
17472 break;
17474 if (current_function_decl == 0)
17475 ctx = comp_unit_die ();
17476 else
17477 ctx = lookup_decl_die (current_function_decl);
17479 decl_die = new_die (DW_TAG_variable, ctx, bound);
17480 add_AT_flag (decl_die, DW_AT_artificial, 1);
17481 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17482 add_AT_location_description (decl_die, DW_AT_location, list);
17483 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17484 break;
17489 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17490 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17491 Note that the block of subscript information for an array type also
17492 includes information about the element type of the given array type. */
17494 static void
17495 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17497 unsigned dimension_number;
17498 tree lower, upper;
17499 dw_die_ref subrange_die;
17501 for (dimension_number = 0;
17502 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17503 type = TREE_TYPE (type), dimension_number++)
17505 tree domain = TYPE_DOMAIN (type);
17507 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17508 break;
17510 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17511 and (in GNU C only) variable bounds. Handle all three forms
17512 here. */
17513 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17514 if (domain)
17516 /* We have an array type with specified bounds. */
17517 lower = TYPE_MIN_VALUE (domain);
17518 upper = TYPE_MAX_VALUE (domain);
17520 /* Define the index type. */
17521 if (TREE_TYPE (domain))
17523 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17524 TREE_TYPE field. We can't emit debug info for this
17525 because it is an unnamed integral type. */
17526 if (TREE_CODE (domain) == INTEGER_TYPE
17527 && TYPE_NAME (domain) == NULL_TREE
17528 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17529 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17531 else
17532 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17533 type_die);
17536 /* ??? If upper is NULL, the array has unspecified length,
17537 but it does have a lower bound. This happens with Fortran
17538 dimension arr(N:*)
17539 Since the debugger is definitely going to need to know N
17540 to produce useful results, go ahead and output the lower
17541 bound solo, and hope the debugger can cope. */
17543 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17544 if (upper)
17545 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17548 /* Otherwise we have an array type with an unspecified length. The
17549 DWARF-2 spec does not say how to handle this; let's just leave out the
17550 bounds. */
17554 static void
17555 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17557 unsigned size;
17559 switch (TREE_CODE (tree_node))
17561 case ERROR_MARK:
17562 size = 0;
17563 break;
17564 case ENUMERAL_TYPE:
17565 case RECORD_TYPE:
17566 case UNION_TYPE:
17567 case QUAL_UNION_TYPE:
17568 size = int_size_in_bytes (tree_node);
17569 break;
17570 case FIELD_DECL:
17571 /* For a data member of a struct or union, the DW_AT_byte_size is
17572 generally given as the number of bytes normally allocated for an
17573 object of the *declared* type of the member itself. This is true
17574 even for bit-fields. */
17575 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17576 break;
17577 default:
17578 gcc_unreachable ();
17581 /* Note that `size' might be -1 when we get to this point. If it is, that
17582 indicates that the byte size of the entity in question is variable. We
17583 have no good way of expressing this fact in Dwarf at the present time,
17584 so just let the -1 pass on through. */
17585 add_AT_unsigned (die, DW_AT_byte_size, size);
17588 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17589 which specifies the distance in bits from the highest order bit of the
17590 "containing object" for the bit-field to the highest order bit of the
17591 bit-field itself.
17593 For any given bit-field, the "containing object" is a hypothetical object
17594 (of some integral or enum type) within which the given bit-field lives. The
17595 type of this hypothetical "containing object" is always the same as the
17596 declared type of the individual bit-field itself. The determination of the
17597 exact location of the "containing object" for a bit-field is rather
17598 complicated. It's handled by the `field_byte_offset' function (above).
17600 Note that it is the size (in bytes) of the hypothetical "containing object"
17601 which will be given in the DW_AT_byte_size attribute for this bit-field.
17602 (See `byte_size_attribute' above). */
17604 static inline void
17605 add_bit_offset_attribute (dw_die_ref die, tree decl)
17607 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17608 tree type = DECL_BIT_FIELD_TYPE (decl);
17609 HOST_WIDE_INT bitpos_int;
17610 HOST_WIDE_INT highest_order_object_bit_offset;
17611 HOST_WIDE_INT highest_order_field_bit_offset;
17612 HOST_WIDE_INT unsigned bit_offset;
17614 /* Must be a field and a bit field. */
17615 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17617 /* We can't yet handle bit-fields whose offsets are variable, so if we
17618 encounter such things, just return without generating any attribute
17619 whatsoever. Likewise for variable or too large size. */
17620 if (! host_integerp (bit_position (decl), 0)
17621 || ! host_integerp (DECL_SIZE (decl), 1))
17622 return;
17624 bitpos_int = int_bit_position (decl);
17626 /* Note that the bit offset is always the distance (in bits) from the
17627 highest-order bit of the "containing object" to the highest-order bit of
17628 the bit-field itself. Since the "high-order end" of any object or field
17629 is different on big-endian and little-endian machines, the computation
17630 below must take account of these differences. */
17631 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17632 highest_order_field_bit_offset = bitpos_int;
17634 if (! BYTES_BIG_ENDIAN)
17636 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17637 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17640 bit_offset
17641 = (! BYTES_BIG_ENDIAN
17642 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17643 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17645 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17648 /* For a FIELD_DECL node which represents a bit field, output an attribute
17649 which specifies the length in bits of the given field. */
17651 static inline void
17652 add_bit_size_attribute (dw_die_ref die, tree decl)
17654 /* Must be a field and a bit field. */
17655 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17656 && DECL_BIT_FIELD_TYPE (decl));
17658 if (host_integerp (DECL_SIZE (decl), 1))
17659 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17662 /* If the compiled language is ANSI C, then add a 'prototyped'
17663 attribute, if arg types are given for the parameters of a function. */
17665 static inline void
17666 add_prototyped_attribute (dw_die_ref die, tree func_type)
17668 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17669 && prototype_p (func_type))
17670 add_AT_flag (die, DW_AT_prototyped, 1);
17673 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17674 by looking in either the type declaration or object declaration
17675 equate table. */
17677 static inline dw_die_ref
17678 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17680 dw_die_ref origin_die = NULL;
17682 if (TREE_CODE (origin) != FUNCTION_DECL)
17684 /* We may have gotten separated from the block for the inlined
17685 function, if we're in an exception handler or some such; make
17686 sure that the abstract function has been written out.
17688 Doing this for nested functions is wrong, however; functions are
17689 distinct units, and our context might not even be inline. */
17690 tree fn = origin;
17692 if (TYPE_P (fn))
17693 fn = TYPE_STUB_DECL (fn);
17695 fn = decl_function_context (fn);
17696 if (fn)
17697 dwarf2out_abstract_function (fn);
17700 if (DECL_P (origin))
17701 origin_die = lookup_decl_die (origin);
17702 else if (TYPE_P (origin))
17703 origin_die = lookup_type_die (origin);
17705 /* XXX: Functions that are never lowered don't always have correct block
17706 trees (in the case of java, they simply have no block tree, in some other
17707 languages). For these functions, there is nothing we can really do to
17708 output correct debug info for inlined functions in all cases. Rather
17709 than die, we'll just produce deficient debug info now, in that we will
17710 have variables without a proper abstract origin. In the future, when all
17711 functions are lowered, we should re-add a gcc_assert (origin_die)
17712 here. */
17714 if (origin_die)
17715 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17716 return origin_die;
17719 /* We do not currently support the pure_virtual attribute. */
17721 static inline void
17722 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17724 if (DECL_VINDEX (func_decl))
17726 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17728 if (host_integerp (DECL_VINDEX (func_decl), 0))
17729 add_AT_loc (die, DW_AT_vtable_elem_location,
17730 new_loc_descr (DW_OP_constu,
17731 tree_low_cst (DECL_VINDEX (func_decl), 0),
17732 0));
17734 /* GNU extension: Record what type this method came from originally. */
17735 if (debug_info_level > DINFO_LEVEL_TERSE
17736 && DECL_CONTEXT (func_decl))
17737 add_AT_die_ref (die, DW_AT_containing_type,
17738 lookup_type_die (DECL_CONTEXT (func_decl)));
17742 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17743 given decl. This used to be a vendor extension until after DWARF 4
17744 standardized it. */
17746 static void
17747 add_linkage_attr (dw_die_ref die, tree decl)
17749 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17751 /* Mimic what assemble_name_raw does with a leading '*'. */
17752 if (name[0] == '*')
17753 name = &name[1];
17755 if (dwarf_version >= 4)
17756 add_AT_string (die, DW_AT_linkage_name, name);
17757 else
17758 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17761 /* Add source coordinate attributes for the given decl. */
17763 static void
17764 add_src_coords_attributes (dw_die_ref die, tree decl)
17766 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17768 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17769 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17772 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17774 static void
17775 add_linkage_name (dw_die_ref die, tree decl)
17777 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17778 && TREE_PUBLIC (decl)
17779 && !DECL_ABSTRACT (decl)
17780 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17781 && die->die_tag != DW_TAG_member)
17783 /* Defer until we have an assembler name set. */
17784 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17786 limbo_die_node *asm_name;
17788 asm_name = ggc_alloc_cleared_limbo_die_node ();
17789 asm_name->die = die;
17790 asm_name->created_for = decl;
17791 asm_name->next = deferred_asm_name;
17792 deferred_asm_name = asm_name;
17794 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17795 add_linkage_attr (die, decl);
17799 /* Add a DW_AT_name attribute and source coordinate attribute for the
17800 given decl, but only if it actually has a name. */
17802 static void
17803 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17805 tree decl_name;
17807 decl_name = DECL_NAME (decl);
17808 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17810 const char *name = dwarf2_name (decl, 0);
17811 if (name)
17812 add_name_attribute (die, name);
17813 if (! DECL_ARTIFICIAL (decl))
17814 add_src_coords_attributes (die, decl);
17816 add_linkage_name (die, decl);
17819 #ifdef VMS_DEBUGGING_INFO
17820 /* Get the function's name, as described by its RTL. This may be different
17821 from the DECL_NAME name used in the source file. */
17822 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17824 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17825 XEXP (DECL_RTL (decl), 0));
17826 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17828 #endif /* VMS_DEBUGGING_INFO */
17831 #ifdef VMS_DEBUGGING_INFO
17832 /* Output the debug main pointer die for VMS */
17834 void
17835 dwarf2out_vms_debug_main_pointer (void)
17837 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17838 dw_die_ref die;
17840 /* Allocate the VMS debug main subprogram die. */
17841 die = ggc_alloc_cleared_die_node ();
17842 die->die_tag = DW_TAG_subprogram;
17843 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17844 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17845 current_function_funcdef_no);
17846 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17848 /* Make it the first child of comp_unit_die (). */
17849 die->die_parent = comp_unit_die ();
17850 if (comp_unit_die ()->die_child)
17852 die->die_sib = comp_unit_die ()->die_child->die_sib;
17853 comp_unit_die ()->die_child->die_sib = die;
17855 else
17857 die->die_sib = die;
17858 comp_unit_die ()->die_child = die;
17861 #endif /* VMS_DEBUGGING_INFO */
17863 /* Push a new declaration scope. */
17865 static void
17866 push_decl_scope (tree scope)
17868 VEC_safe_push (tree, gc, decl_scope_table, scope);
17871 /* Pop a declaration scope. */
17873 static inline void
17874 pop_decl_scope (void)
17876 VEC_pop (tree, decl_scope_table);
17879 /* Return the DIE for the scope that immediately contains this type.
17880 Non-named types get global scope. Named types nested in other
17881 types get their containing scope if it's open, or global scope
17882 otherwise. All other types (i.e. function-local named types) get
17883 the current active scope. */
17885 static dw_die_ref
17886 scope_die_for (tree t, dw_die_ref context_die)
17888 dw_die_ref scope_die = NULL;
17889 tree containing_scope;
17890 int i;
17892 /* Non-types always go in the current scope. */
17893 gcc_assert (TYPE_P (t));
17895 containing_scope = TYPE_CONTEXT (t);
17897 /* Use the containing namespace if it was passed in (for a declaration). */
17898 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17900 if (context_die == lookup_decl_die (containing_scope))
17901 /* OK */;
17902 else
17903 containing_scope = NULL_TREE;
17906 /* Ignore function type "scopes" from the C frontend. They mean that
17907 a tagged type is local to a parmlist of a function declarator, but
17908 that isn't useful to DWARF. */
17909 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17910 containing_scope = NULL_TREE;
17912 if (SCOPE_FILE_SCOPE_P (containing_scope))
17913 scope_die = comp_unit_die ();
17914 else if (TYPE_P (containing_scope))
17916 /* For types, we can just look up the appropriate DIE. But
17917 first we check to see if we're in the middle of emitting it
17918 so we know where the new DIE should go. */
17919 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17920 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17921 break;
17923 if (i < 0)
17925 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17926 || TREE_ASM_WRITTEN (containing_scope));
17927 /*We are not in the middle of emitting the type
17928 CONTAINING_SCOPE. Let's see if it's emitted already. */
17929 scope_die = lookup_type_die (containing_scope);
17931 /* If none of the current dies are suitable, we get file scope. */
17932 if (scope_die == NULL)
17933 scope_die = comp_unit_die ();
17935 else
17936 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17938 else
17939 scope_die = context_die;
17941 return scope_die;
17944 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17946 static inline int
17947 local_scope_p (dw_die_ref context_die)
17949 for (; context_die; context_die = context_die->die_parent)
17950 if (context_die->die_tag == DW_TAG_inlined_subroutine
17951 || context_die->die_tag == DW_TAG_subprogram)
17952 return 1;
17954 return 0;
17957 /* Returns nonzero if CONTEXT_DIE is a class. */
17959 static inline int
17960 class_scope_p (dw_die_ref context_die)
17962 return (context_die
17963 && (context_die->die_tag == DW_TAG_structure_type
17964 || context_die->die_tag == DW_TAG_class_type
17965 || context_die->die_tag == DW_TAG_interface_type
17966 || context_die->die_tag == DW_TAG_union_type));
17969 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17970 whether or not to treat a DIE in this context as a declaration. */
17972 static inline int
17973 class_or_namespace_scope_p (dw_die_ref context_die)
17975 return (class_scope_p (context_die)
17976 || (context_die && context_die->die_tag == DW_TAG_namespace));
17979 /* Many forms of DIEs require a "type description" attribute. This
17980 routine locates the proper "type descriptor" die for the type given
17981 by 'type', and adds a DW_AT_type attribute below the given die. */
17983 static void
17984 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17985 int decl_volatile, dw_die_ref context_die)
17987 enum tree_code code = TREE_CODE (type);
17988 dw_die_ref type_die = NULL;
17990 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17991 or fixed-point type, use the inner type. This is because we have no
17992 support for unnamed types in base_type_die. This can happen if this is
17993 an Ada subrange type. Correct solution is emit a subrange type die. */
17994 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17995 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17996 type = TREE_TYPE (type), code = TREE_CODE (type);
17998 if (code == ERROR_MARK
17999 /* Handle a special case. For functions whose return type is void, we
18000 generate *no* type attribute. (Note that no object may have type
18001 `void', so this only applies to function return types). */
18002 || code == VOID_TYPE)
18003 return;
18005 type_die = modified_type_die (type,
18006 decl_const || TYPE_READONLY (type),
18007 decl_volatile || TYPE_VOLATILE (type),
18008 context_die);
18010 if (type_die != NULL)
18011 add_AT_die_ref (object_die, DW_AT_type, type_die);
18014 /* Given an object die, add the calling convention attribute for the
18015 function call type. */
18016 static void
18017 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18019 enum dwarf_calling_convention value = DW_CC_normal;
18021 value = ((enum dwarf_calling_convention)
18022 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18024 if (is_fortran ()
18025 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18027 /* DWARF 2 doesn't provide a way to identify a program's source-level
18028 entry point. DW_AT_calling_convention attributes are only meant
18029 to describe functions' calling conventions. However, lacking a
18030 better way to signal the Fortran main program, we used this for
18031 a long time, following existing custom. Now, DWARF 4 has
18032 DW_AT_main_subprogram, which we add below, but some tools still
18033 rely on the old way, which we thus keep. */
18034 value = DW_CC_program;
18036 if (dwarf_version >= 4 || !dwarf_strict)
18037 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18040 /* Only add the attribute if the backend requests it, and
18041 is not DW_CC_normal. */
18042 if (value && (value != DW_CC_normal))
18043 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18046 /* Given a tree pointer to a struct, class, union, or enum type node, return
18047 a pointer to the (string) tag name for the given type, or zero if the type
18048 was declared without a tag. */
18050 static const char *
18051 type_tag (const_tree type)
18053 const char *name = 0;
18055 if (TYPE_NAME (type) != 0)
18057 tree t = 0;
18059 /* Find the IDENTIFIER_NODE for the type name. */
18060 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18061 && !TYPE_NAMELESS (type))
18062 t = TYPE_NAME (type);
18064 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18065 a TYPE_DECL node, regardless of whether or not a `typedef' was
18066 involved. */
18067 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18068 && ! DECL_IGNORED_P (TYPE_NAME (type)))
18070 /* We want to be extra verbose. Don't call dwarf_name if
18071 DECL_NAME isn't set. The default hook for decl_printable_name
18072 doesn't like that, and in this context it's correct to return
18073 0, instead of "<anonymous>" or the like. */
18074 if (DECL_NAME (TYPE_NAME (type))
18075 && !DECL_NAMELESS (TYPE_NAME (type)))
18076 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18079 /* Now get the name as a string, or invent one. */
18080 if (!name && t != 0)
18081 name = IDENTIFIER_POINTER (t);
18084 return (name == 0 || *name == '\0') ? 0 : name;
18087 /* Return the type associated with a data member, make a special check
18088 for bit field types. */
18090 static inline tree
18091 member_declared_type (const_tree member)
18093 return (DECL_BIT_FIELD_TYPE (member)
18094 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18097 /* Get the decl's label, as described by its RTL. This may be different
18098 from the DECL_NAME name used in the source file. */
18100 #if 0
18101 static const char *
18102 decl_start_label (tree decl)
18104 rtx x;
18105 const char *fnname;
18107 x = DECL_RTL (decl);
18108 gcc_assert (MEM_P (x));
18110 x = XEXP (x, 0);
18111 gcc_assert (GET_CODE (x) == SYMBOL_REF);
18113 fnname = XSTR (x, 0);
18114 return fnname;
18116 #endif
18118 /* These routines generate the internal representation of the DIE's for
18119 the compilation unit. Debugging information is collected by walking
18120 the declaration trees passed in from dwarf2out_decl(). */
18122 static void
18123 gen_array_type_die (tree type, dw_die_ref context_die)
18125 dw_die_ref scope_die = scope_die_for (type, context_die);
18126 dw_die_ref array_die;
18128 /* GNU compilers represent multidimensional array types as sequences of one
18129 dimensional array types whose element types are themselves array types.
18130 We sometimes squish that down to a single array_type DIE with multiple
18131 subscripts in the Dwarf debugging info. The draft Dwarf specification
18132 say that we are allowed to do this kind of compression in C, because
18133 there is no difference between an array of arrays and a multidimensional
18134 array. We don't do this for Ada to remain as close as possible to the
18135 actual representation, which is especially important against the language
18136 flexibilty wrt arrays of variable size. */
18138 bool collapse_nested_arrays = !is_ada ();
18139 tree element_type;
18141 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18142 DW_TAG_string_type doesn't have DW_AT_type attribute). */
18143 if (TYPE_STRING_FLAG (type)
18144 && TREE_CODE (type) == ARRAY_TYPE
18145 && is_fortran ()
18146 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18148 HOST_WIDE_INT size;
18150 array_die = new_die (DW_TAG_string_type, scope_die, type);
18151 add_name_attribute (array_die, type_tag (type));
18152 equate_type_number_to_die (type, array_die);
18153 size = int_size_in_bytes (type);
18154 if (size >= 0)
18155 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18156 else if (TYPE_DOMAIN (type) != NULL_TREE
18157 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18158 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18160 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18161 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18163 size = int_size_in_bytes (TREE_TYPE (szdecl));
18164 if (loc && size > 0)
18166 add_AT_location_description (array_die, DW_AT_string_length, loc);
18167 if (size != DWARF2_ADDR_SIZE)
18168 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18171 return;
18174 /* ??? The SGI dwarf reader fails for array of array of enum types
18175 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18176 array type comes before the outer array type. We thus call gen_type_die
18177 before we new_die and must prevent nested array types collapsing for this
18178 target. */
18180 #ifdef MIPS_DEBUGGING_INFO
18181 gen_type_die (TREE_TYPE (type), context_die);
18182 collapse_nested_arrays = false;
18183 #endif
18185 array_die = new_die (DW_TAG_array_type, scope_die, type);
18186 add_name_attribute (array_die, type_tag (type));
18187 equate_type_number_to_die (type, array_die);
18189 if (TREE_CODE (type) == VECTOR_TYPE)
18190 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18192 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18193 if (is_fortran ()
18194 && TREE_CODE (type) == ARRAY_TYPE
18195 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18196 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18197 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18199 #if 0
18200 /* We default the array ordering. SDB will probably do
18201 the right things even if DW_AT_ordering is not present. It's not even
18202 an issue until we start to get into multidimensional arrays anyway. If
18203 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18204 then we'll have to put the DW_AT_ordering attribute back in. (But if
18205 and when we find out that we need to put these in, we will only do so
18206 for multidimensional arrays. */
18207 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18208 #endif
18210 #ifdef MIPS_DEBUGGING_INFO
18211 /* The SGI compilers handle arrays of unknown bound by setting
18212 AT_declaration and not emitting any subrange DIEs. */
18213 if (TREE_CODE (type) == ARRAY_TYPE
18214 && ! TYPE_DOMAIN (type))
18215 add_AT_flag (array_die, DW_AT_declaration, 1);
18216 else
18217 #endif
18218 if (TREE_CODE (type) == VECTOR_TYPE)
18220 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
18221 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18222 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18223 add_bound_info (subrange_die, DW_AT_upper_bound,
18224 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18226 else
18227 add_subscript_info (array_die, type, collapse_nested_arrays);
18229 /* Add representation of the type of the elements of this array type and
18230 emit the corresponding DIE if we haven't done it already. */
18231 element_type = TREE_TYPE (type);
18232 if (collapse_nested_arrays)
18233 while (TREE_CODE (element_type) == ARRAY_TYPE)
18235 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18236 break;
18237 element_type = TREE_TYPE (element_type);
18240 #ifndef MIPS_DEBUGGING_INFO
18241 gen_type_die (element_type, context_die);
18242 #endif
18244 add_type_attribute (array_die, element_type, 0, 0, context_die);
18246 if (get_AT (array_die, DW_AT_name))
18247 add_pubtype (type, array_die);
18250 static dw_loc_descr_ref
18251 descr_info_loc (tree val, tree base_decl)
18253 HOST_WIDE_INT size;
18254 dw_loc_descr_ref loc, loc2;
18255 enum dwarf_location_atom op;
18257 if (val == base_decl)
18258 return new_loc_descr (DW_OP_push_object_address, 0, 0);
18260 switch (TREE_CODE (val))
18262 CASE_CONVERT:
18263 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18264 case VAR_DECL:
18265 return loc_descriptor_from_tree (val, 0);
18266 case INTEGER_CST:
18267 if (host_integerp (val, 0))
18268 return int_loc_descriptor (tree_low_cst (val, 0));
18269 break;
18270 case INDIRECT_REF:
18271 size = int_size_in_bytes (TREE_TYPE (val));
18272 if (size < 0)
18273 break;
18274 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18275 if (!loc)
18276 break;
18277 if (size == DWARF2_ADDR_SIZE)
18278 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18279 else
18280 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18281 return loc;
18282 case POINTER_PLUS_EXPR:
18283 case PLUS_EXPR:
18284 if (host_integerp (TREE_OPERAND (val, 1), 1)
18285 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18286 < 16384)
18288 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18289 if (!loc)
18290 break;
18291 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18293 else
18295 op = DW_OP_plus;
18296 do_binop:
18297 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18298 if (!loc)
18299 break;
18300 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18301 if (!loc2)
18302 break;
18303 add_loc_descr (&loc, loc2);
18304 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18306 return loc;
18307 case MINUS_EXPR:
18308 op = DW_OP_minus;
18309 goto do_binop;
18310 case MULT_EXPR:
18311 op = DW_OP_mul;
18312 goto do_binop;
18313 case EQ_EXPR:
18314 op = DW_OP_eq;
18315 goto do_binop;
18316 case NE_EXPR:
18317 op = DW_OP_ne;
18318 goto do_binop;
18319 default:
18320 break;
18322 return NULL;
18325 static void
18326 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18327 tree val, tree base_decl)
18329 dw_loc_descr_ref loc;
18331 if (host_integerp (val, 0))
18333 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18334 return;
18337 loc = descr_info_loc (val, base_decl);
18338 if (!loc)
18339 return;
18341 add_AT_loc (die, attr, loc);
18344 /* This routine generates DIE for array with hidden descriptor, details
18345 are filled into *info by a langhook. */
18347 static void
18348 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18349 dw_die_ref context_die)
18351 dw_die_ref scope_die = scope_die_for (type, context_die);
18352 dw_die_ref array_die;
18353 int dim;
18355 array_die = new_die (DW_TAG_array_type, scope_die, type);
18356 add_name_attribute (array_die, type_tag (type));
18357 equate_type_number_to_die (type, array_die);
18359 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18360 if (is_fortran ()
18361 && info->ndimensions >= 2)
18362 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18364 if (info->data_location)
18365 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18366 info->base_decl);
18367 if (info->associated)
18368 add_descr_info_field (array_die, DW_AT_associated, info->associated,
18369 info->base_decl);
18370 if (info->allocated)
18371 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18372 info->base_decl);
18374 for (dim = 0; dim < info->ndimensions; dim++)
18376 dw_die_ref subrange_die
18377 = new_die (DW_TAG_subrange_type, array_die, NULL);
18379 if (info->dimen[dim].lower_bound)
18381 /* If it is the default value, omit it. */
18382 int dflt;
18384 if (host_integerp (info->dimen[dim].lower_bound, 0)
18385 && (dflt = lower_bound_default ()) != -1
18386 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18388 else
18389 add_descr_info_field (subrange_die, DW_AT_lower_bound,
18390 info->dimen[dim].lower_bound,
18391 info->base_decl);
18393 if (info->dimen[dim].upper_bound)
18394 add_descr_info_field (subrange_die, DW_AT_upper_bound,
18395 info->dimen[dim].upper_bound,
18396 info->base_decl);
18397 if (info->dimen[dim].stride)
18398 add_descr_info_field (subrange_die, DW_AT_byte_stride,
18399 info->dimen[dim].stride,
18400 info->base_decl);
18403 gen_type_die (info->element_type, context_die);
18404 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18406 if (get_AT (array_die, DW_AT_name))
18407 add_pubtype (type, array_die);
18410 #if 0
18411 static void
18412 gen_entry_point_die (tree decl, dw_die_ref context_die)
18414 tree origin = decl_ultimate_origin (decl);
18415 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18417 if (origin != NULL)
18418 add_abstract_origin_attribute (decl_die, origin);
18419 else
18421 add_name_and_src_coords_attributes (decl_die, decl);
18422 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18423 0, 0, context_die);
18426 if (DECL_ABSTRACT (decl))
18427 equate_decl_number_to_die (decl, decl_die);
18428 else
18429 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18431 #endif
18433 /* Walk through the list of incomplete types again, trying once more to
18434 emit full debugging info for them. */
18436 static void
18437 retry_incomplete_types (void)
18439 int i;
18441 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18442 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18443 DINFO_USAGE_DIR_USE))
18444 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18447 /* Determine what tag to use for a record type. */
18449 static enum dwarf_tag
18450 record_type_tag (tree type)
18452 if (! lang_hooks.types.classify_record)
18453 return DW_TAG_structure_type;
18455 switch (lang_hooks.types.classify_record (type))
18457 case RECORD_IS_STRUCT:
18458 return DW_TAG_structure_type;
18460 case RECORD_IS_CLASS:
18461 return DW_TAG_class_type;
18463 case RECORD_IS_INTERFACE:
18464 if (dwarf_version >= 3 || !dwarf_strict)
18465 return DW_TAG_interface_type;
18466 return DW_TAG_structure_type;
18468 default:
18469 gcc_unreachable ();
18473 /* Generate a DIE to represent an enumeration type. Note that these DIEs
18474 include all of the information about the enumeration values also. Each
18475 enumerated type name/value is listed as a child of the enumerated type
18476 DIE. */
18478 static dw_die_ref
18479 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18481 dw_die_ref type_die = lookup_type_die (type);
18483 if (type_die == NULL)
18485 type_die = new_die (DW_TAG_enumeration_type,
18486 scope_die_for (type, context_die), type);
18487 equate_type_number_to_die (type, type_die);
18488 add_name_attribute (type_die, type_tag (type));
18489 if (dwarf_version >= 4 || !dwarf_strict)
18491 if (ENUM_IS_SCOPED (type))
18492 add_AT_flag (type_die, DW_AT_enum_class, 1);
18493 if (ENUM_IS_OPAQUE (type))
18494 add_AT_flag (type_die, DW_AT_declaration, 1);
18497 else if (! TYPE_SIZE (type))
18498 return type_die;
18499 else
18500 remove_AT (type_die, DW_AT_declaration);
18502 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
18503 given enum type is incomplete, do not generate the DW_AT_byte_size
18504 attribute or the DW_AT_element_list attribute. */
18505 if (TYPE_SIZE (type))
18507 tree link;
18509 TREE_ASM_WRITTEN (type) = 1;
18510 add_byte_size_attribute (type_die, type);
18511 if (TYPE_STUB_DECL (type) != NULL_TREE)
18513 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18514 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18517 /* If the first reference to this type was as the return type of an
18518 inline function, then it may not have a parent. Fix this now. */
18519 if (type_die->die_parent == NULL)
18520 add_child_die (scope_die_for (type, context_die), type_die);
18522 for (link = TYPE_VALUES (type);
18523 link != NULL; link = TREE_CHAIN (link))
18525 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18526 tree value = TREE_VALUE (link);
18528 add_name_attribute (enum_die,
18529 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18531 if (TREE_CODE (value) == CONST_DECL)
18532 value = DECL_INITIAL (value);
18534 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18535 /* DWARF2 does not provide a way of indicating whether or
18536 not enumeration constants are signed or unsigned. GDB
18537 always assumes the values are signed, so we output all
18538 values as if they were signed. That means that
18539 enumeration constants with very large unsigned values
18540 will appear to have negative values in the debugger. */
18541 add_AT_int (enum_die, DW_AT_const_value,
18542 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18545 else
18546 add_AT_flag (type_die, DW_AT_declaration, 1);
18548 if (get_AT (type_die, DW_AT_name))
18549 add_pubtype (type, type_die);
18551 return type_die;
18554 /* Generate a DIE to represent either a real live formal parameter decl or to
18555 represent just the type of some formal parameter position in some function
18556 type.
18558 Note that this routine is a bit unusual because its argument may be a
18559 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18560 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18561 node. If it's the former then this function is being called to output a
18562 DIE to represent a formal parameter object (or some inlining thereof). If
18563 it's the latter, then this function is only being called to output a
18564 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18565 argument type of some subprogram type.
18566 If EMIT_NAME_P is true, name and source coordinate attributes
18567 are emitted. */
18569 static dw_die_ref
18570 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18571 dw_die_ref context_die)
18573 tree node_or_origin = node ? node : origin;
18574 tree ultimate_origin;
18575 dw_die_ref parm_die
18576 = new_die (DW_TAG_formal_parameter, context_die, node);
18578 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18580 case tcc_declaration:
18581 ultimate_origin = decl_ultimate_origin (node_or_origin);
18582 if (node || ultimate_origin)
18583 origin = ultimate_origin;
18584 if (origin != NULL)
18585 add_abstract_origin_attribute (parm_die, origin);
18586 else if (emit_name_p)
18587 add_name_and_src_coords_attributes (parm_die, node);
18588 if (origin == NULL
18589 || (! DECL_ABSTRACT (node_or_origin)
18590 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18591 decl_function_context
18592 (node_or_origin))))
18594 tree type = TREE_TYPE (node_or_origin);
18595 if (decl_by_reference_p (node_or_origin))
18596 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18597 context_die);
18598 else
18599 add_type_attribute (parm_die, type,
18600 TREE_READONLY (node_or_origin),
18601 TREE_THIS_VOLATILE (node_or_origin),
18602 context_die);
18604 if (origin == NULL && DECL_ARTIFICIAL (node))
18605 add_AT_flag (parm_die, DW_AT_artificial, 1);
18607 if (node && node != origin)
18608 equate_decl_number_to_die (node, parm_die);
18609 if (! DECL_ABSTRACT (node_or_origin))
18610 add_location_or_const_value_attribute (parm_die, node_or_origin,
18611 DW_AT_location);
18613 break;
18615 case tcc_type:
18616 /* We were called with some kind of a ..._TYPE node. */
18617 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18618 break;
18620 default:
18621 gcc_unreachable ();
18624 return parm_die;
18627 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18628 children DW_TAG_formal_parameter DIEs representing the arguments of the
18629 parameter pack.
18631 PARM_PACK must be a function parameter pack.
18632 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18633 must point to the subsequent arguments of the function PACK_ARG belongs to.
18634 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18635 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18636 following the last one for which a DIE was generated. */
18638 static dw_die_ref
18639 gen_formal_parameter_pack_die (tree parm_pack,
18640 tree pack_arg,
18641 dw_die_ref subr_die,
18642 tree *next_arg)
18644 tree arg;
18645 dw_die_ref parm_pack_die;
18647 gcc_assert (parm_pack
18648 && lang_hooks.function_parameter_pack_p (parm_pack)
18649 && subr_die);
18651 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18652 add_src_coords_attributes (parm_pack_die, parm_pack);
18654 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18656 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18657 parm_pack))
18658 break;
18659 gen_formal_parameter_die (arg, NULL,
18660 false /* Don't emit name attribute. */,
18661 parm_pack_die);
18663 if (next_arg)
18664 *next_arg = arg;
18665 return parm_pack_die;
18668 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18669 at the end of an (ANSI prototyped) formal parameters list. */
18671 static void
18672 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18674 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18677 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18678 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18679 parameters as specified in some function type specification (except for
18680 those which appear as part of a function *definition*). */
18682 static void
18683 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18685 tree link;
18686 tree formal_type = NULL;
18687 tree first_parm_type;
18688 tree arg;
18690 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18692 arg = DECL_ARGUMENTS (function_or_method_type);
18693 function_or_method_type = TREE_TYPE (function_or_method_type);
18695 else
18696 arg = NULL_TREE;
18698 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18700 /* Make our first pass over the list of formal parameter types and output a
18701 DW_TAG_formal_parameter DIE for each one. */
18702 for (link = first_parm_type; link; )
18704 dw_die_ref parm_die;
18706 formal_type = TREE_VALUE (link);
18707 if (formal_type == void_type_node)
18708 break;
18710 /* Output a (nameless) DIE to represent the formal parameter itself. */
18711 parm_die = gen_formal_parameter_die (formal_type, NULL,
18712 true /* Emit name attribute. */,
18713 context_die);
18714 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18715 && link == first_parm_type)
18717 add_AT_flag (parm_die, DW_AT_artificial, 1);
18718 if (dwarf_version >= 3 || !dwarf_strict)
18719 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18721 else if (arg && DECL_ARTIFICIAL (arg))
18722 add_AT_flag (parm_die, DW_AT_artificial, 1);
18724 link = TREE_CHAIN (link);
18725 if (arg)
18726 arg = DECL_CHAIN (arg);
18729 /* If this function type has an ellipsis, add a
18730 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18731 if (formal_type != void_type_node)
18732 gen_unspecified_parameters_die (function_or_method_type, context_die);
18734 /* Make our second (and final) pass over the list of formal parameter types
18735 and output DIEs to represent those types (as necessary). */
18736 for (link = TYPE_ARG_TYPES (function_or_method_type);
18737 link && TREE_VALUE (link);
18738 link = TREE_CHAIN (link))
18739 gen_type_die (TREE_VALUE (link), context_die);
18742 /* We want to generate the DIE for TYPE so that we can generate the
18743 die for MEMBER, which has been defined; we will need to refer back
18744 to the member declaration nested within TYPE. If we're trying to
18745 generate minimal debug info for TYPE, processing TYPE won't do the
18746 trick; we need to attach the member declaration by hand. */
18748 static void
18749 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18751 gen_type_die (type, context_die);
18753 /* If we're trying to avoid duplicate debug info, we may not have
18754 emitted the member decl for this function. Emit it now. */
18755 if (TYPE_STUB_DECL (type)
18756 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18757 && ! lookup_decl_die (member))
18759 dw_die_ref type_die;
18760 gcc_assert (!decl_ultimate_origin (member));
18762 push_decl_scope (type);
18763 type_die = lookup_type_die_strip_naming_typedef (type);
18764 if (TREE_CODE (member) == FUNCTION_DECL)
18765 gen_subprogram_die (member, type_die);
18766 else if (TREE_CODE (member) == FIELD_DECL)
18768 /* Ignore the nameless fields that are used to skip bits but handle
18769 C++ anonymous unions and structs. */
18770 if (DECL_NAME (member) != NULL_TREE
18771 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18772 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18774 gen_type_die (member_declared_type (member), type_die);
18775 gen_field_die (member, type_die);
18778 else
18779 gen_variable_die (member, NULL_TREE, type_die);
18781 pop_decl_scope ();
18785 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18786 may later generate inlined and/or out-of-line instances of. */
18788 static void
18789 dwarf2out_abstract_function (tree decl)
18791 dw_die_ref old_die;
18792 tree save_fn;
18793 tree context;
18794 int was_abstract;
18795 htab_t old_decl_loc_table;
18797 /* Make sure we have the actual abstract inline, not a clone. */
18798 decl = DECL_ORIGIN (decl);
18800 old_die = lookup_decl_die (decl);
18801 if (old_die && get_AT (old_die, DW_AT_inline))
18802 /* We've already generated the abstract instance. */
18803 return;
18805 /* We can be called while recursively when seeing block defining inlined subroutine
18806 DIE. Be sure to not clobber the outer location table nor use it or we would
18807 get locations in abstract instantces. */
18808 old_decl_loc_table = decl_loc_table;
18809 decl_loc_table = NULL;
18811 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18812 we don't get confused by DECL_ABSTRACT. */
18813 if (debug_info_level > DINFO_LEVEL_TERSE)
18815 context = decl_class_context (decl);
18816 if (context)
18817 gen_type_die_for_member
18818 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18821 /* Pretend we've just finished compiling this function. */
18822 save_fn = current_function_decl;
18823 current_function_decl = decl;
18824 push_cfun (DECL_STRUCT_FUNCTION (decl));
18826 was_abstract = DECL_ABSTRACT (decl);
18827 set_decl_abstract_flags (decl, 1);
18828 dwarf2out_decl (decl);
18829 if (! was_abstract)
18830 set_decl_abstract_flags (decl, 0);
18832 current_function_decl = save_fn;
18833 decl_loc_table = old_decl_loc_table;
18834 pop_cfun ();
18837 /* Helper function of premark_used_types() which gets called through
18838 htab_traverse.
18840 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18841 marked as unused by prune_unused_types. */
18843 static int
18844 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18846 tree type;
18847 dw_die_ref die;
18849 type = (tree) *slot;
18850 die = lookup_type_die (type);
18851 if (die != NULL)
18852 die->die_perennial_p = 1;
18853 return 1;
18856 /* Helper function of premark_types_used_by_global_vars which gets called
18857 through htab_traverse.
18859 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18860 marked as unused by prune_unused_types. The DIE of the type is marked
18861 only if the global variable using the type will actually be emitted. */
18863 static int
18864 premark_types_used_by_global_vars_helper (void **slot,
18865 void *data ATTRIBUTE_UNUSED)
18867 struct types_used_by_vars_entry *entry;
18868 dw_die_ref die;
18870 entry = (struct types_used_by_vars_entry *) *slot;
18871 gcc_assert (entry->type != NULL
18872 && entry->var_decl != NULL);
18873 die = lookup_type_die (entry->type);
18874 if (die)
18876 /* Ask cgraph if the global variable really is to be emitted.
18877 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18878 struct varpool_node *node = varpool_get_node (entry->var_decl);
18879 if (node && node->needed)
18881 die->die_perennial_p = 1;
18882 /* Keep the parent DIEs as well. */
18883 while ((die = die->die_parent) && die->die_perennial_p == 0)
18884 die->die_perennial_p = 1;
18887 return 1;
18890 /* Mark all members of used_types_hash as perennial. */
18892 static void
18893 premark_used_types (void)
18895 if (cfun && cfun->used_types_hash)
18896 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18899 /* Mark all members of types_used_by_vars_entry as perennial. */
18901 static void
18902 premark_types_used_by_global_vars (void)
18904 if (types_used_by_vars_hash)
18905 htab_traverse (types_used_by_vars_hash,
18906 premark_types_used_by_global_vars_helper, NULL);
18909 /* Generate a DIE to represent a declared function (either file-scope or
18910 block-local). */
18912 static void
18913 gen_subprogram_die (tree decl, dw_die_ref context_die)
18915 tree origin = decl_ultimate_origin (decl);
18916 dw_die_ref subr_die;
18917 tree outer_scope;
18918 dw_die_ref old_die = lookup_decl_die (decl);
18919 int declaration = (current_function_decl != decl
18920 || class_or_namespace_scope_p (context_die));
18922 premark_used_types ();
18924 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18925 started to generate the abstract instance of an inline, decided to output
18926 its containing class, and proceeded to emit the declaration of the inline
18927 from the member list for the class. If so, DECLARATION takes priority;
18928 we'll get back to the abstract instance when done with the class. */
18930 /* The class-scope declaration DIE must be the primary DIE. */
18931 if (origin && declaration && class_or_namespace_scope_p (context_die))
18933 origin = NULL;
18934 gcc_assert (!old_die);
18937 /* Now that the C++ front end lazily declares artificial member fns, we
18938 might need to retrofit the declaration into its class. */
18939 if (!declaration && !origin && !old_die
18940 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18941 && !class_or_namespace_scope_p (context_die)
18942 && debug_info_level > DINFO_LEVEL_TERSE)
18943 old_die = force_decl_die (decl);
18945 if (origin != NULL)
18947 gcc_assert (!declaration || local_scope_p (context_die));
18949 /* Fixup die_parent for the abstract instance of a nested
18950 inline function. */
18951 if (old_die && old_die->die_parent == NULL)
18952 add_child_die (context_die, old_die);
18954 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18955 add_abstract_origin_attribute (subr_die, origin);
18957 else if (old_die)
18959 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18960 struct dwarf_file_data * file_index = lookup_filename (s.file);
18962 if (!get_AT_flag (old_die, DW_AT_declaration)
18963 /* We can have a normal definition following an inline one in the
18964 case of redefinition of GNU C extern inlines.
18965 It seems reasonable to use AT_specification in this case. */
18966 && !get_AT (old_die, DW_AT_inline))
18968 /* Detect and ignore this case, where we are trying to output
18969 something we have already output. */
18970 return;
18973 /* If the definition comes from the same place as the declaration,
18974 maybe use the old DIE. We always want the DIE for this function
18975 that has the *_pc attributes to be under comp_unit_die so the
18976 debugger can find it. We also need to do this for abstract
18977 instances of inlines, since the spec requires the out-of-line copy
18978 to have the same parent. For local class methods, this doesn't
18979 apply; we just use the old DIE. */
18980 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18981 && (DECL_ARTIFICIAL (decl)
18982 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18983 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18984 == (unsigned) s.line))))
18986 subr_die = old_die;
18988 /* Clear out the declaration attribute and the formal parameters.
18989 Do not remove all children, because it is possible that this
18990 declaration die was forced using force_decl_die(). In such
18991 cases die that forced declaration die (e.g. TAG_imported_module)
18992 is one of the children that we do not want to remove. */
18993 remove_AT (subr_die, DW_AT_declaration);
18994 remove_AT (subr_die, DW_AT_object_pointer);
18995 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18997 else
18999 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19000 add_AT_specification (subr_die, old_die);
19001 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19002 add_AT_file (subr_die, DW_AT_decl_file, file_index);
19003 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19004 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19007 else
19009 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19011 if (TREE_PUBLIC (decl))
19012 add_AT_flag (subr_die, DW_AT_external, 1);
19014 add_name_and_src_coords_attributes (subr_die, decl);
19015 if (debug_info_level > DINFO_LEVEL_TERSE)
19017 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19018 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19019 0, 0, context_die);
19022 add_pure_or_virtual_attribute (subr_die, decl);
19023 if (DECL_ARTIFICIAL (decl))
19024 add_AT_flag (subr_die, DW_AT_artificial, 1);
19026 add_accessibility_attribute (subr_die, decl);
19029 if (declaration)
19031 if (!old_die || !get_AT (old_die, DW_AT_inline))
19033 add_AT_flag (subr_die, DW_AT_declaration, 1);
19035 /* If this is an explicit function declaration then generate
19036 a DW_AT_explicit attribute. */
19037 if (lang_hooks.decls.function_decl_explicit_p (decl)
19038 && (dwarf_version >= 3 || !dwarf_strict))
19039 add_AT_flag (subr_die, DW_AT_explicit, 1);
19041 /* The first time we see a member function, it is in the context of
19042 the class to which it belongs. We make sure of this by emitting
19043 the class first. The next time is the definition, which is
19044 handled above. The two may come from the same source text.
19046 Note that force_decl_die() forces function declaration die. It is
19047 later reused to represent definition. */
19048 equate_decl_number_to_die (decl, subr_die);
19051 else if (DECL_ABSTRACT (decl))
19053 if (DECL_DECLARED_INLINE_P (decl))
19055 if (cgraph_function_possibly_inlined_p (decl))
19056 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19057 else
19058 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19060 else
19062 if (cgraph_function_possibly_inlined_p (decl))
19063 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19064 else
19065 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19068 if (DECL_DECLARED_INLINE_P (decl)
19069 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19070 add_AT_flag (subr_die, DW_AT_artificial, 1);
19072 equate_decl_number_to_die (decl, subr_die);
19074 else if (!DECL_EXTERNAL (decl))
19076 HOST_WIDE_INT cfa_fb_offset;
19078 if (!old_die || !get_AT (old_die, DW_AT_inline))
19079 equate_decl_number_to_die (decl, subr_die);
19081 if (!flag_reorder_blocks_and_partition)
19083 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19084 if (fde->dw_fde_begin)
19086 /* We have already generated the labels. */
19087 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19088 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19090 else
19092 /* Create start/end labels and add the range. */
19093 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19094 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19095 current_function_funcdef_no);
19096 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19097 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19098 current_function_funcdef_no);
19099 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19102 #if VMS_DEBUGGING_INFO
19103 /* HP OpenVMS Industry Standard 64: DWARF Extensions
19104 Section 2.3 Prologue and Epilogue Attributes:
19105 When a breakpoint is set on entry to a function, it is generally
19106 desirable for execution to be suspended, not on the very first
19107 instruction of the function, but rather at a point after the
19108 function's frame has been set up, after any language defined local
19109 declaration processing has been completed, and before execution of
19110 the first statement of the function begins. Debuggers generally
19111 cannot properly determine where this point is. Similarly for a
19112 breakpoint set on exit from a function. The prologue and epilogue
19113 attributes allow a compiler to communicate the location(s) to use. */
19116 if (fde->dw_fde_vms_end_prologue)
19117 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19118 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19120 if (fde->dw_fde_vms_begin_epilogue)
19121 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19122 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19124 #endif
19126 add_pubname (decl, subr_die);
19127 add_arange (decl, subr_die);
19129 else
19130 { /* Generate pubnames entries for the split function code
19131 ranges. */
19132 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19134 if (fde->dw_fde_switched_sections)
19136 if (dwarf_version >= 3 || !dwarf_strict)
19138 /* We should use ranges for non-contiguous code section
19139 addresses. Use the actual code range for the initial
19140 section, since the HOT/COLD labels might precede an
19141 alignment offset. */
19142 bool range_list_added = false;
19143 if (fde->in_std_section)
19145 add_ranges_by_labels (subr_die,
19146 fde->dw_fde_begin,
19147 fde->dw_fde_end,
19148 &range_list_added);
19149 add_ranges_by_labels (subr_die,
19150 fde->dw_fde_unlikely_section_label,
19151 fde->dw_fde_unlikely_section_end_label,
19152 &range_list_added);
19154 else
19156 add_ranges_by_labels (subr_die,
19157 fde->dw_fde_begin,
19158 fde->dw_fde_end,
19159 &range_list_added);
19160 add_ranges_by_labels (subr_die,
19161 fde->dw_fde_hot_section_label,
19162 fde->dw_fde_hot_section_end_label,
19163 &range_list_added);
19165 add_pubname (decl, subr_die);
19166 if (range_list_added)
19167 add_ranges (NULL);
19169 else
19171 /* There is no real support in DW2 for this .. so we make
19172 a work-around. First, emit the pub name for the segment
19173 containing the function label. Then make and emit a
19174 simplified subprogram DIE for the second segment with the
19175 name pre-fixed by __hot/cold_sect_of_. We use the same
19176 linkage name for the second die so that gdb will find both
19177 sections when given "b foo". */
19178 const char *name = NULL;
19179 tree decl_name = DECL_NAME (decl);
19180 dw_die_ref seg_die;
19182 /* Do the 'primary' section. */
19183 add_AT_lbl_id (subr_die, DW_AT_low_pc,
19184 fde->dw_fde_begin);
19185 add_AT_lbl_id (subr_die, DW_AT_high_pc,
19186 fde->dw_fde_end);
19187 /* Add it. */
19188 add_pubname (decl, subr_die);
19189 add_arange (decl, subr_die);
19191 /* Build a minimal DIE for the secondary section. */
19192 seg_die = new_die (DW_TAG_subprogram,
19193 subr_die->die_parent, decl);
19195 if (TREE_PUBLIC (decl))
19196 add_AT_flag (seg_die, DW_AT_external, 1);
19198 if (decl_name != NULL
19199 && IDENTIFIER_POINTER (decl_name) != NULL)
19201 name = dwarf2_name (decl, 1);
19202 if (! DECL_ARTIFICIAL (decl))
19203 add_src_coords_attributes (seg_die, decl);
19205 add_linkage_name (seg_die, decl);
19207 gcc_assert (name!=NULL);
19208 add_pure_or_virtual_attribute (seg_die, decl);
19209 if (DECL_ARTIFICIAL (decl))
19210 add_AT_flag (seg_die, DW_AT_artificial, 1);
19212 if (fde->in_std_section)
19214 name = concat ("__cold_sect_of_", name, NULL);
19215 add_AT_lbl_id (seg_die, DW_AT_low_pc,
19216 fde->dw_fde_unlikely_section_label);
19217 add_AT_lbl_id (seg_die, DW_AT_high_pc,
19218 fde->dw_fde_unlikely_section_end_label);
19220 else
19222 name = concat ("__hot_sect_of_", name, NULL);
19223 add_AT_lbl_id (seg_die, DW_AT_low_pc,
19224 fde->dw_fde_hot_section_label);
19225 add_AT_lbl_id (seg_die, DW_AT_high_pc,
19226 fde->dw_fde_hot_section_end_label);
19228 add_name_attribute (seg_die, name);
19229 add_pubname_string (name, seg_die);
19230 add_arange (decl, seg_die);
19233 else
19235 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19236 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19237 add_pubname (decl, subr_die);
19238 add_arange (decl, subr_die);
19242 #ifdef MIPS_DEBUGGING_INFO
19243 /* Add a reference to the FDE for this routine. */
19244 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19245 #endif
19247 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19249 /* We define the "frame base" as the function's CFA. This is more
19250 convenient for several reasons: (1) It's stable across the prologue
19251 and epilogue, which makes it better than just a frame pointer,
19252 (2) With dwarf3, there exists a one-byte encoding that allows us
19253 to reference the .debug_frame data by proxy, but failing that,
19254 (3) We can at least reuse the code inspection and interpretation
19255 code that determines the CFA position at various points in the
19256 function. */
19257 if (dwarf_version >= 3)
19259 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19260 add_AT_loc (subr_die, DW_AT_frame_base, op);
19262 else
19264 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19265 if (list->dw_loc_next)
19266 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19267 else
19268 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19271 /* Compute a displacement from the "steady-state frame pointer" to
19272 the CFA. The former is what all stack slots and argument slots
19273 will reference in the rtl; the later is what we've told the
19274 debugger about. We'll need to adjust all frame_base references
19275 by this displacement. */
19276 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19278 if (cfun->static_chain_decl)
19279 add_AT_location_description (subr_die, DW_AT_static_link,
19280 loc_list_from_tree (cfun->static_chain_decl, 2));
19283 /* Generate child dies for template paramaters. */
19284 if (debug_info_level > DINFO_LEVEL_TERSE)
19285 gen_generic_params_dies (decl);
19287 /* Now output descriptions of the arguments for this function. This gets
19288 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19289 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19290 `...' at the end of the formal parameter list. In order to find out if
19291 there was a trailing ellipsis or not, we must instead look at the type
19292 associated with the FUNCTION_DECL. This will be a node of type
19293 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19294 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19295 an ellipsis at the end. */
19297 /* In the case where we are describing a mere function declaration, all we
19298 need to do here (and all we *can* do here) is to describe the *types* of
19299 its formal parameters. */
19300 if (debug_info_level <= DINFO_LEVEL_TERSE)
19302 else if (declaration)
19303 gen_formal_types_die (decl, subr_die);
19304 else
19306 /* Generate DIEs to represent all known formal parameters. */
19307 tree parm = DECL_ARGUMENTS (decl);
19308 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19309 tree generic_decl_parm = generic_decl
19310 ? DECL_ARGUMENTS (generic_decl)
19311 : NULL;
19313 /* Now we want to walk the list of parameters of the function and
19314 emit their relevant DIEs.
19316 We consider the case of DECL being an instance of a generic function
19317 as well as it being a normal function.
19319 If DECL is an instance of a generic function we walk the
19320 parameters of the generic function declaration _and_ the parameters of
19321 DECL itself. This is useful because we want to emit specific DIEs for
19322 function parameter packs and those are declared as part of the
19323 generic function declaration. In that particular case,
19324 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19325 That DIE has children DIEs representing the set of arguments
19326 of the pack. Note that the set of pack arguments can be empty.
19327 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19328 children DIE.
19330 Otherwise, we just consider the parameters of DECL. */
19331 while (generic_decl_parm || parm)
19333 if (generic_decl_parm
19334 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19335 gen_formal_parameter_pack_die (generic_decl_parm,
19336 parm, subr_die,
19337 &parm);
19338 else if (parm)
19340 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19342 if (parm == DECL_ARGUMENTS (decl)
19343 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19344 && parm_die
19345 && (dwarf_version >= 3 || !dwarf_strict))
19346 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19348 parm = DECL_CHAIN (parm);
19351 if (generic_decl_parm)
19352 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19355 /* Decide whether we need an unspecified_parameters DIE at the end.
19356 There are 2 more cases to do this for: 1) the ansi ... declaration -
19357 this is detectable when the end of the arg list is not a
19358 void_type_node 2) an unprototyped function declaration (not a
19359 definition). This just means that we have no info about the
19360 parameters at all. */
19361 if (prototype_p (TREE_TYPE (decl)))
19363 /* This is the prototyped case, check for.... */
19364 if (stdarg_p (TREE_TYPE (decl)))
19365 gen_unspecified_parameters_die (decl, subr_die);
19367 else if (DECL_INITIAL (decl) == NULL_TREE)
19368 gen_unspecified_parameters_die (decl, subr_die);
19371 /* Output Dwarf info for all of the stuff within the body of the function
19372 (if it has one - it may be just a declaration). */
19373 outer_scope = DECL_INITIAL (decl);
19375 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19376 a function. This BLOCK actually represents the outermost binding contour
19377 for the function, i.e. the contour in which the function's formal
19378 parameters and labels get declared. Curiously, it appears that the front
19379 end doesn't actually put the PARM_DECL nodes for the current function onto
19380 the BLOCK_VARS list for this outer scope, but are strung off of the
19381 DECL_ARGUMENTS list for the function instead.
19383 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19384 the LABEL_DECL nodes for the function however, and we output DWARF info
19385 for those in decls_for_scope. Just within the `outer_scope' there will be
19386 a BLOCK node representing the function's outermost pair of curly braces,
19387 and any blocks used for the base and member initializers of a C++
19388 constructor function. */
19389 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19391 /* Emit a DW_TAG_variable DIE for a named return value. */
19392 if (DECL_NAME (DECL_RESULT (decl)))
19393 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19395 current_function_has_inlines = 0;
19396 decls_for_scope (outer_scope, subr_die, 0);
19398 /* Add the calling convention attribute if requested. */
19399 add_calling_convention_attribute (subr_die, decl);
19403 /* Returns a hash value for X (which really is a die_struct). */
19405 static hashval_t
19406 common_block_die_table_hash (const void *x)
19408 const_dw_die_ref d = (const_dw_die_ref) x;
19409 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19412 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19413 as decl_id and die_parent of die_struct Y. */
19415 static int
19416 common_block_die_table_eq (const void *x, const void *y)
19418 const_dw_die_ref d = (const_dw_die_ref) x;
19419 const_dw_die_ref e = (const_dw_die_ref) y;
19420 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19423 /* Generate a DIE to represent a declared data object.
19424 Either DECL or ORIGIN must be non-null. */
19426 static void
19427 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19429 HOST_WIDE_INT off;
19430 tree com_decl;
19431 tree decl_or_origin = decl ? decl : origin;
19432 tree ultimate_origin;
19433 dw_die_ref var_die;
19434 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19435 dw_die_ref origin_die;
19436 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19437 || class_or_namespace_scope_p (context_die));
19438 bool specialization_p = false;
19440 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19441 if (decl || ultimate_origin)
19442 origin = ultimate_origin;
19443 com_decl = fortran_common (decl_or_origin, &off);
19445 /* Symbol in common gets emitted as a child of the common block, in the form
19446 of a data member. */
19447 if (com_decl)
19449 dw_die_ref com_die;
19450 dw_loc_list_ref loc;
19451 die_node com_die_arg;
19453 var_die = lookup_decl_die (decl_or_origin);
19454 if (var_die)
19456 if (get_AT (var_die, DW_AT_location) == NULL)
19458 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19459 if (loc)
19461 if (off)
19463 /* Optimize the common case. */
19464 if (single_element_loc_list_p (loc)
19465 && loc->expr->dw_loc_opc == DW_OP_addr
19466 && loc->expr->dw_loc_next == NULL
19467 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19468 == SYMBOL_REF)
19469 loc->expr->dw_loc_oprnd1.v.val_addr
19470 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19471 else
19472 loc_list_plus_const (loc, off);
19474 add_AT_location_description (var_die, DW_AT_location, loc);
19475 remove_AT (var_die, DW_AT_declaration);
19478 return;
19481 if (common_block_die_table == NULL)
19482 common_block_die_table
19483 = htab_create_ggc (10, common_block_die_table_hash,
19484 common_block_die_table_eq, NULL);
19486 com_die_arg.decl_id = DECL_UID (com_decl);
19487 com_die_arg.die_parent = context_die;
19488 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19489 loc = loc_list_from_tree (com_decl, 2);
19490 if (com_die == NULL)
19492 const char *cnam
19493 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19494 void **slot;
19496 com_die = new_die (DW_TAG_common_block, context_die, decl);
19497 add_name_and_src_coords_attributes (com_die, com_decl);
19498 if (loc)
19500 add_AT_location_description (com_die, DW_AT_location, loc);
19501 /* Avoid sharing the same loc descriptor between
19502 DW_TAG_common_block and DW_TAG_variable. */
19503 loc = loc_list_from_tree (com_decl, 2);
19505 else if (DECL_EXTERNAL (decl))
19506 add_AT_flag (com_die, DW_AT_declaration, 1);
19507 add_pubname_string (cnam, com_die); /* ??? needed? */
19508 com_die->decl_id = DECL_UID (com_decl);
19509 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19510 *slot = (void *) com_die;
19512 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19514 add_AT_location_description (com_die, DW_AT_location, loc);
19515 loc = loc_list_from_tree (com_decl, 2);
19516 remove_AT (com_die, DW_AT_declaration);
19518 var_die = new_die (DW_TAG_variable, com_die, decl);
19519 add_name_and_src_coords_attributes (var_die, decl);
19520 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19521 TREE_THIS_VOLATILE (decl), context_die);
19522 add_AT_flag (var_die, DW_AT_external, 1);
19523 if (loc)
19525 if (off)
19527 /* Optimize the common case. */
19528 if (single_element_loc_list_p (loc)
19529 && loc->expr->dw_loc_opc == DW_OP_addr
19530 && loc->expr->dw_loc_next == NULL
19531 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19532 loc->expr->dw_loc_oprnd1.v.val_addr
19533 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19534 else
19535 loc_list_plus_const (loc, off);
19537 add_AT_location_description (var_die, DW_AT_location, loc);
19539 else if (DECL_EXTERNAL (decl))
19540 add_AT_flag (var_die, DW_AT_declaration, 1);
19541 equate_decl_number_to_die (decl, var_die);
19542 return;
19545 /* If the compiler emitted a definition for the DECL declaration
19546 and if we already emitted a DIE for it, don't emit a second
19547 DIE for it again. Allow re-declarations of DECLs that are
19548 inside functions, though. */
19549 if (old_die && declaration && !local_scope_p (context_die))
19550 return;
19552 /* For static data members, the declaration in the class is supposed
19553 to have DW_TAG_member tag; the specification should still be
19554 DW_TAG_variable referencing the DW_TAG_member DIE. */
19555 if (declaration && class_scope_p (context_die))
19556 var_die = new_die (DW_TAG_member, context_die, decl);
19557 else
19558 var_die = new_die (DW_TAG_variable, context_die, decl);
19560 origin_die = NULL;
19561 if (origin != NULL)
19562 origin_die = add_abstract_origin_attribute (var_die, origin);
19564 /* Loop unrolling can create multiple blocks that refer to the same
19565 static variable, so we must test for the DW_AT_declaration flag.
19567 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19568 copy decls and set the DECL_ABSTRACT flag on them instead of
19569 sharing them.
19571 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19573 ??? The declare_in_namespace support causes us to get two DIEs for one
19574 variable, both of which are declarations. We want to avoid considering
19575 one to be a specification, so we must test that this DIE is not a
19576 declaration. */
19577 else if (old_die && TREE_STATIC (decl) && ! declaration
19578 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19580 /* This is a definition of a C++ class level static. */
19581 add_AT_specification (var_die, old_die);
19582 specialization_p = true;
19583 if (DECL_NAME (decl))
19585 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19586 struct dwarf_file_data * file_index = lookup_filename (s.file);
19588 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19589 add_AT_file (var_die, DW_AT_decl_file, file_index);
19591 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19592 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19594 if (old_die->die_tag == DW_TAG_member)
19595 add_linkage_name (var_die, decl);
19598 else
19599 add_name_and_src_coords_attributes (var_die, decl);
19601 if ((origin == NULL && !specialization_p)
19602 || (origin != NULL
19603 && !DECL_ABSTRACT (decl_or_origin)
19604 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19605 decl_function_context
19606 (decl_or_origin))))
19608 tree type = TREE_TYPE (decl_or_origin);
19610 if (decl_by_reference_p (decl_or_origin))
19611 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19612 else
19613 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19614 TREE_THIS_VOLATILE (decl_or_origin), context_die);
19617 if (origin == NULL && !specialization_p)
19619 if (TREE_PUBLIC (decl))
19620 add_AT_flag (var_die, DW_AT_external, 1);
19622 if (DECL_ARTIFICIAL (decl))
19623 add_AT_flag (var_die, DW_AT_artificial, 1);
19625 add_accessibility_attribute (var_die, decl);
19628 if (declaration)
19629 add_AT_flag (var_die, DW_AT_declaration, 1);
19631 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19632 equate_decl_number_to_die (decl, var_die);
19634 if (! declaration
19635 && (! DECL_ABSTRACT (decl_or_origin)
19636 /* Local static vars are shared between all clones/inlines,
19637 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19638 already set. */
19639 || (TREE_CODE (decl_or_origin) == VAR_DECL
19640 && TREE_STATIC (decl_or_origin)
19641 && DECL_RTL_SET_P (decl_or_origin)))
19642 /* When abstract origin already has DW_AT_location attribute, no need
19643 to add it again. */
19644 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19646 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19647 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19648 defer_location (decl_or_origin, var_die);
19649 else
19650 add_location_or_const_value_attribute (var_die,
19651 decl_or_origin,
19652 DW_AT_location);
19653 add_pubname (decl_or_origin, var_die);
19655 else
19656 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19659 /* Generate a DIE to represent a named constant. */
19661 static void
19662 gen_const_die (tree decl, dw_die_ref context_die)
19664 dw_die_ref const_die;
19665 tree type = TREE_TYPE (decl);
19667 const_die = new_die (DW_TAG_constant, context_die, decl);
19668 add_name_and_src_coords_attributes (const_die, decl);
19669 add_type_attribute (const_die, type, 1, 0, context_die);
19670 if (TREE_PUBLIC (decl))
19671 add_AT_flag (const_die, DW_AT_external, 1);
19672 if (DECL_ARTIFICIAL (decl))
19673 add_AT_flag (const_die, DW_AT_artificial, 1);
19674 tree_add_const_value_attribute_for_decl (const_die, decl);
19677 /* Generate a DIE to represent a label identifier. */
19679 static void
19680 gen_label_die (tree decl, dw_die_ref context_die)
19682 tree origin = decl_ultimate_origin (decl);
19683 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19684 rtx insn;
19685 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19687 if (origin != NULL)
19688 add_abstract_origin_attribute (lbl_die, origin);
19689 else
19690 add_name_and_src_coords_attributes (lbl_die, decl);
19692 if (DECL_ABSTRACT (decl))
19693 equate_decl_number_to_die (decl, lbl_die);
19694 else
19696 insn = DECL_RTL_IF_SET (decl);
19698 /* Deleted labels are programmer specified labels which have been
19699 eliminated because of various optimizations. We still emit them
19700 here so that it is possible to put breakpoints on them. */
19701 if (insn
19702 && (LABEL_P (insn)
19703 || ((NOTE_P (insn)
19704 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19706 /* When optimization is enabled (via -O) some parts of the compiler
19707 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19708 represent source-level labels which were explicitly declared by
19709 the user. This really shouldn't be happening though, so catch
19710 it if it ever does happen. */
19711 gcc_assert (!INSN_DELETED_P (insn));
19713 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19714 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19719 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19720 attributes to the DIE for a block STMT, to describe where the inlined
19721 function was called from. This is similar to add_src_coords_attributes. */
19723 static inline void
19724 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19726 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19728 if (dwarf_version >= 3 || !dwarf_strict)
19730 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19731 add_AT_unsigned (die, DW_AT_call_line, s.line);
19736 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19737 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19739 static inline void
19740 add_high_low_attributes (tree stmt, dw_die_ref die)
19742 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19744 if (BLOCK_FRAGMENT_CHAIN (stmt)
19745 && (dwarf_version >= 3 || !dwarf_strict))
19747 tree chain;
19749 if (inlined_function_outer_scope_p (stmt))
19751 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19752 BLOCK_NUMBER (stmt));
19753 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19756 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19758 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19761 add_ranges (chain);
19762 chain = BLOCK_FRAGMENT_CHAIN (chain);
19764 while (chain);
19765 add_ranges (NULL);
19767 else
19769 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19770 BLOCK_NUMBER (stmt));
19771 add_AT_lbl_id (die, DW_AT_low_pc, label);
19772 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19773 BLOCK_NUMBER (stmt));
19774 add_AT_lbl_id (die, DW_AT_high_pc, label);
19778 /* Generate a DIE for a lexical block. */
19780 static void
19781 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19783 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19785 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19786 add_high_low_attributes (stmt, stmt_die);
19788 decls_for_scope (stmt, stmt_die, depth);
19791 /* Generate a DIE for an inlined subprogram. */
19793 static void
19794 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19796 tree decl;
19798 /* The instance of function that is effectively being inlined shall not
19799 be abstract. */
19800 gcc_assert (! BLOCK_ABSTRACT (stmt));
19802 decl = block_ultimate_origin (stmt);
19804 /* Emit info for the abstract instance first, if we haven't yet. We
19805 must emit this even if the block is abstract, otherwise when we
19806 emit the block below (or elsewhere), we may end up trying to emit
19807 a die whose origin die hasn't been emitted, and crashing. */
19808 dwarf2out_abstract_function (decl);
19810 if (! BLOCK_ABSTRACT (stmt))
19812 dw_die_ref subr_die
19813 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19815 add_abstract_origin_attribute (subr_die, decl);
19816 if (TREE_ASM_WRITTEN (stmt))
19817 add_high_low_attributes (stmt, subr_die);
19818 add_call_src_coords_attributes (stmt, subr_die);
19820 decls_for_scope (stmt, subr_die, depth);
19821 current_function_has_inlines = 1;
19825 /* Generate a DIE for a field in a record, or structure. */
19827 static void
19828 gen_field_die (tree decl, dw_die_ref context_die)
19830 dw_die_ref decl_die;
19832 if (TREE_TYPE (decl) == error_mark_node)
19833 return;
19835 decl_die = new_die (DW_TAG_member, context_die, decl);
19836 add_name_and_src_coords_attributes (decl_die, decl);
19837 add_type_attribute (decl_die, member_declared_type (decl),
19838 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19839 context_die);
19841 if (DECL_BIT_FIELD_TYPE (decl))
19843 add_byte_size_attribute (decl_die, decl);
19844 add_bit_size_attribute (decl_die, decl);
19845 add_bit_offset_attribute (decl_die, decl);
19848 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19849 add_data_member_location_attribute (decl_die, decl);
19851 if (DECL_ARTIFICIAL (decl))
19852 add_AT_flag (decl_die, DW_AT_artificial, 1);
19854 add_accessibility_attribute (decl_die, decl);
19856 /* Equate decl number to die, so that we can look up this decl later on. */
19857 equate_decl_number_to_die (decl, decl_die);
19860 #if 0
19861 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19862 Use modified_type_die instead.
19863 We keep this code here just in case these types of DIEs may be needed to
19864 represent certain things in other languages (e.g. Pascal) someday. */
19866 static void
19867 gen_pointer_type_die (tree type, dw_die_ref context_die)
19869 dw_die_ref ptr_die
19870 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19872 equate_type_number_to_die (type, ptr_die);
19873 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19874 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19877 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19878 Use modified_type_die instead.
19879 We keep this code here just in case these types of DIEs may be needed to
19880 represent certain things in other languages (e.g. Pascal) someday. */
19882 static void
19883 gen_reference_type_die (tree type, dw_die_ref context_die)
19885 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19887 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19888 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19889 else
19890 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19892 equate_type_number_to_die (type, ref_die);
19893 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19894 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19896 #endif
19898 /* Generate a DIE for a pointer to a member type. */
19900 static void
19901 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19903 dw_die_ref ptr_die
19904 = new_die (DW_TAG_ptr_to_member_type,
19905 scope_die_for (type, context_die), type);
19907 equate_type_number_to_die (type, ptr_die);
19908 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19909 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19910 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19913 /* Generate the DIE for the compilation unit. */
19915 static dw_die_ref
19916 gen_compile_unit_die (const char *filename)
19918 dw_die_ref die;
19919 char producer[250];
19920 const char *language_string = lang_hooks.name;
19921 int language;
19923 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19925 if (filename)
19927 add_name_attribute (die, filename);
19928 /* Don't add cwd for <built-in>. */
19929 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19930 add_comp_dir_attribute (die);
19933 sprintf (producer, "%s %s", language_string, version_string);
19935 #ifdef MIPS_DEBUGGING_INFO
19936 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19937 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19938 not appear in the producer string, the debugger reaches the conclusion
19939 that the object file is stripped and has no debugging information.
19940 To get the MIPS/SGI debugger to believe that there is debugging
19941 information in the object file, we add a -g to the producer string. */
19942 if (debug_info_level > DINFO_LEVEL_TERSE)
19943 strcat (producer, " -g");
19944 #endif
19946 add_AT_string (die, DW_AT_producer, producer);
19948 /* If our producer is LTO try to figure out a common language to use
19949 from the global list of translation units. */
19950 if (strcmp (language_string, "GNU GIMPLE") == 0)
19952 unsigned i;
19953 tree t;
19954 const char *common_lang = NULL;
19956 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
19958 if (!TRANSLATION_UNIT_LANGUAGE (t))
19959 continue;
19960 if (!common_lang)
19961 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19962 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19964 else if (strncmp (common_lang, "GNU C", 5) == 0
19965 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19966 /* Mixing C and C++ is ok, use C++ in that case. */
19967 common_lang = "GNU C++";
19968 else
19970 /* Fall back to C. */
19971 common_lang = NULL;
19972 break;
19976 if (common_lang)
19977 language_string = common_lang;
19980 language = DW_LANG_C89;
19981 if (strcmp (language_string, "GNU C++") == 0)
19982 language = DW_LANG_C_plus_plus;
19983 else if (strcmp (language_string, "GNU F77") == 0)
19984 language = DW_LANG_Fortran77;
19985 else if (strcmp (language_string, "GNU Pascal") == 0)
19986 language = DW_LANG_Pascal83;
19987 else if (dwarf_version >= 3 || !dwarf_strict)
19989 if (strcmp (language_string, "GNU Ada") == 0)
19990 language = DW_LANG_Ada95;
19991 else if (strcmp (language_string, "GNU Fortran") == 0)
19992 language = DW_LANG_Fortran95;
19993 else if (strcmp (language_string, "GNU Java") == 0)
19994 language = DW_LANG_Java;
19995 else if (strcmp (language_string, "GNU Objective-C") == 0)
19996 language = DW_LANG_ObjC;
19997 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19998 language = DW_LANG_ObjC_plus_plus;
20001 add_AT_unsigned (die, DW_AT_language, language);
20003 switch (language)
20005 case DW_LANG_Fortran77:
20006 case DW_LANG_Fortran90:
20007 case DW_LANG_Fortran95:
20008 /* Fortran has case insensitive identifiers and the front-end
20009 lowercases everything. */
20010 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20011 break;
20012 default:
20013 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20014 break;
20016 return die;
20019 /* Generate the DIE for a base class. */
20021 static void
20022 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20024 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20026 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20027 add_data_member_location_attribute (die, binfo);
20029 if (BINFO_VIRTUAL_P (binfo))
20030 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20032 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20033 children, otherwise the default is DW_ACCESS_public. In DWARF2
20034 the default has always been DW_ACCESS_private. */
20035 if (access == access_public_node)
20037 if (dwarf_version == 2
20038 || context_die->die_tag == DW_TAG_class_type)
20039 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20041 else if (access == access_protected_node)
20042 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20043 else if (dwarf_version > 2
20044 && context_die->die_tag != DW_TAG_class_type)
20045 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20048 /* Generate a DIE for a class member. */
20050 static void
20051 gen_member_die (tree type, dw_die_ref context_die)
20053 tree member;
20054 tree binfo = TYPE_BINFO (type);
20055 dw_die_ref child;
20057 /* If this is not an incomplete type, output descriptions of each of its
20058 members. Note that as we output the DIEs necessary to represent the
20059 members of this record or union type, we will also be trying to output
20060 DIEs to represent the *types* of those members. However the `type'
20061 function (above) will specifically avoid generating type DIEs for member
20062 types *within* the list of member DIEs for this (containing) type except
20063 for those types (of members) which are explicitly marked as also being
20064 members of this (containing) type themselves. The g++ front- end can
20065 force any given type to be treated as a member of some other (containing)
20066 type by setting the TYPE_CONTEXT of the given (member) type to point to
20067 the TREE node representing the appropriate (containing) type. */
20069 /* First output info about the base classes. */
20070 if (binfo)
20072 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20073 int i;
20074 tree base;
20076 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20077 gen_inheritance_die (base,
20078 (accesses ? VEC_index (tree, accesses, i)
20079 : access_public_node), context_die);
20082 /* Now output info about the data members and type members. */
20083 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20085 /* If we thought we were generating minimal debug info for TYPE
20086 and then changed our minds, some of the member declarations
20087 may have already been defined. Don't define them again, but
20088 do put them in the right order. */
20090 child = lookup_decl_die (member);
20091 if (child)
20092 splice_child_die (context_die, child);
20093 else
20094 gen_decl_die (member, NULL, context_die);
20097 /* Now output info about the function members (if any). */
20098 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20100 /* Don't include clones in the member list. */
20101 if (DECL_ABSTRACT_ORIGIN (member))
20102 continue;
20104 child = lookup_decl_die (member);
20105 if (child)
20106 splice_child_die (context_die, child);
20107 else
20108 gen_decl_die (member, NULL, context_die);
20112 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20113 is set, we pretend that the type was never defined, so we only get the
20114 member DIEs needed by later specification DIEs. */
20116 static void
20117 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20118 enum debug_info_usage usage)
20120 dw_die_ref type_die = lookup_type_die (type);
20121 dw_die_ref scope_die = 0;
20122 int nested = 0;
20123 int complete = (TYPE_SIZE (type)
20124 && (! TYPE_STUB_DECL (type)
20125 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20126 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20127 complete = complete && should_emit_struct_debug (type, usage);
20129 if (type_die && ! complete)
20130 return;
20132 if (TYPE_CONTEXT (type) != NULL_TREE
20133 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20134 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20135 nested = 1;
20137 scope_die = scope_die_for (type, context_die);
20139 if (! type_die || (nested && is_cu_die (scope_die)))
20140 /* First occurrence of type or toplevel definition of nested class. */
20142 dw_die_ref old_die = type_die;
20144 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20145 ? record_type_tag (type) : DW_TAG_union_type,
20146 scope_die, type);
20147 equate_type_number_to_die (type, type_die);
20148 if (old_die)
20149 add_AT_specification (type_die, old_die);
20150 else
20151 add_name_attribute (type_die, type_tag (type));
20153 else
20154 remove_AT (type_die, DW_AT_declaration);
20156 /* Generate child dies for template paramaters. */
20157 if (debug_info_level > DINFO_LEVEL_TERSE
20158 && COMPLETE_TYPE_P (type))
20159 schedule_generic_params_dies_gen (type);
20161 /* If this type has been completed, then give it a byte_size attribute and
20162 then give a list of members. */
20163 if (complete && !ns_decl)
20165 /* Prevent infinite recursion in cases where the type of some member of
20166 this type is expressed in terms of this type itself. */
20167 TREE_ASM_WRITTEN (type) = 1;
20168 add_byte_size_attribute (type_die, type);
20169 if (TYPE_STUB_DECL (type) != NULL_TREE)
20171 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20172 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20175 /* If the first reference to this type was as the return type of an
20176 inline function, then it may not have a parent. Fix this now. */
20177 if (type_die->die_parent == NULL)
20178 add_child_die (scope_die, type_die);
20180 push_decl_scope (type);
20181 gen_member_die (type, type_die);
20182 pop_decl_scope ();
20184 /* GNU extension: Record what type our vtable lives in. */
20185 if (TYPE_VFIELD (type))
20187 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20189 gen_type_die (vtype, context_die);
20190 add_AT_die_ref (type_die, DW_AT_containing_type,
20191 lookup_type_die (vtype));
20194 else
20196 add_AT_flag (type_die, DW_AT_declaration, 1);
20198 /* We don't need to do this for function-local types. */
20199 if (TYPE_STUB_DECL (type)
20200 && ! decl_function_context (TYPE_STUB_DECL (type)))
20201 VEC_safe_push (tree, gc, incomplete_types, type);
20204 if (get_AT (type_die, DW_AT_name))
20205 add_pubtype (type, type_die);
20208 /* Generate a DIE for a subroutine _type_. */
20210 static void
20211 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20213 tree return_type = TREE_TYPE (type);
20214 dw_die_ref subr_die
20215 = new_die (DW_TAG_subroutine_type,
20216 scope_die_for (type, context_die), type);
20218 equate_type_number_to_die (type, subr_die);
20219 add_prototyped_attribute (subr_die, type);
20220 add_type_attribute (subr_die, return_type, 0, 0, context_die);
20221 gen_formal_types_die (type, subr_die);
20223 if (get_AT (subr_die, DW_AT_name))
20224 add_pubtype (type, subr_die);
20227 /* Generate a DIE for a type definition. */
20229 static void
20230 gen_typedef_die (tree decl, dw_die_ref context_die)
20232 dw_die_ref type_die;
20233 tree origin;
20235 if (TREE_ASM_WRITTEN (decl))
20236 return;
20238 TREE_ASM_WRITTEN (decl) = 1;
20239 type_die = new_die (DW_TAG_typedef, context_die, decl);
20240 origin = decl_ultimate_origin (decl);
20241 if (origin != NULL)
20242 add_abstract_origin_attribute (type_die, origin);
20243 else
20245 tree type;
20247 add_name_and_src_coords_attributes (type_die, decl);
20248 if (DECL_ORIGINAL_TYPE (decl))
20250 type = DECL_ORIGINAL_TYPE (decl);
20252 gcc_assert (type != TREE_TYPE (decl));
20253 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20255 else
20257 type = TREE_TYPE (decl);
20259 if (is_naming_typedef_decl (TYPE_NAME (type)))
20261 /* Here, we are in the case of decl being a typedef naming
20262 an anonymous type, e.g:
20263 typedef struct {...} foo;
20264 In that case TREE_TYPE (decl) is not a typedef variant
20265 type and TYPE_NAME of the anonymous type is set to the
20266 TYPE_DECL of the typedef. This construct is emitted by
20267 the C++ FE.
20269 TYPE is the anonymous struct named by the typedef
20270 DECL. As we need the DW_AT_type attribute of the
20271 DW_TAG_typedef to point to the DIE of TYPE, let's
20272 generate that DIE right away. add_type_attribute
20273 called below will then pick (via lookup_type_die) that
20274 anonymous struct DIE. */
20275 if (!TREE_ASM_WRITTEN (type))
20276 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20280 add_type_attribute (type_die, type, TREE_READONLY (decl),
20281 TREE_THIS_VOLATILE (decl), context_die);
20283 if (is_naming_typedef_decl (decl))
20284 /* We want that all subsequent calls to lookup_type_die with
20285 TYPE in argument yield the DW_TAG_typedef we have just
20286 created. */
20287 equate_type_number_to_die (type, type_die);
20289 add_accessibility_attribute (type_die, decl);
20292 if (DECL_ABSTRACT (decl))
20293 equate_decl_number_to_die (decl, type_die);
20295 if (get_AT (type_die, DW_AT_name))
20296 add_pubtype (decl, type_die);
20299 /* Generate a DIE for a struct, class, enum or union type. */
20301 static void
20302 gen_tagged_type_die (tree type,
20303 dw_die_ref context_die,
20304 enum debug_info_usage usage)
20306 int need_pop;
20308 if (type == NULL_TREE
20309 || !is_tagged_type (type))
20310 return;
20312 /* If this is a nested type whose containing class hasn't been written
20313 out yet, writing it out will cover this one, too. This does not apply
20314 to instantiations of member class templates; they need to be added to
20315 the containing class as they are generated. FIXME: This hurts the
20316 idea of combining type decls from multiple TUs, since we can't predict
20317 what set of template instantiations we'll get. */
20318 if (TYPE_CONTEXT (type)
20319 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20320 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20322 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20324 if (TREE_ASM_WRITTEN (type))
20325 return;
20327 /* If that failed, attach ourselves to the stub. */
20328 push_decl_scope (TYPE_CONTEXT (type));
20329 context_die = lookup_type_die (TYPE_CONTEXT (type));
20330 need_pop = 1;
20332 else if (TYPE_CONTEXT (type) != NULL_TREE
20333 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20335 /* If this type is local to a function that hasn't been written
20336 out yet, use a NULL context for now; it will be fixed up in
20337 decls_for_scope. */
20338 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20339 /* A declaration DIE doesn't count; nested types need to go in the
20340 specification. */
20341 if (context_die && is_declaration_die (context_die))
20342 context_die = NULL;
20343 need_pop = 0;
20345 else
20347 context_die = declare_in_namespace (type, context_die);
20348 need_pop = 0;
20351 if (TREE_CODE (type) == ENUMERAL_TYPE)
20353 /* This might have been written out by the call to
20354 declare_in_namespace. */
20355 if (!TREE_ASM_WRITTEN (type))
20356 gen_enumeration_type_die (type, context_die);
20358 else
20359 gen_struct_or_union_type_die (type, context_die, usage);
20361 if (need_pop)
20362 pop_decl_scope ();
20364 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20365 it up if it is ever completed. gen_*_type_die will set it for us
20366 when appropriate. */
20369 /* Generate a type description DIE. */
20371 static void
20372 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20373 enum debug_info_usage usage)
20375 struct array_descr_info info;
20377 if (type == NULL_TREE || type == error_mark_node)
20378 return;
20380 if (TYPE_NAME (type) != NULL_TREE
20381 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20382 && is_redundant_typedef (TYPE_NAME (type))
20383 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20384 /* The DECL of this type is a typedef we don't want to emit debug
20385 info for but we want debug info for its underlying typedef.
20386 This can happen for e.g, the injected-class-name of a C++
20387 type. */
20388 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20390 /* If TYPE is a typedef type variant, let's generate debug info
20391 for the parent typedef which TYPE is a type of. */
20392 if (typedef_variant_p (type))
20394 if (TREE_ASM_WRITTEN (type))
20395 return;
20397 /* Prevent broken recursion; we can't hand off to the same type. */
20398 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20400 /* Use the DIE of the containing namespace as the parent DIE of
20401 the type description DIE we want to generate. */
20402 if (DECL_CONTEXT (TYPE_NAME (type))
20403 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20404 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20406 TREE_ASM_WRITTEN (type) = 1;
20408 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20409 return;
20412 /* If type is an anonymous tagged type named by a typedef, let's
20413 generate debug info for the typedef. */
20414 if (is_naming_typedef_decl (TYPE_NAME (type)))
20416 /* Use the DIE of the containing namespace as the parent DIE of
20417 the type description DIE we want to generate. */
20418 if (DECL_CONTEXT (TYPE_NAME (type))
20419 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20420 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20422 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20423 return;
20426 /* If this is an array type with hidden descriptor, handle it first. */
20427 if (!TREE_ASM_WRITTEN (type)
20428 && lang_hooks.types.get_array_descr_info
20429 && lang_hooks.types.get_array_descr_info (type, &info)
20430 && (dwarf_version >= 3 || !dwarf_strict))
20432 gen_descr_array_type_die (type, &info, context_die);
20433 TREE_ASM_WRITTEN (type) = 1;
20434 return;
20437 /* We are going to output a DIE to represent the unqualified version
20438 of this type (i.e. without any const or volatile qualifiers) so
20439 get the main variant (i.e. the unqualified version) of this type
20440 now. (Vectors are special because the debugging info is in the
20441 cloned type itself). */
20442 if (TREE_CODE (type) != VECTOR_TYPE)
20443 type = type_main_variant (type);
20445 if (TREE_ASM_WRITTEN (type))
20446 return;
20448 switch (TREE_CODE (type))
20450 case ERROR_MARK:
20451 break;
20453 case POINTER_TYPE:
20454 case REFERENCE_TYPE:
20455 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20456 ensures that the gen_type_die recursion will terminate even if the
20457 type is recursive. Recursive types are possible in Ada. */
20458 /* ??? We could perhaps do this for all types before the switch
20459 statement. */
20460 TREE_ASM_WRITTEN (type) = 1;
20462 /* For these types, all that is required is that we output a DIE (or a
20463 set of DIEs) to represent the "basis" type. */
20464 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20465 DINFO_USAGE_IND_USE);
20466 break;
20468 case OFFSET_TYPE:
20469 /* This code is used for C++ pointer-to-data-member types.
20470 Output a description of the relevant class type. */
20471 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20472 DINFO_USAGE_IND_USE);
20474 /* Output a description of the type of the object pointed to. */
20475 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20476 DINFO_USAGE_IND_USE);
20478 /* Now output a DIE to represent this pointer-to-data-member type
20479 itself. */
20480 gen_ptr_to_mbr_type_die (type, context_die);
20481 break;
20483 case FUNCTION_TYPE:
20484 /* Force out return type (in case it wasn't forced out already). */
20485 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20486 DINFO_USAGE_DIR_USE);
20487 gen_subroutine_type_die (type, context_die);
20488 break;
20490 case METHOD_TYPE:
20491 /* Force out return type (in case it wasn't forced out already). */
20492 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20493 DINFO_USAGE_DIR_USE);
20494 gen_subroutine_type_die (type, context_die);
20495 break;
20497 case ARRAY_TYPE:
20498 gen_array_type_die (type, context_die);
20499 break;
20501 case VECTOR_TYPE:
20502 gen_array_type_die (type, context_die);
20503 break;
20505 case ENUMERAL_TYPE:
20506 case RECORD_TYPE:
20507 case UNION_TYPE:
20508 case QUAL_UNION_TYPE:
20509 gen_tagged_type_die (type, context_die, usage);
20510 return;
20512 case VOID_TYPE:
20513 case INTEGER_TYPE:
20514 case REAL_TYPE:
20515 case FIXED_POINT_TYPE:
20516 case COMPLEX_TYPE:
20517 case BOOLEAN_TYPE:
20518 /* No DIEs needed for fundamental types. */
20519 break;
20521 case NULLPTR_TYPE:
20522 case LANG_TYPE:
20523 /* Just use DW_TAG_unspecified_type. */
20525 dw_die_ref type_die = lookup_type_die (type);
20526 if (type_die == NULL)
20528 tree name = TYPE_NAME (type);
20529 if (TREE_CODE (name) == TYPE_DECL)
20530 name = DECL_NAME (name);
20531 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20532 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20533 equate_type_number_to_die (type, type_die);
20536 break;
20538 default:
20539 gcc_unreachable ();
20542 TREE_ASM_WRITTEN (type) = 1;
20545 static void
20546 gen_type_die (tree type, dw_die_ref context_die)
20548 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20551 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20552 things which are local to the given block. */
20554 static void
20555 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20557 int must_output_die = 0;
20558 bool inlined_func;
20560 /* Ignore blocks that are NULL. */
20561 if (stmt == NULL_TREE)
20562 return;
20564 inlined_func = inlined_function_outer_scope_p (stmt);
20566 /* If the block is one fragment of a non-contiguous block, do not
20567 process the variables, since they will have been done by the
20568 origin block. Do process subblocks. */
20569 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20571 tree sub;
20573 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20574 gen_block_die (sub, context_die, depth + 1);
20576 return;
20579 /* Determine if we need to output any Dwarf DIEs at all to represent this
20580 block. */
20581 if (inlined_func)
20582 /* The outer scopes for inlinings *must* always be represented. We
20583 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20584 must_output_die = 1;
20585 else
20587 /* Determine if this block directly contains any "significant"
20588 local declarations which we will need to output DIEs for. */
20589 if (debug_info_level > DINFO_LEVEL_TERSE)
20590 /* We are not in terse mode so *any* local declaration counts
20591 as being a "significant" one. */
20592 must_output_die = ((BLOCK_VARS (stmt) != NULL
20593 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20594 && (TREE_USED (stmt)
20595 || TREE_ASM_WRITTEN (stmt)
20596 || BLOCK_ABSTRACT (stmt)));
20597 else if ((TREE_USED (stmt)
20598 || TREE_ASM_WRITTEN (stmt)
20599 || BLOCK_ABSTRACT (stmt))
20600 && !dwarf2out_ignore_block (stmt))
20601 must_output_die = 1;
20604 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20605 DIE for any block which contains no significant local declarations at
20606 all. Rather, in such cases we just call `decls_for_scope' so that any
20607 needed Dwarf info for any sub-blocks will get properly generated. Note
20608 that in terse mode, our definition of what constitutes a "significant"
20609 local declaration gets restricted to include only inlined function
20610 instances and local (nested) function definitions. */
20611 if (must_output_die)
20613 if (inlined_func)
20615 /* If STMT block is abstract, that means we have been called
20616 indirectly from dwarf2out_abstract_function.
20617 That function rightfully marks the descendent blocks (of
20618 the abstract function it is dealing with) as being abstract,
20619 precisely to prevent us from emitting any
20620 DW_TAG_inlined_subroutine DIE as a descendent
20621 of an abstract function instance. So in that case, we should
20622 not call gen_inlined_subroutine_die.
20624 Later though, when cgraph asks dwarf2out to emit info
20625 for the concrete instance of the function decl into which
20626 the concrete instance of STMT got inlined, the later will lead
20627 to the generation of a DW_TAG_inlined_subroutine DIE. */
20628 if (! BLOCK_ABSTRACT (stmt))
20629 gen_inlined_subroutine_die (stmt, context_die, depth);
20631 else
20632 gen_lexical_block_die (stmt, context_die, depth);
20634 else
20635 decls_for_scope (stmt, context_die, depth);
20638 /* Process variable DECL (or variable with origin ORIGIN) within
20639 block STMT and add it to CONTEXT_DIE. */
20640 static void
20641 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20643 dw_die_ref die;
20644 tree decl_or_origin = decl ? decl : origin;
20646 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20647 die = lookup_decl_die (decl_or_origin);
20648 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20649 && TYPE_DECL_IS_STUB (decl_or_origin))
20650 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20651 else
20652 die = NULL;
20654 if (die != NULL && die->die_parent == NULL)
20655 add_child_die (context_die, die);
20656 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20657 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20658 stmt, context_die);
20659 else
20660 gen_decl_die (decl, origin, context_die);
20663 /* Generate all of the decls declared within a given scope and (recursively)
20664 all of its sub-blocks. */
20666 static void
20667 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20669 tree decl;
20670 unsigned int i;
20671 tree subblocks;
20673 /* Ignore NULL blocks. */
20674 if (stmt == NULL_TREE)
20675 return;
20677 /* Output the DIEs to represent all of the data objects and typedefs
20678 declared directly within this block but not within any nested
20679 sub-blocks. Also, nested function and tag DIEs have been
20680 generated with a parent of NULL; fix that up now. */
20681 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20682 process_scope_var (stmt, decl, NULL_TREE, context_die);
20683 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20684 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20685 context_die);
20687 /* If we're at -g1, we're not interested in subblocks. */
20688 if (debug_info_level <= DINFO_LEVEL_TERSE)
20689 return;
20691 /* Output the DIEs to represent all sub-blocks (and the items declared
20692 therein) of this block. */
20693 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20694 subblocks != NULL;
20695 subblocks = BLOCK_CHAIN (subblocks))
20696 gen_block_die (subblocks, context_die, depth + 1);
20699 /* Is this a typedef we can avoid emitting? */
20701 static inline int
20702 is_redundant_typedef (const_tree decl)
20704 if (TYPE_DECL_IS_STUB (decl))
20705 return 1;
20707 if (DECL_ARTIFICIAL (decl)
20708 && DECL_CONTEXT (decl)
20709 && is_tagged_type (DECL_CONTEXT (decl))
20710 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20711 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20712 /* Also ignore the artificial member typedef for the class name. */
20713 return 1;
20715 return 0;
20718 /* Return TRUE if TYPE is a typedef that names a type for linkage
20719 purposes. This kind of typedefs is produced by the C++ FE for
20720 constructs like:
20722 typedef struct {...} foo;
20724 In that case, there is no typedef variant type produced for foo.
20725 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20726 struct type. */
20728 static bool
20729 is_naming_typedef_decl (const_tree decl)
20731 if (decl == NULL_TREE
20732 || TREE_CODE (decl) != TYPE_DECL
20733 || !is_tagged_type (TREE_TYPE (decl))
20734 || DECL_IS_BUILTIN (decl)
20735 || is_redundant_typedef (decl)
20736 /* It looks like Ada produces TYPE_DECLs that are very similar
20737 to C++ naming typedefs but that have different
20738 semantics. Let's be specific to c++ for now. */
20739 || !is_cxx ())
20740 return FALSE;
20742 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20743 && TYPE_NAME (TREE_TYPE (decl)) == decl
20744 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20745 != TYPE_NAME (TREE_TYPE (decl))));
20748 /* Returns the DIE for a context. */
20750 static inline dw_die_ref
20751 get_context_die (tree context)
20753 if (context)
20755 /* Find die that represents this context. */
20756 if (TYPE_P (context))
20757 return force_type_die (TYPE_MAIN_VARIANT (context));
20758 else
20759 return force_decl_die (context);
20761 return comp_unit_die ();
20764 /* Returns the DIE for decl. A DIE will always be returned. */
20766 static dw_die_ref
20767 force_decl_die (tree decl)
20769 dw_die_ref decl_die;
20770 unsigned saved_external_flag;
20771 tree save_fn = NULL_TREE;
20772 decl_die = lookup_decl_die (decl);
20773 if (!decl_die)
20775 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20777 decl_die = lookup_decl_die (decl);
20778 if (decl_die)
20779 return decl_die;
20781 switch (TREE_CODE (decl))
20783 case FUNCTION_DECL:
20784 /* Clear current_function_decl, so that gen_subprogram_die thinks
20785 that this is a declaration. At this point, we just want to force
20786 declaration die. */
20787 save_fn = current_function_decl;
20788 current_function_decl = NULL_TREE;
20789 gen_subprogram_die (decl, context_die);
20790 current_function_decl = save_fn;
20791 break;
20793 case VAR_DECL:
20794 /* Set external flag to force declaration die. Restore it after
20795 gen_decl_die() call. */
20796 saved_external_flag = DECL_EXTERNAL (decl);
20797 DECL_EXTERNAL (decl) = 1;
20798 gen_decl_die (decl, NULL, context_die);
20799 DECL_EXTERNAL (decl) = saved_external_flag;
20800 break;
20802 case NAMESPACE_DECL:
20803 if (dwarf_version >= 3 || !dwarf_strict)
20804 dwarf2out_decl (decl);
20805 else
20806 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20807 decl_die = comp_unit_die ();
20808 break;
20810 case TRANSLATION_UNIT_DECL:
20811 decl_die = comp_unit_die ();
20812 break;
20814 default:
20815 gcc_unreachable ();
20818 /* We should be able to find the DIE now. */
20819 if (!decl_die)
20820 decl_die = lookup_decl_die (decl);
20821 gcc_assert (decl_die);
20824 return decl_die;
20827 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20828 always returned. */
20830 static dw_die_ref
20831 force_type_die (tree type)
20833 dw_die_ref type_die;
20835 type_die = lookup_type_die (type);
20836 if (!type_die)
20838 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20840 type_die = modified_type_die (type, TYPE_READONLY (type),
20841 TYPE_VOLATILE (type), context_die);
20842 gcc_assert (type_die);
20844 return type_die;
20847 /* Force out any required namespaces to be able to output DECL,
20848 and return the new context_die for it, if it's changed. */
20850 static dw_die_ref
20851 setup_namespace_context (tree thing, dw_die_ref context_die)
20853 tree context = (DECL_P (thing)
20854 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20855 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20856 /* Force out the namespace. */
20857 context_die = force_decl_die (context);
20859 return context_die;
20862 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20863 type) within its namespace, if appropriate.
20865 For compatibility with older debuggers, namespace DIEs only contain
20866 declarations; all definitions are emitted at CU scope. */
20868 static dw_die_ref
20869 declare_in_namespace (tree thing, dw_die_ref context_die)
20871 dw_die_ref ns_context;
20873 if (debug_info_level <= DINFO_LEVEL_TERSE)
20874 return context_die;
20876 /* If this decl is from an inlined function, then don't try to emit it in its
20877 namespace, as we will get confused. It would have already been emitted
20878 when the abstract instance of the inline function was emitted anyways. */
20879 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20880 return context_die;
20882 ns_context = setup_namespace_context (thing, context_die);
20884 if (ns_context != context_die)
20886 if (is_fortran ())
20887 return ns_context;
20888 if (DECL_P (thing))
20889 gen_decl_die (thing, NULL, ns_context);
20890 else
20891 gen_type_die (thing, ns_context);
20893 return context_die;
20896 /* Generate a DIE for a namespace or namespace alias. */
20898 static void
20899 gen_namespace_die (tree decl, dw_die_ref context_die)
20901 dw_die_ref namespace_die;
20903 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20904 they are an alias of. */
20905 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20907 /* Output a real namespace or module. */
20908 context_die = setup_namespace_context (decl, comp_unit_die ());
20909 namespace_die = new_die (is_fortran ()
20910 ? DW_TAG_module : DW_TAG_namespace,
20911 context_die, decl);
20912 /* For Fortran modules defined in different CU don't add src coords. */
20913 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20915 const char *name = dwarf2_name (decl, 0);
20916 if (name)
20917 add_name_attribute (namespace_die, name);
20919 else
20920 add_name_and_src_coords_attributes (namespace_die, decl);
20921 if (DECL_EXTERNAL (decl))
20922 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20923 equate_decl_number_to_die (decl, namespace_die);
20925 else
20927 /* Output a namespace alias. */
20929 /* Force out the namespace we are an alias of, if necessary. */
20930 dw_die_ref origin_die
20931 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20933 if (DECL_FILE_SCOPE_P (decl)
20934 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20935 context_die = setup_namespace_context (decl, comp_unit_die ());
20936 /* Now create the namespace alias DIE. */
20937 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20938 add_name_and_src_coords_attributes (namespace_die, decl);
20939 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20940 equate_decl_number_to_die (decl, namespace_die);
20944 /* Generate Dwarf debug information for a decl described by DECL.
20945 The return value is currently only meaningful for PARM_DECLs,
20946 for all other decls it returns NULL. */
20948 static dw_die_ref
20949 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20951 tree decl_or_origin = decl ? decl : origin;
20952 tree class_origin = NULL, ultimate_origin;
20954 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20955 return NULL;
20957 switch (TREE_CODE (decl_or_origin))
20959 case ERROR_MARK:
20960 break;
20962 case CONST_DECL:
20963 if (!is_fortran () && !is_ada ())
20965 /* The individual enumerators of an enum type get output when we output
20966 the Dwarf representation of the relevant enum type itself. */
20967 break;
20970 /* Emit its type. */
20971 gen_type_die (TREE_TYPE (decl), context_die);
20973 /* And its containing namespace. */
20974 context_die = declare_in_namespace (decl, context_die);
20976 gen_const_die (decl, context_die);
20977 break;
20979 case FUNCTION_DECL:
20980 /* Don't output any DIEs to represent mere function declarations,
20981 unless they are class members or explicit block externs. */
20982 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20983 && DECL_FILE_SCOPE_P (decl_or_origin)
20984 && (current_function_decl == NULL_TREE
20985 || DECL_ARTIFICIAL (decl_or_origin)))
20986 break;
20988 #if 0
20989 /* FIXME */
20990 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20991 on local redeclarations of global functions. That seems broken. */
20992 if (current_function_decl != decl)
20993 /* This is only a declaration. */;
20994 #endif
20996 /* If we're emitting a clone, emit info for the abstract instance. */
20997 if (origin || DECL_ORIGIN (decl) != decl)
20998 dwarf2out_abstract_function (origin
20999 ? DECL_ORIGIN (origin)
21000 : DECL_ABSTRACT_ORIGIN (decl));
21002 /* If we're emitting an out-of-line copy of an inline function,
21003 emit info for the abstract instance and set up to refer to it. */
21004 else if (cgraph_function_possibly_inlined_p (decl)
21005 && ! DECL_ABSTRACT (decl)
21006 && ! class_or_namespace_scope_p (context_die)
21007 /* dwarf2out_abstract_function won't emit a die if this is just
21008 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21009 that case, because that works only if we have a die. */
21010 && DECL_INITIAL (decl) != NULL_TREE)
21012 dwarf2out_abstract_function (decl);
21013 set_decl_origin_self (decl);
21016 /* Otherwise we're emitting the primary DIE for this decl. */
21017 else if (debug_info_level > DINFO_LEVEL_TERSE)
21019 /* Before we describe the FUNCTION_DECL itself, make sure that we
21020 have its containing type. */
21021 if (!origin)
21022 origin = decl_class_context (decl);
21023 if (origin != NULL_TREE)
21024 gen_type_die (origin, context_die);
21026 /* And its return type. */
21027 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21029 /* And its virtual context. */
21030 if (DECL_VINDEX (decl) != NULL_TREE)
21031 gen_type_die (DECL_CONTEXT (decl), context_die);
21033 /* Make sure we have a member DIE for decl. */
21034 if (origin != NULL_TREE)
21035 gen_type_die_for_member (origin, decl, context_die);
21037 /* And its containing namespace. */
21038 context_die = declare_in_namespace (decl, context_die);
21041 /* Now output a DIE to represent the function itself. */
21042 if (decl)
21043 gen_subprogram_die (decl, context_die);
21044 break;
21046 case TYPE_DECL:
21047 /* If we are in terse mode, don't generate any DIEs to represent any
21048 actual typedefs. */
21049 if (debug_info_level <= DINFO_LEVEL_TERSE)
21050 break;
21052 /* In the special case of a TYPE_DECL node representing the declaration
21053 of some type tag, if the given TYPE_DECL is marked as having been
21054 instantiated from some other (original) TYPE_DECL node (e.g. one which
21055 was generated within the original definition of an inline function) we
21056 used to generate a special (abbreviated) DW_TAG_structure_type,
21057 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21058 should be actually referencing those DIEs, as variable DIEs with that
21059 type would be emitted already in the abstract origin, so it was always
21060 removed during unused type prunning. Don't add anything in this
21061 case. */
21062 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21063 break;
21065 if (is_redundant_typedef (decl))
21066 gen_type_die (TREE_TYPE (decl), context_die);
21067 else
21068 /* Output a DIE to represent the typedef itself. */
21069 gen_typedef_die (decl, context_die);
21070 break;
21072 case LABEL_DECL:
21073 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21074 gen_label_die (decl, context_die);
21075 break;
21077 case VAR_DECL:
21078 case RESULT_DECL:
21079 /* If we are in terse mode, don't generate any DIEs to represent any
21080 variable declarations or definitions. */
21081 if (debug_info_level <= DINFO_LEVEL_TERSE)
21082 break;
21084 /* Output any DIEs that are needed to specify the type of this data
21085 object. */
21086 if (decl_by_reference_p (decl_or_origin))
21087 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21088 else
21089 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21091 /* And its containing type. */
21092 class_origin = decl_class_context (decl_or_origin);
21093 if (class_origin != NULL_TREE)
21094 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21096 /* And its containing namespace. */
21097 context_die = declare_in_namespace (decl_or_origin, context_die);
21099 /* Now output the DIE to represent the data object itself. This gets
21100 complicated because of the possibility that the VAR_DECL really
21101 represents an inlined instance of a formal parameter for an inline
21102 function. */
21103 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21104 if (ultimate_origin != NULL_TREE
21105 && TREE_CODE (ultimate_origin) == PARM_DECL)
21106 gen_formal_parameter_die (decl, origin,
21107 true /* Emit name attribute. */,
21108 context_die);
21109 else
21110 gen_variable_die (decl, origin, context_die);
21111 break;
21113 case FIELD_DECL:
21114 /* Ignore the nameless fields that are used to skip bits but handle C++
21115 anonymous unions and structs. */
21116 if (DECL_NAME (decl) != NULL_TREE
21117 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21118 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21120 gen_type_die (member_declared_type (decl), context_die);
21121 gen_field_die (decl, context_die);
21123 break;
21125 case PARM_DECL:
21126 if (DECL_BY_REFERENCE (decl_or_origin))
21127 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21128 else
21129 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21130 return gen_formal_parameter_die (decl, origin,
21131 true /* Emit name attribute. */,
21132 context_die);
21134 case NAMESPACE_DECL:
21135 case IMPORTED_DECL:
21136 if (dwarf_version >= 3 || !dwarf_strict)
21137 gen_namespace_die (decl, context_die);
21138 break;
21140 default:
21141 /* Probably some frontend-internal decl. Assume we don't care. */
21142 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21143 break;
21146 return NULL;
21149 /* Output debug information for global decl DECL. Called from toplev.c after
21150 compilation proper has finished. */
21152 static void
21153 dwarf2out_global_decl (tree decl)
21155 /* Output DWARF2 information for file-scope tentative data object
21156 declarations, file-scope (extern) function declarations (which
21157 had no corresponding body) and file-scope tagged type declarations
21158 and definitions which have not yet been forced out. */
21159 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21160 dwarf2out_decl (decl);
21163 /* Output debug information for type decl DECL. Called from toplev.c
21164 and from language front ends (to record built-in types). */
21165 static void
21166 dwarf2out_type_decl (tree decl, int local)
21168 if (!local)
21169 dwarf2out_decl (decl);
21172 /* Output debug information for imported module or decl DECL.
21173 NAME is non-NULL name in the lexical block if the decl has been renamed.
21174 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21175 that DECL belongs to.
21176 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21177 static void
21178 dwarf2out_imported_module_or_decl_1 (tree decl,
21179 tree name,
21180 tree lexical_block,
21181 dw_die_ref lexical_block_die)
21183 expanded_location xloc;
21184 dw_die_ref imported_die = NULL;
21185 dw_die_ref at_import_die;
21187 if (TREE_CODE (decl) == IMPORTED_DECL)
21189 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21190 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21191 gcc_assert (decl);
21193 else
21194 xloc = expand_location (input_location);
21196 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21198 at_import_die = force_type_die (TREE_TYPE (decl));
21199 /* For namespace N { typedef void T; } using N::T; base_type_die
21200 returns NULL, but DW_TAG_imported_declaration requires
21201 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21202 if (!at_import_die)
21204 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21205 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21206 at_import_die = lookup_type_die (TREE_TYPE (decl));
21207 gcc_assert (at_import_die);
21210 else
21212 at_import_die = lookup_decl_die (decl);
21213 if (!at_import_die)
21215 /* If we're trying to avoid duplicate debug info, we may not have
21216 emitted the member decl for this field. Emit it now. */
21217 if (TREE_CODE (decl) == FIELD_DECL)
21219 tree type = DECL_CONTEXT (decl);
21221 if (TYPE_CONTEXT (type)
21222 && TYPE_P (TYPE_CONTEXT (type))
21223 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21224 DINFO_USAGE_DIR_USE))
21225 return;
21226 gen_type_die_for_member (type, decl,
21227 get_context_die (TYPE_CONTEXT (type)));
21229 at_import_die = force_decl_die (decl);
21233 if (TREE_CODE (decl) == NAMESPACE_DECL)
21235 if (dwarf_version >= 3 || !dwarf_strict)
21236 imported_die = new_die (DW_TAG_imported_module,
21237 lexical_block_die,
21238 lexical_block);
21239 else
21240 return;
21242 else
21243 imported_die = new_die (DW_TAG_imported_declaration,
21244 lexical_block_die,
21245 lexical_block);
21247 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21248 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21249 if (name)
21250 add_AT_string (imported_die, DW_AT_name,
21251 IDENTIFIER_POINTER (name));
21252 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21255 /* Output debug information for imported module or decl DECL.
21256 NAME is non-NULL name in context if the decl has been renamed.
21257 CHILD is true if decl is one of the renamed decls as part of
21258 importing whole module. */
21260 static void
21261 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21262 bool child)
21264 /* dw_die_ref at_import_die; */
21265 dw_die_ref scope_die;
21267 if (debug_info_level <= DINFO_LEVEL_TERSE)
21268 return;
21270 gcc_assert (decl);
21272 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21273 We need decl DIE for reference and scope die. First, get DIE for the decl
21274 itself. */
21276 /* Get the scope die for decl context. Use comp_unit_die for global module
21277 or decl. If die is not found for non globals, force new die. */
21278 if (context
21279 && TYPE_P (context)
21280 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21281 return;
21283 if (!(dwarf_version >= 3 || !dwarf_strict))
21284 return;
21286 scope_die = get_context_die (context);
21288 if (child)
21290 gcc_assert (scope_die->die_child);
21291 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21292 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21293 scope_die = scope_die->die_child;
21296 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21297 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21301 /* Write the debugging output for DECL. */
21303 void
21304 dwarf2out_decl (tree decl)
21306 dw_die_ref context_die = comp_unit_die ();
21308 switch (TREE_CODE (decl))
21310 case ERROR_MARK:
21311 return;
21313 case FUNCTION_DECL:
21314 /* What we would really like to do here is to filter out all mere
21315 file-scope declarations of file-scope functions which are never
21316 referenced later within this translation unit (and keep all of ones
21317 that *are* referenced later on) but we aren't clairvoyant, so we have
21318 no idea which functions will be referenced in the future (i.e. later
21319 on within the current translation unit). So here we just ignore all
21320 file-scope function declarations which are not also definitions. If
21321 and when the debugger needs to know something about these functions,
21322 it will have to hunt around and find the DWARF information associated
21323 with the definition of the function.
21325 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21326 nodes represent definitions and which ones represent mere
21327 declarations. We have to check DECL_INITIAL instead. That's because
21328 the C front-end supports some weird semantics for "extern inline"
21329 function definitions. These can get inlined within the current
21330 translation unit (and thus, we need to generate Dwarf info for their
21331 abstract instances so that the Dwarf info for the concrete inlined
21332 instances can have something to refer to) but the compiler never
21333 generates any out-of-lines instances of such things (despite the fact
21334 that they *are* definitions).
21336 The important point is that the C front-end marks these "extern
21337 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21338 them anyway. Note that the C++ front-end also plays some similar games
21339 for inline function definitions appearing within include files which
21340 also contain `#pragma interface' pragmas. */
21341 if (DECL_INITIAL (decl) == NULL_TREE)
21342 return;
21344 /* If we're a nested function, initially use a parent of NULL; if we're
21345 a plain function, this will be fixed up in decls_for_scope. If
21346 we're a method, it will be ignored, since we already have a DIE. */
21347 if (decl_function_context (decl)
21348 /* But if we're in terse mode, we don't care about scope. */
21349 && debug_info_level > DINFO_LEVEL_TERSE)
21350 context_die = NULL;
21351 break;
21353 case VAR_DECL:
21354 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21355 declaration and if the declaration was never even referenced from
21356 within this entire compilation unit. We suppress these DIEs in
21357 order to save space in the .debug section (by eliminating entries
21358 which are probably useless). Note that we must not suppress
21359 block-local extern declarations (whether used or not) because that
21360 would screw-up the debugger's name lookup mechanism and cause it to
21361 miss things which really ought to be in scope at a given point. */
21362 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21363 return;
21365 /* For local statics lookup proper context die. */
21366 if (TREE_STATIC (decl) && decl_function_context (decl))
21367 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21369 /* If we are in terse mode, don't generate any DIEs to represent any
21370 variable declarations or definitions. */
21371 if (debug_info_level <= DINFO_LEVEL_TERSE)
21372 return;
21373 break;
21375 case CONST_DECL:
21376 if (debug_info_level <= DINFO_LEVEL_TERSE)
21377 return;
21378 if (!is_fortran () && !is_ada ())
21379 return;
21380 if (TREE_STATIC (decl) && decl_function_context (decl))
21381 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21382 break;
21384 case NAMESPACE_DECL:
21385 case IMPORTED_DECL:
21386 if (debug_info_level <= DINFO_LEVEL_TERSE)
21387 return;
21388 if (lookup_decl_die (decl) != NULL)
21389 return;
21390 break;
21392 case TYPE_DECL:
21393 /* Don't emit stubs for types unless they are needed by other DIEs. */
21394 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21395 return;
21397 /* Don't bother trying to generate any DIEs to represent any of the
21398 normal built-in types for the language we are compiling. */
21399 if (DECL_IS_BUILTIN (decl))
21400 return;
21402 /* If we are in terse mode, don't generate any DIEs for types. */
21403 if (debug_info_level <= DINFO_LEVEL_TERSE)
21404 return;
21406 /* If we're a function-scope tag, initially use a parent of NULL;
21407 this will be fixed up in decls_for_scope. */
21408 if (decl_function_context (decl))
21409 context_die = NULL;
21411 break;
21413 default:
21414 return;
21417 gen_decl_die (decl, NULL, context_die);
21420 /* Write the debugging output for DECL. */
21422 static void
21423 dwarf2out_function_decl (tree decl)
21425 dwarf2out_decl (decl);
21427 htab_empty (decl_loc_table);
21430 /* Output a marker (i.e. a label) for the beginning of the generated code for
21431 a lexical block. */
21433 static void
21434 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21435 unsigned int blocknum)
21437 switch_to_section (current_function_section ());
21438 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21441 /* Output a marker (i.e. a label) for the end of the generated code for a
21442 lexical block. */
21444 static void
21445 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21447 switch_to_section (current_function_section ());
21448 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21451 /* Returns nonzero if it is appropriate not to emit any debugging
21452 information for BLOCK, because it doesn't contain any instructions.
21454 Don't allow this for blocks with nested functions or local classes
21455 as we would end up with orphans, and in the presence of scheduling
21456 we may end up calling them anyway. */
21458 static bool
21459 dwarf2out_ignore_block (const_tree block)
21461 tree decl;
21462 unsigned int i;
21464 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21465 if (TREE_CODE (decl) == FUNCTION_DECL
21466 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21467 return 0;
21468 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21470 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21471 if (TREE_CODE (decl) == FUNCTION_DECL
21472 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21473 return 0;
21476 return 1;
21479 /* Hash table routines for file_hash. */
21481 static int
21482 file_table_eq (const void *p1_p, const void *p2_p)
21484 const struct dwarf_file_data *const p1 =
21485 (const struct dwarf_file_data *) p1_p;
21486 const char *const p2 = (const char *) p2_p;
21487 return strcmp (p1->filename, p2) == 0;
21490 static hashval_t
21491 file_table_hash (const void *p_p)
21493 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21494 return htab_hash_string (p->filename);
21497 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21498 dwarf2out.c) and return its "index". The index of each (known) filename is
21499 just a unique number which is associated with only that one filename. We
21500 need such numbers for the sake of generating labels (in the .debug_sfnames
21501 section) and references to those files numbers (in the .debug_srcinfo
21502 and.debug_macinfo sections). If the filename given as an argument is not
21503 found in our current list, add it to the list and assign it the next
21504 available unique index number. In order to speed up searches, we remember
21505 the index of the filename was looked up last. This handles the majority of
21506 all searches. */
21508 static struct dwarf_file_data *
21509 lookup_filename (const char *file_name)
21511 void ** slot;
21512 struct dwarf_file_data * created;
21514 /* Check to see if the file name that was searched on the previous
21515 call matches this file name. If so, return the index. */
21516 if (file_table_last_lookup
21517 && (file_name == file_table_last_lookup->filename
21518 || strcmp (file_table_last_lookup->filename, file_name) == 0))
21519 return file_table_last_lookup;
21521 /* Didn't match the previous lookup, search the table. */
21522 slot = htab_find_slot_with_hash (file_table, file_name,
21523 htab_hash_string (file_name), INSERT);
21524 if (*slot)
21525 return (struct dwarf_file_data *) *slot;
21527 created = ggc_alloc_dwarf_file_data ();
21528 created->filename = file_name;
21529 created->emitted_number = 0;
21530 *slot = created;
21531 return created;
21534 /* If the assembler will construct the file table, then translate the compiler
21535 internal file table number into the assembler file table number, and emit
21536 a .file directive if we haven't already emitted one yet. The file table
21537 numbers are different because we prune debug info for unused variables and
21538 types, which may include filenames. */
21540 static int
21541 maybe_emit_file (struct dwarf_file_data * fd)
21543 if (! fd->emitted_number)
21545 if (last_emitted_file)
21546 fd->emitted_number = last_emitted_file->emitted_number + 1;
21547 else
21548 fd->emitted_number = 1;
21549 last_emitted_file = fd;
21551 if (DWARF2_ASM_LINE_DEBUG_INFO)
21553 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21554 output_quoted_string (asm_out_file,
21555 remap_debug_filename (fd->filename));
21556 fputc ('\n', asm_out_file);
21560 return fd->emitted_number;
21563 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21564 That generation should happen after function debug info has been
21565 generated. The value of the attribute is the constant value of ARG. */
21567 static void
21568 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21570 die_arg_entry entry;
21572 if (!die || !arg)
21573 return;
21575 if (!tmpl_value_parm_die_table)
21576 tmpl_value_parm_die_table
21577 = VEC_alloc (die_arg_entry, gc, 32);
21579 entry.die = die;
21580 entry.arg = arg;
21581 VEC_safe_push (die_arg_entry, gc,
21582 tmpl_value_parm_die_table,
21583 &entry);
21586 /* Return TRUE if T is an instance of generic type, FALSE
21587 otherwise. */
21589 static bool
21590 generic_type_p (tree t)
21592 if (t == NULL_TREE || !TYPE_P (t))
21593 return false;
21594 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21597 /* Schedule the generation of the generic parameter dies for the
21598 instance of generic type T. The proper generation itself is later
21599 done by gen_scheduled_generic_parms_dies. */
21601 static void
21602 schedule_generic_params_dies_gen (tree t)
21604 if (!generic_type_p (t))
21605 return;
21607 if (generic_type_instances == NULL)
21608 generic_type_instances = VEC_alloc (tree, gc, 256);
21610 VEC_safe_push (tree, gc, generic_type_instances, t);
21613 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21614 by append_entry_to_tmpl_value_parm_die_table. This function must
21615 be called after function DIEs have been generated. */
21617 static void
21618 gen_remaining_tmpl_value_param_die_attribute (void)
21620 if (tmpl_value_parm_die_table)
21622 unsigned i;
21623 die_arg_entry *e;
21625 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21626 tree_add_const_value_attribute (e->die, e->arg);
21630 /* Generate generic parameters DIEs for instances of generic types
21631 that have been previously scheduled by
21632 schedule_generic_params_dies_gen. This function must be called
21633 after all the types of the CU have been laid out. */
21635 static void
21636 gen_scheduled_generic_parms_dies (void)
21638 unsigned i;
21639 tree t;
21641 if (generic_type_instances == NULL)
21642 return;
21644 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21645 gen_generic_params_dies (t);
21649 /* Replace DW_AT_name for the decl with name. */
21651 static void
21652 dwarf2out_set_name (tree decl, tree name)
21654 dw_die_ref die;
21655 dw_attr_ref attr;
21656 const char *dname;
21658 die = TYPE_SYMTAB_DIE (decl);
21659 if (!die)
21660 return;
21662 dname = dwarf2_name (name, 0);
21663 if (!dname)
21664 return;
21666 attr = get_AT (die, DW_AT_name);
21667 if (attr)
21669 struct indirect_string_node *node;
21671 node = find_AT_string (dname);
21672 /* replace the string. */
21673 attr->dw_attr_val.v.val_str = node;
21676 else
21677 add_name_attribute (die, dname);
21680 /* Called by the final INSN scan whenever we see a direct function call.
21681 Make an entry into the direct call table, recording the point of call
21682 and a reference to the target function's debug entry. */
21684 static void
21685 dwarf2out_direct_call (tree targ)
21687 dcall_entry e;
21688 tree origin = decl_ultimate_origin (targ);
21690 /* If this is a clone, use the abstract origin as the target. */
21691 if (origin)
21692 targ = origin;
21694 e.poc_label_num = poc_label_num++;
21695 e.poc_decl = current_function_decl;
21696 e.targ_die = force_decl_die (targ);
21697 VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21699 /* Drop a label at the return point to mark the point of call. */
21700 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21703 /* Returns a hash value for X (which really is a struct vcall_insn). */
21705 static hashval_t
21706 vcall_insn_table_hash (const void *x)
21708 return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21711 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21712 insnd_uid of *Y. */
21714 static int
21715 vcall_insn_table_eq (const void *x, const void *y)
21717 return (((const struct vcall_insn *) x)->insn_uid
21718 == ((const struct vcall_insn *) y)->insn_uid);
21721 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE. */
21723 static void
21724 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21726 struct vcall_insn *item = ggc_alloc_vcall_insn ();
21727 struct vcall_insn **slot;
21729 gcc_assert (item);
21730 item->insn_uid = insn_uid;
21731 item->vtable_slot = vtable_slot;
21732 slot = (struct vcall_insn **)
21733 htab_find_slot_with_hash (vcall_insn_table, &item,
21734 (hashval_t) insn_uid, INSERT);
21735 *slot = item;
21738 /* Return the VTABLE_SLOT associated with INSN_UID. */
21740 static unsigned int
21741 lookup_vcall_insn (unsigned int insn_uid)
21743 struct vcall_insn item;
21744 struct vcall_insn *p;
21746 item.insn_uid = insn_uid;
21747 item.vtable_slot = 0;
21748 p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21749 (void *) &item,
21750 (hashval_t) insn_uid);
21751 if (p == NULL)
21752 return (unsigned int) -1;
21753 return p->vtable_slot;
21757 /* Called when lowering indirect calls to RTL. We make a note of INSN_UID
21758 and the OBJ_TYPE_REF_TOKEN from ADDR. For C++ virtual calls, the token
21759 is the vtable slot index that we will need to put in the virtual call
21760 table later. */
21762 static void
21763 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21765 if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21767 tree token = OBJ_TYPE_REF_TOKEN (addr);
21768 if (TREE_CODE (token) == INTEGER_CST)
21769 store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21773 /* Called when scheduling RTL, when a CALL_INSN is split. Copies the
21774 OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21775 with NEW_INSN. */
21777 static void
21778 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21780 unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21782 if (vtable_slot != (unsigned int) -1)
21783 store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21786 /* Called by the final INSN scan whenever we see a virtual function call.
21787 Make an entry into the virtual call table, recording the point of call
21788 and the slot index of the vtable entry used to call the virtual member
21789 function. The slot index was associated with the INSN_UID during the
21790 lowering to RTL. */
21792 static void
21793 dwarf2out_virtual_call (int insn_uid)
21795 unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21796 vcall_entry e;
21798 if (vtable_slot == (unsigned int) -1)
21799 return;
21801 e.poc_label_num = poc_label_num++;
21802 e.vtable_slot = vtable_slot;
21803 VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21805 /* Drop a label at the return point to mark the point of call. */
21806 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21809 /* Called by the final INSN scan whenever we see a var location. We
21810 use it to drop labels in the right places, and throw the location in
21811 our lookup table. */
21813 static void
21814 dwarf2out_var_location (rtx loc_note)
21816 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21817 struct var_loc_node *newloc;
21818 rtx next_real;
21819 static const char *last_label;
21820 static const char *last_postcall_label;
21821 static bool last_in_cold_section_p;
21822 tree decl;
21824 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21825 return;
21827 next_real = next_real_insn (loc_note);
21828 /* If there are no instructions which would be affected by this note,
21829 don't do anything. */
21830 if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21831 return;
21833 /* If there were any real insns between note we processed last time
21834 and this note (or if it is the first note), clear
21835 last_{,postcall_}label so that they are not reused this time. */
21836 if (last_var_location_insn == NULL_RTX
21837 || last_var_location_insn != next_real
21838 || last_in_cold_section_p != in_cold_section_p)
21840 last_label = NULL;
21841 last_postcall_label = NULL;
21844 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21845 newloc = add_var_loc_to_decl (decl, loc_note,
21846 NOTE_DURING_CALL_P (loc_note)
21847 ? last_postcall_label : last_label);
21848 if (newloc == NULL)
21849 return;
21851 /* If there were no real insns between note we processed last time
21852 and this note, use the label we emitted last time. Otherwise
21853 create a new label and emit it. */
21854 if (last_label == NULL)
21856 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21857 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21858 loclabel_num++;
21859 last_label = ggc_strdup (loclabel);
21862 if (!NOTE_DURING_CALL_P (loc_note))
21863 newloc->label = last_label;
21864 else
21866 if (!last_postcall_label)
21868 sprintf (loclabel, "%s-1", last_label);
21869 last_postcall_label = ggc_strdup (loclabel);
21871 newloc->label = last_postcall_label;
21874 last_var_location_insn = next_real;
21875 last_in_cold_section_p = in_cold_section_p;
21878 /* We need to reset the locations at the beginning of each
21879 function. We can't do this in the end_function hook, because the
21880 declarations that use the locations won't have been output when
21881 that hook is called. Also compute have_multiple_function_sections here. */
21883 static void
21884 dwarf2out_begin_function (tree fun)
21886 if (function_section (fun) != text_section)
21887 have_multiple_function_sections = true;
21888 else if (flag_reorder_blocks_and_partition && !cold_text_section)
21890 gcc_assert (current_function_decl == fun);
21891 cold_text_section = unlikely_text_section ();
21892 switch_to_section (cold_text_section);
21893 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21894 switch_to_section (current_function_section ());
21897 dwarf2out_note_section_used ();
21900 /* Output a label to mark the beginning of a source code line entry
21901 and record information relating to this source line, in
21902 'line_info_table' for later output of the .debug_line section. */
21904 static void
21905 dwarf2out_source_line (unsigned int line, const char *filename,
21906 int discriminator, bool is_stmt)
21908 static bool last_is_stmt = true;
21910 if (debug_info_level >= DINFO_LEVEL_NORMAL
21911 && line != 0)
21913 int file_num = maybe_emit_file (lookup_filename (filename));
21915 switch_to_section (current_function_section ());
21917 /* If requested, emit something human-readable. */
21918 if (flag_debug_asm)
21919 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21920 filename, line);
21922 if (DWARF2_ASM_LINE_DEBUG_INFO)
21924 /* Emit the .loc directive understood by GNU as. */
21925 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21926 if (is_stmt != last_is_stmt)
21928 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21929 last_is_stmt = is_stmt;
21931 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21932 fprintf (asm_out_file, " discriminator %d", discriminator);
21933 fputc ('\n', asm_out_file);
21935 /* Indicate that line number info exists. */
21936 line_info_table_in_use++;
21938 else if (function_section (current_function_decl) != text_section)
21940 dw_separate_line_info_ref line_info;
21941 targetm.asm_out.internal_label (asm_out_file,
21942 SEPARATE_LINE_CODE_LABEL,
21943 separate_line_info_table_in_use);
21945 /* Expand the line info table if necessary. */
21946 if (separate_line_info_table_in_use
21947 == separate_line_info_table_allocated)
21949 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21950 separate_line_info_table
21951 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21952 separate_line_info_table,
21953 separate_line_info_table_allocated);
21954 memset (separate_line_info_table
21955 + separate_line_info_table_in_use,
21957 (LINE_INFO_TABLE_INCREMENT
21958 * sizeof (dw_separate_line_info_entry)));
21961 /* Add the new entry at the end of the line_info_table. */
21962 line_info
21963 = &separate_line_info_table[separate_line_info_table_in_use++];
21964 line_info->dw_file_num = file_num;
21965 line_info->dw_line_num = line;
21966 line_info->function = current_function_funcdef_no;
21968 else
21970 dw_line_info_ref line_info;
21972 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21973 line_info_table_in_use);
21975 /* Expand the line info table if necessary. */
21976 if (line_info_table_in_use == line_info_table_allocated)
21978 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21979 line_info_table
21980 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21981 line_info_table_allocated);
21982 memset (line_info_table + line_info_table_in_use, 0,
21983 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21986 /* Add the new entry at the end of the line_info_table. */
21987 line_info = &line_info_table[line_info_table_in_use++];
21988 line_info->dw_file_num = file_num;
21989 line_info->dw_line_num = line;
21994 /* Record the beginning of a new source file. */
21996 static void
21997 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21999 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22001 /* Record the beginning of the file for break_out_includes. */
22002 dw_die_ref bincl_die;
22004 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22005 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22008 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22010 macinfo_entry e;
22011 e.code = DW_MACINFO_start_file;
22012 e.lineno = lineno;
22013 e.info = xstrdup (filename);
22014 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22018 /* Record the end of a source file. */
22020 static void
22021 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22023 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22024 /* Record the end of the file for break_out_includes. */
22025 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22027 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22029 macinfo_entry e;
22030 e.code = DW_MACINFO_end_file;
22031 e.lineno = lineno;
22032 e.info = NULL;
22033 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22037 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22038 the tail part of the directive line, i.e. the part which is past the
22039 initial whitespace, #, whitespace, directive-name, whitespace part. */
22041 static void
22042 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22043 const char *buffer ATTRIBUTE_UNUSED)
22045 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22047 macinfo_entry e;
22048 e.code = DW_MACINFO_define;
22049 e.lineno = lineno;
22050 e.info = xstrdup (buffer);;
22051 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22055 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22056 the tail part of the directive line, i.e. the part which is past the
22057 initial whitespace, #, whitespace, directive-name, whitespace part. */
22059 static void
22060 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22061 const char *buffer ATTRIBUTE_UNUSED)
22063 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22065 macinfo_entry e;
22066 e.code = DW_MACINFO_undef;
22067 e.lineno = lineno;
22068 e.info = xstrdup (buffer);;
22069 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22073 static void
22074 output_macinfo (void)
22076 unsigned i;
22077 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22078 macinfo_entry *ref;
22080 if (! length)
22081 return;
22083 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22085 switch (ref->code)
22087 case DW_MACINFO_start_file:
22089 int file_num = maybe_emit_file (lookup_filename (ref->info));
22090 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22091 dw2_asm_output_data_uleb128
22092 (ref->lineno, "Included from line number %lu",
22093 (unsigned long)ref->lineno);
22094 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22096 break;
22097 case DW_MACINFO_end_file:
22098 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22099 break;
22100 case DW_MACINFO_define:
22101 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22102 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22103 (unsigned long)ref->lineno);
22104 dw2_asm_output_nstring (ref->info, -1, "The macro");
22105 break;
22106 case DW_MACINFO_undef:
22107 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22108 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22109 (unsigned long)ref->lineno);
22110 dw2_asm_output_nstring (ref->info, -1, "The macro");
22111 break;
22112 default:
22113 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22114 ASM_COMMENT_START, (unsigned long)ref->code);
22115 break;
22120 /* Set up for Dwarf output at the start of compilation. */
22122 static void
22123 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22125 /* Allocate the file_table. */
22126 file_table = htab_create_ggc (50, file_table_hash,
22127 file_table_eq, NULL);
22129 /* Allocate the decl_die_table. */
22130 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22131 decl_die_table_eq, NULL);
22133 /* Allocate the decl_loc_table. */
22134 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22135 decl_loc_table_eq, NULL);
22137 /* Allocate the initial hunk of the decl_scope_table. */
22138 decl_scope_table = VEC_alloc (tree, gc, 256);
22140 /* Allocate the initial hunk of the abbrev_die_table. */
22141 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22142 (ABBREV_DIE_TABLE_INCREMENT);
22143 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22144 /* Zero-th entry is allocated, but unused. */
22145 abbrev_die_table_in_use = 1;
22147 /* Allocate the initial hunk of the line_info_table. */
22148 line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22149 (LINE_INFO_TABLE_INCREMENT);
22150 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22152 /* Zero-th entry is allocated, but unused. */
22153 line_info_table_in_use = 1;
22155 /* Allocate the pubtypes and pubnames vectors. */
22156 pubname_table = VEC_alloc (pubname_entry, gc, 32);
22157 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22159 /* Allocate the table that maps insn UIDs to vtable slot indexes. */
22160 vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
22161 vcall_insn_table_eq, NULL);
22163 incomplete_types = VEC_alloc (tree, gc, 64);
22165 used_rtx_array = VEC_alloc (rtx, gc, 32);
22167 debug_info_section = get_section (DEBUG_INFO_SECTION,
22168 SECTION_DEBUG, NULL);
22169 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22170 SECTION_DEBUG, NULL);
22171 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22172 SECTION_DEBUG, NULL);
22173 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22174 SECTION_DEBUG, NULL);
22175 debug_line_section = get_section (DEBUG_LINE_SECTION,
22176 SECTION_DEBUG, NULL);
22177 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22178 SECTION_DEBUG, NULL);
22179 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22180 SECTION_DEBUG, NULL);
22181 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22182 SECTION_DEBUG, NULL);
22183 debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
22184 SECTION_DEBUG, NULL);
22185 debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
22186 SECTION_DEBUG, NULL);
22187 debug_str_section = get_section (DEBUG_STR_SECTION,
22188 DEBUG_STR_SECTION_FLAGS, NULL);
22189 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22190 SECTION_DEBUG, NULL);
22191 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22192 SECTION_DEBUG, NULL);
22194 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22195 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22196 DEBUG_ABBREV_SECTION_LABEL, 0);
22197 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22198 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22199 COLD_TEXT_SECTION_LABEL, 0);
22200 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22202 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22203 DEBUG_INFO_SECTION_LABEL, 0);
22204 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22205 DEBUG_LINE_SECTION_LABEL, 0);
22206 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22207 DEBUG_RANGES_SECTION_LABEL, 0);
22208 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22209 DEBUG_MACINFO_SECTION_LABEL, 0);
22211 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22212 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22214 switch_to_section (text_section);
22215 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22218 /* Called before cgraph_optimize starts outputtting functions, variables
22219 and toplevel asms into assembly. */
22221 static void
22222 dwarf2out_assembly_start (void)
22224 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22225 && dwarf2out_do_cfi_asm ()
22226 && (!(flag_unwind_tables || flag_exceptions)
22227 || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22228 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22231 /* A helper function for dwarf2out_finish called through
22232 htab_traverse. Emit one queued .debug_str string. */
22234 static int
22235 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22237 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22239 if (node->label && node->refcount)
22241 switch_to_section (debug_str_section);
22242 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22243 assemble_string (node->str, strlen (node->str) + 1);
22246 return 1;
22249 #if ENABLE_ASSERT_CHECKING
22250 /* Verify that all marks are clear. */
22252 static void
22253 verify_marks_clear (dw_die_ref die)
22255 dw_die_ref c;
22257 gcc_assert (! die->die_mark);
22258 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22260 #endif /* ENABLE_ASSERT_CHECKING */
22262 /* Clear the marks for a die and its children.
22263 Be cool if the mark isn't set. */
22265 static void
22266 prune_unmark_dies (dw_die_ref die)
22268 dw_die_ref c;
22270 if (die->die_mark)
22271 die->die_mark = 0;
22272 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22275 /* Given DIE that we're marking as used, find any other dies
22276 it references as attributes and mark them as used. */
22278 static void
22279 prune_unused_types_walk_attribs (dw_die_ref die)
22281 dw_attr_ref a;
22282 unsigned ix;
22284 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22286 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22288 /* A reference to another DIE.
22289 Make sure that it will get emitted.
22290 If it was broken out into a comdat group, don't follow it. */
22291 if (dwarf_version < 4
22292 || a->dw_attr == DW_AT_specification
22293 || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22294 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22296 /* Set the string's refcount to 0 so that prune_unused_types_mark
22297 accounts properly for it. */
22298 if (AT_class (a) == dw_val_class_str)
22299 a->dw_attr_val.v.val_str->refcount = 0;
22303 /* Mark the generic parameters and arguments children DIEs of DIE. */
22305 static void
22306 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22308 dw_die_ref c;
22310 if (die == NULL || die->die_child == NULL)
22311 return;
22312 c = die->die_child;
22315 switch (c->die_tag)
22317 case DW_TAG_template_type_param:
22318 case DW_TAG_template_value_param:
22319 case DW_TAG_GNU_template_template_param:
22320 case DW_TAG_GNU_template_parameter_pack:
22321 prune_unused_types_mark (c, 1);
22322 break;
22323 default:
22324 break;
22326 c = c->die_sib;
22327 } while (c && c != die->die_child);
22330 /* Mark DIE as being used. If DOKIDS is true, then walk down
22331 to DIE's children. */
22333 static void
22334 prune_unused_types_mark (dw_die_ref die, int dokids)
22336 dw_die_ref c;
22338 if (die->die_mark == 0)
22340 /* We haven't done this node yet. Mark it as used. */
22341 die->die_mark = 1;
22342 /* If this is the DIE of a generic type instantiation,
22343 mark the children DIEs that describe its generic parms and
22344 args. */
22345 prune_unused_types_mark_generic_parms_dies (die);
22347 /* We also have to mark its parents as used.
22348 (But we don't want to mark our parents' kids due to this.) */
22349 if (die->die_parent)
22350 prune_unused_types_mark (die->die_parent, 0);
22352 /* Mark any referenced nodes. */
22353 prune_unused_types_walk_attribs (die);
22355 /* If this node is a specification,
22356 also mark the definition, if it exists. */
22357 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22358 prune_unused_types_mark (die->die_definition, 1);
22361 if (dokids && die->die_mark != 2)
22363 /* We need to walk the children, but haven't done so yet.
22364 Remember that we've walked the kids. */
22365 die->die_mark = 2;
22367 /* If this is an array type, we need to make sure our
22368 kids get marked, even if they're types. If we're
22369 breaking out types into comdat sections, do this
22370 for all type definitions. */
22371 if (die->die_tag == DW_TAG_array_type
22372 || (dwarf_version >= 4
22373 && is_type_die (die) && ! is_declaration_die (die)))
22374 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22375 else
22376 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22380 /* For local classes, look if any static member functions were emitted
22381 and if so, mark them. */
22383 static void
22384 prune_unused_types_walk_local_classes (dw_die_ref die)
22386 dw_die_ref c;
22388 if (die->die_mark == 2)
22389 return;
22391 switch (die->die_tag)
22393 case DW_TAG_structure_type:
22394 case DW_TAG_union_type:
22395 case DW_TAG_class_type:
22396 break;
22398 case DW_TAG_subprogram:
22399 if (!get_AT_flag (die, DW_AT_declaration)
22400 || die->die_definition != NULL)
22401 prune_unused_types_mark (die, 1);
22402 return;
22404 default:
22405 return;
22408 /* Mark children. */
22409 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22412 /* Walk the tree DIE and mark types that we actually use. */
22414 static void
22415 prune_unused_types_walk (dw_die_ref die)
22417 dw_die_ref c;
22419 /* Don't do anything if this node is already marked and
22420 children have been marked as well. */
22421 if (die->die_mark == 2)
22422 return;
22424 switch (die->die_tag)
22426 case DW_TAG_structure_type:
22427 case DW_TAG_union_type:
22428 case DW_TAG_class_type:
22429 if (die->die_perennial_p)
22430 break;
22432 for (c = die->die_parent; c; c = c->die_parent)
22433 if (c->die_tag == DW_TAG_subprogram)
22434 break;
22436 /* Finding used static member functions inside of classes
22437 is needed just for local classes, because for other classes
22438 static member function DIEs with DW_AT_specification
22439 are emitted outside of the DW_TAG_*_type. If we ever change
22440 it, we'd need to call this even for non-local classes. */
22441 if (c)
22442 prune_unused_types_walk_local_classes (die);
22444 /* It's a type node --- don't mark it. */
22445 return;
22447 case DW_TAG_const_type:
22448 case DW_TAG_packed_type:
22449 case DW_TAG_pointer_type:
22450 case DW_TAG_reference_type:
22451 case DW_TAG_rvalue_reference_type:
22452 case DW_TAG_volatile_type:
22453 case DW_TAG_typedef:
22454 case DW_TAG_array_type:
22455 case DW_TAG_interface_type:
22456 case DW_TAG_friend:
22457 case DW_TAG_variant_part:
22458 case DW_TAG_enumeration_type:
22459 case DW_TAG_subroutine_type:
22460 case DW_TAG_string_type:
22461 case DW_TAG_set_type:
22462 case DW_TAG_subrange_type:
22463 case DW_TAG_ptr_to_member_type:
22464 case DW_TAG_file_type:
22465 if (die->die_perennial_p)
22466 break;
22468 /* It's a type node --- don't mark it. */
22469 return;
22471 default:
22472 /* Mark everything else. */
22473 break;
22476 if (die->die_mark == 0)
22478 die->die_mark = 1;
22480 /* Now, mark any dies referenced from here. */
22481 prune_unused_types_walk_attribs (die);
22484 die->die_mark = 2;
22486 /* Mark children. */
22487 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22490 /* Increment the string counts on strings referred to from DIE's
22491 attributes. */
22493 static void
22494 prune_unused_types_update_strings (dw_die_ref die)
22496 dw_attr_ref a;
22497 unsigned ix;
22499 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22500 if (AT_class (a) == dw_val_class_str)
22502 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22503 s->refcount++;
22504 /* Avoid unnecessarily putting strings that are used less than
22505 twice in the hash table. */
22506 if (s->refcount
22507 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22509 void ** slot;
22510 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22511 htab_hash_string (s->str),
22512 INSERT);
22513 gcc_assert (*slot == NULL);
22514 *slot = s;
22519 /* Remove from the tree DIE any dies that aren't marked. */
22521 static void
22522 prune_unused_types_prune (dw_die_ref die)
22524 dw_die_ref c;
22526 gcc_assert (die->die_mark);
22527 prune_unused_types_update_strings (die);
22529 if (! die->die_child)
22530 return;
22532 c = die->die_child;
22533 do {
22534 dw_die_ref prev = c;
22535 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22536 if (c == die->die_child)
22538 /* No marked children between 'prev' and the end of the list. */
22539 if (prev == c)
22540 /* No marked children at all. */
22541 die->die_child = NULL;
22542 else
22544 prev->die_sib = c->die_sib;
22545 die->die_child = prev;
22547 return;
22550 if (c != prev->die_sib)
22551 prev->die_sib = c;
22552 prune_unused_types_prune (c);
22553 } while (c != die->die_child);
22556 /* A helper function for dwarf2out_finish called through
22557 htab_traverse. Clear .debug_str strings that we haven't already
22558 decided to emit. */
22560 static int
22561 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22563 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22565 if (!node->label || !node->refcount)
22566 htab_clear_slot (debug_str_hash, h);
22568 return 1;
22571 /* Remove dies representing declarations that we never use. */
22573 static void
22574 prune_unused_types (void)
22576 unsigned int i;
22577 limbo_die_node *node;
22578 comdat_type_node *ctnode;
22579 pubname_ref pub;
22580 dcall_entry *dcall;
22582 #if ENABLE_ASSERT_CHECKING
22583 /* All the marks should already be clear. */
22584 verify_marks_clear (comp_unit_die ());
22585 for (node = limbo_die_list; node; node = node->next)
22586 verify_marks_clear (node->die);
22587 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22588 verify_marks_clear (ctnode->root_die);
22589 #endif /* ENABLE_ASSERT_CHECKING */
22591 /* Mark types that are used in global variables. */
22592 premark_types_used_by_global_vars ();
22594 /* Set the mark on nodes that are actually used. */
22595 prune_unused_types_walk (comp_unit_die ());
22596 for (node = limbo_die_list; node; node = node->next)
22597 prune_unused_types_walk (node->die);
22598 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22600 prune_unused_types_walk (ctnode->root_die);
22601 prune_unused_types_mark (ctnode->type_die, 1);
22604 /* Also set the mark on nodes referenced from the
22605 pubname_table or arange_table. */
22606 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22607 prune_unused_types_mark (pub->die, 1);
22608 for (i = 0; i < arange_table_in_use; i++)
22609 prune_unused_types_mark (arange_table[i], 1);
22611 /* Mark nodes referenced from the direct call table. */
22612 FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22613 prune_unused_types_mark (dcall->targ_die, 1);
22615 /* Get rid of nodes that aren't marked; and update the string counts. */
22616 if (debug_str_hash && debug_str_hash_forced)
22617 htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22618 else if (debug_str_hash)
22619 htab_empty (debug_str_hash);
22620 prune_unused_types_prune (comp_unit_die ());
22621 for (node = limbo_die_list; node; node = node->next)
22622 prune_unused_types_prune (node->die);
22623 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22624 prune_unused_types_prune (ctnode->root_die);
22626 /* Leave the marks clear. */
22627 prune_unmark_dies (comp_unit_die ());
22628 for (node = limbo_die_list; node; node = node->next)
22629 prune_unmark_dies (node->die);
22630 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22631 prune_unmark_dies (ctnode->root_die);
22634 /* Set the parameter to true if there are any relative pathnames in
22635 the file table. */
22636 static int
22637 file_table_relative_p (void ** slot, void *param)
22639 bool *p = (bool *) param;
22640 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22641 if (!IS_ABSOLUTE_PATH (d->filename))
22643 *p = true;
22644 return 0;
22646 return 1;
22649 /* Routines to manipulate hash table of comdat type units. */
22651 static hashval_t
22652 htab_ct_hash (const void *of)
22654 hashval_t h;
22655 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22657 memcpy (&h, type_node->signature, sizeof (h));
22658 return h;
22661 static int
22662 htab_ct_eq (const void *of1, const void *of2)
22664 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22665 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22667 return (! memcmp (type_node_1->signature, type_node_2->signature,
22668 DWARF_TYPE_SIGNATURE_SIZE));
22671 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22672 to the location it would have been added, should we know its
22673 DECL_ASSEMBLER_NAME when we added other attributes. This will
22674 probably improve compactness of debug info, removing equivalent
22675 abbrevs, and hide any differences caused by deferring the
22676 computation of the assembler name, triggered by e.g. PCH. */
22678 static inline void
22679 move_linkage_attr (dw_die_ref die)
22681 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22682 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22684 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22685 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22687 while (--ix > 0)
22689 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22691 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22692 break;
22695 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22697 VEC_pop (dw_attr_node, die->die_attr);
22698 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22702 /* Helper function for resolve_addr, attempt to resolve
22703 one CONST_STRING, return non-zero if not successful. Similarly verify that
22704 SYMBOL_REFs refer to variables emitted in the current CU. */
22706 static int
22707 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22709 rtx rtl = *addr;
22711 if (GET_CODE (rtl) == CONST_STRING)
22713 size_t len = strlen (XSTR (rtl, 0)) + 1;
22714 tree t = build_string (len, XSTR (rtl, 0));
22715 tree tlen = build_int_cst (NULL_TREE, len - 1);
22716 TREE_TYPE (t)
22717 = build_array_type (char_type_node, build_index_type (tlen));
22718 rtl = lookup_constant_def (t);
22719 if (!rtl || !MEM_P (rtl))
22720 return 1;
22721 rtl = XEXP (rtl, 0);
22722 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22723 *addr = rtl;
22724 return 0;
22727 if (GET_CODE (rtl) == SYMBOL_REF
22728 && SYMBOL_REF_DECL (rtl)
22729 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22730 return 1;
22732 if (GET_CODE (rtl) == CONST
22733 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22734 return 1;
22736 return 0;
22739 /* Helper function for resolve_addr, handle one location
22740 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22741 the location list couldn't be resolved. */
22743 static bool
22744 resolve_addr_in_expr (dw_loc_descr_ref loc)
22746 for (; loc; loc = loc->dw_loc_next)
22747 if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22748 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22749 || (loc->dw_loc_opc == DW_OP_implicit_value
22750 && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22751 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22752 return false;
22753 else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22754 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22756 dw_die_ref ref
22757 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22758 if (ref == NULL)
22759 return false;
22760 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22761 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22762 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22764 return true;
22767 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22768 an address in .rodata section if the string literal is emitted there,
22769 or remove the containing location list or replace DW_AT_const_value
22770 with DW_AT_location and empty location expression, if it isn't found
22771 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22772 to something that has been emitted in the current CU. */
22774 static void
22775 resolve_addr (dw_die_ref die)
22777 dw_die_ref c;
22778 dw_attr_ref a;
22779 dw_loc_list_ref *curr;
22780 unsigned ix;
22782 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22783 switch (AT_class (a))
22785 case dw_val_class_loc_list:
22786 curr = AT_loc_list_ptr (a);
22787 while (*curr)
22789 if (!resolve_addr_in_expr ((*curr)->expr))
22791 dw_loc_list_ref next = (*curr)->dw_loc_next;
22792 if (next && (*curr)->ll_symbol)
22794 gcc_assert (!next->ll_symbol);
22795 next->ll_symbol = (*curr)->ll_symbol;
22797 *curr = next;
22799 else
22800 curr = &(*curr)->dw_loc_next;
22802 if (!AT_loc_list (a))
22804 remove_AT (die, a->dw_attr);
22805 ix--;
22807 break;
22808 case dw_val_class_loc:
22809 if (!resolve_addr_in_expr (AT_loc (a)))
22811 remove_AT (die, a->dw_attr);
22812 ix--;
22814 break;
22815 case dw_val_class_addr:
22816 if (a->dw_attr == DW_AT_const_value
22817 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22819 remove_AT (die, a->dw_attr);
22820 ix--;
22822 break;
22823 default:
22824 break;
22827 FOR_EACH_CHILD (die, c, resolve_addr (c));
22830 /* Helper routines for optimize_location_lists.
22831 This pass tries to share identical local lists in .debug_loc
22832 section. */
22834 /* Iteratively hash operands of LOC opcode. */
22836 static inline hashval_t
22837 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22839 dw_val_ref val1 = &loc->dw_loc_oprnd1;
22840 dw_val_ref val2 = &loc->dw_loc_oprnd2;
22842 switch (loc->dw_loc_opc)
22844 case DW_OP_const4u:
22845 case DW_OP_const8u:
22846 if (loc->dtprel)
22847 goto hash_addr;
22848 /* FALLTHRU */
22849 case DW_OP_const1u:
22850 case DW_OP_const1s:
22851 case DW_OP_const2u:
22852 case DW_OP_const2s:
22853 case DW_OP_const4s:
22854 case DW_OP_const8s:
22855 case DW_OP_constu:
22856 case DW_OP_consts:
22857 case DW_OP_pick:
22858 case DW_OP_plus_uconst:
22859 case DW_OP_breg0:
22860 case DW_OP_breg1:
22861 case DW_OP_breg2:
22862 case DW_OP_breg3:
22863 case DW_OP_breg4:
22864 case DW_OP_breg5:
22865 case DW_OP_breg6:
22866 case DW_OP_breg7:
22867 case DW_OP_breg8:
22868 case DW_OP_breg9:
22869 case DW_OP_breg10:
22870 case DW_OP_breg11:
22871 case DW_OP_breg12:
22872 case DW_OP_breg13:
22873 case DW_OP_breg14:
22874 case DW_OP_breg15:
22875 case DW_OP_breg16:
22876 case DW_OP_breg17:
22877 case DW_OP_breg18:
22878 case DW_OP_breg19:
22879 case DW_OP_breg20:
22880 case DW_OP_breg21:
22881 case DW_OP_breg22:
22882 case DW_OP_breg23:
22883 case DW_OP_breg24:
22884 case DW_OP_breg25:
22885 case DW_OP_breg26:
22886 case DW_OP_breg27:
22887 case DW_OP_breg28:
22888 case DW_OP_breg29:
22889 case DW_OP_breg30:
22890 case DW_OP_breg31:
22891 case DW_OP_regx:
22892 case DW_OP_fbreg:
22893 case DW_OP_piece:
22894 case DW_OP_deref_size:
22895 case DW_OP_xderef_size:
22896 hash = iterative_hash_object (val1->v.val_int, hash);
22897 break;
22898 case DW_OP_skip:
22899 case DW_OP_bra:
22901 int offset;
22903 gcc_assert (val1->val_class == dw_val_class_loc);
22904 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22905 hash = iterative_hash_object (offset, hash);
22907 break;
22908 case DW_OP_implicit_value:
22909 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22910 switch (val2->val_class)
22912 case dw_val_class_const:
22913 hash = iterative_hash_object (val2->v.val_int, hash);
22914 break;
22915 case dw_val_class_vec:
22917 unsigned int elt_size = val2->v.val_vec.elt_size;
22918 unsigned int len = val2->v.val_vec.length;
22920 hash = iterative_hash_object (elt_size, hash);
22921 hash = iterative_hash_object (len, hash);
22922 hash = iterative_hash (val2->v.val_vec.array,
22923 len * elt_size, hash);
22925 break;
22926 case dw_val_class_const_double:
22927 hash = iterative_hash_object (val2->v.val_double.low, hash);
22928 hash = iterative_hash_object (val2->v.val_double.high, hash);
22929 break;
22930 case dw_val_class_addr:
22931 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22932 break;
22933 default:
22934 gcc_unreachable ();
22936 break;
22937 case DW_OP_bregx:
22938 case DW_OP_bit_piece:
22939 hash = iterative_hash_object (val1->v.val_int, hash);
22940 hash = iterative_hash_object (val2->v.val_int, hash);
22941 break;
22942 case DW_OP_addr:
22943 hash_addr:
22944 if (loc->dtprel)
22946 unsigned char dtprel = 0xd1;
22947 hash = iterative_hash_object (dtprel, hash);
22949 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22950 break;
22951 case DW_OP_GNU_implicit_pointer:
22952 hash = iterative_hash_object (val2->v.val_int, hash);
22953 break;
22955 default:
22956 /* Other codes have no operands. */
22957 break;
22959 return hash;
22962 /* Iteratively hash the whole DWARF location expression LOC. */
22964 static inline hashval_t
22965 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22967 dw_loc_descr_ref l;
22968 bool sizes_computed = false;
22969 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
22970 size_of_locs (loc);
22972 for (l = loc; l != NULL; l = l->dw_loc_next)
22974 enum dwarf_location_atom opc = l->dw_loc_opc;
22975 hash = iterative_hash_object (opc, hash);
22976 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22978 size_of_locs (loc);
22979 sizes_computed = true;
22981 hash = hash_loc_operands (l, hash);
22983 return hash;
22986 /* Compute hash of the whole location list LIST_HEAD. */
22988 static inline void
22989 hash_loc_list (dw_loc_list_ref list_head)
22991 dw_loc_list_ref curr = list_head;
22992 hashval_t hash = 0;
22994 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22996 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22997 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22998 if (curr->section)
22999 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23000 hash);
23001 hash = hash_locs (curr->expr, hash);
23003 list_head->hash = hash;
23006 /* Return true if X and Y opcodes have the same operands. */
23008 static inline bool
23009 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23011 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23012 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23013 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23014 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23016 switch (x->dw_loc_opc)
23018 case DW_OP_const4u:
23019 case DW_OP_const8u:
23020 if (x->dtprel)
23021 goto hash_addr;
23022 /* FALLTHRU */
23023 case DW_OP_const1u:
23024 case DW_OP_const1s:
23025 case DW_OP_const2u:
23026 case DW_OP_const2s:
23027 case DW_OP_const4s:
23028 case DW_OP_const8s:
23029 case DW_OP_constu:
23030 case DW_OP_consts:
23031 case DW_OP_pick:
23032 case DW_OP_plus_uconst:
23033 case DW_OP_breg0:
23034 case DW_OP_breg1:
23035 case DW_OP_breg2:
23036 case DW_OP_breg3:
23037 case DW_OP_breg4:
23038 case DW_OP_breg5:
23039 case DW_OP_breg6:
23040 case DW_OP_breg7:
23041 case DW_OP_breg8:
23042 case DW_OP_breg9:
23043 case DW_OP_breg10:
23044 case DW_OP_breg11:
23045 case DW_OP_breg12:
23046 case DW_OP_breg13:
23047 case DW_OP_breg14:
23048 case DW_OP_breg15:
23049 case DW_OP_breg16:
23050 case DW_OP_breg17:
23051 case DW_OP_breg18:
23052 case DW_OP_breg19:
23053 case DW_OP_breg20:
23054 case DW_OP_breg21:
23055 case DW_OP_breg22:
23056 case DW_OP_breg23:
23057 case DW_OP_breg24:
23058 case DW_OP_breg25:
23059 case DW_OP_breg26:
23060 case DW_OP_breg27:
23061 case DW_OP_breg28:
23062 case DW_OP_breg29:
23063 case DW_OP_breg30:
23064 case DW_OP_breg31:
23065 case DW_OP_regx:
23066 case DW_OP_fbreg:
23067 case DW_OP_piece:
23068 case DW_OP_deref_size:
23069 case DW_OP_xderef_size:
23070 return valx1->v.val_int == valy1->v.val_int;
23071 case DW_OP_skip:
23072 case DW_OP_bra:
23073 gcc_assert (valx1->val_class == dw_val_class_loc
23074 && valy1->val_class == dw_val_class_loc
23075 && x->dw_loc_addr == y->dw_loc_addr);
23076 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23077 case DW_OP_implicit_value:
23078 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23079 || valx2->val_class != valy2->val_class)
23080 return false;
23081 switch (valx2->val_class)
23083 case dw_val_class_const:
23084 return valx2->v.val_int == valy2->v.val_int;
23085 case dw_val_class_vec:
23086 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23087 && valx2->v.val_vec.length == valy2->v.val_vec.length
23088 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23089 valx2->v.val_vec.elt_size
23090 * valx2->v.val_vec.length) == 0;
23091 case dw_val_class_const_double:
23092 return valx2->v.val_double.low == valy2->v.val_double.low
23093 && valx2->v.val_double.high == valy2->v.val_double.high;
23094 case dw_val_class_addr:
23095 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23096 default:
23097 gcc_unreachable ();
23099 case DW_OP_bregx:
23100 case DW_OP_bit_piece:
23101 return valx1->v.val_int == valy1->v.val_int
23102 && valx2->v.val_int == valy2->v.val_int;
23103 case DW_OP_addr:
23104 hash_addr:
23105 return rtx_equal_p (valx1->v.val_addr, valx2->v.val_addr);
23106 case DW_OP_GNU_implicit_pointer:
23107 return valx1->val_class == dw_val_class_die_ref
23108 && valx1->val_class == valy1->val_class
23109 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23110 && valx2->v.val_int == valy2->v.val_int;
23111 default:
23112 /* Other codes have no operands. */
23113 return true;
23117 /* Return true if DWARF location expressions X and Y are the same. */
23119 static inline bool
23120 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23122 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23123 if (x->dw_loc_opc != y->dw_loc_opc
23124 || x->dtprel != y->dtprel
23125 || !compare_loc_operands (x, y))
23126 break;
23127 return x == NULL && y == NULL;
23130 /* Return precomputed hash of location list X. */
23132 static hashval_t
23133 loc_list_hash (const void *x)
23135 return ((const struct dw_loc_list_struct *) x)->hash;
23138 /* Return 1 if location lists X and Y are the same. */
23140 static int
23141 loc_list_eq (const void *x, const void *y)
23143 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23144 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23145 if (a == b)
23146 return 1;
23147 if (a->hash != b->hash)
23148 return 0;
23149 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23150 if (strcmp (a->begin, b->begin) != 0
23151 || strcmp (a->end, b->end) != 0
23152 || (a->section == NULL) != (b->section == NULL)
23153 || (a->section && strcmp (a->section, b->section) != 0)
23154 || !compare_locs (a->expr, b->expr))
23155 break;
23156 return a == NULL && b == NULL;
23159 /* Recursively optimize location lists referenced from DIE
23160 children and share them whenever possible. */
23162 static void
23163 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23165 dw_die_ref c;
23166 dw_attr_ref a;
23167 unsigned ix;
23168 void **slot;
23170 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23171 if (AT_class (a) == dw_val_class_loc_list)
23173 dw_loc_list_ref list = AT_loc_list (a);
23174 /* TODO: perform some optimizations here, before hashing
23175 it and storing into the hash table. */
23176 hash_loc_list (list);
23177 slot = htab_find_slot_with_hash (htab, list, list->hash,
23178 INSERT);
23179 if (*slot == NULL)
23180 *slot = (void *) list;
23181 else
23182 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23185 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23188 /* Optimize location lists referenced from DIE
23189 children and share them whenever possible. */
23191 static void
23192 optimize_location_lists (dw_die_ref die)
23194 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23195 optimize_location_lists_1 (die, htab);
23196 htab_delete (htab);
23199 /* Output stuff that dwarf requires at the end of every file,
23200 and generate the DWARF-2 debugging info. */
23202 static void
23203 dwarf2out_finish (const char *filename)
23205 limbo_die_node *node, *next_node;
23206 comdat_type_node *ctnode;
23207 htab_t comdat_type_table;
23208 unsigned int i;
23210 gen_scheduled_generic_parms_dies ();
23211 gen_remaining_tmpl_value_param_die_attribute ();
23213 /* Add the name for the main input file now. We delayed this from
23214 dwarf2out_init to avoid complications with PCH. */
23215 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23216 if (!IS_ABSOLUTE_PATH (filename))
23217 add_comp_dir_attribute (comp_unit_die ());
23218 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23220 bool p = false;
23221 htab_traverse (file_table, file_table_relative_p, &p);
23222 if (p)
23223 add_comp_dir_attribute (comp_unit_die ());
23226 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23228 add_location_or_const_value_attribute (
23229 VEC_index (deferred_locations, deferred_locations_list, i)->die,
23230 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23231 DW_AT_location);
23234 /* Traverse the limbo die list, and add parent/child links. The only
23235 dies without parents that should be here are concrete instances of
23236 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23237 For concrete instances, we can get the parent die from the abstract
23238 instance. */
23239 for (node = limbo_die_list; node; node = next_node)
23241 dw_die_ref die = node->die;
23242 next_node = node->next;
23244 if (die->die_parent == NULL)
23246 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23248 if (origin)
23249 add_child_die (origin->die_parent, die);
23250 else if (is_cu_die (die))
23252 else if (seen_error ())
23253 /* It's OK to be confused by errors in the input. */
23254 add_child_die (comp_unit_die (), die);
23255 else
23257 /* In certain situations, the lexical block containing a
23258 nested function can be optimized away, which results
23259 in the nested function die being orphaned. Likewise
23260 with the return type of that nested function. Force
23261 this to be a child of the containing function.
23263 It may happen that even the containing function got fully
23264 inlined and optimized out. In that case we are lost and
23265 assign the empty child. This should not be big issue as
23266 the function is likely unreachable too. */
23267 tree context = NULL_TREE;
23269 gcc_assert (node->created_for);
23271 if (DECL_P (node->created_for))
23272 context = DECL_CONTEXT (node->created_for);
23273 else if (TYPE_P (node->created_for))
23274 context = TYPE_CONTEXT (node->created_for);
23276 gcc_assert (context
23277 && (TREE_CODE (context) == FUNCTION_DECL
23278 || TREE_CODE (context) == NAMESPACE_DECL));
23280 origin = lookup_decl_die (context);
23281 if (origin)
23282 add_child_die (origin, die);
23283 else
23284 add_child_die (comp_unit_die (), die);
23289 limbo_die_list = NULL;
23291 resolve_addr (comp_unit_die ());
23293 for (node = deferred_asm_name; node; node = node->next)
23295 tree decl = node->created_for;
23296 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23298 add_linkage_attr (node->die, decl);
23299 move_linkage_attr (node->die);
23303 deferred_asm_name = NULL;
23305 /* Walk through the list of incomplete types again, trying once more to
23306 emit full debugging info for them. */
23307 retry_incomplete_types ();
23309 if (flag_eliminate_unused_debug_types)
23310 prune_unused_types ();
23312 /* Generate separate CUs for each of the include files we've seen.
23313 They will go into limbo_die_list. */
23314 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23315 break_out_includes (comp_unit_die ());
23317 /* Generate separate COMDAT sections for type DIEs. */
23318 if (dwarf_version >= 4)
23320 break_out_comdat_types (comp_unit_die ());
23322 /* Each new type_unit DIE was added to the limbo die list when created.
23323 Since these have all been added to comdat_type_list, clear the
23324 limbo die list. */
23325 limbo_die_list = NULL;
23327 /* For each new comdat type unit, copy declarations for incomplete
23328 types to make the new unit self-contained (i.e., no direct
23329 references to the main compile unit). */
23330 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23331 copy_decls_for_unworthy_types (ctnode->root_die);
23332 copy_decls_for_unworthy_types (comp_unit_die ());
23334 /* In the process of copying declarations from one unit to another,
23335 we may have left some declarations behind that are no longer
23336 referenced. Prune them. */
23337 prune_unused_types ();
23340 /* Traverse the DIE's and add add sibling attributes to those DIE's
23341 that have children. */
23342 add_sibling_attributes (comp_unit_die ());
23343 for (node = limbo_die_list; node; node = node->next)
23344 add_sibling_attributes (node->die);
23345 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23346 add_sibling_attributes (ctnode->root_die);
23348 /* Output a terminator label for the .text section. */
23349 switch_to_section (text_section);
23350 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23351 if (cold_text_section)
23353 switch_to_section (cold_text_section);
23354 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23357 /* We can only use the low/high_pc attributes if all of the code was
23358 in .text. */
23359 if (!have_multiple_function_sections
23360 || (dwarf_version < 3 && dwarf_strict))
23362 add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23363 add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23366 else
23368 unsigned fde_idx = 0;
23369 bool range_list_added = false;
23371 /* We need to give .debug_loc and .debug_ranges an appropriate
23372 "base address". Use zero so that these addresses become
23373 absolute. Historically, we've emitted the unexpected
23374 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23375 Emit both to give time for other tools to adapt. */
23376 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23377 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23379 if (text_section_used)
23380 add_ranges_by_labels (comp_unit_die (), text_section_label,
23381 text_end_label, &range_list_added);
23382 if (flag_reorder_blocks_and_partition && cold_text_section_used)
23383 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23384 cold_end_label, &range_list_added);
23386 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23388 dw_fde_ref fde = &fde_table[fde_idx];
23390 if (fde->dw_fde_switched_sections)
23392 if (!fde->in_std_section)
23393 add_ranges_by_labels (comp_unit_die (),
23394 fde->dw_fde_hot_section_label,
23395 fde->dw_fde_hot_section_end_label,
23396 &range_list_added);
23397 if (!fde->cold_in_std_section)
23398 add_ranges_by_labels (comp_unit_die (),
23399 fde->dw_fde_unlikely_section_label,
23400 fde->dw_fde_unlikely_section_end_label,
23401 &range_list_added);
23403 else if (!fde->in_std_section)
23404 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23405 fde->dw_fde_end, &range_list_added);
23408 if (range_list_added)
23409 add_ranges (NULL);
23412 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23413 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23414 debug_line_section_label);
23416 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23417 add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23419 if (have_location_lists)
23420 optimize_location_lists (comp_unit_die ());
23422 /* Output all of the compilation units. We put the main one last so that
23423 the offsets are available to output_pubnames. */
23424 for (node = limbo_die_list; node; node = node->next)
23425 output_comp_unit (node->die, 0);
23427 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23428 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23430 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23432 /* Don't output duplicate types. */
23433 if (*slot != HTAB_EMPTY_ENTRY)
23434 continue;
23436 /* Add a pointer to the line table for the main compilation unit
23437 so that the debugger can make sense of DW_AT_decl_file
23438 attributes. */
23439 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23440 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23441 debug_line_section_label);
23443 output_comdat_type_unit (ctnode);
23444 *slot = ctnode;
23446 htab_delete (comdat_type_table);
23448 /* Output the main compilation unit if non-empty or if .debug_macinfo
23449 will be emitted. */
23450 output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23452 /* Output the abbreviation table. */
23453 switch_to_section (debug_abbrev_section);
23454 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23455 output_abbrev_section ();
23457 /* Output location list section if necessary. */
23458 if (have_location_lists)
23460 /* Output the location lists info. */
23461 switch_to_section (debug_loc_section);
23462 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23463 DEBUG_LOC_SECTION_LABEL, 0);
23464 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23465 output_location_lists (comp_unit_die ());
23468 /* Output public names table if necessary. */
23469 if (!VEC_empty (pubname_entry, pubname_table))
23471 gcc_assert (info_section_emitted);
23472 switch_to_section (debug_pubnames_section);
23473 output_pubnames (pubname_table);
23476 /* Output public types table if necessary. */
23477 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23478 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23479 simply won't look for the section. */
23480 if (!VEC_empty (pubname_entry, pubtype_table))
23482 bool empty = false;
23484 if (flag_eliminate_unused_debug_types)
23486 /* The pubtypes table might be emptied by pruning unused items. */
23487 unsigned i;
23488 pubname_ref p;
23489 empty = true;
23490 FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23491 if (p->die->die_offset != 0)
23493 empty = false;
23494 break;
23497 if (!empty)
23499 gcc_assert (info_section_emitted);
23500 switch_to_section (debug_pubtypes_section);
23501 output_pubnames (pubtype_table);
23505 /* Output direct and virtual call tables if necessary. */
23506 if (!VEC_empty (dcall_entry, dcall_table))
23508 switch_to_section (debug_dcall_section);
23509 output_dcall_table ();
23511 if (!VEC_empty (vcall_entry, vcall_table))
23513 switch_to_section (debug_vcall_section);
23514 output_vcall_table ();
23517 /* Output the address range information. We only put functions in the arange
23518 table, so don't write it out if we don't have any. */
23519 if (arange_table_in_use)
23521 switch_to_section (debug_aranges_section);
23522 output_aranges ();
23525 /* Output ranges section if necessary. */
23526 if (ranges_table_in_use)
23528 switch_to_section (debug_ranges_section);
23529 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23530 output_ranges ();
23533 /* Output the source line correspondence table. We must do this
23534 even if there is no line information. Otherwise, on an empty
23535 translation unit, we will generate a present, but empty,
23536 .debug_info section. IRIX 6.5 `nm' will then complain when
23537 examining the file. This is done late so that any filenames
23538 used by the debug_info section are marked as 'used'. */
23539 switch_to_section (debug_line_section);
23540 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23541 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23542 output_line_info ();
23544 /* Have to end the macro section. */
23545 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23547 switch_to_section (debug_macinfo_section);
23548 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23549 if (!VEC_empty (macinfo_entry, macinfo_table))
23550 output_macinfo ();
23551 dw2_asm_output_data (1, 0, "End compilation unit");
23554 /* If we emitted any DW_FORM_strp form attribute, output the string
23555 table too. */
23556 if (debug_str_hash)
23557 htab_traverse (debug_str_hash, output_indirect_string, NULL);
23560 #include "gt-dwarf2out.h"