]> gcc.gnu.org Git - gcc.git/blame - gcc/dwarf2out.c
c-lex.c (c_lex_with_flags, lex_string): Constify.
[gcc.git] / gcc / dwarf2out.c
CommitLineData
5e6908ea 1/* Output Dwarf2 format symbol table information from GCC.
69bd9368 2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
e53a16e7 3 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
e9a25f70
JL
4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
469ac993 6 Extensively modified by Jason Merrill (jason@cygnus.com).
a3f97cbb 7
1322177d 8This file is part of GCC.
a3f97cbb 9
1322177d
LB
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
12Software Foundation; either version 2, or (at your option) any later
13version.
a3f97cbb 14
1322177d
LB
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.
a3f97cbb
JW
19
20You should have received a copy of the GNU General Public License
1322177d 21along with GCC; see the file COPYING. If not, write to the Free
366ccddb
KC
22Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2302110-1301, USA. */
a3f97cbb 24
9eb4015a 25/* TODO: Emit .debug_line header even when there are no functions, since
348bb3c7
JM
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
30
3f76745e
JM
31/* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
36
f01d40db
BE
37/* DWARF2 Abbreviation Glossary:
38
39 CFA = Canonical Frame Address
40 a fixed address on the stack which identifies a call frame.
41 We define it to be the value of SP just before the call insn.
42 The CFA register and offset, which may change during the course
43 of the function, are used to calculate its value at runtime.
44
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
47
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
50
51 DIE = Debugging Information Entry
52
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
56
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
59
0021b564 60#include "config.h"
670ee920 61#include "system.h"
4977bab6
ZW
62#include "coretypes.h"
63#include "tm.h"
a3f97cbb 64#include "tree.h"
a757585a 65#include "version.h"
a3f97cbb 66#include "flags.h"
11ad4784 67#include "real.h"
a3f97cbb
JW
68#include "rtl.h"
69#include "hard-reg-set.h"
70#include "regs.h"
71#include "insn-config.h"
72#include "reload.h"
52a11cbf 73#include "function.h"
a3f97cbb 74#include "output.h"
71dfc51f 75#include "expr.h"
e78d8e51 76#include "libfuncs.h"
3f76745e 77#include "except.h"
a7cc7f29 78#include "dwarf2.h"
76ead72b 79#include "dwarf2out.h"
2e4b9b8c 80#include "dwarf2asm.h"
10f0ad3d 81#include "toplev.h"
1865dbb5 82#include "varray.h"
951a525f 83#include "ggc.h"
881c6935 84#include "md5.h"
57bed152 85#include "tm_p.h"
2a2b2d43 86#include "diagnostic.h"
a51d908e 87#include "debug.h"
07c9d2eb 88#include "target.h"
3ac88239 89#include "langhooks.h"
cc0017a9 90#include "hashtab.h"
1bb17c21 91#include "cgraph.h"
6097b0c3 92#include "input.h"
a3f97cbb 93
653e276c 94#ifdef DWARF2_DEBUGGING_INFO
7080f735 95static void dwarf2out_source_line (unsigned int, const char *);
653e276c
NB
96#endif
97
658f32fd
AO
98#ifndef DWARF2_FRAME_INFO
99# ifdef DWARF2_DEBUGGING_INFO
100# define DWARF2_FRAME_INFO \
101 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
102# else
103# define DWARF2_FRAME_INFO 0
104# endif
105#endif
106
fb638355
GK
107/* Map register numbers held in the call frame info that gcc has
108 collected using DWARF_FRAME_REGNUM to those that should be output in
109 .debug_frame and .eh_frame. */
110#ifndef DWARF2_FRAME_REG_OUT
111#define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
112#endif
113
0021b564
JM
114/* Decide whether we want to emit frame unwind information for the current
115 translation unit. */
116
117int
7080f735 118dwarf2out_do_frame (void)
0021b564 119{
658f32fd
AO
120 /* We want to emit correct CFA location expressions or lists, so we
121 have to return true if we're going to output debug info, even if
122 we're not going to output frame or unwind info. */
0021b564 123 return (write_symbols == DWARF2_DEBUG
7a0c8d71 124 || write_symbols == VMS_AND_DWARF2_DEBUG
556273e0 125 || DWARF2_FRAME_INFO
0021b564 126#ifdef DWARF2_UNWIND_INFO
658f32fd
AO
127 || (DWARF2_UNWIND_INFO
128 && (flag_unwind_tables
129 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
0021b564
JM
130#endif
131 );
132}
133
f3a8e4f5
KG
134/* The size of the target's pointer type. */
135#ifndef PTR_SIZE
136#define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
137#endif
138
e2500fed
GK
139/* Array of RTXes referenced by the debugging information, which therefore
140 must be kept around forever. */
a1bbd445 141static GTY(()) VEC(rtx,gc) *used_rtx_array;
e2500fed
GK
142
143/* A pointer to the base of a list of incomplete types which might be
887fb69b
KH
144 completed at some later time. incomplete_types_list needs to be a
145 VEC(tree,gc) because we want to tell the garbage collector about
146 it. */
147static GTY(()) VEC(tree,gc) *incomplete_types;
e2500fed
GK
148
149/* A pointer to the base of a table of references to declaration
150 scopes. This table is a display which tracks the nesting
151 of declaration scopes at the current scope and containing
152 scopes. This table is used to find the proper place to
153 define type declaration DIE's. */
0fdc587b 154static GTY(()) VEC(tree,gc) *decl_scope_table;
e2500fed 155
d6b5193b
RS
156/* Pointers to various DWARF2 sections. */
157static GTY(()) section *debug_info_section;
158static GTY(()) section *debug_abbrev_section;
159static GTY(()) section *debug_aranges_section;
160static GTY(()) section *debug_macinfo_section;
161static GTY(()) section *debug_line_section;
162static GTY(()) section *debug_loc_section;
163static GTY(()) section *debug_pubnames_section;
89708594 164static GTY(()) section *debug_pubtypes_section;
d6b5193b
RS
165static GTY(()) section *debug_str_section;
166static GTY(()) section *debug_ranges_section;
192d0f89 167static GTY(()) section *debug_frame_section;
d6b5193b 168
eaf95893
RK
169/* How to start an assembler comment. */
170#ifndef ASM_COMMENT_START
171#define ASM_COMMENT_START ";#"
172#endif
173
a3f97cbb
JW
174typedef struct dw_cfi_struct *dw_cfi_ref;
175typedef struct dw_fde_struct *dw_fde_ref;
176typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
a3f97cbb
JW
177
178/* Call frames are described using a sequence of Call Frame
179 Information instructions. The register number, offset
180 and address fields are provided as possible operands;
181 their use is selected by the opcode field. */
71dfc51f 182
17211ab5
GK
183enum dw_cfi_oprnd_type {
184 dw_cfi_oprnd_unused,
185 dw_cfi_oprnd_reg_num,
186 dw_cfi_oprnd_offset,
187 dw_cfi_oprnd_addr,
188 dw_cfi_oprnd_loc
189};
190
191typedef union dw_cfi_oprnd_struct GTY(())
71dfc51f 192{
f6672e8e 193 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
799f628a 194 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
17211ab5
GK
195 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
196 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
71dfc51f 197}
a3f97cbb
JW
198dw_cfi_oprnd;
199
17211ab5 200typedef struct dw_cfi_struct GTY(())
71dfc51f
RK
201{
202 dw_cfi_ref dw_cfi_next;
203 enum dwarf_call_frame_info dw_cfi_opc;
7080f735 204 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
17211ab5 205 dw_cfi_oprnd1;
7080f735 206 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
17211ab5 207 dw_cfi_oprnd2;
71dfc51f 208}
a3f97cbb
JW
209dw_cfi_node;
210
7d9d8943
AM
211/* This is how we define the location of the CFA. We use to handle it
212 as REG + OFFSET all the time, but now it can be more complex.
213 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
556273e0 214 Instead of passing around REG and OFFSET, we pass a copy
7d9d8943 215 of this structure. */
17211ab5 216typedef struct cfa_loc GTY(())
7d9d8943 217{
799f628a
JH
218 HOST_WIDE_INT offset;
219 HOST_WIDE_INT base_offset;
30e6f306 220 unsigned int reg;
7d9d8943
AM
221 int indirect; /* 1 if CFA is accessed via a dereference. */
222} dw_cfa_location;
223
a3f97cbb 224/* All call frame descriptions (FDE's) in the GCC generated DWARF
4b674448 225 refer to a single Common Information Entry (CIE), defined at
fb530c07 226 the beginning of the .debug_frame section. This use of a single
a3f97cbb
JW
227 CIE obviates the need to keep track of multiple CIE's
228 in the DWARF generation routines below. */
71dfc51f 229
17211ab5 230typedef struct dw_fde_struct GTY(())
71dfc51f 231{
4746cf84 232 tree decl;
d3e3972c
KG
233 const char *dw_fde_begin;
234 const char *dw_fde_current_label;
235 const char *dw_fde_end;
87c8b4be
CT
236 const char *dw_fde_hot_section_label;
237 const char *dw_fde_hot_section_end_label;
238 const char *dw_fde_unlikely_section_label;
239 const char *dw_fde_unlikely_section_end_label;
240 bool dw_fde_switched_sections;
71dfc51f 241 dw_cfi_ref dw_fde_cfi;
52a11cbf 242 unsigned funcdef_number;
b6128b8c 243 unsigned all_throwers_are_sibcalls : 1;
52a11cbf
RH
244 unsigned nothrow : 1;
245 unsigned uses_eh_lsda : 1;
71dfc51f 246}
a3f97cbb
JW
247dw_fde_node;
248
6d2f8887 249/* Maximum size (in bytes) of an artificially generated label. */
a3f97cbb
JW
250#define MAX_ARTIFICIAL_LABEL_BYTES 30
251
a1a4189d
JB
252/* The size of addresses as they appear in the Dwarf 2 data.
253 Some architectures use word addresses to refer to code locations,
254 but Dwarf 2 info always uses byte addresses. On such machines,
255 Dwarf 2 addresses need to be larger than the architecture's
256 pointers. */
257#ifndef DWARF2_ADDR_SIZE
258#define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
259#endif
260
7e23cb16 261/* The size in bytes of a DWARF field indicating an offset or length
a1a4189d
JB
262 relative to a debug info section, specified to be 4 bytes in the
263 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
b13fe8bf 264 as PTR_SIZE. */
71dfc51f 265
7e23cb16
JM
266#ifndef DWARF_OFFSET_SIZE
267#define DWARF_OFFSET_SIZE 4
268#endif
269
9eb0ef7a
KB
270/* According to the (draft) DWARF 3 specification, the initial length
271 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
272 bytes are 0xffffffff, followed by the length stored in the next 8
273 bytes.
274
275 However, the SGI/MIPS ABI uses an initial length which is equal to
276 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
277
278#ifndef DWARF_INITIAL_LENGTH_SIZE
279#define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
280#endif
281
9a666dda
JM
282#define DWARF_VERSION 2
283
7e23cb16
JM
284/* Round SIZE up to the nearest BOUNDARY. */
285#define DWARF_ROUND(SIZE,BOUNDARY) \
262b6384 286 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
a3f97cbb 287
a3f97cbb 288/* Offsets recorded in opcodes are a multiple of this alignment factor. */
27c35f4b 289#ifndef DWARF_CIE_DATA_ALIGNMENT
469ac993 290#ifdef STACK_GROWS_DOWNWARD
08cb3d38 291#define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
469ac993 292#else
08cb3d38 293#define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
469ac993 294#endif
2ad9852d 295#endif
a3f97cbb 296
0c33762a
JM
297/* CIE identifier. */
298#if HOST_BITS_PER_WIDE_INT >= 64
299#define DWARF_CIE_ID \
300 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
301#else
302#define DWARF_CIE_ID DW_CIE_ID
303#endif
304
3f76745e
JM
305/* A pointer to the base of a table that contains frame description
306 information for each routine. */
17211ab5 307static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
a3f97cbb 308
3f76745e 309/* Number of elements currently allocated for fde_table. */
c2e9147c 310static GTY(()) unsigned fde_table_allocated;
a94dbf2c 311
3f76745e 312/* Number of elements in fde_table currently in use. */
044b4de3 313static GTY(()) unsigned fde_table_in_use;
a3f97cbb 314
3f76745e
JM
315/* Size (in elements) of increments by which we may expand the
316 fde_table. */
317#define FDE_TABLE_INCREMENT 256
a3f97cbb 318
a94dbf2c 319/* A list of call frame insns for the CIE. */
17211ab5 320static GTY(()) dw_cfi_ref cie_cfi_head;
a94dbf2c 321
c1b50e49 322#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
a3f97cbb
JW
323/* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
324 attribute that accelerates the lookup of the FDE associated
556273e0 325 with the subprogram. This variable holds the table index of the FDE
a3f97cbb
JW
326 associated with the current function (body) definition. */
327static unsigned current_funcdef_fde;
c1b50e49 328#endif
a3f97cbb 329
17211ab5 330struct indirect_string_node GTY(())
9eb4015a 331{
17211ab5 332 const char *str;
9eb4015a
JJ
333 unsigned int refcount;
334 unsigned int form;
335 char *label;
336};
337
17211ab5
GK
338static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
339
340static GTY(()) int dw2_string_counter;
044b4de3 341static GTY(()) unsigned long dwarf2out_cfi_label_num;
17211ab5
GK
342
343#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
344
a3f97cbb 345/* Forward declarations for functions defined in this file. */
71dfc51f 346
7080f735
AJ
347static char *stripattributes (const char *);
348static const char *dwarf_cfi_name (unsigned);
349static dw_cfi_ref new_cfi (void);
350static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
351static void add_fde_cfi (const char *, dw_cfi_ref);
352static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
353static void lookup_cfa (dw_cfa_location *);
799f628a 354static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
36bd4303 355#ifdef DWARF2_UNWIND_INFO
7080f735 356static void initial_return_save (rtx);
36bd4303 357#endif
799f628a 358static HOST_WIDE_INT stack_adjust_offset (rtx);
7080f735
AJ
359static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
360static void output_call_frame_info (int);
e0c0490b 361static void dwarf2out_stack_adjust (rtx, bool);
7080f735
AJ
362static void flush_queued_reg_saves (void);
363static bool clobbers_queued_reg_save (rtx);
364static void dwarf2out_frame_debug_expr (rtx, const char *);
a3f97cbb 365
7d9d8943 366/* Support for complex CFA locations. */
7080f735
AJ
367static void output_cfa_loc (dw_cfi_ref);
368static void get_cfa_from_loc_descr (dw_cfa_location *,
369 struct dw_loc_descr_struct *);
7d9d8943 370static struct dw_loc_descr_struct *build_cfa_loc
35d177a2 371 (dw_cfa_location *, HOST_WIDE_INT);
7080f735 372static void def_cfa_1 (const char *, dw_cfa_location *);
7d9d8943 373
2e4b9b8c
RH
374/* How to start an assembler comment. */
375#ifndef ASM_COMMENT_START
376#define ASM_COMMENT_START ";#"
a3f97cbb
JW
377#endif
378
7e23cb16
JM
379/* Data and reference forms for relocatable data. */
380#define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
381#define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
382
cf2fe500
RH
383#ifndef DEBUG_FRAME_SECTION
384#define DEBUG_FRAME_SECTION ".debug_frame"
a3f97cbb 385#endif
a3f97cbb 386
5c90448c
JM
387#ifndef FUNC_BEGIN_LABEL
388#define FUNC_BEGIN_LABEL "LFB"
a3f97cbb 389#endif
2ad9852d 390
5c90448c
JM
391#ifndef FUNC_END_LABEL
392#define FUNC_END_LABEL "LFE"
a3f97cbb 393#endif
2ad9852d 394
4746cf84 395#ifndef FRAME_BEGIN_LABEL
27d95cbe 396#define FRAME_BEGIN_LABEL "Lframe"
4746cf84 397#endif
a6ab3aad
JM
398#define CIE_AFTER_SIZE_LABEL "LSCIE"
399#define CIE_END_LABEL "LECIE"
2e4b9b8c
RH
400#define FDE_LABEL "LSFDE"
401#define FDE_AFTER_SIZE_LABEL "LASFDE"
a6ab3aad 402#define FDE_END_LABEL "LEFDE"
981975b6
RH
403#define LINE_NUMBER_BEGIN_LABEL "LSLT"
404#define LINE_NUMBER_END_LABEL "LELT"
405#define LN_PROLOG_AS_LABEL "LASLTP"
406#define LN_PROLOG_END_LABEL "LELTP"
881c6935 407#define DIE_LABEL_PREFIX "DW"
a3f97cbb 408
c8cc5c4a 409/* The DWARF 2 CFA column which tracks the return address. Normally this
a94dbf2c
JM
410 is the column for PC, or the first column after all of the hard
411 registers. */
c8cc5c4a 412#ifndef DWARF_FRAME_RETURN_COLUMN
a94dbf2c 413#ifdef PC_REGNUM
7080f735 414#define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
a94dbf2c 415#else
7080f735 416#define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
a94dbf2c 417#endif
c8cc5c4a
JM
418#endif
419
420/* The mapping from gcc register number to DWARF 2 CFA column number. By
469ac993 421 default, we just provide columns for all registers. */
c8cc5c4a 422#ifndef DWARF_FRAME_REGNUM
469ac993 423#define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
c8cc5c4a 424#endif
2ad9852d 425\f
0021b564
JM
426/* Hook used by __throw. */
427
428rtx
7080f735 429expand_builtin_dwarf_sp_column (void)
0021b564 430{
8f4fc766 431 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
fb638355 432 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
0021b564
JM
433}
434
71dfc51f 435/* Return a pointer to a copy of the section string name S with all
bf20f341 436 attributes stripped off, and an asterisk prepended (for assemble_name). */
71dfc51f
RK
437
438static inline char *
7080f735 439stripattributes (const char *s)
a3f97cbb 440{
5ed6ace5 441 char *stripped = XNEWVEC (char, strlen (s) + 2);
71dfc51f
RK
442 char *p = stripped;
443
bf20f341
JW
444 *p++ = '*';
445
446 while (*s && *s != ',')
447 *p++ = *s++;
71dfc51f 448
a3f97cbb
JW
449 *p = '\0';
450 return stripped;
451}
452
5cd0f915
RS
453/* MEM is a memory reference for the register size table, each element of
454 which has mode MODE. Initialize column C as a return address column. */
455
456static void
457init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
458{
459 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
460 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
461 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
462}
463
d9d5c9de 464/* Generate code to initialize the register size table. */
2f3ca9e7 465
d9d5c9de 466void
7080f735 467expand_builtin_init_dwarf_reg_sizes (tree address)
2f3ca9e7 468{
8f4fc766 469 unsigned int i;
d9d5c9de 470 enum machine_mode mode = TYPE_MODE (char_type_node);
84217346 471 rtx addr = expand_normal (address);
2ad9852d 472 rtx mem = gen_rtx_MEM (BLKmode, addr);
71628aa0 473 bool wrote_return_column = false;
2f3ca9e7 474
91ea38f9 475 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
fb638355
GK
476 {
477 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
2878ea73 478
fb638355
GK
479 if (rnum < DWARF_FRAME_REGISTERS)
480 {
481 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
482 enum machine_mode save_mode = reg_raw_mode[i];
483 HOST_WIDE_INT size;
2878ea73 484
fb638355
GK
485 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
486 save_mode = choose_hard_reg_mode (i, 1, true);
487 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
488 {
489 if (save_mode == VOIDmode)
490 continue;
491 wrote_return_column = true;
492 }
493 size = GET_MODE_SIZE (save_mode);
494 if (offset < 0)
495 continue;
2878ea73 496
fb638355
GK
497 emit_move_insn (adjust_address (mem, mode, offset),
498 gen_int_mode (size, mode));
499 }
500 }
ed80cd68 501
5cd0f915
RS
502 if (!wrote_return_column)
503 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
504
ed80cd68 505#ifdef DWARF_ALT_FRAME_RETURN_COLUMN
5cd0f915 506 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
ed80cd68 507#endif
37ea0b7e
JM
508
509 targetm.init_dwarf_reg_sizes_extra (address);
2f3ca9e7
JM
510}
511
3f76745e 512/* Convert a DWARF call frame info. operation to its string name */
a3f97cbb 513
d560ee52 514static const char *
7080f735 515dwarf_cfi_name (unsigned int cfi_opc)
3f76745e
JM
516{
517 switch (cfi_opc)
518 {
519 case DW_CFA_advance_loc:
520 return "DW_CFA_advance_loc";
521 case DW_CFA_offset:
522 return "DW_CFA_offset";
523 case DW_CFA_restore:
524 return "DW_CFA_restore";
525 case DW_CFA_nop:
526 return "DW_CFA_nop";
527 case DW_CFA_set_loc:
528 return "DW_CFA_set_loc";
529 case DW_CFA_advance_loc1:
530 return "DW_CFA_advance_loc1";
531 case DW_CFA_advance_loc2:
532 return "DW_CFA_advance_loc2";
533 case DW_CFA_advance_loc4:
534 return "DW_CFA_advance_loc4";
535 case DW_CFA_offset_extended:
536 return "DW_CFA_offset_extended";
537 case DW_CFA_restore_extended:
538 return "DW_CFA_restore_extended";
539 case DW_CFA_undefined:
540 return "DW_CFA_undefined";
541 case DW_CFA_same_value:
542 return "DW_CFA_same_value";
543 case DW_CFA_register:
544 return "DW_CFA_register";
545 case DW_CFA_remember_state:
546 return "DW_CFA_remember_state";
547 case DW_CFA_restore_state:
548 return "DW_CFA_restore_state";
549 case DW_CFA_def_cfa:
550 return "DW_CFA_def_cfa";
551 case DW_CFA_def_cfa_register:
552 return "DW_CFA_def_cfa_register";
553 case DW_CFA_def_cfa_offset:
554 return "DW_CFA_def_cfa_offset";
6bb28965
JM
555
556 /* DWARF 3 */
7d9d8943
AM
557 case DW_CFA_def_cfa_expression:
558 return "DW_CFA_def_cfa_expression";
6bb28965
JM
559 case DW_CFA_expression:
560 return "DW_CFA_expression";
561 case DW_CFA_offset_extended_sf:
562 return "DW_CFA_offset_extended_sf";
563 case DW_CFA_def_cfa_sf:
564 return "DW_CFA_def_cfa_sf";
565 case DW_CFA_def_cfa_offset_sf:
566 return "DW_CFA_def_cfa_offset_sf";
c53aa195 567
3f76745e
JM
568 /* SGI/MIPS specific */
569 case DW_CFA_MIPS_advance_loc8:
570 return "DW_CFA_MIPS_advance_loc8";
c53aa195
JM
571
572 /* GNU extensions */
573 case DW_CFA_GNU_window_save:
574 return "DW_CFA_GNU_window_save";
0021b564
JM
575 case DW_CFA_GNU_args_size:
576 return "DW_CFA_GNU_args_size";
3f388b42
GK
577 case DW_CFA_GNU_negative_offset_extended:
578 return "DW_CFA_GNU_negative_offset_extended";
c53aa195 579
3f76745e
JM
580 default:
581 return "DW_CFA_<unknown>";
582 }
583}
a3f97cbb 584
3f76745e 585/* Return a pointer to a newly allocated Call Frame Instruction. */
71dfc51f 586
3f76745e 587static inline dw_cfi_ref
7080f735 588new_cfi (void)
3f76745e 589{
703ad42b 590 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
71dfc51f 591
3f76745e
JM
592 cfi->dw_cfi_next = NULL;
593 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
594 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
a3f97cbb 595
3f76745e
JM
596 return cfi;
597}
a3f97cbb 598
3f76745e 599/* Add a Call Frame Instruction to list of instructions. */
a3f97cbb 600
3f76745e 601static inline void
7080f735 602add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
3f76745e 603{
b3694847 604 dw_cfi_ref *p;
a3f97cbb 605
3f76745e
JM
606 /* Find the end of the chain. */
607 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
608 ;
609
610 *p = cfi;
a3f97cbb
JW
611}
612
3f76745e 613/* Generate a new label for the CFI info to refer to. */
71dfc51f 614
c53aa195 615char *
7080f735 616dwarf2out_cfi_label (void)
a3f97cbb 617{
3f76745e 618 static char label[20];
556273e0 619
044b4de3 620 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
3f76745e 621 ASM_OUTPUT_LABEL (asm_out_file, label);
3f76745e 622 return label;
a3f97cbb
JW
623}
624
3f76745e
JM
625/* Add CFI to the current fde at the PC value indicated by LABEL if specified,
626 or to the CIE if LABEL is NULL. */
71dfc51f 627
3f76745e 628static void
7080f735 629add_fde_cfi (const char *label, dw_cfi_ref cfi)
a3f97cbb 630{
3f76745e
JM
631 if (label)
632 {
b3694847 633 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
a3f97cbb 634
3f76745e
JM
635 if (*label == 0)
636 label = dwarf2out_cfi_label ();
71dfc51f 637
3f76745e
JM
638 if (fde->dw_fde_current_label == NULL
639 || strcmp (label, fde->dw_fde_current_label) != 0)
640 {
b3694847 641 dw_cfi_ref xcfi;
a3f97cbb 642
6a10f7b3 643 label = xstrdup (label);
71dfc51f 644
3f76745e
JM
645 /* Set the location counter to the new label. */
646 xcfi = new_cfi ();
6a10f7b3
RS
647 /* If we have a current label, advance from there, otherwise
648 set the location directly using set_loc. */
649 xcfi->dw_cfi_opc = fde->dw_fde_current_label
650 ? DW_CFA_advance_loc4
651 : DW_CFA_set_loc;
3f76745e
JM
652 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
653 add_cfi (&fde->dw_fde_cfi, xcfi);
6a10f7b3
RS
654
655 fde->dw_fde_current_label = label;
3f76745e 656 }
71dfc51f 657
3f76745e
JM
658 add_cfi (&fde->dw_fde_cfi, cfi);
659 }
660
661 else
662 add_cfi (&cie_cfi_head, cfi);
a3f97cbb
JW
663}
664
3f76745e 665/* Subroutine of lookup_cfa. */
71dfc51f 666
30e6f306 667static void
7080f735 668lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
a3f97cbb 669{
3f76745e
JM
670 switch (cfi->dw_cfi_opc)
671 {
672 case DW_CFA_def_cfa_offset:
7d9d8943 673 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
3f76745e 674 break;
f6672e8e
RH
675 case DW_CFA_def_cfa_offset_sf:
676 loc->offset
677 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
678 break;
3f76745e 679 case DW_CFA_def_cfa_register:
7d9d8943 680 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
3f76745e
JM
681 break;
682 case DW_CFA_def_cfa:
7d9d8943
AM
683 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
684 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
685 break;
f6672e8e
RH
686 case DW_CFA_def_cfa_sf:
687 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
688 loc->offset
689 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
690 break;
7d9d8943
AM
691 case DW_CFA_def_cfa_expression:
692 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
3f76745e 693 break;
e9a25f70
JL
694 default:
695 break;
3f76745e 696 }
a3f97cbb
JW
697}
698
3f76745e 699/* Find the previous value for the CFA. */
71dfc51f 700
3f76745e 701static void
7080f735 702lookup_cfa (dw_cfa_location *loc)
a3f97cbb 703{
b3694847 704 dw_cfi_ref cfi;
3f76745e 705
30e6f306 706 loc->reg = INVALID_REGNUM;
7d9d8943
AM
707 loc->offset = 0;
708 loc->indirect = 0;
709 loc->base_offset = 0;
3f76745e
JM
710
711 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
7d9d8943 712 lookup_cfa_1 (cfi, loc);
3f76745e
JM
713
714 if (fde_table_in_use)
a3f97cbb 715 {
b3694847 716 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
3f76745e 717 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
7d9d8943 718 lookup_cfa_1 (cfi, loc);
a3f97cbb
JW
719 }
720}
721
3f76745e 722/* The current rule for calculating the DWARF2 canonical frame address. */
fbfa55b0 723static dw_cfa_location cfa;
71dfc51f 724
3f76745e
JM
725/* The register used for saving registers to the stack, and its offset
726 from the CFA. */
fbfa55b0 727static dw_cfa_location cfa_store;
3f76745e 728
0021b564 729/* The running total of the size of arguments pushed onto the stack. */
799f628a 730static HOST_WIDE_INT args_size;
0021b564 731
b57d9225 732/* The last args_size we actually output. */
799f628a 733static HOST_WIDE_INT old_args_size;
b57d9225 734
3f76745e
JM
735/* Entry point to update the canonical frame address (CFA).
736 LABEL is passed to add_fde_cfi. The value of CFA is now to be
737 calculated from REG+OFFSET. */
738
739void
799f628a 740dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
7d9d8943
AM
741{
742 dw_cfa_location loc;
743 loc.indirect = 0;
744 loc.base_offset = 0;
745 loc.reg = reg;
746 loc.offset = offset;
747 def_cfa_1 (label, &loc);
748}
749
30e6f306
RH
750/* Determine if two dw_cfa_location structures define the same data. */
751
752static bool
753cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
754{
755 return (loc1->reg == loc2->reg
756 && loc1->offset == loc2->offset
757 && loc1->indirect == loc2->indirect
758 && (loc1->indirect == 0
759 || loc1->base_offset == loc2->base_offset));
760}
761
770ca8c6 762/* This routine does the actual work. The CFA is now calculated from
7d9d8943 763 the dw_cfa_location structure. */
2ad9852d 764
7d9d8943 765static void
7080f735 766def_cfa_1 (const char *label, dw_cfa_location *loc_p)
a3f97cbb 767{
b3694847 768 dw_cfi_ref cfi;
7d9d8943 769 dw_cfa_location old_cfa, loc;
3f76745e 770
7d9d8943
AM
771 cfa = *loc_p;
772 loc = *loc_p;
5bef9b1f 773
7d9d8943
AM
774 if (cfa_store.reg == loc.reg && loc.indirect == 0)
775 cfa_store.offset = loc.offset;
3f76745e 776
7d9d8943
AM
777 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
778 lookup_cfa (&old_cfa);
779
2ad9852d 780 /* If nothing changed, no need to issue any call frame instructions. */
30e6f306 781 if (cfa_equal_p (&loc, &old_cfa))
2ad9852d 782 return;
3f76745e
JM
783
784 cfi = new_cfi ();
785
e09bbb25 786 if (loc.reg == old_cfa.reg && !loc.indirect)
a3f97cbb 787 {
f6672e8e
RH
788 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
789 the CFA register did not change but the offset did. */
790 if (loc.offset < 0)
791 {
792 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
793 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
794
795 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
796 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
797 }
798 else
799 {
800 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
801 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
802 }
3f76745e 803 }
a3f97cbb 804
3f76745e 805#ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
30e6f306
RH
806 else if (loc.offset == old_cfa.offset
807 && old_cfa.reg != INVALID_REGNUM
e09bbb25 808 && !loc.indirect)
3f76745e 809 {
770ca8c6
JO
810 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
811 indicating the CFA register has changed to <register> but the
812 offset has not changed. */
3f76745e 813 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
7d9d8943 814 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
3f76745e
JM
815 }
816#endif
a3f97cbb 817
7d9d8943 818 else if (loc.indirect == 0)
3f76745e 819 {
770ca8c6
JO
820 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
821 indicating the CFA register has changed to <register> with
822 the specified offset. */
f6672e8e
RH
823 if (loc.offset < 0)
824 {
825 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
826 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
827
828 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
829 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
830 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
831 }
832 else
833 {
834 cfi->dw_cfi_opc = DW_CFA_def_cfa;
835 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
836 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
837 }
7d9d8943
AM
838 }
839 else
840 {
770ca8c6
JO
841 /* Construct a DW_CFA_def_cfa_expression instruction to
842 calculate the CFA using a full location expression since no
843 register-offset pair is available. */
556273e0 844 struct dw_loc_descr_struct *loc_list;
2ad9852d 845
7d9d8943 846 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
35d177a2 847 loc_list = build_cfa_loc (&loc, 0);
7d9d8943 848 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
a3f97cbb 849 }
3f76745e
JM
850
851 add_fde_cfi (label, cfi);
a3f97cbb
JW
852}
853
3f76745e
JM
854/* Add the CFI for saving a register. REG is the CFA column number.
855 LABEL is passed to add_fde_cfi.
856 If SREG is -1, the register is saved at OFFSET from the CFA;
857 otherwise it is saved in SREG. */
71dfc51f 858
3f76745e 859static void
799f628a 860reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
a3f97cbb 861{
b3694847 862 dw_cfi_ref cfi = new_cfi ();
3f76745e
JM
863
864 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
865
1bac3811 866 if (sreg == INVALID_REGNUM)
a3f97cbb 867 {
3f76745e
JM
868 if (reg & ~0x3f)
869 /* The register number won't fit in 6 bits, so we have to use
870 the long form. */
871 cfi->dw_cfi_opc = DW_CFA_offset_extended;
872 else
873 cfi->dw_cfi_opc = DW_CFA_offset;
874
27c35f4b
HPN
875#ifdef ENABLE_CHECKING
876 {
877 /* If we get an offset that is not a multiple of
878 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
879 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
880 description. */
799f628a 881 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
27c35f4b 882
ced3f397 883 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
27c35f4b
HPN
884 }
885#endif
3f76745e 886 offset /= DWARF_CIE_DATA_ALIGNMENT;
3a88cbd1 887 if (offset < 0)
6bb28965 888 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
2ad9852d 889
3f76745e
JM
890 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
891 }
2c849145 892 else if (sreg == reg)
f8a57be8 893 cfi->dw_cfi_opc = DW_CFA_same_value;
3f76745e
JM
894 else
895 {
896 cfi->dw_cfi_opc = DW_CFA_register;
897 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
898 }
899
900 add_fde_cfi (label, cfi);
901}
902
c53aa195
JM
903/* Add the CFI for saving a register window. LABEL is passed to reg_save.
904 This CFI tells the unwinder that it needs to restore the window registers
905 from the previous frame's window save area.
556273e0 906
c53aa195
JM
907 ??? Perhaps we should note in the CIE where windows are saved (instead of
908 assuming 0(cfa)) and what registers are in the window. */
909
910void
7080f735 911dwarf2out_window_save (const char *label)
c53aa195 912{
b3694847 913 dw_cfi_ref cfi = new_cfi ();
2ad9852d 914
c53aa195
JM
915 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
916 add_fde_cfi (label, cfi);
917}
918
0021b564
JM
919/* Add a CFI to update the running total of the size of arguments
920 pushed onto the stack. */
921
922void
799f628a 923dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
0021b564 924{
b3694847 925 dw_cfi_ref cfi;
b57d9225
JM
926
927 if (size == old_args_size)
928 return;
2ad9852d 929
b57d9225
JM
930 old_args_size = size;
931
932 cfi = new_cfi ();
0021b564
JM
933 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
934 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
935 add_fde_cfi (label, cfi);
936}
937
c53aa195
JM
938/* Entry point for saving a register to the stack. REG is the GCC register
939 number. LABEL and OFFSET are passed to reg_save. */
3f76745e
JM
940
941void
799f628a 942dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
3f76745e 943{
1bac3811 944 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
3f76745e
JM
945}
946
c53aa195
JM
947/* Entry point for saving the return address in the stack.
948 LABEL and OFFSET are passed to reg_save. */
949
950void
799f628a 951dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
c53aa195 952{
1bac3811 953 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
c53aa195
JM
954}
955
956/* Entry point for saving the return address in a register.
957 LABEL and SREG are passed to reg_save. */
958
959void
7080f735 960dwarf2out_return_reg (const char *label, unsigned int sreg)
c53aa195 961{
1bac3811 962 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
c53aa195
JM
963}
964
36bd4303 965#ifdef DWARF2_UNWIND_INFO
3f76745e
JM
966/* Record the initial position of the return address. RTL is
967 INCOMING_RETURN_ADDR_RTX. */
968
969static void
7080f735 970initial_return_save (rtx rtl)
3f76745e 971{
1bac3811 972 unsigned int reg = INVALID_REGNUM;
2ad9852d 973 HOST_WIDE_INT offset = 0;
3f76745e
JM
974
975 switch (GET_CODE (rtl))
976 {
977 case REG:
978 /* RA is in a register. */
2c849145 979 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
3f76745e 980 break;
2ad9852d 981
3f76745e
JM
982 case MEM:
983 /* RA is on the stack. */
984 rtl = XEXP (rtl, 0);
985 switch (GET_CODE (rtl))
986 {
987 case REG:
ced3f397 988 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
3f76745e
JM
989 offset = 0;
990 break;
2ad9852d 991
3f76745e 992 case PLUS:
ced3f397 993 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
3f76745e
JM
994 offset = INTVAL (XEXP (rtl, 1));
995 break;
2ad9852d 996
3f76745e 997 case MINUS:
ced3f397 998 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
3f76745e
JM
999 offset = -INTVAL (XEXP (rtl, 1));
1000 break;
2ad9852d 1001
3f76745e 1002 default:
ced3f397 1003 gcc_unreachable ();
3f76745e 1004 }
2ad9852d 1005
3f76745e 1006 break;
2ad9852d 1007
c53aa195
JM
1008 case PLUS:
1009 /* The return address is at some offset from any value we can
1010 actually load. For instance, on the SPARC it is in %i7+8. Just
1011 ignore the offset for now; it doesn't matter for unwinding frames. */
ced3f397 1012 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
c53aa195
JM
1013 initial_return_save (XEXP (rtl, 0));
1014 return;
2ad9852d 1015
a3f97cbb 1016 default:
ced3f397 1017 gcc_unreachable ();
a3f97cbb 1018 }
3f76745e 1019
f8a57be8
GK
1020 if (reg != DWARF_FRAME_RETURN_COLUMN)
1021 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
a3f97cbb 1022}
36bd4303 1023#endif
a3f97cbb 1024
1ba5ae8f 1025/* Given a SET, calculate the amount of stack adjustment it
30f7a378 1026 contains. */
1ba5ae8f 1027
799f628a 1028static HOST_WIDE_INT
7080f735 1029stack_adjust_offset (rtx pattern)
1ba5ae8f
AH
1030{
1031 rtx src = SET_SRC (pattern);
1032 rtx dest = SET_DEST (pattern);
2ad9852d 1033 HOST_WIDE_INT offset = 0;
1ba5ae8f
AH
1034 enum rtx_code code;
1035
1036 if (dest == stack_pointer_rtx)
1037 {
1038 /* (set (reg sp) (plus (reg sp) (const_int))) */
1039 code = GET_CODE (src);
1040 if (! (code == PLUS || code == MINUS)
1041 || XEXP (src, 0) != stack_pointer_rtx
1042 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1043 return 0;
1044
1045 offset = INTVAL (XEXP (src, 1));
f472fa29
AM
1046 if (code == PLUS)
1047 offset = -offset;
1ba5ae8f 1048 }
3c0cb5de 1049 else if (MEM_P (dest))
1ba5ae8f
AH
1050 {
1051 /* (set (mem (pre_dec (reg sp))) (foo)) */
1052 src = XEXP (dest, 0);
1053 code = GET_CODE (src);
1054
c26fbbca
KH
1055 switch (code)
1056 {
f472fa29
AM
1057 case PRE_MODIFY:
1058 case POST_MODIFY:
1059 if (XEXP (src, 0) == stack_pointer_rtx)
1060 {
1061 rtx val = XEXP (XEXP (src, 1), 1);
1062 /* We handle only adjustments by constant amount. */
ced3f397
NS
1063 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1064 && GET_CODE (val) == CONST_INT);
f472fa29
AM
1065 offset = -INTVAL (val);
1066 break;
1067 }
1068 return 0;
1069
1070 case PRE_DEC:
1071 case POST_DEC:
1072 if (XEXP (src, 0) == stack_pointer_rtx)
1073 {
1074 offset = GET_MODE_SIZE (GET_MODE (dest));
1075 break;
1076 }
1077 return 0;
1078
1079 case PRE_INC:
1080 case POST_INC:
1081 if (XEXP (src, 0) == stack_pointer_rtx)
1082 {
1083 offset = -GET_MODE_SIZE (GET_MODE (dest));
1084 break;
1085 }
1086 return 0;
2ad9852d 1087
f472fa29
AM
1088 default:
1089 return 0;
e2134eea 1090 }
1ba5ae8f
AH
1091 }
1092 else
1093 return 0;
1094
1ba5ae8f
AH
1095 return offset;
1096}
1097
0021b564
JM
1098/* Check INSN to see if it looks like a push or a stack adjustment, and
1099 make a note of it if it does. EH uses this information to find out how
1100 much extra space it needs to pop off the stack. */
1101
1102static void
b73aec4f 1103dwarf2out_stack_adjust (rtx insn, bool after_p)
0021b564 1104{
2ad9852d 1105 HOST_WIDE_INT offset;
d3e3972c 1106 const char *label;
2ad9852d 1107 int i;
0021b564 1108
b298f00f
RH
1109 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1110 with this function. Proper support would require all frame-related
1111 insns to be marked, and to be able to handle saving state around
1112 epilogues textually in the middle of the function. */
1113 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1114 return;
1115
b73aec4f
GK
1116 /* If only calls can throw, and we have a frame pointer,
1117 save up adjustments until we see the CALL_INSN. */
1118 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1119 {
1120 if (CALL_P (insn) && !after_p)
1121 {
1122 /* Extract the size of the args from the CALL rtx itself. */
1123 insn = PATTERN (insn);
1124 if (GET_CODE (insn) == PARALLEL)
1125 insn = XVECEXP (insn, 0, 0);
1126 if (GET_CODE (insn) == SET)
1127 insn = SET_SRC (insn);
1128 gcc_assert (GET_CODE (insn) == CALL);
1129 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1130 }
1131 return;
1132 }
1133
1134 if (CALL_P (insn) && !after_p)
1135 {
1136 if (!flag_asynchronous_unwind_tables)
1137 dwarf2out_args_size ("", args_size);
1138 return;
1139 }
1140 else if (BARRIER_P (insn))
0021b564 1141 {
6020d360
JM
1142 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1143 the compiler will have already emitted a stack adjustment, but
1144 doesn't bother for calls to noreturn functions. */
1145#ifdef STACK_GROWS_DOWNWARD
1146 offset = -args_size;
1147#else
1148 offset = args_size;
1149#endif
0021b564 1150 }
6020d360 1151 else if (GET_CODE (PATTERN (insn)) == SET)
2ad9852d 1152 offset = stack_adjust_offset (PATTERN (insn));
1ba5ae8f
AH
1153 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1154 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1155 {
1156 /* There may be stack adjustments inside compound insns. Search
2ad9852d
RK
1157 for them. */
1158 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1159 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1160 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
0021b564
JM
1161 }
1162 else
1163 return;
0b34cf1e 1164
6020d360
JM
1165 if (offset == 0)
1166 return;
1167
7d9d8943
AM
1168 if (cfa.reg == STACK_POINTER_REGNUM)
1169 cfa.offset += offset;
0021b564
JM
1170
1171#ifndef STACK_GROWS_DOWNWARD
1172 offset = -offset;
1173#endif
2ad9852d 1174
0021b564
JM
1175 args_size += offset;
1176 if (args_size < 0)
1177 args_size = 0;
1178
1179 label = dwarf2out_cfi_label ();
7d9d8943 1180 def_cfa_1 (label, &cfa);
e0c0490b
RH
1181 if (flag_asynchronous_unwind_tables)
1182 dwarf2out_args_size (label, args_size);
0021b564
JM
1183}
1184
17211ab5
GK
1185#endif
1186
fbfa55b0
RH
1187/* We delay emitting a register save until either (a) we reach the end
1188 of the prologue or (b) the register is clobbered. This clusters
1189 register saves so that there are fewer pc advances. */
1190
17211ab5 1191struct queued_reg_save GTY(())
fbfa55b0
RH
1192{
1193 struct queued_reg_save *next;
1194 rtx reg;
799f628a 1195 HOST_WIDE_INT cfa_offset;
f8a57be8 1196 rtx saved_reg;
fbfa55b0
RH
1197};
1198
17211ab5
GK
1199static GTY(()) struct queued_reg_save *queued_reg_saves;
1200
f8a57be8
GK
1201/* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1202struct reg_saved_in_data GTY(()) {
1203 rtx orig_reg;
1204 rtx saved_in_reg;
1205};
1206
1207/* A list of registers saved in other registers.
1208 The list intentionally has a small maximum capacity of 4; if your
1209 port needs more than that, you might consider implementing a
1210 more efficient data structure. */
1211static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1212static GTY(()) size_t num_regs_saved_in_regs;
a1c496cb 1213
17211ab5 1214#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
fbfa55b0
RH
1215static const char *last_reg_save_label;
1216
f8a57be8
GK
1217/* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1218 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1219
fbfa55b0 1220static void
f8a57be8 1221queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
fbfa55b0 1222{
f8a57be8
GK
1223 struct queued_reg_save *q;
1224
1225 /* Duplicates waste space, but it's also necessary to remove them
1226 for correctness, since the queue gets output in reverse
1227 order. */
1228 for (q = queued_reg_saves; q != NULL; q = q->next)
1229 if (REGNO (q->reg) == REGNO (reg))
1230 break;
1231
1232 if (q == NULL)
1233 {
1234 q = ggc_alloc (sizeof (*q));
1235 q->next = queued_reg_saves;
1236 queued_reg_saves = q;
1237 }
fbfa55b0 1238
fbfa55b0
RH
1239 q->reg = reg;
1240 q->cfa_offset = offset;
f8a57be8 1241 q->saved_reg = sreg;
fbfa55b0
RH
1242
1243 last_reg_save_label = label;
1244}
1245
f8a57be8
GK
1246/* Output all the entries in QUEUED_REG_SAVES. */
1247
fbfa55b0 1248static void
7080f735 1249flush_queued_reg_saves (void)
fbfa55b0 1250{
f8a57be8 1251 struct queued_reg_save *q;
fbfa55b0 1252
f8a57be8 1253 for (q = queued_reg_saves; q; q = q->next)
fbfa55b0 1254 {
f8a57be8 1255 size_t i;
1bac3811
RH
1256 unsigned int reg, sreg;
1257
f8a57be8
GK
1258 for (i = 0; i < num_regs_saved_in_regs; i++)
1259 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1260 break;
1261 if (q->saved_reg && i == num_regs_saved_in_regs)
1262 {
ced3f397 1263 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
f8a57be8
GK
1264 num_regs_saved_in_regs++;
1265 }
1266 if (i != num_regs_saved_in_regs)
1267 {
1268 regs_saved_in_regs[i].orig_reg = q->reg;
1269 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1270 }
1271
1bac3811
RH
1272 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1273 if (q->saved_reg)
1274 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1275 else
1276 sreg = INVALID_REGNUM;
1277 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
fbfa55b0
RH
1278 }
1279
1280 queued_reg_saves = NULL;
1281 last_reg_save_label = NULL;
1282}
1283
f8a57be8
GK
1284/* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1285 location for? Or, does it clobber a register which we've previously
1286 said that some other register is saved in, and for which we now
1287 have a new location for? */
1288
fbfa55b0 1289static bool
7080f735 1290clobbers_queued_reg_save (rtx insn)
fbfa55b0
RH
1291{
1292 struct queued_reg_save *q;
1293
c26fbbca 1294 for (q = queued_reg_saves; q; q = q->next)
f8a57be8
GK
1295 {
1296 size_t i;
1297 if (modified_in_p (q->reg, insn))
1298 return true;
1299 for (i = 0; i < num_regs_saved_in_regs; i++)
1300 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1301 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1302 return true;
1303 }
fbfa55b0
RH
1304
1305 return false;
1306}
c26fbbca 1307
150cdc9e
RH
1308/* Entry point for saving the first register into the second. */
1309
1310void
1311dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1312{
1313 size_t i;
1314 unsigned int regno, sregno;
1315
1316 for (i = 0; i < num_regs_saved_in_regs; i++)
1317 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1318 break;
1319 if (i == num_regs_saved_in_regs)
1320 {
1321 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1322 num_regs_saved_in_regs++;
1323 }
1324 regs_saved_in_regs[i].orig_reg = reg;
1325 regs_saved_in_regs[i].saved_in_reg = sreg;
1326
1327 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1328 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1329 reg_save (label, regno, sregno, 0);
1330}
1331
f8a57be8
GK
1332/* What register, if any, is currently saved in REG? */
1333
1334static rtx
1335reg_saved_in (rtx reg)
1336{
1337 unsigned int regn = REGNO (reg);
1338 size_t i;
1339 struct queued_reg_save *q;
a1c496cb 1340
f8a57be8
GK
1341 for (q = queued_reg_saves; q; q = q->next)
1342 if (q->saved_reg && regn == REGNO (q->saved_reg))
1343 return q->reg;
1344
1345 for (i = 0; i < num_regs_saved_in_regs; i++)
1346 if (regs_saved_in_regs[i].saved_in_reg
1347 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1348 return regs_saved_in_regs[i].orig_reg;
1349
1350 return NULL_RTX;
1351}
1352
fbfa55b0 1353
770ca8c6
JO
1354/* A temporary register holding an integral value used in adjusting SP
1355 or setting up the store_reg. The "offset" field holds the integer
1356 value, not an offset. */
fbfa55b0 1357static dw_cfa_location cfa_temp;
770ca8c6
JO
1358
1359/* Record call frame debugging information for an expression EXPR,
1360 which either sets SP or FP (adjusting how we calculate the frame
f8a57be8
GK
1361 address) or saves a register to the stack or another register.
1362 LABEL indicates the address of EXPR.
770ca8c6
JO
1363
1364 This function encodes a state machine mapping rtxes to actions on
1365 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1366 users need not read the source code.
1367
a401107d
JO
1368 The High-Level Picture
1369
1370 Changes in the register we use to calculate the CFA: Currently we
1371 assume that if you copy the CFA register into another register, we
1372 should take the other one as the new CFA register; this seems to
1373 work pretty well. If it's wrong for some target, it's simple
1374 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1375
1376 Changes in the register we use for saving registers to the stack:
1377 This is usually SP, but not always. Again, we deduce that if you
1378 copy SP into another register (and SP is not the CFA register),
1379 then the new register is the one we will be using for register
1380 saves. This also seems to work.
1381
1382 Register saves: There's not much guesswork about this one; if
1383 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1384 register save, and the register used to calculate the destination
1385 had better be the one we think we're using for this purpose.
f8a57be8
GK
1386 It's also assumed that a copy from a call-saved register to another
1387 register is saving that register if RTX_FRAME_RELATED_P is set on
1388 that instruction. If the copy is from a call-saved register to
1389 the *same* register, that means that the register is now the same
1390 value as in the caller.
a401107d
JO
1391
1392 Except: If the register being saved is the CFA register, and the
cc2902df 1393 offset is nonzero, we are saving the CFA, so we assume we have to
a401107d
JO
1394 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1395 the intent is to save the value of SP from the previous frame.
1396
f8a57be8 1397 In addition, if a register has previously been saved to a different
a1c496cb 1398 register,
f8a57be8 1399
770ca8c6
JO
1400 Invariants / Summaries of Rules
1401
a401107d
JO
1402 cfa current rule for calculating the CFA. It usually
1403 consists of a register and an offset.
770ca8c6
JO
1404 cfa_store register used by prologue code to save things to the stack
1405 cfa_store.offset is the offset from the value of
1406 cfa_store.reg to the actual CFA
1407 cfa_temp register holding an integral value. cfa_temp.offset
1408 stores the value, which will be used to adjust the
19ec6a36
AM
1409 stack pointer. cfa_temp is also used like cfa_store,
1410 to track stores to the stack via fp or a temp reg.
c26fbbca 1411
770ca8c6 1412 Rules 1- 4: Setting a register's value to cfa.reg or an expression
7080f735 1413 with cfa.reg as the first operand changes the cfa.reg and its
19ec6a36
AM
1414 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1415 cfa_temp.offset.
770ca8c6
JO
1416
1417 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1418 expression yielding a constant. This sets cfa_temp.reg
1419 and cfa_temp.offset.
1420
1421 Rule 5: Create a new register cfa_store used to save items to the
1422 stack.
1423
19ec6a36 1424 Rules 10-14: Save a register to the stack. Define offset as the
a401107d 1425 difference of the original location and cfa_store's
19ec6a36 1426 location (or cfa_temp's location if cfa_temp is used).
770ca8c6
JO
1427
1428 The Rules
1429
1430 "{a,b}" indicates a choice of a xor b.
1431 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1432
1433 Rule 1:
1434 (set <reg1> <reg2>:cfa.reg)
19ec6a36 1435 effects: cfa.reg = <reg1>
73c68f61 1436 cfa.offset unchanged
19ec6a36
AM
1437 cfa_temp.reg = <reg1>
1438 cfa_temp.offset = cfa.offset
770ca8c6
JO
1439
1440 Rule 2:
2ad9852d
RK
1441 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1442 {<const_int>,<reg>:cfa_temp.reg}))
770ca8c6 1443 effects: cfa.reg = sp if fp used
7080f735 1444 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
770ca8c6
JO
1445 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1446 if cfa_store.reg==sp
1447
1448 Rule 3:
19ec6a36 1449 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
770ca8c6 1450 effects: cfa.reg = fp
7080f735 1451 cfa_offset += +/- <const_int>
770ca8c6
JO
1452
1453 Rule 4:
19ec6a36 1454 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
770ca8c6 1455 constraints: <reg1> != fp
7080f735 1456 <reg1> != sp
770ca8c6 1457 effects: cfa.reg = <reg1>
19ec6a36
AM
1458 cfa_temp.reg = <reg1>
1459 cfa_temp.offset = cfa.offset
770ca8c6
JO
1460
1461 Rule 5:
1462 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1463 constraints: <reg1> != fp
7080f735 1464 <reg1> != sp
770ca8c6 1465 effects: cfa_store.reg = <reg1>
7080f735 1466 cfa_store.offset = cfa.offset - cfa_temp.offset
770ca8c6
JO
1467
1468 Rule 6:
1469 (set <reg> <const_int>)
1470 effects: cfa_temp.reg = <reg>
7080f735 1471 cfa_temp.offset = <const_int>
770ca8c6
JO
1472
1473 Rule 7:
1474 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1475 effects: cfa_temp.reg = <reg1>
1476 cfa_temp.offset |= <const_int>
1477
1478 Rule 8:
1479 (set <reg> (high <exp>))
1480 effects: none
1481
1482 Rule 9:
1483 (set <reg> (lo_sum <exp> <const_int>))
1484 effects: cfa_temp.reg = <reg>
7080f735 1485 cfa_temp.offset = <const_int>
770ca8c6
JO
1486
1487 Rule 10:
1488 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1489 effects: cfa_store.offset -= <const_int>
1490 cfa.offset = cfa_store.offset if cfa.reg == sp
770ca8c6 1491 cfa.reg = sp
19ec6a36 1492 cfa.base_offset = -cfa_store.offset
770ca8c6
JO
1493
1494 Rule 11:
1495 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1496 effects: cfa_store.offset += -/+ mode_size(mem)
1497 cfa.offset = cfa_store.offset if cfa.reg == sp
770ca8c6 1498 cfa.reg = sp
19ec6a36 1499 cfa.base_offset = -cfa_store.offset
770ca8c6
JO
1500
1501 Rule 12:
2ad9852d
RK
1502 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1503
1504 <reg2>)
19ec6a36
AM
1505 effects: cfa.reg = <reg1>
1506 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
770ca8c6
JO
1507
1508 Rule 13:
19ec6a36
AM
1509 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1510 effects: cfa.reg = <reg1>
1511 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1512
1513 Rule 14:
1514 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1515 effects: cfa.reg = <reg1>
1516 cfa.base_offset = -cfa_temp.offset
e54c7471
EB
1517 cfa_temp.offset -= mode_size(mem)
1518
10b01bca
GK
1519 Rule 15:
1520 (set <reg> {unspec, unspec_volatile})
1521 effects: target-dependent */
b664de3a
AM
1522
1523static void
7080f735 1524dwarf2out_frame_debug_expr (rtx expr, const char *label)
b664de3a
AM
1525{
1526 rtx src, dest;
2ad9852d 1527 HOST_WIDE_INT offset;
556273e0
KH
1528
1529 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1530 the PARALLEL independently. The first element is always processed if
770ca8c6 1531 it is a SET. This is for backward compatibility. Other elements
556273e0
KH
1532 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1533 flag is set in them. */
2ad9852d 1534 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
556273e0 1535 {
b664de3a
AM
1536 int par_index;
1537 int limit = XVECLEN (expr, 0);
55d6f631
RS
1538 rtx elem;
1539
1540 /* PARALLELs have strict read-modify-write semantics, so we
1541 ought to evaluate every rvalue before changing any lvalue.
1542 It's cumbersome to do that in general, but there's an
1543 easy approximation that is enough for all current users:
1544 handle register saves before register assignments. */
1545 if (GET_CODE (expr) == PARALLEL)
1546 for (par_index = 0; par_index < limit; par_index++)
1547 {
1548 elem = XVECEXP (expr, 0, par_index);
1549 if (GET_CODE (elem) == SET
1550 && MEM_P (SET_DEST (elem))
1551 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1552 dwarf2out_frame_debug_expr (elem, label);
1553 }
b664de3a
AM
1554
1555 for (par_index = 0; par_index < limit; par_index++)
55d6f631
RS
1556 {
1557 elem = XVECEXP (expr, 0, par_index);
1558 if (GET_CODE (elem) == SET
1559 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1560 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1561 dwarf2out_frame_debug_expr (elem, label);
1562 }
b664de3a
AM
1563 return;
1564 }
556273e0 1565
ced3f397 1566 gcc_assert (GET_CODE (expr) == SET);
b664de3a
AM
1567
1568 src = SET_SRC (expr);
1569 dest = SET_DEST (expr);
1570
2ca202e7 1571 if (REG_P (src))
f8a57be8
GK
1572 {
1573 rtx rsi = reg_saved_in (src);
1574 if (rsi)
1575 src = rsi;
1576 }
1577
b664de3a
AM
1578 switch (GET_CODE (dest))
1579 {
1580 case REG:
b664de3a 1581 switch (GET_CODE (src))
556273e0
KH
1582 {
1583 /* Setting FP from SP. */
1584 case REG:
1585 if (cfa.reg == (unsigned) REGNO (src))
f8a57be8
GK
1586 {
1587 /* Rule 1 */
1588 /* Update the CFA rule wrt SP or FP. Make sure src is
a1c496cb 1589 relative to the current CFA register.
f8a57be8
GK
1590
1591 We used to require that dest be either SP or FP, but the
1592 ARM copies SP to a temporary register, and from there to
1593 FP. So we just rely on the backends to only set
1594 RTX_FRAME_RELATED_P on appropriate insns. */
1595 cfa.reg = REGNO (dest);
1596 cfa_temp.reg = cfa.reg;
1597 cfa_temp.offset = cfa.offset;
1598 }
ced3f397 1599 else
f8a57be8
GK
1600 {
1601 /* Saving a register in a register. */
2c42e193
AM
1602 gcc_assert (!fixed_regs [REGNO (dest)]
1603 /* For the SPARC and its register window. */
1604 || (DWARF_FRAME_REGNUM (REGNO (src))
1605 == DWARF_FRAME_RETURN_COLUMN));
f8a57be8
GK
1606 queue_reg_save (label, src, dest, 0);
1607 }
556273e0 1608 break;
b664de3a 1609
556273e0
KH
1610 case PLUS:
1611 case MINUS:
19ec6a36 1612 case LO_SUM:
556273e0
KH
1613 if (dest == stack_pointer_rtx)
1614 {
770ca8c6 1615 /* Rule 2 */
2618f955
MM
1616 /* Adjusting SP. */
1617 switch (GET_CODE (XEXP (src, 1)))
1618 {
1619 case CONST_INT:
1620 offset = INTVAL (XEXP (src, 1));
1621 break;
1622 case REG:
ced3f397
NS
1623 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1624 == cfa_temp.reg);
770ca8c6 1625 offset = cfa_temp.offset;
2618f955
MM
1626 break;
1627 default:
ced3f397 1628 gcc_unreachable ();
2618f955
MM
1629 }
1630
1631 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1632 {
1633 /* Restoring SP from FP in the epilogue. */
ced3f397 1634 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
7d9d8943 1635 cfa.reg = STACK_POINTER_REGNUM;
2618f955 1636 }
19ec6a36
AM
1637 else if (GET_CODE (src) == LO_SUM)
1638 /* Assume we've set the source reg of the LO_SUM from sp. */
1639 ;
ced3f397
NS
1640 else
1641 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2618f955 1642
19ec6a36 1643 if (GET_CODE (src) != MINUS)
2618f955 1644 offset = -offset;
7d9d8943
AM
1645 if (cfa.reg == STACK_POINTER_REGNUM)
1646 cfa.offset += offset;
1647 if (cfa_store.reg == STACK_POINTER_REGNUM)
1648 cfa_store.offset += offset;
556273e0
KH
1649 }
1650 else if (dest == hard_frame_pointer_rtx)
1651 {
770ca8c6 1652 /* Rule 3 */
2618f955
MM
1653 /* Either setting the FP from an offset of the SP,
1654 or adjusting the FP */
ced3f397 1655 gcc_assert (frame_pointer_needed);
2618f955 1656
ced3f397
NS
1657 gcc_assert (REG_P (XEXP (src, 0))
1658 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1659 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1660 offset = INTVAL (XEXP (src, 1));
1661 if (GET_CODE (src) != MINUS)
1662 offset = -offset;
1663 cfa.offset += offset;
1664 cfa.reg = HARD_FRAME_POINTER_REGNUM;
556273e0
KH
1665 }
1666 else
1667 {
ced3f397 1668 gcc_assert (GET_CODE (src) != MINUS);
b53ef1a2 1669
770ca8c6 1670 /* Rule 4 */
f8cfc6aa 1671 if (REG_P (XEXP (src, 0))
b53ef1a2
NC
1672 && REGNO (XEXP (src, 0)) == cfa.reg
1673 && GET_CODE (XEXP (src, 1)) == CONST_INT)
34ce3d7b
JM
1674 {
1675 /* Setting a temporary CFA register that will be copied
1676 into the FP later on. */
19ec6a36 1677 offset = - INTVAL (XEXP (src, 1));
34ce3d7b
JM
1678 cfa.offset += offset;
1679 cfa.reg = REGNO (dest);
19ec6a36
AM
1680 /* Or used to save regs to the stack. */
1681 cfa_temp.reg = cfa.reg;
1682 cfa_temp.offset = cfa.offset;
34ce3d7b 1683 }
2ad9852d 1684
770ca8c6 1685 /* Rule 5 */
f8cfc6aa 1686 else if (REG_P (XEXP (src, 0))
19ec6a36
AM
1687 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1688 && XEXP (src, 1) == stack_pointer_rtx)
b53ef1a2 1689 {
00a42e21
JM
1690 /* Setting a scratch register that we will use instead
1691 of SP for saving registers to the stack. */
ced3f397 1692 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
b53ef1a2 1693 cfa_store.reg = REGNO (dest);
770ca8c6 1694 cfa_store.offset = cfa.offset - cfa_temp.offset;
b53ef1a2 1695 }
2ad9852d 1696
19ec6a36
AM
1697 /* Rule 9 */
1698 else if (GET_CODE (src) == LO_SUM
1699 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1700 {
1701 cfa_temp.reg = REGNO (dest);
1702 cfa_temp.offset = INTVAL (XEXP (src, 1));
1703 }
1704 else
ced3f397 1705 gcc_unreachable ();
556273e0
KH
1706 }
1707 break;
b664de3a 1708
770ca8c6 1709 /* Rule 6 */
556273e0 1710 case CONST_INT:
770ca8c6
JO
1711 cfa_temp.reg = REGNO (dest);
1712 cfa_temp.offset = INTVAL (src);
556273e0 1713 break;
b664de3a 1714
770ca8c6 1715 /* Rule 7 */
556273e0 1716 case IOR:
ced3f397
NS
1717 gcc_assert (REG_P (XEXP (src, 0))
1718 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1719 && GET_CODE (XEXP (src, 1)) == CONST_INT);
2ad9852d 1720
770ca8c6
JO
1721 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1722 cfa_temp.reg = REGNO (dest);
1723 cfa_temp.offset |= INTVAL (XEXP (src, 1));
556273e0 1724 break;
b664de3a 1725
9ae21d2a
AM
1726 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1727 which will fill in all of the bits. */
1728 /* Rule 8 */
1729 case HIGH:
1730 break;
1731
e54c7471
EB
1732 /* Rule 15 */
1733 case UNSPEC:
1734 case UNSPEC_VOLATILE:
1735 gcc_assert (targetm.dwarf_handle_frame_unspec);
1736 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
150cdc9e 1737 return;
e54c7471 1738
556273e0 1739 default:
ced3f397 1740 gcc_unreachable ();
556273e0 1741 }
2ad9852d 1742
7d9d8943 1743 def_cfa_1 (label, &cfa);
2618f955 1744 break;
b664de3a 1745
2618f955 1746 case MEM:
ced3f397 1747 gcc_assert (REG_P (src));
7d9d8943 1748
7d9d8943
AM
1749 /* Saving a register to the stack. Make sure dest is relative to the
1750 CFA register. */
2618f955
MM
1751 switch (GET_CODE (XEXP (dest, 0)))
1752 {
770ca8c6 1753 /* Rule 10 */
2618f955 1754 /* With a push. */
e2134eea
JH
1755 case PRE_MODIFY:
1756 /* We can't handle variable size modifications. */
ced3f397
NS
1757 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1758 == CONST_INT);
e2134eea
JH
1759 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1760
ced3f397
NS
1761 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1762 && cfa_store.reg == STACK_POINTER_REGNUM);
2ad9852d 1763
e2134eea
JH
1764 cfa_store.offset += offset;
1765 if (cfa.reg == STACK_POINTER_REGNUM)
1766 cfa.offset = cfa_store.offset;
1767
1768 offset = -cfa_store.offset;
1769 break;
2ad9852d 1770
770ca8c6 1771 /* Rule 11 */
2618f955
MM
1772 case PRE_INC:
1773 case PRE_DEC:
1774 offset = GET_MODE_SIZE (GET_MODE (dest));
1775 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1776 offset = -offset;
b664de3a 1777
ced3f397
NS
1778 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1779 && cfa_store.reg == STACK_POINTER_REGNUM);
2ad9852d 1780
7d9d8943
AM
1781 cfa_store.offset += offset;
1782 if (cfa.reg == STACK_POINTER_REGNUM)
1783 cfa.offset = cfa_store.offset;
b664de3a 1784
7d9d8943 1785 offset = -cfa_store.offset;
2618f955 1786 break;
b664de3a 1787
770ca8c6 1788 /* Rule 12 */
2618f955
MM
1789 /* With an offset. */
1790 case PLUS:
1791 case MINUS:
19ec6a36 1792 case LO_SUM:
ced3f397
NS
1793 {
1794 int regno;
a1c496cb 1795
66a5ab40
AM
1796 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1797 && REG_P (XEXP (XEXP (dest, 0), 0)));
ced3f397
NS
1798 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1799 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1800 offset = -offset;
1801
1802 regno = REGNO (XEXP (XEXP (dest, 0), 0));
a1c496cb 1803
ced3f397
NS
1804 if (cfa_store.reg == (unsigned) regno)
1805 offset -= cfa_store.offset;
1806 else
1807 {
1808 gcc_assert (cfa_temp.reg == (unsigned) regno);
1809 offset -= cfa_temp.offset;
1810 }
1811 }
2618f955
MM
1812 break;
1813
770ca8c6 1814 /* Rule 13 */
2618f955
MM
1815 /* Without an offset. */
1816 case REG:
ced3f397
NS
1817 {
1818 int regno = REGNO (XEXP (dest, 0));
a1c496cb 1819
ced3f397
NS
1820 if (cfa_store.reg == (unsigned) regno)
1821 offset = -cfa_store.offset;
1822 else
1823 {
1824 gcc_assert (cfa_temp.reg == (unsigned) regno);
1825 offset = -cfa_temp.offset;
1826 }
1827 }
19ec6a36
AM
1828 break;
1829
1830 /* Rule 14 */
1831 case POST_INC:
ced3f397
NS
1832 gcc_assert (cfa_temp.reg
1833 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
19ec6a36
AM
1834 offset = -cfa_temp.offset;
1835 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2618f955
MM
1836 break;
1837
1838 default:
ced3f397 1839 gcc_unreachable ();
2618f955 1840 }
e09bbb25 1841
556273e0 1842 if (REGNO (src) != STACK_POINTER_REGNUM
e09bbb25
JM
1843 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1844 && (unsigned) REGNO (src) == cfa.reg)
1845 {
1846 /* We're storing the current CFA reg into the stack. */
1847
1848 if (cfa.offset == 0)
1849 {
1850 /* If the source register is exactly the CFA, assume
1851 we're saving SP like any other register; this happens
1852 on the ARM. */
e09bbb25 1853 def_cfa_1 (label, &cfa);
f8a57be8 1854 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
e09bbb25
JM
1855 break;
1856 }
1857 else
1858 {
1859 /* Otherwise, we'll need to look in the stack to
73c68f61 1860 calculate the CFA. */
e09bbb25 1861 rtx x = XEXP (dest, 0);
2ad9852d 1862
f8cfc6aa 1863 if (!REG_P (x))
e09bbb25 1864 x = XEXP (x, 0);
ced3f397 1865 gcc_assert (REG_P (x));
2ad9852d
RK
1866
1867 cfa.reg = REGNO (x);
e09bbb25
JM
1868 cfa.base_offset = offset;
1869 cfa.indirect = 1;
1870 def_cfa_1 (label, &cfa);
1871 break;
1872 }
1873 }
1874
7d9d8943 1875 def_cfa_1 (label, &cfa);
f8a57be8 1876 queue_reg_save (label, src, NULL_RTX, offset);
2618f955
MM
1877 break;
1878
1879 default:
ced3f397 1880 gcc_unreachable ();
2618f955 1881 }
b664de3a
AM
1882}
1883
3f76745e
JM
1884/* Record call frame debugging information for INSN, which either
1885 sets SP or FP (adjusting how we calculate the frame address) or saves a
e0c0490b
RH
1886 register to the stack. If INSN is NULL_RTX, initialize our state.
1887
1888 If AFTER_P is false, we're being called before the insn is emitted,
1889 otherwise after. Call instructions get invoked twice. */
71dfc51f 1890
3f76745e 1891void
e0c0490b 1892dwarf2out_frame_debug (rtx insn, bool after_p)
a3f97cbb 1893{
d3e3972c 1894 const char *label;
b664de3a 1895 rtx src;
3f76745e
JM
1896
1897 if (insn == NULL_RTX)
a3f97cbb 1898 {
f8a57be8 1899 size_t i;
a1c496cb 1900
fbfa55b0
RH
1901 /* Flush any queued register saves. */
1902 flush_queued_reg_saves ();
1903
3f76745e 1904 /* Set up state for generating call frame debug info. */
7d9d8943 1905 lookup_cfa (&cfa);
ced3f397
NS
1906 gcc_assert (cfa.reg
1907 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2ad9852d 1908
7d9d8943
AM
1909 cfa.reg = STACK_POINTER_REGNUM;
1910 cfa_store = cfa;
770ca8c6
JO
1911 cfa_temp.reg = -1;
1912 cfa_temp.offset = 0;
a1c496cb 1913
f8a57be8
GK
1914 for (i = 0; i < num_regs_saved_in_regs; i++)
1915 {
1916 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1917 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1918 }
1919 num_regs_saved_in_regs = 0;
3f76745e
JM
1920 return;
1921 }
1922
4b4bf941 1923 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
fbfa55b0
RH
1924 flush_queued_reg_saves ();
1925
0021b564
JM
1926 if (! RTX_FRAME_RELATED_P (insn))
1927 {
fbfa55b0 1928 if (!ACCUMULATE_OUTGOING_ARGS)
e0c0490b 1929 dwarf2out_stack_adjust (insn, after_p);
0021b564
JM
1930 return;
1931 }
1932
3f76745e 1933 label = dwarf2out_cfi_label ();
07ebc930
RH
1934 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1935 if (src)
1936 insn = XEXP (src, 0);
556273e0 1937 else
07ebc930
RH
1938 insn = PATTERN (insn);
1939
b664de3a 1940 dwarf2out_frame_debug_expr (insn, label);
3f76745e
JM
1941}
1942
17211ab5
GK
1943#endif
1944
1945/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
7080f735
AJ
1946static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1947 (enum dwarf_call_frame_info cfi);
17211ab5
GK
1948
1949static enum dw_cfi_oprnd_type
7080f735 1950dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
17211ab5
GK
1951{
1952 switch (cfi)
1953 {
1954 case DW_CFA_nop:
1955 case DW_CFA_GNU_window_save:
1956 return dw_cfi_oprnd_unused;
1957
1958 case DW_CFA_set_loc:
1959 case DW_CFA_advance_loc1:
1960 case DW_CFA_advance_loc2:
1961 case DW_CFA_advance_loc4:
1962 case DW_CFA_MIPS_advance_loc8:
1963 return dw_cfi_oprnd_addr;
1964
1965 case DW_CFA_offset:
1966 case DW_CFA_offset_extended:
1967 case DW_CFA_def_cfa:
1968 case DW_CFA_offset_extended_sf:
1969 case DW_CFA_def_cfa_sf:
1970 case DW_CFA_restore_extended:
1971 case DW_CFA_undefined:
1972 case DW_CFA_same_value:
1973 case DW_CFA_def_cfa_register:
1974 case DW_CFA_register:
1975 return dw_cfi_oprnd_reg_num;
1976
1977 case DW_CFA_def_cfa_offset:
1978 case DW_CFA_GNU_args_size:
1979 case DW_CFA_def_cfa_offset_sf:
1980 return dw_cfi_oprnd_offset;
7080f735 1981
17211ab5
GK
1982 case DW_CFA_def_cfa_expression:
1983 case DW_CFA_expression:
1984 return dw_cfi_oprnd_loc;
1985
1986 default:
ced3f397 1987 gcc_unreachable ();
17211ab5
GK
1988 }
1989}
1990
1991/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
7080f735
AJ
1992static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1993 (enum dwarf_call_frame_info cfi);
17211ab5
GK
1994
1995static enum dw_cfi_oprnd_type
7080f735 1996dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
17211ab5
GK
1997{
1998 switch (cfi)
1999 {
2000 case DW_CFA_def_cfa:
2001 case DW_CFA_def_cfa_sf:
2002 case DW_CFA_offset:
2003 case DW_CFA_offset_extended_sf:
2004 case DW_CFA_offset_extended:
2005 return dw_cfi_oprnd_offset;
2006
2007 case DW_CFA_register:
2008 return dw_cfi_oprnd_reg_num;
2009
2010 default:
2011 return dw_cfi_oprnd_unused;
2012 }
2013}
2014
2015#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2016
d6b5193b
RS
2017/* Switch to eh_frame_section. If we don't have an eh_frame_section,
2018 switch to the data section instead, and write out a synthetic label
2019 for collect2. */
2020
2021static void
2022switch_to_eh_frame_section (void)
2023{
2024 tree label;
2025
0f67af1c
RS
2026#ifdef EH_FRAME_SECTION_NAME
2027 if (eh_frame_section == 0)
2028 {
2029 int flags;
2030
2031 if (EH_TABLES_CAN_BE_READ_ONLY)
2032 {
2033 int fde_encoding;
2034 int per_encoding;
2035 int lsda_encoding;
2036
2037 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2038 /*global=*/0);
2039 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2040 /*global=*/1);
2041 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2042 /*global=*/0);
2043 flags = ((! flag_pic
2044 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2045 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2046 && (per_encoding & 0x70) != DW_EH_PE_absptr
2047 && (per_encoding & 0x70) != DW_EH_PE_aligned
2048 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2049 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2050 ? 0 : SECTION_WRITE);
2051 }
2052 else
2053 flags = SECTION_WRITE;
2054 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2055 }
2056#endif
2057
d6b5193b
RS
2058 if (eh_frame_section)
2059 switch_to_section (eh_frame_section);
2060 else
2061 {
0f67af1c
RS
2062 /* We have no special eh_frame section. Put the information in
2063 the data section and emit special labels to guide collect2. */
d6b5193b 2064 switch_to_section (data_section);
5880f14f 2065 label = get_file_function_name ("F");
d6b5193b
RS
2066 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2067 targetm.asm_out.globalize_label (asm_out_file,
2068 IDENTIFIER_POINTER (label));
2069 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2070 }
2071}
2072
3f76745e
JM
2073/* Output a Call Frame Information opcode and its operand(s). */
2074
2075static void
7080f735 2076output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3f76745e 2077{
34c80057 2078 unsigned long r;
3f76745e 2079 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2ad9852d
RK
2080 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2081 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
38f9cd4c 2082 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2ad9852d 2083 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3f76745e
JM
2084 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2085 {
34c80057
AM
2086 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2087 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2088 "DW_CFA_offset, column 0x%lx", r);
2e4b9b8c 2089 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3f76745e
JM
2090 }
2091 else if (cfi->dw_cfi_opc == DW_CFA_restore)
34c80057
AM
2092 {
2093 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2094 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2095 "DW_CFA_restore, column 0x%lx", r);
2096 }
3f76745e
JM
2097 else
2098 {
2e4b9b8c
RH
2099 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2100 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3f76745e 2101
3f76745e
JM
2102 switch (cfi->dw_cfi_opc)
2103 {
2104 case DW_CFA_set_loc:
e1f9550a
RH
2105 if (for_eh)
2106 dw2_asm_output_encoded_addr_rtx (
2107 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2108 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
b03e9863 2109 false, NULL);
e1f9550a
RH
2110 else
2111 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2112 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
6a10f7b3 2113 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3f76745e 2114 break;
2ad9852d 2115
3f76745e 2116 case DW_CFA_advance_loc1:
2e4b9b8c
RH
2117 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2118 fde->dw_fde_current_label, NULL);
bb727b5a 2119 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3f76745e 2120 break;
2ad9852d 2121
3f76745e 2122 case DW_CFA_advance_loc2:
2e4b9b8c
RH
2123 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2124 fde->dw_fde_current_label, NULL);
3f76745e
JM
2125 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2126 break;
2ad9852d 2127
3f76745e 2128 case DW_CFA_advance_loc4:
2e4b9b8c
RH
2129 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2130 fde->dw_fde_current_label, NULL);
3f76745e
JM
2131 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2132 break;
2ad9852d 2133
3f76745e 2134 case DW_CFA_MIPS_advance_loc8:
2e4b9b8c
RH
2135 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2136 fde->dw_fde_current_label, NULL);
2137 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3f76745e 2138 break;
2ad9852d 2139
3f76745e
JM
2140 case DW_CFA_offset_extended:
2141 case DW_CFA_def_cfa:
34c80057
AM
2142 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2143 dw2_asm_output_data_uleb128 (r, NULL);
2e4b9b8c 2144 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3f76745e 2145 break;
2ad9852d 2146
6bb28965
JM
2147 case DW_CFA_offset_extended_sf:
2148 case DW_CFA_def_cfa_sf:
34c80057
AM
2149 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2150 dw2_asm_output_data_uleb128 (r, NULL);
6bb28965
JM
2151 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2152 break;
2153
3f76745e
JM
2154 case DW_CFA_restore_extended:
2155 case DW_CFA_undefined:
3f76745e
JM
2156 case DW_CFA_same_value:
2157 case DW_CFA_def_cfa_register:
34c80057
AM
2158 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2159 dw2_asm_output_data_uleb128 (r, NULL);
3f76745e 2160 break;
2ad9852d 2161
3f76745e 2162 case DW_CFA_register:
34c80057
AM
2163 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2164 dw2_asm_output_data_uleb128 (r, NULL);
2165 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2166 dw2_asm_output_data_uleb128 (r, NULL);
3f76745e 2167 break;
2ad9852d 2168
3f76745e 2169 case DW_CFA_def_cfa_offset:
2e4b9b8c
RH
2170 case DW_CFA_GNU_args_size:
2171 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3f76745e 2172 break;
2ad9852d 2173
6bb28965
JM
2174 case DW_CFA_def_cfa_offset_sf:
2175 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2176 break;
2177
c53aa195
JM
2178 case DW_CFA_GNU_window_save:
2179 break;
2ad9852d 2180
7d9d8943 2181 case DW_CFA_def_cfa_expression:
6bb28965 2182 case DW_CFA_expression:
7d9d8943
AM
2183 output_cfa_loc (cfi);
2184 break;
2ad9852d 2185
6bb28965
JM
2186 case DW_CFA_GNU_negative_offset_extended:
2187 /* Obsoleted by DW_CFA_offset_extended_sf. */
ced3f397 2188 gcc_unreachable ();
6bb28965 2189
3f76745e
JM
2190 default:
2191 break;
2192 }
556273e0 2193 }
3f76745e
JM
2194}
2195
34c80057 2196/* Output the call frame information used to record information
3f76745e
JM
2197 that relates to calculating the frame pointer, and records the
2198 location of saved registers. */
2199
2200static void
7080f735 2201output_call_frame_info (int for_eh)
3f76745e 2202{
b3694847
SS
2203 unsigned int i;
2204 dw_fde_ref fde;
2205 dw_cfi_ref cfi;
27d95cbe 2206 char l1[20], l2[20], section_start_label[20];
ad5eeaa9 2207 bool any_lsda_needed = false;
52a11cbf 2208 char augmentation[6];
e1f9550a
RH
2209 int augmentation_size;
2210 int fde_encoding = DW_EH_PE_absptr;
2211 int per_encoding = DW_EH_PE_absptr;
2212 int lsda_encoding = DW_EH_PE_absptr;
32df413a 2213 int return_reg;
3f76745e 2214
29b91443
JM
2215 /* Don't emit a CIE if there won't be any FDEs. */
2216 if (fde_table_in_use == 0)
2217 return;
2218
4746cf84
MA
2219 /* If we make FDEs linkonce, we may have to emit an empty label for
2220 an FDE that wouldn't otherwise be emitted. We want to avoid
2221 having an FDE kept around when the function it refers to is
f1a66265 2222 discarded. Example where this matters: a primary function
4746cf84 2223 template in C++ requires EH information, but an explicit
9cf737f8 2224 specialization doesn't. */
4746cf84
MA
2225 if (TARGET_USES_WEAK_UNWIND_INFO
2226 && ! flag_asynchronous_unwind_tables
2227 && for_eh)
2228 for (i = 0; i < fde_table_in_use; i++)
2229 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2878ea73 2230 && !fde_table[i].uses_eh_lsda
f1a66265 2231 && ! DECL_WEAK (fde_table[i].decl))
5fd9b178 2232 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
eeab4d81 2233 for_eh, /* empty */ 1);
4746cf84 2234
ad5eeaa9
RH
2235 /* If we don't have any functions we'll want to unwind out of, don't
2236 emit any EH unwind information. Note that if exceptions aren't
2237 enabled, we won't have collected nothrow information, and if we
2238 asked for asynchronous tables, we always want this info. */
737faf14
JM
2239 if (for_eh)
2240 {
ad5eeaa9 2241 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2ad9852d
RK
2242
2243 for (i = 0; i < fde_table_in_use; i++)
52a11cbf 2244 if (fde_table[i].uses_eh_lsda)
ad5eeaa9 2245 any_eh_needed = any_lsda_needed = true;
2878ea73 2246 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
aeb732c1 2247 any_eh_needed = true;
0366359a
GK
2248 else if (! fde_table[i].nothrow
2249 && ! fde_table[i].all_throwers_are_sibcalls)
ad5eeaa9 2250 any_eh_needed = true;
52a11cbf
RH
2251
2252 if (! any_eh_needed)
2253 return;
737faf14
JM
2254 }
2255
aa0c1401
JL
2256 /* We're going to be generating comments, so turn on app. */
2257 if (flag_debug_asm)
2258 app_enable ();
956d6950 2259
3f76745e 2260 if (for_eh)
d6b5193b 2261 switch_to_eh_frame_section ();
3f76745e 2262 else
10934c3b
RS
2263 {
2264 if (!debug_frame_section)
2265 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2266 SECTION_DEBUG, NULL);
2267 switch_to_section (debug_frame_section);
2268 }
3f76745e 2269
27d95cbe
RH
2270 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2271 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2272
556273e0 2273 /* Output the CIE. */
a6ab3aad
JM
2274 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2275 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
0c33762a
JM
2276 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2277 dw2_asm_output_data (4, 0xffffffff,
2278 "Initial length escape value indicating 64-bit DWARF extension");
2e4b9b8c
RH
2279 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2280 "Length of Common Information Entry");
a6ab3aad
JM
2281 ASM_OUTPUT_LABEL (asm_out_file, l1);
2282
2e4b9b8c
RH
2283 /* Now that the CIE pointer is PC-relative for EH,
2284 use 0 to identify the CIE. */
2285 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
0c33762a 2286 (for_eh ? 0 : DWARF_CIE_ID),
2e4b9b8c 2287 "CIE Identifier Tag");
3f76745e 2288
2e4b9b8c 2289 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
3f76745e 2290
52a11cbf 2291 augmentation[0] = 0;
e1f9550a 2292 augmentation_size = 0;
52a11cbf 2293 if (for_eh)
a6ab3aad 2294 {
e1f9550a
RH
2295 char *p;
2296
52a11cbf
RH
2297 /* Augmentation:
2298 z Indicates that a uleb128 is present to size the
7080f735 2299 augmentation section.
e1f9550a
RH
2300 L Indicates the encoding (and thus presence) of
2301 an LSDA pointer in the FDE augmentation.
2302 R Indicates a non-default pointer encoding for
2303 FDE code pointers.
2304 P Indicates the presence of an encoding + language
2305 personality routine in the CIE augmentation. */
2306
aeb732c1 2307 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
e1f9550a
RH
2308 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2309 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2310
2311 p = augmentation + 1;
2312 if (eh_personality_libfunc)
2313 {
2314 *p++ = 'P';
2315 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2a6acff7 2316 assemble_external_libcall (eh_personality_libfunc);
e1f9550a 2317 }
52a11cbf 2318 if (any_lsda_needed)
e1f9550a
RH
2319 {
2320 *p++ = 'L';
2321 augmentation_size += 1;
2322 }
2323 if (fde_encoding != DW_EH_PE_absptr)
2324 {
2325 *p++ = 'R';
2326 augmentation_size += 1;
2327 }
2328 if (p > augmentation + 1)
2329 {
2330 augmentation[0] = 'z';
c26fbbca 2331 *p = '\0';
e1f9550a 2332 }
099c8b17
RH
2333
2334 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2335 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2336 {
2337 int offset = ( 4 /* Length */
2338 + 4 /* CIE Id */
2339 + 1 /* CIE version */
2340 + strlen (augmentation) + 1 /* Augmentation */
2341 + size_of_uleb128 (1) /* Code alignment */
2342 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2343 + 1 /* RA column */
2344 + 1 /* Augmentation size */
2345 + 1 /* Personality encoding */ );
2346 int pad = -offset & (PTR_SIZE - 1);
2347
2348 augmentation_size += pad;
2349
2350 /* Augmentations should be small, so there's scarce need to
2351 iterate for a solution. Die if we exceed one uleb128 byte. */
ced3f397 2352 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
099c8b17 2353 }
a6ab3aad 2354 }
3f76745e 2355
2ad9852d 2356 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2e4b9b8c 2357 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2e4b9b8c
RH
2358 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2359 "CIE Data Alignment Factor");
fc9c8ce1 2360
32df413a 2361 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
fc9c8ce1 2362 if (DW_CIE_VERSION == 1)
32df413a 2363 dw2_asm_output_data (1, return_reg, "CIE RA Column");
fc9c8ce1 2364 else
32df413a 2365 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3f76745e 2366
52a11cbf
RH
2367 if (augmentation[0])
2368 {
e1f9550a 2369 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
52a11cbf 2370 if (eh_personality_libfunc)
e1f9550a
RH
2371 {
2372 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2373 eh_data_format_name (per_encoding));
2374 dw2_asm_output_encoded_addr_rtx (per_encoding,
b03e9863
EB
2375 eh_personality_libfunc,
2376 true, NULL);
e1f9550a 2377 }
2ad9852d 2378
e1f9550a
RH
2379 if (any_lsda_needed)
2380 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2381 eh_data_format_name (lsda_encoding));
2ad9852d 2382
e1f9550a
RH
2383 if (fde_encoding != DW_EH_PE_absptr)
2384 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2385 eh_data_format_name (fde_encoding));
52a11cbf
RH
2386 }
2387
3f76745e 2388 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
12f0b96b 2389 output_cfi (cfi, NULL, for_eh);
3f76745e
JM
2390
2391 /* Pad the CIE out to an address sized boundary. */
c26fbbca 2392 ASM_OUTPUT_ALIGN (asm_out_file,
12f0b96b 2393 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
a6ab3aad 2394 ASM_OUTPUT_LABEL (asm_out_file, l2);
3f76745e
JM
2395
2396 /* Loop through all of the FDE's. */
2ad9852d 2397 for (i = 0; i < fde_table_in_use; i++)
3f76745e
JM
2398 {
2399 fde = &fde_table[i];
3f76745e 2400
52a11cbf 2401 /* Don't emit EH unwind info for leaf functions that don't need it. */
ad5eeaa9 2402 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
b6128b8c 2403 && (fde->nothrow || fde->all_throwers_are_sibcalls)
f1a66265 2404 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
b6128b8c 2405 && !fde->uses_eh_lsda)
737faf14
JM
2406 continue;
2407
eeab4d81 2408 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
5fd9b178 2409 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
556273e0
KH
2410 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2411 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
0c33762a
JM
2412 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2413 dw2_asm_output_data (4, 0xffffffff,
2414 "Initial length escape value indicating 64-bit DWARF extension");
2e4b9b8c
RH
2415 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2416 "FDE Length");
a6ab3aad
JM
2417 ASM_OUTPUT_LABEL (asm_out_file, l1);
2418
3f76745e 2419 if (for_eh)
27d95cbe 2420 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3f76745e 2421 else
27d95cbe 2422 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
192d0f89 2423 debug_frame_section, "FDE CIE offset");
3f76745e 2424
e1f9550a
RH
2425 if (for_eh)
2426 {
aeb732c1
GK
2427 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2428 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2429 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2430 sym_ref,
b03e9863 2431 false,
aeb732c1 2432 "FDE initial location");
87c8b4be
CT
2433 if (fde->dw_fde_switched_sections)
2434 {
2878ea73 2435 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
87c8b4be 2436 fde->dw_fde_unlikely_section_label);
2878ea73 2437 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
87c8b4be
CT
2438 fde->dw_fde_hot_section_label);
2439 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2440 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
b03e9863 2441 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
87c8b4be
CT
2442 "FDE initial location");
2443 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2444 fde->dw_fde_hot_section_end_label,
2445 fde->dw_fde_hot_section_label,
2446 "FDE address range");
b03e9863 2447 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
87c8b4be
CT
2448 "FDE initial location");
2449 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2450 fde->dw_fde_unlikely_section_end_label,
2451 fde->dw_fde_unlikely_section_label,
2452 "FDE address range");
2453 }
2454 else
2455 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2456 fde->dw_fde_end, fde->dw_fde_begin,
2457 "FDE address range");
e1f9550a
RH
2458 }
2459 else
2460 {
2461 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2462 "FDE initial location");
87c8b4be
CT
2463 if (fde->dw_fde_switched_sections)
2464 {
2465 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2466 fde->dw_fde_hot_section_label,
2467 "FDE initial location");
2468 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2469 fde->dw_fde_hot_section_end_label,
2470 fde->dw_fde_hot_section_label,
2471 "FDE address range");
2472 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2473 fde->dw_fde_unlikely_section_label,
2474 "FDE initial location");
2878ea73 2475 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
87c8b4be
CT
2476 fde->dw_fde_unlikely_section_end_label,
2477 fde->dw_fde_unlikely_section_label,
2478 "FDE address range");
2479 }
2480 else
2481 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2482 fde->dw_fde_end, fde->dw_fde_begin,
2483 "FDE address range");
e1f9550a 2484 }
3f76745e 2485
52a11cbf
RH
2486 if (augmentation[0])
2487 {
e1f9550a 2488 if (any_lsda_needed)
52a11cbf 2489 {
099c8b17
RH
2490 int size = size_of_encoded_value (lsda_encoding);
2491
2492 if (lsda_encoding == DW_EH_PE_aligned)
2493 {
2494 int offset = ( 4 /* Length */
2495 + 4 /* CIE offset */
2496 + 2 * size_of_encoded_value (fde_encoding)
2497 + 1 /* Augmentation size */ );
2498 int pad = -offset & (PTR_SIZE - 1);
2499
2500 size += pad;
ced3f397 2501 gcc_assert (size_of_uleb128 (size) == 1);
099c8b17
RH
2502 }
2503
2504 dw2_asm_output_data_uleb128 (size, "Augmentation size");
e1f9550a
RH
2505
2506 if (fde->uses_eh_lsda)
73c68f61
SS
2507 {
2508 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
e1f9550a 2509 fde->funcdef_number);
73c68f61 2510 dw2_asm_output_encoded_addr_rtx (
e1f9550a 2511 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
b03e9863 2512 false, "Language Specific Data Area");
73c68f61 2513 }
e1f9550a 2514 else
099c8b17
RH
2515 {
2516 if (lsda_encoding == DW_EH_PE_aligned)
2517 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2ad9852d
RK
2518 dw2_asm_output_data
2519 (size_of_encoded_value (lsda_encoding), 0,
2520 "Language Specific Data Area (none)");
099c8b17 2521 }
52a11cbf
RH
2522 }
2523 else
e1f9550a 2524 dw2_asm_output_data_uleb128 (0, "Augmentation size");
52a11cbf
RH
2525 }
2526
3f76745e
JM
2527 /* Loop through the Call Frame Instructions associated with
2528 this FDE. */
2529 fde->dw_fde_current_label = fde->dw_fde_begin;
2530 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
12f0b96b 2531 output_cfi (cfi, fde, for_eh);
3f76745e 2532
a6ab3aad 2533 /* Pad the FDE out to an address sized boundary. */
c26fbbca 2534 ASM_OUTPUT_ALIGN (asm_out_file,
73c68f61 2535 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
a6ab3aad 2536 ASM_OUTPUT_LABEL (asm_out_file, l2);
3f76745e 2537 }
2e4b9b8c 2538
7606e68f 2539 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2e4b9b8c 2540 dw2_asm_output_data (4, 0, "End of Table");
a6ab3aad
JM
2541#ifdef MIPS_DEBUGGING_INFO
2542 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2543 get a value of 0. Putting .align 0 after the label fixes it. */
2544 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2545#endif
aa0c1401
JL
2546
2547 /* Turn off app to make assembly quicker. */
2548 if (flag_debug_asm)
2549 app_disable ();
a6ab3aad
JM
2550}
2551
3f76745e
JM
2552/* Output a marker (i.e. a label) for the beginning of a function, before
2553 the prologue. */
2554
2555void
7080f735
AJ
2556dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2557 const char *file ATTRIBUTE_UNUSED)
3f76745e
JM
2558{
2559 char label[MAX_ARTIFICIAL_LABEL_BYTES];
375d2edc 2560 char * dup_label;
b3694847 2561 dw_fde_ref fde;
3f76745e 2562
375d2edc 2563 current_function_func_begin_label = NULL;
2a1ee410 2564
951120ea 2565#ifdef TARGET_UNWIND_INFO
2a1ee410
RH
2566 /* ??? current_function_func_begin_label is also used by except.c
2567 for call-site information. We must emit this label if it might
2568 be used. */
2569 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2570 && ! dwarf2out_do_frame ())
2571 return;
2572#else
2573 if (! dwarf2out_do_frame ())
2574 return;
2575#endif
2576
d6b5193b 2577 switch_to_section (function_section (current_function_decl));
3f76745e 2578 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
df696a75 2579 current_function_funcdef_no);
2a1ee410 2580 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
df696a75 2581 current_function_funcdef_no);
375d2edc
GK
2582 dup_label = xstrdup (label);
2583 current_function_func_begin_label = dup_label;
3f76745e 2584
951120ea 2585#ifdef TARGET_UNWIND_INFO
2a1ee410
RH
2586 /* We can elide the fde allocation if we're not emitting debug info. */
2587 if (! dwarf2out_do_frame ())
2588 return;
2589#endif
2590
3f76745e
JM
2591 /* Expand the fde table if necessary. */
2592 if (fde_table_in_use == fde_table_allocated)
2593 {
2594 fde_table_allocated += FDE_TABLE_INCREMENT;
17211ab5
GK
2595 fde_table = ggc_realloc (fde_table,
2596 fde_table_allocated * sizeof (dw_fde_node));
2597 memset (fde_table + fde_table_in_use, 0,
2598 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
a3f97cbb 2599 }
3f76745e
JM
2600
2601 /* Record the FDE associated with this function. */
2602 current_funcdef_fde = fde_table_in_use;
2603
2604 /* Add the new FDE at the end of the fde_table. */
2605 fde = &fde_table[fde_table_in_use++];
4746cf84 2606 fde->decl = current_function_decl;
375d2edc 2607 fde->dw_fde_begin = dup_label;
72478f32 2608 fde->dw_fde_current_label = dup_label;
87c8b4be
CT
2609 fde->dw_fde_hot_section_label = NULL;
2610 fde->dw_fde_hot_section_end_label = NULL;
2611 fde->dw_fde_unlikely_section_label = NULL;
2612 fde->dw_fde_unlikely_section_end_label = NULL;
2613 fde->dw_fde_switched_sections = false;
3f76745e
JM
2614 fde->dw_fde_end = NULL;
2615 fde->dw_fde_cfi = NULL;
df696a75 2616 fde->funcdef_number = current_function_funcdef_no;
97b0ade3 2617 fde->nothrow = TREE_NOTHROW (current_function_decl);
52a11cbf 2618 fde->uses_eh_lsda = cfun->uses_eh_lsda;
b6128b8c 2619 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
737faf14 2620
b57d9225 2621 args_size = old_args_size = 0;
653e276c 2622
2ad9852d
RK
2623 /* We only want to output line number information for the genuine dwarf2
2624 prologue case, not the eh frame case. */
653e276c
NB
2625#ifdef DWARF2_DEBUGGING_INFO
2626 if (file)
2627 dwarf2out_source_line (line, file);
2628#endif
3f76745e
JM
2629}
2630
2631/* Output a marker (i.e. a label) for the absolute end of the generated code
2632 for a function definition. This gets called *after* the epilogue code has
2633 been generated. */
2634
2635void
7080f735
AJ
2636dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2637 const char *file ATTRIBUTE_UNUSED)
3f76745e
JM
2638{
2639 dw_fde_ref fde;
2640 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2641
2642 /* Output a label to mark the endpoint of the code generated for this
3ef42a0c 2643 function. */
df696a75
RH
2644 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2645 current_function_funcdef_no);
3f76745e
JM
2646 ASM_OUTPUT_LABEL (asm_out_file, label);
2647 fde = &fde_table[fde_table_in_use - 1];
2648 fde->dw_fde_end = xstrdup (label);
3f76745e
JM
2649}
2650
2651void
7080f735 2652dwarf2out_frame_init (void)
3f76745e
JM
2653{
2654 /* Allocate the initial hunk of the fde_table. */
703ad42b 2655 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3f76745e
JM
2656 fde_table_allocated = FDE_TABLE_INCREMENT;
2657 fde_table_in_use = 0;
2658
2659 /* Generate the CFA instructions common to all FDE's. Do it now for the
2660 sake of lookup_cfa. */
2661
91193900
AS
2662 /* On entry, the Canonical Frame Address is at SP. */
2663 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
658f32fd
AO
2664
2665#ifdef DWARF2_UNWIND_INFO
2666 if (DWARF2_UNWIND_INFO)
2667 initial_return_save (INCOMING_RETURN_ADDR_RTX);
3f76745e
JM
2668#endif
2669}
2670
2671void
7080f735 2672dwarf2out_frame_finish (void)
3f76745e 2673{
3f76745e 2674 /* Output call frame information. */
658f32fd 2675 if (DWARF2_FRAME_INFO)
3f76745e 2676 output_call_frame_info (0);
2ad9852d 2677
5cf58f28
PB
2678#ifndef TARGET_UNWIND_INFO
2679 /* Output another copy for the unwinder. */
ddee9e8d 2680 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3f76745e 2681 output_call_frame_info (1);
5cf58f28 2682#endif
556273e0 2683}
17211ab5 2684#endif
7d9d8943
AM
2685\f
2686/* And now, the subset of the debugging information support code necessary
2687 for emitting location expressions. */
3f76745e 2688
d5688810
GK
2689/* Data about a single source file. */
2690struct dwarf_file_data GTY(())
2691{
2692 const char * filename;
2693 int emitted_number;
2694};
2695
b9203463
RH
2696/* We need some way to distinguish DW_OP_addr with a direct symbol
2697 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2698#define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2699
2700
7d9d8943
AM
2701typedef struct dw_val_struct *dw_val_ref;
2702typedef struct die_struct *dw_die_ref;
5f754896 2703typedef const struct die_struct *const_dw_die_ref;
7d9d8943 2704typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
63e46568 2705typedef struct dw_loc_list_struct *dw_loc_list_ref;
3f76745e
JM
2706
2707/* Each DIE may have a series of attribute/value pairs. Values
2708 can take on several forms. The forms that are used in this
2709 implementation are listed below. */
2710
17211ab5 2711enum dw_val_class
3f76745e
JM
2712{
2713 dw_val_class_addr,
a20612aa 2714 dw_val_class_offset,
3f76745e 2715 dw_val_class_loc,
63e46568 2716 dw_val_class_loc_list,
2bee6045 2717 dw_val_class_range_list,
3f76745e
JM
2718 dw_val_class_const,
2719 dw_val_class_unsigned_const,
2720 dw_val_class_long_long,
e7ee3914 2721 dw_val_class_vec,
3f76745e
JM
2722 dw_val_class_flag,
2723 dw_val_class_die_ref,
2724 dw_val_class_fde_ref,
2725 dw_val_class_lbl_id,
192d0f89
GK
2726 dw_val_class_lineptr,
2727 dw_val_class_str,
d5688810
GK
2728 dw_val_class_macptr,
2729 dw_val_class_file
17211ab5 2730};
a3f97cbb 2731
3f76745e 2732/* Describe a double word constant value. */
21217bd0 2733/* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
3f76745e 2734
17211ab5 2735typedef struct dw_long_long_struct GTY(())
a3f97cbb 2736{
3f76745e
JM
2737 unsigned long hi;
2738 unsigned long low;
2739}
2740dw_long_long_const;
2741
e7ee3914 2742/* Describe a floating point constant value, or a vector constant value. */
3f76745e 2743
e7ee3914 2744typedef struct dw_vec_struct GTY(())
3f76745e 2745{
e7ee3914 2746 unsigned char * GTY((length ("%h.length"))) array;
3f76745e 2747 unsigned length;
e7ee3914 2748 unsigned elt_size;
3f76745e 2749}
e7ee3914 2750dw_vec_const;
3f76745e 2751
956d6950 2752/* The dw_val_node describes an attribute's value, as it is
3f76745e
JM
2753 represented internally. */
2754
17211ab5 2755typedef struct dw_val_struct GTY(())
3f76745e 2756{
17211ab5
GK
2757 enum dw_val_class val_class;
2758 union dw_val_struct_union
a3f97cbb 2759 {
17211ab5 2760 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
799f628a 2761 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
17211ab5
GK
2762 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2763 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
1431042e 2764 HOST_WIDE_INT GTY ((default)) val_int;
799f628a 2765 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
17211ab5 2766 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
e7ee3914 2767 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
17211ab5 2768 struct dw_val_die_union
2ad9852d
RK
2769 {
2770 dw_die_ref die;
2771 int external;
17211ab5
GK
2772 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2773 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2774 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2775 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2776 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
d5688810 2777 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
a3f97cbb 2778 }
17211ab5 2779 GTY ((desc ("%1.val_class"))) v;
3f76745e
JM
2780}
2781dw_val_node;
2782
2783/* Locations in memory are described using a sequence of stack machine
2784 operations. */
2785
17211ab5 2786typedef struct dw_loc_descr_struct GTY(())
3f76745e
JM
2787{
2788 dw_loc_descr_ref dw_loc_next;
2789 enum dwarf_location_atom dw_loc_opc;
2790 dw_val_node dw_loc_oprnd1;
2791 dw_val_node dw_loc_oprnd2;
d8041cc8 2792 int dw_loc_addr;
3f76745e
JM
2793}
2794dw_loc_descr_node;
2795
63e46568
DB
2796/* Location lists are ranges + location descriptions for that range,
2797 so you can track variables that are in different places over
30f7a378 2798 their entire life. */
17211ab5 2799typedef struct dw_loc_list_struct GTY(())
63e46568
DB
2800{
2801 dw_loc_list_ref dw_loc_next;
2802 const char *begin; /* Label for begin address of range */
2803 const char *end; /* Label for end address of range */
2ad9852d
RK
2804 char *ll_symbol; /* Label for beginning of location list.
2805 Only on head of list */
63e46568
DB
2806 const char *section; /* Section this loclist is relative to */
2807 dw_loc_descr_ref expr;
2808} dw_loc_list_node;
2809
17211ab5
GK
2810#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2811
7080f735
AJ
2812static const char *dwarf_stack_op_name (unsigned);
2813static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
799f628a 2814 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
7080f735
AJ
2815static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2816static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2817static unsigned long size_of_locs (dw_loc_descr_ref);
2818static void output_loc_operands (dw_loc_descr_ref);
2819static void output_loc_sequence (dw_loc_descr_ref);
3f76745e 2820
7d9d8943 2821/* Convert a DWARF stack opcode into its string name. */
3f76745e 2822
7d9d8943 2823static const char *
7080f735 2824dwarf_stack_op_name (unsigned int op)
ef76d03b 2825{
7d9d8943
AM
2826 switch (op)
2827 {
2828 case DW_OP_addr:
b9203463 2829 case INTERNAL_DW_OP_tls_addr:
7d9d8943
AM
2830 return "DW_OP_addr";
2831 case DW_OP_deref:
2832 return "DW_OP_deref";
2833 case DW_OP_const1u:
2834 return "DW_OP_const1u";
2835 case DW_OP_const1s:
2836 return "DW_OP_const1s";
2837 case DW_OP_const2u:
2838 return "DW_OP_const2u";
2839 case DW_OP_const2s:
2840 return "DW_OP_const2s";
2841 case DW_OP_const4u:
2842 return "DW_OP_const4u";
2843 case DW_OP_const4s:
2844 return "DW_OP_const4s";
2845 case DW_OP_const8u:
2846 return "DW_OP_const8u";
2847 case DW_OP_const8s:
2848 return "DW_OP_const8s";
2849 case DW_OP_constu:
2850 return "DW_OP_constu";
2851 case DW_OP_consts:
2852 return "DW_OP_consts";
2853 case DW_OP_dup:
2854 return "DW_OP_dup";
2855 case DW_OP_drop:
2856 return "DW_OP_drop";
2857 case DW_OP_over:
2858 return "DW_OP_over";
2859 case DW_OP_pick:
2860 return "DW_OP_pick";
2861 case DW_OP_swap:
2862 return "DW_OP_swap";
2863 case DW_OP_rot:
2864 return "DW_OP_rot";
2865 case DW_OP_xderef:
2866 return "DW_OP_xderef";
2867 case DW_OP_abs:
2868 return "DW_OP_abs";
2869 case DW_OP_and:
2870 return "DW_OP_and";
2871 case DW_OP_div:
2872 return "DW_OP_div";
2873 case DW_OP_minus:
2874 return "DW_OP_minus";
2875 case DW_OP_mod:
2876 return "DW_OP_mod";
2877 case DW_OP_mul:
2878 return "DW_OP_mul";
2879 case DW_OP_neg:
2880 return "DW_OP_neg";
2881 case DW_OP_not:
2882 return "DW_OP_not";
2883 case DW_OP_or:
2884 return "DW_OP_or";
2885 case DW_OP_plus:
2886 return "DW_OP_plus";
2887 case DW_OP_plus_uconst:
2888 return "DW_OP_plus_uconst";
2889 case DW_OP_shl:
2890 return "DW_OP_shl";
2891 case DW_OP_shr:
2892 return "DW_OP_shr";
2893 case DW_OP_shra:
2894 return "DW_OP_shra";
2895 case DW_OP_xor:
2896 return "DW_OP_xor";
2897 case DW_OP_bra:
2898 return "DW_OP_bra";
2899 case DW_OP_eq:
2900 return "DW_OP_eq";
2901 case DW_OP_ge:
2902 return "DW_OP_ge";
2903 case DW_OP_gt:
2904 return "DW_OP_gt";
2905 case DW_OP_le:
2906 return "DW_OP_le";
2907 case DW_OP_lt:
2908 return "DW_OP_lt";
2909 case DW_OP_ne:
2910 return "DW_OP_ne";
2911 case DW_OP_skip:
2912 return "DW_OP_skip";
2913 case DW_OP_lit0:
2914 return "DW_OP_lit0";
2915 case DW_OP_lit1:
2916 return "DW_OP_lit1";
2917 case DW_OP_lit2:
2918 return "DW_OP_lit2";
2919 case DW_OP_lit3:
2920 return "DW_OP_lit3";
2921 case DW_OP_lit4:
2922 return "DW_OP_lit4";
2923 case DW_OP_lit5:
2924 return "DW_OP_lit5";
2925 case DW_OP_lit6:
2926 return "DW_OP_lit6";
2927 case DW_OP_lit7:
2928 return "DW_OP_lit7";
2929 case DW_OP_lit8:
2930 return "DW_OP_lit8";
2931 case DW_OP_lit9:
2932 return "DW_OP_lit9";
2933 case DW_OP_lit10:
2934 return "DW_OP_lit10";
2935 case DW_OP_lit11:
2936 return "DW_OP_lit11";
2937 case DW_OP_lit12:
2938 return "DW_OP_lit12";
2939 case DW_OP_lit13:
2940 return "DW_OP_lit13";
2941 case DW_OP_lit14:
2942 return "DW_OP_lit14";
2943 case DW_OP_lit15:
2944 return "DW_OP_lit15";
2945 case DW_OP_lit16:
2946 return "DW_OP_lit16";
2947 case DW_OP_lit17:
2948 return "DW_OP_lit17";
2949 case DW_OP_lit18:
2950 return "DW_OP_lit18";
2951 case DW_OP_lit19:
2952 return "DW_OP_lit19";
2953 case DW_OP_lit20:
2954 return "DW_OP_lit20";
2955 case DW_OP_lit21:
2956 return "DW_OP_lit21";
2957 case DW_OP_lit22:
2958 return "DW_OP_lit22";
2959 case DW_OP_lit23:
2960 return "DW_OP_lit23";
2961 case DW_OP_lit24:
2962 return "DW_OP_lit24";
2963 case DW_OP_lit25:
2964 return "DW_OP_lit25";
2965 case DW_OP_lit26:
2966 return "DW_OP_lit26";
2967 case DW_OP_lit27:
2968 return "DW_OP_lit27";
2969 case DW_OP_lit28:
2970 return "DW_OP_lit28";
2971 case DW_OP_lit29:
2972 return "DW_OP_lit29";
2973 case DW_OP_lit30:
2974 return "DW_OP_lit30";
2975 case DW_OP_lit31:
2976 return "DW_OP_lit31";
2977 case DW_OP_reg0:
2978 return "DW_OP_reg0";
2979 case DW_OP_reg1:
2980 return "DW_OP_reg1";
2981 case DW_OP_reg2:
2982 return "DW_OP_reg2";
2983 case DW_OP_reg3:
2984 return "DW_OP_reg3";
2985 case DW_OP_reg4:
2986 return "DW_OP_reg4";
2987 case DW_OP_reg5:
2988 return "DW_OP_reg5";
2989 case DW_OP_reg6:
2990 return "DW_OP_reg6";
2991 case DW_OP_reg7:
2992 return "DW_OP_reg7";
2993 case DW_OP_reg8:
2994 return "DW_OP_reg8";
2995 case DW_OP_reg9:
2996 return "DW_OP_reg9";
2997 case DW_OP_reg10:
2998 return "DW_OP_reg10";
2999 case DW_OP_reg11:
3000 return "DW_OP_reg11";
3001 case DW_OP_reg12:
3002 return "DW_OP_reg12";
3003 case DW_OP_reg13:
3004 return "DW_OP_reg13";
3005 case DW_OP_reg14:
3006 return "DW_OP_reg14";
3007 case DW_OP_reg15:
3008 return "DW_OP_reg15";
3009 case DW_OP_reg16:
3010 return "DW_OP_reg16";
3011 case DW_OP_reg17:
3012 return "DW_OP_reg17";
3013 case DW_OP_reg18:
3014 return "DW_OP_reg18";
3015 case DW_OP_reg19:
3016 return "DW_OP_reg19";
3017 case DW_OP_reg20:
3018 return "DW_OP_reg20";
3019 case DW_OP_reg21:
3020 return "DW_OP_reg21";
3021 case DW_OP_reg22:
3022 return "DW_OP_reg22";
3023 case DW_OP_reg23:
3024 return "DW_OP_reg23";
3025 case DW_OP_reg24:
3026 return "DW_OP_reg24";
3027 case DW_OP_reg25:
3028 return "DW_OP_reg25";
3029 case DW_OP_reg26:
3030 return "DW_OP_reg26";
3031 case DW_OP_reg27:
3032 return "DW_OP_reg27";
3033 case DW_OP_reg28:
3034 return "DW_OP_reg28";
3035 case DW_OP_reg29:
3036 return "DW_OP_reg29";
3037 case DW_OP_reg30:
3038 return "DW_OP_reg30";
3039 case DW_OP_reg31:
3040 return "DW_OP_reg31";
3041 case DW_OP_breg0:
3042 return "DW_OP_breg0";
3043 case DW_OP_breg1:
3044 return "DW_OP_breg1";
3045 case DW_OP_breg2:
3046 return "DW_OP_breg2";
3047 case DW_OP_breg3:
3048 return "DW_OP_breg3";
3049 case DW_OP_breg4:
3050 return "DW_OP_breg4";
3051 case DW_OP_breg5:
3052 return "DW_OP_breg5";
3053 case DW_OP_breg6:
3054 return "DW_OP_breg6";
3055 case DW_OP_breg7:
3056 return "DW_OP_breg7";
3057 case DW_OP_breg8:
3058 return "DW_OP_breg8";
3059 case DW_OP_breg9:
3060 return "DW_OP_breg9";
3061 case DW_OP_breg10:
3062 return "DW_OP_breg10";
3063 case DW_OP_breg11:
3064 return "DW_OP_breg11";
3065 case DW_OP_breg12:
3066 return "DW_OP_breg12";
3067 case DW_OP_breg13:
3068 return "DW_OP_breg13";
3069 case DW_OP_breg14:
3070 return "DW_OP_breg14";
3071 case DW_OP_breg15:
3072 return "DW_OP_breg15";
3073 case DW_OP_breg16:
3074 return "DW_OP_breg16";
3075 case DW_OP_breg17:
3076 return "DW_OP_breg17";
3077 case DW_OP_breg18:
3078 return "DW_OP_breg18";
3079 case DW_OP_breg19:
3080 return "DW_OP_breg19";
3081 case DW_OP_breg20:
3082 return "DW_OP_breg20";
3083 case DW_OP_breg21:
3084 return "DW_OP_breg21";
3085 case DW_OP_breg22:
3086 return "DW_OP_breg22";
3087 case DW_OP_breg23:
3088 return "DW_OP_breg23";
3089 case DW_OP_breg24:
3090 return "DW_OP_breg24";
3091 case DW_OP_breg25:
3092 return "DW_OP_breg25";
3093 case DW_OP_breg26:
3094 return "DW_OP_breg26";
3095 case DW_OP_breg27:
3096 return "DW_OP_breg27";
3097 case DW_OP_breg28:
3098 return "DW_OP_breg28";
3099 case DW_OP_breg29:
3100 return "DW_OP_breg29";
3101 case DW_OP_breg30:
3102 return "DW_OP_breg30";
3103 case DW_OP_breg31:
3104 return "DW_OP_breg31";
3105 case DW_OP_regx:
3106 return "DW_OP_regx";
3107 case DW_OP_fbreg:
3108 return "DW_OP_fbreg";
3109 case DW_OP_bregx:
3110 return "DW_OP_bregx";
3111 case DW_OP_piece:
3112 return "DW_OP_piece";
3113 case DW_OP_deref_size:
3114 return "DW_OP_deref_size";
3115 case DW_OP_xderef_size:
3116 return "DW_OP_xderef_size";
3117 case DW_OP_nop:
3118 return "DW_OP_nop";
b9203463
RH
3119 case DW_OP_push_object_address:
3120 return "DW_OP_push_object_address";
3121 case DW_OP_call2:
3122 return "DW_OP_call2";
3123 case DW_OP_call4:
3124 return "DW_OP_call4";
3125 case DW_OP_call_ref:
3126 return "DW_OP_call_ref";
3127 case DW_OP_GNU_push_tls_address:
3128 return "DW_OP_GNU_push_tls_address";
62760ffd
CT
3129 case DW_OP_GNU_uninit:
3130 return "DW_OP_GNU_uninit";
3f76745e 3131 default:
7d9d8943 3132 return "OP_<unknown>";
3f76745e 3133 }
bdb669cb 3134}
a3f97cbb 3135
7d9d8943
AM
3136/* Return a pointer to a newly allocated location description. Location
3137 descriptions are simple expression terms that can be strung
3138 together to form more complicated location (address) descriptions. */
3139
3140static inline dw_loc_descr_ref
799f628a
JH
3141new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3142 unsigned HOST_WIDE_INT oprnd2)
4b674448 3143{
703ad42b 3144 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
71dfc51f 3145
7d9d8943
AM
3146 descr->dw_loc_opc = op;
3147 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3148 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3149 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3150 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
71dfc51f 3151
7d9d8943
AM
3152 return descr;
3153}
3154
3155/* Add a location description term to a location description expression. */
3156
3157static inline void
7080f735 3158add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
7d9d8943 3159{
b3694847 3160 dw_loc_descr_ref *d;
7d9d8943
AM
3161
3162 /* Find the end of the chain. */
3163 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3164 ;
3165
3166 *d = descr;
3167}
3168
3169/* Return the size of a location descriptor. */
3170
3171static unsigned long
7080f735 3172size_of_loc_descr (dw_loc_descr_ref loc)
7d9d8943 3173{
b3694847 3174 unsigned long size = 1;
7d9d8943
AM
3175
3176 switch (loc->dw_loc_opc)
3177 {
3178 case DW_OP_addr:
b9203463 3179 case INTERNAL_DW_OP_tls_addr:
7d9d8943
AM
3180 size += DWARF2_ADDR_SIZE;
3181 break;
3182 case DW_OP_const1u:
3183 case DW_OP_const1s:
3184 size += 1;
3185 break;
3186 case DW_OP_const2u:
3187 case DW_OP_const2s:
3188 size += 2;
3189 break;
3190 case DW_OP_const4u:
3191 case DW_OP_const4s:
3192 size += 4;
3193 break;
3194 case DW_OP_const8u:
3195 case DW_OP_const8s:
3196 size += 8;
3197 break;
3198 case DW_OP_constu:
3199 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3200 break;
3201 case DW_OP_consts:
3202 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3203 break;
3204 case DW_OP_pick:
3205 size += 1;
3206 break;
3207 case DW_OP_plus_uconst:
3208 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3209 break;
3210 case DW_OP_skip:
3211 case DW_OP_bra:
3212 size += 2;
3213 break;
3214 case DW_OP_breg0:
3215 case DW_OP_breg1:
3216 case DW_OP_breg2:
3217 case DW_OP_breg3:
3218 case DW_OP_breg4:
3219 case DW_OP_breg5:
3220 case DW_OP_breg6:
3221 case DW_OP_breg7:
3222 case DW_OP_breg8:
3223 case DW_OP_breg9:
3224 case DW_OP_breg10:
3225 case DW_OP_breg11:
3226 case DW_OP_breg12:
3227 case DW_OP_breg13:
3228 case DW_OP_breg14:
3229 case DW_OP_breg15:
3230 case DW_OP_breg16:
3231 case DW_OP_breg17:
3232 case DW_OP_breg18:
3233 case DW_OP_breg19:
3234 case DW_OP_breg20:
3235 case DW_OP_breg21:
3236 case DW_OP_breg22:
3237 case DW_OP_breg23:
3238 case DW_OP_breg24:
3239 case DW_OP_breg25:
3240 case DW_OP_breg26:
3241 case DW_OP_breg27:
3242 case DW_OP_breg28:
3243 case DW_OP_breg29:
3244 case DW_OP_breg30:
3245 case DW_OP_breg31:
3246 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3247 break;
3248 case DW_OP_regx:
3249 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3250 break;
3251 case DW_OP_fbreg:
3252 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3253 break;
3254 case DW_OP_bregx:
3255 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3256 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3257 break;
3258 case DW_OP_piece:
3259 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3260 break;
3261 case DW_OP_deref_size:
3262 case DW_OP_xderef_size:
3263 size += 1;
3264 break;
b9203463
RH
3265 case DW_OP_call2:
3266 size += 2;
3267 break;
3268 case DW_OP_call4:
3269 size += 4;
3270 break;
3271 case DW_OP_call_ref:
3272 size += DWARF2_ADDR_SIZE;
3273 break;
3f76745e 3274 default:
7d9d8943 3275 break;
4b674448 3276 }
7d9d8943
AM
3277
3278 return size;
4b674448
JM
3279}
3280
7d9d8943 3281/* Return the size of a series of location descriptors. */
71dfc51f 3282
7d9d8943 3283static unsigned long
7080f735 3284size_of_locs (dw_loc_descr_ref loc)
4b674448 3285{
887dc802 3286 dw_loc_descr_ref l;
2ad9852d 3287 unsigned long size;
7d9d8943 3288
887dc802
GK
3289 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3290 field, to avoid writing to a PCH file. */
3291 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
d8041cc8 3292 {
887dc802
GK
3293 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3294 break;
3295 size += size_of_loc_descr (l);
3296 }
3297 if (! l)
3298 return size;
3299
3300 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3301 {
3302 l->dw_loc_addr = size;
3303 size += size_of_loc_descr (l);
d8041cc8 3304 }
7d9d8943
AM
3305
3306 return size;
4b674448
JM
3307}
3308
7d9d8943 3309/* Output location description stack opcode's operands (if any). */
71dfc51f 3310
7d9d8943 3311static void
7080f735 3312output_loc_operands (dw_loc_descr_ref loc)
a3f97cbb 3313{
b3694847
SS
3314 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3315 dw_val_ref val2 = &loc->dw_loc_oprnd2;
7d9d8943
AM
3316
3317 switch (loc->dw_loc_opc)
a3f97cbb 3318 {
0517872a 3319#ifdef DWARF2_DEBUGGING_INFO
3f76745e 3320 case DW_OP_addr:
2e4b9b8c 3321 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
7d9d8943 3322 break;
3f76745e 3323 case DW_OP_const2u:
3f76745e 3324 case DW_OP_const2s:
2e4b9b8c 3325 dw2_asm_output_data (2, val1->v.val_int, NULL);
7d9d8943 3326 break;
3f76745e 3327 case DW_OP_const4u:
3f76745e 3328 case DW_OP_const4s:
2e4b9b8c 3329 dw2_asm_output_data (4, val1->v.val_int, NULL);
7d9d8943 3330 break;
3f76745e 3331 case DW_OP_const8u:
3f76745e 3332 case DW_OP_const8s:
ced3f397 3333 gcc_assert (HOST_BITS_PER_LONG >= 64);
2e4b9b8c 3334 dw2_asm_output_data (8, val1->v.val_int, NULL);
7d9d8943 3335 break;
0517872a
JM
3336 case DW_OP_skip:
3337 case DW_OP_bra:
d8041cc8
RH
3338 {
3339 int offset;
3340
ced3f397
NS
3341 gcc_assert (val1->val_class == dw_val_class_loc);
3342 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
d8041cc8 3343
2e4b9b8c 3344 dw2_asm_output_data (2, offset, NULL);
d8041cc8 3345 }
0517872a 3346 break;
3139472f
JM
3347#else
3348 case DW_OP_addr:
3349 case DW_OP_const2u:
3350 case DW_OP_const2s:
3351 case DW_OP_const4u:
3352 case DW_OP_const4s:
3353 case DW_OP_const8u:
3354 case DW_OP_const8s:
3355 case DW_OP_skip:
3356 case DW_OP_bra:
3357 /* We currently don't make any attempt to make sure these are
73c68f61
SS
3358 aligned properly like we do for the main unwind info, so
3359 don't support emitting things larger than a byte if we're
3360 only doing unwinding. */
ced3f397 3361 gcc_unreachable ();
0517872a
JM
3362#endif
3363 case DW_OP_const1u:
3364 case DW_OP_const1s:
2e4b9b8c 3365 dw2_asm_output_data (1, val1->v.val_int, NULL);
0517872a 3366 break;
3f76745e 3367 case DW_OP_constu:
2e4b9b8c 3368 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
7d9d8943 3369 break;
3f76745e 3370 case DW_OP_consts:
2e4b9b8c 3371 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
7d9d8943
AM
3372 break;
3373 case DW_OP_pick:
2e4b9b8c 3374 dw2_asm_output_data (1, val1->v.val_int, NULL);
7d9d8943
AM
3375 break;
3376 case DW_OP_plus_uconst:
2e4b9b8c 3377 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
7d9d8943 3378 break;
3f76745e 3379 case DW_OP_breg0:
3f76745e 3380 case DW_OP_breg1:
3f76745e 3381 case DW_OP_breg2:
3f76745e 3382 case DW_OP_breg3:
3f76745e 3383 case DW_OP_breg4:
3f76745e 3384 case DW_OP_breg5:
3f76745e 3385 case DW_OP_breg6:
3f76745e 3386 case DW_OP_breg7:
3f76745e 3387 case DW_OP_breg8:
3f76745e 3388 case DW_OP_breg9:
3f76745e 3389 case DW_OP_breg10:
3f76745e 3390 case DW_OP_breg11:
3f76745e 3391 case DW_OP_breg12:
3f76745e 3392 case DW_OP_breg13:
3f76745e 3393 case DW_OP_breg14:
3f76745e 3394 case DW_OP_breg15:
3f76745e 3395 case DW_OP_breg16:
3f76745e 3396 case DW_OP_breg17:
3f76745e 3397 case DW_OP_breg18:
3f76745e 3398 case DW_OP_breg19:
3f76745e 3399 case DW_OP_breg20:
3f76745e 3400 case DW_OP_breg21:
3f76745e 3401 case DW_OP_breg22:
3f76745e 3402 case DW_OP_breg23:
3f76745e 3403 case DW_OP_breg24:
3f76745e 3404 case DW_OP_breg25:
3f76745e 3405 case DW_OP_breg26:
3f76745e 3406 case DW_OP_breg27:
3f76745e 3407 case DW_OP_breg28:
3f76745e 3408 case DW_OP_breg29:
3f76745e 3409 case DW_OP_breg30:
3f76745e 3410 case DW_OP_breg31:
2e4b9b8c 3411 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
7d9d8943 3412 break;
3f76745e 3413 case DW_OP_regx:
2e4b9b8c 3414 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
7d9d8943 3415 break;
3f76745e 3416 case DW_OP_fbreg:
2e4b9b8c 3417 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
7d9d8943 3418 break;
3f76745e 3419 case DW_OP_bregx:
2e4b9b8c
RH
3420 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3421 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
7d9d8943 3422 break;
3f76745e 3423 case DW_OP_piece:
2e4b9b8c 3424 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
7d9d8943 3425 break;
3f76745e 3426 case DW_OP_deref_size:
3f76745e 3427 case DW_OP_xderef_size:
2e4b9b8c 3428 dw2_asm_output_data (1, val1->v.val_int, NULL);
7d9d8943 3429 break;
b9203463
RH
3430
3431 case INTERNAL_DW_OP_tls_addr:
fdbe66f2
EB
3432 if (targetm.asm_out.output_dwarf_dtprel)
3433 {
3434 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3435 DWARF2_ADDR_SIZE,
3436 val1->v.val_addr);
3437 fputc ('\n', asm_out_file);
3438 }
3439 else
3440 gcc_unreachable ();
b9203463
RH
3441 break;
3442
7d9d8943 3443 default:
3139472f
JM
3444 /* Other codes have no operands. */
3445 break;
7d9d8943
AM
3446 }
3447}
3448
3449/* Output a sequence of location operations. */
3450
3451static void
7080f735 3452output_loc_sequence (dw_loc_descr_ref loc)
7d9d8943
AM
3453{
3454 for (; loc != NULL; loc = loc->dw_loc_next)
3455 {
3456 /* Output the opcode. */
2e4b9b8c
RH
3457 dw2_asm_output_data (1, loc->dw_loc_opc,
3458 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
7d9d8943
AM
3459
3460 /* Output the operand(s) (if any). */
3461 output_loc_operands (loc);
3462 }
3463}
3464
3465/* This routine will generate the correct assembly data for a location
3466 description based on a cfi entry with a complex address. */
3467
3468static void
7080f735 3469output_cfa_loc (dw_cfi_ref cfi)
7d9d8943
AM
3470{
3471 dw_loc_descr_ref loc;
3472 unsigned long size;
3473
3474 /* Output the size of the block. */
3475 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3476 size = size_of_locs (loc);
2e4b9b8c 3477 dw2_asm_output_data_uleb128 (size, NULL);
7d9d8943
AM
3478
3479 /* Now output the operations themselves. */
3480 output_loc_sequence (loc);
3481}
3482
35d177a2
AO
3483/* This function builds a dwarf location descriptor sequence from a
3484 dw_cfa_location, adding the given OFFSET to the result of the
3485 expression. */
7d9d8943
AM
3486
3487static struct dw_loc_descr_struct *
35d177a2 3488build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
7d9d8943
AM
3489{
3490 struct dw_loc_descr_struct *head, *tmp;
3491
35d177a2
AO
3492 offset += cfa->offset;
3493
30e6f306 3494 if (cfa->indirect)
f299afab 3495 {
30e6f306
RH
3496 if (cfa->base_offset)
3497 {
3498 if (cfa->reg <= 31)
3499 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3500 else
3501 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3502 }
3503 else if (cfa->reg <= 31)
3504 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
f299afab 3505 else
30e6f306
RH
3506 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3507
3508 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3509 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3510 add_loc_descr (&head, tmp);
35d177a2 3511 if (offset != 0)
30e6f306 3512 {
35d177a2 3513 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
30e6f306
RH
3514 add_loc_descr (&head, tmp);
3515 }
f299afab 3516 }
f299afab 3517 else
7d9d8943 3518 {
35d177a2 3519 if (offset == 0)
30e6f306
RH
3520 if (cfa->reg <= 31)
3521 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3522 else
3523 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3524 else if (cfa->reg <= 31)
35d177a2 3525 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
30e6f306 3526 else
35d177a2 3527 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
7d9d8943 3528 }
2ad9852d 3529
7d9d8943
AM
3530 return head;
3531}
3532
2ad9852d
RK
3533/* This function fills in aa dw_cfa_location structure from a dwarf location
3534 descriptor sequence. */
7d9d8943
AM
3535
3536static void
7080f735 3537get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
7d9d8943 3538{
556273e0 3539 struct dw_loc_descr_struct *ptr;
7d9d8943
AM
3540 cfa->offset = 0;
3541 cfa->base_offset = 0;
3542 cfa->indirect = 0;
3543 cfa->reg = -1;
3544
3545 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3546 {
3547 enum dwarf_location_atom op = ptr->dw_loc_opc;
2ad9852d 3548
7d9d8943 3549 switch (op)
556273e0 3550 {
7d9d8943
AM
3551 case DW_OP_reg0:
3552 case DW_OP_reg1:
3553 case DW_OP_reg2:
3554 case DW_OP_reg3:
3555 case DW_OP_reg4:
3556 case DW_OP_reg5:
3557 case DW_OP_reg6:
3558 case DW_OP_reg7:
3559 case DW_OP_reg8:
3560 case DW_OP_reg9:
3561 case DW_OP_reg10:
3562 case DW_OP_reg11:
3563 case DW_OP_reg12:
3564 case DW_OP_reg13:
3565 case DW_OP_reg14:
3566 case DW_OP_reg15:
3567 case DW_OP_reg16:
3568 case DW_OP_reg17:
3569 case DW_OP_reg18:
3570 case DW_OP_reg19:
3571 case DW_OP_reg20:
3572 case DW_OP_reg21:
3573 case DW_OP_reg22:
3574 case DW_OP_reg23:
3575 case DW_OP_reg24:
3576 case DW_OP_reg25:
3577 case DW_OP_reg26:
3578 case DW_OP_reg27:
3579 case DW_OP_reg28:
3580 case DW_OP_reg29:
3581 case DW_OP_reg30:
3582 case DW_OP_reg31:
3583 cfa->reg = op - DW_OP_reg0;
3584 break;
3585 case DW_OP_regx:
3586 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3587 break;
3588 case DW_OP_breg0:
3589 case DW_OP_breg1:
3590 case DW_OP_breg2:
3591 case DW_OP_breg3:
3592 case DW_OP_breg4:
3593 case DW_OP_breg5:
3594 case DW_OP_breg6:
3595 case DW_OP_breg7:
3596 case DW_OP_breg8:
3597 case DW_OP_breg9:
3598 case DW_OP_breg10:
3599 case DW_OP_breg11:
3600 case DW_OP_breg12:
3601 case DW_OP_breg13:
3602 case DW_OP_breg14:
3603 case DW_OP_breg15:
3604 case DW_OP_breg16:
3605 case DW_OP_breg17:
3606 case DW_OP_breg18:
3607 case DW_OP_breg19:
3608 case DW_OP_breg20:
3609 case DW_OP_breg21:
3610 case DW_OP_breg22:
3611 case DW_OP_breg23:
3612 case DW_OP_breg24:
3613 case DW_OP_breg25:
3614 case DW_OP_breg26:
3615 case DW_OP_breg27:
3616 case DW_OP_breg28:
3617 case DW_OP_breg29:
3618 case DW_OP_breg30:
3619 case DW_OP_breg31:
3620 cfa->reg = op - DW_OP_breg0;
3621 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3622 break;
3623 case DW_OP_bregx:
3624 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3625 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3626 break;
3627 case DW_OP_deref:
3628 cfa->indirect = 1;
3629 break;
3630 case DW_OP_plus_uconst:
556273e0 3631 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
7d9d8943
AM
3632 break;
3633 default:
ab532386 3634 internal_error ("DW_LOC_OP %s not implemented",
400500c4 3635 dwarf_stack_op_name (ptr->dw_loc_opc));
7d9d8943
AM
3636 }
3637 }
3638}
3639#endif /* .debug_frame support */
3640\f
3641/* And now, the support for symbolic debugging information. */
3642#ifdef DWARF2_DEBUGGING_INFO
3643
117f9d28 3644/* .debug_str support. */
7080f735
AJ
3645static int output_indirect_string (void **, void *);
3646
3647static void dwarf2out_init (const char *);
3648static void dwarf2out_finish (const char *);
3649static void dwarf2out_define (unsigned int, const char *);
3650static void dwarf2out_undef (unsigned int, const char *);
3651static void dwarf2out_start_source_file (unsigned, const char *);
3652static void dwarf2out_end_source_file (unsigned);
3653static void dwarf2out_begin_block (unsigned, unsigned);
3654static void dwarf2out_end_block (unsigned, unsigned);
3655static bool dwarf2out_ignore_block (tree);
3656static void dwarf2out_global_decl (tree);
21d13d83 3657static void dwarf2out_type_decl (tree, int);
6097b0c3 3658static void dwarf2out_imported_module_or_decl (tree, tree);
7080f735 3659static void dwarf2out_abstract_function (tree);
0a2d3d69
DB
3660static void dwarf2out_var_location (rtx);
3661static void dwarf2out_begin_function (tree);
87c8b4be 3662static void dwarf2out_switch_text_section (void);
7f905405
NB
3663
3664/* The debug hooks structure. */
3665
54b6670a 3666const struct gcc_debug_hooks dwarf2_debug_hooks =
7f905405
NB
3667{
3668 dwarf2out_init,
3669 dwarf2out_finish,
3670 dwarf2out_define,
3671 dwarf2out_undef,
3672 dwarf2out_start_source_file,
a5a42b92
NB
3673 dwarf2out_end_source_file,
3674 dwarf2out_begin_block,
e2a12aca 3675 dwarf2out_end_block,
e1772ac0 3676 dwarf2out_ignore_block,
e2a12aca 3677 dwarf2out_source_line,
653e276c 3678 dwarf2out_begin_prologue,
702ada3d 3679 debug_nothing_int_charstar, /* end_prologue */
e2a12aca 3680 dwarf2out_end_epilogue,
0a2d3d69 3681 dwarf2out_begin_function,
2b85879e
NB
3682 debug_nothing_int, /* end_function */
3683 dwarf2out_decl, /* function_decl */
3684 dwarf2out_global_decl,
21d13d83 3685 dwarf2out_type_decl, /* type_decl */
6097b0c3 3686 dwarf2out_imported_module_or_decl,
e1772ac0
NB
3687 debug_nothing_tree, /* deferred_inline_function */
3688 /* The DWARF 2 backend tries to reduce debugging bloat by not
3689 emitting the abstract description of inline functions until
3690 something tries to reference them. */
3691 dwarf2out_abstract_function, /* outlining_inline_function */
33b49800 3692 debug_nothing_rtx, /* label */
014a1138 3693 debug_nothing_int, /* handle_pch */
9e9945c5 3694 dwarf2out_var_location,
87c8b4be 3695 dwarf2out_switch_text_section,
9e9945c5 3696 1 /* start_end_main_source_file */
7f905405 3697};
17211ab5 3698#endif
7f905405 3699\f
7d9d8943
AM
3700/* NOTE: In the comments in this file, many references are made to
3701 "Debugging Information Entries". This term is abbreviated as `DIE'
3702 throughout the remainder of this file. */
3703
3704/* An internal representation of the DWARF output is built, and then
3705 walked to generate the DWARF debugging info. The walk of the internal
3706 representation is done after the entire program has been compiled.
3707 The types below are used to describe the internal representation. */
3708
3709/* Various DIE's use offsets relative to the beginning of the
3710 .debug_info section to refer to each other. */
3711
3712typedef long int dw_offset;
3713
3714/* Define typedefs here to avoid circular dependencies. */
3715
3716typedef struct dw_attr_struct *dw_attr_ref;
3717typedef struct dw_line_info_struct *dw_line_info_ref;
3718typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3719typedef struct pubname_struct *pubname_ref;
a20612aa 3720typedef struct dw_ranges_struct *dw_ranges_ref;
0435c1d5 3721typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
7d9d8943
AM
3722
3723/* Each entry in the line_info_table maintains the file and
3724 line number associated with the label generated for that
3725 entry. The label gives the PC value associated with
3726 the line number entry. */
3727
17211ab5 3728typedef struct dw_line_info_struct GTY(())
7d9d8943
AM
3729{
3730 unsigned long dw_file_num;
3731 unsigned long dw_line_num;
3732}
3733dw_line_info_entry;
3734
3735/* Line information for functions in separate sections; each one gets its
3736 own sequence. */
17211ab5 3737typedef struct dw_separate_line_info_struct GTY(())
7d9d8943
AM
3738{
3739 unsigned long dw_file_num;
3740 unsigned long dw_line_num;
3741 unsigned long function;
3742}
3743dw_separate_line_info_entry;
3744
3745/* Each DIE attribute has a field specifying the attribute kind,
3746 a link to the next attribute in the chain, and an attribute value.
3747 Attributes are typically linked below the DIE they modify. */
3748
17211ab5 3749typedef struct dw_attr_struct GTY(())
7d9d8943
AM
3750{
3751 enum dwarf_attribute dw_attr;
7d9d8943
AM
3752 dw_val_node dw_attr_val;
3753}
3754dw_attr_node;
3755
1a27722f
GK
3756DEF_VEC_O(dw_attr_node);
3757DEF_VEC_ALLOC_O(dw_attr_node,gc);
3758
d6eeb3ba
GK
3759/* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3760 The children of each node form a circular list linked by
3761 die_sib. die_child points to the node *before* the "first" child node. */
7d9d8943 3762
17211ab5 3763typedef struct die_struct GTY(())
7d9d8943
AM
3764{
3765 enum dwarf_tag die_tag;
881c6935 3766 char *die_symbol;
1a27722f 3767 VEC(dw_attr_node,gc) * die_attr;
7d9d8943
AM
3768 dw_die_ref die_parent;
3769 dw_die_ref die_child;
3770 dw_die_ref die_sib;
47fcfa7b 3771 dw_die_ref die_definition; /* ref from a specification to its definition */
7d9d8943
AM
3772 dw_offset die_offset;
3773 unsigned long die_abbrev;
1bfb5f8f 3774 int die_mark;
33c9159e
AH
3775 /* Die is used and must not be pruned as unused. */
3776 int die_perennial_p;
636c7bc4 3777 unsigned int decl_id;
7d9d8943
AM
3778}
3779die_node;
3780
d6eeb3ba
GK
3781/* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3782#define FOR_EACH_CHILD(die, c, expr) do { \
3783 c = die->die_child; \
3784 if (c) do { \
3785 c = c->die_sib; \
3786 expr; \
3787 } while (c != die->die_child); \
3788} while (0)
3789
7d9d8943
AM
3790/* The pubname structure */
3791
17211ab5 3792typedef struct pubname_struct GTY(())
7d9d8943
AM
3793{
3794 dw_die_ref die;
e01e0201 3795 const char *name;
7d9d8943
AM
3796}
3797pubname_entry;
3798
89708594
CT
3799DEF_VEC_O(pubname_entry);
3800DEF_VEC_ALLOC_O(pubname_entry, gc);
3801
17211ab5 3802struct dw_ranges_struct GTY(())
a20612aa 3803{
0435c1d5
AO
3804 /* If this is positive, it's a block number, otherwise it's a
3805 bitwise-negated index into dw_ranges_by_label. */
3806 int num;
3807};
3808
3809struct dw_ranges_by_label_struct GTY(())
3810{
3811 const char *begin;
3812 const char *end;
a20612aa
RH
3813};
3814
7d9d8943 3815/* The limbo die list structure. */
17211ab5 3816typedef struct limbo_die_struct GTY(())
7d9d8943
AM
3817{
3818 dw_die_ref die;
54ba1f0d 3819 tree created_for;
7d9d8943
AM
3820 struct limbo_die_struct *next;
3821}
3822limbo_die_node;
3823
3824/* How to start an assembler comment. */
3825#ifndef ASM_COMMENT_START
3826#define ASM_COMMENT_START ";#"
3827#endif
3828
cc2902df 3829/* Define a macro which returns nonzero for a TYPE_DECL which was
7d9d8943
AM
3830 implicitly generated for a tagged type.
3831
3832 Note that unlike the gcc front end (which generates a NULL named
3833 TYPE_DECL node for each complete tagged type, each array type, and
3834 each function type node created) the g++ front end generates a
3835 _named_ TYPE_DECL node for each tagged type node created.
3836 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3837 generate a DW_TAG_typedef DIE for them. */
3838
3839#define TYPE_DECL_IS_STUB(decl) \
3840 (DECL_NAME (decl) == NULL_TREE \
3841 || (DECL_ARTIFICIAL (decl) \
3842 && is_tagged_type (TREE_TYPE (decl)) \
3843 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3844 /* This is necessary for stub decls that \
3845 appear in nested inline functions. */ \
3846 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3847 && (decl_ultimate_origin (decl) \
3848 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3849
3850/* Information concerning the compilation unit's programming
3851 language, and compiler version. */
3852
7d9d8943 3853/* Fixed size portion of the DWARF compilation unit header. */
9eb0ef7a
KB
3854#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3855 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
7d9d8943 3856
7d9d8943
AM
3857/* Fixed size portion of public names info. */
3858#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3859
3860/* Fixed size portion of the address range info. */
3861#define DWARF_ARANGES_HEADER_SIZE \
c583e7c3 3862 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2878ea73 3863 DWARF2_ADDR_SIZE * 2) \
c583e7c3 3864 - DWARF_INITIAL_LENGTH_SIZE)
7d9d8943
AM
3865
3866/* Size of padding portion in the address range info. It must be
3867 aligned to twice the pointer size. */
3868#define DWARF_ARANGES_PAD_SIZE \
c583e7c3 3869 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2878ea73 3870 DWARF2_ADDR_SIZE * 2) \
c583e7c3 3871 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
7d9d8943 3872
9d147085 3873/* Use assembler line directives if available. */
7d9d8943 3874#ifndef DWARF2_ASM_LINE_DEBUG_INFO
9d147085
RH
3875#ifdef HAVE_AS_DWARF2_DEBUG_LINE
3876#define DWARF2_ASM_LINE_DEBUG_INFO 1
3877#else
7d9d8943
AM
3878#define DWARF2_ASM_LINE_DEBUG_INFO 0
3879#endif
9d147085 3880#endif
7d9d8943 3881
7d9d8943
AM
3882/* Minimum line offset in a special line info. opcode.
3883 This value was chosen to give a reasonable range of values. */
3884#define DWARF_LINE_BASE -10
3885
a1f300c0 3886/* First special line opcode - leave room for the standard opcodes. */
7d9d8943
AM
3887#define DWARF_LINE_OPCODE_BASE 10
3888
3889/* Range of line offsets in a special line info. opcode. */
3890#define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3891
3892/* Flag that indicates the initial value of the is_stmt_start flag.
3893 In the present implementation, we do not mark any lines as
3894 the beginning of a source statement, because that information
3895 is not made available by the GCC front-end. */
3896#define DWARF_LINE_DEFAULT_IS_STMT_START 1
3897
c1b50e49 3898#ifdef DWARF2_DEBUGGING_INFO
7d9d8943
AM
3899/* This location is used by calc_die_sizes() to keep track
3900 the offset of each DIE within the .debug_info section. */
3901static unsigned long next_die_offset;
c1b50e49 3902#endif
7d9d8943
AM
3903
3904/* Record the root of the DIE's built for the current compilation unit. */
17211ab5 3905static GTY(()) dw_die_ref comp_unit_die;
7d9d8943
AM
3906
3907/* A list of DIEs with a NULL parent waiting to be relocated. */
17211ab5 3908static GTY(()) limbo_die_node *limbo_die_list;
7d9d8943 3909
981975b6 3910/* Filenames referenced by this compilation unit. */
d5688810 3911static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2e18bbae 3912
636c7bc4
JZ
3913/* A hash table of references to DIE's that describe declarations.
3914 The key is a DECL_UID() which is a unique number identifying each decl. */
3915static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
7d9d8943 3916
0a2d3d69
DB
3917/* Node of the variable location list. */
3918struct var_loc_node GTY ((chain_next ("%h.next")))
3919{
3920 rtx GTY (()) var_loc_note;
3921 const char * GTY (()) label;
87c8b4be 3922 const char * GTY (()) section_label;
0a2d3d69
DB
3923 struct var_loc_node * GTY (()) next;
3924};
3925
3926/* Variable location list. */
3927struct var_loc_list_def GTY (())
3928{
3929 struct var_loc_node * GTY (()) first;
3930
3931 /* Do not mark the last element of the chained list because
3932 it is marked through the chain. */
3933 struct var_loc_node * GTY ((skip ("%h"))) last;
3934
3935 /* DECL_UID of the variable decl. */
3936 unsigned int decl_id;
3937};
3938typedef struct var_loc_list_def var_loc_list;
3939
0a2d3d69
DB
3940
3941/* Table of decl location linked lists. */
3942static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3943
7d9d8943
AM
3944/* A pointer to the base of a list of references to DIE's that
3945 are uniquely identified by their tag, presence/absence of
3946 children DIE's, and list of attribute/value pairs. */
7080f735 3947static GTY((length ("abbrev_die_table_allocated")))
17211ab5 3948 dw_die_ref *abbrev_die_table;
7d9d8943
AM
3949
3950/* Number of elements currently allocated for abbrev_die_table. */
c2e9147c 3951static GTY(()) unsigned abbrev_die_table_allocated;
7d9d8943
AM
3952
3953/* Number of elements in type_die_table currently in use. */
c2e9147c 3954static GTY(()) unsigned abbrev_die_table_in_use;
7d9d8943
AM
3955
3956/* Size (in elements) of increments by which we may expand the
3957 abbrev_die_table. */
3958#define ABBREV_DIE_TABLE_INCREMENT 256
3959
3960/* A pointer to the base of a table that contains line information
3961 for each source code line in .text in the compilation unit. */
7080f735 3962static GTY((length ("line_info_table_allocated")))
17211ab5 3963 dw_line_info_ref line_info_table;
7d9d8943
AM
3964
3965/* Number of elements currently allocated for line_info_table. */
c2e9147c 3966static GTY(()) unsigned line_info_table_allocated;
7d9d8943 3967
17211ab5 3968/* Number of elements in line_info_table currently in use. */
c2e9147c 3969static GTY(()) unsigned line_info_table_in_use;
7d9d8943 3970
1146e682
GK
3971/* True if the compilation unit places functions in more than one section. */
3972static GTY(()) bool have_multiple_function_sections = false;
43f237b4 3973
7d9d8943
AM
3974/* A pointer to the base of a table that contains line information
3975 for each source code line outside of .text in the compilation unit. */
17211ab5
GK
3976static GTY ((length ("separate_line_info_table_allocated")))
3977 dw_separate_line_info_ref separate_line_info_table;
7d9d8943
AM
3978
3979/* Number of elements currently allocated for separate_line_info_table. */
c2e9147c 3980static GTY(()) unsigned separate_line_info_table_allocated;
7d9d8943 3981
17211ab5 3982/* Number of elements in separate_line_info_table currently in use. */
c2e9147c 3983static GTY(()) unsigned separate_line_info_table_in_use;
7d9d8943
AM
3984
3985/* Size (in elements) of increments by which we may expand the
3986 line_info_table. */
3987#define LINE_INFO_TABLE_INCREMENT 1024
3988
3989/* A pointer to the base of a table that contains a list of publicly
3990 accessible names. */
89708594 3991static GTY (()) VEC (pubname_entry, gc) * pubname_table;
7d9d8943 3992
89708594
CT
3993/* A pointer to the base of a table that contains a list of publicly
3994 accessible types. */
3995static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
7d9d8943 3996
a20612aa 3997/* Array of dies for which we should generate .debug_arange info. */
17211ab5 3998static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
7d9d8943
AM
3999
4000/* Number of elements currently allocated for arange_table. */
c2e9147c 4001static GTY(()) unsigned arange_table_allocated;
7d9d8943
AM
4002
4003/* Number of elements in arange_table currently in use. */
c2e9147c 4004static GTY(()) unsigned arange_table_in_use;
7d9d8943
AM
4005
4006/* Size (in elements) of increments by which we may expand the
4007 arange_table. */
4008#define ARANGE_TABLE_INCREMENT 64
4009
a20612aa 4010/* Array of dies for which we should generate .debug_ranges info. */
17211ab5 4011static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
a20612aa
RH
4012
4013/* Number of elements currently allocated for ranges_table. */
c2e9147c 4014static GTY(()) unsigned ranges_table_allocated;
a20612aa
RH
4015
4016/* Number of elements in ranges_table currently in use. */
c2e9147c 4017static GTY(()) unsigned ranges_table_in_use;
a20612aa 4018
0435c1d5
AO
4019/* Array of pairs of labels referenced in ranges_table. */
4020static GTY ((length ("ranges_by_label_allocated")))
4021 dw_ranges_by_label_ref ranges_by_label;
4022
4023/* Number of elements currently allocated for ranges_by_label. */
4024static GTY(()) unsigned ranges_by_label_allocated;
4025
4026/* Number of elements in ranges_by_label currently in use. */
4027static GTY(()) unsigned ranges_by_label_in_use;
4028
a20612aa
RH
4029/* Size (in elements) of increments by which we may expand the
4030 ranges_table. */
4031#define RANGES_TABLE_INCREMENT 64
4032
63e46568 4033/* Whether we have location lists that need outputting */
1146e682 4034static GTY(()) bool have_location_lists;
63e46568 4035
57d4f65c
ZW
4036/* Unique label counter. */
4037static GTY(()) unsigned int loclabel_num;
4038
c2e9147c 4039#ifdef DWARF2_DEBUGGING_INFO
7d9d8943
AM
4040/* Record whether the function being analyzed contains inlined functions. */
4041static int current_function_has_inlines;
c1b50e49 4042#endif
7d9d8943
AM
4043#if 0 && defined (MIPS_DEBUGGING_INFO)
4044static int comp_unit_has_inlines;
4045#endif
4046
d5688810
GK
4047/* The last file entry emitted by maybe_emit_file(). */
4048static GTY(()) struct dwarf_file_data * last_emitted_file;
c2e9147c 4049
71c0e7fc 4050/* Number of internal labels generated by gen_internal_sym(). */
c2e9147c
GK
4051static GTY(()) int label_num;
4052
66cccea6
AP
4053/* Cached result of previous call to lookup_filename. */
4054static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4055
17211ab5
GK
4056#ifdef DWARF2_DEBUGGING_INFO
4057
35d177a2 4058/* Offset from the "steady-state frame pointer" to the frame base,
30e6f306 4059 within the current function. */
35d177a2 4060static HOST_WIDE_INT frame_pointer_fb_offset;
30e6f306 4061
7d9d8943
AM
4062/* Forward declarations for functions defined in this file. */
4063
7080f735
AJ
4064static int is_pseudo_reg (rtx);
4065static tree type_main_variant (tree);
4066static int is_tagged_type (tree);
4067static const char *dwarf_tag_name (unsigned);
4068static const char *dwarf_attr_name (unsigned);
4069static const char *dwarf_form_name (unsigned);
7080f735
AJ
4070static tree decl_ultimate_origin (tree);
4071static tree block_ultimate_origin (tree);
4072static tree decl_class_context (tree);
4073static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4074static inline enum dw_val_class AT_class (dw_attr_ref);
4075static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4076static inline unsigned AT_flag (dw_attr_ref);
799f628a
JH
4077static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4078static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4079static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4080static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
7080f735
AJ
4081static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4082 unsigned long);
e7ee3914
AM
4083static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4084 unsigned int, unsigned char *);
7080f735
AJ
4085static hashval_t debug_str_do_hash (const void *);
4086static int debug_str_eq (const void *, const void *);
4087static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4088static inline const char *AT_string (dw_attr_ref);
4089static int AT_string_form (dw_attr_ref);
4090static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
47fcfa7b 4091static void add_AT_specification (dw_die_ref, dw_die_ref);
7080f735
AJ
4092static inline dw_die_ref AT_ref (dw_attr_ref);
4093static inline int AT_ref_external (dw_attr_ref);
4094static inline void set_AT_ref_external (dw_attr_ref, int);
4095static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4096static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4097static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4098static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4099 dw_loc_list_ref);
4100static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4101static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4102static inline rtx AT_addr (dw_attr_ref);
4103static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
192d0f89
GK
4104static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4105static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
799f628a
JH
4106static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4107 unsigned HOST_WIDE_INT);
7080f735
AJ
4108static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4109 unsigned long);
4110static inline const char *AT_lbl (dw_attr_ref);
4111static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4112static const char *get_AT_low_pc (dw_die_ref);
4113static const char *get_AT_hi_pc (dw_die_ref);
4114static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4115static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4116static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4117static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4118static bool is_c_family (void);
4119static bool is_cxx (void);
4120static bool is_java (void);
4121static bool is_fortran (void);
4122static bool is_ada (void);
4123static void remove_AT (dw_die_ref, enum dwarf_attribute);
6097b0c3 4124static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
7080f735
AJ
4125static void add_child_die (dw_die_ref, dw_die_ref);
4126static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4127static dw_die_ref lookup_type_die (tree);
4128static void equate_type_number_to_die (tree, dw_die_ref);
636c7bc4
JZ
4129static hashval_t decl_die_table_hash (const void *);
4130static int decl_die_table_eq (const void *, const void *);
7080f735 4131static dw_die_ref lookup_decl_die (tree);
0a2d3d69
DB
4132static hashval_t decl_loc_table_hash (const void *);
4133static int decl_loc_table_eq (const void *, const void *);
4134static var_loc_list *lookup_decl_loc (tree);
7080f735 4135static void equate_decl_number_to_die (tree, dw_die_ref);
0a2d3d69 4136static void add_var_loc_to_decl (tree, struct var_loc_node *);
7080f735
AJ
4137static void print_spaces (FILE *);
4138static void print_die (dw_die_ref, FILE *);
4139static void print_dwarf_line_table (FILE *);
7080f735
AJ
4140static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4141static dw_die_ref pop_compile_unit (dw_die_ref);
4142static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4143static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4144static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4145static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4146static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4147static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4148static int same_die_p (dw_die_ref, dw_die_ref, int *);
4149static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4150static void compute_section_prefix (dw_die_ref);
4151static int is_type_die (dw_die_ref);
4152static int is_comdat_die (dw_die_ref);
4153static int is_symbol_die (dw_die_ref);
4154static void assign_symbol_names (dw_die_ref);
4155static void break_out_includes (dw_die_ref);
4156static hashval_t htab_cu_hash (const void *);
4157static int htab_cu_eq (const void *, const void *);
4158static void htab_cu_del (void *);
4159static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4160static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4161static void add_sibling_attributes (dw_die_ref);
4162static void build_abbrev_table (dw_die_ref);
4163static void output_location_lists (dw_die_ref);
4164static int constant_size (long unsigned);
4165static unsigned long size_of_die (dw_die_ref);
4166static void calc_die_sizes (dw_die_ref);
4167static void mark_dies (dw_die_ref);
4168static void unmark_dies (dw_die_ref);
4169static void unmark_all_dies (dw_die_ref);
89708594 4170static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
7080f735
AJ
4171static unsigned long size_of_aranges (void);
4172static enum dwarf_form value_format (dw_attr_ref);
4173static void output_value_format (dw_attr_ref);
4174static void output_abbrev_section (void);
4175static void output_die_symbol (dw_die_ref);
4176static void output_die (dw_die_ref);
4177static void output_compilation_unit_header (void);
4178static void output_comp_unit (dw_die_ref, int);
4179static const char *dwarf2_name (tree, int);
4180static void add_pubname (tree, dw_die_ref);
89708594
CT
4181static void add_pubtype (tree, dw_die_ref);
4182static void output_pubnames (VEC (pubname_entry,gc) *);
7080f735
AJ
4183static void add_arange (tree, dw_die_ref);
4184static void output_aranges (void);
0435c1d5 4185static unsigned int add_ranges_num (int);
7080f735 4186static unsigned int add_ranges (tree);
0435c1d5 4187static unsigned int add_ranges_by_labels (const char *, const char *);
7080f735
AJ
4188static void output_ranges (void);
4189static void output_line_info (void);
4190static void output_file_names (void);
4191static dw_die_ref base_type_die (tree);
7080f735 4192static int is_base_type (tree);
e7d23ce3 4193static bool is_subrange_type (tree);
fbfd77b8 4194static dw_die_ref subrange_type_die (tree, dw_die_ref);
7080f735
AJ
4195static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4196static int type_is_enum (tree);
23959f19 4197static unsigned int dbx_reg_number (rtx);
216448c7 4198static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
62760ffd
CT
4199static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4200static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4201 enum var_init_status);
4202static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4203 enum var_init_status);
7080f735 4204static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
62760ffd
CT
4205static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4206 enum var_init_status);
7080f735 4207static int is_based_loc (rtx);
62760ffd
CT
4208static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4209 enum var_init_status);
4210static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4211 enum var_init_status);
4212static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
1a186ec5
RH
4213static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4214static dw_loc_descr_ref loc_descriptor_from_tree (tree);
7080f735
AJ
4215static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4216static tree field_type (tree);
4217static unsigned int simple_type_align_in_bits (tree);
4218static unsigned int simple_decl_align_in_bits (tree);
4219static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4220static HOST_WIDE_INT field_byte_offset (tree);
4221static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4222 dw_loc_descr_ref);
4223static void add_data_member_location_attribute (dw_die_ref, tree);
4224static void add_const_value_attribute (dw_die_ref, rtx);
e7ee3914
AM
4225static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4226static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4227static void insert_float (rtx, unsigned char *);
7080f735 4228static rtx rtl_for_decl_location (tree);
0a2d3d69
DB
4229static void add_location_or_const_value_attribute (dw_die_ref, tree,
4230 enum dwarf_attribute);
7080f735
AJ
4231static void tree_add_const_value_attribute (dw_die_ref, tree);
4232static void add_name_attribute (dw_die_ref, const char *);
4233static void add_comp_dir_attribute (dw_die_ref);
4234static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4235static void add_subscript_info (dw_die_ref, tree);
4236static void add_byte_size_attribute (dw_die_ref, tree);
4237static void add_bit_offset_attribute (dw_die_ref, tree);
4238static void add_bit_size_attribute (dw_die_ref, tree);
4239static void add_prototyped_attribute (dw_die_ref, tree);
4240static void add_abstract_origin_attribute (dw_die_ref, tree);
4241static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4242static void add_src_coords_attributes (dw_die_ref, tree);
4243static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4244static void push_decl_scope (tree);
4245static void pop_decl_scope (void);
4246static dw_die_ref scope_die_for (tree, dw_die_ref);
4247static inline int local_scope_p (dw_die_ref);
66c78aa9 4248static inline int class_or_namespace_scope_p (dw_die_ref);
7080f735 4249static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
a1c496cb 4250static void add_calling_convention_attribute (dw_die_ref, tree);
7080f735
AJ
4251static const char *type_tag (tree);
4252static tree member_declared_type (tree);
7d9d8943 4253#if 0
7080f735 4254static const char *decl_start_label (tree);
7d9d8943 4255#endif
7080f735 4256static void gen_array_type_die (tree, dw_die_ref);
7d9d8943 4257#if 0
7080f735 4258static void gen_entry_point_die (tree, dw_die_ref);
7d9d8943 4259#endif
7080f735
AJ
4260static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4261static void gen_inlined_structure_type_die (tree, dw_die_ref);
4262static void gen_inlined_union_type_die (tree, dw_die_ref);
de99511b 4263static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
7080f735
AJ
4264static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4265static void gen_unspecified_parameters_die (tree, dw_die_ref);
4266static void gen_formal_types_die (tree, dw_die_ref);
4267static void gen_subprogram_die (tree, dw_die_ref);
4268static void gen_variable_die (tree, dw_die_ref);
4269static void gen_label_die (tree, dw_die_ref);
4270static void gen_lexical_block_die (tree, dw_die_ref, int);
4271static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4272static void gen_field_die (tree, dw_die_ref);
4273static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4274static dw_die_ref gen_compile_unit_die (const char *);
7080f735
AJ
4275static void gen_inheritance_die (tree, tree, dw_die_ref);
4276static void gen_member_die (tree, dw_die_ref);
39ef6592
LC
4277static void gen_struct_or_union_type_die (tree, dw_die_ref,
4278 enum debug_info_usage);
7080f735
AJ
4279static void gen_subroutine_type_die (tree, dw_die_ref);
4280static void gen_typedef_die (tree, dw_die_ref);
4281static void gen_type_die (tree, dw_die_ref);
4282static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4283static void gen_block_die (tree, dw_die_ref, int);
4284static void decls_for_scope (tree, dw_die_ref, int);
4285static int is_redundant_typedef (tree);
66c78aa9 4286static void gen_namespace_die (tree);
7080f735 4287static void gen_decl_die (tree, dw_die_ref);
6097b0c3
DP
4288static dw_die_ref force_decl_die (tree);
4289static dw_die_ref force_type_die (tree);
66c78aa9
JM
4290static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4291static void declare_in_namespace (tree, dw_die_ref);
d5688810 4292static struct dwarf_file_data * lookup_filename (const char *);
7080f735
AJ
4293static void retry_incomplete_types (void);
4294static void gen_type_die_for_member (tree, tree, dw_die_ref);
4295static void splice_child_die (dw_die_ref, dw_die_ref);
4296static int file_info_cmp (const void *, const void *);
4297static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4298 const char *, const char *, unsigned);
4299static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4300 const char *, const char *,
4301 const char *);
4302static void output_loc_list (dw_loc_list_ref);
4303static char *gen_internal_sym (const char *);
4304
4305static void prune_unmark_dies (dw_die_ref);
4306static void prune_unused_types_mark (dw_die_ref, int);
4307static void prune_unused_types_walk (dw_die_ref);
4308static void prune_unused_types_walk_attribs (dw_die_ref);
4309static void prune_unused_types_prune (dw_die_ref);
4310static void prune_unused_types (void);
d5688810 4311static int maybe_emit_file (struct dwarf_file_data *fd);
73c68f61 4312
7d9d8943
AM
4313/* Section names used to hold DWARF debugging information. */
4314#ifndef DEBUG_INFO_SECTION
4315#define DEBUG_INFO_SECTION ".debug_info"
4316#endif
9d2f2c45
RH
4317#ifndef DEBUG_ABBREV_SECTION
4318#define DEBUG_ABBREV_SECTION ".debug_abbrev"
7d9d8943 4319#endif
9d2f2c45
RH
4320#ifndef DEBUG_ARANGES_SECTION
4321#define DEBUG_ARANGES_SECTION ".debug_aranges"
7d9d8943 4322#endif
9d2f2c45
RH
4323#ifndef DEBUG_MACINFO_SECTION
4324#define DEBUG_MACINFO_SECTION ".debug_macinfo"
7d9d8943
AM
4325#endif
4326#ifndef DEBUG_LINE_SECTION
4327#define DEBUG_LINE_SECTION ".debug_line"
4328#endif
9d2f2c45
RH
4329#ifndef DEBUG_LOC_SECTION
4330#define DEBUG_LOC_SECTION ".debug_loc"
7d9d8943 4331#endif
9d2f2c45
RH
4332#ifndef DEBUG_PUBNAMES_SECTION
4333#define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
7d9d8943 4334#endif
9d2f2c45
RH
4335#ifndef DEBUG_STR_SECTION
4336#define DEBUG_STR_SECTION ".debug_str"
7d9d8943 4337#endif
a20612aa
RH
4338#ifndef DEBUG_RANGES_SECTION
4339#define DEBUG_RANGES_SECTION ".debug_ranges"
4340#endif
7d9d8943
AM
4341
4342/* Standard ELF section names for compiled code and data. */
f99ffb60
RH
4343#ifndef TEXT_SECTION_NAME
4344#define TEXT_SECTION_NAME ".text"
7d9d8943
AM
4345#endif
4346
9eb4015a 4347/* Section flags for .debug_str section. */
9eb4015a 4348#define DEBUG_STR_SECTION_FLAGS \
5d4856a0 4349 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
b0c242c0
AM
4350 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4351 : SECTION_DEBUG)
9eb4015a 4352
7d9d8943 4353/* Labels we insert at beginning sections we can reference instead of
556273e0 4354 the section names themselves. */
7d9d8943
AM
4355
4356#ifndef TEXT_SECTION_LABEL
9d2f2c45 4357#define TEXT_SECTION_LABEL "Ltext"
7d9d8943 4358#endif
c7466dee
CT
4359#ifndef COLD_TEXT_SECTION_LABEL
4360#define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4361#endif
7d9d8943 4362#ifndef DEBUG_LINE_SECTION_LABEL
9d2f2c45 4363#define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
7d9d8943
AM
4364#endif
4365#ifndef DEBUG_INFO_SECTION_LABEL
9d2f2c45 4366#define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
7d9d8943 4367#endif
9d2f2c45
RH
4368#ifndef DEBUG_ABBREV_SECTION_LABEL
4369#define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
7d9d8943 4370#endif
9d2f2c45
RH
4371#ifndef DEBUG_LOC_SECTION_LABEL
4372#define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
63e46568 4373#endif
2bee6045
JJ
4374#ifndef DEBUG_RANGES_SECTION_LABEL
4375#define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4376#endif
84a5b4f8
DB
4377#ifndef DEBUG_MACINFO_SECTION_LABEL
4378#define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4379#endif
a20612aa 4380
7d9d8943
AM
4381/* Definitions of defaults for formats and names of various special
4382 (artificial) labels which may be generated within this file (when the -g
def66b10 4383 options is used and DWARF2_DEBUGGING_INFO is in effect.
7d9d8943
AM
4384 If necessary, these may be overridden from within the tm.h file, but
4385 typically, overriding these defaults is unnecessary. */
4386
4387static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4388static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
c7466dee 4389static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
2878ea73 4390static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
7d9d8943
AM
4391static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4392static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4393static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
84a5b4f8 4394static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
63e46568 4395static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
2bee6045 4396static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
2ad9852d 4397
7d9d8943
AM
4398#ifndef TEXT_END_LABEL
4399#define TEXT_END_LABEL "Letext"
4400#endif
c7466dee
CT
4401#ifndef COLD_END_LABEL
4402#define COLD_END_LABEL "Letext_cold"
4403#endif
7d9d8943
AM
4404#ifndef BLOCK_BEGIN_LABEL
4405#define BLOCK_BEGIN_LABEL "LBB"
4406#endif
4407#ifndef BLOCK_END_LABEL
4408#define BLOCK_END_LABEL "LBE"
4409#endif
7d9d8943
AM
4410#ifndef LINE_CODE_LABEL
4411#define LINE_CODE_LABEL "LM"
4412#endif
4413#ifndef SEPARATE_LINE_CODE_LABEL
4414#define SEPARATE_LINE_CODE_LABEL "LSM"
4415#endif
4416\f
4417/* We allow a language front-end to designate a function that is to be
3f117656 4418 called to "demangle" any name before it is put into a DIE. */
7d9d8943 4419
7080f735 4420static const char *(*demangle_name_func) (const char *);
7d9d8943
AM
4421
4422void
7080f735 4423dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
7d9d8943
AM
4424{
4425 demangle_name_func = func;
4426}
7d9d8943
AM
4427
4428/* Test if rtl node points to a pseudo register. */
4429
4430static inline int
7080f735 4431is_pseudo_reg (rtx rtl)
7d9d8943 4432{
f8cfc6aa 4433 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
7d9d8943 4434 || (GET_CODE (rtl) == SUBREG
ddef6bc7 4435 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
7d9d8943
AM
4436}
4437
4438/* Return a reference to a type, with its const and volatile qualifiers
4439 removed. */
4440
4441static inline tree
7080f735 4442type_main_variant (tree type)
7d9d8943
AM
4443{
4444 type = TYPE_MAIN_VARIANT (type);
4445
2ad9852d
RK
4446 /* ??? There really should be only one main variant among any group of
4447 variants of a given type (and all of the MAIN_VARIANT values for all
4448 members of the group should point to that one type) but sometimes the C
4449 front-end messes this up for array types, so we work around that bug
4450 here. */
7d9d8943
AM
4451 if (TREE_CODE (type) == ARRAY_TYPE)
4452 while (type != TYPE_MAIN_VARIANT (type))
4453 type = TYPE_MAIN_VARIANT (type);
4454
4455 return type;
4456}
4457
cc2902df 4458/* Return nonzero if the given type node represents a tagged type. */
7d9d8943
AM
4459
4460static inline int
7080f735 4461is_tagged_type (tree type)
7d9d8943 4462{
b3694847 4463 enum tree_code code = TREE_CODE (type);
7d9d8943
AM
4464
4465 return (code == RECORD_TYPE || code == UNION_TYPE
4466 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4467}
4468
4469/* Convert a DIE tag into its string name. */
4470
4471static const char *
7080f735 4472dwarf_tag_name (unsigned int tag)
7d9d8943
AM
4473{
4474 switch (tag)
4475 {
4476 case DW_TAG_padding:
4477 return "DW_TAG_padding";
4478 case DW_TAG_array_type:
4479 return "DW_TAG_array_type";
4480 case DW_TAG_class_type:
4481 return "DW_TAG_class_type";
4482 case DW_TAG_entry_point:
4483 return "DW_TAG_entry_point";
4484 case DW_TAG_enumeration_type:
4485 return "DW_TAG_enumeration_type";
4486 case DW_TAG_formal_parameter:
4487 return "DW_TAG_formal_parameter";
4488 case DW_TAG_imported_declaration:
4489 return "DW_TAG_imported_declaration";
4490 case DW_TAG_label:
4491 return "DW_TAG_label";
4492 case DW_TAG_lexical_block:
4493 return "DW_TAG_lexical_block";
4494 case DW_TAG_member:
4495 return "DW_TAG_member";
4496 case DW_TAG_pointer_type:
4497 return "DW_TAG_pointer_type";
4498 case DW_TAG_reference_type:
4499 return "DW_TAG_reference_type";
4500 case DW_TAG_compile_unit:
4501 return "DW_TAG_compile_unit";
4502 case DW_TAG_string_type:
4503 return "DW_TAG_string_type";
4504 case DW_TAG_structure_type:
4505 return "DW_TAG_structure_type";
4506 case DW_TAG_subroutine_type:
4507 return "DW_TAG_subroutine_type";
4508 case DW_TAG_typedef:
4509 return "DW_TAG_typedef";
4510 case DW_TAG_union_type:
4511 return "DW_TAG_union_type";
4512 case DW_TAG_unspecified_parameters:
4513 return "DW_TAG_unspecified_parameters";
4514 case DW_TAG_variant:
4515 return "DW_TAG_variant";
4516 case DW_TAG_common_block:
4517 return "DW_TAG_common_block";
4518 case DW_TAG_common_inclusion:
4519 return "DW_TAG_common_inclusion";
4520 case DW_TAG_inheritance:
4521 return "DW_TAG_inheritance";
4522 case DW_TAG_inlined_subroutine:
4523 return "DW_TAG_inlined_subroutine";
4524 case DW_TAG_module:
4525 return "DW_TAG_module";
4526 case DW_TAG_ptr_to_member_type:
4527 return "DW_TAG_ptr_to_member_type";
4528 case DW_TAG_set_type:
4529 return "DW_TAG_set_type";
4530 case DW_TAG_subrange_type:
4531 return "DW_TAG_subrange_type";
4532 case DW_TAG_with_stmt:
4533 return "DW_TAG_with_stmt";
4534 case DW_TAG_access_declaration:
4535 return "DW_TAG_access_declaration";
4536 case DW_TAG_base_type:
4537 return "DW_TAG_base_type";
4538 case DW_TAG_catch_block:
4539 return "DW_TAG_catch_block";
4540 case DW_TAG_const_type:
4541 return "DW_TAG_const_type";
4542 case DW_TAG_constant:
4543 return "DW_TAG_constant";
4544 case DW_TAG_enumerator:
4545 return "DW_TAG_enumerator";
4546 case DW_TAG_file_type:
4547 return "DW_TAG_file_type";
4548 case DW_TAG_friend:
4549 return "DW_TAG_friend";
4550 case DW_TAG_namelist:
4551 return "DW_TAG_namelist";
4552 case DW_TAG_namelist_item:
4553 return "DW_TAG_namelist_item";
66c78aa9
JM
4554 case DW_TAG_namespace:
4555 return "DW_TAG_namespace";
7d9d8943
AM
4556 case DW_TAG_packed_type:
4557 return "DW_TAG_packed_type";
4558 case DW_TAG_subprogram:
4559 return "DW_TAG_subprogram";
4560 case DW_TAG_template_type_param:
4561 return "DW_TAG_template_type_param";
4562 case DW_TAG_template_value_param:
4563 return "DW_TAG_template_value_param";
4564 case DW_TAG_thrown_type:
4565 return "DW_TAG_thrown_type";
4566 case DW_TAG_try_block:
4567 return "DW_TAG_try_block";
4568 case DW_TAG_variant_part:
4569 return "DW_TAG_variant_part";
4570 case DW_TAG_variable:
4571 return "DW_TAG_variable";
4572 case DW_TAG_volatile_type:
4573 return "DW_TAG_volatile_type";
6097b0c3
DP
4574 case DW_TAG_imported_module:
4575 return "DW_TAG_imported_module";
7d9d8943
AM
4576 case DW_TAG_MIPS_loop:
4577 return "DW_TAG_MIPS_loop";
4578 case DW_TAG_format_label:
4579 return "DW_TAG_format_label";
4580 case DW_TAG_function_template:
4581 return "DW_TAG_function_template";
4582 case DW_TAG_class_template:
4583 return "DW_TAG_class_template";
881c6935
JM
4584 case DW_TAG_GNU_BINCL:
4585 return "DW_TAG_GNU_BINCL";
4586 case DW_TAG_GNU_EINCL:
4587 return "DW_TAG_GNU_EINCL";
7d9d8943
AM
4588 default:
4589 return "DW_TAG_<unknown>";
4590 }
4591}
4592
4593/* Convert a DWARF attribute code into its string name. */
4594
4595static const char *
7080f735 4596dwarf_attr_name (unsigned int attr)
7d9d8943
AM
4597{
4598 switch (attr)
4599 {
4600 case DW_AT_sibling:
4601 return "DW_AT_sibling";
4602 case DW_AT_location:
4603 return "DW_AT_location";
4604 case DW_AT_name:
4605 return "DW_AT_name";
4606 case DW_AT_ordering:
4607 return "DW_AT_ordering";
4608 case DW_AT_subscr_data:
4609 return "DW_AT_subscr_data";
4610 case DW_AT_byte_size:
4611 return "DW_AT_byte_size";
4612 case DW_AT_bit_offset:
4613 return "DW_AT_bit_offset";
4614 case DW_AT_bit_size:
4615 return "DW_AT_bit_size";
4616 case DW_AT_element_list:
4617 return "DW_AT_element_list";
4618 case DW_AT_stmt_list:
4619 return "DW_AT_stmt_list";
4620 case DW_AT_low_pc:
4621 return "DW_AT_low_pc";
4622 case DW_AT_high_pc:
4623 return "DW_AT_high_pc";
4624 case DW_AT_language:
4625 return "DW_AT_language";
4626 case DW_AT_member:
4627 return "DW_AT_member";
4628 case DW_AT_discr:
4629 return "DW_AT_discr";
4630 case DW_AT_discr_value:
4631 return "DW_AT_discr_value";
4632 case DW_AT_visibility:
4633 return "DW_AT_visibility";
4634 case DW_AT_import:
4635 return "DW_AT_import";
4636 case DW_AT_string_length:
4637 return "DW_AT_string_length";
4638 case DW_AT_common_reference:
4639 return "DW_AT_common_reference";
4640 case DW_AT_comp_dir:
4641 return "DW_AT_comp_dir";
4642 case DW_AT_const_value:
4643 return "DW_AT_const_value";
4644 case DW_AT_containing_type:
4645 return "DW_AT_containing_type";
4646 case DW_AT_default_value:
4647 return "DW_AT_default_value";
4648 case DW_AT_inline:
4649 return "DW_AT_inline";
4650 case DW_AT_is_optional:
4651 return "DW_AT_is_optional";
4652 case DW_AT_lower_bound:
4653 return "DW_AT_lower_bound";
4654 case DW_AT_producer:
4655 return "DW_AT_producer";
4656 case DW_AT_prototyped:
4657 return "DW_AT_prototyped";
4658 case DW_AT_return_addr:
4659 return "DW_AT_return_addr";
4660 case DW_AT_start_scope:
4661 return "DW_AT_start_scope";
4662 case DW_AT_stride_size:
4663 return "DW_AT_stride_size";
4664 case DW_AT_upper_bound:
4665 return "DW_AT_upper_bound";
4666 case DW_AT_abstract_origin:
4667 return "DW_AT_abstract_origin";
4668 case DW_AT_accessibility:
4669 return "DW_AT_accessibility";
4670 case DW_AT_address_class:
4671 return "DW_AT_address_class";
4672 case DW_AT_artificial:
4673 return "DW_AT_artificial";
4674 case DW_AT_base_types:
4675 return "DW_AT_base_types";
4676 case DW_AT_calling_convention:
4677 return "DW_AT_calling_convention";
4678 case DW_AT_count:
4679 return "DW_AT_count";
4680 case DW_AT_data_member_location:
4681 return "DW_AT_data_member_location";
4682 case DW_AT_decl_column:
4683 return "DW_AT_decl_column";
4684 case DW_AT_decl_file:
4685 return "DW_AT_decl_file";
4686 case DW_AT_decl_line:
4687 return "DW_AT_decl_line";
4688 case DW_AT_declaration:
4689 return "DW_AT_declaration";
4690 case DW_AT_discr_list:
4691 return "DW_AT_discr_list";
4692 case DW_AT_encoding:
4693 return "DW_AT_encoding";
4694 case DW_AT_external:
4695 return "DW_AT_external";
4696 case DW_AT_frame_base:
4697 return "DW_AT_frame_base";
4698 case DW_AT_friend:
4699 return "DW_AT_friend";
4700 case DW_AT_identifier_case:
4701 return "DW_AT_identifier_case";
4702 case DW_AT_macro_info:
4703 return "DW_AT_macro_info";
4704 case DW_AT_namelist_items:
4705 return "DW_AT_namelist_items";
4706 case DW_AT_priority:
4707 return "DW_AT_priority";
4708 case DW_AT_segment:
4709 return "DW_AT_segment";
4710 case DW_AT_specification:
4711 return "DW_AT_specification";
4712 case DW_AT_static_link:
4713 return "DW_AT_static_link";
4714 case DW_AT_type:
4715 return "DW_AT_type";
4716 case DW_AT_use_location:
4717 return "DW_AT_use_location";
4718 case DW_AT_variable_parameter:
4719 return "DW_AT_variable_parameter";
4720 case DW_AT_virtuality:
4721 return "DW_AT_virtuality";
4722 case DW_AT_vtable_elem_location:
4723 return "DW_AT_vtable_elem_location";
4724
a20612aa
RH
4725 case DW_AT_allocated:
4726 return "DW_AT_allocated";
4727 case DW_AT_associated:
4728 return "DW_AT_associated";
4729 case DW_AT_data_location:
4730 return "DW_AT_data_location";
4731 case DW_AT_stride:
4732 return "DW_AT_stride";
4733 case DW_AT_entry_pc:
4734 return "DW_AT_entry_pc";
4735 case DW_AT_use_UTF8:
4736 return "DW_AT_use_UTF8";
4737 case DW_AT_extension:
4738 return "DW_AT_extension";
4739 case DW_AT_ranges:
4740 return "DW_AT_ranges";
4741 case DW_AT_trampoline:
4742 return "DW_AT_trampoline";
4743 case DW_AT_call_column:
4744 return "DW_AT_call_column";
4745 case DW_AT_call_file:
4746 return "DW_AT_call_file";
4747 case DW_AT_call_line:
4748 return "DW_AT_call_line";
4749
7d9d8943
AM
4750 case DW_AT_MIPS_fde:
4751 return "DW_AT_MIPS_fde";
4752 case DW_AT_MIPS_loop_begin:
4753 return "DW_AT_MIPS_loop_begin";
4754 case DW_AT_MIPS_tail_loop_begin:
4755 return "DW_AT_MIPS_tail_loop_begin";
4756 case DW_AT_MIPS_epilog_begin:
4757 return "DW_AT_MIPS_epilog_begin";
4758 case DW_AT_MIPS_loop_unroll_factor:
4759 return "DW_AT_MIPS_loop_unroll_factor";
4760 case DW_AT_MIPS_software_pipeline_depth:
4761 return "DW_AT_MIPS_software_pipeline_depth";
4762 case DW_AT_MIPS_linkage_name:
4763 return "DW_AT_MIPS_linkage_name";
4764 case DW_AT_MIPS_stride:
4765 return "DW_AT_MIPS_stride";
4766 case DW_AT_MIPS_abstract_name:
4767 return "DW_AT_MIPS_abstract_name";
4768 case DW_AT_MIPS_clone_origin:
4769 return "DW_AT_MIPS_clone_origin";
4770 case DW_AT_MIPS_has_inlines:
4771 return "DW_AT_MIPS_has_inlines";
4772
4773 case DW_AT_sf_names:
4774 return "DW_AT_sf_names";
4775 case DW_AT_src_info:
4776 return "DW_AT_src_info";
4777 case DW_AT_mac_info:
4778 return "DW_AT_mac_info";
4779 case DW_AT_src_coords:
4780 return "DW_AT_src_coords";
4781 case DW_AT_body_begin:
4782 return "DW_AT_body_begin";
4783 case DW_AT_body_end:
4784 return "DW_AT_body_end";
84f0ace0
JM
4785 case DW_AT_GNU_vector:
4786 return "DW_AT_GNU_vector";
4787
7a0c8d71
DR
4788 case DW_AT_VMS_rtnbeg_pd_address:
4789 return "DW_AT_VMS_rtnbeg_pd_address";
4790
7d9d8943
AM
4791 default:
4792 return "DW_AT_<unknown>";
4793 }
4794}
4795
4796/* Convert a DWARF value form code into its string name. */
4797
4798static const char *
7080f735 4799dwarf_form_name (unsigned int form)
7d9d8943
AM
4800{
4801 switch (form)
4802 {
4803 case DW_FORM_addr:
4804 return "DW_FORM_addr";
4805 case DW_FORM_block2:
4806 return "DW_FORM_block2";
4807 case DW_FORM_block4:
4808 return "DW_FORM_block4";
4809 case DW_FORM_data2:
4810 return "DW_FORM_data2";
4811 case DW_FORM_data4:
4812 return "DW_FORM_data4";
4813 case DW_FORM_data8:
4814 return "DW_FORM_data8";
4815 case DW_FORM_string:
4816 return "DW_FORM_string";
4817 case DW_FORM_block:
4818 return "DW_FORM_block";
4819 case DW_FORM_block1:
4820 return "DW_FORM_block1";
4821 case DW_FORM_data1:
4822 return "DW_FORM_data1";
4823 case DW_FORM_flag:
4824 return "DW_FORM_flag";
4825 case DW_FORM_sdata:
4826 return "DW_FORM_sdata";
4827 case DW_FORM_strp:
4828 return "DW_FORM_strp";
4829 case DW_FORM_udata:
4830 return "DW_FORM_udata";
4831 case DW_FORM_ref_addr:
4832 return "DW_FORM_ref_addr";
4833 case DW_FORM_ref1:
4834 return "DW_FORM_ref1";
4835 case DW_FORM_ref2:
4836 return "DW_FORM_ref2";
4837 case DW_FORM_ref4:
4838 return "DW_FORM_ref4";
4839 case DW_FORM_ref8:
4840 return "DW_FORM_ref8";
4841 case DW_FORM_ref_udata:
4842 return "DW_FORM_ref_udata";
4843 case DW_FORM_indirect:
4844 return "DW_FORM_indirect";
3f76745e 4845 default:
7d9d8943 4846 return "DW_FORM_<unknown>";
a3f97cbb
JW
4847 }
4848}
3f76745e
JM
4849\f
4850/* Determine the "ultimate origin" of a decl. The decl may be an inlined
4851 instance of an inlined instance of a decl which is local to an inline
4852 function, so we have to trace all of the way back through the origin chain
4853 to find out what sort of node actually served as the original seed for the
4854 given block. */
a3f97cbb 4855
3f76745e 4856static tree
7080f735 4857decl_ultimate_origin (tree decl)
a3f97cbb 4858{
820cc88f
DB
4859 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4860 return NULL_TREE;
4861
10a11b75
JM
4862 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4863 nodes in the function to point to themselves; ignore that if
4864 we're trying to output the abstract instance of this function. */
4865 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4866 return NULL_TREE;
4867
ced3f397
NS
4868 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4869 most distant ancestor, this should never happen. */
4870 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3f76745e 4871
02e24c7a 4872 return DECL_ABSTRACT_ORIGIN (decl);
a3f97cbb
JW
4873}
4874
3f76745e
JM
4875/* Determine the "ultimate origin" of a block. The block may be an inlined
4876 instance of an inlined instance of a block which is local to an inline
4877 function, so we have to trace all of the way back through the origin chain
4878 to find out what sort of node actually served as the original seed for the
4879 given block. */
71dfc51f 4880
3f76745e 4881static tree
7080f735 4882block_ultimate_origin (tree block)
a3f97cbb 4883{
b3694847 4884 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
71dfc51f 4885
10a11b75
JM
4886 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4887 nodes in the function to point to themselves; ignore that if
4888 we're trying to output the abstract instance of this function. */
4889 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4890 return NULL_TREE;
4891
3f76745e
JM
4892 if (immediate_origin == NULL_TREE)
4893 return NULL_TREE;
4894 else
4895 {
b3694847
SS
4896 tree ret_val;
4897 tree lookahead = immediate_origin;
71dfc51f 4898
3f76745e
JM
4899 do
4900 {
4901 ret_val = lookahead;
2ad9852d
RK
4902 lookahead = (TREE_CODE (ret_val) == BLOCK
4903 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
3f76745e
JM
4904 }
4905 while (lookahead != NULL && lookahead != ret_val);
2878ea73 4906
1eb3331e
DB
4907 /* The block's abstract origin chain may not be the *ultimate* origin of
4908 the block. It could lead to a DECL that has an abstract origin set.
4909 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4910 will give us if it has one). Note that DECL's abstract origins are
4911 supposed to be the most distant ancestor (or so decl_ultimate_origin
4912 claims), so we don't need to loop following the DECL origins. */
4913 if (DECL_P (ret_val))
4914 return DECL_ORIGIN (ret_val);
3f76745e
JM
4915
4916 return ret_val;
4917 }
a3f97cbb
JW
4918}
4919
3f76745e
JM
4920/* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4921 of a virtual function may refer to a base class, so we check the 'this'
4922 parameter. */
71dfc51f 4923
3f76745e 4924static tree
7080f735 4925decl_class_context (tree decl)
a3f97cbb 4926{
3f76745e 4927 tree context = NULL_TREE;
71dfc51f 4928
3f76745e
JM
4929 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4930 context = DECL_CONTEXT (decl);
4931 else
4932 context = TYPE_MAIN_VARIANT
4933 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
71dfc51f 4934
2f939d94 4935 if (context && !TYPE_P (context))
3f76745e
JM
4936 context = NULL_TREE;
4937
4938 return context;
a3f97cbb
JW
4939}
4940\f
d6eeb3ba 4941/* Add an attribute/value pair to a DIE. */
71dfc51f
RK
4942
4943static inline void
7080f735 4944add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
a3f97cbb 4945{
1a27722f
GK
4946 /* Maybe this should be an assert? */
4947 if (die == NULL)
4948 return;
2878ea73 4949
1a27722f
GK
4950 if (die->die_attr == NULL)
4951 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4952 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
a3f97cbb
JW
4953}
4954
17211ab5 4955static inline enum dw_val_class
7080f735 4956AT_class (dw_attr_ref a)
a96c67ec
JM
4957{
4958 return a->dw_attr_val.val_class;
4959}
4960
3f76745e 4961/* Add a flag value attribute to a DIE. */
71dfc51f 4962
3f76745e 4963static inline void
7080f735 4964add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
a3f97cbb 4965{
1a27722f 4966 dw_attr_node attr;
71dfc51f 4967
1a27722f
GK
4968 attr.dw_attr = attr_kind;
4969 attr.dw_attr_val.val_class = dw_val_class_flag;
4970 attr.dw_attr_val.v.val_flag = flag;
4971 add_dwarf_attr (die, &attr);
a3f97cbb
JW
4972}
4973
a96c67ec 4974static inline unsigned
7080f735 4975AT_flag (dw_attr_ref a)
a96c67ec 4976{
ced3f397
NS
4977 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4978 return a->dw_attr_val.v.val_flag;
a96c67ec
JM
4979}
4980
3f76745e 4981/* Add a signed integer attribute value to a DIE. */
71dfc51f 4982
3f76745e 4983static inline void
799f628a 4984add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
a3f97cbb 4985{
1a27722f 4986 dw_attr_node attr;
3f76745e 4987
1a27722f
GK
4988 attr.dw_attr = attr_kind;
4989 attr.dw_attr_val.val_class = dw_val_class_const;
4990 attr.dw_attr_val.v.val_int = int_val;
4991 add_dwarf_attr (die, &attr);
a3f97cbb
JW
4992}
4993
799f628a 4994static inline HOST_WIDE_INT
7080f735 4995AT_int (dw_attr_ref a)
a96c67ec 4996{
ced3f397
NS
4997 gcc_assert (a && AT_class (a) == dw_val_class_const);
4998 return a->dw_attr_val.v.val_int;
a96c67ec
JM
4999}
5000
3f76745e 5001/* Add an unsigned integer attribute value to a DIE. */
71dfc51f 5002
3f76745e 5003static inline void
7080f735 5004add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
799f628a 5005 unsigned HOST_WIDE_INT unsigned_val)
a3f97cbb 5006{
1a27722f 5007 dw_attr_node attr;
3f76745e 5008
1a27722f
GK
5009 attr.dw_attr = attr_kind;
5010 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5011 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5012 add_dwarf_attr (die, &attr);
a3f97cbb 5013}
71dfc51f 5014
799f628a 5015static inline unsigned HOST_WIDE_INT
7080f735 5016AT_unsigned (dw_attr_ref a)
a96c67ec 5017{
ced3f397
NS
5018 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5019 return a->dw_attr_val.v.val_unsigned;
a96c67ec
JM
5020}
5021
3f76745e
JM
5022/* Add an unsigned double integer attribute value to a DIE. */
5023
5024static inline void
7080f735
AJ
5025add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5026 long unsigned int val_hi, long unsigned int val_low)
a3f97cbb 5027{
1a27722f 5028 dw_attr_node attr;
71dfc51f 5029
1a27722f
GK
5030 attr.dw_attr = attr_kind;
5031 attr.dw_attr_val.val_class = dw_val_class_long_long;
5032 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5033 attr.dw_attr_val.v.val_long_long.low = val_low;
5034 add_dwarf_attr (die, &attr);
3f76745e 5035}
71dfc51f 5036
3f76745e 5037/* Add a floating point attribute value to a DIE and return it. */
71dfc51f 5038
3f76745e 5039static inline void
e7ee3914
AM
5040add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5041 unsigned int length, unsigned int elt_size, unsigned char *array)
3f76745e 5042{
1a27722f 5043 dw_attr_node attr;
3f76745e 5044
1a27722f
GK
5045 attr.dw_attr = attr_kind;
5046 attr.dw_attr_val.val_class = dw_val_class_vec;
5047 attr.dw_attr_val.v.val_vec.length = length;
5048 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5049 attr.dw_attr_val.v.val_vec.array = array;
5050 add_dwarf_attr (die, &attr);
a3f97cbb
JW
5051}
5052
17211ab5
GK
5053/* Hash and equality functions for debug_str_hash. */
5054
5055static hashval_t
7080f735 5056debug_str_do_hash (const void *x)
17211ab5
GK
5057{
5058 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5059}
5060
5061static int
7080f735 5062debug_str_eq (const void *x1, const void *x2)
17211ab5
GK
5063{
5064 return strcmp ((((const struct indirect_string_node *)x1)->str),
5065 (const char *)x2) == 0;
5066}
5067
3f76745e 5068/* Add a string attribute value to a DIE. */
71dfc51f 5069
3f76745e 5070static inline void
7080f735 5071add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
a3f97cbb 5072{
1a27722f 5073 dw_attr_node attr;
9eb4015a 5074 struct indirect_string_node *node;
fad205ff 5075 void **slot;
c26fbbca 5076
9eb4015a 5077 if (! debug_str_hash)
7080f735 5078 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
17211ab5
GK
5079 debug_str_eq, NULL);
5080
5081 slot = htab_find_slot_with_hash (debug_str_hash, str,
5082 htab_hash_string (str), INSERT);
5083 if (*slot == NULL)
a0ad3539
MM
5084 {
5085 node = (struct indirect_string_node *)
5086 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5087 node->str = ggc_strdup (str);
5088 *slot = node;
5089 }
5090 else
5091 node = (struct indirect_string_node *) *slot;
5092
9eb4015a 5093 node->refcount++;
71dfc51f 5094
1a27722f
GK
5095 attr.dw_attr = attr_kind;
5096 attr.dw_attr_val.val_class = dw_val_class_str;
5097 attr.dw_attr_val.v.val_str = node;
5098 add_dwarf_attr (die, &attr);
3f76745e 5099}
71dfc51f 5100
a96c67ec 5101static inline const char *
7080f735 5102AT_string (dw_attr_ref a)
a96c67ec 5103{
ced3f397
NS
5104 gcc_assert (a && AT_class (a) == dw_val_class_str);
5105 return a->dw_attr_val.v.val_str->str;
9eb4015a
JJ
5106}
5107
5108/* Find out whether a string should be output inline in DIE
5109 or out-of-line in .debug_str section. */
5110
9eb4015a 5111static int
7080f735 5112AT_string_form (dw_attr_ref a)
9eb4015a 5113{
ced3f397
NS
5114 struct indirect_string_node *node;
5115 unsigned int len;
5116 char label[32];
9eb4015a 5117
ced3f397 5118 gcc_assert (a && AT_class (a) == dw_val_class_str);
a1c496cb 5119
ced3f397
NS
5120 node = a->dw_attr_val.v.val_str;
5121 if (node->form)
5122 return node->form;
a1c496cb 5123
ced3f397 5124 len = strlen (node->str) + 1;
9eb4015a 5125
ced3f397
NS
5126 /* If the string is shorter or equal to the size of the reference, it is
5127 always better to put it inline. */
5128 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5129 return node->form = DW_FORM_string;
9eb4015a 5130
ced3f397
NS
5131 /* If we cannot expect the linker to merge strings in .debug_str
5132 section, only put it into .debug_str if it is worth even in this
5133 single module. */
d6b5193b 5134 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
ced3f397
NS
5135 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5136 return node->form = DW_FORM_string;
2ad9852d 5137
ced3f397
NS
5138 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5139 ++dw2_string_counter;
5140 node->label = xstrdup (label);
a96c67ec 5141
ced3f397 5142 return node->form = DW_FORM_strp;
a96c67ec
JM
5143}
5144
3f76745e 5145/* Add a DIE reference attribute value to a DIE. */
71dfc51f 5146
3f76745e 5147static inline void
7080f735 5148add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3f76745e 5149{
1a27722f 5150 dw_attr_node attr;
71dfc51f 5151
1a27722f
GK
5152 attr.dw_attr = attr_kind;
5153 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5154 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5155 attr.dw_attr_val.v.val_die_ref.external = 0;
5156 add_dwarf_attr (die, &attr);
3f76745e 5157}
b1ccbc24 5158
47fcfa7b 5159/* Add an AT_specification attribute to a DIE, and also make the back
6614fd40 5160 pointer from the specification to the definition. */
47fcfa7b
SS
5161
5162static inline void
5163add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5164{
5165 add_AT_die_ref (die, DW_AT_specification, targ_die);
ced3f397 5166 gcc_assert (!targ_die->die_definition);
47fcfa7b
SS
5167 targ_die->die_definition = die;
5168}
5169
a96c67ec 5170static inline dw_die_ref
7080f735 5171AT_ref (dw_attr_ref a)
a96c67ec 5172{
ced3f397
NS
5173 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5174 return a->dw_attr_val.v.val_die_ref.die;
a96c67ec
JM
5175}
5176
881c6935 5177static inline int
7080f735 5178AT_ref_external (dw_attr_ref a)
881c6935
JM
5179{
5180 if (a && AT_class (a) == dw_val_class_die_ref)
5181 return a->dw_attr_val.v.val_die_ref.external;
5182
5183 return 0;
5184}
5185
881c6935 5186static inline void
7080f735 5187set_AT_ref_external (dw_attr_ref a, int i)
881c6935 5188{
ced3f397
NS
5189 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5190 a->dw_attr_val.v.val_die_ref.external = i;
881c6935
JM
5191}
5192
3f76745e 5193/* Add an FDE reference attribute value to a DIE. */
b1ccbc24 5194
3f76745e 5195static inline void
7080f735 5196add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
3f76745e 5197{
1a27722f 5198 dw_attr_node attr;
b1ccbc24 5199
1a27722f
GK
5200 attr.dw_attr = attr_kind;
5201 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5202 attr.dw_attr_val.v.val_fde_index = targ_fde;
5203 add_dwarf_attr (die, &attr);
a3f97cbb 5204}
71dfc51f 5205
3f76745e 5206/* Add a location description attribute value to a DIE. */
71dfc51f 5207
3f76745e 5208static inline void
7080f735 5209add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
3f76745e 5210{
1a27722f 5211 dw_attr_node attr;
71dfc51f 5212
1a27722f
GK
5213 attr.dw_attr = attr_kind;
5214 attr.dw_attr_val.val_class = dw_val_class_loc;
5215 attr.dw_attr_val.v.val_loc = loc;
5216 add_dwarf_attr (die, &attr);
a3f97cbb
JW
5217}
5218
a96c67ec 5219static inline dw_loc_descr_ref
7080f735 5220AT_loc (dw_attr_ref a)
a96c67ec 5221{
ced3f397
NS
5222 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5223 return a->dw_attr_val.v.val_loc;
a96c67ec
JM
5224}
5225
63e46568 5226static inline void
7080f735 5227add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
63e46568 5228{
1a27722f 5229 dw_attr_node attr;
63e46568 5230
1a27722f
GK
5231 attr.dw_attr = attr_kind;
5232 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5233 attr.dw_attr_val.v.val_loc_list = loc_list;
5234 add_dwarf_attr (die, &attr);
1146e682 5235 have_location_lists = true;
63e46568
DB
5236}
5237
63e46568 5238static inline dw_loc_list_ref
7080f735 5239AT_loc_list (dw_attr_ref a)
63e46568 5240{
ced3f397
NS
5241 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5242 return a->dw_attr_val.v.val_loc_list;
63e46568
DB
5243}
5244
3f76745e 5245/* Add an address constant attribute value to a DIE. */
71dfc51f 5246
3f76745e 5247static inline void
7080f735 5248add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
a3f97cbb 5249{
1a27722f 5250 dw_attr_node attr;
71dfc51f 5251
1a27722f
GK
5252 attr.dw_attr = attr_kind;
5253 attr.dw_attr_val.val_class = dw_val_class_addr;
5254 attr.dw_attr_val.v.val_addr = addr;
5255 add_dwarf_attr (die, &attr);
a3f97cbb
JW
5256}
5257
d5688810
GK
5258/* Get the RTX from to an address DIE attribute. */
5259
1865dbb5 5260static inline rtx
7080f735 5261AT_addr (dw_attr_ref a)
a96c67ec 5262{
ced3f397
NS
5263 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5264 return a->dw_attr_val.v.val_addr;
a96c67ec
JM
5265}
5266
d5688810
GK
5267/* Add a file attribute value to a DIE. */
5268
5269static inline void
5270add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5271 struct dwarf_file_data *fd)
5272{
5273 dw_attr_node attr;
5274
5275 attr.dw_attr = attr_kind;
5276 attr.dw_attr_val.val_class = dw_val_class_file;
5277 attr.dw_attr_val.v.val_file = fd;
5278 add_dwarf_attr (die, &attr);
5279}
5280
5281/* Get the dwarf_file_data from a file DIE attribute. */
5282
5283static inline struct dwarf_file_data *
5284AT_file (dw_attr_ref a)
5285{
5286 gcc_assert (a && AT_class (a) == dw_val_class_file);
5287 return a->dw_attr_val.v.val_file;
5288}
5289
3f76745e 5290/* Add a label identifier attribute value to a DIE. */
71dfc51f 5291
3f76745e 5292static inline void
7080f735 5293add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
a3f97cbb 5294{
1a27722f 5295 dw_attr_node attr;
71dfc51f 5296
1a27722f
GK
5297 attr.dw_attr = attr_kind;
5298 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5299 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5300 add_dwarf_attr (die, &attr);
3f76745e 5301}
71dfc51f 5302
192d0f89
GK
5303/* Add a section offset attribute value to a DIE, an offset into the
5304 debug_line section. */
3f76745e
JM
5305
5306static inline void
192d0f89
GK
5307add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5308 const char *label)
3f76745e 5309{
1a27722f 5310 dw_attr_node attr;
71dfc51f 5311
1a27722f
GK
5312 attr.dw_attr = attr_kind;
5313 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5314 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5315 add_dwarf_attr (die, &attr);
192d0f89
GK
5316}
5317
5318/* Add a section offset attribute value to a DIE, an offset into the
5319 debug_macinfo section. */
5320
5321static inline void
5322add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5323 const char *label)
5324{
1a27722f 5325 dw_attr_node attr;
192d0f89 5326
1a27722f
GK
5327 attr.dw_attr = attr_kind;
5328 attr.dw_attr_val.val_class = dw_val_class_macptr;
5329 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5330 add_dwarf_attr (die, &attr);
a3f97cbb
JW
5331}
5332
a20612aa
RH
5333/* Add an offset attribute value to a DIE. */
5334
2bee6045 5335static inline void
799f628a
JH
5336add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5337 unsigned HOST_WIDE_INT offset)
a20612aa 5338{
1a27722f 5339 dw_attr_node attr;
a20612aa 5340
1a27722f
GK
5341 attr.dw_attr = attr_kind;
5342 attr.dw_attr_val.val_class = dw_val_class_offset;
5343 attr.dw_attr_val.v.val_offset = offset;
5344 add_dwarf_attr (die, &attr);
a20612aa
RH
5345}
5346
2bee6045
JJ
5347/* Add an range_list attribute value to a DIE. */
5348
5349static void
7080f735
AJ
5350add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5351 long unsigned int offset)
2bee6045 5352{
1a27722f 5353 dw_attr_node attr;
2bee6045 5354
1a27722f
GK
5355 attr.dw_attr = attr_kind;
5356 attr.dw_attr_val.val_class = dw_val_class_range_list;
5357 attr.dw_attr_val.v.val_offset = offset;
5358 add_dwarf_attr (die, &attr);
2bee6045
JJ
5359}
5360
a96c67ec 5361static inline const char *
7080f735 5362AT_lbl (dw_attr_ref a)
a3f97cbb 5363{
ced3f397 5364 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
192d0f89
GK
5365 || AT_class (a) == dw_val_class_lineptr
5366 || AT_class (a) == dw_val_class_macptr));
ced3f397 5367 return a->dw_attr_val.v.val_lbl_id;
a3f97cbb
JW
5368}
5369
3f76745e 5370/* Get the attribute of type attr_kind. */
71dfc51f 5371
965514bd 5372static dw_attr_ref
7080f735 5373get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
f37230f0 5374{
b3694847 5375 dw_attr_ref a;
1a27722f 5376 unsigned ix;
b3694847 5377 dw_die_ref spec = NULL;
556273e0 5378
1a27722f
GK
5379 if (! die)
5380 return NULL;
71dfc51f 5381
1a27722f
GK
5382 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5383 if (a->dw_attr == attr_kind)
5384 return a;
5385 else if (a->dw_attr == DW_AT_specification
5386 || a->dw_attr == DW_AT_abstract_origin)
5387 spec = AT_ref (a);
2878ea73 5388
1a27722f
GK
5389 if (spec)
5390 return get_AT (spec, attr_kind);
3f76745e
JM
5391
5392 return NULL;
f37230f0
JM
5393}
5394
2ad9852d
RK
5395/* Return the "low pc" attribute value, typically associated with a subprogram
5396 DIE. Return null if the "low pc" attribute is either not present, or if it
5397 cannot be represented as an assembler label identifier. */
71dfc51f 5398
a96c67ec 5399static inline const char *
7080f735 5400get_AT_low_pc (dw_die_ref die)
7e23cb16 5401{
b3694847 5402 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
2ad9852d 5403
40e8cc95 5404 return a ? AT_lbl (a) : NULL;
7e23cb16
JM
5405}
5406
2ad9852d
RK
5407/* Return the "high pc" attribute value, typically associated with a subprogram
5408 DIE. Return null if the "high pc" attribute is either not present, or if it
5409 cannot be represented as an assembler label identifier. */
71dfc51f 5410
a96c67ec 5411static inline const char *
7080f735 5412get_AT_hi_pc (dw_die_ref die)
a3f97cbb 5413{
b3694847 5414 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
2ad9852d 5415
40e8cc95 5416 return a ? AT_lbl (a) : NULL;
3f76745e
JM
5417}
5418
5419/* Return the value of the string attribute designated by ATTR_KIND, or
5420 NULL if it is not present. */
71dfc51f 5421
a96c67ec 5422static inline const char *
7080f735 5423get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
3f76745e 5424{
b3694847 5425 dw_attr_ref a = get_AT (die, attr_kind);
2ad9852d 5426
40e8cc95 5427 return a ? AT_string (a) : NULL;
a3f97cbb
JW
5428}
5429
3f76745e
JM
5430/* Return the value of the flag attribute designated by ATTR_KIND, or -1
5431 if it is not present. */
71dfc51f 5432
3f76745e 5433static inline int
7080f735 5434get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
a3f97cbb 5435{
b3694847 5436 dw_attr_ref a = get_AT (die, attr_kind);
2ad9852d 5437
40e8cc95 5438 return a ? AT_flag (a) : 0;
a3f97cbb
JW
5439}
5440
3f76745e
JM
5441/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5442 if it is not present. */
71dfc51f 5443
3f76745e 5444static inline unsigned
7080f735 5445get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
a3f97cbb 5446{
b3694847 5447 dw_attr_ref a = get_AT (die, attr_kind);
2ad9852d 5448
40e8cc95 5449 return a ? AT_unsigned (a) : 0;
a96c67ec 5450}
71dfc51f 5451
a96c67ec 5452static inline dw_die_ref
7080f735 5453get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
a96c67ec 5454{
b3694847 5455 dw_attr_ref a = get_AT (die, attr_kind);
2ad9852d 5456
40e8cc95 5457 return a ? AT_ref (a) : NULL;
3f76745e 5458}
71dfc51f 5459
d5688810
GK
5460static inline struct dwarf_file_data *
5461get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5462{
5463 dw_attr_ref a = get_AT (die, attr_kind);
5464
5465 return a ? AT_file (a) : NULL;
5466}
5467
c3cdeef4
JB
5468/* Return TRUE if the language is C or C++. */
5469
5470static inline bool
7080f735 5471is_c_family (void)
3f76745e 5472{
c3cdeef4 5473 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
71dfc51f 5474
9e51ddaa
GK
5475 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5476 || lang == DW_LANG_C99
5477 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
556273e0 5478}
71dfc51f 5479
c3cdeef4
JB
5480/* Return TRUE if the language is C++. */
5481
5482static inline bool
7080f735 5483is_cxx (void)
1d3d6b1e 5484{
9e51ddaa 5485 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
2878ea73 5486
9e51ddaa 5487 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
c26fbbca 5488}
1d3d6b1e 5489
c3cdeef4
JB
5490/* Return TRUE if the language is Fortran. */
5491
5492static inline bool
7080f735 5493is_fortran (void)
3f76745e 5494{
c3cdeef4 5495 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
71dfc51f 5496
6de9cd9a
DN
5497 return (lang == DW_LANG_Fortran77
5498 || lang == DW_LANG_Fortran90
5499 || lang == DW_LANG_Fortran95);
556273e0 5500}
71dfc51f 5501
c3cdeef4
JB
5502/* Return TRUE if the language is Java. */
5503
5504static inline bool
7080f735 5505is_java (void)
28985b81 5506{
c3cdeef4 5507 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
28985b81 5508
c3cdeef4
JB
5509 return lang == DW_LANG_Java;
5510}
5511
5512/* Return TRUE if the language is Ada. */
5513
5514static inline bool
7080f735 5515is_ada (void)
c3cdeef4
JB
5516{
5517 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7080f735 5518
c3cdeef4 5519 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
28985b81
AG
5520}
5521
10a11b75
JM
5522/* Remove the specified attribute if present. */
5523
5524static void
7080f735 5525remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
3f76745e 5526{
1a27722f
GK
5527 dw_attr_ref a;
5528 unsigned ix;
a3f97cbb 5529
1a27722f
GK
5530 if (! die)
5531 return;
71dfc51f 5532
1a27722f
GK
5533 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5534 if (a->dw_attr == attr_kind)
5535 {
e26303c2
GK
5536 if (AT_class (a) == dw_val_class_str)
5537 if (a->dw_attr_val.v.val_str->refcount)
5538 a->dw_attr_val.v.val_str->refcount--;
5539
1a27722f
GK
5540 /* VEC_ordered_remove should help reduce the number of abbrevs
5541 that are needed. */
5542 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5543 return;
5544 }
10a11b75 5545}
71dfc51f 5546
d6eeb3ba
GK
5547/* Remove CHILD from its parent. PREV must have the property that
5548 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
6097b0c3
DP
5549
5550static void
d6eeb3ba 5551remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6097b0c3 5552{
d6eeb3ba
GK
5553 gcc_assert (child->die_parent == prev->die_parent);
5554 gcc_assert (prev->die_sib == child);
5555 if (prev == child)
6097b0c3 5556 {
d6eeb3ba
GK
5557 gcc_assert (child->die_parent->die_child == child);
5558 prev = NULL;
6097b0c3 5559 }
d6eeb3ba
GK
5560 else
5561 prev->die_sib = child->die_sib;
5562 if (child->die_parent->die_child == child)
5563 child->die_parent->die_child = prev;
6097b0c3
DP
5564}
5565
d6eeb3ba
GK
5566/* Remove child DIE whose die_tag is TAG. Do nothing if no child
5567 matches TAG. */
71dfc51f 5568
d6eeb3ba
GK
5569static void
5570remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5571{
5572 dw_die_ref c;
2878ea73 5573
d6eeb3ba
GK
5574 c = die->die_child;
5575 if (c) do {
5576 dw_die_ref prev = c;
5577 c = c->die_sib;
5578 while (c->die_tag == tag)
5579 {
5580 remove_child_with_prev (c, prev);
5581 /* Might have removed every child. */
5582 if (c == c->die_sib)
5583 return;
5584 c = c->die_sib;
5585 }
5586 } while (c != die->die_child);
5587}
5588
5589/* Add a CHILD_DIE as the last child of DIE. */
5590
5591static void
7080f735 5592add_child_die (dw_die_ref die, dw_die_ref child_die)
3f76745e 5593{
d6eeb3ba
GK
5594 /* FIXME this should probably be an assert. */
5595 if (! die || ! child_die)
5596 return;
5597 gcc_assert (die != child_die);
2ad9852d 5598
d6eeb3ba
GK
5599 child_die->die_parent = die;
5600 if (die->die_child)
5601 {
5602 child_die->die_sib = die->die_child->die_sib;
5603 die->die_child->die_sib = child_die;
3f76745e 5604 }
d6eeb3ba
GK
5605 else
5606 child_die->die_sib = child_die;
5607 die->die_child = child_die;
3f76745e
JM
5608}
5609
2081603c 5610/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
2878ea73 5611 is the specification, to the end of PARENT's list of children.
d6eeb3ba 5612 This is done by removing and re-adding it. */
10a11b75
JM
5613
5614static void
7080f735 5615splice_child_die (dw_die_ref parent, dw_die_ref child)
10a11b75 5616{
d6eeb3ba 5617 dw_die_ref p;
10a11b75
JM
5618
5619 /* We want the declaration DIE from inside the class, not the
5620 specification DIE at toplevel. */
5621 if (child->die_parent != parent)
2081603c
JM
5622 {
5623 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
2ad9852d 5624
2081603c
JM
5625 if (tmp)
5626 child = tmp;
5627 }
10a11b75 5628
ced3f397
NS
5629 gcc_assert (child->die_parent == parent
5630 || (child->die_parent
5631 == get_AT_ref (parent, DW_AT_specification)));
2878ea73 5632
d6eeb3ba
GK
5633 for (p = child->die_parent->die_child; ; p = p->die_sib)
5634 if (p->die_sib == child)
10a11b75 5635 {
d6eeb3ba 5636 remove_child_with_prev (child, p);
10a11b75
JM
5637 break;
5638 }
5639
d6eeb3ba 5640 add_child_die (parent, child);
10a11b75
JM
5641}
5642
3f76745e
JM
5643/* Return a pointer to a newly created DIE node. */
5644
5645static inline dw_die_ref
7080f735 5646new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
3f76745e 5647{
703ad42b 5648 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
3f76745e
JM
5649
5650 die->die_tag = tag_value;
3f76745e
JM
5651
5652 if (parent_die != NULL)
5653 add_child_die (parent_die, die);
5654 else
ef76d03b
JW
5655 {
5656 limbo_die_node *limbo_node;
5657
17211ab5 5658 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
ef76d03b 5659 limbo_node->die = die;
54ba1f0d 5660 limbo_node->created_for = t;
ef76d03b
JW
5661 limbo_node->next = limbo_die_list;
5662 limbo_die_list = limbo_node;
5663 }
71dfc51f 5664
3f76745e
JM
5665 return die;
5666}
71dfc51f 5667
3f76745e 5668/* Return the DIE associated with the given type specifier. */
71dfc51f 5669
3f76745e 5670static inline dw_die_ref
7080f735 5671lookup_type_die (tree type)
3f76745e 5672{
e2500fed 5673 return TYPE_SYMTAB_DIE (type);
3f76745e 5674}
e90b62db 5675
3f76745e 5676/* Equate a DIE to a given type specifier. */
71dfc51f 5677
10a11b75 5678static inline void
7080f735 5679equate_type_number_to_die (tree type, dw_die_ref type_die)
3f76745e 5680{
e2500fed 5681 TYPE_SYMTAB_DIE (type) = type_die;
3f76745e 5682}
71dfc51f 5683
636c7bc4
JZ
5684/* Returns a hash value for X (which really is a die_struct). */
5685
5686static hashval_t
5687decl_die_table_hash (const void *x)
5688{
5f754896 5689 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
636c7bc4
JZ
5690}
5691
5692/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5693
5694static int
5695decl_die_table_eq (const void *x, const void *y)
5696{
5f754896 5697 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
636c7bc4
JZ
5698}
5699
3f76745e 5700/* Return the DIE associated with a given declaration. */
71dfc51f 5701
3f76745e 5702static inline dw_die_ref
7080f735 5703lookup_decl_die (tree decl)
3f76745e 5704{
636c7bc4 5705 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
a3f97cbb
JW
5706}
5707
0a2d3d69
DB
5708/* Returns a hash value for X (which really is a var_loc_list). */
5709
5710static hashval_t
5711decl_loc_table_hash (const void *x)
5712{
5713 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5714}
5715
5716/* Return nonzero if decl_id of var_loc_list X is the same as
5717 UID of decl *Y. */
5718
5719static int
5720decl_loc_table_eq (const void *x, const void *y)
5721{
5722 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5723}
5724
5725/* Return the var_loc list associated with a given declaration. */
5726
5727static inline var_loc_list *
5728lookup_decl_loc (tree decl)
5729{
5730 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5731}
5732
3f76745e 5733/* Equate a DIE to a particular declaration. */
71dfc51f 5734
3f76745e 5735static void
7080f735 5736equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
a3f97cbb 5737{
aea9695c 5738 unsigned int decl_id = DECL_UID (decl);
636c7bc4 5739 void **slot;
3f76745e 5740
636c7bc4
JZ
5741 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5742 *slot = decl_die;
5743 decl_die->decl_id = decl_id;
a3f97cbb 5744}
0a2d3d69
DB
5745
5746/* Add a variable location node to the linked list for DECL. */
5747
5748static void
5749add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5750{
5751 unsigned int decl_id = DECL_UID (decl);
5752 var_loc_list *temp;
5753 void **slot;
5754
5755 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5756 if (*slot == NULL)
5757 {
5758 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5759 temp->decl_id = decl_id;
5760 *slot = temp;
5761 }
5762 else
5763 temp = *slot;
5764
5765 if (temp->last)
5766 {
5767 /* If the current location is the same as the end of the list,
62760ffd 5768 and either both or neither of the locations is uninitialized,
0a2d3d69 5769 we have nothing to do. */
62760ffd
CT
5770 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5771 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5772 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5773 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5774 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5775 == VAR_INIT_STATUS_UNINITIALIZED)
5776 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5777 == VAR_INIT_STATUS_UNINITIALIZED))))
0a2d3d69
DB
5778 {
5779 /* Add LOC to the end of list and update LAST. */
5780 temp->last->next = loc;
5781 temp->last = loc;
5782 }
5783 }
5784 /* Do not add empty location to the beginning of the list. */
5785 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5786 {
5787 temp->first = loc;
5788 temp->last = loc;
5789 }
5790}
3f76745e
JM
5791\f
5792/* Keep track of the number of spaces used to indent the
5793 output of the debugging routines that print the structure of
5794 the DIE internal representation. */
5795static int print_indent;
71dfc51f 5796
3f76745e
JM
5797/* Indent the line the number of spaces given by print_indent. */
5798
5799static inline void
7080f735 5800print_spaces (FILE *outfile)
3f76745e
JM
5801{
5802 fprintf (outfile, "%*s", print_indent, "");
a3f97cbb
JW
5803}
5804
956d6950 5805/* Print the information associated with a given DIE, and its children.
3f76745e 5806 This routine is a debugging aid only. */
71dfc51f 5807
a3f97cbb 5808static void
7080f735 5809print_die (dw_die_ref die, FILE *outfile)
a3f97cbb 5810{
b3694847
SS
5811 dw_attr_ref a;
5812 dw_die_ref c;
1a27722f 5813 unsigned ix;
71dfc51f 5814
3f76745e 5815 print_spaces (outfile);
8d5b1b67 5816 fprintf (outfile, "DIE %4ld: %s\n",
3f76745e
JM
5817 die->die_offset, dwarf_tag_name (die->die_tag));
5818 print_spaces (outfile);
2d8b0f3a 5819 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
8d5b1b67 5820 fprintf (outfile, " offset: %ld\n", die->die_offset);
3f76745e 5821
1a27722f 5822 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
a3f97cbb 5823 {
3f76745e
JM
5824 print_spaces (outfile);
5825 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5826
a96c67ec 5827 switch (AT_class (a))
3f76745e
JM
5828 {
5829 case dw_val_class_addr:
5830 fprintf (outfile, "address");
5831 break;
a20612aa
RH
5832 case dw_val_class_offset:
5833 fprintf (outfile, "offset");
5834 break;
3f76745e
JM
5835 case dw_val_class_loc:
5836 fprintf (outfile, "location descriptor");
5837 break;
63e46568 5838 case dw_val_class_loc_list:
a20612aa
RH
5839 fprintf (outfile, "location list -> label:%s",
5840 AT_loc_list (a)->ll_symbol);
63e46568 5841 break;
2bee6045
JJ
5842 case dw_val_class_range_list:
5843 fprintf (outfile, "range list");
5844 break;
3f76745e 5845 case dw_val_class_const:
38f9cd4c 5846 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
3f76745e
JM
5847 break;
5848 case dw_val_class_unsigned_const:
38f9cd4c 5849 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
3f76745e
JM
5850 break;
5851 case dw_val_class_long_long:
2d8b0f3a 5852 fprintf (outfile, "constant (%lu,%lu)",
556273e0
KH
5853 a->dw_attr_val.v.val_long_long.hi,
5854 a->dw_attr_val.v.val_long_long.low);
3f76745e 5855 break;
e7ee3914
AM
5856 case dw_val_class_vec:
5857 fprintf (outfile, "floating-point or vector constant");
3f76745e
JM
5858 break;
5859 case dw_val_class_flag:
a96c67ec 5860 fprintf (outfile, "%u", AT_flag (a));
3f76745e
JM
5861 break;
5862 case dw_val_class_die_ref:
a96c67ec 5863 if (AT_ref (a) != NULL)
881c6935 5864 {
1bfb5f8f 5865 if (AT_ref (a)->die_symbol)
881c6935
JM
5866 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5867 else
8d5b1b67 5868 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
881c6935 5869 }
3f76745e
JM
5870 else
5871 fprintf (outfile, "die -> <null>");
5872 break;
5873 case dw_val_class_lbl_id:
192d0f89
GK
5874 case dw_val_class_lineptr:
5875 case dw_val_class_macptr:
a96c67ec 5876 fprintf (outfile, "label: %s", AT_lbl (a));
3f76745e 5877 break;
3f76745e 5878 case dw_val_class_str:
a96c67ec
JM
5879 if (AT_string (a) != NULL)
5880 fprintf (outfile, "\"%s\"", AT_string (a));
3f76745e
JM
5881 else
5882 fprintf (outfile, "<null>");
5883 break;
d5688810
GK
5884 case dw_val_class_file:
5885 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5886 AT_file (a)->emitted_number);
5887 break;
e9a25f70
JL
5888 default:
5889 break;
3f76745e
JM
5890 }
5891
5892 fprintf (outfile, "\n");
5893 }
5894
5895 if (die->die_child != NULL)
5896 {
5897 print_indent += 4;
d6eeb3ba 5898 FOR_EACH_CHILD (die, c, print_die (c, outfile));
3f76745e 5899 print_indent -= 4;
a3f97cbb 5900 }
881c6935
JM
5901 if (print_indent == 0)
5902 fprintf (outfile, "\n");
a3f97cbb
JW
5903}
5904
3f76745e
JM
5905/* Print the contents of the source code line number correspondence table.
5906 This routine is a debugging aid only. */
71dfc51f 5907
3f76745e 5908static void
7080f735 5909print_dwarf_line_table (FILE *outfile)
a3f97cbb 5910{
b3694847
SS
5911 unsigned i;
5912 dw_line_info_ref line_info;
3f76745e
JM
5913
5914 fprintf (outfile, "\n\nDWARF source line information\n");
2ad9852d 5915 for (i = 1; i < line_info_table_in_use; i++)
a3f97cbb 5916 {
3f76745e 5917 line_info = &line_info_table[i];
d5688810
GK
5918 fprintf (outfile, "%5d: %4ld %6ld\n", i,
5919 line_info->dw_file_num,
5920 line_info->dw_line_num);
a3f97cbb 5921 }
3f76745e
JM
5922
5923 fprintf (outfile, "\n\n");
f37230f0
JM
5924}
5925
3f76745e
JM
5926/* Print the information collected for a given DIE. */
5927
5928void
7080f735 5929debug_dwarf_die (dw_die_ref die)
3f76745e
JM
5930{
5931 print_die (die, stderr);
5932}
5933
5934/* Print all DWARF information collected for the compilation unit.
5935 This routine is a debugging aid only. */
5936
5937void
7080f735 5938debug_dwarf (void)
3f76745e
JM
5939{
5940 print_indent = 0;
5941 print_die (comp_unit_die, stderr);
b2244e22
JW
5942 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5943 print_dwarf_line_table (stderr);
3f76745e
JM
5944}
5945\f
2ad9852d
RK
5946/* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5947 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5948 DIE that marks the start of the DIEs for this include file. */
881c6935
JM
5949
5950static dw_die_ref
7080f735 5951push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
881c6935
JM
5952{
5953 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5954 dw_die_ref new_unit = gen_compile_unit_die (filename);
2ad9852d 5955
881c6935
JM
5956 new_unit->die_sib = old_unit;
5957 return new_unit;
5958}
5959
5960/* Close an include-file CU and reopen the enclosing one. */
5961
5962static dw_die_ref
7080f735 5963pop_compile_unit (dw_die_ref old_unit)
881c6935
JM
5964{
5965 dw_die_ref new_unit = old_unit->die_sib;
2ad9852d 5966
881c6935
JM
5967 old_unit->die_sib = NULL;
5968 return new_unit;
5969}
5970
2ad9852d
RK
5971#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5972#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
881c6935
JM
5973
5974/* Calculate the checksum of a location expression. */
5975
5976static inline void
7080f735 5977loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
881c6935 5978{
2ad9852d
RK
5979 CHECKSUM (loc->dw_loc_opc);
5980 CHECKSUM (loc->dw_loc_oprnd1);
5981 CHECKSUM (loc->dw_loc_oprnd2);
881c6935
JM
5982}
5983
5984/* Calculate the checksum of an attribute. */
5985
5986static void
7080f735 5987attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
881c6935
JM
5988{
5989 dw_loc_descr_ref loc;
5990 rtx r;
5991
2ad9852d 5992 CHECKSUM (at->dw_attr);
881c6935 5993
d5688810
GK
5994 /* We don't care that this was compiled with a different compiler
5995 snapshot; if the output is the same, that's what matters. */
5996 if (at->dw_attr == DW_AT_producer)
881c6935
JM
5997 return;
5998
5999 switch (AT_class (at))
6000 {
6001 case dw_val_class_const:
2ad9852d 6002 CHECKSUM (at->dw_attr_val.v.val_int);
881c6935
JM
6003 break;
6004 case dw_val_class_unsigned_const:
2ad9852d 6005 CHECKSUM (at->dw_attr_val.v.val_unsigned);
881c6935
JM
6006 break;
6007 case dw_val_class_long_long:
2ad9852d 6008 CHECKSUM (at->dw_attr_val.v.val_long_long);
881c6935 6009 break;
e7ee3914
AM
6010 case dw_val_class_vec:
6011 CHECKSUM (at->dw_attr_val.v.val_vec);
881c6935
JM
6012 break;
6013 case dw_val_class_flag:
2ad9852d 6014 CHECKSUM (at->dw_attr_val.v.val_flag);
881c6935 6015 break;
881c6935 6016 case dw_val_class_str:
2ad9852d 6017 CHECKSUM_STRING (AT_string (at));
881c6935 6018 break;
a20612aa 6019
881c6935
JM
6020 case dw_val_class_addr:
6021 r = AT_addr (at);
ced3f397
NS
6022 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6023 CHECKSUM_STRING (XSTR (r, 0));
881c6935
JM
6024 break;
6025
a20612aa 6026 case dw_val_class_offset:
2ad9852d 6027 CHECKSUM (at->dw_attr_val.v.val_offset);
a20612aa
RH
6028 break;
6029
881c6935
JM
6030 case dw_val_class_loc:
6031 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6032 loc_checksum (loc, ctx);
6033 break;
6034
6035 case dw_val_class_die_ref:
cc0017a9
ZD
6036 die_checksum (AT_ref (at), ctx, mark);
6037 break;
881c6935
JM
6038
6039 case dw_val_class_fde_ref:
6040 case dw_val_class_lbl_id:
192d0f89
GK
6041 case dw_val_class_lineptr:
6042 case dw_val_class_macptr:
a20612aa 6043 break;
881c6935 6044
d5688810
GK
6045 case dw_val_class_file:
6046 CHECKSUM_STRING (AT_file (at)->filename);
6047 break;
6048
881c6935
JM
6049 default:
6050 break;
6051 }
6052}
6053
6054/* Calculate the checksum of a DIE. */
6055
6056static void
7080f735 6057die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
881c6935
JM
6058{
6059 dw_die_ref c;
6060 dw_attr_ref a;
1a27722f 6061 unsigned ix;
881c6935 6062
cc0017a9
ZD
6063 /* To avoid infinite recursion. */
6064 if (die->die_mark)
6065 {
6066 CHECKSUM (die->die_mark);
6067 return;
6068 }
6069 die->die_mark = ++(*mark);
6070
2ad9852d 6071 CHECKSUM (die->die_tag);
881c6935 6072
1a27722f 6073 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
cc0017a9 6074 attr_checksum (a, ctx, mark);
881c6935 6075
d6eeb3ba 6076 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
881c6935
JM
6077}
6078
2ad9852d
RK
6079#undef CHECKSUM
6080#undef CHECKSUM_STRING
881c6935 6081
cc0017a9
ZD
6082/* Do the location expressions look same? */
6083static inline int
7080f735 6084same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
cc0017a9
ZD
6085{
6086 return loc1->dw_loc_opc == loc2->dw_loc_opc
6087 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6088 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6089}
6090
6091/* Do the values look the same? */
6092static int
7080f735 6093same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
cc0017a9
ZD
6094{
6095 dw_loc_descr_ref loc1, loc2;
6096 rtx r1, r2;
cc0017a9
ZD
6097
6098 if (v1->val_class != v2->val_class)
6099 return 0;
6100
6101 switch (v1->val_class)
6102 {
6103 case dw_val_class_const:
6104 return v1->v.val_int == v2->v.val_int;
6105 case dw_val_class_unsigned_const:
6106 return v1->v.val_unsigned == v2->v.val_unsigned;
6107 case dw_val_class_long_long:
6108 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
73c68f61 6109 && v1->v.val_long_long.low == v2->v.val_long_long.low;
e7ee3914
AM
6110 case dw_val_class_vec:
6111 if (v1->v.val_vec.length != v2->v.val_vec.length
6112 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6113 return 0;
6114 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6115 v1->v.val_vec.length * v1->v.val_vec.elt_size))
cc0017a9 6116 return 0;
cc0017a9
ZD
6117 return 1;
6118 case dw_val_class_flag:
6119 return v1->v.val_flag == v2->v.val_flag;
6120 case dw_val_class_str:
17211ab5 6121 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
cc0017a9
ZD
6122
6123 case dw_val_class_addr:
6124 r1 = v1->v.val_addr;
6125 r2 = v2->v.val_addr;
6126 if (GET_CODE (r1) != GET_CODE (r2))
6127 return 0;
ced3f397
NS
6128 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6129 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
cc0017a9
ZD
6130
6131 case dw_val_class_offset:
6132 return v1->v.val_offset == v2->v.val_offset;
6133
6134 case dw_val_class_loc:
6135 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6136 loc1 && loc2;
6137 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6138 if (!same_loc_p (loc1, loc2, mark))
6139 return 0;
6140 return !loc1 && !loc2;
6141
6142 case dw_val_class_die_ref:
6143 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6144
6145 case dw_val_class_fde_ref:
6146 case dw_val_class_lbl_id:
192d0f89
GK
6147 case dw_val_class_lineptr:
6148 case dw_val_class_macptr:
cc0017a9
ZD
6149 return 1;
6150
d5688810
GK
6151 case dw_val_class_file:
6152 return v1->v.val_file == v2->v.val_file;
6153
cc0017a9
ZD
6154 default:
6155 return 1;
6156 }
6157}
6158
6159/* Do the attributes look the same? */
6160
6161static int
7080f735 6162same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
cc0017a9
ZD
6163{
6164 if (at1->dw_attr != at2->dw_attr)
6165 return 0;
6166
d5688810
GK
6167 /* We don't care that this was compiled with a different compiler
6168 snapshot; if the output is the same, that's what matters. */
6169 if (at1->dw_attr == DW_AT_producer)
cc0017a9
ZD
6170 return 1;
6171
6172 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6173}
6174
6175/* Do the dies look the same? */
6176
6177static int
7080f735 6178same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
cc0017a9
ZD
6179{
6180 dw_die_ref c1, c2;
1a27722f
GK
6181 dw_attr_ref a1;
6182 unsigned ix;
cc0017a9
ZD
6183
6184 /* To avoid infinite recursion. */
6185 if (die1->die_mark)
6186 return die1->die_mark == die2->die_mark;
6187 die1->die_mark = die2->die_mark = ++(*mark);
6188
6189 if (die1->die_tag != die2->die_tag)
6190 return 0;
6191
1a27722f
GK
6192 if (VEC_length (dw_attr_node, die1->die_attr)
6193 != VEC_length (dw_attr_node, die2->die_attr))
cc0017a9 6194 return 0;
2878ea73 6195
1a27722f
GK
6196 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6197 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6198 return 0;
cc0017a9 6199
d6eeb3ba
GK
6200 c1 = die1->die_child;
6201 c2 = die2->die_child;
6202 if (! c1)
6203 {
6204 if (c2)
6205 return 0;
6206 }
6207 else
6208 for (;;)
6209 {
6210 if (!same_die_p (c1, c2, mark))
6211 return 0;
6212 c1 = c1->die_sib;
6213 c2 = c2->die_sib;
6214 if (c1 == die1->die_child)
6215 {
6216 if (c2 == die2->die_child)
6217 break;
6218 else
6219 return 0;
6220 }
6221 }
cc0017a9
ZD
6222
6223 return 1;
6224}
6225
6226/* Do the dies look the same? Wrapper around same_die_p. */
6227
6228static int
7080f735 6229same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
cc0017a9
ZD
6230{
6231 int mark = 0;
6232 int ret = same_die_p (die1, die2, &mark);
6233
6234 unmark_all_dies (die1);
6235 unmark_all_dies (die2);
6236
6237 return ret;
6238}
6239
881c6935
JM
6240/* The prefix to attach to symbols on DIEs in the current comdat debug
6241 info section. */
6242static char *comdat_symbol_id;
6243
6244/* The index of the current symbol within the current comdat CU. */
6245static unsigned int comdat_symbol_number;
6246
6247/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6248 children, and set comdat_symbol_id accordingly. */
6249
6250static void
7080f735 6251compute_section_prefix (dw_die_ref unit_die)
881c6935 6252{
cc0017a9
ZD
6253 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6254 const char *base = die_name ? lbasename (die_name) : "anonymous";
703ad42b 6255 char *name = alloca (strlen (base) + 64);
f11c3043 6256 char *p;
cc0017a9 6257 int i, mark;
881c6935
JM
6258 unsigned char checksum[16];
6259 struct md5_ctx ctx;
6260
f11c3043
RK
6261 /* Compute the checksum of the DIE, then append part of it as hex digits to
6262 the name filename of the unit. */
6263
881c6935 6264 md5_init_ctx (&ctx);
cc0017a9
ZD
6265 mark = 0;
6266 die_checksum (unit_die, &ctx, &mark);
6267 unmark_all_dies (unit_die);
881c6935
JM
6268 md5_finish_ctx (&ctx, checksum);
6269
0023400b 6270 sprintf (name, "%s.", base);
881c6935
JM
6271 clean_symbol_name (name);
6272
2ad9852d
RK
6273 p = name + strlen (name);
6274 for (i = 0; i < 4; i++)
6275 {
6276 sprintf (p, "%.2x", checksum[i]);
6277 p += 2;
6278 }
881c6935
JM
6279
6280 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6281 comdat_symbol_number = 0;
6282}
6283
f11c3043 6284/* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
881c6935
JM
6285
6286static int
7080f735 6287is_type_die (dw_die_ref die)
881c6935
JM
6288{
6289 switch (die->die_tag)
6290 {
6291 case DW_TAG_array_type:
6292 case DW_TAG_class_type:
6293 case DW_TAG_enumeration_type:
6294 case DW_TAG_pointer_type:
6295 case DW_TAG_reference_type:
6296 case DW_TAG_string_type:
6297 case DW_TAG_structure_type:
6298 case DW_TAG_subroutine_type:
6299 case DW_TAG_union_type:
6300 case DW_TAG_ptr_to_member_type:
6301 case DW_TAG_set_type:
6302 case DW_TAG_subrange_type:
6303 case DW_TAG_base_type:
6304 case DW_TAG_const_type:
6305 case DW_TAG_file_type:
6306 case DW_TAG_packed_type:
6307 case DW_TAG_volatile_type:
cc0017a9 6308 case DW_TAG_typedef:
881c6935
JM
6309 return 1;
6310 default:
6311 return 0;
6312 }
6313}
6314
6315/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6316 Basically, we want to choose the bits that are likely to be shared between
6317 compilations (types) and leave out the bits that are specific to individual
6318 compilations (functions). */
6319
6320static int
7080f735 6321is_comdat_die (dw_die_ref c)
881c6935 6322{
2ad9852d
RK
6323 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6324 we do for stabs. The advantage is a greater likelihood of sharing between
6325 objects that don't include headers in the same order (and therefore would
6326 put the base types in a different comdat). jason 8/28/00 */
6327
881c6935
JM
6328 if (c->die_tag == DW_TAG_base_type)
6329 return 0;
6330
6331 if (c->die_tag == DW_TAG_pointer_type
6332 || c->die_tag == DW_TAG_reference_type
6333 || c->die_tag == DW_TAG_const_type
6334 || c->die_tag == DW_TAG_volatile_type)
6335 {
6336 dw_die_ref t = get_AT_ref (c, DW_AT_type);
2ad9852d 6337
881c6935
JM
6338 return t ? is_comdat_die (t) : 0;
6339 }
881c6935
JM
6340
6341 return is_type_die (c);
6342}
6343
6344/* Returns 1 iff C is the sort of DIE that might be referred to from another
6345 compilation unit. */
6346
6347static int
7080f735 6348is_symbol_die (dw_die_ref c)
881c6935 6349{
2ad9852d 6350 return (is_type_die (c)
c26fbbca 6351 || (get_AT (c, DW_AT_declaration)
ef85ac9a
JJ
6352 && !get_AT (c, DW_AT_specification))
6353 || c->die_tag == DW_TAG_namespace);
881c6935
JM
6354}
6355
6356static char *
7080f735 6357gen_internal_sym (const char *prefix)
881c6935
JM
6358{
6359 char buf[256];
2ad9852d 6360
63e46568 6361 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
881c6935
JM
6362 return xstrdup (buf);
6363}
6364
6365/* Assign symbols to all worthy DIEs under DIE. */
6366
6367static void
7080f735 6368assign_symbol_names (dw_die_ref die)
881c6935 6369{
b3694847 6370 dw_die_ref c;
881c6935
JM
6371
6372 if (is_symbol_die (die))
6373 {
6374 if (comdat_symbol_id)
6375 {
6376 char *p = alloca (strlen (comdat_symbol_id) + 64);
2ad9852d 6377
881c6935
JM
6378 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6379 comdat_symbol_id, comdat_symbol_number++);
6380 die->die_symbol = xstrdup (p);
6381 }
6382 else
63e46568 6383 die->die_symbol = gen_internal_sym ("LDIE");
881c6935
JM
6384 }
6385
d6eeb3ba 6386 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
881c6935
JM
6387}
6388
cc0017a9
ZD
6389struct cu_hash_table_entry
6390{
6391 dw_die_ref cu;
6392 unsigned min_comdat_num, max_comdat_num;
6393 struct cu_hash_table_entry *next;
6394};
6395
6396/* Routines to manipulate hash table of CUs. */
6397static hashval_t
7080f735 6398htab_cu_hash (const void *of)
cc0017a9
ZD
6399{
6400 const struct cu_hash_table_entry *entry = of;
6401
6402 return htab_hash_string (entry->cu->die_symbol);
6403}
6404
6405static int
7080f735 6406htab_cu_eq (const void *of1, const void *of2)
cc0017a9
ZD
6407{
6408 const struct cu_hash_table_entry *entry1 = of1;
6409 const struct die_struct *entry2 = of2;
6410
6411 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6412}
6413
6414static void
7080f735 6415htab_cu_del (void *what)
cc0017a9
ZD
6416{
6417 struct cu_hash_table_entry *next, *entry = what;
6418
6419 while (entry)
6420 {
6421 next = entry->next;
6422 free (entry);
6423 entry = next;
6424 }
6425}
6426
6427/* Check whether we have already seen this CU and set up SYM_NUM
6428 accordingly. */
6429static int
7080f735 6430check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
cc0017a9
ZD
6431{
6432 struct cu_hash_table_entry dummy;
6433 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6434
6435 dummy.max_comdat_num = 0;
6436
6437 slot = (struct cu_hash_table_entry **)
6438 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6439 INSERT);
6440 entry = *slot;
6441
6442 for (; entry; last = entry, entry = entry->next)
6443 {
6444 if (same_die_p_wrap (cu, entry->cu))
6445 break;
6446 }
6447
6448 if (entry)
6449 {
6450 *sym_num = entry->min_comdat_num;
6451 return 1;
6452 }
6453
5ed6ace5 6454 entry = XCNEW (struct cu_hash_table_entry);
cc0017a9
ZD
6455 entry->cu = cu;
6456 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6457 entry->next = *slot;
6458 *slot = entry;
6459
6460 return 0;
6461}
6462
6463/* Record SYM_NUM to record of CU in HTABLE. */
6464static void
7080f735 6465record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
cc0017a9
ZD
6466{
6467 struct cu_hash_table_entry **slot, *entry;
6468
6469 slot = (struct cu_hash_table_entry **)
6470 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6471 NO_INSERT);
6472 entry = *slot;
6473
6474 entry->max_comdat_num = sym_num;
6475}
6476
881c6935
JM
6477/* Traverse the DIE (which is always comp_unit_die), and set up
6478 additional compilation units for each of the include files we see
6479 bracketed by BINCL/EINCL. */
6480
6481static void
7080f735 6482break_out_includes (dw_die_ref die)
881c6935 6483{
d6eeb3ba 6484 dw_die_ref c;
b3694847 6485 dw_die_ref unit = NULL;
cc0017a9
ZD
6486 limbo_die_node *node, **pnode;
6487 htab_t cu_hash_table;
881c6935 6488
d6eeb3ba
GK
6489 c = die->die_child;
6490 if (c) do {
6491 dw_die_ref prev = c;
6492 c = c->die_sib;
6493 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6494 || (unit && is_comdat_die (c)))
6495 {
6496 dw_die_ref next = c->die_sib;
6497
6498 /* This DIE is for a secondary CU; remove it from the main one. */
6499 remove_child_with_prev (c, prev);
2878ea73 6500
d6eeb3ba
GK
6501 if (c->die_tag == DW_TAG_GNU_BINCL)
6502 unit = push_new_compile_unit (unit, c);
6503 else if (c->die_tag == DW_TAG_GNU_EINCL)
6504 unit = pop_compile_unit (unit);
6505 else
6506 add_child_die (unit, c);
6507 c = next;
6508 if (c == die->die_child)
6509 break;
6510 }
6511 } while (c != die->die_child);
881c6935
JM
6512
6513#if 0
6514 /* We can only use this in debugging, since the frontend doesn't check
0b34cf1e 6515 to make sure that we leave every include file we enter. */
ced3f397 6516 gcc_assert (!unit);
881c6935
JM
6517#endif
6518
6519 assign_symbol_names (die);
cc0017a9
ZD
6520 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6521 for (node = limbo_die_list, pnode = &limbo_die_list;
6522 node;
6523 node = node->next)
881c6935 6524 {
cc0017a9
ZD
6525 int is_dupl;
6526
881c6935 6527 compute_section_prefix (node->die);
cc0017a9
ZD
6528 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6529 &comdat_symbol_number);
881c6935 6530 assign_symbol_names (node->die);
cc0017a9
ZD
6531 if (is_dupl)
6532 *pnode = node->next;
6533 else
73c68f61 6534 {
cc0017a9
ZD
6535 pnode = &node->next;
6536 record_comdat_symbol_number (node->die, cu_hash_table,
6537 comdat_symbol_number);
6538 }
881c6935 6539 }
cc0017a9 6540 htab_delete (cu_hash_table);
881c6935
JM
6541}
6542
6543/* Traverse the DIE and add a sibling attribute if it may have the
6544 effect of speeding up access to siblings. To save some space,
6545 avoid generating sibling attributes for DIE's without children. */
6546
6547static void
7080f735 6548add_sibling_attributes (dw_die_ref die)
881c6935 6549{
b3694847 6550 dw_die_ref c;
881c6935 6551
d6eeb3ba
GK
6552 if (! die->die_child)
6553 return;
6554
6555 if (die->die_parent && die != die->die_parent->die_child)
7d9d8943
AM
6556 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6557
d6eeb3ba 6558 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7d9d8943
AM
6559}
6560
2ad9852d
RK
6561/* Output all location lists for the DIE and its children. */
6562
63e46568 6563static void
7080f735 6564output_location_lists (dw_die_ref die)
63e46568
DB
6565{
6566 dw_die_ref c;
1a27722f
GK
6567 dw_attr_ref a;
6568 unsigned ix;
2ad9852d 6569
1a27722f
GK
6570 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6571 if (AT_class (a) == dw_val_class_loc_list)
6572 output_loc_list (AT_loc_list (a));
2ad9852d 6573
d6eeb3ba 6574 FOR_EACH_CHILD (die, c, output_location_lists (c));
63e46568 6575}
c26fbbca 6576
2ad9852d
RK
6577/* The format of each DIE (and its attribute value pairs) is encoded in an
6578 abbreviation table. This routine builds the abbreviation table and assigns
6579 a unique abbreviation id for each abbreviation entry. The children of each
6580 die are visited recursively. */
7d9d8943
AM
6581
6582static void
7080f735 6583build_abbrev_table (dw_die_ref die)
7d9d8943 6584{
b3694847
SS
6585 unsigned long abbrev_id;
6586 unsigned int n_alloc;
6587 dw_die_ref c;
1a27722f
GK
6588 dw_attr_ref a;
6589 unsigned ix;
881c6935
JM
6590
6591 /* Scan the DIE references, and mark as external any that refer to
1bfb5f8f 6592 DIEs from other CUs (i.e. those which are not marked). */
1a27722f
GK
6593 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6594 if (AT_class (a) == dw_val_class_die_ref
6595 && AT_ref (a)->die_mark == 0)
2ad9852d 6596 {
1a27722f 6597 gcc_assert (AT_ref (a)->die_symbol);
2ad9852d 6598
1a27722f 6599 set_AT_ref_external (a, 1);
2ad9852d 6600 }
881c6935 6601
7d9d8943
AM
6602 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6603 {
b3694847 6604 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
1a27722f
GK
6605 dw_attr_ref die_a, abbrev_a;
6606 unsigned ix;
6607 bool ok = true;
2878ea73 6608
1a27722f
GK
6609 if (abbrev->die_tag != die->die_tag)
6610 continue;
6611 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6612 continue;
2878ea73 6613
1a27722f
GK
6614 if (VEC_length (dw_attr_node, abbrev->die_attr)
6615 != VEC_length (dw_attr_node, die->die_attr))
6616 continue;
2878ea73 6617
1a27722f 6618 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7d9d8943 6619 {
1a27722f
GK
6620 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6621 if ((abbrev_a->dw_attr != die_a->dw_attr)
6622 || (value_format (abbrev_a) != value_format (die_a)))
7d9d8943 6623 {
1a27722f
GK
6624 ok = false;
6625 break;
7d9d8943
AM
6626 }
6627 }
1a27722f
GK
6628 if (ok)
6629 break;
7d9d8943
AM
6630 }
6631
6632 if (abbrev_id >= abbrev_die_table_in_use)
6633 {
6634 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6635 {
6636 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
17211ab5
GK
6637 abbrev_die_table = ggc_realloc (abbrev_die_table,
6638 sizeof (dw_die_ref) * n_alloc);
7d9d8943 6639
703ad42b 6640 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7d9d8943
AM
6641 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6642 abbrev_die_table_allocated = n_alloc;
6643 }
6644
6645 ++abbrev_die_table_in_use;
6646 abbrev_die_table[abbrev_id] = die;
6647 }
6648
6649 die->die_abbrev = abbrev_id;
d6eeb3ba 6650 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7d9d8943
AM
6651}
6652\f
3f76745e
JM
6653/* Return the power-of-two number of bytes necessary to represent VALUE. */
6654
6655static int
7080f735 6656constant_size (long unsigned int value)
3f76745e
JM
6657{
6658 int log;
6659
6660 if (value == 0)
6661 log = 0;
a3f97cbb 6662 else
3f76745e 6663 log = floor_log2 (value);
71dfc51f 6664
3f76745e
JM
6665 log = log / 8;
6666 log = 1 << (floor_log2 (log) + 1);
6667
6668 return log;
a3f97cbb
JW
6669}
6670
2ad9852d 6671/* Return the size of a DIE as it is represented in the
3f76745e 6672 .debug_info section. */
71dfc51f 6673
3f76745e 6674static unsigned long
7080f735 6675size_of_die (dw_die_ref die)
a3f97cbb 6676{
b3694847
SS
6677 unsigned long size = 0;
6678 dw_attr_ref a;
1a27722f 6679 unsigned ix;
71dfc51f 6680
3f76745e 6681 size += size_of_uleb128 (die->die_abbrev);
1a27722f 6682 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
a3f97cbb 6683 {
a96c67ec 6684 switch (AT_class (a))
a3f97cbb
JW
6685 {
6686 case dw_val_class_addr:
a1a4189d 6687 size += DWARF2_ADDR_SIZE;
a3f97cbb 6688 break;
a20612aa
RH
6689 case dw_val_class_offset:
6690 size += DWARF_OFFSET_SIZE;
6691 break;
a3f97cbb 6692 case dw_val_class_loc:
3f76745e 6693 {
b3694847 6694 unsigned long lsize = size_of_locs (AT_loc (a));
71dfc51f 6695
3f76745e
JM
6696 /* Block length. */
6697 size += constant_size (lsize);
6698 size += lsize;
6699 }
a3f97cbb 6700 break;
63e46568
DB
6701 case dw_val_class_loc_list:
6702 size += DWARF_OFFSET_SIZE;
6703 break;
2bee6045
JJ
6704 case dw_val_class_range_list:
6705 size += DWARF_OFFSET_SIZE;
6706 break;
a3f97cbb 6707 case dw_val_class_const:
25dd13ec 6708 size += size_of_sleb128 (AT_int (a));
a3f97cbb
JW
6709 break;
6710 case dw_val_class_unsigned_const:
a96c67ec 6711 size += constant_size (AT_unsigned (a));
a3f97cbb 6712 break;
469ac993 6713 case dw_val_class_long_long:
2e4b9b8c 6714 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
469ac993 6715 break;
e7ee3914
AM
6716 case dw_val_class_vec:
6717 size += 1 + (a->dw_attr_val.v.val_vec.length
6718 * a->dw_attr_val.v.val_vec.elt_size); /* block */
a3f97cbb
JW
6719 break;
6720 case dw_val_class_flag:
3f76745e 6721 size += 1;
a3f97cbb
JW
6722 break;
6723 case dw_val_class_die_ref:
323658ea
ZD
6724 if (AT_ref_external (a))
6725 size += DWARF2_ADDR_SIZE;
6726 else
6727 size += DWARF_OFFSET_SIZE;
a3f97cbb
JW
6728 break;
6729 case dw_val_class_fde_ref:
3f76745e 6730 size += DWARF_OFFSET_SIZE;
a3f97cbb
JW
6731 break;
6732 case dw_val_class_lbl_id:
a1a4189d 6733 size += DWARF2_ADDR_SIZE;
3f76745e 6734 break;
192d0f89
GK
6735 case dw_val_class_lineptr:
6736 case dw_val_class_macptr:
3f76745e
JM
6737 size += DWARF_OFFSET_SIZE;
6738 break;
6739 case dw_val_class_str:
9eb4015a
JJ
6740 if (AT_string_form (a) == DW_FORM_strp)
6741 size += DWARF_OFFSET_SIZE;
6742 else
17211ab5 6743 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
3f76745e 6744 break;
d5688810
GK
6745 case dw_val_class_file:
6746 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6747 break;
3f76745e 6748 default:
ced3f397 6749 gcc_unreachable ();
3f76745e 6750 }
a3f97cbb 6751 }
3f76745e
JM
6752
6753 return size;
a3f97cbb
JW
6754}
6755
2ad9852d
RK
6756/* Size the debugging information associated with a given DIE. Visits the
6757 DIE's children recursively. Updates the global variable next_die_offset, on
6758 each time through. Uses the current value of next_die_offset to update the
6759 die_offset field in each DIE. */
71dfc51f 6760
a3f97cbb 6761static void
7080f735 6762calc_die_sizes (dw_die_ref die)
a3f97cbb 6763{
b3694847 6764 dw_die_ref c;
2ad9852d 6765
3f76745e
JM
6766 die->die_offset = next_die_offset;
6767 next_die_offset += size_of_die (die);
71dfc51f 6768
d6eeb3ba 6769 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
71dfc51f 6770
3f76745e
JM
6771 if (die->die_child != NULL)
6772 /* Count the null byte used to terminate sibling lists. */
6773 next_die_offset += 1;
a3f97cbb
JW
6774}
6775
1bfb5f8f 6776/* Set the marks for a die and its children. We do this so
881c6935 6777 that we know whether or not a reference needs to use FORM_ref_addr; only
1bfb5f8f
JM
6778 DIEs in the same CU will be marked. We used to clear out the offset
6779 and use that as the flag, but ran into ordering problems. */
881c6935
JM
6780
6781static void
7080f735 6782mark_dies (dw_die_ref die)
881c6935 6783{
b3694847 6784 dw_die_ref c;
2ad9852d 6785
ced3f397 6786 gcc_assert (!die->die_mark);
7080f735 6787
1bfb5f8f 6788 die->die_mark = 1;
d6eeb3ba 6789 FOR_EACH_CHILD (die, c, mark_dies (c));
1bfb5f8f
JM
6790}
6791
6792/* Clear the marks for a die and its children. */
6793
6794static void
7080f735 6795unmark_dies (dw_die_ref die)
1bfb5f8f 6796{
b3694847 6797 dw_die_ref c;
2ad9852d 6798
ced3f397 6799 gcc_assert (die->die_mark);
7080f735 6800
1bfb5f8f 6801 die->die_mark = 0;
d6eeb3ba 6802 FOR_EACH_CHILD (die, c, unmark_dies (c));
881c6935
JM
6803}
6804
cc0017a9
ZD
6805/* Clear the marks for a die, its children and referred dies. */
6806
6807static void
7080f735 6808unmark_all_dies (dw_die_ref die)
cc0017a9
ZD
6809{
6810 dw_die_ref c;
6811 dw_attr_ref a;
1a27722f 6812 unsigned ix;
cc0017a9
ZD
6813
6814 if (!die->die_mark)
6815 return;
6816 die->die_mark = 0;
6817
d6eeb3ba 6818 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
cc0017a9 6819
1a27722f 6820 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
cc0017a9
ZD
6821 if (AT_class (a) == dw_val_class_die_ref)
6822 unmark_all_dies (AT_ref (a));
6823}
6824
2878ea73 6825/* Return the size of the .debug_pubnames or .debug_pubtypes table
89708594 6826 generated for the compilation unit. */
a94dbf2c 6827
3f76745e 6828static unsigned long
89708594 6829size_of_pubnames (VEC (pubname_entry, gc) * names)
a94dbf2c 6830{
b3694847
SS
6831 unsigned long size;
6832 unsigned i;
89708594 6833 pubname_ref p;
469ac993 6834
3f76745e 6835 size = DWARF_PUBNAMES_HEADER_SIZE;
89708594
CT
6836 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6837 if (names != pubtype_table
6838 || p->die->die_offset != 0
6839 || !flag_eliminate_unused_debug_types)
6840 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
a94dbf2c 6841
3f76745e
JM
6842 size += DWARF_OFFSET_SIZE;
6843 return size;
a94dbf2c
JM
6844}
6845
956d6950 6846/* Return the size of the information in the .debug_aranges section. */
469ac993 6847
3f76745e 6848static unsigned long
7080f735 6849size_of_aranges (void)
469ac993 6850{
b3694847 6851 unsigned long size;
469ac993 6852
3f76745e 6853 size = DWARF_ARANGES_HEADER_SIZE;
469ac993 6854
3f76745e 6855 /* Count the address/length pair for this compilation unit. */
a1a4189d
JB
6856 size += 2 * DWARF2_ADDR_SIZE;
6857 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
469ac993 6858
3f76745e 6859 /* Count the two zero words used to terminated the address range table. */
a1a4189d 6860 size += 2 * DWARF2_ADDR_SIZE;
3f76745e
JM
6861 return size;
6862}
6863\f
6864/* Select the encoding of an attribute value. */
6865
6866static enum dwarf_form
7080f735 6867value_format (dw_attr_ref a)
3f76745e 6868{
a96c67ec 6869 switch (a->dw_attr_val.val_class)
469ac993 6870 {
3f76745e
JM
6871 case dw_val_class_addr:
6872 return DW_FORM_addr;
2bee6045 6873 case dw_val_class_range_list:
a20612aa 6874 case dw_val_class_offset:
0c33762a 6875 case dw_val_class_loc_list:
ced3f397
NS
6876 switch (DWARF_OFFSET_SIZE)
6877 {
6878 case 4:
6879 return DW_FORM_data4;
6880 case 8:
6881 return DW_FORM_data8;
6882 default:
6883 gcc_unreachable ();
6884 }
3f76745e 6885 case dw_val_class_loc:
a96c67ec 6886 switch (constant_size (size_of_locs (AT_loc (a))))
469ac993 6887 {
3f76745e
JM
6888 case 1:
6889 return DW_FORM_block1;
6890 case 2:
6891 return DW_FORM_block2;
469ac993 6892 default:
ced3f397 6893 gcc_unreachable ();
469ac993 6894 }
3f76745e 6895 case dw_val_class_const:
25dd13ec 6896 return DW_FORM_sdata;
3f76745e 6897 case dw_val_class_unsigned_const:
a96c67ec 6898 switch (constant_size (AT_unsigned (a)))
3f76745e
JM
6899 {
6900 case 1:
6901 return DW_FORM_data1;
6902 case 2:
6903 return DW_FORM_data2;
6904 case 4:
6905 return DW_FORM_data4;
6906 case 8:
6907 return DW_FORM_data8;
6908 default:
ced3f397 6909 gcc_unreachable ();
3f76745e
JM
6910 }
6911 case dw_val_class_long_long:
6912 return DW_FORM_block1;
e7ee3914 6913 case dw_val_class_vec:
3f76745e
JM
6914 return DW_FORM_block1;
6915 case dw_val_class_flag:
6916 return DW_FORM_flag;
6917 case dw_val_class_die_ref:
881c6935
JM
6918 if (AT_ref_external (a))
6919 return DW_FORM_ref_addr;
6920 else
6921 return DW_FORM_ref;
3f76745e
JM
6922 case dw_val_class_fde_ref:
6923 return DW_FORM_data;
6924 case dw_val_class_lbl_id:
6925 return DW_FORM_addr;
192d0f89
GK
6926 case dw_val_class_lineptr:
6927 case dw_val_class_macptr:
3f76745e
JM
6928 return DW_FORM_data;
6929 case dw_val_class_str:
9eb4015a 6930 return AT_string_form (a);
d5688810
GK
6931 case dw_val_class_file:
6932 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6933 {
6934 case 1:
6935 return DW_FORM_data1;
6936 case 2:
6937 return DW_FORM_data2;
6938 case 4:
6939 return DW_FORM_data4;
6940 default:
6941 gcc_unreachable ();
6942 }
a20612aa 6943
469ac993 6944 default:
ced3f397 6945 gcc_unreachable ();
469ac993 6946 }
a94dbf2c
JM
6947}
6948
3f76745e 6949/* Output the encoding of an attribute value. */
469ac993 6950
3f76745e 6951static void
7080f735 6952output_value_format (dw_attr_ref a)
a94dbf2c 6953{
a96c67ec 6954 enum dwarf_form form = value_format (a);
2ad9852d 6955
2e4b9b8c 6956 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
3f76745e 6957}
469ac993 6958
3f76745e
JM
6959/* Output the .debug_abbrev section which defines the DIE abbreviation
6960 table. */
469ac993 6961
3f76745e 6962static void
7080f735 6963output_abbrev_section (void)
3f76745e
JM
6964{
6965 unsigned long abbrev_id;
71dfc51f 6966
3f76745e
JM
6967 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6968 {
b3694847 6969 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
1a27722f
GK
6970 unsigned ix;
6971 dw_attr_ref a_attr;
71dfc51f 6972
2e4b9b8c 6973 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
2e4b9b8c
RH
6974 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6975 dwarf_tag_name (abbrev->die_tag));
71dfc51f 6976
2e4b9b8c
RH
6977 if (abbrev->die_child != NULL)
6978 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6979 else
6980 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
3f76745e 6981
1a27722f
GK
6982 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6983 ix++)
3f76745e 6984 {
2e4b9b8c
RH
6985 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6986 dwarf_attr_name (a_attr->dw_attr));
a96c67ec 6987 output_value_format (a_attr);
469ac993 6988 }
469ac993 6989
2e4b9b8c
RH
6990 dw2_asm_output_data (1, 0, NULL);
6991 dw2_asm_output_data (1, 0, NULL);
469ac993 6992 }
81f374eb
HPN
6993
6994 /* Terminate the table. */
2e4b9b8c 6995 dw2_asm_output_data (1, 0, NULL);
a94dbf2c
JM
6996}
6997
881c6935
JM
6998/* Output a symbol we can use to refer to this DIE from another CU. */
6999
7000static inline void
7080f735 7001output_die_symbol (dw_die_ref die)
881c6935
JM
7002{
7003 char *sym = die->die_symbol;
7004
7005 if (sym == 0)
7006 return;
7007
7008 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7009 /* We make these global, not weak; if the target doesn't support
7010 .linkonce, it doesn't support combining the sections, so debugging
7011 will break. */
5fd9b178 7012 targetm.asm_out.globalize_label (asm_out_file, sym);
2ad9852d 7013
881c6935
JM
7014 ASM_OUTPUT_LABEL (asm_out_file, sym);
7015}
7016
84a5b4f8 7017/* Return a new location list, given the begin and end range, and the
2ad9852d
RK
7018 expression. gensym tells us whether to generate a new internal symbol for
7019 this location list node, which is done for the head of the list only. */
7020
84a5b4f8 7021static inline dw_loc_list_ref
7080f735
AJ
7022new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7023 const char *section, unsigned int gensym)
84a5b4f8 7024{
17211ab5 7025 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
2ad9852d 7026
84a5b4f8
DB
7027 retlist->begin = begin;
7028 retlist->end = end;
7029 retlist->expr = expr;
7030 retlist->section = section;
c26fbbca 7031 if (gensym)
84a5b4f8 7032 retlist->ll_symbol = gen_internal_sym ("LLST");
2ad9852d 7033
84a5b4f8
DB
7034 return retlist;
7035}
7036
f9da5064 7037/* Add a location description expression to a location list. */
2ad9852d 7038
84a5b4f8 7039static inline void
7080f735
AJ
7040add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7041 const char *begin, const char *end,
7042 const char *section)
84a5b4f8 7043{
b3694847 7044 dw_loc_list_ref *d;
c26fbbca 7045
30f7a378 7046 /* Find the end of the chain. */
84a5b4f8
DB
7047 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7048 ;
2ad9852d 7049
f9da5064 7050 /* Add a new location list node to the list. */
84a5b4f8
DB
7051 *d = new_loc_list (descr, begin, end, section, 0);
7052}
7053
87c8b4be
CT
7054static void
7055dwarf2out_switch_text_section (void)
7056{
7057 dw_fde_ref fde;
7058
1ad435a5 7059 gcc_assert (cfun);
c7466dee 7060
87c8b4be
CT
7061 fde = &fde_table[fde_table_in_use - 1];
7062 fde->dw_fde_switched_sections = true;
c7466dee
CT
7063 fde->dw_fde_hot_section_label = cfun->hot_section_label;
7064 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7065 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7066 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
1146e682 7067 have_multiple_function_sections = true;
6a10f7b3
RS
7068
7069 /* Reset the current label on switching text sections, so that we
7070 don't attempt to advance_loc4 between labels in different sections. */
7071 fde->dw_fde_current_label = NULL;
87c8b4be
CT
7072}
7073
f9da5064 7074/* Output the location list given to us. */
2ad9852d 7075
63e46568 7076static void
7080f735 7077output_loc_list (dw_loc_list_ref list_head)
63e46568 7078{
2ad9852d
RK
7079 dw_loc_list_ref curr = list_head;
7080
63e46568 7081 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
a20612aa 7082
1711adc2 7083 /* Walk the location list, and output each range + expression. */
c26fbbca 7084 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
63e46568 7085 {
2bee6045 7086 unsigned long size;
62760ffd
CT
7087 /* Don't output an entry that starts and ends at the same address. */
7088 if (strcmp (curr->begin, curr->end) == 0)
7089 continue;
1146e682 7090 if (!have_multiple_function_sections)
1711adc2
DB
7091 {
7092 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7093 "Location list begin address (%s)",
7094 list_head->ll_symbol);
7095 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7096 "Location list end address (%s)",
7097 list_head->ll_symbol);
7098 }
7099 else
7100 {
7101 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7102 "Location list begin address (%s)",
7103 list_head->ll_symbol);
7104 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7105 "Location list end address (%s)",
7106 list_head->ll_symbol);
7107 }
63e46568 7108 size = size_of_locs (curr->expr);
c26fbbca 7109
63e46568 7110 /* Output the block length for this list of location operations. */
ced3f397 7111 gcc_assert (size <= 0xffff);
2bee6045
JJ
7112 dw2_asm_output_data (2, size, "%s", "Location expression size");
7113
63e46568
DB
7114 output_loc_sequence (curr->expr);
7115 }
2ad9852d 7116
1711adc2 7117 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
aafdcfcd
NS
7118 "Location list terminator begin (%s)",
7119 list_head->ll_symbol);
1711adc2 7120 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
aafdcfcd
NS
7121 "Location list terminator end (%s)",
7122 list_head->ll_symbol);
63e46568 7123}
9eb4015a 7124
3f76745e
JM
7125/* Output the DIE and its attributes. Called recursively to generate
7126 the definitions of each child DIE. */
71dfc51f 7127
a3f97cbb 7128static void
7080f735 7129output_die (dw_die_ref die)
a3f97cbb 7130{
b3694847
SS
7131 dw_attr_ref a;
7132 dw_die_ref c;
7133 unsigned long size;
1a27722f 7134 unsigned ix;
a94dbf2c 7135
881c6935
JM
7136 /* If someone in another CU might refer to us, set up a symbol for
7137 them to point to. */
7138 if (die->die_symbol)
7139 output_die_symbol (die);
7140
2e4b9b8c 7141 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8d5b1b67
GK
7142 (unsigned long)die->die_offset,
7143 dwarf_tag_name (die->die_tag));
a94dbf2c 7144
1a27722f 7145 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
a3f97cbb 7146 {
2e4b9b8c
RH
7147 const char *name = dwarf_attr_name (a->dw_attr);
7148
a96c67ec 7149 switch (AT_class (a))
3f76745e
JM
7150 {
7151 case dw_val_class_addr:
2e4b9b8c 7152 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
3f76745e 7153 break;
a3f97cbb 7154
a20612aa
RH
7155 case dw_val_class_offset:
7156 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7157 "%s", name);
7158 break;
7159
2bee6045
JJ
7160 case dw_val_class_range_list:
7161 {
7162 char *p = strchr (ranges_section_label, '\0');
7163
38f9cd4c
AJ
7164 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7165 a->dw_attr_val.v.val_offset);
2bee6045 7166 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
192d0f89 7167 debug_ranges_section, "%s", name);
2bee6045
JJ
7168 *p = '\0';
7169 }
7170 break;
7171
3f76745e 7172 case dw_val_class_loc:
a96c67ec 7173 size = size_of_locs (AT_loc (a));
71dfc51f 7174
3f76745e 7175 /* Output the block length for this list of location operations. */
2e4b9b8c 7176 dw2_asm_output_data (constant_size (size), size, "%s", name);
71dfc51f 7177
7d9d8943 7178 output_loc_sequence (AT_loc (a));
a3f97cbb 7179 break;
3f76745e
JM
7180
7181 case dw_val_class_const:
25dd13ec
JW
7182 /* ??? It would be slightly more efficient to use a scheme like is
7183 used for unsigned constants below, but gdb 4.x does not sign
7184 extend. Gdb 5.x does sign extend. */
2e4b9b8c 7185 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
a3f97cbb 7186 break;
3f76745e
JM
7187
7188 case dw_val_class_unsigned_const:
2e4b9b8c
RH
7189 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7190 AT_unsigned (a), "%s", name);
a3f97cbb 7191 break;
3f76745e
JM
7192
7193 case dw_val_class_long_long:
2e4b9b8c
RH
7194 {
7195 unsigned HOST_WIDE_INT first, second;
3f76745e 7196
2ad9852d
RK
7197 dw2_asm_output_data (1,
7198 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
c26fbbca 7199 "%s", name);
556273e0 7200
2e4b9b8c
RH
7201 if (WORDS_BIG_ENDIAN)
7202 {
7203 first = a->dw_attr_val.v.val_long_long.hi;
7204 second = a->dw_attr_val.v.val_long_long.low;
7205 }
7206 else
7207 {
7208 first = a->dw_attr_val.v.val_long_long.low;
7209 second = a->dw_attr_val.v.val_long_long.hi;
7210 }
2ad9852d
RK
7211
7212 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
2e4b9b8c 7213 first, "long long constant");
2ad9852d 7214 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
2e4b9b8c
RH
7215 second, NULL);
7216 }
a3f97cbb 7217 break;
3f76745e 7218
e7ee3914 7219 case dw_val_class_vec:
c84e2712 7220 {
e7ee3914
AM
7221 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7222 unsigned int len = a->dw_attr_val.v.val_vec.length;
b3694847 7223 unsigned int i;
e7ee3914 7224 unsigned char *p;
c84e2712 7225
e7ee3914
AM
7226 dw2_asm_output_data (1, len * elt_size, "%s", name);
7227 if (elt_size > sizeof (HOST_WIDE_INT))
7228 {
7229 elt_size /= 2;
7230 len *= 2;
7231 }
7232 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7233 i < len;
7234 i++, p += elt_size)
7235 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7236 "fp or vector constant word %u", i);
556273e0 7237 break;
c84e2712 7238 }
3f76745e
JM
7239
7240 case dw_val_class_flag:
2e4b9b8c 7241 dw2_asm_output_data (1, AT_flag (a), "%s", name);
a3f97cbb 7242 break;
a20612aa 7243
c26fbbca 7244 case dw_val_class_loc_list:
63e46568
DB
7245 {
7246 char *sym = AT_loc_list (a)->ll_symbol;
2ad9852d 7247
ced3f397 7248 gcc_assert (sym);
192d0f89
GK
7249 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7250 "%s", name);
63e46568
DB
7251 }
7252 break;
a20612aa 7253
3f76745e 7254 case dw_val_class_die_ref:
881c6935 7255 if (AT_ref_external (a))
2e4b9b8c
RH
7256 {
7257 char *sym = AT_ref (a)->die_symbol;
2ad9852d 7258
ced3f397 7259 gcc_assert (sym);
192d0f89
GK
7260 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7261 "%s", name);
2e4b9b8c 7262 }
881c6935 7263 else
ced3f397
NS
7264 {
7265 gcc_assert (AT_ref (a)->die_offset);
7266 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7267 "%s", name);
7268 }
a3f97cbb 7269 break;
3f76745e
JM
7270
7271 case dw_val_class_fde_ref:
a6ab3aad
JM
7272 {
7273 char l1[20];
2ad9852d 7274
2e4b9b8c
RH
7275 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7276 a->dw_attr_val.v.val_fde_index * 2);
192d0f89
GK
7277 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7278 "%s", name);
a6ab3aad 7279 }
a3f97cbb 7280 break;
a3f97cbb 7281
3f76745e 7282 case dw_val_class_lbl_id:
8e7fa2c8 7283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
3f76745e 7284 break;
71dfc51f 7285
192d0f89
GK
7286 case dw_val_class_lineptr:
7287 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7288 debug_line_section, "%s", name);
7289 break;
7290
7291 case dw_val_class_macptr:
7292 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7293 debug_macinfo_section, "%s", name);
3f76745e 7294 break;
a3f97cbb 7295
3f76745e 7296 case dw_val_class_str:
9eb4015a
JJ
7297 if (AT_string_form (a) == DW_FORM_strp)
7298 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7299 a->dw_attr_val.v.val_str->label,
192d0f89 7300 debug_str_section,
a4cf1d85 7301 "%s: \"%s\"", name, AT_string (a));
9eb4015a
JJ
7302 else
7303 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
3f76745e 7304 break;
b2932ae5 7305
d5688810
GK
7306 case dw_val_class_file:
7307 {
7308 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
2878ea73 7309
d5688810
GK
7310 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7311 a->dw_attr_val.v.val_file->filename);
7312 break;
7313 }
7314
3f76745e 7315 default:
ced3f397 7316 gcc_unreachable ();
3f76745e 7317 }
3f76745e 7318 }
71dfc51f 7319
d6eeb3ba 7320 FOR_EACH_CHILD (die, c, output_die (c));
71dfc51f 7321
2ad9852d 7322 /* Add null byte to terminate sibling list. */
3f76745e 7323 if (die->die_child != NULL)
2ad9852d 7324 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8d5b1b67 7325 (unsigned long) die->die_offset);
3f76745e 7326}
71dfc51f 7327
3f76745e
JM
7328/* Output the compilation unit that appears at the beginning of the
7329 .debug_info section, and precedes the DIE descriptions. */
71dfc51f 7330
3f76745e 7331static void
7080f735 7332output_compilation_unit_header (void)
3f76745e 7333{
9eb0ef7a
KB
7334 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7335 dw2_asm_output_data (4, 0xffffffff,
7336 "Initial length escape value indicating 64-bit DWARF extension");
7337 dw2_asm_output_data (DWARF_OFFSET_SIZE,
2878ea73 7338 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
2e4b9b8c 7339 "Length of Compilation Unit Info");
2e4b9b8c 7340 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
2e4b9b8c 7341 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
192d0f89 7342 debug_abbrev_section,
2e4b9b8c 7343 "Offset Into Abbrev. Section");
2e4b9b8c 7344 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
a3f97cbb
JW
7345}
7346
881c6935
JM
7347/* Output the compilation unit DIE and its children. */
7348
7349static void
7080f735 7350output_comp_unit (dw_die_ref die, int output_if_empty)
881c6935 7351{
ce1cc601 7352 const char *secname;
cc0017a9
ZD
7353 char *oldsym, *tmp;
7354
7355 /* Unless we are outputting main CU, we may throw away empty ones. */
7356 if (!output_if_empty && die->die_child == NULL)
7357 return;
881c6935 7358
2ad9852d
RK
7359 /* Even if there are no children of this DIE, we must output the information
7360 about the compilation unit. Otherwise, on an empty translation unit, we
7361 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7362 will then complain when examining the file. First mark all the DIEs in
7363 this CU so we know which get local refs. */
1bfb5f8f
JM
7364 mark_dies (die);
7365
7366 build_abbrev_table (die);
7367
6d2f8887 7368 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
881c6935
JM
7369 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7370 calc_die_sizes (die);
7371
cc0017a9
ZD
7372 oldsym = die->die_symbol;
7373 if (oldsym)
881c6935 7374 {
703ad42b 7375 tmp = alloca (strlen (oldsym) + 24);
2ad9852d 7376
cc0017a9 7377 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
ce1cc601 7378 secname = tmp;
881c6935 7379 die->die_symbol = NULL;
d6b5193b 7380 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
881c6935
JM
7381 }
7382 else
d6b5193b 7383 switch_to_section (debug_info_section);
881c6935
JM
7384
7385 /* Output debugging information. */
881c6935
JM
7386 output_compilation_unit_header ();
7387 output_die (die);
7388
1bfb5f8f
JM
7389 /* Leave the marks on the main CU, so we can check them in
7390 output_pubnames. */
cc0017a9
ZD
7391 if (oldsym)
7392 {
7393 unmark_dies (die);
7394 die->die_symbol = oldsym;
7395 }
881c6935
JM
7396}
7397
721a8ac5 7398/* Return the DWARF2/3 pubname associated with a decl. */
a1d7ffe3 7399
d560ee52 7400static const char *
7080f735 7401dwarf2_name (tree decl, int scope)
a1d7ffe3 7402{
721a8ac5 7403 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
a1d7ffe3
JM
7404}
7405
d291dd49 7406/* Add a new entry to .debug_pubnames if appropriate. */
71dfc51f 7407
d291dd49 7408static void
7080f735 7409add_pubname (tree decl, dw_die_ref die)
d291dd49 7410{
89708594 7411 pubname_entry e;
d291dd49
JM
7412
7413 if (! TREE_PUBLIC (decl))
7414 return;
7415
89708594
CT
7416 e.die = die;
7417 e.name = xstrdup (dwarf2_name (decl, 1));
7418 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7419}
7420
7421/* Add a new entry to .debug_pubtypes if appropriate. */
7422
7423static void
7424add_pubtype (tree decl, dw_die_ref die)
7425{
7426 pubname_entry e;
7427
7428 e.name = NULL;
7429 if ((TREE_PUBLIC (decl)
7430 || die->die_parent == comp_unit_die)
7431 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
d291dd49 7432 {
89708594
CT
7433 e.die = die;
7434 if (TYPE_P (decl))
7435 {
7436 if (TYPE_NAME (decl))
7437 {
7438 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
e01e0201 7439 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
89708594
CT
7440 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7441 && DECL_NAME (TYPE_NAME (decl)))
e01e0201 7442 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
2878ea73 7443 else
89708594
CT
7444 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7445 }
7446 }
2878ea73 7447 else
89708594 7448 e.name = xstrdup (dwarf2_name (decl, 1));
71dfc51f 7449
89708594
CT
7450 /* If we don't have a name for the type, there's no point in adding
7451 it to the table. */
7452 if (e.name && e.name[0] != '\0')
7453 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7454 }
d291dd49
JM
7455}
7456
a3f97cbb 7457/* Output the public names table used to speed up access to externally
89708594 7458 visible names; or the public types table used to find type definitions. */
71dfc51f 7459
a3f97cbb 7460static void
89708594 7461output_pubnames (VEC (pubname_entry, gc) * names)
a3f97cbb 7462{
b3694847 7463 unsigned i;
89708594
CT
7464 unsigned long pubnames_length = size_of_pubnames (names);
7465 pubname_ref pub;
71dfc51f 7466
9eb0ef7a
KB
7467 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7468 dw2_asm_output_data (4, 0xffffffff,
7469 "Initial length escape value indicating 64-bit DWARF extension");
89708594
CT
7470 if (names == pubname_table)
7471 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7472 "Length of Public Names Info");
7473 else
7474 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7475 "Length of Public Type Names Info");
2e4b9b8c 7476 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
2e4b9b8c 7477 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
192d0f89 7478 debug_info_section,
2e4b9b8c 7479 "Offset of Compilation Unit Info");
2e4b9b8c
RH
7480 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7481 "Compilation Unit Length");
71dfc51f 7482
89708594 7483 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
a3f97cbb 7484 {
2878ea73 7485 /* We shouldn't see pubnames for DIEs outside of the main CU. */
89708594
CT
7486 if (names == pubname_table)
7487 gcc_assert (pub->die->die_mark);
881c6935 7488
89708594
CT
7489 if (names != pubtype_table
7490 || pub->die->die_offset != 0
7491 || !flag_eliminate_unused_debug_types)
7492 {
7493 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7494 "DIE offset");
71dfc51f 7495
89708594
CT
7496 dw2_asm_output_nstring (pub->name, -1, "external name");
7497 }
a3f97cbb 7498 }
71dfc51f 7499
2e4b9b8c 7500 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
a3f97cbb
JW
7501}
7502
d291dd49 7503/* Add a new entry to .debug_aranges if appropriate. */
71dfc51f 7504
d291dd49 7505static void
7080f735 7506add_arange (tree decl, dw_die_ref die)
d291dd49
JM
7507{
7508 if (! DECL_SECTION_NAME (decl))
7509 return;
7510
7511 if (arange_table_in_use == arange_table_allocated)
7512 {
7513 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7080f735
AJ
7514 arange_table = ggc_realloc (arange_table,
7515 (arange_table_allocated
17211ab5
GK
7516 * sizeof (dw_die_ref)));
7517 memset (arange_table + arange_table_in_use, 0,
7518 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
d291dd49 7519 }
71dfc51f 7520
d291dd49
JM
7521 arange_table[arange_table_in_use++] = die;
7522}
7523
a3f97cbb
JW
7524/* Output the information that goes into the .debug_aranges table.
7525 Namely, define the beginning and ending address range of the
7526 text section generated for this compilation unit. */
71dfc51f 7527
a3f97cbb 7528static void
7080f735 7529output_aranges (void)
a3f97cbb 7530{
b3694847
SS
7531 unsigned i;
7532 unsigned long aranges_length = size_of_aranges ();
71dfc51f 7533
9eb0ef7a
KB
7534 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7535 dw2_asm_output_data (4, 0xffffffff,
7536 "Initial length escape value indicating 64-bit DWARF extension");
2e4b9b8c
RH
7537 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7538 "Length of Address Ranges Info");
2e4b9b8c 7539 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
2e4b9b8c 7540 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
192d0f89 7541 debug_info_section,
2e4b9b8c 7542 "Offset of Compilation Unit Info");
2e4b9b8c 7543 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
2e4b9b8c 7544 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
71dfc51f 7545
262b6384
SC
7546 /* We need to align to twice the pointer size here. */
7547 if (DWARF_ARANGES_PAD_SIZE)
7548 {
2e4b9b8c 7549 /* Pad using a 2 byte words so that padding is correct for any
73c68f61 7550 pointer size. */
2e4b9b8c
RH
7551 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7552 2 * DWARF2_ADDR_SIZE);
770ca8c6 7553 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
2e4b9b8c 7554 dw2_asm_output_data (2, 0, NULL);
262b6384 7555 }
71dfc51f 7556
8e7fa2c8 7557 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
c7466dee
CT
7558 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7559 text_section_label, "Length");
7560 if (flag_reorder_blocks_and_partition)
7561 {
2878ea73 7562 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
c7466dee
CT
7563 "Address");
7564 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7565 cold_text_section_label, "Length");
7566 }
71dfc51f 7567
2ad9852d 7568 for (i = 0; i < arange_table_in_use; i++)
d291dd49 7569 {
e689ae67 7570 dw_die_ref die = arange_table[i];
71dfc51f 7571
881c6935 7572 /* We shouldn't see aranges for DIEs outside of the main CU. */
ced3f397 7573 gcc_assert (die->die_mark);
881c6935 7574
e689ae67 7575 if (die->die_tag == DW_TAG_subprogram)
2e4b9b8c 7576 {
8e7fa2c8 7577 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
173bf5be 7578 "Address");
2e4b9b8c
RH
7579 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7580 get_AT_low_pc (die), "Length");
7581 }
d291dd49 7582 else
a1d7ffe3 7583 {
e689ae67
JM
7584 /* A static variable; extract the symbol from DW_AT_location.
7585 Note that this code isn't currently hit, as we only emit
7586 aranges for functions (jason 9/23/99). */
e689ae67
JM
7587 dw_attr_ref a = get_AT (die, DW_AT_location);
7588 dw_loc_descr_ref loc;
2ad9852d 7589
ced3f397 7590 gcc_assert (a && AT_class (a) == dw_val_class_loc);
e689ae67 7591
a96c67ec 7592 loc = AT_loc (a);
ced3f397 7593 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
e689ae67 7594
2e4b9b8c
RH
7595 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7596 loc->dw_loc_oprnd1.v.val_addr, "Address");
7597 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7598 get_AT_unsigned (die, DW_AT_byte_size),
7599 "Length");
a1d7ffe3 7600 }
d291dd49 7601 }
71dfc51f 7602
a3f97cbb 7603 /* Output the terminator words. */
2e4b9b8c
RH
7604 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7605 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
a3f97cbb
JW
7606}
7607
a20612aa
RH
7608/* Add a new entry to .debug_ranges. Return the offset at which it
7609 was placed. */
7610
7611static unsigned int
0435c1d5 7612add_ranges_num (int num)
a20612aa
RH
7613{
7614 unsigned int in_use = ranges_table_in_use;
7615
7616 if (in_use == ranges_table_allocated)
7617 {
7618 ranges_table_allocated += RANGES_TABLE_INCREMENT;
703ad42b
KG
7619 ranges_table
7620 = ggc_realloc (ranges_table, (ranges_table_allocated
7621 * sizeof (struct dw_ranges_struct)));
17211ab5
GK
7622 memset (ranges_table + ranges_table_in_use, 0,
7623 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
a20612aa
RH
7624 }
7625
0435c1d5 7626 ranges_table[in_use].num = num;
a20612aa
RH
7627 ranges_table_in_use = in_use + 1;
7628
7629 return in_use * 2 * DWARF2_ADDR_SIZE;
7630}
7631
0435c1d5
AO
7632/* Add a new entry to .debug_ranges corresponding to a block, or a
7633 range terminator if BLOCK is NULL. */
7634
7635static unsigned int
7636add_ranges (tree block)
7637{
7638 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7639}
7640
7641/* Add a new entry to .debug_ranges corresponding to a pair of
7642 labels. */
7643
7644static unsigned int
7645add_ranges_by_labels (const char *begin, const char *end)
7646{
7647 unsigned int in_use = ranges_by_label_in_use;
7648
7649 if (in_use == ranges_by_label_allocated)
7650 {
7651 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7652 ranges_by_label
7653 = ggc_realloc (ranges_by_label,
7654 (ranges_by_label_allocated
7655 * sizeof (struct dw_ranges_by_label_struct)));
7656 memset (ranges_by_label + ranges_by_label_in_use, 0,
7657 RANGES_TABLE_INCREMENT
7658 * sizeof (struct dw_ranges_by_label_struct));
7659 }
7660
7661 ranges_by_label[in_use].begin = begin;
7662 ranges_by_label[in_use].end = end;
7663 ranges_by_label_in_use = in_use + 1;
7664
7665 return add_ranges_num (-(int)in_use - 1);
7666}
7667
a20612aa 7668static void
7080f735 7669output_ranges (void)
a20612aa 7670{
b3694847 7671 unsigned i;
83182544 7672 static const char *const start_fmt = "Offset 0x%x";
a20612aa
RH
7673 const char *fmt = start_fmt;
7674
2ad9852d 7675 for (i = 0; i < ranges_table_in_use; i++)
a20612aa 7676 {
0435c1d5 7677 int block_num = ranges_table[i].num;
a20612aa 7678
0435c1d5 7679 if (block_num > 0)
a20612aa
RH
7680 {
7681 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7682 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7683
7684 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7685 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7686
7687 /* If all code is in the text section, then the compilation
7688 unit base address defaults to DW_AT_low_pc, which is the
7689 base of the text section. */
1146e682 7690 if (!have_multiple_function_sections)
a20612aa 7691 {
c7466dee
CT
7692 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7693 text_section_label,
7694 fmt, i * 2 * DWARF2_ADDR_SIZE);
7695 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7696 text_section_label, NULL);
a20612aa 7697 }
2ad9852d 7698
0435c1d5
AO
7699 /* Otherwise, the compilation unit base address is zero,
7700 which allows us to use absolute addresses, and not worry
7701 about whether the target supports cross-section
7702 arithmetic. */
a20612aa
RH
7703 else
7704 {
7705 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7706 fmt, i * 2 * DWARF2_ADDR_SIZE);
7707 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7708 }
7709
7710 fmt = NULL;
7711 }
0435c1d5
AO
7712
7713 /* Negative block_num stands for an index into ranges_by_label. */
7714 else if (block_num < 0)
7715 {
7716 int lab_idx = - block_num - 1;
7717
7718 if (!have_multiple_function_sections)
7719 {
7720 gcc_unreachable ();
7721#if 0
7722 /* If we ever use add_ranges_by_labels () for a single
7723 function section, all we have to do is to take out
7724 the #if 0 above. */
7725 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7726 ranges_by_label[lab_idx].begin,
7727 text_section_label,
7728 fmt, i * 2 * DWARF2_ADDR_SIZE);
7729 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7730 ranges_by_label[lab_idx].end,
7731 text_section_label, NULL);
7732#endif
7733 }
7734 else
7735 {
7736 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7737 ranges_by_label[lab_idx].begin,
7738 fmt, i * 2 * DWARF2_ADDR_SIZE);
7739 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7740 ranges_by_label[lab_idx].end,
7741 NULL);
7742 }
7743 }
a20612aa
RH
7744 else
7745 {
7746 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7747 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7748 fmt = start_fmt;
7749 }
7750 }
7751}
0b34cf1e
UD
7752
7753/* Data structure containing information about input files. */
7754struct file_info
7755{
d5688810
GK
7756 const char *path; /* Complete file name. */
7757 const char *fname; /* File name part. */
0b34cf1e 7758 int length; /* Length of entire string. */
d5688810 7759 struct dwarf_file_data * file_idx; /* Index in input file table. */
0b34cf1e
UD
7760 int dir_idx; /* Index in directory table. */
7761};
7762
7763/* Data structure containing information about directories with source
7764 files. */
7765struct dir_info
7766{
d5688810 7767 const char *path; /* Path including directory name. */
0b34cf1e
UD
7768 int length; /* Path length. */
7769 int prefix; /* Index of directory entry which is a prefix. */
0b34cf1e
UD
7770 int count; /* Number of files in this directory. */
7771 int dir_idx; /* Index of directory used as base. */
0b34cf1e
UD
7772};
7773
7774/* Callback function for file_info comparison. We sort by looking at
7775 the directories in the path. */
356b0698 7776
0b34cf1e 7777static int
7080f735 7778file_info_cmp (const void *p1, const void *p2)
0b34cf1e
UD
7779{
7780 const struct file_info *s1 = p1;
7781 const struct file_info *s2 = p2;
5f754896
KG
7782 const unsigned char *cp1;
7783 const unsigned char *cp2;
0b34cf1e 7784
356b0698
RK
7785 /* Take care of file names without directories. We need to make sure that
7786 we return consistent values to qsort since some will get confused if
7787 we return the same value when identical operands are passed in opposite
7788 orders. So if neither has a directory, return 0 and otherwise return
7789 1 or -1 depending on which one has the directory. */
7790 if ((s1->path == s1->fname || s2->path == s2->fname))
7791 return (s2->path == s2->fname) - (s1->path == s1->fname);
0b34cf1e 7792
5f754896
KG
7793 cp1 = (const unsigned char *) s1->path;
7794 cp2 = (const unsigned char *) s2->path;
0b34cf1e
UD
7795
7796 while (1)
7797 {
7798 ++cp1;
7799 ++cp2;
356b0698 7800 /* Reached the end of the first path? If so, handle like above. */
5f754896
KG
7801 if ((cp1 == (const unsigned char *) s1->fname)
7802 || (cp2 == (const unsigned char *) s2->fname))
7803 return ((cp2 == (const unsigned char *) s2->fname)
7804 - (cp1 == (const unsigned char *) s1->fname));
0b34cf1e
UD
7805
7806 /* Character of current path component the same? */
356b0698 7807 else if (*cp1 != *cp2)
0b34cf1e
UD
7808 return *cp1 - *cp2;
7809 }
7810}
7811
2878ea73 7812struct file_name_acquire_data
d5688810
GK
7813{
7814 struct file_info *files;
7815 int used_files;
7816 int max_files;
7817};
7818
7819/* Traversal function for the hash table. */
7820
7821static int
7822file_name_acquire (void ** slot, void *data)
7823{
7824 struct file_name_acquire_data *fnad = data;
7825 struct dwarf_file_data *d = *slot;
7826 struct file_info *fi;
7827 const char *f;
7828
7829 gcc_assert (fnad->max_files >= d->emitted_number);
7830
7831 if (! d->emitted_number)
7832 return 1;
7833
7834 gcc_assert (fnad->max_files != fnad->used_files);
7835
7836 fi = fnad->files + fnad->used_files++;
7837
7838 /* Skip all leading "./". */
7839 f = d->filename;
1d2c2b96 7840 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
d5688810 7841 f += 2;
2878ea73 7842
d5688810
GK
7843 /* Create a new array entry. */
7844 fi->path = f;
7845 fi->length = strlen (f);
7846 fi->file_idx = d;
2878ea73 7847
d5688810 7848 /* Search for the file name part. */
1d2c2b96
NF
7849 f = strrchr (f, DIR_SEPARATOR);
7850#if defined (DIR_SEPARATOR_2)
7851 {
2310f1f8 7852 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
1d2c2b96
NF
7853
7854 if (g != NULL)
7855 {
7856 if (f == NULL || f < g)
7857 f = g;
7858 }
7859 }
7860#endif
7861
d5688810
GK
7862 fi->fname = f == NULL ? fi->path : f + 1;
7863 return 1;
7864}
7865
0b34cf1e
UD
7866/* Output the directory table and the file name table. We try to minimize
7867 the total amount of memory needed. A heuristic is used to avoid large
7868 slowdowns with many input files. */
2ad9852d 7869
0b34cf1e 7870static void
7080f735 7871output_file_names (void)
0b34cf1e 7872{
d5688810
GK
7873 struct file_name_acquire_data fnad;
7874 int numfiles;
0b34cf1e
UD
7875 struct file_info *files;
7876 struct dir_info *dirs;
7877 int *saved;
7878 int *savehere;
7879 int *backmap;
d5688810 7880 int ndirs;
0b34cf1e 7881 int idx_offset;
d5688810 7882 int i;
0b34cf1e
UD
7883 int idx;
7884
d5688810 7885 if (!last_emitted_file)
f0b886ab
UW
7886 {
7887 dw2_asm_output_data (1, 0, "End directory table");
7888 dw2_asm_output_data (1, 0, "End file name table");
7889 return;
7890 }
7891
d5688810 7892 numfiles = last_emitted_file->emitted_number;
0b34cf1e 7893
d5688810
GK
7894 /* Allocate the various arrays we need. */
7895 files = alloca (numfiles * sizeof (struct file_info));
7896 dirs = alloca (numfiles * sizeof (struct dir_info));
0b34cf1e 7897
d5688810
GK
7898 fnad.files = files;
7899 fnad.used_files = 0;
7900 fnad.max_files = numfiles;
7901 htab_traverse (file_table, file_name_acquire, &fnad);
7902 gcc_assert (fnad.used_files == fnad.max_files);
2ad9852d 7903
d5688810 7904 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
0b34cf1e
UD
7905
7906 /* Find all the different directories used. */
d5688810
GK
7907 dirs[0].path = files[0].path;
7908 dirs[0].length = files[0].fname - files[0].path;
0b34cf1e 7909 dirs[0].prefix = -1;
0b34cf1e
UD
7910 dirs[0].count = 1;
7911 dirs[0].dir_idx = 0;
d5688810 7912 files[0].dir_idx = 0;
0b34cf1e
UD
7913 ndirs = 1;
7914
d5688810 7915 for (i = 1; i < numfiles; i++)
0b34cf1e
UD
7916 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7917 && memcmp (dirs[ndirs - 1].path, files[i].path,
7918 dirs[ndirs - 1].length) == 0)
7919 {
7920 /* Same directory as last entry. */
7921 files[i].dir_idx = ndirs - 1;
0b34cf1e
UD
7922 ++dirs[ndirs - 1].count;
7923 }
7924 else
7925 {
d5688810 7926 int j;
0b34cf1e
UD
7927
7928 /* This is a new directory. */
7929 dirs[ndirs].path = files[i].path;
7930 dirs[ndirs].length = files[i].fname - files[i].path;
0b34cf1e
UD
7931 dirs[ndirs].count = 1;
7932 dirs[ndirs].dir_idx = ndirs;
0b34cf1e
UD
7933 files[i].dir_idx = ndirs;
7934
7935 /* Search for a prefix. */
981975b6 7936 dirs[ndirs].prefix = -1;
2ad9852d 7937 for (j = 0; j < ndirs; j++)
981975b6
RH
7938 if (dirs[j].length < dirs[ndirs].length
7939 && dirs[j].length > 1
7940 && (dirs[ndirs].prefix == -1
7941 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7942 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7943 dirs[ndirs].prefix = j;
0b34cf1e
UD
7944
7945 ++ndirs;
7946 }
7947
2ad9852d
RK
7948 /* Now to the actual work. We have to find a subset of the directories which
7949 allow expressing the file name using references to the directory table
7950 with the least amount of characters. We do not do an exhaustive search
7951 where we would have to check out every combination of every single
7952 possible prefix. Instead we use a heuristic which provides nearly optimal
7953 results in most cases and never is much off. */
703ad42b
KG
7954 saved = alloca (ndirs * sizeof (int));
7955 savehere = alloca (ndirs * sizeof (int));
0b34cf1e
UD
7956
7957 memset (saved, '\0', ndirs * sizeof (saved[0]));
2ad9852d 7958 for (i = 0; i < ndirs; i++)
0b34cf1e 7959 {
d5688810 7960 int j;
0b34cf1e
UD
7961 int total;
7962
2ad9852d
RK
7963 /* We can always save some space for the current directory. But this
7964 does not mean it will be enough to justify adding the directory. */
0b34cf1e
UD
7965 savehere[i] = dirs[i].length;
7966 total = (savehere[i] - saved[i]) * dirs[i].count;
7967
2ad9852d 7968 for (j = i + 1; j < ndirs; j++)
0b34cf1e
UD
7969 {
7970 savehere[j] = 0;
0b34cf1e
UD
7971 if (saved[j] < dirs[i].length)
7972 {
7973 /* Determine whether the dirs[i] path is a prefix of the
7974 dirs[j] path. */
7975 int k;
7976
981975b6 7977 k = dirs[j].prefix;
c4274b22 7978 while (k != -1 && k != (int) i)
981975b6
RH
7979 k = dirs[k].prefix;
7980
c4274b22 7981 if (k == (int) i)
981975b6 7982 {
d5688810 7983 /* Yes it is. We can possibly save some memory by
981975b6
RH
7984 writing the filenames in dirs[j] relative to
7985 dirs[i]. */
7986 savehere[j] = dirs[i].length;
7987 total += (savehere[j] - saved[j]) * dirs[j].count;
7988 }
0b34cf1e
UD
7989 }
7990 }
7991
d5688810 7992 /* Check whether we can save enough to justify adding the dirs[i]
0b34cf1e
UD
7993 directory. */
7994 if (total > dirs[i].length + 1)
7995 {
981975b6 7996 /* It's worthwhile adding. */
c26fbbca 7997 for (j = i; j < ndirs; j++)
0b34cf1e
UD
7998 if (savehere[j] > 0)
7999 {
8000 /* Remember how much we saved for this directory so far. */
8001 saved[j] = savehere[j];
8002
8003 /* Remember the prefix directory. */
8004 dirs[j].dir_idx = i;
8005 }
8006 }
8007 }
8008
d5688810 8009 /* Emit the directory name table. */
0b34cf1e 8010 idx = 1;
e57cabac 8011 idx_offset = dirs[0].length > 0 ? 1 : 0;
2ad9852d 8012 for (i = 1 - idx_offset; i < ndirs; i++)
d5688810
GK
8013 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8014 "Directory Entry: 0x%x", i + idx_offset);
2ad9852d 8015
2e4b9b8c
RH
8016 dw2_asm_output_data (1, 0, "End directory table");
8017
d5688810
GK
8018 /* We have to emit them in the order of emitted_number since that's
8019 used in the debug info generation. To do this efficiently we
8020 generate a back-mapping of the indices first. */
8021 backmap = alloca (numfiles * sizeof (int));
8022 for (i = 0; i < numfiles; i++)
8023 backmap[files[i].file_idx->emitted_number - 1] = i;
0b34cf1e
UD
8024
8025 /* Now write all the file names. */
d5688810 8026 for (i = 0; i < numfiles; i++)
0b34cf1e
UD
8027 {
8028 int file_idx = backmap[i];
8029 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8030
2e4b9b8c 8031 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
d5688810 8032 "File Entry: 0x%x", (unsigned) i + 1);
0b34cf1e
UD
8033
8034 /* Include directory index. */
d5688810 8035 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
0b34cf1e
UD
8036
8037 /* Modification time. */
2e4b9b8c 8038 dw2_asm_output_data_uleb128 (0, NULL);
0b34cf1e
UD
8039
8040 /* File length in bytes. */
2e4b9b8c 8041 dw2_asm_output_data_uleb128 (0, NULL);
0b34cf1e 8042 }
2ad9852d 8043
2e4b9b8c 8044 dw2_asm_output_data (1, 0, "End file name table");
0b34cf1e
UD
8045}
8046
8047
a3f97cbb 8048/* Output the source line number correspondence information. This
14a774a9 8049 information goes into the .debug_line section. */
71dfc51f 8050
a3f97cbb 8051static void
7080f735 8052output_line_info (void)
a3f97cbb 8053{
981975b6 8054 char l1[20], l2[20], p1[20], p2[20];
a3f97cbb
JW
8055 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8056 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
b3694847
SS
8057 unsigned opc;
8058 unsigned n_op_args;
8059 unsigned long lt_index;
8060 unsigned long current_line;
8061 long line_offset;
8062 long line_delta;
8063 unsigned long current_file;
8064 unsigned long function;
71dfc51f 8065
2e4b9b8c
RH
8066 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8067 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
981975b6
RH
8068 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8069 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
71dfc51f 8070
9eb0ef7a
KB
8071 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8072 dw2_asm_output_data (4, 0xffffffff,
8073 "Initial length escape value indicating 64-bit DWARF extension");
2e4b9b8c
RH
8074 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8075 "Length of Source Line Info");
8076 ASM_OUTPUT_LABEL (asm_out_file, l1);
71dfc51f 8077
2e4b9b8c 8078 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
981975b6
RH
8079 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8080 ASM_OUTPUT_LABEL (asm_out_file, p1);
71dfc51f 8081
c1a046e5
TT
8082 /* Define the architecture-dependent minimum instruction length (in
8083 bytes). In this implementation of DWARF, this field is used for
8084 information purposes only. Since GCC generates assembly language,
8085 we have no a priori knowledge of how many instruction bytes are
8086 generated for each source line, and therefore can use only the
8087 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8088 commands. Accordingly, we fix this as `1', which is "correct
8089 enough" for all architectures, and don't let the target override. */
8090 dw2_asm_output_data (1, 1,
2e4b9b8c 8091 "Minimum Instruction Length");
c1a046e5 8092
2e4b9b8c
RH
8093 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8094 "Default is_stmt_start flag");
2e4b9b8c
RH
8095 dw2_asm_output_data (1, DWARF_LINE_BASE,
8096 "Line Base Value (Special Opcodes)");
2e4b9b8c
RH
8097 dw2_asm_output_data (1, DWARF_LINE_RANGE,
8098 "Line Range Value (Special Opcodes)");
2e4b9b8c
RH
8099 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8100 "Special Opcode Base");
71dfc51f 8101
2ad9852d 8102 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
a3f97cbb
JW
8103 {
8104 switch (opc)
8105 {
8106 case DW_LNS_advance_pc:
8107 case DW_LNS_advance_line:
8108 case DW_LNS_set_file:
8109 case DW_LNS_set_column:
8110 case DW_LNS_fixed_advance_pc:
8111 n_op_args = 1;
8112 break;
8113 default:
8114 n_op_args = 0;
8115 break;
8116 }
2e4b9b8c
RH
8117
8118 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8119 opc, n_op_args);
a3f97cbb 8120 }
71dfc51f 8121
0b34cf1e
UD
8122 /* Write out the information about the files we use. */
8123 output_file_names ();
981975b6 8124 ASM_OUTPUT_LABEL (asm_out_file, p2);
a3f97cbb 8125
2f22d404
JM
8126 /* We used to set the address register to the first location in the text
8127 section here, but that didn't accomplish anything since we already
8128 have a line note for the opening brace of the first function. */
a3f97cbb
JW
8129
8130 /* Generate the line number to PC correspondence table, encoded as
8131 a series of state machine operations. */
8132 current_file = 1;
8133 current_line = 1;
c7466dee 8134
c543ca49 8135 if (cfun && in_cold_section_p)
c7466dee 8136 strcpy (prev_line_label, cfun->cold_section_label);
87c8b4be
CT
8137 else
8138 strcpy (prev_line_label, text_section_label);
a3f97cbb
JW
8139 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8140 {
b3694847 8141 dw_line_info_ref line_info = &line_info_table[lt_index];
2f22d404 8142
10a11b75
JM
8143#if 0
8144 /* Disable this optimization for now; GDB wants to see two line notes
8145 at the beginning of a function so it can find the end of the
8146 prologue. */
8147
2f22d404 8148 /* Don't emit anything for redundant notes. Just updating the
73c68f61
SS
8149 address doesn't accomplish anything, because we already assume
8150 that anything after the last address is this line. */
2f22d404
JM
8151 if (line_info->dw_line_num == current_line
8152 && line_info->dw_file_num == current_file)
8153 continue;
10a11b75 8154#endif
71dfc51f 8155
2e4b9b8c
RH
8156 /* Emit debug info for the address of the current line.
8157
8158 Unfortunately, we have little choice here currently, and must always
2ad9852d 8159 use the most general form. GCC does not know the address delta
2e4b9b8c
RH
8160 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
8161 attributes which will give an upper bound on the address range. We
8162 could perhaps use length attributes to determine when it is safe to
8163 use DW_LNS_fixed_advance_pc. */
8164
5c90448c 8165 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
f19a6894
JW
8166 if (0)
8167 {
8168 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
2e4b9b8c
RH
8169 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8170 "DW_LNS_fixed_advance_pc");
8171 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
f19a6894
JW
8172 }
8173 else
8174 {
a1a4189d 8175 /* This can handle any delta. This takes
73c68f61 8176 4+DWARF2_ADDR_SIZE bytes. */
2e4b9b8c
RH
8177 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8178 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8179 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 8180 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
f19a6894 8181 }
2ad9852d 8182
f19a6894
JW
8183 strcpy (prev_line_label, line_label);
8184
8185 /* Emit debug info for the source file of the current line, if
8186 different from the previous line. */
a3f97cbb
JW
8187 if (line_info->dw_file_num != current_file)
8188 {
8189 current_file = line_info->dw_file_num;
2e4b9b8c 8190 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
d5688810 8191 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
a3f97cbb 8192 }
71dfc51f 8193
f19a6894
JW
8194 /* Emit debug info for the current line number, choosing the encoding
8195 that uses the least amount of space. */
2f22d404 8196 if (line_info->dw_line_num != current_line)
a3f97cbb 8197 {
2f22d404
JM
8198 line_offset = line_info->dw_line_num - current_line;
8199 line_delta = line_offset - DWARF_LINE_BASE;
8200 current_line = line_info->dw_line_num;
8201 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
2ad9852d
RK
8202 /* This can handle deltas from -10 to 234, using the current
8203 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
8204 takes 1 byte. */
8205 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8206 "line %lu", current_line);
2f22d404
JM
8207 else
8208 {
8209 /* This can handle any delta. This takes at least 4 bytes,
8210 depending on the value being encoded. */
2e4b9b8c
RH
8211 dw2_asm_output_data (1, DW_LNS_advance_line,
8212 "advance to line %lu", current_line);
8213 dw2_asm_output_data_sleb128 (line_offset, NULL);
8214 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
2f22d404 8215 }
a94dbf2c
JM
8216 }
8217 else
2ad9852d
RK
8218 /* We still need to start a new row, so output a copy insn. */
8219 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
a3f97cbb
JW
8220 }
8221
f19a6894
JW
8222 /* Emit debug info for the address of the end of the function. */
8223 if (0)
8224 {
2e4b9b8c
RH
8225 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8226 "DW_LNS_fixed_advance_pc");
8227 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
f19a6894
JW
8228 }
8229 else
8230 {
2e4b9b8c
RH
8231 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8232 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8233 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 8234 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
f19a6894 8235 }
bdb669cb 8236
2e4b9b8c
RH
8237 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8238 dw2_asm_output_data_uleb128 (1, NULL);
8239 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
e90b62db
JM
8240
8241 function = 0;
8242 current_file = 1;
8243 current_line = 1;
556273e0 8244 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
e90b62db 8245 {
b3694847 8246 dw_separate_line_info_ref line_info
e90b62db 8247 = &separate_line_info_table[lt_index];
71dfc51f 8248
10a11b75 8249#if 0
2f22d404
JM
8250 /* Don't emit anything for redundant notes. */
8251 if (line_info->dw_line_num == current_line
8252 && line_info->dw_file_num == current_file
8253 && line_info->function == function)
8254 goto cont;
10a11b75 8255#endif
2f22d404 8256
f19a6894
JW
8257 /* Emit debug info for the address of the current line. If this is
8258 a new function, or the first line of a function, then we need
8259 to handle it differently. */
5c90448c
JM
8260 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8261 lt_index);
e90b62db
JM
8262 if (function != line_info->function)
8263 {
8264 function = line_info->function;
71dfc51f 8265
f9da5064 8266 /* Set the address register to the first line in the function. */
2e4b9b8c
RH
8267 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8268 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8269 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 8270 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
e90b62db
JM
8271 }
8272 else
8273 {
f19a6894
JW
8274 /* ??? See the DW_LNS_advance_pc comment above. */
8275 if (0)
8276 {
2e4b9b8c
RH
8277 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8278 "DW_LNS_fixed_advance_pc");
8279 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
f19a6894
JW
8280 }
8281 else
8282 {
2e4b9b8c
RH
8283 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8284 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8285 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 8286 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
f19a6894 8287 }
e90b62db 8288 }
2ad9852d 8289
f19a6894 8290 strcpy (prev_line_label, line_label);
71dfc51f 8291
f19a6894
JW
8292 /* Emit debug info for the source file of the current line, if
8293 different from the previous line. */
e90b62db
JM
8294 if (line_info->dw_file_num != current_file)
8295 {
8296 current_file = line_info->dw_file_num;
2e4b9b8c 8297 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
d5688810 8298 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
e90b62db 8299 }
71dfc51f 8300
f19a6894
JW
8301 /* Emit debug info for the current line number, choosing the encoding
8302 that uses the least amount of space. */
e90b62db
JM
8303 if (line_info->dw_line_num != current_line)
8304 {
8305 line_offset = line_info->dw_line_num - current_line;
8306 line_delta = line_offset - DWARF_LINE_BASE;
8307 current_line = line_info->dw_line_num;
8308 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
2e4b9b8c
RH
8309 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8310 "line %lu", current_line);
e90b62db
JM
8311 else
8312 {
2e4b9b8c
RH
8313 dw2_asm_output_data (1, DW_LNS_advance_line,
8314 "advance to line %lu", current_line);
8315 dw2_asm_output_data_sleb128 (line_offset, NULL);
8316 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
e90b62db
JM
8317 }
8318 }
2f22d404 8319 else
2e4b9b8c 8320 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
71dfc51f 8321
10a11b75 8322#if 0
2f22d404 8323 cont:
10a11b75 8324#endif
2ad9852d
RK
8325
8326 lt_index++;
e90b62db
JM
8327
8328 /* If we're done with a function, end its sequence. */
8329 if (lt_index == separate_line_info_table_in_use
8330 || separate_line_info_table[lt_index].function != function)
8331 {
8332 current_file = 1;
8333 current_line = 1;
71dfc51f 8334
f19a6894 8335 /* Emit debug info for the address of the end of the function. */
5c90448c 8336 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
f19a6894
JW
8337 if (0)
8338 {
2e4b9b8c
RH
8339 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8340 "DW_LNS_fixed_advance_pc");
8341 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
f19a6894
JW
8342 }
8343 else
8344 {
2e4b9b8c
RH
8345 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8346 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8347 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 8348 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
f19a6894 8349 }
e90b62db
JM
8350
8351 /* Output the marker for the end of this sequence. */
2e4b9b8c
RH
8352 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8353 dw2_asm_output_data_uleb128 (1, NULL);
8354 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
e90b62db
JM
8355 }
8356 }
f19f17e0
JM
8357
8358 /* Output the marker for the end of the line number info. */
2e4b9b8c 8359 ASM_OUTPUT_LABEL (asm_out_file, l2);
a3f97cbb
JW
8360}
8361\f
a3f97cbb
JW
8362/* Given a pointer to a tree node for some base type, return a pointer to
8363 a DIE that describes the given type.
8364
8365 This routine must only be called for GCC type nodes that correspond to
8366 Dwarf base (fundamental) types. */
71dfc51f 8367
a3f97cbb 8368static dw_die_ref
7080f735 8369base_type_die (tree type)
a3f97cbb 8370{
b3694847 8371 dw_die_ref base_type_result;
b3694847 8372 enum dwarf_type encoding;
a3f97cbb 8373
2ad9852d 8374 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
a3f97cbb
JW
8375 return 0;
8376
8377 switch (TREE_CODE (type))
8378 {
a3f97cbb 8379 case INTEGER_TYPE:
05fa7d54 8380 if (TYPE_STRING_FLAG (type))
a3f97cbb 8381 {
8df83eae 8382 if (TYPE_UNSIGNED (type))
05fa7d54 8383 encoding = DW_ATE_unsigned_char;
a9d38797 8384 else
05fa7d54 8385 encoding = DW_ATE_signed_char;
a3f97cbb 8386 }
05fa7d54
RS
8387 else if (TYPE_UNSIGNED (type))
8388 encoding = DW_ATE_unsigned;
a9d38797 8389 else
05fa7d54 8390 encoding = DW_ATE_signed;
a3f97cbb
JW
8391 break;
8392
8393 case REAL_TYPE:
15ed7b52
JG
8394 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8395 encoding = DW_ATE_decimal_float;
8396 else
8397 encoding = DW_ATE_float;
a3f97cbb
JW
8398 break;
8399
405f63da
MM
8400 /* Dwarf2 doesn't know anything about complex ints, so use
8401 a user defined type for it. */
a3f97cbb 8402 case COMPLEX_TYPE:
405f63da
MM
8403 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8404 encoding = DW_ATE_complex_float;
8405 else
8406 encoding = DW_ATE_lo_user;
a3f97cbb
JW
8407 break;
8408
8409 case BOOLEAN_TYPE:
a9d38797
JM
8410 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8411 encoding = DW_ATE_boolean;
a3f97cbb
JW
8412 break;
8413
8414 default:
2ad9852d 8415 /* No other TREE_CODEs are Dwarf fundamental types. */
ced3f397 8416 gcc_unreachable ();
a3f97cbb
JW
8417 }
8418
54ba1f0d 8419 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
14a774a9 8420
7cdfcf60
GK
8421 /* This probably indicates a bug. */
8422 if (! TYPE_NAME (type))
8423 add_name_attribute (base_type_result, "__unknown__");
8424
a9d38797 8425 add_AT_unsigned (base_type_result, DW_AT_byte_size,
4e5a8d7b 8426 int_size_in_bytes (type));
a9d38797 8427 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
a3f97cbb
JW
8428
8429 return base_type_result;
8430}
8431
cc2902df 8432/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
a3f97cbb 8433 given input type is a Dwarf "fundamental" type. Otherwise return null. */
71dfc51f
RK
8434
8435static inline int
7080f735 8436is_base_type (tree type)
a3f97cbb
JW
8437{
8438 switch (TREE_CODE (type))
8439 {
8440 case ERROR_MARK:
8441 case VOID_TYPE:
8442 case INTEGER_TYPE:
8443 case REAL_TYPE:
8444 case COMPLEX_TYPE:
8445 case BOOLEAN_TYPE:
a3f97cbb
JW
8446 return 1;
8447
a3f97cbb
JW
8448 case ARRAY_TYPE:
8449 case RECORD_TYPE:
8450 case UNION_TYPE:
8451 case QUAL_UNION_TYPE:
8452 case ENUMERAL_TYPE:
8453 case FUNCTION_TYPE:
8454 case METHOD_TYPE:
8455 case POINTER_TYPE:
8456 case REFERENCE_TYPE:
a3f97cbb
JW
8457 case OFFSET_TYPE:
8458 case LANG_TYPE:
604bb87d 8459 case VECTOR_TYPE:
a3f97cbb
JW
8460 return 0;
8461
8462 default:
ced3f397 8463 gcc_unreachable ();
a3f97cbb 8464 }
71dfc51f 8465
a3f97cbb
JW
8466 return 0;
8467}
8468
4977bab6
ZW
8469/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8470 node, return the size in bits for the type if it is a constant, or else
8471 return the alignment for the type if the type's size is not constant, or
8472 else return BITS_PER_WORD if the type actually turns out to be an
8473 ERROR_MARK node. */
8474
8475static inline unsigned HOST_WIDE_INT
7080f735 8476simple_type_size_in_bits (tree type)
4977bab6 8477{
4977bab6
ZW
8478 if (TREE_CODE (type) == ERROR_MARK)
8479 return BITS_PER_WORD;
8480 else if (TYPE_SIZE (type) == NULL_TREE)
8481 return 0;
8482 else if (host_integerp (TYPE_SIZE (type), 1))
8483 return tree_low_cst (TYPE_SIZE (type), 1);
8484 else
8485 return TYPE_ALIGN (type);
8486}
8487
c3cdeef4
JB
8488/* Return true if the debug information for the given type should be
8489 emitted as a subrange type. */
8490
8491static inline bool
e7d23ce3
B
8492is_subrange_type (tree type)
8493{
de99511b
B
8494 tree subtype = TREE_TYPE (type);
8495
886de2d4
JB
8496 /* Subrange types are identified by the fact that they are integer
8497 types, and that they have a subtype which is either an integer type
8498 or an enumeral type. */
8499
8500 if (TREE_CODE (type) != INTEGER_TYPE
8501 || subtype == NULL_TREE)
8502 return false;
8503
8504 if (TREE_CODE (subtype) != INTEGER_TYPE
8505 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8506 return false;
8507
d6672e91
JB
8508 if (TREE_CODE (type) == TREE_CODE (subtype)
8509 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8510 && TYPE_MIN_VALUE (type) != NULL
8511 && TYPE_MIN_VALUE (subtype) != NULL
8512 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8513 && TYPE_MAX_VALUE (type) != NULL
8514 && TYPE_MAX_VALUE (subtype) != NULL
8515 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8516 {
8517 /* The type and its subtype have the same representation. If in
2878ea73
MS
8518 addition the two types also have the same name, then the given
8519 type is not a subrange type, but rather a plain base type. */
d6672e91 8520 /* FIXME: brobecker/2004-03-22:
2878ea73
MS
8521 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8522 therefore be sufficient to check the TYPE_SIZE node pointers
8523 rather than checking the actual size. Unfortunately, we have
8524 found some cases, such as in the Ada "integer" type, where
8525 this is not the case. Until this problem is solved, we need to
8526 keep checking the actual size. */
d6672e91
JB
8527 tree type_name = TYPE_NAME (type);
8528 tree subtype_name = TYPE_NAME (subtype);
8529
8530 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
2878ea73 8531 type_name = DECL_NAME (type_name);
d6672e91
JB
8532
8533 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
2878ea73 8534 subtype_name = DECL_NAME (subtype_name);
d6672e91
JB
8535
8536 if (type_name == subtype_name)
2878ea73 8537 return false;
d6672e91
JB
8538 }
8539
886de2d4 8540 return true;
c3cdeef4
JB
8541}
8542
8543/* Given a pointer to a tree node for a subrange type, return a pointer
8544 to a DIE that describes the given type. */
8545
8546static dw_die_ref
fbfd77b8 8547subrange_type_die (tree type, dw_die_ref context_die)
c3cdeef4 8548{
c3cdeef4 8549 dw_die_ref subrange_die;
e7d23ce3 8550 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
7080f735 8551
fbfd77b8
JB
8552 if (context_die == NULL)
8553 context_die = comp_unit_die;
8554
fbfd77b8 8555 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
b98d154e 8556
7cdfcf60 8557 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
e7d23ce3
B
8558 {
8559 /* The size of the subrange type and its base type do not match,
2878ea73 8560 so we need to generate a size attribute for the subrange type. */
e7d23ce3
B
8561 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8562 }
8563
c3cdeef4
JB
8564 if (TYPE_MIN_VALUE (type) != NULL)
8565 add_bound_info (subrange_die, DW_AT_lower_bound,
2878ea73 8566 TYPE_MIN_VALUE (type));
c3cdeef4
JB
8567 if (TYPE_MAX_VALUE (type) != NULL)
8568 add_bound_info (subrange_die, DW_AT_upper_bound,
2878ea73 8569 TYPE_MAX_VALUE (type));
c3cdeef4
JB
8570
8571 return subrange_die;
8572}
8573
a3f97cbb
JW
8574/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8575 entry that chains various modifiers in front of the given type. */
71dfc51f 8576
a3f97cbb 8577static dw_die_ref
7080f735
AJ
8578modified_type_die (tree type, int is_const_type, int is_volatile_type,
8579 dw_die_ref context_die)
a3f97cbb 8580{
b3694847 8581 enum tree_code code = TREE_CODE (type);
7cdfcf60 8582 dw_die_ref mod_type_die;
b3694847
SS
8583 dw_die_ref sub_die = NULL;
8584 tree item_type = NULL;
7cdfcf60
GK
8585 tree qualified_type;
8586 tree name;
8587
8588 if (code == ERROR_MARK)
8589 return NULL;
8590
8591 /* See if we already have the appropriately qualified variant of
8592 this type. */
8593 qualified_type
8594 = get_qualified_type (type,
8595 ((is_const_type ? TYPE_QUAL_CONST : 0)
8596 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
2878ea73 8597
7cdfcf60
GK
8598 /* If we do, then we can just use its DIE, if it exists. */
8599 if (qualified_type)
a3f97cbb 8600 {
7cdfcf60 8601 mod_type_die = lookup_type_die (qualified_type);
a94dbf2c 8602 if (mod_type_die)
7cdfcf60
GK
8603 return mod_type_die;
8604 }
2878ea73 8605
7cdfcf60 8606 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
2878ea73 8607
7cdfcf60
GK
8608 /* Handle C typedef types. */
8609 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8610 {
8611 tree dtype = TREE_TYPE (name);
2878ea73 8612
7cdfcf60 8613 if (qualified_type == dtype)
a3f97cbb 8614 {
7cdfcf60
GK
8615 /* For a named type, use the typedef. */
8616 gen_type_die (qualified_type, context_die);
8617 return lookup_type_die (qualified_type);
a3f97cbb 8618 }
792eaee2
AO
8619 else if (is_const_type < TYPE_READONLY (dtype)
8620 || is_volatile_type < TYPE_VOLATILE (dtype)
8621 || (is_const_type <= TYPE_READONLY (dtype)
8622 && is_volatile_type <= TYPE_VOLATILE (dtype)
8623 && DECL_ORIGINAL_TYPE (name) != type))
7cdfcf60
GK
8624 /* cv-unqualified version of named type. Just use the unnamed
8625 type to which it refers. */
8626 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8627 is_const_type, is_volatile_type,
8628 context_die);
8629 /* Else cv-qualified version of named type; fall through. */
8630 }
2878ea73 8631
7cdfcf60
GK
8632 if (is_const_type)
8633 {
8634 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8635 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8636 }
8637 else if (is_volatile_type)
8638 {
8639 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8640 sub_die = modified_type_die (type, 0, 0, context_die);
8641 }
8642 else if (code == POINTER_TYPE)
8643 {
8644 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8645 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8646 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8647 item_type = TREE_TYPE (type);
8648 }
8649 else if (code == REFERENCE_TYPE)
8650 {
8651 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8652 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8653 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8654 item_type = TREE_TYPE (type);
8655 }
8656 else if (is_subrange_type (type))
8657 {
8658 mod_type_die = subrange_type_die (type, context_die);
8659 item_type = TREE_TYPE (type);
8660 }
8661 else if (is_base_type (type))
8662 mod_type_die = base_type_die (type);
8663 else
8664 {
8665 gen_type_die (type, context_die);
2878ea73 8666
7cdfcf60
GK
8667 /* We have to get the type_main_variant here (and pass that to the
8668 `lookup_type_die' routine) because the ..._TYPE node we have
8669 might simply be a *copy* of some original type node (where the
8670 copy was created to help us keep track of typedef names) and
8671 that copy might have a different TYPE_UID from the original
8672 ..._TYPE node. */
8673 if (TREE_CODE (type) != VECTOR_TYPE)
8674 return lookup_type_die (type_main_variant (type));
a3f97cbb 8675 else
7cdfcf60
GK
8676 /* Vectors have the debugging information in the type,
8677 not the main variant. */
8678 return lookup_type_die (type);
8679 }
2878ea73 8680
7cdfcf60
GK
8681 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8682 don't output a DW_TAG_typedef, since there isn't one in the
8683 user's program; just attach a DW_AT_name to the type. */
8684 if (name
8685 && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8686 {
8687 if (TREE_CODE (name) == TYPE_DECL)
8688 /* Could just call add_name_and_src_coords_attributes here,
8689 but since this is a builtin type it doesn't have any
8690 useful source coordinates anyway. */
8691 name = DECL_NAME (name);
8692 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
a3f97cbb 8693 }
2878ea73 8694
7cdfcf60
GK
8695 if (qualified_type)
8696 equate_type_number_to_die (qualified_type, mod_type_die);
71dfc51f 8697
dfcf9891 8698 if (item_type)
71dfc51f
RK
8699 /* We must do this after the equate_type_number_to_die call, in case
8700 this is a recursive type. This ensures that the modified_type_die
8701 recursion will terminate even if the type is recursive. Recursive
8702 types are possible in Ada. */
8703 sub_die = modified_type_die (item_type,
8704 TYPE_READONLY (item_type),
8705 TYPE_VOLATILE (item_type),
8706 context_die);
8707
a3f97cbb 8708 if (sub_die != NULL)
71dfc51f
RK
8709 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8710
a3f97cbb
JW
8711 return mod_type_die;
8712}
8713
a3f97cbb 8714/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
6d2f8887 8715 an enumerated type. */
71dfc51f
RK
8716
8717static inline int
7080f735 8718type_is_enum (tree type)
a3f97cbb
JW
8719{
8720 return TREE_CODE (type) == ENUMERAL_TYPE;
8721}
8722
23959f19 8723/* Return the DBX register number described by a given RTL node. */
7d9d8943
AM
8724
8725static unsigned int
23959f19 8726dbx_reg_number (rtx rtl)
7d9d8943 8727{
b3694847 8728 unsigned regno = REGNO (rtl);
7d9d8943 8729
ced3f397 8730 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
7d9d8943 8731
30e6f306 8732#ifdef LEAF_REG_REMAP
ea049a41
EB
8733 if (current_function_uses_only_leaf_regs)
8734 {
8735 int leaf_reg = LEAF_REG_REMAP (regno);
8736 if (leaf_reg != -1)
8737 regno = (unsigned) leaf_reg;
8738 }
30e6f306
RH
8739#endif
8740
e7af1d45 8741 return DBX_REGISTER_NUMBER (regno);
7d9d8943
AM
8742}
8743
216448c7
EB
8744/* Optionally add a DW_OP_piece term to a location description expression.
8745 DW_OP_piece is only added if the location description expression already
8746 doesn't end with DW_OP_piece. */
8747
8748static void
8749add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8750{
8751 dw_loc_descr_ref loc;
8752
8753 if (*list_head != NULL)
8754 {
8755 /* Find the end of the chain. */
8756 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8757 ;
8758
8759 if (loc->dw_loc_opc != DW_OP_piece)
8760 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8761 }
8762}
8763
e7af1d45 8764/* Return a location descriptor that designates a machine register or
96714395 8765 zero if there is none. */
71dfc51f 8766
a3f97cbb 8767static dw_loc_descr_ref
62760ffd 8768reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
a3f97cbb 8769{
96714395 8770 rtx regs;
71dfc51f 8771
e7af1d45
RK
8772 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8773 return 0;
8774
5fd9b178 8775 regs = targetm.dwarf_register_span (rtl);
96714395 8776
30e6f306 8777 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
62760ffd 8778 return multiple_reg_loc_descriptor (rtl, regs, initialized);
96714395 8779 else
62760ffd 8780 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
96714395
AH
8781}
8782
8783/* Return a location descriptor that designates a machine register for
8784 a given hard register number. */
8785
8786static dw_loc_descr_ref
62760ffd 8787one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
96714395 8788{
62760ffd 8789 dw_loc_descr_ref reg_loc_descr;
96714395 8790 if (regno <= 31)
62760ffd 8791 reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
d22c2324 8792 else
62760ffd
CT
8793 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8794
8795 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8796 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8797
8798 return reg_loc_descr;
96714395
AH
8799}
8800
8801/* Given an RTL of a register, return a location descriptor that
8802 designates a value that spans more than one register. */
8803
8804static dw_loc_descr_ref
62760ffd
CT
8805multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8806 enum var_init_status initialized)
96714395
AH
8807{
8808 int nregs, size, i;
8809 unsigned reg;
8810 dw_loc_descr_ref loc_result = NULL;
71dfc51f 8811
f9918968
JJ
8812 reg = REGNO (rtl);
8813#ifdef LEAF_REG_REMAP
ea049a41
EB
8814 if (current_function_uses_only_leaf_regs)
8815 {
8816 int leaf_reg = LEAF_REG_REMAP (reg);
8817 if (leaf_reg != -1)
8818 reg = (unsigned) leaf_reg;
8819 }
f9918968
JJ
8820#endif
8821 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
23959f19 8822 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
96714395
AH
8823
8824 /* Simple, contiguous registers. */
8825 if (regs == NULL_RTX)
8826 {
8827 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8828
8829 loc_result = NULL;
8830 while (nregs--)
8831 {
8832 dw_loc_descr_ref t;
8833
62760ffd
CT
8834 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8835 VAR_INIT_STATUS_INITIALIZED);
96714395 8836 add_loc_descr (&loc_result, t);
c938250d 8837 add_loc_descr_op_piece (&loc_result, size);
31ca3635 8838 ++reg;
96714395
AH
8839 }
8840 return loc_result;
8841 }
8842
8843 /* Now onto stupid register sets in non contiguous locations. */
8844
ced3f397 8845 gcc_assert (GET_CODE (regs) == PARALLEL);
96714395
AH
8846
8847 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8848 loc_result = NULL;
8849
8850 for (i = 0; i < XVECLEN (regs, 0); ++i)
8851 {
8852 dw_loc_descr_ref t;
8853
62760ffd
CT
8854 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8855 VAR_INIT_STATUS_INITIALIZED);
96714395
AH
8856 add_loc_descr (&loc_result, t);
8857 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
c938250d 8858 add_loc_descr_op_piece (&loc_result, size);
96714395 8859 }
62760ffd
CT
8860
8861 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8862 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
a3f97cbb
JW
8863 return loc_result;
8864}
8865
d8041cc8
RH
8866/* Return a location descriptor that designates a constant. */
8867
8868static dw_loc_descr_ref
7080f735 8869int_loc_descriptor (HOST_WIDE_INT i)
d8041cc8
RH
8870{
8871 enum dwarf_location_atom op;
8872
8873 /* Pick the smallest representation of a constant, rather than just
8874 defaulting to the LEB encoding. */
8875 if (i >= 0)
8876 {
8877 if (i <= 31)
8878 op = DW_OP_lit0 + i;
8879 else if (i <= 0xff)
8880 op = DW_OP_const1u;
8881 else if (i <= 0xffff)
8882 op = DW_OP_const2u;
8883 else if (HOST_BITS_PER_WIDE_INT == 32
8884 || i <= 0xffffffff)
8885 op = DW_OP_const4u;
8886 else
8887 op = DW_OP_constu;
8888 }
8889 else
8890 {
8891 if (i >= -0x80)
8892 op = DW_OP_const1s;
8893 else if (i >= -0x8000)
8894 op = DW_OP_const2s;
8895 else if (HOST_BITS_PER_WIDE_INT == 32
8896 || i >= -0x80000000)
8897 op = DW_OP_const4s;
8898 else
8899 op = DW_OP_consts;
8900 }
8901
8902 return new_loc_descr (op, i, 0);
8903}
8904
a3f97cbb 8905/* Return a location descriptor that designates a base+offset location. */
71dfc51f 8906
a3f97cbb 8907static dw_loc_descr_ref
62760ffd
CT
8908based_loc_descr (rtx reg, HOST_WIDE_INT offset,
8909 enum var_init_status initialized)
a3f97cbb 8910{
f6672e8e 8911 unsigned int regno;
62760ffd 8912 dw_loc_descr_ref result;
30e6f306
RH
8913
8914 /* We only use "frame base" when we're sure we're talking about the
8915 post-prologue local stack frame. We do this by *not* running
8916 register elimination until this point, and recognizing the special
8917 argument pointer and soft frame pointer rtx's. */
8918 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8919 {
f6672e8e 8920 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
30e6f306 8921
f6672e8e
RH
8922 if (elim != reg)
8923 {
8924 if (GET_CODE (elim) == PLUS)
8925 {
8926 offset += INTVAL (XEXP (elim, 1));
8927 elim = XEXP (elim, 0);
8928 }
8929 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8930 : stack_pointer_rtx));
2878ea73 8931 offset += frame_pointer_fb_offset;
30e6f306 8932
2878ea73 8933 return new_loc_descr (DW_OP_fbreg, offset, 0);
f6672e8e 8934 }
30e6f306 8935 }
71dfc51f 8936
f6672e8e
RH
8937 regno = dbx_reg_number (reg);
8938 if (regno <= 31)
62760ffd 8939 result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
f6672e8e 8940 else
62760ffd
CT
8941 result = new_loc_descr (DW_OP_bregx, regno, offset);
8942
8943 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8944 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8945
8946 return result;
a3f97cbb
JW
8947}
8948
8949/* Return true if this RTL expression describes a base+offset calculation. */
71dfc51f
RK
8950
8951static inline int
7080f735 8952is_based_loc (rtx rtl)
a3f97cbb 8953{
173bf5be 8954 return (GET_CODE (rtl) == PLUS
f8cfc6aa 8955 && ((REG_P (XEXP (rtl, 0))
173bf5be
KH
8956 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8957 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
a3f97cbb
JW
8958}
8959
0c5c188f
ILT
8960/* Return a descriptor that describes the concatenation of N locations
8961 used to form the address of a memory location. */
8962
8963static dw_loc_descr_ref
62760ffd
CT
8964concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
8965 enum var_init_status initialized)
0c5c188f
ILT
8966{
8967 unsigned int i;
8968 dw_loc_descr_ref cc_loc_result = NULL;
8969 unsigned int n = XVECLEN (concatn, 0);
8970
8971 for (i = 0; i < n; ++i)
8972 {
8973 dw_loc_descr_ref ref;
8974 rtx x = XVECEXP (concatn, 0, i);
8975
62760ffd 8976 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
0c5c188f
ILT
8977 if (ref == NULL)
8978 return NULL;
8979
8980 add_loc_descr (&cc_loc_result, ref);
8981 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
8982 }
8983
62760ffd
CT
8984 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8985 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8986
0c5c188f
ILT
8987 return cc_loc_result;
8988}
8989
a3f97cbb
JW
8990/* The following routine converts the RTL for a variable or parameter
8991 (resident in memory) into an equivalent Dwarf representation of a
8992 mechanism for getting the address of that same variable onto the top of a
8993 hypothetical "address evaluation" stack.
71dfc51f 8994
a3f97cbb
JW
8995 When creating memory location descriptors, we are effectively transforming
8996 the RTL for a memory-resident object into its Dwarf postfix expression
8997 equivalent. This routine recursively descends an RTL tree, turning
e60d4d7b
JL
8998 it into Dwarf postfix code as it goes.
8999
9000 MODE is the mode of the memory reference, needed to handle some
e7af1d45
RK
9001 autoincrement addressing modes.
9002
30e6f306
RH
9003 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9004 location list for RTL.
0a2d3d69 9005
e7af1d45 9006 Return 0 if we can't represent the location. */
71dfc51f 9007
a3f97cbb 9008static dw_loc_descr_ref
62760ffd
CT
9009mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9010 enum var_init_status initialized)
a3f97cbb
JW
9011{
9012 dw_loc_descr_ref mem_loc_result = NULL;
40f0b3ee 9013 enum dwarf_location_atom op;
e7af1d45 9014
556273e0 9015 /* Note that for a dynamically sized array, the location we will generate a
a3f97cbb
JW
9016 description of here will be the lowest numbered location which is
9017 actually within the array. That's *not* necessarily the same as the
9018 zeroth element of the array. */
71dfc51f 9019
5fd9b178 9020 rtl = targetm.delegitimize_address (rtl);
1865dbb5 9021
a3f97cbb
JW
9022 switch (GET_CODE (rtl))
9023 {
e60d4d7b
JL
9024 case POST_INC:
9025 case POST_DEC:
e2134eea 9026 case POST_MODIFY:
e60d4d7b
JL
9027 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
9028 just fall into the SUBREG code. */
9029
2ad9852d 9030 /* ... fall through ... */
e60d4d7b 9031
a3f97cbb
JW
9032 case SUBREG:
9033 /* The case of a subreg may arise when we have a local (register)
73c68f61
SS
9034 variable or a formal (register) parameter which doesn't quite fill
9035 up an entire register. For now, just assume that it is
9036 legitimate to make the Dwarf info refer to the whole register which
9037 contains the given subreg. */
75735872 9038 rtl = XEXP (rtl, 0);
71dfc51f 9039
2ad9852d 9040 /* ... fall through ... */
a3f97cbb
JW
9041
9042 case REG:
9043 /* Whenever a register number forms a part of the description of the
73c68f61
SS
9044 method for calculating the (dynamic) address of a memory resident
9045 object, DWARF rules require the register number be referred to as
9046 a "base register". This distinction is not based in any way upon
9047 what category of register the hardware believes the given register
9048 belongs to. This is strictly DWARF terminology we're dealing with
9049 here. Note that in cases where the location of a memory-resident
9050 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9051 OP_CONST (0)) the actual DWARF location descriptor that we generate
9052 may just be OP_BASEREG (basereg). This may look deceptively like
9053 the object in question was allocated to a register (rather than in
9054 memory) so DWARF consumers need to be aware of the subtle
9055 distinction between OP_REG and OP_BASEREG. */
e7af1d45 9056 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
62760ffd 9057 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
a3f97cbb
JW
9058 break;
9059
9060 case MEM:
62760ffd
CT
9061 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9062 VAR_INIT_STATUS_INITIALIZED);
e7af1d45
RK
9063 if (mem_loc_result != 0)
9064 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
a3f97cbb
JW
9065 break;
9066
1ce324c3
EB
9067 case LO_SUM:
9068 rtl = XEXP (rtl, 1);
9069
9070 /* ... fall through ... */
9071
d8041cc8
RH
9072 case LABEL_REF:
9073 /* Some ports can transform a symbol ref into a label ref, because
7080f735
AJ
9074 the symbol ref is too far away and has to be dumped into a constant
9075 pool. */
a3f97cbb
JW
9076 case CONST:
9077 case SYMBOL_REF:
6331d1c1 9078 /* Alternatively, the symbol in the constant pool might be referenced
c6f9b9a1 9079 by a different symbol. */
2ad9852d 9080 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
79cdfa4b 9081 {
149d6f9e
JJ
9082 bool marked;
9083 rtx tmp = get_pool_constant_mark (rtl, &marked);
2ad9852d 9084
6331d1c1 9085 if (GET_CODE (tmp) == SYMBOL_REF)
149d6f9e
JJ
9086 {
9087 rtl = tmp;
9088 if (CONSTANT_POOL_ADDRESS_P (tmp))
9089 get_pool_constant_mark (tmp, &marked);
9090 else
9091 marked = true;
9092 }
9093
9094 /* If all references to this pool constant were optimized away,
9095 it was not output and thus we can't represent it.
9096 FIXME: might try to use DW_OP_const_value here, though
9097 DW_OP_piece complicates it. */
9098 if (!marked)
9099 return 0;
79cdfa4b
TM
9100 }
9101
a3f97cbb
JW
9102 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9103 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
c470afad 9104 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
a1bbd445 9105 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
a3f97cbb
JW
9106 break;
9107
e2134eea
JH
9108 case PRE_MODIFY:
9109 /* Extract the PLUS expression nested inside and fall into
73c68f61 9110 PLUS code below. */
e2134eea
JH
9111 rtl = XEXP (rtl, 1);
9112 goto plus;
9113
e60d4d7b
JL
9114 case PRE_INC:
9115 case PRE_DEC:
9116 /* Turn these into a PLUS expression and fall into the PLUS code
9117 below. */
9118 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9119 GEN_INT (GET_CODE (rtl) == PRE_INC
556273e0
KH
9120 ? GET_MODE_UNIT_SIZE (mode)
9121 : -GET_MODE_UNIT_SIZE (mode)));
9122
2ad9852d 9123 /* ... fall through ... */
e60d4d7b 9124
a3f97cbb 9125 case PLUS:
e2134eea 9126 plus:
a3f97cbb 9127 if (is_based_loc (rtl))
30e6f306 9128 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
62760ffd
CT
9129 INTVAL (XEXP (rtl, 1)),
9130 VAR_INIT_STATUS_INITIALIZED);
a3f97cbb
JW
9131 else
9132 {
62760ffd
CT
9133 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9134 VAR_INIT_STATUS_INITIALIZED);
e7af1d45
RK
9135 if (mem_loc_result == 0)
9136 break;
d8041cc8
RH
9137
9138 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9139 && INTVAL (XEXP (rtl, 1)) >= 0)
e7af1d45
RK
9140 add_loc_descr (&mem_loc_result,
9141 new_loc_descr (DW_OP_plus_uconst,
9142 INTVAL (XEXP (rtl, 1)), 0));
d8041cc8
RH
9143 else
9144 {
9145 add_loc_descr (&mem_loc_result,
62760ffd
CT
9146 mem_loc_descriptor (XEXP (rtl, 1), mode,
9147 VAR_INIT_STATUS_INITIALIZED));
d8041cc8
RH
9148 add_loc_descr (&mem_loc_result,
9149 new_loc_descr (DW_OP_plus, 0, 0));
9150 }
a3f97cbb
JW
9151 }
9152 break;
9153
40f0b3ee
PB
9154 /* If a pseudo-reg is optimized away, it is possible for it to
9155 be replaced with a MEM containing a multiply or shift. */
dd2478ae 9156 case MULT:
40f0b3ee
PB
9157 op = DW_OP_mul;
9158 goto do_binop;
9159
9160 case ASHIFT:
9161 op = DW_OP_shl;
9162 goto do_binop;
a1c496cb 9163
40f0b3ee
PB
9164 case ASHIFTRT:
9165 op = DW_OP_shra;
9166 goto do_binop;
9167
9168 case LSHIFTRT:
9169 op = DW_OP_shr;
9170 goto do_binop;
9171
9172 do_binop:
e7af1d45 9173 {
62760ffd
CT
9174 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9175 VAR_INIT_STATUS_INITIALIZED);
9176 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9177 VAR_INIT_STATUS_INITIALIZED);
e7af1d45
RK
9178
9179 if (op0 == 0 || op1 == 0)
9180 break;
9181
9182 mem_loc_result = op0;
9183 add_loc_descr (&mem_loc_result, op1);
40f0b3ee 9184 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
e7af1d45
RK
9185 break;
9186 }
dd2478ae 9187
a3f97cbb 9188 case CONST_INT:
d8041cc8 9189 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
a3f97cbb
JW
9190 break;
9191
0c5c188f 9192 case CONCATN:
62760ffd
CT
9193 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9194 VAR_INIT_STATUS_INITIALIZED);
0c5c188f
ILT
9195 break;
9196
a3f97cbb 9197 default:
ced3f397 9198 gcc_unreachable ();
a3f97cbb 9199 }
71dfc51f 9200
62760ffd
CT
9201 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9202 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9203
a3f97cbb
JW
9204 return mem_loc_result;
9205}
9206
956d6950 9207/* Return a descriptor that describes the concatenation of two locations.
4401bf24
JL
9208 This is typically a complex variable. */
9209
9210static dw_loc_descr_ref
62760ffd 9211concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
4401bf24
JL
9212{
9213 dw_loc_descr_ref cc_loc_result = NULL;
62760ffd
CT
9214 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9215 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
4401bf24 9216
e7af1d45
RK
9217 if (x0_ref == 0 || x1_ref == 0)
9218 return 0;
9219
9220 cc_loc_result = x0_ref;
c938250d 9221 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
4401bf24 9222
e7af1d45 9223 add_loc_descr (&cc_loc_result, x1_ref);
c938250d 9224 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
4401bf24 9225
62760ffd
CT
9226 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9227 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9228
4401bf24
JL
9229 return cc_loc_result;
9230}
9231
e53a16e7
ILT
9232/* Return a descriptor that describes the concatenation of N
9233 locations. */
9234
9235static dw_loc_descr_ref
62760ffd 9236concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
e53a16e7
ILT
9237{
9238 unsigned int i;
9239 dw_loc_descr_ref cc_loc_result = NULL;
9240 unsigned int n = XVECLEN (concatn, 0);
9241
9242 for (i = 0; i < n; ++i)
9243 {
9244 dw_loc_descr_ref ref;
9245 rtx x = XVECEXP (concatn, 0, i);
9246
62760ffd 9247 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
e53a16e7
ILT
9248 if (ref == NULL)
9249 return NULL;
9250
9251 add_loc_descr (&cc_loc_result, ref);
9252 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9253 }
9254
62760ffd
CT
9255 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9256 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9257
e53a16e7
ILT
9258 return cc_loc_result;
9259}
9260
a3f97cbb
JW
9261/* Output a proper Dwarf location descriptor for a variable or parameter
9262 which is either allocated in a register or in a memory location. For a
9263 register, we just generate an OP_REG and the register number. For a
9264 memory location we provide a Dwarf postfix expression describing how to
e7af1d45
RK
9265 generate the (dynamic) address of the object onto the address stack.
9266
9267 If we don't know how to describe it, return 0. */
71dfc51f 9268
a3f97cbb 9269static dw_loc_descr_ref
62760ffd 9270loc_descriptor (rtx rtl, enum var_init_status initialized)
a3f97cbb
JW
9271{
9272 dw_loc_descr_ref loc_result = NULL;
e7af1d45 9273
a3f97cbb
JW
9274 switch (GET_CODE (rtl))
9275 {
9276 case SUBREG:
a3f97cbb 9277 /* The case of a subreg may arise when we have a local (register)
73c68f61
SS
9278 variable or a formal (register) parameter which doesn't quite fill
9279 up an entire register. For now, just assume that it is
9280 legitimate to make the Dwarf info refer to the whole register which
9281 contains the given subreg. */
ddef6bc7 9282 rtl = SUBREG_REG (rtl);
71dfc51f 9283
2ad9852d 9284 /* ... fall through ... */
a3f97cbb
JW
9285
9286 case REG:
62760ffd 9287 loc_result = reg_loc_descriptor (rtl, initialized);
a3f97cbb
JW
9288 break;
9289
9290 case MEM:
62760ffd
CT
9291 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9292 initialized);
a3f97cbb
JW
9293 break;
9294
4401bf24 9295 case CONCAT:
62760ffd
CT
9296 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9297 initialized);
4401bf24
JL
9298 break;
9299
e53a16e7 9300 case CONCATN:
62760ffd 9301 loc_result = concatn_loc_descriptor (rtl, initialized);
e53a16e7
ILT
9302 break;
9303
0a2d3d69
DB
9304 case VAR_LOCATION:
9305 /* Single part. */
9306 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9307 {
62760ffd 9308 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
1a186ec5 9309 break;
0a2d3d69 9310 }
0a2d3d69 9311
1a186ec5
RH
9312 rtl = XEXP (rtl, 1);
9313 /* FALLTHRU */
0a2d3d69 9314
1a186ec5
RH
9315 case PARALLEL:
9316 {
9317 rtvec par_elems = XVEC (rtl, 0);
9318 int num_elem = GET_NUM_ELEM (par_elems);
9319 enum machine_mode mode;
9320 int i;
9321
9322 /* Create the first one, so we have something to add to. */
62760ffd
CT
9323 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9324 initialized);
1a186ec5 9325 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
c938250d 9326 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
1a186ec5
RH
9327 for (i = 1; i < num_elem; i++)
9328 {
9329 dw_loc_descr_ref temp;
9330
62760ffd
CT
9331 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9332 initialized);
1a186ec5
RH
9333 add_loc_descr (&loc_result, temp);
9334 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
c938250d 9335 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
1a186ec5
RH
9336 }
9337 }
0a2d3d69
DB
9338 break;
9339
a3f97cbb 9340 default:
ced3f397 9341 gcc_unreachable ();
a3f97cbb 9342 }
71dfc51f 9343
a3f97cbb
JW
9344 return loc_result;
9345}
9346
2ad9852d 9347/* Similar, but generate the descriptor from trees instead of rtl. This comes
1a186ec5
RH
9348 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9349 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9350 top-level invocation, and we require the address of LOC; is 0 if we require
9351 the value of LOC. */
d8041cc8
RH
9352
9353static dw_loc_descr_ref
1a186ec5 9354loc_descriptor_from_tree_1 (tree loc, int want_address)
d8041cc8 9355{
e7af1d45 9356 dw_loc_descr_ref ret, ret1;
1a186ec5 9357 int have_address = 0;
d8041cc8
RH
9358 enum dwarf_location_atom op;
9359
9360 /* ??? Most of the time we do not take proper care for sign/zero
9361 extending the values properly. Hopefully this won't be a real
9362 problem... */
9363
9364 switch (TREE_CODE (loc))
9365 {
9366 case ERROR_MARK:
e7af1d45 9367 return 0;
d8041cc8 9368
e7af1d45 9369 case PLACEHOLDER_EXPR:
b4ae5201
RK
9370 /* This case involves extracting fields from an object to determine the
9371 position of other fields. We don't try to encode this here. The
9372 only user of this is Ada, which encodes the needed information using
9373 the names of types. */
e7af1d45 9374 return 0;
b4ae5201 9375
aea9695c
RK
9376 case CALL_EXPR:
9377 return 0;
9378
4ada538b
MM
9379 case PREINCREMENT_EXPR:
9380 case PREDECREMENT_EXPR:
9381 case POSTINCREMENT_EXPR:
9382 case POSTDECREMENT_EXPR:
9383 /* There are no opcodes for these operations. */
9384 return 0;
9385
aea9695c 9386 case ADDR_EXPR:
1a186ec5
RH
9387 /* If we already want an address, there's nothing we can do. */
9388 if (want_address)
9389 return 0;
aea9695c 9390
1a186ec5
RH
9391 /* Otherwise, process the argument and look for the address. */
9392 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
aea9695c 9393
d8041cc8 9394 case VAR_DECL:
c2f7fa15 9395 if (DECL_THREAD_LOCAL_P (loc))
b9203463
RH
9396 {
9397 rtx rtl;
9398
b9203463 9399 /* If this is not defined, we have no way to emit the data. */
8893239d 9400 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
fdbe66f2 9401 return 0;
b9203463
RH
9402
9403 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9404 look up addresses of objects in the current module. */
3c655f42 9405 if (DECL_EXTERNAL (loc))
b9203463
RH
9406 return 0;
9407
9408 rtl = rtl_for_decl_location (loc);
9409 if (rtl == NULL_RTX)
9410 return 0;
9411
3c0cb5de 9412 if (!MEM_P (rtl))
b9203463
RH
9413 return 0;
9414 rtl = XEXP (rtl, 0);
9415 if (! CONSTANT_P (rtl))
9416 return 0;
9417
9418 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9419 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9420 ret->dw_loc_oprnd1.v.val_addr = rtl;
9421
9422 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9423 add_loc_descr (&ret, ret1);
9424
1a186ec5 9425 have_address = 1;
b9203463
RH
9426 break;
9427 }
1a186ec5 9428 /* FALLTHRU */
b9203463 9429
d8041cc8 9430 case PARM_DECL:
833b3afe
DB
9431 if (DECL_HAS_VALUE_EXPR_P (loc))
9432 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9433 want_address);
1a186ec5
RH
9434 /* FALLTHRU */
9435
6de9cd9a 9436 case RESULT_DECL:
3c3f1a6f 9437 case FUNCTION_DECL:
d8041cc8
RH
9438 {
9439 rtx rtl = rtl_for_decl_location (loc);
d8041cc8 9440
a97c9600 9441 if (rtl == NULL_RTX)
e7af1d45 9442 return 0;
2878ea73 9443 else if (GET_CODE (rtl) == CONST_INT)
1a186ec5
RH
9444 {
9445 HOST_WIDE_INT val = INTVAL (rtl);
9446 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9447 val &= GET_MODE_MASK (DECL_MODE (loc));
9448 ret = int_loc_descriptor (val);
9449 }
9450 else if (GET_CODE (rtl) == CONST_STRING)
9451 return 0;
a97c9600 9452 else if (CONSTANT_P (rtl))
d8041cc8
RH
9453 {
9454 ret = new_loc_descr (DW_OP_addr, 0, 0);
9455 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9456 ret->dw_loc_oprnd1.v.val_addr = rtl;
d8041cc8
RH
9457 }
9458 else
9459 {
1a186ec5
RH
9460 enum machine_mode mode;
9461
9462 /* Certain constructs can only be represented at top-level. */
9463 if (want_address == 2)
62760ffd 9464 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
c28abdf0 9465
1a186ec5 9466 mode = GET_MODE (rtl);
3c0cb5de 9467 if (MEM_P (rtl))
d8041cc8 9468 {
d8041cc8 9469 rtl = XEXP (rtl, 0);
1a186ec5 9470 have_address = 1;
d8041cc8 9471 }
62760ffd 9472 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
d8041cc8
RH
9473 }
9474 }
9475 break;
9476
9477 case INDIRECT_REF:
1a186ec5
RH
9478 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9479 have_address = 1;
d8041cc8
RH
9480 break;
9481
749552c4 9482 case COMPOUND_EXPR:
1a186ec5 9483 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
749552c4 9484
ed972b14
RK
9485 case NOP_EXPR:
9486 case CONVERT_EXPR:
9487 case NON_LVALUE_EXPR:
ed239f5a 9488 case VIEW_CONVERT_EXPR:
b4ae5201 9489 case SAVE_EXPR:
07beea0d
AH
9490 case GIMPLE_MODIFY_STMT:
9491 return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9492 want_address);
e57cabac 9493
d8041cc8
RH
9494 case COMPONENT_REF:
9495 case BIT_FIELD_REF:
9496 case ARRAY_REF:
b4e3fabb 9497 case ARRAY_RANGE_REF:
d8041cc8
RH
9498 {
9499 tree obj, offset;
9500 HOST_WIDE_INT bitsize, bitpos, bytepos;
9501 enum machine_mode mode;
9502 int volatilep;
953ff289 9503 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
d8041cc8
RH
9504
9505 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
2614034e 9506 &unsignedp, &volatilep, false);
e7af1d45
RK
9507
9508 if (obj == loc)
9509 return 0;
9510
1a186ec5 9511 ret = loc_descriptor_from_tree_1 (obj, 1);
e7af1d45 9512 if (ret == 0
2ad9852d 9513 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
e7af1d45 9514 return 0;
d8041cc8
RH
9515
9516 if (offset != NULL_TREE)
9517 {
9518 /* Variable offset. */
1a186ec5 9519 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
d8041cc8
RH
9520 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9521 }
9522
d8041cc8
RH
9523 bytepos = bitpos / BITS_PER_UNIT;
9524 if (bytepos > 0)
9525 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9526 else if (bytepos < 0)
9527 {
9528 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9529 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9530 }
1a186ec5
RH
9531
9532 have_address = 1;
d8041cc8
RH
9533 break;
9534 }
9535
9536 case INTEGER_CST:
9537 if (host_integerp (loc, 0))
9538 ret = int_loc_descriptor (tree_low_cst (loc, 0));
e7af1d45
RK
9539 else
9540 return 0;
d8041cc8 9541 break;
d8041cc8 9542
c67b2a58
RK
9543 case CONSTRUCTOR:
9544 {
75c20980
RH
9545 /* Get an RTL for this, if something has been emitted. */
9546 rtx rtl = lookup_constant_def (loc);
9547 enum machine_mode mode;
9548
1a186ec5 9549 if (!rtl || !MEM_P (rtl))
75c20980
RH
9550 return 0;
9551 mode = GET_MODE (rtl);
9552 rtl = XEXP (rtl, 0);
62760ffd 9553 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
1a186ec5 9554 have_address = 1;
c67b2a58
RK
9555 break;
9556 }
9557
c26fbbca 9558 case TRUTH_AND_EXPR:
9702143f 9559 case TRUTH_ANDIF_EXPR:
d8041cc8
RH
9560 case BIT_AND_EXPR:
9561 op = DW_OP_and;
9562 goto do_binop;
e7af1d45 9563
9702143f 9564 case TRUTH_XOR_EXPR:
d8041cc8
RH
9565 case BIT_XOR_EXPR:
9566 op = DW_OP_xor;
9567 goto do_binop;
e7af1d45 9568
9702143f
RK
9569 case TRUTH_OR_EXPR:
9570 case TRUTH_ORIF_EXPR:
d8041cc8
RH
9571 case BIT_IOR_EXPR:
9572 op = DW_OP_or;
9573 goto do_binop;
e7af1d45 9574
8dcea3f3
VC
9575 case FLOOR_DIV_EXPR:
9576 case CEIL_DIV_EXPR:
9577 case ROUND_DIV_EXPR:
d8041cc8
RH
9578 case TRUNC_DIV_EXPR:
9579 op = DW_OP_div;
9580 goto do_binop;
e7af1d45 9581
d8041cc8
RH
9582 case MINUS_EXPR:
9583 op = DW_OP_minus;
9584 goto do_binop;
e7af1d45 9585
8dcea3f3
VC
9586 case FLOOR_MOD_EXPR:
9587 case CEIL_MOD_EXPR:
9588 case ROUND_MOD_EXPR:
d8041cc8
RH
9589 case TRUNC_MOD_EXPR:
9590 op = DW_OP_mod;
9591 goto do_binop;
e7af1d45 9592
d8041cc8
RH
9593 case MULT_EXPR:
9594 op = DW_OP_mul;
9595 goto do_binop;
e7af1d45 9596
d8041cc8
RH
9597 case LSHIFT_EXPR:
9598 op = DW_OP_shl;
9599 goto do_binop;
e7af1d45 9600
d8041cc8 9601 case RSHIFT_EXPR:
953ff289 9602 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
d8041cc8 9603 goto do_binop;
e7af1d45 9604
5be014d5 9605 case POINTER_PLUS_EXPR:
d8041cc8
RH
9606 case PLUS_EXPR:
9607 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9608 && host_integerp (TREE_OPERAND (loc, 1), 0))
9609 {
1a186ec5 9610 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
e7af1d45
RK
9611 if (ret == 0)
9612 return 0;
9613
d8041cc8
RH
9614 add_loc_descr (&ret,
9615 new_loc_descr (DW_OP_plus_uconst,
9616 tree_low_cst (TREE_OPERAND (loc, 1),
9617 0),
9618 0));
9619 break;
9620 }
e7af1d45 9621
d8041cc8
RH
9622 op = DW_OP_plus;
9623 goto do_binop;
2ad9852d 9624
d8041cc8 9625 case LE_EXPR:
8df83eae 9626 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
e7af1d45
RK
9627 return 0;
9628
d8041cc8
RH
9629 op = DW_OP_le;
9630 goto do_binop;
e7af1d45 9631
d8041cc8 9632 case GE_EXPR:
8df83eae 9633 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
e7af1d45
RK
9634 return 0;
9635
d8041cc8
RH
9636 op = DW_OP_ge;
9637 goto do_binop;
e7af1d45 9638
d8041cc8 9639 case LT_EXPR:
8df83eae 9640 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
e7af1d45
RK
9641 return 0;
9642
d8041cc8
RH
9643 op = DW_OP_lt;
9644 goto do_binop;
e7af1d45 9645
d8041cc8 9646 case GT_EXPR:
8df83eae 9647 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
e7af1d45
RK
9648 return 0;
9649
d8041cc8
RH
9650 op = DW_OP_gt;
9651 goto do_binop;
e7af1d45 9652
d8041cc8
RH
9653 case EQ_EXPR:
9654 op = DW_OP_eq;
9655 goto do_binop;
e7af1d45 9656
d8041cc8
RH
9657 case NE_EXPR:
9658 op = DW_OP_ne;
9659 goto do_binop;
9660
9661 do_binop:
1a186ec5
RH
9662 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9663 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
e7af1d45
RK
9664 if (ret == 0 || ret1 == 0)
9665 return 0;
9666
9667 add_loc_descr (&ret, ret1);
d8041cc8
RH
9668 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9669 break;
9670
9702143f 9671 case TRUTH_NOT_EXPR:
d8041cc8
RH
9672 case BIT_NOT_EXPR:
9673 op = DW_OP_not;
9674 goto do_unop;
e7af1d45 9675
d8041cc8
RH
9676 case ABS_EXPR:
9677 op = DW_OP_abs;
9678 goto do_unop;
e7af1d45 9679
d8041cc8
RH
9680 case NEGATE_EXPR:
9681 op = DW_OP_neg;
9682 goto do_unop;
9683
9684 do_unop:
1a186ec5 9685 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
e7af1d45
RK
9686 if (ret == 0)
9687 return 0;
9688
d8041cc8
RH
9689 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9690 break;
9691
fd5580cb 9692 case MIN_EXPR:
d8041cc8 9693 case MAX_EXPR:
fd5580cb 9694 {
2878ea73
MS
9695 const enum tree_code code =
9696 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
fd5580cb 9697
2878ea73 9698 loc = build3 (COND_EXPR, TREE_TYPE (loc),
3244e67d
RS
9699 build2 (code, integer_type_node,
9700 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
2878ea73 9701 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
fd5580cb 9702 }
2ad9852d 9703
3ef42a0c 9704 /* ... fall through ... */
d8041cc8
RH
9705
9706 case COND_EXPR:
9707 {
e7af1d45 9708 dw_loc_descr_ref lhs
1a186ec5 9709 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
e7af1d45 9710 dw_loc_descr_ref rhs
1a186ec5 9711 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
d8041cc8
RH
9712 dw_loc_descr_ref bra_node, jump_node, tmp;
9713
1a186ec5 9714 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
e7af1d45
RK
9715 if (ret == 0 || lhs == 0 || rhs == 0)
9716 return 0;
9717
d8041cc8
RH
9718 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9719 add_loc_descr (&ret, bra_node);
9720
e7af1d45 9721 add_loc_descr (&ret, rhs);
d8041cc8
RH
9722 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9723 add_loc_descr (&ret, jump_node);
9724
e7af1d45 9725 add_loc_descr (&ret, lhs);
d8041cc8 9726 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
e7af1d45 9727 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
d8041cc8
RH
9728
9729 /* ??? Need a node to point the skip at. Use a nop. */
9730 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9731 add_loc_descr (&ret, tmp);
9732 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9733 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9734 }
9735 break;
9736
0ba6c56d 9737 case FIX_TRUNC_EXPR:
0ba6c56d
RH
9738 return 0;
9739
d8041cc8 9740 default:
7d445f15
RH
9741 /* Leave front-end specific codes as simply unknown. This comes
9742 up, for instance, with the C STMT_EXPR. */
9743 if ((unsigned int) TREE_CODE (loc)
2878ea73 9744 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
7d445f15
RH
9745 return 0;
9746
c3f216e2 9747#ifdef ENABLE_CHECKING
7d445f15 9748 /* Otherwise this is a generic code; we should just lists all of
535a42b1 9749 these explicitly. We forgot one. */
ced3f397 9750 gcc_unreachable ();
c3f216e2
MM
9751#else
9752 /* In a release build, we want to degrade gracefully: better to
9753 generate incomplete debugging information than to crash. */
9754 return NULL;
9755#endif
d8041cc8
RH
9756 }
9757
e7af1d45 9758 /* Show if we can't fill the request for an address. */
1a186ec5 9759 if (want_address && !have_address)
e7af1d45 9760 return 0;
d8041cc8
RH
9761
9762 /* If we've got an address and don't want one, dereference. */
434eba35 9763 if (!want_address && have_address && ret)
d8041cc8 9764 {
e7af1d45
RK
9765 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9766
9767 if (size > DWARF2_ADDR_SIZE || size == -1)
9768 return 0;
2ad9852d 9769 else if (size == DWARF2_ADDR_SIZE)
d8041cc8
RH
9770 op = DW_OP_deref;
9771 else
9772 op = DW_OP_deref_size;
e7af1d45
RK
9773
9774 add_loc_descr (&ret, new_loc_descr (op, size, 0));
d8041cc8
RH
9775 }
9776
9777 return ret;
9778}
9779
1a186ec5
RH
9780static inline dw_loc_descr_ref
9781loc_descriptor_from_tree (tree loc)
9782{
9783 return loc_descriptor_from_tree_1 (loc, 2);
9784}
9785
665f2503 9786/* Given a value, round it up to the lowest multiple of `boundary'
a3f97cbb 9787 which is not less than the value itself. */
71dfc51f 9788
665f2503 9789static inline HOST_WIDE_INT
7080f735 9790ceiling (HOST_WIDE_INT value, unsigned int boundary)
a3f97cbb
JW
9791{
9792 return (((value + boundary - 1) / boundary) * boundary);
9793}
9794
9795/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9796 pointer to the declared type for the relevant field variable, or return
9797 `integer_type_node' if the given node turns out to be an
9798 ERROR_MARK node. */
71dfc51f
RK
9799
9800static inline tree
7080f735 9801field_type (tree decl)
a3f97cbb 9802{
b3694847 9803 tree type;
a3f97cbb
JW
9804
9805 if (TREE_CODE (decl) == ERROR_MARK)
9806 return integer_type_node;
9807
9808 type = DECL_BIT_FIELD_TYPE (decl);
71dfc51f 9809 if (type == NULL_TREE)
a3f97cbb
JW
9810 type = TREE_TYPE (decl);
9811
9812 return type;
9813}
9814
5f446d21
DD
9815/* Given a pointer to a tree node, return the alignment in bits for
9816 it, or else return BITS_PER_WORD if the node actually turns out to
9817 be an ERROR_MARK node. */
71dfc51f
RK
9818
9819static inline unsigned
7080f735 9820simple_type_align_in_bits (tree type)
a3f97cbb
JW
9821{
9822 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9823}
9824
5f446d21 9825static inline unsigned
7080f735 9826simple_decl_align_in_bits (tree decl)
5f446d21
DD
9827{
9828 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9829}
9830
d996473d
EB
9831/* Return the result of rounding T up to ALIGN. */
9832
9833static inline HOST_WIDE_INT
9834round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9835{
9836 /* We must be careful if T is negative because HOST_WIDE_INT can be
9837 either "above" or "below" unsigned int as per the C promotion
9838 rules, depending on the host, thus making the signedness of the
9839 direct multiplication and division unpredictable. */
9840 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9841
9842 u += align - 1;
9843 u /= align;
9844 u *= align;
9845
9846 return (HOST_WIDE_INT) u;
9847}
9848
2ad9852d
RK
9849/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9850 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9851 or return 0 if we are unable to determine what that offset is, either
9852 because the argument turns out to be a pointer to an ERROR_MARK node, or
9853 because the offset is actually variable. (We can't handle the latter case
9854 just yet). */
71dfc51f 9855
665f2503 9856static HOST_WIDE_INT
7080f735 9857field_byte_offset (tree decl)
a3f97cbb 9858{
665f2503 9859 HOST_WIDE_INT object_offset_in_bits;
665f2503 9860 HOST_WIDE_INT bitpos_int;
a3f97cbb
JW
9861
9862 if (TREE_CODE (decl) == ERROR_MARK)
9863 return 0;
a1c496cb 9864
ced3f397 9865 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
a3f97cbb 9866
556273e0 9867 /* We cannot yet cope with fields whose positions are variable, so
a3f97cbb
JW
9868 for now, when we see such things, we simply return 0. Someday, we may
9869 be able to handle such cases, but it will be damn difficult. */
665f2503 9870 if (! host_integerp (bit_position (decl), 0))
a3f97cbb 9871 return 0;
14a774a9 9872
665f2503 9873 bitpos_int = int_bit_position (decl);
a3f97cbb 9874
cd0a10dd
NF
9875#ifdef PCC_BITFIELD_TYPE_MATTERS
9876 if (PCC_BITFIELD_TYPE_MATTERS)
9877 {
9878 tree type;
9879 tree field_size_tree;
9880 HOST_WIDE_INT deepest_bitpos;
9881 unsigned HOST_WIDE_INT field_size_in_bits;
9882 unsigned int type_align_in_bits;
9883 unsigned int decl_align_in_bits;
9884 unsigned HOST_WIDE_INT type_size_in_bits;
a3f97cbb 9885
cd0a10dd
NF
9886 type = field_type (decl);
9887 field_size_tree = DECL_SIZE (decl);
a3f97cbb 9888
cd0a10dd
NF
9889 /* The size could be unspecified if there was an error, or for
9890 a flexible array member. */
9891 if (! field_size_tree)
9892 field_size_tree = bitsize_zero_node;
9893
9894 /* If we don't know the size of the field, pretend it's a full word. */
9895 if (host_integerp (field_size_tree, 1))
9896 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9897 else
9898 field_size_in_bits = BITS_PER_WORD;
9899
9900 type_size_in_bits = simple_type_size_in_bits (type);
9901 type_align_in_bits = simple_type_align_in_bits (type);
9902 decl_align_in_bits = simple_decl_align_in_bits (decl);
9903
9904 /* The GCC front-end doesn't make any attempt to keep track of the
9905 starting bit offset (relative to the start of the containing
9906 structure type) of the hypothetical "containing object" for a
9907 bit-field. Thus, when computing the byte offset value for the
9908 start of the "containing object" of a bit-field, we must deduce
9909 this information on our own. This can be rather tricky to do in
9910 some cases. For example, handling the following structure type
9911 definition when compiling for an i386/i486 target (which only
9912 aligns long long's to 32-bit boundaries) can be very tricky:
a3f97cbb
JW
9913
9914 struct S { int field1; long long field2:31; };
9915
cd0a10dd
NF
9916 Fortunately, there is a simple rule-of-thumb which can be used
9917 in such cases. When compiling for an i386/i486, GCC will
9918 allocate 8 bytes for the structure shown above. It decides to
9919 do this based upon one simple rule for bit-field allocation.
9920 GCC allocates each "containing object" for each bit-field at
9921 the first (i.e. lowest addressed) legitimate alignment boundary
9922 (based upon the required minimum alignment for the declared
9923 type of the field) which it can possibly use, subject to the
9924 condition that there is still enough available space remaining
9925 in the containing object (when allocated at the selected point)
9926 to fully accommodate all of the bits of the bit-field itself.
9927
9928 This simple rule makes it obvious why GCC allocates 8 bytes for
9929 each object of the structure type shown above. When looking
9930 for a place to allocate the "containing object" for `field2',
9931 the compiler simply tries to allocate a 64-bit "containing
9932 object" at each successive 32-bit boundary (starting at zero)
9933 until it finds a place to allocate that 64- bit field such that
9934 at least 31 contiguous (and previously unallocated) bits remain
9935 within that selected 64 bit field. (As it turns out, for the
9936 example above, the compiler finds it is OK to allocate the
9937 "containing object" 64-bit field at bit-offset zero within the
9938 structure type.)
9939
9940 Here we attempt to work backwards from the limited set of facts
9941 we're given, and we try to deduce from those facts, where GCC
9942 must have believed that the containing object started (within
9943 the structure type). The value we deduce is then used (by the
9944 callers of this routine) to generate DW_AT_location and
9945 DW_AT_bit_offset attributes for fields (both bit-fields and, in
9946 the case of DW_AT_location, regular fields as well). */
9947
9948 /* Figure out the bit-distance from the start of the structure to
9949 the "deepest" bit of the bit-field. */
9950 deepest_bitpos = bitpos_int + field_size_in_bits;
9951
9952 /* This is the tricky part. Use some fancy footwork to deduce
9953 where the lowest addressed bit of the containing object must
9954 be. */
5f446d21
DD
9955 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9956
cd0a10dd
NF
9957 /* Round up to type_align by default. This works best for
9958 bitfields. */
d996473d 9959 object_offset_in_bits
cd0a10dd
NF
9960 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
9961
9962 if (object_offset_in_bits > bitpos_int)
9963 {
9964 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9965
9966 /* Round up to decl_align instead. */
9967 object_offset_in_bits
9968 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
9969 }
5f446d21 9970 }
cd0a10dd
NF
9971 else
9972#endif
9973 object_offset_in_bits = bitpos_int;
a3f97cbb 9974
2ad9852d 9975 return object_offset_in_bits / BITS_PER_UNIT;
a3f97cbb 9976}
a3f97cbb 9977\f
71dfc51f
RK
9978/* The following routines define various Dwarf attributes and any data
9979 associated with them. */
a3f97cbb 9980
ef76d03b 9981/* Add a location description attribute value to a DIE.
a3f97cbb 9982
ef76d03b 9983 This emits location attributes suitable for whole variables and
a3f97cbb
JW
9984 whole parameters. Note that the location attributes for struct fields are
9985 generated by the routine `data_member_location_attribute' below. */
71dfc51f 9986
b9203463 9987static inline void
7080f735
AJ
9988add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9989 dw_loc_descr_ref descr)
a3f97cbb 9990{
e7af1d45
RK
9991 if (descr != 0)
9992 add_AT_loc (die, attr_kind, descr);
a3f97cbb
JW
9993}
9994
2ad9852d
RK
9995/* Attach the specialized form of location attribute used for data members of
9996 struct and union types. In the special case of a FIELD_DECL node which
9997 represents a bit-field, the "offset" part of this special location
9998 descriptor must indicate the distance in bytes from the lowest-addressed
9999 byte of the containing struct or union type to the lowest-addressed byte of
10000 the "containing object" for the bit-field. (See the `field_byte_offset'
10001 function above).
10002
10003 For any given bit-field, the "containing object" is a hypothetical object
10004 (of some integral or enum type) within which the given bit-field lives. The
10005 type of this hypothetical "containing object" is always the same as the
10006 declared type of the individual bit-field itself (for GCC anyway... the
10007 DWARF spec doesn't actually mandate this). Note that it is the size (in
10008 bytes) of the hypothetical "containing object" which will be given in the
10009 DW_AT_byte_size attribute for this bit-field. (See the
10010 `byte_size_attribute' function below.) It is also used when calculating the
10011 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
10012 function below.) */
71dfc51f 10013
a3f97cbb 10014static void
7080f735 10015add_data_member_location_attribute (dw_die_ref die, tree decl)
a3f97cbb 10016{
799f628a 10017 HOST_WIDE_INT offset;
649ce3f2 10018 dw_loc_descr_ref loc_descr = 0;
a3f97cbb 10019
95b4aca6 10020 if (TREE_CODE (decl) == TREE_BINFO)
649ce3f2
JM
10021 {
10022 /* We're working on the TAG_inheritance for a base class. */
809e3e7f 10023 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
649ce3f2
JM
10024 {
10025 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10026 aren't at a fixed offset from all (sub)objects of the same
10027 type. We need to extract the appropriate offset from our
10028 vtable. The following dwarf expression means
10029
10030 BaseAddr = ObAddr + *((*ObAddr) - Offset)
10031
10032 This is specific to the V3 ABI, of course. */
10033
10034 dw_loc_descr_ref tmp;
2ad9852d 10035
649ce3f2
JM
10036 /* Make a copy of the object address. */
10037 tmp = new_loc_descr (DW_OP_dup, 0, 0);
10038 add_loc_descr (&loc_descr, tmp);
2ad9852d 10039
649ce3f2
JM
10040 /* Extract the vtable address. */
10041 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10042 add_loc_descr (&loc_descr, tmp);
2ad9852d 10043
649ce3f2
JM
10044 /* Calculate the address of the offset. */
10045 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
ced3f397 10046 gcc_assert (offset < 0);
2ad9852d 10047
649ce3f2
JM
10048 tmp = int_loc_descriptor (-offset);
10049 add_loc_descr (&loc_descr, tmp);
10050 tmp = new_loc_descr (DW_OP_minus, 0, 0);
10051 add_loc_descr (&loc_descr, tmp);
2ad9852d 10052
649ce3f2
JM
10053 /* Extract the offset. */
10054 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10055 add_loc_descr (&loc_descr, tmp);
2ad9852d 10056
649ce3f2
JM
10057 /* Add it to the object address. */
10058 tmp = new_loc_descr (DW_OP_plus, 0, 0);
10059 add_loc_descr (&loc_descr, tmp);
10060 }
10061 else
10062 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10063 }
61b32c02
JM
10064 else
10065 offset = field_byte_offset (decl);
10066
649ce3f2
JM
10067 if (! loc_descr)
10068 {
10069 enum dwarf_location_atom op;
10070
2ad9852d
RK
10071 /* The DWARF2 standard says that we should assume that the structure
10072 address is already on the stack, so we can specify a structure field
10073 address by using DW_OP_plus_uconst. */
71dfc51f 10074
a3f97cbb 10075#ifdef MIPS_DEBUGGING_INFO
2ad9852d
RK
10076 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10077 operator correctly. It works only if we leave the offset on the
10078 stack. */
649ce3f2 10079 op = DW_OP_constu;
a3f97cbb 10080#else
649ce3f2 10081 op = DW_OP_plus_uconst;
a3f97cbb 10082#endif
71dfc51f 10083
649ce3f2
JM
10084 loc_descr = new_loc_descr (op, offset, 0);
10085 }
2ad9852d 10086
a3f97cbb
JW
10087 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10088}
10089
e7ee3914
AM
10090/* Writes integer values to dw_vec_const array. */
10091
10092static void
10093insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10094{
10095 while (size != 0)
10096 {
10097 *dest++ = val & 0xff;
10098 val >>= 8;
10099 --size;
10100 }
10101}
10102
10103/* Reads integers from dw_vec_const array. Inverse of insert_int. */
10104
10105static HOST_WIDE_INT
10106extract_int (const unsigned char *src, unsigned int size)
10107{
10108 HOST_WIDE_INT val = 0;
10109
10110 src += size;
10111 while (size != 0)
10112 {
10113 val <<= 8;
10114 val |= *--src & 0xff;
10115 --size;
10116 }
10117 return val;
10118}
10119
10120/* Writes floating point values to dw_vec_const array. */
10121
10122static void
10123insert_float (rtx rtl, unsigned char *array)
10124{
10125 REAL_VALUE_TYPE rv;
10126 long val[4];
10127 int i;
10128
10129 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10130 real_to_target (val, &rv, GET_MODE (rtl));
10131
10132 /* real_to_target puts 32-bit pieces in each long. Pack them. */
10133 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10134 {
10135 insert_int (val[i], 4, array);
10136 array += 4;
10137 }
10138}
10139
b20b352b 10140/* Attach a DW_AT_const_value attribute for a variable or a parameter which
a3f97cbb
JW
10141 does not have a "location" either in memory or in a register. These
10142 things can arise in GNU C when a constant is passed as an actual parameter
10143 to an inlined function. They can also arise in C++ where declared
10144 constants do not necessarily get memory "homes". */
71dfc51f 10145
a3f97cbb 10146static void
7080f735 10147add_const_value_attribute (dw_die_ref die, rtx rtl)
a3f97cbb
JW
10148{
10149 switch (GET_CODE (rtl))
10150 {
10151 case CONST_INT:
2e4b9b8c
RH
10152 {
10153 HOST_WIDE_INT val = INTVAL (rtl);
c26fbbca 10154
799f628a
JH
10155 if (val < 0)
10156 add_AT_int (die, DW_AT_const_value, val);
a1c496cb 10157 else
799f628a 10158 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
2e4b9b8c 10159 }
a3f97cbb
JW
10160 break;
10161
10162 case CONST_DOUBLE:
10163 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
73c68f61
SS
10164 floating-point constant. A CONST_DOUBLE is used whenever the
10165 constant requires more than one word in order to be adequately
10166 represented. We output CONST_DOUBLEs as blocks. */
469ac993 10167 {
b3694847 10168 enum machine_mode mode = GET_MODE (rtl);
469ac993 10169
3d8bf70f 10170 if (SCALAR_FLOAT_MODE_P (mode))
469ac993 10171 {
e7ee3914
AM
10172 unsigned int length = GET_MODE_SIZE (mode);
10173 unsigned char *array = ggc_alloc (length);
469ac993 10174
e7ee3914
AM
10175 insert_float (rtl, array);
10176 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
469ac993
JM
10177 }
10178 else
2e4b9b8c
RH
10179 {
10180 /* ??? We really should be using HOST_WIDE_INT throughout. */
ced3f397 10181 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
2ad9852d 10182
2e4b9b8c
RH
10183 add_AT_long_long (die, DW_AT_const_value,
10184 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10185 }
469ac993 10186 }
a3f97cbb
JW
10187 break;
10188
e7ee3914
AM
10189 case CONST_VECTOR:
10190 {
10191 enum machine_mode mode = GET_MODE (rtl);
10192 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10193 unsigned int length = CONST_VECTOR_NUNITS (rtl);
10194 unsigned char *array = ggc_alloc (length * elt_size);
10195 unsigned int i;
10196 unsigned char *p;
10197
ced3f397 10198 switch (GET_MODE_CLASS (mode))
e7ee3914 10199 {
ced3f397 10200 case MODE_VECTOR_INT:
e7ee3914
AM
10201 for (i = 0, p = array; i < length; i++, p += elt_size)
10202 {
10203 rtx elt = CONST_VECTOR_ELT (rtl, i);
10204 HOST_WIDE_INT lo, hi;
a1c496cb 10205
ced3f397 10206 switch (GET_CODE (elt))
e7ee3914 10207 {
ced3f397 10208 case CONST_INT:
e7ee3914
AM
10209 lo = INTVAL (elt);
10210 hi = -(lo < 0);
ced3f397 10211 break;
a1c496cb 10212
ced3f397 10213 case CONST_DOUBLE:
e7ee3914
AM
10214 lo = CONST_DOUBLE_LOW (elt);
10215 hi = CONST_DOUBLE_HIGH (elt);
ced3f397 10216 break;
a1c496cb 10217
ced3f397
NS
10218 default:
10219 gcc_unreachable ();
e7ee3914 10220 }
a1c496cb 10221
e7ee3914
AM
10222 if (elt_size <= sizeof (HOST_WIDE_INT))
10223 insert_int (lo, elt_size, p);
ced3f397 10224 else
e7ee3914
AM
10225 {
10226 unsigned char *p0 = p;
10227 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
a1c496cb 10228
ced3f397 10229 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
e7ee3914
AM
10230 if (WORDS_BIG_ENDIAN)
10231 {
10232 p0 = p1;
10233 p1 = p;
10234 }
10235 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10236 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10237 }
e7ee3914 10238 }
ced3f397
NS
10239 break;
10240
10241 case MODE_VECTOR_FLOAT:
e7ee3914
AM
10242 for (i = 0, p = array; i < length; i++, p += elt_size)
10243 {
10244 rtx elt = CONST_VECTOR_ELT (rtl, i);
10245 insert_float (elt, p);
10246 }
ced3f397
NS
10247 break;
10248
10249 default:
10250 gcc_unreachable ();
e7ee3914 10251 }
e7ee3914
AM
10252
10253 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10254 }
10255 break;
10256
a3f97cbb
JW
10257 case CONST_STRING:
10258 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10259 break;
10260
10261 case SYMBOL_REF:
10262 case LABEL_REF:
10263 case CONST:
c470afad 10264 add_AT_addr (die, DW_AT_const_value, rtl);
a1bbd445 10265 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
a3f97cbb
JW
10266 break;
10267
10268 case PLUS:
10269 /* In cases where an inlined instance of an inline function is passed
73c68f61
SS
10270 the address of an `auto' variable (which is local to the caller) we
10271 can get a situation where the DECL_RTL of the artificial local
10272 variable (for the inlining) which acts as a stand-in for the
10273 corresponding formal parameter (of the inline function) will look
10274 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
10275 exactly a compile-time constant expression, but it isn't the address
10276 of the (artificial) local variable either. Rather, it represents the
10277 *value* which the artificial local variable always has during its
10278 lifetime. We currently have no way to represent such quasi-constant
10279 values in Dwarf, so for now we just punt and generate nothing. */
a3f97cbb
JW
10280 break;
10281
10282 default:
10283 /* No other kinds of rtx should be possible here. */
ced3f397 10284 gcc_unreachable ();
a3f97cbb
JW
10285 }
10286
10287}
10288
0d06ead5
GK
10289/* Determine whether the evaluation of EXPR references any variables
10290 or functions which aren't otherwise used (and therefore may not be
10291 output). */
10292static tree
10293reference_to_unused (tree * tp, int * walk_subtrees,
10294 void * data ATTRIBUTE_UNUSED)
10295{
07beea0d 10296 if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
0d06ead5 10297 *walk_subtrees = 0;
2878ea73 10298
0d06ead5
GK
10299 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10300 && ! TREE_ASM_WRITTEN (*tp))
10301 return *tp;
4f40bddf
JH
10302 else if (!flag_unit_at_a_time)
10303 return NULL_TREE;
10304 else if (!cgraph_global_info_ready
10305 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10306 gcc_unreachable ();
10307 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
9b1959ff
RG
10308 {
10309 struct varpool_node *node = varpool_node (*tp);
10310 if (!node->needed)
10311 return *tp;
10312 }
4f40bddf
JH
10313 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10314 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10315 {
10316 struct cgraph_node *node = cgraph_node (*tp);
10317 if (!node->output)
2878ea73 10318 return *tp;
4f40bddf 10319 }
9b1959ff
RG
10320
10321 return NULL_TREE;
0d06ead5
GK
10322}
10323
77270e03
JW
10324/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10325 for use in a later add_const_value_attribute call. */
10326
10327static rtx
10328rtl_for_decl_init (tree init, tree type)
10329{
10330 rtx rtl = NULL_RTX;
10331
10332 /* If a variable is initialized with a string constant without embedded
10333 zeros, build CONST_STRING. */
10334 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10335 {
10336 tree enttype = TREE_TYPE (type);
10337 tree domain = TYPE_DOMAIN (type);
10338 enum machine_mode mode = TYPE_MODE (enttype);
10339
10340 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10341 && domain
10342 && integer_zerop (TYPE_MIN_VALUE (domain))
10343 && compare_tree_int (TYPE_MAX_VALUE (domain),
10344 TREE_STRING_LENGTH (init) - 1) == 0
10345 && ((size_t) TREE_STRING_LENGTH (init)
10346 == strlen (TREE_STRING_POINTER (init)) + 1))
10347 rtl = gen_rtx_CONST_STRING (VOIDmode,
10348 ggc_strdup (TREE_STRING_POINTER (init)));
10349 }
9a23b833
GK
10350 /* Other aggregates, and complex values, could be represented using
10351 CONCAT: FIXME! */
10352 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10353 ;
2878ea73 10354 /* Vectors only work if their mode is supported by the target.
9a23b833
GK
10355 FIXME: generic vectors ought to work too. */
10356 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
0d06ead5 10357 ;
77270e03 10358 /* If the initializer is something that we know will expand into an
0d06ead5
GK
10359 immediate RTL constant, expand it now. We must be careful not to
10360 reference variables which won't be output. */
10361 else if (initializer_constant_valid_p (init, type)
10362 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
77270e03
JW
10363 {
10364 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10365
10366 /* If expand_expr returns a MEM, it wasn't immediate. */
10367 gcc_assert (!rtl || !MEM_P (rtl));
10368 }
10369
10370 return rtl;
10371}
10372
10373/* Generate RTL for the variable DECL to represent its location. */
10374
d8041cc8 10375static rtx
7080f735 10376rtl_for_decl_location (tree decl)
a3f97cbb 10377{
b3694847 10378 rtx rtl;
71dfc51f 10379
a3f97cbb
JW
10380 /* Here we have to decide where we are going to say the parameter "lives"
10381 (as far as the debugger is concerned). We only have a couple of
10382 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
71dfc51f 10383
556273e0 10384 DECL_RTL normally indicates where the parameter lives during most of the
71dfc51f 10385 activation of the function. If optimization is enabled however, this
556273e0 10386 could be either NULL or else a pseudo-reg. Both of those cases indicate
a3f97cbb
JW
10387 that the parameter doesn't really live anywhere (as far as the code
10388 generation parts of GCC are concerned) during most of the function's
10389 activation. That will happen (for example) if the parameter is never
71dfc51f
RK
10390 referenced within the function.
10391
10392 We could just generate a location descriptor here for all non-NULL
10393 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10394 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10395 where DECL_RTL is NULL or is a pseudo-reg.
10396
10397 Note however that we can only get away with using DECL_INCOMING_RTL as
10398 a backup substitute for DECL_RTL in certain limited cases. In cases
10399 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10400 we can be sure that the parameter was passed using the same type as it is
10401 declared to have within the function, and that its DECL_INCOMING_RTL
10402 points us to a place where a value of that type is passed.
10403
10404 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10405 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10406 because in these cases DECL_INCOMING_RTL points us to a value of some
10407 type which is *different* from the type of the parameter itself. Thus,
10408 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10409 such cases, the debugger would end up (for example) trying to fetch a
10410 `float' from a place which actually contains the first part of a
10411 `double'. That would lead to really incorrect and confusing
10412 output at debug-time.
10413
10414 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10415 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10416 are a couple of exceptions however. On little-endian machines we can
10417 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10418 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10419 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10420 when (on a little-endian machine) a non-prototyped function has a
10421 parameter declared to be of type `short' or `char'. In such cases,
10422 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10423 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10424 passed `int' value. If the debugger then uses that address to fetch
10425 a `short' or a `char' (on a little-endian machine) the result will be
10426 the correct data, so we allow for such exceptional cases below.
10427
10428 Note that our goal here is to describe the place where the given formal
2ad9852d
RK
10429 parameter lives during most of the function's activation (i.e. between the
10430 end of the prologue and the start of the epilogue). We'll do that as best
10431 as we can. Note however that if the given formal parameter is modified
10432 sometime during the execution of the function, then a stack backtrace (at
10433 debug-time) will show the function as having been called with the *new*
10434 value rather than the value which was originally passed in. This happens
10435 rarely enough that it is not a major problem, but it *is* a problem, and
10436 I'd like to fix it.
10437
10438 A future version of dwarf2out.c may generate two additional attributes for
10439 any given DW_TAG_formal_parameter DIE which will describe the "passed
10440 type" and the "passed location" for the given formal parameter in addition
10441 to the attributes we now generate to indicate the "declared type" and the
10442 "active location" for each parameter. This additional set of attributes
10443 could be used by debuggers for stack backtraces. Separately, note that
10444 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10445 This happens (for example) for inlined-instances of inline function formal
10446 parameters which are never referenced. This really shouldn't be
10447 happening. All PARM_DECL nodes should get valid non-NULL
6de9cd9a 10448 DECL_INCOMING_RTL values. FIXME. */
a3f97cbb
JW
10449
10450 /* Use DECL_RTL as the "location" unless we find something better. */
110c3568 10451 rtl = DECL_RTL_IF_SET (decl);
a3f97cbb 10452
c28abdf0 10453 /* When generating abstract instances, ignore everything except
234c071b
KB
10454 constants, symbols living in memory, and symbols living in
10455 fixed registers. */
c28abdf0
RH
10456 if (! reload_completed)
10457 {
10458 if (rtl
10459 && (CONSTANT_P (rtl)
3c0cb5de 10460 || (MEM_P (rtl)
234c071b 10461 && CONSTANT_P (XEXP (rtl, 0)))
f8cfc6aa 10462 || (REG_P (rtl)
234c071b
KB
10463 && TREE_CODE (decl) == VAR_DECL
10464 && TREE_STATIC (decl))))
4c8c0dec 10465 {
5fd9b178 10466 rtl = targetm.delegitimize_address (rtl);
4c8c0dec
JJ
10467 return rtl;
10468 }
c28abdf0
RH
10469 rtl = NULL_RTX;
10470 }
10471 else if (TREE_CODE (decl) == PARM_DECL)
a3f97cbb
JW
10472 {
10473 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10474 {
d8cb0614
RH
10475 tree declared_type = TREE_TYPE (decl);
10476 tree passed_type = DECL_ARG_TYPE (decl);
10477 enum machine_mode dmode = TYPE_MODE (declared_type);
10478 enum machine_mode pmode = TYPE_MODE (passed_type);
a3f97cbb 10479
71dfc51f 10480 /* This decl represents a formal parameter which was optimized out.
a3f97cbb 10481 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
2ad9852d 10482 all cases where (rtl == NULL_RTX) just below. */
d8cb0614 10483 if (dmode == pmode)
556273e0 10484 rtl = DECL_INCOMING_RTL (decl);
d8cb0614 10485 else if (SCALAR_INT_MODE_P (dmode)
3401a17f
JJ
10486 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10487 && DECL_INCOMING_RTL (decl))
d8cb0614
RH
10488 {
10489 rtx inc = DECL_INCOMING_RTL (decl);
10490 if (REG_P (inc))
10491 rtl = inc;
10492 else if (MEM_P (inc))
10493 {
10494 if (BYTES_BIG_ENDIAN)
10495 rtl = adjust_address_nv (inc, dmode,
10496 GET_MODE_SIZE (pmode)
10497 - GET_MODE_SIZE (dmode));
10498 else
10499 rtl = inc;
10500 }
10501 }
a3f97cbb 10502 }
5a904a61
JW
10503
10504 /* If the parm was passed in registers, but lives on the stack, then
10505 make a big endian correction if the mode of the type of the
10506 parameter is not the same as the mode of the rtl. */
10507 /* ??? This is the same series of checks that are made in dbxout.c before
10508 we reach the big endian correction code there. It isn't clear if all
10509 of these checks are necessary here, but keeping them all is the safe
10510 thing to do. */
3c0cb5de 10511 else if (MEM_P (rtl)
5a904a61
JW
10512 && XEXP (rtl, 0) != const0_rtx
10513 && ! CONSTANT_P (XEXP (rtl, 0))
10514 /* Not passed in memory. */
3c0cb5de 10515 && !MEM_P (DECL_INCOMING_RTL (decl))
5a904a61 10516 /* Not passed by invisible reference. */
f8cfc6aa 10517 && (!REG_P (XEXP (rtl, 0))
5a904a61
JW
10518 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10519 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10520#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10521 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10522#endif
10523 )
10524 /* Big endian correction check. */
10525 && BYTES_BIG_ENDIAN
10526 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10527 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10528 < UNITS_PER_WORD))
10529 {
10530 int offset = (UNITS_PER_WORD
10531 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
2ad9852d 10532
5a904a61
JW
10533 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10534 plus_constant (XEXP (rtl, 0), offset));
10535 }
a3f97cbb 10536 }
8b495402 10537 else if (TREE_CODE (decl) == VAR_DECL
de3c6d93 10538 && rtl
3c0cb5de 10539 && MEM_P (rtl)
8b495402
DD
10540 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10541 && BYTES_BIG_ENDIAN)
10542 {
10543 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10544 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10545
10546 /* If a variable is declared "register" yet is smaller than
10547 a register, then if we store the variable to memory, it
10548 looks like we're storing a register-sized value, when in
10549 fact we are not. We need to adjust the offset of the
10550 storage location to reflect the actual value's bytes,
10551 else gdb will not be able to display it. */
10552 if (rsize > dsize)
10553 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10554 plus_constant (XEXP (rtl, 0), rsize-dsize));
10555 }
71dfc51f 10556
2ad9852d
RK
10557 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10558 and will have been substituted directly into all expressions that use it.
10559 C does not have such a concept, but C++ and other languages do. */
30e6f306 10560 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
77270e03 10561 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
8063ddcf 10562
4c8c0dec 10563 if (rtl)
5fd9b178 10564 rtl = targetm.delegitimize_address (rtl);
b9203463
RH
10565
10566 /* If we don't look past the constant pool, we risk emitting a
10567 reference to a constant pool entry that isn't referenced from
10568 code, and thus is not emitted. */
10569 if (rtl)
10570 rtl = avoid_constant_pool_reference (rtl);
10571
d8041cc8
RH
10572 return rtl;
10573}
10574
30e6f306
RH
10575/* We need to figure out what section we should use as the base for the
10576 address ranges where a given location is valid.
10577 1. If this particular DECL has a section associated with it, use that.
10578 2. If this function has a section associated with it, use that.
10579 3. Otherwise, use the text section.
10580 XXX: If you split a variable across multiple sections, we won't notice. */
10581
10582static const char *
10583secname_for_decl (tree decl)
10584{
10585 const char *secname;
10586
10587 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10588 {
10589 tree sectree = DECL_SECTION_NAME (decl);
10590 secname = TREE_STRING_POINTER (sectree);
10591 }
10592 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10593 {
10594 tree sectree = DECL_SECTION_NAME (current_function_decl);
10595 secname = TREE_STRING_POINTER (sectree);
10596 }
c543ca49 10597 else if (cfun && in_cold_section_p)
30e6f306
RH
10598 secname = cfun->cold_section_label;
10599 else
10600 secname = text_section_label;
10601
10602 return secname;
10603}
10604
b20b352b 10605/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
d8041cc8
RH
10606 data attribute for a variable or a parameter. We generate the
10607 DW_AT_const_value attribute only in those cases where the given variable
10608 or parameter does not have a true "location" either in memory or in a
10609 register. This can happen (for example) when a constant is passed as an
10610 actual argument in a call to an inline function. (It's possible that
10611 these things can crop up in other ways also.) Note that one type of
10612 constant value which can be passed into an inlined function is a constant
10613 pointer. This can happen for example if an actual argument in an inlined
10614 function call evaluates to a compile-time constant address. */
10615
10616static void
0a2d3d69
DB
10617add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10618 enum dwarf_attribute attr)
d8041cc8 10619{
b3694847 10620 rtx rtl;
b9203463 10621 dw_loc_descr_ref descr;
0a2d3d69 10622 var_loc_list *loc_list;
3c46c513 10623 struct var_loc_node *node;
d8041cc8
RH
10624 if (TREE_CODE (decl) == ERROR_MARK)
10625 return;
ced3f397
NS
10626
10627 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10628 || TREE_CODE (decl) == RESULT_DECL);
2878ea73 10629
0a2d3d69
DB
10630 /* See if we possibly have multiple locations for this variable. */
10631 loc_list = lookup_decl_loc (decl);
10632
10633 /* If it truly has multiple locations, the first and last node will
10634 differ. */
10635 if (loc_list && loc_list->first != loc_list->last)
10636 {
30e6f306 10637 const char *endname, *secname;
0a2d3d69
DB
10638 dw_loc_list_ref list;
10639 rtx varloc;
62760ffd 10640 enum var_init_status initialized;
3c46c513 10641
0a2d3d69 10642 /* Now that we know what section we are using for a base,
2878ea73 10643 actually construct the list of locations.
0a2d3d69
DB
10644 The first location information is what is passed to the
10645 function that creates the location list, and the remaining
10646 locations just get added on to that list.
10647 Note that we only know the start address for a location
10648 (IE location changes), so to build the range, we use
10649 the range [current location start, next location start].
10650 This means we have to special case the last node, and generate
10651 a range of [last location start, end of function label]. */
10652
10653 node = loc_list->first;
10654 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
30e6f306
RH
10655 secname = secname_for_decl (decl);
10656
62760ffd
CT
10657 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10658 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10659 else
10660 initialized = VAR_INIT_STATUS_INITIALIZED;
10661
10662 list = new_loc_list (loc_descriptor (varloc, initialized),
0a2d3d69
DB
10663 node->label, node->next->label, secname, 1);
10664 node = node->next;
10665
10666 for (; node->next; node = node->next)
10667 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10668 {
10669 /* The variable has a location between NODE->LABEL and
10670 NODE->NEXT->LABEL. */
62760ffd
CT
10671 enum var_init_status initialized =
10672 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
0a2d3d69 10673 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
62760ffd
CT
10674 add_loc_descr_to_loc_list (&list,
10675 loc_descriptor (varloc, initialized),
0a2d3d69
DB
10676 node->label, node->next->label, secname);
10677 }
10678
10679 /* If the variable has a location at the last label
10680 it keeps its location until the end of function. */
10681 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10682 {
10683 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
62760ffd
CT
10684 enum var_init_status initialized =
10685 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
0a2d3d69
DB
10686
10687 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10688 if (!current_function_decl)
10689 endname = text_end_label;
10690 else
10691 {
10692 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10693 current_function_funcdef_no);
10694 endname = ggc_strdup (label_id);
10695 }
62760ffd
CT
10696 add_loc_descr_to_loc_list (&list,
10697 loc_descriptor (varloc, initialized),
0a2d3d69
DB
10698 node->label, endname, secname);
10699 }
10700
10701 /* Finally, add the location list to the DIE, and we are done. */
10702 add_AT_loc_list (die, attr, list);
10703 return;
10704 }
10705
3c46c513
DB
10706 /* Try to get some constant RTL for this decl, and use that as the value of
10707 the location. */
2878ea73 10708
d8041cc8 10709 rtl = rtl_for_decl_location (decl);
1a186ec5 10710 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
a3f97cbb 10711 {
a3f97cbb 10712 add_const_value_attribute (die, rtl);
1a186ec5 10713 return;
a3f97cbb 10714 }
2878ea73 10715
86bd8ebf 10716 /* If we have tried to generate the location otherwise, and it
3c46c513
DB
10717 didn't work out (we wouldn't be here if we did), and we have a one entry
10718 location list, try generating a location from that. */
10719 if (loc_list && loc_list->first)
10720 {
62760ffd 10721 enum var_init_status status;
3c46c513 10722 node = loc_list->first;
62760ffd
CT
10723 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10724 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
3c46c513 10725 if (descr)
86bd8ebf
JJ
10726 {
10727 add_AT_location_description (die, attr, descr);
10728 return;
10729 }
10730 }
10731
10732 /* We couldn't get any rtl, so try directly generating the location
10733 description from the tree. */
10734 descr = loc_descriptor_from_tree (decl);
10735 if (descr)
10736 {
10737 add_AT_location_description (die, attr, descr);
10738 return;
3c46c513 10739 }
0d06ead5
GK
10740 /* None of that worked, so it must not really have a location;
10741 try adding a constant value attribute from the DECL_INITIAL. */
10742 tree_add_const_value_attribute (die, decl);
a3f97cbb
JW
10743}
10744
1bfb5f8f
JM
10745/* If we don't have a copy of this variable in memory for some reason (such
10746 as a C++ member constant that doesn't have an out-of-line definition),
10747 we should tell the debugger about the constant value. */
10748
10749static void
7080f735 10750tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
1bfb5f8f
JM
10751{
10752 tree init = DECL_INITIAL (decl);
10753 tree type = TREE_TYPE (decl);
77270e03 10754 rtx rtl;
1bfb5f8f 10755
77270e03 10756 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
1bfb5f8f
JM
10757 /* OK */;
10758 else
10759 return;
10760
77270e03
JW
10761 rtl = rtl_for_decl_init (init, type);
10762 if (rtl)
10763 add_const_value_attribute (var_die, rtl);
1bfb5f8f 10764}
0b34cf1e 10765
35d177a2
AO
10766/* Convert the CFI instructions for the current function into a
10767 location list. This is used for DW_AT_frame_base when we targeting
10768 a dwarf2 consumer that does not support the dwarf3
10769 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10770 expressions. */
30e6f306
RH
10771
10772static dw_loc_list_ref
35d177a2 10773convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
30e6f306
RH
10774{
10775 dw_fde_ref fde;
10776 dw_loc_list_ref list, *list_tail;
10777 dw_cfi_ref cfi;
10778 dw_cfa_location last_cfa, next_cfa;
10779 const char *start_label, *last_label, *section;
10780
10781 fde = &fde_table[fde_table_in_use - 1];
10782
10783 section = secname_for_decl (current_function_decl);
10784 list_tail = &list;
10785 list = NULL;
10786
10787 next_cfa.reg = INVALID_REGNUM;
10788 next_cfa.offset = 0;
10789 next_cfa.indirect = 0;
10790 next_cfa.base_offset = 0;
10791
10792 start_label = fde->dw_fde_begin;
10793
10794 /* ??? Bald assumption that the CIE opcode list does not contain
10795 advance opcodes. */
10796 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10797 lookup_cfa_1 (cfi, &next_cfa);
10798
10799 last_cfa = next_cfa;
10800 last_label = start_label;
10801
10802 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10803 switch (cfi->dw_cfi_opc)
10804 {
66cc7362 10805 case DW_CFA_set_loc:
30e6f306
RH
10806 case DW_CFA_advance_loc1:
10807 case DW_CFA_advance_loc2:
10808 case DW_CFA_advance_loc4:
10809 if (!cfa_equal_p (&last_cfa, &next_cfa))
10810 {
35d177a2
AO
10811 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10812 start_label, last_label, section,
10813 list == NULL);
30e6f306
RH
10814
10815 list_tail = &(*list_tail)->dw_loc_next;
10816 last_cfa = next_cfa;
10817 start_label = last_label;
10818 }
10819 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10820 break;
10821
10822 case DW_CFA_advance_loc:
10823 /* The encoding is complex enough that we should never emit this. */
10824 case DW_CFA_remember_state:
10825 case DW_CFA_restore_state:
10826 /* We don't handle these two in this function. It would be possible
10827 if it were to be required. */
10828 gcc_unreachable ();
10829
10830 default:
10831 lookup_cfa_1 (cfi, &next_cfa);
10832 break;
10833 }
10834
10835 if (!cfa_equal_p (&last_cfa, &next_cfa))
10836 {
35d177a2
AO
10837 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10838 start_label, last_label, section,
10839 list == NULL);
30e6f306
RH
10840 list_tail = &(*list_tail)->dw_loc_next;
10841 start_label = last_label;
10842 }
35d177a2
AO
10843 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10844 start_label, fde->dw_fde_end, section,
10845 list == NULL);
30e6f306
RH
10846
10847 return list;
10848}
10849
35d177a2
AO
10850/* Compute a displacement from the "steady-state frame pointer" to the
10851 frame base (often the same as the CFA), and store it in
10852 frame_pointer_fb_offset. OFFSET is added to the displacement
10853 before the latter is negated. */
30e6f306
RH
10854
10855static void
35d177a2 10856compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
30e6f306 10857{
f6672e8e
RH
10858 rtx reg, elim;
10859
10860#ifdef FRAME_POINTER_CFA_OFFSET
10861 reg = frame_pointer_rtx;
35d177a2 10862 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
f6672e8e
RH
10863#else
10864 reg = arg_pointer_rtx;
35d177a2 10865 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
f6672e8e 10866#endif
30e6f306 10867
f6672e8e
RH
10868 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10869 if (GET_CODE (elim) == PLUS)
10870 {
10871 offset += INTVAL (XEXP (elim, 1));
10872 elim = XEXP (elim, 0);
10873 }
10874 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10875 : stack_pointer_rtx));
30e6f306 10876
35d177a2 10877 frame_pointer_fb_offset = -offset;
30e6f306
RH
10878}
10879
b20b352b 10880/* Generate a DW_AT_name attribute given some string value to be included as
a3f97cbb 10881 the value of the attribute. */
71dfc51f 10882
c4274b22 10883static void
7080f735 10884add_name_attribute (dw_die_ref die, const char *name_string)
a3f97cbb 10885{
71dfc51f 10886 if (name_string != NULL && *name_string != 0)
14a774a9
RK
10887 {
10888 if (demangle_name_func)
10889 name_string = (*demangle_name_func) (name_string);
10890
10891 add_AT_string (die, DW_AT_name, name_string);
10892 }
a3f97cbb
JW
10893}
10894
b20b352b 10895/* Generate a DW_AT_comp_dir attribute for DIE. */
c4274b22
RH
10896
10897static void
7080f735 10898add_comp_dir_attribute (dw_die_ref die)
c4274b22 10899{
b20d9f0c 10900 const char *wd = get_src_pwd ();
c4274b22
RH
10901 if (wd != NULL)
10902 add_AT_string (die, DW_AT_comp_dir, wd);
10903}
10904
a3f97cbb 10905/* Given a tree node describing an array bound (either lower or upper) output
466446b0 10906 a representation for that bound. */
71dfc51f 10907
a3f97cbb 10908static void
7080f735 10909add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
a3f97cbb 10910{
a3f97cbb
JW
10911 switch (TREE_CODE (bound))
10912 {
10913 case ERROR_MARK:
10914 return;
10915
3ef42a0c 10916 /* All fixed-bounds are represented by INTEGER_CST nodes. */
a3f97cbb 10917 case INTEGER_CST:
665f2503
RK
10918 if (! host_integerp (bound, 0)
10919 || (bound_attr == DW_AT_lower_bound
28985b81 10920 && (((is_c_family () || is_java ()) && integer_zerop (bound))
665f2503 10921 || (is_fortran () && integer_onep (bound)))))
a1105617 10922 /* Use the default. */
665f2503 10923 ;
141719a8 10924 else
665f2503 10925 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
a3f97cbb
JW
10926 break;
10927
b1ccbc24 10928 case CONVERT_EXPR:
a3f97cbb 10929 case NOP_EXPR:
b1ccbc24 10930 case NON_LVALUE_EXPR:
ed239f5a 10931 case VIEW_CONVERT_EXPR:
b1ccbc24
RK
10932 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10933 break;
556273e0 10934
a3f97cbb 10935 case SAVE_EXPR:
a3f97cbb 10936 break;
3f76745e 10937
ef76d03b 10938 case VAR_DECL:
d8041cc8 10939 case PARM_DECL:
6de9cd9a 10940 case RESULT_DECL:
d8041cc8
RH
10941 {
10942 dw_die_ref decl_die = lookup_decl_die (bound);
10943
10944 /* ??? Can this happen, or should the variable have been bound
10945 first? Probably it can, since I imagine that we try to create
10946 the types of parameters in the order in which they exist in
0b34cf1e 10947 the list, and won't have created a forward reference to a
d8041cc8
RH
10948 later parameter. */
10949 if (decl_die != NULL)
10950 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10951 break;
10952 }
ef76d03b 10953
3f76745e 10954 default:
d8041cc8
RH
10955 {
10956 /* Otherwise try to create a stack operation procedure to
10957 evaluate the value of the array bound. */
10958
10959 dw_die_ref ctx, decl_die;
10960 dw_loc_descr_ref loc;
10961
1a186ec5 10962 loc = loc_descriptor_from_tree (bound);
d8041cc8
RH
10963 if (loc == NULL)
10964 break;
10965
e7af1d45
RK
10966 if (current_function_decl == 0)
10967 ctx = comp_unit_die;
10968 else
10969 ctx = lookup_decl_die (current_function_decl);
d8041cc8 10970
54ba1f0d 10971 decl_die = new_die (DW_TAG_variable, ctx, bound);
d8041cc8
RH
10972 add_AT_flag (decl_die, DW_AT_artificial, 1);
10973 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10974 add_AT_loc (decl_die, DW_AT_location, loc);
10975
10976 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10977 break;
10978 }
a3f97cbb
JW
10979 }
10980}
10981
10982/* Note that the block of subscript information for an array type also
10983 includes information about the element type of type given array type. */
71dfc51f 10984
a3f97cbb 10985static void
7080f735 10986add_subscript_info (dw_die_ref type_die, tree type)
a3f97cbb 10987{
081f5e7e 10988#ifndef MIPS_DEBUGGING_INFO
b3694847 10989 unsigned dimension_number;
081f5e7e 10990#endif
b3694847
SS
10991 tree lower, upper;
10992 dw_die_ref subrange_die;
a3f97cbb 10993
556273e0 10994 /* The GNU compilers represent multidimensional array types as sequences of
a3f97cbb
JW
10995 one dimensional array types whose element types are themselves array
10996 types. Here we squish that down, so that each multidimensional array
556273e0 10997 type gets only one array_type DIE in the Dwarf debugging info. The draft
a3f97cbb
JW
10998 Dwarf specification say that we are allowed to do this kind of
10999 compression in C (because there is no difference between an array or
556273e0 11000 arrays and a multidimensional array in C) but for other source languages
a3f97cbb 11001 (e.g. Ada) we probably shouldn't do this. */
71dfc51f 11002
a3f97cbb
JW
11003 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11004 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11005 We work around this by disabling this feature. See also
11006 gen_array_type_die. */
11007#ifndef MIPS_DEBUGGING_INFO
11008 for (dimension_number = 0;
11009 TREE_CODE (type) == ARRAY_TYPE;
11010 type = TREE_TYPE (type), dimension_number++)
a3f97cbb 11011#endif
2ad9852d 11012 {
b3694847 11013 tree domain = TYPE_DOMAIN (type);
a3f97cbb
JW
11014
11015 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
556273e0 11016 and (in GNU C only) variable bounds. Handle all three forms
73c68f61 11017 here. */
54ba1f0d 11018 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
a3f97cbb
JW
11019 if (domain)
11020 {
11021 /* We have an array type with specified bounds. */
11022 lower = TYPE_MIN_VALUE (domain);
11023 upper = TYPE_MAX_VALUE (domain);
11024
beb235f8 11025 /* Define the index type. */
a9d38797 11026 if (TREE_TYPE (domain))
ef76d03b
JW
11027 {
11028 /* ??? This is probably an Ada unnamed subrange type. Ignore the
11029 TREE_TYPE field. We can't emit debug info for this
11030 because it is an unnamed integral type. */
11031 if (TREE_CODE (domain) == INTEGER_TYPE
11032 && TYPE_NAME (domain) == NULL_TREE
11033 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11034 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
556273e0 11035 ;
ef76d03b
JW
11036 else
11037 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11038 type_die);
11039 }
a9d38797 11040
e1ee5cdc
RH
11041 /* ??? If upper is NULL, the array has unspecified length,
11042 but it does have a lower bound. This happens with Fortran
11043 dimension arr(N:*)
7080f735 11044 Since the debugger is definitely going to need to know N
e1ee5cdc
RH
11045 to produce useful results, go ahead and output the lower
11046 bound solo, and hope the debugger can cope. */
11047
141719a8 11048 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
e1ee5cdc
RH
11049 if (upper)
11050 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
a3f97cbb 11051 }
71dfc51f 11052
2ad9852d
RK
11053 /* Otherwise we have an array type with an unspecified length. The
11054 DWARF-2 spec does not say how to handle this; let's just leave out the
11055 bounds. */
a3f97cbb 11056 }
a3f97cbb
JW
11057}
11058
11059static void
7080f735 11060add_byte_size_attribute (dw_die_ref die, tree tree_node)
a3f97cbb 11061{
b3694847 11062 unsigned size;
a3f97cbb
JW
11063
11064 switch (TREE_CODE (tree_node))
11065 {
11066 case ERROR_MARK:
11067 size = 0;
11068 break;
11069 case ENUMERAL_TYPE:
11070 case RECORD_TYPE:
11071 case UNION_TYPE:
11072 case QUAL_UNION_TYPE:
11073 size = int_size_in_bytes (tree_node);
11074 break;
11075 case FIELD_DECL:
11076 /* For a data member of a struct or union, the DW_AT_byte_size is
73c68f61
SS
11077 generally given as the number of bytes normally allocated for an
11078 object of the *declared* type of the member itself. This is true
11079 even for bit-fields. */
a3f97cbb
JW
11080 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11081 break;
11082 default:
ced3f397 11083 gcc_unreachable ();
a3f97cbb
JW
11084 }
11085
11086 /* Note that `size' might be -1 when we get to this point. If it is, that
11087 indicates that the byte size of the entity in question is variable. We
11088 have no good way of expressing this fact in Dwarf at the present time,
11089 so just let the -1 pass on through. */
a3f97cbb
JW
11090 add_AT_unsigned (die, DW_AT_byte_size, size);
11091}
11092
11093/* For a FIELD_DECL node which represents a bit-field, output an attribute
11094 which specifies the distance in bits from the highest order bit of the
11095 "containing object" for the bit-field to the highest order bit of the
11096 bit-field itself.
11097
2ad9852d
RK
11098 For any given bit-field, the "containing object" is a hypothetical object
11099 (of some integral or enum type) within which the given bit-field lives. The
11100 type of this hypothetical "containing object" is always the same as the
11101 declared type of the individual bit-field itself. The determination of the
11102 exact location of the "containing object" for a bit-field is rather
11103 complicated. It's handled by the `field_byte_offset' function (above).
a3f97cbb
JW
11104
11105 Note that it is the size (in bytes) of the hypothetical "containing object"
11106 which will be given in the DW_AT_byte_size attribute for this bit-field.
11107 (See `byte_size_attribute' above). */
71dfc51f
RK
11108
11109static inline void
7080f735 11110add_bit_offset_attribute (dw_die_ref die, tree decl)
a3f97cbb 11111{
665f2503
RK
11112 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11113 tree type = DECL_BIT_FIELD_TYPE (decl);
11114 HOST_WIDE_INT bitpos_int;
11115 HOST_WIDE_INT highest_order_object_bit_offset;
11116 HOST_WIDE_INT highest_order_field_bit_offset;
11117 HOST_WIDE_INT unsigned bit_offset;
a3f97cbb 11118
3a88cbd1 11119 /* Must be a field and a bit field. */
ced3f397 11120 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
a3f97cbb
JW
11121
11122 /* We can't yet handle bit-fields whose offsets are variable, so if we
11123 encounter such things, just return without generating any attribute
665f2503
RK
11124 whatsoever. Likewise for variable or too large size. */
11125 if (! host_integerp (bit_position (decl), 0)
11126 || ! host_integerp (DECL_SIZE (decl), 1))
71dfc51f
RK
11127 return;
11128
665f2503 11129 bitpos_int = int_bit_position (decl);
a3f97cbb
JW
11130
11131 /* Note that the bit offset is always the distance (in bits) from the
556273e0
KH
11132 highest-order bit of the "containing object" to the highest-order bit of
11133 the bit-field itself. Since the "high-order end" of any object or field
a3f97cbb
JW
11134 is different on big-endian and little-endian machines, the computation
11135 below must take account of these differences. */
11136 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11137 highest_order_field_bit_offset = bitpos_int;
11138
71dfc51f 11139 if (! BYTES_BIG_ENDIAN)
a3f97cbb 11140 {
665f2503 11141 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
a3f97cbb
JW
11142 highest_order_object_bit_offset += simple_type_size_in_bits (type);
11143 }
71dfc51f
RK
11144
11145 bit_offset
11146 = (! BYTES_BIG_ENDIAN
11147 ? highest_order_object_bit_offset - highest_order_field_bit_offset
11148 : highest_order_field_bit_offset - highest_order_object_bit_offset);
a3f97cbb
JW
11149
11150 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11151}
11152
11153/* For a FIELD_DECL node which represents a bit field, output an attribute
11154 which specifies the length in bits of the given field. */
71dfc51f
RK
11155
11156static inline void
7080f735 11157add_bit_size_attribute (dw_die_ref die, tree decl)
a3f97cbb 11158{
3a88cbd1 11159 /* Must be a field and a bit field. */
ced3f397
NS
11160 gcc_assert (TREE_CODE (decl) == FIELD_DECL
11161 && DECL_BIT_FIELD_TYPE (decl));
665f2503
RK
11162
11163 if (host_integerp (DECL_SIZE (decl), 1))
11164 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
a3f97cbb
JW
11165}
11166
88dad228 11167/* If the compiled language is ANSI C, then add a 'prototyped'
a3f97cbb 11168 attribute, if arg types are given for the parameters of a function. */
71dfc51f
RK
11169
11170static inline void
7080f735 11171add_prototyped_attribute (dw_die_ref die, tree func_type)
a3f97cbb 11172{
88dad228
JM
11173 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11174 && TYPE_ARG_TYPES (func_type) != NULL)
11175 add_AT_flag (die, DW_AT_prototyped, 1);
a3f97cbb
JW
11176}
11177
a3f97cbb
JW
11178/* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
11179 by looking in either the type declaration or object declaration
11180 equate table. */
71dfc51f
RK
11181
11182static inline void
7080f735 11183add_abstract_origin_attribute (dw_die_ref die, tree origin)
a3f97cbb
JW
11184{
11185 dw_die_ref origin_die = NULL;
bbc6ae08 11186
d10b8e05 11187 if (TREE_CODE (origin) != FUNCTION_DECL)
e40a1c67
JM
11188 {
11189 /* We may have gotten separated from the block for the inlined
11190 function, if we're in an exception handler or some such; make
11191 sure that the abstract function has been written out.
11192
73c68f61 11193 Doing this for nested functions is wrong, however; functions are
e40a1c67 11194 distinct units, and our context might not even be inline. */
fb13d4d0 11195 tree fn = origin;
2ad9852d 11196
fb13d4d0
JM
11197 if (TYPE_P (fn))
11198 fn = TYPE_STUB_DECL (fn);
2878ea73 11199
596b98ce 11200 fn = decl_function_context (fn);
e40a1c67 11201 if (fn)
1edf43d6 11202 dwarf2out_abstract_function (fn);
e40a1c67 11203 }
44db1d9c 11204
2f939d94 11205 if (DECL_P (origin))
71dfc51f 11206 origin_die = lookup_decl_die (origin);
2f939d94 11207 else if (TYPE_P (origin))
71dfc51f
RK
11208 origin_die = lookup_type_die (origin);
11209
9acf766f
DB
11210 /* XXX: Functions that are never lowered don't always have correct block
11211 trees (in the case of java, they simply have no block tree, in some other
11212 languages). For these functions, there is nothing we can really do to
11213 output correct debug info for inlined functions in all cases. Rather
535a42b1 11214 than die, we'll just produce deficient debug info now, in that we will
9acf766f
DB
11215 have variables without a proper abstract origin. In the future, when all
11216 functions are lowered, we should re-add a gcc_assert (origin_die)
11217 here. */
556273e0 11218
9acf766f
DB
11219 if (origin_die)
11220 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
a3f97cbb
JW
11221}
11222
bdb669cb
JM
11223/* We do not currently support the pure_virtual attribute. */
11224
71dfc51f 11225static inline void
7080f735 11226add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
a3f97cbb 11227{
a94dbf2c 11228 if (DECL_VINDEX (func_decl))
a3f97cbb 11229 {
bdb669cb 11230 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
665f2503
RK
11231
11232 if (host_integerp (DECL_VINDEX (func_decl), 0))
11233 add_AT_loc (die, DW_AT_vtable_elem_location,
11234 new_loc_descr (DW_OP_constu,
11235 tree_low_cst (DECL_VINDEX (func_decl), 0),
11236 0));
71dfc51f 11237
a94dbf2c
JM
11238 /* GNU extension: Record what type this method came from originally. */
11239 if (debug_info_level > DINFO_LEVEL_TERSE)
11240 add_AT_die_ref (die, DW_AT_containing_type,
11241 lookup_type_die (DECL_CONTEXT (func_decl)));
a3f97cbb
JW
11242 }
11243}
11244\f
b2932ae5 11245/* Add source coordinate attributes for the given decl. */
71dfc51f 11246
b2932ae5 11247static void
7080f735 11248add_src_coords_attributes (dw_die_ref die, tree decl)
b2932ae5 11249{
6773e15f 11250 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
71dfc51f 11251
d5688810 11252 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
6773e15f 11253 add_AT_unsigned (die, DW_AT_decl_line, s.line);
b2932ae5
JM
11254}
11255
b20b352b 11256/* Add a DW_AT_name attribute and source coordinate attribute for the
a3f97cbb 11257 given decl, but only if it actually has a name. */
71dfc51f 11258
a3f97cbb 11259static void
7080f735 11260add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
a3f97cbb 11261{
b3694847 11262 tree decl_name;
71dfc51f 11263
556273e0 11264 decl_name = DECL_NAME (decl);
71dfc51f 11265 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
a3f97cbb 11266 {
a1d7ffe3 11267 add_name_attribute (die, dwarf2_name (decl, 0));
a96c67ec
JM
11268 if (! DECL_ARTIFICIAL (decl))
11269 add_src_coords_attributes (die, decl);
e689ae67 11270
a1d7ffe3 11271 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
bc808e0b 11272 && TREE_PUBLIC (decl)
5daf7c0a 11273 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
fcc207bf
AP
11274 && !DECL_ABSTRACT (decl)
11275 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
a1d7ffe3
JM
11276 add_AT_string (die, DW_AT_MIPS_linkage_name,
11277 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
a3f97cbb 11278 }
7a0c8d71
DR
11279
11280#ifdef VMS_DEBUGGING_INFO
7a0c8d71
DR
11281 /* Get the function's name, as described by its RTL. This may be different
11282 from the DECL_NAME name used in the source file. */
11283 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
c470afad
RK
11284 {
11285 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11286 XEXP (DECL_RTL (decl), 0));
a1bbd445 11287 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
c470afad 11288 }
7a0c8d71 11289#endif
a3f97cbb
JW
11290}
11291
556273e0 11292/* Push a new declaration scope. */
71dfc51f 11293
a3f97cbb 11294static void
7080f735 11295push_decl_scope (tree scope)
a3f97cbb 11296{
0fdc587b 11297 VEC_safe_push (tree, gc, decl_scope_table, scope);
a3f97cbb
JW
11298}
11299
777ad4c2 11300/* Pop a declaration scope. */
2ad9852d 11301
777ad4c2 11302static inline void
7080f735 11303pop_decl_scope (void)
777ad4c2 11304{
0fdc587b 11305 VEC_pop (tree, decl_scope_table);
777ad4c2
JM
11306}
11307
11308/* Return the DIE for the scope that immediately contains this type.
11309 Non-named types get global scope. Named types nested in other
11310 types get their containing scope if it's open, or global scope
11311 otherwise. All other types (i.e. function-local named types) get
11312 the current active scope. */
71dfc51f 11313
a3f97cbb 11314static dw_die_ref
7080f735 11315scope_die_for (tree t, dw_die_ref context_die)
a3f97cbb 11316{
b3694847
SS
11317 dw_die_ref scope_die = NULL;
11318 tree containing_scope;
11319 int i;
a3f97cbb 11320
777ad4c2 11321 /* Non-types always go in the current scope. */
ced3f397 11322 gcc_assert (TYPE_P (t));
777ad4c2
JM
11323
11324 containing_scope = TYPE_CONTEXT (t);
ab72d377 11325
66c78aa9 11326 /* Use the containing namespace if it was passed in (for a declaration). */
2addbe1d 11327 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
66c78aa9
JM
11328 {
11329 if (context_die == lookup_decl_die (containing_scope))
11330 /* OK */;
11331 else
11332 containing_scope = NULL_TREE;
11333 }
2addbe1d 11334
5f2f160c
JM
11335 /* Ignore function type "scopes" from the C frontend. They mean that
11336 a tagged type is local to a parmlist of a function declarator, but
11337 that isn't useful to DWARF. */
11338 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11339 containing_scope = NULL_TREE;
11340
71dfc51f
RK
11341 if (containing_scope == NULL_TREE)
11342 scope_die = comp_unit_die;
777ad4c2 11343 else if (TYPE_P (containing_scope))
348bb3c7 11344 {
777ad4c2
JM
11345 /* For types, we can just look up the appropriate DIE. But
11346 first we check to see if we're in the middle of emitting it
11347 so we know where the new DIE should go. */
0fdc587b
KH
11348 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11349 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
348bb3c7
JM
11350 break;
11351
11352 if (i < 0)
11353 {
ced3f397
NS
11354 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11355 || TREE_ASM_WRITTEN (containing_scope));
348bb3c7
JM
11356
11357 /* If none of the current dies are suitable, we get file scope. */
11358 scope_die = comp_unit_die;
11359 }
11360 else
777ad4c2 11361 scope_die = lookup_type_die (containing_scope);
348bb3c7 11362 }
a3f97cbb 11363 else
777ad4c2 11364 scope_die = context_die;
71dfc51f 11365
a3f97cbb
JW
11366 return scope_die;
11367}
11368
2ad9852d 11369/* Returns nonzero if CONTEXT_DIE is internal to a function. */
777ad4c2
JM
11370
11371static inline int
7080f735 11372local_scope_p (dw_die_ref context_die)
a3f97cbb 11373{
777ad4c2
JM
11374 for (; context_die; context_die = context_die->die_parent)
11375 if (context_die->die_tag == DW_TAG_inlined_subroutine
11376 || context_die->die_tag == DW_TAG_subprogram)
11377 return 1;
2ad9852d 11378
777ad4c2 11379 return 0;
a3f97cbb
JW
11380}
11381
66c78aa9
JM
11382/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11383 whether or not to treat a DIE in this context as a declaration. */
9765e357
JM
11384
11385static inline int
66c78aa9 11386class_or_namespace_scope_p (dw_die_ref context_die)
9765e357
JM
11387{
11388 return (context_die
11389 && (context_die->die_tag == DW_TAG_structure_type
66c78aa9
JM
11390 || context_die->die_tag == DW_TAG_union_type
11391 || context_die->die_tag == DW_TAG_namespace));
9765e357
JM
11392}
11393
a3f97cbb
JW
11394/* Many forms of DIEs require a "type description" attribute. This
11395 routine locates the proper "type descriptor" die for the type given
b20b352b 11396 by 'type', and adds a DW_AT_type attribute below the given die. */
71dfc51f 11397
a3f97cbb 11398static void
7080f735
AJ
11399add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11400 int decl_volatile, dw_die_ref context_die)
a3f97cbb 11401{
b3694847
SS
11402 enum tree_code code = TREE_CODE (type);
11403 dw_die_ref type_die = NULL;
a3f97cbb 11404
ef76d03b
JW
11405 /* ??? If this type is an unnamed subrange type of an integral or
11406 floating-point type, use the inner type. This is because we have no
11407 support for unnamed types in base_type_die. This can happen if this is
11408 an Ada subrange type. Correct solution is emit a subrange type die. */
b1ccbc24
RK
11409 if ((code == INTEGER_TYPE || code == REAL_TYPE)
11410 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11411 type = TREE_TYPE (type), code = TREE_CODE (type);
11412
2ad9852d
RK
11413 if (code == ERROR_MARK
11414 /* Handle a special case. For functions whose return type is void, we
11415 generate *no* type attribute. (Note that no object may have type
11416 `void', so this only applies to function return types). */
11417 || code == VOID_TYPE)
b1ccbc24 11418 return;
a3f97cbb 11419
a3f97cbb
JW
11420 type_die = modified_type_die (type,
11421 decl_const || TYPE_READONLY (type),
11422 decl_volatile || TYPE_VOLATILE (type),
ab72d377 11423 context_die);
2ad9852d 11424
a3f97cbb 11425 if (type_die != NULL)
71dfc51f 11426 add_AT_die_ref (object_die, DW_AT_type, type_die);
a3f97cbb
JW
11427}
11428
a1c496cb
EC
11429/* Given an object die, add the calling convention attribute for the
11430 function call type. */
11431static void
11432add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11433{
11434 enum dwarf_calling_convention value = DW_CC_normal;
11435
11436 value = targetm.dwarf_calling_convention (type);
11437
b9e6959b
EC
11438 /* Only add the attribute if the backend requests it, and
11439 is not DW_CC_normal. */
11440 if (value && (value != DW_CC_normal))
a1c496cb
EC
11441 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11442}
11443
a3f97cbb
JW
11444/* Given a tree pointer to a struct, class, union, or enum type node, return
11445 a pointer to the (string) tag name for the given type, or zero if the type
11446 was declared without a tag. */
71dfc51f 11447
d3e3972c 11448static const char *
7080f735 11449type_tag (tree type)
a3f97cbb 11450{
b3694847 11451 const char *name = 0;
a3f97cbb
JW
11452
11453 if (TYPE_NAME (type) != 0)
11454 {
b3694847 11455 tree t = 0;
a3f97cbb
JW
11456
11457 /* Find the IDENTIFIER_NODE for the type name. */
11458 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11459 t = TYPE_NAME (type);
bdb669cb 11460
556273e0 11461 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
73c68f61
SS
11462 a TYPE_DECL node, regardless of whether or not a `typedef' was
11463 involved. */
a94dbf2c
JM
11464 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11465 && ! DECL_IGNORED_P (TYPE_NAME (type)))
a0ad3539
MM
11466 {
11467 /* We want to be extra verbose. Don't call dwarf_name if
11468 DECL_NAME isn't set. The default hook for decl_printable_name
11469 doesn't like that, and in this context it's correct to return
11470 0, instead of "<anonymous>" or the like. */
11471 if (DECL_NAME (TYPE_NAME (type)))
11472 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11473 }
bdb669cb 11474
a3f97cbb 11475 /* Now get the name as a string, or invent one. */
a0ad3539 11476 if (!name && t != 0)
a94dbf2c 11477 name = IDENTIFIER_POINTER (t);
a3f97cbb 11478 }
71dfc51f 11479
a3f97cbb
JW
11480 return (name == 0 || *name == '\0') ? 0 : name;
11481}
11482
11483/* Return the type associated with a data member, make a special check
11484 for bit field types. */
71dfc51f
RK
11485
11486static inline tree
7080f735 11487member_declared_type (tree member)
a3f97cbb 11488{
71dfc51f 11489 return (DECL_BIT_FIELD_TYPE (member)
2ad9852d 11490 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
a3f97cbb
JW
11491}
11492
d291dd49 11493/* Get the decl's label, as described by its RTL. This may be different
a3f97cbb 11494 from the DECL_NAME name used in the source file. */
71dfc51f 11495
487a6e06 11496#if 0
d3e3972c 11497static const char *
7080f735 11498decl_start_label (tree decl)
a3f97cbb
JW
11499{
11500 rtx x;
d3e3972c 11501 const char *fnname;
2ad9852d 11502
a3f97cbb 11503 x = DECL_RTL (decl);
ced3f397 11504 gcc_assert (MEM_P (x));
71dfc51f 11505
a3f97cbb 11506 x = XEXP (x, 0);
ced3f397 11507 gcc_assert (GET_CODE (x) == SYMBOL_REF);
71dfc51f 11508
a3f97cbb
JW
11509 fnname = XSTR (x, 0);
11510 return fnname;
11511}
487a6e06 11512#endif
a3f97cbb 11513\f
956d6950 11514/* These routines generate the internal representation of the DIE's for
a3f97cbb 11515 the compilation unit. Debugging information is collected by walking
88dad228 11516 the declaration trees passed in from dwarf2out_decl(). */
a3f97cbb
JW
11517
11518static void
7080f735 11519gen_array_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11520{
b3694847
SS
11521 dw_die_ref scope_die = scope_die_for (type, context_die);
11522 dw_die_ref array_die;
11523 tree element_type;
bdb669cb 11524
a9d38797
JM
11525 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11526 the inner array type comes before the outer array type. Thus we must
11527 call gen_type_die before we call new_die. See below also. */
11528#ifdef MIPS_DEBUGGING_INFO
11529 gen_type_die (TREE_TYPE (type), context_die);
11530#endif
11531
54ba1f0d 11532 array_die = new_die (DW_TAG_array_type, scope_die, type);
84f0ace0
JM
11533 add_name_attribute (array_die, type_tag (type));
11534 equate_type_number_to_die (type, array_die);
11535
11536 if (TREE_CODE (type) == VECTOR_TYPE)
11537 {
11538 /* The frontend feeds us a representation for the vector as a struct
11539 containing an array. Pull out the array type. */
11540 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11541 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11542 }
a9d38797 11543
a3f97cbb
JW
11544#if 0
11545 /* We default the array ordering. SDB will probably do
11546 the right things even if DW_AT_ordering is not present. It's not even
11547 an issue until we start to get into multidimensional arrays anyway. If
11548 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11549 then we'll have to put the DW_AT_ordering attribute back in. (But if
11550 and when we find out that we need to put these in, we will only do so
11551 for multidimensional arrays. */
11552 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11553#endif
11554
a9d38797 11555#ifdef MIPS_DEBUGGING_INFO
4edb7b60
JM
11556 /* The SGI compilers handle arrays of unknown bound by setting
11557 AT_declaration and not emitting any subrange DIEs. */
a9d38797 11558 if (! TYPE_DOMAIN (type))
371e8c4f 11559 add_AT_flag (array_die, DW_AT_declaration, 1);
a9d38797
JM
11560 else
11561#endif
11562 add_subscript_info (array_die, type);
a3f97cbb 11563
a3f97cbb
JW
11564 /* Add representation of the type of the elements of this array type. */
11565 element_type = TREE_TYPE (type);
71dfc51f 11566
a3f97cbb
JW
11567 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11568 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11569 We work around this by disabling this feature. See also
11570 add_subscript_info. */
11571#ifndef MIPS_DEBUGGING_INFO
71dfc51f
RK
11572 while (TREE_CODE (element_type) == ARRAY_TYPE)
11573 element_type = TREE_TYPE (element_type);
11574
a3f97cbb 11575 gen_type_die (element_type, context_die);
a9d38797 11576#endif
a3f97cbb
JW
11577
11578 add_type_attribute (array_die, element_type, 0, 0, context_die);
89708594
CT
11579
11580 if (get_AT (array_die, DW_AT_name))
11581 add_pubtype (type, array_die);
a3f97cbb
JW
11582}
11583
d6f4ec51 11584#if 0
a3f97cbb 11585static void
7080f735 11586gen_entry_point_die (tree decl, dw_die_ref context_die)
a3f97cbb 11587{
b3694847 11588 tree origin = decl_ultimate_origin (decl);
54ba1f0d 11589 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
2ad9852d 11590
a3f97cbb 11591 if (origin != NULL)
71dfc51f 11592 add_abstract_origin_attribute (decl_die, origin);
a3f97cbb
JW
11593 else
11594 {
11595 add_name_and_src_coords_attributes (decl_die, decl);
a3f97cbb
JW
11596 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11597 0, 0, context_die);
11598 }
71dfc51f 11599
a3f97cbb 11600 if (DECL_ABSTRACT (decl))
71dfc51f 11601 equate_decl_number_to_die (decl, decl_die);
a3f97cbb 11602 else
71dfc51f 11603 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
a3f97cbb 11604}
d6f4ec51 11605#endif
a3f97cbb 11606
8a8c3656
JM
11607/* Walk through the list of incomplete types again, trying once more to
11608 emit full debugging info for them. */
11609
11610static void
7080f735 11611retry_incomplete_types (void)
8a8c3656 11612{
244a4af0 11613 int i;
2ad9852d 11614
887fb69b
KH
11615 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11616 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
8a8c3656
JM
11617}
11618
a3f97cbb 11619/* Generate a DIE to represent an inlined instance of an enumeration type. */
71dfc51f 11620
a3f97cbb 11621static void
7080f735 11622gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11623{
54ba1f0d 11624 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
2ad9852d 11625
bbc6ae08
NC
11626 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11627 be incomplete and such types are not marked. */
a3f97cbb
JW
11628 add_abstract_origin_attribute (type_die, type);
11629}
11630
11631/* Generate a DIE to represent an inlined instance of a structure type. */
71dfc51f 11632
a3f97cbb 11633static void
7080f735 11634gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11635{
54ba1f0d 11636 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
777ad4c2 11637
bbc6ae08
NC
11638 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11639 be incomplete and such types are not marked. */
a3f97cbb
JW
11640 add_abstract_origin_attribute (type_die, type);
11641}
11642
11643/* Generate a DIE to represent an inlined instance of a union type. */
71dfc51f 11644
a3f97cbb 11645static void
7080f735 11646gen_inlined_union_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11647{
54ba1f0d 11648 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
777ad4c2 11649
bbc6ae08
NC
11650 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11651 be incomplete and such types are not marked. */
a3f97cbb
JW
11652 add_abstract_origin_attribute (type_die, type);
11653}
11654
11655/* Generate a DIE to represent an enumeration type. Note that these DIEs
11656 include all of the information about the enumeration values also. Each
273dbe67
JM
11657 enumerated type name/value is listed as a child of the enumerated type
11658 DIE. */
71dfc51f 11659
de99511b 11660static dw_die_ref
7080f735 11661gen_enumeration_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11662{
b3694847 11663 dw_die_ref type_die = lookup_type_die (type);
273dbe67 11664
a3f97cbb
JW
11665 if (type_die == NULL)
11666 {
11667 type_die = new_die (DW_TAG_enumeration_type,
54ba1f0d 11668 scope_die_for (type, context_die), type);
a3f97cbb
JW
11669 equate_type_number_to_die (type, type_die);
11670 add_name_attribute (type_die, type_tag (type));
a3f97cbb 11671 }
273dbe67 11672 else if (! TYPE_SIZE (type))
de99511b 11673 return type_die;
273dbe67
JM
11674 else
11675 remove_AT (type_die, DW_AT_declaration);
11676
11677 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11678 given enum type is incomplete, do not generate the DW_AT_byte_size
11679 attribute or the DW_AT_element_list attribute. */
11680 if (TYPE_SIZE (type))
a3f97cbb 11681 {
b3694847 11682 tree link;
71dfc51f 11683
a082c85a 11684 TREE_ASM_WRITTEN (type) = 1;
273dbe67 11685 add_byte_size_attribute (type_die, type);
e9a25f70 11686 if (TYPE_STUB_DECL (type) != NULL_TREE)
b2932ae5 11687 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
71dfc51f 11688
ef76d03b
JW
11689 /* If the first reference to this type was as the return type of an
11690 inline function, then it may not have a parent. Fix this now. */
11691 if (type_die->die_parent == NULL)
11692 add_child_die (scope_die_for (type, context_die), type_die);
11693
eb34af89 11694 for (link = TYPE_VALUES (type);
273dbe67 11695 link != NULL; link = TREE_CHAIN (link))
a3f97cbb 11696 {
54ba1f0d 11697 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
5bb2ed2c 11698 tree value = TREE_VALUE (link);
71dfc51f 11699
273dbe67
JM
11700 add_name_attribute (enum_die,
11701 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
665f2503 11702
8df83eae 11703 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
5bb2ed2c
MM
11704 /* DWARF2 does not provide a way of indicating whether or
11705 not enumeration constants are signed or unsigned. GDB
11706 always assumes the values are signed, so we output all
11707 values as if they were signed. That means that
11708 enumeration constants with very large unsigned values
11709 will appear to have negative values in the debugger. */
11710 add_AT_int (enum_die, DW_AT_const_value,
11711 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
a3f97cbb
JW
11712 }
11713 }
273dbe67
JM
11714 else
11715 add_AT_flag (type_die, DW_AT_declaration, 1);
de99511b 11716
89708594
CT
11717 if (get_AT (type_die, DW_AT_name))
11718 add_pubtype (type, type_die);
11719
de99511b 11720 return type_die;
a3f97cbb
JW
11721}
11722
a3f97cbb
JW
11723/* Generate a DIE to represent either a real live formal parameter decl or to
11724 represent just the type of some formal parameter position in some function
11725 type.
71dfc51f 11726
a3f97cbb
JW
11727 Note that this routine is a bit unusual because its argument may be a
11728 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11729 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11730 node. If it's the former then this function is being called to output a
11731 DIE to represent a formal parameter object (or some inlining thereof). If
11732 it's the latter, then this function is only being called to output a
11733 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11734 argument type of some subprogram type. */
71dfc51f 11735
a94dbf2c 11736static dw_die_ref
7080f735 11737gen_formal_parameter_die (tree node, dw_die_ref context_die)
a3f97cbb 11738{
b3694847 11739 dw_die_ref parm_die
54ba1f0d 11740 = new_die (DW_TAG_formal_parameter, context_die, node);
b3694847 11741 tree origin;
71dfc51f 11742
a3f97cbb
JW
11743 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11744 {
6615c446 11745 case tcc_declaration:
a3f97cbb
JW
11746 origin = decl_ultimate_origin (node);
11747 if (origin != NULL)
a94dbf2c 11748 add_abstract_origin_attribute (parm_die, origin);
a3f97cbb
JW
11749 else
11750 {
11751 add_name_and_src_coords_attributes (parm_die, node);
11752 add_type_attribute (parm_die, TREE_TYPE (node),
11753 TREE_READONLY (node),
11754 TREE_THIS_VOLATILE (node),
11755 context_die);
bdb669cb
JM
11756 if (DECL_ARTIFICIAL (node))
11757 add_AT_flag (parm_die, DW_AT_artificial, 1);
a3f97cbb 11758 }
71dfc51f 11759
141719a8
JM
11760 equate_decl_number_to_die (node, parm_die);
11761 if (! DECL_ABSTRACT (node))
0a2d3d69 11762 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
71dfc51f 11763
a3f97cbb
JW
11764 break;
11765
6615c446 11766 case tcc_type:
71dfc51f 11767 /* We were called with some kind of a ..._TYPE node. */
a3f97cbb
JW
11768 add_type_attribute (parm_die, node, 0, 0, context_die);
11769 break;
11770
a3f97cbb 11771 default:
ced3f397 11772 gcc_unreachable ();
a3f97cbb 11773 }
71dfc51f 11774
a94dbf2c 11775 return parm_die;
a3f97cbb
JW
11776}
11777
11778/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11779 at the end of an (ANSI prototyped) formal parameters list. */
71dfc51f 11780
a3f97cbb 11781static void
7080f735 11782gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
a3f97cbb 11783{
54ba1f0d 11784 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
a3f97cbb
JW
11785}
11786
11787/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11788 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11789 parameters as specified in some function type specification (except for
1cfdcc15 11790 those which appear as part of a function *definition*). */
71dfc51f 11791
a3f97cbb 11792static void
7080f735 11793gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
a3f97cbb 11794{
b3694847
SS
11795 tree link;
11796 tree formal_type = NULL;
11797 tree first_parm_type;
5daf7c0a 11798 tree arg;
a3f97cbb 11799
5daf7c0a
JM
11800 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11801 {
11802 arg = DECL_ARGUMENTS (function_or_method_type);
11803 function_or_method_type = TREE_TYPE (function_or_method_type);
11804 }
11805 else
11806 arg = NULL_TREE;
c26fbbca 11807
5daf7c0a 11808 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
a3f97cbb 11809
556273e0 11810 /* Make our first pass over the list of formal parameter types and output a
a3f97cbb 11811 DW_TAG_formal_parameter DIE for each one. */
5daf7c0a 11812 for (link = first_parm_type; link; )
a3f97cbb 11813 {
b3694847 11814 dw_die_ref parm_die;
556273e0 11815
a3f97cbb
JW
11816 formal_type = TREE_VALUE (link);
11817 if (formal_type == void_type_node)
11818 break;
11819
11820 /* Output a (nameless) DIE to represent the formal parameter itself. */
a94dbf2c 11821 parm_die = gen_formal_parameter_die (formal_type, context_die);
5daf7c0a
JM
11822 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11823 && link == first_parm_type)
11824 || (arg && DECL_ARTIFICIAL (arg)))
a94dbf2c 11825 add_AT_flag (parm_die, DW_AT_artificial, 1);
5daf7c0a
JM
11826
11827 link = TREE_CHAIN (link);
11828 if (arg)
11829 arg = TREE_CHAIN (arg);
a3f97cbb
JW
11830 }
11831
11832 /* If this function type has an ellipsis, add a
11833 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11834 if (formal_type != void_type_node)
11835 gen_unspecified_parameters_die (function_or_method_type, context_die);
11836
556273e0 11837 /* Make our second (and final) pass over the list of formal parameter types
a3f97cbb
JW
11838 and output DIEs to represent those types (as necessary). */
11839 for (link = TYPE_ARG_TYPES (function_or_method_type);
2ad9852d 11840 link && TREE_VALUE (link);
a3f97cbb 11841 link = TREE_CHAIN (link))
2ad9852d 11842 gen_type_die (TREE_VALUE (link), context_die);
a3f97cbb
JW
11843}
11844
10a11b75
JM
11845/* We want to generate the DIE for TYPE so that we can generate the
11846 die for MEMBER, which has been defined; we will need to refer back
11847 to the member declaration nested within TYPE. If we're trying to
11848 generate minimal debug info for TYPE, processing TYPE won't do the
11849 trick; we need to attach the member declaration by hand. */
11850
11851static void
7080f735 11852gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
10a11b75
JM
11853{
11854 gen_type_die (type, context_die);
11855
11856 /* If we're trying to avoid duplicate debug info, we may not have
11857 emitted the member decl for this function. Emit it now. */
11858 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11859 && ! lookup_decl_die (member))
11860 {
a288c5cd 11861 dw_die_ref type_die;
ced3f397 11862 gcc_assert (!decl_ultimate_origin (member));
10a11b75
JM
11863
11864 push_decl_scope (type);
a288c5cd 11865 type_die = lookup_type_die (type);
10a11b75 11866 if (TREE_CODE (member) == FUNCTION_DECL)
a288c5cd
JJ
11867 gen_subprogram_die (member, type_die);
11868 else if (TREE_CODE (member) == FIELD_DECL)
11869 {
11870 /* Ignore the nameless fields that are used to skip bits but handle
11871 C++ anonymous unions and structs. */
11872 if (DECL_NAME (member) != NULL_TREE
11873 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11874 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11875 {
11876 gen_type_die (member_declared_type (member), type_die);
11877 gen_field_die (member, type_die);
11878 }
11879 }
10a11b75 11880 else
a288c5cd 11881 gen_variable_die (member, type_die);
2ad9852d 11882
10a11b75
JM
11883 pop_decl_scope ();
11884 }
11885}
11886
2ad9852d
RK
11887/* Generate the DWARF2 info for the "abstract" instance of a function which we
11888 may later generate inlined and/or out-of-line instances of. */
10a11b75 11889
e1772ac0 11890static void
7080f735 11891dwarf2out_abstract_function (tree decl)
10a11b75 11892{
b3694847 11893 dw_die_ref old_die;
777ad4c2 11894 tree save_fn;
6193b8b7 11895 struct function *save_cfun;
5daf7c0a
JM
11896 tree context;
11897 int was_abstract = DECL_ABSTRACT (decl);
11898
11899 /* Make sure we have the actual abstract inline, not a clone. */
11900 decl = DECL_ORIGIN (decl);
10a11b75 11901
c26fbbca 11902 old_die = lookup_decl_die (decl);
ae0f3477 11903 if (old_die && get_AT (old_die, DW_AT_inline))
10a11b75
JM
11904 /* We've already generated the abstract instance. */
11905 return;
11906
5daf7c0a
JM
11907 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11908 we don't get confused by DECL_ABSTRACT. */
8458e954
JS
11909 if (debug_info_level > DINFO_LEVEL_TERSE)
11910 {
11911 context = decl_class_context (decl);
11912 if (context)
11913 gen_type_die_for_member
11914 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11915 }
c26fbbca 11916
5daf7c0a 11917 /* Pretend we've just finished compiling this function. */
777ad4c2 11918 save_fn = current_function_decl;
6193b8b7 11919 save_cfun = cfun;
777ad4c2 11920 current_function_decl = decl;
6193b8b7 11921 cfun = DECL_STRUCT_FUNCTION (decl);
777ad4c2 11922
10a11b75
JM
11923 set_decl_abstract_flags (decl, 1);
11924 dwarf2out_decl (decl);
5daf7c0a
JM
11925 if (! was_abstract)
11926 set_decl_abstract_flags (decl, 0);
777ad4c2
JM
11927
11928 current_function_decl = save_fn;
6193b8b7 11929 cfun = save_cfun;
10a11b75
JM
11930}
11931
33c9159e
AH
11932/* Helper function of premark_used_types() which gets called through
11933 htab_traverse_resize().
11934
11935 Marks the DIE of a given type in *SLOT as perennial, so it never gets
11936 marked as unused by prune_unused_types. */
11937static int
11938premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11939{
11940 tree type;
11941 dw_die_ref die;
11942
11943 type = *slot;
11944 die = lookup_type_die (type);
11945 if (die != NULL)
11946 die->die_perennial_p = 1;
11947 return 1;
11948}
11949
11950/* Mark all members of used_types_hash as perennial. */
11951static void
11952premark_used_types (void)
11953{
11954 if (cfun && cfun->used_types_hash)
11955 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11956}
11957
a3f97cbb
JW
11958/* Generate a DIE to represent a declared function (either file-scope or
11959 block-local). */
71dfc51f 11960
a3f97cbb 11961static void
7080f735 11962gen_subprogram_die (tree decl, dw_die_ref context_die)
a3f97cbb
JW
11963{
11964 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
b3694847
SS
11965 tree origin = decl_ultimate_origin (decl);
11966 dw_die_ref subr_die;
b3694847
SS
11967 tree fn_arg_types;
11968 tree outer_scope;
11969 dw_die_ref old_die = lookup_decl_die (decl);
11970 int declaration = (current_function_decl != decl
66c78aa9 11971 || class_or_namespace_scope_p (context_die));
a3f97cbb 11972
6193b8b7 11973 premark_used_types ();
33c9159e 11974
2ad9852d
RK
11975 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11976 started to generate the abstract instance of an inline, decided to output
11977 its containing class, and proceeded to emit the declaration of the inline
11978 from the member list for the class. If so, DECLARATION takes priority;
11979 we'll get back to the abstract instance when done with the class. */
10a11b75 11980
1cfdcc15 11981 /* The class-scope declaration DIE must be the primary DIE. */
66c78aa9 11982 if (origin && declaration && class_or_namespace_scope_p (context_die))
1cfdcc15
JM
11983 {
11984 origin = NULL;
ced3f397 11985 gcc_assert (!old_die);
1cfdcc15
JM
11986 }
11987
d3e4cd01
JM
11988 /* Now that the C++ front end lazily declares artificial member fns, we
11989 might need to retrofit the declaration into its class. */
11990 if (!declaration && !origin && !old_die
11991 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11992 && !class_or_namespace_scope_p (context_die)
11993 && debug_info_level > DINFO_LEVEL_TERSE)
11994 old_die = force_decl_die (decl);
11995
a3f97cbb
JW
11996 if (origin != NULL)
11997 {
ced3f397 11998 gcc_assert (!declaration || local_scope_p (context_die));
10a11b75 11999
8d8238b6
JM
12000 /* Fixup die_parent for the abstract instance of a nested
12001 inline function. */
12002 if (old_die && old_die->die_parent == NULL)
12003 add_child_die (context_die, old_die);
12004
54ba1f0d 12005 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
a3f97cbb
JW
12006 add_abstract_origin_attribute (subr_die, origin);
12007 }
bdb669cb
JM
12008 else if (old_die)
12009 {
6773e15f 12010 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
d5688810 12011 struct dwarf_file_data * file_index = lookup_filename (s.file);
a94dbf2c 12012
1edf43d6
JM
12013 if (!get_AT_flag (old_die, DW_AT_declaration)
12014 /* We can have a normal definition following an inline one in the
12015 case of redefinition of GNU C extern inlines.
12016 It seems reasonable to use AT_specification in this case. */
ae0f3477 12017 && !get_AT (old_die, DW_AT_inline))
b75ab88b 12018 {
9acf766f
DB
12019 /* Detect and ignore this case, where we are trying to output
12020 something we have already output. */
ced3f397 12021 return;
b75ab88b 12022 }
4b674448
JM
12023
12024 /* If the definition comes from the same place as the declaration,
a94dbf2c
JM
12025 maybe use the old DIE. We always want the DIE for this function
12026 that has the *_pc attributes to be under comp_unit_die so the
cb9e9d8d
JM
12027 debugger can find it. We also need to do this for abstract
12028 instances of inlines, since the spec requires the out-of-line copy
12029 to have the same parent. For local class methods, this doesn't
12030 apply; we just use the old DIE. */
12031 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
a96c67ec 12032 && (DECL_ARTIFICIAL (decl)
d5688810 12033 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
a96c67ec 12034 && (get_AT_unsigned (old_die, DW_AT_decl_line)
6773e15f 12035 == (unsigned) s.line))))
bdb669cb 12036 {
4b674448
JM
12037 subr_die = old_die;
12038
6097b0c3 12039 /* Clear out the declaration attribute and the formal parameters.
a1c496cb 12040 Do not remove all children, because it is possible that this
6097b0c3
DP
12041 declaration die was forced using force_decl_die(). In such
12042 cases die that forced declaration die (e.g. TAG_imported_module)
12043 is one of the children that we do not want to remove. */
4b674448 12044 remove_AT (subr_die, DW_AT_declaration);
6097b0c3 12045 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
4b674448
JM
12046 }
12047 else
12048 {
54ba1f0d 12049 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
47fcfa7b 12050 add_AT_specification (subr_die, old_die);
d5688810
GK
12051 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12052 add_AT_file (subr_die, DW_AT_decl_file, file_index);
12053 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12054 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
bdb669cb
JM
12055 }
12056 }
a3f97cbb
JW
12057 else
12058 {
54ba1f0d 12059 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
556273e0 12060
273dbe67
JM
12061 if (TREE_PUBLIC (decl))
12062 add_AT_flag (subr_die, DW_AT_external, 1);
71dfc51f 12063
a3f97cbb 12064 add_name_and_src_coords_attributes (subr_die, decl);
4927276d
JM
12065 if (debug_info_level > DINFO_LEVEL_TERSE)
12066 {
2ad9852d
RK
12067 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12068 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12069 0, 0, context_die);
4927276d 12070 }
71dfc51f 12071
a3f97cbb 12072 add_pure_or_virtual_attribute (subr_die, decl);
273dbe67
JM
12073 if (DECL_ARTIFICIAL (decl))
12074 add_AT_flag (subr_die, DW_AT_artificial, 1);
2ad9852d 12075
a94dbf2c
JM
12076 if (TREE_PROTECTED (decl))
12077 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12078 else if (TREE_PRIVATE (decl))
12079 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
a3f97cbb 12080 }
4edb7b60 12081
a94dbf2c
JM
12082 if (declaration)
12083 {
ae0f3477 12084 if (!old_die || !get_AT (old_die, DW_AT_inline))
1edf43d6
JM
12085 {
12086 add_AT_flag (subr_die, DW_AT_declaration, 1);
12087
12088 /* The first time we see a member function, it is in the context of
12089 the class to which it belongs. We make sure of this by emitting
12090 the class first. The next time is the definition, which is
a1c496cb 12091 handled above. The two may come from the same source text.
6097b0c3
DP
12092
12093 Note that force_decl_die() forces function declaration die. It is
12094 later reused to represent definition. */
d3e4cd01 12095 equate_decl_number_to_die (decl, subr_die);
1edf43d6 12096 }
a94dbf2c
JM
12097 }
12098 else if (DECL_ABSTRACT (decl))
a3f97cbb 12099 {
1bb17c21 12100 if (DECL_DECLARED_INLINE_P (decl))
61b32c02 12101 {
2878ea73 12102 if (cgraph_function_possibly_inlined_p (decl))
61b32c02
JM
12103 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12104 else
1bb17c21 12105 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
61b32c02 12106 }
61b32c02 12107 else
1bb17c21
JH
12108 {
12109 if (cgraph_function_possibly_inlined_p (decl))
2878ea73 12110 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
1bb17c21 12111 else
2878ea73 12112 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
1bb17c21 12113 }
61b32c02 12114
a3f97cbb
JW
12115 equate_decl_number_to_die (decl, subr_die);
12116 }
12117 else if (!DECL_EXTERNAL (decl))
12118 {
35d177a2
AO
12119 HOST_WIDE_INT cfa_fb_offset;
12120
ae0f3477 12121 if (!old_die || !get_AT (old_die, DW_AT_inline))
ba7b35df 12122 equate_decl_number_to_die (decl, subr_die);
71dfc51f 12123
87c8b4be
CT
12124 if (!flag_reorder_blocks_and_partition)
12125 {
12126 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12127 current_function_funcdef_no);
12128 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12129 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12130 current_function_funcdef_no);
12131 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
2878ea73 12132
87c8b4be
CT
12133 add_pubname (decl, subr_die);
12134 add_arange (decl, subr_die);
12135 }
12136 else
12137 { /* Do nothing for now; maybe need to duplicate die, one for
12138 hot section and ond for cold section, then use the hot/cold
12139 section begin/end labels to generate the aranges... */
12140 /*
12141 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12142 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12143 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12144 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12145
12146 add_pubname (decl, subr_die);
12147 add_arange (decl, subr_die);
12148 add_arange (decl, subr_die);
12149 */
12150 }
d291dd49 12151
a3f97cbb 12152#ifdef MIPS_DEBUGGING_INFO
a3f97cbb
JW
12153 /* Add a reference to the FDE for this routine. */
12154 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12155#endif
12156
35d177a2
AO
12157 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12158
30e6f306
RH
12159 /* We define the "frame base" as the function's CFA. This is more
12160 convenient for several reasons: (1) It's stable across the prologue
12161 and epilogue, which makes it better than just a frame pointer,
12162 (2) With dwarf3, there exists a one-byte encoding that allows us
12163 to reference the .debug_frame data by proxy, but failing that,
12164 (3) We can at least reuse the code inspection and interpretation
12165 code that determines the CFA position at various points in the
12166 function. */
12167 /* ??? Use some command-line or configury switch to enable the use
12168 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
12169 consumers that understand it; fall back to "pure" dwarf2 and
12170 convert the CFA data into a location list. */
12171 {
35d177a2 12172 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
30e6f306
RH
12173 if (list->dw_loc_next)
12174 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12175 else
12176 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12177 }
12178
12179 /* Compute a displacement from the "steady-state frame pointer" to
12180 the CFA. The former is what all stack slots and argument slots
2878ea73 12181 will reference in the rtl; the later is what we've told the
30e6f306
RH
12182 debugger about. We'll need to adjust all frame_base references
12183 by this displacement. */
35d177a2 12184 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
a3f97cbb 12185
6de9cd9a 12186 if (cfun->static_chain_decl)
ef76d03b 12187 add_AT_location_description (subr_die, DW_AT_static_link,
1a186ec5 12188 loc_descriptor_from_tree (cfun->static_chain_decl));
a3f97cbb
JW
12189 }
12190
12191 /* Now output descriptions of the arguments for this function. This gets
556273e0 12192 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
a3f97cbb
JW
12193 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12194 `...' at the end of the formal parameter list. In order to find out if
12195 there was a trailing ellipsis or not, we must instead look at the type
12196 associated with the FUNCTION_DECL. This will be a node of type
12197 FUNCTION_TYPE. If the chain of type nodes hanging off of this
556273e0 12198 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
a3f97cbb 12199 an ellipsis at the end. */
71dfc51f 12200
a3f97cbb 12201 /* In the case where we are describing a mere function declaration, all we
556273e0 12202 need to do here (and all we *can* do here) is to describe the *types* of
a3f97cbb 12203 its formal parameters. */
4927276d 12204 if (debug_info_level <= DINFO_LEVEL_TERSE)
71dfc51f 12205 ;
4edb7b60 12206 else if (declaration)
5daf7c0a 12207 gen_formal_types_die (decl, subr_die);
a3f97cbb
JW
12208 else
12209 {
f9da5064 12210 /* Generate DIEs to represent all known formal parameters. */
b3694847
SS
12211 tree arg_decls = DECL_ARGUMENTS (decl);
12212 tree parm;
a3f97cbb
JW
12213
12214 /* When generating DIEs, generate the unspecified_parameters DIE
73c68f61 12215 instead if we come across the arg "__builtin_va_alist" */
a3f97cbb 12216 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
71dfc51f
RK
12217 if (TREE_CODE (parm) == PARM_DECL)
12218 {
db3cf6fb
MS
12219 if (DECL_NAME (parm)
12220 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12221 "__builtin_va_alist"))
71dfc51f
RK
12222 gen_unspecified_parameters_die (parm, subr_die);
12223 else
12224 gen_decl_die (parm, subr_die);
12225 }
a3f97cbb 12226
4fe9b91c 12227 /* Decide whether we need an unspecified_parameters DIE at the end.
73c68f61
SS
12228 There are 2 more cases to do this for: 1) the ansi ... declaration -
12229 this is detectable when the end of the arg list is not a
12230 void_type_node 2) an unprototyped function declaration (not a
12231 definition). This just means that we have no info about the
12232 parameters at all. */
a3f97cbb 12233 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
71dfc51f 12234 if (fn_arg_types != NULL)
a3f97cbb 12235 {
beb235f8 12236 /* This is the prototyped case, check for.... */
a3f97cbb 12237 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
71dfc51f 12238 gen_unspecified_parameters_die (decl, subr_die);
a3f97cbb 12239 }
71dfc51f
RK
12240 else if (DECL_INITIAL (decl) == NULL_TREE)
12241 gen_unspecified_parameters_die (decl, subr_die);
a3f97cbb
JW
12242 }
12243
12244 /* Output Dwarf info for all of the stuff within the body of the function
12245 (if it has one - it may be just a declaration). */
12246 outer_scope = DECL_INITIAL (decl);
12247
2ad9852d
RK
12248 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12249 a function. This BLOCK actually represents the outermost binding contour
12250 for the function, i.e. the contour in which the function's formal
12251 parameters and labels get declared. Curiously, it appears that the front
12252 end doesn't actually put the PARM_DECL nodes for the current function onto
12253 the BLOCK_VARS list for this outer scope, but are strung off of the
12254 DECL_ARGUMENTS list for the function instead.
12255
12256 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12257 the LABEL_DECL nodes for the function however, and we output DWARF info
12258 for those in decls_for_scope. Just within the `outer_scope' there will be
12259 a BLOCK node representing the function's outermost pair of curly braces,
12260 and any blocks used for the base and member initializers of a C++
d7248bff 12261 constructor function. */
4edb7b60 12262 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
7e23cb16 12263 {
6de9cd9a
DN
12264 /* Emit a DW_TAG_variable DIE for a named return value. */
12265 if (DECL_NAME (DECL_RESULT (decl)))
12266 gen_decl_die (DECL_RESULT (decl), subr_die);
12267
7e23cb16
JM
12268 current_function_has_inlines = 0;
12269 decls_for_scope (outer_scope, subr_die, 0);
71dfc51f 12270
ce61cc73 12271#if 0 && defined (MIPS_DEBUGGING_INFO)
7e23cb16
JM
12272 if (current_function_has_inlines)
12273 {
12274 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12275 if (! comp_unit_has_inlines)
12276 {
12277 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12278 comp_unit_has_inlines = 1;
12279 }
12280 }
12281#endif
12282 }
5c70192c
EC
12283 /* Add the calling convention attribute if requested. */
12284 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12285
a3f97cbb
JW
12286}
12287
12288/* Generate a DIE to represent a declared data object. */
71dfc51f 12289
a3f97cbb 12290static void
7080f735 12291gen_variable_die (tree decl, dw_die_ref context_die)
a3f97cbb 12292{
b3694847 12293 tree origin = decl_ultimate_origin (decl);
54ba1f0d 12294 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
71dfc51f 12295
bdb669cb 12296 dw_die_ref old_die = lookup_decl_die (decl);
9765e357 12297 int declaration = (DECL_EXTERNAL (decl)
2b402955
MM
12298 /* If DECL is COMDAT and has not actually been
12299 emitted, we cannot take its address; there
12300 might end up being no definition anywhere in
12301 the program. For example, consider the C++
12302 test case:
12303
2878ea73
MS
12304 template <class T>
12305 struct S { static const int i = 7; };
2b402955 12306
2878ea73
MS
12307 template <class T>
12308 const int S<T>::i;
12309
12310 int f() { return S<int>::i; }
2b402955 12311
2b402955
MM
12312 Here, S<int>::i is not DECL_EXTERNAL, but no
12313 definition is required, so the compiler will
2878ea73 12314 not emit a definition. */
2b402955
MM
12315 || (TREE_CODE (decl) == VAR_DECL
12316 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
66c78aa9 12317 || class_or_namespace_scope_p (context_die));
4edb7b60 12318
a3f97cbb 12319 if (origin != NULL)
71dfc51f 12320 add_abstract_origin_attribute (var_die, origin);
2ad9852d 12321
f76b8156 12322 /* Loop unrolling can create multiple blocks that refer to the same
2ad9852d
RK
12323 static variable, so we must test for the DW_AT_declaration flag.
12324
12325 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
f76b8156 12326 copy decls and set the DECL_ABSTRACT flag on them instead of
2ad9852d
RK
12327 sharing them.
12328
4424eb91
JW
12329 ??? Duplicated blocks have been rewritten to use .debug_ranges.
12330
12331 ??? The declare_in_namespace support causes us to get two DIEs for one
12332 variable, both of which are declarations. We want to avoid considering
12333 one to be a specification, so we must test that this DIE is not a
12334 declaration. */
12335 else if (old_die && TREE_STATIC (decl) && ! declaration
c26fbbca 12336 && get_AT_flag (old_die, DW_AT_declaration) == 1)
bdb669cb 12337 {
e689ae67 12338 /* This is a definition of a C++ class level static. */
47fcfa7b 12339 add_AT_specification (var_die, old_die);
bdb669cb
JM
12340 if (DECL_NAME (decl))
12341 {
6773e15f 12342 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
d5688810 12343 struct dwarf_file_data * file_index = lookup_filename (s.file);
71dfc51f 12344
d5688810
GK
12345 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12346 add_AT_file (var_die, DW_AT_decl_file, file_index);
71dfc51f 12347
d5688810 12348 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
6773e15f 12349 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
bdb669cb
JM
12350 }
12351 }
a3f97cbb
JW
12352 else
12353 {
12354 add_name_and_src_coords_attributes (var_die, decl);
2ad9852d 12355 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
a3f97cbb 12356 TREE_THIS_VOLATILE (decl), context_die);
71dfc51f 12357
273dbe67
JM
12358 if (TREE_PUBLIC (decl))
12359 add_AT_flag (var_die, DW_AT_external, 1);
71dfc51f 12360
273dbe67
JM
12361 if (DECL_ARTIFICIAL (decl))
12362 add_AT_flag (var_die, DW_AT_artificial, 1);
71dfc51f 12363
a94dbf2c
JM
12364 if (TREE_PROTECTED (decl))
12365 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12366 else if (TREE_PRIVATE (decl))
12367 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
a3f97cbb 12368 }
4edb7b60
JM
12369
12370 if (declaration)
12371 add_AT_flag (var_die, DW_AT_declaration, 1);
556273e0 12372
6097b0c3 12373 if (DECL_ABSTRACT (decl) || declaration)
4edb7b60
JM
12374 equate_decl_number_to_die (decl, var_die);
12375
12376 if (! declaration && ! DECL_ABSTRACT (decl))
a3f97cbb 12377 {
0a2d3d69 12378 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
d291dd49 12379 add_pubname (decl, var_die);
a3f97cbb 12380 }
1bfb5f8f
JM
12381 else
12382 tree_add_const_value_attribute (var_die, decl);
a3f97cbb
JW
12383}
12384
12385/* Generate a DIE to represent a label identifier. */
71dfc51f 12386
a3f97cbb 12387static void
7080f735 12388gen_label_die (tree decl, dw_die_ref context_die)
a3f97cbb 12389{
b3694847 12390 tree origin = decl_ultimate_origin (decl);
54ba1f0d 12391 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
b3694847 12392 rtx insn;
a3f97cbb 12393 char label[MAX_ARTIFICIAL_LABEL_BYTES];
71dfc51f 12394
a3f97cbb 12395 if (origin != NULL)
71dfc51f 12396 add_abstract_origin_attribute (lbl_die, origin);
a3f97cbb 12397 else
71dfc51f
RK
12398 add_name_and_src_coords_attributes (lbl_die, decl);
12399
a3f97cbb 12400 if (DECL_ABSTRACT (decl))
71dfc51f 12401 equate_decl_number_to_die (decl, lbl_die);
a3f97cbb
JW
12402 else
12403 {
d0585b99 12404 insn = DECL_RTL_IF_SET (decl);
088e7160
NC
12405
12406 /* Deleted labels are programmer specified labels which have been
6356f892 12407 eliminated because of various optimizations. We still emit them
088e7160 12408 here so that it is possible to put breakpoints on them. */
d0585b99 12409 if (insn
4b4bf941
JQ
12410 && (LABEL_P (insn)
12411 || ((NOTE_P (insn)
a38e7aa5 12412 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
a3f97cbb 12413 {
556273e0
KH
12414 /* When optimization is enabled (via -O) some parts of the compiler
12415 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
a3f97cbb
JW
12416 represent source-level labels which were explicitly declared by
12417 the user. This really shouldn't be happening though, so catch
12418 it if it ever does happen. */
ced3f397 12419 gcc_assert (!INSN_DELETED_P (insn));
71dfc51f 12420
66234570 12421 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
a3f97cbb
JW
12422 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12423 }
12424 }
12425}
12426
3e2844cb
JW
12427/* A helper function for gen_inlined_subroutine_die. Add source coordinate
12428 attributes to the DIE for a block STMT, to describe where the inlined
12429 function was called from. This is similar to add_src_coords_attributes. */
12430
12431static inline void
12432add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12433{
12434 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
3e2844cb 12435
d5688810 12436 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
3e2844cb
JW
12437 add_AT_unsigned (die, DW_AT_call_line, s.line);
12438}
12439
dfad806e
AO
12440
12441/* If STMT's abstract origin is a function declaration and STMT's
12442 first subblock's abstract origin is the function's outermost block,
12443 then we're looking at the main entry point. */
12444static bool
12445is_inlined_entry_point (tree stmt)
12446{
12447 tree decl, block;
12448
12449 if (!stmt || TREE_CODE (stmt) != BLOCK)
12450 return false;
12451
12452 decl = block_ultimate_origin (stmt);
12453
12454 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12455 return false;
12456
12457 block = BLOCK_SUBBLOCKS (stmt);
12458
12459 if (block)
12460 {
12461 if (TREE_CODE (block) != BLOCK)
12462 return false;
12463
12464 block = block_ultimate_origin (block);
12465 }
12466
12467 return block == DECL_INITIAL (decl);
12468}
12469
fdfa76bd
JW
12470/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12471 Add low_pc and high_pc attributes to the DIE for a block STMT. */
71dfc51f 12472
fdfa76bd
JW
12473static inline void
12474add_high_low_attributes (tree stmt, dw_die_ref die)
a3f97cbb 12475{
a3f97cbb 12476 char label[MAX_ARTIFICIAL_LABEL_BYTES];
71dfc51f 12477
fdfa76bd 12478 if (BLOCK_FRAGMENT_CHAIN (stmt))
a3f97cbb 12479 {
fdfa76bd 12480 tree chain;
a20612aa 12481
dfad806e
AO
12482 if (is_inlined_entry_point (stmt))
12483 {
12484 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12485 BLOCK_NUMBER (stmt));
12486 add_AT_lbl_id (die, DW_AT_entry_pc, label);
12487 }
12488
fdfa76bd 12489 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
a20612aa 12490
fdfa76bd
JW
12491 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12492 do
a20612aa 12493 {
fdfa76bd
JW
12494 add_ranges (chain);
12495 chain = BLOCK_FRAGMENT_CHAIN (chain);
a20612aa 12496 }
fdfa76bd
JW
12497 while (chain);
12498 add_ranges (NULL);
12499 }
12500 else
12501 {
12502 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12503 BLOCK_NUMBER (stmt));
12504 add_AT_lbl_id (die, DW_AT_low_pc, label);
12505 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12506 BLOCK_NUMBER (stmt));
12507 add_AT_lbl_id (die, DW_AT_high_pc, label);
a3f97cbb 12508 }
fdfa76bd
JW
12509}
12510
12511/* Generate a DIE for a lexical block. */
12512
12513static void
12514gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12515{
12516 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12517
12518 if (! BLOCK_ABSTRACT (stmt))
12519 add_high_low_attributes (stmt, stmt_die);
71dfc51f 12520
d7248bff 12521 decls_for_scope (stmt, stmt_die, depth);
a3f97cbb
JW
12522}
12523
12524/* Generate a DIE for an inlined subprogram. */
71dfc51f 12525
a3f97cbb 12526static void
7080f735 12527gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
a3f97cbb 12528{
9bdca184
AO
12529 tree decl = block_ultimate_origin (stmt);
12530
12531 /* Emit info for the abstract instance first, if we haven't yet. We
12532 must emit this even if the block is abstract, otherwise when we
12533 emit the block below (or elsewhere), we may end up trying to emit
12534 a die whose origin die hasn't been emitted, and crashing. */
12535 dwarf2out_abstract_function (decl);
12536
71dfc51f 12537 if (! BLOCK_ABSTRACT (stmt))
a3f97cbb 12538 {
b3694847 12539 dw_die_ref subr_die
54ba1f0d 12540 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
71dfc51f 12541
ab72d377 12542 add_abstract_origin_attribute (subr_die, decl);
fdfa76bd 12543 add_high_low_attributes (stmt, subr_die);
3e2844cb 12544 add_call_src_coords_attributes (stmt, subr_die);
fdfa76bd 12545
d7248bff 12546 decls_for_scope (stmt, subr_die, depth);
7e23cb16 12547 current_function_has_inlines = 1;
a3f97cbb 12548 }
06e224f7
AO
12549 else
12550 /* We may get here if we're the outer block of function A that was
12551 inlined into function B that was inlined into function C. When
12552 generating debugging info for C, dwarf2out_abstract_function(B)
12553 would mark all inlined blocks as abstract, including this one.
12554 So, we wouldn't (and shouldn't) expect labels to be generated
12555 for this one. Instead, just emit debugging info for
12556 declarations within the block. This is particularly important
12557 in the case of initializers of arguments passed from B to us:
12558 if they're statement expressions containing declarations, we
12559 wouldn't generate dies for their abstract variables, and then,
12560 when generating dies for the real variables, we'd die (pun
12561 intended :-) */
12562 gen_lexical_block_die (stmt, context_die, depth);
a3f97cbb
JW
12563}
12564
12565/* Generate a DIE for a field in a record, or structure. */
71dfc51f 12566
a3f97cbb 12567static void
7080f735 12568gen_field_die (tree decl, dw_die_ref context_die)
a3f97cbb 12569{
a53efda2 12570 dw_die_ref decl_die;
71dfc51f 12571
a53efda2
JZ
12572 if (TREE_TYPE (decl) == error_mark_node)
12573 return;
7080f735 12574
a53efda2 12575 decl_die = new_die (DW_TAG_member, context_die, decl);
a3f97cbb 12576 add_name_and_src_coords_attributes (decl_die, decl);
a3f97cbb
JW
12577 add_type_attribute (decl_die, member_declared_type (decl),
12578 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12579 context_die);
71dfc51f 12580
a3f97cbb
JW
12581 if (DECL_BIT_FIELD_TYPE (decl))
12582 {
12583 add_byte_size_attribute (decl_die, decl);
12584 add_bit_size_attribute (decl_die, decl);
12585 add_bit_offset_attribute (decl_die, decl);
12586 }
71dfc51f 12587
a94dbf2c
JM
12588 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12589 add_data_member_location_attribute (decl_die, decl);
71dfc51f 12590
273dbe67
JM
12591 if (DECL_ARTIFICIAL (decl))
12592 add_AT_flag (decl_die, DW_AT_artificial, 1);
71dfc51f 12593
a94dbf2c
JM
12594 if (TREE_PROTECTED (decl))
12595 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12596 else if (TREE_PRIVATE (decl))
12597 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
90300b8c
DP
12598
12599 /* Equate decl number to die, so that we can look up this decl later on. */
12600 equate_decl_number_to_die (decl, decl_die);
a3f97cbb
JW
12601}
12602
ab72d377
JM
12603#if 0
12604/* Don't generate either pointer_type DIEs or reference_type DIEs here.
12605 Use modified_type_die instead.
a3f97cbb
JW
12606 We keep this code here just in case these types of DIEs may be needed to
12607 represent certain things in other languages (e.g. Pascal) someday. */
2ad9852d 12608
a3f97cbb 12609static void
7080f735 12610gen_pointer_type_die (tree type, dw_die_ref context_die)
a3f97cbb 12611{
b3694847 12612 dw_die_ref ptr_die
54ba1f0d 12613 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
71dfc51f 12614
a3f97cbb 12615 equate_type_number_to_die (type, ptr_die);
a3f97cbb 12616 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
ab72d377 12617 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
a3f97cbb
JW
12618}
12619
ab72d377
JM
12620/* Don't generate either pointer_type DIEs or reference_type DIEs here.
12621 Use modified_type_die instead.
a3f97cbb
JW
12622 We keep this code here just in case these types of DIEs may be needed to
12623 represent certain things in other languages (e.g. Pascal) someday. */
2ad9852d 12624
a3f97cbb 12625static void
7080f735 12626gen_reference_type_die (tree type, dw_die_ref context_die)
a3f97cbb 12627{
b3694847 12628 dw_die_ref ref_die
54ba1f0d 12629 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
71dfc51f 12630
a3f97cbb 12631 equate_type_number_to_die (type, ref_die);
a3f97cbb 12632 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
ab72d377 12633 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
a3f97cbb 12634}
ab72d377 12635#endif
a3f97cbb
JW
12636
12637/* Generate a DIE for a pointer to a member type. */
2ad9852d 12638
a3f97cbb 12639static void
7080f735 12640gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
a3f97cbb 12641{
b3694847 12642 dw_die_ref ptr_die
54ba1f0d
RH
12643 = new_die (DW_TAG_ptr_to_member_type,
12644 scope_die_for (type, context_die), type);
71dfc51f 12645
a3f97cbb 12646 equate_type_number_to_die (type, ptr_die);
a3f97cbb 12647 add_AT_die_ref (ptr_die, DW_AT_containing_type,
bdb669cb 12648 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
a3f97cbb
JW
12649 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12650}
12651
12652/* Generate the DIE for the compilation unit. */
71dfc51f 12653
a96c67ec 12654static dw_die_ref
7080f735 12655gen_compile_unit_die (const char *filename)
a3f97cbb 12656{
b3694847 12657 dw_die_ref die;
a3f97cbb 12658 char producer[250];
3ac88239 12659 const char *language_string = lang_hooks.name;
a96c67ec 12660 int language;
a3f97cbb 12661
54ba1f0d 12662 die = new_die (DW_TAG_compile_unit, NULL, NULL);
bdb669cb 12663
c4274b22
RH
12664 if (filename)
12665 {
12666 add_name_attribute (die, filename);
e3091a5f 12667 /* Don't add cwd for <built-in>. */
1d2c2b96 12668 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
c4274b22
RH
12669 add_comp_dir_attribute (die);
12670 }
a3f97cbb
JW
12671
12672 sprintf (producer, "%s %s", language_string, version_string);
12673
12674#ifdef MIPS_DEBUGGING_INFO
12675 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12676 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12677 not appear in the producer string, the debugger reaches the conclusion
12678 that the object file is stripped and has no debugging information.
12679 To get the MIPS/SGI debugger to believe that there is debugging
12680 information in the object file, we add a -g to the producer string. */
4927276d
JM
12681 if (debug_info_level > DINFO_LEVEL_TERSE)
12682 strcat (producer, " -g");
a3f97cbb
JW
12683#endif
12684
a96c67ec 12685 add_AT_string (die, DW_AT_producer, producer);
a9d38797 12686
a3f97cbb 12687 if (strcmp (language_string, "GNU C++") == 0)
a96c67ec 12688 language = DW_LANG_C_plus_plus;
a3f97cbb 12689 else if (strcmp (language_string, "GNU Ada") == 0)
8cb5fbbf 12690 language = DW_LANG_Ada95;
a9d38797 12691 else if (strcmp (language_string, "GNU F77") == 0)
a96c67ec 12692 language = DW_LANG_Fortran77;
6de9cd9a
DN
12693 else if (strcmp (language_string, "GNU F95") == 0)
12694 language = DW_LANG_Fortran95;
bc28c45b 12695 else if (strcmp (language_string, "GNU Pascal") == 0)
a96c67ec 12696 language = DW_LANG_Pascal83;
28985b81
AG
12697 else if (strcmp (language_string, "GNU Java") == 0)
12698 language = DW_LANG_Java;
9e51ddaa
GK
12699 else if (strcmp (language_string, "GNU Objective-C") == 0)
12700 language = DW_LANG_ObjC;
12701 else if (strcmp (language_string, "GNU Objective-C++") == 0)
12702 language = DW_LANG_ObjC_plus_plus;
a3f97cbb 12703 else
a96c67ec 12704 language = DW_LANG_C89;
a9d38797 12705
a96c67ec 12706 add_AT_unsigned (die, DW_AT_language, language);
a96c67ec 12707 return die;
a3f97cbb
JW
12708}
12709
61b32c02 12710/* Generate the DIE for a base class. */
71dfc51f 12711
61b32c02 12712static void
7080f735 12713gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
61b32c02 12714{
54ba1f0d 12715 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
71dfc51f 12716
61b32c02
JM
12717 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12718 add_data_member_location_attribute (die, binfo);
71dfc51f 12719
809e3e7f 12720 if (BINFO_VIRTUAL_P (binfo))
61b32c02 12721 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
2ad9852d 12722
dbbf88d1 12723 if (access == access_public_node)
61b32c02 12724 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
dbbf88d1 12725 else if (access == access_protected_node)
61b32c02
JM
12726 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12727}
12728
956d6950 12729/* Generate a DIE for a class member. */
71dfc51f 12730
a3f97cbb 12731static void
7080f735 12732gen_member_die (tree type, dw_die_ref context_die)
a3f97cbb 12733{
b3694847 12734 tree member;
dbbf88d1 12735 tree binfo = TYPE_BINFO (type);
10a11b75 12736 dw_die_ref child;
71dfc51f 12737
a3f97cbb
JW
12738 /* If this is not an incomplete type, output descriptions of each of its
12739 members. Note that as we output the DIEs necessary to represent the
12740 members of this record or union type, we will also be trying to output
12741 DIEs to represent the *types* of those members. However the `type'
556273e0 12742 function (above) will specifically avoid generating type DIEs for member
eaec9b3d 12743 types *within* the list of member DIEs for this (containing) type except
a3f97cbb
JW
12744 for those types (of members) which are explicitly marked as also being
12745 members of this (containing) type themselves. The g++ front- end can
2ad9852d
RK
12746 force any given type to be treated as a member of some other (containing)
12747 type by setting the TYPE_CONTEXT of the given (member) type to point to
12748 the TREE node representing the appropriate (containing) type. */
a3f97cbb 12749
61b32c02 12750 /* First output info about the base classes. */
fa743e8c 12751 if (binfo)
a3f97cbb 12752 {
d4e6fecb 12753 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
b3694847 12754 int i;
fa743e8c 12755 tree base;
61b32c02 12756
fa743e8c
NS
12757 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12758 gen_inheritance_die (base,
63d1c7b3 12759 (accesses ? VEC_index (tree, accesses, i)
dbbf88d1 12760 : access_public_node), context_die);
a3f97cbb
JW
12761 }
12762
61b32c02
JM
12763 /* Now output info about the data members and type members. */
12764 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
10a11b75
JM
12765 {
12766 /* If we thought we were generating minimal debug info for TYPE
12767 and then changed our minds, some of the member declarations
12768 may have already been defined. Don't define them again, but
12769 do put them in the right order. */
12770
12771 child = lookup_decl_die (member);
12772 if (child)
12773 splice_child_die (context_die, child);
12774 else
12775 gen_decl_die (member, context_die);
12776 }
61b32c02 12777
a3f97cbb 12778 /* Now output info about the function members (if any). */
61b32c02 12779 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
10a11b75 12780 {
5daf7c0a
JM
12781 /* Don't include clones in the member list. */
12782 if (DECL_ABSTRACT_ORIGIN (member))
12783 continue;
12784
10a11b75
JM
12785 child = lookup_decl_die (member);
12786 if (child)
12787 splice_child_die (context_die, child);
12788 else
12789 gen_decl_die (member, context_die);
12790 }
a3f97cbb
JW
12791}
12792
10a11b75
JM
12793/* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12794 is set, we pretend that the type was never defined, so we only get the
12795 member DIEs needed by later specification DIEs. */
71dfc51f 12796
a3f97cbb 12797static void
39ef6592
LC
12798gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12799 enum debug_info_usage usage)
a3f97cbb 12800{
b3694847
SS
12801 dw_die_ref type_die = lookup_type_die (type);
12802 dw_die_ref scope_die = 0;
12803 int nested = 0;
10a11b75 12804 int complete = (TYPE_SIZE (type)
65e1263a
JW
12805 && (! TYPE_STUB_DECL (type)
12806 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
66c78aa9 12807 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
39ef6592 12808 complete = complete && should_emit_struct_debug (type, usage);
273dbe67 12809
10a11b75 12810 if (type_die && ! complete)
273dbe67 12811 return;
a082c85a 12812
71dfc51f 12813 if (TYPE_CONTEXT (type) != NULL_TREE
66c78aa9
JM
12814 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12815 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
a082c85a
JM
12816 nested = 1;
12817
a94dbf2c 12818 scope_die = scope_die_for (type, context_die);
a082c85a
JM
12819
12820 if (! type_die || (nested && scope_die == comp_unit_die))
273dbe67 12821 /* First occurrence of type or toplevel definition of nested class. */
a3f97cbb 12822 {
b3694847 12823 dw_die_ref old_die = type_die;
71dfc51f 12824
a3f97cbb
JW
12825 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12826 ? DW_TAG_structure_type : DW_TAG_union_type,
54ba1f0d 12827 scope_die, type);
a3f97cbb 12828 equate_type_number_to_die (type, type_die);
273dbe67 12829 if (old_die)
47fcfa7b 12830 add_AT_specification (type_die, old_die);
5de0e8d4
JM
12831 else
12832 add_name_attribute (type_die, type_tag (type));
a3f97cbb 12833 }
4b674448 12834 else
273dbe67 12835 remove_AT (type_die, DW_AT_declaration);
a3f97cbb
JW
12836
12837 /* If this type has been completed, then give it a byte_size attribute and
12838 then give a list of members. */
66c78aa9 12839 if (complete && !ns_decl)
a3f97cbb 12840 {
556273e0 12841 /* Prevent infinite recursion in cases where the type of some member of
73c68f61 12842 this type is expressed in terms of this type itself. */
a3f97cbb 12843 TREE_ASM_WRITTEN (type) = 1;
273dbe67 12844 add_byte_size_attribute (type_die, type);
e9a25f70 12845 if (TYPE_STUB_DECL (type) != NULL_TREE)
b2932ae5 12846 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
71dfc51f 12847
ef76d03b
JW
12848 /* If the first reference to this type was as the return type of an
12849 inline function, then it may not have a parent. Fix this now. */
12850 if (type_die->die_parent == NULL)
12851 add_child_die (scope_die, type_die);
12852
273dbe67
JM
12853 push_decl_scope (type);
12854 gen_member_die (type, type_die);
12855 pop_decl_scope ();
71dfc51f 12856
a94dbf2c
JM
12857 /* GNU extension: Record what type our vtable lives in. */
12858 if (TYPE_VFIELD (type))
12859 {
12860 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
71dfc51f 12861
de6e505e
JM
12862 gen_type_die (vtype, context_die);
12863 add_AT_die_ref (type_die, DW_AT_containing_type,
12864 lookup_type_die (vtype));
a94dbf2c 12865 }
a3f97cbb 12866 }
4b674448 12867 else
8a8c3656
JM
12868 {
12869 add_AT_flag (type_die, DW_AT_declaration, 1);
a30d4514 12870
9765e357 12871 /* We don't need to do this for function-local types. */
9702143f
RK
12872 if (TYPE_STUB_DECL (type)
12873 && ! decl_function_context (TYPE_STUB_DECL (type)))
887fb69b 12874 VEC_safe_push (tree, gc, incomplete_types, type);
8a8c3656 12875 }
89708594
CT
12876
12877 if (get_AT (type_die, DW_AT_name))
12878 add_pubtype (type, type_die);
a3f97cbb
JW
12879}
12880
12881/* Generate a DIE for a subroutine _type_. */
71dfc51f 12882
a3f97cbb 12883static void
7080f735 12884gen_subroutine_type_die (tree type, dw_die_ref context_die)
a3f97cbb 12885{
b3694847
SS
12886 tree return_type = TREE_TYPE (type);
12887 dw_die_ref subr_die
54ba1f0d
RH
12888 = new_die (DW_TAG_subroutine_type,
12889 scope_die_for (type, context_die), type);
71dfc51f 12890
a3f97cbb
JW
12891 equate_type_number_to_die (type, subr_die);
12892 add_prototyped_attribute (subr_die, type);
a3f97cbb 12893 add_type_attribute (subr_die, return_type, 0, 0, context_die);
a94dbf2c 12894 gen_formal_types_die (type, subr_die);
89708594
CT
12895
12896 if (get_AT (subr_die, DW_AT_name))
12897 add_pubtype (type, subr_die);
a3f97cbb
JW
12898}
12899
f9da5064 12900/* Generate a DIE for a type definition. */
71dfc51f 12901
a3f97cbb 12902static void
7080f735 12903gen_typedef_die (tree decl, dw_die_ref context_die)
a3f97cbb 12904{
b3694847
SS
12905 dw_die_ref type_die;
12906 tree origin;
a94dbf2c
JM
12907
12908 if (TREE_ASM_WRITTEN (decl))
12909 return;
a94dbf2c 12910
2ad9852d 12911 TREE_ASM_WRITTEN (decl) = 1;
54ba1f0d 12912 type_die = new_die (DW_TAG_typedef, context_die, decl);
a94dbf2c 12913 origin = decl_ultimate_origin (decl);
a3f97cbb 12914 if (origin != NULL)
a94dbf2c 12915 add_abstract_origin_attribute (type_die, origin);
a3f97cbb
JW
12916 else
12917 {
b3694847 12918 tree type;
2ad9852d 12919
a3f97cbb 12920 add_name_and_src_coords_attributes (type_die, decl);
a94dbf2c
JM
12921 if (DECL_ORIGINAL_TYPE (decl))
12922 {
12923 type = DECL_ORIGINAL_TYPE (decl);
62e3bf54 12924
ced3f397
NS
12925 gcc_assert (type != TREE_TYPE (decl));
12926 equate_type_number_to_die (TREE_TYPE (decl), type_die);
a94dbf2c
JM
12927 }
12928 else
12929 type = TREE_TYPE (decl);
2ad9852d 12930
a94dbf2c
JM
12931 add_type_attribute (type_die, type, TREE_READONLY (decl),
12932 TREE_THIS_VOLATILE (decl), context_die);
a3f97cbb 12933 }
71dfc51f 12934
a3f97cbb 12935 if (DECL_ABSTRACT (decl))
a94dbf2c 12936 equate_decl_number_to_die (decl, type_die);
89708594
CT
12937
12938 if (get_AT (type_die, DW_AT_name))
12939 add_pubtype (decl, type_die);
a3f97cbb
JW
12940}
12941
12942/* Generate a type description DIE. */
71dfc51f 12943
a3f97cbb 12944static void
39ef6592
LC
12945gen_type_die_with_usage (tree type, dw_die_ref context_die,
12946 enum debug_info_usage usage)
a3f97cbb 12947{
348bb3c7
JM
12948 int need_pop;
12949
71dfc51f
RK
12950 if (type == NULL_TREE || type == error_mark_node)
12951 return;
a3f97cbb 12952
a94dbf2c
JM
12953 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12954 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
556273e0 12955 {
5d7bed9d
DJ
12956 if (TREE_ASM_WRITTEN (type))
12957 return;
12958
29b91443 12959 /* Prevent broken recursion; we can't hand off to the same type. */
ced3f397 12960 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
29b91443 12961
a94dbf2c
JM
12962 TREE_ASM_WRITTEN (type) = 1;
12963 gen_decl_die (TYPE_NAME (type), context_die);
12964 return;
12965 }
12966
5d7bed9d
DJ
12967 /* We are going to output a DIE to represent the unqualified version
12968 of this type (i.e. without any const or volatile qualifiers) so
12969 get the main variant (i.e. the unqualified version) of this type
12970 now. (Vectors are special because the debugging info is in the
12971 cloned type itself). */
12972 if (TREE_CODE (type) != VECTOR_TYPE)
12973 type = type_main_variant (type);
12974
12975 if (TREE_ASM_WRITTEN (type))
12976 return;
12977
a3f97cbb
JW
12978 switch (TREE_CODE (type))
12979 {
12980 case ERROR_MARK:
12981 break;
12982
12983 case POINTER_TYPE:
12984 case REFERENCE_TYPE:
956d6950
JL
12985 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12986 ensures that the gen_type_die recursion will terminate even if the
12987 type is recursive. Recursive types are possible in Ada. */
12988 /* ??? We could perhaps do this for all types before the switch
12989 statement. */
12990 TREE_ASM_WRITTEN (type) = 1;
12991
a3f97cbb 12992 /* For these types, all that is required is that we output a DIE (or a
73c68f61 12993 set of DIEs) to represent the "basis" type. */
39ef6592
LC
12994 gen_type_die_with_usage (TREE_TYPE (type), context_die,
12995 DINFO_USAGE_IND_USE);
a3f97cbb
JW
12996 break;
12997
12998 case OFFSET_TYPE:
556273e0 12999 /* This code is used for C++ pointer-to-data-member types.
71dfc51f 13000 Output a description of the relevant class type. */
39ef6592
LC
13001 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13002 DINFO_USAGE_IND_USE);
71dfc51f 13003
a3f97cbb 13004 /* Output a description of the type of the object pointed to. */
39ef6592
LC
13005 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13006 DINFO_USAGE_IND_USE);
71dfc51f 13007
a3f97cbb 13008 /* Now output a DIE to represent this pointer-to-data-member type
73c68f61 13009 itself. */
a3f97cbb
JW
13010 gen_ptr_to_mbr_type_die (type, context_die);
13011 break;
13012
a3f97cbb
JW
13013 case FUNCTION_TYPE:
13014 /* Force out return type (in case it wasn't forced out already). */
39ef6592
LC
13015 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13016 DINFO_USAGE_DIR_USE);
a3f97cbb
JW
13017 gen_subroutine_type_die (type, context_die);
13018 break;
13019
13020 case METHOD_TYPE:
13021 /* Force out return type (in case it wasn't forced out already). */
39ef6592
LC
13022 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13023 DINFO_USAGE_DIR_USE);
a3f97cbb
JW
13024 gen_subroutine_type_die (type, context_die);
13025 break;
13026
13027 case ARRAY_TYPE:
71d59383 13028 gen_array_type_die (type, context_die);
a3f97cbb
JW
13029 break;
13030
4061f623 13031 case VECTOR_TYPE:
84f0ace0 13032 gen_array_type_die (type, context_die);
4061f623
BS
13033 break;
13034
a3f97cbb
JW
13035 case ENUMERAL_TYPE:
13036 case RECORD_TYPE:
13037 case UNION_TYPE:
13038 case QUAL_UNION_TYPE:
2ad9852d 13039 /* If this is a nested type whose containing class hasn't been written
73c68f61
SS
13040 out yet, writing it out will cover this one, too. This does not apply
13041 to instantiations of member class templates; they need to be added to
13042 the containing class as they are generated. FIXME: This hurts the
13043 idea of combining type decls from multiple TUs, since we can't predict
13044 what set of template instantiations we'll get. */
a082c85a 13045 if (TYPE_CONTEXT (type)
5f2f160c 13046 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
a082c85a 13047 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
a94dbf2c 13048 {
39ef6592 13049 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
a94dbf2c 13050
348bb3c7 13051 if (TREE_ASM_WRITTEN (type))
a94dbf2c
JM
13052 return;
13053
13054 /* If that failed, attach ourselves to the stub. */
13055 push_decl_scope (TYPE_CONTEXT (type));
13056 context_die = lookup_type_die (TYPE_CONTEXT (type));
348bb3c7 13057 need_pop = 1;
a94dbf2c 13058 }
348bb3c7 13059 else
66c78aa9
JM
13060 {
13061 declare_in_namespace (type, context_die);
13062 need_pop = 0;
13063 }
a94dbf2c
JM
13064
13065 if (TREE_CODE (type) == ENUMERAL_TYPE)
ae64e535
ILT
13066 {
13067 /* This might have been written out by the call to
13068 declare_in_namespace. */
13069 if (!TREE_ASM_WRITTEN (type))
13070 gen_enumeration_type_die (type, context_die);
13071 }
a3f97cbb 13072 else
39ef6592 13073 gen_struct_or_union_type_die (type, context_die, usage);
4b674448 13074
348bb3c7 13075 if (need_pop)
a94dbf2c
JM
13076 pop_decl_scope ();
13077
4b674448 13078 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
a082c85a
JM
13079 it up if it is ever completed. gen_*_type_die will set it for us
13080 when appropriate. */
13081 return;
a3f97cbb
JW
13082
13083 case VOID_TYPE:
13084 case INTEGER_TYPE:
13085 case REAL_TYPE:
13086 case COMPLEX_TYPE:
13087 case BOOLEAN_TYPE:
a3f97cbb
JW
13088 /* No DIEs needed for fundamental types. */
13089 break;
13090
13091 case LANG_TYPE:
13092 /* No Dwarf representation currently defined. */
13093 break;
13094
13095 default:
ced3f397 13096 gcc_unreachable ();
a3f97cbb
JW
13097 }
13098
13099 TREE_ASM_WRITTEN (type) = 1;
13100}
13101
39ef6592
LC
13102static void
13103gen_type_die (tree type, dw_die_ref context_die)
13104{
13105 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13106}
13107
a3f97cbb 13108/* Generate a DIE for a tagged type instantiation. */
71dfc51f 13109
a3f97cbb 13110static void
7080f735 13111gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
a3f97cbb 13112{
71dfc51f
RK
13113 if (type == NULL_TREE || type == error_mark_node)
13114 return;
a3f97cbb 13115
38e01259 13116 /* We are going to output a DIE to represent the unqualified version of
a3f97cbb
JW
13117 this type (i.e. without any const or volatile qualifiers) so make sure
13118 that we have the main variant (i.e. the unqualified version) of this
13119 type now. */
ced3f397 13120 gcc_assert (type == type_main_variant (type));
a3f97cbb 13121
203588e7 13122 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
bbc6ae08 13123 an instance of an unresolved type. */
556273e0 13124
a3f97cbb
JW
13125 switch (TREE_CODE (type))
13126 {
13127 case ERROR_MARK:
13128 break;
13129
13130 case ENUMERAL_TYPE:
13131 gen_inlined_enumeration_type_die (type, context_die);
13132 break;
13133
13134 case RECORD_TYPE:
13135 gen_inlined_structure_type_die (type, context_die);
13136 break;
13137
13138 case UNION_TYPE:
13139 case QUAL_UNION_TYPE:
13140 gen_inlined_union_type_die (type, context_die);
13141 break;
13142
13143 default:
ced3f397 13144 gcc_unreachable ();
a3f97cbb
JW
13145 }
13146}
13147
13148/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13149 things which are local to the given block. */
71dfc51f 13150
a3f97cbb 13151static void
7080f735 13152gen_block_die (tree stmt, dw_die_ref context_die, int depth)
a3f97cbb 13153{
b3694847
SS
13154 int must_output_die = 0;
13155 tree origin;
13156 tree decl;
13157 enum tree_code origin_code;
a3f97cbb 13158
9acf766f
DB
13159 /* Ignore blocks that are NULL. */
13160 if (stmt == NULL_TREE)
71dfc51f 13161 return;
a3f97cbb 13162
a20612aa
RH
13163 /* If the block is one fragment of a non-contiguous block, do not
13164 process the variables, since they will have been done by the
13165 origin block. Do process subblocks. */
13166 if (BLOCK_FRAGMENT_ORIGIN (stmt))
13167 {
13168 tree sub;
13169
2ad9852d 13170 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
a20612aa 13171 gen_block_die (sub, context_die, depth + 1);
2ad9852d 13172
a20612aa
RH
13173 return;
13174 }
13175
a3f97cbb
JW
13176 /* Determine the "ultimate origin" of this block. This block may be an
13177 inlined instance of an inlined instance of inline function, so we have
13178 to trace all of the way back through the origin chain to find out what
13179 sort of node actually served as the original seed for the creation of
13180 the current block. */
13181 origin = block_ultimate_origin (stmt);
13182 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13183
13184 /* Determine if we need to output any Dwarf DIEs at all to represent this
13185 block. */
13186 if (origin_code == FUNCTION_DECL)
71dfc51f
RK
13187 /* The outer scopes for inlinings *must* always be represented. We
13188 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
13189 must_output_die = 1;
a3f97cbb
JW
13190 else
13191 {
13192 /* In the case where the current block represents an inlining of the
73c68f61
SS
13193 "body block" of an inline function, we must *NOT* output any DIE for
13194 this block because we have already output a DIE to represent the whole
13195 inlined function scope and the "body block" of any function doesn't
13196 really represent a different scope according to ANSI C rules. So we
13197 check here to make sure that this block does not represent a "body
13198 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
d7248bff 13199 if (! is_body_block (origin ? origin : stmt))
a3f97cbb
JW
13200 {
13201 /* Determine if this block directly contains any "significant"
13202 local declarations which we will need to output DIEs for. */
13203 if (debug_info_level > DINFO_LEVEL_TERSE)
71dfc51f
RK
13204 /* We are not in terse mode so *any* local declaration counts
13205 as being a "significant" one. */
2878ea73
MS
13206 must_output_die = (BLOCK_VARS (stmt) != NULL
13207 && (TREE_USED (stmt)
9acf766f
DB
13208 || TREE_ASM_WRITTEN (stmt)
13209 || BLOCK_ABSTRACT (stmt)));
a3f97cbb 13210 else
71dfc51f
RK
13211 /* We are in terse mode, so only local (nested) function
13212 definitions count as "significant" local declarations. */
13213 for (decl = BLOCK_VARS (stmt);
13214 decl != NULL; decl = TREE_CHAIN (decl))
13215 if (TREE_CODE (decl) == FUNCTION_DECL
13216 && DECL_INITIAL (decl))
a3f97cbb 13217 {
71dfc51f
RK
13218 must_output_die = 1;
13219 break;
a3f97cbb 13220 }
a3f97cbb
JW
13221 }
13222 }
13223
13224 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13225 DIE for any block which contains no significant local declarations at
13226 all. Rather, in such cases we just call `decls_for_scope' so that any
13227 needed Dwarf info for any sub-blocks will get properly generated. Note
13228 that in terse mode, our definition of what constitutes a "significant"
13229 local declaration gets restricted to include only inlined function
13230 instances and local (nested) function definitions. */
13231 if (must_output_die)
13232 {
13233 if (origin_code == FUNCTION_DECL)
71dfc51f 13234 gen_inlined_subroutine_die (stmt, context_die, depth);
a3f97cbb 13235 else
71dfc51f 13236 gen_lexical_block_die (stmt, context_die, depth);
a3f97cbb
JW
13237 }
13238 else
d7248bff 13239 decls_for_scope (stmt, context_die, depth);
a3f97cbb
JW
13240}
13241
13242/* Generate all of the decls declared within a given scope and (recursively)
9ec36da5 13243 all of its sub-blocks. */
71dfc51f 13244
a3f97cbb 13245static void
7080f735 13246decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
a3f97cbb 13247{
b3694847
SS
13248 tree decl;
13249 tree subblocks;
71dfc51f 13250
9acf766f
DB
13251 /* Ignore NULL blocks. */
13252 if (stmt == NULL_TREE)
71dfc51f
RK
13253 return;
13254
9acf766f 13255 if (TREE_USED (stmt))
a3f97cbb 13256 {
9acf766f
DB
13257 /* Output the DIEs to represent all of the data objects and typedefs
13258 declared directly within this block but not within any nested
13259 sub-blocks. Also, nested function and tag DIEs have been
13260 generated with a parent of NULL; fix that up now. */
13261 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13262 {
13263 dw_die_ref die;
2878ea73 13264
9acf766f
DB
13265 if (TREE_CODE (decl) == FUNCTION_DECL)
13266 die = lookup_decl_die (decl);
13267 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13268 die = lookup_type_die (TREE_TYPE (decl));
13269 else
13270 die = NULL;
2878ea73 13271
9acf766f
DB
13272 if (die != NULL && die->die_parent == NULL)
13273 add_child_die (context_die, die);
30be951a
JH
13274 /* Do not produce debug information for static variables since
13275 these might be optimized out. We are called for these later
8a4a83ed 13276 in varpool_analyze_pending_decls. */
30be951a
JH
13277 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13278 ;
9acf766f
DB
13279 else
13280 gen_decl_die (decl, context_die);
13281 }
a3f97cbb
JW
13282 }
13283
8cadae7e
JM
13284 /* If we're at -g1, we're not interested in subblocks. */
13285 if (debug_info_level <= DINFO_LEVEL_TERSE)
13286 return;
13287
a3f97cbb
JW
13288 /* Output the DIEs to represent all sub-blocks (and the items declared
13289 therein) of this block. */
13290 for (subblocks = BLOCK_SUBBLOCKS (stmt);
13291 subblocks != NULL;
13292 subblocks = BLOCK_CHAIN (subblocks))
71dfc51f 13293 gen_block_die (subblocks, context_die, depth + 1);
a3f97cbb
JW
13294}
13295
a94dbf2c 13296/* Is this a typedef we can avoid emitting? */
71dfc51f
RK
13297
13298static inline int
7080f735 13299is_redundant_typedef (tree decl)
a94dbf2c
JM
13300{
13301 if (TYPE_DECL_IS_STUB (decl))
13302 return 1;
71dfc51f 13303
a94dbf2c
JM
13304 if (DECL_ARTIFICIAL (decl)
13305 && DECL_CONTEXT (decl)
13306 && is_tagged_type (DECL_CONTEXT (decl))
13307 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13308 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13309 /* Also ignore the artificial member typedef for the class name. */
13310 return 1;
71dfc51f 13311
a94dbf2c
JM
13312 return 0;
13313}
13314
535a42b1 13315/* Returns the DIE for decl. A DIE will always be returned. */
6097b0c3
DP
13316
13317static dw_die_ref
13318force_decl_die (tree decl)
13319{
13320 dw_die_ref decl_die;
13321 unsigned saved_external_flag;
13322 tree save_fn = NULL_TREE;
13323 decl_die = lookup_decl_die (decl);
13324 if (!decl_die)
13325 {
13326 dw_die_ref context_die;
13327 tree decl_context = DECL_CONTEXT (decl);
13328 if (decl_context)
13329 {
13330 /* Find die that represents this context. */
13331 if (TYPE_P (decl_context))
13332 context_die = force_type_die (decl_context);
13333 else
13334 context_die = force_decl_die (decl_context);
13335 }
13336 else
13337 context_die = comp_unit_die;
13338
d3e4cd01
JM
13339 decl_die = lookup_decl_die (decl);
13340 if (decl_die)
13341 return decl_die;
13342
6097b0c3
DP
13343 switch (TREE_CODE (decl))
13344 {
13345 case FUNCTION_DECL:
13346 /* Clear current_function_decl, so that gen_subprogram_die thinks
13347 that this is a declaration. At this point, we just want to force
13348 declaration die. */
13349 save_fn = current_function_decl;
13350 current_function_decl = NULL_TREE;
13351 gen_subprogram_die (decl, context_die);
a1c496cb 13352 current_function_decl = save_fn;
6097b0c3
DP
13353 break;
13354
13355 case VAR_DECL:
13356 /* Set external flag to force declaration die. Restore it after
13357 gen_decl_die() call. */
13358 saved_external_flag = DECL_EXTERNAL (decl);
13359 DECL_EXTERNAL (decl) = 1;
13360 gen_decl_die (decl, context_die);
13361 DECL_EXTERNAL (decl) = saved_external_flag;
13362 break;
13363
13364 case NAMESPACE_DECL:
13365 dwarf2out_decl (decl);
13366 break;
13367
13368 default:
ced3f397 13369 gcc_unreachable ();
6097b0c3 13370 }
a1c496cb 13371
535a42b1 13372 /* We should be able to find the DIE now. */
6097b0c3
DP
13373 if (!decl_die)
13374 decl_die = lookup_decl_die (decl);
ced3f397 13375 gcc_assert (decl_die);
6097b0c3 13376 }
a1c496cb 13377
6097b0c3
DP
13378 return decl_die;
13379}
66c78aa9 13380
8857ae1c
AO
13381/* Returns the DIE for TYPE, that must not be a base type. A DIE is
13382 always returned. */
66c78aa9
JM
13383
13384static dw_die_ref
6097b0c3 13385force_type_die (tree type)
66c78aa9 13386{
6097b0c3 13387 dw_die_ref type_die;
66c78aa9 13388
9733d507 13389 type_die = lookup_type_die (type);
6097b0c3
DP
13390 if (!type_die)
13391 {
13392 dw_die_ref context_die;
13393 if (TYPE_CONTEXT (type))
d3e4cd01
JM
13394 {
13395 if (TYPE_P (TYPE_CONTEXT (type)))
13396 context_die = force_type_die (TYPE_CONTEXT (type));
13397 else
13398 context_die = force_decl_die (TYPE_CONTEXT (type));
13399 }
6097b0c3
DP
13400 else
13401 context_die = comp_unit_die;
66c78aa9 13402
d3e4cd01
JM
13403 type_die = lookup_type_die (type);
13404 if (type_die)
13405 return type_die;
6097b0c3 13406 gen_type_die (type, context_die);
9733d507 13407 type_die = lookup_type_die (type);
ced3f397 13408 gcc_assert (type_die);
6097b0c3
DP
13409 }
13410 return type_die;
66c78aa9
JM
13411}
13412
13413/* Force out any required namespaces to be able to output DECL,
13414 and return the new context_die for it, if it's changed. */
13415
13416static dw_die_ref
13417setup_namespace_context (tree thing, dw_die_ref context_die)
13418{
6615c446
JO
13419 tree context = (DECL_P (thing)
13420 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
66c78aa9 13421 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6614fd40 13422 /* Force out the namespace. */
6097b0c3 13423 context_die = force_decl_die (context);
66c78aa9
JM
13424
13425 return context_die;
13426}
13427
13428/* Emit a declaration DIE for THING (which is either a DECL or a tagged
13429 type) within its namespace, if appropriate.
13430
13431 For compatibility with older debuggers, namespace DIEs only contain
13432 declarations; all definitions are emitted at CU scope. */
13433
13434static void
13435declare_in_namespace (tree thing, dw_die_ref context_die)
13436{
13437 dw_die_ref ns_context;
13438
13439 if (debug_info_level <= DINFO_LEVEL_TERSE)
13440 return;
13441
0eb971bd
JW
13442 /* If this decl is from an inlined function, then don't try to emit it in its
13443 namespace, as we will get confused. It would have already been emitted
13444 when the abstract instance of the inline function was emitted anyways. */
13445 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13446 return;
13447
66c78aa9
JM
13448 ns_context = setup_namespace_context (thing, context_die);
13449
13450 if (ns_context != context_die)
13451 {
13452 if (DECL_P (thing))
13453 gen_decl_die (thing, ns_context);
13454 else
13455 gen_type_die (thing, ns_context);
13456 }
13457}
13458
6614fd40 13459/* Generate a DIE for a namespace or namespace alias. */
66c78aa9
JM
13460
13461static void
13462gen_namespace_die (tree decl)
13463{
13464 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13465
13466 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
e0a21ab9 13467 they are an alias of. */
66c78aa9
JM
13468 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13469 {
6614fd40 13470 /* Output a real namespace. */
66c78aa9
JM
13471 dw_die_ref namespace_die
13472 = new_die (DW_TAG_namespace, context_die, decl);
13473 add_name_and_src_coords_attributes (namespace_die, decl);
13474 equate_decl_number_to_die (decl, namespace_die);
13475 }
13476 else
13477 {
6614fd40 13478 /* Output a namespace alias. */
66c78aa9 13479
6614fd40 13480 /* Force out the namespace we are an alias of, if necessary. */
66c78aa9 13481 dw_die_ref origin_die
6097b0c3 13482 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
66c78aa9 13483
6614fd40 13484 /* Now create the namespace alias DIE. */
66c78aa9
JM
13485 dw_die_ref namespace_die
13486 = new_die (DW_TAG_imported_declaration, context_die, decl);
13487 add_name_and_src_coords_attributes (namespace_die, decl);
13488 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13489 equate_decl_number_to_die (decl, namespace_die);
13490 }
13491}
13492
a3f97cbb 13493/* Generate Dwarf debug information for a decl described by DECL. */
71dfc51f 13494
a3f97cbb 13495static void
7080f735 13496gen_decl_die (tree decl, dw_die_ref context_die)
a3f97cbb 13497{
b3694847 13498 tree origin;
71dfc51f 13499
f11c3043 13500 if (DECL_P (decl) && DECL_IGNORED_P (decl))
71dfc51f 13501 return;
a3f97cbb 13502
a3f97cbb
JW
13503 switch (TREE_CODE (decl))
13504 {
2ad9852d
RK
13505 case ERROR_MARK:
13506 break;
13507
a3f97cbb 13508 case CONST_DECL:
556273e0 13509 /* The individual enumerators of an enum type get output when we output
73c68f61 13510 the Dwarf representation of the relevant enum type itself. */
a3f97cbb
JW
13511 break;
13512
13513 case FUNCTION_DECL:
4edb7b60
JM
13514 /* Don't output any DIEs to represent mere function declarations,
13515 unless they are class members or explicit block externs. */
13516 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
777ad4c2 13517 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
71dfc51f 13518 break;
bdb669cb 13519
6de9cd9a
DN
13520#if 0
13521 /* FIXME */
13522 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13523 on local redeclarations of global functions. That seems broken. */
13524 if (current_function_decl != decl)
13525 /* This is only a declaration. */;
13526#endif
13527
5daf7c0a
JM
13528 /* If we're emitting a clone, emit info for the abstract instance. */
13529 if (DECL_ORIGIN (decl) != decl)
13530 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
2ad9852d 13531
1cfdcc15
JM
13532 /* If we're emitting an out-of-line copy of an inline function,
13533 emit info for the abstract instance and set up to refer to it. */
1bb17c21
JH
13534 else if (cgraph_function_possibly_inlined_p (decl)
13535 && ! DECL_ABSTRACT (decl)
66c78aa9 13536 && ! class_or_namespace_scope_p (context_die)
5daf7c0a
JM
13537 /* dwarf2out_abstract_function won't emit a die if this is just
13538 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13539 that case, because that works only if we have a die. */
13540 && DECL_INITIAL (decl) != NULL_TREE)
1cfdcc15 13541 {
1edf43d6 13542 dwarf2out_abstract_function (decl);
1cfdcc15
JM
13543 set_decl_origin_self (decl);
13544 }
2ad9852d 13545
5daf7c0a
JM
13546 /* Otherwise we're emitting the primary DIE for this decl. */
13547 else if (debug_info_level > DINFO_LEVEL_TERSE)
a94dbf2c
JM
13548 {
13549 /* Before we describe the FUNCTION_DECL itself, make sure that we
13550 have described its return type. */
13551 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13552
2081603c
JM
13553 /* And its virtual context. */
13554 if (DECL_VINDEX (decl) != NULL_TREE)
13555 gen_type_die (DECL_CONTEXT (decl), context_die);
13556
a94dbf2c
JM
13557 /* And its containing type. */
13558 origin = decl_class_context (decl);
71dfc51f 13559 if (origin != NULL_TREE)
10a11b75 13560 gen_type_die_for_member (origin, decl, context_die);
66c78aa9
JM
13561
13562 /* And its containing namespace. */
13563 declare_in_namespace (decl, context_die);
a94dbf2c 13564 }
a3f97cbb
JW
13565
13566 /* Now output a DIE to represent the function itself. */
13567 gen_subprogram_die (decl, context_die);
13568 break;
13569
13570 case TYPE_DECL:
13571 /* If we are in terse mode, don't generate any DIEs to represent any
73c68f61 13572 actual typedefs. */
a3f97cbb 13573 if (debug_info_level <= DINFO_LEVEL_TERSE)
4927276d 13574 break;
a3f97cbb 13575
2ad9852d 13576 /* In the special case of a TYPE_DECL node representing the declaration
73c68f61
SS
13577 of some type tag, if the given TYPE_DECL is marked as having been
13578 instantiated from some other (original) TYPE_DECL node (e.g. one which
13579 was generated within the original definition of an inline function) we
13580 have to generate a special (abbreviated) DW_TAG_structure_type,
13581 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
2081603c 13582 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
a3f97cbb
JW
13583 {
13584 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13585 break;
13586 }
a3f97cbb 13587
a94dbf2c
JM
13588 if (is_redundant_typedef (decl))
13589 gen_type_die (TREE_TYPE (decl), context_die);
13590 else
71dfc51f
RK
13591 /* Output a DIE to represent the typedef itself. */
13592 gen_typedef_die (decl, context_die);
a3f97cbb
JW
13593 break;
13594
13595 case LABEL_DECL:
13596 if (debug_info_level >= DINFO_LEVEL_NORMAL)
71dfc51f 13597 gen_label_die (decl, context_die);
a3f97cbb
JW
13598 break;
13599
13600 case VAR_DECL:
6de9cd9a 13601 case RESULT_DECL:
a3f97cbb 13602 /* If we are in terse mode, don't generate any DIEs to represent any
73c68f61 13603 variable declarations or definitions. */
a3f97cbb 13604 if (debug_info_level <= DINFO_LEVEL_TERSE)
71dfc51f 13605 break;
a3f97cbb
JW
13606
13607 /* Output any DIEs that are needed to specify the type of this data
73c68f61 13608 object. */
a3f97cbb
JW
13609 gen_type_die (TREE_TYPE (decl), context_die);
13610
a94dbf2c
JM
13611 /* And its containing type. */
13612 origin = decl_class_context (decl);
71dfc51f 13613 if (origin != NULL_TREE)
10a11b75 13614 gen_type_die_for_member (origin, decl, context_die);
a94dbf2c 13615
66c78aa9
JM
13616 /* And its containing namespace. */
13617 declare_in_namespace (decl, context_die);
13618
a3f97cbb 13619 /* Now output the DIE to represent the data object itself. This gets
73c68f61
SS
13620 complicated because of the possibility that the VAR_DECL really
13621 represents an inlined instance of a formal parameter for an inline
13622 function. */
a3f97cbb 13623 origin = decl_ultimate_origin (decl);
71dfc51f
RK
13624 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13625 gen_formal_parameter_die (decl, context_die);
a3f97cbb 13626 else
71dfc51f 13627 gen_variable_die (decl, context_die);
a3f97cbb
JW
13628 break;
13629
13630 case FIELD_DECL:
2ad9852d 13631 /* Ignore the nameless fields that are used to skip bits but handle C++
3199cb41 13632 anonymous unions and structs. */
71dfc51f 13633 if (DECL_NAME (decl) != NULL_TREE
3199cb41
UW
13634 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13635 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
a3f97cbb
JW
13636 {
13637 gen_type_die (member_declared_type (decl), context_die);
13638 gen_field_die (decl, context_die);
13639 }
13640 break;
13641
13642 case PARM_DECL:
13643 gen_type_die (TREE_TYPE (decl), context_die);
13644 gen_formal_parameter_die (decl, context_die);
13645 break;
13646
348bb3c7 13647 case NAMESPACE_DECL:
66c78aa9 13648 gen_namespace_die (decl);
348bb3c7
JM
13649 break;
13650
a3f97cbb 13651 default:
ced3f397
NS
13652 /* Probably some frontend-internal decl. Assume we don't care. */
13653 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13654 break;
a3f97cbb 13655 }
a3f97cbb
JW
13656}
13657\f
2ad9852d 13658/* Output debug information for global decl DECL. Called from toplev.c after
2b85879e 13659 compilation proper has finished. */
2ad9852d 13660
2b85879e 13661static void
7080f735 13662dwarf2out_global_decl (tree decl)
2b85879e
NB
13663{
13664 /* Output DWARF2 information for file-scope tentative data object
2ad9852d
RK
13665 declarations, file-scope (extern) function declarations (which had no
13666 corresponding body) and file-scope tagged type declarations and
13667 definitions which have not yet been forced out. */
2b85879e
NB
13668 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13669 dwarf2out_decl (decl);
13670}
13671
21d13d83
ZW
13672/* Output debug information for type decl DECL. Called from toplev.c
13673 and from language front ends (to record built-in types). */
13674static void
13675dwarf2out_type_decl (tree decl, int local)
13676{
13677 if (!local)
13678 dwarf2out_decl (decl);
13679}
13680
a1c496cb
EC
13681/* Output debug information for imported module or decl. */
13682
6097b0c3
DP
13683static void
13684dwarf2out_imported_module_or_decl (tree decl, tree context)
13685{
13686 dw_die_ref imported_die, at_import_die;
13687 dw_die_ref scope_die;
6773e15f 13688 expanded_location xloc;
a1c496cb 13689
6097b0c3
DP
13690 if (debug_info_level <= DINFO_LEVEL_TERSE)
13691 return;
13692
ced3f397 13693 gcc_assert (decl);
6097b0c3
DP
13694
13695 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
a1c496cb 13696 We need decl DIE for reference and scope die. First, get DIE for the decl
6097b0c3
DP
13697 itself. */
13698
13699 /* Get the scope die for decl context. Use comp_unit_die for global module
13700 or decl. If die is not found for non globals, force new die. */
13701 if (!context)
13702 scope_die = comp_unit_die;
13703 else if (TYPE_P (context))
39ef6592
LC
13704 {
13705 if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13706 return;
6097b0c3 13707 scope_die = force_type_die (context);
39ef6592 13708 }
6097b0c3
DP
13709 else
13710 scope_die = force_decl_die (context);
13711
834eb1f0
GS
13712 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
13713 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
8857ae1c
AO
13714 {
13715 if (is_base_type (TREE_TYPE (decl)))
13716 at_import_die = base_type_die (TREE_TYPE (decl));
13717 else
13718 at_import_die = force_type_die (TREE_TYPE (decl));
13719 }
6097b0c3 13720 else
a288c5cd
JJ
13721 {
13722 at_import_die = lookup_decl_die (decl);
13723 if (!at_import_die)
13724 {
13725 /* If we're trying to avoid duplicate debug info, we may not have
13726 emitted the member decl for this field. Emit it now. */
13727 if (TREE_CODE (decl) == FIELD_DECL)
13728 {
13729 tree type = DECL_CONTEXT (decl);
13730 dw_die_ref type_context_die;
13731
13732 if (TYPE_CONTEXT (type))
13733 if (TYPE_P (TYPE_CONTEXT (type)))
39ef6592
LC
13734 {
13735 if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13736 DINFO_USAGE_DIR_USE))
13737 return;
a288c5cd 13738 type_context_die = force_type_die (TYPE_CONTEXT (type));
39ef6592 13739 }
a288c5cd
JJ
13740 else
13741 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13742 else
13743 type_context_die = comp_unit_die;
13744 gen_type_die_for_member (type, decl, type_context_die);
13745 }
13746 at_import_die = force_decl_die (decl);
13747 }
13748 }
a1c496cb
EC
13749
13750 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
6097b0c3
DP
13751 if (TREE_CODE (decl) == NAMESPACE_DECL)
13752 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13753 else
13754 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
6773e15f
PB
13755
13756 xloc = expand_location (input_location);
d5688810 13757 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
6773e15f 13758 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
6097b0c3
DP
13759 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13760}
13761
71dfc51f
RK
13762/* Write the debugging output for DECL. */
13763
a3f97cbb 13764void
7080f735 13765dwarf2out_decl (tree decl)
a3f97cbb 13766{
b3694847 13767 dw_die_ref context_die = comp_unit_die;
88dad228 13768
a3f97cbb
JW
13769 switch (TREE_CODE (decl))
13770 {
2ad9852d
RK
13771 case ERROR_MARK:
13772 return;
13773
a3f97cbb 13774 case FUNCTION_DECL:
a3f97cbb 13775 /* What we would really like to do here is to filter out all mere
73c68f61
SS
13776 file-scope declarations of file-scope functions which are never
13777 referenced later within this translation unit (and keep all of ones
13778 that *are* referenced later on) but we aren't clairvoyant, so we have
13779 no idea which functions will be referenced in the future (i.e. later
13780 on within the current translation unit). So here we just ignore all
13781 file-scope function declarations which are not also definitions. If
13782 and when the debugger needs to know something about these functions,
13783 it will have to hunt around and find the DWARF information associated
13784 with the definition of the function.
2ad9852d
RK
13785
13786 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
73c68f61
SS
13787 nodes represent definitions and which ones represent mere
13788 declarations. We have to check DECL_INITIAL instead. That's because
13789 the C front-end supports some weird semantics for "extern inline"
13790 function definitions. These can get inlined within the current
569b7f6a 13791 translation unit (and thus, we need to generate Dwarf info for their
73c68f61
SS
13792 abstract instances so that the Dwarf info for the concrete inlined
13793 instances can have something to refer to) but the compiler never
13794 generates any out-of-lines instances of such things (despite the fact
13795 that they *are* definitions).
2ad9852d
RK
13796
13797 The important point is that the C front-end marks these "extern
13798 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13799 them anyway. Note that the C++ front-end also plays some similar games
13800 for inline function definitions appearing within include files which
13801 also contain `#pragma interface' pragmas. */
a3f97cbb 13802 if (DECL_INITIAL (decl) == NULL_TREE)
b1ccbc24 13803 return;
88dad228 13804
9c6cd30e
JM
13805 /* If we're a nested function, initially use a parent of NULL; if we're
13806 a plain function, this will be fixed up in decls_for_scope. If
13807 we're a method, it will be ignored, since we already have a DIE. */
8cadae7e
JM
13808 if (decl_function_context (decl)
13809 /* But if we're in terse mode, we don't care about scope. */
13810 && debug_info_level > DINFO_LEVEL_TERSE)
9c6cd30e 13811 context_die = NULL;
a3f97cbb
JW
13812 break;
13813
13814 case VAR_DECL:
556273e0 13815 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
73c68f61
SS
13816 declaration and if the declaration was never even referenced from
13817 within this entire compilation unit. We suppress these DIEs in
13818 order to save space in the .debug section (by eliminating entries
13819 which are probably useless). Note that we must not suppress
13820 block-local extern declarations (whether used or not) because that
13821 would screw-up the debugger's name lookup mechanism and cause it to
13822 miss things which really ought to be in scope at a given point. */
a3f97cbb 13823 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
71dfc51f 13824 return;
a3f97cbb 13825
30be951a
JH
13826 /* For local statics lookup proper context die. */
13827 if (TREE_STATIC (decl) && decl_function_context (decl))
13828 context_die = lookup_decl_die (DECL_CONTEXT (decl));
13829
a3f97cbb 13830 /* If we are in terse mode, don't generate any DIEs to represent any
73c68f61 13831 variable declarations or definitions. */
a3f97cbb 13832 if (debug_info_level <= DINFO_LEVEL_TERSE)
71dfc51f 13833 return;
a3f97cbb
JW
13834 break;
13835
66c78aa9
JM
13836 case NAMESPACE_DECL:
13837 if (debug_info_level <= DINFO_LEVEL_TERSE)
13838 return;
13839 if (lookup_decl_die (decl) != NULL)
2878ea73 13840 return;
66c78aa9
JM
13841 break;
13842
a3f97cbb 13843 case TYPE_DECL:
57fb7689
JM
13844 /* Don't emit stubs for types unless they are needed by other DIEs. */
13845 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13846 return;
13847
a3f97cbb 13848 /* Don't bother trying to generate any DIEs to represent any of the
73c68f61 13849 normal built-in types for the language we are compiling. */
6773e15f 13850 if (DECL_IS_BUILTIN (decl))
a94dbf2c
JM
13851 {
13852 /* OK, we need to generate one for `bool' so GDB knows what type
73c68f61 13853 comparisons have. */
9e51ddaa 13854 if (is_cxx ()
f11c3043
RK
13855 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13856 && ! DECL_IGNORED_P (decl))
a94dbf2c 13857 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
71dfc51f 13858
a94dbf2c
JM
13859 return;
13860 }
a3f97cbb 13861
88dad228 13862 /* If we are in terse mode, don't generate any DIEs for types. */
a3f97cbb 13863 if (debug_info_level <= DINFO_LEVEL_TERSE)
4927276d 13864 return;
88dad228
JM
13865
13866 /* If we're a function-scope tag, initially use a parent of NULL;
13867 this will be fixed up in decls_for_scope. */
13868 if (decl_function_context (decl))
3f76745e 13869 context_die = NULL;
88dad228 13870
a3f97cbb
JW
13871 break;
13872
13873 default:
13874 return;
13875 }
13876
88dad228 13877 gen_decl_die (decl, context_die);
a3f97cbb
JW
13878}
13879
13880/* Output a marker (i.e. a label) for the beginning of the generated code for
13881 a lexical block. */
71dfc51f 13882
a5a42b92 13883static void
7080f735
AJ
13884dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13885 unsigned int blocknum)
a3f97cbb 13886{
d6b5193b 13887 switch_to_section (current_function_section ());
8215347e 13888 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
a3f97cbb
JW
13889}
13890
13891/* Output a marker (i.e. a label) for the end of the generated code for a
13892 lexical block. */
71dfc51f 13893
a5a42b92 13894static void
7080f735 13895dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
a3f97cbb 13896{
d6b5193b 13897 switch_to_section (current_function_section ());
8215347e 13898 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
a3f97cbb
JW
13899}
13900
64b59a80
JM
13901/* Returns nonzero if it is appropriate not to emit any debugging
13902 information for BLOCK, because it doesn't contain any instructions.
fcd7f76b 13903
64b59a80
JM
13904 Don't allow this for blocks with nested functions or local classes
13905 as we would end up with orphans, and in the presence of scheduling
13906 we may end up calling them anyway. */
13907
e1772ac0 13908static bool
7080f735 13909dwarf2out_ignore_block (tree block)
fcd7f76b
JM
13910{
13911 tree decl;
2ad9852d 13912
fcd7f76b 13913 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
64b59a80
JM
13914 if (TREE_CODE (decl) == FUNCTION_DECL
13915 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13916 return 0;
2ad9852d 13917
64b59a80 13918 return 1;
fcd7f76b
JM
13919}
13920
d5688810
GK
13921/* Hash table routines for file_hash. */
13922
13923static int
13924file_table_eq (const void *p1_p, const void *p2_p)
13925{
13926 const struct dwarf_file_data * p1 = p1_p;
13927 const char * p2 = p2_p;
13928 return strcmp (p1->filename, p2) == 0;
13929}
13930
13931static hashval_t
13932file_table_hash (const void *p_p)
13933{
13934 const struct dwarf_file_data * p = p_p;
13935 return htab_hash_string (p->filename);
13936}
13937
2ad9852d 13938/* Lookup FILE_NAME (in the list of filenames that we know about here in
9a666dda 13939 dwarf2out.c) and return its "index". The index of each (known) filename is
2ad9852d
RK
13940 just a unique number which is associated with only that one filename. We
13941 need such numbers for the sake of generating labels (in the .debug_sfnames
13942 section) and references to those files numbers (in the .debug_srcinfo
13943 and.debug_macinfo sections). If the filename given as an argument is not
13944 found in our current list, add it to the list and assign it the next
13945 available unique index number. In order to speed up searches, we remember
13946 the index of the filename was looked up last. This handles the majority of
13947 all searches. */
71dfc51f 13948
d5688810 13949static struct dwarf_file_data *
7080f735 13950lookup_filename (const char *file_name)
a3f97cbb 13951{
d5688810
GK
13952 void ** slot;
13953 struct dwarf_file_data * created;
a3f97cbb 13954
2e18bbae
RH
13955 /* Check to see if the file name that was searched on the previous
13956 call matches this file name. If so, return the index. */
d5688810
GK
13957 if (file_table_last_lookup
13958 && (file_name == file_table_last_lookup->filename
13959 || strcmp (file_table_last_lookup->filename, file_name) == 0))
13960 return file_table_last_lookup;
a3f97cbb 13961
ea4b7848 13962 /* Didn't match the previous lookup, search the table. */
d5688810
GK
13963 slot = htab_find_slot_with_hash (file_table, file_name,
13964 htab_hash_string (file_name), INSERT);
13965 if (*slot)
13966 return *slot;
a3f97cbb 13967
d5688810
GK
13968 created = ggc_alloc (sizeof (struct dwarf_file_data));
13969 created->filename = file_name;
13970 created->emitted_number = 0;
13971 *slot = created;
13972 return created;
73c68f61
SS
13973}
13974
3e2844cb
JW
13975/* If the assembler will construct the file table, then translate the compiler
13976 internal file table number into the assembler file table number, and emit
13977 a .file directive if we haven't already emitted one yet. The file table
13978 numbers are different because we prune debug info for unused variables and
13979 types, which may include filenames. */
13980
73c68f61 13981static int
d5688810 13982maybe_emit_file (struct dwarf_file_data * fd)
73c68f61 13983{
d5688810 13984 if (! fd->emitted_number)
211a0cbe 13985 {
d5688810
GK
13986 if (last_emitted_file)
13987 fd->emitted_number = last_emitted_file->emitted_number + 1;
13988 else
13989 fd->emitted_number = 1;
13990 last_emitted_file = fd;
2878ea73 13991
d5688810 13992 if (DWARF2_ASM_LINE_DEBUG_INFO)
73c68f61 13993 {
d5688810
GK
13994 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13995 output_quoted_string (asm_out_file, fd->filename);
73c68f61
SS
13996 fputc ('\n', asm_out_file);
13997 }
211a0cbe 13998 }
2878ea73 13999
d5688810 14000 return fd->emitted_number;
a3f97cbb
JW
14001}
14002
0a2d3d69
DB
14003/* Called by the final INSN scan whenever we see a var location. We
14004 use it to drop labels in the right places, and throw the location in
14005 our lookup table. */
14006
14007static void
14008dwarf2out_var_location (rtx loc_note)
14009{
14010 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14011 struct var_loc_node *newloc;
14012 rtx prev_insn;
14013 static rtx last_insn;
14014 static const char *last_label;
ac3bfd86 14015 tree decl;
0a2d3d69
DB
14016
14017 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14018 return;
14019 prev_insn = PREV_INSN (loc_note);
14020
14021 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14022 /* If the insn we processed last time is the previous insn
14023 and it is also a var location note, use the label we emitted
14024 last time. */
14025 if (last_insn != NULL_RTX
14026 && last_insn == prev_insn
4b4bf941 14027 && NOTE_P (prev_insn)
a38e7aa5 14028 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
0a2d3d69
DB
14029 {
14030 newloc->label = last_label;
14031 }
14032 else
14033 {
14034 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14035 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14036 loclabel_num++;
14037 newloc->label = ggc_strdup (loclabel);
14038 }
14039 newloc->var_loc_note = loc_note;
14040 newloc->next = NULL;
14041
c543ca49 14042 if (cfun && in_cold_section_p)
c7466dee 14043 newloc->section_label = cfun->cold_section_label;
87c8b4be
CT
14044 else
14045 newloc->section_label = text_section_label;
14046
0a2d3d69
DB
14047 last_insn = loc_note;
14048 last_label = newloc->label;
ac3bfd86 14049 decl = NOTE_VAR_LOCATION_DECL (loc_note);
ac3bfd86 14050 add_var_loc_to_decl (decl, newloc);
0a2d3d69
DB
14051}
14052
14053/* We need to reset the locations at the beginning of each
14054 function. We can't do this in the end_function hook, because the
1146e682
GK
14055 declarations that use the locations won't have been output when
14056 that hook is called. Also compute have_multiple_function_sections here. */
0a2d3d69
DB
14057
14058static void
1146e682 14059dwarf2out_begin_function (tree fun)
0a2d3d69
DB
14060{
14061 htab_empty (decl_loc_table);
2878ea73 14062
1146e682
GK
14063 if (function_section (fun) != text_section)
14064 have_multiple_function_sections = true;
0a2d3d69
DB
14065}
14066
a3f97cbb
JW
14067/* Output a label to mark the beginning of a source code line entry
14068 and record information relating to this source line, in
14069 'line_info_table' for later output of the .debug_line section. */
71dfc51f 14070
e2a12aca 14071static void
7080f735 14072dwarf2out_source_line (unsigned int line, const char *filename)
a3f97cbb 14073{
7bf6b23d
JM
14074 if (debug_info_level >= DINFO_LEVEL_NORMAL
14075 && line != 0)
a3f97cbb 14076 {
d5688810 14077 int file_num = maybe_emit_file (lookup_filename (filename));
2878ea73 14078
d6b5193b 14079 switch_to_section (current_function_section ());
a3f97cbb 14080
8aaf55ac
JM
14081 /* If requested, emit something human-readable. */
14082 if (flag_debug_asm)
14083 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14084 filename, line);
14085
b2244e22
JW
14086 if (DWARF2_ASM_LINE_DEBUG_INFO)
14087 {
981975b6 14088 /* Emit the .loc directive understood by GNU as. */
2e18bbae 14089 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
b2244e22
JW
14090
14091 /* Indicate that line number info exists. */
2ad9852d 14092 line_info_table_in_use++;
b2244e22 14093 }
1146e682 14094 else if (function_section (current_function_decl) != text_section)
a3f97cbb 14095 {
b3694847 14096 dw_separate_line_info_ref line_info;
2878ea73 14097 targetm.asm_out.internal_label (asm_out_file,
d5688810
GK
14098 SEPARATE_LINE_CODE_LABEL,
14099 separate_line_info_table_in_use);
e90b62db 14100
a1105617 14101 /* Expand the line info table if necessary. */
e90b62db
JM
14102 if (separate_line_info_table_in_use
14103 == separate_line_info_table_allocated)
14104 {
14105 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14106 separate_line_info_table
703ad42b 14107 = ggc_realloc (separate_line_info_table,
17211ab5
GK
14108 separate_line_info_table_allocated
14109 * sizeof (dw_separate_line_info_entry));
703ad42b
KG
14110 memset (separate_line_info_table
14111 + separate_line_info_table_in_use,
17211ab5 14112 0,
7080f735 14113 (LINE_INFO_TABLE_INCREMENT
17211ab5 14114 * sizeof (dw_separate_line_info_entry)));
e90b62db 14115 }
71dfc51f
RK
14116
14117 /* Add the new entry at the end of the line_info_table. */
e90b62db
JM
14118 line_info
14119 = &separate_line_info_table[separate_line_info_table_in_use++];
d5688810 14120 line_info->dw_file_num = file_num;
e90b62db 14121 line_info->dw_line_num = line;
df696a75 14122 line_info->function = current_function_funcdef_no;
e90b62db
JM
14123 }
14124 else
14125 {
b3694847 14126 dw_line_info_ref line_info;
71dfc51f 14127
5fd9b178 14128 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
5c90448c 14129 line_info_table_in_use);
e90b62db 14130
71dfc51f 14131 /* Expand the line info table if necessary. */
e90b62db
JM
14132 if (line_info_table_in_use == line_info_table_allocated)
14133 {
14134 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14135 line_info_table
17211ab5
GK
14136 = ggc_realloc (line_info_table,
14137 (line_info_table_allocated
14138 * sizeof (dw_line_info_entry)));
14139 memset (line_info_table + line_info_table_in_use, 0,
14140 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
e90b62db 14141 }
71dfc51f
RK
14142
14143 /* Add the new entry at the end of the line_info_table. */
e90b62db 14144 line_info = &line_info_table[line_info_table_in_use++];
d5688810 14145 line_info->dw_file_num = file_num;
e90b62db 14146 line_info->dw_line_num = line;
a3f97cbb 14147 }
a3f97cbb
JW
14148 }
14149}
14150
30f7a378 14151/* Record the beginning of a new source file. */
71dfc51f 14152
7f905405 14153static void
7080f735 14154dwarf2out_start_source_file (unsigned int lineno, const char *filename)
a3f97cbb 14155{
8a7a6f4d 14156 if (flag_eliminate_dwarf2_dups)
881c6935
JM
14157 {
14158 /* Record the beginning of the file for break_out_includes. */
cc0017a9
ZD
14159 dw_die_ref bincl_die;
14160
14161 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
881c6935
JM
14162 add_AT_string (bincl_die, DW_AT_name, filename);
14163 }
2ad9852d 14164
84a5b4f8
DB
14165 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14166 {
d5688810 14167 int file_num = maybe_emit_file (lookup_filename (filename));
5597a350 14168
d6b5193b 14169 switch_to_section (debug_macinfo_section);
84a5b4f8 14170 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
7c262518
RH
14171 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14172 lineno);
5597a350 14173
d5688810 14174 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
84a5b4f8 14175 }
a3f97cbb
JW
14176}
14177
cc260610 14178/* Record the end of a source file. */
71dfc51f 14179
7f905405 14180static void
7080f735 14181dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
a3f97cbb 14182{
881c6935 14183 if (flag_eliminate_dwarf2_dups)
2ad9852d 14184 /* Record the end of the file for break_out_includes. */
54ba1f0d 14185 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
2ad9852d 14186
84a5b4f8
DB
14187 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14188 {
d6b5193b 14189 switch_to_section (debug_macinfo_section);
84a5b4f8
DB
14190 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14191 }
a3f97cbb
JW
14192}
14193
cc260610 14194/* Called from debug_define in toplev.c. The `buffer' parameter contains
a3f97cbb
JW
14195 the tail part of the directive line, i.e. the part which is past the
14196 initial whitespace, #, whitespace, directive-name, whitespace part. */
71dfc51f 14197
7f905405 14198static void
7080f735
AJ
14199dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14200 const char *buffer ATTRIBUTE_UNUSED)
a3f97cbb 14201{
84a5b4f8
DB
14202 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14203 {
d6b5193b 14204 switch_to_section (debug_macinfo_section);
84a5b4f8
DB
14205 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14206 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14207 dw2_asm_output_nstring (buffer, -1, "The macro");
14208 }
a3f97cbb
JW
14209}
14210
cc260610 14211/* Called from debug_undef in toplev.c. The `buffer' parameter contains
a3f97cbb
JW
14212 the tail part of the directive line, i.e. the part which is past the
14213 initial whitespace, #, whitespace, directive-name, whitespace part. */
71dfc51f 14214
7f905405 14215static void
7080f735
AJ
14216dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14217 const char *buffer ATTRIBUTE_UNUSED)
a3f97cbb 14218{
84a5b4f8
DB
14219 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14220 {
d6b5193b 14221 switch_to_section (debug_macinfo_section);
84a5b4f8
DB
14222 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14223 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14224 dw2_asm_output_nstring (buffer, -1, "The macro");
14225 }
a3f97cbb
JW
14226}
14227
14228/* Set up for Dwarf output at the start of compilation. */
71dfc51f 14229
a51d908e 14230static void
7080f735 14231dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
a3f97cbb 14232{
d5688810
GK
14233 /* Allocate the file_table. */
14234 file_table = htab_create_ggc (50, file_table_hash,
14235 file_table_eq, NULL);
acc187f5 14236
0a2d3d69 14237 /* Allocate the decl_die_table. */
636c7bc4
JZ
14238 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14239 decl_die_table_eq, NULL);
0a2d3d69
DB
14240
14241 /* Allocate the decl_loc_table. */
14242 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14243 decl_loc_table_eq, NULL);
a3f97cbb
JW
14244
14245 /* Allocate the initial hunk of the decl_scope_table. */
0fdc587b 14246 decl_scope_table = VEC_alloc (tree, gc, 256);
a3f97cbb
JW
14247
14248 /* Allocate the initial hunk of the abbrev_die_table. */
17211ab5
GK
14249 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14250 * sizeof (dw_die_ref));
a3f97cbb 14251 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
ea4b7848 14252 /* Zero-th entry is allocated, but unused. */
a3f97cbb
JW
14253 abbrev_die_table_in_use = 1;
14254
14255 /* Allocate the initial hunk of the line_info_table. */
17211ab5
GK
14256 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14257 * sizeof (dw_line_info_entry));
a3f97cbb 14258 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
2ad9852d 14259
ea4b7848 14260 /* Zero-th entry is allocated, but unused. */
a3f97cbb
JW
14261 line_info_table_in_use = 1;
14262
89708594
CT
14263 /* Allocate the pubtypes and pubnames vectors. */
14264 pubname_table = VEC_alloc (pubname_entry, gc, 32);
14265 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14266
556273e0 14267 /* Generate the initial DIE for the .debug section. Note that the (string)
a3f97cbb 14268 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
556273e0 14269 will (typically) be a relative pathname and that this pathname should be
a3f97cbb 14270 taken as being relative to the directory from which the compiler was
c4274b22
RH
14271 invoked when the given (base) source file was compiled. We will fill
14272 in this value in dwarf2out_finish. */
14273 comp_unit_die = gen_compile_unit_die (NULL);
a3f97cbb 14274
887fb69b 14275 incomplete_types = VEC_alloc (tree, gc, 64);
244a4af0 14276
a1bbd445 14277 used_rtx_array = VEC_alloc (rtx, gc, 32);
1865dbb5 14278
d6b5193b
RS
14279 debug_info_section = get_section (DEBUG_INFO_SECTION,
14280 SECTION_DEBUG, NULL);
14281 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14282 SECTION_DEBUG, NULL);
14283 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14284 SECTION_DEBUG, NULL);
14285 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14286 SECTION_DEBUG, NULL);
14287 debug_line_section = get_section (DEBUG_LINE_SECTION,
14288 SECTION_DEBUG, NULL);
14289 debug_loc_section = get_section (DEBUG_LOC_SECTION,
14290 SECTION_DEBUG, NULL);
14291 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14292 SECTION_DEBUG, NULL);
89708594
CT
14293#ifdef DEBUG_PUBTYPES_SECTION
14294 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14295 SECTION_DEBUG, NULL);
14296#endif
d6b5193b
RS
14297 debug_str_section = get_section (DEBUG_STR_SECTION,
14298 DEBUG_STR_SECTION_FLAGS, NULL);
14299 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14300 SECTION_DEBUG, NULL);
192d0f89
GK
14301 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14302 SECTION_DEBUG, NULL);
d6b5193b 14303
5c90448c 14304 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
9d2f2c45
RH
14305 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14306 DEBUG_ABBREV_SECTION_LABEL, 0);
9e749a8b 14307 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
2878ea73 14308 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
c7466dee
CT
14309 COLD_TEXT_SECTION_LABEL, 0);
14310 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
2ad9852d 14311
556273e0 14312 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
8b790721 14313 DEBUG_INFO_SECTION_LABEL, 0);
556273e0 14314 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
8b790721 14315 DEBUG_LINE_SECTION_LABEL, 0);
2bee6045
JJ
14316 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14317 DEBUG_RANGES_SECTION_LABEL, 0);
d6b5193b 14318 switch_to_section (debug_abbrev_section);
8b790721 14319 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
d6b5193b 14320 switch_to_section (debug_info_section);
8b790721 14321 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
d6b5193b 14322 switch_to_section (debug_line_section);
8b790721 14323 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
2ad9852d 14324
84a5b4f8
DB
14325 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14326 {
d6b5193b 14327 switch_to_section (debug_macinfo_section);
84a5b4f8
DB
14328 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14329 DEBUG_MACINFO_SECTION_LABEL, 0);
14330 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14331 }
7c262518 14332
d6b5193b 14333 switch_to_section (text_section);
9e749a8b 14334 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
c7466dee
CT
14335 if (flag_reorder_blocks_and_partition)
14336 {
c543ca49 14337 switch_to_section (unlikely_text_section ());
c7466dee
CT
14338 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14339 }
a3f97cbb
JW
14340}
14341
9eb4015a
JJ
14342/* A helper function for dwarf2out_finish called through
14343 ht_forall. Emit one queued .debug_str string. */
14344
14345static int
7080f735 14346output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
9eb4015a 14347{
17211ab5 14348 struct indirect_string_node *node = (struct indirect_string_node *) *h;
9eb4015a 14349
9eb4015a
JJ
14350 if (node->form == DW_FORM_strp)
14351 {
d6b5193b 14352 switch_to_section (debug_str_section);
9eb4015a 14353 ASM_OUTPUT_LABEL (asm_out_file, node->label);
17211ab5 14354 assemble_string (node->str, strlen (node->str) + 1);
9eb4015a 14355 }
2ad9852d 14356
9eb4015a
JJ
14357 return 1;
14358}
14359
6f8d8ef0
GK
14360#if ENABLE_ASSERT_CHECKING
14361/* Verify that all marks are clear. */
73c68f61 14362
6f8d8ef0
GK
14363static void
14364verify_marks_clear (dw_die_ref die)
14365{
14366 dw_die_ref c;
2878ea73 14367
6f8d8ef0
GK
14368 gcc_assert (! die->die_mark);
14369 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14370}
14371#endif /* ENABLE_ASSERT_CHECKING */
73c68f61
SS
14372
14373/* Clear the marks for a die and its children.
3dc575ff 14374 Be cool if the mark isn't set. */
73c68f61
SS
14375
14376static void
7080f735 14377prune_unmark_dies (dw_die_ref die)
73c68f61
SS
14378{
14379 dw_die_ref c;
2878ea73 14380
d6eeb3ba
GK
14381 if (die->die_mark)
14382 die->die_mark = 0;
14383 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
73c68f61
SS
14384}
14385
73c68f61
SS
14386/* Given DIE that we're marking as used, find any other dies
14387 it references as attributes and mark them as used. */
14388
14389static void
7080f735 14390prune_unused_types_walk_attribs (dw_die_ref die)
73c68f61
SS
14391{
14392 dw_attr_ref a;
1a27722f 14393 unsigned ix;
73c68f61 14394
1a27722f 14395 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
73c68f61
SS
14396 {
14397 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14398 {
14399 /* A reference to another DIE.
14400 Make sure that it will get emitted. */
14401 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14402 }
e26303c2
GK
14403 /* Set the string's refcount to 0 so that prune_unused_types_mark
14404 accounts properly for it. */
14405 if (AT_class (a) == dw_val_class_str)
14406 a->dw_attr_val.v.val_str->refcount = 0;
73c68f61
SS
14407 }
14408}
14409
14410
14411/* Mark DIE as being used. If DOKIDS is true, then walk down
14412 to DIE's children. */
14413
14414static void
7080f735 14415prune_unused_types_mark (dw_die_ref die, int dokids)
73c68f61
SS
14416{
14417 dw_die_ref c;
14418
14419 if (die->die_mark == 0)
14420 {
14421 /* We haven't done this node yet. Mark it as used. */
14422 die->die_mark = 1;
14423
14424 /* We also have to mark its parents as used.
14425 (But we don't want to mark our parents' kids due to this.) */
14426 if (die->die_parent)
14427 prune_unused_types_mark (die->die_parent, 0);
14428
14429 /* Mark any referenced nodes. */
14430 prune_unused_types_walk_attribs (die);
47fcfa7b
SS
14431
14432 /* If this node is a specification,
2878ea73 14433 also mark the definition, if it exists. */
47fcfa7b 14434 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
2878ea73 14435 prune_unused_types_mark (die->die_definition, 1);
73c68f61
SS
14436 }
14437
14438 if (dokids && die->die_mark != 2)
14439 {
14440 /* We need to walk the children, but haven't done so yet.
14441 Remember that we've walked the kids. */
14442 die->die_mark = 2;
14443
d6eeb3ba
GK
14444 /* If this is an array type, we need to make sure our
14445 kids get marked, even if they're types. */
14446 if (die->die_tag == DW_TAG_array_type)
14447 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14448 else
14449 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
73c68f61
SS
14450 }
14451}
14452
14453
14454/* Walk the tree DIE and mark types that we actually use. */
14455
14456static void
7080f735 14457prune_unused_types_walk (dw_die_ref die)
73c68f61
SS
14458{
14459 dw_die_ref c;
14460
14461 /* Don't do anything if this node is already marked. */
14462 if (die->die_mark)
14463 return;
14464
428aba16
RS
14465 switch (die->die_tag)
14466 {
14467 case DW_TAG_const_type:
14468 case DW_TAG_packed_type:
14469 case DW_TAG_pointer_type:
14470 case DW_TAG_reference_type:
14471 case DW_TAG_volatile_type:
14472 case DW_TAG_typedef:
14473 case DW_TAG_array_type:
14474 case DW_TAG_structure_type:
14475 case DW_TAG_union_type:
14476 case DW_TAG_class_type:
14477 case DW_TAG_friend:
14478 case DW_TAG_variant_part:
14479 case DW_TAG_enumeration_type:
14480 case DW_TAG_subroutine_type:
14481 case DW_TAG_string_type:
14482 case DW_TAG_set_type:
14483 case DW_TAG_subrange_type:
14484 case DW_TAG_ptr_to_member_type:
14485 case DW_TAG_file_type:
14486 if (die->die_perennial_p)
14487 break;
33c9159e 14488
428aba16
RS
14489 /* It's a type node --- don't mark it. */
14490 return;
73c68f61 14491
428aba16
RS
14492 default:
14493 /* Mark everything else. */
14494 break;
73c68f61
SS
14495 }
14496
14497 die->die_mark = 1;
14498
14499 /* Now, mark any dies referenced from here. */
14500 prune_unused_types_walk_attribs (die);
14501
14502 /* Mark children. */
d6eeb3ba 14503 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
73c68f61
SS
14504}
14505
e26303c2
GK
14506/* Increment the string counts on strings referred to from DIE's
14507 attributes. */
14508
14509static void
14510prune_unused_types_update_strings (dw_die_ref die)
14511{
14512 dw_attr_ref a;
14513 unsigned ix;
14514
14515 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14516 if (AT_class (a) == dw_val_class_str)
14517 {
14518 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14519 s->refcount++;
14520 /* Avoid unnecessarily putting strings that are used less than
14521 twice in the hash table. */
76f7a74f
GK
14522 if (s->refcount
14523 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
e26303c2
GK
14524 {
14525 void ** slot;
14526 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14527 htab_hash_string (s->str),
14528 INSERT);
14529 gcc_assert (*slot == NULL);
14530 *slot = s;
14531 }
14532 }
14533}
73c68f61
SS
14534
14535/* Remove from the tree DIE any dies that aren't marked. */
14536
14537static void
7080f735 14538prune_unused_types_prune (dw_die_ref die)
73c68f61 14539{
d6eeb3ba 14540 dw_die_ref c;
a1c496cb 14541
ced3f397 14542 gcc_assert (die->die_mark);
19450f2b 14543 prune_unused_types_update_strings (die);
73c68f61 14544
d6eeb3ba
GK
14545 if (! die->die_child)
14546 return;
2878ea73 14547
d6eeb3ba
GK
14548 c = die->die_child;
14549 do {
14550 dw_die_ref prev = c;
14551 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14552 if (c == die->die_child)
73c68f61 14553 {
d6eeb3ba
GK
14554 /* No marked children between 'prev' and the end of the list. */
14555 if (prev == c)
14556 /* No marked children at all. */
14557 die->die_child = NULL;
14558 else
14559 {
14560 prev->die_sib = c->die_sib;
14561 die->die_child = prev;
14562 }
14563 return;
73c68f61 14564 }
d6eeb3ba
GK
14565
14566 if (c != prev->die_sib)
14567 prev->die_sib = c;
d6eeb3ba
GK
14568 prune_unused_types_prune (c);
14569 } while (c != die->die_child);
73c68f61
SS
14570}
14571
14572
14573/* Remove dies representing declarations that we never use. */
14574
14575static void
7080f735 14576prune_unused_types (void)
73c68f61
SS
14577{
14578 unsigned int i;
14579 limbo_die_node *node;
89708594 14580 pubname_ref pub;
73c68f61 14581
6f8d8ef0
GK
14582#if ENABLE_ASSERT_CHECKING
14583 /* All the marks should already be clear. */
14584 verify_marks_clear (comp_unit_die);
73c68f61 14585 for (node = limbo_die_list; node; node = node->next)
6f8d8ef0
GK
14586 verify_marks_clear (node->die);
14587#endif /* ENABLE_ASSERT_CHECKING */
73c68f61
SS
14588
14589 /* Set the mark on nodes that are actually used. */
14590 prune_unused_types_walk (comp_unit_die);
14591 for (node = limbo_die_list; node; node = node->next)
14592 prune_unused_types_walk (node->die);
14593
14594 /* Also set the mark on nodes referenced from the
14595 pubname_table or arange_table. */
89708594
CT
14596 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14597 prune_unused_types_mark (pub->die, 1);
6a87d634
RS
14598 for (i = 0; i < arange_table_in_use; i++)
14599 prune_unused_types_mark (arange_table[i], 1);
73c68f61 14600
e26303c2
GK
14601 /* Get rid of nodes that aren't marked; and update the string counts. */
14602 if (debug_str_hash)
14603 htab_empty (debug_str_hash);
73c68f61
SS
14604 prune_unused_types_prune (comp_unit_die);
14605 for (node = limbo_die_list; node; node = node->next)
14606 prune_unused_types_prune (node->die);
14607
14608 /* Leave the marks clear. */
14609 prune_unmark_dies (comp_unit_die);
14610 for (node = limbo_die_list; node; node = node->next)
14611 prune_unmark_dies (node->die);
14612}
14613
d5688810
GK
14614/* Set the parameter to true if there are any relative pathnames in
14615 the file table. */
14616static int
14617file_table_relative_p (void ** slot, void *param)
14618{
14619 bool *p = param;
14620 struct dwarf_file_data *d = *slot;
1d2c2b96 14621 if (d->emitted_number && !IS_ABSOLUTE_PATH (d->filename))
d5688810
GK
14622 {
14623 *p = true;
14624 return 0;
14625 }
14626 return 1;
14627}
14628
a3f97cbb
JW
14629/* Output stuff that dwarf requires at the end of every file,
14630 and generate the DWARF-2 debugging info. */
71dfc51f 14631
a51d908e 14632static void
7080f735 14633dwarf2out_finish (const char *filename)
a3f97cbb 14634{
ef76d03b 14635 limbo_die_node *node, *next_node;
ae0ed63a 14636 dw_die_ref die = 0;
ef76d03b 14637
c4274b22
RH
14638 /* Add the name for the main input file now. We delayed this from
14639 dwarf2out_init to avoid complications with PCH. */
3b895f8e 14640 add_name_attribute (comp_unit_die, filename);
1d2c2b96 14641 if (!IS_ABSOLUTE_PATH (filename))
c4274b22 14642 add_comp_dir_attribute (comp_unit_die);
79c758fb
JJ
14643 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14644 {
d5688810
GK
14645 bool p = false;
14646 htab_traverse (file_table, file_table_relative_p, &p);
14647 if (p)
14648 add_comp_dir_attribute (comp_unit_die);
79c758fb 14649 }
c4274b22 14650
ef76d03b
JW
14651 /* Traverse the limbo die list, and add parent/child links. The only
14652 dies without parents that should be here are concrete instances of
14653 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14654 For concrete instances, we can get the parent die from the abstract
14655 instance. */
14656 for (node = limbo_die_list; node; node = next_node)
14657 {
14658 next_node = node->next;
14659 die = node->die;
14660
14661 if (die->die_parent == NULL)
14662 {
a96c67ec 14663 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
2ad9852d 14664
a96c67ec
JM
14665 if (origin)
14666 add_child_die (origin->die_parent, die);
ef76d03b 14667 else if (die == comp_unit_die)
a96c67ec 14668 ;
6bb28965
JM
14669 else if (errorcount > 0 || sorrycount > 0)
14670 /* It's OK to be confused by errors in the input. */
14671 add_child_die (comp_unit_die, die);
ced3f397 14672 else
54ba1f0d
RH
14673 {
14674 /* In certain situations, the lexical block containing a
14675 nested function can be optimized away, which results
14676 in the nested function die being orphaned. Likewise
14677 with the return type of that nested function. Force
60108d34
JH
14678 this to be a child of the containing function.
14679
14680 It may happen that even the containing function got fully
14681 inlined and optimized out. In that case we are lost and
14682 assign the empty child. This should not be big issue as
14683 the function is likely unreachable too. */
ced3f397
NS
14684 tree context = NULL_TREE;
14685
14686 gcc_assert (node->created_for);
14687
14688 if (DECL_P (node->created_for))
14689 context = DECL_CONTEXT (node->created_for);
14690 else if (TYPE_P (node->created_for))
14691 context = TYPE_CONTEXT (node->created_for);
a1c496cb 14692
db9e0d2a
AO
14693 gcc_assert (context
14694 && (TREE_CODE (context) == FUNCTION_DECL
14695 || TREE_CODE (context) == NAMESPACE_DECL));
a1c496cb 14696
54ba1f0d 14697 origin = lookup_decl_die (context);
60108d34
JH
14698 if (origin)
14699 add_child_die (origin, die);
1168d30f
JH
14700 else
14701 add_child_die (comp_unit_die, die);
54ba1f0d 14702 }
ef76d03b 14703 }
ef76d03b 14704 }
2ad9852d 14705
a96c67ec 14706 limbo_die_list = NULL;
ef76d03b 14707
8a8c3656
JM
14708 /* Walk through the list of incomplete types again, trying once more to
14709 emit full debugging info for them. */
14710 retry_incomplete_types ();
14711
03275f81
ZD
14712 if (flag_eliminate_unused_debug_types)
14713 prune_unused_types ();
14714
881c6935
JM
14715 /* Generate separate CUs for each of the include files we've seen.
14716 They will go into limbo_die_list. */
5f632b5e
JM
14717 if (flag_eliminate_dwarf2_dups)
14718 break_out_includes (comp_unit_die);
881c6935
JM
14719
14720 /* Traverse the DIE's and add add sibling attributes to those DIE's
14721 that have children. */
a3f97cbb 14722 add_sibling_attributes (comp_unit_die);
881c6935
JM
14723 for (node = limbo_die_list; node; node = node->next)
14724 add_sibling_attributes (node->die);
a3f97cbb
JW
14725
14726 /* Output a terminator label for the .text section. */
d6b5193b 14727 switch_to_section (text_section);
5fd9b178 14728 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
c7466dee
CT
14729 if (flag_reorder_blocks_and_partition)
14730 {
c543ca49 14731 switch_to_section (unlikely_text_section ());
c7466dee
CT
14732 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14733 }
a3f97cbb 14734
db3c0315
MM
14735 /* We can only use the low/high_pc attributes if all of the code was
14736 in .text. */
1146e682 14737 if (!have_multiple_function_sections)
db3c0315
MM
14738 {
14739 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14740 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
e90b62db 14741 }
2ad9852d 14742
0435c1d5
AO
14743 else
14744 {
14745 unsigned fde_idx = 0;
14746
14747 /* We need to give .debug_loc and .debug_ranges an appropriate
14748 "base address". Use zero so that these addresses become
14749 absolute. Historically, we've emitted the unexpected
14750 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
14751 Emit both to give time for other tools to adapt. */
14752 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
14753 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14754
14755 add_AT_range_list (comp_unit_die, DW_AT_ranges,
14756 add_ranges_by_labels (text_section_label,
14757 text_end_label));
14758 if (flag_reorder_blocks_and_partition)
14759 add_ranges_by_labels (cold_text_section_label,
14760 cold_end_label);
14761
14762 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
14763 {
14764 dw_fde_ref fde = &fde_table[fde_idx];
14765
14766 if (fde->dw_fde_switched_sections)
14767 {
14768 add_ranges_by_labels (fde->dw_fde_hot_section_label,
14769 fde->dw_fde_hot_section_end_label);
14770 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
14771 fde->dw_fde_unlikely_section_end_label);
14772 }
14773 else
14774 add_ranges_by_labels (fde->dw_fde_begin,
14775 fde->dw_fde_end);
14776 }
14777
14778 add_ranges (NULL);
14779 }
e90b62db 14780
1146e682
GK
14781 /* Output location list section if necessary. */
14782 if (have_location_lists)
14783 {
14784 /* Output the location lists info. */
14785 switch_to_section (debug_loc_section);
14786 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14787 DEBUG_LOC_SECTION_LABEL, 0);
14788 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14789 output_location_lists (die);
14790 }
14791
fe7cd37f 14792 if (debug_info_level >= DINFO_LEVEL_NORMAL)
192d0f89
GK
14793 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14794 debug_line_section_label);
db3c0315 14795
84a5b4f8 14796 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
192d0f89 14797 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
a96c67ec 14798
881c6935
JM
14799 /* Output all of the compilation units. We put the main one last so that
14800 the offsets are available to output_pubnames. */
14801 for (node = limbo_die_list; node; node = node->next)
cc0017a9 14802 output_comp_unit (node->die, 0);
2ad9852d 14803
cc0017a9 14804 output_comp_unit (comp_unit_die, 0);
881c6935 14805
a3f97cbb 14806 /* Output the abbreviation table. */
d6b5193b 14807 switch_to_section (debug_abbrev_section);
a3f97cbb
JW
14808 output_abbrev_section ();
14809
2ad9852d 14810 /* Output public names table if necessary. */
89708594 14811 if (!VEC_empty (pubname_entry, pubname_table))
d291dd49 14812 {
d6b5193b 14813 switch_to_section (debug_pubnames_section);
89708594 14814 output_pubnames (pubname_table);
d291dd49
JM
14815 }
14816
89708594
CT
14817#ifdef DEBUG_PUBTYPES_SECTION
14818 /* Output public types table if necessary. */
14819 if (!VEC_empty (pubname_entry, pubtype_table))
14820 {
14821 switch_to_section (debug_pubtypes_section);
14822 output_pubnames (pubtype_table);
14823 }
14824#endif
2878ea73 14825
2ad9852d
RK
14826 /* Output the address range information. We only put functions in the arange
14827 table, so don't write it out if we don't have any. */
a3f97cbb
JW
14828 if (fde_table_in_use)
14829 {
d6b5193b 14830 switch_to_section (debug_aranges_section);
a3f97cbb
JW
14831 output_aranges ();
14832 }
a20612aa 14833
a20612aa
RH
14834 /* Output ranges section if necessary. */
14835 if (ranges_table_in_use)
14836 {
d6b5193b 14837 switch_to_section (debug_ranges_section);
2bee6045 14838 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
a20612aa
RH
14839 output_ranges ();
14840 }
14841
d5688810
GK
14842 /* Output the source line correspondence table. We must do this
14843 even if there is no line information. Otherwise, on an empty
14844 translation unit, we will generate a present, but empty,
14845 .debug_info section. IRIX 6.5 `nm' will then complain when
14846 examining the file. This is done late so that any filenames
14847 used by the debug_info section are marked as 'used'. */
14848 if (! DWARF2_ASM_LINE_DEBUG_INFO)
14849 {
14850 switch_to_section (debug_line_section);
14851 output_line_info ();
14852 }
14853
3c0d1020 14854 /* Have to end the macro section. */
cc260610 14855 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
c26fbbca 14856 {
d6b5193b 14857 switch_to_section (debug_macinfo_section);
2f8d482e 14858 dw2_asm_output_data (1, 0, "End compilation unit");
cc260610 14859 }
9eb4015a 14860
2ad9852d 14861 /* If we emitted any DW_FORM_strp form attribute, output the string
9eb4015a
JJ
14862 table too. */
14863 if (debug_str_hash)
17211ab5 14864 htab_traverse (debug_str_hash, output_indirect_string, NULL);
a3f97cbb 14865}
e2500fed
GK
14866#else
14867
14868/* This should never be used, but its address is needed for comparisons. */
14869const struct gcc_debug_hooks dwarf2_debug_hooks;
14870
14871#endif /* DWARF2_DEBUGGING_INFO */
14872
14873#include "gt-dwarf2out.h"
This page took 5.978202 seconds and 5 git commands to generate.