2011-04-12 Diego Novillo <dnovillo@google.com>
[official-gcc.git] / gcc / dwarf2out.c
blobaa6f6b6b9cfac5d8f253f36ae9e145e66ed0ca99
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Gary Funck (gary@intrepid.com).
6 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7 Extensively modified by Jason Merrill (jason@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 /* DWARF2 Abbreviation Glossary:
39 CFA = Canonical Frame Address
40 a fixed address on the stack which identifies a call frame.
41 We define it to be the value of SP just before the call insn.
42 The CFA register and offset, which may change during the course
43 of the function, are used to calculate its value at runtime.
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
51 DIE = Debugging Information Entry
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95 #include "cfglayout.h"
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX (gcc_unreachable (), NULL_RTX)
126 #endif
128 /* Map register numbers held in the call frame info that gcc has
129 collected using DWARF_FRAME_REGNUM to those that should be output in
130 .debug_frame and .eh_frame. */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
135 /* Save the result of dwarf2out_do_frame across PCH. */
136 static GTY(()) bool saved_do_cfi_asm = 0;
138 /* Decide whether we want to emit frame unwind information for the current
139 translation unit. */
142 dwarf2out_do_frame (void)
144 /* We want to emit correct CFA location expressions or lists, so we
145 have to return true if we're going to output debug info, even if
146 we're not going to output frame or unwind info. */
147 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148 return true;
150 if (saved_do_cfi_asm)
151 return true;
153 if (targetm.debug_unwind_info () == UI_DWARF2)
154 return true;
156 if ((flag_unwind_tables || flag_exceptions)
157 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158 return true;
160 return false;
163 /* Decide whether to emit frame unwind via assembler directives. */
166 dwarf2out_do_cfi_asm (void)
168 int enc;
170 #ifdef MIPS_DEBUGGING_INFO
171 return false;
172 #endif
173 if (saved_do_cfi_asm)
174 return true;
175 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176 return false;
177 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178 return false;
180 /* Make sure the personality encoding is one the assembler can support.
181 In particular, aligned addresses can't be handled. */
182 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184 return false;
185 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187 return false;
189 /* If we can't get the assembler to emit only .debug_frame, and we don't need
190 dwarf2 unwind info for exceptions, then emit .debug_frame by hand. */
191 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192 && !flag_unwind_tables && !flag_exceptions
193 && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194 return false;
196 saved_do_cfi_asm = true;
197 return true;
200 /* The size of the target's pointer type. */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
205 /* Array of RTXes referenced by the debugging information, which therefore
206 must be kept around forever. */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
209 /* A pointer to the base of a list of incomplete types which might be
210 completed at some later time. incomplete_types_list needs to be a
211 VEC(tree,gc) because we want to tell the garbage collector about
212 it. */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
215 /* A pointer to the base of a table of references to declaration
216 scopes. This table is a display which tracks the nesting
217 of declaration scopes at the current scope and containing
218 scopes. This table is used to find the proper place to
219 define type declaration DIE's. */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
222 /* Pointers to various DWARF2 sections. */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
235 /* Personality decl of current unit. Used only when assembler does not support
236 personality CFI. */
237 static GTY(()) rtx current_unit_personality;
239 /* How to start an assembler comment. */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
248 /* Call frames are described using a sequence of Call Frame
249 Information instructions. The register number, offset
250 and address fields are provided as possible operands;
251 their use is selected by the opcode field. */
253 enum dw_cfi_oprnd_type {
254 dw_cfi_oprnd_unused,
255 dw_cfi_oprnd_reg_num,
256 dw_cfi_oprnd_offset,
257 dw_cfi_oprnd_addr,
258 dw_cfi_oprnd_loc
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
267 dw_cfi_oprnd;
269 typedef struct GTY(()) dw_cfi_struct {
270 dw_cfi_ref dw_cfi_next;
271 enum dwarf_call_frame_info dw_cfi_opc;
272 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
273 dw_cfi_oprnd1;
274 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
275 dw_cfi_oprnd2;
277 dw_cfi_node;
279 /* This is how we define the location of the CFA. We use to handle it
280 as REG + OFFSET all the time, but now it can be more complex.
281 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
282 Instead of passing around REG and OFFSET, we pass a copy
283 of this structure. */
284 typedef struct cfa_loc {
285 HOST_WIDE_INT offset;
286 HOST_WIDE_INT base_offset;
287 unsigned int reg;
288 BOOL_BITFIELD indirect : 1; /* 1 if CFA is accessed via a dereference. */
289 BOOL_BITFIELD in_use : 1; /* 1 if a saved cfa is stored here. */
290 } dw_cfa_location;
292 /* All call frame descriptions (FDE's) in the GCC generated DWARF
293 refer to a single Common Information Entry (CIE), defined at
294 the beginning of the .debug_frame section. This use of a single
295 CIE obviates the need to keep track of multiple CIE's
296 in the DWARF generation routines below. */
298 typedef struct GTY(()) dw_fde_struct {
299 tree decl;
300 const char *dw_fde_begin;
301 const char *dw_fde_current_label;
302 const char *dw_fde_end;
303 const char *dw_fde_vms_end_prologue;
304 const char *dw_fde_vms_begin_epilogue;
305 const char *dw_fde_hot_section_label;
306 const char *dw_fde_hot_section_end_label;
307 const char *dw_fde_unlikely_section_label;
308 const char *dw_fde_unlikely_section_end_label;
309 dw_cfi_ref dw_fde_cfi;
310 dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections. */
311 HOST_WIDE_INT stack_realignment;
312 unsigned funcdef_number;
313 /* Dynamic realign argument pointer register. */
314 unsigned int drap_reg;
315 /* Virtual dynamic realign argument pointer register. */
316 unsigned int vdrap_reg;
317 /* These 3 flags are copied from rtl_data in function.h. */
318 unsigned all_throwers_are_sibcalls : 1;
319 unsigned uses_eh_lsda : 1;
320 unsigned nothrow : 1;
321 /* Whether we did stack realign in this call frame. */
322 unsigned stack_realign : 1;
323 /* Whether dynamic realign argument pointer register has been saved. */
324 unsigned drap_reg_saved: 1;
325 /* True iff dw_fde_begin label is in text_section or cold_text_section. */
326 unsigned in_std_section : 1;
327 /* True iff dw_fde_unlikely_section_label is in text_section or
328 cold_text_section. */
329 unsigned cold_in_std_section : 1;
330 /* True iff switched sections. */
331 unsigned dw_fde_switched_sections : 1;
332 /* True iff switching from cold to hot section. */
333 unsigned dw_fde_switched_cold_to_hot : 1;
335 dw_fde_node;
337 /* Maximum size (in bytes) of an artificially generated label. */
338 #define MAX_ARTIFICIAL_LABEL_BYTES 30
340 /* The size of addresses as they appear in the Dwarf 2 data.
341 Some architectures use word addresses to refer to code locations,
342 but Dwarf 2 info always uses byte addresses. On such machines,
343 Dwarf 2 addresses need to be larger than the architecture's
344 pointers. */
345 #ifndef DWARF2_ADDR_SIZE
346 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
347 #endif
349 /* The size in bytes of a DWARF field indicating an offset or length
350 relative to a debug info section, specified to be 4 bytes in the
351 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
352 as PTR_SIZE. */
354 #ifndef DWARF_OFFSET_SIZE
355 #define DWARF_OFFSET_SIZE 4
356 #endif
358 /* The size in bytes of a DWARF 4 type signature. */
360 #ifndef DWARF_TYPE_SIGNATURE_SIZE
361 #define DWARF_TYPE_SIGNATURE_SIZE 8
362 #endif
364 /* According to the (draft) DWARF 3 specification, the initial length
365 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
366 bytes are 0xffffffff, followed by the length stored in the next 8
367 bytes.
369 However, the SGI/MIPS ABI uses an initial length which is equal to
370 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
372 #ifndef DWARF_INITIAL_LENGTH_SIZE
373 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
374 #endif
376 /* Round SIZE up to the nearest BOUNDARY. */
377 #define DWARF_ROUND(SIZE,BOUNDARY) \
378 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
380 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
381 #ifndef DWARF_CIE_DATA_ALIGNMENT
382 #ifdef STACK_GROWS_DOWNWARD
383 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
384 #else
385 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
386 #endif
387 #endif
389 /* CIE identifier. */
390 #if HOST_BITS_PER_WIDE_INT >= 64
391 #define DWARF_CIE_ID \
392 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
393 #else
394 #define DWARF_CIE_ID DW_CIE_ID
395 #endif
397 /* A pointer to the base of a table that contains frame description
398 information for each routine. */
399 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
401 /* Number of elements currently allocated for fde_table. */
402 static GTY(()) unsigned fde_table_allocated;
404 /* Number of elements in fde_table currently in use. */
405 static GTY(()) unsigned fde_table_in_use;
407 /* Size (in elements) of increments by which we may expand the
408 fde_table. */
409 #define FDE_TABLE_INCREMENT 256
411 /* Get the current fde_table entry we should use. */
413 static inline dw_fde_ref
414 current_fde (void)
416 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
419 /* A list of call frame insns for the CIE. */
420 static GTY(()) dw_cfi_ref cie_cfi_head;
422 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
423 attribute that accelerates the lookup of the FDE associated
424 with the subprogram. This variable holds the table index of the FDE
425 associated with the current function (body) definition. */
426 static unsigned current_funcdef_fde;
428 struct GTY(()) indirect_string_node {
429 const char *str;
430 unsigned int refcount;
431 enum dwarf_form form;
432 char *label;
435 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
437 /* True if the compilation unit has location entries that reference
438 debug strings. */
439 static GTY(()) bool debug_str_hash_forced = false;
441 static GTY(()) int dw2_string_counter;
442 static GTY(()) unsigned long dwarf2out_cfi_label_num;
444 /* True if the compilation unit places functions in more than one section. */
445 static GTY(()) bool have_multiple_function_sections = false;
447 /* Whether the default text and cold text sections have been used at all. */
449 static GTY(()) bool text_section_used = false;
450 static GTY(()) bool cold_text_section_used = false;
452 /* The default cold text section. */
453 static GTY(()) section *cold_text_section;
455 /* Forward declarations for functions defined in this file. */
457 static char *stripattributes (const char *);
458 static const char *dwarf_cfi_name (unsigned);
459 static dw_cfi_ref new_cfi (void);
460 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
461 static void add_fde_cfi (const char *, dw_cfi_ref);
462 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
463 static void lookup_cfa (dw_cfa_location *);
464 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
465 static void initial_return_save (rtx);
466 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
467 HOST_WIDE_INT);
468 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
469 static void output_cfi_directive (dw_cfi_ref);
470 static void output_call_frame_info (int);
471 static void dwarf2out_note_section_used (void);
472 static bool clobbers_queued_reg_save (const_rtx);
473 static void dwarf2out_frame_debug_expr (rtx, const char *);
475 /* Support for complex CFA locations. */
476 static void output_cfa_loc (dw_cfi_ref, int);
477 static void output_cfa_loc_raw (dw_cfi_ref);
478 static void get_cfa_from_loc_descr (dw_cfa_location *,
479 struct dw_loc_descr_struct *);
480 static struct dw_loc_descr_struct *build_cfa_loc
481 (dw_cfa_location *, HOST_WIDE_INT);
482 static struct dw_loc_descr_struct *build_cfa_aligned_loc
483 (HOST_WIDE_INT, HOST_WIDE_INT);
484 static void def_cfa_1 (const char *, dw_cfa_location *);
485 static struct dw_loc_descr_struct *mem_loc_descriptor
486 (rtx, enum machine_mode mode, enum var_init_status);
488 /* How to start an assembler comment. */
489 #ifndef ASM_COMMENT_START
490 #define ASM_COMMENT_START ";#"
491 #endif
493 /* Data and reference forms for relocatable data. */
494 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
495 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
497 #ifndef DEBUG_FRAME_SECTION
498 #define DEBUG_FRAME_SECTION ".debug_frame"
499 #endif
501 #ifndef FUNC_BEGIN_LABEL
502 #define FUNC_BEGIN_LABEL "LFB"
503 #endif
505 #ifndef FUNC_END_LABEL
506 #define FUNC_END_LABEL "LFE"
507 #endif
509 #ifndef PROLOGUE_END_LABEL
510 #define PROLOGUE_END_LABEL "LPE"
511 #endif
513 #ifndef EPILOGUE_BEGIN_LABEL
514 #define EPILOGUE_BEGIN_LABEL "LEB"
515 #endif
517 #ifndef FRAME_BEGIN_LABEL
518 #define FRAME_BEGIN_LABEL "Lframe"
519 #endif
520 #define CIE_AFTER_SIZE_LABEL "LSCIE"
521 #define CIE_END_LABEL "LECIE"
522 #define FDE_LABEL "LSFDE"
523 #define FDE_AFTER_SIZE_LABEL "LASFDE"
524 #define FDE_END_LABEL "LEFDE"
525 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
526 #define LINE_NUMBER_END_LABEL "LELT"
527 #define LN_PROLOG_AS_LABEL "LASLTP"
528 #define LN_PROLOG_END_LABEL "LELTP"
529 #define DIE_LABEL_PREFIX "DW"
531 /* The DWARF 2 CFA column which tracks the return address. Normally this
532 is the column for PC, or the first column after all of the hard
533 registers. */
534 #ifndef DWARF_FRAME_RETURN_COLUMN
535 #ifdef PC_REGNUM
536 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
537 #else
538 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
539 #endif
540 #endif
542 /* The mapping from gcc register number to DWARF 2 CFA column number. By
543 default, we just provide columns for all registers. */
544 #ifndef DWARF_FRAME_REGNUM
545 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
546 #endif
548 /* Match the base name of a file to the base name of a compilation unit. */
550 static int
551 matches_main_base (const char *path)
553 /* Cache the last query. */
554 static const char *last_path = NULL;
555 static int last_match = 0;
556 if (path != last_path)
558 const char *base;
559 int length = base_of_path (path, &base);
560 last_path = path;
561 last_match = (length == main_input_baselength
562 && memcmp (base, main_input_basename, length) == 0);
564 return last_match;
567 #ifdef DEBUG_DEBUG_STRUCT
569 static int
570 dump_struct_debug (tree type, enum debug_info_usage usage,
571 enum debug_struct_file criterion, int generic,
572 int matches, int result)
574 /* Find the type name. */
575 tree type_decl = TYPE_STUB_DECL (type);
576 tree t = type_decl;
577 const char *name = 0;
578 if (TREE_CODE (t) == TYPE_DECL)
579 t = DECL_NAME (t);
580 if (t)
581 name = IDENTIFIER_POINTER (t);
583 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
584 criterion,
585 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
586 matches ? "bas" : "hdr",
587 generic ? "gen" : "ord",
588 usage == DINFO_USAGE_DFN ? ";" :
589 usage == DINFO_USAGE_DIR_USE ? "." : "*",
590 result,
591 (void*) type_decl, name);
592 return result;
594 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
595 dump_struct_debug (type, usage, criterion, generic, matches, result)
597 #else
599 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
600 (result)
602 #endif
604 static bool
605 should_emit_struct_debug (tree type, enum debug_info_usage usage)
607 enum debug_struct_file criterion;
608 tree type_decl;
609 bool generic = lang_hooks.types.generic_p (type);
611 if (generic)
612 criterion = debug_struct_generic[usage];
613 else
614 criterion = debug_struct_ordinary[usage];
616 if (criterion == DINFO_STRUCT_FILE_NONE)
617 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
618 if (criterion == DINFO_STRUCT_FILE_ANY)
619 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
621 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
623 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
624 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
626 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
627 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
628 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
631 /* Hook used by __throw. */
634 expand_builtin_dwarf_sp_column (void)
636 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
637 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
640 /* Return a pointer to a copy of the section string name S with all
641 attributes stripped off, and an asterisk prepended (for assemble_name). */
643 static inline char *
644 stripattributes (const char *s)
646 char *stripped = XNEWVEC (char, strlen (s) + 2);
647 char *p = stripped;
649 *p++ = '*';
651 while (*s && *s != ',')
652 *p++ = *s++;
654 *p = '\0';
655 return stripped;
658 /* MEM is a memory reference for the register size table, each element of
659 which has mode MODE. Initialize column C as a return address column. */
661 static void
662 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
664 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
665 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
666 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
669 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
671 static inline HOST_WIDE_INT
672 div_data_align (HOST_WIDE_INT off)
674 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
675 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
676 return r;
679 /* Return true if we need a signed version of a given opcode
680 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
682 static inline bool
683 need_data_align_sf_opcode (HOST_WIDE_INT off)
685 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
688 /* Generate code to initialize the register size table. */
690 void
691 expand_builtin_init_dwarf_reg_sizes (tree address)
693 unsigned int i;
694 enum machine_mode mode = TYPE_MODE (char_type_node);
695 rtx addr = expand_normal (address);
696 rtx mem = gen_rtx_MEM (BLKmode, addr);
697 bool wrote_return_column = false;
699 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
701 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
703 if (rnum < DWARF_FRAME_REGISTERS)
705 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
706 enum machine_mode save_mode = reg_raw_mode[i];
707 HOST_WIDE_INT size;
709 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
710 save_mode = choose_hard_reg_mode (i, 1, true);
711 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
713 if (save_mode == VOIDmode)
714 continue;
715 wrote_return_column = true;
717 size = GET_MODE_SIZE (save_mode);
718 if (offset < 0)
719 continue;
721 emit_move_insn (adjust_address (mem, mode, offset),
722 gen_int_mode (size, mode));
726 if (!wrote_return_column)
727 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
729 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
730 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
731 #endif
733 targetm.init_dwarf_reg_sizes_extra (address);
736 /* Convert a DWARF call frame info. operation to its string name */
738 static const char *
739 dwarf_cfi_name (unsigned int cfi_opc)
741 switch (cfi_opc)
743 case DW_CFA_advance_loc:
744 return "DW_CFA_advance_loc";
745 case DW_CFA_offset:
746 return "DW_CFA_offset";
747 case DW_CFA_restore:
748 return "DW_CFA_restore";
749 case DW_CFA_nop:
750 return "DW_CFA_nop";
751 case DW_CFA_set_loc:
752 return "DW_CFA_set_loc";
753 case DW_CFA_advance_loc1:
754 return "DW_CFA_advance_loc1";
755 case DW_CFA_advance_loc2:
756 return "DW_CFA_advance_loc2";
757 case DW_CFA_advance_loc4:
758 return "DW_CFA_advance_loc4";
759 case DW_CFA_offset_extended:
760 return "DW_CFA_offset_extended";
761 case DW_CFA_restore_extended:
762 return "DW_CFA_restore_extended";
763 case DW_CFA_undefined:
764 return "DW_CFA_undefined";
765 case DW_CFA_same_value:
766 return "DW_CFA_same_value";
767 case DW_CFA_register:
768 return "DW_CFA_register";
769 case DW_CFA_remember_state:
770 return "DW_CFA_remember_state";
771 case DW_CFA_restore_state:
772 return "DW_CFA_restore_state";
773 case DW_CFA_def_cfa:
774 return "DW_CFA_def_cfa";
775 case DW_CFA_def_cfa_register:
776 return "DW_CFA_def_cfa_register";
777 case DW_CFA_def_cfa_offset:
778 return "DW_CFA_def_cfa_offset";
780 /* DWARF 3 */
781 case DW_CFA_def_cfa_expression:
782 return "DW_CFA_def_cfa_expression";
783 case DW_CFA_expression:
784 return "DW_CFA_expression";
785 case DW_CFA_offset_extended_sf:
786 return "DW_CFA_offset_extended_sf";
787 case DW_CFA_def_cfa_sf:
788 return "DW_CFA_def_cfa_sf";
789 case DW_CFA_def_cfa_offset_sf:
790 return "DW_CFA_def_cfa_offset_sf";
792 /* SGI/MIPS specific */
793 case DW_CFA_MIPS_advance_loc8:
794 return "DW_CFA_MIPS_advance_loc8";
796 /* GNU extensions */
797 case DW_CFA_GNU_window_save:
798 return "DW_CFA_GNU_window_save";
799 case DW_CFA_GNU_args_size:
800 return "DW_CFA_GNU_args_size";
801 case DW_CFA_GNU_negative_offset_extended:
802 return "DW_CFA_GNU_negative_offset_extended";
804 default:
805 return "DW_CFA_<unknown>";
809 /* Return a pointer to a newly allocated Call Frame Instruction. */
811 static inline dw_cfi_ref
812 new_cfi (void)
814 dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
816 cfi->dw_cfi_next = NULL;
817 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
818 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
820 return cfi;
823 /* Add a Call Frame Instruction to list of instructions. */
825 static inline void
826 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
828 dw_cfi_ref *p;
829 dw_fde_ref fde = current_fde ();
831 /* When DRAP is used, CFA is defined with an expression. Redefine
832 CFA may lead to a different CFA value. */
833 /* ??? Of course, this heuristic fails when we're annotating epilogues,
834 because of course we'll always want to redefine the CFA back to the
835 stack pointer on the way out. Where should we move this check? */
836 if (0 && fde && fde->drap_reg != INVALID_REGNUM)
837 switch (cfi->dw_cfi_opc)
839 case DW_CFA_def_cfa_register:
840 case DW_CFA_def_cfa_offset:
841 case DW_CFA_def_cfa_offset_sf:
842 case DW_CFA_def_cfa:
843 case DW_CFA_def_cfa_sf:
844 gcc_unreachable ();
846 default:
847 break;
850 /* Find the end of the chain. */
851 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
854 *p = cfi;
857 /* Generate a new label for the CFI info to refer to. FORCE is true
858 if a label needs to be output even when using .cfi_* directives. */
860 char *
861 dwarf2out_cfi_label (bool force)
863 static char label[20];
865 if (!force && dwarf2out_do_cfi_asm ())
867 /* In this case, we will be emitting the asm directive instead of
868 the label, so just return a placeholder to keep the rest of the
869 interfaces happy. */
870 strcpy (label, "<do not output>");
872 else
874 int num = dwarf2out_cfi_label_num++;
875 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
876 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
879 return label;
882 /* True if remember_state should be emitted before following CFI directive. */
883 static bool emit_cfa_remember;
885 /* True if any CFI directives were emitted at the current insn. */
886 static bool any_cfis_emitted;
888 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
889 or to the CIE if LABEL is NULL. */
891 static void
892 add_fde_cfi (const char *label, dw_cfi_ref cfi)
894 dw_cfi_ref *list_head;
896 if (emit_cfa_remember)
898 dw_cfi_ref cfi_remember;
900 /* Emit the state save. */
901 emit_cfa_remember = false;
902 cfi_remember = new_cfi ();
903 cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
904 add_fde_cfi (label, cfi_remember);
907 list_head = &cie_cfi_head;
909 if (dwarf2out_do_cfi_asm ())
911 if (label)
913 dw_fde_ref fde = current_fde ();
915 gcc_assert (fde != NULL);
917 /* We still have to add the cfi to the list so that lookup_cfa
918 works later on. When -g2 and above we even need to force
919 emitting of CFI labels and add to list a DW_CFA_set_loc for
920 convert_cfa_to_fb_loc_list purposes. If we're generating
921 DWARF3 output we use DW_OP_call_frame_cfa and so don't use
922 convert_cfa_to_fb_loc_list. */
923 if (dwarf_version == 2
924 && debug_info_level > DINFO_LEVEL_TERSE
925 && (write_symbols == DWARF2_DEBUG
926 || write_symbols == VMS_AND_DWARF2_DEBUG))
928 switch (cfi->dw_cfi_opc)
930 case DW_CFA_def_cfa_offset:
931 case DW_CFA_def_cfa_offset_sf:
932 case DW_CFA_def_cfa_register:
933 case DW_CFA_def_cfa:
934 case DW_CFA_def_cfa_sf:
935 case DW_CFA_def_cfa_expression:
936 case DW_CFA_restore_state:
937 if (*label == 0 || strcmp (label, "<do not output>") == 0)
938 label = dwarf2out_cfi_label (true);
940 if (fde->dw_fde_current_label == NULL
941 || strcmp (label, fde->dw_fde_current_label) != 0)
943 dw_cfi_ref xcfi;
945 label = xstrdup (label);
947 /* Set the location counter to the new label. */
948 xcfi = new_cfi ();
949 /* It doesn't metter whether DW_CFA_set_loc
950 or DW_CFA_advance_loc4 is added here, those aren't
951 emitted into assembly, only looked up by
952 convert_cfa_to_fb_loc_list. */
953 xcfi->dw_cfi_opc = DW_CFA_set_loc;
954 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
955 add_cfi (&fde->dw_fde_cfi, xcfi);
956 fde->dw_fde_current_label = label;
958 break;
959 default:
960 break;
964 output_cfi_directive (cfi);
966 list_head = &fde->dw_fde_cfi;
967 any_cfis_emitted = true;
969 /* ??? If this is a CFI for the CIE, we don't emit. This
970 assumes that the standard CIE contents that the assembler
971 uses matches the standard CIE contents that the compiler
972 uses. This is probably a bad assumption. I'm not quite
973 sure how to address this for now. */
975 else if (label)
977 dw_fde_ref fde = current_fde ();
979 gcc_assert (fde != NULL);
981 if (*label == 0)
982 label = dwarf2out_cfi_label (false);
984 if (fde->dw_fde_current_label == NULL
985 || strcmp (label, fde->dw_fde_current_label) != 0)
987 dw_cfi_ref xcfi;
989 label = xstrdup (label);
991 /* Set the location counter to the new label. */
992 xcfi = new_cfi ();
993 /* If we have a current label, advance from there, otherwise
994 set the location directly using set_loc. */
995 xcfi->dw_cfi_opc = fde->dw_fde_current_label
996 ? DW_CFA_advance_loc4
997 : DW_CFA_set_loc;
998 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
999 add_cfi (&fde->dw_fde_cfi, xcfi);
1001 fde->dw_fde_current_label = label;
1004 list_head = &fde->dw_fde_cfi;
1005 any_cfis_emitted = true;
1008 add_cfi (list_head, cfi);
1011 /* Subroutine of lookup_cfa. */
1013 static void
1014 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1016 switch (cfi->dw_cfi_opc)
1018 case DW_CFA_def_cfa_offset:
1019 case DW_CFA_def_cfa_offset_sf:
1020 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1021 break;
1022 case DW_CFA_def_cfa_register:
1023 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1024 break;
1025 case DW_CFA_def_cfa:
1026 case DW_CFA_def_cfa_sf:
1027 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1028 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1029 break;
1030 case DW_CFA_def_cfa_expression:
1031 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1032 break;
1034 case DW_CFA_remember_state:
1035 gcc_assert (!remember->in_use);
1036 *remember = *loc;
1037 remember->in_use = 1;
1038 break;
1039 case DW_CFA_restore_state:
1040 gcc_assert (remember->in_use);
1041 *loc = *remember;
1042 remember->in_use = 0;
1043 break;
1045 default:
1046 break;
1050 /* Find the previous value for the CFA. */
1052 static void
1053 lookup_cfa (dw_cfa_location *loc)
1055 dw_cfi_ref cfi;
1056 dw_fde_ref fde;
1057 dw_cfa_location remember;
1059 memset (loc, 0, sizeof (*loc));
1060 loc->reg = INVALID_REGNUM;
1061 remember = *loc;
1063 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1064 lookup_cfa_1 (cfi, loc, &remember);
1066 fde = current_fde ();
1067 if (fde)
1068 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1069 lookup_cfa_1 (cfi, loc, &remember);
1072 /* The current rule for calculating the DWARF2 canonical frame address. */
1073 static dw_cfa_location cfa;
1075 /* The register used for saving registers to the stack, and its offset
1076 from the CFA. */
1077 static dw_cfa_location cfa_store;
1079 /* The current save location around an epilogue. */
1080 static dw_cfa_location cfa_remember;
1082 /* The running total of the size of arguments pushed onto the stack. */
1083 static HOST_WIDE_INT args_size;
1085 /* The last args_size we actually output. */
1086 static HOST_WIDE_INT old_args_size;
1088 /* Entry point to update the canonical frame address (CFA).
1089 LABEL is passed to add_fde_cfi. The value of CFA is now to be
1090 calculated from REG+OFFSET. */
1092 void
1093 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1095 dw_cfa_location loc;
1096 loc.indirect = 0;
1097 loc.base_offset = 0;
1098 loc.reg = reg;
1099 loc.offset = offset;
1100 def_cfa_1 (label, &loc);
1103 /* Determine if two dw_cfa_location structures define the same data. */
1105 static bool
1106 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1108 return (loc1->reg == loc2->reg
1109 && loc1->offset == loc2->offset
1110 && loc1->indirect == loc2->indirect
1111 && (loc1->indirect == 0
1112 || loc1->base_offset == loc2->base_offset));
1115 /* This routine does the actual work. The CFA is now calculated from
1116 the dw_cfa_location structure. */
1118 static void
1119 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1121 dw_cfi_ref cfi;
1122 dw_cfa_location old_cfa, loc;
1124 cfa = *loc_p;
1125 loc = *loc_p;
1127 if (cfa_store.reg == loc.reg && loc.indirect == 0)
1128 cfa_store.offset = loc.offset;
1130 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1131 lookup_cfa (&old_cfa);
1133 /* If nothing changed, no need to issue any call frame instructions. */
1134 if (cfa_equal_p (&loc, &old_cfa))
1135 return;
1137 cfi = new_cfi ();
1139 if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1141 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1142 the CFA register did not change but the offset did. The data
1143 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1144 in the assembler via the .cfi_def_cfa_offset directive. */
1145 if (loc.offset < 0)
1146 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1147 else
1148 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1149 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1152 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
1153 else if (loc.offset == old_cfa.offset
1154 && old_cfa.reg != INVALID_REGNUM
1155 && !loc.indirect
1156 && !old_cfa.indirect)
1158 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1159 indicating the CFA register has changed to <register> but the
1160 offset has not changed. */
1161 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1162 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1164 #endif
1166 else if (loc.indirect == 0)
1168 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1169 indicating the CFA register has changed to <register> with
1170 the specified offset. The data factoring for DW_CFA_def_cfa_sf
1171 happens in output_cfi, or in the assembler via the .cfi_def_cfa
1172 directive. */
1173 if (loc.offset < 0)
1174 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1175 else
1176 cfi->dw_cfi_opc = DW_CFA_def_cfa;
1177 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1178 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1180 else
1182 /* Construct a DW_CFA_def_cfa_expression instruction to
1183 calculate the CFA using a full location expression since no
1184 register-offset pair is available. */
1185 struct dw_loc_descr_struct *loc_list;
1187 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1188 loc_list = build_cfa_loc (&loc, 0);
1189 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1192 add_fde_cfi (label, cfi);
1195 /* Add the CFI for saving a register. REG is the CFA column number.
1196 LABEL is passed to add_fde_cfi.
1197 If SREG is -1, the register is saved at OFFSET from the CFA;
1198 otherwise it is saved in SREG. */
1200 static void
1201 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1203 dw_cfi_ref cfi = new_cfi ();
1204 dw_fde_ref fde = current_fde ();
1206 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1208 /* When stack is aligned, store REG using DW_CFA_expression with
1209 FP. */
1210 if (fde
1211 && fde->stack_realign
1212 && sreg == INVALID_REGNUM)
1214 cfi->dw_cfi_opc = DW_CFA_expression;
1215 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1216 cfi->dw_cfi_oprnd2.dw_cfi_loc
1217 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1219 else if (sreg == INVALID_REGNUM)
1221 if (need_data_align_sf_opcode (offset))
1222 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1223 else if (reg & ~0x3f)
1224 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1225 else
1226 cfi->dw_cfi_opc = DW_CFA_offset;
1227 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1229 else if (sreg == reg)
1230 cfi->dw_cfi_opc = DW_CFA_same_value;
1231 else
1233 cfi->dw_cfi_opc = DW_CFA_register;
1234 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1237 add_fde_cfi (label, cfi);
1240 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1241 This CFI tells the unwinder that it needs to restore the window registers
1242 from the previous frame's window save area.
1244 ??? Perhaps we should note in the CIE where windows are saved (instead of
1245 assuming 0(cfa)) and what registers are in the window. */
1247 void
1248 dwarf2out_window_save (const char *label)
1250 dw_cfi_ref cfi = new_cfi ();
1252 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1253 add_fde_cfi (label, cfi);
1256 /* Entry point for saving a register to the stack. REG is the GCC register
1257 number. LABEL and OFFSET are passed to reg_save. */
1259 void
1260 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1262 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1265 /* Entry point for saving the return address in the stack.
1266 LABEL and OFFSET are passed to reg_save. */
1268 void
1269 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1271 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1274 /* Entry point for saving the return address in a register.
1275 LABEL and SREG are passed to reg_save. */
1277 void
1278 dwarf2out_return_reg (const char *label, unsigned int sreg)
1280 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1283 /* Record the initial position of the return address. RTL is
1284 INCOMING_RETURN_ADDR_RTX. */
1286 static void
1287 initial_return_save (rtx rtl)
1289 unsigned int reg = INVALID_REGNUM;
1290 HOST_WIDE_INT offset = 0;
1292 switch (GET_CODE (rtl))
1294 case REG:
1295 /* RA is in a register. */
1296 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1297 break;
1299 case MEM:
1300 /* RA is on the stack. */
1301 rtl = XEXP (rtl, 0);
1302 switch (GET_CODE (rtl))
1304 case REG:
1305 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1306 offset = 0;
1307 break;
1309 case PLUS:
1310 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1311 offset = INTVAL (XEXP (rtl, 1));
1312 break;
1314 case MINUS:
1315 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1316 offset = -INTVAL (XEXP (rtl, 1));
1317 break;
1319 default:
1320 gcc_unreachable ();
1323 break;
1325 case PLUS:
1326 /* The return address is at some offset from any value we can
1327 actually load. For instance, on the SPARC it is in %i7+8. Just
1328 ignore the offset for now; it doesn't matter for unwinding frames. */
1329 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1330 initial_return_save (XEXP (rtl, 0));
1331 return;
1333 default:
1334 gcc_unreachable ();
1337 if (reg != DWARF_FRAME_RETURN_COLUMN)
1338 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1341 /* Given a SET, calculate the amount of stack adjustment it
1342 contains. */
1344 static HOST_WIDE_INT
1345 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1346 HOST_WIDE_INT cur_offset)
1348 const_rtx src = SET_SRC (pattern);
1349 const_rtx dest = SET_DEST (pattern);
1350 HOST_WIDE_INT offset = 0;
1351 enum rtx_code code;
1353 if (dest == stack_pointer_rtx)
1355 code = GET_CODE (src);
1357 /* Assume (set (reg sp) (reg whatever)) sets args_size
1358 level to 0. */
1359 if (code == REG && src != stack_pointer_rtx)
1361 offset = -cur_args_size;
1362 #ifndef STACK_GROWS_DOWNWARD
1363 offset = -offset;
1364 #endif
1365 return offset - cur_offset;
1368 if (! (code == PLUS || code == MINUS)
1369 || XEXP (src, 0) != stack_pointer_rtx
1370 || !CONST_INT_P (XEXP (src, 1)))
1371 return 0;
1373 /* (set (reg sp) (plus (reg sp) (const_int))) */
1374 offset = INTVAL (XEXP (src, 1));
1375 if (code == PLUS)
1376 offset = -offset;
1377 return offset;
1380 if (MEM_P (src) && !MEM_P (dest))
1381 dest = src;
1382 if (MEM_P (dest))
1384 /* (set (mem (pre_dec (reg sp))) (foo)) */
1385 src = XEXP (dest, 0);
1386 code = GET_CODE (src);
1388 switch (code)
1390 case PRE_MODIFY:
1391 case POST_MODIFY:
1392 if (XEXP (src, 0) == stack_pointer_rtx)
1394 rtx val = XEXP (XEXP (src, 1), 1);
1395 /* We handle only adjustments by constant amount. */
1396 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1397 && CONST_INT_P (val));
1398 offset = -INTVAL (val);
1399 break;
1401 return 0;
1403 case PRE_DEC:
1404 case POST_DEC:
1405 if (XEXP (src, 0) == stack_pointer_rtx)
1407 offset = GET_MODE_SIZE (GET_MODE (dest));
1408 break;
1410 return 0;
1412 case PRE_INC:
1413 case POST_INC:
1414 if (XEXP (src, 0) == stack_pointer_rtx)
1416 offset = -GET_MODE_SIZE (GET_MODE (dest));
1417 break;
1419 return 0;
1421 default:
1422 return 0;
1425 else
1426 return 0;
1428 return offset;
1431 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1432 indexed by INSN_UID. */
1434 static HOST_WIDE_INT *barrier_args_size;
1436 /* Helper function for compute_barrier_args_size. Handle one insn. */
1438 static HOST_WIDE_INT
1439 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1440 VEC (rtx, heap) **next)
1442 HOST_WIDE_INT offset = 0;
1443 int i;
1445 if (! RTX_FRAME_RELATED_P (insn))
1447 if (prologue_epilogue_contains (insn))
1448 /* Nothing */;
1449 else if (GET_CODE (PATTERN (insn)) == SET)
1450 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1451 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1452 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1454 /* There may be stack adjustments inside compound insns. Search
1455 for them. */
1456 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1457 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1458 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1459 cur_args_size, offset);
1462 else
1464 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1466 if (expr)
1468 expr = XEXP (expr, 0);
1469 if (GET_CODE (expr) == PARALLEL
1470 || GET_CODE (expr) == SEQUENCE)
1471 for (i = 1; i < XVECLEN (expr, 0); i++)
1473 rtx elem = XVECEXP (expr, 0, i);
1475 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1476 offset += stack_adjust_offset (elem, cur_args_size, offset);
1481 #ifndef STACK_GROWS_DOWNWARD
1482 offset = -offset;
1483 #endif
1485 cur_args_size += offset;
1486 if (cur_args_size < 0)
1487 cur_args_size = 0;
1489 if (JUMP_P (insn))
1491 rtx dest = JUMP_LABEL (insn);
1493 if (dest)
1495 if (barrier_args_size [INSN_UID (dest)] < 0)
1497 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1498 VEC_safe_push (rtx, heap, *next, dest);
1503 return cur_args_size;
1506 /* Walk the whole function and compute args_size on BARRIERs. */
1508 static void
1509 compute_barrier_args_size (void)
1511 int max_uid = get_max_uid (), i;
1512 rtx insn;
1513 VEC (rtx, heap) *worklist, *next, *tmp;
1515 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1516 for (i = 0; i < max_uid; i++)
1517 barrier_args_size[i] = -1;
1519 worklist = VEC_alloc (rtx, heap, 20);
1520 next = VEC_alloc (rtx, heap, 20);
1521 insn = get_insns ();
1522 barrier_args_size[INSN_UID (insn)] = 0;
1523 VEC_quick_push (rtx, worklist, insn);
1524 for (;;)
1526 while (!VEC_empty (rtx, worklist))
1528 rtx prev, body, first_insn;
1529 HOST_WIDE_INT cur_args_size;
1531 first_insn = insn = VEC_pop (rtx, worklist);
1532 cur_args_size = barrier_args_size[INSN_UID (insn)];
1533 prev = prev_nonnote_insn (insn);
1534 if (prev && BARRIER_P (prev))
1535 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1537 for (; insn; insn = NEXT_INSN (insn))
1539 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1540 continue;
1541 if (BARRIER_P (insn))
1542 break;
1544 if (LABEL_P (insn))
1546 if (insn == first_insn)
1547 continue;
1548 else if (barrier_args_size[INSN_UID (insn)] < 0)
1550 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1551 continue;
1553 else
1555 /* The insns starting with this label have been
1556 already scanned or are in the worklist. */
1557 break;
1561 body = PATTERN (insn);
1562 if (GET_CODE (body) == SEQUENCE)
1564 HOST_WIDE_INT dest_args_size = cur_args_size;
1565 for (i = 1; i < XVECLEN (body, 0); i++)
1566 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1567 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1568 dest_args_size
1569 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1570 dest_args_size, &next);
1571 else
1572 cur_args_size
1573 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1574 cur_args_size, &next);
1576 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1577 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1578 dest_args_size, &next);
1579 else
1580 cur_args_size
1581 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1582 cur_args_size, &next);
1584 else
1585 cur_args_size
1586 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1590 if (VEC_empty (rtx, next))
1591 break;
1593 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1594 tmp = next;
1595 next = worklist;
1596 worklist = tmp;
1597 VEC_truncate (rtx, next, 0);
1600 VEC_free (rtx, heap, worklist);
1601 VEC_free (rtx, heap, next);
1604 /* Add a CFI to update the running total of the size of arguments
1605 pushed onto the stack. */
1607 static void
1608 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1610 dw_cfi_ref cfi;
1612 if (size == old_args_size)
1613 return;
1615 old_args_size = size;
1617 cfi = new_cfi ();
1618 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1619 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1620 add_fde_cfi (label, cfi);
1623 /* Record a stack adjustment of OFFSET bytes. */
1625 static void
1626 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1628 if (cfa.reg == STACK_POINTER_REGNUM)
1629 cfa.offset += offset;
1631 if (cfa_store.reg == STACK_POINTER_REGNUM)
1632 cfa_store.offset += offset;
1634 if (ACCUMULATE_OUTGOING_ARGS)
1635 return;
1637 #ifndef STACK_GROWS_DOWNWARD
1638 offset = -offset;
1639 #endif
1641 args_size += offset;
1642 if (args_size < 0)
1643 args_size = 0;
1645 def_cfa_1 (label, &cfa);
1646 if (flag_asynchronous_unwind_tables)
1647 dwarf2out_args_size (label, args_size);
1650 /* Check INSN to see if it looks like a push or a stack adjustment, and
1651 make a note of it if it does. EH uses this information to find out
1652 how much extra space it needs to pop off the stack. */
1654 static void
1655 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1657 HOST_WIDE_INT offset;
1658 const char *label;
1659 int i;
1661 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1662 with this function. Proper support would require all frame-related
1663 insns to be marked, and to be able to handle saving state around
1664 epilogues textually in the middle of the function. */
1665 if (prologue_epilogue_contains (insn))
1666 return;
1668 /* If INSN is an instruction from target of an annulled branch, the
1669 effects are for the target only and so current argument size
1670 shouldn't change at all. */
1671 if (final_sequence
1672 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1673 && INSN_FROM_TARGET_P (insn))
1674 return;
1676 /* If only calls can throw, and we have a frame pointer,
1677 save up adjustments until we see the CALL_INSN. */
1678 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1680 if (CALL_P (insn) && !after_p)
1682 /* Extract the size of the args from the CALL rtx itself. */
1683 insn = PATTERN (insn);
1684 if (GET_CODE (insn) == PARALLEL)
1685 insn = XVECEXP (insn, 0, 0);
1686 if (GET_CODE (insn) == SET)
1687 insn = SET_SRC (insn);
1688 gcc_assert (GET_CODE (insn) == CALL);
1689 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1691 return;
1694 if (CALL_P (insn) && !after_p)
1696 if (!flag_asynchronous_unwind_tables)
1697 dwarf2out_args_size ("", args_size);
1698 return;
1700 else if (BARRIER_P (insn))
1702 /* Don't call compute_barrier_args_size () if the only
1703 BARRIER is at the end of function. */
1704 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1705 compute_barrier_args_size ();
1706 if (barrier_args_size == NULL)
1707 offset = 0;
1708 else
1710 offset = barrier_args_size[INSN_UID (insn)];
1711 if (offset < 0)
1712 offset = 0;
1715 offset -= args_size;
1716 #ifndef STACK_GROWS_DOWNWARD
1717 offset = -offset;
1718 #endif
1720 else if (GET_CODE (PATTERN (insn)) == SET)
1721 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1722 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1723 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1725 /* There may be stack adjustments inside compound insns. Search
1726 for them. */
1727 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1728 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1729 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1730 args_size, offset);
1732 else
1733 return;
1735 if (offset == 0)
1736 return;
1738 label = dwarf2out_cfi_label (false);
1739 dwarf2out_stack_adjust (offset, label);
1742 /* We delay emitting a register save until either (a) we reach the end
1743 of the prologue or (b) the register is clobbered. This clusters
1744 register saves so that there are fewer pc advances. */
1746 struct GTY(()) queued_reg_save {
1747 struct queued_reg_save *next;
1748 rtx reg;
1749 HOST_WIDE_INT cfa_offset;
1750 rtx saved_reg;
1753 static GTY(()) struct queued_reg_save *queued_reg_saves;
1755 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1756 struct GTY(()) reg_saved_in_data {
1757 rtx orig_reg;
1758 rtx saved_in_reg;
1761 /* A list of registers saved in other registers.
1762 The list intentionally has a small maximum capacity of 4; if your
1763 port needs more than that, you might consider implementing a
1764 more efficient data structure. */
1765 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1766 static GTY(()) size_t num_regs_saved_in_regs;
1768 static const char *last_reg_save_label;
1770 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1771 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1773 static void
1774 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1776 struct queued_reg_save *q;
1778 /* Duplicates waste space, but it's also necessary to remove them
1779 for correctness, since the queue gets output in reverse
1780 order. */
1781 for (q = queued_reg_saves; q != NULL; q = q->next)
1782 if (REGNO (q->reg) == REGNO (reg))
1783 break;
1785 if (q == NULL)
1787 q = ggc_alloc_queued_reg_save ();
1788 q->next = queued_reg_saves;
1789 queued_reg_saves = q;
1792 q->reg = reg;
1793 q->cfa_offset = offset;
1794 q->saved_reg = sreg;
1796 last_reg_save_label = label;
1799 /* Output all the entries in QUEUED_REG_SAVES. */
1801 void
1802 dwarf2out_flush_queued_reg_saves (void)
1804 struct queued_reg_save *q;
1806 for (q = queued_reg_saves; q; q = q->next)
1808 size_t i;
1809 unsigned int reg, sreg;
1811 for (i = 0; i < num_regs_saved_in_regs; i++)
1812 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1813 break;
1814 if (q->saved_reg && i == num_regs_saved_in_regs)
1816 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1817 num_regs_saved_in_regs++;
1819 if (i != num_regs_saved_in_regs)
1821 regs_saved_in_regs[i].orig_reg = q->reg;
1822 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1825 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1826 if (q->saved_reg)
1827 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1828 else
1829 sreg = INVALID_REGNUM;
1830 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1833 queued_reg_saves = NULL;
1834 last_reg_save_label = NULL;
1837 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1838 location for? Or, does it clobber a register which we've previously
1839 said that some other register is saved in, and for which we now
1840 have a new location for? */
1842 static bool
1843 clobbers_queued_reg_save (const_rtx insn)
1845 struct queued_reg_save *q;
1847 for (q = queued_reg_saves; q; q = q->next)
1849 size_t i;
1850 if (modified_in_p (q->reg, insn))
1851 return true;
1852 for (i = 0; i < num_regs_saved_in_regs; i++)
1853 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1854 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1855 return true;
1858 return false;
1861 /* Entry point for saving the first register into the second. */
1863 void
1864 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1866 size_t i;
1867 unsigned int regno, sregno;
1869 for (i = 0; i < num_regs_saved_in_regs; i++)
1870 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1871 break;
1872 if (i == num_regs_saved_in_regs)
1874 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1875 num_regs_saved_in_regs++;
1877 regs_saved_in_regs[i].orig_reg = reg;
1878 regs_saved_in_regs[i].saved_in_reg = sreg;
1880 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1881 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1882 reg_save (label, regno, sregno, 0);
1885 /* What register, if any, is currently saved in REG? */
1887 static rtx
1888 reg_saved_in (rtx reg)
1890 unsigned int regn = REGNO (reg);
1891 size_t i;
1892 struct queued_reg_save *q;
1894 for (q = queued_reg_saves; q; q = q->next)
1895 if (q->saved_reg && regn == REGNO (q->saved_reg))
1896 return q->reg;
1898 for (i = 0; i < num_regs_saved_in_regs; i++)
1899 if (regs_saved_in_regs[i].saved_in_reg
1900 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1901 return regs_saved_in_regs[i].orig_reg;
1903 return NULL_RTX;
1907 /* A temporary register holding an integral value used in adjusting SP
1908 or setting up the store_reg. The "offset" field holds the integer
1909 value, not an offset. */
1910 static dw_cfa_location cfa_temp;
1912 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1914 static void
1915 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1917 memset (&cfa, 0, sizeof (cfa));
1919 switch (GET_CODE (pat))
1921 case PLUS:
1922 cfa.reg = REGNO (XEXP (pat, 0));
1923 cfa.offset = INTVAL (XEXP (pat, 1));
1924 break;
1926 case REG:
1927 cfa.reg = REGNO (pat);
1928 break;
1930 case MEM:
1931 cfa.indirect = 1;
1932 pat = XEXP (pat, 0);
1933 if (GET_CODE (pat) == PLUS)
1935 cfa.base_offset = INTVAL (XEXP (pat, 1));
1936 pat = XEXP (pat, 0);
1938 cfa.reg = REGNO (pat);
1939 break;
1941 default:
1942 /* Recurse and define an expression. */
1943 gcc_unreachable ();
1946 def_cfa_1 (label, &cfa);
1949 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1951 static void
1952 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1954 rtx src, dest;
1956 gcc_assert (GET_CODE (pat) == SET);
1957 dest = XEXP (pat, 0);
1958 src = XEXP (pat, 1);
1960 switch (GET_CODE (src))
1962 case PLUS:
1963 gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1964 cfa.offset -= INTVAL (XEXP (src, 1));
1965 break;
1967 case REG:
1968 break;
1970 default:
1971 gcc_unreachable ();
1974 cfa.reg = REGNO (dest);
1975 gcc_assert (cfa.indirect == 0);
1977 def_cfa_1 (label, &cfa);
1980 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1982 static void
1983 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1985 HOST_WIDE_INT offset;
1986 rtx src, addr, span;
1988 src = XEXP (set, 1);
1989 addr = XEXP (set, 0);
1990 gcc_assert (MEM_P (addr));
1991 addr = XEXP (addr, 0);
1993 /* As documented, only consider extremely simple addresses. */
1994 switch (GET_CODE (addr))
1996 case REG:
1997 gcc_assert (REGNO (addr) == cfa.reg);
1998 offset = -cfa.offset;
1999 break;
2000 case PLUS:
2001 gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2002 offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2003 break;
2004 default:
2005 gcc_unreachable ();
2008 span = targetm.dwarf_register_span (src);
2010 /* ??? We'd like to use queue_reg_save, but we need to come up with
2011 a different flushing heuristic for epilogues. */
2012 if (!span)
2013 reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2014 else
2016 /* We have a PARALLEL describing where the contents of SRC live.
2017 Queue register saves for each piece of the PARALLEL. */
2018 int par_index;
2019 int limit;
2020 HOST_WIDE_INT span_offset = offset;
2022 gcc_assert (GET_CODE (span) == PARALLEL);
2024 limit = XVECLEN (span, 0);
2025 for (par_index = 0; par_index < limit; par_index++)
2027 rtx elem = XVECEXP (span, 0, par_index);
2029 reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2030 INVALID_REGNUM, span_offset);
2031 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2036 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
2038 static void
2039 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2041 rtx src, dest;
2042 unsigned sregno, dregno;
2044 src = XEXP (set, 1);
2045 dest = XEXP (set, 0);
2047 if (src == pc_rtx)
2048 sregno = DWARF_FRAME_RETURN_COLUMN;
2049 else
2050 sregno = DWARF_FRAME_REGNUM (REGNO (src));
2052 dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2054 /* ??? We'd like to use queue_reg_save, but we need to come up with
2055 a different flushing heuristic for epilogues. */
2056 reg_save (label, sregno, dregno, 0);
2059 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2061 static void
2062 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2064 rtx src, dest, span;
2065 dw_cfi_ref cfi = new_cfi ();
2067 dest = SET_DEST (set);
2068 src = SET_SRC (set);
2070 gcc_assert (REG_P (src));
2071 gcc_assert (MEM_P (dest));
2073 span = targetm.dwarf_register_span (src);
2074 gcc_assert (!span);
2076 cfi->dw_cfi_opc = DW_CFA_expression;
2077 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2078 cfi->dw_cfi_oprnd2.dw_cfi_loc
2079 = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2080 VAR_INIT_STATUS_INITIALIZED);
2082 /* ??? We'd like to use queue_reg_save, were the interface different,
2083 and, as above, we could manage flushing for epilogues. */
2084 add_fde_cfi (label, cfi);
2087 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
2089 static void
2090 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2092 dw_cfi_ref cfi = new_cfi ();
2093 unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2095 cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2096 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2098 add_fde_cfi (label, cfi);
2101 /* Record call frame debugging information for an expression EXPR,
2102 which either sets SP or FP (adjusting how we calculate the frame
2103 address) or saves a register to the stack or another register.
2104 LABEL indicates the address of EXPR.
2106 This function encodes a state machine mapping rtxes to actions on
2107 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
2108 users need not read the source code.
2110 The High-Level Picture
2112 Changes in the register we use to calculate the CFA: Currently we
2113 assume that if you copy the CFA register into another register, we
2114 should take the other one as the new CFA register; this seems to
2115 work pretty well. If it's wrong for some target, it's simple
2116 enough not to set RTX_FRAME_RELATED_P on the insn in question.
2118 Changes in the register we use for saving registers to the stack:
2119 This is usually SP, but not always. Again, we deduce that if you
2120 copy SP into another register (and SP is not the CFA register),
2121 then the new register is the one we will be using for register
2122 saves. This also seems to work.
2124 Register saves: There's not much guesswork about this one; if
2125 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2126 register save, and the register used to calculate the destination
2127 had better be the one we think we're using for this purpose.
2128 It's also assumed that a copy from a call-saved register to another
2129 register is saving that register if RTX_FRAME_RELATED_P is set on
2130 that instruction. If the copy is from a call-saved register to
2131 the *same* register, that means that the register is now the same
2132 value as in the caller.
2134 Except: If the register being saved is the CFA register, and the
2135 offset is nonzero, we are saving the CFA, so we assume we have to
2136 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
2137 the intent is to save the value of SP from the previous frame.
2139 In addition, if a register has previously been saved to a different
2140 register,
2142 Invariants / Summaries of Rules
2144 cfa current rule for calculating the CFA. It usually
2145 consists of a register and an offset.
2146 cfa_store register used by prologue code to save things to the stack
2147 cfa_store.offset is the offset from the value of
2148 cfa_store.reg to the actual CFA
2149 cfa_temp register holding an integral value. cfa_temp.offset
2150 stores the value, which will be used to adjust the
2151 stack pointer. cfa_temp is also used like cfa_store,
2152 to track stores to the stack via fp or a temp reg.
2154 Rules 1- 4: Setting a register's value to cfa.reg or an expression
2155 with cfa.reg as the first operand changes the cfa.reg and its
2156 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
2157 cfa_temp.offset.
2159 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
2160 expression yielding a constant. This sets cfa_temp.reg
2161 and cfa_temp.offset.
2163 Rule 5: Create a new register cfa_store used to save items to the
2164 stack.
2166 Rules 10-14: Save a register to the stack. Define offset as the
2167 difference of the original location and cfa_store's
2168 location (or cfa_temp's location if cfa_temp is used).
2170 Rules 16-20: If AND operation happens on sp in prologue, we assume
2171 stack is realigned. We will use a group of DW_OP_XXX
2172 expressions to represent the location of the stored
2173 register instead of CFA+offset.
2175 The Rules
2177 "{a,b}" indicates a choice of a xor b.
2178 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2180 Rule 1:
2181 (set <reg1> <reg2>:cfa.reg)
2182 effects: cfa.reg = <reg1>
2183 cfa.offset unchanged
2184 cfa_temp.reg = <reg1>
2185 cfa_temp.offset = cfa.offset
2187 Rule 2:
2188 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2189 {<const_int>,<reg>:cfa_temp.reg}))
2190 effects: cfa.reg = sp if fp used
2191 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2192 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2193 if cfa_store.reg==sp
2195 Rule 3:
2196 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2197 effects: cfa.reg = fp
2198 cfa_offset += +/- <const_int>
2200 Rule 4:
2201 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2202 constraints: <reg1> != fp
2203 <reg1> != sp
2204 effects: cfa.reg = <reg1>
2205 cfa_temp.reg = <reg1>
2206 cfa_temp.offset = cfa.offset
2208 Rule 5:
2209 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2210 constraints: <reg1> != fp
2211 <reg1> != sp
2212 effects: cfa_store.reg = <reg1>
2213 cfa_store.offset = cfa.offset - cfa_temp.offset
2215 Rule 6:
2216 (set <reg> <const_int>)
2217 effects: cfa_temp.reg = <reg>
2218 cfa_temp.offset = <const_int>
2220 Rule 7:
2221 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2222 effects: cfa_temp.reg = <reg1>
2223 cfa_temp.offset |= <const_int>
2225 Rule 8:
2226 (set <reg> (high <exp>))
2227 effects: none
2229 Rule 9:
2230 (set <reg> (lo_sum <exp> <const_int>))
2231 effects: cfa_temp.reg = <reg>
2232 cfa_temp.offset = <const_int>
2234 Rule 10:
2235 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2236 effects: cfa_store.offset -= <const_int>
2237 cfa.offset = cfa_store.offset if cfa.reg == sp
2238 cfa.reg = sp
2239 cfa.base_offset = -cfa_store.offset
2241 Rule 11:
2242 (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2243 effects: cfa_store.offset += -/+ mode_size(mem)
2244 cfa.offset = cfa_store.offset if cfa.reg == sp
2245 cfa.reg = sp
2246 cfa.base_offset = -cfa_store.offset
2248 Rule 12:
2249 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2251 <reg2>)
2252 effects: cfa.reg = <reg1>
2253 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2255 Rule 13:
2256 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2257 effects: cfa.reg = <reg1>
2258 cfa.base_offset = -{cfa_store,cfa_temp}.offset
2260 Rule 14:
2261 (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2262 effects: cfa.reg = <reg1>
2263 cfa.base_offset = -cfa_temp.offset
2264 cfa_temp.offset -= mode_size(mem)
2266 Rule 15:
2267 (set <reg> {unspec, unspec_volatile})
2268 effects: target-dependent
2270 Rule 16:
2271 (set sp (and: sp <const_int>))
2272 constraints: cfa_store.reg == sp
2273 effects: current_fde.stack_realign = 1
2274 cfa_store.offset = 0
2275 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2277 Rule 17:
2278 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2279 effects: cfa_store.offset += -/+ mode_size(mem)
2281 Rule 18:
2282 (set (mem ({pre_inc, pre_dec} sp)) fp)
2283 constraints: fde->stack_realign == 1
2284 effects: cfa_store.offset = 0
2285 cfa.reg != HARD_FRAME_POINTER_REGNUM
2287 Rule 19:
2288 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2289 constraints: fde->stack_realign == 1
2290 && cfa.offset == 0
2291 && cfa.indirect == 0
2292 && cfa.reg != HARD_FRAME_POINTER_REGNUM
2293 effects: Use DW_CFA_def_cfa_expression to define cfa
2294 cfa.reg == fde->drap_reg */
2296 static void
2297 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2299 rtx src, dest, span;
2300 HOST_WIDE_INT offset;
2301 dw_fde_ref fde;
2303 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2304 the PARALLEL independently. The first element is always processed if
2305 it is a SET. This is for backward compatibility. Other elements
2306 are processed only if they are SETs and the RTX_FRAME_RELATED_P
2307 flag is set in them. */
2308 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2310 int par_index;
2311 int limit = XVECLEN (expr, 0);
2312 rtx elem;
2314 /* PARALLELs have strict read-modify-write semantics, so we
2315 ought to evaluate every rvalue before changing any lvalue.
2316 It's cumbersome to do that in general, but there's an
2317 easy approximation that is enough for all current users:
2318 handle register saves before register assignments. */
2319 if (GET_CODE (expr) == PARALLEL)
2320 for (par_index = 0; par_index < limit; par_index++)
2322 elem = XVECEXP (expr, 0, par_index);
2323 if (GET_CODE (elem) == SET
2324 && MEM_P (SET_DEST (elem))
2325 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2326 dwarf2out_frame_debug_expr (elem, label);
2329 for (par_index = 0; par_index < limit; par_index++)
2331 elem = XVECEXP (expr, 0, par_index);
2332 if (GET_CODE (elem) == SET
2333 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2334 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2335 dwarf2out_frame_debug_expr (elem, label);
2336 else if (GET_CODE (elem) == SET
2337 && par_index != 0
2338 && !RTX_FRAME_RELATED_P (elem))
2340 /* Stack adjustment combining might combine some post-prologue
2341 stack adjustment into a prologue stack adjustment. */
2342 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2344 if (offset != 0)
2345 dwarf2out_stack_adjust (offset, label);
2348 return;
2351 gcc_assert (GET_CODE (expr) == SET);
2353 src = SET_SRC (expr);
2354 dest = SET_DEST (expr);
2356 if (REG_P (src))
2358 rtx rsi = reg_saved_in (src);
2359 if (rsi)
2360 src = rsi;
2363 fde = current_fde ();
2365 switch (GET_CODE (dest))
2367 case REG:
2368 switch (GET_CODE (src))
2370 /* Setting FP from SP. */
2371 case REG:
2372 if (cfa.reg == (unsigned) REGNO (src))
2374 /* Rule 1 */
2375 /* Update the CFA rule wrt SP or FP. Make sure src is
2376 relative to the current CFA register.
2378 We used to require that dest be either SP or FP, but the
2379 ARM copies SP to a temporary register, and from there to
2380 FP. So we just rely on the backends to only set
2381 RTX_FRAME_RELATED_P on appropriate insns. */
2382 cfa.reg = REGNO (dest);
2383 cfa_temp.reg = cfa.reg;
2384 cfa_temp.offset = cfa.offset;
2386 else
2388 /* Saving a register in a register. */
2389 gcc_assert (!fixed_regs [REGNO (dest)]
2390 /* For the SPARC and its register window. */
2391 || (DWARF_FRAME_REGNUM (REGNO (src))
2392 == DWARF_FRAME_RETURN_COLUMN));
2394 /* After stack is aligned, we can only save SP in FP
2395 if drap register is used. In this case, we have
2396 to restore stack pointer with the CFA value and we
2397 don't generate this DWARF information. */
2398 if (fde
2399 && fde->stack_realign
2400 && REGNO (src) == STACK_POINTER_REGNUM)
2401 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2402 && fde->drap_reg != INVALID_REGNUM
2403 && cfa.reg != REGNO (src));
2404 else
2405 queue_reg_save (label, src, dest, 0);
2407 break;
2409 case PLUS:
2410 case MINUS:
2411 case LO_SUM:
2412 if (dest == stack_pointer_rtx)
2414 /* Rule 2 */
2415 /* Adjusting SP. */
2416 switch (GET_CODE (XEXP (src, 1)))
2418 case CONST_INT:
2419 offset = INTVAL (XEXP (src, 1));
2420 break;
2421 case REG:
2422 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2423 == cfa_temp.reg);
2424 offset = cfa_temp.offset;
2425 break;
2426 default:
2427 gcc_unreachable ();
2430 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2432 /* Restoring SP from FP in the epilogue. */
2433 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2434 cfa.reg = STACK_POINTER_REGNUM;
2436 else if (GET_CODE (src) == LO_SUM)
2437 /* Assume we've set the source reg of the LO_SUM from sp. */
2439 else
2440 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2442 if (GET_CODE (src) != MINUS)
2443 offset = -offset;
2444 if (cfa.reg == STACK_POINTER_REGNUM)
2445 cfa.offset += offset;
2446 if (cfa_store.reg == STACK_POINTER_REGNUM)
2447 cfa_store.offset += offset;
2449 else if (dest == hard_frame_pointer_rtx)
2451 /* Rule 3 */
2452 /* Either setting the FP from an offset of the SP,
2453 or adjusting the FP */
2454 gcc_assert (frame_pointer_needed);
2456 gcc_assert (REG_P (XEXP (src, 0))
2457 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2458 && CONST_INT_P (XEXP (src, 1)));
2459 offset = INTVAL (XEXP (src, 1));
2460 if (GET_CODE (src) != MINUS)
2461 offset = -offset;
2462 cfa.offset += offset;
2463 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2465 else
2467 gcc_assert (GET_CODE (src) != MINUS);
2469 /* Rule 4 */
2470 if (REG_P (XEXP (src, 0))
2471 && REGNO (XEXP (src, 0)) == cfa.reg
2472 && CONST_INT_P (XEXP (src, 1)))
2474 /* Setting a temporary CFA register that will be copied
2475 into the FP later on. */
2476 offset = - INTVAL (XEXP (src, 1));
2477 cfa.offset += offset;
2478 cfa.reg = REGNO (dest);
2479 /* Or used to save regs to the stack. */
2480 cfa_temp.reg = cfa.reg;
2481 cfa_temp.offset = cfa.offset;
2484 /* Rule 5 */
2485 else if (REG_P (XEXP (src, 0))
2486 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2487 && XEXP (src, 1) == stack_pointer_rtx)
2489 /* Setting a scratch register that we will use instead
2490 of SP for saving registers to the stack. */
2491 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2492 cfa_store.reg = REGNO (dest);
2493 cfa_store.offset = cfa.offset - cfa_temp.offset;
2496 /* Rule 9 */
2497 else if (GET_CODE (src) == LO_SUM
2498 && CONST_INT_P (XEXP (src, 1)))
2500 cfa_temp.reg = REGNO (dest);
2501 cfa_temp.offset = INTVAL (XEXP (src, 1));
2503 else
2504 gcc_unreachable ();
2506 break;
2508 /* Rule 6 */
2509 case CONST_INT:
2510 cfa_temp.reg = REGNO (dest);
2511 cfa_temp.offset = INTVAL (src);
2512 break;
2514 /* Rule 7 */
2515 case IOR:
2516 gcc_assert (REG_P (XEXP (src, 0))
2517 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2518 && CONST_INT_P (XEXP (src, 1)));
2520 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2521 cfa_temp.reg = REGNO (dest);
2522 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2523 break;
2525 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2526 which will fill in all of the bits. */
2527 /* Rule 8 */
2528 case HIGH:
2529 break;
2531 /* Rule 15 */
2532 case UNSPEC:
2533 case UNSPEC_VOLATILE:
2534 gcc_assert (targetm.dwarf_handle_frame_unspec);
2535 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2536 return;
2538 /* Rule 16 */
2539 case AND:
2540 /* If this AND operation happens on stack pointer in prologue,
2541 we assume the stack is realigned and we extract the
2542 alignment. */
2543 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2545 /* We interpret reg_save differently with stack_realign set.
2546 Thus we must flush whatever we have queued first. */
2547 dwarf2out_flush_queued_reg_saves ();
2549 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2550 fde->stack_realign = 1;
2551 fde->stack_realignment = INTVAL (XEXP (src, 1));
2552 cfa_store.offset = 0;
2554 if (cfa.reg != STACK_POINTER_REGNUM
2555 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2556 fde->drap_reg = cfa.reg;
2558 return;
2560 default:
2561 gcc_unreachable ();
2564 def_cfa_1 (label, &cfa);
2565 break;
2567 case MEM:
2569 /* Saving a register to the stack. Make sure dest is relative to the
2570 CFA register. */
2571 switch (GET_CODE (XEXP (dest, 0)))
2573 /* Rule 10 */
2574 /* With a push. */
2575 case PRE_MODIFY:
2576 /* We can't handle variable size modifications. */
2577 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2578 == CONST_INT);
2579 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2581 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2582 && cfa_store.reg == STACK_POINTER_REGNUM);
2584 cfa_store.offset += offset;
2585 if (cfa.reg == STACK_POINTER_REGNUM)
2586 cfa.offset = cfa_store.offset;
2588 offset = -cfa_store.offset;
2589 break;
2591 /* Rule 11 */
2592 case PRE_INC:
2593 case PRE_DEC:
2594 case POST_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 if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2620 offset += -cfa_store.offset;
2621 else
2622 offset = -cfa_store.offset;
2623 break;
2625 /* Rule 12 */
2626 /* With an offset. */
2627 case PLUS:
2628 case MINUS:
2629 case LO_SUM:
2631 int regno;
2633 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2634 && REG_P (XEXP (XEXP (dest, 0), 0)));
2635 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2636 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2637 offset = -offset;
2639 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2641 if (cfa.reg == (unsigned) regno)
2642 offset -= cfa.offset;
2643 else if (cfa_store.reg == (unsigned) regno)
2644 offset -= cfa_store.offset;
2645 else
2647 gcc_assert (cfa_temp.reg == (unsigned) regno);
2648 offset -= cfa_temp.offset;
2651 break;
2653 /* Rule 13 */
2654 /* Without an offset. */
2655 case REG:
2657 int regno = REGNO (XEXP (dest, 0));
2659 if (cfa.reg == (unsigned) regno)
2660 offset = -cfa.offset;
2661 else if (cfa_store.reg == (unsigned) regno)
2662 offset = -cfa_store.offset;
2663 else
2665 gcc_assert (cfa_temp.reg == (unsigned) regno);
2666 offset = -cfa_temp.offset;
2669 break;
2671 /* Rule 14 */
2672 case POST_INC:
2673 gcc_assert (cfa_temp.reg
2674 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2675 offset = -cfa_temp.offset;
2676 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2677 break;
2679 default:
2680 gcc_unreachable ();
2683 /* Rule 17 */
2684 /* If the source operand of this MEM operation is not a
2685 register, basically the source is return address. Here
2686 we only care how much stack grew and we don't save it. */
2687 if (!REG_P (src))
2688 break;
2690 if (REGNO (src) != STACK_POINTER_REGNUM
2691 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2692 && (unsigned) REGNO (src) == cfa.reg)
2694 /* We're storing the current CFA reg into the stack. */
2696 if (cfa.offset == 0)
2698 /* Rule 19 */
2699 /* If stack is aligned, putting CFA reg into stack means
2700 we can no longer use reg + offset to represent CFA.
2701 Here we use DW_CFA_def_cfa_expression instead. The
2702 result of this expression equals to the original CFA
2703 value. */
2704 if (fde
2705 && fde->stack_realign
2706 && cfa.indirect == 0
2707 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2709 dw_cfa_location cfa_exp;
2711 gcc_assert (fde->drap_reg == cfa.reg);
2713 cfa_exp.indirect = 1;
2714 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2715 cfa_exp.base_offset = offset;
2716 cfa_exp.offset = 0;
2718 fde->drap_reg_saved = 1;
2720 def_cfa_1 (label, &cfa_exp);
2721 break;
2724 /* If the source register is exactly the CFA, assume
2725 we're saving SP like any other register; this happens
2726 on the ARM. */
2727 def_cfa_1 (label, &cfa);
2728 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2729 break;
2731 else
2733 /* Otherwise, we'll need to look in the stack to
2734 calculate the CFA. */
2735 rtx x = XEXP (dest, 0);
2737 if (!REG_P (x))
2738 x = XEXP (x, 0);
2739 gcc_assert (REG_P (x));
2741 cfa.reg = REGNO (x);
2742 cfa.base_offset = offset;
2743 cfa.indirect = 1;
2744 def_cfa_1 (label, &cfa);
2745 break;
2749 def_cfa_1 (label, &cfa);
2751 span = targetm.dwarf_register_span (src);
2753 if (!span)
2754 queue_reg_save (label, src, NULL_RTX, offset);
2755 else
2757 /* We have a PARALLEL describing where the contents of SRC
2758 live. Queue register saves for each piece of the
2759 PARALLEL. */
2760 int par_index;
2761 int limit;
2762 HOST_WIDE_INT span_offset = offset;
2764 gcc_assert (GET_CODE (span) == PARALLEL);
2766 limit = XVECLEN (span, 0);
2767 for (par_index = 0; par_index < limit; par_index++)
2769 rtx elem = XVECEXP (span, 0, par_index);
2771 queue_reg_save (label, elem, NULL_RTX, span_offset);
2772 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2776 break;
2778 default:
2779 gcc_unreachable ();
2783 /* Record call frame debugging information for INSN, which either
2784 sets SP or FP (adjusting how we calculate the frame address) or saves a
2785 register to the stack. If INSN is NULL_RTX, initialize our state.
2787 If AFTER_P is false, we're being called before the insn is emitted,
2788 otherwise after. Call instructions get invoked twice. */
2790 void
2791 dwarf2out_frame_debug (rtx insn, bool after_p)
2793 const char *label;
2794 rtx note, n;
2795 bool handled_one = false;
2797 if (insn == NULL_RTX)
2799 size_t i;
2801 /* Flush any queued register saves. */
2802 dwarf2out_flush_queued_reg_saves ();
2804 /* Set up state for generating call frame debug info. */
2805 lookup_cfa (&cfa);
2806 gcc_assert (cfa.reg
2807 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2809 cfa.reg = STACK_POINTER_REGNUM;
2810 cfa_store = cfa;
2811 cfa_temp.reg = -1;
2812 cfa_temp.offset = 0;
2814 for (i = 0; i < num_regs_saved_in_regs; i++)
2816 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2817 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2819 num_regs_saved_in_regs = 0;
2821 if (barrier_args_size)
2823 XDELETEVEC (barrier_args_size);
2824 barrier_args_size = NULL;
2826 return;
2829 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2830 dwarf2out_flush_queued_reg_saves ();
2832 if (!RTX_FRAME_RELATED_P (insn))
2834 /* ??? This should be done unconditionally since stack adjustments
2835 matter if the stack pointer is not the CFA register anymore but
2836 is still used to save registers. */
2837 if (!ACCUMULATE_OUTGOING_ARGS)
2838 dwarf2out_notice_stack_adjust (insn, after_p);
2839 return;
2842 label = dwarf2out_cfi_label (false);
2843 any_cfis_emitted = false;
2845 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2846 switch (REG_NOTE_KIND (note))
2848 case REG_FRAME_RELATED_EXPR:
2849 insn = XEXP (note, 0);
2850 goto found;
2852 case REG_CFA_DEF_CFA:
2853 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2854 handled_one = true;
2855 break;
2857 case REG_CFA_ADJUST_CFA:
2858 n = XEXP (note, 0);
2859 if (n == NULL)
2861 n = PATTERN (insn);
2862 if (GET_CODE (n) == PARALLEL)
2863 n = XVECEXP (n, 0, 0);
2865 dwarf2out_frame_debug_adjust_cfa (n, label);
2866 handled_one = true;
2867 break;
2869 case REG_CFA_OFFSET:
2870 n = XEXP (note, 0);
2871 if (n == NULL)
2872 n = single_set (insn);
2873 dwarf2out_frame_debug_cfa_offset (n, label);
2874 handled_one = true;
2875 break;
2877 case REG_CFA_REGISTER:
2878 n = XEXP (note, 0);
2879 if (n == NULL)
2881 n = PATTERN (insn);
2882 if (GET_CODE (n) == PARALLEL)
2883 n = XVECEXP (n, 0, 0);
2885 dwarf2out_frame_debug_cfa_register (n, label);
2886 handled_one = true;
2887 break;
2889 case REG_CFA_EXPRESSION:
2890 n = XEXP (note, 0);
2891 if (n == NULL)
2892 n = single_set (insn);
2893 dwarf2out_frame_debug_cfa_expression (n, label);
2894 handled_one = true;
2895 break;
2897 case REG_CFA_RESTORE:
2898 n = XEXP (note, 0);
2899 if (n == NULL)
2901 n = PATTERN (insn);
2902 if (GET_CODE (n) == PARALLEL)
2903 n = XVECEXP (n, 0, 0);
2904 n = XEXP (n, 0);
2906 dwarf2out_frame_debug_cfa_restore (n, label);
2907 handled_one = true;
2908 break;
2910 case REG_CFA_SET_VDRAP:
2911 n = XEXP (note, 0);
2912 if (REG_P (n))
2914 dw_fde_ref fde = current_fde ();
2915 if (fde)
2917 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2918 if (REG_P (n))
2919 fde->vdrap_reg = REGNO (n);
2922 handled_one = true;
2923 break;
2925 default:
2926 break;
2928 if (handled_one)
2930 if (any_cfis_emitted)
2931 dwarf2out_flush_queued_reg_saves ();
2932 return;
2935 insn = PATTERN (insn);
2936 found:
2937 dwarf2out_frame_debug_expr (insn, label);
2939 /* Check again. A parallel can save and update the same register.
2940 We could probably check just once, here, but this is safer than
2941 removing the check above. */
2942 if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2943 dwarf2out_flush_queued_reg_saves ();
2946 /* Determine if we need to save and restore CFI information around this
2947 epilogue. If SIBCALL is true, then this is a sibcall epilogue. If
2948 we do need to save/restore, then emit the save now, and insert a
2949 NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream. */
2951 void
2952 dwarf2out_cfi_begin_epilogue (rtx insn)
2954 bool saw_frp = false;
2955 rtx i;
2957 /* Scan forward to the return insn, noticing if there are possible
2958 frame related insns. */
2959 for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2961 if (!INSN_P (i))
2962 continue;
2964 /* Look for both regular and sibcalls to end the block. */
2965 if (returnjump_p (i))
2966 break;
2967 if (CALL_P (i) && SIBLING_CALL_P (i))
2968 break;
2970 if (GET_CODE (PATTERN (i)) == SEQUENCE)
2972 int idx;
2973 rtx seq = PATTERN (i);
2975 if (returnjump_p (XVECEXP (seq, 0, 0)))
2976 break;
2977 if (CALL_P (XVECEXP (seq, 0, 0))
2978 && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2979 break;
2981 for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2982 if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2983 saw_frp = true;
2986 if (RTX_FRAME_RELATED_P (i))
2987 saw_frp = true;
2990 /* If the port doesn't emit epilogue unwind info, we don't need a
2991 save/restore pair. */
2992 if (!saw_frp)
2993 return;
2995 /* Otherwise, search forward to see if the return insn was the last
2996 basic block of the function. If so, we don't need save/restore. */
2997 gcc_assert (i != NULL);
2998 i = next_real_insn (i);
2999 if (i == NULL)
3000 return;
3002 /* Insert the restore before that next real insn in the stream, and before
3003 a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3004 properly nested. This should be after any label or alignment. This
3005 will be pushed into the CFI stream by the function below. */
3006 while (1)
3008 rtx p = PREV_INSN (i);
3009 if (!NOTE_P (p))
3010 break;
3011 if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3012 break;
3013 i = p;
3015 emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3017 emit_cfa_remember = true;
3019 /* And emulate the state save. */
3020 gcc_assert (!cfa_remember.in_use);
3021 cfa_remember = cfa;
3022 cfa_remember.in_use = 1;
3025 /* A "subroutine" of dwarf2out_cfi_begin_epilogue. Emit the restore
3026 required. */
3028 void
3029 dwarf2out_frame_debug_restore_state (void)
3031 dw_cfi_ref cfi = new_cfi ();
3032 const char *label = dwarf2out_cfi_label (false);
3034 cfi->dw_cfi_opc = DW_CFA_restore_state;
3035 add_fde_cfi (label, cfi);
3037 gcc_assert (cfa_remember.in_use);
3038 cfa = cfa_remember;
3039 cfa_remember.in_use = 0;
3042 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
3043 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3044 (enum dwarf_call_frame_info cfi);
3046 static enum dw_cfi_oprnd_type
3047 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3049 switch (cfi)
3051 case DW_CFA_nop:
3052 case DW_CFA_GNU_window_save:
3053 case DW_CFA_remember_state:
3054 case DW_CFA_restore_state:
3055 return dw_cfi_oprnd_unused;
3057 case DW_CFA_set_loc:
3058 case DW_CFA_advance_loc1:
3059 case DW_CFA_advance_loc2:
3060 case DW_CFA_advance_loc4:
3061 case DW_CFA_MIPS_advance_loc8:
3062 return dw_cfi_oprnd_addr;
3064 case DW_CFA_offset:
3065 case DW_CFA_offset_extended:
3066 case DW_CFA_def_cfa:
3067 case DW_CFA_offset_extended_sf:
3068 case DW_CFA_def_cfa_sf:
3069 case DW_CFA_restore:
3070 case DW_CFA_restore_extended:
3071 case DW_CFA_undefined:
3072 case DW_CFA_same_value:
3073 case DW_CFA_def_cfa_register:
3074 case DW_CFA_register:
3075 case DW_CFA_expression:
3076 return dw_cfi_oprnd_reg_num;
3078 case DW_CFA_def_cfa_offset:
3079 case DW_CFA_GNU_args_size:
3080 case DW_CFA_def_cfa_offset_sf:
3081 return dw_cfi_oprnd_offset;
3083 case DW_CFA_def_cfa_expression:
3084 return dw_cfi_oprnd_loc;
3086 default:
3087 gcc_unreachable ();
3091 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
3092 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3093 (enum dwarf_call_frame_info cfi);
3095 static enum dw_cfi_oprnd_type
3096 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3098 switch (cfi)
3100 case DW_CFA_def_cfa:
3101 case DW_CFA_def_cfa_sf:
3102 case DW_CFA_offset:
3103 case DW_CFA_offset_extended_sf:
3104 case DW_CFA_offset_extended:
3105 return dw_cfi_oprnd_offset;
3107 case DW_CFA_register:
3108 return dw_cfi_oprnd_reg_num;
3110 case DW_CFA_expression:
3111 return dw_cfi_oprnd_loc;
3113 default:
3114 return dw_cfi_oprnd_unused;
3118 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
3119 switch to the data section instead, and write out a synthetic start label
3120 for collect2 the first time around. */
3122 static void
3123 switch_to_eh_frame_section (bool back)
3125 tree label;
3127 #ifdef EH_FRAME_SECTION_NAME
3128 if (eh_frame_section == 0)
3130 int flags;
3132 if (EH_TABLES_CAN_BE_READ_ONLY)
3134 int fde_encoding;
3135 int per_encoding;
3136 int lsda_encoding;
3138 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3139 /*global=*/0);
3140 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3141 /*global=*/1);
3142 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3143 /*global=*/0);
3144 flags = ((! flag_pic
3145 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3146 && (fde_encoding & 0x70) != DW_EH_PE_aligned
3147 && (per_encoding & 0x70) != DW_EH_PE_absptr
3148 && (per_encoding & 0x70) != DW_EH_PE_aligned
3149 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3150 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3151 ? 0 : SECTION_WRITE);
3153 else
3154 flags = SECTION_WRITE;
3155 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3157 #endif /* EH_FRAME_SECTION_NAME */
3159 if (eh_frame_section)
3160 switch_to_section (eh_frame_section);
3161 else
3163 /* We have no special eh_frame section. Put the information in
3164 the data section and emit special labels to guide collect2. */
3165 switch_to_section (data_section);
3167 if (!back)
3169 label = get_file_function_name ("F");
3170 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3171 targetm.asm_out.globalize_label (asm_out_file,
3172 IDENTIFIER_POINTER (label));
3173 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3178 /* Switch [BACK] to the eh or debug frame table section, depending on
3179 FOR_EH. */
3181 static void
3182 switch_to_frame_table_section (int for_eh, bool back)
3184 if (for_eh)
3185 switch_to_eh_frame_section (back);
3186 else
3188 if (!debug_frame_section)
3189 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3190 SECTION_DEBUG, NULL);
3191 switch_to_section (debug_frame_section);
3195 /* Output a Call Frame Information opcode and its operand(s). */
3197 static void
3198 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3200 unsigned long r;
3201 HOST_WIDE_INT off;
3203 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3204 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3205 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3206 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3207 ((unsigned HOST_WIDE_INT)
3208 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3209 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3211 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3212 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3213 "DW_CFA_offset, column %#lx", r);
3214 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3215 dw2_asm_output_data_uleb128 (off, NULL);
3217 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3219 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3220 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3221 "DW_CFA_restore, column %#lx", r);
3223 else
3225 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3226 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3228 switch (cfi->dw_cfi_opc)
3230 case DW_CFA_set_loc:
3231 if (for_eh)
3232 dw2_asm_output_encoded_addr_rtx (
3233 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3234 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3235 false, NULL);
3236 else
3237 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3238 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3239 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3240 break;
3242 case DW_CFA_advance_loc1:
3243 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3244 fde->dw_fde_current_label, NULL);
3245 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3246 break;
3248 case DW_CFA_advance_loc2:
3249 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3250 fde->dw_fde_current_label, NULL);
3251 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3252 break;
3254 case DW_CFA_advance_loc4:
3255 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3256 fde->dw_fde_current_label, NULL);
3257 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3258 break;
3260 case DW_CFA_MIPS_advance_loc8:
3261 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3262 fde->dw_fde_current_label, NULL);
3263 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3264 break;
3266 case DW_CFA_offset_extended:
3267 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3268 dw2_asm_output_data_uleb128 (r, NULL);
3269 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3270 dw2_asm_output_data_uleb128 (off, NULL);
3271 break;
3273 case DW_CFA_def_cfa:
3274 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3275 dw2_asm_output_data_uleb128 (r, NULL);
3276 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3277 break;
3279 case DW_CFA_offset_extended_sf:
3280 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3281 dw2_asm_output_data_uleb128 (r, NULL);
3282 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3283 dw2_asm_output_data_sleb128 (off, NULL);
3284 break;
3286 case DW_CFA_def_cfa_sf:
3287 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3288 dw2_asm_output_data_uleb128 (r, NULL);
3289 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3290 dw2_asm_output_data_sleb128 (off, NULL);
3291 break;
3293 case DW_CFA_restore_extended:
3294 case DW_CFA_undefined:
3295 case DW_CFA_same_value:
3296 case DW_CFA_def_cfa_register:
3297 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3298 dw2_asm_output_data_uleb128 (r, NULL);
3299 break;
3301 case DW_CFA_register:
3302 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3303 dw2_asm_output_data_uleb128 (r, NULL);
3304 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3305 dw2_asm_output_data_uleb128 (r, NULL);
3306 break;
3308 case DW_CFA_def_cfa_offset:
3309 case DW_CFA_GNU_args_size:
3310 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3311 break;
3313 case DW_CFA_def_cfa_offset_sf:
3314 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3315 dw2_asm_output_data_sleb128 (off, NULL);
3316 break;
3318 case DW_CFA_GNU_window_save:
3319 break;
3321 case DW_CFA_def_cfa_expression:
3322 case DW_CFA_expression:
3323 output_cfa_loc (cfi, for_eh);
3324 break;
3326 case DW_CFA_GNU_negative_offset_extended:
3327 /* Obsoleted by DW_CFA_offset_extended_sf. */
3328 gcc_unreachable ();
3330 default:
3331 break;
3336 /* Similar, but do it via assembler directives instead. */
3338 static void
3339 output_cfi_directive (dw_cfi_ref cfi)
3341 unsigned long r, r2;
3343 switch (cfi->dw_cfi_opc)
3345 case DW_CFA_advance_loc:
3346 case DW_CFA_advance_loc1:
3347 case DW_CFA_advance_loc2:
3348 case DW_CFA_advance_loc4:
3349 case DW_CFA_MIPS_advance_loc8:
3350 case DW_CFA_set_loc:
3351 /* Should only be created by add_fde_cfi in a code path not
3352 followed when emitting via directives. The assembler is
3353 going to take care of this for us. */
3354 gcc_unreachable ();
3356 case DW_CFA_offset:
3357 case DW_CFA_offset_extended:
3358 case DW_CFA_offset_extended_sf:
3359 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3360 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3361 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3362 break;
3364 case DW_CFA_restore:
3365 case DW_CFA_restore_extended:
3366 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3367 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3368 break;
3370 case DW_CFA_undefined:
3371 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3372 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3373 break;
3375 case DW_CFA_same_value:
3376 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3377 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3378 break;
3380 case DW_CFA_def_cfa:
3381 case DW_CFA_def_cfa_sf:
3382 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3383 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3384 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3385 break;
3387 case DW_CFA_def_cfa_register:
3388 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3389 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3390 break;
3392 case DW_CFA_register:
3393 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3394 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3395 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3396 break;
3398 case DW_CFA_def_cfa_offset:
3399 case DW_CFA_def_cfa_offset_sf:
3400 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3401 HOST_WIDE_INT_PRINT_DEC"\n",
3402 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3403 break;
3405 case DW_CFA_remember_state:
3406 fprintf (asm_out_file, "\t.cfi_remember_state\n");
3407 break;
3408 case DW_CFA_restore_state:
3409 fprintf (asm_out_file, "\t.cfi_restore_state\n");
3410 break;
3412 case DW_CFA_GNU_args_size:
3413 fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3414 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3415 if (flag_debug_asm)
3416 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3417 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3418 fputc ('\n', asm_out_file);
3419 break;
3421 case DW_CFA_GNU_window_save:
3422 fprintf (asm_out_file, "\t.cfi_window_save\n");
3423 break;
3425 case DW_CFA_def_cfa_expression:
3426 case DW_CFA_expression:
3427 fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3428 output_cfa_loc_raw (cfi);
3429 fputc ('\n', asm_out_file);
3430 break;
3432 default:
3433 gcc_unreachable ();
3437 DEF_VEC_P (dw_cfi_ref);
3438 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3440 /* Output CFIs to bring current FDE to the same state as after executing
3441 CFIs in CFI chain. DO_CFI_ASM is true if .cfi_* directives shall
3442 be emitted, false otherwise. If it is false, FDE and FOR_EH are the
3443 other arguments to pass to output_cfi. */
3445 static void
3446 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3448 struct dw_cfi_struct cfi_buf;
3449 dw_cfi_ref cfi2;
3450 dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3451 VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3452 unsigned int len, idx;
3454 for (;; cfi = cfi->dw_cfi_next)
3455 switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3457 case DW_CFA_advance_loc:
3458 case DW_CFA_advance_loc1:
3459 case DW_CFA_advance_loc2:
3460 case DW_CFA_advance_loc4:
3461 case DW_CFA_MIPS_advance_loc8:
3462 case DW_CFA_set_loc:
3463 /* All advances should be ignored. */
3464 break;
3465 case DW_CFA_remember_state:
3467 dw_cfi_ref args_size = cfi_args_size;
3469 /* Skip everything between .cfi_remember_state and
3470 .cfi_restore_state. */
3471 for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3472 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3473 break;
3474 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3475 args_size = cfi2;
3476 else
3477 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3479 if (cfi2 == NULL)
3480 goto flush_all;
3481 else
3483 cfi = cfi2;
3484 cfi_args_size = args_size;
3486 break;
3488 case DW_CFA_GNU_args_size:
3489 cfi_args_size = cfi;
3490 break;
3491 case DW_CFA_GNU_window_save:
3492 goto flush_all;
3493 case DW_CFA_offset:
3494 case DW_CFA_offset_extended:
3495 case DW_CFA_offset_extended_sf:
3496 case DW_CFA_restore:
3497 case DW_CFA_restore_extended:
3498 case DW_CFA_undefined:
3499 case DW_CFA_same_value:
3500 case DW_CFA_register:
3501 case DW_CFA_val_offset:
3502 case DW_CFA_val_offset_sf:
3503 case DW_CFA_expression:
3504 case DW_CFA_val_expression:
3505 case DW_CFA_GNU_negative_offset_extended:
3506 if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3507 VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3508 cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3509 VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3510 break;
3511 case DW_CFA_def_cfa:
3512 case DW_CFA_def_cfa_sf:
3513 case DW_CFA_def_cfa_expression:
3514 cfi_cfa = cfi;
3515 cfi_cfa_offset = cfi;
3516 break;
3517 case DW_CFA_def_cfa_register:
3518 cfi_cfa = cfi;
3519 break;
3520 case DW_CFA_def_cfa_offset:
3521 case DW_CFA_def_cfa_offset_sf:
3522 cfi_cfa_offset = cfi;
3523 break;
3524 case DW_CFA_nop:
3525 gcc_assert (cfi == NULL);
3526 flush_all:
3527 len = VEC_length (dw_cfi_ref, regs);
3528 for (idx = 0; idx < len; idx++)
3530 cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3531 if (cfi2 != NULL
3532 && cfi2->dw_cfi_opc != DW_CFA_restore
3533 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3535 if (do_cfi_asm)
3536 output_cfi_directive (cfi2);
3537 else
3538 output_cfi (cfi2, fde, for_eh);
3541 if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3543 gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3544 cfi_buf = *cfi_cfa;
3545 switch (cfi_cfa_offset->dw_cfi_opc)
3547 case DW_CFA_def_cfa_offset:
3548 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3549 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3550 break;
3551 case DW_CFA_def_cfa_offset_sf:
3552 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3553 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3554 break;
3555 case DW_CFA_def_cfa:
3556 case DW_CFA_def_cfa_sf:
3557 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3558 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3559 break;
3560 default:
3561 gcc_unreachable ();
3563 cfi_cfa = &cfi_buf;
3565 else if (cfi_cfa_offset)
3566 cfi_cfa = cfi_cfa_offset;
3567 if (cfi_cfa)
3569 if (do_cfi_asm)
3570 output_cfi_directive (cfi_cfa);
3571 else
3572 output_cfi (cfi_cfa, fde, for_eh);
3574 cfi_cfa = NULL;
3575 cfi_cfa_offset = NULL;
3576 if (cfi_args_size
3577 && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3579 if (do_cfi_asm)
3580 output_cfi_directive (cfi_args_size);
3581 else
3582 output_cfi (cfi_args_size, fde, for_eh);
3584 cfi_args_size = NULL;
3585 if (cfi == NULL)
3587 VEC_free (dw_cfi_ref, heap, regs);
3588 return;
3590 else if (do_cfi_asm)
3591 output_cfi_directive (cfi);
3592 else
3593 output_cfi (cfi, fde, for_eh);
3594 break;
3595 default:
3596 gcc_unreachable ();
3600 /* Output one FDE. */
3602 static void
3603 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3604 char *section_start_label, int fde_encoding, char *augmentation,
3605 bool any_lsda_needed, int lsda_encoding)
3607 const char *begin, *end;
3608 static unsigned int j;
3609 char l1[20], l2[20];
3610 dw_cfi_ref cfi;
3612 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3613 /* empty */ 0);
3614 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3615 for_eh + j);
3616 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3617 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3618 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3619 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3620 " indicating 64-bit DWARF extension");
3621 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3622 "FDE Length");
3623 ASM_OUTPUT_LABEL (asm_out_file, l1);
3625 if (for_eh)
3626 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3627 else
3628 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3629 debug_frame_section, "FDE CIE offset");
3631 if (!fde->dw_fde_switched_sections)
3633 begin = fde->dw_fde_begin;
3634 end = fde->dw_fde_end;
3636 else
3638 /* For the first section, prefer dw_fde_begin over
3639 dw_fde_{hot,cold}_section_label, as the latter
3640 might be separated from the real start of the
3641 function by alignment padding. */
3642 if (!second)
3643 begin = fde->dw_fde_begin;
3644 else if (fde->dw_fde_switched_cold_to_hot)
3645 begin = fde->dw_fde_hot_section_label;
3646 else
3647 begin = fde->dw_fde_unlikely_section_label;
3648 if (second ^ fde->dw_fde_switched_cold_to_hot)
3649 end = fde->dw_fde_unlikely_section_end_label;
3650 else
3651 end = fde->dw_fde_hot_section_end_label;
3654 if (for_eh)
3656 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3657 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3658 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3659 "FDE initial location");
3660 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3661 end, begin, "FDE address range");
3663 else
3665 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3666 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3669 if (augmentation[0])
3671 if (any_lsda_needed)
3673 int size = size_of_encoded_value (lsda_encoding);
3675 if (lsda_encoding == DW_EH_PE_aligned)
3677 int offset = ( 4 /* Length */
3678 + 4 /* CIE offset */
3679 + 2 * size_of_encoded_value (fde_encoding)
3680 + 1 /* Augmentation size */ );
3681 int pad = -offset & (PTR_SIZE - 1);
3683 size += pad;
3684 gcc_assert (size_of_uleb128 (size) == 1);
3687 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3689 if (fde->uses_eh_lsda)
3691 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3692 fde->funcdef_number);
3693 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3694 gen_rtx_SYMBOL_REF (Pmode, l1),
3695 false,
3696 "Language Specific Data Area");
3698 else
3700 if (lsda_encoding == DW_EH_PE_aligned)
3701 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3702 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3703 "Language Specific Data Area (none)");
3706 else
3707 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3710 /* Loop through the Call Frame Instructions associated with
3711 this FDE. */
3712 fde->dw_fde_current_label = begin;
3713 if (!fde->dw_fde_switched_sections)
3714 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3715 output_cfi (cfi, fde, for_eh);
3716 else if (!second)
3718 if (fde->dw_fde_switch_cfi)
3719 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3721 output_cfi (cfi, fde, for_eh);
3722 if (cfi == fde->dw_fde_switch_cfi)
3723 break;
3726 else
3728 dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3730 if (fde->dw_fde_switch_cfi)
3732 cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3733 fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3734 output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3735 fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3737 for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3738 output_cfi (cfi, fde, for_eh);
3741 /* If we are to emit a ref/link from function bodies to their frame tables,
3742 do it now. This is typically performed to make sure that tables
3743 associated with functions are dragged with them and not discarded in
3744 garbage collecting links. We need to do this on a per function basis to
3745 cope with -ffunction-sections. */
3747 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3748 /* Switch to the function section, emit the ref to the tables, and
3749 switch *back* into the table section. */
3750 switch_to_section (function_section (fde->decl));
3751 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3752 switch_to_frame_table_section (for_eh, true);
3753 #endif
3755 /* Pad the FDE out to an address sized boundary. */
3756 ASM_OUTPUT_ALIGN (asm_out_file,
3757 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3758 ASM_OUTPUT_LABEL (asm_out_file, l2);
3760 j += 2;
3763 /* Return true if frame description entry FDE is needed for EH. */
3765 static bool
3766 fde_needed_for_eh_p (dw_fde_ref fde)
3768 if (flag_asynchronous_unwind_tables)
3769 return true;
3771 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3772 return true;
3774 if (fde->uses_eh_lsda)
3775 return true;
3777 /* If exceptions are enabled, we have collected nothrow info. */
3778 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3779 return false;
3781 return true;
3784 /* Output the call frame information used to record information
3785 that relates to calculating the frame pointer, and records the
3786 location of saved registers. */
3788 static void
3789 output_call_frame_info (int for_eh)
3791 unsigned int i;
3792 dw_fde_ref fde;
3793 dw_cfi_ref cfi;
3794 char l1[20], l2[20], section_start_label[20];
3795 bool any_lsda_needed = false;
3796 char augmentation[6];
3797 int augmentation_size;
3798 int fde_encoding = DW_EH_PE_absptr;
3799 int per_encoding = DW_EH_PE_absptr;
3800 int lsda_encoding = DW_EH_PE_absptr;
3801 int return_reg;
3802 rtx personality = NULL;
3803 int dw_cie_version;
3805 /* Don't emit a CIE if there won't be any FDEs. */
3806 if (fde_table_in_use == 0)
3807 return;
3809 /* Nothing to do if the assembler's doing it all. */
3810 if (dwarf2out_do_cfi_asm ())
3811 return;
3813 /* If we don't have any functions we'll want to unwind out of, don't emit
3814 any EH unwind information. If we make FDEs linkonce, we may have to
3815 emit an empty label for an FDE that wouldn't otherwise be emitted. We
3816 want to avoid having an FDE kept around when the function it refers to
3817 is discarded. Example where this matters: a primary function template
3818 in C++ requires EH information, an explicit specialization doesn't. */
3819 if (for_eh)
3821 bool any_eh_needed = false;
3823 for (i = 0; i < fde_table_in_use; i++)
3824 if (fde_table[i].uses_eh_lsda)
3825 any_eh_needed = any_lsda_needed = true;
3826 else if (fde_needed_for_eh_p (&fde_table[i]))
3827 any_eh_needed = true;
3828 else if (TARGET_USES_WEAK_UNWIND_INFO)
3829 targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3830 1, 1);
3832 if (!any_eh_needed)
3833 return;
3836 /* We're going to be generating comments, so turn on app. */
3837 if (flag_debug_asm)
3838 app_enable ();
3840 /* Switch to the proper frame section, first time. */
3841 switch_to_frame_table_section (for_eh, false);
3843 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3844 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3846 /* Output the CIE. */
3847 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3848 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3849 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3850 dw2_asm_output_data (4, 0xffffffff,
3851 "Initial length escape value indicating 64-bit DWARF extension");
3852 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3853 "Length of Common Information Entry");
3854 ASM_OUTPUT_LABEL (asm_out_file, l1);
3856 /* Now that the CIE pointer is PC-relative for EH,
3857 use 0 to identify the CIE. */
3858 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3859 (for_eh ? 0 : DWARF_CIE_ID),
3860 "CIE Identifier Tag");
3862 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3863 use CIE version 1, unless that would produce incorrect results
3864 due to overflowing the return register column. */
3865 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3866 dw_cie_version = 1;
3867 if (return_reg >= 256 || dwarf_version > 2)
3868 dw_cie_version = 3;
3869 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3871 augmentation[0] = 0;
3872 augmentation_size = 0;
3874 personality = current_unit_personality;
3875 if (for_eh)
3877 char *p;
3879 /* Augmentation:
3880 z Indicates that a uleb128 is present to size the
3881 augmentation section.
3882 L Indicates the encoding (and thus presence) of
3883 an LSDA pointer in the FDE augmentation.
3884 R Indicates a non-default pointer encoding for
3885 FDE code pointers.
3886 P Indicates the presence of an encoding + language
3887 personality routine in the CIE augmentation. */
3889 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3890 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3891 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3893 p = augmentation + 1;
3894 if (personality)
3896 *p++ = 'P';
3897 augmentation_size += 1 + size_of_encoded_value (per_encoding);
3898 assemble_external_libcall (personality);
3900 if (any_lsda_needed)
3902 *p++ = 'L';
3903 augmentation_size += 1;
3905 if (fde_encoding != DW_EH_PE_absptr)
3907 *p++ = 'R';
3908 augmentation_size += 1;
3910 if (p > augmentation + 1)
3912 augmentation[0] = 'z';
3913 *p = '\0';
3916 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
3917 if (personality && per_encoding == DW_EH_PE_aligned)
3919 int offset = ( 4 /* Length */
3920 + 4 /* CIE Id */
3921 + 1 /* CIE version */
3922 + strlen (augmentation) + 1 /* Augmentation */
3923 + size_of_uleb128 (1) /* Code alignment */
3924 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3925 + 1 /* RA column */
3926 + 1 /* Augmentation size */
3927 + 1 /* Personality encoding */ );
3928 int pad = -offset & (PTR_SIZE - 1);
3930 augmentation_size += pad;
3932 /* Augmentations should be small, so there's scarce need to
3933 iterate for a solution. Die if we exceed one uleb128 byte. */
3934 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3938 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3939 if (dw_cie_version >= 4)
3941 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3942 dw2_asm_output_data (1, 0, "CIE Segment Size");
3944 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3945 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3946 "CIE Data Alignment Factor");
3948 if (dw_cie_version == 1)
3949 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3950 else
3951 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3953 if (augmentation[0])
3955 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3956 if (personality)
3958 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3959 eh_data_format_name (per_encoding));
3960 dw2_asm_output_encoded_addr_rtx (per_encoding,
3961 personality,
3962 true, NULL);
3965 if (any_lsda_needed)
3966 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3967 eh_data_format_name (lsda_encoding));
3969 if (fde_encoding != DW_EH_PE_absptr)
3970 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3971 eh_data_format_name (fde_encoding));
3974 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3975 output_cfi (cfi, NULL, for_eh);
3977 /* Pad the CIE out to an address sized boundary. */
3978 ASM_OUTPUT_ALIGN (asm_out_file,
3979 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3980 ASM_OUTPUT_LABEL (asm_out_file, l2);
3982 /* Loop through all of the FDE's. */
3983 for (i = 0; i < fde_table_in_use; i++)
3985 unsigned int k;
3986 fde = &fde_table[i];
3988 /* Don't emit EH unwind info for leaf functions that don't need it. */
3989 if (for_eh && !fde_needed_for_eh_p (fde))
3990 continue;
3992 for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3993 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3994 augmentation, any_lsda_needed, lsda_encoding);
3997 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3998 dw2_asm_output_data (4, 0, "End of Table");
3999 #ifdef MIPS_DEBUGGING_INFO
4000 /* Work around Irix 6 assembler bug whereby labels at the end of a section
4001 get a value of 0. Putting .align 0 after the label fixes it. */
4002 ASM_OUTPUT_ALIGN (asm_out_file, 0);
4003 #endif
4005 /* Turn off app to make assembly quicker. */
4006 if (flag_debug_asm)
4007 app_disable ();
4010 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
4012 static void
4013 dwarf2out_do_cfi_startproc (bool second)
4015 int enc;
4016 rtx ref;
4017 rtx personality = get_personality_function (current_function_decl);
4019 fprintf (asm_out_file, "\t.cfi_startproc\n");
4021 if (personality)
4023 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4024 ref = personality;
4026 /* ??? The GAS support isn't entirely consistent. We have to
4027 handle indirect support ourselves, but PC-relative is done
4028 in the assembler. Further, the assembler can't handle any
4029 of the weirder relocation types. */
4030 if (enc & DW_EH_PE_indirect)
4031 ref = dw2_force_const_mem (ref, true);
4033 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4034 output_addr_const (asm_out_file, ref);
4035 fputc ('\n', asm_out_file);
4038 if (crtl->uses_eh_lsda)
4040 char lab[20];
4042 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4043 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4044 current_function_funcdef_no);
4045 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4046 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4048 if (enc & DW_EH_PE_indirect)
4049 ref = dw2_force_const_mem (ref, true);
4051 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4052 output_addr_const (asm_out_file, ref);
4053 fputc ('\n', asm_out_file);
4057 /* Output a marker (i.e. a label) for the beginning of a function, before
4058 the prologue. */
4060 void
4061 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4062 const char *file ATTRIBUTE_UNUSED)
4064 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4065 char * dup_label;
4066 dw_fde_ref fde;
4067 section *fnsec;
4068 bool do_frame;
4070 current_function_func_begin_label = NULL;
4072 do_frame = dwarf2out_do_frame ();
4074 /* ??? current_function_func_begin_label is also used by except.c for
4075 call-site information. We must emit this label if it might be used. */
4076 if (!do_frame
4077 && (!flag_exceptions
4078 || targetm.except_unwind_info (&global_options) != UI_TARGET))
4079 return;
4081 fnsec = function_section (current_function_decl);
4082 switch_to_section (fnsec);
4083 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4084 current_function_funcdef_no);
4085 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4086 current_function_funcdef_no);
4087 dup_label = xstrdup (label);
4088 current_function_func_begin_label = dup_label;
4090 /* We can elide the fde allocation if we're not emitting debug info. */
4091 if (!do_frame)
4092 return;
4094 /* Expand the fde table if necessary. */
4095 if (fde_table_in_use == fde_table_allocated)
4097 fde_table_allocated += FDE_TABLE_INCREMENT;
4098 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4099 memset (fde_table + fde_table_in_use, 0,
4100 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4103 /* Record the FDE associated with this function. */
4104 current_funcdef_fde = fde_table_in_use;
4106 /* Add the new FDE at the end of the fde_table. */
4107 fde = &fde_table[fde_table_in_use++];
4108 fde->decl = current_function_decl;
4109 fde->dw_fde_begin = dup_label;
4110 fde->dw_fde_current_label = dup_label;
4111 fde->dw_fde_hot_section_label = NULL;
4112 fde->dw_fde_hot_section_end_label = NULL;
4113 fde->dw_fde_unlikely_section_label = NULL;
4114 fde->dw_fde_unlikely_section_end_label = NULL;
4115 fde->dw_fde_switched_sections = 0;
4116 fde->dw_fde_switched_cold_to_hot = 0;
4117 fde->dw_fde_end = NULL;
4118 fde->dw_fde_vms_end_prologue = NULL;
4119 fde->dw_fde_vms_begin_epilogue = NULL;
4120 fde->dw_fde_cfi = NULL;
4121 fde->dw_fde_switch_cfi = NULL;
4122 fde->funcdef_number = current_function_funcdef_no;
4123 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4124 fde->uses_eh_lsda = crtl->uses_eh_lsda;
4125 fde->nothrow = crtl->nothrow;
4126 fde->drap_reg = INVALID_REGNUM;
4127 fde->vdrap_reg = INVALID_REGNUM;
4128 if (flag_reorder_blocks_and_partition)
4130 section *unlikelysec;
4131 if (first_function_block_is_cold)
4132 fde->in_std_section = 1;
4133 else
4134 fde->in_std_section
4135 = (fnsec == text_section
4136 || (cold_text_section && fnsec == cold_text_section));
4137 unlikelysec = unlikely_text_section ();
4138 fde->cold_in_std_section
4139 = (unlikelysec == text_section
4140 || (cold_text_section && unlikelysec == cold_text_section));
4142 else
4144 fde->in_std_section
4145 = (fnsec == text_section
4146 || (cold_text_section && fnsec == cold_text_section));
4147 fde->cold_in_std_section = 0;
4150 args_size = old_args_size = 0;
4152 /* We only want to output line number information for the genuine dwarf2
4153 prologue case, not the eh frame case. */
4154 #ifdef DWARF2_DEBUGGING_INFO
4155 if (file)
4156 dwarf2out_source_line (line, file, 0, true);
4157 #endif
4159 if (dwarf2out_do_cfi_asm ())
4160 dwarf2out_do_cfi_startproc (false);
4161 else
4163 rtx personality = get_personality_function (current_function_decl);
4164 if (!current_unit_personality)
4165 current_unit_personality = personality;
4167 /* We cannot keep a current personality per function as without CFI
4168 asm, at the point where we emit the CFI data, there is no current
4169 function anymore. */
4170 if (personality && current_unit_personality != personality)
4171 sorry ("multiple EH personalities are supported only with assemblers "
4172 "supporting .cfi_personality directive");
4176 /* Output a marker (i.e. a label) for the end of the generated code
4177 for a function prologue. This gets called *after* the prologue code has
4178 been generated. */
4180 void
4181 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4182 const char *file ATTRIBUTE_UNUSED)
4184 dw_fde_ref fde;
4185 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4187 /* Output a label to mark the endpoint of the code generated for this
4188 function. */
4189 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4190 current_function_funcdef_no);
4191 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4192 current_function_funcdef_no);
4193 fde = &fde_table[fde_table_in_use - 1];
4194 fde->dw_fde_vms_end_prologue = xstrdup (label);
4197 /* Output a marker (i.e. a label) for the beginning of the generated code
4198 for a function epilogue. This gets called *before* the prologue code has
4199 been generated. */
4201 void
4202 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4203 const char *file ATTRIBUTE_UNUSED)
4205 dw_fde_ref fde;
4206 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4208 fde = &fde_table[fde_table_in_use - 1];
4209 if (fde->dw_fde_vms_begin_epilogue)
4210 return;
4212 /* Output a label to mark the endpoint of the code generated for this
4213 function. */
4214 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4215 current_function_funcdef_no);
4216 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4217 current_function_funcdef_no);
4218 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4221 /* Output a marker (i.e. a label) for the absolute end of the generated code
4222 for a function definition. This gets called *after* the epilogue code has
4223 been generated. */
4225 void
4226 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4227 const char *file ATTRIBUTE_UNUSED)
4229 dw_fde_ref fde;
4230 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4232 last_var_location_insn = NULL_RTX;
4234 if (dwarf2out_do_cfi_asm ())
4235 fprintf (asm_out_file, "\t.cfi_endproc\n");
4237 /* Output a label to mark the endpoint of the code generated for this
4238 function. */
4239 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4240 current_function_funcdef_no);
4241 ASM_OUTPUT_LABEL (asm_out_file, label);
4242 fde = current_fde ();
4243 gcc_assert (fde != NULL);
4244 fde->dw_fde_end = xstrdup (label);
4247 void
4248 dwarf2out_frame_init (void)
4250 /* Allocate the initial hunk of the fde_table. */
4251 fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4252 fde_table_allocated = FDE_TABLE_INCREMENT;
4253 fde_table_in_use = 0;
4255 /* Generate the CFA instructions common to all FDE's. Do it now for the
4256 sake of lookup_cfa. */
4258 /* On entry, the Canonical Frame Address is at SP. */
4259 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4261 if (targetm.debug_unwind_info () == UI_DWARF2
4262 || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4263 initial_return_save (INCOMING_RETURN_ADDR_RTX);
4266 void
4267 dwarf2out_frame_finish (void)
4269 /* Output call frame information. */
4270 if (targetm.debug_unwind_info () == UI_DWARF2)
4271 output_call_frame_info (0);
4273 /* Output another copy for the unwinder. */
4274 if ((flag_unwind_tables || flag_exceptions)
4275 && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4276 output_call_frame_info (1);
4279 /* Note that the current function section is being used for code. */
4281 static void
4282 dwarf2out_note_section_used (void)
4284 section *sec = current_function_section ();
4285 if (sec == text_section)
4286 text_section_used = true;
4287 else if (sec == cold_text_section)
4288 cold_text_section_used = true;
4291 void
4292 dwarf2out_switch_text_section (void)
4294 dw_fde_ref fde = current_fde ();
4296 gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4298 fde->dw_fde_switched_sections = 1;
4299 fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4301 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4302 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4303 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4304 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4305 have_multiple_function_sections = true;
4307 /* Reset the current label on switching text sections, so that we
4308 don't attempt to advance_loc4 between labels in different sections. */
4309 fde->dw_fde_current_label = NULL;
4311 /* There is no need to mark used sections when not debugging. */
4312 if (cold_text_section != NULL)
4313 dwarf2out_note_section_used ();
4315 if (dwarf2out_do_cfi_asm ())
4316 fprintf (asm_out_file, "\t.cfi_endproc\n");
4318 /* Now do the real section switch. */
4319 switch_to_section (current_function_section ());
4321 if (dwarf2out_do_cfi_asm ())
4323 dwarf2out_do_cfi_startproc (true);
4324 /* As this is a different FDE, insert all current CFI instructions
4325 again. */
4326 output_cfis (fde->dw_fde_cfi, true, fde, true);
4328 else
4330 dw_cfi_ref cfi = fde->dw_fde_cfi;
4332 cfi = fde->dw_fde_cfi;
4333 if (cfi)
4334 while (cfi->dw_cfi_next != NULL)
4335 cfi = cfi->dw_cfi_next;
4336 fde->dw_fde_switch_cfi = cfi;
4340 /* And now, the subset of the debugging information support code necessary
4341 for emitting location expressions. */
4343 /* Data about a single source file. */
4344 struct GTY(()) dwarf_file_data {
4345 const char * filename;
4346 int emitted_number;
4349 typedef struct dw_val_struct *dw_val_ref;
4350 typedef struct die_struct *dw_die_ref;
4351 typedef const struct die_struct *const_dw_die_ref;
4352 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4353 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4355 typedef struct GTY(()) deferred_locations_struct
4357 tree variable;
4358 dw_die_ref die;
4359 } deferred_locations;
4361 DEF_VEC_O(deferred_locations);
4362 DEF_VEC_ALLOC_O(deferred_locations,gc);
4364 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4366 DEF_VEC_P(dw_die_ref);
4367 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4369 /* Each DIE may have a series of attribute/value pairs. Values
4370 can take on several forms. The forms that are used in this
4371 implementation are listed below. */
4373 enum dw_val_class
4375 dw_val_class_addr,
4376 dw_val_class_offset,
4377 dw_val_class_loc,
4378 dw_val_class_loc_list,
4379 dw_val_class_range_list,
4380 dw_val_class_const,
4381 dw_val_class_unsigned_const,
4382 dw_val_class_const_double,
4383 dw_val_class_vec,
4384 dw_val_class_flag,
4385 dw_val_class_die_ref,
4386 dw_val_class_fde_ref,
4387 dw_val_class_lbl_id,
4388 dw_val_class_lineptr,
4389 dw_val_class_str,
4390 dw_val_class_macptr,
4391 dw_val_class_file,
4392 dw_val_class_data8,
4393 dw_val_class_decl_ref,
4394 dw_val_class_vms_delta
4397 /* Describe a floating point constant value, or a vector constant value. */
4399 typedef struct GTY(()) dw_vec_struct {
4400 unsigned char * GTY((length ("%h.length"))) array;
4401 unsigned length;
4402 unsigned elt_size;
4404 dw_vec_const;
4406 /* The dw_val_node describes an attribute's value, as it is
4407 represented internally. */
4409 typedef struct GTY(()) dw_val_struct {
4410 enum dw_val_class val_class;
4411 union dw_val_struct_union
4413 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4414 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4415 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4416 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4417 HOST_WIDE_INT GTY ((default)) val_int;
4418 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4419 double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4420 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4421 struct dw_val_die_union
4423 dw_die_ref die;
4424 int external;
4425 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4426 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4427 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4428 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4429 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4430 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4431 unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4432 tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4433 struct dw_val_vms_delta_union
4435 char * lbl1;
4436 char * lbl2;
4437 } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4439 GTY ((desc ("%1.val_class"))) v;
4441 dw_val_node;
4443 /* Locations in memory are described using a sequence of stack machine
4444 operations. */
4446 typedef struct GTY(()) dw_loc_descr_struct {
4447 dw_loc_descr_ref dw_loc_next;
4448 ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4449 /* Used to distinguish DW_OP_addr with a direct symbol relocation
4450 from DW_OP_addr with a dtp-relative symbol relocation. */
4451 unsigned int dtprel : 1;
4452 int dw_loc_addr;
4453 dw_val_node dw_loc_oprnd1;
4454 dw_val_node dw_loc_oprnd2;
4456 dw_loc_descr_node;
4458 /* Location lists are ranges + location descriptions for that range,
4459 so you can track variables that are in different places over
4460 their entire life. */
4461 typedef struct GTY(()) dw_loc_list_struct {
4462 dw_loc_list_ref dw_loc_next;
4463 const char *begin; /* Label for begin address of range */
4464 const char *end; /* Label for end address of range */
4465 char *ll_symbol; /* Label for beginning of location list.
4466 Only on head of list */
4467 const char *section; /* Section this loclist is relative to */
4468 dw_loc_descr_ref expr;
4469 hashval_t hash;
4470 bool emitted;
4471 } dw_loc_list_node;
4473 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4475 /* Convert a DWARF stack opcode into its string name. */
4477 static const char *
4478 dwarf_stack_op_name (unsigned int op)
4480 switch (op)
4482 case DW_OP_addr:
4483 return "DW_OP_addr";
4484 case DW_OP_deref:
4485 return "DW_OP_deref";
4486 case DW_OP_const1u:
4487 return "DW_OP_const1u";
4488 case DW_OP_const1s:
4489 return "DW_OP_const1s";
4490 case DW_OP_const2u:
4491 return "DW_OP_const2u";
4492 case DW_OP_const2s:
4493 return "DW_OP_const2s";
4494 case DW_OP_const4u:
4495 return "DW_OP_const4u";
4496 case DW_OP_const4s:
4497 return "DW_OP_const4s";
4498 case DW_OP_const8u:
4499 return "DW_OP_const8u";
4500 case DW_OP_const8s:
4501 return "DW_OP_const8s";
4502 case DW_OP_constu:
4503 return "DW_OP_constu";
4504 case DW_OP_consts:
4505 return "DW_OP_consts";
4506 case DW_OP_dup:
4507 return "DW_OP_dup";
4508 case DW_OP_drop:
4509 return "DW_OP_drop";
4510 case DW_OP_over:
4511 return "DW_OP_over";
4512 case DW_OP_pick:
4513 return "DW_OP_pick";
4514 case DW_OP_swap:
4515 return "DW_OP_swap";
4516 case DW_OP_rot:
4517 return "DW_OP_rot";
4518 case DW_OP_xderef:
4519 return "DW_OP_xderef";
4520 case DW_OP_abs:
4521 return "DW_OP_abs";
4522 case DW_OP_and:
4523 return "DW_OP_and";
4524 case DW_OP_div:
4525 return "DW_OP_div";
4526 case DW_OP_minus:
4527 return "DW_OP_minus";
4528 case DW_OP_mod:
4529 return "DW_OP_mod";
4530 case DW_OP_mul:
4531 return "DW_OP_mul";
4532 case DW_OP_neg:
4533 return "DW_OP_neg";
4534 case DW_OP_not:
4535 return "DW_OP_not";
4536 case DW_OP_or:
4537 return "DW_OP_or";
4538 case DW_OP_plus:
4539 return "DW_OP_plus";
4540 case DW_OP_plus_uconst:
4541 return "DW_OP_plus_uconst";
4542 case DW_OP_shl:
4543 return "DW_OP_shl";
4544 case DW_OP_shr:
4545 return "DW_OP_shr";
4546 case DW_OP_shra:
4547 return "DW_OP_shra";
4548 case DW_OP_xor:
4549 return "DW_OP_xor";
4550 case DW_OP_bra:
4551 return "DW_OP_bra";
4552 case DW_OP_eq:
4553 return "DW_OP_eq";
4554 case DW_OP_ge:
4555 return "DW_OP_ge";
4556 case DW_OP_gt:
4557 return "DW_OP_gt";
4558 case DW_OP_le:
4559 return "DW_OP_le";
4560 case DW_OP_lt:
4561 return "DW_OP_lt";
4562 case DW_OP_ne:
4563 return "DW_OP_ne";
4564 case DW_OP_skip:
4565 return "DW_OP_skip";
4566 case DW_OP_lit0:
4567 return "DW_OP_lit0";
4568 case DW_OP_lit1:
4569 return "DW_OP_lit1";
4570 case DW_OP_lit2:
4571 return "DW_OP_lit2";
4572 case DW_OP_lit3:
4573 return "DW_OP_lit3";
4574 case DW_OP_lit4:
4575 return "DW_OP_lit4";
4576 case DW_OP_lit5:
4577 return "DW_OP_lit5";
4578 case DW_OP_lit6:
4579 return "DW_OP_lit6";
4580 case DW_OP_lit7:
4581 return "DW_OP_lit7";
4582 case DW_OP_lit8:
4583 return "DW_OP_lit8";
4584 case DW_OP_lit9:
4585 return "DW_OP_lit9";
4586 case DW_OP_lit10:
4587 return "DW_OP_lit10";
4588 case DW_OP_lit11:
4589 return "DW_OP_lit11";
4590 case DW_OP_lit12:
4591 return "DW_OP_lit12";
4592 case DW_OP_lit13:
4593 return "DW_OP_lit13";
4594 case DW_OP_lit14:
4595 return "DW_OP_lit14";
4596 case DW_OP_lit15:
4597 return "DW_OP_lit15";
4598 case DW_OP_lit16:
4599 return "DW_OP_lit16";
4600 case DW_OP_lit17:
4601 return "DW_OP_lit17";
4602 case DW_OP_lit18:
4603 return "DW_OP_lit18";
4604 case DW_OP_lit19:
4605 return "DW_OP_lit19";
4606 case DW_OP_lit20:
4607 return "DW_OP_lit20";
4608 case DW_OP_lit21:
4609 return "DW_OP_lit21";
4610 case DW_OP_lit22:
4611 return "DW_OP_lit22";
4612 case DW_OP_lit23:
4613 return "DW_OP_lit23";
4614 case DW_OP_lit24:
4615 return "DW_OP_lit24";
4616 case DW_OP_lit25:
4617 return "DW_OP_lit25";
4618 case DW_OP_lit26:
4619 return "DW_OP_lit26";
4620 case DW_OP_lit27:
4621 return "DW_OP_lit27";
4622 case DW_OP_lit28:
4623 return "DW_OP_lit28";
4624 case DW_OP_lit29:
4625 return "DW_OP_lit29";
4626 case DW_OP_lit30:
4627 return "DW_OP_lit30";
4628 case DW_OP_lit31:
4629 return "DW_OP_lit31";
4630 case DW_OP_reg0:
4631 return "DW_OP_reg0";
4632 case DW_OP_reg1:
4633 return "DW_OP_reg1";
4634 case DW_OP_reg2:
4635 return "DW_OP_reg2";
4636 case DW_OP_reg3:
4637 return "DW_OP_reg3";
4638 case DW_OP_reg4:
4639 return "DW_OP_reg4";
4640 case DW_OP_reg5:
4641 return "DW_OP_reg5";
4642 case DW_OP_reg6:
4643 return "DW_OP_reg6";
4644 case DW_OP_reg7:
4645 return "DW_OP_reg7";
4646 case DW_OP_reg8:
4647 return "DW_OP_reg8";
4648 case DW_OP_reg9:
4649 return "DW_OP_reg9";
4650 case DW_OP_reg10:
4651 return "DW_OP_reg10";
4652 case DW_OP_reg11:
4653 return "DW_OP_reg11";
4654 case DW_OP_reg12:
4655 return "DW_OP_reg12";
4656 case DW_OP_reg13:
4657 return "DW_OP_reg13";
4658 case DW_OP_reg14:
4659 return "DW_OP_reg14";
4660 case DW_OP_reg15:
4661 return "DW_OP_reg15";
4662 case DW_OP_reg16:
4663 return "DW_OP_reg16";
4664 case DW_OP_reg17:
4665 return "DW_OP_reg17";
4666 case DW_OP_reg18:
4667 return "DW_OP_reg18";
4668 case DW_OP_reg19:
4669 return "DW_OP_reg19";
4670 case DW_OP_reg20:
4671 return "DW_OP_reg20";
4672 case DW_OP_reg21:
4673 return "DW_OP_reg21";
4674 case DW_OP_reg22:
4675 return "DW_OP_reg22";
4676 case DW_OP_reg23:
4677 return "DW_OP_reg23";
4678 case DW_OP_reg24:
4679 return "DW_OP_reg24";
4680 case DW_OP_reg25:
4681 return "DW_OP_reg25";
4682 case DW_OP_reg26:
4683 return "DW_OP_reg26";
4684 case DW_OP_reg27:
4685 return "DW_OP_reg27";
4686 case DW_OP_reg28:
4687 return "DW_OP_reg28";
4688 case DW_OP_reg29:
4689 return "DW_OP_reg29";
4690 case DW_OP_reg30:
4691 return "DW_OP_reg30";
4692 case DW_OP_reg31:
4693 return "DW_OP_reg31";
4694 case DW_OP_breg0:
4695 return "DW_OP_breg0";
4696 case DW_OP_breg1:
4697 return "DW_OP_breg1";
4698 case DW_OP_breg2:
4699 return "DW_OP_breg2";
4700 case DW_OP_breg3:
4701 return "DW_OP_breg3";
4702 case DW_OP_breg4:
4703 return "DW_OP_breg4";
4704 case DW_OP_breg5:
4705 return "DW_OP_breg5";
4706 case DW_OP_breg6:
4707 return "DW_OP_breg6";
4708 case DW_OP_breg7:
4709 return "DW_OP_breg7";
4710 case DW_OP_breg8:
4711 return "DW_OP_breg8";
4712 case DW_OP_breg9:
4713 return "DW_OP_breg9";
4714 case DW_OP_breg10:
4715 return "DW_OP_breg10";
4716 case DW_OP_breg11:
4717 return "DW_OP_breg11";
4718 case DW_OP_breg12:
4719 return "DW_OP_breg12";
4720 case DW_OP_breg13:
4721 return "DW_OP_breg13";
4722 case DW_OP_breg14:
4723 return "DW_OP_breg14";
4724 case DW_OP_breg15:
4725 return "DW_OP_breg15";
4726 case DW_OP_breg16:
4727 return "DW_OP_breg16";
4728 case DW_OP_breg17:
4729 return "DW_OP_breg17";
4730 case DW_OP_breg18:
4731 return "DW_OP_breg18";
4732 case DW_OP_breg19:
4733 return "DW_OP_breg19";
4734 case DW_OP_breg20:
4735 return "DW_OP_breg20";
4736 case DW_OP_breg21:
4737 return "DW_OP_breg21";
4738 case DW_OP_breg22:
4739 return "DW_OP_breg22";
4740 case DW_OP_breg23:
4741 return "DW_OP_breg23";
4742 case DW_OP_breg24:
4743 return "DW_OP_breg24";
4744 case DW_OP_breg25:
4745 return "DW_OP_breg25";
4746 case DW_OP_breg26:
4747 return "DW_OP_breg26";
4748 case DW_OP_breg27:
4749 return "DW_OP_breg27";
4750 case DW_OP_breg28:
4751 return "DW_OP_breg28";
4752 case DW_OP_breg29:
4753 return "DW_OP_breg29";
4754 case DW_OP_breg30:
4755 return "DW_OP_breg30";
4756 case DW_OP_breg31:
4757 return "DW_OP_breg31";
4758 case DW_OP_regx:
4759 return "DW_OP_regx";
4760 case DW_OP_fbreg:
4761 return "DW_OP_fbreg";
4762 case DW_OP_bregx:
4763 return "DW_OP_bregx";
4764 case DW_OP_piece:
4765 return "DW_OP_piece";
4766 case DW_OP_deref_size:
4767 return "DW_OP_deref_size";
4768 case DW_OP_xderef_size:
4769 return "DW_OP_xderef_size";
4770 case DW_OP_nop:
4771 return "DW_OP_nop";
4773 case DW_OP_push_object_address:
4774 return "DW_OP_push_object_address";
4775 case DW_OP_call2:
4776 return "DW_OP_call2";
4777 case DW_OP_call4:
4778 return "DW_OP_call4";
4779 case DW_OP_call_ref:
4780 return "DW_OP_call_ref";
4781 case DW_OP_implicit_value:
4782 return "DW_OP_implicit_value";
4783 case DW_OP_stack_value:
4784 return "DW_OP_stack_value";
4785 case DW_OP_form_tls_address:
4786 return "DW_OP_form_tls_address";
4787 case DW_OP_call_frame_cfa:
4788 return "DW_OP_call_frame_cfa";
4789 case DW_OP_bit_piece:
4790 return "DW_OP_bit_piece";
4792 case DW_OP_GNU_push_tls_address:
4793 return "DW_OP_GNU_push_tls_address";
4794 case DW_OP_GNU_uninit:
4795 return "DW_OP_GNU_uninit";
4796 case DW_OP_GNU_encoded_addr:
4797 return "DW_OP_GNU_encoded_addr";
4798 case DW_OP_GNU_implicit_pointer:
4799 return "DW_OP_GNU_implicit_pointer";
4800 case DW_OP_GNU_entry_value:
4801 return "DW_OP_GNU_entry_value";
4803 default:
4804 return "OP_<unknown>";
4808 /* Return a pointer to a newly allocated location description. Location
4809 descriptions are simple expression terms that can be strung
4810 together to form more complicated location (address) descriptions. */
4812 static inline dw_loc_descr_ref
4813 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4814 unsigned HOST_WIDE_INT oprnd2)
4816 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4818 descr->dw_loc_opc = op;
4819 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4820 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4821 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4822 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4824 return descr;
4827 /* Return a pointer to a newly allocated location description for
4828 REG and OFFSET. */
4830 static inline dw_loc_descr_ref
4831 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
4833 if (reg <= 31)
4834 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4835 offset, 0);
4836 else
4837 return new_loc_descr (DW_OP_bregx, reg, offset);
4840 /* Add a location description term to a location description expression. */
4842 static inline void
4843 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4845 dw_loc_descr_ref *d;
4847 /* Find the end of the chain. */
4848 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4851 *d = descr;
4854 /* Add a constant OFFSET to a location expression. */
4856 static void
4857 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4859 dw_loc_descr_ref loc;
4860 HOST_WIDE_INT *p;
4862 gcc_assert (*list_head != NULL);
4864 if (!offset)
4865 return;
4867 /* Find the end of the chain. */
4868 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4871 p = NULL;
4872 if (loc->dw_loc_opc == DW_OP_fbreg
4873 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4874 p = &loc->dw_loc_oprnd1.v.val_int;
4875 else if (loc->dw_loc_opc == DW_OP_bregx)
4876 p = &loc->dw_loc_oprnd2.v.val_int;
4878 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4879 offset. Don't optimize if an signed integer overflow would happen. */
4880 if (p != NULL
4881 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4882 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4883 *p += offset;
4885 else if (offset > 0)
4886 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4888 else
4890 loc->dw_loc_next = int_loc_descriptor (-offset);
4891 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4895 /* Add a constant OFFSET to a location list. */
4897 static void
4898 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4900 dw_loc_list_ref d;
4901 for (d = list_head; d != NULL; d = d->dw_loc_next)
4902 loc_descr_plus_const (&d->expr, offset);
4905 #define DWARF_REF_SIZE \
4906 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4908 static unsigned long size_of_locs (dw_loc_descr_ref);
4910 /* Return the size of a location descriptor. */
4912 static unsigned long
4913 size_of_loc_descr (dw_loc_descr_ref loc)
4915 unsigned long size = 1;
4917 switch (loc->dw_loc_opc)
4919 case DW_OP_addr:
4920 size += DWARF2_ADDR_SIZE;
4921 break;
4922 case DW_OP_const1u:
4923 case DW_OP_const1s:
4924 size += 1;
4925 break;
4926 case DW_OP_const2u:
4927 case DW_OP_const2s:
4928 size += 2;
4929 break;
4930 case DW_OP_const4u:
4931 case DW_OP_const4s:
4932 size += 4;
4933 break;
4934 case DW_OP_const8u:
4935 case DW_OP_const8s:
4936 size += 8;
4937 break;
4938 case DW_OP_constu:
4939 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4940 break;
4941 case DW_OP_consts:
4942 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4943 break;
4944 case DW_OP_pick:
4945 size += 1;
4946 break;
4947 case DW_OP_plus_uconst:
4948 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4949 break;
4950 case DW_OP_skip:
4951 case DW_OP_bra:
4952 size += 2;
4953 break;
4954 case DW_OP_breg0:
4955 case DW_OP_breg1:
4956 case DW_OP_breg2:
4957 case DW_OP_breg3:
4958 case DW_OP_breg4:
4959 case DW_OP_breg5:
4960 case DW_OP_breg6:
4961 case DW_OP_breg7:
4962 case DW_OP_breg8:
4963 case DW_OP_breg9:
4964 case DW_OP_breg10:
4965 case DW_OP_breg11:
4966 case DW_OP_breg12:
4967 case DW_OP_breg13:
4968 case DW_OP_breg14:
4969 case DW_OP_breg15:
4970 case DW_OP_breg16:
4971 case DW_OP_breg17:
4972 case DW_OP_breg18:
4973 case DW_OP_breg19:
4974 case DW_OP_breg20:
4975 case DW_OP_breg21:
4976 case DW_OP_breg22:
4977 case DW_OP_breg23:
4978 case DW_OP_breg24:
4979 case DW_OP_breg25:
4980 case DW_OP_breg26:
4981 case DW_OP_breg27:
4982 case DW_OP_breg28:
4983 case DW_OP_breg29:
4984 case DW_OP_breg30:
4985 case DW_OP_breg31:
4986 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4987 break;
4988 case DW_OP_regx:
4989 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4990 break;
4991 case DW_OP_fbreg:
4992 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4993 break;
4994 case DW_OP_bregx:
4995 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4996 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4997 break;
4998 case DW_OP_piece:
4999 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5000 break;
5001 case DW_OP_bit_piece:
5002 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5003 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5004 break;
5005 case DW_OP_deref_size:
5006 case DW_OP_xderef_size:
5007 size += 1;
5008 break;
5009 case DW_OP_call2:
5010 size += 2;
5011 break;
5012 case DW_OP_call4:
5013 size += 4;
5014 break;
5015 case DW_OP_call_ref:
5016 size += DWARF_REF_SIZE;
5017 break;
5018 case DW_OP_implicit_value:
5019 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5020 + loc->dw_loc_oprnd1.v.val_unsigned;
5021 break;
5022 case DW_OP_GNU_implicit_pointer:
5023 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5024 break;
5025 case DW_OP_GNU_entry_value:
5027 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5028 size += size_of_uleb128 (op_size) + op_size;
5029 break;
5031 default:
5032 break;
5035 return size;
5038 /* Return the size of a series of location descriptors. */
5040 static unsigned long
5041 size_of_locs (dw_loc_descr_ref loc)
5043 dw_loc_descr_ref l;
5044 unsigned long size;
5046 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5047 field, to avoid writing to a PCH file. */
5048 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5050 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5051 break;
5052 size += size_of_loc_descr (l);
5054 if (! l)
5055 return size;
5057 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5059 l->dw_loc_addr = size;
5060 size += size_of_loc_descr (l);
5063 return size;
5066 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5067 static void get_ref_die_offset_label (char *, dw_die_ref);
5068 static void output_loc_sequence (dw_loc_descr_ref, int);
5070 /* Output location description stack opcode's operands (if any).
5071 The for_eh_or_skip parameter controls whether register numbers are
5072 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5073 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5074 info). This should be suppressed for the cases that have not been converted
5075 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
5077 static void
5078 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5080 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5081 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5083 switch (loc->dw_loc_opc)
5085 #ifdef DWARF2_DEBUGGING_INFO
5086 case DW_OP_const2u:
5087 case DW_OP_const2s:
5088 dw2_asm_output_data (2, val1->v.val_int, NULL);
5089 break;
5090 case DW_OP_const4u:
5091 if (loc->dtprel)
5093 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5094 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5095 val1->v.val_addr);
5096 fputc ('\n', asm_out_file);
5097 break;
5099 /* FALLTHRU */
5100 case DW_OP_const4s:
5101 dw2_asm_output_data (4, val1->v.val_int, NULL);
5102 break;
5103 case DW_OP_const8u:
5104 if (loc->dtprel)
5106 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5107 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5108 val1->v.val_addr);
5109 fputc ('\n', asm_out_file);
5110 break;
5112 /* FALLTHRU */
5113 case DW_OP_const8s:
5114 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5115 dw2_asm_output_data (8, val1->v.val_int, NULL);
5116 break;
5117 case DW_OP_skip:
5118 case DW_OP_bra:
5120 int offset;
5122 gcc_assert (val1->val_class == dw_val_class_loc);
5123 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5125 dw2_asm_output_data (2, offset, NULL);
5127 break;
5128 case DW_OP_implicit_value:
5129 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5130 switch (val2->val_class)
5132 case dw_val_class_const:
5133 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5134 break;
5135 case dw_val_class_vec:
5137 unsigned int elt_size = val2->v.val_vec.elt_size;
5138 unsigned int len = val2->v.val_vec.length;
5139 unsigned int i;
5140 unsigned char *p;
5142 if (elt_size > sizeof (HOST_WIDE_INT))
5144 elt_size /= 2;
5145 len *= 2;
5147 for (i = 0, p = val2->v.val_vec.array;
5148 i < len;
5149 i++, p += elt_size)
5150 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5151 "fp or vector constant word %u", i);
5153 break;
5154 case dw_val_class_const_double:
5156 unsigned HOST_WIDE_INT first, second;
5158 if (WORDS_BIG_ENDIAN)
5160 first = val2->v.val_double.high;
5161 second = val2->v.val_double.low;
5163 else
5165 first = val2->v.val_double.low;
5166 second = val2->v.val_double.high;
5168 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5169 first, NULL);
5170 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5171 second, NULL);
5173 break;
5174 case dw_val_class_addr:
5175 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5176 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5177 break;
5178 default:
5179 gcc_unreachable ();
5181 break;
5182 #else
5183 case DW_OP_const2u:
5184 case DW_OP_const2s:
5185 case DW_OP_const4u:
5186 case DW_OP_const4s:
5187 case DW_OP_const8u:
5188 case DW_OP_const8s:
5189 case DW_OP_skip:
5190 case DW_OP_bra:
5191 case DW_OP_implicit_value:
5192 /* We currently don't make any attempt to make sure these are
5193 aligned properly like we do for the main unwind info, so
5194 don't support emitting things larger than a byte if we're
5195 only doing unwinding. */
5196 gcc_unreachable ();
5197 #endif
5198 case DW_OP_const1u:
5199 case DW_OP_const1s:
5200 dw2_asm_output_data (1, val1->v.val_int, NULL);
5201 break;
5202 case DW_OP_constu:
5203 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5204 break;
5205 case DW_OP_consts:
5206 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5207 break;
5208 case DW_OP_pick:
5209 dw2_asm_output_data (1, val1->v.val_int, NULL);
5210 break;
5211 case DW_OP_plus_uconst:
5212 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5213 break;
5214 case DW_OP_breg0:
5215 case DW_OP_breg1:
5216 case DW_OP_breg2:
5217 case DW_OP_breg3:
5218 case DW_OP_breg4:
5219 case DW_OP_breg5:
5220 case DW_OP_breg6:
5221 case DW_OP_breg7:
5222 case DW_OP_breg8:
5223 case DW_OP_breg9:
5224 case DW_OP_breg10:
5225 case DW_OP_breg11:
5226 case DW_OP_breg12:
5227 case DW_OP_breg13:
5228 case DW_OP_breg14:
5229 case DW_OP_breg15:
5230 case DW_OP_breg16:
5231 case DW_OP_breg17:
5232 case DW_OP_breg18:
5233 case DW_OP_breg19:
5234 case DW_OP_breg20:
5235 case DW_OP_breg21:
5236 case DW_OP_breg22:
5237 case DW_OP_breg23:
5238 case DW_OP_breg24:
5239 case DW_OP_breg25:
5240 case DW_OP_breg26:
5241 case DW_OP_breg27:
5242 case DW_OP_breg28:
5243 case DW_OP_breg29:
5244 case DW_OP_breg30:
5245 case DW_OP_breg31:
5246 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5247 break;
5248 case DW_OP_regx:
5250 unsigned r = val1->v.val_unsigned;
5251 if (for_eh_or_skip >= 0)
5252 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5253 gcc_assert (size_of_uleb128 (r)
5254 == size_of_uleb128 (val1->v.val_unsigned));
5255 dw2_asm_output_data_uleb128 (r, NULL);
5257 break;
5258 case DW_OP_fbreg:
5259 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5260 break;
5261 case DW_OP_bregx:
5263 unsigned r = val1->v.val_unsigned;
5264 if (for_eh_or_skip >= 0)
5265 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5266 gcc_assert (size_of_uleb128 (r)
5267 == size_of_uleb128 (val1->v.val_unsigned));
5268 dw2_asm_output_data_uleb128 (r, NULL);
5269 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5271 break;
5272 case DW_OP_piece:
5273 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5274 break;
5275 case DW_OP_bit_piece:
5276 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5277 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5278 break;
5279 case DW_OP_deref_size:
5280 case DW_OP_xderef_size:
5281 dw2_asm_output_data (1, val1->v.val_int, NULL);
5282 break;
5284 case DW_OP_addr:
5285 if (loc->dtprel)
5287 if (targetm.asm_out.output_dwarf_dtprel)
5289 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5290 DWARF2_ADDR_SIZE,
5291 val1->v.val_addr);
5292 fputc ('\n', asm_out_file);
5294 else
5295 gcc_unreachable ();
5297 else
5299 #ifdef DWARF2_DEBUGGING_INFO
5300 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5301 #else
5302 gcc_unreachable ();
5303 #endif
5305 break;
5307 case DW_OP_GNU_implicit_pointer:
5309 char label[MAX_ARTIFICIAL_LABEL_BYTES
5310 + HOST_BITS_PER_WIDE_INT / 2 + 2];
5311 gcc_assert (val1->val_class == dw_val_class_die_ref);
5312 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5313 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5314 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5316 break;
5318 case DW_OP_GNU_entry_value:
5319 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5320 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5321 break;
5323 default:
5324 /* Other codes have no operands. */
5325 break;
5329 /* Output a sequence of location operations.
5330 The for_eh_or_skip parameter controls whether register numbers are
5331 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5332 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5333 info). This should be suppressed for the cases that have not been converted
5334 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
5336 static void
5337 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5339 for (; loc != NULL; loc = loc->dw_loc_next)
5341 enum dwarf_location_atom opc = loc->dw_loc_opc;
5342 /* Output the opcode. */
5343 if (for_eh_or_skip >= 0
5344 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5346 unsigned r = (opc - DW_OP_breg0);
5347 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5348 gcc_assert (r <= 31);
5349 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5351 else if (for_eh_or_skip >= 0
5352 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5354 unsigned r = (opc - DW_OP_reg0);
5355 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5356 gcc_assert (r <= 31);
5357 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5360 dw2_asm_output_data (1, opc,
5361 "%s", dwarf_stack_op_name (opc));
5363 /* Output the operand(s) (if any). */
5364 output_loc_operands (loc, for_eh_or_skip);
5368 /* Output location description stack opcode's operands (if any).
5369 The output is single bytes on a line, suitable for .cfi_escape. */
5371 static void
5372 output_loc_operands_raw (dw_loc_descr_ref loc)
5374 dw_val_ref val1 = &loc->dw_loc_oprnd1;
5375 dw_val_ref val2 = &loc->dw_loc_oprnd2;
5377 switch (loc->dw_loc_opc)
5379 case DW_OP_addr:
5380 case DW_OP_implicit_value:
5381 /* We cannot output addresses in .cfi_escape, only bytes. */
5382 gcc_unreachable ();
5384 case DW_OP_const1u:
5385 case DW_OP_const1s:
5386 case DW_OP_pick:
5387 case DW_OP_deref_size:
5388 case DW_OP_xderef_size:
5389 fputc (',', asm_out_file);
5390 dw2_asm_output_data_raw (1, val1->v.val_int);
5391 break;
5393 case DW_OP_const2u:
5394 case DW_OP_const2s:
5395 fputc (',', asm_out_file);
5396 dw2_asm_output_data_raw (2, val1->v.val_int);
5397 break;
5399 case DW_OP_const4u:
5400 case DW_OP_const4s:
5401 fputc (',', asm_out_file);
5402 dw2_asm_output_data_raw (4, val1->v.val_int);
5403 break;
5405 case DW_OP_const8u:
5406 case DW_OP_const8s:
5407 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5408 fputc (',', asm_out_file);
5409 dw2_asm_output_data_raw (8, val1->v.val_int);
5410 break;
5412 case DW_OP_skip:
5413 case DW_OP_bra:
5415 int offset;
5417 gcc_assert (val1->val_class == dw_val_class_loc);
5418 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5420 fputc (',', asm_out_file);
5421 dw2_asm_output_data_raw (2, offset);
5423 break;
5425 case DW_OP_regx:
5427 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5428 gcc_assert (size_of_uleb128 (r)
5429 == size_of_uleb128 (val1->v.val_unsigned));
5430 fputc (',', asm_out_file);
5431 dw2_asm_output_data_uleb128_raw (r);
5433 break;
5435 case DW_OP_constu:
5436 case DW_OP_plus_uconst:
5437 case DW_OP_piece:
5438 fputc (',', asm_out_file);
5439 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5440 break;
5442 case DW_OP_bit_piece:
5443 fputc (',', asm_out_file);
5444 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5445 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5446 break;
5448 case DW_OP_consts:
5449 case DW_OP_breg0:
5450 case DW_OP_breg1:
5451 case DW_OP_breg2:
5452 case DW_OP_breg3:
5453 case DW_OP_breg4:
5454 case DW_OP_breg5:
5455 case DW_OP_breg6:
5456 case DW_OP_breg7:
5457 case DW_OP_breg8:
5458 case DW_OP_breg9:
5459 case DW_OP_breg10:
5460 case DW_OP_breg11:
5461 case DW_OP_breg12:
5462 case DW_OP_breg13:
5463 case DW_OP_breg14:
5464 case DW_OP_breg15:
5465 case DW_OP_breg16:
5466 case DW_OP_breg17:
5467 case DW_OP_breg18:
5468 case DW_OP_breg19:
5469 case DW_OP_breg20:
5470 case DW_OP_breg21:
5471 case DW_OP_breg22:
5472 case DW_OP_breg23:
5473 case DW_OP_breg24:
5474 case DW_OP_breg25:
5475 case DW_OP_breg26:
5476 case DW_OP_breg27:
5477 case DW_OP_breg28:
5478 case DW_OP_breg29:
5479 case DW_OP_breg30:
5480 case DW_OP_breg31:
5481 case DW_OP_fbreg:
5482 fputc (',', asm_out_file);
5483 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5484 break;
5486 case DW_OP_bregx:
5488 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5489 gcc_assert (size_of_uleb128 (r)
5490 == size_of_uleb128 (val1->v.val_unsigned));
5491 fputc (',', asm_out_file);
5492 dw2_asm_output_data_uleb128_raw (r);
5493 fputc (',', asm_out_file);
5494 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5496 break;
5498 case DW_OP_GNU_implicit_pointer:
5499 case DW_OP_GNU_entry_value:
5500 gcc_unreachable ();
5501 break;
5503 default:
5504 /* Other codes have no operands. */
5505 break;
5509 static void
5510 output_loc_sequence_raw (dw_loc_descr_ref loc)
5512 while (1)
5514 enum dwarf_location_atom opc = loc->dw_loc_opc;
5515 /* Output the opcode. */
5516 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5518 unsigned r = (opc - DW_OP_breg0);
5519 r = DWARF2_FRAME_REG_OUT (r, 1);
5520 gcc_assert (r <= 31);
5521 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5523 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5525 unsigned r = (opc - DW_OP_reg0);
5526 r = DWARF2_FRAME_REG_OUT (r, 1);
5527 gcc_assert (r <= 31);
5528 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5530 /* Output the opcode. */
5531 fprintf (asm_out_file, "%#x", opc);
5532 output_loc_operands_raw (loc);
5534 if (!loc->dw_loc_next)
5535 break;
5536 loc = loc->dw_loc_next;
5538 fputc (',', asm_out_file);
5542 /* This routine will generate the correct assembly data for a location
5543 description based on a cfi entry with a complex address. */
5545 static void
5546 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5548 dw_loc_descr_ref loc;
5549 unsigned long size;
5551 if (cfi->dw_cfi_opc == DW_CFA_expression)
5553 unsigned r =
5554 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5555 dw2_asm_output_data (1, r, NULL);
5556 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5558 else
5559 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5561 /* Output the size of the block. */
5562 size = size_of_locs (loc);
5563 dw2_asm_output_data_uleb128 (size, NULL);
5565 /* Now output the operations themselves. */
5566 output_loc_sequence (loc, for_eh);
5569 /* Similar, but used for .cfi_escape. */
5571 static void
5572 output_cfa_loc_raw (dw_cfi_ref cfi)
5574 dw_loc_descr_ref loc;
5575 unsigned long size;
5577 if (cfi->dw_cfi_opc == DW_CFA_expression)
5579 unsigned r =
5580 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5581 fprintf (asm_out_file, "%#x,", r);
5582 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5584 else
5585 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5587 /* Output the size of the block. */
5588 size = size_of_locs (loc);
5589 dw2_asm_output_data_uleb128_raw (size);
5590 fputc (',', asm_out_file);
5592 /* Now output the operations themselves. */
5593 output_loc_sequence_raw (loc);
5596 /* This function builds a dwarf location descriptor sequence from a
5597 dw_cfa_location, adding the given OFFSET to the result of the
5598 expression. */
5600 static struct dw_loc_descr_struct *
5601 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5603 struct dw_loc_descr_struct *head, *tmp;
5605 offset += cfa->offset;
5607 if (cfa->indirect)
5609 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5610 head->dw_loc_oprnd1.val_class = dw_val_class_const;
5611 tmp = new_loc_descr (DW_OP_deref, 0, 0);
5612 add_loc_descr (&head, tmp);
5613 if (offset != 0)
5615 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5616 add_loc_descr (&head, tmp);
5619 else
5620 head = new_reg_loc_descr (cfa->reg, offset);
5622 return head;
5625 /* This function builds a dwarf location descriptor sequence for
5626 the address at OFFSET from the CFA when stack is aligned to
5627 ALIGNMENT byte. */
5629 static struct dw_loc_descr_struct *
5630 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5632 struct dw_loc_descr_struct *head;
5633 unsigned int dwarf_fp
5634 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5636 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
5637 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5639 head = new_reg_loc_descr (dwarf_fp, 0);
5640 add_loc_descr (&head, int_loc_descriptor (alignment));
5641 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5642 loc_descr_plus_const (&head, offset);
5644 else
5645 head = new_reg_loc_descr (dwarf_fp, offset);
5646 return head;
5649 /* This function fills in aa dw_cfa_location structure from a dwarf location
5650 descriptor sequence. */
5652 static void
5653 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5655 struct dw_loc_descr_struct *ptr;
5656 cfa->offset = 0;
5657 cfa->base_offset = 0;
5658 cfa->indirect = 0;
5659 cfa->reg = -1;
5661 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5663 enum dwarf_location_atom op = ptr->dw_loc_opc;
5665 switch (op)
5667 case DW_OP_reg0:
5668 case DW_OP_reg1:
5669 case DW_OP_reg2:
5670 case DW_OP_reg3:
5671 case DW_OP_reg4:
5672 case DW_OP_reg5:
5673 case DW_OP_reg6:
5674 case DW_OP_reg7:
5675 case DW_OP_reg8:
5676 case DW_OP_reg9:
5677 case DW_OP_reg10:
5678 case DW_OP_reg11:
5679 case DW_OP_reg12:
5680 case DW_OP_reg13:
5681 case DW_OP_reg14:
5682 case DW_OP_reg15:
5683 case DW_OP_reg16:
5684 case DW_OP_reg17:
5685 case DW_OP_reg18:
5686 case DW_OP_reg19:
5687 case DW_OP_reg20:
5688 case DW_OP_reg21:
5689 case DW_OP_reg22:
5690 case DW_OP_reg23:
5691 case DW_OP_reg24:
5692 case DW_OP_reg25:
5693 case DW_OP_reg26:
5694 case DW_OP_reg27:
5695 case DW_OP_reg28:
5696 case DW_OP_reg29:
5697 case DW_OP_reg30:
5698 case DW_OP_reg31:
5699 cfa->reg = op - DW_OP_reg0;
5700 break;
5701 case DW_OP_regx:
5702 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5703 break;
5704 case DW_OP_breg0:
5705 case DW_OP_breg1:
5706 case DW_OP_breg2:
5707 case DW_OP_breg3:
5708 case DW_OP_breg4:
5709 case DW_OP_breg5:
5710 case DW_OP_breg6:
5711 case DW_OP_breg7:
5712 case DW_OP_breg8:
5713 case DW_OP_breg9:
5714 case DW_OP_breg10:
5715 case DW_OP_breg11:
5716 case DW_OP_breg12:
5717 case DW_OP_breg13:
5718 case DW_OP_breg14:
5719 case DW_OP_breg15:
5720 case DW_OP_breg16:
5721 case DW_OP_breg17:
5722 case DW_OP_breg18:
5723 case DW_OP_breg19:
5724 case DW_OP_breg20:
5725 case DW_OP_breg21:
5726 case DW_OP_breg22:
5727 case DW_OP_breg23:
5728 case DW_OP_breg24:
5729 case DW_OP_breg25:
5730 case DW_OP_breg26:
5731 case DW_OP_breg27:
5732 case DW_OP_breg28:
5733 case DW_OP_breg29:
5734 case DW_OP_breg30:
5735 case DW_OP_breg31:
5736 cfa->reg = op - DW_OP_breg0;
5737 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5738 break;
5739 case DW_OP_bregx:
5740 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5741 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5742 break;
5743 case DW_OP_deref:
5744 cfa->indirect = 1;
5745 break;
5746 case DW_OP_plus_uconst:
5747 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5748 break;
5749 default:
5750 internal_error ("DW_LOC_OP %s not implemented",
5751 dwarf_stack_op_name (ptr->dw_loc_opc));
5756 /* And now, the support for symbolic debugging information. */
5758 /* .debug_str support. */
5759 static int output_indirect_string (void **, void *);
5761 static void dwarf2out_init (const char *);
5762 static void dwarf2out_finish (const char *);
5763 static void dwarf2out_assembly_start (void);
5764 static void dwarf2out_define (unsigned int, const char *);
5765 static void dwarf2out_undef (unsigned int, const char *);
5766 static void dwarf2out_start_source_file (unsigned, const char *);
5767 static void dwarf2out_end_source_file (unsigned);
5768 static void dwarf2out_function_decl (tree);
5769 static void dwarf2out_begin_block (unsigned, unsigned);
5770 static void dwarf2out_end_block (unsigned, unsigned);
5771 static bool dwarf2out_ignore_block (const_tree);
5772 static void dwarf2out_global_decl (tree);
5773 static void dwarf2out_type_decl (tree, int);
5774 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5775 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5776 dw_die_ref);
5777 static void dwarf2out_abstract_function (tree);
5778 static void dwarf2out_var_location (rtx);
5779 static void dwarf2out_begin_function (tree);
5780 static void dwarf2out_set_name (tree, tree);
5782 /* The debug hooks structure. */
5784 const struct gcc_debug_hooks dwarf2_debug_hooks =
5786 dwarf2out_init,
5787 dwarf2out_finish,
5788 dwarf2out_assembly_start,
5789 dwarf2out_define,
5790 dwarf2out_undef,
5791 dwarf2out_start_source_file,
5792 dwarf2out_end_source_file,
5793 dwarf2out_begin_block,
5794 dwarf2out_end_block,
5795 dwarf2out_ignore_block,
5796 dwarf2out_source_line,
5797 dwarf2out_begin_prologue,
5798 #if VMS_DEBUGGING_INFO
5799 dwarf2out_vms_end_prologue,
5800 dwarf2out_vms_begin_epilogue,
5801 #else
5802 debug_nothing_int_charstar,
5803 debug_nothing_int_charstar,
5804 #endif
5805 dwarf2out_end_epilogue,
5806 dwarf2out_begin_function,
5807 debug_nothing_int, /* end_function */
5808 dwarf2out_function_decl, /* function_decl */
5809 dwarf2out_global_decl,
5810 dwarf2out_type_decl, /* type_decl */
5811 dwarf2out_imported_module_or_decl,
5812 debug_nothing_tree, /* deferred_inline_function */
5813 /* The DWARF 2 backend tries to reduce debugging bloat by not
5814 emitting the abstract description of inline functions until
5815 something tries to reference them. */
5816 dwarf2out_abstract_function, /* outlining_inline_function */
5817 debug_nothing_rtx, /* label */
5818 debug_nothing_int, /* handle_pch */
5819 dwarf2out_var_location,
5820 dwarf2out_switch_text_section,
5821 dwarf2out_set_name,
5822 1, /* start_end_main_source_file */
5823 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
5826 /* NOTE: In the comments in this file, many references are made to
5827 "Debugging Information Entries". This term is abbreviated as `DIE'
5828 throughout the remainder of this file. */
5830 /* An internal representation of the DWARF output is built, and then
5831 walked to generate the DWARF debugging info. The walk of the internal
5832 representation is done after the entire program has been compiled.
5833 The types below are used to describe the internal representation. */
5835 /* Various DIE's use offsets relative to the beginning of the
5836 .debug_info section to refer to each other. */
5838 typedef long int dw_offset;
5840 /* Define typedefs here to avoid circular dependencies. */
5842 typedef struct dw_attr_struct *dw_attr_ref;
5843 typedef struct dw_line_info_struct *dw_line_info_ref;
5844 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5845 typedef struct pubname_struct *pubname_ref;
5846 typedef struct dw_ranges_struct *dw_ranges_ref;
5847 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5848 typedef struct comdat_type_struct *comdat_type_node_ref;
5850 /* Each entry in the line_info_table maintains the file and
5851 line number associated with the label generated for that
5852 entry. The label gives the PC value associated with
5853 the line number entry. */
5855 typedef struct GTY(()) dw_line_info_struct {
5856 unsigned long dw_file_num;
5857 unsigned long dw_line_num;
5859 dw_line_info_entry;
5861 /* Line information for functions in separate sections; each one gets its
5862 own sequence. */
5863 typedef struct GTY(()) dw_separate_line_info_struct {
5864 unsigned long dw_file_num;
5865 unsigned long dw_line_num;
5866 unsigned long function;
5868 dw_separate_line_info_entry;
5870 /* Each DIE attribute has a field specifying the attribute kind,
5871 a link to the next attribute in the chain, and an attribute value.
5872 Attributes are typically linked below the DIE they modify. */
5874 typedef struct GTY(()) dw_attr_struct {
5875 enum dwarf_attribute dw_attr;
5876 dw_val_node dw_attr_val;
5878 dw_attr_node;
5880 DEF_VEC_O(dw_attr_node);
5881 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5883 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
5884 The children of each node form a circular list linked by
5885 die_sib. die_child points to the node *before* the "first" child node. */
5887 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5888 union die_symbol_or_type_node
5890 char * GTY ((tag ("0"))) die_symbol;
5891 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5893 GTY ((desc ("dwarf_version >= 4"))) die_id;
5894 VEC(dw_attr_node,gc) * die_attr;
5895 dw_die_ref die_parent;
5896 dw_die_ref die_child;
5897 dw_die_ref die_sib;
5898 dw_die_ref die_definition; /* ref from a specification to its definition */
5899 dw_offset die_offset;
5900 unsigned long die_abbrev;
5901 int die_mark;
5902 /* Die is used and must not be pruned as unused. */
5903 int die_perennial_p;
5904 unsigned int decl_id;
5905 enum dwarf_tag die_tag;
5907 die_node;
5909 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
5910 #define FOR_EACH_CHILD(die, c, expr) do { \
5911 c = die->die_child; \
5912 if (c) do { \
5913 c = c->die_sib; \
5914 expr; \
5915 } while (c != die->die_child); \
5916 } while (0)
5918 /* The pubname structure */
5920 typedef struct GTY(()) pubname_struct {
5921 dw_die_ref die;
5922 const char *name;
5924 pubname_entry;
5926 DEF_VEC_O(pubname_entry);
5927 DEF_VEC_ALLOC_O(pubname_entry, gc);
5929 struct GTY(()) dw_ranges_struct {
5930 /* If this is positive, it's a block number, otherwise it's a
5931 bitwise-negated index into dw_ranges_by_label. */
5932 int num;
5935 /* A structure to hold a macinfo entry. */
5937 typedef struct GTY(()) macinfo_struct {
5938 unsigned HOST_WIDE_INT code;
5939 unsigned HOST_WIDE_INT lineno;
5940 const char *info;
5942 macinfo_entry;
5944 DEF_VEC_O(macinfo_entry);
5945 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5947 struct GTY(()) dw_ranges_by_label_struct {
5948 const char *begin;
5949 const char *end;
5952 /* The comdat type node structure. */
5953 typedef struct GTY(()) comdat_type_struct
5955 dw_die_ref root_die;
5956 dw_die_ref type_die;
5957 char signature[DWARF_TYPE_SIGNATURE_SIZE];
5958 struct comdat_type_struct *next;
5960 comdat_type_node;
5962 /* The limbo die list structure. */
5963 typedef struct GTY(()) limbo_die_struct {
5964 dw_die_ref die;
5965 tree created_for;
5966 struct limbo_die_struct *next;
5968 limbo_die_node;
5970 typedef struct skeleton_chain_struct
5972 dw_die_ref old_die;
5973 dw_die_ref new_die;
5974 struct skeleton_chain_struct *parent;
5976 skeleton_chain_node;
5978 /* How to start an assembler comment. */
5979 #ifndef ASM_COMMENT_START
5980 #define ASM_COMMENT_START ";#"
5981 #endif
5983 /* Define a macro which returns nonzero for a TYPE_DECL which was
5984 implicitly generated for a tagged type.
5986 Note that unlike the gcc front end (which generates a NULL named
5987 TYPE_DECL node for each complete tagged type, each array type, and
5988 each function type node created) the g++ front end generates a
5989 _named_ TYPE_DECL node for each tagged type node created.
5990 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5991 generate a DW_TAG_typedef DIE for them. */
5993 #define TYPE_DECL_IS_STUB(decl) \
5994 (DECL_NAME (decl) == NULL_TREE \
5995 || (DECL_ARTIFICIAL (decl) \
5996 && is_tagged_type (TREE_TYPE (decl)) \
5997 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
5998 /* This is necessary for stub decls that \
5999 appear in nested inline functions. */ \
6000 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6001 && (decl_ultimate_origin (decl) \
6002 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6004 /* Information concerning the compilation unit's programming
6005 language, and compiler version. */
6007 /* Fixed size portion of the DWARF compilation unit header. */
6008 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6009 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6011 /* Fixed size portion of the DWARF comdat type unit header. */
6012 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6013 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6014 + DWARF_OFFSET_SIZE)
6016 /* Fixed size portion of public names info. */
6017 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6019 /* Fixed size portion of the address range info. */
6020 #define DWARF_ARANGES_HEADER_SIZE \
6021 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6022 DWARF2_ADDR_SIZE * 2) \
6023 - DWARF_INITIAL_LENGTH_SIZE)
6025 /* Size of padding portion in the address range info. It must be
6026 aligned to twice the pointer size. */
6027 #define DWARF_ARANGES_PAD_SIZE \
6028 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6029 DWARF2_ADDR_SIZE * 2) \
6030 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6032 /* Use assembler line directives if available. */
6033 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6034 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6035 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6036 #else
6037 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6038 #endif
6039 #endif
6041 /* Minimum line offset in a special line info. opcode.
6042 This value was chosen to give a reasonable range of values. */
6043 #define DWARF_LINE_BASE -10
6045 /* First special line opcode - leave room for the standard opcodes. */
6046 #define DWARF_LINE_OPCODE_BASE 10
6048 /* Range of line offsets in a special line info. opcode. */
6049 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
6051 /* Flag that indicates the initial value of the is_stmt_start flag.
6052 In the present implementation, we do not mark any lines as
6053 the beginning of a source statement, because that information
6054 is not made available by the GCC front-end. */
6055 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6057 /* Maximum number of operations per instruction bundle. */
6058 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6059 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6060 #endif
6062 /* This location is used by calc_die_sizes() to keep track
6063 the offset of each DIE within the .debug_info section. */
6064 static unsigned long next_die_offset;
6066 /* Record the root of the DIE's built for the current compilation unit. */
6067 static GTY(()) dw_die_ref single_comp_unit_die;
6069 /* A list of type DIEs that have been separated into comdat sections. */
6070 static GTY(()) comdat_type_node *comdat_type_list;
6072 /* A list of DIEs with a NULL parent waiting to be relocated. */
6073 static GTY(()) limbo_die_node *limbo_die_list;
6075 /* A list of DIEs for which we may have to generate
6076 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
6077 static GTY(()) limbo_die_node *deferred_asm_name;
6079 /* Filenames referenced by this compilation unit. */
6080 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6082 /* A hash table of references to DIE's that describe declarations.
6083 The key is a DECL_UID() which is a unique number identifying each decl. */
6084 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6086 /* A hash table of references to DIE's that describe COMMON blocks.
6087 The key is DECL_UID() ^ die_parent. */
6088 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6090 typedef struct GTY(()) die_arg_entry_struct {
6091 dw_die_ref die;
6092 tree arg;
6093 } die_arg_entry;
6095 DEF_VEC_O(die_arg_entry);
6096 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6098 /* Node of the variable location list. */
6099 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6100 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6101 EXPR_LIST chain. For small bitsizes, bitsize is encoded
6102 in mode of the EXPR_LIST node and first EXPR_LIST operand
6103 is either NOTE_INSN_VAR_LOCATION for a piece with a known
6104 location or NULL for padding. For larger bitsizes,
6105 mode is 0 and first operand is a CONCAT with bitsize
6106 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6107 NULL as second operand. */
6108 rtx GTY (()) loc;
6109 const char * GTY (()) label;
6110 struct var_loc_node * GTY (()) next;
6113 /* Variable location list. */
6114 struct GTY (()) var_loc_list_def {
6115 struct var_loc_node * GTY (()) first;
6117 /* Pointer to the last but one or last element of the
6118 chained list. If the list is empty, both first and
6119 last are NULL, if the list contains just one node
6120 or the last node certainly is not redundant, it points
6121 to the last node, otherwise points to the last but one.
6122 Do not mark it for GC because it is marked through the chain. */
6123 struct var_loc_node * GTY ((skip ("%h"))) last;
6125 /* DECL_UID of the variable decl. */
6126 unsigned int decl_id;
6128 typedef struct var_loc_list_def var_loc_list;
6130 /* Call argument location list. */
6131 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6132 rtx GTY (()) call_arg_loc_note;
6133 const char * GTY (()) label;
6134 tree GTY (()) block;
6135 bool tail_call_p;
6136 rtx GTY (()) symbol_ref;
6137 struct call_arg_loc_node * GTY (()) next;
6141 /* Table of decl location linked lists. */
6142 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6144 /* Head and tail of call_arg_loc chain. */
6145 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6146 static struct call_arg_loc_node *call_arg_loc_last;
6148 /* Number of call sites in the current function. */
6149 static int call_site_count = -1;
6150 /* Number of tail call sites in the current function. */
6151 static int tail_call_site_count = -1;
6153 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6154 DIEs. */
6155 static VEC (dw_die_ref, heap) *block_map;
6157 /* A pointer to the base of a list of references to DIE's that
6158 are uniquely identified by their tag, presence/absence of
6159 children DIE's, and list of attribute/value pairs. */
6160 static GTY((length ("abbrev_die_table_allocated")))
6161 dw_die_ref *abbrev_die_table;
6163 /* Number of elements currently allocated for abbrev_die_table. */
6164 static GTY(()) unsigned abbrev_die_table_allocated;
6166 /* Number of elements in type_die_table currently in use. */
6167 static GTY(()) unsigned abbrev_die_table_in_use;
6169 /* Size (in elements) of increments by which we may expand the
6170 abbrev_die_table. */
6171 #define ABBREV_DIE_TABLE_INCREMENT 256
6173 /* A pointer to the base of a table that contains line information
6174 for each source code line in .text in the compilation unit. */
6175 static GTY((length ("line_info_table_allocated")))
6176 dw_line_info_ref line_info_table;
6178 /* Number of elements currently allocated for line_info_table. */
6179 static GTY(()) unsigned line_info_table_allocated;
6181 /* Number of elements in line_info_table currently in use. */
6182 static GTY(()) unsigned line_info_table_in_use;
6184 /* A pointer to the base of a table that contains line information
6185 for each source code line outside of .text in the compilation unit. */
6186 static GTY ((length ("separate_line_info_table_allocated")))
6187 dw_separate_line_info_ref separate_line_info_table;
6189 /* Number of elements currently allocated for separate_line_info_table. */
6190 static GTY(()) unsigned separate_line_info_table_allocated;
6192 /* Number of elements in separate_line_info_table currently in use. */
6193 static GTY(()) unsigned separate_line_info_table_in_use;
6195 /* Size (in elements) of increments by which we may expand the
6196 line_info_table. */
6197 #define LINE_INFO_TABLE_INCREMENT 1024
6199 /* A flag to tell pubnames/types export if there is an info section to
6200 refer to. */
6201 static bool info_section_emitted;
6203 /* A pointer to the base of a table that contains a list of publicly
6204 accessible names. */
6205 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
6207 /* A pointer to the base of a table that contains a list of publicly
6208 accessible types. */
6209 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6211 /* A pointer to the base of a table that contains a list of macro
6212 defines/undefines (and file start/end markers). */
6213 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6215 /* Array of dies for which we should generate .debug_arange info. */
6216 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6218 /* Number of elements currently allocated for arange_table. */
6219 static GTY(()) unsigned arange_table_allocated;
6221 /* Number of elements in arange_table currently in use. */
6222 static GTY(()) unsigned arange_table_in_use;
6224 /* Size (in elements) of increments by which we may expand the
6225 arange_table. */
6226 #define ARANGE_TABLE_INCREMENT 64
6228 /* Array of dies for which we should generate .debug_ranges info. */
6229 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6231 /* Number of elements currently allocated for ranges_table. */
6232 static GTY(()) unsigned ranges_table_allocated;
6234 /* Number of elements in ranges_table currently in use. */
6235 static GTY(()) unsigned ranges_table_in_use;
6237 /* Array of pairs of labels referenced in ranges_table. */
6238 static GTY ((length ("ranges_by_label_allocated")))
6239 dw_ranges_by_label_ref ranges_by_label;
6241 /* Number of elements currently allocated for ranges_by_label. */
6242 static GTY(()) unsigned ranges_by_label_allocated;
6244 /* Number of elements in ranges_by_label currently in use. */
6245 static GTY(()) unsigned ranges_by_label_in_use;
6247 /* Size (in elements) of increments by which we may expand the
6248 ranges_table. */
6249 #define RANGES_TABLE_INCREMENT 64
6251 /* Whether we have location lists that need outputting */
6252 static GTY(()) bool have_location_lists;
6254 /* Unique label counter. */
6255 static GTY(()) unsigned int loclabel_num;
6257 /* Unique label counter for point-of-call tables. */
6258 static GTY(()) unsigned int poc_label_num;
6260 /* Record whether the function being analyzed contains inlined functions. */
6261 static int current_function_has_inlines;
6263 /* The last file entry emitted by maybe_emit_file(). */
6264 static GTY(()) struct dwarf_file_data * last_emitted_file;
6266 /* Number of internal labels generated by gen_internal_sym(). */
6267 static GTY(()) int label_num;
6269 /* Cached result of previous call to lookup_filename. */
6270 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6272 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6274 /* Instances of generic types for which we need to generate debug
6275 info that describe their generic parameters and arguments. That
6276 generation needs to happen once all types are properly laid out so
6277 we do it at the end of compilation. */
6278 static GTY(()) VEC(tree,gc) *generic_type_instances;
6280 /* Offset from the "steady-state frame pointer" to the frame base,
6281 within the current function. */
6282 static HOST_WIDE_INT frame_pointer_fb_offset;
6284 /* Forward declarations for functions defined in this file. */
6286 static int is_pseudo_reg (const_rtx);
6287 static tree type_main_variant (tree);
6288 static int is_tagged_type (const_tree);
6289 static const char *dwarf_tag_name (unsigned);
6290 static const char *dwarf_attr_name (unsigned);
6291 static const char *dwarf_form_name (unsigned);
6292 static tree decl_ultimate_origin (const_tree);
6293 static tree decl_class_context (tree);
6294 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6295 static inline enum dw_val_class AT_class (dw_attr_ref);
6296 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6297 static inline unsigned AT_flag (dw_attr_ref);
6298 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6299 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6300 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6301 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6302 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6303 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6304 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6305 unsigned int, unsigned char *);
6306 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6307 static hashval_t debug_str_do_hash (const void *);
6308 static int debug_str_eq (const void *, const void *);
6309 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6310 static inline const char *AT_string (dw_attr_ref);
6311 static enum dwarf_form AT_string_form (dw_attr_ref);
6312 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6313 static void add_AT_specification (dw_die_ref, dw_die_ref);
6314 static inline dw_die_ref AT_ref (dw_attr_ref);
6315 static inline int AT_ref_external (dw_attr_ref);
6316 static inline void set_AT_ref_external (dw_attr_ref, int);
6317 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6318 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6319 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6320 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6321 dw_loc_list_ref);
6322 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6323 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6324 static inline rtx AT_addr (dw_attr_ref);
6325 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6326 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6327 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6328 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6329 unsigned HOST_WIDE_INT);
6330 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6331 unsigned long);
6332 static inline const char *AT_lbl (dw_attr_ref);
6333 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6334 static const char *get_AT_low_pc (dw_die_ref);
6335 static const char *get_AT_hi_pc (dw_die_ref);
6336 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6337 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6338 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6339 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6340 static bool is_cxx (void);
6341 static bool is_fortran (void);
6342 static bool is_ada (void);
6343 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6344 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6345 static void add_child_die (dw_die_ref, dw_die_ref);
6346 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6347 static dw_die_ref lookup_type_die (tree);
6348 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6349 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6350 static void equate_type_number_to_die (tree, dw_die_ref);
6351 static hashval_t decl_die_table_hash (const void *);
6352 static int decl_die_table_eq (const void *, const void *);
6353 static dw_die_ref lookup_decl_die (tree);
6354 static hashval_t common_block_die_table_hash (const void *);
6355 static int common_block_die_table_eq (const void *, const void *);
6356 static hashval_t decl_loc_table_hash (const void *);
6357 static int decl_loc_table_eq (const void *, const void *);
6358 static var_loc_list *lookup_decl_loc (const_tree);
6359 static void equate_decl_number_to_die (tree, dw_die_ref);
6360 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6361 static void print_spaces (FILE *);
6362 static void print_die (dw_die_ref, FILE *);
6363 static void print_dwarf_line_table (FILE *);
6364 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6365 static dw_die_ref pop_compile_unit (dw_die_ref);
6366 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6367 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6368 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6369 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6370 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6371 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6372 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6373 struct md5_ctx *, int *);
6374 struct checksum_attributes;
6375 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6376 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6377 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6378 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6379 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6380 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6381 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6382 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6383 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6384 static void compute_section_prefix (dw_die_ref);
6385 static int is_type_die (dw_die_ref);
6386 static int is_comdat_die (dw_die_ref);
6387 static int is_symbol_die (dw_die_ref);
6388 static void assign_symbol_names (dw_die_ref);
6389 static void break_out_includes (dw_die_ref);
6390 static int is_declaration_die (dw_die_ref);
6391 static int should_move_die_to_comdat (dw_die_ref);
6392 static dw_die_ref clone_as_declaration (dw_die_ref);
6393 static dw_die_ref clone_die (dw_die_ref);
6394 static dw_die_ref clone_tree (dw_die_ref);
6395 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6396 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6397 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6398 static dw_die_ref generate_skeleton (dw_die_ref);
6399 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6400 dw_die_ref);
6401 static void break_out_comdat_types (dw_die_ref);
6402 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6403 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6404 static void copy_decls_for_unworthy_types (dw_die_ref);
6406 static hashval_t htab_cu_hash (const void *);
6407 static int htab_cu_eq (const void *, const void *);
6408 static void htab_cu_del (void *);
6409 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6410 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6411 static void add_sibling_attributes (dw_die_ref);
6412 static void build_abbrev_table (dw_die_ref);
6413 static void output_location_lists (dw_die_ref);
6414 static int constant_size (unsigned HOST_WIDE_INT);
6415 static unsigned long size_of_die (dw_die_ref);
6416 static void calc_die_sizes (dw_die_ref);
6417 static void mark_dies (dw_die_ref);
6418 static void unmark_dies (dw_die_ref);
6419 static void unmark_all_dies (dw_die_ref);
6420 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6421 static unsigned long size_of_aranges (void);
6422 static enum dwarf_form value_format (dw_attr_ref);
6423 static void output_value_format (dw_attr_ref);
6424 static void output_abbrev_section (void);
6425 static void output_die_symbol (dw_die_ref);
6426 static void output_die (dw_die_ref);
6427 static void output_compilation_unit_header (void);
6428 static void output_comp_unit (dw_die_ref, int);
6429 static void output_comdat_type_unit (comdat_type_node *);
6430 static const char *dwarf2_name (tree, int);
6431 static void add_pubname (tree, dw_die_ref);
6432 static void add_pubname_string (const char *, dw_die_ref);
6433 static void add_pubtype (tree, dw_die_ref);
6434 static void output_pubnames (VEC (pubname_entry,gc) *);
6435 static void add_arange (tree, dw_die_ref);
6436 static void output_aranges (void);
6437 static unsigned int add_ranges_num (int);
6438 static unsigned int add_ranges (const_tree);
6439 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6440 bool *);
6441 static void output_ranges (void);
6442 static void output_line_info (void);
6443 static void output_file_names (void);
6444 static dw_die_ref base_type_die (tree);
6445 static int is_base_type (tree);
6446 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6447 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6448 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6449 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6450 static int type_is_enum (const_tree);
6451 static unsigned int dbx_reg_number (const_rtx);
6452 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6453 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6454 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6455 enum var_init_status);
6456 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6457 enum var_init_status);
6458 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6459 enum var_init_status);
6460 static int is_based_loc (const_rtx);
6461 static int resolve_one_addr (rtx *, void *);
6462 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6463 enum var_init_status);
6464 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6465 enum var_init_status);
6466 static dw_loc_list_ref loc_list_from_tree (tree, int);
6467 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6468 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6469 static tree field_type (const_tree);
6470 static unsigned int simple_type_align_in_bits (const_tree);
6471 static unsigned int simple_decl_align_in_bits (const_tree);
6472 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6473 static HOST_WIDE_INT field_byte_offset (const_tree);
6474 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6475 dw_loc_list_ref);
6476 static void add_data_member_location_attribute (dw_die_ref, tree);
6477 static bool add_const_value_attribute (dw_die_ref, rtx);
6478 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6479 static void insert_double (double_int, unsigned char *);
6480 static void insert_float (const_rtx, unsigned char *);
6481 static rtx rtl_for_decl_location (tree);
6482 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6483 enum dwarf_attribute);
6484 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6485 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6486 static void add_name_attribute (dw_die_ref, const char *);
6487 static void add_comp_dir_attribute (dw_die_ref);
6488 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6489 static void add_subscript_info (dw_die_ref, tree, bool);
6490 static void add_byte_size_attribute (dw_die_ref, tree);
6491 static void add_bit_offset_attribute (dw_die_ref, tree);
6492 static void add_bit_size_attribute (dw_die_ref, tree);
6493 static void add_prototyped_attribute (dw_die_ref, tree);
6494 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6495 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6496 static void add_src_coords_attributes (dw_die_ref, tree);
6497 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6498 static void push_decl_scope (tree);
6499 static void pop_decl_scope (void);
6500 static dw_die_ref scope_die_for (tree, dw_die_ref);
6501 static inline int local_scope_p (dw_die_ref);
6502 static inline int class_scope_p (dw_die_ref);
6503 static inline int class_or_namespace_scope_p (dw_die_ref);
6504 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6505 static void add_calling_convention_attribute (dw_die_ref, tree);
6506 static const char *type_tag (const_tree);
6507 static tree member_declared_type (const_tree);
6508 #if 0
6509 static const char *decl_start_label (tree);
6510 #endif
6511 static void gen_array_type_die (tree, dw_die_ref);
6512 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6513 #if 0
6514 static void gen_entry_point_die (tree, dw_die_ref);
6515 #endif
6516 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6517 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6518 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
6519 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6520 static void gen_formal_types_die (tree, dw_die_ref);
6521 static void gen_subprogram_die (tree, dw_die_ref);
6522 static void gen_variable_die (tree, tree, dw_die_ref);
6523 static void gen_const_die (tree, dw_die_ref);
6524 static void gen_label_die (tree, dw_die_ref);
6525 static void gen_lexical_block_die (tree, dw_die_ref, int);
6526 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6527 static void gen_field_die (tree, dw_die_ref);
6528 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6529 static dw_die_ref gen_compile_unit_die (const char *);
6530 static void gen_inheritance_die (tree, tree, dw_die_ref);
6531 static void gen_member_die (tree, dw_die_ref);
6532 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6533 enum debug_info_usage);
6534 static void gen_subroutine_type_die (tree, dw_die_ref);
6535 static void gen_typedef_die (tree, dw_die_ref);
6536 static void gen_type_die (tree, dw_die_ref);
6537 static void gen_block_die (tree, dw_die_ref, int);
6538 static void decls_for_scope (tree, dw_die_ref, int);
6539 static int is_redundant_typedef (const_tree);
6540 static bool is_naming_typedef_decl (const_tree);
6541 static inline dw_die_ref get_context_die (tree);
6542 static void gen_namespace_die (tree, dw_die_ref);
6543 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6544 static dw_die_ref force_decl_die (tree);
6545 static dw_die_ref force_type_die (tree);
6546 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6547 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6548 static struct dwarf_file_data * lookup_filename (const char *);
6549 static void retry_incomplete_types (void);
6550 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6551 static void gen_generic_params_dies (tree);
6552 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6553 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6554 static void splice_child_die (dw_die_ref, dw_die_ref);
6555 static int file_info_cmp (const void *, const void *);
6556 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6557 const char *, const char *);
6558 static void output_loc_list (dw_loc_list_ref);
6559 static char *gen_internal_sym (const char *);
6561 static void prune_unmark_dies (dw_die_ref);
6562 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6563 static void prune_unused_types_mark (dw_die_ref, int);
6564 static void prune_unused_types_walk (dw_die_ref);
6565 static void prune_unused_types_walk_attribs (dw_die_ref);
6566 static void prune_unused_types_prune (dw_die_ref);
6567 static void prune_unused_types (void);
6568 static int maybe_emit_file (struct dwarf_file_data *fd);
6569 static inline const char *AT_vms_delta1 (dw_attr_ref);
6570 static inline const char *AT_vms_delta2 (dw_attr_ref);
6571 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6572 const char *, const char *);
6573 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6574 static void gen_remaining_tmpl_value_param_die_attribute (void);
6575 static bool generic_type_p (tree);
6576 static void schedule_generic_params_dies_gen (tree t);
6577 static void gen_scheduled_generic_parms_dies (void);
6579 /* Section names used to hold DWARF debugging information. */
6580 #ifndef DEBUG_INFO_SECTION
6581 #define DEBUG_INFO_SECTION ".debug_info"
6582 #endif
6583 #ifndef DEBUG_ABBREV_SECTION
6584 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
6585 #endif
6586 #ifndef DEBUG_ARANGES_SECTION
6587 #define DEBUG_ARANGES_SECTION ".debug_aranges"
6588 #endif
6589 #ifndef DEBUG_MACINFO_SECTION
6590 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
6591 #endif
6592 #ifndef DEBUG_LINE_SECTION
6593 #define DEBUG_LINE_SECTION ".debug_line"
6594 #endif
6595 #ifndef DEBUG_LOC_SECTION
6596 #define DEBUG_LOC_SECTION ".debug_loc"
6597 #endif
6598 #ifndef DEBUG_PUBNAMES_SECTION
6599 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
6600 #endif
6601 #ifndef DEBUG_PUBTYPES_SECTION
6602 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
6603 #endif
6604 #ifndef DEBUG_STR_SECTION
6605 #define DEBUG_STR_SECTION ".debug_str"
6606 #endif
6607 #ifndef DEBUG_RANGES_SECTION
6608 #define DEBUG_RANGES_SECTION ".debug_ranges"
6609 #endif
6611 /* Standard ELF section names for compiled code and data. */
6612 #ifndef TEXT_SECTION_NAME
6613 #define TEXT_SECTION_NAME ".text"
6614 #endif
6616 /* Section flags for .debug_str section. */
6617 #define DEBUG_STR_SECTION_FLAGS \
6618 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
6619 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
6620 : SECTION_DEBUG)
6622 /* Labels we insert at beginning sections we can reference instead of
6623 the section names themselves. */
6625 #ifndef TEXT_SECTION_LABEL
6626 #define TEXT_SECTION_LABEL "Ltext"
6627 #endif
6628 #ifndef COLD_TEXT_SECTION_LABEL
6629 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
6630 #endif
6631 #ifndef DEBUG_LINE_SECTION_LABEL
6632 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
6633 #endif
6634 #ifndef DEBUG_INFO_SECTION_LABEL
6635 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
6636 #endif
6637 #ifndef DEBUG_ABBREV_SECTION_LABEL
6638 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
6639 #endif
6640 #ifndef DEBUG_LOC_SECTION_LABEL
6641 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
6642 #endif
6643 #ifndef DEBUG_RANGES_SECTION_LABEL
6644 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
6645 #endif
6646 #ifndef DEBUG_MACINFO_SECTION_LABEL
6647 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
6648 #endif
6651 /* Definitions of defaults for formats and names of various special
6652 (artificial) labels which may be generated within this file (when the -g
6653 options is used and DWARF2_DEBUGGING_INFO is in effect.
6654 If necessary, these may be overridden from within the tm.h file, but
6655 typically, overriding these defaults is unnecessary. */
6657 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6658 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6659 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6660 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6661 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6662 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6663 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6664 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6665 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6666 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6668 #ifndef TEXT_END_LABEL
6669 #define TEXT_END_LABEL "Letext"
6670 #endif
6671 #ifndef COLD_END_LABEL
6672 #define COLD_END_LABEL "Letext_cold"
6673 #endif
6674 #ifndef BLOCK_BEGIN_LABEL
6675 #define BLOCK_BEGIN_LABEL "LBB"
6676 #endif
6677 #ifndef BLOCK_END_LABEL
6678 #define BLOCK_END_LABEL "LBE"
6679 #endif
6680 #ifndef LINE_CODE_LABEL
6681 #define LINE_CODE_LABEL "LM"
6682 #endif
6683 #ifndef SEPARATE_LINE_CODE_LABEL
6684 #define SEPARATE_LINE_CODE_LABEL "LSM"
6685 #endif
6688 /* Return the root of the DIE's built for the current compilation unit. */
6689 static dw_die_ref
6690 comp_unit_die (void)
6692 if (!single_comp_unit_die)
6693 single_comp_unit_die = gen_compile_unit_die (NULL);
6694 return single_comp_unit_die;
6697 /* We allow a language front-end to designate a function that is to be
6698 called to "demangle" any name before it is put into a DIE. */
6700 static const char *(*demangle_name_func) (const char *);
6702 void
6703 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6705 demangle_name_func = func;
6708 /* Test if rtl node points to a pseudo register. */
6710 static inline int
6711 is_pseudo_reg (const_rtx rtl)
6713 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6714 || (GET_CODE (rtl) == SUBREG
6715 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6718 /* Return a reference to a type, with its const and volatile qualifiers
6719 removed. */
6721 static inline tree
6722 type_main_variant (tree type)
6724 type = TYPE_MAIN_VARIANT (type);
6726 /* ??? There really should be only one main variant among any group of
6727 variants of a given type (and all of the MAIN_VARIANT values for all
6728 members of the group should point to that one type) but sometimes the C
6729 front-end messes this up for array types, so we work around that bug
6730 here. */
6731 if (TREE_CODE (type) == ARRAY_TYPE)
6732 while (type != TYPE_MAIN_VARIANT (type))
6733 type = TYPE_MAIN_VARIANT (type);
6735 return type;
6738 /* Return nonzero if the given type node represents a tagged type. */
6740 static inline int
6741 is_tagged_type (const_tree type)
6743 enum tree_code code = TREE_CODE (type);
6745 return (code == RECORD_TYPE || code == UNION_TYPE
6746 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6749 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
6751 static void
6752 get_ref_die_offset_label (char *label, dw_die_ref ref)
6754 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6757 /* Convert a DIE tag into its string name. */
6759 static const char *
6760 dwarf_tag_name (unsigned int tag)
6762 switch (tag)
6764 case DW_TAG_padding:
6765 return "DW_TAG_padding";
6766 case DW_TAG_array_type:
6767 return "DW_TAG_array_type";
6768 case DW_TAG_class_type:
6769 return "DW_TAG_class_type";
6770 case DW_TAG_entry_point:
6771 return "DW_TAG_entry_point";
6772 case DW_TAG_enumeration_type:
6773 return "DW_TAG_enumeration_type";
6774 case DW_TAG_formal_parameter:
6775 return "DW_TAG_formal_parameter";
6776 case DW_TAG_imported_declaration:
6777 return "DW_TAG_imported_declaration";
6778 case DW_TAG_label:
6779 return "DW_TAG_label";
6780 case DW_TAG_lexical_block:
6781 return "DW_TAG_lexical_block";
6782 case DW_TAG_member:
6783 return "DW_TAG_member";
6784 case DW_TAG_pointer_type:
6785 return "DW_TAG_pointer_type";
6786 case DW_TAG_reference_type:
6787 return "DW_TAG_reference_type";
6788 case DW_TAG_compile_unit:
6789 return "DW_TAG_compile_unit";
6790 case DW_TAG_string_type:
6791 return "DW_TAG_string_type";
6792 case DW_TAG_structure_type:
6793 return "DW_TAG_structure_type";
6794 case DW_TAG_subroutine_type:
6795 return "DW_TAG_subroutine_type";
6796 case DW_TAG_typedef:
6797 return "DW_TAG_typedef";
6798 case DW_TAG_union_type:
6799 return "DW_TAG_union_type";
6800 case DW_TAG_unspecified_parameters:
6801 return "DW_TAG_unspecified_parameters";
6802 case DW_TAG_variant:
6803 return "DW_TAG_variant";
6804 case DW_TAG_common_block:
6805 return "DW_TAG_common_block";
6806 case DW_TAG_common_inclusion:
6807 return "DW_TAG_common_inclusion";
6808 case DW_TAG_inheritance:
6809 return "DW_TAG_inheritance";
6810 case DW_TAG_inlined_subroutine:
6811 return "DW_TAG_inlined_subroutine";
6812 case DW_TAG_module:
6813 return "DW_TAG_module";
6814 case DW_TAG_ptr_to_member_type:
6815 return "DW_TAG_ptr_to_member_type";
6816 case DW_TAG_set_type:
6817 return "DW_TAG_set_type";
6818 case DW_TAG_subrange_type:
6819 return "DW_TAG_subrange_type";
6820 case DW_TAG_with_stmt:
6821 return "DW_TAG_with_stmt";
6822 case DW_TAG_access_declaration:
6823 return "DW_TAG_access_declaration";
6824 case DW_TAG_base_type:
6825 return "DW_TAG_base_type";
6826 case DW_TAG_catch_block:
6827 return "DW_TAG_catch_block";
6828 case DW_TAG_const_type:
6829 return "DW_TAG_const_type";
6830 case DW_TAG_constant:
6831 return "DW_TAG_constant";
6832 case DW_TAG_enumerator:
6833 return "DW_TAG_enumerator";
6834 case DW_TAG_file_type:
6835 return "DW_TAG_file_type";
6836 case DW_TAG_friend:
6837 return "DW_TAG_friend";
6838 case DW_TAG_namelist:
6839 return "DW_TAG_namelist";
6840 case DW_TAG_namelist_item:
6841 return "DW_TAG_namelist_item";
6842 case DW_TAG_packed_type:
6843 return "DW_TAG_packed_type";
6844 case DW_TAG_subprogram:
6845 return "DW_TAG_subprogram";
6846 case DW_TAG_template_type_param:
6847 return "DW_TAG_template_type_param";
6848 case DW_TAG_template_value_param:
6849 return "DW_TAG_template_value_param";
6850 case DW_TAG_thrown_type:
6851 return "DW_TAG_thrown_type";
6852 case DW_TAG_try_block:
6853 return "DW_TAG_try_block";
6854 case DW_TAG_variant_part:
6855 return "DW_TAG_variant_part";
6856 case DW_TAG_variable:
6857 return "DW_TAG_variable";
6858 case DW_TAG_volatile_type:
6859 return "DW_TAG_volatile_type";
6860 case DW_TAG_dwarf_procedure:
6861 return "DW_TAG_dwarf_procedure";
6862 case DW_TAG_restrict_type:
6863 return "DW_TAG_restrict_type";
6864 case DW_TAG_interface_type:
6865 return "DW_TAG_interface_type";
6866 case DW_TAG_namespace:
6867 return "DW_TAG_namespace";
6868 case DW_TAG_imported_module:
6869 return "DW_TAG_imported_module";
6870 case DW_TAG_unspecified_type:
6871 return "DW_TAG_unspecified_type";
6872 case DW_TAG_partial_unit:
6873 return "DW_TAG_partial_unit";
6874 case DW_TAG_imported_unit:
6875 return "DW_TAG_imported_unit";
6876 case DW_TAG_condition:
6877 return "DW_TAG_condition";
6878 case DW_TAG_shared_type:
6879 return "DW_TAG_shared_type";
6880 case DW_TAG_type_unit:
6881 return "DW_TAG_type_unit";
6882 case DW_TAG_rvalue_reference_type:
6883 return "DW_TAG_rvalue_reference_type";
6884 case DW_TAG_template_alias:
6885 return "DW_TAG_template_alias";
6886 case DW_TAG_GNU_template_parameter_pack:
6887 return "DW_TAG_GNU_template_parameter_pack";
6888 case DW_TAG_GNU_formal_parameter_pack:
6889 return "DW_TAG_GNU_formal_parameter_pack";
6890 case DW_TAG_MIPS_loop:
6891 return "DW_TAG_MIPS_loop";
6892 case DW_TAG_format_label:
6893 return "DW_TAG_format_label";
6894 case DW_TAG_function_template:
6895 return "DW_TAG_function_template";
6896 case DW_TAG_class_template:
6897 return "DW_TAG_class_template";
6898 case DW_TAG_GNU_BINCL:
6899 return "DW_TAG_GNU_BINCL";
6900 case DW_TAG_GNU_EINCL:
6901 return "DW_TAG_GNU_EINCL";
6902 case DW_TAG_GNU_template_template_param:
6903 return "DW_TAG_GNU_template_template_param";
6904 case DW_TAG_GNU_call_site:
6905 return "DW_TAG_GNU_call_site";
6906 case DW_TAG_GNU_call_site_parameter:
6907 return "DW_TAG_GNU_call_site_parameter";
6908 default:
6909 return "DW_TAG_<unknown>";
6913 /* Convert a DWARF attribute code into its string name. */
6915 static const char *
6916 dwarf_attr_name (unsigned int attr)
6918 switch (attr)
6920 case DW_AT_sibling:
6921 return "DW_AT_sibling";
6922 case DW_AT_location:
6923 return "DW_AT_location";
6924 case DW_AT_name:
6925 return "DW_AT_name";
6926 case DW_AT_ordering:
6927 return "DW_AT_ordering";
6928 case DW_AT_subscr_data:
6929 return "DW_AT_subscr_data";
6930 case DW_AT_byte_size:
6931 return "DW_AT_byte_size";
6932 case DW_AT_bit_offset:
6933 return "DW_AT_bit_offset";
6934 case DW_AT_bit_size:
6935 return "DW_AT_bit_size";
6936 case DW_AT_element_list:
6937 return "DW_AT_element_list";
6938 case DW_AT_stmt_list:
6939 return "DW_AT_stmt_list";
6940 case DW_AT_low_pc:
6941 return "DW_AT_low_pc";
6942 case DW_AT_high_pc:
6943 return "DW_AT_high_pc";
6944 case DW_AT_language:
6945 return "DW_AT_language";
6946 case DW_AT_member:
6947 return "DW_AT_member";
6948 case DW_AT_discr:
6949 return "DW_AT_discr";
6950 case DW_AT_discr_value:
6951 return "DW_AT_discr_value";
6952 case DW_AT_visibility:
6953 return "DW_AT_visibility";
6954 case DW_AT_import:
6955 return "DW_AT_import";
6956 case DW_AT_string_length:
6957 return "DW_AT_string_length";
6958 case DW_AT_common_reference:
6959 return "DW_AT_common_reference";
6960 case DW_AT_comp_dir:
6961 return "DW_AT_comp_dir";
6962 case DW_AT_const_value:
6963 return "DW_AT_const_value";
6964 case DW_AT_containing_type:
6965 return "DW_AT_containing_type";
6966 case DW_AT_default_value:
6967 return "DW_AT_default_value";
6968 case DW_AT_inline:
6969 return "DW_AT_inline";
6970 case DW_AT_is_optional:
6971 return "DW_AT_is_optional";
6972 case DW_AT_lower_bound:
6973 return "DW_AT_lower_bound";
6974 case DW_AT_producer:
6975 return "DW_AT_producer";
6976 case DW_AT_prototyped:
6977 return "DW_AT_prototyped";
6978 case DW_AT_return_addr:
6979 return "DW_AT_return_addr";
6980 case DW_AT_start_scope:
6981 return "DW_AT_start_scope";
6982 case DW_AT_bit_stride:
6983 return "DW_AT_bit_stride";
6984 case DW_AT_upper_bound:
6985 return "DW_AT_upper_bound";
6986 case DW_AT_abstract_origin:
6987 return "DW_AT_abstract_origin";
6988 case DW_AT_accessibility:
6989 return "DW_AT_accessibility";
6990 case DW_AT_address_class:
6991 return "DW_AT_address_class";
6992 case DW_AT_artificial:
6993 return "DW_AT_artificial";
6994 case DW_AT_base_types:
6995 return "DW_AT_base_types";
6996 case DW_AT_calling_convention:
6997 return "DW_AT_calling_convention";
6998 case DW_AT_count:
6999 return "DW_AT_count";
7000 case DW_AT_data_member_location:
7001 return "DW_AT_data_member_location";
7002 case DW_AT_decl_column:
7003 return "DW_AT_decl_column";
7004 case DW_AT_decl_file:
7005 return "DW_AT_decl_file";
7006 case DW_AT_decl_line:
7007 return "DW_AT_decl_line";
7008 case DW_AT_declaration:
7009 return "DW_AT_declaration";
7010 case DW_AT_discr_list:
7011 return "DW_AT_discr_list";
7012 case DW_AT_encoding:
7013 return "DW_AT_encoding";
7014 case DW_AT_external:
7015 return "DW_AT_external";
7016 case DW_AT_explicit:
7017 return "DW_AT_explicit";
7018 case DW_AT_frame_base:
7019 return "DW_AT_frame_base";
7020 case DW_AT_friend:
7021 return "DW_AT_friend";
7022 case DW_AT_identifier_case:
7023 return "DW_AT_identifier_case";
7024 case DW_AT_macro_info:
7025 return "DW_AT_macro_info";
7026 case DW_AT_namelist_items:
7027 return "DW_AT_namelist_items";
7028 case DW_AT_priority:
7029 return "DW_AT_priority";
7030 case DW_AT_segment:
7031 return "DW_AT_segment";
7032 case DW_AT_specification:
7033 return "DW_AT_specification";
7034 case DW_AT_static_link:
7035 return "DW_AT_static_link";
7036 case DW_AT_type:
7037 return "DW_AT_type";
7038 case DW_AT_use_location:
7039 return "DW_AT_use_location";
7040 case DW_AT_variable_parameter:
7041 return "DW_AT_variable_parameter";
7042 case DW_AT_virtuality:
7043 return "DW_AT_virtuality";
7044 case DW_AT_vtable_elem_location:
7045 return "DW_AT_vtable_elem_location";
7047 case DW_AT_allocated:
7048 return "DW_AT_allocated";
7049 case DW_AT_associated:
7050 return "DW_AT_associated";
7051 case DW_AT_data_location:
7052 return "DW_AT_data_location";
7053 case DW_AT_byte_stride:
7054 return "DW_AT_byte_stride";
7055 case DW_AT_entry_pc:
7056 return "DW_AT_entry_pc";
7057 case DW_AT_use_UTF8:
7058 return "DW_AT_use_UTF8";
7059 case DW_AT_extension:
7060 return "DW_AT_extension";
7061 case DW_AT_ranges:
7062 return "DW_AT_ranges";
7063 case DW_AT_trampoline:
7064 return "DW_AT_trampoline";
7065 case DW_AT_call_column:
7066 return "DW_AT_call_column";
7067 case DW_AT_call_file:
7068 return "DW_AT_call_file";
7069 case DW_AT_call_line:
7070 return "DW_AT_call_line";
7071 case DW_AT_object_pointer:
7072 return "DW_AT_object_pointer";
7074 case DW_AT_signature:
7075 return "DW_AT_signature";
7076 case DW_AT_main_subprogram:
7077 return "DW_AT_main_subprogram";
7078 case DW_AT_data_bit_offset:
7079 return "DW_AT_data_bit_offset";
7080 case DW_AT_const_expr:
7081 return "DW_AT_const_expr";
7082 case DW_AT_enum_class:
7083 return "DW_AT_enum_class";
7084 case DW_AT_linkage_name:
7085 return "DW_AT_linkage_name";
7087 case DW_AT_MIPS_fde:
7088 return "DW_AT_MIPS_fde";
7089 case DW_AT_MIPS_loop_begin:
7090 return "DW_AT_MIPS_loop_begin";
7091 case DW_AT_MIPS_tail_loop_begin:
7092 return "DW_AT_MIPS_tail_loop_begin";
7093 case DW_AT_MIPS_epilog_begin:
7094 return "DW_AT_MIPS_epilog_begin";
7095 #if VMS_DEBUGGING_INFO
7096 case DW_AT_HP_prologue:
7097 return "DW_AT_HP_prologue";
7098 #else
7099 case DW_AT_MIPS_loop_unroll_factor:
7100 return "DW_AT_MIPS_loop_unroll_factor";
7101 #endif
7102 case DW_AT_MIPS_software_pipeline_depth:
7103 return "DW_AT_MIPS_software_pipeline_depth";
7104 case DW_AT_MIPS_linkage_name:
7105 return "DW_AT_MIPS_linkage_name";
7106 #if VMS_DEBUGGING_INFO
7107 case DW_AT_HP_epilogue:
7108 return "DW_AT_HP_epilogue";
7109 #else
7110 case DW_AT_MIPS_stride:
7111 return "DW_AT_MIPS_stride";
7112 #endif
7113 case DW_AT_MIPS_abstract_name:
7114 return "DW_AT_MIPS_abstract_name";
7115 case DW_AT_MIPS_clone_origin:
7116 return "DW_AT_MIPS_clone_origin";
7117 case DW_AT_MIPS_has_inlines:
7118 return "DW_AT_MIPS_has_inlines";
7120 case DW_AT_sf_names:
7121 return "DW_AT_sf_names";
7122 case DW_AT_src_info:
7123 return "DW_AT_src_info";
7124 case DW_AT_mac_info:
7125 return "DW_AT_mac_info";
7126 case DW_AT_src_coords:
7127 return "DW_AT_src_coords";
7128 case DW_AT_body_begin:
7129 return "DW_AT_body_begin";
7130 case DW_AT_body_end:
7131 return "DW_AT_body_end";
7132 case DW_AT_GNU_vector:
7133 return "DW_AT_GNU_vector";
7134 case DW_AT_GNU_guarded_by:
7135 return "DW_AT_GNU_guarded_by";
7136 case DW_AT_GNU_pt_guarded_by:
7137 return "DW_AT_GNU_pt_guarded_by";
7138 case DW_AT_GNU_guarded:
7139 return "DW_AT_GNU_guarded";
7140 case DW_AT_GNU_pt_guarded:
7141 return "DW_AT_GNU_pt_guarded";
7142 case DW_AT_GNU_locks_excluded:
7143 return "DW_AT_GNU_locks_excluded";
7144 case DW_AT_GNU_exclusive_locks_required:
7145 return "DW_AT_GNU_exclusive_locks_required";
7146 case DW_AT_GNU_shared_locks_required:
7147 return "DW_AT_GNU_shared_locks_required";
7148 case DW_AT_GNU_odr_signature:
7149 return "DW_AT_GNU_odr_signature";
7150 case DW_AT_GNU_template_name:
7151 return "DW_AT_GNU_template_name";
7152 case DW_AT_GNU_call_site_value:
7153 return "DW_AT_GNU_call_site_value";
7154 case DW_AT_GNU_call_site_data_value:
7155 return "DW_AT_GNU_call_site_data_value";
7156 case DW_AT_GNU_call_site_target:
7157 return "DW_AT_GNU_call_site_target";
7158 case DW_AT_GNU_call_site_target_clobbered:
7159 return "DW_AT_GNU_call_site_target_clobbered";
7160 case DW_AT_GNU_tail_call:
7161 return "DW_AT_GNU_tail_call";
7162 case DW_AT_GNU_all_tail_call_sites:
7163 return "DW_AT_GNU_all_tail_call_sites";
7164 case DW_AT_GNU_all_call_sites:
7165 return "DW_AT_GNU_all_call_sites";
7166 case DW_AT_GNU_all_source_call_sites:
7167 return "DW_AT_GNU_all_source_call_sites";
7169 case DW_AT_VMS_rtnbeg_pd_address:
7170 return "DW_AT_VMS_rtnbeg_pd_address";
7172 default:
7173 return "DW_AT_<unknown>";
7177 /* Convert a DWARF value form code into its string name. */
7179 static const char *
7180 dwarf_form_name (unsigned int form)
7182 switch (form)
7184 case DW_FORM_addr:
7185 return "DW_FORM_addr";
7186 case DW_FORM_block2:
7187 return "DW_FORM_block2";
7188 case DW_FORM_block4:
7189 return "DW_FORM_block4";
7190 case DW_FORM_data2:
7191 return "DW_FORM_data2";
7192 case DW_FORM_data4:
7193 return "DW_FORM_data4";
7194 case DW_FORM_data8:
7195 return "DW_FORM_data8";
7196 case DW_FORM_string:
7197 return "DW_FORM_string";
7198 case DW_FORM_block:
7199 return "DW_FORM_block";
7200 case DW_FORM_block1:
7201 return "DW_FORM_block1";
7202 case DW_FORM_data1:
7203 return "DW_FORM_data1";
7204 case DW_FORM_flag:
7205 return "DW_FORM_flag";
7206 case DW_FORM_sdata:
7207 return "DW_FORM_sdata";
7208 case DW_FORM_strp:
7209 return "DW_FORM_strp";
7210 case DW_FORM_udata:
7211 return "DW_FORM_udata";
7212 case DW_FORM_ref_addr:
7213 return "DW_FORM_ref_addr";
7214 case DW_FORM_ref1:
7215 return "DW_FORM_ref1";
7216 case DW_FORM_ref2:
7217 return "DW_FORM_ref2";
7218 case DW_FORM_ref4:
7219 return "DW_FORM_ref4";
7220 case DW_FORM_ref8:
7221 return "DW_FORM_ref8";
7222 case DW_FORM_ref_udata:
7223 return "DW_FORM_ref_udata";
7224 case DW_FORM_indirect:
7225 return "DW_FORM_indirect";
7226 case DW_FORM_sec_offset:
7227 return "DW_FORM_sec_offset";
7228 case DW_FORM_exprloc:
7229 return "DW_FORM_exprloc";
7230 case DW_FORM_flag_present:
7231 return "DW_FORM_flag_present";
7232 case DW_FORM_ref_sig8:
7233 return "DW_FORM_ref_sig8";
7234 default:
7235 return "DW_FORM_<unknown>";
7239 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
7240 instance of an inlined instance of a decl which is local to an inline
7241 function, so we have to trace all of the way back through the origin chain
7242 to find out what sort of node actually served as the original seed for the
7243 given block. */
7245 static tree
7246 decl_ultimate_origin (const_tree decl)
7248 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7249 return NULL_TREE;
7251 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7252 nodes in the function to point to themselves; ignore that if
7253 we're trying to output the abstract instance of this function. */
7254 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7255 return NULL_TREE;
7257 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7258 most distant ancestor, this should never happen. */
7259 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7261 return DECL_ABSTRACT_ORIGIN (decl);
7264 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
7265 of a virtual function may refer to a base class, so we check the 'this'
7266 parameter. */
7268 static tree
7269 decl_class_context (tree decl)
7271 tree context = NULL_TREE;
7273 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7274 context = DECL_CONTEXT (decl);
7275 else
7276 context = TYPE_MAIN_VARIANT
7277 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7279 if (context && !TYPE_P (context))
7280 context = NULL_TREE;
7282 return context;
7285 /* Add an attribute/value pair to a DIE. */
7287 static inline void
7288 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7290 /* Maybe this should be an assert? */
7291 if (die == NULL)
7292 return;
7294 if (die->die_attr == NULL)
7295 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7296 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7299 static inline enum dw_val_class
7300 AT_class (dw_attr_ref a)
7302 return a->dw_attr_val.val_class;
7305 /* Add a flag value attribute to a DIE. */
7307 static inline void
7308 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7310 dw_attr_node attr;
7312 attr.dw_attr = attr_kind;
7313 attr.dw_attr_val.val_class = dw_val_class_flag;
7314 attr.dw_attr_val.v.val_flag = flag;
7315 add_dwarf_attr (die, &attr);
7318 static inline unsigned
7319 AT_flag (dw_attr_ref a)
7321 gcc_assert (a && AT_class (a) == dw_val_class_flag);
7322 return a->dw_attr_val.v.val_flag;
7325 /* Add a signed integer attribute value to a DIE. */
7327 static inline void
7328 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7330 dw_attr_node attr;
7332 attr.dw_attr = attr_kind;
7333 attr.dw_attr_val.val_class = dw_val_class_const;
7334 attr.dw_attr_val.v.val_int = int_val;
7335 add_dwarf_attr (die, &attr);
7338 static inline HOST_WIDE_INT
7339 AT_int (dw_attr_ref a)
7341 gcc_assert (a && AT_class (a) == dw_val_class_const);
7342 return a->dw_attr_val.v.val_int;
7345 /* Add an unsigned integer attribute value to a DIE. */
7347 static inline void
7348 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7349 unsigned HOST_WIDE_INT unsigned_val)
7351 dw_attr_node attr;
7353 attr.dw_attr = attr_kind;
7354 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7355 attr.dw_attr_val.v.val_unsigned = unsigned_val;
7356 add_dwarf_attr (die, &attr);
7359 static inline unsigned HOST_WIDE_INT
7360 AT_unsigned (dw_attr_ref a)
7362 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7363 return a->dw_attr_val.v.val_unsigned;
7366 /* Add an unsigned double integer attribute value to a DIE. */
7368 static inline void
7369 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7370 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7372 dw_attr_node attr;
7374 attr.dw_attr = attr_kind;
7375 attr.dw_attr_val.val_class = dw_val_class_const_double;
7376 attr.dw_attr_val.v.val_double.high = high;
7377 attr.dw_attr_val.v.val_double.low = low;
7378 add_dwarf_attr (die, &attr);
7381 /* Add a floating point attribute value to a DIE and return it. */
7383 static inline void
7384 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7385 unsigned int length, unsigned int elt_size, unsigned char *array)
7387 dw_attr_node attr;
7389 attr.dw_attr = attr_kind;
7390 attr.dw_attr_val.val_class = dw_val_class_vec;
7391 attr.dw_attr_val.v.val_vec.length = length;
7392 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7393 attr.dw_attr_val.v.val_vec.array = array;
7394 add_dwarf_attr (die, &attr);
7397 /* Add an 8-byte data attribute value to a DIE. */
7399 static inline void
7400 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7401 unsigned char data8[8])
7403 dw_attr_node attr;
7405 attr.dw_attr = attr_kind;
7406 attr.dw_attr_val.val_class = dw_val_class_data8;
7407 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7408 add_dwarf_attr (die, &attr);
7411 /* Hash and equality functions for debug_str_hash. */
7413 static hashval_t
7414 debug_str_do_hash (const void *x)
7416 return htab_hash_string (((const struct indirect_string_node *)x)->str);
7419 static int
7420 debug_str_eq (const void *x1, const void *x2)
7422 return strcmp ((((const struct indirect_string_node *)x1)->str),
7423 (const char *)x2) == 0;
7426 /* Add STR to the indirect string hash table. */
7428 static struct indirect_string_node *
7429 find_AT_string (const char *str)
7431 struct indirect_string_node *node;
7432 void **slot;
7434 if (! debug_str_hash)
7435 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7436 debug_str_eq, NULL);
7438 slot = htab_find_slot_with_hash (debug_str_hash, str,
7439 htab_hash_string (str), INSERT);
7440 if (*slot == NULL)
7442 node = ggc_alloc_cleared_indirect_string_node ();
7443 node->str = ggc_strdup (str);
7444 *slot = node;
7446 else
7447 node = (struct indirect_string_node *) *slot;
7449 node->refcount++;
7450 return node;
7453 /* Add a string attribute value to a DIE. */
7455 static inline void
7456 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7458 dw_attr_node attr;
7459 struct indirect_string_node *node;
7461 node = find_AT_string (str);
7463 attr.dw_attr = attr_kind;
7464 attr.dw_attr_val.val_class = dw_val_class_str;
7465 attr.dw_attr_val.v.val_str = node;
7466 add_dwarf_attr (die, &attr);
7469 /* Create a label for an indirect string node, ensuring it is going to
7470 be output, unless its reference count goes down to zero. */
7472 static inline void
7473 gen_label_for_indirect_string (struct indirect_string_node *node)
7475 char label[32];
7477 if (node->label)
7478 return;
7480 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7481 ++dw2_string_counter;
7482 node->label = xstrdup (label);
7485 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7486 debug string STR. */
7488 static inline rtx
7489 get_debug_string_label (const char *str)
7491 struct indirect_string_node *node = find_AT_string (str);
7493 debug_str_hash_forced = true;
7495 gen_label_for_indirect_string (node);
7497 return gen_rtx_SYMBOL_REF (Pmode, node->label);
7500 static inline const char *
7501 AT_string (dw_attr_ref a)
7503 gcc_assert (a && AT_class (a) == dw_val_class_str);
7504 return a->dw_attr_val.v.val_str->str;
7507 /* Find out whether a string should be output inline in DIE
7508 or out-of-line in .debug_str section. */
7510 static enum dwarf_form
7511 AT_string_form (dw_attr_ref a)
7513 struct indirect_string_node *node;
7514 unsigned int len;
7516 gcc_assert (a && AT_class (a) == dw_val_class_str);
7518 node = a->dw_attr_val.v.val_str;
7519 if (node->form)
7520 return node->form;
7522 len = strlen (node->str) + 1;
7524 /* If the string is shorter or equal to the size of the reference, it is
7525 always better to put it inline. */
7526 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7527 return node->form = DW_FORM_string;
7529 /* If we cannot expect the linker to merge strings in .debug_str
7530 section, only put it into .debug_str if it is worth even in this
7531 single module. */
7532 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7533 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7534 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7535 return node->form = DW_FORM_string;
7537 gen_label_for_indirect_string (node);
7539 return node->form = DW_FORM_strp;
7542 /* Add a DIE reference attribute value to a DIE. */
7544 static inline void
7545 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7547 dw_attr_node attr;
7549 #ifdef ENABLE_CHECKING
7550 gcc_assert (targ_die != NULL);
7551 #else
7552 /* With LTO we can end up trying to reference something we didn't create
7553 a DIE for. Avoid crashing later on a NULL referenced DIE. */
7554 if (targ_die == NULL)
7555 return;
7556 #endif
7558 attr.dw_attr = attr_kind;
7559 attr.dw_attr_val.val_class = dw_val_class_die_ref;
7560 attr.dw_attr_val.v.val_die_ref.die = targ_die;
7561 attr.dw_attr_val.v.val_die_ref.external = 0;
7562 add_dwarf_attr (die, &attr);
7565 /* Add an AT_specification attribute to a DIE, and also make the back
7566 pointer from the specification to the definition. */
7568 static inline void
7569 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7571 add_AT_die_ref (die, DW_AT_specification, targ_die);
7572 gcc_assert (!targ_die->die_definition);
7573 targ_die->die_definition = die;
7576 static inline dw_die_ref
7577 AT_ref (dw_attr_ref a)
7579 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7580 return a->dw_attr_val.v.val_die_ref.die;
7583 static inline int
7584 AT_ref_external (dw_attr_ref a)
7586 if (a && AT_class (a) == dw_val_class_die_ref)
7587 return a->dw_attr_val.v.val_die_ref.external;
7589 return 0;
7592 static inline void
7593 set_AT_ref_external (dw_attr_ref a, int i)
7595 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7596 a->dw_attr_val.v.val_die_ref.external = i;
7599 /* Add an FDE reference attribute value to a DIE. */
7601 static inline void
7602 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7604 dw_attr_node attr;
7606 attr.dw_attr = attr_kind;
7607 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7608 attr.dw_attr_val.v.val_fde_index = targ_fde;
7609 add_dwarf_attr (die, &attr);
7612 /* Add a location description attribute value to a DIE. */
7614 static inline void
7615 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7617 dw_attr_node attr;
7619 attr.dw_attr = attr_kind;
7620 attr.dw_attr_val.val_class = dw_val_class_loc;
7621 attr.dw_attr_val.v.val_loc = loc;
7622 add_dwarf_attr (die, &attr);
7625 static inline dw_loc_descr_ref
7626 AT_loc (dw_attr_ref a)
7628 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7629 return a->dw_attr_val.v.val_loc;
7632 static inline void
7633 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7635 dw_attr_node attr;
7637 attr.dw_attr = attr_kind;
7638 attr.dw_attr_val.val_class = dw_val_class_loc_list;
7639 attr.dw_attr_val.v.val_loc_list = loc_list;
7640 add_dwarf_attr (die, &attr);
7641 have_location_lists = true;
7644 static inline dw_loc_list_ref
7645 AT_loc_list (dw_attr_ref a)
7647 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7648 return a->dw_attr_val.v.val_loc_list;
7651 static inline dw_loc_list_ref *
7652 AT_loc_list_ptr (dw_attr_ref a)
7654 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7655 return &a->dw_attr_val.v.val_loc_list;
7658 /* Add an address constant attribute value to a DIE. */
7660 static inline void
7661 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7663 dw_attr_node attr;
7665 attr.dw_attr = attr_kind;
7666 attr.dw_attr_val.val_class = dw_val_class_addr;
7667 attr.dw_attr_val.v.val_addr = addr;
7668 add_dwarf_attr (die, &attr);
7671 /* Get the RTX from to an address DIE attribute. */
7673 static inline rtx
7674 AT_addr (dw_attr_ref a)
7676 gcc_assert (a && AT_class (a) == dw_val_class_addr);
7677 return a->dw_attr_val.v.val_addr;
7680 /* Add a file attribute value to a DIE. */
7682 static inline void
7683 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7684 struct dwarf_file_data *fd)
7686 dw_attr_node attr;
7688 attr.dw_attr = attr_kind;
7689 attr.dw_attr_val.val_class = dw_val_class_file;
7690 attr.dw_attr_val.v.val_file = fd;
7691 add_dwarf_attr (die, &attr);
7694 /* Get the dwarf_file_data from a file DIE attribute. */
7696 static inline struct dwarf_file_data *
7697 AT_file (dw_attr_ref a)
7699 gcc_assert (a && AT_class (a) == dw_val_class_file);
7700 return a->dw_attr_val.v.val_file;
7703 /* Add a vms delta attribute value to a DIE. */
7705 static inline void
7706 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7707 const char *lbl1, const char *lbl2)
7709 dw_attr_node attr;
7711 attr.dw_attr = attr_kind;
7712 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7713 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7714 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7715 add_dwarf_attr (die, &attr);
7718 /* Add a label identifier attribute value to a DIE. */
7720 static inline void
7721 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7723 dw_attr_node attr;
7725 attr.dw_attr = attr_kind;
7726 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7727 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7728 add_dwarf_attr (die, &attr);
7731 /* Add a section offset attribute value to a DIE, an offset into the
7732 debug_line section. */
7734 static inline void
7735 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7736 const char *label)
7738 dw_attr_node attr;
7740 attr.dw_attr = attr_kind;
7741 attr.dw_attr_val.val_class = dw_val_class_lineptr;
7742 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7743 add_dwarf_attr (die, &attr);
7746 /* Add a section offset attribute value to a DIE, an offset into the
7747 debug_macinfo section. */
7749 static inline void
7750 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7751 const char *label)
7753 dw_attr_node attr;
7755 attr.dw_attr = attr_kind;
7756 attr.dw_attr_val.val_class = dw_val_class_macptr;
7757 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7758 add_dwarf_attr (die, &attr);
7761 /* Add an offset attribute value to a DIE. */
7763 static inline void
7764 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7765 unsigned HOST_WIDE_INT offset)
7767 dw_attr_node attr;
7769 attr.dw_attr = attr_kind;
7770 attr.dw_attr_val.val_class = dw_val_class_offset;
7771 attr.dw_attr_val.v.val_offset = offset;
7772 add_dwarf_attr (die, &attr);
7775 /* Add an range_list attribute value to a DIE. */
7777 static void
7778 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7779 long unsigned int offset)
7781 dw_attr_node attr;
7783 attr.dw_attr = attr_kind;
7784 attr.dw_attr_val.val_class = dw_val_class_range_list;
7785 attr.dw_attr_val.v.val_offset = offset;
7786 add_dwarf_attr (die, &attr);
7789 /* Return the start label of a delta attribute. */
7791 static inline const char *
7792 AT_vms_delta1 (dw_attr_ref a)
7794 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7795 return a->dw_attr_val.v.val_vms_delta.lbl1;
7798 /* Return the end label of a delta attribute. */
7800 static inline const char *
7801 AT_vms_delta2 (dw_attr_ref a)
7803 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7804 return a->dw_attr_val.v.val_vms_delta.lbl2;
7807 static inline const char *
7808 AT_lbl (dw_attr_ref a)
7810 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7811 || AT_class (a) == dw_val_class_lineptr
7812 || AT_class (a) == dw_val_class_macptr));
7813 return a->dw_attr_val.v.val_lbl_id;
7816 /* Get the attribute of type attr_kind. */
7818 static dw_attr_ref
7819 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7821 dw_attr_ref a;
7822 unsigned ix;
7823 dw_die_ref spec = NULL;
7825 if (! die)
7826 return NULL;
7828 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7829 if (a->dw_attr == attr_kind)
7830 return a;
7831 else if (a->dw_attr == DW_AT_specification
7832 || a->dw_attr == DW_AT_abstract_origin)
7833 spec = AT_ref (a);
7835 if (spec)
7836 return get_AT (spec, attr_kind);
7838 return NULL;
7841 /* Return the "low pc" attribute value, typically associated with a subprogram
7842 DIE. Return null if the "low pc" attribute is either not present, or if it
7843 cannot be represented as an assembler label identifier. */
7845 static inline const char *
7846 get_AT_low_pc (dw_die_ref die)
7848 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7850 return a ? AT_lbl (a) : NULL;
7853 /* Return the "high pc" attribute value, typically associated with a subprogram
7854 DIE. Return null if the "high pc" attribute is either not present, or if it
7855 cannot be represented as an assembler label identifier. */
7857 static inline const char *
7858 get_AT_hi_pc (dw_die_ref die)
7860 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7862 return a ? AT_lbl (a) : NULL;
7865 /* Return the value of the string attribute designated by ATTR_KIND, or
7866 NULL if it is not present. */
7868 static inline const char *
7869 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7871 dw_attr_ref a = get_AT (die, attr_kind);
7873 return a ? AT_string (a) : NULL;
7876 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7877 if it is not present. */
7879 static inline int
7880 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7882 dw_attr_ref a = get_AT (die, attr_kind);
7884 return a ? AT_flag (a) : 0;
7887 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7888 if it is not present. */
7890 static inline unsigned
7891 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7893 dw_attr_ref a = get_AT (die, attr_kind);
7895 return a ? AT_unsigned (a) : 0;
7898 static inline dw_die_ref
7899 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7901 dw_attr_ref a = get_AT (die, attr_kind);
7903 return a ? AT_ref (a) : NULL;
7906 static inline struct dwarf_file_data *
7907 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7909 dw_attr_ref a = get_AT (die, attr_kind);
7911 return a ? AT_file (a) : NULL;
7914 /* Return TRUE if the language is C++. */
7916 static inline bool
7917 is_cxx (void)
7919 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7921 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7924 /* Return TRUE if the language is Fortran. */
7926 static inline bool
7927 is_fortran (void)
7929 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7931 return (lang == DW_LANG_Fortran77
7932 || lang == DW_LANG_Fortran90
7933 || lang == DW_LANG_Fortran95);
7936 /* Return TRUE if the language is Ada. */
7938 static inline bool
7939 is_ada (void)
7941 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7943 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7946 /* Remove the specified attribute if present. */
7948 static void
7949 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7951 dw_attr_ref a;
7952 unsigned ix;
7954 if (! die)
7955 return;
7957 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7958 if (a->dw_attr == attr_kind)
7960 if (AT_class (a) == dw_val_class_str)
7961 if (a->dw_attr_val.v.val_str->refcount)
7962 a->dw_attr_val.v.val_str->refcount--;
7964 /* VEC_ordered_remove should help reduce the number of abbrevs
7965 that are needed. */
7966 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7967 return;
7971 /* Remove CHILD from its parent. PREV must have the property that
7972 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
7974 static void
7975 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7977 gcc_assert (child->die_parent == prev->die_parent);
7978 gcc_assert (prev->die_sib == child);
7979 if (prev == child)
7981 gcc_assert (child->die_parent->die_child == child);
7982 prev = NULL;
7984 else
7985 prev->die_sib = child->die_sib;
7986 if (child->die_parent->die_child == child)
7987 child->die_parent->die_child = prev;
7990 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
7991 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
7993 static void
7994 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7996 dw_die_ref parent = old_child->die_parent;
7998 gcc_assert (parent == prev->die_parent);
7999 gcc_assert (prev->die_sib == old_child);
8001 new_child->die_parent = parent;
8002 if (prev == old_child)
8004 gcc_assert (parent->die_child == old_child);
8005 new_child->die_sib = new_child;
8007 else
8009 prev->die_sib = new_child;
8010 new_child->die_sib = old_child->die_sib;
8012 if (old_child->die_parent->die_child == old_child)
8013 old_child->die_parent->die_child = new_child;
8016 /* Move all children from OLD_PARENT to NEW_PARENT. */
8018 static void
8019 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8021 dw_die_ref c;
8022 new_parent->die_child = old_parent->die_child;
8023 old_parent->die_child = NULL;
8024 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8027 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
8028 matches TAG. */
8030 static void
8031 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8033 dw_die_ref c;
8035 c = die->die_child;
8036 if (c) do {
8037 dw_die_ref prev = c;
8038 c = c->die_sib;
8039 while (c->die_tag == tag)
8041 remove_child_with_prev (c, prev);
8042 /* Might have removed every child. */
8043 if (c == c->die_sib)
8044 return;
8045 c = c->die_sib;
8047 } while (c != die->die_child);
8050 /* Add a CHILD_DIE as the last child of DIE. */
8052 static void
8053 add_child_die (dw_die_ref die, dw_die_ref child_die)
8055 /* FIXME this should probably be an assert. */
8056 if (! die || ! child_die)
8057 return;
8058 gcc_assert (die != child_die);
8060 child_die->die_parent = die;
8061 if (die->die_child)
8063 child_die->die_sib = die->die_child->die_sib;
8064 die->die_child->die_sib = child_die;
8066 else
8067 child_die->die_sib = child_die;
8068 die->die_child = child_die;
8071 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8072 is the specification, to the end of PARENT's list of children.
8073 This is done by removing and re-adding it. */
8075 static void
8076 splice_child_die (dw_die_ref parent, dw_die_ref child)
8078 dw_die_ref p;
8080 /* We want the declaration DIE from inside the class, not the
8081 specification DIE at toplevel. */
8082 if (child->die_parent != parent)
8084 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8086 if (tmp)
8087 child = tmp;
8090 gcc_assert (child->die_parent == parent
8091 || (child->die_parent
8092 == get_AT_ref (parent, DW_AT_specification)));
8094 for (p = child->die_parent->die_child; ; p = p->die_sib)
8095 if (p->die_sib == child)
8097 remove_child_with_prev (child, p);
8098 break;
8101 add_child_die (parent, child);
8104 /* Return a pointer to a newly created DIE node. */
8106 static inline dw_die_ref
8107 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8109 dw_die_ref die = ggc_alloc_cleared_die_node ();
8111 die->die_tag = tag_value;
8113 if (parent_die != NULL)
8114 add_child_die (parent_die, die);
8115 else
8117 limbo_die_node *limbo_node;
8119 limbo_node = ggc_alloc_cleared_limbo_die_node ();
8120 limbo_node->die = die;
8121 limbo_node->created_for = t;
8122 limbo_node->next = limbo_die_list;
8123 limbo_die_list = limbo_node;
8126 return die;
8129 /* Return the DIE associated with the given type specifier. */
8131 static inline dw_die_ref
8132 lookup_type_die (tree type)
8134 return TYPE_SYMTAB_DIE (type);
8137 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8138 anonymous type named by the typedef TYPE_DIE, return the DIE of the
8139 anonymous type instead the one of the naming typedef. */
8141 static inline dw_die_ref
8142 strip_naming_typedef (tree type, dw_die_ref type_die)
8144 if (type
8145 && TREE_CODE (type) == RECORD_TYPE
8146 && type_die
8147 && type_die->die_tag == DW_TAG_typedef
8148 && is_naming_typedef_decl (TYPE_NAME (type)))
8149 type_die = get_AT_ref (type_die, DW_AT_type);
8150 return type_die;
8153 /* Like lookup_type_die, but if type is an anonymous type named by a
8154 typedef[1], return the DIE of the anonymous type instead the one of
8155 the naming typedef. This is because in gen_typedef_die, we did
8156 equate the anonymous struct named by the typedef with the DIE of
8157 the naming typedef. So by default, lookup_type_die on an anonymous
8158 struct yields the DIE of the naming typedef.
8160 [1]: Read the comment of is_naming_typedef_decl to learn about what
8161 a naming typedef is. */
8163 static inline dw_die_ref
8164 lookup_type_die_strip_naming_typedef (tree type)
8166 dw_die_ref die = lookup_type_die (type);
8167 return strip_naming_typedef (type, die);
8170 /* Equate a DIE to a given type specifier. */
8172 static inline void
8173 equate_type_number_to_die (tree type, dw_die_ref type_die)
8175 TYPE_SYMTAB_DIE (type) = type_die;
8178 /* Returns a hash value for X (which really is a die_struct). */
8180 static hashval_t
8181 decl_die_table_hash (const void *x)
8183 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8186 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
8188 static int
8189 decl_die_table_eq (const void *x, const void *y)
8191 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8194 /* Return the DIE associated with a given declaration. */
8196 static inline dw_die_ref
8197 lookup_decl_die (tree decl)
8199 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8202 /* Returns a hash value for X (which really is a var_loc_list). */
8204 static hashval_t
8205 decl_loc_table_hash (const void *x)
8207 return (hashval_t) ((const var_loc_list *) x)->decl_id;
8210 /* Return nonzero if decl_id of var_loc_list X is the same as
8211 UID of decl *Y. */
8213 static int
8214 decl_loc_table_eq (const void *x, const void *y)
8216 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8219 /* Return the var_loc list associated with a given declaration. */
8221 static inline var_loc_list *
8222 lookup_decl_loc (const_tree decl)
8224 if (!decl_loc_table)
8225 return NULL;
8226 return (var_loc_list *)
8227 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8230 /* Equate a DIE to a particular declaration. */
8232 static void
8233 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8235 unsigned int decl_id = DECL_UID (decl);
8236 void **slot;
8238 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8239 *slot = decl_die;
8240 decl_die->decl_id = decl_id;
8243 /* Return how many bits covers PIECE EXPR_LIST. */
8245 static int
8246 decl_piece_bitsize (rtx piece)
8248 int ret = (int) GET_MODE (piece);
8249 if (ret)
8250 return ret;
8251 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8252 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8253 return INTVAL (XEXP (XEXP (piece, 0), 0));
8256 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
8258 static rtx *
8259 decl_piece_varloc_ptr (rtx piece)
8261 if ((int) GET_MODE (piece))
8262 return &XEXP (piece, 0);
8263 else
8264 return &XEXP (XEXP (piece, 0), 1);
8267 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8268 Next is the chain of following piece nodes. */
8270 static rtx
8271 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8273 if (bitsize <= (int) MAX_MACHINE_MODE)
8274 return alloc_EXPR_LIST (bitsize, loc_note, next);
8275 else
8276 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8277 GEN_INT (bitsize),
8278 loc_note), next);
8281 /* Return rtx that should be stored into loc field for
8282 LOC_NOTE and BITPOS/BITSIZE. */
8284 static rtx
8285 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8286 HOST_WIDE_INT bitsize)
8288 if (bitsize != -1)
8290 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8291 if (bitpos != 0)
8292 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8294 return loc_note;
8297 /* This function either modifies location piece list *DEST in
8298 place (if SRC and INNER is NULL), or copies location piece list
8299 *SRC to *DEST while modifying it. Location BITPOS is modified
8300 to contain LOC_NOTE, any pieces overlapping it are removed resp.
8301 not copied and if needed some padding around it is added.
8302 When modifying in place, DEST should point to EXPR_LIST where
8303 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8304 to the start of the whole list and INNER points to the EXPR_LIST
8305 where earlier pieces cover PIECE_BITPOS bits. */
8307 static void
8308 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8309 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8310 HOST_WIDE_INT bitsize, rtx loc_note)
8312 int diff;
8313 bool copy = inner != NULL;
8315 if (copy)
8317 /* First copy all nodes preceeding the current bitpos. */
8318 while (src != inner)
8320 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8321 decl_piece_bitsize (*src), NULL_RTX);
8322 dest = &XEXP (*dest, 1);
8323 src = &XEXP (*src, 1);
8326 /* Add padding if needed. */
8327 if (bitpos != piece_bitpos)
8329 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8330 copy ? NULL_RTX : *dest);
8331 dest = &XEXP (*dest, 1);
8333 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8335 gcc_assert (!copy);
8336 /* A piece with correct bitpos and bitsize already exist,
8337 just update the location for it and return. */
8338 *decl_piece_varloc_ptr (*dest) = loc_note;
8339 return;
8341 /* Add the piece that changed. */
8342 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8343 dest = &XEXP (*dest, 1);
8344 /* Skip over pieces that overlap it. */
8345 diff = bitpos - piece_bitpos + bitsize;
8346 if (!copy)
8347 src = dest;
8348 while (diff > 0 && *src)
8350 rtx piece = *src;
8351 diff -= decl_piece_bitsize (piece);
8352 if (copy)
8353 src = &XEXP (piece, 1);
8354 else
8356 *src = XEXP (piece, 1);
8357 free_EXPR_LIST_node (piece);
8360 /* Add padding if needed. */
8361 if (diff < 0 && *src)
8363 if (!copy)
8364 dest = src;
8365 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8366 dest = &XEXP (*dest, 1);
8368 if (!copy)
8369 return;
8370 /* Finally copy all nodes following it. */
8371 while (*src)
8373 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8374 decl_piece_bitsize (*src), NULL_RTX);
8375 dest = &XEXP (*dest, 1);
8376 src = &XEXP (*src, 1);
8380 /* Add a variable location node to the linked list for DECL. */
8382 static struct var_loc_node *
8383 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8385 unsigned int decl_id;
8386 var_loc_list *temp;
8387 void **slot;
8388 struct var_loc_node *loc = NULL;
8389 HOST_WIDE_INT bitsize = -1, bitpos = -1;
8391 if (DECL_DEBUG_EXPR_IS_FROM (decl))
8393 tree realdecl = DECL_DEBUG_EXPR (decl);
8394 if (realdecl && handled_component_p (realdecl))
8396 HOST_WIDE_INT maxsize;
8397 tree innerdecl;
8398 innerdecl
8399 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8400 if (!DECL_P (innerdecl)
8401 || DECL_IGNORED_P (innerdecl)
8402 || TREE_STATIC (innerdecl)
8403 || bitsize <= 0
8404 || bitpos + bitsize > 256
8405 || bitsize != maxsize)
8406 return NULL;
8407 decl = innerdecl;
8411 decl_id = DECL_UID (decl);
8412 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8413 if (*slot == NULL)
8415 temp = ggc_alloc_cleared_var_loc_list ();
8416 temp->decl_id = decl_id;
8417 *slot = temp;
8419 else
8420 temp = (var_loc_list *) *slot;
8422 if (temp->last)
8424 struct var_loc_node *last = temp->last, *unused = NULL;
8425 rtx *piece_loc = NULL, last_loc_note;
8426 int piece_bitpos = 0;
8427 if (last->next)
8429 last = last->next;
8430 gcc_assert (last->next == NULL);
8432 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8434 piece_loc = &last->loc;
8437 int cur_bitsize = decl_piece_bitsize (*piece_loc);
8438 if (piece_bitpos + cur_bitsize > bitpos)
8439 break;
8440 piece_bitpos += cur_bitsize;
8441 piece_loc = &XEXP (*piece_loc, 1);
8443 while (*piece_loc);
8445 /* TEMP->LAST here is either pointer to the last but one or
8446 last element in the chained list, LAST is pointer to the
8447 last element. */
8448 if (label && strcmp (last->label, label) == 0)
8450 /* For SRA optimized variables if there weren't any real
8451 insns since last note, just modify the last node. */
8452 if (piece_loc != NULL)
8454 adjust_piece_list (piece_loc, NULL, NULL,
8455 bitpos, piece_bitpos, bitsize, loc_note);
8456 return NULL;
8458 /* If the last note doesn't cover any instructions, remove it. */
8459 if (temp->last != last)
8461 temp->last->next = NULL;
8462 unused = last;
8463 last = temp->last;
8464 gcc_assert (strcmp (last->label, label) != 0);
8466 else
8468 gcc_assert (temp->first == temp->last);
8469 memset (temp->last, '\0', sizeof (*temp->last));
8470 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8471 return temp->last;
8474 if (bitsize == -1 && NOTE_P (last->loc))
8475 last_loc_note = last->loc;
8476 else if (piece_loc != NULL
8477 && *piece_loc != NULL_RTX
8478 && piece_bitpos == bitpos
8479 && decl_piece_bitsize (*piece_loc) == bitsize)
8480 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8481 else
8482 last_loc_note = NULL_RTX;
8483 /* If the current location is the same as the end of the list,
8484 and either both or neither of the locations is uninitialized,
8485 we have nothing to do. */
8486 if (last_loc_note == NULL_RTX
8487 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8488 NOTE_VAR_LOCATION_LOC (loc_note)))
8489 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8490 != NOTE_VAR_LOCATION_STATUS (loc_note))
8491 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8492 == VAR_INIT_STATUS_UNINITIALIZED)
8493 || (NOTE_VAR_LOCATION_STATUS (loc_note)
8494 == VAR_INIT_STATUS_UNINITIALIZED))))
8496 /* Add LOC to the end of list and update LAST. If the last
8497 element of the list has been removed above, reuse its
8498 memory for the new node, otherwise allocate a new one. */
8499 if (unused)
8501 loc = unused;
8502 memset (loc, '\0', sizeof (*loc));
8504 else
8505 loc = ggc_alloc_cleared_var_loc_node ();
8506 if (bitsize == -1 || piece_loc == NULL)
8507 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8508 else
8509 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8510 bitpos, piece_bitpos, bitsize, loc_note);
8511 last->next = loc;
8512 /* Ensure TEMP->LAST will point either to the new last but one
8513 element of the chain, or to the last element in it. */
8514 if (last != temp->last)
8515 temp->last = last;
8517 else if (unused)
8518 ggc_free (unused);
8520 else
8522 loc = ggc_alloc_cleared_var_loc_node ();
8523 temp->first = loc;
8524 temp->last = loc;
8525 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8527 return loc;
8530 /* Keep track of the number of spaces used to indent the
8531 output of the debugging routines that print the structure of
8532 the DIE internal representation. */
8533 static int print_indent;
8535 /* Indent the line the number of spaces given by print_indent. */
8537 static inline void
8538 print_spaces (FILE *outfile)
8540 fprintf (outfile, "%*s", print_indent, "");
8543 /* Print a type signature in hex. */
8545 static inline void
8546 print_signature (FILE *outfile, char *sig)
8548 int i;
8550 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8551 fprintf (outfile, "%02x", sig[i] & 0xff);
8554 /* Print the information associated with a given DIE, and its children.
8555 This routine is a debugging aid only. */
8557 static void
8558 print_die (dw_die_ref die, FILE *outfile)
8560 dw_attr_ref a;
8561 dw_die_ref c;
8562 unsigned ix;
8564 print_spaces (outfile);
8565 fprintf (outfile, "DIE %4ld: %s (%p)\n",
8566 die->die_offset, dwarf_tag_name (die->die_tag),
8567 (void*) die);
8568 print_spaces (outfile);
8569 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
8570 fprintf (outfile, " offset: %ld", die->die_offset);
8571 fprintf (outfile, " mark: %d\n", die->die_mark);
8573 if (dwarf_version >= 4 && die->die_id.die_type_node)
8575 print_spaces (outfile);
8576 fprintf (outfile, " signature: ");
8577 print_signature (outfile, die->die_id.die_type_node->signature);
8578 fprintf (outfile, "\n");
8581 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8583 print_spaces (outfile);
8584 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
8586 switch (AT_class (a))
8588 case dw_val_class_addr:
8589 fprintf (outfile, "address");
8590 break;
8591 case dw_val_class_offset:
8592 fprintf (outfile, "offset");
8593 break;
8594 case dw_val_class_loc:
8595 fprintf (outfile, "location descriptor");
8596 break;
8597 case dw_val_class_loc_list:
8598 fprintf (outfile, "location list -> label:%s",
8599 AT_loc_list (a)->ll_symbol);
8600 break;
8601 case dw_val_class_range_list:
8602 fprintf (outfile, "range list");
8603 break;
8604 case dw_val_class_const:
8605 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8606 break;
8607 case dw_val_class_unsigned_const:
8608 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8609 break;
8610 case dw_val_class_const_double:
8611 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8612 HOST_WIDE_INT_PRINT_UNSIGNED")",
8613 a->dw_attr_val.v.val_double.high,
8614 a->dw_attr_val.v.val_double.low);
8615 break;
8616 case dw_val_class_vec:
8617 fprintf (outfile, "floating-point or vector constant");
8618 break;
8619 case dw_val_class_flag:
8620 fprintf (outfile, "%u", AT_flag (a));
8621 break;
8622 case dw_val_class_die_ref:
8623 if (AT_ref (a) != NULL)
8625 if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8627 fprintf (outfile, "die -> signature: ");
8628 print_signature (outfile,
8629 AT_ref (a)->die_id.die_type_node->signature);
8631 else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8632 fprintf (outfile, "die -> label: %s",
8633 AT_ref (a)->die_id.die_symbol);
8634 else
8635 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8636 fprintf (outfile, " (%p)", (void *) AT_ref (a));
8638 else
8639 fprintf (outfile, "die -> <null>");
8640 break;
8641 case dw_val_class_vms_delta:
8642 fprintf (outfile, "delta: @slotcount(%s-%s)",
8643 AT_vms_delta2 (a), AT_vms_delta1 (a));
8644 break;
8645 case dw_val_class_lbl_id:
8646 case dw_val_class_lineptr:
8647 case dw_val_class_macptr:
8648 fprintf (outfile, "label: %s", AT_lbl (a));
8649 break;
8650 case dw_val_class_str:
8651 if (AT_string (a) != NULL)
8652 fprintf (outfile, "\"%s\"", AT_string (a));
8653 else
8654 fprintf (outfile, "<null>");
8655 break;
8656 case dw_val_class_file:
8657 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8658 AT_file (a)->emitted_number);
8659 break;
8660 case dw_val_class_data8:
8662 int i;
8664 for (i = 0; i < 8; i++)
8665 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8666 break;
8668 default:
8669 break;
8672 fprintf (outfile, "\n");
8675 if (die->die_child != NULL)
8677 print_indent += 4;
8678 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8679 print_indent -= 4;
8681 if (print_indent == 0)
8682 fprintf (outfile, "\n");
8685 /* Print the contents of the source code line number correspondence table.
8686 This routine is a debugging aid only. */
8688 static void
8689 print_dwarf_line_table (FILE *outfile)
8691 unsigned i;
8692 dw_line_info_ref line_info;
8694 fprintf (outfile, "\n\nDWARF source line information\n");
8695 for (i = 1; i < line_info_table_in_use; i++)
8697 line_info = &line_info_table[i];
8698 fprintf (outfile, "%5d: %4ld %6ld\n", i,
8699 line_info->dw_file_num,
8700 line_info->dw_line_num);
8703 fprintf (outfile, "\n\n");
8706 /* Print the information collected for a given DIE. */
8708 DEBUG_FUNCTION void
8709 debug_dwarf_die (dw_die_ref die)
8711 print_die (die, stderr);
8714 /* Print all DWARF information collected for the compilation unit.
8715 This routine is a debugging aid only. */
8717 DEBUG_FUNCTION void
8718 debug_dwarf (void)
8720 print_indent = 0;
8721 print_die (comp_unit_die (), stderr);
8722 if (! DWARF2_ASM_LINE_DEBUG_INFO)
8723 print_dwarf_line_table (stderr);
8726 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
8727 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
8728 DIE that marks the start of the DIEs for this include file. */
8730 static dw_die_ref
8731 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8733 const char *filename = get_AT_string (bincl_die, DW_AT_name);
8734 dw_die_ref new_unit = gen_compile_unit_die (filename);
8736 new_unit->die_sib = old_unit;
8737 return new_unit;
8740 /* Close an include-file CU and reopen the enclosing one. */
8742 static dw_die_ref
8743 pop_compile_unit (dw_die_ref old_unit)
8745 dw_die_ref new_unit = old_unit->die_sib;
8747 old_unit->die_sib = NULL;
8748 return new_unit;
8751 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8752 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8754 /* Calculate the checksum of a location expression. */
8756 static inline void
8757 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8759 int tem;
8761 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8762 CHECKSUM (tem);
8763 CHECKSUM (loc->dw_loc_oprnd1);
8764 CHECKSUM (loc->dw_loc_oprnd2);
8767 /* Calculate the checksum of an attribute. */
8769 static void
8770 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8772 dw_loc_descr_ref loc;
8773 rtx r;
8775 CHECKSUM (at->dw_attr);
8777 /* We don't care that this was compiled with a different compiler
8778 snapshot; if the output is the same, that's what matters. */
8779 if (at->dw_attr == DW_AT_producer)
8780 return;
8782 switch (AT_class (at))
8784 case dw_val_class_const:
8785 CHECKSUM (at->dw_attr_val.v.val_int);
8786 break;
8787 case dw_val_class_unsigned_const:
8788 CHECKSUM (at->dw_attr_val.v.val_unsigned);
8789 break;
8790 case dw_val_class_const_double:
8791 CHECKSUM (at->dw_attr_val.v.val_double);
8792 break;
8793 case dw_val_class_vec:
8794 CHECKSUM (at->dw_attr_val.v.val_vec);
8795 break;
8796 case dw_val_class_flag:
8797 CHECKSUM (at->dw_attr_val.v.val_flag);
8798 break;
8799 case dw_val_class_str:
8800 CHECKSUM_STRING (AT_string (at));
8801 break;
8803 case dw_val_class_addr:
8804 r = AT_addr (at);
8805 gcc_assert (GET_CODE (r) == SYMBOL_REF);
8806 CHECKSUM_STRING (XSTR (r, 0));
8807 break;
8809 case dw_val_class_offset:
8810 CHECKSUM (at->dw_attr_val.v.val_offset);
8811 break;
8813 case dw_val_class_loc:
8814 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8815 loc_checksum (loc, ctx);
8816 break;
8818 case dw_val_class_die_ref:
8819 die_checksum (AT_ref (at), ctx, mark);
8820 break;
8822 case dw_val_class_fde_ref:
8823 case dw_val_class_vms_delta:
8824 case dw_val_class_lbl_id:
8825 case dw_val_class_lineptr:
8826 case dw_val_class_macptr:
8827 break;
8829 case dw_val_class_file:
8830 CHECKSUM_STRING (AT_file (at)->filename);
8831 break;
8833 case dw_val_class_data8:
8834 CHECKSUM (at->dw_attr_val.v.val_data8);
8835 break;
8837 default:
8838 break;
8842 /* Calculate the checksum of a DIE. */
8844 static void
8845 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8847 dw_die_ref c;
8848 dw_attr_ref a;
8849 unsigned ix;
8851 /* To avoid infinite recursion. */
8852 if (die->die_mark)
8854 CHECKSUM (die->die_mark);
8855 return;
8857 die->die_mark = ++(*mark);
8859 CHECKSUM (die->die_tag);
8861 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8862 attr_checksum (a, ctx, mark);
8864 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8867 #undef CHECKSUM
8868 #undef CHECKSUM_STRING
8870 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
8871 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8872 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8873 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8874 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8875 #define CHECKSUM_ATTR(FOO) \
8876 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8878 /* Calculate the checksum of a number in signed LEB128 format. */
8880 static void
8881 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8883 unsigned char byte;
8884 bool more;
8886 while (1)
8888 byte = (value & 0x7f);
8889 value >>= 7;
8890 more = !((value == 0 && (byte & 0x40) == 0)
8891 || (value == -1 && (byte & 0x40) != 0));
8892 if (more)
8893 byte |= 0x80;
8894 CHECKSUM (byte);
8895 if (!more)
8896 break;
8900 /* Calculate the checksum of a number in unsigned LEB128 format. */
8902 static void
8903 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8905 while (1)
8907 unsigned char byte = (value & 0x7f);
8908 value >>= 7;
8909 if (value != 0)
8910 /* More bytes to follow. */
8911 byte |= 0x80;
8912 CHECKSUM (byte);
8913 if (value == 0)
8914 break;
8918 /* Checksum the context of the DIE. This adds the names of any
8919 surrounding namespaces or structures to the checksum. */
8921 static void
8922 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8924 const char *name;
8925 dw_die_ref spec;
8926 int tag = die->die_tag;
8928 if (tag != DW_TAG_namespace
8929 && tag != DW_TAG_structure_type
8930 && tag != DW_TAG_class_type)
8931 return;
8933 name = get_AT_string (die, DW_AT_name);
8935 spec = get_AT_ref (die, DW_AT_specification);
8936 if (spec != NULL)
8937 die = spec;
8939 if (die->die_parent != NULL)
8940 checksum_die_context (die->die_parent, ctx);
8942 CHECKSUM_ULEB128 ('C');
8943 CHECKSUM_ULEB128 (tag);
8944 if (name != NULL)
8945 CHECKSUM_STRING (name);
8948 /* Calculate the checksum of a location expression. */
8950 static inline void
8951 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8953 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8954 were emitted as a DW_FORM_sdata instead of a location expression. */
8955 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8957 CHECKSUM_ULEB128 (DW_FORM_sdata);
8958 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8959 return;
8962 /* Otherwise, just checksum the raw location expression. */
8963 while (loc != NULL)
8965 CHECKSUM_ULEB128 (loc->dw_loc_opc);
8966 CHECKSUM (loc->dw_loc_oprnd1);
8967 CHECKSUM (loc->dw_loc_oprnd2);
8968 loc = loc->dw_loc_next;
8972 /* Calculate the checksum of an attribute. */
8974 static void
8975 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8976 struct md5_ctx *ctx, int *mark)
8978 dw_loc_descr_ref loc;
8979 rtx r;
8981 if (AT_class (at) == dw_val_class_die_ref)
8983 dw_die_ref target_die = AT_ref (at);
8985 /* For pointer and reference types, we checksum only the (qualified)
8986 name of the target type (if there is a name). For friend entries,
8987 we checksum only the (qualified) name of the target type or function.
8988 This allows the checksum to remain the same whether the target type
8989 is complete or not. */
8990 if ((at->dw_attr == DW_AT_type
8991 && (tag == DW_TAG_pointer_type
8992 || tag == DW_TAG_reference_type
8993 || tag == DW_TAG_rvalue_reference_type
8994 || tag == DW_TAG_ptr_to_member_type))
8995 || (at->dw_attr == DW_AT_friend
8996 && tag == DW_TAG_friend))
8998 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9000 if (name_attr != NULL)
9002 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9004 if (decl == NULL)
9005 decl = target_die;
9006 CHECKSUM_ULEB128 ('N');
9007 CHECKSUM_ULEB128 (at->dw_attr);
9008 if (decl->die_parent != NULL)
9009 checksum_die_context (decl->die_parent, ctx);
9010 CHECKSUM_ULEB128 ('E');
9011 CHECKSUM_STRING (AT_string (name_attr));
9012 return;
9016 /* For all other references to another DIE, we check to see if the
9017 target DIE has already been visited. If it has, we emit a
9018 backward reference; if not, we descend recursively. */
9019 if (target_die->die_mark > 0)
9021 CHECKSUM_ULEB128 ('R');
9022 CHECKSUM_ULEB128 (at->dw_attr);
9023 CHECKSUM_ULEB128 (target_die->die_mark);
9025 else
9027 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9029 if (decl == NULL)
9030 decl = target_die;
9031 target_die->die_mark = ++(*mark);
9032 CHECKSUM_ULEB128 ('T');
9033 CHECKSUM_ULEB128 (at->dw_attr);
9034 if (decl->die_parent != NULL)
9035 checksum_die_context (decl->die_parent, ctx);
9036 die_checksum_ordered (target_die, ctx, mark);
9038 return;
9041 CHECKSUM_ULEB128 ('A');
9042 CHECKSUM_ULEB128 (at->dw_attr);
9044 switch (AT_class (at))
9046 case dw_val_class_const:
9047 CHECKSUM_ULEB128 (DW_FORM_sdata);
9048 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9049 break;
9051 case dw_val_class_unsigned_const:
9052 CHECKSUM_ULEB128 (DW_FORM_sdata);
9053 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9054 break;
9056 case dw_val_class_const_double:
9057 CHECKSUM_ULEB128 (DW_FORM_block);
9058 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9059 CHECKSUM (at->dw_attr_val.v.val_double);
9060 break;
9062 case dw_val_class_vec:
9063 CHECKSUM_ULEB128 (DW_FORM_block);
9064 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9065 CHECKSUM (at->dw_attr_val.v.val_vec);
9066 break;
9068 case dw_val_class_flag:
9069 CHECKSUM_ULEB128 (DW_FORM_flag);
9070 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9071 break;
9073 case dw_val_class_str:
9074 CHECKSUM_ULEB128 (DW_FORM_string);
9075 CHECKSUM_STRING (AT_string (at));
9076 break;
9078 case dw_val_class_addr:
9079 r = AT_addr (at);
9080 gcc_assert (GET_CODE (r) == SYMBOL_REF);
9081 CHECKSUM_ULEB128 (DW_FORM_string);
9082 CHECKSUM_STRING (XSTR (r, 0));
9083 break;
9085 case dw_val_class_offset:
9086 CHECKSUM_ULEB128 (DW_FORM_sdata);
9087 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9088 break;
9090 case dw_val_class_loc:
9091 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9092 loc_checksum_ordered (loc, ctx);
9093 break;
9095 case dw_val_class_fde_ref:
9096 case dw_val_class_lbl_id:
9097 case dw_val_class_lineptr:
9098 case dw_val_class_macptr:
9099 break;
9101 case dw_val_class_file:
9102 CHECKSUM_ULEB128 (DW_FORM_string);
9103 CHECKSUM_STRING (AT_file (at)->filename);
9104 break;
9106 case dw_val_class_data8:
9107 CHECKSUM (at->dw_attr_val.v.val_data8);
9108 break;
9110 default:
9111 break;
9115 struct checksum_attributes
9117 dw_attr_ref at_name;
9118 dw_attr_ref at_type;
9119 dw_attr_ref at_friend;
9120 dw_attr_ref at_accessibility;
9121 dw_attr_ref at_address_class;
9122 dw_attr_ref at_allocated;
9123 dw_attr_ref at_artificial;
9124 dw_attr_ref at_associated;
9125 dw_attr_ref at_binary_scale;
9126 dw_attr_ref at_bit_offset;
9127 dw_attr_ref at_bit_size;
9128 dw_attr_ref at_bit_stride;
9129 dw_attr_ref at_byte_size;
9130 dw_attr_ref at_byte_stride;
9131 dw_attr_ref at_const_value;
9132 dw_attr_ref at_containing_type;
9133 dw_attr_ref at_count;
9134 dw_attr_ref at_data_location;
9135 dw_attr_ref at_data_member_location;
9136 dw_attr_ref at_decimal_scale;
9137 dw_attr_ref at_decimal_sign;
9138 dw_attr_ref at_default_value;
9139 dw_attr_ref at_digit_count;
9140 dw_attr_ref at_discr;
9141 dw_attr_ref at_discr_list;
9142 dw_attr_ref at_discr_value;
9143 dw_attr_ref at_encoding;
9144 dw_attr_ref at_endianity;
9145 dw_attr_ref at_explicit;
9146 dw_attr_ref at_is_optional;
9147 dw_attr_ref at_location;
9148 dw_attr_ref at_lower_bound;
9149 dw_attr_ref at_mutable;
9150 dw_attr_ref at_ordering;
9151 dw_attr_ref at_picture_string;
9152 dw_attr_ref at_prototyped;
9153 dw_attr_ref at_small;
9154 dw_attr_ref at_segment;
9155 dw_attr_ref at_string_length;
9156 dw_attr_ref at_threads_scaled;
9157 dw_attr_ref at_upper_bound;
9158 dw_attr_ref at_use_location;
9159 dw_attr_ref at_use_UTF8;
9160 dw_attr_ref at_variable_parameter;
9161 dw_attr_ref at_virtuality;
9162 dw_attr_ref at_visibility;
9163 dw_attr_ref at_vtable_elem_location;
9166 /* Collect the attributes that we will want to use for the checksum. */
9168 static void
9169 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9171 dw_attr_ref a;
9172 unsigned ix;
9174 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9176 switch (a->dw_attr)
9178 case DW_AT_name:
9179 attrs->at_name = a;
9180 break;
9181 case DW_AT_type:
9182 attrs->at_type = a;
9183 break;
9184 case DW_AT_friend:
9185 attrs->at_friend = a;
9186 break;
9187 case DW_AT_accessibility:
9188 attrs->at_accessibility = a;
9189 break;
9190 case DW_AT_address_class:
9191 attrs->at_address_class = a;
9192 break;
9193 case DW_AT_allocated:
9194 attrs->at_allocated = a;
9195 break;
9196 case DW_AT_artificial:
9197 attrs->at_artificial = a;
9198 break;
9199 case DW_AT_associated:
9200 attrs->at_associated = a;
9201 break;
9202 case DW_AT_binary_scale:
9203 attrs->at_binary_scale = a;
9204 break;
9205 case DW_AT_bit_offset:
9206 attrs->at_bit_offset = a;
9207 break;
9208 case DW_AT_bit_size:
9209 attrs->at_bit_size = a;
9210 break;
9211 case DW_AT_bit_stride:
9212 attrs->at_bit_stride = a;
9213 break;
9214 case DW_AT_byte_size:
9215 attrs->at_byte_size = a;
9216 break;
9217 case DW_AT_byte_stride:
9218 attrs->at_byte_stride = a;
9219 break;
9220 case DW_AT_const_value:
9221 attrs->at_const_value = a;
9222 break;
9223 case DW_AT_containing_type:
9224 attrs->at_containing_type = a;
9225 break;
9226 case DW_AT_count:
9227 attrs->at_count = a;
9228 break;
9229 case DW_AT_data_location:
9230 attrs->at_data_location = a;
9231 break;
9232 case DW_AT_data_member_location:
9233 attrs->at_data_member_location = a;
9234 break;
9235 case DW_AT_decimal_scale:
9236 attrs->at_decimal_scale = a;
9237 break;
9238 case DW_AT_decimal_sign:
9239 attrs->at_decimal_sign = a;
9240 break;
9241 case DW_AT_default_value:
9242 attrs->at_default_value = a;
9243 break;
9244 case DW_AT_digit_count:
9245 attrs->at_digit_count = a;
9246 break;
9247 case DW_AT_discr:
9248 attrs->at_discr = a;
9249 break;
9250 case DW_AT_discr_list:
9251 attrs->at_discr_list = a;
9252 break;
9253 case DW_AT_discr_value:
9254 attrs->at_discr_value = a;
9255 break;
9256 case DW_AT_encoding:
9257 attrs->at_encoding = a;
9258 break;
9259 case DW_AT_endianity:
9260 attrs->at_endianity = a;
9261 break;
9262 case DW_AT_explicit:
9263 attrs->at_explicit = a;
9264 break;
9265 case DW_AT_is_optional:
9266 attrs->at_is_optional = a;
9267 break;
9268 case DW_AT_location:
9269 attrs->at_location = a;
9270 break;
9271 case DW_AT_lower_bound:
9272 attrs->at_lower_bound = a;
9273 break;
9274 case DW_AT_mutable:
9275 attrs->at_mutable = a;
9276 break;
9277 case DW_AT_ordering:
9278 attrs->at_ordering = a;
9279 break;
9280 case DW_AT_picture_string:
9281 attrs->at_picture_string = a;
9282 break;
9283 case DW_AT_prototyped:
9284 attrs->at_prototyped = a;
9285 break;
9286 case DW_AT_small:
9287 attrs->at_small = a;
9288 break;
9289 case DW_AT_segment:
9290 attrs->at_segment = a;
9291 break;
9292 case DW_AT_string_length:
9293 attrs->at_string_length = a;
9294 break;
9295 case DW_AT_threads_scaled:
9296 attrs->at_threads_scaled = a;
9297 break;
9298 case DW_AT_upper_bound:
9299 attrs->at_upper_bound = a;
9300 break;
9301 case DW_AT_use_location:
9302 attrs->at_use_location = a;
9303 break;
9304 case DW_AT_use_UTF8:
9305 attrs->at_use_UTF8 = a;
9306 break;
9307 case DW_AT_variable_parameter:
9308 attrs->at_variable_parameter = a;
9309 break;
9310 case DW_AT_virtuality:
9311 attrs->at_virtuality = a;
9312 break;
9313 case DW_AT_visibility:
9314 attrs->at_visibility = a;
9315 break;
9316 case DW_AT_vtable_elem_location:
9317 attrs->at_vtable_elem_location = a;
9318 break;
9319 default:
9320 break;
9325 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
9327 static void
9328 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9330 dw_die_ref c;
9331 dw_die_ref decl;
9332 struct checksum_attributes attrs;
9334 CHECKSUM_ULEB128 ('D');
9335 CHECKSUM_ULEB128 (die->die_tag);
9337 memset (&attrs, 0, sizeof (attrs));
9339 decl = get_AT_ref (die, DW_AT_specification);
9340 if (decl != NULL)
9341 collect_checksum_attributes (&attrs, decl);
9342 collect_checksum_attributes (&attrs, die);
9344 CHECKSUM_ATTR (attrs.at_name);
9345 CHECKSUM_ATTR (attrs.at_accessibility);
9346 CHECKSUM_ATTR (attrs.at_address_class);
9347 CHECKSUM_ATTR (attrs.at_allocated);
9348 CHECKSUM_ATTR (attrs.at_artificial);
9349 CHECKSUM_ATTR (attrs.at_associated);
9350 CHECKSUM_ATTR (attrs.at_binary_scale);
9351 CHECKSUM_ATTR (attrs.at_bit_offset);
9352 CHECKSUM_ATTR (attrs.at_bit_size);
9353 CHECKSUM_ATTR (attrs.at_bit_stride);
9354 CHECKSUM_ATTR (attrs.at_byte_size);
9355 CHECKSUM_ATTR (attrs.at_byte_stride);
9356 CHECKSUM_ATTR (attrs.at_const_value);
9357 CHECKSUM_ATTR (attrs.at_containing_type);
9358 CHECKSUM_ATTR (attrs.at_count);
9359 CHECKSUM_ATTR (attrs.at_data_location);
9360 CHECKSUM_ATTR (attrs.at_data_member_location);
9361 CHECKSUM_ATTR (attrs.at_decimal_scale);
9362 CHECKSUM_ATTR (attrs.at_decimal_sign);
9363 CHECKSUM_ATTR (attrs.at_default_value);
9364 CHECKSUM_ATTR (attrs.at_digit_count);
9365 CHECKSUM_ATTR (attrs.at_discr);
9366 CHECKSUM_ATTR (attrs.at_discr_list);
9367 CHECKSUM_ATTR (attrs.at_discr_value);
9368 CHECKSUM_ATTR (attrs.at_encoding);
9369 CHECKSUM_ATTR (attrs.at_endianity);
9370 CHECKSUM_ATTR (attrs.at_explicit);
9371 CHECKSUM_ATTR (attrs.at_is_optional);
9372 CHECKSUM_ATTR (attrs.at_location);
9373 CHECKSUM_ATTR (attrs.at_lower_bound);
9374 CHECKSUM_ATTR (attrs.at_mutable);
9375 CHECKSUM_ATTR (attrs.at_ordering);
9376 CHECKSUM_ATTR (attrs.at_picture_string);
9377 CHECKSUM_ATTR (attrs.at_prototyped);
9378 CHECKSUM_ATTR (attrs.at_small);
9379 CHECKSUM_ATTR (attrs.at_segment);
9380 CHECKSUM_ATTR (attrs.at_string_length);
9381 CHECKSUM_ATTR (attrs.at_threads_scaled);
9382 CHECKSUM_ATTR (attrs.at_upper_bound);
9383 CHECKSUM_ATTR (attrs.at_use_location);
9384 CHECKSUM_ATTR (attrs.at_use_UTF8);
9385 CHECKSUM_ATTR (attrs.at_variable_parameter);
9386 CHECKSUM_ATTR (attrs.at_virtuality);
9387 CHECKSUM_ATTR (attrs.at_visibility);
9388 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9389 CHECKSUM_ATTR (attrs.at_type);
9390 CHECKSUM_ATTR (attrs.at_friend);
9392 /* Checksum the child DIEs, except for nested types and member functions. */
9393 c = die->die_child;
9394 if (c) do {
9395 dw_attr_ref name_attr;
9397 c = c->die_sib;
9398 name_attr = get_AT (c, DW_AT_name);
9399 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9400 && name_attr != NULL)
9402 CHECKSUM_ULEB128 ('S');
9403 CHECKSUM_ULEB128 (c->die_tag);
9404 CHECKSUM_STRING (AT_string (name_attr));
9406 else
9408 /* Mark this DIE so it gets processed when unmarking. */
9409 if (c->die_mark == 0)
9410 c->die_mark = -1;
9411 die_checksum_ordered (c, ctx, mark);
9413 } while (c != die->die_child);
9415 CHECKSUM_ULEB128 (0);
9418 #undef CHECKSUM
9419 #undef CHECKSUM_STRING
9420 #undef CHECKSUM_ATTR
9421 #undef CHECKSUM_LEB128
9422 #undef CHECKSUM_ULEB128
9424 /* Generate the type signature for DIE. This is computed by generating an
9425 MD5 checksum over the DIE's tag, its relevant attributes, and its
9426 children. Attributes that are references to other DIEs are processed
9427 by recursion, using the MARK field to prevent infinite recursion.
9428 If the DIE is nested inside a namespace or another type, we also
9429 need to include that context in the signature. The lower 64 bits
9430 of the resulting MD5 checksum comprise the signature. */
9432 static void
9433 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9435 int mark;
9436 const char *name;
9437 unsigned char checksum[16];
9438 struct md5_ctx ctx;
9439 dw_die_ref decl;
9441 name = get_AT_string (die, DW_AT_name);
9442 decl = get_AT_ref (die, DW_AT_specification);
9444 /* First, compute a signature for just the type name (and its surrounding
9445 context, if any. This is stored in the type unit DIE for link-time
9446 ODR (one-definition rule) checking. */
9448 if (is_cxx() && name != NULL)
9450 md5_init_ctx (&ctx);
9452 /* Checksum the names of surrounding namespaces and structures. */
9453 if (decl != NULL && decl->die_parent != NULL)
9454 checksum_die_context (decl->die_parent, &ctx);
9456 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9457 md5_process_bytes (name, strlen (name) + 1, &ctx);
9458 md5_finish_ctx (&ctx, checksum);
9460 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9463 /* Next, compute the complete type signature. */
9465 md5_init_ctx (&ctx);
9466 mark = 1;
9467 die->die_mark = mark;
9469 /* Checksum the names of surrounding namespaces and structures. */
9470 if (decl != NULL && decl->die_parent != NULL)
9471 checksum_die_context (decl->die_parent, &ctx);
9473 /* Checksum the DIE and its children. */
9474 die_checksum_ordered (die, &ctx, &mark);
9475 unmark_all_dies (die);
9476 md5_finish_ctx (&ctx, checksum);
9478 /* Store the signature in the type node and link the type DIE and the
9479 type node together. */
9480 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9481 DWARF_TYPE_SIGNATURE_SIZE);
9482 die->die_id.die_type_node = type_node;
9483 type_node->type_die = die;
9485 /* If the DIE is a specification, link its declaration to the type node
9486 as well. */
9487 if (decl != NULL)
9488 decl->die_id.die_type_node = type_node;
9491 /* Do the location expressions look same? */
9492 static inline int
9493 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9495 return loc1->dw_loc_opc == loc2->dw_loc_opc
9496 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9497 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9500 /* Do the values look the same? */
9501 static int
9502 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9504 dw_loc_descr_ref loc1, loc2;
9505 rtx r1, r2;
9507 if (v1->val_class != v2->val_class)
9508 return 0;
9510 switch (v1->val_class)
9512 case dw_val_class_const:
9513 return v1->v.val_int == v2->v.val_int;
9514 case dw_val_class_unsigned_const:
9515 return v1->v.val_unsigned == v2->v.val_unsigned;
9516 case dw_val_class_const_double:
9517 return v1->v.val_double.high == v2->v.val_double.high
9518 && v1->v.val_double.low == v2->v.val_double.low;
9519 case dw_val_class_vec:
9520 if (v1->v.val_vec.length != v2->v.val_vec.length
9521 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9522 return 0;
9523 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9524 v1->v.val_vec.length * v1->v.val_vec.elt_size))
9525 return 0;
9526 return 1;
9527 case dw_val_class_flag:
9528 return v1->v.val_flag == v2->v.val_flag;
9529 case dw_val_class_str:
9530 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9532 case dw_val_class_addr:
9533 r1 = v1->v.val_addr;
9534 r2 = v2->v.val_addr;
9535 if (GET_CODE (r1) != GET_CODE (r2))
9536 return 0;
9537 return !rtx_equal_p (r1, r2);
9539 case dw_val_class_offset:
9540 return v1->v.val_offset == v2->v.val_offset;
9542 case dw_val_class_loc:
9543 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9544 loc1 && loc2;
9545 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9546 if (!same_loc_p (loc1, loc2, mark))
9547 return 0;
9548 return !loc1 && !loc2;
9550 case dw_val_class_die_ref:
9551 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9553 case dw_val_class_fde_ref:
9554 case dw_val_class_vms_delta:
9555 case dw_val_class_lbl_id:
9556 case dw_val_class_lineptr:
9557 case dw_val_class_macptr:
9558 return 1;
9560 case dw_val_class_file:
9561 return v1->v.val_file == v2->v.val_file;
9563 case dw_val_class_data8:
9564 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9566 default:
9567 return 1;
9571 /* Do the attributes look the same? */
9573 static int
9574 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9576 if (at1->dw_attr != at2->dw_attr)
9577 return 0;
9579 /* We don't care that this was compiled with a different compiler
9580 snapshot; if the output is the same, that's what matters. */
9581 if (at1->dw_attr == DW_AT_producer)
9582 return 1;
9584 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9587 /* Do the dies look the same? */
9589 static int
9590 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9592 dw_die_ref c1, c2;
9593 dw_attr_ref a1;
9594 unsigned ix;
9596 /* To avoid infinite recursion. */
9597 if (die1->die_mark)
9598 return die1->die_mark == die2->die_mark;
9599 die1->die_mark = die2->die_mark = ++(*mark);
9601 if (die1->die_tag != die2->die_tag)
9602 return 0;
9604 if (VEC_length (dw_attr_node, die1->die_attr)
9605 != VEC_length (dw_attr_node, die2->die_attr))
9606 return 0;
9608 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9609 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9610 return 0;
9612 c1 = die1->die_child;
9613 c2 = die2->die_child;
9614 if (! c1)
9616 if (c2)
9617 return 0;
9619 else
9620 for (;;)
9622 if (!same_die_p (c1, c2, mark))
9623 return 0;
9624 c1 = c1->die_sib;
9625 c2 = c2->die_sib;
9626 if (c1 == die1->die_child)
9628 if (c2 == die2->die_child)
9629 break;
9630 else
9631 return 0;
9635 return 1;
9638 /* Do the dies look the same? Wrapper around same_die_p. */
9640 static int
9641 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9643 int mark = 0;
9644 int ret = same_die_p (die1, die2, &mark);
9646 unmark_all_dies (die1);
9647 unmark_all_dies (die2);
9649 return ret;
9652 /* The prefix to attach to symbols on DIEs in the current comdat debug
9653 info section. */
9654 static char *comdat_symbol_id;
9656 /* The index of the current symbol within the current comdat CU. */
9657 static unsigned int comdat_symbol_number;
9659 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9660 children, and set comdat_symbol_id accordingly. */
9662 static void
9663 compute_section_prefix (dw_die_ref unit_die)
9665 const char *die_name = get_AT_string (unit_die, DW_AT_name);
9666 const char *base = die_name ? lbasename (die_name) : "anonymous";
9667 char *name = XALLOCAVEC (char, strlen (base) + 64);
9668 char *p;
9669 int i, mark;
9670 unsigned char checksum[16];
9671 struct md5_ctx ctx;
9673 /* Compute the checksum of the DIE, then append part of it as hex digits to
9674 the name filename of the unit. */
9676 md5_init_ctx (&ctx);
9677 mark = 0;
9678 die_checksum (unit_die, &ctx, &mark);
9679 unmark_all_dies (unit_die);
9680 md5_finish_ctx (&ctx, checksum);
9682 sprintf (name, "%s.", base);
9683 clean_symbol_name (name);
9685 p = name + strlen (name);
9686 for (i = 0; i < 4; i++)
9688 sprintf (p, "%.2x", checksum[i]);
9689 p += 2;
9692 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9693 comdat_symbol_number = 0;
9696 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
9698 static int
9699 is_type_die (dw_die_ref die)
9701 switch (die->die_tag)
9703 case DW_TAG_array_type:
9704 case DW_TAG_class_type:
9705 case DW_TAG_interface_type:
9706 case DW_TAG_enumeration_type:
9707 case DW_TAG_pointer_type:
9708 case DW_TAG_reference_type:
9709 case DW_TAG_rvalue_reference_type:
9710 case DW_TAG_string_type:
9711 case DW_TAG_structure_type:
9712 case DW_TAG_subroutine_type:
9713 case DW_TAG_union_type:
9714 case DW_TAG_ptr_to_member_type:
9715 case DW_TAG_set_type:
9716 case DW_TAG_subrange_type:
9717 case DW_TAG_base_type:
9718 case DW_TAG_const_type:
9719 case DW_TAG_file_type:
9720 case DW_TAG_packed_type:
9721 case DW_TAG_volatile_type:
9722 case DW_TAG_typedef:
9723 return 1;
9724 default:
9725 return 0;
9729 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9730 Basically, we want to choose the bits that are likely to be shared between
9731 compilations (types) and leave out the bits that are specific to individual
9732 compilations (functions). */
9734 static int
9735 is_comdat_die (dw_die_ref c)
9737 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9738 we do for stabs. The advantage is a greater likelihood of sharing between
9739 objects that don't include headers in the same order (and therefore would
9740 put the base types in a different comdat). jason 8/28/00 */
9742 if (c->die_tag == DW_TAG_base_type)
9743 return 0;
9745 if (c->die_tag == DW_TAG_pointer_type
9746 || c->die_tag == DW_TAG_reference_type
9747 || c->die_tag == DW_TAG_rvalue_reference_type
9748 || c->die_tag == DW_TAG_const_type
9749 || c->die_tag == DW_TAG_volatile_type)
9751 dw_die_ref t = get_AT_ref (c, DW_AT_type);
9753 return t ? is_comdat_die (t) : 0;
9756 return is_type_die (c);
9759 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9760 compilation unit. */
9762 static int
9763 is_symbol_die (dw_die_ref c)
9765 return (is_type_die (c)
9766 || is_declaration_die (c)
9767 || c->die_tag == DW_TAG_namespace
9768 || c->die_tag == DW_TAG_module);
9771 /* Returns true iff C is a compile-unit DIE. */
9773 static inline bool
9774 is_cu_die (dw_die_ref c)
9776 return c && c->die_tag == DW_TAG_compile_unit;
9779 static char *
9780 gen_internal_sym (const char *prefix)
9782 char buf[256];
9784 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9785 return xstrdup (buf);
9788 /* Assign symbols to all worthy DIEs under DIE. */
9790 static void
9791 assign_symbol_names (dw_die_ref die)
9793 dw_die_ref c;
9795 if (is_symbol_die (die))
9797 if (comdat_symbol_id)
9799 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9801 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9802 comdat_symbol_id, comdat_symbol_number++);
9803 die->die_id.die_symbol = xstrdup (p);
9805 else
9806 die->die_id.die_symbol = gen_internal_sym ("LDIE");
9809 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9812 struct cu_hash_table_entry
9814 dw_die_ref cu;
9815 unsigned min_comdat_num, max_comdat_num;
9816 struct cu_hash_table_entry *next;
9819 /* Routines to manipulate hash table of CUs. */
9820 static hashval_t
9821 htab_cu_hash (const void *of)
9823 const struct cu_hash_table_entry *const entry =
9824 (const struct cu_hash_table_entry *) of;
9826 return htab_hash_string (entry->cu->die_id.die_symbol);
9829 static int
9830 htab_cu_eq (const void *of1, const void *of2)
9832 const struct cu_hash_table_entry *const entry1 =
9833 (const struct cu_hash_table_entry *) of1;
9834 const struct die_struct *const entry2 = (const struct die_struct *) of2;
9836 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9839 static void
9840 htab_cu_del (void *what)
9842 struct cu_hash_table_entry *next,
9843 *entry = (struct cu_hash_table_entry *) what;
9845 while (entry)
9847 next = entry->next;
9848 free (entry);
9849 entry = next;
9853 /* Check whether we have already seen this CU and set up SYM_NUM
9854 accordingly. */
9855 static int
9856 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9858 struct cu_hash_table_entry dummy;
9859 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9861 dummy.max_comdat_num = 0;
9863 slot = (struct cu_hash_table_entry **)
9864 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9865 INSERT);
9866 entry = *slot;
9868 for (; entry; last = entry, entry = entry->next)
9870 if (same_die_p_wrap (cu, entry->cu))
9871 break;
9874 if (entry)
9876 *sym_num = entry->min_comdat_num;
9877 return 1;
9880 entry = XCNEW (struct cu_hash_table_entry);
9881 entry->cu = cu;
9882 entry->min_comdat_num = *sym_num = last->max_comdat_num;
9883 entry->next = *slot;
9884 *slot = entry;
9886 return 0;
9889 /* Record SYM_NUM to record of CU in HTABLE. */
9890 static void
9891 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9893 struct cu_hash_table_entry **slot, *entry;
9895 slot = (struct cu_hash_table_entry **)
9896 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9897 NO_INSERT);
9898 entry = *slot;
9900 entry->max_comdat_num = sym_num;
9903 /* Traverse the DIE (which is always comp_unit_die), and set up
9904 additional compilation units for each of the include files we see
9905 bracketed by BINCL/EINCL. */
9907 static void
9908 break_out_includes (dw_die_ref die)
9910 dw_die_ref c;
9911 dw_die_ref unit = NULL;
9912 limbo_die_node *node, **pnode;
9913 htab_t cu_hash_table;
9915 c = die->die_child;
9916 if (c) do {
9917 dw_die_ref prev = c;
9918 c = c->die_sib;
9919 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9920 || (unit && is_comdat_die (c)))
9922 dw_die_ref next = c->die_sib;
9924 /* This DIE is for a secondary CU; remove it from the main one. */
9925 remove_child_with_prev (c, prev);
9927 if (c->die_tag == DW_TAG_GNU_BINCL)
9928 unit = push_new_compile_unit (unit, c);
9929 else if (c->die_tag == DW_TAG_GNU_EINCL)
9930 unit = pop_compile_unit (unit);
9931 else
9932 add_child_die (unit, c);
9933 c = next;
9934 if (c == die->die_child)
9935 break;
9937 } while (c != die->die_child);
9939 #if 0
9940 /* We can only use this in debugging, since the frontend doesn't check
9941 to make sure that we leave every include file we enter. */
9942 gcc_assert (!unit);
9943 #endif
9945 assign_symbol_names (die);
9946 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9947 for (node = limbo_die_list, pnode = &limbo_die_list;
9948 node;
9949 node = node->next)
9951 int is_dupl;
9953 compute_section_prefix (node->die);
9954 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9955 &comdat_symbol_number);
9956 assign_symbol_names (node->die);
9957 if (is_dupl)
9958 *pnode = node->next;
9959 else
9961 pnode = &node->next;
9962 record_comdat_symbol_number (node->die, cu_hash_table,
9963 comdat_symbol_number);
9966 htab_delete (cu_hash_table);
9969 /* Return non-zero if this DIE is a declaration. */
9971 static int
9972 is_declaration_die (dw_die_ref die)
9974 dw_attr_ref a;
9975 unsigned ix;
9977 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9978 if (a->dw_attr == DW_AT_declaration)
9979 return 1;
9981 return 0;
9984 /* Return non-zero if this DIE is nested inside a subprogram. */
9986 static int
9987 is_nested_in_subprogram (dw_die_ref die)
9989 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9991 if (decl == NULL)
9992 decl = die;
9993 return local_scope_p (decl);
9996 /* Return non-zero if this is a type DIE that should be moved to a
9997 COMDAT .debug_types section. */
9999 static int
10000 should_move_die_to_comdat (dw_die_ref die)
10002 switch (die->die_tag)
10004 case DW_TAG_class_type:
10005 case DW_TAG_structure_type:
10006 case DW_TAG_enumeration_type:
10007 case DW_TAG_union_type:
10008 /* Don't move declarations, inlined instances, or types nested in a
10009 subprogram. */
10010 if (is_declaration_die (die)
10011 || get_AT (die, DW_AT_abstract_origin)
10012 || is_nested_in_subprogram (die))
10013 return 0;
10014 return 1;
10015 case DW_TAG_array_type:
10016 case DW_TAG_interface_type:
10017 case DW_TAG_pointer_type:
10018 case DW_TAG_reference_type:
10019 case DW_TAG_rvalue_reference_type:
10020 case DW_TAG_string_type:
10021 case DW_TAG_subroutine_type:
10022 case DW_TAG_ptr_to_member_type:
10023 case DW_TAG_set_type:
10024 case DW_TAG_subrange_type:
10025 case DW_TAG_base_type:
10026 case DW_TAG_const_type:
10027 case DW_TAG_file_type:
10028 case DW_TAG_packed_type:
10029 case DW_TAG_volatile_type:
10030 case DW_TAG_typedef:
10031 default:
10032 return 0;
10036 /* Make a clone of DIE. */
10038 static dw_die_ref
10039 clone_die (dw_die_ref die)
10041 dw_die_ref clone;
10042 dw_attr_ref a;
10043 unsigned ix;
10045 clone = ggc_alloc_cleared_die_node ();
10046 clone->die_tag = die->die_tag;
10048 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10049 add_dwarf_attr (clone, a);
10051 return clone;
10054 /* Make a clone of the tree rooted at DIE. */
10056 static dw_die_ref
10057 clone_tree (dw_die_ref die)
10059 dw_die_ref c;
10060 dw_die_ref clone = clone_die (die);
10062 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10064 return clone;
10067 /* Make a clone of DIE as a declaration. */
10069 static dw_die_ref
10070 clone_as_declaration (dw_die_ref die)
10072 dw_die_ref clone;
10073 dw_die_ref decl;
10074 dw_attr_ref a;
10075 unsigned ix;
10077 /* If the DIE is already a declaration, just clone it. */
10078 if (is_declaration_die (die))
10079 return clone_die (die);
10081 /* If the DIE is a specification, just clone its declaration DIE. */
10082 decl = get_AT_ref (die, DW_AT_specification);
10083 if (decl != NULL)
10084 return clone_die (decl);
10086 clone = ggc_alloc_cleared_die_node ();
10087 clone->die_tag = die->die_tag;
10089 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10091 /* We don't want to copy over all attributes.
10092 For example we don't want DW_AT_byte_size because otherwise we will no
10093 longer have a declaration and GDB will treat it as a definition. */
10095 switch (a->dw_attr)
10097 case DW_AT_artificial:
10098 case DW_AT_containing_type:
10099 case DW_AT_external:
10100 case DW_AT_name:
10101 case DW_AT_type:
10102 case DW_AT_virtuality:
10103 case DW_AT_linkage_name:
10104 case DW_AT_MIPS_linkage_name:
10105 add_dwarf_attr (clone, a);
10106 break;
10107 case DW_AT_byte_size:
10108 default:
10109 break;
10113 if (die->die_id.die_type_node)
10114 add_AT_die_ref (clone, DW_AT_signature, die);
10116 add_AT_flag (clone, DW_AT_declaration, 1);
10117 return clone;
10120 /* Copy the declaration context to the new compile unit DIE. This includes
10121 any surrounding namespace or type declarations. If the DIE has an
10122 AT_specification attribute, it also includes attributes and children
10123 attached to the specification. */
10125 static void
10126 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10128 dw_die_ref decl;
10129 dw_die_ref new_decl;
10131 decl = get_AT_ref (die, DW_AT_specification);
10132 if (decl == NULL)
10133 decl = die;
10134 else
10136 unsigned ix;
10137 dw_die_ref c;
10138 dw_attr_ref a;
10140 /* Copy the type node pointer from the new DIE to the original
10141 declaration DIE so we can forward references later. */
10142 decl->die_id.die_type_node = die->die_id.die_type_node;
10144 remove_AT (die, DW_AT_specification);
10146 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10148 if (a->dw_attr != DW_AT_name
10149 && a->dw_attr != DW_AT_declaration
10150 && a->dw_attr != DW_AT_external)
10151 add_dwarf_attr (die, a);
10154 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10157 if (decl->die_parent != NULL
10158 && decl->die_parent->die_tag != DW_TAG_compile_unit
10159 && decl->die_parent->die_tag != DW_TAG_type_unit)
10161 new_decl = copy_ancestor_tree (unit, decl, NULL);
10162 if (new_decl != NULL)
10164 remove_AT (new_decl, DW_AT_signature);
10165 add_AT_specification (die, new_decl);
10170 /* Generate the skeleton ancestor tree for the given NODE, then clone
10171 the DIE and add the clone into the tree. */
10173 static void
10174 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10176 if (node->new_die != NULL)
10177 return;
10179 node->new_die = clone_as_declaration (node->old_die);
10181 if (node->parent != NULL)
10183 generate_skeleton_ancestor_tree (node->parent);
10184 add_child_die (node->parent->new_die, node->new_die);
10188 /* Generate a skeleton tree of DIEs containing any declarations that are
10189 found in the original tree. We traverse the tree looking for declaration
10190 DIEs, and construct the skeleton from the bottom up whenever we find one. */
10192 static void
10193 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10195 skeleton_chain_node node;
10196 dw_die_ref c;
10197 dw_die_ref first;
10198 dw_die_ref prev = NULL;
10199 dw_die_ref next = NULL;
10201 node.parent = parent;
10203 first = c = parent->old_die->die_child;
10204 if (c)
10205 next = c->die_sib;
10206 if (c) do {
10207 if (prev == NULL || prev->die_sib == c)
10208 prev = c;
10209 c = next;
10210 next = (c == first ? NULL : c->die_sib);
10211 node.old_die = c;
10212 node.new_die = NULL;
10213 if (is_declaration_die (c))
10215 /* Clone the existing DIE, move the original to the skeleton
10216 tree (which is in the main CU), and put the clone, with
10217 all the original's children, where the original came from. */
10218 dw_die_ref clone = clone_die (c);
10219 move_all_children (c, clone);
10221 replace_child (c, clone, prev);
10222 generate_skeleton_ancestor_tree (parent);
10223 add_child_die (parent->new_die, c);
10224 node.new_die = c;
10225 c = clone;
10227 generate_skeleton_bottom_up (&node);
10228 } while (next != NULL);
10231 /* Wrapper function for generate_skeleton_bottom_up. */
10233 static dw_die_ref
10234 generate_skeleton (dw_die_ref die)
10236 skeleton_chain_node node;
10238 node.old_die = die;
10239 node.new_die = NULL;
10240 node.parent = NULL;
10242 /* If this type definition is nested inside another type,
10243 always leave at least a declaration in its place. */
10244 if (die->die_parent != NULL && is_type_die (die->die_parent))
10245 node.new_die = clone_as_declaration (die);
10247 generate_skeleton_bottom_up (&node);
10248 return node.new_die;
10251 /* Remove the DIE from its parent, possibly replacing it with a cloned
10252 declaration. The original DIE will be moved to a new compile unit
10253 so that existing references to it follow it to the new location. If
10254 any of the original DIE's descendants is a declaration, we need to
10255 replace the original DIE with a skeleton tree and move the
10256 declarations back into the skeleton tree. */
10258 static dw_die_ref
10259 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10261 dw_die_ref skeleton;
10263 skeleton = generate_skeleton (child);
10264 if (skeleton == NULL)
10265 remove_child_with_prev (child, prev);
10266 else
10268 skeleton->die_id.die_type_node = child->die_id.die_type_node;
10269 replace_child (child, skeleton, prev);
10272 return skeleton;
10275 /* Traverse the DIE and set up additional .debug_types sections for each
10276 type worthy of being placed in a COMDAT section. */
10278 static void
10279 break_out_comdat_types (dw_die_ref die)
10281 dw_die_ref c;
10282 dw_die_ref first;
10283 dw_die_ref prev = NULL;
10284 dw_die_ref next = NULL;
10285 dw_die_ref unit = NULL;
10287 first = c = die->die_child;
10288 if (c)
10289 next = c->die_sib;
10290 if (c) do {
10291 if (prev == NULL || prev->die_sib == c)
10292 prev = c;
10293 c = next;
10294 next = (c == first ? NULL : c->die_sib);
10295 if (should_move_die_to_comdat (c))
10297 dw_die_ref replacement;
10298 comdat_type_node_ref type_node;
10300 /* Create a new type unit DIE as the root for the new tree, and
10301 add it to the list of comdat types. */
10302 unit = new_die (DW_TAG_type_unit, NULL, NULL);
10303 add_AT_unsigned (unit, DW_AT_language,
10304 get_AT_unsigned (comp_unit_die (), DW_AT_language));
10305 type_node = ggc_alloc_cleared_comdat_type_node ();
10306 type_node->root_die = unit;
10307 type_node->next = comdat_type_list;
10308 comdat_type_list = type_node;
10310 /* Generate the type signature. */
10311 generate_type_signature (c, type_node);
10313 /* Copy the declaration context, attributes, and children of the
10314 declaration into the new compile unit DIE. */
10315 copy_declaration_context (unit, c);
10317 /* Remove this DIE from the main CU. */
10318 replacement = remove_child_or_replace_with_skeleton (c, prev);
10320 /* Break out nested types into their own type units. */
10321 break_out_comdat_types (c);
10323 /* Add the DIE to the new compunit. */
10324 add_child_die (unit, c);
10326 if (replacement != NULL)
10327 c = replacement;
10329 else if (c->die_tag == DW_TAG_namespace
10330 || c->die_tag == DW_TAG_class_type
10331 || c->die_tag == DW_TAG_structure_type
10332 || c->die_tag == DW_TAG_union_type)
10334 /* Look for nested types that can be broken out. */
10335 break_out_comdat_types (c);
10337 } while (next != NULL);
10340 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
10342 struct decl_table_entry
10344 dw_die_ref orig;
10345 dw_die_ref copy;
10348 /* Routines to manipulate hash table of copied declarations. */
10350 static hashval_t
10351 htab_decl_hash (const void *of)
10353 const struct decl_table_entry *const entry =
10354 (const struct decl_table_entry *) of;
10356 return htab_hash_pointer (entry->orig);
10359 static int
10360 htab_decl_eq (const void *of1, const void *of2)
10362 const struct decl_table_entry *const entry1 =
10363 (const struct decl_table_entry *) of1;
10364 const struct die_struct *const entry2 = (const struct die_struct *) of2;
10366 return entry1->orig == entry2;
10369 static void
10370 htab_decl_del (void *what)
10372 struct decl_table_entry *entry = (struct decl_table_entry *) what;
10374 free (entry);
10377 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10378 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
10379 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
10380 to check if the ancestor has already been copied into UNIT. */
10382 static dw_die_ref
10383 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10385 dw_die_ref parent = die->die_parent;
10386 dw_die_ref new_parent = unit;
10387 dw_die_ref copy;
10388 void **slot = NULL;
10389 struct decl_table_entry *entry = NULL;
10391 if (decl_table)
10393 /* Check if the entry has already been copied to UNIT. */
10394 slot = htab_find_slot_with_hash (decl_table, die,
10395 htab_hash_pointer (die), INSERT);
10396 if (*slot != HTAB_EMPTY_ENTRY)
10398 entry = (struct decl_table_entry *) *slot;
10399 return entry->copy;
10402 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
10403 entry = XCNEW (struct decl_table_entry);
10404 entry->orig = die;
10405 entry->copy = NULL;
10406 *slot = entry;
10409 if (parent != NULL)
10411 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10412 if (spec != NULL)
10413 parent = spec;
10414 if (parent->die_tag != DW_TAG_compile_unit
10415 && parent->die_tag != DW_TAG_type_unit)
10416 new_parent = copy_ancestor_tree (unit, parent, decl_table);
10419 copy = clone_as_declaration (die);
10420 add_child_die (new_parent, copy);
10422 if (decl_table != NULL)
10424 /* Record the pointer to the copy. */
10425 entry->copy = copy;
10428 return copy;
10431 /* Walk the DIE and its children, looking for references to incomplete
10432 or trivial types that are unmarked (i.e., that are not in the current
10433 type_unit). */
10435 static void
10436 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10438 dw_die_ref c;
10439 dw_attr_ref a;
10440 unsigned ix;
10442 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10444 if (AT_class (a) == dw_val_class_die_ref)
10446 dw_die_ref targ = AT_ref (a);
10447 comdat_type_node_ref type_node = targ->die_id.die_type_node;
10448 void **slot;
10449 struct decl_table_entry *entry;
10451 if (targ->die_mark != 0 || type_node != NULL)
10452 continue;
10454 slot = htab_find_slot_with_hash (decl_table, targ,
10455 htab_hash_pointer (targ), INSERT);
10457 if (*slot != HTAB_EMPTY_ENTRY)
10459 /* TARG has already been copied, so we just need to
10460 modify the reference to point to the copy. */
10461 entry = (struct decl_table_entry *) *slot;
10462 a->dw_attr_val.v.val_die_ref.die = entry->copy;
10464 else
10466 dw_die_ref parent = unit;
10467 dw_die_ref copy = clone_tree (targ);
10469 /* Make sure the cloned tree is marked as part of the
10470 type unit. */
10471 mark_dies (copy);
10473 /* Record in DECL_TABLE that TARG has been copied.
10474 Need to do this now, before the recursive call,
10475 because DECL_TABLE may be expanded and SLOT
10476 would no longer be a valid pointer. */
10477 entry = XCNEW (struct decl_table_entry);
10478 entry->orig = targ;
10479 entry->copy = copy;
10480 *slot = entry;
10482 /* If TARG has surrounding context, copy its ancestor tree
10483 into the new type unit. */
10484 if (targ->die_parent != NULL
10485 && targ->die_parent->die_tag != DW_TAG_compile_unit
10486 && targ->die_parent->die_tag != DW_TAG_type_unit)
10487 parent = copy_ancestor_tree (unit, targ->die_parent,
10488 decl_table);
10490 add_child_die (parent, copy);
10491 a->dw_attr_val.v.val_die_ref.die = copy;
10493 /* Make sure the newly-copied DIE is walked. If it was
10494 installed in a previously-added context, it won't
10495 get visited otherwise. */
10496 if (parent != unit)
10498 /* Find the highest point of the newly-added tree,
10499 mark each node along the way, and walk from there. */
10500 parent->die_mark = 1;
10501 while (parent->die_parent
10502 && parent->die_parent->die_mark == 0)
10504 parent = parent->die_parent;
10505 parent->die_mark = 1;
10507 copy_decls_walk (unit, parent, decl_table);
10513 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10516 /* Copy declarations for "unworthy" types into the new comdat section.
10517 Incomplete types, modified types, and certain other types aren't broken
10518 out into comdat sections of their own, so they don't have a signature,
10519 and we need to copy the declaration into the same section so that we
10520 don't have an external reference. */
10522 static void
10523 copy_decls_for_unworthy_types (dw_die_ref unit)
10525 htab_t decl_table;
10527 mark_dies (unit);
10528 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10529 copy_decls_walk (unit, unit, decl_table);
10530 htab_delete (decl_table);
10531 unmark_dies (unit);
10534 /* Traverse the DIE and add a sibling attribute if it may have the
10535 effect of speeding up access to siblings. To save some space,
10536 avoid generating sibling attributes for DIE's without children. */
10538 static void
10539 add_sibling_attributes (dw_die_ref die)
10541 dw_die_ref c;
10543 if (! die->die_child)
10544 return;
10546 if (die->die_parent && die != die->die_parent->die_child)
10547 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10549 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10552 /* Output all location lists for the DIE and its children. */
10554 static void
10555 output_location_lists (dw_die_ref die)
10557 dw_die_ref c;
10558 dw_attr_ref a;
10559 unsigned ix;
10561 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10562 if (AT_class (a) == dw_val_class_loc_list)
10563 output_loc_list (AT_loc_list (a));
10565 FOR_EACH_CHILD (die, c, output_location_lists (c));
10568 /* The format of each DIE (and its attribute value pairs) is encoded in an
10569 abbreviation table. This routine builds the abbreviation table and assigns
10570 a unique abbreviation id for each abbreviation entry. The children of each
10571 die are visited recursively. */
10573 static void
10574 build_abbrev_table (dw_die_ref die)
10576 unsigned long abbrev_id;
10577 unsigned int n_alloc;
10578 dw_die_ref c;
10579 dw_attr_ref a;
10580 unsigned ix;
10582 /* Scan the DIE references, and mark as external any that refer to
10583 DIEs from other CUs (i.e. those which are not marked). */
10584 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10585 if (AT_class (a) == dw_val_class_die_ref
10586 && AT_ref (a)->die_mark == 0)
10588 gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10589 set_AT_ref_external (a, 1);
10592 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10594 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10595 dw_attr_ref die_a, abbrev_a;
10596 unsigned ix;
10597 bool ok = true;
10599 if (abbrev->die_tag != die->die_tag)
10600 continue;
10601 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10602 continue;
10604 if (VEC_length (dw_attr_node, abbrev->die_attr)
10605 != VEC_length (dw_attr_node, die->die_attr))
10606 continue;
10608 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10610 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10611 if ((abbrev_a->dw_attr != die_a->dw_attr)
10612 || (value_format (abbrev_a) != value_format (die_a)))
10614 ok = false;
10615 break;
10618 if (ok)
10619 break;
10622 if (abbrev_id >= abbrev_die_table_in_use)
10624 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10626 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10627 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10628 n_alloc);
10630 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10631 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10632 abbrev_die_table_allocated = n_alloc;
10635 ++abbrev_die_table_in_use;
10636 abbrev_die_table[abbrev_id] = die;
10639 die->die_abbrev = abbrev_id;
10640 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10643 /* Return the power-of-two number of bytes necessary to represent VALUE. */
10645 static int
10646 constant_size (unsigned HOST_WIDE_INT value)
10648 int log;
10650 if (value == 0)
10651 log = 0;
10652 else
10653 log = floor_log2 (value);
10655 log = log / 8;
10656 log = 1 << (floor_log2 (log) + 1);
10658 return log;
10661 /* Return the size of a DIE as it is represented in the
10662 .debug_info section. */
10664 static unsigned long
10665 size_of_die (dw_die_ref die)
10667 unsigned long size = 0;
10668 dw_attr_ref a;
10669 unsigned ix;
10671 size += size_of_uleb128 (die->die_abbrev);
10672 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10674 switch (AT_class (a))
10676 case dw_val_class_addr:
10677 size += DWARF2_ADDR_SIZE;
10678 break;
10679 case dw_val_class_offset:
10680 size += DWARF_OFFSET_SIZE;
10681 break;
10682 case dw_val_class_loc:
10684 unsigned long lsize = size_of_locs (AT_loc (a));
10686 /* Block length. */
10687 if (dwarf_version >= 4)
10688 size += size_of_uleb128 (lsize);
10689 else
10690 size += constant_size (lsize);
10691 size += lsize;
10693 break;
10694 case dw_val_class_loc_list:
10695 size += DWARF_OFFSET_SIZE;
10696 break;
10697 case dw_val_class_range_list:
10698 size += DWARF_OFFSET_SIZE;
10699 break;
10700 case dw_val_class_const:
10701 size += size_of_sleb128 (AT_int (a));
10702 break;
10703 case dw_val_class_unsigned_const:
10704 size += constant_size (AT_unsigned (a));
10705 break;
10706 case dw_val_class_const_double:
10707 size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10708 if (HOST_BITS_PER_WIDE_INT >= 64)
10709 size++; /* block */
10710 break;
10711 case dw_val_class_vec:
10712 size += constant_size (a->dw_attr_val.v.val_vec.length
10713 * a->dw_attr_val.v.val_vec.elt_size)
10714 + a->dw_attr_val.v.val_vec.length
10715 * a->dw_attr_val.v.val_vec.elt_size; /* block */
10716 break;
10717 case dw_val_class_flag:
10718 if (dwarf_version >= 4)
10719 /* Currently all add_AT_flag calls pass in 1 as last argument,
10720 so DW_FORM_flag_present can be used. If that ever changes,
10721 we'll need to use DW_FORM_flag and have some optimization
10722 in build_abbrev_table that will change those to
10723 DW_FORM_flag_present if it is set to 1 in all DIEs using
10724 the same abbrev entry. */
10725 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10726 else
10727 size += 1;
10728 break;
10729 case dw_val_class_die_ref:
10730 if (AT_ref_external (a))
10732 /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10733 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
10734 is sized by target address length, whereas in DWARF3
10735 it's always sized as an offset. */
10736 if (dwarf_version >= 4)
10737 size += DWARF_TYPE_SIGNATURE_SIZE;
10738 else if (dwarf_version == 2)
10739 size += DWARF2_ADDR_SIZE;
10740 else
10741 size += DWARF_OFFSET_SIZE;
10743 else
10744 size += DWARF_OFFSET_SIZE;
10745 break;
10746 case dw_val_class_fde_ref:
10747 size += DWARF_OFFSET_SIZE;
10748 break;
10749 case dw_val_class_lbl_id:
10750 size += DWARF2_ADDR_SIZE;
10751 break;
10752 case dw_val_class_lineptr:
10753 case dw_val_class_macptr:
10754 size += DWARF_OFFSET_SIZE;
10755 break;
10756 case dw_val_class_str:
10757 if (AT_string_form (a) == DW_FORM_strp)
10758 size += DWARF_OFFSET_SIZE;
10759 else
10760 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10761 break;
10762 case dw_val_class_file:
10763 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10764 break;
10765 case dw_val_class_data8:
10766 size += 8;
10767 break;
10768 case dw_val_class_vms_delta:
10769 size += DWARF_OFFSET_SIZE;
10770 break;
10771 default:
10772 gcc_unreachable ();
10776 return size;
10779 /* Size the debugging information associated with a given DIE. Visits the
10780 DIE's children recursively. Updates the global variable next_die_offset, on
10781 each time through. Uses the current value of next_die_offset to update the
10782 die_offset field in each DIE. */
10784 static void
10785 calc_die_sizes (dw_die_ref die)
10787 dw_die_ref c;
10789 die->die_offset = next_die_offset;
10790 next_die_offset += size_of_die (die);
10792 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10794 if (die->die_child != NULL)
10795 /* Count the null byte used to terminate sibling lists. */
10796 next_die_offset += 1;
10799 /* Set the marks for a die and its children. We do this so
10800 that we know whether or not a reference needs to use FORM_ref_addr; only
10801 DIEs in the same CU will be marked. We used to clear out the offset
10802 and use that as the flag, but ran into ordering problems. */
10804 static void
10805 mark_dies (dw_die_ref die)
10807 dw_die_ref c;
10809 gcc_assert (!die->die_mark);
10811 die->die_mark = 1;
10812 FOR_EACH_CHILD (die, c, mark_dies (c));
10815 /* Clear the marks for a die and its children. */
10817 static void
10818 unmark_dies (dw_die_ref die)
10820 dw_die_ref c;
10822 if (dwarf_version < 4)
10823 gcc_assert (die->die_mark);
10825 die->die_mark = 0;
10826 FOR_EACH_CHILD (die, c, unmark_dies (c));
10829 /* Clear the marks for a die, its children and referred dies. */
10831 static void
10832 unmark_all_dies (dw_die_ref die)
10834 dw_die_ref c;
10835 dw_attr_ref a;
10836 unsigned ix;
10838 if (!die->die_mark)
10839 return;
10840 die->die_mark = 0;
10842 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10844 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10845 if (AT_class (a) == dw_val_class_die_ref)
10846 unmark_all_dies (AT_ref (a));
10849 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10850 generated for the compilation unit. */
10852 static unsigned long
10853 size_of_pubnames (VEC (pubname_entry, gc) * names)
10855 unsigned long size;
10856 unsigned i;
10857 pubname_ref p;
10859 size = DWARF_PUBNAMES_HEADER_SIZE;
10860 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10861 if (names != pubtype_table
10862 || p->die->die_offset != 0
10863 || !flag_eliminate_unused_debug_types)
10864 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10866 size += DWARF_OFFSET_SIZE;
10867 return size;
10870 /* Return the size of the information in the .debug_aranges section. */
10872 static unsigned long
10873 size_of_aranges (void)
10875 unsigned long size;
10877 size = DWARF_ARANGES_HEADER_SIZE;
10879 /* Count the address/length pair for this compilation unit. */
10880 if (text_section_used)
10881 size += 2 * DWARF2_ADDR_SIZE;
10882 if (cold_text_section_used)
10883 size += 2 * DWARF2_ADDR_SIZE;
10884 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10886 /* Count the two zero words used to terminated the address range table. */
10887 size += 2 * DWARF2_ADDR_SIZE;
10888 return size;
10891 /* Select the encoding of an attribute value. */
10893 static enum dwarf_form
10894 value_format (dw_attr_ref a)
10896 switch (a->dw_attr_val.val_class)
10898 case dw_val_class_addr:
10899 /* Only very few attributes allow DW_FORM_addr. */
10900 switch (a->dw_attr)
10902 case DW_AT_low_pc:
10903 case DW_AT_high_pc:
10904 case DW_AT_entry_pc:
10905 case DW_AT_trampoline:
10906 return DW_FORM_addr;
10907 default:
10908 break;
10910 switch (DWARF2_ADDR_SIZE)
10912 case 1:
10913 return DW_FORM_data1;
10914 case 2:
10915 return DW_FORM_data2;
10916 case 4:
10917 return DW_FORM_data4;
10918 case 8:
10919 return DW_FORM_data8;
10920 default:
10921 gcc_unreachable ();
10923 case dw_val_class_range_list:
10924 case dw_val_class_loc_list:
10925 if (dwarf_version >= 4)
10926 return DW_FORM_sec_offset;
10927 /* FALLTHRU */
10928 case dw_val_class_vms_delta:
10929 case dw_val_class_offset:
10930 switch (DWARF_OFFSET_SIZE)
10932 case 4:
10933 return DW_FORM_data4;
10934 case 8:
10935 return DW_FORM_data8;
10936 default:
10937 gcc_unreachable ();
10939 case dw_val_class_loc:
10940 if (dwarf_version >= 4)
10941 return DW_FORM_exprloc;
10942 switch (constant_size (size_of_locs (AT_loc (a))))
10944 case 1:
10945 return DW_FORM_block1;
10946 case 2:
10947 return DW_FORM_block2;
10948 default:
10949 gcc_unreachable ();
10951 case dw_val_class_const:
10952 return DW_FORM_sdata;
10953 case dw_val_class_unsigned_const:
10954 switch (constant_size (AT_unsigned (a)))
10956 case 1:
10957 return DW_FORM_data1;
10958 case 2:
10959 return DW_FORM_data2;
10960 case 4:
10961 return DW_FORM_data4;
10962 case 8:
10963 return DW_FORM_data8;
10964 default:
10965 gcc_unreachable ();
10967 case dw_val_class_const_double:
10968 switch (HOST_BITS_PER_WIDE_INT)
10970 case 8:
10971 return DW_FORM_data2;
10972 case 16:
10973 return DW_FORM_data4;
10974 case 32:
10975 return DW_FORM_data8;
10976 case 64:
10977 default:
10978 return DW_FORM_block1;
10980 case dw_val_class_vec:
10981 switch (constant_size (a->dw_attr_val.v.val_vec.length
10982 * a->dw_attr_val.v.val_vec.elt_size))
10984 case 1:
10985 return DW_FORM_block1;
10986 case 2:
10987 return DW_FORM_block2;
10988 case 4:
10989 return DW_FORM_block4;
10990 default:
10991 gcc_unreachable ();
10993 case dw_val_class_flag:
10994 if (dwarf_version >= 4)
10996 /* Currently all add_AT_flag calls pass in 1 as last argument,
10997 so DW_FORM_flag_present can be used. If that ever changes,
10998 we'll need to use DW_FORM_flag and have some optimization
10999 in build_abbrev_table that will change those to
11000 DW_FORM_flag_present if it is set to 1 in all DIEs using
11001 the same abbrev entry. */
11002 gcc_assert (a->dw_attr_val.v.val_flag == 1);
11003 return DW_FORM_flag_present;
11005 return DW_FORM_flag;
11006 case dw_val_class_die_ref:
11007 if (AT_ref_external (a))
11008 return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
11009 else
11010 return DW_FORM_ref;
11011 case dw_val_class_fde_ref:
11012 return DW_FORM_data;
11013 case dw_val_class_lbl_id:
11014 return DW_FORM_addr;
11015 case dw_val_class_lineptr:
11016 case dw_val_class_macptr:
11017 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11018 case dw_val_class_str:
11019 return AT_string_form (a);
11020 case dw_val_class_file:
11021 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11023 case 1:
11024 return DW_FORM_data1;
11025 case 2:
11026 return DW_FORM_data2;
11027 case 4:
11028 return DW_FORM_data4;
11029 default:
11030 gcc_unreachable ();
11033 case dw_val_class_data8:
11034 return DW_FORM_data8;
11036 default:
11037 gcc_unreachable ();
11041 /* Output the encoding of an attribute value. */
11043 static void
11044 output_value_format (dw_attr_ref a)
11046 enum dwarf_form form = value_format (a);
11048 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11051 /* Output the .debug_abbrev section which defines the DIE abbreviation
11052 table. */
11054 static void
11055 output_abbrev_section (void)
11057 unsigned long abbrev_id;
11059 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11061 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11062 unsigned ix;
11063 dw_attr_ref a_attr;
11065 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11066 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11067 dwarf_tag_name (abbrev->die_tag));
11069 if (abbrev->die_child != NULL)
11070 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11071 else
11072 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11074 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11075 ix++)
11077 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11078 dwarf_attr_name (a_attr->dw_attr));
11079 output_value_format (a_attr);
11082 dw2_asm_output_data (1, 0, NULL);
11083 dw2_asm_output_data (1, 0, NULL);
11086 /* Terminate the table. */
11087 dw2_asm_output_data (1, 0, NULL);
11090 /* Output a symbol we can use to refer to this DIE from another CU. */
11092 static inline void
11093 output_die_symbol (dw_die_ref die)
11095 char *sym = die->die_id.die_symbol;
11097 if (sym == 0)
11098 return;
11100 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11101 /* We make these global, not weak; if the target doesn't support
11102 .linkonce, it doesn't support combining the sections, so debugging
11103 will break. */
11104 targetm.asm_out.globalize_label (asm_out_file, sym);
11106 ASM_OUTPUT_LABEL (asm_out_file, sym);
11109 /* Return a new location list, given the begin and end range, and the
11110 expression. */
11112 static inline dw_loc_list_ref
11113 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11114 const char *section)
11116 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11118 retlist->begin = begin;
11119 retlist->end = end;
11120 retlist->expr = expr;
11121 retlist->section = section;
11123 return retlist;
11126 /* Generate a new internal symbol for this location list node, if it
11127 hasn't got one yet. */
11129 static inline void
11130 gen_llsym (dw_loc_list_ref list)
11132 gcc_assert (!list->ll_symbol);
11133 list->ll_symbol = gen_internal_sym ("LLST");
11136 /* Output the location list given to us. */
11138 static void
11139 output_loc_list (dw_loc_list_ref list_head)
11141 dw_loc_list_ref curr = list_head;
11143 if (list_head->emitted)
11144 return;
11145 list_head->emitted = true;
11147 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11149 /* Walk the location list, and output each range + expression. */
11150 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11152 unsigned long size;
11153 /* Don't output an entry that starts and ends at the same address. */
11154 if (strcmp (curr->begin, curr->end) == 0)
11155 continue;
11156 if (!have_multiple_function_sections)
11158 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11159 "Location list begin address (%s)",
11160 list_head->ll_symbol);
11161 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11162 "Location list end address (%s)",
11163 list_head->ll_symbol);
11165 else
11167 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11168 "Location list begin address (%s)",
11169 list_head->ll_symbol);
11170 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11171 "Location list end address (%s)",
11172 list_head->ll_symbol);
11174 size = size_of_locs (curr->expr);
11176 /* Output the block length for this list of location operations. */
11177 gcc_assert (size <= 0xffff);
11178 dw2_asm_output_data (2, size, "%s", "Location expression size");
11180 output_loc_sequence (curr->expr, -1);
11183 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11184 "Location list terminator begin (%s)",
11185 list_head->ll_symbol);
11186 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11187 "Location list terminator end (%s)",
11188 list_head->ll_symbol);
11191 /* Output a type signature. */
11193 static inline void
11194 output_signature (const char *sig, const char *name)
11196 int i;
11198 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11199 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11202 /* Output the DIE and its attributes. Called recursively to generate
11203 the definitions of each child DIE. */
11205 static void
11206 output_die (dw_die_ref die)
11208 dw_attr_ref a;
11209 dw_die_ref c;
11210 unsigned long size;
11211 unsigned ix;
11213 /* If someone in another CU might refer to us, set up a symbol for
11214 them to point to. */
11215 if (dwarf_version < 4 && die->die_id.die_symbol)
11216 output_die_symbol (die);
11218 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11219 (unsigned long)die->die_offset,
11220 dwarf_tag_name (die->die_tag));
11222 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11224 const char *name = dwarf_attr_name (a->dw_attr);
11226 switch (AT_class (a))
11228 case dw_val_class_addr:
11229 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11230 break;
11232 case dw_val_class_offset:
11233 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11234 "%s", name);
11235 break;
11237 case dw_val_class_range_list:
11239 char *p = strchr (ranges_section_label, '\0');
11241 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11242 a->dw_attr_val.v.val_offset);
11243 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11244 debug_ranges_section, "%s", name);
11245 *p = '\0';
11247 break;
11249 case dw_val_class_loc:
11250 size = size_of_locs (AT_loc (a));
11252 /* Output the block length for this list of location operations. */
11253 if (dwarf_version >= 4)
11254 dw2_asm_output_data_uleb128 (size, "%s", name);
11255 else
11256 dw2_asm_output_data (constant_size (size), size, "%s", name);
11258 output_loc_sequence (AT_loc (a), -1);
11259 break;
11261 case dw_val_class_const:
11262 /* ??? It would be slightly more efficient to use a scheme like is
11263 used for unsigned constants below, but gdb 4.x does not sign
11264 extend. Gdb 5.x does sign extend. */
11265 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11266 break;
11268 case dw_val_class_unsigned_const:
11269 dw2_asm_output_data (constant_size (AT_unsigned (a)),
11270 AT_unsigned (a), "%s", name);
11271 break;
11273 case dw_val_class_const_double:
11275 unsigned HOST_WIDE_INT first, second;
11277 if (HOST_BITS_PER_WIDE_INT >= 64)
11278 dw2_asm_output_data (1,
11279 2 * HOST_BITS_PER_WIDE_INT
11280 / HOST_BITS_PER_CHAR,
11281 NULL);
11283 if (WORDS_BIG_ENDIAN)
11285 first = a->dw_attr_val.v.val_double.high;
11286 second = a->dw_attr_val.v.val_double.low;
11288 else
11290 first = a->dw_attr_val.v.val_double.low;
11291 second = a->dw_attr_val.v.val_double.high;
11294 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11295 first, name);
11296 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11297 second, NULL);
11299 break;
11301 case dw_val_class_vec:
11303 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11304 unsigned int len = a->dw_attr_val.v.val_vec.length;
11305 unsigned int i;
11306 unsigned char *p;
11308 dw2_asm_output_data (constant_size (len * elt_size),
11309 len * elt_size, "%s", name);
11310 if (elt_size > sizeof (HOST_WIDE_INT))
11312 elt_size /= 2;
11313 len *= 2;
11315 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11316 i < len;
11317 i++, p += elt_size)
11318 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11319 "fp or vector constant word %u", i);
11320 break;
11323 case dw_val_class_flag:
11324 if (dwarf_version >= 4)
11326 /* Currently all add_AT_flag calls pass in 1 as last argument,
11327 so DW_FORM_flag_present can be used. If that ever changes,
11328 we'll need to use DW_FORM_flag and have some optimization
11329 in build_abbrev_table that will change those to
11330 DW_FORM_flag_present if it is set to 1 in all DIEs using
11331 the same abbrev entry. */
11332 gcc_assert (AT_flag (a) == 1);
11333 if (flag_debug_asm)
11334 fprintf (asm_out_file, "\t\t\t%s %s\n",
11335 ASM_COMMENT_START, name);
11336 break;
11338 dw2_asm_output_data (1, AT_flag (a), "%s", name);
11339 break;
11341 case dw_val_class_loc_list:
11343 char *sym = AT_loc_list (a)->ll_symbol;
11345 gcc_assert (sym);
11346 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11347 "%s", name);
11349 break;
11351 case dw_val_class_die_ref:
11352 if (AT_ref_external (a))
11354 if (dwarf_version >= 4)
11356 comdat_type_node_ref type_node =
11357 AT_ref (a)->die_id.die_type_node;
11359 gcc_assert (type_node);
11360 output_signature (type_node->signature, name);
11362 else
11364 char *sym = AT_ref (a)->die_id.die_symbol;
11365 int size;
11367 gcc_assert (sym);
11368 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11369 length, whereas in DWARF3 it's always sized as an
11370 offset. */
11371 if (dwarf_version == 2)
11372 size = DWARF2_ADDR_SIZE;
11373 else
11374 size = DWARF_OFFSET_SIZE;
11375 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11376 name);
11379 else
11381 gcc_assert (AT_ref (a)->die_offset);
11382 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11383 "%s", name);
11385 break;
11387 case dw_val_class_fde_ref:
11389 char l1[20];
11391 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11392 a->dw_attr_val.v.val_fde_index * 2);
11393 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11394 "%s", name);
11396 break;
11398 case dw_val_class_vms_delta:
11399 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11400 AT_vms_delta2 (a), AT_vms_delta1 (a),
11401 "%s", name);
11402 break;
11404 case dw_val_class_lbl_id:
11405 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11406 break;
11408 case dw_val_class_lineptr:
11409 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11410 debug_line_section, "%s", name);
11411 break;
11413 case dw_val_class_macptr:
11414 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11415 debug_macinfo_section, "%s", name);
11416 break;
11418 case dw_val_class_str:
11419 if (AT_string_form (a) == DW_FORM_strp)
11420 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11421 a->dw_attr_val.v.val_str->label,
11422 debug_str_section,
11423 "%s: \"%s\"", name, AT_string (a));
11424 else
11425 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11426 break;
11428 case dw_val_class_file:
11430 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11432 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11433 a->dw_attr_val.v.val_file->filename);
11434 break;
11437 case dw_val_class_data8:
11439 int i;
11441 for (i = 0; i < 8; i++)
11442 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11443 i == 0 ? "%s" : NULL, name);
11444 break;
11447 default:
11448 gcc_unreachable ();
11452 FOR_EACH_CHILD (die, c, output_die (c));
11454 /* Add null byte to terminate sibling list. */
11455 if (die->die_child != NULL)
11456 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11457 (unsigned long) die->die_offset);
11460 /* Output the compilation unit that appears at the beginning of the
11461 .debug_info section, and precedes the DIE descriptions. */
11463 static void
11464 output_compilation_unit_header (void)
11466 int ver = dwarf_version;
11468 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11469 dw2_asm_output_data (4, 0xffffffff,
11470 "Initial length escape value indicating 64-bit DWARF extension");
11471 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11472 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11473 "Length of Compilation Unit Info");
11474 dw2_asm_output_data (2, ver, "DWARF version number");
11475 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11476 debug_abbrev_section,
11477 "Offset Into Abbrev. Section");
11478 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11481 /* Output the compilation unit DIE and its children. */
11483 static void
11484 output_comp_unit (dw_die_ref die, int output_if_empty)
11486 const char *secname;
11487 char *oldsym, *tmp;
11489 /* Unless we are outputting main CU, we may throw away empty ones. */
11490 if (!output_if_empty && die->die_child == NULL)
11491 return;
11493 /* Even if there are no children of this DIE, we must output the information
11494 about the compilation unit. Otherwise, on an empty translation unit, we
11495 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11496 will then complain when examining the file. First mark all the DIEs in
11497 this CU so we know which get local refs. */
11498 mark_dies (die);
11500 build_abbrev_table (die);
11502 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11503 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11504 calc_die_sizes (die);
11506 oldsym = die->die_id.die_symbol;
11507 if (oldsym)
11509 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11511 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11512 secname = tmp;
11513 die->die_id.die_symbol = NULL;
11514 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11516 else
11518 switch_to_section (debug_info_section);
11519 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11520 info_section_emitted = true;
11523 /* Output debugging information. */
11524 output_compilation_unit_header ();
11525 output_die (die);
11527 /* Leave the marks on the main CU, so we can check them in
11528 output_pubnames. */
11529 if (oldsym)
11531 unmark_dies (die);
11532 die->die_id.die_symbol = oldsym;
11536 /* Output a comdat type unit DIE and its children. */
11538 static void
11539 output_comdat_type_unit (comdat_type_node *node)
11541 const char *secname;
11542 char *tmp;
11543 int i;
11544 #if defined (OBJECT_FORMAT_ELF)
11545 tree comdat_key;
11546 #endif
11548 /* First mark all the DIEs in this CU so we know which get local refs. */
11549 mark_dies (node->root_die);
11551 build_abbrev_table (node->root_die);
11553 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11554 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11555 calc_die_sizes (node->root_die);
11557 #if defined (OBJECT_FORMAT_ELF)
11558 secname = ".debug_types";
11559 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11560 sprintf (tmp, "wt.");
11561 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11562 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11563 comdat_key = get_identifier (tmp);
11564 targetm.asm_out.named_section (secname,
11565 SECTION_DEBUG | SECTION_LINKONCE,
11566 comdat_key);
11567 #else
11568 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11569 sprintf (tmp, ".gnu.linkonce.wt.");
11570 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11571 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11572 secname = tmp;
11573 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11574 #endif
11576 /* Output debugging information. */
11577 output_compilation_unit_header ();
11578 output_signature (node->signature, "Type Signature");
11579 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11580 "Offset to Type DIE");
11581 output_die (node->root_die);
11583 unmark_dies (node->root_die);
11586 /* Return the DWARF2/3 pubname associated with a decl. */
11588 static const char *
11589 dwarf2_name (tree decl, int scope)
11591 if (DECL_NAMELESS (decl))
11592 return NULL;
11593 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11596 /* Add a new entry to .debug_pubnames if appropriate. */
11598 static void
11599 add_pubname_string (const char *str, dw_die_ref die)
11601 if (targetm.want_debug_pub_sections)
11603 pubname_entry e;
11605 e.die = die;
11606 e.name = xstrdup (str);
11607 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11611 static void
11612 add_pubname (tree decl, dw_die_ref die)
11614 if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11616 const char *name = dwarf2_name (decl, 1);
11617 if (name)
11618 add_pubname_string (name, die);
11622 /* Add a new entry to .debug_pubtypes if appropriate. */
11624 static void
11625 add_pubtype (tree decl, dw_die_ref die)
11627 pubname_entry e;
11629 if (!targetm.want_debug_pub_sections)
11630 return;
11632 e.name = NULL;
11633 if ((TREE_PUBLIC (decl)
11634 || is_cu_die (die->die_parent))
11635 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11637 e.die = die;
11638 if (TYPE_P (decl))
11640 if (TYPE_NAME (decl))
11642 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11643 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11644 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11645 && DECL_NAME (TYPE_NAME (decl)))
11646 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11647 else
11648 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11651 else
11653 e.name = dwarf2_name (decl, 1);
11654 if (e.name)
11655 e.name = xstrdup (e.name);
11658 /* If we don't have a name for the type, there's no point in adding
11659 it to the table. */
11660 if (e.name && e.name[0] != '\0')
11661 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11665 /* Output the public names table used to speed up access to externally
11666 visible names; or the public types table used to find type definitions. */
11668 static void
11669 output_pubnames (VEC (pubname_entry, gc) * names)
11671 unsigned i;
11672 unsigned long pubnames_length = size_of_pubnames (names);
11673 pubname_ref pub;
11675 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11676 dw2_asm_output_data (4, 0xffffffff,
11677 "Initial length escape value indicating 64-bit DWARF extension");
11678 if (names == pubname_table)
11679 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11680 "Length of Public Names Info");
11681 else
11682 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11683 "Length of Public Type Names Info");
11684 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
11685 dw2_asm_output_data (2, 2, "DWARF Version");
11686 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11687 debug_info_section,
11688 "Offset of Compilation Unit Info");
11689 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11690 "Compilation Unit Length");
11692 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11694 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11695 if (names == pubname_table)
11696 gcc_assert (pub->die->die_mark);
11698 if (names != pubtype_table
11699 || pub->die->die_offset != 0
11700 || !flag_eliminate_unused_debug_types)
11702 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11703 "DIE offset");
11705 dw2_asm_output_nstring (pub->name, -1, "external name");
11709 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11712 /* Add a new entry to .debug_aranges if appropriate. */
11714 static void
11715 add_arange (tree decl, dw_die_ref die)
11717 if (! DECL_SECTION_NAME (decl))
11718 return;
11720 if (arange_table_in_use == arange_table_allocated)
11722 arange_table_allocated += ARANGE_TABLE_INCREMENT;
11723 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11724 arange_table_allocated);
11725 memset (arange_table + arange_table_in_use, 0,
11726 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11729 arange_table[arange_table_in_use++] = die;
11732 /* Output the information that goes into the .debug_aranges table.
11733 Namely, define the beginning and ending address range of the
11734 text section generated for this compilation unit. */
11736 static void
11737 output_aranges (void)
11739 unsigned i;
11740 unsigned long aranges_length = size_of_aranges ();
11742 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11743 dw2_asm_output_data (4, 0xffffffff,
11744 "Initial length escape value indicating 64-bit DWARF extension");
11745 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11746 "Length of Address Ranges Info");
11747 /* Version number for aranges is still 2, even in DWARF3. */
11748 dw2_asm_output_data (2, 2, "DWARF Version");
11749 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11750 debug_info_section,
11751 "Offset of Compilation Unit Info");
11752 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11753 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11755 /* We need to align to twice the pointer size here. */
11756 if (DWARF_ARANGES_PAD_SIZE)
11758 /* Pad using a 2 byte words so that padding is correct for any
11759 pointer size. */
11760 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11761 2 * DWARF2_ADDR_SIZE);
11762 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11763 dw2_asm_output_data (2, 0, NULL);
11766 /* It is necessary not to output these entries if the sections were
11767 not used; if the sections were not used, the length will be 0 and
11768 the address may end up as 0 if the section is discarded by ld
11769 --gc-sections, leaving an invalid (0, 0) entry that can be
11770 confused with the terminator. */
11771 if (text_section_used)
11773 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11774 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11775 text_section_label, "Length");
11777 if (cold_text_section_used)
11779 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11780 "Address");
11781 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11782 cold_text_section_label, "Length");
11785 for (i = 0; i < arange_table_in_use; i++)
11787 dw_die_ref die = arange_table[i];
11789 /* We shouldn't see aranges for DIEs outside of the main CU. */
11790 gcc_assert (die->die_mark);
11792 if (die->die_tag == DW_TAG_subprogram)
11794 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11795 "Address");
11796 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11797 get_AT_low_pc (die), "Length");
11799 else
11801 /* A static variable; extract the symbol from DW_AT_location.
11802 Note that this code isn't currently hit, as we only emit
11803 aranges for functions (jason 9/23/99). */
11804 dw_attr_ref a = get_AT (die, DW_AT_location);
11805 dw_loc_descr_ref loc;
11807 gcc_assert (a && AT_class (a) == dw_val_class_loc);
11809 loc = AT_loc (a);
11810 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11812 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11813 loc->dw_loc_oprnd1.v.val_addr, "Address");
11814 dw2_asm_output_data (DWARF2_ADDR_SIZE,
11815 get_AT_unsigned (die, DW_AT_byte_size),
11816 "Length");
11820 /* Output the terminator words. */
11821 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11822 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11825 /* Add a new entry to .debug_ranges. Return the offset at which it
11826 was placed. */
11828 static unsigned int
11829 add_ranges_num (int num)
11831 unsigned int in_use = ranges_table_in_use;
11833 if (in_use == ranges_table_allocated)
11835 ranges_table_allocated += RANGES_TABLE_INCREMENT;
11836 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11837 ranges_table_allocated);
11838 memset (ranges_table + ranges_table_in_use, 0,
11839 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11842 ranges_table[in_use].num = num;
11843 ranges_table_in_use = in_use + 1;
11845 return in_use * 2 * DWARF2_ADDR_SIZE;
11848 /* Add a new entry to .debug_ranges corresponding to a block, or a
11849 range terminator if BLOCK is NULL. */
11851 static unsigned int
11852 add_ranges (const_tree block)
11854 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11857 /* Add a new entry to .debug_ranges corresponding to a pair of
11858 labels. */
11860 static void
11861 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11862 bool *added)
11864 unsigned int in_use = ranges_by_label_in_use;
11865 unsigned int offset;
11867 if (in_use == ranges_by_label_allocated)
11869 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11870 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11871 ranges_by_label,
11872 ranges_by_label_allocated);
11873 memset (ranges_by_label + ranges_by_label_in_use, 0,
11874 RANGES_TABLE_INCREMENT
11875 * sizeof (struct dw_ranges_by_label_struct));
11878 ranges_by_label[in_use].begin = begin;
11879 ranges_by_label[in_use].end = end;
11880 ranges_by_label_in_use = in_use + 1;
11882 offset = add_ranges_num (-(int)in_use - 1);
11883 if (!*added)
11885 add_AT_range_list (die, DW_AT_ranges, offset);
11886 *added = true;
11890 static void
11891 output_ranges (void)
11893 unsigned i;
11894 static const char *const start_fmt = "Offset %#x";
11895 const char *fmt = start_fmt;
11897 for (i = 0; i < ranges_table_in_use; i++)
11899 int block_num = ranges_table[i].num;
11901 if (block_num > 0)
11903 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11904 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11906 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11907 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11909 /* If all code is in the text section, then the compilation
11910 unit base address defaults to DW_AT_low_pc, which is the
11911 base of the text section. */
11912 if (!have_multiple_function_sections)
11914 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11915 text_section_label,
11916 fmt, i * 2 * DWARF2_ADDR_SIZE);
11917 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11918 text_section_label, NULL);
11921 /* Otherwise, the compilation unit base address is zero,
11922 which allows us to use absolute addresses, and not worry
11923 about whether the target supports cross-section
11924 arithmetic. */
11925 else
11927 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11928 fmt, i * 2 * DWARF2_ADDR_SIZE);
11929 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11932 fmt = NULL;
11935 /* Negative block_num stands for an index into ranges_by_label. */
11936 else if (block_num < 0)
11938 int lab_idx = - block_num - 1;
11940 if (!have_multiple_function_sections)
11942 gcc_unreachable ();
11943 #if 0
11944 /* If we ever use add_ranges_by_labels () for a single
11945 function section, all we have to do is to take out
11946 the #if 0 above. */
11947 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11948 ranges_by_label[lab_idx].begin,
11949 text_section_label,
11950 fmt, i * 2 * DWARF2_ADDR_SIZE);
11951 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11952 ranges_by_label[lab_idx].end,
11953 text_section_label, NULL);
11954 #endif
11956 else
11958 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11959 ranges_by_label[lab_idx].begin,
11960 fmt, i * 2 * DWARF2_ADDR_SIZE);
11961 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11962 ranges_by_label[lab_idx].end,
11963 NULL);
11966 else
11968 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11969 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11970 fmt = start_fmt;
11975 /* Data structure containing information about input files. */
11976 struct file_info
11978 const char *path; /* Complete file name. */
11979 const char *fname; /* File name part. */
11980 int length; /* Length of entire string. */
11981 struct dwarf_file_data * file_idx; /* Index in input file table. */
11982 int dir_idx; /* Index in directory table. */
11985 /* Data structure containing information about directories with source
11986 files. */
11987 struct dir_info
11989 const char *path; /* Path including directory name. */
11990 int length; /* Path length. */
11991 int prefix; /* Index of directory entry which is a prefix. */
11992 int count; /* Number of files in this directory. */
11993 int dir_idx; /* Index of directory used as base. */
11996 /* Callback function for file_info comparison. We sort by looking at
11997 the directories in the path. */
11999 static int
12000 file_info_cmp (const void *p1, const void *p2)
12002 const struct file_info *const s1 = (const struct file_info *) p1;
12003 const struct file_info *const s2 = (const struct file_info *) p2;
12004 const unsigned char *cp1;
12005 const unsigned char *cp2;
12007 /* Take care of file names without directories. We need to make sure that
12008 we return consistent values to qsort since some will get confused if
12009 we return the same value when identical operands are passed in opposite
12010 orders. So if neither has a directory, return 0 and otherwise return
12011 1 or -1 depending on which one has the directory. */
12012 if ((s1->path == s1->fname || s2->path == s2->fname))
12013 return (s2->path == s2->fname) - (s1->path == s1->fname);
12015 cp1 = (const unsigned char *) s1->path;
12016 cp2 = (const unsigned char *) s2->path;
12018 while (1)
12020 ++cp1;
12021 ++cp2;
12022 /* Reached the end of the first path? If so, handle like above. */
12023 if ((cp1 == (const unsigned char *) s1->fname)
12024 || (cp2 == (const unsigned char *) s2->fname))
12025 return ((cp2 == (const unsigned char *) s2->fname)
12026 - (cp1 == (const unsigned char *) s1->fname));
12028 /* Character of current path component the same? */
12029 else if (*cp1 != *cp2)
12030 return *cp1 - *cp2;
12034 struct file_name_acquire_data
12036 struct file_info *files;
12037 int used_files;
12038 int max_files;
12041 /* Traversal function for the hash table. */
12043 static int
12044 file_name_acquire (void ** slot, void *data)
12046 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12047 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12048 struct file_info *fi;
12049 const char *f;
12051 gcc_assert (fnad->max_files >= d->emitted_number);
12053 if (! d->emitted_number)
12054 return 1;
12056 gcc_assert (fnad->max_files != fnad->used_files);
12058 fi = fnad->files + fnad->used_files++;
12060 /* Skip all leading "./". */
12061 f = d->filename;
12062 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12063 f += 2;
12065 /* Create a new array entry. */
12066 fi->path = f;
12067 fi->length = strlen (f);
12068 fi->file_idx = d;
12070 /* Search for the file name part. */
12071 f = strrchr (f, DIR_SEPARATOR);
12072 #if defined (DIR_SEPARATOR_2)
12074 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12076 if (g != NULL)
12078 if (f == NULL || f < g)
12079 f = g;
12082 #endif
12084 fi->fname = f == NULL ? fi->path : f + 1;
12085 return 1;
12088 /* Output the directory table and the file name table. We try to minimize
12089 the total amount of memory needed. A heuristic is used to avoid large
12090 slowdowns with many input files. */
12092 static void
12093 output_file_names (void)
12095 struct file_name_acquire_data fnad;
12096 int numfiles;
12097 struct file_info *files;
12098 struct dir_info *dirs;
12099 int *saved;
12100 int *savehere;
12101 int *backmap;
12102 int ndirs;
12103 int idx_offset;
12104 int i;
12106 if (!last_emitted_file)
12108 dw2_asm_output_data (1, 0, "End directory table");
12109 dw2_asm_output_data (1, 0, "End file name table");
12110 return;
12113 numfiles = last_emitted_file->emitted_number;
12115 /* Allocate the various arrays we need. */
12116 files = XALLOCAVEC (struct file_info, numfiles);
12117 dirs = XALLOCAVEC (struct dir_info, numfiles);
12119 fnad.files = files;
12120 fnad.used_files = 0;
12121 fnad.max_files = numfiles;
12122 htab_traverse (file_table, file_name_acquire, &fnad);
12123 gcc_assert (fnad.used_files == fnad.max_files);
12125 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12127 /* Find all the different directories used. */
12128 dirs[0].path = files[0].path;
12129 dirs[0].length = files[0].fname - files[0].path;
12130 dirs[0].prefix = -1;
12131 dirs[0].count = 1;
12132 dirs[0].dir_idx = 0;
12133 files[0].dir_idx = 0;
12134 ndirs = 1;
12136 for (i = 1; i < numfiles; i++)
12137 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12138 && memcmp (dirs[ndirs - 1].path, files[i].path,
12139 dirs[ndirs - 1].length) == 0)
12141 /* Same directory as last entry. */
12142 files[i].dir_idx = ndirs - 1;
12143 ++dirs[ndirs - 1].count;
12145 else
12147 int j;
12149 /* This is a new directory. */
12150 dirs[ndirs].path = files[i].path;
12151 dirs[ndirs].length = files[i].fname - files[i].path;
12152 dirs[ndirs].count = 1;
12153 dirs[ndirs].dir_idx = ndirs;
12154 files[i].dir_idx = ndirs;
12156 /* Search for a prefix. */
12157 dirs[ndirs].prefix = -1;
12158 for (j = 0; j < ndirs; j++)
12159 if (dirs[j].length < dirs[ndirs].length
12160 && dirs[j].length > 1
12161 && (dirs[ndirs].prefix == -1
12162 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12163 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12164 dirs[ndirs].prefix = j;
12166 ++ndirs;
12169 /* Now to the actual work. We have to find a subset of the directories which
12170 allow expressing the file name using references to the directory table
12171 with the least amount of characters. We do not do an exhaustive search
12172 where we would have to check out every combination of every single
12173 possible prefix. Instead we use a heuristic which provides nearly optimal
12174 results in most cases and never is much off. */
12175 saved = XALLOCAVEC (int, ndirs);
12176 savehere = XALLOCAVEC (int, ndirs);
12178 memset (saved, '\0', ndirs * sizeof (saved[0]));
12179 for (i = 0; i < ndirs; i++)
12181 int j;
12182 int total;
12184 /* We can always save some space for the current directory. But this
12185 does not mean it will be enough to justify adding the directory. */
12186 savehere[i] = dirs[i].length;
12187 total = (savehere[i] - saved[i]) * dirs[i].count;
12189 for (j = i + 1; j < ndirs; j++)
12191 savehere[j] = 0;
12192 if (saved[j] < dirs[i].length)
12194 /* Determine whether the dirs[i] path is a prefix of the
12195 dirs[j] path. */
12196 int k;
12198 k = dirs[j].prefix;
12199 while (k != -1 && k != (int) i)
12200 k = dirs[k].prefix;
12202 if (k == (int) i)
12204 /* Yes it is. We can possibly save some memory by
12205 writing the filenames in dirs[j] relative to
12206 dirs[i]. */
12207 savehere[j] = dirs[i].length;
12208 total += (savehere[j] - saved[j]) * dirs[j].count;
12213 /* Check whether we can save enough to justify adding the dirs[i]
12214 directory. */
12215 if (total > dirs[i].length + 1)
12217 /* It's worthwhile adding. */
12218 for (j = i; j < ndirs; j++)
12219 if (savehere[j] > 0)
12221 /* Remember how much we saved for this directory so far. */
12222 saved[j] = savehere[j];
12224 /* Remember the prefix directory. */
12225 dirs[j].dir_idx = i;
12230 /* Emit the directory name table. */
12231 idx_offset = dirs[0].length > 0 ? 1 : 0;
12232 for (i = 1 - idx_offset; i < ndirs; i++)
12233 dw2_asm_output_nstring (dirs[i].path,
12234 dirs[i].length
12235 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12236 "Directory Entry: %#x", i + idx_offset);
12238 dw2_asm_output_data (1, 0, "End directory table");
12240 /* We have to emit them in the order of emitted_number since that's
12241 used in the debug info generation. To do this efficiently we
12242 generate a back-mapping of the indices first. */
12243 backmap = XALLOCAVEC (int, numfiles);
12244 for (i = 0; i < numfiles; i++)
12245 backmap[files[i].file_idx->emitted_number - 1] = i;
12247 /* Now write all the file names. */
12248 for (i = 0; i < numfiles; i++)
12250 int file_idx = backmap[i];
12251 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12253 #ifdef VMS_DEBUGGING_INFO
12254 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12256 /* Setting these fields can lead to debugger miscomparisons,
12257 but VMS Debug requires them to be set correctly. */
12259 int ver;
12260 long long cdt;
12261 long siz;
12262 int maxfilelen = strlen (files[file_idx].path)
12263 + dirs[dir_idx].length
12264 + MAX_VMS_VERSION_LEN + 1;
12265 char *filebuf = XALLOCAVEC (char, maxfilelen);
12267 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12268 snprintf (filebuf, maxfilelen, "%s;%d",
12269 files[file_idx].path + dirs[dir_idx].length, ver);
12271 dw2_asm_output_nstring
12272 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12274 /* Include directory index. */
12275 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12277 /* Modification time. */
12278 dw2_asm_output_data_uleb128
12279 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12280 ? cdt : 0,
12281 NULL);
12283 /* File length in bytes. */
12284 dw2_asm_output_data_uleb128
12285 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12286 ? siz : 0,
12287 NULL);
12288 #else
12289 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12290 "File Entry: %#x", (unsigned) i + 1);
12292 /* Include directory index. */
12293 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12295 /* Modification time. */
12296 dw2_asm_output_data_uleb128 (0, NULL);
12298 /* File length in bytes. */
12299 dw2_asm_output_data_uleb128 (0, NULL);
12300 #endif /* VMS_DEBUGGING_INFO */
12303 dw2_asm_output_data (1, 0, "End file name table");
12307 /* Output the source line number correspondence information. This
12308 information goes into the .debug_line section. */
12310 static void
12311 output_line_info (void)
12313 char l1[20], l2[20], p1[20], p2[20];
12314 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12315 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12316 unsigned opc;
12317 unsigned n_op_args;
12318 unsigned long lt_index;
12319 unsigned long current_line;
12320 long line_offset;
12321 long line_delta;
12322 unsigned long current_file;
12323 unsigned long function;
12324 int ver = dwarf_version;
12326 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12327 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12328 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12329 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12331 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12332 dw2_asm_output_data (4, 0xffffffff,
12333 "Initial length escape value indicating 64-bit DWARF extension");
12334 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12335 "Length of Source Line Info");
12336 ASM_OUTPUT_LABEL (asm_out_file, l1);
12338 dw2_asm_output_data (2, ver, "DWARF Version");
12339 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12340 ASM_OUTPUT_LABEL (asm_out_file, p1);
12342 /* Define the architecture-dependent minimum instruction length (in
12343 bytes). In this implementation of DWARF, this field is used for
12344 information purposes only. Since GCC generates assembly language,
12345 we have no a priori knowledge of how many instruction bytes are
12346 generated for each source line, and therefore can use only the
12347 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12348 commands. Accordingly, we fix this as `1', which is "correct
12349 enough" for all architectures, and don't let the target override. */
12350 dw2_asm_output_data (1, 1,
12351 "Minimum Instruction Length");
12353 if (ver >= 4)
12354 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12355 "Maximum Operations Per Instruction");
12356 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12357 "Default is_stmt_start flag");
12358 dw2_asm_output_data (1, DWARF_LINE_BASE,
12359 "Line Base Value (Special Opcodes)");
12360 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12361 "Line Range Value (Special Opcodes)");
12362 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12363 "Special Opcode Base");
12365 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12367 switch (opc)
12369 case DW_LNS_advance_pc:
12370 case DW_LNS_advance_line:
12371 case DW_LNS_set_file:
12372 case DW_LNS_set_column:
12373 case DW_LNS_fixed_advance_pc:
12374 n_op_args = 1;
12375 break;
12376 default:
12377 n_op_args = 0;
12378 break;
12381 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12382 opc, n_op_args);
12385 /* Write out the information about the files we use. */
12386 output_file_names ();
12387 ASM_OUTPUT_LABEL (asm_out_file, p2);
12389 /* We used to set the address register to the first location in the text
12390 section here, but that didn't accomplish anything since we already
12391 have a line note for the opening brace of the first function. */
12393 /* Generate the line number to PC correspondence table, encoded as
12394 a series of state machine operations. */
12395 current_file = 1;
12396 current_line = 1;
12398 if (cfun && in_cold_section_p)
12399 strcpy (prev_line_label, crtl->subsections.cold_section_label);
12400 else
12401 strcpy (prev_line_label, text_section_label);
12402 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12404 dw_line_info_ref line_info = &line_info_table[lt_index];
12406 #if 0
12407 /* Disable this optimization for now; GDB wants to see two line notes
12408 at the beginning of a function so it can find the end of the
12409 prologue. */
12411 /* Don't emit anything for redundant notes. Just updating the
12412 address doesn't accomplish anything, because we already assume
12413 that anything after the last address is this line. */
12414 if (line_info->dw_line_num == current_line
12415 && line_info->dw_file_num == current_file)
12416 continue;
12417 #endif
12419 /* Emit debug info for the address of the current line.
12421 Unfortunately, we have little choice here currently, and must always
12422 use the most general form. GCC does not know the address delta
12423 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
12424 attributes which will give an upper bound on the address range. We
12425 could perhaps use length attributes to determine when it is safe to
12426 use DW_LNS_fixed_advance_pc. */
12428 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12429 if (0)
12431 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
12432 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12433 "DW_LNS_fixed_advance_pc");
12434 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12436 else
12438 /* This can handle any delta. This takes
12439 4+DWARF2_ADDR_SIZE bytes. */
12440 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12441 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12442 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12443 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12446 strcpy (prev_line_label, line_label);
12448 /* Emit debug info for the source file of the current line, if
12449 different from the previous line. */
12450 if (line_info->dw_file_num != current_file)
12452 current_file = line_info->dw_file_num;
12453 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12454 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12457 /* Emit debug info for the current line number, choosing the encoding
12458 that uses the least amount of space. */
12459 if (line_info->dw_line_num != current_line)
12461 line_offset = line_info->dw_line_num - current_line;
12462 line_delta = line_offset - DWARF_LINE_BASE;
12463 current_line = line_info->dw_line_num;
12464 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12465 /* This can handle deltas from -10 to 234, using the current
12466 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
12467 takes 1 byte. */
12468 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12469 "line %lu", current_line);
12470 else
12472 /* This can handle any delta. This takes at least 4 bytes,
12473 depending on the value being encoded. */
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 /* We still need to start a new row, so output a copy insn. */
12482 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12485 /* Emit debug info for the address of the end of the function. */
12486 if (0)
12488 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12489 "DW_LNS_fixed_advance_pc");
12490 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12492 else
12494 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12495 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12496 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12497 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12500 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12501 dw2_asm_output_data_uleb128 (1, NULL);
12502 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12504 function = 0;
12505 current_file = 1;
12506 current_line = 1;
12507 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12509 dw_separate_line_info_ref line_info
12510 = &separate_line_info_table[lt_index];
12512 #if 0
12513 /* Don't emit anything for redundant notes. */
12514 if (line_info->dw_line_num == current_line
12515 && line_info->dw_file_num == current_file
12516 && line_info->function == function)
12517 goto cont;
12518 #endif
12520 /* Emit debug info for the address of the current line. If this is
12521 a new function, or the first line of a function, then we need
12522 to handle it differently. */
12523 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12524 lt_index);
12525 if (function != line_info->function)
12527 function = line_info->function;
12529 /* Set the address register to the first line in the function. */
12530 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12531 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12532 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12533 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12535 else
12537 /* ??? See the DW_LNS_advance_pc comment above. */
12538 if (0)
12540 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12541 "DW_LNS_fixed_advance_pc");
12542 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12544 else
12546 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12547 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12548 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12549 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12553 strcpy (prev_line_label, line_label);
12555 /* Emit debug info for the source file of the current line, if
12556 different from the previous line. */
12557 if (line_info->dw_file_num != current_file)
12559 current_file = line_info->dw_file_num;
12560 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12561 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12564 /* Emit debug info for the current line number, choosing the encoding
12565 that uses the least amount of space. */
12566 if (line_info->dw_line_num != current_line)
12568 line_offset = line_info->dw_line_num - current_line;
12569 line_delta = line_offset - DWARF_LINE_BASE;
12570 current_line = line_info->dw_line_num;
12571 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12572 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12573 "line %lu", current_line);
12574 else
12576 dw2_asm_output_data (1, DW_LNS_advance_line,
12577 "advance to line %lu", current_line);
12578 dw2_asm_output_data_sleb128 (line_offset, NULL);
12579 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12582 else
12583 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12585 #if 0
12586 cont:
12587 #endif
12589 lt_index++;
12591 /* If we're done with a function, end its sequence. */
12592 if (lt_index == separate_line_info_table_in_use
12593 || separate_line_info_table[lt_index].function != function)
12595 current_file = 1;
12596 current_line = 1;
12598 /* Emit debug info for the address of the end of the function. */
12599 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12600 if (0)
12602 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12603 "DW_LNS_fixed_advance_pc");
12604 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12606 else
12608 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12609 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12610 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12611 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12614 /* Output the marker for the end of this sequence. */
12615 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12616 dw2_asm_output_data_uleb128 (1, NULL);
12617 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12621 /* Output the marker for the end of the line number info. */
12622 ASM_OUTPUT_LABEL (asm_out_file, l2);
12625 /* Given a pointer to a tree node for some base type, return a pointer to
12626 a DIE that describes the given type.
12628 This routine must only be called for GCC type nodes that correspond to
12629 Dwarf base (fundamental) types. */
12631 static dw_die_ref
12632 base_type_die (tree type)
12634 dw_die_ref base_type_result;
12635 enum dwarf_type encoding;
12637 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12638 return 0;
12640 /* If this is a subtype that should not be emitted as a subrange type,
12641 use the base type. See subrange_type_for_debug_p. */
12642 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12643 type = TREE_TYPE (type);
12645 switch (TREE_CODE (type))
12647 case INTEGER_TYPE:
12648 if ((dwarf_version >= 4 || !dwarf_strict)
12649 && TYPE_NAME (type)
12650 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12651 && DECL_IS_BUILTIN (TYPE_NAME (type))
12652 && DECL_NAME (TYPE_NAME (type)))
12654 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12655 if (strcmp (name, "char16_t") == 0
12656 || strcmp (name, "char32_t") == 0)
12658 encoding = DW_ATE_UTF;
12659 break;
12662 if (TYPE_STRING_FLAG (type))
12664 if (TYPE_UNSIGNED (type))
12665 encoding = DW_ATE_unsigned_char;
12666 else
12667 encoding = DW_ATE_signed_char;
12669 else if (TYPE_UNSIGNED (type))
12670 encoding = DW_ATE_unsigned;
12671 else
12672 encoding = DW_ATE_signed;
12673 break;
12675 case REAL_TYPE:
12676 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12678 if (dwarf_version >= 3 || !dwarf_strict)
12679 encoding = DW_ATE_decimal_float;
12680 else
12681 encoding = DW_ATE_lo_user;
12683 else
12684 encoding = DW_ATE_float;
12685 break;
12687 case FIXED_POINT_TYPE:
12688 if (!(dwarf_version >= 3 || !dwarf_strict))
12689 encoding = DW_ATE_lo_user;
12690 else if (TYPE_UNSIGNED (type))
12691 encoding = DW_ATE_unsigned_fixed;
12692 else
12693 encoding = DW_ATE_signed_fixed;
12694 break;
12696 /* Dwarf2 doesn't know anything about complex ints, so use
12697 a user defined type for it. */
12698 case COMPLEX_TYPE:
12699 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12700 encoding = DW_ATE_complex_float;
12701 else
12702 encoding = DW_ATE_lo_user;
12703 break;
12705 case BOOLEAN_TYPE:
12706 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12707 encoding = DW_ATE_boolean;
12708 break;
12710 default:
12711 /* No other TREE_CODEs are Dwarf fundamental types. */
12712 gcc_unreachable ();
12715 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12717 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12718 int_size_in_bytes (type));
12719 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12721 return base_type_result;
12724 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12725 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12727 static inline int
12728 is_base_type (tree type)
12730 switch (TREE_CODE (type))
12732 case ERROR_MARK:
12733 case VOID_TYPE:
12734 case INTEGER_TYPE:
12735 case REAL_TYPE:
12736 case FIXED_POINT_TYPE:
12737 case COMPLEX_TYPE:
12738 case BOOLEAN_TYPE:
12739 return 1;
12741 case ARRAY_TYPE:
12742 case RECORD_TYPE:
12743 case UNION_TYPE:
12744 case QUAL_UNION_TYPE:
12745 case ENUMERAL_TYPE:
12746 case FUNCTION_TYPE:
12747 case METHOD_TYPE:
12748 case POINTER_TYPE:
12749 case REFERENCE_TYPE:
12750 case NULLPTR_TYPE:
12751 case OFFSET_TYPE:
12752 case LANG_TYPE:
12753 case VECTOR_TYPE:
12754 return 0;
12756 default:
12757 gcc_unreachable ();
12760 return 0;
12763 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12764 node, return the size in bits for the type if it is a constant, or else
12765 return the alignment for the type if the type's size is not constant, or
12766 else return BITS_PER_WORD if the type actually turns out to be an
12767 ERROR_MARK node. */
12769 static inline unsigned HOST_WIDE_INT
12770 simple_type_size_in_bits (const_tree type)
12772 if (TREE_CODE (type) == ERROR_MARK)
12773 return BITS_PER_WORD;
12774 else if (TYPE_SIZE (type) == NULL_TREE)
12775 return 0;
12776 else if (host_integerp (TYPE_SIZE (type), 1))
12777 return tree_low_cst (TYPE_SIZE (type), 1);
12778 else
12779 return TYPE_ALIGN (type);
12782 /* Similarly, but return a double_int instead of UHWI. */
12784 static inline double_int
12785 double_int_type_size_in_bits (const_tree type)
12787 if (TREE_CODE (type) == ERROR_MARK)
12788 return uhwi_to_double_int (BITS_PER_WORD);
12789 else if (TYPE_SIZE (type) == NULL_TREE)
12790 return double_int_zero;
12791 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12792 return tree_to_double_int (TYPE_SIZE (type));
12793 else
12794 return uhwi_to_double_int (TYPE_ALIGN (type));
12797 /* Given a pointer to a tree node for a subrange type, return a pointer
12798 to a DIE that describes the given type. */
12800 static dw_die_ref
12801 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12803 dw_die_ref subrange_die;
12804 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12806 if (context_die == NULL)
12807 context_die = comp_unit_die ();
12809 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12811 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12813 /* The size of the subrange type and its base type do not match,
12814 so we need to generate a size attribute for the subrange type. */
12815 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12818 if (low)
12819 add_bound_info (subrange_die, DW_AT_lower_bound, low);
12820 if (high)
12821 add_bound_info (subrange_die, DW_AT_upper_bound, high);
12823 return subrange_die;
12826 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12827 entry that chains various modifiers in front of the given type. */
12829 static dw_die_ref
12830 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12831 dw_die_ref context_die)
12833 enum tree_code code = TREE_CODE (type);
12834 dw_die_ref mod_type_die;
12835 dw_die_ref sub_die = NULL;
12836 tree item_type = NULL;
12837 tree qualified_type;
12838 tree name, low, high;
12840 if (code == ERROR_MARK)
12841 return NULL;
12843 /* See if we already have the appropriately qualified variant of
12844 this type. */
12845 qualified_type
12846 = get_qualified_type (type,
12847 ((is_const_type ? TYPE_QUAL_CONST : 0)
12848 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12850 if (qualified_type == sizetype
12851 && TYPE_NAME (qualified_type)
12852 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12854 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12856 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12857 && TYPE_PRECISION (t)
12858 == TYPE_PRECISION (qualified_type)
12859 && TYPE_UNSIGNED (t)
12860 == TYPE_UNSIGNED (qualified_type));
12861 qualified_type = t;
12864 /* If we do, then we can just use its DIE, if it exists. */
12865 if (qualified_type)
12867 mod_type_die = lookup_type_die (qualified_type);
12868 if (mod_type_die)
12869 return mod_type_die;
12872 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12874 /* Handle C typedef types. */
12875 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12876 && !DECL_ARTIFICIAL (name))
12878 tree dtype = TREE_TYPE (name);
12880 if (qualified_type == dtype)
12882 /* For a named type, use the typedef. */
12883 gen_type_die (qualified_type, context_die);
12884 return lookup_type_die (qualified_type);
12886 else if (is_const_type < TYPE_READONLY (dtype)
12887 || is_volatile_type < TYPE_VOLATILE (dtype)
12888 || (is_const_type <= TYPE_READONLY (dtype)
12889 && is_volatile_type <= TYPE_VOLATILE (dtype)
12890 && DECL_ORIGINAL_TYPE (name) != type))
12891 /* cv-unqualified version of named type. Just use the unnamed
12892 type to which it refers. */
12893 return modified_type_die (DECL_ORIGINAL_TYPE (name),
12894 is_const_type, is_volatile_type,
12895 context_die);
12896 /* Else cv-qualified version of named type; fall through. */
12899 if (is_const_type
12900 /* If both is_const_type and is_volatile_type, prefer the path
12901 which leads to a qualified type. */
12902 && (!is_volatile_type
12903 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12904 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12906 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12907 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12909 else if (is_volatile_type)
12911 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12912 sub_die = modified_type_die (type, is_const_type, 0, context_die);
12914 else if (code == POINTER_TYPE)
12916 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12917 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12918 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12919 item_type = TREE_TYPE (type);
12920 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12921 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12922 TYPE_ADDR_SPACE (item_type));
12924 else if (code == REFERENCE_TYPE)
12926 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12927 mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12928 type);
12929 else
12930 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12931 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12932 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12933 item_type = TREE_TYPE (type);
12934 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12935 add_AT_unsigned (mod_type_die, DW_AT_address_class,
12936 TYPE_ADDR_SPACE (item_type));
12938 else if (code == INTEGER_TYPE
12939 && TREE_TYPE (type) != NULL_TREE
12940 && subrange_type_for_debug_p (type, &low, &high))
12942 mod_type_die = subrange_type_die (type, low, high, context_die);
12943 item_type = TREE_TYPE (type);
12945 else if (is_base_type (type))
12946 mod_type_die = base_type_die (type);
12947 else
12949 gen_type_die (type, context_die);
12951 /* We have to get the type_main_variant here (and pass that to the
12952 `lookup_type_die' routine) because the ..._TYPE node we have
12953 might simply be a *copy* of some original type node (where the
12954 copy was created to help us keep track of typedef names) and
12955 that copy might have a different TYPE_UID from the original
12956 ..._TYPE node. */
12957 if (TREE_CODE (type) != VECTOR_TYPE)
12958 return lookup_type_die (type_main_variant (type));
12959 else
12960 /* Vectors have the debugging information in the type,
12961 not the main variant. */
12962 return lookup_type_die (type);
12965 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12966 don't output a DW_TAG_typedef, since there isn't one in the
12967 user's program; just attach a DW_AT_name to the type.
12968 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12969 if the base type already has the same name. */
12970 if (name
12971 && ((TREE_CODE (name) != TYPE_DECL
12972 && (qualified_type == TYPE_MAIN_VARIANT (type)
12973 || (!is_const_type && !is_volatile_type)))
12974 || (TREE_CODE (name) == TYPE_DECL
12975 && TREE_TYPE (name) == qualified_type
12976 && DECL_NAME (name))))
12978 if (TREE_CODE (name) == TYPE_DECL)
12979 /* Could just call add_name_and_src_coords_attributes here,
12980 but since this is a builtin type it doesn't have any
12981 useful source coordinates anyway. */
12982 name = DECL_NAME (name);
12983 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12985 /* This probably indicates a bug. */
12986 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12987 add_name_attribute (mod_type_die, "__unknown__");
12989 if (qualified_type)
12990 equate_type_number_to_die (qualified_type, mod_type_die);
12992 if (item_type)
12993 /* We must do this after the equate_type_number_to_die call, in case
12994 this is a recursive type. This ensures that the modified_type_die
12995 recursion will terminate even if the type is recursive. Recursive
12996 types are possible in Ada. */
12997 sub_die = modified_type_die (item_type,
12998 TYPE_READONLY (item_type),
12999 TYPE_VOLATILE (item_type),
13000 context_die);
13002 if (sub_die != NULL)
13003 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13005 return mod_type_die;
13008 /* Generate DIEs for the generic parameters of T.
13009 T must be either a generic type or a generic function.
13010 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13012 static void
13013 gen_generic_params_dies (tree t)
13015 tree parms, args;
13016 int parms_num, i;
13017 dw_die_ref die = NULL;
13019 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13020 return;
13022 if (TYPE_P (t))
13023 die = lookup_type_die (t);
13024 else if (DECL_P (t))
13025 die = lookup_decl_die (t);
13027 gcc_assert (die);
13029 parms = lang_hooks.get_innermost_generic_parms (t);
13030 if (!parms)
13031 /* T has no generic parameter. It means T is neither a generic type
13032 or function. End of story. */
13033 return;
13035 parms_num = TREE_VEC_LENGTH (parms);
13036 args = lang_hooks.get_innermost_generic_args (t);
13037 for (i = 0; i < parms_num; i++)
13039 tree parm, arg, arg_pack_elems;
13041 parm = TREE_VEC_ELT (parms, i);
13042 arg = TREE_VEC_ELT (args, i);
13043 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13044 gcc_assert (parm && TREE_VALUE (parm) && arg);
13046 if (parm && TREE_VALUE (parm) && arg)
13048 /* If PARM represents a template parameter pack,
13049 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13050 by DW_TAG_template_*_parameter DIEs for the argument
13051 pack elements of ARG. Note that ARG would then be
13052 an argument pack. */
13053 if (arg_pack_elems)
13054 template_parameter_pack_die (TREE_VALUE (parm),
13055 arg_pack_elems,
13056 die);
13057 else
13058 generic_parameter_die (TREE_VALUE (parm), arg,
13059 true /* Emit DW_AT_name */, die);
13064 /* Create and return a DIE for PARM which should be
13065 the representation of a generic type parameter.
13066 For instance, in the C++ front end, PARM would be a template parameter.
13067 ARG is the argument to PARM.
13068 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13069 name of the PARM.
13070 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13071 as a child node. */
13073 static dw_die_ref
13074 generic_parameter_die (tree parm, tree arg,
13075 bool emit_name_p,
13076 dw_die_ref parent_die)
13078 dw_die_ref tmpl_die = NULL;
13079 const char *name = NULL;
13081 if (!parm || !DECL_NAME (parm) || !arg)
13082 return NULL;
13084 /* We support non-type generic parameters and arguments,
13085 type generic parameters and arguments, as well as
13086 generic generic parameters (a.k.a. template template parameters in C++)
13087 and arguments. */
13088 if (TREE_CODE (parm) == PARM_DECL)
13089 /* PARM is a nontype generic parameter */
13090 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13091 else if (TREE_CODE (parm) == TYPE_DECL)
13092 /* PARM is a type generic parameter. */
13093 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13094 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13095 /* PARM is a generic generic parameter.
13096 Its DIE is a GNU extension. It shall have a
13097 DW_AT_name attribute to represent the name of the template template
13098 parameter, and a DW_AT_GNU_template_name attribute to represent the
13099 name of the template template argument. */
13100 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13101 parent_die, parm);
13102 else
13103 gcc_unreachable ();
13105 if (tmpl_die)
13107 tree tmpl_type;
13109 /* If PARM is a generic parameter pack, it means we are
13110 emitting debug info for a template argument pack element.
13111 In other terms, ARG is a template argument pack element.
13112 In that case, we don't emit any DW_AT_name attribute for
13113 the die. */
13114 if (emit_name_p)
13116 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13117 gcc_assert (name);
13118 add_AT_string (tmpl_die, DW_AT_name, name);
13121 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13123 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13124 TMPL_DIE should have a child DW_AT_type attribute that is set
13125 to the type of the argument to PARM, which is ARG.
13126 If PARM is a type generic parameter, TMPL_DIE should have a
13127 child DW_AT_type that is set to ARG. */
13128 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13129 add_type_attribute (tmpl_die, tmpl_type, 0,
13130 TREE_THIS_VOLATILE (tmpl_type),
13131 parent_die);
13133 else
13135 /* So TMPL_DIE is a DIE representing a
13136 a generic generic template parameter, a.k.a template template
13137 parameter in C++ and arg is a template. */
13139 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13140 to the name of the argument. */
13141 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13142 if (name)
13143 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13146 if (TREE_CODE (parm) == PARM_DECL)
13147 /* So PARM is a non-type generic parameter.
13148 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13149 attribute of TMPL_DIE which value represents the value
13150 of ARG.
13151 We must be careful here:
13152 The value of ARG might reference some function decls.
13153 We might currently be emitting debug info for a generic
13154 type and types are emitted before function decls, we don't
13155 know if the function decls referenced by ARG will actually be
13156 emitted after cgraph computations.
13157 So must defer the generation of the DW_AT_const_value to
13158 after cgraph is ready. */
13159 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13162 return tmpl_die;
13165 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13166 PARM_PACK must be a template parameter pack. The returned DIE
13167 will be child DIE of PARENT_DIE. */
13169 static dw_die_ref
13170 template_parameter_pack_die (tree parm_pack,
13171 tree parm_pack_args,
13172 dw_die_ref parent_die)
13174 dw_die_ref die;
13175 int j;
13177 gcc_assert (parent_die && parm_pack);
13179 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13180 add_name_and_src_coords_attributes (die, parm_pack);
13181 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13182 generic_parameter_die (parm_pack,
13183 TREE_VEC_ELT (parm_pack_args, j),
13184 false /* Don't emit DW_AT_name */,
13185 die);
13186 return die;
13189 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13190 an enumerated type. */
13192 static inline int
13193 type_is_enum (const_tree type)
13195 return TREE_CODE (type) == ENUMERAL_TYPE;
13198 /* Return the DBX register number described by a given RTL node. */
13200 static unsigned int
13201 dbx_reg_number (const_rtx rtl)
13203 unsigned regno = REGNO (rtl);
13205 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13207 #ifdef LEAF_REG_REMAP
13208 if (current_function_uses_only_leaf_regs)
13210 int leaf_reg = LEAF_REG_REMAP (regno);
13211 if (leaf_reg != -1)
13212 regno = (unsigned) leaf_reg;
13214 #endif
13216 return DBX_REGISTER_NUMBER (regno);
13219 /* Optionally add a DW_OP_piece term to a location description expression.
13220 DW_OP_piece is only added if the location description expression already
13221 doesn't end with DW_OP_piece. */
13223 static void
13224 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13226 dw_loc_descr_ref loc;
13228 if (*list_head != NULL)
13230 /* Find the end of the chain. */
13231 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13234 if (loc->dw_loc_opc != DW_OP_piece)
13235 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13239 /* Return a location descriptor that designates a machine register or
13240 zero if there is none. */
13242 static dw_loc_descr_ref
13243 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13245 rtx regs;
13247 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13248 return 0;
13250 /* We only use "frame base" when we're sure we're talking about the
13251 post-prologue local stack frame. We do this by *not* running
13252 register elimination until this point, and recognizing the special
13253 argument pointer and soft frame pointer rtx's.
13254 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13255 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13256 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13258 dw_loc_descr_ref result = NULL;
13260 if (dwarf_version >= 4 || !dwarf_strict)
13262 result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13263 if (result)
13264 add_loc_descr (&result,
13265 new_loc_descr (DW_OP_stack_value, 0, 0));
13267 return result;
13270 regs = targetm.dwarf_register_span (rtl);
13272 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13273 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13274 else
13275 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13278 /* Return a location descriptor that designates a machine register for
13279 a given hard register number. */
13281 static dw_loc_descr_ref
13282 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13284 dw_loc_descr_ref reg_loc_descr;
13286 if (regno <= 31)
13287 reg_loc_descr
13288 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13289 else
13290 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13292 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13293 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13295 return reg_loc_descr;
13298 /* Given an RTL of a register, return a location descriptor that
13299 designates a value that spans more than one register. */
13301 static dw_loc_descr_ref
13302 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13303 enum var_init_status initialized)
13305 int nregs, size, i;
13306 unsigned reg;
13307 dw_loc_descr_ref loc_result = NULL;
13309 reg = REGNO (rtl);
13310 #ifdef LEAF_REG_REMAP
13311 if (current_function_uses_only_leaf_regs)
13313 int leaf_reg = LEAF_REG_REMAP (reg);
13314 if (leaf_reg != -1)
13315 reg = (unsigned) leaf_reg;
13317 #endif
13318 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13319 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13321 /* Simple, contiguous registers. */
13322 if (regs == NULL_RTX)
13324 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13326 loc_result = NULL;
13327 while (nregs--)
13329 dw_loc_descr_ref t;
13331 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13332 VAR_INIT_STATUS_INITIALIZED);
13333 add_loc_descr (&loc_result, t);
13334 add_loc_descr_op_piece (&loc_result, size);
13335 ++reg;
13337 return loc_result;
13340 /* Now onto stupid register sets in non contiguous locations. */
13342 gcc_assert (GET_CODE (regs) == PARALLEL);
13344 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13345 loc_result = NULL;
13347 for (i = 0; i < XVECLEN (regs, 0); ++i)
13349 dw_loc_descr_ref t;
13351 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13352 VAR_INIT_STATUS_INITIALIZED);
13353 add_loc_descr (&loc_result, t);
13354 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13355 add_loc_descr_op_piece (&loc_result, size);
13358 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13359 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13360 return loc_result;
13363 /* Return a location descriptor that designates a constant. */
13365 static dw_loc_descr_ref
13366 int_loc_descriptor (HOST_WIDE_INT i)
13368 enum dwarf_location_atom op;
13370 /* Pick the smallest representation of a constant, rather than just
13371 defaulting to the LEB encoding. */
13372 if (i >= 0)
13374 if (i <= 31)
13375 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13376 else if (i <= 0xff)
13377 op = DW_OP_const1u;
13378 else if (i <= 0xffff)
13379 op = DW_OP_const2u;
13380 else if (HOST_BITS_PER_WIDE_INT == 32
13381 || i <= 0xffffffff)
13382 op = DW_OP_const4u;
13383 else
13384 op = DW_OP_constu;
13386 else
13388 if (i >= -0x80)
13389 op = DW_OP_const1s;
13390 else if (i >= -0x8000)
13391 op = DW_OP_const2s;
13392 else if (HOST_BITS_PER_WIDE_INT == 32
13393 || i >= -0x80000000)
13394 op = DW_OP_const4s;
13395 else
13396 op = DW_OP_consts;
13399 return new_loc_descr (op, i, 0);
13402 /* Return loc description representing "address" of integer value.
13403 This can appear only as toplevel expression. */
13405 static dw_loc_descr_ref
13406 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13408 int litsize;
13409 dw_loc_descr_ref loc_result = NULL;
13411 if (!(dwarf_version >= 4 || !dwarf_strict))
13412 return NULL;
13414 if (i >= 0)
13416 if (i <= 31)
13417 litsize = 1;
13418 else if (i <= 0xff)
13419 litsize = 2;
13420 else if (i <= 0xffff)
13421 litsize = 3;
13422 else if (HOST_BITS_PER_WIDE_INT == 32
13423 || i <= 0xffffffff)
13424 litsize = 5;
13425 else
13426 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13428 else
13430 if (i >= -0x80)
13431 litsize = 2;
13432 else if (i >= -0x8000)
13433 litsize = 3;
13434 else if (HOST_BITS_PER_WIDE_INT == 32
13435 || i >= -0x80000000)
13436 litsize = 5;
13437 else
13438 litsize = 1 + size_of_sleb128 (i);
13440 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13441 is more compact. For DW_OP_stack_value we need:
13442 litsize + 1 (DW_OP_stack_value)
13443 and for DW_OP_implicit_value:
13444 1 (DW_OP_implicit_value) + 1 (length) + size. */
13445 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13447 loc_result = int_loc_descriptor (i);
13448 add_loc_descr (&loc_result,
13449 new_loc_descr (DW_OP_stack_value, 0, 0));
13450 return loc_result;
13453 loc_result = new_loc_descr (DW_OP_implicit_value,
13454 size, 0);
13455 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13456 loc_result->dw_loc_oprnd2.v.val_int = i;
13457 return loc_result;
13460 /* Return a location descriptor that designates a base+offset location. */
13462 static dw_loc_descr_ref
13463 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13464 enum var_init_status initialized)
13466 unsigned int regno;
13467 dw_loc_descr_ref result;
13468 dw_fde_ref fde = current_fde ();
13470 /* We only use "frame base" when we're sure we're talking about the
13471 post-prologue local stack frame. We do this by *not* running
13472 register elimination until this point, and recognizing the special
13473 argument pointer and soft frame pointer rtx's. */
13474 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13476 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13478 if (elim != reg)
13480 if (GET_CODE (elim) == PLUS)
13482 offset += INTVAL (XEXP (elim, 1));
13483 elim = XEXP (elim, 0);
13485 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13486 && (elim == hard_frame_pointer_rtx
13487 || elim == stack_pointer_rtx))
13488 || elim == (frame_pointer_needed
13489 ? hard_frame_pointer_rtx
13490 : stack_pointer_rtx));
13492 /* If drap register is used to align stack, use frame
13493 pointer + offset to access stack variables. If stack
13494 is aligned without drap, use stack pointer + offset to
13495 access stack variables. */
13496 if (crtl->stack_realign_tried
13497 && reg == frame_pointer_rtx)
13499 int base_reg
13500 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13501 ? HARD_FRAME_POINTER_REGNUM
13502 : STACK_POINTER_REGNUM);
13503 return new_reg_loc_descr (base_reg, offset);
13506 offset += frame_pointer_fb_offset;
13507 return new_loc_descr (DW_OP_fbreg, offset, 0);
13510 else if (!optimize
13511 && fde
13512 && (fde->drap_reg == REGNO (reg)
13513 || fde->vdrap_reg == REGNO (reg)))
13515 /* Use cfa+offset to represent the location of arguments passed
13516 on the stack when drap is used to align stack.
13517 Only do this when not optimizing, for optimized code var-tracking
13518 is supposed to track where the arguments live and the register
13519 used as vdrap or drap in some spot might be used for something
13520 else in other part of the routine. */
13521 return new_loc_descr (DW_OP_fbreg, offset, 0);
13524 regno = dbx_reg_number (reg);
13525 if (regno <= 31)
13526 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13527 offset, 0);
13528 else
13529 result = new_loc_descr (DW_OP_bregx, regno, offset);
13531 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13532 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13534 return result;
13537 /* Return true if this RTL expression describes a base+offset calculation. */
13539 static inline int
13540 is_based_loc (const_rtx rtl)
13542 return (GET_CODE (rtl) == PLUS
13543 && ((REG_P (XEXP (rtl, 0))
13544 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13545 && CONST_INT_P (XEXP (rtl, 1)))));
13548 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13549 failed. */
13551 static dw_loc_descr_ref
13552 tls_mem_loc_descriptor (rtx mem)
13554 tree base;
13555 dw_loc_descr_ref loc_result;
13557 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13558 return NULL;
13560 base = get_base_address (MEM_EXPR (mem));
13561 if (base == NULL
13562 || TREE_CODE (base) != VAR_DECL
13563 || !DECL_THREAD_LOCAL_P (base))
13564 return NULL;
13566 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13567 if (loc_result == NULL)
13568 return NULL;
13570 if (INTVAL (MEM_OFFSET (mem)))
13571 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13573 return loc_result;
13576 /* Output debug info about reason why we failed to expand expression as dwarf
13577 expression. */
13579 static void
13580 expansion_failed (tree expr, rtx rtl, char const *reason)
13582 if (dump_file && (dump_flags & TDF_DETAILS))
13584 fprintf (dump_file, "Failed to expand as dwarf: ");
13585 if (expr)
13586 print_generic_expr (dump_file, expr, dump_flags);
13587 if (rtl)
13589 fprintf (dump_file, "\n");
13590 print_rtl (dump_file, rtl);
13592 fprintf (dump_file, "\nReason: %s\n", reason);
13596 /* Helper function for const_ok_for_output, called either directly
13597 or via for_each_rtx. */
13599 static int
13600 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13602 rtx rtl = *rtlp;
13604 if (GET_CODE (rtl) == UNSPEC)
13606 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13607 we can't express it in the debug info. */
13608 #ifdef ENABLE_CHECKING
13609 /* Don't complain about TLS UNSPECs, those are just too hard to
13610 delegitimize. */
13611 if (XVECLEN (rtl, 0) != 1
13612 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13613 || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13614 || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13615 || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13616 inform (current_function_decl
13617 ? DECL_SOURCE_LOCATION (current_function_decl)
13618 : UNKNOWN_LOCATION,
13619 #if NUM_UNSPEC_VALUES > 0
13620 "non-delegitimized UNSPEC %s (%d) found in variable location",
13621 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13622 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13623 XINT (rtl, 1));
13624 #else
13625 "non-delegitimized UNSPEC %d found in variable location",
13626 XINT (rtl, 1));
13627 #endif
13628 #endif
13629 expansion_failed (NULL_TREE, rtl,
13630 "UNSPEC hasn't been delegitimized.\n");
13631 return 1;
13634 if (GET_CODE (rtl) != SYMBOL_REF)
13635 return 0;
13637 if (CONSTANT_POOL_ADDRESS_P (rtl))
13639 bool marked;
13640 get_pool_constant_mark (rtl, &marked);
13641 /* If all references to this pool constant were optimized away,
13642 it was not output and thus we can't represent it. */
13643 if (!marked)
13645 expansion_failed (NULL_TREE, rtl,
13646 "Constant was removed from constant pool.\n");
13647 return 1;
13651 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13652 return 1;
13654 /* Avoid references to external symbols in debug info, on several targets
13655 the linker might even refuse to link when linking a shared library,
13656 and in many other cases the relocations for .debug_info/.debug_loc are
13657 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13658 to be defined within the same shared library or executable are fine. */
13659 if (SYMBOL_REF_EXTERNAL_P (rtl))
13661 tree decl = SYMBOL_REF_DECL (rtl);
13663 if (decl == NULL || !targetm.binds_local_p (decl))
13665 expansion_failed (NULL_TREE, rtl,
13666 "Symbol not defined in current TU.\n");
13667 return 1;
13671 return 0;
13674 /* Return true if constant RTL can be emitted in DW_OP_addr or
13675 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13676 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13678 static bool
13679 const_ok_for_output (rtx rtl)
13681 if (GET_CODE (rtl) == SYMBOL_REF)
13682 return const_ok_for_output_1 (&rtl, NULL) == 0;
13684 if (GET_CODE (rtl) == CONST)
13685 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13687 return true;
13690 /* The following routine converts the RTL for a variable or parameter
13691 (resident in memory) into an equivalent Dwarf representation of a
13692 mechanism for getting the address of that same variable onto the top of a
13693 hypothetical "address evaluation" stack.
13695 When creating memory location descriptors, we are effectively transforming
13696 the RTL for a memory-resident object into its Dwarf postfix expression
13697 equivalent. This routine recursively descends an RTL tree, turning
13698 it into Dwarf postfix code as it goes.
13700 MODE is the mode of the memory reference, needed to handle some
13701 autoincrement addressing modes.
13703 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13704 location list for RTL.
13706 Return 0 if we can't represent the location. */
13708 static dw_loc_descr_ref
13709 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13710 enum var_init_status initialized)
13712 dw_loc_descr_ref mem_loc_result = NULL;
13713 enum dwarf_location_atom op;
13714 dw_loc_descr_ref op0, op1;
13716 /* Note that for a dynamically sized array, the location we will generate a
13717 description of here will be the lowest numbered location which is
13718 actually within the array. That's *not* necessarily the same as the
13719 zeroth element of the array. */
13721 rtl = targetm.delegitimize_address (rtl);
13723 switch (GET_CODE (rtl))
13725 case POST_INC:
13726 case POST_DEC:
13727 case POST_MODIFY:
13728 return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13730 case SUBREG:
13731 /* The case of a subreg may arise when we have a local (register)
13732 variable or a formal (register) parameter which doesn't quite fill
13733 up an entire register. For now, just assume that it is
13734 legitimate to make the Dwarf info refer to the whole register which
13735 contains the given subreg. */
13736 if (!subreg_lowpart_p (rtl))
13737 break;
13738 rtl = SUBREG_REG (rtl);
13739 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13740 break;
13741 if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13742 break;
13743 mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13744 break;
13746 case REG:
13747 /* Whenever a register number forms a part of the description of the
13748 method for calculating the (dynamic) address of a memory resident
13749 object, DWARF rules require the register number be referred to as
13750 a "base register". This distinction is not based in any way upon
13751 what category of register the hardware believes the given register
13752 belongs to. This is strictly DWARF terminology we're dealing with
13753 here. Note that in cases where the location of a memory-resident
13754 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13755 OP_CONST (0)) the actual DWARF location descriptor that we generate
13756 may just be OP_BASEREG (basereg). This may look deceptively like
13757 the object in question was allocated to a register (rather than in
13758 memory) so DWARF consumers need to be aware of the subtle
13759 distinction between OP_REG and OP_BASEREG. */
13760 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13761 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13762 else if (stack_realign_drap
13763 && crtl->drap_reg
13764 && crtl->args.internal_arg_pointer == rtl
13765 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13767 /* If RTL is internal_arg_pointer, which has been optimized
13768 out, use DRAP instead. */
13769 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13770 VAR_INIT_STATUS_INITIALIZED);
13772 break;
13774 case SIGN_EXTEND:
13775 case ZERO_EXTEND:
13776 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13777 VAR_INIT_STATUS_INITIALIZED);
13778 if (op0 == 0)
13779 break;
13780 else
13782 int shift = DWARF2_ADDR_SIZE
13783 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13784 shift *= BITS_PER_UNIT;
13785 if (GET_CODE (rtl) == SIGN_EXTEND)
13786 op = DW_OP_shra;
13787 else
13788 op = DW_OP_shr;
13789 mem_loc_result = op0;
13790 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13791 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13792 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13793 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13795 break;
13797 case MEM:
13798 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13799 VAR_INIT_STATUS_INITIALIZED);
13800 if (mem_loc_result == NULL)
13801 mem_loc_result = tls_mem_loc_descriptor (rtl);
13802 if (mem_loc_result != 0)
13804 if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13806 expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13807 return 0;
13809 else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13810 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13811 else
13812 add_loc_descr (&mem_loc_result,
13813 new_loc_descr (DW_OP_deref_size,
13814 GET_MODE_SIZE (GET_MODE (rtl)), 0));
13816 else
13818 rtx new_rtl = avoid_constant_pool_reference (rtl);
13819 if (new_rtl != rtl)
13820 return mem_loc_descriptor (new_rtl, mode, initialized);
13822 break;
13824 case LO_SUM:
13825 rtl = XEXP (rtl, 1);
13827 /* ... fall through ... */
13829 case LABEL_REF:
13830 /* Some ports can transform a symbol ref into a label ref, because
13831 the symbol ref is too far away and has to be dumped into a constant
13832 pool. */
13833 case CONST:
13834 case SYMBOL_REF:
13835 if (GET_CODE (rtl) == SYMBOL_REF
13836 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13838 dw_loc_descr_ref temp;
13840 /* If this is not defined, we have no way to emit the data. */
13841 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13842 break;
13844 /* We used to emit DW_OP_addr here, but that's wrong, since
13845 DW_OP_addr should be relocated by the debug info consumer,
13846 while DW_OP_GNU_push_tls_address operand should not. */
13847 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13848 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13849 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13850 temp->dw_loc_oprnd1.v.val_addr = rtl;
13851 temp->dtprel = true;
13853 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13854 add_loc_descr (&mem_loc_result, temp);
13856 break;
13859 if (!const_ok_for_output (rtl))
13860 break;
13862 symref:
13863 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13864 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13865 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13866 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13867 break;
13869 case CONCAT:
13870 case CONCATN:
13871 case VAR_LOCATION:
13872 case DEBUG_IMPLICIT_PTR:
13873 expansion_failed (NULL_TREE, rtl,
13874 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13875 return 0;
13877 case ENTRY_VALUE:
13878 if (dwarf_strict)
13879 return NULL;
13880 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13881 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13882 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13883 mem_loc_result->dw_loc_oprnd1.v.val_loc
13884 = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13885 VAR_INIT_STATUS_INITIALIZED);
13886 else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13888 dw_loc_descr_ref ref
13889 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13890 VAR_INIT_STATUS_INITIALIZED);
13891 if (ref == NULL)
13892 return NULL;
13893 mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13895 else
13896 gcc_unreachable ();
13897 return mem_loc_result;
13899 case PRE_MODIFY:
13900 /* Extract the PLUS expression nested inside and fall into
13901 PLUS code below. */
13902 rtl = XEXP (rtl, 1);
13903 goto plus;
13905 case PRE_INC:
13906 case PRE_DEC:
13907 /* Turn these into a PLUS expression and fall into the PLUS code
13908 below. */
13909 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13910 GEN_INT (GET_CODE (rtl) == PRE_INC
13911 ? GET_MODE_UNIT_SIZE (mode)
13912 : -GET_MODE_UNIT_SIZE (mode)));
13914 /* ... fall through ... */
13916 case PLUS:
13917 plus:
13918 if (is_based_loc (rtl))
13919 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13920 INTVAL (XEXP (rtl, 1)),
13921 VAR_INIT_STATUS_INITIALIZED);
13922 else
13924 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13925 VAR_INIT_STATUS_INITIALIZED);
13926 if (mem_loc_result == 0)
13927 break;
13929 if (CONST_INT_P (XEXP (rtl, 1)))
13930 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13931 else
13933 dw_loc_descr_ref mem_loc_result2
13934 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13935 VAR_INIT_STATUS_INITIALIZED);
13936 if (mem_loc_result2 == 0)
13937 break;
13938 add_loc_descr (&mem_loc_result, mem_loc_result2);
13939 add_loc_descr (&mem_loc_result,
13940 new_loc_descr (DW_OP_plus, 0, 0));
13943 break;
13945 /* If a pseudo-reg is optimized away, it is possible for it to
13946 be replaced with a MEM containing a multiply or shift. */
13947 case MINUS:
13948 op = DW_OP_minus;
13949 goto do_binop;
13951 case MULT:
13952 op = DW_OP_mul;
13953 goto do_binop;
13955 case DIV:
13956 op = DW_OP_div;
13957 goto do_binop;
13959 case UMOD:
13960 op = DW_OP_mod;
13961 goto do_binop;
13963 case ASHIFT:
13964 op = DW_OP_shl;
13965 goto do_binop;
13967 case ASHIFTRT:
13968 op = DW_OP_shra;
13969 goto do_binop;
13971 case LSHIFTRT:
13972 op = DW_OP_shr;
13973 goto do_binop;
13975 case AND:
13976 op = DW_OP_and;
13977 goto do_binop;
13979 case IOR:
13980 op = DW_OP_or;
13981 goto do_binop;
13983 case XOR:
13984 op = DW_OP_xor;
13985 goto do_binop;
13987 do_binop:
13988 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13989 VAR_INIT_STATUS_INITIALIZED);
13990 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13991 VAR_INIT_STATUS_INITIALIZED);
13993 if (op0 == 0 || op1 == 0)
13994 break;
13996 mem_loc_result = op0;
13997 add_loc_descr (&mem_loc_result, op1);
13998 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13999 break;
14001 case MOD:
14002 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14003 VAR_INIT_STATUS_INITIALIZED);
14004 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14005 VAR_INIT_STATUS_INITIALIZED);
14007 if (op0 == 0 || op1 == 0)
14008 break;
14010 mem_loc_result = op0;
14011 add_loc_descr (&mem_loc_result, op1);
14012 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14013 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14014 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14015 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14016 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14017 break;
14019 case NOT:
14020 op = DW_OP_not;
14021 goto do_unop;
14023 case ABS:
14024 op = DW_OP_abs;
14025 goto do_unop;
14027 case NEG:
14028 op = DW_OP_neg;
14029 goto do_unop;
14031 do_unop:
14032 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14033 VAR_INIT_STATUS_INITIALIZED);
14035 if (op0 == 0)
14036 break;
14038 mem_loc_result = op0;
14039 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14040 break;
14042 case CONST_INT:
14043 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14044 break;
14046 case EQ:
14047 op = DW_OP_eq;
14048 goto do_scompare;
14050 case GE:
14051 op = DW_OP_ge;
14052 goto do_scompare;
14054 case GT:
14055 op = DW_OP_gt;
14056 goto do_scompare;
14058 case LE:
14059 op = DW_OP_le;
14060 goto do_scompare;
14062 case LT:
14063 op = DW_OP_lt;
14064 goto do_scompare;
14066 case NE:
14067 op = DW_OP_ne;
14068 goto do_scompare;
14070 do_scompare:
14071 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14072 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14073 break;
14074 else
14076 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14078 if (op_mode == VOIDmode)
14079 op_mode = GET_MODE (XEXP (rtl, 1));
14080 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14081 break;
14083 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14084 VAR_INIT_STATUS_INITIALIZED);
14085 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14086 VAR_INIT_STATUS_INITIALIZED);
14088 if (op0 == 0 || op1 == 0)
14089 break;
14091 if (op_mode != VOIDmode
14092 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14094 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14095 shift *= BITS_PER_UNIT;
14096 /* For eq/ne, if the operands are known to be zero-extended,
14097 there is no need to do the fancy shifting up. */
14098 if (op == DW_OP_eq || op == DW_OP_ne)
14100 dw_loc_descr_ref last0, last1;
14101 for (last0 = op0;
14102 last0->dw_loc_next != NULL;
14103 last0 = last0->dw_loc_next)
14105 for (last1 = op1;
14106 last1->dw_loc_next != NULL;
14107 last1 = last1->dw_loc_next)
14109 /* deref_size zero extends, and for constants we can check
14110 whether they are zero extended or not. */
14111 if (((last0->dw_loc_opc == DW_OP_deref_size
14112 && last0->dw_loc_oprnd1.v.val_int
14113 <= GET_MODE_SIZE (op_mode))
14114 || (CONST_INT_P (XEXP (rtl, 0))
14115 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14116 == (INTVAL (XEXP (rtl, 0))
14117 & GET_MODE_MASK (op_mode))))
14118 && ((last1->dw_loc_opc == DW_OP_deref_size
14119 && last1->dw_loc_oprnd1.v.val_int
14120 <= GET_MODE_SIZE (op_mode))
14121 || (CONST_INT_P (XEXP (rtl, 1))
14122 && (unsigned HOST_WIDE_INT)
14123 INTVAL (XEXP (rtl, 1))
14124 == (INTVAL (XEXP (rtl, 1))
14125 & GET_MODE_MASK (op_mode)))))
14126 goto do_compare;
14128 add_loc_descr (&op0, int_loc_descriptor (shift));
14129 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14130 if (CONST_INT_P (XEXP (rtl, 1)))
14131 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14132 else
14134 add_loc_descr (&op1, int_loc_descriptor (shift));
14135 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14140 do_compare:
14141 mem_loc_result = op0;
14142 add_loc_descr (&mem_loc_result, op1);
14143 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14144 if (STORE_FLAG_VALUE != 1)
14146 add_loc_descr (&mem_loc_result,
14147 int_loc_descriptor (STORE_FLAG_VALUE));
14148 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14150 break;
14152 case GEU:
14153 op = DW_OP_ge;
14154 goto do_ucompare;
14156 case GTU:
14157 op = DW_OP_gt;
14158 goto do_ucompare;
14160 case LEU:
14161 op = DW_OP_le;
14162 goto do_ucompare;
14164 case LTU:
14165 op = DW_OP_lt;
14166 goto do_ucompare;
14168 do_ucompare:
14169 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14170 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14171 break;
14172 else
14174 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14176 if (op_mode == VOIDmode)
14177 op_mode = GET_MODE (XEXP (rtl, 1));
14178 if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14179 break;
14181 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14182 VAR_INIT_STATUS_INITIALIZED);
14183 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14184 VAR_INIT_STATUS_INITIALIZED);
14186 if (op0 == 0 || op1 == 0)
14187 break;
14189 if (op_mode != VOIDmode
14190 && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14192 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14193 dw_loc_descr_ref last0, last1;
14194 for (last0 = op0;
14195 last0->dw_loc_next != NULL;
14196 last0 = last0->dw_loc_next)
14198 for (last1 = op1;
14199 last1->dw_loc_next != NULL;
14200 last1 = last1->dw_loc_next)
14202 if (CONST_INT_P (XEXP (rtl, 0)))
14203 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14204 /* deref_size zero extends, so no need to mask it again. */
14205 else if (last0->dw_loc_opc != DW_OP_deref_size
14206 || last0->dw_loc_oprnd1.v.val_int
14207 > GET_MODE_SIZE (op_mode))
14209 add_loc_descr (&op0, int_loc_descriptor (mask));
14210 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14212 if (CONST_INT_P (XEXP (rtl, 1)))
14213 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14214 /* deref_size zero extends, so no need to mask it again. */
14215 else if (last1->dw_loc_opc != DW_OP_deref_size
14216 || last1->dw_loc_oprnd1.v.val_int
14217 > GET_MODE_SIZE (op_mode))
14219 add_loc_descr (&op1, int_loc_descriptor (mask));
14220 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14223 else
14225 HOST_WIDE_INT bias = 1;
14226 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14227 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14228 if (CONST_INT_P (XEXP (rtl, 1)))
14229 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14230 + INTVAL (XEXP (rtl, 1)));
14231 else
14232 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14233 bias, 0));
14236 goto do_compare;
14238 case SMIN:
14239 case SMAX:
14240 case UMIN:
14241 case UMAX:
14242 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14243 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14244 || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14245 break;
14247 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14248 VAR_INIT_STATUS_INITIALIZED);
14249 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14250 VAR_INIT_STATUS_INITIALIZED);
14252 if (op0 == 0 || op1 == 0)
14253 break;
14255 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14256 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14257 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14258 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14260 if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14262 HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14263 add_loc_descr (&op0, int_loc_descriptor (mask));
14264 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14265 add_loc_descr (&op1, int_loc_descriptor (mask));
14266 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14268 else
14270 HOST_WIDE_INT bias = 1;
14271 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14272 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14273 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14276 else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14278 int shift = DWARF2_ADDR_SIZE
14279 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14280 shift *= BITS_PER_UNIT;
14281 add_loc_descr (&op0, int_loc_descriptor (shift));
14282 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14283 add_loc_descr (&op1, int_loc_descriptor (shift));
14284 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14287 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14288 op = DW_OP_lt;
14289 else
14290 op = DW_OP_gt;
14291 mem_loc_result = op0;
14292 add_loc_descr (&mem_loc_result, op1);
14293 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14295 dw_loc_descr_ref bra_node, drop_node;
14297 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14298 add_loc_descr (&mem_loc_result, bra_node);
14299 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14300 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14301 add_loc_descr (&mem_loc_result, drop_node);
14302 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14303 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14305 break;
14307 case ZERO_EXTRACT:
14308 case SIGN_EXTRACT:
14309 if (CONST_INT_P (XEXP (rtl, 1))
14310 && CONST_INT_P (XEXP (rtl, 2))
14311 && ((unsigned) INTVAL (XEXP (rtl, 1))
14312 + (unsigned) INTVAL (XEXP (rtl, 2))
14313 <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14314 && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14315 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14317 int shift, size;
14318 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14319 VAR_INIT_STATUS_INITIALIZED);
14320 if (op0 == 0)
14321 break;
14322 if (GET_CODE (rtl) == SIGN_EXTRACT)
14323 op = DW_OP_shra;
14324 else
14325 op = DW_OP_shr;
14326 mem_loc_result = op0;
14327 size = INTVAL (XEXP (rtl, 1));
14328 shift = INTVAL (XEXP (rtl, 2));
14329 if (BITS_BIG_ENDIAN)
14330 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14331 - shift - size;
14332 if (shift + size != (int) DWARF2_ADDR_SIZE)
14334 add_loc_descr (&mem_loc_result,
14335 int_loc_descriptor (DWARF2_ADDR_SIZE
14336 - shift - size));
14337 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14339 if (size != (int) DWARF2_ADDR_SIZE)
14341 add_loc_descr (&mem_loc_result,
14342 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14343 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14346 break;
14348 case IF_THEN_ELSE:
14350 dw_loc_descr_ref op2, bra_node, drop_node;
14351 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14352 VAR_INIT_STATUS_INITIALIZED);
14353 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14354 VAR_INIT_STATUS_INITIALIZED);
14355 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14356 VAR_INIT_STATUS_INITIALIZED);
14357 if (op0 == NULL || op1 == NULL || op2 == NULL)
14358 break;
14360 mem_loc_result = op1;
14361 add_loc_descr (&mem_loc_result, op2);
14362 add_loc_descr (&mem_loc_result, op0);
14363 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14364 add_loc_descr (&mem_loc_result, bra_node);
14365 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14366 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14367 add_loc_descr (&mem_loc_result, drop_node);
14368 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14369 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14371 break;
14373 case COMPARE:
14374 case ROTATE:
14375 case ROTATERT:
14376 case TRUNCATE:
14377 /* In theory, we could implement the above. */
14378 /* DWARF cannot represent the unsigned compare operations
14379 natively. */
14380 case SS_MULT:
14381 case US_MULT:
14382 case SS_DIV:
14383 case US_DIV:
14384 case SS_PLUS:
14385 case US_PLUS:
14386 case SS_MINUS:
14387 case US_MINUS:
14388 case SS_NEG:
14389 case US_NEG:
14390 case SS_ABS:
14391 case SS_ASHIFT:
14392 case US_ASHIFT:
14393 case SS_TRUNCATE:
14394 case US_TRUNCATE:
14395 case UDIV:
14396 case UNORDERED:
14397 case ORDERED:
14398 case UNEQ:
14399 case UNGE:
14400 case UNGT:
14401 case UNLE:
14402 case UNLT:
14403 case LTGT:
14404 case FLOAT_EXTEND:
14405 case FLOAT_TRUNCATE:
14406 case FLOAT:
14407 case UNSIGNED_FLOAT:
14408 case FIX:
14409 case UNSIGNED_FIX:
14410 case FRACT_CONVERT:
14411 case UNSIGNED_FRACT_CONVERT:
14412 case SAT_FRACT:
14413 case UNSIGNED_SAT_FRACT:
14414 case SQRT:
14415 case BSWAP:
14416 case FFS:
14417 case CLZ:
14418 case CTZ:
14419 case POPCOUNT:
14420 case PARITY:
14421 case ASM_OPERANDS:
14422 case VEC_MERGE:
14423 case VEC_SELECT:
14424 case VEC_CONCAT:
14425 case VEC_DUPLICATE:
14426 case UNSPEC:
14427 case HIGH:
14428 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14429 can't express it in the debug info. This can happen e.g. with some
14430 TLS UNSPECs. */
14431 break;
14433 case CONST_STRING:
14434 resolve_one_addr (&rtl, NULL);
14435 goto symref;
14437 default:
14438 #ifdef ENABLE_CHECKING
14439 print_rtl (stderr, rtl);
14440 gcc_unreachable ();
14441 #else
14442 break;
14443 #endif
14446 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14447 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14449 return mem_loc_result;
14452 /* Return a descriptor that describes the concatenation of two locations.
14453 This is typically a complex variable. */
14455 static dw_loc_descr_ref
14456 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14458 dw_loc_descr_ref cc_loc_result = NULL;
14459 dw_loc_descr_ref x0_ref
14460 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14461 dw_loc_descr_ref x1_ref
14462 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14464 if (x0_ref == 0 || x1_ref == 0)
14465 return 0;
14467 cc_loc_result = x0_ref;
14468 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14470 add_loc_descr (&cc_loc_result, x1_ref);
14471 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14473 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14474 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14476 return cc_loc_result;
14479 /* Return a descriptor that describes the concatenation of N
14480 locations. */
14482 static dw_loc_descr_ref
14483 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14485 unsigned int i;
14486 dw_loc_descr_ref cc_loc_result = NULL;
14487 unsigned int n = XVECLEN (concatn, 0);
14489 for (i = 0; i < n; ++i)
14491 dw_loc_descr_ref ref;
14492 rtx x = XVECEXP (concatn, 0, i);
14494 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14495 if (ref == NULL)
14496 return NULL;
14498 add_loc_descr (&cc_loc_result, ref);
14499 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14502 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14503 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14505 return cc_loc_result;
14508 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14509 for DEBUG_IMPLICIT_PTR RTL. */
14511 static dw_loc_descr_ref
14512 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14514 dw_loc_descr_ref ret;
14515 dw_die_ref ref;
14517 if (dwarf_strict)
14518 return NULL;
14519 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14520 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14521 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14522 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14523 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14524 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14525 if (ref)
14527 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14528 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14529 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14531 else
14533 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14534 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14536 return ret;
14539 /* Output a proper Dwarf location descriptor for a variable or parameter
14540 which is either allocated in a register or in a memory location. For a
14541 register, we just generate an OP_REG and the register number. For a
14542 memory location we provide a Dwarf postfix expression describing how to
14543 generate the (dynamic) address of the object onto the address stack.
14545 MODE is mode of the decl if this loc_descriptor is going to be used in
14546 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14547 allowed, VOIDmode otherwise.
14549 If we don't know how to describe it, return 0. */
14551 static dw_loc_descr_ref
14552 loc_descriptor (rtx rtl, enum machine_mode mode,
14553 enum var_init_status initialized)
14555 dw_loc_descr_ref loc_result = NULL;
14557 switch (GET_CODE (rtl))
14559 case SUBREG:
14560 /* The case of a subreg may arise when we have a local (register)
14561 variable or a formal (register) parameter which doesn't quite fill
14562 up an entire register. For now, just assume that it is
14563 legitimate to make the Dwarf info refer to the whole register which
14564 contains the given subreg. */
14565 loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14566 break;
14568 case REG:
14569 loc_result = reg_loc_descriptor (rtl, initialized);
14570 break;
14572 case MEM:
14573 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14574 initialized);
14575 if (loc_result == NULL)
14576 loc_result = tls_mem_loc_descriptor (rtl);
14577 if (loc_result == NULL)
14579 rtx new_rtl = avoid_constant_pool_reference (rtl);
14580 if (new_rtl != rtl)
14581 loc_result = loc_descriptor (new_rtl, mode, initialized);
14583 break;
14585 case CONCAT:
14586 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14587 initialized);
14588 break;
14590 case CONCATN:
14591 loc_result = concatn_loc_descriptor (rtl, initialized);
14592 break;
14594 case VAR_LOCATION:
14595 /* Single part. */
14596 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14598 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14599 if (GET_CODE (loc) == EXPR_LIST)
14600 loc = XEXP (loc, 0);
14601 loc_result = loc_descriptor (loc, mode, initialized);
14602 break;
14605 rtl = XEXP (rtl, 1);
14606 /* FALLTHRU */
14608 case PARALLEL:
14610 rtvec par_elems = XVEC (rtl, 0);
14611 int num_elem = GET_NUM_ELEM (par_elems);
14612 enum machine_mode mode;
14613 int i;
14615 /* Create the first one, so we have something to add to. */
14616 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14617 VOIDmode, initialized);
14618 if (loc_result == NULL)
14619 return NULL;
14620 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14621 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14622 for (i = 1; i < num_elem; i++)
14624 dw_loc_descr_ref temp;
14626 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14627 VOIDmode, initialized);
14628 if (temp == NULL)
14629 return NULL;
14630 add_loc_descr (&loc_result, temp);
14631 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14632 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14635 break;
14637 case CONST_INT:
14638 if (mode != VOIDmode && mode != BLKmode)
14639 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14640 INTVAL (rtl));
14641 break;
14643 case CONST_DOUBLE:
14644 if (mode == VOIDmode)
14645 mode = GET_MODE (rtl);
14647 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14649 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14651 /* Note that a CONST_DOUBLE rtx could represent either an integer
14652 or a floating-point constant. A CONST_DOUBLE is used whenever
14653 the constant requires more than one word in order to be
14654 adequately represented. We output CONST_DOUBLEs as blocks. */
14655 loc_result = new_loc_descr (DW_OP_implicit_value,
14656 GET_MODE_SIZE (mode), 0);
14657 if (SCALAR_FLOAT_MODE_P (mode))
14659 unsigned int length = GET_MODE_SIZE (mode);
14660 unsigned char *array
14661 = (unsigned char*) ggc_alloc_atomic (length);
14663 insert_float (rtl, array);
14664 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14665 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14666 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14667 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14669 else
14671 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14672 loc_result->dw_loc_oprnd2.v.val_double
14673 = rtx_to_double_int (rtl);
14676 break;
14678 case CONST_VECTOR:
14679 if (mode == VOIDmode)
14680 mode = GET_MODE (rtl);
14682 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14684 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14685 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14686 unsigned char *array = (unsigned char *)
14687 ggc_alloc_atomic (length * elt_size);
14688 unsigned int i;
14689 unsigned char *p;
14691 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14692 switch (GET_MODE_CLASS (mode))
14694 case MODE_VECTOR_INT:
14695 for (i = 0, p = array; i < length; i++, p += elt_size)
14697 rtx elt = CONST_VECTOR_ELT (rtl, i);
14698 double_int val = rtx_to_double_int (elt);
14700 if (elt_size <= sizeof (HOST_WIDE_INT))
14701 insert_int (double_int_to_shwi (val), elt_size, p);
14702 else
14704 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14705 insert_double (val, p);
14708 break;
14710 case MODE_VECTOR_FLOAT:
14711 for (i = 0, p = array; i < length; i++, p += elt_size)
14713 rtx elt = CONST_VECTOR_ELT (rtl, i);
14714 insert_float (elt, p);
14716 break;
14718 default:
14719 gcc_unreachable ();
14722 loc_result = new_loc_descr (DW_OP_implicit_value,
14723 length * elt_size, 0);
14724 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14725 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14726 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14727 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14729 break;
14731 case CONST:
14732 if (mode == VOIDmode
14733 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14734 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14735 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14737 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14738 break;
14740 /* FALLTHROUGH */
14741 case SYMBOL_REF:
14742 if (!const_ok_for_output (rtl))
14743 break;
14744 case LABEL_REF:
14745 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14746 && (dwarf_version >= 4 || !dwarf_strict))
14748 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14749 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14750 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14751 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14752 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14754 break;
14756 case DEBUG_IMPLICIT_PTR:
14757 loc_result = implicit_ptr_descriptor (rtl, 0);
14758 break;
14760 case PLUS:
14761 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14762 && CONST_INT_P (XEXP (rtl, 1)))
14764 loc_result
14765 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14766 break;
14768 /* FALLTHRU */
14769 default:
14770 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14771 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14772 && (dwarf_version >= 4 || !dwarf_strict))
14774 /* Value expression. */
14775 loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14776 if (loc_result)
14777 add_loc_descr (&loc_result,
14778 new_loc_descr (DW_OP_stack_value, 0, 0));
14780 break;
14783 return loc_result;
14786 /* We need to figure out what section we should use as the base for the
14787 address ranges where a given location is valid.
14788 1. If this particular DECL has a section associated with it, use that.
14789 2. If this function has a section associated with it, use that.
14790 3. Otherwise, use the text section.
14791 XXX: If you split a variable across multiple sections, we won't notice. */
14793 static const char *
14794 secname_for_decl (const_tree decl)
14796 const char *secname;
14798 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14800 tree sectree = DECL_SECTION_NAME (decl);
14801 secname = TREE_STRING_POINTER (sectree);
14803 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14805 tree sectree = DECL_SECTION_NAME (current_function_decl);
14806 secname = TREE_STRING_POINTER (sectree);
14808 else if (cfun && in_cold_section_p)
14809 secname = crtl->subsections.cold_section_label;
14810 else
14811 secname = text_section_label;
14813 return secname;
14816 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14818 static bool
14819 decl_by_reference_p (tree decl)
14821 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14822 || TREE_CODE (decl) == VAR_DECL)
14823 && DECL_BY_REFERENCE (decl));
14826 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14827 for VARLOC. */
14829 static dw_loc_descr_ref
14830 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14831 enum var_init_status initialized)
14833 int have_address = 0;
14834 dw_loc_descr_ref descr;
14835 enum machine_mode mode;
14837 if (want_address != 2)
14839 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14840 /* Single part. */
14841 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14843 varloc = PAT_VAR_LOCATION_LOC (varloc);
14844 if (GET_CODE (varloc) == EXPR_LIST)
14845 varloc = XEXP (varloc, 0);
14846 mode = GET_MODE (varloc);
14847 if (MEM_P (varloc))
14849 rtx addr = XEXP (varloc, 0);
14850 descr = mem_loc_descriptor (addr, mode, initialized);
14851 if (descr)
14852 have_address = 1;
14853 else
14855 rtx x = avoid_constant_pool_reference (varloc);
14856 if (x != varloc)
14857 descr = mem_loc_descriptor (x, mode, initialized);
14860 else
14861 descr = mem_loc_descriptor (varloc, mode, initialized);
14863 else
14864 return 0;
14866 else
14868 if (GET_CODE (varloc) == VAR_LOCATION)
14869 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14870 else
14871 mode = DECL_MODE (loc);
14872 descr = loc_descriptor (varloc, mode, initialized);
14873 have_address = 1;
14876 if (!descr)
14877 return 0;
14879 if (want_address == 2 && !have_address
14880 && (dwarf_version >= 4 || !dwarf_strict))
14882 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14884 expansion_failed (loc, NULL_RTX,
14885 "DWARF address size mismatch");
14886 return 0;
14888 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14889 have_address = 1;
14891 /* Show if we can't fill the request for an address. */
14892 if (want_address && !have_address)
14894 expansion_failed (loc, NULL_RTX,
14895 "Want address and only have value");
14896 return 0;
14899 /* If we've got an address and don't want one, dereference. */
14900 if (!want_address && have_address)
14902 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14903 enum dwarf_location_atom op;
14905 if (size > DWARF2_ADDR_SIZE || size == -1)
14907 expansion_failed (loc, NULL_RTX,
14908 "DWARF address size mismatch");
14909 return 0;
14911 else if (size == DWARF2_ADDR_SIZE)
14912 op = DW_OP_deref;
14913 else
14914 op = DW_OP_deref_size;
14916 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14919 return descr;
14922 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14923 if it is not possible. */
14925 static dw_loc_descr_ref
14926 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14928 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14929 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14930 else if (dwarf_version >= 3 || !dwarf_strict)
14931 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14932 else
14933 return NULL;
14936 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14937 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14939 static dw_loc_descr_ref
14940 dw_sra_loc_expr (tree decl, rtx loc)
14942 rtx p;
14943 unsigned int padsize = 0;
14944 dw_loc_descr_ref descr, *descr_tail;
14945 unsigned HOST_WIDE_INT decl_size;
14946 rtx varloc;
14947 enum var_init_status initialized;
14949 if (DECL_SIZE (decl) == NULL
14950 || !host_integerp (DECL_SIZE (decl), 1))
14951 return NULL;
14953 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14954 descr = NULL;
14955 descr_tail = &descr;
14957 for (p = loc; p; p = XEXP (p, 1))
14959 unsigned int bitsize = decl_piece_bitsize (p);
14960 rtx loc_note = *decl_piece_varloc_ptr (p);
14961 dw_loc_descr_ref cur_descr;
14962 dw_loc_descr_ref *tail, last = NULL;
14963 unsigned int opsize = 0;
14965 if (loc_note == NULL_RTX
14966 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14968 padsize += bitsize;
14969 continue;
14971 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14972 varloc = NOTE_VAR_LOCATION (loc_note);
14973 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14974 if (cur_descr == NULL)
14976 padsize += bitsize;
14977 continue;
14980 /* Check that cur_descr either doesn't use
14981 DW_OP_*piece operations, or their sum is equal
14982 to bitsize. Otherwise we can't embed it. */
14983 for (tail = &cur_descr; *tail != NULL;
14984 tail = &(*tail)->dw_loc_next)
14985 if ((*tail)->dw_loc_opc == DW_OP_piece)
14987 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14988 * BITS_PER_UNIT;
14989 last = *tail;
14991 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14993 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14994 last = *tail;
14997 if (last != NULL && opsize != bitsize)
14999 padsize += bitsize;
15000 continue;
15003 /* If there is a hole, add DW_OP_*piece after empty DWARF
15004 expression, which means that those bits are optimized out. */
15005 if (padsize)
15007 if (padsize > decl_size)
15008 return NULL;
15009 decl_size -= padsize;
15010 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15011 if (*descr_tail == NULL)
15012 return NULL;
15013 descr_tail = &(*descr_tail)->dw_loc_next;
15014 padsize = 0;
15016 *descr_tail = cur_descr;
15017 descr_tail = tail;
15018 if (bitsize > decl_size)
15019 return NULL;
15020 decl_size -= bitsize;
15021 if (last == NULL)
15023 HOST_WIDE_INT offset = 0;
15024 if (GET_CODE (varloc) == VAR_LOCATION
15025 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15027 varloc = PAT_VAR_LOCATION_LOC (varloc);
15028 if (GET_CODE (varloc) == EXPR_LIST)
15029 varloc = XEXP (varloc, 0);
15033 if (GET_CODE (varloc) == CONST
15034 || GET_CODE (varloc) == SIGN_EXTEND
15035 || GET_CODE (varloc) == ZERO_EXTEND)
15036 varloc = XEXP (varloc, 0);
15037 else if (GET_CODE (varloc) == SUBREG)
15038 varloc = SUBREG_REG (varloc);
15039 else
15040 break;
15042 while (1);
15043 /* DW_OP_bit_size offset should be zero for register
15044 or implicit location descriptions and empty location
15045 descriptions, but for memory addresses needs big endian
15046 adjustment. */
15047 if (MEM_P (varloc))
15049 unsigned HOST_WIDE_INT memsize
15050 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15051 if (memsize != bitsize)
15053 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15054 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15055 return NULL;
15056 if (memsize < bitsize)
15057 return NULL;
15058 if (BITS_BIG_ENDIAN)
15059 offset = memsize - bitsize;
15063 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15064 if (*descr_tail == NULL)
15065 return NULL;
15066 descr_tail = &(*descr_tail)->dw_loc_next;
15070 /* If there were any non-empty expressions, add padding till the end of
15071 the decl. */
15072 if (descr != NULL && decl_size != 0)
15074 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15075 if (*descr_tail == NULL)
15076 return NULL;
15078 return descr;
15081 /* Return the dwarf representation of the location list LOC_LIST of
15082 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
15083 function. */
15085 static dw_loc_list_ref
15086 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15088 const char *endname, *secname;
15089 rtx varloc;
15090 enum var_init_status initialized;
15091 struct var_loc_node *node;
15092 dw_loc_descr_ref descr;
15093 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15094 dw_loc_list_ref list = NULL;
15095 dw_loc_list_ref *listp = &list;
15097 /* Now that we know what section we are using for a base,
15098 actually construct the list of locations.
15099 The first location information is what is passed to the
15100 function that creates the location list, and the remaining
15101 locations just get added on to that list.
15102 Note that we only know the start address for a location
15103 (IE location changes), so to build the range, we use
15104 the range [current location start, next location start].
15105 This means we have to special case the last node, and generate
15106 a range of [last location start, end of function label]. */
15108 secname = secname_for_decl (decl);
15110 for (node = loc_list->first; node; node = node->next)
15111 if (GET_CODE (node->loc) == EXPR_LIST
15112 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15114 if (GET_CODE (node->loc) == EXPR_LIST)
15116 /* This requires DW_OP_{,bit_}piece, which is not usable
15117 inside DWARF expressions. */
15118 if (want_address != 2)
15119 continue;
15120 descr = dw_sra_loc_expr (decl, node->loc);
15121 if (descr == NULL)
15122 continue;
15124 else
15126 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15127 varloc = NOTE_VAR_LOCATION (node->loc);
15128 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15130 if (descr)
15132 /* The variable has a location between NODE->LABEL and
15133 NODE->NEXT->LABEL. */
15134 if (node->next)
15135 endname = node->next->label;
15136 /* If the variable has a location at the last label
15137 it keeps its location until the end of function. */
15138 else if (!current_function_decl)
15139 endname = text_end_label;
15140 else
15142 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15143 current_function_funcdef_no);
15144 endname = ggc_strdup (label_id);
15147 *listp = new_loc_list (descr, node->label, endname, secname);
15148 listp = &(*listp)->dw_loc_next;
15152 /* Try to avoid the overhead of a location list emitting a location
15153 expression instead, but only if we didn't have more than one
15154 location entry in the first place. If some entries were not
15155 representable, we don't want to pretend a single entry that was
15156 applies to the entire scope in which the variable is
15157 available. */
15158 if (list && loc_list->first->next)
15159 gen_llsym (list);
15161 return list;
15164 /* Return if the loc_list has only single element and thus can be represented
15165 as location description. */
15167 static bool
15168 single_element_loc_list_p (dw_loc_list_ref list)
15170 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15171 return !list->ll_symbol;
15174 /* To each location in list LIST add loc descr REF. */
15176 static void
15177 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15179 dw_loc_descr_ref copy;
15180 add_loc_descr (&list->expr, ref);
15181 list = list->dw_loc_next;
15182 while (list)
15184 copy = ggc_alloc_dw_loc_descr_node ();
15185 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15186 add_loc_descr (&list->expr, copy);
15187 while (copy->dw_loc_next)
15189 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15190 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15191 copy->dw_loc_next = new_copy;
15192 copy = new_copy;
15194 list = list->dw_loc_next;
15198 /* Given two lists RET and LIST
15199 produce location list that is result of adding expression in LIST
15200 to expression in RET on each possition in program.
15201 Might be destructive on both RET and LIST.
15203 TODO: We handle only simple cases of RET or LIST having at most one
15204 element. General case would inolve sorting the lists in program order
15205 and merging them that will need some additional work.
15206 Adding that will improve quality of debug info especially for SRA-ed
15207 structures. */
15209 static void
15210 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15212 if (!list)
15213 return;
15214 if (!*ret)
15216 *ret = list;
15217 return;
15219 if (!list->dw_loc_next)
15221 add_loc_descr_to_each (*ret, list->expr);
15222 return;
15224 if (!(*ret)->dw_loc_next)
15226 add_loc_descr_to_each (list, (*ret)->expr);
15227 *ret = list;
15228 return;
15230 expansion_failed (NULL_TREE, NULL_RTX,
15231 "Don't know how to merge two non-trivial"
15232 " location lists.\n");
15233 *ret = NULL;
15234 return;
15237 /* LOC is constant expression. Try a luck, look it up in constant
15238 pool and return its loc_descr of its address. */
15240 static dw_loc_descr_ref
15241 cst_pool_loc_descr (tree loc)
15243 /* Get an RTL for this, if something has been emitted. */
15244 rtx rtl = lookup_constant_def (loc);
15245 enum machine_mode mode;
15247 if (!rtl || !MEM_P (rtl))
15249 gcc_assert (!rtl);
15250 return 0;
15252 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15254 /* TODO: We might get more coverage if we was actually delaying expansion
15255 of all expressions till end of compilation when constant pools are fully
15256 populated. */
15257 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15259 expansion_failed (loc, NULL_RTX,
15260 "CST value in contant pool but not marked.");
15261 return 0;
15263 mode = GET_MODE (rtl);
15264 rtl = XEXP (rtl, 0);
15265 return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15268 /* Return dw_loc_list representing address of addr_expr LOC
15269 by looking for innder INDIRECT_REF expression and turing it
15270 into simple arithmetics. */
15272 static dw_loc_list_ref
15273 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15275 tree obj, offset;
15276 HOST_WIDE_INT bitsize, bitpos, bytepos;
15277 enum machine_mode mode;
15278 int volatilep;
15279 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15280 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15282 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15283 &bitsize, &bitpos, &offset, &mode,
15284 &unsignedp, &volatilep, false);
15285 STRIP_NOPS (obj);
15286 if (bitpos % BITS_PER_UNIT)
15288 expansion_failed (loc, NULL_RTX, "bitfield access");
15289 return 0;
15291 if (!INDIRECT_REF_P (obj))
15293 expansion_failed (obj,
15294 NULL_RTX, "no indirect ref in inner refrence");
15295 return 0;
15297 if (!offset && !bitpos)
15298 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15299 else if (toplev
15300 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15301 && (dwarf_version >= 4 || !dwarf_strict))
15303 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15304 if (!list_ret)
15305 return 0;
15306 if (offset)
15308 /* Variable offset. */
15309 list_ret1 = loc_list_from_tree (offset, 0);
15310 if (list_ret1 == 0)
15311 return 0;
15312 add_loc_list (&list_ret, list_ret1);
15313 if (!list_ret)
15314 return 0;
15315 add_loc_descr_to_each (list_ret,
15316 new_loc_descr (DW_OP_plus, 0, 0));
15318 bytepos = bitpos / BITS_PER_UNIT;
15319 if (bytepos > 0)
15320 add_loc_descr_to_each (list_ret,
15321 new_loc_descr (DW_OP_plus_uconst,
15322 bytepos, 0));
15323 else if (bytepos < 0)
15324 loc_list_plus_const (list_ret, bytepos);
15325 add_loc_descr_to_each (list_ret,
15326 new_loc_descr (DW_OP_stack_value, 0, 0));
15328 return list_ret;
15332 /* Generate Dwarf location list representing LOC.
15333 If WANT_ADDRESS is false, expression computing LOC will be computed
15334 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15335 if WANT_ADDRESS is 2, expression computing address useable in location
15336 will be returned (i.e. DW_OP_reg can be used
15337 to refer to register values). */
15339 static dw_loc_list_ref
15340 loc_list_from_tree (tree loc, int want_address)
15342 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15343 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15344 int have_address = 0;
15345 enum dwarf_location_atom op;
15347 /* ??? Most of the time we do not take proper care for sign/zero
15348 extending the values properly. Hopefully this won't be a real
15349 problem... */
15351 switch (TREE_CODE (loc))
15353 case ERROR_MARK:
15354 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15355 return 0;
15357 case PLACEHOLDER_EXPR:
15358 /* This case involves extracting fields from an object to determine the
15359 position of other fields. We don't try to encode this here. The
15360 only user of this is Ada, which encodes the needed information using
15361 the names of types. */
15362 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15363 return 0;
15365 case CALL_EXPR:
15366 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15367 /* There are no opcodes for these operations. */
15368 return 0;
15370 case PREINCREMENT_EXPR:
15371 case PREDECREMENT_EXPR:
15372 case POSTINCREMENT_EXPR:
15373 case POSTDECREMENT_EXPR:
15374 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15375 /* There are no opcodes for these operations. */
15376 return 0;
15378 case ADDR_EXPR:
15379 /* If we already want an address, see if there is INDIRECT_REF inside
15380 e.g. for &this->field. */
15381 if (want_address)
15383 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15384 (loc, want_address == 2);
15385 if (list_ret)
15386 have_address = 1;
15387 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15388 && (ret = cst_pool_loc_descr (loc)))
15389 have_address = 1;
15391 /* Otherwise, process the argument and look for the address. */
15392 if (!list_ret && !ret)
15393 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15394 else
15396 if (want_address)
15397 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15398 return NULL;
15400 break;
15402 case VAR_DECL:
15403 if (DECL_THREAD_LOCAL_P (loc))
15405 rtx rtl;
15406 enum dwarf_location_atom first_op;
15407 enum dwarf_location_atom second_op;
15408 bool dtprel = false;
15410 if (targetm.have_tls)
15412 /* If this is not defined, we have no way to emit the
15413 data. */
15414 if (!targetm.asm_out.output_dwarf_dtprel)
15415 return 0;
15417 /* The way DW_OP_GNU_push_tls_address is specified, we
15418 can only look up addresses of objects in the current
15419 module. We used DW_OP_addr as first op, but that's
15420 wrong, because DW_OP_addr is relocated by the debug
15421 info consumer, while DW_OP_GNU_push_tls_address
15422 operand shouldn't be. */
15423 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15424 return 0;
15425 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15426 dtprel = true;
15427 second_op = DW_OP_GNU_push_tls_address;
15429 else
15431 if (!targetm.emutls.debug_form_tls_address
15432 || !(dwarf_version >= 3 || !dwarf_strict))
15433 return 0;
15434 /* We stuffed the control variable into the DECL_VALUE_EXPR
15435 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15436 no longer appear in gimple code. We used the control
15437 variable in specific so that we could pick it up here. */
15438 loc = DECL_VALUE_EXPR (loc);
15439 first_op = DW_OP_addr;
15440 second_op = DW_OP_form_tls_address;
15443 rtl = rtl_for_decl_location (loc);
15444 if (rtl == NULL_RTX)
15445 return 0;
15447 if (!MEM_P (rtl))
15448 return 0;
15449 rtl = XEXP (rtl, 0);
15450 if (! CONSTANT_P (rtl))
15451 return 0;
15453 ret = new_loc_descr (first_op, 0, 0);
15454 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15455 ret->dw_loc_oprnd1.v.val_addr = rtl;
15456 ret->dtprel = dtprel;
15458 ret1 = new_loc_descr (second_op, 0, 0);
15459 add_loc_descr (&ret, ret1);
15461 have_address = 1;
15462 break;
15464 /* FALLTHRU */
15466 case PARM_DECL:
15467 case RESULT_DECL:
15468 if (DECL_HAS_VALUE_EXPR_P (loc))
15469 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15470 want_address);
15471 /* FALLTHRU */
15473 case FUNCTION_DECL:
15475 rtx rtl;
15476 var_loc_list *loc_list = lookup_decl_loc (loc);
15478 if (loc_list && loc_list->first)
15480 list_ret = dw_loc_list (loc_list, loc, want_address);
15481 have_address = want_address != 0;
15482 break;
15484 rtl = rtl_for_decl_location (loc);
15485 if (rtl == NULL_RTX)
15487 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15488 return 0;
15490 else if (CONST_INT_P (rtl))
15492 HOST_WIDE_INT val = INTVAL (rtl);
15493 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15494 val &= GET_MODE_MASK (DECL_MODE (loc));
15495 ret = int_loc_descriptor (val);
15497 else if (GET_CODE (rtl) == CONST_STRING)
15499 expansion_failed (loc, NULL_RTX, "CONST_STRING");
15500 return 0;
15502 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15504 ret = new_loc_descr (DW_OP_addr, 0, 0);
15505 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15506 ret->dw_loc_oprnd1.v.val_addr = rtl;
15508 else
15510 enum machine_mode mode;
15512 /* Certain constructs can only be represented at top-level. */
15513 if (want_address == 2)
15515 ret = loc_descriptor (rtl, VOIDmode,
15516 VAR_INIT_STATUS_INITIALIZED);
15517 have_address = 1;
15519 else
15521 mode = GET_MODE (rtl);
15522 if (MEM_P (rtl))
15524 rtl = XEXP (rtl, 0);
15525 have_address = 1;
15527 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15529 if (!ret)
15530 expansion_failed (loc, rtl,
15531 "failed to produce loc descriptor for rtl");
15534 break;
15536 case MEM_REF:
15537 /* ??? FIXME. */
15538 if (!integer_zerop (TREE_OPERAND (loc, 1)))
15539 return 0;
15540 /* Fallthru. */
15541 case INDIRECT_REF:
15542 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15543 have_address = 1;
15544 break;
15546 case COMPOUND_EXPR:
15547 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15549 CASE_CONVERT:
15550 case VIEW_CONVERT_EXPR:
15551 case SAVE_EXPR:
15552 case MODIFY_EXPR:
15553 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15555 case COMPONENT_REF:
15556 case BIT_FIELD_REF:
15557 case ARRAY_REF:
15558 case ARRAY_RANGE_REF:
15559 case REALPART_EXPR:
15560 case IMAGPART_EXPR:
15562 tree obj, offset;
15563 HOST_WIDE_INT bitsize, bitpos, bytepos;
15564 enum machine_mode mode;
15565 int volatilep;
15566 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15568 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15569 &unsignedp, &volatilep, false);
15571 gcc_assert (obj != loc);
15573 list_ret = loc_list_from_tree (obj,
15574 want_address == 2
15575 && !bitpos && !offset ? 2 : 1);
15576 /* TODO: We can extract value of the small expression via shifting even
15577 for nonzero bitpos. */
15578 if (list_ret == 0)
15579 return 0;
15580 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15582 expansion_failed (loc, NULL_RTX,
15583 "bitfield access");
15584 return 0;
15587 if (offset != NULL_TREE)
15589 /* Variable offset. */
15590 list_ret1 = loc_list_from_tree (offset, 0);
15591 if (list_ret1 == 0)
15592 return 0;
15593 add_loc_list (&list_ret, list_ret1);
15594 if (!list_ret)
15595 return 0;
15596 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15599 bytepos = bitpos / BITS_PER_UNIT;
15600 if (bytepos > 0)
15601 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15602 else if (bytepos < 0)
15603 loc_list_plus_const (list_ret, bytepos);
15605 have_address = 1;
15606 break;
15609 case INTEGER_CST:
15610 if ((want_address || !host_integerp (loc, 0))
15611 && (ret = cst_pool_loc_descr (loc)))
15612 have_address = 1;
15613 else if (want_address == 2
15614 && host_integerp (loc, 0)
15615 && (ret = address_of_int_loc_descriptor
15616 (int_size_in_bytes (TREE_TYPE (loc)),
15617 tree_low_cst (loc, 0))))
15618 have_address = 1;
15619 else if (host_integerp (loc, 0))
15620 ret = int_loc_descriptor (tree_low_cst (loc, 0));
15621 else
15623 expansion_failed (loc, NULL_RTX,
15624 "Integer operand is not host integer");
15625 return 0;
15627 break;
15629 case CONSTRUCTOR:
15630 case REAL_CST:
15631 case STRING_CST:
15632 case COMPLEX_CST:
15633 if ((ret = cst_pool_loc_descr (loc)))
15634 have_address = 1;
15635 else
15636 /* We can construct small constants here using int_loc_descriptor. */
15637 expansion_failed (loc, NULL_RTX,
15638 "constructor or constant not in constant pool");
15639 break;
15641 case TRUTH_AND_EXPR:
15642 case TRUTH_ANDIF_EXPR:
15643 case BIT_AND_EXPR:
15644 op = DW_OP_and;
15645 goto do_binop;
15647 case TRUTH_XOR_EXPR:
15648 case BIT_XOR_EXPR:
15649 op = DW_OP_xor;
15650 goto do_binop;
15652 case TRUTH_OR_EXPR:
15653 case TRUTH_ORIF_EXPR:
15654 case BIT_IOR_EXPR:
15655 op = DW_OP_or;
15656 goto do_binop;
15658 case FLOOR_DIV_EXPR:
15659 case CEIL_DIV_EXPR:
15660 case ROUND_DIV_EXPR:
15661 case TRUNC_DIV_EXPR:
15662 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15663 return 0;
15664 op = DW_OP_div;
15665 goto do_binop;
15667 case MINUS_EXPR:
15668 op = DW_OP_minus;
15669 goto do_binop;
15671 case FLOOR_MOD_EXPR:
15672 case CEIL_MOD_EXPR:
15673 case ROUND_MOD_EXPR:
15674 case TRUNC_MOD_EXPR:
15675 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15677 op = DW_OP_mod;
15678 goto do_binop;
15680 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15681 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15682 if (list_ret == 0 || list_ret1 == 0)
15683 return 0;
15685 add_loc_list (&list_ret, list_ret1);
15686 if (list_ret == 0)
15687 return 0;
15688 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15689 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15690 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15691 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15692 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15693 break;
15695 case MULT_EXPR:
15696 op = DW_OP_mul;
15697 goto do_binop;
15699 case LSHIFT_EXPR:
15700 op = DW_OP_shl;
15701 goto do_binop;
15703 case RSHIFT_EXPR:
15704 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15705 goto do_binop;
15707 case POINTER_PLUS_EXPR:
15708 case PLUS_EXPR:
15709 if (host_integerp (TREE_OPERAND (loc, 1), 0))
15711 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15712 if (list_ret == 0)
15713 return 0;
15715 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15716 break;
15719 op = DW_OP_plus;
15720 goto do_binop;
15722 case LE_EXPR:
15723 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15724 return 0;
15726 op = DW_OP_le;
15727 goto do_binop;
15729 case GE_EXPR:
15730 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15731 return 0;
15733 op = DW_OP_ge;
15734 goto do_binop;
15736 case LT_EXPR:
15737 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15738 return 0;
15740 op = DW_OP_lt;
15741 goto do_binop;
15743 case GT_EXPR:
15744 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15745 return 0;
15747 op = DW_OP_gt;
15748 goto do_binop;
15750 case EQ_EXPR:
15751 op = DW_OP_eq;
15752 goto do_binop;
15754 case NE_EXPR:
15755 op = DW_OP_ne;
15756 goto do_binop;
15758 do_binop:
15759 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15760 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15761 if (list_ret == 0 || list_ret1 == 0)
15762 return 0;
15764 add_loc_list (&list_ret, list_ret1);
15765 if (list_ret == 0)
15766 return 0;
15767 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15768 break;
15770 case TRUTH_NOT_EXPR:
15771 case BIT_NOT_EXPR:
15772 op = DW_OP_not;
15773 goto do_unop;
15775 case ABS_EXPR:
15776 op = DW_OP_abs;
15777 goto do_unop;
15779 case NEGATE_EXPR:
15780 op = DW_OP_neg;
15781 goto do_unop;
15783 do_unop:
15784 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15785 if (list_ret == 0)
15786 return 0;
15788 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15789 break;
15791 case MIN_EXPR:
15792 case MAX_EXPR:
15794 const enum tree_code code =
15795 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15797 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15798 build2 (code, integer_type_node,
15799 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15800 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15803 /* ... fall through ... */
15805 case COND_EXPR:
15807 dw_loc_descr_ref lhs
15808 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15809 dw_loc_list_ref rhs
15810 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15811 dw_loc_descr_ref bra_node, jump_node, tmp;
15813 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15814 if (list_ret == 0 || lhs == 0 || rhs == 0)
15815 return 0;
15817 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15818 add_loc_descr_to_each (list_ret, bra_node);
15820 add_loc_list (&list_ret, rhs);
15821 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15822 add_loc_descr_to_each (list_ret, jump_node);
15824 add_loc_descr_to_each (list_ret, lhs);
15825 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15826 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15828 /* ??? Need a node to point the skip at. Use a nop. */
15829 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15830 add_loc_descr_to_each (list_ret, tmp);
15831 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15832 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15834 break;
15836 case FIX_TRUNC_EXPR:
15837 return 0;
15839 default:
15840 /* Leave front-end specific codes as simply unknown. This comes
15841 up, for instance, with the C STMT_EXPR. */
15842 if ((unsigned int) TREE_CODE (loc)
15843 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15845 expansion_failed (loc, NULL_RTX,
15846 "language specific tree node");
15847 return 0;
15850 #ifdef ENABLE_CHECKING
15851 /* Otherwise this is a generic code; we should just lists all of
15852 these explicitly. We forgot one. */
15853 gcc_unreachable ();
15854 #else
15855 /* In a release build, we want to degrade gracefully: better to
15856 generate incomplete debugging information than to crash. */
15857 return NULL;
15858 #endif
15861 if (!ret && !list_ret)
15862 return 0;
15864 if (want_address == 2 && !have_address
15865 && (dwarf_version >= 4 || !dwarf_strict))
15867 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15869 expansion_failed (loc, NULL_RTX,
15870 "DWARF address size mismatch");
15871 return 0;
15873 if (ret)
15874 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15875 else
15876 add_loc_descr_to_each (list_ret,
15877 new_loc_descr (DW_OP_stack_value, 0, 0));
15878 have_address = 1;
15880 /* Show if we can't fill the request for an address. */
15881 if (want_address && !have_address)
15883 expansion_failed (loc, NULL_RTX,
15884 "Want address and only have value");
15885 return 0;
15888 gcc_assert (!ret || !list_ret);
15890 /* If we've got an address and don't want one, dereference. */
15891 if (!want_address && have_address)
15893 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15895 if (size > DWARF2_ADDR_SIZE || size == -1)
15897 expansion_failed (loc, NULL_RTX,
15898 "DWARF address size mismatch");
15899 return 0;
15901 else if (size == DWARF2_ADDR_SIZE)
15902 op = DW_OP_deref;
15903 else
15904 op = DW_OP_deref_size;
15906 if (ret)
15907 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15908 else
15909 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15911 if (ret)
15912 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15914 return list_ret;
15917 /* Same as above but return only single location expression. */
15918 static dw_loc_descr_ref
15919 loc_descriptor_from_tree (tree loc, int want_address)
15921 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15922 if (!ret)
15923 return NULL;
15924 if (ret->dw_loc_next)
15926 expansion_failed (loc, NULL_RTX,
15927 "Location list where only loc descriptor needed");
15928 return NULL;
15930 return ret->expr;
15933 /* Given a value, round it up to the lowest multiple of `boundary'
15934 which is not less than the value itself. */
15936 static inline HOST_WIDE_INT
15937 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15939 return (((value + boundary - 1) / boundary) * boundary);
15942 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15943 pointer to the declared type for the relevant field variable, or return
15944 `integer_type_node' if the given node turns out to be an
15945 ERROR_MARK node. */
15947 static inline tree
15948 field_type (const_tree decl)
15950 tree type;
15952 if (TREE_CODE (decl) == ERROR_MARK)
15953 return integer_type_node;
15955 type = DECL_BIT_FIELD_TYPE (decl);
15956 if (type == NULL_TREE)
15957 type = TREE_TYPE (decl);
15959 return type;
15962 /* Given a pointer to a tree node, return the alignment in bits for
15963 it, or else return BITS_PER_WORD if the node actually turns out to
15964 be an ERROR_MARK node. */
15966 static inline unsigned
15967 simple_type_align_in_bits (const_tree type)
15969 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15972 static inline unsigned
15973 simple_decl_align_in_bits (const_tree decl)
15975 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15978 /* Return the result of rounding T up to ALIGN. */
15980 static inline double_int
15981 round_up_to_align (double_int t, unsigned int align)
15983 double_int alignd = uhwi_to_double_int (align);
15984 t = double_int_add (t, alignd);
15985 t = double_int_add (t, double_int_minus_one);
15986 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15987 t = double_int_mul (t, alignd);
15988 return t;
15991 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15992 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15993 or return 0 if we are unable to determine what that offset is, either
15994 because the argument turns out to be a pointer to an ERROR_MARK node, or
15995 because the offset is actually variable. (We can't handle the latter case
15996 just yet). */
15998 static HOST_WIDE_INT
15999 field_byte_offset (const_tree decl)
16001 double_int object_offset_in_bits;
16002 double_int object_offset_in_bytes;
16003 double_int bitpos_int;
16005 if (TREE_CODE (decl) == ERROR_MARK)
16006 return 0;
16008 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16010 /* We cannot yet cope with fields whose positions are variable, so
16011 for now, when we see such things, we simply return 0. Someday, we may
16012 be able to handle such cases, but it will be damn difficult. */
16013 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16014 return 0;
16016 bitpos_int = tree_to_double_int (bit_position (decl));
16018 #ifdef PCC_BITFIELD_TYPE_MATTERS
16019 if (PCC_BITFIELD_TYPE_MATTERS)
16021 tree type;
16022 tree field_size_tree;
16023 double_int deepest_bitpos;
16024 double_int field_size_in_bits;
16025 unsigned int type_align_in_bits;
16026 unsigned int decl_align_in_bits;
16027 double_int type_size_in_bits;
16029 type = field_type (decl);
16030 type_size_in_bits = double_int_type_size_in_bits (type);
16031 type_align_in_bits = simple_type_align_in_bits (type);
16033 field_size_tree = DECL_SIZE (decl);
16035 /* The size could be unspecified if there was an error, or for
16036 a flexible array member. */
16037 if (!field_size_tree)
16038 field_size_tree = bitsize_zero_node;
16040 /* If the size of the field is not constant, use the type size. */
16041 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16042 field_size_in_bits = tree_to_double_int (field_size_tree);
16043 else
16044 field_size_in_bits = type_size_in_bits;
16046 decl_align_in_bits = simple_decl_align_in_bits (decl);
16048 /* The GCC front-end doesn't make any attempt to keep track of the
16049 starting bit offset (relative to the start of the containing
16050 structure type) of the hypothetical "containing object" for a
16051 bit-field. Thus, when computing the byte offset value for the
16052 start of the "containing object" of a bit-field, we must deduce
16053 this information on our own. This can be rather tricky to do in
16054 some cases. For example, handling the following structure type
16055 definition when compiling for an i386/i486 target (which only
16056 aligns long long's to 32-bit boundaries) can be very tricky:
16058 struct S { int field1; long long field2:31; };
16060 Fortunately, there is a simple rule-of-thumb which can be used
16061 in such cases. When compiling for an i386/i486, GCC will
16062 allocate 8 bytes for the structure shown above. It decides to
16063 do this based upon one simple rule for bit-field allocation.
16064 GCC allocates each "containing object" for each bit-field at
16065 the first (i.e. lowest addressed) legitimate alignment boundary
16066 (based upon the required minimum alignment for the declared
16067 type of the field) which it can possibly use, subject to the
16068 condition that there is still enough available space remaining
16069 in the containing object (when allocated at the selected point)
16070 to fully accommodate all of the bits of the bit-field itself.
16072 This simple rule makes it obvious why GCC allocates 8 bytes for
16073 each object of the structure type shown above. When looking
16074 for a place to allocate the "containing object" for `field2',
16075 the compiler simply tries to allocate a 64-bit "containing
16076 object" at each successive 32-bit boundary (starting at zero)
16077 until it finds a place to allocate that 64- bit field such that
16078 at least 31 contiguous (and previously unallocated) bits remain
16079 within that selected 64 bit field. (As it turns out, for the
16080 example above, the compiler finds it is OK to allocate the
16081 "containing object" 64-bit field at bit-offset zero within the
16082 structure type.)
16084 Here we attempt to work backwards from the limited set of facts
16085 we're given, and we try to deduce from those facts, where GCC
16086 must have believed that the containing object started (within
16087 the structure type). The value we deduce is then used (by the
16088 callers of this routine) to generate DW_AT_location and
16089 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16090 the case of DW_AT_location, regular fields as well). */
16092 /* Figure out the bit-distance from the start of the structure to
16093 the "deepest" bit of the bit-field. */
16094 deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16096 /* This is the tricky part. Use some fancy footwork to deduce
16097 where the lowest addressed bit of the containing object must
16098 be. */
16099 object_offset_in_bits
16100 = double_int_sub (deepest_bitpos, type_size_in_bits);
16102 /* Round up to type_align by default. This works best for
16103 bitfields. */
16104 object_offset_in_bits
16105 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16107 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16109 object_offset_in_bits
16110 = double_int_sub (deepest_bitpos, type_size_in_bits);
16112 /* Round up to decl_align instead. */
16113 object_offset_in_bits
16114 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16117 else
16118 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16119 object_offset_in_bits = bitpos_int;
16121 object_offset_in_bytes
16122 = double_int_div (object_offset_in_bits,
16123 uhwi_to_double_int (BITS_PER_UNIT), true,
16124 TRUNC_DIV_EXPR);
16125 return double_int_to_shwi (object_offset_in_bytes);
16128 /* The following routines define various Dwarf attributes and any data
16129 associated with them. */
16131 /* Add a location description attribute value to a DIE.
16133 This emits location attributes suitable for whole variables and
16134 whole parameters. Note that the location attributes for struct fields are
16135 generated by the routine `data_member_location_attribute' below. */
16137 static inline void
16138 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16139 dw_loc_list_ref descr)
16141 if (descr == 0)
16142 return;
16143 if (single_element_loc_list_p (descr))
16144 add_AT_loc (die, attr_kind, descr->expr);
16145 else
16146 add_AT_loc_list (die, attr_kind, descr);
16149 /* Add DW_AT_accessibility attribute to DIE if needed. */
16151 static void
16152 add_accessibility_attribute (dw_die_ref die, tree decl)
16154 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16155 children, otherwise the default is DW_ACCESS_public. In DWARF2
16156 the default has always been DW_ACCESS_public. */
16157 if (TREE_PROTECTED (decl))
16158 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16159 else if (TREE_PRIVATE (decl))
16161 if (dwarf_version == 2
16162 || die->die_parent == NULL
16163 || die->die_parent->die_tag != DW_TAG_class_type)
16164 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16166 else if (dwarf_version > 2
16167 && die->die_parent
16168 && die->die_parent->die_tag == DW_TAG_class_type)
16169 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16172 /* Attach the specialized form of location attribute used for data members of
16173 struct and union types. In the special case of a FIELD_DECL node which
16174 represents a bit-field, the "offset" part of this special location
16175 descriptor must indicate the distance in bytes from the lowest-addressed
16176 byte of the containing struct or union type to the lowest-addressed byte of
16177 the "containing object" for the bit-field. (See the `field_byte_offset'
16178 function above).
16180 For any given bit-field, the "containing object" is a hypothetical object
16181 (of some integral or enum type) within which the given bit-field lives. The
16182 type of this hypothetical "containing object" is always the same as the
16183 declared type of the individual bit-field itself (for GCC anyway... the
16184 DWARF spec doesn't actually mandate this). Note that it is the size (in
16185 bytes) of the hypothetical "containing object" which will be given in the
16186 DW_AT_byte_size attribute for this bit-field. (See the
16187 `byte_size_attribute' function below.) It is also used when calculating the
16188 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16189 function below.) */
16191 static void
16192 add_data_member_location_attribute (dw_die_ref die, tree decl)
16194 HOST_WIDE_INT offset;
16195 dw_loc_descr_ref loc_descr = 0;
16197 if (TREE_CODE (decl) == TREE_BINFO)
16199 /* We're working on the TAG_inheritance for a base class. */
16200 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16202 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16203 aren't at a fixed offset from all (sub)objects of the same
16204 type. We need to extract the appropriate offset from our
16205 vtable. The following dwarf expression means
16207 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16209 This is specific to the V3 ABI, of course. */
16211 dw_loc_descr_ref tmp;
16213 /* Make a copy of the object address. */
16214 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16215 add_loc_descr (&loc_descr, tmp);
16217 /* Extract the vtable address. */
16218 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16219 add_loc_descr (&loc_descr, tmp);
16221 /* Calculate the address of the offset. */
16222 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16223 gcc_assert (offset < 0);
16225 tmp = int_loc_descriptor (-offset);
16226 add_loc_descr (&loc_descr, tmp);
16227 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16228 add_loc_descr (&loc_descr, tmp);
16230 /* Extract the offset. */
16231 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16232 add_loc_descr (&loc_descr, tmp);
16234 /* Add it to the object address. */
16235 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16236 add_loc_descr (&loc_descr, tmp);
16238 else
16239 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16241 else
16242 offset = field_byte_offset (decl);
16244 if (! loc_descr)
16246 if (dwarf_version > 2)
16248 /* Don't need to output a location expression, just the constant. */
16249 if (offset < 0)
16250 add_AT_int (die, DW_AT_data_member_location, offset);
16251 else
16252 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16253 return;
16255 else
16257 enum dwarf_location_atom op;
16259 /* The DWARF2 standard says that we should assume that the structure
16260 address is already on the stack, so we can specify a structure
16261 field address by using DW_OP_plus_uconst. */
16263 #ifdef MIPS_DEBUGGING_INFO
16264 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16265 operator correctly. It works only if we leave the offset on the
16266 stack. */
16267 op = DW_OP_constu;
16268 #else
16269 op = DW_OP_plus_uconst;
16270 #endif
16272 loc_descr = new_loc_descr (op, offset, 0);
16276 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16279 /* Writes integer values to dw_vec_const array. */
16281 static void
16282 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16284 while (size != 0)
16286 *dest++ = val & 0xff;
16287 val >>= 8;
16288 --size;
16292 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
16294 static HOST_WIDE_INT
16295 extract_int (const unsigned char *src, unsigned int size)
16297 HOST_WIDE_INT val = 0;
16299 src += size;
16300 while (size != 0)
16302 val <<= 8;
16303 val |= *--src & 0xff;
16304 --size;
16306 return val;
16309 /* Writes double_int values to dw_vec_const array. */
16311 static void
16312 insert_double (double_int val, unsigned char *dest)
16314 unsigned char *p0 = dest;
16315 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16317 if (WORDS_BIG_ENDIAN)
16319 p0 = p1;
16320 p1 = dest;
16323 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16324 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16327 /* Writes floating point values to dw_vec_const array. */
16329 static void
16330 insert_float (const_rtx rtl, unsigned char *array)
16332 REAL_VALUE_TYPE rv;
16333 long val[4];
16334 int i;
16336 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16337 real_to_target (val, &rv, GET_MODE (rtl));
16339 /* real_to_target puts 32-bit pieces in each long. Pack them. */
16340 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16342 insert_int (val[i], 4, array);
16343 array += 4;
16347 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16348 does not have a "location" either in memory or in a register. These
16349 things can arise in GNU C when a constant is passed as an actual parameter
16350 to an inlined function. They can also arise in C++ where declared
16351 constants do not necessarily get memory "homes". */
16353 static bool
16354 add_const_value_attribute (dw_die_ref die, rtx rtl)
16356 switch (GET_CODE (rtl))
16358 case CONST_INT:
16360 HOST_WIDE_INT val = INTVAL (rtl);
16362 if (val < 0)
16363 add_AT_int (die, DW_AT_const_value, val);
16364 else
16365 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16367 return true;
16369 case CONST_DOUBLE:
16370 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16371 floating-point constant. A CONST_DOUBLE is used whenever the
16372 constant requires more than one word in order to be adequately
16373 represented. */
16375 enum machine_mode mode = GET_MODE (rtl);
16377 if (SCALAR_FLOAT_MODE_P (mode))
16379 unsigned int length = GET_MODE_SIZE (mode);
16380 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16382 insert_float (rtl, array);
16383 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16385 else
16386 add_AT_double (die, DW_AT_const_value,
16387 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16389 return true;
16391 case CONST_VECTOR:
16393 enum machine_mode mode = GET_MODE (rtl);
16394 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16395 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16396 unsigned char *array = (unsigned char *) ggc_alloc_atomic
16397 (length * elt_size);
16398 unsigned int i;
16399 unsigned char *p;
16401 switch (GET_MODE_CLASS (mode))
16403 case MODE_VECTOR_INT:
16404 for (i = 0, p = array; i < length; i++, p += elt_size)
16406 rtx elt = CONST_VECTOR_ELT (rtl, i);
16407 double_int val = rtx_to_double_int (elt);
16409 if (elt_size <= sizeof (HOST_WIDE_INT))
16410 insert_int (double_int_to_shwi (val), elt_size, p);
16411 else
16413 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16414 insert_double (val, p);
16417 break;
16419 case MODE_VECTOR_FLOAT:
16420 for (i = 0, p = array; i < length; i++, p += elt_size)
16422 rtx elt = CONST_VECTOR_ELT (rtl, i);
16423 insert_float (elt, p);
16425 break;
16427 default:
16428 gcc_unreachable ();
16431 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16433 return true;
16435 case CONST_STRING:
16436 if (dwarf_version >= 4 || !dwarf_strict)
16438 dw_loc_descr_ref loc_result;
16439 resolve_one_addr (&rtl, NULL);
16440 rtl_addr:
16441 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16442 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16443 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16444 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16445 add_AT_loc (die, DW_AT_location, loc_result);
16446 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16447 return true;
16449 return false;
16451 case CONST:
16452 if (CONSTANT_P (XEXP (rtl, 0)))
16453 return add_const_value_attribute (die, XEXP (rtl, 0));
16454 /* FALLTHROUGH */
16455 case SYMBOL_REF:
16456 if (!const_ok_for_output (rtl))
16457 return false;
16458 case LABEL_REF:
16459 if (dwarf_version >= 4 || !dwarf_strict)
16460 goto rtl_addr;
16461 return false;
16463 case PLUS:
16464 /* In cases where an inlined instance of an inline function is passed
16465 the address of an `auto' variable (which is local to the caller) we
16466 can get a situation where the DECL_RTL of the artificial local
16467 variable (for the inlining) which acts as a stand-in for the
16468 corresponding formal parameter (of the inline function) will look
16469 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
16470 exactly a compile-time constant expression, but it isn't the address
16471 of the (artificial) local variable either. Rather, it represents the
16472 *value* which the artificial local variable always has during its
16473 lifetime. We currently have no way to represent such quasi-constant
16474 values in Dwarf, so for now we just punt and generate nothing. */
16475 return false;
16477 case HIGH:
16478 case CONST_FIXED:
16479 return false;
16481 case MEM:
16482 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16483 && MEM_READONLY_P (rtl)
16484 && GET_MODE (rtl) == BLKmode)
16486 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16487 return true;
16489 return false;
16491 default:
16492 /* No other kinds of rtx should be possible here. */
16493 gcc_unreachable ();
16495 return false;
16498 /* Determine whether the evaluation of EXPR references any variables
16499 or functions which aren't otherwise used (and therefore may not be
16500 output). */
16501 static tree
16502 reference_to_unused (tree * tp, int * walk_subtrees,
16503 void * data ATTRIBUTE_UNUSED)
16505 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16506 *walk_subtrees = 0;
16508 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16509 && ! TREE_ASM_WRITTEN (*tp))
16510 return *tp;
16511 /* ??? The C++ FE emits debug information for using decls, so
16512 putting gcc_unreachable here falls over. See PR31899. For now
16513 be conservative. */
16514 else if (!cgraph_global_info_ready
16515 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16516 return *tp;
16517 else if (TREE_CODE (*tp) == VAR_DECL)
16519 struct varpool_node *node = varpool_get_node (*tp);
16520 if (!node || !node->needed)
16521 return *tp;
16523 else if (TREE_CODE (*tp) == FUNCTION_DECL
16524 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16526 /* The call graph machinery must have finished analyzing,
16527 optimizing and gimplifying the CU by now.
16528 So if *TP has no call graph node associated
16529 to it, it means *TP will not be emitted. */
16530 if (!cgraph_get_node (*tp))
16531 return *tp;
16533 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16534 return *tp;
16536 return NULL_TREE;
16539 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16540 for use in a later add_const_value_attribute call. */
16542 static rtx
16543 rtl_for_decl_init (tree init, tree type)
16545 rtx rtl = NULL_RTX;
16547 STRIP_NOPS (init);
16549 /* If a variable is initialized with a string constant without embedded
16550 zeros, build CONST_STRING. */
16551 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16553 tree enttype = TREE_TYPE (type);
16554 tree domain = TYPE_DOMAIN (type);
16555 enum machine_mode mode = TYPE_MODE (enttype);
16557 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16558 && domain
16559 && integer_zerop (TYPE_MIN_VALUE (domain))
16560 && compare_tree_int (TYPE_MAX_VALUE (domain),
16561 TREE_STRING_LENGTH (init) - 1) == 0
16562 && ((size_t) TREE_STRING_LENGTH (init)
16563 == strlen (TREE_STRING_POINTER (init)) + 1))
16565 rtl = gen_rtx_CONST_STRING (VOIDmode,
16566 ggc_strdup (TREE_STRING_POINTER (init)));
16567 rtl = gen_rtx_MEM (BLKmode, rtl);
16568 MEM_READONLY_P (rtl) = 1;
16571 /* Other aggregates, and complex values, could be represented using
16572 CONCAT: FIXME! */
16573 else if (AGGREGATE_TYPE_P (type)
16574 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16575 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16576 || TREE_CODE (type) == COMPLEX_TYPE)
16578 /* Vectors only work if their mode is supported by the target.
16579 FIXME: generic vectors ought to work too. */
16580 else if (TREE_CODE (type) == VECTOR_TYPE
16581 && !VECTOR_MODE_P (TYPE_MODE (type)))
16583 /* If the initializer is something that we know will expand into an
16584 immediate RTL constant, expand it now. We must be careful not to
16585 reference variables which won't be output. */
16586 else if (initializer_constant_valid_p (init, type)
16587 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16589 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16590 possible. */
16591 if (TREE_CODE (type) == VECTOR_TYPE)
16592 switch (TREE_CODE (init))
16594 case VECTOR_CST:
16595 break;
16596 case CONSTRUCTOR:
16597 if (TREE_CONSTANT (init))
16599 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16600 bool constant_p = true;
16601 tree value;
16602 unsigned HOST_WIDE_INT ix;
16604 /* Even when ctor is constant, it might contain non-*_CST
16605 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16606 belong into VECTOR_CST nodes. */
16607 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16608 if (!CONSTANT_CLASS_P (value))
16610 constant_p = false;
16611 break;
16614 if (constant_p)
16616 init = build_vector_from_ctor (type, elts);
16617 break;
16620 /* FALLTHRU */
16622 default:
16623 return NULL;
16626 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16628 /* If expand_expr returns a MEM, it wasn't immediate. */
16629 gcc_assert (!rtl || !MEM_P (rtl));
16632 return rtl;
16635 /* Generate RTL for the variable DECL to represent its location. */
16637 static rtx
16638 rtl_for_decl_location (tree decl)
16640 rtx rtl;
16642 /* Here we have to decide where we are going to say the parameter "lives"
16643 (as far as the debugger is concerned). We only have a couple of
16644 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16646 DECL_RTL normally indicates where the parameter lives during most of the
16647 activation of the function. If optimization is enabled however, this
16648 could be either NULL or else a pseudo-reg. Both of those cases indicate
16649 that the parameter doesn't really live anywhere (as far as the code
16650 generation parts of GCC are concerned) during most of the function's
16651 activation. That will happen (for example) if the parameter is never
16652 referenced within the function.
16654 We could just generate a location descriptor here for all non-NULL
16655 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16656 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16657 where DECL_RTL is NULL or is a pseudo-reg.
16659 Note however that we can only get away with using DECL_INCOMING_RTL as
16660 a backup substitute for DECL_RTL in certain limited cases. In cases
16661 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16662 we can be sure that the parameter was passed using the same type as it is
16663 declared to have within the function, and that its DECL_INCOMING_RTL
16664 points us to a place where a value of that type is passed.
16666 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16667 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16668 because in these cases DECL_INCOMING_RTL points us to a value of some
16669 type which is *different* from the type of the parameter itself. Thus,
16670 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16671 such cases, the debugger would end up (for example) trying to fetch a
16672 `float' from a place which actually contains the first part of a
16673 `double'. That would lead to really incorrect and confusing
16674 output at debug-time.
16676 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16677 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
16678 are a couple of exceptions however. On little-endian machines we can
16679 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16680 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16681 an integral type that is smaller than TREE_TYPE (decl). These cases arise
16682 when (on a little-endian machine) a non-prototyped function has a
16683 parameter declared to be of type `short' or `char'. In such cases,
16684 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16685 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16686 passed `int' value. If the debugger then uses that address to fetch
16687 a `short' or a `char' (on a little-endian machine) the result will be
16688 the correct data, so we allow for such exceptional cases below.
16690 Note that our goal here is to describe the place where the given formal
16691 parameter lives during most of the function's activation (i.e. between the
16692 end of the prologue and the start of the epilogue). We'll do that as best
16693 as we can. Note however that if the given formal parameter is modified
16694 sometime during the execution of the function, then a stack backtrace (at
16695 debug-time) will show the function as having been called with the *new*
16696 value rather than the value which was originally passed in. This happens
16697 rarely enough that it is not a major problem, but it *is* a problem, and
16698 I'd like to fix it.
16700 A future version of dwarf2out.c may generate two additional attributes for
16701 any given DW_TAG_formal_parameter DIE which will describe the "passed
16702 type" and the "passed location" for the given formal parameter in addition
16703 to the attributes we now generate to indicate the "declared type" and the
16704 "active location" for each parameter. This additional set of attributes
16705 could be used by debuggers for stack backtraces. Separately, note that
16706 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16707 This happens (for example) for inlined-instances of inline function formal
16708 parameters which are never referenced. This really shouldn't be
16709 happening. All PARM_DECL nodes should get valid non-NULL
16710 DECL_INCOMING_RTL values. FIXME. */
16712 /* Use DECL_RTL as the "location" unless we find something better. */
16713 rtl = DECL_RTL_IF_SET (decl);
16715 /* When generating abstract instances, ignore everything except
16716 constants, symbols living in memory, and symbols living in
16717 fixed registers. */
16718 if (! reload_completed)
16720 if (rtl
16721 && (CONSTANT_P (rtl)
16722 || (MEM_P (rtl)
16723 && CONSTANT_P (XEXP (rtl, 0)))
16724 || (REG_P (rtl)
16725 && TREE_CODE (decl) == VAR_DECL
16726 && TREE_STATIC (decl))))
16728 rtl = targetm.delegitimize_address (rtl);
16729 return rtl;
16731 rtl = NULL_RTX;
16733 else if (TREE_CODE (decl) == PARM_DECL)
16735 if (rtl == NULL_RTX
16736 || is_pseudo_reg (rtl)
16737 || (MEM_P (rtl)
16738 && is_pseudo_reg (XEXP (rtl, 0))
16739 && DECL_INCOMING_RTL (decl)
16740 && MEM_P (DECL_INCOMING_RTL (decl))
16741 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16743 tree declared_type = TREE_TYPE (decl);
16744 tree passed_type = DECL_ARG_TYPE (decl);
16745 enum machine_mode dmode = TYPE_MODE (declared_type);
16746 enum machine_mode pmode = TYPE_MODE (passed_type);
16748 /* This decl represents a formal parameter which was optimized out.
16749 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16750 all cases where (rtl == NULL_RTX) just below. */
16751 if (dmode == pmode)
16752 rtl = DECL_INCOMING_RTL (decl);
16753 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16754 && SCALAR_INT_MODE_P (dmode)
16755 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16756 && DECL_INCOMING_RTL (decl))
16758 rtx inc = DECL_INCOMING_RTL (decl);
16759 if (REG_P (inc))
16760 rtl = inc;
16761 else if (MEM_P (inc))
16763 if (BYTES_BIG_ENDIAN)
16764 rtl = adjust_address_nv (inc, dmode,
16765 GET_MODE_SIZE (pmode)
16766 - GET_MODE_SIZE (dmode));
16767 else
16768 rtl = inc;
16773 /* If the parm was passed in registers, but lives on the stack, then
16774 make a big endian correction if the mode of the type of the
16775 parameter is not the same as the mode of the rtl. */
16776 /* ??? This is the same series of checks that are made in dbxout.c before
16777 we reach the big endian correction code there. It isn't clear if all
16778 of these checks are necessary here, but keeping them all is the safe
16779 thing to do. */
16780 else if (MEM_P (rtl)
16781 && XEXP (rtl, 0) != const0_rtx
16782 && ! CONSTANT_P (XEXP (rtl, 0))
16783 /* Not passed in memory. */
16784 && !MEM_P (DECL_INCOMING_RTL (decl))
16785 /* Not passed by invisible reference. */
16786 && (!REG_P (XEXP (rtl, 0))
16787 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16788 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16789 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16790 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16791 #endif
16793 /* Big endian correction check. */
16794 && BYTES_BIG_ENDIAN
16795 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16796 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16797 < UNITS_PER_WORD))
16799 int offset = (UNITS_PER_WORD
16800 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16802 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16803 plus_constant (XEXP (rtl, 0), offset));
16806 else if (TREE_CODE (decl) == VAR_DECL
16807 && rtl
16808 && MEM_P (rtl)
16809 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16810 && BYTES_BIG_ENDIAN)
16812 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16813 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16815 /* If a variable is declared "register" yet is smaller than
16816 a register, then if we store the variable to memory, it
16817 looks like we're storing a register-sized value, when in
16818 fact we are not. We need to adjust the offset of the
16819 storage location to reflect the actual value's bytes,
16820 else gdb will not be able to display it. */
16821 if (rsize > dsize)
16822 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16823 plus_constant (XEXP (rtl, 0), rsize-dsize));
16826 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16827 and will have been substituted directly into all expressions that use it.
16828 C does not have such a concept, but C++ and other languages do. */
16829 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16830 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16832 if (rtl)
16833 rtl = targetm.delegitimize_address (rtl);
16835 /* If we don't look past the constant pool, we risk emitting a
16836 reference to a constant pool entry that isn't referenced from
16837 code, and thus is not emitted. */
16838 if (rtl)
16839 rtl = avoid_constant_pool_reference (rtl);
16841 /* Try harder to get a rtl. If this symbol ends up not being emitted
16842 in the current CU, resolve_addr will remove the expression referencing
16843 it. */
16844 if (rtl == NULL_RTX
16845 && TREE_CODE (decl) == VAR_DECL
16846 && !DECL_EXTERNAL (decl)
16847 && TREE_STATIC (decl)
16848 && DECL_NAME (decl)
16849 && !DECL_HARD_REGISTER (decl)
16850 && DECL_MODE (decl) != VOIDmode)
16852 rtl = make_decl_rtl_for_debug (decl);
16853 if (!MEM_P (rtl)
16854 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16855 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16856 rtl = NULL_RTX;
16859 return rtl;
16862 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16863 returned. If so, the decl for the COMMON block is returned, and the
16864 value is the offset into the common block for the symbol. */
16866 static tree
16867 fortran_common (tree decl, HOST_WIDE_INT *value)
16869 tree val_expr, cvar;
16870 enum machine_mode mode;
16871 HOST_WIDE_INT bitsize, bitpos;
16872 tree offset;
16873 int volatilep = 0, unsignedp = 0;
16875 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16876 it does not have a value (the offset into the common area), or if it
16877 is thread local (as opposed to global) then it isn't common, and shouldn't
16878 be handled as such. */
16879 if (TREE_CODE (decl) != VAR_DECL
16880 || !TREE_STATIC (decl)
16881 || !DECL_HAS_VALUE_EXPR_P (decl)
16882 || !is_fortran ())
16883 return NULL_TREE;
16885 val_expr = DECL_VALUE_EXPR (decl);
16886 if (TREE_CODE (val_expr) != COMPONENT_REF)
16887 return NULL_TREE;
16889 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16890 &mode, &unsignedp, &volatilep, true);
16892 if (cvar == NULL_TREE
16893 || TREE_CODE (cvar) != VAR_DECL
16894 || DECL_ARTIFICIAL (cvar)
16895 || !TREE_PUBLIC (cvar))
16896 return NULL_TREE;
16898 *value = 0;
16899 if (offset != NULL)
16901 if (!host_integerp (offset, 0))
16902 return NULL_TREE;
16903 *value = tree_low_cst (offset, 0);
16905 if (bitpos != 0)
16906 *value += bitpos / BITS_PER_UNIT;
16908 return cvar;
16911 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16912 data attribute for a variable or a parameter. We generate the
16913 DW_AT_const_value attribute only in those cases where the given variable
16914 or parameter does not have a true "location" either in memory or in a
16915 register. This can happen (for example) when a constant is passed as an
16916 actual argument in a call to an inline function. (It's possible that
16917 these things can crop up in other ways also.) Note that one type of
16918 constant value which can be passed into an inlined function is a constant
16919 pointer. This can happen for example if an actual argument in an inlined
16920 function call evaluates to a compile-time constant address. */
16922 static bool
16923 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16924 enum dwarf_attribute attr)
16926 rtx rtl;
16927 dw_loc_list_ref list;
16928 var_loc_list *loc_list;
16930 if (TREE_CODE (decl) == ERROR_MARK)
16931 return false;
16933 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16934 || TREE_CODE (decl) == RESULT_DECL);
16936 /* Try to get some constant RTL for this decl, and use that as the value of
16937 the location. */
16939 rtl = rtl_for_decl_location (decl);
16940 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16941 && add_const_value_attribute (die, rtl))
16942 return true;
16944 /* See if we have single element location list that is equivalent to
16945 a constant value. That way we are better to use add_const_value_attribute
16946 rather than expanding constant value equivalent. */
16947 loc_list = lookup_decl_loc (decl);
16948 if (loc_list
16949 && loc_list->first
16950 && loc_list->first->next == NULL
16951 && NOTE_P (loc_list->first->loc)
16952 && NOTE_VAR_LOCATION (loc_list->first->loc)
16953 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16955 struct var_loc_node *node;
16957 node = loc_list->first;
16958 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16959 if (GET_CODE (rtl) == EXPR_LIST)
16960 rtl = XEXP (rtl, 0);
16961 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16962 && add_const_value_attribute (die, rtl))
16963 return true;
16965 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16966 if (list)
16968 add_AT_location_description (die, attr, list);
16969 return true;
16971 /* None of that worked, so it must not really have a location;
16972 try adding a constant value attribute from the DECL_INITIAL. */
16973 return tree_add_const_value_attribute_for_decl (die, decl);
16976 /* Add VARIABLE and DIE into deferred locations list. */
16978 static void
16979 defer_location (tree variable, dw_die_ref die)
16981 deferred_locations entry;
16982 entry.variable = variable;
16983 entry.die = die;
16984 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16987 /* Helper function for tree_add_const_value_attribute. Natively encode
16988 initializer INIT into an array. Return true if successful. */
16990 static bool
16991 native_encode_initializer (tree init, unsigned char *array, int size)
16993 tree type;
16995 if (init == NULL_TREE)
16996 return false;
16998 STRIP_NOPS (init);
16999 switch (TREE_CODE (init))
17001 case STRING_CST:
17002 type = TREE_TYPE (init);
17003 if (TREE_CODE (type) == ARRAY_TYPE)
17005 tree enttype = TREE_TYPE (type);
17006 enum machine_mode mode = TYPE_MODE (enttype);
17008 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17009 return false;
17010 if (int_size_in_bytes (type) != size)
17011 return false;
17012 if (size > TREE_STRING_LENGTH (init))
17014 memcpy (array, TREE_STRING_POINTER (init),
17015 TREE_STRING_LENGTH (init));
17016 memset (array + TREE_STRING_LENGTH (init),
17017 '\0', size - TREE_STRING_LENGTH (init));
17019 else
17020 memcpy (array, TREE_STRING_POINTER (init), size);
17021 return true;
17023 return false;
17024 case CONSTRUCTOR:
17025 type = TREE_TYPE (init);
17026 if (int_size_in_bytes (type) != size)
17027 return false;
17028 if (TREE_CODE (type) == ARRAY_TYPE)
17030 HOST_WIDE_INT min_index;
17031 unsigned HOST_WIDE_INT cnt;
17032 int curpos = 0, fieldsize;
17033 constructor_elt *ce;
17035 if (TYPE_DOMAIN (type) == NULL_TREE
17036 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17037 return false;
17039 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17040 if (fieldsize <= 0)
17041 return false;
17043 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17044 memset (array, '\0', size);
17045 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17047 tree val = ce->value;
17048 tree index = ce->index;
17049 int pos = curpos;
17050 if (index && TREE_CODE (index) == RANGE_EXPR)
17051 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17052 * fieldsize;
17053 else if (index)
17054 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17056 if (val)
17058 STRIP_NOPS (val);
17059 if (!native_encode_initializer (val, array + pos, fieldsize))
17060 return false;
17062 curpos = pos + fieldsize;
17063 if (index && TREE_CODE (index) == RANGE_EXPR)
17065 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17066 - tree_low_cst (TREE_OPERAND (index, 0), 0);
17067 while (count > 0)
17069 if (val)
17070 memcpy (array + curpos, array + pos, fieldsize);
17071 curpos += fieldsize;
17074 gcc_assert (curpos <= size);
17076 return true;
17078 else if (TREE_CODE (type) == RECORD_TYPE
17079 || TREE_CODE (type) == UNION_TYPE)
17081 tree field = NULL_TREE;
17082 unsigned HOST_WIDE_INT cnt;
17083 constructor_elt *ce;
17085 if (int_size_in_bytes (type) != size)
17086 return false;
17088 if (TREE_CODE (type) == RECORD_TYPE)
17089 field = TYPE_FIELDS (type);
17091 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17093 tree val = ce->value;
17094 int pos, fieldsize;
17096 if (ce->index != 0)
17097 field = ce->index;
17099 if (val)
17100 STRIP_NOPS (val);
17102 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17103 return false;
17105 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17106 && TYPE_DOMAIN (TREE_TYPE (field))
17107 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17108 return false;
17109 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17110 || !host_integerp (DECL_SIZE_UNIT (field), 0))
17111 return false;
17112 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17113 pos = int_byte_position (field);
17114 gcc_assert (pos + fieldsize <= size);
17115 if (val
17116 && !native_encode_initializer (val, array + pos, fieldsize))
17117 return false;
17119 return true;
17121 return false;
17122 case VIEW_CONVERT_EXPR:
17123 case NON_LVALUE_EXPR:
17124 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17125 default:
17126 return native_encode_expr (init, array, size) == size;
17130 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17131 attribute is the const value T. */
17133 static bool
17134 tree_add_const_value_attribute (dw_die_ref die, tree t)
17136 tree init;
17137 tree type = TREE_TYPE (t);
17138 rtx rtl;
17140 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17141 return false;
17143 init = t;
17144 gcc_assert (!DECL_P (init));
17146 rtl = rtl_for_decl_init (init, type);
17147 if (rtl)
17148 return add_const_value_attribute (die, rtl);
17149 /* If the host and target are sane, try harder. */
17150 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17151 && initializer_constant_valid_p (init, type))
17153 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17154 if (size > 0 && (int) size == size)
17156 unsigned char *array = (unsigned char *)
17157 ggc_alloc_cleared_atomic (size);
17159 if (native_encode_initializer (init, array, size))
17161 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17162 return true;
17166 return false;
17169 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17170 attribute is the const value of T, where T is an integral constant
17171 variable with static storage duration
17172 (so it can't be a PARM_DECL or a RESULT_DECL). */
17174 static bool
17175 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17178 if (!decl
17179 || (TREE_CODE (decl) != VAR_DECL
17180 && TREE_CODE (decl) != CONST_DECL))
17181 return false;
17183 if (TREE_READONLY (decl)
17184 && ! TREE_THIS_VOLATILE (decl)
17185 && DECL_INITIAL (decl))
17186 /* OK */;
17187 else
17188 return false;
17190 /* Don't add DW_AT_const_value if abstract origin already has one. */
17191 if (get_AT (var_die, DW_AT_const_value))
17192 return false;
17194 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17197 /* Convert the CFI instructions for the current function into a
17198 location list. This is used for DW_AT_frame_base when we targeting
17199 a dwarf2 consumer that does not support the dwarf3
17200 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17201 expressions. */
17203 static dw_loc_list_ref
17204 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17206 dw_fde_ref fde;
17207 dw_loc_list_ref list, *list_tail;
17208 dw_cfi_ref cfi;
17209 dw_cfa_location last_cfa, next_cfa;
17210 const char *start_label, *last_label, *section;
17211 dw_cfa_location remember;
17213 fde = current_fde ();
17214 gcc_assert (fde != NULL);
17216 section = secname_for_decl (current_function_decl);
17217 list_tail = &list;
17218 list = NULL;
17220 memset (&next_cfa, 0, sizeof (next_cfa));
17221 next_cfa.reg = INVALID_REGNUM;
17222 remember = next_cfa;
17224 start_label = fde->dw_fde_begin;
17226 /* ??? Bald assumption that the CIE opcode list does not contain
17227 advance opcodes. */
17228 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17229 lookup_cfa_1 (cfi, &next_cfa, &remember);
17231 last_cfa = next_cfa;
17232 last_label = start_label;
17234 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17235 switch (cfi->dw_cfi_opc)
17237 case DW_CFA_set_loc:
17238 case DW_CFA_advance_loc1:
17239 case DW_CFA_advance_loc2:
17240 case DW_CFA_advance_loc4:
17241 if (!cfa_equal_p (&last_cfa, &next_cfa))
17243 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17244 start_label, last_label, section);
17246 list_tail = &(*list_tail)->dw_loc_next;
17247 last_cfa = next_cfa;
17248 start_label = last_label;
17250 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17251 break;
17253 case DW_CFA_advance_loc:
17254 /* The encoding is complex enough that we should never emit this. */
17255 gcc_unreachable ();
17257 default:
17258 lookup_cfa_1 (cfi, &next_cfa, &remember);
17259 break;
17262 if (!cfa_equal_p (&last_cfa, &next_cfa))
17264 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17265 start_label, last_label, section);
17266 list_tail = &(*list_tail)->dw_loc_next;
17267 start_label = last_label;
17270 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17271 start_label, fde->dw_fde_end, section);
17273 if (list && list->dw_loc_next)
17274 gen_llsym (list);
17276 return list;
17279 /* Compute a displacement from the "steady-state frame pointer" to the
17280 frame base (often the same as the CFA), and store it in
17281 frame_pointer_fb_offset. OFFSET is added to the displacement
17282 before the latter is negated. */
17284 static void
17285 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17287 rtx reg, elim;
17289 #ifdef FRAME_POINTER_CFA_OFFSET
17290 reg = frame_pointer_rtx;
17291 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17292 #else
17293 reg = arg_pointer_rtx;
17294 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17295 #endif
17297 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17298 if (GET_CODE (elim) == PLUS)
17300 offset += INTVAL (XEXP (elim, 1));
17301 elim = XEXP (elim, 0);
17304 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17305 && (elim == hard_frame_pointer_rtx
17306 || elim == stack_pointer_rtx))
17307 || elim == (frame_pointer_needed
17308 ? hard_frame_pointer_rtx
17309 : stack_pointer_rtx));
17311 frame_pointer_fb_offset = -offset;
17314 /* Generate a DW_AT_name attribute given some string value to be included as
17315 the value of the attribute. */
17317 static void
17318 add_name_attribute (dw_die_ref die, const char *name_string)
17320 if (name_string != NULL && *name_string != 0)
17322 if (demangle_name_func)
17323 name_string = (*demangle_name_func) (name_string);
17325 add_AT_string (die, DW_AT_name, name_string);
17329 /* Generate a DW_AT_comp_dir attribute for DIE. */
17331 static void
17332 add_comp_dir_attribute (dw_die_ref die)
17334 const char *wd = get_src_pwd ();
17335 char *wd1;
17337 if (wd == NULL)
17338 return;
17340 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17342 int wdlen;
17344 wdlen = strlen (wd);
17345 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17346 strcpy (wd1, wd);
17347 wd1 [wdlen] = DIR_SEPARATOR;
17348 wd1 [wdlen + 1] = 0;
17349 wd = wd1;
17352 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17355 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17356 default. */
17358 static int
17359 lower_bound_default (void)
17361 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17363 case DW_LANG_C:
17364 case DW_LANG_C89:
17365 case DW_LANG_C99:
17366 case DW_LANG_C_plus_plus:
17367 case DW_LANG_ObjC:
17368 case DW_LANG_ObjC_plus_plus:
17369 case DW_LANG_Java:
17370 return 0;
17371 case DW_LANG_Fortran77:
17372 case DW_LANG_Fortran90:
17373 case DW_LANG_Fortran95:
17374 return 1;
17375 case DW_LANG_UPC:
17376 case DW_LANG_D:
17377 case DW_LANG_Python:
17378 return dwarf_version >= 4 ? 0 : -1;
17379 case DW_LANG_Ada95:
17380 case DW_LANG_Ada83:
17381 case DW_LANG_Cobol74:
17382 case DW_LANG_Cobol85:
17383 case DW_LANG_Pascal83:
17384 case DW_LANG_Modula2:
17385 case DW_LANG_PLI:
17386 return dwarf_version >= 4 ? 1 : -1;
17387 default:
17388 return -1;
17392 /* Given a tree node describing an array bound (either lower or upper) output
17393 a representation for that bound. */
17395 static void
17396 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17398 switch (TREE_CODE (bound))
17400 case ERROR_MARK:
17401 return;
17403 /* All fixed-bounds are represented by INTEGER_CST nodes. */
17404 case INTEGER_CST:
17406 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17407 int dflt;
17409 /* Use the default if possible. */
17410 if (bound_attr == DW_AT_lower_bound
17411 && host_integerp (bound, 0)
17412 && (dflt = lower_bound_default ()) != -1
17413 && tree_low_cst (bound, 0) == dflt)
17416 /* Otherwise represent the bound as an unsigned value with the
17417 precision of its type. The precision and signedness of the
17418 type will be necessary to re-interpret it unambiguously. */
17419 else if (prec < HOST_BITS_PER_WIDE_INT)
17421 unsigned HOST_WIDE_INT mask
17422 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17423 add_AT_unsigned (subrange_die, bound_attr,
17424 TREE_INT_CST_LOW (bound) & mask);
17426 else if (prec == HOST_BITS_PER_WIDE_INT
17427 || TREE_INT_CST_HIGH (bound) == 0)
17428 add_AT_unsigned (subrange_die, bound_attr,
17429 TREE_INT_CST_LOW (bound));
17430 else
17431 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17432 TREE_INT_CST_LOW (bound));
17434 break;
17436 CASE_CONVERT:
17437 case VIEW_CONVERT_EXPR:
17438 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17439 break;
17441 case SAVE_EXPR:
17442 break;
17444 case VAR_DECL:
17445 case PARM_DECL:
17446 case RESULT_DECL:
17448 dw_die_ref decl_die = lookup_decl_die (bound);
17450 /* ??? Can this happen, or should the variable have been bound
17451 first? Probably it can, since I imagine that we try to create
17452 the types of parameters in the order in which they exist in
17453 the list, and won't have created a forward reference to a
17454 later parameter. */
17455 if (decl_die != NULL)
17457 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17458 break;
17461 /* FALLTHRU */
17463 default:
17465 /* Otherwise try to create a stack operation procedure to
17466 evaluate the value of the array bound. */
17468 dw_die_ref ctx, decl_die;
17469 dw_loc_list_ref list;
17471 list = loc_list_from_tree (bound, 2);
17472 if (list == NULL || single_element_loc_list_p (list))
17474 /* If DW_AT_*bound is not a reference nor constant, it is
17475 a DWARF expression rather than location description.
17476 For that loc_list_from_tree (bound, 0) is needed.
17477 If that fails to give a single element list,
17478 fall back to outputting this as a reference anyway. */
17479 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17480 if (list2 && single_element_loc_list_p (list2))
17482 add_AT_loc (subrange_die, bound_attr, list2->expr);
17483 break;
17486 if (list == NULL)
17487 break;
17489 if (current_function_decl == 0)
17490 ctx = comp_unit_die ();
17491 else
17492 ctx = lookup_decl_die (current_function_decl);
17494 decl_die = new_die (DW_TAG_variable, ctx, bound);
17495 add_AT_flag (decl_die, DW_AT_artificial, 1);
17496 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17497 add_AT_location_description (decl_die, DW_AT_location, list);
17498 add_AT_die_ref (subrange_die, bound_attr, decl_die);
17499 break;
17504 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17505 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17506 Note that the block of subscript information for an array type also
17507 includes information about the element type of the given array type. */
17509 static void
17510 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17512 unsigned dimension_number;
17513 tree lower, upper;
17514 dw_die_ref subrange_die;
17516 for (dimension_number = 0;
17517 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17518 type = TREE_TYPE (type), dimension_number++)
17520 tree domain = TYPE_DOMAIN (type);
17522 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17523 break;
17525 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17526 and (in GNU C only) variable bounds. Handle all three forms
17527 here. */
17528 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17529 if (domain)
17531 /* We have an array type with specified bounds. */
17532 lower = TYPE_MIN_VALUE (domain);
17533 upper = TYPE_MAX_VALUE (domain);
17535 /* Define the index type. */
17536 if (TREE_TYPE (domain))
17538 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17539 TREE_TYPE field. We can't emit debug info for this
17540 because it is an unnamed integral type. */
17541 if (TREE_CODE (domain) == INTEGER_TYPE
17542 && TYPE_NAME (domain) == NULL_TREE
17543 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17544 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17546 else
17547 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17548 type_die);
17551 /* ??? If upper is NULL, the array has unspecified length,
17552 but it does have a lower bound. This happens with Fortran
17553 dimension arr(N:*)
17554 Since the debugger is definitely going to need to know N
17555 to produce useful results, go ahead and output the lower
17556 bound solo, and hope the debugger can cope. */
17558 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17559 if (upper)
17560 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17563 /* Otherwise we have an array type with an unspecified length. The
17564 DWARF-2 spec does not say how to handle this; let's just leave out the
17565 bounds. */
17569 static void
17570 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17572 unsigned size;
17574 switch (TREE_CODE (tree_node))
17576 case ERROR_MARK:
17577 size = 0;
17578 break;
17579 case ENUMERAL_TYPE:
17580 case RECORD_TYPE:
17581 case UNION_TYPE:
17582 case QUAL_UNION_TYPE:
17583 size = int_size_in_bytes (tree_node);
17584 break;
17585 case FIELD_DECL:
17586 /* For a data member of a struct or union, the DW_AT_byte_size is
17587 generally given as the number of bytes normally allocated for an
17588 object of the *declared* type of the member itself. This is true
17589 even for bit-fields. */
17590 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17591 break;
17592 default:
17593 gcc_unreachable ();
17596 /* Note that `size' might be -1 when we get to this point. If it is, that
17597 indicates that the byte size of the entity in question is variable. We
17598 have no good way of expressing this fact in Dwarf at the present time,
17599 so just let the -1 pass on through. */
17600 add_AT_unsigned (die, DW_AT_byte_size, size);
17603 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17604 which specifies the distance in bits from the highest order bit of the
17605 "containing object" for the bit-field to the highest order bit of the
17606 bit-field itself.
17608 For any given bit-field, the "containing object" is a hypothetical object
17609 (of some integral or enum type) within which the given bit-field lives. The
17610 type of this hypothetical "containing object" is always the same as the
17611 declared type of the individual bit-field itself. The determination of the
17612 exact location of the "containing object" for a bit-field is rather
17613 complicated. It's handled by the `field_byte_offset' function (above).
17615 Note that it is the size (in bytes) of the hypothetical "containing object"
17616 which will be given in the DW_AT_byte_size attribute for this bit-field.
17617 (See `byte_size_attribute' above). */
17619 static inline void
17620 add_bit_offset_attribute (dw_die_ref die, tree decl)
17622 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17623 tree type = DECL_BIT_FIELD_TYPE (decl);
17624 HOST_WIDE_INT bitpos_int;
17625 HOST_WIDE_INT highest_order_object_bit_offset;
17626 HOST_WIDE_INT highest_order_field_bit_offset;
17627 HOST_WIDE_INT bit_offset;
17629 /* Must be a field and a bit field. */
17630 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17632 /* We can't yet handle bit-fields whose offsets are variable, so if we
17633 encounter such things, just return without generating any attribute
17634 whatsoever. Likewise for variable or too large size. */
17635 if (! host_integerp (bit_position (decl), 0)
17636 || ! host_integerp (DECL_SIZE (decl), 1))
17637 return;
17639 bitpos_int = int_bit_position (decl);
17641 /* Note that the bit offset is always the distance (in bits) from the
17642 highest-order bit of the "containing object" to the highest-order bit of
17643 the bit-field itself. Since the "high-order end" of any object or field
17644 is different on big-endian and little-endian machines, the computation
17645 below must take account of these differences. */
17646 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17647 highest_order_field_bit_offset = bitpos_int;
17649 if (! BYTES_BIG_ENDIAN)
17651 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17652 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17655 bit_offset
17656 = (! BYTES_BIG_ENDIAN
17657 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17658 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17660 if (bit_offset < 0)
17661 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17662 else
17663 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17666 /* For a FIELD_DECL node which represents a bit field, output an attribute
17667 which specifies the length in bits of the given field. */
17669 static inline void
17670 add_bit_size_attribute (dw_die_ref die, tree decl)
17672 /* Must be a field and a bit field. */
17673 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17674 && DECL_BIT_FIELD_TYPE (decl));
17676 if (host_integerp (DECL_SIZE (decl), 1))
17677 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17680 /* If the compiled language is ANSI C, then add a 'prototyped'
17681 attribute, if arg types are given for the parameters of a function. */
17683 static inline void
17684 add_prototyped_attribute (dw_die_ref die, tree func_type)
17686 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17687 && prototype_p (func_type))
17688 add_AT_flag (die, DW_AT_prototyped, 1);
17691 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17692 by looking in either the type declaration or object declaration
17693 equate table. */
17695 static inline dw_die_ref
17696 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17698 dw_die_ref origin_die = NULL;
17700 if (TREE_CODE (origin) != FUNCTION_DECL)
17702 /* We may have gotten separated from the block for the inlined
17703 function, if we're in an exception handler or some such; make
17704 sure that the abstract function has been written out.
17706 Doing this for nested functions is wrong, however; functions are
17707 distinct units, and our context might not even be inline. */
17708 tree fn = origin;
17710 if (TYPE_P (fn))
17711 fn = TYPE_STUB_DECL (fn);
17713 fn = decl_function_context (fn);
17714 if (fn)
17715 dwarf2out_abstract_function (fn);
17718 if (DECL_P (origin))
17719 origin_die = lookup_decl_die (origin);
17720 else if (TYPE_P (origin))
17721 origin_die = lookup_type_die (origin);
17723 /* XXX: Functions that are never lowered don't always have correct block
17724 trees (in the case of java, they simply have no block tree, in some other
17725 languages). For these functions, there is nothing we can really do to
17726 output correct debug info for inlined functions in all cases. Rather
17727 than die, we'll just produce deficient debug info now, in that we will
17728 have variables without a proper abstract origin. In the future, when all
17729 functions are lowered, we should re-add a gcc_assert (origin_die)
17730 here. */
17732 if (origin_die)
17733 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17734 return origin_die;
17737 /* We do not currently support the pure_virtual attribute. */
17739 static inline void
17740 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17742 if (DECL_VINDEX (func_decl))
17744 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17746 if (host_integerp (DECL_VINDEX (func_decl), 0))
17747 add_AT_loc (die, DW_AT_vtable_elem_location,
17748 new_loc_descr (DW_OP_constu,
17749 tree_low_cst (DECL_VINDEX (func_decl), 0),
17750 0));
17752 /* GNU extension: Record what type this method came from originally. */
17753 if (debug_info_level > DINFO_LEVEL_TERSE
17754 && DECL_CONTEXT (func_decl))
17755 add_AT_die_ref (die, DW_AT_containing_type,
17756 lookup_type_die (DECL_CONTEXT (func_decl)));
17760 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17761 given decl. This used to be a vendor extension until after DWARF 4
17762 standardized it. */
17764 static void
17765 add_linkage_attr (dw_die_ref die, tree decl)
17767 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17769 /* Mimic what assemble_name_raw does with a leading '*'. */
17770 if (name[0] == '*')
17771 name = &name[1];
17773 if (dwarf_version >= 4)
17774 add_AT_string (die, DW_AT_linkage_name, name);
17775 else
17776 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17779 /* Add source coordinate attributes for the given decl. */
17781 static void
17782 add_src_coords_attributes (dw_die_ref die, tree decl)
17784 expanded_location s;
17786 if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17787 return;
17788 s = expand_location (DECL_SOURCE_LOCATION (decl));
17789 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17790 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17793 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17795 static void
17796 add_linkage_name (dw_die_ref die, tree decl)
17798 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17799 && TREE_PUBLIC (decl)
17800 && !DECL_ABSTRACT (decl)
17801 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17802 && die->die_tag != DW_TAG_member)
17804 /* Defer until we have an assembler name set. */
17805 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17807 limbo_die_node *asm_name;
17809 asm_name = ggc_alloc_cleared_limbo_die_node ();
17810 asm_name->die = die;
17811 asm_name->created_for = decl;
17812 asm_name->next = deferred_asm_name;
17813 deferred_asm_name = asm_name;
17815 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17816 add_linkage_attr (die, decl);
17820 /* Add a DW_AT_name attribute and source coordinate attribute for the
17821 given decl, but only if it actually has a name. */
17823 static void
17824 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17826 tree decl_name;
17828 decl_name = DECL_NAME (decl);
17829 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17831 const char *name = dwarf2_name (decl, 0);
17832 if (name)
17833 add_name_attribute (die, name);
17834 if (! DECL_ARTIFICIAL (decl))
17835 add_src_coords_attributes (die, decl);
17837 add_linkage_name (die, decl);
17840 #ifdef VMS_DEBUGGING_INFO
17841 /* Get the function's name, as described by its RTL. This may be different
17842 from the DECL_NAME name used in the source file. */
17843 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17845 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17846 XEXP (DECL_RTL (decl), 0));
17847 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17849 #endif /* VMS_DEBUGGING_INFO */
17852 #ifdef VMS_DEBUGGING_INFO
17853 /* Output the debug main pointer die for VMS */
17855 void
17856 dwarf2out_vms_debug_main_pointer (void)
17858 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17859 dw_die_ref die;
17861 /* Allocate the VMS debug main subprogram die. */
17862 die = ggc_alloc_cleared_die_node ();
17863 die->die_tag = DW_TAG_subprogram;
17864 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17865 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17866 current_function_funcdef_no);
17867 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17869 /* Make it the first child of comp_unit_die (). */
17870 die->die_parent = comp_unit_die ();
17871 if (comp_unit_die ()->die_child)
17873 die->die_sib = comp_unit_die ()->die_child->die_sib;
17874 comp_unit_die ()->die_child->die_sib = die;
17876 else
17878 die->die_sib = die;
17879 comp_unit_die ()->die_child = die;
17882 #endif /* VMS_DEBUGGING_INFO */
17884 /* Push a new declaration scope. */
17886 static void
17887 push_decl_scope (tree scope)
17889 VEC_safe_push (tree, gc, decl_scope_table, scope);
17892 /* Pop a declaration scope. */
17894 static inline void
17895 pop_decl_scope (void)
17897 VEC_pop (tree, decl_scope_table);
17900 /* Return the DIE for the scope that immediately contains this type.
17901 Non-named types get global scope. Named types nested in other
17902 types get their containing scope if it's open, or global scope
17903 otherwise. All other types (i.e. function-local named types) get
17904 the current active scope. */
17906 static dw_die_ref
17907 scope_die_for (tree t, dw_die_ref context_die)
17909 dw_die_ref scope_die = NULL;
17910 tree containing_scope;
17911 int i;
17913 /* Non-types always go in the current scope. */
17914 gcc_assert (TYPE_P (t));
17916 containing_scope = TYPE_CONTEXT (t);
17918 /* Use the containing namespace if it was passed in (for a declaration). */
17919 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17921 if (context_die == lookup_decl_die (containing_scope))
17922 /* OK */;
17923 else
17924 containing_scope = NULL_TREE;
17927 /* Ignore function type "scopes" from the C frontend. They mean that
17928 a tagged type is local to a parmlist of a function declarator, but
17929 that isn't useful to DWARF. */
17930 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17931 containing_scope = NULL_TREE;
17933 if (SCOPE_FILE_SCOPE_P (containing_scope))
17934 scope_die = comp_unit_die ();
17935 else if (TYPE_P (containing_scope))
17937 /* For types, we can just look up the appropriate DIE. But
17938 first we check to see if we're in the middle of emitting it
17939 so we know where the new DIE should go. */
17940 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17941 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17942 break;
17944 if (i < 0)
17946 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17947 || TREE_ASM_WRITTEN (containing_scope));
17948 /*We are not in the middle of emitting the type
17949 CONTAINING_SCOPE. Let's see if it's emitted already. */
17950 scope_die = lookup_type_die (containing_scope);
17952 /* If none of the current dies are suitable, we get file scope. */
17953 if (scope_die == NULL)
17954 scope_die = comp_unit_die ();
17956 else
17957 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17959 else
17960 scope_die = context_die;
17962 return scope_die;
17965 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17967 static inline int
17968 local_scope_p (dw_die_ref context_die)
17970 for (; context_die; context_die = context_die->die_parent)
17971 if (context_die->die_tag == DW_TAG_inlined_subroutine
17972 || context_die->die_tag == DW_TAG_subprogram)
17973 return 1;
17975 return 0;
17978 /* Returns nonzero if CONTEXT_DIE is a class. */
17980 static inline int
17981 class_scope_p (dw_die_ref context_die)
17983 return (context_die
17984 && (context_die->die_tag == DW_TAG_structure_type
17985 || context_die->die_tag == DW_TAG_class_type
17986 || context_die->die_tag == DW_TAG_interface_type
17987 || context_die->die_tag == DW_TAG_union_type));
17990 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17991 whether or not to treat a DIE in this context as a declaration. */
17993 static inline int
17994 class_or_namespace_scope_p (dw_die_ref context_die)
17996 return (class_scope_p (context_die)
17997 || (context_die && context_die->die_tag == DW_TAG_namespace));
18000 /* Many forms of DIEs require a "type description" attribute. This
18001 routine locates the proper "type descriptor" die for the type given
18002 by 'type', and adds a DW_AT_type attribute below the given die. */
18004 static void
18005 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18006 int decl_volatile, dw_die_ref context_die)
18008 enum tree_code code = TREE_CODE (type);
18009 dw_die_ref type_die = NULL;
18011 /* ??? If this type is an unnamed subrange type of an integral, floating-point
18012 or fixed-point type, use the inner type. This is because we have no
18013 support for unnamed types in base_type_die. This can happen if this is
18014 an Ada subrange type. Correct solution is emit a subrange type die. */
18015 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18016 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18017 type = TREE_TYPE (type), code = TREE_CODE (type);
18019 if (code == ERROR_MARK
18020 /* Handle a special case. For functions whose return type is void, we
18021 generate *no* type attribute. (Note that no object may have type
18022 `void', so this only applies to function return types). */
18023 || code == VOID_TYPE)
18024 return;
18026 type_die = modified_type_die (type,
18027 decl_const || TYPE_READONLY (type),
18028 decl_volatile || TYPE_VOLATILE (type),
18029 context_die);
18031 if (type_die != NULL)
18032 add_AT_die_ref (object_die, DW_AT_type, type_die);
18035 /* Given an object die, add the calling convention attribute for the
18036 function call type. */
18037 static void
18038 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18040 enum dwarf_calling_convention value = DW_CC_normal;
18042 value = ((enum dwarf_calling_convention)
18043 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18045 if (is_fortran ()
18046 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18048 /* DWARF 2 doesn't provide a way to identify a program's source-level
18049 entry point. DW_AT_calling_convention attributes are only meant
18050 to describe functions' calling conventions. However, lacking a
18051 better way to signal the Fortran main program, we used this for
18052 a long time, following existing custom. Now, DWARF 4 has
18053 DW_AT_main_subprogram, which we add below, but some tools still
18054 rely on the old way, which we thus keep. */
18055 value = DW_CC_program;
18057 if (dwarf_version >= 4 || !dwarf_strict)
18058 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18061 /* Only add the attribute if the backend requests it, and
18062 is not DW_CC_normal. */
18063 if (value && (value != DW_CC_normal))
18064 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18067 /* Given a tree pointer to a struct, class, union, or enum type node, return
18068 a pointer to the (string) tag name for the given type, or zero if the type
18069 was declared without a tag. */
18071 static const char *
18072 type_tag (const_tree type)
18074 const char *name = 0;
18076 if (TYPE_NAME (type) != 0)
18078 tree t = 0;
18080 /* Find the IDENTIFIER_NODE for the type name. */
18081 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18082 && !TYPE_NAMELESS (type))
18083 t = TYPE_NAME (type);
18085 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18086 a TYPE_DECL node, regardless of whether or not a `typedef' was
18087 involved. */
18088 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18089 && ! DECL_IGNORED_P (TYPE_NAME (type)))
18091 /* We want to be extra verbose. Don't call dwarf_name if
18092 DECL_NAME isn't set. The default hook for decl_printable_name
18093 doesn't like that, and in this context it's correct to return
18094 0, instead of "<anonymous>" or the like. */
18095 if (DECL_NAME (TYPE_NAME (type))
18096 && !DECL_NAMELESS (TYPE_NAME (type)))
18097 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18100 /* Now get the name as a string, or invent one. */
18101 if (!name && t != 0)
18102 name = IDENTIFIER_POINTER (t);
18105 return (name == 0 || *name == '\0') ? 0 : name;
18108 /* Return the type associated with a data member, make a special check
18109 for bit field types. */
18111 static inline tree
18112 member_declared_type (const_tree member)
18114 return (DECL_BIT_FIELD_TYPE (member)
18115 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18118 /* Get the decl's label, as described by its RTL. This may be different
18119 from the DECL_NAME name used in the source file. */
18121 #if 0
18122 static const char *
18123 decl_start_label (tree decl)
18125 rtx x;
18126 const char *fnname;
18128 x = DECL_RTL (decl);
18129 gcc_assert (MEM_P (x));
18131 x = XEXP (x, 0);
18132 gcc_assert (GET_CODE (x) == SYMBOL_REF);
18134 fnname = XSTR (x, 0);
18135 return fnname;
18137 #endif
18139 /* These routines generate the internal representation of the DIE's for
18140 the compilation unit. Debugging information is collected by walking
18141 the declaration trees passed in from dwarf2out_decl(). */
18143 static void
18144 gen_array_type_die (tree type, dw_die_ref context_die)
18146 dw_die_ref scope_die = scope_die_for (type, context_die);
18147 dw_die_ref array_die;
18149 /* GNU compilers represent multidimensional array types as sequences of one
18150 dimensional array types whose element types are themselves array types.
18151 We sometimes squish that down to a single array_type DIE with multiple
18152 subscripts in the Dwarf debugging info. The draft Dwarf specification
18153 say that we are allowed to do this kind of compression in C, because
18154 there is no difference between an array of arrays and a multidimensional
18155 array. We don't do this for Ada to remain as close as possible to the
18156 actual representation, which is especially important against the language
18157 flexibilty wrt arrays of variable size. */
18159 bool collapse_nested_arrays = !is_ada ();
18160 tree element_type;
18162 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18163 DW_TAG_string_type doesn't have DW_AT_type attribute). */
18164 if (TYPE_STRING_FLAG (type)
18165 && TREE_CODE (type) == ARRAY_TYPE
18166 && is_fortran ()
18167 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18169 HOST_WIDE_INT size;
18171 array_die = new_die (DW_TAG_string_type, scope_die, type);
18172 add_name_attribute (array_die, type_tag (type));
18173 equate_type_number_to_die (type, array_die);
18174 size = int_size_in_bytes (type);
18175 if (size >= 0)
18176 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18177 else if (TYPE_DOMAIN (type) != NULL_TREE
18178 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18179 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18181 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18182 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18184 size = int_size_in_bytes (TREE_TYPE (szdecl));
18185 if (loc && size > 0)
18187 add_AT_location_description (array_die, DW_AT_string_length, loc);
18188 if (size != DWARF2_ADDR_SIZE)
18189 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18192 return;
18195 /* ??? The SGI dwarf reader fails for array of array of enum types
18196 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18197 array type comes before the outer array type. We thus call gen_type_die
18198 before we new_die and must prevent nested array types collapsing for this
18199 target. */
18201 #ifdef MIPS_DEBUGGING_INFO
18202 gen_type_die (TREE_TYPE (type), context_die);
18203 collapse_nested_arrays = false;
18204 #endif
18206 array_die = new_die (DW_TAG_array_type, scope_die, type);
18207 add_name_attribute (array_die, type_tag (type));
18208 equate_type_number_to_die (type, array_die);
18210 if (TREE_CODE (type) == VECTOR_TYPE)
18211 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18213 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18214 if (is_fortran ()
18215 && TREE_CODE (type) == ARRAY_TYPE
18216 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18217 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18218 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18220 #if 0
18221 /* We default the array ordering. SDB will probably do
18222 the right things even if DW_AT_ordering is not present. It's not even
18223 an issue until we start to get into multidimensional arrays anyway. If
18224 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18225 then we'll have to put the DW_AT_ordering attribute back in. (But if
18226 and when we find out that we need to put these in, we will only do so
18227 for multidimensional arrays. */
18228 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18229 #endif
18231 #ifdef MIPS_DEBUGGING_INFO
18232 /* The SGI compilers handle arrays of unknown bound by setting
18233 AT_declaration and not emitting any subrange DIEs. */
18234 if (TREE_CODE (type) == ARRAY_TYPE
18235 && ! TYPE_DOMAIN (type))
18236 add_AT_flag (array_die, DW_AT_declaration, 1);
18237 else
18238 #endif
18239 if (TREE_CODE (type) == VECTOR_TYPE)
18241 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
18242 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18243 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18244 add_bound_info (subrange_die, DW_AT_upper_bound,
18245 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18247 else
18248 add_subscript_info (array_die, type, collapse_nested_arrays);
18250 /* Add representation of the type of the elements of this array type and
18251 emit the corresponding DIE if we haven't done it already. */
18252 element_type = TREE_TYPE (type);
18253 if (collapse_nested_arrays)
18254 while (TREE_CODE (element_type) == ARRAY_TYPE)
18256 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18257 break;
18258 element_type = TREE_TYPE (element_type);
18261 #ifndef MIPS_DEBUGGING_INFO
18262 gen_type_die (element_type, context_die);
18263 #endif
18265 add_type_attribute (array_die, element_type, 0, 0, context_die);
18267 if (get_AT (array_die, DW_AT_name))
18268 add_pubtype (type, array_die);
18271 static dw_loc_descr_ref
18272 descr_info_loc (tree val, tree base_decl)
18274 HOST_WIDE_INT size;
18275 dw_loc_descr_ref loc, loc2;
18276 enum dwarf_location_atom op;
18278 if (val == base_decl)
18279 return new_loc_descr (DW_OP_push_object_address, 0, 0);
18281 switch (TREE_CODE (val))
18283 CASE_CONVERT:
18284 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18285 case VAR_DECL:
18286 return loc_descriptor_from_tree (val, 0);
18287 case INTEGER_CST:
18288 if (host_integerp (val, 0))
18289 return int_loc_descriptor (tree_low_cst (val, 0));
18290 break;
18291 case INDIRECT_REF:
18292 size = int_size_in_bytes (TREE_TYPE (val));
18293 if (size < 0)
18294 break;
18295 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18296 if (!loc)
18297 break;
18298 if (size == DWARF2_ADDR_SIZE)
18299 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18300 else
18301 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18302 return loc;
18303 case POINTER_PLUS_EXPR:
18304 case PLUS_EXPR:
18305 if (host_integerp (TREE_OPERAND (val, 1), 1)
18306 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18307 < 16384)
18309 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18310 if (!loc)
18311 break;
18312 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18314 else
18316 op = DW_OP_plus;
18317 do_binop:
18318 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18319 if (!loc)
18320 break;
18321 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18322 if (!loc2)
18323 break;
18324 add_loc_descr (&loc, loc2);
18325 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18327 return loc;
18328 case MINUS_EXPR:
18329 op = DW_OP_minus;
18330 goto do_binop;
18331 case MULT_EXPR:
18332 op = DW_OP_mul;
18333 goto do_binop;
18334 case EQ_EXPR:
18335 op = DW_OP_eq;
18336 goto do_binop;
18337 case NE_EXPR:
18338 op = DW_OP_ne;
18339 goto do_binop;
18340 default:
18341 break;
18343 return NULL;
18346 static void
18347 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18348 tree val, tree base_decl)
18350 dw_loc_descr_ref loc;
18352 if (host_integerp (val, 0))
18354 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18355 return;
18358 loc = descr_info_loc (val, base_decl);
18359 if (!loc)
18360 return;
18362 add_AT_loc (die, attr, loc);
18365 /* This routine generates DIE for array with hidden descriptor, details
18366 are filled into *info by a langhook. */
18368 static void
18369 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18370 dw_die_ref context_die)
18372 dw_die_ref scope_die = scope_die_for (type, context_die);
18373 dw_die_ref array_die;
18374 int dim;
18376 array_die = new_die (DW_TAG_array_type, scope_die, type);
18377 add_name_attribute (array_die, type_tag (type));
18378 equate_type_number_to_die (type, array_die);
18380 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
18381 if (is_fortran ()
18382 && info->ndimensions >= 2)
18383 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18385 if (info->data_location)
18386 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18387 info->base_decl);
18388 if (info->associated)
18389 add_descr_info_field (array_die, DW_AT_associated, info->associated,
18390 info->base_decl);
18391 if (info->allocated)
18392 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18393 info->base_decl);
18395 for (dim = 0; dim < info->ndimensions; dim++)
18397 dw_die_ref subrange_die
18398 = new_die (DW_TAG_subrange_type, array_die, NULL);
18400 if (info->dimen[dim].lower_bound)
18402 /* If it is the default value, omit it. */
18403 int dflt;
18405 if (host_integerp (info->dimen[dim].lower_bound, 0)
18406 && (dflt = lower_bound_default ()) != -1
18407 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18409 else
18410 add_descr_info_field (subrange_die, DW_AT_lower_bound,
18411 info->dimen[dim].lower_bound,
18412 info->base_decl);
18414 if (info->dimen[dim].upper_bound)
18415 add_descr_info_field (subrange_die, DW_AT_upper_bound,
18416 info->dimen[dim].upper_bound,
18417 info->base_decl);
18418 if (info->dimen[dim].stride)
18419 add_descr_info_field (subrange_die, DW_AT_byte_stride,
18420 info->dimen[dim].stride,
18421 info->base_decl);
18424 gen_type_die (info->element_type, context_die);
18425 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18427 if (get_AT (array_die, DW_AT_name))
18428 add_pubtype (type, array_die);
18431 #if 0
18432 static void
18433 gen_entry_point_die (tree decl, dw_die_ref context_die)
18435 tree origin = decl_ultimate_origin (decl);
18436 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18438 if (origin != NULL)
18439 add_abstract_origin_attribute (decl_die, origin);
18440 else
18442 add_name_and_src_coords_attributes (decl_die, decl);
18443 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18444 0, 0, context_die);
18447 if (DECL_ABSTRACT (decl))
18448 equate_decl_number_to_die (decl, decl_die);
18449 else
18450 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18452 #endif
18454 /* Walk through the list of incomplete types again, trying once more to
18455 emit full debugging info for them. */
18457 static void
18458 retry_incomplete_types (void)
18460 int i;
18462 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18463 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18464 DINFO_USAGE_DIR_USE))
18465 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18468 /* Determine what tag to use for a record type. */
18470 static enum dwarf_tag
18471 record_type_tag (tree type)
18473 if (! lang_hooks.types.classify_record)
18474 return DW_TAG_structure_type;
18476 switch (lang_hooks.types.classify_record (type))
18478 case RECORD_IS_STRUCT:
18479 return DW_TAG_structure_type;
18481 case RECORD_IS_CLASS:
18482 return DW_TAG_class_type;
18484 case RECORD_IS_INTERFACE:
18485 if (dwarf_version >= 3 || !dwarf_strict)
18486 return DW_TAG_interface_type;
18487 return DW_TAG_structure_type;
18489 default:
18490 gcc_unreachable ();
18494 /* Generate a DIE to represent an enumeration type. Note that these DIEs
18495 include all of the information about the enumeration values also. Each
18496 enumerated type name/value is listed as a child of the enumerated type
18497 DIE. */
18499 static dw_die_ref
18500 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18502 dw_die_ref type_die = lookup_type_die (type);
18504 if (type_die == NULL)
18506 type_die = new_die (DW_TAG_enumeration_type,
18507 scope_die_for (type, context_die), type);
18508 equate_type_number_to_die (type, type_die);
18509 add_name_attribute (type_die, type_tag (type));
18510 if (dwarf_version >= 4 || !dwarf_strict)
18512 if (ENUM_IS_SCOPED (type))
18513 add_AT_flag (type_die, DW_AT_enum_class, 1);
18514 if (ENUM_IS_OPAQUE (type))
18515 add_AT_flag (type_die, DW_AT_declaration, 1);
18518 else if (! TYPE_SIZE (type))
18519 return type_die;
18520 else
18521 remove_AT (type_die, DW_AT_declaration);
18523 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
18524 given enum type is incomplete, do not generate the DW_AT_byte_size
18525 attribute or the DW_AT_element_list attribute. */
18526 if (TYPE_SIZE (type))
18528 tree link;
18530 TREE_ASM_WRITTEN (type) = 1;
18531 add_byte_size_attribute (type_die, type);
18532 if (TYPE_STUB_DECL (type) != NULL_TREE)
18534 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18535 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18538 /* If the first reference to this type was as the return type of an
18539 inline function, then it may not have a parent. Fix this now. */
18540 if (type_die->die_parent == NULL)
18541 add_child_die (scope_die_for (type, context_die), type_die);
18543 for (link = TYPE_VALUES (type);
18544 link != NULL; link = TREE_CHAIN (link))
18546 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18547 tree value = TREE_VALUE (link);
18549 add_name_attribute (enum_die,
18550 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18552 if (TREE_CODE (value) == CONST_DECL)
18553 value = DECL_INITIAL (value);
18555 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18556 /* DWARF2 does not provide a way of indicating whether or
18557 not enumeration constants are signed or unsigned. GDB
18558 always assumes the values are signed, so we output all
18559 values as if they were signed. That means that
18560 enumeration constants with very large unsigned values
18561 will appear to have negative values in the debugger. */
18562 add_AT_int (enum_die, DW_AT_const_value,
18563 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18566 else
18567 add_AT_flag (type_die, DW_AT_declaration, 1);
18569 if (get_AT (type_die, DW_AT_name))
18570 add_pubtype (type, type_die);
18572 return type_die;
18575 /* Generate a DIE to represent either a real live formal parameter decl or to
18576 represent just the type of some formal parameter position in some function
18577 type.
18579 Note that this routine is a bit unusual because its argument may be a
18580 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18581 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18582 node. If it's the former then this function is being called to output a
18583 DIE to represent a formal parameter object (or some inlining thereof). If
18584 it's the latter, then this function is only being called to output a
18585 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18586 argument type of some subprogram type.
18587 If EMIT_NAME_P is true, name and source coordinate attributes
18588 are emitted. */
18590 static dw_die_ref
18591 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18592 dw_die_ref context_die)
18594 tree node_or_origin = node ? node : origin;
18595 tree ultimate_origin;
18596 dw_die_ref parm_die
18597 = new_die (DW_TAG_formal_parameter, context_die, node);
18599 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18601 case tcc_declaration:
18602 ultimate_origin = decl_ultimate_origin (node_or_origin);
18603 if (node || ultimate_origin)
18604 origin = ultimate_origin;
18605 if (origin != NULL)
18606 add_abstract_origin_attribute (parm_die, origin);
18607 else if (emit_name_p)
18608 add_name_and_src_coords_attributes (parm_die, node);
18609 if (origin == NULL
18610 || (! DECL_ABSTRACT (node_or_origin)
18611 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18612 decl_function_context
18613 (node_or_origin))))
18615 tree type = TREE_TYPE (node_or_origin);
18616 if (decl_by_reference_p (node_or_origin))
18617 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18618 context_die);
18619 else
18620 add_type_attribute (parm_die, type,
18621 TREE_READONLY (node_or_origin),
18622 TREE_THIS_VOLATILE (node_or_origin),
18623 context_die);
18625 if (origin == NULL && DECL_ARTIFICIAL (node))
18626 add_AT_flag (parm_die, DW_AT_artificial, 1);
18628 if (node && node != origin)
18629 equate_decl_number_to_die (node, parm_die);
18630 if (! DECL_ABSTRACT (node_or_origin))
18631 add_location_or_const_value_attribute (parm_die, node_or_origin,
18632 DW_AT_location);
18634 break;
18636 case tcc_type:
18637 /* We were called with some kind of a ..._TYPE node. */
18638 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18639 break;
18641 default:
18642 gcc_unreachable ();
18645 return parm_die;
18648 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18649 children DW_TAG_formal_parameter DIEs representing the arguments of the
18650 parameter pack.
18652 PARM_PACK must be a function parameter pack.
18653 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18654 must point to the subsequent arguments of the function PACK_ARG belongs to.
18655 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18656 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18657 following the last one for which a DIE was generated. */
18659 static dw_die_ref
18660 gen_formal_parameter_pack_die (tree parm_pack,
18661 tree pack_arg,
18662 dw_die_ref subr_die,
18663 tree *next_arg)
18665 tree arg;
18666 dw_die_ref parm_pack_die;
18668 gcc_assert (parm_pack
18669 && lang_hooks.function_parameter_pack_p (parm_pack)
18670 && subr_die);
18672 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18673 add_src_coords_attributes (parm_pack_die, parm_pack);
18675 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18677 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18678 parm_pack))
18679 break;
18680 gen_formal_parameter_die (arg, NULL,
18681 false /* Don't emit name attribute. */,
18682 parm_pack_die);
18684 if (next_arg)
18685 *next_arg = arg;
18686 return parm_pack_die;
18689 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18690 at the end of an (ANSI prototyped) formal parameters list. */
18692 static void
18693 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18695 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18698 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18699 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18700 parameters as specified in some function type specification (except for
18701 those which appear as part of a function *definition*). */
18703 static void
18704 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18706 tree link;
18707 tree formal_type = NULL;
18708 tree first_parm_type;
18709 tree arg;
18711 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18713 arg = DECL_ARGUMENTS (function_or_method_type);
18714 function_or_method_type = TREE_TYPE (function_or_method_type);
18716 else
18717 arg = NULL_TREE;
18719 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18721 /* Make our first pass over the list of formal parameter types and output a
18722 DW_TAG_formal_parameter DIE for each one. */
18723 for (link = first_parm_type; link; )
18725 dw_die_ref parm_die;
18727 formal_type = TREE_VALUE (link);
18728 if (formal_type == void_type_node)
18729 break;
18731 /* Output a (nameless) DIE to represent the formal parameter itself. */
18732 parm_die = gen_formal_parameter_die (formal_type, NULL,
18733 true /* Emit name attribute. */,
18734 context_die);
18735 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18736 && link == first_parm_type)
18738 add_AT_flag (parm_die, DW_AT_artificial, 1);
18739 if (dwarf_version >= 3 || !dwarf_strict)
18740 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18742 else if (arg && DECL_ARTIFICIAL (arg))
18743 add_AT_flag (parm_die, DW_AT_artificial, 1);
18745 link = TREE_CHAIN (link);
18746 if (arg)
18747 arg = DECL_CHAIN (arg);
18750 /* If this function type has an ellipsis, add a
18751 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18752 if (formal_type != void_type_node)
18753 gen_unspecified_parameters_die (function_or_method_type, context_die);
18755 /* Make our second (and final) pass over the list of formal parameter types
18756 and output DIEs to represent those types (as necessary). */
18757 for (link = TYPE_ARG_TYPES (function_or_method_type);
18758 link && TREE_VALUE (link);
18759 link = TREE_CHAIN (link))
18760 gen_type_die (TREE_VALUE (link), context_die);
18763 /* We want to generate the DIE for TYPE so that we can generate the
18764 die for MEMBER, which has been defined; we will need to refer back
18765 to the member declaration nested within TYPE. If we're trying to
18766 generate minimal debug info for TYPE, processing TYPE won't do the
18767 trick; we need to attach the member declaration by hand. */
18769 static void
18770 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18772 gen_type_die (type, context_die);
18774 /* If we're trying to avoid duplicate debug info, we may not have
18775 emitted the member decl for this function. Emit it now. */
18776 if (TYPE_STUB_DECL (type)
18777 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18778 && ! lookup_decl_die (member))
18780 dw_die_ref type_die;
18781 gcc_assert (!decl_ultimate_origin (member));
18783 push_decl_scope (type);
18784 type_die = lookup_type_die_strip_naming_typedef (type);
18785 if (TREE_CODE (member) == FUNCTION_DECL)
18786 gen_subprogram_die (member, type_die);
18787 else if (TREE_CODE (member) == FIELD_DECL)
18789 /* Ignore the nameless fields that are used to skip bits but handle
18790 C++ anonymous unions and structs. */
18791 if (DECL_NAME (member) != NULL_TREE
18792 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18793 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18795 gen_type_die (member_declared_type (member), type_die);
18796 gen_field_die (member, type_die);
18799 else
18800 gen_variable_die (member, NULL_TREE, type_die);
18802 pop_decl_scope ();
18806 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18807 may later generate inlined and/or out-of-line instances of. */
18809 static void
18810 dwarf2out_abstract_function (tree decl)
18812 dw_die_ref old_die;
18813 tree save_fn;
18814 tree context;
18815 int was_abstract;
18816 htab_t old_decl_loc_table;
18817 int old_call_site_count, old_tail_call_site_count;
18818 struct call_arg_loc_node *old_call_arg_locations;
18820 /* Make sure we have the actual abstract inline, not a clone. */
18821 decl = DECL_ORIGIN (decl);
18823 old_die = lookup_decl_die (decl);
18824 if (old_die && get_AT (old_die, DW_AT_inline))
18825 /* We've already generated the abstract instance. */
18826 return;
18828 /* We can be called while recursively when seeing block defining inlined subroutine
18829 DIE. Be sure to not clobber the outer location table nor use it or we would
18830 get locations in abstract instantces. */
18831 old_decl_loc_table = decl_loc_table;
18832 decl_loc_table = NULL;
18833 old_call_arg_locations = call_arg_locations;
18834 call_arg_locations = NULL;
18835 old_call_site_count = call_site_count;
18836 call_site_count = -1;
18837 old_tail_call_site_count = tail_call_site_count;
18838 tail_call_site_count = -1;
18840 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18841 we don't get confused by DECL_ABSTRACT. */
18842 if (debug_info_level > DINFO_LEVEL_TERSE)
18844 context = decl_class_context (decl);
18845 if (context)
18846 gen_type_die_for_member
18847 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18850 /* Pretend we've just finished compiling this function. */
18851 save_fn = current_function_decl;
18852 current_function_decl = decl;
18853 push_cfun (DECL_STRUCT_FUNCTION (decl));
18855 was_abstract = DECL_ABSTRACT (decl);
18856 set_decl_abstract_flags (decl, 1);
18857 dwarf2out_decl (decl);
18858 if (! was_abstract)
18859 set_decl_abstract_flags (decl, 0);
18861 current_function_decl = save_fn;
18862 decl_loc_table = old_decl_loc_table;
18863 call_arg_locations = old_call_arg_locations;
18864 call_site_count = old_call_site_count;
18865 tail_call_site_count = old_tail_call_site_count;
18866 pop_cfun ();
18869 /* Helper function of premark_used_types() which gets called through
18870 htab_traverse.
18872 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18873 marked as unused by prune_unused_types. */
18875 static int
18876 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18878 tree type;
18879 dw_die_ref die;
18881 type = (tree) *slot;
18882 die = lookup_type_die (type);
18883 if (die != NULL)
18884 die->die_perennial_p = 1;
18885 return 1;
18888 /* Helper function of premark_types_used_by_global_vars which gets called
18889 through htab_traverse.
18891 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18892 marked as unused by prune_unused_types. The DIE of the type is marked
18893 only if the global variable using the type will actually be emitted. */
18895 static int
18896 premark_types_used_by_global_vars_helper (void **slot,
18897 void *data ATTRIBUTE_UNUSED)
18899 struct types_used_by_vars_entry *entry;
18900 dw_die_ref die;
18902 entry = (struct types_used_by_vars_entry *) *slot;
18903 gcc_assert (entry->type != NULL
18904 && entry->var_decl != NULL);
18905 die = lookup_type_die (entry->type);
18906 if (die)
18908 /* Ask cgraph if the global variable really is to be emitted.
18909 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18910 struct varpool_node *node = varpool_get_node (entry->var_decl);
18911 if (node && node->needed)
18913 die->die_perennial_p = 1;
18914 /* Keep the parent DIEs as well. */
18915 while ((die = die->die_parent) && die->die_perennial_p == 0)
18916 die->die_perennial_p = 1;
18919 return 1;
18922 /* Mark all members of used_types_hash as perennial. */
18924 static void
18925 premark_used_types (void)
18927 if (cfun && cfun->used_types_hash)
18928 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18931 /* Mark all members of types_used_by_vars_entry as perennial. */
18933 static void
18934 premark_types_used_by_global_vars (void)
18936 if (types_used_by_vars_hash)
18937 htab_traverse (types_used_by_vars_hash,
18938 premark_types_used_by_global_vars_helper, NULL);
18941 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18942 for CA_LOC call arg loc node. */
18944 static dw_die_ref
18945 gen_call_site_die (tree decl, dw_die_ref subr_die,
18946 struct call_arg_loc_node *ca_loc)
18948 dw_die_ref stmt_die = NULL, die;
18949 tree block = ca_loc->block;
18951 while (block
18952 && block != DECL_INITIAL (decl)
18953 && TREE_CODE (block) == BLOCK)
18955 if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
18956 stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
18957 if (stmt_die)
18958 break;
18959 block = BLOCK_SUPERCONTEXT (block);
18961 if (stmt_die == NULL)
18962 stmt_die = subr_die;
18963 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18964 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18965 if (ca_loc->tail_call_p)
18966 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18967 if (ca_loc->symbol_ref)
18969 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18970 if (tdie)
18971 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18972 else
18973 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
18975 return die;
18978 /* Generate a DIE to represent a declared function (either file-scope or
18979 block-local). */
18981 static void
18982 gen_subprogram_die (tree decl, dw_die_ref context_die)
18984 tree origin = decl_ultimate_origin (decl);
18985 dw_die_ref subr_die;
18986 tree outer_scope;
18987 dw_die_ref old_die = lookup_decl_die (decl);
18988 int declaration = (current_function_decl != decl
18989 || class_or_namespace_scope_p (context_die));
18991 premark_used_types ();
18993 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18994 started to generate the abstract instance of an inline, decided to output
18995 its containing class, and proceeded to emit the declaration of the inline
18996 from the member list for the class. If so, DECLARATION takes priority;
18997 we'll get back to the abstract instance when done with the class. */
18999 /* The class-scope declaration DIE must be the primary DIE. */
19000 if (origin && declaration && class_or_namespace_scope_p (context_die))
19002 origin = NULL;
19003 gcc_assert (!old_die);
19006 /* Now that the C++ front end lazily declares artificial member fns, we
19007 might need to retrofit the declaration into its class. */
19008 if (!declaration && !origin && !old_die
19009 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19010 && !class_or_namespace_scope_p (context_die)
19011 && debug_info_level > DINFO_LEVEL_TERSE)
19012 old_die = force_decl_die (decl);
19014 if (origin != NULL)
19016 gcc_assert (!declaration || local_scope_p (context_die));
19018 /* Fixup die_parent for the abstract instance of a nested
19019 inline function. */
19020 if (old_die && old_die->die_parent == NULL)
19021 add_child_die (context_die, old_die);
19023 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19024 add_abstract_origin_attribute (subr_die, origin);
19026 else if (old_die)
19028 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19029 struct dwarf_file_data * file_index = lookup_filename (s.file);
19031 if (!get_AT_flag (old_die, DW_AT_declaration)
19032 /* We can have a normal definition following an inline one in the
19033 case of redefinition of GNU C extern inlines.
19034 It seems reasonable to use AT_specification in this case. */
19035 && !get_AT (old_die, DW_AT_inline))
19037 /* Detect and ignore this case, where we are trying to output
19038 something we have already output. */
19039 return;
19042 /* If the definition comes from the same place as the declaration,
19043 maybe use the old DIE. We always want the DIE for this function
19044 that has the *_pc attributes to be under comp_unit_die so the
19045 debugger can find it. We also need to do this for abstract
19046 instances of inlines, since the spec requires the out-of-line copy
19047 to have the same parent. For local class methods, this doesn't
19048 apply; we just use the old DIE. */
19049 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19050 && (DECL_ARTIFICIAL (decl)
19051 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19052 && (get_AT_unsigned (old_die, DW_AT_decl_line)
19053 == (unsigned) s.line))))
19055 subr_die = old_die;
19057 /* Clear out the declaration attribute and the formal parameters.
19058 Do not remove all children, because it is possible that this
19059 declaration die was forced using force_decl_die(). In such
19060 cases die that forced declaration die (e.g. TAG_imported_module)
19061 is one of the children that we do not want to remove. */
19062 remove_AT (subr_die, DW_AT_declaration);
19063 remove_AT (subr_die, DW_AT_object_pointer);
19064 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19066 else
19068 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19069 add_AT_specification (subr_die, old_die);
19070 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19071 add_AT_file (subr_die, DW_AT_decl_file, file_index);
19072 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19073 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19076 else
19078 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19080 if (TREE_PUBLIC (decl))
19081 add_AT_flag (subr_die, DW_AT_external, 1);
19083 add_name_and_src_coords_attributes (subr_die, decl);
19084 if (debug_info_level > DINFO_LEVEL_TERSE)
19086 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19087 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19088 0, 0, context_die);
19091 add_pure_or_virtual_attribute (subr_die, decl);
19092 if (DECL_ARTIFICIAL (decl))
19093 add_AT_flag (subr_die, DW_AT_artificial, 1);
19095 add_accessibility_attribute (subr_die, decl);
19098 if (declaration)
19100 if (!old_die || !get_AT (old_die, DW_AT_inline))
19102 add_AT_flag (subr_die, DW_AT_declaration, 1);
19104 /* If this is an explicit function declaration then generate
19105 a DW_AT_explicit attribute. */
19106 if (lang_hooks.decls.function_decl_explicit_p (decl)
19107 && (dwarf_version >= 3 || !dwarf_strict))
19108 add_AT_flag (subr_die, DW_AT_explicit, 1);
19110 /* The first time we see a member function, it is in the context of
19111 the class to which it belongs. We make sure of this by emitting
19112 the class first. The next time is the definition, which is
19113 handled above. The two may come from the same source text.
19115 Note that force_decl_die() forces function declaration die. It is
19116 later reused to represent definition. */
19117 equate_decl_number_to_die (decl, subr_die);
19120 else if (DECL_ABSTRACT (decl))
19122 if (DECL_DECLARED_INLINE_P (decl))
19124 if (cgraph_function_possibly_inlined_p (decl))
19125 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19126 else
19127 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19129 else
19131 if (cgraph_function_possibly_inlined_p (decl))
19132 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19133 else
19134 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19137 if (DECL_DECLARED_INLINE_P (decl)
19138 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19139 add_AT_flag (subr_die, DW_AT_artificial, 1);
19141 equate_decl_number_to_die (decl, subr_die);
19143 else if (!DECL_EXTERNAL (decl))
19145 HOST_WIDE_INT cfa_fb_offset;
19147 if (!old_die || !get_AT (old_die, DW_AT_inline))
19148 equate_decl_number_to_die (decl, subr_die);
19150 if (!flag_reorder_blocks_and_partition)
19152 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19153 if (fde->dw_fde_begin)
19155 /* We have already generated the labels. */
19156 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19157 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19159 else
19161 /* Create start/end labels and add the range. */
19162 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19163 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19164 current_function_funcdef_no);
19165 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19166 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19167 current_function_funcdef_no);
19168 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19171 #if VMS_DEBUGGING_INFO
19172 /* HP OpenVMS Industry Standard 64: DWARF Extensions
19173 Section 2.3 Prologue and Epilogue Attributes:
19174 When a breakpoint is set on entry to a function, it is generally
19175 desirable for execution to be suspended, not on the very first
19176 instruction of the function, but rather at a point after the
19177 function's frame has been set up, after any language defined local
19178 declaration processing has been completed, and before execution of
19179 the first statement of the function begins. Debuggers generally
19180 cannot properly determine where this point is. Similarly for a
19181 breakpoint set on exit from a function. The prologue and epilogue
19182 attributes allow a compiler to communicate the location(s) to use. */
19185 if (fde->dw_fde_vms_end_prologue)
19186 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19187 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19189 if (fde->dw_fde_vms_begin_epilogue)
19190 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19191 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19193 #endif
19195 add_pubname (decl, subr_die);
19196 add_arange (decl, subr_die);
19198 else
19199 { /* Generate pubnames entries for the split function code
19200 ranges. */
19201 dw_fde_ref fde = &fde_table[current_funcdef_fde];
19203 if (fde->dw_fde_switched_sections)
19205 if (dwarf_version >= 3 || !dwarf_strict)
19207 /* We should use ranges for non-contiguous code section
19208 addresses. Use the actual code range for the initial
19209 section, since the HOT/COLD labels might precede an
19210 alignment offset. */
19211 bool range_list_added = false;
19212 if (fde->in_std_section)
19214 add_ranges_by_labels (subr_die,
19215 fde->dw_fde_begin,
19216 fde->dw_fde_end,
19217 &range_list_added);
19218 add_ranges_by_labels (subr_die,
19219 fde->dw_fde_unlikely_section_label,
19220 fde->dw_fde_unlikely_section_end_label,
19221 &range_list_added);
19223 else
19225 add_ranges_by_labels (subr_die,
19226 fde->dw_fde_begin,
19227 fde->dw_fde_end,
19228 &range_list_added);
19229 add_ranges_by_labels (subr_die,
19230 fde->dw_fde_hot_section_label,
19231 fde->dw_fde_hot_section_end_label,
19232 &range_list_added);
19234 add_pubname (decl, subr_die);
19235 if (range_list_added)
19236 add_ranges (NULL);
19238 else
19240 /* There is no real support in DW2 for this .. so we make
19241 a work-around. First, emit the pub name for the segment
19242 containing the function label. Then make and emit a
19243 simplified subprogram DIE for the second segment with the
19244 name pre-fixed by __hot/cold_sect_of_. We use the same
19245 linkage name for the second die so that gdb will find both
19246 sections when given "b foo". */
19247 const char *name = NULL;
19248 tree decl_name = DECL_NAME (decl);
19249 dw_die_ref seg_die;
19251 /* Do the 'primary' section. */
19252 add_AT_lbl_id (subr_die, DW_AT_low_pc,
19253 fde->dw_fde_begin);
19254 add_AT_lbl_id (subr_die, DW_AT_high_pc,
19255 fde->dw_fde_end);
19256 /* Add it. */
19257 add_pubname (decl, subr_die);
19258 add_arange (decl, subr_die);
19260 /* Build a minimal DIE for the secondary section. */
19261 seg_die = new_die (DW_TAG_subprogram,
19262 subr_die->die_parent, decl);
19264 if (TREE_PUBLIC (decl))
19265 add_AT_flag (seg_die, DW_AT_external, 1);
19267 if (decl_name != NULL
19268 && IDENTIFIER_POINTER (decl_name) != NULL)
19270 name = dwarf2_name (decl, 1);
19271 if (! DECL_ARTIFICIAL (decl))
19272 add_src_coords_attributes (seg_die, decl);
19274 add_linkage_name (seg_die, decl);
19276 gcc_assert (name!=NULL);
19277 add_pure_or_virtual_attribute (seg_die, decl);
19278 if (DECL_ARTIFICIAL (decl))
19279 add_AT_flag (seg_die, DW_AT_artificial, 1);
19281 if (fde->in_std_section)
19283 name = concat ("__cold_sect_of_", name, NULL);
19284 add_AT_lbl_id (seg_die, DW_AT_low_pc,
19285 fde->dw_fde_unlikely_section_label);
19286 add_AT_lbl_id (seg_die, DW_AT_high_pc,
19287 fde->dw_fde_unlikely_section_end_label);
19289 else
19291 name = concat ("__hot_sect_of_", name, NULL);
19292 add_AT_lbl_id (seg_die, DW_AT_low_pc,
19293 fde->dw_fde_hot_section_label);
19294 add_AT_lbl_id (seg_die, DW_AT_high_pc,
19295 fde->dw_fde_hot_section_end_label);
19297 add_name_attribute (seg_die, name);
19298 add_pubname_string (name, seg_die);
19299 add_arange (decl, seg_die);
19302 else
19304 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19305 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19306 add_pubname (decl, subr_die);
19307 add_arange (decl, subr_die);
19311 #ifdef MIPS_DEBUGGING_INFO
19312 /* Add a reference to the FDE for this routine. */
19313 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19314 #endif
19316 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19318 /* We define the "frame base" as the function's CFA. This is more
19319 convenient for several reasons: (1) It's stable across the prologue
19320 and epilogue, which makes it better than just a frame pointer,
19321 (2) With dwarf3, there exists a one-byte encoding that allows us
19322 to reference the .debug_frame data by proxy, but failing that,
19323 (3) We can at least reuse the code inspection and interpretation
19324 code that determines the CFA position at various points in the
19325 function. */
19326 if (dwarf_version >= 3)
19328 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19329 add_AT_loc (subr_die, DW_AT_frame_base, op);
19331 else
19333 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19334 if (list->dw_loc_next)
19335 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19336 else
19337 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19340 /* Compute a displacement from the "steady-state frame pointer" to
19341 the CFA. The former is what all stack slots and argument slots
19342 will reference in the rtl; the later is what we've told the
19343 debugger about. We'll need to adjust all frame_base references
19344 by this displacement. */
19345 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19347 if (cfun->static_chain_decl)
19348 add_AT_location_description (subr_die, DW_AT_static_link,
19349 loc_list_from_tree (cfun->static_chain_decl, 2));
19352 /* Generate child dies for template paramaters. */
19353 if (debug_info_level > DINFO_LEVEL_TERSE)
19354 gen_generic_params_dies (decl);
19356 /* Now output descriptions of the arguments for this function. This gets
19357 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19358 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19359 `...' at the end of the formal parameter list. In order to find out if
19360 there was a trailing ellipsis or not, we must instead look at the type
19361 associated with the FUNCTION_DECL. This will be a node of type
19362 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19363 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19364 an ellipsis at the end. */
19366 /* In the case where we are describing a mere function declaration, all we
19367 need to do here (and all we *can* do here) is to describe the *types* of
19368 its formal parameters. */
19369 if (debug_info_level <= DINFO_LEVEL_TERSE)
19371 else if (declaration)
19372 gen_formal_types_die (decl, subr_die);
19373 else
19375 /* Generate DIEs to represent all known formal parameters. */
19376 tree parm = DECL_ARGUMENTS (decl);
19377 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19378 tree generic_decl_parm = generic_decl
19379 ? DECL_ARGUMENTS (generic_decl)
19380 : NULL;
19382 /* Now we want to walk the list of parameters of the function and
19383 emit their relevant DIEs.
19385 We consider the case of DECL being an instance of a generic function
19386 as well as it being a normal function.
19388 If DECL is an instance of a generic function we walk the
19389 parameters of the generic function declaration _and_ the parameters of
19390 DECL itself. This is useful because we want to emit specific DIEs for
19391 function parameter packs and those are declared as part of the
19392 generic function declaration. In that particular case,
19393 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19394 That DIE has children DIEs representing the set of arguments
19395 of the pack. Note that the set of pack arguments can be empty.
19396 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19397 children DIE.
19399 Otherwise, we just consider the parameters of DECL. */
19400 while (generic_decl_parm || parm)
19402 if (generic_decl_parm
19403 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19404 gen_formal_parameter_pack_die (generic_decl_parm,
19405 parm, subr_die,
19406 &parm);
19407 else if (parm)
19409 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19411 if (parm == DECL_ARGUMENTS (decl)
19412 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19413 && parm_die
19414 && (dwarf_version >= 3 || !dwarf_strict))
19415 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19417 parm = DECL_CHAIN (parm);
19420 if (generic_decl_parm)
19421 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19424 /* Decide whether we need an unspecified_parameters DIE at the end.
19425 There are 2 more cases to do this for: 1) the ansi ... declaration -
19426 this is detectable when the end of the arg list is not a
19427 void_type_node 2) an unprototyped function declaration (not a
19428 definition). This just means that we have no info about the
19429 parameters at all. */
19430 if (prototype_p (TREE_TYPE (decl)))
19432 /* This is the prototyped case, check for.... */
19433 if (stdarg_p (TREE_TYPE (decl)))
19434 gen_unspecified_parameters_die (decl, subr_die);
19436 else if (DECL_INITIAL (decl) == NULL_TREE)
19437 gen_unspecified_parameters_die (decl, subr_die);
19440 /* Output Dwarf info for all of the stuff within the body of the function
19441 (if it has one - it may be just a declaration). */
19442 outer_scope = DECL_INITIAL (decl);
19444 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19445 a function. This BLOCK actually represents the outermost binding contour
19446 for the function, i.e. the contour in which the function's formal
19447 parameters and labels get declared. Curiously, it appears that the front
19448 end doesn't actually put the PARM_DECL nodes for the current function onto
19449 the BLOCK_VARS list for this outer scope, but are strung off of the
19450 DECL_ARGUMENTS list for the function instead.
19452 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19453 the LABEL_DECL nodes for the function however, and we output DWARF info
19454 for those in decls_for_scope. Just within the `outer_scope' there will be
19455 a BLOCK node representing the function's outermost pair of curly braces,
19456 and any blocks used for the base and member initializers of a C++
19457 constructor function. */
19458 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19460 int call_site_note_count = 0;
19461 int tail_call_site_note_count = 0;
19463 /* Emit a DW_TAG_variable DIE for a named return value. */
19464 if (DECL_NAME (DECL_RESULT (decl)))
19465 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19467 current_function_has_inlines = 0;
19468 decls_for_scope (outer_scope, subr_die, 0);
19470 if (call_arg_locations && !dwarf_strict)
19472 struct call_arg_loc_node *ca_loc;
19473 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19475 dw_die_ref die = NULL;
19476 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19477 rtx arg, next_arg;
19479 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19480 arg; arg = next_arg)
19482 dw_loc_descr_ref reg, val;
19483 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19484 dw_die_ref cdie;
19486 next_arg = XEXP (arg, 1);
19487 if (REG_P (XEXP (XEXP (arg, 0), 0))
19488 && next_arg
19489 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19490 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19491 && REGNO (XEXP (XEXP (arg, 0), 0))
19492 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19493 next_arg = XEXP (next_arg, 1);
19494 if (mode == VOIDmode)
19496 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19497 if (mode == VOIDmode)
19498 mode = GET_MODE (XEXP (arg, 0));
19500 if (GET_MODE_CLASS (mode) != MODE_INT
19501 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19502 continue;
19503 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19505 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19506 tloc = XEXP (XEXP (arg, 0), 1);
19507 continue;
19509 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19510 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19512 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19513 tlocc = XEXP (XEXP (arg, 0), 1);
19514 continue;
19516 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19517 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19518 VAR_INIT_STATUS_INITIALIZED);
19519 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19520 reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19521 0), 0), mode,
19522 VAR_INIT_STATUS_INITIALIZED);
19523 else
19524 continue;
19525 if (reg == NULL)
19526 continue;
19527 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19528 VAR_INIT_STATUS_INITIALIZED);
19529 if (val == NULL)
19530 continue;
19531 if (die == NULL)
19532 die = gen_call_site_die (decl, subr_die, ca_loc);
19533 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19534 NULL_TREE);
19535 add_AT_loc (cdie, DW_AT_location, reg);
19536 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19537 if (next_arg != XEXP (arg, 1))
19539 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19540 0), 1), VOIDmode,
19541 VAR_INIT_STATUS_INITIALIZED);
19542 if (val != NULL)
19543 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19546 if (die == NULL
19547 && (ca_loc->symbol_ref || tloc))
19548 die = gen_call_site_die (decl, subr_die, ca_loc);
19549 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19551 dw_loc_descr_ref tval = NULL;
19553 if (tloc != NULL_RTX)
19554 tval = mem_loc_descriptor (tloc, VOIDmode,
19555 VAR_INIT_STATUS_INITIALIZED);
19556 if (tval)
19557 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19558 else if (tlocc != NULL_RTX)
19560 tval = mem_loc_descriptor (tlocc, VOIDmode,
19561 VAR_INIT_STATUS_INITIALIZED);
19562 if (tval)
19563 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19564 tval);
19567 if (die != NULL)
19569 call_site_note_count++;
19570 if (ca_loc->tail_call_p)
19571 tail_call_site_note_count++;
19575 call_arg_locations = NULL;
19576 call_arg_loc_last = NULL;
19577 if (tail_call_site_count >= 0
19578 && tail_call_site_count == tail_call_site_note_count
19579 && !dwarf_strict)
19581 if (call_site_count >= 0
19582 && call_site_count == call_site_note_count)
19583 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19584 else
19585 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19587 call_site_count = -1;
19588 tail_call_site_count = -1;
19590 /* Add the calling convention attribute if requested. */
19591 add_calling_convention_attribute (subr_die, decl);
19595 /* Returns a hash value for X (which really is a die_struct). */
19597 static hashval_t
19598 common_block_die_table_hash (const void *x)
19600 const_dw_die_ref d = (const_dw_die_ref) x;
19601 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19604 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19605 as decl_id and die_parent of die_struct Y. */
19607 static int
19608 common_block_die_table_eq (const void *x, const void *y)
19610 const_dw_die_ref d = (const_dw_die_ref) x;
19611 const_dw_die_ref e = (const_dw_die_ref) y;
19612 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19615 /* Generate a DIE to represent a declared data object.
19616 Either DECL or ORIGIN must be non-null. */
19618 static void
19619 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19621 HOST_WIDE_INT off;
19622 tree com_decl;
19623 tree decl_or_origin = decl ? decl : origin;
19624 tree ultimate_origin;
19625 dw_die_ref var_die;
19626 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19627 dw_die_ref origin_die;
19628 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19629 || class_or_namespace_scope_p (context_die));
19630 bool specialization_p = false;
19632 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19633 if (decl || ultimate_origin)
19634 origin = ultimate_origin;
19635 com_decl = fortran_common (decl_or_origin, &off);
19637 /* Symbol in common gets emitted as a child of the common block, in the form
19638 of a data member. */
19639 if (com_decl)
19641 dw_die_ref com_die;
19642 dw_loc_list_ref loc;
19643 die_node com_die_arg;
19645 var_die = lookup_decl_die (decl_or_origin);
19646 if (var_die)
19648 if (get_AT (var_die, DW_AT_location) == NULL)
19650 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19651 if (loc)
19653 if (off)
19655 /* Optimize the common case. */
19656 if (single_element_loc_list_p (loc)
19657 && loc->expr->dw_loc_opc == DW_OP_addr
19658 && loc->expr->dw_loc_next == NULL
19659 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19660 == SYMBOL_REF)
19661 loc->expr->dw_loc_oprnd1.v.val_addr
19662 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19663 else
19664 loc_list_plus_const (loc, off);
19666 add_AT_location_description (var_die, DW_AT_location, loc);
19667 remove_AT (var_die, DW_AT_declaration);
19670 return;
19673 if (common_block_die_table == NULL)
19674 common_block_die_table
19675 = htab_create_ggc (10, common_block_die_table_hash,
19676 common_block_die_table_eq, NULL);
19678 com_die_arg.decl_id = DECL_UID (com_decl);
19679 com_die_arg.die_parent = context_die;
19680 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19681 loc = loc_list_from_tree (com_decl, 2);
19682 if (com_die == NULL)
19684 const char *cnam
19685 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19686 void **slot;
19688 com_die = new_die (DW_TAG_common_block, context_die, decl);
19689 add_name_and_src_coords_attributes (com_die, com_decl);
19690 if (loc)
19692 add_AT_location_description (com_die, DW_AT_location, loc);
19693 /* Avoid sharing the same loc descriptor between
19694 DW_TAG_common_block and DW_TAG_variable. */
19695 loc = loc_list_from_tree (com_decl, 2);
19697 else if (DECL_EXTERNAL (decl))
19698 add_AT_flag (com_die, DW_AT_declaration, 1);
19699 add_pubname_string (cnam, com_die); /* ??? needed? */
19700 com_die->decl_id = DECL_UID (com_decl);
19701 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19702 *slot = (void *) com_die;
19704 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19706 add_AT_location_description (com_die, DW_AT_location, loc);
19707 loc = loc_list_from_tree (com_decl, 2);
19708 remove_AT (com_die, DW_AT_declaration);
19710 var_die = new_die (DW_TAG_variable, com_die, decl);
19711 add_name_and_src_coords_attributes (var_die, decl);
19712 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19713 TREE_THIS_VOLATILE (decl), context_die);
19714 add_AT_flag (var_die, DW_AT_external, 1);
19715 if (loc)
19717 if (off)
19719 /* Optimize the common case. */
19720 if (single_element_loc_list_p (loc)
19721 && loc->expr->dw_loc_opc == DW_OP_addr
19722 && loc->expr->dw_loc_next == NULL
19723 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19724 loc->expr->dw_loc_oprnd1.v.val_addr
19725 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19726 else
19727 loc_list_plus_const (loc, off);
19729 add_AT_location_description (var_die, DW_AT_location, loc);
19731 else if (DECL_EXTERNAL (decl))
19732 add_AT_flag (var_die, DW_AT_declaration, 1);
19733 equate_decl_number_to_die (decl, var_die);
19734 return;
19737 /* If the compiler emitted a definition for the DECL declaration
19738 and if we already emitted a DIE for it, don't emit a second
19739 DIE for it again. Allow re-declarations of DECLs that are
19740 inside functions, though. */
19741 if (old_die && declaration && !local_scope_p (context_die))
19742 return;
19744 /* For static data members, the declaration in the class is supposed
19745 to have DW_TAG_member tag; the specification should still be
19746 DW_TAG_variable referencing the DW_TAG_member DIE. */
19747 if (declaration && class_scope_p (context_die))
19748 var_die = new_die (DW_TAG_member, context_die, decl);
19749 else
19750 var_die = new_die (DW_TAG_variable, context_die, decl);
19752 origin_die = NULL;
19753 if (origin != NULL)
19754 origin_die = add_abstract_origin_attribute (var_die, origin);
19756 /* Loop unrolling can create multiple blocks that refer to the same
19757 static variable, so we must test for the DW_AT_declaration flag.
19759 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19760 copy decls and set the DECL_ABSTRACT flag on them instead of
19761 sharing them.
19763 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19765 ??? The declare_in_namespace support causes us to get two DIEs for one
19766 variable, both of which are declarations. We want to avoid considering
19767 one to be a specification, so we must test that this DIE is not a
19768 declaration. */
19769 else if (old_die && TREE_STATIC (decl) && ! declaration
19770 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19772 /* This is a definition of a C++ class level static. */
19773 add_AT_specification (var_die, old_die);
19774 specialization_p = true;
19775 if (DECL_NAME (decl))
19777 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19778 struct dwarf_file_data * file_index = lookup_filename (s.file);
19780 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19781 add_AT_file (var_die, DW_AT_decl_file, file_index);
19783 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19784 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19786 if (old_die->die_tag == DW_TAG_member)
19787 add_linkage_name (var_die, decl);
19790 else
19791 add_name_and_src_coords_attributes (var_die, decl);
19793 if ((origin == NULL && !specialization_p)
19794 || (origin != NULL
19795 && !DECL_ABSTRACT (decl_or_origin)
19796 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19797 decl_function_context
19798 (decl_or_origin))))
19800 tree type = TREE_TYPE (decl_or_origin);
19802 if (decl_by_reference_p (decl_or_origin))
19803 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19804 else
19805 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19806 TREE_THIS_VOLATILE (decl_or_origin), context_die);
19809 if (origin == NULL && !specialization_p)
19811 if (TREE_PUBLIC (decl))
19812 add_AT_flag (var_die, DW_AT_external, 1);
19814 if (DECL_ARTIFICIAL (decl))
19815 add_AT_flag (var_die, DW_AT_artificial, 1);
19817 add_accessibility_attribute (var_die, decl);
19820 if (declaration)
19821 add_AT_flag (var_die, DW_AT_declaration, 1);
19823 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19824 equate_decl_number_to_die (decl, var_die);
19826 if (! declaration
19827 && (! DECL_ABSTRACT (decl_or_origin)
19828 /* Local static vars are shared between all clones/inlines,
19829 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19830 already set. */
19831 || (TREE_CODE (decl_or_origin) == VAR_DECL
19832 && TREE_STATIC (decl_or_origin)
19833 && DECL_RTL_SET_P (decl_or_origin)))
19834 /* When abstract origin already has DW_AT_location attribute, no need
19835 to add it again. */
19836 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19838 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19839 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19840 defer_location (decl_or_origin, var_die);
19841 else
19842 add_location_or_const_value_attribute (var_die,
19843 decl_or_origin,
19844 DW_AT_location);
19845 add_pubname (decl_or_origin, var_die);
19847 else
19848 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19851 /* Generate a DIE to represent a named constant. */
19853 static void
19854 gen_const_die (tree decl, dw_die_ref context_die)
19856 dw_die_ref const_die;
19857 tree type = TREE_TYPE (decl);
19859 const_die = new_die (DW_TAG_constant, context_die, decl);
19860 add_name_and_src_coords_attributes (const_die, decl);
19861 add_type_attribute (const_die, type, 1, 0, context_die);
19862 if (TREE_PUBLIC (decl))
19863 add_AT_flag (const_die, DW_AT_external, 1);
19864 if (DECL_ARTIFICIAL (decl))
19865 add_AT_flag (const_die, DW_AT_artificial, 1);
19866 tree_add_const_value_attribute_for_decl (const_die, decl);
19869 /* Generate a DIE to represent a label identifier. */
19871 static void
19872 gen_label_die (tree decl, dw_die_ref context_die)
19874 tree origin = decl_ultimate_origin (decl);
19875 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19876 rtx insn;
19877 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19879 if (origin != NULL)
19880 add_abstract_origin_attribute (lbl_die, origin);
19881 else
19882 add_name_and_src_coords_attributes (lbl_die, decl);
19884 if (DECL_ABSTRACT (decl))
19885 equate_decl_number_to_die (decl, lbl_die);
19886 else
19888 insn = DECL_RTL_IF_SET (decl);
19890 /* Deleted labels are programmer specified labels which have been
19891 eliminated because of various optimizations. We still emit them
19892 here so that it is possible to put breakpoints on them. */
19893 if (insn
19894 && (LABEL_P (insn)
19895 || ((NOTE_P (insn)
19896 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19898 /* When optimization is enabled (via -O) some parts of the compiler
19899 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19900 represent source-level labels which were explicitly declared by
19901 the user. This really shouldn't be happening though, so catch
19902 it if it ever does happen. */
19903 gcc_assert (!INSN_DELETED_P (insn));
19905 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19906 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19911 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19912 attributes to the DIE for a block STMT, to describe where the inlined
19913 function was called from. This is similar to add_src_coords_attributes. */
19915 static inline void
19916 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19918 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19920 if (dwarf_version >= 3 || !dwarf_strict)
19922 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19923 add_AT_unsigned (die, DW_AT_call_line, s.line);
19928 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19929 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19931 static inline void
19932 add_high_low_attributes (tree stmt, dw_die_ref die)
19934 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19936 if (BLOCK_FRAGMENT_CHAIN (stmt)
19937 && (dwarf_version >= 3 || !dwarf_strict))
19939 tree chain;
19941 if (inlined_function_outer_scope_p (stmt))
19943 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19944 BLOCK_NUMBER (stmt));
19945 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19948 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19950 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19953 add_ranges (chain);
19954 chain = BLOCK_FRAGMENT_CHAIN (chain);
19956 while (chain);
19957 add_ranges (NULL);
19959 else
19961 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19962 BLOCK_NUMBER (stmt));
19963 add_AT_lbl_id (die, DW_AT_low_pc, label);
19964 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19965 BLOCK_NUMBER (stmt));
19966 add_AT_lbl_id (die, DW_AT_high_pc, label);
19970 /* Generate a DIE for a lexical block. */
19972 static void
19973 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19975 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19977 if (call_arg_locations)
19979 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
19980 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
19981 BLOCK_NUMBER (stmt) + 1);
19982 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
19985 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19986 add_high_low_attributes (stmt, stmt_die);
19988 decls_for_scope (stmt, stmt_die, depth);
19991 /* Generate a DIE for an inlined subprogram. */
19993 static void
19994 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19996 tree decl;
19998 /* The instance of function that is effectively being inlined shall not
19999 be abstract. */
20000 gcc_assert (! BLOCK_ABSTRACT (stmt));
20002 decl = block_ultimate_origin (stmt);
20004 /* Emit info for the abstract instance first, if we haven't yet. We
20005 must emit this even if the block is abstract, otherwise when we
20006 emit the block below (or elsewhere), we may end up trying to emit
20007 a die whose origin die hasn't been emitted, and crashing. */
20008 dwarf2out_abstract_function (decl);
20010 if (! BLOCK_ABSTRACT (stmt))
20012 dw_die_ref subr_die
20013 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20015 if (call_arg_locations)
20017 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20018 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20019 BLOCK_NUMBER (stmt) + 1);
20020 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20022 add_abstract_origin_attribute (subr_die, decl);
20023 if (TREE_ASM_WRITTEN (stmt))
20024 add_high_low_attributes (stmt, subr_die);
20025 add_call_src_coords_attributes (stmt, subr_die);
20027 decls_for_scope (stmt, subr_die, depth);
20028 current_function_has_inlines = 1;
20032 /* Generate a DIE for a field in a record, or structure. */
20034 static void
20035 gen_field_die (tree decl, dw_die_ref context_die)
20037 dw_die_ref decl_die;
20039 if (TREE_TYPE (decl) == error_mark_node)
20040 return;
20042 decl_die = new_die (DW_TAG_member, context_die, decl);
20043 add_name_and_src_coords_attributes (decl_die, decl);
20044 add_type_attribute (decl_die, member_declared_type (decl),
20045 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20046 context_die);
20048 if (DECL_BIT_FIELD_TYPE (decl))
20050 add_byte_size_attribute (decl_die, decl);
20051 add_bit_size_attribute (decl_die, decl);
20052 add_bit_offset_attribute (decl_die, decl);
20055 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20056 add_data_member_location_attribute (decl_die, decl);
20058 if (DECL_ARTIFICIAL (decl))
20059 add_AT_flag (decl_die, DW_AT_artificial, 1);
20061 add_accessibility_attribute (decl_die, decl);
20063 /* Equate decl number to die, so that we can look up this decl later on. */
20064 equate_decl_number_to_die (decl, decl_die);
20067 #if 0
20068 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20069 Use modified_type_die instead.
20070 We keep this code here just in case these types of DIEs may be needed to
20071 represent certain things in other languages (e.g. Pascal) someday. */
20073 static void
20074 gen_pointer_type_die (tree type, dw_die_ref context_die)
20076 dw_die_ref ptr_die
20077 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20079 equate_type_number_to_die (type, ptr_die);
20080 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20081 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20084 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20085 Use modified_type_die instead.
20086 We keep this code here just in case these types of DIEs may be needed to
20087 represent certain things in other languages (e.g. Pascal) someday. */
20089 static void
20090 gen_reference_type_die (tree type, dw_die_ref context_die)
20092 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20094 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20095 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20096 else
20097 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20099 equate_type_number_to_die (type, ref_die);
20100 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20101 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20103 #endif
20105 /* Generate a DIE for a pointer to a member type. */
20107 static void
20108 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20110 dw_die_ref ptr_die
20111 = new_die (DW_TAG_ptr_to_member_type,
20112 scope_die_for (type, context_die), type);
20114 equate_type_number_to_die (type, ptr_die);
20115 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20116 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20117 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20120 /* Generate the DIE for the compilation unit. */
20122 static dw_die_ref
20123 gen_compile_unit_die (const char *filename)
20125 dw_die_ref die;
20126 char producer[250];
20127 const char *language_string = lang_hooks.name;
20128 int language;
20130 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20132 if (filename)
20134 add_name_attribute (die, filename);
20135 /* Don't add cwd for <built-in>. */
20136 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20137 add_comp_dir_attribute (die);
20140 sprintf (producer, "%s %s", language_string, version_string);
20142 #ifdef MIPS_DEBUGGING_INFO
20143 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20144 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20145 not appear in the producer string, the debugger reaches the conclusion
20146 that the object file is stripped and has no debugging information.
20147 To get the MIPS/SGI debugger to believe that there is debugging
20148 information in the object file, we add a -g to the producer string. */
20149 if (debug_info_level > DINFO_LEVEL_TERSE)
20150 strcat (producer, " -g");
20151 #endif
20153 add_AT_string (die, DW_AT_producer, producer);
20155 /* If our producer is LTO try to figure out a common language to use
20156 from the global list of translation units. */
20157 if (strcmp (language_string, "GNU GIMPLE") == 0)
20159 unsigned i;
20160 tree t;
20161 const char *common_lang = NULL;
20163 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20165 if (!TRANSLATION_UNIT_LANGUAGE (t))
20166 continue;
20167 if (!common_lang)
20168 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20169 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20171 else if (strncmp (common_lang, "GNU C", 5) == 0
20172 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20173 /* Mixing C and C++ is ok, use C++ in that case. */
20174 common_lang = "GNU C++";
20175 else
20177 /* Fall back to C. */
20178 common_lang = NULL;
20179 break;
20183 if (common_lang)
20184 language_string = common_lang;
20187 language = DW_LANG_C89;
20188 if (strcmp (language_string, "GNU C++") == 0)
20189 language = DW_LANG_C_plus_plus;
20190 else if (strcmp (language_string, "GNU F77") == 0)
20191 language = DW_LANG_Fortran77;
20192 else if (strcmp (language_string, "GNU Pascal") == 0)
20193 language = DW_LANG_Pascal83;
20194 else if (dwarf_version >= 3 || !dwarf_strict)
20196 if (strcmp (language_string, "GNU Ada") == 0)
20197 language = DW_LANG_Ada95;
20198 else if (strcmp (language_string, "GNU Fortran") == 0)
20199 language = DW_LANG_Fortran95;
20200 else if (strcmp (language_string, "GNU Java") == 0)
20201 language = DW_LANG_Java;
20202 else if (strcmp (language_string, "GNU Objective-C") == 0)
20203 language = DW_LANG_ObjC;
20204 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20205 language = DW_LANG_ObjC_plus_plus;
20208 add_AT_unsigned (die, DW_AT_language, language);
20210 switch (language)
20212 case DW_LANG_Fortran77:
20213 case DW_LANG_Fortran90:
20214 case DW_LANG_Fortran95:
20215 /* Fortran has case insensitive identifiers and the front-end
20216 lowercases everything. */
20217 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20218 break;
20219 default:
20220 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20221 break;
20223 return die;
20226 /* Generate the DIE for a base class. */
20228 static void
20229 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20231 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20233 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20234 add_data_member_location_attribute (die, binfo);
20236 if (BINFO_VIRTUAL_P (binfo))
20237 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20239 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20240 children, otherwise the default is DW_ACCESS_public. In DWARF2
20241 the default has always been DW_ACCESS_private. */
20242 if (access == access_public_node)
20244 if (dwarf_version == 2
20245 || context_die->die_tag == DW_TAG_class_type)
20246 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20248 else if (access == access_protected_node)
20249 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20250 else if (dwarf_version > 2
20251 && context_die->die_tag != DW_TAG_class_type)
20252 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20255 /* Generate a DIE for a class member. */
20257 static void
20258 gen_member_die (tree type, dw_die_ref context_die)
20260 tree member;
20261 tree binfo = TYPE_BINFO (type);
20262 dw_die_ref child;
20264 /* If this is not an incomplete type, output descriptions of each of its
20265 members. Note that as we output the DIEs necessary to represent the
20266 members of this record or union type, we will also be trying to output
20267 DIEs to represent the *types* of those members. However the `type'
20268 function (above) will specifically avoid generating type DIEs for member
20269 types *within* the list of member DIEs for this (containing) type except
20270 for those types (of members) which are explicitly marked as also being
20271 members of this (containing) type themselves. The g++ front- end can
20272 force any given type to be treated as a member of some other (containing)
20273 type by setting the TYPE_CONTEXT of the given (member) type to point to
20274 the TREE node representing the appropriate (containing) type. */
20276 /* First output info about the base classes. */
20277 if (binfo)
20279 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20280 int i;
20281 tree base;
20283 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20284 gen_inheritance_die (base,
20285 (accesses ? VEC_index (tree, accesses, i)
20286 : access_public_node), context_die);
20289 /* Now output info about the data members and type members. */
20290 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20292 /* If we thought we were generating minimal debug info for TYPE
20293 and then changed our minds, some of the member declarations
20294 may have already been defined. Don't define them again, but
20295 do put them in the right order. */
20297 child = lookup_decl_die (member);
20298 if (child)
20299 splice_child_die (context_die, child);
20300 else
20301 gen_decl_die (member, NULL, context_die);
20304 /* Now output info about the function members (if any). */
20305 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20307 /* Don't include clones in the member list. */
20308 if (DECL_ABSTRACT_ORIGIN (member))
20309 continue;
20311 child = lookup_decl_die (member);
20312 if (child)
20313 splice_child_die (context_die, child);
20314 else
20315 gen_decl_die (member, NULL, context_die);
20319 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20320 is set, we pretend that the type was never defined, so we only get the
20321 member DIEs needed by later specification DIEs. */
20323 static void
20324 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20325 enum debug_info_usage usage)
20327 dw_die_ref type_die = lookup_type_die (type);
20328 dw_die_ref scope_die = 0;
20329 int nested = 0;
20330 int complete = (TYPE_SIZE (type)
20331 && (! TYPE_STUB_DECL (type)
20332 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20333 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20334 complete = complete && should_emit_struct_debug (type, usage);
20336 if (type_die && ! complete)
20337 return;
20339 if (TYPE_CONTEXT (type) != NULL_TREE
20340 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20341 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20342 nested = 1;
20344 scope_die = scope_die_for (type, context_die);
20346 if (! type_die || (nested && is_cu_die (scope_die)))
20347 /* First occurrence of type or toplevel definition of nested class. */
20349 dw_die_ref old_die = type_die;
20351 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20352 ? record_type_tag (type) : DW_TAG_union_type,
20353 scope_die, type);
20354 equate_type_number_to_die (type, type_die);
20355 if (old_die)
20356 add_AT_specification (type_die, old_die);
20357 else
20358 add_name_attribute (type_die, type_tag (type));
20360 else
20361 remove_AT (type_die, DW_AT_declaration);
20363 /* Generate child dies for template paramaters. */
20364 if (debug_info_level > DINFO_LEVEL_TERSE
20365 && COMPLETE_TYPE_P (type))
20366 schedule_generic_params_dies_gen (type);
20368 /* If this type has been completed, then give it a byte_size attribute and
20369 then give a list of members. */
20370 if (complete && !ns_decl)
20372 /* Prevent infinite recursion in cases where the type of some member of
20373 this type is expressed in terms of this type itself. */
20374 TREE_ASM_WRITTEN (type) = 1;
20375 add_byte_size_attribute (type_die, type);
20376 if (TYPE_STUB_DECL (type) != NULL_TREE)
20378 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20379 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20382 /* If the first reference to this type was as the return type of an
20383 inline function, then it may not have a parent. Fix this now. */
20384 if (type_die->die_parent == NULL)
20385 add_child_die (scope_die, type_die);
20387 push_decl_scope (type);
20388 gen_member_die (type, type_die);
20389 pop_decl_scope ();
20391 /* GNU extension: Record what type our vtable lives in. */
20392 if (TYPE_VFIELD (type))
20394 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20396 gen_type_die (vtype, context_die);
20397 add_AT_die_ref (type_die, DW_AT_containing_type,
20398 lookup_type_die (vtype));
20401 else
20403 add_AT_flag (type_die, DW_AT_declaration, 1);
20405 /* We don't need to do this for function-local types. */
20406 if (TYPE_STUB_DECL (type)
20407 && ! decl_function_context (TYPE_STUB_DECL (type)))
20408 VEC_safe_push (tree, gc, incomplete_types, type);
20411 if (get_AT (type_die, DW_AT_name))
20412 add_pubtype (type, type_die);
20415 /* Generate a DIE for a subroutine _type_. */
20417 static void
20418 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20420 tree return_type = TREE_TYPE (type);
20421 dw_die_ref subr_die
20422 = new_die (DW_TAG_subroutine_type,
20423 scope_die_for (type, context_die), type);
20425 equate_type_number_to_die (type, subr_die);
20426 add_prototyped_attribute (subr_die, type);
20427 add_type_attribute (subr_die, return_type, 0, 0, context_die);
20428 gen_formal_types_die (type, subr_die);
20430 if (get_AT (subr_die, DW_AT_name))
20431 add_pubtype (type, subr_die);
20434 /* Generate a DIE for a type definition. */
20436 static void
20437 gen_typedef_die (tree decl, dw_die_ref context_die)
20439 dw_die_ref type_die;
20440 tree origin;
20442 if (TREE_ASM_WRITTEN (decl))
20443 return;
20445 TREE_ASM_WRITTEN (decl) = 1;
20446 type_die = new_die (DW_TAG_typedef, context_die, decl);
20447 origin = decl_ultimate_origin (decl);
20448 if (origin != NULL)
20449 add_abstract_origin_attribute (type_die, origin);
20450 else
20452 tree type;
20454 add_name_and_src_coords_attributes (type_die, decl);
20455 if (DECL_ORIGINAL_TYPE (decl))
20457 type = DECL_ORIGINAL_TYPE (decl);
20459 gcc_assert (type != TREE_TYPE (decl));
20460 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20462 else
20464 type = TREE_TYPE (decl);
20466 if (is_naming_typedef_decl (TYPE_NAME (type)))
20468 /* Here, we are in the case of decl being a typedef naming
20469 an anonymous type, e.g:
20470 typedef struct {...} foo;
20471 In that case TREE_TYPE (decl) is not a typedef variant
20472 type and TYPE_NAME of the anonymous type is set to the
20473 TYPE_DECL of the typedef. This construct is emitted by
20474 the C++ FE.
20476 TYPE is the anonymous struct named by the typedef
20477 DECL. As we need the DW_AT_type attribute of the
20478 DW_TAG_typedef to point to the DIE of TYPE, let's
20479 generate that DIE right away. add_type_attribute
20480 called below will then pick (via lookup_type_die) that
20481 anonymous struct DIE. */
20482 if (!TREE_ASM_WRITTEN (type))
20483 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20485 /* This is a GNU Extension. We are adding a
20486 DW_AT_linkage_name attribute to the DIE of the
20487 anonymous struct TYPE. The value of that attribute
20488 is the name of the typedef decl naming the anonymous
20489 struct. This greatly eases the work of consumers of
20490 this debug info. */
20491 add_linkage_attr (lookup_type_die (type), decl);
20495 add_type_attribute (type_die, type, TREE_READONLY (decl),
20496 TREE_THIS_VOLATILE (decl), context_die);
20498 if (is_naming_typedef_decl (decl))
20499 /* We want that all subsequent calls to lookup_type_die with
20500 TYPE in argument yield the DW_TAG_typedef we have just
20501 created. */
20502 equate_type_number_to_die (type, type_die);
20504 add_accessibility_attribute (type_die, decl);
20507 if (DECL_ABSTRACT (decl))
20508 equate_decl_number_to_die (decl, type_die);
20510 if (get_AT (type_die, DW_AT_name))
20511 add_pubtype (decl, type_die);
20514 /* Generate a DIE for a struct, class, enum or union type. */
20516 static void
20517 gen_tagged_type_die (tree type,
20518 dw_die_ref context_die,
20519 enum debug_info_usage usage)
20521 int need_pop;
20523 if (type == NULL_TREE
20524 || !is_tagged_type (type))
20525 return;
20527 /* If this is a nested type whose containing class hasn't been written
20528 out yet, writing it out will cover this one, too. This does not apply
20529 to instantiations of member class templates; they need to be added to
20530 the containing class as they are generated. FIXME: This hurts the
20531 idea of combining type decls from multiple TUs, since we can't predict
20532 what set of template instantiations we'll get. */
20533 if (TYPE_CONTEXT (type)
20534 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20535 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20537 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20539 if (TREE_ASM_WRITTEN (type))
20540 return;
20542 /* If that failed, attach ourselves to the stub. */
20543 push_decl_scope (TYPE_CONTEXT (type));
20544 context_die = lookup_type_die (TYPE_CONTEXT (type));
20545 need_pop = 1;
20547 else if (TYPE_CONTEXT (type) != NULL_TREE
20548 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20550 /* If this type is local to a function that hasn't been written
20551 out yet, use a NULL context for now; it will be fixed up in
20552 decls_for_scope. */
20553 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20554 /* A declaration DIE doesn't count; nested types need to go in the
20555 specification. */
20556 if (context_die && is_declaration_die (context_die))
20557 context_die = NULL;
20558 need_pop = 0;
20560 else
20562 context_die = declare_in_namespace (type, context_die);
20563 need_pop = 0;
20566 if (TREE_CODE (type) == ENUMERAL_TYPE)
20568 /* This might have been written out by the call to
20569 declare_in_namespace. */
20570 if (!TREE_ASM_WRITTEN (type))
20571 gen_enumeration_type_die (type, context_die);
20573 else
20574 gen_struct_or_union_type_die (type, context_die, usage);
20576 if (need_pop)
20577 pop_decl_scope ();
20579 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20580 it up if it is ever completed. gen_*_type_die will set it for us
20581 when appropriate. */
20584 /* Generate a type description DIE. */
20586 static void
20587 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20588 enum debug_info_usage usage)
20590 struct array_descr_info info;
20592 if (type == NULL_TREE || type == error_mark_node)
20593 return;
20595 if (TYPE_NAME (type) != NULL_TREE
20596 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20597 && is_redundant_typedef (TYPE_NAME (type))
20598 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20599 /* The DECL of this type is a typedef we don't want to emit debug
20600 info for but we want debug info for its underlying typedef.
20601 This can happen for e.g, the injected-class-name of a C++
20602 type. */
20603 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20605 /* If TYPE is a typedef type variant, let's generate debug info
20606 for the parent typedef which TYPE is a type of. */
20607 if (typedef_variant_p (type))
20609 if (TREE_ASM_WRITTEN (type))
20610 return;
20612 /* Prevent broken recursion; we can't hand off to the same type. */
20613 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20615 /* Use the DIE of the containing namespace as the parent DIE of
20616 the type description DIE we want to generate. */
20617 if (DECL_CONTEXT (TYPE_NAME (type))
20618 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20619 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20621 TREE_ASM_WRITTEN (type) = 1;
20623 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20624 return;
20627 /* If type is an anonymous tagged type named by a typedef, let's
20628 generate debug info for the typedef. */
20629 if (is_naming_typedef_decl (TYPE_NAME (type)))
20631 /* Use the DIE of the containing namespace as the parent DIE of
20632 the type description DIE we want to generate. */
20633 if (DECL_CONTEXT (TYPE_NAME (type))
20634 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20635 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20637 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20638 return;
20641 /* If this is an array type with hidden descriptor, handle it first. */
20642 if (!TREE_ASM_WRITTEN (type)
20643 && lang_hooks.types.get_array_descr_info
20644 && lang_hooks.types.get_array_descr_info (type, &info)
20645 && (dwarf_version >= 3 || !dwarf_strict))
20647 gen_descr_array_type_die (type, &info, context_die);
20648 TREE_ASM_WRITTEN (type) = 1;
20649 return;
20652 /* We are going to output a DIE to represent the unqualified version
20653 of this type (i.e. without any const or volatile qualifiers) so
20654 get the main variant (i.e. the unqualified version) of this type
20655 now. (Vectors are special because the debugging info is in the
20656 cloned type itself). */
20657 if (TREE_CODE (type) != VECTOR_TYPE)
20658 type = type_main_variant (type);
20660 if (TREE_ASM_WRITTEN (type))
20661 return;
20663 switch (TREE_CODE (type))
20665 case ERROR_MARK:
20666 break;
20668 case POINTER_TYPE:
20669 case REFERENCE_TYPE:
20670 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20671 ensures that the gen_type_die recursion will terminate even if the
20672 type is recursive. Recursive types are possible in Ada. */
20673 /* ??? We could perhaps do this for all types before the switch
20674 statement. */
20675 TREE_ASM_WRITTEN (type) = 1;
20677 /* For these types, all that is required is that we output a DIE (or a
20678 set of DIEs) to represent the "basis" type. */
20679 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20680 DINFO_USAGE_IND_USE);
20681 break;
20683 case OFFSET_TYPE:
20684 /* This code is used for C++ pointer-to-data-member types.
20685 Output a description of the relevant class type. */
20686 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20687 DINFO_USAGE_IND_USE);
20689 /* Output a description of the type of the object pointed to. */
20690 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20691 DINFO_USAGE_IND_USE);
20693 /* Now output a DIE to represent this pointer-to-data-member type
20694 itself. */
20695 gen_ptr_to_mbr_type_die (type, context_die);
20696 break;
20698 case FUNCTION_TYPE:
20699 /* Force out return type (in case it wasn't forced out already). */
20700 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20701 DINFO_USAGE_DIR_USE);
20702 gen_subroutine_type_die (type, context_die);
20703 break;
20705 case METHOD_TYPE:
20706 /* Force out return type (in case it wasn't forced out already). */
20707 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20708 DINFO_USAGE_DIR_USE);
20709 gen_subroutine_type_die (type, context_die);
20710 break;
20712 case ARRAY_TYPE:
20713 gen_array_type_die (type, context_die);
20714 break;
20716 case VECTOR_TYPE:
20717 gen_array_type_die (type, context_die);
20718 break;
20720 case ENUMERAL_TYPE:
20721 case RECORD_TYPE:
20722 case UNION_TYPE:
20723 case QUAL_UNION_TYPE:
20724 gen_tagged_type_die (type, context_die, usage);
20725 return;
20727 case VOID_TYPE:
20728 case INTEGER_TYPE:
20729 case REAL_TYPE:
20730 case FIXED_POINT_TYPE:
20731 case COMPLEX_TYPE:
20732 case BOOLEAN_TYPE:
20733 /* No DIEs needed for fundamental types. */
20734 break;
20736 case NULLPTR_TYPE:
20737 case LANG_TYPE:
20738 /* Just use DW_TAG_unspecified_type. */
20740 dw_die_ref type_die = lookup_type_die (type);
20741 if (type_die == NULL)
20743 tree name = TYPE_NAME (type);
20744 if (TREE_CODE (name) == TYPE_DECL)
20745 name = DECL_NAME (name);
20746 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20747 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20748 equate_type_number_to_die (type, type_die);
20751 break;
20753 default:
20754 gcc_unreachable ();
20757 TREE_ASM_WRITTEN (type) = 1;
20760 static void
20761 gen_type_die (tree type, dw_die_ref context_die)
20763 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20766 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20767 things which are local to the given block. */
20769 static void
20770 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20772 int must_output_die = 0;
20773 bool inlined_func;
20775 /* Ignore blocks that are NULL. */
20776 if (stmt == NULL_TREE)
20777 return;
20779 inlined_func = inlined_function_outer_scope_p (stmt);
20781 /* If the block is one fragment of a non-contiguous block, do not
20782 process the variables, since they will have been done by the
20783 origin block. Do process subblocks. */
20784 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20786 tree sub;
20788 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20789 gen_block_die (sub, context_die, depth + 1);
20791 return;
20794 /* Determine if we need to output any Dwarf DIEs at all to represent this
20795 block. */
20796 if (inlined_func)
20797 /* The outer scopes for inlinings *must* always be represented. We
20798 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20799 must_output_die = 1;
20800 else
20802 /* Determine if this block directly contains any "significant"
20803 local declarations which we will need to output DIEs for. */
20804 if (debug_info_level > DINFO_LEVEL_TERSE)
20805 /* We are not in terse mode so *any* local declaration counts
20806 as being a "significant" one. */
20807 must_output_die = ((BLOCK_VARS (stmt) != NULL
20808 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20809 && (TREE_USED (stmt)
20810 || TREE_ASM_WRITTEN (stmt)
20811 || BLOCK_ABSTRACT (stmt)));
20812 else if ((TREE_USED (stmt)
20813 || TREE_ASM_WRITTEN (stmt)
20814 || BLOCK_ABSTRACT (stmt))
20815 && !dwarf2out_ignore_block (stmt))
20816 must_output_die = 1;
20819 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20820 DIE for any block which contains no significant local declarations at
20821 all. Rather, in such cases we just call `decls_for_scope' so that any
20822 needed Dwarf info for any sub-blocks will get properly generated. Note
20823 that in terse mode, our definition of what constitutes a "significant"
20824 local declaration gets restricted to include only inlined function
20825 instances and local (nested) function definitions. */
20826 if (must_output_die)
20828 if (inlined_func)
20830 /* If STMT block is abstract, that means we have been called
20831 indirectly from dwarf2out_abstract_function.
20832 That function rightfully marks the descendent blocks (of
20833 the abstract function it is dealing with) as being abstract,
20834 precisely to prevent us from emitting any
20835 DW_TAG_inlined_subroutine DIE as a descendent
20836 of an abstract function instance. So in that case, we should
20837 not call gen_inlined_subroutine_die.
20839 Later though, when cgraph asks dwarf2out to emit info
20840 for the concrete instance of the function decl into which
20841 the concrete instance of STMT got inlined, the later will lead
20842 to the generation of a DW_TAG_inlined_subroutine DIE. */
20843 if (! BLOCK_ABSTRACT (stmt))
20844 gen_inlined_subroutine_die (stmt, context_die, depth);
20846 else
20847 gen_lexical_block_die (stmt, context_die, depth);
20849 else
20850 decls_for_scope (stmt, context_die, depth);
20853 /* Process variable DECL (or variable with origin ORIGIN) within
20854 block STMT and add it to CONTEXT_DIE. */
20855 static void
20856 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20858 dw_die_ref die;
20859 tree decl_or_origin = decl ? decl : origin;
20861 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20862 die = lookup_decl_die (decl_or_origin);
20863 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20864 && TYPE_DECL_IS_STUB (decl_or_origin))
20865 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20866 else
20867 die = NULL;
20869 if (die != NULL && die->die_parent == NULL)
20870 add_child_die (context_die, die);
20871 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20872 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20873 stmt, context_die);
20874 else
20875 gen_decl_die (decl, origin, context_die);
20878 /* Generate all of the decls declared within a given scope and (recursively)
20879 all of its sub-blocks. */
20881 static void
20882 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20884 tree decl;
20885 unsigned int i;
20886 tree subblocks;
20888 /* Ignore NULL blocks. */
20889 if (stmt == NULL_TREE)
20890 return;
20892 /* Output the DIEs to represent all of the data objects and typedefs
20893 declared directly within this block but not within any nested
20894 sub-blocks. Also, nested function and tag DIEs have been
20895 generated with a parent of NULL; fix that up now. */
20896 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20897 process_scope_var (stmt, decl, NULL_TREE, context_die);
20898 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20899 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20900 context_die);
20902 /* If we're at -g1, we're not interested in subblocks. */
20903 if (debug_info_level <= DINFO_LEVEL_TERSE)
20904 return;
20906 /* Output the DIEs to represent all sub-blocks (and the items declared
20907 therein) of this block. */
20908 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20909 subblocks != NULL;
20910 subblocks = BLOCK_CHAIN (subblocks))
20911 gen_block_die (subblocks, context_die, depth + 1);
20914 /* Is this a typedef we can avoid emitting? */
20916 static inline int
20917 is_redundant_typedef (const_tree decl)
20919 if (TYPE_DECL_IS_STUB (decl))
20920 return 1;
20922 if (DECL_ARTIFICIAL (decl)
20923 && DECL_CONTEXT (decl)
20924 && is_tagged_type (DECL_CONTEXT (decl))
20925 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20926 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20927 /* Also ignore the artificial member typedef for the class name. */
20928 return 1;
20930 return 0;
20933 /* Return TRUE if TYPE is a typedef that names a type for linkage
20934 purposes. This kind of typedefs is produced by the C++ FE for
20935 constructs like:
20937 typedef struct {...} foo;
20939 In that case, there is no typedef variant type produced for foo.
20940 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20941 struct type. */
20943 static bool
20944 is_naming_typedef_decl (const_tree decl)
20946 if (decl == NULL_TREE
20947 || TREE_CODE (decl) != TYPE_DECL
20948 || !is_tagged_type (TREE_TYPE (decl))
20949 || DECL_IS_BUILTIN (decl)
20950 || is_redundant_typedef (decl)
20951 /* It looks like Ada produces TYPE_DECLs that are very similar
20952 to C++ naming typedefs but that have different
20953 semantics. Let's be specific to c++ for now. */
20954 || !is_cxx ())
20955 return FALSE;
20957 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20958 && TYPE_NAME (TREE_TYPE (decl)) == decl
20959 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20960 != TYPE_NAME (TREE_TYPE (decl))));
20963 /* Returns the DIE for a context. */
20965 static inline dw_die_ref
20966 get_context_die (tree context)
20968 if (context)
20970 /* Find die that represents this context. */
20971 if (TYPE_P (context))
20973 context = TYPE_MAIN_VARIANT (context);
20974 return strip_naming_typedef (context, force_type_die (context));
20976 else
20977 return force_decl_die (context);
20979 return comp_unit_die ();
20982 /* Returns the DIE for decl. A DIE will always be returned. */
20984 static dw_die_ref
20985 force_decl_die (tree decl)
20987 dw_die_ref decl_die;
20988 unsigned saved_external_flag;
20989 tree save_fn = NULL_TREE;
20990 decl_die = lookup_decl_die (decl);
20991 if (!decl_die)
20993 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20995 decl_die = lookup_decl_die (decl);
20996 if (decl_die)
20997 return decl_die;
20999 switch (TREE_CODE (decl))
21001 case FUNCTION_DECL:
21002 /* Clear current_function_decl, so that gen_subprogram_die thinks
21003 that this is a declaration. At this point, we just want to force
21004 declaration die. */
21005 save_fn = current_function_decl;
21006 current_function_decl = NULL_TREE;
21007 gen_subprogram_die (decl, context_die);
21008 current_function_decl = save_fn;
21009 break;
21011 case VAR_DECL:
21012 /* Set external flag to force declaration die. Restore it after
21013 gen_decl_die() call. */
21014 saved_external_flag = DECL_EXTERNAL (decl);
21015 DECL_EXTERNAL (decl) = 1;
21016 gen_decl_die (decl, NULL, context_die);
21017 DECL_EXTERNAL (decl) = saved_external_flag;
21018 break;
21020 case NAMESPACE_DECL:
21021 if (dwarf_version >= 3 || !dwarf_strict)
21022 dwarf2out_decl (decl);
21023 else
21024 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21025 decl_die = comp_unit_die ();
21026 break;
21028 case TRANSLATION_UNIT_DECL:
21029 decl_die = comp_unit_die ();
21030 break;
21032 default:
21033 gcc_unreachable ();
21036 /* We should be able to find the DIE now. */
21037 if (!decl_die)
21038 decl_die = lookup_decl_die (decl);
21039 gcc_assert (decl_die);
21042 return decl_die;
21045 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21046 always returned. */
21048 static dw_die_ref
21049 force_type_die (tree type)
21051 dw_die_ref type_die;
21053 type_die = lookup_type_die (type);
21054 if (!type_die)
21056 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21058 type_die = modified_type_die (type, TYPE_READONLY (type),
21059 TYPE_VOLATILE (type), context_die);
21060 gcc_assert (type_die);
21062 return type_die;
21065 /* Force out any required namespaces to be able to output DECL,
21066 and return the new context_die for it, if it's changed. */
21068 static dw_die_ref
21069 setup_namespace_context (tree thing, dw_die_ref context_die)
21071 tree context = (DECL_P (thing)
21072 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21073 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21074 /* Force out the namespace. */
21075 context_die = force_decl_die (context);
21077 return context_die;
21080 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21081 type) within its namespace, if appropriate.
21083 For compatibility with older debuggers, namespace DIEs only contain
21084 declarations; all definitions are emitted at CU scope. */
21086 static dw_die_ref
21087 declare_in_namespace (tree thing, dw_die_ref context_die)
21089 dw_die_ref ns_context;
21091 if (debug_info_level <= DINFO_LEVEL_TERSE)
21092 return context_die;
21094 /* If this decl is from an inlined function, then don't try to emit it in its
21095 namespace, as we will get confused. It would have already been emitted
21096 when the abstract instance of the inline function was emitted anyways. */
21097 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21098 return context_die;
21100 ns_context = setup_namespace_context (thing, context_die);
21102 if (ns_context != context_die)
21104 if (is_fortran ())
21105 return ns_context;
21106 if (DECL_P (thing))
21107 gen_decl_die (thing, NULL, ns_context);
21108 else
21109 gen_type_die (thing, ns_context);
21111 return context_die;
21114 /* Generate a DIE for a namespace or namespace alias. */
21116 static void
21117 gen_namespace_die (tree decl, dw_die_ref context_die)
21119 dw_die_ref namespace_die;
21121 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21122 they are an alias of. */
21123 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21125 /* Output a real namespace or module. */
21126 context_die = setup_namespace_context (decl, comp_unit_die ());
21127 namespace_die = new_die (is_fortran ()
21128 ? DW_TAG_module : DW_TAG_namespace,
21129 context_die, decl);
21130 /* For Fortran modules defined in different CU don't add src coords. */
21131 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21133 const char *name = dwarf2_name (decl, 0);
21134 if (name)
21135 add_name_attribute (namespace_die, name);
21137 else
21138 add_name_and_src_coords_attributes (namespace_die, decl);
21139 if (DECL_EXTERNAL (decl))
21140 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21141 equate_decl_number_to_die (decl, namespace_die);
21143 else
21145 /* Output a namespace alias. */
21147 /* Force out the namespace we are an alias of, if necessary. */
21148 dw_die_ref origin_die
21149 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21151 if (DECL_FILE_SCOPE_P (decl)
21152 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21153 context_die = setup_namespace_context (decl, comp_unit_die ());
21154 /* Now create the namespace alias DIE. */
21155 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21156 add_name_and_src_coords_attributes (namespace_die, decl);
21157 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21158 equate_decl_number_to_die (decl, namespace_die);
21162 /* Generate Dwarf debug information for a decl described by DECL.
21163 The return value is currently only meaningful for PARM_DECLs,
21164 for all other decls it returns NULL. */
21166 static dw_die_ref
21167 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21169 tree decl_or_origin = decl ? decl : origin;
21170 tree class_origin = NULL, ultimate_origin;
21172 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21173 return NULL;
21175 switch (TREE_CODE (decl_or_origin))
21177 case ERROR_MARK:
21178 break;
21180 case CONST_DECL:
21181 if (!is_fortran () && !is_ada ())
21183 /* The individual enumerators of an enum type get output when we output
21184 the Dwarf representation of the relevant enum type itself. */
21185 break;
21188 /* Emit its type. */
21189 gen_type_die (TREE_TYPE (decl), context_die);
21191 /* And its containing namespace. */
21192 context_die = declare_in_namespace (decl, context_die);
21194 gen_const_die (decl, context_die);
21195 break;
21197 case FUNCTION_DECL:
21198 /* Don't output any DIEs to represent mere function declarations,
21199 unless they are class members or explicit block externs. */
21200 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21201 && DECL_FILE_SCOPE_P (decl_or_origin)
21202 && (current_function_decl == NULL_TREE
21203 || DECL_ARTIFICIAL (decl_or_origin)))
21204 break;
21206 #if 0
21207 /* FIXME */
21208 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21209 on local redeclarations of global functions. That seems broken. */
21210 if (current_function_decl != decl)
21211 /* This is only a declaration. */;
21212 #endif
21214 /* If we're emitting a clone, emit info for the abstract instance. */
21215 if (origin || DECL_ORIGIN (decl) != decl)
21216 dwarf2out_abstract_function (origin
21217 ? DECL_ORIGIN (origin)
21218 : DECL_ABSTRACT_ORIGIN (decl));
21220 /* If we're emitting an out-of-line copy of an inline function,
21221 emit info for the abstract instance and set up to refer to it. */
21222 else if (cgraph_function_possibly_inlined_p (decl)
21223 && ! DECL_ABSTRACT (decl)
21224 && ! class_or_namespace_scope_p (context_die)
21225 /* dwarf2out_abstract_function won't emit a die if this is just
21226 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21227 that case, because that works only if we have a die. */
21228 && DECL_INITIAL (decl) != NULL_TREE)
21230 dwarf2out_abstract_function (decl);
21231 set_decl_origin_self (decl);
21234 /* Otherwise we're emitting the primary DIE for this decl. */
21235 else if (debug_info_level > DINFO_LEVEL_TERSE)
21237 /* Before we describe the FUNCTION_DECL itself, make sure that we
21238 have its containing type. */
21239 if (!origin)
21240 origin = decl_class_context (decl);
21241 if (origin != NULL_TREE)
21242 gen_type_die (origin, context_die);
21244 /* And its return type. */
21245 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21247 /* And its virtual context. */
21248 if (DECL_VINDEX (decl) != NULL_TREE)
21249 gen_type_die (DECL_CONTEXT (decl), context_die);
21251 /* Make sure we have a member DIE for decl. */
21252 if (origin != NULL_TREE)
21253 gen_type_die_for_member (origin, decl, context_die);
21255 /* And its containing namespace. */
21256 context_die = declare_in_namespace (decl, context_die);
21259 /* Now output a DIE to represent the function itself. */
21260 if (decl)
21261 gen_subprogram_die (decl, context_die);
21262 break;
21264 case TYPE_DECL:
21265 /* If we are in terse mode, don't generate any DIEs to represent any
21266 actual typedefs. */
21267 if (debug_info_level <= DINFO_LEVEL_TERSE)
21268 break;
21270 /* In the special case of a TYPE_DECL node representing the declaration
21271 of some type tag, if the given TYPE_DECL is marked as having been
21272 instantiated from some other (original) TYPE_DECL node (e.g. one which
21273 was generated within the original definition of an inline function) we
21274 used to generate a special (abbreviated) DW_TAG_structure_type,
21275 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21276 should be actually referencing those DIEs, as variable DIEs with that
21277 type would be emitted already in the abstract origin, so it was always
21278 removed during unused type prunning. Don't add anything in this
21279 case. */
21280 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21281 break;
21283 if (is_redundant_typedef (decl))
21284 gen_type_die (TREE_TYPE (decl), context_die);
21285 else
21286 /* Output a DIE to represent the typedef itself. */
21287 gen_typedef_die (decl, context_die);
21288 break;
21290 case LABEL_DECL:
21291 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21292 gen_label_die (decl, context_die);
21293 break;
21295 case VAR_DECL:
21296 case RESULT_DECL:
21297 /* If we are in terse mode, don't generate any DIEs to represent any
21298 variable declarations or definitions. */
21299 if (debug_info_level <= DINFO_LEVEL_TERSE)
21300 break;
21302 /* Output any DIEs that are needed to specify the type of this data
21303 object. */
21304 if (decl_by_reference_p (decl_or_origin))
21305 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21306 else
21307 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21309 /* And its containing type. */
21310 class_origin = decl_class_context (decl_or_origin);
21311 if (class_origin != NULL_TREE)
21312 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21314 /* And its containing namespace. */
21315 context_die = declare_in_namespace (decl_or_origin, context_die);
21317 /* Now output the DIE to represent the data object itself. This gets
21318 complicated because of the possibility that the VAR_DECL really
21319 represents an inlined instance of a formal parameter for an inline
21320 function. */
21321 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21322 if (ultimate_origin != NULL_TREE
21323 && TREE_CODE (ultimate_origin) == PARM_DECL)
21324 gen_formal_parameter_die (decl, origin,
21325 true /* Emit name attribute. */,
21326 context_die);
21327 else
21328 gen_variable_die (decl, origin, context_die);
21329 break;
21331 case FIELD_DECL:
21332 /* Ignore the nameless fields that are used to skip bits but handle C++
21333 anonymous unions and structs. */
21334 if (DECL_NAME (decl) != NULL_TREE
21335 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21336 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21338 gen_type_die (member_declared_type (decl), context_die);
21339 gen_field_die (decl, context_die);
21341 break;
21343 case PARM_DECL:
21344 if (DECL_BY_REFERENCE (decl_or_origin))
21345 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21346 else
21347 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21348 return gen_formal_parameter_die (decl, origin,
21349 true /* Emit name attribute. */,
21350 context_die);
21352 case NAMESPACE_DECL:
21353 case IMPORTED_DECL:
21354 if (dwarf_version >= 3 || !dwarf_strict)
21355 gen_namespace_die (decl, context_die);
21356 break;
21358 default:
21359 /* Probably some frontend-internal decl. Assume we don't care. */
21360 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21361 break;
21364 return NULL;
21367 /* Output debug information for global decl DECL. Called from toplev.c after
21368 compilation proper has finished. */
21370 static void
21371 dwarf2out_global_decl (tree decl)
21373 /* Output DWARF2 information for file-scope tentative data object
21374 declarations, file-scope (extern) function declarations (which
21375 had no corresponding body) and file-scope tagged type declarations
21376 and definitions which have not yet been forced out. */
21377 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21378 dwarf2out_decl (decl);
21381 /* Output debug information for type decl DECL. Called from toplev.c
21382 and from language front ends (to record built-in types). */
21383 static void
21384 dwarf2out_type_decl (tree decl, int local)
21386 if (!local)
21387 dwarf2out_decl (decl);
21390 /* Output debug information for imported module or decl DECL.
21391 NAME is non-NULL name in the lexical block if the decl has been renamed.
21392 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21393 that DECL belongs to.
21394 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21395 static void
21396 dwarf2out_imported_module_or_decl_1 (tree decl,
21397 tree name,
21398 tree lexical_block,
21399 dw_die_ref lexical_block_die)
21401 expanded_location xloc;
21402 dw_die_ref imported_die = NULL;
21403 dw_die_ref at_import_die;
21405 if (TREE_CODE (decl) == IMPORTED_DECL)
21407 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21408 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21409 gcc_assert (decl);
21411 else
21412 xloc = expand_location (input_location);
21414 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21416 at_import_die = force_type_die (TREE_TYPE (decl));
21417 /* For namespace N { typedef void T; } using N::T; base_type_die
21418 returns NULL, but DW_TAG_imported_declaration requires
21419 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21420 if (!at_import_die)
21422 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21423 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21424 at_import_die = lookup_type_die (TREE_TYPE (decl));
21425 gcc_assert (at_import_die);
21428 else
21430 at_import_die = lookup_decl_die (decl);
21431 if (!at_import_die)
21433 /* If we're trying to avoid duplicate debug info, we may not have
21434 emitted the member decl for this field. Emit it now. */
21435 if (TREE_CODE (decl) == FIELD_DECL)
21437 tree type = DECL_CONTEXT (decl);
21439 if (TYPE_CONTEXT (type)
21440 && TYPE_P (TYPE_CONTEXT (type))
21441 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21442 DINFO_USAGE_DIR_USE))
21443 return;
21444 gen_type_die_for_member (type, decl,
21445 get_context_die (TYPE_CONTEXT (type)));
21447 at_import_die = force_decl_die (decl);
21451 if (TREE_CODE (decl) == NAMESPACE_DECL)
21453 if (dwarf_version >= 3 || !dwarf_strict)
21454 imported_die = new_die (DW_TAG_imported_module,
21455 lexical_block_die,
21456 lexical_block);
21457 else
21458 return;
21460 else
21461 imported_die = new_die (DW_TAG_imported_declaration,
21462 lexical_block_die,
21463 lexical_block);
21465 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21466 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21467 if (name)
21468 add_AT_string (imported_die, DW_AT_name,
21469 IDENTIFIER_POINTER (name));
21470 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21473 /* Output debug information for imported module or decl DECL.
21474 NAME is non-NULL name in context if the decl has been renamed.
21475 CHILD is true if decl is one of the renamed decls as part of
21476 importing whole module. */
21478 static void
21479 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21480 bool child)
21482 /* dw_die_ref at_import_die; */
21483 dw_die_ref scope_die;
21485 if (debug_info_level <= DINFO_LEVEL_TERSE)
21486 return;
21488 gcc_assert (decl);
21490 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21491 We need decl DIE for reference and scope die. First, get DIE for the decl
21492 itself. */
21494 /* Get the scope die for decl context. Use comp_unit_die for global module
21495 or decl. If die is not found for non globals, force new die. */
21496 if (context
21497 && TYPE_P (context)
21498 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21499 return;
21501 if (!(dwarf_version >= 3 || !dwarf_strict))
21502 return;
21504 scope_die = get_context_die (context);
21506 if (child)
21508 gcc_assert (scope_die->die_child);
21509 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21510 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21511 scope_die = scope_die->die_child;
21514 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21515 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21519 /* Write the debugging output for DECL. */
21521 void
21522 dwarf2out_decl (tree decl)
21524 dw_die_ref context_die = comp_unit_die ();
21526 switch (TREE_CODE (decl))
21528 case ERROR_MARK:
21529 return;
21531 case FUNCTION_DECL:
21532 /* What we would really like to do here is to filter out all mere
21533 file-scope declarations of file-scope functions which are never
21534 referenced later within this translation unit (and keep all of ones
21535 that *are* referenced later on) but we aren't clairvoyant, so we have
21536 no idea which functions will be referenced in the future (i.e. later
21537 on within the current translation unit). So here we just ignore all
21538 file-scope function declarations which are not also definitions. If
21539 and when the debugger needs to know something about these functions,
21540 it will have to hunt around and find the DWARF information associated
21541 with the definition of the function.
21543 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21544 nodes represent definitions and which ones represent mere
21545 declarations. We have to check DECL_INITIAL instead. That's because
21546 the C front-end supports some weird semantics for "extern inline"
21547 function definitions. These can get inlined within the current
21548 translation unit (and thus, we need to generate Dwarf info for their
21549 abstract instances so that the Dwarf info for the concrete inlined
21550 instances can have something to refer to) but the compiler never
21551 generates any out-of-lines instances of such things (despite the fact
21552 that they *are* definitions).
21554 The important point is that the C front-end marks these "extern
21555 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21556 them anyway. Note that the C++ front-end also plays some similar games
21557 for inline function definitions appearing within include files which
21558 also contain `#pragma interface' pragmas. */
21559 if (DECL_INITIAL (decl) == NULL_TREE)
21560 return;
21562 /* If we're a nested function, initially use a parent of NULL; if we're
21563 a plain function, this will be fixed up in decls_for_scope. If
21564 we're a method, it will be ignored, since we already have a DIE. */
21565 if (decl_function_context (decl)
21566 /* But if we're in terse mode, we don't care about scope. */
21567 && debug_info_level > DINFO_LEVEL_TERSE)
21568 context_die = NULL;
21569 break;
21571 case VAR_DECL:
21572 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21573 declaration and if the declaration was never even referenced from
21574 within this entire compilation unit. We suppress these DIEs in
21575 order to save space in the .debug section (by eliminating entries
21576 which are probably useless). Note that we must not suppress
21577 block-local extern declarations (whether used or not) because that
21578 would screw-up the debugger's name lookup mechanism and cause it to
21579 miss things which really ought to be in scope at a given point. */
21580 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21581 return;
21583 /* For local statics lookup proper context die. */
21584 if (TREE_STATIC (decl) && decl_function_context (decl))
21585 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21587 /* If we are in terse mode, don't generate any DIEs to represent any
21588 variable declarations or definitions. */
21589 if (debug_info_level <= DINFO_LEVEL_TERSE)
21590 return;
21591 break;
21593 case CONST_DECL:
21594 if (debug_info_level <= DINFO_LEVEL_TERSE)
21595 return;
21596 if (!is_fortran () && !is_ada ())
21597 return;
21598 if (TREE_STATIC (decl) && decl_function_context (decl))
21599 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21600 break;
21602 case NAMESPACE_DECL:
21603 case IMPORTED_DECL:
21604 if (debug_info_level <= DINFO_LEVEL_TERSE)
21605 return;
21606 if (lookup_decl_die (decl) != NULL)
21607 return;
21608 break;
21610 case TYPE_DECL:
21611 /* Don't emit stubs for types unless they are needed by other DIEs. */
21612 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21613 return;
21615 /* Don't bother trying to generate any DIEs to represent any of the
21616 normal built-in types for the language we are compiling. */
21617 if (DECL_IS_BUILTIN (decl))
21618 return;
21620 /* If we are in terse mode, don't generate any DIEs for types. */
21621 if (debug_info_level <= DINFO_LEVEL_TERSE)
21622 return;
21624 /* If we're a function-scope tag, initially use a parent of NULL;
21625 this will be fixed up in decls_for_scope. */
21626 if (decl_function_context (decl))
21627 context_die = NULL;
21629 break;
21631 default:
21632 return;
21635 gen_decl_die (decl, NULL, context_die);
21638 /* Write the debugging output for DECL. */
21640 static void
21641 dwarf2out_function_decl (tree decl)
21643 dwarf2out_decl (decl);
21644 call_arg_locations = NULL;
21645 call_arg_loc_last = NULL;
21646 call_site_count = -1;
21647 tail_call_site_count = -1;
21648 VEC_free (dw_die_ref, heap, block_map);
21649 htab_empty (decl_loc_table);
21652 /* Output a marker (i.e. a label) for the beginning of the generated code for
21653 a lexical block. */
21655 static void
21656 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21657 unsigned int blocknum)
21659 switch_to_section (current_function_section ());
21660 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21663 /* Output a marker (i.e. a label) for the end of the generated code for a
21664 lexical block. */
21666 static void
21667 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21669 switch_to_section (current_function_section ());
21670 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21673 /* Returns nonzero if it is appropriate not to emit any debugging
21674 information for BLOCK, because it doesn't contain any instructions.
21676 Don't allow this for blocks with nested functions or local classes
21677 as we would end up with orphans, and in the presence of scheduling
21678 we may end up calling them anyway. */
21680 static bool
21681 dwarf2out_ignore_block (const_tree block)
21683 tree decl;
21684 unsigned int i;
21686 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21687 if (TREE_CODE (decl) == FUNCTION_DECL
21688 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21689 return 0;
21690 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21692 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21693 if (TREE_CODE (decl) == FUNCTION_DECL
21694 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21695 return 0;
21698 return 1;
21701 /* Hash table routines for file_hash. */
21703 static int
21704 file_table_eq (const void *p1_p, const void *p2_p)
21706 const struct dwarf_file_data *const p1 =
21707 (const struct dwarf_file_data *) p1_p;
21708 const char *const p2 = (const char *) p2_p;
21709 return filename_cmp (p1->filename, p2) == 0;
21712 static hashval_t
21713 file_table_hash (const void *p_p)
21715 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21716 return htab_hash_string (p->filename);
21719 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21720 dwarf2out.c) and return its "index". The index of each (known) filename is
21721 just a unique number which is associated with only that one filename. We
21722 need such numbers for the sake of generating labels (in the .debug_sfnames
21723 section) and references to those files numbers (in the .debug_srcinfo
21724 and.debug_macinfo sections). If the filename given as an argument is not
21725 found in our current list, add it to the list and assign it the next
21726 available unique index number. In order to speed up searches, we remember
21727 the index of the filename was looked up last. This handles the majority of
21728 all searches. */
21730 static struct dwarf_file_data *
21731 lookup_filename (const char *file_name)
21733 void ** slot;
21734 struct dwarf_file_data * created;
21736 /* Check to see if the file name that was searched on the previous
21737 call matches this file name. If so, return the index. */
21738 if (file_table_last_lookup
21739 && (file_name == file_table_last_lookup->filename
21740 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21741 return file_table_last_lookup;
21743 /* Didn't match the previous lookup, search the table. */
21744 slot = htab_find_slot_with_hash (file_table, file_name,
21745 htab_hash_string (file_name), INSERT);
21746 if (*slot)
21747 return (struct dwarf_file_data *) *slot;
21749 created = ggc_alloc_dwarf_file_data ();
21750 created->filename = file_name;
21751 created->emitted_number = 0;
21752 *slot = created;
21753 return created;
21756 /* If the assembler will construct the file table, then translate the compiler
21757 internal file table number into the assembler file table number, and emit
21758 a .file directive if we haven't already emitted one yet. The file table
21759 numbers are different because we prune debug info for unused variables and
21760 types, which may include filenames. */
21762 static int
21763 maybe_emit_file (struct dwarf_file_data * fd)
21765 if (! fd->emitted_number)
21767 if (last_emitted_file)
21768 fd->emitted_number = last_emitted_file->emitted_number + 1;
21769 else
21770 fd->emitted_number = 1;
21771 last_emitted_file = fd;
21773 if (DWARF2_ASM_LINE_DEBUG_INFO)
21775 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21776 output_quoted_string (asm_out_file,
21777 remap_debug_filename (fd->filename));
21778 fputc ('\n', asm_out_file);
21782 return fd->emitted_number;
21785 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21786 That generation should happen after function debug info has been
21787 generated. The value of the attribute is the constant value of ARG. */
21789 static void
21790 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21792 die_arg_entry entry;
21794 if (!die || !arg)
21795 return;
21797 if (!tmpl_value_parm_die_table)
21798 tmpl_value_parm_die_table
21799 = VEC_alloc (die_arg_entry, gc, 32);
21801 entry.die = die;
21802 entry.arg = arg;
21803 VEC_safe_push (die_arg_entry, gc,
21804 tmpl_value_parm_die_table,
21805 &entry);
21808 /* Return TRUE if T is an instance of generic type, FALSE
21809 otherwise. */
21811 static bool
21812 generic_type_p (tree t)
21814 if (t == NULL_TREE || !TYPE_P (t))
21815 return false;
21816 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21819 /* Schedule the generation of the generic parameter dies for the
21820 instance of generic type T. The proper generation itself is later
21821 done by gen_scheduled_generic_parms_dies. */
21823 static void
21824 schedule_generic_params_dies_gen (tree t)
21826 if (!generic_type_p (t))
21827 return;
21829 if (generic_type_instances == NULL)
21830 generic_type_instances = VEC_alloc (tree, gc, 256);
21832 VEC_safe_push (tree, gc, generic_type_instances, t);
21835 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21836 by append_entry_to_tmpl_value_parm_die_table. This function must
21837 be called after function DIEs have been generated. */
21839 static void
21840 gen_remaining_tmpl_value_param_die_attribute (void)
21842 if (tmpl_value_parm_die_table)
21844 unsigned i;
21845 die_arg_entry *e;
21847 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21848 tree_add_const_value_attribute (e->die, e->arg);
21852 /* Generate generic parameters DIEs for instances of generic types
21853 that have been previously scheduled by
21854 schedule_generic_params_dies_gen. This function must be called
21855 after all the types of the CU have been laid out. */
21857 static void
21858 gen_scheduled_generic_parms_dies (void)
21860 unsigned i;
21861 tree t;
21863 if (generic_type_instances == NULL)
21864 return;
21866 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21867 gen_generic_params_dies (t);
21871 /* Replace DW_AT_name for the decl with name. */
21873 static void
21874 dwarf2out_set_name (tree decl, tree name)
21876 dw_die_ref die;
21877 dw_attr_ref attr;
21878 const char *dname;
21880 die = TYPE_SYMTAB_DIE (decl);
21881 if (!die)
21882 return;
21884 dname = dwarf2_name (name, 0);
21885 if (!dname)
21886 return;
21888 attr = get_AT (die, DW_AT_name);
21889 if (attr)
21891 struct indirect_string_node *node;
21893 node = find_AT_string (dname);
21894 /* replace the string. */
21895 attr->dw_attr_val.v.val_str = node;
21898 else
21899 add_name_attribute (die, dname);
21902 /* Called by the final INSN scan whenever we see a var location. We
21903 use it to drop labels in the right places, and throw the location in
21904 our lookup table. */
21906 static void
21907 dwarf2out_var_location (rtx loc_note)
21909 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21910 struct var_loc_node *newloc;
21911 rtx next_real;
21912 static const char *last_label;
21913 static const char *last_postcall_label;
21914 static bool last_in_cold_section_p;
21915 tree decl;
21916 bool var_loc_p;
21918 if (!NOTE_P (loc_note))
21920 if (CALL_P (loc_note))
21922 call_site_count++;
21923 if (SIBLING_CALL_P (loc_note))
21924 tail_call_site_count++;
21926 return;
21929 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21930 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21931 return;
21933 next_real = next_real_insn (loc_note);
21935 /* If there are no instructions which would be affected by this note,
21936 don't do anything. */
21937 if (var_loc_p
21938 && next_real == NULL_RTX
21939 && !NOTE_DURING_CALL_P (loc_note))
21940 return;
21942 if (next_real == NULL_RTX)
21943 next_real = get_last_insn ();
21945 /* If there were any real insns between note we processed last time
21946 and this note (or if it is the first note), clear
21947 last_{,postcall_}label so that they are not reused this time. */
21948 if (last_var_location_insn == NULL_RTX
21949 || last_var_location_insn != next_real
21950 || last_in_cold_section_p != in_cold_section_p)
21952 last_label = NULL;
21953 last_postcall_label = NULL;
21956 if (var_loc_p)
21958 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21959 newloc = add_var_loc_to_decl (decl, loc_note,
21960 NOTE_DURING_CALL_P (loc_note)
21961 ? last_postcall_label : last_label);
21962 if (newloc == NULL)
21963 return;
21965 else
21967 decl = NULL_TREE;
21968 newloc = NULL;
21971 /* If there were no real insns between note we processed last time
21972 and this note, use the label we emitted last time. Otherwise
21973 create a new label and emit it. */
21974 if (last_label == NULL)
21976 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21977 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21978 loclabel_num++;
21979 last_label = ggc_strdup (loclabel);
21982 if (!var_loc_p)
21984 struct call_arg_loc_node *ca_loc
21985 = ggc_alloc_cleared_call_arg_loc_node ();
21986 rtx prev = prev_real_insn (loc_note), x;
21987 ca_loc->call_arg_loc_note = loc_note;
21988 ca_loc->next = NULL;
21989 ca_loc->label = last_label;
21990 gcc_assert (prev
21991 && (CALL_P (prev)
21992 || (NONJUMP_INSN_P (prev)
21993 && GET_CODE (PATTERN (prev)) == SEQUENCE
21994 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21995 if (!CALL_P (prev))
21996 prev = XVECEXP (PATTERN (prev), 0, 0);
21997 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21998 x = PATTERN (prev);
21999 if (GET_CODE (x) == PARALLEL)
22000 x = XVECEXP (x, 0, 0);
22001 if (GET_CODE (x) == SET)
22002 x = SET_SRC (x);
22003 if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22005 x = XEXP (XEXP (x, 0), 0);
22006 if (GET_CODE (x) == SYMBOL_REF
22007 && SYMBOL_REF_DECL (x)
22008 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22009 ca_loc->symbol_ref = x;
22011 ca_loc->block = insn_scope (prev);
22012 if (call_arg_locations)
22013 call_arg_loc_last->next = ca_loc;
22014 else
22015 call_arg_locations = ca_loc;
22016 call_arg_loc_last = ca_loc;
22018 else if (!NOTE_DURING_CALL_P (loc_note))
22019 newloc->label = last_label;
22020 else
22022 if (!last_postcall_label)
22024 sprintf (loclabel, "%s-1", last_label);
22025 last_postcall_label = ggc_strdup (loclabel);
22027 newloc->label = last_postcall_label;
22030 last_var_location_insn = next_real;
22031 last_in_cold_section_p = in_cold_section_p;
22034 /* We need to reset the locations at the beginning of each
22035 function. We can't do this in the end_function hook, because the
22036 declarations that use the locations won't have been output when
22037 that hook is called. Also compute have_multiple_function_sections here. */
22039 static void
22040 dwarf2out_begin_function (tree fun)
22042 if (function_section (fun) != text_section)
22043 have_multiple_function_sections = true;
22044 else if (flag_reorder_blocks_and_partition && !cold_text_section)
22046 gcc_assert (current_function_decl == fun);
22047 cold_text_section = unlikely_text_section ();
22048 switch_to_section (cold_text_section);
22049 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22050 switch_to_section (current_function_section ());
22053 dwarf2out_note_section_used ();
22054 call_site_count = 0;
22055 tail_call_site_count = 0;
22058 /* Output a label to mark the beginning of a source code line entry
22059 and record information relating to this source line, in
22060 'line_info_table' for later output of the .debug_line section. */
22062 static void
22063 dwarf2out_source_line (unsigned int line, const char *filename,
22064 int discriminator, bool is_stmt)
22066 static bool last_is_stmt = true;
22068 if (debug_info_level >= DINFO_LEVEL_NORMAL
22069 && line != 0)
22071 int file_num = maybe_emit_file (lookup_filename (filename));
22073 switch_to_section (current_function_section ());
22075 /* If requested, emit something human-readable. */
22076 if (flag_debug_asm)
22077 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
22078 filename, line);
22080 if (DWARF2_ASM_LINE_DEBUG_INFO)
22082 /* Emit the .loc directive understood by GNU as. */
22083 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22084 if (is_stmt != last_is_stmt)
22086 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22087 last_is_stmt = is_stmt;
22089 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22090 fprintf (asm_out_file, " discriminator %d", discriminator);
22091 fputc ('\n', asm_out_file);
22093 /* Indicate that line number info exists. */
22094 line_info_table_in_use++;
22096 else if (function_section (current_function_decl) != text_section)
22098 dw_separate_line_info_ref line_info;
22099 targetm.asm_out.internal_label (asm_out_file,
22100 SEPARATE_LINE_CODE_LABEL,
22101 separate_line_info_table_in_use);
22103 /* Expand the line info table if necessary. */
22104 if (separate_line_info_table_in_use
22105 == separate_line_info_table_allocated)
22107 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22108 separate_line_info_table
22109 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22110 separate_line_info_table,
22111 separate_line_info_table_allocated);
22112 memset (separate_line_info_table
22113 + separate_line_info_table_in_use,
22115 (LINE_INFO_TABLE_INCREMENT
22116 * sizeof (dw_separate_line_info_entry)));
22119 /* Add the new entry at the end of the line_info_table. */
22120 line_info
22121 = &separate_line_info_table[separate_line_info_table_in_use++];
22122 line_info->dw_file_num = file_num;
22123 line_info->dw_line_num = line;
22124 line_info->function = current_function_funcdef_no;
22126 else
22128 dw_line_info_ref line_info;
22130 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22131 line_info_table_in_use);
22133 /* Expand the line info table if necessary. */
22134 if (line_info_table_in_use == line_info_table_allocated)
22136 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22137 line_info_table
22138 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22139 line_info_table_allocated);
22140 memset (line_info_table + line_info_table_in_use, 0,
22141 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22144 /* Add the new entry at the end of the line_info_table. */
22145 line_info = &line_info_table[line_info_table_in_use++];
22146 line_info->dw_file_num = file_num;
22147 line_info->dw_line_num = line;
22152 /* Record the beginning of a new source file. */
22154 static void
22155 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22157 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22159 /* Record the beginning of the file for break_out_includes. */
22160 dw_die_ref bincl_die;
22162 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22163 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22166 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22168 macinfo_entry e;
22169 e.code = DW_MACINFO_start_file;
22170 e.lineno = lineno;
22171 e.info = xstrdup (filename);
22172 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22176 /* Record the end of a source file. */
22178 static void
22179 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22181 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22182 /* Record the end of the file for break_out_includes. */
22183 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22185 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22187 macinfo_entry e;
22188 e.code = DW_MACINFO_end_file;
22189 e.lineno = lineno;
22190 e.info = NULL;
22191 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22195 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22196 the tail part of the directive line, i.e. the part which is past the
22197 initial whitespace, #, whitespace, directive-name, whitespace part. */
22199 static void
22200 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22201 const char *buffer ATTRIBUTE_UNUSED)
22203 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22205 macinfo_entry e;
22206 e.code = DW_MACINFO_define;
22207 e.lineno = lineno;
22208 e.info = xstrdup (buffer);;
22209 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22213 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22214 the tail part of the directive line, i.e. the part which is past the
22215 initial whitespace, #, whitespace, directive-name, whitespace part. */
22217 static void
22218 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22219 const char *buffer ATTRIBUTE_UNUSED)
22221 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22223 macinfo_entry e;
22224 e.code = DW_MACINFO_undef;
22225 e.lineno = lineno;
22226 e.info = xstrdup (buffer);;
22227 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22231 static void
22232 output_macinfo (void)
22234 unsigned i;
22235 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22236 macinfo_entry *ref;
22238 if (! length)
22239 return;
22241 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22243 switch (ref->code)
22245 case DW_MACINFO_start_file:
22247 int file_num = maybe_emit_file (lookup_filename (ref->info));
22248 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22249 dw2_asm_output_data_uleb128
22250 (ref->lineno, "Included from line number %lu",
22251 (unsigned long)ref->lineno);
22252 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22254 break;
22255 case DW_MACINFO_end_file:
22256 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22257 break;
22258 case DW_MACINFO_define:
22259 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22260 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22261 (unsigned long)ref->lineno);
22262 dw2_asm_output_nstring (ref->info, -1, "The macro");
22263 break;
22264 case DW_MACINFO_undef:
22265 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22266 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22267 (unsigned long)ref->lineno);
22268 dw2_asm_output_nstring (ref->info, -1, "The macro");
22269 break;
22270 default:
22271 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22272 ASM_COMMENT_START, (unsigned long)ref->code);
22273 break;
22278 /* Set up for Dwarf output at the start of compilation. */
22280 static void
22281 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22283 /* Allocate the file_table. */
22284 file_table = htab_create_ggc (50, file_table_hash,
22285 file_table_eq, NULL);
22287 /* Allocate the decl_die_table. */
22288 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22289 decl_die_table_eq, NULL);
22291 /* Allocate the decl_loc_table. */
22292 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22293 decl_loc_table_eq, NULL);
22295 /* Allocate the initial hunk of the decl_scope_table. */
22296 decl_scope_table = VEC_alloc (tree, gc, 256);
22298 /* Allocate the initial hunk of the abbrev_die_table. */
22299 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22300 (ABBREV_DIE_TABLE_INCREMENT);
22301 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22302 /* Zero-th entry is allocated, but unused. */
22303 abbrev_die_table_in_use = 1;
22305 /* Allocate the initial hunk of the line_info_table. */
22306 line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22307 (LINE_INFO_TABLE_INCREMENT);
22308 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22310 /* Zero-th entry is allocated, but unused. */
22311 line_info_table_in_use = 1;
22313 /* Allocate the pubtypes and pubnames vectors. */
22314 pubname_table = VEC_alloc (pubname_entry, gc, 32);
22315 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22317 incomplete_types = VEC_alloc (tree, gc, 64);
22319 used_rtx_array = VEC_alloc (rtx, gc, 32);
22321 debug_info_section = get_section (DEBUG_INFO_SECTION,
22322 SECTION_DEBUG, NULL);
22323 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22324 SECTION_DEBUG, NULL);
22325 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22326 SECTION_DEBUG, NULL);
22327 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22328 SECTION_DEBUG, NULL);
22329 debug_line_section = get_section (DEBUG_LINE_SECTION,
22330 SECTION_DEBUG, NULL);
22331 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22332 SECTION_DEBUG, NULL);
22333 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22334 SECTION_DEBUG, NULL);
22335 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22336 SECTION_DEBUG, NULL);
22337 debug_str_section = get_section (DEBUG_STR_SECTION,
22338 DEBUG_STR_SECTION_FLAGS, NULL);
22339 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22340 SECTION_DEBUG, NULL);
22341 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22342 SECTION_DEBUG, NULL);
22344 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22345 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22346 DEBUG_ABBREV_SECTION_LABEL, 0);
22347 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22348 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22349 COLD_TEXT_SECTION_LABEL, 0);
22350 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22352 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22353 DEBUG_INFO_SECTION_LABEL, 0);
22354 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22355 DEBUG_LINE_SECTION_LABEL, 0);
22356 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22357 DEBUG_RANGES_SECTION_LABEL, 0);
22358 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22359 DEBUG_MACINFO_SECTION_LABEL, 0);
22361 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22362 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22364 switch_to_section (text_section);
22365 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22368 /* Called before cgraph_optimize starts outputtting functions, variables
22369 and toplevel asms into assembly. */
22371 static void
22372 dwarf2out_assembly_start (void)
22374 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22375 && dwarf2out_do_cfi_asm ()
22376 && (!(flag_unwind_tables || flag_exceptions)
22377 || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22378 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22381 /* A helper function for dwarf2out_finish called through
22382 htab_traverse. Emit one queued .debug_str string. */
22384 static int
22385 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22387 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22389 if (node->label && node->refcount)
22391 switch_to_section (debug_str_section);
22392 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22393 assemble_string (node->str, strlen (node->str) + 1);
22396 return 1;
22399 #if ENABLE_ASSERT_CHECKING
22400 /* Verify that all marks are clear. */
22402 static void
22403 verify_marks_clear (dw_die_ref die)
22405 dw_die_ref c;
22407 gcc_assert (! die->die_mark);
22408 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22410 #endif /* ENABLE_ASSERT_CHECKING */
22412 /* Clear the marks for a die and its children.
22413 Be cool if the mark isn't set. */
22415 static void
22416 prune_unmark_dies (dw_die_ref die)
22418 dw_die_ref c;
22420 if (die->die_mark)
22421 die->die_mark = 0;
22422 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22425 /* Given DIE that we're marking as used, find any other dies
22426 it references as attributes and mark them as used. */
22428 static void
22429 prune_unused_types_walk_attribs (dw_die_ref die)
22431 dw_attr_ref a;
22432 unsigned ix;
22434 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22436 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22438 /* A reference to another DIE.
22439 Make sure that it will get emitted.
22440 If it was broken out into a comdat group, don't follow it. */
22441 if (dwarf_version < 4
22442 || a->dw_attr == DW_AT_specification
22443 || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22444 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22446 /* Set the string's refcount to 0 so that prune_unused_types_mark
22447 accounts properly for it. */
22448 if (AT_class (a) == dw_val_class_str)
22449 a->dw_attr_val.v.val_str->refcount = 0;
22453 /* Mark the generic parameters and arguments children DIEs of DIE. */
22455 static void
22456 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22458 dw_die_ref c;
22460 if (die == NULL || die->die_child == NULL)
22461 return;
22462 c = die->die_child;
22465 switch (c->die_tag)
22467 case DW_TAG_template_type_param:
22468 case DW_TAG_template_value_param:
22469 case DW_TAG_GNU_template_template_param:
22470 case DW_TAG_GNU_template_parameter_pack:
22471 prune_unused_types_mark (c, 1);
22472 break;
22473 default:
22474 break;
22476 c = c->die_sib;
22477 } while (c && c != die->die_child);
22480 /* Mark DIE as being used. If DOKIDS is true, then walk down
22481 to DIE's children. */
22483 static void
22484 prune_unused_types_mark (dw_die_ref die, int dokids)
22486 dw_die_ref c;
22488 if (die->die_mark == 0)
22490 /* We haven't done this node yet. Mark it as used. */
22491 die->die_mark = 1;
22492 /* If this is the DIE of a generic type instantiation,
22493 mark the children DIEs that describe its generic parms and
22494 args. */
22495 prune_unused_types_mark_generic_parms_dies (die);
22497 /* We also have to mark its parents as used.
22498 (But we don't want to mark our parents' kids due to this.) */
22499 if (die->die_parent)
22500 prune_unused_types_mark (die->die_parent, 0);
22502 /* Mark any referenced nodes. */
22503 prune_unused_types_walk_attribs (die);
22505 /* If this node is a specification,
22506 also mark the definition, if it exists. */
22507 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22508 prune_unused_types_mark (die->die_definition, 1);
22511 if (dokids && die->die_mark != 2)
22513 /* We need to walk the children, but haven't done so yet.
22514 Remember that we've walked the kids. */
22515 die->die_mark = 2;
22517 /* If this is an array type, we need to make sure our
22518 kids get marked, even if they're types. If we're
22519 breaking out types into comdat sections, do this
22520 for all type definitions. */
22521 if (die->die_tag == DW_TAG_array_type
22522 || (dwarf_version >= 4
22523 && is_type_die (die) && ! is_declaration_die (die)))
22524 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22525 else
22526 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22530 /* For local classes, look if any static member functions were emitted
22531 and if so, mark them. */
22533 static void
22534 prune_unused_types_walk_local_classes (dw_die_ref die)
22536 dw_die_ref c;
22538 if (die->die_mark == 2)
22539 return;
22541 switch (die->die_tag)
22543 case DW_TAG_structure_type:
22544 case DW_TAG_union_type:
22545 case DW_TAG_class_type:
22546 break;
22548 case DW_TAG_subprogram:
22549 if (!get_AT_flag (die, DW_AT_declaration)
22550 || die->die_definition != NULL)
22551 prune_unused_types_mark (die, 1);
22552 return;
22554 default:
22555 return;
22558 /* Mark children. */
22559 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22562 /* Walk the tree DIE and mark types that we actually use. */
22564 static void
22565 prune_unused_types_walk (dw_die_ref die)
22567 dw_die_ref c;
22569 /* Don't do anything if this node is already marked and
22570 children have been marked as well. */
22571 if (die->die_mark == 2)
22572 return;
22574 switch (die->die_tag)
22576 case DW_TAG_structure_type:
22577 case DW_TAG_union_type:
22578 case DW_TAG_class_type:
22579 if (die->die_perennial_p)
22580 break;
22582 for (c = die->die_parent; c; c = c->die_parent)
22583 if (c->die_tag == DW_TAG_subprogram)
22584 break;
22586 /* Finding used static member functions inside of classes
22587 is needed just for local classes, because for other classes
22588 static member function DIEs with DW_AT_specification
22589 are emitted outside of the DW_TAG_*_type. If we ever change
22590 it, we'd need to call this even for non-local classes. */
22591 if (c)
22592 prune_unused_types_walk_local_classes (die);
22594 /* It's a type node --- don't mark it. */
22595 return;
22597 case DW_TAG_const_type:
22598 case DW_TAG_packed_type:
22599 case DW_TAG_pointer_type:
22600 case DW_TAG_reference_type:
22601 case DW_TAG_rvalue_reference_type:
22602 case DW_TAG_volatile_type:
22603 case DW_TAG_typedef:
22604 case DW_TAG_array_type:
22605 case DW_TAG_interface_type:
22606 case DW_TAG_friend:
22607 case DW_TAG_variant_part:
22608 case DW_TAG_enumeration_type:
22609 case DW_TAG_subroutine_type:
22610 case DW_TAG_string_type:
22611 case DW_TAG_set_type:
22612 case DW_TAG_subrange_type:
22613 case DW_TAG_ptr_to_member_type:
22614 case DW_TAG_file_type:
22615 if (die->die_perennial_p)
22616 break;
22618 /* It's a type node --- don't mark it. */
22619 return;
22621 default:
22622 /* Mark everything else. */
22623 break;
22626 if (die->die_mark == 0)
22628 die->die_mark = 1;
22630 /* Now, mark any dies referenced from here. */
22631 prune_unused_types_walk_attribs (die);
22634 die->die_mark = 2;
22636 /* Mark children. */
22637 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22640 /* Increment the string counts on strings referred to from DIE's
22641 attributes. */
22643 static void
22644 prune_unused_types_update_strings (dw_die_ref die)
22646 dw_attr_ref a;
22647 unsigned ix;
22649 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22650 if (AT_class (a) == dw_val_class_str)
22652 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22653 s->refcount++;
22654 /* Avoid unnecessarily putting strings that are used less than
22655 twice in the hash table. */
22656 if (s->refcount
22657 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22659 void ** slot;
22660 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22661 htab_hash_string (s->str),
22662 INSERT);
22663 gcc_assert (*slot == NULL);
22664 *slot = s;
22669 /* Remove from the tree DIE any dies that aren't marked. */
22671 static void
22672 prune_unused_types_prune (dw_die_ref die)
22674 dw_die_ref c;
22676 gcc_assert (die->die_mark);
22677 prune_unused_types_update_strings (die);
22679 if (! die->die_child)
22680 return;
22682 c = die->die_child;
22683 do {
22684 dw_die_ref prev = c;
22685 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22686 if (c == die->die_child)
22688 /* No marked children between 'prev' and the end of the list. */
22689 if (prev == c)
22690 /* No marked children at all. */
22691 die->die_child = NULL;
22692 else
22694 prev->die_sib = c->die_sib;
22695 die->die_child = prev;
22697 return;
22700 if (c != prev->die_sib)
22701 prev->die_sib = c;
22702 prune_unused_types_prune (c);
22703 } while (c != die->die_child);
22706 /* A helper function for dwarf2out_finish called through
22707 htab_traverse. Clear .debug_str strings that we haven't already
22708 decided to emit. */
22710 static int
22711 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22713 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22715 if (!node->label || !node->refcount)
22716 htab_clear_slot (debug_str_hash, h);
22718 return 1;
22721 /* Remove dies representing declarations that we never use. */
22723 static void
22724 prune_unused_types (void)
22726 unsigned int i;
22727 limbo_die_node *node;
22728 comdat_type_node *ctnode;
22729 pubname_ref pub;
22731 #if ENABLE_ASSERT_CHECKING
22732 /* All the marks should already be clear. */
22733 verify_marks_clear (comp_unit_die ());
22734 for (node = limbo_die_list; node; node = node->next)
22735 verify_marks_clear (node->die);
22736 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22737 verify_marks_clear (ctnode->root_die);
22738 #endif /* ENABLE_ASSERT_CHECKING */
22740 /* Mark types that are used in global variables. */
22741 premark_types_used_by_global_vars ();
22743 /* Set the mark on nodes that are actually used. */
22744 prune_unused_types_walk (comp_unit_die ());
22745 for (node = limbo_die_list; node; node = node->next)
22746 prune_unused_types_walk (node->die);
22747 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22749 prune_unused_types_walk (ctnode->root_die);
22750 prune_unused_types_mark (ctnode->type_die, 1);
22753 /* Also set the mark on nodes referenced from the
22754 pubname_table or arange_table. */
22755 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22756 prune_unused_types_mark (pub->die, 1);
22757 for (i = 0; i < arange_table_in_use; i++)
22758 prune_unused_types_mark (arange_table[i], 1);
22760 /* Get rid of nodes that aren't marked; and update the string counts. */
22761 if (debug_str_hash && debug_str_hash_forced)
22762 htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22763 else if (debug_str_hash)
22764 htab_empty (debug_str_hash);
22765 prune_unused_types_prune (comp_unit_die ());
22766 for (node = limbo_die_list; node; node = node->next)
22767 prune_unused_types_prune (node->die);
22768 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22769 prune_unused_types_prune (ctnode->root_die);
22771 /* Leave the marks clear. */
22772 prune_unmark_dies (comp_unit_die ());
22773 for (node = limbo_die_list; node; node = node->next)
22774 prune_unmark_dies (node->die);
22775 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22776 prune_unmark_dies (ctnode->root_die);
22779 /* Set the parameter to true if there are any relative pathnames in
22780 the file table. */
22781 static int
22782 file_table_relative_p (void ** slot, void *param)
22784 bool *p = (bool *) param;
22785 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22786 if (!IS_ABSOLUTE_PATH (d->filename))
22788 *p = true;
22789 return 0;
22791 return 1;
22794 /* Routines to manipulate hash table of comdat type units. */
22796 static hashval_t
22797 htab_ct_hash (const void *of)
22799 hashval_t h;
22800 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22802 memcpy (&h, type_node->signature, sizeof (h));
22803 return h;
22806 static int
22807 htab_ct_eq (const void *of1, const void *of2)
22809 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22810 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22812 return (! memcmp (type_node_1->signature, type_node_2->signature,
22813 DWARF_TYPE_SIGNATURE_SIZE));
22816 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22817 to the location it would have been added, should we know its
22818 DECL_ASSEMBLER_NAME when we added other attributes. This will
22819 probably improve compactness of debug info, removing equivalent
22820 abbrevs, and hide any differences caused by deferring the
22821 computation of the assembler name, triggered by e.g. PCH. */
22823 static inline void
22824 move_linkage_attr (dw_die_ref die)
22826 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22827 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22829 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22830 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22832 while (--ix > 0)
22834 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22836 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22837 break;
22840 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22842 VEC_pop (dw_attr_node, die->die_attr);
22843 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22847 /* Helper function for resolve_addr, attempt to resolve
22848 one CONST_STRING, return non-zero if not successful. Similarly verify that
22849 SYMBOL_REFs refer to variables emitted in the current CU. */
22851 static int
22852 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22854 rtx rtl = *addr;
22856 if (GET_CODE (rtl) == CONST_STRING)
22858 size_t len = strlen (XSTR (rtl, 0)) + 1;
22859 tree t = build_string (len, XSTR (rtl, 0));
22860 tree tlen = build_int_cst (NULL_TREE, len - 1);
22861 TREE_TYPE (t)
22862 = build_array_type (char_type_node, build_index_type (tlen));
22863 rtl = lookup_constant_def (t);
22864 if (!rtl || !MEM_P (rtl))
22865 return 1;
22866 rtl = XEXP (rtl, 0);
22867 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22868 *addr = rtl;
22869 return 0;
22872 if (GET_CODE (rtl) == SYMBOL_REF
22873 && SYMBOL_REF_DECL (rtl))
22875 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22877 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22878 return 1;
22880 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22881 return 1;
22884 if (GET_CODE (rtl) == CONST
22885 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22886 return 1;
22888 return 0;
22891 /* Helper function for resolve_addr, handle one location
22892 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22893 the location list couldn't be resolved. */
22895 static bool
22896 resolve_addr_in_expr (dw_loc_descr_ref loc)
22898 for (; loc; loc = loc->dw_loc_next)
22899 if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22900 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22901 || (loc->dw_loc_opc == DW_OP_implicit_value
22902 && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22903 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22904 return false;
22905 else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22906 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22908 dw_die_ref ref
22909 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22910 if (ref == NULL)
22911 return false;
22912 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22913 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22914 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22916 return true;
22919 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22920 an address in .rodata section if the string literal is emitted there,
22921 or remove the containing location list or replace DW_AT_const_value
22922 with DW_AT_location and empty location expression, if it isn't found
22923 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22924 to something that has been emitted in the current CU. */
22926 static void
22927 resolve_addr (dw_die_ref die)
22929 dw_die_ref c;
22930 dw_attr_ref a;
22931 dw_loc_list_ref *curr;
22932 unsigned ix;
22934 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22935 switch (AT_class (a))
22937 case dw_val_class_loc_list:
22938 curr = AT_loc_list_ptr (a);
22939 while (*curr)
22941 if (!resolve_addr_in_expr ((*curr)->expr))
22943 dw_loc_list_ref next = (*curr)->dw_loc_next;
22944 if (next && (*curr)->ll_symbol)
22946 gcc_assert (!next->ll_symbol);
22947 next->ll_symbol = (*curr)->ll_symbol;
22949 *curr = next;
22951 else
22952 curr = &(*curr)->dw_loc_next;
22954 if (!AT_loc_list (a))
22956 remove_AT (die, a->dw_attr);
22957 ix--;
22959 break;
22960 case dw_val_class_loc:
22961 if (!resolve_addr_in_expr (AT_loc (a)))
22963 remove_AT (die, a->dw_attr);
22964 ix--;
22966 break;
22967 case dw_val_class_addr:
22968 if (a->dw_attr == DW_AT_const_value
22969 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22971 remove_AT (die, a->dw_attr);
22972 ix--;
22974 if (die->die_tag == DW_TAG_GNU_call_site
22975 && a->dw_attr == DW_AT_abstract_origin)
22977 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22978 dw_die_ref tdie = lookup_decl_die (tdecl);
22979 if (tdie == NULL && DECL_EXTERNAL (tdecl))
22981 force_decl_die (tdecl);
22982 tdie = lookup_decl_die (tdecl);
22984 if (tdie)
22986 a->dw_attr_val.val_class = dw_val_class_die_ref;
22987 a->dw_attr_val.v.val_die_ref.die = tdie;
22988 a->dw_attr_val.v.val_die_ref.external = 0;
22990 else
22992 remove_AT (die, a->dw_attr);
22993 ix--;
22996 break;
22997 default:
22998 break;
23001 FOR_EACH_CHILD (die, c, resolve_addr (c));
23004 /* Helper routines for optimize_location_lists.
23005 This pass tries to share identical local lists in .debug_loc
23006 section. */
23008 /* Iteratively hash operands of LOC opcode. */
23010 static inline hashval_t
23011 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23013 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23014 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23016 switch (loc->dw_loc_opc)
23018 case DW_OP_const4u:
23019 case DW_OP_const8u:
23020 if (loc->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 hash = iterative_hash_object (val1->v.val_int, hash);
23071 break;
23072 case DW_OP_skip:
23073 case DW_OP_bra:
23075 int offset;
23077 gcc_assert (val1->val_class == dw_val_class_loc);
23078 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23079 hash = iterative_hash_object (offset, hash);
23081 break;
23082 case DW_OP_implicit_value:
23083 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23084 switch (val2->val_class)
23086 case dw_val_class_const:
23087 hash = iterative_hash_object (val2->v.val_int, hash);
23088 break;
23089 case dw_val_class_vec:
23091 unsigned int elt_size = val2->v.val_vec.elt_size;
23092 unsigned int len = val2->v.val_vec.length;
23094 hash = iterative_hash_object (elt_size, hash);
23095 hash = iterative_hash_object (len, hash);
23096 hash = iterative_hash (val2->v.val_vec.array,
23097 len * elt_size, hash);
23099 break;
23100 case dw_val_class_const_double:
23101 hash = iterative_hash_object (val2->v.val_double.low, hash);
23102 hash = iterative_hash_object (val2->v.val_double.high, hash);
23103 break;
23104 case dw_val_class_addr:
23105 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23106 break;
23107 default:
23108 gcc_unreachable ();
23110 break;
23111 case DW_OP_bregx:
23112 case DW_OP_bit_piece:
23113 hash = iterative_hash_object (val1->v.val_int, hash);
23114 hash = iterative_hash_object (val2->v.val_int, hash);
23115 break;
23116 case DW_OP_addr:
23117 hash_addr:
23118 if (loc->dtprel)
23120 unsigned char dtprel = 0xd1;
23121 hash = iterative_hash_object (dtprel, hash);
23123 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23124 break;
23125 case DW_OP_GNU_implicit_pointer:
23126 hash = iterative_hash_object (val2->v.val_int, hash);
23127 break;
23129 default:
23130 /* Other codes have no operands. */
23131 break;
23133 return hash;
23136 /* Iteratively hash the whole DWARF location expression LOC. */
23138 static inline hashval_t
23139 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23141 dw_loc_descr_ref l;
23142 bool sizes_computed = false;
23143 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23144 size_of_locs (loc);
23146 for (l = loc; l != NULL; l = l->dw_loc_next)
23148 enum dwarf_location_atom opc = l->dw_loc_opc;
23149 hash = iterative_hash_object (opc, hash);
23150 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23152 size_of_locs (loc);
23153 sizes_computed = true;
23155 hash = hash_loc_operands (l, hash);
23157 return hash;
23160 /* Compute hash of the whole location list LIST_HEAD. */
23162 static inline void
23163 hash_loc_list (dw_loc_list_ref list_head)
23165 dw_loc_list_ref curr = list_head;
23166 hashval_t hash = 0;
23168 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23170 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23171 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23172 if (curr->section)
23173 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23174 hash);
23175 hash = hash_locs (curr->expr, hash);
23177 list_head->hash = hash;
23180 /* Return true if X and Y opcodes have the same operands. */
23182 static inline bool
23183 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23185 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23186 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23187 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23188 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23190 switch (x->dw_loc_opc)
23192 case DW_OP_const4u:
23193 case DW_OP_const8u:
23194 if (x->dtprel)
23195 goto hash_addr;
23196 /* FALLTHRU */
23197 case DW_OP_const1u:
23198 case DW_OP_const1s:
23199 case DW_OP_const2u:
23200 case DW_OP_const2s:
23201 case DW_OP_const4s:
23202 case DW_OP_const8s:
23203 case DW_OP_constu:
23204 case DW_OP_consts:
23205 case DW_OP_pick:
23206 case DW_OP_plus_uconst:
23207 case DW_OP_breg0:
23208 case DW_OP_breg1:
23209 case DW_OP_breg2:
23210 case DW_OP_breg3:
23211 case DW_OP_breg4:
23212 case DW_OP_breg5:
23213 case DW_OP_breg6:
23214 case DW_OP_breg7:
23215 case DW_OP_breg8:
23216 case DW_OP_breg9:
23217 case DW_OP_breg10:
23218 case DW_OP_breg11:
23219 case DW_OP_breg12:
23220 case DW_OP_breg13:
23221 case DW_OP_breg14:
23222 case DW_OP_breg15:
23223 case DW_OP_breg16:
23224 case DW_OP_breg17:
23225 case DW_OP_breg18:
23226 case DW_OP_breg19:
23227 case DW_OP_breg20:
23228 case DW_OP_breg21:
23229 case DW_OP_breg22:
23230 case DW_OP_breg23:
23231 case DW_OP_breg24:
23232 case DW_OP_breg25:
23233 case DW_OP_breg26:
23234 case DW_OP_breg27:
23235 case DW_OP_breg28:
23236 case DW_OP_breg29:
23237 case DW_OP_breg30:
23238 case DW_OP_breg31:
23239 case DW_OP_regx:
23240 case DW_OP_fbreg:
23241 case DW_OP_piece:
23242 case DW_OP_deref_size:
23243 case DW_OP_xderef_size:
23244 return valx1->v.val_int == valy1->v.val_int;
23245 case DW_OP_skip:
23246 case DW_OP_bra:
23247 gcc_assert (valx1->val_class == dw_val_class_loc
23248 && valy1->val_class == dw_val_class_loc
23249 && x->dw_loc_addr == y->dw_loc_addr);
23250 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23251 case DW_OP_implicit_value:
23252 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23253 || valx2->val_class != valy2->val_class)
23254 return false;
23255 switch (valx2->val_class)
23257 case dw_val_class_const:
23258 return valx2->v.val_int == valy2->v.val_int;
23259 case dw_val_class_vec:
23260 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23261 && valx2->v.val_vec.length == valy2->v.val_vec.length
23262 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23263 valx2->v.val_vec.elt_size
23264 * valx2->v.val_vec.length) == 0;
23265 case dw_val_class_const_double:
23266 return valx2->v.val_double.low == valy2->v.val_double.low
23267 && valx2->v.val_double.high == valy2->v.val_double.high;
23268 case dw_val_class_addr:
23269 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23270 default:
23271 gcc_unreachable ();
23273 case DW_OP_bregx:
23274 case DW_OP_bit_piece:
23275 return valx1->v.val_int == valy1->v.val_int
23276 && valx2->v.val_int == valy2->v.val_int;
23277 case DW_OP_addr:
23278 hash_addr:
23279 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23280 case DW_OP_GNU_implicit_pointer:
23281 return valx1->val_class == dw_val_class_die_ref
23282 && valx1->val_class == valy1->val_class
23283 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23284 && valx2->v.val_int == valy2->v.val_int;
23285 default:
23286 /* Other codes have no operands. */
23287 return true;
23291 /* Return true if DWARF location expressions X and Y are the same. */
23293 static inline bool
23294 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23296 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23297 if (x->dw_loc_opc != y->dw_loc_opc
23298 || x->dtprel != y->dtprel
23299 || !compare_loc_operands (x, y))
23300 break;
23301 return x == NULL && y == NULL;
23304 /* Return precomputed hash of location list X. */
23306 static hashval_t
23307 loc_list_hash (const void *x)
23309 return ((const struct dw_loc_list_struct *) x)->hash;
23312 /* Return 1 if location lists X and Y are the same. */
23314 static int
23315 loc_list_eq (const void *x, const void *y)
23317 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23318 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23319 if (a == b)
23320 return 1;
23321 if (a->hash != b->hash)
23322 return 0;
23323 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23324 if (strcmp (a->begin, b->begin) != 0
23325 || strcmp (a->end, b->end) != 0
23326 || (a->section == NULL) != (b->section == NULL)
23327 || (a->section && strcmp (a->section, b->section) != 0)
23328 || !compare_locs (a->expr, b->expr))
23329 break;
23330 return a == NULL && b == NULL;
23333 /* Recursively optimize location lists referenced from DIE
23334 children and share them whenever possible. */
23336 static void
23337 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23339 dw_die_ref c;
23340 dw_attr_ref a;
23341 unsigned ix;
23342 void **slot;
23344 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23345 if (AT_class (a) == dw_val_class_loc_list)
23347 dw_loc_list_ref list = AT_loc_list (a);
23348 /* TODO: perform some optimizations here, before hashing
23349 it and storing into the hash table. */
23350 hash_loc_list (list);
23351 slot = htab_find_slot_with_hash (htab, list, list->hash,
23352 INSERT);
23353 if (*slot == NULL)
23354 *slot = (void *) list;
23355 else
23356 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23359 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23362 /* Optimize location lists referenced from DIE
23363 children and share them whenever possible. */
23365 static void
23366 optimize_location_lists (dw_die_ref die)
23368 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23369 optimize_location_lists_1 (die, htab);
23370 htab_delete (htab);
23373 /* Output stuff that dwarf requires at the end of every file,
23374 and generate the DWARF-2 debugging info. */
23376 static void
23377 dwarf2out_finish (const char *filename)
23379 limbo_die_node *node, *next_node;
23380 comdat_type_node *ctnode;
23381 htab_t comdat_type_table;
23382 unsigned int i;
23384 gen_scheduled_generic_parms_dies ();
23385 gen_remaining_tmpl_value_param_die_attribute ();
23387 /* Add the name for the main input file now. We delayed this from
23388 dwarf2out_init to avoid complications with PCH. */
23389 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23390 if (!IS_ABSOLUTE_PATH (filename))
23391 add_comp_dir_attribute (comp_unit_die ());
23392 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23394 bool p = false;
23395 htab_traverse (file_table, file_table_relative_p, &p);
23396 if (p)
23397 add_comp_dir_attribute (comp_unit_die ());
23400 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23402 add_location_or_const_value_attribute (
23403 VEC_index (deferred_locations, deferred_locations_list, i)->die,
23404 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23405 DW_AT_location);
23408 /* Traverse the limbo die list, and add parent/child links. The only
23409 dies without parents that should be here are concrete instances of
23410 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23411 For concrete instances, we can get the parent die from the abstract
23412 instance. */
23413 for (node = limbo_die_list; node; node = next_node)
23415 dw_die_ref die = node->die;
23416 next_node = node->next;
23418 if (die->die_parent == NULL)
23420 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23422 if (origin)
23423 add_child_die (origin->die_parent, die);
23424 else if (is_cu_die (die))
23426 else if (seen_error ())
23427 /* It's OK to be confused by errors in the input. */
23428 add_child_die (comp_unit_die (), die);
23429 else
23431 /* In certain situations, the lexical block containing a
23432 nested function can be optimized away, which results
23433 in the nested function die being orphaned. Likewise
23434 with the return type of that nested function. Force
23435 this to be a child of the containing function.
23437 It may happen that even the containing function got fully
23438 inlined and optimized out. In that case we are lost and
23439 assign the empty child. This should not be big issue as
23440 the function is likely unreachable too. */
23441 tree context = NULL_TREE;
23443 gcc_assert (node->created_for);
23445 if (DECL_P (node->created_for))
23446 context = DECL_CONTEXT (node->created_for);
23447 else if (TYPE_P (node->created_for))
23448 context = TYPE_CONTEXT (node->created_for);
23450 gcc_assert (context
23451 && (TREE_CODE (context) == FUNCTION_DECL
23452 || TREE_CODE (context) == NAMESPACE_DECL));
23454 origin = lookup_decl_die (context);
23455 if (origin)
23456 add_child_die (origin, die);
23457 else
23458 add_child_die (comp_unit_die (), die);
23463 limbo_die_list = NULL;
23465 resolve_addr (comp_unit_die ());
23467 for (node = deferred_asm_name; node; node = node->next)
23469 tree decl = node->created_for;
23470 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23472 add_linkage_attr (node->die, decl);
23473 move_linkage_attr (node->die);
23477 deferred_asm_name = NULL;
23479 /* Walk through the list of incomplete types again, trying once more to
23480 emit full debugging info for them. */
23481 retry_incomplete_types ();
23483 if (flag_eliminate_unused_debug_types)
23484 prune_unused_types ();
23486 /* Generate separate CUs for each of the include files we've seen.
23487 They will go into limbo_die_list. */
23488 if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23489 break_out_includes (comp_unit_die ());
23491 /* Generate separate COMDAT sections for type DIEs. */
23492 if (dwarf_version >= 4)
23494 break_out_comdat_types (comp_unit_die ());
23496 /* Each new type_unit DIE was added to the limbo die list when created.
23497 Since these have all been added to comdat_type_list, clear the
23498 limbo die list. */
23499 limbo_die_list = NULL;
23501 /* For each new comdat type unit, copy declarations for incomplete
23502 types to make the new unit self-contained (i.e., no direct
23503 references to the main compile unit). */
23504 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23505 copy_decls_for_unworthy_types (ctnode->root_die);
23506 copy_decls_for_unworthy_types (comp_unit_die ());
23508 /* In the process of copying declarations from one unit to another,
23509 we may have left some declarations behind that are no longer
23510 referenced. Prune them. */
23511 prune_unused_types ();
23514 /* Traverse the DIE's and add add sibling attributes to those DIE's
23515 that have children. */
23516 add_sibling_attributes (comp_unit_die ());
23517 for (node = limbo_die_list; node; node = node->next)
23518 add_sibling_attributes (node->die);
23519 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23520 add_sibling_attributes (ctnode->root_die);
23522 /* Output a terminator label for the .text section. */
23523 switch_to_section (text_section);
23524 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23525 if (cold_text_section)
23527 switch_to_section (cold_text_section);
23528 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23531 /* We can only use the low/high_pc attributes if all of the code was
23532 in .text. */
23533 if (!have_multiple_function_sections
23534 || (dwarf_version < 3 && dwarf_strict))
23536 add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23537 add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23540 else
23542 unsigned fde_idx = 0;
23543 bool range_list_added = false;
23545 /* We need to give .debug_loc and .debug_ranges an appropriate
23546 "base address". Use zero so that these addresses become
23547 absolute. Historically, we've emitted the unexpected
23548 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23549 Emit both to give time for other tools to adapt. */
23550 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23551 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23553 if (text_section_used)
23554 add_ranges_by_labels (comp_unit_die (), text_section_label,
23555 text_end_label, &range_list_added);
23556 if (flag_reorder_blocks_and_partition && cold_text_section_used)
23557 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23558 cold_end_label, &range_list_added);
23560 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23562 dw_fde_ref fde = &fde_table[fde_idx];
23564 if (fde->dw_fde_switched_sections)
23566 if (!fde->in_std_section)
23567 add_ranges_by_labels (comp_unit_die (),
23568 fde->dw_fde_hot_section_label,
23569 fde->dw_fde_hot_section_end_label,
23570 &range_list_added);
23571 if (!fde->cold_in_std_section)
23572 add_ranges_by_labels (comp_unit_die (),
23573 fde->dw_fde_unlikely_section_label,
23574 fde->dw_fde_unlikely_section_end_label,
23575 &range_list_added);
23577 else if (!fde->in_std_section)
23578 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23579 fde->dw_fde_end, &range_list_added);
23582 if (range_list_added)
23583 add_ranges (NULL);
23586 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23587 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23588 debug_line_section_label);
23590 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23591 add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23593 if (have_location_lists)
23594 optimize_location_lists (comp_unit_die ());
23596 /* Output all of the compilation units. We put the main one last so that
23597 the offsets are available to output_pubnames. */
23598 for (node = limbo_die_list; node; node = node->next)
23599 output_comp_unit (node->die, 0);
23601 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23602 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23604 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23606 /* Don't output duplicate types. */
23607 if (*slot != HTAB_EMPTY_ENTRY)
23608 continue;
23610 /* Add a pointer to the line table for the main compilation unit
23611 so that the debugger can make sense of DW_AT_decl_file
23612 attributes. */
23613 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23614 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23615 debug_line_section_label);
23617 output_comdat_type_unit (ctnode);
23618 *slot = ctnode;
23620 htab_delete (comdat_type_table);
23622 /* Output the main compilation unit if non-empty or if .debug_macinfo
23623 will be emitted. */
23624 output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23626 /* Output the abbreviation table. */
23627 switch_to_section (debug_abbrev_section);
23628 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23629 output_abbrev_section ();
23631 /* Output location list section if necessary. */
23632 if (have_location_lists)
23634 /* Output the location lists info. */
23635 switch_to_section (debug_loc_section);
23636 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23637 DEBUG_LOC_SECTION_LABEL, 0);
23638 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23639 output_location_lists (comp_unit_die ());
23642 /* Output public names table if necessary. */
23643 if (!VEC_empty (pubname_entry, pubname_table))
23645 gcc_assert (info_section_emitted);
23646 switch_to_section (debug_pubnames_section);
23647 output_pubnames (pubname_table);
23650 /* Output public types table if necessary. */
23651 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23652 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23653 simply won't look for the section. */
23654 if (!VEC_empty (pubname_entry, pubtype_table))
23656 bool empty = false;
23658 if (flag_eliminate_unused_debug_types)
23660 /* The pubtypes table might be emptied by pruning unused items. */
23661 unsigned i;
23662 pubname_ref p;
23663 empty = true;
23664 FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23665 if (p->die->die_offset != 0)
23667 empty = false;
23668 break;
23671 if (!empty)
23673 gcc_assert (info_section_emitted);
23674 switch_to_section (debug_pubtypes_section);
23675 output_pubnames (pubtype_table);
23679 /* Output the address range information. We only put functions in the arange
23680 table, so don't write it out if we don't have any. */
23681 if ((text_section_used || cold_text_section_used || arange_table_in_use)
23682 && info_section_emitted)
23684 switch_to_section (debug_aranges_section);
23685 output_aranges ();
23688 /* Output ranges section if necessary. */
23689 if (ranges_table_in_use)
23691 switch_to_section (debug_ranges_section);
23692 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23693 output_ranges ();
23696 /* Output the source line correspondence table. We must do this
23697 even if there is no line information. Otherwise, on an empty
23698 translation unit, we will generate a present, but empty,
23699 .debug_info section. IRIX 6.5 `nm' will then complain when
23700 examining the file. This is done late so that any filenames
23701 used by the debug_info section are marked as 'used'. */
23702 switch_to_section (debug_line_section);
23703 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23704 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23705 output_line_info ();
23707 /* Have to end the macro section. */
23708 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23710 switch_to_section (debug_macinfo_section);
23711 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23712 if (!VEC_empty (macinfo_entry, macinfo_table))
23713 output_macinfo ();
23714 dw2_asm_output_data (1, 0, "End compilation unit");
23717 /* If we emitted any DW_FORM_strp form attribute, output the string
23718 table too. */
23719 if (debug_str_hash)
23720 htab_traverse (debug_str_hash, output_indirect_string, NULL);
23723 #include "gt-dwarf2out.h"