]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dwarf2out.c
Merge tree-ssa-20020619-branch into mainline.
[thirdparty/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,
affad9a4 3 2003, 2004 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
LB
21along with GCC; see the file COPYING. If not, write to the Free
22Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2302111-1307, 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
0021b564 37#include "config.h"
670ee920 38#include "system.h"
4977bab6
ZW
39#include "coretypes.h"
40#include "tm.h"
a3f97cbb
JW
41#include "tree.h"
42#include "flags.h"
11ad4784 43#include "real.h"
a3f97cbb
JW
44#include "rtl.h"
45#include "hard-reg-set.h"
46#include "regs.h"
47#include "insn-config.h"
48#include "reload.h"
52a11cbf 49#include "function.h"
a3f97cbb 50#include "output.h"
71dfc51f 51#include "expr.h"
e78d8e51 52#include "libfuncs.h"
3f76745e 53#include "except.h"
a7cc7f29 54#include "dwarf2.h"
76ead72b 55#include "dwarf2out.h"
2e4b9b8c 56#include "dwarf2asm.h"
10f0ad3d 57#include "toplev.h"
1865dbb5 58#include "varray.h"
951a525f 59#include "ggc.h"
881c6935 60#include "md5.h"
57bed152 61#include "tm_p.h"
2a2b2d43 62#include "diagnostic.h"
a51d908e 63#include "debug.h"
07c9d2eb 64#include "target.h"
3ac88239 65#include "langhooks.h"
cc0017a9 66#include "hashtab.h"
1bb17c21 67#include "cgraph.h"
6097b0c3 68#include "input.h"
a3f97cbb 69
653e276c 70#ifdef DWARF2_DEBUGGING_INFO
7080f735 71static void dwarf2out_source_line (unsigned int, const char *);
653e276c
NB
72#endif
73
770ca8c6
JO
74/* DWARF2 Abbreviation Glossary:
75 CFA = Canonical Frame Address
00a42e21
JM
76 a fixed address on the stack which identifies a call frame.
77 We define it to be the value of SP just before the call insn.
78 The CFA register and offset, which may change during the course
79 of the function, are used to calculate its value at runtime.
a401107d
JO
80 CFI = Call Frame Instruction
81 an instruction for the DWARF2 abstract machine
770ca8c6
JO
82 CIE = Common Information Entry
83 information describing information common to one or more FDEs
84 DIE = Debugging Information Entry
85 FDE = Frame Description Entry
86 information describing the stack call frame, in particular,
87 how to restore registers
88
89 DW_CFA_... = DWARF2 CFA call frame instruction
90 DW_TAG_... = DWARF2 DIE tag */
91
0021b564
JM
92/* Decide whether we want to emit frame unwind information for the current
93 translation unit. */
94
95int
7080f735 96dwarf2out_do_frame (void)
0021b564
JM
97{
98 return (write_symbols == DWARF2_DEBUG
7a0c8d71 99 || write_symbols == VMS_AND_DWARF2_DEBUG
9ec36da5 100#ifdef DWARF2_FRAME_INFO
556273e0 101 || DWARF2_FRAME_INFO
9ec36da5 102#endif
0021b564 103#ifdef DWARF2_UNWIND_INFO
14a774a9 104 || flag_unwind_tables
531073e7 105 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)
0021b564
JM
106#endif
107 );
108}
109
f3a8e4f5
KG
110/* The size of the target's pointer type. */
111#ifndef PTR_SIZE
112#define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
113#endif
114
9d340419
RO
115/* Various versions of targetm.eh_frame_section. Note these must appear
116 outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro guards. */
f3a8e4f5 117
9d340419 118/* Version of targetm.eh_frame_section for systems with named sections. */
f3a8e4f5 119void
9d340419 120named_section_eh_frame_section (void)
f3a8e4f5
KG
121{
122#ifdef EH_FRAME_SECTION_NAME
96d0f4dc
JJ
123#ifdef HAVE_LD_RO_RW_SECTION_MIXING
124 int fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
125 int per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
126 int lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
127 int flags;
128
129 flags = (! flag_pic
130 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
131 && (fde_encoding & 0x70) != DW_EH_PE_aligned
132 && (per_encoding & 0x70) != DW_EH_PE_absptr
133 && (per_encoding & 0x70) != DW_EH_PE_aligned
134 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
135 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
136 ? 0 : SECTION_WRITE;
137 named_section_flags (EH_FRAME_SECTION_NAME, flags);
138#else
f3a8e4f5 139 named_section_flags (EH_FRAME_SECTION_NAME, SECTION_WRITE);
96d0f4dc 140#endif
9d340419
RO
141#endif
142}
143
144/* Version of targetm.eh_frame_section for systems using collect2. */
145void
146collect2_eh_frame_section (void)
147{
f3a8e4f5
KG
148 tree label = get_file_function_name ('F');
149
150 data_section ();
151 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
5fd9b178 152 targetm.asm_out.globalize_label (asm_out_file, IDENTIFIER_POINTER (label));
f3a8e4f5 153 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
9d340419
RO
154}
155
156/* Default version of targetm.eh_frame_section. */
157void
158default_eh_frame_section (void)
159{
160#ifdef EH_FRAME_SECTION_NAME
161 named_section_eh_frame_section ();
162#else
163 collect2_eh_frame_section ();
f3a8e4f5
KG
164#endif
165}
166
e2500fed
GK
167/* Array of RTXes referenced by the debugging information, which therefore
168 must be kept around forever. */
169static GTY(()) varray_type used_rtx_varray;
170
171/* A pointer to the base of a list of incomplete types which might be
172 completed at some later time. incomplete_types_list needs to be a VARRAY
173 because we want to tell the garbage collector about it. */
174static GTY(()) varray_type incomplete_types;
175
176/* A pointer to the base of a table of references to declaration
177 scopes. This table is a display which tracks the nesting
178 of declaration scopes at the current scope and containing
179 scopes. This table is used to find the proper place to
180 define type declaration DIE's. */
181static GTY(()) varray_type decl_scope_table;
182
eaf95893
RK
183/* How to start an assembler comment. */
184#ifndef ASM_COMMENT_START
185#define ASM_COMMENT_START ";#"
186#endif
187
a3f97cbb
JW
188typedef struct dw_cfi_struct *dw_cfi_ref;
189typedef struct dw_fde_struct *dw_fde_ref;
190typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
a3f97cbb
JW
191
192/* Call frames are described using a sequence of Call Frame
193 Information instructions. The register number, offset
194 and address fields are provided as possible operands;
195 their use is selected by the opcode field. */
71dfc51f 196
17211ab5
GK
197enum dw_cfi_oprnd_type {
198 dw_cfi_oprnd_unused,
199 dw_cfi_oprnd_reg_num,
200 dw_cfi_oprnd_offset,
201 dw_cfi_oprnd_addr,
202 dw_cfi_oprnd_loc
203};
204
205typedef union dw_cfi_oprnd_struct GTY(())
71dfc51f 206{
17211ab5 207 unsigned long GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
799f628a 208 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
17211ab5
GK
209 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
210 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
71dfc51f 211}
a3f97cbb
JW
212dw_cfi_oprnd;
213
17211ab5 214typedef struct dw_cfi_struct GTY(())
71dfc51f
RK
215{
216 dw_cfi_ref dw_cfi_next;
217 enum dwarf_call_frame_info dw_cfi_opc;
7080f735 218 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
17211ab5 219 dw_cfi_oprnd1;
7080f735 220 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
17211ab5 221 dw_cfi_oprnd2;
71dfc51f 222}
a3f97cbb
JW
223dw_cfi_node;
224
7d9d8943
AM
225/* This is how we define the location of the CFA. We use to handle it
226 as REG + OFFSET all the time, but now it can be more complex.
227 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
556273e0 228 Instead of passing around REG and OFFSET, we pass a copy
7d9d8943 229 of this structure. */
17211ab5 230typedef struct cfa_loc GTY(())
7d9d8943 231{
556273e0 232 unsigned long reg;
799f628a
JH
233 HOST_WIDE_INT offset;
234 HOST_WIDE_INT base_offset;
7d9d8943
AM
235 int indirect; /* 1 if CFA is accessed via a dereference. */
236} dw_cfa_location;
237
a3f97cbb 238/* All call frame descriptions (FDE's) in the GCC generated DWARF
4b674448 239 refer to a single Common Information Entry (CIE), defined at
fb530c07 240 the beginning of the .debug_frame section. This use of a single
a3f97cbb
JW
241 CIE obviates the need to keep track of multiple CIE's
242 in the DWARF generation routines below. */
71dfc51f 243
17211ab5 244typedef struct dw_fde_struct GTY(())
71dfc51f 245{
4746cf84 246 tree decl;
d3e3972c
KG
247 const char *dw_fde_begin;
248 const char *dw_fde_current_label;
249 const char *dw_fde_end;
71dfc51f 250 dw_cfi_ref dw_fde_cfi;
52a11cbf 251 unsigned funcdef_number;
b6128b8c 252 unsigned all_throwers_are_sibcalls : 1;
52a11cbf
RH
253 unsigned nothrow : 1;
254 unsigned uses_eh_lsda : 1;
71dfc51f 255}
a3f97cbb
JW
256dw_fde_node;
257
6d2f8887 258/* Maximum size (in bytes) of an artificially generated label. */
a3f97cbb
JW
259#define MAX_ARTIFICIAL_LABEL_BYTES 30
260
a1a4189d
JB
261/* The size of addresses as they appear in the Dwarf 2 data.
262 Some architectures use word addresses to refer to code locations,
263 but Dwarf 2 info always uses byte addresses. On such machines,
264 Dwarf 2 addresses need to be larger than the architecture's
265 pointers. */
266#ifndef DWARF2_ADDR_SIZE
267#define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
268#endif
269
7e23cb16 270/* The size in bytes of a DWARF field indicating an offset or length
a1a4189d
JB
271 relative to a debug info section, specified to be 4 bytes in the
272 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
b13fe8bf 273 as PTR_SIZE. */
71dfc51f 274
7e23cb16
JM
275#ifndef DWARF_OFFSET_SIZE
276#define DWARF_OFFSET_SIZE 4
277#endif
278
9eb0ef7a
KB
279/* According to the (draft) DWARF 3 specification, the initial length
280 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
281 bytes are 0xffffffff, followed by the length stored in the next 8
282 bytes.
283
284 However, the SGI/MIPS ABI uses an initial length which is equal to
285 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
286
287#ifndef DWARF_INITIAL_LENGTH_SIZE
288#define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
289#endif
290
9a666dda
JM
291#define DWARF_VERSION 2
292
7e23cb16
JM
293/* Round SIZE up to the nearest BOUNDARY. */
294#define DWARF_ROUND(SIZE,BOUNDARY) \
262b6384 295 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
a3f97cbb 296
a3f97cbb 297/* Offsets recorded in opcodes are a multiple of this alignment factor. */
27c35f4b 298#ifndef DWARF_CIE_DATA_ALIGNMENT
469ac993 299#ifdef STACK_GROWS_DOWNWARD
08cb3d38 300#define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
469ac993 301#else
08cb3d38 302#define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
469ac993 303#endif
2ad9852d 304#endif
a3f97cbb 305
3f76745e
JM
306/* A pointer to the base of a table that contains frame description
307 information for each routine. */
17211ab5 308static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
a3f97cbb 309
3f76745e 310/* Number of elements currently allocated for fde_table. */
c2e9147c 311static GTY(()) unsigned fde_table_allocated;
a94dbf2c 312
3f76745e 313/* Number of elements in fde_table currently in use. */
044b4de3 314static GTY(()) unsigned fde_table_in_use;
a3f97cbb 315
3f76745e
JM
316/* Size (in elements) of increments by which we may expand the
317 fde_table. */
318#define FDE_TABLE_INCREMENT 256
a3f97cbb 319
a94dbf2c 320/* A list of call frame insns for the CIE. */
17211ab5 321static GTY(()) dw_cfi_ref cie_cfi_head;
a94dbf2c 322
c1b50e49 323#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
a3f97cbb
JW
324/* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
325 attribute that accelerates the lookup of the FDE associated
556273e0 326 with the subprogram. This variable holds the table index of the FDE
a3f97cbb
JW
327 associated with the current function (body) definition. */
328static unsigned current_funcdef_fde;
c1b50e49 329#endif
a3f97cbb 330
17211ab5 331struct indirect_string_node GTY(())
9eb4015a 332{
17211ab5 333 const char *str;
9eb4015a
JJ
334 unsigned int refcount;
335 unsigned int form;
336 char *label;
337};
338
17211ab5
GK
339static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
340
341static GTY(()) int dw2_string_counter;
044b4de3 342static GTY(()) unsigned long dwarf2out_cfi_label_num;
17211ab5
GK
343
344#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
345
a3f97cbb 346/* Forward declarations for functions defined in this file. */
71dfc51f 347
7080f735
AJ
348static char *stripattributes (const char *);
349static const char *dwarf_cfi_name (unsigned);
350static dw_cfi_ref new_cfi (void);
351static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
352static void add_fde_cfi (const char *, dw_cfi_ref);
353static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
354static void lookup_cfa (dw_cfa_location *);
799f628a 355static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
7080f735 356static void initial_return_save (rtx);
799f628a 357static HOST_WIDE_INT stack_adjust_offset (rtx);
7080f735
AJ
358static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
359static void output_call_frame_info (int);
360static void dwarf2out_stack_adjust (rtx);
799f628a 361static void queue_reg_save (const char *, rtx, HOST_WIDE_INT);
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
7080f735
AJ
371 (dw_cfa_location *);
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
3f76745e 425
2ad9852d
RK
426/* The offset from the incoming value of %sp to the top of the stack frame
427 for the current function. */
428#ifndef INCOMING_FRAME_SP_OFFSET
429#define INCOMING_FRAME_SP_OFFSET 0
430#endif
431\f
0021b564
JM
432/* Hook used by __throw. */
433
434rtx
7080f735 435expand_builtin_dwarf_sp_column (void)
0021b564 436{
9c80ff25 437 return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
0021b564
JM
438}
439
71dfc51f 440/* Return a pointer to a copy of the section string name S with all
bf20f341 441 attributes stripped off, and an asterisk prepended (for assemble_name). */
71dfc51f
RK
442
443static inline char *
7080f735 444stripattributes (const char *s)
a3f97cbb 445{
bf20f341 446 char *stripped = xmalloc (strlen (s) + 2);
71dfc51f
RK
447 char *p = stripped;
448
bf20f341
JW
449 *p++ = '*';
450
451 while (*s && *s != ',')
452 *p++ = *s++;
71dfc51f 453
a3f97cbb
JW
454 *p = '\0';
455 return stripped;
456}
457
d9d5c9de 458/* Generate code to initialize the register size table. */
2f3ca9e7 459
d9d5c9de 460void
7080f735 461expand_builtin_init_dwarf_reg_sizes (tree address)
2f3ca9e7 462{
d9d5c9de
BS
463 int i;
464 enum machine_mode mode = TYPE_MODE (char_type_node);
465 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
2ad9852d 466 rtx mem = gen_rtx_MEM (BLKmode, addr);
71628aa0 467 bool wrote_return_column = false;
2f3ca9e7 468
91ea38f9
JH
469 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
470 if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
471 {
472 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
fee226d2
R
473 enum machine_mode save_mode = reg_raw_mode[i];
474 HOST_WIDE_INT size;
2f3ca9e7 475
fee226d2
R
476 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
477 save_mode = choose_hard_reg_mode (i, 1, true);
71628aa0
R
478 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
479 {
480 if (save_mode == VOIDmode)
481 continue;
482 wrote_return_column = true;
483 }
fee226d2 484 size = GET_MODE_SIZE (save_mode);
91ea38f9
JH
485 if (offset < 0)
486 continue;
c699cee9 487
91ea38f9
JH
488 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
489 }
ed80cd68
RH
490
491#ifdef DWARF_ALT_FRAME_RETURN_COLUMN
492 if (! wrote_return_column)
493 abort ();
494 i = DWARF_ALT_FRAME_RETURN_COLUMN;
495 wrote_return_column = false;
496#else
497 i = DWARF_FRAME_RETURN_COLUMN;
498#endif
499
71628aa0
R
500 if (! wrote_return_column)
501 {
502 enum machine_mode save_mode = Pmode;
ed80cd68 503 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
71628aa0
R
504 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
505 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
506 }
2f3ca9e7
JM
507}
508
3f76745e 509/* Convert a DWARF call frame info. operation to its string name */
a3f97cbb 510
d560ee52 511static const char *
7080f735 512dwarf_cfi_name (unsigned int cfi_opc)
3f76745e
JM
513{
514 switch (cfi_opc)
515 {
516 case DW_CFA_advance_loc:
517 return "DW_CFA_advance_loc";
518 case DW_CFA_offset:
519 return "DW_CFA_offset";
520 case DW_CFA_restore:
521 return "DW_CFA_restore";
522 case DW_CFA_nop:
523 return "DW_CFA_nop";
524 case DW_CFA_set_loc:
525 return "DW_CFA_set_loc";
526 case DW_CFA_advance_loc1:
527 return "DW_CFA_advance_loc1";
528 case DW_CFA_advance_loc2:
529 return "DW_CFA_advance_loc2";
530 case DW_CFA_advance_loc4:
531 return "DW_CFA_advance_loc4";
532 case DW_CFA_offset_extended:
533 return "DW_CFA_offset_extended";
534 case DW_CFA_restore_extended:
535 return "DW_CFA_restore_extended";
536 case DW_CFA_undefined:
537 return "DW_CFA_undefined";
538 case DW_CFA_same_value:
539 return "DW_CFA_same_value";
540 case DW_CFA_register:
541 return "DW_CFA_register";
542 case DW_CFA_remember_state:
543 return "DW_CFA_remember_state";
544 case DW_CFA_restore_state:
545 return "DW_CFA_restore_state";
546 case DW_CFA_def_cfa:
547 return "DW_CFA_def_cfa";
548 case DW_CFA_def_cfa_register:
549 return "DW_CFA_def_cfa_register";
550 case DW_CFA_def_cfa_offset:
551 return "DW_CFA_def_cfa_offset";
6bb28965
JM
552
553 /* DWARF 3 */
7d9d8943
AM
554 case DW_CFA_def_cfa_expression:
555 return "DW_CFA_def_cfa_expression";
6bb28965
JM
556 case DW_CFA_expression:
557 return "DW_CFA_expression";
558 case DW_CFA_offset_extended_sf:
559 return "DW_CFA_offset_extended_sf";
560 case DW_CFA_def_cfa_sf:
561 return "DW_CFA_def_cfa_sf";
562 case DW_CFA_def_cfa_offset_sf:
563 return "DW_CFA_def_cfa_offset_sf";
c53aa195 564
3f76745e
JM
565 /* SGI/MIPS specific */
566 case DW_CFA_MIPS_advance_loc8:
567 return "DW_CFA_MIPS_advance_loc8";
c53aa195
JM
568
569 /* GNU extensions */
570 case DW_CFA_GNU_window_save:
571 return "DW_CFA_GNU_window_save";
0021b564
JM
572 case DW_CFA_GNU_args_size:
573 return "DW_CFA_GNU_args_size";
3f388b42
GK
574 case DW_CFA_GNU_negative_offset_extended:
575 return "DW_CFA_GNU_negative_offset_extended";
c53aa195 576
3f76745e
JM
577 default:
578 return "DW_CFA_<unknown>";
579 }
580}
a3f97cbb 581
3f76745e 582/* Return a pointer to a newly allocated Call Frame Instruction. */
71dfc51f 583
3f76745e 584static inline dw_cfi_ref
7080f735 585new_cfi (void)
3f76745e 586{
703ad42b 587 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
71dfc51f 588
3f76745e
JM
589 cfi->dw_cfi_next = NULL;
590 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
591 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
a3f97cbb 592
3f76745e
JM
593 return cfi;
594}
a3f97cbb 595
3f76745e 596/* Add a Call Frame Instruction to list of instructions. */
a3f97cbb 597
3f76745e 598static inline void
7080f735 599add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
3f76745e 600{
b3694847 601 dw_cfi_ref *p;
a3f97cbb 602
3f76745e
JM
603 /* Find the end of the chain. */
604 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
605 ;
606
607 *p = cfi;
a3f97cbb
JW
608}
609
3f76745e 610/* Generate a new label for the CFI info to refer to. */
71dfc51f 611
c53aa195 612char *
7080f735 613dwarf2out_cfi_label (void)
a3f97cbb 614{
3f76745e 615 static char label[20];
556273e0 616
044b4de3 617 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
3f76745e 618 ASM_OUTPUT_LABEL (asm_out_file, label);
3f76745e 619 return label;
a3f97cbb
JW
620}
621
3f76745e
JM
622/* Add CFI to the current fde at the PC value indicated by LABEL if specified,
623 or to the CIE if LABEL is NULL. */
71dfc51f 624
3f76745e 625static void
7080f735 626add_fde_cfi (const char *label, dw_cfi_ref cfi)
a3f97cbb 627{
3f76745e
JM
628 if (label)
629 {
b3694847 630 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
a3f97cbb 631
3f76745e
JM
632 if (*label == 0)
633 label = dwarf2out_cfi_label ();
71dfc51f 634
3f76745e
JM
635 if (fde->dw_fde_current_label == NULL
636 || strcmp (label, fde->dw_fde_current_label) != 0)
637 {
b3694847 638 dw_cfi_ref xcfi;
a3f97cbb 639
3f76745e 640 fde->dw_fde_current_label = label = xstrdup (label);
71dfc51f 641
3f76745e
JM
642 /* Set the location counter to the new label. */
643 xcfi = new_cfi ();
644 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
645 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
646 add_cfi (&fde->dw_fde_cfi, xcfi);
647 }
71dfc51f 648
3f76745e
JM
649 add_cfi (&fde->dw_fde_cfi, cfi);
650 }
651
652 else
653 add_cfi (&cie_cfi_head, cfi);
a3f97cbb
JW
654}
655
3f76745e 656/* Subroutine of lookup_cfa. */
71dfc51f 657
3f76745e 658static inline void
7080f735 659lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
a3f97cbb 660{
3f76745e
JM
661 switch (cfi->dw_cfi_opc)
662 {
663 case DW_CFA_def_cfa_offset:
7d9d8943 664 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
3f76745e
JM
665 break;
666 case DW_CFA_def_cfa_register:
7d9d8943 667 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
3f76745e
JM
668 break;
669 case DW_CFA_def_cfa:
7d9d8943
AM
670 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
671 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
672 break;
673 case DW_CFA_def_cfa_expression:
674 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
3f76745e 675 break;
e9a25f70
JL
676 default:
677 break;
3f76745e 678 }
a3f97cbb
JW
679}
680
3f76745e 681/* Find the previous value for the CFA. */
71dfc51f 682
3f76745e 683static void
7080f735 684lookup_cfa (dw_cfa_location *loc)
a3f97cbb 685{
b3694847 686 dw_cfi_ref cfi;
3f76745e 687
7d9d8943
AM
688 loc->reg = (unsigned long) -1;
689 loc->offset = 0;
690 loc->indirect = 0;
691 loc->base_offset = 0;
3f76745e
JM
692
693 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
7d9d8943 694 lookup_cfa_1 (cfi, loc);
3f76745e
JM
695
696 if (fde_table_in_use)
a3f97cbb 697 {
b3694847 698 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
3f76745e 699 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
7d9d8943 700 lookup_cfa_1 (cfi, loc);
a3f97cbb
JW
701 }
702}
703
3f76745e 704/* The current rule for calculating the DWARF2 canonical frame address. */
fbfa55b0 705static dw_cfa_location cfa;
71dfc51f 706
3f76745e
JM
707/* The register used for saving registers to the stack, and its offset
708 from the CFA. */
fbfa55b0 709static dw_cfa_location cfa_store;
3f76745e 710
0021b564 711/* The running total of the size of arguments pushed onto the stack. */
799f628a 712static HOST_WIDE_INT args_size;
0021b564 713
b57d9225 714/* The last args_size we actually output. */
799f628a 715static HOST_WIDE_INT old_args_size;
b57d9225 716
3f76745e
JM
717/* Entry point to update the canonical frame address (CFA).
718 LABEL is passed to add_fde_cfi. The value of CFA is now to be
719 calculated from REG+OFFSET. */
720
721void
799f628a 722dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
7d9d8943
AM
723{
724 dw_cfa_location loc;
725 loc.indirect = 0;
726 loc.base_offset = 0;
727 loc.reg = reg;
728 loc.offset = offset;
729 def_cfa_1 (label, &loc);
730}
731
770ca8c6 732/* This routine does the actual work. The CFA is now calculated from
7d9d8943 733 the dw_cfa_location structure. */
2ad9852d 734
7d9d8943 735static void
7080f735 736def_cfa_1 (const char *label, dw_cfa_location *loc_p)
a3f97cbb 737{
b3694847 738 dw_cfi_ref cfi;
7d9d8943 739 dw_cfa_location old_cfa, loc;
3f76745e 740
7d9d8943
AM
741 cfa = *loc_p;
742 loc = *loc_p;
5bef9b1f 743
7d9d8943
AM
744 if (cfa_store.reg == loc.reg && loc.indirect == 0)
745 cfa_store.offset = loc.offset;
3f76745e 746
7d9d8943
AM
747 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
748 lookup_cfa (&old_cfa);
749
2ad9852d
RK
750 /* If nothing changed, no need to issue any call frame instructions. */
751 if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
752 && loc.indirect == old_cfa.indirect
753 && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
754 return;
3f76745e
JM
755
756 cfi = new_cfi ();
757
e09bbb25 758 if (loc.reg == old_cfa.reg && !loc.indirect)
a3f97cbb 759 {
770ca8c6
JO
760 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
761 indicating the CFA register did not change but the offset
762 did. */
3f76745e 763 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
7d9d8943 764 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
3f76745e 765 }
a3f97cbb 766
3f76745e 767#ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
7d9d8943 768 else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
e09bbb25 769 && !loc.indirect)
3f76745e 770 {
770ca8c6
JO
771 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
772 indicating the CFA register has changed to <register> but the
773 offset has not changed. */
3f76745e 774 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
7d9d8943 775 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
3f76745e
JM
776 }
777#endif
a3f97cbb 778
7d9d8943 779 else if (loc.indirect == 0)
3f76745e 780 {
770ca8c6
JO
781 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
782 indicating the CFA register has changed to <register> with
783 the specified offset. */
3f76745e 784 cfi->dw_cfi_opc = DW_CFA_def_cfa;
7d9d8943
AM
785 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
786 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
787 }
788 else
789 {
770ca8c6
JO
790 /* Construct a DW_CFA_def_cfa_expression instruction to
791 calculate the CFA using a full location expression since no
792 register-offset pair is available. */
556273e0 793 struct dw_loc_descr_struct *loc_list;
2ad9852d 794
7d9d8943
AM
795 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
796 loc_list = build_cfa_loc (&loc);
797 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
a3f97cbb 798 }
3f76745e
JM
799
800 add_fde_cfi (label, cfi);
a3f97cbb
JW
801}
802
3f76745e
JM
803/* Add the CFI for saving a register. REG is the CFA column number.
804 LABEL is passed to add_fde_cfi.
805 If SREG is -1, the register is saved at OFFSET from the CFA;
806 otherwise it is saved in SREG. */
71dfc51f 807
3f76745e 808static void
799f628a 809reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
a3f97cbb 810{
b3694847 811 dw_cfi_ref cfi = new_cfi ();
3f76745e
JM
812
813 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
814
85066503
MH
815 /* The following comparison is correct. -1 is used to indicate that
816 the value isn't a register number. */
817 if (sreg == (unsigned int) -1)
a3f97cbb 818 {
3f76745e
JM
819 if (reg & ~0x3f)
820 /* The register number won't fit in 6 bits, so we have to use
821 the long form. */
822 cfi->dw_cfi_opc = DW_CFA_offset_extended;
823 else
824 cfi->dw_cfi_opc = DW_CFA_offset;
825
27c35f4b
HPN
826#ifdef ENABLE_CHECKING
827 {
828 /* If we get an offset that is not a multiple of
829 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
830 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
831 description. */
799f628a 832 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
27c35f4b
HPN
833
834 if (check_offset * DWARF_CIE_DATA_ALIGNMENT != offset)
835 abort ();
836 }
837#endif
3f76745e 838 offset /= DWARF_CIE_DATA_ALIGNMENT;
3a88cbd1 839 if (offset < 0)
6bb28965 840 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
2ad9852d 841
3f76745e
JM
842 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
843 }
2c849145
JM
844 else if (sreg == reg)
845 /* We could emit a DW_CFA_same_value in this case, but don't bother. */
846 return;
3f76745e
JM
847 else
848 {
849 cfi->dw_cfi_opc = DW_CFA_register;
850 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
851 }
852
853 add_fde_cfi (label, cfi);
854}
855
c53aa195
JM
856/* Add the CFI for saving a register window. LABEL is passed to reg_save.
857 This CFI tells the unwinder that it needs to restore the window registers
858 from the previous frame's window save area.
556273e0 859
c53aa195
JM
860 ??? Perhaps we should note in the CIE where windows are saved (instead of
861 assuming 0(cfa)) and what registers are in the window. */
862
863void
7080f735 864dwarf2out_window_save (const char *label)
c53aa195 865{
b3694847 866 dw_cfi_ref cfi = new_cfi ();
2ad9852d 867
c53aa195
JM
868 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
869 add_fde_cfi (label, cfi);
870}
871
0021b564
JM
872/* Add a CFI to update the running total of the size of arguments
873 pushed onto the stack. */
874
875void
799f628a 876dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
0021b564 877{
b3694847 878 dw_cfi_ref cfi;
b57d9225
JM
879
880 if (size == old_args_size)
881 return;
2ad9852d 882
b57d9225
JM
883 old_args_size = size;
884
885 cfi = new_cfi ();
0021b564
JM
886 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
887 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
888 add_fde_cfi (label, cfi);
889}
890
c53aa195
JM
891/* Entry point for saving a register to the stack. REG is the GCC register
892 number. LABEL and OFFSET are passed to reg_save. */
3f76745e
JM
893
894void
799f628a 895dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
3f76745e
JM
896{
897 reg_save (label, DWARF_FRAME_REGNUM (reg), -1, offset);
898}
899
c53aa195
JM
900/* Entry point for saving the return address in the stack.
901 LABEL and OFFSET are passed to reg_save. */
902
903void
799f628a 904dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
c53aa195
JM
905{
906 reg_save (label, DWARF_FRAME_RETURN_COLUMN, -1, offset);
907}
908
909/* Entry point for saving the return address in a register.
910 LABEL and SREG are passed to reg_save. */
911
912void
7080f735 913dwarf2out_return_reg (const char *label, unsigned int sreg)
c53aa195
JM
914{
915 reg_save (label, DWARF_FRAME_RETURN_COLUMN, sreg, 0);
916}
917
3f76745e
JM
918/* Record the initial position of the return address. RTL is
919 INCOMING_RETURN_ADDR_RTX. */
920
921static void
7080f735 922initial_return_save (rtx rtl)
3f76745e 923{
973838fd 924 unsigned int reg = (unsigned int) -1;
2ad9852d 925 HOST_WIDE_INT offset = 0;
3f76745e
JM
926
927 switch (GET_CODE (rtl))
928 {
929 case REG:
930 /* RA is in a register. */
2c849145 931 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
3f76745e 932 break;
2ad9852d 933
3f76745e
JM
934 case MEM:
935 /* RA is on the stack. */
936 rtl = XEXP (rtl, 0);
937 switch (GET_CODE (rtl))
938 {
939 case REG:
3a88cbd1
JL
940 if (REGNO (rtl) != STACK_POINTER_REGNUM)
941 abort ();
3f76745e
JM
942 offset = 0;
943 break;
2ad9852d 944
3f76745e 945 case PLUS:
3a88cbd1
JL
946 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
947 abort ();
3f76745e
JM
948 offset = INTVAL (XEXP (rtl, 1));
949 break;
2ad9852d 950
3f76745e 951 case MINUS:
3a88cbd1
JL
952 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
953 abort ();
3f76745e
JM
954 offset = -INTVAL (XEXP (rtl, 1));
955 break;
2ad9852d 956
3f76745e
JM
957 default:
958 abort ();
959 }
2ad9852d 960
3f76745e 961 break;
2ad9852d 962
c53aa195
JM
963 case PLUS:
964 /* The return address is at some offset from any value we can
965 actually load. For instance, on the SPARC it is in %i7+8. Just
966 ignore the offset for now; it doesn't matter for unwinding frames. */
3a88cbd1
JL
967 if (GET_CODE (XEXP (rtl, 1)) != CONST_INT)
968 abort ();
c53aa195
JM
969 initial_return_save (XEXP (rtl, 0));
970 return;
2ad9852d 971
a3f97cbb 972 default:
3f76745e 973 abort ();
a3f97cbb 974 }
3f76745e 975
7d9d8943 976 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
a3f97cbb
JW
977}
978
1ba5ae8f 979/* Given a SET, calculate the amount of stack adjustment it
30f7a378 980 contains. */
1ba5ae8f 981
799f628a 982static HOST_WIDE_INT
7080f735 983stack_adjust_offset (rtx pattern)
1ba5ae8f
AH
984{
985 rtx src = SET_SRC (pattern);
986 rtx dest = SET_DEST (pattern);
2ad9852d 987 HOST_WIDE_INT offset = 0;
1ba5ae8f
AH
988 enum rtx_code code;
989
990 if (dest == stack_pointer_rtx)
991 {
992 /* (set (reg sp) (plus (reg sp) (const_int))) */
993 code = GET_CODE (src);
994 if (! (code == PLUS || code == MINUS)
995 || XEXP (src, 0) != stack_pointer_rtx
996 || GET_CODE (XEXP (src, 1)) != CONST_INT)
997 return 0;
998
999 offset = INTVAL (XEXP (src, 1));
f472fa29
AM
1000 if (code == PLUS)
1001 offset = -offset;
1ba5ae8f
AH
1002 }
1003 else if (GET_CODE (dest) == MEM)
1004 {
1005 /* (set (mem (pre_dec (reg sp))) (foo)) */
1006 src = XEXP (dest, 0);
1007 code = GET_CODE (src);
1008
c26fbbca
KH
1009 switch (code)
1010 {
f472fa29
AM
1011 case PRE_MODIFY:
1012 case POST_MODIFY:
1013 if (XEXP (src, 0) == stack_pointer_rtx)
1014 {
1015 rtx val = XEXP (XEXP (src, 1), 1);
1016 /* We handle only adjustments by constant amount. */
1017 if (GET_CODE (XEXP (src, 1)) != PLUS ||
1018 GET_CODE (val) != CONST_INT)
c26fbbca 1019 abort ();
f472fa29
AM
1020 offset = -INTVAL (val);
1021 break;
1022 }
1023 return 0;
1024
1025 case PRE_DEC:
1026 case POST_DEC:
1027 if (XEXP (src, 0) == stack_pointer_rtx)
1028 {
1029 offset = GET_MODE_SIZE (GET_MODE (dest));
1030 break;
1031 }
1032 return 0;
1033
1034 case PRE_INC:
1035 case POST_INC:
1036 if (XEXP (src, 0) == stack_pointer_rtx)
1037 {
1038 offset = -GET_MODE_SIZE (GET_MODE (dest));
1039 break;
1040 }
1041 return 0;
2ad9852d 1042
f472fa29
AM
1043 default:
1044 return 0;
e2134eea 1045 }
1ba5ae8f
AH
1046 }
1047 else
1048 return 0;
1049
1ba5ae8f
AH
1050 return offset;
1051}
1052
0021b564
JM
1053/* Check INSN to see if it looks like a push or a stack adjustment, and
1054 make a note of it if it does. EH uses this information to find out how
1055 much extra space it needs to pop off the stack. */
1056
1057static void
7080f735 1058dwarf2out_stack_adjust (rtx insn)
0021b564 1059{
2ad9852d 1060 HOST_WIDE_INT offset;
d3e3972c 1061 const char *label;
2ad9852d 1062 int i;
0021b564 1063
b298f00f
RH
1064 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1065 with this function. Proper support would require all frame-related
1066 insns to be marked, and to be able to handle saving state around
1067 epilogues textually in the middle of the function. */
1068 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1069 return;
1070
2ad9852d 1071 if (!flag_asynchronous_unwind_tables && GET_CODE (insn) == CALL_INSN)
b57d9225
JM
1072 {
1073 /* Extract the size of the args from the CALL rtx itself. */
b57d9225
JM
1074 insn = PATTERN (insn);
1075 if (GET_CODE (insn) == PARALLEL)
1076 insn = XVECEXP (insn, 0, 0);
1077 if (GET_CODE (insn) == SET)
1078 insn = SET_SRC (insn);
3db35af4
MM
1079 if (GET_CODE (insn) != CALL)
1080 abort ();
2ad9852d 1081
b57d9225
JM
1082 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1083 return;
1084 }
1085
1086 /* If only calls can throw, and we have a frame pointer,
1087 save up adjustments until we see the CALL_INSN. */
2ad9852d 1088 else if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
b57d9225
JM
1089 return;
1090
6020d360 1091 if (GET_CODE (insn) == BARRIER)
0021b564 1092 {
6020d360
JM
1093 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1094 the compiler will have already emitted a stack adjustment, but
1095 doesn't bother for calls to noreturn functions. */
1096#ifdef STACK_GROWS_DOWNWARD
1097 offset = -args_size;
1098#else
1099 offset = args_size;
1100#endif
0021b564 1101 }
6020d360 1102 else if (GET_CODE (PATTERN (insn)) == SET)
2ad9852d 1103 offset = stack_adjust_offset (PATTERN (insn));
1ba5ae8f
AH
1104 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1105 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1106 {
1107 /* There may be stack adjustments inside compound insns. Search
2ad9852d
RK
1108 for them. */
1109 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1110 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1111 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
0021b564
JM
1112 }
1113 else
1114 return;
0b34cf1e 1115
6020d360
JM
1116 if (offset == 0)
1117 return;
1118
7d9d8943
AM
1119 if (cfa.reg == STACK_POINTER_REGNUM)
1120 cfa.offset += offset;
0021b564
JM
1121
1122#ifndef STACK_GROWS_DOWNWARD
1123 offset = -offset;
1124#endif
2ad9852d 1125
0021b564
JM
1126 args_size += offset;
1127 if (args_size < 0)
1128 args_size = 0;
1129
1130 label = dwarf2out_cfi_label ();
7d9d8943 1131 def_cfa_1 (label, &cfa);
0021b564
JM
1132 dwarf2out_args_size (label, args_size);
1133}
1134
17211ab5
GK
1135#endif
1136
fbfa55b0
RH
1137/* We delay emitting a register save until either (a) we reach the end
1138 of the prologue or (b) the register is clobbered. This clusters
1139 register saves so that there are fewer pc advances. */
1140
17211ab5 1141struct queued_reg_save GTY(())
fbfa55b0
RH
1142{
1143 struct queued_reg_save *next;
1144 rtx reg;
799f628a 1145 HOST_WIDE_INT cfa_offset;
fbfa55b0
RH
1146};
1147
17211ab5
GK
1148static GTY(()) struct queued_reg_save *queued_reg_saves;
1149
1150#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
fbfa55b0
RH
1151static const char *last_reg_save_label;
1152
1153static void
799f628a 1154queue_reg_save (const char *label, rtx reg, HOST_WIDE_INT offset)
fbfa55b0 1155{
17211ab5 1156 struct queued_reg_save *q = ggc_alloc (sizeof (*q));
fbfa55b0
RH
1157
1158 q->next = queued_reg_saves;
1159 q->reg = reg;
1160 q->cfa_offset = offset;
1161 queued_reg_saves = q;
1162
1163 last_reg_save_label = label;
1164}
1165
1166static void
7080f735 1167flush_queued_reg_saves (void)
fbfa55b0
RH
1168{
1169 struct queued_reg_save *q, *next;
1170
c26fbbca 1171 for (q = queued_reg_saves; q; q = next)
fbfa55b0
RH
1172 {
1173 dwarf2out_reg_save (last_reg_save_label, REGNO (q->reg), q->cfa_offset);
1174 next = q->next;
fbfa55b0
RH
1175 }
1176
1177 queued_reg_saves = NULL;
1178 last_reg_save_label = NULL;
1179}
1180
1181static bool
7080f735 1182clobbers_queued_reg_save (rtx insn)
fbfa55b0
RH
1183{
1184 struct queued_reg_save *q;
1185
c26fbbca 1186 for (q = queued_reg_saves; q; q = q->next)
fbfa55b0
RH
1187 if (modified_in_p (q->reg, insn))
1188 return true;
1189
1190 return false;
1191}
c26fbbca 1192
fbfa55b0 1193
770ca8c6
JO
1194/* A temporary register holding an integral value used in adjusting SP
1195 or setting up the store_reg. The "offset" field holds the integer
1196 value, not an offset. */
fbfa55b0 1197static dw_cfa_location cfa_temp;
770ca8c6
JO
1198
1199/* Record call frame debugging information for an expression EXPR,
1200 which either sets SP or FP (adjusting how we calculate the frame
1201 address) or saves a register to the stack. LABEL indicates the
1202 address of EXPR.
1203
1204 This function encodes a state machine mapping rtxes to actions on
1205 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1206 users need not read the source code.
1207
a401107d
JO
1208 The High-Level Picture
1209
1210 Changes in the register we use to calculate the CFA: Currently we
1211 assume that if you copy the CFA register into another register, we
1212 should take the other one as the new CFA register; this seems to
1213 work pretty well. If it's wrong for some target, it's simple
1214 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1215
1216 Changes in the register we use for saving registers to the stack:
1217 This is usually SP, but not always. Again, we deduce that if you
1218 copy SP into another register (and SP is not the CFA register),
1219 then the new register is the one we will be using for register
1220 saves. This also seems to work.
1221
1222 Register saves: There's not much guesswork about this one; if
1223 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1224 register save, and the register used to calculate the destination
1225 had better be the one we think we're using for this purpose.
1226
1227 Except: If the register being saved is the CFA register, and the
cc2902df 1228 offset is nonzero, we are saving the CFA, so we assume we have to
a401107d
JO
1229 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1230 the intent is to save the value of SP from the previous frame.
1231
770ca8c6
JO
1232 Invariants / Summaries of Rules
1233
a401107d
JO
1234 cfa current rule for calculating the CFA. It usually
1235 consists of a register and an offset.
770ca8c6
JO
1236 cfa_store register used by prologue code to save things to the stack
1237 cfa_store.offset is the offset from the value of
1238 cfa_store.reg to the actual CFA
1239 cfa_temp register holding an integral value. cfa_temp.offset
1240 stores the value, which will be used to adjust the
19ec6a36
AM
1241 stack pointer. cfa_temp is also used like cfa_store,
1242 to track stores to the stack via fp or a temp reg.
c26fbbca 1243
770ca8c6 1244 Rules 1- 4: Setting a register's value to cfa.reg or an expression
7080f735 1245 with cfa.reg as the first operand changes the cfa.reg and its
19ec6a36
AM
1246 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1247 cfa_temp.offset.
770ca8c6
JO
1248
1249 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1250 expression yielding a constant. This sets cfa_temp.reg
1251 and cfa_temp.offset.
1252
1253 Rule 5: Create a new register cfa_store used to save items to the
1254 stack.
1255
19ec6a36 1256 Rules 10-14: Save a register to the stack. Define offset as the
a401107d 1257 difference of the original location and cfa_store's
19ec6a36 1258 location (or cfa_temp's location if cfa_temp is used).
770ca8c6
JO
1259
1260 The Rules
1261
1262 "{a,b}" indicates a choice of a xor b.
1263 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1264
1265 Rule 1:
1266 (set <reg1> <reg2>:cfa.reg)
19ec6a36 1267 effects: cfa.reg = <reg1>
73c68f61 1268 cfa.offset unchanged
19ec6a36
AM
1269 cfa_temp.reg = <reg1>
1270 cfa_temp.offset = cfa.offset
770ca8c6
JO
1271
1272 Rule 2:
2ad9852d
RK
1273 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1274 {<const_int>,<reg>:cfa_temp.reg}))
770ca8c6 1275 effects: cfa.reg = sp if fp used
7080f735 1276 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
770ca8c6
JO
1277 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1278 if cfa_store.reg==sp
1279
1280 Rule 3:
19ec6a36 1281 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
770ca8c6 1282 effects: cfa.reg = fp
7080f735 1283 cfa_offset += +/- <const_int>
770ca8c6
JO
1284
1285 Rule 4:
19ec6a36 1286 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
770ca8c6 1287 constraints: <reg1> != fp
7080f735 1288 <reg1> != sp
770ca8c6 1289 effects: cfa.reg = <reg1>
19ec6a36
AM
1290 cfa_temp.reg = <reg1>
1291 cfa_temp.offset = cfa.offset
770ca8c6
JO
1292
1293 Rule 5:
1294 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1295 constraints: <reg1> != fp
7080f735 1296 <reg1> != sp
770ca8c6 1297 effects: cfa_store.reg = <reg1>
7080f735 1298 cfa_store.offset = cfa.offset - cfa_temp.offset
770ca8c6
JO
1299
1300 Rule 6:
1301 (set <reg> <const_int>)
1302 effects: cfa_temp.reg = <reg>
7080f735 1303 cfa_temp.offset = <const_int>
770ca8c6
JO
1304
1305 Rule 7:
1306 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1307 effects: cfa_temp.reg = <reg1>
1308 cfa_temp.offset |= <const_int>
1309
1310 Rule 8:
1311 (set <reg> (high <exp>))
1312 effects: none
1313
1314 Rule 9:
1315 (set <reg> (lo_sum <exp> <const_int>))
1316 effects: cfa_temp.reg = <reg>
7080f735 1317 cfa_temp.offset = <const_int>
770ca8c6
JO
1318
1319 Rule 10:
1320 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1321 effects: cfa_store.offset -= <const_int>
1322 cfa.offset = cfa_store.offset if cfa.reg == sp
770ca8c6 1323 cfa.reg = sp
19ec6a36 1324 cfa.base_offset = -cfa_store.offset
770ca8c6
JO
1325
1326 Rule 11:
1327 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1328 effects: cfa_store.offset += -/+ mode_size(mem)
1329 cfa.offset = cfa_store.offset if cfa.reg == sp
770ca8c6 1330 cfa.reg = sp
19ec6a36 1331 cfa.base_offset = -cfa_store.offset
770ca8c6
JO
1332
1333 Rule 12:
2ad9852d
RK
1334 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1335
1336 <reg2>)
19ec6a36
AM
1337 effects: cfa.reg = <reg1>
1338 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
770ca8c6
JO
1339
1340 Rule 13:
19ec6a36
AM
1341 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1342 effects: cfa.reg = <reg1>
1343 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1344
1345 Rule 14:
1346 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1347 effects: cfa.reg = <reg1>
1348 cfa.base_offset = -cfa_temp.offset
1349 cfa_temp.offset -= mode_size(mem) */
b664de3a
AM
1350
1351static void
7080f735 1352dwarf2out_frame_debug_expr (rtx expr, const char *label)
b664de3a
AM
1353{
1354 rtx src, dest;
2ad9852d 1355 HOST_WIDE_INT offset;
556273e0
KH
1356
1357 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1358 the PARALLEL independently. The first element is always processed if
770ca8c6 1359 it is a SET. This is for backward compatibility. Other elements
556273e0
KH
1360 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1361 flag is set in them. */
2ad9852d 1362 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
556273e0 1363 {
b664de3a
AM
1364 int par_index;
1365 int limit = XVECLEN (expr, 0);
1366
1367 for (par_index = 0; par_index < limit; par_index++)
2ad9852d
RK
1368 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1369 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1370 || par_index == 0))
1371 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
556273e0 1372
b664de3a
AM
1373 return;
1374 }
556273e0 1375
b664de3a
AM
1376 if (GET_CODE (expr) != SET)
1377 abort ();
1378
1379 src = SET_SRC (expr);
1380 dest = SET_DEST (expr);
1381
1382 switch (GET_CODE (dest))
1383 {
1384 case REG:
770ca8c6 1385 /* Rule 1 */
b664de3a 1386 /* Update the CFA rule wrt SP or FP. Make sure src is
73c68f61 1387 relative to the current CFA register. */
b664de3a 1388 switch (GET_CODE (src))
556273e0
KH
1389 {
1390 /* Setting FP from SP. */
1391 case REG:
1392 if (cfa.reg == (unsigned) REGNO (src))
1393 /* OK. */
1394 ;
626d1efd 1395 else
556273e0 1396 abort ();
2c849145
JM
1397
1398 /* We used to require that dest be either SP or FP, but the
1399 ARM copies SP to a temporary register, and from there to
1400 FP. So we just rely on the backends to only set
1401 RTX_FRAME_RELATED_P on appropriate insns. */
556273e0 1402 cfa.reg = REGNO (dest);
19ec6a36
AM
1403 cfa_temp.reg = cfa.reg;
1404 cfa_temp.offset = cfa.offset;
556273e0 1405 break;
b664de3a 1406
556273e0
KH
1407 case PLUS:
1408 case MINUS:
19ec6a36 1409 case LO_SUM:
556273e0
KH
1410 if (dest == stack_pointer_rtx)
1411 {
770ca8c6 1412 /* Rule 2 */
2618f955
MM
1413 /* Adjusting SP. */
1414 switch (GET_CODE (XEXP (src, 1)))
1415 {
1416 case CONST_INT:
1417 offset = INTVAL (XEXP (src, 1));
1418 break;
1419 case REG:
770ca8c6 1420 if ((unsigned) REGNO (XEXP (src, 1)) != cfa_temp.reg)
2618f955 1421 abort ();
770ca8c6 1422 offset = cfa_temp.offset;
2618f955
MM
1423 break;
1424 default:
1425 abort ();
1426 }
1427
1428 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1429 {
1430 /* Restoring SP from FP in the epilogue. */
7d9d8943 1431 if (cfa.reg != (unsigned) HARD_FRAME_POINTER_REGNUM)
2618f955 1432 abort ();
7d9d8943 1433 cfa.reg = STACK_POINTER_REGNUM;
2618f955 1434 }
19ec6a36
AM
1435 else if (GET_CODE (src) == LO_SUM)
1436 /* Assume we've set the source reg of the LO_SUM from sp. */
1437 ;
2618f955
MM
1438 else if (XEXP (src, 0) != stack_pointer_rtx)
1439 abort ();
1440
19ec6a36 1441 if (GET_CODE (src) != MINUS)
2618f955 1442 offset = -offset;
7d9d8943
AM
1443 if (cfa.reg == STACK_POINTER_REGNUM)
1444 cfa.offset += offset;
1445 if (cfa_store.reg == STACK_POINTER_REGNUM)
1446 cfa_store.offset += offset;
556273e0
KH
1447 }
1448 else if (dest == hard_frame_pointer_rtx)
1449 {
770ca8c6 1450 /* Rule 3 */
2618f955
MM
1451 /* Either setting the FP from an offset of the SP,
1452 or adjusting the FP */
2c849145 1453 if (! frame_pointer_needed)
2618f955
MM
1454 abort ();
1455
2c849145 1456 if (GET_CODE (XEXP (src, 0)) == REG
7d9d8943 1457 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2618f955
MM
1458 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1459 {
2618f955 1460 offset = INTVAL (XEXP (src, 1));
19ec6a36 1461 if (GET_CODE (src) != MINUS)
2618f955 1462 offset = -offset;
7d9d8943
AM
1463 cfa.offset += offset;
1464 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2618f955 1465 }
556273e0
KH
1466 else
1467 abort ();
1468 }
1469 else
1470 {
19ec6a36 1471 if (GET_CODE (src) == MINUS)
2618f955 1472 abort ();
b53ef1a2 1473
770ca8c6 1474 /* Rule 4 */
b53ef1a2
NC
1475 if (GET_CODE (XEXP (src, 0)) == REG
1476 && REGNO (XEXP (src, 0)) == cfa.reg
1477 && GET_CODE (XEXP (src, 1)) == CONST_INT)
34ce3d7b
JM
1478 {
1479 /* Setting a temporary CFA register that will be copied
1480 into the FP later on. */
19ec6a36 1481 offset = - INTVAL (XEXP (src, 1));
34ce3d7b
JM
1482 cfa.offset += offset;
1483 cfa.reg = REGNO (dest);
19ec6a36
AM
1484 /* Or used to save regs to the stack. */
1485 cfa_temp.reg = cfa.reg;
1486 cfa_temp.offset = cfa.offset;
34ce3d7b 1487 }
2ad9852d 1488
770ca8c6 1489 /* Rule 5 */
19ec6a36
AM
1490 else if (GET_CODE (XEXP (src, 0)) == REG
1491 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1492 && XEXP (src, 1) == stack_pointer_rtx)
b53ef1a2 1493 {
00a42e21
JM
1494 /* Setting a scratch register that we will use instead
1495 of SP for saving registers to the stack. */
b53ef1a2
NC
1496 if (cfa.reg != STACK_POINTER_REGNUM)
1497 abort ();
1498 cfa_store.reg = REGNO (dest);
770ca8c6 1499 cfa_store.offset = cfa.offset - cfa_temp.offset;
b53ef1a2 1500 }
2ad9852d 1501
19ec6a36
AM
1502 /* Rule 9 */
1503 else if (GET_CODE (src) == LO_SUM
1504 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1505 {
1506 cfa_temp.reg = REGNO (dest);
1507 cfa_temp.offset = INTVAL (XEXP (src, 1));
1508 }
1509 else
1510 abort ();
556273e0
KH
1511 }
1512 break;
b664de3a 1513
770ca8c6 1514 /* Rule 6 */
556273e0 1515 case CONST_INT:
770ca8c6
JO
1516 cfa_temp.reg = REGNO (dest);
1517 cfa_temp.offset = INTVAL (src);
556273e0 1518 break;
b664de3a 1519
770ca8c6 1520 /* Rule 7 */
556273e0
KH
1521 case IOR:
1522 if (GET_CODE (XEXP (src, 0)) != REG
770ca8c6 1523 || (unsigned) REGNO (XEXP (src, 0)) != cfa_temp.reg
2618f955 1524 || GET_CODE (XEXP (src, 1)) != CONST_INT)
556273e0 1525 abort ();
2ad9852d 1526
770ca8c6
JO
1527 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1528 cfa_temp.reg = REGNO (dest);
1529 cfa_temp.offset |= INTVAL (XEXP (src, 1));
556273e0 1530 break;
b664de3a 1531
9ae21d2a
AM
1532 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1533 which will fill in all of the bits. */
1534 /* Rule 8 */
1535 case HIGH:
1536 break;
1537
556273e0
KH
1538 default:
1539 abort ();
1540 }
2ad9852d 1541
7d9d8943 1542 def_cfa_1 (label, &cfa);
2618f955 1543 break;
b664de3a 1544
2618f955 1545 case MEM:
2618f955
MM
1546 if (GET_CODE (src) != REG)
1547 abort ();
7d9d8943 1548
7d9d8943
AM
1549 /* Saving a register to the stack. Make sure dest is relative to the
1550 CFA register. */
2618f955
MM
1551 switch (GET_CODE (XEXP (dest, 0)))
1552 {
770ca8c6 1553 /* Rule 10 */
2618f955 1554 /* With a push. */
e2134eea
JH
1555 case PRE_MODIFY:
1556 /* We can't handle variable size modifications. */
1557 if (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1)) != CONST_INT)
173bf5be 1558 abort ();
e2134eea
JH
1559 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1560
1561 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1562 || cfa_store.reg != STACK_POINTER_REGNUM)
1563 abort ();
2ad9852d 1564
e2134eea
JH
1565 cfa_store.offset += offset;
1566 if (cfa.reg == STACK_POINTER_REGNUM)
1567 cfa.offset = cfa_store.offset;
1568
1569 offset = -cfa_store.offset;
1570 break;
2ad9852d 1571
770ca8c6 1572 /* Rule 11 */
2618f955
MM
1573 case PRE_INC:
1574 case PRE_DEC:
1575 offset = GET_MODE_SIZE (GET_MODE (dest));
1576 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1577 offset = -offset;
b664de3a 1578
2618f955 1579 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
7d9d8943 1580 || cfa_store.reg != STACK_POINTER_REGNUM)
2618f955 1581 abort ();
2ad9852d 1582
7d9d8943
AM
1583 cfa_store.offset += offset;
1584 if (cfa.reg == STACK_POINTER_REGNUM)
1585 cfa.offset = cfa_store.offset;
b664de3a 1586
7d9d8943 1587 offset = -cfa_store.offset;
2618f955 1588 break;
b664de3a 1589
770ca8c6 1590 /* Rule 12 */
2618f955
MM
1591 /* With an offset. */
1592 case PLUS:
1593 case MINUS:
19ec6a36 1594 case LO_SUM:
770ca8c6
JO
1595 if (GET_CODE (XEXP (XEXP (dest, 0), 1)) != CONST_INT)
1596 abort ();
2618f955
MM
1597 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1598 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1599 offset = -offset;
b664de3a 1600
19ec6a36
AM
1601 if (cfa_store.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1602 offset -= cfa_store.offset;
1603 else if (cfa_temp.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1604 offset -= cfa_temp.offset;
1605 else
2618f955 1606 abort ();
2618f955
MM
1607 break;
1608
770ca8c6 1609 /* Rule 13 */
2618f955
MM
1610 /* Without an offset. */
1611 case REG:
19ec6a36
AM
1612 if (cfa_store.reg == (unsigned) REGNO (XEXP (dest, 0)))
1613 offset = -cfa_store.offset;
1614 else if (cfa_temp.reg == (unsigned) REGNO (XEXP (dest, 0)))
1615 offset = -cfa_temp.offset;
1616 else
556273e0 1617 abort ();
19ec6a36
AM
1618 break;
1619
1620 /* Rule 14 */
1621 case POST_INC:
1622 if (cfa_temp.reg != (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1623 abort ();
1624 offset = -cfa_temp.offset;
1625 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2618f955
MM
1626 break;
1627
1628 default:
1629 abort ();
1630 }
e09bbb25 1631
556273e0 1632 if (REGNO (src) != STACK_POINTER_REGNUM
e09bbb25
JM
1633 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1634 && (unsigned) REGNO (src) == cfa.reg)
1635 {
1636 /* We're storing the current CFA reg into the stack. */
1637
1638 if (cfa.offset == 0)
1639 {
1640 /* If the source register is exactly the CFA, assume
1641 we're saving SP like any other register; this happens
1642 on the ARM. */
e09bbb25 1643 def_cfa_1 (label, &cfa);
fbfa55b0 1644 queue_reg_save (label, stack_pointer_rtx, offset);
e09bbb25
JM
1645 break;
1646 }
1647 else
1648 {
1649 /* Otherwise, we'll need to look in the stack to
73c68f61 1650 calculate the CFA. */
e09bbb25 1651 rtx x = XEXP (dest, 0);
2ad9852d 1652
e09bbb25
JM
1653 if (GET_CODE (x) != REG)
1654 x = XEXP (x, 0);
1655 if (GET_CODE (x) != REG)
1656 abort ();
2ad9852d
RK
1657
1658 cfa.reg = REGNO (x);
e09bbb25
JM
1659 cfa.base_offset = offset;
1660 cfa.indirect = 1;
1661 def_cfa_1 (label, &cfa);
1662 break;
1663 }
1664 }
1665
7d9d8943 1666 def_cfa_1 (label, &cfa);
fbfa55b0 1667 queue_reg_save (label, src, offset);
2618f955
MM
1668 break;
1669
1670 default:
1671 abort ();
1672 }
b664de3a
AM
1673}
1674
3f76745e
JM
1675/* Record call frame debugging information for INSN, which either
1676 sets SP or FP (adjusting how we calculate the frame address) or saves a
1677 register to the stack. If INSN is NULL_RTX, initialize our state. */
71dfc51f 1678
3f76745e 1679void
7080f735 1680dwarf2out_frame_debug (rtx insn)
a3f97cbb 1681{
d3e3972c 1682 const char *label;
b664de3a 1683 rtx src;
3f76745e
JM
1684
1685 if (insn == NULL_RTX)
a3f97cbb 1686 {
fbfa55b0
RH
1687 /* Flush any queued register saves. */
1688 flush_queued_reg_saves ();
1689
3f76745e 1690 /* Set up state for generating call frame debug info. */
7d9d8943
AM
1691 lookup_cfa (&cfa);
1692 if (cfa.reg != (unsigned long) DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM))
3a88cbd1 1693 abort ();
2ad9852d 1694
7d9d8943
AM
1695 cfa.reg = STACK_POINTER_REGNUM;
1696 cfa_store = cfa;
770ca8c6
JO
1697 cfa_temp.reg = -1;
1698 cfa_temp.offset = 0;
3f76745e
JM
1699 return;
1700 }
1701
fbfa55b0
RH
1702 if (GET_CODE (insn) != INSN || clobbers_queued_reg_save (insn))
1703 flush_queued_reg_saves ();
1704
0021b564
JM
1705 if (! RTX_FRAME_RELATED_P (insn))
1706 {
fbfa55b0 1707 if (!ACCUMULATE_OUTGOING_ARGS)
c26fbbca 1708 dwarf2out_stack_adjust (insn);
2ad9852d 1709
0021b564
JM
1710 return;
1711 }
1712
3f76745e 1713 label = dwarf2out_cfi_label ();
07ebc930
RH
1714 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1715 if (src)
1716 insn = XEXP (src, 0);
556273e0 1717 else
07ebc930
RH
1718 insn = PATTERN (insn);
1719
b664de3a 1720 dwarf2out_frame_debug_expr (insn, label);
3f76745e
JM
1721}
1722
17211ab5
GK
1723#endif
1724
1725/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
7080f735
AJ
1726static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1727 (enum dwarf_call_frame_info cfi);
17211ab5
GK
1728
1729static enum dw_cfi_oprnd_type
7080f735 1730dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
17211ab5
GK
1731{
1732 switch (cfi)
1733 {
1734 case DW_CFA_nop:
1735 case DW_CFA_GNU_window_save:
1736 return dw_cfi_oprnd_unused;
1737
1738 case DW_CFA_set_loc:
1739 case DW_CFA_advance_loc1:
1740 case DW_CFA_advance_loc2:
1741 case DW_CFA_advance_loc4:
1742 case DW_CFA_MIPS_advance_loc8:
1743 return dw_cfi_oprnd_addr;
1744
1745 case DW_CFA_offset:
1746 case DW_CFA_offset_extended:
1747 case DW_CFA_def_cfa:
1748 case DW_CFA_offset_extended_sf:
1749 case DW_CFA_def_cfa_sf:
1750 case DW_CFA_restore_extended:
1751 case DW_CFA_undefined:
1752 case DW_CFA_same_value:
1753 case DW_CFA_def_cfa_register:
1754 case DW_CFA_register:
1755 return dw_cfi_oprnd_reg_num;
1756
1757 case DW_CFA_def_cfa_offset:
1758 case DW_CFA_GNU_args_size:
1759 case DW_CFA_def_cfa_offset_sf:
1760 return dw_cfi_oprnd_offset;
7080f735 1761
17211ab5
GK
1762 case DW_CFA_def_cfa_expression:
1763 case DW_CFA_expression:
1764 return dw_cfi_oprnd_loc;
1765
1766 default:
1767 abort ();
1768 }
1769}
1770
1771/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
7080f735
AJ
1772static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1773 (enum dwarf_call_frame_info cfi);
17211ab5
GK
1774
1775static enum dw_cfi_oprnd_type
7080f735 1776dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
17211ab5
GK
1777{
1778 switch (cfi)
1779 {
1780 case DW_CFA_def_cfa:
1781 case DW_CFA_def_cfa_sf:
1782 case DW_CFA_offset:
1783 case DW_CFA_offset_extended_sf:
1784 case DW_CFA_offset_extended:
1785 return dw_cfi_oprnd_offset;
1786
1787 case DW_CFA_register:
1788 return dw_cfi_oprnd_reg_num;
1789
1790 default:
1791 return dw_cfi_oprnd_unused;
1792 }
1793}
1794
1795#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1796
34c80057
AM
1797/* Map register numbers held in the call frame info that gcc has
1798 collected using DWARF_FRAME_REGNUM to those that should be output in
1799 .debug_frame and .eh_frame. */
1800#ifndef DWARF2_FRAME_REG_OUT
1801#define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
1802#endif
1803
3f76745e
JM
1804/* Output a Call Frame Information opcode and its operand(s). */
1805
1806static void
7080f735 1807output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3f76745e 1808{
34c80057 1809 unsigned long r;
3f76745e 1810 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2ad9852d
RK
1811 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1812 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
38f9cd4c 1813 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2ad9852d 1814 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3f76745e
JM
1815 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1816 {
34c80057
AM
1817 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1818 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1819 "DW_CFA_offset, column 0x%lx", r);
2e4b9b8c 1820 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3f76745e
JM
1821 }
1822 else if (cfi->dw_cfi_opc == DW_CFA_restore)
34c80057
AM
1823 {
1824 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1825 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1826 "DW_CFA_restore, column 0x%lx", r);
1827 }
3f76745e
JM
1828 else
1829 {
2e4b9b8c
RH
1830 dw2_asm_output_data (1, cfi->dw_cfi_opc,
1831 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3f76745e 1832
3f76745e
JM
1833 switch (cfi->dw_cfi_opc)
1834 {
1835 case DW_CFA_set_loc:
e1f9550a
RH
1836 if (for_eh)
1837 dw2_asm_output_encoded_addr_rtx (
1838 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1839 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1840 NULL);
1841 else
1842 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1843 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3f76745e 1844 break;
2ad9852d 1845
3f76745e 1846 case DW_CFA_advance_loc1:
2e4b9b8c
RH
1847 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1848 fde->dw_fde_current_label, NULL);
bb727b5a 1849 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3f76745e 1850 break;
2ad9852d 1851
3f76745e 1852 case DW_CFA_advance_loc2:
2e4b9b8c
RH
1853 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1854 fde->dw_fde_current_label, NULL);
3f76745e
JM
1855 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1856 break;
2ad9852d 1857
3f76745e 1858 case DW_CFA_advance_loc4:
2e4b9b8c
RH
1859 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1860 fde->dw_fde_current_label, NULL);
3f76745e
JM
1861 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1862 break;
2ad9852d 1863
3f76745e 1864 case DW_CFA_MIPS_advance_loc8:
2e4b9b8c
RH
1865 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1866 fde->dw_fde_current_label, NULL);
1867 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3f76745e 1868 break;
2ad9852d 1869
3f76745e
JM
1870 case DW_CFA_offset_extended:
1871 case DW_CFA_def_cfa:
34c80057
AM
1872 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1873 dw2_asm_output_data_uleb128 (r, NULL);
2e4b9b8c 1874 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3f76745e 1875 break;
2ad9852d 1876
6bb28965
JM
1877 case DW_CFA_offset_extended_sf:
1878 case DW_CFA_def_cfa_sf:
34c80057
AM
1879 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1880 dw2_asm_output_data_uleb128 (r, NULL);
6bb28965
JM
1881 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1882 break;
1883
3f76745e
JM
1884 case DW_CFA_restore_extended:
1885 case DW_CFA_undefined:
3f76745e
JM
1886 case DW_CFA_same_value:
1887 case DW_CFA_def_cfa_register:
34c80057
AM
1888 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1889 dw2_asm_output_data_uleb128 (r, NULL);
3f76745e 1890 break;
2ad9852d 1891
3f76745e 1892 case DW_CFA_register:
34c80057
AM
1893 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1894 dw2_asm_output_data_uleb128 (r, NULL);
1895 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
1896 dw2_asm_output_data_uleb128 (r, NULL);
3f76745e 1897 break;
2ad9852d 1898
3f76745e 1899 case DW_CFA_def_cfa_offset:
2e4b9b8c
RH
1900 case DW_CFA_GNU_args_size:
1901 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3f76745e 1902 break;
2ad9852d 1903
6bb28965
JM
1904 case DW_CFA_def_cfa_offset_sf:
1905 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1906 break;
1907
c53aa195
JM
1908 case DW_CFA_GNU_window_save:
1909 break;
2ad9852d 1910
7d9d8943 1911 case DW_CFA_def_cfa_expression:
6bb28965 1912 case DW_CFA_expression:
7d9d8943
AM
1913 output_cfa_loc (cfi);
1914 break;
2ad9852d 1915
6bb28965
JM
1916 case DW_CFA_GNU_negative_offset_extended:
1917 /* Obsoleted by DW_CFA_offset_extended_sf. */
1918 abort ();
1919
3f76745e
JM
1920 default:
1921 break;
1922 }
556273e0 1923 }
3f76745e
JM
1924}
1925
34c80057 1926/* Output the call frame information used to record information
3f76745e
JM
1927 that relates to calculating the frame pointer, and records the
1928 location of saved registers. */
1929
1930static void
7080f735 1931output_call_frame_info (int for_eh)
3f76745e 1932{
b3694847
SS
1933 unsigned int i;
1934 dw_fde_ref fde;
1935 dw_cfi_ref cfi;
27d95cbe 1936 char l1[20], l2[20], section_start_label[20];
ad5eeaa9 1937 bool any_lsda_needed = false;
52a11cbf 1938 char augmentation[6];
e1f9550a
RH
1939 int augmentation_size;
1940 int fde_encoding = DW_EH_PE_absptr;
1941 int per_encoding = DW_EH_PE_absptr;
1942 int lsda_encoding = DW_EH_PE_absptr;
3f76745e 1943
29b91443
JM
1944 /* Don't emit a CIE if there won't be any FDEs. */
1945 if (fde_table_in_use == 0)
1946 return;
1947
4746cf84
MA
1948 /* If we make FDEs linkonce, we may have to emit an empty label for
1949 an FDE that wouldn't otherwise be emitted. We want to avoid
1950 having an FDE kept around when the function it refers to is
1951 discarded. (Example where this matters: a primary function
1952 template in C++ requires EH information, but an explicit
1953 specialization doesn't. */
1954 if (TARGET_USES_WEAK_UNWIND_INFO
1955 && ! flag_asynchronous_unwind_tables
1956 && for_eh)
1957 for (i = 0; i < fde_table_in_use; i++)
1958 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
1959 && !fde_table[i].uses_eh_lsda
1960 && ! DECL_ONE_ONLY (fde_table[i].decl))
5fd9b178 1961 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
4746cf84
MA
1962 /* empty */ 1);
1963
ad5eeaa9
RH
1964 /* If we don't have any functions we'll want to unwind out of, don't
1965 emit any EH unwind information. Note that if exceptions aren't
1966 enabled, we won't have collected nothrow information, and if we
1967 asked for asynchronous tables, we always want this info. */
737faf14
JM
1968 if (for_eh)
1969 {
ad5eeaa9 1970 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2ad9852d
RK
1971
1972 for (i = 0; i < fde_table_in_use; i++)
52a11cbf 1973 if (fde_table[i].uses_eh_lsda)
ad5eeaa9 1974 any_eh_needed = any_lsda_needed = true;
4746cf84
MA
1975 else if (TARGET_USES_WEAK_UNWIND_INFO
1976 && DECL_ONE_ONLY (fde_table[i].decl))
1977 any_eh_needed = 1;
0366359a
GK
1978 else if (! fde_table[i].nothrow
1979 && ! fde_table[i].all_throwers_are_sibcalls)
ad5eeaa9 1980 any_eh_needed = true;
52a11cbf
RH
1981
1982 if (! any_eh_needed)
1983 return;
737faf14
JM
1984 }
1985
aa0c1401
JL
1986 /* We're going to be generating comments, so turn on app. */
1987 if (flag_debug_asm)
1988 app_enable ();
956d6950 1989
3f76745e 1990 if (for_eh)
5fd9b178 1991 targetm.asm_out.eh_frame_section ();
3f76745e 1992 else
715bdd29 1993 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
3f76745e 1994
27d95cbe
RH
1995 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
1996 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
1997
556273e0 1998 /* Output the CIE. */
a6ab3aad
JM
1999 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2000 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2e4b9b8c
RH
2001 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2002 "Length of Common Information Entry");
a6ab3aad
JM
2003 ASM_OUTPUT_LABEL (asm_out_file, l1);
2004
2e4b9b8c
RH
2005 /* Now that the CIE pointer is PC-relative for EH,
2006 use 0 to identify the CIE. */
2007 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2008 (for_eh ? 0 : DW_CIE_ID),
2009 "CIE Identifier Tag");
3f76745e 2010
2e4b9b8c 2011 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
3f76745e 2012
52a11cbf 2013 augmentation[0] = 0;
e1f9550a 2014 augmentation_size = 0;
52a11cbf 2015 if (for_eh)
a6ab3aad 2016 {
e1f9550a
RH
2017 char *p;
2018
52a11cbf
RH
2019 /* Augmentation:
2020 z Indicates that a uleb128 is present to size the
7080f735 2021 augmentation section.
e1f9550a
RH
2022 L Indicates the encoding (and thus presence) of
2023 an LSDA pointer in the FDE augmentation.
2024 R Indicates a non-default pointer encoding for
2025 FDE code pointers.
2026 P Indicates the presence of an encoding + language
2027 personality routine in the CIE augmentation. */
2028
4746cf84
MA
2029 fde_encoding = TARGET_USES_WEAK_UNWIND_INFO
2030 ? ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1)
2031 : ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
e1f9550a
RH
2032 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2033 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2034
2035 p = augmentation + 1;
2036 if (eh_personality_libfunc)
2037 {
2038 *p++ = 'P';
2039 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2040 }
52a11cbf 2041 if (any_lsda_needed)
e1f9550a
RH
2042 {
2043 *p++ = 'L';
2044 augmentation_size += 1;
2045 }
2046 if (fde_encoding != DW_EH_PE_absptr)
2047 {
2048 *p++ = 'R';
2049 augmentation_size += 1;
2050 }
2051 if (p > augmentation + 1)
2052 {
2053 augmentation[0] = 'z';
c26fbbca 2054 *p = '\0';
e1f9550a 2055 }
099c8b17
RH
2056
2057 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2058 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2059 {
2060 int offset = ( 4 /* Length */
2061 + 4 /* CIE Id */
2062 + 1 /* CIE version */
2063 + strlen (augmentation) + 1 /* Augmentation */
2064 + size_of_uleb128 (1) /* Code alignment */
2065 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2066 + 1 /* RA column */
2067 + 1 /* Augmentation size */
2068 + 1 /* Personality encoding */ );
2069 int pad = -offset & (PTR_SIZE - 1);
2070
2071 augmentation_size += pad;
2072
2073 /* Augmentations should be small, so there's scarce need to
2074 iterate for a solution. Die if we exceed one uleb128 byte. */
2075 if (size_of_uleb128 (augmentation_size) != 1)
2076 abort ();
2077 }
a6ab3aad 2078 }
3f76745e 2079
2ad9852d 2080 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2e4b9b8c 2081 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2e4b9b8c
RH
2082 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2083 "CIE Data Alignment Factor");
2e4b9b8c 2084 dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
3f76745e 2085
52a11cbf
RH
2086 if (augmentation[0])
2087 {
e1f9550a 2088 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
52a11cbf 2089 if (eh_personality_libfunc)
e1f9550a
RH
2090 {
2091 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2092 eh_data_format_name (per_encoding));
2093 dw2_asm_output_encoded_addr_rtx (per_encoding,
2094 eh_personality_libfunc, NULL);
2095 }
2ad9852d 2096
e1f9550a
RH
2097 if (any_lsda_needed)
2098 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2099 eh_data_format_name (lsda_encoding));
2ad9852d 2100
e1f9550a
RH
2101 if (fde_encoding != DW_EH_PE_absptr)
2102 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2103 eh_data_format_name (fde_encoding));
52a11cbf
RH
2104 }
2105
3f76745e 2106 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
12f0b96b 2107 output_cfi (cfi, NULL, for_eh);
3f76745e
JM
2108
2109 /* Pad the CIE out to an address sized boundary. */
c26fbbca 2110 ASM_OUTPUT_ALIGN (asm_out_file,
12f0b96b 2111 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
a6ab3aad 2112 ASM_OUTPUT_LABEL (asm_out_file, l2);
3f76745e
JM
2113
2114 /* Loop through all of the FDE's. */
2ad9852d 2115 for (i = 0; i < fde_table_in_use; i++)
3f76745e
JM
2116 {
2117 fde = &fde_table[i];
3f76745e 2118
52a11cbf 2119 /* Don't emit EH unwind info for leaf functions that don't need it. */
ad5eeaa9 2120 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
b6128b8c 2121 && (fde->nothrow || fde->all_throwers_are_sibcalls)
4746cf84 2122 && (! TARGET_USES_WEAK_UNWIND_INFO || ! DECL_ONE_ONLY (fde->decl))
b6128b8c 2123 && !fde->uses_eh_lsda)
737faf14
JM
2124 continue;
2125
5fd9b178
KH
2126 targetm.asm_out.unwind_label (asm_out_file, fde->decl, /* empty */ 0);
2127 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
556273e0
KH
2128 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2129 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2e4b9b8c
RH
2130 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2131 "FDE Length");
a6ab3aad
JM
2132 ASM_OUTPUT_LABEL (asm_out_file, l1);
2133
3f76745e 2134 if (for_eh)
27d95cbe 2135 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3f76745e 2136 else
27d95cbe 2137 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2e4b9b8c 2138 "FDE CIE offset");
3f76745e 2139
e1f9550a
RH
2140 if (for_eh)
2141 {
4746cf84
MA
2142 if (TARGET_USES_WEAK_UNWIND_INFO
2143 && DECL_ONE_ONLY (fde->decl))
2144 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2145 gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER
2146 (DECL_ASSEMBLER_NAME (fde->decl))),
2147 "FDE initial location");
2148 else
2149 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2150 gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin),
2151 "FDE initial location");
e1f9550a 2152 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
c26fbbca 2153 fde->dw_fde_end, fde->dw_fde_begin,
e1f9550a
RH
2154 "FDE address range");
2155 }
2156 else
2157 {
2158 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2159 "FDE initial location");
c26fbbca
KH
2160 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2161 fde->dw_fde_end, fde->dw_fde_begin,
e1f9550a
RH
2162 "FDE address range");
2163 }
3f76745e 2164
52a11cbf
RH
2165 if (augmentation[0])
2166 {
e1f9550a 2167 if (any_lsda_needed)
52a11cbf 2168 {
099c8b17
RH
2169 int size = size_of_encoded_value (lsda_encoding);
2170
2171 if (lsda_encoding == DW_EH_PE_aligned)
2172 {
2173 int offset = ( 4 /* Length */
2174 + 4 /* CIE offset */
2175 + 2 * size_of_encoded_value (fde_encoding)
2176 + 1 /* Augmentation size */ );
2177 int pad = -offset & (PTR_SIZE - 1);
2178
2179 size += pad;
2180 if (size_of_uleb128 (size) != 1)
2181 abort ();
2182 }
2183
2184 dw2_asm_output_data_uleb128 (size, "Augmentation size");
e1f9550a
RH
2185
2186 if (fde->uses_eh_lsda)
73c68f61
SS
2187 {
2188 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
e1f9550a 2189 fde->funcdef_number);
73c68f61 2190 dw2_asm_output_encoded_addr_rtx (
e1f9550a 2191 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
7080f735 2192 "Language Specific Data Area");
73c68f61 2193 }
e1f9550a 2194 else
099c8b17
RH
2195 {
2196 if (lsda_encoding == DW_EH_PE_aligned)
2197 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2ad9852d
RK
2198 dw2_asm_output_data
2199 (size_of_encoded_value (lsda_encoding), 0,
2200 "Language Specific Data Area (none)");
099c8b17 2201 }
52a11cbf
RH
2202 }
2203 else
e1f9550a 2204 dw2_asm_output_data_uleb128 (0, "Augmentation size");
52a11cbf
RH
2205 }
2206
3f76745e
JM
2207 /* Loop through the Call Frame Instructions associated with
2208 this FDE. */
2209 fde->dw_fde_current_label = fde->dw_fde_begin;
2210 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
12f0b96b 2211 output_cfi (cfi, fde, for_eh);
3f76745e 2212
a6ab3aad 2213 /* Pad the FDE out to an address sized boundary. */
c26fbbca 2214 ASM_OUTPUT_ALIGN (asm_out_file,
73c68f61 2215 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
a6ab3aad 2216 ASM_OUTPUT_LABEL (asm_out_file, l2);
3f76745e 2217 }
2e4b9b8c 2218
7606e68f 2219 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2e4b9b8c 2220 dw2_asm_output_data (4, 0, "End of Table");
a6ab3aad
JM
2221#ifdef MIPS_DEBUGGING_INFO
2222 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2223 get a value of 0. Putting .align 0 after the label fixes it. */
2224 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2225#endif
aa0c1401
JL
2226
2227 /* Turn off app to make assembly quicker. */
2228 if (flag_debug_asm)
2229 app_disable ();
a6ab3aad
JM
2230}
2231
3f76745e
JM
2232/* Output a marker (i.e. a label) for the beginning of a function, before
2233 the prologue. */
2234
2235void
7080f735
AJ
2236dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2237 const char *file ATTRIBUTE_UNUSED)
3f76745e
JM
2238{
2239 char label[MAX_ARTIFICIAL_LABEL_BYTES];
b3694847 2240 dw_fde_ref fde;
3f76745e 2241
2a1ee410
RH
2242 current_function_func_begin_label = 0;
2243
2244#ifdef IA64_UNWIND_INFO
2245 /* ??? current_function_func_begin_label is also used by except.c
2246 for call-site information. We must emit this label if it might
2247 be used. */
2248 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2249 && ! dwarf2out_do_frame ())
2250 return;
2251#else
2252 if (! dwarf2out_do_frame ())
2253 return;
2254#endif
2255
3f76745e
JM
2256 function_section (current_function_decl);
2257 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
df696a75 2258 current_function_funcdef_no);
2a1ee410 2259 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
df696a75 2260 current_function_funcdef_no);
00262c8a 2261 current_function_func_begin_label = get_identifier (label);
3f76745e 2262
2a1ee410
RH
2263#ifdef IA64_UNWIND_INFO
2264 /* We can elide the fde allocation if we're not emitting debug info. */
2265 if (! dwarf2out_do_frame ())
2266 return;
2267#endif
2268
3f76745e
JM
2269 /* Expand the fde table if necessary. */
2270 if (fde_table_in_use == fde_table_allocated)
2271 {
2272 fde_table_allocated += FDE_TABLE_INCREMENT;
17211ab5
GK
2273 fde_table = ggc_realloc (fde_table,
2274 fde_table_allocated * sizeof (dw_fde_node));
2275 memset (fde_table + fde_table_in_use, 0,
2276 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
a3f97cbb 2277 }
3f76745e
JM
2278
2279 /* Record the FDE associated with this function. */
2280 current_funcdef_fde = fde_table_in_use;
2281
2282 /* Add the new FDE at the end of the fde_table. */
2283 fde = &fde_table[fde_table_in_use++];
4746cf84 2284 fde->decl = current_function_decl;
3f76745e
JM
2285 fde->dw_fde_begin = xstrdup (label);
2286 fde->dw_fde_current_label = NULL;
2287 fde->dw_fde_end = NULL;
2288 fde->dw_fde_cfi = NULL;
df696a75 2289 fde->funcdef_number = current_function_funcdef_no;
fb13d4d0 2290 fde->nothrow = current_function_nothrow;
52a11cbf 2291 fde->uses_eh_lsda = cfun->uses_eh_lsda;
b6128b8c 2292 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
737faf14 2293
b57d9225 2294 args_size = old_args_size = 0;
653e276c 2295
2ad9852d
RK
2296 /* We only want to output line number information for the genuine dwarf2
2297 prologue case, not the eh frame case. */
653e276c
NB
2298#ifdef DWARF2_DEBUGGING_INFO
2299 if (file)
2300 dwarf2out_source_line (line, file);
2301#endif
3f76745e
JM
2302}
2303
2304/* Output a marker (i.e. a label) for the absolute end of the generated code
2305 for a function definition. This gets called *after* the epilogue code has
2306 been generated. */
2307
2308void
7080f735
AJ
2309dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2310 const char *file ATTRIBUTE_UNUSED)
3f76745e
JM
2311{
2312 dw_fde_ref fde;
2313 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2314
2315 /* Output a label to mark the endpoint of the code generated for this
3ef42a0c 2316 function. */
df696a75
RH
2317 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2318 current_function_funcdef_no);
3f76745e
JM
2319 ASM_OUTPUT_LABEL (asm_out_file, label);
2320 fde = &fde_table[fde_table_in_use - 1];
2321 fde->dw_fde_end = xstrdup (label);
3f76745e
JM
2322}
2323
2324void
7080f735 2325dwarf2out_frame_init (void)
3f76745e
JM
2326{
2327 /* Allocate the initial hunk of the fde_table. */
703ad42b 2328 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3f76745e
JM
2329 fde_table_allocated = FDE_TABLE_INCREMENT;
2330 fde_table_in_use = 0;
2331
2332 /* Generate the CFA instructions common to all FDE's. Do it now for the
2333 sake of lookup_cfa. */
2334
a6ab3aad 2335#ifdef DWARF2_UNWIND_INFO
91193900
AS
2336 /* On entry, the Canonical Frame Address is at SP. */
2337 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2338 initial_return_save (INCOMING_RETURN_ADDR_RTX);
3f76745e
JM
2339#endif
2340}
2341
2342void
7080f735 2343dwarf2out_frame_finish (void)
3f76745e 2344{
3f76745e 2345 /* Output call frame information. */
7a0c8d71 2346 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
3f76745e 2347 output_call_frame_info (0);
2ad9852d 2348
ddee9e8d 2349 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3f76745e 2350 output_call_frame_info (1);
556273e0 2351}
17211ab5 2352#endif
7d9d8943
AM
2353\f
2354/* And now, the subset of the debugging information support code necessary
2355 for emitting location expressions. */
3f76745e 2356
b9203463
RH
2357/* We need some way to distinguish DW_OP_addr with a direct symbol
2358 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2359#define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2360
2361
7d9d8943
AM
2362typedef struct dw_val_struct *dw_val_ref;
2363typedef struct die_struct *dw_die_ref;
2364typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
63e46568 2365typedef struct dw_loc_list_struct *dw_loc_list_ref;
3f76745e
JM
2366
2367/* Each DIE may have a series of attribute/value pairs. Values
2368 can take on several forms. The forms that are used in this
2369 implementation are listed below. */
2370
17211ab5 2371enum dw_val_class
3f76745e
JM
2372{
2373 dw_val_class_addr,
a20612aa 2374 dw_val_class_offset,
3f76745e 2375 dw_val_class_loc,
63e46568 2376 dw_val_class_loc_list,
2bee6045 2377 dw_val_class_range_list,
3f76745e
JM
2378 dw_val_class_const,
2379 dw_val_class_unsigned_const,
2380 dw_val_class_long_long,
e7ee3914 2381 dw_val_class_vec,
3f76745e
JM
2382 dw_val_class_flag,
2383 dw_val_class_die_ref,
2384 dw_val_class_fde_ref,
2385 dw_val_class_lbl_id,
8b790721 2386 dw_val_class_lbl_offset,
3f76745e 2387 dw_val_class_str
17211ab5 2388};
a3f97cbb 2389
3f76745e 2390/* Describe a double word constant value. */
21217bd0 2391/* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
3f76745e 2392
17211ab5 2393typedef struct dw_long_long_struct GTY(())
a3f97cbb 2394{
3f76745e
JM
2395 unsigned long hi;
2396 unsigned long low;
2397}
2398dw_long_long_const;
2399
e7ee3914 2400/* Describe a floating point constant value, or a vector constant value. */
3f76745e 2401
e7ee3914 2402typedef struct dw_vec_struct GTY(())
3f76745e 2403{
e7ee3914 2404 unsigned char * GTY((length ("%h.length"))) array;
3f76745e 2405 unsigned length;
e7ee3914 2406 unsigned elt_size;
3f76745e 2407}
e7ee3914 2408dw_vec_const;
3f76745e 2409
956d6950 2410/* The dw_val_node describes an attribute's value, as it is
3f76745e
JM
2411 represented internally. */
2412
17211ab5 2413typedef struct dw_val_struct GTY(())
3f76745e 2414{
17211ab5
GK
2415 enum dw_val_class val_class;
2416 union dw_val_struct_union
a3f97cbb 2417 {
17211ab5 2418 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
799f628a 2419 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
17211ab5
GK
2420 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2421 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
1431042e 2422 HOST_WIDE_INT GTY ((default)) val_int;
799f628a 2423 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
17211ab5 2424 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
e7ee3914 2425 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
17211ab5 2426 struct dw_val_die_union
2ad9852d
RK
2427 {
2428 dw_die_ref die;
2429 int external;
17211ab5
GK
2430 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2431 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2432 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2433 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2434 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
a3f97cbb 2435 }
17211ab5 2436 GTY ((desc ("%1.val_class"))) v;
3f76745e
JM
2437}
2438dw_val_node;
2439
2440/* Locations in memory are described using a sequence of stack machine
2441 operations. */
2442
17211ab5 2443typedef struct dw_loc_descr_struct GTY(())
3f76745e
JM
2444{
2445 dw_loc_descr_ref dw_loc_next;
2446 enum dwarf_location_atom dw_loc_opc;
2447 dw_val_node dw_loc_oprnd1;
2448 dw_val_node dw_loc_oprnd2;
d8041cc8 2449 int dw_loc_addr;
3f76745e
JM
2450}
2451dw_loc_descr_node;
2452
63e46568
DB
2453/* Location lists are ranges + location descriptions for that range,
2454 so you can track variables that are in different places over
30f7a378 2455 their entire life. */
17211ab5 2456typedef struct dw_loc_list_struct GTY(())
63e46568
DB
2457{
2458 dw_loc_list_ref dw_loc_next;
2459 const char *begin; /* Label for begin address of range */
2460 const char *end; /* Label for end address of range */
2ad9852d
RK
2461 char *ll_symbol; /* Label for beginning of location list.
2462 Only on head of list */
63e46568
DB
2463 const char *section; /* Section this loclist is relative to */
2464 dw_loc_descr_ref expr;
2465} dw_loc_list_node;
2466
17211ab5
GK
2467#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2468
7080f735
AJ
2469static const char *dwarf_stack_op_name (unsigned);
2470static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
799f628a 2471 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
7080f735
AJ
2472static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2473static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2474static unsigned long size_of_locs (dw_loc_descr_ref);
2475static void output_loc_operands (dw_loc_descr_ref);
2476static void output_loc_sequence (dw_loc_descr_ref);
3f76745e 2477
7d9d8943 2478/* Convert a DWARF stack opcode into its string name. */
3f76745e 2479
7d9d8943 2480static const char *
7080f735 2481dwarf_stack_op_name (unsigned int op)
ef76d03b 2482{
7d9d8943
AM
2483 switch (op)
2484 {
2485 case DW_OP_addr:
b9203463 2486 case INTERNAL_DW_OP_tls_addr:
7d9d8943
AM
2487 return "DW_OP_addr";
2488 case DW_OP_deref:
2489 return "DW_OP_deref";
2490 case DW_OP_const1u:
2491 return "DW_OP_const1u";
2492 case DW_OP_const1s:
2493 return "DW_OP_const1s";
2494 case DW_OP_const2u:
2495 return "DW_OP_const2u";
2496 case DW_OP_const2s:
2497 return "DW_OP_const2s";
2498 case DW_OP_const4u:
2499 return "DW_OP_const4u";
2500 case DW_OP_const4s:
2501 return "DW_OP_const4s";
2502 case DW_OP_const8u:
2503 return "DW_OP_const8u";
2504 case DW_OP_const8s:
2505 return "DW_OP_const8s";
2506 case DW_OP_constu:
2507 return "DW_OP_constu";
2508 case DW_OP_consts:
2509 return "DW_OP_consts";
2510 case DW_OP_dup:
2511 return "DW_OP_dup";
2512 case DW_OP_drop:
2513 return "DW_OP_drop";
2514 case DW_OP_over:
2515 return "DW_OP_over";
2516 case DW_OP_pick:
2517 return "DW_OP_pick";
2518 case DW_OP_swap:
2519 return "DW_OP_swap";
2520 case DW_OP_rot:
2521 return "DW_OP_rot";
2522 case DW_OP_xderef:
2523 return "DW_OP_xderef";
2524 case DW_OP_abs:
2525 return "DW_OP_abs";
2526 case DW_OP_and:
2527 return "DW_OP_and";
2528 case DW_OP_div:
2529 return "DW_OP_div";
2530 case DW_OP_minus:
2531 return "DW_OP_minus";
2532 case DW_OP_mod:
2533 return "DW_OP_mod";
2534 case DW_OP_mul:
2535 return "DW_OP_mul";
2536 case DW_OP_neg:
2537 return "DW_OP_neg";
2538 case DW_OP_not:
2539 return "DW_OP_not";
2540 case DW_OP_or:
2541 return "DW_OP_or";
2542 case DW_OP_plus:
2543 return "DW_OP_plus";
2544 case DW_OP_plus_uconst:
2545 return "DW_OP_plus_uconst";
2546 case DW_OP_shl:
2547 return "DW_OP_shl";
2548 case DW_OP_shr:
2549 return "DW_OP_shr";
2550 case DW_OP_shra:
2551 return "DW_OP_shra";
2552 case DW_OP_xor:
2553 return "DW_OP_xor";
2554 case DW_OP_bra:
2555 return "DW_OP_bra";
2556 case DW_OP_eq:
2557 return "DW_OP_eq";
2558 case DW_OP_ge:
2559 return "DW_OP_ge";
2560 case DW_OP_gt:
2561 return "DW_OP_gt";
2562 case DW_OP_le:
2563 return "DW_OP_le";
2564 case DW_OP_lt:
2565 return "DW_OP_lt";
2566 case DW_OP_ne:
2567 return "DW_OP_ne";
2568 case DW_OP_skip:
2569 return "DW_OP_skip";
2570 case DW_OP_lit0:
2571 return "DW_OP_lit0";
2572 case DW_OP_lit1:
2573 return "DW_OP_lit1";
2574 case DW_OP_lit2:
2575 return "DW_OP_lit2";
2576 case DW_OP_lit3:
2577 return "DW_OP_lit3";
2578 case DW_OP_lit4:
2579 return "DW_OP_lit4";
2580 case DW_OP_lit5:
2581 return "DW_OP_lit5";
2582 case DW_OP_lit6:
2583 return "DW_OP_lit6";
2584 case DW_OP_lit7:
2585 return "DW_OP_lit7";
2586 case DW_OP_lit8:
2587 return "DW_OP_lit8";
2588 case DW_OP_lit9:
2589 return "DW_OP_lit9";
2590 case DW_OP_lit10:
2591 return "DW_OP_lit10";
2592 case DW_OP_lit11:
2593 return "DW_OP_lit11";
2594 case DW_OP_lit12:
2595 return "DW_OP_lit12";
2596 case DW_OP_lit13:
2597 return "DW_OP_lit13";
2598 case DW_OP_lit14:
2599 return "DW_OP_lit14";
2600 case DW_OP_lit15:
2601 return "DW_OP_lit15";
2602 case DW_OP_lit16:
2603 return "DW_OP_lit16";
2604 case DW_OP_lit17:
2605 return "DW_OP_lit17";
2606 case DW_OP_lit18:
2607 return "DW_OP_lit18";
2608 case DW_OP_lit19:
2609 return "DW_OP_lit19";
2610 case DW_OP_lit20:
2611 return "DW_OP_lit20";
2612 case DW_OP_lit21:
2613 return "DW_OP_lit21";
2614 case DW_OP_lit22:
2615 return "DW_OP_lit22";
2616 case DW_OP_lit23:
2617 return "DW_OP_lit23";
2618 case DW_OP_lit24:
2619 return "DW_OP_lit24";
2620 case DW_OP_lit25:
2621 return "DW_OP_lit25";
2622 case DW_OP_lit26:
2623 return "DW_OP_lit26";
2624 case DW_OP_lit27:
2625 return "DW_OP_lit27";
2626 case DW_OP_lit28:
2627 return "DW_OP_lit28";
2628 case DW_OP_lit29:
2629 return "DW_OP_lit29";
2630 case DW_OP_lit30:
2631 return "DW_OP_lit30";
2632 case DW_OP_lit31:
2633 return "DW_OP_lit31";
2634 case DW_OP_reg0:
2635 return "DW_OP_reg0";
2636 case DW_OP_reg1:
2637 return "DW_OP_reg1";
2638 case DW_OP_reg2:
2639 return "DW_OP_reg2";
2640 case DW_OP_reg3:
2641 return "DW_OP_reg3";
2642 case DW_OP_reg4:
2643 return "DW_OP_reg4";
2644 case DW_OP_reg5:
2645 return "DW_OP_reg5";
2646 case DW_OP_reg6:
2647 return "DW_OP_reg6";
2648 case DW_OP_reg7:
2649 return "DW_OP_reg7";
2650 case DW_OP_reg8:
2651 return "DW_OP_reg8";
2652 case DW_OP_reg9:
2653 return "DW_OP_reg9";
2654 case DW_OP_reg10:
2655 return "DW_OP_reg10";
2656 case DW_OP_reg11:
2657 return "DW_OP_reg11";
2658 case DW_OP_reg12:
2659 return "DW_OP_reg12";
2660 case DW_OP_reg13:
2661 return "DW_OP_reg13";
2662 case DW_OP_reg14:
2663 return "DW_OP_reg14";
2664 case DW_OP_reg15:
2665 return "DW_OP_reg15";
2666 case DW_OP_reg16:
2667 return "DW_OP_reg16";
2668 case DW_OP_reg17:
2669 return "DW_OP_reg17";
2670 case DW_OP_reg18:
2671 return "DW_OP_reg18";
2672 case DW_OP_reg19:
2673 return "DW_OP_reg19";
2674 case DW_OP_reg20:
2675 return "DW_OP_reg20";
2676 case DW_OP_reg21:
2677 return "DW_OP_reg21";
2678 case DW_OP_reg22:
2679 return "DW_OP_reg22";
2680 case DW_OP_reg23:
2681 return "DW_OP_reg23";
2682 case DW_OP_reg24:
2683 return "DW_OP_reg24";
2684 case DW_OP_reg25:
2685 return "DW_OP_reg25";
2686 case DW_OP_reg26:
2687 return "DW_OP_reg26";
2688 case DW_OP_reg27:
2689 return "DW_OP_reg27";
2690 case DW_OP_reg28:
2691 return "DW_OP_reg28";
2692 case DW_OP_reg29:
2693 return "DW_OP_reg29";
2694 case DW_OP_reg30:
2695 return "DW_OP_reg30";
2696 case DW_OP_reg31:
2697 return "DW_OP_reg31";
2698 case DW_OP_breg0:
2699 return "DW_OP_breg0";
2700 case DW_OP_breg1:
2701 return "DW_OP_breg1";
2702 case DW_OP_breg2:
2703 return "DW_OP_breg2";
2704 case DW_OP_breg3:
2705 return "DW_OP_breg3";
2706 case DW_OP_breg4:
2707 return "DW_OP_breg4";
2708 case DW_OP_breg5:
2709 return "DW_OP_breg5";
2710 case DW_OP_breg6:
2711 return "DW_OP_breg6";
2712 case DW_OP_breg7:
2713 return "DW_OP_breg7";
2714 case DW_OP_breg8:
2715 return "DW_OP_breg8";
2716 case DW_OP_breg9:
2717 return "DW_OP_breg9";
2718 case DW_OP_breg10:
2719 return "DW_OP_breg10";
2720 case DW_OP_breg11:
2721 return "DW_OP_breg11";
2722 case DW_OP_breg12:
2723 return "DW_OP_breg12";
2724 case DW_OP_breg13:
2725 return "DW_OP_breg13";
2726 case DW_OP_breg14:
2727 return "DW_OP_breg14";
2728 case DW_OP_breg15:
2729 return "DW_OP_breg15";
2730 case DW_OP_breg16:
2731 return "DW_OP_breg16";
2732 case DW_OP_breg17:
2733 return "DW_OP_breg17";
2734 case DW_OP_breg18:
2735 return "DW_OP_breg18";
2736 case DW_OP_breg19:
2737 return "DW_OP_breg19";
2738 case DW_OP_breg20:
2739 return "DW_OP_breg20";
2740 case DW_OP_breg21:
2741 return "DW_OP_breg21";
2742 case DW_OP_breg22:
2743 return "DW_OP_breg22";
2744 case DW_OP_breg23:
2745 return "DW_OP_breg23";
2746 case DW_OP_breg24:
2747 return "DW_OP_breg24";
2748 case DW_OP_breg25:
2749 return "DW_OP_breg25";
2750 case DW_OP_breg26:
2751 return "DW_OP_breg26";
2752 case DW_OP_breg27:
2753 return "DW_OP_breg27";
2754 case DW_OP_breg28:
2755 return "DW_OP_breg28";
2756 case DW_OP_breg29:
2757 return "DW_OP_breg29";
2758 case DW_OP_breg30:
2759 return "DW_OP_breg30";
2760 case DW_OP_breg31:
2761 return "DW_OP_breg31";
2762 case DW_OP_regx:
2763 return "DW_OP_regx";
2764 case DW_OP_fbreg:
2765 return "DW_OP_fbreg";
2766 case DW_OP_bregx:
2767 return "DW_OP_bregx";
2768 case DW_OP_piece:
2769 return "DW_OP_piece";
2770 case DW_OP_deref_size:
2771 return "DW_OP_deref_size";
2772 case DW_OP_xderef_size:
2773 return "DW_OP_xderef_size";
2774 case DW_OP_nop:
2775 return "DW_OP_nop";
b9203463
RH
2776 case DW_OP_push_object_address:
2777 return "DW_OP_push_object_address";
2778 case DW_OP_call2:
2779 return "DW_OP_call2";
2780 case DW_OP_call4:
2781 return "DW_OP_call4";
2782 case DW_OP_call_ref:
2783 return "DW_OP_call_ref";
2784 case DW_OP_GNU_push_tls_address:
2785 return "DW_OP_GNU_push_tls_address";
3f76745e 2786 default:
7d9d8943 2787 return "OP_<unknown>";
3f76745e 2788 }
bdb669cb 2789}
a3f97cbb 2790
7d9d8943
AM
2791/* Return a pointer to a newly allocated location description. Location
2792 descriptions are simple expression terms that can be strung
2793 together to form more complicated location (address) descriptions. */
2794
2795static inline dw_loc_descr_ref
799f628a
JH
2796new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2797 unsigned HOST_WIDE_INT oprnd2)
4b674448 2798{
703ad42b 2799 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
71dfc51f 2800
7d9d8943
AM
2801 descr->dw_loc_opc = op;
2802 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2803 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2804 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2805 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
71dfc51f 2806
7d9d8943
AM
2807 return descr;
2808}
2809
63e46568 2810
7d9d8943
AM
2811/* Add a location description term to a location description expression. */
2812
2813static inline void
7080f735 2814add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
7d9d8943 2815{
b3694847 2816 dw_loc_descr_ref *d;
7d9d8943
AM
2817
2818 /* Find the end of the chain. */
2819 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2820 ;
2821
2822 *d = descr;
2823}
2824
2825/* Return the size of a location descriptor. */
2826
2827static unsigned long
7080f735 2828size_of_loc_descr (dw_loc_descr_ref loc)
7d9d8943 2829{
b3694847 2830 unsigned long size = 1;
7d9d8943
AM
2831
2832 switch (loc->dw_loc_opc)
2833 {
2834 case DW_OP_addr:
b9203463 2835 case INTERNAL_DW_OP_tls_addr:
7d9d8943
AM
2836 size += DWARF2_ADDR_SIZE;
2837 break;
2838 case DW_OP_const1u:
2839 case DW_OP_const1s:
2840 size += 1;
2841 break;
2842 case DW_OP_const2u:
2843 case DW_OP_const2s:
2844 size += 2;
2845 break;
2846 case DW_OP_const4u:
2847 case DW_OP_const4s:
2848 size += 4;
2849 break;
2850 case DW_OP_const8u:
2851 case DW_OP_const8s:
2852 size += 8;
2853 break;
2854 case DW_OP_constu:
2855 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2856 break;
2857 case DW_OP_consts:
2858 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2859 break;
2860 case DW_OP_pick:
2861 size += 1;
2862 break;
2863 case DW_OP_plus_uconst:
2864 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2865 break;
2866 case DW_OP_skip:
2867 case DW_OP_bra:
2868 size += 2;
2869 break;
2870 case DW_OP_breg0:
2871 case DW_OP_breg1:
2872 case DW_OP_breg2:
2873 case DW_OP_breg3:
2874 case DW_OP_breg4:
2875 case DW_OP_breg5:
2876 case DW_OP_breg6:
2877 case DW_OP_breg7:
2878 case DW_OP_breg8:
2879 case DW_OP_breg9:
2880 case DW_OP_breg10:
2881 case DW_OP_breg11:
2882 case DW_OP_breg12:
2883 case DW_OP_breg13:
2884 case DW_OP_breg14:
2885 case DW_OP_breg15:
2886 case DW_OP_breg16:
2887 case DW_OP_breg17:
2888 case DW_OP_breg18:
2889 case DW_OP_breg19:
2890 case DW_OP_breg20:
2891 case DW_OP_breg21:
2892 case DW_OP_breg22:
2893 case DW_OP_breg23:
2894 case DW_OP_breg24:
2895 case DW_OP_breg25:
2896 case DW_OP_breg26:
2897 case DW_OP_breg27:
2898 case DW_OP_breg28:
2899 case DW_OP_breg29:
2900 case DW_OP_breg30:
2901 case DW_OP_breg31:
2902 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2903 break;
2904 case DW_OP_regx:
2905 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2906 break;
2907 case DW_OP_fbreg:
2908 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2909 break;
2910 case DW_OP_bregx:
2911 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2912 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2913 break;
2914 case DW_OP_piece:
2915 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2916 break;
2917 case DW_OP_deref_size:
2918 case DW_OP_xderef_size:
2919 size += 1;
2920 break;
b9203463
RH
2921 case DW_OP_call2:
2922 size += 2;
2923 break;
2924 case DW_OP_call4:
2925 size += 4;
2926 break;
2927 case DW_OP_call_ref:
2928 size += DWARF2_ADDR_SIZE;
2929 break;
3f76745e 2930 default:
7d9d8943 2931 break;
4b674448 2932 }
7d9d8943
AM
2933
2934 return size;
4b674448
JM
2935}
2936
7d9d8943 2937/* Return the size of a series of location descriptors. */
71dfc51f 2938
7d9d8943 2939static unsigned long
7080f735 2940size_of_locs (dw_loc_descr_ref loc)
4b674448 2941{
2ad9852d 2942 unsigned long size;
7d9d8943 2943
2ad9852d 2944 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
d8041cc8
RH
2945 {
2946 loc->dw_loc_addr = size;
2947 size += size_of_loc_descr (loc);
2948 }
7d9d8943
AM
2949
2950 return size;
4b674448
JM
2951}
2952
7d9d8943 2953/* Output location description stack opcode's operands (if any). */
71dfc51f 2954
7d9d8943 2955static void
7080f735 2956output_loc_operands (dw_loc_descr_ref loc)
a3f97cbb 2957{
b3694847
SS
2958 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2959 dw_val_ref val2 = &loc->dw_loc_oprnd2;
7d9d8943
AM
2960
2961 switch (loc->dw_loc_opc)
a3f97cbb 2962 {
0517872a 2963#ifdef DWARF2_DEBUGGING_INFO
3f76745e 2964 case DW_OP_addr:
2e4b9b8c 2965 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
7d9d8943 2966 break;
3f76745e 2967 case DW_OP_const2u:
3f76745e 2968 case DW_OP_const2s:
2e4b9b8c 2969 dw2_asm_output_data (2, val1->v.val_int, NULL);
7d9d8943 2970 break;
3f76745e 2971 case DW_OP_const4u:
3f76745e 2972 case DW_OP_const4s:
2e4b9b8c 2973 dw2_asm_output_data (4, val1->v.val_int, NULL);
7d9d8943 2974 break;
3f76745e 2975 case DW_OP_const8u:
3f76745e 2976 case DW_OP_const8s:
2e4b9b8c
RH
2977 if (HOST_BITS_PER_LONG < 64)
2978 abort ();
2979 dw2_asm_output_data (8, val1->v.val_int, NULL);
7d9d8943 2980 break;
0517872a
JM
2981 case DW_OP_skip:
2982 case DW_OP_bra:
d8041cc8
RH
2983 {
2984 int offset;
2985
2986 if (val1->val_class == dw_val_class_loc)
2987 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2988 else
2989 abort ();
2990
2e4b9b8c 2991 dw2_asm_output_data (2, offset, NULL);
d8041cc8 2992 }
0517872a 2993 break;
3139472f
JM
2994#else
2995 case DW_OP_addr:
2996 case DW_OP_const2u:
2997 case DW_OP_const2s:
2998 case DW_OP_const4u:
2999 case DW_OP_const4s:
3000 case DW_OP_const8u:
3001 case DW_OP_const8s:
3002 case DW_OP_skip:
3003 case DW_OP_bra:
3004 /* We currently don't make any attempt to make sure these are
73c68f61
SS
3005 aligned properly like we do for the main unwind info, so
3006 don't support emitting things larger than a byte if we're
3007 only doing unwinding. */
3139472f 3008 abort ();
0517872a
JM
3009#endif
3010 case DW_OP_const1u:
3011 case DW_OP_const1s:
2e4b9b8c 3012 dw2_asm_output_data (1, val1->v.val_int, NULL);
0517872a 3013 break;
3f76745e 3014 case DW_OP_constu:
2e4b9b8c 3015 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
7d9d8943 3016 break;
3f76745e 3017 case DW_OP_consts:
2e4b9b8c 3018 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
7d9d8943
AM
3019 break;
3020 case DW_OP_pick:
2e4b9b8c 3021 dw2_asm_output_data (1, val1->v.val_int, NULL);
7d9d8943
AM
3022 break;
3023 case DW_OP_plus_uconst:
2e4b9b8c 3024 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
7d9d8943 3025 break;
3f76745e 3026 case DW_OP_breg0:
3f76745e 3027 case DW_OP_breg1:
3f76745e 3028 case DW_OP_breg2:
3f76745e 3029 case DW_OP_breg3:
3f76745e 3030 case DW_OP_breg4:
3f76745e 3031 case DW_OP_breg5:
3f76745e 3032 case DW_OP_breg6:
3f76745e 3033 case DW_OP_breg7:
3f76745e 3034 case DW_OP_breg8:
3f76745e 3035 case DW_OP_breg9:
3f76745e 3036 case DW_OP_breg10:
3f76745e 3037 case DW_OP_breg11:
3f76745e 3038 case DW_OP_breg12:
3f76745e 3039 case DW_OP_breg13:
3f76745e 3040 case DW_OP_breg14:
3f76745e 3041 case DW_OP_breg15:
3f76745e 3042 case DW_OP_breg16:
3f76745e 3043 case DW_OP_breg17:
3f76745e 3044 case DW_OP_breg18:
3f76745e 3045 case DW_OP_breg19:
3f76745e 3046 case DW_OP_breg20:
3f76745e 3047 case DW_OP_breg21:
3f76745e 3048 case DW_OP_breg22:
3f76745e 3049 case DW_OP_breg23:
3f76745e 3050 case DW_OP_breg24:
3f76745e 3051 case DW_OP_breg25:
3f76745e 3052 case DW_OP_breg26:
3f76745e 3053 case DW_OP_breg27:
3f76745e 3054 case DW_OP_breg28:
3f76745e 3055 case DW_OP_breg29:
3f76745e 3056 case DW_OP_breg30:
3f76745e 3057 case DW_OP_breg31:
2e4b9b8c 3058 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
7d9d8943 3059 break;
3f76745e 3060 case DW_OP_regx:
2e4b9b8c 3061 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
7d9d8943 3062 break;
3f76745e 3063 case DW_OP_fbreg:
2e4b9b8c 3064 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
7d9d8943 3065 break;
3f76745e 3066 case DW_OP_bregx:
2e4b9b8c
RH
3067 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3068 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
7d9d8943 3069 break;
3f76745e 3070 case DW_OP_piece:
2e4b9b8c 3071 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
7d9d8943 3072 break;
3f76745e 3073 case DW_OP_deref_size:
3f76745e 3074 case DW_OP_xderef_size:
2e4b9b8c 3075 dw2_asm_output_data (1, val1->v.val_int, NULL);
7d9d8943 3076 break;
b9203463
RH
3077
3078 case INTERNAL_DW_OP_tls_addr:
3079#ifdef ASM_OUTPUT_DWARF_DTPREL
3080 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3081 val1->v.val_addr);
3082 fputc ('\n', asm_out_file);
3083#else
3084 abort ();
3085#endif
3086 break;
3087
7d9d8943 3088 default:
3139472f
JM
3089 /* Other codes have no operands. */
3090 break;
7d9d8943
AM
3091 }
3092}
3093
3094/* Output a sequence of location operations. */
3095
3096static void
7080f735 3097output_loc_sequence (dw_loc_descr_ref loc)
7d9d8943
AM
3098{
3099 for (; loc != NULL; loc = loc->dw_loc_next)
3100 {
3101 /* Output the opcode. */
2e4b9b8c
RH
3102 dw2_asm_output_data (1, loc->dw_loc_opc,
3103 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
7d9d8943
AM
3104
3105 /* Output the operand(s) (if any). */
3106 output_loc_operands (loc);
3107 }
3108}
3109
3110/* This routine will generate the correct assembly data for a location
3111 description based on a cfi entry with a complex address. */
3112
3113static void
7080f735 3114output_cfa_loc (dw_cfi_ref cfi)
7d9d8943
AM
3115{
3116 dw_loc_descr_ref loc;
3117 unsigned long size;
3118
3119 /* Output the size of the block. */
3120 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3121 size = size_of_locs (loc);
2e4b9b8c 3122 dw2_asm_output_data_uleb128 (size, NULL);
7d9d8943
AM
3123
3124 /* Now output the operations themselves. */
3125 output_loc_sequence (loc);
3126}
3127
dd49a9ec 3128/* This function builds a dwarf location descriptor sequence from
556273e0 3129 a dw_cfa_location. */
7d9d8943
AM
3130
3131static struct dw_loc_descr_struct *
7080f735 3132build_cfa_loc (dw_cfa_location *cfa)
7d9d8943
AM
3133{
3134 struct dw_loc_descr_struct *head, *tmp;
3135
3136 if (cfa->indirect == 0)
3137 abort ();
3138
3139 if (cfa->base_offset)
f299afab
HPN
3140 {
3141 if (cfa->reg <= 31)
3142 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3143 else
3144 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3145 }
3146 else if (cfa->reg <= 31)
7d9d8943 3147 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
f299afab
HPN
3148 else
3149 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
2ad9852d 3150
7d9d8943
AM
3151 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3152 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3153 add_loc_descr (&head, tmp);
3154 if (cfa->offset != 0)
3155 {
3156 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3157 add_loc_descr (&head, tmp);
3158 }
2ad9852d 3159
7d9d8943
AM
3160 return head;
3161}
3162
2ad9852d
RK
3163/* This function fills in aa dw_cfa_location structure from a dwarf location
3164 descriptor sequence. */
7d9d8943
AM
3165
3166static void
7080f735 3167get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
7d9d8943 3168{
556273e0 3169 struct dw_loc_descr_struct *ptr;
7d9d8943
AM
3170 cfa->offset = 0;
3171 cfa->base_offset = 0;
3172 cfa->indirect = 0;
3173 cfa->reg = -1;
3174
3175 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3176 {
3177 enum dwarf_location_atom op = ptr->dw_loc_opc;
2ad9852d 3178
7d9d8943 3179 switch (op)
556273e0 3180 {
7d9d8943
AM
3181 case DW_OP_reg0:
3182 case DW_OP_reg1:
3183 case DW_OP_reg2:
3184 case DW_OP_reg3:
3185 case DW_OP_reg4:
3186 case DW_OP_reg5:
3187 case DW_OP_reg6:
3188 case DW_OP_reg7:
3189 case DW_OP_reg8:
3190 case DW_OP_reg9:
3191 case DW_OP_reg10:
3192 case DW_OP_reg11:
3193 case DW_OP_reg12:
3194 case DW_OP_reg13:
3195 case DW_OP_reg14:
3196 case DW_OP_reg15:
3197 case DW_OP_reg16:
3198 case DW_OP_reg17:
3199 case DW_OP_reg18:
3200 case DW_OP_reg19:
3201 case DW_OP_reg20:
3202 case DW_OP_reg21:
3203 case DW_OP_reg22:
3204 case DW_OP_reg23:
3205 case DW_OP_reg24:
3206 case DW_OP_reg25:
3207 case DW_OP_reg26:
3208 case DW_OP_reg27:
3209 case DW_OP_reg28:
3210 case DW_OP_reg29:
3211 case DW_OP_reg30:
3212 case DW_OP_reg31:
3213 cfa->reg = op - DW_OP_reg0;
3214 break;
3215 case DW_OP_regx:
3216 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3217 break;
3218 case DW_OP_breg0:
3219 case DW_OP_breg1:
3220 case DW_OP_breg2:
3221 case DW_OP_breg3:
3222 case DW_OP_breg4:
3223 case DW_OP_breg5:
3224 case DW_OP_breg6:
3225 case DW_OP_breg7:
3226 case DW_OP_breg8:
3227 case DW_OP_breg9:
3228 case DW_OP_breg10:
3229 case DW_OP_breg11:
3230 case DW_OP_breg12:
3231 case DW_OP_breg13:
3232 case DW_OP_breg14:
3233 case DW_OP_breg15:
3234 case DW_OP_breg16:
3235 case DW_OP_breg17:
3236 case DW_OP_breg18:
3237 case DW_OP_breg19:
3238 case DW_OP_breg20:
3239 case DW_OP_breg21:
3240 case DW_OP_breg22:
3241 case DW_OP_breg23:
3242 case DW_OP_breg24:
3243 case DW_OP_breg25:
3244 case DW_OP_breg26:
3245 case DW_OP_breg27:
3246 case DW_OP_breg28:
3247 case DW_OP_breg29:
3248 case DW_OP_breg30:
3249 case DW_OP_breg31:
3250 cfa->reg = op - DW_OP_breg0;
3251 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3252 break;
3253 case DW_OP_bregx:
3254 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3255 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3256 break;
3257 case DW_OP_deref:
3258 cfa->indirect = 1;
3259 break;
3260 case DW_OP_plus_uconst:
556273e0 3261 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
7d9d8943
AM
3262 break;
3263 default:
a1f300c0 3264 internal_error ("DW_LOC_OP %s not implemented\n",
400500c4 3265 dwarf_stack_op_name (ptr->dw_loc_opc));
7d9d8943
AM
3266 }
3267 }
3268}
3269#endif /* .debug_frame support */
3270\f
3271/* And now, the support for symbolic debugging information. */
3272#ifdef DWARF2_DEBUGGING_INFO
3273
117f9d28 3274/* .debug_str support. */
7080f735
AJ
3275static int output_indirect_string (void **, void *);
3276
3277static void dwarf2out_init (const char *);
3278static void dwarf2out_finish (const char *);
3279static void dwarf2out_define (unsigned int, const char *);
3280static void dwarf2out_undef (unsigned int, const char *);
3281static void dwarf2out_start_source_file (unsigned, const char *);
3282static void dwarf2out_end_source_file (unsigned);
3283static void dwarf2out_begin_block (unsigned, unsigned);
3284static void dwarf2out_end_block (unsigned, unsigned);
3285static bool dwarf2out_ignore_block (tree);
3286static void dwarf2out_global_decl (tree);
21d13d83 3287static void dwarf2out_type_decl (tree, int);
6097b0c3 3288static void dwarf2out_imported_module_or_decl (tree, tree);
7080f735 3289static void dwarf2out_abstract_function (tree);
0a2d3d69
DB
3290static void dwarf2out_var_location (rtx);
3291static void dwarf2out_begin_function (tree);
7f905405
NB
3292
3293/* The debug hooks structure. */
3294
54b6670a 3295const struct gcc_debug_hooks dwarf2_debug_hooks =
7f905405
NB
3296{
3297 dwarf2out_init,
3298 dwarf2out_finish,
3299 dwarf2out_define,
3300 dwarf2out_undef,
3301 dwarf2out_start_source_file,
a5a42b92
NB
3302 dwarf2out_end_source_file,
3303 dwarf2out_begin_block,
e2a12aca 3304 dwarf2out_end_block,
e1772ac0 3305 dwarf2out_ignore_block,
e2a12aca 3306 dwarf2out_source_line,
653e276c 3307 dwarf2out_begin_prologue,
702ada3d 3308 debug_nothing_int_charstar, /* end_prologue */
e2a12aca 3309 dwarf2out_end_epilogue,
0a2d3d69 3310 dwarf2out_begin_function,
2b85879e
NB
3311 debug_nothing_int, /* end_function */
3312 dwarf2out_decl, /* function_decl */
3313 dwarf2out_global_decl,
21d13d83 3314 dwarf2out_type_decl, /* type_decl */
6097b0c3 3315 dwarf2out_imported_module_or_decl,
e1772ac0
NB
3316 debug_nothing_tree, /* deferred_inline_function */
3317 /* The DWARF 2 backend tries to reduce debugging bloat by not
3318 emitting the abstract description of inline functions until
3319 something tries to reference them. */
3320 dwarf2out_abstract_function, /* outlining_inline_function */
33b49800 3321 debug_nothing_rtx, /* label */
014a1138 3322 debug_nothing_int, /* handle_pch */
0a2d3d69 3323 dwarf2out_var_location
7f905405 3324};
17211ab5 3325#endif
7f905405 3326\f
7d9d8943
AM
3327/* NOTE: In the comments in this file, many references are made to
3328 "Debugging Information Entries". This term is abbreviated as `DIE'
3329 throughout the remainder of this file. */
3330
3331/* An internal representation of the DWARF output is built, and then
3332 walked to generate the DWARF debugging info. The walk of the internal
3333 representation is done after the entire program has been compiled.
3334 The types below are used to describe the internal representation. */
3335
3336/* Various DIE's use offsets relative to the beginning of the
3337 .debug_info section to refer to each other. */
3338
3339typedef long int dw_offset;
3340
3341/* Define typedefs here to avoid circular dependencies. */
3342
3343typedef struct dw_attr_struct *dw_attr_ref;
3344typedef struct dw_line_info_struct *dw_line_info_ref;
3345typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3346typedef struct pubname_struct *pubname_ref;
a20612aa 3347typedef struct dw_ranges_struct *dw_ranges_ref;
7d9d8943
AM
3348
3349/* Each entry in the line_info_table maintains the file and
3350 line number associated with the label generated for that
3351 entry. The label gives the PC value associated with
3352 the line number entry. */
3353
17211ab5 3354typedef struct dw_line_info_struct GTY(())
7d9d8943
AM
3355{
3356 unsigned long dw_file_num;
3357 unsigned long dw_line_num;
3358}
3359dw_line_info_entry;
3360
3361/* Line information for functions in separate sections; each one gets its
3362 own sequence. */
17211ab5 3363typedef struct dw_separate_line_info_struct GTY(())
7d9d8943
AM
3364{
3365 unsigned long dw_file_num;
3366 unsigned long dw_line_num;
3367 unsigned long function;
3368}
3369dw_separate_line_info_entry;
3370
3371/* Each DIE attribute has a field specifying the attribute kind,
3372 a link to the next attribute in the chain, and an attribute value.
3373 Attributes are typically linked below the DIE they modify. */
3374
17211ab5 3375typedef struct dw_attr_struct GTY(())
7d9d8943
AM
3376{
3377 enum dwarf_attribute dw_attr;
3378 dw_attr_ref dw_attr_next;
3379 dw_val_node dw_attr_val;
3380}
3381dw_attr_node;
3382
3383/* The Debugging Information Entry (DIE) structure */
3384
17211ab5 3385typedef struct die_struct GTY(())
7d9d8943
AM
3386{
3387 enum dwarf_tag die_tag;
881c6935 3388 char *die_symbol;
7d9d8943
AM
3389 dw_attr_ref die_attr;
3390 dw_die_ref die_parent;
3391 dw_die_ref die_child;
3392 dw_die_ref die_sib;
47fcfa7b 3393 dw_die_ref die_definition; /* ref from a specification to its definition */
7d9d8943
AM
3394 dw_offset die_offset;
3395 unsigned long die_abbrev;
1bfb5f8f 3396 int die_mark;
636c7bc4 3397 unsigned int decl_id;
7d9d8943
AM
3398}
3399die_node;
3400
3401/* The pubname structure */
3402
17211ab5 3403typedef struct pubname_struct GTY(())
7d9d8943
AM
3404{
3405 dw_die_ref die;
556273e0 3406 char *name;
7d9d8943
AM
3407}
3408pubname_entry;
3409
17211ab5 3410struct dw_ranges_struct GTY(())
a20612aa
RH
3411{
3412 int block_num;
3413};
3414
7d9d8943 3415/* The limbo die list structure. */
17211ab5 3416typedef struct limbo_die_struct GTY(())
7d9d8943
AM
3417{
3418 dw_die_ref die;
54ba1f0d 3419 tree created_for;
7d9d8943
AM
3420 struct limbo_die_struct *next;
3421}
3422limbo_die_node;
3423
3424/* How to start an assembler comment. */
3425#ifndef ASM_COMMENT_START
3426#define ASM_COMMENT_START ";#"
3427#endif
3428
cc2902df 3429/* Define a macro which returns nonzero for a TYPE_DECL which was
7d9d8943
AM
3430 implicitly generated for a tagged type.
3431
3432 Note that unlike the gcc front end (which generates a NULL named
3433 TYPE_DECL node for each complete tagged type, each array type, and
3434 each function type node created) the g++ front end generates a
3435 _named_ TYPE_DECL node for each tagged type node created.
3436 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3437 generate a DW_TAG_typedef DIE for them. */
3438
3439#define TYPE_DECL_IS_STUB(decl) \
3440 (DECL_NAME (decl) == NULL_TREE \
3441 || (DECL_ARTIFICIAL (decl) \
3442 && is_tagged_type (TREE_TYPE (decl)) \
3443 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3444 /* This is necessary for stub decls that \
3445 appear in nested inline functions. */ \
3446 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3447 && (decl_ultimate_origin (decl) \
3448 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3449
3450/* Information concerning the compilation unit's programming
3451 language, and compiler version. */
3452
7d9d8943 3453/* Fixed size portion of the DWARF compilation unit header. */
9eb0ef7a
KB
3454#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3455 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
7d9d8943 3456
7d9d8943
AM
3457/* Fixed size portion of public names info. */
3458#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3459
3460/* Fixed size portion of the address range info. */
3461#define DWARF_ARANGES_HEADER_SIZE \
c583e7c3
KB
3462 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3463 DWARF2_ADDR_SIZE * 2) \
3464 - DWARF_INITIAL_LENGTH_SIZE)
7d9d8943
AM
3465
3466/* Size of padding portion in the address range info. It must be
3467 aligned to twice the pointer size. */
3468#define DWARF_ARANGES_PAD_SIZE \
c583e7c3
KB
3469 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3470 DWARF2_ADDR_SIZE * 2) \
3471 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
7d9d8943 3472
9d147085 3473/* Use assembler line directives if available. */
7d9d8943 3474#ifndef DWARF2_ASM_LINE_DEBUG_INFO
9d147085
RH
3475#ifdef HAVE_AS_DWARF2_DEBUG_LINE
3476#define DWARF2_ASM_LINE_DEBUG_INFO 1
3477#else
7d9d8943
AM
3478#define DWARF2_ASM_LINE_DEBUG_INFO 0
3479#endif
9d147085 3480#endif
7d9d8943 3481
7d9d8943
AM
3482/* Minimum line offset in a special line info. opcode.
3483 This value was chosen to give a reasonable range of values. */
3484#define DWARF_LINE_BASE -10
3485
a1f300c0 3486/* First special line opcode - leave room for the standard opcodes. */
7d9d8943
AM
3487#define DWARF_LINE_OPCODE_BASE 10
3488
3489/* Range of line offsets in a special line info. opcode. */
3490#define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3491
3492/* Flag that indicates the initial value of the is_stmt_start flag.
3493 In the present implementation, we do not mark any lines as
3494 the beginning of a source statement, because that information
3495 is not made available by the GCC front-end. */
3496#define DWARF_LINE_DEFAULT_IS_STMT_START 1
3497
c1b50e49 3498#ifdef DWARF2_DEBUGGING_INFO
7d9d8943
AM
3499/* This location is used by calc_die_sizes() to keep track
3500 the offset of each DIE within the .debug_info section. */
3501static unsigned long next_die_offset;
c1b50e49 3502#endif
7d9d8943
AM
3503
3504/* Record the root of the DIE's built for the current compilation unit. */
17211ab5 3505static GTY(()) dw_die_ref comp_unit_die;
7d9d8943
AM
3506
3507/* A list of DIEs with a NULL parent waiting to be relocated. */
17211ab5 3508static GTY(()) limbo_die_node *limbo_die_list;
7d9d8943 3509
981975b6 3510/* Filenames referenced by this compilation unit. */
c4274b22 3511static GTY(()) varray_type file_table;
73c68f61 3512static GTY(()) varray_type file_table_emitted;
c4274b22 3513static GTY(()) size_t file_table_last_lookup_index;
2e18bbae 3514
636c7bc4
JZ
3515/* A hash table of references to DIE's that describe declarations.
3516 The key is a DECL_UID() which is a unique number identifying each decl. */
3517static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
7d9d8943 3518
0a2d3d69
DB
3519/* Node of the variable location list. */
3520struct var_loc_node GTY ((chain_next ("%h.next")))
3521{
3522 rtx GTY (()) var_loc_note;
3523 const char * GTY (()) label;
3524 struct var_loc_node * GTY (()) next;
3525};
3526
3527/* Variable location list. */
3528struct var_loc_list_def GTY (())
3529{
3530 struct var_loc_node * GTY (()) first;
3531
3532 /* Do not mark the last element of the chained list because
3533 it is marked through the chain. */
3534 struct var_loc_node * GTY ((skip ("%h"))) last;
3535
3536 /* DECL_UID of the variable decl. */
3537 unsigned int decl_id;
3538};
3539typedef struct var_loc_list_def var_loc_list;
3540
0a2d3d69
DB
3541
3542/* Table of decl location linked lists. */
3543static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3544
7d9d8943
AM
3545/* A pointer to the base of a list of references to DIE's that
3546 are uniquely identified by their tag, presence/absence of
3547 children DIE's, and list of attribute/value pairs. */
7080f735 3548static GTY((length ("abbrev_die_table_allocated")))
17211ab5 3549 dw_die_ref *abbrev_die_table;
7d9d8943
AM
3550
3551/* Number of elements currently allocated for abbrev_die_table. */
c2e9147c 3552static GTY(()) unsigned abbrev_die_table_allocated;
7d9d8943
AM
3553
3554/* Number of elements in type_die_table currently in use. */
c2e9147c 3555static GTY(()) unsigned abbrev_die_table_in_use;
7d9d8943
AM
3556
3557/* Size (in elements) of increments by which we may expand the
3558 abbrev_die_table. */
3559#define ABBREV_DIE_TABLE_INCREMENT 256
3560
3561/* A pointer to the base of a table that contains line information
3562 for each source code line in .text in the compilation unit. */
7080f735 3563static GTY((length ("line_info_table_allocated")))
17211ab5 3564 dw_line_info_ref line_info_table;
7d9d8943
AM
3565
3566/* Number of elements currently allocated for line_info_table. */
c2e9147c 3567static GTY(()) unsigned line_info_table_allocated;
7d9d8943 3568
17211ab5 3569/* Number of elements in line_info_table currently in use. */
c2e9147c 3570static GTY(()) unsigned line_info_table_in_use;
7d9d8943
AM
3571
3572/* A pointer to the base of a table that contains line information
3573 for each source code line outside of .text in the compilation unit. */
17211ab5
GK
3574static GTY ((length ("separate_line_info_table_allocated")))
3575 dw_separate_line_info_ref separate_line_info_table;
7d9d8943
AM
3576
3577/* Number of elements currently allocated for separate_line_info_table. */
c2e9147c 3578static GTY(()) unsigned separate_line_info_table_allocated;
7d9d8943 3579
17211ab5 3580/* Number of elements in separate_line_info_table currently in use. */
c2e9147c 3581static GTY(()) unsigned separate_line_info_table_in_use;
7d9d8943
AM
3582
3583/* Size (in elements) of increments by which we may expand the
3584 line_info_table. */
3585#define LINE_INFO_TABLE_INCREMENT 1024
3586
3587/* A pointer to the base of a table that contains a list of publicly
3588 accessible names. */
17211ab5 3589static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
7d9d8943
AM
3590
3591/* Number of elements currently allocated for pubname_table. */
c2e9147c 3592static GTY(()) unsigned pubname_table_allocated;
7d9d8943
AM
3593
3594/* Number of elements in pubname_table currently in use. */
c2e9147c 3595static GTY(()) unsigned pubname_table_in_use;
7d9d8943
AM
3596
3597/* Size (in elements) of increments by which we may expand the
3598 pubname_table. */
3599#define PUBNAME_TABLE_INCREMENT 64
3600
a20612aa 3601/* Array of dies for which we should generate .debug_arange info. */
17211ab5 3602static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
7d9d8943
AM
3603
3604/* Number of elements currently allocated for arange_table. */
c2e9147c 3605static GTY(()) unsigned arange_table_allocated;
7d9d8943
AM
3606
3607/* Number of elements in arange_table currently in use. */
c2e9147c 3608static GTY(()) unsigned arange_table_in_use;
7d9d8943
AM
3609
3610/* Size (in elements) of increments by which we may expand the
3611 arange_table. */
3612#define ARANGE_TABLE_INCREMENT 64
3613
a20612aa 3614/* Array of dies for which we should generate .debug_ranges info. */
17211ab5 3615static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
a20612aa
RH
3616
3617/* Number of elements currently allocated for ranges_table. */
c2e9147c 3618static GTY(()) unsigned ranges_table_allocated;
a20612aa
RH
3619
3620/* Number of elements in ranges_table currently in use. */
c2e9147c 3621static GTY(()) unsigned ranges_table_in_use;
a20612aa
RH
3622
3623/* Size (in elements) of increments by which we may expand the
3624 ranges_table. */
3625#define RANGES_TABLE_INCREMENT 64
3626
63e46568 3627/* Whether we have location lists that need outputting */
c2e9147c 3628static GTY(()) unsigned have_location_lists;
63e46568 3629
57d4f65c
ZW
3630/* Unique label counter. */
3631static GTY(()) unsigned int loclabel_num;
3632
c2e9147c 3633#ifdef DWARF2_DEBUGGING_INFO
7d9d8943
AM
3634/* Record whether the function being analyzed contains inlined functions. */
3635static int current_function_has_inlines;
c1b50e49 3636#endif
7d9d8943
AM
3637#if 0 && defined (MIPS_DEBUGGING_INFO)
3638static int comp_unit_has_inlines;
3639#endif
3640
e0bb17a8 3641/* Number of file tables emitted in maybe_emit_file(). */
c2e9147c
GK
3642static GTY(()) int emitcount = 0;
3643
71c0e7fc 3644/* Number of internal labels generated by gen_internal_sym(). */
c2e9147c
GK
3645static GTY(()) int label_num;
3646
17211ab5
GK
3647#ifdef DWARF2_DEBUGGING_INFO
3648
7d9d8943
AM
3649/* Forward declarations for functions defined in this file. */
3650
7080f735
AJ
3651static int is_pseudo_reg (rtx);
3652static tree type_main_variant (tree);
3653static int is_tagged_type (tree);
3654static const char *dwarf_tag_name (unsigned);
3655static const char *dwarf_attr_name (unsigned);
3656static const char *dwarf_form_name (unsigned);
7d9d8943 3657#if 0
7080f735 3658static const char *dwarf_type_encoding_name (unsigned);
7d9d8943 3659#endif
7080f735
AJ
3660static tree decl_ultimate_origin (tree);
3661static tree block_ultimate_origin (tree);
3662static tree decl_class_context (tree);
3663static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3664static inline enum dw_val_class AT_class (dw_attr_ref);
3665static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3666static inline unsigned AT_flag (dw_attr_ref);
799f628a
JH
3667static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3668static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3669static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3670static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
7080f735
AJ
3671static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3672 unsigned long);
e7ee3914
AM
3673static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3674 unsigned int, unsigned char *);
7080f735
AJ
3675static hashval_t debug_str_do_hash (const void *);
3676static int debug_str_eq (const void *, const void *);
3677static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3678static inline const char *AT_string (dw_attr_ref);
3679static int AT_string_form (dw_attr_ref);
3680static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
47fcfa7b 3681static void add_AT_specification (dw_die_ref, dw_die_ref);
7080f735
AJ
3682static inline dw_die_ref AT_ref (dw_attr_ref);
3683static inline int AT_ref_external (dw_attr_ref);
3684static inline void set_AT_ref_external (dw_attr_ref, int);
3685static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3686static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3687static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3688static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3689 dw_loc_list_ref);
3690static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3691static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3692static inline rtx AT_addr (dw_attr_ref);
3693static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3694static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
799f628a
JH
3695static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3696 unsigned HOST_WIDE_INT);
7080f735
AJ
3697static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3698 unsigned long);
3699static inline const char *AT_lbl (dw_attr_ref);
3700static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3701static const char *get_AT_low_pc (dw_die_ref);
3702static const char *get_AT_hi_pc (dw_die_ref);
3703static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3704static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3705static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3706static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3707static bool is_c_family (void);
3708static bool is_cxx (void);
3709static bool is_java (void);
3710static bool is_fortran (void);
3711static bool is_ada (void);
3712static void remove_AT (dw_die_ref, enum dwarf_attribute);
6097b0c3 3713static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
7080f735
AJ
3714static inline void free_die (dw_die_ref);
3715static void remove_children (dw_die_ref);
3716static void add_child_die (dw_die_ref, dw_die_ref);
3717static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3718static dw_die_ref lookup_type_die (tree);
3719static void equate_type_number_to_die (tree, dw_die_ref);
636c7bc4
JZ
3720static hashval_t decl_die_table_hash (const void *);
3721static int decl_die_table_eq (const void *, const void *);
7080f735 3722static dw_die_ref lookup_decl_die (tree);
0a2d3d69
DB
3723static hashval_t decl_loc_table_hash (const void *);
3724static int decl_loc_table_eq (const void *, const void *);
3725static var_loc_list *lookup_decl_loc (tree);
7080f735 3726static void equate_decl_number_to_die (tree, dw_die_ref);
0a2d3d69 3727static void add_var_loc_to_decl (tree, struct var_loc_node *);
7080f735
AJ
3728static void print_spaces (FILE *);
3729static void print_die (dw_die_ref, FILE *);
3730static void print_dwarf_line_table (FILE *);
3731static void reverse_die_lists (dw_die_ref);
3732static void reverse_all_dies (dw_die_ref);
3733static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3734static dw_die_ref pop_compile_unit (dw_die_ref);
3735static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3736static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3737static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3738static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3739static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3740static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3741static int same_die_p (dw_die_ref, dw_die_ref, int *);
3742static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3743static void compute_section_prefix (dw_die_ref);
3744static int is_type_die (dw_die_ref);
3745static int is_comdat_die (dw_die_ref);
3746static int is_symbol_die (dw_die_ref);
3747static void assign_symbol_names (dw_die_ref);
3748static void break_out_includes (dw_die_ref);
3749static hashval_t htab_cu_hash (const void *);
3750static int htab_cu_eq (const void *, const void *);
3751static void htab_cu_del (void *);
3752static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3753static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3754static void add_sibling_attributes (dw_die_ref);
3755static void build_abbrev_table (dw_die_ref);
3756static void output_location_lists (dw_die_ref);
3757static int constant_size (long unsigned);
3758static unsigned long size_of_die (dw_die_ref);
3759static void calc_die_sizes (dw_die_ref);
3760static void mark_dies (dw_die_ref);
3761static void unmark_dies (dw_die_ref);
3762static void unmark_all_dies (dw_die_ref);
3763static unsigned long size_of_pubnames (void);
3764static unsigned long size_of_aranges (void);
3765static enum dwarf_form value_format (dw_attr_ref);
3766static void output_value_format (dw_attr_ref);
3767static void output_abbrev_section (void);
3768static void output_die_symbol (dw_die_ref);
3769static void output_die (dw_die_ref);
3770static void output_compilation_unit_header (void);
3771static void output_comp_unit (dw_die_ref, int);
3772static const char *dwarf2_name (tree, int);
3773static void add_pubname (tree, dw_die_ref);
3774static void output_pubnames (void);
3775static void add_arange (tree, dw_die_ref);
3776static void output_aranges (void);
3777static unsigned int add_ranges (tree);
3778static void output_ranges (void);
3779static void output_line_info (void);
3780static void output_file_names (void);
3781static dw_die_ref base_type_die (tree);
3782static tree root_type (tree);
3783static int is_base_type (tree);
e7d23ce3 3784static bool is_subrange_type (tree);
fbfd77b8 3785static dw_die_ref subrange_type_die (tree, dw_die_ref);
7080f735
AJ
3786static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3787static int type_is_enum (tree);
23959f19 3788static unsigned int dbx_reg_number (rtx);
7080f735
AJ
3789static dw_loc_descr_ref reg_loc_descriptor (rtx);
3790static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3791static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3792static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
0a2d3d69 3793static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
7080f735 3794static int is_based_loc (rtx);
0a2d3d69 3795static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
7080f735 3796static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
0a2d3d69 3797static dw_loc_descr_ref loc_descriptor (rtx, bool);
7080f735
AJ
3798static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3799static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3800static tree field_type (tree);
3801static unsigned int simple_type_align_in_bits (tree);
3802static unsigned int simple_decl_align_in_bits (tree);
3803static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3804static HOST_WIDE_INT field_byte_offset (tree);
3805static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3806 dw_loc_descr_ref);
3807static void add_data_member_location_attribute (dw_die_ref, tree);
3808static void add_const_value_attribute (dw_die_ref, rtx);
e7ee3914
AM
3809static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3810static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3811static void insert_float (rtx, unsigned char *);
7080f735 3812static rtx rtl_for_decl_location (tree);
0a2d3d69
DB
3813static void add_location_or_const_value_attribute (dw_die_ref, tree,
3814 enum dwarf_attribute);
7080f735
AJ
3815static void tree_add_const_value_attribute (dw_die_ref, tree);
3816static void add_name_attribute (dw_die_ref, const char *);
3817static void add_comp_dir_attribute (dw_die_ref);
3818static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3819static void add_subscript_info (dw_die_ref, tree);
3820static void add_byte_size_attribute (dw_die_ref, tree);
3821static void add_bit_offset_attribute (dw_die_ref, tree);
3822static void add_bit_size_attribute (dw_die_ref, tree);
3823static void add_prototyped_attribute (dw_die_ref, tree);
3824static void add_abstract_origin_attribute (dw_die_ref, tree);
3825static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3826static void add_src_coords_attributes (dw_die_ref, tree);
3827static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3828static void push_decl_scope (tree);
3829static void pop_decl_scope (void);
3830static dw_die_ref scope_die_for (tree, dw_die_ref);
3831static inline int local_scope_p (dw_die_ref);
66c78aa9 3832static inline int class_or_namespace_scope_p (dw_die_ref);
7080f735
AJ
3833static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3834static const char *type_tag (tree);
3835static tree member_declared_type (tree);
7d9d8943 3836#if 0
7080f735 3837static const char *decl_start_label (tree);
7d9d8943 3838#endif
7080f735
AJ
3839static void gen_array_type_die (tree, dw_die_ref);
3840static void gen_set_type_die (tree, dw_die_ref);
7d9d8943 3841#if 0
7080f735 3842static void gen_entry_point_die (tree, dw_die_ref);
7d9d8943 3843#endif
7080f735
AJ
3844static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
3845static void gen_inlined_structure_type_die (tree, dw_die_ref);
3846static void gen_inlined_union_type_die (tree, dw_die_ref);
de99511b 3847static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
7080f735
AJ
3848static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
3849static void gen_unspecified_parameters_die (tree, dw_die_ref);
3850static void gen_formal_types_die (tree, dw_die_ref);
3851static void gen_subprogram_die (tree, dw_die_ref);
3852static void gen_variable_die (tree, dw_die_ref);
3853static void gen_label_die (tree, dw_die_ref);
3854static void gen_lexical_block_die (tree, dw_die_ref, int);
3855static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3856static void gen_field_die (tree, dw_die_ref);
3857static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3858static dw_die_ref gen_compile_unit_die (const char *);
3859static void gen_string_type_die (tree, dw_die_ref);
3860static void gen_inheritance_die (tree, tree, dw_die_ref);
3861static void gen_member_die (tree, dw_die_ref);
3862static void gen_struct_or_union_type_die (tree, dw_die_ref);
3863static void gen_subroutine_type_die (tree, dw_die_ref);
3864static void gen_typedef_die (tree, dw_die_ref);
3865static void gen_type_die (tree, dw_die_ref);
3866static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
3867static void gen_block_die (tree, dw_die_ref, int);
3868static void decls_for_scope (tree, dw_die_ref, int);
3869static int is_redundant_typedef (tree);
66c78aa9 3870static void gen_namespace_die (tree);
7080f735 3871static void gen_decl_die (tree, dw_die_ref);
6097b0c3
DP
3872static dw_die_ref force_decl_die (tree);
3873static dw_die_ref force_type_die (tree);
66c78aa9
JM
3874static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3875static void declare_in_namespace (tree, dw_die_ref);
7080f735
AJ
3876static unsigned lookup_filename (const char *);
3877static void init_file_table (void);
3878static void retry_incomplete_types (void);
3879static void gen_type_die_for_member (tree, tree, dw_die_ref);
3880static void splice_child_die (dw_die_ref, dw_die_ref);
3881static int file_info_cmp (const void *, const void *);
3882static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3883 const char *, const char *, unsigned);
3884static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
3885 const char *, const char *,
3886 const char *);
3887static void output_loc_list (dw_loc_list_ref);
3888static char *gen_internal_sym (const char *);
3889
3890static void prune_unmark_dies (dw_die_ref);
3891static void prune_unused_types_mark (dw_die_ref, int);
3892static void prune_unused_types_walk (dw_die_ref);
3893static void prune_unused_types_walk_attribs (dw_die_ref);
3894static void prune_unused_types_prune (dw_die_ref);
3895static void prune_unused_types (void);
3896static int maybe_emit_file (int);
73c68f61 3897
7d9d8943
AM
3898/* Section names used to hold DWARF debugging information. */
3899#ifndef DEBUG_INFO_SECTION
3900#define DEBUG_INFO_SECTION ".debug_info"
3901#endif
9d2f2c45
RH
3902#ifndef DEBUG_ABBREV_SECTION
3903#define DEBUG_ABBREV_SECTION ".debug_abbrev"
7d9d8943 3904#endif
9d2f2c45
RH
3905#ifndef DEBUG_ARANGES_SECTION
3906#define DEBUG_ARANGES_SECTION ".debug_aranges"
7d9d8943 3907#endif
9d2f2c45
RH
3908#ifndef DEBUG_MACINFO_SECTION
3909#define DEBUG_MACINFO_SECTION ".debug_macinfo"
7d9d8943
AM
3910#endif
3911#ifndef DEBUG_LINE_SECTION
3912#define DEBUG_LINE_SECTION ".debug_line"
3913#endif
9d2f2c45
RH
3914#ifndef DEBUG_LOC_SECTION
3915#define DEBUG_LOC_SECTION ".debug_loc"
7d9d8943 3916#endif
9d2f2c45
RH
3917#ifndef DEBUG_PUBNAMES_SECTION
3918#define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
7d9d8943 3919#endif
9d2f2c45
RH
3920#ifndef DEBUG_STR_SECTION
3921#define DEBUG_STR_SECTION ".debug_str"
7d9d8943 3922#endif
a20612aa
RH
3923#ifndef DEBUG_RANGES_SECTION
3924#define DEBUG_RANGES_SECTION ".debug_ranges"
3925#endif
7d9d8943
AM
3926
3927/* Standard ELF section names for compiled code and data. */
f99ffb60
RH
3928#ifndef TEXT_SECTION_NAME
3929#define TEXT_SECTION_NAME ".text"
7d9d8943
AM
3930#endif
3931
9eb4015a 3932/* Section flags for .debug_str section. */
9eb4015a 3933#define DEBUG_STR_SECTION_FLAGS \
5d4856a0 3934 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
b0c242c0
AM
3935 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3936 : SECTION_DEBUG)
9eb4015a 3937
7d9d8943 3938/* Labels we insert at beginning sections we can reference instead of
556273e0 3939 the section names themselves. */
7d9d8943
AM
3940
3941#ifndef TEXT_SECTION_LABEL
9d2f2c45 3942#define TEXT_SECTION_LABEL "Ltext"
7d9d8943
AM
3943#endif
3944#ifndef DEBUG_LINE_SECTION_LABEL
9d2f2c45 3945#define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
7d9d8943
AM
3946#endif
3947#ifndef DEBUG_INFO_SECTION_LABEL
9d2f2c45 3948#define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
7d9d8943 3949#endif
9d2f2c45
RH
3950#ifndef DEBUG_ABBREV_SECTION_LABEL
3951#define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
7d9d8943 3952#endif
9d2f2c45
RH
3953#ifndef DEBUG_LOC_SECTION_LABEL
3954#define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
63e46568 3955#endif
2bee6045
JJ
3956#ifndef DEBUG_RANGES_SECTION_LABEL
3957#define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3958#endif
84a5b4f8
DB
3959#ifndef DEBUG_MACINFO_SECTION_LABEL
3960#define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3961#endif
a20612aa 3962
7d9d8943
AM
3963/* Definitions of defaults for formats and names of various special
3964 (artificial) labels which may be generated within this file (when the -g
def66b10 3965 options is used and DWARF2_DEBUGGING_INFO is in effect.
7d9d8943
AM
3966 If necessary, these may be overridden from within the tm.h file, but
3967 typically, overriding these defaults is unnecessary. */
3968
3969static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3970static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3971static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3972static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3973static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
84a5b4f8 3974static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
63e46568 3975static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
2bee6045 3976static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
2ad9852d 3977
7d9d8943
AM
3978#ifndef TEXT_END_LABEL
3979#define TEXT_END_LABEL "Letext"
3980#endif
7d9d8943
AM
3981#ifndef BLOCK_BEGIN_LABEL
3982#define BLOCK_BEGIN_LABEL "LBB"
3983#endif
3984#ifndef BLOCK_END_LABEL
3985#define BLOCK_END_LABEL "LBE"
3986#endif
7d9d8943
AM
3987#ifndef LINE_CODE_LABEL
3988#define LINE_CODE_LABEL "LM"
3989#endif
3990#ifndef SEPARATE_LINE_CODE_LABEL
3991#define SEPARATE_LINE_CODE_LABEL "LSM"
3992#endif
3993\f
3994/* We allow a language front-end to designate a function that is to be
3995 called to "demangle" any name before it it put into a DIE. */
3996
7080f735 3997static const char *(*demangle_name_func) (const char *);
7d9d8943
AM
3998
3999void
7080f735 4000dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
7d9d8943
AM
4001{
4002 demangle_name_func = func;
4003}
7d9d8943
AM
4004
4005/* Test if rtl node points to a pseudo register. */
4006
4007static inline int
7080f735 4008is_pseudo_reg (rtx rtl)
7d9d8943
AM
4009{
4010 return ((GET_CODE (rtl) == REG && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4011 || (GET_CODE (rtl) == SUBREG
ddef6bc7 4012 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
7d9d8943
AM
4013}
4014
4015/* Return a reference to a type, with its const and volatile qualifiers
4016 removed. */
4017
4018static inline tree
7080f735 4019type_main_variant (tree type)
7d9d8943
AM
4020{
4021 type = TYPE_MAIN_VARIANT (type);
4022
2ad9852d
RK
4023 /* ??? There really should be only one main variant among any group of
4024 variants of a given type (and all of the MAIN_VARIANT values for all
4025 members of the group should point to that one type) but sometimes the C
4026 front-end messes this up for array types, so we work around that bug
4027 here. */
7d9d8943
AM
4028 if (TREE_CODE (type) == ARRAY_TYPE)
4029 while (type != TYPE_MAIN_VARIANT (type))
4030 type = TYPE_MAIN_VARIANT (type);
4031
4032 return type;
4033}
4034
cc2902df 4035/* Return nonzero if the given type node represents a tagged type. */
7d9d8943
AM
4036
4037static inline int
7080f735 4038is_tagged_type (tree type)
7d9d8943 4039{
b3694847 4040 enum tree_code code = TREE_CODE (type);
7d9d8943
AM
4041
4042 return (code == RECORD_TYPE || code == UNION_TYPE
4043 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4044}
4045
4046/* Convert a DIE tag into its string name. */
4047
4048static const char *
7080f735 4049dwarf_tag_name (unsigned int tag)
7d9d8943
AM
4050{
4051 switch (tag)
4052 {
4053 case DW_TAG_padding:
4054 return "DW_TAG_padding";
4055 case DW_TAG_array_type:
4056 return "DW_TAG_array_type";
4057 case DW_TAG_class_type:
4058 return "DW_TAG_class_type";
4059 case DW_TAG_entry_point:
4060 return "DW_TAG_entry_point";
4061 case DW_TAG_enumeration_type:
4062 return "DW_TAG_enumeration_type";
4063 case DW_TAG_formal_parameter:
4064 return "DW_TAG_formal_parameter";
4065 case DW_TAG_imported_declaration:
4066 return "DW_TAG_imported_declaration";
4067 case DW_TAG_label:
4068 return "DW_TAG_label";
4069 case DW_TAG_lexical_block:
4070 return "DW_TAG_lexical_block";
4071 case DW_TAG_member:
4072 return "DW_TAG_member";
4073 case DW_TAG_pointer_type:
4074 return "DW_TAG_pointer_type";
4075 case DW_TAG_reference_type:
4076 return "DW_TAG_reference_type";
4077 case DW_TAG_compile_unit:
4078 return "DW_TAG_compile_unit";
4079 case DW_TAG_string_type:
4080 return "DW_TAG_string_type";
4081 case DW_TAG_structure_type:
4082 return "DW_TAG_structure_type";
4083 case DW_TAG_subroutine_type:
4084 return "DW_TAG_subroutine_type";
4085 case DW_TAG_typedef:
4086 return "DW_TAG_typedef";
4087 case DW_TAG_union_type:
4088 return "DW_TAG_union_type";
4089 case DW_TAG_unspecified_parameters:
4090 return "DW_TAG_unspecified_parameters";
4091 case DW_TAG_variant:
4092 return "DW_TAG_variant";
4093 case DW_TAG_common_block:
4094 return "DW_TAG_common_block";
4095 case DW_TAG_common_inclusion:
4096 return "DW_TAG_common_inclusion";
4097 case DW_TAG_inheritance:
4098 return "DW_TAG_inheritance";
4099 case DW_TAG_inlined_subroutine:
4100 return "DW_TAG_inlined_subroutine";
4101 case DW_TAG_module:
4102 return "DW_TAG_module";
4103 case DW_TAG_ptr_to_member_type:
4104 return "DW_TAG_ptr_to_member_type";
4105 case DW_TAG_set_type:
4106 return "DW_TAG_set_type";
4107 case DW_TAG_subrange_type:
4108 return "DW_TAG_subrange_type";
4109 case DW_TAG_with_stmt:
4110 return "DW_TAG_with_stmt";
4111 case DW_TAG_access_declaration:
4112 return "DW_TAG_access_declaration";
4113 case DW_TAG_base_type:
4114 return "DW_TAG_base_type";
4115 case DW_TAG_catch_block:
4116 return "DW_TAG_catch_block";
4117 case DW_TAG_const_type:
4118 return "DW_TAG_const_type";
4119 case DW_TAG_constant:
4120 return "DW_TAG_constant";
4121 case DW_TAG_enumerator:
4122 return "DW_TAG_enumerator";
4123 case DW_TAG_file_type:
4124 return "DW_TAG_file_type";
4125 case DW_TAG_friend:
4126 return "DW_TAG_friend";
4127 case DW_TAG_namelist:
4128 return "DW_TAG_namelist";
4129 case DW_TAG_namelist_item:
4130 return "DW_TAG_namelist_item";
66c78aa9
JM
4131 case DW_TAG_namespace:
4132 return "DW_TAG_namespace";
7d9d8943
AM
4133 case DW_TAG_packed_type:
4134 return "DW_TAG_packed_type";
4135 case DW_TAG_subprogram:
4136 return "DW_TAG_subprogram";
4137 case DW_TAG_template_type_param:
4138 return "DW_TAG_template_type_param";
4139 case DW_TAG_template_value_param:
4140 return "DW_TAG_template_value_param";
4141 case DW_TAG_thrown_type:
4142 return "DW_TAG_thrown_type";
4143 case DW_TAG_try_block:
4144 return "DW_TAG_try_block";
4145 case DW_TAG_variant_part:
4146 return "DW_TAG_variant_part";
4147 case DW_TAG_variable:
4148 return "DW_TAG_variable";
4149 case DW_TAG_volatile_type:
4150 return "DW_TAG_volatile_type";
6097b0c3
DP
4151 case DW_TAG_imported_module:
4152 return "DW_TAG_imported_module";
7d9d8943
AM
4153 case DW_TAG_MIPS_loop:
4154 return "DW_TAG_MIPS_loop";
4155 case DW_TAG_format_label:
4156 return "DW_TAG_format_label";
4157 case DW_TAG_function_template:
4158 return "DW_TAG_function_template";
4159 case DW_TAG_class_template:
4160 return "DW_TAG_class_template";
881c6935
JM
4161 case DW_TAG_GNU_BINCL:
4162 return "DW_TAG_GNU_BINCL";
4163 case DW_TAG_GNU_EINCL:
4164 return "DW_TAG_GNU_EINCL";
7d9d8943
AM
4165 default:
4166 return "DW_TAG_<unknown>";
4167 }
4168}
4169
4170/* Convert a DWARF attribute code into its string name. */
4171
4172static const char *
7080f735 4173dwarf_attr_name (unsigned int attr)
7d9d8943
AM
4174{
4175 switch (attr)
4176 {
4177 case DW_AT_sibling:
4178 return "DW_AT_sibling";
4179 case DW_AT_location:
4180 return "DW_AT_location";
4181 case DW_AT_name:
4182 return "DW_AT_name";
4183 case DW_AT_ordering:
4184 return "DW_AT_ordering";
4185 case DW_AT_subscr_data:
4186 return "DW_AT_subscr_data";
4187 case DW_AT_byte_size:
4188 return "DW_AT_byte_size";
4189 case DW_AT_bit_offset:
4190 return "DW_AT_bit_offset";
4191 case DW_AT_bit_size:
4192 return "DW_AT_bit_size";
4193 case DW_AT_element_list:
4194 return "DW_AT_element_list";
4195 case DW_AT_stmt_list:
4196 return "DW_AT_stmt_list";
4197 case DW_AT_low_pc:
4198 return "DW_AT_low_pc";
4199 case DW_AT_high_pc:
4200 return "DW_AT_high_pc";
4201 case DW_AT_language:
4202 return "DW_AT_language";
4203 case DW_AT_member:
4204 return "DW_AT_member";
4205 case DW_AT_discr:
4206 return "DW_AT_discr";
4207 case DW_AT_discr_value:
4208 return "DW_AT_discr_value";
4209 case DW_AT_visibility:
4210 return "DW_AT_visibility";
4211 case DW_AT_import:
4212 return "DW_AT_import";
4213 case DW_AT_string_length:
4214 return "DW_AT_string_length";
4215 case DW_AT_common_reference:
4216 return "DW_AT_common_reference";
4217 case DW_AT_comp_dir:
4218 return "DW_AT_comp_dir";
4219 case DW_AT_const_value:
4220 return "DW_AT_const_value";
4221 case DW_AT_containing_type:
4222 return "DW_AT_containing_type";
4223 case DW_AT_default_value:
4224 return "DW_AT_default_value";
4225 case DW_AT_inline:
4226 return "DW_AT_inline";
4227 case DW_AT_is_optional:
4228 return "DW_AT_is_optional";
4229 case DW_AT_lower_bound:
4230 return "DW_AT_lower_bound";
4231 case DW_AT_producer:
4232 return "DW_AT_producer";
4233 case DW_AT_prototyped:
4234 return "DW_AT_prototyped";
4235 case DW_AT_return_addr:
4236 return "DW_AT_return_addr";
4237 case DW_AT_start_scope:
4238 return "DW_AT_start_scope";
4239 case DW_AT_stride_size:
4240 return "DW_AT_stride_size";
4241 case DW_AT_upper_bound:
4242 return "DW_AT_upper_bound";
4243 case DW_AT_abstract_origin:
4244 return "DW_AT_abstract_origin";
4245 case DW_AT_accessibility:
4246 return "DW_AT_accessibility";
4247 case DW_AT_address_class:
4248 return "DW_AT_address_class";
4249 case DW_AT_artificial:
4250 return "DW_AT_artificial";
4251 case DW_AT_base_types:
4252 return "DW_AT_base_types";
4253 case DW_AT_calling_convention:
4254 return "DW_AT_calling_convention";
4255 case DW_AT_count:
4256 return "DW_AT_count";
4257 case DW_AT_data_member_location:
4258 return "DW_AT_data_member_location";
4259 case DW_AT_decl_column:
4260 return "DW_AT_decl_column";
4261 case DW_AT_decl_file:
4262 return "DW_AT_decl_file";
4263 case DW_AT_decl_line:
4264 return "DW_AT_decl_line";
4265 case DW_AT_declaration:
4266 return "DW_AT_declaration";
4267 case DW_AT_discr_list:
4268 return "DW_AT_discr_list";
4269 case DW_AT_encoding:
4270 return "DW_AT_encoding";
4271 case DW_AT_external:
4272 return "DW_AT_external";
4273 case DW_AT_frame_base:
4274 return "DW_AT_frame_base";
4275 case DW_AT_friend:
4276 return "DW_AT_friend";
4277 case DW_AT_identifier_case:
4278 return "DW_AT_identifier_case";
4279 case DW_AT_macro_info:
4280 return "DW_AT_macro_info";
4281 case DW_AT_namelist_items:
4282 return "DW_AT_namelist_items";
4283 case DW_AT_priority:
4284 return "DW_AT_priority";
4285 case DW_AT_segment:
4286 return "DW_AT_segment";
4287 case DW_AT_specification:
4288 return "DW_AT_specification";
4289 case DW_AT_static_link:
4290 return "DW_AT_static_link";
4291 case DW_AT_type:
4292 return "DW_AT_type";
4293 case DW_AT_use_location:
4294 return "DW_AT_use_location";
4295 case DW_AT_variable_parameter:
4296 return "DW_AT_variable_parameter";
4297 case DW_AT_virtuality:
4298 return "DW_AT_virtuality";
4299 case DW_AT_vtable_elem_location:
4300 return "DW_AT_vtable_elem_location";
4301
a20612aa
RH
4302 case DW_AT_allocated:
4303 return "DW_AT_allocated";
4304 case DW_AT_associated:
4305 return "DW_AT_associated";
4306 case DW_AT_data_location:
4307 return "DW_AT_data_location";
4308 case DW_AT_stride:
4309 return "DW_AT_stride";
4310 case DW_AT_entry_pc:
4311 return "DW_AT_entry_pc";
4312 case DW_AT_use_UTF8:
4313 return "DW_AT_use_UTF8";
4314 case DW_AT_extension:
4315 return "DW_AT_extension";
4316 case DW_AT_ranges:
4317 return "DW_AT_ranges";
4318 case DW_AT_trampoline:
4319 return "DW_AT_trampoline";
4320 case DW_AT_call_column:
4321 return "DW_AT_call_column";
4322 case DW_AT_call_file:
4323 return "DW_AT_call_file";
4324 case DW_AT_call_line:
4325 return "DW_AT_call_line";
4326
7d9d8943
AM
4327 case DW_AT_MIPS_fde:
4328 return "DW_AT_MIPS_fde";
4329 case DW_AT_MIPS_loop_begin:
4330 return "DW_AT_MIPS_loop_begin";
4331 case DW_AT_MIPS_tail_loop_begin:
4332 return "DW_AT_MIPS_tail_loop_begin";
4333 case DW_AT_MIPS_epilog_begin:
4334 return "DW_AT_MIPS_epilog_begin";
4335 case DW_AT_MIPS_loop_unroll_factor:
4336 return "DW_AT_MIPS_loop_unroll_factor";
4337 case DW_AT_MIPS_software_pipeline_depth:
4338 return "DW_AT_MIPS_software_pipeline_depth";
4339 case DW_AT_MIPS_linkage_name:
4340 return "DW_AT_MIPS_linkage_name";
4341 case DW_AT_MIPS_stride:
4342 return "DW_AT_MIPS_stride";
4343 case DW_AT_MIPS_abstract_name:
4344 return "DW_AT_MIPS_abstract_name";
4345 case DW_AT_MIPS_clone_origin:
4346 return "DW_AT_MIPS_clone_origin";
4347 case DW_AT_MIPS_has_inlines:
4348 return "DW_AT_MIPS_has_inlines";
4349
4350 case DW_AT_sf_names:
4351 return "DW_AT_sf_names";
4352 case DW_AT_src_info:
4353 return "DW_AT_src_info";
4354 case DW_AT_mac_info:
4355 return "DW_AT_mac_info";
4356 case DW_AT_src_coords:
4357 return "DW_AT_src_coords";
4358 case DW_AT_body_begin:
4359 return "DW_AT_body_begin";
4360 case DW_AT_body_end:
4361 return "DW_AT_body_end";
84f0ace0
JM
4362 case DW_AT_GNU_vector:
4363 return "DW_AT_GNU_vector";
4364
7a0c8d71
DR
4365 case DW_AT_VMS_rtnbeg_pd_address:
4366 return "DW_AT_VMS_rtnbeg_pd_address";
4367
7d9d8943
AM
4368 default:
4369 return "DW_AT_<unknown>";
4370 }
4371}
4372
4373/* Convert a DWARF value form code into its string name. */
4374
4375static const char *
7080f735 4376dwarf_form_name (unsigned int form)
7d9d8943
AM
4377{
4378 switch (form)
4379 {
4380 case DW_FORM_addr:
4381 return "DW_FORM_addr";
4382 case DW_FORM_block2:
4383 return "DW_FORM_block2";
4384 case DW_FORM_block4:
4385 return "DW_FORM_block4";
4386 case DW_FORM_data2:
4387 return "DW_FORM_data2";
4388 case DW_FORM_data4:
4389 return "DW_FORM_data4";
4390 case DW_FORM_data8:
4391 return "DW_FORM_data8";
4392 case DW_FORM_string:
4393 return "DW_FORM_string";
4394 case DW_FORM_block:
4395 return "DW_FORM_block";
4396 case DW_FORM_block1:
4397 return "DW_FORM_block1";
4398 case DW_FORM_data1:
4399 return "DW_FORM_data1";
4400 case DW_FORM_flag:
4401 return "DW_FORM_flag";
4402 case DW_FORM_sdata:
4403 return "DW_FORM_sdata";
4404 case DW_FORM_strp:
4405 return "DW_FORM_strp";
4406 case DW_FORM_udata:
4407 return "DW_FORM_udata";
4408 case DW_FORM_ref_addr:
4409 return "DW_FORM_ref_addr";
4410 case DW_FORM_ref1:
4411 return "DW_FORM_ref1";
4412 case DW_FORM_ref2:
4413 return "DW_FORM_ref2";
4414 case DW_FORM_ref4:
4415 return "DW_FORM_ref4";
4416 case DW_FORM_ref8:
4417 return "DW_FORM_ref8";
4418 case DW_FORM_ref_udata:
4419 return "DW_FORM_ref_udata";
4420 case DW_FORM_indirect:
4421 return "DW_FORM_indirect";
3f76745e 4422 default:
7d9d8943 4423 return "DW_FORM_<unknown>";
a3f97cbb
JW
4424 }
4425}
4426
3f76745e 4427/* Convert a DWARF type code into its string name. */
71dfc51f 4428
487a6e06 4429#if 0
d560ee52 4430static const char *
7080f735 4431dwarf_type_encoding_name (unsigned enc)
a3f97cbb 4432{
3f76745e 4433 switch (enc)
a3f97cbb 4434 {
3f76745e
JM
4435 case DW_ATE_address:
4436 return "DW_ATE_address";
4437 case DW_ATE_boolean:
4438 return "DW_ATE_boolean";
4439 case DW_ATE_complex_float:
4440 return "DW_ATE_complex_float";
4441 case DW_ATE_float:
4442 return "DW_ATE_float";
4443 case DW_ATE_signed:
4444 return "DW_ATE_signed";
4445 case DW_ATE_signed_char:
4446 return "DW_ATE_signed_char";
4447 case DW_ATE_unsigned:
4448 return "DW_ATE_unsigned";
4449 case DW_ATE_unsigned_char:
4450 return "DW_ATE_unsigned_char";
4451 default:
4452 return "DW_ATE_<unknown>";
4453 }
a3f97cbb 4454}
487a6e06 4455#endif
3f76745e
JM
4456\f
4457/* Determine the "ultimate origin" of a decl. The decl may be an inlined
4458 instance of an inlined instance of a decl which is local to an inline
4459 function, so we have to trace all of the way back through the origin chain
4460 to find out what sort of node actually served as the original seed for the
4461 given block. */
a3f97cbb 4462
3f76745e 4463static tree
7080f735 4464decl_ultimate_origin (tree decl)
a3f97cbb 4465{
10a11b75
JM
4466 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4467 nodes in the function to point to themselves; ignore that if
4468 we're trying to output the abstract instance of this function. */
4469 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4470 return NULL_TREE;
4471
556273e0 4472#ifdef ENABLE_CHECKING
02e24c7a
MM
4473 if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4474 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4475 most distant ancestor, this should never happen. */
4476 abort ();
4477#endif
3f76745e 4478
02e24c7a 4479 return DECL_ABSTRACT_ORIGIN (decl);
a3f97cbb
JW
4480}
4481
3f76745e
JM
4482/* Determine the "ultimate origin" of a block. The block may be an inlined
4483 instance of an inlined instance of a block which is local to an inline
4484 function, so we have to trace all of the way back through the origin chain
4485 to find out what sort of node actually served as the original seed for the
4486 given block. */
71dfc51f 4487
3f76745e 4488static tree
7080f735 4489block_ultimate_origin (tree block)
a3f97cbb 4490{
b3694847 4491 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
71dfc51f 4492
10a11b75
JM
4493 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4494 nodes in the function to point to themselves; ignore that if
4495 we're trying to output the abstract instance of this function. */
4496 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4497 return NULL_TREE;
4498
3f76745e
JM
4499 if (immediate_origin == NULL_TREE)
4500 return NULL_TREE;
4501 else
4502 {
b3694847
SS
4503 tree ret_val;
4504 tree lookahead = immediate_origin;
71dfc51f 4505
3f76745e
JM
4506 do
4507 {
4508 ret_val = lookahead;
2ad9852d
RK
4509 lookahead = (TREE_CODE (ret_val) == BLOCK
4510 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
3f76745e
JM
4511 }
4512 while (lookahead != NULL && lookahead != ret_val);
4513
4514 return ret_val;
4515 }
a3f97cbb
JW
4516}
4517
3f76745e
JM
4518/* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4519 of a virtual function may refer to a base class, so we check the 'this'
4520 parameter. */
71dfc51f 4521
3f76745e 4522static tree
7080f735 4523decl_class_context (tree decl)
a3f97cbb 4524{
3f76745e 4525 tree context = NULL_TREE;
71dfc51f 4526
3f76745e
JM
4527 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4528 context = DECL_CONTEXT (decl);
4529 else
4530 context = TYPE_MAIN_VARIANT
4531 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
71dfc51f 4532
2f939d94 4533 if (context && !TYPE_P (context))
3f76745e
JM
4534 context = NULL_TREE;
4535
4536 return context;
a3f97cbb
JW
4537}
4538\f
a96c67ec 4539/* Add an attribute/value pair to a DIE. We build the lists up in reverse
881c6935 4540 addition order, and correct that in reverse_all_dies. */
71dfc51f
RK
4541
4542static inline void
7080f735 4543add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
a3f97cbb 4544{
3f76745e 4545 if (die != NULL && attr != NULL)
a3f97cbb 4546 {
a96c67ec
JM
4547 attr->dw_attr_next = die->die_attr;
4548 die->die_attr = attr;
a3f97cbb
JW
4549 }
4550}
4551
17211ab5 4552static inline enum dw_val_class
7080f735 4553AT_class (dw_attr_ref a)
a96c67ec
JM
4554{
4555 return a->dw_attr_val.val_class;
4556}
4557
3f76745e 4558/* Add a flag value attribute to a DIE. */
71dfc51f 4559
3f76745e 4560static inline void
7080f735 4561add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
a3f97cbb 4562{
703ad42b 4563 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
71dfc51f 4564
3f76745e
JM
4565 attr->dw_attr_next = NULL;
4566 attr->dw_attr = attr_kind;
4567 attr->dw_attr_val.val_class = dw_val_class_flag;
4568 attr->dw_attr_val.v.val_flag = flag;
4569 add_dwarf_attr (die, attr);
a3f97cbb
JW
4570}
4571
a96c67ec 4572static inline unsigned
7080f735 4573AT_flag (dw_attr_ref a)
a96c67ec
JM
4574{
4575 if (a && AT_class (a) == dw_val_class_flag)
4576 return a->dw_attr_val.v.val_flag;
4577
40e8cc95 4578 abort ();
a96c67ec
JM
4579}
4580
3f76745e 4581/* Add a signed integer attribute value to a DIE. */
71dfc51f 4582
3f76745e 4583static inline void
799f628a 4584add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
a3f97cbb 4585{
703ad42b 4586 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
3f76745e
JM
4587
4588 attr->dw_attr_next = NULL;
4589 attr->dw_attr = attr_kind;
4590 attr->dw_attr_val.val_class = dw_val_class_const;
4591 attr->dw_attr_val.v.val_int = int_val;
4592 add_dwarf_attr (die, attr);
a3f97cbb
JW
4593}
4594
799f628a 4595static inline HOST_WIDE_INT
7080f735 4596AT_int (dw_attr_ref a)
a96c67ec
JM
4597{
4598 if (a && AT_class (a) == dw_val_class_const)
4599 return a->dw_attr_val.v.val_int;
4600
40e8cc95 4601 abort ();
a96c67ec
JM
4602}
4603
3f76745e 4604/* Add an unsigned integer attribute value to a DIE. */
71dfc51f 4605
3f76745e 4606static inline void
7080f735 4607add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
799f628a 4608 unsigned HOST_WIDE_INT unsigned_val)
a3f97cbb 4609{
703ad42b 4610 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
3f76745e
JM
4611
4612 attr->dw_attr_next = NULL;
4613 attr->dw_attr = attr_kind;
4614 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4615 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4616 add_dwarf_attr (die, attr);
a3f97cbb 4617}
71dfc51f 4618
799f628a 4619static inline unsigned HOST_WIDE_INT
7080f735 4620AT_unsigned (dw_attr_ref a)
a96c67ec
JM
4621{
4622 if (a && AT_class (a) == dw_val_class_unsigned_const)
4623 return a->dw_attr_val.v.val_unsigned;
4624
40e8cc95 4625 abort ();
a96c67ec
JM
4626}
4627
3f76745e
JM
4628/* Add an unsigned double integer attribute value to a DIE. */
4629
4630static inline void
7080f735
AJ
4631add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4632 long unsigned int val_hi, long unsigned int val_low)
a3f97cbb 4633{
703ad42b 4634 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
71dfc51f 4635
3f76745e
JM
4636 attr->dw_attr_next = NULL;
4637 attr->dw_attr = attr_kind;
4638 attr->dw_attr_val.val_class = dw_val_class_long_long;
4639 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4640 attr->dw_attr_val.v.val_long_long.low = val_low;
4641 add_dwarf_attr (die, attr);
4642}
71dfc51f 4643
3f76745e 4644/* Add a floating point attribute value to a DIE and return it. */
71dfc51f 4645
3f76745e 4646static inline void
e7ee3914
AM
4647add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4648 unsigned int length, unsigned int elt_size, unsigned char *array)
3f76745e 4649{
703ad42b 4650 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
3f76745e
JM
4651
4652 attr->dw_attr_next = NULL;
4653 attr->dw_attr = attr_kind;
e7ee3914
AM
4654 attr->dw_attr_val.val_class = dw_val_class_vec;
4655 attr->dw_attr_val.v.val_vec.length = length;
4656 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4657 attr->dw_attr_val.v.val_vec.array = array;
3f76745e 4658 add_dwarf_attr (die, attr);
a3f97cbb
JW
4659}
4660
17211ab5
GK
4661/* Hash and equality functions for debug_str_hash. */
4662
4663static hashval_t
7080f735 4664debug_str_do_hash (const void *x)
17211ab5
GK
4665{
4666 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4667}
4668
4669static int
7080f735 4670debug_str_eq (const void *x1, const void *x2)
17211ab5
GK
4671{
4672 return strcmp ((((const struct indirect_string_node *)x1)->str),
4673 (const char *)x2) == 0;
4674}
4675
3f76745e 4676/* Add a string attribute value to a DIE. */
71dfc51f 4677
3f76745e 4678static inline void
7080f735 4679add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
a3f97cbb 4680{
703ad42b 4681 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
9eb4015a 4682 struct indirect_string_node *node;
fad205ff 4683 void **slot;
c26fbbca 4684
9eb4015a 4685 if (! debug_str_hash)
7080f735 4686 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
17211ab5
GK
4687 debug_str_eq, NULL);
4688
4689 slot = htab_find_slot_with_hash (debug_str_hash, str,
4690 htab_hash_string (str), INSERT);
4691 if (*slot == NULL)
4692 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4693 node = (struct indirect_string_node *) *slot;
485bad26 4694 node->str = ggc_strdup (str);
9eb4015a 4695 node->refcount++;
71dfc51f 4696
3f76745e
JM
4697 attr->dw_attr_next = NULL;
4698 attr->dw_attr = attr_kind;
4699 attr->dw_attr_val.val_class = dw_val_class_str;
9eb4015a 4700 attr->dw_attr_val.v.val_str = node;
3f76745e
JM
4701 add_dwarf_attr (die, attr);
4702}
71dfc51f 4703
a96c67ec 4704static inline const char *
7080f735 4705AT_string (dw_attr_ref a)
a96c67ec
JM
4706{
4707 if (a && AT_class (a) == dw_val_class_str)
17211ab5 4708 return a->dw_attr_val.v.val_str->str;
9eb4015a
JJ
4709
4710 abort ();
4711}
4712
4713/* Find out whether a string should be output inline in DIE
4714 or out-of-line in .debug_str section. */
4715
9eb4015a 4716static int
7080f735 4717AT_string_form (dw_attr_ref a)
9eb4015a
JJ
4718{
4719 if (a && AT_class (a) == dw_val_class_str)
4720 {
4721 struct indirect_string_node *node;
4722 unsigned int len;
9eb4015a
JJ
4723 char label[32];
4724
4725 node = a->dw_attr_val.v.val_str;
4726 if (node->form)
4727 return node->form;
4728
17211ab5 4729 len = strlen (node->str) + 1;
9eb4015a 4730
2ad9852d
RK
4731 /* If the string is shorter or equal to the size of the reference, it is
4732 always better to put it inline. */
9eb4015a
JJ
4733 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4734 return node->form = DW_FORM_string;
4735
2ad9852d
RK
4736 /* If we cannot expect the linker to merge strings in .debug_str
4737 section, only put it into .debug_str if it is worth even in this
4738 single module. */
4739 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4740 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4741 return node->form = DW_FORM_string;
9eb4015a 4742
17211ab5
GK
4743 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4744 ++dw2_string_counter;
9eb4015a 4745 node->label = xstrdup (label);
2ad9852d 4746
9eb4015a
JJ
4747 return node->form = DW_FORM_strp;
4748 }
a96c67ec 4749
40e8cc95 4750 abort ();
a96c67ec
JM
4751}
4752
3f76745e 4753/* Add a DIE reference attribute value to a DIE. */
71dfc51f 4754
3f76745e 4755static inline void
7080f735 4756add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3f76745e 4757{
703ad42b 4758 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
71dfc51f 4759
3f76745e
JM
4760 attr->dw_attr_next = NULL;
4761 attr->dw_attr = attr_kind;
4762 attr->dw_attr_val.val_class = dw_val_class_die_ref;
881c6935
JM
4763 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4764 attr->dw_attr_val.v.val_die_ref.external = 0;
3f76745e
JM
4765 add_dwarf_attr (die, attr);
4766}
b1ccbc24 4767
47fcfa7b 4768/* Add an AT_specification attribute to a DIE, and also make the back
6614fd40 4769 pointer from the specification to the definition. */
47fcfa7b
SS
4770
4771static inline void
4772add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4773{
4774 add_AT_die_ref (die, DW_AT_specification, targ_die);
4775 if (targ_die->die_definition)
4776 abort ();
4777 targ_die->die_definition = die;
4778}
4779
a96c67ec 4780static inline dw_die_ref
7080f735 4781AT_ref (dw_attr_ref a)
a96c67ec
JM
4782{
4783 if (a && AT_class (a) == dw_val_class_die_ref)
881c6935 4784 return a->dw_attr_val.v.val_die_ref.die;
a96c67ec 4785
40e8cc95 4786 abort ();
a96c67ec
JM
4787}
4788
881c6935 4789static inline int
7080f735 4790AT_ref_external (dw_attr_ref a)
881c6935
JM
4791{
4792 if (a && AT_class (a) == dw_val_class_die_ref)
4793 return a->dw_attr_val.v.val_die_ref.external;
4794
4795 return 0;
4796}
4797
881c6935 4798static inline void
7080f735 4799set_AT_ref_external (dw_attr_ref a, int i)
881c6935
JM
4800{
4801 if (a && AT_class (a) == dw_val_class_die_ref)
4802 a->dw_attr_val.v.val_die_ref.external = i;
4803 else
4804 abort ();
4805}
4806
3f76745e 4807/* Add an FDE reference attribute value to a DIE. */
b1ccbc24 4808
3f76745e 4809static inline void
7080f735 4810add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
3f76745e 4811{
703ad42b 4812 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
b1ccbc24 4813
3f76745e
JM
4814 attr->dw_attr_next = NULL;
4815 attr->dw_attr = attr_kind;
4816 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4817 attr->dw_attr_val.v.val_fde_index = targ_fde;
4818 add_dwarf_attr (die, attr);
a3f97cbb 4819}
71dfc51f 4820
3f76745e 4821/* Add a location description attribute value to a DIE. */
71dfc51f 4822
3f76745e 4823static inline void
7080f735 4824add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
3f76745e 4825{
703ad42b 4826 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
71dfc51f 4827
3f76745e
JM
4828 attr->dw_attr_next = NULL;
4829 attr->dw_attr = attr_kind;
4830 attr->dw_attr_val.val_class = dw_val_class_loc;
4831 attr->dw_attr_val.v.val_loc = loc;
4832 add_dwarf_attr (die, attr);
a3f97cbb
JW
4833}
4834
a96c67ec 4835static inline dw_loc_descr_ref
7080f735 4836AT_loc (dw_attr_ref a)
a96c67ec
JM
4837{
4838 if (a && AT_class (a) == dw_val_class_loc)
4839 return a->dw_attr_val.v.val_loc;
4840
40e8cc95 4841 abort ();
a96c67ec
JM
4842}
4843
63e46568 4844static inline void
7080f735 4845add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
63e46568 4846{
703ad42b 4847 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
63e46568
DB
4848
4849 attr->dw_attr_next = NULL;
4850 attr->dw_attr = attr_kind;
4851 attr->dw_attr_val.val_class = dw_val_class_loc_list;
4852 attr->dw_attr_val.v.val_loc_list = loc_list;
4853 add_dwarf_attr (die, attr);
4854 have_location_lists = 1;
4855}
4856
63e46568 4857static inline dw_loc_list_ref
7080f735 4858AT_loc_list (dw_attr_ref a)
63e46568
DB
4859{
4860 if (a && AT_class (a) == dw_val_class_loc_list)
4861 return a->dw_attr_val.v.val_loc_list;
4862
4863 abort ();
4864}
4865
3f76745e 4866/* Add an address constant attribute value to a DIE. */
71dfc51f 4867
3f76745e 4868static inline void
7080f735 4869add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
a3f97cbb 4870{
703ad42b 4871 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
71dfc51f 4872
3f76745e
JM
4873 attr->dw_attr_next = NULL;
4874 attr->dw_attr = attr_kind;
4875 attr->dw_attr_val.val_class = dw_val_class_addr;
4876 attr->dw_attr_val.v.val_addr = addr;
4877 add_dwarf_attr (die, attr);
a3f97cbb
JW
4878}
4879
1865dbb5 4880static inline rtx
7080f735 4881AT_addr (dw_attr_ref a)
a96c67ec
JM
4882{
4883 if (a && AT_class (a) == dw_val_class_addr)
4884 return a->dw_attr_val.v.val_addr;
4885
40e8cc95 4886 abort ();
a96c67ec
JM
4887}
4888
3f76745e 4889/* Add a label identifier attribute value to a DIE. */
71dfc51f 4890
3f76745e 4891static inline void
7080f735 4892add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
a3f97cbb 4893{
703ad42b 4894 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
71dfc51f 4895
3f76745e
JM
4896 attr->dw_attr_next = NULL;
4897 attr->dw_attr = attr_kind;
4898 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4899 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4900 add_dwarf_attr (die, attr);
4901}
71dfc51f 4902
3f76745e
JM
4903/* Add a section offset attribute value to a DIE. */
4904
4905static inline void
7080f735 4906add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
3f76745e 4907{
703ad42b 4908 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
71dfc51f 4909
3f76745e
JM
4910 attr->dw_attr_next = NULL;
4911 attr->dw_attr = attr_kind;
8b790721 4912 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
a96c67ec 4913 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
3f76745e 4914 add_dwarf_attr (die, attr);
a3f97cbb
JW
4915}
4916
a20612aa
RH
4917/* Add an offset attribute value to a DIE. */
4918
2bee6045 4919static inline void
799f628a
JH
4920add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4921 unsigned HOST_WIDE_INT offset)
a20612aa 4922{
703ad42b 4923 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
a20612aa
RH
4924
4925 attr->dw_attr_next = NULL;
4926 attr->dw_attr = attr_kind;
4927 attr->dw_attr_val.val_class = dw_val_class_offset;
4928 attr->dw_attr_val.v.val_offset = offset;
4929 add_dwarf_attr (die, attr);
4930}
4931
2bee6045
JJ
4932/* Add an range_list attribute value to a DIE. */
4933
4934static void
7080f735
AJ
4935add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4936 long unsigned int offset)
2bee6045 4937{
703ad42b 4938 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
2bee6045
JJ
4939
4940 attr->dw_attr_next = NULL;
4941 attr->dw_attr = attr_kind;
4942 attr->dw_attr_val.val_class = dw_val_class_range_list;
4943 attr->dw_attr_val.v.val_offset = offset;
4944 add_dwarf_attr (die, attr);
4945}
4946
a96c67ec 4947static inline const char *
7080f735 4948AT_lbl (dw_attr_ref a)
a3f97cbb 4949{
a96c67ec
JM
4950 if (a && (AT_class (a) == dw_val_class_lbl_id
4951 || AT_class (a) == dw_val_class_lbl_offset))
4952 return a->dw_attr_val.v.val_lbl_id;
71dfc51f 4953
40e8cc95 4954 abort ();
a3f97cbb
JW
4955}
4956
3f76745e 4957/* Get the attribute of type attr_kind. */
71dfc51f 4958
965514bd 4959static dw_attr_ref
7080f735 4960get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
f37230f0 4961{
b3694847
SS
4962 dw_attr_ref a;
4963 dw_die_ref spec = NULL;
556273e0 4964
3f76745e
JM
4965 if (die != NULL)
4966 {
4967 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
2ad9852d
RK
4968 if (a->dw_attr == attr_kind)
4969 return a;
4970 else if (a->dw_attr == DW_AT_specification
4971 || a->dw_attr == DW_AT_abstract_origin)
4972 spec = AT_ref (a);
71dfc51f 4973
3f76745e
JM
4974 if (spec)
4975 return get_AT (spec, attr_kind);
4976 }
4977
4978 return NULL;
f37230f0
JM
4979}
4980
2ad9852d
RK
4981/* Return the "low pc" attribute value, typically associated with a subprogram
4982 DIE. Return null if the "low pc" attribute is either not present, or if it
4983 cannot be represented as an assembler label identifier. */
71dfc51f 4984
a96c67ec 4985static inline const char *
7080f735 4986get_AT_low_pc (dw_die_ref die)
7e23cb16 4987{
b3694847 4988 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
2ad9852d 4989
40e8cc95 4990 return a ? AT_lbl (a) : NULL;
7e23cb16
JM
4991}
4992
2ad9852d
RK
4993/* Return the "high pc" attribute value, typically associated with a subprogram
4994 DIE. Return null if the "high pc" attribute is either not present, or if it
4995 cannot be represented as an assembler label identifier. */
71dfc51f 4996
a96c67ec 4997static inline const char *
7080f735 4998get_AT_hi_pc (dw_die_ref die)
a3f97cbb 4999{
b3694847 5000 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
2ad9852d 5001
40e8cc95 5002 return a ? AT_lbl (a) : NULL;
3f76745e
JM
5003}
5004
5005/* Return the value of the string attribute designated by ATTR_KIND, or
5006 NULL if it is not present. */
71dfc51f 5007
a96c67ec 5008static inline const char *
7080f735 5009get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
3f76745e 5010{
b3694847 5011 dw_attr_ref a = get_AT (die, attr_kind);
2ad9852d 5012
40e8cc95 5013 return a ? AT_string (a) : NULL;
a3f97cbb
JW
5014}
5015
3f76745e
JM
5016/* Return the value of the flag attribute designated by ATTR_KIND, or -1
5017 if it is not present. */
71dfc51f 5018
3f76745e 5019static inline int
7080f735 5020get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
a3f97cbb 5021{
b3694847 5022 dw_attr_ref a = get_AT (die, attr_kind);
2ad9852d 5023
40e8cc95 5024 return a ? AT_flag (a) : 0;
a3f97cbb
JW
5025}
5026
3f76745e
JM
5027/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5028 if it is not present. */
71dfc51f 5029
3f76745e 5030static inline unsigned
7080f735 5031get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
a3f97cbb 5032{
b3694847 5033 dw_attr_ref a = get_AT (die, attr_kind);
2ad9852d 5034
40e8cc95 5035 return a ? AT_unsigned (a) : 0;
a96c67ec 5036}
71dfc51f 5037
a96c67ec 5038static inline dw_die_ref
7080f735 5039get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
a96c67ec 5040{
b3694847 5041 dw_attr_ref a = get_AT (die, attr_kind);
2ad9852d 5042
40e8cc95 5043 return a ? AT_ref (a) : NULL;
3f76745e 5044}
71dfc51f 5045
c3cdeef4
JB
5046/* Return TRUE if the language is C or C++. */
5047
5048static inline bool
7080f735 5049is_c_family (void)
3f76745e 5050{
c3cdeef4 5051 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
71dfc51f 5052
3f76745e
JM
5053 return (lang == DW_LANG_C || lang == DW_LANG_C89
5054 || lang == DW_LANG_C_plus_plus);
556273e0 5055}
71dfc51f 5056
c3cdeef4
JB
5057/* Return TRUE if the language is C++. */
5058
5059static inline bool
7080f735 5060is_cxx (void)
1d3d6b1e
JM
5061{
5062 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5063 == DW_LANG_C_plus_plus);
c26fbbca 5064}
1d3d6b1e 5065
c3cdeef4
JB
5066/* Return TRUE if the language is Fortran. */
5067
5068static inline bool
7080f735 5069is_fortran (void)
3f76745e 5070{
c3cdeef4 5071 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
71dfc51f 5072
6de9cd9a
DN
5073 return (lang == DW_LANG_Fortran77
5074 || lang == DW_LANG_Fortran90
5075 || lang == DW_LANG_Fortran95);
556273e0 5076}
71dfc51f 5077
c3cdeef4
JB
5078/* Return TRUE if the language is Java. */
5079
5080static inline bool
7080f735 5081is_java (void)
28985b81 5082{
c3cdeef4 5083 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
28985b81 5084
c3cdeef4
JB
5085 return lang == DW_LANG_Java;
5086}
5087
5088/* Return TRUE if the language is Ada. */
5089
5090static inline bool
7080f735 5091is_ada (void)
c3cdeef4
JB
5092{
5093 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7080f735 5094
c3cdeef4 5095 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
28985b81
AG
5096}
5097
10a11b75 5098/* Free up the memory used by A. */
71dfc51f 5099
7080f735 5100static inline void free_AT (dw_attr_ref);
3f76745e 5101static inline void
7080f735 5102free_AT (dw_attr_ref a)
10a11b75 5103{
17211ab5
GK
5104 if (AT_class (a) == dw_val_class_str)
5105 if (a->dw_attr_val.v.val_str->refcount)
5106 a->dw_attr_val.v.val_str->refcount--;
556273e0 5107}
10a11b75
JM
5108
5109/* Remove the specified attribute if present. */
5110
5111static void
7080f735 5112remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
3f76745e 5113{
b3694847
SS
5114 dw_attr_ref *p;
5115 dw_attr_ref removed = NULL;
a3f97cbb 5116
3f76745e
JM
5117 if (die != NULL)
5118 {
a96c67ec
JM
5119 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5120 if ((*p)->dw_attr == attr_kind)
5121 {
5122 removed = *p;
5123 *p = (*p)->dw_attr_next;
5124 break;
5125 }
71dfc51f 5126
a96c67ec 5127 if (removed != 0)
10a11b75
JM
5128 free_AT (removed);
5129 }
5130}
71dfc51f 5131
6097b0c3
DP
5132/* Remove child die whose die_tag is specified tag. */
5133
5134static void
5135remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5136{
5137 dw_die_ref current, prev, next;
5138 current = die->die_child;
5139 prev = NULL;
5140 while (current != NULL)
5141 {
5142 if (current->die_tag == tag)
5143 {
5144 next = current->die_sib;
5145 if (prev == NULL)
5146 die->die_child = next;
5147 else
5148 prev->die_sib = next;
5149 free_die (current);
5150 current = next;
5151 }
5152 else
5153 {
5154 prev = current;
5155 current = current->die_sib;
5156 }
5157 }
5158}
5159
10a11b75 5160/* Free up the memory used by DIE. */
71dfc51f 5161
10a11b75 5162static inline void
7080f735 5163free_die (dw_die_ref die)
10a11b75
JM
5164{
5165 remove_children (die);
3f76745e 5166}
71dfc51f 5167
3f76745e 5168/* Discard the children of this DIE. */
71dfc51f 5169
10a11b75 5170static void
7080f735 5171remove_children (dw_die_ref die)
3f76745e 5172{
b3694847 5173 dw_die_ref child_die = die->die_child;
3f76745e
JM
5174
5175 die->die_child = NULL;
3f76745e
JM
5176
5177 while (child_die != NULL)
a3f97cbb 5178 {
b3694847
SS
5179 dw_die_ref tmp_die = child_die;
5180 dw_attr_ref a;
71dfc51f 5181
3f76745e 5182 child_die = child_die->die_sib;
556273e0
KH
5183
5184 for (a = tmp_die->die_attr; a != NULL;)
a3f97cbb 5185 {
b3694847 5186 dw_attr_ref tmp_a = a;
71dfc51f 5187
3f76745e 5188 a = a->dw_attr_next;
10a11b75 5189 free_AT (tmp_a);
a3f97cbb 5190 }
71dfc51f 5191
10a11b75 5192 free_die (tmp_die);
3f76745e
JM
5193 }
5194}
71dfc51f 5195
a96c67ec 5196/* Add a child DIE below its parent. We build the lists up in reverse
881c6935 5197 addition order, and correct that in reverse_all_dies. */
71dfc51f 5198
3f76745e 5199static inline void
7080f735 5200add_child_die (dw_die_ref die, dw_die_ref child_die)
3f76745e
JM
5201{
5202 if (die != NULL && child_die != NULL)
e90b62db 5203 {
3a88cbd1
JL
5204 if (die == child_die)
5205 abort ();
2ad9852d 5206
3f76745e 5207 child_die->die_parent = die;
a96c67ec
JM
5208 child_die->die_sib = die->die_child;
5209 die->die_child = child_die;
3f76745e
JM
5210 }
5211}
5212
2081603c
JM
5213/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5214 is the specification, to the front of PARENT's list of children. */
10a11b75
JM
5215
5216static void
7080f735 5217splice_child_die (dw_die_ref parent, dw_die_ref child)
10a11b75
JM
5218{
5219 dw_die_ref *p;
5220
5221 /* We want the declaration DIE from inside the class, not the
5222 specification DIE at toplevel. */
5223 if (child->die_parent != parent)
2081603c
JM
5224 {
5225 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
2ad9852d 5226
2081603c
JM
5227 if (tmp)
5228 child = tmp;
5229 }
10a11b75 5230
2081603c
JM
5231 if (child->die_parent != parent
5232 && child->die_parent != get_AT_ref (parent, DW_AT_specification))
10a11b75
JM
5233 abort ();
5234
5de0e8d4 5235 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
10a11b75
JM
5236 if (*p == child)
5237 {
5238 *p = child->die_sib;
5239 break;
5240 }
5241
73c68f61 5242 child->die_parent = parent;
10a11b75
JM
5243 child->die_sib = parent->die_child;
5244 parent->die_child = child;
5245}
5246
3f76745e
JM
5247/* Return a pointer to a newly created DIE node. */
5248
5249static inline dw_die_ref
7080f735 5250new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
3f76745e 5251{
703ad42b 5252 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
3f76745e
JM
5253
5254 die->die_tag = tag_value;
3f76745e
JM
5255
5256 if (parent_die != NULL)
5257 add_child_die (parent_die, die);
5258 else
ef76d03b
JW
5259 {
5260 limbo_die_node *limbo_node;
5261
17211ab5 5262 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
ef76d03b 5263 limbo_node->die = die;
54ba1f0d 5264 limbo_node->created_for = t;
ef76d03b
JW
5265 limbo_node->next = limbo_die_list;
5266 limbo_die_list = limbo_node;
5267 }
71dfc51f 5268
3f76745e
JM
5269 return die;
5270}
71dfc51f 5271
3f76745e 5272/* Return the DIE associated with the given type specifier. */
71dfc51f 5273
3f76745e 5274static inline dw_die_ref
7080f735 5275lookup_type_die (tree type)
3f76745e 5276{
e2500fed 5277 return TYPE_SYMTAB_DIE (type);
3f76745e 5278}
e90b62db 5279
3f76745e 5280/* Equate a DIE to a given type specifier. */
71dfc51f 5281
10a11b75 5282static inline void
7080f735 5283equate_type_number_to_die (tree type, dw_die_ref type_die)
3f76745e 5284{
e2500fed 5285 TYPE_SYMTAB_DIE (type) = type_die;
3f76745e 5286}
71dfc51f 5287
636c7bc4
JZ
5288/* Returns a hash value for X (which really is a die_struct). */
5289
5290static hashval_t
5291decl_die_table_hash (const void *x)
5292{
5293 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5294}
5295
5296/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5297
5298static int
5299decl_die_table_eq (const void *x, const void *y)
5300{
5301 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5302}
5303
3f76745e 5304/* Return the DIE associated with a given declaration. */
71dfc51f 5305
3f76745e 5306static inline dw_die_ref
7080f735 5307lookup_decl_die (tree decl)
3f76745e 5308{
636c7bc4 5309 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
a3f97cbb
JW
5310}
5311
0a2d3d69
DB
5312/* Returns a hash value for X (which really is a var_loc_list). */
5313
5314static hashval_t
5315decl_loc_table_hash (const void *x)
5316{
5317 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5318}
5319
5320/* Return nonzero if decl_id of var_loc_list X is the same as
5321 UID of decl *Y. */
5322
5323static int
5324decl_loc_table_eq (const void *x, const void *y)
5325{
5326 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5327}
5328
5329/* Return the var_loc list associated with a given declaration. */
5330
5331static inline var_loc_list *
5332lookup_decl_loc (tree decl)
5333{
5334 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5335}
5336
3f76745e 5337/* Equate a DIE to a particular declaration. */
71dfc51f 5338
3f76745e 5339static void
7080f735 5340equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
a3f97cbb 5341{
aea9695c 5342 unsigned int decl_id = DECL_UID (decl);
636c7bc4 5343 void **slot;
3f76745e 5344
636c7bc4
JZ
5345 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5346 *slot = decl_die;
5347 decl_die->decl_id = decl_id;
a3f97cbb 5348}
0a2d3d69
DB
5349
5350/* Add a variable location node to the linked list for DECL. */
5351
5352static void
5353add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5354{
5355 unsigned int decl_id = DECL_UID (decl);
5356 var_loc_list *temp;
5357 void **slot;
5358
5359 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5360 if (*slot == NULL)
5361 {
5362 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5363 temp->decl_id = decl_id;
5364 *slot = temp;
5365 }
5366 else
5367 temp = *slot;
5368
5369 if (temp->last)
5370 {
5371 /* If the current location is the same as the end of the list,
5372 we have nothing to do. */
5373 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5374 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5375 {
5376 /* Add LOC to the end of list and update LAST. */
5377 temp->last->next = loc;
5378 temp->last = loc;
5379 }
5380 }
5381 /* Do not add empty location to the beginning of the list. */
5382 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5383 {
5384 temp->first = loc;
5385 temp->last = loc;
5386 }
5387}
3f76745e
JM
5388\f
5389/* Keep track of the number of spaces used to indent the
5390 output of the debugging routines that print the structure of
5391 the DIE internal representation. */
5392static int print_indent;
71dfc51f 5393
3f76745e
JM
5394/* Indent the line the number of spaces given by print_indent. */
5395
5396static inline void
7080f735 5397print_spaces (FILE *outfile)
3f76745e
JM
5398{
5399 fprintf (outfile, "%*s", print_indent, "");
a3f97cbb
JW
5400}
5401
956d6950 5402/* Print the information associated with a given DIE, and its children.
3f76745e 5403 This routine is a debugging aid only. */
71dfc51f 5404
a3f97cbb 5405static void
7080f735 5406print_die (dw_die_ref die, FILE *outfile)
a3f97cbb 5407{
b3694847
SS
5408 dw_attr_ref a;
5409 dw_die_ref c;
71dfc51f 5410
3f76745e 5411 print_spaces (outfile);
2d8b0f3a 5412 fprintf (outfile, "DIE %4lu: %s\n",
3f76745e
JM
5413 die->die_offset, dwarf_tag_name (die->die_tag));
5414 print_spaces (outfile);
2d8b0f3a
JL
5415 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5416 fprintf (outfile, " offset: %lu\n", die->die_offset);
3f76745e
JM
5417
5418 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
a3f97cbb 5419 {
3f76745e
JM
5420 print_spaces (outfile);
5421 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5422
a96c67ec 5423 switch (AT_class (a))
3f76745e
JM
5424 {
5425 case dw_val_class_addr:
5426 fprintf (outfile, "address");
5427 break;
a20612aa
RH
5428 case dw_val_class_offset:
5429 fprintf (outfile, "offset");
5430 break;
3f76745e
JM
5431 case dw_val_class_loc:
5432 fprintf (outfile, "location descriptor");
5433 break;
63e46568 5434 case dw_val_class_loc_list:
a20612aa
RH
5435 fprintf (outfile, "location list -> label:%s",
5436 AT_loc_list (a)->ll_symbol);
63e46568 5437 break;
2bee6045
JJ
5438 case dw_val_class_range_list:
5439 fprintf (outfile, "range list");
5440 break;
3f76745e 5441 case dw_val_class_const:
38f9cd4c 5442 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
3f76745e
JM
5443 break;
5444 case dw_val_class_unsigned_const:
38f9cd4c 5445 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
3f76745e
JM
5446 break;
5447 case dw_val_class_long_long:
2d8b0f3a 5448 fprintf (outfile, "constant (%lu,%lu)",
556273e0
KH
5449 a->dw_attr_val.v.val_long_long.hi,
5450 a->dw_attr_val.v.val_long_long.low);
3f76745e 5451 break;
e7ee3914
AM
5452 case dw_val_class_vec:
5453 fprintf (outfile, "floating-point or vector constant");
3f76745e
JM
5454 break;
5455 case dw_val_class_flag:
a96c67ec 5456 fprintf (outfile, "%u", AT_flag (a));
3f76745e
JM
5457 break;
5458 case dw_val_class_die_ref:
a96c67ec 5459 if (AT_ref (a) != NULL)
881c6935 5460 {
1bfb5f8f 5461 if (AT_ref (a)->die_symbol)
881c6935
JM
5462 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5463 else
5464 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5465 }
3f76745e
JM
5466 else
5467 fprintf (outfile, "die -> <null>");
5468 break;
5469 case dw_val_class_lbl_id:
8b790721 5470 case dw_val_class_lbl_offset:
a96c67ec 5471 fprintf (outfile, "label: %s", AT_lbl (a));
3f76745e 5472 break;
3f76745e 5473 case dw_val_class_str:
a96c67ec
JM
5474 if (AT_string (a) != NULL)
5475 fprintf (outfile, "\"%s\"", AT_string (a));
3f76745e
JM
5476 else
5477 fprintf (outfile, "<null>");
5478 break;
e9a25f70
JL
5479 default:
5480 break;
3f76745e
JM
5481 }
5482
5483 fprintf (outfile, "\n");
5484 }
5485
5486 if (die->die_child != NULL)
5487 {
5488 print_indent += 4;
5489 for (c = die->die_child; c != NULL; c = c->die_sib)
5490 print_die (c, outfile);
71dfc51f 5491
3f76745e 5492 print_indent -= 4;
a3f97cbb 5493 }
881c6935
JM
5494 if (print_indent == 0)
5495 fprintf (outfile, "\n");
a3f97cbb
JW
5496}
5497
3f76745e
JM
5498/* Print the contents of the source code line number correspondence table.
5499 This routine is a debugging aid only. */
71dfc51f 5500
3f76745e 5501static void
7080f735 5502print_dwarf_line_table (FILE *outfile)
a3f97cbb 5503{
b3694847
SS
5504 unsigned i;
5505 dw_line_info_ref line_info;
3f76745e
JM
5506
5507 fprintf (outfile, "\n\nDWARF source line information\n");
2ad9852d 5508 for (i = 1; i < line_info_table_in_use; i++)
a3f97cbb 5509 {
3f76745e
JM
5510 line_info = &line_info_table[i];
5511 fprintf (outfile, "%5d: ", i);
c4274b22
RH
5512 fprintf (outfile, "%-20s",
5513 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
2d8b0f3a 5514 fprintf (outfile, "%6ld", line_info->dw_line_num);
3f76745e 5515 fprintf (outfile, "\n");
a3f97cbb 5516 }
3f76745e
JM
5517
5518 fprintf (outfile, "\n\n");
f37230f0
JM
5519}
5520
3f76745e
JM
5521/* Print the information collected for a given DIE. */
5522
5523void
7080f735 5524debug_dwarf_die (dw_die_ref die)
3f76745e
JM
5525{
5526 print_die (die, stderr);
5527}
5528
5529/* Print all DWARF information collected for the compilation unit.
5530 This routine is a debugging aid only. */
5531
5532void
7080f735 5533debug_dwarf (void)
3f76745e
JM
5534{
5535 print_indent = 0;
5536 print_die (comp_unit_die, stderr);
b2244e22
JW
5537 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5538 print_dwarf_line_table (stderr);
3f76745e
JM
5539}
5540\f
a96c67ec
JM
5541/* We build up the lists of children and attributes by pushing new ones
5542 onto the beginning of the list. Reverse the lists for DIE so that
5543 they are in order of addition. */
71dfc51f 5544
f37230f0 5545static void
7080f735 5546reverse_die_lists (dw_die_ref die)
f37230f0 5547{
b3694847
SS
5548 dw_die_ref c, cp, cn;
5549 dw_attr_ref a, ap, an;
71dfc51f 5550
a96c67ec 5551 for (a = die->die_attr, ap = 0; a; a = an)
7d9d8943
AM
5552 {
5553 an = a->dw_attr_next;
5554 a->dw_attr_next = ap;
5555 ap = a;
a3f97cbb 5556 }
2ad9852d 5557
7d9d8943 5558 die->die_attr = ap;
3f76745e 5559
7d9d8943
AM
5560 for (c = die->die_child, cp = 0; c; c = cn)
5561 {
5562 cn = c->die_sib;
5563 c->die_sib = cp;
5564 cp = c;
5565 }
2ad9852d 5566
7d9d8943 5567 die->die_child = cp;
a3f97cbb
JW
5568}
5569
2ad9852d
RK
5570/* reverse_die_lists only reverses the single die you pass it. Since we used to
5571 reverse all dies in add_sibling_attributes, which runs through all the dies,
5572 it would reverse all the dies. Now, however, since we don't call
5573 reverse_die_lists in add_sibling_attributes, we need a routine to
5574 recursively reverse all the dies. This is that routine. */
71dfc51f 5575
7d9d8943 5576static void
7080f735 5577reverse_all_dies (dw_die_ref die)
a3f97cbb 5578{
b3694847 5579 dw_die_ref c;
71dfc51f 5580
7d9d8943 5581 reverse_die_lists (die);
3f76745e 5582
881c6935
JM
5583 for (c = die->die_child; c; c = c->die_sib)
5584 reverse_all_dies (c);
5585}
5586
2ad9852d
RK
5587/* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5588 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5589 DIE that marks the start of the DIEs for this include file. */
881c6935
JM
5590
5591static dw_die_ref
7080f735 5592push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
881c6935
JM
5593{
5594 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5595 dw_die_ref new_unit = gen_compile_unit_die (filename);
2ad9852d 5596
881c6935
JM
5597 new_unit->die_sib = old_unit;
5598 return new_unit;
5599}
5600
5601/* Close an include-file CU and reopen the enclosing one. */
5602
5603static dw_die_ref
7080f735 5604pop_compile_unit (dw_die_ref old_unit)
881c6935
JM
5605{
5606 dw_die_ref new_unit = old_unit->die_sib;
2ad9852d 5607
881c6935
JM
5608 old_unit->die_sib = NULL;
5609 return new_unit;
5610}
5611
2ad9852d
RK
5612#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5613#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
881c6935
JM
5614
5615/* Calculate the checksum of a location expression. */
5616
5617static inline void
7080f735 5618loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
881c6935 5619{
2ad9852d
RK
5620 CHECKSUM (loc->dw_loc_opc);
5621 CHECKSUM (loc->dw_loc_oprnd1);
5622 CHECKSUM (loc->dw_loc_oprnd2);
881c6935
JM
5623}
5624
5625/* Calculate the checksum of an attribute. */
5626
5627static void
7080f735 5628attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
881c6935
JM
5629{
5630 dw_loc_descr_ref loc;
5631 rtx r;
5632
2ad9852d 5633 CHECKSUM (at->dw_attr);
881c6935
JM
5634
5635 /* We don't care about differences in file numbering. */
5f632b5e
JM
5636 if (at->dw_attr == DW_AT_decl_file
5637 /* Or that this was compiled with a different compiler snapshot; if
5638 the output is the same, that's what matters. */
5639 || at->dw_attr == DW_AT_producer)
881c6935
JM
5640 return;
5641
5642 switch (AT_class (at))
5643 {
5644 case dw_val_class_const:
2ad9852d 5645 CHECKSUM (at->dw_attr_val.v.val_int);
881c6935
JM
5646 break;
5647 case dw_val_class_unsigned_const:
2ad9852d 5648 CHECKSUM (at->dw_attr_val.v.val_unsigned);
881c6935
JM
5649 break;
5650 case dw_val_class_long_long:
2ad9852d 5651 CHECKSUM (at->dw_attr_val.v.val_long_long);
881c6935 5652 break;
e7ee3914
AM
5653 case dw_val_class_vec:
5654 CHECKSUM (at->dw_attr_val.v.val_vec);
881c6935
JM
5655 break;
5656 case dw_val_class_flag:
2ad9852d 5657 CHECKSUM (at->dw_attr_val.v.val_flag);
881c6935 5658 break;
881c6935 5659 case dw_val_class_str:
2ad9852d 5660 CHECKSUM_STRING (AT_string (at));
881c6935 5661 break;
a20612aa 5662
881c6935
JM
5663 case dw_val_class_addr:
5664 r = AT_addr (at);
5665 switch (GET_CODE (r))
5666 {
5667 case SYMBOL_REF:
2ad9852d 5668 CHECKSUM_STRING (XSTR (r, 0));
881c6935
JM
5669 break;
5670
5671 default:
5672 abort ();
5673 }
5674 break;
5675
a20612aa 5676 case dw_val_class_offset:
2ad9852d 5677 CHECKSUM (at->dw_attr_val.v.val_offset);
a20612aa
RH
5678 break;
5679
881c6935
JM
5680 case dw_val_class_loc:
5681 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5682 loc_checksum (loc, ctx);
5683 break;
5684
5685 case dw_val_class_die_ref:
cc0017a9
ZD
5686 die_checksum (AT_ref (at), ctx, mark);
5687 break;
881c6935
JM
5688
5689 case dw_val_class_fde_ref:
5690 case dw_val_class_lbl_id:
5691 case dw_val_class_lbl_offset:
a20612aa 5692 break;
881c6935
JM
5693
5694 default:
5695 break;
5696 }
5697}
5698
5699/* Calculate the checksum of a DIE. */
5700
5701static void
7080f735 5702die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
881c6935
JM
5703{
5704 dw_die_ref c;
5705 dw_attr_ref a;
5706
cc0017a9
ZD
5707 /* To avoid infinite recursion. */
5708 if (die->die_mark)
5709 {
5710 CHECKSUM (die->die_mark);
5711 return;
5712 }
5713 die->die_mark = ++(*mark);
5714
2ad9852d 5715 CHECKSUM (die->die_tag);
881c6935
JM
5716
5717 for (a = die->die_attr; a; a = a->dw_attr_next)
cc0017a9 5718 attr_checksum (a, ctx, mark);
881c6935
JM
5719
5720 for (c = die->die_child; c; c = c->die_sib)
cc0017a9 5721 die_checksum (c, ctx, mark);
881c6935
JM
5722}
5723
2ad9852d
RK
5724#undef CHECKSUM
5725#undef CHECKSUM_STRING
881c6935 5726
cc0017a9
ZD
5727/* Do the location expressions look same? */
5728static inline int
7080f735 5729same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
cc0017a9
ZD
5730{
5731 return loc1->dw_loc_opc == loc2->dw_loc_opc
5732 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5733 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5734}
5735
5736/* Do the values look the same? */
5737static int
7080f735 5738same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
cc0017a9
ZD
5739{
5740 dw_loc_descr_ref loc1, loc2;
5741 rtx r1, r2;
cc0017a9
ZD
5742
5743 if (v1->val_class != v2->val_class)
5744 return 0;
5745
5746 switch (v1->val_class)
5747 {
5748 case dw_val_class_const:
5749 return v1->v.val_int == v2->v.val_int;
5750 case dw_val_class_unsigned_const:
5751 return v1->v.val_unsigned == v2->v.val_unsigned;
5752 case dw_val_class_long_long:
5753 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
73c68f61 5754 && v1->v.val_long_long.low == v2->v.val_long_long.low;
e7ee3914
AM
5755 case dw_val_class_vec:
5756 if (v1->v.val_vec.length != v2->v.val_vec.length
5757 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5758 return 0;
5759 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5760 v1->v.val_vec.length * v1->v.val_vec.elt_size))
cc0017a9 5761 return 0;
cc0017a9
ZD
5762 return 1;
5763 case dw_val_class_flag:
5764 return v1->v.val_flag == v2->v.val_flag;
5765 case dw_val_class_str:
17211ab5 5766 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
cc0017a9
ZD
5767
5768 case dw_val_class_addr:
5769 r1 = v1->v.val_addr;
5770 r2 = v2->v.val_addr;
5771 if (GET_CODE (r1) != GET_CODE (r2))
5772 return 0;
5773 switch (GET_CODE (r1))
5774 {
5775 case SYMBOL_REF:
5776 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5777
5778 default:
5779 abort ();
5780 }
5781
5782 case dw_val_class_offset:
5783 return v1->v.val_offset == v2->v.val_offset;
5784
5785 case dw_val_class_loc:
5786 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5787 loc1 && loc2;
5788 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5789 if (!same_loc_p (loc1, loc2, mark))
5790 return 0;
5791 return !loc1 && !loc2;
5792
5793 case dw_val_class_die_ref:
5794 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5795
5796 case dw_val_class_fde_ref:
5797 case dw_val_class_lbl_id:
5798 case dw_val_class_lbl_offset:
5799 return 1;
5800
5801 default:
5802 return 1;
5803 }
5804}
5805
5806/* Do the attributes look the same? */
5807
5808static int
7080f735 5809same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
cc0017a9
ZD
5810{
5811 if (at1->dw_attr != at2->dw_attr)
5812 return 0;
5813
5814 /* We don't care about differences in file numbering. */
5815 if (at1->dw_attr == DW_AT_decl_file
5816 /* Or that this was compiled with a different compiler snapshot; if
5817 the output is the same, that's what matters. */
5818 || at1->dw_attr == DW_AT_producer)
5819 return 1;
5820
5821 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5822}
5823
5824/* Do the dies look the same? */
5825
5826static int
7080f735 5827same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
cc0017a9
ZD
5828{
5829 dw_die_ref c1, c2;
5830 dw_attr_ref a1, a2;
5831
5832 /* To avoid infinite recursion. */
5833 if (die1->die_mark)
5834 return die1->die_mark == die2->die_mark;
5835 die1->die_mark = die2->die_mark = ++(*mark);
5836
5837 if (die1->die_tag != die2->die_tag)
5838 return 0;
5839
5840 for (a1 = die1->die_attr, a2 = die2->die_attr;
5841 a1 && a2;
5842 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5843 if (!same_attr_p (a1, a2, mark))
5844 return 0;
5845 if (a1 || a2)
5846 return 0;
5847
5848 for (c1 = die1->die_child, c2 = die2->die_child;
5849 c1 && c2;
5850 c1 = c1->die_sib, c2 = c2->die_sib)
5851 if (!same_die_p (c1, c2, mark))
5852 return 0;
5853 if (c1 || c2)
5854 return 0;
5855
5856 return 1;
5857}
5858
5859/* Do the dies look the same? Wrapper around same_die_p. */
5860
5861static int
7080f735 5862same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
cc0017a9
ZD
5863{
5864 int mark = 0;
5865 int ret = same_die_p (die1, die2, &mark);
5866
5867 unmark_all_dies (die1);
5868 unmark_all_dies (die2);
5869
5870 return ret;
5871}
5872
881c6935
JM
5873/* The prefix to attach to symbols on DIEs in the current comdat debug
5874 info section. */
5875static char *comdat_symbol_id;
5876
5877/* The index of the current symbol within the current comdat CU. */
5878static unsigned int comdat_symbol_number;
5879
5880/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5881 children, and set comdat_symbol_id accordingly. */
5882
5883static void
7080f735 5884compute_section_prefix (dw_die_ref unit_die)
881c6935 5885{
cc0017a9
ZD
5886 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5887 const char *base = die_name ? lbasename (die_name) : "anonymous";
703ad42b 5888 char *name = alloca (strlen (base) + 64);
f11c3043 5889 char *p;
cc0017a9 5890 int i, mark;
881c6935
JM
5891 unsigned char checksum[16];
5892 struct md5_ctx ctx;
5893
f11c3043
RK
5894 /* Compute the checksum of the DIE, then append part of it as hex digits to
5895 the name filename of the unit. */
5896
881c6935 5897 md5_init_ctx (&ctx);
cc0017a9
ZD
5898 mark = 0;
5899 die_checksum (unit_die, &ctx, &mark);
5900 unmark_all_dies (unit_die);
881c6935
JM
5901 md5_finish_ctx (&ctx, checksum);
5902
0023400b 5903 sprintf (name, "%s.", base);
881c6935
JM
5904 clean_symbol_name (name);
5905
2ad9852d
RK
5906 p = name + strlen (name);
5907 for (i = 0; i < 4; i++)
5908 {
5909 sprintf (p, "%.2x", checksum[i]);
5910 p += 2;
5911 }
881c6935
JM
5912
5913 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5914 comdat_symbol_number = 0;
5915}
5916
f11c3043 5917/* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
881c6935
JM
5918
5919static int
7080f735 5920is_type_die (dw_die_ref die)
881c6935
JM
5921{
5922 switch (die->die_tag)
5923 {
5924 case DW_TAG_array_type:
5925 case DW_TAG_class_type:
5926 case DW_TAG_enumeration_type:
5927 case DW_TAG_pointer_type:
5928 case DW_TAG_reference_type:
5929 case DW_TAG_string_type:
5930 case DW_TAG_structure_type:
5931 case DW_TAG_subroutine_type:
5932 case DW_TAG_union_type:
5933 case DW_TAG_ptr_to_member_type:
5934 case DW_TAG_set_type:
5935 case DW_TAG_subrange_type:
5936 case DW_TAG_base_type:
5937 case DW_TAG_const_type:
5938 case DW_TAG_file_type:
5939 case DW_TAG_packed_type:
5940 case DW_TAG_volatile_type:
cc0017a9 5941 case DW_TAG_typedef:
881c6935
JM
5942 return 1;
5943 default:
5944 return 0;
5945 }
5946}
5947
5948/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5949 Basically, we want to choose the bits that are likely to be shared between
5950 compilations (types) and leave out the bits that are specific to individual
5951 compilations (functions). */
5952
5953static int
7080f735 5954is_comdat_die (dw_die_ref c)
881c6935 5955{
2ad9852d
RK
5956 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5957 we do for stabs. The advantage is a greater likelihood of sharing between
5958 objects that don't include headers in the same order (and therefore would
5959 put the base types in a different comdat). jason 8/28/00 */
5960
881c6935
JM
5961 if (c->die_tag == DW_TAG_base_type)
5962 return 0;
5963
5964 if (c->die_tag == DW_TAG_pointer_type
5965 || c->die_tag == DW_TAG_reference_type
5966 || c->die_tag == DW_TAG_const_type
5967 || c->die_tag == DW_TAG_volatile_type)
5968 {
5969 dw_die_ref t = get_AT_ref (c, DW_AT_type);
2ad9852d 5970
881c6935
JM
5971 return t ? is_comdat_die (t) : 0;
5972 }
881c6935
JM
5973
5974 return is_type_die (c);
5975}
5976
5977/* Returns 1 iff C is the sort of DIE that might be referred to from another
5978 compilation unit. */
5979
5980static int
7080f735 5981is_symbol_die (dw_die_ref c)
881c6935 5982{
2ad9852d 5983 return (is_type_die (c)
c26fbbca 5984 || (get_AT (c, DW_AT_declaration)
2ad9852d 5985 && !get_AT (c, DW_AT_specification)));
881c6935
JM
5986}
5987
5988static char *
7080f735 5989gen_internal_sym (const char *prefix)
881c6935
JM
5990{
5991 char buf[256];
2ad9852d 5992
63e46568 5993 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
881c6935
JM
5994 return xstrdup (buf);
5995}
5996
5997/* Assign symbols to all worthy DIEs under DIE. */
5998
5999static void
7080f735 6000assign_symbol_names (dw_die_ref die)
881c6935 6001{
b3694847 6002 dw_die_ref c;
881c6935
JM
6003
6004 if (is_symbol_die (die))
6005 {
6006 if (comdat_symbol_id)
6007 {
6008 char *p = alloca (strlen (comdat_symbol_id) + 64);
2ad9852d 6009
881c6935
JM
6010 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6011 comdat_symbol_id, comdat_symbol_number++);
6012 die->die_symbol = xstrdup (p);
6013 }
6014 else
63e46568 6015 die->die_symbol = gen_internal_sym ("LDIE");
881c6935
JM
6016 }
6017
6018 for (c = die->die_child; c != NULL; c = c->die_sib)
6019 assign_symbol_names (c);
6020}
6021
cc0017a9
ZD
6022struct cu_hash_table_entry
6023{
6024 dw_die_ref cu;
6025 unsigned min_comdat_num, max_comdat_num;
6026 struct cu_hash_table_entry *next;
6027};
6028
6029/* Routines to manipulate hash table of CUs. */
6030static hashval_t
7080f735 6031htab_cu_hash (const void *of)
cc0017a9
ZD
6032{
6033 const struct cu_hash_table_entry *entry = of;
6034
6035 return htab_hash_string (entry->cu->die_symbol);
6036}
6037
6038static int
7080f735 6039htab_cu_eq (const void *of1, const void *of2)
cc0017a9
ZD
6040{
6041 const struct cu_hash_table_entry *entry1 = of1;
6042 const struct die_struct *entry2 = of2;
6043
6044 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6045}
6046
6047static void
7080f735 6048htab_cu_del (void *what)
cc0017a9
ZD
6049{
6050 struct cu_hash_table_entry *next, *entry = what;
6051
6052 while (entry)
6053 {
6054 next = entry->next;
6055 free (entry);
6056 entry = next;
6057 }
6058}
6059
6060/* Check whether we have already seen this CU and set up SYM_NUM
6061 accordingly. */
6062static int
7080f735 6063check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
cc0017a9
ZD
6064{
6065 struct cu_hash_table_entry dummy;
6066 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6067
6068 dummy.max_comdat_num = 0;
6069
6070 slot = (struct cu_hash_table_entry **)
6071 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6072 INSERT);
6073 entry = *slot;
6074
6075 for (; entry; last = entry, entry = entry->next)
6076 {
6077 if (same_die_p_wrap (cu, entry->cu))
6078 break;
6079 }
6080
6081 if (entry)
6082 {
6083 *sym_num = entry->min_comdat_num;
6084 return 1;
6085 }
6086
6087 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6088 entry->cu = cu;
6089 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6090 entry->next = *slot;
6091 *slot = entry;
6092
6093 return 0;
6094}
6095
6096/* Record SYM_NUM to record of CU in HTABLE. */
6097static void
7080f735 6098record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
cc0017a9
ZD
6099{
6100 struct cu_hash_table_entry **slot, *entry;
6101
6102 slot = (struct cu_hash_table_entry **)
6103 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6104 NO_INSERT);
6105 entry = *slot;
6106
6107 entry->max_comdat_num = sym_num;
6108}
6109
881c6935
JM
6110/* Traverse the DIE (which is always comp_unit_die), and set up
6111 additional compilation units for each of the include files we see
6112 bracketed by BINCL/EINCL. */
6113
6114static void
7080f735 6115break_out_includes (dw_die_ref die)
881c6935
JM
6116{
6117 dw_die_ref *ptr;
b3694847 6118 dw_die_ref unit = NULL;
cc0017a9
ZD
6119 limbo_die_node *node, **pnode;
6120 htab_t cu_hash_table;
881c6935 6121
c26fbbca 6122 for (ptr = &(die->die_child); *ptr;)
881c6935 6123 {
b3694847 6124 dw_die_ref c = *ptr;
881c6935 6125
2ad9852d 6126 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
881c6935
JM
6127 || (unit && is_comdat_die (c)))
6128 {
6129 /* This DIE is for a secondary CU; remove it from the main one. */
6130 *ptr = c->die_sib;
6131
6132 if (c->die_tag == DW_TAG_GNU_BINCL)
6133 {
6134 unit = push_new_compile_unit (unit, c);
6135 free_die (c);
6136 }
6137 else if (c->die_tag == DW_TAG_GNU_EINCL)
6138 {
6139 unit = pop_compile_unit (unit);
6140 free_die (c);
6141 }
6142 else
6143 add_child_die (unit, c);
6144 }
6145 else
6146 {
6147 /* Leave this DIE in the main CU. */
6148 ptr = &(c->die_sib);
6149 continue;
6150 }
6151 }
6152
6153#if 0
6154 /* We can only use this in debugging, since the frontend doesn't check
0b34cf1e 6155 to make sure that we leave every include file we enter. */
881c6935
JM
6156 if (unit != NULL)
6157 abort ();
6158#endif
6159
6160 assign_symbol_names (die);
cc0017a9
ZD
6161 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6162 for (node = limbo_die_list, pnode = &limbo_die_list;
6163 node;
6164 node = node->next)
881c6935 6165 {
cc0017a9
ZD
6166 int is_dupl;
6167
881c6935 6168 compute_section_prefix (node->die);
cc0017a9
ZD
6169 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6170 &comdat_symbol_number);
881c6935 6171 assign_symbol_names (node->die);
cc0017a9
ZD
6172 if (is_dupl)
6173 *pnode = node->next;
6174 else
73c68f61 6175 {
cc0017a9
ZD
6176 pnode = &node->next;
6177 record_comdat_symbol_number (node->die, cu_hash_table,
6178 comdat_symbol_number);
6179 }
881c6935 6180 }
cc0017a9 6181 htab_delete (cu_hash_table);
881c6935
JM
6182}
6183
6184/* Traverse the DIE and add a sibling attribute if it may have the
6185 effect of speeding up access to siblings. To save some space,
6186 avoid generating sibling attributes for DIE's without children. */
6187
6188static void
7080f735 6189add_sibling_attributes (dw_die_ref die)
881c6935 6190{
b3694847 6191 dw_die_ref c;
881c6935
JM
6192
6193 if (die->die_tag != DW_TAG_compile_unit
6194 && die->die_sib && die->die_child != NULL)
7d9d8943
AM
6195 /* Add the sibling link to the front of the attribute list. */
6196 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6197
6198 for (c = die->die_child; c != NULL; c = c->die_sib)
6199 add_sibling_attributes (c);
6200}
6201
2ad9852d
RK
6202/* Output all location lists for the DIE and its children. */
6203
63e46568 6204static void
7080f735 6205output_location_lists (dw_die_ref die)
63e46568
DB
6206{
6207 dw_die_ref c;
6208 dw_attr_ref d_attr;
2ad9852d 6209
63e46568 6210 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
2ad9852d
RK
6211 if (AT_class (d_attr) == dw_val_class_loc_list)
6212 output_loc_list (AT_loc_list (d_attr));
6213
63e46568
DB
6214 for (c = die->die_child; c != NULL; c = c->die_sib)
6215 output_location_lists (c);
6216
6217}
c26fbbca 6218
2ad9852d
RK
6219/* The format of each DIE (and its attribute value pairs) is encoded in an
6220 abbreviation table. This routine builds the abbreviation table and assigns
6221 a unique abbreviation id for each abbreviation entry. The children of each
6222 die are visited recursively. */
7d9d8943
AM
6223
6224static void
7080f735 6225build_abbrev_table (dw_die_ref die)
7d9d8943 6226{
b3694847
SS
6227 unsigned long abbrev_id;
6228 unsigned int n_alloc;
6229 dw_die_ref c;
6230 dw_attr_ref d_attr, a_attr;
881c6935
JM
6231
6232 /* Scan the DIE references, and mark as external any that refer to
1bfb5f8f 6233 DIEs from other CUs (i.e. those which are not marked). */
881c6935 6234 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
2ad9852d
RK
6235 if (AT_class (d_attr) == dw_val_class_die_ref
6236 && AT_ref (d_attr)->die_mark == 0)
6237 {
6238 if (AT_ref (d_attr)->die_symbol == 0)
6239 abort ();
6240
6241 set_AT_ref_external (d_attr, 1);
6242 }
881c6935 6243
7d9d8943
AM
6244 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6245 {
b3694847 6246 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7d9d8943
AM
6247
6248 if (abbrev->die_tag == die->die_tag)
6249 {
6250 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6251 {
6252 a_attr = abbrev->die_attr;
6253 d_attr = die->die_attr;
6254
6255 while (a_attr != NULL && d_attr != NULL)
6256 {
6257 if ((a_attr->dw_attr != d_attr->dw_attr)
6258 || (value_format (a_attr) != value_format (d_attr)))
6259 break;
6260
6261 a_attr = a_attr->dw_attr_next;
6262 d_attr = d_attr->dw_attr_next;
6263 }
6264
6265 if (a_attr == NULL && d_attr == NULL)
6266 break;
6267 }
6268 }
6269 }
6270
6271 if (abbrev_id >= abbrev_die_table_in_use)
6272 {
6273 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6274 {
6275 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
17211ab5
GK
6276 abbrev_die_table = ggc_realloc (abbrev_die_table,
6277 sizeof (dw_die_ref) * n_alloc);
7d9d8943 6278
703ad42b 6279 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7d9d8943
AM
6280 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6281 abbrev_die_table_allocated = n_alloc;
6282 }
6283
6284 ++abbrev_die_table_in_use;
6285 abbrev_die_table[abbrev_id] = die;
6286 }
6287
6288 die->die_abbrev = abbrev_id;
6289 for (c = die->die_child; c != NULL; c = c->die_sib)
6290 build_abbrev_table (c);
6291}
6292\f
3f76745e
JM
6293/* Return the power-of-two number of bytes necessary to represent VALUE. */
6294
6295static int
7080f735 6296constant_size (long unsigned int value)
3f76745e
JM
6297{
6298 int log;
6299
6300 if (value == 0)
6301 log = 0;
a3f97cbb 6302 else
3f76745e 6303 log = floor_log2 (value);
71dfc51f 6304
3f76745e
JM
6305 log = log / 8;
6306 log = 1 << (floor_log2 (log) + 1);
6307
6308 return log;
a3f97cbb
JW
6309}
6310
2ad9852d 6311/* Return the size of a DIE as it is represented in the
3f76745e 6312 .debug_info section. */
71dfc51f 6313
3f76745e 6314static unsigned long
7080f735 6315size_of_die (dw_die_ref die)
a3f97cbb 6316{
b3694847
SS
6317 unsigned long size = 0;
6318 dw_attr_ref a;
71dfc51f 6319
3f76745e 6320 size += size_of_uleb128 (die->die_abbrev);
a3f97cbb
JW
6321 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6322 {
a96c67ec 6323 switch (AT_class (a))
a3f97cbb
JW
6324 {
6325 case dw_val_class_addr:
a1a4189d 6326 size += DWARF2_ADDR_SIZE;
a3f97cbb 6327 break;
a20612aa
RH
6328 case dw_val_class_offset:
6329 size += DWARF_OFFSET_SIZE;
6330 break;
a3f97cbb 6331 case dw_val_class_loc:
3f76745e 6332 {
b3694847 6333 unsigned long lsize = size_of_locs (AT_loc (a));
71dfc51f 6334
3f76745e
JM
6335 /* Block length. */
6336 size += constant_size (lsize);
6337 size += lsize;
6338 }
a3f97cbb 6339 break;
63e46568
DB
6340 case dw_val_class_loc_list:
6341 size += DWARF_OFFSET_SIZE;
6342 break;
2bee6045
JJ
6343 case dw_val_class_range_list:
6344 size += DWARF_OFFSET_SIZE;
6345 break;
a3f97cbb 6346 case dw_val_class_const:
25dd13ec 6347 size += size_of_sleb128 (AT_int (a));
a3f97cbb
JW
6348 break;
6349 case dw_val_class_unsigned_const:
a96c67ec 6350 size += constant_size (AT_unsigned (a));
a3f97cbb 6351 break;
469ac993 6352 case dw_val_class_long_long:
2e4b9b8c 6353 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
469ac993 6354 break;
e7ee3914
AM
6355 case dw_val_class_vec:
6356 size += 1 + (a->dw_attr_val.v.val_vec.length
6357 * a->dw_attr_val.v.val_vec.elt_size); /* block */
a3f97cbb
JW
6358 break;
6359 case dw_val_class_flag:
3f76745e 6360 size += 1;
a3f97cbb
JW
6361 break;
6362 case dw_val_class_die_ref:
323658ea
ZD
6363 if (AT_ref_external (a))
6364 size += DWARF2_ADDR_SIZE;
6365 else
6366 size += DWARF_OFFSET_SIZE;
a3f97cbb
JW
6367 break;
6368 case dw_val_class_fde_ref:
3f76745e 6369 size += DWARF_OFFSET_SIZE;
a3f97cbb
JW
6370 break;
6371 case dw_val_class_lbl_id:
a1a4189d 6372 size += DWARF2_ADDR_SIZE;
3f76745e 6373 break;
8b790721 6374 case dw_val_class_lbl_offset:
3f76745e
JM
6375 size += DWARF_OFFSET_SIZE;
6376 break;
6377 case dw_val_class_str:
9eb4015a
JJ
6378 if (AT_string_form (a) == DW_FORM_strp)
6379 size += DWARF_OFFSET_SIZE;
6380 else
17211ab5 6381 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
3f76745e
JM
6382 break;
6383 default:
6384 abort ();
6385 }
a3f97cbb 6386 }
3f76745e
JM
6387
6388 return size;
a3f97cbb
JW
6389}
6390
2ad9852d
RK
6391/* Size the debugging information associated with a given DIE. Visits the
6392 DIE's children recursively. Updates the global variable next_die_offset, on
6393 each time through. Uses the current value of next_die_offset to update the
6394 die_offset field in each DIE. */
71dfc51f 6395
a3f97cbb 6396static void
7080f735 6397calc_die_sizes (dw_die_ref die)
a3f97cbb 6398{
b3694847 6399 dw_die_ref c;
2ad9852d 6400
3f76745e
JM
6401 die->die_offset = next_die_offset;
6402 next_die_offset += size_of_die (die);
71dfc51f 6403
3f76745e
JM
6404 for (c = die->die_child; c != NULL; c = c->die_sib)
6405 calc_die_sizes (c);
71dfc51f 6406
3f76745e
JM
6407 if (die->die_child != NULL)
6408 /* Count the null byte used to terminate sibling lists. */
6409 next_die_offset += 1;
a3f97cbb
JW
6410}
6411
1bfb5f8f 6412/* Set the marks for a die and its children. We do this so
881c6935 6413 that we know whether or not a reference needs to use FORM_ref_addr; only
1bfb5f8f
JM
6414 DIEs in the same CU will be marked. We used to clear out the offset
6415 and use that as the flag, but ran into ordering problems. */
881c6935
JM
6416
6417static void
7080f735 6418mark_dies (dw_die_ref die)
881c6935 6419{
b3694847 6420 dw_die_ref c;
2ad9852d 6421
cc0017a9
ZD
6422 if (die->die_mark)
6423 abort ();
7080f735 6424
1bfb5f8f
JM
6425 die->die_mark = 1;
6426 for (c = die->die_child; c; c = c->die_sib)
6427 mark_dies (c);
6428}
6429
6430/* Clear the marks for a die and its children. */
6431
6432static void
7080f735 6433unmark_dies (dw_die_ref die)
1bfb5f8f 6434{
b3694847 6435 dw_die_ref c;
2ad9852d 6436
cc0017a9
ZD
6437 if (!die->die_mark)
6438 abort ();
7080f735 6439
1bfb5f8f 6440 die->die_mark = 0;
881c6935 6441 for (c = die->die_child; c; c = c->die_sib)
1bfb5f8f 6442 unmark_dies (c);
881c6935
JM
6443}
6444
cc0017a9
ZD
6445/* Clear the marks for a die, its children and referred dies. */
6446
6447static void
7080f735 6448unmark_all_dies (dw_die_ref die)
cc0017a9
ZD
6449{
6450 dw_die_ref c;
6451 dw_attr_ref a;
6452
6453 if (!die->die_mark)
6454 return;
6455 die->die_mark = 0;
6456
6457 for (c = die->die_child; c; c = c->die_sib)
6458 unmark_all_dies (c);
6459
6460 for (a = die->die_attr; a; a = a->dw_attr_next)
6461 if (AT_class (a) == dw_val_class_die_ref)
6462 unmark_all_dies (AT_ref (a));
6463}
6464
3f76745e
JM
6465/* Return the size of the .debug_pubnames table generated for the
6466 compilation unit. */
a94dbf2c 6467
3f76745e 6468static unsigned long
7080f735 6469size_of_pubnames (void)
a94dbf2c 6470{
b3694847
SS
6471 unsigned long size;
6472 unsigned i;
469ac993 6473
3f76745e 6474 size = DWARF_PUBNAMES_HEADER_SIZE;
2ad9852d 6475 for (i = 0; i < pubname_table_in_use; i++)
a94dbf2c 6476 {
b3694847 6477 pubname_ref p = &pubname_table[i];
9eb4015a 6478 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
a94dbf2c
JM
6479 }
6480
3f76745e
JM
6481 size += DWARF_OFFSET_SIZE;
6482 return size;
a94dbf2c
JM
6483}
6484
956d6950 6485/* Return the size of the information in the .debug_aranges section. */
469ac993 6486
3f76745e 6487static unsigned long
7080f735 6488size_of_aranges (void)
469ac993 6489{
b3694847 6490 unsigned long size;
469ac993 6491
3f76745e 6492 size = DWARF_ARANGES_HEADER_SIZE;
469ac993 6493
3f76745e 6494 /* Count the address/length pair for this compilation unit. */
a1a4189d
JB
6495 size += 2 * DWARF2_ADDR_SIZE;
6496 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
469ac993 6497
3f76745e 6498 /* Count the two zero words used to terminated the address range table. */
a1a4189d 6499 size += 2 * DWARF2_ADDR_SIZE;
3f76745e
JM
6500 return size;
6501}
6502\f
6503/* Select the encoding of an attribute value. */
6504
6505static enum dwarf_form
7080f735 6506value_format (dw_attr_ref a)
3f76745e 6507{
a96c67ec 6508 switch (a->dw_attr_val.val_class)
469ac993 6509 {
3f76745e
JM
6510 case dw_val_class_addr:
6511 return DW_FORM_addr;
2bee6045 6512 case dw_val_class_range_list:
a20612aa
RH
6513 case dw_val_class_offset:
6514 if (DWARF_OFFSET_SIZE == 4)
6515 return DW_FORM_data4;
6516 if (DWARF_OFFSET_SIZE == 8)
6517 return DW_FORM_data8;
6518 abort ();
63e46568 6519 case dw_val_class_loc_list:
9d2f2c45
RH
6520 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6521 .debug_loc section */
6522 return DW_FORM_data4;
3f76745e 6523 case dw_val_class_loc:
a96c67ec 6524 switch (constant_size (size_of_locs (AT_loc (a))))
469ac993 6525 {
3f76745e
JM
6526 case 1:
6527 return DW_FORM_block1;
6528 case 2:
6529 return DW_FORM_block2;
469ac993
JM
6530 default:
6531 abort ();
6532 }
3f76745e 6533 case dw_val_class_const:
25dd13ec 6534 return DW_FORM_sdata;
3f76745e 6535 case dw_val_class_unsigned_const:
a96c67ec 6536 switch (constant_size (AT_unsigned (a)))
3f76745e
JM
6537 {
6538 case 1:
6539 return DW_FORM_data1;
6540 case 2:
6541 return DW_FORM_data2;
6542 case 4:
6543 return DW_FORM_data4;
6544 case 8:
6545 return DW_FORM_data8;
6546 default:
6547 abort ();
6548 }
6549 case dw_val_class_long_long:
6550 return DW_FORM_block1;
e7ee3914 6551 case dw_val_class_vec:
3f76745e
JM
6552 return DW_FORM_block1;
6553 case dw_val_class_flag:
6554 return DW_FORM_flag;
6555 case dw_val_class_die_ref:
881c6935
JM
6556 if (AT_ref_external (a))
6557 return DW_FORM_ref_addr;
6558 else
6559 return DW_FORM_ref;
3f76745e
JM
6560 case dw_val_class_fde_ref:
6561 return DW_FORM_data;
6562 case dw_val_class_lbl_id:
6563 return DW_FORM_addr;
8b790721 6564 case dw_val_class_lbl_offset:
3f76745e
JM
6565 return DW_FORM_data;
6566 case dw_val_class_str:
9eb4015a 6567 return AT_string_form (a);
a20612aa 6568
469ac993
JM
6569 default:
6570 abort ();
6571 }
a94dbf2c
JM
6572}
6573
3f76745e 6574/* Output the encoding of an attribute value. */
469ac993 6575
3f76745e 6576static void
7080f735 6577output_value_format (dw_attr_ref a)
a94dbf2c 6578{
a96c67ec 6579 enum dwarf_form form = value_format (a);
2ad9852d 6580
2e4b9b8c 6581 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
3f76745e 6582}
469ac993 6583
3f76745e
JM
6584/* Output the .debug_abbrev section which defines the DIE abbreviation
6585 table. */
469ac993 6586
3f76745e 6587static void
7080f735 6588output_abbrev_section (void)
3f76745e
JM
6589{
6590 unsigned long abbrev_id;
71dfc51f 6591
3f76745e 6592 dw_attr_ref a_attr;
2ad9852d 6593
3f76745e
JM
6594 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6595 {
b3694847 6596 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
71dfc51f 6597
2e4b9b8c 6598 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
2e4b9b8c
RH
6599 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6600 dwarf_tag_name (abbrev->die_tag));
71dfc51f 6601
2e4b9b8c
RH
6602 if (abbrev->die_child != NULL)
6603 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6604 else
6605 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
3f76745e
JM
6606
6607 for (a_attr = abbrev->die_attr; a_attr != NULL;
6608 a_attr = a_attr->dw_attr_next)
6609 {
2e4b9b8c
RH
6610 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6611 dwarf_attr_name (a_attr->dw_attr));
a96c67ec 6612 output_value_format (a_attr);
469ac993 6613 }
469ac993 6614
2e4b9b8c
RH
6615 dw2_asm_output_data (1, 0, NULL);
6616 dw2_asm_output_data (1, 0, NULL);
469ac993 6617 }
81f374eb
HPN
6618
6619 /* Terminate the table. */
2e4b9b8c 6620 dw2_asm_output_data (1, 0, NULL);
a94dbf2c
JM
6621}
6622
881c6935
JM
6623/* Output a symbol we can use to refer to this DIE from another CU. */
6624
6625static inline void
7080f735 6626output_die_symbol (dw_die_ref die)
881c6935
JM
6627{
6628 char *sym = die->die_symbol;
6629
6630 if (sym == 0)
6631 return;
6632
6633 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6634 /* We make these global, not weak; if the target doesn't support
6635 .linkonce, it doesn't support combining the sections, so debugging
6636 will break. */
5fd9b178 6637 targetm.asm_out.globalize_label (asm_out_file, sym);
2ad9852d 6638
881c6935
JM
6639 ASM_OUTPUT_LABEL (asm_out_file, sym);
6640}
6641
84a5b4f8 6642/* Return a new location list, given the begin and end range, and the
2ad9852d
RK
6643 expression. gensym tells us whether to generate a new internal symbol for
6644 this location list node, which is done for the head of the list only. */
6645
84a5b4f8 6646static inline dw_loc_list_ref
7080f735
AJ
6647new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6648 const char *section, unsigned int gensym)
84a5b4f8 6649{
17211ab5 6650 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
2ad9852d 6651
84a5b4f8
DB
6652 retlist->begin = begin;
6653 retlist->end = end;
6654 retlist->expr = expr;
6655 retlist->section = section;
c26fbbca 6656 if (gensym)
84a5b4f8 6657 retlist->ll_symbol = gen_internal_sym ("LLST");
2ad9852d 6658
84a5b4f8
DB
6659 return retlist;
6660}
6661
f9da5064 6662/* Add a location description expression to a location list. */
2ad9852d 6663
84a5b4f8 6664static inline void
7080f735
AJ
6665add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6666 const char *begin, const char *end,
6667 const char *section)
84a5b4f8 6668{
b3694847 6669 dw_loc_list_ref *d;
c26fbbca 6670
30f7a378 6671 /* Find the end of the chain. */
84a5b4f8
DB
6672 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6673 ;
2ad9852d 6674
f9da5064 6675 /* Add a new location list node to the list. */
84a5b4f8
DB
6676 *d = new_loc_list (descr, begin, end, section, 0);
6677}
6678
f9da5064 6679/* Output the location list given to us. */
2ad9852d 6680
63e46568 6681static void
7080f735 6682output_loc_list (dw_loc_list_ref list_head)
63e46568 6683{
2ad9852d
RK
6684 dw_loc_list_ref curr = list_head;
6685
63e46568 6686 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
a20612aa 6687
1711adc2 6688 /* Walk the location list, and output each range + expression. */
c26fbbca 6689 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
63e46568 6690 {
2bee6045 6691 unsigned long size;
1711adc2
DB
6692 if (separate_line_info_table_in_use == 0)
6693 {
6694 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6695 "Location list begin address (%s)",
6696 list_head->ll_symbol);
6697 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6698 "Location list end address (%s)",
6699 list_head->ll_symbol);
6700 }
6701 else
6702 {
6703 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6704 "Location list begin address (%s)",
6705 list_head->ll_symbol);
6706 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6707 "Location list end address (%s)",
6708 list_head->ll_symbol);
6709 }
63e46568 6710 size = size_of_locs (curr->expr);
c26fbbca 6711
63e46568 6712 /* Output the block length for this list of location operations. */
2bee6045
JJ
6713 if (size > 0xffff)
6714 abort ();
6715 dw2_asm_output_data (2, size, "%s", "Location expression size");
6716
63e46568
DB
6717 output_loc_sequence (curr->expr);
6718 }
2ad9852d 6719
1711adc2 6720 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
aafdcfcd
NS
6721 "Location list terminator begin (%s)",
6722 list_head->ll_symbol);
1711adc2 6723 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
aafdcfcd
NS
6724 "Location list terminator end (%s)",
6725 list_head->ll_symbol);
63e46568 6726}
9eb4015a 6727
3f76745e
JM
6728/* Output the DIE and its attributes. Called recursively to generate
6729 the definitions of each child DIE. */
71dfc51f 6730
a3f97cbb 6731static void
7080f735 6732output_die (dw_die_ref die)
a3f97cbb 6733{
b3694847
SS
6734 dw_attr_ref a;
6735 dw_die_ref c;
6736 unsigned long size;
a94dbf2c 6737
881c6935
JM
6738 /* If someone in another CU might refer to us, set up a symbol for
6739 them to point to. */
6740 if (die->die_symbol)
6741 output_die_symbol (die);
6742
2e4b9b8c
RH
6743 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6744 die->die_offset, dwarf_tag_name (die->die_tag));
a94dbf2c 6745
3f76745e 6746 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
a3f97cbb 6747 {
2e4b9b8c
RH
6748 const char *name = dwarf_attr_name (a->dw_attr);
6749
a96c67ec 6750 switch (AT_class (a))
3f76745e
JM
6751 {
6752 case dw_val_class_addr:
2e4b9b8c 6753 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
3f76745e 6754 break;
a3f97cbb 6755
a20612aa
RH
6756 case dw_val_class_offset:
6757 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6758 "%s", name);
6759 break;
6760
2bee6045
JJ
6761 case dw_val_class_range_list:
6762 {
6763 char *p = strchr (ranges_section_label, '\0');
6764
38f9cd4c
AJ
6765 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6766 a->dw_attr_val.v.val_offset);
2bee6045
JJ
6767 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6768 "%s", name);
6769 *p = '\0';
6770 }
6771 break;
6772
3f76745e 6773 case dw_val_class_loc:
a96c67ec 6774 size = size_of_locs (AT_loc (a));
71dfc51f 6775
3f76745e 6776 /* Output the block length for this list of location operations. */
2e4b9b8c 6777 dw2_asm_output_data (constant_size (size), size, "%s", name);
71dfc51f 6778
7d9d8943 6779 output_loc_sequence (AT_loc (a));
a3f97cbb 6780 break;
3f76745e
JM
6781
6782 case dw_val_class_const:
25dd13ec
JW
6783 /* ??? It would be slightly more efficient to use a scheme like is
6784 used for unsigned constants below, but gdb 4.x does not sign
6785 extend. Gdb 5.x does sign extend. */
2e4b9b8c 6786 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
a3f97cbb 6787 break;
3f76745e
JM
6788
6789 case dw_val_class_unsigned_const:
2e4b9b8c
RH
6790 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6791 AT_unsigned (a), "%s", name);
a3f97cbb 6792 break;
3f76745e
JM
6793
6794 case dw_val_class_long_long:
2e4b9b8c
RH
6795 {
6796 unsigned HOST_WIDE_INT first, second;
3f76745e 6797
2ad9852d
RK
6798 dw2_asm_output_data (1,
6799 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
c26fbbca 6800 "%s", name);
556273e0 6801
2e4b9b8c
RH
6802 if (WORDS_BIG_ENDIAN)
6803 {
6804 first = a->dw_attr_val.v.val_long_long.hi;
6805 second = a->dw_attr_val.v.val_long_long.low;
6806 }
6807 else
6808 {
6809 first = a->dw_attr_val.v.val_long_long.low;
6810 second = a->dw_attr_val.v.val_long_long.hi;
6811 }
2ad9852d
RK
6812
6813 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
2e4b9b8c 6814 first, "long long constant");
2ad9852d 6815 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
2e4b9b8c
RH
6816 second, NULL);
6817 }
a3f97cbb 6818 break;
3f76745e 6819
e7ee3914 6820 case dw_val_class_vec:
c84e2712 6821 {
e7ee3914
AM
6822 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6823 unsigned int len = a->dw_attr_val.v.val_vec.length;
b3694847 6824 unsigned int i;
e7ee3914 6825 unsigned char *p;
c84e2712 6826
e7ee3914
AM
6827 dw2_asm_output_data (1, len * elt_size, "%s", name);
6828 if (elt_size > sizeof (HOST_WIDE_INT))
6829 {
6830 elt_size /= 2;
6831 len *= 2;
6832 }
6833 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6834 i < len;
6835 i++, p += elt_size)
6836 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6837 "fp or vector constant word %u", i);
556273e0 6838 break;
c84e2712 6839 }
3f76745e
JM
6840
6841 case dw_val_class_flag:
2e4b9b8c 6842 dw2_asm_output_data (1, AT_flag (a), "%s", name);
a3f97cbb 6843 break;
a20612aa 6844
c26fbbca 6845 case dw_val_class_loc_list:
63e46568
DB
6846 {
6847 char *sym = AT_loc_list (a)->ll_symbol;
2ad9852d 6848
63e46568 6849 if (sym == 0)
173bf5be 6850 abort ();
1711adc2 6851 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
63e46568
DB
6852 }
6853 break;
a20612aa 6854
3f76745e 6855 case dw_val_class_die_ref:
881c6935 6856 if (AT_ref_external (a))
2e4b9b8c
RH
6857 {
6858 char *sym = AT_ref (a)->die_symbol;
2ad9852d 6859
2e4b9b8c
RH
6860 if (sym == 0)
6861 abort ();
6862 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6863 }
3f4907a6
JM
6864 else if (AT_ref (a)->die_offset == 0)
6865 abort ();
881c6935 6866 else
2e4b9b8c
RH
6867 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6868 "%s", name);
a3f97cbb 6869 break;
3f76745e
JM
6870
6871 case dw_val_class_fde_ref:
a6ab3aad
JM
6872 {
6873 char l1[20];
2ad9852d 6874
2e4b9b8c
RH
6875 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6876 a->dw_attr_val.v.val_fde_index * 2);
6877 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
a6ab3aad 6878 }
a3f97cbb 6879 break;
a3f97cbb 6880
3f76745e 6881 case dw_val_class_lbl_id:
8e7fa2c8 6882 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
3f76745e 6883 break;
71dfc51f 6884
8b790721 6885 case dw_val_class_lbl_offset:
2e4b9b8c 6886 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
3f76745e 6887 break;
a3f97cbb 6888
3f76745e 6889 case dw_val_class_str:
9eb4015a
JJ
6890 if (AT_string_form (a) == DW_FORM_strp)
6891 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6892 a->dw_attr_val.v.val_str->label,
a4cf1d85 6893 "%s: \"%s\"", name, AT_string (a));
9eb4015a
JJ
6894 else
6895 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
3f76745e 6896 break;
b2932ae5 6897
3f76745e
JM
6898 default:
6899 abort ();
6900 }
3f76745e 6901 }
71dfc51f 6902
3f76745e
JM
6903 for (c = die->die_child; c != NULL; c = c->die_sib)
6904 output_die (c);
71dfc51f 6905
2ad9852d 6906 /* Add null byte to terminate sibling list. */
3f76745e 6907 if (die->die_child != NULL)
2ad9852d
RK
6908 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6909 die->die_offset);
3f76745e 6910}
71dfc51f 6911
3f76745e
JM
6912/* Output the compilation unit that appears at the beginning of the
6913 .debug_info section, and precedes the DIE descriptions. */
71dfc51f 6914
3f76745e 6915static void
7080f735 6916output_compilation_unit_header (void)
3f76745e 6917{
9eb0ef7a
KB
6918 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
6919 dw2_asm_output_data (4, 0xffffffff,
6920 "Initial length escape value indicating 64-bit DWARF extension");
6921 dw2_asm_output_data (DWARF_OFFSET_SIZE,
6922 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
2e4b9b8c 6923 "Length of Compilation Unit Info");
2e4b9b8c 6924 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
2e4b9b8c
RH
6925 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6926 "Offset Into Abbrev. Section");
2e4b9b8c 6927 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
a3f97cbb
JW
6928}
6929
881c6935
JM
6930/* Output the compilation unit DIE and its children. */
6931
6932static void
7080f735 6933output_comp_unit (dw_die_ref die, int output_if_empty)
881c6935 6934{
ce1cc601 6935 const char *secname;
cc0017a9
ZD
6936 char *oldsym, *tmp;
6937
6938 /* Unless we are outputting main CU, we may throw away empty ones. */
6939 if (!output_if_empty && die->die_child == NULL)
6940 return;
881c6935 6941
2ad9852d
RK
6942 /* Even if there are no children of this DIE, we must output the information
6943 about the compilation unit. Otherwise, on an empty translation unit, we
6944 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
6945 will then complain when examining the file. First mark all the DIEs in
6946 this CU so we know which get local refs. */
1bfb5f8f
JM
6947 mark_dies (die);
6948
6949 build_abbrev_table (die);
6950
6d2f8887 6951 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
881c6935
JM
6952 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6953 calc_die_sizes (die);
6954
cc0017a9
ZD
6955 oldsym = die->die_symbol;
6956 if (oldsym)
881c6935 6957 {
703ad42b 6958 tmp = alloca (strlen (oldsym) + 24);
2ad9852d 6959
cc0017a9 6960 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
ce1cc601 6961 secname = tmp;
881c6935
JM
6962 die->die_symbol = NULL;
6963 }
6964 else
ce1cc601 6965 secname = (const char *) DEBUG_INFO_SECTION;
881c6935
JM
6966
6967 /* Output debugging information. */
715bdd29 6968 named_section_flags (secname, SECTION_DEBUG);
881c6935
JM
6969 output_compilation_unit_header ();
6970 output_die (die);
6971
1bfb5f8f
JM
6972 /* Leave the marks on the main CU, so we can check them in
6973 output_pubnames. */
cc0017a9
ZD
6974 if (oldsym)
6975 {
6976 unmark_dies (die);
6977 die->die_symbol = oldsym;
6978 }
881c6935
JM
6979}
6980
7afff7cf
NB
6981/* The DWARF2 pubname for a nested thingy looks like "A::f". The
6982 output of lang_hooks.decl_printable_name for C++ looks like
6983 "A::f(int)". Let's drop the argument list, and maybe the scope. */
a1d7ffe3 6984
d560ee52 6985static const char *
7080f735 6986dwarf2_name (tree decl, int scope)
a1d7ffe3 6987{
ae2bcd98 6988 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
a1d7ffe3
JM
6989}
6990
d291dd49 6991/* Add a new entry to .debug_pubnames if appropriate. */
71dfc51f 6992
d291dd49 6993static void
7080f735 6994add_pubname (tree decl, dw_die_ref die)
d291dd49
JM
6995{
6996 pubname_ref p;
6997
6998 if (! TREE_PUBLIC (decl))
6999 return;
7000
7001 if (pubname_table_in_use == pubname_table_allocated)
7002 {
7003 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
2ad9852d 7004 pubname_table
703ad42b
KG
7005 = ggc_realloc (pubname_table,
7006 (pubname_table_allocated * sizeof (pubname_entry)));
17211ab5
GK
7007 memset (pubname_table + pubname_table_in_use, 0,
7008 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
d291dd49 7009 }
71dfc51f 7010
d291dd49
JM
7011 p = &pubname_table[pubname_table_in_use++];
7012 p->die = die;
a1d7ffe3 7013 p->name = xstrdup (dwarf2_name (decl, 1));
d291dd49
JM
7014}
7015
a3f97cbb
JW
7016/* Output the public names table used to speed up access to externally
7017 visible names. For now, only generate entries for externally
7018 visible procedures. */
71dfc51f 7019
a3f97cbb 7020static void
7080f735 7021output_pubnames (void)
a3f97cbb 7022{
b3694847
SS
7023 unsigned i;
7024 unsigned long pubnames_length = size_of_pubnames ();
71dfc51f 7025
9eb0ef7a
KB
7026 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7027 dw2_asm_output_data (4, 0xffffffff,
7028 "Initial length escape value indicating 64-bit DWARF extension");
2e4b9b8c
RH
7029 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7030 "Length of Public Names Info");
2e4b9b8c 7031 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
2e4b9b8c
RH
7032 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7033 "Offset of Compilation Unit Info");
2e4b9b8c
RH
7034 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7035 "Compilation Unit Length");
71dfc51f 7036
2ad9852d 7037 for (i = 0; i < pubname_table_in_use; i++)
a3f97cbb 7038 {
b3694847 7039 pubname_ref pub = &pubname_table[i];
71dfc51f 7040
881c6935 7041 /* We shouldn't see pubnames for DIEs outside of the main CU. */
1bfb5f8f 7042 if (pub->die->die_mark == 0)
881c6935
JM
7043 abort ();
7044
2e4b9b8c
RH
7045 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7046 "DIE offset");
71dfc51f 7047
2e4b9b8c 7048 dw2_asm_output_nstring (pub->name, -1, "external name");
a3f97cbb 7049 }
71dfc51f 7050
2e4b9b8c 7051 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
a3f97cbb
JW
7052}
7053
d291dd49 7054/* Add a new entry to .debug_aranges if appropriate. */
71dfc51f 7055
d291dd49 7056static void
7080f735 7057add_arange (tree decl, dw_die_ref die)
d291dd49
JM
7058{
7059 if (! DECL_SECTION_NAME (decl))
7060 return;
7061
7062 if (arange_table_in_use == arange_table_allocated)
7063 {
7064 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7080f735
AJ
7065 arange_table = ggc_realloc (arange_table,
7066 (arange_table_allocated
17211ab5
GK
7067 * sizeof (dw_die_ref)));
7068 memset (arange_table + arange_table_in_use, 0,
7069 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
d291dd49 7070 }
71dfc51f 7071
d291dd49
JM
7072 arange_table[arange_table_in_use++] = die;
7073}
7074
a3f97cbb
JW
7075/* Output the information that goes into the .debug_aranges table.
7076 Namely, define the beginning and ending address range of the
7077 text section generated for this compilation unit. */
71dfc51f 7078
a3f97cbb 7079static void
7080f735 7080output_aranges (void)
a3f97cbb 7081{
b3694847
SS
7082 unsigned i;
7083 unsigned long aranges_length = size_of_aranges ();
71dfc51f 7084
9eb0ef7a
KB
7085 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7086 dw2_asm_output_data (4, 0xffffffff,
7087 "Initial length escape value indicating 64-bit DWARF extension");
2e4b9b8c
RH
7088 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7089 "Length of Address Ranges Info");
2e4b9b8c 7090 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
2e4b9b8c
RH
7091 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7092 "Offset of Compilation Unit Info");
2e4b9b8c 7093 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
2e4b9b8c 7094 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
71dfc51f 7095
262b6384
SC
7096 /* We need to align to twice the pointer size here. */
7097 if (DWARF_ARANGES_PAD_SIZE)
7098 {
2e4b9b8c 7099 /* Pad using a 2 byte words so that padding is correct for any
73c68f61 7100 pointer size. */
2e4b9b8c
RH
7101 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7102 2 * DWARF2_ADDR_SIZE);
770ca8c6 7103 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
2e4b9b8c 7104 dw2_asm_output_data (2, 0, NULL);
262b6384 7105 }
71dfc51f 7106
8e7fa2c8 7107 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
2e4b9b8c
RH
7108 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7109 text_section_label, "Length");
71dfc51f 7110
2ad9852d 7111 for (i = 0; i < arange_table_in_use; i++)
d291dd49 7112 {
e689ae67 7113 dw_die_ref die = arange_table[i];
71dfc51f 7114
881c6935 7115 /* We shouldn't see aranges for DIEs outside of the main CU. */
1bfb5f8f 7116 if (die->die_mark == 0)
881c6935
JM
7117 abort ();
7118
e689ae67 7119 if (die->die_tag == DW_TAG_subprogram)
2e4b9b8c 7120 {
8e7fa2c8 7121 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
173bf5be 7122 "Address");
2e4b9b8c
RH
7123 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7124 get_AT_low_pc (die), "Length");
7125 }
d291dd49 7126 else
a1d7ffe3 7127 {
e689ae67
JM
7128 /* A static variable; extract the symbol from DW_AT_location.
7129 Note that this code isn't currently hit, as we only emit
7130 aranges for functions (jason 9/23/99). */
e689ae67
JM
7131 dw_attr_ref a = get_AT (die, DW_AT_location);
7132 dw_loc_descr_ref loc;
2ad9852d 7133
a96c67ec 7134 if (! a || AT_class (a) != dw_val_class_loc)
e689ae67
JM
7135 abort ();
7136
a96c67ec 7137 loc = AT_loc (a);
e689ae67
JM
7138 if (loc->dw_loc_opc != DW_OP_addr)
7139 abort ();
7140
2e4b9b8c
RH
7141 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7142 loc->dw_loc_oprnd1.v.val_addr, "Address");
7143 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7144 get_AT_unsigned (die, DW_AT_byte_size),
7145 "Length");
a1d7ffe3 7146 }
d291dd49 7147 }
71dfc51f 7148
a3f97cbb 7149 /* Output the terminator words. */
2e4b9b8c
RH
7150 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7151 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
a3f97cbb
JW
7152}
7153
a20612aa
RH
7154/* Add a new entry to .debug_ranges. Return the offset at which it
7155 was placed. */
7156
7157static unsigned int
7080f735 7158add_ranges (tree block)
a20612aa
RH
7159{
7160 unsigned int in_use = ranges_table_in_use;
7161
7162 if (in_use == ranges_table_allocated)
7163 {
7164 ranges_table_allocated += RANGES_TABLE_INCREMENT;
703ad42b
KG
7165 ranges_table
7166 = ggc_realloc (ranges_table, (ranges_table_allocated
7167 * sizeof (struct dw_ranges_struct)));
17211ab5
GK
7168 memset (ranges_table + ranges_table_in_use, 0,
7169 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
a20612aa
RH
7170 }
7171
7172 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7173 ranges_table_in_use = in_use + 1;
7174
7175 return in_use * 2 * DWARF2_ADDR_SIZE;
7176}
7177
7178static void
7080f735 7179output_ranges (void)
a20612aa 7180{
b3694847 7181 unsigned i;
83182544 7182 static const char *const start_fmt = "Offset 0x%x";
a20612aa
RH
7183 const char *fmt = start_fmt;
7184
2ad9852d 7185 for (i = 0; i < ranges_table_in_use; i++)
a20612aa
RH
7186 {
7187 int block_num = ranges_table[i].block_num;
7188
7189 if (block_num)
7190 {
7191 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7192 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7193
7194 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7195 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7196
7197 /* If all code is in the text section, then the compilation
7198 unit base address defaults to DW_AT_low_pc, which is the
7199 base of the text section. */
7200 if (separate_line_info_table_in_use == 0)
7201 {
7202 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7203 text_section_label,
7204 fmt, i * 2 * DWARF2_ADDR_SIZE);
7205 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7206 text_section_label, NULL);
7207 }
2ad9852d 7208
a20612aa
RH
7209 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7210 compilation unit base address to zero, which allows us to
7211 use absolute addresses, and not worry about whether the
7212 target supports cross-section arithmetic. */
7213 else
7214 {
7215 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7216 fmt, i * 2 * DWARF2_ADDR_SIZE);
7217 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7218 }
7219
7220 fmt = NULL;
7221 }
7222 else
7223 {
7224 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7225 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7226 fmt = start_fmt;
7227 }
7228 }
7229}
0b34cf1e
UD
7230
7231/* Data structure containing information about input files. */
7232struct file_info
7233{
7234 char *path; /* Complete file name. */
7235 char *fname; /* File name part. */
7236 int length; /* Length of entire string. */
7237 int file_idx; /* Index in input file table. */
7238 int dir_idx; /* Index in directory table. */
7239};
7240
7241/* Data structure containing information about directories with source
7242 files. */
7243struct dir_info
7244{
7245 char *path; /* Path including directory name. */
7246 int length; /* Path length. */
7247 int prefix; /* Index of directory entry which is a prefix. */
0b34cf1e
UD
7248 int count; /* Number of files in this directory. */
7249 int dir_idx; /* Index of directory used as base. */
7250 int used; /* Used in the end? */
7251};
7252
7253/* Callback function for file_info comparison. We sort by looking at
7254 the directories in the path. */
356b0698 7255
0b34cf1e 7256static int
7080f735 7257file_info_cmp (const void *p1, const void *p2)
0b34cf1e
UD
7258{
7259 const struct file_info *s1 = p1;
7260 const struct file_info *s2 = p2;
7261 unsigned char *cp1;
7262 unsigned char *cp2;
7263
356b0698
RK
7264 /* Take care of file names without directories. We need to make sure that
7265 we return consistent values to qsort since some will get confused if
7266 we return the same value when identical operands are passed in opposite
7267 orders. So if neither has a directory, return 0 and otherwise return
7268 1 or -1 depending on which one has the directory. */
7269 if ((s1->path == s1->fname || s2->path == s2->fname))
7270 return (s2->path == s2->fname) - (s1->path == s1->fname);
0b34cf1e
UD
7271
7272 cp1 = (unsigned char *) s1->path;
7273 cp2 = (unsigned char *) s2->path;
7274
7275 while (1)
7276 {
7277 ++cp1;
7278 ++cp2;
356b0698
RK
7279 /* Reached the end of the first path? If so, handle like above. */
7280 if ((cp1 == (unsigned char *) s1->fname)
7281 || (cp2 == (unsigned char *) s2->fname))
7282 return ((cp2 == (unsigned char *) s2->fname)
7283 - (cp1 == (unsigned char *) s1->fname));
0b34cf1e
UD
7284
7285 /* Character of current path component the same? */
356b0698 7286 else if (*cp1 != *cp2)
0b34cf1e
UD
7287 return *cp1 - *cp2;
7288 }
7289}
7290
7291/* Output the directory table and the file name table. We try to minimize
7292 the total amount of memory needed. A heuristic is used to avoid large
7293 slowdowns with many input files. */
2ad9852d 7294
0b34cf1e 7295static void
7080f735 7296output_file_names (void)
0b34cf1e
UD
7297{
7298 struct file_info *files;
7299 struct dir_info *dirs;
7300 int *saved;
7301 int *savehere;
7302 int *backmap;
c4274b22 7303 size_t ndirs;
0b34cf1e 7304 int idx_offset;
c4274b22 7305 size_t i;
0b34cf1e
UD
7306 int idx;
7307
f0b886ab
UW
7308 /* Handle the case where file_table is empty. */
7309 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7310 {
7311 dw2_asm_output_data (1, 0, "End directory table");
7312 dw2_asm_output_data (1, 0, "End file name table");
7313 return;
7314 }
7315
0b34cf1e 7316 /* Allocate the various arrays we need. */
703ad42b
KG
7317 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7318 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
0b34cf1e
UD
7319
7320 /* Sort the file names. */
c4274b22 7321 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
0b34cf1e
UD
7322 {
7323 char *f;
7324
7325 /* Skip all leading "./". */
c4274b22 7326 f = VARRAY_CHAR_PTR (file_table, i);
0b34cf1e
UD
7327 while (f[0] == '.' && f[1] == '/')
7328 f += 2;
7329
7330 /* Create a new array entry. */
7331 files[i].path = f;
7332 files[i].length = strlen (f);
7333 files[i].file_idx = i;
7334
7335 /* Search for the file name part. */
7336 f = strrchr (f, '/');
7337 files[i].fname = f == NULL ? files[i].path : f + 1;
7338 }
2ad9852d 7339
c4274b22
RH
7340 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7341 sizeof (files[0]), file_info_cmp);
0b34cf1e
UD
7342
7343 /* Find all the different directories used. */
7344 dirs[0].path = files[1].path;
7345 dirs[0].length = files[1].fname - files[1].path;
7346 dirs[0].prefix = -1;
0b34cf1e
UD
7347 dirs[0].count = 1;
7348 dirs[0].dir_idx = 0;
7349 dirs[0].used = 0;
7350 files[1].dir_idx = 0;
7351 ndirs = 1;
7352
c4274b22 7353 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
0b34cf1e
UD
7354 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7355 && memcmp (dirs[ndirs - 1].path, files[i].path,
7356 dirs[ndirs - 1].length) == 0)
7357 {
7358 /* Same directory as last entry. */
7359 files[i].dir_idx = ndirs - 1;
0b34cf1e
UD
7360 ++dirs[ndirs - 1].count;
7361 }
7362 else
7363 {
c4274b22 7364 size_t j;
0b34cf1e
UD
7365
7366 /* This is a new directory. */
7367 dirs[ndirs].path = files[i].path;
7368 dirs[ndirs].length = files[i].fname - files[i].path;
0b34cf1e
UD
7369 dirs[ndirs].count = 1;
7370 dirs[ndirs].dir_idx = ndirs;
7371 dirs[ndirs].used = 0;
7372 files[i].dir_idx = ndirs;
7373
7374 /* Search for a prefix. */
981975b6 7375 dirs[ndirs].prefix = -1;
2ad9852d 7376 for (j = 0; j < ndirs; j++)
981975b6
RH
7377 if (dirs[j].length < dirs[ndirs].length
7378 && dirs[j].length > 1
7379 && (dirs[ndirs].prefix == -1
7380 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7381 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7382 dirs[ndirs].prefix = j;
0b34cf1e
UD
7383
7384 ++ndirs;
7385 }
7386
2ad9852d
RK
7387 /* Now to the actual work. We have to find a subset of the directories which
7388 allow expressing the file name using references to the directory table
7389 with the least amount of characters. We do not do an exhaustive search
7390 where we would have to check out every combination of every single
7391 possible prefix. Instead we use a heuristic which provides nearly optimal
7392 results in most cases and never is much off. */
703ad42b
KG
7393 saved = alloca (ndirs * sizeof (int));
7394 savehere = alloca (ndirs * sizeof (int));
0b34cf1e
UD
7395
7396 memset (saved, '\0', ndirs * sizeof (saved[0]));
2ad9852d 7397 for (i = 0; i < ndirs; i++)
0b34cf1e 7398 {
c4274b22 7399 size_t j;
0b34cf1e
UD
7400 int total;
7401
2ad9852d
RK
7402 /* We can always save some space for the current directory. But this
7403 does not mean it will be enough to justify adding the directory. */
0b34cf1e
UD
7404 savehere[i] = dirs[i].length;
7405 total = (savehere[i] - saved[i]) * dirs[i].count;
7406
2ad9852d 7407 for (j = i + 1; j < ndirs; j++)
0b34cf1e
UD
7408 {
7409 savehere[j] = 0;
0b34cf1e
UD
7410 if (saved[j] < dirs[i].length)
7411 {
7412 /* Determine whether the dirs[i] path is a prefix of the
7413 dirs[j] path. */
7414 int k;
7415
981975b6 7416 k = dirs[j].prefix;
c4274b22 7417 while (k != -1 && k != (int) i)
981975b6
RH
7418 k = dirs[k].prefix;
7419
c4274b22 7420 if (k == (int) i)
981975b6
RH
7421 {
7422 /* Yes it is. We can possibly safe some memory but
7423 writing the filenames in dirs[j] relative to
7424 dirs[i]. */
7425 savehere[j] = dirs[i].length;
7426 total += (savehere[j] - saved[j]) * dirs[j].count;
7427 }
0b34cf1e
UD
7428 }
7429 }
7430
7431 /* Check whether we can safe enough to justify adding the dirs[i]
7432 directory. */
7433 if (total > dirs[i].length + 1)
7434 {
981975b6 7435 /* It's worthwhile adding. */
c26fbbca 7436 for (j = i; j < ndirs; j++)
0b34cf1e
UD
7437 if (savehere[j] > 0)
7438 {
7439 /* Remember how much we saved for this directory so far. */
7440 saved[j] = savehere[j];
7441
7442 /* Remember the prefix directory. */
7443 dirs[j].dir_idx = i;
7444 }
7445 }
7446 }
7447
2ad9852d
RK
7448 /* We have to emit them in the order they appear in the file_table array
7449 since the index is used in the debug info generation. To do this
7450 efficiently we generate a back-mapping of the indices first. */
703ad42b 7451 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
c4274b22 7452 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
0b34cf1e
UD
7453 {
7454 backmap[files[i].file_idx] = i;
2ad9852d 7455
0b34cf1e
UD
7456 /* Mark this directory as used. */
7457 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7458 }
7459
2ad9852d
RK
7460 /* That was it. We are ready to emit the information. First emit the
7461 directory name table. We have to make sure the first actually emitted
7462 directory name has index one; zero is reserved for the current working
7463 directory. Make sure we do not confuse these indices with the one for the
7464 constructed table (even though most of the time they are identical). */
0b34cf1e 7465 idx = 1;
e57cabac 7466 idx_offset = dirs[0].length > 0 ? 1 : 0;
2ad9852d 7467 for (i = 1 - idx_offset; i < ndirs; i++)
0b34cf1e
UD
7468 if (dirs[i].used != 0)
7469 {
7470 dirs[i].used = idx++;
2e4b9b8c
RH
7471 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7472 "Directory Entry: 0x%x", dirs[i].used);
0b34cf1e 7473 }
2ad9852d 7474
2e4b9b8c
RH
7475 dw2_asm_output_data (1, 0, "End directory table");
7476
0b34cf1e
UD
7477 /* Correct the index for the current working directory entry if it
7478 exists. */
7479 if (idx_offset == 0)
7480 dirs[0].used = 0;
0b34cf1e
UD
7481
7482 /* Now write all the file names. */
c4274b22 7483 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
0b34cf1e
UD
7484 {
7485 int file_idx = backmap[i];
7486 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7487
2e4b9b8c 7488 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
94e001a9 7489 "File Entry: 0x%lx", (unsigned long) i);
0b34cf1e
UD
7490
7491 /* Include directory index. */
2e4b9b8c 7492 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
0b34cf1e
UD
7493
7494 /* Modification time. */
2e4b9b8c 7495 dw2_asm_output_data_uleb128 (0, NULL);
0b34cf1e
UD
7496
7497 /* File length in bytes. */
2e4b9b8c 7498 dw2_asm_output_data_uleb128 (0, NULL);
0b34cf1e 7499 }
2ad9852d 7500
2e4b9b8c 7501 dw2_asm_output_data (1, 0, "End file name table");
0b34cf1e
UD
7502}
7503
7504
a3f97cbb 7505/* Output the source line number correspondence information. This
14a774a9 7506 information goes into the .debug_line section. */
71dfc51f 7507
a3f97cbb 7508static void
7080f735 7509output_line_info (void)
a3f97cbb 7510{
981975b6 7511 char l1[20], l2[20], p1[20], p2[20];
a3f97cbb
JW
7512 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7513 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
b3694847
SS
7514 unsigned opc;
7515 unsigned n_op_args;
7516 unsigned long lt_index;
7517 unsigned long current_line;
7518 long line_offset;
7519 long line_delta;
7520 unsigned long current_file;
7521 unsigned long function;
71dfc51f 7522
2e4b9b8c
RH
7523 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7524 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
981975b6
RH
7525 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7526 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
71dfc51f 7527
9eb0ef7a
KB
7528 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7529 dw2_asm_output_data (4, 0xffffffff,
7530 "Initial length escape value indicating 64-bit DWARF extension");
2e4b9b8c
RH
7531 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7532 "Length of Source Line Info");
7533 ASM_OUTPUT_LABEL (asm_out_file, l1);
71dfc51f 7534
2e4b9b8c 7535 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
981975b6
RH
7536 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7537 ASM_OUTPUT_LABEL (asm_out_file, p1);
71dfc51f 7538
c1a046e5
TT
7539 /* Define the architecture-dependent minimum instruction length (in
7540 bytes). In this implementation of DWARF, this field is used for
7541 information purposes only. Since GCC generates assembly language,
7542 we have no a priori knowledge of how many instruction bytes are
7543 generated for each source line, and therefore can use only the
7544 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7545 commands. Accordingly, we fix this as `1', which is "correct
7546 enough" for all architectures, and don't let the target override. */
7547 dw2_asm_output_data (1, 1,
2e4b9b8c 7548 "Minimum Instruction Length");
c1a046e5 7549
2e4b9b8c
RH
7550 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7551 "Default is_stmt_start flag");
2e4b9b8c
RH
7552 dw2_asm_output_data (1, DWARF_LINE_BASE,
7553 "Line Base Value (Special Opcodes)");
2e4b9b8c
RH
7554 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7555 "Line Range Value (Special Opcodes)");
2e4b9b8c
RH
7556 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7557 "Special Opcode Base");
71dfc51f 7558
2ad9852d 7559 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
a3f97cbb
JW
7560 {
7561 switch (opc)
7562 {
7563 case DW_LNS_advance_pc:
7564 case DW_LNS_advance_line:
7565 case DW_LNS_set_file:
7566 case DW_LNS_set_column:
7567 case DW_LNS_fixed_advance_pc:
7568 n_op_args = 1;
7569 break;
7570 default:
7571 n_op_args = 0;
7572 break;
7573 }
2e4b9b8c
RH
7574
7575 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7576 opc, n_op_args);
a3f97cbb 7577 }
71dfc51f 7578
0b34cf1e
UD
7579 /* Write out the information about the files we use. */
7580 output_file_names ();
981975b6 7581 ASM_OUTPUT_LABEL (asm_out_file, p2);
a3f97cbb 7582
2f22d404
JM
7583 /* We used to set the address register to the first location in the text
7584 section here, but that didn't accomplish anything since we already
7585 have a line note for the opening brace of the first function. */
a3f97cbb
JW
7586
7587 /* Generate the line number to PC correspondence table, encoded as
7588 a series of state machine operations. */
7589 current_file = 1;
7590 current_line = 1;
8b790721 7591 strcpy (prev_line_label, text_section_label);
a3f97cbb
JW
7592 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7593 {
b3694847 7594 dw_line_info_ref line_info = &line_info_table[lt_index];
2f22d404 7595
10a11b75
JM
7596#if 0
7597 /* Disable this optimization for now; GDB wants to see two line notes
7598 at the beginning of a function so it can find the end of the
7599 prologue. */
7600
2f22d404 7601 /* Don't emit anything for redundant notes. Just updating the
73c68f61
SS
7602 address doesn't accomplish anything, because we already assume
7603 that anything after the last address is this line. */
2f22d404
JM
7604 if (line_info->dw_line_num == current_line
7605 && line_info->dw_file_num == current_file)
7606 continue;
10a11b75 7607#endif
71dfc51f 7608
2e4b9b8c
RH
7609 /* Emit debug info for the address of the current line.
7610
7611 Unfortunately, we have little choice here currently, and must always
2ad9852d 7612 use the most general form. GCC does not know the address delta
2e4b9b8c
RH
7613 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7614 attributes which will give an upper bound on the address range. We
7615 could perhaps use length attributes to determine when it is safe to
7616 use DW_LNS_fixed_advance_pc. */
7617
5c90448c 7618 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
f19a6894
JW
7619 if (0)
7620 {
7621 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
2e4b9b8c
RH
7622 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7623 "DW_LNS_fixed_advance_pc");
7624 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
f19a6894
JW
7625 }
7626 else
7627 {
a1a4189d 7628 /* This can handle any delta. This takes
73c68f61 7629 4+DWARF2_ADDR_SIZE bytes. */
2e4b9b8c
RH
7630 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7631 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7632 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 7633 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
f19a6894 7634 }
2ad9852d 7635
f19a6894
JW
7636 strcpy (prev_line_label, line_label);
7637
7638 /* Emit debug info for the source file of the current line, if
7639 different from the previous line. */
a3f97cbb
JW
7640 if (line_info->dw_file_num != current_file)
7641 {
7642 current_file = line_info->dw_file_num;
2e4b9b8c
RH
7643 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7644 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
c4274b22
RH
7645 VARRAY_CHAR_PTR (file_table,
7646 current_file));
a3f97cbb 7647 }
71dfc51f 7648
f19a6894
JW
7649 /* Emit debug info for the current line number, choosing the encoding
7650 that uses the least amount of space. */
2f22d404 7651 if (line_info->dw_line_num != current_line)
a3f97cbb 7652 {
2f22d404
JM
7653 line_offset = line_info->dw_line_num - current_line;
7654 line_delta = line_offset - DWARF_LINE_BASE;
7655 current_line = line_info->dw_line_num;
7656 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
2ad9852d
RK
7657 /* This can handle deltas from -10 to 234, using the current
7658 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7659 takes 1 byte. */
7660 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7661 "line %lu", current_line);
2f22d404
JM
7662 else
7663 {
7664 /* This can handle any delta. This takes at least 4 bytes,
7665 depending on the value being encoded. */
2e4b9b8c
RH
7666 dw2_asm_output_data (1, DW_LNS_advance_line,
7667 "advance to line %lu", current_line);
7668 dw2_asm_output_data_sleb128 (line_offset, NULL);
7669 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
2f22d404 7670 }
a94dbf2c
JM
7671 }
7672 else
2ad9852d
RK
7673 /* We still need to start a new row, so output a copy insn. */
7674 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
a3f97cbb
JW
7675 }
7676
f19a6894
JW
7677 /* Emit debug info for the address of the end of the function. */
7678 if (0)
7679 {
2e4b9b8c
RH
7680 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7681 "DW_LNS_fixed_advance_pc");
7682 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
f19a6894
JW
7683 }
7684 else
7685 {
2e4b9b8c
RH
7686 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7687 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7688 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 7689 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
f19a6894 7690 }
bdb669cb 7691
2e4b9b8c
RH
7692 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7693 dw2_asm_output_data_uleb128 (1, NULL);
7694 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
e90b62db
JM
7695
7696 function = 0;
7697 current_file = 1;
7698 current_line = 1;
556273e0 7699 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
e90b62db 7700 {
b3694847 7701 dw_separate_line_info_ref line_info
e90b62db 7702 = &separate_line_info_table[lt_index];
71dfc51f 7703
10a11b75 7704#if 0
2f22d404
JM
7705 /* Don't emit anything for redundant notes. */
7706 if (line_info->dw_line_num == current_line
7707 && line_info->dw_file_num == current_file
7708 && line_info->function == function)
7709 goto cont;
10a11b75 7710#endif
2f22d404 7711
f19a6894
JW
7712 /* Emit debug info for the address of the current line. If this is
7713 a new function, or the first line of a function, then we need
7714 to handle it differently. */
5c90448c
JM
7715 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7716 lt_index);
e90b62db
JM
7717 if (function != line_info->function)
7718 {
7719 function = line_info->function;
71dfc51f 7720
f9da5064 7721 /* Set the address register to the first line in the function. */
2e4b9b8c
RH
7722 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7723 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7724 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 7725 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
e90b62db
JM
7726 }
7727 else
7728 {
f19a6894
JW
7729 /* ??? See the DW_LNS_advance_pc comment above. */
7730 if (0)
7731 {
2e4b9b8c
RH
7732 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7733 "DW_LNS_fixed_advance_pc");
7734 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
f19a6894
JW
7735 }
7736 else
7737 {
2e4b9b8c
RH
7738 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7739 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7740 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 7741 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
f19a6894 7742 }
e90b62db 7743 }
2ad9852d 7744
f19a6894 7745 strcpy (prev_line_label, line_label);
71dfc51f 7746
f19a6894
JW
7747 /* Emit debug info for the source file of the current line, if
7748 different from the previous line. */
e90b62db
JM
7749 if (line_info->dw_file_num != current_file)
7750 {
7751 current_file = line_info->dw_file_num;
2e4b9b8c
RH
7752 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7753 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
c4274b22
RH
7754 VARRAY_CHAR_PTR (file_table,
7755 current_file));
e90b62db 7756 }
71dfc51f 7757
f19a6894
JW
7758 /* Emit debug info for the current line number, choosing the encoding
7759 that uses the least amount of space. */
e90b62db
JM
7760 if (line_info->dw_line_num != current_line)
7761 {
7762 line_offset = line_info->dw_line_num - current_line;
7763 line_delta = line_offset - DWARF_LINE_BASE;
7764 current_line = line_info->dw_line_num;
7765 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
2e4b9b8c
RH
7766 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7767 "line %lu", current_line);
e90b62db
JM
7768 else
7769 {
2e4b9b8c
RH
7770 dw2_asm_output_data (1, DW_LNS_advance_line,
7771 "advance to line %lu", current_line);
7772 dw2_asm_output_data_sleb128 (line_offset, NULL);
7773 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
e90b62db
JM
7774 }
7775 }
2f22d404 7776 else
2e4b9b8c 7777 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
71dfc51f 7778
10a11b75 7779#if 0
2f22d404 7780 cont:
10a11b75 7781#endif
2ad9852d
RK
7782
7783 lt_index++;
e90b62db
JM
7784
7785 /* If we're done with a function, end its sequence. */
7786 if (lt_index == separate_line_info_table_in_use
7787 || separate_line_info_table[lt_index].function != function)
7788 {
7789 current_file = 1;
7790 current_line = 1;
71dfc51f 7791
f19a6894 7792 /* Emit debug info for the address of the end of the function. */
5c90448c 7793 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
f19a6894
JW
7794 if (0)
7795 {
2e4b9b8c
RH
7796 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7797 "DW_LNS_fixed_advance_pc");
7798 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
f19a6894
JW
7799 }
7800 else
7801 {
2e4b9b8c
RH
7802 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7803 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7804 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8e7fa2c8 7805 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
f19a6894 7806 }
e90b62db
JM
7807
7808 /* Output the marker for the end of this sequence. */
2e4b9b8c
RH
7809 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7810 dw2_asm_output_data_uleb128 (1, NULL);
7811 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
e90b62db
JM
7812 }
7813 }
f19f17e0
JM
7814
7815 /* Output the marker for the end of the line number info. */
2e4b9b8c 7816 ASM_OUTPUT_LABEL (asm_out_file, l2);
a3f97cbb
JW
7817}
7818\f
a3f97cbb
JW
7819/* Given a pointer to a tree node for some base type, return a pointer to
7820 a DIE that describes the given type.
7821
7822 This routine must only be called for GCC type nodes that correspond to
7823 Dwarf base (fundamental) types. */
71dfc51f 7824
a3f97cbb 7825static dw_die_ref
7080f735 7826base_type_die (tree type)
a3f97cbb 7827{
b3694847
SS
7828 dw_die_ref base_type_result;
7829 const char *type_name;
7830 enum dwarf_type encoding;
7831 tree name = TYPE_NAME (type);
a3f97cbb 7832
2ad9852d 7833 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
a3f97cbb
JW
7834 return 0;
7835
405f63da
MM
7836 if (name)
7837 {
7838 if (TREE_CODE (name) == TYPE_DECL)
7839 name = DECL_NAME (name);
7840
7841 type_name = IDENTIFIER_POINTER (name);
7842 }
7843 else
7844 type_name = "__unknown__";
a9d38797 7845
a3f97cbb
JW
7846 switch (TREE_CODE (type))
7847 {
a3f97cbb 7848 case INTEGER_TYPE:
a9d38797 7849 /* Carefully distinguish the C character types, without messing
73c68f61
SS
7850 up if the language is not C. Note that we check only for the names
7851 that contain spaces; other names might occur by coincidence in other
7852 languages. */
a9d38797
JM
7853 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7854 && (type == char_type_node
7855 || ! strcmp (type_name, "signed char")
7856 || ! strcmp (type_name, "unsigned char"))))
a3f97cbb 7857 {
8df83eae 7858 if (TYPE_UNSIGNED (type))
a9d38797
JM
7859 encoding = DW_ATE_unsigned;
7860 else
7861 encoding = DW_ATE_signed;
7862 break;
a3f97cbb 7863 }
556273e0 7864 /* else fall through. */
a3f97cbb 7865
a9d38797
JM
7866 case CHAR_TYPE:
7867 /* GNU Pascal/Ada CHAR type. Not used in C. */
8df83eae 7868 if (TYPE_UNSIGNED (type))
a9d38797
JM
7869 encoding = DW_ATE_unsigned_char;
7870 else
7871 encoding = DW_ATE_signed_char;
a3f97cbb
JW
7872 break;
7873
7874 case REAL_TYPE:
a9d38797 7875 encoding = DW_ATE_float;
a3f97cbb
JW
7876 break;
7877
405f63da
MM
7878 /* Dwarf2 doesn't know anything about complex ints, so use
7879 a user defined type for it. */
a3f97cbb 7880 case COMPLEX_TYPE:
405f63da
MM
7881 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7882 encoding = DW_ATE_complex_float;
7883 else
7884 encoding = DW_ATE_lo_user;
a3f97cbb
JW
7885 break;
7886
7887 case BOOLEAN_TYPE:
a9d38797
JM
7888 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
7889 encoding = DW_ATE_boolean;
a3f97cbb
JW
7890 break;
7891
7892 default:
2ad9852d
RK
7893 /* No other TREE_CODEs are Dwarf fundamental types. */
7894 abort ();
a3f97cbb
JW
7895 }
7896
54ba1f0d 7897 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
14a774a9
RK
7898 if (demangle_name_func)
7899 type_name = (*demangle_name_func) (type_name);
7900
a9d38797
JM
7901 add_AT_string (base_type_result, DW_AT_name, type_name);
7902 add_AT_unsigned (base_type_result, DW_AT_byte_size,
4e5a8d7b 7903 int_size_in_bytes (type));
a9d38797 7904 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
a3f97cbb
JW
7905
7906 return base_type_result;
7907}
7908
7909/* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7910 the Dwarf "root" type for the given input type. The Dwarf "root" type of
7911 a given type is generally the same as the given type, except that if the
7912 given type is a pointer or reference type, then the root type of the given
7913 type is the root type of the "basis" type for the pointer or reference
7914 type. (This definition of the "root" type is recursive.) Also, the root
7915 type of a `const' qualified type or a `volatile' qualified type is the
7916 root type of the given type without the qualifiers. */
71dfc51f 7917
a3f97cbb 7918static tree
7080f735 7919root_type (tree type)
a3f97cbb
JW
7920{
7921 if (TREE_CODE (type) == ERROR_MARK)
7922 return error_mark_node;
7923
7924 switch (TREE_CODE (type))
7925 {
7926 case ERROR_MARK:
7927 return error_mark_node;
7928
7929 case POINTER_TYPE:
7930 case REFERENCE_TYPE:
7931 return type_main_variant (root_type (TREE_TYPE (type)));
7932
7933 default:
7934 return type_main_variant (type);
7935 }
7936}
7937
cc2902df 7938/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
a3f97cbb 7939 given input type is a Dwarf "fundamental" type. Otherwise return null. */
71dfc51f
RK
7940
7941static inline int
7080f735 7942is_base_type (tree type)
a3f97cbb
JW
7943{
7944 switch (TREE_CODE (type))
7945 {
7946 case ERROR_MARK:
7947 case VOID_TYPE:
7948 case INTEGER_TYPE:
7949 case REAL_TYPE:
7950 case COMPLEX_TYPE:
7951 case BOOLEAN_TYPE:
7952 case CHAR_TYPE:
7953 return 1;
7954
7955 case SET_TYPE:
7956 case ARRAY_TYPE:
7957 case RECORD_TYPE:
7958 case UNION_TYPE:
7959 case QUAL_UNION_TYPE:
7960 case ENUMERAL_TYPE:
7961 case FUNCTION_TYPE:
7962 case METHOD_TYPE:
7963 case POINTER_TYPE:
7964 case REFERENCE_TYPE:
7965 case FILE_TYPE:
7966 case OFFSET_TYPE:
7967 case LANG_TYPE:
604bb87d 7968 case VECTOR_TYPE:
a3f97cbb
JW
7969 return 0;
7970
7971 default:
7972 abort ();
7973 }
71dfc51f 7974
a3f97cbb
JW
7975 return 0;
7976}
7977
4977bab6
ZW
7978/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
7979 node, return the size in bits for the type if it is a constant, or else
7980 return the alignment for the type if the type's size is not constant, or
7981 else return BITS_PER_WORD if the type actually turns out to be an
7982 ERROR_MARK node. */
7983
7984static inline unsigned HOST_WIDE_INT
7080f735 7985simple_type_size_in_bits (tree type)
4977bab6 7986{
4977bab6
ZW
7987 if (TREE_CODE (type) == ERROR_MARK)
7988 return BITS_PER_WORD;
7989 else if (TYPE_SIZE (type) == NULL_TREE)
7990 return 0;
7991 else if (host_integerp (TYPE_SIZE (type), 1))
7992 return tree_low_cst (TYPE_SIZE (type), 1);
7993 else
7994 return TYPE_ALIGN (type);
7995}
7996
c3cdeef4
JB
7997/* Return true if the debug information for the given type should be
7998 emitted as a subrange type. */
7999
8000static inline bool
e7d23ce3
B
8001is_subrange_type (tree type)
8002{
de99511b
B
8003 tree subtype = TREE_TYPE (type);
8004
886de2d4
JB
8005 /* Subrange types are identified by the fact that they are integer
8006 types, and that they have a subtype which is either an integer type
8007 or an enumeral type. */
8008
8009 if (TREE_CODE (type) != INTEGER_TYPE
8010 || subtype == NULL_TREE)
8011 return false;
8012
8013 if (TREE_CODE (subtype) != INTEGER_TYPE
8014 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8015 return false;
8016
d6672e91
JB
8017 if (TREE_CODE (type) == TREE_CODE (subtype)
8018 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8019 && TYPE_MIN_VALUE (type) != NULL
8020 && TYPE_MIN_VALUE (subtype) != NULL
8021 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8022 && TYPE_MAX_VALUE (type) != NULL
8023 && TYPE_MAX_VALUE (subtype) != NULL
8024 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8025 {
8026 /* The type and its subtype have the same representation. If in
8027 addition the two types also have the same name, then the given
8028 type is not a subrange type, but rather a plain base type. */
8029 /* FIXME: brobecker/2004-03-22:
8030 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8031 therefore be sufficient to check the TYPE_SIZE node pointers
8032 rather than checking the actual size. Unfortunately, we have
8033 found some cases, such as in the Ada "integer" type, where
8034 this is not the case. Until this problem is solved, we need to
8035 keep checking the actual size. */
8036 tree type_name = TYPE_NAME (type);
8037 tree subtype_name = TYPE_NAME (subtype);
8038
8039 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8040 type_name = DECL_NAME (type_name);
8041
8042 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8043 subtype_name = DECL_NAME (subtype_name);
8044
8045 if (type_name == subtype_name)
8046 return false;
8047 }
8048
886de2d4 8049 return true;
c3cdeef4
JB
8050}
8051
8052/* Given a pointer to a tree node for a subrange type, return a pointer
8053 to a DIE that describes the given type. */
8054
8055static dw_die_ref
fbfd77b8 8056subrange_type_die (tree type, dw_die_ref context_die)
c3cdeef4
JB
8057{
8058 dw_die_ref subtype_die;
8059 dw_die_ref subrange_die;
8060 tree name = TYPE_NAME (type);
e7d23ce3 8061 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
6582c808 8062 tree subtype = TREE_TYPE (type);
7080f735 8063
fbfd77b8
JB
8064 if (context_die == NULL)
8065 context_die = comp_unit_die;
8066
6582c808
JB
8067 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8068 subtype_die = gen_enumeration_type_die (subtype, context_die);
de99511b 8069 else
6582c808 8070 subtype_die = base_type_die (subtype);
c3cdeef4 8071
fbfd77b8 8072 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
b98d154e
B
8073
8074 if (name != NULL)
8075 {
8076 if (TREE_CODE (name) == TYPE_DECL)
8077 name = DECL_NAME (name);
8078 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8079 }
8080
6582c808 8081 if (int_size_in_bytes (subtype) != size_in_bytes)
e7d23ce3
B
8082 {
8083 /* The size of the subrange type and its base type do not match,
8084 so we need to generate a size attribute for the subrange type. */
8085 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8086 }
8087
c3cdeef4
JB
8088 if (TYPE_MIN_VALUE (type) != NULL)
8089 add_bound_info (subrange_die, DW_AT_lower_bound,
8090 TYPE_MIN_VALUE (type));
8091 if (TYPE_MAX_VALUE (type) != NULL)
8092 add_bound_info (subrange_die, DW_AT_upper_bound,
8093 TYPE_MAX_VALUE (type));
8094 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8095
8096 return subrange_die;
8097}
8098
a3f97cbb
JW
8099/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8100 entry that chains various modifiers in front of the given type. */
71dfc51f 8101
a3f97cbb 8102static dw_die_ref
7080f735
AJ
8103modified_type_die (tree type, int is_const_type, int is_volatile_type,
8104 dw_die_ref context_die)
a3f97cbb 8105{
b3694847
SS
8106 enum tree_code code = TREE_CODE (type);
8107 dw_die_ref mod_type_die = NULL;
8108 dw_die_ref sub_die = NULL;
8109 tree item_type = NULL;
a3f97cbb
JW
8110
8111 if (code != ERROR_MARK)
8112 {
5101b304
MM
8113 tree qualified_type;
8114
8115 /* See if we already have the appropriately qualified variant of
8116 this type. */
c26fbbca 8117 qualified_type
5101b304
MM
8118 = get_qualified_type (type,
8119 ((is_const_type ? TYPE_QUAL_CONST : 0)
c26fbbca 8120 | (is_volatile_type
5101b304 8121 ? TYPE_QUAL_VOLATILE : 0)));
2ad9852d 8122
5101b304
MM
8123 /* If we do, then we can just use its DIE, if it exists. */
8124 if (qualified_type)
8125 {
8126 mod_type_die = lookup_type_die (qualified_type);
8127 if (mod_type_die)
8128 return mod_type_die;
8129 }
bdb669cb 8130
556273e0 8131 /* Handle C typedef types. */
c26fbbca 8132 if (qualified_type && TYPE_NAME (qualified_type)
5101b304
MM
8133 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8134 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
a94dbf2c 8135 {
5101b304
MM
8136 tree type_name = TYPE_NAME (qualified_type);
8137 tree dtype = TREE_TYPE (type_name);
2ad9852d 8138
5101b304 8139 if (qualified_type == dtype)
a94dbf2c
JM
8140 {
8141 /* For a named type, use the typedef. */
5101b304
MM
8142 gen_type_die (qualified_type, context_die);
8143 mod_type_die = lookup_type_die (qualified_type);
a94dbf2c
JM
8144 }
8145 else if (is_const_type < TYPE_READONLY (dtype)
8146 || is_volatile_type < TYPE_VOLATILE (dtype))
8147 /* cv-unqualified version of named type. Just use the unnamed
8148 type to which it refers. */
71dfc51f 8149 mod_type_die
5101b304 8150 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
71dfc51f
RK
8151 is_const_type, is_volatile_type,
8152 context_die);
2ad9852d 8153
71dfc51f 8154 /* Else cv-qualified version of named type; fall through. */
a94dbf2c
JM
8155 }
8156
8157 if (mod_type_die)
556273e0
KH
8158 /* OK. */
8159 ;
a94dbf2c 8160 else if (is_const_type)
a3f97cbb 8161 {
54ba1f0d 8162 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
a9d38797 8163 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
a3f97cbb
JW
8164 }
8165 else if (is_volatile_type)
8166 {
54ba1f0d 8167 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
a9d38797 8168 sub_die = modified_type_die (type, 0, 0, context_die);
a3f97cbb
JW
8169 }
8170 else if (code == POINTER_TYPE)
8171 {
54ba1f0d 8172 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
4977bab6
ZW
8173 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8174 simple_type_size_in_bits (type) / BITS_PER_UNIT);
61b32c02 8175#if 0
a3f97cbb 8176 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
61b32c02 8177#endif
a3f97cbb 8178 item_type = TREE_TYPE (type);
a3f97cbb
JW
8179 }
8180 else if (code == REFERENCE_TYPE)
8181 {
54ba1f0d 8182 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
4977bab6
ZW
8183 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8184 simple_type_size_in_bits (type) / BITS_PER_UNIT);
61b32c02 8185#if 0
a3f97cbb 8186 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
556273e0 8187#endif
a3f97cbb 8188 item_type = TREE_TYPE (type);
a3f97cbb 8189 }
e7d23ce3 8190 else if (is_subrange_type (type))
fbfd77b8 8191 mod_type_die = subrange_type_die (type, context_die);
a3f97cbb 8192 else if (is_base_type (type))
71dfc51f 8193 mod_type_die = base_type_die (type);
a3f97cbb
JW
8194 else
8195 {
4b674448
JM
8196 gen_type_die (type, context_die);
8197
a3f97cbb
JW
8198 /* We have to get the type_main_variant here (and pass that to the
8199 `lookup_type_die' routine) because the ..._TYPE node we have
8200 might simply be a *copy* of some original type node (where the
8201 copy was created to help us keep track of typedef names) and
8202 that copy might have a different TYPE_UID from the original
a94dbf2c 8203 ..._TYPE node. */
0e98f924
AH
8204 if (TREE_CODE (type) != VECTOR_TYPE)
8205 mod_type_die = lookup_type_die (type_main_variant (type));
8206 else
8207 /* Vectors have the debugging information in the type,
8208 not the main variant. */
8209 mod_type_die = lookup_type_die (type);
3a88cbd1
JL
8210 if (mod_type_die == NULL)
8211 abort ();
a3f97cbb 8212 }
3d2999ba
MK
8213
8214 /* We want to equate the qualified type to the die below. */
8370aa3a 8215 type = qualified_type;
a3f97cbb 8216 }
71dfc51f 8217
8370aa3a
RH
8218 if (type)
8219 equate_type_number_to_die (type, mod_type_die);
dfcf9891 8220 if (item_type)
71dfc51f
RK
8221 /* We must do this after the equate_type_number_to_die call, in case
8222 this is a recursive type. This ensures that the modified_type_die
8223 recursion will terminate even if the type is recursive. Recursive
8224 types are possible in Ada. */
8225 sub_die = modified_type_die (item_type,
8226 TYPE_READONLY (item_type),
8227 TYPE_VOLATILE (item_type),
8228 context_die);
8229
a3f97cbb 8230 if (sub_die != NULL)
71dfc51f
RK
8231 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8232
a3f97cbb
JW
8233 return mod_type_die;
8234}
8235
a3f97cbb 8236/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
6d2f8887 8237 an enumerated type. */
71dfc51f
RK
8238
8239static inline int
7080f735 8240type_is_enum (tree type)
a3f97cbb
JW
8241{
8242 return TREE_CODE (type) == ENUMERAL_TYPE;
8243}
8244
23959f19 8245/* Return the DBX register number described by a given RTL node. */
7d9d8943
AM
8246
8247static unsigned int
23959f19 8248dbx_reg_number (rtx rtl)
7d9d8943 8249{
b3694847 8250 unsigned regno = REGNO (rtl);
7d9d8943
AM
8251
8252 if (regno >= FIRST_PSEUDO_REGISTER)
e7af1d45 8253 abort ();
7d9d8943 8254
e7af1d45 8255 return DBX_REGISTER_NUMBER (regno);
7d9d8943
AM
8256}
8257
e7af1d45 8258/* Return a location descriptor that designates a machine register or
96714395 8259 zero if there is none. */
71dfc51f 8260
a3f97cbb 8261static dw_loc_descr_ref
7080f735 8262reg_loc_descriptor (rtx rtl)
a3f97cbb 8263{
d22c2324 8264 unsigned reg;
96714395 8265 rtx regs;
71dfc51f 8266
e7af1d45
RK
8267 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8268 return 0;
8269
23959f19 8270 reg = dbx_reg_number (rtl);
5fd9b178 8271 regs = targetm.dwarf_register_span (rtl);
96714395 8272
23959f19 8273 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
96714395
AH
8274 || regs)
8275 return multiple_reg_loc_descriptor (rtl, regs);
8276 else
8277 return one_reg_loc_descriptor (reg);
8278}
8279
8280/* Return a location descriptor that designates a machine register for
8281 a given hard register number. */
8282
8283static dw_loc_descr_ref
7080f735 8284one_reg_loc_descriptor (unsigned int regno)
96714395
AH
8285{
8286 if (regno <= 31)
8287 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
d22c2324 8288 else
96714395
AH
8289 return new_loc_descr (DW_OP_regx, regno, 0);
8290}
8291
8292/* Given an RTL of a register, return a location descriptor that
8293 designates a value that spans more than one register. */
8294
8295static dw_loc_descr_ref
7080f735 8296multiple_reg_loc_descriptor (rtx rtl, rtx regs)
96714395
AH
8297{
8298 int nregs, size, i;
8299 unsigned reg;
8300 dw_loc_descr_ref loc_result = NULL;
71dfc51f 8301
23959f19
JDA
8302 reg = dbx_reg_number (rtl);
8303 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
96714395
AH
8304
8305 /* Simple, contiguous registers. */
8306 if (regs == NULL_RTX)
8307 {
8308 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8309
8310 loc_result = NULL;
8311 while (nregs--)
8312 {
8313 dw_loc_descr_ref t;
8314
96714395
AH
8315 t = one_reg_loc_descriptor (reg);
8316 add_loc_descr (&loc_result, t);
8317 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
31ca3635 8318 ++reg;
96714395
AH
8319 }
8320 return loc_result;
8321 }
8322
8323 /* Now onto stupid register sets in non contiguous locations. */
8324
8325 if (GET_CODE (regs) != PARALLEL)
8326 abort ();
8327
8328 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8329 loc_result = NULL;
8330
8331 for (i = 0; i < XVECLEN (regs, 0); ++i)
8332 {
8333 dw_loc_descr_ref t;
8334
8335 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8336 add_loc_descr (&loc_result, t);
8337 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8338 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8339 }
a3f97cbb
JW
8340 return loc_result;
8341}
8342
d8041cc8
RH
8343/* Return a location descriptor that designates a constant. */
8344
8345static dw_loc_descr_ref
7080f735 8346int_loc_descriptor (HOST_WIDE_INT i)
d8041cc8
RH
8347{
8348 enum dwarf_location_atom op;
8349
8350 /* Pick the smallest representation of a constant, rather than just
8351 defaulting to the LEB encoding. */
8352 if (i >= 0)
8353 {
8354 if (i <= 31)
8355 op = DW_OP_lit0 + i;
8356 else if (i <= 0xff)
8357 op = DW_OP_const1u;
8358 else if (i <= 0xffff)
8359 op = DW_OP_const2u;
8360 else if (HOST_BITS_PER_WIDE_INT == 32
8361 || i <= 0xffffffff)
8362 op = DW_OP_const4u;
8363 else
8364 op = DW_OP_constu;
8365 }
8366 else
8367 {
8368 if (i >= -0x80)
8369 op = DW_OP_const1s;
8370 else if (i >= -0x8000)
8371 op = DW_OP_const2s;
8372 else if (HOST_BITS_PER_WIDE_INT == 32
8373 || i >= -0x80000000)
8374 op = DW_OP_const4s;
8375 else
8376 op = DW_OP_consts;
8377 }
8378
8379 return new_loc_descr (op, i, 0);
8380}
8381
a3f97cbb 8382/* Return a location descriptor that designates a base+offset location. */
71dfc51f 8383
a3f97cbb 8384static dw_loc_descr_ref
0a2d3d69 8385based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
a3f97cbb 8386{
b3694847 8387 dw_loc_descr_ref loc_result;
810429b7
JM
8388 /* For the "frame base", we use the frame pointer or stack pointer
8389 registers, since the RTL for local variables is relative to one of
8390 them. */
b3694847
SS
8391 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8392 ? HARD_FRAME_POINTER_REGNUM
8393 : STACK_POINTER_REGNUM);
71dfc51f 8394
0a2d3d69 8395 if (reg == fp_reg && can_use_fbreg)
71dfc51f 8396 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
85066503 8397 else if (reg <= 31)
71dfc51f 8398 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
a3f97cbb 8399 else
71dfc51f
RK
8400 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8401
a3f97cbb
JW
8402 return loc_result;
8403}
8404
8405/* Return true if this RTL expression describes a base+offset calculation. */
71dfc51f
RK
8406
8407static inline int
7080f735 8408is_based_loc (rtx rtl)
a3f97cbb 8409{
173bf5be
KH
8410 return (GET_CODE (rtl) == PLUS
8411 && ((GET_CODE (XEXP (rtl, 0)) == REG
8412 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8413 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
a3f97cbb
JW
8414}
8415
8416/* The following routine converts the RTL for a variable or parameter
8417 (resident in memory) into an equivalent Dwarf representation of a
8418 mechanism for getting the address of that same variable onto the top of a
8419 hypothetical "address evaluation" stack.
71dfc51f 8420
a3f97cbb
JW
8421 When creating memory location descriptors, we are effectively transforming
8422 the RTL for a memory-resident object into its Dwarf postfix expression
8423 equivalent. This routine recursively descends an RTL tree, turning
e60d4d7b
JL
8424 it into Dwarf postfix code as it goes.
8425
8426 MODE is the mode of the memory reference, needed to handle some
e7af1d45
RK
8427 autoincrement addressing modes.
8428
0a2d3d69
DB
8429 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8430 list for RTL. We can't use it when we are emitting location list for
8431 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8432 which describes how frame base changes when !frame_pointer_needed.
8433
e7af1d45 8434 Return 0 if we can't represent the location. */
71dfc51f 8435
a3f97cbb 8436static dw_loc_descr_ref
0a2d3d69 8437mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
a3f97cbb
JW
8438{
8439 dw_loc_descr_ref mem_loc_result = NULL;
40f0b3ee 8440 enum dwarf_location_atom op;
e7af1d45 8441
556273e0 8442 /* Note that for a dynamically sized array, the location we will generate a
a3f97cbb
JW
8443 description of here will be the lowest numbered location which is
8444 actually within the array. That's *not* necessarily the same as the
8445 zeroth element of the array. */
71dfc51f 8446
5fd9b178 8447 rtl = targetm.delegitimize_address (rtl);
1865dbb5 8448
a3f97cbb
JW
8449 switch (GET_CODE (rtl))
8450 {
e60d4d7b
JL
8451 case POST_INC:
8452 case POST_DEC:
e2134eea 8453 case POST_MODIFY:
e60d4d7b
JL
8454 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8455 just fall into the SUBREG code. */
8456
2ad9852d 8457 /* ... fall through ... */
e60d4d7b 8458
a3f97cbb
JW
8459 case SUBREG:
8460 /* The case of a subreg may arise when we have a local (register)
73c68f61
SS
8461 variable or a formal (register) parameter which doesn't quite fill
8462 up an entire register. For now, just assume that it is
8463 legitimate to make the Dwarf info refer to the whole register which
8464 contains the given subreg. */
ddef6bc7 8465 rtl = SUBREG_REG (rtl);
71dfc51f 8466
2ad9852d 8467 /* ... fall through ... */
a3f97cbb
JW
8468
8469 case REG:
8470 /* Whenever a register number forms a part of the description of the
73c68f61
SS
8471 method for calculating the (dynamic) address of a memory resident
8472 object, DWARF rules require the register number be referred to as
8473 a "base register". This distinction is not based in any way upon
8474 what category of register the hardware believes the given register
8475 belongs to. This is strictly DWARF terminology we're dealing with
8476 here. Note that in cases where the location of a memory-resident
8477 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8478 OP_CONST (0)) the actual DWARF location descriptor that we generate
8479 may just be OP_BASEREG (basereg). This may look deceptively like
8480 the object in question was allocated to a register (rather than in
8481 memory) so DWARF consumers need to be aware of the subtle
8482 distinction between OP_REG and OP_BASEREG. */
e7af1d45 8483 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
23959f19
JDA
8484 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8485 can_use_fbreg);
a3f97cbb
JW
8486 break;
8487
8488 case MEM:
0a2d3d69
DB
8489 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8490 can_use_fbreg);
e7af1d45
RK
8491 if (mem_loc_result != 0)
8492 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
a3f97cbb
JW
8493 break;
8494
1ce324c3
EB
8495 case LO_SUM:
8496 rtl = XEXP (rtl, 1);
8497
8498 /* ... fall through ... */
8499
d8041cc8
RH
8500 case LABEL_REF:
8501 /* Some ports can transform a symbol ref into a label ref, because
7080f735
AJ
8502 the symbol ref is too far away and has to be dumped into a constant
8503 pool. */
a3f97cbb
JW
8504 case CONST:
8505 case SYMBOL_REF:
6331d1c1 8506 /* Alternatively, the symbol in the constant pool might be referenced
c6f9b9a1 8507 by a different symbol. */
2ad9852d 8508 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
79cdfa4b 8509 {
149d6f9e
JJ
8510 bool marked;
8511 rtx tmp = get_pool_constant_mark (rtl, &marked);
2ad9852d 8512
6331d1c1 8513 if (GET_CODE (tmp) == SYMBOL_REF)
149d6f9e
JJ
8514 {
8515 rtl = tmp;
8516 if (CONSTANT_POOL_ADDRESS_P (tmp))
8517 get_pool_constant_mark (tmp, &marked);
8518 else
8519 marked = true;
8520 }
8521
8522 /* If all references to this pool constant were optimized away,
8523 it was not output and thus we can't represent it.
8524 FIXME: might try to use DW_OP_const_value here, though
8525 DW_OP_piece complicates it. */
8526 if (!marked)
8527 return 0;
79cdfa4b
TM
8528 }
8529
a3f97cbb
JW
8530 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8531 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
c470afad
RK
8532 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8533 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
a3f97cbb
JW
8534 break;
8535
e2134eea
JH
8536 case PRE_MODIFY:
8537 /* Extract the PLUS expression nested inside and fall into
73c68f61 8538 PLUS code below. */
e2134eea
JH
8539 rtl = XEXP (rtl, 1);
8540 goto plus;
8541
e60d4d7b
JL
8542 case PRE_INC:
8543 case PRE_DEC:
8544 /* Turn these into a PLUS expression and fall into the PLUS code
8545 below. */
8546 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8547 GEN_INT (GET_CODE (rtl) == PRE_INC
556273e0
KH
8548 ? GET_MODE_UNIT_SIZE (mode)
8549 : -GET_MODE_UNIT_SIZE (mode)));
8550
2ad9852d 8551 /* ... fall through ... */
e60d4d7b 8552
a3f97cbb 8553 case PLUS:
e2134eea 8554 plus:
a3f97cbb 8555 if (is_based_loc (rtl))
23959f19 8556 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
0a2d3d69
DB
8557 INTVAL (XEXP (rtl, 1)),
8558 can_use_fbreg);
a3f97cbb
JW
8559 else
8560 {
0a2d3d69
DB
8561 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8562 can_use_fbreg);
e7af1d45
RK
8563 if (mem_loc_result == 0)
8564 break;
d8041cc8
RH
8565
8566 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8567 && INTVAL (XEXP (rtl, 1)) >= 0)
e7af1d45
RK
8568 add_loc_descr (&mem_loc_result,
8569 new_loc_descr (DW_OP_plus_uconst,
8570 INTVAL (XEXP (rtl, 1)), 0));
d8041cc8
RH
8571 else
8572 {
8573 add_loc_descr (&mem_loc_result,
0a2d3d69
DB
8574 mem_loc_descriptor (XEXP (rtl, 1), mode,
8575 can_use_fbreg));
d8041cc8
RH
8576 add_loc_descr (&mem_loc_result,
8577 new_loc_descr (DW_OP_plus, 0, 0));
8578 }
a3f97cbb
JW
8579 }
8580 break;
8581
40f0b3ee
PB
8582 /* If a pseudo-reg is optimized away, it is possible for it to
8583 be replaced with a MEM containing a multiply or shift. */
dd2478ae 8584 case MULT:
40f0b3ee
PB
8585 op = DW_OP_mul;
8586 goto do_binop;
8587
8588 case ASHIFT:
8589 op = DW_OP_shl;
8590 goto do_binop;
8591
8592 case ASHIFTRT:
8593 op = DW_OP_shra;
8594 goto do_binop;
8595
8596 case LSHIFTRT:
8597 op = DW_OP_shr;
8598 goto do_binop;
8599
8600 do_binop:
e7af1d45 8601 {
0a2d3d69
DB
8602 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8603 can_use_fbreg);
8604 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8605 can_use_fbreg);
e7af1d45
RK
8606
8607 if (op0 == 0 || op1 == 0)
8608 break;
8609
8610 mem_loc_result = op0;
8611 add_loc_descr (&mem_loc_result, op1);
40f0b3ee 8612 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
e7af1d45
RK
8613 break;
8614 }
dd2478ae 8615
a3f97cbb 8616 case CONST_INT:
d8041cc8 8617 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
a3f97cbb
JW
8618 break;
8619
a9e8a5ee
RK
8620 case ADDRESSOF:
8621 /* If this is a MEM, return its address. Otherwise, we can't
8622 represent this. */
8623 if (GET_CODE (XEXP (rtl, 0)) == MEM)
0a2d3d69
DB
8624 return mem_loc_descriptor (XEXP (XEXP (rtl, 0), 0), mode,
8625 can_use_fbreg);
a9e8a5ee
RK
8626 else
8627 return 0;
8628
a3f97cbb
JW
8629 default:
8630 abort ();
8631 }
71dfc51f 8632
a3f97cbb
JW
8633 return mem_loc_result;
8634}
8635
956d6950 8636/* Return a descriptor that describes the concatenation of two locations.
4401bf24
JL
8637 This is typically a complex variable. */
8638
8639static dw_loc_descr_ref
7080f735 8640concat_loc_descriptor (rtx x0, rtx x1)
4401bf24
JL
8641{
8642 dw_loc_descr_ref cc_loc_result = NULL;
0a2d3d69
DB
8643 dw_loc_descr_ref x0_ref = loc_descriptor (x0, true);
8644 dw_loc_descr_ref x1_ref = loc_descriptor (x1, true);
4401bf24 8645
e7af1d45
RK
8646 if (x0_ref == 0 || x1_ref == 0)
8647 return 0;
8648
8649 cc_loc_result = x0_ref;
4401bf24 8650 add_loc_descr (&cc_loc_result,
e7af1d45
RK
8651 new_loc_descr (DW_OP_piece,
8652 GET_MODE_SIZE (GET_MODE (x0)), 0));
4401bf24 8653
e7af1d45 8654 add_loc_descr (&cc_loc_result, x1_ref);
4401bf24 8655 add_loc_descr (&cc_loc_result,
e7af1d45
RK
8656 new_loc_descr (DW_OP_piece,
8657 GET_MODE_SIZE (GET_MODE (x1)), 0));
4401bf24
JL
8658
8659 return cc_loc_result;
8660}
8661
a3f97cbb
JW
8662/* Output a proper Dwarf location descriptor for a variable or parameter
8663 which is either allocated in a register or in a memory location. For a
8664 register, we just generate an OP_REG and the register number. For a
8665 memory location we provide a Dwarf postfix expression describing how to
e7af1d45
RK
8666 generate the (dynamic) address of the object onto the address stack.
8667
8668 If we don't know how to describe it, return 0. */
71dfc51f 8669
a3f97cbb 8670static dw_loc_descr_ref
0a2d3d69 8671loc_descriptor (rtx rtl, bool can_use_fbreg)
a3f97cbb
JW
8672{
8673 dw_loc_descr_ref loc_result = NULL;
e7af1d45 8674
a3f97cbb
JW
8675 switch (GET_CODE (rtl))
8676 {
8677 case SUBREG:
a3f97cbb 8678 /* The case of a subreg may arise when we have a local (register)
73c68f61
SS
8679 variable or a formal (register) parameter which doesn't quite fill
8680 up an entire register. For now, just assume that it is
8681 legitimate to make the Dwarf info refer to the whole register which
8682 contains the given subreg. */
ddef6bc7 8683 rtl = SUBREG_REG (rtl);
71dfc51f 8684
2ad9852d 8685 /* ... fall through ... */
a3f97cbb
JW
8686
8687 case REG:
5c90448c 8688 loc_result = reg_loc_descriptor (rtl);
a3f97cbb
JW
8689 break;
8690
8691 case MEM:
0a2d3d69
DB
8692 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8693 can_use_fbreg);
a3f97cbb
JW
8694 break;
8695
4401bf24
JL
8696 case CONCAT:
8697 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8698 break;
8699
0a2d3d69
DB
8700 case VAR_LOCATION:
8701 /* Single part. */
8702 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8703 {
8704 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8705 }
8706 /* Multiple parts. */
8707 else
8708 {
8709 rtvec par_elems = XVEC (XEXP (rtl, 1), 0);
8710 int num_elem = GET_NUM_ELEM (par_elems);
8711 enum machine_mode mode;
8712 int i;
8713
8714 /* Create the first one, so we have something to add to. */
8715 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8716 can_use_fbreg);
8717 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8718 add_loc_descr (&loc_result,
8719 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8720 for (i = 1; i < num_elem; i++)
8721 {
8722 dw_loc_descr_ref temp;
8723
8724 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8725 can_use_fbreg);
8726 add_loc_descr (&loc_result, temp);
8727 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8728 add_loc_descr (&loc_result,
8729 new_loc_descr (DW_OP_piece,
8730 GET_MODE_SIZE (mode), 0));
8731 }
8732 }
8733 break;
8734
a3f97cbb 8735 default:
71dfc51f 8736 abort ();
a3f97cbb 8737 }
71dfc51f 8738
a3f97cbb
JW
8739 return loc_result;
8740}
8741
2ad9852d
RK
8742/* Similar, but generate the descriptor from trees instead of rtl. This comes
8743 up particularly with variable length arrays. If ADDRESSP is nonzero, we are
8744 looking for an address. Otherwise, we return a value. If we can't make a
8745 descriptor, return 0. */
d8041cc8
RH
8746
8747static dw_loc_descr_ref
7080f735 8748loc_descriptor_from_tree (tree loc, int addressp)
d8041cc8 8749{
e7af1d45
RK
8750 dw_loc_descr_ref ret, ret1;
8751 int indirect_p = 0;
8df83eae 8752 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
d8041cc8
RH
8753 enum dwarf_location_atom op;
8754
8755 /* ??? Most of the time we do not take proper care for sign/zero
8756 extending the values properly. Hopefully this won't be a real
8757 problem... */
8758
8759 switch (TREE_CODE (loc))
8760 {
8761 case ERROR_MARK:
e7af1d45 8762 return 0;
d8041cc8 8763
e7af1d45 8764 case PLACEHOLDER_EXPR:
b4ae5201
RK
8765 /* This case involves extracting fields from an object to determine the
8766 position of other fields. We don't try to encode this here. The
8767 only user of this is Ada, which encodes the needed information using
8768 the names of types. */
e7af1d45 8769 return 0;
b4ae5201 8770
aea9695c
RK
8771 case CALL_EXPR:
8772 return 0;
8773
4ada538b
MM
8774 case PREINCREMENT_EXPR:
8775 case PREDECREMENT_EXPR:
8776 case POSTINCREMENT_EXPR:
8777 case POSTDECREMENT_EXPR:
8778 /* There are no opcodes for these operations. */
8779 return 0;
8780
aea9695c
RK
8781 case ADDR_EXPR:
8782 /* We can support this only if we can look through conversions and
8783 find an INDIRECT_EXPR. */
8784 for (loc = TREE_OPERAND (loc, 0);
8785 TREE_CODE (loc) == CONVERT_EXPR || TREE_CODE (loc) == NOP_EXPR
8786 || TREE_CODE (loc) == NON_LVALUE_EXPR
8787 || TREE_CODE (loc) == VIEW_CONVERT_EXPR
8788 || TREE_CODE (loc) == SAVE_EXPR;
8789 loc = TREE_OPERAND (loc, 0))
8790 ;
8791
8792 return (TREE_CODE (loc) == INDIRECT_REF
8793 ? loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp)
8794 : 0);
8795
d8041cc8 8796 case VAR_DECL:
b9203463
RH
8797 if (DECL_THREAD_LOCAL (loc))
8798 {
8799 rtx rtl;
8800
8801#ifndef ASM_OUTPUT_DWARF_DTPREL
8802 /* If this is not defined, we have no way to emit the data. */
8803 return 0;
8804#endif
8805
8806 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8807 look up addresses of objects in the current module. */
3c655f42 8808 if (DECL_EXTERNAL (loc))
b9203463
RH
8809 return 0;
8810
8811 rtl = rtl_for_decl_location (loc);
8812 if (rtl == NULL_RTX)
8813 return 0;
8814
8815 if (GET_CODE (rtl) != MEM)
8816 return 0;
8817 rtl = XEXP (rtl, 0);
8818 if (! CONSTANT_P (rtl))
8819 return 0;
8820
8821 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8822 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8823 ret->dw_loc_oprnd1.v.val_addr = rtl;
8824
8825 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8826 add_loc_descr (&ret, ret1);
8827
8828 indirect_p = 1;
8829 break;
8830 }
5d3cc252 8831 /* Fall through. */
b9203463 8832
d8041cc8 8833 case PARM_DECL:
6de9cd9a 8834 case RESULT_DECL:
d8041cc8
RH
8835 {
8836 rtx rtl = rtl_for_decl_location (loc);
d8041cc8 8837
a97c9600 8838 if (rtl == NULL_RTX)
e7af1d45 8839 return 0;
a97c9600 8840 else if (CONSTANT_P (rtl))
d8041cc8
RH
8841 {
8842 ret = new_loc_descr (DW_OP_addr, 0, 0);
8843 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8844 ret->dw_loc_oprnd1.v.val_addr = rtl;
e7af1d45 8845 indirect_p = 1;
d8041cc8
RH
8846 }
8847 else
8848 {
c28abdf0
RH
8849 enum machine_mode mode = GET_MODE (rtl);
8850
d8041cc8
RH
8851 if (GET_CODE (rtl) == MEM)
8852 {
e7af1d45 8853 indirect_p = 1;
d8041cc8
RH
8854 rtl = XEXP (rtl, 0);
8855 }
2ad9852d 8856
0a2d3d69 8857 ret = mem_loc_descriptor (rtl, mode, true);
d8041cc8
RH
8858 }
8859 }
8860 break;
8861
8862 case INDIRECT_REF:
8863 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
e7af1d45 8864 indirect_p = 1;
d8041cc8
RH
8865 break;
8866
749552c4
RK
8867 case COMPOUND_EXPR:
8868 return loc_descriptor_from_tree (TREE_OPERAND (loc, 1), addressp);
8869
ed972b14
RK
8870 case NOP_EXPR:
8871 case CONVERT_EXPR:
8872 case NON_LVALUE_EXPR:
ed239f5a 8873 case VIEW_CONVERT_EXPR:
b4ae5201 8874 case SAVE_EXPR:
032cb602 8875 case MODIFY_EXPR:
ed972b14 8876 return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
e57cabac 8877
d8041cc8
RH
8878 case COMPONENT_REF:
8879 case BIT_FIELD_REF:
8880 case ARRAY_REF:
b4e3fabb 8881 case ARRAY_RANGE_REF:
d8041cc8
RH
8882 {
8883 tree obj, offset;
8884 HOST_WIDE_INT bitsize, bitpos, bytepos;
8885 enum machine_mode mode;
8886 int volatilep;
d8041cc8
RH
8887
8888 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
a06ef755 8889 &unsignedp, &volatilep);
e7af1d45
RK
8890
8891 if (obj == loc)
8892 return 0;
8893
d8041cc8 8894 ret = loc_descriptor_from_tree (obj, 1);
e7af1d45 8895 if (ret == 0
2ad9852d 8896 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
e7af1d45 8897 return 0;
d8041cc8
RH
8898
8899 if (offset != NULL_TREE)
8900 {
8901 /* Variable offset. */
8902 add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
8903 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8904 }
8905
e7af1d45
RK
8906 if (!addressp)
8907 indirect_p = 1;
d8041cc8
RH
8908
8909 bytepos = bitpos / BITS_PER_UNIT;
8910 if (bytepos > 0)
8911 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8912 else if (bytepos < 0)
8913 {
8914 add_loc_descr (&ret, int_loc_descriptor (bytepos));
8915 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8916 }
8917 break;
8918 }
8919
8920 case INTEGER_CST:
8921 if (host_integerp (loc, 0))
8922 ret = int_loc_descriptor (tree_low_cst (loc, 0));
e7af1d45
RK
8923 else
8924 return 0;
d8041cc8 8925 break;
d8041cc8 8926
c67b2a58
RK
8927 case CONSTRUCTOR:
8928 {
75c20980
RH
8929 /* Get an RTL for this, if something has been emitted. */
8930 rtx rtl = lookup_constant_def (loc);
8931 enum machine_mode mode;
8932
8933 if (GET_CODE (rtl) != MEM)
8934 return 0;
8935 mode = GET_MODE (rtl);
8936 rtl = XEXP (rtl, 0);
8937
5fd9b178 8938 rtl = targetm.delegitimize_address (rtl);
75c20980 8939
c67b2a58 8940 indirect_p = 1;
0a2d3d69 8941 ret = mem_loc_descriptor (rtl, mode, true);
c67b2a58
RK
8942 break;
8943 }
8944
c26fbbca 8945 case TRUTH_AND_EXPR:
9702143f 8946 case TRUTH_ANDIF_EXPR:
d8041cc8
RH
8947 case BIT_AND_EXPR:
8948 op = DW_OP_and;
8949 goto do_binop;
e7af1d45 8950
9702143f 8951 case TRUTH_XOR_EXPR:
d8041cc8
RH
8952 case BIT_XOR_EXPR:
8953 op = DW_OP_xor;
8954 goto do_binop;
e7af1d45 8955
9702143f
RK
8956 case TRUTH_OR_EXPR:
8957 case TRUTH_ORIF_EXPR:
d8041cc8
RH
8958 case BIT_IOR_EXPR:
8959 op = DW_OP_or;
8960 goto do_binop;
e7af1d45 8961
8dcea3f3
VC
8962 case FLOOR_DIV_EXPR:
8963 case CEIL_DIV_EXPR:
8964 case ROUND_DIV_EXPR:
d8041cc8
RH
8965 case TRUNC_DIV_EXPR:
8966 op = DW_OP_div;
8967 goto do_binop;
e7af1d45 8968
d8041cc8
RH
8969 case MINUS_EXPR:
8970 op = DW_OP_minus;
8971 goto do_binop;
e7af1d45 8972
8dcea3f3
VC
8973 case FLOOR_MOD_EXPR:
8974 case CEIL_MOD_EXPR:
8975 case ROUND_MOD_EXPR:
d8041cc8
RH
8976 case TRUNC_MOD_EXPR:
8977 op = DW_OP_mod;
8978 goto do_binop;
e7af1d45 8979
d8041cc8
RH
8980 case MULT_EXPR:
8981 op = DW_OP_mul;
8982 goto do_binop;
e7af1d45 8983
d8041cc8
RH
8984 case LSHIFT_EXPR:
8985 op = DW_OP_shl;
8986 goto do_binop;
e7af1d45 8987
d8041cc8
RH
8988 case RSHIFT_EXPR:
8989 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
8990 goto do_binop;
e7af1d45 8991
d8041cc8
RH
8992 case PLUS_EXPR:
8993 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
8994 && host_integerp (TREE_OPERAND (loc, 1), 0))
8995 {
8996 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
e7af1d45
RK
8997 if (ret == 0)
8998 return 0;
8999
d8041cc8
RH
9000 add_loc_descr (&ret,
9001 new_loc_descr (DW_OP_plus_uconst,
9002 tree_low_cst (TREE_OPERAND (loc, 1),
9003 0),
9004 0));
9005 break;
9006 }
e7af1d45 9007
d8041cc8
RH
9008 op = DW_OP_plus;
9009 goto do_binop;
2ad9852d 9010
d8041cc8 9011 case LE_EXPR:
8df83eae 9012 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
e7af1d45
RK
9013 return 0;
9014
d8041cc8
RH
9015 op = DW_OP_le;
9016 goto do_binop;
e7af1d45 9017
d8041cc8 9018 case GE_EXPR:
8df83eae 9019 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
e7af1d45
RK
9020 return 0;
9021
d8041cc8
RH
9022 op = DW_OP_ge;
9023 goto do_binop;
e7af1d45 9024
d8041cc8 9025 case LT_EXPR:
8df83eae 9026 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
e7af1d45
RK
9027 return 0;
9028
d8041cc8
RH
9029 op = DW_OP_lt;
9030 goto do_binop;
e7af1d45 9031
d8041cc8 9032 case GT_EXPR:
8df83eae 9033 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
e7af1d45
RK
9034 return 0;
9035
d8041cc8
RH
9036 op = DW_OP_gt;
9037 goto do_binop;
e7af1d45 9038
d8041cc8
RH
9039 case EQ_EXPR:
9040 op = DW_OP_eq;
9041 goto do_binop;
e7af1d45 9042
d8041cc8
RH
9043 case NE_EXPR:
9044 op = DW_OP_ne;
9045 goto do_binop;
9046
9047 do_binop:
9048 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
e7af1d45
RK
9049 ret1 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
9050 if (ret == 0 || ret1 == 0)
9051 return 0;
9052
9053 add_loc_descr (&ret, ret1);
d8041cc8
RH
9054 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9055 break;
9056
9702143f 9057 case TRUTH_NOT_EXPR:
d8041cc8
RH
9058 case BIT_NOT_EXPR:
9059 op = DW_OP_not;
9060 goto do_unop;
e7af1d45 9061
d8041cc8
RH
9062 case ABS_EXPR:
9063 op = DW_OP_abs;
9064 goto do_unop;
e7af1d45 9065
d8041cc8
RH
9066 case NEGATE_EXPR:
9067 op = DW_OP_neg;
9068 goto do_unop;
9069
9070 do_unop:
9071 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
e7af1d45
RK
9072 if (ret == 0)
9073 return 0;
9074
d8041cc8
RH
9075 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9076 break;
9077
fd5580cb 9078 case MIN_EXPR:
d8041cc8 9079 case MAX_EXPR:
fd5580cb
B
9080 {
9081 const enum tree_code code =
9082 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9083
9084 loc = build (COND_EXPR, TREE_TYPE (loc),
9085 build (code, integer_type_node,
9086 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9087 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9088 }
2ad9852d 9089
3ef42a0c 9090 /* ... fall through ... */
d8041cc8
RH
9091
9092 case COND_EXPR:
9093 {
e7af1d45
RK
9094 dw_loc_descr_ref lhs
9095 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
9096 dw_loc_descr_ref rhs
9097 = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
d8041cc8
RH
9098 dw_loc_descr_ref bra_node, jump_node, tmp;
9099
9100 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
e7af1d45
RK
9101 if (ret == 0 || lhs == 0 || rhs == 0)
9102 return 0;
9103
d8041cc8
RH
9104 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9105 add_loc_descr (&ret, bra_node);
9106
e7af1d45 9107 add_loc_descr (&ret, rhs);
d8041cc8
RH
9108 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9109 add_loc_descr (&ret, jump_node);
9110
e7af1d45 9111 add_loc_descr (&ret, lhs);
d8041cc8 9112 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
e7af1d45 9113 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
d8041cc8
RH
9114
9115 /* ??? Need a node to point the skip at. Use a nop. */
9116 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9117 add_loc_descr (&ret, tmp);
9118 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9119 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9120 }
9121 break;
9122
9123 default:
7d445f15
RH
9124 /* Leave front-end specific codes as simply unknown. This comes
9125 up, for instance, with the C STMT_EXPR. */
9126 if ((unsigned int) TREE_CODE (loc)
9127 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9128 return 0;
9129
9130 /* Otherwise this is a generic code; we should just lists all of
9131 these explicitly. Aborting means we forgot one. */
d8041cc8
RH
9132 abort ();
9133 }
9134
e7af1d45
RK
9135 /* Show if we can't fill the request for an address. */
9136 if (addressp && indirect_p == 0)
9137 return 0;
d8041cc8
RH
9138
9139 /* If we've got an address and don't want one, dereference. */
e7af1d45 9140 if (!addressp && indirect_p > 0)
d8041cc8 9141 {
e7af1d45
RK
9142 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9143
9144 if (size > DWARF2_ADDR_SIZE || size == -1)
9145 return 0;
2ad9852d 9146 else if (size == DWARF2_ADDR_SIZE)
d8041cc8
RH
9147 op = DW_OP_deref;
9148 else
9149 op = DW_OP_deref_size;
e7af1d45
RK
9150
9151 add_loc_descr (&ret, new_loc_descr (op, size, 0));
d8041cc8
RH
9152 }
9153
9154 return ret;
9155}
9156
665f2503 9157/* Given a value, round it up to the lowest multiple of `boundary'
a3f97cbb 9158 which is not less than the value itself. */
71dfc51f 9159
665f2503 9160static inline HOST_WIDE_INT
7080f735 9161ceiling (HOST_WIDE_INT value, unsigned int boundary)
a3f97cbb
JW
9162{
9163 return (((value + boundary - 1) / boundary) * boundary);
9164}
9165
9166/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9167 pointer to the declared type for the relevant field variable, or return
9168 `integer_type_node' if the given node turns out to be an
9169 ERROR_MARK node. */
71dfc51f
RK
9170
9171static inline tree
7080f735 9172field_type (tree decl)
a3f97cbb 9173{
b3694847 9174 tree type;
a3f97cbb
JW
9175
9176 if (TREE_CODE (decl) == ERROR_MARK)
9177 return integer_type_node;
9178
9179 type = DECL_BIT_FIELD_TYPE (decl);
71dfc51f 9180 if (type == NULL_TREE)
a3f97cbb
JW
9181 type = TREE_TYPE (decl);
9182
9183 return type;
9184}
9185
5f446d21
DD
9186/* Given a pointer to a tree node, return the alignment in bits for
9187 it, or else return BITS_PER_WORD if the node actually turns out to
9188 be an ERROR_MARK node. */
71dfc51f
RK
9189
9190static inline unsigned
7080f735 9191simple_type_align_in_bits (tree type)
a3f97cbb
JW
9192{
9193 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9194}
9195
5f446d21 9196static inline unsigned
7080f735 9197simple_decl_align_in_bits (tree decl)
5f446d21
DD
9198{
9199 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9200}
9201
2ad9852d
RK
9202/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9203 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9204 or return 0 if we are unable to determine what that offset is, either
9205 because the argument turns out to be a pointer to an ERROR_MARK node, or
9206 because the offset is actually variable. (We can't handle the latter case
9207 just yet). */
71dfc51f 9208
665f2503 9209static HOST_WIDE_INT
7080f735 9210field_byte_offset (tree decl)
a3f97cbb 9211{
665f2503 9212 unsigned int type_align_in_bits;
5f446d21 9213 unsigned int decl_align_in_bits;
665f2503 9214 unsigned HOST_WIDE_INT type_size_in_bits;
665f2503 9215 HOST_WIDE_INT object_offset_in_bits;
665f2503
RK
9216 tree type;
9217 tree field_size_tree;
9218 HOST_WIDE_INT bitpos_int;
9219 HOST_WIDE_INT deepest_bitpos;
9220 unsigned HOST_WIDE_INT field_size_in_bits;
a3f97cbb
JW
9221
9222 if (TREE_CODE (decl) == ERROR_MARK)
9223 return 0;
2ad9852d 9224 else if (TREE_CODE (decl) != FIELD_DECL)
a3f97cbb
JW
9225 abort ();
9226
9227 type = field_type (decl);
a3f97cbb
JW
9228 field_size_tree = DECL_SIZE (decl);
9229
3df18884
RH
9230 /* The size could be unspecified if there was an error, or for
9231 a flexible array member. */
50352c9c 9232 if (! field_size_tree)
3df18884 9233 field_size_tree = bitsize_zero_node;
50352c9c 9234
556273e0 9235 /* We cannot yet cope with fields whose positions are variable, so
a3f97cbb
JW
9236 for now, when we see such things, we simply return 0. Someday, we may
9237 be able to handle such cases, but it will be damn difficult. */
665f2503 9238 if (! host_integerp (bit_position (decl), 0))
a3f97cbb 9239 return 0;
14a774a9 9240
665f2503 9241 bitpos_int = int_bit_position (decl);
a3f97cbb 9242
3df18884 9243 /* If we don't know the size of the field, pretend it's a full word. */
665f2503
RK
9244 if (host_integerp (field_size_tree, 1))
9245 field_size_in_bits = tree_low_cst (field_size_tree, 1);
14a774a9
RK
9246 else
9247 field_size_in_bits = BITS_PER_WORD;
a3f97cbb
JW
9248
9249 type_size_in_bits = simple_type_size_in_bits (type);
a3f97cbb 9250 type_align_in_bits = simple_type_align_in_bits (type);
5f446d21 9251 decl_align_in_bits = simple_decl_align_in_bits (decl);
a3f97cbb 9252
2ad9852d
RK
9253 /* The GCC front-end doesn't make any attempt to keep track of the starting
9254 bit offset (relative to the start of the containing structure type) of the
9255 hypothetical "containing object" for a bit-field. Thus, when computing
9256 the byte offset value for the start of the "containing object" of a
9257 bit-field, we must deduce this information on our own. This can be rather
9258 tricky to do in some cases. For example, handling the following structure
9259 type definition when compiling for an i386/i486 target (which only aligns
9260 long long's to 32-bit boundaries) can be very tricky:
a3f97cbb
JW
9261
9262 struct S { int field1; long long field2:31; };
9263
2ad9852d
RK
9264 Fortunately, there is a simple rule-of-thumb which can be used in such
9265 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9266 structure shown above. It decides to do this based upon one simple rule
9267 for bit-field allocation. GCC allocates each "containing object" for each
9268 bit-field at the first (i.e. lowest addressed) legitimate alignment
9269 boundary (based upon the required minimum alignment for the declared type
9270 of the field) which it can possibly use, subject to the condition that
9271 there is still enough available space remaining in the containing object
9272 (when allocated at the selected point) to fully accommodate all of the
9273 bits of the bit-field itself.
9274
9275 This simple rule makes it obvious why GCC allocates 8 bytes for each
9276 object of the structure type shown above. When looking for a place to
9277 allocate the "containing object" for `field2', the compiler simply tries
9278 to allocate a 64-bit "containing object" at each successive 32-bit
9279 boundary (starting at zero) until it finds a place to allocate that 64-
9280 bit field such that at least 31 contiguous (and previously unallocated)
9281 bits remain within that selected 64 bit field. (As it turns out, for the
9282 example above, the compiler finds it is OK to allocate the "containing
9283 object" 64-bit field at bit-offset zero within the structure type.)
9284
9285 Here we attempt to work backwards from the limited set of facts we're
9286 given, and we try to deduce from those facts, where GCC must have believed
9287 that the containing object started (within the structure type). The value
9288 we deduce is then used (by the callers of this routine) to generate
9289 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9290 and, in the case of DW_AT_location, regular fields as well). */
a3f97cbb
JW
9291
9292 /* Figure out the bit-distance from the start of the structure to the
9293 "deepest" bit of the bit-field. */
9294 deepest_bitpos = bitpos_int + field_size_in_bits;
9295
9296 /* This is the tricky part. Use some fancy footwork to deduce where the
9297 lowest addressed bit of the containing object must be. */
5f446d21
DD
9298 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9299
9300 /* Round up to type_align by default. This works best for bitfields. */
9301 object_offset_in_bits += type_align_in_bits - 1;
9302 object_offset_in_bits /= type_align_in_bits;
9303 object_offset_in_bits *= type_align_in_bits;
a3f97cbb 9304
5f446d21
DD
9305 if (object_offset_in_bits > bitpos_int)
9306 {
9307 /* Sigh, the decl must be packed. */
9308 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9309
9310 /* Round up to decl_align instead. */
9311 object_offset_in_bits += decl_align_in_bits - 1;
9312 object_offset_in_bits /= decl_align_in_bits;
9313 object_offset_in_bits *= decl_align_in_bits;
9314 }
a3f97cbb 9315
2ad9852d 9316 return object_offset_in_bits / BITS_PER_UNIT;
a3f97cbb 9317}
a3f97cbb 9318\f
71dfc51f
RK
9319/* The following routines define various Dwarf attributes and any data
9320 associated with them. */
a3f97cbb 9321
ef76d03b 9322/* Add a location description attribute value to a DIE.
a3f97cbb 9323
ef76d03b 9324 This emits location attributes suitable for whole variables and
a3f97cbb
JW
9325 whole parameters. Note that the location attributes for struct fields are
9326 generated by the routine `data_member_location_attribute' below. */
71dfc51f 9327
b9203463 9328static inline void
7080f735
AJ
9329add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9330 dw_loc_descr_ref descr)
a3f97cbb 9331{
e7af1d45
RK
9332 if (descr != 0)
9333 add_AT_loc (die, attr_kind, descr);
a3f97cbb
JW
9334}
9335
2ad9852d
RK
9336/* Attach the specialized form of location attribute used for data members of
9337 struct and union types. In the special case of a FIELD_DECL node which
9338 represents a bit-field, the "offset" part of this special location
9339 descriptor must indicate the distance in bytes from the lowest-addressed
9340 byte of the containing struct or union type to the lowest-addressed byte of
9341 the "containing object" for the bit-field. (See the `field_byte_offset'
9342 function above).
9343
9344 For any given bit-field, the "containing object" is a hypothetical object
9345 (of some integral or enum type) within which the given bit-field lives. The
9346 type of this hypothetical "containing object" is always the same as the
9347 declared type of the individual bit-field itself (for GCC anyway... the
9348 DWARF spec doesn't actually mandate this). Note that it is the size (in
9349 bytes) of the hypothetical "containing object" which will be given in the
9350 DW_AT_byte_size attribute for this bit-field. (See the
9351 `byte_size_attribute' function below.) It is also used when calculating the
9352 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9353 function below.) */
71dfc51f 9354
a3f97cbb 9355static void
7080f735 9356add_data_member_location_attribute (dw_die_ref die, tree decl)
a3f97cbb 9357{
799f628a 9358 HOST_WIDE_INT offset;
649ce3f2 9359 dw_loc_descr_ref loc_descr = 0;
a3f97cbb 9360
61b32c02 9361 if (TREE_CODE (decl) == TREE_VEC)
649ce3f2
JM
9362 {
9363 /* We're working on the TAG_inheritance for a base class. */
1d3d6b1e 9364 if (TREE_VIA_VIRTUAL (decl) && is_cxx ())
649ce3f2
JM
9365 {
9366 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9367 aren't at a fixed offset from all (sub)objects of the same
9368 type. We need to extract the appropriate offset from our
9369 vtable. The following dwarf expression means
9370
9371 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9372
9373 This is specific to the V3 ABI, of course. */
9374
9375 dw_loc_descr_ref tmp;
2ad9852d 9376
649ce3f2
JM
9377 /* Make a copy of the object address. */
9378 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9379 add_loc_descr (&loc_descr, tmp);
2ad9852d 9380
649ce3f2
JM
9381 /* Extract the vtable address. */
9382 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9383 add_loc_descr (&loc_descr, tmp);
2ad9852d 9384
649ce3f2
JM
9385 /* Calculate the address of the offset. */
9386 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9387 if (offset >= 0)
9388 abort ();
2ad9852d 9389
649ce3f2
JM
9390 tmp = int_loc_descriptor (-offset);
9391 add_loc_descr (&loc_descr, tmp);
9392 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9393 add_loc_descr (&loc_descr, tmp);
2ad9852d 9394
649ce3f2
JM
9395 /* Extract the offset. */
9396 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9397 add_loc_descr (&loc_descr, tmp);
2ad9852d 9398
649ce3f2
JM
9399 /* Add it to the object address. */
9400 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9401 add_loc_descr (&loc_descr, tmp);
9402 }
9403 else
9404 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9405 }
61b32c02
JM
9406 else
9407 offset = field_byte_offset (decl);
9408
649ce3f2
JM
9409 if (! loc_descr)
9410 {
9411 enum dwarf_location_atom op;
9412
2ad9852d
RK
9413 /* The DWARF2 standard says that we should assume that the structure
9414 address is already on the stack, so we can specify a structure field
9415 address by using DW_OP_plus_uconst. */
71dfc51f 9416
a3f97cbb 9417#ifdef MIPS_DEBUGGING_INFO
2ad9852d
RK
9418 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9419 operator correctly. It works only if we leave the offset on the
9420 stack. */
649ce3f2 9421 op = DW_OP_constu;
a3f97cbb 9422#else
649ce3f2 9423 op = DW_OP_plus_uconst;
a3f97cbb 9424#endif
71dfc51f 9425
649ce3f2
JM
9426 loc_descr = new_loc_descr (op, offset, 0);
9427 }
2ad9852d 9428
a3f97cbb
JW
9429 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9430}
9431
e7ee3914
AM
9432/* Writes integer values to dw_vec_const array. */
9433
9434static void
9435insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9436{
9437 while (size != 0)
9438 {
9439 *dest++ = val & 0xff;
9440 val >>= 8;
9441 --size;
9442 }
9443}
9444
9445/* Reads integers from dw_vec_const array. Inverse of insert_int. */
9446
9447static HOST_WIDE_INT
9448extract_int (const unsigned char *src, unsigned int size)
9449{
9450 HOST_WIDE_INT val = 0;
9451
9452 src += size;
9453 while (size != 0)
9454 {
9455 val <<= 8;
9456 val |= *--src & 0xff;
9457 --size;
9458 }
9459 return val;
9460}
9461
9462/* Writes floating point values to dw_vec_const array. */
9463
9464static void
9465insert_float (rtx rtl, unsigned char *array)
9466{
9467 REAL_VALUE_TYPE rv;
9468 long val[4];
9469 int i;
9470
9471 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9472 real_to_target (val, &rv, GET_MODE (rtl));
9473
9474 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9475 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9476 {
9477 insert_int (val[i], 4, array);
9478 array += 4;
9479 }
9480}
9481
b20b352b 9482/* Attach a DW_AT_const_value attribute for a variable or a parameter which
a3f97cbb
JW
9483 does not have a "location" either in memory or in a register. These
9484 things can arise in GNU C when a constant is passed as an actual parameter
9485 to an inlined function. They can also arise in C++ where declared
9486 constants do not necessarily get memory "homes". */
71dfc51f 9487
a3f97cbb 9488static void
7080f735 9489add_const_value_attribute (dw_die_ref die, rtx rtl)
a3f97cbb
JW
9490{
9491 switch (GET_CODE (rtl))
9492 {
9493 case CONST_INT:
2e4b9b8c
RH
9494 {
9495 HOST_WIDE_INT val = INTVAL (rtl);
c26fbbca 9496
799f628a
JH
9497 if (val < 0)
9498 add_AT_int (die, DW_AT_const_value, val);
9499 else
9500 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
2e4b9b8c 9501 }
a3f97cbb
JW
9502 break;
9503
9504 case CONST_DOUBLE:
9505 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
73c68f61
SS
9506 floating-point constant. A CONST_DOUBLE is used whenever the
9507 constant requires more than one word in order to be adequately
9508 represented. We output CONST_DOUBLEs as blocks. */
469ac993 9509 {
b3694847 9510 enum machine_mode mode = GET_MODE (rtl);
469ac993
JM
9511
9512 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9513 {
e7ee3914
AM
9514 unsigned int length = GET_MODE_SIZE (mode);
9515 unsigned char *array = ggc_alloc (length);
469ac993 9516
e7ee3914
AM
9517 insert_float (rtl, array);
9518 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
469ac993
JM
9519 }
9520 else
2e4b9b8c
RH
9521 {
9522 /* ??? We really should be using HOST_WIDE_INT throughout. */
9523 if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
9524 abort ();
2ad9852d 9525
2e4b9b8c
RH
9526 add_AT_long_long (die, DW_AT_const_value,
9527 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9528 }
469ac993 9529 }
a3f97cbb
JW
9530 break;
9531
e7ee3914
AM
9532 case CONST_VECTOR:
9533 {
9534 enum machine_mode mode = GET_MODE (rtl);
9535 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9536 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9537 unsigned char *array = ggc_alloc (length * elt_size);
9538 unsigned int i;
9539 unsigned char *p;
9540
9541 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9542 {
9543 for (i = 0, p = array; i < length; i++, p += elt_size)
9544 {
9545 rtx elt = CONST_VECTOR_ELT (rtl, i);
9546 HOST_WIDE_INT lo, hi;
9547 if (GET_CODE (elt) == CONST_INT)
9548 {
9549 lo = INTVAL (elt);
9550 hi = -(lo < 0);
9551 }
9552 else if (GET_CODE (elt) == CONST_DOUBLE)
9553 {
9554 lo = CONST_DOUBLE_LOW (elt);
9555 hi = CONST_DOUBLE_HIGH (elt);
9556 }
9557 else
9558 abort ();
9559
9560 if (elt_size <= sizeof (HOST_WIDE_INT))
9561 insert_int (lo, elt_size, p);
9562 else if (elt_size == 2 * sizeof (HOST_WIDE_INT))
9563 {
9564 unsigned char *p0 = p;
9565 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9566
9567 if (WORDS_BIG_ENDIAN)
9568 {
9569 p0 = p1;
9570 p1 = p;
9571 }
9572 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9573 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9574 }
9575 else
9576 abort ();
9577 }
9578 }
9579 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
9580 {
9581 for (i = 0, p = array; i < length; i++, p += elt_size)
9582 {
9583 rtx elt = CONST_VECTOR_ELT (rtl, i);
9584 insert_float (elt, p);
9585 }
9586 }
9587 else
9588 abort ();
9589
9590 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9591 }
9592 break;
9593
a3f97cbb
JW
9594 case CONST_STRING:
9595 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9596 break;
9597
9598 case SYMBOL_REF:
9599 case LABEL_REF:
9600 case CONST:
c470afad
RK
9601 add_AT_addr (die, DW_AT_const_value, rtl);
9602 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
a3f97cbb
JW
9603 break;
9604
9605 case PLUS:
9606 /* In cases where an inlined instance of an inline function is passed
73c68f61
SS
9607 the address of an `auto' variable (which is local to the caller) we
9608 can get a situation where the DECL_RTL of the artificial local
9609 variable (for the inlining) which acts as a stand-in for the
9610 corresponding formal parameter (of the inline function) will look
9611 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9612 exactly a compile-time constant expression, but it isn't the address
9613 of the (artificial) local variable either. Rather, it represents the
9614 *value* which the artificial local variable always has during its
9615 lifetime. We currently have no way to represent such quasi-constant
9616 values in Dwarf, so for now we just punt and generate nothing. */
a3f97cbb
JW
9617 break;
9618
9619 default:
9620 /* No other kinds of rtx should be possible here. */
9621 abort ();
9622 }
9623
9624}
9625
d8041cc8 9626static rtx
7080f735 9627rtl_for_decl_location (tree decl)
a3f97cbb 9628{
b3694847 9629 rtx rtl;
71dfc51f 9630
a3f97cbb
JW
9631 /* Here we have to decide where we are going to say the parameter "lives"
9632 (as far as the debugger is concerned). We only have a couple of
9633 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
71dfc51f 9634
556273e0 9635 DECL_RTL normally indicates where the parameter lives during most of the
71dfc51f 9636 activation of the function. If optimization is enabled however, this
556273e0 9637 could be either NULL or else a pseudo-reg. Both of those cases indicate
a3f97cbb
JW
9638 that the parameter doesn't really live anywhere (as far as the code
9639 generation parts of GCC are concerned) during most of the function's
9640 activation. That will happen (for example) if the parameter is never
71dfc51f
RK
9641 referenced within the function.
9642
9643 We could just generate a location descriptor here for all non-NULL
9644 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9645 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9646 where DECL_RTL is NULL or is a pseudo-reg.
9647
9648 Note however that we can only get away with using DECL_INCOMING_RTL as
9649 a backup substitute for DECL_RTL in certain limited cases. In cases
9650 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9651 we can be sure that the parameter was passed using the same type as it is
9652 declared to have within the function, and that its DECL_INCOMING_RTL
9653 points us to a place where a value of that type is passed.
9654
9655 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9656 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9657 because in these cases DECL_INCOMING_RTL points us to a value of some
9658 type which is *different* from the type of the parameter itself. Thus,
9659 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9660 such cases, the debugger would end up (for example) trying to fetch a
9661 `float' from a place which actually contains the first part of a
9662 `double'. That would lead to really incorrect and confusing
9663 output at debug-time.
9664
9665 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9666 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9667 are a couple of exceptions however. On little-endian machines we can
9668 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9669 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9670 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9671 when (on a little-endian machine) a non-prototyped function has a
9672 parameter declared to be of type `short' or `char'. In such cases,
9673 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9674 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9675 passed `int' value. If the debugger then uses that address to fetch
9676 a `short' or a `char' (on a little-endian machine) the result will be
9677 the correct data, so we allow for such exceptional cases below.
9678
9679 Note that our goal here is to describe the place where the given formal
2ad9852d
RK
9680 parameter lives during most of the function's activation (i.e. between the
9681 end of the prologue and the start of the epilogue). We'll do that as best
9682 as we can. Note however that if the given formal parameter is modified
9683 sometime during the execution of the function, then a stack backtrace (at
9684 debug-time) will show the function as having been called with the *new*
9685 value rather than the value which was originally passed in. This happens
9686 rarely enough that it is not a major problem, but it *is* a problem, and
9687 I'd like to fix it.
9688
9689 A future version of dwarf2out.c may generate two additional attributes for
9690 any given DW_TAG_formal_parameter DIE which will describe the "passed
9691 type" and the "passed location" for the given formal parameter in addition
9692 to the attributes we now generate to indicate the "declared type" and the
9693 "active location" for each parameter. This additional set of attributes
9694 could be used by debuggers for stack backtraces. Separately, note that
9695 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9696 This happens (for example) for inlined-instances of inline function formal
9697 parameters which are never referenced. This really shouldn't be
9698 happening. All PARM_DECL nodes should get valid non-NULL
6de9cd9a 9699 DECL_INCOMING_RTL values. FIXME. */
a3f97cbb
JW
9700
9701 /* Use DECL_RTL as the "location" unless we find something better. */
110c3568 9702 rtl = DECL_RTL_IF_SET (decl);
a3f97cbb 9703
c28abdf0 9704 /* When generating abstract instances, ignore everything except
234c071b
KB
9705 constants, symbols living in memory, and symbols living in
9706 fixed registers. */
c28abdf0
RH
9707 if (! reload_completed)
9708 {
9709 if (rtl
9710 && (CONSTANT_P (rtl)
9711 || (GET_CODE (rtl) == MEM
234c071b
KB
9712 && CONSTANT_P (XEXP (rtl, 0)))
9713 || (GET_CODE (rtl) == REG
9714 && TREE_CODE (decl) == VAR_DECL
9715 && TREE_STATIC (decl))))
4c8c0dec 9716 {
5fd9b178 9717 rtl = targetm.delegitimize_address (rtl);
4c8c0dec
JJ
9718 return rtl;
9719 }
c28abdf0
RH
9720 rtl = NULL_RTX;
9721 }
9722 else if (TREE_CODE (decl) == PARM_DECL)
a3f97cbb
JW
9723 {
9724 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9725 {
d8041cc8
RH
9726 tree declared_type = type_main_variant (TREE_TYPE (decl));
9727 tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
a3f97cbb 9728
71dfc51f 9729 /* This decl represents a formal parameter which was optimized out.
a3f97cbb 9730 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
2ad9852d 9731 all cases where (rtl == NULL_RTX) just below. */
a3f97cbb 9732 if (declared_type == passed_type)
71dfc51f
RK
9733 rtl = DECL_INCOMING_RTL (decl);
9734 else if (! BYTES_BIG_ENDIAN
9735 && TREE_CODE (declared_type) == INTEGER_TYPE
555b6442
HPN
9736 && (GET_MODE_SIZE (TYPE_MODE (declared_type))
9737 <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
556273e0 9738 rtl = DECL_INCOMING_RTL (decl);
a3f97cbb 9739 }
5a904a61
JW
9740
9741 /* If the parm was passed in registers, but lives on the stack, then
9742 make a big endian correction if the mode of the type of the
9743 parameter is not the same as the mode of the rtl. */
9744 /* ??? This is the same series of checks that are made in dbxout.c before
9745 we reach the big endian correction code there. It isn't clear if all
9746 of these checks are necessary here, but keeping them all is the safe
9747 thing to do. */
9748 else if (GET_CODE (rtl) == MEM
9749 && XEXP (rtl, 0) != const0_rtx
9750 && ! CONSTANT_P (XEXP (rtl, 0))
9751 /* Not passed in memory. */
9752 && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
9753 /* Not passed by invisible reference. */
9754 && (GET_CODE (XEXP (rtl, 0)) != REG
9755 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9756 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9757#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9758 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9759#endif
9760 )
9761 /* Big endian correction check. */
9762 && BYTES_BIG_ENDIAN
9763 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9764 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9765 < UNITS_PER_WORD))
9766 {
9767 int offset = (UNITS_PER_WORD
9768 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
2ad9852d 9769
5a904a61
JW
9770 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9771 plus_constant (XEXP (rtl, 0), offset));
9772 }
a3f97cbb 9773 }
8b495402 9774 else if (TREE_CODE (decl) == VAR_DECL
de3c6d93 9775 && rtl
8b495402
DD
9776 && GET_CODE (rtl) == MEM
9777 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
9778 && BYTES_BIG_ENDIAN)
9779 {
9780 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
9781 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
9782
9783 /* If a variable is declared "register" yet is smaller than
9784 a register, then if we store the variable to memory, it
9785 looks like we're storing a register-sized value, when in
9786 fact we are not. We need to adjust the offset of the
9787 storage location to reflect the actual value's bytes,
9788 else gdb will not be able to display it. */
9789 if (rsize > dsize)
9790 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9791 plus_constant (XEXP (rtl, 0), rsize-dsize));
9792 }
71dfc51f 9793
d8041cc8
RH
9794 if (rtl != NULL_RTX)
9795 {
9796 rtl = eliminate_regs (rtl, 0, NULL_RTX);
6a7a9f01 9797#ifdef LEAF_REG_REMAP
d8041cc8
RH
9798 if (current_function_uses_only_leaf_regs)
9799 leaf_renumber_regs_insn (rtl);
6a7a9f01 9800#endif
d8041cc8
RH
9801 }
9802
2ad9852d
RK
9803 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9804 and will have been substituted directly into all expressions that use it.
9805 C does not have such a concept, but C++ and other languages do. */
c28abdf0 9806 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
6d73371a
JJ
9807 {
9808 /* If a variable is initialized with a string constant without embedded
9809 zeros, build CONST_STRING. */
9810 if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9811 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9812 {
9813 tree arrtype = TREE_TYPE (decl);
9814 tree enttype = TREE_TYPE (arrtype);
9815 tree domain = TYPE_DOMAIN (arrtype);
9816 tree init = DECL_INITIAL (decl);
9817 enum machine_mode mode = TYPE_MODE (enttype);
9818
9819 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9820 && domain
9821 && integer_zerop (TYPE_MIN_VALUE (domain))
9822 && compare_tree_int (TYPE_MAX_VALUE (domain),
9823 TREE_STRING_LENGTH (init) - 1) == 0
9824 && ((size_t) TREE_STRING_LENGTH (init)
9825 == strlen (TREE_STRING_POINTER (init)) + 1))
839ee4bc 9826 rtl = gen_rtx_CONST_STRING (VOIDmode, TREE_STRING_POINTER (init));
6d73371a 9827 }
29b91443
JM
9828 /* If the initializer is something that we know will expand into an
9829 immediate RTL constant, expand it now. Expanding anything else
9830 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9831 else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9832 || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
6d73371a
JJ
9833 {
9834 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9835 EXPAND_INITIALIZER);
29b91443 9836 /* If expand_expr returns a MEM, it wasn't immediate. */
6d73371a 9837 if (rtl && GET_CODE (rtl) == MEM)
29b91443 9838 abort ();
6d73371a
JJ
9839 }
9840 }
8063ddcf 9841
4c8c0dec 9842 if (rtl)
5fd9b178 9843 rtl = targetm.delegitimize_address (rtl);
b9203463
RH
9844
9845 /* If we don't look past the constant pool, we risk emitting a
9846 reference to a constant pool entry that isn't referenced from
9847 code, and thus is not emitted. */
9848 if (rtl)
9849 rtl = avoid_constant_pool_reference (rtl);
9850
d8041cc8
RH
9851 return rtl;
9852}
9853
b20b352b 9854/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
d8041cc8
RH
9855 data attribute for a variable or a parameter. We generate the
9856 DW_AT_const_value attribute only in those cases where the given variable
9857 or parameter does not have a true "location" either in memory or in a
9858 register. This can happen (for example) when a constant is passed as an
9859 actual argument in a call to an inline function. (It's possible that
9860 these things can crop up in other ways also.) Note that one type of
9861 constant value which can be passed into an inlined function is a constant
9862 pointer. This can happen for example if an actual argument in an inlined
9863 function call evaluates to a compile-time constant address. */
9864
9865static void
0a2d3d69
DB
9866add_location_or_const_value_attribute (dw_die_ref die, tree decl,
9867 enum dwarf_attribute attr)
d8041cc8 9868{
b3694847 9869 rtx rtl;
b9203463 9870 dw_loc_descr_ref descr;
0a2d3d69 9871 var_loc_list *loc_list;
d8041cc8
RH
9872
9873 if (TREE_CODE (decl) == ERROR_MARK)
9874 return;
6de9cd9a
DN
9875 else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL
9876 && TREE_CODE (decl) != RESULT_DECL)
d8041cc8
RH
9877 abort ();
9878
0a2d3d69
DB
9879 /* See if we possibly have multiple locations for this variable. */
9880 loc_list = lookup_decl_loc (decl);
9881
9882 /* If it truly has multiple locations, the first and last node will
9883 differ. */
9884 if (loc_list && loc_list->first != loc_list->last)
9885 {
9886 const char *secname;
9887 const char *endname;
9888 dw_loc_list_ref list;
9889 rtx varloc;
9890 struct var_loc_node *node;
9891
9892 /* We need to figure out what section we should use as the base
9893 for the address ranges where a given location is valid.
9894 1. If this particular DECL has a section associated with it,
9895 use that.
9896 2. If this function has a section associated with it, use
9897 that.
9898 3. Otherwise, use the text section.
9899 XXX: If you split a variable across multiple sections, this
9900 won't notice. */
9901
9902 if (DECL_SECTION_NAME (decl))
9903 {
9904 tree sectree = DECL_SECTION_NAME (decl);
9905 secname = TREE_STRING_POINTER (sectree);
9906 }
9907 else if (current_function_decl
9908 && DECL_SECTION_NAME (current_function_decl))
9909 {
9910 tree sectree = DECL_SECTION_NAME (current_function_decl);
9911 secname = TREE_STRING_POINTER (sectree);
9912 }
9913 else
e193b408 9914 secname = text_section_label;
0a2d3d69
DB
9915
9916 /* Now that we know what section we are using for a base,
9917 actually construct the list of locations.
9918 The first location information is what is passed to the
9919 function that creates the location list, and the remaining
9920 locations just get added on to that list.
9921 Note that we only know the start address for a location
9922 (IE location changes), so to build the range, we use
9923 the range [current location start, next location start].
9924 This means we have to special case the last node, and generate
9925 a range of [last location start, end of function label]. */
9926
9927 node = loc_list->first;
9928 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9929 list = new_loc_list (loc_descriptor (varloc, attr != DW_AT_frame_base),
9930 node->label, node->next->label, secname, 1);
9931 node = node->next;
9932
9933 for (; node->next; node = node->next)
9934 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
9935 {
9936 /* The variable has a location between NODE->LABEL and
9937 NODE->NEXT->LABEL. */
9938 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9939 add_loc_descr_to_loc_list (&list,
9940 loc_descriptor (varloc,
9941 attr != DW_AT_frame_base),
9942 node->label, node->next->label, secname);
9943 }
9944
9945 /* If the variable has a location at the last label
9946 it keeps its location until the end of function. */
9947 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
9948 {
9949 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
9950
9951 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9952 if (!current_function_decl)
9953 endname = text_end_label;
9954 else
9955 {
9956 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
9957 current_function_funcdef_no);
9958 endname = ggc_strdup (label_id);
9959 }
9960 add_loc_descr_to_loc_list (&list,
9961 loc_descriptor (varloc,
9962 attr != DW_AT_frame_base),
9963 node->label, endname, secname);
9964 }
9965
9966 /* Finally, add the location list to the DIE, and we are done. */
9967 add_AT_loc_list (die, attr, list);
9968 return;
9969 }
9970
d8041cc8 9971 rtl = rtl_for_decl_location (decl);
a97c9600
RH
9972 if (rtl == NULL_RTX)
9973 return;
6a7a9f01 9974
a3f97cbb
JW
9975 switch (GET_CODE (rtl))
9976 {
e9a25f70 9977 case ADDRESSOF:
b9203463
RH
9978 /* The address of a variable that was optimized away;
9979 don't emit anything. */
e9a25f70
JL
9980 break;
9981
a3f97cbb
JW
9982 case CONST_INT:
9983 case CONST_DOUBLE:
e7ee3914 9984 case CONST_VECTOR:
a3f97cbb
JW
9985 case CONST_STRING:
9986 case SYMBOL_REF:
9987 case LABEL_REF:
9988 case CONST:
9989 case PLUS:
9990 /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
9991 add_const_value_attribute (die, rtl);
9992 break;
9993
9994 case MEM:
b9203463
RH
9995 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
9996 {
9997 /* Need loc_descriptor_from_tree since that's where we know
9998 how to handle TLS variables. Want the object's address
9999 since the top-level DW_AT_location assumes such. See
10000 the confusion in loc_descriptor for reference. */
10001 descr = loc_descriptor_from_tree (decl, 1);
10002 }
10003 else
10004 {
10005 case REG:
10006 case SUBREG:
10007 case CONCAT:
0a2d3d69 10008 descr = loc_descriptor (rtl, true);
b9203463 10009 }
0a2d3d69 10010 add_AT_location_description (die, attr, descr);
a3f97cbb 10011 break;
7080f735 10012
d44c7e36
DB
10013 case PARALLEL:
10014 {
10015 rtvec par_elems = XVEC (rtl, 0);
10016 int num_elem = GET_NUM_ELEM (par_elems);
10017 enum machine_mode mode;
10018 int i;
10019
10020 /* Create the first one, so we have something to add to. */
0a2d3d69 10021 descr = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0), true);
d44c7e36
DB
10022 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10023 add_loc_descr (&descr,
10024 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
10025 for (i = 1; i < num_elem; i++)
10026 {
10027 dw_loc_descr_ref temp;
10028
0a2d3d69 10029 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0), true);
d44c7e36
DB
10030 add_loc_descr (&descr, temp);
10031 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10032 add_loc_descr (&descr,
10033 new_loc_descr (DW_OP_piece,
10034 GET_MODE_SIZE (mode), 0));
10035 }
10036 }
10037 add_AT_location_description (die, DW_AT_location, descr);
10038 break;
10039
a3f97cbb 10040 default:
71dfc51f 10041 abort ();
a3f97cbb
JW
10042 }
10043}
10044
1bfb5f8f
JM
10045/* If we don't have a copy of this variable in memory for some reason (such
10046 as a C++ member constant that doesn't have an out-of-line definition),
10047 we should tell the debugger about the constant value. */
10048
10049static void
7080f735 10050tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
1bfb5f8f
JM
10051{
10052 tree init = DECL_INITIAL (decl);
10053 tree type = TREE_TYPE (decl);
10054
10055 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
10056 && initializer_constant_valid_p (init, type) == null_pointer_node)
10057 /* OK */;
10058 else
10059 return;
10060
10061 switch (TREE_CODE (type))
10062 {
10063 case INTEGER_TYPE:
10064 if (host_integerp (init, 0))
10065 add_AT_unsigned (var_die, DW_AT_const_value,
2ad9852d 10066 tree_low_cst (init, 0));
1bfb5f8f
JM
10067 else
10068 add_AT_long_long (var_die, DW_AT_const_value,
10069 TREE_INT_CST_HIGH (init),
10070 TREE_INT_CST_LOW (init));
10071 break;
10072
10073 default:;
10074 }
10075}
0b34cf1e 10076
b20b352b 10077/* Generate a DW_AT_name attribute given some string value to be included as
a3f97cbb 10078 the value of the attribute. */
71dfc51f 10079
c4274b22 10080static void
7080f735 10081add_name_attribute (dw_die_ref die, const char *name_string)
a3f97cbb 10082{
71dfc51f 10083 if (name_string != NULL && *name_string != 0)
14a774a9
RK
10084 {
10085 if (demangle_name_func)
10086 name_string = (*demangle_name_func) (name_string);
10087
10088 add_AT_string (die, DW_AT_name, name_string);
10089 }
a3f97cbb
JW
10090}
10091
b20b352b 10092/* Generate a DW_AT_comp_dir attribute for DIE. */
c4274b22
RH
10093
10094static void
7080f735 10095add_comp_dir_attribute (dw_die_ref die)
c4274b22 10096{
b20d9f0c 10097 const char *wd = get_src_pwd ();
c4274b22
RH
10098 if (wd != NULL)
10099 add_AT_string (die, DW_AT_comp_dir, wd);
10100}
10101
a3f97cbb 10102/* Given a tree node describing an array bound (either lower or upper) output
466446b0 10103 a representation for that bound. */
71dfc51f 10104
a3f97cbb 10105static void
7080f735 10106add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
a3f97cbb 10107{
a3f97cbb
JW
10108 switch (TREE_CODE (bound))
10109 {
10110 case ERROR_MARK:
10111 return;
10112
3ef42a0c 10113 /* All fixed-bounds are represented by INTEGER_CST nodes. */
a3f97cbb 10114 case INTEGER_CST:
665f2503
RK
10115 if (! host_integerp (bound, 0)
10116 || (bound_attr == DW_AT_lower_bound
28985b81 10117 && (((is_c_family () || is_java ()) && integer_zerop (bound))
665f2503 10118 || (is_fortran () && integer_onep (bound)))))
a1105617 10119 /* Use the default. */
665f2503 10120 ;
141719a8 10121 else
665f2503 10122 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
a3f97cbb
JW
10123 break;
10124
b1ccbc24 10125 case CONVERT_EXPR:
a3f97cbb 10126 case NOP_EXPR:
b1ccbc24 10127 case NON_LVALUE_EXPR:
ed239f5a 10128 case VIEW_CONVERT_EXPR:
b1ccbc24
RK
10129 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10130 break;
556273e0 10131
a3f97cbb
JW
10132 case SAVE_EXPR:
10133 /* If optimization is turned on, the SAVE_EXPRs that describe how to
73c68f61
SS
10134 access the upper bound values may be bogus. If they refer to a
10135 register, they may only describe how to get at these values at the
10136 points in the generated code right after they have just been
10137 computed. Worse yet, in the typical case, the upper bound values
10138 will not even *be* computed in the optimized code (though the
10139 number of elements will), so these SAVE_EXPRs are entirely
10140 bogus. In order to compensate for this fact, we check here to see
10141 if optimization is enabled, and if so, we don't add an attribute
10142 for the (unknown and unknowable) upper bound. This should not
10143 cause too much trouble for existing (stupid?) debuggers because
10144 they have to deal with empty upper bounds location descriptions
10145 anyway in order to be able to deal with incomplete array types.
10146 Of course an intelligent debugger (GDB?) should be able to
10147 comprehend that a missing upper bound specification in an array
10148 type used for a storage class `auto' local array variable
10149 indicates that the upper bound is both unknown (at compile- time)
10150 and unknowable (at run-time) due to optimization.
466446b0
JM
10151
10152 We assume that a MEM rtx is safe because gcc wouldn't put the
10153 value there unless it was going to be used repeatedly in the
10154 function, i.e. for cleanups. */
1edf43d6
JM
10155 if (SAVE_EXPR_RTL (bound)
10156 && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
a3f97cbb 10157 {
b3694847 10158 dw_die_ref ctx = lookup_decl_die (current_function_decl);
54ba1f0d 10159 dw_die_ref decl_die = new_die (DW_TAG_variable, ctx, bound);
b3694847 10160 rtx loc = SAVE_EXPR_RTL (bound);
f5963e61
JL
10161
10162 /* If the RTL for the SAVE_EXPR is memory, handle the case where
10163 it references an outer function's frame. */
f5963e61
JL
10164 if (GET_CODE (loc) == MEM)
10165 {
10166 rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
10167
10168 if (XEXP (loc, 0) != new_addr)
c5c76735 10169 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
f5963e61
JL
10170 }
10171
466446b0
JM
10172 add_AT_flag (decl_die, DW_AT_artificial, 1);
10173 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
b9203463 10174 add_AT_location_description (decl_die, DW_AT_location,
0a2d3d69 10175 loc_descriptor (loc, true));
466446b0 10176 add_AT_die_ref (subrange_die, bound_attr, decl_die);
a3f97cbb 10177 }
71dfc51f
RK
10178
10179 /* Else leave out the attribute. */
a3f97cbb 10180 break;
3f76745e 10181
ef76d03b 10182 case VAR_DECL:
d8041cc8 10183 case PARM_DECL:
6de9cd9a 10184 case RESULT_DECL:
d8041cc8
RH
10185 {
10186 dw_die_ref decl_die = lookup_decl_die (bound);
10187
10188 /* ??? Can this happen, or should the variable have been bound
10189 first? Probably it can, since I imagine that we try to create
10190 the types of parameters in the order in which they exist in
0b34cf1e 10191 the list, and won't have created a forward reference to a
d8041cc8
RH
10192 later parameter. */
10193 if (decl_die != NULL)
10194 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10195 break;
10196 }
ef76d03b 10197
3f76745e 10198 default:
d8041cc8
RH
10199 {
10200 /* Otherwise try to create a stack operation procedure to
10201 evaluate the value of the array bound. */
10202
10203 dw_die_ref ctx, decl_die;
10204 dw_loc_descr_ref loc;
10205
10206 loc = loc_descriptor_from_tree (bound, 0);
10207 if (loc == NULL)
10208 break;
10209
e7af1d45
RK
10210 if (current_function_decl == 0)
10211 ctx = comp_unit_die;
10212 else
10213 ctx = lookup_decl_die (current_function_decl);
d8041cc8 10214
aea9695c
RK
10215 /* If we weren't able to find a context, it's most likely the case
10216 that we are processing the return type of the function. So
10217 make a SAVE_EXPR to point to it and have the limbo DIE code
10218 find the proper die. The save_expr function doesn't always
10219 make a SAVE_EXPR, so do it ourselves. */
10220 if (ctx == 0)
10221 bound = build (SAVE_EXPR, TREE_TYPE (bound), bound,
10222 current_function_decl, NULL_TREE);
10223
54ba1f0d 10224 decl_die = new_die (DW_TAG_variable, ctx, bound);
d8041cc8
RH
10225 add_AT_flag (decl_die, DW_AT_artificial, 1);
10226 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10227 add_AT_loc (decl_die, DW_AT_location, loc);
10228
10229 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10230 break;
10231 }
a3f97cbb
JW
10232 }
10233}
10234
10235/* Note that the block of subscript information for an array type also
10236 includes information about the element type of type given array type. */
71dfc51f 10237
a3f97cbb 10238static void
7080f735 10239add_subscript_info (dw_die_ref type_die, tree type)
a3f97cbb 10240{
081f5e7e 10241#ifndef MIPS_DEBUGGING_INFO
b3694847 10242 unsigned dimension_number;
081f5e7e 10243#endif
b3694847
SS
10244 tree lower, upper;
10245 dw_die_ref subrange_die;
a3f97cbb 10246
556273e0 10247 /* The GNU compilers represent multidimensional array types as sequences of
a3f97cbb
JW
10248 one dimensional array types whose element types are themselves array
10249 types. Here we squish that down, so that each multidimensional array
556273e0 10250 type gets only one array_type DIE in the Dwarf debugging info. The draft
a3f97cbb
JW
10251 Dwarf specification say that we are allowed to do this kind of
10252 compression in C (because there is no difference between an array or
556273e0 10253 arrays and a multidimensional array in C) but for other source languages
a3f97cbb 10254 (e.g. Ada) we probably shouldn't do this. */
71dfc51f 10255
a3f97cbb
JW
10256 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10257 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10258 We work around this by disabling this feature. See also
10259 gen_array_type_die. */
10260#ifndef MIPS_DEBUGGING_INFO
10261 for (dimension_number = 0;
10262 TREE_CODE (type) == ARRAY_TYPE;
10263 type = TREE_TYPE (type), dimension_number++)
a3f97cbb 10264#endif
2ad9852d 10265 {
b3694847 10266 tree domain = TYPE_DOMAIN (type);
a3f97cbb
JW
10267
10268 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
556273e0 10269 and (in GNU C only) variable bounds. Handle all three forms
73c68f61 10270 here. */
54ba1f0d 10271 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
a3f97cbb
JW
10272 if (domain)
10273 {
10274 /* We have an array type with specified bounds. */
10275 lower = TYPE_MIN_VALUE (domain);
10276 upper = TYPE_MAX_VALUE (domain);
10277
beb235f8 10278 /* Define the index type. */
a9d38797 10279 if (TREE_TYPE (domain))
ef76d03b
JW
10280 {
10281 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10282 TREE_TYPE field. We can't emit debug info for this
10283 because it is an unnamed integral type. */
10284 if (TREE_CODE (domain) == INTEGER_TYPE
10285 && TYPE_NAME (domain) == NULL_TREE
10286 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10287 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
556273e0 10288 ;
ef76d03b
JW
10289 else
10290 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10291 type_die);
10292 }
a9d38797 10293
e1ee5cdc
RH
10294 /* ??? If upper is NULL, the array has unspecified length,
10295 but it does have a lower bound. This happens with Fortran
10296 dimension arr(N:*)
7080f735 10297 Since the debugger is definitely going to need to know N
e1ee5cdc
RH
10298 to produce useful results, go ahead and output the lower
10299 bound solo, and hope the debugger can cope. */
10300
141719a8 10301 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
e1ee5cdc
RH
10302 if (upper)
10303 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
a3f97cbb 10304 }
71dfc51f 10305
2ad9852d
RK
10306 /* Otherwise we have an array type with an unspecified length. The
10307 DWARF-2 spec does not say how to handle this; let's just leave out the
10308 bounds. */
a3f97cbb 10309 }
a3f97cbb
JW
10310}
10311
10312static void
7080f735 10313add_byte_size_attribute (dw_die_ref die, tree tree_node)
a3f97cbb 10314{
b3694847 10315 unsigned size;
a3f97cbb
JW
10316
10317 switch (TREE_CODE (tree_node))
10318 {
10319 case ERROR_MARK:
10320 size = 0;
10321 break;
10322 case ENUMERAL_TYPE:
10323 case RECORD_TYPE:
10324 case UNION_TYPE:
10325 case QUAL_UNION_TYPE:
10326 size = int_size_in_bytes (tree_node);
10327 break;
10328 case FIELD_DECL:
10329 /* For a data member of a struct or union, the DW_AT_byte_size is
73c68f61
SS
10330 generally given as the number of bytes normally allocated for an
10331 object of the *declared* type of the member itself. This is true
10332 even for bit-fields. */
a3f97cbb
JW
10333 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10334 break;
10335 default:
10336 abort ();
10337 }
10338
10339 /* Note that `size' might be -1 when we get to this point. If it is, that
10340 indicates that the byte size of the entity in question is variable. We
10341 have no good way of expressing this fact in Dwarf at the present time,
10342 so just let the -1 pass on through. */
a3f97cbb
JW
10343 add_AT_unsigned (die, DW_AT_byte_size, size);
10344}
10345
10346/* For a FIELD_DECL node which represents a bit-field, output an attribute
10347 which specifies the distance in bits from the highest order bit of the
10348 "containing object" for the bit-field to the highest order bit of the
10349 bit-field itself.
10350
2ad9852d
RK
10351 For any given bit-field, the "containing object" is a hypothetical object
10352 (of some integral or enum type) within which the given bit-field lives. The
10353 type of this hypothetical "containing object" is always the same as the
10354 declared type of the individual bit-field itself. The determination of the
10355 exact location of the "containing object" for a bit-field is rather
10356 complicated. It's handled by the `field_byte_offset' function (above).
a3f97cbb
JW
10357
10358 Note that it is the size (in bytes) of the hypothetical "containing object"
10359 which will be given in the DW_AT_byte_size attribute for this bit-field.
10360 (See `byte_size_attribute' above). */
71dfc51f
RK
10361
10362static inline void
7080f735 10363add_bit_offset_attribute (dw_die_ref die, tree decl)
a3f97cbb 10364{
665f2503
RK
10365 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10366 tree type = DECL_BIT_FIELD_TYPE (decl);
10367 HOST_WIDE_INT bitpos_int;
10368 HOST_WIDE_INT highest_order_object_bit_offset;
10369 HOST_WIDE_INT highest_order_field_bit_offset;
10370 HOST_WIDE_INT unsigned bit_offset;
a3f97cbb 10371
3a88cbd1
JL
10372 /* Must be a field and a bit field. */
10373 if (!type
10374 || TREE_CODE (decl) != FIELD_DECL)
10375 abort ();
a3f97cbb
JW
10376
10377 /* We can't yet handle bit-fields whose offsets are variable, so if we
10378 encounter such things, just return without generating any attribute
665f2503
RK
10379 whatsoever. Likewise for variable or too large size. */
10380 if (! host_integerp (bit_position (decl), 0)
10381 || ! host_integerp (DECL_SIZE (decl), 1))
71dfc51f
RK
10382 return;
10383
665f2503 10384 bitpos_int = int_bit_position (decl);
a3f97cbb
JW
10385
10386 /* Note that the bit offset is always the distance (in bits) from the
556273e0
KH
10387 highest-order bit of the "containing object" to the highest-order bit of
10388 the bit-field itself. Since the "high-order end" of any object or field
a3f97cbb
JW
10389 is different on big-endian and little-endian machines, the computation
10390 below must take account of these differences. */
10391 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10392 highest_order_field_bit_offset = bitpos_int;
10393
71dfc51f 10394 if (! BYTES_BIG_ENDIAN)
a3f97cbb 10395 {
665f2503 10396 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
a3f97cbb
JW
10397 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10398 }
71dfc51f
RK
10399
10400 bit_offset
10401 = (! BYTES_BIG_ENDIAN
10402 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10403 : highest_order_field_bit_offset - highest_order_object_bit_offset);
a3f97cbb
JW
10404
10405 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10406}
10407
10408/* For a FIELD_DECL node which represents a bit field, output an attribute
10409 which specifies the length in bits of the given field. */
71dfc51f
RK
10410
10411static inline void
7080f735 10412add_bit_size_attribute (dw_die_ref die, tree decl)
a3f97cbb 10413{
3a88cbd1
JL
10414 /* Must be a field and a bit field. */
10415 if (TREE_CODE (decl) != FIELD_DECL
10416 || ! DECL_BIT_FIELD_TYPE (decl))
10417 abort ();
665f2503
RK
10418
10419 if (host_integerp (DECL_SIZE (decl), 1))
10420 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
a3f97cbb
JW
10421}
10422
88dad228 10423/* If the compiled language is ANSI C, then add a 'prototyped'
a3f97cbb 10424 attribute, if arg types are given for the parameters of a function. */
71dfc51f
RK
10425
10426static inline void
7080f735 10427add_prototyped_attribute (dw_die_ref die, tree func_type)
a3f97cbb 10428{
88dad228
JM
10429 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10430 && TYPE_ARG_TYPES (func_type) != NULL)
10431 add_AT_flag (die, DW_AT_prototyped, 1);
a3f97cbb
JW
10432}
10433
a3f97cbb
JW
10434/* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10435 by looking in either the type declaration or object declaration
10436 equate table. */
71dfc51f
RK
10437
10438static inline void
7080f735 10439add_abstract_origin_attribute (dw_die_ref die, tree origin)
a3f97cbb
JW
10440{
10441 dw_die_ref origin_die = NULL;
bbc6ae08 10442
d10b8e05 10443 if (TREE_CODE (origin) != FUNCTION_DECL)
e40a1c67
JM
10444 {
10445 /* We may have gotten separated from the block for the inlined
10446 function, if we're in an exception handler or some such; make
10447 sure that the abstract function has been written out.
10448
73c68f61 10449 Doing this for nested functions is wrong, however; functions are
e40a1c67 10450 distinct units, and our context might not even be inline. */
fb13d4d0 10451 tree fn = origin;
2ad9852d 10452
fb13d4d0
JM
10453 if (TYPE_P (fn))
10454 fn = TYPE_STUB_DECL (fn);
2ad9852d 10455
fb13d4d0 10456 fn = decl_function_context (fn);
e40a1c67 10457 if (fn)
1edf43d6 10458 dwarf2out_abstract_function (fn);
e40a1c67 10459 }
44db1d9c 10460
2f939d94 10461 if (DECL_P (origin))
71dfc51f 10462 origin_die = lookup_decl_die (origin);
2f939d94 10463 else if (TYPE_P (origin))
71dfc51f
RK
10464 origin_die = lookup_type_die (origin);
10465
bbc6ae08 10466 if (origin_die == NULL)
1ae8994f 10467 abort ();
556273e0 10468
a3f97cbb
JW
10469 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10470}
10471
bdb669cb
JM
10472/* We do not currently support the pure_virtual attribute. */
10473
71dfc51f 10474static inline void
7080f735 10475add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
a3f97cbb 10476{
a94dbf2c 10477 if (DECL_VINDEX (func_decl))
a3f97cbb 10478 {
bdb669cb 10479 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
665f2503
RK
10480
10481 if (host_integerp (DECL_VINDEX (func_decl), 0))
10482 add_AT_loc (die, DW_AT_vtable_elem_location,
10483 new_loc_descr (DW_OP_constu,
10484 tree_low_cst (DECL_VINDEX (func_decl), 0),
10485 0));
71dfc51f 10486
a94dbf2c
JM
10487 /* GNU extension: Record what type this method came from originally. */
10488 if (debug_info_level > DINFO_LEVEL_TERSE)
10489 add_AT_die_ref (die, DW_AT_containing_type,
10490 lookup_type_die (DECL_CONTEXT (func_decl)));
a3f97cbb
JW
10491 }
10492}
10493\f
b2932ae5 10494/* Add source coordinate attributes for the given decl. */
71dfc51f 10495
b2932ae5 10496static void
7080f735 10497add_src_coords_attributes (dw_die_ref die, tree decl)
b2932ae5 10498{
f31686a3 10499 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
71dfc51f 10500
b2932ae5 10501 add_AT_unsigned (die, DW_AT_decl_file, file_index);
f31686a3 10502 add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
b2932ae5
JM
10503}
10504
b20b352b 10505/* Add a DW_AT_name attribute and source coordinate attribute for the
a3f97cbb 10506 given decl, but only if it actually has a name. */
71dfc51f 10507
a3f97cbb 10508static void
7080f735 10509add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
a3f97cbb 10510{
b3694847 10511 tree decl_name;
71dfc51f 10512
556273e0 10513 decl_name = DECL_NAME (decl);
71dfc51f 10514 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
a3f97cbb 10515 {
a1d7ffe3 10516 add_name_attribute (die, dwarf2_name (decl, 0));
a96c67ec
JM
10517 if (! DECL_ARTIFICIAL (decl))
10518 add_src_coords_attributes (die, decl);
e689ae67 10519
a1d7ffe3 10520 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
bc808e0b 10521 && TREE_PUBLIC (decl)
5daf7c0a
JM
10522 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10523 && !DECL_ABSTRACT (decl))
a1d7ffe3
JM
10524 add_AT_string (die, DW_AT_MIPS_linkage_name,
10525 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
a3f97cbb 10526 }
7a0c8d71
DR
10527
10528#ifdef VMS_DEBUGGING_INFO
7a0c8d71
DR
10529 /* Get the function's name, as described by its RTL. This may be different
10530 from the DECL_NAME name used in the source file. */
10531 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
c470afad
RK
10532 {
10533 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10534 XEXP (DECL_RTL (decl), 0));
10535 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
10536 }
7a0c8d71 10537#endif
a3f97cbb
JW
10538}
10539
556273e0 10540/* Push a new declaration scope. */
71dfc51f 10541
a3f97cbb 10542static void
7080f735 10543push_decl_scope (tree scope)
a3f97cbb 10544{
244a4af0 10545 VARRAY_PUSH_TREE (decl_scope_table, scope);
a3f97cbb
JW
10546}
10547
777ad4c2 10548/* Pop a declaration scope. */
2ad9852d 10549
777ad4c2 10550static inline void
7080f735 10551pop_decl_scope (void)
777ad4c2 10552{
244a4af0 10553 if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
777ad4c2 10554 abort ();
2ad9852d 10555
244a4af0 10556 VARRAY_POP (decl_scope_table);
777ad4c2
JM
10557}
10558
10559/* Return the DIE for the scope that immediately contains this type.
10560 Non-named types get global scope. Named types nested in other
10561 types get their containing scope if it's open, or global scope
10562 otherwise. All other types (i.e. function-local named types) get
10563 the current active scope. */
71dfc51f 10564
a3f97cbb 10565static dw_die_ref
7080f735 10566scope_die_for (tree t, dw_die_ref context_die)
a3f97cbb 10567{
b3694847
SS
10568 dw_die_ref scope_die = NULL;
10569 tree containing_scope;
10570 int i;
a3f97cbb 10571
777ad4c2
JM
10572 /* Non-types always go in the current scope. */
10573 if (! TYPE_P (t))
10574 abort ();
10575
10576 containing_scope = TYPE_CONTEXT (t);
ab72d377 10577
66c78aa9 10578 /* Use the containing namespace if it was passed in (for a declaration). */
2addbe1d 10579 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
66c78aa9
JM
10580 {
10581 if (context_die == lookup_decl_die (containing_scope))
10582 /* OK */;
10583 else
10584 containing_scope = NULL_TREE;
10585 }
2addbe1d 10586
5f2f160c
JM
10587 /* Ignore function type "scopes" from the C frontend. They mean that
10588 a tagged type is local to a parmlist of a function declarator, but
10589 that isn't useful to DWARF. */
10590 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10591 containing_scope = NULL_TREE;
10592
71dfc51f
RK
10593 if (containing_scope == NULL_TREE)
10594 scope_die = comp_unit_die;
777ad4c2 10595 else if (TYPE_P (containing_scope))
348bb3c7 10596 {
777ad4c2
JM
10597 /* For types, we can just look up the appropriate DIE. But
10598 first we check to see if we're in the middle of emitting it
10599 so we know where the new DIE should go. */
244a4af0
TF
10600 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10601 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
348bb3c7
JM
10602 break;
10603
10604 if (i < 0)
10605 {
348bb3c7
JM
10606 if (debug_info_level > DINFO_LEVEL_TERSE
10607 && !TREE_ASM_WRITTEN (containing_scope))
10608 abort ();
10609
10610 /* If none of the current dies are suitable, we get file scope. */
10611 scope_die = comp_unit_die;
10612 }
10613 else
777ad4c2 10614 scope_die = lookup_type_die (containing_scope);
348bb3c7 10615 }
a3f97cbb 10616 else
777ad4c2 10617 scope_die = context_die;
71dfc51f 10618
a3f97cbb
JW
10619 return scope_die;
10620}
10621
2ad9852d 10622/* Returns nonzero if CONTEXT_DIE is internal to a function. */
777ad4c2
JM
10623
10624static inline int
7080f735 10625local_scope_p (dw_die_ref context_die)
a3f97cbb 10626{
777ad4c2
JM
10627 for (; context_die; context_die = context_die->die_parent)
10628 if (context_die->die_tag == DW_TAG_inlined_subroutine
10629 || context_die->die_tag == DW_TAG_subprogram)
10630 return 1;
2ad9852d 10631
777ad4c2 10632 return 0;
a3f97cbb
JW
10633}
10634
66c78aa9
JM
10635/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10636 whether or not to treat a DIE in this context as a declaration. */
9765e357
JM
10637
10638static inline int
66c78aa9 10639class_or_namespace_scope_p (dw_die_ref context_die)
9765e357
JM
10640{
10641 return (context_die
10642 && (context_die->die_tag == DW_TAG_structure_type
66c78aa9
JM
10643 || context_die->die_tag == DW_TAG_union_type
10644 || context_die->die_tag == DW_TAG_namespace));
9765e357
JM
10645}
10646
a3f97cbb
JW
10647/* Many forms of DIEs require a "type description" attribute. This
10648 routine locates the proper "type descriptor" die for the type given
b20b352b 10649 by 'type', and adds a DW_AT_type attribute below the given die. */
71dfc51f 10650
a3f97cbb 10651static void
7080f735
AJ
10652add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10653 int decl_volatile, dw_die_ref context_die)
a3f97cbb 10654{
b3694847
SS
10655 enum tree_code code = TREE_CODE (type);
10656 dw_die_ref type_die = NULL;
a3f97cbb 10657
ef76d03b
JW
10658 /* ??? If this type is an unnamed subrange type of an integral or
10659 floating-point type, use the inner type. This is because we have no
10660 support for unnamed types in base_type_die. This can happen if this is
10661 an Ada subrange type. Correct solution is emit a subrange type die. */
b1ccbc24
RK
10662 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10663 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10664 type = TREE_TYPE (type), code = TREE_CODE (type);
10665
2ad9852d
RK
10666 if (code == ERROR_MARK
10667 /* Handle a special case. For functions whose return type is void, we
10668 generate *no* type attribute. (Note that no object may have type
10669 `void', so this only applies to function return types). */
10670 || code == VOID_TYPE)
b1ccbc24 10671 return;
a3f97cbb 10672
a3f97cbb
JW
10673 type_die = modified_type_die (type,
10674 decl_const || TYPE_READONLY (type),
10675 decl_volatile || TYPE_VOLATILE (type),
ab72d377 10676 context_die);
2ad9852d 10677
a3f97cbb 10678 if (type_die != NULL)
71dfc51f 10679 add_AT_die_ref (object_die, DW_AT_type, type_die);
a3f97cbb
JW
10680}
10681
10682/* Given a tree pointer to a struct, class, union, or enum type node, return
10683 a pointer to the (string) tag name for the given type, or zero if the type
10684 was declared without a tag. */
71dfc51f 10685
d3e3972c 10686static const char *
7080f735 10687type_tag (tree type)
a3f97cbb 10688{
b3694847 10689 const char *name = 0;
a3f97cbb
JW
10690
10691 if (TYPE_NAME (type) != 0)
10692 {
b3694847 10693 tree t = 0;
a3f97cbb
JW
10694
10695 /* Find the IDENTIFIER_NODE for the type name. */
10696 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10697 t = TYPE_NAME (type);
bdb669cb 10698
556273e0 10699 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
73c68f61
SS
10700 a TYPE_DECL node, regardless of whether or not a `typedef' was
10701 involved. */
a94dbf2c
JM
10702 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10703 && ! DECL_IGNORED_P (TYPE_NAME (type)))
a3f97cbb 10704 t = DECL_NAME (TYPE_NAME (type));
bdb669cb 10705
a3f97cbb
JW
10706 /* Now get the name as a string, or invent one. */
10707 if (t != 0)
a94dbf2c 10708 name = IDENTIFIER_POINTER (t);
a3f97cbb 10709 }
71dfc51f 10710
a3f97cbb
JW
10711 return (name == 0 || *name == '\0') ? 0 : name;
10712}
10713
10714/* Return the type associated with a data member, make a special check
10715 for bit field types. */
71dfc51f
RK
10716
10717static inline tree
7080f735 10718member_declared_type (tree member)
a3f97cbb 10719{
71dfc51f 10720 return (DECL_BIT_FIELD_TYPE (member)
2ad9852d 10721 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
a3f97cbb
JW
10722}
10723
d291dd49 10724/* Get the decl's label, as described by its RTL. This may be different
a3f97cbb 10725 from the DECL_NAME name used in the source file. */
71dfc51f 10726
487a6e06 10727#if 0
d3e3972c 10728static const char *
7080f735 10729decl_start_label (tree decl)
a3f97cbb
JW
10730{
10731 rtx x;
d3e3972c 10732 const char *fnname;
2ad9852d 10733
a3f97cbb
JW
10734 x = DECL_RTL (decl);
10735 if (GET_CODE (x) != MEM)
71dfc51f
RK
10736 abort ();
10737
a3f97cbb
JW
10738 x = XEXP (x, 0);
10739 if (GET_CODE (x) != SYMBOL_REF)
71dfc51f
RK
10740 abort ();
10741
a3f97cbb
JW
10742 fnname = XSTR (x, 0);
10743 return fnname;
10744}
487a6e06 10745#endif
a3f97cbb 10746\f
956d6950 10747/* These routines generate the internal representation of the DIE's for
a3f97cbb 10748 the compilation unit. Debugging information is collected by walking
88dad228 10749 the declaration trees passed in from dwarf2out_decl(). */
a3f97cbb
JW
10750
10751static void
7080f735 10752gen_array_type_die (tree type, dw_die_ref context_die)
a3f97cbb 10753{
b3694847
SS
10754 dw_die_ref scope_die = scope_die_for (type, context_die);
10755 dw_die_ref array_die;
10756 tree element_type;
bdb669cb 10757
a9d38797
JM
10758 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10759 the inner array type comes before the outer array type. Thus we must
10760 call gen_type_die before we call new_die. See below also. */
10761#ifdef MIPS_DEBUGGING_INFO
10762 gen_type_die (TREE_TYPE (type), context_die);
10763#endif
10764
54ba1f0d 10765 array_die = new_die (DW_TAG_array_type, scope_die, type);
84f0ace0
JM
10766 add_name_attribute (array_die, type_tag (type));
10767 equate_type_number_to_die (type, array_die);
10768
10769 if (TREE_CODE (type) == VECTOR_TYPE)
10770 {
10771 /* The frontend feeds us a representation for the vector as a struct
10772 containing an array. Pull out the array type. */
10773 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10774 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10775 }
a9d38797 10776
a3f97cbb
JW
10777#if 0
10778 /* We default the array ordering. SDB will probably do
10779 the right things even if DW_AT_ordering is not present. It's not even
10780 an issue until we start to get into multidimensional arrays anyway. If
10781 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10782 then we'll have to put the DW_AT_ordering attribute back in. (But if
10783 and when we find out that we need to put these in, we will only do so
10784 for multidimensional arrays. */
10785 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10786#endif
10787
a9d38797 10788#ifdef MIPS_DEBUGGING_INFO
4edb7b60
JM
10789 /* The SGI compilers handle arrays of unknown bound by setting
10790 AT_declaration and not emitting any subrange DIEs. */
a9d38797 10791 if (! TYPE_DOMAIN (type))
371e8c4f 10792 add_AT_flag (array_die, DW_AT_declaration, 1);
a9d38797
JM
10793 else
10794#endif
10795 add_subscript_info (array_die, type);
a3f97cbb 10796
a3f97cbb
JW
10797 /* Add representation of the type of the elements of this array type. */
10798 element_type = TREE_TYPE (type);
71dfc51f 10799
a3f97cbb
JW
10800 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10801 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10802 We work around this by disabling this feature. See also
10803 add_subscript_info. */
10804#ifndef MIPS_DEBUGGING_INFO
71dfc51f
RK
10805 while (TREE_CODE (element_type) == ARRAY_TYPE)
10806 element_type = TREE_TYPE (element_type);
10807
a3f97cbb 10808 gen_type_die (element_type, context_die);
a9d38797 10809#endif
a3f97cbb
JW
10810
10811 add_type_attribute (array_die, element_type, 0, 0, context_die);
10812}
10813
10814static void
7080f735 10815gen_set_type_die (tree type, dw_die_ref context_die)
a3f97cbb 10816{
b3694847 10817 dw_die_ref type_die
54ba1f0d 10818 = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
71dfc51f 10819
a3f97cbb 10820 equate_type_number_to_die (type, type_die);
a3f97cbb
JW
10821 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
10822}
10823
d6f4ec51 10824#if 0
a3f97cbb 10825static void
7080f735 10826gen_entry_point_die (tree decl, dw_die_ref context_die)
a3f97cbb 10827{
b3694847 10828 tree origin = decl_ultimate_origin (decl);
54ba1f0d 10829 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
2ad9852d 10830
a3f97cbb 10831 if (origin != NULL)
71dfc51f 10832 add_abstract_origin_attribute (decl_die, origin);
a3f97cbb
JW
10833 else
10834 {
10835 add_name_and_src_coords_attributes (decl_die, decl);
a3f97cbb
JW
10836 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10837 0, 0, context_die);
10838 }
71dfc51f 10839
a3f97cbb 10840 if (DECL_ABSTRACT (decl))
71dfc51f 10841 equate_decl_number_to_die (decl, decl_die);
a3f97cbb 10842 else
71dfc51f 10843 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
a3f97cbb 10844}
d6f4ec51 10845#endif
a3f97cbb 10846
8a8c3656
JM
10847/* Walk through the list of incomplete types again, trying once more to
10848 emit full debugging info for them. */
10849
10850static void
7080f735 10851retry_incomplete_types (void)
8a8c3656 10852{
244a4af0 10853 int i;
2ad9852d 10854
244a4af0 10855 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
2ad9852d 10856 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
8a8c3656
JM
10857}
10858
a3f97cbb 10859/* Generate a DIE to represent an inlined instance of an enumeration type. */
71dfc51f 10860
a3f97cbb 10861static void
7080f735 10862gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
a3f97cbb 10863{
54ba1f0d 10864 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
2ad9852d 10865
bbc6ae08
NC
10866 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10867 be incomplete and such types are not marked. */
a3f97cbb
JW
10868 add_abstract_origin_attribute (type_die, type);
10869}
10870
10871/* Generate a DIE to represent an inlined instance of a structure type. */
71dfc51f 10872
a3f97cbb 10873static void
7080f735 10874gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
a3f97cbb 10875{
54ba1f0d 10876 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
777ad4c2 10877
bbc6ae08
NC
10878 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10879 be incomplete and such types are not marked. */
a3f97cbb
JW
10880 add_abstract_origin_attribute (type_die, type);
10881}
10882
10883/* Generate a DIE to represent an inlined instance of a union type. */
71dfc51f 10884
a3f97cbb 10885static void
7080f735 10886gen_inlined_union_type_die (tree type, dw_die_ref context_die)
a3f97cbb 10887{
54ba1f0d 10888 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
777ad4c2 10889
bbc6ae08
NC
10890 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10891 be incomplete and such types are not marked. */
a3f97cbb
JW
10892 add_abstract_origin_attribute (type_die, type);
10893}
10894
10895/* Generate a DIE to represent an enumeration type. Note that these DIEs
10896 include all of the information about the enumeration values also. Each
273dbe67
JM
10897 enumerated type name/value is listed as a child of the enumerated type
10898 DIE. */
71dfc51f 10899
de99511b 10900static dw_die_ref
7080f735 10901gen_enumeration_type_die (tree type, dw_die_ref context_die)
a3f97cbb 10902{
b3694847 10903 dw_die_ref type_die = lookup_type_die (type);
273dbe67 10904
a3f97cbb
JW
10905 if (type_die == NULL)
10906 {
10907 type_die = new_die (DW_TAG_enumeration_type,
54ba1f0d 10908 scope_die_for (type, context_die), type);
a3f97cbb
JW
10909 equate_type_number_to_die (type, type_die);
10910 add_name_attribute (type_die, type_tag (type));
a3f97cbb 10911 }
273dbe67 10912 else if (! TYPE_SIZE (type))
de99511b 10913 return type_die;
273dbe67
JM
10914 else
10915 remove_AT (type_die, DW_AT_declaration);
10916
10917 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
10918 given enum type is incomplete, do not generate the DW_AT_byte_size
10919 attribute or the DW_AT_element_list attribute. */
10920 if (TYPE_SIZE (type))
a3f97cbb 10921 {
b3694847 10922 tree link;
71dfc51f 10923
a082c85a 10924 TREE_ASM_WRITTEN (type) = 1;
273dbe67 10925 add_byte_size_attribute (type_die, type);
e9a25f70 10926 if (TYPE_STUB_DECL (type) != NULL_TREE)
b2932ae5 10927 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
71dfc51f 10928
ef76d03b
JW
10929 /* If the first reference to this type was as the return type of an
10930 inline function, then it may not have a parent. Fix this now. */
10931 if (type_die->die_parent == NULL)
10932 add_child_die (scope_die_for (type, context_die), type_die);
10933
eb34af89 10934 for (link = TYPE_VALUES (type);
273dbe67 10935 link != NULL; link = TREE_CHAIN (link))
a3f97cbb 10936 {
54ba1f0d 10937 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
5bb2ed2c 10938 tree value = TREE_VALUE (link);
71dfc51f 10939
273dbe67
JM
10940 add_name_attribute (enum_die,
10941 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
665f2503 10942
8df83eae 10943 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
5bb2ed2c
MM
10944 /* DWARF2 does not provide a way of indicating whether or
10945 not enumeration constants are signed or unsigned. GDB
10946 always assumes the values are signed, so we output all
10947 values as if they were signed. That means that
10948 enumeration constants with very large unsigned values
10949 will appear to have negative values in the debugger. */
10950 add_AT_int (enum_die, DW_AT_const_value,
10951 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
a3f97cbb
JW
10952 }
10953 }
273dbe67
JM
10954 else
10955 add_AT_flag (type_die, DW_AT_declaration, 1);
de99511b
B
10956
10957 return type_die;
a3f97cbb
JW
10958}
10959
a3f97cbb
JW
10960/* Generate a DIE to represent either a real live formal parameter decl or to
10961 represent just the type of some formal parameter position in some function
10962 type.
71dfc51f 10963
a3f97cbb
JW
10964 Note that this routine is a bit unusual because its argument may be a
10965 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
10966 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
10967 node. If it's the former then this function is being called to output a
10968 DIE to represent a formal parameter object (or some inlining thereof). If
10969 it's the latter, then this function is only being called to output a
10970 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
10971 argument type of some subprogram type. */
71dfc51f 10972
a94dbf2c 10973static dw_die_ref
7080f735 10974gen_formal_parameter_die (tree node, dw_die_ref context_die)
a3f97cbb 10975{
b3694847 10976 dw_die_ref parm_die
54ba1f0d 10977 = new_die (DW_TAG_formal_parameter, context_die, node);
b3694847 10978 tree origin;
71dfc51f 10979
a3f97cbb
JW
10980 switch (TREE_CODE_CLASS (TREE_CODE (node)))
10981 {
a3f97cbb
JW
10982 case 'd':
10983 origin = decl_ultimate_origin (node);
10984 if (origin != NULL)
a94dbf2c 10985 add_abstract_origin_attribute (parm_die, origin);
a3f97cbb
JW
10986 else
10987 {
10988 add_name_and_src_coords_attributes (parm_die, node);
10989 add_type_attribute (parm_die, TREE_TYPE (node),
10990 TREE_READONLY (node),
10991 TREE_THIS_VOLATILE (node),
10992 context_die);
bdb669cb
JM
10993 if (DECL_ARTIFICIAL (node))
10994 add_AT_flag (parm_die, DW_AT_artificial, 1);
a3f97cbb 10995 }
71dfc51f 10996
141719a8
JM
10997 equate_decl_number_to_die (node, parm_die);
10998 if (! DECL_ABSTRACT (node))
0a2d3d69 10999 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
71dfc51f 11000
a3f97cbb
JW
11001 break;
11002
a3f97cbb 11003 case 't':
71dfc51f 11004 /* We were called with some kind of a ..._TYPE node. */
a3f97cbb
JW
11005 add_type_attribute (parm_die, node, 0, 0, context_die);
11006 break;
11007
a3f97cbb
JW
11008 default:
11009 abort ();
11010 }
71dfc51f 11011
a94dbf2c 11012 return parm_die;
a3f97cbb
JW
11013}
11014
11015/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11016 at the end of an (ANSI prototyped) formal parameters list. */
71dfc51f 11017
a3f97cbb 11018static void
7080f735 11019gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
a3f97cbb 11020{
54ba1f0d 11021 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
a3f97cbb
JW
11022}
11023
11024/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11025 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11026 parameters as specified in some function type specification (except for
1cfdcc15 11027 those which appear as part of a function *definition*). */
71dfc51f 11028
a3f97cbb 11029static void
7080f735 11030gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
a3f97cbb 11031{
b3694847
SS
11032 tree link;
11033 tree formal_type = NULL;
11034 tree first_parm_type;
5daf7c0a 11035 tree arg;
a3f97cbb 11036
5daf7c0a
JM
11037 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11038 {
11039 arg = DECL_ARGUMENTS (function_or_method_type);
11040 function_or_method_type = TREE_TYPE (function_or_method_type);
11041 }
11042 else
11043 arg = NULL_TREE;
c26fbbca 11044
5daf7c0a 11045 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
a3f97cbb 11046
556273e0 11047 /* Make our first pass over the list of formal parameter types and output a
a3f97cbb 11048 DW_TAG_formal_parameter DIE for each one. */
5daf7c0a 11049 for (link = first_parm_type; link; )
a3f97cbb 11050 {
b3694847 11051 dw_die_ref parm_die;
556273e0 11052
a3f97cbb
JW
11053 formal_type = TREE_VALUE (link);
11054 if (formal_type == void_type_node)
11055 break;
11056
11057 /* Output a (nameless) DIE to represent the formal parameter itself. */
a94dbf2c 11058 parm_die = gen_formal_parameter_die (formal_type, context_die);
5daf7c0a
JM
11059 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11060 && link == first_parm_type)
11061 || (arg && DECL_ARTIFICIAL (arg)))
a94dbf2c 11062 add_AT_flag (parm_die, DW_AT_artificial, 1);
5daf7c0a
JM
11063
11064 link = TREE_CHAIN (link);
11065 if (arg)
11066 arg = TREE_CHAIN (arg);
a3f97cbb
JW
11067 }
11068
11069 /* If this function type has an ellipsis, add a
11070 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11071 if (formal_type != void_type_node)
11072 gen_unspecified_parameters_die (function_or_method_type, context_die);
11073
556273e0 11074 /* Make our second (and final) pass over the list of formal parameter types
a3f97cbb
JW
11075 and output DIEs to represent those types (as necessary). */
11076 for (link = TYPE_ARG_TYPES (function_or_method_type);
2ad9852d 11077 link && TREE_VALUE (link);
a3f97cbb 11078 link = TREE_CHAIN (link))
2ad9852d 11079 gen_type_die (TREE_VALUE (link), context_die);
a3f97cbb
JW
11080}
11081
10a11b75
JM
11082/* We want to generate the DIE for TYPE so that we can generate the
11083 die for MEMBER, which has been defined; we will need to refer back
11084 to the member declaration nested within TYPE. If we're trying to
11085 generate minimal debug info for TYPE, processing TYPE won't do the
11086 trick; we need to attach the member declaration by hand. */
11087
11088static void
7080f735 11089gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
10a11b75
JM
11090{
11091 gen_type_die (type, context_die);
11092
11093 /* If we're trying to avoid duplicate debug info, we may not have
11094 emitted the member decl for this function. Emit it now. */
11095 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11096 && ! lookup_decl_die (member))
11097 {
11098 if (decl_ultimate_origin (member))
11099 abort ();
11100
11101 push_decl_scope (type);
11102 if (TREE_CODE (member) == FUNCTION_DECL)
11103 gen_subprogram_die (member, lookup_type_die (type));
11104 else
11105 gen_variable_die (member, lookup_type_die (type));
2ad9852d 11106
10a11b75
JM
11107 pop_decl_scope ();
11108 }
11109}
11110
2ad9852d
RK
11111/* Generate the DWARF2 info for the "abstract" instance of a function which we
11112 may later generate inlined and/or out-of-line instances of. */
10a11b75 11113
e1772ac0 11114static void
7080f735 11115dwarf2out_abstract_function (tree decl)
10a11b75 11116{
b3694847 11117 dw_die_ref old_die;
777ad4c2 11118 tree save_fn;
5daf7c0a
JM
11119 tree context;
11120 int was_abstract = DECL_ABSTRACT (decl);
11121
11122 /* Make sure we have the actual abstract inline, not a clone. */
11123 decl = DECL_ORIGIN (decl);
10a11b75 11124
c26fbbca 11125 old_die = lookup_decl_die (decl);
ae0f3477 11126 if (old_die && get_AT (old_die, DW_AT_inline))
10a11b75
JM
11127 /* We've already generated the abstract instance. */
11128 return;
11129
5daf7c0a
JM
11130 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11131 we don't get confused by DECL_ABSTRACT. */
8458e954
JS
11132 if (debug_info_level > DINFO_LEVEL_TERSE)
11133 {
11134 context = decl_class_context (decl);
11135 if (context)
11136 gen_type_die_for_member
11137 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11138 }
c26fbbca 11139
5daf7c0a 11140 /* Pretend we've just finished compiling this function. */
777ad4c2
JM
11141 save_fn = current_function_decl;
11142 current_function_decl = decl;
11143
10a11b75
JM
11144 set_decl_abstract_flags (decl, 1);
11145 dwarf2out_decl (decl);
5daf7c0a
JM
11146 if (! was_abstract)
11147 set_decl_abstract_flags (decl, 0);
777ad4c2
JM
11148
11149 current_function_decl = save_fn;
10a11b75
JM
11150}
11151
a3f97cbb
JW
11152/* Generate a DIE to represent a declared function (either file-scope or
11153 block-local). */
71dfc51f 11154
a3f97cbb 11155static void
7080f735 11156gen_subprogram_die (tree decl, dw_die_ref context_die)
a3f97cbb
JW
11157{
11158 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
b3694847
SS
11159 tree origin = decl_ultimate_origin (decl);
11160 dw_die_ref subr_die;
11161 rtx fp_reg;
11162 tree fn_arg_types;
11163 tree outer_scope;
11164 dw_die_ref old_die = lookup_decl_die (decl);
11165 int declaration = (current_function_decl != decl
66c78aa9 11166 || class_or_namespace_scope_p (context_die));
a3f97cbb 11167
2ad9852d
RK
11168 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11169 started to generate the abstract instance of an inline, decided to output
11170 its containing class, and proceeded to emit the declaration of the inline
11171 from the member list for the class. If so, DECLARATION takes priority;
11172 we'll get back to the abstract instance when done with the class. */
10a11b75 11173
1cfdcc15 11174 /* The class-scope declaration DIE must be the primary DIE. */
66c78aa9 11175 if (origin && declaration && class_or_namespace_scope_p (context_die))
1cfdcc15
JM
11176 {
11177 origin = NULL;
11178 if (old_die)
11179 abort ();
11180 }
11181
a3f97cbb
JW
11182 if (origin != NULL)
11183 {
777ad4c2 11184 if (declaration && ! local_scope_p (context_die))
10a11b75
JM
11185 abort ();
11186
8d8238b6
JM
11187 /* Fixup die_parent for the abstract instance of a nested
11188 inline function. */
11189 if (old_die && old_die->die_parent == NULL)
11190 add_child_die (context_die, old_die);
11191
54ba1f0d 11192 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
a3f97cbb
JW
11193 add_abstract_origin_attribute (subr_die, origin);
11194 }
bdb669cb
JM
11195 else if (old_die)
11196 {
f31686a3 11197 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
a94dbf2c 11198
1edf43d6
JM
11199 if (!get_AT_flag (old_die, DW_AT_declaration)
11200 /* We can have a normal definition following an inline one in the
11201 case of redefinition of GNU C extern inlines.
11202 It seems reasonable to use AT_specification in this case. */
ae0f3477 11203 && !get_AT (old_die, DW_AT_inline))
b75ab88b
NC
11204 {
11205 /* ??? This can happen if there is a bug in the program, for
11206 instance, if it has duplicate function definitions. Ideally,
11207 we should detect this case and ignore it. For now, if we have
11208 already reported an error, any error at all, then assume that
4fe9b91c 11209 we got here because of an input error, not a dwarf2 bug. */
b75ab88b
NC
11210 if (errorcount)
11211 return;
11212 abort ();
11213 }
4b674448
JM
11214
11215 /* If the definition comes from the same place as the declaration,
a94dbf2c
JM
11216 maybe use the old DIE. We always want the DIE for this function
11217 that has the *_pc attributes to be under comp_unit_die so the
cb9e9d8d
JM
11218 debugger can find it. We also need to do this for abstract
11219 instances of inlines, since the spec requires the out-of-line copy
11220 to have the same parent. For local class methods, this doesn't
11221 apply; we just use the old DIE. */
11222 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
a96c67ec
JM
11223 && (DECL_ARTIFICIAL (decl)
11224 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11225 && (get_AT_unsigned (old_die, DW_AT_decl_line)
f31686a3 11226 == (unsigned) DECL_SOURCE_LINE (decl)))))
bdb669cb 11227 {
4b674448
JM
11228 subr_die = old_die;
11229
6097b0c3
DP
11230 /* Clear out the declaration attribute and the formal parameters.
11231 Do not remove all children, because it is possible that this
11232 declaration die was forced using force_decl_die(). In such
11233 cases die that forced declaration die (e.g. TAG_imported_module)
11234 is one of the children that we do not want to remove. */
4b674448 11235 remove_AT (subr_die, DW_AT_declaration);
6097b0c3 11236 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
4b674448
JM
11237 }
11238 else
11239 {
54ba1f0d 11240 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
47fcfa7b 11241 add_AT_specification (subr_die, old_die);
bdb669cb
JM
11242 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11243 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11244 if (get_AT_unsigned (old_die, DW_AT_decl_line)
f31686a3 11245 != (unsigned) DECL_SOURCE_LINE (decl))
bdb669cb 11246 add_AT_unsigned
f31686a3 11247 (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
bdb669cb
JM
11248 }
11249 }
a3f97cbb
JW
11250 else
11251 {
54ba1f0d 11252 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
556273e0 11253
273dbe67
JM
11254 if (TREE_PUBLIC (decl))
11255 add_AT_flag (subr_die, DW_AT_external, 1);
71dfc51f 11256
a3f97cbb 11257 add_name_and_src_coords_attributes (subr_die, decl);
4927276d
JM
11258 if (debug_info_level > DINFO_LEVEL_TERSE)
11259 {
2ad9852d
RK
11260 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11261 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11262 0, 0, context_die);
4927276d 11263 }
71dfc51f 11264
a3f97cbb 11265 add_pure_or_virtual_attribute (subr_die, decl);
273dbe67
JM
11266 if (DECL_ARTIFICIAL (decl))
11267 add_AT_flag (subr_die, DW_AT_artificial, 1);
2ad9852d 11268
a94dbf2c
JM
11269 if (TREE_PROTECTED (decl))
11270 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11271 else if (TREE_PRIVATE (decl))
11272 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
a3f97cbb 11273 }
4edb7b60 11274
a94dbf2c
JM
11275 if (declaration)
11276 {
ae0f3477 11277 if (!old_die || !get_AT (old_die, DW_AT_inline))
1edf43d6
JM
11278 {
11279 add_AT_flag (subr_die, DW_AT_declaration, 1);
11280
11281 /* The first time we see a member function, it is in the context of
11282 the class to which it belongs. We make sure of this by emitting
11283 the class first. The next time is the definition, which is
6097b0c3
DP
11284 handled above. The two may come from the same source text.
11285
11286 Note that force_decl_die() forces function declaration die. It is
11287 later reused to represent definition. */
1edf43d6
JM
11288 equate_decl_number_to_die (decl, subr_die);
11289 }
a94dbf2c
JM
11290 }
11291 else if (DECL_ABSTRACT (decl))
a3f97cbb 11292 {
1bb17c21 11293 if (DECL_DECLARED_INLINE_P (decl))
61b32c02 11294 {
1bb17c21 11295 if (cgraph_function_possibly_inlined_p (decl))
61b32c02
JM
11296 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11297 else
1bb17c21 11298 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
61b32c02 11299 }
61b32c02 11300 else
1bb17c21
JH
11301 {
11302 if (cgraph_function_possibly_inlined_p (decl))
11303 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11304 else
ae0f3477 11305 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
1bb17c21 11306 }
61b32c02 11307
a3f97cbb
JW
11308 equate_decl_number_to_die (decl, subr_die);
11309 }
11310 else if (!DECL_EXTERNAL (decl))
11311 {
ae0f3477 11312 if (!old_die || !get_AT (old_die, DW_AT_inline))
ba7b35df 11313 equate_decl_number_to_die (decl, subr_die);
71dfc51f 11314
5c90448c 11315 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
df696a75 11316 current_function_funcdef_no);
7d4440be 11317 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
5c90448c 11318 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
df696a75 11319 current_function_funcdef_no);
a3f97cbb
JW
11320 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11321
d291dd49
JM
11322 add_pubname (decl, subr_die);
11323 add_arange (decl, subr_die);
11324
a3f97cbb 11325#ifdef MIPS_DEBUGGING_INFO
a3f97cbb
JW
11326 /* Add a reference to the FDE for this routine. */
11327 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11328#endif
11329
810429b7 11330 /* Define the "frame base" location for this routine. We use the
73c68f61
SS
11331 frame pointer or stack pointer registers, since the RTL for local
11332 variables is relative to one of them. */
0a2d3d69
DB
11333 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11334 {
11335 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11336 DW_AT_frame_base);
11337 }
11338 else
11339 {
11340 fp_reg
11341 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11342 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11343 }
a3f97cbb 11344
6de9cd9a 11345 if (cfun->static_chain_decl)
ef76d03b 11346 add_AT_location_description (subr_die, DW_AT_static_link,
6de9cd9a 11347 loc_descriptor_from_tree (cfun->static_chain_decl, 0));
a3f97cbb
JW
11348 }
11349
11350 /* Now output descriptions of the arguments for this function. This gets
556273e0 11351 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
a3f97cbb
JW
11352 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11353 `...' at the end of the formal parameter list. In order to find out if
11354 there was a trailing ellipsis or not, we must instead look at the type
11355 associated with the FUNCTION_DECL. This will be a node of type
11356 FUNCTION_TYPE. If the chain of type nodes hanging off of this
556273e0 11357 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
a3f97cbb 11358 an ellipsis at the end. */
71dfc51f 11359
a3f97cbb 11360 /* In the case where we are describing a mere function declaration, all we
556273e0 11361 need to do here (and all we *can* do here) is to describe the *types* of
a3f97cbb 11362 its formal parameters. */
4927276d 11363 if (debug_info_level <= DINFO_LEVEL_TERSE)
71dfc51f 11364 ;
4edb7b60 11365 else if (declaration)
5daf7c0a 11366 gen_formal_types_die (decl, subr_die);
a3f97cbb
JW
11367 else
11368 {
f9da5064 11369 /* Generate DIEs to represent all known formal parameters. */
b3694847
SS
11370 tree arg_decls = DECL_ARGUMENTS (decl);
11371 tree parm;
a3f97cbb
JW
11372
11373 /* When generating DIEs, generate the unspecified_parameters DIE
73c68f61 11374 instead if we come across the arg "__builtin_va_alist" */
a3f97cbb 11375 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
71dfc51f
RK
11376 if (TREE_CODE (parm) == PARM_DECL)
11377 {
db3cf6fb
MS
11378 if (DECL_NAME (parm)
11379 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11380 "__builtin_va_alist"))
71dfc51f
RK
11381 gen_unspecified_parameters_die (parm, subr_die);
11382 else
11383 gen_decl_die (parm, subr_die);
11384 }
a3f97cbb 11385
4fe9b91c 11386 /* Decide whether we need an unspecified_parameters DIE at the end.
73c68f61
SS
11387 There are 2 more cases to do this for: 1) the ansi ... declaration -
11388 this is detectable when the end of the arg list is not a
11389 void_type_node 2) an unprototyped function declaration (not a
11390 definition). This just means that we have no info about the
11391 parameters at all. */
a3f97cbb 11392 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
71dfc51f 11393 if (fn_arg_types != NULL)
a3f97cbb 11394 {
beb235f8 11395 /* This is the prototyped case, check for.... */
a3f97cbb 11396 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
71dfc51f 11397 gen_unspecified_parameters_die (decl, subr_die);
a3f97cbb 11398 }
71dfc51f
RK
11399 else if (DECL_INITIAL (decl) == NULL_TREE)
11400 gen_unspecified_parameters_die (decl, subr_die);
a3f97cbb
JW
11401 }
11402
11403 /* Output Dwarf info for all of the stuff within the body of the function
11404 (if it has one - it may be just a declaration). */
11405 outer_scope = DECL_INITIAL (decl);
11406
2ad9852d
RK
11407 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11408 a function. This BLOCK actually represents the outermost binding contour
11409 for the function, i.e. the contour in which the function's formal
11410 parameters and labels get declared. Curiously, it appears that the front
11411 end doesn't actually put the PARM_DECL nodes for the current function onto
11412 the BLOCK_VARS list for this outer scope, but are strung off of the
11413 DECL_ARGUMENTS list for the function instead.
11414
11415 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11416 the LABEL_DECL nodes for the function however, and we output DWARF info
11417 for those in decls_for_scope. Just within the `outer_scope' there will be
11418 a BLOCK node representing the function's outermost pair of curly braces,
11419 and any blocks used for the base and member initializers of a C++
d7248bff 11420 constructor function. */
4edb7b60 11421 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
7e23cb16 11422 {
6de9cd9a
DN
11423 /* Emit a DW_TAG_variable DIE for a named return value. */
11424 if (DECL_NAME (DECL_RESULT (decl)))
11425 gen_decl_die (DECL_RESULT (decl), subr_die);
11426
7e23cb16
JM
11427 current_function_has_inlines = 0;
11428 decls_for_scope (outer_scope, subr_die, 0);
71dfc51f 11429
ce61cc73 11430#if 0 && defined (MIPS_DEBUGGING_INFO)
7e23cb16
JM
11431 if (current_function_has_inlines)
11432 {
11433 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11434 if (! comp_unit_has_inlines)
11435 {
11436 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11437 comp_unit_has_inlines = 1;
11438 }
11439 }
11440#endif
11441 }
a3f97cbb
JW
11442}
11443
11444/* Generate a DIE to represent a declared data object. */
71dfc51f 11445
a3f97cbb 11446static void
7080f735 11447gen_variable_die (tree decl, dw_die_ref context_die)
a3f97cbb 11448{
b3694847 11449 tree origin = decl_ultimate_origin (decl);
54ba1f0d 11450 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
71dfc51f 11451
bdb669cb 11452 dw_die_ref old_die = lookup_decl_die (decl);
9765e357 11453 int declaration = (DECL_EXTERNAL (decl)
66c78aa9 11454 || class_or_namespace_scope_p (context_die));
4edb7b60 11455
a3f97cbb 11456 if (origin != NULL)
71dfc51f 11457 add_abstract_origin_attribute (var_die, origin);
2ad9852d 11458
f76b8156 11459 /* Loop unrolling can create multiple blocks that refer to the same
2ad9852d
RK
11460 static variable, so we must test for the DW_AT_declaration flag.
11461
11462 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
f76b8156 11463 copy decls and set the DECL_ABSTRACT flag on them instead of
2ad9852d
RK
11464 sharing them.
11465
11466 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
f76b8156 11467 else if (old_die && TREE_STATIC (decl)
c26fbbca 11468 && get_AT_flag (old_die, DW_AT_declaration) == 1)
bdb669cb 11469 {
e689ae67 11470 /* This is a definition of a C++ class level static. */
47fcfa7b 11471 add_AT_specification (var_die, old_die);
bdb669cb
JM
11472 if (DECL_NAME (decl))
11473 {
f31686a3 11474 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
71dfc51f 11475
bdb669cb
JM
11476 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11477 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
71dfc51f 11478
bdb669cb 11479 if (get_AT_unsigned (old_die, DW_AT_decl_line)
f31686a3 11480 != (unsigned) DECL_SOURCE_LINE (decl))
71dfc51f
RK
11481
11482 add_AT_unsigned (var_die, DW_AT_decl_line,
f31686a3 11483 DECL_SOURCE_LINE (decl));
bdb669cb
JM
11484 }
11485 }
a3f97cbb
JW
11486 else
11487 {
11488 add_name_and_src_coords_attributes (var_die, decl);
2ad9852d 11489 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
a3f97cbb 11490 TREE_THIS_VOLATILE (decl), context_die);
71dfc51f 11491
273dbe67
JM
11492 if (TREE_PUBLIC (decl))
11493 add_AT_flag (var_die, DW_AT_external, 1);
71dfc51f 11494
273dbe67
JM
11495 if (DECL_ARTIFICIAL (decl))
11496 add_AT_flag (var_die, DW_AT_artificial, 1);
71dfc51f 11497
a94dbf2c
JM
11498 if (TREE_PROTECTED (decl))
11499 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11500 else if (TREE_PRIVATE (decl))
11501 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
a3f97cbb 11502 }
4edb7b60
JM
11503
11504 if (declaration)
11505 add_AT_flag (var_die, DW_AT_declaration, 1);
556273e0 11506
6097b0c3 11507 if (DECL_ABSTRACT (decl) || declaration)
4edb7b60
JM
11508 equate_decl_number_to_die (decl, var_die);
11509
11510 if (! declaration && ! DECL_ABSTRACT (decl))
a3f97cbb 11511 {
0a2d3d69 11512 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
d291dd49 11513 add_pubname (decl, var_die);
a3f97cbb 11514 }
1bfb5f8f
JM
11515 else
11516 tree_add_const_value_attribute (var_die, decl);
a3f97cbb
JW
11517}
11518
11519/* Generate a DIE to represent a label identifier. */
71dfc51f 11520
a3f97cbb 11521static void
7080f735 11522gen_label_die (tree decl, dw_die_ref context_die)
a3f97cbb 11523{
b3694847 11524 tree origin = decl_ultimate_origin (decl);
54ba1f0d 11525 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
b3694847 11526 rtx insn;
a3f97cbb 11527 char label[MAX_ARTIFICIAL_LABEL_BYTES];
71dfc51f 11528
a3f97cbb 11529 if (origin != NULL)
71dfc51f 11530 add_abstract_origin_attribute (lbl_die, origin);
a3f97cbb 11531 else
71dfc51f
RK
11532 add_name_and_src_coords_attributes (lbl_die, decl);
11533
a3f97cbb 11534 if (DECL_ABSTRACT (decl))
71dfc51f 11535 equate_decl_number_to_die (decl, lbl_die);
a3f97cbb
JW
11536 else
11537 {
d0585b99 11538 insn = DECL_RTL_IF_SET (decl);
088e7160
NC
11539
11540 /* Deleted labels are programmer specified labels which have been
6356f892 11541 eliminated because of various optimizations. We still emit them
088e7160 11542 here so that it is possible to put breakpoints on them. */
d0585b99
RH
11543 if (insn
11544 && (GET_CODE (insn) == CODE_LABEL
11545 || ((GET_CODE (insn) == NOTE
11546 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
a3f97cbb 11547 {
556273e0
KH
11548 /* When optimization is enabled (via -O) some parts of the compiler
11549 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
a3f97cbb
JW
11550 represent source-level labels which were explicitly declared by
11551 the user. This really shouldn't be happening though, so catch
11552 it if it ever does happen. */
11553 if (INSN_DELETED_P (insn))
71dfc51f
RK
11554 abort ();
11555
66234570 11556 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
a3f97cbb
JW
11557 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11558 }
11559 }
11560}
11561
11562/* Generate a DIE for a lexical block. */
71dfc51f 11563
a3f97cbb 11564static void
7080f735 11565gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
a3f97cbb 11566{
54ba1f0d 11567 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
a3f97cbb 11568 char label[MAX_ARTIFICIAL_LABEL_BYTES];
71dfc51f
RK
11569
11570 if (! BLOCK_ABSTRACT (stmt))
a3f97cbb 11571 {
a20612aa
RH
11572 if (BLOCK_FRAGMENT_CHAIN (stmt))
11573 {
11574 tree chain;
11575
2bee6045 11576 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
a20612aa
RH
11577
11578 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11579 do
11580 {
11581 add_ranges (chain);
11582 chain = BLOCK_FRAGMENT_CHAIN (chain);
11583 }
11584 while (chain);
11585 add_ranges (NULL);
11586 }
11587 else
11588 {
11589 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11590 BLOCK_NUMBER (stmt));
11591 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11592 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11593 BLOCK_NUMBER (stmt));
11594 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11595 }
a3f97cbb 11596 }
71dfc51f 11597
d7248bff 11598 decls_for_scope (stmt, stmt_die, depth);
a3f97cbb
JW
11599}
11600
11601/* Generate a DIE for an inlined subprogram. */
71dfc51f 11602
a3f97cbb 11603static void
7080f735 11604gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
a3f97cbb 11605{
9bdca184
AO
11606 tree decl = block_ultimate_origin (stmt);
11607
11608 /* Emit info for the abstract instance first, if we haven't yet. We
11609 must emit this even if the block is abstract, otherwise when we
11610 emit the block below (or elsewhere), we may end up trying to emit
11611 a die whose origin die hasn't been emitted, and crashing. */
11612 dwarf2out_abstract_function (decl);
11613
71dfc51f 11614 if (! BLOCK_ABSTRACT (stmt))
a3f97cbb 11615 {
b3694847 11616 dw_die_ref subr_die
54ba1f0d 11617 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
d7248bff 11618 char label[MAX_ARTIFICIAL_LABEL_BYTES];
71dfc51f 11619
ab72d377 11620 add_abstract_origin_attribute (subr_die, decl);
5c90448c 11621 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18c038b9 11622 BLOCK_NUMBER (stmt));
a3f97cbb 11623 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
18c038b9
MM
11624 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11625 BLOCK_NUMBER (stmt));
a3f97cbb 11626 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
d7248bff 11627 decls_for_scope (stmt, subr_die, depth);
7e23cb16 11628 current_function_has_inlines = 1;
a3f97cbb 11629 }
06e224f7
AO
11630 else
11631 /* We may get here if we're the outer block of function A that was
11632 inlined into function B that was inlined into function C. When
11633 generating debugging info for C, dwarf2out_abstract_function(B)
11634 would mark all inlined blocks as abstract, including this one.
11635 So, we wouldn't (and shouldn't) expect labels to be generated
11636 for this one. Instead, just emit debugging info for
11637 declarations within the block. This is particularly important
11638 in the case of initializers of arguments passed from B to us:
11639 if they're statement expressions containing declarations, we
11640 wouldn't generate dies for their abstract variables, and then,
11641 when generating dies for the real variables, we'd die (pun
11642 intended :-) */
11643 gen_lexical_block_die (stmt, context_die, depth);
a3f97cbb
JW
11644}
11645
11646/* Generate a DIE for a field in a record, or structure. */
71dfc51f 11647
a3f97cbb 11648static void
7080f735 11649gen_field_die (tree decl, dw_die_ref context_die)
a3f97cbb 11650{
a53efda2 11651 dw_die_ref decl_die;
71dfc51f 11652
a53efda2
JZ
11653 if (TREE_TYPE (decl) == error_mark_node)
11654 return;
7080f735 11655
a53efda2 11656 decl_die = new_die (DW_TAG_member, context_die, decl);
a3f97cbb 11657 add_name_and_src_coords_attributes (decl_die, decl);
a3f97cbb
JW
11658 add_type_attribute (decl_die, member_declared_type (decl),
11659 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11660 context_die);
71dfc51f 11661
a3f97cbb
JW
11662 if (DECL_BIT_FIELD_TYPE (decl))
11663 {
11664 add_byte_size_attribute (decl_die, decl);
11665 add_bit_size_attribute (decl_die, decl);
11666 add_bit_offset_attribute (decl_die, decl);
11667 }
71dfc51f 11668
a94dbf2c
JM
11669 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11670 add_data_member_location_attribute (decl_die, decl);
71dfc51f 11671
273dbe67
JM
11672 if (DECL_ARTIFICIAL (decl))
11673 add_AT_flag (decl_die, DW_AT_artificial, 1);
71dfc51f 11674
a94dbf2c
JM
11675 if (TREE_PROTECTED (decl))
11676 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11677 else if (TREE_PRIVATE (decl))
11678 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
a3f97cbb
JW
11679}
11680
ab72d377
JM
11681#if 0
11682/* Don't generate either pointer_type DIEs or reference_type DIEs here.
11683 Use modified_type_die instead.
a3f97cbb
JW
11684 We keep this code here just in case these types of DIEs may be needed to
11685 represent certain things in other languages (e.g. Pascal) someday. */
2ad9852d 11686
a3f97cbb 11687static void
7080f735 11688gen_pointer_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11689{
b3694847 11690 dw_die_ref ptr_die
54ba1f0d 11691 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
71dfc51f 11692
a3f97cbb 11693 equate_type_number_to_die (type, ptr_die);
a3f97cbb 11694 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
ab72d377 11695 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
a3f97cbb
JW
11696}
11697
ab72d377
JM
11698/* Don't generate either pointer_type DIEs or reference_type DIEs here.
11699 Use modified_type_die instead.
a3f97cbb
JW
11700 We keep this code here just in case these types of DIEs may be needed to
11701 represent certain things in other languages (e.g. Pascal) someday. */
2ad9852d 11702
a3f97cbb 11703static void
7080f735 11704gen_reference_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11705{
b3694847 11706 dw_die_ref ref_die
54ba1f0d 11707 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
71dfc51f 11708
a3f97cbb 11709 equate_type_number_to_die (type, ref_die);
a3f97cbb 11710 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
ab72d377 11711 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
a3f97cbb 11712}
ab72d377 11713#endif
a3f97cbb
JW
11714
11715/* Generate a DIE for a pointer to a member type. */
2ad9852d 11716
a3f97cbb 11717static void
7080f735 11718gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11719{
b3694847 11720 dw_die_ref ptr_die
54ba1f0d
RH
11721 = new_die (DW_TAG_ptr_to_member_type,
11722 scope_die_for (type, context_die), type);
71dfc51f 11723
a3f97cbb 11724 equate_type_number_to_die (type, ptr_die);
a3f97cbb 11725 add_AT_die_ref (ptr_die, DW_AT_containing_type,
bdb669cb 11726 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
a3f97cbb
JW
11727 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11728}
11729
11730/* Generate the DIE for the compilation unit. */
71dfc51f 11731
a96c67ec 11732static dw_die_ref
7080f735 11733gen_compile_unit_die (const char *filename)
a3f97cbb 11734{
b3694847 11735 dw_die_ref die;
a3f97cbb 11736 char producer[250];
3ac88239 11737 const char *language_string = lang_hooks.name;
a96c67ec 11738 int language;
a3f97cbb 11739
54ba1f0d 11740 die = new_die (DW_TAG_compile_unit, NULL, NULL);
bdb669cb 11741
c4274b22
RH
11742 if (filename)
11743 {
11744 add_name_attribute (die, filename);
e3091a5f
R
11745 /* Don't add cwd for <built-in>. */
11746 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
c4274b22
RH
11747 add_comp_dir_attribute (die);
11748 }
a3f97cbb
JW
11749
11750 sprintf (producer, "%s %s", language_string, version_string);
11751
11752#ifdef MIPS_DEBUGGING_INFO
11753 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11754 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11755 not appear in the producer string, the debugger reaches the conclusion
11756 that the object file is stripped and has no debugging information.
11757 To get the MIPS/SGI debugger to believe that there is debugging
11758 information in the object file, we add a -g to the producer string. */
4927276d
JM
11759 if (debug_info_level > DINFO_LEVEL_TERSE)
11760 strcat (producer, " -g");
a3f97cbb
JW
11761#endif
11762
a96c67ec 11763 add_AT_string (die, DW_AT_producer, producer);
a9d38797 11764
a3f97cbb 11765 if (strcmp (language_string, "GNU C++") == 0)
a96c67ec 11766 language = DW_LANG_C_plus_plus;
a3f97cbb 11767 else if (strcmp (language_string, "GNU Ada") == 0)
8cb5fbbf 11768 language = DW_LANG_Ada95;
a9d38797 11769 else if (strcmp (language_string, "GNU F77") == 0)
a96c67ec 11770 language = DW_LANG_Fortran77;
6de9cd9a
DN
11771 else if (strcmp (language_string, "GNU F95") == 0)
11772 language = DW_LANG_Fortran95;
bc28c45b 11773 else if (strcmp (language_string, "GNU Pascal") == 0)
a96c67ec 11774 language = DW_LANG_Pascal83;
28985b81
AG
11775 else if (strcmp (language_string, "GNU Java") == 0)
11776 language = DW_LANG_Java;
a3f97cbb 11777 else
a96c67ec 11778 language = DW_LANG_C89;
a9d38797 11779
a96c67ec 11780 add_AT_unsigned (die, DW_AT_language, language);
a96c67ec 11781 return die;
a3f97cbb
JW
11782}
11783
11784/* Generate a DIE for a string type. */
71dfc51f 11785
a3f97cbb 11786static void
7080f735 11787gen_string_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11788{
b3694847 11789 dw_die_ref type_die
54ba1f0d 11790 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
71dfc51f 11791
bdb669cb 11792 equate_type_number_to_die (type, type_die);
a3f97cbb 11793
2ad9852d
RK
11794 /* ??? Fudge the string length attribute for now.
11795 TODO: add string length info. */
11796#if 0
11797 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11798 bound_representation (upper_bound, 0, 'u');
11799#endif
a3f97cbb
JW
11800}
11801
61b32c02 11802/* Generate the DIE for a base class. */
71dfc51f 11803
61b32c02 11804static void
7080f735 11805gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
61b32c02 11806{
54ba1f0d 11807 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
71dfc51f 11808
61b32c02
JM
11809 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11810 add_data_member_location_attribute (die, binfo);
71dfc51f 11811
61b32c02
JM
11812 if (TREE_VIA_VIRTUAL (binfo))
11813 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
2ad9852d 11814
dbbf88d1 11815 if (access == access_public_node)
61b32c02 11816 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
dbbf88d1 11817 else if (access == access_protected_node)
61b32c02
JM
11818 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11819}
11820
956d6950 11821/* Generate a DIE for a class member. */
71dfc51f 11822
a3f97cbb 11823static void
7080f735 11824gen_member_die (tree type, dw_die_ref context_die)
a3f97cbb 11825{
b3694847 11826 tree member;
dbbf88d1 11827 tree binfo = TYPE_BINFO (type);
10a11b75 11828 dw_die_ref child;
71dfc51f 11829
a3f97cbb
JW
11830 /* If this is not an incomplete type, output descriptions of each of its
11831 members. Note that as we output the DIEs necessary to represent the
11832 members of this record or union type, we will also be trying to output
11833 DIEs to represent the *types* of those members. However the `type'
556273e0 11834 function (above) will specifically avoid generating type DIEs for member
eaec9b3d 11835 types *within* the list of member DIEs for this (containing) type except
a3f97cbb
JW
11836 for those types (of members) which are explicitly marked as also being
11837 members of this (containing) type themselves. The g++ front- end can
2ad9852d
RK
11838 force any given type to be treated as a member of some other (containing)
11839 type by setting the TYPE_CONTEXT of the given (member) type to point to
11840 the TREE node representing the appropriate (containing) type. */
a3f97cbb 11841
61b32c02 11842 /* First output info about the base classes. */
dbbf88d1 11843 if (binfo && BINFO_BASETYPES (binfo))
a3f97cbb 11844 {
dbbf88d1
NS
11845 tree bases = BINFO_BASETYPES (binfo);
11846 tree accesses = BINFO_BASEACCESSES (binfo);
b3694847
SS
11847 int n_bases = TREE_VEC_LENGTH (bases);
11848 int i;
61b32c02
JM
11849
11850 for (i = 0; i < n_bases; i++)
dbbf88d1
NS
11851 gen_inheritance_die (TREE_VEC_ELT (bases, i),
11852 (accesses ? TREE_VEC_ELT (accesses, i)
11853 : access_public_node), context_die);
a3f97cbb
JW
11854 }
11855
61b32c02
JM
11856 /* Now output info about the data members and type members. */
11857 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
10a11b75
JM
11858 {
11859 /* If we thought we were generating minimal debug info for TYPE
11860 and then changed our minds, some of the member declarations
11861 may have already been defined. Don't define them again, but
11862 do put them in the right order. */
11863
11864 child = lookup_decl_die (member);
11865 if (child)
11866 splice_child_die (context_die, child);
11867 else
11868 gen_decl_die (member, context_die);
11869 }
61b32c02 11870
a3f97cbb 11871 /* Now output info about the function members (if any). */
61b32c02 11872 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
10a11b75 11873 {
5daf7c0a
JM
11874 /* Don't include clones in the member list. */
11875 if (DECL_ABSTRACT_ORIGIN (member))
11876 continue;
11877
10a11b75
JM
11878 child = lookup_decl_die (member);
11879 if (child)
11880 splice_child_die (context_die, child);
11881 else
11882 gen_decl_die (member, context_die);
11883 }
a3f97cbb
JW
11884}
11885
10a11b75
JM
11886/* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
11887 is set, we pretend that the type was never defined, so we only get the
11888 member DIEs needed by later specification DIEs. */
71dfc51f 11889
a3f97cbb 11890static void
7080f735 11891gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11892{
b3694847
SS
11893 dw_die_ref type_die = lookup_type_die (type);
11894 dw_die_ref scope_die = 0;
11895 int nested = 0;
10a11b75 11896 int complete = (TYPE_SIZE (type)
65e1263a
JW
11897 && (! TYPE_STUB_DECL (type)
11898 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
66c78aa9 11899 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
273dbe67 11900
10a11b75 11901 if (type_die && ! complete)
273dbe67 11902 return;
a082c85a 11903
71dfc51f 11904 if (TYPE_CONTEXT (type) != NULL_TREE
66c78aa9
JM
11905 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11906 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
a082c85a
JM
11907 nested = 1;
11908
a94dbf2c 11909 scope_die = scope_die_for (type, context_die);
a082c85a
JM
11910
11911 if (! type_die || (nested && scope_die == comp_unit_die))
273dbe67 11912 /* First occurrence of type or toplevel definition of nested class. */
a3f97cbb 11913 {
b3694847 11914 dw_die_ref old_die = type_die;
71dfc51f 11915
a3f97cbb
JW
11916 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11917 ? DW_TAG_structure_type : DW_TAG_union_type,
54ba1f0d 11918 scope_die, type);
a3f97cbb 11919 equate_type_number_to_die (type, type_die);
273dbe67 11920 if (old_die)
47fcfa7b 11921 add_AT_specification (type_die, old_die);
5de0e8d4
JM
11922 else
11923 add_name_attribute (type_die, type_tag (type));
a3f97cbb 11924 }
4b674448 11925 else
273dbe67 11926 remove_AT (type_die, DW_AT_declaration);
a3f97cbb
JW
11927
11928 /* If this type has been completed, then give it a byte_size attribute and
11929 then give a list of members. */
66c78aa9 11930 if (complete && !ns_decl)
a3f97cbb 11931 {
556273e0 11932 /* Prevent infinite recursion in cases where the type of some member of
73c68f61 11933 this type is expressed in terms of this type itself. */
a3f97cbb 11934 TREE_ASM_WRITTEN (type) = 1;
273dbe67 11935 add_byte_size_attribute (type_die, type);
e9a25f70 11936 if (TYPE_STUB_DECL (type) != NULL_TREE)
b2932ae5 11937 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
71dfc51f 11938
ef76d03b
JW
11939 /* If the first reference to this type was as the return type of an
11940 inline function, then it may not have a parent. Fix this now. */
11941 if (type_die->die_parent == NULL)
11942 add_child_die (scope_die, type_die);
11943
273dbe67
JM
11944 push_decl_scope (type);
11945 gen_member_die (type, type_die);
11946 pop_decl_scope ();
71dfc51f 11947
a94dbf2c
JM
11948 /* GNU extension: Record what type our vtable lives in. */
11949 if (TYPE_VFIELD (type))
11950 {
11951 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
71dfc51f 11952
de6e505e
JM
11953 gen_type_die (vtype, context_die);
11954 add_AT_die_ref (type_die, DW_AT_containing_type,
11955 lookup_type_die (vtype));
a94dbf2c 11956 }
a3f97cbb 11957 }
4b674448 11958 else
8a8c3656
JM
11959 {
11960 add_AT_flag (type_die, DW_AT_declaration, 1);
a30d4514 11961
9765e357 11962 /* We don't need to do this for function-local types. */
9702143f
RK
11963 if (TYPE_STUB_DECL (type)
11964 && ! decl_function_context (TYPE_STUB_DECL (type)))
2ad9852d 11965 VARRAY_PUSH_TREE (incomplete_types, type);
8a8c3656 11966 }
a3f97cbb
JW
11967}
11968
11969/* Generate a DIE for a subroutine _type_. */
71dfc51f 11970
a3f97cbb 11971static void
7080f735 11972gen_subroutine_type_die (tree type, dw_die_ref context_die)
a3f97cbb 11973{
b3694847
SS
11974 tree return_type = TREE_TYPE (type);
11975 dw_die_ref subr_die
54ba1f0d
RH
11976 = new_die (DW_TAG_subroutine_type,
11977 scope_die_for (type, context_die), type);
71dfc51f 11978
a3f97cbb
JW
11979 equate_type_number_to_die (type, subr_die);
11980 add_prototyped_attribute (subr_die, type);
a3f97cbb 11981 add_type_attribute (subr_die, return_type, 0, 0, context_die);
a94dbf2c 11982 gen_formal_types_die (type, subr_die);
a3f97cbb
JW
11983}
11984
f9da5064 11985/* Generate a DIE for a type definition. */
71dfc51f 11986
a3f97cbb 11987static void
7080f735 11988gen_typedef_die (tree decl, dw_die_ref context_die)
a3f97cbb 11989{
b3694847
SS
11990 dw_die_ref type_die;
11991 tree origin;
a94dbf2c
JM
11992
11993 if (TREE_ASM_WRITTEN (decl))
11994 return;
a94dbf2c 11995
2ad9852d 11996 TREE_ASM_WRITTEN (decl) = 1;
54ba1f0d 11997 type_die = new_die (DW_TAG_typedef, context_die, decl);
a94dbf2c 11998 origin = decl_ultimate_origin (decl);
a3f97cbb 11999 if (origin != NULL)
a94dbf2c 12000 add_abstract_origin_attribute (type_die, origin);
a3f97cbb
JW
12001 else
12002 {
b3694847 12003 tree type;
2ad9852d 12004
a3f97cbb 12005 add_name_and_src_coords_attributes (type_die, decl);
a94dbf2c
JM
12006 if (DECL_ORIGINAL_TYPE (decl))
12007 {
12008 type = DECL_ORIGINAL_TYPE (decl);
62e3bf54
JM
12009
12010 if (type == TREE_TYPE (decl))
12011 abort ();
12012 else
12013 equate_type_number_to_die (TREE_TYPE (decl), type_die);
a94dbf2c
JM
12014 }
12015 else
12016 type = TREE_TYPE (decl);
2ad9852d 12017
a94dbf2c
JM
12018 add_type_attribute (type_die, type, TREE_READONLY (decl),
12019 TREE_THIS_VOLATILE (decl), context_die);
a3f97cbb 12020 }
71dfc51f 12021
a3f97cbb 12022 if (DECL_ABSTRACT (decl))
a94dbf2c 12023 equate_decl_number_to_die (decl, type_die);
a3f97cbb
JW
12024}
12025
12026/* Generate a type description DIE. */
71dfc51f 12027
a3f97cbb 12028static void
7080f735 12029gen_type_die (tree type, dw_die_ref context_die)
a3f97cbb 12030{
348bb3c7
JM
12031 int need_pop;
12032
71dfc51f
RK
12033 if (type == NULL_TREE || type == error_mark_node)
12034 return;
a3f97cbb 12035
a94dbf2c
JM
12036 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12037 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
556273e0 12038 {
5d7bed9d
DJ
12039 if (TREE_ASM_WRITTEN (type))
12040 return;
12041
29b91443
JM
12042 /* Prevent broken recursion; we can't hand off to the same type. */
12043 if (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) == type)
12044 abort ();
12045
a94dbf2c
JM
12046 TREE_ASM_WRITTEN (type) = 1;
12047 gen_decl_die (TYPE_NAME (type), context_die);
12048 return;
12049 }
12050
5d7bed9d
DJ
12051 /* We are going to output a DIE to represent the unqualified version
12052 of this type (i.e. without any const or volatile qualifiers) so
12053 get the main variant (i.e. the unqualified version) of this type
12054 now. (Vectors are special because the debugging info is in the
12055 cloned type itself). */
12056 if (TREE_CODE (type) != VECTOR_TYPE)
12057 type = type_main_variant (type);
12058
12059 if (TREE_ASM_WRITTEN (type))
12060 return;
12061
a3f97cbb
JW
12062 switch (TREE_CODE (type))
12063 {
12064 case ERROR_MARK:
12065 break;
12066
12067 case POINTER_TYPE:
12068 case REFERENCE_TYPE:
956d6950
JL
12069 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12070 ensures that the gen_type_die recursion will terminate even if the
12071 type is recursive. Recursive types are possible in Ada. */
12072 /* ??? We could perhaps do this for all types before the switch
12073 statement. */
12074 TREE_ASM_WRITTEN (type) = 1;
12075
a3f97cbb 12076 /* For these types, all that is required is that we output a DIE (or a
73c68f61 12077 set of DIEs) to represent the "basis" type. */
a3f97cbb
JW
12078 gen_type_die (TREE_TYPE (type), context_die);
12079 break;
12080
12081 case OFFSET_TYPE:
556273e0 12082 /* This code is used for C++ pointer-to-data-member types.
71dfc51f 12083 Output a description of the relevant class type. */
a3f97cbb 12084 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
71dfc51f 12085
a3f97cbb
JW
12086 /* Output a description of the type of the object pointed to. */
12087 gen_type_die (TREE_TYPE (type), context_die);
71dfc51f 12088
a3f97cbb 12089 /* Now output a DIE to represent this pointer-to-data-member type
73c68f61 12090 itself. */
a3f97cbb
JW
12091 gen_ptr_to_mbr_type_die (type, context_die);
12092 break;
12093
12094 case SET_TYPE:
12095 gen_type_die (TYPE_DOMAIN (type), context_die);
12096 gen_set_type_die (type, context_die);
12097 break;
12098
12099 case FILE_TYPE:
12100 gen_type_die (TREE_TYPE (type), context_die);
12101 abort (); /* No way to represent these in Dwarf yet! */
12102 break;
12103
12104 case FUNCTION_TYPE:
12105 /* Force out return type (in case it wasn't forced out already). */
12106 gen_type_die (TREE_TYPE (type), context_die);
12107 gen_subroutine_type_die (type, context_die);
12108 break;
12109
12110 case METHOD_TYPE:
12111 /* Force out return type (in case it wasn't forced out already). */
12112 gen_type_die (TREE_TYPE (type), context_die);
12113 gen_subroutine_type_die (type, context_die);
12114 break;
12115
12116 case ARRAY_TYPE:
12117 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12118 {
12119 gen_type_die (TREE_TYPE (type), context_die);
12120 gen_string_type_die (type, context_die);
12121 }
12122 else
71dfc51f 12123 gen_array_type_die (type, context_die);
a3f97cbb
JW
12124 break;
12125
4061f623 12126 case VECTOR_TYPE:
84f0ace0 12127 gen_array_type_die (type, context_die);
4061f623
BS
12128 break;
12129
a3f97cbb
JW
12130 case ENUMERAL_TYPE:
12131 case RECORD_TYPE:
12132 case UNION_TYPE:
12133 case QUAL_UNION_TYPE:
2ad9852d 12134 /* If this is a nested type whose containing class hasn't been written
73c68f61
SS
12135 out yet, writing it out will cover this one, too. This does not apply
12136 to instantiations of member class templates; they need to be added to
12137 the containing class as they are generated. FIXME: This hurts the
12138 idea of combining type decls from multiple TUs, since we can't predict
12139 what set of template instantiations we'll get. */
a082c85a 12140 if (TYPE_CONTEXT (type)
5f2f160c 12141 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
a082c85a 12142 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
a94dbf2c
JM
12143 {
12144 gen_type_die (TYPE_CONTEXT (type), context_die);
12145
348bb3c7 12146 if (TREE_ASM_WRITTEN (type))
a94dbf2c
JM
12147 return;
12148
12149 /* If that failed, attach ourselves to the stub. */
12150 push_decl_scope (TYPE_CONTEXT (type));
12151 context_die = lookup_type_die (TYPE_CONTEXT (type));
348bb3c7 12152 need_pop = 1;
a94dbf2c 12153 }
348bb3c7 12154 else
66c78aa9
JM
12155 {
12156 declare_in_namespace (type, context_die);
12157 need_pop = 0;
12158 }
a94dbf2c
JM
12159
12160 if (TREE_CODE (type) == ENUMERAL_TYPE)
273dbe67 12161 gen_enumeration_type_die (type, context_die);
a3f97cbb 12162 else
273dbe67 12163 gen_struct_or_union_type_die (type, context_die);
4b674448 12164
348bb3c7 12165 if (need_pop)
a94dbf2c
JM
12166 pop_decl_scope ();
12167
4b674448 12168 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
a082c85a
JM
12169 it up if it is ever completed. gen_*_type_die will set it for us
12170 when appropriate. */
12171 return;
a3f97cbb
JW
12172
12173 case VOID_TYPE:
12174 case INTEGER_TYPE:
12175 case REAL_TYPE:
12176 case COMPLEX_TYPE:
12177 case BOOLEAN_TYPE:
12178 case CHAR_TYPE:
12179 /* No DIEs needed for fundamental types. */
12180 break;
12181
12182 case LANG_TYPE:
12183 /* No Dwarf representation currently defined. */
12184 break;
12185
12186 default:
12187 abort ();
12188 }
12189
12190 TREE_ASM_WRITTEN (type) = 1;
12191}
12192
12193/* Generate a DIE for a tagged type instantiation. */
71dfc51f 12194
a3f97cbb 12195static void
7080f735 12196gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
a3f97cbb 12197{
71dfc51f
RK
12198 if (type == NULL_TREE || type == error_mark_node)
12199 return;
a3f97cbb 12200
38e01259 12201 /* We are going to output a DIE to represent the unqualified version of
a3f97cbb
JW
12202 this type (i.e. without any const or volatile qualifiers) so make sure
12203 that we have the main variant (i.e. the unqualified version) of this
12204 type now. */
bbc6ae08 12205 if (type != type_main_variant (type))
3a88cbd1 12206 abort ();
a3f97cbb 12207
203588e7 12208 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
bbc6ae08 12209 an instance of an unresolved type. */
556273e0 12210
a3f97cbb
JW
12211 switch (TREE_CODE (type))
12212 {
12213 case ERROR_MARK:
12214 break;
12215
12216 case ENUMERAL_TYPE:
12217 gen_inlined_enumeration_type_die (type, context_die);
12218 break;
12219
12220 case RECORD_TYPE:
12221 gen_inlined_structure_type_die (type, context_die);
12222 break;
12223
12224 case UNION_TYPE:
12225 case QUAL_UNION_TYPE:
12226 gen_inlined_union_type_die (type, context_die);
12227 break;
12228
12229 default:
71dfc51f 12230 abort ();
a3f97cbb
JW
12231 }
12232}
12233
12234/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12235 things which are local to the given block. */
71dfc51f 12236
a3f97cbb 12237static void
7080f735 12238gen_block_die (tree stmt, dw_die_ref context_die, int depth)
a3f97cbb 12239{
b3694847
SS
12240 int must_output_die = 0;
12241 tree origin;
12242 tree decl;
12243 enum tree_code origin_code;
a3f97cbb
JW
12244
12245 /* Ignore blocks never really used to make RTL. */
1e7f092a
JM
12246 if (stmt == NULL_TREE || !TREE_USED (stmt)
12247 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
71dfc51f 12248 return;
a3f97cbb 12249
a20612aa
RH
12250 /* If the block is one fragment of a non-contiguous block, do not
12251 process the variables, since they will have been done by the
12252 origin block. Do process subblocks. */
12253 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12254 {
12255 tree sub;
12256
2ad9852d 12257 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
a20612aa 12258 gen_block_die (sub, context_die, depth + 1);
2ad9852d 12259
a20612aa
RH
12260 return;
12261 }
12262
a3f97cbb
JW
12263 /* Determine the "ultimate origin" of this block. This block may be an
12264 inlined instance of an inlined instance of inline function, so we have
12265 to trace all of the way back through the origin chain to find out what
12266 sort of node actually served as the original seed for the creation of
12267 the current block. */
12268 origin = block_ultimate_origin (stmt);
12269 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12270
12271 /* Determine if we need to output any Dwarf DIEs at all to represent this
12272 block. */
12273 if (origin_code == FUNCTION_DECL)
71dfc51f
RK
12274 /* The outer scopes for inlinings *must* always be represented. We
12275 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12276 must_output_die = 1;
a3f97cbb
JW
12277 else
12278 {
12279 /* In the case where the current block represents an inlining of the
73c68f61
SS
12280 "body block" of an inline function, we must *NOT* output any DIE for
12281 this block because we have already output a DIE to represent the whole
12282 inlined function scope and the "body block" of any function doesn't
12283 really represent a different scope according to ANSI C rules. So we
12284 check here to make sure that this block does not represent a "body
12285 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
d7248bff 12286 if (! is_body_block (origin ? origin : stmt))
a3f97cbb
JW
12287 {
12288 /* Determine if this block directly contains any "significant"
12289 local declarations which we will need to output DIEs for. */
12290 if (debug_info_level > DINFO_LEVEL_TERSE)
71dfc51f
RK
12291 /* We are not in terse mode so *any* local declaration counts
12292 as being a "significant" one. */
12293 must_output_die = (BLOCK_VARS (stmt) != NULL);
a3f97cbb 12294 else
71dfc51f
RK
12295 /* We are in terse mode, so only local (nested) function
12296 definitions count as "significant" local declarations. */
12297 for (decl = BLOCK_VARS (stmt);
12298 decl != NULL; decl = TREE_CHAIN (decl))
12299 if (TREE_CODE (decl) == FUNCTION_DECL
12300 && DECL_INITIAL (decl))
a3f97cbb 12301 {
71dfc51f
RK
12302 must_output_die = 1;
12303 break;
a3f97cbb 12304 }
a3f97cbb
JW
12305 }
12306 }
12307
12308 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12309 DIE for any block which contains no significant local declarations at
12310 all. Rather, in such cases we just call `decls_for_scope' so that any
12311 needed Dwarf info for any sub-blocks will get properly generated. Note
12312 that in terse mode, our definition of what constitutes a "significant"
12313 local declaration gets restricted to include only inlined function
12314 instances and local (nested) function definitions. */
12315 if (must_output_die)
12316 {
12317 if (origin_code == FUNCTION_DECL)
71dfc51f 12318 gen_inlined_subroutine_die (stmt, context_die, depth);
a3f97cbb 12319 else
71dfc51f 12320 gen_lexical_block_die (stmt, context_die, depth);
a3f97cbb
JW
12321 }
12322 else
d7248bff 12323 decls_for_scope (stmt, context_die, depth);
a3f97cbb
JW
12324}
12325
12326/* Generate all of the decls declared within a given scope and (recursively)
9ec36da5 12327 all of its sub-blocks. */
71dfc51f 12328
a3f97cbb 12329static void
7080f735 12330decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
a3f97cbb 12331{
b3694847
SS
12332 tree decl;
12333 tree subblocks;
71dfc51f 12334
a3f97cbb 12335 /* Ignore blocks never really used to make RTL. */
71dfc51f
RK
12336 if (stmt == NULL_TREE || ! TREE_USED (stmt))
12337 return;
12338
88dad228
JM
12339 /* Output the DIEs to represent all of the data objects and typedefs
12340 declared directly within this block but not within any nested
12341 sub-blocks. Also, nested function and tag DIEs have been
12342 generated with a parent of NULL; fix that up now. */
2ad9852d 12343 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
a3f97cbb 12344 {
b3694847 12345 dw_die_ref die;
a94dbf2c 12346
88dad228 12347 if (TREE_CODE (decl) == FUNCTION_DECL)
a94dbf2c 12348 die = lookup_decl_die (decl);
88dad228 12349 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
a94dbf2c
JM
12350 die = lookup_type_die (TREE_TYPE (decl));
12351 else
12352 die = NULL;
12353
71dfc51f 12354 if (die != NULL && die->die_parent == NULL)
ef76d03b 12355 add_child_die (context_die, die);
88dad228
JM
12356 else
12357 gen_decl_die (decl, context_die);
a3f97cbb
JW
12358 }
12359
8cadae7e
JM
12360 /* If we're at -g1, we're not interested in subblocks. */
12361 if (debug_info_level <= DINFO_LEVEL_TERSE)
12362 return;
12363
a3f97cbb
JW
12364 /* Output the DIEs to represent all sub-blocks (and the items declared
12365 therein) of this block. */
12366 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12367 subblocks != NULL;
12368 subblocks = BLOCK_CHAIN (subblocks))
71dfc51f 12369 gen_block_die (subblocks, context_die, depth + 1);
a3f97cbb
JW
12370}
12371
a94dbf2c 12372/* Is this a typedef we can avoid emitting? */
71dfc51f
RK
12373
12374static inline int
7080f735 12375is_redundant_typedef (tree decl)
a94dbf2c
JM
12376{
12377 if (TYPE_DECL_IS_STUB (decl))
12378 return 1;
71dfc51f 12379
a94dbf2c
JM
12380 if (DECL_ARTIFICIAL (decl)
12381 && DECL_CONTEXT (decl)
12382 && is_tagged_type (DECL_CONTEXT (decl))
12383 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12384 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12385 /* Also ignore the artificial member typedef for the class name. */
12386 return 1;
71dfc51f 12387
a94dbf2c
JM
12388 return 0;
12389}
12390
6097b0c3
DP
12391/* Returns the DIE for decl or aborts. */
12392
12393static dw_die_ref
12394force_decl_die (tree decl)
12395{
12396 dw_die_ref decl_die;
12397 unsigned saved_external_flag;
12398 tree save_fn = NULL_TREE;
12399 decl_die = lookup_decl_die (decl);
12400 if (!decl_die)
12401 {
12402 dw_die_ref context_die;
12403 tree decl_context = DECL_CONTEXT (decl);
12404 if (decl_context)
12405 {
12406 /* Find die that represents this context. */
12407 if (TYPE_P (decl_context))
12408 context_die = force_type_die (decl_context);
12409 else
12410 context_die = force_decl_die (decl_context);
12411 }
12412 else
12413 context_die = comp_unit_die;
12414
12415 switch (TREE_CODE (decl))
12416 {
12417 case FUNCTION_DECL:
12418 /* Clear current_function_decl, so that gen_subprogram_die thinks
12419 that this is a declaration. At this point, we just want to force
12420 declaration die. */
12421 save_fn = current_function_decl;
12422 current_function_decl = NULL_TREE;
12423 gen_subprogram_die (decl, context_die);
12424 current_function_decl = save_fn;
12425 break;
12426
12427 case VAR_DECL:
12428 /* Set external flag to force declaration die. Restore it after
12429 gen_decl_die() call. */
12430 saved_external_flag = DECL_EXTERNAL (decl);
12431 DECL_EXTERNAL (decl) = 1;
12432 gen_decl_die (decl, context_die);
12433 DECL_EXTERNAL (decl) = saved_external_flag;
12434 break;
12435
12436 case NAMESPACE_DECL:
12437 dwarf2out_decl (decl);
12438 break;
12439
12440 default:
12441 abort ();
12442 }
12443
12444 /* See if we can find the die for this deci now.
12445 If not then abort. */
12446 if (!decl_die)
12447 decl_die = lookup_decl_die (decl);
12448 if (!decl_die)
12449 abort ();
12450 }
12451
12452 return decl_die;
12453}
66c78aa9 12454
6097b0c3 12455/* Returns the DIE for decl or aborts. */
66c78aa9
JM
12456
12457static dw_die_ref
6097b0c3 12458force_type_die (tree type)
66c78aa9 12459{
6097b0c3 12460 dw_die_ref type_die;
66c78aa9 12461
9733d507 12462 type_die = lookup_type_die (type);
6097b0c3
DP
12463 if (!type_die)
12464 {
12465 dw_die_ref context_die;
12466 if (TYPE_CONTEXT (type))
12467 if (TYPE_P (TYPE_CONTEXT (type)))
12468 context_die = force_type_die (TYPE_CONTEXT (type));
12469 else
12470 context_die = force_decl_die (TYPE_CONTEXT (type));
12471 else
12472 context_die = comp_unit_die;
66c78aa9 12473
6097b0c3 12474 gen_type_die (type, context_die);
9733d507 12475 type_die = lookup_type_die (type);
6097b0c3
DP
12476 if (!type_die)
12477 abort();
12478 }
12479 return type_die;
66c78aa9
JM
12480}
12481
12482/* Force out any required namespaces to be able to output DECL,
12483 and return the new context_die for it, if it's changed. */
12484
12485static dw_die_ref
12486setup_namespace_context (tree thing, dw_die_ref context_die)
12487{
12488 tree context = DECL_P (thing) ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing);
12489 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6614fd40 12490 /* Force out the namespace. */
6097b0c3 12491 context_die = force_decl_die (context);
66c78aa9
JM
12492
12493 return context_die;
12494}
12495
12496/* Emit a declaration DIE for THING (which is either a DECL or a tagged
12497 type) within its namespace, if appropriate.
12498
12499 For compatibility with older debuggers, namespace DIEs only contain
12500 declarations; all definitions are emitted at CU scope. */
12501
12502static void
12503declare_in_namespace (tree thing, dw_die_ref context_die)
12504{
12505 dw_die_ref ns_context;
12506
12507 if (debug_info_level <= DINFO_LEVEL_TERSE)
12508 return;
12509
12510 ns_context = setup_namespace_context (thing, context_die);
12511
12512 if (ns_context != context_die)
12513 {
12514 if (DECL_P (thing))
12515 gen_decl_die (thing, ns_context);
12516 else
12517 gen_type_die (thing, ns_context);
12518 }
12519}
12520
6614fd40 12521/* Generate a DIE for a namespace or namespace alias. */
66c78aa9
JM
12522
12523static void
12524gen_namespace_die (tree decl)
12525{
12526 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12527
12528 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
e0a21ab9 12529 they are an alias of. */
66c78aa9
JM
12530 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12531 {
6614fd40 12532 /* Output a real namespace. */
66c78aa9
JM
12533 dw_die_ref namespace_die
12534 = new_die (DW_TAG_namespace, context_die, decl);
12535 add_name_and_src_coords_attributes (namespace_die, decl);
12536 equate_decl_number_to_die (decl, namespace_die);
12537 }
12538 else
12539 {
6614fd40 12540 /* Output a namespace alias. */
66c78aa9 12541
6614fd40 12542 /* Force out the namespace we are an alias of, if necessary. */
66c78aa9 12543 dw_die_ref origin_die
6097b0c3 12544 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
66c78aa9 12545
6614fd40 12546 /* Now create the namespace alias DIE. */
66c78aa9
JM
12547 dw_die_ref namespace_die
12548 = new_die (DW_TAG_imported_declaration, context_die, decl);
12549 add_name_and_src_coords_attributes (namespace_die, decl);
12550 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12551 equate_decl_number_to_die (decl, namespace_die);
12552 }
12553}
12554
a3f97cbb 12555/* Generate Dwarf debug information for a decl described by DECL. */
71dfc51f 12556
a3f97cbb 12557static void
7080f735 12558gen_decl_die (tree decl, dw_die_ref context_die)
a3f97cbb 12559{
b3694847 12560 tree origin;
71dfc51f 12561
f11c3043 12562 if (DECL_P (decl) && DECL_IGNORED_P (decl))
71dfc51f 12563 return;
a3f97cbb 12564
a3f97cbb
JW
12565 switch (TREE_CODE (decl))
12566 {
2ad9852d
RK
12567 case ERROR_MARK:
12568 break;
12569
a3f97cbb 12570 case CONST_DECL:
556273e0 12571 /* The individual enumerators of an enum type get output when we output
73c68f61 12572 the Dwarf representation of the relevant enum type itself. */
a3f97cbb
JW
12573 break;
12574
12575 case FUNCTION_DECL:
4edb7b60
JM
12576 /* Don't output any DIEs to represent mere function declarations,
12577 unless they are class members or explicit block externs. */
12578 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
777ad4c2 12579 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
71dfc51f 12580 break;
bdb669cb 12581
6de9cd9a
DN
12582#if 0
12583 /* FIXME */
12584 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12585 on local redeclarations of global functions. That seems broken. */
12586 if (current_function_decl != decl)
12587 /* This is only a declaration. */;
12588#endif
12589
5daf7c0a
JM
12590 /* If we're emitting a clone, emit info for the abstract instance. */
12591 if (DECL_ORIGIN (decl) != decl)
12592 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
2ad9852d 12593
1cfdcc15
JM
12594 /* If we're emitting an out-of-line copy of an inline function,
12595 emit info for the abstract instance and set up to refer to it. */
1bb17c21
JH
12596 else if (cgraph_function_possibly_inlined_p (decl)
12597 && ! DECL_ABSTRACT (decl)
66c78aa9 12598 && ! class_or_namespace_scope_p (context_die)
5daf7c0a
JM
12599 /* dwarf2out_abstract_function won't emit a die if this is just
12600 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12601 that case, because that works only if we have a die. */
12602 && DECL_INITIAL (decl) != NULL_TREE)
1cfdcc15 12603 {
1edf43d6 12604 dwarf2out_abstract_function (decl);
1cfdcc15
JM
12605 set_decl_origin_self (decl);
12606 }
2ad9852d 12607
5daf7c0a
JM
12608 /* Otherwise we're emitting the primary DIE for this decl. */
12609 else if (debug_info_level > DINFO_LEVEL_TERSE)
a94dbf2c
JM
12610 {
12611 /* Before we describe the FUNCTION_DECL itself, make sure that we
12612 have described its return type. */
12613 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12614
2081603c
JM
12615 /* And its virtual context. */
12616 if (DECL_VINDEX (decl) != NULL_TREE)
12617 gen_type_die (DECL_CONTEXT (decl), context_die);
12618
a94dbf2c
JM
12619 /* And its containing type. */
12620 origin = decl_class_context (decl);
71dfc51f 12621 if (origin != NULL_TREE)
10a11b75 12622 gen_type_die_for_member (origin, decl, context_die);
66c78aa9
JM
12623
12624 /* And its containing namespace. */
12625 declare_in_namespace (decl, context_die);
a94dbf2c 12626 }
a3f97cbb
JW
12627
12628 /* Now output a DIE to represent the function itself. */
12629 gen_subprogram_die (decl, context_die);
12630 break;
12631
12632 case TYPE_DECL:
12633 /* If we are in terse mode, don't generate any DIEs to represent any
73c68f61 12634 actual typedefs. */
a3f97cbb 12635 if (debug_info_level <= DINFO_LEVEL_TERSE)
4927276d 12636 break;
a3f97cbb 12637
2ad9852d 12638 /* In the special case of a TYPE_DECL node representing the declaration
73c68f61
SS
12639 of some type tag, if the given TYPE_DECL is marked as having been
12640 instantiated from some other (original) TYPE_DECL node (e.g. one which
12641 was generated within the original definition of an inline function) we
12642 have to generate a special (abbreviated) DW_TAG_structure_type,
12643 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
2081603c 12644 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
a3f97cbb
JW
12645 {
12646 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12647 break;
12648 }
a3f97cbb 12649
a94dbf2c
JM
12650 if (is_redundant_typedef (decl))
12651 gen_type_die (TREE_TYPE (decl), context_die);
12652 else
71dfc51f
RK
12653 /* Output a DIE to represent the typedef itself. */
12654 gen_typedef_die (decl, context_die);
a3f97cbb
JW
12655 break;
12656
12657 case LABEL_DECL:
12658 if (debug_info_level >= DINFO_LEVEL_NORMAL)
71dfc51f 12659 gen_label_die (decl, context_die);
a3f97cbb
JW
12660 break;
12661
12662 case VAR_DECL:
6de9cd9a 12663 case RESULT_DECL:
a3f97cbb 12664 /* If we are in terse mode, don't generate any DIEs to represent any
73c68f61 12665 variable declarations or definitions. */
a3f97cbb 12666 if (debug_info_level <= DINFO_LEVEL_TERSE)
71dfc51f 12667 break;
a3f97cbb
JW
12668
12669 /* Output any DIEs that are needed to specify the type of this data
73c68f61 12670 object. */
a3f97cbb
JW
12671 gen_type_die (TREE_TYPE (decl), context_die);
12672
a94dbf2c
JM
12673 /* And its containing type. */
12674 origin = decl_class_context (decl);
71dfc51f 12675 if (origin != NULL_TREE)
10a11b75 12676 gen_type_die_for_member (origin, decl, context_die);
a94dbf2c 12677
66c78aa9
JM
12678 /* And its containing namespace. */
12679 declare_in_namespace (decl, context_die);
12680
a3f97cbb 12681 /* Now output the DIE to represent the data object itself. This gets
73c68f61
SS
12682 complicated because of the possibility that the VAR_DECL really
12683 represents an inlined instance of a formal parameter for an inline
12684 function. */
a3f97cbb 12685 origin = decl_ultimate_origin (decl);
71dfc51f
RK
12686 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12687 gen_formal_parameter_die (decl, context_die);
a3f97cbb 12688 else
71dfc51f 12689 gen_variable_die (decl, context_die);
a3f97cbb
JW
12690 break;
12691
12692 case FIELD_DECL:
2ad9852d 12693 /* Ignore the nameless fields that are used to skip bits but handle C++
3199cb41 12694 anonymous unions and structs. */
71dfc51f 12695 if (DECL_NAME (decl) != NULL_TREE
3199cb41
UW
12696 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12697 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
a3f97cbb
JW
12698 {
12699 gen_type_die (member_declared_type (decl), context_die);
12700 gen_field_die (decl, context_die);
12701 }
12702 break;
12703
12704 case PARM_DECL:
12705 gen_type_die (TREE_TYPE (decl), context_die);
12706 gen_formal_parameter_die (decl, context_die);
12707 break;
12708
348bb3c7 12709 case NAMESPACE_DECL:
66c78aa9 12710 gen_namespace_die (decl);
348bb3c7
JM
12711 break;
12712
a3f97cbb 12713 default:
ae0e5982
JM
12714 if ((int)TREE_CODE (decl) > NUM_TREE_CODES)
12715 /* Probably some frontend-internal decl. Assume we don't care. */
12716 break;
a3f97cbb
JW
12717 abort ();
12718 }
a3f97cbb
JW
12719}
12720\f
14a774a9
RK
12721/* Add Ada "use" clause information for SGI Workshop debugger. */
12722
12723void
7080f735 12724dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
14a774a9
RK
12725{
12726 unsigned int file_index;
12727
12728 if (filename != NULL)
12729 {
54ba1f0d 12730 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
556273e0 12731 tree context_list_decl
14a774a9
RK
12732 = build_decl (LABEL_DECL, get_identifier (context_list),
12733 void_type_node);
12734
12735 TREE_PUBLIC (context_list_decl) = TRUE;
12736 add_name_attribute (unit_die, context_list);
981975b6 12737 file_index = lookup_filename (filename);
14a774a9
RK
12738 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12739 add_pubname (context_list_decl, unit_die);
12740 }
12741}
12742
2ad9852d 12743/* Output debug information for global decl DECL. Called from toplev.c after
2b85879e 12744 compilation proper has finished. */
2ad9852d 12745
2b85879e 12746static void
7080f735 12747dwarf2out_global_decl (tree decl)
2b85879e
NB
12748{
12749 /* Output DWARF2 information for file-scope tentative data object
2ad9852d
RK
12750 declarations, file-scope (extern) function declarations (which had no
12751 corresponding body) and file-scope tagged type declarations and
12752 definitions which have not yet been forced out. */
2b85879e
NB
12753 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12754 dwarf2out_decl (decl);
12755}
12756
21d13d83
ZW
12757/* Output debug information for type decl DECL. Called from toplev.c
12758 and from language front ends (to record built-in types). */
12759static void
12760dwarf2out_type_decl (tree decl, int local)
12761{
12762 if (!local)
12763 dwarf2out_decl (decl);
12764}
12765
6097b0c3
DP
12766/* Output debug information for imported module or decl. */
12767
12768static void
12769dwarf2out_imported_module_or_decl (tree decl, tree context)
12770{
12771 dw_die_ref imported_die, at_import_die;
12772 dw_die_ref scope_die;
12773 unsigned file_index;
12774
12775 if (debug_info_level <= DINFO_LEVEL_TERSE)
12776 return;
12777
12778 if (!decl)
12779 abort ();
12780
12781 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12782 We need decl DIE for reference and scope die. First, get DIE for the decl
12783 itself. */
12784
12785 /* Get the scope die for decl context. Use comp_unit_die for global module
12786 or decl. If die is not found for non globals, force new die. */
12787 if (!context)
12788 scope_die = comp_unit_die;
12789 else if (TYPE_P (context))
12790 scope_die = force_type_die (context);
12791 else
12792 scope_die = force_decl_die (context);
12793
834eb1f0
GS
12794 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12795 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
6097b0c3
DP
12796 at_import_die = force_type_die (TREE_TYPE (decl));
12797 else
12798 at_import_die = force_decl_die (decl);
12799
12800 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12801 if (TREE_CODE (decl) == NAMESPACE_DECL)
12802 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12803 else
12804 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12805
12806 file_index = lookup_filename (input_filename);
12807 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12808 add_AT_unsigned (imported_die, DW_AT_decl_line, input_line);
12809 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12810}
12811
71dfc51f
RK
12812/* Write the debugging output for DECL. */
12813
a3f97cbb 12814void
7080f735 12815dwarf2out_decl (tree decl)
a3f97cbb 12816{
b3694847 12817 dw_die_ref context_die = comp_unit_die;
88dad228 12818
a3f97cbb
JW
12819 switch (TREE_CODE (decl))
12820 {
2ad9852d
RK
12821 case ERROR_MARK:
12822 return;
12823
a3f97cbb 12824 case FUNCTION_DECL:
a3f97cbb 12825 /* What we would really like to do here is to filter out all mere
73c68f61
SS
12826 file-scope declarations of file-scope functions which are never
12827 referenced later within this translation unit (and keep all of ones
12828 that *are* referenced later on) but we aren't clairvoyant, so we have
12829 no idea which functions will be referenced in the future (i.e. later
12830 on within the current translation unit). So here we just ignore all
12831 file-scope function declarations which are not also definitions. If
12832 and when the debugger needs to know something about these functions,
12833 it will have to hunt around and find the DWARF information associated
12834 with the definition of the function.
2ad9852d
RK
12835
12836 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
73c68f61
SS
12837 nodes represent definitions and which ones represent mere
12838 declarations. We have to check DECL_INITIAL instead. That's because
12839 the C front-end supports some weird semantics for "extern inline"
12840 function definitions. These can get inlined within the current
12841 translation unit (an thus, we need to generate Dwarf info for their
12842 abstract instances so that the Dwarf info for the concrete inlined
12843 instances can have something to refer to) but the compiler never
12844 generates any out-of-lines instances of such things (despite the fact
12845 that they *are* definitions).
2ad9852d
RK
12846
12847 The important point is that the C front-end marks these "extern
12848 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12849 them anyway. Note that the C++ front-end also plays some similar games
12850 for inline function definitions appearing within include files which
12851 also contain `#pragma interface' pragmas. */
a3f97cbb 12852 if (DECL_INITIAL (decl) == NULL_TREE)
b1ccbc24 12853 return;
88dad228 12854
9c6cd30e
JM
12855 /* If we're a nested function, initially use a parent of NULL; if we're
12856 a plain function, this will be fixed up in decls_for_scope. If
12857 we're a method, it will be ignored, since we already have a DIE. */
8cadae7e
JM
12858 if (decl_function_context (decl)
12859 /* But if we're in terse mode, we don't care about scope. */
12860 && debug_info_level > DINFO_LEVEL_TERSE)
9c6cd30e 12861 context_die = NULL;
a3f97cbb
JW
12862 break;
12863
12864 case VAR_DECL:
556273e0 12865 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
73c68f61
SS
12866 declaration and if the declaration was never even referenced from
12867 within this entire compilation unit. We suppress these DIEs in
12868 order to save space in the .debug section (by eliminating entries
12869 which are probably useless). Note that we must not suppress
12870 block-local extern declarations (whether used or not) because that
12871 would screw-up the debugger's name lookup mechanism and cause it to
12872 miss things which really ought to be in scope at a given point. */
a3f97cbb 12873 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
71dfc51f 12874 return;
a3f97cbb
JW
12875
12876 /* If we are in terse mode, don't generate any DIEs to represent any
73c68f61 12877 variable declarations or definitions. */
a3f97cbb 12878 if (debug_info_level <= DINFO_LEVEL_TERSE)
71dfc51f 12879 return;
a3f97cbb
JW
12880 break;
12881
66c78aa9
JM
12882 case NAMESPACE_DECL:
12883 if (debug_info_level <= DINFO_LEVEL_TERSE)
12884 return;
12885 if (lookup_decl_die (decl) != NULL)
12886 return;
12887 break;
12888
a3f97cbb 12889 case TYPE_DECL:
57fb7689
JM
12890 /* Don't emit stubs for types unless they are needed by other DIEs. */
12891 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12892 return;
12893
a3f97cbb 12894 /* Don't bother trying to generate any DIEs to represent any of the
73c68f61 12895 normal built-in types for the language we are compiling. */
f31686a3 12896 if (DECL_SOURCE_LINE (decl) == 0)
a94dbf2c
JM
12897 {
12898 /* OK, we need to generate one for `bool' so GDB knows what type
73c68f61 12899 comparisons have. */
a94dbf2c
JM
12900 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12901 == DW_LANG_C_plus_plus)
f11c3043
RK
12902 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12903 && ! DECL_IGNORED_P (decl))
a94dbf2c 12904 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
71dfc51f 12905
a94dbf2c
JM
12906 return;
12907 }
a3f97cbb 12908
88dad228 12909 /* If we are in terse mode, don't generate any DIEs for types. */
a3f97cbb 12910 if (debug_info_level <= DINFO_LEVEL_TERSE)
4927276d 12911 return;
88dad228
JM
12912
12913 /* If we're a function-scope tag, initially use a parent of NULL;
12914 this will be fixed up in decls_for_scope. */
12915 if (decl_function_context (decl))
3f76745e 12916 context_die = NULL;
88dad228 12917
a3f97cbb
JW
12918 break;
12919
12920 default:
12921 return;
12922 }
12923
88dad228 12924 gen_decl_die (decl, context_die);
a3f97cbb
JW
12925}
12926
12927/* Output a marker (i.e. a label) for the beginning of the generated code for
12928 a lexical block. */
71dfc51f 12929
a5a42b92 12930static void
7080f735
AJ
12931dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
12932 unsigned int blocknum)
a3f97cbb 12933{
a3f97cbb 12934 function_section (current_function_decl);
8215347e 12935 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
a3f97cbb
JW
12936}
12937
12938/* Output a marker (i.e. a label) for the end of the generated code for a
12939 lexical block. */
71dfc51f 12940
a5a42b92 12941static void
7080f735 12942dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
a3f97cbb 12943{
a3f97cbb 12944 function_section (current_function_decl);
8215347e 12945 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
a3f97cbb
JW
12946}
12947
64b59a80
JM
12948/* Returns nonzero if it is appropriate not to emit any debugging
12949 information for BLOCK, because it doesn't contain any instructions.
fcd7f76b 12950
64b59a80
JM
12951 Don't allow this for blocks with nested functions or local classes
12952 as we would end up with orphans, and in the presence of scheduling
12953 we may end up calling them anyway. */
12954
e1772ac0 12955static bool
7080f735 12956dwarf2out_ignore_block (tree block)
fcd7f76b
JM
12957{
12958 tree decl;
2ad9852d 12959
fcd7f76b 12960 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
64b59a80
JM
12961 if (TREE_CODE (decl) == FUNCTION_DECL
12962 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
12963 return 0;
2ad9852d 12964
64b59a80 12965 return 1;
fcd7f76b
JM
12966}
12967
2ad9852d 12968/* Lookup FILE_NAME (in the list of filenames that we know about here in
9a666dda 12969 dwarf2out.c) and return its "index". The index of each (known) filename is
2ad9852d
RK
12970 just a unique number which is associated with only that one filename. We
12971 need such numbers for the sake of generating labels (in the .debug_sfnames
12972 section) and references to those files numbers (in the .debug_srcinfo
12973 and.debug_macinfo sections). If the filename given as an argument is not
12974 found in our current list, add it to the list and assign it the next
12975 available unique index number. In order to speed up searches, we remember
12976 the index of the filename was looked up last. This handles the majority of
12977 all searches. */
71dfc51f 12978
a3f97cbb 12979static unsigned
7080f735 12980lookup_filename (const char *file_name)
a3f97cbb 12981{
c4274b22
RH
12982 size_t i, n;
12983 char *save_file_name;
a3f97cbb 12984
2e18bbae
RH
12985 /* Check to see if the file name that was searched on the previous
12986 call matches this file name. If so, return the index. */
c4274b22
RH
12987 if (file_table_last_lookup_index != 0)
12988 {
12989 const char *last
12990 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
12991 if (strcmp (file_name, last) == 0)
73c68f61 12992 return file_table_last_lookup_index;
c4274b22 12993 }
a3f97cbb
JW
12994
12995 /* Didn't match the previous lookup, search the table */
c4274b22
RH
12996 n = VARRAY_ACTIVE_SIZE (file_table);
12997 for (i = 1; i < n; i++)
12998 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
71dfc51f 12999 {
c4274b22 13000 file_table_last_lookup_index = i;
71dfc51f
RK
13001 return i;
13002 }
a3f97cbb 13003
71dfc51f 13004 /* Add the new entry to the end of the filename table. */
c4274b22
RH
13005 file_table_last_lookup_index = n;
13006 save_file_name = (char *) ggc_strdup (file_name);
13007 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
73c68f61 13008 VARRAY_PUSH_UINT (file_table_emitted, 0);
2e18bbae 13009
73c68f61
SS
13010 return i;
13011}
13012
13013static int
7080f735 13014maybe_emit_file (int fileno)
73c68f61 13015{
73c68f61 13016 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
211a0cbe 13017 {
73c68f61
SS
13018 if (!VARRAY_UINT (file_table_emitted, fileno))
13019 {
13020 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13021 fprintf (asm_out_file, "\t.file %u ",
13022 VARRAY_UINT (file_table_emitted, fileno));
13023 output_quoted_string (asm_out_file,
13024 VARRAY_CHAR_PTR (file_table, fileno));
13025 fputc ('\n', asm_out_file);
13026 }
13027 return VARRAY_UINT (file_table_emitted, fileno);
211a0cbe 13028 }
73c68f61
SS
13029 else
13030 return fileno;
2e18bbae
RH
13031}
13032
13033static void
7080f735 13034init_file_table (void)
2e18bbae
RH
13035{
13036 /* Allocate the initial hunk of the file_table. */
c4274b22 13037 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
73c68f61 13038 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
71dfc51f 13039
2e18bbae 13040 /* Skip the first entry - file numbers begin at 1. */
c4274b22 13041 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
73c68f61 13042 VARRAY_PUSH_UINT (file_table_emitted, 0);
c4274b22 13043 file_table_last_lookup_index = 0;
a3f97cbb
JW
13044}
13045
0a2d3d69
DB
13046/* Called by the final INSN scan whenever we see a var location. We
13047 use it to drop labels in the right places, and throw the location in
13048 our lookup table. */
13049
13050static void
13051dwarf2out_var_location (rtx loc_note)
13052{
13053 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13054 struct var_loc_node *newloc;
13055 rtx prev_insn;
13056 static rtx last_insn;
13057 static const char *last_label;
13058
13059 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13060 return;
13061 prev_insn = PREV_INSN (loc_note);
13062
13063 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13064 /* If the insn we processed last time is the previous insn
13065 and it is also a var location note, use the label we emitted
13066 last time. */
13067 if (last_insn != NULL_RTX
13068 && last_insn == prev_insn
13069 && GET_CODE (prev_insn) == NOTE
13070 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13071 {
13072 newloc->label = last_label;
13073 }
13074 else
13075 {
13076 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13077 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13078 loclabel_num++;
13079 newloc->label = ggc_strdup (loclabel);
13080 }
13081 newloc->var_loc_note = loc_note;
13082 newloc->next = NULL;
13083
13084 last_insn = loc_note;
13085 last_label = newloc->label;
13086
13087 add_var_loc_to_decl (NOTE_VAR_LOCATION_DECL (loc_note), newloc);
13088}
13089
13090/* We need to reset the locations at the beginning of each
13091 function. We can't do this in the end_function hook, because the
13092 declarations that use the locations won't have been outputted when
13093 that hook is called. */
13094
13095static void
13096dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13097{
13098 htab_empty (decl_loc_table);
13099}
13100
a3f97cbb
JW
13101/* Output a label to mark the beginning of a source code line entry
13102 and record information relating to this source line, in
13103 'line_info_table' for later output of the .debug_line section. */
71dfc51f 13104
e2a12aca 13105static void
7080f735 13106dwarf2out_source_line (unsigned int line, const char *filename)
a3f97cbb 13107{
7bf6b23d
JM
13108 if (debug_info_level >= DINFO_LEVEL_NORMAL
13109 && line != 0)
a3f97cbb
JW
13110 {
13111 function_section (current_function_decl);
a3f97cbb 13112
8aaf55ac
JM
13113 /* If requested, emit something human-readable. */
13114 if (flag_debug_asm)
13115 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13116 filename, line);
13117
b2244e22
JW
13118 if (DWARF2_ASM_LINE_DEBUG_INFO)
13119 {
981975b6 13120 unsigned file_num = lookup_filename (filename);
b2244e22 13121
73c68f61
SS
13122 file_num = maybe_emit_file (file_num);
13123
981975b6 13124 /* Emit the .loc directive understood by GNU as. */
2e18bbae 13125 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
b2244e22
JW
13126
13127 /* Indicate that line number info exists. */
2ad9852d 13128 line_info_table_in_use++;
b2244e22
JW
13129
13130 /* Indicate that multiple line number tables exist. */
13131 if (DECL_SECTION_NAME (current_function_decl))
2ad9852d 13132 separate_line_info_table_in_use++;
b2244e22
JW
13133 }
13134 else if (DECL_SECTION_NAME (current_function_decl))
a3f97cbb 13135 {
b3694847 13136 dw_separate_line_info_ref line_info;
5fd9b178 13137 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
5c90448c 13138 separate_line_info_table_in_use);
e90b62db 13139
a1105617 13140 /* Expand the line info table if necessary. */
e90b62db
JM
13141 if (separate_line_info_table_in_use
13142 == separate_line_info_table_allocated)
13143 {
13144 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13145 separate_line_info_table
703ad42b 13146 = ggc_realloc (separate_line_info_table,
17211ab5
GK
13147 separate_line_info_table_allocated
13148 * sizeof (dw_separate_line_info_entry));
703ad42b
KG
13149 memset (separate_line_info_table
13150 + separate_line_info_table_in_use,
17211ab5 13151 0,
7080f735 13152 (LINE_INFO_TABLE_INCREMENT
17211ab5 13153 * sizeof (dw_separate_line_info_entry)));
e90b62db 13154 }
71dfc51f
RK
13155
13156 /* Add the new entry at the end of the line_info_table. */
e90b62db
JM
13157 line_info
13158 = &separate_line_info_table[separate_line_info_table_in_use++];
981975b6 13159 line_info->dw_file_num = lookup_filename (filename);
e90b62db 13160 line_info->dw_line_num = line;
df696a75 13161 line_info->function = current_function_funcdef_no;
e90b62db
JM
13162 }
13163 else
13164 {
b3694847 13165 dw_line_info_ref line_info;
71dfc51f 13166
5fd9b178 13167 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
5c90448c 13168 line_info_table_in_use);
e90b62db 13169
71dfc51f 13170 /* Expand the line info table if necessary. */
e90b62db
JM
13171 if (line_info_table_in_use == line_info_table_allocated)
13172 {
13173 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13174 line_info_table
17211ab5
GK
13175 = ggc_realloc (line_info_table,
13176 (line_info_table_allocated
13177 * sizeof (dw_line_info_entry)));
13178 memset (line_info_table + line_info_table_in_use, 0,
13179 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
e90b62db 13180 }
71dfc51f
RK
13181
13182 /* Add the new entry at the end of the line_info_table. */
e90b62db 13183 line_info = &line_info_table[line_info_table_in_use++];
981975b6 13184 line_info->dw_file_num = lookup_filename (filename);
e90b62db 13185 line_info->dw_line_num = line;
a3f97cbb 13186 }
a3f97cbb
JW
13187 }
13188}
13189
30f7a378 13190/* Record the beginning of a new source file. */
71dfc51f 13191
7f905405 13192static void
7080f735 13193dwarf2out_start_source_file (unsigned int lineno, const char *filename)
a3f97cbb 13194{
8a7a6f4d 13195 if (flag_eliminate_dwarf2_dups)
881c6935
JM
13196 {
13197 /* Record the beginning of the file for break_out_includes. */
cc0017a9
ZD
13198 dw_die_ref bincl_die;
13199
13200 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
881c6935
JM
13201 add_AT_string (bincl_die, DW_AT_name, filename);
13202 }
2ad9852d 13203
84a5b4f8
DB
13204 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13205 {
715bdd29 13206 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
84a5b4f8 13207 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
7c262518
RH
13208 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13209 lineno);
73c68f61 13210 maybe_emit_file (lookup_filename (filename));
7c262518
RH
13211 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13212 "Filename we just started");
84a5b4f8 13213 }
a3f97cbb
JW
13214}
13215
cc260610 13216/* Record the end of a source file. */
71dfc51f 13217
7f905405 13218static void
7080f735 13219dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
a3f97cbb 13220{
881c6935 13221 if (flag_eliminate_dwarf2_dups)
2ad9852d 13222 /* Record the end of the file for break_out_includes. */
54ba1f0d 13223 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
2ad9852d 13224
84a5b4f8
DB
13225 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13226 {
715bdd29 13227 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
84a5b4f8
DB
13228 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13229 }
a3f97cbb
JW
13230}
13231
cc260610 13232/* Called from debug_define in toplev.c. The `buffer' parameter contains
a3f97cbb
JW
13233 the tail part of the directive line, i.e. the part which is past the
13234 initial whitespace, #, whitespace, directive-name, whitespace part. */
71dfc51f 13235
7f905405 13236static void
7080f735
AJ
13237dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13238 const char *buffer ATTRIBUTE_UNUSED)
a3f97cbb 13239{
84a5b4f8
DB
13240 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13241 {
715bdd29 13242 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
84a5b4f8
DB
13243 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13244 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13245 dw2_asm_output_nstring (buffer, -1, "The macro");
13246 }
a3f97cbb
JW
13247}
13248
cc260610 13249/* Called from debug_undef in toplev.c. The `buffer' parameter contains
a3f97cbb
JW
13250 the tail part of the directive line, i.e. the part which is past the
13251 initial whitespace, #, whitespace, directive-name, whitespace part. */
71dfc51f 13252
7f905405 13253static void
7080f735
AJ
13254dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13255 const char *buffer ATTRIBUTE_UNUSED)
a3f97cbb 13256{
84a5b4f8
DB
13257 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13258 {
715bdd29 13259 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
84a5b4f8
DB
13260 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13261 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13262 dw2_asm_output_nstring (buffer, -1, "The macro");
13263 }
a3f97cbb
JW
13264}
13265
13266/* Set up for Dwarf output at the start of compilation. */
71dfc51f 13267
a51d908e 13268static void
7080f735 13269dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
a3f97cbb 13270{
acc187f5
RH
13271 init_file_table ();
13272
0a2d3d69 13273 /* Allocate the decl_die_table. */
636c7bc4
JZ
13274 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13275 decl_die_table_eq, NULL);
0a2d3d69
DB
13276
13277 /* Allocate the decl_loc_table. */
13278 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13279 decl_loc_table_eq, NULL);
a3f97cbb
JW
13280
13281 /* Allocate the initial hunk of the decl_scope_table. */
244a4af0 13282 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
a3f97cbb
JW
13283
13284 /* Allocate the initial hunk of the abbrev_die_table. */
17211ab5
GK
13285 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13286 * sizeof (dw_die_ref));
a3f97cbb 13287 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
71dfc51f 13288 /* Zero-th entry is allocated, but unused */
a3f97cbb
JW
13289 abbrev_die_table_in_use = 1;
13290
13291 /* Allocate the initial hunk of the line_info_table. */
17211ab5
GK
13292 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13293 * sizeof (dw_line_info_entry));
a3f97cbb 13294 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
2ad9852d 13295
71dfc51f 13296 /* Zero-th entry is allocated, but unused */
a3f97cbb
JW
13297 line_info_table_in_use = 1;
13298
556273e0 13299 /* Generate the initial DIE for the .debug section. Note that the (string)
a3f97cbb 13300 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
556273e0 13301 will (typically) be a relative pathname and that this pathname should be
a3f97cbb 13302 taken as being relative to the directory from which the compiler was
c4274b22
RH
13303 invoked when the given (base) source file was compiled. We will fill
13304 in this value in dwarf2out_finish. */
13305 comp_unit_die = gen_compile_unit_die (NULL);
a3f97cbb 13306
244a4af0 13307 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
244a4af0 13308
1f8f4a0b 13309 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
1865dbb5 13310
5c90448c 13311 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
9d2f2c45
RH
13312 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13313 DEBUG_ABBREV_SECTION_LABEL, 0);
b366352b
MM
13314 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13315 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13316 else
f99ffb60 13317 strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
2ad9852d 13318
556273e0 13319 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
8b790721 13320 DEBUG_INFO_SECTION_LABEL, 0);
556273e0 13321 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
8b790721 13322 DEBUG_LINE_SECTION_LABEL, 0);
2bee6045
JJ
13323 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13324 DEBUG_RANGES_SECTION_LABEL, 0);
715bdd29 13325 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
8b790721 13326 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
715bdd29 13327 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
8b790721 13328 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
715bdd29 13329 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
8b790721 13330 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
2ad9852d 13331
84a5b4f8
DB
13332 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13333 {
715bdd29 13334 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
84a5b4f8
DB
13335 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13336 DEBUG_MACINFO_SECTION_LABEL, 0);
13337 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13338 }
7c262518
RH
13339
13340 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13341 {
13342 text_section ();
13343 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13344 }
a3f97cbb
JW
13345}
13346
9eb4015a
JJ
13347/* A helper function for dwarf2out_finish called through
13348 ht_forall. Emit one queued .debug_str string. */
13349
13350static int
7080f735 13351output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
9eb4015a 13352{
17211ab5 13353 struct indirect_string_node *node = (struct indirect_string_node *) *h;
9eb4015a 13354
9eb4015a
JJ
13355 if (node->form == DW_FORM_strp)
13356 {
13357 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13358 ASM_OUTPUT_LABEL (asm_out_file, node->label);
17211ab5 13359 assemble_string (node->str, strlen (node->str) + 1);
9eb4015a 13360 }
2ad9852d 13361
9eb4015a
JJ
13362 return 1;
13363}
13364
73c68f61
SS
13365
13366
13367/* Clear the marks for a die and its children.
3dc575ff 13368 Be cool if the mark isn't set. */
73c68f61
SS
13369
13370static void
7080f735 13371prune_unmark_dies (dw_die_ref die)
73c68f61
SS
13372{
13373 dw_die_ref c;
13374 die->die_mark = 0;
13375 for (c = die->die_child; c; c = c->die_sib)
13376 prune_unmark_dies (c);
13377}
13378
13379
13380/* Given DIE that we're marking as used, find any other dies
13381 it references as attributes and mark them as used. */
13382
13383static void
7080f735 13384prune_unused_types_walk_attribs (dw_die_ref die)
73c68f61
SS
13385{
13386 dw_attr_ref a;
13387
13388 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13389 {
13390 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13391 {
13392 /* A reference to another DIE.
13393 Make sure that it will get emitted. */
13394 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13395 }
13396 else if (a->dw_attr == DW_AT_decl_file)
13397 {
13398 /* A reference to a file. Make sure the file name is emitted. */
13399 a->dw_attr_val.v.val_unsigned =
13400 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13401 }
13402 }
13403}
13404
13405
13406/* Mark DIE as being used. If DOKIDS is true, then walk down
13407 to DIE's children. */
13408
13409static void
7080f735 13410prune_unused_types_mark (dw_die_ref die, int dokids)
73c68f61
SS
13411{
13412 dw_die_ref c;
13413
13414 if (die->die_mark == 0)
13415 {
13416 /* We haven't done this node yet. Mark it as used. */
13417 die->die_mark = 1;
13418
13419 /* We also have to mark its parents as used.
13420 (But we don't want to mark our parents' kids due to this.) */
13421 if (die->die_parent)
13422 prune_unused_types_mark (die->die_parent, 0);
13423
13424 /* Mark any referenced nodes. */
13425 prune_unused_types_walk_attribs (die);
47fcfa7b
SS
13426
13427 /* If this node is a specification,
6614fd40 13428 also mark the definition, if it exists. */
47fcfa7b
SS
13429 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13430 prune_unused_types_mark (die->die_definition, 1);
73c68f61
SS
13431 }
13432
13433 if (dokids && die->die_mark != 2)
13434 {
13435 /* We need to walk the children, but haven't done so yet.
13436 Remember that we've walked the kids. */
13437 die->die_mark = 2;
13438
13439 /* Walk them. */
13440 for (c = die->die_child; c; c = c->die_sib)
13441 {
13442 /* If this is an array type, we need to make sure our
3dc575ff 13443 kids get marked, even if they're types. */
73c68f61
SS
13444 if (die->die_tag == DW_TAG_array_type)
13445 prune_unused_types_mark (c, 1);
13446 else
13447 prune_unused_types_walk (c);
13448 }
13449 }
13450}
13451
13452
13453/* Walk the tree DIE and mark types that we actually use. */
13454
13455static void
7080f735 13456prune_unused_types_walk (dw_die_ref die)
73c68f61
SS
13457{
13458 dw_die_ref c;
13459
13460 /* Don't do anything if this node is already marked. */
13461 if (die->die_mark)
13462 return;
13463
13464 switch (die->die_tag) {
13465 case DW_TAG_const_type:
13466 case DW_TAG_packed_type:
13467 case DW_TAG_pointer_type:
13468 case DW_TAG_reference_type:
13469 case DW_TAG_volatile_type:
13470 case DW_TAG_typedef:
13471 case DW_TAG_array_type:
13472 case DW_TAG_structure_type:
13473 case DW_TAG_union_type:
13474 case DW_TAG_class_type:
13475 case DW_TAG_friend:
13476 case DW_TAG_variant_part:
13477 case DW_TAG_enumeration_type:
13478 case DW_TAG_subroutine_type:
13479 case DW_TAG_string_type:
13480 case DW_TAG_set_type:
13481 case DW_TAG_subrange_type:
13482 case DW_TAG_ptr_to_member_type:
13483 case DW_TAG_file_type:
13484 /* It's a type node --- don't mark it. */
13485 return;
13486
13487 default:
13488 /* Mark everything else. */
13489 break;
13490 }
13491
13492 die->die_mark = 1;
13493
13494 /* Now, mark any dies referenced from here. */
13495 prune_unused_types_walk_attribs (die);
13496
13497 /* Mark children. */
13498 for (c = die->die_child; c; c = c->die_sib)
13499 prune_unused_types_walk (c);
13500}
13501
13502
13503/* Remove from the tree DIE any dies that aren't marked. */
13504
13505static void
7080f735 13506prune_unused_types_prune (dw_die_ref die)
73c68f61
SS
13507{
13508 dw_die_ref c, p, n;
13509 if (!die->die_mark)
13510 abort();
13511
13512 p = NULL;
13513 for (c = die->die_child; c; c = n)
13514 {
13515 n = c->die_sib;
13516 if (c->die_mark)
13517 {
13518 prune_unused_types_prune (c);
13519 p = c;
13520 }
13521 else
13522 {
13523 if (p)
13524 p->die_sib = n;
13525 else
13526 die->die_child = n;
13527 free_die (c);
13528 }
13529 }
13530}
13531
13532
13533/* Remove dies representing declarations that we never use. */
13534
13535static void
7080f735 13536prune_unused_types (void)
73c68f61
SS
13537{
13538 unsigned int i;
13539 limbo_die_node *node;
13540
13541 /* Clear all the marks. */
13542 prune_unmark_dies (comp_unit_die);
13543 for (node = limbo_die_list; node; node = node->next)
13544 prune_unmark_dies (node->die);
13545
13546 /* Set the mark on nodes that are actually used. */
13547 prune_unused_types_walk (comp_unit_die);
13548 for (node = limbo_die_list; node; node = node->next)
13549 prune_unused_types_walk (node->die);
13550
13551 /* Also set the mark on nodes referenced from the
13552 pubname_table or arange_table. */
6a87d634
RS
13553 for (i = 0; i < pubname_table_in_use; i++)
13554 prune_unused_types_mark (pubname_table[i].die, 1);
13555 for (i = 0; i < arange_table_in_use; i++)
13556 prune_unused_types_mark (arange_table[i], 1);
73c68f61
SS
13557
13558 /* Get rid of nodes that aren't marked. */
13559 prune_unused_types_prune (comp_unit_die);
13560 for (node = limbo_die_list; node; node = node->next)
13561 prune_unused_types_prune (node->die);
13562
13563 /* Leave the marks clear. */
13564 prune_unmark_dies (comp_unit_die);
13565 for (node = limbo_die_list; node; node = node->next)
13566 prune_unmark_dies (node->die);
13567}
13568
a3f97cbb
JW
13569/* Output stuff that dwarf requires at the end of every file,
13570 and generate the DWARF-2 debugging info. */
71dfc51f 13571
a51d908e 13572static void
7080f735 13573dwarf2out_finish (const char *filename)
a3f97cbb 13574{
ef76d03b 13575 limbo_die_node *node, *next_node;
ae0ed63a 13576 dw_die_ref die = 0;
ef76d03b 13577
c4274b22
RH
13578 /* Add the name for the main input file now. We delayed this from
13579 dwarf2out_init to avoid complications with PCH. */
3b895f8e
NS
13580 add_name_attribute (comp_unit_die, filename);
13581 if (filename[0] != DIR_SEPARATOR)
c4274b22 13582 add_comp_dir_attribute (comp_unit_die);
79c758fb
JJ
13583 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13584 {
13585 size_t i;
13586 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
e3091a5f
R
13587 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13588 /* Don't add cwd for <built-in>. */
13589 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
79c758fb
JJ
13590 {
13591 add_comp_dir_attribute (comp_unit_die);
13592 break;
13593 }
13594 }
c4274b22 13595
ef76d03b
JW
13596 /* Traverse the limbo die list, and add parent/child links. The only
13597 dies without parents that should be here are concrete instances of
13598 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13599 For concrete instances, we can get the parent die from the abstract
13600 instance. */
13601 for (node = limbo_die_list; node; node = next_node)
13602 {
13603 next_node = node->next;
13604 die = node->die;
13605
13606 if (die->die_parent == NULL)
13607 {
a96c67ec 13608 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
54ba1f0d 13609 tree context;
2ad9852d 13610
a96c67ec
JM
13611 if (origin)
13612 add_child_die (origin->die_parent, die);
ef76d03b 13613 else if (die == comp_unit_die)
a96c67ec 13614 ;
aea9695c
RK
13615 /* If this was an expression for a bound involved in a function
13616 return type, it may be a SAVE_EXPR for which we weren't able
13617 to find a DIE previously. So try now. */
13618 else if (node->created_for
13619 && TREE_CODE (node->created_for) == SAVE_EXPR
13620 && 0 != (origin = (lookup_decl_die
13621 (SAVE_EXPR_CONTEXT
13622 (node->created_for)))))
13623 add_child_die (origin, die);
6bb28965
JM
13624 else if (errorcount > 0 || sorrycount > 0)
13625 /* It's OK to be confused by errors in the input. */
13626 add_child_die (comp_unit_die, die);
54ba1f0d
RH
13627 else if (node->created_for
13628 && ((DECL_P (node->created_for)
c26fbbca 13629 && (context = DECL_CONTEXT (node->created_for)))
54ba1f0d
RH
13630 || (TYPE_P (node->created_for)
13631 && (context = TYPE_CONTEXT (node->created_for))))
13632 && TREE_CODE (context) == FUNCTION_DECL)
13633 {
13634 /* In certain situations, the lexical block containing a
13635 nested function can be optimized away, which results
13636 in the nested function die being orphaned. Likewise
13637 with the return type of that nested function. Force
13638 this to be a child of the containing function. */
13639 origin = lookup_decl_die (context);
13640 if (! origin)
13641 abort ();
13642 add_child_die (origin, die);
13643 }
ef76d03b
JW
13644 else
13645 abort ();
13646 }
ef76d03b 13647 }
2ad9852d 13648
a96c67ec 13649 limbo_die_list = NULL;
ef76d03b 13650
8a8c3656
JM
13651 /* Walk through the list of incomplete types again, trying once more to
13652 emit full debugging info for them. */
13653 retry_incomplete_types ();
13654
881c6935
JM
13655 /* We need to reverse all the dies before break_out_includes, or
13656 we'll see the end of an include file before the beginning. */
13657 reverse_all_dies (comp_unit_die);
13658
03275f81
ZD
13659 if (flag_eliminate_unused_debug_types)
13660 prune_unused_types ();
13661
881c6935
JM
13662 /* Generate separate CUs for each of the include files we've seen.
13663 They will go into limbo_die_list. */
5f632b5e
JM
13664 if (flag_eliminate_dwarf2_dups)
13665 break_out_includes (comp_unit_die);
881c6935
JM
13666
13667 /* Traverse the DIE's and add add sibling attributes to those DIE's
13668 that have children. */
a3f97cbb 13669 add_sibling_attributes (comp_unit_die);
881c6935
JM
13670 for (node = limbo_die_list; node; node = node->next)
13671 add_sibling_attributes (node->die);
a3f97cbb
JW
13672
13673 /* Output a terminator label for the .text section. */
7c262518 13674 text_section ();
5fd9b178 13675 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
a3f97cbb 13676
db3c0315
MM
13677 /* Output the source line correspondence table. We must do this
13678 even if there is no line information. Otherwise, on an empty
13679 translation unit, we will generate a present, but empty,
13680 .debug_info section. IRIX 6.5 `nm' will then complain when
13681 examining the file. */
13682 if (! DWARF2_ASM_LINE_DEBUG_INFO)
e90b62db 13683 {
715bdd29 13684 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
db3c0315
MM
13685 output_line_info ();
13686 }
71dfc51f 13687
b38a75e5
RH
13688 /* Output location list section if necessary. */
13689 if (have_location_lists)
13690 {
13691 /* Output the location lists info. */
13692 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13693 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13694 DEBUG_LOC_SECTION_LABEL, 0);
13695 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13696 output_location_lists (die);
13697 have_location_lists = 0;
13698 }
13699
db3c0315
MM
13700 /* We can only use the low/high_pc attributes if all of the code was
13701 in .text. */
13702 if (separate_line_info_table_in_use == 0)
13703 {
13704 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13705 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
e90b62db 13706 }
2ad9852d
RK
13707
13708 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13709 "base address". Use zero so that these addresses become absolute. */
a20612aa
RH
13710 else if (have_location_lists || ranges_table_in_use)
13711 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
e90b62db 13712
fe7cd37f
RH
13713 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13714 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13715 debug_line_section_label);
db3c0315 13716
84a5b4f8
DB
13717 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13718 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
a96c67ec 13719
881c6935
JM
13720 /* Output all of the compilation units. We put the main one last so that
13721 the offsets are available to output_pubnames. */
13722 for (node = limbo_die_list; node; node = node->next)
cc0017a9 13723 output_comp_unit (node->die, 0);
2ad9852d 13724
cc0017a9 13725 output_comp_unit (comp_unit_die, 0);
881c6935 13726
a3f97cbb 13727 /* Output the abbreviation table. */
715bdd29 13728 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
a3f97cbb
JW
13729 output_abbrev_section ();
13730
2ad9852d 13731 /* Output public names table if necessary. */
d291dd49
JM
13732 if (pubname_table_in_use)
13733 {
715bdd29 13734 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
d291dd49
JM
13735 output_pubnames ();
13736 }
13737
2ad9852d
RK
13738 /* Output the address range information. We only put functions in the arange
13739 table, so don't write it out if we don't have any. */
a3f97cbb
JW
13740 if (fde_table_in_use)
13741 {
715bdd29 13742 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
a3f97cbb
JW
13743 output_aranges ();
13744 }
a20612aa 13745
a20612aa
RH
13746 /* Output ranges section if necessary. */
13747 if (ranges_table_in_use)
13748 {
715bdd29 13749 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
2bee6045 13750 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
a20612aa
RH
13751 output_ranges ();
13752 }
13753
30f7a378 13754 /* Have to end the primary source file. */
cc260610 13755 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
c26fbbca 13756 {
715bdd29 13757 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
cc260610 13758 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
2f8d482e 13759 dw2_asm_output_data (1, 0, "End compilation unit");
cc260610 13760 }
9eb4015a 13761
2ad9852d 13762 /* If we emitted any DW_FORM_strp form attribute, output the string
9eb4015a
JJ
13763 table too. */
13764 if (debug_str_hash)
17211ab5 13765 htab_traverse (debug_str_hash, output_indirect_string, NULL);
a3f97cbb 13766}
e2500fed
GK
13767#else
13768
13769/* This should never be used, but its address is needed for comparisons. */
13770const struct gcc_debug_hooks dwarf2_debug_hooks;
13771
13772#endif /* DWARF2_DEBUGGING_INFO */
13773
13774#include "gt-dwarf2out.h"