]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dwarf2out.c
2007-08-08 Samuel Thibault <samuel.thibault@ens-lyon.org>
[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,
1a6a0f2a 3 2003, 2004, 2005, 2006, 2007 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
d757b8c9 113/* Decide whether we want to emit frame unwind information for the current
114 translation unit. */
115
116int
8ec3a57b 117dwarf2out_do_frame (void)
d757b8c9 118{
34986748 119 /* We want to emit correct CFA location expressions or lists, so we
120 have to return true if we're going to output debug info, even if
121 we're not going to output frame or unwind info. */
d757b8c9 122 return (write_symbols == DWARF2_DEBUG
8d60d2bc 123 || write_symbols == VMS_AND_DWARF2_DEBUG
f80d1bcd 124 || DWARF2_FRAME_INFO
d757b8c9 125#ifdef DWARF2_UNWIND_INFO
34986748 126 || (DWARF2_UNWIND_INFO
127 && (flag_unwind_tables
128 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
d757b8c9 129#endif
130 );
131}
132
13c14f1c 133/* The size of the target's pointer type. */
134#ifndef PTR_SIZE
135#define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136#endif
137
1f3233d1 138/* Array of RTXes referenced by the debugging information, which therefore
139 must be kept around forever. */
62aedc4c 140static GTY(()) VEC(rtx,gc) *used_rtx_array;
1f3233d1 141
142/* A pointer to the base of a list of incomplete types which might be
22230dd1 143 completed at some later time. incomplete_types_list needs to be a
144 VEC(tree,gc) because we want to tell the garbage collector about
145 it. */
146static GTY(()) VEC(tree,gc) *incomplete_types;
1f3233d1 147
148/* A pointer to the base of a table of references to declaration
149 scopes. This table is a display which tracks the nesting
150 of declaration scopes at the current scope and containing
151 scopes. This table is used to find the proper place to
152 define type declaration DIE's. */
4a940e75 153static GTY(()) VEC(tree,gc) *decl_scope_table;
1f3233d1 154
2f14b1f9 155/* Pointers to various DWARF2 sections. */
156static GTY(()) section *debug_info_section;
157static GTY(()) section *debug_abbrev_section;
158static GTY(()) section *debug_aranges_section;
159static GTY(()) section *debug_macinfo_section;
160static GTY(()) section *debug_line_section;
161static GTY(()) section *debug_loc_section;
162static GTY(()) section *debug_pubnames_section;
af84796a 163static GTY(()) section *debug_pubtypes_section;
2f14b1f9 164static GTY(()) section *debug_str_section;
165static GTY(()) section *debug_ranges_section;
d08d29c0 166static GTY(()) section *debug_frame_section;
2f14b1f9 167
e5530d32 168/* How to start an assembler comment. */
169#ifndef ASM_COMMENT_START
170#define ASM_COMMENT_START ";#"
171#endif
172
30ade641 173typedef struct dw_cfi_struct *dw_cfi_ref;
174typedef struct dw_fde_struct *dw_fde_ref;
175typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
30ade641 176
177/* Call frames are described using a sequence of Call Frame
178 Information instructions. The register number, offset
179 and address fields are provided as possible operands;
180 their use is selected by the opcode field. */
ec1e49cc 181
573aba85 182enum dw_cfi_oprnd_type {
183 dw_cfi_oprnd_unused,
184 dw_cfi_oprnd_reg_num,
185 dw_cfi_oprnd_offset,
186 dw_cfi_oprnd_addr,
187 dw_cfi_oprnd_loc
188};
189
190typedef union dw_cfi_oprnd_struct GTY(())
ec1e49cc 191{
da72c083 192 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
3d867824 193 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
573aba85 194 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
ec1e49cc 196}
30ade641 197dw_cfi_oprnd;
198
573aba85 199typedef struct dw_cfi_struct GTY(())
ec1e49cc 200{
201 dw_cfi_ref dw_cfi_next;
202 enum dwarf_call_frame_info dw_cfi_opc;
8ec3a57b 203 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
573aba85 204 dw_cfi_oprnd1;
8ec3a57b 205 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
573aba85 206 dw_cfi_oprnd2;
ec1e49cc 207}
30ade641 208dw_cfi_node;
209
4b72e226 210/* This is how we define the location of the CFA. We use to handle it
211 as REG + OFFSET all the time, but now it can be more complex.
212 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
f80d1bcd 213 Instead of passing around REG and OFFSET, we pass a copy
4b72e226 214 of this structure. */
573aba85 215typedef struct cfa_loc GTY(())
4b72e226 216{
3d867824 217 HOST_WIDE_INT offset;
218 HOST_WIDE_INT base_offset;
12d886b8 219 unsigned int reg;
4b72e226 220 int indirect; /* 1 if CFA is accessed via a dereference. */
221} dw_cfa_location;
222
30ade641 223/* All call frame descriptions (FDE's) in the GCC generated DWARF
752e49ca 224 refer to a single Common Information Entry (CIE), defined at
dae39efc 225 the beginning of the .debug_frame section. This use of a single
30ade641 226 CIE obviates the need to keep track of multiple CIE's
227 in the DWARF generation routines below. */
ec1e49cc 228
573aba85 229typedef struct dw_fde_struct GTY(())
ec1e49cc 230{
2f9fc8ef 231 tree decl;
1e034a40 232 const char *dw_fde_begin;
233 const char *dw_fde_current_label;
234 const char *dw_fde_end;
1897b881 235 const char *dw_fde_hot_section_label;
236 const char *dw_fde_hot_section_end_label;
237 const char *dw_fde_unlikely_section_label;
238 const char *dw_fde_unlikely_section_end_label;
239 bool dw_fde_switched_sections;
ec1e49cc 240 dw_cfi_ref dw_fde_cfi;
df4b504c 241 unsigned funcdef_number;
04396483 242 unsigned all_throwers_are_sibcalls : 1;
df4b504c 243 unsigned nothrow : 1;
244 unsigned uses_eh_lsda : 1;
ec1e49cc 245}
30ade641 246dw_fde_node;
247
1e625a2e 248/* Maximum size (in bytes) of an artificially generated label. */
30ade641 249#define MAX_ARTIFICIAL_LABEL_BYTES 30
250
aaa408cd 251/* The size of addresses as they appear in the Dwarf 2 data.
252 Some architectures use word addresses to refer to code locations,
253 but Dwarf 2 info always uses byte addresses. On such machines,
254 Dwarf 2 addresses need to be larger than the architecture's
255 pointers. */
256#ifndef DWARF2_ADDR_SIZE
257#define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258#endif
259
a3899bb7 260/* The size in bytes of a DWARF field indicating an offset or length
aaa408cd 261 relative to a debug info section, specified to be 4 bytes in the
262 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
b6ce7963 263 as PTR_SIZE. */
ec1e49cc 264
a3899bb7 265#ifndef DWARF_OFFSET_SIZE
266#define DWARF_OFFSET_SIZE 4
267#endif
268
65bdc57c 269/* According to the (draft) DWARF 3 specification, the initial length
270 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
271 bytes are 0xffffffff, followed by the length stored in the next 8
272 bytes.
273
274 However, the SGI/MIPS ABI uses an initial length which is equal to
275 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
276
277#ifndef DWARF_INITIAL_LENGTH_SIZE
278#define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279#endif
280
be6eb971 281#define DWARF_VERSION 2
282
a3899bb7 283/* Round SIZE up to the nearest BOUNDARY. */
284#define DWARF_ROUND(SIZE,BOUNDARY) \
e711a040 285 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
30ade641 286
30ade641 287/* Offsets recorded in opcodes are a multiple of this alignment factor. */
78ac74b9 288#ifndef DWARF_CIE_DATA_ALIGNMENT
df78b73b 289#ifdef STACK_GROWS_DOWNWARD
7eb04d1c 290#define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
df78b73b 291#else
7eb04d1c 292#define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
df78b73b 293#endif
8c3f468d 294#endif
30ade641 295
04da8de9 296/* CIE identifier. */
297#if HOST_BITS_PER_WIDE_INT >= 64
298#define DWARF_CIE_ID \
299 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300#else
301#define DWARF_CIE_ID DW_CIE_ID
302#endif
303
8a8bfbe7 304/* A pointer to the base of a table that contains frame description
305 information for each routine. */
573aba85 306static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
30ade641 307
8a8bfbe7 308/* Number of elements currently allocated for fde_table. */
909be935 309static GTY(()) unsigned fde_table_allocated;
6efd403b 310
8a8bfbe7 311/* Number of elements in fde_table currently in use. */
9105005a 312static GTY(()) unsigned fde_table_in_use;
30ade641 313
8a8bfbe7 314/* Size (in elements) of increments by which we may expand the
315 fde_table. */
316#define FDE_TABLE_INCREMENT 256
30ade641 317
6efd403b 318/* A list of call frame insns for the CIE. */
573aba85 319static GTY(()) dw_cfi_ref cie_cfi_head;
6efd403b 320
38ac91bf 321#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
30ade641 322/* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
323 attribute that accelerates the lookup of the FDE associated
f80d1bcd 324 with the subprogram. This variable holds the table index of the FDE
30ade641 325 associated with the current function (body) definition. */
326static unsigned current_funcdef_fde;
38ac91bf 327#endif
30ade641 328
573aba85 329struct indirect_string_node GTY(())
80b7bd06 330{
573aba85 331 const char *str;
80b7bd06 332 unsigned int refcount;
333 unsigned int form;
334 char *label;
335};
336
573aba85 337static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
338
339static GTY(()) int dw2_string_counter;
9105005a 340static GTY(()) unsigned long dwarf2out_cfi_label_num;
573aba85 341
342#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
343
30ade641 344/* Forward declarations for functions defined in this file. */
ec1e49cc 345
8ec3a57b 346static char *stripattributes (const char *);
347static const char *dwarf_cfi_name (unsigned);
348static dw_cfi_ref new_cfi (void);
349static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
350static void add_fde_cfi (const char *, dw_cfi_ref);
351static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
352static void lookup_cfa (dw_cfa_location *);
3d867824 353static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
bf780b7e 354#ifdef DWARF2_UNWIND_INFO
8ec3a57b 355static void initial_return_save (rtx);
bf780b7e 356#endif
3d867824 357static HOST_WIDE_INT stack_adjust_offset (rtx);
8ec3a57b 358static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
359static void output_call_frame_info (int);
535fcfa4 360static void dwarf2out_stack_adjust (rtx, bool);
8ec3a57b 361static void flush_queued_reg_saves (void);
362static bool clobbers_queued_reg_save (rtx);
363static void dwarf2out_frame_debug_expr (rtx, const char *);
30ade641 364
4b72e226 365/* Support for complex CFA locations. */
8ec3a57b 366static void output_cfa_loc (dw_cfi_ref);
367static void get_cfa_from_loc_descr (dw_cfa_location *,
368 struct dw_loc_descr_struct *);
4b72e226 369static struct dw_loc_descr_struct *build_cfa_loc
89fa767a 370 (dw_cfa_location *, HOST_WIDE_INT);
8ec3a57b 371static void def_cfa_1 (const char *, dw_cfa_location *);
4b72e226 372
ca98eb0a 373/* How to start an assembler comment. */
374#ifndef ASM_COMMENT_START
375#define ASM_COMMENT_START ";#"
30ade641 376#endif
377
a3899bb7 378/* Data and reference forms for relocatable data. */
379#define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
380#define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
381
702620e0 382#ifndef DEBUG_FRAME_SECTION
383#define DEBUG_FRAME_SECTION ".debug_frame"
30ade641 384#endif
30ade641 385
d58978a6 386#ifndef FUNC_BEGIN_LABEL
387#define FUNC_BEGIN_LABEL "LFB"
30ade641 388#endif
8c3f468d 389
d58978a6 390#ifndef FUNC_END_LABEL
391#define FUNC_END_LABEL "LFE"
30ade641 392#endif
8c3f468d 393
2f9fc8ef 394#ifndef FRAME_BEGIN_LABEL
48ead6eb 395#define FRAME_BEGIN_LABEL "Lframe"
2f9fc8ef 396#endif
19bce576 397#define CIE_AFTER_SIZE_LABEL "LSCIE"
398#define CIE_END_LABEL "LECIE"
ca98eb0a 399#define FDE_LABEL "LSFDE"
400#define FDE_AFTER_SIZE_LABEL "LASFDE"
19bce576 401#define FDE_END_LABEL "LEFDE"
3740694f 402#define LINE_NUMBER_BEGIN_LABEL "LSLT"
403#define LINE_NUMBER_END_LABEL "LELT"
404#define LN_PROLOG_AS_LABEL "LASLTP"
405#define LN_PROLOG_END_LABEL "LELTP"
19f716e5 406#define DIE_LABEL_PREFIX "DW"
30ade641 407
212538c2 408/* The DWARF 2 CFA column which tracks the return address. Normally this
6efd403b 409 is the column for PC, or the first column after all of the hard
410 registers. */
212538c2 411#ifndef DWARF_FRAME_RETURN_COLUMN
6efd403b 412#ifdef PC_REGNUM
8ec3a57b 413#define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
6efd403b 414#else
8ec3a57b 415#define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
6efd403b 416#endif
212538c2 417#endif
418
419/* The mapping from gcc register number to DWARF 2 CFA column number. By
df78b73b 420 default, we just provide columns for all registers. */
212538c2 421#ifndef DWARF_FRAME_REGNUM
df78b73b 422#define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
212538c2 423#endif
8c3f468d 424\f
d757b8c9 425/* Hook used by __throw. */
426
427rtx
8ec3a57b 428expand_builtin_dwarf_sp_column (void)
d757b8c9 429{
963e1d38 430 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
c98ee857 431 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
d757b8c9 432}
433
ec1e49cc 434/* Return a pointer to a copy of the section string name S with all
1bfb8e27 435 attributes stripped off, and an asterisk prepended (for assemble_name). */
ec1e49cc 436
437static inline char *
8ec3a57b 438stripattributes (const char *s)
30ade641 439{
4c36ffe6 440 char *stripped = XNEWVEC (char, strlen (s) + 2);
ec1e49cc 441 char *p = stripped;
442
1bfb8e27 443 *p++ = '*';
444
445 while (*s && *s != ',')
446 *p++ = *s++;
ec1e49cc 447
30ade641 448 *p = '\0';
449 return stripped;
450}
451
dd5e1e90 452/* MEM is a memory reference for the register size table, each element of
453 which has mode MODE. Initialize column C as a return address column. */
454
455static void
456init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
457{
458 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
459 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
460 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
461}
462
695e919b 463/* Generate code to initialize the register size table. */
5ff00a1d 464
695e919b 465void
8ec3a57b 466expand_builtin_init_dwarf_reg_sizes (tree address)
5ff00a1d 467{
963e1d38 468 unsigned int i;
695e919b 469 enum machine_mode mode = TYPE_MODE (char_type_node);
8ec3c5c2 470 rtx addr = expand_normal (address);
8c3f468d 471 rtx mem = gen_rtx_MEM (BLKmode, addr);
5fec5f34 472 bool wrote_return_column = false;
5ff00a1d 473
33f90206 474 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
c98ee857 475 {
476 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
61a9389f 477
c98ee857 478 if (rnum < DWARF_FRAME_REGISTERS)
479 {
480 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
481 enum machine_mode save_mode = reg_raw_mode[i];
482 HOST_WIDE_INT size;
61a9389f 483
c98ee857 484 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
485 save_mode = choose_hard_reg_mode (i, 1, true);
486 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
487 {
488 if (save_mode == VOIDmode)
489 continue;
490 wrote_return_column = true;
491 }
492 size = GET_MODE_SIZE (save_mode);
493 if (offset < 0)
494 continue;
61a9389f 495
c98ee857 496 emit_move_insn (adjust_address (mem, mode, offset),
497 gen_int_mode (size, mode));
498 }
499 }
c49ad9ef 500
dd5e1e90 501 if (!wrote_return_column)
502 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
503
c49ad9ef 504#ifdef DWARF_ALT_FRAME_RETURN_COLUMN
dd5e1e90 505 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
c49ad9ef 506#endif
114a8a4b 507
508 targetm.init_dwarf_reg_sizes_extra (address);
5ff00a1d 509}
510
8a8bfbe7 511/* Convert a DWARF call frame info. operation to its string name */
30ade641 512
7795e5d1 513static const char *
8ec3a57b 514dwarf_cfi_name (unsigned int cfi_opc)
8a8bfbe7 515{
516 switch (cfi_opc)
517 {
518 case DW_CFA_advance_loc:
519 return "DW_CFA_advance_loc";
520 case DW_CFA_offset:
521 return "DW_CFA_offset";
522 case DW_CFA_restore:
523 return "DW_CFA_restore";
524 case DW_CFA_nop:
525 return "DW_CFA_nop";
526 case DW_CFA_set_loc:
527 return "DW_CFA_set_loc";
528 case DW_CFA_advance_loc1:
529 return "DW_CFA_advance_loc1";
530 case DW_CFA_advance_loc2:
531 return "DW_CFA_advance_loc2";
532 case DW_CFA_advance_loc4:
533 return "DW_CFA_advance_loc4";
534 case DW_CFA_offset_extended:
535 return "DW_CFA_offset_extended";
536 case DW_CFA_restore_extended:
537 return "DW_CFA_restore_extended";
538 case DW_CFA_undefined:
539 return "DW_CFA_undefined";
540 case DW_CFA_same_value:
541 return "DW_CFA_same_value";
542 case DW_CFA_register:
543 return "DW_CFA_register";
544 case DW_CFA_remember_state:
545 return "DW_CFA_remember_state";
546 case DW_CFA_restore_state:
547 return "DW_CFA_restore_state";
548 case DW_CFA_def_cfa:
549 return "DW_CFA_def_cfa";
550 case DW_CFA_def_cfa_register:
551 return "DW_CFA_def_cfa_register";
552 case DW_CFA_def_cfa_offset:
553 return "DW_CFA_def_cfa_offset";
15a56411 554
555 /* DWARF 3 */
4b72e226 556 case DW_CFA_def_cfa_expression:
557 return "DW_CFA_def_cfa_expression";
15a56411 558 case DW_CFA_expression:
559 return "DW_CFA_expression";
560 case DW_CFA_offset_extended_sf:
561 return "DW_CFA_offset_extended_sf";
562 case DW_CFA_def_cfa_sf:
563 return "DW_CFA_def_cfa_sf";
564 case DW_CFA_def_cfa_offset_sf:
565 return "DW_CFA_def_cfa_offset_sf";
4ad3f9b3 566
8a8bfbe7 567 /* SGI/MIPS specific */
568 case DW_CFA_MIPS_advance_loc8:
569 return "DW_CFA_MIPS_advance_loc8";
4ad3f9b3 570
571 /* GNU extensions */
572 case DW_CFA_GNU_window_save:
573 return "DW_CFA_GNU_window_save";
d757b8c9 574 case DW_CFA_GNU_args_size:
575 return "DW_CFA_GNU_args_size";
db3d4a18 576 case DW_CFA_GNU_negative_offset_extended:
577 return "DW_CFA_GNU_negative_offset_extended";
4ad3f9b3 578
8a8bfbe7 579 default:
580 return "DW_CFA_<unknown>";
581 }
582}
30ade641 583
8a8bfbe7 584/* Return a pointer to a newly allocated Call Frame Instruction. */
ec1e49cc 585
8a8bfbe7 586static inline dw_cfi_ref
8ec3a57b 587new_cfi (void)
8a8bfbe7 588{
f0af5a88 589 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
ec1e49cc 590
8a8bfbe7 591 cfi->dw_cfi_next = NULL;
592 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
593 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
30ade641 594
8a8bfbe7 595 return cfi;
596}
30ade641 597
8a8bfbe7 598/* Add a Call Frame Instruction to list of instructions. */
30ade641 599
8a8bfbe7 600static inline void
8ec3a57b 601add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
8a8bfbe7 602{
19cb6b50 603 dw_cfi_ref *p;
30ade641 604
8a8bfbe7 605 /* Find the end of the chain. */
606 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
607 ;
608
609 *p = cfi;
30ade641 610}
611
8a8bfbe7 612/* Generate a new label for the CFI info to refer to. */
ec1e49cc 613
4ad3f9b3 614char *
8ec3a57b 615dwarf2out_cfi_label (void)
30ade641 616{
8a8bfbe7 617 static char label[20];
f80d1bcd 618
9105005a 619 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
8a8bfbe7 620 ASM_OUTPUT_LABEL (asm_out_file, label);
8a8bfbe7 621 return label;
30ade641 622}
623
8a8bfbe7 624/* Add CFI to the current fde at the PC value indicated by LABEL if specified,
625 or to the CIE if LABEL is NULL. */
ec1e49cc 626
8a8bfbe7 627static void
8ec3a57b 628add_fde_cfi (const char *label, dw_cfi_ref cfi)
30ade641 629{
8a8bfbe7 630 if (label)
631 {
19cb6b50 632 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
30ade641 633
8a8bfbe7 634 if (*label == 0)
635 label = dwarf2out_cfi_label ();
ec1e49cc 636
8a8bfbe7 637 if (fde->dw_fde_current_label == NULL
638 || strcmp (label, fde->dw_fde_current_label) != 0)
639 {
19cb6b50 640 dw_cfi_ref xcfi;
30ade641 641
d8eb7025 642 label = xstrdup (label);
ec1e49cc 643
8a8bfbe7 644 /* Set the location counter to the new label. */
645 xcfi = new_cfi ();
d8eb7025 646 /* If we have a current label, advance from there, otherwise
647 set the location directly using set_loc. */
648 xcfi->dw_cfi_opc = fde->dw_fde_current_label
649 ? DW_CFA_advance_loc4
650 : DW_CFA_set_loc;
8a8bfbe7 651 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
652 add_cfi (&fde->dw_fde_cfi, xcfi);
d8eb7025 653
654 fde->dw_fde_current_label = label;
8a8bfbe7 655 }
ec1e49cc 656
8a8bfbe7 657 add_cfi (&fde->dw_fde_cfi, cfi);
658 }
659
660 else
661 add_cfi (&cie_cfi_head, cfi);
30ade641 662}
663
8a8bfbe7 664/* Subroutine of lookup_cfa. */
ec1e49cc 665
12d886b8 666static void
8ec3a57b 667lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
30ade641 668{
8a8bfbe7 669 switch (cfi->dw_cfi_opc)
670 {
671 case DW_CFA_def_cfa_offset:
4b72e226 672 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
8a8bfbe7 673 break;
da72c083 674 case DW_CFA_def_cfa_offset_sf:
675 loc->offset
676 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
677 break;
8a8bfbe7 678 case DW_CFA_def_cfa_register:
4b72e226 679 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
8a8bfbe7 680 break;
681 case DW_CFA_def_cfa:
4b72e226 682 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
683 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
684 break;
da72c083 685 case DW_CFA_def_cfa_sf:
686 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
687 loc->offset
688 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689 break;
4b72e226 690 case DW_CFA_def_cfa_expression:
691 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
8a8bfbe7 692 break;
0dbd1c74 693 default:
694 break;
8a8bfbe7 695 }
30ade641 696}
697
8a8bfbe7 698/* Find the previous value for the CFA. */
ec1e49cc 699
8a8bfbe7 700static void
8ec3a57b 701lookup_cfa (dw_cfa_location *loc)
30ade641 702{
19cb6b50 703 dw_cfi_ref cfi;
8a8bfbe7 704
12d886b8 705 loc->reg = INVALID_REGNUM;
4b72e226 706 loc->offset = 0;
707 loc->indirect = 0;
708 loc->base_offset = 0;
8a8bfbe7 709
710 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
4b72e226 711 lookup_cfa_1 (cfi, loc);
8a8bfbe7 712
713 if (fde_table_in_use)
30ade641 714 {
19cb6b50 715 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
8a8bfbe7 716 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
4b72e226 717 lookup_cfa_1 (cfi, loc);
30ade641 718 }
719}
720
8a8bfbe7 721/* The current rule for calculating the DWARF2 canonical frame address. */
b0d72d68 722static dw_cfa_location cfa;
ec1e49cc 723
8a8bfbe7 724/* The register used for saving registers to the stack, and its offset
725 from the CFA. */
b0d72d68 726static dw_cfa_location cfa_store;
8a8bfbe7 727
d757b8c9 728/* The running total of the size of arguments pushed onto the stack. */
3d867824 729static HOST_WIDE_INT args_size;
d757b8c9 730
08532d4f 731/* The last args_size we actually output. */
3d867824 732static HOST_WIDE_INT old_args_size;
08532d4f 733
8a8bfbe7 734/* Entry point to update the canonical frame address (CFA).
735 LABEL is passed to add_fde_cfi. The value of CFA is now to be
736 calculated from REG+OFFSET. */
737
738void
3d867824 739dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
4b72e226 740{
741 dw_cfa_location loc;
742 loc.indirect = 0;
743 loc.base_offset = 0;
744 loc.reg = reg;
745 loc.offset = offset;
746 def_cfa_1 (label, &loc);
747}
748
12d886b8 749/* Determine if two dw_cfa_location structures define the same data. */
750
751static bool
752cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
753{
754 return (loc1->reg == loc2->reg
755 && loc1->offset == loc2->offset
756 && loc1->indirect == loc2->indirect
757 && (loc1->indirect == 0
758 || loc1->base_offset == loc2->base_offset));
759}
760
950ae8fe 761/* This routine does the actual work. The CFA is now calculated from
4b72e226 762 the dw_cfa_location structure. */
8c3f468d 763
4b72e226 764static void
8ec3a57b 765def_cfa_1 (const char *label, dw_cfa_location *loc_p)
30ade641 766{
19cb6b50 767 dw_cfi_ref cfi;
4b72e226 768 dw_cfa_location old_cfa, loc;
8a8bfbe7 769
4b72e226 770 cfa = *loc_p;
771 loc = *loc_p;
8ab7f849 772
4b72e226 773 if (cfa_store.reg == loc.reg && loc.indirect == 0)
774 cfa_store.offset = loc.offset;
8a8bfbe7 775
4b72e226 776 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
777 lookup_cfa (&old_cfa);
778
8c3f468d 779 /* If nothing changed, no need to issue any call frame instructions. */
12d886b8 780 if (cfa_equal_p (&loc, &old_cfa))
8c3f468d 781 return;
8a8bfbe7 782
783 cfi = new_cfi ();
784
49a9983c 785 if (loc.reg == old_cfa.reg && !loc.indirect)
30ade641 786 {
da72c083 787 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
788 the CFA register did not change but the offset did. */
789 if (loc.offset < 0)
790 {
791 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
792 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
793
794 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
795 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
796 }
797 else
798 {
799 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
800 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
801 }
8a8bfbe7 802 }
30ade641 803
8a8bfbe7 804#ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
12d886b8 805 else if (loc.offset == old_cfa.offset
806 && old_cfa.reg != INVALID_REGNUM
49a9983c 807 && !loc.indirect)
8a8bfbe7 808 {
950ae8fe 809 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
810 indicating the CFA register has changed to <register> but the
811 offset has not changed. */
8a8bfbe7 812 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
4b72e226 813 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
8a8bfbe7 814 }
815#endif
30ade641 816
4b72e226 817 else if (loc.indirect == 0)
8a8bfbe7 818 {
950ae8fe 819 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
820 indicating the CFA register has changed to <register> with
821 the specified offset. */
da72c083 822 if (loc.offset < 0)
823 {
824 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
825 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
826
827 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
828 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
830 }
831 else
832 {
833 cfi->dw_cfi_opc = DW_CFA_def_cfa;
834 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
836 }
4b72e226 837 }
838 else
839 {
950ae8fe 840 /* Construct a DW_CFA_def_cfa_expression instruction to
841 calculate the CFA using a full location expression since no
842 register-offset pair is available. */
f80d1bcd 843 struct dw_loc_descr_struct *loc_list;
8c3f468d 844
4b72e226 845 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
89fa767a 846 loc_list = build_cfa_loc (&loc, 0);
4b72e226 847 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
30ade641 848 }
8a8bfbe7 849
850 add_fde_cfi (label, cfi);
30ade641 851}
852
8a8bfbe7 853/* Add the CFI for saving a register. REG is the CFA column number.
854 LABEL is passed to add_fde_cfi.
855 If SREG is -1, the register is saved at OFFSET from the CFA;
856 otherwise it is saved in SREG. */
ec1e49cc 857
8a8bfbe7 858static void
3d867824 859reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
30ade641 860{
19cb6b50 861 dw_cfi_ref cfi = new_cfi ();
8a8bfbe7 862
863 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
864
f481766a 865 if (sreg == INVALID_REGNUM)
30ade641 866 {
8a8bfbe7 867 if (reg & ~0x3f)
868 /* The register number won't fit in 6 bits, so we have to use
869 the long form. */
870 cfi->dw_cfi_opc = DW_CFA_offset_extended;
871 else
872 cfi->dw_cfi_opc = DW_CFA_offset;
873
78ac74b9 874#ifdef ENABLE_CHECKING
875 {
876 /* If we get an offset that is not a multiple of
877 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
878 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
879 description. */
3d867824 880 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
78ac74b9 881
7bd4f6b6 882 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
78ac74b9 883 }
884#endif
8a8bfbe7 885 offset /= DWARF_CIE_DATA_ALIGNMENT;
7e2bfe1e 886 if (offset < 0)
15a56411 887 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
8c3f468d 888
8a8bfbe7 889 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
890 }
220d204b 891 else if (sreg == reg)
60ea93bb 892 cfi->dw_cfi_opc = DW_CFA_same_value;
8a8bfbe7 893 else
894 {
895 cfi->dw_cfi_opc = DW_CFA_register;
896 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
897 }
898
899 add_fde_cfi (label, cfi);
900}
901
4ad3f9b3 902/* Add the CFI for saving a register window. LABEL is passed to reg_save.
903 This CFI tells the unwinder that it needs to restore the window registers
904 from the previous frame's window save area.
f80d1bcd 905
4ad3f9b3 906 ??? Perhaps we should note in the CIE where windows are saved (instead of
907 assuming 0(cfa)) and what registers are in the window. */
908
909void
8ec3a57b 910dwarf2out_window_save (const char *label)
4ad3f9b3 911{
19cb6b50 912 dw_cfi_ref cfi = new_cfi ();
8c3f468d 913
4ad3f9b3 914 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
915 add_fde_cfi (label, cfi);
916}
917
d757b8c9 918/* Add a CFI to update the running total of the size of arguments
919 pushed onto the stack. */
920
921void
3d867824 922dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
d757b8c9 923{
19cb6b50 924 dw_cfi_ref cfi;
08532d4f 925
926 if (size == old_args_size)
927 return;
8c3f468d 928
08532d4f 929 old_args_size = size;
930
931 cfi = new_cfi ();
d757b8c9 932 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
933 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
934 add_fde_cfi (label, cfi);
935}
936
4ad3f9b3 937/* Entry point for saving a register to the stack. REG is the GCC register
938 number. LABEL and OFFSET are passed to reg_save. */
8a8bfbe7 939
940void
3d867824 941dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
8a8bfbe7 942{
f481766a 943 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
8a8bfbe7 944}
945
4ad3f9b3 946/* Entry point for saving the return address in the stack.
947 LABEL and OFFSET are passed to reg_save. */
948
949void
3d867824 950dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
4ad3f9b3 951{
f481766a 952 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
4ad3f9b3 953}
954
955/* Entry point for saving the return address in a register.
956 LABEL and SREG are passed to reg_save. */
957
958void
8ec3a57b 959dwarf2out_return_reg (const char *label, unsigned int sreg)
4ad3f9b3 960{
f481766a 961 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
4ad3f9b3 962}
963
bf780b7e 964#ifdef DWARF2_UNWIND_INFO
8a8bfbe7 965/* Record the initial position of the return address. RTL is
966 INCOMING_RETURN_ADDR_RTX. */
967
968static void
8ec3a57b 969initial_return_save (rtx rtl)
8a8bfbe7 970{
f481766a 971 unsigned int reg = INVALID_REGNUM;
8c3f468d 972 HOST_WIDE_INT offset = 0;
8a8bfbe7 973
974 switch (GET_CODE (rtl))
975 {
976 case REG:
977 /* RA is in a register. */
220d204b 978 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
8a8bfbe7 979 break;
8c3f468d 980
8a8bfbe7 981 case MEM:
982 /* RA is on the stack. */
983 rtl = XEXP (rtl, 0);
984 switch (GET_CODE (rtl))
985 {
986 case REG:
7bd4f6b6 987 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
8a8bfbe7 988 offset = 0;
989 break;
8c3f468d 990
8a8bfbe7 991 case PLUS:
7bd4f6b6 992 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
8a8bfbe7 993 offset = INTVAL (XEXP (rtl, 1));
994 break;
8c3f468d 995
8a8bfbe7 996 case MINUS:
7bd4f6b6 997 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
8a8bfbe7 998 offset = -INTVAL (XEXP (rtl, 1));
999 break;
8c3f468d 1000
8a8bfbe7 1001 default:
7bd4f6b6 1002 gcc_unreachable ();
8a8bfbe7 1003 }
8c3f468d 1004
8a8bfbe7 1005 break;
8c3f468d 1006
4ad3f9b3 1007 case PLUS:
1008 /* The return address is at some offset from any value we can
1009 actually load. For instance, on the SPARC it is in %i7+8. Just
1010 ignore the offset for now; it doesn't matter for unwinding frames. */
7bd4f6b6 1011 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
4ad3f9b3 1012 initial_return_save (XEXP (rtl, 0));
1013 return;
8c3f468d 1014
30ade641 1015 default:
7bd4f6b6 1016 gcc_unreachable ();
30ade641 1017 }
8a8bfbe7 1018
60ea93bb 1019 if (reg != DWARF_FRAME_RETURN_COLUMN)
1020 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
30ade641 1021}
bf780b7e 1022#endif
30ade641 1023
6ee89c56 1024/* Given a SET, calculate the amount of stack adjustment it
6312a35e 1025 contains. */
6ee89c56 1026
3d867824 1027static HOST_WIDE_INT
8ec3a57b 1028stack_adjust_offset (rtx pattern)
6ee89c56 1029{
1030 rtx src = SET_SRC (pattern);
1031 rtx dest = SET_DEST (pattern);
8c3f468d 1032 HOST_WIDE_INT offset = 0;
6ee89c56 1033 enum rtx_code code;
1034
1035 if (dest == stack_pointer_rtx)
1036 {
1037 /* (set (reg sp) (plus (reg sp) (const_int))) */
1038 code = GET_CODE (src);
1039 if (! (code == PLUS || code == MINUS)
1040 || XEXP (src, 0) != stack_pointer_rtx
1041 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1042 return 0;
1043
1044 offset = INTVAL (XEXP (src, 1));
052c7a5c 1045 if (code == PLUS)
1046 offset = -offset;
6ee89c56 1047 }
e16ceb8e 1048 else if (MEM_P (dest))
6ee89c56 1049 {
1050 /* (set (mem (pre_dec (reg sp))) (foo)) */
1051 src = XEXP (dest, 0);
1052 code = GET_CODE (src);
1053
bc70bd5e 1054 switch (code)
1055 {
052c7a5c 1056 case PRE_MODIFY:
1057 case POST_MODIFY:
1058 if (XEXP (src, 0) == stack_pointer_rtx)
1059 {
1060 rtx val = XEXP (XEXP (src, 1), 1);
1061 /* We handle only adjustments by constant amount. */
7bd4f6b6 1062 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1063 && GET_CODE (val) == CONST_INT);
052c7a5c 1064 offset = -INTVAL (val);
1065 break;
1066 }
1067 return 0;
1068
1069 case PRE_DEC:
1070 case POST_DEC:
1071 if (XEXP (src, 0) == stack_pointer_rtx)
1072 {
1073 offset = GET_MODE_SIZE (GET_MODE (dest));
1074 break;
1075 }
1076 return 0;
1077
1078 case PRE_INC:
1079 case POST_INC:
1080 if (XEXP (src, 0) == stack_pointer_rtx)
1081 {
1082 offset = -GET_MODE_SIZE (GET_MODE (dest));
1083 break;
1084 }
1085 return 0;
8c3f468d 1086
052c7a5c 1087 default:
1088 return 0;
93fbe1f3 1089 }
6ee89c56 1090 }
1091 else
1092 return 0;
1093
6ee89c56 1094 return offset;
1095}
1096
d757b8c9 1097/* Check INSN to see if it looks like a push or a stack adjustment, and
1098 make a note of it if it does. EH uses this information to find out how
1099 much extra space it needs to pop off the stack. */
1100
1101static void
46b2b3c8 1102dwarf2out_stack_adjust (rtx insn, bool after_p)
d757b8c9 1103{
8c3f468d 1104 HOST_WIDE_INT offset;
1e034a40 1105 const char *label;
8c3f468d 1106 int i;
d757b8c9 1107
31b1fbc5 1108 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1109 with this function. Proper support would require all frame-related
1110 insns to be marked, and to be able to handle saving state around
1111 epilogues textually in the middle of the function. */
1112 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1113 return;
1114
46b2b3c8 1115 /* If only calls can throw, and we have a frame pointer,
1116 save up adjustments until we see the CALL_INSN. */
1117 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1118 {
1119 if (CALL_P (insn) && !after_p)
1120 {
1121 /* Extract the size of the args from the CALL rtx itself. */
1122 insn = PATTERN (insn);
1123 if (GET_CODE (insn) == PARALLEL)
1124 insn = XVECEXP (insn, 0, 0);
1125 if (GET_CODE (insn) == SET)
1126 insn = SET_SRC (insn);
1127 gcc_assert (GET_CODE (insn) == CALL);
1128 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1129 }
1130 return;
1131 }
1132
1133 if (CALL_P (insn) && !after_p)
1134 {
1135 if (!flag_asynchronous_unwind_tables)
1136 dwarf2out_args_size ("", args_size);
1137 return;
1138 }
1139 else if (BARRIER_P (insn))
d757b8c9 1140 {
24db2725 1141 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1142 the compiler will have already emitted a stack adjustment, but
1143 doesn't bother for calls to noreturn functions. */
1144#ifdef STACK_GROWS_DOWNWARD
1145 offset = -args_size;
1146#else
1147 offset = args_size;
1148#endif
d757b8c9 1149 }
24db2725 1150 else if (GET_CODE (PATTERN (insn)) == SET)
8c3f468d 1151 offset = stack_adjust_offset (PATTERN (insn));
6ee89c56 1152 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1153 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1154 {
1155 /* There may be stack adjustments inside compound insns. Search
8c3f468d 1156 for them. */
1157 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1158 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1159 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
d757b8c9 1160 }
1161 else
1162 return;
ac02093f 1163
24db2725 1164 if (offset == 0)
1165 return;
1166
4b72e226 1167 if (cfa.reg == STACK_POINTER_REGNUM)
1168 cfa.offset += offset;
d757b8c9 1169
1170#ifndef STACK_GROWS_DOWNWARD
1171 offset = -offset;
1172#endif
8c3f468d 1173
d757b8c9 1174 args_size += offset;
1175 if (args_size < 0)
1176 args_size = 0;
1177
1178 label = dwarf2out_cfi_label ();
4b72e226 1179 def_cfa_1 (label, &cfa);
535fcfa4 1180 if (flag_asynchronous_unwind_tables)
1181 dwarf2out_args_size (label, args_size);
d757b8c9 1182}
1183
573aba85 1184#endif
1185
b0d72d68 1186/* We delay emitting a register save until either (a) we reach the end
1187 of the prologue or (b) the register is clobbered. This clusters
1188 register saves so that there are fewer pc advances. */
1189
573aba85 1190struct queued_reg_save GTY(())
b0d72d68 1191{
1192 struct queued_reg_save *next;
1193 rtx reg;
3d867824 1194 HOST_WIDE_INT cfa_offset;
60ea93bb 1195 rtx saved_reg;
b0d72d68 1196};
1197
573aba85 1198static GTY(()) struct queued_reg_save *queued_reg_saves;
1199
60ea93bb 1200/* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1201struct reg_saved_in_data GTY(()) {
1202 rtx orig_reg;
1203 rtx saved_in_reg;
1204};
1205
1206/* A list of registers saved in other registers.
1207 The list intentionally has a small maximum capacity of 4; if your
1208 port needs more than that, you might consider implementing a
1209 more efficient data structure. */
1210static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1211static GTY(()) size_t num_regs_saved_in_regs;
8ff30ff6 1212
573aba85 1213#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
b0d72d68 1214static const char *last_reg_save_label;
1215
60ea93bb 1216/* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1217 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1218
b0d72d68 1219static void
60ea93bb 1220queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
b0d72d68 1221{
60ea93bb 1222 struct queued_reg_save *q;
1223
1224 /* Duplicates waste space, but it's also necessary to remove them
1225 for correctness, since the queue gets output in reverse
1226 order. */
1227 for (q = queued_reg_saves; q != NULL; q = q->next)
1228 if (REGNO (q->reg) == REGNO (reg))
1229 break;
1230
1231 if (q == NULL)
1232 {
1233 q = ggc_alloc (sizeof (*q));
1234 q->next = queued_reg_saves;
1235 queued_reg_saves = q;
1236 }
b0d72d68 1237
b0d72d68 1238 q->reg = reg;
1239 q->cfa_offset = offset;
60ea93bb 1240 q->saved_reg = sreg;
b0d72d68 1241
1242 last_reg_save_label = label;
1243}
1244
60ea93bb 1245/* Output all the entries in QUEUED_REG_SAVES. */
1246
b0d72d68 1247static void
8ec3a57b 1248flush_queued_reg_saves (void)
b0d72d68 1249{
60ea93bb 1250 struct queued_reg_save *q;
b0d72d68 1251
60ea93bb 1252 for (q = queued_reg_saves; q; q = q->next)
b0d72d68 1253 {
60ea93bb 1254 size_t i;
f481766a 1255 unsigned int reg, sreg;
1256
60ea93bb 1257 for (i = 0; i < num_regs_saved_in_regs; i++)
1258 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1259 break;
1260 if (q->saved_reg && i == num_regs_saved_in_regs)
1261 {
7bd4f6b6 1262 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
60ea93bb 1263 num_regs_saved_in_regs++;
1264 }
1265 if (i != num_regs_saved_in_regs)
1266 {
1267 regs_saved_in_regs[i].orig_reg = q->reg;
1268 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1269 }
1270
f481766a 1271 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1272 if (q->saved_reg)
1273 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1274 else
1275 sreg = INVALID_REGNUM;
1276 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
b0d72d68 1277 }
1278
1279 queued_reg_saves = NULL;
1280 last_reg_save_label = NULL;
1281}
1282
60ea93bb 1283/* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1284 location for? Or, does it clobber a register which we've previously
1285 said that some other register is saved in, and for which we now
1286 have a new location for? */
1287
b0d72d68 1288static bool
8ec3a57b 1289clobbers_queued_reg_save (rtx insn)
b0d72d68 1290{
1291 struct queued_reg_save *q;
1292
bc70bd5e 1293 for (q = queued_reg_saves; q; q = q->next)
60ea93bb 1294 {
1295 size_t i;
1296 if (modified_in_p (q->reg, insn))
1297 return true;
1298 for (i = 0; i < num_regs_saved_in_regs; i++)
1299 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1300 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1301 return true;
1302 }
b0d72d68 1303
1304 return false;
1305}
bc70bd5e 1306
567925e3 1307/* Entry point for saving the first register into the second. */
1308
1309void
1310dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1311{
1312 size_t i;
1313 unsigned int regno, sregno;
1314
1315 for (i = 0; i < num_regs_saved_in_regs; i++)
1316 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1317 break;
1318 if (i == num_regs_saved_in_regs)
1319 {
1320 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1321 num_regs_saved_in_regs++;
1322 }
1323 regs_saved_in_regs[i].orig_reg = reg;
1324 regs_saved_in_regs[i].saved_in_reg = sreg;
1325
1326 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1327 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1328 reg_save (label, regno, sregno, 0);
1329}
1330
60ea93bb 1331/* What register, if any, is currently saved in REG? */
1332
1333static rtx
1334reg_saved_in (rtx reg)
1335{
1336 unsigned int regn = REGNO (reg);
1337 size_t i;
1338 struct queued_reg_save *q;
8ff30ff6 1339
60ea93bb 1340 for (q = queued_reg_saves; q; q = q->next)
1341 if (q->saved_reg && regn == REGNO (q->saved_reg))
1342 return q->reg;
1343
1344 for (i = 0; i < num_regs_saved_in_regs; i++)
1345 if (regs_saved_in_regs[i].saved_in_reg
1346 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1347 return regs_saved_in_regs[i].orig_reg;
1348
1349 return NULL_RTX;
1350}
1351
b0d72d68 1352
950ae8fe 1353/* A temporary register holding an integral value used in adjusting SP
1354 or setting up the store_reg. The "offset" field holds the integer
1355 value, not an offset. */
b0d72d68 1356static dw_cfa_location cfa_temp;
950ae8fe 1357
1358/* Record call frame debugging information for an expression EXPR,
1359 which either sets SP or FP (adjusting how we calculate the frame
60ea93bb 1360 address) or saves a register to the stack or another register.
1361 LABEL indicates the address of EXPR.
950ae8fe 1362
1363 This function encodes a state machine mapping rtxes to actions on
1364 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1365 users need not read the source code.
1366
ae8c6892 1367 The High-Level Picture
1368
1369 Changes in the register we use to calculate the CFA: Currently we
1370 assume that if you copy the CFA register into another register, we
1371 should take the other one as the new CFA register; this seems to
1372 work pretty well. If it's wrong for some target, it's simple
1373 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1374
1375 Changes in the register we use for saving registers to the stack:
1376 This is usually SP, but not always. Again, we deduce that if you
1377 copy SP into another register (and SP is not the CFA register),
1378 then the new register is the one we will be using for register
1379 saves. This also seems to work.
1380
1381 Register saves: There's not much guesswork about this one; if
1382 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1383 register save, and the register used to calculate the destination
1384 had better be the one we think we're using for this purpose.
60ea93bb 1385 It's also assumed that a copy from a call-saved register to another
1386 register is saving that register if RTX_FRAME_RELATED_P is set on
1387 that instruction. If the copy is from a call-saved register to
1388 the *same* register, that means that the register is now the same
1389 value as in the caller.
ae8c6892 1390
1391 Except: If the register being saved is the CFA register, and the
6ef828f9 1392 offset is nonzero, we are saving the CFA, so we assume we have to
ae8c6892 1393 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1394 the intent is to save the value of SP from the previous frame.
1395
60ea93bb 1396 In addition, if a register has previously been saved to a different
8ff30ff6 1397 register,
60ea93bb 1398
950ae8fe 1399 Invariants / Summaries of Rules
1400
ae8c6892 1401 cfa current rule for calculating the CFA. It usually
1402 consists of a register and an offset.
950ae8fe 1403 cfa_store register used by prologue code to save things to the stack
1404 cfa_store.offset is the offset from the value of
1405 cfa_store.reg to the actual CFA
1406 cfa_temp register holding an integral value. cfa_temp.offset
1407 stores the value, which will be used to adjust the
cc858176 1408 stack pointer. cfa_temp is also used like cfa_store,
1409 to track stores to the stack via fp or a temp reg.
bc70bd5e 1410
950ae8fe 1411 Rules 1- 4: Setting a register's value to cfa.reg or an expression
8ec3a57b 1412 with cfa.reg as the first operand changes the cfa.reg and its
cc858176 1413 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1414 cfa_temp.offset.
950ae8fe 1415
1416 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1417 expression yielding a constant. This sets cfa_temp.reg
1418 and cfa_temp.offset.
1419
1420 Rule 5: Create a new register cfa_store used to save items to the
1421 stack.
1422
cc858176 1423 Rules 10-14: Save a register to the stack. Define offset as the
ae8c6892 1424 difference of the original location and cfa_store's
cc858176 1425 location (or cfa_temp's location if cfa_temp is used).
950ae8fe 1426
1427 The Rules
1428
1429 "{a,b}" indicates a choice of a xor b.
1430 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1431
1432 Rule 1:
1433 (set <reg1> <reg2>:cfa.reg)
cc858176 1434 effects: cfa.reg = <reg1>
c83a163c 1435 cfa.offset unchanged
cc858176 1436 cfa_temp.reg = <reg1>
1437 cfa_temp.offset = cfa.offset
950ae8fe 1438
1439 Rule 2:
8c3f468d 1440 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1441 {<const_int>,<reg>:cfa_temp.reg}))
950ae8fe 1442 effects: cfa.reg = sp if fp used
8ec3a57b 1443 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
950ae8fe 1444 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1445 if cfa_store.reg==sp
1446
1447 Rule 3:
cc858176 1448 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
950ae8fe 1449 effects: cfa.reg = fp
8ec3a57b 1450 cfa_offset += +/- <const_int>
950ae8fe 1451
1452 Rule 4:
cc858176 1453 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
950ae8fe 1454 constraints: <reg1> != fp
8ec3a57b 1455 <reg1> != sp
950ae8fe 1456 effects: cfa.reg = <reg1>
cc858176 1457 cfa_temp.reg = <reg1>
1458 cfa_temp.offset = cfa.offset
950ae8fe 1459
1460 Rule 5:
1461 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1462 constraints: <reg1> != fp
8ec3a57b 1463 <reg1> != sp
950ae8fe 1464 effects: cfa_store.reg = <reg1>
8ec3a57b 1465 cfa_store.offset = cfa.offset - cfa_temp.offset
950ae8fe 1466
1467 Rule 6:
1468 (set <reg> <const_int>)
1469 effects: cfa_temp.reg = <reg>
8ec3a57b 1470 cfa_temp.offset = <const_int>
950ae8fe 1471
1472 Rule 7:
1473 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1474 effects: cfa_temp.reg = <reg1>
1475 cfa_temp.offset |= <const_int>
1476
1477 Rule 8:
1478 (set <reg> (high <exp>))
1479 effects: none
1480
1481 Rule 9:
1482 (set <reg> (lo_sum <exp> <const_int>))
1483 effects: cfa_temp.reg = <reg>
8ec3a57b 1484 cfa_temp.offset = <const_int>
950ae8fe 1485
1486 Rule 10:
1487 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1488 effects: cfa_store.offset -= <const_int>
1489 cfa.offset = cfa_store.offset if cfa.reg == sp
950ae8fe 1490 cfa.reg = sp
cc858176 1491 cfa.base_offset = -cfa_store.offset
950ae8fe 1492
1493 Rule 11:
1494 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1495 effects: cfa_store.offset += -/+ mode_size(mem)
1496 cfa.offset = cfa_store.offset if cfa.reg == sp
950ae8fe 1497 cfa.reg = sp
cc858176 1498 cfa.base_offset = -cfa_store.offset
950ae8fe 1499
1500 Rule 12:
8c3f468d 1501 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1502
1503 <reg2>)
cc858176 1504 effects: cfa.reg = <reg1>
1505 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
950ae8fe 1506
1507 Rule 13:
cc858176 1508 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1509 effects: cfa.reg = <reg1>
1510 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1511
1512 Rule 14:
1513 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1514 effects: cfa.reg = <reg1>
1515 cfa.base_offset = -cfa_temp.offset
d15ee1a5 1516 cfa_temp.offset -= mode_size(mem)
1517
85fdc672 1518 Rule 15:
1519 (set <reg> {unspec, unspec_volatile})
1520 effects: target-dependent */
fa19b467 1521
1522static void
8ec3a57b 1523dwarf2out_frame_debug_expr (rtx expr, const char *label)
fa19b467 1524{
1525 rtx src, dest;
8c3f468d 1526 HOST_WIDE_INT offset;
f80d1bcd 1527
1528 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1529 the PARALLEL independently. The first element is always processed if
950ae8fe 1530 it is a SET. This is for backward compatibility. Other elements
f80d1bcd 1531 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1532 flag is set in them. */
8c3f468d 1533 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
f80d1bcd 1534 {
fa19b467 1535 int par_index;
1536 int limit = XVECLEN (expr, 0);
a1d50f1d 1537 rtx elem;
1538
1539 /* PARALLELs have strict read-modify-write semantics, so we
1540 ought to evaluate every rvalue before changing any lvalue.
1541 It's cumbersome to do that in general, but there's an
1542 easy approximation that is enough for all current users:
1543 handle register saves before register assignments. */
1544 if (GET_CODE (expr) == PARALLEL)
1545 for (par_index = 0; par_index < limit; par_index++)
1546 {
1547 elem = XVECEXP (expr, 0, par_index);
1548 if (GET_CODE (elem) == SET
1549 && MEM_P (SET_DEST (elem))
1550 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1551 dwarf2out_frame_debug_expr (elem, label);
1552 }
fa19b467 1553
1554 for (par_index = 0; par_index < limit; par_index++)
a1d50f1d 1555 {
1556 elem = XVECEXP (expr, 0, par_index);
1557 if (GET_CODE (elem) == SET
1558 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1559 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1560 dwarf2out_frame_debug_expr (elem, label);
1561 }
fa19b467 1562 return;
1563 }
f80d1bcd 1564
7bd4f6b6 1565 gcc_assert (GET_CODE (expr) == SET);
fa19b467 1566
1567 src = SET_SRC (expr);
1568 dest = SET_DEST (expr);
1569
1c14a50e 1570 if (REG_P (src))
60ea93bb 1571 {
1572 rtx rsi = reg_saved_in (src);
1573 if (rsi)
1574 src = rsi;
1575 }
1576
fa19b467 1577 switch (GET_CODE (dest))
1578 {
1579 case REG:
fa19b467 1580 switch (GET_CODE (src))
f80d1bcd 1581 {
1582 /* Setting FP from SP. */
1583 case REG:
1584 if (cfa.reg == (unsigned) REGNO (src))
60ea93bb 1585 {
1586 /* Rule 1 */
1587 /* Update the CFA rule wrt SP or FP. Make sure src is
8ff30ff6 1588 relative to the current CFA register.
60ea93bb 1589
1590 We used to require that dest be either SP or FP, but the
1591 ARM copies SP to a temporary register, and from there to
1592 FP. So we just rely on the backends to only set
1593 RTX_FRAME_RELATED_P on appropriate insns. */
1594 cfa.reg = REGNO (dest);
1595 cfa_temp.reg = cfa.reg;
1596 cfa_temp.offset = cfa.offset;
1597 }
7bd4f6b6 1598 else
60ea93bb 1599 {
1600 /* Saving a register in a register. */
ed86dceb 1601 gcc_assert (!fixed_regs [REGNO (dest)]
1602 /* For the SPARC and its register window. */
1603 || (DWARF_FRAME_REGNUM (REGNO (src))
1604 == DWARF_FRAME_RETURN_COLUMN));
60ea93bb 1605 queue_reg_save (label, src, dest, 0);
1606 }
f80d1bcd 1607 break;
fa19b467 1608
f80d1bcd 1609 case PLUS:
1610 case MINUS:
cc858176 1611 case LO_SUM:
f80d1bcd 1612 if (dest == stack_pointer_rtx)
1613 {
950ae8fe 1614 /* Rule 2 */
31306376 1615 /* Adjusting SP. */
1616 switch (GET_CODE (XEXP (src, 1)))
1617 {
1618 case CONST_INT:
1619 offset = INTVAL (XEXP (src, 1));
1620 break;
1621 case REG:
7bd4f6b6 1622 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1623 == cfa_temp.reg);
950ae8fe 1624 offset = cfa_temp.offset;
31306376 1625 break;
1626 default:
7bd4f6b6 1627 gcc_unreachable ();
31306376 1628 }
1629
1630 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1631 {
1632 /* Restoring SP from FP in the epilogue. */
7bd4f6b6 1633 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
4b72e226 1634 cfa.reg = STACK_POINTER_REGNUM;
31306376 1635 }
cc858176 1636 else if (GET_CODE (src) == LO_SUM)
1637 /* Assume we've set the source reg of the LO_SUM from sp. */
1638 ;
7bd4f6b6 1639 else
1640 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
31306376 1641
cc858176 1642 if (GET_CODE (src) != MINUS)
31306376 1643 offset = -offset;
4b72e226 1644 if (cfa.reg == STACK_POINTER_REGNUM)
1645 cfa.offset += offset;
1646 if (cfa_store.reg == STACK_POINTER_REGNUM)
1647 cfa_store.offset += offset;
f80d1bcd 1648 }
1649 else if (dest == hard_frame_pointer_rtx)
1650 {
950ae8fe 1651 /* Rule 3 */
31306376 1652 /* Either setting the FP from an offset of the SP,
1653 or adjusting the FP */
7bd4f6b6 1654 gcc_assert (frame_pointer_needed);
31306376 1655
7bd4f6b6 1656 gcc_assert (REG_P (XEXP (src, 0))
1657 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1658 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1659 offset = INTVAL (XEXP (src, 1));
1660 if (GET_CODE (src) != MINUS)
1661 offset = -offset;
1662 cfa.offset += offset;
1663 cfa.reg = HARD_FRAME_POINTER_REGNUM;
f80d1bcd 1664 }
1665 else
1666 {
7bd4f6b6 1667 gcc_assert (GET_CODE (src) != MINUS);
4747ea36 1668
950ae8fe 1669 /* Rule 4 */
8ad4c111 1670 if (REG_P (XEXP (src, 0))
4747ea36 1671 && REGNO (XEXP (src, 0)) == cfa.reg
1672 && GET_CODE (XEXP (src, 1)) == CONST_INT)
9b536fa6 1673 {
1674 /* Setting a temporary CFA register that will be copied
1675 into the FP later on. */
cc858176 1676 offset = - INTVAL (XEXP (src, 1));
9b536fa6 1677 cfa.offset += offset;
1678 cfa.reg = REGNO (dest);
cc858176 1679 /* Or used to save regs to the stack. */
1680 cfa_temp.reg = cfa.reg;
1681 cfa_temp.offset = cfa.offset;
9b536fa6 1682 }
8c3f468d 1683
950ae8fe 1684 /* Rule 5 */
8ad4c111 1685 else if (REG_P (XEXP (src, 0))
cc858176 1686 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1687 && XEXP (src, 1) == stack_pointer_rtx)
4747ea36 1688 {
ca6c45a9 1689 /* Setting a scratch register that we will use instead
1690 of SP for saving registers to the stack. */
7bd4f6b6 1691 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
4747ea36 1692 cfa_store.reg = REGNO (dest);
950ae8fe 1693 cfa_store.offset = cfa.offset - cfa_temp.offset;
4747ea36 1694 }
8c3f468d 1695
cc858176 1696 /* Rule 9 */
1697 else if (GET_CODE (src) == LO_SUM
1698 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1699 {
1700 cfa_temp.reg = REGNO (dest);
1701 cfa_temp.offset = INTVAL (XEXP (src, 1));
1702 }
1703 else
7bd4f6b6 1704 gcc_unreachable ();
f80d1bcd 1705 }
1706 break;
fa19b467 1707
950ae8fe 1708 /* Rule 6 */
f80d1bcd 1709 case CONST_INT:
950ae8fe 1710 cfa_temp.reg = REGNO (dest);
1711 cfa_temp.offset = INTVAL (src);
f80d1bcd 1712 break;
fa19b467 1713
950ae8fe 1714 /* Rule 7 */
f80d1bcd 1715 case IOR:
7bd4f6b6 1716 gcc_assert (REG_P (XEXP (src, 0))
1717 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1718 && GET_CODE (XEXP (src, 1)) == CONST_INT);
8c3f468d 1719
950ae8fe 1720 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1721 cfa_temp.reg = REGNO (dest);
1722 cfa_temp.offset |= INTVAL (XEXP (src, 1));
f80d1bcd 1723 break;
fa19b467 1724
e0cedf2c 1725 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1726 which will fill in all of the bits. */
1727 /* Rule 8 */
1728 case HIGH:
1729 break;
1730
d15ee1a5 1731 /* Rule 15 */
1732 case UNSPEC:
1733 case UNSPEC_VOLATILE:
1734 gcc_assert (targetm.dwarf_handle_frame_unspec);
1735 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
567925e3 1736 return;
d15ee1a5 1737
f80d1bcd 1738 default:
7bd4f6b6 1739 gcc_unreachable ();
f80d1bcd 1740 }
8c3f468d 1741
4b72e226 1742 def_cfa_1 (label, &cfa);
31306376 1743 break;
fa19b467 1744
31306376 1745 case MEM:
7bd4f6b6 1746 gcc_assert (REG_P (src));
4b72e226 1747
4b72e226 1748 /* Saving a register to the stack. Make sure dest is relative to the
1749 CFA register. */
31306376 1750 switch (GET_CODE (XEXP (dest, 0)))
1751 {
950ae8fe 1752 /* Rule 10 */
31306376 1753 /* With a push. */
93fbe1f3 1754 case PRE_MODIFY:
1755 /* We can't handle variable size modifications. */
7bd4f6b6 1756 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1757 == CONST_INT);
93fbe1f3 1758 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1759
7bd4f6b6 1760 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1761 && cfa_store.reg == STACK_POINTER_REGNUM);
8c3f468d 1762
93fbe1f3 1763 cfa_store.offset += offset;
1764 if (cfa.reg == STACK_POINTER_REGNUM)
1765 cfa.offset = cfa_store.offset;
1766
1767 offset = -cfa_store.offset;
1768 break;
8c3f468d 1769
950ae8fe 1770 /* Rule 11 */
31306376 1771 case PRE_INC:
1772 case PRE_DEC:
1773 offset = GET_MODE_SIZE (GET_MODE (dest));
1774 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1775 offset = -offset;
fa19b467 1776
7bd4f6b6 1777 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1778 && cfa_store.reg == STACK_POINTER_REGNUM);
8c3f468d 1779
4b72e226 1780 cfa_store.offset += offset;
1781 if (cfa.reg == STACK_POINTER_REGNUM)
1782 cfa.offset = cfa_store.offset;
fa19b467 1783
4b72e226 1784 offset = -cfa_store.offset;
31306376 1785 break;
fa19b467 1786
950ae8fe 1787 /* Rule 12 */
31306376 1788 /* With an offset. */
1789 case PLUS:
1790 case MINUS:
cc858176 1791 case LO_SUM:
7bd4f6b6 1792 {
1793 int regno;
8ff30ff6 1794
ccb88806 1795 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1796 && REG_P (XEXP (XEXP (dest, 0), 0)));
7bd4f6b6 1797 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1798 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1799 offset = -offset;
1800
1801 regno = REGNO (XEXP (XEXP (dest, 0), 0));
8ff30ff6 1802
7bd4f6b6 1803 if (cfa_store.reg == (unsigned) regno)
1804 offset -= cfa_store.offset;
1805 else
1806 {
1807 gcc_assert (cfa_temp.reg == (unsigned) regno);
1808 offset -= cfa_temp.offset;
1809 }
1810 }
31306376 1811 break;
1812
950ae8fe 1813 /* Rule 13 */
31306376 1814 /* Without an offset. */
1815 case REG:
7bd4f6b6 1816 {
1817 int regno = REGNO (XEXP (dest, 0));
8ff30ff6 1818
7bd4f6b6 1819 if (cfa_store.reg == (unsigned) regno)
1820 offset = -cfa_store.offset;
1821 else
1822 {
1823 gcc_assert (cfa_temp.reg == (unsigned) regno);
1824 offset = -cfa_temp.offset;
1825 }
1826 }
cc858176 1827 break;
1828
1829 /* Rule 14 */
1830 case POST_INC:
7bd4f6b6 1831 gcc_assert (cfa_temp.reg
1832 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
cc858176 1833 offset = -cfa_temp.offset;
1834 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
31306376 1835 break;
1836
1837 default:
7bd4f6b6 1838 gcc_unreachable ();
31306376 1839 }
49a9983c 1840
f80d1bcd 1841 if (REGNO (src) != STACK_POINTER_REGNUM
49a9983c 1842 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1843 && (unsigned) REGNO (src) == cfa.reg)
1844 {
1845 /* We're storing the current CFA reg into the stack. */
1846
1847 if (cfa.offset == 0)
1848 {
1849 /* If the source register is exactly the CFA, assume
1850 we're saving SP like any other register; this happens
1851 on the ARM. */
49a9983c 1852 def_cfa_1 (label, &cfa);
60ea93bb 1853 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
49a9983c 1854 break;
1855 }
1856 else
1857 {
1858 /* Otherwise, we'll need to look in the stack to
c83a163c 1859 calculate the CFA. */
49a9983c 1860 rtx x = XEXP (dest, 0);
8c3f468d 1861
8ad4c111 1862 if (!REG_P (x))
49a9983c 1863 x = XEXP (x, 0);
7bd4f6b6 1864 gcc_assert (REG_P (x));
8c3f468d 1865
1866 cfa.reg = REGNO (x);
49a9983c 1867 cfa.base_offset = offset;
1868 cfa.indirect = 1;
1869 def_cfa_1 (label, &cfa);
1870 break;
1871 }
1872 }
1873
4b72e226 1874 def_cfa_1 (label, &cfa);
60ea93bb 1875 queue_reg_save (label, src, NULL_RTX, offset);
31306376 1876 break;
1877
1878 default:
7bd4f6b6 1879 gcc_unreachable ();
31306376 1880 }
fa19b467 1881}
1882
8a8bfbe7 1883/* Record call frame debugging information for INSN, which either
1884 sets SP or FP (adjusting how we calculate the frame address) or saves a
535fcfa4 1885 register to the stack. If INSN is NULL_RTX, initialize our state.
1886
1887 If AFTER_P is false, we're being called before the insn is emitted,
1888 otherwise after. Call instructions get invoked twice. */
ec1e49cc 1889
8a8bfbe7 1890void
535fcfa4 1891dwarf2out_frame_debug (rtx insn, bool after_p)
30ade641 1892{
1e034a40 1893 const char *label;
fa19b467 1894 rtx src;
8a8bfbe7 1895
1896 if (insn == NULL_RTX)
30ade641 1897 {
60ea93bb 1898 size_t i;
8ff30ff6 1899
b0d72d68 1900 /* Flush any queued register saves. */
1901 flush_queued_reg_saves ();
1902
8a8bfbe7 1903 /* Set up state for generating call frame debug info. */
4b72e226 1904 lookup_cfa (&cfa);
7bd4f6b6 1905 gcc_assert (cfa.reg
1906 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
8c3f468d 1907
4b72e226 1908 cfa.reg = STACK_POINTER_REGNUM;
1909 cfa_store = cfa;
950ae8fe 1910 cfa_temp.reg = -1;
1911 cfa_temp.offset = 0;
8ff30ff6 1912
60ea93bb 1913 for (i = 0; i < num_regs_saved_in_regs; i++)
1914 {
1915 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1916 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1917 }
1918 num_regs_saved_in_regs = 0;
8a8bfbe7 1919 return;
1920 }
1921
6d7dc5b9 1922 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
b0d72d68 1923 flush_queued_reg_saves ();
1924
d757b8c9 1925 if (! RTX_FRAME_RELATED_P (insn))
1926 {
b0d72d68 1927 if (!ACCUMULATE_OUTGOING_ARGS)
535fcfa4 1928 dwarf2out_stack_adjust (insn, after_p);
d757b8c9 1929 return;
1930 }
1931
8a8bfbe7 1932 label = dwarf2out_cfi_label ();
86b18255 1933 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1934 if (src)
1935 insn = XEXP (src, 0);
f80d1bcd 1936 else
86b18255 1937 insn = PATTERN (insn);
1938
fa19b467 1939 dwarf2out_frame_debug_expr (insn, label);
8a8bfbe7 1940}
1941
573aba85 1942#endif
1943
1944/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
8ec3a57b 1945static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1946 (enum dwarf_call_frame_info cfi);
573aba85 1947
1948static enum dw_cfi_oprnd_type
8ec3a57b 1949dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
573aba85 1950{
1951 switch (cfi)
1952 {
1953 case DW_CFA_nop:
1954 case DW_CFA_GNU_window_save:
1955 return dw_cfi_oprnd_unused;
1956
1957 case DW_CFA_set_loc:
1958 case DW_CFA_advance_loc1:
1959 case DW_CFA_advance_loc2:
1960 case DW_CFA_advance_loc4:
1961 case DW_CFA_MIPS_advance_loc8:
1962 return dw_cfi_oprnd_addr;
1963
1964 case DW_CFA_offset:
1965 case DW_CFA_offset_extended:
1966 case DW_CFA_def_cfa:
1967 case DW_CFA_offset_extended_sf:
1968 case DW_CFA_def_cfa_sf:
1969 case DW_CFA_restore_extended:
1970 case DW_CFA_undefined:
1971 case DW_CFA_same_value:
1972 case DW_CFA_def_cfa_register:
1973 case DW_CFA_register:
1974 return dw_cfi_oprnd_reg_num;
1975
1976 case DW_CFA_def_cfa_offset:
1977 case DW_CFA_GNU_args_size:
1978 case DW_CFA_def_cfa_offset_sf:
1979 return dw_cfi_oprnd_offset;
8ec3a57b 1980
573aba85 1981 case DW_CFA_def_cfa_expression:
1982 case DW_CFA_expression:
1983 return dw_cfi_oprnd_loc;
1984
1985 default:
7bd4f6b6 1986 gcc_unreachable ();
573aba85 1987 }
1988}
1989
1990/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
8ec3a57b 1991static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1992 (enum dwarf_call_frame_info cfi);
573aba85 1993
1994static enum dw_cfi_oprnd_type
8ec3a57b 1995dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
573aba85 1996{
1997 switch (cfi)
1998 {
1999 case DW_CFA_def_cfa:
2000 case DW_CFA_def_cfa_sf:
2001 case DW_CFA_offset:
2002 case DW_CFA_offset_extended_sf:
2003 case DW_CFA_offset_extended:
2004 return dw_cfi_oprnd_offset;
2005
2006 case DW_CFA_register:
2007 return dw_cfi_oprnd_reg_num;
2008
2009 default:
2010 return dw_cfi_oprnd_unused;
2011 }
2012}
2013
2014#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2015
2f14b1f9 2016/* Switch to eh_frame_section. If we don't have an eh_frame_section,
2017 switch to the data section instead, and write out a synthetic label
2018 for collect2. */
2019
2020static void
2021switch_to_eh_frame_section (void)
2022{
2023 tree label;
2024
2943ce06 2025#ifdef EH_FRAME_SECTION_NAME
2026 if (eh_frame_section == 0)
2027 {
2028 int flags;
2029
2030 if (EH_TABLES_CAN_BE_READ_ONLY)
2031 {
2032 int fde_encoding;
2033 int per_encoding;
2034 int lsda_encoding;
2035
2036 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2037 /*global=*/0);
2038 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2039 /*global=*/1);
2040 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2041 /*global=*/0);
2042 flags = ((! flag_pic
2043 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2044 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2045 && (per_encoding & 0x70) != DW_EH_PE_absptr
2046 && (per_encoding & 0x70) != DW_EH_PE_aligned
2047 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2048 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2049 ? 0 : SECTION_WRITE);
2050 }
2051 else
2052 flags = SECTION_WRITE;
2053 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2054 }
2055#endif
2056
2f14b1f9 2057 if (eh_frame_section)
2058 switch_to_section (eh_frame_section);
2059 else
2060 {
2943ce06 2061 /* We have no special eh_frame section. Put the information in
2062 the data section and emit special labels to guide collect2. */
2f14b1f9 2063 switch_to_section (data_section);
db85cc4f 2064 label = get_file_function_name ("F");
2f14b1f9 2065 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2066 targetm.asm_out.globalize_label (asm_out_file,
2067 IDENTIFIER_POINTER (label));
2068 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2069 }
2070}
2071
8a8bfbe7 2072/* Output a Call Frame Information opcode and its operand(s). */
2073
2074static void
8ec3a57b 2075output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
8a8bfbe7 2076{
4eeb8b5d 2077 unsigned long r;
8a8bfbe7 2078 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
8c3f468d 2079 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2080 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3201d6f1 2081 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
8c3f468d 2082 cfi->dw_cfi_oprnd1.dw_cfi_offset);
8a8bfbe7 2083 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2084 {
4eeb8b5d 2085 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2086 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2087 "DW_CFA_offset, column 0x%lx", r);
ca98eb0a 2088 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
8a8bfbe7 2089 }
2090 else if (cfi->dw_cfi_opc == DW_CFA_restore)
4eeb8b5d 2091 {
2092 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2093 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2094 "DW_CFA_restore, column 0x%lx", r);
2095 }
8a8bfbe7 2096 else
2097 {
ca98eb0a 2098 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2099 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
8a8bfbe7 2100
8a8bfbe7 2101 switch (cfi->dw_cfi_opc)
2102 {
2103 case DW_CFA_set_loc:
9b84bf7d 2104 if (for_eh)
2105 dw2_asm_output_encoded_addr_rtx (
2106 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2107 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
42e07529 2108 false, NULL);
9b84bf7d 2109 else
2110 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2111 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
d8eb7025 2112 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
8a8bfbe7 2113 break;
8c3f468d 2114
8a8bfbe7 2115 case DW_CFA_advance_loc1:
ca98eb0a 2116 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2117 fde->dw_fde_current_label, NULL);
c96dd0ff 2118 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
8a8bfbe7 2119 break;
8c3f468d 2120
8a8bfbe7 2121 case DW_CFA_advance_loc2:
ca98eb0a 2122 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2123 fde->dw_fde_current_label, NULL);
8a8bfbe7 2124 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2125 break;
8c3f468d 2126
8a8bfbe7 2127 case DW_CFA_advance_loc4:
ca98eb0a 2128 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2129 fde->dw_fde_current_label, NULL);
8a8bfbe7 2130 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2131 break;
8c3f468d 2132
8a8bfbe7 2133 case DW_CFA_MIPS_advance_loc8:
ca98eb0a 2134 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2135 fde->dw_fde_current_label, NULL);
2136 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
8a8bfbe7 2137 break;
8c3f468d 2138
8a8bfbe7 2139 case DW_CFA_offset_extended:
2140 case DW_CFA_def_cfa:
4eeb8b5d 2141 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2142 dw2_asm_output_data_uleb128 (r, NULL);
ca98eb0a 2143 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
8a8bfbe7 2144 break;
8c3f468d 2145
15a56411 2146 case DW_CFA_offset_extended_sf:
2147 case DW_CFA_def_cfa_sf:
4eeb8b5d 2148 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2149 dw2_asm_output_data_uleb128 (r, NULL);
15a56411 2150 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2151 break;
2152
8a8bfbe7 2153 case DW_CFA_restore_extended:
2154 case DW_CFA_undefined:
8a8bfbe7 2155 case DW_CFA_same_value:
2156 case DW_CFA_def_cfa_register:
4eeb8b5d 2157 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2158 dw2_asm_output_data_uleb128 (r, NULL);
8a8bfbe7 2159 break;
8c3f468d 2160
8a8bfbe7 2161 case DW_CFA_register:
4eeb8b5d 2162 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2163 dw2_asm_output_data_uleb128 (r, NULL);
2164 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2165 dw2_asm_output_data_uleb128 (r, NULL);
8a8bfbe7 2166 break;
8c3f468d 2167
8a8bfbe7 2168 case DW_CFA_def_cfa_offset:
ca98eb0a 2169 case DW_CFA_GNU_args_size:
2170 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
8a8bfbe7 2171 break;
8c3f468d 2172
15a56411 2173 case DW_CFA_def_cfa_offset_sf:
2174 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2175 break;
2176
4ad3f9b3 2177 case DW_CFA_GNU_window_save:
2178 break;
8c3f468d 2179
4b72e226 2180 case DW_CFA_def_cfa_expression:
15a56411 2181 case DW_CFA_expression:
4b72e226 2182 output_cfa_loc (cfi);
2183 break;
8c3f468d 2184
15a56411 2185 case DW_CFA_GNU_negative_offset_extended:
2186 /* Obsoleted by DW_CFA_offset_extended_sf. */
7bd4f6b6 2187 gcc_unreachable ();
15a56411 2188
8a8bfbe7 2189 default:
2190 break;
2191 }
f80d1bcd 2192 }
8a8bfbe7 2193}
2194
4eeb8b5d 2195/* Output the call frame information used to record information
8a8bfbe7 2196 that relates to calculating the frame pointer, and records the
2197 location of saved registers. */
2198
2199static void
8ec3a57b 2200output_call_frame_info (int for_eh)
8a8bfbe7 2201{
19cb6b50 2202 unsigned int i;
2203 dw_fde_ref fde;
2204 dw_cfi_ref cfi;
48ead6eb 2205 char l1[20], l2[20], section_start_label[20];
f7b10771 2206 bool any_lsda_needed = false;
df4b504c 2207 char augmentation[6];
9b84bf7d 2208 int augmentation_size;
2209 int fde_encoding = DW_EH_PE_absptr;
2210 int per_encoding = DW_EH_PE_absptr;
2211 int lsda_encoding = DW_EH_PE_absptr;
51ea5d02 2212 int return_reg;
8a8bfbe7 2213
637d3308 2214 /* Don't emit a CIE if there won't be any FDEs. */
2215 if (fde_table_in_use == 0)
2216 return;
2217
2f9fc8ef 2218 /* If we make FDEs linkonce, we may have to emit an empty label for
2219 an FDE that wouldn't otherwise be emitted. We want to avoid
2220 having an FDE kept around when the function it refers to is
1dc74225 2221 discarded. Example where this matters: a primary function
2f9fc8ef 2222 template in C++ requires EH information, but an explicit
0bed3869 2223 specialization doesn't. */
2f9fc8ef 2224 if (TARGET_USES_WEAK_UNWIND_INFO
2225 && ! flag_asynchronous_unwind_tables
2226 && for_eh)
2227 for (i = 0; i < fde_table_in_use; i++)
2228 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
61a9389f 2229 && !fde_table[i].uses_eh_lsda
1dc74225 2230 && ! DECL_WEAK (fde_table[i].decl))
883b2e73 2231 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
ef1074f7 2232 for_eh, /* empty */ 1);
2f9fc8ef 2233
f7b10771 2234 /* If we don't have any functions we'll want to unwind out of, don't
2235 emit any EH unwind information. Note that if exceptions aren't
2236 enabled, we won't have collected nothrow information, and if we
2237 asked for asynchronous tables, we always want this info. */
f543a963 2238 if (for_eh)
2239 {
f7b10771 2240 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
8c3f468d 2241
2242 for (i = 0; i < fde_table_in_use; i++)
df4b504c 2243 if (fde_table[i].uses_eh_lsda)
f7b10771 2244 any_eh_needed = any_lsda_needed = true;
61a9389f 2245 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3ff2e849 2246 any_eh_needed = true;
d744d41d 2247 else if (! fde_table[i].nothrow
2248 && ! fde_table[i].all_throwers_are_sibcalls)
f7b10771 2249 any_eh_needed = true;
df4b504c 2250
2251 if (! any_eh_needed)
2252 return;
f543a963 2253 }
2254
009a56ab 2255 /* We're going to be generating comments, so turn on app. */
2256 if (flag_debug_asm)
2257 app_enable ();
ad87de1e 2258
8a8bfbe7 2259 if (for_eh)
2f14b1f9 2260 switch_to_eh_frame_section ();
8a8bfbe7 2261 else
4494ff1b 2262 {
2263 if (!debug_frame_section)
2264 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2265 SECTION_DEBUG, NULL);
2266 switch_to_section (debug_frame_section);
2267 }
8a8bfbe7 2268
48ead6eb 2269 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2270 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2271
f80d1bcd 2272 /* Output the CIE. */
19bce576 2273 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2274 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
04da8de9 2275 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2276 dw2_asm_output_data (4, 0xffffffff,
2277 "Initial length escape value indicating 64-bit DWARF extension");
ca98eb0a 2278 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2279 "Length of Common Information Entry");
19bce576 2280 ASM_OUTPUT_LABEL (asm_out_file, l1);
2281
ca98eb0a 2282 /* Now that the CIE pointer is PC-relative for EH,
2283 use 0 to identify the CIE. */
2284 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
04da8de9 2285 (for_eh ? 0 : DWARF_CIE_ID),
ca98eb0a 2286 "CIE Identifier Tag");
8a8bfbe7 2287
ca98eb0a 2288 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
8a8bfbe7 2289
df4b504c 2290 augmentation[0] = 0;
9b84bf7d 2291 augmentation_size = 0;
df4b504c 2292 if (for_eh)
19bce576 2293 {
9b84bf7d 2294 char *p;
2295
df4b504c 2296 /* Augmentation:
2297 z Indicates that a uleb128 is present to size the
8ec3a57b 2298 augmentation section.
9b84bf7d 2299 L Indicates the encoding (and thus presence) of
2300 an LSDA pointer in the FDE augmentation.
2301 R Indicates a non-default pointer encoding for
2302 FDE code pointers.
2303 P Indicates the presence of an encoding + language
2304 personality routine in the CIE augmentation. */
2305
3ff2e849 2306 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
9b84bf7d 2307 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2308 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2309
2310 p = augmentation + 1;
2311 if (eh_personality_libfunc)
2312 {
2313 *p++ = 'P';
2314 augmentation_size += 1 + size_of_encoded_value (per_encoding);
849ea31c 2315 assemble_external_libcall (eh_personality_libfunc);
9b84bf7d 2316 }
df4b504c 2317 if (any_lsda_needed)
9b84bf7d 2318 {
2319 *p++ = 'L';
2320 augmentation_size += 1;
2321 }
2322 if (fde_encoding != DW_EH_PE_absptr)
2323 {
2324 *p++ = 'R';
2325 augmentation_size += 1;
2326 }
2327 if (p > augmentation + 1)
2328 {
2329 augmentation[0] = 'z';
bc70bd5e 2330 *p = '\0';
9b84bf7d 2331 }
9a4d22ba 2332
2333 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2334 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2335 {
2336 int offset = ( 4 /* Length */
2337 + 4 /* CIE Id */
2338 + 1 /* CIE version */
2339 + strlen (augmentation) + 1 /* Augmentation */
2340 + size_of_uleb128 (1) /* Code alignment */
2341 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2342 + 1 /* RA column */
2343 + 1 /* Augmentation size */
2344 + 1 /* Personality encoding */ );
2345 int pad = -offset & (PTR_SIZE - 1);
2346
2347 augmentation_size += pad;
2348
2349 /* Augmentations should be small, so there's scarce need to
2350 iterate for a solution. Die if we exceed one uleb128 byte. */
7bd4f6b6 2351 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
9a4d22ba 2352 }
19bce576 2353 }
8a8bfbe7 2354
8c3f468d 2355 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
ca98eb0a 2356 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
ca98eb0a 2357 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2358 "CIE Data Alignment Factor");
ab569c0c 2359
51ea5d02 2360 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
ab569c0c 2361 if (DW_CIE_VERSION == 1)
51ea5d02 2362 dw2_asm_output_data (1, return_reg, "CIE RA Column");
ab569c0c 2363 else
51ea5d02 2364 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
8a8bfbe7 2365
df4b504c 2366 if (augmentation[0])
2367 {
9b84bf7d 2368 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
df4b504c 2369 if (eh_personality_libfunc)
9b84bf7d 2370 {
2371 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2372 eh_data_format_name (per_encoding));
2373 dw2_asm_output_encoded_addr_rtx (per_encoding,
42e07529 2374 eh_personality_libfunc,
2375 true, NULL);
9b84bf7d 2376 }
8c3f468d 2377
9b84bf7d 2378 if (any_lsda_needed)
2379 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2380 eh_data_format_name (lsda_encoding));
8c3f468d 2381
9b84bf7d 2382 if (fde_encoding != DW_EH_PE_absptr)
2383 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2384 eh_data_format_name (fde_encoding));
df4b504c 2385 }
2386
8a8bfbe7 2387 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
b7020468 2388 output_cfi (cfi, NULL, for_eh);
8a8bfbe7 2389
2390 /* Pad the CIE out to an address sized boundary. */
bc70bd5e 2391 ASM_OUTPUT_ALIGN (asm_out_file,
b7020468 2392 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
19bce576 2393 ASM_OUTPUT_LABEL (asm_out_file, l2);
8a8bfbe7 2394
2395 /* Loop through all of the FDE's. */
8c3f468d 2396 for (i = 0; i < fde_table_in_use; i++)
8a8bfbe7 2397 {
2398 fde = &fde_table[i];
8a8bfbe7 2399
df4b504c 2400 /* Don't emit EH unwind info for leaf functions that don't need it. */
f7b10771 2401 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
04396483 2402 && (fde->nothrow || fde->all_throwers_are_sibcalls)
1dc74225 2403 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
04396483 2404 && !fde->uses_eh_lsda)
f543a963 2405 continue;
2406
ef1074f7 2407 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
883b2e73 2408 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
f80d1bcd 2409 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2410 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
04da8de9 2411 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2412 dw2_asm_output_data (4, 0xffffffff,
2413 "Initial length escape value indicating 64-bit DWARF extension");
ca98eb0a 2414 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2415 "FDE Length");
19bce576 2416 ASM_OUTPUT_LABEL (asm_out_file, l1);
2417
8a8bfbe7 2418 if (for_eh)
48ead6eb 2419 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
8a8bfbe7 2420 else
48ead6eb 2421 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
d08d29c0 2422 debug_frame_section, "FDE CIE offset");
8a8bfbe7 2423
9b84bf7d 2424 if (for_eh)
2425 {
3ff2e849 2426 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2427 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2428 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2429 sym_ref,
42e07529 2430 false,
3ff2e849 2431 "FDE initial location");
1897b881 2432 if (fde->dw_fde_switched_sections)
2433 {
61a9389f 2434 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
1897b881 2435 fde->dw_fde_unlikely_section_label);
61a9389f 2436 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
1897b881 2437 fde->dw_fde_hot_section_label);
2438 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2439 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
42e07529 2440 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
1897b881 2441 "FDE initial location");
2442 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2443 fde->dw_fde_hot_section_end_label,
2444 fde->dw_fde_hot_section_label,
2445 "FDE address range");
42e07529 2446 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
1897b881 2447 "FDE initial location");
2448 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2449 fde->dw_fde_unlikely_section_end_label,
2450 fde->dw_fde_unlikely_section_label,
2451 "FDE address range");
2452 }
2453 else
2454 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2455 fde->dw_fde_end, fde->dw_fde_begin,
2456 "FDE address range");
9b84bf7d 2457 }
2458 else
2459 {
2460 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2461 "FDE initial location");
1897b881 2462 if (fde->dw_fde_switched_sections)
2463 {
2464 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2465 fde->dw_fde_hot_section_label,
2466 "FDE initial location");
2467 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2468 fde->dw_fde_hot_section_end_label,
2469 fde->dw_fde_hot_section_label,
2470 "FDE address range");
2471 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2472 fde->dw_fde_unlikely_section_label,
2473 "FDE initial location");
61a9389f 2474 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
1897b881 2475 fde->dw_fde_unlikely_section_end_label,
2476 fde->dw_fde_unlikely_section_label,
2477 "FDE address range");
2478 }
2479 else
2480 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2481 fde->dw_fde_end, fde->dw_fde_begin,
2482 "FDE address range");
9b84bf7d 2483 }
8a8bfbe7 2484
df4b504c 2485 if (augmentation[0])
2486 {
9b84bf7d 2487 if (any_lsda_needed)
df4b504c 2488 {
9a4d22ba 2489 int size = size_of_encoded_value (lsda_encoding);
2490
2491 if (lsda_encoding == DW_EH_PE_aligned)
2492 {
2493 int offset = ( 4 /* Length */
2494 + 4 /* CIE offset */
2495 + 2 * size_of_encoded_value (fde_encoding)
2496 + 1 /* Augmentation size */ );
2497 int pad = -offset & (PTR_SIZE - 1);
2498
2499 size += pad;
7bd4f6b6 2500 gcc_assert (size_of_uleb128 (size) == 1);
9a4d22ba 2501 }
2502
2503 dw2_asm_output_data_uleb128 (size, "Augmentation size");
9b84bf7d 2504
2505 if (fde->uses_eh_lsda)
c83a163c 2506 {
2507 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
9b84bf7d 2508 fde->funcdef_number);
c83a163c 2509 dw2_asm_output_encoded_addr_rtx (
9b84bf7d 2510 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
42e07529 2511 false, "Language Specific Data Area");
c83a163c 2512 }
9b84bf7d 2513 else
9a4d22ba 2514 {
2515 if (lsda_encoding == DW_EH_PE_aligned)
2516 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
8c3f468d 2517 dw2_asm_output_data
2518 (size_of_encoded_value (lsda_encoding), 0,
2519 "Language Specific Data Area (none)");
9a4d22ba 2520 }
df4b504c 2521 }
2522 else
9b84bf7d 2523 dw2_asm_output_data_uleb128 (0, "Augmentation size");
df4b504c 2524 }
2525
8a8bfbe7 2526 /* Loop through the Call Frame Instructions associated with
2527 this FDE. */
2528 fde->dw_fde_current_label = fde->dw_fde_begin;
2529 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
b7020468 2530 output_cfi (cfi, fde, for_eh);
8a8bfbe7 2531
19bce576 2532 /* Pad the FDE out to an address sized boundary. */
bc70bd5e 2533 ASM_OUTPUT_ALIGN (asm_out_file,
c83a163c 2534 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
19bce576 2535 ASM_OUTPUT_LABEL (asm_out_file, l2);
8a8bfbe7 2536 }
ca98eb0a 2537
a08b74c8 2538 if (for_eh && targetm.terminate_dw2_eh_frame_info)
ca98eb0a 2539 dw2_asm_output_data (4, 0, "End of Table");
19bce576 2540#ifdef MIPS_DEBUGGING_INFO
2541 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2542 get a value of 0. Putting .align 0 after the label fixes it. */
2543 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2544#endif
009a56ab 2545
2546 /* Turn off app to make assembly quicker. */
2547 if (flag_debug_asm)
2548 app_disable ();
19bce576 2549}
2550
8a8bfbe7 2551/* Output a marker (i.e. a label) for the beginning of a function, before
2552 the prologue. */
2553
2554void
8ec3a57b 2555dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2556 const char *file ATTRIBUTE_UNUSED)
8a8bfbe7 2557{
2558 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2d754264 2559 char * dup_label;
19cb6b50 2560 dw_fde_ref fde;
8a8bfbe7 2561
2d754264 2562 current_function_func_begin_label = NULL;
ad5818ae 2563
8ec87476 2564#ifdef TARGET_UNWIND_INFO
ad5818ae 2565 /* ??? current_function_func_begin_label is also used by except.c
2566 for call-site information. We must emit this label if it might
2567 be used. */
2568 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2569 && ! dwarf2out_do_frame ())
2570 return;
2571#else
2572 if (! dwarf2out_do_frame ())
2573 return;
2574#endif
2575
2f14b1f9 2576 switch_to_section (function_section (current_function_decl));
8a8bfbe7 2577 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4781f9b9 2578 current_function_funcdef_no);
ad5818ae 2579 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4781f9b9 2580 current_function_funcdef_no);
2d754264 2581 dup_label = xstrdup (label);
2582 current_function_func_begin_label = dup_label;
8a8bfbe7 2583
8ec87476 2584#ifdef TARGET_UNWIND_INFO
ad5818ae 2585 /* We can elide the fde allocation if we're not emitting debug info. */
2586 if (! dwarf2out_do_frame ())
2587 return;
2588#endif
2589
8a8bfbe7 2590 /* Expand the fde table if necessary. */
2591 if (fde_table_in_use == fde_table_allocated)
2592 {
2593 fde_table_allocated += FDE_TABLE_INCREMENT;
573aba85 2594 fde_table = ggc_realloc (fde_table,
2595 fde_table_allocated * sizeof (dw_fde_node));
2596 memset (fde_table + fde_table_in_use, 0,
2597 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
30ade641 2598 }
8a8bfbe7 2599
2600 /* Record the FDE associated with this function. */
2601 current_funcdef_fde = fde_table_in_use;
2602
2603 /* Add the new FDE at the end of the fde_table. */
2604 fde = &fde_table[fde_table_in_use++];
2f9fc8ef 2605 fde->decl = current_function_decl;
2d754264 2606 fde->dw_fde_begin = dup_label;
3036ecbe 2607 fde->dw_fde_current_label = dup_label;
1897b881 2608 fde->dw_fde_hot_section_label = NULL;
2609 fde->dw_fde_hot_section_end_label = NULL;
2610 fde->dw_fde_unlikely_section_label = NULL;
2611 fde->dw_fde_unlikely_section_end_label = NULL;
2612 fde->dw_fde_switched_sections = false;
8a8bfbe7 2613 fde->dw_fde_end = NULL;
2614 fde->dw_fde_cfi = NULL;
4781f9b9 2615 fde->funcdef_number = current_function_funcdef_no;
da2f1613 2616 fde->nothrow = TREE_NOTHROW (current_function_decl);
df4b504c 2617 fde->uses_eh_lsda = cfun->uses_eh_lsda;
04396483 2618 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
f543a963 2619
08532d4f 2620 args_size = old_args_size = 0;
f76df888 2621
8c3f468d 2622 /* We only want to output line number information for the genuine dwarf2
2623 prologue case, not the eh frame case. */
f76df888 2624#ifdef DWARF2_DEBUGGING_INFO
2625 if (file)
2626 dwarf2out_source_line (line, file);
2627#endif
8a8bfbe7 2628}
2629
2630/* Output a marker (i.e. a label) for the absolute end of the generated code
2631 for a function definition. This gets called *after* the epilogue code has
2632 been generated. */
2633
2634void
8ec3a57b 2635dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2636 const char *file ATTRIBUTE_UNUSED)
8a8bfbe7 2637{
2638 dw_fde_ref fde;
2639 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2640
2641 /* Output a label to mark the endpoint of the code generated for this
04641143 2642 function. */
4781f9b9 2643 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2644 current_function_funcdef_no);
8a8bfbe7 2645 ASM_OUTPUT_LABEL (asm_out_file, label);
2646 fde = &fde_table[fde_table_in_use - 1];
2647 fde->dw_fde_end = xstrdup (label);
8a8bfbe7 2648}
2649
2650void
8ec3a57b 2651dwarf2out_frame_init (void)
8a8bfbe7 2652{
2653 /* Allocate the initial hunk of the fde_table. */
f0af5a88 2654 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
8a8bfbe7 2655 fde_table_allocated = FDE_TABLE_INCREMENT;
2656 fde_table_in_use = 0;
2657
2658 /* Generate the CFA instructions common to all FDE's. Do it now for the
2659 sake of lookup_cfa. */
2660
56daab87 2661 /* On entry, the Canonical Frame Address is at SP. */
2662 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
34986748 2663
2664#ifdef DWARF2_UNWIND_INFO
2665 if (DWARF2_UNWIND_INFO)
2666 initial_return_save (INCOMING_RETURN_ADDR_RTX);
8a8bfbe7 2667#endif
2668}
2669
2670void
8ec3a57b 2671dwarf2out_frame_finish (void)
8a8bfbe7 2672{
8a8bfbe7 2673 /* Output call frame information. */
34986748 2674 if (DWARF2_FRAME_INFO)
8a8bfbe7 2675 output_call_frame_info (0);
8c3f468d 2676
a28008f5 2677#ifndef TARGET_UNWIND_INFO
2678 /* Output another copy for the unwinder. */
6851a1fc 2679 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
8a8bfbe7 2680 output_call_frame_info (1);
a28008f5 2681#endif
f80d1bcd 2682}
573aba85 2683#endif
4b72e226 2684\f
2685/* And now, the subset of the debugging information support code necessary
2686 for emitting location expressions. */
8a8bfbe7 2687
69278c24 2688/* Data about a single source file. */
2689struct dwarf_file_data GTY(())
2690{
2691 const char * filename;
2692 int emitted_number;
2693};
2694
931e9893 2695/* We need some way to distinguish DW_OP_addr with a direct symbol
2696 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2697#define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2698
2699
4b72e226 2700typedef struct dw_val_struct *dw_val_ref;
2701typedef struct die_struct *dw_die_ref;
c1fdef8e 2702typedef const struct die_struct *const_dw_die_ref;
4b72e226 2703typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4c21a22f 2704typedef struct dw_loc_list_struct *dw_loc_list_ref;
8a8bfbe7 2705
2706/* Each DIE may have a series of attribute/value pairs. Values
2707 can take on several forms. The forms that are used in this
2708 implementation are listed below. */
2709
573aba85 2710enum dw_val_class
8a8bfbe7 2711{
2712 dw_val_class_addr,
a36145ca 2713 dw_val_class_offset,
8a8bfbe7 2714 dw_val_class_loc,
4c21a22f 2715 dw_val_class_loc_list,
fe39c28c 2716 dw_val_class_range_list,
8a8bfbe7 2717 dw_val_class_const,
2718 dw_val_class_unsigned_const,
2719 dw_val_class_long_long,
1b6ad376 2720 dw_val_class_vec,
8a8bfbe7 2721 dw_val_class_flag,
2722 dw_val_class_die_ref,
2723 dw_val_class_fde_ref,
2724 dw_val_class_lbl_id,
d08d29c0 2725 dw_val_class_lineptr,
2726 dw_val_class_str,
69278c24 2727 dw_val_class_macptr,
2728 dw_val_class_file
573aba85 2729};
30ade641 2730
8a8bfbe7 2731/* Describe a double word constant value. */
0a44b200 2732/* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
8a8bfbe7 2733
573aba85 2734typedef struct dw_long_long_struct GTY(())
30ade641 2735{
8a8bfbe7 2736 unsigned long hi;
2737 unsigned long low;
2738}
2739dw_long_long_const;
2740
1b6ad376 2741/* Describe a floating point constant value, or a vector constant value. */
8a8bfbe7 2742
1b6ad376 2743typedef struct dw_vec_struct GTY(())
8a8bfbe7 2744{
1b6ad376 2745 unsigned char * GTY((length ("%h.length"))) array;
8a8bfbe7 2746 unsigned length;
1b6ad376 2747 unsigned elt_size;
8a8bfbe7 2748}
1b6ad376 2749dw_vec_const;
8a8bfbe7 2750
ad87de1e 2751/* The dw_val_node describes an attribute's value, as it is
8a8bfbe7 2752 represented internally. */
2753
573aba85 2754typedef struct dw_val_struct GTY(())
8a8bfbe7 2755{
573aba85 2756 enum dw_val_class val_class;
2757 union dw_val_struct_union
30ade641 2758 {
573aba85 2759 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3d867824 2760 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
573aba85 2761 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2762 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
7035b2ab 2763 HOST_WIDE_INT GTY ((default)) val_int;
3d867824 2764 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
573aba85 2765 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
1b6ad376 2766 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
573aba85 2767 struct dw_val_die_union
8c3f468d 2768 {
2769 dw_die_ref die;
2770 int external;
573aba85 2771 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2772 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2773 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2774 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2775 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
69278c24 2776 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
30ade641 2777 }
573aba85 2778 GTY ((desc ("%1.val_class"))) v;
8a8bfbe7 2779}
2780dw_val_node;
2781
2782/* Locations in memory are described using a sequence of stack machine
2783 operations. */
2784
573aba85 2785typedef struct dw_loc_descr_struct GTY(())
8a8bfbe7 2786{
2787 dw_loc_descr_ref dw_loc_next;
2788 enum dwarf_location_atom dw_loc_opc;
2789 dw_val_node dw_loc_oprnd1;
2790 dw_val_node dw_loc_oprnd2;
9ed904da 2791 int dw_loc_addr;
8a8bfbe7 2792}
2793dw_loc_descr_node;
2794
4c21a22f 2795/* Location lists are ranges + location descriptions for that range,
2796 so you can track variables that are in different places over
6312a35e 2797 their entire life. */
573aba85 2798typedef struct dw_loc_list_struct GTY(())
4c21a22f 2799{
2800 dw_loc_list_ref dw_loc_next;
2801 const char *begin; /* Label for begin address of range */
2802 const char *end; /* Label for end address of range */
8c3f468d 2803 char *ll_symbol; /* Label for beginning of location list.
2804 Only on head of list */
4c21a22f 2805 const char *section; /* Section this loclist is relative to */
2806 dw_loc_descr_ref expr;
2807} dw_loc_list_node;
2808
573aba85 2809#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2810
8ec3a57b 2811static const char *dwarf_stack_op_name (unsigned);
2812static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
3d867824 2813 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
8ec3a57b 2814static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2815static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2816static unsigned long size_of_locs (dw_loc_descr_ref);
2817static void output_loc_operands (dw_loc_descr_ref);
2818static void output_loc_sequence (dw_loc_descr_ref);
8a8bfbe7 2819
4b72e226 2820/* Convert a DWARF stack opcode into its string name. */
8a8bfbe7 2821
4b72e226 2822static const char *
8ec3a57b 2823dwarf_stack_op_name (unsigned int op)
678d90bb 2824{
4b72e226 2825 switch (op)
2826 {
2827 case DW_OP_addr:
931e9893 2828 case INTERNAL_DW_OP_tls_addr:
4b72e226 2829 return "DW_OP_addr";
2830 case DW_OP_deref:
2831 return "DW_OP_deref";
2832 case DW_OP_const1u:
2833 return "DW_OP_const1u";
2834 case DW_OP_const1s:
2835 return "DW_OP_const1s";
2836 case DW_OP_const2u:
2837 return "DW_OP_const2u";
2838 case DW_OP_const2s:
2839 return "DW_OP_const2s";
2840 case DW_OP_const4u:
2841 return "DW_OP_const4u";
2842 case DW_OP_const4s:
2843 return "DW_OP_const4s";
2844 case DW_OP_const8u:
2845 return "DW_OP_const8u";
2846 case DW_OP_const8s:
2847 return "DW_OP_const8s";
2848 case DW_OP_constu:
2849 return "DW_OP_constu";
2850 case DW_OP_consts:
2851 return "DW_OP_consts";
2852 case DW_OP_dup:
2853 return "DW_OP_dup";
2854 case DW_OP_drop:
2855 return "DW_OP_drop";
2856 case DW_OP_over:
2857 return "DW_OP_over";
2858 case DW_OP_pick:
2859 return "DW_OP_pick";
2860 case DW_OP_swap:
2861 return "DW_OP_swap";
2862 case DW_OP_rot:
2863 return "DW_OP_rot";
2864 case DW_OP_xderef:
2865 return "DW_OP_xderef";
2866 case DW_OP_abs:
2867 return "DW_OP_abs";
2868 case DW_OP_and:
2869 return "DW_OP_and";
2870 case DW_OP_div:
2871 return "DW_OP_div";
2872 case DW_OP_minus:
2873 return "DW_OP_minus";
2874 case DW_OP_mod:
2875 return "DW_OP_mod";
2876 case DW_OP_mul:
2877 return "DW_OP_mul";
2878 case DW_OP_neg:
2879 return "DW_OP_neg";
2880 case DW_OP_not:
2881 return "DW_OP_not";
2882 case DW_OP_or:
2883 return "DW_OP_or";
2884 case DW_OP_plus:
2885 return "DW_OP_plus";
2886 case DW_OP_plus_uconst:
2887 return "DW_OP_plus_uconst";
2888 case DW_OP_shl:
2889 return "DW_OP_shl";
2890 case DW_OP_shr:
2891 return "DW_OP_shr";
2892 case DW_OP_shra:
2893 return "DW_OP_shra";
2894 case DW_OP_xor:
2895 return "DW_OP_xor";
2896 case DW_OP_bra:
2897 return "DW_OP_bra";
2898 case DW_OP_eq:
2899 return "DW_OP_eq";
2900 case DW_OP_ge:
2901 return "DW_OP_ge";
2902 case DW_OP_gt:
2903 return "DW_OP_gt";
2904 case DW_OP_le:
2905 return "DW_OP_le";
2906 case DW_OP_lt:
2907 return "DW_OP_lt";
2908 case DW_OP_ne:
2909 return "DW_OP_ne";
2910 case DW_OP_skip:
2911 return "DW_OP_skip";
2912 case DW_OP_lit0:
2913 return "DW_OP_lit0";
2914 case DW_OP_lit1:
2915 return "DW_OP_lit1";
2916 case DW_OP_lit2:
2917 return "DW_OP_lit2";
2918 case DW_OP_lit3:
2919 return "DW_OP_lit3";
2920 case DW_OP_lit4:
2921 return "DW_OP_lit4";
2922 case DW_OP_lit5:
2923 return "DW_OP_lit5";
2924 case DW_OP_lit6:
2925 return "DW_OP_lit6";
2926 case DW_OP_lit7:
2927 return "DW_OP_lit7";
2928 case DW_OP_lit8:
2929 return "DW_OP_lit8";
2930 case DW_OP_lit9:
2931 return "DW_OP_lit9";
2932 case DW_OP_lit10:
2933 return "DW_OP_lit10";
2934 case DW_OP_lit11:
2935 return "DW_OP_lit11";
2936 case DW_OP_lit12:
2937 return "DW_OP_lit12";
2938 case DW_OP_lit13:
2939 return "DW_OP_lit13";
2940 case DW_OP_lit14:
2941 return "DW_OP_lit14";
2942 case DW_OP_lit15:
2943 return "DW_OP_lit15";
2944 case DW_OP_lit16:
2945 return "DW_OP_lit16";
2946 case DW_OP_lit17:
2947 return "DW_OP_lit17";
2948 case DW_OP_lit18:
2949 return "DW_OP_lit18";
2950 case DW_OP_lit19:
2951 return "DW_OP_lit19";
2952 case DW_OP_lit20:
2953 return "DW_OP_lit20";
2954 case DW_OP_lit21:
2955 return "DW_OP_lit21";
2956 case DW_OP_lit22:
2957 return "DW_OP_lit22";
2958 case DW_OP_lit23:
2959 return "DW_OP_lit23";
2960 case DW_OP_lit24:
2961 return "DW_OP_lit24";
2962 case DW_OP_lit25:
2963 return "DW_OP_lit25";
2964 case DW_OP_lit26:
2965 return "DW_OP_lit26";
2966 case DW_OP_lit27:
2967 return "DW_OP_lit27";
2968 case DW_OP_lit28:
2969 return "DW_OP_lit28";
2970 case DW_OP_lit29:
2971 return "DW_OP_lit29";
2972 case DW_OP_lit30:
2973 return "DW_OP_lit30";
2974 case DW_OP_lit31:
2975 return "DW_OP_lit31";
2976 case DW_OP_reg0:
2977 return "DW_OP_reg0";
2978 case DW_OP_reg1:
2979 return "DW_OP_reg1";
2980 case DW_OP_reg2:
2981 return "DW_OP_reg2";
2982 case DW_OP_reg3:
2983 return "DW_OP_reg3";
2984 case DW_OP_reg4:
2985 return "DW_OP_reg4";
2986 case DW_OP_reg5:
2987 return "DW_OP_reg5";
2988 case DW_OP_reg6:
2989 return "DW_OP_reg6";
2990 case DW_OP_reg7:
2991 return "DW_OP_reg7";
2992 case DW_OP_reg8:
2993 return "DW_OP_reg8";
2994 case DW_OP_reg9:
2995 return "DW_OP_reg9";
2996 case DW_OP_reg10:
2997 return "DW_OP_reg10";
2998 case DW_OP_reg11:
2999 return "DW_OP_reg11";
3000 case DW_OP_reg12:
3001 return "DW_OP_reg12";
3002 case DW_OP_reg13:
3003 return "DW_OP_reg13";
3004 case DW_OP_reg14:
3005 return "DW_OP_reg14";
3006 case DW_OP_reg15:
3007 return "DW_OP_reg15";
3008 case DW_OP_reg16:
3009 return "DW_OP_reg16";
3010 case DW_OP_reg17:
3011 return "DW_OP_reg17";
3012 case DW_OP_reg18:
3013 return "DW_OP_reg18";
3014 case DW_OP_reg19:
3015 return "DW_OP_reg19";
3016 case DW_OP_reg20:
3017 return "DW_OP_reg20";
3018 case DW_OP_reg21:
3019 return "DW_OP_reg21";
3020 case DW_OP_reg22:
3021 return "DW_OP_reg22";
3022 case DW_OP_reg23:
3023 return "DW_OP_reg23";
3024 case DW_OP_reg24:
3025 return "DW_OP_reg24";
3026 case DW_OP_reg25:
3027 return "DW_OP_reg25";
3028 case DW_OP_reg26:
3029 return "DW_OP_reg26";
3030 case DW_OP_reg27:
3031 return "DW_OP_reg27";
3032 case DW_OP_reg28:
3033 return "DW_OP_reg28";
3034 case DW_OP_reg29:
3035 return "DW_OP_reg29";
3036 case DW_OP_reg30:
3037 return "DW_OP_reg30";
3038 case DW_OP_reg31:
3039 return "DW_OP_reg31";
3040 case DW_OP_breg0:
3041 return "DW_OP_breg0";
3042 case DW_OP_breg1:
3043 return "DW_OP_breg1";
3044 case DW_OP_breg2:
3045 return "DW_OP_breg2";
3046 case DW_OP_breg3:
3047 return "DW_OP_breg3";
3048 case DW_OP_breg4:
3049 return "DW_OP_breg4";
3050 case DW_OP_breg5:
3051 return "DW_OP_breg5";
3052 case DW_OP_breg6:
3053 return "DW_OP_breg6";
3054 case DW_OP_breg7:
3055 return "DW_OP_breg7";
3056 case DW_OP_breg8:
3057 return "DW_OP_breg8";
3058 case DW_OP_breg9:
3059 return "DW_OP_breg9";
3060 case DW_OP_breg10:
3061 return "DW_OP_breg10";
3062 case DW_OP_breg11:
3063 return "DW_OP_breg11";
3064 case DW_OP_breg12:
3065 return "DW_OP_breg12";
3066 case DW_OP_breg13:
3067 return "DW_OP_breg13";
3068 case DW_OP_breg14:
3069 return "DW_OP_breg14";
3070 case DW_OP_breg15:
3071 return "DW_OP_breg15";
3072 case DW_OP_breg16:
3073 return "DW_OP_breg16";
3074 case DW_OP_breg17:
3075 return "DW_OP_breg17";
3076 case DW_OP_breg18:
3077 return "DW_OP_breg18";
3078 case DW_OP_breg19:
3079 return "DW_OP_breg19";
3080 case DW_OP_breg20:
3081 return "DW_OP_breg20";
3082 case DW_OP_breg21:
3083 return "DW_OP_breg21";
3084 case DW_OP_breg22:
3085 return "DW_OP_breg22";
3086 case DW_OP_breg23:
3087 return "DW_OP_breg23";
3088 case DW_OP_breg24:
3089 return "DW_OP_breg24";
3090 case DW_OP_breg25:
3091 return "DW_OP_breg25";
3092 case DW_OP_breg26:
3093 return "DW_OP_breg26";
3094 case DW_OP_breg27:
3095 return "DW_OP_breg27";
3096 case DW_OP_breg28:
3097 return "DW_OP_breg28";
3098 case DW_OP_breg29:
3099 return "DW_OP_breg29";
3100 case DW_OP_breg30:
3101 return "DW_OP_breg30";
3102 case DW_OP_breg31:
3103 return "DW_OP_breg31";
3104 case DW_OP_regx:
3105 return "DW_OP_regx";
3106 case DW_OP_fbreg:
3107 return "DW_OP_fbreg";
3108 case DW_OP_bregx:
3109 return "DW_OP_bregx";
3110 case DW_OP_piece:
3111 return "DW_OP_piece";
3112 case DW_OP_deref_size:
3113 return "DW_OP_deref_size";
3114 case DW_OP_xderef_size:
3115 return "DW_OP_xderef_size";
3116 case DW_OP_nop:
3117 return "DW_OP_nop";
931e9893 3118 case DW_OP_push_object_address:
3119 return "DW_OP_push_object_address";
3120 case DW_OP_call2:
3121 return "DW_OP_call2";
3122 case DW_OP_call4:
3123 return "DW_OP_call4";
3124 case DW_OP_call_ref:
3125 return "DW_OP_call_ref";
3126 case DW_OP_GNU_push_tls_address:
3127 return "DW_OP_GNU_push_tls_address";
d53bb226 3128 case DW_OP_GNU_uninit:
3129 return "DW_OP_GNU_uninit";
8a8bfbe7 3130 default:
4b72e226 3131 return "OP_<unknown>";
8a8bfbe7 3132 }
6ed29fb8 3133}
30ade641 3134
4b72e226 3135/* Return a pointer to a newly allocated location description. Location
3136 descriptions are simple expression terms that can be strung
3137 together to form more complicated location (address) descriptions. */
3138
3139static inline dw_loc_descr_ref
3d867824 3140new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3141 unsigned HOST_WIDE_INT oprnd2)
752e49ca 3142{
f0af5a88 3143 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
ec1e49cc 3144
4b72e226 3145 descr->dw_loc_opc = op;
3146 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3147 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3148 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3149 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
ec1e49cc 3150
4b72e226 3151 return descr;
3152}
3153
3154/* Add a location description term to a location description expression. */
3155
3156static inline void
8ec3a57b 3157add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4b72e226 3158{
19cb6b50 3159 dw_loc_descr_ref *d;
4b72e226 3160
3161 /* Find the end of the chain. */
3162 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3163 ;
3164
3165 *d = descr;
3166}
3167
3168/* Return the size of a location descriptor. */
3169
3170static unsigned long
8ec3a57b 3171size_of_loc_descr (dw_loc_descr_ref loc)
4b72e226 3172{
19cb6b50 3173 unsigned long size = 1;
4b72e226 3174
3175 switch (loc->dw_loc_opc)
3176 {
3177 case DW_OP_addr:
931e9893 3178 case INTERNAL_DW_OP_tls_addr:
4b72e226 3179 size += DWARF2_ADDR_SIZE;
3180 break;
3181 case DW_OP_const1u:
3182 case DW_OP_const1s:
3183 size += 1;
3184 break;
3185 case DW_OP_const2u:
3186 case DW_OP_const2s:
3187 size += 2;
3188 break;
3189 case DW_OP_const4u:
3190 case DW_OP_const4s:
3191 size += 4;
3192 break;
3193 case DW_OP_const8u:
3194 case DW_OP_const8s:
3195 size += 8;
3196 break;
3197 case DW_OP_constu:
3198 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3199 break;
3200 case DW_OP_consts:
3201 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3202 break;
3203 case DW_OP_pick:
3204 size += 1;
3205 break;
3206 case DW_OP_plus_uconst:
3207 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3208 break;
3209 case DW_OP_skip:
3210 case DW_OP_bra:
3211 size += 2;
3212 break;
3213 case DW_OP_breg0:
3214 case DW_OP_breg1:
3215 case DW_OP_breg2:
3216 case DW_OP_breg3:
3217 case DW_OP_breg4:
3218 case DW_OP_breg5:
3219 case DW_OP_breg6:
3220 case DW_OP_breg7:
3221 case DW_OP_breg8:
3222 case DW_OP_breg9:
3223 case DW_OP_breg10:
3224 case DW_OP_breg11:
3225 case DW_OP_breg12:
3226 case DW_OP_breg13:
3227 case DW_OP_breg14:
3228 case DW_OP_breg15:
3229 case DW_OP_breg16:
3230 case DW_OP_breg17:
3231 case DW_OP_breg18:
3232 case DW_OP_breg19:
3233 case DW_OP_breg20:
3234 case DW_OP_breg21:
3235 case DW_OP_breg22:
3236 case DW_OP_breg23:
3237 case DW_OP_breg24:
3238 case DW_OP_breg25:
3239 case DW_OP_breg26:
3240 case DW_OP_breg27:
3241 case DW_OP_breg28:
3242 case DW_OP_breg29:
3243 case DW_OP_breg30:
3244 case DW_OP_breg31:
3245 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3246 break;
3247 case DW_OP_regx:
3248 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3249 break;
3250 case DW_OP_fbreg:
3251 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3252 break;
3253 case DW_OP_bregx:
3254 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3255 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3256 break;
3257 case DW_OP_piece:
3258 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3259 break;
3260 case DW_OP_deref_size:
3261 case DW_OP_xderef_size:
3262 size += 1;
3263 break;
931e9893 3264 case DW_OP_call2:
3265 size += 2;
3266 break;
3267 case DW_OP_call4:
3268 size += 4;
3269 break;
3270 case DW_OP_call_ref:
3271 size += DWARF2_ADDR_SIZE;
3272 break;
8a8bfbe7 3273 default:
4b72e226 3274 break;
752e49ca 3275 }
4b72e226 3276
3277 return size;
752e49ca 3278}
3279
4b72e226 3280/* Return the size of a series of location descriptors. */
ec1e49cc 3281
4b72e226 3282static unsigned long
8ec3a57b 3283size_of_locs (dw_loc_descr_ref loc)
752e49ca 3284{
2fa2456e 3285 dw_loc_descr_ref l;
8c3f468d 3286 unsigned long size;
4b72e226 3287
2fa2456e 3288 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3289 field, to avoid writing to a PCH file. */
3290 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
9ed904da 3291 {
2fa2456e 3292 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3293 break;
3294 size += size_of_loc_descr (l);
3295 }
3296 if (! l)
3297 return size;
3298
3299 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3300 {
3301 l->dw_loc_addr = size;
3302 size += size_of_loc_descr (l);
9ed904da 3303 }
4b72e226 3304
3305 return size;
752e49ca 3306}
3307
4b72e226 3308/* Output location description stack opcode's operands (if any). */
ec1e49cc 3309
4b72e226 3310static void
8ec3a57b 3311output_loc_operands (dw_loc_descr_ref loc)
30ade641 3312{
19cb6b50 3313 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3314 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4b72e226 3315
3316 switch (loc->dw_loc_opc)
30ade641 3317 {
a6c3bce6 3318#ifdef DWARF2_DEBUGGING_INFO
8a8bfbe7 3319 case DW_OP_addr:
ca98eb0a 3320 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4b72e226 3321 break;
8a8bfbe7 3322 case DW_OP_const2u:
8a8bfbe7 3323 case DW_OP_const2s:
ca98eb0a 3324 dw2_asm_output_data (2, val1->v.val_int, NULL);
4b72e226 3325 break;
8a8bfbe7 3326 case DW_OP_const4u:
8a8bfbe7 3327 case DW_OP_const4s:
ca98eb0a 3328 dw2_asm_output_data (4, val1->v.val_int, NULL);
4b72e226 3329 break;
8a8bfbe7 3330 case DW_OP_const8u:
8a8bfbe7 3331 case DW_OP_const8s:
7bd4f6b6 3332 gcc_assert (HOST_BITS_PER_LONG >= 64);
ca98eb0a 3333 dw2_asm_output_data (8, val1->v.val_int, NULL);
4b72e226 3334 break;
a6c3bce6 3335 case DW_OP_skip:
3336 case DW_OP_bra:
9ed904da 3337 {
3338 int offset;
3339
7bd4f6b6 3340 gcc_assert (val1->val_class == dw_val_class_loc);
3341 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
9ed904da 3342
ca98eb0a 3343 dw2_asm_output_data (2, offset, NULL);
9ed904da 3344 }
a6c3bce6 3345 break;
ccd12125 3346#else
3347 case DW_OP_addr:
3348 case DW_OP_const2u:
3349 case DW_OP_const2s:
3350 case DW_OP_const4u:
3351 case DW_OP_const4s:
3352 case DW_OP_const8u:
3353 case DW_OP_const8s:
3354 case DW_OP_skip:
3355 case DW_OP_bra:
3356 /* We currently don't make any attempt to make sure these are
c83a163c 3357 aligned properly like we do for the main unwind info, so
3358 don't support emitting things larger than a byte if we're
3359 only doing unwinding. */
7bd4f6b6 3360 gcc_unreachable ();
a6c3bce6 3361#endif
3362 case DW_OP_const1u:
3363 case DW_OP_const1s:
ca98eb0a 3364 dw2_asm_output_data (1, val1->v.val_int, NULL);
a6c3bce6 3365 break;
8a8bfbe7 3366 case DW_OP_constu:
ca98eb0a 3367 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4b72e226 3368 break;
8a8bfbe7 3369 case DW_OP_consts:
ca98eb0a 3370 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4b72e226 3371 break;
3372 case DW_OP_pick:
ca98eb0a 3373 dw2_asm_output_data (1, val1->v.val_int, NULL);
4b72e226 3374 break;
3375 case DW_OP_plus_uconst:
ca98eb0a 3376 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4b72e226 3377 break;
8a8bfbe7 3378 case DW_OP_breg0:
8a8bfbe7 3379 case DW_OP_breg1:
8a8bfbe7 3380 case DW_OP_breg2:
8a8bfbe7 3381 case DW_OP_breg3:
8a8bfbe7 3382 case DW_OP_breg4:
8a8bfbe7 3383 case DW_OP_breg5:
8a8bfbe7 3384 case DW_OP_breg6:
8a8bfbe7 3385 case DW_OP_breg7:
8a8bfbe7 3386 case DW_OP_breg8:
8a8bfbe7 3387 case DW_OP_breg9:
8a8bfbe7 3388 case DW_OP_breg10:
8a8bfbe7 3389 case DW_OP_breg11:
8a8bfbe7 3390 case DW_OP_breg12:
8a8bfbe7 3391 case DW_OP_breg13:
8a8bfbe7 3392 case DW_OP_breg14:
8a8bfbe7 3393 case DW_OP_breg15:
8a8bfbe7 3394 case DW_OP_breg16:
8a8bfbe7 3395 case DW_OP_breg17:
8a8bfbe7 3396 case DW_OP_breg18:
8a8bfbe7 3397 case DW_OP_breg19:
8a8bfbe7 3398 case DW_OP_breg20:
8a8bfbe7 3399 case DW_OP_breg21:
8a8bfbe7 3400 case DW_OP_breg22:
8a8bfbe7 3401 case DW_OP_breg23:
8a8bfbe7 3402 case DW_OP_breg24:
8a8bfbe7 3403 case DW_OP_breg25:
8a8bfbe7 3404 case DW_OP_breg26:
8a8bfbe7 3405 case DW_OP_breg27:
8a8bfbe7 3406 case DW_OP_breg28:
8a8bfbe7 3407 case DW_OP_breg29:
8a8bfbe7 3408 case DW_OP_breg30:
8a8bfbe7 3409 case DW_OP_breg31:
ca98eb0a 3410 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4b72e226 3411 break;
8a8bfbe7 3412 case DW_OP_regx:
ca98eb0a 3413 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4b72e226 3414 break;
8a8bfbe7 3415 case DW_OP_fbreg:
ca98eb0a 3416 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4b72e226 3417 break;
8a8bfbe7 3418 case DW_OP_bregx:
ca98eb0a 3419 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3420 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4b72e226 3421 break;
8a8bfbe7 3422 case DW_OP_piece:
ca98eb0a 3423 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4b72e226 3424 break;
8a8bfbe7 3425 case DW_OP_deref_size:
8a8bfbe7 3426 case DW_OP_xderef_size:
ca98eb0a 3427 dw2_asm_output_data (1, val1->v.val_int, NULL);
4b72e226 3428 break;
931e9893 3429
3430 case INTERNAL_DW_OP_tls_addr:
40af64cc 3431 if (targetm.asm_out.output_dwarf_dtprel)
3432 {
3433 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3434 DWARF2_ADDR_SIZE,
3435 val1->v.val_addr);
3436 fputc ('\n', asm_out_file);
3437 }
3438 else
3439 gcc_unreachable ();
931e9893 3440 break;
3441
4b72e226 3442 default:
ccd12125 3443 /* Other codes have no operands. */
3444 break;
4b72e226 3445 }
3446}
3447
3448/* Output a sequence of location operations. */
3449
3450static void
8ec3a57b 3451output_loc_sequence (dw_loc_descr_ref loc)
4b72e226 3452{
3453 for (; loc != NULL; loc = loc->dw_loc_next)
3454 {
3455 /* Output the opcode. */
ca98eb0a 3456 dw2_asm_output_data (1, loc->dw_loc_opc,
3457 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4b72e226 3458
3459 /* Output the operand(s) (if any). */
3460 output_loc_operands (loc);
3461 }
3462}
3463
3464/* This routine will generate the correct assembly data for a location
3465 description based on a cfi entry with a complex address. */
3466
3467static void
8ec3a57b 3468output_cfa_loc (dw_cfi_ref cfi)
4b72e226 3469{
3470 dw_loc_descr_ref loc;
3471 unsigned long size;
3472
3473 /* Output the size of the block. */
3474 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3475 size = size_of_locs (loc);
ca98eb0a 3476 dw2_asm_output_data_uleb128 (size, NULL);
4b72e226 3477
3478 /* Now output the operations themselves. */
3479 output_loc_sequence (loc);
3480}
3481
89fa767a 3482/* This function builds a dwarf location descriptor sequence from a
3483 dw_cfa_location, adding the given OFFSET to the result of the
3484 expression. */
4b72e226 3485
3486static struct dw_loc_descr_struct *
89fa767a 3487build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4b72e226 3488{
3489 struct dw_loc_descr_struct *head, *tmp;
3490
89fa767a 3491 offset += cfa->offset;
3492
12d886b8 3493 if (cfa->indirect)
5f19af7a 3494 {
12d886b8 3495 if (cfa->base_offset)
3496 {
3497 if (cfa->reg <= 31)
3498 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3499 else
3500 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3501 }
3502 else if (cfa->reg <= 31)
3503 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
5f19af7a 3504 else
12d886b8 3505 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3506
3507 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3508 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3509 add_loc_descr (&head, tmp);
89fa767a 3510 if (offset != 0)
12d886b8 3511 {
89fa767a 3512 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
12d886b8 3513 add_loc_descr (&head, tmp);
3514 }
5f19af7a 3515 }
5f19af7a 3516 else
4b72e226 3517 {
89fa767a 3518 if (offset == 0)
12d886b8 3519 if (cfa->reg <= 31)
3520 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3521 else
3522 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3523 else if (cfa->reg <= 31)
89fa767a 3524 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
12d886b8 3525 else
89fa767a 3526 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
4b72e226 3527 }
8c3f468d 3528
4b72e226 3529 return head;
3530}
3531
8c3f468d 3532/* This function fills in aa dw_cfa_location structure from a dwarf location
3533 descriptor sequence. */
4b72e226 3534
3535static void
8ec3a57b 3536get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4b72e226 3537{
f80d1bcd 3538 struct dw_loc_descr_struct *ptr;
4b72e226 3539 cfa->offset = 0;
3540 cfa->base_offset = 0;
3541 cfa->indirect = 0;
3542 cfa->reg = -1;
3543
3544 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3545 {
3546 enum dwarf_location_atom op = ptr->dw_loc_opc;
8c3f468d 3547
4b72e226 3548 switch (op)
f80d1bcd 3549 {
4b72e226 3550 case DW_OP_reg0:
3551 case DW_OP_reg1:
3552 case DW_OP_reg2:
3553 case DW_OP_reg3:
3554 case DW_OP_reg4:
3555 case DW_OP_reg5:
3556 case DW_OP_reg6:
3557 case DW_OP_reg7:
3558 case DW_OP_reg8:
3559 case DW_OP_reg9:
3560 case DW_OP_reg10:
3561 case DW_OP_reg11:
3562 case DW_OP_reg12:
3563 case DW_OP_reg13:
3564 case DW_OP_reg14:
3565 case DW_OP_reg15:
3566 case DW_OP_reg16:
3567 case DW_OP_reg17:
3568 case DW_OP_reg18:
3569 case DW_OP_reg19:
3570 case DW_OP_reg20:
3571 case DW_OP_reg21:
3572 case DW_OP_reg22:
3573 case DW_OP_reg23:
3574 case DW_OP_reg24:
3575 case DW_OP_reg25:
3576 case DW_OP_reg26:
3577 case DW_OP_reg27:
3578 case DW_OP_reg28:
3579 case DW_OP_reg29:
3580 case DW_OP_reg30:
3581 case DW_OP_reg31:
3582 cfa->reg = op - DW_OP_reg0;
3583 break;
3584 case DW_OP_regx:
3585 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3586 break;
3587 case DW_OP_breg0:
3588 case DW_OP_breg1:
3589 case DW_OP_breg2:
3590 case DW_OP_breg3:
3591 case DW_OP_breg4:
3592 case DW_OP_breg5:
3593 case DW_OP_breg6:
3594 case DW_OP_breg7:
3595 case DW_OP_breg8:
3596 case DW_OP_breg9:
3597 case DW_OP_breg10:
3598 case DW_OP_breg11:
3599 case DW_OP_breg12:
3600 case DW_OP_breg13:
3601 case DW_OP_breg14:
3602 case DW_OP_breg15:
3603 case DW_OP_breg16:
3604 case DW_OP_breg17:
3605 case DW_OP_breg18:
3606 case DW_OP_breg19:
3607 case DW_OP_breg20:
3608 case DW_OP_breg21:
3609 case DW_OP_breg22:
3610 case DW_OP_breg23:
3611 case DW_OP_breg24:
3612 case DW_OP_breg25:
3613 case DW_OP_breg26:
3614 case DW_OP_breg27:
3615 case DW_OP_breg28:
3616 case DW_OP_breg29:
3617 case DW_OP_breg30:
3618 case DW_OP_breg31:
3619 cfa->reg = op - DW_OP_breg0;
3620 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3621 break;
3622 case DW_OP_bregx:
3623 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3624 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3625 break;
3626 case DW_OP_deref:
3627 cfa->indirect = 1;
3628 break;
3629 case DW_OP_plus_uconst:
f80d1bcd 3630 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4b72e226 3631 break;
3632 default:
0a81f5a0 3633 internal_error ("DW_LOC_OP %s not implemented",
f060a027 3634 dwarf_stack_op_name (ptr->dw_loc_opc));
4b72e226 3635 }
3636 }
3637}
3638#endif /* .debug_frame support */
3639\f
3640/* And now, the support for symbolic debugging information. */
3641#ifdef DWARF2_DEBUGGING_INFO
3642
c366eeee 3643/* .debug_str support. */
8ec3a57b 3644static int output_indirect_string (void **, void *);
3645
3646static void dwarf2out_init (const char *);
3647static void dwarf2out_finish (const char *);
3648static void dwarf2out_define (unsigned int, const char *);
3649static void dwarf2out_undef (unsigned int, const char *);
3650static void dwarf2out_start_source_file (unsigned, const char *);
3651static void dwarf2out_end_source_file (unsigned);
3652static void dwarf2out_begin_block (unsigned, unsigned);
3653static void dwarf2out_end_block (unsigned, unsigned);
3654static bool dwarf2out_ignore_block (tree);
3655static void dwarf2out_global_decl (tree);
73ae3ef7 3656static void dwarf2out_type_decl (tree, int);
2b49746a 3657static void dwarf2out_imported_module_or_decl (tree, tree);
8ec3a57b 3658static void dwarf2out_abstract_function (tree);
b2025850 3659static void dwarf2out_var_location (rtx);
3660static void dwarf2out_begin_function (tree);
1897b881 3661static void dwarf2out_switch_text_section (void);
c140b944 3662
3663/* The debug hooks structure. */
3664
e42f6423 3665const struct gcc_debug_hooks dwarf2_debug_hooks =
c140b944 3666{
3667 dwarf2out_init,
3668 dwarf2out_finish,
3669 dwarf2out_define,
3670 dwarf2out_undef,
3671 dwarf2out_start_source_file,
1dff614c 3672 dwarf2out_end_source_file,
3673 dwarf2out_begin_block,
b9b7f8b4 3674 dwarf2out_end_block,
b29760a8 3675 dwarf2out_ignore_block,
b9b7f8b4 3676 dwarf2out_source_line,
f76df888 3677 dwarf2out_begin_prologue,
e74e8242 3678 debug_nothing_int_charstar, /* end_prologue */
b9b7f8b4 3679 dwarf2out_end_epilogue,
b2025850 3680 dwarf2out_begin_function,
c37d72e9 3681 debug_nothing_int, /* end_function */
3682 dwarf2out_decl, /* function_decl */
3683 dwarf2out_global_decl,
73ae3ef7 3684 dwarf2out_type_decl, /* type_decl */
2b49746a 3685 dwarf2out_imported_module_or_decl,
b29760a8 3686 debug_nothing_tree, /* deferred_inline_function */
3687 /* The DWARF 2 backend tries to reduce debugging bloat by not
3688 emitting the abstract description of inline functions until
3689 something tries to reference them. */
3690 dwarf2out_abstract_function, /* outlining_inline_function */
cf8e41a4 3691 debug_nothing_rtx, /* label */
5923a5e7 3692 debug_nothing_int, /* handle_pch */
7a4afb3f 3693 dwarf2out_var_location,
1897b881 3694 dwarf2out_switch_text_section,
7a4afb3f 3695 1 /* start_end_main_source_file */
c140b944 3696};
573aba85 3697#endif
c140b944 3698\f
4b72e226 3699/* NOTE: In the comments in this file, many references are made to
3700 "Debugging Information Entries". This term is abbreviated as `DIE'
3701 throughout the remainder of this file. */
3702
3703/* An internal representation of the DWARF output is built, and then
3704 walked to generate the DWARF debugging info. The walk of the internal
3705 representation is done after the entire program has been compiled.
3706 The types below are used to describe the internal representation. */
3707
3708/* Various DIE's use offsets relative to the beginning of the
3709 .debug_info section to refer to each other. */
3710
3711typedef long int dw_offset;
3712
3713/* Define typedefs here to avoid circular dependencies. */
3714
3715typedef struct dw_attr_struct *dw_attr_ref;
3716typedef struct dw_line_info_struct *dw_line_info_ref;
3717typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3718typedef struct pubname_struct *pubname_ref;
a36145ca 3719typedef struct dw_ranges_struct *dw_ranges_ref;
f221c0bd 3720typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4b72e226 3721
3722/* Each entry in the line_info_table maintains the file and
3723 line number associated with the label generated for that
3724 entry. The label gives the PC value associated with
3725 the line number entry. */
3726
573aba85 3727typedef struct dw_line_info_struct GTY(())
4b72e226 3728{
3729 unsigned long dw_file_num;
3730 unsigned long dw_line_num;
3731}
3732dw_line_info_entry;
3733
3734/* Line information for functions in separate sections; each one gets its
3735 own sequence. */
573aba85 3736typedef struct dw_separate_line_info_struct GTY(())
4b72e226 3737{
3738 unsigned long dw_file_num;
3739 unsigned long dw_line_num;
3740 unsigned long function;
3741}
3742dw_separate_line_info_entry;
3743
3744/* Each DIE attribute has a field specifying the attribute kind,
3745 a link to the next attribute in the chain, and an attribute value.
3746 Attributes are typically linked below the DIE they modify. */
3747
573aba85 3748typedef struct dw_attr_struct GTY(())
4b72e226 3749{
3750 enum dwarf_attribute dw_attr;
4b72e226 3751 dw_val_node dw_attr_val;
3752}
3753dw_attr_node;
3754
6f56c055 3755DEF_VEC_O(dw_attr_node);
3756DEF_VEC_ALLOC_O(dw_attr_node,gc);
3757
958656b7 3758/* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3759 The children of each node form a circular list linked by
3760 die_sib. die_child points to the node *before* the "first" child node. */
4b72e226 3761
573aba85 3762typedef struct die_struct GTY(())
4b72e226 3763{
3764 enum dwarf_tag die_tag;
19f716e5 3765 char *die_symbol;
6f56c055 3766 VEC(dw_attr_node,gc) * die_attr;
4b72e226 3767 dw_die_ref die_parent;
3768 dw_die_ref die_child;
3769 dw_die_ref die_sib;
023dc493 3770 dw_die_ref die_definition; /* ref from a specification to its definition */
4b72e226 3771 dw_offset die_offset;
3772 unsigned long die_abbrev;
eabb26f3 3773 int die_mark;
f6e59711 3774 /* Die is used and must not be pruned as unused. */
3775 int die_perennial_p;
26863140 3776 unsigned int decl_id;
4b72e226 3777}
3778die_node;
3779
958656b7 3780/* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3781#define FOR_EACH_CHILD(die, c, expr) do { \
3782 c = die->die_child; \
3783 if (c) do { \
3784 c = c->die_sib; \
3785 expr; \
3786 } while (c != die->die_child); \
3787} while (0)
3788
4b72e226 3789/* The pubname structure */
3790
573aba85 3791typedef struct pubname_struct GTY(())
4b72e226 3792{
3793 dw_die_ref die;
52570507 3794 const char *name;
4b72e226 3795}
3796pubname_entry;
3797
af84796a 3798DEF_VEC_O(pubname_entry);
3799DEF_VEC_ALLOC_O(pubname_entry, gc);
3800
573aba85 3801struct dw_ranges_struct GTY(())
a36145ca 3802{
f221c0bd 3803 /* If this is positive, it's a block number, otherwise it's a
3804 bitwise-negated index into dw_ranges_by_label. */
3805 int num;
3806};
3807
3808struct dw_ranges_by_label_struct GTY(())
3809{
3810 const char *begin;
3811 const char *end;
a36145ca 3812};
3813
4b72e226 3814/* The limbo die list structure. */
573aba85 3815typedef struct limbo_die_struct GTY(())
4b72e226 3816{
3817 dw_die_ref die;
15cfae4e 3818 tree created_for;
4b72e226 3819 struct limbo_die_struct *next;
3820}
3821limbo_die_node;
3822
3823/* How to start an assembler comment. */
3824#ifndef ASM_COMMENT_START
3825#define ASM_COMMENT_START ";#"
3826#endif
3827
6ef828f9 3828/* Define a macro which returns nonzero for a TYPE_DECL which was
4b72e226 3829 implicitly generated for a tagged type.
3830
3831 Note that unlike the gcc front end (which generates a NULL named
3832 TYPE_DECL node for each complete tagged type, each array type, and
3833 each function type node created) the g++ front end generates a
3834 _named_ TYPE_DECL node for each tagged type node created.
3835 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3836 generate a DW_TAG_typedef DIE for them. */
3837
3838#define TYPE_DECL_IS_STUB(decl) \
3839 (DECL_NAME (decl) == NULL_TREE \
3840 || (DECL_ARTIFICIAL (decl) \
3841 && is_tagged_type (TREE_TYPE (decl)) \
3842 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3843 /* This is necessary for stub decls that \
3844 appear in nested inline functions. */ \
3845 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3846 && (decl_ultimate_origin (decl) \
3847 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3848
3849/* Information concerning the compilation unit's programming
3850 language, and compiler version. */
3851
4b72e226 3852/* Fixed size portion of the DWARF compilation unit header. */
65bdc57c 3853#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3854 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4b72e226 3855
4b72e226 3856/* Fixed size portion of public names info. */
3857#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3858
3859/* Fixed size portion of the address range info. */
3860#define DWARF_ARANGES_HEADER_SIZE \
38c41660 3861 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
61a9389f 3862 DWARF2_ADDR_SIZE * 2) \
38c41660 3863 - DWARF_INITIAL_LENGTH_SIZE)
4b72e226 3864
3865/* Size of padding portion in the address range info. It must be
3866 aligned to twice the pointer size. */
3867#define DWARF_ARANGES_PAD_SIZE \
38c41660 3868 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
61a9389f 3869 DWARF2_ADDR_SIZE * 2) \
38c41660 3870 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4b72e226 3871
142cf471 3872/* Use assembler line directives if available. */
4b72e226 3873#ifndef DWARF2_ASM_LINE_DEBUG_INFO
142cf471 3874#ifdef HAVE_AS_DWARF2_DEBUG_LINE
3875#define DWARF2_ASM_LINE_DEBUG_INFO 1
3876#else
4b72e226 3877#define DWARF2_ASM_LINE_DEBUG_INFO 0
3878#endif
142cf471 3879#endif
4b72e226 3880
4b72e226 3881/* Minimum line offset in a special line info. opcode.
3882 This value was chosen to give a reasonable range of values. */
3883#define DWARF_LINE_BASE -10
3884
3fb1e43b 3885/* First special line opcode - leave room for the standard opcodes. */
4b72e226 3886#define DWARF_LINE_OPCODE_BASE 10
3887
3888/* Range of line offsets in a special line info. opcode. */
3889#define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3890
3891/* Flag that indicates the initial value of the is_stmt_start flag.
3892 In the present implementation, we do not mark any lines as
3893 the beginning of a source statement, because that information
3894 is not made available by the GCC front-end. */
3895#define DWARF_LINE_DEFAULT_IS_STMT_START 1
3896
38ac91bf 3897#ifdef DWARF2_DEBUGGING_INFO
4b72e226 3898/* This location is used by calc_die_sizes() to keep track
3899 the offset of each DIE within the .debug_info section. */
3900static unsigned long next_die_offset;
38ac91bf 3901#endif
4b72e226 3902
3903/* Record the root of the DIE's built for the current compilation unit. */
573aba85 3904static GTY(()) dw_die_ref comp_unit_die;
4b72e226 3905
3906/* A list of DIEs with a NULL parent waiting to be relocated. */
573aba85 3907static GTY(()) limbo_die_node *limbo_die_list;
4b72e226 3908
3740694f 3909/* Filenames referenced by this compilation unit. */
69278c24 3910static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5a3023d9 3911
26863140 3912/* A hash table of references to DIE's that describe declarations.
3913 The key is a DECL_UID() which is a unique number identifying each decl. */
3914static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4b72e226 3915
b2025850 3916/* Node of the variable location list. */
3917struct var_loc_node GTY ((chain_next ("%h.next")))
3918{
3919 rtx GTY (()) var_loc_note;
3920 const char * GTY (()) label;
1897b881 3921 const char * GTY (()) section_label;
b2025850 3922 struct var_loc_node * GTY (()) next;
3923};
3924
3925/* Variable location list. */
3926struct var_loc_list_def GTY (())
3927{
3928 struct var_loc_node * GTY (()) first;
3929
3930 /* Do not mark the last element of the chained list because
3931 it is marked through the chain. */
3932 struct var_loc_node * GTY ((skip ("%h"))) last;
3933
3934 /* DECL_UID of the variable decl. */
3935 unsigned int decl_id;
3936};
3937typedef struct var_loc_list_def var_loc_list;
3938
b2025850 3939
3940/* Table of decl location linked lists. */
3941static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3942
4b72e226 3943/* A pointer to the base of a list of references to DIE's that
3944 are uniquely identified by their tag, presence/absence of
3945 children DIE's, and list of attribute/value pairs. */
8ec3a57b 3946static GTY((length ("abbrev_die_table_allocated")))
573aba85 3947 dw_die_ref *abbrev_die_table;
4b72e226 3948
3949/* Number of elements currently allocated for abbrev_die_table. */
909be935 3950static GTY(()) unsigned abbrev_die_table_allocated;
4b72e226 3951
3952/* Number of elements in type_die_table currently in use. */
909be935 3953static GTY(()) unsigned abbrev_die_table_in_use;
4b72e226 3954
3955/* Size (in elements) of increments by which we may expand the
3956 abbrev_die_table. */
3957#define ABBREV_DIE_TABLE_INCREMENT 256
3958
3959/* A pointer to the base of a table that contains line information
3960 for each source code line in .text in the compilation unit. */
8ec3a57b 3961static GTY((length ("line_info_table_allocated")))
573aba85 3962 dw_line_info_ref line_info_table;
4b72e226 3963
3964/* Number of elements currently allocated for line_info_table. */
909be935 3965static GTY(()) unsigned line_info_table_allocated;
4b72e226 3966
573aba85 3967/* Number of elements in line_info_table currently in use. */
909be935 3968static GTY(()) unsigned line_info_table_in_use;
4b72e226 3969
dae1861f 3970/* True if the compilation unit places functions in more than one section. */
3971static GTY(()) bool have_multiple_function_sections = false;
3eb32482 3972
4b72e226 3973/* A pointer to the base of a table that contains line information
3974 for each source code line outside of .text in the compilation unit. */
573aba85 3975static GTY ((length ("separate_line_info_table_allocated")))
3976 dw_separate_line_info_ref separate_line_info_table;
4b72e226 3977
3978/* Number of elements currently allocated for separate_line_info_table. */
909be935 3979static GTY(()) unsigned separate_line_info_table_allocated;
4b72e226 3980
573aba85 3981/* Number of elements in separate_line_info_table currently in use. */
909be935 3982static GTY(()) unsigned separate_line_info_table_in_use;
4b72e226 3983
3984/* Size (in elements) of increments by which we may expand the
3985 line_info_table. */
3986#define LINE_INFO_TABLE_INCREMENT 1024
3987
3988/* A pointer to the base of a table that contains a list of publicly
3989 accessible names. */
af84796a 3990static GTY (()) VEC (pubname_entry, gc) * pubname_table;
4b72e226 3991
af84796a 3992/* A pointer to the base of a table that contains a list of publicly
3993 accessible types. */
3994static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4b72e226 3995
a36145ca 3996/* Array of dies for which we should generate .debug_arange info. */
573aba85 3997static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4b72e226 3998
3999/* Number of elements currently allocated for arange_table. */
909be935 4000static GTY(()) unsigned arange_table_allocated;
4b72e226 4001
4002/* Number of elements in arange_table currently in use. */
909be935 4003static GTY(()) unsigned arange_table_in_use;
4b72e226 4004
4005/* Size (in elements) of increments by which we may expand the
4006 arange_table. */
4007#define ARANGE_TABLE_INCREMENT 64
4008
a36145ca 4009/* Array of dies for which we should generate .debug_ranges info. */
573aba85 4010static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
a36145ca 4011
4012/* Number of elements currently allocated for ranges_table. */
909be935 4013static GTY(()) unsigned ranges_table_allocated;
a36145ca 4014
4015/* Number of elements in ranges_table currently in use. */
909be935 4016static GTY(()) unsigned ranges_table_in_use;
a36145ca 4017
f221c0bd 4018/* Array of pairs of labels referenced in ranges_table. */
4019static GTY ((length ("ranges_by_label_allocated")))
4020 dw_ranges_by_label_ref ranges_by_label;
4021
4022/* Number of elements currently allocated for ranges_by_label. */
4023static GTY(()) unsigned ranges_by_label_allocated;
4024
4025/* Number of elements in ranges_by_label currently in use. */
4026static GTY(()) unsigned ranges_by_label_in_use;
4027
a36145ca 4028/* Size (in elements) of increments by which we may expand the
4029 ranges_table. */
4030#define RANGES_TABLE_INCREMENT 64
4031
4c21a22f 4032/* Whether we have location lists that need outputting */
dae1861f 4033static GTY(()) bool have_location_lists;
4c21a22f 4034
d3cdd238 4035/* Unique label counter. */
4036static GTY(()) unsigned int loclabel_num;
4037
909be935 4038#ifdef DWARF2_DEBUGGING_INFO
4b72e226 4039/* Record whether the function being analyzed contains inlined functions. */
4040static int current_function_has_inlines;
38ac91bf 4041#endif
4b72e226 4042#if 0 && defined (MIPS_DEBUGGING_INFO)
4043static int comp_unit_has_inlines;
4044#endif
4045
69278c24 4046/* The last file entry emitted by maybe_emit_file(). */
4047static GTY(()) struct dwarf_file_data * last_emitted_file;
909be935 4048
6473f3f4 4049/* Number of internal labels generated by gen_internal_sym(). */
909be935 4050static GTY(()) int label_num;
4051
62435250 4052/* Cached result of previous call to lookup_filename. */
4053static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4054
573aba85 4055#ifdef DWARF2_DEBUGGING_INFO
4056
89fa767a 4057/* Offset from the "steady-state frame pointer" to the frame base,
12d886b8 4058 within the current function. */
89fa767a 4059static HOST_WIDE_INT frame_pointer_fb_offset;
12d886b8 4060
4b72e226 4061/* Forward declarations for functions defined in this file. */
4062
8ec3a57b 4063static int is_pseudo_reg (rtx);
4064static tree type_main_variant (tree);
4065static int is_tagged_type (tree);
4066static const char *dwarf_tag_name (unsigned);
4067static const char *dwarf_attr_name (unsigned);
4068static const char *dwarf_form_name (unsigned);
8ec3a57b 4069static tree decl_ultimate_origin (tree);
4070static tree block_ultimate_origin (tree);
4071static tree decl_class_context (tree);
4072static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4073static inline enum dw_val_class AT_class (dw_attr_ref);
4074static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4075static inline unsigned AT_flag (dw_attr_ref);
3d867824 4076static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4077static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4078static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4079static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
8ec3a57b 4080static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4081 unsigned long);
1b6ad376 4082static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4083 unsigned int, unsigned char *);
8ec3a57b 4084static hashval_t debug_str_do_hash (const void *);
4085static int debug_str_eq (const void *, const void *);
4086static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4087static inline const char *AT_string (dw_attr_ref);
4088static int AT_string_form (dw_attr_ref);
4089static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
023dc493 4090static void add_AT_specification (dw_die_ref, dw_die_ref);
8ec3a57b 4091static inline dw_die_ref AT_ref (dw_attr_ref);
4092static inline int AT_ref_external (dw_attr_ref);
4093static inline void set_AT_ref_external (dw_attr_ref, int);
4094static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4095static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4096static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4097static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4098 dw_loc_list_ref);
4099static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4100static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4101static inline rtx AT_addr (dw_attr_ref);
4102static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
d08d29c0 4103static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4104static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3d867824 4105static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4106 unsigned HOST_WIDE_INT);
8ec3a57b 4107static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4108 unsigned long);
4109static inline const char *AT_lbl (dw_attr_ref);
4110static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4111static const char *get_AT_low_pc (dw_die_ref);
4112static const char *get_AT_hi_pc (dw_die_ref);
4113static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4114static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4115static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4116static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4117static bool is_c_family (void);
4118static bool is_cxx (void);
4119static bool is_java (void);
4120static bool is_fortran (void);
4121static bool is_ada (void);
4122static void remove_AT (dw_die_ref, enum dwarf_attribute);
2b49746a 4123static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
8ec3a57b 4124static void add_child_die (dw_die_ref, dw_die_ref);
4125static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4126static dw_die_ref lookup_type_die (tree);
4127static void equate_type_number_to_die (tree, dw_die_ref);
26863140 4128static hashval_t decl_die_table_hash (const void *);
4129static int decl_die_table_eq (const void *, const void *);
8ec3a57b 4130static dw_die_ref lookup_decl_die (tree);
b2025850 4131static hashval_t decl_loc_table_hash (const void *);
4132static int decl_loc_table_eq (const void *, const void *);
4133static var_loc_list *lookup_decl_loc (tree);
8ec3a57b 4134static void equate_decl_number_to_die (tree, dw_die_ref);
b2025850 4135static void add_var_loc_to_decl (tree, struct var_loc_node *);
8ec3a57b 4136static void print_spaces (FILE *);
4137static void print_die (dw_die_ref, FILE *);
4138static void print_dwarf_line_table (FILE *);
8ec3a57b 4139static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4140static dw_die_ref pop_compile_unit (dw_die_ref);
4141static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4142static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4143static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4144static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4145static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4146static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4147static int same_die_p (dw_die_ref, dw_die_ref, int *);
4148static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4149static void compute_section_prefix (dw_die_ref);
4150static int is_type_die (dw_die_ref);
4151static int is_comdat_die (dw_die_ref);
4152static int is_symbol_die (dw_die_ref);
4153static void assign_symbol_names (dw_die_ref);
4154static void break_out_includes (dw_die_ref);
4155static hashval_t htab_cu_hash (const void *);
4156static int htab_cu_eq (const void *, const void *);
4157static void htab_cu_del (void *);
4158static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4159static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4160static void add_sibling_attributes (dw_die_ref);
4161static void build_abbrev_table (dw_die_ref);
4162static void output_location_lists (dw_die_ref);
4163static int constant_size (long unsigned);
4164static unsigned long size_of_die (dw_die_ref);
4165static void calc_die_sizes (dw_die_ref);
4166static void mark_dies (dw_die_ref);
4167static void unmark_dies (dw_die_ref);
4168static void unmark_all_dies (dw_die_ref);
af84796a 4169static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
8ec3a57b 4170static unsigned long size_of_aranges (void);
4171static enum dwarf_form value_format (dw_attr_ref);
4172static void output_value_format (dw_attr_ref);
4173static void output_abbrev_section (void);
4174static void output_die_symbol (dw_die_ref);
4175static void output_die (dw_die_ref);
4176static void output_compilation_unit_header (void);
4177static void output_comp_unit (dw_die_ref, int);
4178static const char *dwarf2_name (tree, int);
4179static void add_pubname (tree, dw_die_ref);
af84796a 4180static void add_pubtype (tree, dw_die_ref);
4181static void output_pubnames (VEC (pubname_entry,gc) *);
8ec3a57b 4182static void add_arange (tree, dw_die_ref);
4183static void output_aranges (void);
f221c0bd 4184static unsigned int add_ranges_num (int);
8ec3a57b 4185static unsigned int add_ranges (tree);
f221c0bd 4186static unsigned int add_ranges_by_labels (const char *, const char *);
8ec3a57b 4187static void output_ranges (void);
4188static void output_line_info (void);
4189static void output_file_names (void);
4190static dw_die_ref base_type_die (tree);
8ec3a57b 4191static int is_base_type (tree);
6114cbf0 4192static bool is_subrange_type (tree);
a7011153 4193static dw_die_ref subrange_type_die (tree, dw_die_ref);
8ec3a57b 4194static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4195static int type_is_enum (tree);
7f3ca0ce 4196static unsigned int dbx_reg_number (rtx);
fd51758c 4197static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
d53bb226 4198static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4199static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4200 enum var_init_status);
4201static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4202 enum var_init_status);
8ec3a57b 4203static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
d53bb226 4204static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4205 enum var_init_status);
8ec3a57b 4206static int is_based_loc (rtx);
d53bb226 4207static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4208 enum var_init_status);
4209static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4210 enum var_init_status);
4211static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
afcf285e 4212static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4213static dw_loc_descr_ref loc_descriptor_from_tree (tree);
8ec3a57b 4214static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4215static tree field_type (tree);
4216static unsigned int simple_type_align_in_bits (tree);
4217static unsigned int simple_decl_align_in_bits (tree);
4218static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4219static HOST_WIDE_INT field_byte_offset (tree);
4220static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4221 dw_loc_descr_ref);
4222static void add_data_member_location_attribute (dw_die_ref, tree);
4223static void add_const_value_attribute (dw_die_ref, rtx);
1b6ad376 4224static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4225static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4226static void insert_float (rtx, unsigned char *);
8ec3a57b 4227static rtx rtl_for_decl_location (tree);
b2025850 4228static void add_location_or_const_value_attribute (dw_die_ref, tree,
4229 enum dwarf_attribute);
8ec3a57b 4230static void tree_add_const_value_attribute (dw_die_ref, tree);
4231static void add_name_attribute (dw_die_ref, const char *);
4232static void add_comp_dir_attribute (dw_die_ref);
4233static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4234static void add_subscript_info (dw_die_ref, tree);
4235static void add_byte_size_attribute (dw_die_ref, tree);
4236static void add_bit_offset_attribute (dw_die_ref, tree);
4237static void add_bit_size_attribute (dw_die_ref, tree);
4238static void add_prototyped_attribute (dw_die_ref, tree);
4239static void add_abstract_origin_attribute (dw_die_ref, tree);
4240static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4241static void add_src_coords_attributes (dw_die_ref, tree);
4242static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4243static void push_decl_scope (tree);
4244static void pop_decl_scope (void);
4245static dw_die_ref scope_die_for (tree, dw_die_ref);
4246static inline int local_scope_p (dw_die_ref);
e89530cd 4247static inline int class_or_namespace_scope_p (dw_die_ref);
8ec3a57b 4248static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
8ff30ff6 4249static void add_calling_convention_attribute (dw_die_ref, tree);
8ec3a57b 4250static const char *type_tag (tree);
4251static tree member_declared_type (tree);
4b72e226 4252#if 0
8ec3a57b 4253static const char *decl_start_label (tree);
4b72e226 4254#endif
8ec3a57b 4255static void gen_array_type_die (tree, dw_die_ref);
4b72e226 4256#if 0
8ec3a57b 4257static void gen_entry_point_die (tree, dw_die_ref);
4b72e226 4258#endif
8ec3a57b 4259static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4260static void gen_inlined_structure_type_die (tree, dw_die_ref);
4261static void gen_inlined_union_type_die (tree, dw_die_ref);
93c7db82 4262static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
8ec3a57b 4263static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4264static void gen_unspecified_parameters_die (tree, dw_die_ref);
4265static void gen_formal_types_die (tree, dw_die_ref);
4266static void gen_subprogram_die (tree, dw_die_ref);
4267static void gen_variable_die (tree, dw_die_ref);
4268static void gen_label_die (tree, dw_die_ref);
4269static void gen_lexical_block_die (tree, dw_die_ref, int);
4270static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4271static void gen_field_die (tree, dw_die_ref);
4272static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4273static dw_die_ref gen_compile_unit_die (const char *);
8ec3a57b 4274static void gen_inheritance_die (tree, tree, dw_die_ref);
4275static void gen_member_die (tree, dw_die_ref);
0e4744ac 4276static void gen_struct_or_union_type_die (tree, dw_die_ref,
4277 enum debug_info_usage);
8ec3a57b 4278static void gen_subroutine_type_die (tree, dw_die_ref);
4279static void gen_typedef_die (tree, dw_die_ref);
4280static void gen_type_die (tree, dw_die_ref);
4281static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4282static void gen_block_die (tree, dw_die_ref, int);
4283static void decls_for_scope (tree, dw_die_ref, int);
4284static int is_redundant_typedef (tree);
e89530cd 4285static void gen_namespace_die (tree);
8ec3a57b 4286static void gen_decl_die (tree, dw_die_ref);
2b49746a 4287static dw_die_ref force_decl_die (tree);
4288static dw_die_ref force_type_die (tree);
e89530cd 4289static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4290static void declare_in_namespace (tree, dw_die_ref);
69278c24 4291static struct dwarf_file_data * lookup_filename (const char *);
8ec3a57b 4292static void retry_incomplete_types (void);
4293static void gen_type_die_for_member (tree, tree, dw_die_ref);
4294static void splice_child_die (dw_die_ref, dw_die_ref);
4295static int file_info_cmp (const void *, const void *);
4296static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4297 const char *, const char *, unsigned);
4298static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4299 const char *, const char *,
4300 const char *);
4301static void output_loc_list (dw_loc_list_ref);
4302static char *gen_internal_sym (const char *);
4303
4304static void prune_unmark_dies (dw_die_ref);
4305static void prune_unused_types_mark (dw_die_ref, int);
4306static void prune_unused_types_walk (dw_die_ref);
4307static void prune_unused_types_walk_attribs (dw_die_ref);
4308static void prune_unused_types_prune (dw_die_ref);
4309static void prune_unused_types (void);
69278c24 4310static int maybe_emit_file (struct dwarf_file_data *fd);
c83a163c 4311
4b72e226 4312/* Section names used to hold DWARF debugging information. */
4313#ifndef DEBUG_INFO_SECTION
4314#define DEBUG_INFO_SECTION ".debug_info"
4315#endif
049aa99b 4316#ifndef DEBUG_ABBREV_SECTION
4317#define DEBUG_ABBREV_SECTION ".debug_abbrev"
4b72e226 4318#endif
049aa99b 4319#ifndef DEBUG_ARANGES_SECTION
4320#define DEBUG_ARANGES_SECTION ".debug_aranges"
4b72e226 4321#endif
049aa99b 4322#ifndef DEBUG_MACINFO_SECTION
4323#define DEBUG_MACINFO_SECTION ".debug_macinfo"
4b72e226 4324#endif
4325#ifndef DEBUG_LINE_SECTION
4326#define DEBUG_LINE_SECTION ".debug_line"
4327#endif
049aa99b 4328#ifndef DEBUG_LOC_SECTION
4329#define DEBUG_LOC_SECTION ".debug_loc"
4b72e226 4330#endif
049aa99b 4331#ifndef DEBUG_PUBNAMES_SECTION
4332#define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4b72e226 4333#endif
049aa99b 4334#ifndef DEBUG_STR_SECTION
4335#define DEBUG_STR_SECTION ".debug_str"
4b72e226 4336#endif
a36145ca 4337#ifndef DEBUG_RANGES_SECTION
4338#define DEBUG_RANGES_SECTION ".debug_ranges"
4339#endif
4b72e226 4340
4341/* Standard ELF section names for compiled code and data. */
25e5d448 4342#ifndef TEXT_SECTION_NAME
4343#define TEXT_SECTION_NAME ".text"
4b72e226 4344#endif
4345
80b7bd06 4346/* Section flags for .debug_str section. */
80b7bd06 4347#define DEBUG_STR_SECTION_FLAGS \
cdb2d692 4348 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
44bbb5f3 4349 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4350 : SECTION_DEBUG)
80b7bd06 4351
4b72e226 4352/* Labels we insert at beginning sections we can reference instead of
f80d1bcd 4353 the section names themselves. */
4b72e226 4354
4355#ifndef TEXT_SECTION_LABEL
049aa99b 4356#define TEXT_SECTION_LABEL "Ltext"
4b72e226 4357#endif
4d0e931f 4358#ifndef COLD_TEXT_SECTION_LABEL
4359#define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4360#endif
4b72e226 4361#ifndef DEBUG_LINE_SECTION_LABEL
049aa99b 4362#define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4b72e226 4363#endif
4364#ifndef DEBUG_INFO_SECTION_LABEL
049aa99b 4365#define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4b72e226 4366#endif
049aa99b 4367#ifndef DEBUG_ABBREV_SECTION_LABEL
4368#define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4b72e226 4369#endif
049aa99b 4370#ifndef DEBUG_LOC_SECTION_LABEL
4371#define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4c21a22f 4372#endif
fe39c28c 4373#ifndef DEBUG_RANGES_SECTION_LABEL
4374#define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4375#endif
1d340a5e 4376#ifndef DEBUG_MACINFO_SECTION_LABEL
4377#define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4378#endif
a36145ca 4379
4b72e226 4380/* Definitions of defaults for formats and names of various special
4381 (artificial) labels which may be generated within this file (when the -g
ad8d48ea 4382 options is used and DWARF2_DEBUGGING_INFO is in effect.
4b72e226 4383 If necessary, these may be overridden from within the tm.h file, but
4384 typically, overriding these defaults is unnecessary. */
4385
4386static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4387static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4d0e931f 4388static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
61a9389f 4389static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4b72e226 4390static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4391static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4392static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
1d340a5e 4393static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4c21a22f 4394static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
fe39c28c 4395static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
8c3f468d 4396
4b72e226 4397#ifndef TEXT_END_LABEL
4398#define TEXT_END_LABEL "Letext"
4399#endif
4d0e931f 4400#ifndef COLD_END_LABEL
4401#define COLD_END_LABEL "Letext_cold"
4402#endif
4b72e226 4403#ifndef BLOCK_BEGIN_LABEL
4404#define BLOCK_BEGIN_LABEL "LBB"
4405#endif
4406#ifndef BLOCK_END_LABEL
4407#define BLOCK_END_LABEL "LBE"
4408#endif
4b72e226 4409#ifndef LINE_CODE_LABEL
4410#define LINE_CODE_LABEL "LM"
4411#endif
4412#ifndef SEPARATE_LINE_CODE_LABEL
4413#define SEPARATE_LINE_CODE_LABEL "LSM"
4414#endif
4415\f
4416/* We allow a language front-end to designate a function that is to be
822e391f 4417 called to "demangle" any name before it is put into a DIE. */
4b72e226 4418
8ec3a57b 4419static const char *(*demangle_name_func) (const char *);
4b72e226 4420
4421void
8ec3a57b 4422dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4b72e226 4423{
4424 demangle_name_func = func;
4425}
4b72e226 4426
4427/* Test if rtl node points to a pseudo register. */
4428
4429static inline int
8ec3a57b 4430is_pseudo_reg (rtx rtl)
4b72e226 4431{
8ad4c111 4432 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4b72e226 4433 || (GET_CODE (rtl) == SUBREG
701e46d0 4434 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4b72e226 4435}
4436
4437/* Return a reference to a type, with its const and volatile qualifiers
4438 removed. */
4439
4440static inline tree
8ec3a57b 4441type_main_variant (tree type)
4b72e226 4442{
4443 type = TYPE_MAIN_VARIANT (type);
4444
8c3f468d 4445 /* ??? There really should be only one main variant among any group of
4446 variants of a given type (and all of the MAIN_VARIANT values for all
4447 members of the group should point to that one type) but sometimes the C
4448 front-end messes this up for array types, so we work around that bug
4449 here. */
4b72e226 4450 if (TREE_CODE (type) == ARRAY_TYPE)
4451 while (type != TYPE_MAIN_VARIANT (type))
4452 type = TYPE_MAIN_VARIANT (type);
4453
4454 return type;
4455}
4456
6ef828f9 4457/* Return nonzero if the given type node represents a tagged type. */
4b72e226 4458
4459static inline int
8ec3a57b 4460is_tagged_type (tree type)
4b72e226 4461{
19cb6b50 4462 enum tree_code code = TREE_CODE (type);
4b72e226 4463
4464 return (code == RECORD_TYPE || code == UNION_TYPE
4465 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4466}
4467
4468/* Convert a DIE tag into its string name. */
4469
4470static const char *
8ec3a57b 4471dwarf_tag_name (unsigned int tag)
4b72e226 4472{
4473 switch (tag)
4474 {
4475 case DW_TAG_padding:
4476 return "DW_TAG_padding";
4477 case DW_TAG_array_type:
4478 return "DW_TAG_array_type";
4479 case DW_TAG_class_type:
4480 return "DW_TAG_class_type";
4481 case DW_TAG_entry_point:
4482 return "DW_TAG_entry_point";
4483 case DW_TAG_enumeration_type:
4484 return "DW_TAG_enumeration_type";
4485 case DW_TAG_formal_parameter:
4486 return "DW_TAG_formal_parameter";
4487 case DW_TAG_imported_declaration:
4488 return "DW_TAG_imported_declaration";
4489 case DW_TAG_label:
4490 return "DW_TAG_label";
4491 case DW_TAG_lexical_block:
4492 return "DW_TAG_lexical_block";
4493 case DW_TAG_member:
4494 return "DW_TAG_member";
4495 case DW_TAG_pointer_type:
4496 return "DW_TAG_pointer_type";
4497 case DW_TAG_reference_type:
4498 return "DW_TAG_reference_type";
4499 case DW_TAG_compile_unit:
4500 return "DW_TAG_compile_unit";
4501 case DW_TAG_string_type:
4502 return "DW_TAG_string_type";
4503 case DW_TAG_structure_type:
4504 return "DW_TAG_structure_type";
4505 case DW_TAG_subroutine_type:
4506 return "DW_TAG_subroutine_type";
4507 case DW_TAG_typedef:
4508 return "DW_TAG_typedef";
4509 case DW_TAG_union_type:
4510 return "DW_TAG_union_type";
4511 case DW_TAG_unspecified_parameters:
4512 return "DW_TAG_unspecified_parameters";
4513 case DW_TAG_variant:
4514 return "DW_TAG_variant";
4515 case DW_TAG_common_block:
4516 return "DW_TAG_common_block";
4517 case DW_TAG_common_inclusion:
4518 return "DW_TAG_common_inclusion";
4519 case DW_TAG_inheritance:
4520 return "DW_TAG_inheritance";
4521 case DW_TAG_inlined_subroutine:
4522 return "DW_TAG_inlined_subroutine";
4523 case DW_TAG_module:
4524 return "DW_TAG_module";
4525 case DW_TAG_ptr_to_member_type:
4526 return "DW_TAG_ptr_to_member_type";
4527 case DW_TAG_set_type:
4528 return "DW_TAG_set_type";
4529 case DW_TAG_subrange_type:
4530 return "DW_TAG_subrange_type";
4531 case DW_TAG_with_stmt:
4532 return "DW_TAG_with_stmt";
4533 case DW_TAG_access_declaration:
4534 return "DW_TAG_access_declaration";
4535 case DW_TAG_base_type:
4536 return "DW_TAG_base_type";
4537 case DW_TAG_catch_block:
4538 return "DW_TAG_catch_block";
4539 case DW_TAG_const_type:
4540 return "DW_TAG_const_type";
4541 case DW_TAG_constant:
4542 return "DW_TAG_constant";
4543 case DW_TAG_enumerator:
4544 return "DW_TAG_enumerator";
4545 case DW_TAG_file_type:
4546 return "DW_TAG_file_type";
4547 case DW_TAG_friend:
4548 return "DW_TAG_friend";
4549 case DW_TAG_namelist:
4550 return "DW_TAG_namelist";
4551 case DW_TAG_namelist_item:
4552 return "DW_TAG_namelist_item";
e89530cd 4553 case DW_TAG_namespace:
4554 return "DW_TAG_namespace";
4b72e226 4555 case DW_TAG_packed_type:
4556 return "DW_TAG_packed_type";
4557 case DW_TAG_subprogram:
4558 return "DW_TAG_subprogram";
4559 case DW_TAG_template_type_param:
4560 return "DW_TAG_template_type_param";
4561 case DW_TAG_template_value_param:
4562 return "DW_TAG_template_value_param";
4563 case DW_TAG_thrown_type:
4564 return "DW_TAG_thrown_type";
4565 case DW_TAG_try_block:
4566 return "DW_TAG_try_block";
4567 case DW_TAG_variant_part:
4568 return "DW_TAG_variant_part";
4569 case DW_TAG_variable:
4570 return "DW_TAG_variable";
4571 case DW_TAG_volatile_type:
4572 return "DW_TAG_volatile_type";
2b49746a 4573 case DW_TAG_imported_module:
4574 return "DW_TAG_imported_module";
4b72e226 4575 case DW_TAG_MIPS_loop:
4576 return "DW_TAG_MIPS_loop";
4577 case DW_TAG_format_label:
4578 return "DW_TAG_format_label";
4579 case DW_TAG_function_template:
4580 return "DW_TAG_function_template";
4581 case DW_TAG_class_template:
4582 return "DW_TAG_class_template";
19f716e5 4583 case DW_TAG_GNU_BINCL:
4584 return "DW_TAG_GNU_BINCL";
4585 case DW_TAG_GNU_EINCL:
4586 return "DW_TAG_GNU_EINCL";
4b72e226 4587 default:
4588 return "DW_TAG_<unknown>";
4589 }
4590}
4591
4592/* Convert a DWARF attribute code into its string name. */
4593
4594static const char *
8ec3a57b 4595dwarf_attr_name (unsigned int attr)
4b72e226 4596{
4597 switch (attr)
4598 {
4599 case DW_AT_sibling:
4600 return "DW_AT_sibling";
4601 case DW_AT_location:
4602 return "DW_AT_location";
4603 case DW_AT_name:
4604 return "DW_AT_name";
4605 case DW_AT_ordering:
4606 return "DW_AT_ordering";
4607 case DW_AT_subscr_data:
4608 return "DW_AT_subscr_data";
4609 case DW_AT_byte_size:
4610 return "DW_AT_byte_size";
4611 case DW_AT_bit_offset:
4612 return "DW_AT_bit_offset";
4613 case DW_AT_bit_size:
4614 return "DW_AT_bit_size";
4615 case DW_AT_element_list:
4616 return "DW_AT_element_list";
4617 case DW_AT_stmt_list:
4618 return "DW_AT_stmt_list";
4619 case DW_AT_low_pc:
4620 return "DW_AT_low_pc";
4621 case DW_AT_high_pc:
4622 return "DW_AT_high_pc";
4623 case DW_AT_language:
4624 return "DW_AT_language";
4625 case DW_AT_member:
4626 return "DW_AT_member";
4627 case DW_AT_discr:
4628 return "DW_AT_discr";
4629 case DW_AT_discr_value:
4630 return "DW_AT_discr_value";
4631 case DW_AT_visibility:
4632 return "DW_AT_visibility";
4633 case DW_AT_import:
4634 return "DW_AT_import";
4635 case DW_AT_string_length:
4636 return "DW_AT_string_length";
4637 case DW_AT_common_reference:
4638 return "DW_AT_common_reference";
4639 case DW_AT_comp_dir:
4640 return "DW_AT_comp_dir";
4641 case DW_AT_const_value:
4642 return "DW_AT_const_value";
4643 case DW_AT_containing_type:
4644 return "DW_AT_containing_type";
4645 case DW_AT_default_value:
4646 return "DW_AT_default_value";
4647 case DW_AT_inline:
4648 return "DW_AT_inline";
4649 case DW_AT_is_optional:
4650 return "DW_AT_is_optional";
4651 case DW_AT_lower_bound:
4652 return "DW_AT_lower_bound";
4653 case DW_AT_producer:
4654 return "DW_AT_producer";
4655 case DW_AT_prototyped:
4656 return "DW_AT_prototyped";
4657 case DW_AT_return_addr:
4658 return "DW_AT_return_addr";
4659 case DW_AT_start_scope:
4660 return "DW_AT_start_scope";
4661 case DW_AT_stride_size:
4662 return "DW_AT_stride_size";
4663 case DW_AT_upper_bound:
4664 return "DW_AT_upper_bound";
4665 case DW_AT_abstract_origin:
4666 return "DW_AT_abstract_origin";
4667 case DW_AT_accessibility:
4668 return "DW_AT_accessibility";
4669 case DW_AT_address_class:
4670 return "DW_AT_address_class";
4671 case DW_AT_artificial:
4672 return "DW_AT_artificial";
4673 case DW_AT_base_types:
4674 return "DW_AT_base_types";
4675 case DW_AT_calling_convention:
4676 return "DW_AT_calling_convention";
4677 case DW_AT_count:
4678 return "DW_AT_count";
4679 case DW_AT_data_member_location:
4680 return "DW_AT_data_member_location";
4681 case DW_AT_decl_column:
4682 return "DW_AT_decl_column";
4683 case DW_AT_decl_file:
4684 return "DW_AT_decl_file";
4685 case DW_AT_decl_line:
4686 return "DW_AT_decl_line";
4687 case DW_AT_declaration:
4688 return "DW_AT_declaration";
4689 case DW_AT_discr_list:
4690 return "DW_AT_discr_list";
4691 case DW_AT_encoding:
4692 return "DW_AT_encoding";
4693 case DW_AT_external:
4694 return "DW_AT_external";
4695 case DW_AT_frame_base:
4696 return "DW_AT_frame_base";
4697 case DW_AT_friend:
4698 return "DW_AT_friend";
4699 case DW_AT_identifier_case:
4700 return "DW_AT_identifier_case";
4701 case DW_AT_macro_info:
4702 return "DW_AT_macro_info";
4703 case DW_AT_namelist_items:
4704 return "DW_AT_namelist_items";
4705 case DW_AT_priority:
4706 return "DW_AT_priority";
4707 case DW_AT_segment:
4708 return "DW_AT_segment";
4709 case DW_AT_specification:
4710 return "DW_AT_specification";
4711 case DW_AT_static_link:
4712 return "DW_AT_static_link";
4713 case DW_AT_type:
4714 return "DW_AT_type";
4715 case DW_AT_use_location:
4716 return "DW_AT_use_location";
4717 case DW_AT_variable_parameter:
4718 return "DW_AT_variable_parameter";
4719 case DW_AT_virtuality:
4720 return "DW_AT_virtuality";
4721 case DW_AT_vtable_elem_location:
4722 return "DW_AT_vtable_elem_location";
4723
a36145ca 4724 case DW_AT_allocated:
4725 return "DW_AT_allocated";
4726 case DW_AT_associated:
4727 return "DW_AT_associated";
4728 case DW_AT_data_location:
4729 return "DW_AT_data_location";
4730 case DW_AT_stride:
4731 return "DW_AT_stride";
4732 case DW_AT_entry_pc:
4733 return "DW_AT_entry_pc";
4734 case DW_AT_use_UTF8:
4735 return "DW_AT_use_UTF8";
4736 case DW_AT_extension:
4737 return "DW_AT_extension";
4738 case DW_AT_ranges:
4739 return "DW_AT_ranges";
4740 case DW_AT_trampoline:
4741 return "DW_AT_trampoline";
4742 case DW_AT_call_column:
4743 return "DW_AT_call_column";
4744 case DW_AT_call_file:
4745 return "DW_AT_call_file";
4746 case DW_AT_call_line:
4747 return "DW_AT_call_line";
4748
4b72e226 4749 case DW_AT_MIPS_fde:
4750 return "DW_AT_MIPS_fde";
4751 case DW_AT_MIPS_loop_begin:
4752 return "DW_AT_MIPS_loop_begin";
4753 case DW_AT_MIPS_tail_loop_begin:
4754 return "DW_AT_MIPS_tail_loop_begin";
4755 case DW_AT_MIPS_epilog_begin:
4756 return "DW_AT_MIPS_epilog_begin";
4757 case DW_AT_MIPS_loop_unroll_factor:
4758 return "DW_AT_MIPS_loop_unroll_factor";
4759 case DW_AT_MIPS_software_pipeline_depth:
4760 return "DW_AT_MIPS_software_pipeline_depth";
4761 case DW_AT_MIPS_linkage_name:
4762 return "DW_AT_MIPS_linkage_name";
4763 case DW_AT_MIPS_stride:
4764 return "DW_AT_MIPS_stride";
4765 case DW_AT_MIPS_abstract_name:
4766 return "DW_AT_MIPS_abstract_name";
4767 case DW_AT_MIPS_clone_origin:
4768 return "DW_AT_MIPS_clone_origin";
4769 case DW_AT_MIPS_has_inlines:
4770 return "DW_AT_MIPS_has_inlines";
4771
4772 case DW_AT_sf_names:
4773 return "DW_AT_sf_names";
4774 case DW_AT_src_info:
4775 return "DW_AT_src_info";
4776 case DW_AT_mac_info:
4777 return "DW_AT_mac_info";
4778 case DW_AT_src_coords:
4779 return "DW_AT_src_coords";
4780 case DW_AT_body_begin:
4781 return "DW_AT_body_begin";
4782 case DW_AT_body_end:
4783 return "DW_AT_body_end";
634906d6 4784 case DW_AT_GNU_vector:
4785 return "DW_AT_GNU_vector";
4786
8d60d2bc 4787 case DW_AT_VMS_rtnbeg_pd_address:
4788 return "DW_AT_VMS_rtnbeg_pd_address";
4789
4b72e226 4790 default:
4791 return "DW_AT_<unknown>";
4792 }
4793}
4794
4795/* Convert a DWARF value form code into its string name. */
4796
4797static const char *
8ec3a57b 4798dwarf_form_name (unsigned int form)
4b72e226 4799{
4800 switch (form)
4801 {
4802 case DW_FORM_addr:
4803 return "DW_FORM_addr";
4804 case DW_FORM_block2:
4805 return "DW_FORM_block2";
4806 case DW_FORM_block4:
4807 return "DW_FORM_block4";
4808 case DW_FORM_data2:
4809 return "DW_FORM_data2";
4810 case DW_FORM_data4:
4811 return "DW_FORM_data4";
4812 case DW_FORM_data8:
4813 return "DW_FORM_data8";
4814 case DW_FORM_string:
4815 return "DW_FORM_string";
4816 case DW_FORM_block:
4817 return "DW_FORM_block";
4818 case DW_FORM_block1:
4819 return "DW_FORM_block1";
4820 case DW_FORM_data1:
4821 return "DW_FORM_data1";
4822 case DW_FORM_flag:
4823 return "DW_FORM_flag";
4824 case DW_FORM_sdata:
4825 return "DW_FORM_sdata";
4826 case DW_FORM_strp:
4827 return "DW_FORM_strp";
4828 case DW_FORM_udata:
4829 return "DW_FORM_udata";
4830 case DW_FORM_ref_addr:
4831 return "DW_FORM_ref_addr";
4832 case DW_FORM_ref1:
4833 return "DW_FORM_ref1";
4834 case DW_FORM_ref2:
4835 return "DW_FORM_ref2";
4836 case DW_FORM_ref4:
4837 return "DW_FORM_ref4";
4838 case DW_FORM_ref8:
4839 return "DW_FORM_ref8";
4840 case DW_FORM_ref_udata:
4841 return "DW_FORM_ref_udata";
4842 case DW_FORM_indirect:
4843 return "DW_FORM_indirect";
8a8bfbe7 4844 default:
4b72e226 4845 return "DW_FORM_<unknown>";
30ade641 4846 }
4847}
8a8bfbe7 4848\f
4849/* Determine the "ultimate origin" of a decl. The decl may be an inlined
4850 instance of an inlined instance of a decl which is local to an inline
4851 function, so we have to trace all of the way back through the origin chain
4852 to find out what sort of node actually served as the original seed for the
4853 given block. */
30ade641 4854
8a8bfbe7 4855static tree
8ec3a57b 4856decl_ultimate_origin (tree decl)
30ade641 4857{
5ded8c6f 4858 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4859 return NULL_TREE;
4860
e7b3c55c 4861 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4862 nodes in the function to point to themselves; ignore that if
4863 we're trying to output the abstract instance of this function. */
4864 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4865 return NULL_TREE;
4866
7bd4f6b6 4867 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4868 most distant ancestor, this should never happen. */
4869 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
8a8bfbe7 4870
c0671ae8 4871 return DECL_ABSTRACT_ORIGIN (decl);
30ade641 4872}
4873
8a8bfbe7 4874/* Determine the "ultimate origin" of a block. The block may be an inlined
4875 instance of an inlined instance of a block which is local to an inline
4876 function, so we have to trace all of the way back through the origin chain
4877 to find out what sort of node actually served as the original seed for the
4878 given block. */
ec1e49cc 4879
8a8bfbe7 4880static tree
8ec3a57b 4881block_ultimate_origin (tree block)
30ade641 4882{
19cb6b50 4883 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
ec1e49cc 4884
e7b3c55c 4885 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4886 nodes in the function to point to themselves; ignore that if
4887 we're trying to output the abstract instance of this function. */
4888 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4889 return NULL_TREE;
4890
8a8bfbe7 4891 if (immediate_origin == NULL_TREE)
4892 return NULL_TREE;
4893 else
4894 {
19cb6b50 4895 tree ret_val;
4896 tree lookahead = immediate_origin;
ec1e49cc 4897
8a8bfbe7 4898 do
4899 {
4900 ret_val = lookahead;
8c3f468d 4901 lookahead = (TREE_CODE (ret_val) == BLOCK
4902 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
8a8bfbe7 4903 }
4904 while (lookahead != NULL && lookahead != ret_val);
61a9389f 4905
9e45f419 4906 /* The block's abstract origin chain may not be the *ultimate* origin of
4907 the block. It could lead to a DECL that has an abstract origin set.
4908 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4909 will give us if it has one). Note that DECL's abstract origins are
4910 supposed to be the most distant ancestor (or so decl_ultimate_origin
4911 claims), so we don't need to loop following the DECL origins. */
4912 if (DECL_P (ret_val))
4913 return DECL_ORIGIN (ret_val);
8a8bfbe7 4914
4915 return ret_val;
4916 }
30ade641 4917}
4918
8a8bfbe7 4919/* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4920 of a virtual function may refer to a base class, so we check the 'this'
4921 parameter. */
ec1e49cc 4922
8a8bfbe7 4923static tree
8ec3a57b 4924decl_class_context (tree decl)
30ade641 4925{
8a8bfbe7 4926 tree context = NULL_TREE;
ec1e49cc 4927
8a8bfbe7 4928 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4929 context = DECL_CONTEXT (decl);
4930 else
4931 context = TYPE_MAIN_VARIANT
4932 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
ec1e49cc 4933
9308e976 4934 if (context && !TYPE_P (context))
8a8bfbe7 4935 context = NULL_TREE;
4936
4937 return context;
30ade641 4938}
4939\f
958656b7 4940/* Add an attribute/value pair to a DIE. */
ec1e49cc 4941
4942static inline void
8ec3a57b 4943add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
30ade641 4944{
6f56c055 4945 /* Maybe this should be an assert? */
4946 if (die == NULL)
4947 return;
61a9389f 4948
6f56c055 4949 if (die->die_attr == NULL)
4950 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4951 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
30ade641 4952}
4953
573aba85 4954static inline enum dw_val_class
8ec3a57b 4955AT_class (dw_attr_ref a)
c90bf86c 4956{
4957 return a->dw_attr_val.val_class;
4958}
4959
8a8bfbe7 4960/* Add a flag value attribute to a DIE. */
ec1e49cc 4961
8a8bfbe7 4962static inline void
8ec3a57b 4963add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
30ade641 4964{
6f56c055 4965 dw_attr_node attr;
ec1e49cc 4966
6f56c055 4967 attr.dw_attr = attr_kind;
4968 attr.dw_attr_val.val_class = dw_val_class_flag;
4969 attr.dw_attr_val.v.val_flag = flag;
4970 add_dwarf_attr (die, &attr);
30ade641 4971}
4972
c90bf86c 4973static inline unsigned
8ec3a57b 4974AT_flag (dw_attr_ref a)
c90bf86c 4975{
7bd4f6b6 4976 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4977 return a->dw_attr_val.v.val_flag;
c90bf86c 4978}
4979
8a8bfbe7 4980/* Add a signed integer attribute value to a DIE. */
ec1e49cc 4981
8a8bfbe7 4982static inline void
3d867824 4983add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
30ade641 4984{
6f56c055 4985 dw_attr_node attr;
8a8bfbe7 4986
6f56c055 4987 attr.dw_attr = attr_kind;
4988 attr.dw_attr_val.val_class = dw_val_class_const;
4989 attr.dw_attr_val.v.val_int = int_val;
4990 add_dwarf_attr (die, &attr);
30ade641 4991}
4992
3d867824 4993static inline HOST_WIDE_INT
8ec3a57b 4994AT_int (dw_attr_ref a)
c90bf86c 4995{
7bd4f6b6 4996 gcc_assert (a && AT_class (a) == dw_val_class_const);
4997 return a->dw_attr_val.v.val_int;
c90bf86c 4998}
4999
8a8bfbe7 5000/* Add an unsigned integer attribute value to a DIE. */
ec1e49cc 5001
8a8bfbe7 5002static inline void
8ec3a57b 5003add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3d867824 5004 unsigned HOST_WIDE_INT unsigned_val)
30ade641 5005{
6f56c055 5006 dw_attr_node attr;
8a8bfbe7 5007
6f56c055 5008 attr.dw_attr = attr_kind;
5009 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5010 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5011 add_dwarf_attr (die, &attr);
30ade641 5012}
ec1e49cc 5013
3d867824 5014static inline unsigned HOST_WIDE_INT
8ec3a57b 5015AT_unsigned (dw_attr_ref a)
c90bf86c 5016{
7bd4f6b6 5017 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5018 return a->dw_attr_val.v.val_unsigned;
c90bf86c 5019}
5020
8a8bfbe7 5021/* Add an unsigned double integer attribute value to a DIE. */
5022
5023static inline void
8ec3a57b 5024add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5025 long unsigned int val_hi, long unsigned int val_low)
30ade641 5026{
6f56c055 5027 dw_attr_node attr;
ec1e49cc 5028
6f56c055 5029 attr.dw_attr = attr_kind;
5030 attr.dw_attr_val.val_class = dw_val_class_long_long;
5031 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5032 attr.dw_attr_val.v.val_long_long.low = val_low;
5033 add_dwarf_attr (die, &attr);
8a8bfbe7 5034}
ec1e49cc 5035
8a8bfbe7 5036/* Add a floating point attribute value to a DIE and return it. */
ec1e49cc 5037
8a8bfbe7 5038static inline void
1b6ad376 5039add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5040 unsigned int length, unsigned int elt_size, unsigned char *array)
8a8bfbe7 5041{
6f56c055 5042 dw_attr_node attr;
8a8bfbe7 5043
6f56c055 5044 attr.dw_attr = attr_kind;
5045 attr.dw_attr_val.val_class = dw_val_class_vec;
5046 attr.dw_attr_val.v.val_vec.length = length;
5047 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5048 attr.dw_attr_val.v.val_vec.array = array;
5049 add_dwarf_attr (die, &attr);
30ade641 5050}
5051
573aba85 5052/* Hash and equality functions for debug_str_hash. */
5053
5054static hashval_t
8ec3a57b 5055debug_str_do_hash (const void *x)
573aba85 5056{
5057 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5058}
5059
5060static int
8ec3a57b 5061debug_str_eq (const void *x1, const void *x2)
573aba85 5062{
5063 return strcmp ((((const struct indirect_string_node *)x1)->str),
5064 (const char *)x2) == 0;
5065}
5066
8a8bfbe7 5067/* Add a string attribute value to a DIE. */
ec1e49cc 5068
8a8bfbe7 5069static inline void
8ec3a57b 5070add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
30ade641 5071{
6f56c055 5072 dw_attr_node attr;
80b7bd06 5073 struct indirect_string_node *node;
b9a7cc69 5074 void **slot;
bc70bd5e 5075
80b7bd06 5076 if (! debug_str_hash)
8ec3a57b 5077 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
573aba85 5078 debug_str_eq, NULL);
5079
5080 slot = htab_find_slot_with_hash (debug_str_hash, str,
5081 htab_hash_string (str), INSERT);
5082 if (*slot == NULL)
facb12b2 5083 {
5084 node = (struct indirect_string_node *)
5085 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5086 node->str = ggc_strdup (str);
5087 *slot = node;
5088 }
5089 else
5090 node = (struct indirect_string_node *) *slot;
5091
80b7bd06 5092 node->refcount++;
ec1e49cc 5093
6f56c055 5094 attr.dw_attr = attr_kind;
5095 attr.dw_attr_val.val_class = dw_val_class_str;
5096 attr.dw_attr_val.v.val_str = node;
5097 add_dwarf_attr (die, &attr);
8a8bfbe7 5098}
ec1e49cc 5099
c90bf86c 5100static inline const char *
8ec3a57b 5101AT_string (dw_attr_ref a)
c90bf86c 5102{
7bd4f6b6 5103 gcc_assert (a && AT_class (a) == dw_val_class_str);
5104 return a->dw_attr_val.v.val_str->str;
80b7bd06 5105}
5106
5107/* Find out whether a string should be output inline in DIE
5108 or out-of-line in .debug_str section. */
5109
80b7bd06 5110static int
8ec3a57b 5111AT_string_form (dw_attr_ref a)
80b7bd06 5112{
7bd4f6b6 5113 struct indirect_string_node *node;
5114 unsigned int len;
5115 char label[32];
80b7bd06 5116
7bd4f6b6 5117 gcc_assert (a && AT_class (a) == dw_val_class_str);
8ff30ff6 5118
7bd4f6b6 5119 node = a->dw_attr_val.v.val_str;
5120 if (node->form)
5121 return node->form;
8ff30ff6 5122
7bd4f6b6 5123 len = strlen (node->str) + 1;
80b7bd06 5124
7bd4f6b6 5125 /* If the string is shorter or equal to the size of the reference, it is
5126 always better to put it inline. */
5127 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5128 return node->form = DW_FORM_string;
80b7bd06 5129
7bd4f6b6 5130 /* If we cannot expect the linker to merge strings in .debug_str
5131 section, only put it into .debug_str if it is worth even in this
5132 single module. */
2f14b1f9 5133 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
7bd4f6b6 5134 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5135 return node->form = DW_FORM_string;
8c3f468d 5136
7bd4f6b6 5137 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5138 ++dw2_string_counter;
5139 node->label = xstrdup (label);
c90bf86c 5140
7bd4f6b6 5141 return node->form = DW_FORM_strp;
c90bf86c 5142}
5143
8a8bfbe7 5144/* Add a DIE reference attribute value to a DIE. */
ec1e49cc 5145
8a8bfbe7 5146static inline void
8ec3a57b 5147add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
8a8bfbe7 5148{
6f56c055 5149 dw_attr_node attr;
ec1e49cc 5150
6f56c055 5151 attr.dw_attr = attr_kind;
5152 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5153 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5154 attr.dw_attr_val.v.val_die_ref.external = 0;
5155 add_dwarf_attr (die, &attr);
8a8bfbe7 5156}
34425fdc 5157
023dc493 5158/* Add an AT_specification attribute to a DIE, and also make the back
8b332087 5159 pointer from the specification to the definition. */
023dc493 5160
5161static inline void
5162add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5163{
5164 add_AT_die_ref (die, DW_AT_specification, targ_die);
7bd4f6b6 5165 gcc_assert (!targ_die->die_definition);
023dc493 5166 targ_die->die_definition = die;
5167}
5168
c90bf86c 5169static inline dw_die_ref
8ec3a57b 5170AT_ref (dw_attr_ref a)
c90bf86c 5171{
7bd4f6b6 5172 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5173 return a->dw_attr_val.v.val_die_ref.die;
c90bf86c 5174}
5175
19f716e5 5176static inline int
8ec3a57b 5177AT_ref_external (dw_attr_ref a)
19f716e5 5178{
5179 if (a && AT_class (a) == dw_val_class_die_ref)
5180 return a->dw_attr_val.v.val_die_ref.external;
5181
5182 return 0;
5183}
5184
19f716e5 5185static inline void
8ec3a57b 5186set_AT_ref_external (dw_attr_ref a, int i)
19f716e5 5187{
7bd4f6b6 5188 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5189 a->dw_attr_val.v.val_die_ref.external = i;
19f716e5 5190}
5191
8a8bfbe7 5192/* Add an FDE reference attribute value to a DIE. */
34425fdc 5193
8a8bfbe7 5194static inline void
8ec3a57b 5195add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
8a8bfbe7 5196{
6f56c055 5197 dw_attr_node attr;
34425fdc 5198
6f56c055 5199 attr.dw_attr = attr_kind;
5200 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5201 attr.dw_attr_val.v.val_fde_index = targ_fde;
5202 add_dwarf_attr (die, &attr);
30ade641 5203}
ec1e49cc 5204
8a8bfbe7 5205/* Add a location description attribute value to a DIE. */
ec1e49cc 5206
8a8bfbe7 5207static inline void
8ec3a57b 5208add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
8a8bfbe7 5209{
6f56c055 5210 dw_attr_node attr;
ec1e49cc 5211
6f56c055 5212 attr.dw_attr = attr_kind;
5213 attr.dw_attr_val.val_class = dw_val_class_loc;
5214 attr.dw_attr_val.v.val_loc = loc;
5215 add_dwarf_attr (die, &attr);
30ade641 5216}
5217
c90bf86c 5218static inline dw_loc_descr_ref
8ec3a57b 5219AT_loc (dw_attr_ref a)
c90bf86c 5220{
7bd4f6b6 5221 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5222 return a->dw_attr_val.v.val_loc;
c90bf86c 5223}
5224
4c21a22f 5225static inline void
8ec3a57b 5226add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4c21a22f 5227{
6f56c055 5228 dw_attr_node attr;
4c21a22f 5229
6f56c055 5230 attr.dw_attr = attr_kind;
5231 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5232 attr.dw_attr_val.v.val_loc_list = loc_list;
5233 add_dwarf_attr (die, &attr);
dae1861f 5234 have_location_lists = true;
4c21a22f 5235}
5236
4c21a22f 5237static inline dw_loc_list_ref
8ec3a57b 5238AT_loc_list (dw_attr_ref a)
4c21a22f 5239{
7bd4f6b6 5240 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5241 return a->dw_attr_val.v.val_loc_list;
4c21a22f 5242}
5243
8a8bfbe7 5244/* Add an address constant attribute value to a DIE. */
ec1e49cc 5245
8a8bfbe7 5246static inline void
8ec3a57b 5247add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
30ade641 5248{
6f56c055 5249 dw_attr_node attr;
ec1e49cc 5250
6f56c055 5251 attr.dw_attr = attr_kind;
5252 attr.dw_attr_val.val_class = dw_val_class_addr;
5253 attr.dw_attr_val.v.val_addr = addr;
5254 add_dwarf_attr (die, &attr);
30ade641 5255}
5256
69278c24 5257/* Get the RTX from to an address DIE attribute. */
5258
eacbfaac 5259static inline rtx
8ec3a57b 5260AT_addr (dw_attr_ref a)
c90bf86c 5261{
7bd4f6b6 5262 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5263 return a->dw_attr_val.v.val_addr;
c90bf86c 5264}
5265
69278c24 5266/* Add a file attribute value to a DIE. */
5267
5268static inline void
5269add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5270 struct dwarf_file_data *fd)
5271{
5272 dw_attr_node attr;
5273
5274 attr.dw_attr = attr_kind;
5275 attr.dw_attr_val.val_class = dw_val_class_file;
5276 attr.dw_attr_val.v.val_file = fd;
5277 add_dwarf_attr (die, &attr);
5278}
5279
5280/* Get the dwarf_file_data from a file DIE attribute. */
5281
5282static inline struct dwarf_file_data *
5283AT_file (dw_attr_ref a)
5284{
5285 gcc_assert (a && AT_class (a) == dw_val_class_file);
5286 return a->dw_attr_val.v.val_file;
5287}
5288
8a8bfbe7 5289/* Add a label identifier attribute value to a DIE. */
ec1e49cc 5290
8a8bfbe7 5291static inline void
8ec3a57b 5292add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
30ade641 5293{
6f56c055 5294 dw_attr_node attr;
ec1e49cc 5295
6f56c055 5296 attr.dw_attr = attr_kind;
5297 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5298 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5299 add_dwarf_attr (die, &attr);
8a8bfbe7 5300}
ec1e49cc 5301
d08d29c0 5302/* Add a section offset attribute value to a DIE, an offset into the
5303 debug_line section. */
8a8bfbe7 5304
5305static inline void
d08d29c0 5306add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5307 const char *label)
8a8bfbe7 5308{
6f56c055 5309 dw_attr_node attr;
ec1e49cc 5310
6f56c055 5311 attr.dw_attr = attr_kind;
5312 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5313 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5314 add_dwarf_attr (die, &attr);
d08d29c0 5315}
5316
5317/* Add a section offset attribute value to a DIE, an offset into the
5318 debug_macinfo section. */
5319
5320static inline void
5321add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5322 const char *label)
5323{
6f56c055 5324 dw_attr_node attr;
d08d29c0 5325
6f56c055 5326 attr.dw_attr = attr_kind;
5327 attr.dw_attr_val.val_class = dw_val_class_macptr;
5328 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5329 add_dwarf_attr (die, &attr);
30ade641 5330}
5331
a36145ca 5332/* Add an offset attribute value to a DIE. */
5333
fe39c28c 5334static inline void
3d867824 5335add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5336 unsigned HOST_WIDE_INT offset)
a36145ca 5337{
6f56c055 5338 dw_attr_node attr;
a36145ca 5339
6f56c055 5340 attr.dw_attr = attr_kind;
5341 attr.dw_attr_val.val_class = dw_val_class_offset;
5342 attr.dw_attr_val.v.val_offset = offset;
5343 add_dwarf_attr (die, &attr);
a36145ca 5344}
5345
fe39c28c 5346/* Add an range_list attribute value to a DIE. */
5347
5348static void
8ec3a57b 5349add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5350 long unsigned int offset)
fe39c28c 5351{
6f56c055 5352 dw_attr_node attr;
fe39c28c 5353
6f56c055 5354 attr.dw_attr = attr_kind;
5355 attr.dw_attr_val.val_class = dw_val_class_range_list;
5356 attr.dw_attr_val.v.val_offset = offset;
5357 add_dwarf_attr (die, &attr);
fe39c28c 5358}
5359
c90bf86c 5360static inline const char *
8ec3a57b 5361AT_lbl (dw_attr_ref a)
30ade641 5362{
7bd4f6b6 5363 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
d08d29c0 5364 || AT_class (a) == dw_val_class_lineptr
5365 || AT_class (a) == dw_val_class_macptr));
7bd4f6b6 5366 return a->dw_attr_val.v.val_lbl_id;
30ade641 5367}
5368
8a8bfbe7 5369/* Get the attribute of type attr_kind. */
ec1e49cc 5370
89df180d 5371static dw_attr_ref
8ec3a57b 5372get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7524eb42 5373{
19cb6b50 5374 dw_attr_ref a;
6f56c055 5375 unsigned ix;
19cb6b50 5376 dw_die_ref spec = NULL;
f80d1bcd 5377
6f56c055 5378 if (! die)
5379 return NULL;
ec1e49cc 5380
6f56c055 5381 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5382 if (a->dw_attr == attr_kind)
5383 return a;
5384 else if (a->dw_attr == DW_AT_specification
5385 || a->dw_attr == DW_AT_abstract_origin)
5386 spec = AT_ref (a);
61a9389f 5387
6f56c055 5388 if (spec)
5389 return get_AT (spec, attr_kind);
8a8bfbe7 5390
5391 return NULL;
7524eb42 5392}
5393
8c3f468d 5394/* Return the "low pc" attribute value, typically associated with a subprogram
5395 DIE. Return null if the "low pc" attribute is either not present, or if it
5396 cannot be represented as an assembler label identifier. */
ec1e49cc 5397
c90bf86c 5398static inline const char *
8ec3a57b 5399get_AT_low_pc (dw_die_ref die)
a3899bb7 5400{
19cb6b50 5401 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
8c3f468d 5402
433e0c6c 5403 return a ? AT_lbl (a) : NULL;
a3899bb7 5404}
5405
8c3f468d 5406/* Return the "high pc" attribute value, typically associated with a subprogram
5407 DIE. Return null if the "high pc" attribute is either not present, or if it
5408 cannot be represented as an assembler label identifier. */
ec1e49cc 5409
c90bf86c 5410static inline const char *
8ec3a57b 5411get_AT_hi_pc (dw_die_ref die)
30ade641 5412{
19cb6b50 5413 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
8c3f468d 5414
433e0c6c 5415 return a ? AT_lbl (a) : NULL;
8a8bfbe7 5416}
5417
5418/* Return the value of the string attribute designated by ATTR_KIND, or
5419 NULL if it is not present. */
ec1e49cc 5420
c90bf86c 5421static inline const char *
8ec3a57b 5422get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
8a8bfbe7 5423{
19cb6b50 5424 dw_attr_ref a = get_AT (die, attr_kind);
8c3f468d 5425
433e0c6c 5426 return a ? AT_string (a) : NULL;
30ade641 5427}
5428
8a8bfbe7 5429/* Return the value of the flag attribute designated by ATTR_KIND, or -1
5430 if it is not present. */
ec1e49cc 5431
8a8bfbe7 5432static inline int
8ec3a57b 5433get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
30ade641 5434{
19cb6b50 5435 dw_attr_ref a = get_AT (die, attr_kind);
8c3f468d 5436
433e0c6c 5437 return a ? AT_flag (a) : 0;
30ade641 5438}
5439
8a8bfbe7 5440/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5441 if it is not present. */
ec1e49cc 5442
8a8bfbe7 5443static inline unsigned
8ec3a57b 5444get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
30ade641 5445{
19cb6b50 5446 dw_attr_ref a = get_AT (die, attr_kind);
8c3f468d 5447
433e0c6c 5448 return a ? AT_unsigned (a) : 0;
c90bf86c 5449}
ec1e49cc 5450
c90bf86c 5451static inline dw_die_ref
8ec3a57b 5452get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
c90bf86c 5453{
19cb6b50 5454 dw_attr_ref a = get_AT (die, attr_kind);
8c3f468d 5455
433e0c6c 5456 return a ? AT_ref (a) : NULL;
8a8bfbe7 5457}
ec1e49cc 5458
69278c24 5459static inline struct dwarf_file_data *
5460get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5461{
5462 dw_attr_ref a = get_AT (die, attr_kind);
5463
5464 return a ? AT_file (a) : NULL;
5465}
5466
600dbd47 5467/* Return TRUE if the language is C or C++. */
5468
5469static inline bool
8ec3a57b 5470is_c_family (void)
8a8bfbe7 5471{
600dbd47 5472 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
ec1e49cc 5473
bda642f9 5474 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5475 || lang == DW_LANG_C99
5476 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
f80d1bcd 5477}
ec1e49cc 5478
600dbd47 5479/* Return TRUE if the language is C++. */
5480
5481static inline bool
8ec3a57b 5482is_cxx (void)
bde7be7a 5483{
bda642f9 5484 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
61a9389f 5485
bda642f9 5486 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
bc70bd5e 5487}
bde7be7a 5488
600dbd47 5489/* Return TRUE if the language is Fortran. */
5490
5491static inline bool
8ec3a57b 5492is_fortran (void)
8a8bfbe7 5493{
600dbd47 5494 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
ec1e49cc 5495
4ee9c684 5496 return (lang == DW_LANG_Fortran77
5497 || lang == DW_LANG_Fortran90
5498 || lang == DW_LANG_Fortran95);
f80d1bcd 5499}
ec1e49cc 5500
600dbd47 5501/* Return TRUE if the language is Java. */
5502
5503static inline bool
8ec3a57b 5504is_java (void)
af4d39d8 5505{
600dbd47 5506 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
af4d39d8 5507
600dbd47 5508 return lang == DW_LANG_Java;
5509}
5510
5511/* Return TRUE if the language is Ada. */
5512
5513static inline bool
8ec3a57b 5514is_ada (void)
600dbd47 5515{
5516 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
8ec3a57b 5517
600dbd47 5518 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
af4d39d8 5519}
5520
e7b3c55c 5521/* Remove the specified attribute if present. */
5522
5523static void
8ec3a57b 5524remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8a8bfbe7 5525{
6f56c055 5526 dw_attr_ref a;
5527 unsigned ix;
30ade641 5528
6f56c055 5529 if (! die)
5530 return;
ec1e49cc 5531
6f56c055 5532 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5533 if (a->dw_attr == attr_kind)
5534 {
b0aa6b33 5535 if (AT_class (a) == dw_val_class_str)
5536 if (a->dw_attr_val.v.val_str->refcount)
5537 a->dw_attr_val.v.val_str->refcount--;
5538
6f56c055 5539 /* VEC_ordered_remove should help reduce the number of abbrevs
5540 that are needed. */
5541 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5542 return;
5543 }
e7b3c55c 5544}
ec1e49cc 5545
958656b7 5546/* Remove CHILD from its parent. PREV must have the property that
5547 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
2b49746a 5548
5549static void
958656b7 5550remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
2b49746a 5551{
958656b7 5552 gcc_assert (child->die_parent == prev->die_parent);
5553 gcc_assert (prev->die_sib == child);
5554 if (prev == child)
2b49746a 5555 {
958656b7 5556 gcc_assert (child->die_parent->die_child == child);
5557 prev = NULL;
2b49746a 5558 }
958656b7 5559 else
5560 prev->die_sib = child->die_sib;
5561 if (child->die_parent->die_child == child)
5562 child->die_parent->die_child = prev;
2b49746a 5563}
5564
958656b7 5565/* Remove child DIE whose die_tag is TAG. Do nothing if no child
5566 matches TAG. */
ec1e49cc 5567
958656b7 5568static void
5569remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5570{
5571 dw_die_ref c;
61a9389f 5572
958656b7 5573 c = die->die_child;
5574 if (c) do {
5575 dw_die_ref prev = c;
5576 c = c->die_sib;
5577 while (c->die_tag == tag)
5578 {
5579 remove_child_with_prev (c, prev);
5580 /* Might have removed every child. */
5581 if (c == c->die_sib)
5582 return;
5583 c = c->die_sib;
5584 }
5585 } while (c != die->die_child);
5586}
5587
5588/* Add a CHILD_DIE as the last child of DIE. */
5589
5590static void
8ec3a57b 5591add_child_die (dw_die_ref die, dw_die_ref child_die)
8a8bfbe7 5592{
958656b7 5593 /* FIXME this should probably be an assert. */
5594 if (! die || ! child_die)
5595 return;
5596 gcc_assert (die != child_die);
8c3f468d 5597
958656b7 5598 child_die->die_parent = die;
5599 if (die->die_child)
5600 {
5601 child_die->die_sib = die->die_child->die_sib;
5602 die->die_child->die_sib = child_die;
8a8bfbe7 5603 }
958656b7 5604 else
5605 child_die->die_sib = child_die;
5606 die->die_child = child_die;
8a8bfbe7 5607}
5608
5134c73b 5609/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
61a9389f 5610 is the specification, to the end of PARENT's list of children.
958656b7 5611 This is done by removing and re-adding it. */
e7b3c55c 5612
5613static void
8ec3a57b 5614splice_child_die (dw_die_ref parent, dw_die_ref child)
e7b3c55c 5615{
958656b7 5616 dw_die_ref p;
e7b3c55c 5617
5618 /* We want the declaration DIE from inside the class, not the
5619 specification DIE at toplevel. */
5620 if (child->die_parent != parent)
5134c73b 5621 {
5622 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8c3f468d 5623
5134c73b 5624 if (tmp)
5625 child = tmp;
5626 }
e7b3c55c 5627
7bd4f6b6 5628 gcc_assert (child->die_parent == parent
5629 || (child->die_parent
5630 == get_AT_ref (parent, DW_AT_specification)));
61a9389f 5631
958656b7 5632 for (p = child->die_parent->die_child; ; p = p->die_sib)
5633 if (p->die_sib == child)
e7b3c55c 5634 {
958656b7 5635 remove_child_with_prev (child, p);
e7b3c55c 5636 break;
5637 }
5638
958656b7 5639 add_child_die (parent, child);
e7b3c55c 5640}
5641
8a8bfbe7 5642/* Return a pointer to a newly created DIE node. */
5643
5644static inline dw_die_ref
8ec3a57b 5645new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8a8bfbe7 5646{
f0af5a88 5647 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
8a8bfbe7 5648
5649 die->die_tag = tag_value;
8a8bfbe7 5650
5651 if (parent_die != NULL)
5652 add_child_die (parent_die, die);
5653 else
678d90bb 5654 {
5655 limbo_die_node *limbo_node;
5656
573aba85 5657 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
678d90bb 5658 limbo_node->die = die;
15cfae4e 5659 limbo_node->created_for = t;
678d90bb 5660 limbo_node->next = limbo_die_list;
5661 limbo_die_list = limbo_node;
5662 }
ec1e49cc 5663
8a8bfbe7 5664 return die;
5665}
ec1e49cc 5666
8a8bfbe7 5667/* Return the DIE associated with the given type specifier. */
ec1e49cc 5668
8a8bfbe7 5669static inline dw_die_ref
8ec3a57b 5670lookup_type_die (tree type)
8a8bfbe7 5671{
1f3233d1 5672 return TYPE_SYMTAB_DIE (type);
8a8bfbe7 5673}
c05d7491 5674
8a8bfbe7 5675/* Equate a DIE to a given type specifier. */
ec1e49cc 5676
e7b3c55c 5677static inline void
8ec3a57b 5678equate_type_number_to_die (tree type, dw_die_ref type_die)
8a8bfbe7 5679{
1f3233d1 5680 TYPE_SYMTAB_DIE (type) = type_die;
8a8bfbe7 5681}
ec1e49cc 5682
26863140 5683/* Returns a hash value for X (which really is a die_struct). */
5684
5685static hashval_t
5686decl_die_table_hash (const void *x)
5687{
c1fdef8e 5688 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
26863140 5689}
5690
5691/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5692
5693static int
5694decl_die_table_eq (const void *x, const void *y)
5695{
aae87fc3 5696 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
26863140 5697}
5698
8a8bfbe7 5699/* Return the DIE associated with a given declaration. */
ec1e49cc 5700
8a8bfbe7 5701static inline dw_die_ref
8ec3a57b 5702lookup_decl_die (tree decl)
8a8bfbe7 5703{
26863140 5704 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
30ade641 5705}
5706
b2025850 5707/* Returns a hash value for X (which really is a var_loc_list). */
5708
5709static hashval_t
5710decl_loc_table_hash (const void *x)
5711{
5712 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5713}
5714
5715/* Return nonzero if decl_id of var_loc_list X is the same as
5716 UID of decl *Y. */
5717
5718static int
5719decl_loc_table_eq (const void *x, const void *y)
5720{
aae87fc3 5721 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
b2025850 5722}
5723
5724/* Return the var_loc list associated with a given declaration. */
5725
5726static inline var_loc_list *
5727lookup_decl_loc (tree decl)
5728{
5729 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5730}
5731
8a8bfbe7 5732/* Equate a DIE to a particular declaration. */
ec1e49cc 5733
8a8bfbe7 5734static void
8ec3a57b 5735equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
30ade641 5736{
dff29840 5737 unsigned int decl_id = DECL_UID (decl);
26863140 5738 void **slot;
8a8bfbe7 5739
26863140 5740 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5741 *slot = decl_die;
5742 decl_die->decl_id = decl_id;
30ade641 5743}
b2025850 5744
5745/* Add a variable location node to the linked list for DECL. */
5746
5747static void
5748add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5749{
5750 unsigned int decl_id = DECL_UID (decl);
5751 var_loc_list *temp;
5752 void **slot;
5753
5754 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5755 if (*slot == NULL)
5756 {
5757 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5758 temp->decl_id = decl_id;
5759 *slot = temp;
5760 }
5761 else
5762 temp = *slot;
5763
5764 if (temp->last)
5765 {
5766 /* If the current location is the same as the end of the list,
d53bb226 5767 and either both or neither of the locations is uninitialized,
b2025850 5768 we have nothing to do. */
d53bb226 5769 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5770 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5771 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5772 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5773 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5774 == VAR_INIT_STATUS_UNINITIALIZED)
5775 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5776 == VAR_INIT_STATUS_UNINITIALIZED))))
b2025850 5777 {
5778 /* Add LOC to the end of list and update LAST. */
5779 temp->last->next = loc;
5780 temp->last = loc;
5781 }
5782 }
5783 /* Do not add empty location to the beginning of the list. */
5784 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5785 {
5786 temp->first = loc;
5787 temp->last = loc;
5788 }
5789}
8a8bfbe7 5790\f
5791/* Keep track of the number of spaces used to indent the
5792 output of the debugging routines that print the structure of
5793 the DIE internal representation. */
5794static int print_indent;
ec1e49cc 5795
8a8bfbe7 5796/* Indent the line the number of spaces given by print_indent. */
5797
5798static inline void
8ec3a57b 5799print_spaces (FILE *outfile)
8a8bfbe7 5800{
5801 fprintf (outfile, "%*s", print_indent, "");
30ade641 5802}
5803
ad87de1e 5804/* Print the information associated with a given DIE, and its children.
8a8bfbe7 5805 This routine is a debugging aid only. */
ec1e49cc 5806
30ade641 5807static void
8ec3a57b 5808print_die (dw_die_ref die, FILE *outfile)
30ade641 5809{
19cb6b50 5810 dw_attr_ref a;
5811 dw_die_ref c;
6f56c055 5812 unsigned ix;
ec1e49cc 5813
8a8bfbe7 5814 print_spaces (outfile);
de064be9 5815 fprintf (outfile, "DIE %4ld: %s\n",
8a8bfbe7 5816 die->die_offset, dwarf_tag_name (die->die_tag));
5817 print_spaces (outfile);
c08e043f 5818 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
de064be9 5819 fprintf (outfile, " offset: %ld\n", die->die_offset);
8a8bfbe7 5820
6f56c055 5821 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
30ade641 5822 {
8a8bfbe7 5823 print_spaces (outfile);
5824 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5825
c90bf86c 5826 switch (AT_class (a))
8a8bfbe7 5827 {
5828 case dw_val_class_addr:
5829 fprintf (outfile, "address");
5830 break;
a36145ca 5831 case dw_val_class_offset:
5832 fprintf (outfile, "offset");
5833 break;
8a8bfbe7 5834 case dw_val_class_loc:
5835 fprintf (outfile, "location descriptor");
5836 break;
4c21a22f 5837 case dw_val_class_loc_list:
a36145ca 5838 fprintf (outfile, "location list -> label:%s",
5839 AT_loc_list (a)->ll_symbol);
4c21a22f 5840 break;
fe39c28c 5841 case dw_val_class_range_list:
5842 fprintf (outfile, "range list");
5843 break;
8a8bfbe7 5844 case dw_val_class_const:
3201d6f1 5845 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8a8bfbe7 5846 break;
5847 case dw_val_class_unsigned_const:
3201d6f1 5848 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8a8bfbe7 5849 break;
5850 case dw_val_class_long_long:
c08e043f 5851 fprintf (outfile, "constant (%lu,%lu)",
f80d1bcd 5852 a->dw_attr_val.v.val_long_long.hi,
5853 a->dw_attr_val.v.val_long_long.low);
8a8bfbe7 5854 break;
1b6ad376 5855 case dw_val_class_vec:
5856 fprintf (outfile, "floating-point or vector constant");
8a8bfbe7 5857 break;
5858 case dw_val_class_flag:
c90bf86c 5859 fprintf (outfile, "%u", AT_flag (a));
8a8bfbe7 5860 break;
5861 case dw_val_class_die_ref:
c90bf86c 5862 if (AT_ref (a) != NULL)
19f716e5 5863 {
eabb26f3 5864 if (AT_ref (a)->die_symbol)
19f716e5 5865 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5866 else
de064be9 5867 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
19f716e5 5868 }
8a8bfbe7 5869 else
5870 fprintf (outfile, "die -> <null>");
5871 break;
5872 case dw_val_class_lbl_id:
d08d29c0 5873 case dw_val_class_lineptr:
5874 case dw_val_class_macptr:
c90bf86c 5875 fprintf (outfile, "label: %s", AT_lbl (a));
8a8bfbe7 5876 break;
8a8bfbe7 5877 case dw_val_class_str:
c90bf86c 5878 if (AT_string (a) != NULL)
5879 fprintf (outfile, "\"%s\"", AT_string (a));
8a8bfbe7 5880 else
5881 fprintf (outfile, "<null>");
5882 break;
69278c24 5883 case dw_val_class_file:
5884 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5885 AT_file (a)->emitted_number);
5886 break;
0dbd1c74 5887 default:
5888 break;
8a8bfbe7 5889 }
5890
5891 fprintf (outfile, "\n");
5892 }
5893
5894 if (die->die_child != NULL)
5895 {
5896 print_indent += 4;
958656b7 5897 FOR_EACH_CHILD (die, c, print_die (c, outfile));
8a8bfbe7 5898 print_indent -= 4;
30ade641 5899 }
19f716e5 5900 if (print_indent == 0)
5901 fprintf (outfile, "\n");
30ade641 5902}
5903
8a8bfbe7 5904/* Print the contents of the source code line number correspondence table.
5905 This routine is a debugging aid only. */
ec1e49cc 5906
8a8bfbe7 5907static void
8ec3a57b 5908print_dwarf_line_table (FILE *outfile)
30ade641 5909{
19cb6b50 5910 unsigned i;
5911 dw_line_info_ref line_info;
8a8bfbe7 5912
5913 fprintf (outfile, "\n\nDWARF source line information\n");
8c3f468d 5914 for (i = 1; i < line_info_table_in_use; i++)
30ade641 5915 {
8a8bfbe7 5916 line_info = &line_info_table[i];
69278c24 5917 fprintf (outfile, "%5d: %4ld %6ld\n", i,
5918 line_info->dw_file_num,
5919 line_info->dw_line_num);
30ade641 5920 }
8a8bfbe7 5921
5922 fprintf (outfile, "\n\n");
7524eb42 5923}
5924
8a8bfbe7 5925/* Print the information collected for a given DIE. */
5926
5927void
8ec3a57b 5928debug_dwarf_die (dw_die_ref die)
8a8bfbe7 5929{
5930 print_die (die, stderr);
5931}
5932
5933/* Print all DWARF information collected for the compilation unit.
5934 This routine is a debugging aid only. */
5935
5936void
8ec3a57b 5937debug_dwarf (void)
8a8bfbe7 5938{
5939 print_indent = 0;
5940 print_die (comp_unit_die, stderr);
985956c1 5941 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5942 print_dwarf_line_table (stderr);
8a8bfbe7 5943}
5944\f
8c3f468d 5945/* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5946 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5947 DIE that marks the start of the DIEs for this include file. */
19f716e5 5948
5949static dw_die_ref
8ec3a57b 5950push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
19f716e5 5951{
5952 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5953 dw_die_ref new_unit = gen_compile_unit_die (filename);
8c3f468d 5954
19f716e5 5955 new_unit->die_sib = old_unit;
5956 return new_unit;
5957}
5958
5959/* Close an include-file CU and reopen the enclosing one. */
5960
5961static dw_die_ref
8ec3a57b 5962pop_compile_unit (dw_die_ref old_unit)
19f716e5 5963{
5964 dw_die_ref new_unit = old_unit->die_sib;
8c3f468d 5965
19f716e5 5966 old_unit->die_sib = NULL;
5967 return new_unit;
5968}
5969
8c3f468d 5970#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5971#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
19f716e5 5972
5973/* Calculate the checksum of a location expression. */
5974
5975static inline void
8ec3a57b 5976loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
19f716e5 5977{
8c3f468d 5978 CHECKSUM (loc->dw_loc_opc);
5979 CHECKSUM (loc->dw_loc_oprnd1);
5980 CHECKSUM (loc->dw_loc_oprnd2);
19f716e5 5981}
5982
5983/* Calculate the checksum of an attribute. */
5984
5985static void
8ec3a57b 5986attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
19f716e5 5987{
5988 dw_loc_descr_ref loc;
5989 rtx r;
5990
8c3f468d 5991 CHECKSUM (at->dw_attr);
19f716e5 5992
69278c24 5993 /* We don't care that this was compiled with a different compiler
5994 snapshot; if the output is the same, that's what matters. */
5995 if (at->dw_attr == DW_AT_producer)
19f716e5 5996 return;
5997
5998 switch (AT_class (at))
5999 {
6000 case dw_val_class_const:
8c3f468d 6001 CHECKSUM (at->dw_attr_val.v.val_int);
19f716e5 6002 break;
6003 case dw_val_class_unsigned_const:
8c3f468d 6004 CHECKSUM (at->dw_attr_val.v.val_unsigned);
19f716e5 6005 break;
6006 case dw_val_class_long_long:
8c3f468d 6007 CHECKSUM (at->dw_attr_val.v.val_long_long);
19f716e5 6008 break;
1b6ad376 6009 case dw_val_class_vec:
6010 CHECKSUM (at->dw_attr_val.v.val_vec);
19f716e5 6011 break;
6012 case dw_val_class_flag:
8c3f468d 6013 CHECKSUM (at->dw_attr_val.v.val_flag);
19f716e5 6014 break;
19f716e5 6015 case dw_val_class_str:
8c3f468d 6016 CHECKSUM_STRING (AT_string (at));
19f716e5 6017 break;
a36145ca 6018
19f716e5 6019 case dw_val_class_addr:
6020 r = AT_addr (at);
7bd4f6b6 6021 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6022 CHECKSUM_STRING (XSTR (r, 0));
19f716e5 6023 break;
6024
a36145ca 6025 case dw_val_class_offset:
8c3f468d 6026 CHECKSUM (at->dw_attr_val.v.val_offset);
a36145ca 6027 break;
6028
19f716e5 6029 case dw_val_class_loc:
6030 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6031 loc_checksum (loc, ctx);
6032 break;
6033
6034 case dw_val_class_die_ref:
51e8c210 6035 die_checksum (AT_ref (at), ctx, mark);
6036 break;
19f716e5 6037
6038 case dw_val_class_fde_ref:
6039 case dw_val_class_lbl_id:
d08d29c0 6040 case dw_val_class_lineptr:
6041 case dw_val_class_macptr:
a36145ca 6042 break;
19f716e5 6043
69278c24 6044 case dw_val_class_file:
6045 CHECKSUM_STRING (AT_file (at)->filename);
6046 break;
6047
19f716e5 6048 default:
6049 break;
6050 }
6051}
6052
6053/* Calculate the checksum of a DIE. */
6054
6055static void
8ec3a57b 6056die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
19f716e5 6057{
6058 dw_die_ref c;
6059 dw_attr_ref a;
6f56c055 6060 unsigned ix;
19f716e5 6061
51e8c210 6062 /* To avoid infinite recursion. */
6063 if (die->die_mark)
6064 {
6065 CHECKSUM (die->die_mark);
6066 return;
6067 }
6068 die->die_mark = ++(*mark);
6069
8c3f468d 6070 CHECKSUM (die->die_tag);
19f716e5 6071
6f56c055 6072 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
51e8c210 6073 attr_checksum (a, ctx, mark);
19f716e5 6074
958656b7 6075 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
19f716e5 6076}
6077
8c3f468d 6078#undef CHECKSUM
6079#undef CHECKSUM_STRING
19f716e5 6080
51e8c210 6081/* Do the location expressions look same? */
6082static inline int
8ec3a57b 6083same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
51e8c210 6084{
6085 return loc1->dw_loc_opc == loc2->dw_loc_opc
6086 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6087 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6088}
6089
6090/* Do the values look the same? */
6091static int
8ec3a57b 6092same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
51e8c210 6093{
6094 dw_loc_descr_ref loc1, loc2;
6095 rtx r1, r2;
51e8c210 6096
6097 if (v1->val_class != v2->val_class)
6098 return 0;
6099
6100 switch (v1->val_class)
6101 {
6102 case dw_val_class_const:
6103 return v1->v.val_int == v2->v.val_int;
6104 case dw_val_class_unsigned_const:
6105 return v1->v.val_unsigned == v2->v.val_unsigned;
6106 case dw_val_class_long_long:
6107 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
c83a163c 6108 && v1->v.val_long_long.low == v2->v.val_long_long.low;
1b6ad376 6109 case dw_val_class_vec:
6110 if (v1->v.val_vec.length != v2->v.val_vec.length
6111 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6112 return 0;
6113 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6114 v1->v.val_vec.length * v1->v.val_vec.elt_size))
51e8c210 6115 return 0;
51e8c210 6116 return 1;
6117 case dw_val_class_flag:
6118 return v1->v.val_flag == v2->v.val_flag;
6119 case dw_val_class_str:
573aba85 6120 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
51e8c210 6121
6122 case dw_val_class_addr:
6123 r1 = v1->v.val_addr;
6124 r2 = v2->v.val_addr;
6125 if (GET_CODE (r1) != GET_CODE (r2))
6126 return 0;
7bd4f6b6 6127 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6128 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
51e8c210 6129
6130 case dw_val_class_offset:
6131 return v1->v.val_offset == v2->v.val_offset;
6132
6133 case dw_val_class_loc:
6134 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6135 loc1 && loc2;
6136 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6137 if (!same_loc_p (loc1, loc2, mark))
6138 return 0;
6139 return !loc1 && !loc2;
6140
6141 case dw_val_class_die_ref:
6142 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6143
6144 case dw_val_class_fde_ref:
6145 case dw_val_class_lbl_id:
d08d29c0 6146 case dw_val_class_lineptr:
6147 case dw_val_class_macptr:
51e8c210 6148 return 1;
6149
69278c24 6150 case dw_val_class_file:
6151 return v1->v.val_file == v2->v.val_file;
6152
51e8c210 6153 default:
6154 return 1;
6155 }
6156}
6157
6158/* Do the attributes look the same? */
6159
6160static int
8ec3a57b 6161same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
51e8c210 6162{
6163 if (at1->dw_attr != at2->dw_attr)
6164 return 0;
6165
69278c24 6166 /* We don't care that this was compiled with a different compiler
6167 snapshot; if the output is the same, that's what matters. */
6168 if (at1->dw_attr == DW_AT_producer)
51e8c210 6169 return 1;
6170
6171 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6172}
6173
6174/* Do the dies look the same? */
6175
6176static int
8ec3a57b 6177same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
51e8c210 6178{
6179 dw_die_ref c1, c2;
6f56c055 6180 dw_attr_ref a1;
6181 unsigned ix;
51e8c210 6182
6183 /* To avoid infinite recursion. */
6184 if (die1->die_mark)
6185 return die1->die_mark == die2->die_mark;
6186 die1->die_mark = die2->die_mark = ++(*mark);
6187
6188 if (die1->die_tag != die2->die_tag)
6189 return 0;
6190
6f56c055 6191 if (VEC_length (dw_attr_node, die1->die_attr)
6192 != VEC_length (dw_attr_node, die2->die_attr))
51e8c210 6193 return 0;
61a9389f 6194
6f56c055 6195 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6196 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6197 return 0;
51e8c210 6198
958656b7 6199 c1 = die1->die_child;
6200 c2 = die2->die_child;
6201 if (! c1)
6202 {
6203 if (c2)
6204 return 0;
6205 }
6206 else
6207 for (;;)
6208 {
6209 if (!same_die_p (c1, c2, mark))
6210 return 0;
6211 c1 = c1->die_sib;
6212 c2 = c2->die_sib;
6213 if (c1 == die1->die_child)
6214 {
6215 if (c2 == die2->die_child)
6216 break;
6217 else
6218 return 0;
6219 }
6220 }
51e8c210 6221
6222 return 1;
6223}
6224
6225/* Do the dies look the same? Wrapper around same_die_p. */
6226
6227static int
8ec3a57b 6228same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
51e8c210 6229{
6230 int mark = 0;
6231 int ret = same_die_p (die1, die2, &mark);
6232
6233 unmark_all_dies (die1);
6234 unmark_all_dies (die2);
6235
6236 return ret;
6237}
6238
19f716e5 6239/* The prefix to attach to symbols on DIEs in the current comdat debug
6240 info section. */
6241static char *comdat_symbol_id;
6242
6243/* The index of the current symbol within the current comdat CU. */
6244static unsigned int comdat_symbol_number;
6245
6246/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6247 children, and set comdat_symbol_id accordingly. */
6248
6249static void
8ec3a57b 6250compute_section_prefix (dw_die_ref unit_die)
19f716e5 6251{
51e8c210 6252 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6253 const char *base = die_name ? lbasename (die_name) : "anonymous";
f0af5a88 6254 char *name = alloca (strlen (base) + 64);
90f973ed 6255 char *p;
51e8c210 6256 int i, mark;
19f716e5 6257 unsigned char checksum[16];
6258 struct md5_ctx ctx;
6259
90f973ed 6260 /* Compute the checksum of the DIE, then append part of it as hex digits to
6261 the name filename of the unit. */
6262
19f716e5 6263 md5_init_ctx (&ctx);
51e8c210 6264 mark = 0;
6265 die_checksum (unit_die, &ctx, &mark);
6266 unmark_all_dies (unit_die);
19f716e5 6267 md5_finish_ctx (&ctx, checksum);
6268
93d164ee 6269 sprintf (name, "%s.", base);
19f716e5 6270 clean_symbol_name (name);
6271
8c3f468d 6272 p = name + strlen (name);
6273 for (i = 0; i < 4; i++)
6274 {
6275 sprintf (p, "%.2x", checksum[i]);
6276 p += 2;
6277 }
19f716e5 6278
6279 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6280 comdat_symbol_number = 0;
6281}
6282
90f973ed 6283/* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
19f716e5 6284
6285static int
8ec3a57b 6286is_type_die (dw_die_ref die)
19f716e5 6287{
6288 switch (die->die_tag)
6289 {
6290 case DW_TAG_array_type:
6291 case DW_TAG_class_type:
6292 case DW_TAG_enumeration_type:
6293 case DW_TAG_pointer_type:
6294 case DW_TAG_reference_type:
6295 case DW_TAG_string_type:
6296 case DW_TAG_structure_type:
6297 case DW_TAG_subroutine_type:
6298 case DW_TAG_union_type:
6299 case DW_TAG_ptr_to_member_type:
6300 case DW_TAG_set_type:
6301 case DW_TAG_subrange_type:
6302 case DW_TAG_base_type:
6303 case DW_TAG_const_type:
6304 case DW_TAG_file_type:
6305 case DW_TAG_packed_type:
6306 case DW_TAG_volatile_type:
51e8c210 6307 case DW_TAG_typedef:
19f716e5 6308 return 1;
6309 default:
6310 return 0;
6311 }
6312}
6313
6314/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6315 Basically, we want to choose the bits that are likely to be shared between
6316 compilations (types) and leave out the bits that are specific to individual
6317 compilations (functions). */
6318
6319static int
8ec3a57b 6320is_comdat_die (dw_die_ref c)
19f716e5 6321{
8c3f468d 6322 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6323 we do for stabs. The advantage is a greater likelihood of sharing between
6324 objects that don't include headers in the same order (and therefore would
6325 put the base types in a different comdat). jason 8/28/00 */
6326
19f716e5 6327 if (c->die_tag == DW_TAG_base_type)
6328 return 0;
6329
6330 if (c->die_tag == DW_TAG_pointer_type
6331 || c->die_tag == DW_TAG_reference_type
6332 || c->die_tag == DW_TAG_const_type
6333 || c->die_tag == DW_TAG_volatile_type)
6334 {
6335 dw_die_ref t = get_AT_ref (c, DW_AT_type);
8c3f468d 6336
19f716e5 6337 return t ? is_comdat_die (t) : 0;
6338 }
19f716e5 6339
6340 return is_type_die (c);
6341}
6342
6343/* Returns 1 iff C is the sort of DIE that might be referred to from another
6344 compilation unit. */
6345
6346static int
8ec3a57b 6347is_symbol_die (dw_die_ref c)
19f716e5 6348{
8c3f468d 6349 return (is_type_die (c)
bc70bd5e 6350 || (get_AT (c, DW_AT_declaration)
8462b107 6351 && !get_AT (c, DW_AT_specification))
6352 || c->die_tag == DW_TAG_namespace);
19f716e5 6353}
6354
6355static char *
8ec3a57b 6356gen_internal_sym (const char *prefix)
19f716e5 6357{
6358 char buf[256];
8c3f468d 6359
4c21a22f 6360 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
19f716e5 6361 return xstrdup (buf);
6362}
6363
6364/* Assign symbols to all worthy DIEs under DIE. */
6365
6366static void
8ec3a57b 6367assign_symbol_names (dw_die_ref die)
19f716e5 6368{
19cb6b50 6369 dw_die_ref c;
19f716e5 6370
6371 if (is_symbol_die (die))
6372 {
6373 if (comdat_symbol_id)
6374 {
6375 char *p = alloca (strlen (comdat_symbol_id) + 64);
8c3f468d 6376
19f716e5 6377 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6378 comdat_symbol_id, comdat_symbol_number++);
6379 die->die_symbol = xstrdup (p);
6380 }
6381 else
4c21a22f 6382 die->die_symbol = gen_internal_sym ("LDIE");
19f716e5 6383 }
6384
958656b7 6385 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
19f716e5 6386}
6387
51e8c210 6388struct cu_hash_table_entry
6389{
6390 dw_die_ref cu;
6391 unsigned min_comdat_num, max_comdat_num;
6392 struct cu_hash_table_entry *next;
6393};
6394
6395/* Routines to manipulate hash table of CUs. */
6396static hashval_t
8ec3a57b 6397htab_cu_hash (const void *of)
51e8c210 6398{
6399 const struct cu_hash_table_entry *entry = of;
6400
6401 return htab_hash_string (entry->cu->die_symbol);
6402}
6403
6404static int
8ec3a57b 6405htab_cu_eq (const void *of1, const void *of2)
51e8c210 6406{
6407 const struct cu_hash_table_entry *entry1 = of1;
6408 const struct die_struct *entry2 = of2;
6409
6410 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6411}
6412
6413static void
8ec3a57b 6414htab_cu_del (void *what)
51e8c210 6415{
6416 struct cu_hash_table_entry *next, *entry = what;
6417
6418 while (entry)
6419 {
6420 next = entry->next;
6421 free (entry);
6422 entry = next;
6423 }
6424}
6425
6426/* Check whether we have already seen this CU and set up SYM_NUM
6427 accordingly. */
6428static int
8ec3a57b 6429check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
51e8c210 6430{
6431 struct cu_hash_table_entry dummy;
6432 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6433
6434 dummy.max_comdat_num = 0;
6435
6436 slot = (struct cu_hash_table_entry **)
6437 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6438 INSERT);
6439 entry = *slot;
6440
6441 for (; entry; last = entry, entry = entry->next)
6442 {
6443 if (same_die_p_wrap (cu, entry->cu))
6444 break;
6445 }
6446
6447 if (entry)
6448 {
6449 *sym_num = entry->min_comdat_num;
6450 return 1;
6451 }
6452
4c36ffe6 6453 entry = XCNEW (struct cu_hash_table_entry);
51e8c210 6454 entry->cu = cu;
6455 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6456 entry->next = *slot;
6457 *slot = entry;
6458
6459 return 0;
6460}
6461
6462/* Record SYM_NUM to record of CU in HTABLE. */
6463static void
8ec3a57b 6464record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
51e8c210 6465{
6466 struct cu_hash_table_entry **slot, *entry;
6467
6468 slot = (struct cu_hash_table_entry **)
6469 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6470 NO_INSERT);
6471 entry = *slot;
6472
6473 entry->max_comdat_num = sym_num;
6474}
6475
19f716e5 6476/* Traverse the DIE (which is always comp_unit_die), and set up
6477 additional compilation units for each of the include files we see
6478 bracketed by BINCL/EINCL. */
6479
6480static void
8ec3a57b 6481break_out_includes (dw_die_ref die)
19f716e5 6482{
958656b7 6483 dw_die_ref c;
19cb6b50 6484 dw_die_ref unit = NULL;
51e8c210 6485 limbo_die_node *node, **pnode;
6486 htab_t cu_hash_table;
19f716e5 6487
958656b7 6488 c = die->die_child;
6489 if (c) do {
6490 dw_die_ref prev = c;
6491 c = c->die_sib;
6492 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6493 || (unit && is_comdat_die (c)))
6494 {
6495 dw_die_ref next = c->die_sib;
6496
6497 /* This DIE is for a secondary CU; remove it from the main one. */
6498 remove_child_with_prev (c, prev);
61a9389f 6499
958656b7 6500 if (c->die_tag == DW_TAG_GNU_BINCL)
6501 unit = push_new_compile_unit (unit, c);
6502 else if (c->die_tag == DW_TAG_GNU_EINCL)
6503 unit = pop_compile_unit (unit);
6504 else
6505 add_child_die (unit, c);
6506 c = next;
6507 if (c == die->die_child)
6508 break;
6509 }
6510 } while (c != die->die_child);
19f716e5 6511
6512#if 0
6513 /* We can only use this in debugging, since the frontend doesn't check
ac02093f 6514 to make sure that we leave every include file we enter. */
7bd4f6b6 6515 gcc_assert (!unit);
19f716e5 6516#endif
6517
6518 assign_symbol_names (die);
51e8c210 6519 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6520 for (node = limbo_die_list, pnode = &limbo_die_list;
6521 node;
6522 node = node->next)
19f716e5 6523 {
51e8c210 6524 int is_dupl;
6525
19f716e5 6526 compute_section_prefix (node->die);
51e8c210 6527 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6528 &comdat_symbol_number);
19f716e5 6529 assign_symbol_names (node->die);
51e8c210 6530 if (is_dupl)
6531 *pnode = node->next;
6532 else
c83a163c 6533 {
51e8c210 6534 pnode = &node->next;
6535 record_comdat_symbol_number (node->die, cu_hash_table,
6536 comdat_symbol_number);
6537 }
19f716e5 6538 }
51e8c210 6539 htab_delete (cu_hash_table);
19f716e5 6540}
6541
6542/* Traverse the DIE and add a sibling attribute if it may have the
6543 effect of speeding up access to siblings. To save some space,
6544 avoid generating sibling attributes for DIE's without children. */
6545
6546static void
8ec3a57b 6547add_sibling_attributes (dw_die_ref die)
19f716e5 6548{
19cb6b50 6549 dw_die_ref c;
19f716e5 6550
958656b7 6551 if (! die->die_child)
6552 return;
6553
6554 if (die->die_parent && die != die->die_parent->die_child)
4b72e226 6555 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6556
958656b7 6557 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
4b72e226 6558}
6559
8c3f468d 6560/* Output all location lists for the DIE and its children. */
6561
4c21a22f 6562static void
8ec3a57b 6563output_location_lists (dw_die_ref die)
4c21a22f 6564{
6565 dw_die_ref c;
6f56c055 6566 dw_attr_ref a;
6567 unsigned ix;
8c3f468d 6568
6f56c055 6569 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6570 if (AT_class (a) == dw_val_class_loc_list)
6571 output_loc_list (AT_loc_list (a));
8c3f468d 6572
958656b7 6573 FOR_EACH_CHILD (die, c, output_location_lists (c));
4c21a22f 6574}
bc70bd5e 6575
8c3f468d 6576/* The format of each DIE (and its attribute value pairs) is encoded in an
6577 abbreviation table. This routine builds the abbreviation table and assigns
6578 a unique abbreviation id for each abbreviation entry. The children of each
6579 die are visited recursively. */
4b72e226 6580
6581static void
8ec3a57b 6582build_abbrev_table (dw_die_ref die)
4b72e226 6583{
19cb6b50 6584 unsigned long abbrev_id;
6585 unsigned int n_alloc;
6586 dw_die_ref c;
6f56c055 6587 dw_attr_ref a;
6588 unsigned ix;
19f716e5 6589
6590 /* Scan the DIE references, and mark as external any that refer to
eabb26f3 6591 DIEs from other CUs (i.e. those which are not marked). */
6f56c055 6592 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6593 if (AT_class (a) == dw_val_class_die_ref
6594 && AT_ref (a)->die_mark == 0)
8c3f468d 6595 {
6f56c055 6596 gcc_assert (AT_ref (a)->die_symbol);
8c3f468d 6597
6f56c055 6598 set_AT_ref_external (a, 1);
8c3f468d 6599 }
19f716e5 6600
4b72e226 6601 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6602 {
19cb6b50 6603 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6f56c055 6604 dw_attr_ref die_a, abbrev_a;
6605 unsigned ix;
6606 bool ok = true;
61a9389f 6607
6f56c055 6608 if (abbrev->die_tag != die->die_tag)
6609 continue;
6610 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6611 continue;
61a9389f 6612
6f56c055 6613 if (VEC_length (dw_attr_node, abbrev->die_attr)
6614 != VEC_length (dw_attr_node, die->die_attr))
6615 continue;
61a9389f 6616
6f56c055 6617 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
4b72e226 6618 {
6f56c055 6619 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6620 if ((abbrev_a->dw_attr != die_a->dw_attr)
6621 || (value_format (abbrev_a) != value_format (die_a)))
4b72e226 6622 {
6f56c055 6623 ok = false;
6624 break;
4b72e226 6625 }
6626 }
6f56c055 6627 if (ok)
6628 break;
4b72e226 6629 }
6630
6631 if (abbrev_id >= abbrev_die_table_in_use)
6632 {
6633 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6634 {
6635 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
573aba85 6636 abbrev_die_table = ggc_realloc (abbrev_die_table,
6637 sizeof (dw_die_ref) * n_alloc);
4b72e226 6638
f0af5a88 6639 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
4b72e226 6640 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6641 abbrev_die_table_allocated = n_alloc;
6642 }
6643
6644 ++abbrev_die_table_in_use;
6645 abbrev_die_table[abbrev_id] = die;
6646 }
6647
6648 die->die_abbrev = abbrev_id;
958656b7 6649 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
4b72e226 6650}
6651\f
8a8bfbe7 6652/* Return the power-of-two number of bytes necessary to represent VALUE. */
6653
6654static int
8ec3a57b 6655constant_size (long unsigned int value)
8a8bfbe7 6656{
6657 int log;
6658
6659 if (value == 0)
6660 log = 0;
30ade641 6661 else
8a8bfbe7 6662 log = floor_log2 (value);
ec1e49cc 6663
8a8bfbe7 6664 log = log / 8;
6665 log = 1 << (floor_log2 (log) + 1);
6666
6667 return log;
30ade641 6668}
6669
8c3f468d 6670/* Return the size of a DIE as it is represented in the
8a8bfbe7 6671 .debug_info section. */
ec1e49cc 6672
8a8bfbe7 6673static unsigned long
8ec3a57b 6674size_of_die (dw_die_ref die)
30ade641 6675{
19cb6b50 6676 unsigned long size = 0;
6677 dw_attr_ref a;
6f56c055 6678 unsigned ix;
ec1e49cc 6679
8a8bfbe7 6680 size += size_of_uleb128 (die->die_abbrev);
6f56c055 6681 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
30ade641 6682 {
c90bf86c 6683 switch (AT_class (a))
30ade641 6684 {
6685 case dw_val_class_addr:
aaa408cd 6686 size += DWARF2_ADDR_SIZE;
30ade641 6687 break;
a36145ca 6688 case dw_val_class_offset:
6689 size += DWARF_OFFSET_SIZE;
6690 break;
30ade641 6691 case dw_val_class_loc:
8a8bfbe7 6692 {
19cb6b50 6693 unsigned long lsize = size_of_locs (AT_loc (a));
ec1e49cc 6694
8a8bfbe7 6695 /* Block length. */
6696 size += constant_size (lsize);
6697 size += lsize;
6698 }
30ade641 6699 break;
4c21a22f 6700 case dw_val_class_loc_list:
6701 size += DWARF_OFFSET_SIZE;
6702 break;
fe39c28c 6703 case dw_val_class_range_list:
6704 size += DWARF_OFFSET_SIZE;
6705 break;
30ade641 6706 case dw_val_class_const:
fddebe76 6707 size += size_of_sleb128 (AT_int (a));
30ade641 6708 break;
6709 case dw_val_class_unsigned_const:
c90bf86c 6710 size += constant_size (AT_unsigned (a));
30ade641 6711 break;
df78b73b 6712 case dw_val_class_long_long:
ca98eb0a 6713 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
df78b73b 6714 break;
1b6ad376 6715 case dw_val_class_vec:
6716 size += 1 + (a->dw_attr_val.v.val_vec.length
6717 * a->dw_attr_val.v.val_vec.elt_size); /* block */
30ade641 6718 break;
6719 case dw_val_class_flag:
8a8bfbe7 6720 size += 1;
30ade641 6721 break;
6722 case dw_val_class_die_ref:
1ef5e659 6723 if (AT_ref_external (a))
6724 size += DWARF2_ADDR_SIZE;
6725 else
6726 size += DWARF_OFFSET_SIZE;
30ade641 6727 break;
6728 case dw_val_class_fde_ref:
8a8bfbe7 6729 size += DWARF_OFFSET_SIZE;
30ade641 6730 break;
6731 case dw_val_class_lbl_id:
aaa408cd 6732 size += DWARF2_ADDR_SIZE;
8a8bfbe7 6733 break;
d08d29c0 6734 case dw_val_class_lineptr:
6735 case dw_val_class_macptr:
8a8bfbe7 6736 size += DWARF_OFFSET_SIZE;
6737 break;
6738 case dw_val_class_str:
80b7bd06 6739 if (AT_string_form (a) == DW_FORM_strp)
6740 size += DWARF_OFFSET_SIZE;
6741 else
573aba85 6742 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8a8bfbe7 6743 break;
69278c24 6744 case dw_val_class_file:
6745 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6746 break;
8a8bfbe7 6747 default:
7bd4f6b6 6748 gcc_unreachable ();
8a8bfbe7 6749 }
30ade641 6750 }
8a8bfbe7 6751
6752 return size;
30ade641 6753}
6754
8c3f468d 6755/* Size the debugging information associated with a given DIE. Visits the
6756 DIE's children recursively. Updates the global variable next_die_offset, on
6757 each time through. Uses the current value of next_die_offset to update the
6758 die_offset field in each DIE. */
ec1e49cc 6759
30ade641 6760static void
8ec3a57b 6761calc_die_sizes (dw_die_ref die)
30ade641 6762{
19cb6b50 6763 dw_die_ref c;
8c3f468d 6764
8a8bfbe7 6765 die->die_offset = next_die_offset;
6766 next_die_offset += size_of_die (die);
ec1e49cc 6767
958656b7 6768 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
ec1e49cc 6769
8a8bfbe7 6770 if (die->die_child != NULL)
6771 /* Count the null byte used to terminate sibling lists. */
6772 next_die_offset += 1;
30ade641 6773}
6774
eabb26f3 6775/* Set the marks for a die and its children. We do this so
19f716e5 6776 that we know whether or not a reference needs to use FORM_ref_addr; only
eabb26f3 6777 DIEs in the same CU will be marked. We used to clear out the offset
6778 and use that as the flag, but ran into ordering problems. */
19f716e5 6779
6780static void
8ec3a57b 6781mark_dies (dw_die_ref die)
19f716e5 6782{
19cb6b50 6783 dw_die_ref c;
8c3f468d 6784
7bd4f6b6 6785 gcc_assert (!die->die_mark);
8ec3a57b 6786
eabb26f3 6787 die->die_mark = 1;
958656b7 6788 FOR_EACH_CHILD (die, c, mark_dies (c));
eabb26f3 6789}
6790
6791/* Clear the marks for a die and its children. */
6792
6793static void
8ec3a57b 6794unmark_dies (dw_die_ref die)
eabb26f3 6795{
19cb6b50 6796 dw_die_ref c;
8c3f468d 6797
7bd4f6b6 6798 gcc_assert (die->die_mark);
8ec3a57b 6799
eabb26f3 6800 die->die_mark = 0;
958656b7 6801 FOR_EACH_CHILD (die, c, unmark_dies (c));
19f716e5 6802}
6803
51e8c210 6804/* Clear the marks for a die, its children and referred dies. */
6805
6806static void
8ec3a57b 6807unmark_all_dies (dw_die_ref die)
51e8c210 6808{
6809 dw_die_ref c;
6810 dw_attr_ref a;
6f56c055 6811 unsigned ix;
51e8c210 6812
6813 if (!die->die_mark)
6814 return;
6815 die->die_mark = 0;
6816
958656b7 6817 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
51e8c210 6818
6f56c055 6819 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
51e8c210 6820 if (AT_class (a) == dw_val_class_die_ref)
6821 unmark_all_dies (AT_ref (a));
6822}
6823
61a9389f 6824/* Return the size of the .debug_pubnames or .debug_pubtypes table
af84796a 6825 generated for the compilation unit. */
6efd403b 6826
8a8bfbe7 6827static unsigned long
af84796a 6828size_of_pubnames (VEC (pubname_entry, gc) * names)
6efd403b 6829{
19cb6b50 6830 unsigned long size;
6831 unsigned i;
af84796a 6832 pubname_ref p;
df78b73b 6833
8a8bfbe7 6834 size = DWARF_PUBNAMES_HEADER_SIZE;
af84796a 6835 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6836 if (names != pubtype_table
6837 || p->die->die_offset != 0
6838 || !flag_eliminate_unused_debug_types)
6839 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6efd403b 6840
8a8bfbe7 6841 size += DWARF_OFFSET_SIZE;
6842 return size;
6efd403b 6843}
6844
ad87de1e 6845/* Return the size of the information in the .debug_aranges section. */
df78b73b 6846
8a8bfbe7 6847static unsigned long
8ec3a57b 6848size_of_aranges (void)
df78b73b 6849{
19cb6b50 6850 unsigned long size;
df78b73b 6851
8a8bfbe7 6852 size = DWARF_ARANGES_HEADER_SIZE;
df78b73b 6853
8a8bfbe7 6854 /* Count the address/length pair for this compilation unit. */
aaa408cd 6855 size += 2 * DWARF2_ADDR_SIZE;
6856 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
df78b73b 6857
8a8bfbe7 6858 /* Count the two zero words used to terminated the address range table. */
aaa408cd 6859 size += 2 * DWARF2_ADDR_SIZE;
8a8bfbe7 6860 return size;
6861}
6862\f
6863/* Select the encoding of an attribute value. */
6864
6865static enum dwarf_form
8ec3a57b 6866value_format (dw_attr_ref a)
8a8bfbe7 6867{
c90bf86c 6868 switch (a->dw_attr_val.val_class)
df78b73b 6869 {
8a8bfbe7 6870 case dw_val_class_addr:
6871 return DW_FORM_addr;
fe39c28c 6872 case dw_val_class_range_list:
a36145ca 6873 case dw_val_class_offset:
04da8de9 6874 case dw_val_class_loc_list:
7bd4f6b6 6875 switch (DWARF_OFFSET_SIZE)
6876 {
6877 case 4:
6878 return DW_FORM_data4;
6879 case 8:
6880 return DW_FORM_data8;
6881 default:
6882 gcc_unreachable ();
6883 }
8a8bfbe7 6884 case dw_val_class_loc:
c90bf86c 6885 switch (constant_size (size_of_locs (AT_loc (a))))
df78b73b 6886 {
8a8bfbe7 6887 case 1:
6888 return DW_FORM_block1;
6889 case 2:
6890 return DW_FORM_block2;
df78b73b 6891 default:
7bd4f6b6 6892 gcc_unreachable ();
df78b73b 6893 }
8a8bfbe7 6894 case dw_val_class_const:
fddebe76 6895 return DW_FORM_sdata;
8a8bfbe7 6896 case dw_val_class_unsigned_const:
c90bf86c 6897 switch (constant_size (AT_unsigned (a)))
8a8bfbe7 6898 {
6899 case 1:
6900 return DW_FORM_data1;
6901 case 2:
6902 return DW_FORM_data2;
6903 case 4:
6904 return DW_FORM_data4;
6905 case 8:
6906 return DW_FORM_data8;
6907 default:
7bd4f6b6 6908 gcc_unreachable ();
8a8bfbe7 6909 }
6910 case dw_val_class_long_long:
6911 return DW_FORM_block1;
1b6ad376 6912 case dw_val_class_vec:
8a8bfbe7 6913 return DW_FORM_block1;
6914 case dw_val_class_flag:
6915 return DW_FORM_flag;
6916 case dw_val_class_die_ref:
19f716e5 6917 if (AT_ref_external (a))
6918 return DW_FORM_ref_addr;
6919 else
6920 return DW_FORM_ref;
8a8bfbe7 6921 case dw_val_class_fde_ref:
6922 return DW_FORM_data;
6923 case dw_val_class_lbl_id:
6924 return DW_FORM_addr;
d08d29c0 6925 case dw_val_class_lineptr:
6926 case dw_val_class_macptr:
8a8bfbe7 6927 return DW_FORM_data;
6928 case dw_val_class_str:
80b7bd06 6929 return AT_string_form (a);
69278c24 6930 case dw_val_class_file:
6931 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6932 {
6933 case 1:
6934 return DW_FORM_data1;
6935 case 2:
6936 return DW_FORM_data2;
6937 case 4:
6938 return DW_FORM_data4;
6939 default:
6940 gcc_unreachable ();
6941 }
a36145ca 6942
df78b73b 6943 default:
7bd4f6b6 6944 gcc_unreachable ();
df78b73b 6945 }
6efd403b 6946}
6947
8a8bfbe7 6948/* Output the encoding of an attribute value. */
df78b73b 6949
8a8bfbe7 6950static void
8ec3a57b 6951output_value_format (dw_attr_ref a)
6efd403b 6952{
c90bf86c 6953 enum dwarf_form form = value_format (a);
8c3f468d 6954
ca98eb0a 6955 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8a8bfbe7 6956}
df78b73b 6957
8a8bfbe7 6958/* Output the .debug_abbrev section which defines the DIE abbreviation
6959 table. */
df78b73b 6960
8a8bfbe7 6961static void
8ec3a57b 6962output_abbrev_section (void)
8a8bfbe7 6963{
6964 unsigned long abbrev_id;
ec1e49cc 6965
8a8bfbe7 6966 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6967 {
19cb6b50 6968 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6f56c055 6969 unsigned ix;
6970 dw_attr_ref a_attr;
ec1e49cc 6971
ca98eb0a 6972 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
ca98eb0a 6973 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6974 dwarf_tag_name (abbrev->die_tag));
ec1e49cc 6975
ca98eb0a 6976 if (abbrev->die_child != NULL)
6977 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6978 else
6979 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8a8bfbe7 6980
6f56c055 6981 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6982 ix++)
8a8bfbe7 6983 {
ca98eb0a 6984 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6985 dwarf_attr_name (a_attr->dw_attr));
c90bf86c 6986 output_value_format (a_attr);
df78b73b 6987 }
df78b73b 6988
ca98eb0a 6989 dw2_asm_output_data (1, 0, NULL);
6990 dw2_asm_output_data (1, 0, NULL);
df78b73b 6991 }
dd198c78 6992
6993 /* Terminate the table. */
ca98eb0a 6994 dw2_asm_output_data (1, 0, NULL);
6efd403b 6995}
6996
19f716e5 6997/* Output a symbol we can use to refer to this DIE from another CU. */
6998
6999static inline void
8ec3a57b 7000output_die_symbol (dw_die_ref die)
19f716e5 7001{
7002 char *sym = die->die_symbol;
7003
7004 if (sym == 0)
7005 return;
7006
7007 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7008 /* We make these global, not weak; if the target doesn't support
7009 .linkonce, it doesn't support combining the sections, so debugging
7010 will break. */
883b2e73 7011 targetm.asm_out.globalize_label (asm_out_file, sym);
8c3f468d 7012
19f716e5 7013 ASM_OUTPUT_LABEL (asm_out_file, sym);
7014}
7015
1d340a5e 7016/* Return a new location list, given the begin and end range, and the
8c3f468d 7017 expression. gensym tells us whether to generate a new internal symbol for
7018 this location list node, which is done for the head of the list only. */
7019
1d340a5e 7020static inline dw_loc_list_ref
8ec3a57b 7021new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7022 const char *section, unsigned int gensym)
1d340a5e 7023{
573aba85 7024 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
8c3f468d 7025
1d340a5e 7026 retlist->begin = begin;
7027 retlist->end = end;
7028 retlist->expr = expr;
7029 retlist->section = section;
bc70bd5e 7030 if (gensym)
1d340a5e 7031 retlist->ll_symbol = gen_internal_sym ("LLST");
8c3f468d 7032
1d340a5e 7033 return retlist;
7034}
7035
2358393e 7036/* Add a location description expression to a location list. */
8c3f468d 7037
1d340a5e 7038static inline void
8ec3a57b 7039add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7040 const char *begin, const char *end,
7041 const char *section)
1d340a5e 7042{
19cb6b50 7043 dw_loc_list_ref *d;
bc70bd5e 7044
6312a35e 7045 /* Find the end of the chain. */
1d340a5e 7046 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7047 ;
8c3f468d 7048
2358393e 7049 /* Add a new location list node to the list. */
1d340a5e 7050 *d = new_loc_list (descr, begin, end, section, 0);
7051}
7052
1897b881 7053static void
7054dwarf2out_switch_text_section (void)
7055{
7056 dw_fde_ref fde;
7057
792a073a 7058 gcc_assert (cfun);
4d0e931f 7059
1897b881 7060 fde = &fde_table[fde_table_in_use - 1];
7061 fde->dw_fde_switched_sections = true;
4d0e931f 7062 fde->dw_fde_hot_section_label = cfun->hot_section_label;
7063 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7064 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7065 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
dae1861f 7066 have_multiple_function_sections = true;
d8eb7025 7067
7068 /* Reset the current label on switching text sections, so that we
7069 don't attempt to advance_loc4 between labels in different sections. */
7070 fde->dw_fde_current_label = NULL;
1897b881 7071}
7072
2358393e 7073/* Output the location list given to us. */
8c3f468d 7074
4c21a22f 7075static void
8ec3a57b 7076output_loc_list (dw_loc_list_ref list_head)
4c21a22f 7077{
8c3f468d 7078 dw_loc_list_ref curr = list_head;
7079
4c21a22f 7080 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
a36145ca 7081
71c23453 7082 /* Walk the location list, and output each range + expression. */
bc70bd5e 7083 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
4c21a22f 7084 {
fe39c28c 7085 unsigned long size;
d53bb226 7086 /* Don't output an entry that starts and ends at the same address. */
7087 if (strcmp (curr->begin, curr->end) == 0)
7088 continue;
dae1861f 7089 if (!have_multiple_function_sections)
71c23453 7090 {
7091 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7092 "Location list begin address (%s)",
7093 list_head->ll_symbol);
7094 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7095 "Location list end address (%s)",
7096 list_head->ll_symbol);
7097 }
7098 else
7099 {
7100 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7101 "Location list begin address (%s)",
7102 list_head->ll_symbol);
7103 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7104 "Location list end address (%s)",
7105 list_head->ll_symbol);
7106 }
4c21a22f 7107 size = size_of_locs (curr->expr);
bc70bd5e 7108
4c21a22f 7109 /* Output the block length for this list of location operations. */
7bd4f6b6 7110 gcc_assert (size <= 0xffff);
fe39c28c 7111 dw2_asm_output_data (2, size, "%s", "Location expression size");
7112
4c21a22f 7113 output_loc_sequence (curr->expr);
7114 }
8c3f468d 7115
71c23453 7116 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
ec98ecf4 7117 "Location list terminator begin (%s)",
7118 list_head->ll_symbol);
71c23453 7119 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
ec98ecf4 7120 "Location list terminator end (%s)",
7121 list_head->ll_symbol);
4c21a22f 7122}
80b7bd06 7123
8a8bfbe7 7124/* Output the DIE and its attributes. Called recursively to generate
7125 the definitions of each child DIE. */
ec1e49cc 7126
30ade641 7127static void
8ec3a57b 7128output_die (dw_die_ref die)
30ade641 7129{
19cb6b50 7130 dw_attr_ref a;
7131 dw_die_ref c;
7132 unsigned long size;
6f56c055 7133 unsigned ix;
6efd403b 7134
19f716e5 7135 /* If someone in another CU might refer to us, set up a symbol for
7136 them to point to. */
7137 if (die->die_symbol)
7138 output_die_symbol (die);
7139
ca98eb0a 7140 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
de064be9 7141 (unsigned long)die->die_offset,
7142 dwarf_tag_name (die->die_tag));
6efd403b 7143
6f56c055 7144 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
30ade641 7145 {
ca98eb0a 7146 const char *name = dwarf_attr_name (a->dw_attr);
7147
c90bf86c 7148 switch (AT_class (a))
8a8bfbe7 7149 {
7150 case dw_val_class_addr:
ca98eb0a 7151 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8a8bfbe7 7152 break;
30ade641 7153
a36145ca 7154 case dw_val_class_offset:
7155 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7156 "%s", name);
7157 break;
7158
fe39c28c 7159 case dw_val_class_range_list:
7160 {
7161 char *p = strchr (ranges_section_label, '\0');
7162
3201d6f1 7163 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7164 a->dw_attr_val.v.val_offset);
fe39c28c 7165 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
d08d29c0 7166 debug_ranges_section, "%s", name);
fe39c28c 7167 *p = '\0';
7168 }
7169 break;
7170
8a8bfbe7 7171 case dw_val_class_loc:
c90bf86c 7172 size = size_of_locs (AT_loc (a));
ec1e49cc 7173
8a8bfbe7 7174 /* Output the block length for this list of location operations. */
ca98eb0a 7175 dw2_asm_output_data (constant_size (size), size, "%s", name);
ec1e49cc 7176
4b72e226 7177 output_loc_sequence (AT_loc (a));
30ade641 7178 break;
8a8bfbe7 7179
7180 case dw_val_class_const:
fddebe76 7181 /* ??? It would be slightly more efficient to use a scheme like is
7182 used for unsigned constants below, but gdb 4.x does not sign
7183 extend. Gdb 5.x does sign extend. */
ca98eb0a 7184 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
30ade641 7185 break;
8a8bfbe7 7186
7187 case dw_val_class_unsigned_const:
ca98eb0a 7188 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7189 AT_unsigned (a), "%s", name);
30ade641 7190 break;
8a8bfbe7 7191
7192 case dw_val_class_long_long:
ca98eb0a 7193 {
7194 unsigned HOST_WIDE_INT first, second;
8a8bfbe7 7195
8c3f468d 7196 dw2_asm_output_data (1,
7197 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
bc70bd5e 7198 "%s", name);
f80d1bcd 7199
ca98eb0a 7200 if (WORDS_BIG_ENDIAN)
7201 {
7202 first = a->dw_attr_val.v.val_long_long.hi;
7203 second = a->dw_attr_val.v.val_long_long.low;
7204 }
7205 else
7206 {
7207 first = a->dw_attr_val.v.val_long_long.low;
7208 second = a->dw_attr_val.v.val_long_long.hi;
7209 }
8c3f468d 7210
7211 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
ca98eb0a 7212 first, "long long constant");
8c3f468d 7213 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
ca98eb0a 7214 second, NULL);
7215 }
30ade641 7216 break;
8a8bfbe7 7217
1b6ad376 7218 case dw_val_class_vec:
57380eb2 7219 {
1b6ad376 7220 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7221 unsigned int len = a->dw_attr_val.v.val_vec.length;
19cb6b50 7222 unsigned int i;
1b6ad376 7223 unsigned char *p;
57380eb2 7224
1b6ad376 7225 dw2_asm_output_data (1, len * elt_size, "%s", name);
7226 if (elt_size > sizeof (HOST_WIDE_INT))
7227 {
7228 elt_size /= 2;
7229 len *= 2;
7230 }
7231 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7232 i < len;
7233 i++, p += elt_size)
7234 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7235 "fp or vector constant word %u", i);
f80d1bcd 7236 break;
57380eb2 7237 }
8a8bfbe7 7238
7239 case dw_val_class_flag:
ca98eb0a 7240 dw2_asm_output_data (1, AT_flag (a), "%s", name);
30ade641 7241 break;
a36145ca 7242
bc70bd5e 7243 case dw_val_class_loc_list:
4c21a22f 7244 {
7245 char *sym = AT_loc_list (a)->ll_symbol;
8c3f468d 7246
7bd4f6b6 7247 gcc_assert (sym);
d08d29c0 7248 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7249 "%s", name);
4c21a22f 7250 }
7251 break;
a36145ca 7252
8a8bfbe7 7253 case dw_val_class_die_ref:
19f716e5 7254 if (AT_ref_external (a))
ca98eb0a 7255 {
7256 char *sym = AT_ref (a)->die_symbol;
8c3f468d 7257
7bd4f6b6 7258 gcc_assert (sym);
d08d29c0 7259 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7260 "%s", name);
ca98eb0a 7261 }
19f716e5 7262 else
7bd4f6b6 7263 {
7264 gcc_assert (AT_ref (a)->die_offset);
7265 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7266 "%s", name);
7267 }
30ade641 7268 break;
8a8bfbe7 7269
7270 case dw_val_class_fde_ref:
19bce576 7271 {
7272 char l1[20];
8c3f468d 7273
ca98eb0a 7274 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7275 a->dw_attr_val.v.val_fde_index * 2);
d08d29c0 7276 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7277 "%s", name);
19bce576 7278 }
30ade641 7279 break;
30ade641 7280
8a8bfbe7 7281 case dw_val_class_lbl_id:
19e5668c 7282 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8a8bfbe7 7283 break;
ec1e49cc 7284
d08d29c0 7285 case dw_val_class_lineptr:
7286 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7287 debug_line_section, "%s", name);
7288 break;
7289
7290 case dw_val_class_macptr:
7291 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7292 debug_macinfo_section, "%s", name);
8a8bfbe7 7293 break;
30ade641 7294
8a8bfbe7 7295 case dw_val_class_str:
80b7bd06 7296 if (AT_string_form (a) == DW_FORM_strp)
7297 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7298 a->dw_attr_val.v.val_str->label,
d08d29c0 7299 debug_str_section,
895ecd4c 7300 "%s: \"%s\"", name, AT_string (a));
80b7bd06 7301 else
7302 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8a8bfbe7 7303 break;
840b696a 7304
69278c24 7305 case dw_val_class_file:
7306 {
7307 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
61a9389f 7308
69278c24 7309 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7310 a->dw_attr_val.v.val_file->filename);
7311 break;
7312 }
7313
8a8bfbe7 7314 default:
7bd4f6b6 7315 gcc_unreachable ();
8a8bfbe7 7316 }
8a8bfbe7 7317 }
ec1e49cc 7318
958656b7 7319 FOR_EACH_CHILD (die, c, output_die (c));
ec1e49cc 7320
8c3f468d 7321 /* Add null byte to terminate sibling list. */
8a8bfbe7 7322 if (die->die_child != NULL)
8c3f468d 7323 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
de064be9 7324 (unsigned long) die->die_offset);
8a8bfbe7 7325}
ec1e49cc 7326
8a8bfbe7 7327/* Output the compilation unit that appears at the beginning of the
7328 .debug_info section, and precedes the DIE descriptions. */
ec1e49cc 7329
8a8bfbe7 7330static void
8ec3a57b 7331output_compilation_unit_header (void)
8a8bfbe7 7332{
65bdc57c 7333 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7334 dw2_asm_output_data (4, 0xffffffff,
7335 "Initial length escape value indicating 64-bit DWARF extension");
7336 dw2_asm_output_data (DWARF_OFFSET_SIZE,
61a9389f 7337 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
ca98eb0a 7338 "Length of Compilation Unit Info");
ca98eb0a 7339 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
ca98eb0a 7340 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
d08d29c0 7341 debug_abbrev_section,
ca98eb0a 7342 "Offset Into Abbrev. Section");
ca98eb0a 7343 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
30ade641 7344}
7345
19f716e5 7346/* Output the compilation unit DIE and its children. */
7347
7348static void
8ec3a57b 7349output_comp_unit (dw_die_ref die, int output_if_empty)
19f716e5 7350{
dd9977e9 7351 const char *secname;
51e8c210 7352 char *oldsym, *tmp;
7353
7354 /* Unless we are outputting main CU, we may throw away empty ones. */
7355 if (!output_if_empty && die->die_child == NULL)
7356 return;
19f716e5 7357
8c3f468d 7358 /* Even if there are no children of this DIE, we must output the information
7359 about the compilation unit. Otherwise, on an empty translation unit, we
7360 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7361 will then complain when examining the file. First mark all the DIEs in
7362 this CU so we know which get local refs. */
eabb26f3 7363 mark_dies (die);
7364
7365 build_abbrev_table (die);
7366
1e625a2e 7367 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
19f716e5 7368 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7369 calc_die_sizes (die);
7370
51e8c210 7371 oldsym = die->die_symbol;
7372 if (oldsym)
19f716e5 7373 {
f0af5a88 7374 tmp = alloca (strlen (oldsym) + 24);
8c3f468d 7375
51e8c210 7376 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
dd9977e9 7377 secname = tmp;
19f716e5 7378 die->die_symbol = NULL;
2f14b1f9 7379 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
19f716e5 7380 }
7381 else
2f14b1f9 7382 switch_to_section (debug_info_section);
19f716e5 7383
7384 /* Output debugging information. */
19f716e5 7385 output_compilation_unit_header ();
7386 output_die (die);
7387
eabb26f3 7388 /* Leave the marks on the main CU, so we can check them in
7389 output_pubnames. */
51e8c210 7390 if (oldsym)
7391 {
7392 unmark_dies (die);
7393 die->die_symbol = oldsym;
7394 }
19f716e5 7395}
7396
7d709201 7397/* Return the DWARF2/3 pubname associated with a decl. */
59561872 7398
7795e5d1 7399static const char *
8ec3a57b 7400dwarf2_name (tree decl, int scope)
59561872 7401{
7d709201 7402 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
59561872 7403}
7404
dc7a29ce 7405/* Add a new entry to .debug_pubnames if appropriate. */
ec1e49cc 7406
dc7a29ce 7407static void
8ec3a57b 7408add_pubname (tree decl, dw_die_ref die)
dc7a29ce 7409{
af84796a 7410 pubname_entry e;
dc7a29ce 7411
7412 if (! TREE_PUBLIC (decl))
7413 return;
7414
af84796a 7415 e.die = die;
7416 e.name = xstrdup (dwarf2_name (decl, 1));
7417 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7418}
7419
7420/* Add a new entry to .debug_pubtypes if appropriate. */
7421
7422static void
7423add_pubtype (tree decl, dw_die_ref die)
7424{
7425 pubname_entry e;
7426
7427 e.name = NULL;
7428 if ((TREE_PUBLIC (decl)
7429 || die->die_parent == comp_unit_die)
7430 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
dc7a29ce 7431 {
af84796a 7432 e.die = die;
7433 if (TYPE_P (decl))
7434 {
7435 if (TYPE_NAME (decl))
7436 {
7437 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
52570507 7438 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
af84796a 7439 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7440 && DECL_NAME (TYPE_NAME (decl)))
52570507 7441 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
61a9389f 7442 else
af84796a 7443 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7444 }
7445 }
61a9389f 7446 else
af84796a 7447 e.name = xstrdup (dwarf2_name (decl, 1));
ec1e49cc 7448
af84796a 7449 /* If we don't have a name for the type, there's no point in adding
7450 it to the table. */
7451 if (e.name && e.name[0] != '\0')
7452 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7453 }
dc7a29ce 7454}
7455
30ade641 7456/* Output the public names table used to speed up access to externally
af84796a 7457 visible names; or the public types table used to find type definitions. */
ec1e49cc 7458
30ade641 7459static void
af84796a 7460output_pubnames (VEC (pubname_entry, gc) * names)
30ade641 7461{
19cb6b50 7462 unsigned i;
af84796a 7463 unsigned long pubnames_length = size_of_pubnames (names);
7464 pubname_ref pub;
ec1e49cc 7465
65bdc57c 7466 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7467 dw2_asm_output_data (4, 0xffffffff,
7468 "Initial length escape value indicating 64-bit DWARF extension");
af84796a 7469 if (names == pubname_table)
7470 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7471 "Length of Public Names Info");
7472 else
7473 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7474 "Length of Public Type Names Info");
ca98eb0a 7475 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
ca98eb0a 7476 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
d08d29c0 7477 debug_info_section,
ca98eb0a 7478 "Offset of Compilation Unit Info");
ca98eb0a 7479 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7480 "Compilation Unit Length");
ec1e49cc 7481
af84796a 7482 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
30ade641 7483 {
61a9389f 7484 /* We shouldn't see pubnames for DIEs outside of the main CU. */
af84796a 7485 if (names == pubname_table)
7486 gcc_assert (pub->die->die_mark);
19f716e5 7487
af84796a 7488 if (names != pubtype_table
7489 || pub->die->die_offset != 0
7490 || !flag_eliminate_unused_debug_types)
7491 {
7492 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7493 "DIE offset");
ec1e49cc 7494
af84796a 7495 dw2_asm_output_nstring (pub->name, -1, "external name");
7496 }
30ade641 7497 }
ec1e49cc 7498
ca98eb0a 7499 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
30ade641 7500}
7501
dc7a29ce 7502/* Add a new entry to .debug_aranges if appropriate. */
ec1e49cc 7503
dc7a29ce 7504static void
8ec3a57b 7505add_arange (tree decl, dw_die_ref die)
dc7a29ce 7506{
7507 if (! DECL_SECTION_NAME (decl))
7508 return;
7509
7510 if (arange_table_in_use == arange_table_allocated)
7511 {
7512 arange_table_allocated += ARANGE_TABLE_INCREMENT;
8ec3a57b 7513 arange_table = ggc_realloc (arange_table,
7514 (arange_table_allocated
573aba85 7515 * sizeof (dw_die_ref)));
7516 memset (arange_table + arange_table_in_use, 0,
7517 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
dc7a29ce 7518 }
ec1e49cc 7519
dc7a29ce 7520 arange_table[arange_table_in_use++] = die;
7521}
7522
30ade641 7523/* Output the information that goes into the .debug_aranges table.
7524 Namely, define the beginning and ending address range of the
7525 text section generated for this compilation unit. */
ec1e49cc 7526
30ade641 7527static void
8ec3a57b 7528output_aranges (void)
30ade641 7529{
19cb6b50 7530 unsigned i;
7531 unsigned long aranges_length = size_of_aranges ();
ec1e49cc 7532
65bdc57c 7533 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7534 dw2_asm_output_data (4, 0xffffffff,
7535 "Initial length escape value indicating 64-bit DWARF extension");
ca98eb0a 7536 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7537 "Length of Address Ranges Info");
ca98eb0a 7538 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
ca98eb0a 7539 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
d08d29c0 7540 debug_info_section,
ca98eb0a 7541 "Offset of Compilation Unit Info");
ca98eb0a 7542 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
ca98eb0a 7543 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
ec1e49cc 7544
e711a040 7545 /* We need to align to twice the pointer size here. */
7546 if (DWARF_ARANGES_PAD_SIZE)
7547 {
ca98eb0a 7548 /* Pad using a 2 byte words so that padding is correct for any
c83a163c 7549 pointer size. */
ca98eb0a 7550 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7551 2 * DWARF2_ADDR_SIZE);
950ae8fe 7552 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
ca98eb0a 7553 dw2_asm_output_data (2, 0, NULL);
e711a040 7554 }
ec1e49cc 7555
19e5668c 7556 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
4d0e931f 7557 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7558 text_section_label, "Length");
7559 if (flag_reorder_blocks_and_partition)
7560 {
61a9389f 7561 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
4d0e931f 7562 "Address");
7563 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7564 cold_text_section_label, "Length");
7565 }
ec1e49cc 7566
8c3f468d 7567 for (i = 0; i < arange_table_in_use; i++)
dc7a29ce 7568 {
2b553659 7569 dw_die_ref die = arange_table[i];
ec1e49cc 7570
19f716e5 7571 /* We shouldn't see aranges for DIEs outside of the main CU. */
7bd4f6b6 7572 gcc_assert (die->die_mark);
19f716e5 7573
2b553659 7574 if (die->die_tag == DW_TAG_subprogram)
ca98eb0a 7575 {
19e5668c 7576 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7cc7e163 7577 "Address");
ca98eb0a 7578 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7579 get_AT_low_pc (die), "Length");
7580 }
dc7a29ce 7581 else
59561872 7582 {
2b553659 7583 /* A static variable; extract the symbol from DW_AT_location.
7584 Note that this code isn't currently hit, as we only emit
7585 aranges for functions (jason 9/23/99). */
2b553659 7586 dw_attr_ref a = get_AT (die, DW_AT_location);
7587 dw_loc_descr_ref loc;
8c3f468d 7588
7bd4f6b6 7589 gcc_assert (a && AT_class (a) == dw_val_class_loc);
2b553659 7590
c90bf86c 7591 loc = AT_loc (a);
7bd4f6b6 7592 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
2b553659 7593
ca98eb0a 7594 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7595 loc->dw_loc_oprnd1.v.val_addr, "Address");
7596 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7597 get_AT_unsigned (die, DW_AT_byte_size),
7598 "Length");
59561872 7599 }
dc7a29ce 7600 }
ec1e49cc 7601
30ade641 7602 /* Output the terminator words. */
ca98eb0a 7603 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7604 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
30ade641 7605}
7606
a36145ca 7607/* Add a new entry to .debug_ranges. Return the offset at which it
7608 was placed. */
7609
7610static unsigned int
f221c0bd 7611add_ranges_num (int num)
a36145ca 7612{
7613 unsigned int in_use = ranges_table_in_use;
7614
7615 if (in_use == ranges_table_allocated)
7616 {
7617 ranges_table_allocated += RANGES_TABLE_INCREMENT;
f0af5a88 7618 ranges_table
7619 = ggc_realloc (ranges_table, (ranges_table_allocated
7620 * sizeof (struct dw_ranges_struct)));
573aba85 7621 memset (ranges_table + ranges_table_in_use, 0,
7622 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
a36145ca 7623 }
7624
f221c0bd 7625 ranges_table[in_use].num = num;
a36145ca 7626 ranges_table_in_use = in_use + 1;
7627
7628 return in_use * 2 * DWARF2_ADDR_SIZE;
7629}
7630
f221c0bd 7631/* Add a new entry to .debug_ranges corresponding to a block, or a
7632 range terminator if BLOCK is NULL. */
7633
7634static unsigned int
7635add_ranges (tree block)
7636{
7637 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7638}
7639
7640/* Add a new entry to .debug_ranges corresponding to a pair of
7641 labels. */
7642
7643static unsigned int
7644add_ranges_by_labels (const char *begin, const char *end)
7645{
7646 unsigned int in_use = ranges_by_label_in_use;
7647
7648 if (in_use == ranges_by_label_allocated)
7649 {
7650 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7651 ranges_by_label
7652 = ggc_realloc (ranges_by_label,
7653 (ranges_by_label_allocated
7654 * sizeof (struct dw_ranges_by_label_struct)));
7655 memset (ranges_by_label + ranges_by_label_in_use, 0,
7656 RANGES_TABLE_INCREMENT
7657 * sizeof (struct dw_ranges_by_label_struct));
7658 }
7659
7660 ranges_by_label[in_use].begin = begin;
7661 ranges_by_label[in_use].end = end;
7662 ranges_by_label_in_use = in_use + 1;
7663
7664 return add_ranges_num (-(int)in_use - 1);
7665}
7666
a36145ca 7667static void
8ec3a57b 7668output_ranges (void)
a36145ca 7669{
19cb6b50 7670 unsigned i;
0d95286f 7671 static const char *const start_fmt = "Offset 0x%x";
a36145ca 7672 const char *fmt = start_fmt;
7673
8c3f468d 7674 for (i = 0; i < ranges_table_in_use; i++)
a36145ca 7675 {
f221c0bd 7676 int block_num = ranges_table[i].num;
a36145ca 7677
f221c0bd 7678 if (block_num > 0)
a36145ca 7679 {
7680 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7681 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7682
7683 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7684 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7685
7686 /* If all code is in the text section, then the compilation
7687 unit base address defaults to DW_AT_low_pc, which is the
7688 base of the text section. */
dae1861f 7689 if (!have_multiple_function_sections)
a36145ca 7690 {
4d0e931f 7691 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7692 text_section_label,
7693 fmt, i * 2 * DWARF2_ADDR_SIZE);
7694 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7695 text_section_label, NULL);
a36145ca 7696 }
8c3f468d 7697
f221c0bd 7698 /* Otherwise, the compilation unit base address is zero,
7699 which allows us to use absolute addresses, and not worry
7700 about whether the target supports cross-section
7701 arithmetic. */
a36145ca 7702 else
7703 {
7704 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7705 fmt, i * 2 * DWARF2_ADDR_SIZE);
7706 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7707 }
7708
7709 fmt = NULL;
7710 }
f221c0bd 7711
7712 /* Negative block_num stands for an index into ranges_by_label. */
7713 else if (block_num < 0)
7714 {
7715 int lab_idx = - block_num - 1;
7716
7717 if (!have_multiple_function_sections)
7718 {
7719 gcc_unreachable ();
7720#if 0
7721 /* If we ever use add_ranges_by_labels () for a single
7722 function section, all we have to do is to take out
7723 the #if 0 above. */
7724 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7725 ranges_by_label[lab_idx].begin,
7726 text_section_label,
7727 fmt, i * 2 * DWARF2_ADDR_SIZE);
7728 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7729 ranges_by_label[lab_idx].end,
7730 text_section_label, NULL);
7731#endif
7732 }
7733 else
7734 {
7735 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7736 ranges_by_label[lab_idx].begin,
7737 fmt, i * 2 * DWARF2_ADDR_SIZE);
7738 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7739 ranges_by_label[lab_idx].end,
7740 NULL);
7741 }
7742 }
a36145ca 7743 else
7744 {
7745 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7746 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7747 fmt = start_fmt;
7748 }
7749 }
7750}
ac02093f 7751
7752/* Data structure containing information about input files. */
7753struct file_info
7754{
69278c24 7755 const char *path; /* Complete file name. */
7756 const char *fname; /* File name part. */
ac02093f 7757 int length; /* Length of entire string. */
69278c24 7758 struct dwarf_file_data * file_idx; /* Index in input file table. */
ac02093f 7759 int dir_idx; /* Index in directory table. */
7760};
7761
7762/* Data structure containing information about directories with source
7763 files. */
7764struct dir_info
7765{
69278c24 7766 const char *path; /* Path including directory name. */
ac02093f 7767 int length; /* Path length. */
7768 int prefix; /* Index of directory entry which is a prefix. */
ac02093f 7769 int count; /* Number of files in this directory. */
7770 int dir_idx; /* Index of directory used as base. */
ac02093f 7771};
7772
7773/* Callback function for file_info comparison. We sort by looking at
7774 the directories in the path. */
5fbe2ebb 7775
ac02093f 7776static int
8ec3a57b 7777file_info_cmp (const void *p1, const void *p2)
ac02093f 7778{
7779 const struct file_info *s1 = p1;
7780 const struct file_info *s2 = p2;
c1fdef8e 7781 const unsigned char *cp1;
7782 const unsigned char *cp2;
ac02093f 7783
5fbe2ebb 7784 /* Take care of file names without directories. We need to make sure that
7785 we return consistent values to qsort since some will get confused if
7786 we return the same value when identical operands are passed in opposite
7787 orders. So if neither has a directory, return 0 and otherwise return
7788 1 or -1 depending on which one has the directory. */
7789 if ((s1->path == s1->fname || s2->path == s2->fname))
7790 return (s2->path == s2->fname) - (s1->path == s1->fname);
ac02093f 7791
c1fdef8e 7792 cp1 = (const unsigned char *) s1->path;
7793 cp2 = (const unsigned char *) s2->path;
ac02093f 7794
7795 while (1)
7796 {
7797 ++cp1;
7798 ++cp2;
5fbe2ebb 7799 /* Reached the end of the first path? If so, handle like above. */
c1fdef8e 7800 if ((cp1 == (const unsigned char *) s1->fname)
7801 || (cp2 == (const unsigned char *) s2->fname))
7802 return ((cp2 == (const unsigned char *) s2->fname)
7803 - (cp1 == (const unsigned char *) s1->fname));
ac02093f 7804
7805 /* Character of current path component the same? */
5fbe2ebb 7806 else if (*cp1 != *cp2)
ac02093f 7807 return *cp1 - *cp2;
7808 }
7809}
7810
61a9389f 7811struct file_name_acquire_data
69278c24 7812{
7813 struct file_info *files;
7814 int used_files;
7815 int max_files;
7816};
7817
7818/* Traversal function for the hash table. */
7819
7820static int
7821file_name_acquire (void ** slot, void *data)
7822{
7823 struct file_name_acquire_data *fnad = data;
7824 struct dwarf_file_data *d = *slot;
7825 struct file_info *fi;
7826 const char *f;
7827
7828 gcc_assert (fnad->max_files >= d->emitted_number);
7829
7830 if (! d->emitted_number)
7831 return 1;
7832
7833 gcc_assert (fnad->max_files != fnad->used_files);
7834
7835 fi = fnad->files + fnad->used_files++;
7836
7837 /* Skip all leading "./". */
7838 f = d->filename;
974a92fe 7839 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
69278c24 7840 f += 2;
61a9389f 7841
69278c24 7842 /* Create a new array entry. */
7843 fi->path = f;
7844 fi->length = strlen (f);
7845 fi->file_idx = d;
61a9389f 7846
69278c24 7847 /* Search for the file name part. */
974a92fe 7848 f = strrchr (f, DIR_SEPARATOR);
7849#if defined (DIR_SEPARATOR_2)
7850 {
8defa33e 7851 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
974a92fe 7852
7853 if (g != NULL)
7854 {
7855 if (f == NULL || f < g)
7856 f = g;
7857 }
7858 }
7859#endif
7860
69278c24 7861 fi->fname = f == NULL ? fi->path : f + 1;
7862 return 1;
7863}
7864
ac02093f 7865/* Output the directory table and the file name table. We try to minimize
7866 the total amount of memory needed. A heuristic is used to avoid large
7867 slowdowns with many input files. */
8c3f468d 7868
ac02093f 7869static void
8ec3a57b 7870output_file_names (void)
ac02093f 7871{
69278c24 7872 struct file_name_acquire_data fnad;
7873 int numfiles;
ac02093f 7874 struct file_info *files;
7875 struct dir_info *dirs;
7876 int *saved;
7877 int *savehere;
7878 int *backmap;
69278c24 7879 int ndirs;
ac02093f 7880 int idx_offset;
69278c24 7881 int i;
ac02093f 7882 int idx;
7883
69278c24 7884 if (!last_emitted_file)
21d1bacf 7885 {
7886 dw2_asm_output_data (1, 0, "End directory table");
7887 dw2_asm_output_data (1, 0, "End file name table");
7888 return;
7889 }
7890
69278c24 7891 numfiles = last_emitted_file->emitted_number;
ac02093f 7892
69278c24 7893 /* Allocate the various arrays we need. */
7894 files = alloca (numfiles * sizeof (struct file_info));
7895 dirs = alloca (numfiles * sizeof (struct dir_info));
ac02093f 7896
69278c24 7897 fnad.files = files;
7898 fnad.used_files = 0;
7899 fnad.max_files = numfiles;
7900 htab_traverse (file_table, file_name_acquire, &fnad);
7901 gcc_assert (fnad.used_files == fnad.max_files);
8c3f468d 7902
69278c24 7903 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
ac02093f 7904
7905 /* Find all the different directories used. */
69278c24 7906 dirs[0].path = files[0].path;
7907 dirs[0].length = files[0].fname - files[0].path;
ac02093f 7908 dirs[0].prefix = -1;
ac02093f 7909 dirs[0].count = 1;
7910 dirs[0].dir_idx = 0;
69278c24 7911 files[0].dir_idx = 0;
ac02093f 7912 ndirs = 1;
7913
69278c24 7914 for (i = 1; i < numfiles; i++)
ac02093f 7915 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7916 && memcmp (dirs[ndirs - 1].path, files[i].path,
7917 dirs[ndirs - 1].length) == 0)
7918 {
7919 /* Same directory as last entry. */
7920 files[i].dir_idx = ndirs - 1;
ac02093f 7921 ++dirs[ndirs - 1].count;
7922 }
7923 else
7924 {
69278c24 7925 int j;
ac02093f 7926
7927 /* This is a new directory. */
7928 dirs[ndirs].path = files[i].path;
7929 dirs[ndirs].length = files[i].fname - files[i].path;
ac02093f 7930 dirs[ndirs].count = 1;
7931 dirs[ndirs].dir_idx = ndirs;
ac02093f 7932 files[i].dir_idx = ndirs;
7933
7934 /* Search for a prefix. */
3740694f 7935 dirs[ndirs].prefix = -1;
8c3f468d 7936 for (j = 0; j < ndirs; j++)
3740694f 7937 if (dirs[j].length < dirs[ndirs].length
7938 && dirs[j].length > 1
7939 && (dirs[ndirs].prefix == -1
7940 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7941 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7942 dirs[ndirs].prefix = j;
ac02093f 7943
7944 ++ndirs;
7945 }
7946
8c3f468d 7947 /* Now to the actual work. We have to find a subset of the directories which
7948 allow expressing the file name using references to the directory table
7949 with the least amount of characters. We do not do an exhaustive search
7950 where we would have to check out every combination of every single
7951 possible prefix. Instead we use a heuristic which provides nearly optimal
7952 results in most cases and never is much off. */
f0af5a88 7953 saved = alloca (ndirs * sizeof (int));
7954 savehere = alloca (ndirs * sizeof (int));
ac02093f 7955
7956 memset (saved, '\0', ndirs * sizeof (saved[0]));
8c3f468d 7957 for (i = 0; i < ndirs; i++)
ac02093f 7958 {
69278c24 7959 int j;
ac02093f 7960 int total;
7961
8c3f468d 7962 /* We can always save some space for the current directory. But this
7963 does not mean it will be enough to justify adding the directory. */
ac02093f 7964 savehere[i] = dirs[i].length;
7965 total = (savehere[i] - saved[i]) * dirs[i].count;
7966
8c3f468d 7967 for (j = i + 1; j < ndirs; j++)
ac02093f 7968 {
7969 savehere[j] = 0;
ac02093f 7970 if (saved[j] < dirs[i].length)
7971 {
7972 /* Determine whether the dirs[i] path is a prefix of the
7973 dirs[j] path. */
7974 int k;
7975
3740694f 7976 k = dirs[j].prefix;
ff279357 7977 while (k != -1 && k != (int) i)
3740694f 7978 k = dirs[k].prefix;
7979
ff279357 7980 if (k == (int) i)
3740694f 7981 {
69278c24 7982 /* Yes it is. We can possibly save some memory by
3740694f 7983 writing the filenames in dirs[j] relative to
7984 dirs[i]. */
7985 savehere[j] = dirs[i].length;
7986 total += (savehere[j] - saved[j]) * dirs[j].count;
7987 }
ac02093f 7988 }
7989 }
7990
69278c24 7991 /* Check whether we can save enough to justify adding the dirs[i]
ac02093f 7992 directory. */
7993 if (total > dirs[i].length + 1)
7994 {
3740694f 7995 /* It's worthwhile adding. */
bc70bd5e 7996 for (j = i; j < ndirs; j++)
ac02093f 7997 if (savehere[j] > 0)
7998 {
7999 /* Remember how much we saved for this directory so far. */
8000 saved[j] = savehere[j];
8001
8002 /* Remember the prefix directory. */
8003 dirs[j].dir_idx = i;
8004 }
8005 }
8006 }
8007
69278c24 8008 /* Emit the directory name table. */
ac02093f 8009 idx = 1;
f9038ab4 8010 idx_offset = dirs[0].length > 0 ? 1 : 0;
8c3f468d 8011 for (i = 1 - idx_offset; i < ndirs; i++)
69278c24 8012 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8013 "Directory Entry: 0x%x", i + idx_offset);
8c3f468d 8014
ca98eb0a 8015 dw2_asm_output_data (1, 0, "End directory table");
8016
69278c24 8017 /* We have to emit them in the order of emitted_number since that's
8018 used in the debug info generation. To do this efficiently we
8019 generate a back-mapping of the indices first. */
8020 backmap = alloca (numfiles * sizeof (int));
8021 for (i = 0; i < numfiles; i++)
8022 backmap[files[i].file_idx->emitted_number - 1] = i;
ac02093f 8023
8024 /* Now write all the file names. */
69278c24 8025 for (i = 0; i < numfiles; i++)
ac02093f 8026 {
8027 int file_idx = backmap[i];
8028 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8029
ca98eb0a 8030 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
69278c24 8031 "File Entry: 0x%x", (unsigned) i + 1);
ac02093f 8032
8033 /* Include directory index. */
69278c24 8034 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
ac02093f 8035
8036 /* Modification time. */
ca98eb0a 8037 dw2_asm_output_data_uleb128 (0, NULL);
ac02093f 8038
8039 /* File length in bytes. */
ca98eb0a 8040 dw2_asm_output_data_uleb128 (0, NULL);
ac02093f 8041 }
8c3f468d 8042
ca98eb0a 8043 dw2_asm_output_data (1, 0, "End file name table");
ac02093f 8044}
8045
8046
30ade641 8047/* Output the source line number correspondence information. This
155b05dc 8048 information goes into the .debug_line section. */
ec1e49cc 8049
30ade641 8050static void
8ec3a57b 8051output_line_info (void)
30ade641 8052{
3740694f 8053 char l1[20], l2[20], p1[20], p2[20];
30ade641 8054 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8055 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
19cb6b50 8056 unsigned opc;
8057 unsigned n_op_args;
8058 unsigned long lt_index;
8059 unsigned long current_line;
8060 long line_offset;
8061 long line_delta;
8062 unsigned long current_file;
8063 unsigned long function;
ec1e49cc 8064
ca98eb0a 8065 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8066 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
3740694f 8067 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8068 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
ec1e49cc 8069
65bdc57c 8070 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8071 dw2_asm_output_data (4, 0xffffffff,
8072 "Initial length escape value indicating 64-bit DWARF extension");
ca98eb0a 8073 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8074 "Length of Source Line Info");
8075 ASM_OUTPUT_LABEL (asm_out_file, l1);
ec1e49cc 8076
ca98eb0a 8077 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
3740694f 8078 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8079 ASM_OUTPUT_LABEL (asm_out_file, p1);
ec1e49cc 8080
bfba49c6 8081 /* Define the architecture-dependent minimum instruction length (in
8082 bytes). In this implementation of DWARF, this field is used for
8083 information purposes only. Since GCC generates assembly language,
8084 we have no a priori knowledge of how many instruction bytes are
8085 generated for each source line, and therefore can use only the
8086 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8087 commands. Accordingly, we fix this as `1', which is "correct
8088 enough" for all architectures, and don't let the target override. */
8089 dw2_asm_output_data (1, 1,
ca98eb0a 8090 "Minimum Instruction Length");
bfba49c6 8091
ca98eb0a 8092 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8093 "Default is_stmt_start flag");
ca98eb0a 8094 dw2_asm_output_data (1, DWARF_LINE_BASE,
8095 "Line Base Value (Special Opcodes)");
ca98eb0a 8096 dw2_asm_output_data (1, DWARF_LINE_RANGE,
8097 "Line Range Value (Special Opcodes)");
ca98eb0a 8098 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8099 "Special Opcode Base");
ec1e49cc 8100
8c3f468d 8101 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
30ade641 8102 {
8103 switch (opc)
8104 {
8105 case DW_LNS_advance_pc:
8106 case DW_LNS_advance_line:
8107 case DW_LNS_set_file:
8108 case DW_LNS_set_column:
8109 case DW_LNS_fixed_advance_pc:
8110 n_op_args = 1;
8111 break;
8112 default:
8113 n_op_args = 0;
8114 break;
8115 }
ca98eb0a 8116
8117 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8118 opc, n_op_args);
30ade641 8119 }
ec1e49cc 8120
ac02093f 8121 /* Write out the information about the files we use. */
8122 output_file_names ();
3740694f 8123 ASM_OUTPUT_LABEL (asm_out_file, p2);
30ade641 8124
d8488b8a 8125 /* We used to set the address register to the first location in the text
8126 section here, but that didn't accomplish anything since we already
8127 have a line note for the opening brace of the first function. */
30ade641 8128
8129 /* Generate the line number to PC correspondence table, encoded as
8130 a series of state machine operations. */
8131 current_file = 1;
8132 current_line = 1;
4d0e931f 8133
5fbee89d 8134 if (cfun && in_cold_section_p)
4d0e931f 8135 strcpy (prev_line_label, cfun->cold_section_label);
1897b881 8136 else
8137 strcpy (prev_line_label, text_section_label);
30ade641 8138 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8139 {
19cb6b50 8140 dw_line_info_ref line_info = &line_info_table[lt_index];
d8488b8a 8141
e7b3c55c 8142#if 0
8143 /* Disable this optimization for now; GDB wants to see two line notes
8144 at the beginning of a function so it can find the end of the
8145 prologue. */
8146
d8488b8a 8147 /* Don't emit anything for redundant notes. Just updating the
c83a163c 8148 address doesn't accomplish anything, because we already assume
8149 that anything after the last address is this line. */
d8488b8a 8150 if (line_info->dw_line_num == current_line
8151 && line_info->dw_file_num == current_file)
8152 continue;
e7b3c55c 8153#endif
ec1e49cc 8154
ca98eb0a 8155 /* Emit debug info for the address of the current line.
8156
8157 Unfortunately, we have little choice here currently, and must always
8c3f468d 8158 use the most general form. GCC does not know the address delta
ca98eb0a 8159 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
8160 attributes which will give an upper bound on the address range. We
8161 could perhaps use length attributes to determine when it is safe to
8162 use DW_LNS_fixed_advance_pc. */
8163
d58978a6 8164 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
db998a6a 8165 if (0)
8166 {
8167 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
ca98eb0a 8168 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8169 "DW_LNS_fixed_advance_pc");
8170 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
db998a6a 8171 }
8172 else
8173 {
aaa408cd 8174 /* This can handle any delta. This takes
c83a163c 8175 4+DWARF2_ADDR_SIZE bytes. */
ca98eb0a 8176 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8177 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8178 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 8179 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
db998a6a 8180 }
8c3f468d 8181
db998a6a 8182 strcpy (prev_line_label, line_label);
8183
8184 /* Emit debug info for the source file of the current line, if
8185 different from the previous line. */
30ade641 8186 if (line_info->dw_file_num != current_file)
8187 {
8188 current_file = line_info->dw_file_num;
ca98eb0a 8189 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
69278c24 8190 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
30ade641 8191 }
ec1e49cc 8192
db998a6a 8193 /* Emit debug info for the current line number, choosing the encoding
8194 that uses the least amount of space. */
d8488b8a 8195 if (line_info->dw_line_num != current_line)
30ade641 8196 {
d8488b8a 8197 line_offset = line_info->dw_line_num - current_line;
8198 line_delta = line_offset - DWARF_LINE_BASE;
8199 current_line = line_info->dw_line_num;
8200 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8c3f468d 8201 /* This can handle deltas from -10 to 234, using the current
8202 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
8203 takes 1 byte. */
8204 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8205 "line %lu", current_line);
d8488b8a 8206 else
8207 {
8208 /* This can handle any delta. This takes at least 4 bytes,
8209 depending on the value being encoded. */
ca98eb0a 8210 dw2_asm_output_data (1, DW_LNS_advance_line,
8211 "advance to line %lu", current_line);
8212 dw2_asm_output_data_sleb128 (line_offset, NULL);
8213 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
d8488b8a 8214 }
6efd403b 8215 }
8216 else
8c3f468d 8217 /* We still need to start a new row, so output a copy insn. */
8218 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
30ade641 8219 }
8220
db998a6a 8221 /* Emit debug info for the address of the end of the function. */
8222 if (0)
8223 {
ca98eb0a 8224 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8225 "DW_LNS_fixed_advance_pc");
8226 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
db998a6a 8227 }
8228 else
8229 {
ca98eb0a 8230 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8231 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8232 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 8233 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
db998a6a 8234 }
6ed29fb8 8235
ca98eb0a 8236 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8237 dw2_asm_output_data_uleb128 (1, NULL);
8238 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
c05d7491 8239
8240 function = 0;
8241 current_file = 1;
8242 current_line = 1;
f80d1bcd 8243 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
c05d7491 8244 {
19cb6b50 8245 dw_separate_line_info_ref line_info
c05d7491 8246 = &separate_line_info_table[lt_index];
ec1e49cc 8247
e7b3c55c 8248#if 0
d8488b8a 8249 /* Don't emit anything for redundant notes. */
8250 if (line_info->dw_line_num == current_line
8251 && line_info->dw_file_num == current_file
8252 && line_info->function == function)
8253 goto cont;
e7b3c55c 8254#endif
d8488b8a 8255
db998a6a 8256 /* Emit debug info for the address of the current line. If this is
8257 a new function, or the first line of a function, then we need
8258 to handle it differently. */
d58978a6 8259 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8260 lt_index);
c05d7491 8261 if (function != line_info->function)
8262 {
8263 function = line_info->function;
ec1e49cc 8264
2358393e 8265 /* Set the address register to the first line in the function. */
ca98eb0a 8266 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8267 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8268 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 8269 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
c05d7491 8270 }
8271 else
8272 {
db998a6a 8273 /* ??? See the DW_LNS_advance_pc comment above. */
8274 if (0)
8275 {
ca98eb0a 8276 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8277 "DW_LNS_fixed_advance_pc");
8278 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
db998a6a 8279 }
8280 else
8281 {
ca98eb0a 8282 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8283 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8284 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 8285 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
db998a6a 8286 }
c05d7491 8287 }
8c3f468d 8288
db998a6a 8289 strcpy (prev_line_label, line_label);
ec1e49cc 8290
db998a6a 8291 /* Emit debug info for the source file of the current line, if
8292 different from the previous line. */
c05d7491 8293 if (line_info->dw_file_num != current_file)
8294 {
8295 current_file = line_info->dw_file_num;
ca98eb0a 8296 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
69278c24 8297 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
c05d7491 8298 }
ec1e49cc 8299
db998a6a 8300 /* Emit debug info for the current line number, choosing the encoding
8301 that uses the least amount of space. */
c05d7491 8302 if (line_info->dw_line_num != current_line)
8303 {
8304 line_offset = line_info->dw_line_num - current_line;
8305 line_delta = line_offset - DWARF_LINE_BASE;
8306 current_line = line_info->dw_line_num;
8307 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
ca98eb0a 8308 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8309 "line %lu", current_line);
c05d7491 8310 else
8311 {
ca98eb0a 8312 dw2_asm_output_data (1, DW_LNS_advance_line,
8313 "advance to line %lu", current_line);
8314 dw2_asm_output_data_sleb128 (line_offset, NULL);
8315 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
c05d7491 8316 }
8317 }
d8488b8a 8318 else
ca98eb0a 8319 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
ec1e49cc 8320
e7b3c55c 8321#if 0
d8488b8a 8322 cont:
e7b3c55c 8323#endif
8c3f468d 8324
8325 lt_index++;
c05d7491 8326
8327 /* If we're done with a function, end its sequence. */
8328 if (lt_index == separate_line_info_table_in_use
8329 || separate_line_info_table[lt_index].function != function)
8330 {
8331 current_file = 1;
8332 current_line = 1;
ec1e49cc 8333
db998a6a 8334 /* Emit debug info for the address of the end of the function. */
d58978a6 8335 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
db998a6a 8336 if (0)
8337 {
ca98eb0a 8338 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8339 "DW_LNS_fixed_advance_pc");
8340 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
db998a6a 8341 }
8342 else
8343 {
ca98eb0a 8344 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8345 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8346 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
19e5668c 8347 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
db998a6a 8348 }
c05d7491 8349
8350 /* Output the marker for the end of this sequence. */
ca98eb0a 8351 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8352 dw2_asm_output_data_uleb128 (1, NULL);
8353 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
c05d7491 8354 }
8355 }
d6d10a79 8356
8357 /* Output the marker for the end of the line number info. */
ca98eb0a 8358 ASM_OUTPUT_LABEL (asm_out_file, l2);
30ade641 8359}
8360\f
30ade641 8361/* Given a pointer to a tree node for some base type, return a pointer to
8362 a DIE that describes the given type.
8363
8364 This routine must only be called for GCC type nodes that correspond to
8365 Dwarf base (fundamental) types. */
ec1e49cc 8366
30ade641 8367static dw_die_ref
8ec3a57b 8368base_type_die (tree type)
30ade641 8369{
19cb6b50 8370 dw_die_ref base_type_result;
19cb6b50 8371 enum dwarf_type encoding;
30ade641 8372
8c3f468d 8373 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
30ade641 8374 return 0;
8375
8376 switch (TREE_CODE (type))
8377 {
30ade641 8378 case INTEGER_TYPE:
e026e576 8379 if (TYPE_STRING_FLAG (type))
30ade641 8380 {
78a8ed03 8381 if (TYPE_UNSIGNED (type))
e026e576 8382 encoding = DW_ATE_unsigned_char;
5b67860b 8383 else
e026e576 8384 encoding = DW_ATE_signed_char;
30ade641 8385 }
e026e576 8386 else if (TYPE_UNSIGNED (type))
8387 encoding = DW_ATE_unsigned;
5b67860b 8388 else
e026e576 8389 encoding = DW_ATE_signed;
30ade641 8390 break;
8391
8392 case REAL_TYPE:
069b07bf 8393 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8394 encoding = DW_ATE_decimal_float;
8395 else
8396 encoding = DW_ATE_float;
30ade641 8397 break;
8398
5b5abf88 8399 /* Dwarf2 doesn't know anything about complex ints, so use
8400 a user defined type for it. */
30ade641 8401 case COMPLEX_TYPE:
5b5abf88 8402 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8403 encoding = DW_ATE_complex_float;
8404 else
8405 encoding = DW_ATE_lo_user;
30ade641 8406 break;
8407
8408 case BOOLEAN_TYPE:
5b67860b 8409 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8410 encoding = DW_ATE_boolean;
30ade641 8411 break;
8412
8413 default:
8c3f468d 8414 /* No other TREE_CODEs are Dwarf fundamental types. */
7bd4f6b6 8415 gcc_unreachable ();
30ade641 8416 }
8417
15cfae4e 8418 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
155b05dc 8419
1524656f 8420 /* This probably indicates a bug. */
8421 if (! TYPE_NAME (type))
8422 add_name_attribute (base_type_result, "__unknown__");
8423
5b67860b 8424 add_AT_unsigned (base_type_result, DW_AT_byte_size,
21638aad 8425 int_size_in_bytes (type));
5b67860b 8426 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
30ade641 8427
8428 return base_type_result;
8429}
8430
6ef828f9 8431/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
30ade641 8432 given input type is a Dwarf "fundamental" type. Otherwise return null. */
ec1e49cc 8433
8434static inline int
8ec3a57b 8435is_base_type (tree type)
30ade641 8436{
8437 switch (TREE_CODE (type))
8438 {
8439 case ERROR_MARK:
8440 case VOID_TYPE:
8441 case INTEGER_TYPE:
8442 case REAL_TYPE:
8443 case COMPLEX_TYPE:
8444 case BOOLEAN_TYPE:
30ade641 8445 return 1;
8446
30ade641 8447 case ARRAY_TYPE:
8448 case RECORD_TYPE:
8449 case UNION_TYPE:
8450 case QUAL_UNION_TYPE:
8451 case ENUMERAL_TYPE:
8452 case FUNCTION_TYPE:
8453 case METHOD_TYPE:
8454 case POINTER_TYPE:
8455 case REFERENCE_TYPE:
30ade641 8456 case OFFSET_TYPE:
8457 case LANG_TYPE:
4405d230 8458 case VECTOR_TYPE:
30ade641 8459 return 0;
8460
8461 default:
7bd4f6b6 8462 gcc_unreachable ();
30ade641 8463 }
ec1e49cc 8464
30ade641 8465 return 0;
8466}
8467
805e22b2 8468/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8469 node, return the size in bits for the type if it is a constant, or else
8470 return the alignment for the type if the type's size is not constant, or
8471 else return BITS_PER_WORD if the type actually turns out to be an
8472 ERROR_MARK node. */
8473
8474static inline unsigned HOST_WIDE_INT
8ec3a57b 8475simple_type_size_in_bits (tree type)
805e22b2 8476{
805e22b2 8477 if (TREE_CODE (type) == ERROR_MARK)
8478 return BITS_PER_WORD;
8479 else if (TYPE_SIZE (type) == NULL_TREE)
8480 return 0;
8481 else if (host_integerp (TYPE_SIZE (type), 1))
8482 return tree_low_cst (TYPE_SIZE (type), 1);
8483 else
8484 return TYPE_ALIGN (type);
8485}
8486
600dbd47 8487/* Return true if the debug information for the given type should be
8488 emitted as a subrange type. */
8489
8490static inline bool
6114cbf0 8491is_subrange_type (tree type)
8492{
93c7db82 8493 tree subtype = TREE_TYPE (type);
8494
fd45b48c 8495 /* Subrange types are identified by the fact that they are integer
8496 types, and that they have a subtype which is either an integer type
8497 or an enumeral type. */
8498
8499 if (TREE_CODE (type) != INTEGER_TYPE
8500 || subtype == NULL_TREE)
8501 return false;
8502
8503 if (TREE_CODE (subtype) != INTEGER_TYPE
8504 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8505 return false;
8506
62351b00 8507 if (TREE_CODE (type) == TREE_CODE (subtype)
8508 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8509 && TYPE_MIN_VALUE (type) != NULL
8510 && TYPE_MIN_VALUE (subtype) != NULL
8511 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8512 && TYPE_MAX_VALUE (type) != NULL
8513 && TYPE_MAX_VALUE (subtype) != NULL
8514 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8515 {
8516 /* The type and its subtype have the same representation. If in
61a9389f 8517 addition the two types also have the same name, then the given
8518 type is not a subrange type, but rather a plain base type. */
62351b00 8519 /* FIXME: brobecker/2004-03-22:
61a9389f 8520 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8521 therefore be sufficient to check the TYPE_SIZE node pointers
8522 rather than checking the actual size. Unfortunately, we have
8523 found some cases, such as in the Ada "integer" type, where
8524 this is not the case. Until this problem is solved, we need to
8525 keep checking the actual size. */
62351b00 8526 tree type_name = TYPE_NAME (type);
8527 tree subtype_name = TYPE_NAME (subtype);
8528
8529 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
61a9389f 8530 type_name = DECL_NAME (type_name);
62351b00 8531
8532 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
61a9389f 8533 subtype_name = DECL_NAME (subtype_name);
62351b00 8534
8535 if (type_name == subtype_name)
61a9389f 8536 return false;
62351b00 8537 }
8538
fd45b48c 8539 return true;
600dbd47 8540}
8541
8542/* Given a pointer to a tree node for a subrange type, return a pointer
8543 to a DIE that describes the given type. */
8544
8545static dw_die_ref
a7011153 8546subrange_type_die (tree type, dw_die_ref context_die)
600dbd47 8547{
600dbd47 8548 dw_die_ref subrange_die;
6114cbf0 8549 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8ec3a57b 8550
a7011153 8551 if (context_die == NULL)
8552 context_die = comp_unit_die;
8553
a7011153 8554 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
a84a50a5 8555
1524656f 8556 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
6114cbf0 8557 {
8558 /* The size of the subrange type and its base type do not match,
61a9389f 8559 so we need to generate a size attribute for the subrange type. */
6114cbf0 8560 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8561 }
8562
600dbd47 8563 if (TYPE_MIN_VALUE (type) != NULL)
8564 add_bound_info (subrange_die, DW_AT_lower_bound,
61a9389f 8565 TYPE_MIN_VALUE (type));
600dbd47 8566 if (TYPE_MAX_VALUE (type) != NULL)
8567 add_bound_info (subrange_die, DW_AT_upper_bound,
61a9389f 8568 TYPE_MAX_VALUE (type));
600dbd47 8569
8570 return subrange_die;
8571}
8572
30ade641 8573/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8574 entry that chains various modifiers in front of the given type. */
ec1e49cc 8575
30ade641 8576static dw_die_ref
8ec3a57b 8577modified_type_die (tree type, int is_const_type, int is_volatile_type,
8578 dw_die_ref context_die)
30ade641 8579{
19cb6b50 8580 enum tree_code code = TREE_CODE (type);
1524656f 8581 dw_die_ref mod_type_die;
19cb6b50 8582 dw_die_ref sub_die = NULL;
8583 tree item_type = NULL;
1524656f 8584 tree qualified_type;
8585 tree name;
8586
8587 if (code == ERROR_MARK)
8588 return NULL;
8589
8590 /* See if we already have the appropriately qualified variant of
8591 this type. */
8592 qualified_type
8593 = get_qualified_type (type,
8594 ((is_const_type ? TYPE_QUAL_CONST : 0)
8595 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
61a9389f 8596
1524656f 8597 /* If we do, then we can just use its DIE, if it exists. */
8598 if (qualified_type)
30ade641 8599 {
1524656f 8600 mod_type_die = lookup_type_die (qualified_type);
6efd403b 8601 if (mod_type_die)
1524656f 8602 return mod_type_die;
8603 }
61a9389f 8604
1524656f 8605 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
61a9389f 8606
1524656f 8607 /* Handle C typedef types. */
8608 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8609 {
8610 tree dtype = TREE_TYPE (name);
61a9389f 8611
1524656f 8612 if (qualified_type == dtype)
30ade641 8613 {
1524656f 8614 /* For a named type, use the typedef. */
8615 gen_type_die (qualified_type, context_die);
8616 return lookup_type_die (qualified_type);
30ade641 8617 }
37f26c2d 8618 else if (is_const_type < TYPE_READONLY (dtype)
8619 || is_volatile_type < TYPE_VOLATILE (dtype)
8620 || (is_const_type <= TYPE_READONLY (dtype)
8621 && is_volatile_type <= TYPE_VOLATILE (dtype)
8622 && DECL_ORIGINAL_TYPE (name) != type))
1524656f 8623 /* cv-unqualified version of named type. Just use the unnamed
8624 type to which it refers. */
8625 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8626 is_const_type, is_volatile_type,
8627 context_die);
8628 /* Else cv-qualified version of named type; fall through. */
8629 }
61a9389f 8630
1524656f 8631 if (is_const_type)
8632 {
8633 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8634 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8635 }
8636 else if (is_volatile_type)
8637 {
8638 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8639 sub_die = modified_type_die (type, 0, 0, context_die);
8640 }
8641 else if (code == POINTER_TYPE)
8642 {
8643 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8644 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8645 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8646 item_type = TREE_TYPE (type);
8647 }
8648 else if (code == REFERENCE_TYPE)
8649 {
8650 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8651 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8652 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8653 item_type = TREE_TYPE (type);
8654 }
8655 else if (is_subrange_type (type))
8656 {
8657 mod_type_die = subrange_type_die (type, context_die);
8658 item_type = TREE_TYPE (type);
8659 }
8660 else if (is_base_type (type))
8661 mod_type_die = base_type_die (type);
8662 else
8663 {
8664 gen_type_die (type, context_die);
61a9389f 8665
1524656f 8666 /* We have to get the type_main_variant here (and pass that to the
8667 `lookup_type_die' routine) because the ..._TYPE node we have
8668 might simply be a *copy* of some original type node (where the
8669 copy was created to help us keep track of typedef names) and
8670 that copy might have a different TYPE_UID from the original
8671 ..._TYPE node. */
8672 if (TREE_CODE (type) != VECTOR_TYPE)
8673 return lookup_type_die (type_main_variant (type));
30ade641 8674 else
1524656f 8675 /* Vectors have the debugging information in the type,
8676 not the main variant. */
8677 return lookup_type_die (type);
8678 }
61a9389f 8679
1524656f 8680 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8681 don't output a DW_TAG_typedef, since there isn't one in the
8682 user's program; just attach a DW_AT_name to the type. */
8683 if (name
8684 && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8685 {
8686 if (TREE_CODE (name) == TYPE_DECL)
8687 /* Could just call add_name_and_src_coords_attributes here,
8688 but since this is a builtin type it doesn't have any
8689 useful source coordinates anyway. */
8690 name = DECL_NAME (name);
8691 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
30ade641 8692 }
61a9389f 8693
1524656f 8694 if (qualified_type)
8695 equate_type_number_to_die (qualified_type, mod_type_die);
ec1e49cc 8696
39ee7a4a 8697 if (item_type)
ec1e49cc 8698 /* We must do this after the equate_type_number_to_die call, in case
8699 this is a recursive type. This ensures that the modified_type_die
8700 recursion will terminate even if the type is recursive. Recursive
8701 types are possible in Ada. */
8702 sub_die = modified_type_die (item_type,
8703 TYPE_READONLY (item_type),
8704 TYPE_VOLATILE (item_type),
8705 context_die);
8706
30ade641 8707 if (sub_die != NULL)
ec1e49cc 8708 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8709
30ade641 8710 return mod_type_die;
8711}
8712
30ade641 8713/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
1e625a2e 8714 an enumerated type. */
ec1e49cc 8715
8716static inline int
8ec3a57b 8717type_is_enum (tree type)
30ade641 8718{
8719 return TREE_CODE (type) == ENUMERAL_TYPE;
8720}
8721
7f3ca0ce 8722/* Return the DBX register number described by a given RTL node. */
4b72e226 8723
8724static unsigned int
7f3ca0ce 8725dbx_reg_number (rtx rtl)
4b72e226 8726{
19cb6b50 8727 unsigned regno = REGNO (rtl);
4b72e226 8728
7bd4f6b6 8729 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
4b72e226 8730
12d886b8 8731#ifdef LEAF_REG_REMAP
203898cb 8732 if (current_function_uses_only_leaf_regs)
8733 {
8734 int leaf_reg = LEAF_REG_REMAP (regno);
8735 if (leaf_reg != -1)
8736 regno = (unsigned) leaf_reg;
8737 }
12d886b8 8738#endif
8739
86e12d28 8740 return DBX_REGISTER_NUMBER (regno);
4b72e226 8741}
8742
fd51758c 8743/* Optionally add a DW_OP_piece term to a location description expression.
8744 DW_OP_piece is only added if the location description expression already
8745 doesn't end with DW_OP_piece. */
8746
8747static void
8748add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8749{
8750 dw_loc_descr_ref loc;
8751
8752 if (*list_head != NULL)
8753 {
8754 /* Find the end of the chain. */
8755 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8756 ;
8757
8758 if (loc->dw_loc_opc != DW_OP_piece)
8759 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8760 }
8761}
8762
86e12d28 8763/* Return a location descriptor that designates a machine register or
9754a2f0 8764 zero if there is none. */
ec1e49cc 8765
30ade641 8766static dw_loc_descr_ref
d53bb226 8767reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
30ade641 8768{
9754a2f0 8769 rtx regs;
ec1e49cc 8770
86e12d28 8771 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8772 return 0;
8773
883b2e73 8774 regs = targetm.dwarf_register_span (rtl);
9754a2f0 8775
12d886b8 8776 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
d53bb226 8777 return multiple_reg_loc_descriptor (rtl, regs, initialized);
9754a2f0 8778 else
d53bb226 8779 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9754a2f0 8780}
8781
8782/* Return a location descriptor that designates a machine register for
8783 a given hard register number. */
8784
8785static dw_loc_descr_ref
d53bb226 8786one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9754a2f0 8787{
d53bb226 8788 dw_loc_descr_ref reg_loc_descr;
9754a2f0 8789 if (regno <= 31)
d53bb226 8790 reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
88e24dbb 8791 else
d53bb226 8792 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8793
8794 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8795 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8796
8797 return reg_loc_descr;
9754a2f0 8798}
8799
8800/* Given an RTL of a register, return a location descriptor that
8801 designates a value that spans more than one register. */
8802
8803static dw_loc_descr_ref
d53bb226 8804multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8805 enum var_init_status initialized)
9754a2f0 8806{
8807 int nregs, size, i;
8808 unsigned reg;
8809 dw_loc_descr_ref loc_result = NULL;
ec1e49cc 8810
b6ea71e9 8811 reg = REGNO (rtl);
8812#ifdef LEAF_REG_REMAP
203898cb 8813 if (current_function_uses_only_leaf_regs)
8814 {
8815 int leaf_reg = LEAF_REG_REMAP (reg);
8816 if (leaf_reg != -1)
8817 reg = (unsigned) leaf_reg;
8818 }
b6ea71e9 8819#endif
8820 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
7f3ca0ce 8821 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9754a2f0 8822
8823 /* Simple, contiguous registers. */
8824 if (regs == NULL_RTX)
8825 {
8826 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8827
8828 loc_result = NULL;
8829 while (nregs--)
8830 {
8831 dw_loc_descr_ref t;
8832
d53bb226 8833 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8834 VAR_INIT_STATUS_INITIALIZED);
9754a2f0 8835 add_loc_descr (&loc_result, t);
4719779b 8836 add_loc_descr_op_piece (&loc_result, size);
a4920475 8837 ++reg;
9754a2f0 8838 }
8839 return loc_result;
8840 }
8841
8842 /* Now onto stupid register sets in non contiguous locations. */
8843
7bd4f6b6 8844 gcc_assert (GET_CODE (regs) == PARALLEL);
9754a2f0 8845
8846 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8847 loc_result = NULL;
8848
8849 for (i = 0; i < XVECLEN (regs, 0); ++i)
8850 {
8851 dw_loc_descr_ref t;
8852
d53bb226 8853 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8854 VAR_INIT_STATUS_INITIALIZED);
9754a2f0 8855 add_loc_descr (&loc_result, t);
8856 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
4719779b 8857 add_loc_descr_op_piece (&loc_result, size);
9754a2f0 8858 }
d53bb226 8859
8860 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8861 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
30ade641 8862 return loc_result;
8863}
8864
9ed904da 8865/* Return a location descriptor that designates a constant. */
8866
8867static dw_loc_descr_ref
8ec3a57b 8868int_loc_descriptor (HOST_WIDE_INT i)
9ed904da 8869{
8870 enum dwarf_location_atom op;
8871
8872 /* Pick the smallest representation of a constant, rather than just
8873 defaulting to the LEB encoding. */
8874 if (i >= 0)
8875 {
8876 if (i <= 31)
8877 op = DW_OP_lit0 + i;
8878 else if (i <= 0xff)
8879 op = DW_OP_const1u;
8880 else if (i <= 0xffff)
8881 op = DW_OP_const2u;
8882 else if (HOST_BITS_PER_WIDE_INT == 32
8883 || i <= 0xffffffff)
8884 op = DW_OP_const4u;
8885 else
8886 op = DW_OP_constu;
8887 }
8888 else
8889 {
8890 if (i >= -0x80)
8891 op = DW_OP_const1s;
8892 else if (i >= -0x8000)
8893 op = DW_OP_const2s;
8894 else if (HOST_BITS_PER_WIDE_INT == 32
8895 || i >= -0x80000000)
8896 op = DW_OP_const4s;
8897 else
8898 op = DW_OP_consts;
8899 }
8900
8901 return new_loc_descr (op, i, 0);
8902}
8903
30ade641 8904/* Return a location descriptor that designates a base+offset location. */
ec1e49cc 8905
30ade641 8906static dw_loc_descr_ref
d53bb226 8907based_loc_descr (rtx reg, HOST_WIDE_INT offset,
8908 enum var_init_status initialized)
30ade641 8909{
da72c083 8910 unsigned int regno;
d53bb226 8911 dw_loc_descr_ref result;
12d886b8 8912
8913 /* We only use "frame base" when we're sure we're talking about the
8914 post-prologue local stack frame. We do this by *not* running
8915 register elimination until this point, and recognizing the special
8916 argument pointer and soft frame pointer rtx's. */
8917 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8918 {
da72c083 8919 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12d886b8 8920
da72c083 8921 if (elim != reg)
8922 {
8923 if (GET_CODE (elim) == PLUS)
8924 {
8925 offset += INTVAL (XEXP (elim, 1));
8926 elim = XEXP (elim, 0);
8927 }
8928 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8929 : stack_pointer_rtx));
61a9389f 8930 offset += frame_pointer_fb_offset;
12d886b8 8931
61a9389f 8932 return new_loc_descr (DW_OP_fbreg, offset, 0);
da72c083 8933 }
12d886b8 8934 }
ec1e49cc 8935
da72c083 8936 regno = dbx_reg_number (reg);
8937 if (regno <= 31)
d53bb226 8938 result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
da72c083 8939 else
d53bb226 8940 result = new_loc_descr (DW_OP_bregx, regno, offset);
8941
8942 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8943 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8944
8945 return result;
30ade641 8946}
8947
8948/* Return true if this RTL expression describes a base+offset calculation. */
ec1e49cc 8949
8950static inline int
8ec3a57b 8951is_based_loc (rtx rtl)
30ade641 8952{
7cc7e163 8953 return (GET_CODE (rtl) == PLUS
8ad4c111 8954 && ((REG_P (XEXP (rtl, 0))
7cc7e163 8955 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8956 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
30ade641 8957}
8958
3ab1d710 8959/* Return a descriptor that describes the concatenation of N locations
8960 used to form the address of a memory location. */
8961
8962static dw_loc_descr_ref
d53bb226 8963concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
8964 enum var_init_status initialized)
3ab1d710 8965{
8966 unsigned int i;
8967 dw_loc_descr_ref cc_loc_result = NULL;
8968 unsigned int n = XVECLEN (concatn, 0);
8969
8970 for (i = 0; i < n; ++i)
8971 {
8972 dw_loc_descr_ref ref;
8973 rtx x = XVECEXP (concatn, 0, i);
8974
d53bb226 8975 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
3ab1d710 8976 if (ref == NULL)
8977 return NULL;
8978
8979 add_loc_descr (&cc_loc_result, ref);
8980 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
8981 }
8982
d53bb226 8983 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8984 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8985
3ab1d710 8986 return cc_loc_result;
8987}
8988
30ade641 8989/* The following routine converts the RTL for a variable or parameter
8990 (resident in memory) into an equivalent Dwarf representation of a
8991 mechanism for getting the address of that same variable onto the top of a
8992 hypothetical "address evaluation" stack.
ec1e49cc 8993
30ade641 8994 When creating memory location descriptors, we are effectively transforming
8995 the RTL for a memory-resident object into its Dwarf postfix expression
8996 equivalent. This routine recursively descends an RTL tree, turning
92a94502 8997 it into Dwarf postfix code as it goes.
8998
8999 MODE is the mode of the memory reference, needed to handle some
86e12d28 9000 autoincrement addressing modes.
9001
12d886b8 9002 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9003 location list for RTL.
b2025850 9004
86e12d28 9005 Return 0 if we can't represent the location. */
ec1e49cc 9006
30ade641 9007static dw_loc_descr_ref
d53bb226 9008mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9009 enum var_init_status initialized)
30ade641 9010{
9011 dw_loc_descr_ref mem_loc_result = NULL;
3122a117 9012 enum dwarf_location_atom op;
86e12d28 9013
f80d1bcd 9014 /* Note that for a dynamically sized array, the location we will generate a
30ade641 9015 description of here will be the lowest numbered location which is
9016 actually within the array. That's *not* necessarily the same as the
9017 zeroth element of the array. */
ec1e49cc 9018
883b2e73 9019 rtl = targetm.delegitimize_address (rtl);
eacbfaac 9020
30ade641 9021 switch (GET_CODE (rtl))
9022 {
92a94502 9023 case POST_INC:
9024 case POST_DEC:
93fbe1f3 9025 case POST_MODIFY:
92a94502 9026 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
9027 just fall into the SUBREG code. */
9028
8c3f468d 9029 /* ... fall through ... */
92a94502 9030
30ade641 9031 case SUBREG:
9032 /* The case of a subreg may arise when we have a local (register)
c83a163c 9033 variable or a formal (register) parameter which doesn't quite fill
9034 up an entire register. For now, just assume that it is
9035 legitimate to make the Dwarf info refer to the whole register which
9036 contains the given subreg. */
822e27f9 9037 rtl = XEXP (rtl, 0);
ec1e49cc 9038
8c3f468d 9039 /* ... fall through ... */
30ade641 9040
9041 case REG:
9042 /* Whenever a register number forms a part of the description of the
c83a163c 9043 method for calculating the (dynamic) address of a memory resident
9044 object, DWARF rules require the register number be referred to as
9045 a "base register". This distinction is not based in any way upon
9046 what category of register the hardware believes the given register
9047 belongs to. This is strictly DWARF terminology we're dealing with
9048 here. Note that in cases where the location of a memory-resident
9049 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9050 OP_CONST (0)) the actual DWARF location descriptor that we generate
9051 may just be OP_BASEREG (basereg). This may look deceptively like
9052 the object in question was allocated to a register (rather than in
9053 memory) so DWARF consumers need to be aware of the subtle
9054 distinction between OP_REG and OP_BASEREG. */
86e12d28 9055 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
d53bb226 9056 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
30ade641 9057 break;
9058
9059 case MEM:
d53bb226 9060 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9061 VAR_INIT_STATUS_INITIALIZED);
86e12d28 9062 if (mem_loc_result != 0)
9063 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
30ade641 9064 break;
9065
095ec610 9066 case LO_SUM:
9067 rtl = XEXP (rtl, 1);
9068
9069 /* ... fall through ... */
9070
9ed904da 9071 case LABEL_REF:
9072 /* Some ports can transform a symbol ref into a label ref, because
8ec3a57b 9073 the symbol ref is too far away and has to be dumped into a constant
9074 pool. */
30ade641 9075 case CONST:
9076 case SYMBOL_REF:
7012770b 9077 /* Alternatively, the symbol in the constant pool might be referenced
efdf6c61 9078 by a different symbol. */
8c3f468d 9079 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
dfc1ac47 9080 {
7ad1c520 9081 bool marked;
9082 rtx tmp = get_pool_constant_mark (rtl, &marked);
8c3f468d 9083
7012770b 9084 if (GET_CODE (tmp) == SYMBOL_REF)
7ad1c520 9085 {
9086 rtl = tmp;
9087 if (CONSTANT_POOL_ADDRESS_P (tmp))
9088 get_pool_constant_mark (tmp, &marked);
9089 else
9090 marked = true;
9091 }
9092
9093 /* If all references to this pool constant were optimized away,
9094 it was not output and thus we can't represent it.
9095 FIXME: might try to use DW_OP_const_value here, though
9096 DW_OP_piece complicates it. */
9097 if (!marked)
9098 return 0;
dfc1ac47 9099 }
9100
30ade641 9101 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9102 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
7facaa35 9103 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
62aedc4c 9104 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
30ade641 9105 break;
9106
93fbe1f3 9107 case PRE_MODIFY:
9108 /* Extract the PLUS expression nested inside and fall into
c83a163c 9109 PLUS code below. */
93fbe1f3 9110 rtl = XEXP (rtl, 1);
9111 goto plus;
9112
92a94502 9113 case PRE_INC:
9114 case PRE_DEC:
9115 /* Turn these into a PLUS expression and fall into the PLUS code
9116 below. */
9117 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9118 GEN_INT (GET_CODE (rtl) == PRE_INC
f80d1bcd 9119 ? GET_MODE_UNIT_SIZE (mode)
9120 : -GET_MODE_UNIT_SIZE (mode)));
9121
8c3f468d 9122 /* ... fall through ... */
92a94502 9123
30ade641 9124 case PLUS:
93fbe1f3 9125 plus:
30ade641 9126 if (is_based_loc (rtl))
12d886b8 9127 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
d53bb226 9128 INTVAL (XEXP (rtl, 1)),
9129 VAR_INIT_STATUS_INITIALIZED);
30ade641 9130 else
9131 {
d53bb226 9132 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9133 VAR_INIT_STATUS_INITIALIZED);
86e12d28 9134 if (mem_loc_result == 0)
9135 break;
9ed904da 9136
9137 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9138 && INTVAL (XEXP (rtl, 1)) >= 0)
86e12d28 9139 add_loc_descr (&mem_loc_result,
9140 new_loc_descr (DW_OP_plus_uconst,
9141 INTVAL (XEXP (rtl, 1)), 0));
9ed904da 9142 else
9143 {
9144 add_loc_descr (&mem_loc_result,
d53bb226 9145 mem_loc_descriptor (XEXP (rtl, 1), mode,
9146 VAR_INIT_STATUS_INITIALIZED));
9ed904da 9147 add_loc_descr (&mem_loc_result,
9148 new_loc_descr (DW_OP_plus, 0, 0));
9149 }
30ade641 9150 }
9151 break;
9152
3122a117 9153 /* If a pseudo-reg is optimized away, it is possible for it to
9154 be replaced with a MEM containing a multiply or shift. */
a10de18c 9155 case MULT:
3122a117 9156 op = DW_OP_mul;
9157 goto do_binop;
9158
9159 case ASHIFT:
9160 op = DW_OP_shl;
9161 goto do_binop;
8ff30ff6 9162
3122a117 9163 case ASHIFTRT:
9164 op = DW_OP_shra;
9165 goto do_binop;
9166
9167 case LSHIFTRT:
9168 op = DW_OP_shr;
9169 goto do_binop;
9170
9171 do_binop:
86e12d28 9172 {
d53bb226 9173 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9174 VAR_INIT_STATUS_INITIALIZED);
9175 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9176 VAR_INIT_STATUS_INITIALIZED);
86e12d28 9177
9178 if (op0 == 0 || op1 == 0)
9179 break;
9180
9181 mem_loc_result = op0;
9182 add_loc_descr (&mem_loc_result, op1);
3122a117 9183 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
86e12d28 9184 break;
9185 }
a10de18c 9186
30ade641 9187 case CONST_INT:
9ed904da 9188 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
30ade641 9189 break;
9190
3ab1d710 9191 case CONCATN:
d53bb226 9192 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9193 VAR_INIT_STATUS_INITIALIZED);
3ab1d710 9194 break;
9195
30ade641 9196 default:
7bd4f6b6 9197 gcc_unreachable ();
30ade641 9198 }
ec1e49cc 9199
d53bb226 9200 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9201 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9202
30ade641 9203 return mem_loc_result;
9204}
9205
ad87de1e 9206/* Return a descriptor that describes the concatenation of two locations.
fe829d4e 9207 This is typically a complex variable. */
9208
9209static dw_loc_descr_ref
d53bb226 9210concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
fe829d4e 9211{
9212 dw_loc_descr_ref cc_loc_result = NULL;
d53bb226 9213 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9214 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
fe829d4e 9215
86e12d28 9216 if (x0_ref == 0 || x1_ref == 0)
9217 return 0;
9218
9219 cc_loc_result = x0_ref;
4719779b 9220 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
fe829d4e 9221
86e12d28 9222 add_loc_descr (&cc_loc_result, x1_ref);
4719779b 9223 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
fe829d4e 9224
d53bb226 9225 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9226 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9227
fe829d4e 9228 return cc_loc_result;
9229}
9230
1a6a0f2a 9231/* Return a descriptor that describes the concatenation of N
9232 locations. */
9233
9234static dw_loc_descr_ref
d53bb226 9235concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
1a6a0f2a 9236{
9237 unsigned int i;
9238 dw_loc_descr_ref cc_loc_result = NULL;
9239 unsigned int n = XVECLEN (concatn, 0);
9240
9241 for (i = 0; i < n; ++i)
9242 {
9243 dw_loc_descr_ref ref;
9244 rtx x = XVECEXP (concatn, 0, i);
9245
d53bb226 9246 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
1a6a0f2a 9247 if (ref == NULL)
9248 return NULL;
9249
9250 add_loc_descr (&cc_loc_result, ref);
9251 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9252 }
9253
d53bb226 9254 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9255 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9256
1a6a0f2a 9257 return cc_loc_result;
9258}
9259
30ade641 9260/* Output a proper Dwarf location descriptor for a variable or parameter
9261 which is either allocated in a register or in a memory location. For a
9262 register, we just generate an OP_REG and the register number. For a
9263 memory location we provide a Dwarf postfix expression describing how to
86e12d28 9264 generate the (dynamic) address of the object onto the address stack.
9265
9266 If we don't know how to describe it, return 0. */
ec1e49cc 9267
30ade641 9268static dw_loc_descr_ref
d53bb226 9269loc_descriptor (rtx rtl, enum var_init_status initialized)
30ade641 9270{
9271 dw_loc_descr_ref loc_result = NULL;
86e12d28 9272
30ade641 9273 switch (GET_CODE (rtl))
9274 {
9275 case SUBREG:
30ade641 9276 /* The case of a subreg may arise when we have a local (register)
c83a163c 9277 variable or a formal (register) parameter which doesn't quite fill
9278 up an entire register. For now, just assume that it is
9279 legitimate to make the Dwarf info refer to the whole register which
9280 contains the given subreg. */
701e46d0 9281 rtl = SUBREG_REG (rtl);
ec1e49cc 9282
8c3f468d 9283 /* ... fall through ... */
30ade641 9284
9285 case REG:
d53bb226 9286 loc_result = reg_loc_descriptor (rtl, initialized);
30ade641 9287 break;
9288
9289 case MEM:
d53bb226 9290 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9291 initialized);
30ade641 9292 break;
9293
fe829d4e 9294 case CONCAT:
d53bb226 9295 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9296 initialized);
fe829d4e 9297 break;
9298
1a6a0f2a 9299 case CONCATN:
d53bb226 9300 loc_result = concatn_loc_descriptor (rtl, initialized);
1a6a0f2a 9301 break;
9302
b2025850 9303 case VAR_LOCATION:
9304 /* Single part. */
9305 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9306 {
d53bb226 9307 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
afcf285e 9308 break;
b2025850 9309 }
b2025850 9310
afcf285e 9311 rtl = XEXP (rtl, 1);
9312 /* FALLTHRU */
b2025850 9313
afcf285e 9314 case PARALLEL:
9315 {
9316 rtvec par_elems = XVEC (rtl, 0);
9317 int num_elem = GET_NUM_ELEM (par_elems);
9318 enum machine_mode mode;
9319 int i;
9320
9321 /* Create the first one, so we have something to add to. */
d53bb226 9322 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9323 initialized);
afcf285e 9324 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
4719779b 9325 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
afcf285e 9326 for (i = 1; i < num_elem; i++)
9327 {
9328 dw_loc_descr_ref temp;
9329
d53bb226 9330 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9331 initialized);
afcf285e 9332 add_loc_descr (&loc_result, temp);
9333 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
4719779b 9334 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
afcf285e 9335 }
9336 }
b2025850 9337 break;
9338
30ade641 9339 default:
7bd4f6b6 9340 gcc_unreachable ();
30ade641 9341 }
ec1e49cc 9342
30ade641 9343 return loc_result;
9344}
9345
8c3f468d 9346/* Similar, but generate the descriptor from trees instead of rtl. This comes
afcf285e 9347 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9348 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9349 top-level invocation, and we require the address of LOC; is 0 if we require
9350 the value of LOC. */
9ed904da 9351
9352static dw_loc_descr_ref
afcf285e 9353loc_descriptor_from_tree_1 (tree loc, int want_address)
9ed904da 9354{
86e12d28 9355 dw_loc_descr_ref ret, ret1;
afcf285e 9356 int have_address = 0;
9ed904da 9357 enum dwarf_location_atom op;
9358
9359 /* ??? Most of the time we do not take proper care for sign/zero
9360 extending the values properly. Hopefully this won't be a real
9361 problem... */
9362
9363 switch (TREE_CODE (loc))
9364 {
9365 case ERROR_MARK:
86e12d28 9366 return 0;
9ed904da 9367
86e12d28 9368 case PLACEHOLDER_EXPR:
a3915b32 9369 /* This case involves extracting fields from an object to determine the
9370 position of other fields. We don't try to encode this here. The
9371 only user of this is Ada, which encodes the needed information using
9372 the names of types. */
86e12d28 9373 return 0;
a3915b32 9374
dff29840 9375 case CALL_EXPR:
9376 return 0;
9377
7ddf4456 9378 case PREINCREMENT_EXPR:
9379 case PREDECREMENT_EXPR:
9380 case POSTINCREMENT_EXPR:
9381 case POSTDECREMENT_EXPR:
9382 /* There are no opcodes for these operations. */
9383 return 0;
9384
dff29840 9385 case ADDR_EXPR:
afcf285e 9386 /* If we already want an address, there's nothing we can do. */
9387 if (want_address)
9388 return 0;
dff29840 9389
afcf285e 9390 /* Otherwise, process the argument and look for the address. */
9391 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
dff29840 9392
9ed904da 9393 case VAR_DECL:
1b53eb20 9394 if (DECL_THREAD_LOCAL_P (loc))
931e9893 9395 {
9396 rtx rtl;
9397
931e9893 9398 /* If this is not defined, we have no way to emit the data. */
9dda1f80 9399 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
40af64cc 9400 return 0;
931e9893 9401
9402 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9403 look up addresses of objects in the current module. */
55bceb41 9404 if (DECL_EXTERNAL (loc))
931e9893 9405 return 0;
9406
9407 rtl = rtl_for_decl_location (loc);
9408 if (rtl == NULL_RTX)
9409 return 0;
9410
e16ceb8e 9411 if (!MEM_P (rtl))
931e9893 9412 return 0;
9413 rtl = XEXP (rtl, 0);
9414 if (! CONSTANT_P (rtl))
9415 return 0;
9416
9417 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9418 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9419 ret->dw_loc_oprnd1.v.val_addr = rtl;
9420
9421 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9422 add_loc_descr (&ret, ret1);
9423
afcf285e 9424 have_address = 1;
931e9893 9425 break;
9426 }
afcf285e 9427 /* FALLTHRU */
931e9893 9428
9ed904da 9429 case PARM_DECL:
75fa4f82 9430 if (DECL_HAS_VALUE_EXPR_P (loc))
9431 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9432 want_address);
afcf285e 9433 /* FALLTHRU */
9434
4ee9c684 9435 case RESULT_DECL:
cfdab332 9436 case FUNCTION_DECL:
9ed904da 9437 {
9438 rtx rtl = rtl_for_decl_location (loc);
9ed904da 9439
0ff98c8f 9440 if (rtl == NULL_RTX)
86e12d28 9441 return 0;
61a9389f 9442 else if (GET_CODE (rtl) == CONST_INT)
afcf285e 9443 {
9444 HOST_WIDE_INT val = INTVAL (rtl);
9445 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9446 val &= GET_MODE_MASK (DECL_MODE (loc));
9447 ret = int_loc_descriptor (val);
9448 }
9449 else if (GET_CODE (rtl) == CONST_STRING)
9450 return 0;
0ff98c8f 9451 else if (CONSTANT_P (rtl))
9ed904da 9452 {
9453 ret = new_loc_descr (DW_OP_addr, 0, 0);
9454 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9455 ret->dw_loc_oprnd1.v.val_addr = rtl;
9ed904da 9456 }
9457 else
9458 {
afcf285e 9459 enum machine_mode mode;
9460
9461 /* Certain constructs can only be represented at top-level. */
9462 if (want_address == 2)
d53bb226 9463 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
f3546830 9464
afcf285e 9465 mode = GET_MODE (rtl);
e16ceb8e 9466 if (MEM_P (rtl))
9ed904da 9467 {
9ed904da 9468 rtl = XEXP (rtl, 0);
afcf285e 9469 have_address = 1;
9ed904da 9470 }
d53bb226 9471 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9ed904da 9472 }
9473 }
9474 break;
9475
9476 case INDIRECT_REF:
afcf285e 9477 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9478 have_address = 1;
9ed904da 9479 break;
9480
3de30178 9481 case COMPOUND_EXPR:
afcf285e 9482 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
3de30178 9483
a8abe560 9484 case NOP_EXPR:
9485 case CONVERT_EXPR:
9486 case NON_LVALUE_EXPR:
f96c43fb 9487 case VIEW_CONVERT_EXPR:
a3915b32 9488 case SAVE_EXPR:
35cc02b5 9489 case GIMPLE_MODIFY_STMT:
9490 return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9491 want_address);
f9038ab4 9492
9ed904da 9493 case COMPONENT_REF:
9494 case BIT_FIELD_REF:
9495 case ARRAY_REF:
ba04d9d5 9496 case ARRAY_RANGE_REF:
9ed904da 9497 {
9498 tree obj, offset;
9499 HOST_WIDE_INT bitsize, bitpos, bytepos;
9500 enum machine_mode mode;
9501 int volatilep;
1e8e9920 9502 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9ed904da 9503
9504 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
e7e9416e 9505 &unsignedp, &volatilep, false);
86e12d28 9506
9507 if (obj == loc)
9508 return 0;
9509
afcf285e 9510 ret = loc_descriptor_from_tree_1 (obj, 1);
86e12d28 9511 if (ret == 0
8c3f468d 9512 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
86e12d28 9513 return 0;
9ed904da 9514
9515 if (offset != NULL_TREE)
9516 {
9517 /* Variable offset. */
afcf285e 9518 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9ed904da 9519 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9520 }
9521
9ed904da 9522 bytepos = bitpos / BITS_PER_UNIT;
9523 if (bytepos > 0)
9524 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9525 else if (bytepos < 0)
9526 {
9527 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9528 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9529 }
afcf285e 9530
9531 have_address = 1;
9ed904da 9532 break;
9533 }
9534
9535 case INTEGER_CST:
9536 if (host_integerp (loc, 0))
9537 ret = int_loc_descriptor (tree_low_cst (loc, 0));
86e12d28 9538 else
9539 return 0;
9ed904da 9540 break;
9ed904da 9541
15b7bb11 9542 case CONSTRUCTOR:
9543 {
6e326506 9544 /* Get an RTL for this, if something has been emitted. */
9545 rtx rtl = lookup_constant_def (loc);
9546 enum machine_mode mode;
9547
afcf285e 9548 if (!rtl || !MEM_P (rtl))
6e326506 9549 return 0;
9550 mode = GET_MODE (rtl);
9551 rtl = XEXP (rtl, 0);
d53bb226 9552 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
afcf285e 9553 have_address = 1;
15b7bb11 9554 break;
9555 }
9556
bc70bd5e 9557 case TRUTH_AND_EXPR:
cfd66c04 9558 case TRUTH_ANDIF_EXPR:
9ed904da 9559 case BIT_AND_EXPR:
9560 op = DW_OP_and;
9561 goto do_binop;
86e12d28 9562
cfd66c04 9563 case TRUTH_XOR_EXPR:
9ed904da 9564 case BIT_XOR_EXPR:
9565 op = DW_OP_xor;
9566 goto do_binop;
86e12d28 9567
cfd66c04 9568 case TRUTH_OR_EXPR:
9569 case TRUTH_ORIF_EXPR:
9ed904da 9570 case BIT_IOR_EXPR:
9571 op = DW_OP_or;
9572 goto do_binop;
86e12d28 9573
d7f71e5a 9574 case FLOOR_DIV_EXPR:
9575 case CEIL_DIV_EXPR:
9576 case ROUND_DIV_EXPR:
9ed904da 9577 case TRUNC_DIV_EXPR:
9578 op = DW_OP_div;
9579 goto do_binop;
86e12d28 9580
9ed904da 9581 case MINUS_EXPR:
9582 op = DW_OP_minus;
9583 goto do_binop;
86e12d28 9584
d7f71e5a 9585 case FLOOR_MOD_EXPR:
9586 case CEIL_MOD_EXPR:
9587 case ROUND_MOD_EXPR:
9ed904da 9588 case TRUNC_MOD_EXPR:
9589 op = DW_OP_mod;
9590 goto do_binop;
86e12d28 9591
9ed904da 9592 case MULT_EXPR:
9593 op = DW_OP_mul;
9594 goto do_binop;
86e12d28 9595
9ed904da 9596 case LSHIFT_EXPR:
9597 op = DW_OP_shl;
9598 goto do_binop;
86e12d28 9599
9ed904da 9600 case RSHIFT_EXPR:
1e8e9920 9601 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9ed904da 9602 goto do_binop;
86e12d28 9603
0de36bdb 9604 case POINTER_PLUS_EXPR:
9ed904da 9605 case PLUS_EXPR:
9606 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9607 && host_integerp (TREE_OPERAND (loc, 1), 0))
9608 {
afcf285e 9609 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
86e12d28 9610 if (ret == 0)
9611 return 0;
9612
9ed904da 9613 add_loc_descr (&ret,
9614 new_loc_descr (DW_OP_plus_uconst,
9615 tree_low_cst (TREE_OPERAND (loc, 1),
9616 0),
9617 0));
9618 break;
9619 }
86e12d28 9620
9ed904da 9621 op = DW_OP_plus;
9622 goto do_binop;
8c3f468d 9623
9ed904da 9624 case LE_EXPR:
78a8ed03 9625 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
86e12d28 9626 return 0;
9627
9ed904da 9628 op = DW_OP_le;
9629 goto do_binop;
86e12d28 9630
9ed904da 9631 case GE_EXPR:
78a8ed03 9632 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
86e12d28 9633 return 0;
9634
9ed904da 9635 op = DW_OP_ge;
9636 goto do_binop;
86e12d28 9637
9ed904da 9638 case LT_EXPR:
78a8ed03 9639 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
86e12d28 9640 return 0;
9641
9ed904da 9642 op = DW_OP_lt;
9643 goto do_binop;
86e12d28 9644
9ed904da 9645 case GT_EXPR:
78a8ed03 9646 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
86e12d28 9647 return 0;
9648
9ed904da 9649 op = DW_OP_gt;
9650 goto do_binop;
86e12d28 9651
9ed904da 9652 case EQ_EXPR:
9653 op = DW_OP_eq;
9654 goto do_binop;
86e12d28 9655
9ed904da 9656 case NE_EXPR:
9657 op = DW_OP_ne;
9658 goto do_binop;
9659
9660 do_binop:
afcf285e 9661 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9662 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
86e12d28 9663 if (ret == 0 || ret1 == 0)
9664 return 0;
9665
9666 add_loc_descr (&ret, ret1);
9ed904da 9667 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9668 break;
9669
cfd66c04 9670 case TRUTH_NOT_EXPR:
9ed904da 9671 case BIT_NOT_EXPR:
9672 op = DW_OP_not;
9673 goto do_unop;
86e12d28 9674
9ed904da 9675 case ABS_EXPR:
9676 op = DW_OP_abs;
9677 goto do_unop;
86e12d28 9678
9ed904da 9679 case NEGATE_EXPR:
9680 op = DW_OP_neg;
9681 goto do_unop;
9682
9683 do_unop:
afcf285e 9684 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
86e12d28 9685 if (ret == 0)
9686 return 0;
9687
9ed904da 9688 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9689 break;
9690
93823dba 9691 case MIN_EXPR:
9ed904da 9692 case MAX_EXPR:
93823dba 9693 {
61a9389f 9694 const enum tree_code code =
9695 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
93823dba 9696
61a9389f 9697 loc = build3 (COND_EXPR, TREE_TYPE (loc),
b55f9493 9698 build2 (code, integer_type_node,
9699 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
61a9389f 9700 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
93823dba 9701 }
8c3f468d 9702
04641143 9703 /* ... fall through ... */
9ed904da 9704
9705 case COND_EXPR:
9706 {
86e12d28 9707 dw_loc_descr_ref lhs
afcf285e 9708 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
86e12d28 9709 dw_loc_descr_ref rhs
afcf285e 9710 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9ed904da 9711 dw_loc_descr_ref bra_node, jump_node, tmp;
9712
afcf285e 9713 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
86e12d28 9714 if (ret == 0 || lhs == 0 || rhs == 0)
9715 return 0;
9716
9ed904da 9717 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9718 add_loc_descr (&ret, bra_node);
9719
86e12d28 9720 add_loc_descr (&ret, rhs);
9ed904da 9721 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9722 add_loc_descr (&ret, jump_node);
9723
86e12d28 9724 add_loc_descr (&ret, lhs);
9ed904da 9725 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
86e12d28 9726 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9ed904da 9727
9728 /* ??? Need a node to point the skip at. Use a nop. */
9729 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9730 add_loc_descr (&ret, tmp);
9731 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9732 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9733 }
9734 break;
9735
c98c6570 9736 case FIX_TRUNC_EXPR:
c98c6570 9737 return 0;
9738
9ed904da 9739 default:
76d1c62d 9740 /* Leave front-end specific codes as simply unknown. This comes
9741 up, for instance, with the C STMT_EXPR. */
9742 if ((unsigned int) TREE_CODE (loc)
61a9389f 9743 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
76d1c62d 9744 return 0;
9745
13346250 9746#ifdef ENABLE_CHECKING
76d1c62d 9747 /* Otherwise this is a generic code; we should just lists all of
89f18f73 9748 these explicitly. We forgot one. */
7bd4f6b6 9749 gcc_unreachable ();
13346250 9750#else
9751 /* In a release build, we want to degrade gracefully: better to
9752 generate incomplete debugging information than to crash. */
9753 return NULL;
9754#endif
9ed904da 9755 }
9756
86e12d28 9757 /* Show if we can't fill the request for an address. */
afcf285e 9758 if (want_address && !have_address)
86e12d28 9759 return 0;
9ed904da 9760
9761 /* If we've got an address and don't want one, dereference. */
9338678e 9762 if (!want_address && have_address && ret)
9ed904da 9763 {
86e12d28 9764 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9765
9766 if (size > DWARF2_ADDR_SIZE || size == -1)
9767 return 0;
8c3f468d 9768 else if (size == DWARF2_ADDR_SIZE)
9ed904da 9769 op = DW_OP_deref;
9770 else
9771 op = DW_OP_deref_size;
86e12d28 9772
9773 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9ed904da 9774 }
9775
9776 return ret;
9777}
9778
afcf285e 9779static inline dw_loc_descr_ref
9780loc_descriptor_from_tree (tree loc)
9781{
9782 return loc_descriptor_from_tree_1 (loc, 2);
9783}
9784
5d844ba2 9785/* Given a value, round it up to the lowest multiple of `boundary'
30ade641 9786 which is not less than the value itself. */
ec1e49cc 9787
5d844ba2 9788static inline HOST_WIDE_INT
8ec3a57b 9789ceiling (HOST_WIDE_INT value, unsigned int boundary)
30ade641 9790{
9791 return (((value + boundary - 1) / boundary) * boundary);
9792}
9793
9794/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9795 pointer to the declared type for the relevant field variable, or return
9796 `integer_type_node' if the given node turns out to be an
9797 ERROR_MARK node. */
ec1e49cc 9798
9799static inline tree
8ec3a57b 9800field_type (tree decl)
30ade641 9801{
19cb6b50 9802 tree type;
30ade641 9803
9804 if (TREE_CODE (decl) == ERROR_MARK)
9805 return integer_type_node;
9806
9807 type = DECL_BIT_FIELD_TYPE (decl);
ec1e49cc 9808 if (type == NULL_TREE)
30ade641 9809 type = TREE_TYPE (decl);
9810
9811 return type;
9812}
9813
2180a0af 9814/* Given a pointer to a tree node, return the alignment in bits for
9815 it, or else return BITS_PER_WORD if the node actually turns out to
9816 be an ERROR_MARK node. */
ec1e49cc 9817
9818static inline unsigned
8ec3a57b 9819simple_type_align_in_bits (tree type)
30ade641 9820{
9821 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9822}
9823
2180a0af 9824static inline unsigned
8ec3a57b 9825simple_decl_align_in_bits (tree decl)
2180a0af 9826{
9827 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9828}
9829
71b5358c 9830/* Return the result of rounding T up to ALIGN. */
9831
9832static inline HOST_WIDE_INT
9833round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9834{
9835 /* We must be careful if T is negative because HOST_WIDE_INT can be
9836 either "above" or "below" unsigned int as per the C promotion
9837 rules, depending on the host, thus making the signedness of the
9838 direct multiplication and division unpredictable. */
9839 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9840
9841 u += align - 1;
9842 u /= align;
9843 u *= align;
9844
9845 return (HOST_WIDE_INT) u;
9846}
9847
8c3f468d 9848/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9849 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9850 or return 0 if we are unable to determine what that offset is, either
9851 because the argument turns out to be a pointer to an ERROR_MARK node, or
9852 because the offset is actually variable. (We can't handle the latter case
9853 just yet). */
ec1e49cc 9854
5d844ba2 9855static HOST_WIDE_INT
8ec3a57b 9856field_byte_offset (tree decl)
30ade641 9857{
5d844ba2 9858 HOST_WIDE_INT object_offset_in_bits;
5d844ba2 9859 HOST_WIDE_INT bitpos_int;
30ade641 9860
9861 if (TREE_CODE (decl) == ERROR_MARK)
9862 return 0;
8ff30ff6 9863
7bd4f6b6 9864 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
30ade641 9865
f80d1bcd 9866 /* We cannot yet cope with fields whose positions are variable, so
30ade641 9867 for now, when we see such things, we simply return 0. Someday, we may
9868 be able to handle such cases, but it will be damn difficult. */
5d844ba2 9869 if (! host_integerp (bit_position (decl), 0))
30ade641 9870 return 0;
155b05dc 9871
5d844ba2 9872 bitpos_int = int_bit_position (decl);
30ade641 9873
feb5f1b1 9874#ifdef PCC_BITFIELD_TYPE_MATTERS
9875 if (PCC_BITFIELD_TYPE_MATTERS)
9876 {
9877 tree type;
9878 tree field_size_tree;
9879 HOST_WIDE_INT deepest_bitpos;
9880 unsigned HOST_WIDE_INT field_size_in_bits;
9881 unsigned int type_align_in_bits;
9882 unsigned int decl_align_in_bits;
9883 unsigned HOST_WIDE_INT type_size_in_bits;
30ade641 9884
feb5f1b1 9885 type = field_type (decl);
9886 field_size_tree = DECL_SIZE (decl);
30ade641 9887
feb5f1b1 9888 /* The size could be unspecified if there was an error, or for
9889 a flexible array member. */
9890 if (! field_size_tree)
9891 field_size_tree = bitsize_zero_node;
9892
9893 /* If we don't know the size of the field, pretend it's a full word. */
9894 if (host_integerp (field_size_tree, 1))
9895 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9896 else
9897 field_size_in_bits = BITS_PER_WORD;
9898
9899 type_size_in_bits = simple_type_size_in_bits (type);
9900 type_align_in_bits = simple_type_align_in_bits (type);
9901 decl_align_in_bits = simple_decl_align_in_bits (decl);
9902
9903 /* The GCC front-end doesn't make any attempt to keep track of the
9904 starting bit offset (relative to the start of the containing
9905 structure type) of the hypothetical "containing object" for a
9906 bit-field. Thus, when computing the byte offset value for the
9907 start of the "containing object" of a bit-field, we must deduce
9908 this information on our own. This can be rather tricky to do in
9909 some cases. For example, handling the following structure type
9910 definition when compiling for an i386/i486 target (which only
9911 aligns long long's to 32-bit boundaries) can be very tricky:
30ade641 9912
9913 struct S { int field1; long long field2:31; };
9914
feb5f1b1 9915 Fortunately, there is a simple rule-of-thumb which can be used
9916 in such cases. When compiling for an i386/i486, GCC will
9917 allocate 8 bytes for the structure shown above. It decides to
9918 do this based upon one simple rule for bit-field allocation.
9919 GCC allocates each "containing object" for each bit-field at
9920 the first (i.e. lowest addressed) legitimate alignment boundary
9921 (based upon the required minimum alignment for the declared
9922 type of the field) which it can possibly use, subject to the
9923 condition that there is still enough available space remaining
9924 in the containing object (when allocated at the selected point)
9925 to fully accommodate all of the bits of the bit-field itself.
9926
9927 This simple rule makes it obvious why GCC allocates 8 bytes for
9928 each object of the structure type shown above. When looking
9929 for a place to allocate the "containing object" for `field2',
9930 the compiler simply tries to allocate a 64-bit "containing
9931 object" at each successive 32-bit boundary (starting at zero)
9932 until it finds a place to allocate that 64- bit field such that
9933 at least 31 contiguous (and previously unallocated) bits remain
9934 within that selected 64 bit field. (As it turns out, for the
9935 example above, the compiler finds it is OK to allocate the
9936 "containing object" 64-bit field at bit-offset zero within the
9937 structure type.)
9938
9939 Here we attempt to work backwards from the limited set of facts
9940 we're given, and we try to deduce from those facts, where GCC
9941 must have believed that the containing object started (within
9942 the structure type). The value we deduce is then used (by the
9943 callers of this routine) to generate DW_AT_location and
9944 DW_AT_bit_offset attributes for fields (both bit-fields and, in
9945 the case of DW_AT_location, regular fields as well). */
9946
9947 /* Figure out the bit-distance from the start of the structure to
9948 the "deepest" bit of the bit-field. */
9949 deepest_bitpos = bitpos_int + field_size_in_bits;
9950
9951 /* This is the tricky part. Use some fancy footwork to deduce
9952 where the lowest addressed bit of the containing object must
9953 be. */
2180a0af 9954 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9955
feb5f1b1 9956 /* Round up to type_align by default. This works best for
9957 bitfields. */
71b5358c 9958 object_offset_in_bits
feb5f1b1 9959 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
9960
9961 if (object_offset_in_bits > bitpos_int)
9962 {
9963 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9964
9965 /* Round up to decl_align instead. */
9966 object_offset_in_bits
9967 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
9968 }
2180a0af 9969 }
feb5f1b1 9970 else
9971#endif
9972 object_offset_in_bits = bitpos_int;
30ade641 9973
8c3f468d 9974 return object_offset_in_bits / BITS_PER_UNIT;
30ade641 9975}
30ade641 9976\f
ec1e49cc 9977/* The following routines define various Dwarf attributes and any data
9978 associated with them. */
30ade641 9979
678d90bb 9980/* Add a location description attribute value to a DIE.
30ade641 9981
678d90bb 9982 This emits location attributes suitable for whole variables and
30ade641 9983 whole parameters. Note that the location attributes for struct fields are
9984 generated by the routine `data_member_location_attribute' below. */
ec1e49cc 9985
931e9893 9986static inline void
8ec3a57b 9987add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9988 dw_loc_descr_ref descr)
30ade641 9989{
86e12d28 9990 if (descr != 0)
9991 add_AT_loc (die, attr_kind, descr);
30ade641 9992}
9993
8c3f468d 9994/* Attach the specialized form of location attribute used for data members of
9995 struct and union types. In the special case of a FIELD_DECL node which
9996 represents a bit-field, the "offset" part of this special location
9997 descriptor must indicate the distance in bytes from the lowest-addressed
9998 byte of the containing struct or union type to the lowest-addressed byte of
9999 the "containing object" for the bit-field. (See the `field_byte_offset'
10000 function above).
10001
10002 For any given bit-field, the "containing object" is a hypothetical object
10003 (of some integral or enum type) within which the given bit-field lives. The
10004 type of this hypothetical "containing object" is always the same as the
10005 declared type of the individual bit-field itself (for GCC anyway... the
10006 DWARF spec doesn't actually mandate this). Note that it is the size (in
10007 bytes) of the hypothetical "containing object" which will be given in the
10008 DW_AT_byte_size attribute for this bit-field. (See the
10009 `byte_size_attribute' function below.) It is also used when calculating the
10010 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
10011 function below.) */
ec1e49cc 10012
30ade641 10013static void
8ec3a57b 10014add_data_member_location_attribute (dw_die_ref die, tree decl)
30ade641 10015{
3d867824 10016 HOST_WIDE_INT offset;
3e14aa38 10017 dw_loc_descr_ref loc_descr = 0;
30ade641 10018
3cb98335 10019 if (TREE_CODE (decl) == TREE_BINFO)
3e14aa38 10020 {
10021 /* We're working on the TAG_inheritance for a base class. */
57c28194 10022 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
3e14aa38 10023 {
10024 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10025 aren't at a fixed offset from all (sub)objects of the same
10026 type. We need to extract the appropriate offset from our
10027 vtable. The following dwarf expression means
10028
10029 BaseAddr = ObAddr + *((*ObAddr) - Offset)
10030
10031 This is specific to the V3 ABI, of course. */
10032
10033 dw_loc_descr_ref tmp;
8c3f468d 10034
3e14aa38 10035 /* Make a copy of the object address. */
10036 tmp = new_loc_descr (DW_OP_dup, 0, 0);
10037 add_loc_descr (&loc_descr, tmp);
8c3f468d 10038
3e14aa38 10039 /* Extract the vtable address. */
10040 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10041 add_loc_descr (&loc_descr, tmp);
8c3f468d 10042
3e14aa38 10043 /* Calculate the address of the offset. */
10044 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
7bd4f6b6 10045 gcc_assert (offset < 0);
8c3f468d 10046
3e14aa38 10047 tmp = int_loc_descriptor (-offset);
10048 add_loc_descr (&loc_descr, tmp);
10049 tmp = new_loc_descr (DW_OP_minus, 0, 0);
10050 add_loc_descr (&loc_descr, tmp);
8c3f468d 10051
3e14aa38 10052 /* Extract the offset. */
10053 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10054 add_loc_descr (&loc_descr, tmp);
8c3f468d 10055
3e14aa38 10056 /* Add it to the object address. */
10057 tmp = new_loc_descr (DW_OP_plus, 0, 0);
10058 add_loc_descr (&loc_descr, tmp);
10059 }
10060 else
10061 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10062 }
404ba76d 10063 else
10064 offset = field_byte_offset (decl);
10065
3e14aa38 10066 if (! loc_descr)
10067 {
10068 enum dwarf_location_atom op;
10069
8c3f468d 10070 /* The DWARF2 standard says that we should assume that the structure
10071 address is already on the stack, so we can specify a structure field
10072 address by using DW_OP_plus_uconst. */
ec1e49cc 10073
30ade641 10074#ifdef MIPS_DEBUGGING_INFO
8c3f468d 10075 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10076 operator correctly. It works only if we leave the offset on the
10077 stack. */
3e14aa38 10078 op = DW_OP_constu;
30ade641 10079#else
3e14aa38 10080 op = DW_OP_plus_uconst;
30ade641 10081#endif
ec1e49cc 10082
3e14aa38 10083 loc_descr = new_loc_descr (op, offset, 0);
10084 }
8c3f468d 10085
30ade641 10086 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10087}
10088
1b6ad376 10089/* Writes integer values to dw_vec_const array. */
10090
10091static void
10092insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10093{
10094 while (size != 0)
10095 {
10096 *dest++ = val & 0xff;
10097 val >>= 8;
10098 --size;
10099 }
10100}
10101
10102/* Reads integers from dw_vec_const array. Inverse of insert_int. */
10103
10104static HOST_WIDE_INT
10105extract_int (const unsigned char *src, unsigned int size)
10106{
10107 HOST_WIDE_INT val = 0;
10108
10109 src += size;
10110 while (size != 0)
10111 {
10112 val <<= 8;
10113 val |= *--src & 0xff;
10114 --size;
10115 }
10116 return val;
10117}
10118
10119/* Writes floating point values to dw_vec_const array. */
10120
10121static void
10122insert_float (rtx rtl, unsigned char *array)
10123{
10124 REAL_VALUE_TYPE rv;
10125 long val[4];
10126 int i;
10127
10128 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10129 real_to_target (val, &rv, GET_MODE (rtl));
10130
10131 /* real_to_target puts 32-bit pieces in each long. Pack them. */
10132 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10133 {
10134 insert_int (val[i], 4, array);
10135 array += 4;
10136 }
10137}
10138
df07c3ae 10139/* Attach a DW_AT_const_value attribute for a variable or a parameter which
30ade641 10140 does not have a "location" either in memory or in a register. These
10141 things can arise in GNU C when a constant is passed as an actual parameter
10142 to an inlined function. They can also arise in C++ where declared
10143 constants do not necessarily get memory "homes". */
ec1e49cc 10144
30ade641 10145static void
8ec3a57b 10146add_const_value_attribute (dw_die_ref die, rtx rtl)
30ade641 10147{
10148 switch (GET_CODE (rtl))
10149 {
10150 case CONST_INT:
ca98eb0a 10151 {
10152 HOST_WIDE_INT val = INTVAL (rtl);
bc70bd5e 10153
3d867824 10154 if (val < 0)
10155 add_AT_int (die, DW_AT_const_value, val);
8ff30ff6 10156 else
3d867824 10157 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
ca98eb0a 10158 }
30ade641 10159 break;
10160
10161 case CONST_DOUBLE:
10162 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
c83a163c 10163 floating-point constant. A CONST_DOUBLE is used whenever the
10164 constant requires more than one word in order to be adequately
10165 represented. We output CONST_DOUBLEs as blocks. */
df78b73b 10166 {
19cb6b50 10167 enum machine_mode mode = GET_MODE (rtl);
df78b73b 10168
cee7491d 10169 if (SCALAR_FLOAT_MODE_P (mode))
df78b73b 10170 {
1b6ad376 10171 unsigned int length = GET_MODE_SIZE (mode);
10172 unsigned char *array = ggc_alloc (length);
df78b73b 10173
1b6ad376 10174 insert_float (rtl, array);
10175 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
df78b73b 10176 }
10177 else
ca98eb0a 10178 {
10179 /* ??? We really should be using HOST_WIDE_INT throughout. */
7bd4f6b6 10180 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
8c3f468d 10181
ca98eb0a 10182 add_AT_long_long (die, DW_AT_const_value,
10183 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10184 }
df78b73b 10185 }
30ade641 10186 break;
10187
1b6ad376 10188 case CONST_VECTOR:
10189 {
10190 enum machine_mode mode = GET_MODE (rtl);
10191 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10192 unsigned int length = CONST_VECTOR_NUNITS (rtl);
10193 unsigned char *array = ggc_alloc (length * elt_size);
10194 unsigned int i;
10195 unsigned char *p;
10196
7bd4f6b6 10197 switch (GET_MODE_CLASS (mode))
1b6ad376 10198 {
7bd4f6b6 10199 case MODE_VECTOR_INT:
1b6ad376 10200 for (i = 0, p = array; i < length; i++, p += elt_size)
10201 {
10202 rtx elt = CONST_VECTOR_ELT (rtl, i);
10203 HOST_WIDE_INT lo, hi;
8ff30ff6 10204
7bd4f6b6 10205 switch (GET_CODE (elt))
1b6ad376 10206 {
7bd4f6b6 10207 case CONST_INT:
1b6ad376 10208 lo = INTVAL (elt);
10209 hi = -(lo < 0);
7bd4f6b6 10210 break;
8ff30ff6 10211
7bd4f6b6 10212 case CONST_DOUBLE:
1b6ad376 10213 lo = CONST_DOUBLE_LOW (elt);
10214 hi = CONST_DOUBLE_HIGH (elt);
7bd4f6b6 10215 break;
8ff30ff6 10216
7bd4f6b6 10217 default:
10218 gcc_unreachable ();
1b6ad376 10219 }
8ff30ff6 10220
1b6ad376 10221 if (elt_size <= sizeof (HOST_WIDE_INT))
10222 insert_int (lo, elt_size, p);
7bd4f6b6 10223 else
1b6ad376 10224 {
10225 unsigned char *p0 = p;
10226 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
8ff30ff6 10227
7bd4f6b6 10228 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
1b6ad376 10229 if (WORDS_BIG_ENDIAN)
10230 {
10231 p0 = p1;
10232 p1 = p;
10233 }
10234 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10235 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10236 }
1b6ad376 10237 }
7bd4f6b6 10238 break;
10239
10240 case MODE_VECTOR_FLOAT:
1b6ad376 10241 for (i = 0, p = array; i < length; i++, p += elt_size)
10242 {
10243 rtx elt = CONST_VECTOR_ELT (rtl, i);
10244 insert_float (elt, p);
10245 }
7bd4f6b6 10246 break;
10247
10248 default:
10249 gcc_unreachable ();
1b6ad376 10250 }
1b6ad376 10251
10252 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10253 }
10254 break;
10255
30ade641 10256 case CONST_STRING:
10257 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10258 break;
10259
10260 case SYMBOL_REF:
10261 case LABEL_REF:
10262 case CONST:
7facaa35 10263 add_AT_addr (die, DW_AT_const_value, rtl);
62aedc4c 10264 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
30ade641 10265 break;
10266
10267 case PLUS:
10268 /* In cases where an inlined instance of an inline function is passed
c83a163c 10269 the address of an `auto' variable (which is local to the caller) we
10270 can get a situation where the DECL_RTL of the artificial local
10271 variable (for the inlining) which acts as a stand-in for the
10272 corresponding formal parameter (of the inline function) will look
10273 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
10274 exactly a compile-time constant expression, but it isn't the address
10275 of the (artificial) local variable either. Rather, it represents the
10276 *value* which the artificial local variable always has during its
10277 lifetime. We currently have no way to represent such quasi-constant
10278 values in Dwarf, so for now we just punt and generate nothing. */
30ade641 10279 break;
10280
10281 default:
10282 /* No other kinds of rtx should be possible here. */
7bd4f6b6 10283 gcc_unreachable ();
30ade641 10284 }
10285
10286}
10287
e124d6c7 10288/* Determine whether the evaluation of EXPR references any variables
10289 or functions which aren't otherwise used (and therefore may not be
10290 output). */
10291static tree
10292reference_to_unused (tree * tp, int * walk_subtrees,
10293 void * data ATTRIBUTE_UNUSED)
10294{
35cc02b5 10295 if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
e124d6c7 10296 *walk_subtrees = 0;
61a9389f 10297
e124d6c7 10298 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10299 && ! TREE_ASM_WRITTEN (*tp))
10300 return *tp;
5615be0f 10301 else if (!flag_unit_at_a_time)
10302 return NULL_TREE;
10303 else if (!cgraph_global_info_ready
10304 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10305 gcc_unreachable ();
10306 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
56e902bd 10307 {
10308 struct varpool_node *node = varpool_node (*tp);
10309 if (!node->needed)
10310 return *tp;
10311 }
5615be0f 10312 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10313 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10314 {
10315 struct cgraph_node *node = cgraph_node (*tp);
10316 if (!node->output)
61a9389f 10317 return *tp;
5615be0f 10318 }
56e902bd 10319
10320 return NULL_TREE;
e124d6c7 10321}
10322
9293d8bd 10323/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10324 for use in a later add_const_value_attribute call. */
10325
10326static rtx
10327rtl_for_decl_init (tree init, tree type)
10328{
10329 rtx rtl = NULL_RTX;
10330
10331 /* If a variable is initialized with a string constant without embedded
10332 zeros, build CONST_STRING. */
10333 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10334 {
10335 tree enttype = TREE_TYPE (type);
10336 tree domain = TYPE_DOMAIN (type);
10337 enum machine_mode mode = TYPE_MODE (enttype);
10338
10339 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10340 && domain
10341 && integer_zerop (TYPE_MIN_VALUE (domain))
10342 && compare_tree_int (TYPE_MAX_VALUE (domain),
10343 TREE_STRING_LENGTH (init) - 1) == 0
10344 && ((size_t) TREE_STRING_LENGTH (init)
10345 == strlen (TREE_STRING_POINTER (init)) + 1))
10346 rtl = gen_rtx_CONST_STRING (VOIDmode,
10347 ggc_strdup (TREE_STRING_POINTER (init)));
10348 }
bf591863 10349 /* Other aggregates, and complex values, could be represented using
10350 CONCAT: FIXME! */
10351 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10352 ;
61a9389f 10353 /* Vectors only work if their mode is supported by the target.
bf591863 10354 FIXME: generic vectors ought to work too. */
10355 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
e124d6c7 10356 ;
9293d8bd 10357 /* If the initializer is something that we know will expand into an
e124d6c7 10358 immediate RTL constant, expand it now. We must be careful not to
10359 reference variables which won't be output. */
10360 else if (initializer_constant_valid_p (init, type)
10361 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
9293d8bd 10362 {
10363 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10364
10365 /* If expand_expr returns a MEM, it wasn't immediate. */
10366 gcc_assert (!rtl || !MEM_P (rtl));
10367 }
10368
10369 return rtl;
10370}
10371
10372/* Generate RTL for the variable DECL to represent its location. */
10373
9ed904da 10374static rtx
8ec3a57b 10375rtl_for_decl_location (tree decl)
30ade641 10376{
19cb6b50 10377 rtx rtl;
ec1e49cc 10378
30ade641 10379 /* Here we have to decide where we are going to say the parameter "lives"
10380 (as far as the debugger is concerned). We only have a couple of
10381 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
ec1e49cc 10382
f80d1bcd 10383 DECL_RTL normally indicates where the parameter lives during most of the
ec1e49cc 10384 activation of the function. If optimization is enabled however, this
f80d1bcd 10385 could be either NULL or else a pseudo-reg. Both of those cases indicate
30ade641 10386 that the parameter doesn't really live anywhere (as far as the code
10387 generation parts of GCC are concerned) during most of the function's
10388 activation. That will happen (for example) if the parameter is never
ec1e49cc 10389 referenced within the function.
10390
10391 We could just generate a location descriptor here for all non-NULL
10392 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10393 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10394 where DECL_RTL is NULL or is a pseudo-reg.
10395
10396 Note however that we can only get away with using DECL_INCOMING_RTL as
10397 a backup substitute for DECL_RTL in certain limited cases. In cases
10398 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10399 we can be sure that the parameter was passed using the same type as it is
10400 declared to have within the function, and that its DECL_INCOMING_RTL
10401 points us to a place where a value of that type is passed.
10402
10403 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10404 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10405 because in these cases DECL_INCOMING_RTL points us to a value of some
10406 type which is *different* from the type of the parameter itself. Thus,
10407 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10408 such cases, the debugger would end up (for example) trying to fetch a
10409 `float' from a place which actually contains the first part of a
10410 `double'. That would lead to really incorrect and confusing
10411 output at debug-time.
10412
10413 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10414 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10415 are a couple of exceptions however. On little-endian machines we can
10416 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10417 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10418 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10419 when (on a little-endian machine) a non-prototyped function has a
10420 parameter declared to be of type `short' or `char'. In such cases,
10421 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10422 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10423 passed `int' value. If the debugger then uses that address to fetch
10424 a `short' or a `char' (on a little-endian machine) the result will be
10425 the correct data, so we allow for such exceptional cases below.
10426
10427 Note that our goal here is to describe the place where the given formal
8c3f468d 10428 parameter lives during most of the function's activation (i.e. between the
10429 end of the prologue and the start of the epilogue). We'll do that as best
10430 as we can. Note however that if the given formal parameter is modified
10431 sometime during the execution of the function, then a stack backtrace (at
10432 debug-time) will show the function as having been called with the *new*
10433 value rather than the value which was originally passed in. This happens
10434 rarely enough that it is not a major problem, but it *is* a problem, and
10435 I'd like to fix it.
10436
10437 A future version of dwarf2out.c may generate two additional attributes for
10438 any given DW_TAG_formal_parameter DIE which will describe the "passed
10439 type" and the "passed location" for the given formal parameter in addition
10440 to the attributes we now generate to indicate the "declared type" and the
10441 "active location" for each parameter. This additional set of attributes
10442 could be used by debuggers for stack backtraces. Separately, note that
10443 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10444 This happens (for example) for inlined-instances of inline function formal
10445 parameters which are never referenced. This really shouldn't be
10446 happening. All PARM_DECL nodes should get valid non-NULL
4ee9c684 10447 DECL_INCOMING_RTL values. FIXME. */
30ade641 10448
10449 /* Use DECL_RTL as the "location" unless we find something better. */
ff12286a 10450 rtl = DECL_RTL_IF_SET (decl);
30ade641 10451
f3546830 10452 /* When generating abstract instances, ignore everything except
3332aee2 10453 constants, symbols living in memory, and symbols living in
10454 fixed registers. */
f3546830 10455 if (! reload_completed)
10456 {
10457 if (rtl
10458 && (CONSTANT_P (rtl)
e16ceb8e 10459 || (MEM_P (rtl)
3332aee2 10460 && CONSTANT_P (XEXP (rtl, 0)))
8ad4c111 10461 || (REG_P (rtl)
3332aee2 10462 && TREE_CODE (decl) == VAR_DECL
10463 && TREE_STATIC (decl))))
e93986bb 10464 {
883b2e73 10465 rtl = targetm.delegitimize_address (rtl);
e93986bb 10466 return rtl;
10467 }
f3546830 10468 rtl = NULL_RTX;
10469 }
10470 else if (TREE_CODE (decl) == PARM_DECL)
30ade641 10471 {
10472 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10473 {
0189d7ee 10474 tree declared_type = TREE_TYPE (decl);
10475 tree passed_type = DECL_ARG_TYPE (decl);
10476 enum machine_mode dmode = TYPE_MODE (declared_type);
10477 enum machine_mode pmode = TYPE_MODE (passed_type);
30ade641 10478
ec1e49cc 10479 /* This decl represents a formal parameter which was optimized out.
30ade641 10480 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
8c3f468d 10481 all cases where (rtl == NULL_RTX) just below. */
0189d7ee 10482 if (dmode == pmode)
f80d1bcd 10483 rtl = DECL_INCOMING_RTL (decl);
0189d7ee 10484 else if (SCALAR_INT_MODE_P (dmode)
a4cb69f8 10485 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10486 && DECL_INCOMING_RTL (decl))
0189d7ee 10487 {
10488 rtx inc = DECL_INCOMING_RTL (decl);
10489 if (REG_P (inc))
10490 rtl = inc;
10491 else if (MEM_P (inc))
10492 {
10493 if (BYTES_BIG_ENDIAN)
10494 rtl = adjust_address_nv (inc, dmode,
10495 GET_MODE_SIZE (pmode)
10496 - GET_MODE_SIZE (dmode));
10497 else
10498 rtl = inc;
10499 }
10500 }
30ade641 10501 }
80291b9e 10502
10503 /* If the parm was passed in registers, but lives on the stack, then
10504 make a big endian correction if the mode of the type of the
10505 parameter is not the same as the mode of the rtl. */
10506 /* ??? This is the same series of checks that are made in dbxout.c before
10507 we reach the big endian correction code there. It isn't clear if all
10508 of these checks are necessary here, but keeping them all is the safe
10509 thing to do. */
e16ceb8e 10510 else if (MEM_P (rtl)
80291b9e 10511 && XEXP (rtl, 0) != const0_rtx
10512 && ! CONSTANT_P (XEXP (rtl, 0))
10513 /* Not passed in memory. */
e16ceb8e 10514 && !MEM_P (DECL_INCOMING_RTL (decl))
80291b9e 10515 /* Not passed by invisible reference. */
8ad4c111 10516 && (!REG_P (XEXP (rtl, 0))
80291b9e 10517 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10518 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10519#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10520 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10521#endif
10522 )
10523 /* Big endian correction check. */
10524 && BYTES_BIG_ENDIAN
10525 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10526 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10527 < UNITS_PER_WORD))
10528 {
10529 int offset = (UNITS_PER_WORD
10530 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
8c3f468d 10531
80291b9e 10532 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10533 plus_constant (XEXP (rtl, 0), offset));
10534 }
30ade641 10535 }
13906b02 10536 else if (TREE_CODE (decl) == VAR_DECL
c7c9d0ca 10537 && rtl
e16ceb8e 10538 && MEM_P (rtl)
13906b02 10539 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10540 && BYTES_BIG_ENDIAN)
10541 {
10542 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10543 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10544
10545 /* If a variable is declared "register" yet is smaller than
10546 a register, then if we store the variable to memory, it
10547 looks like we're storing a register-sized value, when in
10548 fact we are not. We need to adjust the offset of the
10549 storage location to reflect the actual value's bytes,
10550 else gdb will not be able to display it. */
10551 if (rsize > dsize)
10552 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10553 plus_constant (XEXP (rtl, 0), rsize-dsize));
10554 }
ec1e49cc 10555
8c3f468d 10556 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10557 and will have been substituted directly into all expressions that use it.
10558 C does not have such a concept, but C++ and other languages do. */
12d886b8 10559 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9293d8bd 10560 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
2fbd3b4c 10561
e93986bb 10562 if (rtl)
883b2e73 10563 rtl = targetm.delegitimize_address (rtl);
931e9893 10564
10565 /* If we don't look past the constant pool, we risk emitting a
10566 reference to a constant pool entry that isn't referenced from
10567 code, and thus is not emitted. */
10568 if (rtl)
10569 rtl = avoid_constant_pool_reference (rtl);
10570
9ed904da 10571 return rtl;
10572}
10573
12d886b8 10574/* We need to figure out what section we should use as the base for the
10575 address ranges where a given location is valid.
10576 1. If this particular DECL has a section associated with it, use that.
10577 2. If this function has a section associated with it, use that.
10578 3. Otherwise, use the text section.
10579 XXX: If you split a variable across multiple sections, we won't notice. */
10580
10581static const char *
10582secname_for_decl (tree decl)
10583{
10584 const char *secname;
10585
10586 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10587 {
10588 tree sectree = DECL_SECTION_NAME (decl);
10589 secname = TREE_STRING_POINTER (sectree);
10590 }
10591 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10592 {
10593 tree sectree = DECL_SECTION_NAME (current_function_decl);
10594 secname = TREE_STRING_POINTER (sectree);
10595 }
5fbee89d 10596 else if (cfun && in_cold_section_p)
12d886b8 10597 secname = cfun->cold_section_label;
10598 else
10599 secname = text_section_label;
10600
10601 return secname;
10602}
10603
df07c3ae 10604/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9ed904da 10605 data attribute for a variable or a parameter. We generate the
10606 DW_AT_const_value attribute only in those cases where the given variable
10607 or parameter does not have a true "location" either in memory or in a
10608 register. This can happen (for example) when a constant is passed as an
10609 actual argument in a call to an inline function. (It's possible that
10610 these things can crop up in other ways also.) Note that one type of
10611 constant value which can be passed into an inlined function is a constant
10612 pointer. This can happen for example if an actual argument in an inlined
10613 function call evaluates to a compile-time constant address. */
10614
10615static void
b2025850 10616add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10617 enum dwarf_attribute attr)
9ed904da 10618{
19cb6b50 10619 rtx rtl;
931e9893 10620 dw_loc_descr_ref descr;
b2025850 10621 var_loc_list *loc_list;
6ad1968a 10622 struct var_loc_node *node;
9ed904da 10623 if (TREE_CODE (decl) == ERROR_MARK)
10624 return;
7bd4f6b6 10625
10626 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10627 || TREE_CODE (decl) == RESULT_DECL);
61a9389f 10628
b2025850 10629 /* See if we possibly have multiple locations for this variable. */
10630 loc_list = lookup_decl_loc (decl);
10631
10632 /* If it truly has multiple locations, the first and last node will
10633 differ. */
10634 if (loc_list && loc_list->first != loc_list->last)
10635 {
12d886b8 10636 const char *endname, *secname;
b2025850 10637 dw_loc_list_ref list;
10638 rtx varloc;
d53bb226 10639 enum var_init_status initialized;
6ad1968a 10640
b2025850 10641 /* Now that we know what section we are using for a base,
61a9389f 10642 actually construct the list of locations.
b2025850 10643 The first location information is what is passed to the
10644 function that creates the location list, and the remaining
10645 locations just get added on to that list.
10646 Note that we only know the start address for a location
10647 (IE location changes), so to build the range, we use
10648 the range [current location start, next location start].
10649 This means we have to special case the last node, and generate
10650 a range of [last location start, end of function label]. */
10651
10652 node = loc_list->first;
10653 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12d886b8 10654 secname = secname_for_decl (decl);
10655
d53bb226 10656 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10657 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10658 else
10659 initialized = VAR_INIT_STATUS_INITIALIZED;
10660
10661 list = new_loc_list (loc_descriptor (varloc, initialized),
b2025850 10662 node->label, node->next->label, secname, 1);
10663 node = node->next;
10664
10665 for (; node->next; node = node->next)
10666 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10667 {
10668 /* The variable has a location between NODE->LABEL and
10669 NODE->NEXT->LABEL. */
d53bb226 10670 enum var_init_status initialized =
10671 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
b2025850 10672 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
d53bb226 10673 add_loc_descr_to_loc_list (&list,
10674 loc_descriptor (varloc, initialized),
b2025850 10675 node->label, node->next->label, secname);
10676 }
10677
10678 /* If the variable has a location at the last label
10679 it keeps its location until the end of function. */
10680 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10681 {
10682 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
d53bb226 10683 enum var_init_status initialized =
10684 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
b2025850 10685
10686 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10687 if (!current_function_decl)
10688 endname = text_end_label;
10689 else
10690 {
10691 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10692 current_function_funcdef_no);
10693 endname = ggc_strdup (label_id);
10694 }
d53bb226 10695 add_loc_descr_to_loc_list (&list,
10696 loc_descriptor (varloc, initialized),
b2025850 10697 node->label, endname, secname);
10698 }
10699
10700 /* Finally, add the location list to the DIE, and we are done. */
10701 add_AT_loc_list (die, attr, list);
10702 return;
10703 }
10704
6ad1968a 10705 /* Try to get some constant RTL for this decl, and use that as the value of
10706 the location. */
61a9389f 10707
9ed904da 10708 rtl = rtl_for_decl_location (decl);
afcf285e 10709 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
30ade641 10710 {
30ade641 10711 add_const_value_attribute (die, rtl);
afcf285e 10712 return;
30ade641 10713 }
61a9389f 10714
600b9bbf 10715 /* If we have tried to generate the location otherwise, and it
6ad1968a 10716 didn't work out (we wouldn't be here if we did), and we have a one entry
10717 location list, try generating a location from that. */
10718 if (loc_list && loc_list->first)
10719 {
d53bb226 10720 enum var_init_status status;
6ad1968a 10721 node = loc_list->first;
d53bb226 10722 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10723 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
6ad1968a 10724 if (descr)
600b9bbf 10725 {
10726 add_AT_location_description (die, attr, descr);
10727 return;
10728 }
10729 }
10730
10731 /* We couldn't get any rtl, so try directly generating the location
10732 description from the tree. */
10733 descr = loc_descriptor_from_tree (decl);
10734 if (descr)
10735 {
10736 add_AT_location_description (die, attr, descr);
10737 return;
6ad1968a 10738 }
e124d6c7 10739 /* None of that worked, so it must not really have a location;
10740 try adding a constant value attribute from the DECL_INITIAL. */
10741 tree_add_const_value_attribute (die, decl);
30ade641 10742}
10743
eabb26f3 10744/* If we don't have a copy of this variable in memory for some reason (such
10745 as a C++ member constant that doesn't have an out-of-line definition),
10746 we should tell the debugger about the constant value. */
10747
10748static void
8ec3a57b 10749tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
eabb26f3 10750{
10751 tree init = DECL_INITIAL (decl);
10752 tree type = TREE_TYPE (decl);
9293d8bd 10753 rtx rtl;
eabb26f3 10754
9293d8bd 10755 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
eabb26f3 10756 /* OK */;
10757 else
10758 return;
10759
9293d8bd 10760 rtl = rtl_for_decl_init (init, type);
10761 if (rtl)
10762 add_const_value_attribute (var_die, rtl);
eabb26f3 10763}
ac02093f 10764
89fa767a 10765/* Convert the CFI instructions for the current function into a
10766 location list. This is used for DW_AT_frame_base when we targeting
10767 a dwarf2 consumer that does not support the dwarf3
10768 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10769 expressions. */
12d886b8 10770
10771static dw_loc_list_ref
89fa767a 10772convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12d886b8 10773{
10774 dw_fde_ref fde;
10775 dw_loc_list_ref list, *list_tail;
10776 dw_cfi_ref cfi;
10777 dw_cfa_location last_cfa, next_cfa;
10778 const char *start_label, *last_label, *section;
10779
10780 fde = &fde_table[fde_table_in_use - 1];
10781
10782 section = secname_for_decl (current_function_decl);
10783 list_tail = &list;
10784 list = NULL;
10785
10786 next_cfa.reg = INVALID_REGNUM;
10787 next_cfa.offset = 0;
10788 next_cfa.indirect = 0;
10789 next_cfa.base_offset = 0;
10790
10791 start_label = fde->dw_fde_begin;
10792
10793 /* ??? Bald assumption that the CIE opcode list does not contain
10794 advance opcodes. */
10795 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10796 lookup_cfa_1 (cfi, &next_cfa);
10797
10798 last_cfa = next_cfa;
10799 last_label = start_label;
10800
10801 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10802 switch (cfi->dw_cfi_opc)
10803 {
bea04f76 10804 case DW_CFA_set_loc:
12d886b8 10805 case DW_CFA_advance_loc1:
10806 case DW_CFA_advance_loc2:
10807 case DW_CFA_advance_loc4:
10808 if (!cfa_equal_p (&last_cfa, &next_cfa))
10809 {
89fa767a 10810 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10811 start_label, last_label, section,
10812 list == NULL);
12d886b8 10813
10814 list_tail = &(*list_tail)->dw_loc_next;
10815 last_cfa = next_cfa;
10816 start_label = last_label;
10817 }
10818 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10819 break;
10820
10821 case DW_CFA_advance_loc:
10822 /* The encoding is complex enough that we should never emit this. */
10823 case DW_CFA_remember_state:
10824 case DW_CFA_restore_state:
10825 /* We don't handle these two in this function. It would be possible
10826 if it were to be required. */
10827 gcc_unreachable ();
10828
10829 default:
10830 lookup_cfa_1 (cfi, &next_cfa);
10831 break;
10832 }
10833
10834 if (!cfa_equal_p (&last_cfa, &next_cfa))
10835 {
89fa767a 10836 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10837 start_label, last_label, section,
10838 list == NULL);
12d886b8 10839 list_tail = &(*list_tail)->dw_loc_next;
10840 start_label = last_label;
10841 }
89fa767a 10842 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10843 start_label, fde->dw_fde_end, section,
10844 list == NULL);
12d886b8 10845
10846 return list;
10847}
10848
89fa767a 10849/* Compute a displacement from the "steady-state frame pointer" to the
10850 frame base (often the same as the CFA), and store it in
10851 frame_pointer_fb_offset. OFFSET is added to the displacement
10852 before the latter is negated. */
12d886b8 10853
10854static void
89fa767a 10855compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12d886b8 10856{
da72c083 10857 rtx reg, elim;
10858
10859#ifdef FRAME_POINTER_CFA_OFFSET
10860 reg = frame_pointer_rtx;
89fa767a 10861 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
da72c083 10862#else
10863 reg = arg_pointer_rtx;
89fa767a 10864 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
da72c083 10865#endif
12d886b8 10866
da72c083 10867 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10868 if (GET_CODE (elim) == PLUS)
10869 {
10870 offset += INTVAL (XEXP (elim, 1));
10871 elim = XEXP (elim, 0);
10872 }
10873 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10874 : stack_pointer_rtx));
12d886b8 10875
89fa767a 10876 frame_pointer_fb_offset = -offset;
12d886b8 10877}
10878
df07c3ae 10879/* Generate a DW_AT_name attribute given some string value to be included as
30ade641 10880 the value of the attribute. */
ec1e49cc 10881
ff279357 10882static void
8ec3a57b 10883add_name_attribute (dw_die_ref die, const char *name_string)
30ade641 10884{
ec1e49cc 10885 if (name_string != NULL && *name_string != 0)
155b05dc 10886 {
10887 if (demangle_name_func)
10888 name_string = (*demangle_name_func) (name_string);
10889
10890 add_AT_string (die, DW_AT_name, name_string);
10891 }
30ade641 10892}
10893
df07c3ae 10894/* Generate a DW_AT_comp_dir attribute for DIE. */
ff279357 10895
10896static void
8ec3a57b 10897add_comp_dir_attribute (dw_die_ref die)
ff279357 10898{
e7aa92b2 10899 const char *wd = get_src_pwd ();
ff279357 10900 if (wd != NULL)
10901 add_AT_string (die, DW_AT_comp_dir, wd);
10902}
10903
30ade641 10904/* Given a tree node describing an array bound (either lower or upper) output
b58d53bf 10905 a representation for that bound. */
ec1e49cc 10906
30ade641 10907static void
8ec3a57b 10908add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
30ade641 10909{
30ade641 10910 switch (TREE_CODE (bound))
10911 {
10912 case ERROR_MARK:
10913 return;
10914
04641143 10915 /* All fixed-bounds are represented by INTEGER_CST nodes. */
30ade641 10916 case INTEGER_CST:
5d844ba2 10917 if (! host_integerp (bound, 0)
10918 || (bound_attr == DW_AT_lower_bound
af4d39d8 10919 && (((is_c_family () || is_java ()) && integer_zerop (bound))
5d844ba2 10920 || (is_fortran () && integer_onep (bound)))))
aab2cf92 10921 /* Use the default. */
5d844ba2 10922 ;
0defae70 10923 else
5d844ba2 10924 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
30ade641 10925 break;
10926
34425fdc 10927 case CONVERT_EXPR:
30ade641 10928 case NOP_EXPR:
34425fdc 10929 case NON_LVALUE_EXPR:
f96c43fb 10930 case VIEW_CONVERT_EXPR:
34425fdc 10931 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10932 break;
f80d1bcd 10933
30ade641 10934 case SAVE_EXPR:
30ade641 10935 break;
8a8bfbe7 10936
678d90bb 10937 case VAR_DECL:
9ed904da 10938 case PARM_DECL:
4ee9c684 10939 case RESULT_DECL:
9ed904da 10940 {
10941 dw_die_ref decl_die = lookup_decl_die (bound);
10942
10943 /* ??? Can this happen, or should the variable have been bound
10944 first? Probably it can, since I imagine that we try to create
10945 the types of parameters in the order in which they exist in
ac02093f 10946 the list, and won't have created a forward reference to a
9ed904da 10947 later parameter. */
10948 if (decl_die != NULL)
10949 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10950 break;
10951 }
678d90bb 10952
8a8bfbe7 10953 default:
9ed904da 10954 {
10955 /* Otherwise try to create a stack operation procedure to
10956 evaluate the value of the array bound. */
10957
10958 dw_die_ref ctx, decl_die;
10959 dw_loc_descr_ref loc;
10960
afcf285e 10961 loc = loc_descriptor_from_tree (bound);
9ed904da 10962 if (loc == NULL)
10963 break;
10964
86e12d28 10965 if (current_function_decl == 0)
10966 ctx = comp_unit_die;
10967 else
10968 ctx = lookup_decl_die (current_function_decl);
9ed904da 10969
15cfae4e 10970 decl_die = new_die (DW_TAG_variable, ctx, bound);
9ed904da 10971 add_AT_flag (decl_die, DW_AT_artificial, 1);
10972 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10973 add_AT_loc (decl_die, DW_AT_location, loc);
10974
10975 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10976 break;
10977 }
30ade641 10978 }
10979}
10980
10981/* Note that the block of subscript information for an array type also
10982 includes information about the element type of type given array type. */
ec1e49cc 10983
30ade641 10984static void
8ec3a57b 10985add_subscript_info (dw_die_ref type_die, tree type)
30ade641 10986{
73439ee0 10987#ifndef MIPS_DEBUGGING_INFO
19cb6b50 10988 unsigned dimension_number;
73439ee0 10989#endif
19cb6b50 10990 tree lower, upper;
10991 dw_die_ref subrange_die;
30ade641 10992
f80d1bcd 10993 /* The GNU compilers represent multidimensional array types as sequences of
30ade641 10994 one dimensional array types whose element types are themselves array
10995 types. Here we squish that down, so that each multidimensional array
f80d1bcd 10996 type gets only one array_type DIE in the Dwarf debugging info. The draft
30ade641 10997 Dwarf specification say that we are allowed to do this kind of
10998 compression in C (because there is no difference between an array or
f80d1bcd 10999 arrays and a multidimensional array in C) but for other source languages
30ade641 11000 (e.g. Ada) we probably shouldn't do this. */
ec1e49cc 11001
30ade641 11002 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11003 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11004 We work around this by disabling this feature. See also
11005 gen_array_type_die. */
11006#ifndef MIPS_DEBUGGING_INFO
11007 for (dimension_number = 0;
11008 TREE_CODE (type) == ARRAY_TYPE;
11009 type = TREE_TYPE (type), dimension_number++)
30ade641 11010#endif
8c3f468d 11011 {
19cb6b50 11012 tree domain = TYPE_DOMAIN (type);
30ade641 11013
11014 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
f80d1bcd 11015 and (in GNU C only) variable bounds. Handle all three forms
c83a163c 11016 here. */
15cfae4e 11017 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
30ade641 11018 if (domain)
11019 {
11020 /* We have an array type with specified bounds. */
11021 lower = TYPE_MIN_VALUE (domain);
11022 upper = TYPE_MAX_VALUE (domain);
11023
139c3f48 11024 /* Define the index type. */
5b67860b 11025 if (TREE_TYPE (domain))
678d90bb 11026 {
11027 /* ??? This is probably an Ada unnamed subrange type. Ignore the
11028 TREE_TYPE field. We can't emit debug info for this
11029 because it is an unnamed integral type. */
11030 if (TREE_CODE (domain) == INTEGER_TYPE
11031 && TYPE_NAME (domain) == NULL_TREE
11032 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11033 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
f80d1bcd 11034 ;
678d90bb 11035 else
11036 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11037 type_die);
11038 }
5b67860b 11039
f52483b5 11040 /* ??? If upper is NULL, the array has unspecified length,
11041 but it does have a lower bound. This happens with Fortran
11042 dimension arr(N:*)
8ec3a57b 11043 Since the debugger is definitely going to need to know N
f52483b5 11044 to produce useful results, go ahead and output the lower
11045 bound solo, and hope the debugger can cope. */
11046
0defae70 11047 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
f52483b5 11048 if (upper)
11049 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
30ade641 11050 }
ec1e49cc 11051
8c3f468d 11052 /* Otherwise we have an array type with an unspecified length. The
11053 DWARF-2 spec does not say how to handle this; let's just leave out the
11054 bounds. */
30ade641 11055 }
30ade641 11056}
11057
11058static void
8ec3a57b 11059add_byte_size_attribute (dw_die_ref die, tree tree_node)
30ade641 11060{
19cb6b50 11061 unsigned size;
30ade641 11062
11063 switch (TREE_CODE (tree_node))
11064 {
11065 case ERROR_MARK:
11066 size = 0;
11067 break;
11068 case ENUMERAL_TYPE:
11069 case RECORD_TYPE:
11070 case UNION_TYPE:
11071 case QUAL_UNION_TYPE:
11072 size = int_size_in_bytes (tree_node);
11073 break;
11074 case FIELD_DECL:
11075 /* For a data member of a struct or union, the DW_AT_byte_size is
c83a163c 11076 generally given as the number of bytes normally allocated for an
11077 object of the *declared* type of the member itself. This is true
11078 even for bit-fields. */
30ade641 11079 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11080 break;
11081 default:
7bd4f6b6 11082 gcc_unreachable ();
30ade641 11083 }
11084
11085 /* Note that `size' might be -1 when we get to this point. If it is, that
11086 indicates that the byte size of the entity in question is variable. We
11087 have no good way of expressing this fact in Dwarf at the present time,
11088 so just let the -1 pass on through. */
30ade641 11089 add_AT_unsigned (die, DW_AT_byte_size, size);
11090}
11091
11092/* For a FIELD_DECL node which represents a bit-field, output an attribute
11093 which specifies the distance in bits from the highest order bit of the
11094 "containing object" for the bit-field to the highest order bit of the
11095 bit-field itself.
11096
8c3f468d 11097 For any given bit-field, the "containing object" is a hypothetical object
11098 (of some integral or enum type) within which the given bit-field lives. The
11099 type of this hypothetical "containing object" is always the same as the
11100 declared type of the individual bit-field itself. The determination of the
11101 exact location of the "containing object" for a bit-field is rather
11102 complicated. It's handled by the `field_byte_offset' function (above).
30ade641 11103
11104 Note that it is the size (in bytes) of the hypothetical "containing object"
11105 which will be given in the DW_AT_byte_size attribute for this bit-field.
11106 (See `byte_size_attribute' above). */
ec1e49cc 11107
11108static inline void
8ec3a57b 11109add_bit_offset_attribute (dw_die_ref die, tree decl)
30ade641 11110{
5d844ba2 11111 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11112 tree type = DECL_BIT_FIELD_TYPE (decl);
11113 HOST_WIDE_INT bitpos_int;
11114 HOST_WIDE_INT highest_order_object_bit_offset;
11115 HOST_WIDE_INT highest_order_field_bit_offset;
11116 HOST_WIDE_INT unsigned bit_offset;
30ade641 11117
7e2bfe1e 11118 /* Must be a field and a bit field. */
7bd4f6b6 11119 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
30ade641 11120
11121 /* We can't yet handle bit-fields whose offsets are variable, so if we
11122 encounter such things, just return without generating any attribute
5d844ba2 11123 whatsoever. Likewise for variable or too large size. */
11124 if (! host_integerp (bit_position (decl), 0)
11125 || ! host_integerp (DECL_SIZE (decl), 1))
ec1e49cc 11126 return;
11127
5d844ba2 11128 bitpos_int = int_bit_position (decl);
30ade641 11129
11130 /* Note that the bit offset is always the distance (in bits) from the
f80d1bcd 11131 highest-order bit of the "containing object" to the highest-order bit of
11132 the bit-field itself. Since the "high-order end" of any object or field
30ade641 11133 is different on big-endian and little-endian machines, the computation
11134 below must take account of these differences. */
11135 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11136 highest_order_field_bit_offset = bitpos_int;
11137
ec1e49cc 11138 if (! BYTES_BIG_ENDIAN)
30ade641 11139 {
5d844ba2 11140 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
30ade641 11141 highest_order_object_bit_offset += simple_type_size_in_bits (type);
11142 }
ec1e49cc 11143
11144 bit_offset
11145 = (! BYTES_BIG_ENDIAN
11146 ? highest_order_object_bit_offset - highest_order_field_bit_offset
11147 : highest_order_field_bit_offset - highest_order_object_bit_offset);
30ade641 11148
11149 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11150}
11151
11152/* For a FIELD_DECL node which represents a bit field, output an attribute
11153 which specifies the length in bits of the given field. */
ec1e49cc 11154
11155static inline void
8ec3a57b 11156add_bit_size_attribute (dw_die_ref die, tree decl)
30ade641 11157{
7e2bfe1e 11158 /* Must be a field and a bit field. */
7bd4f6b6 11159 gcc_assert (TREE_CODE (decl) == FIELD_DECL
11160 && DECL_BIT_FIELD_TYPE (decl));
5d844ba2 11161
11162 if (host_integerp (DECL_SIZE (decl), 1))
11163 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
30ade641 11164}
11165
464217f3 11166/* If the compiled language is ANSI C, then add a 'prototyped'
30ade641 11167 attribute, if arg types are given for the parameters of a function. */
ec1e49cc 11168
11169static inline void
8ec3a57b 11170add_prototyped_attribute (dw_die_ref die, tree func_type)
30ade641 11171{
464217f3 11172 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11173 && TYPE_ARG_TYPES (func_type) != NULL)
11174 add_AT_flag (die, DW_AT_prototyped, 1);
30ade641 11175}
11176
30ade641 11177/* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
11178 by looking in either the type declaration or object declaration
11179 equate table. */
ec1e49cc 11180
11181static inline void
8ec3a57b 11182add_abstract_origin_attribute (dw_die_ref die, tree origin)
30ade641 11183{
11184 dw_die_ref origin_die = NULL;
b2ca6017 11185
bb0f15b4 11186 if (TREE_CODE (origin) != FUNCTION_DECL)
6c92ff4f 11187 {
11188 /* We may have gotten separated from the block for the inlined
11189 function, if we're in an exception handler or some such; make
11190 sure that the abstract function has been written out.
11191
c83a163c 11192 Doing this for nested functions is wrong, however; functions are
6c92ff4f 11193 distinct units, and our context might not even be inline. */
f929a98a 11194 tree fn = origin;
8c3f468d 11195
f929a98a 11196 if (TYPE_P (fn))
11197 fn = TYPE_STUB_DECL (fn);
61a9389f 11198
f10b7a77 11199 fn = decl_function_context (fn);
6c92ff4f 11200 if (fn)
f414ade2 11201 dwarf2out_abstract_function (fn);
6c92ff4f 11202 }
e3b3c2ae 11203
9308e976 11204 if (DECL_P (origin))
ec1e49cc 11205 origin_die = lookup_decl_die (origin);
9308e976 11206 else if (TYPE_P (origin))
ec1e49cc 11207 origin_die = lookup_type_die (origin);
11208
7c0a8197 11209 /* XXX: Functions that are never lowered don't always have correct block
11210 trees (in the case of java, they simply have no block tree, in some other
11211 languages). For these functions, there is nothing we can really do to
11212 output correct debug info for inlined functions in all cases. Rather
89f18f73 11213 than die, we'll just produce deficient debug info now, in that we will
7c0a8197 11214 have variables without a proper abstract origin. In the future, when all
11215 functions are lowered, we should re-add a gcc_assert (origin_die)
11216 here. */
f80d1bcd 11217
7c0a8197 11218 if (origin_die)
11219 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
30ade641 11220}
11221
6ed29fb8 11222/* We do not currently support the pure_virtual attribute. */
11223
ec1e49cc 11224static inline void
8ec3a57b 11225add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
30ade641 11226{
6efd403b 11227 if (DECL_VINDEX (func_decl))
30ade641 11228 {
6ed29fb8 11229 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
5d844ba2 11230
11231 if (host_integerp (DECL_VINDEX (func_decl), 0))
11232 add_AT_loc (die, DW_AT_vtable_elem_location,
11233 new_loc_descr (DW_OP_constu,
11234 tree_low_cst (DECL_VINDEX (func_decl), 0),
11235 0));
ec1e49cc 11236
6efd403b 11237 /* GNU extension: Record what type this method came from originally. */
11238 if (debug_info_level > DINFO_LEVEL_TERSE)
11239 add_AT_die_ref (die, DW_AT_containing_type,
11240 lookup_type_die (DECL_CONTEXT (func_decl)));
30ade641 11241 }
11242}
11243\f
840b696a 11244/* Add source coordinate attributes for the given decl. */
ec1e49cc 11245
840b696a 11246static void
8ec3a57b 11247add_src_coords_attributes (dw_die_ref die, tree decl)
840b696a 11248{
7bd3dcc4 11249 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
ec1e49cc 11250
69278c24 11251 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
7bd3dcc4 11252 add_AT_unsigned (die, DW_AT_decl_line, s.line);
840b696a 11253}
11254
df07c3ae 11255/* Add a DW_AT_name attribute and source coordinate attribute for the
30ade641 11256 given decl, but only if it actually has a name. */
ec1e49cc 11257
30ade641 11258static void
8ec3a57b 11259add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
30ade641 11260{
19cb6b50 11261 tree decl_name;
ec1e49cc 11262
f80d1bcd 11263 decl_name = DECL_NAME (decl);
ec1e49cc 11264 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
30ade641 11265 {
59561872 11266 add_name_attribute (die, dwarf2_name (decl, 0));
c90bf86c 11267 if (! DECL_ARTIFICIAL (decl))
11268 add_src_coords_attributes (die, decl);
2b553659 11269
59561872 11270 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4e1d939e 11271 && TREE_PUBLIC (decl)
8f80e66d 11272 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
23bf35fe 11273 && !DECL_ABSTRACT (decl)
11274 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
59561872 11275 add_AT_string (die, DW_AT_MIPS_linkage_name,
11276 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
30ade641 11277 }
8d60d2bc 11278
11279#ifdef VMS_DEBUGGING_INFO
8d60d2bc 11280 /* Get the function's name, as described by its RTL. This may be different
11281 from the DECL_NAME name used in the source file. */
11282 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
7facaa35 11283 {
11284 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11285 XEXP (DECL_RTL (decl), 0));
62aedc4c 11286 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
7facaa35 11287 }
8d60d2bc 11288#endif
30ade641 11289}
11290
f80d1bcd 11291/* Push a new declaration scope. */
ec1e49cc 11292
30ade641 11293static void
8ec3a57b 11294push_decl_scope (tree scope)
30ade641 11295{
4a940e75 11296 VEC_safe_push (tree, gc, decl_scope_table, scope);
30ade641 11297}
11298
14b40abb 11299/* Pop a declaration scope. */
8c3f468d 11300
14b40abb 11301static inline void
8ec3a57b 11302pop_decl_scope (void)
14b40abb 11303{
4a940e75 11304 VEC_pop (tree, decl_scope_table);
14b40abb 11305}
11306
11307/* Return the DIE for the scope that immediately contains this type.
11308 Non-named types get global scope. Named types nested in other
11309 types get their containing scope if it's open, or global scope
11310 otherwise. All other types (i.e. function-local named types) get
11311 the current active scope. */
ec1e49cc 11312
30ade641 11313static dw_die_ref
8ec3a57b 11314scope_die_for (tree t, dw_die_ref context_die)
30ade641 11315{
19cb6b50 11316 dw_die_ref scope_die = NULL;
11317 tree containing_scope;
11318 int i;
30ade641 11319
14b40abb 11320 /* Non-types always go in the current scope. */
7bd4f6b6 11321 gcc_assert (TYPE_P (t));
14b40abb 11322
11323 containing_scope = TYPE_CONTEXT (t);
db42c2b2 11324
e89530cd 11325 /* Use the containing namespace if it was passed in (for a declaration). */
7c43cc0e 11326 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
e89530cd 11327 {
11328 if (context_die == lookup_decl_die (containing_scope))
11329 /* OK */;
11330 else
11331 containing_scope = NULL_TREE;
11332 }
7c43cc0e 11333
5ef8d04d 11334 /* Ignore function type "scopes" from the C frontend. They mean that
11335 a tagged type is local to a parmlist of a function declarator, but
11336 that isn't useful to DWARF. */
11337 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11338 containing_scope = NULL_TREE;
11339
ec1e49cc 11340 if (containing_scope == NULL_TREE)
11341 scope_die = comp_unit_die;
14b40abb 11342 else if (TYPE_P (containing_scope))
5c65b85a 11343 {
14b40abb 11344 /* For types, we can just look up the appropriate DIE. But
11345 first we check to see if we're in the middle of emitting it
11346 so we know where the new DIE should go. */
4a940e75 11347 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11348 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
5c65b85a 11349 break;
11350
11351 if (i < 0)
11352 {
7bd4f6b6 11353 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11354 || TREE_ASM_WRITTEN (containing_scope));
5c65b85a 11355
11356 /* If none of the current dies are suitable, we get file scope. */
11357 scope_die = comp_unit_die;
11358 }
11359 else
14b40abb 11360 scope_die = lookup_type_die (containing_scope);
5c65b85a 11361 }
30ade641 11362 else
14b40abb 11363 scope_die = context_die;
ec1e49cc 11364
30ade641 11365 return scope_die;
11366}
11367
8c3f468d 11368/* Returns nonzero if CONTEXT_DIE is internal to a function. */
14b40abb 11369
11370static inline int
8ec3a57b 11371local_scope_p (dw_die_ref context_die)
30ade641 11372{
14b40abb 11373 for (; context_die; context_die = context_die->die_parent)
11374 if (context_die->die_tag == DW_TAG_inlined_subroutine
11375 || context_die->die_tag == DW_TAG_subprogram)
11376 return 1;
8c3f468d 11377
14b40abb 11378 return 0;
30ade641 11379}
11380
e89530cd 11381/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11382 whether or not to treat a DIE in this context as a declaration. */
ee1cd281 11383
11384static inline int
e89530cd 11385class_or_namespace_scope_p (dw_die_ref context_die)
ee1cd281 11386{
11387 return (context_die
11388 && (context_die->die_tag == DW_TAG_structure_type
e89530cd 11389 || context_die->die_tag == DW_TAG_union_type
11390 || context_die->die_tag == DW_TAG_namespace));
ee1cd281 11391}
11392
30ade641 11393/* Many forms of DIEs require a "type description" attribute. This
11394 routine locates the proper "type descriptor" die for the type given
df07c3ae 11395 by 'type', and adds a DW_AT_type attribute below the given die. */
ec1e49cc 11396
30ade641 11397static void
8ec3a57b 11398add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11399 int decl_volatile, dw_die_ref context_die)
30ade641 11400{
19cb6b50 11401 enum tree_code code = TREE_CODE (type);
11402 dw_die_ref type_die = NULL;
30ade641 11403
678d90bb 11404 /* ??? If this type is an unnamed subrange type of an integral or
11405 floating-point type, use the inner type. This is because we have no
11406 support for unnamed types in base_type_die. This can happen if this is
11407 an Ada subrange type. Correct solution is emit a subrange type die. */
34425fdc 11408 if ((code == INTEGER_TYPE || code == REAL_TYPE)
11409 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11410 type = TREE_TYPE (type), code = TREE_CODE (type);
11411
8c3f468d 11412 if (code == ERROR_MARK
11413 /* Handle a special case. For functions whose return type is void, we
11414 generate *no* type attribute. (Note that no object may have type
11415 `void', so this only applies to function return types). */
11416 || code == VOID_TYPE)
34425fdc 11417 return;
30ade641 11418
30ade641 11419 type_die = modified_type_die (type,
11420 decl_const || TYPE_READONLY (type),
11421 decl_volatile || TYPE_VOLATILE (type),
db42c2b2 11422 context_die);
8c3f468d 11423
30ade641 11424 if (type_die != NULL)
ec1e49cc 11425 add_AT_die_ref (object_die, DW_AT_type, type_die);
30ade641 11426}
11427
8ff30ff6 11428/* Given an object die, add the calling convention attribute for the
11429 function call type. */
11430static void
11431add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11432{
11433 enum dwarf_calling_convention value = DW_CC_normal;
11434
11435 value = targetm.dwarf_calling_convention (type);
11436
785a2b1d 11437 /* Only add the attribute if the backend requests it, and
11438 is not DW_CC_normal. */
11439 if (value && (value != DW_CC_normal))
8ff30ff6 11440 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11441}
11442
30ade641 11443/* Given a tree pointer to a struct, class, union, or enum type node, return
11444 a pointer to the (string) tag name for the given type, or zero if the type
11445 was declared without a tag. */
ec1e49cc 11446
1e034a40 11447static const char *
8ec3a57b 11448type_tag (tree type)
30ade641 11449{
19cb6b50 11450 const char *name = 0;
30ade641 11451
11452 if (TYPE_NAME (type) != 0)
11453 {
19cb6b50 11454 tree t = 0;
30ade641 11455
11456 /* Find the IDENTIFIER_NODE for the type name. */
11457 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11458 t = TYPE_NAME (type);
6ed29fb8 11459
f80d1bcd 11460 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
c83a163c 11461 a TYPE_DECL node, regardless of whether or not a `typedef' was
11462 involved. */
6efd403b 11463 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11464 && ! DECL_IGNORED_P (TYPE_NAME (type)))
facb12b2 11465 {
11466 /* We want to be extra verbose. Don't call dwarf_name if
11467 DECL_NAME isn't set. The default hook for decl_printable_name
11468 doesn't like that, and in this context it's correct to return
11469 0, instead of "<anonymous>" or the like. */
11470 if (DECL_NAME (TYPE_NAME (type)))
11471 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11472 }
6ed29fb8 11473
30ade641 11474 /* Now get the name as a string, or invent one. */
facb12b2 11475 if (!name && t != 0)
6efd403b 11476 name = IDENTIFIER_POINTER (t);
30ade641 11477 }
ec1e49cc 11478
30ade641 11479 return (name == 0 || *name == '\0') ? 0 : name;
11480}
11481
11482/* Return the type associated with a data member, make a special check
11483 for bit field types. */
ec1e49cc 11484
11485static inline tree
8ec3a57b 11486member_declared_type (tree member)
30ade641 11487{
ec1e49cc 11488 return (DECL_BIT_FIELD_TYPE (member)
8c3f468d 11489 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
30ade641 11490}
11491
dc7a29ce 11492/* Get the decl's label, as described by its RTL. This may be different
30ade641 11493 from the DECL_NAME name used in the source file. */
ec1e49cc 11494
0e93a6ac 11495#if 0
1e034a40 11496static const char *
8ec3a57b 11497decl_start_label (tree decl)
30ade641 11498{
11499 rtx x;
1e034a40 11500 const char *fnname;
8c3f468d 11501
30ade641 11502 x = DECL_RTL (decl);
7bd4f6b6 11503 gcc_assert (MEM_P (x));
ec1e49cc 11504
30ade641 11505 x = XEXP (x, 0);
7bd4f6b6 11506 gcc_assert (GET_CODE (x) == SYMBOL_REF);
ec1e49cc 11507
30ade641 11508 fnname = XSTR (x, 0);
11509 return fnname;
11510}
0e93a6ac 11511#endif
30ade641 11512\f
ad87de1e 11513/* These routines generate the internal representation of the DIE's for
30ade641 11514 the compilation unit. Debugging information is collected by walking
464217f3 11515 the declaration trees passed in from dwarf2out_decl(). */
30ade641 11516
11517static void
8ec3a57b 11518gen_array_type_die (tree type, dw_die_ref context_die)
30ade641 11519{
19cb6b50 11520 dw_die_ref scope_die = scope_die_for (type, context_die);
11521 dw_die_ref array_die;
11522 tree element_type;
6ed29fb8 11523
5b67860b 11524 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11525 the inner array type comes before the outer array type. Thus we must
11526 call gen_type_die before we call new_die. See below also. */
11527#ifdef MIPS_DEBUGGING_INFO
11528 gen_type_die (TREE_TYPE (type), context_die);
11529#endif
11530
15cfae4e 11531 array_die = new_die (DW_TAG_array_type, scope_die, type);
634906d6 11532 add_name_attribute (array_die, type_tag (type));
11533 equate_type_number_to_die (type, array_die);
11534
11535 if (TREE_CODE (type) == VECTOR_TYPE)
11536 {
11537 /* The frontend feeds us a representation for the vector as a struct
11538 containing an array. Pull out the array type. */
11539 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11540 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11541 }
5b67860b 11542
30ade641 11543#if 0
11544 /* We default the array ordering. SDB will probably do
11545 the right things even if DW_AT_ordering is not present. It's not even
11546 an issue until we start to get into multidimensional arrays anyway. If
11547 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11548 then we'll have to put the DW_AT_ordering attribute back in. (But if
11549 and when we find out that we need to put these in, we will only do so
11550 for multidimensional arrays. */
11551 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11552#endif
11553
5b67860b 11554#ifdef MIPS_DEBUGGING_INFO
cc324702 11555 /* The SGI compilers handle arrays of unknown bound by setting
11556 AT_declaration and not emitting any subrange DIEs. */
5b67860b 11557 if (! TYPE_DOMAIN (type))
8c50ec6a 11558 add_AT_flag (array_die, DW_AT_declaration, 1);
5b67860b 11559 else
11560#endif
11561 add_subscript_info (array_die, type);
30ade641 11562
30ade641 11563 /* Add representation of the type of the elements of this array type. */
11564 element_type = TREE_TYPE (type);
ec1e49cc 11565
30ade641 11566 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11567 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11568 We work around this by disabling this feature. See also
11569 add_subscript_info. */
11570#ifndef MIPS_DEBUGGING_INFO
ec1e49cc 11571 while (TREE_CODE (element_type) == ARRAY_TYPE)
11572 element_type = TREE_TYPE (element_type);
11573
30ade641 11574 gen_type_die (element_type, context_die);
5b67860b 11575#endif
30ade641 11576
11577 add_type_attribute (array_die, element_type, 0, 0, context_die);
af84796a 11578
11579 if (get_AT (array_die, DW_AT_name))
11580 add_pubtype (type, array_die);
30ade641 11581}
11582
cd03a192 11583#if 0
30ade641 11584static void
8ec3a57b 11585gen_entry_point_die (tree decl, dw_die_ref context_die)
30ade641 11586{
19cb6b50 11587 tree origin = decl_ultimate_origin (decl);
15cfae4e 11588 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
8c3f468d 11589
30ade641 11590 if (origin != NULL)
ec1e49cc 11591 add_abstract_origin_attribute (decl_die, origin);
30ade641 11592 else
11593 {
11594 add_name_and_src_coords_attributes (decl_die, decl);
30ade641 11595 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11596 0, 0, context_die);
11597 }
ec1e49cc 11598
30ade641 11599 if (DECL_ABSTRACT (decl))
ec1e49cc 11600 equate_decl_number_to_die (decl, decl_die);
30ade641 11601 else
ec1e49cc 11602 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
30ade641 11603}
cd03a192 11604#endif
30ade641 11605
a4617d03 11606/* Walk through the list of incomplete types again, trying once more to
11607 emit full debugging info for them. */
11608
11609static void
8ec3a57b 11610retry_incomplete_types (void)
a4617d03 11611{
52a7cc7b 11612 int i;
8c3f468d 11613
22230dd1 11614 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11615 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
a4617d03 11616}
11617
30ade641 11618/* Generate a DIE to represent an inlined instance of an enumeration type. */
ec1e49cc 11619
30ade641 11620static void
8ec3a57b 11621gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
30ade641 11622{
15cfae4e 11623 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
8c3f468d 11624
b2ca6017 11625 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11626 be incomplete and such types are not marked. */
30ade641 11627 add_abstract_origin_attribute (type_die, type);
11628}
11629
11630/* Generate a DIE to represent an inlined instance of a structure type. */
ec1e49cc 11631
30ade641 11632static void
8ec3a57b 11633gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
30ade641 11634{
15cfae4e 11635 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
14b40abb 11636
b2ca6017 11637 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11638 be incomplete and such types are not marked. */
30ade641 11639 add_abstract_origin_attribute (type_die, type);
11640}
11641
11642/* Generate a DIE to represent an inlined instance of a union type. */
ec1e49cc 11643
30ade641 11644static void
8ec3a57b 11645gen_inlined_union_type_die (tree type, dw_die_ref context_die)
30ade641 11646{
15cfae4e 11647 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
14b40abb 11648
b2ca6017 11649 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11650 be incomplete and such types are not marked. */
30ade641 11651 add_abstract_origin_attribute (type_die, type);
11652}
11653
11654/* Generate a DIE to represent an enumeration type. Note that these DIEs
11655 include all of the information about the enumeration values also. Each
6542a017 11656 enumerated type name/value is listed as a child of the enumerated type
11657 DIE. */
ec1e49cc 11658
93c7db82 11659static dw_die_ref
8ec3a57b 11660gen_enumeration_type_die (tree type, dw_die_ref context_die)
30ade641 11661{
19cb6b50 11662 dw_die_ref type_die = lookup_type_die (type);
6542a017 11663
30ade641 11664 if (type_die == NULL)
11665 {
11666 type_die = new_die (DW_TAG_enumeration_type,
15cfae4e 11667 scope_die_for (type, context_die), type);
30ade641 11668 equate_type_number_to_die (type, type_die);
11669 add_name_attribute (type_die, type_tag (type));
30ade641 11670 }
6542a017 11671 else if (! TYPE_SIZE (type))
93c7db82 11672 return type_die;
6542a017 11673 else
11674 remove_AT (type_die, DW_AT_declaration);
11675
11676 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11677 given enum type is incomplete, do not generate the DW_AT_byte_size
11678 attribute or the DW_AT_element_list attribute. */
11679 if (TYPE_SIZE (type))
30ade641 11680 {
19cb6b50 11681 tree link;
ec1e49cc 11682
a3377a8b 11683 TREE_ASM_WRITTEN (type) = 1;
6542a017 11684 add_byte_size_attribute (type_die, type);
0dbd1c74 11685 if (TYPE_STUB_DECL (type) != NULL_TREE)
840b696a 11686 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
ec1e49cc 11687
678d90bb 11688 /* If the first reference to this type was as the return type of an
11689 inline function, then it may not have a parent. Fix this now. */
11690 if (type_die->die_parent == NULL)
11691 add_child_die (scope_die_for (type, context_die), type_die);
11692
82bb2115 11693 for (link = TYPE_VALUES (type);
6542a017 11694 link != NULL; link = TREE_CHAIN (link))
30ade641 11695 {
15cfae4e 11696 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
99f3dd6a 11697 tree value = TREE_VALUE (link);
ec1e49cc 11698
6542a017 11699 add_name_attribute (enum_die,
11700 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
5d844ba2 11701
78a8ed03 11702 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
99f3dd6a 11703 /* DWARF2 does not provide a way of indicating whether or
11704 not enumeration constants are signed or unsigned. GDB
11705 always assumes the values are signed, so we output all
11706 values as if they were signed. That means that
11707 enumeration constants with very large unsigned values
11708 will appear to have negative values in the debugger. */
11709 add_AT_int (enum_die, DW_AT_const_value,
11710 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
30ade641 11711 }
11712 }
6542a017 11713 else
11714 add_AT_flag (type_die, DW_AT_declaration, 1);
93c7db82 11715
af84796a 11716 if (get_AT (type_die, DW_AT_name))
11717 add_pubtype (type, type_die);
11718
93c7db82 11719 return type_die;
30ade641 11720}
11721
30ade641 11722/* Generate a DIE to represent either a real live formal parameter decl or to
11723 represent just the type of some formal parameter position in some function
11724 type.
ec1e49cc 11725
30ade641 11726 Note that this routine is a bit unusual because its argument may be a
11727 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11728 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11729 node. If it's the former then this function is being called to output a
11730 DIE to represent a formal parameter object (or some inlining thereof). If
11731 it's the latter, then this function is only being called to output a
11732 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11733 argument type of some subprogram type. */
ec1e49cc 11734
6efd403b 11735static dw_die_ref
8ec3a57b 11736gen_formal_parameter_die (tree node, dw_die_ref context_die)
30ade641 11737{
19cb6b50 11738 dw_die_ref parm_die
15cfae4e 11739 = new_die (DW_TAG_formal_parameter, context_die, node);
19cb6b50 11740 tree origin;
ec1e49cc 11741
30ade641 11742 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11743 {
ce45a448 11744 case tcc_declaration:
30ade641 11745 origin = decl_ultimate_origin (node);
11746 if (origin != NULL)
6efd403b 11747 add_abstract_origin_attribute (parm_die, origin);
30ade641 11748 else
11749 {
11750 add_name_and_src_coords_attributes (parm_die, node);
11751 add_type_attribute (parm_die, TREE_TYPE (node),
11752 TREE_READONLY (node),
11753 TREE_THIS_VOLATILE (node),
11754 context_die);
6ed29fb8 11755 if (DECL_ARTIFICIAL (node))
11756 add_AT_flag (parm_die, DW_AT_artificial, 1);
30ade641 11757 }
ec1e49cc 11758
0defae70 11759 equate_decl_number_to_die (node, parm_die);
11760 if (! DECL_ABSTRACT (node))
b2025850 11761 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
ec1e49cc 11762
30ade641 11763 break;
11764
ce45a448 11765 case tcc_type:
ec1e49cc 11766 /* We were called with some kind of a ..._TYPE node. */
30ade641 11767 add_type_attribute (parm_die, node, 0, 0, context_die);
11768 break;
11769
30ade641 11770 default:
7bd4f6b6 11771 gcc_unreachable ();
30ade641 11772 }
ec1e49cc 11773
6efd403b 11774 return parm_die;
30ade641 11775}
11776
11777/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11778 at the end of an (ANSI prototyped) formal parameters list. */
ec1e49cc 11779
30ade641 11780static void
8ec3a57b 11781gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
30ade641 11782{
15cfae4e 11783 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
30ade641 11784}
11785
11786/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11787 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11788 parameters as specified in some function type specification (except for
0dbc398a 11789 those which appear as part of a function *definition*). */
ec1e49cc 11790
30ade641 11791static void
8ec3a57b 11792gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
30ade641 11793{
19cb6b50 11794 tree link;
11795 tree formal_type = NULL;
11796 tree first_parm_type;
8f80e66d 11797 tree arg;
30ade641 11798
8f80e66d 11799 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11800 {
11801 arg = DECL_ARGUMENTS (function_or_method_type);
11802 function_or_method_type = TREE_TYPE (function_or_method_type);
11803 }
11804 else
11805 arg = NULL_TREE;
bc70bd5e 11806
8f80e66d 11807 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
30ade641 11808
f80d1bcd 11809 /* Make our first pass over the list of formal parameter types and output a
30ade641 11810 DW_TAG_formal_parameter DIE for each one. */
8f80e66d 11811 for (link = first_parm_type; link; )
30ade641 11812 {
19cb6b50 11813 dw_die_ref parm_die;
f80d1bcd 11814
30ade641 11815 formal_type = TREE_VALUE (link);
11816 if (formal_type == void_type_node)
11817 break;
11818
11819 /* Output a (nameless) DIE to represent the formal parameter itself. */
6efd403b 11820 parm_die = gen_formal_parameter_die (formal_type, context_die);
8f80e66d 11821 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11822 && link == first_parm_type)
11823 || (arg && DECL_ARTIFICIAL (arg)))
6efd403b 11824 add_AT_flag (parm_die, DW_AT_artificial, 1);
8f80e66d 11825
11826 link = TREE_CHAIN (link);
11827 if (arg)
11828 arg = TREE_CHAIN (arg);
30ade641 11829 }
11830
11831 /* If this function type has an ellipsis, add a
11832 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11833 if (formal_type != void_type_node)
11834 gen_unspecified_parameters_die (function_or_method_type, context_die);
11835
f80d1bcd 11836 /* Make our second (and final) pass over the list of formal parameter types
30ade641 11837 and output DIEs to represent those types (as necessary). */
11838 for (link = TYPE_ARG_TYPES (function_or_method_type);
8c3f468d 11839 link && TREE_VALUE (link);
30ade641 11840 link = TREE_CHAIN (link))
8c3f468d 11841 gen_type_die (TREE_VALUE (link), context_die);
30ade641 11842}
11843
e7b3c55c 11844/* We want to generate the DIE for TYPE so that we can generate the
11845 die for MEMBER, which has been defined; we will need to refer back
11846 to the member declaration nested within TYPE. If we're trying to
11847 generate minimal debug info for TYPE, processing TYPE won't do the
11848 trick; we need to attach the member declaration by hand. */
11849
11850static void
8ec3a57b 11851gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
e7b3c55c 11852{
11853 gen_type_die (type, context_die);
11854
11855 /* If we're trying to avoid duplicate debug info, we may not have
11856 emitted the member decl for this function. Emit it now. */
11857 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11858 && ! lookup_decl_die (member))
11859 {
d4946992 11860 dw_die_ref type_die;
7bd4f6b6 11861 gcc_assert (!decl_ultimate_origin (member));
e7b3c55c 11862
11863 push_decl_scope (type);
d4946992 11864 type_die = lookup_type_die (type);
e7b3c55c 11865 if (TREE_CODE (member) == FUNCTION_DECL)
d4946992 11866 gen_subprogram_die (member, type_die);
11867 else if (TREE_CODE (member) == FIELD_DECL)
11868 {
11869 /* Ignore the nameless fields that are used to skip bits but handle
11870 C++ anonymous unions and structs. */
11871 if (DECL_NAME (member) != NULL_TREE
11872 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11873 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11874 {
11875 gen_type_die (member_declared_type (member), type_die);
11876 gen_field_die (member, type_die);
11877 }
11878 }
e7b3c55c 11879 else
d4946992 11880 gen_variable_die (member, type_die);
8c3f468d 11881
e7b3c55c 11882 pop_decl_scope ();
11883 }
11884}
11885
8c3f468d 11886/* Generate the DWARF2 info for the "abstract" instance of a function which we
11887 may later generate inlined and/or out-of-line instances of. */
e7b3c55c 11888
b29760a8 11889static void
8ec3a57b 11890dwarf2out_abstract_function (tree decl)
e7b3c55c 11891{
19cb6b50 11892 dw_die_ref old_die;
14b40abb 11893 tree save_fn;
89c30811 11894 struct function *save_cfun;
8f80e66d 11895 tree context;
11896 int was_abstract = DECL_ABSTRACT (decl);
11897
11898 /* Make sure we have the actual abstract inline, not a clone. */
11899 decl = DECL_ORIGIN (decl);
e7b3c55c 11900
bc70bd5e 11901 old_die = lookup_decl_die (decl);
2e14ce2e 11902 if (old_die && get_AT (old_die, DW_AT_inline))
e7b3c55c 11903 /* We've already generated the abstract instance. */
11904 return;
11905
8f80e66d 11906 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11907 we don't get confused by DECL_ABSTRACT. */
0c88fb4f 11908 if (debug_info_level > DINFO_LEVEL_TERSE)
11909 {
11910 context = decl_class_context (decl);
11911 if (context)
11912 gen_type_die_for_member
11913 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11914 }
bc70bd5e 11915
8f80e66d 11916 /* Pretend we've just finished compiling this function. */
14b40abb 11917 save_fn = current_function_decl;
89c30811 11918 save_cfun = cfun;
14b40abb 11919 current_function_decl = decl;
89c30811 11920 cfun = DECL_STRUCT_FUNCTION (decl);
14b40abb 11921
e7b3c55c 11922 set_decl_abstract_flags (decl, 1);
11923 dwarf2out_decl (decl);
8f80e66d 11924 if (! was_abstract)
11925 set_decl_abstract_flags (decl, 0);
14b40abb 11926
11927 current_function_decl = save_fn;
89c30811 11928 cfun = save_cfun;
e7b3c55c 11929}
11930
f6e59711 11931/* Helper function of premark_used_types() which gets called through
11932 htab_traverse_resize().
11933
11934 Marks the DIE of a given type in *SLOT as perennial, so it never gets
11935 marked as unused by prune_unused_types. */
11936static int
11937premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11938{
11939 tree type;
11940 dw_die_ref die;
11941
11942 type = *slot;
11943 die = lookup_type_die (type);
11944 if (die != NULL)
11945 die->die_perennial_p = 1;
11946 return 1;
11947}
11948
11949/* Mark all members of used_types_hash as perennial. */
11950static void
11951premark_used_types (void)
11952{
11953 if (cfun && cfun->used_types_hash)
11954 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11955}
11956
30ade641 11957/* Generate a DIE to represent a declared function (either file-scope or
11958 block-local). */
ec1e49cc 11959
30ade641 11960static void
8ec3a57b 11961gen_subprogram_die (tree decl, dw_die_ref context_die)
30ade641 11962{
11963 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19cb6b50 11964 tree origin = decl_ultimate_origin (decl);
11965 dw_die_ref subr_die;
19cb6b50 11966 tree fn_arg_types;
11967 tree outer_scope;
11968 dw_die_ref old_die = lookup_decl_die (decl);
11969 int declaration = (current_function_decl != decl
e89530cd 11970 || class_or_namespace_scope_p (context_die));
30ade641 11971
89c30811 11972 premark_used_types ();
f6e59711 11973
8c3f468d 11974 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11975 started to generate the abstract instance of an inline, decided to output
11976 its containing class, and proceeded to emit the declaration of the inline
11977 from the member list for the class. If so, DECLARATION takes priority;
11978 we'll get back to the abstract instance when done with the class. */
e7b3c55c 11979
0dbc398a 11980 /* The class-scope declaration DIE must be the primary DIE. */
e89530cd 11981 if (origin && declaration && class_or_namespace_scope_p (context_die))
0dbc398a 11982 {
11983 origin = NULL;
7bd4f6b6 11984 gcc_assert (!old_die);
0dbc398a 11985 }
11986
dcfa82ba 11987 /* Now that the C++ front end lazily declares artificial member fns, we
11988 might need to retrofit the declaration into its class. */
11989 if (!declaration && !origin && !old_die
11990 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11991 && !class_or_namespace_scope_p (context_die)
11992 && debug_info_level > DINFO_LEVEL_TERSE)
11993 old_die = force_decl_die (decl);
11994
30ade641 11995 if (origin != NULL)
11996 {
7bd4f6b6 11997 gcc_assert (!declaration || local_scope_p (context_die));
e7b3c55c 11998
48fdacd0 11999 /* Fixup die_parent for the abstract instance of a nested
12000 inline function. */
12001 if (old_die && old_die->die_parent == NULL)
12002 add_child_die (context_die, old_die);
12003
15cfae4e 12004 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
30ade641 12005 add_abstract_origin_attribute (subr_die, origin);
12006 }
6ed29fb8 12007 else if (old_die)
12008 {
7bd3dcc4 12009 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
69278c24 12010 struct dwarf_file_data * file_index = lookup_filename (s.file);
6efd403b 12011
f414ade2 12012 if (!get_AT_flag (old_die, DW_AT_declaration)
12013 /* We can have a normal definition following an inline one in the
12014 case of redefinition of GNU C extern inlines.
12015 It seems reasonable to use AT_specification in this case. */
2e14ce2e 12016 && !get_AT (old_die, DW_AT_inline))
c2581433 12017 {
7c0a8197 12018 /* Detect and ignore this case, where we are trying to output
12019 something we have already output. */
7bd4f6b6 12020 return;
c2581433 12021 }
752e49ca 12022
12023 /* If the definition comes from the same place as the declaration,
6efd403b 12024 maybe use the old DIE. We always want the DIE for this function
12025 that has the *_pc attributes to be under comp_unit_die so the
a7678b15 12026 debugger can find it. We also need to do this for abstract
12027 instances of inlines, since the spec requires the out-of-line copy
12028 to have the same parent. For local class methods, this doesn't
12029 apply; we just use the old DIE. */
12030 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
c90bf86c 12031 && (DECL_ARTIFICIAL (decl)
69278c24 12032 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
c90bf86c 12033 && (get_AT_unsigned (old_die, DW_AT_decl_line)
7bd3dcc4 12034 == (unsigned) s.line))))
6ed29fb8 12035 {
752e49ca 12036 subr_die = old_die;
12037
2b49746a 12038 /* Clear out the declaration attribute and the formal parameters.
8ff30ff6 12039 Do not remove all children, because it is possible that this
2b49746a 12040 declaration die was forced using force_decl_die(). In such
12041 cases die that forced declaration die (e.g. TAG_imported_module)
12042 is one of the children that we do not want to remove. */
752e49ca 12043 remove_AT (subr_die, DW_AT_declaration);
2b49746a 12044 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
752e49ca 12045 }
12046 else
12047 {
15cfae4e 12048 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
023dc493 12049 add_AT_specification (subr_die, old_die);
69278c24 12050 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12051 add_AT_file (subr_die, DW_AT_decl_file, file_index);
12052 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12053 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
6ed29fb8 12054 }
12055 }
30ade641 12056 else
12057 {
15cfae4e 12058 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
f80d1bcd 12059
6542a017 12060 if (TREE_PUBLIC (decl))
12061 add_AT_flag (subr_die, DW_AT_external, 1);
ec1e49cc 12062
30ade641 12063 add_name_and_src_coords_attributes (subr_die, decl);
43f116ae 12064 if (debug_info_level > DINFO_LEVEL_TERSE)
12065 {
8c3f468d 12066 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12067 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12068 0, 0, context_die);
43f116ae 12069 }
ec1e49cc 12070
30ade641 12071 add_pure_or_virtual_attribute (subr_die, decl);
6542a017 12072 if (DECL_ARTIFICIAL (decl))
12073 add_AT_flag (subr_die, DW_AT_artificial, 1);
8c3f468d 12074
6efd403b 12075 if (TREE_PROTECTED (decl))
12076 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12077 else if (TREE_PRIVATE (decl))
12078 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
30ade641 12079 }
cc324702 12080
6efd403b 12081 if (declaration)
12082 {
2e14ce2e 12083 if (!old_die || !get_AT (old_die, DW_AT_inline))
f414ade2 12084 {
12085 add_AT_flag (subr_die, DW_AT_declaration, 1);
12086
12087 /* The first time we see a member function, it is in the context of
12088 the class to which it belongs. We make sure of this by emitting
12089 the class first. The next time is the definition, which is
8ff30ff6 12090 handled above. The two may come from the same source text.
2b49746a 12091
12092 Note that force_decl_die() forces function declaration die. It is
12093 later reused to represent definition. */
dcfa82ba 12094 equate_decl_number_to_die (decl, subr_die);
f414ade2 12095 }
6efd403b 12096 }
12097 else if (DECL_ABSTRACT (decl))
30ade641 12098 {
5bd74231 12099 if (DECL_DECLARED_INLINE_P (decl))
404ba76d 12100 {
61a9389f 12101 if (cgraph_function_possibly_inlined_p (decl))
404ba76d 12102 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12103 else
5bd74231 12104 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
404ba76d 12105 }
404ba76d 12106 else
5bd74231 12107 {
12108 if (cgraph_function_possibly_inlined_p (decl))
61a9389f 12109 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
5bd74231 12110 else
61a9389f 12111 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
5bd74231 12112 }
404ba76d 12113
30ade641 12114 equate_decl_number_to_die (decl, subr_die);
12115 }
12116 else if (!DECL_EXTERNAL (decl))
12117 {
89fa767a 12118 HOST_WIDE_INT cfa_fb_offset;
12119
2e14ce2e 12120 if (!old_die || !get_AT (old_die, DW_AT_inline))
ca2cef7a 12121 equate_decl_number_to_die (decl, subr_die);
ec1e49cc 12122
1897b881 12123 if (!flag_reorder_blocks_and_partition)
12124 {
12125 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12126 current_function_funcdef_no);
12127 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12128 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12129 current_function_funcdef_no);
12130 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
61a9389f 12131
1897b881 12132 add_pubname (decl, subr_die);
12133 add_arange (decl, subr_die);
12134 }
12135 else
12136 { /* Do nothing for now; maybe need to duplicate die, one for
12137 hot section and ond for cold section, then use the hot/cold
12138 section begin/end labels to generate the aranges... */
12139 /*
12140 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12141 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12142 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12143 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12144
12145 add_pubname (decl, subr_die);
12146 add_arange (decl, subr_die);
12147 add_arange (decl, subr_die);
12148 */
12149 }
dc7a29ce 12150
30ade641 12151#ifdef MIPS_DEBUGGING_INFO
30ade641 12152 /* Add a reference to the FDE for this routine. */
12153 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12154#endif
12155
89fa767a 12156 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12157
12d886b8 12158 /* We define the "frame base" as the function's CFA. This is more
12159 convenient for several reasons: (1) It's stable across the prologue
12160 and epilogue, which makes it better than just a frame pointer,
12161 (2) With dwarf3, there exists a one-byte encoding that allows us
12162 to reference the .debug_frame data by proxy, but failing that,
12163 (3) We can at least reuse the code inspection and interpretation
12164 code that determines the CFA position at various points in the
12165 function. */
12166 /* ??? Use some command-line or configury switch to enable the use
12167 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
12168 consumers that understand it; fall back to "pure" dwarf2 and
12169 convert the CFA data into a location list. */
12170 {
89fa767a 12171 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12d886b8 12172 if (list->dw_loc_next)
12173 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12174 else
12175 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12176 }
12177
12178 /* Compute a displacement from the "steady-state frame pointer" to
12179 the CFA. The former is what all stack slots and argument slots
61a9389f 12180 will reference in the rtl; the later is what we've told the
12d886b8 12181 debugger about. We'll need to adjust all frame_base references
12182 by this displacement. */
89fa767a 12183 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
30ade641 12184
4ee9c684 12185 if (cfun->static_chain_decl)
678d90bb 12186 add_AT_location_description (subr_die, DW_AT_static_link,
afcf285e 12187 loc_descriptor_from_tree (cfun->static_chain_decl));
30ade641 12188 }
12189
12190 /* Now output descriptions of the arguments for this function. This gets
f80d1bcd 12191 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
30ade641 12192 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12193 `...' at the end of the formal parameter list. In order to find out if
12194 there was a trailing ellipsis or not, we must instead look at the type
12195 associated with the FUNCTION_DECL. This will be a node of type
12196 FUNCTION_TYPE. If the chain of type nodes hanging off of this
f80d1bcd 12197 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
30ade641 12198 an ellipsis at the end. */
ec1e49cc 12199
30ade641 12200 /* In the case where we are describing a mere function declaration, all we
f80d1bcd 12201 need to do here (and all we *can* do here) is to describe the *types* of
30ade641 12202 its formal parameters. */
43f116ae 12203 if (debug_info_level <= DINFO_LEVEL_TERSE)
ec1e49cc 12204 ;
cc324702 12205 else if (declaration)
8f80e66d 12206 gen_formal_types_die (decl, subr_die);
30ade641 12207 else
12208 {
2358393e 12209 /* Generate DIEs to represent all known formal parameters. */
19cb6b50 12210 tree arg_decls = DECL_ARGUMENTS (decl);
12211 tree parm;
30ade641 12212
12213 /* When generating DIEs, generate the unspecified_parameters DIE
c83a163c 12214 instead if we come across the arg "__builtin_va_alist" */
30ade641 12215 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
ec1e49cc 12216 if (TREE_CODE (parm) == PARM_DECL)
12217 {
0bc644e0 12218 if (DECL_NAME (parm)
12219 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12220 "__builtin_va_alist"))
ec1e49cc 12221 gen_unspecified_parameters_die (parm, subr_die);
12222 else
12223 gen_decl_die (parm, subr_die);
12224 }
30ade641 12225
20dd417a 12226 /* Decide whether we need an unspecified_parameters DIE at the end.
c83a163c 12227 There are 2 more cases to do this for: 1) the ansi ... declaration -
12228 this is detectable when the end of the arg list is not a
12229 void_type_node 2) an unprototyped function declaration (not a
12230 definition). This just means that we have no info about the
12231 parameters at all. */
30ade641 12232 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
ec1e49cc 12233 if (fn_arg_types != NULL)
30ade641 12234 {
139c3f48 12235 /* This is the prototyped case, check for.... */
30ade641 12236 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
ec1e49cc 12237 gen_unspecified_parameters_die (decl, subr_die);
30ade641 12238 }
ec1e49cc 12239 else if (DECL_INITIAL (decl) == NULL_TREE)
12240 gen_unspecified_parameters_die (decl, subr_die);
30ade641 12241 }
12242
12243 /* Output Dwarf info for all of the stuff within the body of the function
12244 (if it has one - it may be just a declaration). */
12245 outer_scope = DECL_INITIAL (decl);
12246
8c3f468d 12247 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12248 a function. This BLOCK actually represents the outermost binding contour
12249 for the function, i.e. the contour in which the function's formal
12250 parameters and labels get declared. Curiously, it appears that the front
12251 end doesn't actually put the PARM_DECL nodes for the current function onto
12252 the BLOCK_VARS list for this outer scope, but are strung off of the
12253 DECL_ARGUMENTS list for the function instead.
12254
12255 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12256 the LABEL_DECL nodes for the function however, and we output DWARF info
12257 for those in decls_for_scope. Just within the `outer_scope' there will be
12258 a BLOCK node representing the function's outermost pair of curly braces,
12259 and any blocks used for the base and member initializers of a C++
cb371216 12260 constructor function. */
cc324702 12261 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
a3899bb7 12262 {
4ee9c684 12263 /* Emit a DW_TAG_variable DIE for a named return value. */
12264 if (DECL_NAME (DECL_RESULT (decl)))
12265 gen_decl_die (DECL_RESULT (decl), subr_die);
12266
a3899bb7 12267 current_function_has_inlines = 0;
12268 decls_for_scope (outer_scope, subr_die, 0);
ec1e49cc 12269
0680318b 12270#if 0 && defined (MIPS_DEBUGGING_INFO)
a3899bb7 12271 if (current_function_has_inlines)
12272 {
12273 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12274 if (! comp_unit_has_inlines)
12275 {
12276 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12277 comp_unit_has_inlines = 1;
12278 }
12279 }
12280#endif
12281 }
a4b48f01 12282 /* Add the calling convention attribute if requested. */
12283 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12284
30ade641 12285}
12286
12287/* Generate a DIE to represent a declared data object. */
ec1e49cc 12288
30ade641 12289static void
8ec3a57b 12290gen_variable_die (tree decl, dw_die_ref context_die)
30ade641 12291{
19cb6b50 12292 tree origin = decl_ultimate_origin (decl);
15cfae4e 12293 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
ec1e49cc 12294
6ed29fb8 12295 dw_die_ref old_die = lookup_decl_die (decl);
ee1cd281 12296 int declaration = (DECL_EXTERNAL (decl)
211fa870 12297 /* If DECL is COMDAT and has not actually been
12298 emitted, we cannot take its address; there
12299 might end up being no definition anywhere in
12300 the program. For example, consider the C++
12301 test case:
12302
61a9389f 12303 template <class T>
12304 struct S { static const int i = 7; };
211fa870 12305
61a9389f 12306 template <class T>
12307 const int S<T>::i;
12308
12309 int f() { return S<int>::i; }
211fa870 12310
211fa870 12311 Here, S<int>::i is not DECL_EXTERNAL, but no
12312 definition is required, so the compiler will
61a9389f 12313 not emit a definition. */
211fa870 12314 || (TREE_CODE (decl) == VAR_DECL
12315 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
e89530cd 12316 || class_or_namespace_scope_p (context_die));
cc324702 12317
30ade641 12318 if (origin != NULL)
ec1e49cc 12319 add_abstract_origin_attribute (var_die, origin);
8c3f468d 12320
5e1bdb0e 12321 /* Loop unrolling can create multiple blocks that refer to the same
8c3f468d 12322 static variable, so we must test for the DW_AT_declaration flag.
12323
12324 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
5e1bdb0e 12325 copy decls and set the DECL_ABSTRACT flag on them instead of
8c3f468d 12326 sharing them.
12327
6e395578 12328 ??? Duplicated blocks have been rewritten to use .debug_ranges.
12329
12330 ??? The declare_in_namespace support causes us to get two DIEs for one
12331 variable, both of which are declarations. We want to avoid considering
12332 one to be a specification, so we must test that this DIE is not a
12333 declaration. */
12334 else if (old_die && TREE_STATIC (decl) && ! declaration
bc70bd5e 12335 && get_AT_flag (old_die, DW_AT_declaration) == 1)
6ed29fb8 12336 {
2b553659 12337 /* This is a definition of a C++ class level static. */
023dc493 12338 add_AT_specification (var_die, old_die);
6ed29fb8 12339 if (DECL_NAME (decl))
12340 {
7bd3dcc4 12341 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
69278c24 12342 struct dwarf_file_data * file_index = lookup_filename (s.file);
ec1e49cc 12343
69278c24 12344 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12345 add_AT_file (var_die, DW_AT_decl_file, file_index);
ec1e49cc 12346
69278c24 12347 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
7bd3dcc4 12348 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
6ed29fb8 12349 }
12350 }
30ade641 12351 else
12352 {
12353 add_name_and_src_coords_attributes (var_die, decl);
8c3f468d 12354 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
30ade641 12355 TREE_THIS_VOLATILE (decl), context_die);
ec1e49cc 12356
6542a017 12357 if (TREE_PUBLIC (decl))
12358 add_AT_flag (var_die, DW_AT_external, 1);
ec1e49cc 12359
6542a017 12360 if (DECL_ARTIFICIAL (decl))
12361 add_AT_flag (var_die, DW_AT_artificial, 1);
ec1e49cc 12362
6efd403b 12363 if (TREE_PROTECTED (decl))
12364 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12365 else if (TREE_PRIVATE (decl))
12366 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
30ade641 12367 }
cc324702 12368
12369 if (declaration)
12370 add_AT_flag (var_die, DW_AT_declaration, 1);
f80d1bcd 12371
2b49746a 12372 if (DECL_ABSTRACT (decl) || declaration)
cc324702 12373 equate_decl_number_to_die (decl, var_die);
12374
12375 if (! declaration && ! DECL_ABSTRACT (decl))
30ade641 12376 {
b2025850 12377 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
dc7a29ce 12378 add_pubname (decl, var_die);
30ade641 12379 }
eabb26f3 12380 else
12381 tree_add_const_value_attribute (var_die, decl);
30ade641 12382}
12383
12384/* Generate a DIE to represent a label identifier. */
ec1e49cc 12385
30ade641 12386static void
8ec3a57b 12387gen_label_die (tree decl, dw_die_ref context_die)
30ade641 12388{
19cb6b50 12389 tree origin = decl_ultimate_origin (decl);
15cfae4e 12390 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19cb6b50 12391 rtx insn;
30ade641 12392 char label[MAX_ARTIFICIAL_LABEL_BYTES];
ec1e49cc 12393
30ade641 12394 if (origin != NULL)
ec1e49cc 12395 add_abstract_origin_attribute (lbl_die, origin);
30ade641 12396 else
ec1e49cc 12397 add_name_and_src_coords_attributes (lbl_die, decl);
12398
30ade641 12399 if (DECL_ABSTRACT (decl))
ec1e49cc 12400 equate_decl_number_to_die (decl, lbl_die);
30ade641 12401 else
12402 {
c9f0a9eb 12403 insn = DECL_RTL_IF_SET (decl);
165b3519 12404
12405 /* Deleted labels are programmer specified labels which have been
7ef5b942 12406 eliminated because of various optimizations. We still emit them
165b3519 12407 here so that it is possible to put breakpoints on them. */
c9f0a9eb 12408 if (insn
6d7dc5b9 12409 && (LABEL_P (insn)
12410 || ((NOTE_P (insn)
ad4583d9 12411 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
30ade641 12412 {
f80d1bcd 12413 /* When optimization is enabled (via -O) some parts of the compiler
12414 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
30ade641 12415 represent source-level labels which were explicitly declared by
12416 the user. This really shouldn't be happening though, so catch
12417 it if it ever does happen. */
7bd4f6b6 12418 gcc_assert (!INSN_DELETED_P (insn));
ec1e49cc 12419
1134a028 12420 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
30ade641 12421 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12422 }
12423 }
12424}
12425
44276901 12426/* A helper function for gen_inlined_subroutine_die. Add source coordinate
12427 attributes to the DIE for a block STMT, to describe where the inlined
12428 function was called from. This is similar to add_src_coords_attributes. */
12429
12430static inline void
12431add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12432{
12433 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
44276901 12434
69278c24 12435 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
44276901 12436 add_AT_unsigned (die, DW_AT_call_line, s.line);
12437}
12438
17db73b6 12439
12440/* If STMT's abstract origin is a function declaration and STMT's
12441 first subblock's abstract origin is the function's outermost block,
12442 then we're looking at the main entry point. */
12443static bool
12444is_inlined_entry_point (tree stmt)
12445{
12446 tree decl, block;
12447
12448 if (!stmt || TREE_CODE (stmt) != BLOCK)
12449 return false;
12450
12451 decl = block_ultimate_origin (stmt);
12452
12453 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12454 return false;
12455
12456 block = BLOCK_SUBBLOCKS (stmt);
12457
12458 if (block)
12459 {
12460 if (TREE_CODE (block) != BLOCK)
12461 return false;
12462
12463 block = block_ultimate_origin (block);
12464 }
12465
12466 return block == DECL_INITIAL (decl);
12467}
12468
3ac15270 12469/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12470 Add low_pc and high_pc attributes to the DIE for a block STMT. */
ec1e49cc 12471
3ac15270 12472static inline void
12473add_high_low_attributes (tree stmt, dw_die_ref die)
30ade641 12474{
30ade641 12475 char label[MAX_ARTIFICIAL_LABEL_BYTES];
ec1e49cc 12476
3ac15270 12477 if (BLOCK_FRAGMENT_CHAIN (stmt))
30ade641 12478 {
3ac15270 12479 tree chain;
a36145ca 12480
17db73b6 12481 if (is_inlined_entry_point (stmt))
12482 {
12483 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12484 BLOCK_NUMBER (stmt));
12485 add_AT_lbl_id (die, DW_AT_entry_pc, label);
12486 }
12487
3ac15270 12488 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
a36145ca 12489
3ac15270 12490 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12491 do
a36145ca 12492 {
3ac15270 12493 add_ranges (chain);
12494 chain = BLOCK_FRAGMENT_CHAIN (chain);
a36145ca 12495 }
3ac15270 12496 while (chain);
12497 add_ranges (NULL);
12498 }
12499 else
12500 {
12501 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12502 BLOCK_NUMBER (stmt));
12503 add_AT_lbl_id (die, DW_AT_low_pc, label);
12504 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12505 BLOCK_NUMBER (stmt));
12506 add_AT_lbl_id (die, DW_AT_high_pc, label);
30ade641 12507 }
3ac15270 12508}
12509
12510/* Generate a DIE for a lexical block. */
12511
12512static void
12513gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12514{
12515 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12516
12517 if (! BLOCK_ABSTRACT (stmt))
12518 add_high_low_attributes (stmt, stmt_die);
ec1e49cc 12519
cb371216 12520 decls_for_scope (stmt, stmt_die, depth);
30ade641 12521}
12522
12523/* Generate a DIE for an inlined subprogram. */
ec1e49cc 12524
30ade641 12525static void
8ec3a57b 12526gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
30ade641 12527{
b1682481 12528 tree decl = block_ultimate_origin (stmt);
12529
12530 /* Emit info for the abstract instance first, if we haven't yet. We
12531 must emit this even if the block is abstract, otherwise when we
12532 emit the block below (or elsewhere), we may end up trying to emit
12533 a die whose origin die hasn't been emitted, and crashing. */
12534 dwarf2out_abstract_function (decl);
12535
ec1e49cc 12536 if (! BLOCK_ABSTRACT (stmt))
30ade641 12537 {
19cb6b50 12538 dw_die_ref subr_die
15cfae4e 12539 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
ec1e49cc 12540
db42c2b2 12541 add_abstract_origin_attribute (subr_die, decl);
3ac15270 12542 add_high_low_attributes (stmt, subr_die);
44276901 12543 add_call_src_coords_attributes (stmt, subr_die);
3ac15270 12544
cb371216 12545 decls_for_scope (stmt, subr_die, depth);
a3899bb7 12546 current_function_has_inlines = 1;
30ade641 12547 }
6e1e0aa6 12548 else
12549 /* We may get here if we're the outer block of function A that was
12550 inlined into function B that was inlined into function C. When
12551 generating debugging info for C, dwarf2out_abstract_function(B)
12552 would mark all inlined blocks as abstract, including this one.
12553 So, we wouldn't (and shouldn't) expect labels to be generated
12554 for this one. Instead, just emit debugging info for
12555 declarations within the block. This is particularly important
12556 in the case of initializers of arguments passed from B to us:
12557 if they're statement expressions containing declarations, we
12558 wouldn't generate dies for their abstract variables, and then,
12559 when generating dies for the real variables, we'd die (pun
12560 intended :-) */
12561 gen_lexical_block_die (stmt, context_die, depth);
30ade641 12562}
12563
12564/* Generate a DIE for a field in a record, or structure. */
ec1e49cc 12565
30ade641 12566static void
8ec3a57b 12567gen_field_die (tree decl, dw_die_ref context_die)
30ade641 12568{
443a33a3 12569 dw_die_ref decl_die;
ec1e49cc 12570
443a33a3 12571 if (TREE_TYPE (decl) == error_mark_node)
12572 return;
8ec3a57b 12573
443a33a3 12574 decl_die = new_die (DW_TAG_member, context_die, decl);
30ade641 12575 add_name_and_src_coords_attributes (decl_die, decl);
30ade641 12576 add_type_attribute (decl_die, member_declared_type (decl),
12577 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12578 context_die);
ec1e49cc 12579
30ade641 12580 if (DECL_BIT_FIELD_TYPE (decl))
12581 {
12582 add_byte_size_attribute (decl_die, decl);
12583 add_bit_size_attribute (decl_die, decl);
12584 add_bit_offset_attribute (decl_die, decl);
12585 }
ec1e49cc 12586
6efd403b 12587 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12588 add_data_member_location_attribute (decl_die, decl);
ec1e49cc 12589
6542a017 12590 if (DECL_ARTIFICIAL (decl))
12591 add_AT_flag (decl_die, DW_AT_artificial, 1);
ec1e49cc 12592
6efd403b 12593 if (TREE_PROTECTED (decl))
12594 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12595 else if (TREE_PRIVATE (decl))
12596 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
243f8437 12597
12598 /* Equate decl number to die, so that we can look up this decl later on. */
12599 equate_decl_number_to_die (decl, decl_die);
30ade641 12600}
12601
db42c2b2 12602#if 0
12603/* Don't generate either pointer_type DIEs or reference_type DIEs here.
12604 Use modified_type_die instead.
30ade641 12605 We keep this code here just in case these types of DIEs may be needed to
12606 represent certain things in other languages (e.g. Pascal) someday. */
8c3f468d 12607
30ade641 12608static void
8ec3a57b 12609gen_pointer_type_die (tree type, dw_die_ref context_die)
30ade641 12610{
19cb6b50 12611 dw_die_ref ptr_die
15cfae4e 12612 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
ec1e49cc 12613
30ade641 12614 equate_type_number_to_die (type, ptr_die);
30ade641 12615 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
db42c2b2 12616 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
30ade641 12617}
12618
db42c2b2 12619/* Don't generate either pointer_type DIEs or reference_type DIEs here.
12620 Use modified_type_die instead.
30ade641 12621 We keep this code here just in case these types of DIEs may be needed to
12622 represent certain things in other languages (e.g. Pascal) someday. */
8c3f468d 12623
30ade641 12624static void
8ec3a57b 12625gen_reference_type_die (tree type, dw_die_ref context_die)
30ade641 12626{
19cb6b50 12627 dw_die_ref ref_die
15cfae4e 12628 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
ec1e49cc 12629
30ade641 12630 equate_type_number_to_die (type, ref_die);
30ade641 12631 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
db42c2b2 12632 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
30ade641 12633}
db42c2b2 12634#endif
30ade641 12635
12636/* Generate a DIE for a pointer to a member type. */
8c3f468d 12637
30ade641 12638static void
8ec3a57b 12639gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
30ade641 12640{
19cb6b50 12641 dw_die_ref ptr_die
15cfae4e 12642 = new_die (DW_TAG_ptr_to_member_type,
12643 scope_die_for (type, context_die), type);
ec1e49cc 12644
30ade641 12645 equate_type_number_to_die (type, ptr_die);
30ade641 12646 add_AT_die_ref (ptr_die, DW_AT_containing_type,
6ed29fb8 12647 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
30ade641 12648 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12649}
12650
12651/* Generate the DIE for the compilation unit. */
ec1e49cc 12652
c90bf86c 12653static dw_die_ref
8ec3a57b 12654gen_compile_unit_die (const char *filename)
30ade641 12655{
19cb6b50 12656 dw_die_ref die;
30ade641 12657 char producer[250];
d19bd1f0 12658 const char *language_string = lang_hooks.name;
c90bf86c 12659 int language;
30ade641 12660
15cfae4e 12661 die = new_die (DW_TAG_compile_unit, NULL, NULL);
6ed29fb8 12662
ff279357 12663 if (filename)
12664 {
12665 add_name_attribute (die, filename);
6d042e21 12666 /* Don't add cwd for <built-in>. */
974a92fe 12667 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
ff279357 12668 add_comp_dir_attribute (die);
12669 }
30ade641 12670
12671 sprintf (producer, "%s %s", language_string, version_string);
12672
12673#ifdef MIPS_DEBUGGING_INFO
12674 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12675 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12676 not appear in the producer string, the debugger reaches the conclusion
12677 that the object file is stripped and has no debugging information.
12678 To get the MIPS/SGI debugger to believe that there is debugging
12679 information in the object file, we add a -g to the producer string. */
43f116ae 12680 if (debug_info_level > DINFO_LEVEL_TERSE)
12681 strcat (producer, " -g");
30ade641 12682#endif
12683
c90bf86c 12684 add_AT_string (die, DW_AT_producer, producer);
5b67860b 12685
30ade641 12686 if (strcmp (language_string, "GNU C++") == 0)
c90bf86c 12687 language = DW_LANG_C_plus_plus;
30ade641 12688 else if (strcmp (language_string, "GNU Ada") == 0)
7f2ad96e 12689 language = DW_LANG_Ada95;
5b67860b 12690 else if (strcmp (language_string, "GNU F77") == 0)
c90bf86c 12691 language = DW_LANG_Fortran77;
4ee9c684 12692 else if (strcmp (language_string, "GNU F95") == 0)
12693 language = DW_LANG_Fortran95;
063295fb 12694 else if (strcmp (language_string, "GNU Pascal") == 0)
c90bf86c 12695 language = DW_LANG_Pascal83;
af4d39d8 12696 else if (strcmp (language_string, "GNU Java") == 0)
12697 language = DW_LANG_Java;
bda642f9 12698 else if (strcmp (language_string, "GNU Objective-C") == 0)
12699 language = DW_LANG_ObjC;
12700 else if (strcmp (language_string, "GNU Objective-C++") == 0)
12701 language = DW_LANG_ObjC_plus_plus;
30ade641 12702 else
c90bf86c 12703 language = DW_LANG_C89;
5b67860b 12704
c90bf86c 12705 add_AT_unsigned (die, DW_AT_language, language);
c90bf86c 12706 return die;
30ade641 12707}
12708
404ba76d 12709/* Generate the DIE for a base class. */
ec1e49cc 12710
404ba76d 12711static void
8ec3a57b 12712gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
404ba76d 12713{
15cfae4e 12714 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
ec1e49cc 12715
404ba76d 12716 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12717 add_data_member_location_attribute (die, binfo);
ec1e49cc 12718
57c28194 12719 if (BINFO_VIRTUAL_P (binfo))
404ba76d 12720 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
8c3f468d 12721
95f3173a 12722 if (access == access_public_node)
404ba76d 12723 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
95f3173a 12724 else if (access == access_protected_node)
404ba76d 12725 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12726}
12727
ad87de1e 12728/* Generate a DIE for a class member. */
ec1e49cc 12729
30ade641 12730static void
8ec3a57b 12731gen_member_die (tree type, dw_die_ref context_die)
30ade641 12732{
19cb6b50 12733 tree member;
95f3173a 12734 tree binfo = TYPE_BINFO (type);
e7b3c55c 12735 dw_die_ref child;
ec1e49cc 12736
30ade641 12737 /* If this is not an incomplete type, output descriptions of each of its
12738 members. Note that as we output the DIEs necessary to represent the
12739 members of this record or union type, we will also be trying to output
12740 DIEs to represent the *types* of those members. However the `type'
f80d1bcd 12741 function (above) will specifically avoid generating type DIEs for member
4a82352a 12742 types *within* the list of member DIEs for this (containing) type except
30ade641 12743 for those types (of members) which are explicitly marked as also being
12744 members of this (containing) type themselves. The g++ front- end can
8c3f468d 12745 force any given type to be treated as a member of some other (containing)
12746 type by setting the TYPE_CONTEXT of the given (member) type to point to
12747 the TREE node representing the appropriate (containing) type. */
30ade641 12748
404ba76d 12749 /* First output info about the base classes. */
f6cc6a08 12750 if (binfo)
30ade641 12751 {
046bfc77 12752 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19cb6b50 12753 int i;
f6cc6a08 12754 tree base;
404ba76d 12755
f6cc6a08 12756 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12757 gen_inheritance_die (base,
db77fe17 12758 (accesses ? VEC_index (tree, accesses, i)
95f3173a 12759 : access_public_node), context_die);
30ade641 12760 }
12761
404ba76d 12762 /* Now output info about the data members and type members. */
12763 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
e7b3c55c 12764 {
12765 /* If we thought we were generating minimal debug info for TYPE
12766 and then changed our minds, some of the member declarations
12767 may have already been defined. Don't define them again, but
12768 do put them in the right order. */
12769
12770 child = lookup_decl_die (member);
12771 if (child)
12772 splice_child_die (context_die, child);
12773 else
12774 gen_decl_die (member, context_die);
12775 }
404ba76d 12776
30ade641 12777 /* Now output info about the function members (if any). */
404ba76d 12778 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
e7b3c55c 12779 {
8f80e66d 12780 /* Don't include clones in the member list. */
12781 if (DECL_ABSTRACT_ORIGIN (member))
12782 continue;
12783
e7b3c55c 12784 child = lookup_decl_die (member);
12785 if (child)
12786 splice_child_die (context_die, child);
12787 else
12788 gen_decl_die (member, context_die);
12789 }
30ade641 12790}
12791
e7b3c55c 12792/* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12793 is set, we pretend that the type was never defined, so we only get the
12794 member DIEs needed by later specification DIEs. */
ec1e49cc 12795
30ade641 12796static void
0e4744ac 12797gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12798 enum debug_info_usage usage)
30ade641 12799{
19cb6b50 12800 dw_die_ref type_die = lookup_type_die (type);
12801 dw_die_ref scope_die = 0;
12802 int nested = 0;
e7b3c55c 12803 int complete = (TYPE_SIZE (type)
87ccbd32 12804 && (! TYPE_STUB_DECL (type)
12805 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
e89530cd 12806 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
0e4744ac 12807 complete = complete && should_emit_struct_debug (type, usage);
6542a017 12808
e7b3c55c 12809 if (type_die && ! complete)
6542a017 12810 return;
a3377a8b 12811
ec1e49cc 12812 if (TYPE_CONTEXT (type) != NULL_TREE
e89530cd 12813 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12814 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
a3377a8b 12815 nested = 1;
12816
6efd403b 12817 scope_die = scope_die_for (type, context_die);
a3377a8b 12818
12819 if (! type_die || (nested && scope_die == comp_unit_die))
6542a017 12820 /* First occurrence of type or toplevel definition of nested class. */
30ade641 12821 {
19cb6b50 12822 dw_die_ref old_die = type_die;
ec1e49cc 12823
30ade641 12824 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12825 ? DW_TAG_structure_type : DW_TAG_union_type,
15cfae4e 12826 scope_die, type);
30ade641 12827 equate_type_number_to_die (type, type_die);
6542a017 12828 if (old_die)
023dc493 12829 add_AT_specification (type_die, old_die);
2dfaa9ec 12830 else
12831 add_name_attribute (type_die, type_tag (type));
30ade641 12832 }
752e49ca 12833 else
6542a017 12834 remove_AT (type_die, DW_AT_declaration);
30ade641 12835
12836 /* If this type has been completed, then give it a byte_size attribute and
12837 then give a list of members. */
e89530cd 12838 if (complete && !ns_decl)
30ade641 12839 {
f80d1bcd 12840 /* Prevent infinite recursion in cases where the type of some member of
c83a163c 12841 this type is expressed in terms of this type itself. */
30ade641 12842 TREE_ASM_WRITTEN (type) = 1;
6542a017 12843 add_byte_size_attribute (type_die, type);
0dbd1c74 12844 if (TYPE_STUB_DECL (type) != NULL_TREE)
840b696a 12845 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
ec1e49cc 12846
678d90bb 12847 /* If the first reference to this type was as the return type of an
12848 inline function, then it may not have a parent. Fix this now. */
12849 if (type_die->die_parent == NULL)
12850 add_child_die (scope_die, type_die);
12851
6542a017 12852 push_decl_scope (type);
12853 gen_member_die (type, type_die);
12854 pop_decl_scope ();
ec1e49cc 12855
6efd403b 12856 /* GNU extension: Record what type our vtable lives in. */
12857 if (TYPE_VFIELD (type))
12858 {
12859 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
ec1e49cc 12860
ad5808e7 12861 gen_type_die (vtype, context_die);
12862 add_AT_die_ref (type_die, DW_AT_containing_type,
12863 lookup_type_die (vtype));
6efd403b 12864 }
30ade641 12865 }
752e49ca 12866 else
a4617d03 12867 {
12868 add_AT_flag (type_die, DW_AT_declaration, 1);
a41e1595 12869
ee1cd281 12870 /* We don't need to do this for function-local types. */
cfd66c04 12871 if (TYPE_STUB_DECL (type)
12872 && ! decl_function_context (TYPE_STUB_DECL (type)))
22230dd1 12873 VEC_safe_push (tree, gc, incomplete_types, type);
a4617d03 12874 }
af84796a 12875
12876 if (get_AT (type_die, DW_AT_name))
12877 add_pubtype (type, type_die);
30ade641 12878}
12879
12880/* Generate a DIE for a subroutine _type_. */
ec1e49cc 12881
30ade641 12882static void
8ec3a57b 12883gen_subroutine_type_die (tree type, dw_die_ref context_die)
30ade641 12884{
19cb6b50 12885 tree return_type = TREE_TYPE (type);
12886 dw_die_ref subr_die
15cfae4e 12887 = new_die (DW_TAG_subroutine_type,
12888 scope_die_for (type, context_die), type);
ec1e49cc 12889
30ade641 12890 equate_type_number_to_die (type, subr_die);
12891 add_prototyped_attribute (subr_die, type);
30ade641 12892 add_type_attribute (subr_die, return_type, 0, 0, context_die);
6efd403b 12893 gen_formal_types_die (type, subr_die);
af84796a 12894
12895 if (get_AT (subr_die, DW_AT_name))
12896 add_pubtype (type, subr_die);
30ade641 12897}
12898
2358393e 12899/* Generate a DIE for a type definition. */
ec1e49cc 12900
30ade641 12901static void
8ec3a57b 12902gen_typedef_die (tree decl, dw_die_ref context_die)
30ade641 12903{
19cb6b50 12904 dw_die_ref type_die;
12905 tree origin;
6efd403b 12906
12907 if (TREE_ASM_WRITTEN (decl))
12908 return;
6efd403b 12909
8c3f468d 12910 TREE_ASM_WRITTEN (decl) = 1;
15cfae4e 12911 type_die = new_die (DW_TAG_typedef, context_die, decl);
6efd403b 12912 origin = decl_ultimate_origin (decl);
30ade641 12913 if (origin != NULL)
6efd403b 12914 add_abstract_origin_attribute (type_die, origin);
30ade641 12915 else
12916 {
19cb6b50 12917 tree type;
8c3f468d 12918
30ade641 12919 add_name_and_src_coords_attributes (type_die, decl);
6efd403b 12920 if (DECL_ORIGINAL_TYPE (decl))
12921 {
12922 type = DECL_ORIGINAL_TYPE (decl);
522649bb 12923
7bd4f6b6 12924 gcc_assert (type != TREE_TYPE (decl));
12925 equate_type_number_to_die (TREE_TYPE (decl), type_die);
6efd403b 12926 }
12927 else
12928 type = TREE_TYPE (decl);
8c3f468d 12929
6efd403b 12930 add_type_attribute (type_die, type, TREE_READONLY (decl),
12931 TREE_THIS_VOLATILE (decl), context_die);
30ade641 12932 }
ec1e49cc 12933
30ade641 12934 if (DECL_ABSTRACT (decl))
6efd403b 12935 equate_decl_number_to_die (decl, type_die);
af84796a 12936
12937 if (get_AT (type_die, DW_AT_name))
12938 add_pubtype (decl, type_die);
30ade641 12939}
12940
12941/* Generate a type description DIE. */
ec1e49cc 12942
30ade641 12943static void
0e4744ac 12944gen_type_die_with_usage (tree type, dw_die_ref context_die,
12945 enum debug_info_usage usage)
30ade641 12946{
5c65b85a 12947 int need_pop;
12948
ec1e49cc 12949 if (type == NULL_TREE || type == error_mark_node)
12950 return;
30ade641 12951
6efd403b 12952 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12953 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
f80d1bcd 12954 {
dc346c40 12955 if (TREE_ASM_WRITTEN (type))
12956 return;
12957
637d3308 12958 /* Prevent broken recursion; we can't hand off to the same type. */
7bd4f6b6 12959 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
637d3308 12960
6efd403b 12961 TREE_ASM_WRITTEN (type) = 1;
12962 gen_decl_die (TYPE_NAME (type), context_die);
12963 return;
12964 }
12965
dc346c40 12966 /* We are going to output a DIE to represent the unqualified version
12967 of this type (i.e. without any const or volatile qualifiers) so
12968 get the main variant (i.e. the unqualified version) of this type
12969 now. (Vectors are special because the debugging info is in the
12970 cloned type itself). */
12971 if (TREE_CODE (type) != VECTOR_TYPE)
12972 type = type_main_variant (type);
12973
12974 if (TREE_ASM_WRITTEN (type))
12975 return;
12976
30ade641 12977 switch (TREE_CODE (type))
12978 {
12979 case ERROR_MARK:
12980 break;
12981
12982 case POINTER_TYPE:
12983 case REFERENCE_TYPE:
ad87de1e 12984 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12985 ensures that the gen_type_die recursion will terminate even if the
12986 type is recursive. Recursive types are possible in Ada. */
12987 /* ??? We could perhaps do this for all types before the switch
12988 statement. */
12989 TREE_ASM_WRITTEN (type) = 1;
12990
30ade641 12991 /* For these types, all that is required is that we output a DIE (or a
c83a163c 12992 set of DIEs) to represent the "basis" type. */
0e4744ac 12993 gen_type_die_with_usage (TREE_TYPE (type), context_die,
12994 DINFO_USAGE_IND_USE);
30ade641 12995 break;
12996
12997 case OFFSET_TYPE:
f80d1bcd 12998 /* This code is used for C++ pointer-to-data-member types.
ec1e49cc 12999 Output a description of the relevant class type. */
0e4744ac 13000 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13001 DINFO_USAGE_IND_USE);
ec1e49cc 13002
30ade641 13003 /* Output a description of the type of the object pointed to. */
0e4744ac 13004 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13005 DINFO_USAGE_IND_USE);
ec1e49cc 13006
30ade641 13007 /* Now output a DIE to represent this pointer-to-data-member type
c83a163c 13008 itself. */
30ade641 13009 gen_ptr_to_mbr_type_die (type, context_die);
13010 break;
13011
30ade641 13012 case FUNCTION_TYPE:
13013 /* Force out return type (in case it wasn't forced out already). */
0e4744ac 13014 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13015 DINFO_USAGE_DIR_USE);
30ade641 13016 gen_subroutine_type_die (type, context_die);
13017 break;
13018
13019 case METHOD_TYPE:
13020 /* Force out return type (in case it wasn't forced out already). */
0e4744ac 13021 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13022 DINFO_USAGE_DIR_USE);
30ade641 13023 gen_subroutine_type_die (type, context_die);
13024 break;
13025
13026 case ARRAY_TYPE:
63bf54cf 13027 gen_array_type_die (type, context_die);
30ade641 13028 break;
13029
e2ea7e3a 13030 case VECTOR_TYPE:
634906d6 13031 gen_array_type_die (type, context_die);
e2ea7e3a 13032 break;
13033
30ade641 13034 case ENUMERAL_TYPE:
13035 case RECORD_TYPE:
13036 case UNION_TYPE:
13037 case QUAL_UNION_TYPE:
8c3f468d 13038 /* If this is a nested type whose containing class hasn't been written
c83a163c 13039 out yet, writing it out will cover this one, too. This does not apply
13040 to instantiations of member class templates; they need to be added to
13041 the containing class as they are generated. FIXME: This hurts the
13042 idea of combining type decls from multiple TUs, since we can't predict
13043 what set of template instantiations we'll get. */
a3377a8b 13044 if (TYPE_CONTEXT (type)
5ef8d04d 13045 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
a3377a8b 13046 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
6efd403b 13047 {
0e4744ac 13048 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
6efd403b 13049
5c65b85a 13050 if (TREE_ASM_WRITTEN (type))
6efd403b 13051 return;
13052
13053 /* If that failed, attach ourselves to the stub. */
13054 push_decl_scope (TYPE_CONTEXT (type));
13055 context_die = lookup_type_die (TYPE_CONTEXT (type));
5c65b85a 13056 need_pop = 1;
6efd403b 13057 }
5c65b85a 13058 else
e89530cd 13059 {
13060 declare_in_namespace (type, context_die);
13061 need_pop = 0;
13062 }
6efd403b 13063
13064 if (TREE_CODE (type) == ENUMERAL_TYPE)
3d9b511b 13065 {
13066 /* This might have been written out by the call to
13067 declare_in_namespace. */
13068 if (!TREE_ASM_WRITTEN (type))
13069 gen_enumeration_type_die (type, context_die);
13070 }
30ade641 13071 else
0e4744ac 13072 gen_struct_or_union_type_die (type, context_die, usage);
752e49ca 13073
5c65b85a 13074 if (need_pop)
6efd403b 13075 pop_decl_scope ();
13076
752e49ca 13077 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
a3377a8b 13078 it up if it is ever completed. gen_*_type_die will set it for us
13079 when appropriate. */
13080 return;
30ade641 13081
13082 case VOID_TYPE:
13083 case INTEGER_TYPE:
13084 case REAL_TYPE:
13085 case COMPLEX_TYPE:
13086 case BOOLEAN_TYPE:
30ade641 13087 /* No DIEs needed for fundamental types. */
13088 break;
13089
13090 case LANG_TYPE:
13091 /* No Dwarf representation currently defined. */
13092 break;
13093
13094 default:
7bd4f6b6 13095 gcc_unreachable ();
30ade641 13096 }
13097
13098 TREE_ASM_WRITTEN (type) = 1;
13099}
13100
0e4744ac 13101static void
13102gen_type_die (tree type, dw_die_ref context_die)
13103{
13104 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13105}
13106
30ade641 13107/* Generate a DIE for a tagged type instantiation. */
ec1e49cc 13108
30ade641 13109static void
8ec3a57b 13110gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
30ade641 13111{
ec1e49cc 13112 if (type == NULL_TREE || type == error_mark_node)
13113 return;
30ade641 13114
3398e91d 13115 /* We are going to output a DIE to represent the unqualified version of
30ade641 13116 this type (i.e. without any const or volatile qualifiers) so make sure
13117 that we have the main variant (i.e. the unqualified version) of this
13118 type now. */
7bd4f6b6 13119 gcc_assert (type == type_main_variant (type));
30ade641 13120
fa5a8144 13121 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
b2ca6017 13122 an instance of an unresolved type. */
f80d1bcd 13123
30ade641 13124 switch (TREE_CODE (type))
13125 {
13126 case ERROR_MARK:
13127 break;
13128
13129 case ENUMERAL_TYPE:
13130 gen_inlined_enumeration_type_die (type, context_die);
13131 break;
13132
13133 case RECORD_TYPE:
13134 gen_inlined_structure_type_die (type, context_die);
13135 break;
13136
13137 case UNION_TYPE:
13138 case QUAL_UNION_TYPE:
13139 gen_inlined_union_type_die (type, context_die);
13140 break;
13141
13142 default:
7bd4f6b6 13143 gcc_unreachable ();
30ade641 13144 }
13145}
13146
13147/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13148 things which are local to the given block. */
ec1e49cc 13149
30ade641 13150static void
8ec3a57b 13151gen_block_die (tree stmt, dw_die_ref context_die, int depth)
30ade641 13152{
19cb6b50 13153 int must_output_die = 0;
13154 tree origin;
13155 tree decl;
13156 enum tree_code origin_code;
30ade641 13157
7c0a8197 13158 /* Ignore blocks that are NULL. */
13159 if (stmt == NULL_TREE)
ec1e49cc 13160 return;
30ade641 13161
a36145ca 13162 /* If the block is one fragment of a non-contiguous block, do not
13163 process the variables, since they will have been done by the
13164 origin block. Do process subblocks. */
13165 if (BLOCK_FRAGMENT_ORIGIN (stmt))
13166 {
13167 tree sub;
13168
8c3f468d 13169 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
a36145ca 13170 gen_block_die (sub, context_die, depth + 1);
8c3f468d 13171
a36145ca 13172 return;
13173 }
13174
30ade641 13175 /* Determine the "ultimate origin" of this block. This block may be an
13176 inlined instance of an inlined instance of inline function, so we have
13177 to trace all of the way back through the origin chain to find out what
13178 sort of node actually served as the original seed for the creation of
13179 the current block. */
13180 origin = block_ultimate_origin (stmt);
13181 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13182
13183 /* Determine if we need to output any Dwarf DIEs at all to represent this
13184 block. */
13185 if (origin_code == FUNCTION_DECL)
ec1e49cc 13186 /* The outer scopes for inlinings *must* always be represented. We
13187 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
13188 must_output_die = 1;
30ade641 13189 else
13190 {
13191 /* In the case where the current block represents an inlining of the
c83a163c 13192 "body block" of an inline function, we must *NOT* output any DIE for
13193 this block because we have already output a DIE to represent the whole
13194 inlined function scope and the "body block" of any function doesn't
13195 really represent a different scope according to ANSI C rules. So we
13196 check here to make sure that this block does not represent a "body
13197 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
cb371216 13198 if (! is_body_block (origin ? origin : stmt))
30ade641 13199 {
13200 /* Determine if this block directly contains any "significant"
13201 local declarations which we will need to output DIEs for. */
13202 if (debug_info_level > DINFO_LEVEL_TERSE)
ec1e49cc 13203 /* We are not in terse mode so *any* local declaration counts
13204 as being a "significant" one. */
61a9389f 13205 must_output_die = (BLOCK_VARS (stmt) != NULL
13206 && (TREE_USED (stmt)
7c0a8197 13207 || TREE_ASM_WRITTEN (stmt)
13208 || BLOCK_ABSTRACT (stmt)));
30ade641 13209 else
ec1e49cc 13210 /* We are in terse mode, so only local (nested) function
13211 definitions count as "significant" local declarations. */
13212 for (decl = BLOCK_VARS (stmt);
13213 decl != NULL; decl = TREE_CHAIN (decl))
13214 if (TREE_CODE (decl) == FUNCTION_DECL
13215 && DECL_INITIAL (decl))
30ade641 13216 {
ec1e49cc 13217 must_output_die = 1;
13218 break;
30ade641 13219 }
30ade641 13220 }
13221 }
13222
13223 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13224 DIE for any block which contains no significant local declarations at
13225 all. Rather, in such cases we just call `decls_for_scope' so that any
13226 needed Dwarf info for any sub-blocks will get properly generated. Note
13227 that in terse mode, our definition of what constitutes a "significant"
13228 local declaration gets restricted to include only inlined function
13229 instances and local (nested) function definitions. */
13230 if (must_output_die)
13231 {
13232 if (origin_code == FUNCTION_DECL)
ec1e49cc 13233 gen_inlined_subroutine_die (stmt, context_die, depth);
30ade641 13234 else
ec1e49cc 13235 gen_lexical_block_die (stmt, context_die, depth);
30ade641 13236 }
13237 else
cb371216 13238 decls_for_scope (stmt, context_die, depth);
30ade641 13239}
13240
13241/* Generate all of the decls declared within a given scope and (recursively)
9e042f31 13242 all of its sub-blocks. */
ec1e49cc 13243
30ade641 13244static void
8ec3a57b 13245decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
30ade641 13246{
19cb6b50 13247 tree decl;
13248 tree subblocks;
ec1e49cc 13249
7c0a8197 13250 /* Ignore NULL blocks. */
13251 if (stmt == NULL_TREE)
ec1e49cc 13252 return;
13253
7c0a8197 13254 if (TREE_USED (stmt))
30ade641 13255 {
7c0a8197 13256 /* Output the DIEs to represent all of the data objects and typedefs
13257 declared directly within this block but not within any nested
13258 sub-blocks. Also, nested function and tag DIEs have been
13259 generated with a parent of NULL; fix that up now. */
13260 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13261 {
13262 dw_die_ref die;
61a9389f 13263
7c0a8197 13264 if (TREE_CODE (decl) == FUNCTION_DECL)
13265 die = lookup_decl_die (decl);
13266 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13267 die = lookup_type_die (TREE_TYPE (decl));
13268 else
13269 die = NULL;
61a9389f 13270
7c0a8197 13271 if (die != NULL && die->die_parent == NULL)
13272 add_child_die (context_die, die);
127d7f21 13273 /* Do not produce debug information for static variables since
13274 these might be optimized out. We are called for these later
1d416bd7 13275 in varpool_analyze_pending_decls. */
127d7f21 13276 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13277 ;
7c0a8197 13278 else
13279 gen_decl_die (decl, context_die);
13280 }
30ade641 13281 }
13282
e883780d 13283 /* If we're at -g1, we're not interested in subblocks. */
13284 if (debug_info_level <= DINFO_LEVEL_TERSE)
13285 return;
13286
30ade641 13287 /* Output the DIEs to represent all sub-blocks (and the items declared
13288 therein) of this block. */
13289 for (subblocks = BLOCK_SUBBLOCKS (stmt);
13290 subblocks != NULL;
13291 subblocks = BLOCK_CHAIN (subblocks))
ec1e49cc 13292 gen_block_die (subblocks, context_die, depth + 1);
30ade641 13293}
13294
6efd403b 13295/* Is this a typedef we can avoid emitting? */
ec1e49cc 13296
13297static inline int
8ec3a57b 13298is_redundant_typedef (tree decl)
6efd403b 13299{
13300 if (TYPE_DECL_IS_STUB (decl))
13301 return 1;
ec1e49cc 13302
6efd403b 13303 if (DECL_ARTIFICIAL (decl)
13304 && DECL_CONTEXT (decl)
13305 && is_tagged_type (DECL_CONTEXT (decl))
13306 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13307 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13308 /* Also ignore the artificial member typedef for the class name. */
13309 return 1;
ec1e49cc 13310
6efd403b 13311 return 0;
13312}
13313
89f18f73 13314/* Returns the DIE for decl. A DIE will always be returned. */
2b49746a 13315
13316static dw_die_ref
13317force_decl_die (tree decl)
13318{
13319 dw_die_ref decl_die;
13320 unsigned saved_external_flag;
13321 tree save_fn = NULL_TREE;
13322 decl_die = lookup_decl_die (decl);
13323 if (!decl_die)
13324 {
13325 dw_die_ref context_die;
13326 tree decl_context = DECL_CONTEXT (decl);
13327 if (decl_context)
13328 {
13329 /* Find die that represents this context. */
13330 if (TYPE_P (decl_context))
13331 context_die = force_type_die (decl_context);
13332 else
13333 context_die = force_decl_die (decl_context);
13334 }
13335 else
13336 context_die = comp_unit_die;
13337
dcfa82ba 13338 decl_die = lookup_decl_die (decl);
13339 if (decl_die)
13340 return decl_die;
13341
2b49746a 13342 switch (TREE_CODE (decl))
13343 {
13344 case FUNCTION_DECL:
13345 /* Clear current_function_decl, so that gen_subprogram_die thinks
13346 that this is a declaration. At this point, we just want to force
13347 declaration die. */
13348 save_fn = current_function_decl;
13349 current_function_decl = NULL_TREE;
13350 gen_subprogram_die (decl, context_die);
8ff30ff6 13351 current_function_decl = save_fn;
2b49746a 13352 break;
13353
13354 case VAR_DECL:
13355 /* Set external flag to force declaration die. Restore it after
13356 gen_decl_die() call. */
13357 saved_external_flag = DECL_EXTERNAL (decl);
13358 DECL_EXTERNAL (decl) = 1;
13359 gen_decl_die (decl, context_die);
13360 DECL_EXTERNAL (decl) = saved_external_flag;
13361 break;
13362
13363 case NAMESPACE_DECL:
13364 dwarf2out_decl (decl);
13365 break;
13366
13367 default:
7bd4f6b6 13368 gcc_unreachable ();
2b49746a 13369 }
8ff30ff6 13370
89f18f73 13371 /* We should be able to find the DIE now. */
2b49746a 13372 if (!decl_die)
13373 decl_die = lookup_decl_die (decl);
7bd4f6b6 13374 gcc_assert (decl_die);
2b49746a 13375 }
8ff30ff6 13376
2b49746a 13377 return decl_die;
13378}
e89530cd 13379
a357c7c2 13380/* Returns the DIE for TYPE, that must not be a base type. A DIE is
13381 always returned. */
e89530cd 13382
13383static dw_die_ref
2b49746a 13384force_type_die (tree type)
e89530cd 13385{
2b49746a 13386 dw_die_ref type_die;
e89530cd 13387
eb550b19 13388 type_die = lookup_type_die (type);
2b49746a 13389 if (!type_die)
13390 {
13391 dw_die_ref context_die;
13392 if (TYPE_CONTEXT (type))
dcfa82ba 13393 {
13394 if (TYPE_P (TYPE_CONTEXT (type)))
13395 context_die = force_type_die (TYPE_CONTEXT (type));
13396 else
13397 context_die = force_decl_die (TYPE_CONTEXT (type));
13398 }
2b49746a 13399 else
13400 context_die = comp_unit_die;
e89530cd 13401
dcfa82ba 13402 type_die = lookup_type_die (type);
13403 if (type_die)
13404 return type_die;
2b49746a 13405 gen_type_die (type, context_die);
eb550b19 13406 type_die = lookup_type_die (type);
7bd4f6b6 13407 gcc_assert (type_die);
2b49746a 13408 }
13409 return type_die;
e89530cd 13410}
13411
13412/* Force out any required namespaces to be able to output DECL,
13413 and return the new context_die for it, if it's changed. */
13414
13415static dw_die_ref
13416setup_namespace_context (tree thing, dw_die_ref context_die)
13417{
ce45a448 13418 tree context = (DECL_P (thing)
13419 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
e89530cd 13420 if (context && TREE_CODE (context) == NAMESPACE_DECL)
8b332087 13421 /* Force out the namespace. */
2b49746a 13422 context_die = force_decl_die (context);
e89530cd 13423
13424 return context_die;
13425}
13426
13427/* Emit a declaration DIE for THING (which is either a DECL or a tagged
13428 type) within its namespace, if appropriate.
13429
13430 For compatibility with older debuggers, namespace DIEs only contain
13431 declarations; all definitions are emitted at CU scope. */
13432
13433static void
13434declare_in_namespace (tree thing, dw_die_ref context_die)
13435{
13436 dw_die_ref ns_context;
13437
13438 if (debug_info_level <= DINFO_LEVEL_TERSE)
13439 return;
13440
d799a629 13441 /* If this decl is from an inlined function, then don't try to emit it in its
13442 namespace, as we will get confused. It would have already been emitted
13443 when the abstract instance of the inline function was emitted anyways. */
13444 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13445 return;
13446
e89530cd 13447 ns_context = setup_namespace_context (thing, context_die);
13448
13449 if (ns_context != context_die)
13450 {
13451 if (DECL_P (thing))
13452 gen_decl_die (thing, ns_context);
13453 else
13454 gen_type_die (thing, ns_context);
13455 }
13456}
13457
8b332087 13458/* Generate a DIE for a namespace or namespace alias. */
e89530cd 13459
13460static void
13461gen_namespace_die (tree decl)
13462{
13463 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13464
13465 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21dda4ee 13466 they are an alias of. */
e89530cd 13467 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13468 {
8b332087 13469 /* Output a real namespace. */
e89530cd 13470 dw_die_ref namespace_die
13471 = new_die (DW_TAG_namespace, context_die, decl);
13472 add_name_and_src_coords_attributes (namespace_die, decl);
13473 equate_decl_number_to_die (decl, namespace_die);
13474 }
13475 else
13476 {
8b332087 13477 /* Output a namespace alias. */
e89530cd 13478
8b332087 13479 /* Force out the namespace we are an alias of, if necessary. */
e89530cd 13480 dw_die_ref origin_die
2b49746a 13481 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
e89530cd 13482
8b332087 13483 /* Now create the namespace alias DIE. */
e89530cd 13484 dw_die_ref namespace_die
13485 = new_die (DW_TAG_imported_declaration, context_die, decl);
13486 add_name_and_src_coords_attributes (namespace_die, decl);
13487 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13488 equate_decl_number_to_die (decl, namespace_die);
13489 }
13490}
13491
30ade641 13492/* Generate Dwarf debug information for a decl described by DECL. */
ec1e49cc 13493
30ade641 13494static void
8ec3a57b 13495gen_decl_die (tree decl, dw_die_ref context_die)
30ade641 13496{
19cb6b50 13497 tree origin;
ec1e49cc 13498
90f973ed 13499 if (DECL_P (decl) && DECL_IGNORED_P (decl))
ec1e49cc 13500 return;
30ade641 13501
30ade641 13502 switch (TREE_CODE (decl))
13503 {
8c3f468d 13504 case ERROR_MARK:
13505 break;
13506
30ade641 13507 case CONST_DECL:
f80d1bcd 13508 /* The individual enumerators of an enum type get output when we output
c83a163c 13509 the Dwarf representation of the relevant enum type itself. */
30ade641 13510 break;
13511
13512 case FUNCTION_DECL:
cc324702 13513 /* Don't output any DIEs to represent mere function declarations,
13514 unless they are class members or explicit block externs. */
13515 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14b40abb 13516 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
ec1e49cc 13517 break;
6ed29fb8 13518
4ee9c684 13519#if 0
13520 /* FIXME */
13521 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13522 on local redeclarations of global functions. That seems broken. */
13523 if (current_function_decl != decl)
13524 /* This is only a declaration. */;
13525#endif
13526
8f80e66d 13527 /* If we're emitting a clone, emit info for the abstract instance. */
13528 if (DECL_ORIGIN (decl) != decl)
13529 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
8c3f468d 13530
0dbc398a 13531 /* If we're emitting an out-of-line copy of an inline function,
13532 emit info for the abstract instance and set up to refer to it. */
5bd74231 13533 else if (cgraph_function_possibly_inlined_p (decl)
13534 && ! DECL_ABSTRACT (decl)
e89530cd 13535 && ! class_or_namespace_scope_p (context_die)
8f80e66d 13536 /* dwarf2out_abstract_function won't emit a die if this is just
13537 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13538 that case, because that works only if we have a die. */
13539 && DECL_INITIAL (decl) != NULL_TREE)
0dbc398a 13540 {
f414ade2 13541 dwarf2out_abstract_function (decl);
0dbc398a 13542 set_decl_origin_self (decl);
13543 }
8c3f468d 13544
8f80e66d 13545 /* Otherwise we're emitting the primary DIE for this decl. */
13546 else if (debug_info_level > DINFO_LEVEL_TERSE)
6efd403b 13547 {
13548 /* Before we describe the FUNCTION_DECL itself, make sure that we
13549 have described its return type. */
13550 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13551
5134c73b 13552 /* And its virtual context. */
13553 if (DECL_VINDEX (decl) != NULL_TREE)
13554 gen_type_die (DECL_CONTEXT (decl), context_die);
13555
6efd403b 13556 /* And its containing type. */
13557 origin = decl_class_context (decl);
ec1e49cc 13558 if (origin != NULL_TREE)
e7b3c55c 13559 gen_type_die_for_member (origin, decl, context_die);
e89530cd 13560
13561 /* And its containing namespace. */
13562 declare_in_namespace (decl, context_die);
6efd403b 13563 }
30ade641 13564
13565 /* Now output a DIE to represent the function itself. */
13566 gen_subprogram_die (decl, context_die);
13567 break;
13568
13569 case TYPE_DECL:
13570 /* If we are in terse mode, don't generate any DIEs to represent any
c83a163c 13571 actual typedefs. */
30ade641 13572 if (debug_info_level <= DINFO_LEVEL_TERSE)
43f116ae 13573 break;
30ade641 13574
8c3f468d 13575 /* In the special case of a TYPE_DECL node representing the declaration
c83a163c 13576 of some type tag, if the given TYPE_DECL is marked as having been
13577 instantiated from some other (original) TYPE_DECL node (e.g. one which
13578 was generated within the original definition of an inline function) we
13579 have to generate a special (abbreviated) DW_TAG_structure_type,
13580 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
5134c73b 13581 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
30ade641 13582 {
13583 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13584 break;
13585 }
30ade641 13586
6efd403b 13587 if (is_redundant_typedef (decl))
13588 gen_type_die (TREE_TYPE (decl), context_die);
13589 else
ec1e49cc 13590 /* Output a DIE to represent the typedef itself. */
13591 gen_typedef_die (decl, context_die);
30ade641 13592 break;
13593
13594 case LABEL_DECL:
13595 if (debug_info_level >= DINFO_LEVEL_NORMAL)
ec1e49cc 13596 gen_label_die (decl, context_die);
30ade641 13597 break;
13598
13599 case VAR_DECL:
4ee9c684 13600 case RESULT_DECL:
30ade641 13601 /* If we are in terse mode, don't generate any DIEs to represent any
c83a163c 13602 variable declarations or definitions. */
30ade641 13603 if (debug_info_level <= DINFO_LEVEL_TERSE)
ec1e49cc 13604 break;
30ade641 13605
13606 /* Output any DIEs that are needed to specify the type of this data
c83a163c 13607 object. */
30ade641 13608 gen_type_die (TREE_TYPE (decl), context_die);
13609
6efd403b 13610 /* And its containing type. */
13611 origin = decl_class_context (decl);
ec1e49cc 13612 if (origin != NULL_TREE)
e7b3c55c 13613 gen_type_die_for_member (origin, decl, context_die);
6efd403b 13614
e89530cd 13615 /* And its containing namespace. */
13616 declare_in_namespace (decl, context_die);
13617
30ade641 13618 /* Now output the DIE to represent the data object itself. This gets
c83a163c 13619 complicated because of the possibility that the VAR_DECL really
13620 represents an inlined instance of a formal parameter for an inline
13621 function. */
30ade641 13622 origin = decl_ultimate_origin (decl);
ec1e49cc 13623 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13624 gen_formal_parameter_die (decl, context_die);
30ade641 13625 else
ec1e49cc 13626 gen_variable_die (decl, context_die);
30ade641 13627 break;
13628
13629 case FIELD_DECL:
8c3f468d 13630 /* Ignore the nameless fields that are used to skip bits but handle C++
dbb28acc 13631 anonymous unions and structs. */
ec1e49cc 13632 if (DECL_NAME (decl) != NULL_TREE
dbb28acc 13633 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13634 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
30ade641 13635 {
13636 gen_type_die (member_declared_type (decl), context_die);
13637 gen_field_die (decl, context_die);
13638 }
13639 break;
13640
13641 case PARM_DECL:
13642 gen_type_die (TREE_TYPE (decl), context_die);
13643 gen_formal_parameter_die (decl, context_die);
13644 break;
13645
5c65b85a 13646 case NAMESPACE_DECL:
e89530cd 13647 gen_namespace_die (decl);
5c65b85a 13648 break;
13649
30ade641 13650 default:
7bd4f6b6 13651 /* Probably some frontend-internal decl. Assume we don't care. */
13652 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13653 break;
30ade641 13654 }
30ade641 13655}
13656\f
8c3f468d 13657/* Output debug information for global decl DECL. Called from toplev.c after
c37d72e9 13658 compilation proper has finished. */
8c3f468d 13659
c37d72e9 13660static void
8ec3a57b 13661dwarf2out_global_decl (tree decl)
c37d72e9 13662{
13663 /* Output DWARF2 information for file-scope tentative data object
8c3f468d 13664 declarations, file-scope (extern) function declarations (which had no
13665 corresponding body) and file-scope tagged type declarations and
13666 definitions which have not yet been forced out. */
c37d72e9 13667 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13668 dwarf2out_decl (decl);
13669}
13670
73ae3ef7 13671/* Output debug information for type decl DECL. Called from toplev.c
13672 and from language front ends (to record built-in types). */
13673static void
13674dwarf2out_type_decl (tree decl, int local)
13675{
13676 if (!local)
13677 dwarf2out_decl (decl);
13678}
13679
8ff30ff6 13680/* Output debug information for imported module or decl. */
13681
2b49746a 13682static void
13683dwarf2out_imported_module_or_decl (tree decl, tree context)
13684{
13685 dw_die_ref imported_die, at_import_die;
13686 dw_die_ref scope_die;
7bd3dcc4 13687 expanded_location xloc;
8ff30ff6 13688
2b49746a 13689 if (debug_info_level <= DINFO_LEVEL_TERSE)
13690 return;
13691
7bd4f6b6 13692 gcc_assert (decl);
2b49746a 13693
13694 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
8ff30ff6 13695 We need decl DIE for reference and scope die. First, get DIE for the decl
2b49746a 13696 itself. */
13697
13698 /* Get the scope die for decl context. Use comp_unit_die for global module
13699 or decl. If die is not found for non globals, force new die. */
13700 if (!context)
13701 scope_die = comp_unit_die;
13702 else if (TYPE_P (context))
0e4744ac 13703 {
13704 if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13705 return;
2b49746a 13706 scope_die = force_type_die (context);
0e4744ac 13707 }
2b49746a 13708 else
13709 scope_die = force_decl_die (context);
13710
cdcf9499 13711 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13712 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
a357c7c2 13713 {
13714 if (is_base_type (TREE_TYPE (decl)))
13715 at_import_die = base_type_die (TREE_TYPE (decl));
13716 else
13717 at_import_die = force_type_die (TREE_TYPE (decl));
13718 }
2b49746a 13719 else
d4946992 13720 {
13721 at_import_die = lookup_decl_die (decl);
13722 if (!at_import_die)
13723 {
13724 /* If we're trying to avoid duplicate debug info, we may not have
13725 emitted the member decl for this field. Emit it now. */
13726 if (TREE_CODE (decl) == FIELD_DECL)
13727 {
13728 tree type = DECL_CONTEXT (decl);
13729 dw_die_ref type_context_die;
13730
13731 if (TYPE_CONTEXT (type))
13732 if (TYPE_P (TYPE_CONTEXT (type)))
0e4744ac 13733 {
13734 if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13735 DINFO_USAGE_DIR_USE))
13736 return;
d4946992 13737 type_context_die = force_type_die (TYPE_CONTEXT (type));
0e4744ac 13738 }
d4946992 13739 else
13740 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13741 else
13742 type_context_die = comp_unit_die;
13743 gen_type_die_for_member (type, decl, type_context_die);
13744 }
13745 at_import_die = force_decl_die (decl);
13746 }
13747 }
8ff30ff6 13748
13749 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
2b49746a 13750 if (TREE_CODE (decl) == NAMESPACE_DECL)
13751 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13752 else
13753 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
7bd3dcc4 13754
13755 xloc = expand_location (input_location);
69278c24 13756 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
7bd3dcc4 13757 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
2b49746a 13758 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13759}
13760
ec1e49cc 13761/* Write the debugging output for DECL. */
13762
30ade641 13763void
8ec3a57b 13764dwarf2out_decl (tree decl)
30ade641 13765{
19cb6b50 13766 dw_die_ref context_die = comp_unit_die;
464217f3 13767
30ade641 13768 switch (TREE_CODE (decl))
13769 {
8c3f468d 13770 case ERROR_MARK:
13771 return;
13772
30ade641 13773 case FUNCTION_DECL:
30ade641 13774 /* What we would really like to do here is to filter out all mere
c83a163c 13775 file-scope declarations of file-scope functions which are never
13776 referenced later within this translation unit (and keep all of ones
13777 that *are* referenced later on) but we aren't clairvoyant, so we have
13778 no idea which functions will be referenced in the future (i.e. later
13779 on within the current translation unit). So here we just ignore all
13780 file-scope function declarations which are not also definitions. If
13781 and when the debugger needs to know something about these functions,
13782 it will have to hunt around and find the DWARF information associated
13783 with the definition of the function.
8c3f468d 13784
13785 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
c83a163c 13786 nodes represent definitions and which ones represent mere
13787 declarations. We have to check DECL_INITIAL instead. That's because
13788 the C front-end supports some weird semantics for "extern inline"
13789 function definitions. These can get inlined within the current
77aa6362 13790 translation unit (and thus, we need to generate Dwarf info for their
c83a163c 13791 abstract instances so that the Dwarf info for the concrete inlined
13792 instances can have something to refer to) but the compiler never
13793 generates any out-of-lines instances of such things (despite the fact
13794 that they *are* definitions).
8c3f468d 13795
13796 The important point is that the C front-end marks these "extern
13797 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13798 them anyway. Note that the C++ front-end also plays some similar games
13799 for inline function definitions appearing within include files which
13800 also contain `#pragma interface' pragmas. */
30ade641 13801 if (DECL_INITIAL (decl) == NULL_TREE)
34425fdc 13802 return;
464217f3 13803
bf1e7d9a 13804 /* If we're a nested function, initially use a parent of NULL; if we're
13805 a plain function, this will be fixed up in decls_for_scope. If
13806 we're a method, it will be ignored, since we already have a DIE. */
e883780d 13807 if (decl_function_context (decl)
13808 /* But if we're in terse mode, we don't care about scope. */
13809 && debug_info_level > DINFO_LEVEL_TERSE)
bf1e7d9a 13810 context_die = NULL;
30ade641 13811 break;
13812
13813 case VAR_DECL:
f80d1bcd 13814 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
c83a163c 13815 declaration and if the declaration was never even referenced from
13816 within this entire compilation unit. We suppress these DIEs in
13817 order to save space in the .debug section (by eliminating entries
13818 which are probably useless). Note that we must not suppress
13819 block-local extern declarations (whether used or not) because that
13820 would screw-up the debugger's name lookup mechanism and cause it to
13821 miss things which really ought to be in scope at a given point. */
30ade641 13822 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
ec1e49cc 13823 return;
30ade641 13824
127d7f21 13825 /* For local statics lookup proper context die. */
13826 if (TREE_STATIC (decl) && decl_function_context (decl))
13827 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13828
30ade641 13829 /* If we are in terse mode, don't generate any DIEs to represent any
c83a163c 13830 variable declarations or definitions. */
30ade641 13831 if (debug_info_level <= DINFO_LEVEL_TERSE)
ec1e49cc 13832 return;
30ade641 13833 break;
13834
e89530cd 13835 case NAMESPACE_DECL:
13836 if (debug_info_level <= DINFO_LEVEL_TERSE)
13837 return;
13838 if (lookup_decl_die (decl) != NULL)
61a9389f 13839 return;
e89530cd 13840 break;
13841
30ade641 13842 case TYPE_DECL:
ee536dac 13843 /* Don't emit stubs for types unless they are needed by other DIEs. */
13844 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13845 return;
13846
30ade641 13847 /* Don't bother trying to generate any DIEs to represent any of the
c83a163c 13848 normal built-in types for the language we are compiling. */
7bd3dcc4 13849 if (DECL_IS_BUILTIN (decl))
6efd403b 13850 {
13851 /* OK, we need to generate one for `bool' so GDB knows what type
c83a163c 13852 comparisons have. */
bda642f9 13853 if (is_cxx ()
90f973ed 13854 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13855 && ! DECL_IGNORED_P (decl))
6efd403b 13856 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
ec1e49cc 13857
6efd403b 13858 return;
13859 }
30ade641 13860
464217f3 13861 /* If we are in terse mode, don't generate any DIEs for types. */
30ade641 13862 if (debug_info_level <= DINFO_LEVEL_TERSE)
43f116ae 13863 return;
464217f3 13864
13865 /* If we're a function-scope tag, initially use a parent of NULL;
13866 this will be fixed up in decls_for_scope. */
13867 if (decl_function_context (decl))
8a8bfbe7 13868 context_die = NULL;
464217f3 13869
30ade641 13870 break;
13871
13872 default:
13873 return;
13874 }
13875
464217f3 13876 gen_decl_die (decl, context_die);
30ade641 13877}
13878
13879/* Output a marker (i.e. a label) for the beginning of the generated code for
13880 a lexical block. */
ec1e49cc 13881
1dff614c 13882static void
8ec3a57b 13883dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13884 unsigned int blocknum)
30ade641 13885{
2f14b1f9 13886 switch_to_section (current_function_section ());
64e17633 13887 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
30ade641 13888}
13889
13890/* Output a marker (i.e. a label) for the end of the generated code for a
13891 lexical block. */
ec1e49cc 13892
1dff614c 13893static void
8ec3a57b 13894dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
30ade641 13895{
2f14b1f9 13896 switch_to_section (current_function_section ());
64e17633 13897 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
30ade641 13898}
13899
0a78547b 13900/* Returns nonzero if it is appropriate not to emit any debugging
13901 information for BLOCK, because it doesn't contain any instructions.
ad2fe2cd 13902
0a78547b 13903 Don't allow this for blocks with nested functions or local classes
13904 as we would end up with orphans, and in the presence of scheduling
13905 we may end up calling them anyway. */
13906
b29760a8 13907static bool
8ec3a57b 13908dwarf2out_ignore_block (tree block)
ad2fe2cd 13909{
13910 tree decl;
8c3f468d 13911
ad2fe2cd 13912 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
0a78547b 13913 if (TREE_CODE (decl) == FUNCTION_DECL
13914 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13915 return 0;
8c3f468d 13916
0a78547b 13917 return 1;
ad2fe2cd 13918}
13919
69278c24 13920/* Hash table routines for file_hash. */
13921
13922static int
13923file_table_eq (const void *p1_p, const void *p2_p)
13924{
13925 const struct dwarf_file_data * p1 = p1_p;
13926 const char * p2 = p2_p;
13927 return strcmp (p1->filename, p2) == 0;
13928}
13929
13930static hashval_t
13931file_table_hash (const void *p_p)
13932{
13933 const struct dwarf_file_data * p = p_p;
13934 return htab_hash_string (p->filename);
13935}
13936
8c3f468d 13937/* Lookup FILE_NAME (in the list of filenames that we know about here in
be6eb971 13938 dwarf2out.c) and return its "index". The index of each (known) filename is
8c3f468d 13939 just a unique number which is associated with only that one filename. We
13940 need such numbers for the sake of generating labels (in the .debug_sfnames
13941 section) and references to those files numbers (in the .debug_srcinfo
13942 and.debug_macinfo sections). If the filename given as an argument is not
13943 found in our current list, add it to the list and assign it the next
13944 available unique index number. In order to speed up searches, we remember
13945 the index of the filename was looked up last. This handles the majority of
13946 all searches. */
ec1e49cc 13947
69278c24 13948static struct dwarf_file_data *
8ec3a57b 13949lookup_filename (const char *file_name)
30ade641 13950{
69278c24 13951 void ** slot;
13952 struct dwarf_file_data * created;
30ade641 13953
5a3023d9 13954 /* Check to see if the file name that was searched on the previous
13955 call matches this file name. If so, return the index. */
69278c24 13956 if (file_table_last_lookup
13957 && (file_name == file_table_last_lookup->filename
13958 || strcmp (file_table_last_lookup->filename, file_name) == 0))
13959 return file_table_last_lookup;
30ade641 13960
778ac06a 13961 /* Didn't match the previous lookup, search the table. */
69278c24 13962 slot = htab_find_slot_with_hash (file_table, file_name,
13963 htab_hash_string (file_name), INSERT);
13964 if (*slot)
13965 return *slot;
30ade641 13966
69278c24 13967 created = ggc_alloc (sizeof (struct dwarf_file_data));
13968 created->filename = file_name;
13969 created->emitted_number = 0;
13970 *slot = created;
13971 return created;
c83a163c 13972}
13973
44276901 13974/* If the assembler will construct the file table, then translate the compiler
13975 internal file table number into the assembler file table number, and emit
13976 a .file directive if we haven't already emitted one yet. The file table
13977 numbers are different because we prune debug info for unused variables and
13978 types, which may include filenames. */
13979
c83a163c 13980static int
69278c24 13981maybe_emit_file (struct dwarf_file_data * fd)
c83a163c 13982{
69278c24 13983 if (! fd->emitted_number)
6e957326 13984 {
69278c24 13985 if (last_emitted_file)
13986 fd->emitted_number = last_emitted_file->emitted_number + 1;
13987 else
13988 fd->emitted_number = 1;
13989 last_emitted_file = fd;
61a9389f 13990
69278c24 13991 if (DWARF2_ASM_LINE_DEBUG_INFO)
c83a163c 13992 {
69278c24 13993 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13994 output_quoted_string (asm_out_file, fd->filename);
c83a163c 13995 fputc ('\n', asm_out_file);
13996 }
6e957326 13997 }
61a9389f 13998
69278c24 13999 return fd->emitted_number;
30ade641 14000}
14001
b2025850 14002/* Called by the final INSN scan whenever we see a var location. We
14003 use it to drop labels in the right places, and throw the location in
14004 our lookup table. */
14005
14006static void
14007dwarf2out_var_location (rtx loc_note)
14008{
14009 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14010 struct var_loc_node *newloc;
14011 rtx prev_insn;
14012 static rtx last_insn;
14013 static const char *last_label;
bbc7bce1 14014 tree decl;
b2025850 14015
14016 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14017 return;
14018 prev_insn = PREV_INSN (loc_note);
14019
14020 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14021 /* If the insn we processed last time is the previous insn
14022 and it is also a var location note, use the label we emitted
14023 last time. */
14024 if (last_insn != NULL_RTX
14025 && last_insn == prev_insn
6d7dc5b9 14026 && NOTE_P (prev_insn)
ad4583d9 14027 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
b2025850 14028 {
14029 newloc->label = last_label;
14030 }
14031 else
14032 {
14033 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14034 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14035 loclabel_num++;
14036 newloc->label = ggc_strdup (loclabel);
14037 }
14038 newloc->var_loc_note = loc_note;
14039 newloc->next = NULL;
14040
5fbee89d 14041 if (cfun && in_cold_section_p)
4d0e931f 14042 newloc->section_label = cfun->cold_section_label;
1897b881 14043 else
14044 newloc->section_label = text_section_label;
14045
b2025850 14046 last_insn = loc_note;
14047 last_label = newloc->label;
bbc7bce1 14048 decl = NOTE_VAR_LOCATION_DECL (loc_note);
bbc7bce1 14049 add_var_loc_to_decl (decl, newloc);
b2025850 14050}
14051
14052/* We need to reset the locations at the beginning of each
14053 function. We can't do this in the end_function hook, because the
dae1861f 14054 declarations that use the locations won't have been output when
14055 that hook is called. Also compute have_multiple_function_sections here. */
b2025850 14056
14057static void
dae1861f 14058dwarf2out_begin_function (tree fun)
b2025850 14059{
14060 htab_empty (decl_loc_table);
61a9389f 14061
dae1861f 14062 if (function_section (fun) != text_section)
14063 have_multiple_function_sections = true;
b2025850 14064}
14065
30ade641 14066/* Output a label to mark the beginning of a source code line entry
14067 and record information relating to this source line, in
14068 'line_info_table' for later output of the .debug_line section. */
ec1e49cc 14069
b9b7f8b4 14070static void
8ec3a57b 14071dwarf2out_source_line (unsigned int line, const char *filename)
30ade641 14072{
d8a4712b 14073 if (debug_info_level >= DINFO_LEVEL_NORMAL
14074 && line != 0)
30ade641 14075 {
69278c24 14076 int file_num = maybe_emit_file (lookup_filename (filename));
61a9389f 14077
2f14b1f9 14078 switch_to_section (current_function_section ());
30ade641 14079
80ae3362 14080 /* If requested, emit something human-readable. */
14081 if (flag_debug_asm)
14082 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14083 filename, line);
14084
985956c1 14085 if (DWARF2_ASM_LINE_DEBUG_INFO)
14086 {
3740694f 14087 /* Emit the .loc directive understood by GNU as. */
5a3023d9 14088 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
985956c1 14089
14090 /* Indicate that line number info exists. */
8c3f468d 14091 line_info_table_in_use++;
985956c1 14092 }
dae1861f 14093 else if (function_section (current_function_decl) != text_section)
30ade641 14094 {
19cb6b50 14095 dw_separate_line_info_ref line_info;
61a9389f 14096 targetm.asm_out.internal_label (asm_out_file,
69278c24 14097 SEPARATE_LINE_CODE_LABEL,
14098 separate_line_info_table_in_use);
c05d7491 14099
aab2cf92 14100 /* Expand the line info table if necessary. */
c05d7491 14101 if (separate_line_info_table_in_use
14102 == separate_line_info_table_allocated)
14103 {
14104 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14105 separate_line_info_table
f0af5a88 14106 = ggc_realloc (separate_line_info_table,
573aba85 14107 separate_line_info_table_allocated
14108 * sizeof (dw_separate_line_info_entry));
f0af5a88 14109 memset (separate_line_info_table
14110 + separate_line_info_table_in_use,
573aba85 14111 0,
8ec3a57b 14112 (LINE_INFO_TABLE_INCREMENT
573aba85 14113 * sizeof (dw_separate_line_info_entry)));
c05d7491 14114 }
ec1e49cc 14115
14116 /* Add the new entry at the end of the line_info_table. */
c05d7491 14117 line_info
14118 = &separate_line_info_table[separate_line_info_table_in_use++];
69278c24 14119 line_info->dw_file_num = file_num;
c05d7491 14120 line_info->dw_line_num = line;
4781f9b9 14121 line_info->function = current_function_funcdef_no;
c05d7491 14122 }
14123 else
14124 {
19cb6b50 14125 dw_line_info_ref line_info;
ec1e49cc 14126
883b2e73 14127 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
d58978a6 14128 line_info_table_in_use);
c05d7491 14129
ec1e49cc 14130 /* Expand the line info table if necessary. */
c05d7491 14131 if (line_info_table_in_use == line_info_table_allocated)
14132 {
14133 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14134 line_info_table
573aba85 14135 = ggc_realloc (line_info_table,
14136 (line_info_table_allocated
14137 * sizeof (dw_line_info_entry)));
14138 memset (line_info_table + line_info_table_in_use, 0,
14139 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
c05d7491 14140 }
ec1e49cc 14141
14142 /* Add the new entry at the end of the line_info_table. */
c05d7491 14143 line_info = &line_info_table[line_info_table_in_use++];
69278c24 14144 line_info->dw_file_num = file_num;
c05d7491 14145 line_info->dw_line_num = line;
30ade641 14146 }
30ade641 14147 }
14148}
14149
6312a35e 14150/* Record the beginning of a new source file. */
ec1e49cc 14151
c140b944 14152static void
8ec3a57b 14153dwarf2out_start_source_file (unsigned int lineno, const char *filename)
30ade641 14154{
7a614b74 14155 if (flag_eliminate_dwarf2_dups)
19f716e5 14156 {
14157 /* Record the beginning of the file for break_out_includes. */
51e8c210 14158 dw_die_ref bincl_die;
14159
14160 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
19f716e5 14161 add_AT_string (bincl_die, DW_AT_name, filename);
14162 }
8c3f468d 14163
1d340a5e 14164 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14165 {
69278c24 14166 int file_num = maybe_emit_file (lookup_filename (filename));
9b1f6100 14167
2f14b1f9 14168 switch_to_section (debug_macinfo_section);
1d340a5e 14169 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
2cb4ac60 14170 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14171 lineno);
9b1f6100 14172
69278c24 14173 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
1d340a5e 14174 }
30ade641 14175}
14176
c5c7e194 14177/* Record the end of a source file. */
ec1e49cc 14178
c140b944 14179static void
8ec3a57b 14180dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
30ade641 14181{
19f716e5 14182 if (flag_eliminate_dwarf2_dups)
8c3f468d 14183 /* Record the end of the file for break_out_includes. */
15cfae4e 14184 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
8c3f468d 14185
1d340a5e 14186 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14187 {
2f14b1f9 14188 switch_to_section (debug_macinfo_section);
1d340a5e 14189 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14190 }
30ade641 14191}
14192
c5c7e194 14193/* Called from debug_define in toplev.c. The `buffer' parameter contains
30ade641 14194 the tail part of the directive line, i.e. the part which is past the
14195 initial whitespace, #, whitespace, directive-name, whitespace part. */
ec1e49cc 14196
c140b944 14197static void
8ec3a57b 14198dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14199 const char *buffer ATTRIBUTE_UNUSED)
30ade641 14200{
1d340a5e 14201 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14202 {
2f14b1f9 14203 switch_to_section (debug_macinfo_section);
1d340a5e 14204 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14205 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14206 dw2_asm_output_nstring (buffer, -1, "The macro");
14207 }
30ade641 14208}
14209
c5c7e194 14210/* Called from debug_undef in toplev.c. The `buffer' parameter contains
30ade641 14211 the tail part of the directive line, i.e. the part which is past the
14212 initial whitespace, #, whitespace, directive-name, whitespace part. */
ec1e49cc 14213
c140b944 14214static void
8ec3a57b 14215dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14216 const char *buffer ATTRIBUTE_UNUSED)
30ade641 14217{
1d340a5e 14218 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14219 {
2f14b1f9 14220 switch_to_section (debug_macinfo_section);
1d340a5e 14221 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14222 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14223 dw2_asm_output_nstring (buffer, -1, "The macro");
14224 }
30ade641 14225}
14226
14227/* Set up for Dwarf output at the start of compilation. */
ec1e49cc 14228
b896d81b 14229static void
8ec3a57b 14230dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
30ade641 14231{
69278c24 14232 /* Allocate the file_table. */
14233 file_table = htab_create_ggc (50, file_table_hash,
14234 file_table_eq, NULL);
0924bbb7 14235
b2025850 14236 /* Allocate the decl_die_table. */
26863140 14237 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14238 decl_die_table_eq, NULL);
b2025850 14239
14240 /* Allocate the decl_loc_table. */
14241 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14242 decl_loc_table_eq, NULL);
30ade641 14243
14244 /* Allocate the initial hunk of the decl_scope_table. */
4a940e75 14245 decl_scope_table = VEC_alloc (tree, gc, 256);
30ade641 14246
14247 /* Allocate the initial hunk of the abbrev_die_table. */
573aba85 14248 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14249 * sizeof (dw_die_ref));
30ade641 14250 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
778ac06a 14251 /* Zero-th entry is allocated, but unused. */
30ade641 14252 abbrev_die_table_in_use = 1;
14253
14254 /* Allocate the initial hunk of the line_info_table. */
573aba85 14255 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14256 * sizeof (dw_line_info_entry));
30ade641 14257 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
8c3f468d 14258
778ac06a 14259 /* Zero-th entry is allocated, but unused. */
30ade641 14260 line_info_table_in_use = 1;
14261
af84796a 14262 /* Allocate the pubtypes and pubnames vectors. */
14263 pubname_table = VEC_alloc (pubname_entry, gc, 32);
14264 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14265
f80d1bcd 14266 /* Generate the initial DIE for the .debug section. Note that the (string)
30ade641 14267 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
f80d1bcd 14268 will (typically) be a relative pathname and that this pathname should be
30ade641 14269 taken as being relative to the directory from which the compiler was
ff279357 14270 invoked when the given (base) source file was compiled. We will fill
14271 in this value in dwarf2out_finish. */
14272 comp_unit_die = gen_compile_unit_die (NULL);
30ade641 14273
22230dd1 14274 incomplete_types = VEC_alloc (tree, gc, 64);
52a7cc7b 14275
62aedc4c 14276 used_rtx_array = VEC_alloc (rtx, gc, 32);
eacbfaac 14277
2f14b1f9 14278 debug_info_section = get_section (DEBUG_INFO_SECTION,
14279 SECTION_DEBUG, NULL);
14280 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14281 SECTION_DEBUG, NULL);
14282 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14283 SECTION_DEBUG, NULL);
14284 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14285 SECTION_DEBUG, NULL);
14286 debug_line_section = get_section (DEBUG_LINE_SECTION,
14287 SECTION_DEBUG, NULL);
14288 debug_loc_section = get_section (DEBUG_LOC_SECTION,
14289 SECTION_DEBUG, NULL);
14290 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14291 SECTION_DEBUG, NULL);
af84796a 14292#ifdef DEBUG_PUBTYPES_SECTION
14293 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14294 SECTION_DEBUG, NULL);
14295#endif
2f14b1f9 14296 debug_str_section = get_section (DEBUG_STR_SECTION,
14297 DEBUG_STR_SECTION_FLAGS, NULL);
14298 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14299 SECTION_DEBUG, NULL);
d08d29c0 14300 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14301 SECTION_DEBUG, NULL);
2f14b1f9 14302
d58978a6 14303 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
049aa99b 14304 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14305 DEBUG_ABBREV_SECTION_LABEL, 0);
e335d512 14306 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
61a9389f 14307 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
4d0e931f 14308 COLD_TEXT_SECTION_LABEL, 0);
14309 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
8c3f468d 14310
f80d1bcd 14311 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
65fc1a16 14312 DEBUG_INFO_SECTION_LABEL, 0);
f80d1bcd 14313 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
65fc1a16 14314 DEBUG_LINE_SECTION_LABEL, 0);
fe39c28c 14315 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14316 DEBUG_RANGES_SECTION_LABEL, 0);
2f14b1f9 14317 switch_to_section (debug_abbrev_section);
65fc1a16 14318 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
2f14b1f9 14319 switch_to_section (debug_info_section);
65fc1a16 14320 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
2f14b1f9 14321 switch_to_section (debug_line_section);
65fc1a16 14322 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
8c3f468d 14323
1d340a5e 14324 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14325 {
2f14b1f9 14326 switch_to_section (debug_macinfo_section);
1d340a5e 14327 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14328 DEBUG_MACINFO_SECTION_LABEL, 0);
14329 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14330 }
2cb4ac60 14331
2f14b1f9 14332 switch_to_section (text_section);
e335d512 14333 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
4d0e931f 14334 if (flag_reorder_blocks_and_partition)
14335 {
5fbee89d 14336 switch_to_section (unlikely_text_section ());
4d0e931f 14337 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14338 }
30ade641 14339}
14340
80b7bd06 14341/* A helper function for dwarf2out_finish called through
14342 ht_forall. Emit one queued .debug_str string. */
14343
14344static int
8ec3a57b 14345output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
80b7bd06 14346{
573aba85 14347 struct indirect_string_node *node = (struct indirect_string_node *) *h;
80b7bd06 14348
80b7bd06 14349 if (node->form == DW_FORM_strp)
14350 {
2f14b1f9 14351 switch_to_section (debug_str_section);
80b7bd06 14352 ASM_OUTPUT_LABEL (asm_out_file, node->label);
573aba85 14353 assemble_string (node->str, strlen (node->str) + 1);
80b7bd06 14354 }
8c3f468d 14355
80b7bd06 14356 return 1;
14357}
14358
cd04bce0 14359#if ENABLE_ASSERT_CHECKING
14360/* Verify that all marks are clear. */
c83a163c 14361
cd04bce0 14362static void
14363verify_marks_clear (dw_die_ref die)
14364{
14365 dw_die_ref c;
61a9389f 14366
cd04bce0 14367 gcc_assert (! die->die_mark);
14368 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14369}
14370#endif /* ENABLE_ASSERT_CHECKING */
c83a163c 14371
14372/* Clear the marks for a die and its children.
037845e5 14373 Be cool if the mark isn't set. */
c83a163c 14374
14375static void
8ec3a57b 14376prune_unmark_dies (dw_die_ref die)
c83a163c 14377{
14378 dw_die_ref c;
61a9389f 14379
958656b7 14380 if (die->die_mark)
14381 die->die_mark = 0;
14382 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
c83a163c 14383}
14384
c83a163c 14385/* Given DIE that we're marking as used, find any other dies
14386 it references as attributes and mark them as used. */
14387
14388static void
8ec3a57b 14389prune_unused_types_walk_attribs (dw_die_ref die)
c83a163c 14390{
14391 dw_attr_ref a;
6f56c055 14392 unsigned ix;
c83a163c 14393
6f56c055 14394 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
c83a163c 14395 {
14396 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14397 {
14398 /* A reference to another DIE.
14399 Make sure that it will get emitted. */
14400 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14401 }
b0aa6b33 14402 /* Set the string's refcount to 0 so that prune_unused_types_mark
14403 accounts properly for it. */
14404 if (AT_class (a) == dw_val_class_str)
14405 a->dw_attr_val.v.val_str->refcount = 0;
c83a163c 14406 }
14407}
14408
14409
14410/* Mark DIE as being used. If DOKIDS is true, then walk down
14411 to DIE's children. */
14412
14413static void
8ec3a57b 14414prune_unused_types_mark (dw_die_ref die, int dokids)
c83a163c 14415{
14416 dw_die_ref c;
14417
14418 if (die->die_mark == 0)
14419 {
14420 /* We haven't done this node yet. Mark it as used. */
14421 die->die_mark = 1;
14422
14423 /* We also have to mark its parents as used.
14424 (But we don't want to mark our parents' kids due to this.) */
14425 if (die->die_parent)
14426 prune_unused_types_mark (die->die_parent, 0);
14427
14428 /* Mark any referenced nodes. */
14429 prune_unused_types_walk_attribs (die);
023dc493 14430
14431 /* If this node is a specification,
61a9389f 14432 also mark the definition, if it exists. */
023dc493 14433 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
61a9389f 14434 prune_unused_types_mark (die->die_definition, 1);
c83a163c 14435 }
14436
14437 if (dokids && die->die_mark != 2)
14438 {
14439 /* We need to walk the children, but haven't done so yet.
14440 Remember that we've walked the kids. */
14441 die->die_mark = 2;
14442
958656b7 14443 /* If this is an array type, we need to make sure our
14444 kids get marked, even if they're types. */
14445 if (die->die_tag == DW_TAG_array_type)
14446 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14447 else
14448 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
c83a163c 14449 }
14450}
14451
14452
14453/* Walk the tree DIE and mark types that we actually use. */
14454
14455static void
8ec3a57b 14456prune_unused_types_walk (dw_die_ref die)
c83a163c 14457{
14458 dw_die_ref c;
14459
14460 /* Don't do anything if this node is already marked. */
14461 if (die->die_mark)
14462 return;
14463
7d4c98bc 14464 switch (die->die_tag)
14465 {
14466 case DW_TAG_const_type:
14467 case DW_TAG_packed_type:
14468 case DW_TAG_pointer_type:
14469 case DW_TAG_reference_type:
14470 case DW_TAG_volatile_type:
14471 case DW_TAG_typedef:
14472 case DW_TAG_array_type:
14473 case DW_TAG_structure_type:
14474 case DW_TAG_union_type:
14475 case DW_TAG_class_type:
14476 case DW_TAG_friend:
14477 case DW_TAG_variant_part:
14478 case DW_TAG_enumeration_type:
14479 case DW_TAG_subroutine_type:
14480 case DW_TAG_string_type:
14481 case DW_TAG_set_type:
14482 case DW_TAG_subrange_type:
14483 case DW_TAG_ptr_to_member_type:
14484 case DW_TAG_file_type:
14485 if (die->die_perennial_p)
14486 break;
f6e59711 14487
7d4c98bc 14488 /* It's a type node --- don't mark it. */
14489 return;
c83a163c 14490
7d4c98bc 14491 default:
14492 /* Mark everything else. */
14493 break;
c83a163c 14494 }
14495
14496 die->die_mark = 1;
14497
14498 /* Now, mark any dies referenced from here. */
14499 prune_unused_types_walk_attribs (die);
14500
14501 /* Mark children. */
958656b7 14502 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
c83a163c 14503}
14504
b0aa6b33 14505/* Increment the string counts on strings referred to from DIE's
14506 attributes. */
14507
14508static void
14509prune_unused_types_update_strings (dw_die_ref die)
14510{
14511 dw_attr_ref a;
14512 unsigned ix;
14513
14514 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14515 if (AT_class (a) == dw_val_class_str)
14516 {
14517 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14518 s->refcount++;
14519 /* Avoid unnecessarily putting strings that are used less than
14520 twice in the hash table. */
20f220a9 14521 if (s->refcount
14522 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
b0aa6b33 14523 {
14524 void ** slot;
14525 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14526 htab_hash_string (s->str),
14527 INSERT);
14528 gcc_assert (*slot == NULL);
14529 *slot = s;
14530 }
14531 }
14532}
c83a163c 14533
14534/* Remove from the tree DIE any dies that aren't marked. */
14535
14536static void
8ec3a57b 14537prune_unused_types_prune (dw_die_ref die)
c83a163c 14538{
958656b7 14539 dw_die_ref c;
8ff30ff6 14540
7bd4f6b6 14541 gcc_assert (die->die_mark);
4533b23c 14542 prune_unused_types_update_strings (die);
c83a163c 14543
958656b7 14544 if (! die->die_child)
14545 return;
61a9389f 14546
958656b7 14547 c = die->die_child;
14548 do {
14549 dw_die_ref prev = c;
14550 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14551 if (c == die->die_child)
c83a163c 14552 {
958656b7 14553 /* No marked children between 'prev' and the end of the list. */
14554 if (prev == c)
14555 /* No marked children at all. */
14556 die->die_child = NULL;
14557 else
14558 {
14559 prev->die_sib = c->die_sib;
14560 die->die_child = prev;
14561 }
14562 return;
c83a163c 14563 }
958656b7 14564
14565 if (c != prev->die_sib)
14566 prev->die_sib = c;
958656b7 14567 prune_unused_types_prune (c);
14568 } while (c != die->die_child);
c83a163c 14569}
14570
14571
14572/* Remove dies representing declarations that we never use. */
14573
14574static void
8ec3a57b 14575prune_unused_types (void)
c83a163c 14576{
14577 unsigned int i;
14578 limbo_die_node *node;
af84796a 14579 pubname_ref pub;
c83a163c 14580
cd04bce0 14581#if ENABLE_ASSERT_CHECKING
14582 /* All the marks should already be clear. */
14583 verify_marks_clear (comp_unit_die);
c83a163c 14584 for (node = limbo_die_list; node; node = node->next)
cd04bce0 14585 verify_marks_clear (node->die);
14586#endif /* ENABLE_ASSERT_CHECKING */
c83a163c 14587
14588 /* Set the mark on nodes that are actually used. */
14589 prune_unused_types_walk (comp_unit_die);
14590 for (node = limbo_die_list; node; node = node->next)
14591 prune_unused_types_walk (node->die);
14592
14593 /* Also set the mark on nodes referenced from the
14594 pubname_table or arange_table. */
af84796a 14595 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14596 prune_unused_types_mark (pub->die, 1);
ea0041f4 14597 for (i = 0; i < arange_table_in_use; i++)
14598 prune_unused_types_mark (arange_table[i], 1);
c83a163c 14599
b0aa6b33 14600 /* Get rid of nodes that aren't marked; and update the string counts. */
14601 if (debug_str_hash)
14602 htab_empty (debug_str_hash);
c83a163c 14603 prune_unused_types_prune (comp_unit_die);
14604 for (node = limbo_die_list; node; node = node->next)
14605 prune_unused_types_prune (node->die);
14606
14607 /* Leave the marks clear. */
14608 prune_unmark_dies (comp_unit_die);
14609 for (node = limbo_die_list; node; node = node->next)
14610 prune_unmark_dies (node->die);
14611}
14612
69278c24 14613/* Set the parameter to true if there are any relative pathnames in
14614 the file table. */
14615static int
14616file_table_relative_p (void ** slot, void *param)
14617{
14618 bool *p = param;
14619 struct dwarf_file_data *d = *slot;
974a92fe 14620 if (d->emitted_number && !IS_ABSOLUTE_PATH (d->filename))
69278c24 14621 {
14622 *p = true;
14623 return 0;
14624 }
14625 return 1;
14626}
14627
30ade641 14628/* Output stuff that dwarf requires at the end of every file,
14629 and generate the DWARF-2 debugging info. */
ec1e49cc 14630
b896d81b 14631static void
8ec3a57b 14632dwarf2out_finish (const char *filename)
30ade641 14633{
678d90bb 14634 limbo_die_node *node, *next_node;
97b330ca 14635 dw_die_ref die = 0;
678d90bb 14636
ff279357 14637 /* Add the name for the main input file now. We delayed this from
14638 dwarf2out_init to avoid complications with PCH. */
37b555e3 14639 add_name_attribute (comp_unit_die, filename);
974a92fe 14640 if (!IS_ABSOLUTE_PATH (filename))
ff279357 14641 add_comp_dir_attribute (comp_unit_die);
83f77ecb 14642 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14643 {
69278c24 14644 bool p = false;
14645 htab_traverse (file_table, file_table_relative_p, &p);
14646 if (p)
14647 add_comp_dir_attribute (comp_unit_die);
83f77ecb 14648 }
ff279357 14649
678d90bb 14650 /* Traverse the limbo die list, and add parent/child links. The only
14651 dies without parents that should be here are concrete instances of
14652 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14653 For concrete instances, we can get the parent die from the abstract
14654 instance. */
14655 for (node = limbo_die_list; node; node = next_node)
14656 {
14657 next_node = node->next;
14658 die = node->die;
14659
14660 if (die->die_parent == NULL)
14661 {
c90bf86c 14662 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
8c3f468d 14663
c90bf86c 14664 if (origin)
14665 add_child_die (origin->die_parent, die);
678d90bb 14666 else if (die == comp_unit_die)
c90bf86c 14667 ;
15a56411 14668 else if (errorcount > 0 || sorrycount > 0)
14669 /* It's OK to be confused by errors in the input. */
14670 add_child_die (comp_unit_die, die);
7bd4f6b6 14671 else
15cfae4e 14672 {
14673 /* In certain situations, the lexical block containing a
14674 nested function can be optimized away, which results
14675 in the nested function die being orphaned. Likewise
14676 with the return type of that nested function. Force
1b55e9dc 14677 this to be a child of the containing function.
14678
14679 It may happen that even the containing function got fully
14680 inlined and optimized out. In that case we are lost and
14681 assign the empty child. This should not be big issue as
14682 the function is likely unreachable too. */
7bd4f6b6 14683 tree context = NULL_TREE;
14684
14685 gcc_assert (node->created_for);
14686
14687 if (DECL_P (node->created_for))
14688 context = DECL_CONTEXT (node->created_for);
14689 else if (TYPE_P (node->created_for))
14690 context = TYPE_CONTEXT (node->created_for);
8ff30ff6 14691
356f311d 14692 gcc_assert (context
14693 && (TREE_CODE (context) == FUNCTION_DECL
14694 || TREE_CODE (context) == NAMESPACE_DECL));
8ff30ff6 14695
15cfae4e 14696 origin = lookup_decl_die (context);
1b55e9dc 14697 if (origin)
14698 add_child_die (origin, die);
68690e9c 14699 else
14700 add_child_die (comp_unit_die, die);
15cfae4e 14701 }
678d90bb 14702 }
678d90bb 14703 }
8c3f468d 14704
c90bf86c 14705 limbo_die_list = NULL;
678d90bb 14706
a4617d03 14707 /* Walk through the list of incomplete types again, trying once more to
14708 emit full debugging info for them. */
14709 retry_incomplete_types ();
14710
449db731 14711 if (flag_eliminate_unused_debug_types)
14712 prune_unused_types ();
14713
19f716e5 14714 /* Generate separate CUs for each of the include files we've seen.
14715 They will go into limbo_die_list. */
02749c22 14716 if (flag_eliminate_dwarf2_dups)
14717 break_out_includes (comp_unit_die);
19f716e5 14718
14719 /* Traverse the DIE's and add add sibling attributes to those DIE's
14720 that have children. */
30ade641 14721 add_sibling_attributes (comp_unit_die);
19f716e5 14722 for (node = limbo_die_list; node; node = node->next)
14723 add_sibling_attributes (node->die);
30ade641 14724
14725 /* Output a terminator label for the .text section. */
2f14b1f9 14726 switch_to_section (text_section);
883b2e73 14727 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
4d0e931f 14728 if (flag_reorder_blocks_and_partition)
14729 {
5fbee89d 14730 switch_to_section (unlikely_text_section ());
4d0e931f 14731 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14732 }
30ade641 14733
603796f0 14734 /* We can only use the low/high_pc attributes if all of the code was
14735 in .text. */
dae1861f 14736 if (!have_multiple_function_sections)
603796f0 14737 {
14738 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14739 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
c05d7491 14740 }
8c3f468d 14741
f221c0bd 14742 else
14743 {
14744 unsigned fde_idx = 0;
14745
14746 /* We need to give .debug_loc and .debug_ranges an appropriate
14747 "base address". Use zero so that these addresses become
14748 absolute. Historically, we've emitted the unexpected
14749 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
14750 Emit both to give time for other tools to adapt. */
14751 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
14752 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14753
14754 add_AT_range_list (comp_unit_die, DW_AT_ranges,
14755 add_ranges_by_labels (text_section_label,
14756 text_end_label));
14757 if (flag_reorder_blocks_and_partition)
14758 add_ranges_by_labels (cold_text_section_label,
14759 cold_end_label);
14760
14761 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
14762 {
14763 dw_fde_ref fde = &fde_table[fde_idx];
14764
14765 if (fde->dw_fde_switched_sections)
14766 {
14767 add_ranges_by_labels (fde->dw_fde_hot_section_label,
14768 fde->dw_fde_hot_section_end_label);
14769 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
14770 fde->dw_fde_unlikely_section_end_label);
14771 }
14772 else
14773 add_ranges_by_labels (fde->dw_fde_begin,
14774 fde->dw_fde_end);
14775 }
14776
14777 add_ranges (NULL);
14778 }
c05d7491 14779
dae1861f 14780 /* Output location list section if necessary. */
14781 if (have_location_lists)
14782 {
14783 /* Output the location lists info. */
14784 switch_to_section (debug_loc_section);
14785 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14786 DEBUG_LOC_SECTION_LABEL, 0);
14787 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14788 output_location_lists (die);
14789 }
14790
28833db5 14791 if (debug_info_level >= DINFO_LEVEL_NORMAL)
d08d29c0 14792 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14793 debug_line_section_label);
603796f0 14794
1d340a5e 14795 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
d08d29c0 14796 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
c90bf86c 14797
19f716e5 14798 /* Output all of the compilation units. We put the main one last so that
14799 the offsets are available to output_pubnames. */
14800 for (node = limbo_die_list; node; node = node->next)
51e8c210 14801 output_comp_unit (node->die, 0);
8c3f468d 14802
51e8c210 14803 output_comp_unit (comp_unit_die, 0);
19f716e5 14804
30ade641 14805 /* Output the abbreviation table. */
2f14b1f9 14806 switch_to_section (debug_abbrev_section);
30ade641 14807 output_abbrev_section ();
14808
8c3f468d 14809 /* Output public names table if necessary. */
af84796a 14810 if (!VEC_empty (pubname_entry, pubname_table))
dc7a29ce 14811 {
2f14b1f9 14812 switch_to_section (debug_pubnames_section);
af84796a 14813 output_pubnames (pubname_table);
dc7a29ce 14814 }
14815
af84796a 14816#ifdef DEBUG_PUBTYPES_SECTION
14817 /* Output public types table if necessary. */
14818 if (!VEC_empty (pubname_entry, pubtype_table))
14819 {
14820 switch_to_section (debug_pubtypes_section);
14821 output_pubnames (pubtype_table);
14822 }
14823#endif
61a9389f 14824
8c3f468d 14825 /* Output the address range information. We only put functions in the arange
14826 table, so don't write it out if we don't have any. */
30ade641 14827 if (fde_table_in_use)
14828 {
2f14b1f9 14829 switch_to_section (debug_aranges_section);
30ade641 14830 output_aranges ();
14831 }
a36145ca 14832
a36145ca 14833 /* Output ranges section if necessary. */
14834 if (ranges_table_in_use)
14835 {
2f14b1f9 14836 switch_to_section (debug_ranges_section);
fe39c28c 14837 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
a36145ca 14838 output_ranges ();
14839 }
14840
69278c24 14841 /* Output the source line correspondence table. We must do this
14842 even if there is no line information. Otherwise, on an empty
14843 translation unit, we will generate a present, but empty,
14844 .debug_info section. IRIX 6.5 `nm' will then complain when
14845 examining the file. This is done late so that any filenames
14846 used by the debug_info section are marked as 'used'. */
14847 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14848 {
14849 switch_to_section (debug_line_section);
14850 output_line_info ();
14851 }
14852
156660d7 14853 /* Have to end the macro section. */
c5c7e194 14854 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
bc70bd5e 14855 {
2f14b1f9 14856 switch_to_section (debug_macinfo_section);
323583a1 14857 dw2_asm_output_data (1, 0, "End compilation unit");
c5c7e194 14858 }
80b7bd06 14859
8c3f468d 14860 /* If we emitted any DW_FORM_strp form attribute, output the string
80b7bd06 14861 table too. */
14862 if (debug_str_hash)
573aba85 14863 htab_traverse (debug_str_hash, output_indirect_string, NULL);
30ade641 14864}
1f3233d1 14865#else
14866
14867/* This should never be used, but its address is needed for comparisons. */
14868const struct gcc_debug_hooks dwarf2_debug_hooks;
14869
14870#endif /* DWARF2_DEBUGGING_INFO */
14871
14872#include "gt-dwarf2out.h"