]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dwarf2out.c
./:
[thirdparty/gcc.git] / gcc / dwarf2out.c
CommitLineData
bccafa26 1/* Output Dwarf2 format symbol table information from GCC.
07576557 2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
cfaf579d 3 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
0dbd1c74 4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
df78b73b 6 Extensively modified by Jason Merrill (jason@cygnus.com).
30ade641 7
f12b58b3 8This file is part of GCC.
30ade641 9
f12b58b3 10GCC is free software; you can redistribute it and/or modify it under
11the terms of the GNU General Public License as published by the Free
8c4c00c1 12Software Foundation; either version 3, or (at your option) any later
f12b58b3 13version.
30ade641 14
f12b58b3 15GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18for more details.
30ade641 19
20You should have received a copy of the GNU General Public License
8c4c00c1 21along with GCC; see the file COPYING3. If not see
22<http://www.gnu.org/licenses/>. */
30ade641 23
80b7bd06 24/* TODO: Emit .debug_line header even when there are no functions, since
5c65b85a 25 the file numbers are used by .debug_info. Alternately, leave
26 out locations for types and decls.
27 Avoid talking about ctors and op= for PODs.
28 Factor out common prologue sequences into multiple CIEs. */
29
8a8bfbe7 30/* The first part of this file deals with the DWARF 2 frame unwind
31 information, which is also used by the GCC efficient exception handling
32 mechanism. The second part, controlled only by an #ifdef
33 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34 information. */
35
7734155f 36/* DWARF2 Abbreviation Glossary:
37
38 CFA = Canonical Frame Address
39 a fixed address on the stack which identifies a call frame.
40 We define it to be the value of SP just before the call insn.
41 The CFA register and offset, which may change during the course
42 of the function, are used to calculate its value at runtime.
43
44 CFI = Call Frame Instruction
45 an instruction for the DWARF2 abstract machine
46
47 CIE = Common Information Entry
48 information describing information common to one or more FDEs
49
50 DIE = Debugging Information Entry
51
52 FDE = Frame Description Entry
53 information describing the stack call frame, in particular,
54 how to restore registers
55
56 DW_CFA_... = DWARF2 CFA call frame instruction
57 DW_TAG_... = DWARF2 DIE tag */
58
d757b8c9 59#include "config.h"
405711de 60#include "system.h"
805e22b2 61#include "coretypes.h"
62#include "tm.h"
30ade641 63#include "tree.h"
af225e7d 64#include "version.h"
30ade641 65#include "flags.h"
ef258422 66#include "real.h"
30ade641 67#include "rtl.h"
68#include "hard-reg-set.h"
69#include "regs.h"
70#include "insn-config.h"
71#include "reload.h"
df4b504c 72#include "function.h"
30ade641 73#include "output.h"
ec1e49cc 74#include "expr.h"
d8fc4d0b 75#include "libfuncs.h"
8a8bfbe7 76#include "except.h"
2c133160 77#include "dwarf2.h"
744d3441 78#include "dwarf2out.h"
ca98eb0a 79#include "dwarf2asm.h"
12874aaf 80#include "toplev.h"
eacbfaac 81#include "varray.h"
cff53614 82#include "ggc.h"
19f716e5 83#include "md5.h"
39697b37 84#include "tm_p.h"
a587b03b 85#include "diagnostic.h"
b896d81b 86#include "debug.h"
02c8b767 87#include "target.h"
d19bd1f0 88#include "langhooks.h"
51e8c210 89#include "hashtab.h"
5bd74231 90#include "cgraph.h"
2b49746a 91#include "input.h"
30ade641 92
f76df888 93#ifdef DWARF2_DEBUGGING_INFO
8ec3a57b 94static void dwarf2out_source_line (unsigned int, const char *);
f76df888 95#endif
96
34986748 97#ifndef DWARF2_FRAME_INFO
98# ifdef DWARF2_DEBUGGING_INFO
99# define DWARF2_FRAME_INFO \
100 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101# else
102# define DWARF2_FRAME_INFO 0
103# endif
104#endif
105
c98ee857 106/* Map register numbers held in the call frame info that gcc has
107 collected using DWARF_FRAME_REGNUM to those that should be output in
108 .debug_frame and .eh_frame. */
109#ifndef DWARF2_FRAME_REG_OUT
110#define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111#endif
112
c1203b1c 113/* Save the result of dwarf2out_do_frame across PCH. */
114static GTY(()) bool saved_do_cfi_asm = 0;
115
d757b8c9 116/* Decide whether we want to emit frame unwind information for the current
117 translation unit. */
118
119int
8ec3a57b 120dwarf2out_do_frame (void)
d757b8c9 121{
34986748 122 /* We want to emit correct CFA location expressions or lists, so we
123 have to return true if we're going to output debug info, even if
124 we're not going to output frame or unwind info. */
d757b8c9 125 return (write_symbols == DWARF2_DEBUG
8d60d2bc 126 || write_symbols == VMS_AND_DWARF2_DEBUG
c1203b1c 127 || DWARF2_FRAME_INFO || saved_do_cfi_asm
d757b8c9 128#ifdef DWARF2_UNWIND_INFO
34986748 129 || (DWARF2_UNWIND_INFO
130 && (flag_unwind_tables
131 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
d757b8c9 132#endif
133 );
134}
135
3dcd5df1 136/* Decide whether to emit frame unwind via assembler directives. */
137
138int
139dwarf2out_do_cfi_asm (void)
140{
141 int enc;
142
90a47a46 143#ifdef MIPS_DEBUGGING_INFO
144 return false;
145#endif
3dcd5df1 146 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
147 return false;
c1203b1c 148 if (saved_do_cfi_asm || !eh_personality_libfunc)
3dcd5df1 149 return true;
150 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
151 return false;
152
153 /* Make sure the personality encoding is one the assembler can support.
154 In particular, aligned addresses can't be handled. */
155 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
156 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157 return false;
158 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
159 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
160 return false;
161
c1203b1c 162 saved_do_cfi_asm = true;
3dcd5df1 163 return true;
164}
165
13c14f1c 166/* The size of the target's pointer type. */
167#ifndef PTR_SIZE
168#define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
169#endif
170
1f3233d1 171/* Array of RTXes referenced by the debugging information, which therefore
172 must be kept around forever. */
62aedc4c 173static GTY(()) VEC(rtx,gc) *used_rtx_array;
1f3233d1 174
175/* A pointer to the base of a list of incomplete types which might be
22230dd1 176 completed at some later time. incomplete_types_list needs to be a
177 VEC(tree,gc) because we want to tell the garbage collector about
178 it. */
179static GTY(()) VEC(tree,gc) *incomplete_types;
1f3233d1 180
181/* A pointer to the base of a table of references to declaration
182 scopes. This table is a display which tracks the nesting
183 of declaration scopes at the current scope and containing
184 scopes. This table is used to find the proper place to
185 define type declaration DIE's. */
4a940e75 186static GTY(()) VEC(tree,gc) *decl_scope_table;
1f3233d1 187
2f14b1f9 188/* Pointers to various DWARF2 sections. */
189static GTY(()) section *debug_info_section;
190static GTY(()) section *debug_abbrev_section;
191static GTY(()) section *debug_aranges_section;
192static GTY(()) section *debug_macinfo_section;
193static GTY(()) section *debug_line_section;
194static GTY(()) section *debug_loc_section;
195static GTY(()) section *debug_pubnames_section;
af84796a 196static GTY(()) section *debug_pubtypes_section;
2f14b1f9 197static GTY(()) section *debug_str_section;
198static GTY(()) section *debug_ranges_section;
d08d29c0 199static GTY(()) section *debug_frame_section;
2f14b1f9 200
e5530d32 201/* How to start an assembler comment. */
202#ifndef ASM_COMMENT_START
203#define ASM_COMMENT_START ";#"
204#endif
205
30ade641 206typedef struct dw_cfi_struct *dw_cfi_ref;
207typedef struct dw_fde_struct *dw_fde_ref;
208typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
30ade641 209
210/* Call frames are described using a sequence of Call Frame
211 Information instructions. The register number, offset
212 and address fields are provided as possible operands;
213 their use is selected by the opcode field. */
ec1e49cc 214
573aba85 215enum dw_cfi_oprnd_type {
216 dw_cfi_oprnd_unused,
217 dw_cfi_oprnd_reg_num,
218 dw_cfi_oprnd_offset,
219 dw_cfi_oprnd_addr,
220 dw_cfi_oprnd_loc
221};
222
fb1e4f4a 223typedef union GTY(()) dw_cfi_oprnd_struct {
da72c083 224 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
3d867824 225 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
573aba85 226 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
227 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
ec1e49cc 228}
30ade641 229dw_cfi_oprnd;
230
fb1e4f4a 231typedef struct GTY(()) dw_cfi_struct {
ec1e49cc 232 dw_cfi_ref dw_cfi_next;
233 enum dwarf_call_frame_info dw_cfi_opc;
8ec3a57b 234 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
573aba85 235 dw_cfi_oprnd1;
8ec3a57b 236 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
573aba85 237 dw_cfi_oprnd2;
ec1e49cc 238}
30ade641 239dw_cfi_node;
240
4b72e226 241/* This is how we define the location of the CFA. We use to handle it
242 as REG + OFFSET all the time, but now it can be more complex.
243 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
f80d1bcd 244 Instead of passing around REG and OFFSET, we pass a copy
4b72e226 245 of this structure. */
fb1e4f4a 246typedef struct GTY(()) cfa_loc {
3d867824 247 HOST_WIDE_INT offset;
248 HOST_WIDE_INT base_offset;
12d886b8 249 unsigned int reg;
4b72e226 250 int indirect; /* 1 if CFA is accessed via a dereference. */
251} dw_cfa_location;
252
30ade641 253/* All call frame descriptions (FDE's) in the GCC generated DWARF
752e49ca 254 refer to a single Common Information Entry (CIE), defined at
dae39efc 255 the beginning of the .debug_frame section. This use of a single
30ade641 256 CIE obviates the need to keep track of multiple CIE's
257 in the DWARF generation routines below. */
ec1e49cc 258
fb1e4f4a 259typedef struct GTY(()) dw_fde_struct {
2f9fc8ef 260 tree decl;
1e034a40 261 const char *dw_fde_begin;
262 const char *dw_fde_current_label;
263 const char *dw_fde_end;
1897b881 264 const char *dw_fde_hot_section_label;
265 const char *dw_fde_hot_section_end_label;
266 const char *dw_fde_unlikely_section_label;
267 const char *dw_fde_unlikely_section_end_label;
268 bool dw_fde_switched_sections;
ec1e49cc 269 dw_cfi_ref dw_fde_cfi;
df4b504c 270 unsigned funcdef_number;
27a7a23a 271 HOST_WIDE_INT stack_realignment;
272 /* Dynamic realign argument pointer register. */
273 unsigned int drap_reg;
274 /* Virtual dynamic realign argument pointer register. */
275 unsigned int vdrap_reg;
04396483 276 unsigned all_throwers_are_sibcalls : 1;
df4b504c 277 unsigned nothrow : 1;
278 unsigned uses_eh_lsda : 1;
27a7a23a 279 /* Whether we did stack realign in this call frame. */
280 unsigned stack_realign : 1;
281 /* Whether dynamic realign argument pointer register has been saved. */
282 unsigned drap_reg_saved: 1;
ec1e49cc 283}
30ade641 284dw_fde_node;
285
1e625a2e 286/* Maximum size (in bytes) of an artificially generated label. */
30ade641 287#define MAX_ARTIFICIAL_LABEL_BYTES 30
288
aaa408cd 289/* The size of addresses as they appear in the Dwarf 2 data.
290 Some architectures use word addresses to refer to code locations,
291 but Dwarf 2 info always uses byte addresses. On such machines,
292 Dwarf 2 addresses need to be larger than the architecture's
293 pointers. */
294#ifndef DWARF2_ADDR_SIZE
295#define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
296#endif
297
a3899bb7 298/* The size in bytes of a DWARF field indicating an offset or length
aaa408cd 299 relative to a debug info section, specified to be 4 bytes in the
300 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
b6ce7963 301 as PTR_SIZE. */
ec1e49cc 302
a3899bb7 303#ifndef DWARF_OFFSET_SIZE
304#define DWARF_OFFSET_SIZE 4
305#endif
306
65bdc57c 307/* According to the (draft) DWARF 3 specification, the initial length
308 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
309 bytes are 0xffffffff, followed by the length stored in the next 8
310 bytes.
311
312 However, the SGI/MIPS ABI uses an initial length which is equal to
313 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
314
315#ifndef DWARF_INITIAL_LENGTH_SIZE
316#define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
317#endif
318
be6eb971 319#define DWARF_VERSION 2
320
a3899bb7 321/* Round SIZE up to the nearest BOUNDARY. */
322#define DWARF_ROUND(SIZE,BOUNDARY) \
e711a040 323 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
30ade641 324
30ade641 325/* Offsets recorded in opcodes are a multiple of this alignment factor. */
78ac74b9 326#ifndef DWARF_CIE_DATA_ALIGNMENT
df78b73b 327#ifdef STACK_GROWS_DOWNWARD
7eb04d1c 328#define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
df78b73b 329#else
7eb04d1c 330#define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
df78b73b 331#endif
8c3f468d 332#endif
30ade641 333
04da8de9 334/* CIE identifier. */
335#if HOST_BITS_PER_WIDE_INT >= 64
336#define DWARF_CIE_ID \
337 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
338#else
339#define DWARF_CIE_ID DW_CIE_ID
340#endif
341
8a8bfbe7 342/* A pointer to the base of a table that contains frame description
343 information for each routine. */
573aba85 344static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
30ade641 345
8a8bfbe7 346/* Number of elements currently allocated for fde_table. */
909be935 347static GTY(()) unsigned fde_table_allocated;
6efd403b 348
8a8bfbe7 349/* Number of elements in fde_table currently in use. */
9105005a 350static GTY(()) unsigned fde_table_in_use;
30ade641 351
8a8bfbe7 352/* Size (in elements) of increments by which we may expand the
353 fde_table. */
354#define FDE_TABLE_INCREMENT 256
30ade641 355
a3f14923 356/* Get the current fde_table entry we should use. */
357
358static inline dw_fde_ref
359current_fde (void)
360{
361 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
362}
363
6efd403b 364/* A list of call frame insns for the CIE. */
573aba85 365static GTY(()) dw_cfi_ref cie_cfi_head;
6efd403b 366
38ac91bf 367#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
30ade641 368/* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
369 attribute that accelerates the lookup of the FDE associated
f80d1bcd 370 with the subprogram. This variable holds the table index of the FDE
30ade641 371 associated with the current function (body) definition. */
372static unsigned current_funcdef_fde;
38ac91bf 373#endif
30ade641 374
fb1e4f4a 375struct GTY(()) indirect_string_node {
573aba85 376 const char *str;
80b7bd06 377 unsigned int refcount;
bc620c5c 378 enum dwarf_form form;
80b7bd06 379 char *label;
380};
381
573aba85 382static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
383
384static GTY(()) int dw2_string_counter;
9105005a 385static GTY(()) unsigned long dwarf2out_cfi_label_num;
573aba85 386
af30c139 387/* True if the compilation unit places functions in more than one section. */
388static GTY(()) bool have_multiple_function_sections = false;
389
390/* Whether the default text and cold text sections have been used at all. */
391
392static GTY(()) bool text_section_used = false;
393static GTY(()) bool cold_text_section_used = false;
394
395/* The default cold text section. */
396static GTY(()) section *cold_text_section;
397
573aba85 398#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
399
30ade641 400/* Forward declarations for functions defined in this file. */
ec1e49cc 401
8ec3a57b 402static char *stripattributes (const char *);
403static const char *dwarf_cfi_name (unsigned);
404static dw_cfi_ref new_cfi (void);
405static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
406static void add_fde_cfi (const char *, dw_cfi_ref);
407static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
408static void lookup_cfa (dw_cfa_location *);
3d867824 409static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
bf780b7e 410#ifdef DWARF2_UNWIND_INFO
8ec3a57b 411static void initial_return_save (rtx);
bf780b7e 412#endif
2f87ccae 413static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
414 HOST_WIDE_INT);
8ec3a57b 415static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
fb39ff6e 416static void output_cfi_directive (dw_cfi_ref);
8ec3a57b 417static void output_call_frame_info (int);
af30c139 418static void dwarf2out_note_section_used (void);
535fcfa4 419static void dwarf2out_stack_adjust (rtx, bool);
fe3cf4b3 420static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
8ec3a57b 421static void flush_queued_reg_saves (void);
5493cb9a 422static bool clobbers_queued_reg_save (const_rtx);
8ec3a57b 423static void dwarf2out_frame_debug_expr (rtx, const char *);
30ade641 424
4b72e226 425/* Support for complex CFA locations. */
8ec3a57b 426static void output_cfa_loc (dw_cfi_ref);
fb39ff6e 427static void output_cfa_loc_raw (dw_cfi_ref);
8ec3a57b 428static void get_cfa_from_loc_descr (dw_cfa_location *,
429 struct dw_loc_descr_struct *);
4b72e226 430static struct dw_loc_descr_struct *build_cfa_loc
89fa767a 431 (dw_cfa_location *, HOST_WIDE_INT);
27a7a23a 432static struct dw_loc_descr_struct *build_cfa_aligned_loc
433 (HOST_WIDE_INT, HOST_WIDE_INT);
8ec3a57b 434static void def_cfa_1 (const char *, dw_cfa_location *);
4b72e226 435
ca98eb0a 436/* How to start an assembler comment. */
437#ifndef ASM_COMMENT_START
438#define ASM_COMMENT_START ";#"
30ade641 439#endif
440
a3899bb7 441/* Data and reference forms for relocatable data. */
442#define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
443#define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
444
702620e0 445#ifndef DEBUG_FRAME_SECTION
446#define DEBUG_FRAME_SECTION ".debug_frame"
30ade641 447#endif
30ade641 448
d58978a6 449#ifndef FUNC_BEGIN_LABEL
450#define FUNC_BEGIN_LABEL "LFB"
30ade641 451#endif
8c3f468d 452
d58978a6 453#ifndef FUNC_END_LABEL
454#define FUNC_END_LABEL "LFE"
30ade641 455#endif
8c3f468d 456
2f9fc8ef 457#ifndef FRAME_BEGIN_LABEL
48ead6eb 458#define FRAME_BEGIN_LABEL "Lframe"
2f9fc8ef 459#endif
19bce576 460#define CIE_AFTER_SIZE_LABEL "LSCIE"
461#define CIE_END_LABEL "LECIE"
ca98eb0a 462#define FDE_LABEL "LSFDE"
463#define FDE_AFTER_SIZE_LABEL "LASFDE"
19bce576 464#define FDE_END_LABEL "LEFDE"
3740694f 465#define LINE_NUMBER_BEGIN_LABEL "LSLT"
466#define LINE_NUMBER_END_LABEL "LELT"
467#define LN_PROLOG_AS_LABEL "LASLTP"
468#define LN_PROLOG_END_LABEL "LELTP"
19f716e5 469#define DIE_LABEL_PREFIX "DW"
30ade641 470
212538c2 471/* The DWARF 2 CFA column which tracks the return address. Normally this
6efd403b 472 is the column for PC, or the first column after all of the hard
473 registers. */
212538c2 474#ifndef DWARF_FRAME_RETURN_COLUMN
6efd403b 475#ifdef PC_REGNUM
8ec3a57b 476#define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
6efd403b 477#else
8ec3a57b 478#define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
6efd403b 479#endif
212538c2 480#endif
481
482/* The mapping from gcc register number to DWARF 2 CFA column number. By
df78b73b 483 default, we just provide columns for all registers. */
212538c2 484#ifndef DWARF_FRAME_REGNUM
df78b73b 485#define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
212538c2 486#endif
8c3f468d 487\f
d757b8c9 488/* Hook used by __throw. */
489
490rtx
8ec3a57b 491expand_builtin_dwarf_sp_column (void)
d757b8c9 492{
963e1d38 493 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
c98ee857 494 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
d757b8c9 495}
496
ec1e49cc 497/* Return a pointer to a copy of the section string name S with all
1bfb8e27 498 attributes stripped off, and an asterisk prepended (for assemble_name). */
ec1e49cc 499
500static inline char *
8ec3a57b 501stripattributes (const char *s)
30ade641 502{
4c36ffe6 503 char *stripped = XNEWVEC (char, strlen (s) + 2);
ec1e49cc 504 char *p = stripped;
505
1bfb8e27 506 *p++ = '*';
507
508 while (*s && *s != ',')
509 *p++ = *s++;
ec1e49cc 510
30ade641 511 *p = '\0';
512 return stripped;
513}
514
dd5e1e90 515/* MEM is a memory reference for the register size table, each element of
516 which has mode MODE. Initialize column C as a return address column. */
517
518static void
519init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
520{
521 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
522 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
523 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
524}
525
695e919b 526/* Generate code to initialize the register size table. */
5ff00a1d 527
695e919b 528void
8ec3a57b 529expand_builtin_init_dwarf_reg_sizes (tree address)
5ff00a1d 530{
963e1d38 531 unsigned int i;
695e919b 532 enum machine_mode mode = TYPE_MODE (char_type_node);
8ec3c5c2 533 rtx addr = expand_normal (address);
8c3f468d 534 rtx mem = gen_rtx_MEM (BLKmode, addr);
5fec5f34 535 bool wrote_return_column = false;
5ff00a1d 536
33f90206 537 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
c98ee857 538 {
539 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
61a9389f 540
c98ee857 541 if (rnum < DWARF_FRAME_REGISTERS)
542 {
543 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
544 enum machine_mode save_mode = reg_raw_mode[i];
545 HOST_WIDE_INT size;
61a9389f 546
c98ee857 547 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
548 save_mode = choose_hard_reg_mode (i, 1, true);
549 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
550 {
551 if (save_mode == VOIDmode)
552 continue;
553 wrote_return_column = true;
554 }
555 size = GET_MODE_SIZE (save_mode);
556 if (offset < 0)
557 continue;
61a9389f 558
c98ee857 559 emit_move_insn (adjust_address (mem, mode, offset),
560 gen_int_mode (size, mode));
561 }
562 }
c49ad9ef 563
dd5e1e90 564 if (!wrote_return_column)
565 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
566
c49ad9ef 567#ifdef DWARF_ALT_FRAME_RETURN_COLUMN
dd5e1e90 568 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
c49ad9ef 569#endif
114a8a4b 570
571 targetm.init_dwarf_reg_sizes_extra (address);
5ff00a1d 572}
573
8a8bfbe7 574/* Convert a DWARF call frame info. operation to its string name */
30ade641 575
7795e5d1 576static const char *
8ec3a57b 577dwarf_cfi_name (unsigned int cfi_opc)
8a8bfbe7 578{
579 switch (cfi_opc)
580 {
581 case DW_CFA_advance_loc:
582 return "DW_CFA_advance_loc";
583 case DW_CFA_offset:
584 return "DW_CFA_offset";
585 case DW_CFA_restore:
586 return "DW_CFA_restore";
587 case DW_CFA_nop:
588 return "DW_CFA_nop";
589 case DW_CFA_set_loc:
590 return "DW_CFA_set_loc";
591 case DW_CFA_advance_loc1:
592 return "DW_CFA_advance_loc1";
593 case DW_CFA_advance_loc2:
594 return "DW_CFA_advance_loc2";
595 case DW_CFA_advance_loc4:
596 return "DW_CFA_advance_loc4";
597 case DW_CFA_offset_extended:
598 return "DW_CFA_offset_extended";
599 case DW_CFA_restore_extended:
600 return "DW_CFA_restore_extended";
601 case DW_CFA_undefined:
602 return "DW_CFA_undefined";
603 case DW_CFA_same_value:
604 return "DW_CFA_same_value";
605 case DW_CFA_register:
606 return "DW_CFA_register";
607 case DW_CFA_remember_state:
608 return "DW_CFA_remember_state";
609 case DW_CFA_restore_state:
610 return "DW_CFA_restore_state";
611 case DW_CFA_def_cfa:
612 return "DW_CFA_def_cfa";
613 case DW_CFA_def_cfa_register:
614 return "DW_CFA_def_cfa_register";
615 case DW_CFA_def_cfa_offset:
616 return "DW_CFA_def_cfa_offset";
15a56411 617
618 /* DWARF 3 */
4b72e226 619 case DW_CFA_def_cfa_expression:
620 return "DW_CFA_def_cfa_expression";
15a56411 621 case DW_CFA_expression:
622 return "DW_CFA_expression";
623 case DW_CFA_offset_extended_sf:
624 return "DW_CFA_offset_extended_sf";
625 case DW_CFA_def_cfa_sf:
626 return "DW_CFA_def_cfa_sf";
627 case DW_CFA_def_cfa_offset_sf:
628 return "DW_CFA_def_cfa_offset_sf";
4ad3f9b3 629
8a8bfbe7 630 /* SGI/MIPS specific */
631 case DW_CFA_MIPS_advance_loc8:
632 return "DW_CFA_MIPS_advance_loc8";
4ad3f9b3 633
634 /* GNU extensions */
635 case DW_CFA_GNU_window_save:
636 return "DW_CFA_GNU_window_save";
d757b8c9 637 case DW_CFA_GNU_args_size:
638 return "DW_CFA_GNU_args_size";
db3d4a18 639 case DW_CFA_GNU_negative_offset_extended:
640 return "DW_CFA_GNU_negative_offset_extended";
4ad3f9b3 641
8a8bfbe7 642 default:
643 return "DW_CFA_<unknown>";
644 }
645}
30ade641 646
8a8bfbe7 647/* Return a pointer to a newly allocated Call Frame Instruction. */
ec1e49cc 648
8a8bfbe7 649static inline dw_cfi_ref
8ec3a57b 650new_cfi (void)
8a8bfbe7 651{
2457c754 652 dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
ec1e49cc 653
8a8bfbe7 654 cfi->dw_cfi_next = NULL;
655 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
656 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
30ade641 657
8a8bfbe7 658 return cfi;
659}
30ade641 660
8a8bfbe7 661/* Add a Call Frame Instruction to list of instructions. */
30ade641 662
8a8bfbe7 663static inline void
8ec3a57b 664add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
8a8bfbe7 665{
19cb6b50 666 dw_cfi_ref *p;
27a7a23a 667 dw_fde_ref fde = current_fde ();
668
669 /* When DRAP is used, CFA is defined with an expression. Redefine
670 CFA may lead to a different CFA value. */
671 if (fde && fde->drap_reg != INVALID_REGNUM)
672 switch (cfi->dw_cfi_opc)
673 {
674 case DW_CFA_def_cfa_register:
675 case DW_CFA_def_cfa_offset:
676 case DW_CFA_def_cfa_offset_sf:
677 case DW_CFA_def_cfa:
678 case DW_CFA_def_cfa_sf:
679 gcc_unreachable ();
680
681 default:
682 break;
683 }
30ade641 684
8a8bfbe7 685 /* Find the end of the chain. */
686 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
687 ;
688
689 *p = cfi;
30ade641 690}
691
8a8bfbe7 692/* Generate a new label for the CFI info to refer to. */
ec1e49cc 693
4ad3f9b3 694char *
8ec3a57b 695dwarf2out_cfi_label (void)
30ade641 696{
8a8bfbe7 697 static char label[20];
f80d1bcd 698
3dcd5df1 699 if (dwarf2out_do_cfi_asm ())
fb39ff6e 700 {
701 /* In this case, we will be emitting the asm directive instead of
702 the label, so just return a placeholder to keep the rest of the
703 interfaces happy. */
704 strcpy (label, "<do not output>");
705 }
706 else
707 {
708 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
709 ASM_OUTPUT_LABEL (asm_out_file, label);
710 }
711
8a8bfbe7 712 return label;
30ade641 713}
714
8a8bfbe7 715/* Add CFI to the current fde at the PC value indicated by LABEL if specified,
716 or to the CIE if LABEL is NULL. */
ec1e49cc 717
8a8bfbe7 718static void
8ec3a57b 719add_fde_cfi (const char *label, dw_cfi_ref cfi)
30ade641 720{
fb39ff6e 721 dw_cfi_ref *list_head = &cie_cfi_head;
722
3dcd5df1 723 if (dwarf2out_do_cfi_asm ())
fb39ff6e 724 {
725 if (label)
726 {
727 output_cfi_directive (cfi);
728
729 /* We still have to add the cfi to the list so that
730 lookup_cfa works later on. */
731 list_head = &current_fde ()->dw_fde_cfi;
732 }
733 /* ??? If this is a CFI for the CIE, we don't emit. This
734 assumes that the standard CIE contents that the assembler
735 uses matches the standard CIE contents that the compiler
736 uses. This is probably a bad assumption. I'm not quite
737 sure how to address this for now. */
738 }
739 else if (label)
8a8bfbe7 740 {
c0fd44c1 741 dw_fde_ref fde = current_fde ();
742
743 gcc_assert (fde != NULL);
30ade641 744
8a8bfbe7 745 if (*label == 0)
746 label = dwarf2out_cfi_label ();
ec1e49cc 747
8a8bfbe7 748 if (fde->dw_fde_current_label == NULL
749 || strcmp (label, fde->dw_fde_current_label) != 0)
750 {
19cb6b50 751 dw_cfi_ref xcfi;
30ade641 752
d8eb7025 753 label = xstrdup (label);
ec1e49cc 754
8a8bfbe7 755 /* Set the location counter to the new label. */
756 xcfi = new_cfi ();
d8eb7025 757 /* If we have a current label, advance from there, otherwise
758 set the location directly using set_loc. */
759 xcfi->dw_cfi_opc = fde->dw_fde_current_label
760 ? DW_CFA_advance_loc4
761 : DW_CFA_set_loc;
8a8bfbe7 762 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
763 add_cfi (&fde->dw_fde_cfi, xcfi);
d8eb7025 764
765 fde->dw_fde_current_label = label;
8a8bfbe7 766 }
ec1e49cc 767
fb39ff6e 768 list_head = &fde->dw_fde_cfi;
8a8bfbe7 769 }
770
fb39ff6e 771 add_cfi (list_head, cfi);
30ade641 772}
773
8a8bfbe7 774/* Subroutine of lookup_cfa. */
ec1e49cc 775
12d886b8 776static void
8ec3a57b 777lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
30ade641 778{
8a8bfbe7 779 switch (cfi->dw_cfi_opc)
780 {
781 case DW_CFA_def_cfa_offset:
da72c083 782 case DW_CFA_def_cfa_offset_sf:
be30e9d9 783 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
da72c083 784 break;
8a8bfbe7 785 case DW_CFA_def_cfa_register:
4b72e226 786 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
8a8bfbe7 787 break;
788 case DW_CFA_def_cfa:
da72c083 789 case DW_CFA_def_cfa_sf:
790 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
be30e9d9 791 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
da72c083 792 break;
4b72e226 793 case DW_CFA_def_cfa_expression:
794 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
8a8bfbe7 795 break;
0dbd1c74 796 default:
797 break;
8a8bfbe7 798 }
30ade641 799}
800
8a8bfbe7 801/* Find the previous value for the CFA. */
ec1e49cc 802
8a8bfbe7 803static void
8ec3a57b 804lookup_cfa (dw_cfa_location *loc)
30ade641 805{
19cb6b50 806 dw_cfi_ref cfi;
c0fd44c1 807 dw_fde_ref fde;
8a8bfbe7 808
12d886b8 809 loc->reg = INVALID_REGNUM;
4b72e226 810 loc->offset = 0;
811 loc->indirect = 0;
812 loc->base_offset = 0;
8a8bfbe7 813
814 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
4b72e226 815 lookup_cfa_1 (cfi, loc);
8a8bfbe7 816
c0fd44c1 817 fde = current_fde ();
818 if (fde)
819 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
820 lookup_cfa_1 (cfi, loc);
30ade641 821}
822
8a8bfbe7 823/* The current rule for calculating the DWARF2 canonical frame address. */
b0d72d68 824static dw_cfa_location cfa;
ec1e49cc 825
8a8bfbe7 826/* The register used for saving registers to the stack, and its offset
827 from the CFA. */
b0d72d68 828static dw_cfa_location cfa_store;
8a8bfbe7 829
d757b8c9 830/* The running total of the size of arguments pushed onto the stack. */
3d867824 831static HOST_WIDE_INT args_size;
d757b8c9 832
08532d4f 833/* The last args_size we actually output. */
3d867824 834static HOST_WIDE_INT old_args_size;
08532d4f 835
8a8bfbe7 836/* Entry point to update the canonical frame address (CFA).
837 LABEL is passed to add_fde_cfi. The value of CFA is now to be
838 calculated from REG+OFFSET. */
839
840void
3d867824 841dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
4b72e226 842{
843 dw_cfa_location loc;
844 loc.indirect = 0;
845 loc.base_offset = 0;
846 loc.reg = reg;
847 loc.offset = offset;
848 def_cfa_1 (label, &loc);
849}
850
12d886b8 851/* Determine if two dw_cfa_location structures define the same data. */
852
853static bool
854cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
855{
856 return (loc1->reg == loc2->reg
857 && loc1->offset == loc2->offset
858 && loc1->indirect == loc2->indirect
859 && (loc1->indirect == 0
860 || loc1->base_offset == loc2->base_offset));
861}
862
950ae8fe 863/* This routine does the actual work. The CFA is now calculated from
4b72e226 864 the dw_cfa_location structure. */
8c3f468d 865
4b72e226 866static void
8ec3a57b 867def_cfa_1 (const char *label, dw_cfa_location *loc_p)
30ade641 868{
19cb6b50 869 dw_cfi_ref cfi;
4b72e226 870 dw_cfa_location old_cfa, loc;
8a8bfbe7 871
4b72e226 872 cfa = *loc_p;
873 loc = *loc_p;
8ab7f849 874
4b72e226 875 if (cfa_store.reg == loc.reg && loc.indirect == 0)
876 cfa_store.offset = loc.offset;
8a8bfbe7 877
4b72e226 878 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
879 lookup_cfa (&old_cfa);
880
8c3f468d 881 /* If nothing changed, no need to issue any call frame instructions. */
12d886b8 882 if (cfa_equal_p (&loc, &old_cfa))
8c3f468d 883 return;
8a8bfbe7 884
885 cfi = new_cfi ();
886
49a9983c 887 if (loc.reg == old_cfa.reg && !loc.indirect)
30ade641 888 {
da72c083 889 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
be30e9d9 890 the CFA register did not change but the offset did. The data
891 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
892 in the assembler via the .cfi_def_cfa_offset directive. */
da72c083 893 if (loc.offset < 0)
be30e9d9 894 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
da72c083 895 else
be30e9d9 896 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
897 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
8a8bfbe7 898 }
30ade641 899
8a8bfbe7 900#ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
12d886b8 901 else if (loc.offset == old_cfa.offset
902 && old_cfa.reg != INVALID_REGNUM
49a9983c 903 && !loc.indirect)
8a8bfbe7 904 {
950ae8fe 905 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
906 indicating the CFA register has changed to <register> but the
907 offset has not changed. */
8a8bfbe7 908 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
4b72e226 909 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
8a8bfbe7 910 }
911#endif
30ade641 912
4b72e226 913 else if (loc.indirect == 0)
8a8bfbe7 914 {
950ae8fe 915 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
916 indicating the CFA register has changed to <register> with
be30e9d9 917 the specified offset. The data factoring for DW_CFA_def_cfa_sf
918 happens in output_cfi, or in the assembler via the .cfi_def_cfa
919 directive. */
da72c083 920 if (loc.offset < 0)
be30e9d9 921 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
da72c083 922 else
be30e9d9 923 cfi->dw_cfi_opc = DW_CFA_def_cfa;
924 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
925 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
4b72e226 926 }
927 else
928 {
950ae8fe 929 /* Construct a DW_CFA_def_cfa_expression instruction to
930 calculate the CFA using a full location expression since no
931 register-offset pair is available. */
f80d1bcd 932 struct dw_loc_descr_struct *loc_list;
8c3f468d 933
4b72e226 934 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
89fa767a 935 loc_list = build_cfa_loc (&loc, 0);
4b72e226 936 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
30ade641 937 }
8a8bfbe7 938
939 add_fde_cfi (label, cfi);
30ade641 940}
941
8a8bfbe7 942/* Add the CFI for saving a register. REG is the CFA column number.
943 LABEL is passed to add_fde_cfi.
944 If SREG is -1, the register is saved at OFFSET from the CFA;
945 otherwise it is saved in SREG. */
ec1e49cc 946
8a8bfbe7 947static void
3d867824 948reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
30ade641 949{
19cb6b50 950 dw_cfi_ref cfi = new_cfi ();
27a7a23a 951 dw_fde_ref fde = current_fde ();
8a8bfbe7 952
953 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
954
27a7a23a 955 /* When stack is aligned, store REG using DW_CFA_expression with
956 FP. */
957 if (fde
958 && fde->stack_realign
959 && sreg == INVALID_REGNUM)
960 {
961 cfi->dw_cfi_opc = DW_CFA_expression;
962 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
963 cfi->dw_cfi_oprnd1.dw_cfi_loc
964 = build_cfa_aligned_loc (offset, fde->stack_realignment);
965 }
966 else if (sreg == INVALID_REGNUM)
30ade641 967 {
be30e9d9 968 if (offset < 0)
969 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
970 else if (reg & ~0x3f)
8a8bfbe7 971 cfi->dw_cfi_opc = DW_CFA_offset_extended;
972 else
973 cfi->dw_cfi_opc = DW_CFA_offset;
8a8bfbe7 974 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
975 }
220d204b 976 else if (sreg == reg)
60ea93bb 977 cfi->dw_cfi_opc = DW_CFA_same_value;
8a8bfbe7 978 else
979 {
980 cfi->dw_cfi_opc = DW_CFA_register;
981 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
982 }
983
984 add_fde_cfi (label, cfi);
985}
986
4ad3f9b3 987/* Add the CFI for saving a register window. LABEL is passed to reg_save.
988 This CFI tells the unwinder that it needs to restore the window registers
989 from the previous frame's window save area.
f80d1bcd 990
4ad3f9b3 991 ??? Perhaps we should note in the CIE where windows are saved (instead of
992 assuming 0(cfa)) and what registers are in the window. */
993
994void
8ec3a57b 995dwarf2out_window_save (const char *label)
4ad3f9b3 996{
19cb6b50 997 dw_cfi_ref cfi = new_cfi ();
8c3f468d 998
4ad3f9b3 999 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1000 add_fde_cfi (label, cfi);
1001}
1002
d757b8c9 1003/* Add a CFI to update the running total of the size of arguments
1004 pushed onto the stack. */
1005
1006void
3d867824 1007dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
d757b8c9 1008{
19cb6b50 1009 dw_cfi_ref cfi;
08532d4f 1010
1011 if (size == old_args_size)
1012 return;
8c3f468d 1013
08532d4f 1014 old_args_size = size;
1015
1016 cfi = new_cfi ();
d757b8c9 1017 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1018 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1019 add_fde_cfi (label, cfi);
1020}
1021
4ad3f9b3 1022/* Entry point for saving a register to the stack. REG is the GCC register
1023 number. LABEL and OFFSET are passed to reg_save. */
8a8bfbe7 1024
1025void
3d867824 1026dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
8a8bfbe7 1027{
f481766a 1028 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
8a8bfbe7 1029}
1030
4ad3f9b3 1031/* Entry point for saving the return address in the stack.
1032 LABEL and OFFSET are passed to reg_save. */
1033
1034void
3d867824 1035dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
4ad3f9b3 1036{
f481766a 1037 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
4ad3f9b3 1038}
1039
1040/* Entry point for saving the return address in a register.
1041 LABEL and SREG are passed to reg_save. */
1042
1043void
8ec3a57b 1044dwarf2out_return_reg (const char *label, unsigned int sreg)
4ad3f9b3 1045{
f481766a 1046 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
4ad3f9b3 1047}
1048
bf780b7e 1049#ifdef DWARF2_UNWIND_INFO
8a8bfbe7 1050/* Record the initial position of the return address. RTL is
1051 INCOMING_RETURN_ADDR_RTX. */
1052
1053static void
8ec3a57b 1054initial_return_save (rtx rtl)
8a8bfbe7 1055{
f481766a 1056 unsigned int reg = INVALID_REGNUM;
8c3f468d 1057 HOST_WIDE_INT offset = 0;
8a8bfbe7 1058
1059 switch (GET_CODE (rtl))
1060 {
1061 case REG:
1062 /* RA is in a register. */
220d204b 1063 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
8a8bfbe7 1064 break;
8c3f468d 1065
8a8bfbe7 1066 case MEM:
1067 /* RA is on the stack. */
1068 rtl = XEXP (rtl, 0);
1069 switch (GET_CODE (rtl))
1070 {
1071 case REG:
7bd4f6b6 1072 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
8a8bfbe7 1073 offset = 0;
1074 break;
8c3f468d 1075
8a8bfbe7 1076 case PLUS:
7bd4f6b6 1077 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
8a8bfbe7 1078 offset = INTVAL (XEXP (rtl, 1));
1079 break;
8c3f468d 1080
8a8bfbe7 1081 case MINUS:
7bd4f6b6 1082 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
8a8bfbe7 1083 offset = -INTVAL (XEXP (rtl, 1));
1084 break;
8c3f468d 1085
8a8bfbe7 1086 default:
7bd4f6b6 1087 gcc_unreachable ();
8a8bfbe7 1088 }
8c3f468d 1089
8a8bfbe7 1090 break;
8c3f468d 1091
4ad3f9b3 1092 case PLUS:
1093 /* The return address is at some offset from any value we can
1094 actually load. For instance, on the SPARC it is in %i7+8. Just
1095 ignore the offset for now; it doesn't matter for unwinding frames. */
7bd4f6b6 1096 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
4ad3f9b3 1097 initial_return_save (XEXP (rtl, 0));
1098 return;
8c3f468d 1099
30ade641 1100 default:
7bd4f6b6 1101 gcc_unreachable ();
30ade641 1102 }
8a8bfbe7 1103
60ea93bb 1104 if (reg != DWARF_FRAME_RETURN_COLUMN)
1105 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
30ade641 1106}
bf780b7e 1107#endif
30ade641 1108
6ee89c56 1109/* Given a SET, calculate the amount of stack adjustment it
6312a35e 1110 contains. */
6ee89c56 1111
3d867824 1112static HOST_WIDE_INT
2f87ccae 1113stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1114 HOST_WIDE_INT cur_offset)
6ee89c56 1115{
5493cb9a 1116 const_rtx src = SET_SRC (pattern);
1117 const_rtx dest = SET_DEST (pattern);
8c3f468d 1118 HOST_WIDE_INT offset = 0;
6ee89c56 1119 enum rtx_code code;
1120
1121 if (dest == stack_pointer_rtx)
1122 {
6ee89c56 1123 code = GET_CODE (src);
2f87ccae 1124
1125 /* Assume (set (reg sp) (reg whatever)) sets args_size
1126 level to 0. */
1127 if (code == REG && src != stack_pointer_rtx)
1128 {
1129 offset = -cur_args_size;
1130#ifndef STACK_GROWS_DOWNWARD
1131 offset = -offset;
1132#endif
1133 return offset - cur_offset;
1134 }
1135
6ee89c56 1136 if (! (code == PLUS || code == MINUS)
1137 || XEXP (src, 0) != stack_pointer_rtx
1138 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1139 return 0;
1140
2f87ccae 1141 /* (set (reg sp) (plus (reg sp) (const_int))) */
6ee89c56 1142 offset = INTVAL (XEXP (src, 1));
052c7a5c 1143 if (code == PLUS)
1144 offset = -offset;
2f87ccae 1145 return offset;
6ee89c56 1146 }
2f87ccae 1147
1148 if (MEM_P (src) && !MEM_P (dest))
1149 dest = src;
1150 if (MEM_P (dest))
6ee89c56 1151 {
1152 /* (set (mem (pre_dec (reg sp))) (foo)) */
1153 src = XEXP (dest, 0);
1154 code = GET_CODE (src);
1155
bc70bd5e 1156 switch (code)
1157 {
052c7a5c 1158 case PRE_MODIFY:
1159 case POST_MODIFY:
1160 if (XEXP (src, 0) == stack_pointer_rtx)
1161 {
1162 rtx val = XEXP (XEXP (src, 1), 1);
1163 /* We handle only adjustments by constant amount. */
7bd4f6b6 1164 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1165 && GET_CODE (val) == CONST_INT);
052c7a5c 1166 offset = -INTVAL (val);
1167 break;
1168 }
1169 return 0;
1170
1171 case PRE_DEC:
1172 case POST_DEC:
1173 if (XEXP (src, 0) == stack_pointer_rtx)
1174 {
1175 offset = GET_MODE_SIZE (GET_MODE (dest));
1176 break;
1177 }
1178 return 0;
1179
1180 case PRE_INC:
1181 case POST_INC:
1182 if (XEXP (src, 0) == stack_pointer_rtx)
1183 {
1184 offset = -GET_MODE_SIZE (GET_MODE (dest));
1185 break;
1186 }
1187 return 0;
8c3f468d 1188
052c7a5c 1189 default:
1190 return 0;
93fbe1f3 1191 }
6ee89c56 1192 }
1193 else
1194 return 0;
1195
6ee89c56 1196 return offset;
1197}
1198
9396ae97 1199/* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1200 indexed by INSN_UID. */
1201
1202static HOST_WIDE_INT *barrier_args_size;
1203
1204/* Helper function for compute_barrier_args_size. Handle one insn. */
1205
1206static HOST_WIDE_INT
1207compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1208 VEC (rtx, heap) **next)
1209{
1210 HOST_WIDE_INT offset = 0;
1211 int i;
1212
1213 if (! RTX_FRAME_RELATED_P (insn))
1214 {
1215 if (prologue_epilogue_contains (insn)
1216 || sibcall_epilogue_contains (insn))
1217 /* Nothing */;
1218 else if (GET_CODE (PATTERN (insn)) == SET)
2f87ccae 1219 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
9396ae97 1220 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1221 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1222 {
1223 /* There may be stack adjustments inside compound insns. Search
1224 for them. */
1225 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1226 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
2f87ccae 1227 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1228 cur_args_size, offset);
9396ae97 1229 }
1230 }
1231 else
1232 {
1233 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1234
1235 if (expr)
1236 {
1237 expr = XEXP (expr, 0);
1238 if (GET_CODE (expr) == PARALLEL
1239 || GET_CODE (expr) == SEQUENCE)
1240 for (i = 1; i < XVECLEN (expr, 0); i++)
1241 {
1242 rtx elem = XVECEXP (expr, 0, i);
1243
1244 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
2f87ccae 1245 offset += stack_adjust_offset (elem, cur_args_size, offset);
9396ae97 1246 }
1247 }
1248 }
1249
1250#ifndef STACK_GROWS_DOWNWARD
1251 offset = -offset;
1252#endif
1253
1254 cur_args_size += offset;
1255 if (cur_args_size < 0)
1256 cur_args_size = 0;
1257
1258 if (JUMP_P (insn))
1259 {
1260 rtx dest = JUMP_LABEL (insn);
1261
1262 if (dest)
1263 {
1264 if (barrier_args_size [INSN_UID (dest)] < 0)
1265 {
1266 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1267 VEC_safe_push (rtx, heap, *next, dest);
1268 }
9396ae97 1269 }
1270 }
1271
1272 return cur_args_size;
1273}
1274
1275/* Walk the whole function and compute args_size on BARRIERs. */
1276
1277static void
1278compute_barrier_args_size (void)
1279{
1280 int max_uid = get_max_uid (), i;
1281 rtx insn;
1282 VEC (rtx, heap) *worklist, *next, *tmp;
1283
1284 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1285 for (i = 0; i < max_uid; i++)
1286 barrier_args_size[i] = -1;
1287
1288 worklist = VEC_alloc (rtx, heap, 20);
1289 next = VEC_alloc (rtx, heap, 20);
1290 insn = get_insns ();
1291 barrier_args_size[INSN_UID (insn)] = 0;
1292 VEC_quick_push (rtx, worklist, insn);
1293 for (;;)
1294 {
1295 while (!VEC_empty (rtx, worklist))
1296 {
8aa5820b 1297 rtx prev, body, first_insn;
9396ae97 1298 HOST_WIDE_INT cur_args_size;
1299
8aa5820b 1300 first_insn = insn = VEC_pop (rtx, worklist);
9396ae97 1301 cur_args_size = barrier_args_size[INSN_UID (insn)];
1302 prev = prev_nonnote_insn (insn);
1303 if (prev && BARRIER_P (prev))
1304 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1305
1306 for (; insn; insn = NEXT_INSN (insn))
1307 {
1308 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1309 continue;
1310 if (BARRIER_P (insn))
1311 break;
1312
1313 if (LABEL_P (insn))
1314 {
8aa5820b 1315 if (insn == first_insn)
1316 continue;
1317 else if (barrier_args_size[INSN_UID (insn)] < 0)
1318 {
1319 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1320 continue;
1321 }
1322 else
1323 {
1324 /* The insns starting with this label have been
1325 already scanned or are in the worklist. */
8aa5820b 1326 break;
1327 }
9396ae97 1328 }
1329
1330 body = PATTERN (insn);
1331 if (GET_CODE (body) == SEQUENCE)
1332 {
2f87ccae 1333 HOST_WIDE_INT dest_args_size = cur_args_size;
9396ae97 1334 for (i = 1; i < XVECLEN (body, 0); i++)
2f87ccae 1335 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1336 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1337 dest_args_size
1338 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1339 dest_args_size, &next);
1340 else
1341 cur_args_size
1342 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1343 cur_args_size, &next);
1344
1345 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1346 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1347 dest_args_size, &next);
1348 else
9396ae97 1349 cur_args_size
2f87ccae 1350 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
9396ae97 1351 cur_args_size, &next);
9396ae97 1352 }
1353 else
1354 cur_args_size
1355 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1356 }
1357 }
1358
1359 if (VEC_empty (rtx, next))
1360 break;
1361
1362 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1363 tmp = next;
1364 next = worklist;
1365 worklist = tmp;
1366 VEC_truncate (rtx, next, 0);
1367 }
1368
1369 VEC_free (rtx, heap, worklist);
1370 VEC_free (rtx, heap, next);
1371}
1372
1373
d757b8c9 1374/* Check INSN to see if it looks like a push or a stack adjustment, and
1375 make a note of it if it does. EH uses this information to find out how
1376 much extra space it needs to pop off the stack. */
1377
1378static void
46b2b3c8 1379dwarf2out_stack_adjust (rtx insn, bool after_p)
d757b8c9 1380{
8c3f468d 1381 HOST_WIDE_INT offset;
1e034a40 1382 const char *label;
8c3f468d 1383 int i;
d757b8c9 1384
31b1fbc5 1385 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1386 with this function. Proper support would require all frame-related
1387 insns to be marked, and to be able to handle saving state around
1388 epilogues textually in the middle of the function. */
1389 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1390 return;
1391
2f87ccae 1392 /* If INSN is an instruction from target of an annulled branch, the
1393 effects are for the target only and so current argument size
1394 shouldn't change at all. */
1395 if (final_sequence
1396 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1397 && INSN_FROM_TARGET_P (insn))
1398 return;
1399
46b2b3c8 1400 /* If only calls can throw, and we have a frame pointer,
1401 save up adjustments until we see the CALL_INSN. */
1402 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1403 {
1404 if (CALL_P (insn) && !after_p)
1405 {
1406 /* Extract the size of the args from the CALL rtx itself. */
1407 insn = PATTERN (insn);
1408 if (GET_CODE (insn) == PARALLEL)
1409 insn = XVECEXP (insn, 0, 0);
1410 if (GET_CODE (insn) == SET)
1411 insn = SET_SRC (insn);
1412 gcc_assert (GET_CODE (insn) == CALL);
1413 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1414 }
1415 return;
1416 }
1417
1418 if (CALL_P (insn) && !after_p)
1419 {
1420 if (!flag_asynchronous_unwind_tables)
1421 dwarf2out_args_size ("", args_size);
1422 return;
1423 }
1424 else if (BARRIER_P (insn))
d757b8c9 1425 {
8aa5820b 1426 /* Don't call compute_barrier_args_size () if the only
1427 BARRIER is at the end of function. */
1428 if (barrier_args_size == NULL && next_nonnote_insn (insn))
9396ae97 1429 compute_barrier_args_size ();
8aa5820b 1430 if (barrier_args_size == NULL)
9396ae97 1431 offset = 0;
8aa5820b 1432 else
1433 {
1434 offset = barrier_args_size[INSN_UID (insn)];
1435 if (offset < 0)
1436 offset = 0;
1437 }
9396ae97 1438
1439 offset -= args_size;
1440#ifndef STACK_GROWS_DOWNWARD
1441 offset = -offset;
24db2725 1442#endif
d757b8c9 1443 }
24db2725 1444 else if (GET_CODE (PATTERN (insn)) == SET)
2f87ccae 1445 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
6ee89c56 1446 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1447 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1448 {
1449 /* There may be stack adjustments inside compound insns. Search
8c3f468d 1450 for them. */
1451 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1452 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
2f87ccae 1453 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1454 args_size, offset);
d757b8c9 1455 }
1456 else
1457 return;
ac02093f 1458
24db2725 1459 if (offset == 0)
1460 return;
1461
fe3cf4b3 1462 label = dwarf2out_cfi_label ();
1463 dwarf2out_args_size_adjust (offset, label);
1464}
1465
1466/* Adjust args_size based on stack adjustment OFFSET. */
1467
1468static void
1469dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1470{
4b72e226 1471 if (cfa.reg == STACK_POINTER_REGNUM)
1472 cfa.offset += offset;
d757b8c9 1473
fe3cf4b3 1474 if (cfa_store.reg == STACK_POINTER_REGNUM)
1475 cfa_store.offset += offset;
1476
d757b8c9 1477#ifndef STACK_GROWS_DOWNWARD
1478 offset = -offset;
1479#endif
8c3f468d 1480
d757b8c9 1481 args_size += offset;
1482 if (args_size < 0)
1483 args_size = 0;
1484
4b72e226 1485 def_cfa_1 (label, &cfa);
535fcfa4 1486 if (flag_asynchronous_unwind_tables)
1487 dwarf2out_args_size (label, args_size);
d757b8c9 1488}
1489
573aba85 1490#endif
1491
b0d72d68 1492/* We delay emitting a register save until either (a) we reach the end
1493 of the prologue or (b) the register is clobbered. This clusters
1494 register saves so that there are fewer pc advances. */
1495
fb1e4f4a 1496struct GTY(()) queued_reg_save {
b0d72d68 1497 struct queued_reg_save *next;
1498 rtx reg;
3d867824 1499 HOST_WIDE_INT cfa_offset;
60ea93bb 1500 rtx saved_reg;
b0d72d68 1501};
1502
573aba85 1503static GTY(()) struct queued_reg_save *queued_reg_saves;
1504
60ea93bb 1505/* The caller's ORIG_REG is saved in SAVED_IN_REG. */
fb1e4f4a 1506struct GTY(()) reg_saved_in_data {
60ea93bb 1507 rtx orig_reg;
1508 rtx saved_in_reg;
1509};
1510
1511/* A list of registers saved in other registers.
1512 The list intentionally has a small maximum capacity of 4; if your
1513 port needs more than that, you might consider implementing a
1514 more efficient data structure. */
1515static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1516static GTY(()) size_t num_regs_saved_in_regs;
8ff30ff6 1517
573aba85 1518#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
b0d72d68 1519static const char *last_reg_save_label;
1520
60ea93bb 1521/* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1522 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1523
b0d72d68 1524static void
60ea93bb 1525queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
b0d72d68 1526{
60ea93bb 1527 struct queued_reg_save *q;
1528
1529 /* Duplicates waste space, but it's also necessary to remove them
1530 for correctness, since the queue gets output in reverse
1531 order. */
1532 for (q = queued_reg_saves; q != NULL; q = q->next)
1533 if (REGNO (q->reg) == REGNO (reg))
1534 break;
1535
1536 if (q == NULL)
1537 {
2457c754 1538 q = GGC_NEW (struct queued_reg_save);
60ea93bb 1539 q->next = queued_reg_saves;
1540 queued_reg_saves = q;
1541 }
b0d72d68 1542
b0d72d68 1543 q->reg = reg;
1544 q->cfa_offset = offset;
60ea93bb 1545 q->saved_reg = sreg;
b0d72d68 1546
1547 last_reg_save_label = label;
1548}
1549
60ea93bb 1550/* Output all the entries in QUEUED_REG_SAVES. */
1551
b0d72d68 1552static void
8ec3a57b 1553flush_queued_reg_saves (void)
b0d72d68 1554{
60ea93bb 1555 struct queued_reg_save *q;
b0d72d68 1556
60ea93bb 1557 for (q = queued_reg_saves; q; q = q->next)
b0d72d68 1558 {
60ea93bb 1559 size_t i;
f481766a 1560 unsigned int reg, sreg;
1561
60ea93bb 1562 for (i = 0; i < num_regs_saved_in_regs; i++)
1563 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1564 break;
1565 if (q->saved_reg && i == num_regs_saved_in_regs)
1566 {
7bd4f6b6 1567 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
60ea93bb 1568 num_regs_saved_in_regs++;
1569 }
1570 if (i != num_regs_saved_in_regs)
1571 {
1572 regs_saved_in_regs[i].orig_reg = q->reg;
1573 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1574 }
1575
f481766a 1576 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1577 if (q->saved_reg)
1578 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1579 else
1580 sreg = INVALID_REGNUM;
1581 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
b0d72d68 1582 }
1583
1584 queued_reg_saves = NULL;
1585 last_reg_save_label = NULL;
1586}
1587
60ea93bb 1588/* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1589 location for? Or, does it clobber a register which we've previously
1590 said that some other register is saved in, and for which we now
1591 have a new location for? */
1592
b0d72d68 1593static bool
5493cb9a 1594clobbers_queued_reg_save (const_rtx insn)
b0d72d68 1595{
1596 struct queued_reg_save *q;
1597
bc70bd5e 1598 for (q = queued_reg_saves; q; q = q->next)
60ea93bb 1599 {
1600 size_t i;
1601 if (modified_in_p (q->reg, insn))
1602 return true;
1603 for (i = 0; i < num_regs_saved_in_regs; i++)
1604 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1605 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1606 return true;
1607 }
b0d72d68 1608
1609 return false;
1610}
bc70bd5e 1611
567925e3 1612/* Entry point for saving the first register into the second. */
1613
1614void
1615dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1616{
1617 size_t i;
1618 unsigned int regno, sregno;
1619
1620 for (i = 0; i < num_regs_saved_in_regs; i++)
1621 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1622 break;
1623 if (i == num_regs_saved_in_regs)
1624 {
1625 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1626 num_regs_saved_in_regs++;
1627 }
1628 regs_saved_in_regs[i].orig_reg = reg;
1629 regs_saved_in_regs[i].saved_in_reg = sreg;
1630
1631 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1632 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1633 reg_save (label, regno, sregno, 0);
1634}
1635
60ea93bb 1636/* What register, if any, is currently saved in REG? */
1637
1638static rtx
1639reg_saved_in (rtx reg)
1640{
1641 unsigned int regn = REGNO (reg);
1642 size_t i;
1643 struct queued_reg_save *q;
8ff30ff6 1644
60ea93bb 1645 for (q = queued_reg_saves; q; q = q->next)
1646 if (q->saved_reg && regn == REGNO (q->saved_reg))
1647 return q->reg;
1648
1649 for (i = 0; i < num_regs_saved_in_regs; i++)
1650 if (regs_saved_in_regs[i].saved_in_reg
1651 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1652 return regs_saved_in_regs[i].orig_reg;
1653
1654 return NULL_RTX;
1655}
1656
b0d72d68 1657
950ae8fe 1658/* A temporary register holding an integral value used in adjusting SP
1659 or setting up the store_reg. The "offset" field holds the integer
1660 value, not an offset. */
b0d72d68 1661static dw_cfa_location cfa_temp;
950ae8fe 1662
1663/* Record call frame debugging information for an expression EXPR,
1664 which either sets SP or FP (adjusting how we calculate the frame
60ea93bb 1665 address) or saves a register to the stack or another register.
1666 LABEL indicates the address of EXPR.
950ae8fe 1667
1668 This function encodes a state machine mapping rtxes to actions on
1669 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1670 users need not read the source code.
1671
ae8c6892 1672 The High-Level Picture
1673
1674 Changes in the register we use to calculate the CFA: Currently we
1675 assume that if you copy the CFA register into another register, we
1676 should take the other one as the new CFA register; this seems to
1677 work pretty well. If it's wrong for some target, it's simple
1678 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1679
1680 Changes in the register we use for saving registers to the stack:
1681 This is usually SP, but not always. Again, we deduce that if you
1682 copy SP into another register (and SP is not the CFA register),
1683 then the new register is the one we will be using for register
1684 saves. This also seems to work.
1685
1686 Register saves: There's not much guesswork about this one; if
1687 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1688 register save, and the register used to calculate the destination
1689 had better be the one we think we're using for this purpose.
60ea93bb 1690 It's also assumed that a copy from a call-saved register to another
1691 register is saving that register if RTX_FRAME_RELATED_P is set on
1692 that instruction. If the copy is from a call-saved register to
1693 the *same* register, that means that the register is now the same
1694 value as in the caller.
ae8c6892 1695
1696 Except: If the register being saved is the CFA register, and the
6ef828f9 1697 offset is nonzero, we are saving the CFA, so we assume we have to
ae8c6892 1698 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1699 the intent is to save the value of SP from the previous frame.
1700
60ea93bb 1701 In addition, if a register has previously been saved to a different
8ff30ff6 1702 register,
60ea93bb 1703
950ae8fe 1704 Invariants / Summaries of Rules
1705
ae8c6892 1706 cfa current rule for calculating the CFA. It usually
1707 consists of a register and an offset.
950ae8fe 1708 cfa_store register used by prologue code to save things to the stack
1709 cfa_store.offset is the offset from the value of
1710 cfa_store.reg to the actual CFA
1711 cfa_temp register holding an integral value. cfa_temp.offset
1712 stores the value, which will be used to adjust the
cc858176 1713 stack pointer. cfa_temp is also used like cfa_store,
1714 to track stores to the stack via fp or a temp reg.
bc70bd5e 1715
950ae8fe 1716 Rules 1- 4: Setting a register's value to cfa.reg or an expression
8ec3a57b 1717 with cfa.reg as the first operand changes the cfa.reg and its
cc858176 1718 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1719 cfa_temp.offset.
950ae8fe 1720
1721 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1722 expression yielding a constant. This sets cfa_temp.reg
1723 and cfa_temp.offset.
1724
1725 Rule 5: Create a new register cfa_store used to save items to the
1726 stack.
1727
cc858176 1728 Rules 10-14: Save a register to the stack. Define offset as the
ae8c6892 1729 difference of the original location and cfa_store's
cc858176 1730 location (or cfa_temp's location if cfa_temp is used).
950ae8fe 1731
27a7a23a 1732 Rules 16-20: If AND operation happens on sp in prologue, we assume
1733 stack is realigned. We will use a group of DW_OP_XXX
1734 expressions to represent the location of the stored
1735 register instead of CFA+offset.
1736
950ae8fe 1737 The Rules
1738
1739 "{a,b}" indicates a choice of a xor b.
1740 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1741
1742 Rule 1:
1743 (set <reg1> <reg2>:cfa.reg)
cc858176 1744 effects: cfa.reg = <reg1>
c83a163c 1745 cfa.offset unchanged
cc858176 1746 cfa_temp.reg = <reg1>
1747 cfa_temp.offset = cfa.offset
950ae8fe 1748
1749 Rule 2:
8c3f468d 1750 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1751 {<const_int>,<reg>:cfa_temp.reg}))
950ae8fe 1752 effects: cfa.reg = sp if fp used
8ec3a57b 1753 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
950ae8fe 1754 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1755 if cfa_store.reg==sp
1756
1757 Rule 3:
cc858176 1758 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
950ae8fe 1759 effects: cfa.reg = fp
8ec3a57b 1760 cfa_offset += +/- <const_int>
950ae8fe 1761
1762 Rule 4:
cc858176 1763 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
950ae8fe 1764 constraints: <reg1> != fp
8ec3a57b 1765 <reg1> != sp
950ae8fe 1766 effects: cfa.reg = <reg1>
cc858176 1767 cfa_temp.reg = <reg1>
1768 cfa_temp.offset = cfa.offset
950ae8fe 1769
1770 Rule 5:
1771 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1772 constraints: <reg1> != fp
8ec3a57b 1773 <reg1> != sp
950ae8fe 1774 effects: cfa_store.reg = <reg1>
8ec3a57b 1775 cfa_store.offset = cfa.offset - cfa_temp.offset
950ae8fe 1776
1777 Rule 6:
1778 (set <reg> <const_int>)
1779 effects: cfa_temp.reg = <reg>
8ec3a57b 1780 cfa_temp.offset = <const_int>
950ae8fe 1781
1782 Rule 7:
1783 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1784 effects: cfa_temp.reg = <reg1>
1785 cfa_temp.offset |= <const_int>
1786
1787 Rule 8:
1788 (set <reg> (high <exp>))
1789 effects: none
1790
1791 Rule 9:
1792 (set <reg> (lo_sum <exp> <const_int>))
1793 effects: cfa_temp.reg = <reg>
8ec3a57b 1794 cfa_temp.offset = <const_int>
950ae8fe 1795
1796 Rule 10:
1797 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1798 effects: cfa_store.offset -= <const_int>
1799 cfa.offset = cfa_store.offset if cfa.reg == sp
950ae8fe 1800 cfa.reg = sp
cc858176 1801 cfa.base_offset = -cfa_store.offset
950ae8fe 1802
1803 Rule 11:
1804 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1805 effects: cfa_store.offset += -/+ mode_size(mem)
1806 cfa.offset = cfa_store.offset if cfa.reg == sp
950ae8fe 1807 cfa.reg = sp
cc858176 1808 cfa.base_offset = -cfa_store.offset
950ae8fe 1809
1810 Rule 12:
8c3f468d 1811 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1812
1813 <reg2>)
cc858176 1814 effects: cfa.reg = <reg1>
1815 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
950ae8fe 1816
1817 Rule 13:
cc858176 1818 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1819 effects: cfa.reg = <reg1>
1820 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1821
1822 Rule 14:
1823 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1824 effects: cfa.reg = <reg1>
1825 cfa.base_offset = -cfa_temp.offset
d15ee1a5 1826 cfa_temp.offset -= mode_size(mem)
1827
85fdc672 1828 Rule 15:
1829 (set <reg> {unspec, unspec_volatile})
27a7a23a 1830 effects: target-dependent
1831
1832 Rule 16:
1833 (set sp (and: sp <const_int>))
1834 constraints: cfa_store.reg == sp
1835 effects: current_fde.stack_realign = 1
1836 cfa_store.offset = 0
1837 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1838
1839 Rule 17:
1840 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1841 effects: cfa_store.offset += -/+ mode_size(mem)
1842
1843 Rule 18:
1844 (set (mem ({pre_inc, pre_dec} sp)) fp)
1845 constraints: fde->stack_realign == 1
1846 effects: cfa_store.offset = 0
1847 cfa.reg != HARD_FRAME_POINTER_REGNUM
1848
1849 Rule 19:
1850 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1851 constraints: fde->stack_realign == 1
1852 && cfa.offset == 0
1853 && cfa.indirect == 0
1854 && cfa.reg != HARD_FRAME_POINTER_REGNUM
1855 effects: Use DW_CFA_def_cfa_expression to define cfa
1856 cfa.reg == fde->drap_reg
1857
1858 Rule 20:
1859 (set reg fde->drap_reg)
1860 constraints: fde->vdrap_reg == INVALID_REGNUM
1861 effects: fde->vdrap_reg = reg.
1862 (set mem fde->drap_reg)
1863 constraints: fde->drap_reg_saved == 1
1864 effects: none. */
fa19b467 1865
1866static void
8ec3a57b 1867dwarf2out_frame_debug_expr (rtx expr, const char *label)
fa19b467 1868{
bf2e2aa9 1869 rtx src, dest, span;
8c3f468d 1870 HOST_WIDE_INT offset;
27a7a23a 1871 dw_fde_ref fde;
f80d1bcd 1872
1873 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1874 the PARALLEL independently. The first element is always processed if
950ae8fe 1875 it is a SET. This is for backward compatibility. Other elements
f80d1bcd 1876 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1877 flag is set in them. */
8c3f468d 1878 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
f80d1bcd 1879 {
fa19b467 1880 int par_index;
1881 int limit = XVECLEN (expr, 0);
a1d50f1d 1882 rtx elem;
1883
1884 /* PARALLELs have strict read-modify-write semantics, so we
1885 ought to evaluate every rvalue before changing any lvalue.
1886 It's cumbersome to do that in general, but there's an
1887 easy approximation that is enough for all current users:
1888 handle register saves before register assignments. */
1889 if (GET_CODE (expr) == PARALLEL)
1890 for (par_index = 0; par_index < limit; par_index++)
1891 {
1892 elem = XVECEXP (expr, 0, par_index);
1893 if (GET_CODE (elem) == SET
1894 && MEM_P (SET_DEST (elem))
1895 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1896 dwarf2out_frame_debug_expr (elem, label);
1897 }
fa19b467 1898
1899 for (par_index = 0; par_index < limit; par_index++)
a1d50f1d 1900 {
1901 elem = XVECEXP (expr, 0, par_index);
1902 if (GET_CODE (elem) == SET
1903 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1904 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1905 dwarf2out_frame_debug_expr (elem, label);
6c504100 1906 else if (GET_CODE (elem) == SET
1907 && par_index != 0
1908 && !RTX_FRAME_RELATED_P (elem))
1909 {
1910 /* Stack adjustment combining might combine some post-prologue
1911 stack adjustment into a prologue stack adjustment. */
2f87ccae 1912 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
6c504100 1913
1914 if (offset != 0)
fe3cf4b3 1915 dwarf2out_args_size_adjust (offset, label);
6c504100 1916 }
a1d50f1d 1917 }
fa19b467 1918 return;
1919 }
f80d1bcd 1920
7bd4f6b6 1921 gcc_assert (GET_CODE (expr) == SET);
fa19b467 1922
1923 src = SET_SRC (expr);
1924 dest = SET_DEST (expr);
1925
1c14a50e 1926 if (REG_P (src))
60ea93bb 1927 {
1928 rtx rsi = reg_saved_in (src);
1929 if (rsi)
1930 src = rsi;
1931 }
1932
27a7a23a 1933 fde = current_fde ();
1934
1935 if (GET_CODE (src) == REG
1936 && fde
1937 && fde->drap_reg == REGNO (src)
1938 && (fde->drap_reg_saved
1939 || GET_CODE (dest) == REG))
1940 {
1941 /* Rule 20 */
1942 /* If we are saving dynamic realign argument pointer to a
1943 register, the destination is virtual dynamic realign
1944 argument pointer. It may be used to access argument. */
1945 if (GET_CODE (dest) == REG)
1946 {
1947 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1948 fde->vdrap_reg = REGNO (dest);
1949 }
1950 return;
1951 }
1952
fa19b467 1953 switch (GET_CODE (dest))
1954 {
1955 case REG:
fa19b467 1956 switch (GET_CODE (src))
f80d1bcd 1957 {
1958 /* Setting FP from SP. */
1959 case REG:
1960 if (cfa.reg == (unsigned) REGNO (src))
60ea93bb 1961 {
1962 /* Rule 1 */
1963 /* Update the CFA rule wrt SP or FP. Make sure src is
8ff30ff6 1964 relative to the current CFA register.
60ea93bb 1965
1966 We used to require that dest be either SP or FP, but the
1967 ARM copies SP to a temporary register, and from there to
1968 FP. So we just rely on the backends to only set
1969 RTX_FRAME_RELATED_P on appropriate insns. */
1970 cfa.reg = REGNO (dest);
1971 cfa_temp.reg = cfa.reg;
1972 cfa_temp.offset = cfa.offset;
1973 }
7bd4f6b6 1974 else
60ea93bb 1975 {
1976 /* Saving a register in a register. */
ed86dceb 1977 gcc_assert (!fixed_regs [REGNO (dest)]
1978 /* For the SPARC and its register window. */
1979 || (DWARF_FRAME_REGNUM (REGNO (src))
1980 == DWARF_FRAME_RETURN_COLUMN));
27a7a23a 1981
1982 /* After stack is aligned, we can only save SP in FP
1983 if drap register is used. In this case, we have
1984 to restore stack pointer with the CFA value and we
1985 don't generate this DWARF information. */
1986 if (fde
1987 && fde->stack_realign
1988 && REGNO (src) == STACK_POINTER_REGNUM)
1989 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1990 && fde->drap_reg != INVALID_REGNUM
1991 && cfa.reg != REGNO (src));
1992 else
1993 queue_reg_save (label, src, dest, 0);
60ea93bb 1994 }
f80d1bcd 1995 break;
fa19b467 1996
f80d1bcd 1997 case PLUS:
1998 case MINUS:
cc858176 1999 case LO_SUM:
f80d1bcd 2000 if (dest == stack_pointer_rtx)
2001 {
950ae8fe 2002 /* Rule 2 */
31306376 2003 /* Adjusting SP. */
2004 switch (GET_CODE (XEXP (src, 1)))
2005 {
2006 case CONST_INT:
2007 offset = INTVAL (XEXP (src, 1));
2008 break;
2009 case REG:
7bd4f6b6 2010 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2011 == cfa_temp.reg);
950ae8fe 2012 offset = cfa_temp.offset;
31306376 2013 break;
2014 default:
7bd4f6b6 2015 gcc_unreachable ();
31306376 2016 }
2017
2018 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2019 {
2020 /* Restoring SP from FP in the epilogue. */
7bd4f6b6 2021 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
4b72e226 2022 cfa.reg = STACK_POINTER_REGNUM;
31306376 2023 }
cc858176 2024 else if (GET_CODE (src) == LO_SUM)
2025 /* Assume we've set the source reg of the LO_SUM from sp. */
2026 ;
7bd4f6b6 2027 else
2028 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
31306376 2029
cc858176 2030 if (GET_CODE (src) != MINUS)
31306376 2031 offset = -offset;
4b72e226 2032 if (cfa.reg == STACK_POINTER_REGNUM)
2033 cfa.offset += offset;
2034 if (cfa_store.reg == STACK_POINTER_REGNUM)
2035 cfa_store.offset += offset;
f80d1bcd 2036 }
2037 else if (dest == hard_frame_pointer_rtx)
2038 {
950ae8fe 2039 /* Rule 3 */
31306376 2040 /* Either setting the FP from an offset of the SP,
2041 or adjusting the FP */
7bd4f6b6 2042 gcc_assert (frame_pointer_needed);
31306376 2043
7bd4f6b6 2044 gcc_assert (REG_P (XEXP (src, 0))
2045 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2046 && GET_CODE (XEXP (src, 1)) == CONST_INT);
2047 offset = INTVAL (XEXP (src, 1));
2048 if (GET_CODE (src) != MINUS)
2049 offset = -offset;
2050 cfa.offset += offset;
2051 cfa.reg = HARD_FRAME_POINTER_REGNUM;
f80d1bcd 2052 }
2053 else
2054 {
7bd4f6b6 2055 gcc_assert (GET_CODE (src) != MINUS);
4747ea36 2056
950ae8fe 2057 /* Rule 4 */
8ad4c111 2058 if (REG_P (XEXP (src, 0))
4747ea36 2059 && REGNO (XEXP (src, 0)) == cfa.reg
2060 && GET_CODE (XEXP (src, 1)) == CONST_INT)
9b536fa6 2061 {
2062 /* Setting a temporary CFA register that will be copied
2063 into the FP later on. */
cc858176 2064 offset = - INTVAL (XEXP (src, 1));
9b536fa6 2065 cfa.offset += offset;
2066 cfa.reg = REGNO (dest);
cc858176 2067 /* Or used to save regs to the stack. */
2068 cfa_temp.reg = cfa.reg;
2069 cfa_temp.offset = cfa.offset;
9b536fa6 2070 }
8c3f468d 2071
950ae8fe 2072 /* Rule 5 */
8ad4c111 2073 else if (REG_P (XEXP (src, 0))
cc858176 2074 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2075 && XEXP (src, 1) == stack_pointer_rtx)
4747ea36 2076 {
ca6c45a9 2077 /* Setting a scratch register that we will use instead
2078 of SP for saving registers to the stack. */
7bd4f6b6 2079 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
4747ea36 2080 cfa_store.reg = REGNO (dest);
950ae8fe 2081 cfa_store.offset = cfa.offset - cfa_temp.offset;
4747ea36 2082 }
8c3f468d 2083
cc858176 2084 /* Rule 9 */
2085 else if (GET_CODE (src) == LO_SUM
2086 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2087 {
2088 cfa_temp.reg = REGNO (dest);
2089 cfa_temp.offset = INTVAL (XEXP (src, 1));
2090 }
2091 else
7bd4f6b6 2092 gcc_unreachable ();
f80d1bcd 2093 }
2094 break;
fa19b467 2095
950ae8fe 2096 /* Rule 6 */
f80d1bcd 2097 case CONST_INT:
950ae8fe 2098 cfa_temp.reg = REGNO (dest);
2099 cfa_temp.offset = INTVAL (src);
f80d1bcd 2100 break;
fa19b467 2101
950ae8fe 2102 /* Rule 7 */
f80d1bcd 2103 case IOR:
7bd4f6b6 2104 gcc_assert (REG_P (XEXP (src, 0))
2105 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2106 && GET_CODE (XEXP (src, 1)) == CONST_INT);
8c3f468d 2107
950ae8fe 2108 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2109 cfa_temp.reg = REGNO (dest);
2110 cfa_temp.offset |= INTVAL (XEXP (src, 1));
f80d1bcd 2111 break;
fa19b467 2112
e0cedf2c 2113 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2114 which will fill in all of the bits. */
2115 /* Rule 8 */
2116 case HIGH:
2117 break;
2118
d15ee1a5 2119 /* Rule 15 */
2120 case UNSPEC:
2121 case UNSPEC_VOLATILE:
2122 gcc_assert (targetm.dwarf_handle_frame_unspec);
2123 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
567925e3 2124 return;
d15ee1a5 2125
27a7a23a 2126 /* Rule 16 */
2127 case AND:
2128 /* If this AND operation happens on stack pointer in prologue,
2129 we assume the stack is realigned and we extract the
2130 alignment. */
2131 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2132 {
2133 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2134 fde->stack_realign = 1;
2135 fde->stack_realignment = INTVAL (XEXP (src, 1));
2136 cfa_store.offset = 0;
2137
2138 if (cfa.reg != STACK_POINTER_REGNUM
2139 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2140 fde->drap_reg = cfa.reg;
2141 }
2142 return;
2143
f80d1bcd 2144 default:
7bd4f6b6 2145 gcc_unreachable ();
f80d1bcd 2146 }
8c3f468d 2147
4b72e226 2148 def_cfa_1 (label, &cfa);
31306376 2149 break;
fa19b467 2150
31306376 2151 case MEM:
4b72e226 2152
4b72e226 2153 /* Saving a register to the stack. Make sure dest is relative to the
2154 CFA register. */
31306376 2155 switch (GET_CODE (XEXP (dest, 0)))
2156 {
950ae8fe 2157 /* Rule 10 */
31306376 2158 /* With a push. */
93fbe1f3 2159 case PRE_MODIFY:
2160 /* We can't handle variable size modifications. */
7bd4f6b6 2161 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2162 == CONST_INT);
93fbe1f3 2163 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2164
7bd4f6b6 2165 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2166 && cfa_store.reg == STACK_POINTER_REGNUM);
8c3f468d 2167
93fbe1f3 2168 cfa_store.offset += offset;
2169 if (cfa.reg == STACK_POINTER_REGNUM)
2170 cfa.offset = cfa_store.offset;
2171
2172 offset = -cfa_store.offset;
2173 break;
8c3f468d 2174
950ae8fe 2175 /* Rule 11 */
31306376 2176 case PRE_INC:
2177 case PRE_DEC:
2178 offset = GET_MODE_SIZE (GET_MODE (dest));
2179 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2180 offset = -offset;
fa19b467 2181
27a7a23a 2182 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2183 == STACK_POINTER_REGNUM)
7bd4f6b6 2184 && cfa_store.reg == STACK_POINTER_REGNUM);
8c3f468d 2185
4b72e226 2186 cfa_store.offset += offset;
27a7a23a 2187
2188 /* Rule 18: If stack is aligned, we will use FP as a
2189 reference to represent the address of the stored
2190 regiser. */
2191 if (fde
2192 && fde->stack_realign
2193 && src == hard_frame_pointer_rtx)
2194 {
2195 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2196 cfa_store.offset = 0;
2197 }
2198
4b72e226 2199 if (cfa.reg == STACK_POINTER_REGNUM)
2200 cfa.offset = cfa_store.offset;
fa19b467 2201
4b72e226 2202 offset = -cfa_store.offset;
31306376 2203 break;
fa19b467 2204
950ae8fe 2205 /* Rule 12 */
31306376 2206 /* With an offset. */
2207 case PLUS:
2208 case MINUS:
cc858176 2209 case LO_SUM:
7bd4f6b6 2210 {
2211 int regno;
8ff30ff6 2212
ccb88806 2213 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2214 && REG_P (XEXP (XEXP (dest, 0), 0)));
7bd4f6b6 2215 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2216 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2217 offset = -offset;
2218
2219 regno = REGNO (XEXP (XEXP (dest, 0), 0));
8ff30ff6 2220
7bd4f6b6 2221 if (cfa_store.reg == (unsigned) regno)
2222 offset -= cfa_store.offset;
2223 else
2224 {
2225 gcc_assert (cfa_temp.reg == (unsigned) regno);
2226 offset -= cfa_temp.offset;
2227 }
2228 }
31306376 2229 break;
2230
950ae8fe 2231 /* Rule 13 */
31306376 2232 /* Without an offset. */
2233 case REG:
7bd4f6b6 2234 {
2235 int regno = REGNO (XEXP (dest, 0));
8ff30ff6 2236
7bd4f6b6 2237 if (cfa_store.reg == (unsigned) regno)
2238 offset = -cfa_store.offset;
2239 else
2240 {
2241 gcc_assert (cfa_temp.reg == (unsigned) regno);
2242 offset = -cfa_temp.offset;
2243 }
2244 }
cc858176 2245 break;
2246
2247 /* Rule 14 */
2248 case POST_INC:
7bd4f6b6 2249 gcc_assert (cfa_temp.reg
2250 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
cc858176 2251 offset = -cfa_temp.offset;
2252 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
31306376 2253 break;
2254
2255 default:
7bd4f6b6 2256 gcc_unreachable ();
31306376 2257 }
49a9983c 2258
3d3b5ccd 2259 /* Rule 17 */
2260 /* If the source operand of this MEM operation is not a
2261 register, basically the source is return address. Here
2262 we only care how much stack grew and we don't save it. */
2263 if (!REG_P (src))
2264 break;
2265
f80d1bcd 2266 if (REGNO (src) != STACK_POINTER_REGNUM
49a9983c 2267 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2268 && (unsigned) REGNO (src) == cfa.reg)
2269 {
2270 /* We're storing the current CFA reg into the stack. */
2271
2272 if (cfa.offset == 0)
2273 {
27a7a23a 2274 /* Rule 19 */
2275 /* If stack is aligned, putting CFA reg into stack means
2276 we can no longer use reg + offset to represent CFA.
2277 Here we use DW_CFA_def_cfa_expression instead. The
2278 result of this expression equals to the original CFA
2279 value. */
2280 if (fde
2281 && fde->stack_realign
2282 && cfa.indirect == 0
2283 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2284 {
2285 dw_cfa_location cfa_exp;
2286
2287 gcc_assert (fde->drap_reg == cfa.reg);
2288
2289 cfa_exp.indirect = 1;
2290 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2291 cfa_exp.base_offset = offset;
2292 cfa_exp.offset = 0;
2293
2294 fde->drap_reg_saved = 1;
2295
2296 def_cfa_1 (label, &cfa_exp);
2297 break;
2298 }
2299
49a9983c 2300 /* If the source register is exactly the CFA, assume
2301 we're saving SP like any other register; this happens
2302 on the ARM. */
49a9983c 2303 def_cfa_1 (label, &cfa);
60ea93bb 2304 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
49a9983c 2305 break;
2306 }
2307 else
2308 {
2309 /* Otherwise, we'll need to look in the stack to
c83a163c 2310 calculate the CFA. */
49a9983c 2311 rtx x = XEXP (dest, 0);
8c3f468d 2312
8ad4c111 2313 if (!REG_P (x))
49a9983c 2314 x = XEXP (x, 0);
7bd4f6b6 2315 gcc_assert (REG_P (x));
8c3f468d 2316
2317 cfa.reg = REGNO (x);
49a9983c 2318 cfa.base_offset = offset;
2319 cfa.indirect = 1;
2320 def_cfa_1 (label, &cfa);
2321 break;
2322 }
2323 }
2324
4b72e226 2325 def_cfa_1 (label, &cfa);
bf2e2aa9 2326 {
2327 span = targetm.dwarf_register_span (src);
2328
2329 if (!span)
2330 queue_reg_save (label, src, NULL_RTX, offset);
2331 else
2332 {
2333 /* We have a PARALLEL describing where the contents of SRC
2334 live. Queue register saves for each piece of the
2335 PARALLEL. */
2336 int par_index;
2337 int limit;
2338 HOST_WIDE_INT span_offset = offset;
2339
2340 gcc_assert (GET_CODE (span) == PARALLEL);
2341
2342 limit = XVECLEN (span, 0);
2343 for (par_index = 0; par_index < limit; par_index++)
2344 {
2345 rtx elem = XVECEXP (span, 0, par_index);
2346
2347 queue_reg_save (label, elem, NULL_RTX, span_offset);
2348 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2349 }
2350 }
2351 }
31306376 2352 break;
2353
2354 default:
7bd4f6b6 2355 gcc_unreachable ();
31306376 2356 }
fa19b467 2357}
2358
8a8bfbe7 2359/* Record call frame debugging information for INSN, which either
2360 sets SP or FP (adjusting how we calculate the frame address) or saves a
535fcfa4 2361 register to the stack. If INSN is NULL_RTX, initialize our state.
2362
2363 If AFTER_P is false, we're being called before the insn is emitted,
2364 otherwise after. Call instructions get invoked twice. */
ec1e49cc 2365
8a8bfbe7 2366void
535fcfa4 2367dwarf2out_frame_debug (rtx insn, bool after_p)
30ade641 2368{
1e034a40 2369 const char *label;
fa19b467 2370 rtx src;
8a8bfbe7 2371
2372 if (insn == NULL_RTX)
30ade641 2373 {
60ea93bb 2374 size_t i;
8ff30ff6 2375
b0d72d68 2376 /* Flush any queued register saves. */
2377 flush_queued_reg_saves ();
2378
8a8bfbe7 2379 /* Set up state for generating call frame debug info. */
4b72e226 2380 lookup_cfa (&cfa);
7bd4f6b6 2381 gcc_assert (cfa.reg
2382 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
8c3f468d 2383
4b72e226 2384 cfa.reg = STACK_POINTER_REGNUM;
2385 cfa_store = cfa;
950ae8fe 2386 cfa_temp.reg = -1;
2387 cfa_temp.offset = 0;
8ff30ff6 2388
60ea93bb 2389 for (i = 0; i < num_regs_saved_in_regs; i++)
2390 {
2391 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2392 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2393 }
2394 num_regs_saved_in_regs = 0;
9396ae97 2395
2396 if (barrier_args_size)
2397 {
2398 XDELETEVEC (barrier_args_size);
2399 barrier_args_size = NULL;
2400 }
8a8bfbe7 2401 return;
2402 }
2403
6d7dc5b9 2404 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
b0d72d68 2405 flush_queued_reg_saves ();
2406
d757b8c9 2407 if (! RTX_FRAME_RELATED_P (insn))
2408 {
b0d72d68 2409 if (!ACCUMULATE_OUTGOING_ARGS)
535fcfa4 2410 dwarf2out_stack_adjust (insn, after_p);
d757b8c9 2411 return;
2412 }
2413
8a8bfbe7 2414 label = dwarf2out_cfi_label ();
86b18255 2415 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2416 if (src)
2417 insn = XEXP (src, 0);
f80d1bcd 2418 else
86b18255 2419 insn = PATTERN (insn);
2420
fa19b467 2421 dwarf2out_frame_debug_expr (insn, label);
8a8bfbe7 2422}
2423
573aba85 2424#endif
2425
2426/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
8ec3a57b 2427static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2428 (enum dwarf_call_frame_info cfi);
573aba85 2429
2430static enum dw_cfi_oprnd_type
8ec3a57b 2431dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
573aba85 2432{
2433 switch (cfi)
2434 {
2435 case DW_CFA_nop:
2436 case DW_CFA_GNU_window_save:
2437 return dw_cfi_oprnd_unused;
2438
2439 case DW_CFA_set_loc:
2440 case DW_CFA_advance_loc1:
2441 case DW_CFA_advance_loc2:
2442 case DW_CFA_advance_loc4:
2443 case DW_CFA_MIPS_advance_loc8:
2444 return dw_cfi_oprnd_addr;
2445
2446 case DW_CFA_offset:
2447 case DW_CFA_offset_extended:
2448 case DW_CFA_def_cfa:
2449 case DW_CFA_offset_extended_sf:
2450 case DW_CFA_def_cfa_sf:
2451 case DW_CFA_restore_extended:
2452 case DW_CFA_undefined:
2453 case DW_CFA_same_value:
2454 case DW_CFA_def_cfa_register:
2455 case DW_CFA_register:
2456 return dw_cfi_oprnd_reg_num;
2457
2458 case DW_CFA_def_cfa_offset:
2459 case DW_CFA_GNU_args_size:
2460 case DW_CFA_def_cfa_offset_sf:
2461 return dw_cfi_oprnd_offset;
8ec3a57b 2462
573aba85 2463 case DW_CFA_def_cfa_expression:
2464 case DW_CFA_expression:
2465 return dw_cfi_oprnd_loc;
2466
2467 default:
7bd4f6b6 2468 gcc_unreachable ();
573aba85 2469 }
2470}
2471
2472/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
8ec3a57b 2473static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2474 (enum dwarf_call_frame_info cfi);
573aba85 2475
2476static enum dw_cfi_oprnd_type
8ec3a57b 2477dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
573aba85 2478{
2479 switch (cfi)
2480 {
2481 case DW_CFA_def_cfa:
2482 case DW_CFA_def_cfa_sf:
2483 case DW_CFA_offset:
2484 case DW_CFA_offset_extended_sf:
2485 case DW_CFA_offset_extended:
2486 return dw_cfi_oprnd_offset;
2487
2488 case DW_CFA_register:
2489 return dw_cfi_oprnd_reg_num;
2490
2491 default:
2492 return dw_cfi_oprnd_unused;
2493 }
2494}
2495
2496#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2497
2f14b1f9 2498/* Switch to eh_frame_section. If we don't have an eh_frame_section,
2499 switch to the data section instead, and write out a synthetic label
2500 for collect2. */
2501
2502static void
2503switch_to_eh_frame_section (void)
2504{
2505 tree label;
2506
2943ce06 2507#ifdef EH_FRAME_SECTION_NAME
2508 if (eh_frame_section == 0)
2509 {
2510 int flags;
2511
2512 if (EH_TABLES_CAN_BE_READ_ONLY)
2513 {
2514 int fde_encoding;
2515 int per_encoding;
2516 int lsda_encoding;
2517
2518 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2519 /*global=*/0);
2520 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2521 /*global=*/1);
2522 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2523 /*global=*/0);
2524 flags = ((! flag_pic
2525 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2526 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2527 && (per_encoding & 0x70) != DW_EH_PE_absptr
2528 && (per_encoding & 0x70) != DW_EH_PE_aligned
2529 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2530 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2531 ? 0 : SECTION_WRITE);
2532 }
2533 else
2534 flags = SECTION_WRITE;
2535 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2536 }
2537#endif
2538
2f14b1f9 2539 if (eh_frame_section)
2540 switch_to_section (eh_frame_section);
2541 else
2542 {
2943ce06 2543 /* We have no special eh_frame section. Put the information in
2544 the data section and emit special labels to guide collect2. */
2f14b1f9 2545 switch_to_section (data_section);
db85cc4f 2546 label = get_file_function_name ("F");
2f14b1f9 2547 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2548 targetm.asm_out.globalize_label (asm_out_file,
2549 IDENTIFIER_POINTER (label));
2550 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2551 }
2552}
2553
be30e9d9 2554/* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
2555
2556static HOST_WIDE_INT
2557div_data_align (HOST_WIDE_INT off)
2558{
2559 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2560 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2561 return r;
2562}
2563
8a8bfbe7 2564/* Output a Call Frame Information opcode and its operand(s). */
2565
2566static void
8ec3a57b 2567output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
8a8bfbe7 2568{
4eeb8b5d 2569 unsigned long r;
be30e9d9 2570 HOST_WIDE_INT off;
2571
8a8bfbe7 2572 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
8c3f468d 2573 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2574 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3201d6f1 2575 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
7df7561b 2576 ((unsigned HOST_WIDE_INT)
2577 cfi->dw_cfi_oprnd1.dw_cfi_offset));
8a8bfbe7 2578 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2579 {
4eeb8b5d 2580 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2581 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2582 "DW_CFA_offset, column 0x%lx", r);
be30e9d9 2583 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2584 dw2_asm_output_data_uleb128 (off, NULL);
8a8bfbe7 2585 }
2586 else if (cfi->dw_cfi_opc == DW_CFA_restore)
4eeb8b5d 2587 {
2588 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2589 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2590 "DW_CFA_restore, column 0x%lx", r);
2591 }
8a8bfbe7 2592 else
2593 {
ca98eb0a 2594 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2595 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
8a8bfbe7 2596
8a8bfbe7 2597 switch (cfi->dw_cfi_opc)
2598 {
2599 case DW_CFA_set_loc:
9b84bf7d 2600 if (for_eh)
2601 dw2_asm_output_encoded_addr_rtx (
2602 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2603 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
42e07529 2604 false, NULL);
9b84bf7d 2605 else
2606 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2607 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
d8eb7025 2608 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
8a8bfbe7 2609 break;
8c3f468d 2610
8a8bfbe7 2611 case DW_CFA_advance_loc1:
ca98eb0a 2612 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2613 fde->dw_fde_current_label, NULL);
c96dd0ff 2614 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
8a8bfbe7 2615 break;
8c3f468d 2616
8a8bfbe7 2617 case DW_CFA_advance_loc2:
ca98eb0a 2618 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2619 fde->dw_fde_current_label, NULL);
8a8bfbe7 2620 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2621 break;
8c3f468d 2622
8a8bfbe7 2623 case DW_CFA_advance_loc4:
ca98eb0a 2624 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2625 fde->dw_fde_current_label, NULL);
8a8bfbe7 2626 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2627 break;
8c3f468d 2628
8a8bfbe7 2629 case DW_CFA_MIPS_advance_loc8:
ca98eb0a 2630 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2631 fde->dw_fde_current_label, NULL);
2632 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
8a8bfbe7 2633 break;
8c3f468d 2634
8a8bfbe7 2635 case DW_CFA_offset_extended:
be30e9d9 2636 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2637 dw2_asm_output_data_uleb128 (r, NULL);
2638 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2639 dw2_asm_output_data_uleb128 (off, NULL);
2640 break;
2641
8a8bfbe7 2642 case DW_CFA_def_cfa:
4eeb8b5d 2643 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2644 dw2_asm_output_data_uleb128 (r, NULL);
ca98eb0a 2645 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
8a8bfbe7 2646 break;
8c3f468d 2647
15a56411 2648 case DW_CFA_offset_extended_sf:
be30e9d9 2649 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2650 dw2_asm_output_data_uleb128 (r, NULL);
2651 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2652 dw2_asm_output_data_sleb128 (off, NULL);
2653 break;
2654
15a56411 2655 case DW_CFA_def_cfa_sf:
4eeb8b5d 2656 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2657 dw2_asm_output_data_uleb128 (r, NULL);
be30e9d9 2658 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2659 dw2_asm_output_data_sleb128 (off, NULL);
15a56411 2660 break;
2661
8a8bfbe7 2662 case DW_CFA_restore_extended:
2663 case DW_CFA_undefined:
8a8bfbe7 2664 case DW_CFA_same_value:
2665 case DW_CFA_def_cfa_register:
4eeb8b5d 2666 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2667 dw2_asm_output_data_uleb128 (r, NULL);
8a8bfbe7 2668 break;
8c3f468d 2669
8a8bfbe7 2670 case DW_CFA_register:
4eeb8b5d 2671 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2672 dw2_asm_output_data_uleb128 (r, NULL);
2673 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2674 dw2_asm_output_data_uleb128 (r, NULL);
8a8bfbe7 2675 break;
8c3f468d 2676
8a8bfbe7 2677 case DW_CFA_def_cfa_offset:
ca98eb0a 2678 case DW_CFA_GNU_args_size:
2679 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
8a8bfbe7 2680 break;
8c3f468d 2681
15a56411 2682 case DW_CFA_def_cfa_offset_sf:
be30e9d9 2683 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2684 dw2_asm_output_data_sleb128 (off, NULL);
15a56411 2685 break;
2686
4ad3f9b3 2687 case DW_CFA_GNU_window_save:
2688 break;
8c3f468d 2689
4b72e226 2690 case DW_CFA_def_cfa_expression:
15a56411 2691 case DW_CFA_expression:
4b72e226 2692 output_cfa_loc (cfi);
2693 break;
8c3f468d 2694
15a56411 2695 case DW_CFA_GNU_negative_offset_extended:
2696 /* Obsoleted by DW_CFA_offset_extended_sf. */
7bd4f6b6 2697 gcc_unreachable ();
15a56411 2698
8a8bfbe7 2699 default:
2700 break;
2701 }
f80d1bcd 2702 }
8a8bfbe7 2703}
2704
fb39ff6e 2705/* Similar, but do it via assembler directives instead. */
2706
2707static void
2708output_cfi_directive (dw_cfi_ref cfi)
2709{
2710 unsigned long r, r2;
2711
2712 switch (cfi->dw_cfi_opc)
2713 {
2714 case DW_CFA_advance_loc:
2715 case DW_CFA_advance_loc1:
2716 case DW_CFA_advance_loc2:
2717 case DW_CFA_advance_loc4:
2718 case DW_CFA_MIPS_advance_loc8:
2719 case DW_CFA_set_loc:
2720 /* Should only be created by add_fde_cfi in a code path not
2721 followed when emitting via directives. The assembler is
2722 going to take care of this for us. */
2723 gcc_unreachable ();
2724
2725 case DW_CFA_offset:
2726 case DW_CFA_offset_extended:
2727 case DW_CFA_offset_extended_sf:
2728 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2729 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
be30e9d9 2730 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
fb39ff6e 2731 break;
2732
2733 case DW_CFA_restore:
2734 case DW_CFA_restore_extended:
2735 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2736 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2737 break;
2738
2739 case DW_CFA_undefined:
2740 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2741 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2742 break;
2743
2744 case DW_CFA_same_value:
2745 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2746 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2747 break;
2748
2749 case DW_CFA_def_cfa:
2750 case DW_CFA_def_cfa_sf:
2751 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2752 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2753 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2754 break;
2755
2756 case DW_CFA_def_cfa_register:
2757 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2758 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2759 break;
2760
2761 case DW_CFA_register:
2762 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2763 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2764 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2765 break;
2766
2767 case DW_CFA_def_cfa_offset:
2768 case DW_CFA_def_cfa_offset_sf:
2769 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2770 HOST_WIDE_INT_PRINT_DEC"\n",
2771 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2772 break;
2773
2774 case DW_CFA_GNU_args_size:
2775 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2776 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2777 if (flag_debug_asm)
2778 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2779 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2780 fputc ('\n', asm_out_file);
2781 break;
2782
2783 case DW_CFA_GNU_window_save:
2784 fprintf (asm_out_file, "\t.cfi_window_save\n");
2785 break;
2786
2787 case DW_CFA_def_cfa_expression:
2788 case DW_CFA_expression:
2789 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2790 output_cfa_loc_raw (cfi);
2791 fputc ('\n', asm_out_file);
2792 break;
2793
2794 default:
2795 gcc_unreachable ();
2796 }
2797}
2798
4eeb8b5d 2799/* Output the call frame information used to record information
8a8bfbe7 2800 that relates to calculating the frame pointer, and records the
2801 location of saved registers. */
2802
2803static void
8ec3a57b 2804output_call_frame_info (int for_eh)
8a8bfbe7 2805{
19cb6b50 2806 unsigned int i;
2807 dw_fde_ref fde;
2808 dw_cfi_ref cfi;
48ead6eb 2809 char l1[20], l2[20], section_start_label[20];
f7b10771 2810 bool any_lsda_needed = false;
df4b504c 2811 char augmentation[6];
9b84bf7d 2812 int augmentation_size;
2813 int fde_encoding = DW_EH_PE_absptr;
2814 int per_encoding = DW_EH_PE_absptr;
2815 int lsda_encoding = DW_EH_PE_absptr;
51ea5d02 2816 int return_reg;
8a8bfbe7 2817
637d3308 2818 /* Don't emit a CIE if there won't be any FDEs. */
2819 if (fde_table_in_use == 0)
2820 return;
2821
fb39ff6e 2822 /* Nothing to do if the assembler's doing it all. */
3dcd5df1 2823 if (dwarf2out_do_cfi_asm ())
fb39ff6e 2824 return;
2825
2f9fc8ef 2826 /* If we make FDEs linkonce, we may have to emit an empty label for
2827 an FDE that wouldn't otherwise be emitted. We want to avoid
2828 having an FDE kept around when the function it refers to is
1dc74225 2829 discarded. Example where this matters: a primary function
2f9fc8ef 2830 template in C++ requires EH information, but an explicit
0bed3869 2831 specialization doesn't. */
2f9fc8ef 2832 if (TARGET_USES_WEAK_UNWIND_INFO
2833 && ! flag_asynchronous_unwind_tables
e8f535c2 2834 && flag_exceptions
2f9fc8ef 2835 && for_eh)
2836 for (i = 0; i < fde_table_in_use; i++)
2837 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
61a9389f 2838 && !fde_table[i].uses_eh_lsda
1dc74225 2839 && ! DECL_WEAK (fde_table[i].decl))
883b2e73 2840 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
ef1074f7 2841 for_eh, /* empty */ 1);
2f9fc8ef 2842
f7b10771 2843 /* If we don't have any functions we'll want to unwind out of, don't
2844 emit any EH unwind information. Note that if exceptions aren't
2845 enabled, we won't have collected nothrow information, and if we
2846 asked for asynchronous tables, we always want this info. */
f543a963 2847 if (for_eh)
2848 {
f7b10771 2849 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
8c3f468d 2850
2851 for (i = 0; i < fde_table_in_use; i++)
df4b504c 2852 if (fde_table[i].uses_eh_lsda)
f7b10771 2853 any_eh_needed = any_lsda_needed = true;
61a9389f 2854 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3ff2e849 2855 any_eh_needed = true;
d744d41d 2856 else if (! fde_table[i].nothrow
2857 && ! fde_table[i].all_throwers_are_sibcalls)
f7b10771 2858 any_eh_needed = true;
df4b504c 2859
2860 if (! any_eh_needed)
2861 return;
f543a963 2862 }
2863
009a56ab 2864 /* We're going to be generating comments, so turn on app. */
2865 if (flag_debug_asm)
2866 app_enable ();
ad87de1e 2867
8a8bfbe7 2868 if (for_eh)
2f14b1f9 2869 switch_to_eh_frame_section ();
8a8bfbe7 2870 else
4494ff1b 2871 {
2872 if (!debug_frame_section)
2873 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2874 SECTION_DEBUG, NULL);
2875 switch_to_section (debug_frame_section);
2876 }
8a8bfbe7 2877
48ead6eb 2878 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2879 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2880
f80d1bcd 2881 /* Output the CIE. */
19bce576 2882 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2883 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
04da8de9 2884 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2885 dw2_asm_output_data (4, 0xffffffff,
2886 "Initial length escape value indicating 64-bit DWARF extension");
ca98eb0a 2887 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2888 "Length of Common Information Entry");
19bce576 2889 ASM_OUTPUT_LABEL (asm_out_file, l1);
2890
ca98eb0a 2891 /* Now that the CIE pointer is PC-relative for EH,
2892 use 0 to identify the CIE. */
2893 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
04da8de9 2894 (for_eh ? 0 : DWARF_CIE_ID),
ca98eb0a 2895 "CIE Identifier Tag");
8a8bfbe7 2896
ca98eb0a 2897 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
8a8bfbe7 2898
df4b504c 2899 augmentation[0] = 0;
9b84bf7d 2900 augmentation_size = 0;
df4b504c 2901 if (for_eh)
19bce576 2902 {
9b84bf7d 2903 char *p;
2904
df4b504c 2905 /* Augmentation:
2906 z Indicates that a uleb128 is present to size the
8ec3a57b 2907 augmentation section.
9b84bf7d 2908 L Indicates the encoding (and thus presence) of
2909 an LSDA pointer in the FDE augmentation.
2910 R Indicates a non-default pointer encoding for
2911 FDE code pointers.
2912 P Indicates the presence of an encoding + language
2913 personality routine in the CIE augmentation. */
2914
3ff2e849 2915 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
9b84bf7d 2916 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2917 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2918
2919 p = augmentation + 1;
2920 if (eh_personality_libfunc)
2921 {
2922 *p++ = 'P';
2923 augmentation_size += 1 + size_of_encoded_value (per_encoding);
849ea31c 2924 assemble_external_libcall (eh_personality_libfunc);
9b84bf7d 2925 }
df4b504c 2926 if (any_lsda_needed)
9b84bf7d 2927 {
2928 *p++ = 'L';
2929 augmentation_size += 1;
2930 }
2931 if (fde_encoding != DW_EH_PE_absptr)
2932 {
2933 *p++ = 'R';
2934 augmentation_size += 1;
2935 }
2936 if (p > augmentation + 1)
2937 {
2938 augmentation[0] = 'z';
bc70bd5e 2939 *p = '\0';
9b84bf7d 2940 }
9a4d22ba 2941
2942 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2943 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2944 {
2945 int offset = ( 4 /* Length */
2946 + 4 /* CIE Id */
2947 + 1 /* CIE version */
2948 + strlen (augmentation) + 1 /* Augmentation */
2949 + size_of_uleb128 (1) /* Code alignment */
2950 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2951 + 1 /* RA column */
2952 + 1 /* Augmentation size */
2953 + 1 /* Personality encoding */ );
2954 int pad = -offset & (PTR_SIZE - 1);
2955
2956 augmentation_size += pad;
2957
2958 /* Augmentations should be small, so there's scarce need to
2959 iterate for a solution. Die if we exceed one uleb128 byte. */
7bd4f6b6 2960 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
9a4d22ba 2961 }
19bce576 2962 }
8a8bfbe7 2963
8c3f468d 2964 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
ca98eb0a 2965 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
ca98eb0a 2966 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2967 "CIE Data Alignment Factor");
ab569c0c 2968
51ea5d02 2969 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
ab569c0c 2970 if (DW_CIE_VERSION == 1)
51ea5d02 2971 dw2_asm_output_data (1, return_reg, "CIE RA Column");
ab569c0c 2972 else
51ea5d02 2973 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
8a8bfbe7 2974
df4b504c 2975 if (augmentation[0])
2976 {
9b84bf7d 2977 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
df4b504c 2978 if (eh_personality_libfunc)
9b84bf7d 2979 {
2980 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2981 eh_data_format_name (per_encoding));
2982 dw2_asm_output_encoded_addr_rtx (per_encoding,
42e07529 2983 eh_personality_libfunc,
2984 true, NULL);
9b84bf7d 2985 }
8c3f468d 2986
9b84bf7d 2987 if (any_lsda_needed)
2988 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2989 eh_data_format_name (lsda_encoding));
8c3f468d 2990
9b84bf7d 2991 if (fde_encoding != DW_EH_PE_absptr)
2992 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2993 eh_data_format_name (fde_encoding));
df4b504c 2994 }
2995
8a8bfbe7 2996 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
b7020468 2997 output_cfi (cfi, NULL, for_eh);
8a8bfbe7 2998
2999 /* Pad the CIE out to an address sized boundary. */
bc70bd5e 3000 ASM_OUTPUT_ALIGN (asm_out_file,
b7020468 3001 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
19bce576 3002 ASM_OUTPUT_LABEL (asm_out_file, l2);
8a8bfbe7 3003
3004 /* Loop through all of the FDE's. */
8c3f468d 3005 for (i = 0; i < fde_table_in_use; i++)
8a8bfbe7 3006 {
3007 fde = &fde_table[i];
8a8bfbe7 3008
df4b504c 3009 /* Don't emit EH unwind info for leaf functions that don't need it. */
f7b10771 3010 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
04396483 3011 && (fde->nothrow || fde->all_throwers_are_sibcalls)
1dc74225 3012 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
04396483 3013 && !fde->uses_eh_lsda)
f543a963 3014 continue;
3015
ef1074f7 3016 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
883b2e73 3017 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
f80d1bcd 3018 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3019 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
04da8de9 3020 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3021 dw2_asm_output_data (4, 0xffffffff,
3022 "Initial length escape value indicating 64-bit DWARF extension");
ca98eb0a 3023 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3024 "FDE Length");
19bce576 3025 ASM_OUTPUT_LABEL (asm_out_file, l1);
3026
8a8bfbe7 3027 if (for_eh)
48ead6eb 3028 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
8a8bfbe7 3029 else
48ead6eb 3030 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
d08d29c0 3031 debug_frame_section, "FDE CIE offset");
8a8bfbe7 3032
9b84bf7d 3033 if (for_eh)
3034 {
1897b881 3035 if (fde->dw_fde_switched_sections)
3036 {
61a9389f 3037 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
1897b881 3038 fde->dw_fde_unlikely_section_label);
61a9389f 3039 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
1897b881 3040 fde->dw_fde_hot_section_label);
3041 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3042 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
42e07529 3043 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
1897b881 3044 "FDE initial location");
3045 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3046 fde->dw_fde_hot_section_end_label,
3047 fde->dw_fde_hot_section_label,
3048 "FDE address range");
42e07529 3049 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
1897b881 3050 "FDE initial location");
3051 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3052 fde->dw_fde_unlikely_section_end_label,
3053 fde->dw_fde_unlikely_section_label,
3054 "FDE address range");
3055 }
3056 else
d848c04f 3057 {
3058 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3059 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3060 dw2_asm_output_encoded_addr_rtx (fde_encoding,
3061 sym_ref,
3062 false,
3063 "FDE initial location");
3064 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3065 fde->dw_fde_end, fde->dw_fde_begin,
3066 "FDE address range");
3067 }
9b84bf7d 3068 }
3069 else
3070 {
1897b881 3071 if (fde->dw_fde_switched_sections)
3072 {
3073 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3074 fde->dw_fde_hot_section_label,
3075 "FDE initial location");
3076 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3077 fde->dw_fde_hot_section_end_label,
3078 fde->dw_fde_hot_section_label,
3079 "FDE address range");
3080 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3081 fde->dw_fde_unlikely_section_label,
3082 "FDE initial location");
61a9389f 3083 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
1897b881 3084 fde->dw_fde_unlikely_section_end_label,
3085 fde->dw_fde_unlikely_section_label,
3086 "FDE address range");
3087 }
3088 else
d848c04f 3089 {
3090 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3091 "FDE initial location");
3092 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3093 fde->dw_fde_end, fde->dw_fde_begin,
3094 "FDE address range");
3095 }
9b84bf7d 3096 }
8a8bfbe7 3097
df4b504c 3098 if (augmentation[0])
3099 {
9b84bf7d 3100 if (any_lsda_needed)
df4b504c 3101 {
9a4d22ba 3102 int size = size_of_encoded_value (lsda_encoding);
3103
3104 if (lsda_encoding == DW_EH_PE_aligned)
3105 {
3106 int offset = ( 4 /* Length */
3107 + 4 /* CIE offset */
3108 + 2 * size_of_encoded_value (fde_encoding)
3109 + 1 /* Augmentation size */ );
3110 int pad = -offset & (PTR_SIZE - 1);
3111
3112 size += pad;
7bd4f6b6 3113 gcc_assert (size_of_uleb128 (size) == 1);
9a4d22ba 3114 }
3115
3116 dw2_asm_output_data_uleb128 (size, "Augmentation size");
9b84bf7d 3117
3118 if (fde->uses_eh_lsda)
c83a163c 3119 {
3120 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
9b84bf7d 3121 fde->funcdef_number);
c83a163c 3122 dw2_asm_output_encoded_addr_rtx (
9b84bf7d 3123 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
42e07529 3124 false, "Language Specific Data Area");
c83a163c 3125 }
9b84bf7d 3126 else
9a4d22ba 3127 {
3128 if (lsda_encoding == DW_EH_PE_aligned)
3129 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
8c3f468d 3130 dw2_asm_output_data
3131 (size_of_encoded_value (lsda_encoding), 0,
3132 "Language Specific Data Area (none)");
9a4d22ba 3133 }
df4b504c 3134 }
3135 else
9b84bf7d 3136 dw2_asm_output_data_uleb128 (0, "Augmentation size");
df4b504c 3137 }
3138
8a8bfbe7 3139 /* Loop through the Call Frame Instructions associated with
3140 this FDE. */
3141 fde->dw_fde_current_label = fde->dw_fde_begin;
3142 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
b7020468 3143 output_cfi (cfi, fde, for_eh);
8a8bfbe7 3144
19bce576 3145 /* Pad the FDE out to an address sized boundary. */
bc70bd5e 3146 ASM_OUTPUT_ALIGN (asm_out_file,
c83a163c 3147 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
19bce576 3148 ASM_OUTPUT_LABEL (asm_out_file, l2);
8a8bfbe7 3149 }
ca98eb0a 3150
a08b74c8 3151 if (for_eh && targetm.terminate_dw2_eh_frame_info)
ca98eb0a 3152 dw2_asm_output_data (4, 0, "End of Table");
19bce576 3153#ifdef MIPS_DEBUGGING_INFO
3154 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3155 get a value of 0. Putting .align 0 after the label fixes it. */
3156 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3157#endif
009a56ab 3158
3159 /* Turn off app to make assembly quicker. */
3160 if (flag_debug_asm)
3161 app_disable ();
19bce576 3162}
3163
8a8bfbe7 3164/* Output a marker (i.e. a label) for the beginning of a function, before
3165 the prologue. */
3166
3167void
8ec3a57b 3168dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3169 const char *file ATTRIBUTE_UNUSED)
8a8bfbe7 3170{
3171 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2d754264 3172 char * dup_label;
19cb6b50 3173 dw_fde_ref fde;
8a8bfbe7 3174
2d754264 3175 current_function_func_begin_label = NULL;
ad5818ae 3176
8ec87476 3177#ifdef TARGET_UNWIND_INFO
ad5818ae 3178 /* ??? current_function_func_begin_label is also used by except.c
3179 for call-site information. We must emit this label if it might
3180 be used. */
3181 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3182 && ! dwarf2out_do_frame ())
3183 return;
3184#else
3185 if (! dwarf2out_do_frame ())
3186 return;
3187#endif
3188
2f14b1f9 3189 switch_to_section (function_section (current_function_decl));
8a8bfbe7 3190 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4781f9b9 3191 current_function_funcdef_no);
ad5818ae 3192 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4781f9b9 3193 current_function_funcdef_no);
2d754264 3194 dup_label = xstrdup (label);
3195 current_function_func_begin_label = dup_label;
8a8bfbe7 3196
8ec87476 3197#ifdef TARGET_UNWIND_INFO
ad5818ae 3198 /* We can elide the fde allocation if we're not emitting debug info. */
3199 if (! dwarf2out_do_frame ())
3200 return;
3201#endif
3202
8a8bfbe7 3203 /* Expand the fde table if necessary. */
3204 if (fde_table_in_use == fde_table_allocated)
3205 {
3206 fde_table_allocated += FDE_TABLE_INCREMENT;
2457c754 3207 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
573aba85 3208 memset (fde_table + fde_table_in_use, 0,
3209 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
30ade641 3210 }
8a8bfbe7 3211
3212 /* Record the FDE associated with this function. */
3213 current_funcdef_fde = fde_table_in_use;
3214
3215 /* Add the new FDE at the end of the fde_table. */
3216 fde = &fde_table[fde_table_in_use++];
2f9fc8ef 3217 fde->decl = current_function_decl;
2d754264 3218 fde->dw_fde_begin = dup_label;
3036ecbe 3219 fde->dw_fde_current_label = dup_label;
1897b881 3220 fde->dw_fde_hot_section_label = NULL;
3221 fde->dw_fde_hot_section_end_label = NULL;
3222 fde->dw_fde_unlikely_section_label = NULL;
3223 fde->dw_fde_unlikely_section_end_label = NULL;
3224 fde->dw_fde_switched_sections = false;
8a8bfbe7 3225 fde->dw_fde_end = NULL;
3226 fde->dw_fde_cfi = NULL;
4781f9b9 3227 fde->funcdef_number = current_function_funcdef_no;
b0d29d1c 3228 fde->nothrow = crtl->nothrow;
18d50ae6 3229 fde->uses_eh_lsda = crtl->uses_eh_lsda;
3230 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
27a7a23a 3231 fde->drap_reg = INVALID_REGNUM;
3232 fde->vdrap_reg = INVALID_REGNUM;
f543a963 3233
08532d4f 3234 args_size = old_args_size = 0;
f76df888 3235
8c3f468d 3236 /* We only want to output line number information for the genuine dwarf2
3237 prologue case, not the eh frame case. */
f76df888 3238#ifdef DWARF2_DEBUGGING_INFO
3239 if (file)
3240 dwarf2out_source_line (line, file);
3241#endif
fb39ff6e 3242
3dcd5df1 3243 if (dwarf2out_do_cfi_asm ())
fb39ff6e 3244 {
3245 int enc;
3246 rtx ref;
3247
3248 fprintf (asm_out_file, "\t.cfi_startproc\n");
3249
3250 if (eh_personality_libfunc)
3251 {
3252 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3253 ref = eh_personality_libfunc;
3254
3255 /* ??? The GAS support isn't entirely consistent. We have to
3256 handle indirect support ourselves, but PC-relative is done
3257 in the assembler. Further, the assembler can't handle any
3258 of the weirder relocation types. */
3259 if (enc & DW_EH_PE_indirect)
3260 ref = dw2_force_const_mem (ref, true);
3261
3262 fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3263 output_addr_const (asm_out_file, ref);
3264 fputc ('\n', asm_out_file);
3265 }
3266
3267 if (crtl->uses_eh_lsda)
3268 {
3269 char lab[20];
3270
3271 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3272 ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3273 current_function_funcdef_no);
3274 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3275 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3276
3277 if (enc & DW_EH_PE_indirect)
3278 ref = dw2_force_const_mem (ref, true);
3279
3280 fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3281 output_addr_const (asm_out_file, ref);
3282 fputc ('\n', asm_out_file);
3283 }
3284 }
8a8bfbe7 3285}
3286
3287/* Output a marker (i.e. a label) for the absolute end of the generated code
3288 for a function definition. This gets called *after* the epilogue code has
3289 been generated. */
3290
3291void
8ec3a57b 3292dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3293 const char *file ATTRIBUTE_UNUSED)
8a8bfbe7 3294{
3295 dw_fde_ref fde;
3296 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3297
3dcd5df1 3298 if (dwarf2out_do_cfi_asm ())
fb39ff6e 3299 fprintf (asm_out_file, "\t.cfi_endproc\n");
3300
8a8bfbe7 3301 /* Output a label to mark the endpoint of the code generated for this
04641143 3302 function. */
4781f9b9 3303 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3304 current_function_funcdef_no);
8a8bfbe7 3305 ASM_OUTPUT_LABEL (asm_out_file, label);
c0fd44c1 3306 fde = current_fde ();
3307 gcc_assert (fde != NULL);
8a8bfbe7 3308 fde->dw_fde_end = xstrdup (label);
8a8bfbe7 3309}
3310
3311void
8ec3a57b 3312dwarf2out_frame_init (void)
8a8bfbe7 3313{
3314 /* Allocate the initial hunk of the fde_table. */
2457c754 3315 fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
8a8bfbe7 3316 fde_table_allocated = FDE_TABLE_INCREMENT;
3317 fde_table_in_use = 0;
3318
3319 /* Generate the CFA instructions common to all FDE's. Do it now for the
3320 sake of lookup_cfa. */
3321
56daab87 3322 /* On entry, the Canonical Frame Address is at SP. */
3323 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
34986748 3324
3325#ifdef DWARF2_UNWIND_INFO
20ebe75d 3326 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
34986748 3327 initial_return_save (INCOMING_RETURN_ADDR_RTX);
8a8bfbe7 3328#endif
3329}
3330
3331void
8ec3a57b 3332dwarf2out_frame_finish (void)
8a8bfbe7 3333{
8a8bfbe7 3334 /* Output call frame information. */
34986748 3335 if (DWARF2_FRAME_INFO)
8a8bfbe7 3336 output_call_frame_info (0);
8c3f468d 3337
a28008f5 3338#ifndef TARGET_UNWIND_INFO
3339 /* Output another copy for the unwinder. */
6851a1fc 3340 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
8a8bfbe7 3341 output_call_frame_info (1);
a28008f5 3342#endif
f80d1bcd 3343}
af30c139 3344
3345/* Note that the current function section is being used for code. */
3346
3347static void
3348dwarf2out_note_section_used (void)
3349{
3350 section *sec = current_function_section ();
3351 if (sec == text_section)
3352 text_section_used = true;
3353 else if (sec == cold_text_section)
3354 cold_text_section_used = true;
3355}
3356
3357void
3358dwarf2out_switch_text_section (void)
3359{
c0fd44c1 3360 dw_fde_ref fde = current_fde ();
af30c139 3361
c0fd44c1 3362 gcc_assert (cfun && fde);
af30c139 3363
af30c139 3364 fde->dw_fde_switched_sections = true;
abe32cce 3365 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3366 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3367 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3368 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
af30c139 3369 have_multiple_function_sections = true;
3370
3371 /* Reset the current label on switching text sections, so that we
3372 don't attempt to advance_loc4 between labels in different sections. */
3373 fde->dw_fde_current_label = NULL;
3374
4e971a07 3375 /* There is no need to mark used sections when not debugging. */
3376 if (cold_text_section != NULL)
3377 dwarf2out_note_section_used ();
af30c139 3378}
573aba85 3379#endif
4b72e226 3380\f
3381/* And now, the subset of the debugging information support code necessary
3382 for emitting location expressions. */
8a8bfbe7 3383
69278c24 3384/* Data about a single source file. */
fb1e4f4a 3385struct GTY(()) dwarf_file_data {
69278c24 3386 const char * filename;
3387 int emitted_number;
3388};
3389
931e9893 3390/* We need some way to distinguish DW_OP_addr with a direct symbol
3391 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
3392#define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
3393
3394
4b72e226 3395typedef struct dw_val_struct *dw_val_ref;
3396typedef struct die_struct *dw_die_ref;
c1fdef8e 3397typedef const struct die_struct *const_dw_die_ref;
4b72e226 3398typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4c21a22f 3399typedef struct dw_loc_list_struct *dw_loc_list_ref;
8a8bfbe7 3400
fb1e4f4a 3401typedef struct GTY(()) deferred_locations_struct
89f29a1b 3402{
3403 tree variable;
3404 dw_die_ref die;
3405} deferred_locations;
3406
3407DEF_VEC_O(deferred_locations);
3408DEF_VEC_ALLOC_O(deferred_locations,gc);
3409
3410static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3411
8a8bfbe7 3412/* Each DIE may have a series of attribute/value pairs. Values
3413 can take on several forms. The forms that are used in this
3414 implementation are listed below. */
3415
573aba85 3416enum dw_val_class
8a8bfbe7 3417{
3418 dw_val_class_addr,
a36145ca 3419 dw_val_class_offset,
8a8bfbe7 3420 dw_val_class_loc,
4c21a22f 3421 dw_val_class_loc_list,
fe39c28c 3422 dw_val_class_range_list,
8a8bfbe7 3423 dw_val_class_const,
3424 dw_val_class_unsigned_const,
3425 dw_val_class_long_long,
1b6ad376 3426 dw_val_class_vec,
8a8bfbe7 3427 dw_val_class_flag,
3428 dw_val_class_die_ref,
3429 dw_val_class_fde_ref,
3430 dw_val_class_lbl_id,
d08d29c0 3431 dw_val_class_lineptr,
3432 dw_val_class_str,
69278c24 3433 dw_val_class_macptr,
3434 dw_val_class_file
573aba85 3435};
30ade641 3436
8a8bfbe7 3437/* Describe a double word constant value. */
0a44b200 3438/* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
8a8bfbe7 3439
fb1e4f4a 3440typedef struct GTY(()) dw_long_long_struct {
8a8bfbe7 3441 unsigned long hi;
3442 unsigned long low;
3443}
3444dw_long_long_const;
3445
1b6ad376 3446/* Describe a floating point constant value, or a vector constant value. */
8a8bfbe7 3447
fb1e4f4a 3448typedef struct GTY(()) dw_vec_struct {
1b6ad376 3449 unsigned char * GTY((length ("%h.length"))) array;
8a8bfbe7 3450 unsigned length;
1b6ad376 3451 unsigned elt_size;
8a8bfbe7 3452}
1b6ad376 3453dw_vec_const;
8a8bfbe7 3454
ad87de1e 3455/* The dw_val_node describes an attribute's value, as it is
8a8bfbe7 3456 represented internally. */
3457
fb1e4f4a 3458typedef struct GTY(()) dw_val_struct {
573aba85 3459 enum dw_val_class val_class;
3460 union dw_val_struct_union
30ade641 3461 {
573aba85 3462 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3d867824 3463 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
573aba85 3464 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3465 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
7035b2ab 3466 HOST_WIDE_INT GTY ((default)) val_int;
3d867824 3467 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
573aba85 3468 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
1b6ad376 3469 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
573aba85 3470 struct dw_val_die_union
8c3f468d 3471 {
3472 dw_die_ref die;
3473 int external;
573aba85 3474 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3475 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3476 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3477 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3478 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
69278c24 3479 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
30ade641 3480 }
573aba85 3481 GTY ((desc ("%1.val_class"))) v;
8a8bfbe7 3482}
3483dw_val_node;
3484
3485/* Locations in memory are described using a sequence of stack machine
3486 operations. */
3487
fb1e4f4a 3488typedef struct GTY(()) dw_loc_descr_struct {
8a8bfbe7 3489 dw_loc_descr_ref dw_loc_next;
3490 enum dwarf_location_atom dw_loc_opc;
9fac1c66 3491 int dw_loc_addr;
8a8bfbe7 3492 dw_val_node dw_loc_oprnd1;
3493 dw_val_node dw_loc_oprnd2;
3494}
3495dw_loc_descr_node;
3496
4c21a22f 3497/* Location lists are ranges + location descriptions for that range,
3498 so you can track variables that are in different places over
6312a35e 3499 their entire life. */
fb1e4f4a 3500typedef struct GTY(()) dw_loc_list_struct {
4c21a22f 3501 dw_loc_list_ref dw_loc_next;
3502 const char *begin; /* Label for begin address of range */
3503 const char *end; /* Label for end address of range */
8c3f468d 3504 char *ll_symbol; /* Label for beginning of location list.
3505 Only on head of list */
4c21a22f 3506 const char *section; /* Section this loclist is relative to */
3507 dw_loc_descr_ref expr;
3508} dw_loc_list_node;
3509
573aba85 3510#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3511
27a7a23a 3512static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
8a8bfbe7 3513
4b72e226 3514/* Convert a DWARF stack opcode into its string name. */
8a8bfbe7 3515
4b72e226 3516static const char *
8ec3a57b 3517dwarf_stack_op_name (unsigned int op)
678d90bb 3518{
4b72e226 3519 switch (op)
3520 {
3521 case DW_OP_addr:
931e9893 3522 case INTERNAL_DW_OP_tls_addr:
4b72e226 3523 return "DW_OP_addr";
3524 case DW_OP_deref:
3525 return "DW_OP_deref";
3526 case DW_OP_const1u:
3527 return "DW_OP_const1u";
3528 case DW_OP_const1s:
3529 return "DW_OP_const1s";
3530 case DW_OP_const2u:
3531 return "DW_OP_const2u";
3532 case DW_OP_const2s:
3533 return "DW_OP_const2s";
3534 case DW_OP_const4u:
3535 return "DW_OP_const4u";
3536 case DW_OP_const4s:
3537 return "DW_OP_const4s";
3538 case DW_OP_const8u:
3539 return "DW_OP_const8u";
3540 case DW_OP_const8s:
3541 return "DW_OP_const8s";
3542 case DW_OP_constu:
3543 return "DW_OP_constu";
3544 case DW_OP_consts:
3545 return "DW_OP_consts";
3546 case DW_OP_dup:
3547 return "DW_OP_dup";
3548 case DW_OP_drop:
3549 return "DW_OP_drop";
3550 case DW_OP_over:
3551 return "DW_OP_over";
3552 case DW_OP_pick:
3553 return "DW_OP_pick";
3554 case DW_OP_swap:
3555 return "DW_OP_swap";
3556 case DW_OP_rot:
3557 return "DW_OP_rot";
3558 case DW_OP_xderef:
3559 return "DW_OP_xderef";
3560 case DW_OP_abs:
3561 return "DW_OP_abs";
3562 case DW_OP_and:
3563 return "DW_OP_and";
3564 case DW_OP_div:
3565 return "DW_OP_div";
3566 case DW_OP_minus:
3567 return "DW_OP_minus";
3568 case DW_OP_mod:
3569 return "DW_OP_mod";
3570 case DW_OP_mul:
3571 return "DW_OP_mul";
3572 case DW_OP_neg:
3573 return "DW_OP_neg";
3574 case DW_OP_not:
3575 return "DW_OP_not";
3576 case DW_OP_or:
3577 return "DW_OP_or";
3578 case DW_OP_plus:
3579 return "DW_OP_plus";
3580 case DW_OP_plus_uconst:
3581 return "DW_OP_plus_uconst";
3582 case DW_OP_shl:
3583 return "DW_OP_shl";
3584 case DW_OP_shr:
3585 return "DW_OP_shr";
3586 case DW_OP_shra:
3587 return "DW_OP_shra";
3588 case DW_OP_xor:
3589 return "DW_OP_xor";
3590 case DW_OP_bra:
3591 return "DW_OP_bra";
3592 case DW_OP_eq:
3593 return "DW_OP_eq";
3594 case DW_OP_ge:
3595 return "DW_OP_ge";
3596 case DW_OP_gt:
3597 return "DW_OP_gt";
3598 case DW_OP_le:
3599 return "DW_OP_le";
3600 case DW_OP_lt:
3601 return "DW_OP_lt";
3602 case DW_OP_ne:
3603 return "DW_OP_ne";
3604 case DW_OP_skip:
3605 return "DW_OP_skip";
3606 case DW_OP_lit0:
3607 return "DW_OP_lit0";
3608 case DW_OP_lit1:
3609 return "DW_OP_lit1";
3610 case DW_OP_lit2:
3611 return "DW_OP_lit2";
3612 case DW_OP_lit3:
3613 return "DW_OP_lit3";
3614 case DW_OP_lit4:
3615 return "DW_OP_lit4";
3616 case DW_OP_lit5:
3617 return "DW_OP_lit5";
3618 case DW_OP_lit6:
3619 return "DW_OP_lit6";
3620 case DW_OP_lit7:
3621 return "DW_OP_lit7";
3622 case DW_OP_lit8:
3623 return "DW_OP_lit8";
3624 case DW_OP_lit9:
3625 return "DW_OP_lit9";
3626 case DW_OP_lit10:
3627 return "DW_OP_lit10";
3628 case DW_OP_lit11:
3629 return "DW_OP_lit11";
3630 case DW_OP_lit12:
3631 return "DW_OP_lit12";
3632 case DW_OP_lit13:
3633 return "DW_OP_lit13";
3634 case DW_OP_lit14:
3635 return "DW_OP_lit14";
3636 case DW_OP_lit15:
3637 return "DW_OP_lit15";
3638 case DW_OP_lit16:
3639 return "DW_OP_lit16";
3640 case DW_OP_lit17:
3641 return "DW_OP_lit17";
3642 case DW_OP_lit18:
3643 return "DW_OP_lit18";
3644 case DW_OP_lit19:
3645 return "DW_OP_lit19";
3646 case DW_OP_lit20:
3647 return "DW_OP_lit20";
3648 case DW_OP_lit21:
3649 return "DW_OP_lit21";
3650 case DW_OP_lit22:
3651 return "DW_OP_lit22";
3652 case DW_OP_lit23:
3653 return "DW_OP_lit23";
3654 case DW_OP_lit24:
3655 return "DW_OP_lit24";
3656 case DW_OP_lit25:
3657 return "DW_OP_lit25";
3658 case DW_OP_lit26:
3659 return "DW_OP_lit26";
3660 case DW_OP_lit27:
3661 return "DW_OP_lit27";
3662 case DW_OP_lit28:
3663 return "DW_OP_lit28";
3664 case DW_OP_lit29:
3665 return "DW_OP_lit29";
3666 case DW_OP_lit30:
3667 return "DW_OP_lit30";
3668 case DW_OP_lit31:
3669 return "DW_OP_lit31";
3670 case DW_OP_reg0:
3671 return "DW_OP_reg0";
3672 case DW_OP_reg1:
3673 return "DW_OP_reg1";
3674 case DW_OP_reg2:
3675 return "DW_OP_reg2";
3676 case DW_OP_reg3:
3677 return "DW_OP_reg3";
3678 case DW_OP_reg4:
3679 return "DW_OP_reg4";
3680 case DW_OP_reg5:
3681 return "DW_OP_reg5";
3682 case DW_OP_reg6:
3683 return "DW_OP_reg6";
3684 case DW_OP_reg7:
3685 return "DW_OP_reg7";
3686 case DW_OP_reg8:
3687 return "DW_OP_reg8";
3688 case DW_OP_reg9:
3689 return "DW_OP_reg9";
3690 case DW_OP_reg10:
3691 return "DW_OP_reg10";
3692 case DW_OP_reg11:
3693 return "DW_OP_reg11";
3694 case DW_OP_reg12:
3695 return "DW_OP_reg12";
3696 case DW_OP_reg13:
3697 return "DW_OP_reg13";
3698 case DW_OP_reg14:
3699 return "DW_OP_reg14";
3700 case DW_OP_reg15:
3701 return "DW_OP_reg15";
3702 case DW_OP_reg16:
3703 return "DW_OP_reg16";
3704 case DW_OP_reg17:
3705 return "DW_OP_reg17";
3706 case DW_OP_reg18:
3707 return "DW_OP_reg18";
3708 case DW_OP_reg19:
3709 return "DW_OP_reg19";
3710 case DW_OP_reg20:
3711 return "DW_OP_reg20";
3712 case DW_OP_reg21:
3713 return "DW_OP_reg21";
3714 case DW_OP_reg22:
3715 return "DW_OP_reg22";
3716 case DW_OP_reg23:
3717 return "DW_OP_reg23";
3718 case DW_OP_reg24:
3719 return "DW_OP_reg24";
3720 case DW_OP_reg25:
3721 return "DW_OP_reg25";
3722 case DW_OP_reg26:
3723 return "DW_OP_reg26";
3724 case DW_OP_reg27:
3725 return "DW_OP_reg27";
3726 case DW_OP_reg28:
3727 return "DW_OP_reg28";
3728 case DW_OP_reg29:
3729 return "DW_OP_reg29";
3730 case DW_OP_reg30:
3731 return "DW_OP_reg30";
3732 case DW_OP_reg31:
3733 return "DW_OP_reg31";
3734 case DW_OP_breg0:
3735 return "DW_OP_breg0";
3736 case DW_OP_breg1:
3737 return "DW_OP_breg1";
3738 case DW_OP_breg2:
3739 return "DW_OP_breg2";
3740 case DW_OP_breg3:
3741 return "DW_OP_breg3";
3742 case DW_OP_breg4:
3743 return "DW_OP_breg4";
3744 case DW_OP_breg5:
3745 return "DW_OP_breg5";
3746 case DW_OP_breg6:
3747 return "DW_OP_breg6";
3748 case DW_OP_breg7:
3749 return "DW_OP_breg7";
3750 case DW_OP_breg8:
3751 return "DW_OP_breg8";
3752 case DW_OP_breg9:
3753 return "DW_OP_breg9";
3754 case DW_OP_breg10:
3755 return "DW_OP_breg10";
3756 case DW_OP_breg11:
3757 return "DW_OP_breg11";
3758 case DW_OP_breg12:
3759 return "DW_OP_breg12";
3760 case DW_OP_breg13:
3761 return "DW_OP_breg13";
3762 case DW_OP_breg14:
3763 return "DW_OP_breg14";
3764 case DW_OP_breg15:
3765 return "DW_OP_breg15";
3766 case DW_OP_breg16:
3767 return "DW_OP_breg16";
3768 case DW_OP_breg17:
3769 return "DW_OP_breg17";
3770 case DW_OP_breg18:
3771 return "DW_OP_breg18";
3772 case DW_OP_breg19:
3773 return "DW_OP_breg19";
3774 case DW_OP_breg20:
3775 return "DW_OP_breg20";
3776 case DW_OP_breg21:
3777 return "DW_OP_breg21";
3778 case DW_OP_breg22:
3779 return "DW_OP_breg22";
3780 case DW_OP_breg23:
3781 return "DW_OP_breg23";
3782 case DW_OP_breg24:
3783 return "DW_OP_breg24";
3784 case DW_OP_breg25:
3785 return "DW_OP_breg25";
3786 case DW_OP_breg26:
3787 return "DW_OP_breg26";
3788 case DW_OP_breg27:
3789 return "DW_OP_breg27";
3790 case DW_OP_breg28:
3791 return "DW_OP_breg28";
3792 case DW_OP_breg29:
3793 return "DW_OP_breg29";
3794 case DW_OP_breg30:
3795 return "DW_OP_breg30";
3796 case DW_OP_breg31:
3797 return "DW_OP_breg31";
3798 case DW_OP_regx:
3799 return "DW_OP_regx";
3800 case DW_OP_fbreg:
3801 return "DW_OP_fbreg";
3802 case DW_OP_bregx:
3803 return "DW_OP_bregx";
3804 case DW_OP_piece:
3805 return "DW_OP_piece";
3806 case DW_OP_deref_size:
3807 return "DW_OP_deref_size";
3808 case DW_OP_xderef_size:
3809 return "DW_OP_xderef_size";
3810 case DW_OP_nop:
3811 return "DW_OP_nop";
931e9893 3812 case DW_OP_push_object_address:
3813 return "DW_OP_push_object_address";
3814 case DW_OP_call2:
3815 return "DW_OP_call2";
3816 case DW_OP_call4:
3817 return "DW_OP_call4";
3818 case DW_OP_call_ref:
3819 return "DW_OP_call_ref";
3820 case DW_OP_GNU_push_tls_address:
3821 return "DW_OP_GNU_push_tls_address";
d53bb226 3822 case DW_OP_GNU_uninit:
3823 return "DW_OP_GNU_uninit";
8a8bfbe7 3824 default:
4b72e226 3825 return "OP_<unknown>";
8a8bfbe7 3826 }
6ed29fb8 3827}
30ade641 3828
4b72e226 3829/* Return a pointer to a newly allocated location description. Location
3830 descriptions are simple expression terms that can be strung
3831 together to form more complicated location (address) descriptions. */
3832
3833static inline dw_loc_descr_ref
3d867824 3834new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3835 unsigned HOST_WIDE_INT oprnd2)
752e49ca 3836{
2457c754 3837 dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
ec1e49cc 3838
4b72e226 3839 descr->dw_loc_opc = op;
3840 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3841 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3842 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3843 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
ec1e49cc 3844
4b72e226 3845 return descr;
3846}
3847
dde9bb3b 3848/* Return a pointer to a newly allocated location description for
3849 REG and OFFSET. */
3850
3851static inline dw_loc_descr_ref
3852new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
3853{
3854 if (offset)
3855 {
3856 if (reg <= 31)
b9c74b4d 3857 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
3858 offset, 0);
dde9bb3b 3859 else
3860 return new_loc_descr (DW_OP_bregx, reg, offset);
3861 }
3862 else if (reg <= 31)
b9c74b4d 3863 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + reg), 0, 0);
dde9bb3b 3864 else
3865 return new_loc_descr (DW_OP_regx, reg, 0);
3866}
3867
4b72e226 3868/* Add a location description term to a location description expression. */
3869
3870static inline void
8ec3a57b 3871add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4b72e226 3872{
19cb6b50 3873 dw_loc_descr_ref *d;
4b72e226 3874
3875 /* Find the end of the chain. */
3876 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3877 ;
3878
3879 *d = descr;
3880}
3881
1938132c 3882/* Add a constant OFFSET to a location expression. */
3883
3884static void
3885loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
3886{
3887 dw_loc_descr_ref loc;
3888 HOST_WIDE_INT *p;
3889
3890 gcc_assert (*list_head != NULL);
3891
3892 if (!offset)
3893 return;
3894
3895 /* Find the end of the chain. */
3896 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
3897 ;
3898
3899 p = NULL;
3900 if (loc->dw_loc_opc == DW_OP_fbreg
3901 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
3902 p = &loc->dw_loc_oprnd1.v.val_int;
3903 else if (loc->dw_loc_opc == DW_OP_bregx)
3904 p = &loc->dw_loc_oprnd2.v.val_int;
3905
3906 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
3907 offset. Don't optimize if an signed integer overflow would happen. */
3908 if (p != NULL
3909 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
3910 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
3911 *p += offset;
3912
3913 else if (offset > 0)
3914 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3915
3916 else
3917 {
3918 loc->dw_loc_next = int_loc_descriptor (offset);
3919 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
3920 }
3921}
3922
4b72e226 3923/* Return the size of a location descriptor. */
3924
3925static unsigned long
8ec3a57b 3926size_of_loc_descr (dw_loc_descr_ref loc)
4b72e226 3927{
19cb6b50 3928 unsigned long size = 1;
4b72e226 3929
3930 switch (loc->dw_loc_opc)
3931 {
3932 case DW_OP_addr:
931e9893 3933 case INTERNAL_DW_OP_tls_addr:
4b72e226 3934 size += DWARF2_ADDR_SIZE;
3935 break;
3936 case DW_OP_const1u:
3937 case DW_OP_const1s:
3938 size += 1;
3939 break;
3940 case DW_OP_const2u:
3941 case DW_OP_const2s:
3942 size += 2;
3943 break;
3944 case DW_OP_const4u:
3945 case DW_OP_const4s:
3946 size += 4;
3947 break;
3948 case DW_OP_const8u:
3949 case DW_OP_const8s:
3950 size += 8;
3951 break;
3952 case DW_OP_constu:
3953 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3954 break;
3955 case DW_OP_consts:
3956 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3957 break;
3958 case DW_OP_pick:
3959 size += 1;
3960 break;
3961 case DW_OP_plus_uconst:
3962 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3963 break;
3964 case DW_OP_skip:
3965 case DW_OP_bra:
3966 size += 2;
3967 break;
3968 case DW_OP_breg0:
3969 case DW_OP_breg1:
3970 case DW_OP_breg2:
3971 case DW_OP_breg3:
3972 case DW_OP_breg4:
3973 case DW_OP_breg5:
3974 case DW_OP_breg6:
3975 case DW_OP_breg7:
3976 case DW_OP_breg8:
3977 case DW_OP_breg9:
3978 case DW_OP_breg10:
3979 case DW_OP_breg11:
3980 case DW_OP_breg12:
3981 case DW_OP_breg13:
3982 case DW_OP_breg14:
3983 case DW_OP_breg15:
3984 case DW_OP_breg16:
3985 case DW_OP_breg17:
3986 case DW_OP_breg18:
3987 case DW_OP_breg19:
3988 case DW_OP_breg20:
3989 case DW_OP_breg21:
3990 case DW_OP_breg22:
3991 case DW_OP_breg23:
3992 case DW_OP_breg24:
3993 case DW_OP_breg25:
3994 case DW_OP_breg26:
3995 case DW_OP_breg27:
3996 case DW_OP_breg28:
3997 case DW_OP_breg29:
3998 case DW_OP_breg30:
3999 case DW_OP_breg31:
4000 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4001 break;
4002 case DW_OP_regx:
4003 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4004 break;
4005 case DW_OP_fbreg:
4006 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4007 break;
4008 case DW_OP_bregx:
4009 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4010 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4011 break;
4012 case DW_OP_piece:
4013 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4014 break;
4015 case DW_OP_deref_size:
4016 case DW_OP_xderef_size:
4017 size += 1;
4018 break;
931e9893 4019 case DW_OP_call2:
4020 size += 2;
4021 break;
4022 case DW_OP_call4:
4023 size += 4;
4024 break;
4025 case DW_OP_call_ref:
4026 size += DWARF2_ADDR_SIZE;
4027 break;
8a8bfbe7 4028 default:
4b72e226 4029 break;
752e49ca 4030 }
4b72e226 4031
4032 return size;
752e49ca 4033}
4034
4b72e226 4035/* Return the size of a series of location descriptors. */
ec1e49cc 4036
4b72e226 4037static unsigned long
8ec3a57b 4038size_of_locs (dw_loc_descr_ref loc)
752e49ca 4039{
2fa2456e 4040 dw_loc_descr_ref l;
8c3f468d 4041 unsigned long size;
4b72e226 4042
2fa2456e 4043 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4044 field, to avoid writing to a PCH file. */
4045 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
9ed904da 4046 {
2fa2456e 4047 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4048 break;
4049 size += size_of_loc_descr (l);
4050 }
4051 if (! l)
4052 return size;
4053
4054 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4055 {
4056 l->dw_loc_addr = size;
4057 size += size_of_loc_descr (l);
9ed904da 4058 }
4b72e226 4059
4060 return size;
752e49ca 4061}
4062
4b72e226 4063/* Output location description stack opcode's operands (if any). */
ec1e49cc 4064
4b72e226 4065static void
8ec3a57b 4066output_loc_operands (dw_loc_descr_ref loc)
30ade641 4067{
19cb6b50 4068 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4069 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4b72e226 4070
4071 switch (loc->dw_loc_opc)
30ade641 4072 {
a6c3bce6 4073#ifdef DWARF2_DEBUGGING_INFO
8a8bfbe7 4074 case DW_OP_addr:
ca98eb0a 4075 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4b72e226 4076 break;
8a8bfbe7 4077 case DW_OP_const2u:
8a8bfbe7 4078 case DW_OP_const2s:
ca98eb0a 4079 dw2_asm_output_data (2, val1->v.val_int, NULL);
4b72e226 4080 break;
8a8bfbe7 4081 case DW_OP_const4u:
8a8bfbe7 4082 case DW_OP_const4s:
ca98eb0a 4083 dw2_asm_output_data (4, val1->v.val_int, NULL);
4b72e226 4084 break;
8a8bfbe7 4085 case DW_OP_const8u:
8a8bfbe7 4086 case DW_OP_const8s:
7bd4f6b6 4087 gcc_assert (HOST_BITS_PER_LONG >= 64);
ca98eb0a 4088 dw2_asm_output_data (8, val1->v.val_int, NULL);
4b72e226 4089 break;
a6c3bce6 4090 case DW_OP_skip:
4091 case DW_OP_bra:
9ed904da 4092 {
4093 int offset;
4094
7bd4f6b6 4095 gcc_assert (val1->val_class == dw_val_class_loc);
4096 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
9ed904da 4097
ca98eb0a 4098 dw2_asm_output_data (2, offset, NULL);
9ed904da 4099 }
a6c3bce6 4100 break;
ccd12125 4101#else
4102 case DW_OP_addr:
4103 case DW_OP_const2u:
4104 case DW_OP_const2s:
4105 case DW_OP_const4u:
4106 case DW_OP_const4s:
4107 case DW_OP_const8u:
4108 case DW_OP_const8s:
4109 case DW_OP_skip:
4110 case DW_OP_bra:
4111 /* We currently don't make any attempt to make sure these are
c83a163c 4112 aligned properly like we do for the main unwind info, so
4113 don't support emitting things larger than a byte if we're
4114 only doing unwinding. */
7bd4f6b6 4115 gcc_unreachable ();
a6c3bce6 4116#endif
4117 case DW_OP_const1u:
4118 case DW_OP_const1s:
ca98eb0a 4119 dw2_asm_output_data (1, val1->v.val_int, NULL);
a6c3bce6 4120 break;
8a8bfbe7 4121 case DW_OP_constu:
ca98eb0a 4122 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4b72e226 4123 break;
8a8bfbe7 4124 case DW_OP_consts:
ca98eb0a 4125 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4b72e226 4126 break;
4127 case DW_OP_pick:
ca98eb0a 4128 dw2_asm_output_data (1, val1->v.val_int, NULL);
4b72e226 4129 break;
4130 case DW_OP_plus_uconst:
ca98eb0a 4131 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4b72e226 4132 break;
8a8bfbe7 4133 case DW_OP_breg0:
8a8bfbe7 4134 case DW_OP_breg1:
8a8bfbe7 4135 case DW_OP_breg2:
8a8bfbe7 4136 case DW_OP_breg3:
8a8bfbe7 4137 case DW_OP_breg4:
8a8bfbe7 4138 case DW_OP_breg5:
8a8bfbe7 4139 case DW_OP_breg6:
8a8bfbe7 4140 case DW_OP_breg7:
8a8bfbe7 4141 case DW_OP_breg8:
8a8bfbe7 4142 case DW_OP_breg9:
8a8bfbe7 4143 case DW_OP_breg10:
8a8bfbe7 4144 case DW_OP_breg11:
8a8bfbe7 4145 case DW_OP_breg12:
8a8bfbe7 4146 case DW_OP_breg13:
8a8bfbe7 4147 case DW_OP_breg14:
8a8bfbe7 4148 case DW_OP_breg15:
8a8bfbe7 4149 case DW_OP_breg16:
8a8bfbe7 4150 case DW_OP_breg17:
8a8bfbe7 4151 case DW_OP_breg18:
8a8bfbe7 4152 case DW_OP_breg19:
8a8bfbe7 4153 case DW_OP_breg20:
8a8bfbe7 4154 case DW_OP_breg21:
8a8bfbe7 4155 case DW_OP_breg22:
8a8bfbe7 4156 case DW_OP_breg23:
8a8bfbe7 4157 case DW_OP_breg24:
8a8bfbe7 4158 case DW_OP_breg25:
8a8bfbe7 4159 case DW_OP_breg26:
8a8bfbe7 4160 case DW_OP_breg27:
8a8bfbe7 4161 case DW_OP_breg28:
8a8bfbe7 4162 case DW_OP_breg29:
8a8bfbe7 4163 case DW_OP_breg30:
8a8bfbe7 4164 case DW_OP_breg31:
ca98eb0a 4165 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4b72e226 4166 break;
8a8bfbe7 4167 case DW_OP_regx:
ca98eb0a 4168 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4b72e226 4169 break;
8a8bfbe7 4170 case DW_OP_fbreg:
ca98eb0a 4171 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4b72e226 4172 break;
8a8bfbe7 4173 case DW_OP_bregx:
ca98eb0a 4174 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4175 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4b72e226 4176 break;
8a8bfbe7 4177 case DW_OP_piece:
ca98eb0a 4178 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4b72e226 4179 break;
8a8bfbe7 4180 case DW_OP_deref_size:
8a8bfbe7 4181 case DW_OP_xderef_size:
ca98eb0a 4182 dw2_asm_output_data (1, val1->v.val_int, NULL);
4b72e226 4183 break;
931e9893 4184
4185 case INTERNAL_DW_OP_tls_addr:
40af64cc 4186 if (targetm.asm_out.output_dwarf_dtprel)
4187 {
4188 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4189 DWARF2_ADDR_SIZE,
4190 val1->v.val_addr);
4191 fputc ('\n', asm_out_file);
4192 }
4193 else
4194 gcc_unreachable ();
931e9893 4195 break;
4196
4b72e226 4197 default:
ccd12125 4198 /* Other codes have no operands. */
4199 break;
4b72e226 4200 }
4201}
4202
4203/* Output a sequence of location operations. */
4204
4205static void
8ec3a57b 4206output_loc_sequence (dw_loc_descr_ref loc)
4b72e226 4207{
4208 for (; loc != NULL; loc = loc->dw_loc_next)
4209 {
4210 /* Output the opcode. */
ca98eb0a 4211 dw2_asm_output_data (1, loc->dw_loc_opc,
4212 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4b72e226 4213
4214 /* Output the operand(s) (if any). */
4215 output_loc_operands (loc);
4216 }
4217}
4218
fb39ff6e 4219/* Output location description stack opcode's operands (if any).
4220 The output is single bytes on a line, suitable for .cfi_escape. */
4221
4222static void
4223output_loc_operands_raw (dw_loc_descr_ref loc)
4224{
4225 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4226 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4227
4228 switch (loc->dw_loc_opc)
4229 {
4230 case DW_OP_addr:
4231 /* We cannot output addresses in .cfi_escape, only bytes. */
4232 gcc_unreachable ();
4233
4234 case DW_OP_const1u:
4235 case DW_OP_const1s:
4236 case DW_OP_pick:
4237 case DW_OP_deref_size:
4238 case DW_OP_xderef_size:
4239 fputc (',', asm_out_file);
4240 dw2_asm_output_data_raw (1, val1->v.val_int);
4241 break;
4242
4243 case DW_OP_const2u:
4244 case DW_OP_const2s:
4245 fputc (',', asm_out_file);
4246 dw2_asm_output_data_raw (2, val1->v.val_int);
4247 break;
4248
4249 case DW_OP_const4u:
4250 case DW_OP_const4s:
4251 fputc (',', asm_out_file);
4252 dw2_asm_output_data_raw (4, val1->v.val_int);
4253 break;
4254
4255 case DW_OP_const8u:
4256 case DW_OP_const8s:
4257 gcc_assert (HOST_BITS_PER_LONG >= 64);
4258 fputc (',', asm_out_file);
4259 dw2_asm_output_data_raw (8, val1->v.val_int);
4260 break;
4261
4262 case DW_OP_skip:
4263 case DW_OP_bra:
4264 {
4265 int offset;
4266
4267 gcc_assert (val1->val_class == dw_val_class_loc);
4268 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4269
4270 fputc (',', asm_out_file);
4271 dw2_asm_output_data_raw (2, offset);
4272 }
4273 break;
4274
4275 case DW_OP_constu:
4276 case DW_OP_plus_uconst:
4277 case DW_OP_regx:
4278 case DW_OP_piece:
4279 fputc (',', asm_out_file);
4280 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4281 break;
4282
4283 case DW_OP_consts:
4284 case DW_OP_breg0:
4285 case DW_OP_breg1:
4286 case DW_OP_breg2:
4287 case DW_OP_breg3:
4288 case DW_OP_breg4:
4289 case DW_OP_breg5:
4290 case DW_OP_breg6:
4291 case DW_OP_breg7:
4292 case DW_OP_breg8:
4293 case DW_OP_breg9:
4294 case DW_OP_breg10:
4295 case DW_OP_breg11:
4296 case DW_OP_breg12:
4297 case DW_OP_breg13:
4298 case DW_OP_breg14:
4299 case DW_OP_breg15:
4300 case DW_OP_breg16:
4301 case DW_OP_breg17:
4302 case DW_OP_breg18:
4303 case DW_OP_breg19:
4304 case DW_OP_breg20:
4305 case DW_OP_breg21:
4306 case DW_OP_breg22:
4307 case DW_OP_breg23:
4308 case DW_OP_breg24:
4309 case DW_OP_breg25:
4310 case DW_OP_breg26:
4311 case DW_OP_breg27:
4312 case DW_OP_breg28:
4313 case DW_OP_breg29:
4314 case DW_OP_breg30:
4315 case DW_OP_breg31:
4316 case DW_OP_fbreg:
4317 fputc (',', asm_out_file);
4318 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4319 break;
4320
4321 case DW_OP_bregx:
4322 fputc (',', asm_out_file);
4323 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4324 fputc (',', asm_out_file);
4325 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4326 break;
4327
4328 case INTERNAL_DW_OP_tls_addr:
4329 gcc_unreachable ();
4330
4331 default:
4332 /* Other codes have no operands. */
4333 break;
4334 }
4335}
4336
4337static void
4338output_loc_sequence_raw (dw_loc_descr_ref loc)
4339{
4340 while (1)
4341 {
4342 /* Output the opcode. */
4343 fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4344 output_loc_operands_raw (loc);
4345
4346 if (!loc->dw_loc_next)
4347 break;
4348 loc = loc->dw_loc_next;
4349
4350 fputc (',', asm_out_file);
4351 }
4352}
4353
4b72e226 4354/* This routine will generate the correct assembly data for a location
4355 description based on a cfi entry with a complex address. */
4356
4357static void
8ec3a57b 4358output_cfa_loc (dw_cfi_ref cfi)
4b72e226 4359{
4360 dw_loc_descr_ref loc;
4361 unsigned long size;
4362
27a7a23a 4363 if (cfi->dw_cfi_opc == DW_CFA_expression)
4364 dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4365
4b72e226 4366 /* Output the size of the block. */
4367 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4368 size = size_of_locs (loc);
ca98eb0a 4369 dw2_asm_output_data_uleb128 (size, NULL);
4b72e226 4370
4371 /* Now output the operations themselves. */
4372 output_loc_sequence (loc);
4373}
4374
fb39ff6e 4375/* Similar, but used for .cfi_escape. */
4376
4377static void
4378output_cfa_loc_raw (dw_cfi_ref cfi)
4379{
4380 dw_loc_descr_ref loc;
4381 unsigned long size;
4382
4383 if (cfi->dw_cfi_opc == DW_CFA_expression)
4384 fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4385
4386 /* Output the size of the block. */
4387 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4388 size = size_of_locs (loc);
4389 dw2_asm_output_data_uleb128_raw (size);
4390 fputc (',', asm_out_file);
4391
4392 /* Now output the operations themselves. */
4393 output_loc_sequence_raw (loc);
4394}
4395
89fa767a 4396/* This function builds a dwarf location descriptor sequence from a
4397 dw_cfa_location, adding the given OFFSET to the result of the
4398 expression. */
4b72e226 4399
4400static struct dw_loc_descr_struct *
89fa767a 4401build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4b72e226 4402{
4403 struct dw_loc_descr_struct *head, *tmp;
4404
89fa767a 4405 offset += cfa->offset;
4406
12d886b8 4407 if (cfa->indirect)
5f19af7a 4408 {
dde9bb3b 4409 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
12d886b8 4410 head->dw_loc_oprnd1.val_class = dw_val_class_const;
4411 tmp = new_loc_descr (DW_OP_deref, 0, 0);
4412 add_loc_descr (&head, tmp);
89fa767a 4413 if (offset != 0)
12d886b8 4414 {
89fa767a 4415 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
12d886b8 4416 add_loc_descr (&head, tmp);
4417 }
5f19af7a 4418 }
5f19af7a 4419 else
dde9bb3b 4420 head = new_reg_loc_descr (cfa->reg, offset);
8c3f468d 4421
4b72e226 4422 return head;
4423}
4424
27a7a23a 4425/* This function builds a dwarf location descriptor sequence for
4426 the address at OFFSET from the CFA when stack is aligned to
4427 ALIGNMENT byte. */
4428
4429static struct dw_loc_descr_struct *
4430build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4431{
4432 struct dw_loc_descr_struct *head;
4433 unsigned int dwarf_fp
4434 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4435
4436 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
4437 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4438 {
dde9bb3b 4439 head = new_reg_loc_descr (dwarf_fp, 0);
27a7a23a 4440 add_loc_descr (&head, int_loc_descriptor (alignment));
4441 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
1938132c 4442 loc_descr_plus_const (&head, offset);
27a7a23a 4443 }
27a7a23a 4444 else
dde9bb3b 4445 head = new_reg_loc_descr (dwarf_fp, offset);
27a7a23a 4446 return head;
4447}
4448
8c3f468d 4449/* This function fills in aa dw_cfa_location structure from a dwarf location
4450 descriptor sequence. */
4b72e226 4451
4452static void
8ec3a57b 4453get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4b72e226 4454{
f80d1bcd 4455 struct dw_loc_descr_struct *ptr;
4b72e226 4456 cfa->offset = 0;
4457 cfa->base_offset = 0;
4458 cfa->indirect = 0;
4459 cfa->reg = -1;
4460
4461 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4462 {
4463 enum dwarf_location_atom op = ptr->dw_loc_opc;
8c3f468d 4464
4b72e226 4465 switch (op)
f80d1bcd 4466 {
4b72e226 4467 case DW_OP_reg0:
4468 case DW_OP_reg1:
4469 case DW_OP_reg2:
4470 case DW_OP_reg3:
4471 case DW_OP_reg4:
4472 case DW_OP_reg5:
4473 case DW_OP_reg6:
4474 case DW_OP_reg7:
4475 case DW_OP_reg8:
4476 case DW_OP_reg9:
4477 case DW_OP_reg10:
4478 case DW_OP_reg11:
4479 case DW_OP_reg12:
4480 case DW_OP_reg13:
4481 case DW_OP_reg14:
4482 case DW_OP_reg15:
4483 case DW_OP_reg16:
4484 case DW_OP_reg17:
4485 case DW_OP_reg18:
4486 case DW_OP_reg19:
4487 case DW_OP_reg20:
4488 case DW_OP_reg21:
4489 case DW_OP_reg22:
4490 case DW_OP_reg23:
4491 case DW_OP_reg24:
4492 case DW_OP_reg25:
4493 case DW_OP_reg26:
4494 case DW_OP_reg27:
4495 case DW_OP_reg28:
4496 case DW_OP_reg29:
4497 case DW_OP_reg30:
4498 case DW_OP_reg31:
4499 cfa->reg = op - DW_OP_reg0;
4500 break;
4501 case DW_OP_regx:
4502 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4503 break;
4504 case DW_OP_breg0:
4505 case DW_OP_breg1:
4506 case DW_OP_breg2:
4507 case DW_OP_breg3:
4508 case DW_OP_breg4:
4509 case DW_OP_breg5:
4510 case DW_OP_breg6:
4511 case DW_OP_breg7:
4512 case DW_OP_breg8:
4513 case DW_OP_breg9:
4514 case DW_OP_breg10:
4515 case DW_OP_breg11:
4516 case DW_OP_breg12:
4517 case DW_OP_breg13:
4518 case DW_OP_breg14:
4519 case DW_OP_breg15:
4520 case DW_OP_breg16:
4521 case DW_OP_breg17:
4522 case DW_OP_breg18:
4523 case DW_OP_breg19:
4524 case DW_OP_breg20:
4525 case DW_OP_breg21:
4526 case DW_OP_breg22:
4527 case DW_OP_breg23:
4528 case DW_OP_breg24:
4529 case DW_OP_breg25:
4530 case DW_OP_breg26:
4531 case DW_OP_breg27:
4532 case DW_OP_breg28:
4533 case DW_OP_breg29:
4534 case DW_OP_breg30:
4535 case DW_OP_breg31:
4536 cfa->reg = op - DW_OP_breg0;
4537 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4538 break;
4539 case DW_OP_bregx:
4540 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4541 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4542 break;
4543 case DW_OP_deref:
4544 cfa->indirect = 1;
4545 break;
4546 case DW_OP_plus_uconst:
f80d1bcd 4547 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4b72e226 4548 break;
4549 default:
0a81f5a0 4550 internal_error ("DW_LOC_OP %s not implemented",
f060a027 4551 dwarf_stack_op_name (ptr->dw_loc_opc));
4b72e226 4552 }
4553 }
4554}
4555#endif /* .debug_frame support */
4556\f
4557/* And now, the support for symbolic debugging information. */
4558#ifdef DWARF2_DEBUGGING_INFO
4559
c366eeee 4560/* .debug_str support. */
8ec3a57b 4561static int output_indirect_string (void **, void *);
4562
4563static void dwarf2out_init (const char *);
4564static void dwarf2out_finish (const char *);
4565static void dwarf2out_define (unsigned int, const char *);
4566static void dwarf2out_undef (unsigned int, const char *);
4567static void dwarf2out_start_source_file (unsigned, const char *);
4568static void dwarf2out_end_source_file (unsigned);
4569static void dwarf2out_begin_block (unsigned, unsigned);
4570static void dwarf2out_end_block (unsigned, unsigned);
5493cb9a 4571static bool dwarf2out_ignore_block (const_tree);
8ec3a57b 4572static void dwarf2out_global_decl (tree);
73ae3ef7 4573static void dwarf2out_type_decl (tree, int);
df4d540f 4574static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5d8a39b7 4575static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4576 dw_die_ref);
8ec3a57b 4577static void dwarf2out_abstract_function (tree);
b2025850 4578static void dwarf2out_var_location (rtx);
4579static void dwarf2out_begin_function (tree);
8d17cbdd 4580static void dwarf2out_set_name (tree, tree);
c140b944 4581
4582/* The debug hooks structure. */
4583
e42f6423 4584const struct gcc_debug_hooks dwarf2_debug_hooks =
c140b944 4585{
4586 dwarf2out_init,
4587 dwarf2out_finish,
4588 dwarf2out_define,
4589 dwarf2out_undef,
4590 dwarf2out_start_source_file,
1dff614c 4591 dwarf2out_end_source_file,
4592 dwarf2out_begin_block,
b9b7f8b4 4593 dwarf2out_end_block,
b29760a8 4594 dwarf2out_ignore_block,
b9b7f8b4 4595 dwarf2out_source_line,
f76df888 4596 dwarf2out_begin_prologue,
e74e8242 4597 debug_nothing_int_charstar, /* end_prologue */
b9b7f8b4 4598 dwarf2out_end_epilogue,
b2025850 4599 dwarf2out_begin_function,
c37d72e9 4600 debug_nothing_int, /* end_function */
4601 dwarf2out_decl, /* function_decl */
4602 dwarf2out_global_decl,
73ae3ef7 4603 dwarf2out_type_decl, /* type_decl */
2b49746a 4604 dwarf2out_imported_module_or_decl,
b29760a8 4605 debug_nothing_tree, /* deferred_inline_function */
4606 /* The DWARF 2 backend tries to reduce debugging bloat by not
4607 emitting the abstract description of inline functions until
4608 something tries to reference them. */
4609 dwarf2out_abstract_function, /* outlining_inline_function */
cf8e41a4 4610 debug_nothing_rtx, /* label */
5923a5e7 4611 debug_nothing_int, /* handle_pch */
7a4afb3f 4612 dwarf2out_var_location,
1897b881 4613 dwarf2out_switch_text_section,
8d17cbdd 4614 dwarf2out_set_name,
7a4afb3f 4615 1 /* start_end_main_source_file */
c140b944 4616};
573aba85 4617#endif
c140b944 4618\f
4b72e226 4619/* NOTE: In the comments in this file, many references are made to
4620 "Debugging Information Entries". This term is abbreviated as `DIE'
4621 throughout the remainder of this file. */
4622
4623/* An internal representation of the DWARF output is built, and then
4624 walked to generate the DWARF debugging info. The walk of the internal
4625 representation is done after the entire program has been compiled.
4626 The types below are used to describe the internal representation. */
4627
4628/* Various DIE's use offsets relative to the beginning of the
4629 .debug_info section to refer to each other. */
4630
4631typedef long int dw_offset;
4632
4633/* Define typedefs here to avoid circular dependencies. */
4634
4635typedef struct dw_attr_struct *dw_attr_ref;
4636typedef struct dw_line_info_struct *dw_line_info_ref;
4637typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4638typedef struct pubname_struct *pubname_ref;
a36145ca 4639typedef struct dw_ranges_struct *dw_ranges_ref;
f221c0bd 4640typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4b72e226 4641
4642/* Each entry in the line_info_table maintains the file and
4643 line number associated with the label generated for that
4644 entry. The label gives the PC value associated with
4645 the line number entry. */
4646
fb1e4f4a 4647typedef struct GTY(()) dw_line_info_struct {
4b72e226 4648 unsigned long dw_file_num;
4649 unsigned long dw_line_num;
4650}
4651dw_line_info_entry;
4652
4653/* Line information for functions in separate sections; each one gets its
4654 own sequence. */
fb1e4f4a 4655typedef struct GTY(()) dw_separate_line_info_struct {
4b72e226 4656 unsigned long dw_file_num;
4657 unsigned long dw_line_num;
4658 unsigned long function;
4659}
4660dw_separate_line_info_entry;
4661
4662/* Each DIE attribute has a field specifying the attribute kind,
4663 a link to the next attribute in the chain, and an attribute value.
4664 Attributes are typically linked below the DIE they modify. */
4665
fb1e4f4a 4666typedef struct GTY(()) dw_attr_struct {
4b72e226 4667 enum dwarf_attribute dw_attr;
4b72e226 4668 dw_val_node dw_attr_val;
4669}
4670dw_attr_node;
4671
6f56c055 4672DEF_VEC_O(dw_attr_node);
4673DEF_VEC_ALLOC_O(dw_attr_node,gc);
4674
958656b7 4675/* The Debugging Information Entry (DIE) structure. DIEs form a tree.
4676 The children of each node form a circular list linked by
4677 die_sib. die_child points to the node *before* the "first" child node. */
4b72e226 4678
fb1e4f4a 4679typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
4b72e226 4680 enum dwarf_tag die_tag;
19f716e5 4681 char *die_symbol;
6f56c055 4682 VEC(dw_attr_node,gc) * die_attr;
4b72e226 4683 dw_die_ref die_parent;
4684 dw_die_ref die_child;
4685 dw_die_ref die_sib;
023dc493 4686 dw_die_ref die_definition; /* ref from a specification to its definition */
4b72e226 4687 dw_offset die_offset;
4688 unsigned long die_abbrev;
eabb26f3 4689 int die_mark;
f6e59711 4690 /* Die is used and must not be pruned as unused. */
4691 int die_perennial_p;
26863140 4692 unsigned int decl_id;
4b72e226 4693}
4694die_node;
4695
958656b7 4696/* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
4697#define FOR_EACH_CHILD(die, c, expr) do { \
4698 c = die->die_child; \
4699 if (c) do { \
4700 c = c->die_sib; \
4701 expr; \
4702 } while (c != die->die_child); \
4703} while (0)
4704
4b72e226 4705/* The pubname structure */
4706
fb1e4f4a 4707typedef struct GTY(()) pubname_struct {
4b72e226 4708 dw_die_ref die;
52570507 4709 const char *name;
4b72e226 4710}
4711pubname_entry;
4712
af84796a 4713DEF_VEC_O(pubname_entry);
4714DEF_VEC_ALLOC_O(pubname_entry, gc);
4715
fb1e4f4a 4716struct GTY(()) dw_ranges_struct {
f221c0bd 4717 /* If this is positive, it's a block number, otherwise it's a
4718 bitwise-negated index into dw_ranges_by_label. */
4719 int num;
4720};
4721
fb1e4f4a 4722struct GTY(()) dw_ranges_by_label_struct {
f221c0bd 4723 const char *begin;
4724 const char *end;
a36145ca 4725};
4726
4b72e226 4727/* The limbo die list structure. */
fb1e4f4a 4728typedef struct GTY(()) limbo_die_struct {
4b72e226 4729 dw_die_ref die;
15cfae4e 4730 tree created_for;
4b72e226 4731 struct limbo_die_struct *next;
4732}
4733limbo_die_node;
4734
4735/* How to start an assembler comment. */
4736#ifndef ASM_COMMENT_START
4737#define ASM_COMMENT_START ";#"
4738#endif
4739
6ef828f9 4740/* Define a macro which returns nonzero for a TYPE_DECL which was
4b72e226 4741 implicitly generated for a tagged type.
4742
4743 Note that unlike the gcc front end (which generates a NULL named
4744 TYPE_DECL node for each complete tagged type, each array type, and
4745 each function type node created) the g++ front end generates a
4746 _named_ TYPE_DECL node for each tagged type node created.
4747 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4748 generate a DW_TAG_typedef DIE for them. */
4749
4750#define TYPE_DECL_IS_STUB(decl) \
4751 (DECL_NAME (decl) == NULL_TREE \
4752 || (DECL_ARTIFICIAL (decl) \
4753 && is_tagged_type (TREE_TYPE (decl)) \
4754 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
4755 /* This is necessary for stub decls that \
4756 appear in nested inline functions. */ \
4757 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4758 && (decl_ultimate_origin (decl) \
4759 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4760
4761/* Information concerning the compilation unit's programming
4762 language, and compiler version. */
4763
4b72e226 4764/* Fixed size portion of the DWARF compilation unit header. */
65bdc57c 4765#define DWARF_COMPILE_UNIT_HEADER_SIZE \
4766 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4b72e226 4767
4b72e226 4768/* Fixed size portion of public names info. */
4769#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4770
4771/* Fixed size portion of the address range info. */
4772#define DWARF_ARANGES_HEADER_SIZE \
38c41660 4773 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
61a9389f 4774 DWARF2_ADDR_SIZE * 2) \
38c41660 4775 - DWARF_INITIAL_LENGTH_SIZE)
4b72e226 4776
4777/* Size of padding portion in the address range info. It must be
4778 aligned to twice the pointer size. */
4779#define DWARF_ARANGES_PAD_SIZE \
38c41660 4780 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
61a9389f 4781 DWARF2_ADDR_SIZE * 2) \
38c41660 4782 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4b72e226 4783
142cf471 4784/* Use assembler line directives if available. */
4b72e226 4785#ifndef DWARF2_ASM_LINE_DEBUG_INFO
142cf471 4786#ifdef HAVE_AS_DWARF2_DEBUG_LINE
4787#define DWARF2_ASM_LINE_DEBUG_INFO 1
4788#else
4b72e226 4789#define DWARF2_ASM_LINE_DEBUG_INFO 0
4790#endif
142cf471 4791#endif
4b72e226 4792
4b72e226 4793/* Minimum line offset in a special line info. opcode.
4794 This value was chosen to give a reasonable range of values. */
4795#define DWARF_LINE_BASE -10
4796
3fb1e43b 4797/* First special line opcode - leave room for the standard opcodes. */
4b72e226 4798#define DWARF_LINE_OPCODE_BASE 10
4799
4800/* Range of line offsets in a special line info. opcode. */
4801#define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
4802
4803/* Flag that indicates the initial value of the is_stmt_start flag.
4804 In the present implementation, we do not mark any lines as
4805 the beginning of a source statement, because that information
4806 is not made available by the GCC front-end. */
4807#define DWARF_LINE_DEFAULT_IS_STMT_START 1
4808
38ac91bf 4809#ifdef DWARF2_DEBUGGING_INFO
4b72e226 4810/* This location is used by calc_die_sizes() to keep track
4811 the offset of each DIE within the .debug_info section. */
4812static unsigned long next_die_offset;
38ac91bf 4813#endif
4b72e226 4814
4815/* Record the root of the DIE's built for the current compilation unit. */
573aba85 4816static GTY(()) dw_die_ref comp_unit_die;
4b72e226 4817
4818/* A list of DIEs with a NULL parent waiting to be relocated. */
573aba85 4819static GTY(()) limbo_die_node *limbo_die_list;
4b72e226 4820
3740694f 4821/* Filenames referenced by this compilation unit. */
69278c24 4822static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5a3023d9 4823
26863140 4824/* A hash table of references to DIE's that describe declarations.
4825 The key is a DECL_UID() which is a unique number identifying each decl. */
4826static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4b72e226 4827
bbc59868 4828/* A hash table of references to DIE's that describe COMMON blocks.
4829 The key is DECL_UID() ^ die_parent. */
4830static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4831
b2025850 4832/* Node of the variable location list. */
fb1e4f4a 4833struct GTY ((chain_next ("%h.next"))) var_loc_node {
b2025850 4834 rtx GTY (()) var_loc_note;
4835 const char * GTY (()) label;
1897b881 4836 const char * GTY (()) section_label;
b2025850 4837 struct var_loc_node * GTY (()) next;
4838};
4839
4840/* Variable location list. */
fb1e4f4a 4841struct GTY (()) var_loc_list_def {
b2025850 4842 struct var_loc_node * GTY (()) first;
4843
4844 /* Do not mark the last element of the chained list because
4845 it is marked through the chain. */
4846 struct var_loc_node * GTY ((skip ("%h"))) last;
4847
4848 /* DECL_UID of the variable decl. */
4849 unsigned int decl_id;
4850};
4851typedef struct var_loc_list_def var_loc_list;
4852
b2025850 4853
4854/* Table of decl location linked lists. */
4855static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4856
4b72e226 4857/* A pointer to the base of a list of references to DIE's that
4858 are uniquely identified by their tag, presence/absence of
4859 children DIE's, and list of attribute/value pairs. */
8ec3a57b 4860static GTY((length ("abbrev_die_table_allocated")))
573aba85 4861 dw_die_ref *abbrev_die_table;
4b72e226 4862
4863/* Number of elements currently allocated for abbrev_die_table. */
909be935 4864static GTY(()) unsigned abbrev_die_table_allocated;
4b72e226 4865
4866/* Number of elements in type_die_table currently in use. */
909be935 4867static GTY(()) unsigned abbrev_die_table_in_use;
4b72e226 4868
4869/* Size (in elements) of increments by which we may expand the
4870 abbrev_die_table. */
4871#define ABBREV_DIE_TABLE_INCREMENT 256
4872
4873/* A pointer to the base of a table that contains line information
4874 for each source code line in .text in the compilation unit. */
8ec3a57b 4875static GTY((length ("line_info_table_allocated")))
573aba85 4876 dw_line_info_ref line_info_table;
4b72e226 4877
4878/* Number of elements currently allocated for line_info_table. */
909be935 4879static GTY(()) unsigned line_info_table_allocated;
4b72e226 4880
573aba85 4881/* Number of elements in line_info_table currently in use. */
909be935 4882static GTY(()) unsigned line_info_table_in_use;
4b72e226 4883
4884/* A pointer to the base of a table that contains line information
4885 for each source code line outside of .text in the compilation unit. */
573aba85 4886static GTY ((length ("separate_line_info_table_allocated")))
4887 dw_separate_line_info_ref separate_line_info_table;
4b72e226 4888
4889/* Number of elements currently allocated for separate_line_info_table. */
909be935 4890static GTY(()) unsigned separate_line_info_table_allocated;
4b72e226 4891
573aba85 4892/* Number of elements in separate_line_info_table currently in use. */
909be935 4893static GTY(()) unsigned separate_line_info_table_in_use;
4b72e226 4894
4895/* Size (in elements) of increments by which we may expand the
4896 line_info_table. */
4897#define LINE_INFO_TABLE_INCREMENT 1024
4898
4899/* A pointer to the base of a table that contains a list of publicly
4900 accessible names. */
af84796a 4901static GTY (()) VEC (pubname_entry, gc) * pubname_table;
4b72e226 4902
af84796a 4903/* A pointer to the base of a table that contains a list of publicly
4904 accessible types. */
4905static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4b72e226 4906
a36145ca 4907/* Array of dies for which we should generate .debug_arange info. */
573aba85 4908static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4b72e226 4909
4910/* Number of elements currently allocated for arange_table. */
909be935 4911static GTY(()) unsigned arange_table_allocated;
4b72e226 4912
4913/* Number of elements in arange_table currently in use. */
909be935 4914static GTY(()) unsigned arange_table_in_use;
4b72e226 4915
4916/* Size (in elements) of increments by which we may expand the
4917 arange_table. */
4918#define ARANGE_TABLE_INCREMENT 64
4919
a36145ca 4920/* Array of dies for which we should generate .debug_ranges info. */
573aba85 4921static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
a36145ca 4922
4923/* Number of elements currently allocated for ranges_table. */
909be935 4924static GTY(()) unsigned ranges_table_allocated;
a36145ca 4925
4926/* Number of elements in ranges_table currently in use. */
909be935 4927static GTY(()) unsigned ranges_table_in_use;
a36145ca 4928
f221c0bd 4929/* Array of pairs of labels referenced in ranges_table. */
4930static GTY ((length ("ranges_by_label_allocated")))
4931 dw_ranges_by_label_ref ranges_by_label;
4932
4933/* Number of elements currently allocated for ranges_by_label. */
4934static GTY(()) unsigned ranges_by_label_allocated;
4935
4936/* Number of elements in ranges_by_label currently in use. */
4937static GTY(()) unsigned ranges_by_label_in_use;
4938
a36145ca 4939/* Size (in elements) of increments by which we may expand the
4940 ranges_table. */
4941#define RANGES_TABLE_INCREMENT 64
4942
4c21a22f 4943/* Whether we have location lists that need outputting */
dae1861f 4944static GTY(()) bool have_location_lists;
4c21a22f 4945
d3cdd238 4946/* Unique label counter. */
4947static GTY(()) unsigned int loclabel_num;
4948
909be935 4949#ifdef DWARF2_DEBUGGING_INFO
4b72e226 4950/* Record whether the function being analyzed contains inlined functions. */
4951static int current_function_has_inlines;
38ac91bf 4952#endif
4b72e226 4953#if 0 && defined (MIPS_DEBUGGING_INFO)
4954static int comp_unit_has_inlines;
4955#endif
4956
69278c24 4957/* The last file entry emitted by maybe_emit_file(). */
4958static GTY(()) struct dwarf_file_data * last_emitted_file;
909be935 4959
6473f3f4 4960/* Number of internal labels generated by gen_internal_sym(). */
909be935 4961static GTY(()) int label_num;
4962
62435250 4963/* Cached result of previous call to lookup_filename. */
4964static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4965
573aba85 4966#ifdef DWARF2_DEBUGGING_INFO
4967
89fa767a 4968/* Offset from the "steady-state frame pointer" to the frame base,
12d886b8 4969 within the current function. */
89fa767a 4970static HOST_WIDE_INT frame_pointer_fb_offset;
12d886b8 4971
4b72e226 4972/* Forward declarations for functions defined in this file. */
4973
5493cb9a 4974static int is_pseudo_reg (const_rtx);
8ec3a57b 4975static tree type_main_variant (tree);
5493cb9a 4976static int is_tagged_type (const_tree);
8ec3a57b 4977static const char *dwarf_tag_name (unsigned);
4978static const char *dwarf_attr_name (unsigned);
4979static const char *dwarf_form_name (unsigned);
5493cb9a 4980static tree decl_ultimate_origin (const_tree);
8ec3a57b 4981static tree decl_class_context (tree);
4982static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4983static inline enum dw_val_class AT_class (dw_attr_ref);
4984static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4985static inline unsigned AT_flag (dw_attr_ref);
3d867824 4986static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4987static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4988static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4989static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
8ec3a57b 4990static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4991 unsigned long);
1b6ad376 4992static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4993 unsigned int, unsigned char *);
8ec3a57b 4994static hashval_t debug_str_do_hash (const void *);
4995static int debug_str_eq (const void *, const void *);
4996static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4997static inline const char *AT_string (dw_attr_ref);
bc620c5c 4998static enum dwarf_form AT_string_form (dw_attr_ref);
8ec3a57b 4999static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
023dc493 5000static void add_AT_specification (dw_die_ref, dw_die_ref);
8ec3a57b 5001static inline dw_die_ref AT_ref (dw_attr_ref);
5002static inline int AT_ref_external (dw_attr_ref);
5003static inline void set_AT_ref_external (dw_attr_ref, int);
5004static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5005static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5006static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5007static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5008 dw_loc_list_ref);
5009static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5010static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5011static inline rtx AT_addr (dw_attr_ref);
5012static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
d08d29c0 5013static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5014static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3d867824 5015static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5016 unsigned HOST_WIDE_INT);
8ec3a57b 5017static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5018 unsigned long);
5019static inline const char *AT_lbl (dw_attr_ref);
5020static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5021static const char *get_AT_low_pc (dw_die_ref);
5022static const char *get_AT_hi_pc (dw_die_ref);
5023static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5024static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5025static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5026static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5027static bool is_c_family (void);
5028static bool is_cxx (void);
5029static bool is_java (void);
5030static bool is_fortran (void);
5031static bool is_ada (void);
5032static void remove_AT (dw_die_ref, enum dwarf_attribute);
2b49746a 5033static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
8ec3a57b 5034static void add_child_die (dw_die_ref, dw_die_ref);
5035static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5036static dw_die_ref lookup_type_die (tree);
5037static void equate_type_number_to_die (tree, dw_die_ref);
26863140 5038static hashval_t decl_die_table_hash (const void *);
5039static int decl_die_table_eq (const void *, const void *);
8ec3a57b 5040static dw_die_ref lookup_decl_die (tree);
bbc59868 5041static hashval_t common_block_die_table_hash (const void *);
5042static int common_block_die_table_eq (const void *, const void *);
b2025850 5043static hashval_t decl_loc_table_hash (const void *);
5044static int decl_loc_table_eq (const void *, const void *);
5493cb9a 5045static var_loc_list *lookup_decl_loc (const_tree);
8ec3a57b 5046static void equate_decl_number_to_die (tree, dw_die_ref);
b2025850 5047static void add_var_loc_to_decl (tree, struct var_loc_node *);
8ec3a57b 5048static void print_spaces (FILE *);
5049static void print_die (dw_die_ref, FILE *);
5050static void print_dwarf_line_table (FILE *);
8ec3a57b 5051static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5052static dw_die_ref pop_compile_unit (dw_die_ref);
5053static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5054static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5055static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5056static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5493cb9a 5057static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
8ec3a57b 5058static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5059static int same_die_p (dw_die_ref, dw_die_ref, int *);
5060static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5061static void compute_section_prefix (dw_die_ref);
5062static int is_type_die (dw_die_ref);
5063static int is_comdat_die (dw_die_ref);
5064static int is_symbol_die (dw_die_ref);
5065static void assign_symbol_names (dw_die_ref);
5066static void break_out_includes (dw_die_ref);
5067static hashval_t htab_cu_hash (const void *);
5068static int htab_cu_eq (const void *, const void *);
5069static void htab_cu_del (void *);
5070static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5071static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5072static void add_sibling_attributes (dw_die_ref);
5073static void build_abbrev_table (dw_die_ref);
5074static void output_location_lists (dw_die_ref);
d6d5e57f 5075static int constant_size (unsigned HOST_WIDE_INT);
8ec3a57b 5076static unsigned long size_of_die (dw_die_ref);
5077static void calc_die_sizes (dw_die_ref);
5078static void mark_dies (dw_die_ref);
5079static void unmark_dies (dw_die_ref);
5080static void unmark_all_dies (dw_die_ref);
af84796a 5081static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
8ec3a57b 5082static unsigned long size_of_aranges (void);
5083static enum dwarf_form value_format (dw_attr_ref);
5084static void output_value_format (dw_attr_ref);
5085static void output_abbrev_section (void);
5086static void output_die_symbol (dw_die_ref);
5087static void output_die (dw_die_ref);
5088static void output_compilation_unit_header (void);
5089static void output_comp_unit (dw_die_ref, int);
5090static const char *dwarf2_name (tree, int);
5091static void add_pubname (tree, dw_die_ref);
a12691f0 5092static void add_pubname_string (const char *, dw_die_ref);
af84796a 5093static void add_pubtype (tree, dw_die_ref);
5094static void output_pubnames (VEC (pubname_entry,gc) *);
8ec3a57b 5095static void add_arange (tree, dw_die_ref);
5096static void output_aranges (void);
f221c0bd 5097static unsigned int add_ranges_num (int);
5493cb9a 5098static unsigned int add_ranges (const_tree);
f221c0bd 5099static unsigned int add_ranges_by_labels (const char *, const char *);
8ec3a57b 5100static void output_ranges (void);
5101static void output_line_info (void);
5102static void output_file_names (void);
5103static dw_die_ref base_type_die (tree);
8ec3a57b 5104static int is_base_type (tree);
5493cb9a 5105static bool is_subrange_type (const_tree);
a7011153 5106static dw_die_ref subrange_type_die (tree, dw_die_ref);
8ec3a57b 5107static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5493cb9a 5108static int type_is_enum (const_tree);
5109static unsigned int dbx_reg_number (const_rtx);
fd51758c 5110static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
d53bb226 5111static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
39c7766b 5112static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
d53bb226 5113 enum var_init_status);
5114static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5115 enum var_init_status);
d53bb226 5116static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5117 enum var_init_status);
5493cb9a 5118static int is_based_loc (const_rtx);
d53bb226 5119static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5120 enum var_init_status);
5121static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5122 enum var_init_status);
5123static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
afcf285e 5124static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5125static dw_loc_descr_ref loc_descriptor_from_tree (tree);
8ec3a57b 5126static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5493cb9a 5127static tree field_type (const_tree);
5128static unsigned int simple_type_align_in_bits (const_tree);
5129static unsigned int simple_decl_align_in_bits (const_tree);
5130static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5131static HOST_WIDE_INT field_byte_offset (const_tree);
8ec3a57b 5132static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5133 dw_loc_descr_ref);
5134static void add_data_member_location_attribute (dw_die_ref, tree);
5135static void add_const_value_attribute (dw_die_ref, rtx);
1b6ad376 5136static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5137static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5493cb9a 5138static void insert_float (const_rtx, unsigned char *);
8ec3a57b 5139static rtx rtl_for_decl_location (tree);
b2025850 5140static void add_location_or_const_value_attribute (dw_die_ref, tree,
5141 enum dwarf_attribute);
8ec3a57b 5142static void tree_add_const_value_attribute (dw_die_ref, tree);
5143static void add_name_attribute (dw_die_ref, const char *);
5144static void add_comp_dir_attribute (dw_die_ref);
5145static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
544cd34c 5146static void add_subscript_info (dw_die_ref, tree, bool);
8ec3a57b 5147static void add_byte_size_attribute (dw_die_ref, tree);
5148static void add_bit_offset_attribute (dw_die_ref, tree);
5149static void add_bit_size_attribute (dw_die_ref, tree);
5150static void add_prototyped_attribute (dw_die_ref, tree);
ebcb0478 5151static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
8ec3a57b 5152static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5153static void add_src_coords_attributes (dw_die_ref, tree);
5154static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5155static void push_decl_scope (tree);
5156static void pop_decl_scope (void);
5157static dw_die_ref scope_die_for (tree, dw_die_ref);
5158static inline int local_scope_p (dw_die_ref);
a974aa3e 5159static inline int class_scope_p (dw_die_ref);
e89530cd 5160static inline int class_or_namespace_scope_p (dw_die_ref);
8ec3a57b 5161static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
8ff30ff6 5162static void add_calling_convention_attribute (dw_die_ref, tree);
5493cb9a 5163static const char *type_tag (const_tree);
5164static tree member_declared_type (const_tree);
4b72e226 5165#if 0
8ec3a57b 5166static const char *decl_start_label (tree);
4b72e226 5167#endif
8ec3a57b 5168static void gen_array_type_die (tree, dw_die_ref);
1c79cc8c 5169static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4b72e226 5170#if 0
8ec3a57b 5171static void gen_entry_point_die (tree, dw_die_ref);
4b72e226 5172#endif
93c7db82 5173static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4b5d70fd 5174static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
8ec3a57b 5175static void gen_unspecified_parameters_die (tree, dw_die_ref);
5176static void gen_formal_types_die (tree, dw_die_ref);
5177static void gen_subprogram_die (tree, dw_die_ref);
4b5d70fd 5178static void gen_variable_die (tree, tree, dw_die_ref);
2eb674c9 5179static void gen_const_die (tree, dw_die_ref);
8ec3a57b 5180static void gen_label_die (tree, dw_die_ref);
5181static void gen_lexical_block_die (tree, dw_die_ref, int);
5182static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5183static void gen_field_die (tree, dw_die_ref);
5184static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5185static dw_die_ref gen_compile_unit_die (const char *);
8ec3a57b 5186static void gen_inheritance_die (tree, tree, dw_die_ref);
5187static void gen_member_die (tree, dw_die_ref);
0e4744ac 5188static void gen_struct_or_union_type_die (tree, dw_die_ref,
5189 enum debug_info_usage);
8ec3a57b 5190static void gen_subroutine_type_die (tree, dw_die_ref);
5191static void gen_typedef_die (tree, dw_die_ref);
5192static void gen_type_die (tree, dw_die_ref);
8ec3a57b 5193static void gen_block_die (tree, dw_die_ref, int);
5194static void decls_for_scope (tree, dw_die_ref, int);
5493cb9a 5195static int is_redundant_typedef (const_tree);
4b1ab129 5196static void gen_namespace_die (tree, dw_die_ref);
4b5d70fd 5197static void gen_decl_die (tree, tree, dw_die_ref);
2b49746a 5198static dw_die_ref force_decl_die (tree);
5199static dw_die_ref force_type_die (tree);
e89530cd 5200static dw_die_ref setup_namespace_context (tree, dw_die_ref);
df4d540f 5201static dw_die_ref declare_in_namespace (tree, dw_die_ref);
69278c24 5202static struct dwarf_file_data * lookup_filename (const char *);
8ec3a57b 5203static void retry_incomplete_types (void);
5204static void gen_type_die_for_member (tree, tree, dw_die_ref);
5205static void splice_child_die (dw_die_ref, dw_die_ref);
5206static int file_info_cmp (const void *, const void *);
5207static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5208 const char *, const char *, unsigned);
5209static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5210 const char *, const char *,
5211 const char *);
5212static void output_loc_list (dw_loc_list_ref);
5213static char *gen_internal_sym (const char *);
5214
5215static void prune_unmark_dies (dw_die_ref);
5216static void prune_unused_types_mark (dw_die_ref, int);
5217static void prune_unused_types_walk (dw_die_ref);
5218static void prune_unused_types_walk_attribs (dw_die_ref);
5219static void prune_unused_types_prune (dw_die_ref);
5220static void prune_unused_types (void);
69278c24 5221static int maybe_emit_file (struct dwarf_file_data *fd);
c83a163c 5222
4b72e226 5223/* Section names used to hold DWARF debugging information. */
5224#ifndef DEBUG_INFO_SECTION
5225#define DEBUG_INFO_SECTION ".debug_info"
5226#endif
049aa99b 5227#ifndef DEBUG_ABBREV_SECTION
5228#define DEBUG_ABBREV_SECTION ".debug_abbrev"
4b72e226 5229#endif
049aa99b 5230#ifndef DEBUG_ARANGES_SECTION
5231#define DEBUG_ARANGES_SECTION ".debug_aranges"
4b72e226 5232#endif
049aa99b 5233#ifndef DEBUG_MACINFO_SECTION
5234#define DEBUG_MACINFO_SECTION ".debug_macinfo"
4b72e226 5235#endif
5236#ifndef DEBUG_LINE_SECTION
5237#define DEBUG_LINE_SECTION ".debug_line"
5238#endif
049aa99b 5239#ifndef DEBUG_LOC_SECTION
5240#define DEBUG_LOC_SECTION ".debug_loc"
4b72e226 5241#endif
049aa99b 5242#ifndef DEBUG_PUBNAMES_SECTION
5243#define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4b72e226 5244#endif
049aa99b 5245#ifndef DEBUG_STR_SECTION
5246#define DEBUG_STR_SECTION ".debug_str"
4b72e226 5247#endif
a36145ca 5248#ifndef DEBUG_RANGES_SECTION
5249#define DEBUG_RANGES_SECTION ".debug_ranges"
5250#endif
4b72e226 5251
5252/* Standard ELF section names for compiled code and data. */
25e5d448 5253#ifndef TEXT_SECTION_NAME
5254#define TEXT_SECTION_NAME ".text"
4b72e226 5255#endif
5256
80b7bd06 5257/* Section flags for .debug_str section. */
80b7bd06 5258#define DEBUG_STR_SECTION_FLAGS \
7765591b 5259 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
44bbb5f3 5260 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
5261 : SECTION_DEBUG)
80b7bd06 5262
4b72e226 5263/* Labels we insert at beginning sections we can reference instead of
f80d1bcd 5264 the section names themselves. */
4b72e226 5265
5266#ifndef TEXT_SECTION_LABEL
049aa99b 5267#define TEXT_SECTION_LABEL "Ltext"
4b72e226 5268#endif
4d0e931f 5269#ifndef COLD_TEXT_SECTION_LABEL
5270#define COLD_TEXT_SECTION_LABEL "Ltext_cold"
5271#endif
4b72e226 5272#ifndef DEBUG_LINE_SECTION_LABEL
049aa99b 5273#define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4b72e226 5274#endif
5275#ifndef DEBUG_INFO_SECTION_LABEL
049aa99b 5276#define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4b72e226 5277#endif
049aa99b 5278#ifndef DEBUG_ABBREV_SECTION_LABEL
5279#define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4b72e226 5280#endif
049aa99b 5281#ifndef DEBUG_LOC_SECTION_LABEL
5282#define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4c21a22f 5283#endif
fe39c28c 5284#ifndef DEBUG_RANGES_SECTION_LABEL
5285#define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
5286#endif
1d340a5e 5287#ifndef DEBUG_MACINFO_SECTION_LABEL
5288#define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
5289#endif
a36145ca 5290
4b72e226 5291/* Definitions of defaults for formats and names of various special
5292 (artificial) labels which may be generated within this file (when the -g
ad8d48ea 5293 options is used and DWARF2_DEBUGGING_INFO is in effect.
4b72e226 5294 If necessary, these may be overridden from within the tm.h file, but
5295 typically, overriding these defaults is unnecessary. */
5296
5297static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5298static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4d0e931f 5299static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
61a9389f 5300static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4b72e226 5301static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5302static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5303static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
1d340a5e 5304static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4c21a22f 5305static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
fe39c28c 5306static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
8c3f468d 5307
4b72e226 5308#ifndef TEXT_END_LABEL
5309#define TEXT_END_LABEL "Letext"
5310#endif
4d0e931f 5311#ifndef COLD_END_LABEL
5312#define COLD_END_LABEL "Letext_cold"
5313#endif
4b72e226 5314#ifndef BLOCK_BEGIN_LABEL
5315#define BLOCK_BEGIN_LABEL "LBB"
5316#endif
5317#ifndef BLOCK_END_LABEL
5318#define BLOCK_END_LABEL "LBE"
5319#endif
4b72e226 5320#ifndef LINE_CODE_LABEL
5321#define LINE_CODE_LABEL "LM"
5322#endif
5323#ifndef SEPARATE_LINE_CODE_LABEL
5324#define SEPARATE_LINE_CODE_LABEL "LSM"
5325#endif
d6de7df9 5326
4b72e226 5327\f
5328/* We allow a language front-end to designate a function that is to be
822e391f 5329 called to "demangle" any name before it is put into a DIE. */
4b72e226 5330
8ec3a57b 5331static const char *(*demangle_name_func) (const char *);
4b72e226 5332
5333void
8ec3a57b 5334dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4b72e226 5335{
5336 demangle_name_func = func;
5337}
4b72e226 5338
5339/* Test if rtl node points to a pseudo register. */
5340
5341static inline int
5493cb9a 5342is_pseudo_reg (const_rtx rtl)
4b72e226 5343{
8ad4c111 5344 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4b72e226 5345 || (GET_CODE (rtl) == SUBREG
701e46d0 5346 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4b72e226 5347}
5348
5349/* Return a reference to a type, with its const and volatile qualifiers
5350 removed. */
5351
5352static inline tree
8ec3a57b 5353type_main_variant (tree type)
4b72e226 5354{
5355 type = TYPE_MAIN_VARIANT (type);
5356
8c3f468d 5357 /* ??? There really should be only one main variant among any group of
5358 variants of a given type (and all of the MAIN_VARIANT values for all
5359 members of the group should point to that one type) but sometimes the C
5360 front-end messes this up for array types, so we work around that bug
5361 here. */
4b72e226 5362 if (TREE_CODE (type) == ARRAY_TYPE)
5363 while (type != TYPE_MAIN_VARIANT (type))
5364 type = TYPE_MAIN_VARIANT (type);
5365
5366 return type;
5367}
5368
6ef828f9 5369/* Return nonzero if the given type node represents a tagged type. */
4b72e226 5370
5371static inline int
5493cb9a 5372is_tagged_type (const_tree type)
4b72e226 5373{
19cb6b50 5374 enum tree_code code = TREE_CODE (type);
4b72e226 5375
5376 return (code == RECORD_TYPE || code == UNION_TYPE
5377 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5378}
5379
5380/* Convert a DIE tag into its string name. */
5381
5382static const char *
8ec3a57b 5383dwarf_tag_name (unsigned int tag)
4b72e226 5384{
5385 switch (tag)
5386 {
5387 case DW_TAG_padding:
5388 return "DW_TAG_padding";
5389 case DW_TAG_array_type:
5390 return "DW_TAG_array_type";
5391 case DW_TAG_class_type:
5392 return "DW_TAG_class_type";
5393 case DW_TAG_entry_point:
5394 return "DW_TAG_entry_point";
5395 case DW_TAG_enumeration_type:
5396 return "DW_TAG_enumeration_type";
5397 case DW_TAG_formal_parameter:
5398 return "DW_TAG_formal_parameter";
5399 case DW_TAG_imported_declaration:
5400 return "DW_TAG_imported_declaration";
5401 case DW_TAG_label:
5402 return "DW_TAG_label";
5403 case DW_TAG_lexical_block:
5404 return "DW_TAG_lexical_block";
5405 case DW_TAG_member:
5406 return "DW_TAG_member";
5407 case DW_TAG_pointer_type:
5408 return "DW_TAG_pointer_type";
5409 case DW_TAG_reference_type:
5410 return "DW_TAG_reference_type";
5411 case DW_TAG_compile_unit:
5412 return "DW_TAG_compile_unit";
5413 case DW_TAG_string_type:
5414 return "DW_TAG_string_type";
5415 case DW_TAG_structure_type:
5416 return "DW_TAG_structure_type";
5417 case DW_TAG_subroutine_type:
5418 return "DW_TAG_subroutine_type";
5419 case DW_TAG_typedef:
5420 return "DW_TAG_typedef";
5421 case DW_TAG_union_type:
5422 return "DW_TAG_union_type";
5423 case DW_TAG_unspecified_parameters:
5424 return "DW_TAG_unspecified_parameters";
5425 case DW_TAG_variant:
5426 return "DW_TAG_variant";
5427 case DW_TAG_common_block:
5428 return "DW_TAG_common_block";
5429 case DW_TAG_common_inclusion:
5430 return "DW_TAG_common_inclusion";
5431 case DW_TAG_inheritance:
5432 return "DW_TAG_inheritance";
5433 case DW_TAG_inlined_subroutine:
5434 return "DW_TAG_inlined_subroutine";
5435 case DW_TAG_module:
5436 return "DW_TAG_module";
5437 case DW_TAG_ptr_to_member_type:
5438 return "DW_TAG_ptr_to_member_type";
5439 case DW_TAG_set_type:
5440 return "DW_TAG_set_type";
5441 case DW_TAG_subrange_type:
5442 return "DW_TAG_subrange_type";
5443 case DW_TAG_with_stmt:
5444 return "DW_TAG_with_stmt";
5445 case DW_TAG_access_declaration:
5446 return "DW_TAG_access_declaration";
5447 case DW_TAG_base_type:
5448 return "DW_TAG_base_type";
5449 case DW_TAG_catch_block:
5450 return "DW_TAG_catch_block";
5451 case DW_TAG_const_type:
5452 return "DW_TAG_const_type";
5453 case DW_TAG_constant:
5454 return "DW_TAG_constant";
5455 case DW_TAG_enumerator:
5456 return "DW_TAG_enumerator";
5457 case DW_TAG_file_type:
5458 return "DW_TAG_file_type";
5459 case DW_TAG_friend:
5460 return "DW_TAG_friend";
5461 case DW_TAG_namelist:
5462 return "DW_TAG_namelist";
5463 case DW_TAG_namelist_item:
5464 return "DW_TAG_namelist_item";
5465 case DW_TAG_packed_type:
5466 return "DW_TAG_packed_type";
5467 case DW_TAG_subprogram:
5468 return "DW_TAG_subprogram";
5469 case DW_TAG_template_type_param:
5470 return "DW_TAG_template_type_param";
5471 case DW_TAG_template_value_param:
5472 return "DW_TAG_template_value_param";
5473 case DW_TAG_thrown_type:
5474 return "DW_TAG_thrown_type";
5475 case DW_TAG_try_block:
5476 return "DW_TAG_try_block";
5477 case DW_TAG_variant_part:
5478 return "DW_TAG_variant_part";
5479 case DW_TAG_variable:
5480 return "DW_TAG_variable";
5481 case DW_TAG_volatile_type:
5482 return "DW_TAG_volatile_type";
03a61d93 5483 case DW_TAG_dwarf_procedure:
5484 return "DW_TAG_dwarf_procedure";
5485 case DW_TAG_restrict_type:
5486 return "DW_TAG_restrict_type";
5487 case DW_TAG_interface_type:
5488 return "DW_TAG_interface_type";
5489 case DW_TAG_namespace:
5490 return "DW_TAG_namespace";
2b49746a 5491 case DW_TAG_imported_module:
5492 return "DW_TAG_imported_module";
03a61d93 5493 case DW_TAG_unspecified_type:
5494 return "DW_TAG_unspecified_type";
5495 case DW_TAG_partial_unit:
5496 return "DW_TAG_partial_unit";
5497 case DW_TAG_imported_unit:
5498 return "DW_TAG_imported_unit";
5499 case DW_TAG_condition:
5500 return "DW_TAG_condition";
5501 case DW_TAG_shared_type:
5502 return "DW_TAG_shared_type";
4b72e226 5503 case DW_TAG_MIPS_loop:
5504 return "DW_TAG_MIPS_loop";
5505 case DW_TAG_format_label:
5506 return "DW_TAG_format_label";
5507 case DW_TAG_function_template:
5508 return "DW_TAG_function_template";
5509 case DW_TAG_class_template:
5510 return "DW_TAG_class_template";
19f716e5 5511 case DW_TAG_GNU_BINCL:
5512 return "DW_TAG_GNU_BINCL";
5513 case DW_TAG_GNU_EINCL:
5514 return "DW_TAG_GNU_EINCL";
4b72e226 5515 default:
5516 return "DW_TAG_<unknown>";
5517 }
5518}
5519
5520/* Convert a DWARF attribute code into its string name. */
5521
5522static const char *
8ec3a57b 5523dwarf_attr_name (unsigned int attr)
4b72e226 5524{
5525 switch (attr)
5526 {
5527 case DW_AT_sibling:
5528 return "DW_AT_sibling";
5529 case DW_AT_location:
5530 return "DW_AT_location";
5531 case DW_AT_name:
5532 return "DW_AT_name";
5533 case DW_AT_ordering:
5534 return "DW_AT_ordering";
5535 case DW_AT_subscr_data:
5536 return "DW_AT_subscr_data";
5537 case DW_AT_byte_size:
5538 return "DW_AT_byte_size";
5539 case DW_AT_bit_offset:
5540 return "DW_AT_bit_offset";
5541 case DW_AT_bit_size:
5542 return "DW_AT_bit_size";
5543 case DW_AT_element_list:
5544 return "DW_AT_element_list";
5545 case DW_AT_stmt_list:
5546 return "DW_AT_stmt_list";
5547 case DW_AT_low_pc:
5548 return "DW_AT_low_pc";
5549 case DW_AT_high_pc:
5550 return "DW_AT_high_pc";
5551 case DW_AT_language:
5552 return "DW_AT_language";
5553 case DW_AT_member:
5554 return "DW_AT_member";
5555 case DW_AT_discr:
5556 return "DW_AT_discr";
5557 case DW_AT_discr_value:
5558 return "DW_AT_discr_value";
5559 case DW_AT_visibility:
5560 return "DW_AT_visibility";
5561 case DW_AT_import:
5562 return "DW_AT_import";
5563 case DW_AT_string_length:
5564 return "DW_AT_string_length";
5565 case DW_AT_common_reference:
5566 return "DW_AT_common_reference";
5567 case DW_AT_comp_dir:
5568 return "DW_AT_comp_dir";
5569 case DW_AT_const_value:
5570 return "DW_AT_const_value";
5571 case DW_AT_containing_type:
5572 return "DW_AT_containing_type";
5573 case DW_AT_default_value:
5574 return "DW_AT_default_value";
5575 case DW_AT_inline:
5576 return "DW_AT_inline";
5577 case DW_AT_is_optional:
5578 return "DW_AT_is_optional";
5579 case DW_AT_lower_bound:
5580 return "DW_AT_lower_bound";
5581 case DW_AT_producer:
5582 return "DW_AT_producer";
5583 case DW_AT_prototyped:
5584 return "DW_AT_prototyped";
5585 case DW_AT_return_addr:
5586 return "DW_AT_return_addr";
5587 case DW_AT_start_scope:
5588 return "DW_AT_start_scope";
1c79cc8c 5589 case DW_AT_bit_stride:
5590 return "DW_AT_bit_stride";
4b72e226 5591 case DW_AT_upper_bound:
5592 return "DW_AT_upper_bound";
5593 case DW_AT_abstract_origin:
5594 return "DW_AT_abstract_origin";
5595 case DW_AT_accessibility:
5596 return "DW_AT_accessibility";
5597 case DW_AT_address_class:
5598 return "DW_AT_address_class";
5599 case DW_AT_artificial:
5600 return "DW_AT_artificial";
5601 case DW_AT_base_types:
5602 return "DW_AT_base_types";
5603 case DW_AT_calling_convention:
5604 return "DW_AT_calling_convention";
5605 case DW_AT_count:
5606 return "DW_AT_count";
5607 case DW_AT_data_member_location:
5608 return "DW_AT_data_member_location";
5609 case DW_AT_decl_column:
5610 return "DW_AT_decl_column";
5611 case DW_AT_decl_file:
5612 return "DW_AT_decl_file";
5613 case DW_AT_decl_line:
5614 return "DW_AT_decl_line";
5615 case DW_AT_declaration:
5616 return "DW_AT_declaration";
5617 case DW_AT_discr_list:
5618 return "DW_AT_discr_list";
5619 case DW_AT_encoding:
5620 return "DW_AT_encoding";
5621 case DW_AT_external:
5622 return "DW_AT_external";
cb3582e7 5623 case DW_AT_explicit:
5624 return "DW_AT_explicit";
4b72e226 5625 case DW_AT_frame_base:
5626 return "DW_AT_frame_base";
5627 case DW_AT_friend:
5628 return "DW_AT_friend";
5629 case DW_AT_identifier_case:
5630 return "DW_AT_identifier_case";
5631 case DW_AT_macro_info:
5632 return "DW_AT_macro_info";
5633 case DW_AT_namelist_items:
5634 return "DW_AT_namelist_items";
5635 case DW_AT_priority:
5636 return "DW_AT_priority";
5637 case DW_AT_segment:
5638 return "DW_AT_segment";
5639 case DW_AT_specification:
5640 return "DW_AT_specification";
5641 case DW_AT_static_link:
5642 return "DW_AT_static_link";
5643 case DW_AT_type:
5644 return "DW_AT_type";
5645 case DW_AT_use_location:
5646 return "DW_AT_use_location";
5647 case DW_AT_variable_parameter:
5648 return "DW_AT_variable_parameter";
5649 case DW_AT_virtuality:
5650 return "DW_AT_virtuality";
5651 case DW_AT_vtable_elem_location:
5652 return "DW_AT_vtable_elem_location";
5653
a36145ca 5654 case DW_AT_allocated:
5655 return "DW_AT_allocated";
5656 case DW_AT_associated:
5657 return "DW_AT_associated";
5658 case DW_AT_data_location:
5659 return "DW_AT_data_location";
1c79cc8c 5660 case DW_AT_byte_stride:
5661 return "DW_AT_byte_stride";
a36145ca 5662 case DW_AT_entry_pc:
5663 return "DW_AT_entry_pc";
5664 case DW_AT_use_UTF8:
5665 return "DW_AT_use_UTF8";
5666 case DW_AT_extension:
5667 return "DW_AT_extension";
5668 case DW_AT_ranges:
5669 return "DW_AT_ranges";
5670 case DW_AT_trampoline:
5671 return "DW_AT_trampoline";
5672 case DW_AT_call_column:
5673 return "DW_AT_call_column";
5674 case DW_AT_call_file:
5675 return "DW_AT_call_file";
5676 case DW_AT_call_line:
5677 return "DW_AT_call_line";
5678
4b72e226 5679 case DW_AT_MIPS_fde:
5680 return "DW_AT_MIPS_fde";
5681 case DW_AT_MIPS_loop_begin:
5682 return "DW_AT_MIPS_loop_begin";
5683 case DW_AT_MIPS_tail_loop_begin:
5684 return "DW_AT_MIPS_tail_loop_begin";
5685 case DW_AT_MIPS_epilog_begin:
5686 return "DW_AT_MIPS_epilog_begin";
5687 case DW_AT_MIPS_loop_unroll_factor:
5688 return "DW_AT_MIPS_loop_unroll_factor";
5689 case DW_AT_MIPS_software_pipeline_depth:
5690 return "DW_AT_MIPS_software_pipeline_depth";
5691 case DW_AT_MIPS_linkage_name:
5692 return "DW_AT_MIPS_linkage_name";
5693 case DW_AT_MIPS_stride:
5694 return "DW_AT_MIPS_stride";
5695 case DW_AT_MIPS_abstract_name:
5696 return "DW_AT_MIPS_abstract_name";
5697 case DW_AT_MIPS_clone_origin:
5698 return "DW_AT_MIPS_clone_origin";
5699 case DW_AT_MIPS_has_inlines:
5700 return "DW_AT_MIPS_has_inlines";
5701
5702 case DW_AT_sf_names:
5703 return "DW_AT_sf_names";
5704 case DW_AT_src_info:
5705 return "DW_AT_src_info";
5706 case DW_AT_mac_info:
5707 return "DW_AT_mac_info";
5708 case DW_AT_src_coords:
5709 return "DW_AT_src_coords";
5710 case DW_AT_body_begin:
5711 return "DW_AT_body_begin";
5712 case DW_AT_body_end:
5713 return "DW_AT_body_end";
634906d6 5714 case DW_AT_GNU_vector:
5715 return "DW_AT_GNU_vector";
5716
8d60d2bc 5717 case DW_AT_VMS_rtnbeg_pd_address:
5718 return "DW_AT_VMS_rtnbeg_pd_address";
5719
4b72e226 5720 default:
5721 return "DW_AT_<unknown>";
5722 }
5723}
5724
5725/* Convert a DWARF value form code into its string name. */
5726
5727static const char *
8ec3a57b 5728dwarf_form_name (unsigned int form)
4b72e226 5729{
5730 switch (form)
5731 {
5732 case DW_FORM_addr:
5733 return "DW_FORM_addr";
5734 case DW_FORM_block2:
5735 return "DW_FORM_block2";
5736 case DW_FORM_block4:
5737 return "DW_FORM_block4";
5738 case DW_FORM_data2:
5739 return "DW_FORM_data2";
5740 case DW_FORM_data4:
5741 return "DW_FORM_data4";
5742 case DW_FORM_data8:
5743 return "DW_FORM_data8";
5744 case DW_FORM_string:
5745 return "DW_FORM_string";
5746 case DW_FORM_block:
5747 return "DW_FORM_block";
5748 case DW_FORM_block1:
5749 return "DW_FORM_block1";
5750 case DW_FORM_data1:
5751 return "DW_FORM_data1";
5752 case DW_FORM_flag:
5753 return "DW_FORM_flag";
5754 case DW_FORM_sdata:
5755 return "DW_FORM_sdata";
5756 case DW_FORM_strp:
5757 return "DW_FORM_strp";
5758 case DW_FORM_udata:
5759 return "DW_FORM_udata";
5760 case DW_FORM_ref_addr:
5761 return "DW_FORM_ref_addr";
5762 case DW_FORM_ref1:
5763 return "DW_FORM_ref1";
5764 case DW_FORM_ref2:
5765 return "DW_FORM_ref2";
5766 case DW_FORM_ref4:
5767 return "DW_FORM_ref4";
5768 case DW_FORM_ref8:
5769 return "DW_FORM_ref8";
5770 case DW_FORM_ref_udata:
5771 return "DW_FORM_ref_udata";
5772 case DW_FORM_indirect:
5773 return "DW_FORM_indirect";
8a8bfbe7 5774 default:
4b72e226 5775 return "DW_FORM_<unknown>";
30ade641 5776 }
5777}
8a8bfbe7 5778\f
5779/* Determine the "ultimate origin" of a decl. The decl may be an inlined
5780 instance of an inlined instance of a decl which is local to an inline
5781 function, so we have to trace all of the way back through the origin chain
5782 to find out what sort of node actually served as the original seed for the
5783 given block. */
30ade641 5784
8a8bfbe7 5785static tree
5493cb9a 5786decl_ultimate_origin (const_tree decl)
30ade641 5787{
5ded8c6f 5788 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5789 return NULL_TREE;
5790
e7b3c55c 5791 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5792 nodes in the function to point to themselves; ignore that if
5793 we're trying to output the abstract instance of this function. */
5794 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5795 return NULL_TREE;
5796
7bd4f6b6 5797 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5798 most distant ancestor, this should never happen. */
5799 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
8a8bfbe7 5800
c0671ae8 5801 return DECL_ABSTRACT_ORIGIN (decl);
30ade641 5802}
5803
8a8bfbe7 5804/* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
5805 of a virtual function may refer to a base class, so we check the 'this'
5806 parameter. */
ec1e49cc 5807
8a8bfbe7 5808static tree
8ec3a57b 5809decl_class_context (tree decl)
30ade641 5810{
8a8bfbe7 5811 tree context = NULL_TREE;
ec1e49cc 5812
8a8bfbe7 5813 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5814 context = DECL_CONTEXT (decl);
5815 else
5816 context = TYPE_MAIN_VARIANT
5817 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
ec1e49cc 5818
9308e976 5819 if (context && !TYPE_P (context))
8a8bfbe7 5820 context = NULL_TREE;
5821
5822 return context;
30ade641 5823}
5824\f
958656b7 5825/* Add an attribute/value pair to a DIE. */
ec1e49cc 5826
5827static inline void
8ec3a57b 5828add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
30ade641 5829{
6f56c055 5830 /* Maybe this should be an assert? */
5831 if (die == NULL)
5832 return;
61a9389f 5833
6f56c055 5834 if (die->die_attr == NULL)
5835 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5836 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
30ade641 5837}
5838
573aba85 5839static inline enum dw_val_class
8ec3a57b 5840AT_class (dw_attr_ref a)
c90bf86c 5841{
5842 return a->dw_attr_val.val_class;
5843}
5844
8a8bfbe7 5845/* Add a flag value attribute to a DIE. */
ec1e49cc 5846
8a8bfbe7 5847static inline void
8ec3a57b 5848add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
30ade641 5849{
6f56c055 5850 dw_attr_node attr;
ec1e49cc 5851
6f56c055 5852 attr.dw_attr = attr_kind;
5853 attr.dw_attr_val.val_class = dw_val_class_flag;
5854 attr.dw_attr_val.v.val_flag = flag;
5855 add_dwarf_attr (die, &attr);
30ade641 5856}
5857
c90bf86c 5858static inline unsigned
8ec3a57b 5859AT_flag (dw_attr_ref a)
c90bf86c 5860{
7bd4f6b6 5861 gcc_assert (a && AT_class (a) == dw_val_class_flag);
5862 return a->dw_attr_val.v.val_flag;
c90bf86c 5863}
5864
8a8bfbe7 5865/* Add a signed integer attribute value to a DIE. */
ec1e49cc 5866
8a8bfbe7 5867static inline void
3d867824 5868add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
30ade641 5869{
6f56c055 5870 dw_attr_node attr;
8a8bfbe7 5871
6f56c055 5872 attr.dw_attr = attr_kind;
5873 attr.dw_attr_val.val_class = dw_val_class_const;
5874 attr.dw_attr_val.v.val_int = int_val;
5875 add_dwarf_attr (die, &attr);
30ade641 5876}
5877
3d867824 5878static inline HOST_WIDE_INT
8ec3a57b 5879AT_int (dw_attr_ref a)
c90bf86c 5880{
7bd4f6b6 5881 gcc_assert (a && AT_class (a) == dw_val_class_const);
5882 return a->dw_attr_val.v.val_int;
c90bf86c 5883}
5884
8a8bfbe7 5885/* Add an unsigned integer attribute value to a DIE. */
ec1e49cc 5886
8a8bfbe7 5887static inline void
8ec3a57b 5888add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3d867824 5889 unsigned HOST_WIDE_INT unsigned_val)
30ade641 5890{
6f56c055 5891 dw_attr_node attr;
8a8bfbe7 5892
6f56c055 5893 attr.dw_attr = attr_kind;
5894 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5895 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5896 add_dwarf_attr (die, &attr);
30ade641 5897}
ec1e49cc 5898
3d867824 5899static inline unsigned HOST_WIDE_INT
8ec3a57b 5900AT_unsigned (dw_attr_ref a)
c90bf86c 5901{
7bd4f6b6 5902 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5903 return a->dw_attr_val.v.val_unsigned;
c90bf86c 5904}
5905
8a8bfbe7 5906/* Add an unsigned double integer attribute value to a DIE. */
5907
5908static inline void
8ec3a57b 5909add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5910 long unsigned int val_hi, long unsigned int val_low)
30ade641 5911{
6f56c055 5912 dw_attr_node attr;
ec1e49cc 5913
6f56c055 5914 attr.dw_attr = attr_kind;
5915 attr.dw_attr_val.val_class = dw_val_class_long_long;
5916 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5917 attr.dw_attr_val.v.val_long_long.low = val_low;
5918 add_dwarf_attr (die, &attr);
8a8bfbe7 5919}
ec1e49cc 5920
8a8bfbe7 5921/* Add a floating point attribute value to a DIE and return it. */
ec1e49cc 5922
8a8bfbe7 5923static inline void
1b6ad376 5924add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5925 unsigned int length, unsigned int elt_size, unsigned char *array)
8a8bfbe7 5926{
6f56c055 5927 dw_attr_node attr;
8a8bfbe7 5928
6f56c055 5929 attr.dw_attr = attr_kind;
5930 attr.dw_attr_val.val_class = dw_val_class_vec;
5931 attr.dw_attr_val.v.val_vec.length = length;
5932 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5933 attr.dw_attr_val.v.val_vec.array = array;
5934 add_dwarf_attr (die, &attr);
30ade641 5935}
5936
573aba85 5937/* Hash and equality functions for debug_str_hash. */
5938
5939static hashval_t
8ec3a57b 5940debug_str_do_hash (const void *x)
573aba85 5941{
5942 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5943}
5944
5945static int
8ec3a57b 5946debug_str_eq (const void *x1, const void *x2)
573aba85 5947{
5948 return strcmp ((((const struct indirect_string_node *)x1)->str),
5949 (const char *)x2) == 0;
5950}
5951
8d17cbdd 5952static struct indirect_string_node *
5953find_AT_string (const char *str)
30ade641 5954{
80b7bd06 5955 struct indirect_string_node *node;
b9a7cc69 5956 void **slot;
bc70bd5e 5957
80b7bd06 5958 if (! debug_str_hash)
8ec3a57b 5959 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
573aba85 5960 debug_str_eq, NULL);
5961
5962 slot = htab_find_slot_with_hash (debug_str_hash, str,
5963 htab_hash_string (str), INSERT);
5964 if (*slot == NULL)
facb12b2 5965 {
5966 node = (struct indirect_string_node *)
5967 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5968 node->str = ggc_strdup (str);
5969 *slot = node;
5970 }
5971 else
5972 node = (struct indirect_string_node *) *slot;
5973
80b7bd06 5974 node->refcount++;
8d17cbdd 5975 return node;
5976}
5977
5978/* Add a string attribute value to a DIE. */
5979
5980static inline void
5981add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5982{
5983 dw_attr_node attr;
5984 struct indirect_string_node *node;
5985
5986 node = find_AT_string (str);
ec1e49cc 5987
6f56c055 5988 attr.dw_attr = attr_kind;
5989 attr.dw_attr_val.val_class = dw_val_class_str;
5990 attr.dw_attr_val.v.val_str = node;
5991 add_dwarf_attr (die, &attr);
8a8bfbe7 5992}
ec1e49cc 5993
c90bf86c 5994static inline const char *
8ec3a57b 5995AT_string (dw_attr_ref a)
c90bf86c 5996{
7bd4f6b6 5997 gcc_assert (a && AT_class (a) == dw_val_class_str);
5998 return a->dw_attr_val.v.val_str->str;
80b7bd06 5999}
6000
6001/* Find out whether a string should be output inline in DIE
6002 or out-of-line in .debug_str section. */
6003
bc620c5c 6004static enum dwarf_form
8ec3a57b 6005AT_string_form (dw_attr_ref a)
80b7bd06 6006{
7bd4f6b6 6007 struct indirect_string_node *node;
6008 unsigned int len;
6009 char label[32];
80b7bd06 6010
7bd4f6b6 6011 gcc_assert (a && AT_class (a) == dw_val_class_str);
8ff30ff6 6012
7bd4f6b6 6013 node = a->dw_attr_val.v.val_str;
6014 if (node->form)
6015 return node->form;
8ff30ff6 6016
7bd4f6b6 6017 len = strlen (node->str) + 1;
80b7bd06 6018
7bd4f6b6 6019 /* If the string is shorter or equal to the size of the reference, it is
6020 always better to put it inline. */
6021 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6022 return node->form = DW_FORM_string;
80b7bd06 6023
7bd4f6b6 6024 /* If we cannot expect the linker to merge strings in .debug_str
6025 section, only put it into .debug_str if it is worth even in this
6026 single module. */
2f14b1f9 6027 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
7bd4f6b6 6028 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6029 return node->form = DW_FORM_string;
8c3f468d 6030
7bd4f6b6 6031 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6032 ++dw2_string_counter;
6033 node->label = xstrdup (label);
c90bf86c 6034
7bd4f6b6 6035 return node->form = DW_FORM_strp;
c90bf86c 6036}
6037
8a8bfbe7 6038/* Add a DIE reference attribute value to a DIE. */
ec1e49cc 6039
8a8bfbe7 6040static inline void
8ec3a57b 6041add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
8a8bfbe7 6042{
6f56c055 6043 dw_attr_node attr;
ec1e49cc 6044
6f56c055 6045 attr.dw_attr = attr_kind;
6046 attr.dw_attr_val.val_class = dw_val_class_die_ref;
6047 attr.dw_attr_val.v.val_die_ref.die = targ_die;
6048 attr.dw_attr_val.v.val_die_ref.external = 0;
6049 add_dwarf_attr (die, &attr);
8a8bfbe7 6050}
34425fdc 6051
023dc493 6052/* Add an AT_specification attribute to a DIE, and also make the back
8b332087 6053 pointer from the specification to the definition. */
023dc493 6054
6055static inline void
6056add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6057{
6058 add_AT_die_ref (die, DW_AT_specification, targ_die);
7bd4f6b6 6059 gcc_assert (!targ_die->die_definition);
023dc493 6060 targ_die->die_definition = die;
6061}
6062
c90bf86c 6063static inline dw_die_ref
8ec3a57b 6064AT_ref (dw_attr_ref a)
c90bf86c 6065{
7bd4f6b6 6066 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6067 return a->dw_attr_val.v.val_die_ref.die;
c90bf86c 6068}
6069
19f716e5 6070static inline int
8ec3a57b 6071AT_ref_external (dw_attr_ref a)
19f716e5 6072{
6073 if (a && AT_class (a) == dw_val_class_die_ref)
6074 return a->dw_attr_val.v.val_die_ref.external;
6075
6076 return 0;
6077}
6078
19f716e5 6079static inline void
8ec3a57b 6080set_AT_ref_external (dw_attr_ref a, int i)
19f716e5 6081{
7bd4f6b6 6082 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6083 a->dw_attr_val.v.val_die_ref.external = i;
19f716e5 6084}
6085
8a8bfbe7 6086/* Add an FDE reference attribute value to a DIE. */
34425fdc 6087
8a8bfbe7 6088static inline void
8ec3a57b 6089add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
8a8bfbe7 6090{
6f56c055 6091 dw_attr_node attr;
34425fdc 6092
6f56c055 6093 attr.dw_attr = attr_kind;
6094 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6095 attr.dw_attr_val.v.val_fde_index = targ_fde;
6096 add_dwarf_attr (die, &attr);
30ade641 6097}
ec1e49cc 6098
8a8bfbe7 6099/* Add a location description attribute value to a DIE. */
ec1e49cc 6100
8a8bfbe7 6101static inline void
8ec3a57b 6102add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
8a8bfbe7 6103{
6f56c055 6104 dw_attr_node attr;
ec1e49cc 6105
6f56c055 6106 attr.dw_attr = attr_kind;
6107 attr.dw_attr_val.val_class = dw_val_class_loc;
6108 attr.dw_attr_val.v.val_loc = loc;
6109 add_dwarf_attr (die, &attr);
30ade641 6110}
6111
c90bf86c 6112static inline dw_loc_descr_ref
8ec3a57b 6113AT_loc (dw_attr_ref a)
c90bf86c 6114{
7bd4f6b6 6115 gcc_assert (a && AT_class (a) == dw_val_class_loc);
6116 return a->dw_attr_val.v.val_loc;
c90bf86c 6117}
6118
4c21a22f 6119static inline void
8ec3a57b 6120add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4c21a22f 6121{
6f56c055 6122 dw_attr_node attr;
4c21a22f 6123
6f56c055 6124 attr.dw_attr = attr_kind;
6125 attr.dw_attr_val.val_class = dw_val_class_loc_list;
6126 attr.dw_attr_val.v.val_loc_list = loc_list;
6127 add_dwarf_attr (die, &attr);
dae1861f 6128 have_location_lists = true;
4c21a22f 6129}
6130
4c21a22f 6131static inline dw_loc_list_ref
8ec3a57b 6132AT_loc_list (dw_attr_ref a)
4c21a22f 6133{
7bd4f6b6 6134 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6135 return a->dw_attr_val.v.val_loc_list;
4c21a22f 6136}
6137
8a8bfbe7 6138/* Add an address constant attribute value to a DIE. */
ec1e49cc 6139
8a8bfbe7 6140static inline void
8ec3a57b 6141add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
30ade641 6142{
6f56c055 6143 dw_attr_node attr;
ec1e49cc 6144
6f56c055 6145 attr.dw_attr = attr_kind;
6146 attr.dw_attr_val.val_class = dw_val_class_addr;
6147 attr.dw_attr_val.v.val_addr = addr;
6148 add_dwarf_attr (die, &attr);
30ade641 6149}
6150
69278c24 6151/* Get the RTX from to an address DIE attribute. */
6152
eacbfaac 6153static inline rtx
8ec3a57b 6154AT_addr (dw_attr_ref a)
c90bf86c 6155{
7bd4f6b6 6156 gcc_assert (a && AT_class (a) == dw_val_class_addr);
6157 return a->dw_attr_val.v.val_addr;
c90bf86c 6158}
6159
69278c24 6160/* Add a file attribute value to a DIE. */
6161
6162static inline void
6163add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6164 struct dwarf_file_data *fd)
6165{
6166 dw_attr_node attr;
6167
6168 attr.dw_attr = attr_kind;
6169 attr.dw_attr_val.val_class = dw_val_class_file;
6170 attr.dw_attr_val.v.val_file = fd;
6171 add_dwarf_attr (die, &attr);
6172}
6173
6174/* Get the dwarf_file_data from a file DIE attribute. */
6175
6176static inline struct dwarf_file_data *
6177AT_file (dw_attr_ref a)
6178{
6179 gcc_assert (a && AT_class (a) == dw_val_class_file);
6180 return a->dw_attr_val.v.val_file;
6181}
6182
8a8bfbe7 6183/* Add a label identifier attribute value to a DIE. */
ec1e49cc 6184
8a8bfbe7 6185static inline void
8ec3a57b 6186add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
30ade641 6187{
6f56c055 6188 dw_attr_node attr;
ec1e49cc 6189
6f56c055 6190 attr.dw_attr = attr_kind;
6191 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6192 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6193 add_dwarf_attr (die, &attr);
8a8bfbe7 6194}
ec1e49cc 6195
d08d29c0 6196/* Add a section offset attribute value to a DIE, an offset into the
6197 debug_line section. */
8a8bfbe7 6198
6199static inline void
d08d29c0 6200add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6201 const char *label)
8a8bfbe7 6202{
6f56c055 6203 dw_attr_node attr;
ec1e49cc 6204
6f56c055 6205 attr.dw_attr = attr_kind;
6206 attr.dw_attr_val.val_class = dw_val_class_lineptr;
6207 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6208 add_dwarf_attr (die, &attr);
d08d29c0 6209}
6210
6211/* Add a section offset attribute value to a DIE, an offset into the
6212 debug_macinfo section. */
6213
6214static inline void
6215add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6216 const char *label)
6217{
6f56c055 6218 dw_attr_node attr;
d08d29c0 6219
6f56c055 6220 attr.dw_attr = attr_kind;
6221 attr.dw_attr_val.val_class = dw_val_class_macptr;
6222 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6223 add_dwarf_attr (die, &attr);
30ade641 6224}
6225
a36145ca 6226/* Add an offset attribute value to a DIE. */
6227
fe39c28c 6228static inline void
3d867824 6229add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6230 unsigned HOST_WIDE_INT offset)
a36145ca 6231{
6f56c055 6232 dw_attr_node attr;
a36145ca 6233
6f56c055 6234 attr.dw_attr = attr_kind;
6235 attr.dw_attr_val.val_class = dw_val_class_offset;
6236 attr.dw_attr_val.v.val_offset = offset;
6237 add_dwarf_attr (die, &attr);
a36145ca 6238}
6239
fe39c28c 6240/* Add an range_list attribute value to a DIE. */
6241
6242static void
8ec3a57b 6243add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6244 long unsigned int offset)
fe39c28c 6245{
6f56c055 6246 dw_attr_node attr;
fe39c28c 6247
6f56c055 6248 attr.dw_attr = attr_kind;
6249 attr.dw_attr_val.val_class = dw_val_class_range_list;
6250 attr.dw_attr_val.v.val_offset = offset;
6251 add_dwarf_attr (die, &attr);
fe39c28c 6252}
6253
c90bf86c 6254static inline const char *
8ec3a57b 6255AT_lbl (dw_attr_ref a)
30ade641 6256{
7bd4f6b6 6257 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
d08d29c0 6258 || AT_class (a) == dw_val_class_lineptr
6259 || AT_class (a) == dw_val_class_macptr));
7bd4f6b6 6260 return a->dw_attr_val.v.val_lbl_id;
30ade641 6261}
6262
8a8bfbe7 6263/* Get the attribute of type attr_kind. */
ec1e49cc 6264
89df180d 6265static dw_attr_ref
8ec3a57b 6266get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7524eb42 6267{
19cb6b50 6268 dw_attr_ref a;
6f56c055 6269 unsigned ix;
19cb6b50 6270 dw_die_ref spec = NULL;
f80d1bcd 6271
6f56c055 6272 if (! die)
6273 return NULL;
ec1e49cc 6274
6f56c055 6275 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6276 if (a->dw_attr == attr_kind)
6277 return a;
6278 else if (a->dw_attr == DW_AT_specification
6279 || a->dw_attr == DW_AT_abstract_origin)
6280 spec = AT_ref (a);
61a9389f 6281
6f56c055 6282 if (spec)
6283 return get_AT (spec, attr_kind);
8a8bfbe7 6284
6285 return NULL;
7524eb42 6286}
6287
8c3f468d 6288/* Return the "low pc" attribute value, typically associated with a subprogram
6289 DIE. Return null if the "low pc" attribute is either not present, or if it
6290 cannot be represented as an assembler label identifier. */
ec1e49cc 6291
c90bf86c 6292static inline const char *
8ec3a57b 6293get_AT_low_pc (dw_die_ref die)
a3899bb7 6294{
19cb6b50 6295 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
8c3f468d 6296
433e0c6c 6297 return a ? AT_lbl (a) : NULL;
a3899bb7 6298}
6299
8c3f468d 6300/* Return the "high pc" attribute value, typically associated with a subprogram
6301 DIE. Return null if the "high pc" attribute is either not present, or if it
6302 cannot be represented as an assembler label identifier. */
ec1e49cc 6303
c90bf86c 6304static inline const char *
8ec3a57b 6305get_AT_hi_pc (dw_die_ref die)
30ade641 6306{
19cb6b50 6307 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
8c3f468d 6308
433e0c6c 6309 return a ? AT_lbl (a) : NULL;
8a8bfbe7 6310}
6311
6312/* Return the value of the string attribute designated by ATTR_KIND, or
6313 NULL if it is not present. */
ec1e49cc 6314
c90bf86c 6315static inline const char *
8ec3a57b 6316get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
8a8bfbe7 6317{
19cb6b50 6318 dw_attr_ref a = get_AT (die, attr_kind);
8c3f468d 6319
433e0c6c 6320 return a ? AT_string (a) : NULL;
30ade641 6321}
6322
8a8bfbe7 6323/* Return the value of the flag attribute designated by ATTR_KIND, or -1
6324 if it is not present. */
ec1e49cc 6325
8a8bfbe7 6326static inline int
8ec3a57b 6327get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
30ade641 6328{
19cb6b50 6329 dw_attr_ref a = get_AT (die, attr_kind);
8c3f468d 6330
433e0c6c 6331 return a ? AT_flag (a) : 0;
30ade641 6332}
6333
8a8bfbe7 6334/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6335 if it is not present. */
ec1e49cc 6336
8a8bfbe7 6337static inline unsigned
8ec3a57b 6338get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
30ade641 6339{
19cb6b50 6340 dw_attr_ref a = get_AT (die, attr_kind);
8c3f468d 6341
433e0c6c 6342 return a ? AT_unsigned (a) : 0;
c90bf86c 6343}
ec1e49cc 6344
c90bf86c 6345static inline dw_die_ref
8ec3a57b 6346get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
c90bf86c 6347{
19cb6b50 6348 dw_attr_ref a = get_AT (die, attr_kind);
8c3f468d 6349
433e0c6c 6350 return a ? AT_ref (a) : NULL;
8a8bfbe7 6351}
ec1e49cc 6352
69278c24 6353static inline struct dwarf_file_data *
6354get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6355{
6356 dw_attr_ref a = get_AT (die, attr_kind);
6357
6358 return a ? AT_file (a) : NULL;
6359}
6360
600dbd47 6361/* Return TRUE if the language is C or C++. */
6362
6363static inline bool
8ec3a57b 6364is_c_family (void)
8a8bfbe7 6365{
600dbd47 6366 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
ec1e49cc 6367
bda642f9 6368 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6369 || lang == DW_LANG_C99
6370 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
f80d1bcd 6371}
ec1e49cc 6372
600dbd47 6373/* Return TRUE if the language is C++. */
6374
6375static inline bool
8ec3a57b 6376is_cxx (void)
bde7be7a 6377{
bda642f9 6378 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
61a9389f 6379
bda642f9 6380 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
bc70bd5e 6381}
bde7be7a 6382
600dbd47 6383/* Return TRUE if the language is Fortran. */
6384
6385static inline bool
8ec3a57b 6386is_fortran (void)
8a8bfbe7 6387{
600dbd47 6388 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
ec1e49cc 6389
4ee9c684 6390 return (lang == DW_LANG_Fortran77
6391 || lang == DW_LANG_Fortran90
6392 || lang == DW_LANG_Fortran95);
f80d1bcd 6393}
ec1e49cc 6394
600dbd47 6395/* Return TRUE if the language is Java. */
6396
6397static inline bool
8ec3a57b 6398is_java (void)
af4d39d8 6399{
600dbd47 6400 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
af4d39d8 6401
600dbd47 6402 return lang == DW_LANG_Java;
6403}
6404
6405/* Return TRUE if the language is Ada. */
6406
6407static inline bool
8ec3a57b 6408is_ada (void)
600dbd47 6409{
6410 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
8ec3a57b 6411
600dbd47 6412 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
af4d39d8 6413}
6414
e7b3c55c 6415/* Remove the specified attribute if present. */
6416
6417static void
8ec3a57b 6418remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8a8bfbe7 6419{
6f56c055 6420 dw_attr_ref a;
6421 unsigned ix;
30ade641 6422
6f56c055 6423 if (! die)
6424 return;
ec1e49cc 6425
6f56c055 6426 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6427 if (a->dw_attr == attr_kind)
6428 {
b0aa6b33 6429 if (AT_class (a) == dw_val_class_str)
6430 if (a->dw_attr_val.v.val_str->refcount)
6431 a->dw_attr_val.v.val_str->refcount--;
6432
6f56c055 6433 /* VEC_ordered_remove should help reduce the number of abbrevs
6434 that are needed. */
6435 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6436 return;
6437 }
e7b3c55c 6438}
ec1e49cc 6439
958656b7 6440/* Remove CHILD from its parent. PREV must have the property that
6441 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
2b49746a 6442
6443static void
958656b7 6444remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
2b49746a 6445{
958656b7 6446 gcc_assert (child->die_parent == prev->die_parent);
6447 gcc_assert (prev->die_sib == child);
6448 if (prev == child)
2b49746a 6449 {
958656b7 6450 gcc_assert (child->die_parent->die_child == child);
6451 prev = NULL;
2b49746a 6452 }
958656b7 6453 else
6454 prev->die_sib = child->die_sib;
6455 if (child->die_parent->die_child == child)
6456 child->die_parent->die_child = prev;
2b49746a 6457}
6458
958656b7 6459/* Remove child DIE whose die_tag is TAG. Do nothing if no child
6460 matches TAG. */
ec1e49cc 6461
958656b7 6462static void
6463remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6464{
6465 dw_die_ref c;
61a9389f 6466
958656b7 6467 c = die->die_child;
6468 if (c) do {
6469 dw_die_ref prev = c;
6470 c = c->die_sib;
6471 while (c->die_tag == tag)
6472 {
6473 remove_child_with_prev (c, prev);
6474 /* Might have removed every child. */
6475 if (c == c->die_sib)
6476 return;
6477 c = c->die_sib;
6478 }
6479 } while (c != die->die_child);
6480}
6481
6482/* Add a CHILD_DIE as the last child of DIE. */
6483
6484static void
8ec3a57b 6485add_child_die (dw_die_ref die, dw_die_ref child_die)
8a8bfbe7 6486{
958656b7 6487 /* FIXME this should probably be an assert. */
6488 if (! die || ! child_die)
6489 return;
6490 gcc_assert (die != child_die);
8c3f468d 6491
958656b7 6492 child_die->die_parent = die;
6493 if (die->die_child)
6494 {
6495 child_die->die_sib = die->die_child->die_sib;
6496 die->die_child->die_sib = child_die;
8a8bfbe7 6497 }
958656b7 6498 else
6499 child_die->die_sib = child_die;
6500 die->die_child = child_die;
8a8bfbe7 6501}
6502
5134c73b 6503/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
61a9389f 6504 is the specification, to the end of PARENT's list of children.
958656b7 6505 This is done by removing and re-adding it. */
e7b3c55c 6506
6507static void
8ec3a57b 6508splice_child_die (dw_die_ref parent, dw_die_ref child)
e7b3c55c 6509{
958656b7 6510 dw_die_ref p;
e7b3c55c 6511
6512 /* We want the declaration DIE from inside the class, not the
6513 specification DIE at toplevel. */
6514 if (child->die_parent != parent)
5134c73b 6515 {
6516 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8c3f468d 6517
5134c73b 6518 if (tmp)
6519 child = tmp;
6520 }
e7b3c55c 6521
7bd4f6b6 6522 gcc_assert (child->die_parent == parent
6523 || (child->die_parent
6524 == get_AT_ref (parent, DW_AT_specification)));
61a9389f 6525
958656b7 6526 for (p = child->die_parent->die_child; ; p = p->die_sib)
6527 if (p->die_sib == child)
e7b3c55c 6528 {
958656b7 6529 remove_child_with_prev (child, p);
e7b3c55c 6530 break;
6531 }
6532
958656b7 6533 add_child_die (parent, child);
e7b3c55c 6534}
6535
8a8bfbe7 6536/* Return a pointer to a newly created DIE node. */
6537
6538static inline dw_die_ref
8ec3a57b 6539new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8a8bfbe7 6540{
2457c754 6541 dw_die_ref die = GGC_CNEW (die_node);
8a8bfbe7 6542
6543 die->die_tag = tag_value;
8a8bfbe7 6544
6545 if (parent_die != NULL)
6546 add_child_die (parent_die, die);
6547 else
678d90bb 6548 {
6549 limbo_die_node *limbo_node;
6550
2457c754 6551 limbo_node = GGC_CNEW (limbo_die_node);
678d90bb 6552 limbo_node->die = die;
15cfae4e 6553 limbo_node->created_for = t;
678d90bb 6554 limbo_node->next = limbo_die_list;
6555 limbo_die_list = limbo_node;
6556 }
ec1e49cc 6557
8a8bfbe7 6558 return die;
6559}
ec1e49cc 6560
8a8bfbe7 6561/* Return the DIE associated with the given type specifier. */
ec1e49cc 6562
8a8bfbe7 6563static inline dw_die_ref
8ec3a57b 6564lookup_type_die (tree type)
8a8bfbe7 6565{
1f3233d1 6566 return TYPE_SYMTAB_DIE (type);
8a8bfbe7 6567}
c05d7491 6568
8a8bfbe7 6569/* Equate a DIE to a given type specifier. */
ec1e49cc 6570
e7b3c55c 6571static inline void
8ec3a57b 6572equate_type_number_to_die (tree type, dw_die_ref type_die)
8a8bfbe7 6573{
1f3233d1 6574 TYPE_SYMTAB_DIE (type) = type_die;
8a8bfbe7 6575}
ec1e49cc 6576
26863140 6577/* Returns a hash value for X (which really is a die_struct). */
6578
6579static hashval_t
6580decl_die_table_hash (const void *x)
6581{
c1fdef8e 6582 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
26863140 6583}
6584
6585/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
6586
6587static int
6588decl_die_table_eq (const void *x, const void *y)
6589{
aae87fc3 6590 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
26863140 6591}
6592
8a8bfbe7 6593/* Return the DIE associated with a given declaration. */
ec1e49cc 6594
8a8bfbe7 6595static inline dw_die_ref
8ec3a57b 6596lookup_decl_die (tree decl)
8a8bfbe7 6597{
2457c754 6598 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
30ade641 6599}
6600
b2025850 6601/* Returns a hash value for X (which really is a var_loc_list). */
6602
6603static hashval_t
6604decl_loc_table_hash (const void *x)
6605{
6606 return (hashval_t) ((const var_loc_list *) x)->decl_id;
6607}
6608
6609/* Return nonzero if decl_id of var_loc_list X is the same as
6610 UID of decl *Y. */
6611
6612static int
6613decl_loc_table_eq (const void *x, const void *y)
6614{
aae87fc3 6615 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
b2025850 6616}
6617
6618/* Return the var_loc list associated with a given declaration. */
6619
6620static inline var_loc_list *
5493cb9a 6621lookup_decl_loc (const_tree decl)
b2025850 6622{
2457c754 6623 return (var_loc_list *)
6624 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
b2025850 6625}
6626
8a8bfbe7 6627/* Equate a DIE to a particular declaration. */
ec1e49cc 6628
8a8bfbe7 6629static void
8ec3a57b 6630equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
30ade641 6631{
dff29840 6632 unsigned int decl_id = DECL_UID (decl);
26863140 6633 void **slot;
8a8bfbe7 6634
26863140 6635 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6636 *slot = decl_die;
6637 decl_die->decl_id = decl_id;
30ade641 6638}
b2025850 6639
6640/* Add a variable location node to the linked list for DECL. */
6641
6642static void
6643add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6644{
6645 unsigned int decl_id = DECL_UID (decl);
6646 var_loc_list *temp;
6647 void **slot;
6648
6649 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6650 if (*slot == NULL)
6651 {
2457c754 6652 temp = GGC_CNEW (var_loc_list);
b2025850 6653 temp->decl_id = decl_id;
6654 *slot = temp;
6655 }
6656 else
2457c754 6657 temp = (var_loc_list *) *slot;
b2025850 6658
6659 if (temp->last)
6660 {
6661 /* If the current location is the same as the end of the list,
d53bb226 6662 and either both or neither of the locations is uninitialized,
b2025850 6663 we have nothing to do. */
d53bb226 6664 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6665 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6666 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6667 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6668 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6669 == VAR_INIT_STATUS_UNINITIALIZED)
6670 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6671 == VAR_INIT_STATUS_UNINITIALIZED))))
b2025850 6672 {
6673 /* Add LOC to the end of list and update LAST. */
6674 temp->last->next = loc;
6675 temp->last = loc;
6676 }
6677 }
6678 /* Do not add empty location to the beginning of the list. */
6679 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6680 {
6681 temp->first = loc;
6682 temp->last = loc;
6683 }
6684}
8a8bfbe7 6685\f
6686/* Keep track of the number of spaces used to indent the
6687 output of the debugging routines that print the structure of
6688 the DIE internal representation. */
6689static int print_indent;
ec1e49cc 6690
8a8bfbe7 6691/* Indent the line the number of spaces given by print_indent. */
6692
6693static inline void
8ec3a57b 6694print_spaces (FILE *outfile)
8a8bfbe7 6695{
6696 fprintf (outfile, "%*s", print_indent, "");
30ade641 6697}
6698
ad87de1e 6699/* Print the information associated with a given DIE, and its children.
8a8bfbe7 6700 This routine is a debugging aid only. */
ec1e49cc 6701
30ade641 6702static void
8ec3a57b 6703print_die (dw_die_ref die, FILE *outfile)
30ade641 6704{
19cb6b50 6705 dw_attr_ref a;
6706 dw_die_ref c;
6f56c055 6707 unsigned ix;
ec1e49cc 6708
8a8bfbe7 6709 print_spaces (outfile);
de064be9 6710 fprintf (outfile, "DIE %4ld: %s\n",
8a8bfbe7 6711 die->die_offset, dwarf_tag_name (die->die_tag));
6712 print_spaces (outfile);
c08e043f 6713 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
de064be9 6714 fprintf (outfile, " offset: %ld\n", die->die_offset);
8a8bfbe7 6715
6f56c055 6716 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
30ade641 6717 {
8a8bfbe7 6718 print_spaces (outfile);
6719 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6720
c90bf86c 6721 switch (AT_class (a))
8a8bfbe7 6722 {
6723 case dw_val_class_addr:
6724 fprintf (outfile, "address");
6725 break;
a36145ca 6726 case dw_val_class_offset:
6727 fprintf (outfile, "offset");
6728 break;
8a8bfbe7 6729 case dw_val_class_loc:
6730 fprintf (outfile, "location descriptor");
6731 break;
4c21a22f 6732 case dw_val_class_loc_list:
a36145ca 6733 fprintf (outfile, "location list -> label:%s",
6734 AT_loc_list (a)->ll_symbol);
4c21a22f 6735 break;
fe39c28c 6736 case dw_val_class_range_list:
6737 fprintf (outfile, "range list");
6738 break;
8a8bfbe7 6739 case dw_val_class_const:
3201d6f1 6740 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8a8bfbe7 6741 break;
6742 case dw_val_class_unsigned_const:
3201d6f1 6743 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8a8bfbe7 6744 break;
6745 case dw_val_class_long_long:
c08e043f 6746 fprintf (outfile, "constant (%lu,%lu)",
f80d1bcd 6747 a->dw_attr_val.v.val_long_long.hi,
6748 a->dw_attr_val.v.val_long_long.low);
8a8bfbe7 6749 break;
1b6ad376 6750 case dw_val_class_vec:
6751 fprintf (outfile, "floating-point or vector constant");
8a8bfbe7 6752 break;
6753 case dw_val_class_flag:
c90bf86c 6754 fprintf (outfile, "%u", AT_flag (a));
8a8bfbe7 6755 break;
6756 case dw_val_class_die_ref:
c90bf86c 6757 if (AT_ref (a) != NULL)
19f716e5 6758 {
eabb26f3 6759 if (AT_ref (a)->die_symbol)
19f716e5 6760 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6761 else
de064be9 6762 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
19f716e5 6763 }
8a8bfbe7 6764 else
6765 fprintf (outfile, "die -> <null>");
6766 break;
6767 case dw_val_class_lbl_id:
d08d29c0 6768 case dw_val_class_lineptr:
6769 case dw_val_class_macptr:
c90bf86c 6770 fprintf (outfile, "label: %s", AT_lbl (a));
8a8bfbe7 6771 break;
8a8bfbe7 6772 case dw_val_class_str:
c90bf86c 6773 if (AT_string (a) != NULL)
6774 fprintf (outfile, "\"%s\"", AT_string (a));
8a8bfbe7 6775 else
6776 fprintf (outfile, "<null>");
6777 break;
69278c24 6778 case dw_val_class_file:
6779 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6780 AT_file (a)->emitted_number);
6781 break;
0dbd1c74 6782 default:
6783 break;
8a8bfbe7 6784 }
6785
6786 fprintf (outfile, "\n");
6787 }
6788
6789 if (die->die_child != NULL)
6790 {
6791 print_indent += 4;
958656b7 6792 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8a8bfbe7 6793 print_indent -= 4;
30ade641 6794 }
19f716e5 6795 if (print_indent == 0)
6796 fprintf (outfile, "\n");
30ade641 6797}
6798
8a8bfbe7 6799/* Print the contents of the source code line number correspondence table.
6800 This routine is a debugging aid only. */
ec1e49cc 6801
8a8bfbe7 6802static void
8ec3a57b 6803print_dwarf_line_table (FILE *outfile)
30ade641 6804{
19cb6b50 6805 unsigned i;
6806 dw_line_info_ref line_info;
8a8bfbe7 6807
6808 fprintf (outfile, "\n\nDWARF source line information\n");
8c3f468d 6809 for (i = 1; i < line_info_table_in_use; i++)
30ade641 6810 {
8a8bfbe7 6811 line_info = &line_info_table[i];
69278c24 6812 fprintf (outfile, "%5d: %4ld %6ld\n", i,
6813 line_info->dw_file_num,
6814 line_info->dw_line_num);
30ade641 6815 }
8a8bfbe7 6816
6817 fprintf (outfile, "\n\n");
7524eb42 6818}
6819
8a8bfbe7 6820/* Print the information collected for a given DIE. */
6821
6822void
8ec3a57b 6823debug_dwarf_die (dw_die_ref die)
8a8bfbe7 6824{
6825 print_die (die, stderr);
6826}
6827
6828/* Print all DWARF information collected for the compilation unit.
6829 This routine is a debugging aid only. */
6830
6831void
8ec3a57b 6832debug_dwarf (void)
8a8bfbe7 6833{
6834 print_indent = 0;
6835 print_die (comp_unit_die, stderr);
985956c1 6836 if (! DWARF2_ASM_LINE_DEBUG_INFO)
6837 print_dwarf_line_table (stderr);
8a8bfbe7 6838}
6839\f
8c3f468d 6840/* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6841 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6842 DIE that marks the start of the DIEs for this include file. */
19f716e5 6843
6844static dw_die_ref
8ec3a57b 6845push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
19f716e5 6846{
6847 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6848 dw_die_ref new_unit = gen_compile_unit_die (filename);
8c3f468d 6849
19f716e5 6850 new_unit->die_sib = old_unit;
6851 return new_unit;
6852}
6853
6854/* Close an include-file CU and reopen the enclosing one. */
6855
6856static dw_die_ref
8ec3a57b 6857pop_compile_unit (dw_die_ref old_unit)
19f716e5 6858{
6859 dw_die_ref new_unit = old_unit->die_sib;
8c3f468d 6860
19f716e5 6861 old_unit->die_sib = NULL;
6862 return new_unit;
6863}
6864
8c3f468d 6865#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6866#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
19f716e5 6867
6868/* Calculate the checksum of a location expression. */
6869
6870static inline void
8ec3a57b 6871loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
19f716e5 6872{
8c3f468d 6873 CHECKSUM (loc->dw_loc_opc);
6874 CHECKSUM (loc->dw_loc_oprnd1);
6875 CHECKSUM (loc->dw_loc_oprnd2);
19f716e5 6876}
6877
6878/* Calculate the checksum of an attribute. */
6879
6880static void
8ec3a57b 6881attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
19f716e5 6882{
6883 dw_loc_descr_ref loc;
6884 rtx r;
6885
8c3f468d 6886 CHECKSUM (at->dw_attr);
19f716e5 6887
69278c24 6888 /* We don't care that this was compiled with a different compiler
6889 snapshot; if the output is the same, that's what matters. */
6890 if (at->dw_attr == DW_AT_producer)
19f716e5 6891 return;
6892
6893 switch (AT_class (at))
6894 {
6895 case dw_val_class_const:
8c3f468d 6896 CHECKSUM (at->dw_attr_val.v.val_int);
19f716e5 6897 break;
6898 case dw_val_class_unsigned_const:
8c3f468d 6899 CHECKSUM (at->dw_attr_val.v.val_unsigned);
19f716e5 6900 break;
6901 case dw_val_class_long_long:
8c3f468d 6902 CHECKSUM (at->dw_attr_val.v.val_long_long);
19f716e5 6903 break;
1b6ad376 6904 case dw_val_class_vec:
6905 CHECKSUM (at->dw_attr_val.v.val_vec);
19f716e5 6906 break;
6907 case dw_val_class_flag:
8c3f468d 6908 CHECKSUM (at->dw_attr_val.v.val_flag);
19f716e5 6909 break;
19f716e5 6910 case dw_val_class_str:
8c3f468d 6911 CHECKSUM_STRING (AT_string (at));
19f716e5 6912 break;
a36145ca 6913
19f716e5 6914 case dw_val_class_addr:
6915 r = AT_addr (at);
7bd4f6b6 6916 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6917 CHECKSUM_STRING (XSTR (r, 0));
19f716e5 6918 break;
6919
a36145ca 6920 case dw_val_class_offset:
8c3f468d 6921 CHECKSUM (at->dw_attr_val.v.val_offset);
a36145ca 6922 break;
6923
19f716e5 6924 case dw_val_class_loc:
6925 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6926 loc_checksum (loc, ctx);
6927 break;
6928
6929 case dw_val_class_die_ref:
51e8c210 6930 die_checksum (AT_ref (at), ctx, mark);
6931 break;
19f716e5 6932
6933 case dw_val_class_fde_ref:
6934 case dw_val_class_lbl_id:
d08d29c0 6935 case dw_val_class_lineptr:
6936 case dw_val_class_macptr:
a36145ca 6937 break;
19f716e5 6938
69278c24 6939 case dw_val_class_file:
6940 CHECKSUM_STRING (AT_file (at)->filename);
6941 break;
6942
19f716e5 6943 default:
6944 break;
6945 }
6946}
6947
6948/* Calculate the checksum of a DIE. */
6949
6950static void
8ec3a57b 6951die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
19f716e5 6952{
6953 dw_die_ref c;
6954 dw_attr_ref a;
6f56c055 6955 unsigned ix;
19f716e5 6956
51e8c210 6957 /* To avoid infinite recursion. */
6958 if (die->die_mark)
6959 {
6960 CHECKSUM (die->die_mark);
6961 return;
6962 }
6963 die->die_mark = ++(*mark);
6964
8c3f468d 6965 CHECKSUM (die->die_tag);
19f716e5 6966
6f56c055 6967 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
51e8c210 6968 attr_checksum (a, ctx, mark);
19f716e5 6969
958656b7 6970 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
19f716e5 6971}
6972
8c3f468d 6973#undef CHECKSUM
6974#undef CHECKSUM_STRING
19f716e5 6975
51e8c210 6976/* Do the location expressions look same? */
6977static inline int
8ec3a57b 6978same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
51e8c210 6979{
6980 return loc1->dw_loc_opc == loc2->dw_loc_opc
6981 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6982 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6983}
6984
6985/* Do the values look the same? */
6986static int
5493cb9a 6987same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
51e8c210 6988{
6989 dw_loc_descr_ref loc1, loc2;
6990 rtx r1, r2;
51e8c210 6991
6992 if (v1->val_class != v2->val_class)
6993 return 0;
6994
6995 switch (v1->val_class)
6996 {
6997 case dw_val_class_const:
6998 return v1->v.val_int == v2->v.val_int;
6999 case dw_val_class_unsigned_const:
7000 return v1->v.val_unsigned == v2->v.val_unsigned;
7001 case dw_val_class_long_long:
7002 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
c83a163c 7003 && v1->v.val_long_long.low == v2->v.val_long_long.low;
1b6ad376 7004 case dw_val_class_vec:
7005 if (v1->v.val_vec.length != v2->v.val_vec.length
7006 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7007 return 0;
7008 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7009 v1->v.val_vec.length * v1->v.val_vec.elt_size))
51e8c210 7010 return 0;
51e8c210 7011 return 1;
7012 case dw_val_class_flag:
7013 return v1->v.val_flag == v2->v.val_flag;
7014 case dw_val_class_str:
573aba85 7015 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
51e8c210 7016
7017 case dw_val_class_addr:
7018 r1 = v1->v.val_addr;
7019 r2 = v2->v.val_addr;
7020 if (GET_CODE (r1) != GET_CODE (r2))
7021 return 0;
7bd4f6b6 7022 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7023 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
51e8c210 7024
7025 case dw_val_class_offset:
7026 return v1->v.val_offset == v2->v.val_offset;
7027
7028 case dw_val_class_loc:
7029 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7030 loc1 && loc2;
7031 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7032 if (!same_loc_p (loc1, loc2, mark))
7033 return 0;
7034 return !loc1 && !loc2;
7035
7036 case dw_val_class_die_ref:
7037 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7038
7039 case dw_val_class_fde_ref:
7040 case dw_val_class_lbl_id:
d08d29c0 7041 case dw_val_class_lineptr:
7042 case dw_val_class_macptr:
51e8c210 7043 return 1;
7044
69278c24 7045 case dw_val_class_file:
7046 return v1->v.val_file == v2->v.val_file;
7047
51e8c210 7048 default:
7049 return 1;
7050 }
7051}
7052
7053/* Do the attributes look the same? */
7054
7055static int
8ec3a57b 7056same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
51e8c210 7057{
7058 if (at1->dw_attr != at2->dw_attr)
7059 return 0;
7060
69278c24 7061 /* We don't care that this was compiled with a different compiler
7062 snapshot; if the output is the same, that's what matters. */
7063 if (at1->dw_attr == DW_AT_producer)
51e8c210 7064 return 1;
7065
7066 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7067}
7068
7069/* Do the dies look the same? */
7070
7071static int
8ec3a57b 7072same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
51e8c210 7073{
7074 dw_die_ref c1, c2;
6f56c055 7075 dw_attr_ref a1;
7076 unsigned ix;
51e8c210 7077
7078 /* To avoid infinite recursion. */
7079 if (die1->die_mark)
7080 return die1->die_mark == die2->die_mark;
7081 die1->die_mark = die2->die_mark = ++(*mark);
7082
7083 if (die1->die_tag != die2->die_tag)
7084 return 0;
7085
6f56c055 7086 if (VEC_length (dw_attr_node, die1->die_attr)
7087 != VEC_length (dw_attr_node, die2->die_attr))
51e8c210 7088 return 0;
61a9389f 7089
6f56c055 7090 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7091 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7092 return 0;
51e8c210 7093
958656b7 7094 c1 = die1->die_child;
7095 c2 = die2->die_child;
7096 if (! c1)
7097 {
7098 if (c2)
7099 return 0;
7100 }
7101 else
7102 for (;;)
7103 {
7104 if (!same_die_p (c1, c2, mark))
7105 return 0;
7106 c1 = c1->die_sib;
7107 c2 = c2->die_sib;
7108 if (c1 == die1->die_child)
7109 {
7110 if (c2 == die2->die_child)
7111 break;
7112 else
7113 return 0;
7114 }
7115 }
51e8c210 7116
7117 return 1;
7118}
7119
7120/* Do the dies look the same? Wrapper around same_die_p. */
7121
7122static int
8ec3a57b 7123same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
51e8c210 7124{
7125 int mark = 0;
7126 int ret = same_die_p (die1, die2, &mark);
7127
7128 unmark_all_dies (die1);
7129 unmark_all_dies (die2);
7130
7131 return ret;
7132}
7133
19f716e5 7134/* The prefix to attach to symbols on DIEs in the current comdat debug
7135 info section. */
7136static char *comdat_symbol_id;
7137
7138/* The index of the current symbol within the current comdat CU. */
7139static unsigned int comdat_symbol_number;
7140
7141/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7142 children, and set comdat_symbol_id accordingly. */
7143
7144static void
8ec3a57b 7145compute_section_prefix (dw_die_ref unit_die)
19f716e5 7146{
51e8c210 7147 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7148 const char *base = die_name ? lbasename (die_name) : "anonymous";
2457c754 7149 char *name = XALLOCAVEC (char, strlen (base) + 64);
90f973ed 7150 char *p;
51e8c210 7151 int i, mark;
19f716e5 7152 unsigned char checksum[16];
7153 struct md5_ctx ctx;
7154
90f973ed 7155 /* Compute the checksum of the DIE, then append part of it as hex digits to
7156 the name filename of the unit. */
7157
19f716e5 7158 md5_init_ctx (&ctx);
51e8c210 7159 mark = 0;
7160 die_checksum (unit_die, &ctx, &mark);
7161 unmark_all_dies (unit_die);
19f716e5 7162 md5_finish_ctx (&ctx, checksum);
7163
93d164ee 7164 sprintf (name, "%s.", base);
19f716e5 7165 clean_symbol_name (name);
7166
8c3f468d 7167 p = name + strlen (name);
7168 for (i = 0; i < 4; i++)
7169 {
7170 sprintf (p, "%.2x", checksum[i]);
7171 p += 2;
7172 }
19f716e5 7173
7174 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7175 comdat_symbol_number = 0;
7176}
7177
90f973ed 7178/* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
19f716e5 7179
7180static int
8ec3a57b 7181is_type_die (dw_die_ref die)
19f716e5 7182{
7183 switch (die->die_tag)
7184 {
7185 case DW_TAG_array_type:
7186 case DW_TAG_class_type:
03a61d93 7187 case DW_TAG_interface_type:
19f716e5 7188 case DW_TAG_enumeration_type:
7189 case DW_TAG_pointer_type:
7190 case DW_TAG_reference_type:
7191 case DW_TAG_string_type:
7192 case DW_TAG_structure_type:
7193 case DW_TAG_subroutine_type:
7194 case DW_TAG_union_type:
7195 case DW_TAG_ptr_to_member_type:
7196 case DW_TAG_set_type:
7197 case DW_TAG_subrange_type:
7198 case DW_TAG_base_type:
7199 case DW_TAG_const_type:
7200 case DW_TAG_file_type:
7201 case DW_TAG_packed_type:
7202 case DW_TAG_volatile_type:
51e8c210 7203 case DW_TAG_typedef:
19f716e5 7204 return 1;
7205 default:
7206 return 0;
7207 }
7208}
7209
7210/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7211 Basically, we want to choose the bits that are likely to be shared between
7212 compilations (types) and leave out the bits that are specific to individual
7213 compilations (functions). */
7214
7215static int
8ec3a57b 7216is_comdat_die (dw_die_ref c)
19f716e5 7217{
8c3f468d 7218 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7219 we do for stabs. The advantage is a greater likelihood of sharing between
7220 objects that don't include headers in the same order (and therefore would
7221 put the base types in a different comdat). jason 8/28/00 */
7222
19f716e5 7223 if (c->die_tag == DW_TAG_base_type)
7224 return 0;
7225
7226 if (c->die_tag == DW_TAG_pointer_type
7227 || c->die_tag == DW_TAG_reference_type
7228 || c->die_tag == DW_TAG_const_type
7229 || c->die_tag == DW_TAG_volatile_type)
7230 {
7231 dw_die_ref t = get_AT_ref (c, DW_AT_type);
8c3f468d 7232
19f716e5 7233 return t ? is_comdat_die (t) : 0;
7234 }
19f716e5 7235
7236 return is_type_die (c);
7237}
7238
7239/* Returns 1 iff C is the sort of DIE that might be referred to from another
7240 compilation unit. */
7241
7242static int
8ec3a57b 7243is_symbol_die (dw_die_ref c)
19f716e5 7244{
8c3f468d 7245 return (is_type_die (c)
bc70bd5e 7246 || (get_AT (c, DW_AT_declaration)
8462b107 7247 && !get_AT (c, DW_AT_specification))
df4d540f 7248 || c->die_tag == DW_TAG_namespace
7249 || c->die_tag == DW_TAG_module);
19f716e5 7250}
7251
7252static char *
8ec3a57b 7253gen_internal_sym (const char *prefix)
19f716e5 7254{
7255 char buf[256];
8c3f468d 7256
4c21a22f 7257 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
19f716e5 7258 return xstrdup (buf);
7259}
7260
7261/* Assign symbols to all worthy DIEs under DIE. */
7262
7263static void
8ec3a57b 7264assign_symbol_names (dw_die_ref die)
19f716e5 7265{
19cb6b50 7266 dw_die_ref c;
19f716e5 7267
7268 if (is_symbol_die (die))
7269 {
7270 if (comdat_symbol_id)
7271 {
2457c754 7272 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8c3f468d 7273
19f716e5 7274 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7275 comdat_symbol_id, comdat_symbol_number++);
7276 die->die_symbol = xstrdup (p);
7277 }
7278 else
4c21a22f 7279 die->die_symbol = gen_internal_sym ("LDIE");
19f716e5 7280 }
7281
958656b7 7282 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
19f716e5 7283}
7284
51e8c210 7285struct cu_hash_table_entry
7286{
7287 dw_die_ref cu;
7288 unsigned min_comdat_num, max_comdat_num;
7289 struct cu_hash_table_entry *next;
7290};
7291
7292/* Routines to manipulate hash table of CUs. */
7293static hashval_t
8ec3a57b 7294htab_cu_hash (const void *of)
51e8c210 7295{
2457c754 7296 const struct cu_hash_table_entry *const entry =
7297 (const struct cu_hash_table_entry *) of;
51e8c210 7298
7299 return htab_hash_string (entry->cu->die_symbol);
7300}
7301
7302static int
8ec3a57b 7303htab_cu_eq (const void *of1, const void *of2)
51e8c210 7304{
2457c754 7305 const struct cu_hash_table_entry *const entry1 =
7306 (const struct cu_hash_table_entry *) of1;
7307 const struct die_struct *const entry2 = (const struct die_struct *) of2;
51e8c210 7308
7309 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7310}
7311
7312static void
8ec3a57b 7313htab_cu_del (void *what)
51e8c210 7314{
2457c754 7315 struct cu_hash_table_entry *next,
7316 *entry = (struct cu_hash_table_entry *) what;
51e8c210 7317
7318 while (entry)
7319 {
7320 next = entry->next;
7321 free (entry);
7322 entry = next;
7323 }
7324}
7325
7326/* Check whether we have already seen this CU and set up SYM_NUM
7327 accordingly. */
7328static int
8ec3a57b 7329check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
51e8c210 7330{
7331 struct cu_hash_table_entry dummy;
7332 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7333
7334 dummy.max_comdat_num = 0;
7335
7336 slot = (struct cu_hash_table_entry **)
7337 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7338 INSERT);
7339 entry = *slot;
7340
7341 for (; entry; last = entry, entry = entry->next)
7342 {
7343 if (same_die_p_wrap (cu, entry->cu))
7344 break;
7345 }
7346
7347 if (entry)
7348 {
7349 *sym_num = entry->min_comdat_num;
7350 return 1;
7351 }
7352
4c36ffe6 7353 entry = XCNEW (struct cu_hash_table_entry);
51e8c210 7354 entry->cu = cu;
7355 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7356 entry->next = *slot;
7357 *slot = entry;
7358
7359 return 0;
7360}
7361
7362/* Record SYM_NUM to record of CU in HTABLE. */
7363static void
8ec3a57b 7364record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
51e8c210 7365{
7366 struct cu_hash_table_entry **slot, *entry;
7367
7368 slot = (struct cu_hash_table_entry **)
7369 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7370 NO_INSERT);
7371 entry = *slot;
7372
7373 entry->max_comdat_num = sym_num;
7374}
7375
19f716e5 7376/* Traverse the DIE (which is always comp_unit_die), and set up
7377 additional compilation units for each of the include files we see
7378 bracketed by BINCL/EINCL. */
7379
7380static void
8ec3a57b 7381break_out_includes (dw_die_ref die)
19f716e5 7382{
958656b7 7383 dw_die_ref c;
19cb6b50 7384 dw_die_ref unit = NULL;
51e8c210 7385 limbo_die_node *node, **pnode;
7386 htab_t cu_hash_table;
19f716e5 7387
958656b7 7388 c = die->die_child;
7389 if (c) do {
7390 dw_die_ref prev = c;
7391 c = c->die_sib;
7392 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7393 || (unit && is_comdat_die (c)))
7394 {
7395 dw_die_ref next = c->die_sib;
7396
7397 /* This DIE is for a secondary CU; remove it from the main one. */
7398 remove_child_with_prev (c, prev);
61a9389f 7399
958656b7 7400 if (c->die_tag == DW_TAG_GNU_BINCL)
7401 unit = push_new_compile_unit (unit, c);
7402 else if (c->die_tag == DW_TAG_GNU_EINCL)
7403 unit = pop_compile_unit (unit);
7404 else
7405 add_child_die (unit, c);
7406 c = next;
7407 if (c == die->die_child)
7408 break;
7409 }
7410 } while (c != die->die_child);
19f716e5 7411
7412#if 0
7413 /* We can only use this in debugging, since the frontend doesn't check
ac02093f 7414 to make sure that we leave every include file we enter. */
7bd4f6b6 7415 gcc_assert (!unit);
19f716e5 7416#endif
7417
7418 assign_symbol_names (die);
51e8c210 7419 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7420 for (node = limbo_die_list, pnode = &limbo_die_list;
7421 node;
7422 node = node->next)
19f716e5 7423 {
51e8c210 7424 int is_dupl;
7425
19f716e5 7426 compute_section_prefix (node->die);
51e8c210 7427 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7428 &comdat_symbol_number);
19f716e5 7429 assign_symbol_names (node->die);
51e8c210 7430 if (is_dupl)
7431 *pnode = node->next;
7432 else
c83a163c 7433 {
51e8c210 7434 pnode = &node->next;
7435 record_comdat_symbol_number (node->die, cu_hash_table,
7436 comdat_symbol_number);
7437 }
19f716e5 7438 }
51e8c210 7439 htab_delete (cu_hash_table);
19f716e5 7440}
7441
7442/* Traverse the DIE and add a sibling attribute if it may have the
7443 effect of speeding up access to siblings. To save some space,
7444 avoid generating sibling attributes for DIE's without children. */
7445
7446static void
8ec3a57b 7447add_sibling_attributes (dw_die_ref die)
19f716e5 7448{
19cb6b50 7449 dw_die_ref c;
19f716e5 7450
958656b7 7451 if (! die->die_child)
7452 return;
7453
7454 if (die->die_parent && die != die->die_parent->die_child)
4b72e226 7455 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7456
958656b7 7457 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
4b72e226 7458}
7459
8c3f468d 7460/* Output all location lists for the DIE and its children. */
7461
4c21a22f 7462static void
8ec3a57b 7463output_location_lists (dw_die_ref die)
4c21a22f 7464{
7465 dw_die_ref c;
6f56c055 7466 dw_attr_ref a;
7467 unsigned ix;
8c3f468d 7468
6f56c055 7469 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7470 if (AT_class (a) == dw_val_class_loc_list)
7471 output_loc_list (AT_loc_list (a));
8c3f468d 7472
958656b7 7473 FOR_EACH_CHILD (die, c, output_location_lists (c));
4c21a22f 7474}
bc70bd5e 7475
8c3f468d 7476/* The format of each DIE (and its attribute value pairs) is encoded in an
7477 abbreviation table. This routine builds the abbreviation table and assigns
7478 a unique abbreviation id for each abbreviation entry. The children of each
7479 die are visited recursively. */
4b72e226 7480
7481static void
8ec3a57b 7482build_abbrev_table (dw_die_ref die)
4b72e226 7483{
19cb6b50 7484 unsigned long abbrev_id;
7485 unsigned int n_alloc;
7486 dw_die_ref c;
6f56c055 7487 dw_attr_ref a;
7488 unsigned ix;
19f716e5 7489
7490 /* Scan the DIE references, and mark as external any that refer to
eabb26f3 7491 DIEs from other CUs (i.e. those which are not marked). */
6f56c055 7492 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7493 if (AT_class (a) == dw_val_class_die_ref
7494 && AT_ref (a)->die_mark == 0)
8c3f468d 7495 {
6f56c055 7496 gcc_assert (AT_ref (a)->die_symbol);
8c3f468d 7497
6f56c055 7498 set_AT_ref_external (a, 1);
8c3f468d 7499 }
19f716e5 7500
4b72e226 7501 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7502 {
19cb6b50 7503 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6f56c055 7504 dw_attr_ref die_a, abbrev_a;
7505 unsigned ix;
7506 bool ok = true;
61a9389f 7507
6f56c055 7508 if (abbrev->die_tag != die->die_tag)
7509 continue;
7510 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7511 continue;
61a9389f 7512
6f56c055 7513 if (VEC_length (dw_attr_node, abbrev->die_attr)
7514 != VEC_length (dw_attr_node, die->die_attr))
7515 continue;
61a9389f 7516
6f56c055 7517 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
4b72e226 7518 {
6f56c055 7519 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7520 if ((abbrev_a->dw_attr != die_a->dw_attr)
7521 || (value_format (abbrev_a) != value_format (die_a)))
4b72e226 7522 {
6f56c055 7523 ok = false;
7524 break;
4b72e226 7525 }
7526 }
6f56c055 7527 if (ok)
7528 break;
4b72e226 7529 }
7530
7531 if (abbrev_id >= abbrev_die_table_in_use)
7532 {
7533 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7534 {
7535 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
2457c754 7536 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7537 n_alloc);
4b72e226 7538
f0af5a88 7539 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
4b72e226 7540 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7541 abbrev_die_table_allocated = n_alloc;
7542 }
7543
7544 ++abbrev_die_table_in_use;
7545 abbrev_die_table[abbrev_id] = die;
7546 }
7547
7548 die->die_abbrev = abbrev_id;
958656b7 7549 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
4b72e226 7550}
7551\f
8a8bfbe7 7552/* Return the power-of-two number of bytes necessary to represent VALUE. */
7553
7554static int
d6d5e57f 7555constant_size (unsigned HOST_WIDE_INT value)
8a8bfbe7 7556{
7557 int log;
7558
7559 if (value == 0)
7560 log = 0;
30ade641 7561 else
8a8bfbe7 7562 log = floor_log2 (value);
ec1e49cc 7563
8a8bfbe7 7564 log = log / 8;
7565 log = 1 << (floor_log2 (log) + 1);
7566
7567 return log;
30ade641 7568}
7569
8c3f468d 7570/* Return the size of a DIE as it is represented in the
8a8bfbe7 7571 .debug_info section. */
ec1e49cc 7572
8a8bfbe7 7573static unsigned long
8ec3a57b 7574size_of_die (dw_die_ref die)
30ade641 7575{
19cb6b50 7576 unsigned long size = 0;
7577 dw_attr_ref a;
6f56c055 7578 unsigned ix;
ec1e49cc 7579
8a8bfbe7 7580 size += size_of_uleb128 (die->die_abbrev);
6f56c055 7581 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
30ade641 7582 {
c90bf86c 7583 switch (AT_class (a))
30ade641 7584 {
7585 case dw_val_class_addr:
aaa408cd 7586 size += DWARF2_ADDR_SIZE;
30ade641 7587 break;
a36145ca 7588 case dw_val_class_offset:
7589 size += DWARF_OFFSET_SIZE;
7590 break;
30ade641 7591 case dw_val_class_loc:
8a8bfbe7 7592 {
19cb6b50 7593 unsigned long lsize = size_of_locs (AT_loc (a));
ec1e49cc 7594
8a8bfbe7 7595 /* Block length. */
7596 size += constant_size (lsize);
7597 size += lsize;
7598 }
30ade641 7599 break;
4c21a22f 7600 case dw_val_class_loc_list:
7601 size += DWARF_OFFSET_SIZE;
7602 break;
fe39c28c 7603 case dw_val_class_range_list:
7604 size += DWARF_OFFSET_SIZE;
7605 break;
30ade641 7606 case dw_val_class_const:
fddebe76 7607 size += size_of_sleb128 (AT_int (a));
30ade641 7608 break;
7609 case dw_val_class_unsigned_const:
c90bf86c 7610 size += constant_size (AT_unsigned (a));
30ade641 7611 break;
df78b73b 7612 case dw_val_class_long_long:
ca98eb0a 7613 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
df78b73b 7614 break;
1b6ad376 7615 case dw_val_class_vec:
2eb674c9 7616 size += constant_size (a->dw_attr_val.v.val_vec.length
7617 * a->dw_attr_val.v.val_vec.elt_size)
7618 + a->dw_attr_val.v.val_vec.length
7619 * a->dw_attr_val.v.val_vec.elt_size; /* block */
30ade641 7620 break;
7621 case dw_val_class_flag:
8a8bfbe7 7622 size += 1;
30ade641 7623 break;
7624 case dw_val_class_die_ref:
1ef5e659 7625 if (AT_ref_external (a))
7626 size += DWARF2_ADDR_SIZE;
7627 else
7628 size += DWARF_OFFSET_SIZE;
30ade641 7629 break;
7630 case dw_val_class_fde_ref:
8a8bfbe7 7631 size += DWARF_OFFSET_SIZE;
30ade641 7632 break;
7633 case dw_val_class_lbl_id:
aaa408cd 7634 size += DWARF2_ADDR_SIZE;
8a8bfbe7 7635 break;
d08d29c0 7636 case dw_val_class_lineptr:
7637 case dw_val_class_macptr:
8a8bfbe7 7638 size += DWARF_OFFSET_SIZE;
7639 break;
7640 case dw_val_class_str:
80b7bd06 7641 if (AT_string_form (a) == DW_FORM_strp)
7642 size += DWARF_OFFSET_SIZE;
7643 else
573aba85 7644 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8a8bfbe7 7645 break;
69278c24 7646 case dw_val_class_file:
7647 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7648 break;
8a8bfbe7 7649 default:
7bd4f6b6 7650 gcc_unreachable ();
8a8bfbe7 7651 }
30ade641 7652 }
8a8bfbe7 7653
7654 return size;
30ade641 7655}
7656
8c3f468d 7657/* Size the debugging information associated with a given DIE. Visits the
7658 DIE's children recursively. Updates the global variable next_die_offset, on
7659 each time through. Uses the current value of next_die_offset to update the
7660 die_offset field in each DIE. */
ec1e49cc 7661
30ade641 7662static void
8ec3a57b 7663calc_die_sizes (dw_die_ref die)
30ade641 7664{
19cb6b50 7665 dw_die_ref c;
8c3f468d 7666
8a8bfbe7 7667 die->die_offset = next_die_offset;
7668 next_die_offset += size_of_die (die);
ec1e49cc 7669
958656b7 7670 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
ec1e49cc 7671
8a8bfbe7 7672 if (die->die_child != NULL)
7673 /* Count the null byte used to terminate sibling lists. */
7674 next_die_offset += 1;
30ade641 7675}
7676
eabb26f3 7677/* Set the marks for a die and its children. We do this so
19f716e5 7678 that we know whether or not a reference needs to use FORM_ref_addr; only
eabb26f3 7679 DIEs in the same CU will be marked. We used to clear out the offset
7680 and use that as the flag, but ran into ordering problems. */
19f716e5 7681
7682static void
8ec3a57b 7683mark_dies (dw_die_ref die)
19f716e5 7684{
19cb6b50 7685 dw_die_ref c;
8c3f468d 7686
7bd4f6b6 7687 gcc_assert (!die->die_mark);
8ec3a57b 7688
eabb26f3 7689 die->die_mark = 1;
958656b7 7690 FOR_EACH_CHILD (die, c, mark_dies (c));
eabb26f3 7691}
7692
7693/* Clear the marks for a die and its children. */
7694
7695static void
8ec3a57b 7696unmark_dies (dw_die_ref die)
eabb26f3 7697{
19cb6b50 7698 dw_die_ref c;
8c3f468d 7699
7bd4f6b6 7700 gcc_assert (die->die_mark);
8ec3a57b 7701
eabb26f3 7702 die->die_mark = 0;
958656b7 7703 FOR_EACH_CHILD (die, c, unmark_dies (c));
19f716e5 7704}
7705
51e8c210 7706/* Clear the marks for a die, its children and referred dies. */
7707
7708static void
8ec3a57b 7709unmark_all_dies (dw_die_ref die)
51e8c210 7710{
7711 dw_die_ref c;
7712 dw_attr_ref a;
6f56c055 7713 unsigned ix;
51e8c210 7714
7715 if (!die->die_mark)
7716 return;
7717 die->die_mark = 0;
7718
958656b7 7719 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
51e8c210 7720
6f56c055 7721 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
51e8c210 7722 if (AT_class (a) == dw_val_class_die_ref)
7723 unmark_all_dies (AT_ref (a));
7724}
7725
61a9389f 7726/* Return the size of the .debug_pubnames or .debug_pubtypes table
af84796a 7727 generated for the compilation unit. */
6efd403b 7728
8a8bfbe7 7729static unsigned long
af84796a 7730size_of_pubnames (VEC (pubname_entry, gc) * names)
6efd403b 7731{
19cb6b50 7732 unsigned long size;
7733 unsigned i;
af84796a 7734 pubname_ref p;
df78b73b 7735
8a8bfbe7 7736 size = DWARF_PUBNAMES_HEADER_SIZE;
af84796a 7737 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7738 if (names != pubtype_table
7739 || p->die->die_offset != 0
7740 || !flag_eliminate_unused_debug_types)
7741 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6efd403b 7742
8a8bfbe7 7743 size += DWARF_OFFSET_SIZE;
7744 return size;
6efd403b 7745}
7746
ad87de1e 7747/* Return the size of the information in the .debug_aranges section. */
df78b73b 7748
8a8bfbe7 7749static unsigned long
8ec3a57b 7750size_of_aranges (void)
df78b73b 7751{
19cb6b50 7752 unsigned long size;
df78b73b 7753
8a8bfbe7 7754 size = DWARF_ARANGES_HEADER_SIZE;
df78b73b 7755
8a8bfbe7 7756 /* Count the address/length pair for this compilation unit. */
d6de7df9 7757 if (text_section_used)
7758 size += 2 * DWARF2_ADDR_SIZE;
7759 if (cold_text_section_used)
7760 size += 2 * DWARF2_ADDR_SIZE;
aaa408cd 7761 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
df78b73b 7762
8a8bfbe7 7763 /* Count the two zero words used to terminated the address range table. */
aaa408cd 7764 size += 2 * DWARF2_ADDR_SIZE;
8a8bfbe7 7765 return size;
7766}
7767\f
7768/* Select the encoding of an attribute value. */
7769
7770static enum dwarf_form
8ec3a57b 7771value_format (dw_attr_ref a)
8a8bfbe7 7772{
c90bf86c 7773 switch (a->dw_attr_val.val_class)
df78b73b 7774 {
8a8bfbe7 7775 case dw_val_class_addr:
7776 return DW_FORM_addr;
fe39c28c 7777 case dw_val_class_range_list:
a36145ca 7778 case dw_val_class_offset:
04da8de9 7779 case dw_val_class_loc_list:
7bd4f6b6 7780 switch (DWARF_OFFSET_SIZE)
7781 {
7782 case 4:
7783 return DW_FORM_data4;
7784 case 8:
7785 return DW_FORM_data8;
7786 default:
7787 gcc_unreachable ();
7788 }
8a8bfbe7 7789 case dw_val_class_loc:
c90bf86c 7790 switch (constant_size (size_of_locs (AT_loc (a))))
df78b73b 7791 {
8a8bfbe7 7792 case 1:
7793 return DW_FORM_block1;
7794 case 2:
7795 return DW_FORM_block2;
df78b73b 7796 default:
7bd4f6b6 7797 gcc_unreachable ();
df78b73b 7798 }
8a8bfbe7 7799 case dw_val_class_const:
fddebe76 7800 return DW_FORM_sdata;
8a8bfbe7 7801 case dw_val_class_unsigned_const:
c90bf86c 7802 switch (constant_size (AT_unsigned (a)))
8a8bfbe7 7803 {
7804 case 1:
7805 return DW_FORM_data1;
7806 case 2:
7807 return DW_FORM_data2;
7808 case 4:
7809 return DW_FORM_data4;
7810 case 8:
7811 return DW_FORM_data8;
7812 default:
7bd4f6b6 7813 gcc_unreachable ();
8a8bfbe7 7814 }
7815 case dw_val_class_long_long:
7816 return DW_FORM_block1;
1b6ad376 7817 case dw_val_class_vec:
2eb674c9 7818 switch (constant_size (a->dw_attr_val.v.val_vec.length
7819 * a->dw_attr_val.v.val_vec.elt_size))
7820 {
7821 case 1:
7822 return DW_FORM_block1;
7823 case 2:
7824 return DW_FORM_block2;
7825 case 4:
7826 return DW_FORM_block4;
7827 default:
7828 gcc_unreachable ();
7829 }
8a8bfbe7 7830 case dw_val_class_flag:
7831 return DW_FORM_flag;
7832 case dw_val_class_die_ref:
19f716e5 7833 if (AT_ref_external (a))
7834 return DW_FORM_ref_addr;
7835 else
7836 return DW_FORM_ref;
8a8bfbe7 7837 case dw_val_class_fde_ref:
7838 return DW_FORM_data;
7839 case dw_val_class_lbl_id:
7840 return DW_FORM_addr;
d08d29c0 7841 case dw_val_class_lineptr:
7842 case dw_val_class_macptr:
8a8bfbe7 7843 return DW_FORM_data;
7844 case dw_val_class_str:
80b7bd06 7845 return AT_string_form (a);
69278c24 7846 case dw_val_class_file:
7847 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7848 {
7849 case 1:
7850 return DW_FORM_data1;
7851 case 2:
7852 return DW_FORM_data2;
7853 case 4:
7854 return DW_FORM_data4;
7855 default:
7856 gcc_unreachable ();
7857 }
a36145ca 7858
df78b73b 7859 default:
7bd4f6b6 7860 gcc_unreachable ();
df78b73b 7861 }
6efd403b 7862}
7863
8a8bfbe7 7864/* Output the encoding of an attribute value. */
df78b73b 7865
8a8bfbe7 7866static void
8ec3a57b 7867output_value_format (dw_attr_ref a)
6efd403b 7868{
c90bf86c 7869 enum dwarf_form form = value_format (a);
8c3f468d 7870
ca98eb0a 7871 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8a8bfbe7 7872}
df78b73b 7873
8a8bfbe7 7874/* Output the .debug_abbrev section which defines the DIE abbreviation
7875 table. */
df78b73b 7876
8a8bfbe7 7877static void
8ec3a57b 7878output_abbrev_section (void)
8a8bfbe7 7879{
7880 unsigned long abbrev_id;
ec1e49cc 7881
8a8bfbe7 7882 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7883 {
19cb6b50 7884 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6f56c055 7885 unsigned ix;
7886 dw_attr_ref a_attr;
ec1e49cc 7887
ca98eb0a 7888 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
ca98eb0a 7889 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7890 dwarf_tag_name (abbrev->die_tag));
ec1e49cc 7891
ca98eb0a 7892 if (abbrev->die_child != NULL)
7893 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7894 else
7895 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8a8bfbe7 7896
6f56c055 7897 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7898 ix++)
8a8bfbe7 7899 {
ca98eb0a 7900 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7901 dwarf_attr_name (a_attr->dw_attr));
c90bf86c 7902 output_value_format (a_attr);
df78b73b 7903 }
df78b73b 7904
ca98eb0a 7905 dw2_asm_output_data (1, 0, NULL);
7906 dw2_asm_output_data (1, 0, NULL);
df78b73b 7907 }
dd198c78 7908
7909 /* Terminate the table. */
ca98eb0a 7910 dw2_asm_output_data (1, 0, NULL);
6efd403b 7911}
7912
19f716e5 7913/* Output a symbol we can use to refer to this DIE from another CU. */
7914
7915static inline void
8ec3a57b 7916output_die_symbol (dw_die_ref die)
19f716e5 7917{
7918 char *sym = die->die_symbol;
7919
7920 if (sym == 0)
7921 return;
7922
7923 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7924 /* We make these global, not weak; if the target doesn't support
7925 .linkonce, it doesn't support combining the sections, so debugging
7926 will break. */
883b2e73 7927 targetm.asm_out.globalize_label (asm_out_file, sym);
8c3f468d 7928
19f716e5 7929 ASM_OUTPUT_LABEL (asm_out_file, sym);
7930}
7931
1d340a5e 7932/* Return a new location list, given the begin and end range, and the
8c3f468d 7933 expression. gensym tells us whether to generate a new internal symbol for
7934 this location list node, which is done for the head of the list only. */
7935
1d340a5e 7936static inline dw_loc_list_ref
8ec3a57b 7937new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7938 const char *section, unsigned int gensym)
1d340a5e 7939{
2457c754 7940 dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8c3f468d 7941
1d340a5e 7942 retlist->begin = begin;
7943 retlist->end = end;
7944 retlist->expr = expr;
7945 retlist->section = section;
bc70bd5e 7946 if (gensym)
1d340a5e 7947 retlist->ll_symbol = gen_internal_sym ("LLST");
8c3f468d 7948
1d340a5e 7949 return retlist;
7950}
7951
2358393e 7952/* Add a location description expression to a location list. */
8c3f468d 7953
1d340a5e 7954static inline void
8ec3a57b 7955add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7956 const char *begin, const char *end,
7957 const char *section)
1d340a5e 7958{
19cb6b50 7959 dw_loc_list_ref *d;
bc70bd5e 7960
6312a35e 7961 /* Find the end of the chain. */
1d340a5e 7962 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7963 ;
8c3f468d 7964
2358393e 7965 /* Add a new location list node to the list. */
1d340a5e 7966 *d = new_loc_list (descr, begin, end, section, 0);
7967}
7968
2358393e 7969/* Output the location list given to us. */
8c3f468d 7970
4c21a22f 7971static void
8ec3a57b 7972output_loc_list (dw_loc_list_ref list_head)
4c21a22f 7973{
8c3f468d 7974 dw_loc_list_ref curr = list_head;
7975
4c21a22f 7976 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
a36145ca 7977
71c23453 7978 /* Walk the location list, and output each range + expression. */
bc70bd5e 7979 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
4c21a22f 7980 {
fe39c28c 7981 unsigned long size;
d53bb226 7982 /* Don't output an entry that starts and ends at the same address. */
7983 if (strcmp (curr->begin, curr->end) == 0)
7984 continue;
dae1861f 7985 if (!have_multiple_function_sections)
71c23453 7986 {
7987 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7988 "Location list begin address (%s)",
7989 list_head->ll_symbol);
7990 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7991 "Location list end address (%s)",
7992 list_head->ll_symbol);
7993 }
7994 else
7995 {
7996 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7997 "Location list begin address (%s)",
7998 list_head->ll_symbol);
7999 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8000 "Location list end address (%s)",
8001 list_head->ll_symbol);
8002 }
4c21a22f 8003 size = size_of_locs (curr->expr);
bc70bd5e 8004
4c21a22f 8005 /* Output the block length for this list of location operations. */
7bd4f6b6 8006 gcc_assert (size <= 0xffff);
fe39c28c 8007 dw2_asm_output_data (2, size, "%s", "Location expression size");
8008
4c21a22f 8009 output_loc_sequence (curr->expr);
8010 }
8c3f468d 8011
71c23453 8012 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
ec98ecf4 8013 "Location list terminator begin (%s)",
8014 list_head->ll_symbol);
71c23453 8015 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
ec98ecf4 8016 "Location list terminator end (%s)",
8017 list_head->ll_symbol);
4c21a22f 8018}
80b7bd06 8019
8a8bfbe7 8020/* Output the DIE and its attributes. Called recursively to generate
8021 the definitions of each child DIE. */
ec1e49cc 8022
30ade641 8023static void
8ec3a57b 8024output_die (dw_die_ref die)
30ade641 8025{
19cb6b50 8026 dw_attr_ref a;
8027 dw_die_ref c;
8028 unsigned long size;
6f56c055 8029 unsigned ix;
6efd403b 8030
19f716e5 8031 /* If someone in another CU might refer to us, set up a symbol for
8032 them to point to. */
8033 if (die->die_symbol)
8034 output_die_symbol (die);
8035
ca98eb0a 8036 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
de064be9 8037 (unsigned long)die->die_offset,
8038 dwarf_tag_name (die->die_tag));
6efd403b 8039
6f56c055 8040 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
30ade641 8041 {
ca98eb0a 8042 const char *name = dwarf_attr_name (a->dw_attr);
8043
c90bf86c 8044 switch (AT_class (a))
8a8bfbe7 8045 {
8046 case dw_val_class_addr:
ca98eb0a 8047 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8a8bfbe7 8048 break;
30ade641 8049
a36145ca 8050 case dw_val_class_offset:
8051 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8052 "%s", name);
8053 break;
8054
fe39c28c 8055 case dw_val_class_range_list:
8056 {
8057 char *p = strchr (ranges_section_label, '\0');
8058
3201d6f1 8059 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8060 a->dw_attr_val.v.val_offset);
fe39c28c 8061 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
d08d29c0 8062 debug_ranges_section, "%s", name);
fe39c28c 8063 *p = '\0';
8064 }
8065 break;
8066
8a8bfbe7 8067 case dw_val_class_loc:
c90bf86c 8068 size = size_of_locs (AT_loc (a));
ec1e49cc 8069
8a8bfbe7 8070 /* Output the block length for this list of location operations. */
ca98eb0a 8071 dw2_asm_output_data (constant_size (size), size, "%s", name);
ec1e49cc 8072
4b72e226 8073 output_loc_sequence (AT_loc (a));
30ade641 8074 break;
8a8bfbe7 8075
8076 case dw_val_class_const:
fddebe76 8077 /* ??? It would be slightly more efficient to use a scheme like is
8078 used for unsigned constants below, but gdb 4.x does not sign
8079 extend. Gdb 5.x does sign extend. */
ca98eb0a 8080 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
30ade641 8081 break;
8a8bfbe7 8082
8083 case dw_val_class_unsigned_const:
ca98eb0a 8084 dw2_asm_output_data (constant_size (AT_unsigned (a)),
8085 AT_unsigned (a), "%s", name);
30ade641 8086 break;
8a8bfbe7 8087
8088 case dw_val_class_long_long:
ca98eb0a 8089 {
8090 unsigned HOST_WIDE_INT first, second;
8a8bfbe7 8091
8c3f468d 8092 dw2_asm_output_data (1,
8093 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
bc70bd5e 8094 "%s", name);
f80d1bcd 8095
ca98eb0a 8096 if (WORDS_BIG_ENDIAN)
8097 {
8098 first = a->dw_attr_val.v.val_long_long.hi;
8099 second = a->dw_attr_val.v.val_long_long.low;
8100 }
8101 else
8102 {
8103 first = a->dw_attr_val.v.val_long_long.low;
8104 second = a->dw_attr_val.v.val_long_long.hi;
8105 }
8c3f468d 8106
8107 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
ca98eb0a 8108 first, "long long constant");
8c3f468d 8109 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
ca98eb0a 8110 second, NULL);
8111 }
30ade641 8112 break;
8a8bfbe7 8113
1b6ad376 8114 case dw_val_class_vec:
57380eb2 8115 {
1b6ad376 8116 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8117 unsigned int len = a->dw_attr_val.v.val_vec.length;
19cb6b50 8118 unsigned int i;
1b6ad376 8119 unsigned char *p;
57380eb2 8120
2eb674c9 8121 dw2_asm_output_data (constant_size (len * elt_size),
8122 len * elt_size, "%s", name);
1b6ad376 8123 if (elt_size > sizeof (HOST_WIDE_INT))
8124 {
8125 elt_size /= 2;
8126 len *= 2;
8127 }
8128 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8129 i < len;
8130 i++, p += elt_size)
8131 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8132 "fp or vector constant word %u", i);
f80d1bcd 8133 break;
57380eb2 8134 }
8a8bfbe7 8135
8136 case dw_val_class_flag:
ca98eb0a 8137 dw2_asm_output_data (1, AT_flag (a), "%s", name);
30ade641 8138 break;
a36145ca 8139
bc70bd5e 8140 case dw_val_class_loc_list:
4c21a22f 8141 {
8142 char *sym = AT_loc_list (a)->ll_symbol;
8c3f468d 8143
7bd4f6b6 8144 gcc_assert (sym);
d08d29c0 8145 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8146 "%s", name);
4c21a22f 8147 }
8148 break;
a36145ca 8149
8a8bfbe7 8150 case dw_val_class_die_ref:
19f716e5 8151 if (AT_ref_external (a))
ca98eb0a 8152 {
8153 char *sym = AT_ref (a)->die_symbol;
8c3f468d 8154
7bd4f6b6 8155 gcc_assert (sym);
d08d29c0 8156 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8157 "%s", name);
ca98eb0a 8158 }
19f716e5 8159 else
7bd4f6b6 8160 {
8161 gcc_assert (AT_ref (a)->die_offset);
8162 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8163 "%s", name);
8164 }
30ade641 8165 break;
8a8bfbe7 8166
8167 case dw_val_class_fde_ref:
19bce576 8168 {
8169 char l1[20];
8c3f468d 8170
ca98eb0a 8171 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8172 a->dw_attr_val.v.val_fde_index * 2);
d08d29c0 8173 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8174 "%s", name);
19bce576 8175 }
30ade641 8176 break;
30ade641 8177
8a8bfbe7 8178 case dw_val_class_lbl_id:
19e5668c 8179 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8a8bfbe7 8180 break;
ec1e49cc 8181
d08d29c0 8182 case dw_val_class_lineptr:
8183 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8184 debug_line_section, "%s", name);
8185 break;
8186
8187 case dw_val_class_macptr:
8188 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8189 debug_macinfo_section, "%s", name);
8a8bfbe7 8190 break;
30ade641 8191
8a8bfbe7 8192 case dw_val_class_str:
80b7bd06 8193 if (AT_string_form (a) == DW_FORM_strp)
8194 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8195 a->dw_attr_val.v.val_str->label,
d08d29c0 8196 debug_str_section,
895ecd4c 8197 "%s: \"%s\"", name, AT_string (a));
80b7bd06 8198 else
8199 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8a8bfbe7 8200 break;
840b696a 8201
69278c24 8202 case dw_val_class_file:
8203 {
8204 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
61a9389f 8205
69278c24 8206 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8207 a->dw_attr_val.v.val_file->filename);
8208 break;
8209 }
8210
8a8bfbe7 8211 default:
7bd4f6b6 8212 gcc_unreachable ();
8a8bfbe7 8213 }
8a8bfbe7 8214 }
ec1e49cc 8215
958656b7 8216 FOR_EACH_CHILD (die, c, output_die (c));
ec1e49cc 8217
8c3f468d 8218 /* Add null byte to terminate sibling list. */
8a8bfbe7 8219 if (die->die_child != NULL)
8c3f468d 8220 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
de064be9 8221 (unsigned long) die->die_offset);
8a8bfbe7 8222}
ec1e49cc 8223
8a8bfbe7 8224/* Output the compilation unit that appears at the beginning of the
8225 .debug_info section, and precedes the DIE descriptions. */
ec1e49cc 8226
8a8bfbe7 8227static void
8ec3a57b 8228output_compilation_unit_header (void)
8a8bfbe7 8229{
65bdc57c 8230 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8231 dw2_asm_output_data (4, 0xffffffff,
8232 "Initial length escape value indicating 64-bit DWARF extension");
8233 dw2_asm_output_data (DWARF_OFFSET_SIZE,
61a9389f 8234 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
ca98eb0a 8235 "Length of Compilation Unit Info");
ca98eb0a 8236 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
ca98eb0a 8237 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
d08d29c0 8238 debug_abbrev_section,
ca98eb0a 8239 "Offset Into Abbrev. Section");
ca98eb0a 8240 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
30ade641 8241}
8242
19f716e5 8243/* Output the compilation unit DIE and its children. */
8244
8245static void
8ec3a57b 8246output_comp_unit (dw_die_ref die, int output_if_empty)
19f716e5 8247{
dd9977e9 8248 const char *secname;
51e8c210 8249 char *oldsym, *tmp;
8250
8251 /* Unless we are outputting main CU, we may throw away empty ones. */
8252 if (!output_if_empty && die->die_child == NULL)
8253 return;
19f716e5 8254
8c3f468d 8255 /* Even if there are no children of this DIE, we must output the information
8256 about the compilation unit. Otherwise, on an empty translation unit, we
8257 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8258 will then complain when examining the file. First mark all the DIEs in
8259 this CU so we know which get local refs. */
eabb26f3 8260 mark_dies (die);
8261
8262 build_abbrev_table (die);
8263
1e625a2e 8264 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
19f716e5 8265 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8266 calc_die_sizes (die);
8267
51e8c210 8268 oldsym = die->die_symbol;
8269 if (oldsym)
19f716e5 8270 {
2457c754 8271 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8c3f468d 8272
51e8c210 8273 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
dd9977e9 8274 secname = tmp;
19f716e5 8275 die->die_symbol = NULL;
2f14b1f9 8276 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
19f716e5 8277 }
8278 else
2f14b1f9 8279 switch_to_section (debug_info_section);
19f716e5 8280
8281 /* Output debugging information. */
19f716e5 8282 output_compilation_unit_header ();
8283 output_die (die);
8284
eabb26f3 8285 /* Leave the marks on the main CU, so we can check them in
8286 output_pubnames. */
51e8c210 8287 if (oldsym)
8288 {
8289 unmark_dies (die);
8290 die->die_symbol = oldsym;
8291 }
19f716e5 8292}
8293
7d709201 8294/* Return the DWARF2/3 pubname associated with a decl. */
59561872 8295
7795e5d1 8296static const char *
8ec3a57b 8297dwarf2_name (tree decl, int scope)
59561872 8298{
7d709201 8299 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
59561872 8300}
8301
dc7a29ce 8302/* Add a new entry to .debug_pubnames if appropriate. */
ec1e49cc 8303
dc7a29ce 8304static void
a12691f0 8305add_pubname_string (const char *str, dw_die_ref die)
dc7a29ce 8306{
af84796a 8307 pubname_entry e;
dc7a29ce 8308
af84796a 8309 e.die = die;
a12691f0 8310 e.name = xstrdup (str);
af84796a 8311 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8312}
8313
a12691f0 8314static void
8315add_pubname (tree decl, dw_die_ref die)
8316{
8317
8318 if (TREE_PUBLIC (decl))
8319 add_pubname_string (dwarf2_name (decl, 1), die);
8320}
8321
af84796a 8322/* Add a new entry to .debug_pubtypes if appropriate. */
8323
8324static void
8325add_pubtype (tree decl, dw_die_ref die)
8326{
8327 pubname_entry e;
8328
8329 e.name = NULL;
8330 if ((TREE_PUBLIC (decl)
8331 || die->die_parent == comp_unit_die)
8332 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
dc7a29ce 8333 {
af84796a 8334 e.die = die;
8335 if (TYPE_P (decl))
8336 {
8337 if (TYPE_NAME (decl))
8338 {
8339 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
52570507 8340 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
af84796a 8341 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8342 && DECL_NAME (TYPE_NAME (decl)))
52570507 8343 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
61a9389f 8344 else
af84796a 8345 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8346 }
8347 }
61a9389f 8348 else
af84796a 8349 e.name = xstrdup (dwarf2_name (decl, 1));
ec1e49cc 8350
af84796a 8351 /* If we don't have a name for the type, there's no point in adding
8352 it to the table. */
8353 if (e.name && e.name[0] != '\0')
8354 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8355 }
dc7a29ce 8356}
8357
30ade641 8358/* Output the public names table used to speed up access to externally
af84796a 8359 visible names; or the public types table used to find type definitions. */
ec1e49cc 8360
30ade641 8361static void
af84796a 8362output_pubnames (VEC (pubname_entry, gc) * names)
30ade641 8363{
19cb6b50 8364 unsigned i;
af84796a 8365 unsigned long pubnames_length = size_of_pubnames (names);
8366 pubname_ref pub;
ec1e49cc 8367
65bdc57c 8368 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8369 dw2_asm_output_data (4, 0xffffffff,
8370 "Initial length escape value indicating 64-bit DWARF extension");
af84796a 8371 if (names == pubname_table)
8372 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8373 "Length of Public Names Info");
8374 else
8375 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8376 "Length of Public Type Names Info");
ca98eb0a 8377 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
ca98eb0a 8378 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
d08d29c0 8379 debug_info_section,
ca98eb0a 8380 "Offset of Compilation Unit Info");
ca98eb0a 8381 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8382 "Compilation Unit Length");
ec1e49cc 8383
af84796a 8384 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
30ade641 8385 {
61a9389f 8386 /* We shouldn't see pubnames for DIEs outside of the main CU. */
af84796a 8387 if (names == pubname_table)
8388 gcc_assert (pub->die->die_mark);
19f716e5 8389
af84796a 8390 if (names != pubtype_table
8391 || pub->die->die_offset != 0
8392 || !flag_eliminate_unused_debug_types)
8393 {
8394 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8395 "DIE offset");
ec1e49cc 8396
af84796a 8397 dw2_asm_output_nstring (pub->name, -1, "external name");
8398 }
30ade641 8399 }
ec1e49cc 8400
ca98eb0a 8401 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
30ade641 8402}
8403
dc7a29ce 8404/* Add a new entry to .debug_aranges if appropriate. */
ec1e49cc 8405
dc7a29ce 8406static void
8ec3a57b 8407add_arange (tree decl, dw_die_ref die)
dc7a29ce 8408{
8409 if (! DECL_SECTION_NAME (decl))
8410 return;
8411
8412 if (arange_table_in_use == arange_table_allocated)
8413 {
8414 arange_table_allocated += ARANGE_TABLE_INCREMENT;
2457c754 8415 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8416 arange_table_allocated);
573aba85 8417 memset (arange_table + arange_table_in_use, 0,
8418 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
dc7a29ce 8419 }
ec1e49cc 8420
dc7a29ce 8421 arange_table[arange_table_in_use++] = die;
8422}
8423
30ade641 8424/* Output the information that goes into the .debug_aranges table.
8425 Namely, define the beginning and ending address range of the
8426 text section generated for this compilation unit. */
ec1e49cc 8427
30ade641 8428static void
8ec3a57b 8429output_aranges (void)
30ade641 8430{
19cb6b50 8431 unsigned i;
8432 unsigned long aranges_length = size_of_aranges ();
ec1e49cc 8433
65bdc57c 8434 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8435 dw2_asm_output_data (4, 0xffffffff,
8436 "Initial length escape value indicating 64-bit DWARF extension");
ca98eb0a 8437 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8438 "Length of Address Ranges Info");
ca98eb0a 8439 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
ca98eb0a 8440 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
d08d29c0 8441 debug_info_section,
ca98eb0a 8442 "Offset of Compilation Unit Info");
ca98eb0a 8443 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
ca98eb0a 8444 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
ec1e49cc 8445
e711a040 8446 /* We need to align to twice the pointer size here. */
8447 if (DWARF_ARANGES_PAD_SIZE)
8448 {
ca98eb0a 8449 /* Pad using a 2 byte words so that padding is correct for any
c83a163c 8450 pointer size. */
ca98eb0a 8451 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8452 2 * DWARF2_ADDR_SIZE);
950ae8fe 8453 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
ca98eb0a 8454 dw2_asm_output_data (2, 0, NULL);
e711a040 8455 }
ec1e49cc 8456
d6de7df9 8457 /* It is necessary not to output these entries if the sections were
8458 not used; if the sections were not used, the length will be 0 and
8459 the address may end up as 0 if the section is discarded by ld
8460 --gc-sections, leaving an invalid (0, 0) entry that can be
8461 confused with the terminator. */
8462 if (text_section_used)
8463 {
8464 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8465 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8466 text_section_label, "Length");
8467 }
8468 if (cold_text_section_used)
4d0e931f 8469 {
61a9389f 8470 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
4d0e931f 8471 "Address");
8472 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8473 cold_text_section_label, "Length");
8474 }
ec1e49cc 8475
8c3f468d 8476 for (i = 0; i < arange_table_in_use; i++)
dc7a29ce 8477 {
2b553659 8478 dw_die_ref die = arange_table[i];
ec1e49cc 8479
19f716e5 8480 /* We shouldn't see aranges for DIEs outside of the main CU. */
7bd4f6b6 8481 gcc_assert (die->die_mark);
19f716e5 8482
2b553659 8483 if (die->die_tag == DW_TAG_subprogram)
ca98eb0a 8484 {
19e5668c 8485 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7cc7e163 8486 "Address");
ca98eb0a 8487 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8488 get_AT_low_pc (die), "Length");
8489 }
dc7a29ce 8490 else
59561872 8491 {
2b553659 8492 /* A static variable; extract the symbol from DW_AT_location.
8493 Note that this code isn't currently hit, as we only emit
8494 aranges for functions (jason 9/23/99). */
2b553659 8495 dw_attr_ref a = get_AT (die, DW_AT_location);
8496 dw_loc_descr_ref loc;
8c3f468d 8497
7bd4f6b6 8498 gcc_assert (a && AT_class (a) == dw_val_class_loc);
2b553659 8499
c90bf86c 8500 loc = AT_loc (a);
7bd4f6b6 8501 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
2b553659 8502
ca98eb0a 8503 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8504 loc->dw_loc_oprnd1.v.val_addr, "Address");
8505 dw2_asm_output_data (DWARF2_ADDR_SIZE,
8506 get_AT_unsigned (die, DW_AT_byte_size),
8507 "Length");
59561872 8508 }
dc7a29ce 8509 }
ec1e49cc 8510
30ade641 8511 /* Output the terminator words. */
ca98eb0a 8512 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8513 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
30ade641 8514}
8515
a36145ca 8516/* Add a new entry to .debug_ranges. Return the offset at which it
8517 was placed. */
8518
8519static unsigned int
f221c0bd 8520add_ranges_num (int num)
a36145ca 8521{
8522 unsigned int in_use = ranges_table_in_use;
8523
8524 if (in_use == ranges_table_allocated)
8525 {
8526 ranges_table_allocated += RANGES_TABLE_INCREMENT;
2457c754 8527 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8528 ranges_table_allocated);
573aba85 8529 memset (ranges_table + ranges_table_in_use, 0,
8530 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
a36145ca 8531 }
8532
f221c0bd 8533 ranges_table[in_use].num = num;
a36145ca 8534 ranges_table_in_use = in_use + 1;
8535
8536 return in_use * 2 * DWARF2_ADDR_SIZE;
8537}
8538
f221c0bd 8539/* Add a new entry to .debug_ranges corresponding to a block, or a
8540 range terminator if BLOCK is NULL. */
8541
8542static unsigned int
5493cb9a 8543add_ranges (const_tree block)
f221c0bd 8544{
8545 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8546}
8547
8548/* Add a new entry to .debug_ranges corresponding to a pair of
8549 labels. */
8550
8551static unsigned int
8552add_ranges_by_labels (const char *begin, const char *end)
8553{
8554 unsigned int in_use = ranges_by_label_in_use;
8555
8556 if (in_use == ranges_by_label_allocated)
8557 {
8558 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
2457c754 8559 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8560 ranges_by_label,
8561 ranges_by_label_allocated);
f221c0bd 8562 memset (ranges_by_label + ranges_by_label_in_use, 0,
8563 RANGES_TABLE_INCREMENT
8564 * sizeof (struct dw_ranges_by_label_struct));
8565 }
8566
8567 ranges_by_label[in_use].begin = begin;
8568 ranges_by_label[in_use].end = end;
8569 ranges_by_label_in_use = in_use + 1;
8570
8571 return add_ranges_num (-(int)in_use - 1);
8572}
8573
a36145ca 8574static void
8ec3a57b 8575output_ranges (void)
a36145ca 8576{
19cb6b50 8577 unsigned i;
0d95286f 8578 static const char *const start_fmt = "Offset 0x%x";
a36145ca 8579 const char *fmt = start_fmt;
8580
8c3f468d 8581 for (i = 0; i < ranges_table_in_use; i++)
a36145ca 8582 {
f221c0bd 8583 int block_num = ranges_table[i].num;
a36145ca 8584
f221c0bd 8585 if (block_num > 0)
a36145ca 8586 {
8587 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8588 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8589
8590 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8591 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8592
8593 /* If all code is in the text section, then the compilation
8594 unit base address defaults to DW_AT_low_pc, which is the
8595 base of the text section. */
dae1861f 8596 if (!have_multiple_function_sections)
a36145ca 8597 {
4d0e931f 8598 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8599 text_section_label,
8600 fmt, i * 2 * DWARF2_ADDR_SIZE);
8601 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8602 text_section_label, NULL);
a36145ca 8603 }
8c3f468d 8604
f221c0bd 8605 /* Otherwise, the compilation unit base address is zero,
8606 which allows us to use absolute addresses, and not worry
8607 about whether the target supports cross-section
8608 arithmetic. */
a36145ca 8609 else
8610 {
8611 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8612 fmt, i * 2 * DWARF2_ADDR_SIZE);
8613 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8614 }
8615
8616 fmt = NULL;
8617 }
f221c0bd 8618
8619 /* Negative block_num stands for an index into ranges_by_label. */
8620 else if (block_num < 0)
8621 {
8622 int lab_idx = - block_num - 1;
8623
8624 if (!have_multiple_function_sections)
8625 {
8626 gcc_unreachable ();
8627#if 0
8628 /* If we ever use add_ranges_by_labels () for a single
8629 function section, all we have to do is to take out
8630 the #if 0 above. */
8631 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8632 ranges_by_label[lab_idx].begin,
8633 text_section_label,
8634 fmt, i * 2 * DWARF2_ADDR_SIZE);
8635 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8636 ranges_by_label[lab_idx].end,
8637 text_section_label, NULL);
8638#endif
8639 }
8640 else
8641 {
8642 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8643 ranges_by_label[lab_idx].begin,
8644 fmt, i * 2 * DWARF2_ADDR_SIZE);
8645 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8646 ranges_by_label[lab_idx].end,
8647 NULL);
8648 }
8649 }
a36145ca 8650 else
8651 {
8652 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8653 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8654 fmt = start_fmt;
8655 }
8656 }
8657}
ac02093f 8658
8659/* Data structure containing information about input files. */
8660struct file_info
8661{
69278c24 8662 const char *path; /* Complete file name. */
8663 const char *fname; /* File name part. */
ac02093f 8664 int length; /* Length of entire string. */
69278c24 8665 struct dwarf_file_data * file_idx; /* Index in input file table. */
ac02093f 8666 int dir_idx; /* Index in directory table. */
8667};
8668
8669/* Data structure containing information about directories with source
8670 files. */
8671struct dir_info
8672{
69278c24 8673 const char *path; /* Path including directory name. */
ac02093f 8674 int length; /* Path length. */
8675 int prefix; /* Index of directory entry which is a prefix. */
ac02093f 8676 int count; /* Number of files in this directory. */
8677 int dir_idx; /* Index of directory used as base. */
ac02093f 8678};
8679
8680/* Callback function for file_info comparison. We sort by looking at
8681 the directories in the path. */
5fbe2ebb 8682
ac02093f 8683static int
8ec3a57b 8684file_info_cmp (const void *p1, const void *p2)
ac02093f 8685{
2457c754 8686 const struct file_info *const s1 = (const struct file_info *) p1;
8687 const struct file_info *const s2 = (const struct file_info *) p2;
c1fdef8e 8688 const unsigned char *cp1;
8689 const unsigned char *cp2;
ac02093f 8690
5fbe2ebb 8691 /* Take care of file names without directories. We need to make sure that
8692 we return consistent values to qsort since some will get confused if
8693 we return the same value when identical operands are passed in opposite
8694 orders. So if neither has a directory, return 0 and otherwise return
8695 1 or -1 depending on which one has the directory. */
8696 if ((s1->path == s1->fname || s2->path == s2->fname))
8697 return (s2->path == s2->fname) - (s1->path == s1->fname);
ac02093f 8698
c1fdef8e 8699 cp1 = (const unsigned char *) s1->path;
8700 cp2 = (const unsigned char *) s2->path;
ac02093f 8701
8702 while (1)
8703 {
8704 ++cp1;
8705 ++cp2;
5fbe2ebb 8706 /* Reached the end of the first path? If so, handle like above. */
c1fdef8e 8707 if ((cp1 == (const unsigned char *) s1->fname)
8708 || (cp2 == (const unsigned char *) s2->fname))
8709 return ((cp2 == (const unsigned char *) s2->fname)
8710 - (cp1 == (const unsigned char *) s1->fname));
ac02093f 8711
8712 /* Character of current path component the same? */
5fbe2ebb 8713 else if (*cp1 != *cp2)
ac02093f 8714 return *cp1 - *cp2;
8715 }
8716}
8717
61a9389f 8718struct file_name_acquire_data
69278c24 8719{
8720 struct file_info *files;
8721 int used_files;
8722 int max_files;
8723};
8724
8725/* Traversal function for the hash table. */
8726
8727static int
8728file_name_acquire (void ** slot, void *data)
8729{
2457c754 8730 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8731 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
69278c24 8732 struct file_info *fi;
8733 const char *f;
8734
8735 gcc_assert (fnad->max_files >= d->emitted_number);
8736
8737 if (! d->emitted_number)
8738 return 1;
8739
8740 gcc_assert (fnad->max_files != fnad->used_files);
8741
8742 fi = fnad->files + fnad->used_files++;
8743
8744 /* Skip all leading "./". */
8745 f = d->filename;
974a92fe 8746 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
69278c24 8747 f += 2;
61a9389f 8748
69278c24 8749 /* Create a new array entry. */
8750 fi->path = f;
8751 fi->length = strlen (f);
8752 fi->file_idx = d;
61a9389f 8753
69278c24 8754 /* Search for the file name part. */
974a92fe 8755 f = strrchr (f, DIR_SEPARATOR);
8756#if defined (DIR_SEPARATOR_2)
8757 {
8defa33e 8758 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
974a92fe 8759
8760 if (g != NULL)
8761 {
8762 if (f == NULL || f < g)
8763 f = g;
8764 }
8765 }
8766#endif
8767
69278c24 8768 fi->fname = f == NULL ? fi->path : f + 1;
8769 return 1;
8770}
8771
ac02093f 8772/* Output the directory table and the file name table. We try to minimize
8773 the total amount of memory needed. A heuristic is used to avoid large
8774 slowdowns with many input files. */
8c3f468d 8775
ac02093f 8776static void
8ec3a57b 8777output_file_names (void)
ac02093f 8778{
69278c24 8779 struct file_name_acquire_data fnad;
8780 int numfiles;
ac02093f 8781 struct file_info *files;
8782 struct dir_info *dirs;
8783 int *saved;
8784 int *savehere;
8785 int *backmap;
69278c24 8786 int ndirs;
ac02093f 8787 int idx_offset;
69278c24 8788 int i;
ac02093f 8789 int idx;
8790
69278c24 8791 if (!last_emitted_file)
21d1bacf 8792 {
8793 dw2_asm_output_data (1, 0, "End directory table");
8794 dw2_asm_output_data (1, 0, "End file name table");
8795 return;
8796 }
8797
69278c24 8798 numfiles = last_emitted_file->emitted_number;
ac02093f 8799
69278c24 8800 /* Allocate the various arrays we need. */
2457c754 8801 files = XALLOCAVEC (struct file_info, numfiles);
8802 dirs = XALLOCAVEC (struct dir_info, numfiles);
ac02093f 8803
69278c24 8804 fnad.files = files;
8805 fnad.used_files = 0;
8806 fnad.max_files = numfiles;
8807 htab_traverse (file_table, file_name_acquire, &fnad);
8808 gcc_assert (fnad.used_files == fnad.max_files);
8c3f468d 8809
69278c24 8810 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
ac02093f 8811
8812 /* Find all the different directories used. */
69278c24 8813 dirs[0].path = files[0].path;
8814 dirs[0].length = files[0].fname - files[0].path;
ac02093f 8815 dirs[0].prefix = -1;
ac02093f 8816 dirs[0].count = 1;
8817 dirs[0].dir_idx = 0;
69278c24 8818 files[0].dir_idx = 0;
ac02093f 8819 ndirs = 1;
8820
69278c24 8821 for (i = 1; i < numfiles; i++)
ac02093f 8822 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8823 && memcmp (dirs[ndirs - 1].path, files[i].path,
8824 dirs[ndirs - 1].length) == 0)
8825 {
8826 /* Same directory as last entry. */
8827 files[i].dir_idx = ndirs - 1;
ac02093f 8828 ++dirs[ndirs - 1].count;
8829 }
8830 else
8831 {
69278c24 8832 int j;
ac02093f 8833
8834 /* This is a new directory. */
8835 dirs[ndirs].path = files[i].path;
8836 dirs[ndirs].length = files[i].fname - files[i].path;
ac02093f 8837 dirs[ndirs].count = 1;
8838 dirs[ndirs].dir_idx = ndirs;
ac02093f 8839 files[i].dir_idx = ndirs;
8840
8841 /* Search for a prefix. */
3740694f 8842 dirs[ndirs].prefix = -1;
8c3f468d 8843 for (j = 0; j < ndirs; j++)
3740694f 8844 if (dirs[j].length < dirs[ndirs].length
8845 && dirs[j].length > 1
8846 && (dirs[ndirs].prefix == -1
8847 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8848 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8849 dirs[ndirs].prefix = j;
ac02093f 8850
8851 ++ndirs;
8852 }
8853
8c3f468d 8854 /* Now to the actual work. We have to find a subset of the directories which
8855 allow expressing the file name using references to the directory table
8856 with the least amount of characters. We do not do an exhaustive search
8857 where we would have to check out every combination of every single
8858 possible prefix. Instead we use a heuristic which provides nearly optimal
8859 results in most cases and never is much off. */
2457c754 8860 saved = XALLOCAVEC (int, ndirs);
8861 savehere = XALLOCAVEC (int, ndirs);
ac02093f 8862
8863 memset (saved, '\0', ndirs * sizeof (saved[0]));
8c3f468d 8864 for (i = 0; i < ndirs; i++)
ac02093f 8865 {
69278c24 8866 int j;
ac02093f 8867 int total;
8868
8c3f468d 8869 /* We can always save some space for the current directory. But this
8870 does not mean it will be enough to justify adding the directory. */
ac02093f 8871 savehere[i] = dirs[i].length;
8872 total = (savehere[i] - saved[i]) * dirs[i].count;
8873
8c3f468d 8874 for (j = i + 1; j < ndirs; j++)
ac02093f 8875 {
8876 savehere[j] = 0;
ac02093f 8877 if (saved[j] < dirs[i].length)
8878 {
8879 /* Determine whether the dirs[i] path is a prefix of the
8880 dirs[j] path. */
8881 int k;
8882
3740694f 8883 k = dirs[j].prefix;
ff279357 8884 while (k != -1 && k != (int) i)
3740694f 8885 k = dirs[k].prefix;
8886
ff279357 8887 if (k == (int) i)
3740694f 8888 {
69278c24 8889 /* Yes it is. We can possibly save some memory by
3740694f 8890 writing the filenames in dirs[j] relative to
8891 dirs[i]. */
8892 savehere[j] = dirs[i].length;
8893 total += (savehere[j] - saved[j]) * dirs[j].count;
8894 }
ac02093f 8895 }
8896 }
8897
69278c24 8898 /* Check whether we can save enough to justify adding the dirs[i]
ac02093f 8899 directory. */
8900 if (total > dirs[i].length + 1)
8901 {
3740694f 8902 /* It's worthwhile adding. */
bc70bd5e 8903 for (j = i; j < ndirs; j++)
ac02093f 8904 if (savehere[j] > 0)
8905 {
8906 /* Remember how much we saved for this directory so far. */
8907 saved[j] = savehere[j];
8908
8909 /* Remember the prefix directory. */
8910 dirs[j].dir_idx = i;
8911 }
8912 }
8913 }
8914
69278c24 8915 /* Emit the directory name table. */
ac02093f 8916 idx = 1;
f9038ab4 8917 idx_offset = dirs[0].length > 0 ? 1 : 0;
8c3f468d 8918 for (i = 1 - idx_offset; i < ndirs; i++)
69278c24 8919 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8920 "Directory Entry: 0x%x", i + idx_offset);
8c3f468d 8921
ca98eb0a 8922 dw2_asm_output_data (1, 0, "End directory table");
8923
69278c24 8924 /* We have to emit them in the order of emitted_number since that's
8925 used in the debug info generation. To do this efficiently we
8926 generate a back-mapping of the indices first. */
2457c754 8927 backmap = XALLOCAVEC (int, numfiles);
69278c24 8928 for (i = 0; i < numfiles; i++)
8929 backmap[files[i].file_idx->emitted_number - 1] = i;
ac02093f 8930
8931 /* Now write all the file names. */
69278c24 8932 for (i = 0; i < numfiles; i++)
ac02093f 8933 {
8934 int file_idx = backmap[i];
8935 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8936
ca98eb0a 8937 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
69278c24 8938 "File Entry: 0x%x", (unsigned) i + 1);
ac02093f 8939
8940 /* Include directory index. */
69278c24 8941 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
ac02093f 8942
8943 /* Modification time. */
ca98eb0a 8944 dw2_asm_output_data_uleb128 (0, NULL);
ac02093f 8945
8946 /* File length in bytes. */
ca98eb0a 8947 dw2_asm_output_data_uleb128 (0, NULL);
ac02093f 8948 }
8c3f468d 8949
ca98eb0a 8950 dw2_asm_output_data (1, 0, "End file name table");
ac02093f 8951}
8952
8953
30ade641 8954/* Output the source line number correspondence information. This
155b05dc 8955 information goes into the .debug_line section. */
ec1e49cc 8956
30ade641 8957static void
8ec3a57b 8958output_line_info (void)
30ade641 8959{
3740694f 8960 char l1[20], l2[20], p1[20], p2[20];
30ade641 8961 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8962 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
19cb6b50 8963 unsigned opc;
8964 unsigned n_op_args;
8965 unsigned long lt_index;
8966 unsigned long current_line;
8967 long line_offset;
8968 long line_delta;
8969 unsigned long current_file;
8970 unsigned long function;
ec1e49cc 8971
ca98eb0a 8972 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8973 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
3740694f 8974 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8975 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
ec1e49cc 8976
65bdc57c 8977 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8978 dw2_asm_output_data (4, 0xffffffff,
8979 "Initial length escape value indicating 64-bit DWARF extension");
ca98eb0a 8980 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8981 "Length of Source Line Info");
8982 ASM_OUTPUT_LABEL (asm_out_file, l1);
ec1e49cc 8983
ca98eb0a 8984 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
3740694f 8985 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8986 ASM_OUTPUT_LABEL (asm_out_file, p1);
ec1e49cc 8987
bfba49c6 8988 /* Define the architecture-dependent minimum instruction length (in
8989 bytes). In this implementation of DWARF, this field is used for
8990 information purposes only. Since GCC generates assembly language,
8991 we have no a priori knowledge of how many instruction bytes are
8992 generated for each source line, and therefore can use only the
8993 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8994 commands. Accordingly, we fix this as `1', which is "correct
8995 enough" for all architectures, and don't let the target override. */
8996 dw2_asm_output_data (1, 1,
ca98eb0a 8997 "Minimum Instruction Length");
bfba49c6 8998
ca98eb0a 8999 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9000 "Default is_stmt_start flag");
ca98eb0a 9001 dw2_asm_output_data (1, DWARF_LINE_BASE,
9002 "Line Base Value (Special Opcodes)");
ca98eb0a 9003 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9004 "Line Range Value (Special Opcodes)");
ca98eb0a 9005 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9006 "Special Opcode Base");
ec1e49cc 9007
8c3f468d 9008 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
30ade641 9009 {
9010 switch (opc)
9011 {
9012 case DW_LNS_advance_pc:
9013 case DW_LNS_advance_line:
9014 case DW_LNS_set_file:
9015 case DW_LNS_set_column:
9016 case DW_LNS_fixed_advance_pc:
9017 n_op_args = 1;
9018 break;
9019 default:
9020 n_op_args = 0;
9021 break;
9022 }
ca98eb0a 9023
9024 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9025 opc, n_op_args);
30ade641 9026 }
ec1e49cc 9027
ac02093f 9028 /* Write out the information about the files we use. */
9029 output_file_names ();
3740694f 9030 ASM_OUTPUT_LABEL (asm_out_file, p2);
30ade641 9031
d8488b8a 9032 /* We used to set the address register to the first location in the text
9033 section here, but that didn't accomplish anything since we already
9034 have a line note for the opening brace of the first function. */
30ade641 9035
9036 /* Generate the line number to PC correspondence table, encoded as
9037 a series of state machine operations. */
9038 current_file = 1;
9039 current_line = 1;
4d0e931f 9040
5fbee89d 9041 if (cfun && in_cold_section_p)
abe32cce 9042 strcpy (prev_line_label, crtl->subsections.cold_section_label);
1897b881 9043 else
9044 strcpy (prev_line_label, text_section_label);
30ade641 9045 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9046 {
19cb6b50 9047 dw_line_info_ref line_info = &line_info_table[lt_index];
d8488b8a 9048
e7b3c55c 9049#if 0
9050 /* Disable this optimization for now; GDB wants to see two line notes
9051 at the beginning of a function so it can find the end of the
9052 prologue. */
9053
d8488b8a 9054 /* Don't emit anything for redundant notes. Just updating the
c83a163c 9055 address doesn't accomplish anything, because we already assume
9056 that anything after the last address is this line. */
d8488b8a 9057 if (line_info->dw_line_num == current_line
9058 && line_info->dw_file_num == current_file)
9059 continue;
e7b3c55c 9060#endif
ec1e49cc 9061
ca98eb0a 9062 /* Emit debug info for the address of the current line.
9063
9064 Unfortunately, we have little choice here currently, and must always
8c3f468d 9065 use the most general form. GCC does not know the address delta
ca98eb0a 9066 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
9067 attributes which will give an upper bound on the address range. We
9068 could perhaps use length attributes to determine when it is safe to
9069 use DW_LNS_fixed_advance_pc. */
9070
d58978a6 9071 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
db998a6a 9072 if (0)
9073 {
9074 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
ca98eb0a 9075 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9076 "DW_LNS_fixed_advance_pc");
9077 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
db998a6a 9078 }
9079 else
9080 {
aaa408cd 9081 /* This can handle any delta. This takes
c83a163c 9082 4+DWARF2_ADDR_SIZE bytes. */
ca98eb0a 9083 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9084 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9085 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 9086 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
db998a6a 9087 }
8c3f468d 9088
db998a6a 9089 strcpy (prev_line_label, line_label);
9090
9091 /* Emit debug info for the source file of the current line, if
9092 different from the previous line. */
30ade641 9093 if (line_info->dw_file_num != current_file)
9094 {
9095 current_file = line_info->dw_file_num;
ca98eb0a 9096 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
69278c24 9097 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
30ade641 9098 }
ec1e49cc 9099
db998a6a 9100 /* Emit debug info for the current line number, choosing the encoding
9101 that uses the least amount of space. */
d8488b8a 9102 if (line_info->dw_line_num != current_line)
30ade641 9103 {
d8488b8a 9104 line_offset = line_info->dw_line_num - current_line;
9105 line_delta = line_offset - DWARF_LINE_BASE;
9106 current_line = line_info->dw_line_num;
9107 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8c3f468d 9108 /* This can handle deltas from -10 to 234, using the current
9109 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
9110 takes 1 byte. */
9111 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9112 "line %lu", current_line);
d8488b8a 9113 else
9114 {
9115 /* This can handle any delta. This takes at least 4 bytes,
9116 depending on the value being encoded. */
ca98eb0a 9117 dw2_asm_output_data (1, DW_LNS_advance_line,
9118 "advance to line %lu", current_line);
9119 dw2_asm_output_data_sleb128 (line_offset, NULL);
9120 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
d8488b8a 9121 }
6efd403b 9122 }
9123 else
8c3f468d 9124 /* We still need to start a new row, so output a copy insn. */
9125 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
30ade641 9126 }
9127
db998a6a 9128 /* Emit debug info for the address of the end of the function. */
9129 if (0)
9130 {
ca98eb0a 9131 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9132 "DW_LNS_fixed_advance_pc");
9133 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
db998a6a 9134 }
9135 else
9136 {
ca98eb0a 9137 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9138 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9139 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 9140 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
db998a6a 9141 }
6ed29fb8 9142
ca98eb0a 9143 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9144 dw2_asm_output_data_uleb128 (1, NULL);
9145 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
c05d7491 9146
9147 function = 0;
9148 current_file = 1;
9149 current_line = 1;
f80d1bcd 9150 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
c05d7491 9151 {
19cb6b50 9152 dw_separate_line_info_ref line_info
c05d7491 9153 = &separate_line_info_table[lt_index];
ec1e49cc 9154
e7b3c55c 9155#if 0
d8488b8a 9156 /* Don't emit anything for redundant notes. */
9157 if (line_info->dw_line_num == current_line
9158 && line_info->dw_file_num == current_file
9159 && line_info->function == function)
9160 goto cont;
e7b3c55c 9161#endif
d8488b8a 9162
db998a6a 9163 /* Emit debug info for the address of the current line. If this is
9164 a new function, or the first line of a function, then we need
9165 to handle it differently. */
d58978a6 9166 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9167 lt_index);
c05d7491 9168 if (function != line_info->function)
9169 {
9170 function = line_info->function;
ec1e49cc 9171
2358393e 9172 /* Set the address register to the first line in the function. */
ca98eb0a 9173 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9174 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9175 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 9176 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
c05d7491 9177 }
9178 else
9179 {
db998a6a 9180 /* ??? See the DW_LNS_advance_pc comment above. */
9181 if (0)
9182 {
ca98eb0a 9183 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9184 "DW_LNS_fixed_advance_pc");
9185 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
db998a6a 9186 }
9187 else
9188 {
ca98eb0a 9189 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9190 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9191 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 9192 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
db998a6a 9193 }
c05d7491 9194 }
8c3f468d 9195
db998a6a 9196 strcpy (prev_line_label, line_label);
ec1e49cc 9197
db998a6a 9198 /* Emit debug info for the source file of the current line, if
9199 different from the previous line. */
c05d7491 9200 if (line_info->dw_file_num != current_file)
9201 {
9202 current_file = line_info->dw_file_num;
ca98eb0a 9203 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
69278c24 9204 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
c05d7491 9205 }
ec1e49cc 9206
db998a6a 9207 /* Emit debug info for the current line number, choosing the encoding
9208 that uses the least amount of space. */
c05d7491 9209 if (line_info->dw_line_num != current_line)
9210 {
9211 line_offset = line_info->dw_line_num - current_line;
9212 line_delta = line_offset - DWARF_LINE_BASE;
9213 current_line = line_info->dw_line_num;
9214 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
ca98eb0a 9215 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9216 "line %lu", current_line);
c05d7491 9217 else
9218 {
ca98eb0a 9219 dw2_asm_output_data (1, DW_LNS_advance_line,
9220 "advance to line %lu", current_line);
9221 dw2_asm_output_data_sleb128 (line_offset, NULL);
9222 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
c05d7491 9223 }
9224 }
d8488b8a 9225 else
ca98eb0a 9226 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
ec1e49cc 9227
e7b3c55c 9228#if 0
d8488b8a 9229 cont:
e7b3c55c 9230#endif
8c3f468d 9231
9232 lt_index++;
c05d7491 9233
9234 /* If we're done with a function, end its sequence. */
9235 if (lt_index == separate_line_info_table_in_use
9236 || separate_line_info_table[lt_index].function != function)
9237 {
9238 current_file = 1;
9239 current_line = 1;
ec1e49cc 9240
db998a6a 9241 /* Emit debug info for the address of the end of the function. */
d58978a6 9242 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
db998a6a 9243 if (0)
9244 {
ca98eb0a 9245 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9246 "DW_LNS_fixed_advance_pc");
9247 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
db998a6a 9248 }
9249 else
9250 {
ca98eb0a 9251 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9252 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9253 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 9254 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
db998a6a 9255 }
c05d7491 9256
9257 /* Output the marker for the end of this sequence. */
ca98eb0a 9258 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9259 dw2_asm_output_data_uleb128 (1, NULL);
9260 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
c05d7491 9261 }
9262 }
d6d10a79 9263
9264 /* Output the marker for the end of the line number info. */
ca98eb0a 9265 ASM_OUTPUT_LABEL (asm_out_file, l2);
30ade641 9266}
9267\f
30ade641 9268/* Given a pointer to a tree node for some base type, return a pointer to
9269 a DIE that describes the given type.
9270
9271 This routine must only be called for GCC type nodes that correspond to
9272 Dwarf base (fundamental) types. */
ec1e49cc 9273
30ade641 9274static dw_die_ref
8ec3a57b 9275base_type_die (tree type)
30ade641 9276{
19cb6b50 9277 dw_die_ref base_type_result;
19cb6b50 9278 enum dwarf_type encoding;
30ade641 9279
8c3f468d 9280 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
30ade641 9281 return 0;
9282
9283 switch (TREE_CODE (type))
9284 {
30ade641 9285 case INTEGER_TYPE:
e026e576 9286 if (TYPE_STRING_FLAG (type))
30ade641 9287 {
78a8ed03 9288 if (TYPE_UNSIGNED (type))
e026e576 9289 encoding = DW_ATE_unsigned_char;
5b67860b 9290 else
e026e576 9291 encoding = DW_ATE_signed_char;
30ade641 9292 }
e026e576 9293 else if (TYPE_UNSIGNED (type))
9294 encoding = DW_ATE_unsigned;
5b67860b 9295 else
e026e576 9296 encoding = DW_ATE_signed;
30ade641 9297 break;
9298
9299 case REAL_TYPE:
069b07bf 9300 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9301 encoding = DW_ATE_decimal_float;
9302 else
9303 encoding = DW_ATE_float;
30ade641 9304 break;
9305
06f0b99c 9306 case FIXED_POINT_TYPE:
9307 if (TYPE_UNSIGNED (type))
06f0b99c 9308 encoding = DW_ATE_unsigned_fixed;
fc0a87d6 9309 else
9310 encoding = DW_ATE_signed_fixed;
06f0b99c 9311 break;
9312
5b5abf88 9313 /* Dwarf2 doesn't know anything about complex ints, so use
9314 a user defined type for it. */
30ade641 9315 case COMPLEX_TYPE:
5b5abf88 9316 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9317 encoding = DW_ATE_complex_float;
9318 else
9319 encoding = DW_ATE_lo_user;
30ade641 9320 break;
9321
9322 case BOOLEAN_TYPE:
5b67860b 9323 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9324 encoding = DW_ATE_boolean;
30ade641 9325 break;
9326
9327 default:
8c3f468d 9328 /* No other TREE_CODEs are Dwarf fundamental types. */
7bd4f6b6 9329 gcc_unreachable ();
30ade641 9330 }
9331
15cfae4e 9332 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
155b05dc 9333
1524656f 9334 /* This probably indicates a bug. */
9335 if (! TYPE_NAME (type))
9336 add_name_attribute (base_type_result, "__unknown__");
9337
5b67860b 9338 add_AT_unsigned (base_type_result, DW_AT_byte_size,
21638aad 9339 int_size_in_bytes (type));
5b67860b 9340 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
30ade641 9341
9342 return base_type_result;
9343}
9344
6ef828f9 9345/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
30ade641 9346 given input type is a Dwarf "fundamental" type. Otherwise return null. */
ec1e49cc 9347
9348static inline int
8ec3a57b 9349is_base_type (tree type)
30ade641 9350{
9351 switch (TREE_CODE (type))
9352 {
9353 case ERROR_MARK:
9354 case VOID_TYPE:
9355 case INTEGER_TYPE:
9356 case REAL_TYPE:
06f0b99c 9357 case FIXED_POINT_TYPE:
30ade641 9358 case COMPLEX_TYPE:
9359 case BOOLEAN_TYPE:
30ade641 9360 return 1;
9361
30ade641 9362 case ARRAY_TYPE:
9363 case RECORD_TYPE:
9364 case UNION_TYPE:
9365 case QUAL_UNION_TYPE:
9366 case ENUMERAL_TYPE:
9367 case FUNCTION_TYPE:
9368 case METHOD_TYPE:
9369 case POINTER_TYPE:
9370 case REFERENCE_TYPE:
30ade641 9371 case OFFSET_TYPE:
9372 case LANG_TYPE:
4405d230 9373 case VECTOR_TYPE:
30ade641 9374 return 0;
9375
9376 default:
7bd4f6b6 9377 gcc_unreachable ();
30ade641 9378 }
ec1e49cc 9379
30ade641 9380 return 0;
9381}
9382
805e22b2 9383/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9384 node, return the size in bits for the type if it is a constant, or else
9385 return the alignment for the type if the type's size is not constant, or
9386 else return BITS_PER_WORD if the type actually turns out to be an
9387 ERROR_MARK node. */
9388
9389static inline unsigned HOST_WIDE_INT
5493cb9a 9390simple_type_size_in_bits (const_tree type)
805e22b2 9391{
805e22b2 9392 if (TREE_CODE (type) == ERROR_MARK)
9393 return BITS_PER_WORD;
9394 else if (TYPE_SIZE (type) == NULL_TREE)
9395 return 0;
9396 else if (host_integerp (TYPE_SIZE (type), 1))
9397 return tree_low_cst (TYPE_SIZE (type), 1);
9398 else
9399 return TYPE_ALIGN (type);
9400}
9401
600dbd47 9402/* Return true if the debug information for the given type should be
9403 emitted as a subrange type. */
9404
9405static inline bool
5493cb9a 9406is_subrange_type (const_tree type)
6114cbf0 9407{
93c7db82 9408 tree subtype = TREE_TYPE (type);
9409
fd45b48c 9410 /* Subrange types are identified by the fact that they are integer
9411 types, and that they have a subtype which is either an integer type
9412 or an enumeral type. */
9413
9414 if (TREE_CODE (type) != INTEGER_TYPE
9415 || subtype == NULL_TREE)
9416 return false;
9417
9418 if (TREE_CODE (subtype) != INTEGER_TYPE
69c2baa9 9419 && TREE_CODE (subtype) != ENUMERAL_TYPE
9420 && TREE_CODE (subtype) != BOOLEAN_TYPE)
fd45b48c 9421 return false;
9422
62351b00 9423 if (TREE_CODE (type) == TREE_CODE (subtype)
9424 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9425 && TYPE_MIN_VALUE (type) != NULL
9426 && TYPE_MIN_VALUE (subtype) != NULL
9427 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9428 && TYPE_MAX_VALUE (type) != NULL
9429 && TYPE_MAX_VALUE (subtype) != NULL
9430 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9431 {
9432 /* The type and its subtype have the same representation. If in
61a9389f 9433 addition the two types also have the same name, then the given
9434 type is not a subrange type, but rather a plain base type. */
62351b00 9435 /* FIXME: brobecker/2004-03-22:
61a9389f 9436 Sizetype INTEGER_CSTs nodes are canonicalized. It should
9437 therefore be sufficient to check the TYPE_SIZE node pointers
9438 rather than checking the actual size. Unfortunately, we have
9439 found some cases, such as in the Ada "integer" type, where
9440 this is not the case. Until this problem is solved, we need to
9441 keep checking the actual size. */
62351b00 9442 tree type_name = TYPE_NAME (type);
9443 tree subtype_name = TYPE_NAME (subtype);
9444
9445 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
61a9389f 9446 type_name = DECL_NAME (type_name);
62351b00 9447
9448 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
61a9389f 9449 subtype_name = DECL_NAME (subtype_name);
62351b00 9450
9451 if (type_name == subtype_name)
61a9389f 9452 return false;
62351b00 9453 }
9454
fd45b48c 9455 return true;
600dbd47 9456}
9457
9458/* Given a pointer to a tree node for a subrange type, return a pointer
9459 to a DIE that describes the given type. */
9460
9461static dw_die_ref
a7011153 9462subrange_type_die (tree type, dw_die_ref context_die)
600dbd47 9463{
600dbd47 9464 dw_die_ref subrange_die;
6114cbf0 9465 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8ec3a57b 9466
a7011153 9467 if (context_die == NULL)
9468 context_die = comp_unit_die;
9469
a7011153 9470 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
a84a50a5 9471
1524656f 9472 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
6114cbf0 9473 {
9474 /* The size of the subrange type and its base type do not match,
61a9389f 9475 so we need to generate a size attribute for the subrange type. */
6114cbf0 9476 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9477 }
9478
600dbd47 9479 if (TYPE_MIN_VALUE (type) != NULL)
9480 add_bound_info (subrange_die, DW_AT_lower_bound,
61a9389f 9481 TYPE_MIN_VALUE (type));
600dbd47 9482 if (TYPE_MAX_VALUE (type) != NULL)
9483 add_bound_info (subrange_die, DW_AT_upper_bound,
61a9389f 9484 TYPE_MAX_VALUE (type));
600dbd47 9485
9486 return subrange_die;
9487}
9488
30ade641 9489/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9490 entry that chains various modifiers in front of the given type. */
ec1e49cc 9491
30ade641 9492static dw_die_ref
8ec3a57b 9493modified_type_die (tree type, int is_const_type, int is_volatile_type,
9494 dw_die_ref context_die)
30ade641 9495{
19cb6b50 9496 enum tree_code code = TREE_CODE (type);
1524656f 9497 dw_die_ref mod_type_die;
19cb6b50 9498 dw_die_ref sub_die = NULL;
9499 tree item_type = NULL;
1524656f 9500 tree qualified_type;
9501 tree name;
9502
9503 if (code == ERROR_MARK)
9504 return NULL;
9505
9506 /* See if we already have the appropriately qualified variant of
9507 this type. */
9508 qualified_type
9509 = get_qualified_type (type,
9510 ((is_const_type ? TYPE_QUAL_CONST : 0)
9511 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
61a9389f 9512
1524656f 9513 /* If we do, then we can just use its DIE, if it exists. */
9514 if (qualified_type)
30ade641 9515 {
1524656f 9516 mod_type_die = lookup_type_die (qualified_type);
6efd403b 9517 if (mod_type_die)
1524656f 9518 return mod_type_die;
9519 }
61a9389f 9520
1524656f 9521 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
61a9389f 9522
1524656f 9523 /* Handle C typedef types. */
9524 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9525 {
9526 tree dtype = TREE_TYPE (name);
61a9389f 9527
1524656f 9528 if (qualified_type == dtype)
30ade641 9529 {
1524656f 9530 /* For a named type, use the typedef. */
9531 gen_type_die (qualified_type, context_die);
9532 return lookup_type_die (qualified_type);
30ade641 9533 }
37f26c2d 9534 else if (is_const_type < TYPE_READONLY (dtype)
9535 || is_volatile_type < TYPE_VOLATILE (dtype)
9536 || (is_const_type <= TYPE_READONLY (dtype)
9537 && is_volatile_type <= TYPE_VOLATILE (dtype)
9538 && DECL_ORIGINAL_TYPE (name) != type))
1524656f 9539 /* cv-unqualified version of named type. Just use the unnamed
9540 type to which it refers. */
9541 return modified_type_die (DECL_ORIGINAL_TYPE (name),
9542 is_const_type, is_volatile_type,
9543 context_die);
9544 /* Else cv-qualified version of named type; fall through. */
9545 }
61a9389f 9546
1524656f 9547 if (is_const_type)
9548 {
9549 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9550 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9551 }
9552 else if (is_volatile_type)
9553 {
9554 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9555 sub_die = modified_type_die (type, 0, 0, context_die);
9556 }
9557 else if (code == POINTER_TYPE)
9558 {
9559 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9560 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9561 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9562 item_type = TREE_TYPE (type);
9563 }
9564 else if (code == REFERENCE_TYPE)
9565 {
9566 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9567 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9568 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9569 item_type = TREE_TYPE (type);
9570 }
9571 else if (is_subrange_type (type))
9572 {
9573 mod_type_die = subrange_type_die (type, context_die);
9574 item_type = TREE_TYPE (type);
9575 }
9576 else if (is_base_type (type))
9577 mod_type_die = base_type_die (type);
9578 else
9579 {
9580 gen_type_die (type, context_die);
61a9389f 9581
1524656f 9582 /* We have to get the type_main_variant here (and pass that to the
9583 `lookup_type_die' routine) because the ..._TYPE node we have
9584 might simply be a *copy* of some original type node (where the
9585 copy was created to help us keep track of typedef names) and
9586 that copy might have a different TYPE_UID from the original
9587 ..._TYPE node. */
9588 if (TREE_CODE (type) != VECTOR_TYPE)
9589 return lookup_type_die (type_main_variant (type));
30ade641 9590 else
1524656f 9591 /* Vectors have the debugging information in the type,
9592 not the main variant. */
9593 return lookup_type_die (type);
9594 }
61a9389f 9595
1524656f 9596 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
9597 don't output a DW_TAG_typedef, since there isn't one in the
9598 user's program; just attach a DW_AT_name to the type. */
9599 if (name
029dbf29 9600 && (TREE_CODE (name) != TYPE_DECL
9601 || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
1524656f 9602 {
9603 if (TREE_CODE (name) == TYPE_DECL)
9604 /* Could just call add_name_and_src_coords_attributes here,
9605 but since this is a builtin type it doesn't have any
9606 useful source coordinates anyway. */
9607 name = DECL_NAME (name);
9608 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
30ade641 9609 }
61a9389f 9610
1524656f 9611 if (qualified_type)
9612 equate_type_number_to_die (qualified_type, mod_type_die);
ec1e49cc 9613
39ee7a4a 9614 if (item_type)
ec1e49cc 9615 /* We must do this after the equate_type_number_to_die call, in case
9616 this is a recursive type. This ensures that the modified_type_die
9617 recursion will terminate even if the type is recursive. Recursive
9618 types are possible in Ada. */
9619 sub_die = modified_type_die (item_type,
9620 TYPE_READONLY (item_type),
9621 TYPE_VOLATILE (item_type),
9622 context_die);
9623
30ade641 9624 if (sub_die != NULL)
ec1e49cc 9625 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9626
30ade641 9627 return mod_type_die;
9628}
9629
30ade641 9630/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
1e625a2e 9631 an enumerated type. */
ec1e49cc 9632
9633static inline int
5493cb9a 9634type_is_enum (const_tree type)
30ade641 9635{
9636 return TREE_CODE (type) == ENUMERAL_TYPE;
9637}
9638
7f3ca0ce 9639/* Return the DBX register number described by a given RTL node. */
4b72e226 9640
9641static unsigned int
5493cb9a 9642dbx_reg_number (const_rtx rtl)
4b72e226 9643{
19cb6b50 9644 unsigned regno = REGNO (rtl);
4b72e226 9645
7bd4f6b6 9646 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
4b72e226 9647
12d886b8 9648#ifdef LEAF_REG_REMAP
203898cb 9649 if (current_function_uses_only_leaf_regs)
9650 {
9651 int leaf_reg = LEAF_REG_REMAP (regno);
9652 if (leaf_reg != -1)
9653 regno = (unsigned) leaf_reg;
9654 }
12d886b8 9655#endif
9656
86e12d28 9657 return DBX_REGISTER_NUMBER (regno);
4b72e226 9658}
9659
fd51758c 9660/* Optionally add a DW_OP_piece term to a location description expression.
9661 DW_OP_piece is only added if the location description expression already
9662 doesn't end with DW_OP_piece. */
9663
9664static void
9665add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9666{
9667 dw_loc_descr_ref loc;
9668
9669 if (*list_head != NULL)
9670 {
9671 /* Find the end of the chain. */
9672 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9673 ;
9674
9675 if (loc->dw_loc_opc != DW_OP_piece)
9676 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9677 }
9678}
9679
86e12d28 9680/* Return a location descriptor that designates a machine register or
9754a2f0 9681 zero if there is none. */
ec1e49cc 9682
30ade641 9683static dw_loc_descr_ref
d53bb226 9684reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
30ade641 9685{
9754a2f0 9686 rtx regs;
ec1e49cc 9687
86e12d28 9688 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9689 return 0;
9690
883b2e73 9691 regs = targetm.dwarf_register_span (rtl);
9754a2f0 9692
12d886b8 9693 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
d53bb226 9694 return multiple_reg_loc_descriptor (rtl, regs, initialized);
9754a2f0 9695 else
d53bb226 9696 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9754a2f0 9697}
9698
9699/* Return a location descriptor that designates a machine register for
9700 a given hard register number. */
9701
9702static dw_loc_descr_ref
d53bb226 9703one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9754a2f0 9704{
dde9bb3b 9705 dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
d53bb226 9706
9707 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9708 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9709
9710 return reg_loc_descr;
9754a2f0 9711}
9712
9713/* Given an RTL of a register, return a location descriptor that
9714 designates a value that spans more than one register. */
9715
9716static dw_loc_descr_ref
39c7766b 9717multiple_reg_loc_descriptor (rtx rtl, rtx regs,
d53bb226 9718 enum var_init_status initialized)
9754a2f0 9719{
9720 int nregs, size, i;
9721 unsigned reg;
9722 dw_loc_descr_ref loc_result = NULL;
ec1e49cc 9723
b6ea71e9 9724 reg = REGNO (rtl);
9725#ifdef LEAF_REG_REMAP
203898cb 9726 if (current_function_uses_only_leaf_regs)
9727 {
9728 int leaf_reg = LEAF_REG_REMAP (reg);
9729 if (leaf_reg != -1)
9730 reg = (unsigned) leaf_reg;
9731 }
b6ea71e9 9732#endif
9733 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
7f3ca0ce 9734 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9754a2f0 9735
9736 /* Simple, contiguous registers. */
9737 if (regs == NULL_RTX)
9738 {
9739 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9740
9741 loc_result = NULL;
9742 while (nregs--)
9743 {
9744 dw_loc_descr_ref t;
9745
d53bb226 9746 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9747 VAR_INIT_STATUS_INITIALIZED);
9754a2f0 9748 add_loc_descr (&loc_result, t);
4719779b 9749 add_loc_descr_op_piece (&loc_result, size);
a4920475 9750 ++reg;
9754a2f0 9751 }
9752 return loc_result;
9753 }
9754
9755 /* Now onto stupid register sets in non contiguous locations. */
9756
7bd4f6b6 9757 gcc_assert (GET_CODE (regs) == PARALLEL);
9754a2f0 9758
9759 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9760 loc_result = NULL;
9761
9762 for (i = 0; i < XVECLEN (regs, 0); ++i)
9763 {
9764 dw_loc_descr_ref t;
9765
d53bb226 9766 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9767 VAR_INIT_STATUS_INITIALIZED);
9754a2f0 9768 add_loc_descr (&loc_result, t);
9769 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
4719779b 9770 add_loc_descr_op_piece (&loc_result, size);
9754a2f0 9771 }
d53bb226 9772
9773 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9774 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
30ade641 9775 return loc_result;
9776}
9777
27a7a23a 9778#endif /* DWARF2_DEBUGGING_INFO */
9779
9780#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9781
9ed904da 9782/* Return a location descriptor that designates a constant. */
9783
9784static dw_loc_descr_ref
8ec3a57b 9785int_loc_descriptor (HOST_WIDE_INT i)
9ed904da 9786{
9787 enum dwarf_location_atom op;
9788
9789 /* Pick the smallest representation of a constant, rather than just
9790 defaulting to the LEB encoding. */
9791 if (i >= 0)
9792 {
9793 if (i <= 31)
8458f4ca 9794 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
9ed904da 9795 else if (i <= 0xff)
9796 op = DW_OP_const1u;
9797 else if (i <= 0xffff)
9798 op = DW_OP_const2u;
9799 else if (HOST_BITS_PER_WIDE_INT == 32
9800 || i <= 0xffffffff)
9801 op = DW_OP_const4u;
9802 else
9803 op = DW_OP_constu;
9804 }
9805 else
9806 {
9807 if (i >= -0x80)
9808 op = DW_OP_const1s;
9809 else if (i >= -0x8000)
9810 op = DW_OP_const2s;
9811 else if (HOST_BITS_PER_WIDE_INT == 32
9812 || i >= -0x80000000)
9813 op = DW_OP_const4s;
9814 else
9815 op = DW_OP_consts;
9816 }
9817
9818 return new_loc_descr (op, i, 0);
9819}
27a7a23a 9820#endif
9821
9822#ifdef DWARF2_DEBUGGING_INFO
9ed904da 9823
30ade641 9824/* Return a location descriptor that designates a base+offset location. */
ec1e49cc 9825
30ade641 9826static dw_loc_descr_ref
d53bb226 9827based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9828 enum var_init_status initialized)
30ade641 9829{
da72c083 9830 unsigned int regno;
d53bb226 9831 dw_loc_descr_ref result;
27a7a23a 9832 dw_fde_ref fde = current_fde ();
12d886b8 9833
9834 /* We only use "frame base" when we're sure we're talking about the
9835 post-prologue local stack frame. We do this by *not* running
9836 register elimination until this point, and recognizing the special
9837 argument pointer and soft frame pointer rtx's. */
9838 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9839 {
da72c083 9840 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12d886b8 9841
da72c083 9842 if (elim != reg)
9843 {
9844 if (GET_CODE (elim) == PLUS)
9845 {
9846 offset += INTVAL (XEXP (elim, 1));
9847 elim = XEXP (elim, 0);
9848 }
27a7a23a 9849 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9850 && (elim == hard_frame_pointer_rtx
9851 || elim == stack_pointer_rtx))
9852 || elim == (frame_pointer_needed
9853 ? hard_frame_pointer_rtx
9854 : stack_pointer_rtx));
9855
9856 /* If drap register is used to align stack, use frame
9857 pointer + offset to access stack variables. If stack
9858 is aligned without drap, use stack pointer + offset to
9859 access stack variables. */
7b70fdf7 9860 if (crtl->stack_realign_tried
27a7a23a 9861 && cfa.reg == HARD_FRAME_POINTER_REGNUM
9862 && reg == frame_pointer_rtx)
9863 {
9864 int base_reg
9865 = DWARF_FRAME_REGNUM (cfa.indirect
9866 ? HARD_FRAME_POINTER_REGNUM
9867 : STACK_POINTER_REGNUM);
dde9bb3b 9868 return new_reg_loc_descr (base_reg, offset);
27a7a23a 9869 }
12d886b8 9870
27a7a23a 9871 offset += frame_pointer_fb_offset;
61a9389f 9872 return new_loc_descr (DW_OP_fbreg, offset, 0);
da72c083 9873 }
12d886b8 9874 }
27a7a23a 9875 else if (fde
9876 && fde->drap_reg != INVALID_REGNUM
9877 && (fde->drap_reg == REGNO (reg)
9878 || fde->vdrap_reg == REGNO (reg)))
9879 {
9880 /* Use cfa+offset to represent the location of arguments passed
9881 on stack when drap is used to align stack. */
9882 return new_loc_descr (DW_OP_fbreg, offset, 0);
9883 }
ec1e49cc 9884
da72c083 9885 regno = dbx_reg_number (reg);
9886 if (regno <= 31)
b9c74b4d 9887 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
9888 offset, 0);
da72c083 9889 else
d53bb226 9890 result = new_loc_descr (DW_OP_bregx, regno, offset);
9891
9892 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9893 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9894
9895 return result;
30ade641 9896}
9897
9898/* Return true if this RTL expression describes a base+offset calculation. */
ec1e49cc 9899
9900static inline int
5493cb9a 9901is_based_loc (const_rtx rtl)
30ade641 9902{
7cc7e163 9903 return (GET_CODE (rtl) == PLUS
8ad4c111 9904 && ((REG_P (XEXP (rtl, 0))
7cc7e163 9905 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9906 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
30ade641 9907}
9908
3ab1d710 9909/* Return a descriptor that describes the concatenation of N locations
9910 used to form the address of a memory location. */
9911
9912static dw_loc_descr_ref
d53bb226 9913concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9914 enum var_init_status initialized)
3ab1d710 9915{
9916 unsigned int i;
9917 dw_loc_descr_ref cc_loc_result = NULL;
9918 unsigned int n = XVECLEN (concatn, 0);
9919
9920 for (i = 0; i < n; ++i)
9921 {
9922 dw_loc_descr_ref ref;
9923 rtx x = XVECEXP (concatn, 0, i);
9924
d53bb226 9925 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
3ab1d710 9926 if (ref == NULL)
9927 return NULL;
9928
9929 add_loc_descr (&cc_loc_result, ref);
9930 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9931 }
9932
d53bb226 9933 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9934 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9935
3ab1d710 9936 return cc_loc_result;
9937}
9938
f7c2629f 9939/* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9940 failed. */
9941
9942static dw_loc_descr_ref
9943tls_mem_loc_descriptor (rtx mem)
9944{
9945 tree base;
1938132c 9946 dw_loc_descr_ref loc_result;
f7c2629f 9947
9948 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9949 return NULL;
9950
9951 base = get_base_address (MEM_EXPR (mem));
9952 if (base == NULL
9953 || TREE_CODE (base) != VAR_DECL
9954 || !DECL_THREAD_LOCAL_P (base))
9955 return NULL;
9956
9957 loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9958 if (loc_result == NULL)
9959 return NULL;
9960
9961 if (INTVAL (MEM_OFFSET (mem)))
1938132c 9962 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
f7c2629f 9963
9964 return loc_result;
9965}
9966
30ade641 9967/* The following routine converts the RTL for a variable or parameter
9968 (resident in memory) into an equivalent Dwarf representation of a
9969 mechanism for getting the address of that same variable onto the top of a
9970 hypothetical "address evaluation" stack.
ec1e49cc 9971
30ade641 9972 When creating memory location descriptors, we are effectively transforming
9973 the RTL for a memory-resident object into its Dwarf postfix expression
9974 equivalent. This routine recursively descends an RTL tree, turning
92a94502 9975 it into Dwarf postfix code as it goes.
9976
9977 MODE is the mode of the memory reference, needed to handle some
86e12d28 9978 autoincrement addressing modes.
9979
12d886b8 9980 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9981 location list for RTL.
b2025850 9982
86e12d28 9983 Return 0 if we can't represent the location. */
ec1e49cc 9984
30ade641 9985static dw_loc_descr_ref
d53bb226 9986mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9987 enum var_init_status initialized)
30ade641 9988{
9989 dw_loc_descr_ref mem_loc_result = NULL;
3122a117 9990 enum dwarf_location_atom op;
86e12d28 9991
f80d1bcd 9992 /* Note that for a dynamically sized array, the location we will generate a
30ade641 9993 description of here will be the lowest numbered location which is
9994 actually within the array. That's *not* necessarily the same as the
9995 zeroth element of the array. */
ec1e49cc 9996
883b2e73 9997 rtl = targetm.delegitimize_address (rtl);
eacbfaac 9998
30ade641 9999 switch (GET_CODE (rtl))
10000 {
92a94502 10001 case POST_INC:
10002 case POST_DEC:
93fbe1f3 10003 case POST_MODIFY:
92a94502 10004 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
10005 just fall into the SUBREG code. */
10006
8c3f468d 10007 /* ... fall through ... */
92a94502 10008
30ade641 10009 case SUBREG:
10010 /* The case of a subreg may arise when we have a local (register)
c83a163c 10011 variable or a formal (register) parameter which doesn't quite fill
10012 up an entire register. For now, just assume that it is
10013 legitimate to make the Dwarf info refer to the whole register which
10014 contains the given subreg. */
822e27f9 10015 rtl = XEXP (rtl, 0);
ec1e49cc 10016
8c3f468d 10017 /* ... fall through ... */
30ade641 10018
10019 case REG:
10020 /* Whenever a register number forms a part of the description of the
c83a163c 10021 method for calculating the (dynamic) address of a memory resident
10022 object, DWARF rules require the register number be referred to as
10023 a "base register". This distinction is not based in any way upon
10024 what category of register the hardware believes the given register
10025 belongs to. This is strictly DWARF terminology we're dealing with
10026 here. Note that in cases where the location of a memory-resident
10027 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10028 OP_CONST (0)) the actual DWARF location descriptor that we generate
10029 may just be OP_BASEREG (basereg). This may look deceptively like
10030 the object in question was allocated to a register (rather than in
10031 memory) so DWARF consumers need to be aware of the subtle
10032 distinction between OP_REG and OP_BASEREG. */
86e12d28 10033 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
d53bb226 10034 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
5a821301 10035 else if (stack_realign_drap
10036 && crtl->drap_reg
10037 && crtl->args.internal_arg_pointer == rtl
10038 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10039 {
10040 /* If RTL is internal_arg_pointer, which has been optimized
10041 out, use DRAP instead. */
10042 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10043 VAR_INIT_STATUS_INITIALIZED);
10044 }
30ade641 10045 break;
10046
10047 case MEM:
d53bb226 10048 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10049 VAR_INIT_STATUS_INITIALIZED);
f7c2629f 10050 if (mem_loc_result == NULL)
10051 mem_loc_result = tls_mem_loc_descriptor (rtl);
86e12d28 10052 if (mem_loc_result != 0)
10053 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
30ade641 10054 break;
10055
095ec610 10056 case LO_SUM:
10057 rtl = XEXP (rtl, 1);
10058
10059 /* ... fall through ... */
10060
9ed904da 10061 case LABEL_REF:
10062 /* Some ports can transform a symbol ref into a label ref, because
8ec3a57b 10063 the symbol ref is too far away and has to be dumped into a constant
10064 pool. */
30ade641 10065 case CONST:
10066 case SYMBOL_REF:
7012770b 10067 /* Alternatively, the symbol in the constant pool might be referenced
efdf6c61 10068 by a different symbol. */
8c3f468d 10069 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
dfc1ac47 10070 {
7ad1c520 10071 bool marked;
10072 rtx tmp = get_pool_constant_mark (rtl, &marked);
8c3f468d 10073
7012770b 10074 if (GET_CODE (tmp) == SYMBOL_REF)
7ad1c520 10075 {
10076 rtl = tmp;
10077 if (CONSTANT_POOL_ADDRESS_P (tmp))
10078 get_pool_constant_mark (tmp, &marked);
10079 else
10080 marked = true;
10081 }
10082
10083 /* If all references to this pool constant were optimized away,
10084 it was not output and thus we can't represent it.
10085 FIXME: might try to use DW_OP_const_value here, though
10086 DW_OP_piece complicates it. */
10087 if (!marked)
10088 return 0;
dfc1ac47 10089 }
10090
30ade641 10091 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10092 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
7facaa35 10093 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
62aedc4c 10094 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
30ade641 10095 break;
10096
93fbe1f3 10097 case PRE_MODIFY:
10098 /* Extract the PLUS expression nested inside and fall into
c83a163c 10099 PLUS code below. */
93fbe1f3 10100 rtl = XEXP (rtl, 1);
10101 goto plus;
10102
92a94502 10103 case PRE_INC:
10104 case PRE_DEC:
10105 /* Turn these into a PLUS expression and fall into the PLUS code
10106 below. */
10107 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10108 GEN_INT (GET_CODE (rtl) == PRE_INC
f80d1bcd 10109 ? GET_MODE_UNIT_SIZE (mode)
10110 : -GET_MODE_UNIT_SIZE (mode)));
10111
8c3f468d 10112 /* ... fall through ... */
92a94502 10113
30ade641 10114 case PLUS:
93fbe1f3 10115 plus:
30ade641 10116 if (is_based_loc (rtl))
12d886b8 10117 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
d53bb226 10118 INTVAL (XEXP (rtl, 1)),
10119 VAR_INIT_STATUS_INITIALIZED);
30ade641 10120 else
10121 {
d53bb226 10122 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10123 VAR_INIT_STATUS_INITIALIZED);
86e12d28 10124 if (mem_loc_result == 0)
10125 break;
9ed904da 10126
1938132c 10127 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT)
10128 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
9ed904da 10129 else
10130 {
f7c2629f 10131 dw_loc_descr_ref mem_loc_result2
10132 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10133 VAR_INIT_STATUS_INITIALIZED);
10134 if (mem_loc_result2 == 0)
10135 break;
10136 add_loc_descr (&mem_loc_result, mem_loc_result2);
9ed904da 10137 add_loc_descr (&mem_loc_result,
10138 new_loc_descr (DW_OP_plus, 0, 0));
10139 }
30ade641 10140 }
10141 break;
10142
3122a117 10143 /* If a pseudo-reg is optimized away, it is possible for it to
10144 be replaced with a MEM containing a multiply or shift. */
a10de18c 10145 case MULT:
3122a117 10146 op = DW_OP_mul;
10147 goto do_binop;
10148
10149 case ASHIFT:
10150 op = DW_OP_shl;
10151 goto do_binop;
8ff30ff6 10152
3122a117 10153 case ASHIFTRT:
10154 op = DW_OP_shra;
10155 goto do_binop;
10156
10157 case LSHIFTRT:
10158 op = DW_OP_shr;
10159 goto do_binop;
10160
10161 do_binop:
86e12d28 10162 {
d53bb226 10163 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10164 VAR_INIT_STATUS_INITIALIZED);
10165 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10166 VAR_INIT_STATUS_INITIALIZED);
86e12d28 10167
10168 if (op0 == 0 || op1 == 0)
10169 break;
10170
10171 mem_loc_result = op0;
10172 add_loc_descr (&mem_loc_result, op1);
3122a117 10173 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
86e12d28 10174 break;
10175 }
a10de18c 10176
30ade641 10177 case CONST_INT:
9ed904da 10178 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
30ade641 10179 break;
10180
3ab1d710 10181 case CONCATN:
39c7766b 10182 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
d53bb226 10183 VAR_INIT_STATUS_INITIALIZED);
3ab1d710 10184 break;
10185
f7c2629f 10186 case UNSPEC:
10187 /* If delegitimize_address couldn't do anything with the UNSPEC, we
10188 can't express it in the debug info. This can happen e.g. with some
10189 TLS UNSPECs. */
10190 break;
10191
30ade641 10192 default:
7bd4f6b6 10193 gcc_unreachable ();
30ade641 10194 }
ec1e49cc 10195
d53bb226 10196 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10197 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10198
30ade641 10199 return mem_loc_result;
10200}
10201
ad87de1e 10202/* Return a descriptor that describes the concatenation of two locations.
fe829d4e 10203 This is typically a complex variable. */
10204
10205static dw_loc_descr_ref
d53bb226 10206concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
fe829d4e 10207{
10208 dw_loc_descr_ref cc_loc_result = NULL;
d53bb226 10209 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10210 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
fe829d4e 10211
86e12d28 10212 if (x0_ref == 0 || x1_ref == 0)
10213 return 0;
10214
10215 cc_loc_result = x0_ref;
4719779b 10216 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
fe829d4e 10217
86e12d28 10218 add_loc_descr (&cc_loc_result, x1_ref);
4719779b 10219 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
fe829d4e 10220
d53bb226 10221 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10222 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10223
fe829d4e 10224 return cc_loc_result;
10225}
10226
1a6a0f2a 10227/* Return a descriptor that describes the concatenation of N
10228 locations. */
10229
10230static dw_loc_descr_ref
d53bb226 10231concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
1a6a0f2a 10232{
10233 unsigned int i;
10234 dw_loc_descr_ref cc_loc_result = NULL;
10235 unsigned int n = XVECLEN (concatn, 0);
10236
10237 for (i = 0; i < n; ++i)
10238 {
10239 dw_loc_descr_ref ref;
10240 rtx x = XVECEXP (concatn, 0, i);
10241
d53bb226 10242 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
1a6a0f2a 10243 if (ref == NULL)
10244 return NULL;
10245
10246 add_loc_descr (&cc_loc_result, ref);
10247 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10248 }
10249
d53bb226 10250 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10251 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10252
1a6a0f2a 10253 return cc_loc_result;
10254}
10255
30ade641 10256/* Output a proper Dwarf location descriptor for a variable or parameter
10257 which is either allocated in a register or in a memory location. For a
10258 register, we just generate an OP_REG and the register number. For a
10259 memory location we provide a Dwarf postfix expression describing how to
86e12d28 10260 generate the (dynamic) address of the object onto the address stack.
10261
10262 If we don't know how to describe it, return 0. */
ec1e49cc 10263
30ade641 10264static dw_loc_descr_ref
d53bb226 10265loc_descriptor (rtx rtl, enum var_init_status initialized)
30ade641 10266{
10267 dw_loc_descr_ref loc_result = NULL;
86e12d28 10268
30ade641 10269 switch (GET_CODE (rtl))
10270 {
10271 case SUBREG:
30ade641 10272 /* The case of a subreg may arise when we have a local (register)
c83a163c 10273 variable or a formal (register) parameter which doesn't quite fill
10274 up an entire register. For now, just assume that it is
10275 legitimate to make the Dwarf info refer to the whole register which
10276 contains the given subreg. */
701e46d0 10277 rtl = SUBREG_REG (rtl);
ec1e49cc 10278
8c3f468d 10279 /* ... fall through ... */
30ade641 10280
10281 case REG:
d53bb226 10282 loc_result = reg_loc_descriptor (rtl, initialized);
30ade641 10283 break;
10284
10285 case MEM:
d53bb226 10286 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10287 initialized);
f7c2629f 10288 if (loc_result == NULL)
10289 loc_result = tls_mem_loc_descriptor (rtl);
30ade641 10290 break;
10291
fe829d4e 10292 case CONCAT:
d53bb226 10293 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10294 initialized);
fe829d4e 10295 break;
10296
1a6a0f2a 10297 case CONCATN:
d53bb226 10298 loc_result = concatn_loc_descriptor (rtl, initialized);
1a6a0f2a 10299 break;
10300
b2025850 10301 case VAR_LOCATION:
10302 /* Single part. */
10303 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10304 {
d53bb226 10305 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
afcf285e 10306 break;
b2025850 10307 }
b2025850 10308
afcf285e 10309 rtl = XEXP (rtl, 1);
10310 /* FALLTHRU */
b2025850 10311
afcf285e 10312 case PARALLEL:
10313 {
10314 rtvec par_elems = XVEC (rtl, 0);
10315 int num_elem = GET_NUM_ELEM (par_elems);
10316 enum machine_mode mode;
10317 int i;
10318
10319 /* Create the first one, so we have something to add to. */
d53bb226 10320 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10321 initialized);
f7c2629f 10322 if (loc_result == NULL)
10323 return NULL;
afcf285e 10324 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
4719779b 10325 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
afcf285e 10326 for (i = 1; i < num_elem; i++)
10327 {
10328 dw_loc_descr_ref temp;
10329
d53bb226 10330 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10331 initialized);
f7c2629f 10332 if (temp == NULL)
10333 return NULL;
afcf285e 10334 add_loc_descr (&loc_result, temp);
10335 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
4719779b 10336 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
afcf285e 10337 }
10338 }
b2025850 10339 break;
10340
30ade641 10341 default:
7bd4f6b6 10342 gcc_unreachable ();
30ade641 10343 }
ec1e49cc 10344
30ade641 10345 return loc_result;
10346}
10347
8c3f468d 10348/* Similar, but generate the descriptor from trees instead of rtl. This comes
afcf285e 10349 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
10350 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10351 top-level invocation, and we require the address of LOC; is 0 if we require
10352 the value of LOC. */
9ed904da 10353
10354static dw_loc_descr_ref
afcf285e 10355loc_descriptor_from_tree_1 (tree loc, int want_address)
9ed904da 10356{
86e12d28 10357 dw_loc_descr_ref ret, ret1;
afcf285e 10358 int have_address = 0;
9ed904da 10359 enum dwarf_location_atom op;
10360
10361 /* ??? Most of the time we do not take proper care for sign/zero
10362 extending the values properly. Hopefully this won't be a real
10363 problem... */
10364
10365 switch (TREE_CODE (loc))
10366 {
10367 case ERROR_MARK:
86e12d28 10368 return 0;
9ed904da 10369
86e12d28 10370 case PLACEHOLDER_EXPR:
a3915b32 10371 /* This case involves extracting fields from an object to determine the
10372 position of other fields. We don't try to encode this here. The
10373 only user of this is Ada, which encodes the needed information using
10374 the names of types. */
86e12d28 10375 return 0;
a3915b32 10376
dff29840 10377 case CALL_EXPR:
10378 return 0;
10379
7ddf4456 10380 case PREINCREMENT_EXPR:
10381 case PREDECREMENT_EXPR:
10382 case POSTINCREMENT_EXPR:
10383 case POSTDECREMENT_EXPR:
10384 /* There are no opcodes for these operations. */
10385 return 0;
10386
dff29840 10387 case ADDR_EXPR:
afcf285e 10388 /* If we already want an address, there's nothing we can do. */
10389 if (want_address)
10390 return 0;
dff29840 10391
afcf285e 10392 /* Otherwise, process the argument and look for the address. */
10393 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
dff29840 10394
9ed904da 10395 case VAR_DECL:
1b53eb20 10396 if (DECL_THREAD_LOCAL_P (loc))
931e9893 10397 {
10398 rtx rtl;
b9c74b4d 10399 enum dwarf_location_atom first_op;
10400 enum dwarf_location_atom second_op;
931e9893 10401
38475469 10402 if (targetm.have_tls)
10403 {
10404 /* If this is not defined, we have no way to emit the
10405 data. */
10406 if (!targetm.asm_out.output_dwarf_dtprel)
10407 return 0;
10408
10409 /* The way DW_OP_GNU_push_tls_address is specified, we
10410 can only look up addresses of objects in the current
10411 module. */
f7c2629f 10412 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
38475469 10413 return 0;
b9c74b4d 10414 first_op = (enum dwarf_location_atom) INTERNAL_DW_OP_tls_addr;
38475469 10415 second_op = DW_OP_GNU_push_tls_address;
10416 }
10417 else
10418 {
10419 if (!targetm.emutls.debug_form_tls_address)
10420 return 0;
10421 loc = emutls_decl (loc);
10422 first_op = DW_OP_addr;
10423 second_op = DW_OP_form_tls_address;
10424 }
39c7766b 10425
931e9893 10426 rtl = rtl_for_decl_location (loc);
10427 if (rtl == NULL_RTX)
10428 return 0;
10429
e16ceb8e 10430 if (!MEM_P (rtl))
931e9893 10431 return 0;
10432 rtl = XEXP (rtl, 0);
10433 if (! CONSTANT_P (rtl))
10434 return 0;
10435
38475469 10436 ret = new_loc_descr (first_op, 0, 0);
931e9893 10437 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10438 ret->dw_loc_oprnd1.v.val_addr = rtl;
39c7766b 10439
38475469 10440 ret1 = new_loc_descr (second_op, 0, 0);
931e9893 10441 add_loc_descr (&ret, ret1);
10442
afcf285e 10443 have_address = 1;
931e9893 10444 break;
10445 }
afcf285e 10446 /* FALLTHRU */
931e9893 10447
9ed904da 10448 case PARM_DECL:
75fa4f82 10449 if (DECL_HAS_VALUE_EXPR_P (loc))
10450 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10451 want_address);
afcf285e 10452 /* FALLTHRU */
10453
4ee9c684 10454 case RESULT_DECL:
cfdab332 10455 case FUNCTION_DECL:
9ed904da 10456 {
10457 rtx rtl = rtl_for_decl_location (loc);
9ed904da 10458
0ff98c8f 10459 if (rtl == NULL_RTX)
86e12d28 10460 return 0;
61a9389f 10461 else if (GET_CODE (rtl) == CONST_INT)
afcf285e 10462 {
10463 HOST_WIDE_INT val = INTVAL (rtl);
10464 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10465 val &= GET_MODE_MASK (DECL_MODE (loc));
10466 ret = int_loc_descriptor (val);
10467 }
10468 else if (GET_CODE (rtl) == CONST_STRING)
10469 return 0;
0ff98c8f 10470 else if (CONSTANT_P (rtl))
9ed904da 10471 {
10472 ret = new_loc_descr (DW_OP_addr, 0, 0);
10473 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10474 ret->dw_loc_oprnd1.v.val_addr = rtl;
9ed904da 10475 }
10476 else
10477 {
afcf285e 10478 enum machine_mode mode;
10479
10480 /* Certain constructs can only be represented at top-level. */
10481 if (want_address == 2)
d53bb226 10482 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
f3546830 10483
afcf285e 10484 mode = GET_MODE (rtl);
e16ceb8e 10485 if (MEM_P (rtl))
9ed904da 10486 {
9ed904da 10487 rtl = XEXP (rtl, 0);
afcf285e 10488 have_address = 1;
9ed904da 10489 }
d53bb226 10490 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9ed904da 10491 }
10492 }
10493 break;
10494
10495 case INDIRECT_REF:
afcf285e 10496 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10497 have_address = 1;
9ed904da 10498 break;
10499
3de30178 10500 case COMPOUND_EXPR:
afcf285e 10501 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
3de30178 10502
72dd6141 10503 CASE_CONVERT:
f96c43fb 10504 case VIEW_CONVERT_EXPR:
a3915b32 10505 case SAVE_EXPR:
75a70cf9 10506 case MODIFY_EXPR:
10507 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
f9038ab4 10508
9ed904da 10509 case COMPONENT_REF:
10510 case BIT_FIELD_REF:
10511 case ARRAY_REF:
ba04d9d5 10512 case ARRAY_RANGE_REF:
9ed904da 10513 {
10514 tree obj, offset;
10515 HOST_WIDE_INT bitsize, bitpos, bytepos;
10516 enum machine_mode mode;
10517 int volatilep;
1e8e9920 10518 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9ed904da 10519
10520 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
e7e9416e 10521 &unsignedp, &volatilep, false);
86e12d28 10522
10523 if (obj == loc)
10524 return 0;
10525
afcf285e 10526 ret = loc_descriptor_from_tree_1 (obj, 1);
86e12d28 10527 if (ret == 0
8c3f468d 10528 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
86e12d28 10529 return 0;
9ed904da 10530
10531 if (offset != NULL_TREE)
10532 {
10533 /* Variable offset. */
f7c2629f 10534 ret1 = loc_descriptor_from_tree_1 (offset, 0);
10535 if (ret1 == 0)
10536 return 0;
10537 add_loc_descr (&ret, ret1);
9ed904da 10538 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10539 }
10540
9ed904da 10541 bytepos = bitpos / BITS_PER_UNIT;
1938132c 10542 loc_descr_plus_const (&ret, bytepos);
afcf285e 10543
10544 have_address = 1;
9ed904da 10545 break;
10546 }
10547
10548 case INTEGER_CST:
10549 if (host_integerp (loc, 0))
10550 ret = int_loc_descriptor (tree_low_cst (loc, 0));
86e12d28 10551 else
10552 return 0;
9ed904da 10553 break;
9ed904da 10554
15b7bb11 10555 case CONSTRUCTOR:
10556 {
6e326506 10557 /* Get an RTL for this, if something has been emitted. */
10558 rtx rtl = lookup_constant_def (loc);
10559 enum machine_mode mode;
10560
afcf285e 10561 if (!rtl || !MEM_P (rtl))
6e326506 10562 return 0;
10563 mode = GET_MODE (rtl);
10564 rtl = XEXP (rtl, 0);
d53bb226 10565 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
afcf285e 10566 have_address = 1;
15b7bb11 10567 break;
10568 }
10569
bc70bd5e 10570 case TRUTH_AND_EXPR:
cfd66c04 10571 case TRUTH_ANDIF_EXPR:
9ed904da 10572 case BIT_AND_EXPR:
10573 op = DW_OP_and;
10574 goto do_binop;
86e12d28 10575
cfd66c04 10576 case TRUTH_XOR_EXPR:
9ed904da 10577 case BIT_XOR_EXPR:
10578 op = DW_OP_xor;
10579 goto do_binop;
86e12d28 10580
cfd66c04 10581 case TRUTH_OR_EXPR:
10582 case TRUTH_ORIF_EXPR:
9ed904da 10583 case BIT_IOR_EXPR:
10584 op = DW_OP_or;
10585 goto do_binop;
86e12d28 10586
d7f71e5a 10587 case FLOOR_DIV_EXPR:
10588 case CEIL_DIV_EXPR:
10589 case ROUND_DIV_EXPR:
9ed904da 10590 case TRUNC_DIV_EXPR:
10591 op = DW_OP_div;
10592 goto do_binop;
86e12d28 10593
9ed904da 10594 case MINUS_EXPR:
10595 op = DW_OP_minus;
10596 goto do_binop;
86e12d28 10597
d7f71e5a 10598 case FLOOR_MOD_EXPR:
10599 case CEIL_MOD_EXPR:
10600 case ROUND_MOD_EXPR:
9ed904da 10601 case TRUNC_MOD_EXPR:
10602 op = DW_OP_mod;
10603 goto do_binop;
86e12d28 10604
9ed904da 10605 case MULT_EXPR:
10606 op = DW_OP_mul;
10607 goto do_binop;
86e12d28 10608
9ed904da 10609 case LSHIFT_EXPR:
10610 op = DW_OP_shl;
10611 goto do_binop;
86e12d28 10612
9ed904da 10613 case RSHIFT_EXPR:
1e8e9920 10614 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9ed904da 10615 goto do_binop;
86e12d28 10616
0de36bdb 10617 case POINTER_PLUS_EXPR:
9ed904da 10618 case PLUS_EXPR:
10619 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10620 && host_integerp (TREE_OPERAND (loc, 1), 0))
10621 {
afcf285e 10622 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
86e12d28 10623 if (ret == 0)
10624 return 0;
10625
1938132c 10626 loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
9ed904da 10627 break;
10628 }
86e12d28 10629
9ed904da 10630 op = DW_OP_plus;
10631 goto do_binop;
8c3f468d 10632
9ed904da 10633 case LE_EXPR:
78a8ed03 10634 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
86e12d28 10635 return 0;
10636
9ed904da 10637 op = DW_OP_le;
10638 goto do_binop;
86e12d28 10639
9ed904da 10640 case GE_EXPR:
78a8ed03 10641 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
86e12d28 10642 return 0;
10643
9ed904da 10644 op = DW_OP_ge;
10645 goto do_binop;
86e12d28 10646
9ed904da 10647 case LT_EXPR:
78a8ed03 10648 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
86e12d28 10649 return 0;
10650
9ed904da 10651 op = DW_OP_lt;
10652 goto do_binop;
86e12d28 10653
9ed904da 10654 case GT_EXPR:
78a8ed03 10655 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
86e12d28 10656 return 0;
10657
9ed904da 10658 op = DW_OP_gt;
10659 goto do_binop;
86e12d28 10660
9ed904da 10661 case EQ_EXPR:
10662 op = DW_OP_eq;
10663 goto do_binop;
86e12d28 10664
9ed904da 10665 case NE_EXPR:
10666 op = DW_OP_ne;
10667 goto do_binop;
10668
10669 do_binop:
afcf285e 10670 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10671 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
86e12d28 10672 if (ret == 0 || ret1 == 0)
10673 return 0;
10674
10675 add_loc_descr (&ret, ret1);
9ed904da 10676 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10677 break;
10678
cfd66c04 10679 case TRUTH_NOT_EXPR:
9ed904da 10680 case BIT_NOT_EXPR:
10681 op = DW_OP_not;
10682 goto do_unop;
86e12d28 10683
9ed904da 10684 case ABS_EXPR:
10685 op = DW_OP_abs;
10686 goto do_unop;
86e12d28 10687
9ed904da 10688 case NEGATE_EXPR:
10689 op = DW_OP_neg;
10690 goto do_unop;
10691
10692 do_unop:
afcf285e 10693 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
86e12d28 10694 if (ret == 0)
10695 return 0;
10696
9ed904da 10697 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10698 break;
10699
93823dba 10700 case MIN_EXPR:
9ed904da 10701 case MAX_EXPR:
93823dba 10702 {
61a9389f 10703 const enum tree_code code =
10704 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
93823dba 10705
61a9389f 10706 loc = build3 (COND_EXPR, TREE_TYPE (loc),
b55f9493 10707 build2 (code, integer_type_node,
10708 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
61a9389f 10709 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
93823dba 10710 }
8c3f468d 10711
04641143 10712 /* ... fall through ... */
9ed904da 10713
10714 case COND_EXPR:
10715 {
86e12d28 10716 dw_loc_descr_ref lhs
afcf285e 10717 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
86e12d28 10718 dw_loc_descr_ref rhs
afcf285e 10719 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9ed904da 10720 dw_loc_descr_ref bra_node, jump_node, tmp;
10721
afcf285e 10722 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
86e12d28 10723 if (ret == 0 || lhs == 0 || rhs == 0)
10724 return 0;
10725
9ed904da 10726 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10727 add_loc_descr (&ret, bra_node);
10728
86e12d28 10729 add_loc_descr (&ret, rhs);
9ed904da 10730 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10731 add_loc_descr (&ret, jump_node);
10732
86e12d28 10733 add_loc_descr (&ret, lhs);
9ed904da 10734 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
86e12d28 10735 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9ed904da 10736
10737 /* ??? Need a node to point the skip at. Use a nop. */
10738 tmp = new_loc_descr (DW_OP_nop, 0, 0);
10739 add_loc_descr (&ret, tmp);
10740 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10741 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10742 }
10743 break;
10744
c98c6570 10745 case FIX_TRUNC_EXPR:
c98c6570 10746 return 0;
10747
9ed904da 10748 default:
76d1c62d 10749 /* Leave front-end specific codes as simply unknown. This comes
10750 up, for instance, with the C STMT_EXPR. */
10751 if ((unsigned int) TREE_CODE (loc)
61a9389f 10752 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
76d1c62d 10753 return 0;
10754
13346250 10755#ifdef ENABLE_CHECKING
76d1c62d 10756 /* Otherwise this is a generic code; we should just lists all of
89f18f73 10757 these explicitly. We forgot one. */
7bd4f6b6 10758 gcc_unreachable ();
13346250 10759#else
10760 /* In a release build, we want to degrade gracefully: better to
10761 generate incomplete debugging information than to crash. */
10762 return NULL;
10763#endif
9ed904da 10764 }
10765
86e12d28 10766 /* Show if we can't fill the request for an address. */
afcf285e 10767 if (want_address && !have_address)
86e12d28 10768 return 0;
9ed904da 10769
10770 /* If we've got an address and don't want one, dereference. */
9338678e 10771 if (!want_address && have_address && ret)
9ed904da 10772 {
86e12d28 10773 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10774
10775 if (size > DWARF2_ADDR_SIZE || size == -1)
10776 return 0;
8c3f468d 10777 else if (size == DWARF2_ADDR_SIZE)
9ed904da 10778 op = DW_OP_deref;
10779 else
10780 op = DW_OP_deref_size;
86e12d28 10781
10782 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9ed904da 10783 }
10784
10785 return ret;
10786}
10787
afcf285e 10788static inline dw_loc_descr_ref
10789loc_descriptor_from_tree (tree loc)
10790{
10791 return loc_descriptor_from_tree_1 (loc, 2);
10792}
10793
5d844ba2 10794/* Given a value, round it up to the lowest multiple of `boundary'
30ade641 10795 which is not less than the value itself. */
ec1e49cc 10796
5d844ba2 10797static inline HOST_WIDE_INT
8ec3a57b 10798ceiling (HOST_WIDE_INT value, unsigned int boundary)
30ade641 10799{
10800 return (((value + boundary - 1) / boundary) * boundary);
10801}
10802
10803/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10804 pointer to the declared type for the relevant field variable, or return
10805 `integer_type_node' if the given node turns out to be an
10806 ERROR_MARK node. */
ec1e49cc 10807
10808static inline tree
5493cb9a 10809field_type (const_tree decl)
30ade641 10810{
19cb6b50 10811 tree type;
30ade641 10812
10813 if (TREE_CODE (decl) == ERROR_MARK)
10814 return integer_type_node;
10815
10816 type = DECL_BIT_FIELD_TYPE (decl);
ec1e49cc 10817 if (type == NULL_TREE)
30ade641 10818 type = TREE_TYPE (decl);
10819
10820 return type;
10821}
10822
2180a0af 10823/* Given a pointer to a tree node, return the alignment in bits for
10824 it, or else return BITS_PER_WORD if the node actually turns out to
10825 be an ERROR_MARK node. */
ec1e49cc 10826
10827static inline unsigned
5493cb9a 10828simple_type_align_in_bits (const_tree type)
30ade641 10829{
10830 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10831}
10832
2180a0af 10833static inline unsigned
5493cb9a 10834simple_decl_align_in_bits (const_tree decl)
2180a0af 10835{
10836 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10837}
10838
71b5358c 10839/* Return the result of rounding T up to ALIGN. */
10840
10841static inline HOST_WIDE_INT
10842round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10843{
10844 /* We must be careful if T is negative because HOST_WIDE_INT can be
10845 either "above" or "below" unsigned int as per the C promotion
10846 rules, depending on the host, thus making the signedness of the
10847 direct multiplication and division unpredictable. */
10848 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10849
10850 u += align - 1;
10851 u /= align;
10852 u *= align;
10853
10854 return (HOST_WIDE_INT) u;
10855}
10856
8c3f468d 10857/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10858 lowest addressed byte of the "containing object" for the given FIELD_DECL,
10859 or return 0 if we are unable to determine what that offset is, either
10860 because the argument turns out to be a pointer to an ERROR_MARK node, or
10861 because the offset is actually variable. (We can't handle the latter case
10862 just yet). */
ec1e49cc 10863
5d844ba2 10864static HOST_WIDE_INT
5493cb9a 10865field_byte_offset (const_tree decl)
30ade641 10866{
5d844ba2 10867 HOST_WIDE_INT object_offset_in_bits;
5d844ba2 10868 HOST_WIDE_INT bitpos_int;
30ade641 10869
10870 if (TREE_CODE (decl) == ERROR_MARK)
10871 return 0;
8ff30ff6 10872
7bd4f6b6 10873 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
30ade641 10874
f80d1bcd 10875 /* We cannot yet cope with fields whose positions are variable, so
30ade641 10876 for now, when we see such things, we simply return 0. Someday, we may
10877 be able to handle such cases, but it will be damn difficult. */
5d844ba2 10878 if (! host_integerp (bit_position (decl), 0))
30ade641 10879 return 0;
155b05dc 10880
5d844ba2 10881 bitpos_int = int_bit_position (decl);
30ade641 10882
feb5f1b1 10883#ifdef PCC_BITFIELD_TYPE_MATTERS
10884 if (PCC_BITFIELD_TYPE_MATTERS)
10885 {
10886 tree type;
10887 tree field_size_tree;
10888 HOST_WIDE_INT deepest_bitpos;
10889 unsigned HOST_WIDE_INT field_size_in_bits;
10890 unsigned int type_align_in_bits;
10891 unsigned int decl_align_in_bits;
10892 unsigned HOST_WIDE_INT type_size_in_bits;
30ade641 10893
feb5f1b1 10894 type = field_type (decl);
0bae362c 10895 type_size_in_bits = simple_type_size_in_bits (type);
10896 type_align_in_bits = simple_type_align_in_bits (type);
10897
feb5f1b1 10898 field_size_tree = DECL_SIZE (decl);
30ade641 10899
feb5f1b1 10900 /* The size could be unspecified if there was an error, or for
10901 a flexible array member. */
0bae362c 10902 if (!field_size_tree)
feb5f1b1 10903 field_size_tree = bitsize_zero_node;
10904
0bae362c 10905 /* If the size of the field is not constant, use the type size. */
feb5f1b1 10906 if (host_integerp (field_size_tree, 1))
10907 field_size_in_bits = tree_low_cst (field_size_tree, 1);
10908 else
0bae362c 10909 field_size_in_bits = type_size_in_bits;
feb5f1b1 10910
feb5f1b1 10911 decl_align_in_bits = simple_decl_align_in_bits (decl);
10912
10913 /* The GCC front-end doesn't make any attempt to keep track of the
10914 starting bit offset (relative to the start of the containing
10915 structure type) of the hypothetical "containing object" for a
10916 bit-field. Thus, when computing the byte offset value for the
10917 start of the "containing object" of a bit-field, we must deduce
10918 this information on our own. This can be rather tricky to do in
10919 some cases. For example, handling the following structure type
10920 definition when compiling for an i386/i486 target (which only
10921 aligns long long's to 32-bit boundaries) can be very tricky:
30ade641 10922
10923 struct S { int field1; long long field2:31; };
10924
feb5f1b1 10925 Fortunately, there is a simple rule-of-thumb which can be used
10926 in such cases. When compiling for an i386/i486, GCC will
10927 allocate 8 bytes for the structure shown above. It decides to
10928 do this based upon one simple rule for bit-field allocation.
10929 GCC allocates each "containing object" for each bit-field at
10930 the first (i.e. lowest addressed) legitimate alignment boundary
10931 (based upon the required minimum alignment for the declared
10932 type of the field) which it can possibly use, subject to the
10933 condition that there is still enough available space remaining
10934 in the containing object (when allocated at the selected point)
10935 to fully accommodate all of the bits of the bit-field itself.
10936
10937 This simple rule makes it obvious why GCC allocates 8 bytes for
10938 each object of the structure type shown above. When looking
10939 for a place to allocate the "containing object" for `field2',
10940 the compiler simply tries to allocate a 64-bit "containing
10941 object" at each successive 32-bit boundary (starting at zero)
10942 until it finds a place to allocate that 64- bit field such that
10943 at least 31 contiguous (and previously unallocated) bits remain
10944 within that selected 64 bit field. (As it turns out, for the
10945 example above, the compiler finds it is OK to allocate the
10946 "containing object" 64-bit field at bit-offset zero within the
10947 structure type.)
10948
10949 Here we attempt to work backwards from the limited set of facts
10950 we're given, and we try to deduce from those facts, where GCC
10951 must have believed that the containing object started (within
10952 the structure type). The value we deduce is then used (by the
10953 callers of this routine) to generate DW_AT_location and
10954 DW_AT_bit_offset attributes for fields (both bit-fields and, in
10955 the case of DW_AT_location, regular fields as well). */
10956
10957 /* Figure out the bit-distance from the start of the structure to
10958 the "deepest" bit of the bit-field. */
10959 deepest_bitpos = bitpos_int + field_size_in_bits;
10960
10961 /* This is the tricky part. Use some fancy footwork to deduce
10962 where the lowest addressed bit of the containing object must
10963 be. */
2180a0af 10964 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10965
feb5f1b1 10966 /* Round up to type_align by default. This works best for
10967 bitfields. */
71b5358c 10968 object_offset_in_bits
feb5f1b1 10969 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10970
10971 if (object_offset_in_bits > bitpos_int)
10972 {
10973 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10974
10975 /* Round up to decl_align instead. */
10976 object_offset_in_bits
10977 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10978 }
2180a0af 10979 }
feb5f1b1 10980 else
10981#endif
10982 object_offset_in_bits = bitpos_int;
30ade641 10983
8c3f468d 10984 return object_offset_in_bits / BITS_PER_UNIT;
30ade641 10985}
30ade641 10986\f
ec1e49cc 10987/* The following routines define various Dwarf attributes and any data
10988 associated with them. */
30ade641 10989
678d90bb 10990/* Add a location description attribute value to a DIE.
30ade641 10991
678d90bb 10992 This emits location attributes suitable for whole variables and
30ade641 10993 whole parameters. Note that the location attributes for struct fields are
10994 generated by the routine `data_member_location_attribute' below. */
ec1e49cc 10995
931e9893 10996static inline void
8ec3a57b 10997add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10998 dw_loc_descr_ref descr)
30ade641 10999{
86e12d28 11000 if (descr != 0)
11001 add_AT_loc (die, attr_kind, descr);
30ade641 11002}
11003
8c3f468d 11004/* Attach the specialized form of location attribute used for data members of
11005 struct and union types. In the special case of a FIELD_DECL node which
11006 represents a bit-field, the "offset" part of this special location
11007 descriptor must indicate the distance in bytes from the lowest-addressed
11008 byte of the containing struct or union type to the lowest-addressed byte of
11009 the "containing object" for the bit-field. (See the `field_byte_offset'
11010 function above).
11011
11012 For any given bit-field, the "containing object" is a hypothetical object
11013 (of some integral or enum type) within which the given bit-field lives. The
11014 type of this hypothetical "containing object" is always the same as the
11015 declared type of the individual bit-field itself (for GCC anyway... the
11016 DWARF spec doesn't actually mandate this). Note that it is the size (in
11017 bytes) of the hypothetical "containing object" which will be given in the
11018 DW_AT_byte_size attribute for this bit-field. (See the
11019 `byte_size_attribute' function below.) It is also used when calculating the
11020 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
11021 function below.) */
ec1e49cc 11022
30ade641 11023static void
8ec3a57b 11024add_data_member_location_attribute (dw_die_ref die, tree decl)
30ade641 11025{
3d867824 11026 HOST_WIDE_INT offset;
3e14aa38 11027 dw_loc_descr_ref loc_descr = 0;
30ade641 11028
3cb98335 11029 if (TREE_CODE (decl) == TREE_BINFO)
3e14aa38 11030 {
11031 /* We're working on the TAG_inheritance for a base class. */
57c28194 11032 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
3e14aa38 11033 {
11034 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11035 aren't at a fixed offset from all (sub)objects of the same
11036 type. We need to extract the appropriate offset from our
11037 vtable. The following dwarf expression means
11038
11039 BaseAddr = ObAddr + *((*ObAddr) - Offset)
11040
11041 This is specific to the V3 ABI, of course. */
11042
11043 dw_loc_descr_ref tmp;
8c3f468d 11044
3e14aa38 11045 /* Make a copy of the object address. */
11046 tmp = new_loc_descr (DW_OP_dup, 0, 0);
11047 add_loc_descr (&loc_descr, tmp);
8c3f468d 11048
3e14aa38 11049 /* Extract the vtable address. */
11050 tmp = new_loc_descr (DW_OP_deref, 0, 0);
11051 add_loc_descr (&loc_descr, tmp);
8c3f468d 11052
3e14aa38 11053 /* Calculate the address of the offset. */
11054 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
7bd4f6b6 11055 gcc_assert (offset < 0);
8c3f468d 11056
3e14aa38 11057 tmp = int_loc_descriptor (-offset);
11058 add_loc_descr (&loc_descr, tmp);
11059 tmp = new_loc_descr (DW_OP_minus, 0, 0);
11060 add_loc_descr (&loc_descr, tmp);
8c3f468d 11061
3e14aa38 11062 /* Extract the offset. */
11063 tmp = new_loc_descr (DW_OP_deref, 0, 0);
11064 add_loc_descr (&loc_descr, tmp);
8c3f468d 11065
3e14aa38 11066 /* Add it to the object address. */
11067 tmp = new_loc_descr (DW_OP_plus, 0, 0);
11068 add_loc_descr (&loc_descr, tmp);
11069 }
11070 else
11071 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11072 }
404ba76d 11073 else
11074 offset = field_byte_offset (decl);
11075
3e14aa38 11076 if (! loc_descr)
11077 {
11078 enum dwarf_location_atom op;
11079
8c3f468d 11080 /* The DWARF2 standard says that we should assume that the structure
11081 address is already on the stack, so we can specify a structure field
11082 address by using DW_OP_plus_uconst. */
ec1e49cc 11083
30ade641 11084#ifdef MIPS_DEBUGGING_INFO
8c3f468d 11085 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11086 operator correctly. It works only if we leave the offset on the
11087 stack. */
3e14aa38 11088 op = DW_OP_constu;
30ade641 11089#else
3e14aa38 11090 op = DW_OP_plus_uconst;
30ade641 11091#endif
ec1e49cc 11092
3e14aa38 11093 loc_descr = new_loc_descr (op, offset, 0);
11094 }
8c3f468d 11095
30ade641 11096 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11097}
11098
1b6ad376 11099/* Writes integer values to dw_vec_const array. */
11100
11101static void
11102insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11103{
11104 while (size != 0)
11105 {
11106 *dest++ = val & 0xff;
11107 val >>= 8;
11108 --size;
11109 }
11110}
11111
11112/* Reads integers from dw_vec_const array. Inverse of insert_int. */
11113
11114static HOST_WIDE_INT
11115extract_int (const unsigned char *src, unsigned int size)
11116{
11117 HOST_WIDE_INT val = 0;
11118
11119 src += size;
11120 while (size != 0)
11121 {
11122 val <<= 8;
11123 val |= *--src & 0xff;
11124 --size;
11125 }
11126 return val;
11127}
11128
11129/* Writes floating point values to dw_vec_const array. */
11130
11131static void
5493cb9a 11132insert_float (const_rtx rtl, unsigned char *array)
1b6ad376 11133{
11134 REAL_VALUE_TYPE rv;
11135 long val[4];
11136 int i;
11137
11138 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11139 real_to_target (val, &rv, GET_MODE (rtl));
11140
11141 /* real_to_target puts 32-bit pieces in each long. Pack them. */
11142 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11143 {
11144 insert_int (val[i], 4, array);
11145 array += 4;
11146 }
11147}
11148
df07c3ae 11149/* Attach a DW_AT_const_value attribute for a variable or a parameter which
30ade641 11150 does not have a "location" either in memory or in a register. These
11151 things can arise in GNU C when a constant is passed as an actual parameter
11152 to an inlined function. They can also arise in C++ where declared
11153 constants do not necessarily get memory "homes". */
ec1e49cc 11154
30ade641 11155static void
8ec3a57b 11156add_const_value_attribute (dw_die_ref die, rtx rtl)
30ade641 11157{
11158 switch (GET_CODE (rtl))
11159 {
11160 case CONST_INT:
ca98eb0a 11161 {
11162 HOST_WIDE_INT val = INTVAL (rtl);
bc70bd5e 11163
3d867824 11164 if (val < 0)
11165 add_AT_int (die, DW_AT_const_value, val);
8ff30ff6 11166 else
3d867824 11167 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
ca98eb0a 11168 }
30ade641 11169 break;
11170
11171 case CONST_DOUBLE:
11172 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
c83a163c 11173 floating-point constant. A CONST_DOUBLE is used whenever the
11174 constant requires more than one word in order to be adequately
11175 represented. We output CONST_DOUBLEs as blocks. */
df78b73b 11176 {
19cb6b50 11177 enum machine_mode mode = GET_MODE (rtl);
df78b73b 11178
cee7491d 11179 if (SCALAR_FLOAT_MODE_P (mode))
df78b73b 11180 {
1b6ad376 11181 unsigned int length = GET_MODE_SIZE (mode);
2457c754 11182 unsigned char *array = GGC_NEWVEC (unsigned char, length);
df78b73b 11183
1b6ad376 11184 insert_float (rtl, array);
11185 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
df78b73b 11186 }
11187 else
ca98eb0a 11188 {
11189 /* ??? We really should be using HOST_WIDE_INT throughout. */
7bd4f6b6 11190 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
8c3f468d 11191
ca98eb0a 11192 add_AT_long_long (die, DW_AT_const_value,
11193 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11194 }
df78b73b 11195 }
30ade641 11196 break;
11197
1b6ad376 11198 case CONST_VECTOR:
11199 {
11200 enum machine_mode mode = GET_MODE (rtl);
11201 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11202 unsigned int length = CONST_VECTOR_NUNITS (rtl);
2457c754 11203 unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
1b6ad376 11204 unsigned int i;
11205 unsigned char *p;
11206
7bd4f6b6 11207 switch (GET_MODE_CLASS (mode))
1b6ad376 11208 {
7bd4f6b6 11209 case MODE_VECTOR_INT:
1b6ad376 11210 for (i = 0, p = array; i < length; i++, p += elt_size)
11211 {
11212 rtx elt = CONST_VECTOR_ELT (rtl, i);
11213 HOST_WIDE_INT lo, hi;
8ff30ff6 11214
7bd4f6b6 11215 switch (GET_CODE (elt))
1b6ad376 11216 {
7bd4f6b6 11217 case CONST_INT:
1b6ad376 11218 lo = INTVAL (elt);
11219 hi = -(lo < 0);
7bd4f6b6 11220 break;
8ff30ff6 11221
7bd4f6b6 11222 case CONST_DOUBLE:
1b6ad376 11223 lo = CONST_DOUBLE_LOW (elt);
11224 hi = CONST_DOUBLE_HIGH (elt);
7bd4f6b6 11225 break;
8ff30ff6 11226
7bd4f6b6 11227 default:
11228 gcc_unreachable ();
1b6ad376 11229 }
8ff30ff6 11230
1b6ad376 11231 if (elt_size <= sizeof (HOST_WIDE_INT))
11232 insert_int (lo, elt_size, p);
7bd4f6b6 11233 else
1b6ad376 11234 {
11235 unsigned char *p0 = p;
11236 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
8ff30ff6 11237
7bd4f6b6 11238 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
1b6ad376 11239 if (WORDS_BIG_ENDIAN)
11240 {
11241 p0 = p1;
11242 p1 = p;
11243 }
11244 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11245 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11246 }
1b6ad376 11247 }
7bd4f6b6 11248 break;
11249
11250 case MODE_VECTOR_FLOAT:
1b6ad376 11251 for (i = 0, p = array; i < length; i++, p += elt_size)
11252 {
11253 rtx elt = CONST_VECTOR_ELT (rtl, i);
11254 insert_float (elt, p);
11255 }
7bd4f6b6 11256 break;
11257
11258 default:
11259 gcc_unreachable ();
1b6ad376 11260 }
1b6ad376 11261
11262 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11263 }
11264 break;
11265
30ade641 11266 case CONST_STRING:
11267 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11268 break;
11269
11270 case SYMBOL_REF:
11271 case LABEL_REF:
11272 case CONST:
7facaa35 11273 add_AT_addr (die, DW_AT_const_value, rtl);
62aedc4c 11274 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
30ade641 11275 break;
11276
11277 case PLUS:
11278 /* In cases where an inlined instance of an inline function is passed
c83a163c 11279 the address of an `auto' variable (which is local to the caller) we
11280 can get a situation where the DECL_RTL of the artificial local
11281 variable (for the inlining) which acts as a stand-in for the
11282 corresponding formal parameter (of the inline function) will look
11283 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
11284 exactly a compile-time constant expression, but it isn't the address
11285 of the (artificial) local variable either. Rather, it represents the
11286 *value* which the artificial local variable always has during its
11287 lifetime. We currently have no way to represent such quasi-constant
11288 values in Dwarf, so for now we just punt and generate nothing. */
30ade641 11289 break;
11290
11291 default:
11292 /* No other kinds of rtx should be possible here. */
7bd4f6b6 11293 gcc_unreachable ();
30ade641 11294 }
11295
11296}
11297
e124d6c7 11298/* Determine whether the evaluation of EXPR references any variables
11299 or functions which aren't otherwise used (and therefore may not be
11300 output). */
11301static tree
11302reference_to_unused (tree * tp, int * walk_subtrees,
11303 void * data ATTRIBUTE_UNUSED)
11304{
75a70cf9 11305 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
e124d6c7 11306 *walk_subtrees = 0;
61a9389f 11307
e124d6c7 11308 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11309 && ! TREE_ASM_WRITTEN (*tp))
11310 return *tp;
a43689ad 11311 /* ??? The C++ FE emits debug information for using decls, so
11312 putting gcc_unreachable here falls over. See PR31899. For now
11313 be conservative. */
5615be0f 11314 else if (!cgraph_global_info_ready
11315 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
a43689ad 11316 return *tp;
5615be0f 11317 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
56e902bd 11318 {
11319 struct varpool_node *node = varpool_node (*tp);
11320 if (!node->needed)
11321 return *tp;
11322 }
5615be0f 11323 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11324 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11325 {
11326 struct cgraph_node *node = cgraph_node (*tp);
09fc9532 11327 if (node->process || TREE_ASM_WRITTEN (*tp))
61a9389f 11328 return *tp;
5615be0f 11329 }
9238c28d 11330 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11331 return *tp;
56e902bd 11332
11333 return NULL_TREE;
e124d6c7 11334}
11335
9293d8bd 11336/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11337 for use in a later add_const_value_attribute call. */
11338
11339static rtx
11340rtl_for_decl_init (tree init, tree type)
11341{
11342 rtx rtl = NULL_RTX;
11343
11344 /* If a variable is initialized with a string constant without embedded
11345 zeros, build CONST_STRING. */
11346 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11347 {
11348 tree enttype = TREE_TYPE (type);
11349 tree domain = TYPE_DOMAIN (type);
11350 enum machine_mode mode = TYPE_MODE (enttype);
11351
11352 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11353 && domain
11354 && integer_zerop (TYPE_MIN_VALUE (domain))
11355 && compare_tree_int (TYPE_MAX_VALUE (domain),
11356 TREE_STRING_LENGTH (init) - 1) == 0
11357 && ((size_t) TREE_STRING_LENGTH (init)
11358 == strlen (TREE_STRING_POINTER (init)) + 1))
11359 rtl = gen_rtx_CONST_STRING (VOIDmode,
11360 ggc_strdup (TREE_STRING_POINTER (init)));
11361 }
bf591863 11362 /* Other aggregates, and complex values, could be represented using
11363 CONCAT: FIXME! */
11364 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11365 ;
61a9389f 11366 /* Vectors only work if their mode is supported by the target.
bf591863 11367 FIXME: generic vectors ought to work too. */
11368 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
e124d6c7 11369 ;
9293d8bd 11370 /* If the initializer is something that we know will expand into an
e124d6c7 11371 immediate RTL constant, expand it now. We must be careful not to
11372 reference variables which won't be output. */
11373 else if (initializer_constant_valid_p (init, type)
11374 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
9293d8bd 11375 {
c3910319 11376 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11377 possible. */
11378 if (TREE_CODE (type) == VECTOR_TYPE)
11379 switch (TREE_CODE (init))
11380 {
11381 case VECTOR_CST:
11382 break;
11383 case CONSTRUCTOR:
11384 if (TREE_CONSTANT (init))
11385 {
11386 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11387 bool constant_p = true;
11388 tree value;
11389 unsigned HOST_WIDE_INT ix;
11390
11391 /* Even when ctor is constant, it might contain non-*_CST
11392 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11393 belong into VECTOR_CST nodes. */
11394 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11395 if (!CONSTANT_CLASS_P (value))
11396 {
11397 constant_p = false;
11398 break;
11399 }
11400
11401 if (constant_p)
11402 {
11403 init = build_vector_from_ctor (type, elts);
11404 break;
11405 }
11406 }
11407 /* FALLTHRU */
11408
11409 default:
11410 return NULL;
11411 }
11412
9293d8bd 11413 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11414
11415 /* If expand_expr returns a MEM, it wasn't immediate. */
11416 gcc_assert (!rtl || !MEM_P (rtl));
11417 }
11418
11419 return rtl;
11420}
11421
11422/* Generate RTL for the variable DECL to represent its location. */
11423
9ed904da 11424static rtx
8ec3a57b 11425rtl_for_decl_location (tree decl)
30ade641 11426{
19cb6b50 11427 rtx rtl;
ec1e49cc 11428
30ade641 11429 /* Here we have to decide where we are going to say the parameter "lives"
11430 (as far as the debugger is concerned). We only have a couple of
11431 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
ec1e49cc 11432
f80d1bcd 11433 DECL_RTL normally indicates where the parameter lives during most of the
ec1e49cc 11434 activation of the function. If optimization is enabled however, this
f80d1bcd 11435 could be either NULL or else a pseudo-reg. Both of those cases indicate
30ade641 11436 that the parameter doesn't really live anywhere (as far as the code
11437 generation parts of GCC are concerned) during most of the function's
11438 activation. That will happen (for example) if the parameter is never
ec1e49cc 11439 referenced within the function.
11440
11441 We could just generate a location descriptor here for all non-NULL
11442 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11443 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11444 where DECL_RTL is NULL or is a pseudo-reg.
11445
11446 Note however that we can only get away with using DECL_INCOMING_RTL as
11447 a backup substitute for DECL_RTL in certain limited cases. In cases
11448 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11449 we can be sure that the parameter was passed using the same type as it is
11450 declared to have within the function, and that its DECL_INCOMING_RTL
11451 points us to a place where a value of that type is passed.
11452
11453 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11454 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11455 because in these cases DECL_INCOMING_RTL points us to a value of some
11456 type which is *different* from the type of the parameter itself. Thus,
11457 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11458 such cases, the debugger would end up (for example) trying to fetch a
11459 `float' from a place which actually contains the first part of a
11460 `double'. That would lead to really incorrect and confusing
11461 output at debug-time.
11462
11463 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11464 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
11465 are a couple of exceptions however. On little-endian machines we can
11466 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11467 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11468 an integral type that is smaller than TREE_TYPE (decl). These cases arise
11469 when (on a little-endian machine) a non-prototyped function has a
11470 parameter declared to be of type `short' or `char'. In such cases,
11471 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11472 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11473 passed `int' value. If the debugger then uses that address to fetch
11474 a `short' or a `char' (on a little-endian machine) the result will be
11475 the correct data, so we allow for such exceptional cases below.
11476
11477 Note that our goal here is to describe the place where the given formal
8c3f468d 11478 parameter lives during most of the function's activation (i.e. between the
11479 end of the prologue and the start of the epilogue). We'll do that as best
11480 as we can. Note however that if the given formal parameter is modified
11481 sometime during the execution of the function, then a stack backtrace (at
11482 debug-time) will show the function as having been called with the *new*
11483 value rather than the value which was originally passed in. This happens
11484 rarely enough that it is not a major problem, but it *is* a problem, and
11485 I'd like to fix it.
11486
11487 A future version of dwarf2out.c may generate two additional attributes for
11488 any given DW_TAG_formal_parameter DIE which will describe the "passed
11489 type" and the "passed location" for the given formal parameter in addition
11490 to the attributes we now generate to indicate the "declared type" and the
11491 "active location" for each parameter. This additional set of attributes
11492 could be used by debuggers for stack backtraces. Separately, note that
11493 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11494 This happens (for example) for inlined-instances of inline function formal
11495 parameters which are never referenced. This really shouldn't be
11496 happening. All PARM_DECL nodes should get valid non-NULL
4ee9c684 11497 DECL_INCOMING_RTL values. FIXME. */
30ade641 11498
11499 /* Use DECL_RTL as the "location" unless we find something better. */
ff12286a 11500 rtl = DECL_RTL_IF_SET (decl);
30ade641 11501
f3546830 11502 /* When generating abstract instances, ignore everything except
3332aee2 11503 constants, symbols living in memory, and symbols living in
11504 fixed registers. */
f3546830 11505 if (! reload_completed)
11506 {
11507 if (rtl
11508 && (CONSTANT_P (rtl)
e16ceb8e 11509 || (MEM_P (rtl)
3332aee2 11510 && CONSTANT_P (XEXP (rtl, 0)))
8ad4c111 11511 || (REG_P (rtl)
3332aee2 11512 && TREE_CODE (decl) == VAR_DECL
11513 && TREE_STATIC (decl))))
e93986bb 11514 {
883b2e73 11515 rtl = targetm.delegitimize_address (rtl);
e93986bb 11516 return rtl;
11517 }
f3546830 11518 rtl = NULL_RTX;
11519 }
11520 else if (TREE_CODE (decl) == PARM_DECL)
30ade641 11521 {
11522 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11523 {
0189d7ee 11524 tree declared_type = TREE_TYPE (decl);
11525 tree passed_type = DECL_ARG_TYPE (decl);
11526 enum machine_mode dmode = TYPE_MODE (declared_type);
11527 enum machine_mode pmode = TYPE_MODE (passed_type);
30ade641 11528
ec1e49cc 11529 /* This decl represents a formal parameter which was optimized out.
30ade641 11530 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
8c3f468d 11531 all cases where (rtl == NULL_RTX) just below. */
0189d7ee 11532 if (dmode == pmode)
f80d1bcd 11533 rtl = DECL_INCOMING_RTL (decl);
0189d7ee 11534 else if (SCALAR_INT_MODE_P (dmode)
a4cb69f8 11535 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11536 && DECL_INCOMING_RTL (decl))
0189d7ee 11537 {
11538 rtx inc = DECL_INCOMING_RTL (decl);
11539 if (REG_P (inc))
11540 rtl = inc;
11541 else if (MEM_P (inc))
11542 {
11543 if (BYTES_BIG_ENDIAN)
11544 rtl = adjust_address_nv (inc, dmode,
11545 GET_MODE_SIZE (pmode)
11546 - GET_MODE_SIZE (dmode));
11547 else
11548 rtl = inc;
11549 }
11550 }
30ade641 11551 }
80291b9e 11552
11553 /* If the parm was passed in registers, but lives on the stack, then
11554 make a big endian correction if the mode of the type of the
11555 parameter is not the same as the mode of the rtl. */
11556 /* ??? This is the same series of checks that are made in dbxout.c before
11557 we reach the big endian correction code there. It isn't clear if all
11558 of these checks are necessary here, but keeping them all is the safe
11559 thing to do. */
e16ceb8e 11560 else if (MEM_P (rtl)
80291b9e 11561 && XEXP (rtl, 0) != const0_rtx
11562 && ! CONSTANT_P (XEXP (rtl, 0))
11563 /* Not passed in memory. */
e16ceb8e 11564 && !MEM_P (DECL_INCOMING_RTL (decl))
80291b9e 11565 /* Not passed by invisible reference. */
8ad4c111 11566 && (!REG_P (XEXP (rtl, 0))
80291b9e 11567 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11568 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11569#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11570 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11571#endif
11572 )
11573 /* Big endian correction check. */
11574 && BYTES_BIG_ENDIAN
11575 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11576 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11577 < UNITS_PER_WORD))
11578 {
11579 int offset = (UNITS_PER_WORD
11580 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
8c3f468d 11581
80291b9e 11582 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11583 plus_constant (XEXP (rtl, 0), offset));
11584 }
30ade641 11585 }
13906b02 11586 else if (TREE_CODE (decl) == VAR_DECL
c7c9d0ca 11587 && rtl
e16ceb8e 11588 && MEM_P (rtl)
13906b02 11589 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11590 && BYTES_BIG_ENDIAN)
11591 {
11592 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11593 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11594
11595 /* If a variable is declared "register" yet is smaller than
11596 a register, then if we store the variable to memory, it
11597 looks like we're storing a register-sized value, when in
11598 fact we are not. We need to adjust the offset of the
11599 storage location to reflect the actual value's bytes,
11600 else gdb will not be able to display it. */
11601 if (rsize > dsize)
11602 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11603 plus_constant (XEXP (rtl, 0), rsize-dsize));
11604 }
ec1e49cc 11605
8c3f468d 11606 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11607 and will have been substituted directly into all expressions that use it.
11608 C does not have such a concept, but C++ and other languages do. */
12d886b8 11609 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9293d8bd 11610 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
2fbd3b4c 11611
e93986bb 11612 if (rtl)
883b2e73 11613 rtl = targetm.delegitimize_address (rtl);
931e9893 11614
11615 /* If we don't look past the constant pool, we risk emitting a
11616 reference to a constant pool entry that isn't referenced from
11617 code, and thus is not emitted. */
11618 if (rtl)
11619 rtl = avoid_constant_pool_reference (rtl);
11620
9ed904da 11621 return rtl;
11622}
11623
12d886b8 11624/* We need to figure out what section we should use as the base for the
11625 address ranges where a given location is valid.
11626 1. If this particular DECL has a section associated with it, use that.
11627 2. If this function has a section associated with it, use that.
11628 3. Otherwise, use the text section.
11629 XXX: If you split a variable across multiple sections, we won't notice. */
11630
11631static const char *
5493cb9a 11632secname_for_decl (const_tree decl)
12d886b8 11633{
11634 const char *secname;
11635
11636 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11637 {
11638 tree sectree = DECL_SECTION_NAME (decl);
11639 secname = TREE_STRING_POINTER (sectree);
11640 }
11641 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11642 {
11643 tree sectree = DECL_SECTION_NAME (current_function_decl);
11644 secname = TREE_STRING_POINTER (sectree);
11645 }
5fbee89d 11646 else if (cfun && in_cold_section_p)
abe32cce 11647 secname = crtl->subsections.cold_section_label;
12d886b8 11648 else
11649 secname = text_section_label;
11650
11651 return secname;
11652}
11653
b61ffa4f 11654/* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
11655 returned. If so, the decl for the COMMON block is returned, and the
a12691f0 11656 value is the offset into the common block for the symbol. */
11657
845c3089 11658static tree
11659fortran_common (tree decl, HOST_WIDE_INT *value)
a12691f0 11660{
845c3089 11661 tree val_expr, cvar;
11662 enum machine_mode mode;
11663 HOST_WIDE_INT bitsize, bitpos;
11664 tree offset;
11665 int volatilep = 0, unsignedp = 0;
11666
a12691f0 11667 /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11668 it does not have a value (the offset into the common area), or if it
11669 is thread local (as opposed to global) then it isn't common, and shouldn't
11670 be handled as such. */
11671 if (TREE_CODE (decl) != VAR_DECL
845c3089 11672 || !TREE_PUBLIC (decl)
11673 || !TREE_STATIC (decl)
11674 || !DECL_HAS_VALUE_EXPR_P (decl)
11675 || !is_fortran ())
11676 return NULL_TREE;
a12691f0 11677
845c3089 11678 val_expr = DECL_VALUE_EXPR (decl);
11679 if (TREE_CODE (val_expr) != COMPONENT_REF)
11680 return NULL_TREE;
a12691f0 11681
845c3089 11682 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11683 &mode, &unsignedp, &volatilep, true);
a12691f0 11684
845c3089 11685 if (cvar == NULL_TREE
11686 || TREE_CODE (cvar) != VAR_DECL
11687 || DECL_ARTIFICIAL (cvar)
11688 || !TREE_PUBLIC (cvar))
11689 return NULL_TREE;
a12691f0 11690
845c3089 11691 *value = 0;
11692 if (offset != NULL)
11693 {
11694 if (!host_integerp (offset, 0))
11695 return NULL_TREE;
11696 *value = tree_low_cst (offset, 0);
a12691f0 11697 }
845c3089 11698 if (bitpos != 0)
11699 *value += bitpos / BITS_PER_UNIT;
a12691f0 11700
845c3089 11701 return cvar;
a12691f0 11702}
11703
cc476c39 11704/* Dereference a location expression LOC if DECL is passed by invisible
11705 reference. */
11706
11707static dw_loc_descr_ref
11708loc_by_reference (dw_loc_descr_ref loc, tree decl)
11709{
11710 HOST_WIDE_INT size;
11711 enum dwarf_location_atom op;
11712
11713 if (loc == NULL)
11714 return NULL;
11715
1095d222 11716 if ((TREE_CODE (decl) != PARM_DECL
11717 && TREE_CODE (decl) != RESULT_DECL
11718 && (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)))
cc476c39 11719 || !DECL_BY_REFERENCE (decl))
11720 return loc;
11721
11722 size = int_size_in_bytes (TREE_TYPE (decl));
11723 if (size > DWARF2_ADDR_SIZE || size == -1)
11724 return 0;
11725 else if (size == DWARF2_ADDR_SIZE)
11726 op = DW_OP_deref;
11727 else
11728 op = DW_OP_deref_size;
11729 add_loc_descr (&loc, new_loc_descr (op, size, 0));
11730 return loc;
11731}
a12691f0 11732
df07c3ae 11733/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9ed904da 11734 data attribute for a variable or a parameter. We generate the
11735 DW_AT_const_value attribute only in those cases where the given variable
11736 or parameter does not have a true "location" either in memory or in a
11737 register. This can happen (for example) when a constant is passed as an
11738 actual argument in a call to an inline function. (It's possible that
11739 these things can crop up in other ways also.) Note that one type of
11740 constant value which can be passed into an inlined function is a constant
11741 pointer. This can happen for example if an actual argument in an inlined
11742 function call evaluates to a compile-time constant address. */
11743
11744static void
b2025850 11745add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11746 enum dwarf_attribute attr)
9ed904da 11747{
19cb6b50 11748 rtx rtl;
931e9893 11749 dw_loc_descr_ref descr;
b2025850 11750 var_loc_list *loc_list;
6ad1968a 11751 struct var_loc_node *node;
9ed904da 11752 if (TREE_CODE (decl) == ERROR_MARK)
11753 return;
7bd4f6b6 11754
11755 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11756 || TREE_CODE (decl) == RESULT_DECL);
61a9389f 11757
b2025850 11758 /* See if we possibly have multiple locations for this variable. */
11759 loc_list = lookup_decl_loc (decl);
11760
11761 /* If it truly has multiple locations, the first and last node will
11762 differ. */
11763 if (loc_list && loc_list->first != loc_list->last)
11764 {
12d886b8 11765 const char *endname, *secname;
b2025850 11766 dw_loc_list_ref list;
11767 rtx varloc;
d53bb226 11768 enum var_init_status initialized;
6ad1968a 11769
b2025850 11770 /* Now that we know what section we are using for a base,
61a9389f 11771 actually construct the list of locations.
b2025850 11772 The first location information is what is passed to the
11773 function that creates the location list, and the remaining
11774 locations just get added on to that list.
11775 Note that we only know the start address for a location
11776 (IE location changes), so to build the range, we use
11777 the range [current location start, next location start].
11778 This means we have to special case the last node, and generate
11779 a range of [last location start, end of function label]. */
11780
11781 node = loc_list->first;
11782 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12d886b8 11783 secname = secname_for_decl (decl);
11784
d53bb226 11785 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11786 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11787 else
11788 initialized = VAR_INIT_STATUS_INITIALIZED;
11789
cc476c39 11790 descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11791 list = new_loc_list (descr, node->label, node->next->label, secname, 1);
b2025850 11792 node = node->next;
11793
11794 for (; node->next; node = node->next)
11795 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11796 {
11797 /* The variable has a location between NODE->LABEL and
11798 NODE->NEXT->LABEL. */
d53bb226 11799 enum var_init_status initialized =
11800 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
b2025850 11801 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
cc476c39 11802 descr = loc_by_reference (loc_descriptor (varloc, initialized),
11803 decl);
11804 add_loc_descr_to_loc_list (&list, descr,
b2025850 11805 node->label, node->next->label, secname);
11806 }
11807
11808 /* If the variable has a location at the last label
11809 it keeps its location until the end of function. */
11810 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11811 {
11812 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
d53bb226 11813 enum var_init_status initialized =
11814 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
b2025850 11815
11816 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11817 if (!current_function_decl)
11818 endname = text_end_label;
11819 else
11820 {
11821 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11822 current_function_funcdef_no);
11823 endname = ggc_strdup (label_id);
11824 }
cc476c39 11825 descr = loc_by_reference (loc_descriptor (varloc, initialized),
11826 decl);
11827 add_loc_descr_to_loc_list (&list, descr,
b2025850 11828 node->label, endname, secname);
11829 }
11830
11831 /* Finally, add the location list to the DIE, and we are done. */
11832 add_AT_loc_list (die, attr, list);
11833 return;
11834 }
11835
6ad1968a 11836 /* Try to get some constant RTL for this decl, and use that as the value of
11837 the location. */
61a9389f 11838
9ed904da 11839 rtl = rtl_for_decl_location (decl);
afcf285e 11840 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
30ade641 11841 {
30ade641 11842 add_const_value_attribute (die, rtl);
afcf285e 11843 return;
30ade641 11844 }
61a9389f 11845
600b9bbf 11846 /* If we have tried to generate the location otherwise, and it
6ad1968a 11847 didn't work out (we wouldn't be here if we did), and we have a one entry
11848 location list, try generating a location from that. */
11849 if (loc_list && loc_list->first)
11850 {
d53bb226 11851 enum var_init_status status;
6ad1968a 11852 node = loc_list->first;
d53bb226 11853 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11854 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
6ad1968a 11855 if (descr)
600b9bbf 11856 {
cc476c39 11857 descr = loc_by_reference (descr, decl);
600b9bbf 11858 add_AT_location_description (die, attr, descr);
11859 return;
11860 }
11861 }
11862
11863 /* We couldn't get any rtl, so try directly generating the location
11864 description from the tree. */
11865 descr = loc_descriptor_from_tree (decl);
11866 if (descr)
11867 {
cc476c39 11868 descr = loc_by_reference (descr, decl);
600b9bbf 11869 add_AT_location_description (die, attr, descr);
11870 return;
6ad1968a 11871 }
e124d6c7 11872 /* None of that worked, so it must not really have a location;
11873 try adding a constant value attribute from the DECL_INITIAL. */
11874 tree_add_const_value_attribute (die, decl);
30ade641 11875}
11876
89f29a1b 11877/* Add VARIABLE and DIE into deferred locations list. */
11878
11879static void
11880defer_location (tree variable, dw_die_ref die)
11881{
11882 deferred_locations entry;
11883 entry.variable = variable;
11884 entry.die = die;
11885 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
11886}
11887
2eb674c9 11888/* Helper function for tree_add_const_value_attribute. Natively encode
11889 initializer INIT into an array. Return true if successful. */
11890
11891static bool
11892native_encode_initializer (tree init, unsigned char *array, int size)
11893{
11894 tree type;
11895
11896 if (init == NULL_TREE)
11897 return false;
11898
11899 STRIP_NOPS (init);
11900 switch (TREE_CODE (init))
11901 {
11902 case STRING_CST:
11903 type = TREE_TYPE (init);
11904 if (TREE_CODE (type) == ARRAY_TYPE)
11905 {
11906 tree enttype = TREE_TYPE (type);
11907 enum machine_mode mode = TYPE_MODE (enttype);
11908
11909 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11910 return false;
11911 if (int_size_in_bytes (type) != size)
11912 return false;
11913 if (size > TREE_STRING_LENGTH (init))
11914 {
11915 memcpy (array, TREE_STRING_POINTER (init),
11916 TREE_STRING_LENGTH (init));
11917 memset (array + TREE_STRING_LENGTH (init),
11918 '\0', size - TREE_STRING_LENGTH (init));
11919 }
11920 else
11921 memcpy (array, TREE_STRING_POINTER (init), size);
11922 return true;
11923 }
11924 return false;
11925 case CONSTRUCTOR:
11926 type = TREE_TYPE (init);
11927 if (int_size_in_bytes (type) != size)
11928 return false;
11929 if (TREE_CODE (type) == ARRAY_TYPE)
11930 {
11931 HOST_WIDE_INT min_index;
11932 unsigned HOST_WIDE_INT cnt;
11933 int curpos = 0, fieldsize;
11934 constructor_elt *ce;
11935
11936 if (TYPE_DOMAIN (type) == NULL_TREE
11937 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11938 return false;
11939
11940 fieldsize = int_size_in_bytes (TREE_TYPE (type));
11941 if (fieldsize <= 0)
11942 return false;
11943
11944 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11945 memset (array, '\0', size);
11946 for (cnt = 0;
11947 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11948 cnt++)
11949 {
11950 tree val = ce->value;
11951 tree index = ce->index;
11952 int pos = curpos;
11953 if (index && TREE_CODE (index) == RANGE_EXPR)
11954 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11955 * fieldsize;
11956 else if (index)
00c47da2 11957 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
2eb674c9 11958
11959 if (val)
11960 {
11961 STRIP_NOPS (val);
11962 if (!native_encode_initializer (val, array + pos, fieldsize))
11963 return false;
11964 }
11965 curpos = pos + fieldsize;
11966 if (index && TREE_CODE (index) == RANGE_EXPR)
11967 {
11968 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
11969 - tree_low_cst (TREE_OPERAND (index, 0), 0);
11970 while (count > 0)
11971 {
11972 if (val)
11973 memcpy (array + curpos, array + pos, fieldsize);
11974 curpos += fieldsize;
11975 }
11976 }
11977 gcc_assert (curpos <= size);
11978 }
11979 return true;
11980 }
11981 else if (TREE_CODE (type) == RECORD_TYPE
11982 || TREE_CODE (type) == UNION_TYPE)
11983 {
11984 tree field = NULL_TREE;
11985 unsigned HOST_WIDE_INT cnt;
11986 constructor_elt *ce;
11987
11988 if (int_size_in_bytes (type) != size)
11989 return false;
11990
11991 if (TREE_CODE (type) == RECORD_TYPE)
11992 field = TYPE_FIELDS (type);
11993
11994 for (cnt = 0;
11995 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11996 cnt++, field = field ? TREE_CHAIN (field) : 0)
11997 {
11998 tree val = ce->value;
11999 int pos, fieldsize;
12000
12001 if (ce->index != 0)
12002 field = ce->index;
12003
12004 if (val)
12005 STRIP_NOPS (val);
12006
12007 if (field == NULL_TREE || DECL_BIT_FIELD (field))
12008 return false;
12009
12010 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12011 && TYPE_DOMAIN (TREE_TYPE (field))
12012 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12013 return false;
12014 else if (DECL_SIZE_UNIT (field) == NULL_TREE
12015 || !host_integerp (DECL_SIZE_UNIT (field), 0))
12016 return false;
12017 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12018 pos = int_byte_position (field);
12019 gcc_assert (pos + fieldsize <= size);
12020 if (val
12021 && !native_encode_initializer (val, array + pos, fieldsize))
12022 return false;
12023 }
12024 return true;
12025 }
12026 return false;
00c47da2 12027 case VIEW_CONVERT_EXPR:
12028 case NON_LVALUE_EXPR:
12029 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
2eb674c9 12030 default:
12031 return native_encode_expr (init, array, size) == size;
12032 }
12033}
12034
eabb26f3 12035/* If we don't have a copy of this variable in memory for some reason (such
12036 as a C++ member constant that doesn't have an out-of-line definition),
12037 we should tell the debugger about the constant value. */
12038
12039static void
8ec3a57b 12040tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
eabb26f3 12041{
45b07c10 12042 tree init;
eabb26f3 12043 tree type = TREE_TYPE (decl);
9293d8bd 12044 rtx rtl;
eabb26f3 12045
45b07c10 12046 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12047 return;
12048
12049 init = DECL_INITIAL (decl);
9293d8bd 12050 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
eabb26f3 12051 /* OK */;
12052 else
12053 return;
12054
9293d8bd 12055 rtl = rtl_for_decl_init (init, type);
12056 if (rtl)
12057 add_const_value_attribute (var_die, rtl);
2eb674c9 12058 /* If the host and target are sane, try harder. */
12059 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12060 && initializer_constant_valid_p (init, type))
12061 {
12062 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12063 if (size > 0 && (int) size == size)
12064 {
12065 unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12066
12067 if (native_encode_initializer (init, array, size))
12068 add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12069 }
12070 }
eabb26f3 12071}
ac02093f 12072
89fa767a 12073/* Convert the CFI instructions for the current function into a
12074 location list. This is used for DW_AT_frame_base when we targeting
12075 a dwarf2 consumer that does not support the dwarf3
12076 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
12077 expressions. */
12d886b8 12078
12079static dw_loc_list_ref
89fa767a 12080convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12d886b8 12081{
12082 dw_fde_ref fde;
12083 dw_loc_list_ref list, *list_tail;
12084 dw_cfi_ref cfi;
12085 dw_cfa_location last_cfa, next_cfa;
12086 const char *start_label, *last_label, *section;
12087
c0fd44c1 12088 fde = current_fde ();
12089 gcc_assert (fde != NULL);
12d886b8 12090
12091 section = secname_for_decl (current_function_decl);
12092 list_tail = &list;
12093 list = NULL;
12094
12095 next_cfa.reg = INVALID_REGNUM;
12096 next_cfa.offset = 0;
12097 next_cfa.indirect = 0;
12098 next_cfa.base_offset = 0;
12099
12100 start_label = fde->dw_fde_begin;
12101
12102 /* ??? Bald assumption that the CIE opcode list does not contain
12103 advance opcodes. */
12104 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12105 lookup_cfa_1 (cfi, &next_cfa);
12106
12107 last_cfa = next_cfa;
12108 last_label = start_label;
12109
12110 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12111 switch (cfi->dw_cfi_opc)
12112 {
bea04f76 12113 case DW_CFA_set_loc:
12d886b8 12114 case DW_CFA_advance_loc1:
12115 case DW_CFA_advance_loc2:
12116 case DW_CFA_advance_loc4:
12117 if (!cfa_equal_p (&last_cfa, &next_cfa))
12118 {
89fa767a 12119 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12120 start_label, last_label, section,
12121 list == NULL);
12d886b8 12122
12123 list_tail = &(*list_tail)->dw_loc_next;
12124 last_cfa = next_cfa;
12125 start_label = last_label;
12126 }
12127 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12128 break;
12129
12130 case DW_CFA_advance_loc:
12131 /* The encoding is complex enough that we should never emit this. */
12132 case DW_CFA_remember_state:
12133 case DW_CFA_restore_state:
12134 /* We don't handle these two in this function. It would be possible
12135 if it were to be required. */
12136 gcc_unreachable ();
12137
12138 default:
12139 lookup_cfa_1 (cfi, &next_cfa);
12140 break;
12141 }
12142
12143 if (!cfa_equal_p (&last_cfa, &next_cfa))
12144 {
89fa767a 12145 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12146 start_label, last_label, section,
12147 list == NULL);
12d886b8 12148 list_tail = &(*list_tail)->dw_loc_next;
12149 start_label = last_label;
12150 }
89fa767a 12151 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12152 start_label, fde->dw_fde_end, section,
12153 list == NULL);
12d886b8 12154
12155 return list;
12156}
12157
89fa767a 12158/* Compute a displacement from the "steady-state frame pointer" to the
12159 frame base (often the same as the CFA), and store it in
12160 frame_pointer_fb_offset. OFFSET is added to the displacement
12161 before the latter is negated. */
12d886b8 12162
12163static void
89fa767a 12164compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12d886b8 12165{
da72c083 12166 rtx reg, elim;
12167
12168#ifdef FRAME_POINTER_CFA_OFFSET
12169 reg = frame_pointer_rtx;
89fa767a 12170 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
da72c083 12171#else
12172 reg = arg_pointer_rtx;
89fa767a 12173 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
da72c083 12174#endif
12d886b8 12175
da72c083 12176 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12177 if (GET_CODE (elim) == PLUS)
12178 {
12179 offset += INTVAL (XEXP (elim, 1));
12180 elim = XEXP (elim, 0);
12181 }
27a7a23a 12182
12183 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12184 && (elim == hard_frame_pointer_rtx
12185 || elim == stack_pointer_rtx))
12186 || elim == (frame_pointer_needed
12187 ? hard_frame_pointer_rtx
12188 : stack_pointer_rtx));
12d886b8 12189
89fa767a 12190 frame_pointer_fb_offset = -offset;
12d886b8 12191}
12192
df07c3ae 12193/* Generate a DW_AT_name attribute given some string value to be included as
30ade641 12194 the value of the attribute. */
ec1e49cc 12195
ff279357 12196static void
8ec3a57b 12197add_name_attribute (dw_die_ref die, const char *name_string)
30ade641 12198{
ec1e49cc 12199 if (name_string != NULL && *name_string != 0)
155b05dc 12200 {
12201 if (demangle_name_func)
12202 name_string = (*demangle_name_func) (name_string);
12203
12204 add_AT_string (die, DW_AT_name, name_string);
12205 }
30ade641 12206}
12207
df07c3ae 12208/* Generate a DW_AT_comp_dir attribute for DIE. */
ff279357 12209
12210static void
8ec3a57b 12211add_comp_dir_attribute (dw_die_ref die)
ff279357 12212{
e7aa92b2 12213 const char *wd = get_src_pwd ();
ff279357 12214 if (wd != NULL)
5f1f2de5 12215 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
ff279357 12216}
12217
30ade641 12218/* Given a tree node describing an array bound (either lower or upper) output
b58d53bf 12219 a representation for that bound. */
ec1e49cc 12220
30ade641 12221static void
8ec3a57b 12222add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
30ade641 12223{
30ade641 12224 switch (TREE_CODE (bound))
12225 {
12226 case ERROR_MARK:
12227 return;
12228
04641143 12229 /* All fixed-bounds are represented by INTEGER_CST nodes. */
30ade641 12230 case INTEGER_CST:
5d844ba2 12231 if (! host_integerp (bound, 0)
12232 || (bound_attr == DW_AT_lower_bound
af4d39d8 12233 && (((is_c_family () || is_java ()) && integer_zerop (bound))
5d844ba2 12234 || (is_fortran () && integer_onep (bound)))))
aab2cf92 12235 /* Use the default. */
5d844ba2 12236 ;
0defae70 12237 else
5d844ba2 12238 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
30ade641 12239 break;
12240
72dd6141 12241 CASE_CONVERT:
f96c43fb 12242 case VIEW_CONVERT_EXPR:
34425fdc 12243 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12244 break;
f80d1bcd 12245
30ade641 12246 case SAVE_EXPR:
30ade641 12247 break;
8a8bfbe7 12248
678d90bb 12249 case VAR_DECL:
9ed904da 12250 case PARM_DECL:
4ee9c684 12251 case RESULT_DECL:
9ed904da 12252 {
12253 dw_die_ref decl_die = lookup_decl_die (bound);
6cf159a6 12254 dw_loc_descr_ref loc;
9ed904da 12255
12256 /* ??? Can this happen, or should the variable have been bound
12257 first? Probably it can, since I imagine that we try to create
12258 the types of parameters in the order in which they exist in
ac02093f 12259 the list, and won't have created a forward reference to a
9ed904da 12260 later parameter. */
12261 if (decl_die != NULL)
12262 add_AT_die_ref (subrange_die, bound_attr, decl_die);
6cf159a6 12263 else
12264 {
12265 loc = loc_descriptor_from_tree_1 (bound, 0);
12266 add_AT_location_description (subrange_die, bound_attr, loc);
12267 }
9ed904da 12268 break;
12269 }
678d90bb 12270
8a8bfbe7 12271 default:
9ed904da 12272 {
12273 /* Otherwise try to create a stack operation procedure to
12274 evaluate the value of the array bound. */
12275
12276 dw_die_ref ctx, decl_die;
12277 dw_loc_descr_ref loc;
12278
afcf285e 12279 loc = loc_descriptor_from_tree (bound);
9ed904da 12280 if (loc == NULL)
12281 break;
12282
86e12d28 12283 if (current_function_decl == 0)
12284 ctx = comp_unit_die;
12285 else
12286 ctx = lookup_decl_die (current_function_decl);
9ed904da 12287
15cfae4e 12288 decl_die = new_die (DW_TAG_variable, ctx, bound);
9ed904da 12289 add_AT_flag (decl_die, DW_AT_artificial, 1);
12290 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12291 add_AT_loc (decl_die, DW_AT_location, loc);
12292
12293 add_AT_die_ref (subrange_die, bound_attr, decl_die);
12294 break;
12295 }
30ade641 12296 }
12297}
12298
544cd34c 12299/* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12300 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12301 Note that the block of subscript information for an array type also
12302 includes information about the element type of the given array type. */
ec1e49cc 12303
30ade641 12304static void
544cd34c 12305add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
30ade641 12306{
19cb6b50 12307 unsigned dimension_number;
19cb6b50 12308 tree lower, upper;
12309 dw_die_ref subrange_die;
30ade641 12310
30ade641 12311 for (dimension_number = 0;
544cd34c 12312 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
30ade641 12313 type = TREE_TYPE (type), dimension_number++)
8c3f468d 12314 {
19cb6b50 12315 tree domain = TYPE_DOMAIN (type);
30ade641 12316
cdba1d8f 12317 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12318 break;
12319
30ade641 12320 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
f80d1bcd 12321 and (in GNU C only) variable bounds. Handle all three forms
c83a163c 12322 here. */
15cfae4e 12323 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
30ade641 12324 if (domain)
12325 {
12326 /* We have an array type with specified bounds. */
12327 lower = TYPE_MIN_VALUE (domain);
12328 upper = TYPE_MAX_VALUE (domain);
12329
139c3f48 12330 /* Define the index type. */
5b67860b 12331 if (TREE_TYPE (domain))
678d90bb 12332 {
12333 /* ??? This is probably an Ada unnamed subrange type. Ignore the
12334 TREE_TYPE field. We can't emit debug info for this
12335 because it is an unnamed integral type. */
12336 if (TREE_CODE (domain) == INTEGER_TYPE
12337 && TYPE_NAME (domain) == NULL_TREE
12338 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12339 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
f80d1bcd 12340 ;
678d90bb 12341 else
12342 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12343 type_die);
12344 }
5b67860b 12345
f52483b5 12346 /* ??? If upper is NULL, the array has unspecified length,
12347 but it does have a lower bound. This happens with Fortran
12348 dimension arr(N:*)
8ec3a57b 12349 Since the debugger is definitely going to need to know N
f52483b5 12350 to produce useful results, go ahead and output the lower
12351 bound solo, and hope the debugger can cope. */
12352
0defae70 12353 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
f52483b5 12354 if (upper)
12355 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
30ade641 12356 }
ec1e49cc 12357
8c3f468d 12358 /* Otherwise we have an array type with an unspecified length. The
12359 DWARF-2 spec does not say how to handle this; let's just leave out the
12360 bounds. */
30ade641 12361 }
30ade641 12362}
12363
12364static void
8ec3a57b 12365add_byte_size_attribute (dw_die_ref die, tree tree_node)
30ade641 12366{
19cb6b50 12367 unsigned size;
30ade641 12368
12369 switch (TREE_CODE (tree_node))
12370 {
12371 case ERROR_MARK:
12372 size = 0;
12373 break;
12374 case ENUMERAL_TYPE:
12375 case RECORD_TYPE:
12376 case UNION_TYPE:
12377 case QUAL_UNION_TYPE:
12378 size = int_size_in_bytes (tree_node);
12379 break;
12380 case FIELD_DECL:
12381 /* For a data member of a struct or union, the DW_AT_byte_size is
c83a163c 12382 generally given as the number of bytes normally allocated for an
12383 object of the *declared* type of the member itself. This is true
12384 even for bit-fields. */
30ade641 12385 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12386 break;
12387 default:
7bd4f6b6 12388 gcc_unreachable ();
30ade641 12389 }
12390
12391 /* Note that `size' might be -1 when we get to this point. If it is, that
12392 indicates that the byte size of the entity in question is variable. We
12393 have no good way of expressing this fact in Dwarf at the present time,
12394 so just let the -1 pass on through. */
30ade641 12395 add_AT_unsigned (die, DW_AT_byte_size, size);
12396}
12397
12398/* For a FIELD_DECL node which represents a bit-field, output an attribute
12399 which specifies the distance in bits from the highest order bit of the
12400 "containing object" for the bit-field to the highest order bit of the
12401 bit-field itself.
12402
8c3f468d 12403 For any given bit-field, the "containing object" is a hypothetical object
12404 (of some integral or enum type) within which the given bit-field lives. The
12405 type of this hypothetical "containing object" is always the same as the
12406 declared type of the individual bit-field itself. The determination of the
12407 exact location of the "containing object" for a bit-field is rather
12408 complicated. It's handled by the `field_byte_offset' function (above).
30ade641 12409
12410 Note that it is the size (in bytes) of the hypothetical "containing object"
12411 which will be given in the DW_AT_byte_size attribute for this bit-field.
12412 (See `byte_size_attribute' above). */
ec1e49cc 12413
12414static inline void
8ec3a57b 12415add_bit_offset_attribute (dw_die_ref die, tree decl)
30ade641 12416{
5d844ba2 12417 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12418 tree type = DECL_BIT_FIELD_TYPE (decl);
12419 HOST_WIDE_INT bitpos_int;
12420 HOST_WIDE_INT highest_order_object_bit_offset;
12421 HOST_WIDE_INT highest_order_field_bit_offset;
12422 HOST_WIDE_INT unsigned bit_offset;
30ade641 12423
7e2bfe1e 12424 /* Must be a field and a bit field. */
7bd4f6b6 12425 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
30ade641 12426
12427 /* We can't yet handle bit-fields whose offsets are variable, so if we
12428 encounter such things, just return without generating any attribute
5d844ba2 12429 whatsoever. Likewise for variable or too large size. */
12430 if (! host_integerp (bit_position (decl), 0)
12431 || ! host_integerp (DECL_SIZE (decl), 1))
ec1e49cc 12432 return;
12433
5d844ba2 12434 bitpos_int = int_bit_position (decl);
30ade641 12435
12436 /* Note that the bit offset is always the distance (in bits) from the
f80d1bcd 12437 highest-order bit of the "containing object" to the highest-order bit of
12438 the bit-field itself. Since the "high-order end" of any object or field
30ade641 12439 is different on big-endian and little-endian machines, the computation
12440 below must take account of these differences. */
12441 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12442 highest_order_field_bit_offset = bitpos_int;
12443
ec1e49cc 12444 if (! BYTES_BIG_ENDIAN)
30ade641 12445 {
5d844ba2 12446 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
30ade641 12447 highest_order_object_bit_offset += simple_type_size_in_bits (type);
12448 }
ec1e49cc 12449
12450 bit_offset
12451 = (! BYTES_BIG_ENDIAN
12452 ? highest_order_object_bit_offset - highest_order_field_bit_offset
12453 : highest_order_field_bit_offset - highest_order_object_bit_offset);
30ade641 12454
12455 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12456}
12457
12458/* For a FIELD_DECL node which represents a bit field, output an attribute
12459 which specifies the length in bits of the given field. */
ec1e49cc 12460
12461static inline void
8ec3a57b 12462add_bit_size_attribute (dw_die_ref die, tree decl)
30ade641 12463{
7e2bfe1e 12464 /* Must be a field and a bit field. */
7bd4f6b6 12465 gcc_assert (TREE_CODE (decl) == FIELD_DECL
12466 && DECL_BIT_FIELD_TYPE (decl));
5d844ba2 12467
12468 if (host_integerp (DECL_SIZE (decl), 1))
12469 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
30ade641 12470}
12471
464217f3 12472/* If the compiled language is ANSI C, then add a 'prototyped'
30ade641 12473 attribute, if arg types are given for the parameters of a function. */
ec1e49cc 12474
12475static inline void
8ec3a57b 12476add_prototyped_attribute (dw_die_ref die, tree func_type)
30ade641 12477{
464217f3 12478 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12479 && TYPE_ARG_TYPES (func_type) != NULL)
12480 add_AT_flag (die, DW_AT_prototyped, 1);
30ade641 12481}
12482
30ade641 12483/* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
12484 by looking in either the type declaration or object declaration
12485 equate table. */
ec1e49cc 12486
ebcb0478 12487static inline dw_die_ref
8ec3a57b 12488add_abstract_origin_attribute (dw_die_ref die, tree origin)
30ade641 12489{
12490 dw_die_ref origin_die = NULL;
b2ca6017 12491
bb0f15b4 12492 if (TREE_CODE (origin) != FUNCTION_DECL)
6c92ff4f 12493 {
12494 /* We may have gotten separated from the block for the inlined
12495 function, if we're in an exception handler or some such; make
12496 sure that the abstract function has been written out.
12497
c83a163c 12498 Doing this for nested functions is wrong, however; functions are
6c92ff4f 12499 distinct units, and our context might not even be inline. */
f929a98a 12500 tree fn = origin;
8c3f468d 12501
f929a98a 12502 if (TYPE_P (fn))
12503 fn = TYPE_STUB_DECL (fn);
61a9389f 12504
f10b7a77 12505 fn = decl_function_context (fn);
6c92ff4f 12506 if (fn)
f414ade2 12507 dwarf2out_abstract_function (fn);
6c92ff4f 12508 }
e3b3c2ae 12509
9308e976 12510 if (DECL_P (origin))
ec1e49cc 12511 origin_die = lookup_decl_die (origin);
9308e976 12512 else if (TYPE_P (origin))
ec1e49cc 12513 origin_die = lookup_type_die (origin);
12514
7c0a8197 12515 /* XXX: Functions that are never lowered don't always have correct block
12516 trees (in the case of java, they simply have no block tree, in some other
12517 languages). For these functions, there is nothing we can really do to
12518 output correct debug info for inlined functions in all cases. Rather
89f18f73 12519 than die, we'll just produce deficient debug info now, in that we will
7c0a8197 12520 have variables without a proper abstract origin. In the future, when all
12521 functions are lowered, we should re-add a gcc_assert (origin_die)
12522 here. */
f80d1bcd 12523
7c0a8197 12524 if (origin_die)
ebcb0478 12525 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12526 return origin_die;
30ade641 12527}
12528
6ed29fb8 12529/* We do not currently support the pure_virtual attribute. */
12530
ec1e49cc 12531static inline void
8ec3a57b 12532add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
30ade641 12533{
6efd403b 12534 if (DECL_VINDEX (func_decl))
30ade641 12535 {
6ed29fb8 12536 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
5d844ba2 12537
12538 if (host_integerp (DECL_VINDEX (func_decl), 0))
12539 add_AT_loc (die, DW_AT_vtable_elem_location,
12540 new_loc_descr (DW_OP_constu,
12541 tree_low_cst (DECL_VINDEX (func_decl), 0),
12542 0));
ec1e49cc 12543
6efd403b 12544 /* GNU extension: Record what type this method came from originally. */
12545 if (debug_info_level > DINFO_LEVEL_TERSE)
12546 add_AT_die_ref (die, DW_AT_containing_type,
12547 lookup_type_die (DECL_CONTEXT (func_decl)));
30ade641 12548 }
12549}
12550\f
840b696a 12551/* Add source coordinate attributes for the given decl. */
ec1e49cc 12552
840b696a 12553static void
8ec3a57b 12554add_src_coords_attributes (dw_die_ref die, tree decl)
840b696a 12555{
7bd3dcc4 12556 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
ec1e49cc 12557
69278c24 12558 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
7bd3dcc4 12559 add_AT_unsigned (die, DW_AT_decl_line, s.line);
840b696a 12560}
12561
df07c3ae 12562/* Add a DW_AT_name attribute and source coordinate attribute for the
30ade641 12563 given decl, but only if it actually has a name. */
ec1e49cc 12564
30ade641 12565static void
8ec3a57b 12566add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
30ade641 12567{
19cb6b50 12568 tree decl_name;
ec1e49cc 12569
f80d1bcd 12570 decl_name = DECL_NAME (decl);
ec1e49cc 12571 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
30ade641 12572 {
59561872 12573 add_name_attribute (die, dwarf2_name (decl, 0));
c90bf86c 12574 if (! DECL_ARTIFICIAL (decl))
12575 add_src_coords_attributes (die, decl);
2b553659 12576
59561872 12577 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4e1d939e 12578 && TREE_PUBLIC (decl)
8f80e66d 12579 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
23bf35fe 12580 && !DECL_ABSTRACT (decl)
7d022cbc 12581 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12582 && !is_fortran ())
59561872 12583 add_AT_string (die, DW_AT_MIPS_linkage_name,
12584 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
30ade641 12585 }
8d60d2bc 12586
12587#ifdef VMS_DEBUGGING_INFO
8d60d2bc 12588 /* Get the function's name, as described by its RTL. This may be different
12589 from the DECL_NAME name used in the source file. */
12590 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
7facaa35 12591 {
12592 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12593 XEXP (DECL_RTL (decl), 0));
62aedc4c 12594 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
7facaa35 12595 }
8d60d2bc 12596#endif
30ade641 12597}
12598
f80d1bcd 12599/* Push a new declaration scope. */
ec1e49cc 12600
30ade641 12601static void
8ec3a57b 12602push_decl_scope (tree scope)
30ade641 12603{
4a940e75 12604 VEC_safe_push (tree, gc, decl_scope_table, scope);
30ade641 12605}
12606
14b40abb 12607/* Pop a declaration scope. */
8c3f468d 12608
14b40abb 12609static inline void
8ec3a57b 12610pop_decl_scope (void)
14b40abb 12611{
4a940e75 12612 VEC_pop (tree, decl_scope_table);
14b40abb 12613}
12614
12615/* Return the DIE for the scope that immediately contains this type.
12616 Non-named types get global scope. Named types nested in other
12617 types get their containing scope if it's open, or global scope
12618 otherwise. All other types (i.e. function-local named types) get
12619 the current active scope. */
ec1e49cc 12620
30ade641 12621static dw_die_ref
8ec3a57b 12622scope_die_for (tree t, dw_die_ref context_die)
30ade641 12623{
19cb6b50 12624 dw_die_ref scope_die = NULL;
12625 tree containing_scope;
12626 int i;
30ade641 12627
14b40abb 12628 /* Non-types always go in the current scope. */
7bd4f6b6 12629 gcc_assert (TYPE_P (t));
14b40abb 12630
12631 containing_scope = TYPE_CONTEXT (t);
db42c2b2 12632
e89530cd 12633 /* Use the containing namespace if it was passed in (for a declaration). */
7c43cc0e 12634 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
e89530cd 12635 {
12636 if (context_die == lookup_decl_die (containing_scope))
12637 /* OK */;
12638 else
12639 containing_scope = NULL_TREE;
12640 }
7c43cc0e 12641
5ef8d04d 12642 /* Ignore function type "scopes" from the C frontend. They mean that
12643 a tagged type is local to a parmlist of a function declarator, but
12644 that isn't useful to DWARF. */
12645 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12646 containing_scope = NULL_TREE;
12647
ec1e49cc 12648 if (containing_scope == NULL_TREE)
12649 scope_die = comp_unit_die;
14b40abb 12650 else if (TYPE_P (containing_scope))
5c65b85a 12651 {
14b40abb 12652 /* For types, we can just look up the appropriate DIE. But
12653 first we check to see if we're in the middle of emitting it
12654 so we know where the new DIE should go. */
4a940e75 12655 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12656 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
5c65b85a 12657 break;
12658
12659 if (i < 0)
12660 {
7bd4f6b6 12661 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12662 || TREE_ASM_WRITTEN (containing_scope));
5c65b85a 12663
12664 /* If none of the current dies are suitable, we get file scope. */
12665 scope_die = comp_unit_die;
12666 }
12667 else
14b40abb 12668 scope_die = lookup_type_die (containing_scope);
5c65b85a 12669 }
30ade641 12670 else
14b40abb 12671 scope_die = context_die;
ec1e49cc 12672
30ade641 12673 return scope_die;
12674}
12675
8c3f468d 12676/* Returns nonzero if CONTEXT_DIE is internal to a function. */
14b40abb 12677
12678static inline int
8ec3a57b 12679local_scope_p (dw_die_ref context_die)
30ade641 12680{
14b40abb 12681 for (; context_die; context_die = context_die->die_parent)
12682 if (context_die->die_tag == DW_TAG_inlined_subroutine
12683 || context_die->die_tag == DW_TAG_subprogram)
12684 return 1;
8c3f468d 12685
14b40abb 12686 return 0;
30ade641 12687}
12688
a974aa3e 12689/* Returns nonzero if CONTEXT_DIE is a class. */
ee1cd281 12690
12691static inline int
a974aa3e 12692class_scope_p (dw_die_ref context_die)
ee1cd281 12693{
12694 return (context_die
12695 && (context_die->die_tag == DW_TAG_structure_type
03a61d93 12696 || context_die->die_tag == DW_TAG_class_type
12697 || context_die->die_tag == DW_TAG_interface_type
a974aa3e 12698 || context_die->die_tag == DW_TAG_union_type));
12699}
12700
12701/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12702 whether or not to treat a DIE in this context as a declaration. */
12703
12704static inline int
12705class_or_namespace_scope_p (dw_die_ref context_die)
12706{
12707 return (class_scope_p (context_die)
12708 || (context_die && context_die->die_tag == DW_TAG_namespace));
ee1cd281 12709}
12710
30ade641 12711/* Many forms of DIEs require a "type description" attribute. This
12712 routine locates the proper "type descriptor" die for the type given
df07c3ae 12713 by 'type', and adds a DW_AT_type attribute below the given die. */
ec1e49cc 12714
30ade641 12715static void
8ec3a57b 12716add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12717 int decl_volatile, dw_die_ref context_die)
30ade641 12718{
19cb6b50 12719 enum tree_code code = TREE_CODE (type);
12720 dw_die_ref type_die = NULL;
30ade641 12721
06f0b99c 12722 /* ??? If this type is an unnamed subrange type of an integral, floating-point
12723 or fixed-point type, use the inner type. This is because we have no
678d90bb 12724 support for unnamed types in base_type_die. This can happen if this is
12725 an Ada subrange type. Correct solution is emit a subrange type die. */
06f0b99c 12726 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
34425fdc 12727 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12728 type = TREE_TYPE (type), code = TREE_CODE (type);
12729
8c3f468d 12730 if (code == ERROR_MARK
12731 /* Handle a special case. For functions whose return type is void, we
12732 generate *no* type attribute. (Note that no object may have type
12733 `void', so this only applies to function return types). */
12734 || code == VOID_TYPE)
34425fdc 12735 return;
30ade641 12736
30ade641 12737 type_die = modified_type_die (type,
12738 decl_const || TYPE_READONLY (type),
12739 decl_volatile || TYPE_VOLATILE (type),
db42c2b2 12740 context_die);
8c3f468d 12741
30ade641 12742 if (type_die != NULL)
ec1e49cc 12743 add_AT_die_ref (object_die, DW_AT_type, type_die);
30ade641 12744}
12745
8ff30ff6 12746/* Given an object die, add the calling convention attribute for the
12747 function call type. */
12748static void
7d022cbc 12749add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
8ff30ff6 12750{
12751 enum dwarf_calling_convention value = DW_CC_normal;
12752
8458f4ca 12753 value = ((enum dwarf_calling_convention)
12754 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
7d022cbc 12755
12756 /* DWARF doesn't provide a way to identify a program's source-level
12757 entry point. DW_AT_calling_convention attributes are only meant
12758 to describe functions' calling conventions. However, lacking a
12759 better way to signal the Fortran main program, we use this for the
12760 time being, following existing custom. */
12761 if (is_fortran ()
12762 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12763 value = DW_CC_program;
8ff30ff6 12764
785a2b1d 12765 /* Only add the attribute if the backend requests it, and
12766 is not DW_CC_normal. */
12767 if (value && (value != DW_CC_normal))
8ff30ff6 12768 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12769}
12770
30ade641 12771/* Given a tree pointer to a struct, class, union, or enum type node, return
12772 a pointer to the (string) tag name for the given type, or zero if the type
12773 was declared without a tag. */
ec1e49cc 12774
1e034a40 12775static const char *
5493cb9a 12776type_tag (const_tree type)
30ade641 12777{
19cb6b50 12778 const char *name = 0;
30ade641 12779
12780 if (TYPE_NAME (type) != 0)
12781 {
19cb6b50 12782 tree t = 0;
30ade641 12783
12784 /* Find the IDENTIFIER_NODE for the type name. */
12785 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12786 t = TYPE_NAME (type);
6ed29fb8 12787
f80d1bcd 12788 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
c83a163c 12789 a TYPE_DECL node, regardless of whether or not a `typedef' was
12790 involved. */
6efd403b 12791 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12792 && ! DECL_IGNORED_P (TYPE_NAME (type)))
facb12b2 12793 {
12794 /* We want to be extra verbose. Don't call dwarf_name if
12795 DECL_NAME isn't set. The default hook for decl_printable_name
12796 doesn't like that, and in this context it's correct to return
12797 0, instead of "<anonymous>" or the like. */
12798 if (DECL_NAME (TYPE_NAME (type)))
12799 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12800 }
6ed29fb8 12801
30ade641 12802 /* Now get the name as a string, or invent one. */
facb12b2 12803 if (!name && t != 0)
6efd403b 12804 name = IDENTIFIER_POINTER (t);
30ade641 12805 }
ec1e49cc 12806
30ade641 12807 return (name == 0 || *name == '\0') ? 0 : name;
12808}
12809
12810/* Return the type associated with a data member, make a special check
12811 for bit field types. */
ec1e49cc 12812
12813static inline tree
5493cb9a 12814member_declared_type (const_tree member)
30ade641 12815{
ec1e49cc 12816 return (DECL_BIT_FIELD_TYPE (member)
8c3f468d 12817 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
30ade641 12818}
12819
dc7a29ce 12820/* Get the decl's label, as described by its RTL. This may be different
30ade641 12821 from the DECL_NAME name used in the source file. */
ec1e49cc 12822
0e93a6ac 12823#if 0
1e034a40 12824static const char *
8ec3a57b 12825decl_start_label (tree decl)
30ade641 12826{
12827 rtx x;
1e034a40 12828 const char *fnname;
8c3f468d 12829
30ade641 12830 x = DECL_RTL (decl);
7bd4f6b6 12831 gcc_assert (MEM_P (x));
ec1e49cc 12832
30ade641 12833 x = XEXP (x, 0);
7bd4f6b6 12834 gcc_assert (GET_CODE (x) == SYMBOL_REF);
ec1e49cc 12835
30ade641 12836 fnname = XSTR (x, 0);
12837 return fnname;
12838}
0e93a6ac 12839#endif
30ade641 12840\f
ad87de1e 12841/* These routines generate the internal representation of the DIE's for
30ade641 12842 the compilation unit. Debugging information is collected by walking
464217f3 12843 the declaration trees passed in from dwarf2out_decl(). */
30ade641 12844
12845static void
8ec3a57b 12846gen_array_type_die (tree type, dw_die_ref context_die)
30ade641 12847{
19cb6b50 12848 dw_die_ref scope_die = scope_die_for (type, context_die);
12849 dw_die_ref array_die;
544cd34c 12850
12851 /* GNU compilers represent multidimensional array types as sequences of one
12852 dimensional array types whose element types are themselves array types.
12853 We sometimes squish that down to a single array_type DIE with multiple
12854 subscripts in the Dwarf debugging info. The draft Dwarf specification
12855 say that we are allowed to do this kind of compression in C, because
12856 there is no difference between an array of arrays and a multidimensional
12857 array. We don't do this for Ada to remain as close as possible to the
12858 actual representation, which is especially important against the language
12859 flexibilty wrt arrays of variable size. */
12860
12861 bool collapse_nested_arrays = !is_ada ();
19cb6b50 12862 tree element_type;
cdba1d8f 12863
12864 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12865 DW_TAG_string_type doesn't have DW_AT_type attribute). */
12866 if (TYPE_STRING_FLAG (type)
12867 && TREE_CODE (type) == ARRAY_TYPE
12868 && is_fortran ()
12869 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12870 {
12871 HOST_WIDE_INT size;
12872
12873 array_die = new_die (DW_TAG_string_type, scope_die, type);
12874 add_name_attribute (array_die, type_tag (type));
12875 equate_type_number_to_die (type, array_die);
12876 size = int_size_in_bytes (type);
12877 if (size >= 0)
12878 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12879 else if (TYPE_DOMAIN (type) != NULL_TREE
12880 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12881 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12882 {
12883 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12884 dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12885
12886 size = int_size_in_bytes (TREE_TYPE (szdecl));
12887 if (loc && size > 0)
12888 {
12889 add_AT_loc (array_die, DW_AT_string_length, loc);
12890 if (size != DWARF2_ADDR_SIZE)
12891 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12892 }
12893 }
12894 return;
12895 }
12896
544cd34c 12897 /* ??? The SGI dwarf reader fails for array of array of enum types
12898 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12899 array type comes before the outer array type. We thus call gen_type_die
12900 before we new_die and must prevent nested array types collapsing for this
12901 target. */
6ed29fb8 12902
5b67860b 12903#ifdef MIPS_DEBUGGING_INFO
12904 gen_type_die (TREE_TYPE (type), context_die);
544cd34c 12905 collapse_nested_arrays = false;
5b67860b 12906#endif
12907
15cfae4e 12908 array_die = new_die (DW_TAG_array_type, scope_die, type);
634906d6 12909 add_name_attribute (array_die, type_tag (type));
12910 equate_type_number_to_die (type, array_die);
12911
12912 if (TREE_CODE (type) == VECTOR_TYPE)
12913 {
12914 /* The frontend feeds us a representation for the vector as a struct
12915 containing an array. Pull out the array type. */
12916 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12917 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12918 }
5b67860b 12919
00e7d8a5 12920 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
12921 if (is_fortran ()
12922 && TREE_CODE (type) == ARRAY_TYPE
cdba1d8f 12923 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12924 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
00e7d8a5 12925 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12926
30ade641 12927#if 0
12928 /* We default the array ordering. SDB will probably do
12929 the right things even if DW_AT_ordering is not present. It's not even
12930 an issue until we start to get into multidimensional arrays anyway. If
12931 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12932 then we'll have to put the DW_AT_ordering attribute back in. (But if
12933 and when we find out that we need to put these in, we will only do so
12934 for multidimensional arrays. */
12935 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12936#endif
12937
5b67860b 12938#ifdef MIPS_DEBUGGING_INFO
cc324702 12939 /* The SGI compilers handle arrays of unknown bound by setting
12940 AT_declaration and not emitting any subrange DIEs. */
5b67860b 12941 if (! TYPE_DOMAIN (type))
8c50ec6a 12942 add_AT_flag (array_die, DW_AT_declaration, 1);
5b67860b 12943 else
12944#endif
544cd34c 12945 add_subscript_info (array_die, type, collapse_nested_arrays);
30ade641 12946
544cd34c 12947 /* Add representation of the type of the elements of this array type and
12948 emit the corresponding DIE if we haven't done it already. */
30ade641 12949 element_type = TREE_TYPE (type);
544cd34c 12950 if (collapse_nested_arrays)
12951 while (TREE_CODE (element_type) == ARRAY_TYPE)
cdba1d8f 12952 {
12953 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12954 break;
12955 element_type = TREE_TYPE (element_type);
12956 }
12957
30ade641 12958#ifndef MIPS_DEBUGGING_INFO
30ade641 12959 gen_type_die (element_type, context_die);
5b67860b 12960#endif
30ade641 12961
12962 add_type_attribute (array_die, element_type, 0, 0, context_die);
af84796a 12963
12964 if (get_AT (array_die, DW_AT_name))
12965 add_pubtype (type, array_die);
30ade641 12966}
12967
1c79cc8c 12968static dw_loc_descr_ref
12969descr_info_loc (tree val, tree base_decl)
12970{
12971 HOST_WIDE_INT size;
12972 dw_loc_descr_ref loc, loc2;
12973 enum dwarf_location_atom op;
12974
12975 if (val == base_decl)
12976 return new_loc_descr (DW_OP_push_object_address, 0, 0);
12977
12978 switch (TREE_CODE (val))
12979 {
72dd6141 12980 CASE_CONVERT:
1c79cc8c 12981 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
6180d82a 12982 case VAR_DECL:
12983 return loc_descriptor_from_tree_1 (val, 0);
1c79cc8c 12984 case INTEGER_CST:
12985 if (host_integerp (val, 0))
12986 return int_loc_descriptor (tree_low_cst (val, 0));
12987 break;
12988 case INDIRECT_REF:
12989 size = int_size_in_bytes (TREE_TYPE (val));
12990 if (size < 0)
12991 break;
12992 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12993 if (!loc)
12994 break;
12995 if (size == DWARF2_ADDR_SIZE)
12996 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12997 else
12998 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12999 return loc;
13000 case POINTER_PLUS_EXPR:
13001 case PLUS_EXPR:
13002 if (host_integerp (TREE_OPERAND (val, 1), 1)
13003 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13004 < 16384)
13005 {
13006 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13007 if (!loc)
13008 break;
1938132c 13009 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
1c79cc8c 13010 }
13011 else
13012 {
13013 op = DW_OP_plus;
13014 do_binop:
13015 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13016 if (!loc)
13017 break;
13018 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13019 if (!loc2)
13020 break;
13021 add_loc_descr (&loc, loc2);
13022 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13023 }
13024 return loc;
13025 case MINUS_EXPR:
13026 op = DW_OP_minus;
13027 goto do_binop;
13028 case MULT_EXPR:
13029 op = DW_OP_mul;
13030 goto do_binop;
13031 case EQ_EXPR:
13032 op = DW_OP_eq;
13033 goto do_binop;
13034 case NE_EXPR:
13035 op = DW_OP_ne;
13036 goto do_binop;
13037 default:
13038 break;
13039 }
13040 return NULL;
13041}
13042
13043static void
13044add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13045 tree val, tree base_decl)
13046{
13047 dw_loc_descr_ref loc;
13048
13049 if (host_integerp (val, 0))
13050 {
13051 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13052 return;
13053 }
13054
13055 loc = descr_info_loc (val, base_decl);
13056 if (!loc)
13057 return;
13058
13059 add_AT_loc (die, attr, loc);
13060}
13061
13062/* This routine generates DIE for array with hidden descriptor, details
13063 are filled into *info by a langhook. */
13064
13065static void
13066gen_descr_array_type_die (tree type, struct array_descr_info *info,
13067 dw_die_ref context_die)
13068{
13069 dw_die_ref scope_die = scope_die_for (type, context_die);
13070 dw_die_ref array_die;
13071 int dim;
13072
13073 array_die = new_die (DW_TAG_array_type, scope_die, type);
13074 add_name_attribute (array_die, type_tag (type));
13075 equate_type_number_to_die (type, array_die);
13076
00e7d8a5 13077 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
13078 if (is_fortran ()
13079 && info->ndimensions >= 2)
13080 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13081
1c79cc8c 13082 if (info->data_location)
13083 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13084 info->base_decl);
13085 if (info->associated)
13086 add_descr_info_field (array_die, DW_AT_associated, info->associated,
13087 info->base_decl);
13088 if (info->allocated)
13089 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13090 info->base_decl);
13091
13092 for (dim = 0; dim < info->ndimensions; dim++)
13093 {
13094 dw_die_ref subrange_die
13095 = new_die (DW_TAG_subrange_type, array_die, NULL);
13096
13097 if (info->dimen[dim].lower_bound)
13098 {
13099 /* If it is the default value, omit it. */
13100 if ((is_c_family () || is_java ())
13101 && integer_zerop (info->dimen[dim].lower_bound))
13102 ;
13103 else if (is_fortran ()
13104 && integer_onep (info->dimen[dim].lower_bound))
13105 ;
13106 else
13107 add_descr_info_field (subrange_die, DW_AT_lower_bound,
13108 info->dimen[dim].lower_bound,
13109 info->base_decl);
13110 }
13111 if (info->dimen[dim].upper_bound)
13112 add_descr_info_field (subrange_die, DW_AT_upper_bound,
13113 info->dimen[dim].upper_bound,
13114 info->base_decl);
13115 if (info->dimen[dim].stride)
13116 add_descr_info_field (subrange_die, DW_AT_byte_stride,
13117 info->dimen[dim].stride,
13118 info->base_decl);
13119 }
13120
13121 gen_type_die (info->element_type, context_die);
13122 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13123
13124 if (get_AT (array_die, DW_AT_name))
13125 add_pubtype (type, array_die);
13126}
13127
cd03a192 13128#if 0
30ade641 13129static void
8ec3a57b 13130gen_entry_point_die (tree decl, dw_die_ref context_die)
30ade641 13131{
19cb6b50 13132 tree origin = decl_ultimate_origin (decl);
15cfae4e 13133 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
8c3f468d 13134
30ade641 13135 if (origin != NULL)
ec1e49cc 13136 add_abstract_origin_attribute (decl_die, origin);
30ade641 13137 else
13138 {
13139 add_name_and_src_coords_attributes (decl_die, decl);
30ade641 13140 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13141 0, 0, context_die);
13142 }
ec1e49cc 13143
30ade641 13144 if (DECL_ABSTRACT (decl))
ec1e49cc 13145 equate_decl_number_to_die (decl, decl_die);
30ade641 13146 else
ec1e49cc 13147 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
30ade641 13148}
cd03a192 13149#endif
30ade641 13150
a4617d03 13151/* Walk through the list of incomplete types again, trying once more to
13152 emit full debugging info for them. */
13153
13154static void
8ec3a57b 13155retry_incomplete_types (void)
a4617d03 13156{
52a7cc7b 13157 int i;
8c3f468d 13158
22230dd1 13159 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13160 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
a4617d03 13161}
13162
03a61d93 13163/* Determine what tag to use for a record type. */
13164
13165static enum dwarf_tag
13166record_type_tag (tree type)
13167{
13168 if (! lang_hooks.types.classify_record)
13169 return DW_TAG_structure_type;
13170
13171 switch (lang_hooks.types.classify_record (type))
13172 {
13173 case RECORD_IS_STRUCT:
13174 return DW_TAG_structure_type;
13175
13176 case RECORD_IS_CLASS:
13177 return DW_TAG_class_type;
13178
13179 case RECORD_IS_INTERFACE:
13180 return DW_TAG_interface_type;
13181
13182 default:
13183 gcc_unreachable ();
13184 }
13185}
13186
30ade641 13187/* Generate a DIE to represent an enumeration type. Note that these DIEs
13188 include all of the information about the enumeration values also. Each
6542a017 13189 enumerated type name/value is listed as a child of the enumerated type
13190 DIE. */
ec1e49cc 13191
93c7db82 13192static dw_die_ref
8ec3a57b 13193gen_enumeration_type_die (tree type, dw_die_ref context_die)
30ade641 13194{
19cb6b50 13195 dw_die_ref type_die = lookup_type_die (type);
6542a017 13196
30ade641 13197 if (type_die == NULL)
13198 {
13199 type_die = new_die (DW_TAG_enumeration_type,
15cfae4e 13200 scope_die_for (type, context_die), type);
30ade641 13201 equate_type_number_to_die (type, type_die);
13202 add_name_attribute (type_die, type_tag (type));
30ade641 13203 }
6542a017 13204 else if (! TYPE_SIZE (type))
93c7db82 13205 return type_die;
6542a017 13206 else
13207 remove_AT (type_die, DW_AT_declaration);
13208
13209 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
13210 given enum type is incomplete, do not generate the DW_AT_byte_size
13211 attribute or the DW_AT_element_list attribute. */
13212 if (TYPE_SIZE (type))
30ade641 13213 {
19cb6b50 13214 tree link;
ec1e49cc 13215
a3377a8b 13216 TREE_ASM_WRITTEN (type) = 1;
6542a017 13217 add_byte_size_attribute (type_die, type);
0dbd1c74 13218 if (TYPE_STUB_DECL (type) != NULL_TREE)
840b696a 13219 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
ec1e49cc 13220
678d90bb 13221 /* If the first reference to this type was as the return type of an
13222 inline function, then it may not have a parent. Fix this now. */
13223 if (type_die->die_parent == NULL)
13224 add_child_die (scope_die_for (type, context_die), type_die);
13225
82bb2115 13226 for (link = TYPE_VALUES (type);
6542a017 13227 link != NULL; link = TREE_CHAIN (link))
30ade641 13228 {
15cfae4e 13229 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
99f3dd6a 13230 tree value = TREE_VALUE (link);
ec1e49cc 13231
6542a017 13232 add_name_attribute (enum_die,
13233 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
5d844ba2 13234
dbc4ace1 13235 if (TREE_CODE (value) == CONST_DECL)
13236 value = DECL_INITIAL (value);
13237
78a8ed03 13238 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
99f3dd6a 13239 /* DWARF2 does not provide a way of indicating whether or
13240 not enumeration constants are signed or unsigned. GDB
13241 always assumes the values are signed, so we output all
13242 values as if they were signed. That means that
13243 enumeration constants with very large unsigned values
13244 will appear to have negative values in the debugger. */
13245 add_AT_int (enum_die, DW_AT_const_value,
13246 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
30ade641 13247 }
13248 }
6542a017 13249 else
13250 add_AT_flag (type_die, DW_AT_declaration, 1);
93c7db82 13251
af84796a 13252 if (get_AT (type_die, DW_AT_name))
13253 add_pubtype (type, type_die);
13254
93c7db82 13255 return type_die;
30ade641 13256}
13257
30ade641 13258/* Generate a DIE to represent either a real live formal parameter decl or to
13259 represent just the type of some formal parameter position in some function
13260 type.
ec1e49cc 13261
30ade641 13262 Note that this routine is a bit unusual because its argument may be a
13263 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13264 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13265 node. If it's the former then this function is being called to output a
13266 DIE to represent a formal parameter object (or some inlining thereof). If
13267 it's the latter, then this function is only being called to output a
13268 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13269 argument type of some subprogram type. */
ec1e49cc 13270
6efd403b 13271static dw_die_ref
4b5d70fd 13272gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
30ade641 13273{
4b5d70fd 13274 tree node_or_origin = node ? node : origin;
19cb6b50 13275 dw_die_ref parm_die
15cfae4e 13276 = new_die (DW_TAG_formal_parameter, context_die, node);
ec1e49cc 13277
4b5d70fd 13278 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
30ade641 13279 {
ce45a448 13280 case tcc_declaration:
4b5d70fd 13281 if (!origin)
13282 origin = decl_ultimate_origin (node);
30ade641 13283 if (origin != NULL)
6efd403b 13284 add_abstract_origin_attribute (parm_die, origin);
30ade641 13285 else
13286 {
73bcd2ba 13287 tree type = TREE_TYPE (node);
30ade641 13288 add_name_and_src_coords_attributes (parm_die, node);
73bcd2ba 13289 if (DECL_BY_REFERENCE (node))
1a3ef8f6 13290 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13291 context_die);
13292 else
13293 add_type_attribute (parm_die, type,
13294 TREE_READONLY (node),
13295 TREE_THIS_VOLATILE (node),
13296 context_die);
6ed29fb8 13297 if (DECL_ARTIFICIAL (node))
13298 add_AT_flag (parm_die, DW_AT_artificial, 1);
30ade641 13299 }
ec1e49cc 13300
4b5d70fd 13301 if (node)
13302 equate_decl_number_to_die (node, parm_die);
13303 if (! DECL_ABSTRACT (node_or_origin))
13304 add_location_or_const_value_attribute (parm_die, node_or_origin,
13305 DW_AT_location);
ec1e49cc 13306
30ade641 13307 break;
13308
ce45a448 13309 case tcc_type:
ec1e49cc 13310 /* We were called with some kind of a ..._TYPE node. */
4b5d70fd 13311 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
30ade641 13312 break;
13313
30ade641 13314 default:
7bd4f6b6 13315 gcc_unreachable ();
30ade641 13316 }
ec1e49cc 13317
6efd403b 13318 return parm_die;
30ade641 13319}
13320
13321/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13322 at the end of an (ANSI prototyped) formal parameters list. */
ec1e49cc 13323
30ade641 13324static void
8ec3a57b 13325gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
30ade641 13326{
15cfae4e 13327 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
30ade641 13328}
13329
13330/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13331 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13332 parameters as specified in some function type specification (except for
0dbc398a 13333 those which appear as part of a function *definition*). */
ec1e49cc 13334
30ade641 13335static void
8ec3a57b 13336gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
30ade641 13337{
19cb6b50 13338 tree link;
13339 tree formal_type = NULL;
13340 tree first_parm_type;
8f80e66d 13341 tree arg;
30ade641 13342
8f80e66d 13343 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13344 {
13345 arg = DECL_ARGUMENTS (function_or_method_type);
13346 function_or_method_type = TREE_TYPE (function_or_method_type);
13347 }
13348 else
13349 arg = NULL_TREE;
bc70bd5e 13350
8f80e66d 13351 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
30ade641 13352
f80d1bcd 13353 /* Make our first pass over the list of formal parameter types and output a
30ade641 13354 DW_TAG_formal_parameter DIE for each one. */
8f80e66d 13355 for (link = first_parm_type; link; )
30ade641 13356 {
19cb6b50 13357 dw_die_ref parm_die;
f80d1bcd 13358
30ade641 13359 formal_type = TREE_VALUE (link);
13360 if (formal_type == void_type_node)
13361 break;
13362
13363 /* Output a (nameless) DIE to represent the formal parameter itself. */
4b5d70fd 13364 parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
8f80e66d 13365 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13366 && link == first_parm_type)
13367 || (arg && DECL_ARTIFICIAL (arg)))
6efd403b 13368 add_AT_flag (parm_die, DW_AT_artificial, 1);
8f80e66d 13369
13370 link = TREE_CHAIN (link);
13371 if (arg)
13372 arg = TREE_CHAIN (arg);
30ade641 13373 }
13374
13375 /* If this function type has an ellipsis, add a
13376 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
13377 if (formal_type != void_type_node)
13378 gen_unspecified_parameters_die (function_or_method_type, context_die);
13379
f80d1bcd 13380 /* Make our second (and final) pass over the list of formal parameter types
30ade641 13381 and output DIEs to represent those types (as necessary). */
13382 for (link = TYPE_ARG_TYPES (function_or_method_type);
8c3f468d 13383 link && TREE_VALUE (link);
30ade641 13384 link = TREE_CHAIN (link))
8c3f468d 13385 gen_type_die (TREE_VALUE (link), context_die);
30ade641 13386}
13387
e7b3c55c 13388/* We want to generate the DIE for TYPE so that we can generate the
13389 die for MEMBER, which has been defined; we will need to refer back
13390 to the member declaration nested within TYPE. If we're trying to
13391 generate minimal debug info for TYPE, processing TYPE won't do the
13392 trick; we need to attach the member declaration by hand. */
13393
13394static void
8ec3a57b 13395gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
e7b3c55c 13396{
13397 gen_type_die (type, context_die);
13398
13399 /* If we're trying to avoid duplicate debug info, we may not have
13400 emitted the member decl for this function. Emit it now. */
13401 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13402 && ! lookup_decl_die (member))
13403 {
d4946992 13404 dw_die_ref type_die;
7bd4f6b6 13405 gcc_assert (!decl_ultimate_origin (member));
e7b3c55c 13406
13407 push_decl_scope (type);
d4946992 13408 type_die = lookup_type_die (type);
e7b3c55c 13409 if (TREE_CODE (member) == FUNCTION_DECL)
d4946992 13410 gen_subprogram_die (member, type_die);
13411 else if (TREE_CODE (member) == FIELD_DECL)
13412 {
13413 /* Ignore the nameless fields that are used to skip bits but handle
13414 C++ anonymous unions and structs. */
13415 if (DECL_NAME (member) != NULL_TREE
13416 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13417 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13418 {
13419 gen_type_die (member_declared_type (member), type_die);
13420 gen_field_die (member, type_die);
13421 }
13422 }
e7b3c55c 13423 else
4b5d70fd 13424 gen_variable_die (member, NULL_TREE, type_die);
8c3f468d 13425
e7b3c55c 13426 pop_decl_scope ();
13427 }
13428}
13429
8c3f468d 13430/* Generate the DWARF2 info for the "abstract" instance of a function which we
13431 may later generate inlined and/or out-of-line instances of. */
e7b3c55c 13432
b29760a8 13433static void
8ec3a57b 13434dwarf2out_abstract_function (tree decl)
e7b3c55c 13435{
19cb6b50 13436 dw_die_ref old_die;
14b40abb 13437 tree save_fn;
8f80e66d 13438 tree context;
13439 int was_abstract = DECL_ABSTRACT (decl);
13440
13441 /* Make sure we have the actual abstract inline, not a clone. */
13442 decl = DECL_ORIGIN (decl);
e7b3c55c 13443
bc70bd5e 13444 old_die = lookup_decl_die (decl);
2e14ce2e 13445 if (old_die && get_AT (old_die, DW_AT_inline))
e7b3c55c 13446 /* We've already generated the abstract instance. */
13447 return;
13448
8f80e66d 13449 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13450 we don't get confused by DECL_ABSTRACT. */
0c88fb4f 13451 if (debug_info_level > DINFO_LEVEL_TERSE)
13452 {
13453 context = decl_class_context (decl);
13454 if (context)
13455 gen_type_die_for_member
13456 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13457 }
bc70bd5e 13458
8f80e66d 13459 /* Pretend we've just finished compiling this function. */
14b40abb 13460 save_fn = current_function_decl;
13461 current_function_decl = decl;
87d4aa85 13462 push_cfun (DECL_STRUCT_FUNCTION (decl));
14b40abb 13463
e7b3c55c 13464 set_decl_abstract_flags (decl, 1);
13465 dwarf2out_decl (decl);
8f80e66d 13466 if (! was_abstract)
13467 set_decl_abstract_flags (decl, 0);
14b40abb 13468
13469 current_function_decl = save_fn;
87d4aa85 13470 pop_cfun ();
e7b3c55c 13471}
13472
f6e59711 13473/* Helper function of premark_used_types() which gets called through
13474 htab_traverse_resize().
13475
13476 Marks the DIE of a given type in *SLOT as perennial, so it never gets
13477 marked as unused by prune_unused_types. */
13478static int
13479premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13480{
13481 tree type;
13482 dw_die_ref die;
13483
2457c754 13484 type = (tree) *slot;
f6e59711 13485 die = lookup_type_die (type);
13486 if (die != NULL)
13487 die->die_perennial_p = 1;
13488 return 1;
13489}
13490
13491/* Mark all members of used_types_hash as perennial. */
13492static void
13493premark_used_types (void)
13494{
13495 if (cfun && cfun->used_types_hash)
13496 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13497}
13498
30ade641 13499/* Generate a DIE to represent a declared function (either file-scope or
13500 block-local). */
ec1e49cc 13501
30ade641 13502static void
8ec3a57b 13503gen_subprogram_die (tree decl, dw_die_ref context_die)
30ade641 13504{
13505 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19cb6b50 13506 tree origin = decl_ultimate_origin (decl);
13507 dw_die_ref subr_die;
19cb6b50 13508 tree fn_arg_types;
13509 tree outer_scope;
13510 dw_die_ref old_die = lookup_decl_die (decl);
13511 int declaration = (current_function_decl != decl
e89530cd 13512 || class_or_namespace_scope_p (context_die));
30ade641 13513
89c30811 13514 premark_used_types ();
f6e59711 13515
8c3f468d 13516 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13517 started to generate the abstract instance of an inline, decided to output
13518 its containing class, and proceeded to emit the declaration of the inline
13519 from the member list for the class. If so, DECLARATION takes priority;
13520 we'll get back to the abstract instance when done with the class. */
e7b3c55c 13521
0dbc398a 13522 /* The class-scope declaration DIE must be the primary DIE. */
e89530cd 13523 if (origin && declaration && class_or_namespace_scope_p (context_die))
0dbc398a 13524 {
13525 origin = NULL;
7bd4f6b6 13526 gcc_assert (!old_die);
0dbc398a 13527 }
13528
dcfa82ba 13529 /* Now that the C++ front end lazily declares artificial member fns, we
13530 might need to retrofit the declaration into its class. */
13531 if (!declaration && !origin && !old_die
13532 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13533 && !class_or_namespace_scope_p (context_die)
13534 && debug_info_level > DINFO_LEVEL_TERSE)
13535 old_die = force_decl_die (decl);
13536
30ade641 13537 if (origin != NULL)
13538 {
7bd4f6b6 13539 gcc_assert (!declaration || local_scope_p (context_die));
e7b3c55c 13540
48fdacd0 13541 /* Fixup die_parent for the abstract instance of a nested
13542 inline function. */
13543 if (old_die && old_die->die_parent == NULL)
13544 add_child_die (context_die, old_die);
13545
15cfae4e 13546 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
30ade641 13547 add_abstract_origin_attribute (subr_die, origin);
13548 }
6ed29fb8 13549 else if (old_die)
13550 {
7bd3dcc4 13551 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
69278c24 13552 struct dwarf_file_data * file_index = lookup_filename (s.file);
6efd403b 13553
f414ade2 13554 if (!get_AT_flag (old_die, DW_AT_declaration)
13555 /* We can have a normal definition following an inline one in the
13556 case of redefinition of GNU C extern inlines.
13557 It seems reasonable to use AT_specification in this case. */
2e14ce2e 13558 && !get_AT (old_die, DW_AT_inline))
c2581433 13559 {
7c0a8197 13560 /* Detect and ignore this case, where we are trying to output
13561 something we have already output. */
7bd4f6b6 13562 return;
c2581433 13563 }
752e49ca 13564
13565 /* If the definition comes from the same place as the declaration,
6efd403b 13566 maybe use the old DIE. We always want the DIE for this function
13567 that has the *_pc attributes to be under comp_unit_die so the
a7678b15 13568 debugger can find it. We also need to do this for abstract
13569 instances of inlines, since the spec requires the out-of-line copy
13570 to have the same parent. For local class methods, this doesn't
13571 apply; we just use the old DIE. */
13572 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
c90bf86c 13573 && (DECL_ARTIFICIAL (decl)
69278c24 13574 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
c90bf86c 13575 && (get_AT_unsigned (old_die, DW_AT_decl_line)
7bd3dcc4 13576 == (unsigned) s.line))))
6ed29fb8 13577 {
752e49ca 13578 subr_die = old_die;
13579
2b49746a 13580 /* Clear out the declaration attribute and the formal parameters.
8ff30ff6 13581 Do not remove all children, because it is possible that this
2b49746a 13582 declaration die was forced using force_decl_die(). In such
13583 cases die that forced declaration die (e.g. TAG_imported_module)
13584 is one of the children that we do not want to remove. */
752e49ca 13585 remove_AT (subr_die, DW_AT_declaration);
2b49746a 13586 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
752e49ca 13587 }
13588 else
13589 {
15cfae4e 13590 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
023dc493 13591 add_AT_specification (subr_die, old_die);
69278c24 13592 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13593 add_AT_file (subr_die, DW_AT_decl_file, file_index);
13594 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13595 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
6ed29fb8 13596 }
13597 }
30ade641 13598 else
13599 {
15cfae4e 13600 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
f80d1bcd 13601
6542a017 13602 if (TREE_PUBLIC (decl))
13603 add_AT_flag (subr_die, DW_AT_external, 1);
ec1e49cc 13604
30ade641 13605 add_name_and_src_coords_attributes (subr_die, decl);
43f116ae 13606 if (debug_info_level > DINFO_LEVEL_TERSE)
13607 {
8c3f468d 13608 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13609 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13610 0, 0, context_die);
43f116ae 13611 }
ec1e49cc 13612
30ade641 13613 add_pure_or_virtual_attribute (subr_die, decl);
6542a017 13614 if (DECL_ARTIFICIAL (decl))
13615 add_AT_flag (subr_die, DW_AT_artificial, 1);
8c3f468d 13616
6efd403b 13617 if (TREE_PROTECTED (decl))
13618 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13619 else if (TREE_PRIVATE (decl))
13620 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
30ade641 13621 }
cc324702 13622
6efd403b 13623 if (declaration)
13624 {
2e14ce2e 13625 if (!old_die || !get_AT (old_die, DW_AT_inline))
f414ade2 13626 {
13627 add_AT_flag (subr_die, DW_AT_declaration, 1);
13628
cb3582e7 13629 /* If this is an explicit function declaration then generate
13630 a DW_AT_explicit attribute. */
13631 if (lang_hooks.decls.function_decl_explicit_p (decl))
13632 add_AT_flag (subr_die, DW_AT_explicit, 1);
13633
f414ade2 13634 /* The first time we see a member function, it is in the context of
13635 the class to which it belongs. We make sure of this by emitting
13636 the class first. The next time is the definition, which is
8ff30ff6 13637 handled above. The two may come from the same source text.
2b49746a 13638
13639 Note that force_decl_die() forces function declaration die. It is
13640 later reused to represent definition. */
dcfa82ba 13641 equate_decl_number_to_die (decl, subr_die);
f414ade2 13642 }
6efd403b 13643 }
13644 else if (DECL_ABSTRACT (decl))
30ade641 13645 {
5bd74231 13646 if (DECL_DECLARED_INLINE_P (decl))
404ba76d 13647 {
61a9389f 13648 if (cgraph_function_possibly_inlined_p (decl))
404ba76d 13649 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13650 else
5bd74231 13651 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
404ba76d 13652 }
404ba76d 13653 else
5bd74231 13654 {
13655 if (cgraph_function_possibly_inlined_p (decl))
61a9389f 13656 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
5bd74231 13657 else
61a9389f 13658 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
5bd74231 13659 }
404ba76d 13660
1b16fc45 13661 if (DECL_DECLARED_INLINE_P (decl)
13662 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13663 add_AT_flag (subr_die, DW_AT_artificial, 1);
13664
30ade641 13665 equate_decl_number_to_die (decl, subr_die);
13666 }
13667 else if (!DECL_EXTERNAL (decl))
13668 {
89fa767a 13669 HOST_WIDE_INT cfa_fb_offset;
13670
2e14ce2e 13671 if (!old_die || !get_AT (old_die, DW_AT_inline))
ca2cef7a 13672 equate_decl_number_to_die (decl, subr_die);
ec1e49cc 13673
1897b881 13674 if (!flag_reorder_blocks_and_partition)
13675 {
13676 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13677 current_function_funcdef_no);
13678 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13679 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13680 current_function_funcdef_no);
13681 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
61a9389f 13682
1897b881 13683 add_pubname (decl, subr_die);
13684 add_arange (decl, subr_die);
13685 }
13686 else
13687 { /* Do nothing for now; maybe need to duplicate die, one for
f0b5f617 13688 hot section and one for cold section, then use the hot/cold
1897b881 13689 section begin/end labels to generate the aranges... */
13690 /*
13691 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13692 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13693 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13694 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13695
13696 add_pubname (decl, subr_die);
13697 add_arange (decl, subr_die);
13698 add_arange (decl, subr_die);
13699 */
13700 }
dc7a29ce 13701
30ade641 13702#ifdef MIPS_DEBUGGING_INFO
30ade641 13703 /* Add a reference to the FDE for this routine. */
13704 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13705#endif
13706
89fa767a 13707 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13708
12d886b8 13709 /* We define the "frame base" as the function's CFA. This is more
13710 convenient for several reasons: (1) It's stable across the prologue
13711 and epilogue, which makes it better than just a frame pointer,
13712 (2) With dwarf3, there exists a one-byte encoding that allows us
13713 to reference the .debug_frame data by proxy, but failing that,
13714 (3) We can at least reuse the code inspection and interpretation
13715 code that determines the CFA position at various points in the
13716 function. */
13717 /* ??? Use some command-line or configury switch to enable the use
13718 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
13719 consumers that understand it; fall back to "pure" dwarf2 and
13720 convert the CFA data into a location list. */
13721 {
89fa767a 13722 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12d886b8 13723 if (list->dw_loc_next)
13724 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13725 else
13726 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13727 }
13728
13729 /* Compute a displacement from the "steady-state frame pointer" to
13730 the CFA. The former is what all stack slots and argument slots
61a9389f 13731 will reference in the rtl; the later is what we've told the
12d886b8 13732 debugger about. We'll need to adjust all frame_base references
13733 by this displacement. */
89fa767a 13734 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
30ade641 13735
4ee9c684 13736 if (cfun->static_chain_decl)
678d90bb 13737 add_AT_location_description (subr_die, DW_AT_static_link,
afcf285e 13738 loc_descriptor_from_tree (cfun->static_chain_decl));
30ade641 13739 }
13740
13741 /* Now output descriptions of the arguments for this function. This gets
f80d1bcd 13742 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
30ade641 13743 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13744 `...' at the end of the formal parameter list. In order to find out if
13745 there was a trailing ellipsis or not, we must instead look at the type
13746 associated with the FUNCTION_DECL. This will be a node of type
13747 FUNCTION_TYPE. If the chain of type nodes hanging off of this
f80d1bcd 13748 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
30ade641 13749 an ellipsis at the end. */
ec1e49cc 13750
30ade641 13751 /* In the case where we are describing a mere function declaration, all we
f80d1bcd 13752 need to do here (and all we *can* do here) is to describe the *types* of
30ade641 13753 its formal parameters. */
43f116ae 13754 if (debug_info_level <= DINFO_LEVEL_TERSE)
ec1e49cc 13755 ;
cc324702 13756 else if (declaration)
8f80e66d 13757 gen_formal_types_die (decl, subr_die);
30ade641 13758 else
13759 {
2358393e 13760 /* Generate DIEs to represent all known formal parameters. */
19cb6b50 13761 tree arg_decls = DECL_ARGUMENTS (decl);
13762 tree parm;
30ade641 13763
13764 /* When generating DIEs, generate the unspecified_parameters DIE
c83a163c 13765 instead if we come across the arg "__builtin_va_alist" */
30ade641 13766 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
ec1e49cc 13767 if (TREE_CODE (parm) == PARM_DECL)
13768 {
0bc644e0 13769 if (DECL_NAME (parm)
13770 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13771 "__builtin_va_alist"))
ec1e49cc 13772 gen_unspecified_parameters_die (parm, subr_die);
13773 else
4b5d70fd 13774 gen_decl_die (parm, NULL, subr_die);
ec1e49cc 13775 }
30ade641 13776
20dd417a 13777 /* Decide whether we need an unspecified_parameters DIE at the end.
c83a163c 13778 There are 2 more cases to do this for: 1) the ansi ... declaration -
13779 this is detectable when the end of the arg list is not a
13780 void_type_node 2) an unprototyped function declaration (not a
13781 definition). This just means that we have no info about the
13782 parameters at all. */
30ade641 13783 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
ec1e49cc 13784 if (fn_arg_types != NULL)
30ade641 13785 {
139c3f48 13786 /* This is the prototyped case, check for.... */
30ade641 13787 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
ec1e49cc 13788 gen_unspecified_parameters_die (decl, subr_die);
30ade641 13789 }
ec1e49cc 13790 else if (DECL_INITIAL (decl) == NULL_TREE)
13791 gen_unspecified_parameters_die (decl, subr_die);
30ade641 13792 }
13793
13794 /* Output Dwarf info for all of the stuff within the body of the function
13795 (if it has one - it may be just a declaration). */
13796 outer_scope = DECL_INITIAL (decl);
13797
8c3f468d 13798 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13799 a function. This BLOCK actually represents the outermost binding contour
13800 for the function, i.e. the contour in which the function's formal
13801 parameters and labels get declared. Curiously, it appears that the front
13802 end doesn't actually put the PARM_DECL nodes for the current function onto
13803 the BLOCK_VARS list for this outer scope, but are strung off of the
13804 DECL_ARGUMENTS list for the function instead.
13805
13806 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13807 the LABEL_DECL nodes for the function however, and we output DWARF info
13808 for those in decls_for_scope. Just within the `outer_scope' there will be
13809 a BLOCK node representing the function's outermost pair of curly braces,
13810 and any blocks used for the base and member initializers of a C++
cb371216 13811 constructor function. */
cc324702 13812 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
a3899bb7 13813 {
4ee9c684 13814 /* Emit a DW_TAG_variable DIE for a named return value. */
13815 if (DECL_NAME (DECL_RESULT (decl)))
4b5d70fd 13816 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
4ee9c684 13817
a3899bb7 13818 current_function_has_inlines = 0;
13819 decls_for_scope (outer_scope, subr_die, 0);
ec1e49cc 13820
0680318b 13821#if 0 && defined (MIPS_DEBUGGING_INFO)
a3899bb7 13822 if (current_function_has_inlines)
13823 {
13824 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13825 if (! comp_unit_has_inlines)
13826 {
13827 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13828 comp_unit_has_inlines = 1;
13829 }
13830 }
13831#endif
13832 }
a4b48f01 13833 /* Add the calling convention attribute if requested. */
7d022cbc 13834 add_calling_convention_attribute (subr_die, decl);
a4b48f01 13835
30ade641 13836}
13837
bbc59868 13838/* Returns a hash value for X (which really is a die_struct). */
13839
13840static hashval_t
13841common_block_die_table_hash (const void *x)
13842{
13843 const_dw_die_ref d = (const_dw_die_ref) x;
13844 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13845}
13846
13847/* Return nonzero if decl_id and die_parent of die_struct X is the same
13848 as decl_id and die_parent of die_struct Y. */
13849
13850static int
13851common_block_die_table_eq (const void *x, const void *y)
13852{
13853 const_dw_die_ref d = (const_dw_die_ref) x;
13854 const_dw_die_ref e = (const_dw_die_ref) y;
13855 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13856}
13857
4b5d70fd 13858/* Generate a DIE to represent a declared data object.
13859 Either DECL or ORIGIN must be non-null. */
ec1e49cc 13860
30ade641 13861static void
4b5d70fd 13862gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
30ade641 13863{
a12691f0 13864 HOST_WIDE_INT off;
845c3089 13865 tree com_decl;
4b5d70fd 13866 tree decl_or_origin = decl ? decl : origin;
a12691f0 13867 dw_die_ref var_die;
4b5d70fd 13868 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
ebcb0478 13869 dw_die_ref origin_die;
4b5d70fd 13870 int declaration = (DECL_EXTERNAL (decl_or_origin)
211fa870 13871 /* If DECL is COMDAT and has not actually been
13872 emitted, we cannot take its address; there
13873 might end up being no definition anywhere in
13874 the program. For example, consider the C++
13875 test case:
13876
61a9389f 13877 template <class T>
13878 struct S { static const int i = 7; };
211fa870 13879
61a9389f 13880 template <class T>
13881 const int S<T>::i;
13882
13883 int f() { return S<int>::i; }
211fa870 13884
211fa870 13885 Here, S<int>::i is not DECL_EXTERNAL, but no
13886 definition is required, so the compiler will
61a9389f 13887 not emit a definition. */
4b5d70fd 13888 || (TREE_CODE (decl_or_origin) == VAR_DECL
13889 && DECL_COMDAT (decl_or_origin)
13890 && !TREE_ASM_WRITTEN (decl_or_origin))
e89530cd 13891 || class_or_namespace_scope_p (context_die));
cc324702 13892
4b5d70fd 13893 if (!origin)
13894 origin = decl_ultimate_origin (decl);
13895
13896 com_decl = fortran_common (decl_or_origin, &off);
a12691f0 13897
13898 /* Symbol in common gets emitted as a child of the common block, in the form
df4d540f 13899 of a data member. */
845c3089 13900 if (com_decl)
a12691f0 13901 {
845c3089 13902 tree field;
a12691f0 13903 dw_die_ref com_die;
bd63009e 13904 dw_loc_descr_ref loc;
bbc59868 13905 die_node com_die_arg;
a12691f0 13906
4b5d70fd 13907 var_die = lookup_decl_die (decl_or_origin);
b61ffa4f 13908 if (var_die)
bd63009e 13909 {
b61ffa4f 13910 if (get_AT (var_die, DW_AT_location) == NULL)
bd63009e 13911 {
13912 loc = loc_descriptor_from_tree (com_decl);
13913 if (loc)
13914 {
13915 if (off)
bbc59868 13916 {
13917 /* Optimize the common case. */
13918 if (loc->dw_loc_opc == DW_OP_addr
13919 && loc->dw_loc_next == NULL
13920 && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13921 == SYMBOL_REF)
13922 loc->dw_loc_oprnd1.v.val_addr
13923 = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13924 else
1938132c 13925 loc_descr_plus_const (&loc, off);
bbc59868 13926 }
b61ffa4f 13927 add_AT_loc (var_die, DW_AT_location, loc);
13928 remove_AT (var_die, DW_AT_declaration);
bd63009e 13929 }
13930 }
13931 return;
13932 }
bbc59868 13933
13934 if (common_block_die_table == NULL)
13935 common_block_die_table
13936 = htab_create_ggc (10, common_block_die_table_hash,
13937 common_block_die_table_eq, NULL);
13938
845c3089 13939 field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
bbc59868 13940 com_die_arg.decl_id = DECL_UID (com_decl);
13941 com_die_arg.die_parent = context_die;
13942 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
bd63009e 13943 loc = loc_descriptor_from_tree (com_decl);
b61ffa4f 13944 if (com_die == NULL)
df4d540f 13945 {
13946 const char *cnam
13947 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
bbc59868 13948 void **slot;
df4d540f 13949
b61ffa4f 13950 com_die = new_die (DW_TAG_common_block, context_die, decl);
13951 add_name_and_src_coords_attributes (com_die, com_decl);
df4d540f 13952 if (loc)
bd63009e 13953 {
b61ffa4f 13954 add_AT_loc (com_die, DW_AT_location, loc);
bd63009e 13955 /* Avoid sharing the same loc descriptor between
13956 DW_TAG_common_block and DW_TAG_variable. */
13957 loc = loc_descriptor_from_tree (com_decl);
13958 }
df4d540f 13959 else if (DECL_EXTERNAL (decl))
b61ffa4f 13960 add_AT_flag (com_die, DW_AT_declaration, 1);
13961 add_pubname_string (cnam, com_die); /* ??? needed? */
bbc59868 13962 com_die->decl_id = DECL_UID (com_decl);
13963 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
13964 *slot = (void *) com_die;
df4d540f 13965 }
b61ffa4f 13966 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
df4d540f 13967 {
b61ffa4f 13968 add_AT_loc (com_die, DW_AT_location, loc);
bd63009e 13969 loc = loc_descriptor_from_tree (com_decl);
b61ffa4f 13970 remove_AT (com_die, DW_AT_declaration);
df4d540f 13971 }
b61ffa4f 13972 var_die = new_die (DW_TAG_variable, com_die, decl);
13973 add_name_and_src_coords_attributes (var_die, decl);
13974 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
845c3089 13975 TREE_THIS_VOLATILE (decl), context_die);
b61ffa4f 13976 add_AT_flag (var_die, DW_AT_external, 1);
bd63009e 13977 if (loc)
13978 {
13979 if (off)
bbc59868 13980 {
13981 /* Optimize the common case. */
13982 if (loc->dw_loc_opc == DW_OP_addr
13983 && loc->dw_loc_next == NULL
13984 && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
13985 loc->dw_loc_oprnd1.v.val_addr
13986 = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13987 else
1938132c 13988 loc_descr_plus_const (&loc, off);
bbc59868 13989 }
b61ffa4f 13990 add_AT_loc (var_die, DW_AT_location, loc);
bd63009e 13991 }
13992 else if (DECL_EXTERNAL (decl))
b61ffa4f 13993 add_AT_flag (var_die, DW_AT_declaration, 1);
13994 equate_decl_number_to_die (decl, var_die);
a12691f0 13995 return;
13996 }
13997
ab977c1f 13998 /* If the compiler emitted a definition for the DECL declaration
13999 and if we already emitted a DIE for it, don't emit a second
14000 DIE for it again. */
14001 if (old_die
14002 && declaration
14003 && old_die->die_parent == context_die)
14004 return;
14005
a974aa3e 14006 /* For static data members, the declaration in the class is supposed
14007 to have DW_TAG_member tag; the specification should still be
14008 DW_TAG_variable referencing the DW_TAG_member DIE. */
14009 if (declaration && class_scope_p (context_die))
14010 var_die = new_die (DW_TAG_member, context_die, decl);
14011 else
14012 var_die = new_die (DW_TAG_variable, context_die, decl);
a12691f0 14013
ebcb0478 14014 origin_die = NULL;
30ade641 14015 if (origin != NULL)
ebcb0478 14016 origin_die = add_abstract_origin_attribute (var_die, origin);
8c3f468d 14017
5e1bdb0e 14018 /* Loop unrolling can create multiple blocks that refer to the same
8c3f468d 14019 static variable, so we must test for the DW_AT_declaration flag.
14020
14021 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
5e1bdb0e 14022 copy decls and set the DECL_ABSTRACT flag on them instead of
8c3f468d 14023 sharing them.
14024
6e395578 14025 ??? Duplicated blocks have been rewritten to use .debug_ranges.
14026
14027 ??? The declare_in_namespace support causes us to get two DIEs for one
14028 variable, both of which are declarations. We want to avoid considering
14029 one to be a specification, so we must test that this DIE is not a
14030 declaration. */
14031 else if (old_die && TREE_STATIC (decl) && ! declaration
bc70bd5e 14032 && get_AT_flag (old_die, DW_AT_declaration) == 1)
6ed29fb8 14033 {
2b553659 14034 /* This is a definition of a C++ class level static. */
023dc493 14035 add_AT_specification (var_die, old_die);
6ed29fb8 14036 if (DECL_NAME (decl))
14037 {
7bd3dcc4 14038 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
69278c24 14039 struct dwarf_file_data * file_index = lookup_filename (s.file);
ec1e49cc 14040
69278c24 14041 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14042 add_AT_file (var_die, DW_AT_decl_file, file_index);
ec1e49cc 14043
69278c24 14044 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
7bd3dcc4 14045 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
6ed29fb8 14046 }
14047 }
30ade641 14048 else
14049 {
73bcd2ba 14050 tree type = TREE_TYPE (decl);
1095d222 14051 bool private_flag_valid = true;
1a3ef8f6 14052
14053 add_name_and_src_coords_attributes (var_die, decl);
73bcd2ba 14054 if ((TREE_CODE (decl) == PARM_DECL
1095d222 14055 || TREE_CODE (decl) == RESULT_DECL
14056 || (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)))
73bcd2ba 14057 && DECL_BY_REFERENCE (decl))
1095d222 14058 {
14059 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14060 /* DECL_BY_REFERENCE uses the same bit as TREE_PRIVATE,
14061 for PARM_DECL, RESULT_DECL or non-static VAR_DECL. */
14062 private_flag_valid = false;
14063 }
1a3ef8f6 14064 else
14065 add_type_attribute (var_die, type, TREE_READONLY (decl),
14066 TREE_THIS_VOLATILE (decl), context_die);
ec1e49cc 14067
6542a017 14068 if (TREE_PUBLIC (decl))
14069 add_AT_flag (var_die, DW_AT_external, 1);
ec1e49cc 14070
6542a017 14071 if (DECL_ARTIFICIAL (decl))
14072 add_AT_flag (var_die, DW_AT_artificial, 1);
ec1e49cc 14073
6efd403b 14074 if (TREE_PROTECTED (decl))
14075 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
1095d222 14076 else if (private_flag_valid && TREE_PRIVATE (decl))
6efd403b 14077 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
30ade641 14078 }
cc324702 14079
14080 if (declaration)
14081 add_AT_flag (var_die, DW_AT_declaration, 1);
f80d1bcd 14082
4b5d70fd 14083 if (decl && (DECL_ABSTRACT (decl) || declaration))
cc324702 14084 equate_decl_number_to_die (decl, var_die);
14085
ebcb0478 14086 if (! declaration
14087 && (! DECL_ABSTRACT (decl_or_origin)
14088 /* Local static vars are shared between all clones/inlines,
14089 so emit DW_AT_location on the abstract DIE if DECL_RTL is
14090 already set. */
14091 || (TREE_CODE (decl_or_origin) == VAR_DECL
14092 && TREE_STATIC (decl_or_origin)
14093 && DECL_RTL_SET_P (decl_or_origin)))
14094 /* When abstract origin already has DW_AT_location attribute, no need
14095 to add it again. */
14096 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
30ade641 14097 {
4b5d70fd 14098 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14099 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14100 defer_location (decl_or_origin, var_die);
89f29a1b 14101 else
4b5d70fd 14102 add_location_or_const_value_attribute (var_die,
14103 decl_or_origin,
14104 DW_AT_location);
14105 add_pubname (decl_or_origin, var_die);
30ade641 14106 }
eabb26f3 14107 else
4b5d70fd 14108 tree_add_const_value_attribute (var_die, decl_or_origin);
30ade641 14109}
14110
2eb674c9 14111/* Generate a DIE to represent a named constant. */
14112
14113static void
14114gen_const_die (tree decl, dw_die_ref context_die)
14115{
14116 dw_die_ref const_die;
14117 tree type = TREE_TYPE (decl);
14118
14119 const_die = new_die (DW_TAG_constant, context_die, decl);
14120 add_name_and_src_coords_attributes (const_die, decl);
14121 add_type_attribute (const_die, type, 1, 0, context_die);
14122 if (TREE_PUBLIC (decl))
14123 add_AT_flag (const_die, DW_AT_external, 1);
14124 if (DECL_ARTIFICIAL (decl))
14125 add_AT_flag (const_die, DW_AT_artificial, 1);
14126 tree_add_const_value_attribute (const_die, decl);
14127}
14128
30ade641 14129/* Generate a DIE to represent a label identifier. */
ec1e49cc 14130
30ade641 14131static void
8ec3a57b 14132gen_label_die (tree decl, dw_die_ref context_die)
30ade641 14133{
19cb6b50 14134 tree origin = decl_ultimate_origin (decl);
15cfae4e 14135 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19cb6b50 14136 rtx insn;
30ade641 14137 char label[MAX_ARTIFICIAL_LABEL_BYTES];
ec1e49cc 14138
30ade641 14139 if (origin != NULL)
ec1e49cc 14140 add_abstract_origin_attribute (lbl_die, origin);
30ade641 14141 else
ec1e49cc 14142 add_name_and_src_coords_attributes (lbl_die, decl);
14143
30ade641 14144 if (DECL_ABSTRACT (decl))
ec1e49cc 14145 equate_decl_number_to_die (decl, lbl_die);
30ade641 14146 else
14147 {
c9f0a9eb 14148 insn = DECL_RTL_IF_SET (decl);
165b3519 14149
14150 /* Deleted labels are programmer specified labels which have been
7ef5b942 14151 eliminated because of various optimizations. We still emit them
165b3519 14152 here so that it is possible to put breakpoints on them. */
c9f0a9eb 14153 if (insn
6d7dc5b9 14154 && (LABEL_P (insn)
14155 || ((NOTE_P (insn)
ad4583d9 14156 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
30ade641 14157 {
f80d1bcd 14158 /* When optimization is enabled (via -O) some parts of the compiler
14159 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
30ade641 14160 represent source-level labels which were explicitly declared by
14161 the user. This really shouldn't be happening though, so catch
14162 it if it ever does happen. */
7bd4f6b6 14163 gcc_assert (!INSN_DELETED_P (insn));
ec1e49cc 14164
1134a028 14165 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
30ade641 14166 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14167 }
14168 }
14169}
14170
44276901 14171/* A helper function for gen_inlined_subroutine_die. Add source coordinate
14172 attributes to the DIE for a block STMT, to describe where the inlined
14173 function was called from. This is similar to add_src_coords_attributes. */
14174
14175static inline void
14176add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14177{
14178 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
44276901 14179
69278c24 14180 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
44276901 14181 add_AT_unsigned (die, DW_AT_call_line, s.line);
14182}
14183
17db73b6 14184
3ac15270 14185/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14186 Add low_pc and high_pc attributes to the DIE for a block STMT. */
ec1e49cc 14187
3ac15270 14188static inline void
14189add_high_low_attributes (tree stmt, dw_die_ref die)
30ade641 14190{
30ade641 14191 char label[MAX_ARTIFICIAL_LABEL_BYTES];
ec1e49cc 14192
3ac15270 14193 if (BLOCK_FRAGMENT_CHAIN (stmt))
30ade641 14194 {
3ac15270 14195 tree chain;
a36145ca 14196
cee43f7e 14197 if (inlined_function_outer_scope_p (stmt))
17db73b6 14198 {
14199 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14200 BLOCK_NUMBER (stmt));
14201 add_AT_lbl_id (die, DW_AT_entry_pc, label);
14202 }
14203
3ac15270 14204 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
a36145ca 14205
3ac15270 14206 chain = BLOCK_FRAGMENT_CHAIN (stmt);
14207 do
a36145ca 14208 {
3ac15270 14209 add_ranges (chain);
14210 chain = BLOCK_FRAGMENT_CHAIN (chain);
a36145ca 14211 }
3ac15270 14212 while (chain);
14213 add_ranges (NULL);
14214 }
14215 else
14216 {
14217 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14218 BLOCK_NUMBER (stmt));
14219 add_AT_lbl_id (die, DW_AT_low_pc, label);
14220 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14221 BLOCK_NUMBER (stmt));
14222 add_AT_lbl_id (die, DW_AT_high_pc, label);
30ade641 14223 }
3ac15270 14224}
14225
14226/* Generate a DIE for a lexical block. */
14227
14228static void
14229gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14230{
14231 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14232
4b5d70fd 14233 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
3ac15270 14234 add_high_low_attributes (stmt, stmt_die);
ec1e49cc 14235
cb371216 14236 decls_for_scope (stmt, stmt_die, depth);
30ade641 14237}
14238
14239/* Generate a DIE for an inlined subprogram. */
ec1e49cc 14240
30ade641 14241static void
8ec3a57b 14242gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
30ade641 14243{
b1682481 14244 tree decl = block_ultimate_origin (stmt);
14245
14246 /* Emit info for the abstract instance first, if we haven't yet. We
14247 must emit this even if the block is abstract, otherwise when we
14248 emit the block below (or elsewhere), we may end up trying to emit
14249 a die whose origin die hasn't been emitted, and crashing. */
14250 dwarf2out_abstract_function (decl);
14251
ec1e49cc 14252 if (! BLOCK_ABSTRACT (stmt))
30ade641 14253 {
19cb6b50 14254 dw_die_ref subr_die
15cfae4e 14255 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
ec1e49cc 14256
db42c2b2 14257 add_abstract_origin_attribute (subr_die, decl);
4b5d70fd 14258 if (TREE_ASM_WRITTEN (stmt))
14259 add_high_low_attributes (stmt, subr_die);
44276901 14260 add_call_src_coords_attributes (stmt, subr_die);
3ac15270 14261
cb371216 14262 decls_for_scope (stmt, subr_die, depth);
a3899bb7 14263 current_function_has_inlines = 1;
30ade641 14264 }
6e1e0aa6 14265 else
14266 /* We may get here if we're the outer block of function A that was
14267 inlined into function B that was inlined into function C. When
14268 generating debugging info for C, dwarf2out_abstract_function(B)
14269 would mark all inlined blocks as abstract, including this one.
14270 So, we wouldn't (and shouldn't) expect labels to be generated
14271 for this one. Instead, just emit debugging info for
14272 declarations within the block. This is particularly important
14273 in the case of initializers of arguments passed from B to us:
14274 if they're statement expressions containing declarations, we
14275 wouldn't generate dies for their abstract variables, and then,
14276 when generating dies for the real variables, we'd die (pun
14277 intended :-) */
14278 gen_lexical_block_die (stmt, context_die, depth);
30ade641 14279}
14280
14281/* Generate a DIE for a field in a record, or structure. */
ec1e49cc 14282
30ade641 14283static void
8ec3a57b 14284gen_field_die (tree decl, dw_die_ref context_die)
30ade641 14285{
443a33a3 14286 dw_die_ref decl_die;
ec1e49cc 14287
443a33a3 14288 if (TREE_TYPE (decl) == error_mark_node)
14289 return;
8ec3a57b 14290
443a33a3 14291 decl_die = new_die (DW_TAG_member, context_die, decl);
30ade641 14292 add_name_and_src_coords_attributes (decl_die, decl);
30ade641 14293 add_type_attribute (decl_die, member_declared_type (decl),
14294 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14295 context_die);
ec1e49cc 14296
30ade641 14297 if (DECL_BIT_FIELD_TYPE (decl))
14298 {
14299 add_byte_size_attribute (decl_die, decl);
14300 add_bit_size_attribute (decl_die, decl);
14301 add_bit_offset_attribute (decl_die, decl);
14302 }
ec1e49cc 14303
6efd403b 14304 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14305 add_data_member_location_attribute (decl_die, decl);
ec1e49cc 14306
6542a017 14307 if (DECL_ARTIFICIAL (decl))
14308 add_AT_flag (decl_die, DW_AT_artificial, 1);
ec1e49cc 14309
6efd403b 14310 if (TREE_PROTECTED (decl))
14311 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14312 else if (TREE_PRIVATE (decl))
14313 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
243f8437 14314
14315 /* Equate decl number to die, so that we can look up this decl later on. */
14316 equate_decl_number_to_die (decl, decl_die);
30ade641 14317}
14318
db42c2b2 14319#if 0
14320/* Don't generate either pointer_type DIEs or reference_type DIEs here.
14321 Use modified_type_die instead.
30ade641 14322 We keep this code here just in case these types of DIEs may be needed to
14323 represent certain things in other languages (e.g. Pascal) someday. */
8c3f468d 14324
30ade641 14325static void
8ec3a57b 14326gen_pointer_type_die (tree type, dw_die_ref context_die)
30ade641 14327{
19cb6b50 14328 dw_die_ref ptr_die
15cfae4e 14329 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
ec1e49cc 14330
30ade641 14331 equate_type_number_to_die (type, ptr_die);
30ade641 14332 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
db42c2b2 14333 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
30ade641 14334}
14335
db42c2b2 14336/* Don't generate either pointer_type DIEs or reference_type DIEs here.
14337 Use modified_type_die instead.
30ade641 14338 We keep this code here just in case these types of DIEs may be needed to
14339 represent certain things in other languages (e.g. Pascal) someday. */
8c3f468d 14340
30ade641 14341static void
8ec3a57b 14342gen_reference_type_die (tree type, dw_die_ref context_die)
30ade641 14343{
19cb6b50 14344 dw_die_ref ref_die
15cfae4e 14345 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
ec1e49cc 14346
30ade641 14347 equate_type_number_to_die (type, ref_die);
30ade641 14348 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
db42c2b2 14349 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
30ade641 14350}
db42c2b2 14351#endif
30ade641 14352
14353/* Generate a DIE for a pointer to a member type. */
8c3f468d 14354
30ade641 14355static void
8ec3a57b 14356gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
30ade641 14357{
19cb6b50 14358 dw_die_ref ptr_die
15cfae4e 14359 = new_die (DW_TAG_ptr_to_member_type,
14360 scope_die_for (type, context_die), type);
ec1e49cc 14361
30ade641 14362 equate_type_number_to_die (type, ptr_die);
30ade641 14363 add_AT_die_ref (ptr_die, DW_AT_containing_type,
6ed29fb8 14364 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
30ade641 14365 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14366}
14367
14368/* Generate the DIE for the compilation unit. */
ec1e49cc 14369
c90bf86c 14370static dw_die_ref
8ec3a57b 14371gen_compile_unit_die (const char *filename)
30ade641 14372{
19cb6b50 14373 dw_die_ref die;
30ade641 14374 char producer[250];
d19bd1f0 14375 const char *language_string = lang_hooks.name;
c90bf86c 14376 int language;
30ade641 14377
15cfae4e 14378 die = new_die (DW_TAG_compile_unit, NULL, NULL);
6ed29fb8 14379
ff279357 14380 if (filename)
14381 {
14382 add_name_attribute (die, filename);
6d042e21 14383 /* Don't add cwd for <built-in>. */
974a92fe 14384 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
ff279357 14385 add_comp_dir_attribute (die);
14386 }
30ade641 14387
14388 sprintf (producer, "%s %s", language_string, version_string);
14389
14390#ifdef MIPS_DEBUGGING_INFO
14391 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14392 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14393 not appear in the producer string, the debugger reaches the conclusion
14394 that the object file is stripped and has no debugging information.
14395 To get the MIPS/SGI debugger to believe that there is debugging
14396 information in the object file, we add a -g to the producer string. */
43f116ae 14397 if (debug_info_level > DINFO_LEVEL_TERSE)
14398 strcat (producer, " -g");
30ade641 14399#endif
14400
c90bf86c 14401 add_AT_string (die, DW_AT_producer, producer);
5b67860b 14402
30ade641 14403 if (strcmp (language_string, "GNU C++") == 0)
c90bf86c 14404 language = DW_LANG_C_plus_plus;
30ade641 14405 else if (strcmp (language_string, "GNU Ada") == 0)
7f2ad96e 14406 language = DW_LANG_Ada95;
5b67860b 14407 else if (strcmp (language_string, "GNU F77") == 0)
c90bf86c 14408 language = DW_LANG_Fortran77;
00c6e780 14409 else if (strcmp (language_string, "GNU Fortran") == 0)
4ee9c684 14410 language = DW_LANG_Fortran95;
063295fb 14411 else if (strcmp (language_string, "GNU Pascal") == 0)
c90bf86c 14412 language = DW_LANG_Pascal83;
af4d39d8 14413 else if (strcmp (language_string, "GNU Java") == 0)
14414 language = DW_LANG_Java;
bda642f9 14415 else if (strcmp (language_string, "GNU Objective-C") == 0)
14416 language = DW_LANG_ObjC;
14417 else if (strcmp (language_string, "GNU Objective-C++") == 0)
14418 language = DW_LANG_ObjC_plus_plus;
30ade641 14419 else
c90bf86c 14420 language = DW_LANG_C89;
5b67860b 14421
c90bf86c 14422 add_AT_unsigned (die, DW_AT_language, language);
c90bf86c 14423 return die;
30ade641 14424}
14425
404ba76d 14426/* Generate the DIE for a base class. */
ec1e49cc 14427
404ba76d 14428static void
8ec3a57b 14429gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
404ba76d 14430{
15cfae4e 14431 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
ec1e49cc 14432
404ba76d 14433 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14434 add_data_member_location_attribute (die, binfo);
ec1e49cc 14435
57c28194 14436 if (BINFO_VIRTUAL_P (binfo))
404ba76d 14437 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
8c3f468d 14438
95f3173a 14439 if (access == access_public_node)
404ba76d 14440 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
95f3173a 14441 else if (access == access_protected_node)
404ba76d 14442 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14443}
14444
ad87de1e 14445/* Generate a DIE for a class member. */
ec1e49cc 14446
30ade641 14447static void
8ec3a57b 14448gen_member_die (tree type, dw_die_ref context_die)
30ade641 14449{
19cb6b50 14450 tree member;
95f3173a 14451 tree binfo = TYPE_BINFO (type);
e7b3c55c 14452 dw_die_ref child;
ec1e49cc 14453
30ade641 14454 /* If this is not an incomplete type, output descriptions of each of its
14455 members. Note that as we output the DIEs necessary to represent the
14456 members of this record or union type, we will also be trying to output
14457 DIEs to represent the *types* of those members. However the `type'
f80d1bcd 14458 function (above) will specifically avoid generating type DIEs for member
4a82352a 14459 types *within* the list of member DIEs for this (containing) type except
30ade641 14460 for those types (of members) which are explicitly marked as also being
14461 members of this (containing) type themselves. The g++ front- end can
8c3f468d 14462 force any given type to be treated as a member of some other (containing)
14463 type by setting the TYPE_CONTEXT of the given (member) type to point to
14464 the TREE node representing the appropriate (containing) type. */
30ade641 14465
404ba76d 14466 /* First output info about the base classes. */
f6cc6a08 14467 if (binfo)
30ade641 14468 {
046bfc77 14469 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19cb6b50 14470 int i;
f6cc6a08 14471 tree base;
404ba76d 14472
f6cc6a08 14473 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14474 gen_inheritance_die (base,
db77fe17 14475 (accesses ? VEC_index (tree, accesses, i)
95f3173a 14476 : access_public_node), context_die);
30ade641 14477 }
14478
404ba76d 14479 /* Now output info about the data members and type members. */
14480 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
e7b3c55c 14481 {
14482 /* If we thought we were generating minimal debug info for TYPE
14483 and then changed our minds, some of the member declarations
14484 may have already been defined. Don't define them again, but
14485 do put them in the right order. */
14486
14487 child = lookup_decl_die (member);
14488 if (child)
14489 splice_child_die (context_die, child);
14490 else
4b5d70fd 14491 gen_decl_die (member, NULL, context_die);
e7b3c55c 14492 }
404ba76d 14493
30ade641 14494 /* Now output info about the function members (if any). */
404ba76d 14495 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
e7b3c55c 14496 {
8f80e66d 14497 /* Don't include clones in the member list. */
14498 if (DECL_ABSTRACT_ORIGIN (member))
14499 continue;
14500
e7b3c55c 14501 child = lookup_decl_die (member);
14502 if (child)
14503 splice_child_die (context_die, child);
14504 else
4b5d70fd 14505 gen_decl_die (member, NULL, context_die);
e7b3c55c 14506 }
30ade641 14507}
14508
e7b3c55c 14509/* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
14510 is set, we pretend that the type was never defined, so we only get the
14511 member DIEs needed by later specification DIEs. */
ec1e49cc 14512
30ade641 14513static void
0e4744ac 14514gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14515 enum debug_info_usage usage)
30ade641 14516{
19cb6b50 14517 dw_die_ref type_die = lookup_type_die (type);
14518 dw_die_ref scope_die = 0;
14519 int nested = 0;
e7b3c55c 14520 int complete = (TYPE_SIZE (type)
87ccbd32 14521 && (! TYPE_STUB_DECL (type)
14522 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
e89530cd 14523 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
0e4744ac 14524 complete = complete && should_emit_struct_debug (type, usage);
6542a017 14525
e7b3c55c 14526 if (type_die && ! complete)
6542a017 14527 return;
a3377a8b 14528
ec1e49cc 14529 if (TYPE_CONTEXT (type) != NULL_TREE
e89530cd 14530 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14531 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
a3377a8b 14532 nested = 1;
14533
6efd403b 14534 scope_die = scope_die_for (type, context_die);
a3377a8b 14535
14536 if (! type_die || (nested && scope_die == comp_unit_die))
6542a017 14537 /* First occurrence of type or toplevel definition of nested class. */
30ade641 14538 {
19cb6b50 14539 dw_die_ref old_die = type_die;
ec1e49cc 14540
30ade641 14541 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
03a61d93 14542 ? record_type_tag (type) : DW_TAG_union_type,
15cfae4e 14543 scope_die, type);
30ade641 14544 equate_type_number_to_die (type, type_die);
6542a017 14545 if (old_die)
023dc493 14546 add_AT_specification (type_die, old_die);
2dfaa9ec 14547 else
14548 add_name_attribute (type_die, type_tag (type));
30ade641 14549 }
752e49ca 14550 else
6542a017 14551 remove_AT (type_die, DW_AT_declaration);
30ade641 14552
14553 /* If this type has been completed, then give it a byte_size attribute and
14554 then give a list of members. */
e89530cd 14555 if (complete && !ns_decl)
30ade641 14556 {
f80d1bcd 14557 /* Prevent infinite recursion in cases where the type of some member of
c83a163c 14558 this type is expressed in terms of this type itself. */
30ade641 14559 TREE_ASM_WRITTEN (type) = 1;
6542a017 14560 add_byte_size_attribute (type_die, type);
0dbd1c74 14561 if (TYPE_STUB_DECL (type) != NULL_TREE)
840b696a 14562 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
ec1e49cc 14563
678d90bb 14564 /* If the first reference to this type was as the return type of an
14565 inline function, then it may not have a parent. Fix this now. */
14566 if (type_die->die_parent == NULL)
14567 add_child_die (scope_die, type_die);
14568
6542a017 14569 push_decl_scope (type);
14570 gen_member_die (type, type_die);
14571 pop_decl_scope ();
ec1e49cc 14572
6efd403b 14573 /* GNU extension: Record what type our vtable lives in. */
14574 if (TYPE_VFIELD (type))
14575 {
14576 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
ec1e49cc 14577
ad5808e7 14578 gen_type_die (vtype, context_die);
14579 add_AT_die_ref (type_die, DW_AT_containing_type,
14580 lookup_type_die (vtype));
6efd403b 14581 }
30ade641 14582 }
752e49ca 14583 else
a4617d03 14584 {
14585 add_AT_flag (type_die, DW_AT_declaration, 1);
a41e1595 14586
ee1cd281 14587 /* We don't need to do this for function-local types. */
cfd66c04 14588 if (TYPE_STUB_DECL (type)
14589 && ! decl_function_context (TYPE_STUB_DECL (type)))
22230dd1 14590 VEC_safe_push (tree, gc, incomplete_types, type);
a4617d03 14591 }
af84796a 14592
14593 if (get_AT (type_die, DW_AT_name))
14594 add_pubtype (type, type_die);
30ade641 14595}
14596
14597/* Generate a DIE for a subroutine _type_. */
ec1e49cc 14598
30ade641 14599static void
8ec3a57b 14600gen_subroutine_type_die (tree type, dw_die_ref context_die)
30ade641 14601{
19cb6b50 14602 tree return_type = TREE_TYPE (type);
14603 dw_die_ref subr_die
15cfae4e 14604 = new_die (DW_TAG_subroutine_type,
14605 scope_die_for (type, context_die), type);
ec1e49cc 14606
30ade641 14607 equate_type_number_to_die (type, subr_die);
14608 add_prototyped_attribute (subr_die, type);
30ade641 14609 add_type_attribute (subr_die, return_type, 0, 0, context_die);
6efd403b 14610 gen_formal_types_die (type, subr_die);
af84796a 14611
14612 if (get_AT (subr_die, DW_AT_name))
14613 add_pubtype (type, subr_die);
30ade641 14614}
14615
2358393e 14616/* Generate a DIE for a type definition. */
ec1e49cc 14617
30ade641 14618static void
8ec3a57b 14619gen_typedef_die (tree decl, dw_die_ref context_die)
30ade641 14620{
19cb6b50 14621 dw_die_ref type_die;
14622 tree origin;
6efd403b 14623
14624 if (TREE_ASM_WRITTEN (decl))
14625 return;
6efd403b 14626
8c3f468d 14627 TREE_ASM_WRITTEN (decl) = 1;
15cfae4e 14628 type_die = new_die (DW_TAG_typedef, context_die, decl);
6efd403b 14629 origin = decl_ultimate_origin (decl);
30ade641 14630 if (origin != NULL)
6efd403b 14631 add_abstract_origin_attribute (type_die, origin);
30ade641 14632 else
14633 {
19cb6b50 14634 tree type;
8c3f468d 14635
30ade641 14636 add_name_and_src_coords_attributes (type_die, decl);
6efd403b 14637 if (DECL_ORIGINAL_TYPE (decl))
14638 {
14639 type = DECL_ORIGINAL_TYPE (decl);
522649bb 14640
7bd4f6b6 14641 gcc_assert (type != TREE_TYPE (decl));
14642 equate_type_number_to_die (TREE_TYPE (decl), type_die);
6efd403b 14643 }
14644 else
14645 type = TREE_TYPE (decl);
8c3f468d 14646
6efd403b 14647 add_type_attribute (type_die, type, TREE_READONLY (decl),
14648 TREE_THIS_VOLATILE (decl), context_die);
30ade641 14649 }
ec1e49cc 14650
30ade641 14651 if (DECL_ABSTRACT (decl))
6efd403b 14652 equate_decl_number_to_die (decl, type_die);
af84796a 14653
14654 if (get_AT (type_die, DW_AT_name))
14655 add_pubtype (decl, type_die);
30ade641 14656}
14657
14658/* Generate a type description DIE. */
ec1e49cc 14659
30ade641 14660static void
0e4744ac 14661gen_type_die_with_usage (tree type, dw_die_ref context_die,
14662 enum debug_info_usage usage)
30ade641 14663{
5c65b85a 14664 int need_pop;
1c79cc8c 14665 struct array_descr_info info;
5c65b85a 14666
ec1e49cc 14667 if (type == NULL_TREE || type == error_mark_node)
14668 return;
30ade641 14669
6efd403b 14670 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14671 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
f80d1bcd 14672 {
dc346c40 14673 if (TREE_ASM_WRITTEN (type))
14674 return;
14675
637d3308 14676 /* Prevent broken recursion; we can't hand off to the same type. */
7bd4f6b6 14677 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
637d3308 14678
6efd403b 14679 TREE_ASM_WRITTEN (type) = 1;
4b5d70fd 14680 gen_decl_die (TYPE_NAME (type), NULL, context_die);
6efd403b 14681 return;
14682 }
14683
1c79cc8c 14684 /* If this is an array type with hidden descriptor, handle it first. */
14685 if (!TREE_ASM_WRITTEN (type)
14686 && lang_hooks.types.get_array_descr_info
14687 && lang_hooks.types.get_array_descr_info (type, &info))
14688 {
14689 gen_descr_array_type_die (type, &info, context_die);
14690 TREE_ASM_WRITTEN (type) = 1;
14691 return;
14692 }
14693
dc346c40 14694 /* We are going to output a DIE to represent the unqualified version
14695 of this type (i.e. without any const or volatile qualifiers) so
14696 get the main variant (i.e. the unqualified version) of this type
14697 now. (Vectors are special because the debugging info is in the
14698 cloned type itself). */
14699 if (TREE_CODE (type) != VECTOR_TYPE)
14700 type = type_main_variant (type);
14701
14702 if (TREE_ASM_WRITTEN (type))
14703 return;
14704
30ade641 14705 switch (TREE_CODE (type))
14706 {
14707 case ERROR_MARK:
14708 break;
14709
14710 case POINTER_TYPE:
14711 case REFERENCE_TYPE:
ad87de1e 14712 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
14713 ensures that the gen_type_die recursion will terminate even if the
14714 type is recursive. Recursive types are possible in Ada. */
14715 /* ??? We could perhaps do this for all types before the switch
14716 statement. */
14717 TREE_ASM_WRITTEN (type) = 1;
14718
30ade641 14719 /* For these types, all that is required is that we output a DIE (or a
c83a163c 14720 set of DIEs) to represent the "basis" type. */
0e4744ac 14721 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14722 DINFO_USAGE_IND_USE);
30ade641 14723 break;
14724
14725 case OFFSET_TYPE:
f80d1bcd 14726 /* This code is used for C++ pointer-to-data-member types.
ec1e49cc 14727 Output a description of the relevant class type. */
0e4744ac 14728 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14729 DINFO_USAGE_IND_USE);
ec1e49cc 14730
30ade641 14731 /* Output a description of the type of the object pointed to. */
0e4744ac 14732 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14733 DINFO_USAGE_IND_USE);
ec1e49cc 14734
30ade641 14735 /* Now output a DIE to represent this pointer-to-data-member type
c83a163c 14736 itself. */
30ade641 14737 gen_ptr_to_mbr_type_die (type, context_die);
14738 break;
14739
30ade641 14740 case FUNCTION_TYPE:
14741 /* Force out return type (in case it wasn't forced out already). */
0e4744ac 14742 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14743 DINFO_USAGE_DIR_USE);
30ade641 14744 gen_subroutine_type_die (type, context_die);
14745 break;
14746
14747 case METHOD_TYPE:
14748 /* Force out return type (in case it wasn't forced out already). */
0e4744ac 14749 gen_type_die_with_usage (TREE_TYPE (type), context_die,
14750 DINFO_USAGE_DIR_USE);
30ade641 14751 gen_subroutine_type_die (type, context_die);
14752 break;
14753
14754 case ARRAY_TYPE:
63bf54cf 14755 gen_array_type_die (type, context_die);
30ade641 14756 break;
14757
e2ea7e3a 14758 case VECTOR_TYPE:
634906d6 14759 gen_array_type_die (type, context_die);
e2ea7e3a 14760 break;
14761
30ade641 14762 case ENUMERAL_TYPE:
14763 case RECORD_TYPE:
14764 case UNION_TYPE:
14765 case QUAL_UNION_TYPE:
8c3f468d 14766 /* If this is a nested type whose containing class hasn't been written
c83a163c 14767 out yet, writing it out will cover this one, too. This does not apply
14768 to instantiations of member class templates; they need to be added to
14769 the containing class as they are generated. FIXME: This hurts the
14770 idea of combining type decls from multiple TUs, since we can't predict
14771 what set of template instantiations we'll get. */
a3377a8b 14772 if (TYPE_CONTEXT (type)
5ef8d04d 14773 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
a3377a8b 14774 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
6efd403b 14775 {
0e4744ac 14776 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
6efd403b 14777
5c65b85a 14778 if (TREE_ASM_WRITTEN (type))
6efd403b 14779 return;
14780
14781 /* If that failed, attach ourselves to the stub. */
14782 push_decl_scope (TYPE_CONTEXT (type));
14783 context_die = lookup_type_die (TYPE_CONTEXT (type));
5c65b85a 14784 need_pop = 1;
6efd403b 14785 }
5c65b85a 14786 else
e89530cd 14787 {
df4d540f 14788 context_die = declare_in_namespace (type, context_die);
e89530cd 14789 need_pop = 0;
14790 }
6efd403b 14791
14792 if (TREE_CODE (type) == ENUMERAL_TYPE)
3d9b511b 14793 {
14794 /* This might have been written out by the call to
14795 declare_in_namespace. */
14796 if (!TREE_ASM_WRITTEN (type))
14797 gen_enumeration_type_die (type, context_die);
14798 }
30ade641 14799 else
0e4744ac 14800 gen_struct_or_union_type_die (type, context_die, usage);
752e49ca 14801
5c65b85a 14802 if (need_pop)
6efd403b 14803 pop_decl_scope ();
14804
752e49ca 14805 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
a3377a8b 14806 it up if it is ever completed. gen_*_type_die will set it for us
14807 when appropriate. */
14808 return;
30ade641 14809
14810 case VOID_TYPE:
14811 case INTEGER_TYPE:
14812 case REAL_TYPE:
06f0b99c 14813 case FIXED_POINT_TYPE:
30ade641 14814 case COMPLEX_TYPE:
14815 case BOOLEAN_TYPE:
30ade641 14816 /* No DIEs needed for fundamental types. */
14817 break;
14818
14819 case LANG_TYPE:
14820 /* No Dwarf representation currently defined. */
14821 break;
14822
14823 default:
7bd4f6b6 14824 gcc_unreachable ();
30ade641 14825 }
14826
14827 TREE_ASM_WRITTEN (type) = 1;
14828}
14829
0e4744ac 14830static void
14831gen_type_die (tree type, dw_die_ref context_die)
14832{
14833 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14834}
14835
30ade641 14836/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14837 things which are local to the given block. */
ec1e49cc 14838
30ade641 14839static void
8ec3a57b 14840gen_block_die (tree stmt, dw_die_ref context_die, int depth)
30ade641 14841{
19cb6b50 14842 int must_output_die = 0;
cee43f7e 14843 bool inlined_func;
30ade641 14844
7c0a8197 14845 /* Ignore blocks that are NULL. */
14846 if (stmt == NULL_TREE)
ec1e49cc 14847 return;
30ade641 14848
cee43f7e 14849 inlined_func = inlined_function_outer_scope_p (stmt);
14850
a36145ca 14851 /* If the block is one fragment of a non-contiguous block, do not
14852 process the variables, since they will have been done by the
14853 origin block. Do process subblocks. */
14854 if (BLOCK_FRAGMENT_ORIGIN (stmt))
14855 {
14856 tree sub;
14857
8c3f468d 14858 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
a36145ca 14859 gen_block_die (sub, context_die, depth + 1);
8c3f468d 14860
a36145ca 14861 return;
14862 }
14863
30ade641 14864 /* Determine if we need to output any Dwarf DIEs at all to represent this
14865 block. */
cee43f7e 14866 if (inlined_func)
ec1e49cc 14867 /* The outer scopes for inlinings *must* always be represented. We
14868 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
14869 must_output_die = 1;
30ade641 14870 else
14871 {
cee43f7e 14872 /* Determine if this block directly contains any "significant"
14873 local declarations which we will need to output DIEs for. */
14874 if (debug_info_level > DINFO_LEVEL_TERSE)
14875 /* We are not in terse mode so *any* local declaration counts
14876 as being a "significant" one. */
4b5d70fd 14877 must_output_die = ((BLOCK_VARS (stmt) != NULL
14878 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
cee43f7e 14879 && (TREE_USED (stmt)
14880 || TREE_ASM_WRITTEN (stmt)
14881 || BLOCK_ABSTRACT (stmt)));
4b5d70fd 14882 else if ((TREE_USED (stmt)
14883 || TREE_ASM_WRITTEN (stmt)
14884 || BLOCK_ABSTRACT (stmt))
14885 && !dwarf2out_ignore_block (stmt))
14886 must_output_die = 1;
30ade641 14887 }
14888
14889 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14890 DIE for any block which contains no significant local declarations at
14891 all. Rather, in such cases we just call `decls_for_scope' so that any
14892 needed Dwarf info for any sub-blocks will get properly generated. Note
14893 that in terse mode, our definition of what constitutes a "significant"
14894 local declaration gets restricted to include only inlined function
14895 instances and local (nested) function definitions. */
14896 if (must_output_die)
14897 {
cee43f7e 14898 if (inlined_func)
ec1e49cc 14899 gen_inlined_subroutine_die (stmt, context_die, depth);
30ade641 14900 else
ec1e49cc 14901 gen_lexical_block_die (stmt, context_die, depth);
30ade641 14902 }
14903 else
cb371216 14904 decls_for_scope (stmt, context_die, depth);
30ade641 14905}
14906
4b5d70fd 14907/* Process variable DECL (or variable with origin ORIGIN) within
14908 block STMT and add it to CONTEXT_DIE. */
14909static void
14910process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
14911{
14912 dw_die_ref die;
14913 tree decl_or_origin = decl ? decl : origin;
14914 tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
14915
14916 if (ultimate_origin)
14917 origin = ultimate_origin;
14918
14919 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
14920 die = lookup_decl_die (decl_or_origin);
14921 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
14922 && TYPE_DECL_IS_STUB (decl_or_origin))
14923 die = lookup_type_die (TREE_TYPE (decl_or_origin));
14924 else
14925 die = NULL;
14926
14927 if (die != NULL && die->die_parent == NULL)
14928 add_child_die (context_die, die);
14929 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
14930 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
14931 stmt, context_die);
14932 else
14933 gen_decl_die (decl, origin, context_die);
14934}
14935
30ade641 14936/* Generate all of the decls declared within a given scope and (recursively)
9e042f31 14937 all of its sub-blocks. */
ec1e49cc 14938
30ade641 14939static void
8ec3a57b 14940decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
30ade641 14941{
19cb6b50 14942 tree decl;
4b5d70fd 14943 unsigned int i;
19cb6b50 14944 tree subblocks;
ec1e49cc 14945
7c0a8197 14946 /* Ignore NULL blocks. */
14947 if (stmt == NULL_TREE)
ec1e49cc 14948 return;
14949
4b5d70fd 14950 /* Output the DIEs to represent all of the data objects and typedefs
14951 declared directly within this block but not within any nested
14952 sub-blocks. Also, nested function and tag DIEs have been
14953 generated with a parent of NULL; fix that up now. */
14954 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14955 process_scope_var (stmt, decl, NULL_TREE, context_die);
14956 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
14957 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
14958 context_die);
30ade641 14959
e883780d 14960 /* If we're at -g1, we're not interested in subblocks. */
14961 if (debug_info_level <= DINFO_LEVEL_TERSE)
14962 return;
14963
30ade641 14964 /* Output the DIEs to represent all sub-blocks (and the items declared
14965 therein) of this block. */
14966 for (subblocks = BLOCK_SUBBLOCKS (stmt);
14967 subblocks != NULL;
14968 subblocks = BLOCK_CHAIN (subblocks))
ec1e49cc 14969 gen_block_die (subblocks, context_die, depth + 1);
30ade641 14970}
14971
6efd403b 14972/* Is this a typedef we can avoid emitting? */
ec1e49cc 14973
14974static inline int
5493cb9a 14975is_redundant_typedef (const_tree decl)
6efd403b 14976{
14977 if (TYPE_DECL_IS_STUB (decl))
14978 return 1;
ec1e49cc 14979
6efd403b 14980 if (DECL_ARTIFICIAL (decl)
14981 && DECL_CONTEXT (decl)
14982 && is_tagged_type (DECL_CONTEXT (decl))
14983 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14984 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14985 /* Also ignore the artificial member typedef for the class name. */
14986 return 1;
ec1e49cc 14987
6efd403b 14988 return 0;
14989}
14990
01d2a17d 14991/* Returns the DIE for a context. */
14992
14993static inline dw_die_ref
14994get_context_die (tree context)
14995{
14996 if (context)
14997 {
14998 /* Find die that represents this context. */
14999 if (TYPE_P (context))
15000 return force_type_die (context);
15001 else
15002 return force_decl_die (context);
15003 }
15004 return comp_unit_die;
15005}
15006
89f18f73 15007/* Returns the DIE for decl. A DIE will always be returned. */
2b49746a 15008
15009static dw_die_ref
15010force_decl_die (tree decl)
15011{
15012 dw_die_ref decl_die;
15013 unsigned saved_external_flag;
15014 tree save_fn = NULL_TREE;
15015 decl_die = lookup_decl_die (decl);
15016 if (!decl_die)
15017 {
01d2a17d 15018 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
2b49746a 15019
dcfa82ba 15020 decl_die = lookup_decl_die (decl);
15021 if (decl_die)
15022 return decl_die;
15023
2b49746a 15024 switch (TREE_CODE (decl))
15025 {
15026 case FUNCTION_DECL:
15027 /* Clear current_function_decl, so that gen_subprogram_die thinks
15028 that this is a declaration. At this point, we just want to force
15029 declaration die. */
15030 save_fn = current_function_decl;
15031 current_function_decl = NULL_TREE;
15032 gen_subprogram_die (decl, context_die);
8ff30ff6 15033 current_function_decl = save_fn;
2b49746a 15034 break;
15035
15036 case VAR_DECL:
15037 /* Set external flag to force declaration die. Restore it after
15038 gen_decl_die() call. */
15039 saved_external_flag = DECL_EXTERNAL (decl);
15040 DECL_EXTERNAL (decl) = 1;
4b5d70fd 15041 gen_decl_die (decl, NULL, context_die);
2b49746a 15042 DECL_EXTERNAL (decl) = saved_external_flag;
15043 break;
15044
15045 case NAMESPACE_DECL:
15046 dwarf2out_decl (decl);
15047 break;
15048
15049 default:
7bd4f6b6 15050 gcc_unreachable ();
2b49746a 15051 }
8ff30ff6 15052
89f18f73 15053 /* We should be able to find the DIE now. */
2b49746a 15054 if (!decl_die)
15055 decl_die = lookup_decl_die (decl);
7bd4f6b6 15056 gcc_assert (decl_die);
2b49746a 15057 }
8ff30ff6 15058
2b49746a 15059 return decl_die;
15060}
e89530cd 15061
a357c7c2 15062/* Returns the DIE for TYPE, that must not be a base type. A DIE is
15063 always returned. */
e89530cd 15064
15065static dw_die_ref
2b49746a 15066force_type_die (tree type)
e89530cd 15067{
2b49746a 15068 dw_die_ref type_die;
e89530cd 15069
eb550b19 15070 type_die = lookup_type_die (type);
2b49746a 15071 if (!type_die)
15072 {
01d2a17d 15073 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
e89530cd 15074
f4aea3f4 15075 type_die = modified_type_die (type, TYPE_READONLY (type),
15076 TYPE_VOLATILE (type), context_die);
7bd4f6b6 15077 gcc_assert (type_die);
2b49746a 15078 }
15079 return type_die;
e89530cd 15080}
15081
15082/* Force out any required namespaces to be able to output DECL,
15083 and return the new context_die for it, if it's changed. */
15084
15085static dw_die_ref
15086setup_namespace_context (tree thing, dw_die_ref context_die)
15087{
ce45a448 15088 tree context = (DECL_P (thing)
15089 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
e89530cd 15090 if (context && TREE_CODE (context) == NAMESPACE_DECL)
8b332087 15091 /* Force out the namespace. */
2b49746a 15092 context_die = force_decl_die (context);
e89530cd 15093
15094 return context_die;
15095}
15096
15097/* Emit a declaration DIE for THING (which is either a DECL or a tagged
15098 type) within its namespace, if appropriate.
15099
15100 For compatibility with older debuggers, namespace DIEs only contain
15101 declarations; all definitions are emitted at CU scope. */
15102
df4d540f 15103static dw_die_ref
e89530cd 15104declare_in_namespace (tree thing, dw_die_ref context_die)
15105{
15106 dw_die_ref ns_context;
15107
15108 if (debug_info_level <= DINFO_LEVEL_TERSE)
df4d540f 15109 return context_die;
e89530cd 15110
d799a629 15111 /* If this decl is from an inlined function, then don't try to emit it in its
15112 namespace, as we will get confused. It would have already been emitted
15113 when the abstract instance of the inline function was emitted anyways. */
15114 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
df4d540f 15115 return context_die;
d799a629 15116
e89530cd 15117 ns_context = setup_namespace_context (thing, context_die);
15118
15119 if (ns_context != context_die)
15120 {
df4d540f 15121 if (is_fortran ())
15122 return ns_context;
e89530cd 15123 if (DECL_P (thing))
4b5d70fd 15124 gen_decl_die (thing, NULL, ns_context);
e89530cd 15125 else
15126 gen_type_die (thing, ns_context);
15127 }
df4d540f 15128 return context_die;
e89530cd 15129}
15130
8b332087 15131/* Generate a DIE for a namespace or namespace alias. */
e89530cd 15132
15133static void
4b1ab129 15134gen_namespace_die (tree decl, dw_die_ref context_die)
e89530cd 15135{
4b1ab129 15136 dw_die_ref namespace_die;
e89530cd 15137
15138 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21dda4ee 15139 they are an alias of. */
e89530cd 15140 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15141 {
302e9fd3 15142 /* Output a real namespace or module. */
4b1ab129 15143 context_die = setup_namespace_context (decl, comp_unit_die);
15144 namespace_die = new_die (is_fortran ()
15145 ? DW_TAG_module : DW_TAG_namespace,
15146 context_die, decl);
302e9fd3 15147 /* For Fortran modules defined in different CU don't add src coords. */
15148 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15149 add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15150 else
15151 add_name_and_src_coords_attributes (namespace_die, decl);
df4d540f 15152 if (DECL_EXTERNAL (decl))
15153 add_AT_flag (namespace_die, DW_AT_declaration, 1);
e89530cd 15154 equate_decl_number_to_die (decl, namespace_die);
15155 }
15156 else
15157 {
8b332087 15158 /* Output a namespace alias. */
e89530cd 15159
8b332087 15160 /* Force out the namespace we are an alias of, if necessary. */
e89530cd 15161 dw_die_ref origin_die
2b49746a 15162 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
e89530cd 15163
4b1ab129 15164 if (DECL_CONTEXT (decl) == NULL_TREE
15165 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15166 context_die = setup_namespace_context (decl, comp_unit_die);
8b332087 15167 /* Now create the namespace alias DIE. */
4b1ab129 15168 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
e89530cd 15169 add_name_and_src_coords_attributes (namespace_die, decl);
15170 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15171 equate_decl_number_to_die (decl, namespace_die);
15172 }
15173}
15174
30ade641 15175/* Generate Dwarf debug information for a decl described by DECL. */
ec1e49cc 15176
30ade641 15177static void
4b5d70fd 15178gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
30ade641 15179{
4b5d70fd 15180 tree decl_or_origin = decl ? decl : origin;
15181 tree class_origin = NULL;
ec1e49cc 15182
4b5d70fd 15183 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
ec1e49cc 15184 return;
30ade641 15185
4b5d70fd 15186 switch (TREE_CODE (decl_or_origin))
30ade641 15187 {
8c3f468d 15188 case ERROR_MARK:
15189 break;
15190
30ade641 15191 case CONST_DECL:
2eb674c9 15192 if (!is_fortran ())
15193 {
15194 /* The individual enumerators of an enum type get output when we output
15195 the Dwarf representation of the relevant enum type itself. */
15196 break;
15197 }
15198
15199 /* Emit its type. */
15200 gen_type_die (TREE_TYPE (decl), context_die);
15201
15202 /* And its containing namespace. */
15203 context_die = declare_in_namespace (decl, context_die);
15204
15205 gen_const_die (decl, context_die);
30ade641 15206 break;
15207
15208 case FUNCTION_DECL:
cc324702 15209 /* Don't output any DIEs to represent mere function declarations,
15210 unless they are class members or explicit block externs. */
4b5d70fd 15211 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15212 && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15213 && (current_function_decl == NULL_TREE
15214 || DECL_ARTIFICIAL (decl_or_origin)))
ec1e49cc 15215 break;
6ed29fb8 15216
4ee9c684 15217#if 0
15218 /* FIXME */
15219 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15220 on local redeclarations of global functions. That seems broken. */
15221 if (current_function_decl != decl)
15222 /* This is only a declaration. */;
15223#endif
15224
8f80e66d 15225 /* If we're emitting a clone, emit info for the abstract instance. */
4b5d70fd 15226 if (origin || DECL_ORIGIN (decl) != decl)
15227 dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
8c3f468d 15228
0dbc398a 15229 /* If we're emitting an out-of-line copy of an inline function,
15230 emit info for the abstract instance and set up to refer to it. */
5bd74231 15231 else if (cgraph_function_possibly_inlined_p (decl)
15232 && ! DECL_ABSTRACT (decl)
e89530cd 15233 && ! class_or_namespace_scope_p (context_die)
8f80e66d 15234 /* dwarf2out_abstract_function won't emit a die if this is just
15235 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
15236 that case, because that works only if we have a die. */
15237 && DECL_INITIAL (decl) != NULL_TREE)
0dbc398a 15238 {
f414ade2 15239 dwarf2out_abstract_function (decl);
0dbc398a 15240 set_decl_origin_self (decl);
15241 }
8c3f468d 15242
8f80e66d 15243 /* Otherwise we're emitting the primary DIE for this decl. */
15244 else if (debug_info_level > DINFO_LEVEL_TERSE)
6efd403b 15245 {
15246 /* Before we describe the FUNCTION_DECL itself, make sure that we
15247 have described its return type. */
15248 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15249
5134c73b 15250 /* And its virtual context. */
15251 if (DECL_VINDEX (decl) != NULL_TREE)
15252 gen_type_die (DECL_CONTEXT (decl), context_die);
15253
6efd403b 15254 /* And its containing type. */
4b5d70fd 15255 if (!origin)
15256 origin = decl_class_context (decl);
ec1e49cc 15257 if (origin != NULL_TREE)
e7b3c55c 15258 gen_type_die_for_member (origin, decl, context_die);
e89530cd 15259
15260 /* And its containing namespace. */
df4d540f 15261 context_die = declare_in_namespace (decl, context_die);
6efd403b 15262 }
30ade641 15263
15264 /* Now output a DIE to represent the function itself. */
4b5d70fd 15265 if (decl)
15266 gen_subprogram_die (decl, context_die);
30ade641 15267 break;
15268
15269 case TYPE_DECL:
15270 /* If we are in terse mode, don't generate any DIEs to represent any
c83a163c 15271 actual typedefs. */
30ade641 15272 if (debug_info_level <= DINFO_LEVEL_TERSE)
43f116ae 15273 break;
30ade641 15274
8c3f468d 15275 /* In the special case of a TYPE_DECL node representing the declaration
c83a163c 15276 of some type tag, if the given TYPE_DECL is marked as having been
15277 instantiated from some other (original) TYPE_DECL node (e.g. one which
15278 was generated within the original definition of an inline function) we
31334434 15279 used to generate a special (abbreviated) DW_TAG_structure_type,
15280 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
15281 should be actually referencing those DIEs, as variable DIEs with that
15282 type would be emitted already in the abstract origin, so it was always
15283 removed during unused type prunning. Don't add anything in this
15284 case. */
15285 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15286 break;
30ade641 15287
6efd403b 15288 if (is_redundant_typedef (decl))
15289 gen_type_die (TREE_TYPE (decl), context_die);
15290 else
ec1e49cc 15291 /* Output a DIE to represent the typedef itself. */
15292 gen_typedef_die (decl, context_die);
30ade641 15293 break;
15294
15295 case LABEL_DECL:
15296 if (debug_info_level >= DINFO_LEVEL_NORMAL)
ec1e49cc 15297 gen_label_die (decl, context_die);
30ade641 15298 break;
15299
15300 case VAR_DECL:
4ee9c684 15301 case RESULT_DECL:
30ade641 15302 /* If we are in terse mode, don't generate any DIEs to represent any
c83a163c 15303 variable declarations or definitions. */
30ade641 15304 if (debug_info_level <= DINFO_LEVEL_TERSE)
ec1e49cc 15305 break;
30ade641 15306
15307 /* Output any DIEs that are needed to specify the type of this data
c83a163c 15308 object. */
1095d222 15309 if ((TREE_CODE (decl_or_origin) == RESULT_DECL
15310 || (TREE_CODE (decl_or_origin) == VAR_DECL
15311 && !TREE_STATIC (decl_or_origin)))
4b5d70fd 15312 && DECL_BY_REFERENCE (decl_or_origin))
15313 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
73bcd2ba 15314 else
4b5d70fd 15315 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
30ade641 15316
6efd403b 15317 /* And its containing type. */
4b5d70fd 15318 class_origin = decl_class_context (decl_or_origin);
15319 if (class_origin != NULL_TREE)
15320 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
6efd403b 15321
e89530cd 15322 /* And its containing namespace. */
4b5d70fd 15323 context_die = declare_in_namespace (decl_or_origin, context_die);
e89530cd 15324
30ade641 15325 /* Now output the DIE to represent the data object itself. This gets
c83a163c 15326 complicated because of the possibility that the VAR_DECL really
15327 represents an inlined instance of a formal parameter for an inline
15328 function. */
4b5d70fd 15329 if (!origin)
15330 origin = decl_ultimate_origin (decl);
ec1e49cc 15331 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
4b5d70fd 15332 gen_formal_parameter_die (decl, origin, context_die);
30ade641 15333 else
4b5d70fd 15334 gen_variable_die (decl, origin, context_die);
30ade641 15335 break;
15336
15337 case FIELD_DECL:
8c3f468d 15338 /* Ignore the nameless fields that are used to skip bits but handle C++
dbb28acc 15339 anonymous unions and structs. */
ec1e49cc 15340 if (DECL_NAME (decl) != NULL_TREE
dbb28acc 15341 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15342 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
30ade641 15343 {
15344 gen_type_die (member_declared_type (decl), context_die);
15345 gen_field_die (decl, context_die);
15346 }
15347 break;
15348
15349 case PARM_DECL:
4b5d70fd 15350 if (DECL_BY_REFERENCE (decl_or_origin))
15351 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
73bcd2ba 15352 else
4b5d70fd 15353 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15354 gen_formal_parameter_die (decl, origin, context_die);
30ade641 15355 break;
15356
5c65b85a 15357 case NAMESPACE_DECL:
5d8a39b7 15358 case IMPORTED_DECL:
4b1ab129 15359 gen_namespace_die (decl, context_die);
5c65b85a 15360 break;
15361
30ade641 15362 default:
7bd4f6b6 15363 /* Probably some frontend-internal decl. Assume we don't care. */
15364 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15365 break;
30ade641 15366 }
30ade641 15367}
15368\f
8c3f468d 15369/* Output debug information for global decl DECL. Called from toplev.c after
c37d72e9 15370 compilation proper has finished. */
8c3f468d 15371
c37d72e9 15372static void
8ec3a57b 15373dwarf2out_global_decl (tree decl)
c37d72e9 15374{
15375 /* Output DWARF2 information for file-scope tentative data object
39c7766b 15376 declarations, file-scope (extern) function declarations (which
15377 had no corresponding body) and file-scope tagged type declarations
df4d540f 15378 and definitions which have not yet been forced out. */
c37d72e9 15379 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15380 dwarf2out_decl (decl);
15381}
15382
73ae3ef7 15383/* Output debug information for type decl DECL. Called from toplev.c
15384 and from language front ends (to record built-in types). */
15385static void
15386dwarf2out_type_decl (tree decl, int local)
15387{
15388 if (!local)
15389 dwarf2out_decl (decl);
15390}
15391
df4d540f 15392/* Output debug information for imported module or decl DECL.
5d8a39b7 15393 NAME is non-NULL name in the lexical block if the decl has been renamed.
15394 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15395 that DECL belongs to.
15396 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
2b49746a 15397static void
5d8a39b7 15398dwarf2out_imported_module_or_decl_1 (tree decl,
15399 tree name,
15400 tree lexical_block,
15401 dw_die_ref lexical_block_die)
2b49746a 15402{
7bd3dcc4 15403 expanded_location xloc;
5d8a39b7 15404 dw_die_ref imported_die = NULL;
15405 dw_die_ref at_import_die;
8ff30ff6 15406
169f8686 15407 if (TREE_CODE (decl) == IMPORTED_DECL)
15408 {
15409 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15410 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15411 gcc_assert (decl);
15412 }
15413 else
15414 xloc = expand_location (input_location);
15415
cdcf9499 15416 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
a357c7c2 15417 {
15418 if (is_base_type (TREE_TYPE (decl)))
15419 at_import_die = base_type_die (TREE_TYPE (decl));
15420 else
15421 at_import_die = force_type_die (TREE_TYPE (decl));
01d2a17d 15422 /* For namespace N { typedef void T; } using N::T; base_type_die
15423 returns NULL, but DW_TAG_imported_declaration requires
15424 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
15425 if (!at_import_die)
15426 {
15427 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15428 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15429 at_import_die = lookup_type_die (TREE_TYPE (decl));
15430 gcc_assert (at_import_die);
15431 }
a357c7c2 15432 }
2b49746a 15433 else
d4946992 15434 {
15435 at_import_die = lookup_decl_die (decl);
15436 if (!at_import_die)
15437 {
15438 /* If we're trying to avoid duplicate debug info, we may not have
15439 emitted the member decl for this field. Emit it now. */
15440 if (TREE_CODE (decl) == FIELD_DECL)
15441 {
15442 tree type = DECL_CONTEXT (decl);
d4946992 15443
01d2a17d 15444 if (TYPE_CONTEXT (type)
15445 && TYPE_P (TYPE_CONTEXT (type))
15446 && !should_emit_struct_debug (TYPE_CONTEXT (type),
15447 DINFO_USAGE_DIR_USE))
15448 return;
15449 gen_type_die_for_member (type, decl,
15450 get_context_die (TYPE_CONTEXT (type)));
d4946992 15451 }
15452 at_import_die = force_decl_die (decl);
15453 }
15454 }
8ff30ff6 15455
169f8686 15456 if (TREE_CODE (decl) == NAMESPACE_DECL)
5d8a39b7 15457 imported_die = new_die (DW_TAG_imported_module,
15458 lexical_block_die,
15459 lexical_block);
2b49746a 15460 else
5d8a39b7 15461 imported_die = new_die (DW_TAG_imported_declaration,
15462 lexical_block_die,
15463 lexical_block);
7bd3dcc4 15464
69278c24 15465 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
7bd3dcc4 15466 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
df4d540f 15467 if (name)
5d8a39b7 15468 add_AT_string (imported_die, DW_AT_name,
15469 IDENTIFIER_POINTER (name));
2b49746a 15470 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15471}
15472
5d8a39b7 15473/* Output debug information for imported module or decl DECL.
15474 NAME is non-NULL name in context if the decl has been renamed.
15475 CHILD is true if decl is one of the renamed decls as part of
15476 importing whole module. */
15477
15478static void
15479dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15480 bool child)
15481{
15482 /* dw_die_ref at_import_die; */
15483 dw_die_ref scope_die;
15484
15485 if (debug_info_level <= DINFO_LEVEL_TERSE)
15486 return;
15487
15488 gcc_assert (decl);
15489
15490 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15491 We need decl DIE for reference and scope die. First, get DIE for the decl
15492 itself. */
15493
15494 /* Get the scope die for decl context. Use comp_unit_die for global module
15495 or decl. If die is not found for non globals, force new die. */
15496 if (context
15497 && TYPE_P (context)
15498 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15499 return;
15500 scope_die = get_context_die (context);
15501
15502 if (child)
15503 {
15504 gcc_assert (scope_die->die_child);
15505 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15506 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15507 scope_die = scope_die->die_child;
15508 }
15509
15510 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
15511 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15512
15513}
15514
ec1e49cc 15515/* Write the debugging output for DECL. */
15516
30ade641 15517void
8ec3a57b 15518dwarf2out_decl (tree decl)
30ade641 15519{
19cb6b50 15520 dw_die_ref context_die = comp_unit_die;
464217f3 15521
30ade641 15522 switch (TREE_CODE (decl))
15523 {
8c3f468d 15524 case ERROR_MARK:
15525 return;
15526
30ade641 15527 case FUNCTION_DECL:
30ade641 15528 /* What we would really like to do here is to filter out all mere
c83a163c 15529 file-scope declarations of file-scope functions which are never
15530 referenced later within this translation unit (and keep all of ones
15531 that *are* referenced later on) but we aren't clairvoyant, so we have
15532 no idea which functions will be referenced in the future (i.e. later
15533 on within the current translation unit). So here we just ignore all
15534 file-scope function declarations which are not also definitions. If
15535 and when the debugger needs to know something about these functions,
15536 it will have to hunt around and find the DWARF information associated
15537 with the definition of the function.
8c3f468d 15538
15539 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
c83a163c 15540 nodes represent definitions and which ones represent mere
15541 declarations. We have to check DECL_INITIAL instead. That's because
15542 the C front-end supports some weird semantics for "extern inline"
15543 function definitions. These can get inlined within the current
77aa6362 15544 translation unit (and thus, we need to generate Dwarf info for their
c83a163c 15545 abstract instances so that the Dwarf info for the concrete inlined
15546 instances can have something to refer to) but the compiler never
15547 generates any out-of-lines instances of such things (despite the fact
15548 that they *are* definitions).
8c3f468d 15549
15550 The important point is that the C front-end marks these "extern
15551 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15552 them anyway. Note that the C++ front-end also plays some similar games
15553 for inline function definitions appearing within include files which
15554 also contain `#pragma interface' pragmas. */
30ade641 15555 if (DECL_INITIAL (decl) == NULL_TREE)
34425fdc 15556 return;
464217f3 15557
bf1e7d9a 15558 /* If we're a nested function, initially use a parent of NULL; if we're
15559 a plain function, this will be fixed up in decls_for_scope. If
15560 we're a method, it will be ignored, since we already have a DIE. */
e883780d 15561 if (decl_function_context (decl)
15562 /* But if we're in terse mode, we don't care about scope. */
15563 && debug_info_level > DINFO_LEVEL_TERSE)
bf1e7d9a 15564 context_die = NULL;
30ade641 15565 break;
15566
15567 case VAR_DECL:
f80d1bcd 15568 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
c83a163c 15569 declaration and if the declaration was never even referenced from
15570 within this entire compilation unit. We suppress these DIEs in
15571 order to save space in the .debug section (by eliminating entries
15572 which are probably useless). Note that we must not suppress
15573 block-local extern declarations (whether used or not) because that
15574 would screw-up the debugger's name lookup mechanism and cause it to
15575 miss things which really ought to be in scope at a given point. */
30ade641 15576 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
ec1e49cc 15577 return;
30ade641 15578
127d7f21 15579 /* For local statics lookup proper context die. */
15580 if (TREE_STATIC (decl) && decl_function_context (decl))
15581 context_die = lookup_decl_die (DECL_CONTEXT (decl));
15582
30ade641 15583 /* If we are in terse mode, don't generate any DIEs to represent any
c83a163c 15584 variable declarations or definitions. */
30ade641 15585 if (debug_info_level <= DINFO_LEVEL_TERSE)
ec1e49cc 15586 return;
30ade641 15587 break;
15588
2eb674c9 15589 case CONST_DECL:
15590 if (debug_info_level <= DINFO_LEVEL_TERSE)
15591 return;
15592 if (!is_fortran ())
15593 return;
15594 if (TREE_STATIC (decl) && decl_function_context (decl))
15595 context_die = lookup_decl_die (DECL_CONTEXT (decl));
15596 break;
15597
e89530cd 15598 case NAMESPACE_DECL:
5d8a39b7 15599 case IMPORTED_DECL:
e89530cd 15600 if (debug_info_level <= DINFO_LEVEL_TERSE)
15601 return;
15602 if (lookup_decl_die (decl) != NULL)
61a9389f 15603 return;
e89530cd 15604 break;
15605
30ade641 15606 case TYPE_DECL:
ee536dac 15607 /* Don't emit stubs for types unless they are needed by other DIEs. */
15608 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15609 return;
15610
30ade641 15611 /* Don't bother trying to generate any DIEs to represent any of the
c83a163c 15612 normal built-in types for the language we are compiling. */
7bd3dcc4 15613 if (DECL_IS_BUILTIN (decl))
6efd403b 15614 {
15615 /* OK, we need to generate one for `bool' so GDB knows what type
c83a163c 15616 comparisons have. */
bda642f9 15617 if (is_cxx ()
90f973ed 15618 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15619 && ! DECL_IGNORED_P (decl))
6efd403b 15620 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
ec1e49cc 15621
6efd403b 15622 return;
15623 }
30ade641 15624
464217f3 15625 /* If we are in terse mode, don't generate any DIEs for types. */
30ade641 15626 if (debug_info_level <= DINFO_LEVEL_TERSE)
43f116ae 15627 return;
464217f3 15628
15629 /* If we're a function-scope tag, initially use a parent of NULL;
15630 this will be fixed up in decls_for_scope. */
15631 if (decl_function_context (decl))
8a8bfbe7 15632 context_die = NULL;
464217f3 15633
30ade641 15634 break;
15635
15636 default:
15637 return;
15638 }
15639
4b5d70fd 15640 gen_decl_die (decl, NULL, context_die);
30ade641 15641}
15642
15643/* Output a marker (i.e. a label) for the beginning of the generated code for
15644 a lexical block. */
ec1e49cc 15645
1dff614c 15646static void
8ec3a57b 15647dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15648 unsigned int blocknum)
30ade641 15649{
2f14b1f9 15650 switch_to_section (current_function_section ());
64e17633 15651 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
30ade641 15652}
15653
15654/* Output a marker (i.e. a label) for the end of the generated code for a
15655 lexical block. */
ec1e49cc 15656
1dff614c 15657static void
8ec3a57b 15658dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
30ade641 15659{
2f14b1f9 15660 switch_to_section (current_function_section ());
64e17633 15661 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
30ade641 15662}
15663
0a78547b 15664/* Returns nonzero if it is appropriate not to emit any debugging
15665 information for BLOCK, because it doesn't contain any instructions.
ad2fe2cd 15666
0a78547b 15667 Don't allow this for blocks with nested functions or local classes
15668 as we would end up with orphans, and in the presence of scheduling
15669 we may end up calling them anyway. */
15670
b29760a8 15671static bool
5493cb9a 15672dwarf2out_ignore_block (const_tree block)
ad2fe2cd 15673{
15674 tree decl;
4b5d70fd 15675 unsigned int i;
8c3f468d 15676
ad2fe2cd 15677 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
0a78547b 15678 if (TREE_CODE (decl) == FUNCTION_DECL
15679 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15680 return 0;
4b5d70fd 15681 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15682 {
15683 decl = BLOCK_NONLOCALIZED_VAR (block, i);
15684 if (TREE_CODE (decl) == FUNCTION_DECL
15685 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15686 return 0;
15687 }
8c3f468d 15688
0a78547b 15689 return 1;
ad2fe2cd 15690}
15691
69278c24 15692/* Hash table routines for file_hash. */
15693
15694static int
15695file_table_eq (const void *p1_p, const void *p2_p)
15696{
2457c754 15697 const struct dwarf_file_data *const p1 =
15698 (const struct dwarf_file_data *) p1_p;
15699 const char *const p2 = (const char *) p2_p;
69278c24 15700 return strcmp (p1->filename, p2) == 0;
15701}
15702
15703static hashval_t
15704file_table_hash (const void *p_p)
15705{
2457c754 15706 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
69278c24 15707 return htab_hash_string (p->filename);
15708}
15709
8c3f468d 15710/* Lookup FILE_NAME (in the list of filenames that we know about here in
be6eb971 15711 dwarf2out.c) and return its "index". The index of each (known) filename is
8c3f468d 15712 just a unique number which is associated with only that one filename. We
15713 need such numbers for the sake of generating labels (in the .debug_sfnames
15714 section) and references to those files numbers (in the .debug_srcinfo
15715 and.debug_macinfo sections). If the filename given as an argument is not
15716 found in our current list, add it to the list and assign it the next
15717 available unique index number. In order to speed up searches, we remember
15718 the index of the filename was looked up last. This handles the majority of
15719 all searches. */
ec1e49cc 15720
69278c24 15721static struct dwarf_file_data *
8ec3a57b 15722lookup_filename (const char *file_name)
30ade641 15723{
69278c24 15724 void ** slot;
15725 struct dwarf_file_data * created;
30ade641 15726
5a3023d9 15727 /* Check to see if the file name that was searched on the previous
15728 call matches this file name. If so, return the index. */
69278c24 15729 if (file_table_last_lookup
15730 && (file_name == file_table_last_lookup->filename
15731 || strcmp (file_table_last_lookup->filename, file_name) == 0))
15732 return file_table_last_lookup;
30ade641 15733
778ac06a 15734 /* Didn't match the previous lookup, search the table. */
69278c24 15735 slot = htab_find_slot_with_hash (file_table, file_name,
15736 htab_hash_string (file_name), INSERT);
15737 if (*slot)
2457c754 15738 return (struct dwarf_file_data *) *slot;
30ade641 15739
2457c754 15740 created = GGC_NEW (struct dwarf_file_data);
69278c24 15741 created->filename = file_name;
15742 created->emitted_number = 0;
15743 *slot = created;
15744 return created;
c83a163c 15745}
15746
44276901 15747/* If the assembler will construct the file table, then translate the compiler
15748 internal file table number into the assembler file table number, and emit
15749 a .file directive if we haven't already emitted one yet. The file table
15750 numbers are different because we prune debug info for unused variables and
15751 types, which may include filenames. */
15752
c83a163c 15753static int
69278c24 15754maybe_emit_file (struct dwarf_file_data * fd)
c83a163c 15755{
69278c24 15756 if (! fd->emitted_number)
6e957326 15757 {
69278c24 15758 if (last_emitted_file)
15759 fd->emitted_number = last_emitted_file->emitted_number + 1;
15760 else
15761 fd->emitted_number = 1;
15762 last_emitted_file = fd;
61a9389f 15763
69278c24 15764 if (DWARF2_ASM_LINE_DEBUG_INFO)
c83a163c 15765 {
69278c24 15766 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
5f1f2de5 15767 output_quoted_string (asm_out_file,
15768 remap_debug_filename (fd->filename));
c83a163c 15769 fputc ('\n', asm_out_file);
15770 }
6e957326 15771 }
61a9389f 15772
69278c24 15773 return fd->emitted_number;
30ade641 15774}
15775
8d17cbdd 15776/* Replace DW_AT_name for the decl with name. */
15777
15778static void
15779dwarf2out_set_name (tree decl, tree name)
15780{
15781 dw_die_ref die;
15782 dw_attr_ref attr;
15783
15784 die = TYPE_SYMTAB_DIE (decl);
15785 if (!die)
15786 return;
15787
15788 attr = get_AT (die, DW_AT_name);
15789 if (attr)
15790 {
15791 struct indirect_string_node *node;
15792
15793 node = find_AT_string (dwarf2_name (name, 0));
15794 /* replace the string. */
15795 attr->dw_attr_val.v.val_str = node;
15796 }
15797
15798 else
15799 add_name_attribute (die, dwarf2_name (name, 0));
15800}
b2025850 15801/* Called by the final INSN scan whenever we see a var location. We
15802 use it to drop labels in the right places, and throw the location in
15803 our lookup table. */
15804
15805static void
15806dwarf2out_var_location (rtx loc_note)
15807{
15808 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15809 struct var_loc_node *newloc;
15810 rtx prev_insn;
15811 static rtx last_insn;
15812 static const char *last_label;
bbc7bce1 15813 tree decl;
b2025850 15814
15815 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15816 return;
15817 prev_insn = PREV_INSN (loc_note);
15818
2457c754 15819 newloc = GGC_CNEW (struct var_loc_node);
b2025850 15820 /* If the insn we processed last time is the previous insn
15821 and it is also a var location note, use the label we emitted
15822 last time. */
15823 if (last_insn != NULL_RTX
15824 && last_insn == prev_insn
6d7dc5b9 15825 && NOTE_P (prev_insn)
ad4583d9 15826 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
b2025850 15827 {
15828 newloc->label = last_label;
15829 }
15830 else
15831 {
15832 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15833 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15834 loclabel_num++;
15835 newloc->label = ggc_strdup (loclabel);
15836 }
15837 newloc->var_loc_note = loc_note;
15838 newloc->next = NULL;
15839
5fbee89d 15840 if (cfun && in_cold_section_p)
abe32cce 15841 newloc->section_label = crtl->subsections.cold_section_label;
1897b881 15842 else
15843 newloc->section_label = text_section_label;
15844
b2025850 15845 last_insn = loc_note;
15846 last_label = newloc->label;
bbc7bce1 15847 decl = NOTE_VAR_LOCATION_DECL (loc_note);
bbc7bce1 15848 add_var_loc_to_decl (decl, newloc);
b2025850 15849}
15850
15851/* We need to reset the locations at the beginning of each
15852 function. We can't do this in the end_function hook, because the
dae1861f 15853 declarations that use the locations won't have been output when
15854 that hook is called. Also compute have_multiple_function_sections here. */
b2025850 15855
15856static void
dae1861f 15857dwarf2out_begin_function (tree fun)
b2025850 15858{
15859 htab_empty (decl_loc_table);
61a9389f 15860
dae1861f 15861 if (function_section (fun) != text_section)
15862 have_multiple_function_sections = true;
d6de7df9 15863
15864 dwarf2out_note_section_used ();
b2025850 15865}
15866
30ade641 15867/* Output a label to mark the beginning of a source code line entry
15868 and record information relating to this source line, in
15869 'line_info_table' for later output of the .debug_line section. */
ec1e49cc 15870
b9b7f8b4 15871static void
8ec3a57b 15872dwarf2out_source_line (unsigned int line, const char *filename)
30ade641 15873{
d8a4712b 15874 if (debug_info_level >= DINFO_LEVEL_NORMAL
15875 && line != 0)
30ade641 15876 {
69278c24 15877 int file_num = maybe_emit_file (lookup_filename (filename));
61a9389f 15878
2f14b1f9 15879 switch_to_section (current_function_section ());
30ade641 15880
80ae3362 15881 /* If requested, emit something human-readable. */
15882 if (flag_debug_asm)
15883 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15884 filename, line);
15885
985956c1 15886 if (DWARF2_ASM_LINE_DEBUG_INFO)
15887 {
3740694f 15888 /* Emit the .loc directive understood by GNU as. */
5a3023d9 15889 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
985956c1 15890
15891 /* Indicate that line number info exists. */
8c3f468d 15892 line_info_table_in_use++;
985956c1 15893 }
dae1861f 15894 else if (function_section (current_function_decl) != text_section)
30ade641 15895 {
19cb6b50 15896 dw_separate_line_info_ref line_info;
61a9389f 15897 targetm.asm_out.internal_label (asm_out_file,
69278c24 15898 SEPARATE_LINE_CODE_LABEL,
15899 separate_line_info_table_in_use);
c05d7491 15900
aab2cf92 15901 /* Expand the line info table if necessary. */
c05d7491 15902 if (separate_line_info_table_in_use
15903 == separate_line_info_table_allocated)
15904 {
15905 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15906 separate_line_info_table
2457c754 15907 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15908 separate_line_info_table,
15909 separate_line_info_table_allocated);
f0af5a88 15910 memset (separate_line_info_table
15911 + separate_line_info_table_in_use,
573aba85 15912 0,
8ec3a57b 15913 (LINE_INFO_TABLE_INCREMENT
573aba85 15914 * sizeof (dw_separate_line_info_entry)));
c05d7491 15915 }
ec1e49cc 15916
15917 /* Add the new entry at the end of the line_info_table. */
c05d7491 15918 line_info
15919 = &separate_line_info_table[separate_line_info_table_in_use++];
69278c24 15920 line_info->dw_file_num = file_num;
c05d7491 15921 line_info->dw_line_num = line;
4781f9b9 15922 line_info->function = current_function_funcdef_no;
c05d7491 15923 }
15924 else
15925 {
19cb6b50 15926 dw_line_info_ref line_info;
ec1e49cc 15927
883b2e73 15928 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
d58978a6 15929 line_info_table_in_use);
c05d7491 15930
ec1e49cc 15931 /* Expand the line info table if necessary. */
c05d7491 15932 if (line_info_table_in_use == line_info_table_allocated)
15933 {
15934 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15935 line_info_table
2457c754 15936 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15937 line_info_table_allocated);
573aba85 15938 memset (line_info_table + line_info_table_in_use, 0,
15939 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
c05d7491 15940 }
ec1e49cc 15941
15942 /* Add the new entry at the end of the line_info_table. */
c05d7491 15943 line_info = &line_info_table[line_info_table_in_use++];
69278c24 15944 line_info->dw_file_num = file_num;
c05d7491 15945 line_info->dw_line_num = line;
30ade641 15946 }
30ade641 15947 }
15948}
15949
6312a35e 15950/* Record the beginning of a new source file. */
ec1e49cc 15951
c140b944 15952static void
8ec3a57b 15953dwarf2out_start_source_file (unsigned int lineno, const char *filename)
30ade641 15954{
7a614b74 15955 if (flag_eliminate_dwarf2_dups)
19f716e5 15956 {
15957 /* Record the beginning of the file for break_out_includes. */
51e8c210 15958 dw_die_ref bincl_die;
15959
15960 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
5f1f2de5 15961 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
19f716e5 15962 }
8c3f468d 15963
1d340a5e 15964 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15965 {
69278c24 15966 int file_num = maybe_emit_file (lookup_filename (filename));
9b1f6100 15967
2f14b1f9 15968 switch_to_section (debug_macinfo_section);
1d340a5e 15969 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
2cb4ac60 15970 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15971 lineno);
9b1f6100 15972
69278c24 15973 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
1d340a5e 15974 }
30ade641 15975}
15976
c5c7e194 15977/* Record the end of a source file. */
ec1e49cc 15978
c140b944 15979static void
8ec3a57b 15980dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
30ade641 15981{
19f716e5 15982 if (flag_eliminate_dwarf2_dups)
8c3f468d 15983 /* Record the end of the file for break_out_includes. */
15cfae4e 15984 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
8c3f468d 15985
1d340a5e 15986 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15987 {
2f14b1f9 15988 switch_to_section (debug_macinfo_section);
1d340a5e 15989 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15990 }
30ade641 15991}
15992
c5c7e194 15993/* Called from debug_define in toplev.c. The `buffer' parameter contains
30ade641 15994 the tail part of the directive line, i.e. the part which is past the
15995 initial whitespace, #, whitespace, directive-name, whitespace part. */
ec1e49cc 15996
c140b944 15997static void
8ec3a57b 15998dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15999 const char *buffer ATTRIBUTE_UNUSED)
30ade641 16000{
1d340a5e 16001 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16002 {
2f14b1f9 16003 switch_to_section (debug_macinfo_section);
1d340a5e 16004 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16005 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16006 dw2_asm_output_nstring (buffer, -1, "The macro");
16007 }
30ade641 16008}
16009
c5c7e194 16010/* Called from debug_undef in toplev.c. The `buffer' parameter contains
30ade641 16011 the tail part of the directive line, i.e. the part which is past the
16012 initial whitespace, #, whitespace, directive-name, whitespace part. */
ec1e49cc 16013
c140b944 16014static void
8ec3a57b 16015dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16016 const char *buffer ATTRIBUTE_UNUSED)
30ade641 16017{
1d340a5e 16018 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16019 {
2f14b1f9 16020 switch_to_section (debug_macinfo_section);
1d340a5e 16021 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16022 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16023 dw2_asm_output_nstring (buffer, -1, "The macro");
16024 }
30ade641 16025}
16026
16027/* Set up for Dwarf output at the start of compilation. */
ec1e49cc 16028
b896d81b 16029static void
8ec3a57b 16030dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
30ade641 16031{
69278c24 16032 /* Allocate the file_table. */
16033 file_table = htab_create_ggc (50, file_table_hash,
16034 file_table_eq, NULL);
0924bbb7 16035
b2025850 16036 /* Allocate the decl_die_table. */
26863140 16037 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16038 decl_die_table_eq, NULL);
b2025850 16039
16040 /* Allocate the decl_loc_table. */
16041 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16042 decl_loc_table_eq, NULL);
30ade641 16043
16044 /* Allocate the initial hunk of the decl_scope_table. */
4a940e75 16045 decl_scope_table = VEC_alloc (tree, gc, 256);
30ade641 16046
16047 /* Allocate the initial hunk of the abbrev_die_table. */
2457c754 16048 abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
30ade641 16049 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
778ac06a 16050 /* Zero-th entry is allocated, but unused. */
30ade641 16051 abbrev_die_table_in_use = 1;
16052
16053 /* Allocate the initial hunk of the line_info_table. */
2457c754 16054 line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
30ade641 16055 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
8c3f468d 16056
778ac06a 16057 /* Zero-th entry is allocated, but unused. */
30ade641 16058 line_info_table_in_use = 1;
16059
af84796a 16060 /* Allocate the pubtypes and pubnames vectors. */
16061 pubname_table = VEC_alloc (pubname_entry, gc, 32);
16062 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16063
f80d1bcd 16064 /* Generate the initial DIE for the .debug section. Note that the (string)
30ade641 16065 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
f80d1bcd 16066 will (typically) be a relative pathname and that this pathname should be
30ade641 16067 taken as being relative to the directory from which the compiler was
ff279357 16068 invoked when the given (base) source file was compiled. We will fill
16069 in this value in dwarf2out_finish. */
16070 comp_unit_die = gen_compile_unit_die (NULL);
30ade641 16071
22230dd1 16072 incomplete_types = VEC_alloc (tree, gc, 64);
52a7cc7b 16073
62aedc4c 16074 used_rtx_array = VEC_alloc (rtx, gc, 32);
eacbfaac 16075
2f14b1f9 16076 debug_info_section = get_section (DEBUG_INFO_SECTION,
16077 SECTION_DEBUG, NULL);
16078 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16079 SECTION_DEBUG, NULL);
16080 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16081 SECTION_DEBUG, NULL);
16082 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16083 SECTION_DEBUG, NULL);
16084 debug_line_section = get_section (DEBUG_LINE_SECTION,
16085 SECTION_DEBUG, NULL);
16086 debug_loc_section = get_section (DEBUG_LOC_SECTION,
16087 SECTION_DEBUG, NULL);
16088 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16089 SECTION_DEBUG, NULL);
af84796a 16090#ifdef DEBUG_PUBTYPES_SECTION
16091 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16092 SECTION_DEBUG, NULL);
16093#endif
2f14b1f9 16094 debug_str_section = get_section (DEBUG_STR_SECTION,
16095 DEBUG_STR_SECTION_FLAGS, NULL);
16096 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16097 SECTION_DEBUG, NULL);
d08d29c0 16098 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16099 SECTION_DEBUG, NULL);
2f14b1f9 16100
d58978a6 16101 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
049aa99b 16102 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16103 DEBUG_ABBREV_SECTION_LABEL, 0);
e335d512 16104 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
61a9389f 16105 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
4d0e931f 16106 COLD_TEXT_SECTION_LABEL, 0);
16107 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
8c3f468d 16108
f80d1bcd 16109 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
65fc1a16 16110 DEBUG_INFO_SECTION_LABEL, 0);
f80d1bcd 16111 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
65fc1a16 16112 DEBUG_LINE_SECTION_LABEL, 0);
fe39c28c 16113 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16114 DEBUG_RANGES_SECTION_LABEL, 0);
2f14b1f9 16115 switch_to_section (debug_abbrev_section);
65fc1a16 16116 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
2f14b1f9 16117 switch_to_section (debug_info_section);
65fc1a16 16118 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
2f14b1f9 16119 switch_to_section (debug_line_section);
65fc1a16 16120 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
8c3f468d 16121
1d340a5e 16122 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16123 {
2f14b1f9 16124 switch_to_section (debug_macinfo_section);
1d340a5e 16125 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16126 DEBUG_MACINFO_SECTION_LABEL, 0);
16127 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16128 }
2cb4ac60 16129
2f14b1f9 16130 switch_to_section (text_section);
e335d512 16131 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
4d0e931f 16132 if (flag_reorder_blocks_and_partition)
16133 {
d6de7df9 16134 cold_text_section = unlikely_text_section ();
16135 switch_to_section (cold_text_section);
4d0e931f 16136 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16137 }
30ade641 16138}
16139
80b7bd06 16140/* A helper function for dwarf2out_finish called through
16141 ht_forall. Emit one queued .debug_str string. */
16142
16143static int
8ec3a57b 16144output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
80b7bd06 16145{
573aba85 16146 struct indirect_string_node *node = (struct indirect_string_node *) *h;
80b7bd06 16147
80b7bd06 16148 if (node->form == DW_FORM_strp)
16149 {
2f14b1f9 16150 switch_to_section (debug_str_section);
80b7bd06 16151 ASM_OUTPUT_LABEL (asm_out_file, node->label);
573aba85 16152 assemble_string (node->str, strlen (node->str) + 1);
80b7bd06 16153 }
8c3f468d 16154
80b7bd06 16155 return 1;
16156}
16157
cd04bce0 16158#if ENABLE_ASSERT_CHECKING
16159/* Verify that all marks are clear. */
c83a163c 16160
cd04bce0 16161static void
16162verify_marks_clear (dw_die_ref die)
16163{
16164 dw_die_ref c;
61a9389f 16165
cd04bce0 16166 gcc_assert (! die->die_mark);
16167 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16168}
16169#endif /* ENABLE_ASSERT_CHECKING */
c83a163c 16170
16171/* Clear the marks for a die and its children.
037845e5 16172 Be cool if the mark isn't set. */
c83a163c 16173
16174static void
8ec3a57b 16175prune_unmark_dies (dw_die_ref die)
c83a163c 16176{
16177 dw_die_ref c;
61a9389f 16178
958656b7 16179 if (die->die_mark)
16180 die->die_mark = 0;
16181 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
c83a163c 16182}
16183
c83a163c 16184/* Given DIE that we're marking as used, find any other dies
16185 it references as attributes and mark them as used. */
16186
16187static void
8ec3a57b 16188prune_unused_types_walk_attribs (dw_die_ref die)
c83a163c 16189{
16190 dw_attr_ref a;
6f56c055 16191 unsigned ix;
c83a163c 16192
6f56c055 16193 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
c83a163c 16194 {
16195 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16196 {
16197 /* A reference to another DIE.
16198 Make sure that it will get emitted. */
16199 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16200 }
b0aa6b33 16201 /* Set the string's refcount to 0 so that prune_unused_types_mark
16202 accounts properly for it. */
16203 if (AT_class (a) == dw_val_class_str)
16204 a->dw_attr_val.v.val_str->refcount = 0;
c83a163c 16205 }
16206}
16207
16208
16209/* Mark DIE as being used. If DOKIDS is true, then walk down
16210 to DIE's children. */
16211
16212static void
8ec3a57b 16213prune_unused_types_mark (dw_die_ref die, int dokids)
c83a163c 16214{
16215 dw_die_ref c;
16216
16217 if (die->die_mark == 0)
16218 {
16219 /* We haven't done this node yet. Mark it as used. */
16220 die->die_mark = 1;
16221
16222 /* We also have to mark its parents as used.
16223 (But we don't want to mark our parents' kids due to this.) */
16224 if (die->die_parent)
16225 prune_unused_types_mark (die->die_parent, 0);
16226
16227 /* Mark any referenced nodes. */
16228 prune_unused_types_walk_attribs (die);
023dc493 16229
16230 /* If this node is a specification,
61a9389f 16231 also mark the definition, if it exists. */
023dc493 16232 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
61a9389f 16233 prune_unused_types_mark (die->die_definition, 1);
c83a163c 16234 }
16235
16236 if (dokids && die->die_mark != 2)
16237 {
16238 /* We need to walk the children, but haven't done so yet.
16239 Remember that we've walked the kids. */
16240 die->die_mark = 2;
16241
958656b7 16242 /* If this is an array type, we need to make sure our
16243 kids get marked, even if they're types. */
16244 if (die->die_tag == DW_TAG_array_type)
16245 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16246 else
16247 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
c83a163c 16248 }
16249}
16250
4e55a395 16251/* For local classes, look if any static member functions were emitted
16252 and if so, mark them. */
16253
16254static void
16255prune_unused_types_walk_local_classes (dw_die_ref die)
16256{
16257 dw_die_ref c;
16258
16259 if (die->die_mark == 2)
16260 return;
16261
16262 switch (die->die_tag)
16263 {
16264 case DW_TAG_structure_type:
16265 case DW_TAG_union_type:
16266 case DW_TAG_class_type:
16267 break;
16268
16269 case DW_TAG_subprogram:
16270 if (!get_AT_flag (die, DW_AT_declaration)
16271 || die->die_definition != NULL)
16272 prune_unused_types_mark (die, 1);
16273 return;
16274
16275 default:
16276 return;
16277 }
16278
16279 /* Mark children. */
16280 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16281}
c83a163c 16282
16283/* Walk the tree DIE and mark types that we actually use. */
16284
16285static void
8ec3a57b 16286prune_unused_types_walk (dw_die_ref die)
c83a163c 16287{
16288 dw_die_ref c;
16289
4e55a395 16290 /* Don't do anything if this node is already marked and
16291 children have been marked as well. */
16292 if (die->die_mark == 2)
c83a163c 16293 return;
16294
7d4c98bc 16295 switch (die->die_tag)
16296 {
4e55a395 16297 case DW_TAG_structure_type:
16298 case DW_TAG_union_type:
16299 case DW_TAG_class_type:
16300 if (die->die_perennial_p)
16301 break;
16302
16303 for (c = die->die_parent; c; c = c->die_parent)
16304 if (c->die_tag == DW_TAG_subprogram)
16305 break;
16306
16307 /* Finding used static member functions inside of classes
16308 is needed just for local classes, because for other classes
16309 static member function DIEs with DW_AT_specification
16310 are emitted outside of the DW_TAG_*_type. If we ever change
16311 it, we'd need to call this even for non-local classes. */
16312 if (c)
16313 prune_unused_types_walk_local_classes (die);
16314
16315 /* It's a type node --- don't mark it. */
16316 return;
16317
7d4c98bc 16318 case DW_TAG_const_type:
16319 case DW_TAG_packed_type:
16320 case DW_TAG_pointer_type:
16321 case DW_TAG_reference_type:
16322 case DW_TAG_volatile_type:
16323 case DW_TAG_typedef:
16324 case DW_TAG_array_type:
03a61d93 16325 case DW_TAG_interface_type:
7d4c98bc 16326 case DW_TAG_friend:
16327 case DW_TAG_variant_part:
16328 case DW_TAG_enumeration_type:
16329 case DW_TAG_subroutine_type:
16330 case DW_TAG_string_type:
16331 case DW_TAG_set_type:
16332 case DW_TAG_subrange_type:
16333 case DW_TAG_ptr_to_member_type:
16334 case DW_TAG_file_type:
16335 if (die->die_perennial_p)
16336 break;
f6e59711 16337
7d4c98bc 16338 /* It's a type node --- don't mark it. */
16339 return;
c83a163c 16340
7d4c98bc 16341 default:
16342 /* Mark everything else. */
16343 break;
c83a163c 16344 }
16345
4e55a395 16346 if (die->die_mark == 0)
16347 {
16348 die->die_mark = 1;
16349
16350 /* Now, mark any dies referenced from here. */
16351 prune_unused_types_walk_attribs (die);
16352 }
c83a163c 16353
4e55a395 16354 die->die_mark = 2;
c83a163c 16355
16356 /* Mark children. */
958656b7 16357 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
c83a163c 16358}
16359
b0aa6b33 16360/* Increment the string counts on strings referred to from DIE's
16361 attributes. */
16362
16363static void
16364prune_unused_types_update_strings (dw_die_ref die)
16365{
16366 dw_attr_ref a;
16367 unsigned ix;
16368
16369 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16370 if (AT_class (a) == dw_val_class_str)
16371 {
16372 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16373 s->refcount++;
16374 /* Avoid unnecessarily putting strings that are used less than
16375 twice in the hash table. */
20f220a9 16376 if (s->refcount
16377 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
b0aa6b33 16378 {
16379 void ** slot;
16380 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16381 htab_hash_string (s->str),
16382 INSERT);
16383 gcc_assert (*slot == NULL);
16384 *slot = s;
16385 }
16386 }
16387}
c83a163c 16388
16389/* Remove from the tree DIE any dies that aren't marked. */
16390
16391static void
8ec3a57b 16392prune_unused_types_prune (dw_die_ref die)
c83a163c 16393{
958656b7 16394 dw_die_ref c;
8ff30ff6 16395
7bd4f6b6 16396 gcc_assert (die->die_mark);
4533b23c 16397 prune_unused_types_update_strings (die);
c83a163c 16398
958656b7 16399 if (! die->die_child)
16400 return;
61a9389f 16401
958656b7 16402 c = die->die_child;
16403 do {
16404 dw_die_ref prev = c;
16405 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16406 if (c == die->die_child)
c83a163c 16407 {
958656b7 16408 /* No marked children between 'prev' and the end of the list. */
16409 if (prev == c)
16410 /* No marked children at all. */
16411 die->die_child = NULL;
16412 else
16413 {
16414 prev->die_sib = c->die_sib;
16415 die->die_child = prev;
16416 }
16417 return;
c83a163c 16418 }
958656b7 16419
16420 if (c != prev->die_sib)
16421 prev->die_sib = c;
958656b7 16422 prune_unused_types_prune (c);
16423 } while (c != die->die_child);
c83a163c 16424}
16425
16426
16427/* Remove dies representing declarations that we never use. */
16428
16429static void
8ec3a57b 16430prune_unused_types (void)
c83a163c 16431{
16432 unsigned int i;
16433 limbo_die_node *node;
af84796a 16434 pubname_ref pub;
c83a163c 16435
cd04bce0 16436#if ENABLE_ASSERT_CHECKING
16437 /* All the marks should already be clear. */
16438 verify_marks_clear (comp_unit_die);
c83a163c 16439 for (node = limbo_die_list; node; node = node->next)
cd04bce0 16440 verify_marks_clear (node->die);
16441#endif /* ENABLE_ASSERT_CHECKING */
c83a163c 16442
16443 /* Set the mark on nodes that are actually used. */
16444 prune_unused_types_walk (comp_unit_die);
16445 for (node = limbo_die_list; node; node = node->next)
16446 prune_unused_types_walk (node->die);
16447
16448 /* Also set the mark on nodes referenced from the
16449 pubname_table or arange_table. */
af84796a 16450 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16451 prune_unused_types_mark (pub->die, 1);
ea0041f4 16452 for (i = 0; i < arange_table_in_use; i++)
16453 prune_unused_types_mark (arange_table[i], 1);
c83a163c 16454
b0aa6b33 16455 /* Get rid of nodes that aren't marked; and update the string counts. */
16456 if (debug_str_hash)
16457 htab_empty (debug_str_hash);
c83a163c 16458 prune_unused_types_prune (comp_unit_die);
16459 for (node = limbo_die_list; node; node = node->next)
16460 prune_unused_types_prune (node->die);
16461
16462 /* Leave the marks clear. */
16463 prune_unmark_dies (comp_unit_die);
16464 for (node = limbo_die_list; node; node = node->next)
16465 prune_unmark_dies (node->die);
16466}
16467
69278c24 16468/* Set the parameter to true if there are any relative pathnames in
16469 the file table. */
16470static int
16471file_table_relative_p (void ** slot, void *param)
16472{
2457c754 16473 bool *p = (bool *) param;
16474 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
5f1f2de5 16475 if (!IS_ABSOLUTE_PATH (d->filename))
69278c24 16476 {
16477 *p = true;
16478 return 0;
16479 }
16480 return 1;
16481}
16482
30ade641 16483/* Output stuff that dwarf requires at the end of every file,
16484 and generate the DWARF-2 debugging info. */
ec1e49cc 16485
b896d81b 16486static void
8ec3a57b 16487dwarf2out_finish (const char *filename)
30ade641 16488{
678d90bb 16489 limbo_die_node *node, *next_node;
97b330ca 16490 dw_die_ref die = 0;
89f29a1b 16491 unsigned int i;
678d90bb 16492
ff279357 16493 /* Add the name for the main input file now. We delayed this from
16494 dwarf2out_init to avoid complications with PCH. */
5f1f2de5 16495 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
974a92fe 16496 if (!IS_ABSOLUTE_PATH (filename))
ff279357 16497 add_comp_dir_attribute (comp_unit_die);
83f77ecb 16498 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16499 {
69278c24 16500 bool p = false;
16501 htab_traverse (file_table, file_table_relative_p, &p);
16502 if (p)
16503 add_comp_dir_attribute (comp_unit_die);
83f77ecb 16504 }
ff279357 16505
89f29a1b 16506 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16507 {
16508 add_location_or_const_value_attribute (
16509 VEC_index (deferred_locations, deferred_locations_list, i)->die,
16510 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16511 DW_AT_location);
16512 }
16513
678d90bb 16514 /* Traverse the limbo die list, and add parent/child links. The only
16515 dies without parents that should be here are concrete instances of
16516 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
16517 For concrete instances, we can get the parent die from the abstract
16518 instance. */
16519 for (node = limbo_die_list; node; node = next_node)
16520 {
16521 next_node = node->next;
16522 die = node->die;
16523
16524 if (die->die_parent == NULL)
16525 {
c90bf86c 16526 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
8c3f468d 16527
c90bf86c 16528 if (origin)
16529 add_child_die (origin->die_parent, die);
678d90bb 16530 else if (die == comp_unit_die)
c90bf86c 16531 ;
15a56411 16532 else if (errorcount > 0 || sorrycount > 0)
16533 /* It's OK to be confused by errors in the input. */
16534 add_child_die (comp_unit_die, die);
7bd4f6b6 16535 else
15cfae4e 16536 {
16537 /* In certain situations, the lexical block containing a
16538 nested function can be optimized away, which results
16539 in the nested function die being orphaned. Likewise
16540 with the return type of that nested function. Force
1b55e9dc 16541 this to be a child of the containing function.
16542
16543 It may happen that even the containing function got fully
16544 inlined and optimized out. In that case we are lost and
16545 assign the empty child. This should not be big issue as
16546 the function is likely unreachable too. */
7bd4f6b6 16547 tree context = NULL_TREE;
16548
16549 gcc_assert (node->created_for);
16550
16551 if (DECL_P (node->created_for))
16552 context = DECL_CONTEXT (node->created_for);
16553 else if (TYPE_P (node->created_for))
16554 context = TYPE_CONTEXT (node->created_for);
8ff30ff6 16555
356f311d 16556 gcc_assert (context
16557 && (TREE_CODE (context) == FUNCTION_DECL
16558 || TREE_CODE (context) == NAMESPACE_DECL));
8ff30ff6 16559
15cfae4e 16560 origin = lookup_decl_die (context);
1b55e9dc 16561 if (origin)
16562 add_child_die (origin, die);
68690e9c 16563 else
16564 add_child_die (comp_unit_die, die);
15cfae4e 16565 }
678d90bb 16566 }
678d90bb 16567 }
8c3f468d 16568
c90bf86c 16569 limbo_die_list = NULL;
678d90bb 16570
a4617d03 16571 /* Walk through the list of incomplete types again, trying once more to
16572 emit full debugging info for them. */
16573 retry_incomplete_types ();
16574
449db731 16575 if (flag_eliminate_unused_debug_types)
16576 prune_unused_types ();
16577
19f716e5 16578 /* Generate separate CUs for each of the include files we've seen.
16579 They will go into limbo_die_list. */
02749c22 16580 if (flag_eliminate_dwarf2_dups)
16581 break_out_includes (comp_unit_die);
19f716e5 16582
16583 /* Traverse the DIE's and add add sibling attributes to those DIE's
16584 that have children. */
30ade641 16585 add_sibling_attributes (comp_unit_die);
19f716e5 16586 for (node = limbo_die_list; node; node = node->next)
16587 add_sibling_attributes (node->die);
30ade641 16588
16589 /* Output a terminator label for the .text section. */
2f14b1f9 16590 switch_to_section (text_section);
883b2e73 16591 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
4d0e931f 16592 if (flag_reorder_blocks_and_partition)
16593 {
5fbee89d 16594 switch_to_section (unlikely_text_section ());
4d0e931f 16595 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16596 }
30ade641 16597
603796f0 16598 /* We can only use the low/high_pc attributes if all of the code was
16599 in .text. */
dae1861f 16600 if (!have_multiple_function_sections)
603796f0 16601 {
16602 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16603 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
c05d7491 16604 }
8c3f468d 16605
f221c0bd 16606 else
16607 {
16608 unsigned fde_idx = 0;
16609
16610 /* We need to give .debug_loc and .debug_ranges an appropriate
16611 "base address". Use zero so that these addresses become
16612 absolute. Historically, we've emitted the unexpected
16613 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16614 Emit both to give time for other tools to adapt. */
16615 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16616 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16617
16618 add_AT_range_list (comp_unit_die, DW_AT_ranges,
16619 add_ranges_by_labels (text_section_label,
16620 text_end_label));
16621 if (flag_reorder_blocks_and_partition)
16622 add_ranges_by_labels (cold_text_section_label,
16623 cold_end_label);
16624
16625 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16626 {
16627 dw_fde_ref fde = &fde_table[fde_idx];
16628
16629 if (fde->dw_fde_switched_sections)
16630 {
16631 add_ranges_by_labels (fde->dw_fde_hot_section_label,
16632 fde->dw_fde_hot_section_end_label);
16633 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16634 fde->dw_fde_unlikely_section_end_label);
16635 }
16636 else
16637 add_ranges_by_labels (fde->dw_fde_begin,
16638 fde->dw_fde_end);
16639 }
16640
16641 add_ranges (NULL);
16642 }
c05d7491 16643
dae1861f 16644 /* Output location list section if necessary. */
16645 if (have_location_lists)
16646 {
16647 /* Output the location lists info. */
16648 switch_to_section (debug_loc_section);
16649 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16650 DEBUG_LOC_SECTION_LABEL, 0);
16651 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16652 output_location_lists (die);
16653 }
16654
28833db5 16655 if (debug_info_level >= DINFO_LEVEL_NORMAL)
d08d29c0 16656 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16657 debug_line_section_label);
603796f0 16658
1d340a5e 16659 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
d08d29c0 16660 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
c90bf86c 16661
19f716e5 16662 /* Output all of the compilation units. We put the main one last so that
16663 the offsets are available to output_pubnames. */
16664 for (node = limbo_die_list; node; node = node->next)
51e8c210 16665 output_comp_unit (node->die, 0);
8c3f468d 16666
e1b3cc49 16667 /* Output the main compilation unit if non-empty or if .debug_macinfo
16668 has been emitted. */
16669 output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
19f716e5 16670
30ade641 16671 /* Output the abbreviation table. */
2f14b1f9 16672 switch_to_section (debug_abbrev_section);
30ade641 16673 output_abbrev_section ();
16674
8c3f468d 16675 /* Output public names table if necessary. */
af84796a 16676 if (!VEC_empty (pubname_entry, pubname_table))
dc7a29ce 16677 {
2f14b1f9 16678 switch_to_section (debug_pubnames_section);
af84796a 16679 output_pubnames (pubname_table);
dc7a29ce 16680 }
16681
af84796a 16682#ifdef DEBUG_PUBTYPES_SECTION
16683 /* Output public types table if necessary. */
16684 if (!VEC_empty (pubname_entry, pubtype_table))
16685 {
16686 switch_to_section (debug_pubtypes_section);
16687 output_pubnames (pubtype_table);
16688 }
16689#endif
61a9389f 16690
8c3f468d 16691 /* Output the address range information. We only put functions in the arange
16692 table, so don't write it out if we don't have any. */
30ade641 16693 if (fde_table_in_use)
16694 {
2f14b1f9 16695 switch_to_section (debug_aranges_section);
30ade641 16696 output_aranges ();
16697 }
a36145ca 16698
a36145ca 16699 /* Output ranges section if necessary. */
16700 if (ranges_table_in_use)
16701 {
2f14b1f9 16702 switch_to_section (debug_ranges_section);
fe39c28c 16703 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
a36145ca 16704 output_ranges ();
16705 }
16706
69278c24 16707 /* Output the source line correspondence table. We must do this
16708 even if there is no line information. Otherwise, on an empty
16709 translation unit, we will generate a present, but empty,
16710 .debug_info section. IRIX 6.5 `nm' will then complain when
16711 examining the file. This is done late so that any filenames
16712 used by the debug_info section are marked as 'used'. */
16713 if (! DWARF2_ASM_LINE_DEBUG_INFO)
16714 {
16715 switch_to_section (debug_line_section);
16716 output_line_info ();
16717 }
16718
156660d7 16719 /* Have to end the macro section. */
c5c7e194 16720 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
bc70bd5e 16721 {
2f14b1f9 16722 switch_to_section (debug_macinfo_section);
323583a1 16723 dw2_asm_output_data (1, 0, "End compilation unit");
c5c7e194 16724 }
80b7bd06 16725
8c3f468d 16726 /* If we emitted any DW_FORM_strp form attribute, output the string
80b7bd06 16727 table too. */
16728 if (debug_str_hash)
573aba85 16729 htab_traverse (debug_str_hash, output_indirect_string, NULL);
30ade641 16730}
1f3233d1 16731#else
16732
16733/* This should never be used, but its address is needed for comparisons. */
16734const struct gcc_debug_hooks dwarf2_debug_hooks;
16735
16736#endif /* DWARF2_DEBUGGING_INFO */
16737
16738#include "gt-dwarf2out.h"