inclhack.def (hpux_imaginary_i): Remove spaces.
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6 Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25 the file numbers are used by .debug_info. Alternately, leave
26 out locations for types and decls.
27 Avoid talking about ctors and op= for PODs.
28 Factor out common prologue sequences into multiple CIEs. */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31 information, which is also used by the GCC efficient exception handling
32 mechanism. The second part, controlled only by an #ifdef
33 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34 information. */
35
36 /* DWARF2 Abbreviation Glossary:
37
38 CFA = Canonical Frame Address
39 a fixed address on the stack which identifies a call frame.
40 We define it to be the value of SP just before the call insn.
41 The CFA register and offset, which may change during the course
42 of the function, are used to calculate its value at runtime.
43
44 CFI = Call Frame Instruction
45 an instruction for the DWARF2 abstract machine
46
47 CIE = Common Information Entry
48 information describing information common to one or more FDEs
49
50 DIE = Debugging Information Entry
51
52 FDE = Frame Description Entry
53 information describing the stack call frame, in particular,
54 how to restore registers
55
56 DW_CFA_... = DWARF2 CFA call frame instruction
57 DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
95
96 static rtx last_var_location_insn;
97 #endif
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103 which are output in the debug info should end with a separator. */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106 from generating indirect strings in DWARF2 debug information, for instance
107 if your target is stuck with an old version of GDB that is unable to
108 process them properly or uses VMS Debug. */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 #ifndef DWARF2_FRAME_INFO
116 # ifdef DWARF2_DEBUGGING_INFO
117 # define DWARF2_FRAME_INFO \
118 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
119 # else
120 # define DWARF2_FRAME_INFO 0
121 # endif
122 #endif
123
124 /* Map register numbers held in the call frame info that gcc has
125 collected using DWARF_FRAME_REGNUM to those that should be output in
126 .debug_frame and .eh_frame. */
127 #ifndef DWARF2_FRAME_REG_OUT
128 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
129 #endif
130
131 /* Save the result of dwarf2out_do_frame across PCH. */
132 static GTY(()) bool saved_do_cfi_asm = 0;
133
134 /* Decide whether we want to emit frame unwind information for the current
135 translation unit. */
136
137 int
138 dwarf2out_do_frame (void)
139 {
140 /* We want to emit correct CFA location expressions or lists, so we
141 have to return true if we're going to output debug info, even if
142 we're not going to output frame or unwind info. */
143 return (write_symbols == DWARF2_DEBUG
144 || write_symbols == VMS_AND_DWARF2_DEBUG
145 || DWARF2_FRAME_INFO || saved_do_cfi_asm
146 #ifdef DWARF2_UNWIND_INFO
147 || (DWARF2_UNWIND_INFO
148 && (flag_unwind_tables
149 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
150 #endif
151 );
152 }
153
154 /* Decide whether to emit frame unwind via assembler directives. */
155
156 int
157 dwarf2out_do_cfi_asm (void)
158 {
159 int enc;
160
161 #ifdef MIPS_DEBUGGING_INFO
162 return false;
163 #endif
164 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
165 return false;
166 if (saved_do_cfi_asm || !eh_personality_libfunc)
167 return true;
168 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
169 return false;
170
171 /* Make sure the personality encoding is one the assembler can support.
172 In particular, aligned addresses can't be handled. */
173 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
174 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
175 return false;
176 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
177 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
178 return false;
179
180 saved_do_cfi_asm = true;
181 return true;
182 }
183
184 /* The size of the target's pointer type. */
185 #ifndef PTR_SIZE
186 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
187 #endif
188
189 /* Array of RTXes referenced by the debugging information, which therefore
190 must be kept around forever. */
191 static GTY(()) VEC(rtx,gc) *used_rtx_array;
192
193 /* A pointer to the base of a list of incomplete types which might be
194 completed at some later time. incomplete_types_list needs to be a
195 VEC(tree,gc) because we want to tell the garbage collector about
196 it. */
197 static GTY(()) VEC(tree,gc) *incomplete_types;
198
199 /* A pointer to the base of a table of references to declaration
200 scopes. This table is a display which tracks the nesting
201 of declaration scopes at the current scope and containing
202 scopes. This table is used to find the proper place to
203 define type declaration DIE's. */
204 static GTY(()) VEC(tree,gc) *decl_scope_table;
205
206 /* Pointers to various DWARF2 sections. */
207 static GTY(()) section *debug_info_section;
208 static GTY(()) section *debug_abbrev_section;
209 static GTY(()) section *debug_aranges_section;
210 static GTY(()) section *debug_macinfo_section;
211 static GTY(()) section *debug_line_section;
212 static GTY(()) section *debug_loc_section;
213 static GTY(()) section *debug_pubnames_section;
214 static GTY(()) section *debug_pubtypes_section;
215 static GTY(()) section *debug_str_section;
216 static GTY(()) section *debug_ranges_section;
217 static GTY(()) section *debug_frame_section;
218
219 /* How to start an assembler comment. */
220 #ifndef ASM_COMMENT_START
221 #define ASM_COMMENT_START ";#"
222 #endif
223
224 typedef struct dw_cfi_struct *dw_cfi_ref;
225 typedef struct dw_fde_struct *dw_fde_ref;
226 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
227
228 /* Call frames are described using a sequence of Call Frame
229 Information instructions. The register number, offset
230 and address fields are provided as possible operands;
231 their use is selected by the opcode field. */
232
233 enum dw_cfi_oprnd_type {
234 dw_cfi_oprnd_unused,
235 dw_cfi_oprnd_reg_num,
236 dw_cfi_oprnd_offset,
237 dw_cfi_oprnd_addr,
238 dw_cfi_oprnd_loc
239 };
240
241 typedef union GTY(()) dw_cfi_oprnd_struct {
242 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
243 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
244 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
245 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
246 }
247 dw_cfi_oprnd;
248
249 typedef struct GTY(()) dw_cfi_struct {
250 dw_cfi_ref dw_cfi_next;
251 enum dwarf_call_frame_info dw_cfi_opc;
252 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
253 dw_cfi_oprnd1;
254 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
255 dw_cfi_oprnd2;
256 }
257 dw_cfi_node;
258
259 /* This is how we define the location of the CFA. We use to handle it
260 as REG + OFFSET all the time, but now it can be more complex.
261 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
262 Instead of passing around REG and OFFSET, we pass a copy
263 of this structure. */
264 typedef struct GTY(()) cfa_loc {
265 HOST_WIDE_INT offset;
266 HOST_WIDE_INT base_offset;
267 unsigned int reg;
268 BOOL_BITFIELD indirect : 1; /* 1 if CFA is accessed via a dereference. */
269 BOOL_BITFIELD in_use : 1; /* 1 if a saved cfa is stored here. */
270 } dw_cfa_location;
271
272 /* All call frame descriptions (FDE's) in the GCC generated DWARF
273 refer to a single Common Information Entry (CIE), defined at
274 the beginning of the .debug_frame section. This use of a single
275 CIE obviates the need to keep track of multiple CIE's
276 in the DWARF generation routines below. */
277
278 typedef struct GTY(()) dw_fde_struct {
279 tree decl;
280 const char *dw_fde_begin;
281 const char *dw_fde_current_label;
282 const char *dw_fde_end;
283 const char *dw_fde_hot_section_label;
284 const char *dw_fde_hot_section_end_label;
285 const char *dw_fde_unlikely_section_label;
286 const char *dw_fde_unlikely_section_end_label;
287 dw_cfi_ref dw_fde_cfi;
288 dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections. */
289 unsigned funcdef_number;
290 HOST_WIDE_INT stack_realignment;
291 /* Dynamic realign argument pointer register. */
292 unsigned int drap_reg;
293 /* Virtual dynamic realign argument pointer register. */
294 unsigned int vdrap_reg;
295 unsigned all_throwers_are_sibcalls : 1;
296 unsigned nothrow : 1;
297 unsigned uses_eh_lsda : 1;
298 /* Whether we did stack realign in this call frame. */
299 unsigned stack_realign : 1;
300 /* Whether dynamic realign argument pointer register has been saved. */
301 unsigned drap_reg_saved: 1;
302 /* True iff dw_fde_begin label is in text_section or cold_text_section. */
303 unsigned in_std_section : 1;
304 /* True iff dw_fde_unlikely_section_label is in text_section or
305 cold_text_section. */
306 unsigned cold_in_std_section : 1;
307 /* True iff switched sections. */
308 unsigned dw_fde_switched_sections : 1;
309 /* True iff switching from cold to hot section. */
310 unsigned dw_fde_switched_cold_to_hot : 1;
311 }
312 dw_fde_node;
313
314 /* Maximum size (in bytes) of an artificially generated label. */
315 #define MAX_ARTIFICIAL_LABEL_BYTES 30
316
317 /* The size of addresses as they appear in the Dwarf 2 data.
318 Some architectures use word addresses to refer to code locations,
319 but Dwarf 2 info always uses byte addresses. On such machines,
320 Dwarf 2 addresses need to be larger than the architecture's
321 pointers. */
322 #ifndef DWARF2_ADDR_SIZE
323 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
324 #endif
325
326 /* The size in bytes of a DWARF field indicating an offset or length
327 relative to a debug info section, specified to be 4 bytes in the
328 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
329 as PTR_SIZE. */
330
331 #ifndef DWARF_OFFSET_SIZE
332 #define DWARF_OFFSET_SIZE 4
333 #endif
334
335 /* According to the (draft) DWARF 3 specification, the initial length
336 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
337 bytes are 0xffffffff, followed by the length stored in the next 8
338 bytes.
339
340 However, the SGI/MIPS ABI uses an initial length which is equal to
341 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
342
343 #ifndef DWARF_INITIAL_LENGTH_SIZE
344 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
345 #endif
346
347 /* Round SIZE up to the nearest BOUNDARY. */
348 #define DWARF_ROUND(SIZE,BOUNDARY) \
349 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
350
351 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
352 #ifndef DWARF_CIE_DATA_ALIGNMENT
353 #ifdef STACK_GROWS_DOWNWARD
354 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
355 #else
356 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
357 #endif
358 #endif
359
360 /* CIE identifier. */
361 #if HOST_BITS_PER_WIDE_INT >= 64
362 #define DWARF_CIE_ID \
363 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
364 #else
365 #define DWARF_CIE_ID DW_CIE_ID
366 #endif
367
368 /* A pointer to the base of a table that contains frame description
369 information for each routine. */
370 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
371
372 /* Number of elements currently allocated for fde_table. */
373 static GTY(()) unsigned fde_table_allocated;
374
375 /* Number of elements in fde_table currently in use. */
376 static GTY(()) unsigned fde_table_in_use;
377
378 /* Size (in elements) of increments by which we may expand the
379 fde_table. */
380 #define FDE_TABLE_INCREMENT 256
381
382 /* Get the current fde_table entry we should use. */
383
384 static inline dw_fde_ref
385 current_fde (void)
386 {
387 return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
388 }
389
390 /* A list of call frame insns for the CIE. */
391 static GTY(()) dw_cfi_ref cie_cfi_head;
392
393 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
394 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
395 attribute that accelerates the lookup of the FDE associated
396 with the subprogram. This variable holds the table index of the FDE
397 associated with the current function (body) definition. */
398 static unsigned current_funcdef_fde;
399 #endif
400
401 struct GTY(()) indirect_string_node {
402 const char *str;
403 unsigned int refcount;
404 enum dwarf_form form;
405 char *label;
406 };
407
408 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
409
410 static GTY(()) int dw2_string_counter;
411 static GTY(()) unsigned long dwarf2out_cfi_label_num;
412
413 /* True if the compilation unit places functions in more than one section. */
414 static GTY(()) bool have_multiple_function_sections = false;
415
416 /* Whether the default text and cold text sections have been used at all. */
417
418 static GTY(()) bool text_section_used = false;
419 static GTY(()) bool cold_text_section_used = false;
420
421 /* The default cold text section. */
422 static GTY(()) section *cold_text_section;
423
424 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
425
426 /* Forward declarations for functions defined in this file. */
427
428 static char *stripattributes (const char *);
429 static const char *dwarf_cfi_name (unsigned);
430 static dw_cfi_ref new_cfi (void);
431 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
432 static void add_fde_cfi (const char *, dw_cfi_ref);
433 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
434 static void lookup_cfa (dw_cfa_location *);
435 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
436 #ifdef DWARF2_UNWIND_INFO
437 static void initial_return_save (rtx);
438 #endif
439 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
440 HOST_WIDE_INT);
441 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
442 static void output_cfi_directive (dw_cfi_ref);
443 static void output_call_frame_info (int);
444 static void dwarf2out_note_section_used (void);
445 static void dwarf2out_stack_adjust (rtx, bool);
446 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
447 static void flush_queued_reg_saves (void);
448 static bool clobbers_queued_reg_save (const_rtx);
449 static void dwarf2out_frame_debug_expr (rtx, const char *);
450
451 /* Support for complex CFA locations. */
452 static void output_cfa_loc (dw_cfi_ref);
453 static void output_cfa_loc_raw (dw_cfi_ref);
454 static void get_cfa_from_loc_descr (dw_cfa_location *,
455 struct dw_loc_descr_struct *);
456 static struct dw_loc_descr_struct *build_cfa_loc
457 (dw_cfa_location *, HOST_WIDE_INT);
458 static struct dw_loc_descr_struct *build_cfa_aligned_loc
459 (HOST_WIDE_INT, HOST_WIDE_INT);
460 static void def_cfa_1 (const char *, dw_cfa_location *);
461
462 /* How to start an assembler comment. */
463 #ifndef ASM_COMMENT_START
464 #define ASM_COMMENT_START ";#"
465 #endif
466
467 /* Data and reference forms for relocatable data. */
468 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
469 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
470
471 #ifndef DEBUG_FRAME_SECTION
472 #define DEBUG_FRAME_SECTION ".debug_frame"
473 #endif
474
475 #ifndef FUNC_BEGIN_LABEL
476 #define FUNC_BEGIN_LABEL "LFB"
477 #endif
478
479 #ifndef FUNC_END_LABEL
480 #define FUNC_END_LABEL "LFE"
481 #endif
482
483 #ifndef FRAME_BEGIN_LABEL
484 #define FRAME_BEGIN_LABEL "Lframe"
485 #endif
486 #define CIE_AFTER_SIZE_LABEL "LSCIE"
487 #define CIE_END_LABEL "LECIE"
488 #define FDE_LABEL "LSFDE"
489 #define FDE_AFTER_SIZE_LABEL "LASFDE"
490 #define FDE_END_LABEL "LEFDE"
491 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
492 #define LINE_NUMBER_END_LABEL "LELT"
493 #define LN_PROLOG_AS_LABEL "LASLTP"
494 #define LN_PROLOG_END_LABEL "LELTP"
495 #define DIE_LABEL_PREFIX "DW"
496
497 /* The DWARF 2 CFA column which tracks the return address. Normally this
498 is the column for PC, or the first column after all of the hard
499 registers. */
500 #ifndef DWARF_FRAME_RETURN_COLUMN
501 #ifdef PC_REGNUM
502 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
503 #else
504 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
505 #endif
506 #endif
507
508 /* The mapping from gcc register number to DWARF 2 CFA column number. By
509 default, we just provide columns for all registers. */
510 #ifndef DWARF_FRAME_REGNUM
511 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
512 #endif
513 \f
514 /* Hook used by __throw. */
515
516 rtx
517 expand_builtin_dwarf_sp_column (void)
518 {
519 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
520 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
521 }
522
523 /* Return a pointer to a copy of the section string name S with all
524 attributes stripped off, and an asterisk prepended (for assemble_name). */
525
526 static inline char *
527 stripattributes (const char *s)
528 {
529 char *stripped = XNEWVEC (char, strlen (s) + 2);
530 char *p = stripped;
531
532 *p++ = '*';
533
534 while (*s && *s != ',')
535 *p++ = *s++;
536
537 *p = '\0';
538 return stripped;
539 }
540
541 /* MEM is a memory reference for the register size table, each element of
542 which has mode MODE. Initialize column C as a return address column. */
543
544 static void
545 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
546 {
547 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
548 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
549 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
550 }
551
552 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
553
554 static inline HOST_WIDE_INT
555 div_data_align (HOST_WIDE_INT off)
556 {
557 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
558 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
559 return r;
560 }
561
562 /* Return true if we need a signed version of a given opcode
563 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
564
565 static inline bool
566 need_data_align_sf_opcode (HOST_WIDE_INT off)
567 {
568 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
569 }
570
571 /* Generate code to initialize the register size table. */
572
573 void
574 expand_builtin_init_dwarf_reg_sizes (tree address)
575 {
576 unsigned int i;
577 enum machine_mode mode = TYPE_MODE (char_type_node);
578 rtx addr = expand_normal (address);
579 rtx mem = gen_rtx_MEM (BLKmode, addr);
580 bool wrote_return_column = false;
581
582 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
583 {
584 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
585
586 if (rnum < DWARF_FRAME_REGISTERS)
587 {
588 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
589 enum machine_mode save_mode = reg_raw_mode[i];
590 HOST_WIDE_INT size;
591
592 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
593 save_mode = choose_hard_reg_mode (i, 1, true);
594 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
595 {
596 if (save_mode == VOIDmode)
597 continue;
598 wrote_return_column = true;
599 }
600 size = GET_MODE_SIZE (save_mode);
601 if (offset < 0)
602 continue;
603
604 emit_move_insn (adjust_address (mem, mode, offset),
605 gen_int_mode (size, mode));
606 }
607 }
608
609 if (!wrote_return_column)
610 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
611
612 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
613 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
614 #endif
615
616 targetm.init_dwarf_reg_sizes_extra (address);
617 }
618
619 /* Convert a DWARF call frame info. operation to its string name */
620
621 static const char *
622 dwarf_cfi_name (unsigned int cfi_opc)
623 {
624 switch (cfi_opc)
625 {
626 case DW_CFA_advance_loc:
627 return "DW_CFA_advance_loc";
628 case DW_CFA_offset:
629 return "DW_CFA_offset";
630 case DW_CFA_restore:
631 return "DW_CFA_restore";
632 case DW_CFA_nop:
633 return "DW_CFA_nop";
634 case DW_CFA_set_loc:
635 return "DW_CFA_set_loc";
636 case DW_CFA_advance_loc1:
637 return "DW_CFA_advance_loc1";
638 case DW_CFA_advance_loc2:
639 return "DW_CFA_advance_loc2";
640 case DW_CFA_advance_loc4:
641 return "DW_CFA_advance_loc4";
642 case DW_CFA_offset_extended:
643 return "DW_CFA_offset_extended";
644 case DW_CFA_restore_extended:
645 return "DW_CFA_restore_extended";
646 case DW_CFA_undefined:
647 return "DW_CFA_undefined";
648 case DW_CFA_same_value:
649 return "DW_CFA_same_value";
650 case DW_CFA_register:
651 return "DW_CFA_register";
652 case DW_CFA_remember_state:
653 return "DW_CFA_remember_state";
654 case DW_CFA_restore_state:
655 return "DW_CFA_restore_state";
656 case DW_CFA_def_cfa:
657 return "DW_CFA_def_cfa";
658 case DW_CFA_def_cfa_register:
659 return "DW_CFA_def_cfa_register";
660 case DW_CFA_def_cfa_offset:
661 return "DW_CFA_def_cfa_offset";
662
663 /* DWARF 3 */
664 case DW_CFA_def_cfa_expression:
665 return "DW_CFA_def_cfa_expression";
666 case DW_CFA_expression:
667 return "DW_CFA_expression";
668 case DW_CFA_offset_extended_sf:
669 return "DW_CFA_offset_extended_sf";
670 case DW_CFA_def_cfa_sf:
671 return "DW_CFA_def_cfa_sf";
672 case DW_CFA_def_cfa_offset_sf:
673 return "DW_CFA_def_cfa_offset_sf";
674
675 /* SGI/MIPS specific */
676 case DW_CFA_MIPS_advance_loc8:
677 return "DW_CFA_MIPS_advance_loc8";
678
679 /* GNU extensions */
680 case DW_CFA_GNU_window_save:
681 return "DW_CFA_GNU_window_save";
682 case DW_CFA_GNU_args_size:
683 return "DW_CFA_GNU_args_size";
684 case DW_CFA_GNU_negative_offset_extended:
685 return "DW_CFA_GNU_negative_offset_extended";
686
687 default:
688 return "DW_CFA_<unknown>";
689 }
690 }
691
692 /* Return a pointer to a newly allocated Call Frame Instruction. */
693
694 static inline dw_cfi_ref
695 new_cfi (void)
696 {
697 dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
698
699 cfi->dw_cfi_next = NULL;
700 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
701 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
702
703 return cfi;
704 }
705
706 /* Add a Call Frame Instruction to list of instructions. */
707
708 static inline void
709 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
710 {
711 dw_cfi_ref *p;
712 dw_fde_ref fde = current_fde ();
713
714 /* When DRAP is used, CFA is defined with an expression. Redefine
715 CFA may lead to a different CFA value. */
716 /* ??? Of course, this heuristic fails when we're annotating epilogues,
717 because of course we'll always want to redefine the CFA back to the
718 stack pointer on the way out. Where should we move this check? */
719 if (0 && fde && fde->drap_reg != INVALID_REGNUM)
720 switch (cfi->dw_cfi_opc)
721 {
722 case DW_CFA_def_cfa_register:
723 case DW_CFA_def_cfa_offset:
724 case DW_CFA_def_cfa_offset_sf:
725 case DW_CFA_def_cfa:
726 case DW_CFA_def_cfa_sf:
727 gcc_unreachable ();
728
729 default:
730 break;
731 }
732
733 /* Find the end of the chain. */
734 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
735 ;
736
737 *p = cfi;
738 }
739
740 /* Generate a new label for the CFI info to refer to. FORCE is true
741 if a label needs to be output even when using .cfi_* directives. */
742
743 char *
744 dwarf2out_cfi_label (bool force)
745 {
746 static char label[20];
747
748 if (!force && dwarf2out_do_cfi_asm ())
749 {
750 /* In this case, we will be emitting the asm directive instead of
751 the label, so just return a placeholder to keep the rest of the
752 interfaces happy. */
753 strcpy (label, "<do not output>");
754 }
755 else
756 {
757 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
758 ASM_OUTPUT_LABEL (asm_out_file, label);
759 }
760
761 return label;
762 }
763
764 /* True if remember_state should be emitted before following CFI directive. */
765 static bool emit_cfa_remember;
766
767 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
768 or to the CIE if LABEL is NULL. */
769
770 static void
771 add_fde_cfi (const char *label, dw_cfi_ref cfi)
772 {
773 dw_cfi_ref *list_head;
774
775 if (emit_cfa_remember)
776 {
777 dw_cfi_ref cfi_remember;
778
779 /* Emit the state save. */
780 emit_cfa_remember = false;
781 cfi_remember = new_cfi ();
782 cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
783 add_fde_cfi (label, cfi_remember);
784 }
785
786 list_head = &cie_cfi_head;
787
788 if (dwarf2out_do_cfi_asm ())
789 {
790 if (label)
791 {
792 dw_fde_ref fde = current_fde ();
793
794 gcc_assert (fde != NULL);
795
796 /* We still have to add the cfi to the list so that lookup_cfa
797 works later on. When -g2 and above we even need to force
798 emitting of CFI labels and add to list a DW_CFA_set_loc for
799 convert_cfa_to_fb_loc_list purposes. If we're generating
800 DWARF3 output we use DW_OP_call_frame_cfa and so don't use
801 convert_cfa_to_fb_loc_list. */
802 if (dwarf_version == 2
803 && debug_info_level > DINFO_LEVEL_TERSE
804 && (write_symbols == DWARF2_DEBUG
805 || write_symbols == VMS_AND_DWARF2_DEBUG))
806 {
807 switch (cfi->dw_cfi_opc)
808 {
809 case DW_CFA_def_cfa_offset:
810 case DW_CFA_def_cfa_offset_sf:
811 case DW_CFA_def_cfa_register:
812 case DW_CFA_def_cfa:
813 case DW_CFA_def_cfa_sf:
814 case DW_CFA_def_cfa_expression:
815 case DW_CFA_restore_state:
816 if (*label == 0 || strcmp (label, "<do not output>") == 0)
817 label = dwarf2out_cfi_label (true);
818
819 if (fde->dw_fde_current_label == NULL
820 || strcmp (label, fde->dw_fde_current_label) != 0)
821 {
822 dw_cfi_ref xcfi;
823
824 label = xstrdup (label);
825
826 /* Set the location counter to the new label. */
827 xcfi = new_cfi ();
828 /* It doesn't metter whether DW_CFA_set_loc
829 or DW_CFA_advance_loc4 is added here, those aren't
830 emitted into assembly, only looked up by
831 convert_cfa_to_fb_loc_list. */
832 xcfi->dw_cfi_opc = DW_CFA_set_loc;
833 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
834 add_cfi (&fde->dw_fde_cfi, xcfi);
835 fde->dw_fde_current_label = label;
836 }
837 break;
838 default:
839 break;
840 }
841 }
842
843 output_cfi_directive (cfi);
844
845 list_head = &fde->dw_fde_cfi;
846 }
847 /* ??? If this is a CFI for the CIE, we don't emit. This
848 assumes that the standard CIE contents that the assembler
849 uses matches the standard CIE contents that the compiler
850 uses. This is probably a bad assumption. I'm not quite
851 sure how to address this for now. */
852 }
853 else if (label)
854 {
855 dw_fde_ref fde = current_fde ();
856
857 gcc_assert (fde != NULL);
858
859 if (*label == 0)
860 label = dwarf2out_cfi_label (false);
861
862 if (fde->dw_fde_current_label == NULL
863 || strcmp (label, fde->dw_fde_current_label) != 0)
864 {
865 dw_cfi_ref xcfi;
866
867 label = xstrdup (label);
868
869 /* Set the location counter to the new label. */
870 xcfi = new_cfi ();
871 /* If we have a current label, advance from there, otherwise
872 set the location directly using set_loc. */
873 xcfi->dw_cfi_opc = fde->dw_fde_current_label
874 ? DW_CFA_advance_loc4
875 : DW_CFA_set_loc;
876 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
877 add_cfi (&fde->dw_fde_cfi, xcfi);
878
879 fde->dw_fde_current_label = label;
880 }
881
882 list_head = &fde->dw_fde_cfi;
883 }
884
885 add_cfi (list_head, cfi);
886 }
887
888 /* Subroutine of lookup_cfa. */
889
890 static void
891 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
892 {
893 switch (cfi->dw_cfi_opc)
894 {
895 case DW_CFA_def_cfa_offset:
896 case DW_CFA_def_cfa_offset_sf:
897 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
898 break;
899 case DW_CFA_def_cfa_register:
900 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
901 break;
902 case DW_CFA_def_cfa:
903 case DW_CFA_def_cfa_sf:
904 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
905 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
906 break;
907 case DW_CFA_def_cfa_expression:
908 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
909 break;
910
911 case DW_CFA_remember_state:
912 gcc_assert (!remember->in_use);
913 *remember = *loc;
914 remember->in_use = 1;
915 break;
916 case DW_CFA_restore_state:
917 gcc_assert (remember->in_use);
918 *loc = *remember;
919 remember->in_use = 0;
920 break;
921
922 default:
923 break;
924 }
925 }
926
927 /* Find the previous value for the CFA. */
928
929 static void
930 lookup_cfa (dw_cfa_location *loc)
931 {
932 dw_cfi_ref cfi;
933 dw_fde_ref fde;
934 dw_cfa_location remember;
935
936 memset (loc, 0, sizeof (*loc));
937 loc->reg = INVALID_REGNUM;
938 remember = *loc;
939
940 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
941 lookup_cfa_1 (cfi, loc, &remember);
942
943 fde = current_fde ();
944 if (fde)
945 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
946 lookup_cfa_1 (cfi, loc, &remember);
947 }
948
949 /* The current rule for calculating the DWARF2 canonical frame address. */
950 static dw_cfa_location cfa;
951
952 /* The register used for saving registers to the stack, and its offset
953 from the CFA. */
954 static dw_cfa_location cfa_store;
955
956 /* The current save location around an epilogue. */
957 static dw_cfa_location cfa_remember;
958
959 /* The running total of the size of arguments pushed onto the stack. */
960 static HOST_WIDE_INT args_size;
961
962 /* The last args_size we actually output. */
963 static HOST_WIDE_INT old_args_size;
964
965 /* Entry point to update the canonical frame address (CFA).
966 LABEL is passed to add_fde_cfi. The value of CFA is now to be
967 calculated from REG+OFFSET. */
968
969 void
970 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
971 {
972 dw_cfa_location loc;
973 loc.indirect = 0;
974 loc.base_offset = 0;
975 loc.reg = reg;
976 loc.offset = offset;
977 def_cfa_1 (label, &loc);
978 }
979
980 /* Determine if two dw_cfa_location structures define the same data. */
981
982 static bool
983 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
984 {
985 return (loc1->reg == loc2->reg
986 && loc1->offset == loc2->offset
987 && loc1->indirect == loc2->indirect
988 && (loc1->indirect == 0
989 || loc1->base_offset == loc2->base_offset));
990 }
991
992 /* This routine does the actual work. The CFA is now calculated from
993 the dw_cfa_location structure. */
994
995 static void
996 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
997 {
998 dw_cfi_ref cfi;
999 dw_cfa_location old_cfa, loc;
1000
1001 cfa = *loc_p;
1002 loc = *loc_p;
1003
1004 if (cfa_store.reg == loc.reg && loc.indirect == 0)
1005 cfa_store.offset = loc.offset;
1006
1007 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1008 lookup_cfa (&old_cfa);
1009
1010 /* If nothing changed, no need to issue any call frame instructions. */
1011 if (cfa_equal_p (&loc, &old_cfa))
1012 return;
1013
1014 cfi = new_cfi ();
1015
1016 if (loc.reg == old_cfa.reg && !loc.indirect)
1017 {
1018 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1019 the CFA register did not change but the offset did. The data
1020 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1021 in the assembler via the .cfi_def_cfa_offset directive. */
1022 if (loc.offset < 0)
1023 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1024 else
1025 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1026 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1027 }
1028
1029 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
1030 else if (loc.offset == old_cfa.offset
1031 && old_cfa.reg != INVALID_REGNUM
1032 && !loc.indirect)
1033 {
1034 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1035 indicating the CFA register has changed to <register> but the
1036 offset has not changed. */
1037 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1038 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1039 }
1040 #endif
1041
1042 else if (loc.indirect == 0)
1043 {
1044 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1045 indicating the CFA register has changed to <register> with
1046 the specified offset. The data factoring for DW_CFA_def_cfa_sf
1047 happens in output_cfi, or in the assembler via the .cfi_def_cfa
1048 directive. */
1049 if (loc.offset < 0)
1050 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1051 else
1052 cfi->dw_cfi_opc = DW_CFA_def_cfa;
1053 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1054 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1055 }
1056 else
1057 {
1058 /* Construct a DW_CFA_def_cfa_expression instruction to
1059 calculate the CFA using a full location expression since no
1060 register-offset pair is available. */
1061 struct dw_loc_descr_struct *loc_list;
1062
1063 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1064 loc_list = build_cfa_loc (&loc, 0);
1065 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1066 }
1067
1068 add_fde_cfi (label, cfi);
1069 }
1070
1071 /* Add the CFI for saving a register. REG is the CFA column number.
1072 LABEL is passed to add_fde_cfi.
1073 If SREG is -1, the register is saved at OFFSET from the CFA;
1074 otherwise it is saved in SREG. */
1075
1076 static void
1077 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1078 {
1079 dw_cfi_ref cfi = new_cfi ();
1080 dw_fde_ref fde = current_fde ();
1081
1082 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1083
1084 /* When stack is aligned, store REG using DW_CFA_expression with
1085 FP. */
1086 if (fde
1087 && fde->stack_realign
1088 && sreg == INVALID_REGNUM)
1089 {
1090 cfi->dw_cfi_opc = DW_CFA_expression;
1091 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1092 cfi->dw_cfi_oprnd1.dw_cfi_loc
1093 = build_cfa_aligned_loc (offset, fde->stack_realignment);
1094 }
1095 else if (sreg == INVALID_REGNUM)
1096 {
1097 if (need_data_align_sf_opcode (offset))
1098 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1099 else if (reg & ~0x3f)
1100 cfi->dw_cfi_opc = DW_CFA_offset_extended;
1101 else
1102 cfi->dw_cfi_opc = DW_CFA_offset;
1103 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1104 }
1105 else if (sreg == reg)
1106 cfi->dw_cfi_opc = DW_CFA_same_value;
1107 else
1108 {
1109 cfi->dw_cfi_opc = DW_CFA_register;
1110 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1111 }
1112
1113 add_fde_cfi (label, cfi);
1114 }
1115
1116 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1117 This CFI tells the unwinder that it needs to restore the window registers
1118 from the previous frame's window save area.
1119
1120 ??? Perhaps we should note in the CIE where windows are saved (instead of
1121 assuming 0(cfa)) and what registers are in the window. */
1122
1123 void
1124 dwarf2out_window_save (const char *label)
1125 {
1126 dw_cfi_ref cfi = new_cfi ();
1127
1128 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1129 add_fde_cfi (label, cfi);
1130 }
1131
1132 /* Add a CFI to update the running total of the size of arguments
1133 pushed onto the stack. */
1134
1135 void
1136 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1137 {
1138 dw_cfi_ref cfi;
1139
1140 if (size == old_args_size)
1141 return;
1142
1143 old_args_size = size;
1144
1145 cfi = new_cfi ();
1146 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1147 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1148 add_fde_cfi (label, cfi);
1149 }
1150
1151 /* Entry point for saving a register to the stack. REG is the GCC register
1152 number. LABEL and OFFSET are passed to reg_save. */
1153
1154 void
1155 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1156 {
1157 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1158 }
1159
1160 /* Entry point for saving the return address in the stack.
1161 LABEL and OFFSET are passed to reg_save. */
1162
1163 void
1164 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1165 {
1166 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1167 }
1168
1169 /* Entry point for saving the return address in a register.
1170 LABEL and SREG are passed to reg_save. */
1171
1172 void
1173 dwarf2out_return_reg (const char *label, unsigned int sreg)
1174 {
1175 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1176 }
1177
1178 #ifdef DWARF2_UNWIND_INFO
1179 /* Record the initial position of the return address. RTL is
1180 INCOMING_RETURN_ADDR_RTX. */
1181
1182 static void
1183 initial_return_save (rtx rtl)
1184 {
1185 unsigned int reg = INVALID_REGNUM;
1186 HOST_WIDE_INT offset = 0;
1187
1188 switch (GET_CODE (rtl))
1189 {
1190 case REG:
1191 /* RA is in a register. */
1192 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1193 break;
1194
1195 case MEM:
1196 /* RA is on the stack. */
1197 rtl = XEXP (rtl, 0);
1198 switch (GET_CODE (rtl))
1199 {
1200 case REG:
1201 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1202 offset = 0;
1203 break;
1204
1205 case PLUS:
1206 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1207 offset = INTVAL (XEXP (rtl, 1));
1208 break;
1209
1210 case MINUS:
1211 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1212 offset = -INTVAL (XEXP (rtl, 1));
1213 break;
1214
1215 default:
1216 gcc_unreachable ();
1217 }
1218
1219 break;
1220
1221 case PLUS:
1222 /* The return address is at some offset from any value we can
1223 actually load. For instance, on the SPARC it is in %i7+8. Just
1224 ignore the offset for now; it doesn't matter for unwinding frames. */
1225 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1226 initial_return_save (XEXP (rtl, 0));
1227 return;
1228
1229 default:
1230 gcc_unreachable ();
1231 }
1232
1233 if (reg != DWARF_FRAME_RETURN_COLUMN)
1234 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1235 }
1236 #endif
1237
1238 /* Given a SET, calculate the amount of stack adjustment it
1239 contains. */
1240
1241 static HOST_WIDE_INT
1242 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1243 HOST_WIDE_INT cur_offset)
1244 {
1245 const_rtx src = SET_SRC (pattern);
1246 const_rtx dest = SET_DEST (pattern);
1247 HOST_WIDE_INT offset = 0;
1248 enum rtx_code code;
1249
1250 if (dest == stack_pointer_rtx)
1251 {
1252 code = GET_CODE (src);
1253
1254 /* Assume (set (reg sp) (reg whatever)) sets args_size
1255 level to 0. */
1256 if (code == REG && src != stack_pointer_rtx)
1257 {
1258 offset = -cur_args_size;
1259 #ifndef STACK_GROWS_DOWNWARD
1260 offset = -offset;
1261 #endif
1262 return offset - cur_offset;
1263 }
1264
1265 if (! (code == PLUS || code == MINUS)
1266 || XEXP (src, 0) != stack_pointer_rtx
1267 || !CONST_INT_P (XEXP (src, 1)))
1268 return 0;
1269
1270 /* (set (reg sp) (plus (reg sp) (const_int))) */
1271 offset = INTVAL (XEXP (src, 1));
1272 if (code == PLUS)
1273 offset = -offset;
1274 return offset;
1275 }
1276
1277 if (MEM_P (src) && !MEM_P (dest))
1278 dest = src;
1279 if (MEM_P (dest))
1280 {
1281 /* (set (mem (pre_dec (reg sp))) (foo)) */
1282 src = XEXP (dest, 0);
1283 code = GET_CODE (src);
1284
1285 switch (code)
1286 {
1287 case PRE_MODIFY:
1288 case POST_MODIFY:
1289 if (XEXP (src, 0) == stack_pointer_rtx)
1290 {
1291 rtx val = XEXP (XEXP (src, 1), 1);
1292 /* We handle only adjustments by constant amount. */
1293 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1294 && CONST_INT_P (val));
1295 offset = -INTVAL (val);
1296 break;
1297 }
1298 return 0;
1299
1300 case PRE_DEC:
1301 case POST_DEC:
1302 if (XEXP (src, 0) == stack_pointer_rtx)
1303 {
1304 offset = GET_MODE_SIZE (GET_MODE (dest));
1305 break;
1306 }
1307 return 0;
1308
1309 case PRE_INC:
1310 case POST_INC:
1311 if (XEXP (src, 0) == stack_pointer_rtx)
1312 {
1313 offset = -GET_MODE_SIZE (GET_MODE (dest));
1314 break;
1315 }
1316 return 0;
1317
1318 default:
1319 return 0;
1320 }
1321 }
1322 else
1323 return 0;
1324
1325 return offset;
1326 }
1327
1328 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1329 indexed by INSN_UID. */
1330
1331 static HOST_WIDE_INT *barrier_args_size;
1332
1333 /* Helper function for compute_barrier_args_size. Handle one insn. */
1334
1335 static HOST_WIDE_INT
1336 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1337 VEC (rtx, heap) **next)
1338 {
1339 HOST_WIDE_INT offset = 0;
1340 int i;
1341
1342 if (! RTX_FRAME_RELATED_P (insn))
1343 {
1344 if (prologue_epilogue_contains (insn))
1345 /* Nothing */;
1346 else if (GET_CODE (PATTERN (insn)) == SET)
1347 offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1348 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1349 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1350 {
1351 /* There may be stack adjustments inside compound insns. Search
1352 for them. */
1353 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1354 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1355 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1356 cur_args_size, offset);
1357 }
1358 }
1359 else
1360 {
1361 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1362
1363 if (expr)
1364 {
1365 expr = XEXP (expr, 0);
1366 if (GET_CODE (expr) == PARALLEL
1367 || GET_CODE (expr) == SEQUENCE)
1368 for (i = 1; i < XVECLEN (expr, 0); i++)
1369 {
1370 rtx elem = XVECEXP (expr, 0, i);
1371
1372 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1373 offset += stack_adjust_offset (elem, cur_args_size, offset);
1374 }
1375 }
1376 }
1377
1378 #ifndef STACK_GROWS_DOWNWARD
1379 offset = -offset;
1380 #endif
1381
1382 cur_args_size += offset;
1383 if (cur_args_size < 0)
1384 cur_args_size = 0;
1385
1386 if (JUMP_P (insn))
1387 {
1388 rtx dest = JUMP_LABEL (insn);
1389
1390 if (dest)
1391 {
1392 if (barrier_args_size [INSN_UID (dest)] < 0)
1393 {
1394 barrier_args_size [INSN_UID (dest)] = cur_args_size;
1395 VEC_safe_push (rtx, heap, *next, dest);
1396 }
1397 }
1398 }
1399
1400 return cur_args_size;
1401 }
1402
1403 /* Walk the whole function and compute args_size on BARRIERs. */
1404
1405 static void
1406 compute_barrier_args_size (void)
1407 {
1408 int max_uid = get_max_uid (), i;
1409 rtx insn;
1410 VEC (rtx, heap) *worklist, *next, *tmp;
1411
1412 barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1413 for (i = 0; i < max_uid; i++)
1414 barrier_args_size[i] = -1;
1415
1416 worklist = VEC_alloc (rtx, heap, 20);
1417 next = VEC_alloc (rtx, heap, 20);
1418 insn = get_insns ();
1419 barrier_args_size[INSN_UID (insn)] = 0;
1420 VEC_quick_push (rtx, worklist, insn);
1421 for (;;)
1422 {
1423 while (!VEC_empty (rtx, worklist))
1424 {
1425 rtx prev, body, first_insn;
1426 HOST_WIDE_INT cur_args_size;
1427
1428 first_insn = insn = VEC_pop (rtx, worklist);
1429 cur_args_size = barrier_args_size[INSN_UID (insn)];
1430 prev = prev_nonnote_insn (insn);
1431 if (prev && BARRIER_P (prev))
1432 barrier_args_size[INSN_UID (prev)] = cur_args_size;
1433
1434 for (; insn; insn = NEXT_INSN (insn))
1435 {
1436 if (INSN_DELETED_P (insn) || NOTE_P (insn))
1437 continue;
1438 if (BARRIER_P (insn))
1439 break;
1440
1441 if (LABEL_P (insn))
1442 {
1443 if (insn == first_insn)
1444 continue;
1445 else if (barrier_args_size[INSN_UID (insn)] < 0)
1446 {
1447 barrier_args_size[INSN_UID (insn)] = cur_args_size;
1448 continue;
1449 }
1450 else
1451 {
1452 /* The insns starting with this label have been
1453 already scanned or are in the worklist. */
1454 break;
1455 }
1456 }
1457
1458 body = PATTERN (insn);
1459 if (GET_CODE (body) == SEQUENCE)
1460 {
1461 HOST_WIDE_INT dest_args_size = cur_args_size;
1462 for (i = 1; i < XVECLEN (body, 0); i++)
1463 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1464 && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1465 dest_args_size
1466 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1467 dest_args_size, &next);
1468 else
1469 cur_args_size
1470 = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1471 cur_args_size, &next);
1472
1473 if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1474 compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1475 dest_args_size, &next);
1476 else
1477 cur_args_size
1478 = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1479 cur_args_size, &next);
1480 }
1481 else
1482 cur_args_size
1483 = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1484 }
1485 }
1486
1487 if (VEC_empty (rtx, next))
1488 break;
1489
1490 /* Swap WORKLIST with NEXT and truncate NEXT for next iteration. */
1491 tmp = next;
1492 next = worklist;
1493 worklist = tmp;
1494 VEC_truncate (rtx, next, 0);
1495 }
1496
1497 VEC_free (rtx, heap, worklist);
1498 VEC_free (rtx, heap, next);
1499 }
1500
1501
1502 /* Check INSN to see if it looks like a push or a stack adjustment, and
1503 make a note of it if it does. EH uses this information to find out how
1504 much extra space it needs to pop off the stack. */
1505
1506 static void
1507 dwarf2out_stack_adjust (rtx insn, bool after_p)
1508 {
1509 HOST_WIDE_INT offset;
1510 const char *label;
1511 int i;
1512
1513 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1514 with this function. Proper support would require all frame-related
1515 insns to be marked, and to be able to handle saving state around
1516 epilogues textually in the middle of the function. */
1517 if (prologue_epilogue_contains (insn))
1518 return;
1519
1520 /* If INSN is an instruction from target of an annulled branch, the
1521 effects are for the target only and so current argument size
1522 shouldn't change at all. */
1523 if (final_sequence
1524 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1525 && INSN_FROM_TARGET_P (insn))
1526 return;
1527
1528 /* If only calls can throw, and we have a frame pointer,
1529 save up adjustments until we see the CALL_INSN. */
1530 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1531 {
1532 if (CALL_P (insn) && !after_p)
1533 {
1534 /* Extract the size of the args from the CALL rtx itself. */
1535 insn = PATTERN (insn);
1536 if (GET_CODE (insn) == PARALLEL)
1537 insn = XVECEXP (insn, 0, 0);
1538 if (GET_CODE (insn) == SET)
1539 insn = SET_SRC (insn);
1540 gcc_assert (GET_CODE (insn) == CALL);
1541 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1542 }
1543 return;
1544 }
1545
1546 if (CALL_P (insn) && !after_p)
1547 {
1548 if (!flag_asynchronous_unwind_tables)
1549 dwarf2out_args_size ("", args_size);
1550 return;
1551 }
1552 else if (BARRIER_P (insn))
1553 {
1554 /* Don't call compute_barrier_args_size () if the only
1555 BARRIER is at the end of function. */
1556 if (barrier_args_size == NULL && next_nonnote_insn (insn))
1557 compute_barrier_args_size ();
1558 if (barrier_args_size == NULL)
1559 offset = 0;
1560 else
1561 {
1562 offset = barrier_args_size[INSN_UID (insn)];
1563 if (offset < 0)
1564 offset = 0;
1565 }
1566
1567 offset -= args_size;
1568 #ifndef STACK_GROWS_DOWNWARD
1569 offset = -offset;
1570 #endif
1571 }
1572 else if (GET_CODE (PATTERN (insn)) == SET)
1573 offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1574 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1575 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1576 {
1577 /* There may be stack adjustments inside compound insns. Search
1578 for them. */
1579 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1580 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1581 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1582 args_size, offset);
1583 }
1584 else
1585 return;
1586
1587 if (offset == 0)
1588 return;
1589
1590 label = dwarf2out_cfi_label (false);
1591 dwarf2out_args_size_adjust (offset, label);
1592 }
1593
1594 /* Adjust args_size based on stack adjustment OFFSET. */
1595
1596 static void
1597 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1598 {
1599 if (cfa.reg == STACK_POINTER_REGNUM)
1600 cfa.offset += offset;
1601
1602 if (cfa_store.reg == STACK_POINTER_REGNUM)
1603 cfa_store.offset += offset;
1604
1605 #ifndef STACK_GROWS_DOWNWARD
1606 offset = -offset;
1607 #endif
1608
1609 args_size += offset;
1610 if (args_size < 0)
1611 args_size = 0;
1612
1613 def_cfa_1 (label, &cfa);
1614 if (flag_asynchronous_unwind_tables)
1615 dwarf2out_args_size (label, args_size);
1616 }
1617
1618 #endif
1619
1620 /* We delay emitting a register save until either (a) we reach the end
1621 of the prologue or (b) the register is clobbered. This clusters
1622 register saves so that there are fewer pc advances. */
1623
1624 struct GTY(()) queued_reg_save {
1625 struct queued_reg_save *next;
1626 rtx reg;
1627 HOST_WIDE_INT cfa_offset;
1628 rtx saved_reg;
1629 };
1630
1631 static GTY(()) struct queued_reg_save *queued_reg_saves;
1632
1633 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1634 struct GTY(()) reg_saved_in_data {
1635 rtx orig_reg;
1636 rtx saved_in_reg;
1637 };
1638
1639 /* A list of registers saved in other registers.
1640 The list intentionally has a small maximum capacity of 4; if your
1641 port needs more than that, you might consider implementing a
1642 more efficient data structure. */
1643 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1644 static GTY(()) size_t num_regs_saved_in_regs;
1645
1646 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1647 static const char *last_reg_save_label;
1648
1649 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1650 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1651
1652 static void
1653 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1654 {
1655 struct queued_reg_save *q;
1656
1657 /* Duplicates waste space, but it's also necessary to remove them
1658 for correctness, since the queue gets output in reverse
1659 order. */
1660 for (q = queued_reg_saves; q != NULL; q = q->next)
1661 if (REGNO (q->reg) == REGNO (reg))
1662 break;
1663
1664 if (q == NULL)
1665 {
1666 q = GGC_NEW (struct queued_reg_save);
1667 q->next = queued_reg_saves;
1668 queued_reg_saves = q;
1669 }
1670
1671 q->reg = reg;
1672 q->cfa_offset = offset;
1673 q->saved_reg = sreg;
1674
1675 last_reg_save_label = label;
1676 }
1677
1678 /* Output all the entries in QUEUED_REG_SAVES. */
1679
1680 static void
1681 flush_queued_reg_saves (void)
1682 {
1683 struct queued_reg_save *q;
1684
1685 for (q = queued_reg_saves; q; q = q->next)
1686 {
1687 size_t i;
1688 unsigned int reg, sreg;
1689
1690 for (i = 0; i < num_regs_saved_in_regs; i++)
1691 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1692 break;
1693 if (q->saved_reg && i == num_regs_saved_in_regs)
1694 {
1695 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1696 num_regs_saved_in_regs++;
1697 }
1698 if (i != num_regs_saved_in_regs)
1699 {
1700 regs_saved_in_regs[i].orig_reg = q->reg;
1701 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1702 }
1703
1704 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1705 if (q->saved_reg)
1706 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1707 else
1708 sreg = INVALID_REGNUM;
1709 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1710 }
1711
1712 queued_reg_saves = NULL;
1713 last_reg_save_label = NULL;
1714 }
1715
1716 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1717 location for? Or, does it clobber a register which we've previously
1718 said that some other register is saved in, and for which we now
1719 have a new location for? */
1720
1721 static bool
1722 clobbers_queued_reg_save (const_rtx insn)
1723 {
1724 struct queued_reg_save *q;
1725
1726 for (q = queued_reg_saves; q; q = q->next)
1727 {
1728 size_t i;
1729 if (modified_in_p (q->reg, insn))
1730 return true;
1731 for (i = 0; i < num_regs_saved_in_regs; i++)
1732 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1733 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1734 return true;
1735 }
1736
1737 return false;
1738 }
1739
1740 /* Entry point for saving the first register into the second. */
1741
1742 void
1743 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1744 {
1745 size_t i;
1746 unsigned int regno, sregno;
1747
1748 for (i = 0; i < num_regs_saved_in_regs; i++)
1749 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1750 break;
1751 if (i == num_regs_saved_in_regs)
1752 {
1753 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1754 num_regs_saved_in_regs++;
1755 }
1756 regs_saved_in_regs[i].orig_reg = reg;
1757 regs_saved_in_regs[i].saved_in_reg = sreg;
1758
1759 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1760 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1761 reg_save (label, regno, sregno, 0);
1762 }
1763
1764 /* What register, if any, is currently saved in REG? */
1765
1766 static rtx
1767 reg_saved_in (rtx reg)
1768 {
1769 unsigned int regn = REGNO (reg);
1770 size_t i;
1771 struct queued_reg_save *q;
1772
1773 for (q = queued_reg_saves; q; q = q->next)
1774 if (q->saved_reg && regn == REGNO (q->saved_reg))
1775 return q->reg;
1776
1777 for (i = 0; i < num_regs_saved_in_regs; i++)
1778 if (regs_saved_in_regs[i].saved_in_reg
1779 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1780 return regs_saved_in_regs[i].orig_reg;
1781
1782 return NULL_RTX;
1783 }
1784
1785
1786 /* A temporary register holding an integral value used in adjusting SP
1787 or setting up the store_reg. The "offset" field holds the integer
1788 value, not an offset. */
1789 static dw_cfa_location cfa_temp;
1790
1791 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1792
1793 static void
1794 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1795 {
1796 memset (&cfa, 0, sizeof (cfa));
1797
1798 switch (GET_CODE (pat))
1799 {
1800 case PLUS:
1801 cfa.reg = REGNO (XEXP (pat, 0));
1802 cfa.offset = INTVAL (XEXP (pat, 1));
1803 break;
1804
1805 case REG:
1806 cfa.reg = REGNO (pat);
1807 break;
1808
1809 default:
1810 /* Recurse and define an expression. */
1811 gcc_unreachable ();
1812 }
1813
1814 def_cfa_1 (label, &cfa);
1815 }
1816
1817 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1818
1819 static void
1820 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1821 {
1822 rtx src, dest;
1823
1824 gcc_assert (GET_CODE (pat) == SET);
1825 dest = XEXP (pat, 0);
1826 src = XEXP (pat, 1);
1827
1828 switch (GET_CODE (src))
1829 {
1830 case PLUS:
1831 gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1832 cfa.offset -= INTVAL (XEXP (src, 1));
1833 break;
1834
1835 case REG:
1836 break;
1837
1838 default:
1839 gcc_unreachable ();
1840 }
1841
1842 cfa.reg = REGNO (dest);
1843 gcc_assert (cfa.indirect == 0);
1844
1845 def_cfa_1 (label, &cfa);
1846 }
1847
1848 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1849
1850 static void
1851 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1852 {
1853 HOST_WIDE_INT offset;
1854 rtx src, addr, span;
1855
1856 src = XEXP (set, 1);
1857 addr = XEXP (set, 0);
1858 gcc_assert (MEM_P (addr));
1859 addr = XEXP (addr, 0);
1860
1861 /* As documented, only consider extremely simple addresses. */
1862 switch (GET_CODE (addr))
1863 {
1864 case REG:
1865 gcc_assert (REGNO (addr) == cfa.reg);
1866 offset = -cfa.offset;
1867 break;
1868 case PLUS:
1869 gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1870 offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1871 break;
1872 default:
1873 gcc_unreachable ();
1874 }
1875
1876 span = targetm.dwarf_register_span (src);
1877
1878 /* ??? We'd like to use queue_reg_save, but we need to come up with
1879 a different flushing heuristic for epilogues. */
1880 if (!span)
1881 reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1882 else
1883 {
1884 /* We have a PARALLEL describing where the contents of SRC live.
1885 Queue register saves for each piece of the PARALLEL. */
1886 int par_index;
1887 int limit;
1888 HOST_WIDE_INT span_offset = offset;
1889
1890 gcc_assert (GET_CODE (span) == PARALLEL);
1891
1892 limit = XVECLEN (span, 0);
1893 for (par_index = 0; par_index < limit; par_index++)
1894 {
1895 rtx elem = XVECEXP (span, 0, par_index);
1896
1897 reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1898 INVALID_REGNUM, span_offset);
1899 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1900 }
1901 }
1902 }
1903
1904 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
1905
1906 static void
1907 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1908 {
1909 rtx src, dest;
1910 unsigned sregno, dregno;
1911
1912 src = XEXP (set, 1);
1913 dest = XEXP (set, 0);
1914
1915 if (src == pc_rtx)
1916 sregno = DWARF_FRAME_RETURN_COLUMN;
1917 else
1918 sregno = DWARF_FRAME_REGNUM (REGNO (src));
1919
1920 dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1921
1922 /* ??? We'd like to use queue_reg_save, but we need to come up with
1923 a different flushing heuristic for epilogues. */
1924 reg_save (label, sregno, dregno, 0);
1925 }
1926
1927 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
1928
1929 static void
1930 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1931 {
1932 dw_cfi_ref cfi = new_cfi ();
1933 unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1934
1935 cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1936 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1937
1938 add_fde_cfi (label, cfi);
1939 }
1940
1941 /* Record call frame debugging information for an expression EXPR,
1942 which either sets SP or FP (adjusting how we calculate the frame
1943 address) or saves a register to the stack or another register.
1944 LABEL indicates the address of EXPR.
1945
1946 This function encodes a state machine mapping rtxes to actions on
1947 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1948 users need not read the source code.
1949
1950 The High-Level Picture
1951
1952 Changes in the register we use to calculate the CFA: Currently we
1953 assume that if you copy the CFA register into another register, we
1954 should take the other one as the new CFA register; this seems to
1955 work pretty well. If it's wrong for some target, it's simple
1956 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1957
1958 Changes in the register we use for saving registers to the stack:
1959 This is usually SP, but not always. Again, we deduce that if you
1960 copy SP into another register (and SP is not the CFA register),
1961 then the new register is the one we will be using for register
1962 saves. This also seems to work.
1963
1964 Register saves: There's not much guesswork about this one; if
1965 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1966 register save, and the register used to calculate the destination
1967 had better be the one we think we're using for this purpose.
1968 It's also assumed that a copy from a call-saved register to another
1969 register is saving that register if RTX_FRAME_RELATED_P is set on
1970 that instruction. If the copy is from a call-saved register to
1971 the *same* register, that means that the register is now the same
1972 value as in the caller.
1973
1974 Except: If the register being saved is the CFA register, and the
1975 offset is nonzero, we are saving the CFA, so we assume we have to
1976 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1977 the intent is to save the value of SP from the previous frame.
1978
1979 In addition, if a register has previously been saved to a different
1980 register,
1981
1982 Invariants / Summaries of Rules
1983
1984 cfa current rule for calculating the CFA. It usually
1985 consists of a register and an offset.
1986 cfa_store register used by prologue code to save things to the stack
1987 cfa_store.offset is the offset from the value of
1988 cfa_store.reg to the actual CFA
1989 cfa_temp register holding an integral value. cfa_temp.offset
1990 stores the value, which will be used to adjust the
1991 stack pointer. cfa_temp is also used like cfa_store,
1992 to track stores to the stack via fp or a temp reg.
1993
1994 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1995 with cfa.reg as the first operand changes the cfa.reg and its
1996 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1997 cfa_temp.offset.
1998
1999 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
2000 expression yielding a constant. This sets cfa_temp.reg
2001 and cfa_temp.offset.
2002
2003 Rule 5: Create a new register cfa_store used to save items to the
2004 stack.
2005
2006 Rules 10-14: Save a register to the stack. Define offset as the
2007 difference of the original location and cfa_store's
2008 location (or cfa_temp's location if cfa_temp is used).
2009
2010 Rules 16-20: If AND operation happens on sp in prologue, we assume
2011 stack is realigned. We will use a group of DW_OP_XXX
2012 expressions to represent the location of the stored
2013 register instead of CFA+offset.
2014
2015 The Rules
2016
2017 "{a,b}" indicates a choice of a xor b.
2018 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2019
2020 Rule 1:
2021 (set <reg1> <reg2>:cfa.reg)
2022 effects: cfa.reg = <reg1>
2023 cfa.offset unchanged
2024 cfa_temp.reg = <reg1>
2025 cfa_temp.offset = cfa.offset
2026
2027 Rule 2:
2028 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2029 {<const_int>,<reg>:cfa_temp.reg}))
2030 effects: cfa.reg = sp if fp used
2031 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2032 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2033 if cfa_store.reg==sp
2034
2035 Rule 3:
2036 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2037 effects: cfa.reg = fp
2038 cfa_offset += +/- <const_int>
2039
2040 Rule 4:
2041 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2042 constraints: <reg1> != fp
2043 <reg1> != sp
2044 effects: cfa.reg = <reg1>
2045 cfa_temp.reg = <reg1>
2046 cfa_temp.offset = cfa.offset
2047
2048 Rule 5:
2049 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2050 constraints: <reg1> != fp
2051 <reg1> != sp
2052 effects: cfa_store.reg = <reg1>
2053 cfa_store.offset = cfa.offset - cfa_temp.offset
2054
2055 Rule 6:
2056 (set <reg> <const_int>)
2057 effects: cfa_temp.reg = <reg>
2058 cfa_temp.offset = <const_int>
2059
2060 Rule 7:
2061 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2062 effects: cfa_temp.reg = <reg1>
2063 cfa_temp.offset |= <const_int>
2064
2065 Rule 8:
2066 (set <reg> (high <exp>))
2067 effects: none
2068
2069 Rule 9:
2070 (set <reg> (lo_sum <exp> <const_int>))
2071 effects: cfa_temp.reg = <reg>
2072 cfa_temp.offset = <const_int>
2073
2074 Rule 10:
2075 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2076 effects: cfa_store.offset -= <const_int>
2077 cfa.offset = cfa_store.offset if cfa.reg == sp
2078 cfa.reg = sp
2079 cfa.base_offset = -cfa_store.offset
2080
2081 Rule 11:
2082 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2083 effects: cfa_store.offset += -/+ mode_size(mem)
2084 cfa.offset = cfa_store.offset if cfa.reg == sp
2085 cfa.reg = sp
2086 cfa.base_offset = -cfa_store.offset
2087
2088 Rule 12:
2089 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2090
2091 <reg2>)
2092 effects: cfa.reg = <reg1>
2093 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2094
2095 Rule 13:
2096 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2097 effects: cfa.reg = <reg1>
2098 cfa.base_offset = -{cfa_store,cfa_temp}.offset
2099
2100 Rule 14:
2101 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2102 effects: cfa.reg = <reg1>
2103 cfa.base_offset = -cfa_temp.offset
2104 cfa_temp.offset -= mode_size(mem)
2105
2106 Rule 15:
2107 (set <reg> {unspec, unspec_volatile})
2108 effects: target-dependent
2109
2110 Rule 16:
2111 (set sp (and: sp <const_int>))
2112 constraints: cfa_store.reg == sp
2113 effects: current_fde.stack_realign = 1
2114 cfa_store.offset = 0
2115 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2116
2117 Rule 17:
2118 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2119 effects: cfa_store.offset += -/+ mode_size(mem)
2120
2121 Rule 18:
2122 (set (mem ({pre_inc, pre_dec} sp)) fp)
2123 constraints: fde->stack_realign == 1
2124 effects: cfa_store.offset = 0
2125 cfa.reg != HARD_FRAME_POINTER_REGNUM
2126
2127 Rule 19:
2128 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2129 constraints: fde->stack_realign == 1
2130 && cfa.offset == 0
2131 && cfa.indirect == 0
2132 && cfa.reg != HARD_FRAME_POINTER_REGNUM
2133 effects: Use DW_CFA_def_cfa_expression to define cfa
2134 cfa.reg == fde->drap_reg
2135
2136 Rule 20:
2137 (set reg fde->drap_reg)
2138 constraints: fde->vdrap_reg == INVALID_REGNUM
2139 effects: fde->vdrap_reg = reg.
2140 (set mem fde->drap_reg)
2141 constraints: fde->drap_reg_saved == 1
2142 effects: none. */
2143
2144 static void
2145 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2146 {
2147 rtx src, dest, span;
2148 HOST_WIDE_INT offset;
2149 dw_fde_ref fde;
2150
2151 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2152 the PARALLEL independently. The first element is always processed if
2153 it is a SET. This is for backward compatibility. Other elements
2154 are processed only if they are SETs and the RTX_FRAME_RELATED_P
2155 flag is set in them. */
2156 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2157 {
2158 int par_index;
2159 int limit = XVECLEN (expr, 0);
2160 rtx elem;
2161
2162 /* PARALLELs have strict read-modify-write semantics, so we
2163 ought to evaluate every rvalue before changing any lvalue.
2164 It's cumbersome to do that in general, but there's an
2165 easy approximation that is enough for all current users:
2166 handle register saves before register assignments. */
2167 if (GET_CODE (expr) == PARALLEL)
2168 for (par_index = 0; par_index < limit; par_index++)
2169 {
2170 elem = XVECEXP (expr, 0, par_index);
2171 if (GET_CODE (elem) == SET
2172 && MEM_P (SET_DEST (elem))
2173 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2174 dwarf2out_frame_debug_expr (elem, label);
2175 }
2176
2177 for (par_index = 0; par_index < limit; par_index++)
2178 {
2179 elem = XVECEXP (expr, 0, par_index);
2180 if (GET_CODE (elem) == SET
2181 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2182 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2183 dwarf2out_frame_debug_expr (elem, label);
2184 else if (GET_CODE (elem) == SET
2185 && par_index != 0
2186 && !RTX_FRAME_RELATED_P (elem))
2187 {
2188 /* Stack adjustment combining might combine some post-prologue
2189 stack adjustment into a prologue stack adjustment. */
2190 HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2191
2192 if (offset != 0)
2193 dwarf2out_args_size_adjust (offset, label);
2194 }
2195 }
2196 return;
2197 }
2198
2199 gcc_assert (GET_CODE (expr) == SET);
2200
2201 src = SET_SRC (expr);
2202 dest = SET_DEST (expr);
2203
2204 if (REG_P (src))
2205 {
2206 rtx rsi = reg_saved_in (src);
2207 if (rsi)
2208 src = rsi;
2209 }
2210
2211 fde = current_fde ();
2212
2213 if (REG_P (src)
2214 && fde
2215 && fde->drap_reg == REGNO (src)
2216 && (fde->drap_reg_saved
2217 || REG_P (dest)))
2218 {
2219 /* Rule 20 */
2220 /* If we are saving dynamic realign argument pointer to a
2221 register, the destination is virtual dynamic realign
2222 argument pointer. It may be used to access argument. */
2223 if (REG_P (dest))
2224 {
2225 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2226 fde->vdrap_reg = REGNO (dest);
2227 }
2228 return;
2229 }
2230
2231 switch (GET_CODE (dest))
2232 {
2233 case REG:
2234 switch (GET_CODE (src))
2235 {
2236 /* Setting FP from SP. */
2237 case REG:
2238 if (cfa.reg == (unsigned) REGNO (src))
2239 {
2240 /* Rule 1 */
2241 /* Update the CFA rule wrt SP or FP. Make sure src is
2242 relative to the current CFA register.
2243
2244 We used to require that dest be either SP or FP, but the
2245 ARM copies SP to a temporary register, and from there to
2246 FP. So we just rely on the backends to only set
2247 RTX_FRAME_RELATED_P on appropriate insns. */
2248 cfa.reg = REGNO (dest);
2249 cfa_temp.reg = cfa.reg;
2250 cfa_temp.offset = cfa.offset;
2251 }
2252 else
2253 {
2254 /* Saving a register in a register. */
2255 gcc_assert (!fixed_regs [REGNO (dest)]
2256 /* For the SPARC and its register window. */
2257 || (DWARF_FRAME_REGNUM (REGNO (src))
2258 == DWARF_FRAME_RETURN_COLUMN));
2259
2260 /* After stack is aligned, we can only save SP in FP
2261 if drap register is used. In this case, we have
2262 to restore stack pointer with the CFA value and we
2263 don't generate this DWARF information. */
2264 if (fde
2265 && fde->stack_realign
2266 && REGNO (src) == STACK_POINTER_REGNUM)
2267 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2268 && fde->drap_reg != INVALID_REGNUM
2269 && cfa.reg != REGNO (src));
2270 else
2271 queue_reg_save (label, src, dest, 0);
2272 }
2273 break;
2274
2275 case PLUS:
2276 case MINUS:
2277 case LO_SUM:
2278 if (dest == stack_pointer_rtx)
2279 {
2280 /* Rule 2 */
2281 /* Adjusting SP. */
2282 switch (GET_CODE (XEXP (src, 1)))
2283 {
2284 case CONST_INT:
2285 offset = INTVAL (XEXP (src, 1));
2286 break;
2287 case REG:
2288 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2289 == cfa_temp.reg);
2290 offset = cfa_temp.offset;
2291 break;
2292 default:
2293 gcc_unreachable ();
2294 }
2295
2296 if (XEXP (src, 0) == hard_frame_pointer_rtx)
2297 {
2298 /* Restoring SP from FP in the epilogue. */
2299 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2300 cfa.reg = STACK_POINTER_REGNUM;
2301 }
2302 else if (GET_CODE (src) == LO_SUM)
2303 /* Assume we've set the source reg of the LO_SUM from sp. */
2304 ;
2305 else
2306 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2307
2308 if (GET_CODE (src) != MINUS)
2309 offset = -offset;
2310 if (cfa.reg == STACK_POINTER_REGNUM)
2311 cfa.offset += offset;
2312 if (cfa_store.reg == STACK_POINTER_REGNUM)
2313 cfa_store.offset += offset;
2314 }
2315 else if (dest == hard_frame_pointer_rtx)
2316 {
2317 /* Rule 3 */
2318 /* Either setting the FP from an offset of the SP,
2319 or adjusting the FP */
2320 gcc_assert (frame_pointer_needed);
2321
2322 gcc_assert (REG_P (XEXP (src, 0))
2323 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2324 && CONST_INT_P (XEXP (src, 1)));
2325 offset = INTVAL (XEXP (src, 1));
2326 if (GET_CODE (src) != MINUS)
2327 offset = -offset;
2328 cfa.offset += offset;
2329 cfa.reg = HARD_FRAME_POINTER_REGNUM;
2330 }
2331 else
2332 {
2333 gcc_assert (GET_CODE (src) != MINUS);
2334
2335 /* Rule 4 */
2336 if (REG_P (XEXP (src, 0))
2337 && REGNO (XEXP (src, 0)) == cfa.reg
2338 && CONST_INT_P (XEXP (src, 1)))
2339 {
2340 /* Setting a temporary CFA register that will be copied
2341 into the FP later on. */
2342 offset = - INTVAL (XEXP (src, 1));
2343 cfa.offset += offset;
2344 cfa.reg = REGNO (dest);
2345 /* Or used to save regs to the stack. */
2346 cfa_temp.reg = cfa.reg;
2347 cfa_temp.offset = cfa.offset;
2348 }
2349
2350 /* Rule 5 */
2351 else if (REG_P (XEXP (src, 0))
2352 && REGNO (XEXP (src, 0)) == cfa_temp.reg
2353 && XEXP (src, 1) == stack_pointer_rtx)
2354 {
2355 /* Setting a scratch register that we will use instead
2356 of SP for saving registers to the stack. */
2357 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2358 cfa_store.reg = REGNO (dest);
2359 cfa_store.offset = cfa.offset - cfa_temp.offset;
2360 }
2361
2362 /* Rule 9 */
2363 else if (GET_CODE (src) == LO_SUM
2364 && CONST_INT_P (XEXP (src, 1)))
2365 {
2366 cfa_temp.reg = REGNO (dest);
2367 cfa_temp.offset = INTVAL (XEXP (src, 1));
2368 }
2369 else
2370 gcc_unreachable ();
2371 }
2372 break;
2373
2374 /* Rule 6 */
2375 case CONST_INT:
2376 cfa_temp.reg = REGNO (dest);
2377 cfa_temp.offset = INTVAL (src);
2378 break;
2379
2380 /* Rule 7 */
2381 case IOR:
2382 gcc_assert (REG_P (XEXP (src, 0))
2383 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2384 && CONST_INT_P (XEXP (src, 1)));
2385
2386 if ((unsigned) REGNO (dest) != cfa_temp.reg)
2387 cfa_temp.reg = REGNO (dest);
2388 cfa_temp.offset |= INTVAL (XEXP (src, 1));
2389 break;
2390
2391 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2392 which will fill in all of the bits. */
2393 /* Rule 8 */
2394 case HIGH:
2395 break;
2396
2397 /* Rule 15 */
2398 case UNSPEC:
2399 case UNSPEC_VOLATILE:
2400 gcc_assert (targetm.dwarf_handle_frame_unspec);
2401 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2402 return;
2403
2404 /* Rule 16 */
2405 case AND:
2406 /* If this AND operation happens on stack pointer in prologue,
2407 we assume the stack is realigned and we extract the
2408 alignment. */
2409 if (fde && XEXP (src, 0) == stack_pointer_rtx)
2410 {
2411 gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2412 fde->stack_realign = 1;
2413 fde->stack_realignment = INTVAL (XEXP (src, 1));
2414 cfa_store.offset = 0;
2415
2416 if (cfa.reg != STACK_POINTER_REGNUM
2417 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2418 fde->drap_reg = cfa.reg;
2419 }
2420 return;
2421
2422 default:
2423 gcc_unreachable ();
2424 }
2425
2426 def_cfa_1 (label, &cfa);
2427 break;
2428
2429 case MEM:
2430
2431 /* Saving a register to the stack. Make sure dest is relative to the
2432 CFA register. */
2433 switch (GET_CODE (XEXP (dest, 0)))
2434 {
2435 /* Rule 10 */
2436 /* With a push. */
2437 case PRE_MODIFY:
2438 /* We can't handle variable size modifications. */
2439 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2440 == CONST_INT);
2441 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2442
2443 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2444 && cfa_store.reg == STACK_POINTER_REGNUM);
2445
2446 cfa_store.offset += offset;
2447 if (cfa.reg == STACK_POINTER_REGNUM)
2448 cfa.offset = cfa_store.offset;
2449
2450 offset = -cfa_store.offset;
2451 break;
2452
2453 /* Rule 11 */
2454 case PRE_INC:
2455 case PRE_DEC:
2456 offset = GET_MODE_SIZE (GET_MODE (dest));
2457 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2458 offset = -offset;
2459
2460 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2461 == STACK_POINTER_REGNUM)
2462 && cfa_store.reg == STACK_POINTER_REGNUM);
2463
2464 cfa_store.offset += offset;
2465
2466 /* Rule 18: If stack is aligned, we will use FP as a
2467 reference to represent the address of the stored
2468 regiser. */
2469 if (fde
2470 && fde->stack_realign
2471 && src == hard_frame_pointer_rtx)
2472 {
2473 gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2474 cfa_store.offset = 0;
2475 }
2476
2477 if (cfa.reg == STACK_POINTER_REGNUM)
2478 cfa.offset = cfa_store.offset;
2479
2480 offset = -cfa_store.offset;
2481 break;
2482
2483 /* Rule 12 */
2484 /* With an offset. */
2485 case PLUS:
2486 case MINUS:
2487 case LO_SUM:
2488 {
2489 int regno;
2490
2491 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2492 && REG_P (XEXP (XEXP (dest, 0), 0)));
2493 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2494 if (GET_CODE (XEXP (dest, 0)) == MINUS)
2495 offset = -offset;
2496
2497 regno = REGNO (XEXP (XEXP (dest, 0), 0));
2498
2499 if (cfa_store.reg == (unsigned) regno)
2500 offset -= cfa_store.offset;
2501 else
2502 {
2503 gcc_assert (cfa_temp.reg == (unsigned) regno);
2504 offset -= cfa_temp.offset;
2505 }
2506 }
2507 break;
2508
2509 /* Rule 13 */
2510 /* Without an offset. */
2511 case REG:
2512 {
2513 int regno = REGNO (XEXP (dest, 0));
2514
2515 if (cfa_store.reg == (unsigned) regno)
2516 offset = -cfa_store.offset;
2517 else
2518 {
2519 gcc_assert (cfa_temp.reg == (unsigned) regno);
2520 offset = -cfa_temp.offset;
2521 }
2522 }
2523 break;
2524
2525 /* Rule 14 */
2526 case POST_INC:
2527 gcc_assert (cfa_temp.reg
2528 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2529 offset = -cfa_temp.offset;
2530 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2531 break;
2532
2533 default:
2534 gcc_unreachable ();
2535 }
2536
2537 /* Rule 17 */
2538 /* If the source operand of this MEM operation is not a
2539 register, basically the source is return address. Here
2540 we only care how much stack grew and we don't save it. */
2541 if (!REG_P (src))
2542 break;
2543
2544 if (REGNO (src) != STACK_POINTER_REGNUM
2545 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2546 && (unsigned) REGNO (src) == cfa.reg)
2547 {
2548 /* We're storing the current CFA reg into the stack. */
2549
2550 if (cfa.offset == 0)
2551 {
2552 /* Rule 19 */
2553 /* If stack is aligned, putting CFA reg into stack means
2554 we can no longer use reg + offset to represent CFA.
2555 Here we use DW_CFA_def_cfa_expression instead. The
2556 result of this expression equals to the original CFA
2557 value. */
2558 if (fde
2559 && fde->stack_realign
2560 && cfa.indirect == 0
2561 && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2562 {
2563 dw_cfa_location cfa_exp;
2564
2565 gcc_assert (fde->drap_reg == cfa.reg);
2566
2567 cfa_exp.indirect = 1;
2568 cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2569 cfa_exp.base_offset = offset;
2570 cfa_exp.offset = 0;
2571
2572 fde->drap_reg_saved = 1;
2573
2574 def_cfa_1 (label, &cfa_exp);
2575 break;
2576 }
2577
2578 /* If the source register is exactly the CFA, assume
2579 we're saving SP like any other register; this happens
2580 on the ARM. */
2581 def_cfa_1 (label, &cfa);
2582 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2583 break;
2584 }
2585 else
2586 {
2587 /* Otherwise, we'll need to look in the stack to
2588 calculate the CFA. */
2589 rtx x = XEXP (dest, 0);
2590
2591 if (!REG_P (x))
2592 x = XEXP (x, 0);
2593 gcc_assert (REG_P (x));
2594
2595 cfa.reg = REGNO (x);
2596 cfa.base_offset = offset;
2597 cfa.indirect = 1;
2598 def_cfa_1 (label, &cfa);
2599 break;
2600 }
2601 }
2602
2603 def_cfa_1 (label, &cfa);
2604 {
2605 span = targetm.dwarf_register_span (src);
2606
2607 if (!span)
2608 queue_reg_save (label, src, NULL_RTX, offset);
2609 else
2610 {
2611 /* We have a PARALLEL describing where the contents of SRC
2612 live. Queue register saves for each piece of the
2613 PARALLEL. */
2614 int par_index;
2615 int limit;
2616 HOST_WIDE_INT span_offset = offset;
2617
2618 gcc_assert (GET_CODE (span) == PARALLEL);
2619
2620 limit = XVECLEN (span, 0);
2621 for (par_index = 0; par_index < limit; par_index++)
2622 {
2623 rtx elem = XVECEXP (span, 0, par_index);
2624
2625 queue_reg_save (label, elem, NULL_RTX, span_offset);
2626 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2627 }
2628 }
2629 }
2630 break;
2631
2632 default:
2633 gcc_unreachable ();
2634 }
2635 }
2636
2637 /* Record call frame debugging information for INSN, which either
2638 sets SP or FP (adjusting how we calculate the frame address) or saves a
2639 register to the stack. If INSN is NULL_RTX, initialize our state.
2640
2641 If AFTER_P is false, we're being called before the insn is emitted,
2642 otherwise after. Call instructions get invoked twice. */
2643
2644 void
2645 dwarf2out_frame_debug (rtx insn, bool after_p)
2646 {
2647 const char *label;
2648 rtx note, n;
2649 bool handled_one = false;
2650
2651 if (insn == NULL_RTX)
2652 {
2653 size_t i;
2654
2655 /* Flush any queued register saves. */
2656 flush_queued_reg_saves ();
2657
2658 /* Set up state for generating call frame debug info. */
2659 lookup_cfa (&cfa);
2660 gcc_assert (cfa.reg
2661 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2662
2663 cfa.reg = STACK_POINTER_REGNUM;
2664 cfa_store = cfa;
2665 cfa_temp.reg = -1;
2666 cfa_temp.offset = 0;
2667
2668 for (i = 0; i < num_regs_saved_in_regs; i++)
2669 {
2670 regs_saved_in_regs[i].orig_reg = NULL_RTX;
2671 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2672 }
2673 num_regs_saved_in_regs = 0;
2674
2675 if (barrier_args_size)
2676 {
2677 XDELETEVEC (barrier_args_size);
2678 barrier_args_size = NULL;
2679 }
2680 return;
2681 }
2682
2683 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2684 flush_queued_reg_saves ();
2685
2686 if (! RTX_FRAME_RELATED_P (insn))
2687 {
2688 if (!ACCUMULATE_OUTGOING_ARGS)
2689 dwarf2out_stack_adjust (insn, after_p);
2690 return;
2691 }
2692
2693 label = dwarf2out_cfi_label (false);
2694
2695 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2696 switch (REG_NOTE_KIND (note))
2697 {
2698 case REG_FRAME_RELATED_EXPR:
2699 insn = XEXP (note, 0);
2700 goto found;
2701
2702 case REG_CFA_DEF_CFA:
2703 dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2704 handled_one = true;
2705 break;
2706
2707 case REG_CFA_ADJUST_CFA:
2708 n = XEXP (note, 0);
2709 if (n == NULL)
2710 {
2711 n = PATTERN (insn);
2712 if (GET_CODE (n) == PARALLEL)
2713 n = XVECEXP (n, 0, 0);
2714 }
2715 dwarf2out_frame_debug_adjust_cfa (n, label);
2716 handled_one = true;
2717 break;
2718
2719 case REG_CFA_OFFSET:
2720 n = XEXP (note, 0);
2721 if (n == NULL)
2722 n = single_set (insn);
2723 dwarf2out_frame_debug_cfa_offset (n, label);
2724 handled_one = true;
2725 break;
2726
2727 case REG_CFA_REGISTER:
2728 n = XEXP (note, 0);
2729 if (n == NULL)
2730 {
2731 n = PATTERN (insn);
2732 if (GET_CODE (n) == PARALLEL)
2733 n = XVECEXP (n, 0, 0);
2734 }
2735 dwarf2out_frame_debug_cfa_register (n, label);
2736 handled_one = true;
2737 break;
2738
2739 case REG_CFA_RESTORE:
2740 n = XEXP (note, 0);
2741 if (n == NULL)
2742 {
2743 n = PATTERN (insn);
2744 if (GET_CODE (n) == PARALLEL)
2745 n = XVECEXP (n, 0, 0);
2746 n = XEXP (n, 0);
2747 }
2748 dwarf2out_frame_debug_cfa_restore (n, label);
2749 handled_one = true;
2750 break;
2751
2752 default:
2753 break;
2754 }
2755 if (handled_one)
2756 return;
2757
2758 insn = PATTERN (insn);
2759 found:
2760 dwarf2out_frame_debug_expr (insn, label);
2761 }
2762
2763 /* Determine if we need to save and restore CFI information around this
2764 epilogue. If SIBCALL is true, then this is a sibcall epilogue. If
2765 we do need to save/restore, then emit the save now, and insert a
2766 NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream. */
2767
2768 void
2769 dwarf2out_begin_epilogue (rtx insn)
2770 {
2771 bool saw_frp = false;
2772 rtx i;
2773
2774 /* Scan forward to the return insn, noticing if there are possible
2775 frame related insns. */
2776 for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2777 {
2778 if (!INSN_P (i))
2779 continue;
2780
2781 /* Look for both regular and sibcalls to end the block. */
2782 if (returnjump_p (i))
2783 break;
2784 if (CALL_P (i) && SIBLING_CALL_P (i))
2785 break;
2786
2787 if (GET_CODE (PATTERN (i)) == SEQUENCE)
2788 {
2789 int idx;
2790 rtx seq = PATTERN (i);
2791
2792 if (returnjump_p (XVECEXP (seq, 0, 0)))
2793 break;
2794 if (CALL_P (XVECEXP (seq, 0, 0))
2795 && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2796 break;
2797
2798 for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2799 if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2800 saw_frp = true;
2801 }
2802
2803 if (RTX_FRAME_RELATED_P (i))
2804 saw_frp = true;
2805 }
2806
2807 /* If the port doesn't emit epilogue unwind info, we don't need a
2808 save/restore pair. */
2809 if (!saw_frp)
2810 return;
2811
2812 /* Otherwise, search forward to see if the return insn was the last
2813 basic block of the function. If so, we don't need save/restore. */
2814 gcc_assert (i != NULL);
2815 i = next_real_insn (i);
2816 if (i == NULL)
2817 return;
2818
2819 /* Insert the restore before that next real insn in the stream, and before
2820 a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2821 properly nested. This should be after any label or alignment. This
2822 will be pushed into the CFI stream by the function below. */
2823 while (1)
2824 {
2825 rtx p = PREV_INSN (i);
2826 if (!NOTE_P (p))
2827 break;
2828 if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2829 break;
2830 i = p;
2831 }
2832 emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2833
2834 emit_cfa_remember = true;
2835
2836 /* And emulate the state save. */
2837 gcc_assert (!cfa_remember.in_use);
2838 cfa_remember = cfa;
2839 cfa_remember.in_use = 1;
2840 }
2841
2842 /* A "subroutine" of dwarf2out_begin_epilogue. Emit the restore required. */
2843
2844 void
2845 dwarf2out_frame_debug_restore_state (void)
2846 {
2847 dw_cfi_ref cfi = new_cfi ();
2848 const char *label = dwarf2out_cfi_label (false);
2849
2850 cfi->dw_cfi_opc = DW_CFA_restore_state;
2851 add_fde_cfi (label, cfi);
2852
2853 gcc_assert (cfa_remember.in_use);
2854 cfa = cfa_remember;
2855 cfa_remember.in_use = 0;
2856 }
2857
2858 #endif
2859
2860 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
2861 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2862 (enum dwarf_call_frame_info cfi);
2863
2864 static enum dw_cfi_oprnd_type
2865 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2866 {
2867 switch (cfi)
2868 {
2869 case DW_CFA_nop:
2870 case DW_CFA_GNU_window_save:
2871 case DW_CFA_remember_state:
2872 case DW_CFA_restore_state:
2873 return dw_cfi_oprnd_unused;
2874
2875 case DW_CFA_set_loc:
2876 case DW_CFA_advance_loc1:
2877 case DW_CFA_advance_loc2:
2878 case DW_CFA_advance_loc4:
2879 case DW_CFA_MIPS_advance_loc8:
2880 return dw_cfi_oprnd_addr;
2881
2882 case DW_CFA_offset:
2883 case DW_CFA_offset_extended:
2884 case DW_CFA_def_cfa:
2885 case DW_CFA_offset_extended_sf:
2886 case DW_CFA_def_cfa_sf:
2887 case DW_CFA_restore:
2888 case DW_CFA_restore_extended:
2889 case DW_CFA_undefined:
2890 case DW_CFA_same_value:
2891 case DW_CFA_def_cfa_register:
2892 case DW_CFA_register:
2893 return dw_cfi_oprnd_reg_num;
2894
2895 case DW_CFA_def_cfa_offset:
2896 case DW_CFA_GNU_args_size:
2897 case DW_CFA_def_cfa_offset_sf:
2898 return dw_cfi_oprnd_offset;
2899
2900 case DW_CFA_def_cfa_expression:
2901 case DW_CFA_expression:
2902 return dw_cfi_oprnd_loc;
2903
2904 default:
2905 gcc_unreachable ();
2906 }
2907 }
2908
2909 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
2910 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2911 (enum dwarf_call_frame_info cfi);
2912
2913 static enum dw_cfi_oprnd_type
2914 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2915 {
2916 switch (cfi)
2917 {
2918 case DW_CFA_def_cfa:
2919 case DW_CFA_def_cfa_sf:
2920 case DW_CFA_offset:
2921 case DW_CFA_offset_extended_sf:
2922 case DW_CFA_offset_extended:
2923 return dw_cfi_oprnd_offset;
2924
2925 case DW_CFA_register:
2926 return dw_cfi_oprnd_reg_num;
2927
2928 default:
2929 return dw_cfi_oprnd_unused;
2930 }
2931 }
2932
2933 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2934
2935 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
2936 switch to the data section instead, and write out a synthetic start label
2937 for collect2 the first time around. */
2938
2939 static void
2940 switch_to_eh_frame_section (bool back)
2941 {
2942 tree label;
2943
2944 #ifdef EH_FRAME_SECTION_NAME
2945 if (eh_frame_section == 0)
2946 {
2947 int flags;
2948
2949 if (EH_TABLES_CAN_BE_READ_ONLY)
2950 {
2951 int fde_encoding;
2952 int per_encoding;
2953 int lsda_encoding;
2954
2955 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2956 /*global=*/0);
2957 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2958 /*global=*/1);
2959 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2960 /*global=*/0);
2961 flags = ((! flag_pic
2962 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2963 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2964 && (per_encoding & 0x70) != DW_EH_PE_absptr
2965 && (per_encoding & 0x70) != DW_EH_PE_aligned
2966 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2967 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2968 ? 0 : SECTION_WRITE);
2969 }
2970 else
2971 flags = SECTION_WRITE;
2972 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2973 }
2974 #endif
2975
2976 if (eh_frame_section)
2977 switch_to_section (eh_frame_section);
2978 else
2979 {
2980 /* We have no special eh_frame section. Put the information in
2981 the data section and emit special labels to guide collect2. */
2982 switch_to_section (data_section);
2983
2984 if (!back)
2985 {
2986 label = get_file_function_name ("F");
2987 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2988 targetm.asm_out.globalize_label (asm_out_file,
2989 IDENTIFIER_POINTER (label));
2990 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2991 }
2992 }
2993 }
2994
2995 /* Output a Call Frame Information opcode and its operand(s). */
2996
2997 static void
2998 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2999 {
3000 unsigned long r;
3001 HOST_WIDE_INT off;
3002
3003 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3004 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3005 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3006 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3007 ((unsigned HOST_WIDE_INT)
3008 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3009 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3010 {
3011 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3012 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3013 "DW_CFA_offset, column 0x%lx", r);
3014 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3015 dw2_asm_output_data_uleb128 (off, NULL);
3016 }
3017 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3018 {
3019 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3020 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3021 "DW_CFA_restore, column 0x%lx", r);
3022 }
3023 else
3024 {
3025 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3026 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3027
3028 switch (cfi->dw_cfi_opc)
3029 {
3030 case DW_CFA_set_loc:
3031 if (for_eh)
3032 dw2_asm_output_encoded_addr_rtx (
3033 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3034 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3035 false, NULL);
3036 else
3037 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3038 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3039 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3040 break;
3041
3042 case DW_CFA_advance_loc1:
3043 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3044 fde->dw_fde_current_label, NULL);
3045 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3046 break;
3047
3048 case DW_CFA_advance_loc2:
3049 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3050 fde->dw_fde_current_label, NULL);
3051 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3052 break;
3053
3054 case DW_CFA_advance_loc4:
3055 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3056 fde->dw_fde_current_label, NULL);
3057 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3058 break;
3059
3060 case DW_CFA_MIPS_advance_loc8:
3061 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3062 fde->dw_fde_current_label, NULL);
3063 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3064 break;
3065
3066 case DW_CFA_offset_extended:
3067 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3068 dw2_asm_output_data_uleb128 (r, NULL);
3069 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3070 dw2_asm_output_data_uleb128 (off, NULL);
3071 break;
3072
3073 case DW_CFA_def_cfa:
3074 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3075 dw2_asm_output_data_uleb128 (r, NULL);
3076 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3077 break;
3078
3079 case DW_CFA_offset_extended_sf:
3080 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3081 dw2_asm_output_data_uleb128 (r, NULL);
3082 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3083 dw2_asm_output_data_sleb128 (off, NULL);
3084 break;
3085
3086 case DW_CFA_def_cfa_sf:
3087 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3088 dw2_asm_output_data_uleb128 (r, NULL);
3089 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3090 dw2_asm_output_data_sleb128 (off, NULL);
3091 break;
3092
3093 case DW_CFA_restore_extended:
3094 case DW_CFA_undefined:
3095 case DW_CFA_same_value:
3096 case DW_CFA_def_cfa_register:
3097 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3098 dw2_asm_output_data_uleb128 (r, NULL);
3099 break;
3100
3101 case DW_CFA_register:
3102 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3103 dw2_asm_output_data_uleb128 (r, NULL);
3104 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3105 dw2_asm_output_data_uleb128 (r, NULL);
3106 break;
3107
3108 case DW_CFA_def_cfa_offset:
3109 case DW_CFA_GNU_args_size:
3110 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3111 break;
3112
3113 case DW_CFA_def_cfa_offset_sf:
3114 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3115 dw2_asm_output_data_sleb128 (off, NULL);
3116 break;
3117
3118 case DW_CFA_GNU_window_save:
3119 break;
3120
3121 case DW_CFA_def_cfa_expression:
3122 case DW_CFA_expression:
3123 output_cfa_loc (cfi);
3124 break;
3125
3126 case DW_CFA_GNU_negative_offset_extended:
3127 /* Obsoleted by DW_CFA_offset_extended_sf. */
3128 gcc_unreachable ();
3129
3130 default:
3131 break;
3132 }
3133 }
3134 }
3135
3136 /* Similar, but do it via assembler directives instead. */
3137
3138 static void
3139 output_cfi_directive (dw_cfi_ref cfi)
3140 {
3141 unsigned long r, r2;
3142
3143 switch (cfi->dw_cfi_opc)
3144 {
3145 case DW_CFA_advance_loc:
3146 case DW_CFA_advance_loc1:
3147 case DW_CFA_advance_loc2:
3148 case DW_CFA_advance_loc4:
3149 case DW_CFA_MIPS_advance_loc8:
3150 case DW_CFA_set_loc:
3151 /* Should only be created by add_fde_cfi in a code path not
3152 followed when emitting via directives. The assembler is
3153 going to take care of this for us. */
3154 gcc_unreachable ();
3155
3156 case DW_CFA_offset:
3157 case DW_CFA_offset_extended:
3158 case DW_CFA_offset_extended_sf:
3159 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3160 fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3161 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3162 break;
3163
3164 case DW_CFA_restore:
3165 case DW_CFA_restore_extended:
3166 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3167 fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3168 break;
3169
3170 case DW_CFA_undefined:
3171 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3172 fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3173 break;
3174
3175 case DW_CFA_same_value:
3176 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3177 fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3178 break;
3179
3180 case DW_CFA_def_cfa:
3181 case DW_CFA_def_cfa_sf:
3182 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3183 fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3184 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3185 break;
3186
3187 case DW_CFA_def_cfa_register:
3188 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3189 fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3190 break;
3191
3192 case DW_CFA_register:
3193 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3194 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3195 fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3196 break;
3197
3198 case DW_CFA_def_cfa_offset:
3199 case DW_CFA_def_cfa_offset_sf:
3200 fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3201 HOST_WIDE_INT_PRINT_DEC"\n",
3202 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3203 break;
3204
3205 case DW_CFA_remember_state:
3206 fprintf (asm_out_file, "\t.cfi_remember_state\n");
3207 break;
3208 case DW_CFA_restore_state:
3209 fprintf (asm_out_file, "\t.cfi_restore_state\n");
3210 break;
3211
3212 case DW_CFA_GNU_args_size:
3213 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3214 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3215 if (flag_debug_asm)
3216 fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3217 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3218 fputc ('\n', asm_out_file);
3219 break;
3220
3221 case DW_CFA_GNU_window_save:
3222 fprintf (asm_out_file, "\t.cfi_window_save\n");
3223 break;
3224
3225 case DW_CFA_def_cfa_expression:
3226 case DW_CFA_expression:
3227 fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3228 output_cfa_loc_raw (cfi);
3229 fputc ('\n', asm_out_file);
3230 break;
3231
3232 default:
3233 gcc_unreachable ();
3234 }
3235 }
3236
3237 DEF_VEC_P (dw_cfi_ref);
3238 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3239
3240 /* Output CFIs to bring current FDE to the same state as after executing
3241 CFIs in CFI chain. DO_CFI_ASM is true if .cfi_* directives shall
3242 be emitted, false otherwise. If it is false, FDE and FOR_EH are the
3243 other arguments to pass to output_cfi. */
3244
3245 static void
3246 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3247 {
3248 struct dw_cfi_struct cfi_buf;
3249 dw_cfi_ref cfi2;
3250 dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3251 VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3252 unsigned int len, idx;
3253
3254 for (;; cfi = cfi->dw_cfi_next)
3255 switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3256 {
3257 case DW_CFA_advance_loc:
3258 case DW_CFA_advance_loc1:
3259 case DW_CFA_advance_loc2:
3260 case DW_CFA_advance_loc4:
3261 case DW_CFA_MIPS_advance_loc8:
3262 case DW_CFA_set_loc:
3263 /* All advances should be ignored. */
3264 break;
3265 case DW_CFA_remember_state:
3266 {
3267 dw_cfi_ref args_size = cfi_args_size;
3268
3269 /* Skip everything between .cfi_remember_state and
3270 .cfi_restore_state. */
3271 for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3272 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3273 break;
3274 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3275 args_size = cfi2;
3276 else
3277 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3278
3279 if (cfi2 == NULL)
3280 goto flush_all;
3281 else
3282 {
3283 cfi = cfi2;
3284 cfi_args_size = args_size;
3285 }
3286 break;
3287 }
3288 case DW_CFA_GNU_args_size:
3289 cfi_args_size = cfi;
3290 break;
3291 case DW_CFA_GNU_window_save:
3292 goto flush_all;
3293 case DW_CFA_offset:
3294 case DW_CFA_offset_extended:
3295 case DW_CFA_offset_extended_sf:
3296 case DW_CFA_restore:
3297 case DW_CFA_restore_extended:
3298 case DW_CFA_undefined:
3299 case DW_CFA_same_value:
3300 case DW_CFA_register:
3301 case DW_CFA_val_offset:
3302 case DW_CFA_val_offset_sf:
3303 case DW_CFA_expression:
3304 case DW_CFA_val_expression:
3305 case DW_CFA_GNU_negative_offset_extended:
3306 if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3307 VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3308 cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3309 VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3310 break;
3311 case DW_CFA_def_cfa:
3312 case DW_CFA_def_cfa_sf:
3313 case DW_CFA_def_cfa_expression:
3314 cfi_cfa = cfi;
3315 cfi_cfa_offset = cfi;
3316 break;
3317 case DW_CFA_def_cfa_register:
3318 cfi_cfa = cfi;
3319 break;
3320 case DW_CFA_def_cfa_offset:
3321 case DW_CFA_def_cfa_offset_sf:
3322 cfi_cfa_offset = cfi;
3323 break;
3324 case DW_CFA_nop:
3325 gcc_assert (cfi == NULL);
3326 flush_all:
3327 len = VEC_length (dw_cfi_ref, regs);
3328 for (idx = 0; idx < len; idx++)
3329 {
3330 cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3331 if (cfi2 != NULL
3332 && cfi2->dw_cfi_opc != DW_CFA_restore
3333 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3334 {
3335 if (do_cfi_asm)
3336 output_cfi_directive (cfi2);
3337 else
3338 output_cfi (cfi2, fde, for_eh);
3339 }
3340 }
3341 if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3342 {
3343 gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3344 cfi_buf = *cfi_cfa;
3345 switch (cfi_cfa_offset->dw_cfi_opc)
3346 {
3347 case DW_CFA_def_cfa_offset:
3348 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3349 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3350 break;
3351 case DW_CFA_def_cfa_offset_sf:
3352 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3353 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3354 break;
3355 case DW_CFA_def_cfa:
3356 case DW_CFA_def_cfa_sf:
3357 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3358 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3359 break;
3360 default:
3361 gcc_unreachable ();
3362 }
3363 cfi_cfa = &cfi_buf;
3364 }
3365 else if (cfi_cfa_offset)
3366 cfi_cfa = cfi_cfa_offset;
3367 if (cfi_cfa)
3368 {
3369 if (do_cfi_asm)
3370 output_cfi_directive (cfi_cfa);
3371 else
3372 output_cfi (cfi_cfa, fde, for_eh);
3373 }
3374 cfi_cfa = NULL;
3375 cfi_cfa_offset = NULL;
3376 if (cfi_args_size
3377 && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3378 {
3379 if (do_cfi_asm)
3380 output_cfi_directive (cfi_args_size);
3381 else
3382 output_cfi (cfi_args_size, fde, for_eh);
3383 }
3384 cfi_args_size = NULL;
3385 if (cfi == NULL)
3386 {
3387 VEC_free (dw_cfi_ref, heap, regs);
3388 return;
3389 }
3390 else if (do_cfi_asm)
3391 output_cfi_directive (cfi);
3392 else
3393 output_cfi (cfi, fde, for_eh);
3394 break;
3395 default:
3396 gcc_unreachable ();
3397 }
3398 }
3399
3400 /* Output one FDE. */
3401
3402 static void
3403 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3404 char *section_start_label, int fde_encoding, char *augmentation,
3405 bool any_lsda_needed, int lsda_encoding)
3406 {
3407 const char *begin, *end;
3408 static unsigned int j;
3409 char l1[20], l2[20];
3410 dw_cfi_ref cfi;
3411
3412 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3413 /* empty */ 0);
3414 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3415 for_eh + j);
3416 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3417 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3418 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3419 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3420 " indicating 64-bit DWARF extension");
3421 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3422 "FDE Length");
3423 ASM_OUTPUT_LABEL (asm_out_file, l1);
3424
3425 if (for_eh)
3426 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3427 else
3428 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3429 debug_frame_section, "FDE CIE offset");
3430
3431 if (!fde->dw_fde_switched_sections)
3432 {
3433 begin = fde->dw_fde_begin;
3434 end = fde->dw_fde_end;
3435 }
3436 else
3437 {
3438 /* For the first section, prefer dw_fde_begin over
3439 dw_fde_{hot,cold}_section_label, as the latter
3440 might be separated from the real start of the
3441 function by alignment padding. */
3442 if (!second)
3443 begin = fde->dw_fde_begin;
3444 else if (fde->dw_fde_switched_cold_to_hot)
3445 begin = fde->dw_fde_hot_section_label;
3446 else
3447 begin = fde->dw_fde_unlikely_section_label;
3448 if (second ^ fde->dw_fde_switched_cold_to_hot)
3449 end = fde->dw_fde_unlikely_section_end_label;
3450 else
3451 end = fde->dw_fde_hot_section_end_label;
3452 }
3453
3454 if (for_eh)
3455 {
3456 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3457 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3458 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3459 "FDE initial location");
3460 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3461 end, begin, "FDE address range");
3462 }
3463 else
3464 {
3465 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3466 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3467 }
3468
3469 if (augmentation[0])
3470 {
3471 if (any_lsda_needed)
3472 {
3473 int size = size_of_encoded_value (lsda_encoding);
3474
3475 if (lsda_encoding == DW_EH_PE_aligned)
3476 {
3477 int offset = ( 4 /* Length */
3478 + 4 /* CIE offset */
3479 + 2 * size_of_encoded_value (fde_encoding)
3480 + 1 /* Augmentation size */ );
3481 int pad = -offset & (PTR_SIZE - 1);
3482
3483 size += pad;
3484 gcc_assert (size_of_uleb128 (size) == 1);
3485 }
3486
3487 dw2_asm_output_data_uleb128 (size, "Augmentation size");
3488
3489 if (fde->uses_eh_lsda)
3490 {
3491 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3492 fde->funcdef_number);
3493 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3494 gen_rtx_SYMBOL_REF (Pmode, l1),
3495 false,
3496 "Language Specific Data Area");
3497 }
3498 else
3499 {
3500 if (lsda_encoding == DW_EH_PE_aligned)
3501 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3502 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3503 "Language Specific Data Area (none)");
3504 }
3505 }
3506 else
3507 dw2_asm_output_data_uleb128 (0, "Augmentation size");
3508 }
3509
3510 /* Loop through the Call Frame Instructions associated with
3511 this FDE. */
3512 fde->dw_fde_current_label = begin;
3513 if (!fde->dw_fde_switched_sections)
3514 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3515 output_cfi (cfi, fde, for_eh);
3516 else if (!second)
3517 {
3518 if (fde->dw_fde_switch_cfi)
3519 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3520 {
3521 output_cfi (cfi, fde, for_eh);
3522 if (cfi == fde->dw_fde_switch_cfi)
3523 break;
3524 }
3525 }
3526 else
3527 {
3528 dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3529
3530 if (fde->dw_fde_switch_cfi)
3531 {
3532 cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3533 fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3534 output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3535 fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3536 }
3537 for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3538 output_cfi (cfi, fde, for_eh);
3539 }
3540
3541 /* If we are to emit a ref/link from function bodies to their frame tables,
3542 do it now. This is typically performed to make sure that tables
3543 associated with functions are dragged with them and not discarded in
3544 garbage collecting links. We need to do this on a per function basis to
3545 cope with -ffunction-sections. */
3546
3547 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3548 /* Switch to the function section, emit the ref to the tables, and
3549 switch *back* into the table section. */
3550 switch_to_section (function_section (fde->decl));
3551 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3552 switch_to_frame_table_section (for_eh, true);
3553 #endif
3554
3555 /* Pad the FDE out to an address sized boundary. */
3556 ASM_OUTPUT_ALIGN (asm_out_file,
3557 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3558 ASM_OUTPUT_LABEL (asm_out_file, l2);
3559
3560 j += 2;
3561 }
3562
3563
3564 /* Switch [BACK] to the eh or debug frame table section, depending on
3565 FOR_EH. */
3566 static void
3567 switch_to_frame_table_section (int for_eh, bool back)
3568 {
3569 if (for_eh)
3570 switch_to_eh_frame_section (back);
3571 else
3572 {
3573 if (!debug_frame_section)
3574 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3575 SECTION_DEBUG, NULL);
3576 switch_to_section (debug_frame_section);
3577 }
3578 }
3579
3580 /* Output the call frame information used to record information
3581 that relates to calculating the frame pointer, and records the
3582 location of saved registers. */
3583
3584 static void
3585 output_call_frame_info (int for_eh)
3586 {
3587 unsigned int i;
3588 dw_fde_ref fde;
3589 dw_cfi_ref cfi;
3590 char l1[20], l2[20], section_start_label[20];
3591 bool any_lsda_needed = false;
3592 char augmentation[6];
3593 int augmentation_size;
3594 int fde_encoding = DW_EH_PE_absptr;
3595 int per_encoding = DW_EH_PE_absptr;
3596 int lsda_encoding = DW_EH_PE_absptr;
3597 int return_reg;
3598 int dw_cie_version;
3599
3600 /* Don't emit a CIE if there won't be any FDEs. */
3601 if (fde_table_in_use == 0)
3602 return;
3603
3604 /* Nothing to do if the assembler's doing it all. */
3605 if (dwarf2out_do_cfi_asm ())
3606 return;
3607
3608 /* If we make FDEs linkonce, we may have to emit an empty label for
3609 an FDE that wouldn't otherwise be emitted. We want to avoid
3610 having an FDE kept around when the function it refers to is
3611 discarded. Example where this matters: a primary function
3612 template in C++ requires EH information, but an explicit
3613 specialization doesn't. */
3614 if (TARGET_USES_WEAK_UNWIND_INFO
3615 && ! flag_asynchronous_unwind_tables
3616 && flag_exceptions
3617 && for_eh)
3618 for (i = 0; i < fde_table_in_use; i++)
3619 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3620 && !fde_table[i].uses_eh_lsda
3621 && ! DECL_WEAK (fde_table[i].decl))
3622 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3623 for_eh, /* empty */ 1);
3624
3625 /* If we don't have any functions we'll want to unwind out of, don't
3626 emit any EH unwind information. Note that if exceptions aren't
3627 enabled, we won't have collected nothrow information, and if we
3628 asked for asynchronous tables, we always want this info. */
3629 if (for_eh)
3630 {
3631 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3632
3633 for (i = 0; i < fde_table_in_use; i++)
3634 if (fde_table[i].uses_eh_lsda)
3635 any_eh_needed = any_lsda_needed = true;
3636 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3637 any_eh_needed = true;
3638 else if (! fde_table[i].nothrow
3639 && ! fde_table[i].all_throwers_are_sibcalls)
3640 any_eh_needed = true;
3641
3642 if (! any_eh_needed)
3643 return;
3644 }
3645
3646 /* We're going to be generating comments, so turn on app. */
3647 if (flag_debug_asm)
3648 app_enable ();
3649
3650 /* Switch to the proper frame section, first time. */
3651 switch_to_frame_table_section (for_eh, false);
3652
3653 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3654 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3655
3656 /* Output the CIE. */
3657 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3658 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3659 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3660 dw2_asm_output_data (4, 0xffffffff,
3661 "Initial length escape value indicating 64-bit DWARF extension");
3662 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3663 "Length of Common Information Entry");
3664 ASM_OUTPUT_LABEL (asm_out_file, l1);
3665
3666 /* Now that the CIE pointer is PC-relative for EH,
3667 use 0 to identify the CIE. */
3668 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3669 (for_eh ? 0 : DWARF_CIE_ID),
3670 "CIE Identifier Tag");
3671
3672 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3673 use CIE version 1, unless that would produce incorrect results
3674 due to overflowing the return register column. */
3675 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3676 dw_cie_version = 1;
3677 if (return_reg >= 256 || dwarf_version > 2)
3678 dw_cie_version = 3;
3679 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3680
3681 augmentation[0] = 0;
3682 augmentation_size = 0;
3683 if (for_eh)
3684 {
3685 char *p;
3686
3687 /* Augmentation:
3688 z Indicates that a uleb128 is present to size the
3689 augmentation section.
3690 L Indicates the encoding (and thus presence) of
3691 an LSDA pointer in the FDE augmentation.
3692 R Indicates a non-default pointer encoding for
3693 FDE code pointers.
3694 P Indicates the presence of an encoding + language
3695 personality routine in the CIE augmentation. */
3696
3697 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3698 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3699 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3700
3701 p = augmentation + 1;
3702 if (eh_personality_libfunc)
3703 {
3704 *p++ = 'P';
3705 augmentation_size += 1 + size_of_encoded_value (per_encoding);
3706 assemble_external_libcall (eh_personality_libfunc);
3707 }
3708 if (any_lsda_needed)
3709 {
3710 *p++ = 'L';
3711 augmentation_size += 1;
3712 }
3713 if (fde_encoding != DW_EH_PE_absptr)
3714 {
3715 *p++ = 'R';
3716 augmentation_size += 1;
3717 }
3718 if (p > augmentation + 1)
3719 {
3720 augmentation[0] = 'z';
3721 *p = '\0';
3722 }
3723
3724 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
3725 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3726 {
3727 int offset = ( 4 /* Length */
3728 + 4 /* CIE Id */
3729 + 1 /* CIE version */
3730 + strlen (augmentation) + 1 /* Augmentation */
3731 + size_of_uleb128 (1) /* Code alignment */
3732 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3733 + 1 /* RA column */
3734 + 1 /* Augmentation size */
3735 + 1 /* Personality encoding */ );
3736 int pad = -offset & (PTR_SIZE - 1);
3737
3738 augmentation_size += pad;
3739
3740 /* Augmentations should be small, so there's scarce need to
3741 iterate for a solution. Die if we exceed one uleb128 byte. */
3742 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3743 }
3744 }
3745
3746 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3747 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3748 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3749 "CIE Data Alignment Factor");
3750
3751 if (dw_cie_version == 1)
3752 dw2_asm_output_data (1, return_reg, "CIE RA Column");
3753 else
3754 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3755
3756 if (augmentation[0])
3757 {
3758 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3759 if (eh_personality_libfunc)
3760 {
3761 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3762 eh_data_format_name (per_encoding));
3763 dw2_asm_output_encoded_addr_rtx (per_encoding,
3764 eh_personality_libfunc,
3765 true, NULL);
3766 }
3767
3768 if (any_lsda_needed)
3769 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3770 eh_data_format_name (lsda_encoding));
3771
3772 if (fde_encoding != DW_EH_PE_absptr)
3773 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3774 eh_data_format_name (fde_encoding));
3775 }
3776
3777 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3778 output_cfi (cfi, NULL, for_eh);
3779
3780 /* Pad the CIE out to an address sized boundary. */
3781 ASM_OUTPUT_ALIGN (asm_out_file,
3782 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3783 ASM_OUTPUT_LABEL (asm_out_file, l2);
3784
3785 /* Loop through all of the FDE's. */
3786 for (i = 0; i < fde_table_in_use; i++)
3787 {
3788 unsigned int k;
3789 fde = &fde_table[i];
3790
3791 /* Don't emit EH unwind info for leaf functions that don't need it. */
3792 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3793 && (fde->nothrow || fde->all_throwers_are_sibcalls)
3794 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3795 && !fde->uses_eh_lsda)
3796 continue;
3797
3798 for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3799 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3800 augmentation, any_lsda_needed, lsda_encoding);
3801 }
3802
3803 if (for_eh && targetm.terminate_dw2_eh_frame_info)
3804 dw2_asm_output_data (4, 0, "End of Table");
3805 #ifdef MIPS_DEBUGGING_INFO
3806 /* Work around Irix 6 assembler bug whereby labels at the end of a section
3807 get a value of 0. Putting .align 0 after the label fixes it. */
3808 ASM_OUTPUT_ALIGN (asm_out_file, 0);
3809 #endif
3810
3811 /* Turn off app to make assembly quicker. */
3812 if (flag_debug_asm)
3813 app_disable ();
3814 }
3815
3816 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
3817
3818 static void
3819 dwarf2out_do_cfi_startproc (bool second)
3820 {
3821 int enc;
3822 rtx ref;
3823
3824 fprintf (asm_out_file, "\t.cfi_startproc\n");
3825
3826 if (eh_personality_libfunc)
3827 {
3828 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3829 ref = eh_personality_libfunc;
3830
3831 /* ??? The GAS support isn't entirely consistent. We have to
3832 handle indirect support ourselves, but PC-relative is done
3833 in the assembler. Further, the assembler can't handle any
3834 of the weirder relocation types. */
3835 if (enc & DW_EH_PE_indirect)
3836 ref = dw2_force_const_mem (ref, true);
3837
3838 fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3839 output_addr_const (asm_out_file, ref);
3840 fputc ('\n', asm_out_file);
3841 }
3842
3843 if (crtl->uses_eh_lsda)
3844 {
3845 char lab[20];
3846
3847 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3848 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3849 current_function_funcdef_no);
3850 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3851 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3852
3853 if (enc & DW_EH_PE_indirect)
3854 ref = dw2_force_const_mem (ref, true);
3855
3856 fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3857 output_addr_const (asm_out_file, ref);
3858 fputc ('\n', asm_out_file);
3859 }
3860 }
3861
3862 /* Output a marker (i.e. a label) for the beginning of a function, before
3863 the prologue. */
3864
3865 void
3866 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3867 const char *file ATTRIBUTE_UNUSED)
3868 {
3869 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3870 char * dup_label;
3871 dw_fde_ref fde;
3872 section *fnsec;
3873
3874 current_function_func_begin_label = NULL;
3875
3876 #ifdef TARGET_UNWIND_INFO
3877 /* ??? current_function_func_begin_label is also used by except.c
3878 for call-site information. We must emit this label if it might
3879 be used. */
3880 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3881 && ! dwarf2out_do_frame ())
3882 return;
3883 #else
3884 if (! dwarf2out_do_frame ())
3885 return;
3886 #endif
3887
3888 fnsec = function_section (current_function_decl);
3889 switch_to_section (fnsec);
3890 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3891 current_function_funcdef_no);
3892 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3893 current_function_funcdef_no);
3894 dup_label = xstrdup (label);
3895 current_function_func_begin_label = dup_label;
3896
3897 #ifdef TARGET_UNWIND_INFO
3898 /* We can elide the fde allocation if we're not emitting debug info. */
3899 if (! dwarf2out_do_frame ())
3900 return;
3901 #endif
3902
3903 /* Expand the fde table if necessary. */
3904 if (fde_table_in_use == fde_table_allocated)
3905 {
3906 fde_table_allocated += FDE_TABLE_INCREMENT;
3907 fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3908 memset (fde_table + fde_table_in_use, 0,
3909 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3910 }
3911
3912 /* Record the FDE associated with this function. */
3913 current_funcdef_fde = fde_table_in_use;
3914
3915 /* Add the new FDE at the end of the fde_table. */
3916 fde = &fde_table[fde_table_in_use++];
3917 fde->decl = current_function_decl;
3918 fde->dw_fde_begin = dup_label;
3919 fde->dw_fde_current_label = dup_label;
3920 fde->dw_fde_hot_section_label = NULL;
3921 fde->dw_fde_hot_section_end_label = NULL;
3922 fde->dw_fde_unlikely_section_label = NULL;
3923 fde->dw_fde_unlikely_section_end_label = NULL;
3924 fde->dw_fde_switched_sections = 0;
3925 fde->dw_fde_switched_cold_to_hot = 0;
3926 fde->dw_fde_end = NULL;
3927 fde->dw_fde_cfi = NULL;
3928 fde->dw_fde_switch_cfi = NULL;
3929 fde->funcdef_number = current_function_funcdef_no;
3930 fde->nothrow = crtl->nothrow;
3931 fde->uses_eh_lsda = crtl->uses_eh_lsda;
3932 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3933 fde->drap_reg = INVALID_REGNUM;
3934 fde->vdrap_reg = INVALID_REGNUM;
3935 if (flag_reorder_blocks_and_partition)
3936 {
3937 section *unlikelysec;
3938 if (first_function_block_is_cold)
3939 fde->in_std_section = 1;
3940 else
3941 fde->in_std_section
3942 = (fnsec == text_section
3943 || (cold_text_section && fnsec == cold_text_section));
3944 unlikelysec = unlikely_text_section ();
3945 fde->cold_in_std_section
3946 = (unlikelysec == text_section
3947 || (cold_text_section && unlikelysec == cold_text_section));
3948 }
3949 else
3950 {
3951 fde->in_std_section
3952 = (fnsec == text_section
3953 || (cold_text_section && fnsec == cold_text_section));
3954 fde->cold_in_std_section = 0;
3955 }
3956
3957 args_size = old_args_size = 0;
3958
3959 /* We only want to output line number information for the genuine dwarf2
3960 prologue case, not the eh frame case. */
3961 #ifdef DWARF2_DEBUGGING_INFO
3962 if (file)
3963 dwarf2out_source_line (line, file, 0, true);
3964 #endif
3965
3966 if (dwarf2out_do_cfi_asm ())
3967 dwarf2out_do_cfi_startproc (false);
3968 }
3969
3970 /* Output a marker (i.e. a label) for the absolute end of the generated code
3971 for a function definition. This gets called *after* the epilogue code has
3972 been generated. */
3973
3974 void
3975 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3976 const char *file ATTRIBUTE_UNUSED)
3977 {
3978 dw_fde_ref fde;
3979 char label[MAX_ARTIFICIAL_LABEL_BYTES];
3980
3981 #ifdef DWARF2_DEBUGGING_INFO
3982 last_var_location_insn = NULL_RTX;
3983 #endif
3984
3985 if (dwarf2out_do_cfi_asm ())
3986 fprintf (asm_out_file, "\t.cfi_endproc\n");
3987
3988 /* Output a label to mark the endpoint of the code generated for this
3989 function. */
3990 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3991 current_function_funcdef_no);
3992 ASM_OUTPUT_LABEL (asm_out_file, label);
3993 fde = current_fde ();
3994 gcc_assert (fde != NULL);
3995 fde->dw_fde_end = xstrdup (label);
3996 }
3997
3998 void
3999 dwarf2out_frame_init (void)
4000 {
4001 /* Allocate the initial hunk of the fde_table. */
4002 fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4003 fde_table_allocated = FDE_TABLE_INCREMENT;
4004 fde_table_in_use = 0;
4005
4006 /* Generate the CFA instructions common to all FDE's. Do it now for the
4007 sake of lookup_cfa. */
4008
4009 /* On entry, the Canonical Frame Address is at SP. */
4010 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4011
4012 #ifdef DWARF2_UNWIND_INFO
4013 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4014 initial_return_save (INCOMING_RETURN_ADDR_RTX);
4015 #endif
4016 }
4017
4018 void
4019 dwarf2out_frame_finish (void)
4020 {
4021 /* Output call frame information. */
4022 if (DWARF2_FRAME_INFO)
4023 output_call_frame_info (0);
4024
4025 #ifndef TARGET_UNWIND_INFO
4026 /* Output another copy for the unwinder. */
4027 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4028 output_call_frame_info (1);
4029 #endif
4030 }
4031
4032 /* Note that the current function section is being used for code. */
4033
4034 static void
4035 dwarf2out_note_section_used (void)
4036 {
4037 section *sec = current_function_section ();
4038 if (sec == text_section)
4039 text_section_used = true;
4040 else if (sec == cold_text_section)
4041 cold_text_section_used = true;
4042 }
4043
4044 void
4045 dwarf2out_switch_text_section (void)
4046 {
4047 dw_fde_ref fde = current_fde ();
4048
4049 gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4050
4051 fde->dw_fde_switched_sections = 1;
4052 fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4053
4054 fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4055 fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4056 fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4057 fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4058 have_multiple_function_sections = true;
4059
4060 /* Reset the current label on switching text sections, so that we
4061 don't attempt to advance_loc4 between labels in different sections. */
4062 fde->dw_fde_current_label = NULL;
4063
4064 /* There is no need to mark used sections when not debugging. */
4065 if (cold_text_section != NULL)
4066 dwarf2out_note_section_used ();
4067
4068 if (dwarf2out_do_cfi_asm ())
4069 fprintf (asm_out_file, "\t.cfi_endproc\n");
4070
4071 /* Now do the real section switch. */
4072 switch_to_section (current_function_section ());
4073
4074 if (dwarf2out_do_cfi_asm ())
4075 {
4076 dwarf2out_do_cfi_startproc (true);
4077 /* As this is a different FDE, insert all current CFI instructions
4078 again. */
4079 output_cfis (fde->dw_fde_cfi, true, fde, true);
4080 }
4081 else
4082 {
4083 dw_cfi_ref cfi = fde->dw_fde_cfi;
4084
4085 cfi = fde->dw_fde_cfi;
4086 if (cfi)
4087 while (cfi->dw_cfi_next != NULL)
4088 cfi = cfi->dw_cfi_next;
4089 fde->dw_fde_switch_cfi = cfi;
4090 }
4091 }
4092 #endif
4093 \f
4094 /* And now, the subset of the debugging information support code necessary
4095 for emitting location expressions. */
4096
4097 /* Data about a single source file. */
4098 struct GTY(()) dwarf_file_data {
4099 const char * filename;
4100 int emitted_number;
4101 };
4102
4103 typedef struct dw_val_struct *dw_val_ref;
4104 typedef struct die_struct *dw_die_ref;
4105 typedef const struct die_struct *const_dw_die_ref;
4106 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4107 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4108
4109 typedef struct GTY(()) deferred_locations_struct
4110 {
4111 tree variable;
4112 dw_die_ref die;
4113 } deferred_locations;
4114
4115 DEF_VEC_O(deferred_locations);
4116 DEF_VEC_ALLOC_O(deferred_locations,gc);
4117
4118 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4119
4120 /* Each DIE may have a series of attribute/value pairs. Values
4121 can take on several forms. The forms that are used in this
4122 implementation are listed below. */
4123
4124 enum dw_val_class
4125 {
4126 dw_val_class_addr,
4127 dw_val_class_offset,
4128 dw_val_class_loc,
4129 dw_val_class_loc_list,
4130 dw_val_class_range_list,
4131 dw_val_class_const,
4132 dw_val_class_unsigned_const,
4133 dw_val_class_long_long,
4134 dw_val_class_vec,
4135 dw_val_class_flag,
4136 dw_val_class_die_ref,
4137 dw_val_class_fde_ref,
4138 dw_val_class_lbl_id,
4139 dw_val_class_lineptr,
4140 dw_val_class_str,
4141 dw_val_class_macptr,
4142 dw_val_class_file
4143 };
4144
4145 /* Describe a double word constant value. */
4146 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
4147
4148 typedef struct GTY(()) dw_long_long_struct {
4149 unsigned long hi;
4150 unsigned long low;
4151 }
4152 dw_long_long_const;
4153
4154 /* Describe a floating point constant value, or a vector constant value. */
4155
4156 typedef struct GTY(()) dw_vec_struct {
4157 unsigned char * GTY((length ("%h.length"))) array;
4158 unsigned length;
4159 unsigned elt_size;
4160 }
4161 dw_vec_const;
4162
4163 /* The dw_val_node describes an attribute's value, as it is
4164 represented internally. */
4165
4166 typedef struct GTY(()) dw_val_struct {
4167 enum dw_val_class val_class;
4168 union dw_val_struct_union
4169 {
4170 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4171 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4172 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4173 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4174 HOST_WIDE_INT GTY ((default)) val_int;
4175 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4176 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
4177 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4178 struct dw_val_die_union
4179 {
4180 dw_die_ref die;
4181 int external;
4182 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4183 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4184 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4185 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4186 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4187 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4188 }
4189 GTY ((desc ("%1.val_class"))) v;
4190 }
4191 dw_val_node;
4192
4193 /* Locations in memory are described using a sequence of stack machine
4194 operations. */
4195
4196 typedef struct GTY(()) dw_loc_descr_struct {
4197 dw_loc_descr_ref dw_loc_next;
4198 ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4199 /* Used to distinguish DW_OP_addr with a direct symbol relocation
4200 from DW_OP_addr with a dtp-relative symbol relocation. */
4201 unsigned int dtprel : 1;
4202 int dw_loc_addr;
4203 dw_val_node dw_loc_oprnd1;
4204 dw_val_node dw_loc_oprnd2;
4205 }
4206 dw_loc_descr_node;
4207
4208 /* Location lists are ranges + location descriptions for that range,
4209 so you can track variables that are in different places over
4210 their entire life. */
4211 typedef struct GTY(()) dw_loc_list_struct {
4212 dw_loc_list_ref dw_loc_next;
4213 const char *begin; /* Label for begin address of range */
4214 const char *end; /* Label for end address of range */
4215 char *ll_symbol; /* Label for beginning of location list.
4216 Only on head of list */
4217 const char *section; /* Section this loclist is relative to */
4218 dw_loc_descr_ref expr;
4219 } dw_loc_list_node;
4220
4221 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4222
4223 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4224
4225 /* Convert a DWARF stack opcode into its string name. */
4226
4227 static const char *
4228 dwarf_stack_op_name (unsigned int op)
4229 {
4230 switch (op)
4231 {
4232 case DW_OP_addr:
4233 return "DW_OP_addr";
4234 case DW_OP_deref:
4235 return "DW_OP_deref";
4236 case DW_OP_const1u:
4237 return "DW_OP_const1u";
4238 case DW_OP_const1s:
4239 return "DW_OP_const1s";
4240 case DW_OP_const2u:
4241 return "DW_OP_const2u";
4242 case DW_OP_const2s:
4243 return "DW_OP_const2s";
4244 case DW_OP_const4u:
4245 return "DW_OP_const4u";
4246 case DW_OP_const4s:
4247 return "DW_OP_const4s";
4248 case DW_OP_const8u:
4249 return "DW_OP_const8u";
4250 case DW_OP_const8s:
4251 return "DW_OP_const8s";
4252 case DW_OP_constu:
4253 return "DW_OP_constu";
4254 case DW_OP_consts:
4255 return "DW_OP_consts";
4256 case DW_OP_dup:
4257 return "DW_OP_dup";
4258 case DW_OP_drop:
4259 return "DW_OP_drop";
4260 case DW_OP_over:
4261 return "DW_OP_over";
4262 case DW_OP_pick:
4263 return "DW_OP_pick";
4264 case DW_OP_swap:
4265 return "DW_OP_swap";
4266 case DW_OP_rot:
4267 return "DW_OP_rot";
4268 case DW_OP_xderef:
4269 return "DW_OP_xderef";
4270 case DW_OP_abs:
4271 return "DW_OP_abs";
4272 case DW_OP_and:
4273 return "DW_OP_and";
4274 case DW_OP_div:
4275 return "DW_OP_div";
4276 case DW_OP_minus:
4277 return "DW_OP_minus";
4278 case DW_OP_mod:
4279 return "DW_OP_mod";
4280 case DW_OP_mul:
4281 return "DW_OP_mul";
4282 case DW_OP_neg:
4283 return "DW_OP_neg";
4284 case DW_OP_not:
4285 return "DW_OP_not";
4286 case DW_OP_or:
4287 return "DW_OP_or";
4288 case DW_OP_plus:
4289 return "DW_OP_plus";
4290 case DW_OP_plus_uconst:
4291 return "DW_OP_plus_uconst";
4292 case DW_OP_shl:
4293 return "DW_OP_shl";
4294 case DW_OP_shr:
4295 return "DW_OP_shr";
4296 case DW_OP_shra:
4297 return "DW_OP_shra";
4298 case DW_OP_xor:
4299 return "DW_OP_xor";
4300 case DW_OP_bra:
4301 return "DW_OP_bra";
4302 case DW_OP_eq:
4303 return "DW_OP_eq";
4304 case DW_OP_ge:
4305 return "DW_OP_ge";
4306 case DW_OP_gt:
4307 return "DW_OP_gt";
4308 case DW_OP_le:
4309 return "DW_OP_le";
4310 case DW_OP_lt:
4311 return "DW_OP_lt";
4312 case DW_OP_ne:
4313 return "DW_OP_ne";
4314 case DW_OP_skip:
4315 return "DW_OP_skip";
4316 case DW_OP_lit0:
4317 return "DW_OP_lit0";
4318 case DW_OP_lit1:
4319 return "DW_OP_lit1";
4320 case DW_OP_lit2:
4321 return "DW_OP_lit2";
4322 case DW_OP_lit3:
4323 return "DW_OP_lit3";
4324 case DW_OP_lit4:
4325 return "DW_OP_lit4";
4326 case DW_OP_lit5:
4327 return "DW_OP_lit5";
4328 case DW_OP_lit6:
4329 return "DW_OP_lit6";
4330 case DW_OP_lit7:
4331 return "DW_OP_lit7";
4332 case DW_OP_lit8:
4333 return "DW_OP_lit8";
4334 case DW_OP_lit9:
4335 return "DW_OP_lit9";
4336 case DW_OP_lit10:
4337 return "DW_OP_lit10";
4338 case DW_OP_lit11:
4339 return "DW_OP_lit11";
4340 case DW_OP_lit12:
4341 return "DW_OP_lit12";
4342 case DW_OP_lit13:
4343 return "DW_OP_lit13";
4344 case DW_OP_lit14:
4345 return "DW_OP_lit14";
4346 case DW_OP_lit15:
4347 return "DW_OP_lit15";
4348 case DW_OP_lit16:
4349 return "DW_OP_lit16";
4350 case DW_OP_lit17:
4351 return "DW_OP_lit17";
4352 case DW_OP_lit18:
4353 return "DW_OP_lit18";
4354 case DW_OP_lit19:
4355 return "DW_OP_lit19";
4356 case DW_OP_lit20:
4357 return "DW_OP_lit20";
4358 case DW_OP_lit21:
4359 return "DW_OP_lit21";
4360 case DW_OP_lit22:
4361 return "DW_OP_lit22";
4362 case DW_OP_lit23:
4363 return "DW_OP_lit23";
4364 case DW_OP_lit24:
4365 return "DW_OP_lit24";
4366 case DW_OP_lit25:
4367 return "DW_OP_lit25";
4368 case DW_OP_lit26:
4369 return "DW_OP_lit26";
4370 case DW_OP_lit27:
4371 return "DW_OP_lit27";
4372 case DW_OP_lit28:
4373 return "DW_OP_lit28";
4374 case DW_OP_lit29:
4375 return "DW_OP_lit29";
4376 case DW_OP_lit30:
4377 return "DW_OP_lit30";
4378 case DW_OP_lit31:
4379 return "DW_OP_lit31";
4380 case DW_OP_reg0:
4381 return "DW_OP_reg0";
4382 case DW_OP_reg1:
4383 return "DW_OP_reg1";
4384 case DW_OP_reg2:
4385 return "DW_OP_reg2";
4386 case DW_OP_reg3:
4387 return "DW_OP_reg3";
4388 case DW_OP_reg4:
4389 return "DW_OP_reg4";
4390 case DW_OP_reg5:
4391 return "DW_OP_reg5";
4392 case DW_OP_reg6:
4393 return "DW_OP_reg6";
4394 case DW_OP_reg7:
4395 return "DW_OP_reg7";
4396 case DW_OP_reg8:
4397 return "DW_OP_reg8";
4398 case DW_OP_reg9:
4399 return "DW_OP_reg9";
4400 case DW_OP_reg10:
4401 return "DW_OP_reg10";
4402 case DW_OP_reg11:
4403 return "DW_OP_reg11";
4404 case DW_OP_reg12:
4405 return "DW_OP_reg12";
4406 case DW_OP_reg13:
4407 return "DW_OP_reg13";
4408 case DW_OP_reg14:
4409 return "DW_OP_reg14";
4410 case DW_OP_reg15:
4411 return "DW_OP_reg15";
4412 case DW_OP_reg16:
4413 return "DW_OP_reg16";
4414 case DW_OP_reg17:
4415 return "DW_OP_reg17";
4416 case DW_OP_reg18:
4417 return "DW_OP_reg18";
4418 case DW_OP_reg19:
4419 return "DW_OP_reg19";
4420 case DW_OP_reg20:
4421 return "DW_OP_reg20";
4422 case DW_OP_reg21:
4423 return "DW_OP_reg21";
4424 case DW_OP_reg22:
4425 return "DW_OP_reg22";
4426 case DW_OP_reg23:
4427 return "DW_OP_reg23";
4428 case DW_OP_reg24:
4429 return "DW_OP_reg24";
4430 case DW_OP_reg25:
4431 return "DW_OP_reg25";
4432 case DW_OP_reg26:
4433 return "DW_OP_reg26";
4434 case DW_OP_reg27:
4435 return "DW_OP_reg27";
4436 case DW_OP_reg28:
4437 return "DW_OP_reg28";
4438 case DW_OP_reg29:
4439 return "DW_OP_reg29";
4440 case DW_OP_reg30:
4441 return "DW_OP_reg30";
4442 case DW_OP_reg31:
4443 return "DW_OP_reg31";
4444 case DW_OP_breg0:
4445 return "DW_OP_breg0";
4446 case DW_OP_breg1:
4447 return "DW_OP_breg1";
4448 case DW_OP_breg2:
4449 return "DW_OP_breg2";
4450 case DW_OP_breg3:
4451 return "DW_OP_breg3";
4452 case DW_OP_breg4:
4453 return "DW_OP_breg4";
4454 case DW_OP_breg5:
4455 return "DW_OP_breg5";
4456 case DW_OP_breg6:
4457 return "DW_OP_breg6";
4458 case DW_OP_breg7:
4459 return "DW_OP_breg7";
4460 case DW_OP_breg8:
4461 return "DW_OP_breg8";
4462 case DW_OP_breg9:
4463 return "DW_OP_breg9";
4464 case DW_OP_breg10:
4465 return "DW_OP_breg10";
4466 case DW_OP_breg11:
4467 return "DW_OP_breg11";
4468 case DW_OP_breg12:
4469 return "DW_OP_breg12";
4470 case DW_OP_breg13:
4471 return "DW_OP_breg13";
4472 case DW_OP_breg14:
4473 return "DW_OP_breg14";
4474 case DW_OP_breg15:
4475 return "DW_OP_breg15";
4476 case DW_OP_breg16:
4477 return "DW_OP_breg16";
4478 case DW_OP_breg17:
4479 return "DW_OP_breg17";
4480 case DW_OP_breg18:
4481 return "DW_OP_breg18";
4482 case DW_OP_breg19:
4483 return "DW_OP_breg19";
4484 case DW_OP_breg20:
4485 return "DW_OP_breg20";
4486 case DW_OP_breg21:
4487 return "DW_OP_breg21";
4488 case DW_OP_breg22:
4489 return "DW_OP_breg22";
4490 case DW_OP_breg23:
4491 return "DW_OP_breg23";
4492 case DW_OP_breg24:
4493 return "DW_OP_breg24";
4494 case DW_OP_breg25:
4495 return "DW_OP_breg25";
4496 case DW_OP_breg26:
4497 return "DW_OP_breg26";
4498 case DW_OP_breg27:
4499 return "DW_OP_breg27";
4500 case DW_OP_breg28:
4501 return "DW_OP_breg28";
4502 case DW_OP_breg29:
4503 return "DW_OP_breg29";
4504 case DW_OP_breg30:
4505 return "DW_OP_breg30";
4506 case DW_OP_breg31:
4507 return "DW_OP_breg31";
4508 case DW_OP_regx:
4509 return "DW_OP_regx";
4510 case DW_OP_fbreg:
4511 return "DW_OP_fbreg";
4512 case DW_OP_bregx:
4513 return "DW_OP_bregx";
4514 case DW_OP_piece:
4515 return "DW_OP_piece";
4516 case DW_OP_deref_size:
4517 return "DW_OP_deref_size";
4518 case DW_OP_xderef_size:
4519 return "DW_OP_xderef_size";
4520 case DW_OP_nop:
4521 return "DW_OP_nop";
4522
4523 case DW_OP_push_object_address:
4524 return "DW_OP_push_object_address";
4525 case DW_OP_call2:
4526 return "DW_OP_call2";
4527 case DW_OP_call4:
4528 return "DW_OP_call4";
4529 case DW_OP_call_ref:
4530 return "DW_OP_call_ref";
4531 case DW_OP_form_tls_address:
4532 return "DW_OP_form_tls_address";
4533 case DW_OP_call_frame_cfa:
4534 return "DW_OP_call_frame_cfa";
4535 case DW_OP_bit_piece:
4536 return "DW_OP_bit_piece";
4537
4538 case DW_OP_GNU_push_tls_address:
4539 return "DW_OP_GNU_push_tls_address";
4540 case DW_OP_GNU_uninit:
4541 return "DW_OP_GNU_uninit";
4542 case DW_OP_GNU_encoded_addr:
4543 return "DW_OP_GNU_encoded_addr";
4544
4545 default:
4546 return "OP_<unknown>";
4547 }
4548 }
4549
4550 /* Return a pointer to a newly allocated location description. Location
4551 descriptions are simple expression terms that can be strung
4552 together to form more complicated location (address) descriptions. */
4553
4554 static inline dw_loc_descr_ref
4555 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4556 unsigned HOST_WIDE_INT oprnd2)
4557 {
4558 dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4559
4560 descr->dw_loc_opc = op;
4561 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4562 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4563 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4564 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4565
4566 return descr;
4567 }
4568
4569 /* Return a pointer to a newly allocated location description for
4570 REG and OFFSET. */
4571
4572 static inline dw_loc_descr_ref
4573 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
4574 {
4575 if (reg <= 31)
4576 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4577 offset, 0);
4578 else
4579 return new_loc_descr (DW_OP_bregx, reg, offset);
4580 }
4581
4582 /* Add a location description term to a location description expression. */
4583
4584 static inline void
4585 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4586 {
4587 dw_loc_descr_ref *d;
4588
4589 /* Find the end of the chain. */
4590 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4591 ;
4592
4593 *d = descr;
4594 }
4595
4596 /* Add a constant OFFSET to a location expression. */
4597
4598 static void
4599 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4600 {
4601 dw_loc_descr_ref loc;
4602 HOST_WIDE_INT *p;
4603
4604 gcc_assert (*list_head != NULL);
4605
4606 if (!offset)
4607 return;
4608
4609 /* Find the end of the chain. */
4610 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4611 ;
4612
4613 p = NULL;
4614 if (loc->dw_loc_opc == DW_OP_fbreg
4615 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4616 p = &loc->dw_loc_oprnd1.v.val_int;
4617 else if (loc->dw_loc_opc == DW_OP_bregx)
4618 p = &loc->dw_loc_oprnd2.v.val_int;
4619
4620 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4621 offset. Don't optimize if an signed integer overflow would happen. */
4622 if (p != NULL
4623 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4624 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4625 *p += offset;
4626
4627 else if (offset > 0)
4628 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4629
4630 else
4631 {
4632 loc->dw_loc_next = int_loc_descriptor (offset);
4633 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4634 }
4635 }
4636
4637 /* Return the size of a location descriptor. */
4638
4639 static unsigned long
4640 size_of_loc_descr (dw_loc_descr_ref loc)
4641 {
4642 unsigned long size = 1;
4643
4644 switch (loc->dw_loc_opc)
4645 {
4646 case DW_OP_addr:
4647 size += DWARF2_ADDR_SIZE;
4648 break;
4649 case DW_OP_const1u:
4650 case DW_OP_const1s:
4651 size += 1;
4652 break;
4653 case DW_OP_const2u:
4654 case DW_OP_const2s:
4655 size += 2;
4656 break;
4657 case DW_OP_const4u:
4658 case DW_OP_const4s:
4659 size += 4;
4660 break;
4661 case DW_OP_const8u:
4662 case DW_OP_const8s:
4663 size += 8;
4664 break;
4665 case DW_OP_constu:
4666 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4667 break;
4668 case DW_OP_consts:
4669 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4670 break;
4671 case DW_OP_pick:
4672 size += 1;
4673 break;
4674 case DW_OP_plus_uconst:
4675 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4676 break;
4677 case DW_OP_skip:
4678 case DW_OP_bra:
4679 size += 2;
4680 break;
4681 case DW_OP_breg0:
4682 case DW_OP_breg1:
4683 case DW_OP_breg2:
4684 case DW_OP_breg3:
4685 case DW_OP_breg4:
4686 case DW_OP_breg5:
4687 case DW_OP_breg6:
4688 case DW_OP_breg7:
4689 case DW_OP_breg8:
4690 case DW_OP_breg9:
4691 case DW_OP_breg10:
4692 case DW_OP_breg11:
4693 case DW_OP_breg12:
4694 case DW_OP_breg13:
4695 case DW_OP_breg14:
4696 case DW_OP_breg15:
4697 case DW_OP_breg16:
4698 case DW_OP_breg17:
4699 case DW_OP_breg18:
4700 case DW_OP_breg19:
4701 case DW_OP_breg20:
4702 case DW_OP_breg21:
4703 case DW_OP_breg22:
4704 case DW_OP_breg23:
4705 case DW_OP_breg24:
4706 case DW_OP_breg25:
4707 case DW_OP_breg26:
4708 case DW_OP_breg27:
4709 case DW_OP_breg28:
4710 case DW_OP_breg29:
4711 case DW_OP_breg30:
4712 case DW_OP_breg31:
4713 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4714 break;
4715 case DW_OP_regx:
4716 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4717 break;
4718 case DW_OP_fbreg:
4719 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4720 break;
4721 case DW_OP_bregx:
4722 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4723 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4724 break;
4725 case DW_OP_piece:
4726 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4727 break;
4728 case DW_OP_deref_size:
4729 case DW_OP_xderef_size:
4730 size += 1;
4731 break;
4732 case DW_OP_call2:
4733 size += 2;
4734 break;
4735 case DW_OP_call4:
4736 size += 4;
4737 break;
4738 case DW_OP_call_ref:
4739 size += DWARF2_ADDR_SIZE;
4740 break;
4741 default:
4742 break;
4743 }
4744
4745 return size;
4746 }
4747
4748 /* Return the size of a series of location descriptors. */
4749
4750 static unsigned long
4751 size_of_locs (dw_loc_descr_ref loc)
4752 {
4753 dw_loc_descr_ref l;
4754 unsigned long size;
4755
4756 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4757 field, to avoid writing to a PCH file. */
4758 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4759 {
4760 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4761 break;
4762 size += size_of_loc_descr (l);
4763 }
4764 if (! l)
4765 return size;
4766
4767 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4768 {
4769 l->dw_loc_addr = size;
4770 size += size_of_loc_descr (l);
4771 }
4772
4773 return size;
4774 }
4775
4776 /* Output location description stack opcode's operands (if any). */
4777
4778 static void
4779 output_loc_operands (dw_loc_descr_ref loc)
4780 {
4781 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4782 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4783
4784 switch (loc->dw_loc_opc)
4785 {
4786 #ifdef DWARF2_DEBUGGING_INFO
4787 case DW_OP_const2u:
4788 case DW_OP_const2s:
4789 dw2_asm_output_data (2, val1->v.val_int, NULL);
4790 break;
4791 case DW_OP_const4u:
4792 case DW_OP_const4s:
4793 dw2_asm_output_data (4, val1->v.val_int, NULL);
4794 break;
4795 case DW_OP_const8u:
4796 case DW_OP_const8s:
4797 gcc_assert (HOST_BITS_PER_LONG >= 64);
4798 dw2_asm_output_data (8, val1->v.val_int, NULL);
4799 break;
4800 case DW_OP_skip:
4801 case DW_OP_bra:
4802 {
4803 int offset;
4804
4805 gcc_assert (val1->val_class == dw_val_class_loc);
4806 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4807
4808 dw2_asm_output_data (2, offset, NULL);
4809 }
4810 break;
4811 #else
4812 case DW_OP_const2u:
4813 case DW_OP_const2s:
4814 case DW_OP_const4u:
4815 case DW_OP_const4s:
4816 case DW_OP_const8u:
4817 case DW_OP_const8s:
4818 case DW_OP_skip:
4819 case DW_OP_bra:
4820 /* We currently don't make any attempt to make sure these are
4821 aligned properly like we do for the main unwind info, so
4822 don't support emitting things larger than a byte if we're
4823 only doing unwinding. */
4824 gcc_unreachable ();
4825 #endif
4826 case DW_OP_const1u:
4827 case DW_OP_const1s:
4828 dw2_asm_output_data (1, val1->v.val_int, NULL);
4829 break;
4830 case DW_OP_constu:
4831 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4832 break;
4833 case DW_OP_consts:
4834 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4835 break;
4836 case DW_OP_pick:
4837 dw2_asm_output_data (1, val1->v.val_int, NULL);
4838 break;
4839 case DW_OP_plus_uconst:
4840 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4841 break;
4842 case DW_OP_breg0:
4843 case DW_OP_breg1:
4844 case DW_OP_breg2:
4845 case DW_OP_breg3:
4846 case DW_OP_breg4:
4847 case DW_OP_breg5:
4848 case DW_OP_breg6:
4849 case DW_OP_breg7:
4850 case DW_OP_breg8:
4851 case DW_OP_breg9:
4852 case DW_OP_breg10:
4853 case DW_OP_breg11:
4854 case DW_OP_breg12:
4855 case DW_OP_breg13:
4856 case DW_OP_breg14:
4857 case DW_OP_breg15:
4858 case DW_OP_breg16:
4859 case DW_OP_breg17:
4860 case DW_OP_breg18:
4861 case DW_OP_breg19:
4862 case DW_OP_breg20:
4863 case DW_OP_breg21:
4864 case DW_OP_breg22:
4865 case DW_OP_breg23:
4866 case DW_OP_breg24:
4867 case DW_OP_breg25:
4868 case DW_OP_breg26:
4869 case DW_OP_breg27:
4870 case DW_OP_breg28:
4871 case DW_OP_breg29:
4872 case DW_OP_breg30:
4873 case DW_OP_breg31:
4874 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4875 break;
4876 case DW_OP_regx:
4877 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4878 break;
4879 case DW_OP_fbreg:
4880 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4881 break;
4882 case DW_OP_bregx:
4883 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4884 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4885 break;
4886 case DW_OP_piece:
4887 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4888 break;
4889 case DW_OP_deref_size:
4890 case DW_OP_xderef_size:
4891 dw2_asm_output_data (1, val1->v.val_int, NULL);
4892 break;
4893
4894 case DW_OP_addr:
4895 if (loc->dtprel)
4896 {
4897 if (targetm.asm_out.output_dwarf_dtprel)
4898 {
4899 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4900 DWARF2_ADDR_SIZE,
4901 val1->v.val_addr);
4902 fputc ('\n', asm_out_file);
4903 }
4904 else
4905 gcc_unreachable ();
4906 }
4907 else
4908 {
4909 #ifdef DWARF2_DEBUGGING_INFO
4910 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4911 #else
4912 gcc_unreachable ();
4913 #endif
4914 }
4915 break;
4916
4917 default:
4918 /* Other codes have no operands. */
4919 break;
4920 }
4921 }
4922
4923 /* Output a sequence of location operations. */
4924
4925 static void
4926 output_loc_sequence (dw_loc_descr_ref loc)
4927 {
4928 for (; loc != NULL; loc = loc->dw_loc_next)
4929 {
4930 /* Output the opcode. */
4931 dw2_asm_output_data (1, loc->dw_loc_opc,
4932 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4933
4934 /* Output the operand(s) (if any). */
4935 output_loc_operands (loc);
4936 }
4937 }
4938
4939 /* Output location description stack opcode's operands (if any).
4940 The output is single bytes on a line, suitable for .cfi_escape. */
4941
4942 static void
4943 output_loc_operands_raw (dw_loc_descr_ref loc)
4944 {
4945 dw_val_ref val1 = &loc->dw_loc_oprnd1;
4946 dw_val_ref val2 = &loc->dw_loc_oprnd2;
4947
4948 switch (loc->dw_loc_opc)
4949 {
4950 case DW_OP_addr:
4951 /* We cannot output addresses in .cfi_escape, only bytes. */
4952 gcc_unreachable ();
4953
4954 case DW_OP_const1u:
4955 case DW_OP_const1s:
4956 case DW_OP_pick:
4957 case DW_OP_deref_size:
4958 case DW_OP_xderef_size:
4959 fputc (',', asm_out_file);
4960 dw2_asm_output_data_raw (1, val1->v.val_int);
4961 break;
4962
4963 case DW_OP_const2u:
4964 case DW_OP_const2s:
4965 fputc (',', asm_out_file);
4966 dw2_asm_output_data_raw (2, val1->v.val_int);
4967 break;
4968
4969 case DW_OP_const4u:
4970 case DW_OP_const4s:
4971 fputc (',', asm_out_file);
4972 dw2_asm_output_data_raw (4, val1->v.val_int);
4973 break;
4974
4975 case DW_OP_const8u:
4976 case DW_OP_const8s:
4977 gcc_assert (HOST_BITS_PER_LONG >= 64);
4978 fputc (',', asm_out_file);
4979 dw2_asm_output_data_raw (8, val1->v.val_int);
4980 break;
4981
4982 case DW_OP_skip:
4983 case DW_OP_bra:
4984 {
4985 int offset;
4986
4987 gcc_assert (val1->val_class == dw_val_class_loc);
4988 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4989
4990 fputc (',', asm_out_file);
4991 dw2_asm_output_data_raw (2, offset);
4992 }
4993 break;
4994
4995 case DW_OP_constu:
4996 case DW_OP_plus_uconst:
4997 case DW_OP_regx:
4998 case DW_OP_piece:
4999 fputc (',', asm_out_file);
5000 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5001 break;
5002
5003 case DW_OP_consts:
5004 case DW_OP_breg0:
5005 case DW_OP_breg1:
5006 case DW_OP_breg2:
5007 case DW_OP_breg3:
5008 case DW_OP_breg4:
5009 case DW_OP_breg5:
5010 case DW_OP_breg6:
5011 case DW_OP_breg7:
5012 case DW_OP_breg8:
5013 case DW_OP_breg9:
5014 case DW_OP_breg10:
5015 case DW_OP_breg11:
5016 case DW_OP_breg12:
5017 case DW_OP_breg13:
5018 case DW_OP_breg14:
5019 case DW_OP_breg15:
5020 case DW_OP_breg16:
5021 case DW_OP_breg17:
5022 case DW_OP_breg18:
5023 case DW_OP_breg19:
5024 case DW_OP_breg20:
5025 case DW_OP_breg21:
5026 case DW_OP_breg22:
5027 case DW_OP_breg23:
5028 case DW_OP_breg24:
5029 case DW_OP_breg25:
5030 case DW_OP_breg26:
5031 case DW_OP_breg27:
5032 case DW_OP_breg28:
5033 case DW_OP_breg29:
5034 case DW_OP_breg30:
5035 case DW_OP_breg31:
5036 case DW_OP_fbreg:
5037 fputc (',', asm_out_file);
5038 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5039 break;
5040
5041 case DW_OP_bregx:
5042 fputc (',', asm_out_file);
5043 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5044 fputc (',', asm_out_file);
5045 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5046 break;
5047
5048 default:
5049 /* Other codes have no operands. */
5050 break;
5051 }
5052 }
5053
5054 static void
5055 output_loc_sequence_raw (dw_loc_descr_ref loc)
5056 {
5057 while (1)
5058 {
5059 /* Output the opcode. */
5060 fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5061 output_loc_operands_raw (loc);
5062
5063 if (!loc->dw_loc_next)
5064 break;
5065 loc = loc->dw_loc_next;
5066
5067 fputc (',', asm_out_file);
5068 }
5069 }
5070
5071 /* This routine will generate the correct assembly data for a location
5072 description based on a cfi entry with a complex address. */
5073
5074 static void
5075 output_cfa_loc (dw_cfi_ref cfi)
5076 {
5077 dw_loc_descr_ref loc;
5078 unsigned long size;
5079
5080 if (cfi->dw_cfi_opc == DW_CFA_expression)
5081 dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5082
5083 /* Output the size of the block. */
5084 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5085 size = size_of_locs (loc);
5086 dw2_asm_output_data_uleb128 (size, NULL);
5087
5088 /* Now output the operations themselves. */
5089 output_loc_sequence (loc);
5090 }
5091
5092 /* Similar, but used for .cfi_escape. */
5093
5094 static void
5095 output_cfa_loc_raw (dw_cfi_ref cfi)
5096 {
5097 dw_loc_descr_ref loc;
5098 unsigned long size;
5099
5100 if (cfi->dw_cfi_opc == DW_CFA_expression)
5101 fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5102
5103 /* Output the size of the block. */
5104 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5105 size = size_of_locs (loc);
5106 dw2_asm_output_data_uleb128_raw (size);
5107 fputc (',', asm_out_file);
5108
5109 /* Now output the operations themselves. */
5110 output_loc_sequence_raw (loc);
5111 }
5112
5113 /* This function builds a dwarf location descriptor sequence from a
5114 dw_cfa_location, adding the given OFFSET to the result of the
5115 expression. */
5116
5117 static struct dw_loc_descr_struct *
5118 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5119 {
5120 struct dw_loc_descr_struct *head, *tmp;
5121
5122 offset += cfa->offset;
5123
5124 if (cfa->indirect)
5125 {
5126 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5127 head->dw_loc_oprnd1.val_class = dw_val_class_const;
5128 tmp = new_loc_descr (DW_OP_deref, 0, 0);
5129 add_loc_descr (&head, tmp);
5130 if (offset != 0)
5131 {
5132 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5133 add_loc_descr (&head, tmp);
5134 }
5135 }
5136 else
5137 head = new_reg_loc_descr (cfa->reg, offset);
5138
5139 return head;
5140 }
5141
5142 /* This function builds a dwarf location descriptor sequence for
5143 the address at OFFSET from the CFA when stack is aligned to
5144 ALIGNMENT byte. */
5145
5146 static struct dw_loc_descr_struct *
5147 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5148 {
5149 struct dw_loc_descr_struct *head;
5150 unsigned int dwarf_fp
5151 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5152
5153 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
5154 if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5155 {
5156 head = new_reg_loc_descr (dwarf_fp, 0);
5157 add_loc_descr (&head, int_loc_descriptor (alignment));
5158 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5159 loc_descr_plus_const (&head, offset);
5160 }
5161 else
5162 head = new_reg_loc_descr (dwarf_fp, offset);
5163 return head;
5164 }
5165
5166 /* This function fills in aa dw_cfa_location structure from a dwarf location
5167 descriptor sequence. */
5168
5169 static void
5170 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5171 {
5172 struct dw_loc_descr_struct *ptr;
5173 cfa->offset = 0;
5174 cfa->base_offset = 0;
5175 cfa->indirect = 0;
5176 cfa->reg = -1;
5177
5178 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5179 {
5180 enum dwarf_location_atom op = ptr->dw_loc_opc;
5181
5182 switch (op)
5183 {
5184 case DW_OP_reg0:
5185 case DW_OP_reg1:
5186 case DW_OP_reg2:
5187 case DW_OP_reg3:
5188 case DW_OP_reg4:
5189 case DW_OP_reg5:
5190 case DW_OP_reg6:
5191 case DW_OP_reg7:
5192 case DW_OP_reg8:
5193 case DW_OP_reg9:
5194 case DW_OP_reg10:
5195 case DW_OP_reg11:
5196 case DW_OP_reg12:
5197 case DW_OP_reg13:
5198 case DW_OP_reg14:
5199 case DW_OP_reg15:
5200 case DW_OP_reg16:
5201 case DW_OP_reg17:
5202 case DW_OP_reg18:
5203 case DW_OP_reg19:
5204 case DW_OP_reg20:
5205 case DW_OP_reg21:
5206 case DW_OP_reg22:
5207 case DW_OP_reg23:
5208 case DW_OP_reg24:
5209 case DW_OP_reg25:
5210 case DW_OP_reg26:
5211 case DW_OP_reg27:
5212 case DW_OP_reg28:
5213 case DW_OP_reg29:
5214 case DW_OP_reg30:
5215 case DW_OP_reg31:
5216 cfa->reg = op - DW_OP_reg0;
5217 break;
5218 case DW_OP_regx:
5219 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5220 break;
5221 case DW_OP_breg0:
5222 case DW_OP_breg1:
5223 case DW_OP_breg2:
5224 case DW_OP_breg3:
5225 case DW_OP_breg4:
5226 case DW_OP_breg5:
5227 case DW_OP_breg6:
5228 case DW_OP_breg7:
5229 case DW_OP_breg8:
5230 case DW_OP_breg9:
5231 case DW_OP_breg10:
5232 case DW_OP_breg11:
5233 case DW_OP_breg12:
5234 case DW_OP_breg13:
5235 case DW_OP_breg14:
5236 case DW_OP_breg15:
5237 case DW_OP_breg16:
5238 case DW_OP_breg17:
5239 case DW_OP_breg18:
5240 case DW_OP_breg19:
5241 case DW_OP_breg20:
5242 case DW_OP_breg21:
5243 case DW_OP_breg22:
5244 case DW_OP_breg23:
5245 case DW_OP_breg24:
5246 case DW_OP_breg25:
5247 case DW_OP_breg26:
5248 case DW_OP_breg27:
5249 case DW_OP_breg28:
5250 case DW_OP_breg29:
5251 case DW_OP_breg30:
5252 case DW_OP_breg31:
5253 cfa->reg = op - DW_OP_breg0;
5254 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5255 break;
5256 case DW_OP_bregx:
5257 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5258 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5259 break;
5260 case DW_OP_deref:
5261 cfa->indirect = 1;
5262 break;
5263 case DW_OP_plus_uconst:
5264 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5265 break;
5266 default:
5267 internal_error ("DW_LOC_OP %s not implemented",
5268 dwarf_stack_op_name (ptr->dw_loc_opc));
5269 }
5270 }
5271 }
5272 #endif /* .debug_frame support */
5273 \f
5274 /* And now, the support for symbolic debugging information. */
5275 #ifdef DWARF2_DEBUGGING_INFO
5276
5277 /* .debug_str support. */
5278 static int output_indirect_string (void **, void *);
5279
5280 static void dwarf2out_init (const char *);
5281 static void dwarf2out_finish (const char *);
5282 static void dwarf2out_define (unsigned int, const char *);
5283 static void dwarf2out_undef (unsigned int, const char *);
5284 static void dwarf2out_start_source_file (unsigned, const char *);
5285 static void dwarf2out_end_source_file (unsigned);
5286 static void dwarf2out_begin_block (unsigned, unsigned);
5287 static void dwarf2out_end_block (unsigned, unsigned);
5288 static bool dwarf2out_ignore_block (const_tree);
5289 static void dwarf2out_global_decl (tree);
5290 static void dwarf2out_type_decl (tree, int);
5291 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5292 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5293 dw_die_ref);
5294 static void dwarf2out_abstract_function (tree);
5295 static void dwarf2out_var_location (rtx);
5296 static void dwarf2out_begin_function (tree);
5297 static void dwarf2out_set_name (tree, tree);
5298
5299 /* The debug hooks structure. */
5300
5301 const struct gcc_debug_hooks dwarf2_debug_hooks =
5302 {
5303 dwarf2out_init,
5304 dwarf2out_finish,
5305 dwarf2out_define,
5306 dwarf2out_undef,
5307 dwarf2out_start_source_file,
5308 dwarf2out_end_source_file,
5309 dwarf2out_begin_block,
5310 dwarf2out_end_block,
5311 dwarf2out_ignore_block,
5312 dwarf2out_source_line,
5313 dwarf2out_begin_prologue,
5314 debug_nothing_int_charstar, /* end_prologue */
5315 dwarf2out_end_epilogue,
5316 dwarf2out_begin_function,
5317 debug_nothing_int, /* end_function */
5318 dwarf2out_decl, /* function_decl */
5319 dwarf2out_global_decl,
5320 dwarf2out_type_decl, /* type_decl */
5321 dwarf2out_imported_module_or_decl,
5322 debug_nothing_tree, /* deferred_inline_function */
5323 /* The DWARF 2 backend tries to reduce debugging bloat by not
5324 emitting the abstract description of inline functions until
5325 something tries to reference them. */
5326 dwarf2out_abstract_function, /* outlining_inline_function */
5327 debug_nothing_rtx, /* label */
5328 debug_nothing_int, /* handle_pch */
5329 dwarf2out_var_location,
5330 dwarf2out_switch_text_section,
5331 dwarf2out_set_name,
5332 1 /* start_end_main_source_file */
5333 };
5334 #endif
5335 \f
5336 /* NOTE: In the comments in this file, many references are made to
5337 "Debugging Information Entries". This term is abbreviated as `DIE'
5338 throughout the remainder of this file. */
5339
5340 /* An internal representation of the DWARF output is built, and then
5341 walked to generate the DWARF debugging info. The walk of the internal
5342 representation is done after the entire program has been compiled.
5343 The types below are used to describe the internal representation. */
5344
5345 /* Various DIE's use offsets relative to the beginning of the
5346 .debug_info section to refer to each other. */
5347
5348 typedef long int dw_offset;
5349
5350 /* Define typedefs here to avoid circular dependencies. */
5351
5352 typedef struct dw_attr_struct *dw_attr_ref;
5353 typedef struct dw_line_info_struct *dw_line_info_ref;
5354 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5355 typedef struct pubname_struct *pubname_ref;
5356 typedef struct dw_ranges_struct *dw_ranges_ref;
5357 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5358
5359 /* Each entry in the line_info_table maintains the file and
5360 line number associated with the label generated for that
5361 entry. The label gives the PC value associated with
5362 the line number entry. */
5363
5364 typedef struct GTY(()) dw_line_info_struct {
5365 unsigned long dw_file_num;
5366 unsigned long dw_line_num;
5367 }
5368 dw_line_info_entry;
5369
5370 /* Line information for functions in separate sections; each one gets its
5371 own sequence. */
5372 typedef struct GTY(()) dw_separate_line_info_struct {
5373 unsigned long dw_file_num;
5374 unsigned long dw_line_num;
5375 unsigned long function;
5376 }
5377 dw_separate_line_info_entry;
5378
5379 /* Each DIE attribute has a field specifying the attribute kind,
5380 a link to the next attribute in the chain, and an attribute value.
5381 Attributes are typically linked below the DIE they modify. */
5382
5383 typedef struct GTY(()) dw_attr_struct {
5384 enum dwarf_attribute dw_attr;
5385 dw_val_node dw_attr_val;
5386 }
5387 dw_attr_node;
5388
5389 DEF_VEC_O(dw_attr_node);
5390 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5391
5392 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
5393 The children of each node form a circular list linked by
5394 die_sib. die_child points to the node *before* the "first" child node. */
5395
5396 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5397 enum dwarf_tag die_tag;
5398 char *die_symbol;
5399 VEC(dw_attr_node,gc) * die_attr;
5400 dw_die_ref die_parent;
5401 dw_die_ref die_child;
5402 dw_die_ref die_sib;
5403 dw_die_ref die_definition; /* ref from a specification to its definition */
5404 dw_offset die_offset;
5405 unsigned long die_abbrev;
5406 int die_mark;
5407 /* Die is used and must not be pruned as unused. */
5408 int die_perennial_p;
5409 unsigned int decl_id;
5410 }
5411 die_node;
5412
5413 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
5414 #define FOR_EACH_CHILD(die, c, expr) do { \
5415 c = die->die_child; \
5416 if (c) do { \
5417 c = c->die_sib; \
5418 expr; \
5419 } while (c != die->die_child); \
5420 } while (0)
5421
5422 /* The pubname structure */
5423
5424 typedef struct GTY(()) pubname_struct {
5425 dw_die_ref die;
5426 const char *name;
5427 }
5428 pubname_entry;
5429
5430 DEF_VEC_O(pubname_entry);
5431 DEF_VEC_ALLOC_O(pubname_entry, gc);
5432
5433 struct GTY(()) dw_ranges_struct {
5434 /* If this is positive, it's a block number, otherwise it's a
5435 bitwise-negated index into dw_ranges_by_label. */
5436 int num;
5437 };
5438
5439 struct GTY(()) dw_ranges_by_label_struct {
5440 const char *begin;
5441 const char *end;
5442 };
5443
5444 /* The limbo die list structure. */
5445 typedef struct GTY(()) limbo_die_struct {
5446 dw_die_ref die;
5447 tree created_for;
5448 struct limbo_die_struct *next;
5449 }
5450 limbo_die_node;
5451
5452 /* How to start an assembler comment. */
5453 #ifndef ASM_COMMENT_START
5454 #define ASM_COMMENT_START ";#"
5455 #endif
5456
5457 /* Define a macro which returns nonzero for a TYPE_DECL which was
5458 implicitly generated for a tagged type.
5459
5460 Note that unlike the gcc front end (which generates a NULL named
5461 TYPE_DECL node for each complete tagged type, each array type, and
5462 each function type node created) the g++ front end generates a
5463 _named_ TYPE_DECL node for each tagged type node created.
5464 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5465 generate a DW_TAG_typedef DIE for them. */
5466
5467 #define TYPE_DECL_IS_STUB(decl) \
5468 (DECL_NAME (decl) == NULL_TREE \
5469 || (DECL_ARTIFICIAL (decl) \
5470 && is_tagged_type (TREE_TYPE (decl)) \
5471 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
5472 /* This is necessary for stub decls that \
5473 appear in nested inline functions. */ \
5474 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5475 && (decl_ultimate_origin (decl) \
5476 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5477
5478 /* Information concerning the compilation unit's programming
5479 language, and compiler version. */
5480
5481 /* Fixed size portion of the DWARF compilation unit header. */
5482 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5483 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5484
5485 /* Fixed size portion of public names info. */
5486 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5487
5488 /* Fixed size portion of the address range info. */
5489 #define DWARF_ARANGES_HEADER_SIZE \
5490 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5491 DWARF2_ADDR_SIZE * 2) \
5492 - DWARF_INITIAL_LENGTH_SIZE)
5493
5494 /* Size of padding portion in the address range info. It must be
5495 aligned to twice the pointer size. */
5496 #define DWARF_ARANGES_PAD_SIZE \
5497 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5498 DWARF2_ADDR_SIZE * 2) \
5499 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5500
5501 /* Use assembler line directives if available. */
5502 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5503 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5504 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5505 #else
5506 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5507 #endif
5508 #endif
5509
5510 /* Minimum line offset in a special line info. opcode.
5511 This value was chosen to give a reasonable range of values. */
5512 #define DWARF_LINE_BASE -10
5513
5514 /* First special line opcode - leave room for the standard opcodes. */
5515 #define DWARF_LINE_OPCODE_BASE 10
5516
5517 /* Range of line offsets in a special line info. opcode. */
5518 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
5519
5520 /* Flag that indicates the initial value of the is_stmt_start flag.
5521 In the present implementation, we do not mark any lines as
5522 the beginning of a source statement, because that information
5523 is not made available by the GCC front-end. */
5524 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5525
5526 #ifdef DWARF2_DEBUGGING_INFO
5527 /* This location is used by calc_die_sizes() to keep track
5528 the offset of each DIE within the .debug_info section. */
5529 static unsigned long next_die_offset;
5530 #endif
5531
5532 /* Record the root of the DIE's built for the current compilation unit. */
5533 static GTY(()) dw_die_ref comp_unit_die;
5534
5535 /* A list of DIEs with a NULL parent waiting to be relocated. */
5536 static GTY(()) limbo_die_node *limbo_die_list;
5537
5538 /* A list of DIEs for which we may have to generate
5539 DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5540 set. */
5541 static GTY(()) limbo_die_node *deferred_asm_name;
5542
5543 /* Filenames referenced by this compilation unit. */
5544 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5545
5546 /* A hash table of references to DIE's that describe declarations.
5547 The key is a DECL_UID() which is a unique number identifying each decl. */
5548 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5549
5550 /* A hash table of references to DIE's that describe COMMON blocks.
5551 The key is DECL_UID() ^ die_parent. */
5552 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5553
5554 /* Node of the variable location list. */
5555 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5556 rtx GTY (()) var_loc_note;
5557 const char * GTY (()) label;
5558 const char * GTY (()) section_label;
5559 struct var_loc_node * GTY (()) next;
5560 };
5561
5562 /* Variable location list. */
5563 struct GTY (()) var_loc_list_def {
5564 struct var_loc_node * GTY (()) first;
5565
5566 /* Do not mark the last element of the chained list because
5567 it is marked through the chain. */
5568 struct var_loc_node * GTY ((skip ("%h"))) last;
5569
5570 /* DECL_UID of the variable decl. */
5571 unsigned int decl_id;
5572 };
5573 typedef struct var_loc_list_def var_loc_list;
5574
5575
5576 /* Table of decl location linked lists. */
5577 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5578
5579 /* A pointer to the base of a list of references to DIE's that
5580 are uniquely identified by their tag, presence/absence of
5581 children DIE's, and list of attribute/value pairs. */
5582 static GTY((length ("abbrev_die_table_allocated")))
5583 dw_die_ref *abbrev_die_table;
5584
5585 /* Number of elements currently allocated for abbrev_die_table. */
5586 static GTY(()) unsigned abbrev_die_table_allocated;
5587
5588 /* Number of elements in type_die_table currently in use. */
5589 static GTY(()) unsigned abbrev_die_table_in_use;
5590
5591 /* Size (in elements) of increments by which we may expand the
5592 abbrev_die_table. */
5593 #define ABBREV_DIE_TABLE_INCREMENT 256
5594
5595 /* A pointer to the base of a table that contains line information
5596 for each source code line in .text in the compilation unit. */
5597 static GTY((length ("line_info_table_allocated")))
5598 dw_line_info_ref line_info_table;
5599
5600 /* Number of elements currently allocated for line_info_table. */
5601 static GTY(()) unsigned line_info_table_allocated;
5602
5603 /* Number of elements in line_info_table currently in use. */
5604 static GTY(()) unsigned line_info_table_in_use;
5605
5606 /* A pointer to the base of a table that contains line information
5607 for each source code line outside of .text in the compilation unit. */
5608 static GTY ((length ("separate_line_info_table_allocated")))
5609 dw_separate_line_info_ref separate_line_info_table;
5610
5611 /* Number of elements currently allocated for separate_line_info_table. */
5612 static GTY(()) unsigned separate_line_info_table_allocated;
5613
5614 /* Number of elements in separate_line_info_table currently in use. */
5615 static GTY(()) unsigned separate_line_info_table_in_use;
5616
5617 /* Size (in elements) of increments by which we may expand the
5618 line_info_table. */
5619 #define LINE_INFO_TABLE_INCREMENT 1024
5620
5621 /* A pointer to the base of a table that contains a list of publicly
5622 accessible names. */
5623 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
5624
5625 /* A pointer to the base of a table that contains a list of publicly
5626 accessible types. */
5627 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5628
5629 /* Array of dies for which we should generate .debug_arange info. */
5630 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5631
5632 /* Number of elements currently allocated for arange_table. */
5633 static GTY(()) unsigned arange_table_allocated;
5634
5635 /* Number of elements in arange_table currently in use. */
5636 static GTY(()) unsigned arange_table_in_use;
5637
5638 /* Size (in elements) of increments by which we may expand the
5639 arange_table. */
5640 #define ARANGE_TABLE_INCREMENT 64
5641
5642 /* Array of dies for which we should generate .debug_ranges info. */
5643 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5644
5645 /* Number of elements currently allocated for ranges_table. */
5646 static GTY(()) unsigned ranges_table_allocated;
5647
5648 /* Number of elements in ranges_table currently in use. */
5649 static GTY(()) unsigned ranges_table_in_use;
5650
5651 /* Array of pairs of labels referenced in ranges_table. */
5652 static GTY ((length ("ranges_by_label_allocated")))
5653 dw_ranges_by_label_ref ranges_by_label;
5654
5655 /* Number of elements currently allocated for ranges_by_label. */
5656 static GTY(()) unsigned ranges_by_label_allocated;
5657
5658 /* Number of elements in ranges_by_label currently in use. */
5659 static GTY(()) unsigned ranges_by_label_in_use;
5660
5661 /* Size (in elements) of increments by which we may expand the
5662 ranges_table. */
5663 #define RANGES_TABLE_INCREMENT 64
5664
5665 /* Whether we have location lists that need outputting */
5666 static GTY(()) bool have_location_lists;
5667
5668 /* Unique label counter. */
5669 static GTY(()) unsigned int loclabel_num;
5670
5671 #ifdef DWARF2_DEBUGGING_INFO
5672 /* Record whether the function being analyzed contains inlined functions. */
5673 static int current_function_has_inlines;
5674 #endif
5675 #if 0 && defined (MIPS_DEBUGGING_INFO)
5676 static int comp_unit_has_inlines;
5677 #endif
5678
5679 /* The last file entry emitted by maybe_emit_file(). */
5680 static GTY(()) struct dwarf_file_data * last_emitted_file;
5681
5682 /* Number of internal labels generated by gen_internal_sym(). */
5683 static GTY(()) int label_num;
5684
5685 /* Cached result of previous call to lookup_filename. */
5686 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5687
5688 #ifdef DWARF2_DEBUGGING_INFO
5689
5690 /* Offset from the "steady-state frame pointer" to the frame base,
5691 within the current function. */
5692 static HOST_WIDE_INT frame_pointer_fb_offset;
5693
5694 /* Forward declarations for functions defined in this file. */
5695
5696 static int is_pseudo_reg (const_rtx);
5697 static tree type_main_variant (tree);
5698 static int is_tagged_type (const_tree);
5699 static const char *dwarf_tag_name (unsigned);
5700 static const char *dwarf_attr_name (unsigned);
5701 static const char *dwarf_form_name (unsigned);
5702 static tree decl_ultimate_origin (const_tree);
5703 static tree decl_class_context (tree);
5704 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5705 static inline enum dw_val_class AT_class (dw_attr_ref);
5706 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5707 static inline unsigned AT_flag (dw_attr_ref);
5708 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5709 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5710 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5711 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5712 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5713 unsigned long);
5714 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5715 unsigned int, unsigned char *);
5716 static hashval_t debug_str_do_hash (const void *);
5717 static int debug_str_eq (const void *, const void *);
5718 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5719 static inline const char *AT_string (dw_attr_ref);
5720 static enum dwarf_form AT_string_form (dw_attr_ref);
5721 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5722 static void add_AT_specification (dw_die_ref, dw_die_ref);
5723 static inline dw_die_ref AT_ref (dw_attr_ref);
5724 static inline int AT_ref_external (dw_attr_ref);
5725 static inline void set_AT_ref_external (dw_attr_ref, int);
5726 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5727 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5728 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5729 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5730 dw_loc_list_ref);
5731 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5732 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5733 static inline rtx AT_addr (dw_attr_ref);
5734 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5735 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5736 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5737 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5738 unsigned HOST_WIDE_INT);
5739 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5740 unsigned long);
5741 static inline const char *AT_lbl (dw_attr_ref);
5742 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5743 static const char *get_AT_low_pc (dw_die_ref);
5744 static const char *get_AT_hi_pc (dw_die_ref);
5745 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5746 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5747 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5748 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5749 static bool is_c_family (void);
5750 static bool is_cxx (void);
5751 static bool is_java (void);
5752 static bool is_fortran (void);
5753 static bool is_ada (void);
5754 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5755 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5756 static void add_child_die (dw_die_ref, dw_die_ref);
5757 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5758 static dw_die_ref lookup_type_die (tree);
5759 static void equate_type_number_to_die (tree, dw_die_ref);
5760 static hashval_t decl_die_table_hash (const void *);
5761 static int decl_die_table_eq (const void *, const void *);
5762 static dw_die_ref lookup_decl_die (tree);
5763 static hashval_t common_block_die_table_hash (const void *);
5764 static int common_block_die_table_eq (const void *, const void *);
5765 static hashval_t decl_loc_table_hash (const void *);
5766 static int decl_loc_table_eq (const void *, const void *);
5767 static var_loc_list *lookup_decl_loc (const_tree);
5768 static void equate_decl_number_to_die (tree, dw_die_ref);
5769 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5770 static void print_spaces (FILE *);
5771 static void print_die (dw_die_ref, FILE *);
5772 static void print_dwarf_line_table (FILE *);
5773 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5774 static dw_die_ref pop_compile_unit (dw_die_ref);
5775 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5776 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5777 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5778 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5779 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5780 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5781 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5782 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5783 static void compute_section_prefix (dw_die_ref);
5784 static int is_type_die (dw_die_ref);
5785 static int is_comdat_die (dw_die_ref);
5786 static int is_symbol_die (dw_die_ref);
5787 static void assign_symbol_names (dw_die_ref);
5788 static void break_out_includes (dw_die_ref);
5789 static hashval_t htab_cu_hash (const void *);
5790 static int htab_cu_eq (const void *, const void *);
5791 static void htab_cu_del (void *);
5792 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5793 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5794 static void add_sibling_attributes (dw_die_ref);
5795 static void build_abbrev_table (dw_die_ref);
5796 static void output_location_lists (dw_die_ref);
5797 static int constant_size (unsigned HOST_WIDE_INT);
5798 static unsigned long size_of_die (dw_die_ref);
5799 static void calc_die_sizes (dw_die_ref);
5800 static void mark_dies (dw_die_ref);
5801 static void unmark_dies (dw_die_ref);
5802 static void unmark_all_dies (dw_die_ref);
5803 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5804 static unsigned long size_of_aranges (void);
5805 static enum dwarf_form value_format (dw_attr_ref);
5806 static void output_value_format (dw_attr_ref);
5807 static void output_abbrev_section (void);
5808 static void output_die_symbol (dw_die_ref);
5809 static void output_die (dw_die_ref);
5810 static void output_compilation_unit_header (void);
5811 static void output_comp_unit (dw_die_ref, int);
5812 static const char *dwarf2_name (tree, int);
5813 static void add_pubname (tree, dw_die_ref);
5814 static void add_pubname_string (const char *, dw_die_ref);
5815 static void add_pubtype (tree, dw_die_ref);
5816 static void output_pubnames (VEC (pubname_entry,gc) *);
5817 static void add_arange (tree, dw_die_ref);
5818 static void output_aranges (void);
5819 static unsigned int add_ranges_num (int);
5820 static unsigned int add_ranges (const_tree);
5821 static unsigned int add_ranges_by_labels (const char *, const char *);
5822 static void output_ranges (void);
5823 static void output_line_info (void);
5824 static void output_file_names (void);
5825 static dw_die_ref base_type_die (tree);
5826 static int is_base_type (tree);
5827 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5828 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5829 static int type_is_enum (const_tree);
5830 static unsigned int dbx_reg_number (const_rtx);
5831 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5832 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5833 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5834 enum var_init_status);
5835 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5836 enum var_init_status);
5837 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5838 enum var_init_status);
5839 static int is_based_loc (const_rtx);
5840 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5841 enum var_init_status);
5842 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5843 enum var_init_status);
5844 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5845 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5846 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5847 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5848 static tree field_type (const_tree);
5849 static unsigned int simple_type_align_in_bits (const_tree);
5850 static unsigned int simple_decl_align_in_bits (const_tree);
5851 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5852 static HOST_WIDE_INT field_byte_offset (const_tree);
5853 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5854 dw_loc_descr_ref);
5855 static void add_data_member_location_attribute (dw_die_ref, tree);
5856 static void add_const_value_attribute (dw_die_ref, rtx);
5857 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5858 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5859 static void insert_float (const_rtx, unsigned char *);
5860 static rtx rtl_for_decl_location (tree);
5861 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5862 enum dwarf_attribute);
5863 static void tree_add_const_value_attribute (dw_die_ref, tree);
5864 static void add_name_attribute (dw_die_ref, const char *);
5865 static void add_comp_dir_attribute (dw_die_ref);
5866 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5867 static void add_subscript_info (dw_die_ref, tree, bool);
5868 static void add_byte_size_attribute (dw_die_ref, tree);
5869 static void add_bit_offset_attribute (dw_die_ref, tree);
5870 static void add_bit_size_attribute (dw_die_ref, tree);
5871 static void add_prototyped_attribute (dw_die_ref, tree);
5872 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5873 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5874 static void add_src_coords_attributes (dw_die_ref, tree);
5875 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5876 static void push_decl_scope (tree);
5877 static void pop_decl_scope (void);
5878 static dw_die_ref scope_die_for (tree, dw_die_ref);
5879 static inline int local_scope_p (dw_die_ref);
5880 static inline int class_scope_p (dw_die_ref);
5881 static inline int class_or_namespace_scope_p (dw_die_ref);
5882 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5883 static void add_calling_convention_attribute (dw_die_ref, tree);
5884 static const char *type_tag (const_tree);
5885 static tree member_declared_type (const_tree);
5886 #if 0
5887 static const char *decl_start_label (tree);
5888 #endif
5889 static void gen_array_type_die (tree, dw_die_ref);
5890 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5891 #if 0
5892 static void gen_entry_point_die (tree, dw_die_ref);
5893 #endif
5894 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5895 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5896 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5897 static void gen_formal_types_die (tree, dw_die_ref);
5898 static void gen_subprogram_die (tree, dw_die_ref);
5899 static void gen_variable_die (tree, tree, dw_die_ref);
5900 static void gen_const_die (tree, dw_die_ref);
5901 static void gen_label_die (tree, dw_die_ref);
5902 static void gen_lexical_block_die (tree, dw_die_ref, int);
5903 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5904 static void gen_field_die (tree, dw_die_ref);
5905 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5906 static dw_die_ref gen_compile_unit_die (const char *);
5907 static void gen_inheritance_die (tree, tree, dw_die_ref);
5908 static void gen_member_die (tree, dw_die_ref);
5909 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5910 enum debug_info_usage);
5911 static void gen_subroutine_type_die (tree, dw_die_ref);
5912 static void gen_typedef_die (tree, dw_die_ref);
5913 static void gen_type_die (tree, dw_die_ref);
5914 static void gen_block_die (tree, dw_die_ref, int);
5915 static void decls_for_scope (tree, dw_die_ref, int);
5916 static int is_redundant_typedef (const_tree);
5917 static inline dw_die_ref get_context_die (tree);
5918 static void gen_namespace_die (tree, dw_die_ref);
5919 static void gen_decl_die (tree, tree, dw_die_ref);
5920 static dw_die_ref force_decl_die (tree);
5921 static dw_die_ref force_type_die (tree);
5922 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5923 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5924 static struct dwarf_file_data * lookup_filename (const char *);
5925 static void retry_incomplete_types (void);
5926 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5927 static void splice_child_die (dw_die_ref, dw_die_ref);
5928 static int file_info_cmp (const void *, const void *);
5929 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5930 const char *, const char *, unsigned);
5931 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5932 const char *, const char *,
5933 const char *);
5934 static void output_loc_list (dw_loc_list_ref);
5935 static char *gen_internal_sym (const char *);
5936
5937 static void prune_unmark_dies (dw_die_ref);
5938 static void prune_unused_types_mark (dw_die_ref, int);
5939 static void prune_unused_types_walk (dw_die_ref);
5940 static void prune_unused_types_walk_attribs (dw_die_ref);
5941 static void prune_unused_types_prune (dw_die_ref);
5942 static void prune_unused_types (void);
5943 static int maybe_emit_file (struct dwarf_file_data *fd);
5944
5945 /* Section names used to hold DWARF debugging information. */
5946 #ifndef DEBUG_INFO_SECTION
5947 #define DEBUG_INFO_SECTION ".debug_info"
5948 #endif
5949 #ifndef DEBUG_ABBREV_SECTION
5950 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
5951 #endif
5952 #ifndef DEBUG_ARANGES_SECTION
5953 #define DEBUG_ARANGES_SECTION ".debug_aranges"
5954 #endif
5955 #ifndef DEBUG_MACINFO_SECTION
5956 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
5957 #endif
5958 #ifndef DEBUG_LINE_SECTION
5959 #define DEBUG_LINE_SECTION ".debug_line"
5960 #endif
5961 #ifndef DEBUG_LOC_SECTION
5962 #define DEBUG_LOC_SECTION ".debug_loc"
5963 #endif
5964 #ifndef DEBUG_PUBNAMES_SECTION
5965 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
5966 #endif
5967 #ifndef DEBUG_PUBTYPES_SECTION
5968 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
5969 #endif
5970 #ifndef DEBUG_STR_SECTION
5971 #define DEBUG_STR_SECTION ".debug_str"
5972 #endif
5973 #ifndef DEBUG_RANGES_SECTION
5974 #define DEBUG_RANGES_SECTION ".debug_ranges"
5975 #endif
5976
5977 /* Standard ELF section names for compiled code and data. */
5978 #ifndef TEXT_SECTION_NAME
5979 #define TEXT_SECTION_NAME ".text"
5980 #endif
5981
5982 /* Section flags for .debug_str section. */
5983 #define DEBUG_STR_SECTION_FLAGS \
5984 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
5985 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
5986 : SECTION_DEBUG)
5987
5988 /* Labels we insert at beginning sections we can reference instead of
5989 the section names themselves. */
5990
5991 #ifndef TEXT_SECTION_LABEL
5992 #define TEXT_SECTION_LABEL "Ltext"
5993 #endif
5994 #ifndef COLD_TEXT_SECTION_LABEL
5995 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
5996 #endif
5997 #ifndef DEBUG_LINE_SECTION_LABEL
5998 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
5999 #endif
6000 #ifndef DEBUG_INFO_SECTION_LABEL
6001 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
6002 #endif
6003 #ifndef DEBUG_ABBREV_SECTION_LABEL
6004 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
6005 #endif
6006 #ifndef DEBUG_LOC_SECTION_LABEL
6007 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
6008 #endif
6009 #ifndef DEBUG_RANGES_SECTION_LABEL
6010 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
6011 #endif
6012 #ifndef DEBUG_MACINFO_SECTION_LABEL
6013 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
6014 #endif
6015
6016 /* Definitions of defaults for formats and names of various special
6017 (artificial) labels which may be generated within this file (when the -g
6018 options is used and DWARF2_DEBUGGING_INFO is in effect.
6019 If necessary, these may be overridden from within the tm.h file, but
6020 typically, overriding these defaults is unnecessary. */
6021
6022 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6023 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6024 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6025 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6026 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6027 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6028 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6029 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6030 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6031 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6032
6033 #ifndef TEXT_END_LABEL
6034 #define TEXT_END_LABEL "Letext"
6035 #endif
6036 #ifndef COLD_END_LABEL
6037 #define COLD_END_LABEL "Letext_cold"
6038 #endif
6039 #ifndef BLOCK_BEGIN_LABEL
6040 #define BLOCK_BEGIN_LABEL "LBB"
6041 #endif
6042 #ifndef BLOCK_END_LABEL
6043 #define BLOCK_END_LABEL "LBE"
6044 #endif
6045 #ifndef LINE_CODE_LABEL
6046 #define LINE_CODE_LABEL "LM"
6047 #endif
6048 #ifndef SEPARATE_LINE_CODE_LABEL
6049 #define SEPARATE_LINE_CODE_LABEL "LSM"
6050 #endif
6051
6052 \f
6053 /* We allow a language front-end to designate a function that is to be
6054 called to "demangle" any name before it is put into a DIE. */
6055
6056 static const char *(*demangle_name_func) (const char *);
6057
6058 void
6059 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6060 {
6061 demangle_name_func = func;
6062 }
6063
6064 /* Test if rtl node points to a pseudo register. */
6065
6066 static inline int
6067 is_pseudo_reg (const_rtx rtl)
6068 {
6069 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6070 || (GET_CODE (rtl) == SUBREG
6071 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6072 }
6073
6074 /* Return a reference to a type, with its const and volatile qualifiers
6075 removed. */
6076
6077 static inline tree
6078 type_main_variant (tree type)
6079 {
6080 type = TYPE_MAIN_VARIANT (type);
6081
6082 /* ??? There really should be only one main variant among any group of
6083 variants of a given type (and all of the MAIN_VARIANT values for all
6084 members of the group should point to that one type) but sometimes the C
6085 front-end messes this up for array types, so we work around that bug
6086 here. */
6087 if (TREE_CODE (type) == ARRAY_TYPE)
6088 while (type != TYPE_MAIN_VARIANT (type))
6089 type = TYPE_MAIN_VARIANT (type);
6090
6091 return type;
6092 }
6093
6094 /* Return nonzero if the given type node represents a tagged type. */
6095
6096 static inline int
6097 is_tagged_type (const_tree type)
6098 {
6099 enum tree_code code = TREE_CODE (type);
6100
6101 return (code == RECORD_TYPE || code == UNION_TYPE
6102 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6103 }
6104
6105 /* Convert a DIE tag into its string name. */
6106
6107 static const char *
6108 dwarf_tag_name (unsigned int tag)
6109 {
6110 switch (tag)
6111 {
6112 case DW_TAG_padding:
6113 return "DW_TAG_padding";
6114 case DW_TAG_array_type:
6115 return "DW_TAG_array_type";
6116 case DW_TAG_class_type:
6117 return "DW_TAG_class_type";
6118 case DW_TAG_entry_point:
6119 return "DW_TAG_entry_point";
6120 case DW_TAG_enumeration_type:
6121 return "DW_TAG_enumeration_type";
6122 case DW_TAG_formal_parameter:
6123 return "DW_TAG_formal_parameter";
6124 case DW_TAG_imported_declaration:
6125 return "DW_TAG_imported_declaration";
6126 case DW_TAG_label:
6127 return "DW_TAG_label";
6128 case DW_TAG_lexical_block:
6129 return "DW_TAG_lexical_block";
6130 case DW_TAG_member:
6131 return "DW_TAG_member";
6132 case DW_TAG_pointer_type:
6133 return "DW_TAG_pointer_type";
6134 case DW_TAG_reference_type:
6135 return "DW_TAG_reference_type";
6136 case DW_TAG_compile_unit:
6137 return "DW_TAG_compile_unit";
6138 case DW_TAG_string_type:
6139 return "DW_TAG_string_type";
6140 case DW_TAG_structure_type:
6141 return "DW_TAG_structure_type";
6142 case DW_TAG_subroutine_type:
6143 return "DW_TAG_subroutine_type";
6144 case DW_TAG_typedef:
6145 return "DW_TAG_typedef";
6146 case DW_TAG_union_type:
6147 return "DW_TAG_union_type";
6148 case DW_TAG_unspecified_parameters:
6149 return "DW_TAG_unspecified_parameters";
6150 case DW_TAG_variant:
6151 return "DW_TAG_variant";
6152 case DW_TAG_common_block:
6153 return "DW_TAG_common_block";
6154 case DW_TAG_common_inclusion:
6155 return "DW_TAG_common_inclusion";
6156 case DW_TAG_inheritance:
6157 return "DW_TAG_inheritance";
6158 case DW_TAG_inlined_subroutine:
6159 return "DW_TAG_inlined_subroutine";
6160 case DW_TAG_module:
6161 return "DW_TAG_module";
6162 case DW_TAG_ptr_to_member_type:
6163 return "DW_TAG_ptr_to_member_type";
6164 case DW_TAG_set_type:
6165 return "DW_TAG_set_type";
6166 case DW_TAG_subrange_type:
6167 return "DW_TAG_subrange_type";
6168 case DW_TAG_with_stmt:
6169 return "DW_TAG_with_stmt";
6170 case DW_TAG_access_declaration:
6171 return "DW_TAG_access_declaration";
6172 case DW_TAG_base_type:
6173 return "DW_TAG_base_type";
6174 case DW_TAG_catch_block:
6175 return "DW_TAG_catch_block";
6176 case DW_TAG_const_type:
6177 return "DW_TAG_const_type";
6178 case DW_TAG_constant:
6179 return "DW_TAG_constant";
6180 case DW_TAG_enumerator:
6181 return "DW_TAG_enumerator";
6182 case DW_TAG_file_type:
6183 return "DW_TAG_file_type";
6184 case DW_TAG_friend:
6185 return "DW_TAG_friend";
6186 case DW_TAG_namelist:
6187 return "DW_TAG_namelist";
6188 case DW_TAG_namelist_item:
6189 return "DW_TAG_namelist_item";
6190 case DW_TAG_packed_type:
6191 return "DW_TAG_packed_type";
6192 case DW_TAG_subprogram:
6193 return "DW_TAG_subprogram";
6194 case DW_TAG_template_type_param:
6195 return "DW_TAG_template_type_param";
6196 case DW_TAG_template_value_param:
6197 return "DW_TAG_template_value_param";
6198 case DW_TAG_thrown_type:
6199 return "DW_TAG_thrown_type";
6200 case DW_TAG_try_block:
6201 return "DW_TAG_try_block";
6202 case DW_TAG_variant_part:
6203 return "DW_TAG_variant_part";
6204 case DW_TAG_variable:
6205 return "DW_TAG_variable";
6206 case DW_TAG_volatile_type:
6207 return "DW_TAG_volatile_type";
6208 case DW_TAG_dwarf_procedure:
6209 return "DW_TAG_dwarf_procedure";
6210 case DW_TAG_restrict_type:
6211 return "DW_TAG_restrict_type";
6212 case DW_TAG_interface_type:
6213 return "DW_TAG_interface_type";
6214 case DW_TAG_namespace:
6215 return "DW_TAG_namespace";
6216 case DW_TAG_imported_module:
6217 return "DW_TAG_imported_module";
6218 case DW_TAG_unspecified_type:
6219 return "DW_TAG_unspecified_type";
6220 case DW_TAG_partial_unit:
6221 return "DW_TAG_partial_unit";
6222 case DW_TAG_imported_unit:
6223 return "DW_TAG_imported_unit";
6224 case DW_TAG_condition:
6225 return "DW_TAG_condition";
6226 case DW_TAG_shared_type:
6227 return "DW_TAG_shared_type";
6228 case DW_TAG_MIPS_loop:
6229 return "DW_TAG_MIPS_loop";
6230 case DW_TAG_format_label:
6231 return "DW_TAG_format_label";
6232 case DW_TAG_function_template:
6233 return "DW_TAG_function_template";
6234 case DW_TAG_class_template:
6235 return "DW_TAG_class_template";
6236 case DW_TAG_GNU_BINCL:
6237 return "DW_TAG_GNU_BINCL";
6238 case DW_TAG_GNU_EINCL:
6239 return "DW_TAG_GNU_EINCL";
6240 default:
6241 return "DW_TAG_<unknown>";
6242 }
6243 }
6244
6245 /* Convert a DWARF attribute code into its string name. */
6246
6247 static const char *
6248 dwarf_attr_name (unsigned int attr)
6249 {
6250 switch (attr)
6251 {
6252 case DW_AT_sibling:
6253 return "DW_AT_sibling";
6254 case DW_AT_location:
6255 return "DW_AT_location";
6256 case DW_AT_name:
6257 return "DW_AT_name";
6258 case DW_AT_ordering:
6259 return "DW_AT_ordering";
6260 case DW_AT_subscr_data:
6261 return "DW_AT_subscr_data";
6262 case DW_AT_byte_size:
6263 return "DW_AT_byte_size";
6264 case DW_AT_bit_offset:
6265 return "DW_AT_bit_offset";
6266 case DW_AT_bit_size:
6267 return "DW_AT_bit_size";
6268 case DW_AT_element_list:
6269 return "DW_AT_element_list";
6270 case DW_AT_stmt_list:
6271 return "DW_AT_stmt_list";
6272 case DW_AT_low_pc:
6273 return "DW_AT_low_pc";
6274 case DW_AT_high_pc:
6275 return "DW_AT_high_pc";
6276 case DW_AT_language:
6277 return "DW_AT_language";
6278 case DW_AT_member:
6279 return "DW_AT_member";
6280 case DW_AT_discr:
6281 return "DW_AT_discr";
6282 case DW_AT_discr_value:
6283 return "DW_AT_discr_value";
6284 case DW_AT_visibility:
6285 return "DW_AT_visibility";
6286 case DW_AT_import:
6287 return "DW_AT_import";
6288 case DW_AT_string_length:
6289 return "DW_AT_string_length";
6290 case DW_AT_common_reference:
6291 return "DW_AT_common_reference";
6292 case DW_AT_comp_dir:
6293 return "DW_AT_comp_dir";
6294 case DW_AT_const_value:
6295 return "DW_AT_const_value";
6296 case DW_AT_containing_type:
6297 return "DW_AT_containing_type";
6298 case DW_AT_default_value:
6299 return "DW_AT_default_value";
6300 case DW_AT_inline:
6301 return "DW_AT_inline";
6302 case DW_AT_is_optional:
6303 return "DW_AT_is_optional";
6304 case DW_AT_lower_bound:
6305 return "DW_AT_lower_bound";
6306 case DW_AT_producer:
6307 return "DW_AT_producer";
6308 case DW_AT_prototyped:
6309 return "DW_AT_prototyped";
6310 case DW_AT_return_addr:
6311 return "DW_AT_return_addr";
6312 case DW_AT_start_scope:
6313 return "DW_AT_start_scope";
6314 case DW_AT_bit_stride:
6315 return "DW_AT_bit_stride";
6316 case DW_AT_upper_bound:
6317 return "DW_AT_upper_bound";
6318 case DW_AT_abstract_origin:
6319 return "DW_AT_abstract_origin";
6320 case DW_AT_accessibility:
6321 return "DW_AT_accessibility";
6322 case DW_AT_address_class:
6323 return "DW_AT_address_class";
6324 case DW_AT_artificial:
6325 return "DW_AT_artificial";
6326 case DW_AT_base_types:
6327 return "DW_AT_base_types";
6328 case DW_AT_calling_convention:
6329 return "DW_AT_calling_convention";
6330 case DW_AT_count:
6331 return "DW_AT_count";
6332 case DW_AT_data_member_location:
6333 return "DW_AT_data_member_location";
6334 case DW_AT_decl_column:
6335 return "DW_AT_decl_column";
6336 case DW_AT_decl_file:
6337 return "DW_AT_decl_file";
6338 case DW_AT_decl_line:
6339 return "DW_AT_decl_line";
6340 case DW_AT_declaration:
6341 return "DW_AT_declaration";
6342 case DW_AT_discr_list:
6343 return "DW_AT_discr_list";
6344 case DW_AT_encoding:
6345 return "DW_AT_encoding";
6346 case DW_AT_external:
6347 return "DW_AT_external";
6348 case DW_AT_explicit:
6349 return "DW_AT_explicit";
6350 case DW_AT_frame_base:
6351 return "DW_AT_frame_base";
6352 case DW_AT_friend:
6353 return "DW_AT_friend";
6354 case DW_AT_identifier_case:
6355 return "DW_AT_identifier_case";
6356 case DW_AT_macro_info:
6357 return "DW_AT_macro_info";
6358 case DW_AT_namelist_items:
6359 return "DW_AT_namelist_items";
6360 case DW_AT_priority:
6361 return "DW_AT_priority";
6362 case DW_AT_segment:
6363 return "DW_AT_segment";
6364 case DW_AT_specification:
6365 return "DW_AT_specification";
6366 case DW_AT_static_link:
6367 return "DW_AT_static_link";
6368 case DW_AT_type:
6369 return "DW_AT_type";
6370 case DW_AT_use_location:
6371 return "DW_AT_use_location";
6372 case DW_AT_variable_parameter:
6373 return "DW_AT_variable_parameter";
6374 case DW_AT_virtuality:
6375 return "DW_AT_virtuality";
6376 case DW_AT_vtable_elem_location:
6377 return "DW_AT_vtable_elem_location";
6378
6379 case DW_AT_allocated:
6380 return "DW_AT_allocated";
6381 case DW_AT_associated:
6382 return "DW_AT_associated";
6383 case DW_AT_data_location:
6384 return "DW_AT_data_location";
6385 case DW_AT_byte_stride:
6386 return "DW_AT_byte_stride";
6387 case DW_AT_entry_pc:
6388 return "DW_AT_entry_pc";
6389 case DW_AT_use_UTF8:
6390 return "DW_AT_use_UTF8";
6391 case DW_AT_extension:
6392 return "DW_AT_extension";
6393 case DW_AT_ranges:
6394 return "DW_AT_ranges";
6395 case DW_AT_trampoline:
6396 return "DW_AT_trampoline";
6397 case DW_AT_call_column:
6398 return "DW_AT_call_column";
6399 case DW_AT_call_file:
6400 return "DW_AT_call_file";
6401 case DW_AT_call_line:
6402 return "DW_AT_call_line";
6403
6404 case DW_AT_MIPS_fde:
6405 return "DW_AT_MIPS_fde";
6406 case DW_AT_MIPS_loop_begin:
6407 return "DW_AT_MIPS_loop_begin";
6408 case DW_AT_MIPS_tail_loop_begin:
6409 return "DW_AT_MIPS_tail_loop_begin";
6410 case DW_AT_MIPS_epilog_begin:
6411 return "DW_AT_MIPS_epilog_begin";
6412 case DW_AT_MIPS_loop_unroll_factor:
6413 return "DW_AT_MIPS_loop_unroll_factor";
6414 case DW_AT_MIPS_software_pipeline_depth:
6415 return "DW_AT_MIPS_software_pipeline_depth";
6416 case DW_AT_MIPS_linkage_name:
6417 return "DW_AT_MIPS_linkage_name";
6418 case DW_AT_MIPS_stride:
6419 return "DW_AT_MIPS_stride";
6420 case DW_AT_MIPS_abstract_name:
6421 return "DW_AT_MIPS_abstract_name";
6422 case DW_AT_MIPS_clone_origin:
6423 return "DW_AT_MIPS_clone_origin";
6424 case DW_AT_MIPS_has_inlines:
6425 return "DW_AT_MIPS_has_inlines";
6426
6427 case DW_AT_sf_names:
6428 return "DW_AT_sf_names";
6429 case DW_AT_src_info:
6430 return "DW_AT_src_info";
6431 case DW_AT_mac_info:
6432 return "DW_AT_mac_info";
6433 case DW_AT_src_coords:
6434 return "DW_AT_src_coords";
6435 case DW_AT_body_begin:
6436 return "DW_AT_body_begin";
6437 case DW_AT_body_end:
6438 return "DW_AT_body_end";
6439 case DW_AT_GNU_vector:
6440 return "DW_AT_GNU_vector";
6441
6442 case DW_AT_VMS_rtnbeg_pd_address:
6443 return "DW_AT_VMS_rtnbeg_pd_address";
6444
6445 default:
6446 return "DW_AT_<unknown>";
6447 }
6448 }
6449
6450 /* Convert a DWARF value form code into its string name. */
6451
6452 static const char *
6453 dwarf_form_name (unsigned int form)
6454 {
6455 switch (form)
6456 {
6457 case DW_FORM_addr:
6458 return "DW_FORM_addr";
6459 case DW_FORM_block2:
6460 return "DW_FORM_block2";
6461 case DW_FORM_block4:
6462 return "DW_FORM_block4";
6463 case DW_FORM_data2:
6464 return "DW_FORM_data2";
6465 case DW_FORM_data4:
6466 return "DW_FORM_data4";
6467 case DW_FORM_data8:
6468 return "DW_FORM_data8";
6469 case DW_FORM_string:
6470 return "DW_FORM_string";
6471 case DW_FORM_block:
6472 return "DW_FORM_block";
6473 case DW_FORM_block1:
6474 return "DW_FORM_block1";
6475 case DW_FORM_data1:
6476 return "DW_FORM_data1";
6477 case DW_FORM_flag:
6478 return "DW_FORM_flag";
6479 case DW_FORM_sdata:
6480 return "DW_FORM_sdata";
6481 case DW_FORM_strp:
6482 return "DW_FORM_strp";
6483 case DW_FORM_udata:
6484 return "DW_FORM_udata";
6485 case DW_FORM_ref_addr:
6486 return "DW_FORM_ref_addr";
6487 case DW_FORM_ref1:
6488 return "DW_FORM_ref1";
6489 case DW_FORM_ref2:
6490 return "DW_FORM_ref2";
6491 case DW_FORM_ref4:
6492 return "DW_FORM_ref4";
6493 case DW_FORM_ref8:
6494 return "DW_FORM_ref8";
6495 case DW_FORM_ref_udata:
6496 return "DW_FORM_ref_udata";
6497 case DW_FORM_indirect:
6498 return "DW_FORM_indirect";
6499 default:
6500 return "DW_FORM_<unknown>";
6501 }
6502 }
6503 \f
6504 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
6505 instance of an inlined instance of a decl which is local to an inline
6506 function, so we have to trace all of the way back through the origin chain
6507 to find out what sort of node actually served as the original seed for the
6508 given block. */
6509
6510 static tree
6511 decl_ultimate_origin (const_tree decl)
6512 {
6513 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6514 return NULL_TREE;
6515
6516 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6517 nodes in the function to point to themselves; ignore that if
6518 we're trying to output the abstract instance of this function. */
6519 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6520 return NULL_TREE;
6521
6522 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6523 most distant ancestor, this should never happen. */
6524 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6525
6526 return DECL_ABSTRACT_ORIGIN (decl);
6527 }
6528
6529 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
6530 of a virtual function may refer to a base class, so we check the 'this'
6531 parameter. */
6532
6533 static tree
6534 decl_class_context (tree decl)
6535 {
6536 tree context = NULL_TREE;
6537
6538 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6539 context = DECL_CONTEXT (decl);
6540 else
6541 context = TYPE_MAIN_VARIANT
6542 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6543
6544 if (context && !TYPE_P (context))
6545 context = NULL_TREE;
6546
6547 return context;
6548 }
6549 \f
6550 /* Add an attribute/value pair to a DIE. */
6551
6552 static inline void
6553 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6554 {
6555 /* Maybe this should be an assert? */
6556 if (die == NULL)
6557 return;
6558
6559 if (die->die_attr == NULL)
6560 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6561 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6562 }
6563
6564 static inline enum dw_val_class
6565 AT_class (dw_attr_ref a)
6566 {
6567 return a->dw_attr_val.val_class;
6568 }
6569
6570 /* Add a flag value attribute to a DIE. */
6571
6572 static inline void
6573 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6574 {
6575 dw_attr_node attr;
6576
6577 attr.dw_attr = attr_kind;
6578 attr.dw_attr_val.val_class = dw_val_class_flag;
6579 attr.dw_attr_val.v.val_flag = flag;
6580 add_dwarf_attr (die, &attr);
6581 }
6582
6583 static inline unsigned
6584 AT_flag (dw_attr_ref a)
6585 {
6586 gcc_assert (a && AT_class (a) == dw_val_class_flag);
6587 return a->dw_attr_val.v.val_flag;
6588 }
6589
6590 /* Add a signed integer attribute value to a DIE. */
6591
6592 static inline void
6593 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6594 {
6595 dw_attr_node attr;
6596
6597 attr.dw_attr = attr_kind;
6598 attr.dw_attr_val.val_class = dw_val_class_const;
6599 attr.dw_attr_val.v.val_int = int_val;
6600 add_dwarf_attr (die, &attr);
6601 }
6602
6603 static inline HOST_WIDE_INT
6604 AT_int (dw_attr_ref a)
6605 {
6606 gcc_assert (a && AT_class (a) == dw_val_class_const);
6607 return a->dw_attr_val.v.val_int;
6608 }
6609
6610 /* Add an unsigned integer attribute value to a DIE. */
6611
6612 static inline void
6613 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6614 unsigned HOST_WIDE_INT unsigned_val)
6615 {
6616 dw_attr_node attr;
6617
6618 attr.dw_attr = attr_kind;
6619 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6620 attr.dw_attr_val.v.val_unsigned = unsigned_val;
6621 add_dwarf_attr (die, &attr);
6622 }
6623
6624 static inline unsigned HOST_WIDE_INT
6625 AT_unsigned (dw_attr_ref a)
6626 {
6627 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6628 return a->dw_attr_val.v.val_unsigned;
6629 }
6630
6631 /* Add an unsigned double integer attribute value to a DIE. */
6632
6633 static inline void
6634 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6635 long unsigned int val_hi, long unsigned int val_low)
6636 {
6637 dw_attr_node attr;
6638
6639 attr.dw_attr = attr_kind;
6640 attr.dw_attr_val.val_class = dw_val_class_long_long;
6641 attr.dw_attr_val.v.val_long_long.hi = val_hi;
6642 attr.dw_attr_val.v.val_long_long.low = val_low;
6643 add_dwarf_attr (die, &attr);
6644 }
6645
6646 /* Add a floating point attribute value to a DIE and return it. */
6647
6648 static inline void
6649 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6650 unsigned int length, unsigned int elt_size, unsigned char *array)
6651 {
6652 dw_attr_node attr;
6653
6654 attr.dw_attr = attr_kind;
6655 attr.dw_attr_val.val_class = dw_val_class_vec;
6656 attr.dw_attr_val.v.val_vec.length = length;
6657 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6658 attr.dw_attr_val.v.val_vec.array = array;
6659 add_dwarf_attr (die, &attr);
6660 }
6661
6662 /* Hash and equality functions for debug_str_hash. */
6663
6664 static hashval_t
6665 debug_str_do_hash (const void *x)
6666 {
6667 return htab_hash_string (((const struct indirect_string_node *)x)->str);
6668 }
6669
6670 static int
6671 debug_str_eq (const void *x1, const void *x2)
6672 {
6673 return strcmp ((((const struct indirect_string_node *)x1)->str),
6674 (const char *)x2) == 0;
6675 }
6676
6677 static struct indirect_string_node *
6678 find_AT_string (const char *str)
6679 {
6680 struct indirect_string_node *node;
6681 void **slot;
6682
6683 if (! debug_str_hash)
6684 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6685 debug_str_eq, NULL);
6686
6687 slot = htab_find_slot_with_hash (debug_str_hash, str,
6688 htab_hash_string (str), INSERT);
6689 if (*slot == NULL)
6690 {
6691 node = (struct indirect_string_node *)
6692 ggc_alloc_cleared (sizeof (struct indirect_string_node));
6693 node->str = ggc_strdup (str);
6694 *slot = node;
6695 }
6696 else
6697 node = (struct indirect_string_node *) *slot;
6698
6699 node->refcount++;
6700 return node;
6701 }
6702
6703 /* Add a string attribute value to a DIE. */
6704
6705 static inline void
6706 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6707 {
6708 dw_attr_node attr;
6709 struct indirect_string_node *node;
6710
6711 node = find_AT_string (str);
6712
6713 attr.dw_attr = attr_kind;
6714 attr.dw_attr_val.val_class = dw_val_class_str;
6715 attr.dw_attr_val.v.val_str = node;
6716 add_dwarf_attr (die, &attr);
6717 }
6718
6719 static inline const char *
6720 AT_string (dw_attr_ref a)
6721 {
6722 gcc_assert (a && AT_class (a) == dw_val_class_str);
6723 return a->dw_attr_val.v.val_str->str;
6724 }
6725
6726 /* Find out whether a string should be output inline in DIE
6727 or out-of-line in .debug_str section. */
6728
6729 static enum dwarf_form
6730 AT_string_form (dw_attr_ref a)
6731 {
6732 struct indirect_string_node *node;
6733 unsigned int len;
6734 char label[32];
6735
6736 gcc_assert (a && AT_class (a) == dw_val_class_str);
6737
6738 node = a->dw_attr_val.v.val_str;
6739 if (node->form)
6740 return node->form;
6741
6742 len = strlen (node->str) + 1;
6743
6744 /* If the string is shorter or equal to the size of the reference, it is
6745 always better to put it inline. */
6746 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6747 return node->form = DW_FORM_string;
6748
6749 /* If we cannot expect the linker to merge strings in .debug_str
6750 section, only put it into .debug_str if it is worth even in this
6751 single module. */
6752 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6753 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6754 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6755 return node->form = DW_FORM_string;
6756
6757 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6758 ++dw2_string_counter;
6759 node->label = xstrdup (label);
6760
6761 return node->form = DW_FORM_strp;
6762 }
6763
6764 /* Add a DIE reference attribute value to a DIE. */
6765
6766 static inline void
6767 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6768 {
6769 dw_attr_node attr;
6770
6771 attr.dw_attr = attr_kind;
6772 attr.dw_attr_val.val_class = dw_val_class_die_ref;
6773 attr.dw_attr_val.v.val_die_ref.die = targ_die;
6774 attr.dw_attr_val.v.val_die_ref.external = 0;
6775 add_dwarf_attr (die, &attr);
6776 }
6777
6778 /* Add an AT_specification attribute to a DIE, and also make the back
6779 pointer from the specification to the definition. */
6780
6781 static inline void
6782 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6783 {
6784 add_AT_die_ref (die, DW_AT_specification, targ_die);
6785 gcc_assert (!targ_die->die_definition);
6786 targ_die->die_definition = die;
6787 }
6788
6789 static inline dw_die_ref
6790 AT_ref (dw_attr_ref a)
6791 {
6792 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6793 return a->dw_attr_val.v.val_die_ref.die;
6794 }
6795
6796 static inline int
6797 AT_ref_external (dw_attr_ref a)
6798 {
6799 if (a && AT_class (a) == dw_val_class_die_ref)
6800 return a->dw_attr_val.v.val_die_ref.external;
6801
6802 return 0;
6803 }
6804
6805 static inline void
6806 set_AT_ref_external (dw_attr_ref a, int i)
6807 {
6808 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6809 a->dw_attr_val.v.val_die_ref.external = i;
6810 }
6811
6812 /* Add an FDE reference attribute value to a DIE. */
6813
6814 static inline void
6815 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6816 {
6817 dw_attr_node attr;
6818
6819 attr.dw_attr = attr_kind;
6820 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6821 attr.dw_attr_val.v.val_fde_index = targ_fde;
6822 add_dwarf_attr (die, &attr);
6823 }
6824
6825 /* Add a location description attribute value to a DIE. */
6826
6827 static inline void
6828 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6829 {
6830 dw_attr_node attr;
6831
6832 attr.dw_attr = attr_kind;
6833 attr.dw_attr_val.val_class = dw_val_class_loc;
6834 attr.dw_attr_val.v.val_loc = loc;
6835 add_dwarf_attr (die, &attr);
6836 }
6837
6838 static inline dw_loc_descr_ref
6839 AT_loc (dw_attr_ref a)
6840 {
6841 gcc_assert (a && AT_class (a) == dw_val_class_loc);
6842 return a->dw_attr_val.v.val_loc;
6843 }
6844
6845 static inline void
6846 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6847 {
6848 dw_attr_node attr;
6849
6850 attr.dw_attr = attr_kind;
6851 attr.dw_attr_val.val_class = dw_val_class_loc_list;
6852 attr.dw_attr_val.v.val_loc_list = loc_list;
6853 add_dwarf_attr (die, &attr);
6854 have_location_lists = true;
6855 }
6856
6857 static inline dw_loc_list_ref
6858 AT_loc_list (dw_attr_ref a)
6859 {
6860 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6861 return a->dw_attr_val.v.val_loc_list;
6862 }
6863
6864 /* Add an address constant attribute value to a DIE. */
6865
6866 static inline void
6867 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6868 {
6869 dw_attr_node attr;
6870
6871 attr.dw_attr = attr_kind;
6872 attr.dw_attr_val.val_class = dw_val_class_addr;
6873 attr.dw_attr_val.v.val_addr = addr;
6874 add_dwarf_attr (die, &attr);
6875 }
6876
6877 /* Get the RTX from to an address DIE attribute. */
6878
6879 static inline rtx
6880 AT_addr (dw_attr_ref a)
6881 {
6882 gcc_assert (a && AT_class (a) == dw_val_class_addr);
6883 return a->dw_attr_val.v.val_addr;
6884 }
6885
6886 /* Add a file attribute value to a DIE. */
6887
6888 static inline void
6889 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6890 struct dwarf_file_data *fd)
6891 {
6892 dw_attr_node attr;
6893
6894 attr.dw_attr = attr_kind;
6895 attr.dw_attr_val.val_class = dw_val_class_file;
6896 attr.dw_attr_val.v.val_file = fd;
6897 add_dwarf_attr (die, &attr);
6898 }
6899
6900 /* Get the dwarf_file_data from a file DIE attribute. */
6901
6902 static inline struct dwarf_file_data *
6903 AT_file (dw_attr_ref a)
6904 {
6905 gcc_assert (a && AT_class (a) == dw_val_class_file);
6906 return a->dw_attr_val.v.val_file;
6907 }
6908
6909 /* Add a label identifier attribute value to a DIE. */
6910
6911 static inline void
6912 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6913 {
6914 dw_attr_node attr;
6915
6916 attr.dw_attr = attr_kind;
6917 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6918 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6919 add_dwarf_attr (die, &attr);
6920 }
6921
6922 /* Add a section offset attribute value to a DIE, an offset into the
6923 debug_line section. */
6924
6925 static inline void
6926 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6927 const char *label)
6928 {
6929 dw_attr_node attr;
6930
6931 attr.dw_attr = attr_kind;
6932 attr.dw_attr_val.val_class = dw_val_class_lineptr;
6933 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6934 add_dwarf_attr (die, &attr);
6935 }
6936
6937 /* Add a section offset attribute value to a DIE, an offset into the
6938 debug_macinfo section. */
6939
6940 static inline void
6941 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6942 const char *label)
6943 {
6944 dw_attr_node attr;
6945
6946 attr.dw_attr = attr_kind;
6947 attr.dw_attr_val.val_class = dw_val_class_macptr;
6948 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6949 add_dwarf_attr (die, &attr);
6950 }
6951
6952 /* Add an offset attribute value to a DIE. */
6953
6954 static inline void
6955 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6956 unsigned HOST_WIDE_INT offset)
6957 {
6958 dw_attr_node attr;
6959
6960 attr.dw_attr = attr_kind;
6961 attr.dw_attr_val.val_class = dw_val_class_offset;
6962 attr.dw_attr_val.v.val_offset = offset;
6963 add_dwarf_attr (die, &attr);
6964 }
6965
6966 /* Add an range_list attribute value to a DIE. */
6967
6968 static void
6969 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6970 long unsigned int offset)
6971 {
6972 dw_attr_node attr;
6973
6974 attr.dw_attr = attr_kind;
6975 attr.dw_attr_val.val_class = dw_val_class_range_list;
6976 attr.dw_attr_val.v.val_offset = offset;
6977 add_dwarf_attr (die, &attr);
6978 }
6979
6980 static inline const char *
6981 AT_lbl (dw_attr_ref a)
6982 {
6983 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6984 || AT_class (a) == dw_val_class_lineptr
6985 || AT_class (a) == dw_val_class_macptr));
6986 return a->dw_attr_val.v.val_lbl_id;
6987 }
6988
6989 /* Get the attribute of type attr_kind. */
6990
6991 static dw_attr_ref
6992 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6993 {
6994 dw_attr_ref a;
6995 unsigned ix;
6996 dw_die_ref spec = NULL;
6997
6998 if (! die)
6999 return NULL;
7000
7001 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7002 if (a->dw_attr == attr_kind)
7003 return a;
7004 else if (a->dw_attr == DW_AT_specification
7005 || a->dw_attr == DW_AT_abstract_origin)
7006 spec = AT_ref (a);
7007
7008 if (spec)
7009 return get_AT (spec, attr_kind);
7010
7011 return NULL;
7012 }
7013
7014 /* Return the "low pc" attribute value, typically associated with a subprogram
7015 DIE. Return null if the "low pc" attribute is either not present, or if it
7016 cannot be represented as an assembler label identifier. */
7017
7018 static inline const char *
7019 get_AT_low_pc (dw_die_ref die)
7020 {
7021 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7022
7023 return a ? AT_lbl (a) : NULL;
7024 }
7025
7026 /* Return the "high pc" attribute value, typically associated with a subprogram
7027 DIE. Return null if the "high pc" attribute is either not present, or if it
7028 cannot be represented as an assembler label identifier. */
7029
7030 static inline const char *
7031 get_AT_hi_pc (dw_die_ref die)
7032 {
7033 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7034
7035 return a ? AT_lbl (a) : NULL;
7036 }
7037
7038 /* Return the value of the string attribute designated by ATTR_KIND, or
7039 NULL if it is not present. */
7040
7041 static inline const char *
7042 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7043 {
7044 dw_attr_ref a = get_AT (die, attr_kind);
7045
7046 return a ? AT_string (a) : NULL;
7047 }
7048
7049 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7050 if it is not present. */
7051
7052 static inline int
7053 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7054 {
7055 dw_attr_ref a = get_AT (die, attr_kind);
7056
7057 return a ? AT_flag (a) : 0;
7058 }
7059
7060 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7061 if it is not present. */
7062
7063 static inline unsigned
7064 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7065 {
7066 dw_attr_ref a = get_AT (die, attr_kind);
7067
7068 return a ? AT_unsigned (a) : 0;
7069 }
7070
7071 static inline dw_die_ref
7072 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7073 {
7074 dw_attr_ref a = get_AT (die, attr_kind);
7075
7076 return a ? AT_ref (a) : NULL;
7077 }
7078
7079 static inline struct dwarf_file_data *
7080 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7081 {
7082 dw_attr_ref a = get_AT (die, attr_kind);
7083
7084 return a ? AT_file (a) : NULL;
7085 }
7086
7087 /* Return TRUE if the language is C or C++. */
7088
7089 static inline bool
7090 is_c_family (void)
7091 {
7092 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7093
7094 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7095 || lang == DW_LANG_C99
7096 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7097 }
7098
7099 /* Return TRUE if the language is C++. */
7100
7101 static inline bool
7102 is_cxx (void)
7103 {
7104 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7105
7106 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7107 }
7108
7109 /* Return TRUE if the language is Fortran. */
7110
7111 static inline bool
7112 is_fortran (void)
7113 {
7114 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7115
7116 return (lang == DW_LANG_Fortran77
7117 || lang == DW_LANG_Fortran90
7118 || lang == DW_LANG_Fortran95);
7119 }
7120
7121 /* Return TRUE if the language is Java. */
7122
7123 static inline bool
7124 is_java (void)
7125 {
7126 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7127
7128 return lang == DW_LANG_Java;
7129 }
7130
7131 /* Return TRUE if the language is Ada. */
7132
7133 static inline bool
7134 is_ada (void)
7135 {
7136 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7137
7138 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7139 }
7140
7141 /* Remove the specified attribute if present. */
7142
7143 static void
7144 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7145 {
7146 dw_attr_ref a;
7147 unsigned ix;
7148
7149 if (! die)
7150 return;
7151
7152 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7153 if (a->dw_attr == attr_kind)
7154 {
7155 if (AT_class (a) == dw_val_class_str)
7156 if (a->dw_attr_val.v.val_str->refcount)
7157 a->dw_attr_val.v.val_str->refcount--;
7158
7159 /* VEC_ordered_remove should help reduce the number of abbrevs
7160 that are needed. */
7161 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7162 return;
7163 }
7164 }
7165
7166 /* Remove CHILD from its parent. PREV must have the property that
7167 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
7168
7169 static void
7170 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7171 {
7172 gcc_assert (child->die_parent == prev->die_parent);
7173 gcc_assert (prev->die_sib == child);
7174 if (prev == child)
7175 {
7176 gcc_assert (child->die_parent->die_child == child);
7177 prev = NULL;
7178 }
7179 else
7180 prev->die_sib = child->die_sib;
7181 if (child->die_parent->die_child == child)
7182 child->die_parent->die_child = prev;
7183 }
7184
7185 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
7186 matches TAG. */
7187
7188 static void
7189 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7190 {
7191 dw_die_ref c;
7192
7193 c = die->die_child;
7194 if (c) do {
7195 dw_die_ref prev = c;
7196 c = c->die_sib;
7197 while (c->die_tag == tag)
7198 {
7199 remove_child_with_prev (c, prev);
7200 /* Might have removed every child. */
7201 if (c == c->die_sib)
7202 return;
7203 c = c->die_sib;
7204 }
7205 } while (c != die->die_child);
7206 }
7207
7208 /* Add a CHILD_DIE as the last child of DIE. */
7209
7210 static void
7211 add_child_die (dw_die_ref die, dw_die_ref child_die)
7212 {
7213 /* FIXME this should probably be an assert. */
7214 if (! die || ! child_die)
7215 return;
7216 gcc_assert (die != child_die);
7217
7218 child_die->die_parent = die;
7219 if (die->die_child)
7220 {
7221 child_die->die_sib = die->die_child->die_sib;
7222 die->die_child->die_sib = child_die;
7223 }
7224 else
7225 child_die->die_sib = child_die;
7226 die->die_child = child_die;
7227 }
7228
7229 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7230 is the specification, to the end of PARENT's list of children.
7231 This is done by removing and re-adding it. */
7232
7233 static void
7234 splice_child_die (dw_die_ref parent, dw_die_ref child)
7235 {
7236 dw_die_ref p;
7237
7238 /* We want the declaration DIE from inside the class, not the
7239 specification DIE at toplevel. */
7240 if (child->die_parent != parent)
7241 {
7242 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7243
7244 if (tmp)
7245 child = tmp;
7246 }
7247
7248 gcc_assert (child->die_parent == parent
7249 || (child->die_parent
7250 == get_AT_ref (parent, DW_AT_specification)));
7251
7252 for (p = child->die_parent->die_child; ; p = p->die_sib)
7253 if (p->die_sib == child)
7254 {
7255 remove_child_with_prev (child, p);
7256 break;
7257 }
7258
7259 add_child_die (parent, child);
7260 }
7261
7262 /* Return a pointer to a newly created DIE node. */
7263
7264 static inline dw_die_ref
7265 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7266 {
7267 dw_die_ref die = GGC_CNEW (die_node);
7268
7269 die->die_tag = tag_value;
7270
7271 if (parent_die != NULL)
7272 add_child_die (parent_die, die);
7273 else
7274 {
7275 limbo_die_node *limbo_node;
7276
7277 limbo_node = GGC_CNEW (limbo_die_node);
7278 limbo_node->die = die;
7279 limbo_node->created_for = t;
7280 limbo_node->next = limbo_die_list;
7281 limbo_die_list = limbo_node;
7282 }
7283
7284 return die;
7285 }
7286
7287 /* Return the DIE associated with the given type specifier. */
7288
7289 static inline dw_die_ref
7290 lookup_type_die (tree type)
7291 {
7292 return TYPE_SYMTAB_DIE (type);
7293 }
7294
7295 /* Equate a DIE to a given type specifier. */
7296
7297 static inline void
7298 equate_type_number_to_die (tree type, dw_die_ref type_die)
7299 {
7300 TYPE_SYMTAB_DIE (type) = type_die;
7301 }
7302
7303 /* Returns a hash value for X (which really is a die_struct). */
7304
7305 static hashval_t
7306 decl_die_table_hash (const void *x)
7307 {
7308 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7309 }
7310
7311 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
7312
7313 static int
7314 decl_die_table_eq (const void *x, const void *y)
7315 {
7316 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7317 }
7318
7319 /* Return the DIE associated with a given declaration. */
7320
7321 static inline dw_die_ref
7322 lookup_decl_die (tree decl)
7323 {
7324 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7325 }
7326
7327 /* Returns a hash value for X (which really is a var_loc_list). */
7328
7329 static hashval_t
7330 decl_loc_table_hash (const void *x)
7331 {
7332 return (hashval_t) ((const var_loc_list *) x)->decl_id;
7333 }
7334
7335 /* Return nonzero if decl_id of var_loc_list X is the same as
7336 UID of decl *Y. */
7337
7338 static int
7339 decl_loc_table_eq (const void *x, const void *y)
7340 {
7341 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7342 }
7343
7344 /* Return the var_loc list associated with a given declaration. */
7345
7346 static inline var_loc_list *
7347 lookup_decl_loc (const_tree decl)
7348 {
7349 return (var_loc_list *)
7350 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7351 }
7352
7353 /* Equate a DIE to a particular declaration. */
7354
7355 static void
7356 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7357 {
7358 unsigned int decl_id = DECL_UID (decl);
7359 void **slot;
7360
7361 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7362 *slot = decl_die;
7363 decl_die->decl_id = decl_id;
7364 }
7365
7366 /* Add a variable location node to the linked list for DECL. */
7367
7368 static void
7369 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7370 {
7371 unsigned int decl_id = DECL_UID (decl);
7372 var_loc_list *temp;
7373 void **slot;
7374
7375 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7376 if (*slot == NULL)
7377 {
7378 temp = GGC_CNEW (var_loc_list);
7379 temp->decl_id = decl_id;
7380 *slot = temp;
7381 }
7382 else
7383 temp = (var_loc_list *) *slot;
7384
7385 if (temp->last)
7386 {
7387 /* If the current location is the same as the end of the list,
7388 and either both or neither of the locations is uninitialized,
7389 we have nothing to do. */
7390 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7391 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7392 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7393 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7394 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7395 == VAR_INIT_STATUS_UNINITIALIZED)
7396 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7397 == VAR_INIT_STATUS_UNINITIALIZED))))
7398 {
7399 /* Add LOC to the end of list and update LAST. */
7400 temp->last->next = loc;
7401 temp->last = loc;
7402 }
7403 }
7404 /* Do not add empty location to the beginning of the list. */
7405 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7406 {
7407 temp->first = loc;
7408 temp->last = loc;
7409 }
7410 }
7411 \f
7412 /* Keep track of the number of spaces used to indent the
7413 output of the debugging routines that print the structure of
7414 the DIE internal representation. */
7415 static int print_indent;
7416
7417 /* Indent the line the number of spaces given by print_indent. */
7418
7419 static inline void
7420 print_spaces (FILE *outfile)
7421 {
7422 fprintf (outfile, "%*s", print_indent, "");
7423 }
7424
7425 /* Print the information associated with a given DIE, and its children.
7426 This routine is a debugging aid only. */
7427
7428 static void
7429 print_die (dw_die_ref die, FILE *outfile)
7430 {
7431 dw_attr_ref a;
7432 dw_die_ref c;
7433 unsigned ix;
7434
7435 print_spaces (outfile);
7436 fprintf (outfile, "DIE %4ld: %s\n",
7437 die->die_offset, dwarf_tag_name (die->die_tag));
7438 print_spaces (outfile);
7439 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
7440 fprintf (outfile, " offset: %ld\n", die->die_offset);
7441
7442 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7443 {
7444 print_spaces (outfile);
7445 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
7446
7447 switch (AT_class (a))
7448 {
7449 case dw_val_class_addr:
7450 fprintf (outfile, "address");
7451 break;
7452 case dw_val_class_offset:
7453 fprintf (outfile, "offset");
7454 break;
7455 case dw_val_class_loc:
7456 fprintf (outfile, "location descriptor");
7457 break;
7458 case dw_val_class_loc_list:
7459 fprintf (outfile, "location list -> label:%s",
7460 AT_loc_list (a)->ll_symbol);
7461 break;
7462 case dw_val_class_range_list:
7463 fprintf (outfile, "range list");
7464 break;
7465 case dw_val_class_const:
7466 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7467 break;
7468 case dw_val_class_unsigned_const:
7469 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7470 break;
7471 case dw_val_class_long_long:
7472 fprintf (outfile, "constant (%lu,%lu)",
7473 a->dw_attr_val.v.val_long_long.hi,
7474 a->dw_attr_val.v.val_long_long.low);
7475 break;
7476 case dw_val_class_vec:
7477 fprintf (outfile, "floating-point or vector constant");
7478 break;
7479 case dw_val_class_flag:
7480 fprintf (outfile, "%u", AT_flag (a));
7481 break;
7482 case dw_val_class_die_ref:
7483 if (AT_ref (a) != NULL)
7484 {
7485 if (AT_ref (a)->die_symbol)
7486 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7487 else
7488 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7489 }
7490 else
7491 fprintf (outfile, "die -> <null>");
7492 break;
7493 case dw_val_class_lbl_id:
7494 case dw_val_class_lineptr:
7495 case dw_val_class_macptr:
7496 fprintf (outfile, "label: %s", AT_lbl (a));
7497 break;
7498 case dw_val_class_str:
7499 if (AT_string (a) != NULL)
7500 fprintf (outfile, "\"%s\"", AT_string (a));
7501 else
7502 fprintf (outfile, "<null>");
7503 break;
7504 case dw_val_class_file:
7505 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7506 AT_file (a)->emitted_number);
7507 break;
7508 default:
7509 break;
7510 }
7511
7512 fprintf (outfile, "\n");
7513 }
7514
7515 if (die->die_child != NULL)
7516 {
7517 print_indent += 4;
7518 FOR_EACH_CHILD (die, c, print_die (c, outfile));
7519 print_indent -= 4;
7520 }
7521 if (print_indent == 0)
7522 fprintf (outfile, "\n");
7523 }
7524
7525 /* Print the contents of the source code line number correspondence table.
7526 This routine is a debugging aid only. */
7527
7528 static void
7529 print_dwarf_line_table (FILE *outfile)
7530 {
7531 unsigned i;
7532 dw_line_info_ref line_info;
7533
7534 fprintf (outfile, "\n\nDWARF source line information\n");
7535 for (i = 1; i < line_info_table_in_use; i++)
7536 {
7537 line_info = &line_info_table[i];
7538 fprintf (outfile, "%5d: %4ld %6ld\n", i,
7539 line_info->dw_file_num,
7540 line_info->dw_line_num);
7541 }
7542
7543 fprintf (outfile, "\n\n");
7544 }
7545
7546 /* Print the information collected for a given DIE. */
7547
7548 void
7549 debug_dwarf_die (dw_die_ref die)
7550 {
7551 print_die (die, stderr);
7552 }
7553
7554 /* Print all DWARF information collected for the compilation unit.
7555 This routine is a debugging aid only. */
7556
7557 void
7558 debug_dwarf (void)
7559 {
7560 print_indent = 0;
7561 print_die (comp_unit_die, stderr);
7562 if (! DWARF2_ASM_LINE_DEBUG_INFO)
7563 print_dwarf_line_table (stderr);
7564 }
7565 \f
7566 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
7567 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
7568 DIE that marks the start of the DIEs for this include file. */
7569
7570 static dw_die_ref
7571 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7572 {
7573 const char *filename = get_AT_string (bincl_die, DW_AT_name);
7574 dw_die_ref new_unit = gen_compile_unit_die (filename);
7575
7576 new_unit->die_sib = old_unit;
7577 return new_unit;
7578 }
7579
7580 /* Close an include-file CU and reopen the enclosing one. */
7581
7582 static dw_die_ref
7583 pop_compile_unit (dw_die_ref old_unit)
7584 {
7585 dw_die_ref new_unit = old_unit->die_sib;
7586
7587 old_unit->die_sib = NULL;
7588 return new_unit;
7589 }
7590
7591 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7592 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7593
7594 /* Calculate the checksum of a location expression. */
7595
7596 static inline void
7597 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7598 {
7599 int tem;
7600
7601 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7602 CHECKSUM (tem);
7603 CHECKSUM (loc->dw_loc_oprnd1);
7604 CHECKSUM (loc->dw_loc_oprnd2);
7605 }
7606
7607 /* Calculate the checksum of an attribute. */
7608
7609 static void
7610 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7611 {
7612 dw_loc_descr_ref loc;
7613 rtx r;
7614
7615 CHECKSUM (at->dw_attr);
7616
7617 /* We don't care that this was compiled with a different compiler
7618 snapshot; if the output is the same, that's what matters. */
7619 if (at->dw_attr == DW_AT_producer)
7620 return;
7621
7622 switch (AT_class (at))
7623 {
7624 case dw_val_class_const:
7625 CHECKSUM (at->dw_attr_val.v.val_int);
7626 break;
7627 case dw_val_class_unsigned_const:
7628 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7629 break;
7630 case dw_val_class_long_long:
7631 CHECKSUM (at->dw_attr_val.v.val_long_long);
7632 break;
7633 case dw_val_class_vec:
7634 CHECKSUM (at->dw_attr_val.v.val_vec);
7635 break;
7636 case dw_val_class_flag:
7637 CHECKSUM (at->dw_attr_val.v.val_flag);
7638 break;
7639 case dw_val_class_str:
7640 CHECKSUM_STRING (AT_string (at));
7641 break;
7642
7643 case dw_val_class_addr:
7644 r = AT_addr (at);
7645 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7646 CHECKSUM_STRING (XSTR (r, 0));
7647 break;
7648
7649 case dw_val_class_offset:
7650 CHECKSUM (at->dw_attr_val.v.val_offset);
7651 break;
7652
7653 case dw_val_class_loc:
7654 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7655 loc_checksum (loc, ctx);
7656 break;
7657
7658 case dw_val_class_die_ref:
7659 die_checksum (AT_ref (at), ctx, mark);
7660 break;
7661
7662 case dw_val_class_fde_ref:
7663 case dw_val_class_lbl_id:
7664 case dw_val_class_lineptr:
7665 case dw_val_class_macptr:
7666 break;
7667
7668 case dw_val_class_file:
7669 CHECKSUM_STRING (AT_file (at)->filename);
7670 break;
7671
7672 default:
7673 break;
7674 }
7675 }
7676
7677 /* Calculate the checksum of a DIE. */
7678
7679 static void
7680 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7681 {
7682 dw_die_ref c;
7683 dw_attr_ref a;
7684 unsigned ix;
7685
7686 /* To avoid infinite recursion. */
7687 if (die->die_mark)
7688 {
7689 CHECKSUM (die->die_mark);
7690 return;
7691 }
7692 die->die_mark = ++(*mark);
7693
7694 CHECKSUM (die->die_tag);
7695
7696 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7697 attr_checksum (a, ctx, mark);
7698
7699 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7700 }
7701
7702 #undef CHECKSUM
7703 #undef CHECKSUM_STRING
7704
7705 /* Do the location expressions look same? */
7706 static inline int
7707 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7708 {
7709 return loc1->dw_loc_opc == loc2->dw_loc_opc
7710 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7711 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7712 }
7713
7714 /* Do the values look the same? */
7715 static int
7716 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7717 {
7718 dw_loc_descr_ref loc1, loc2;
7719 rtx r1, r2;
7720
7721 if (v1->val_class != v2->val_class)
7722 return 0;
7723
7724 switch (v1->val_class)
7725 {
7726 case dw_val_class_const:
7727 return v1->v.val_int == v2->v.val_int;
7728 case dw_val_class_unsigned_const:
7729 return v1->v.val_unsigned == v2->v.val_unsigned;
7730 case dw_val_class_long_long:
7731 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7732 && v1->v.val_long_long.low == v2->v.val_long_long.low;
7733 case dw_val_class_vec:
7734 if (v1->v.val_vec.length != v2->v.val_vec.length
7735 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7736 return 0;
7737 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7738 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7739 return 0;
7740 return 1;
7741 case dw_val_class_flag:
7742 return v1->v.val_flag == v2->v.val_flag;
7743 case dw_val_class_str:
7744 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7745
7746 case dw_val_class_addr:
7747 r1 = v1->v.val_addr;
7748 r2 = v2->v.val_addr;
7749 if (GET_CODE (r1) != GET_CODE (r2))
7750 return 0;
7751 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7752 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7753
7754 case dw_val_class_offset:
7755 return v1->v.val_offset == v2->v.val_offset;
7756
7757 case dw_val_class_loc:
7758 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7759 loc1 && loc2;
7760 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7761 if (!same_loc_p (loc1, loc2, mark))
7762 return 0;
7763 return !loc1 && !loc2;
7764
7765 case dw_val_class_die_ref:
7766 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7767
7768 case dw_val_class_fde_ref:
7769 case dw_val_class_lbl_id:
7770 case dw_val_class_lineptr:
7771 case dw_val_class_macptr:
7772 return 1;
7773
7774 case dw_val_class_file:
7775 return v1->v.val_file == v2->v.val_file;
7776
7777 default:
7778 return 1;
7779 }
7780 }
7781
7782 /* Do the attributes look the same? */
7783
7784 static int
7785 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7786 {
7787 if (at1->dw_attr != at2->dw_attr)
7788 return 0;
7789
7790 /* We don't care that this was compiled with a different compiler
7791 snapshot; if the output is the same, that's what matters. */
7792 if (at1->dw_attr == DW_AT_producer)
7793 return 1;
7794
7795 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7796 }
7797
7798 /* Do the dies look the same? */
7799
7800 static int
7801 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7802 {
7803 dw_die_ref c1, c2;
7804 dw_attr_ref a1;
7805 unsigned ix;
7806
7807 /* To avoid infinite recursion. */
7808 if (die1->die_mark)
7809 return die1->die_mark == die2->die_mark;
7810 die1->die_mark = die2->die_mark = ++(*mark);
7811
7812 if (die1->die_tag != die2->die_tag)
7813 return 0;
7814
7815 if (VEC_length (dw_attr_node, die1->die_attr)
7816 != VEC_length (dw_attr_node, die2->die_attr))
7817 return 0;
7818
7819 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7820 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7821 return 0;
7822
7823 c1 = die1->die_child;
7824 c2 = die2->die_child;
7825 if (! c1)
7826 {
7827 if (c2)
7828 return 0;
7829 }
7830 else
7831 for (;;)
7832 {
7833 if (!same_die_p (c1, c2, mark))
7834 return 0;
7835 c1 = c1->die_sib;
7836 c2 = c2->die_sib;
7837 if (c1 == die1->die_child)
7838 {
7839 if (c2 == die2->die_child)
7840 break;
7841 else
7842 return 0;
7843 }
7844 }
7845
7846 return 1;
7847 }
7848
7849 /* Do the dies look the same? Wrapper around same_die_p. */
7850
7851 static int
7852 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7853 {
7854 int mark = 0;
7855 int ret = same_die_p (die1, die2, &mark);
7856
7857 unmark_all_dies (die1);
7858 unmark_all_dies (die2);
7859
7860 return ret;
7861 }
7862
7863 /* The prefix to attach to symbols on DIEs in the current comdat debug
7864 info section. */
7865 static char *comdat_symbol_id;
7866
7867 /* The index of the current symbol within the current comdat CU. */
7868 static unsigned int comdat_symbol_number;
7869
7870 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7871 children, and set comdat_symbol_id accordingly. */
7872
7873 static void
7874 compute_section_prefix (dw_die_ref unit_die)
7875 {
7876 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7877 const char *base = die_name ? lbasename (die_name) : "anonymous";
7878 char *name = XALLOCAVEC (char, strlen (base) + 64);
7879 char *p;
7880 int i, mark;
7881 unsigned char checksum[16];
7882 struct md5_ctx ctx;
7883
7884 /* Compute the checksum of the DIE, then append part of it as hex digits to
7885 the name filename of the unit. */
7886
7887 md5_init_ctx (&ctx);
7888 mark = 0;
7889 die_checksum (unit_die, &ctx, &mark);
7890 unmark_all_dies (unit_die);
7891 md5_finish_ctx (&ctx, checksum);
7892
7893 sprintf (name, "%s.", base);
7894 clean_symbol_name (name);
7895
7896 p = name + strlen (name);
7897 for (i = 0; i < 4; i++)
7898 {
7899 sprintf (p, "%.2x", checksum[i]);
7900 p += 2;
7901 }
7902
7903 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7904 comdat_symbol_number = 0;
7905 }
7906
7907 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7908
7909 static int
7910 is_type_die (dw_die_ref die)
7911 {
7912 switch (die->die_tag)
7913 {
7914 case DW_TAG_array_type:
7915 case DW_TAG_class_type:
7916 case DW_TAG_interface_type:
7917 case DW_TAG_enumeration_type:
7918 case DW_TAG_pointer_type:
7919 case DW_TAG_reference_type:
7920 case DW_TAG_string_type:
7921 case DW_TAG_structure_type:
7922 case DW_TAG_subroutine_type:
7923 case DW_TAG_union_type:
7924 case DW_TAG_ptr_to_member_type:
7925 case DW_TAG_set_type:
7926 case DW_TAG_subrange_type:
7927 case DW_TAG_base_type:
7928 case DW_TAG_const_type:
7929 case DW_TAG_file_type:
7930 case DW_TAG_packed_type:
7931 case DW_TAG_volatile_type:
7932 case DW_TAG_typedef:
7933 return 1;
7934 default:
7935 return 0;
7936 }
7937 }
7938
7939 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7940 Basically, we want to choose the bits that are likely to be shared between
7941 compilations (types) and leave out the bits that are specific to individual
7942 compilations (functions). */
7943
7944 static int
7945 is_comdat_die (dw_die_ref c)
7946 {
7947 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7948 we do for stabs. The advantage is a greater likelihood of sharing between
7949 objects that don't include headers in the same order (and therefore would
7950 put the base types in a different comdat). jason 8/28/00 */
7951
7952 if (c->die_tag == DW_TAG_base_type)
7953 return 0;
7954
7955 if (c->die_tag == DW_TAG_pointer_type
7956 || c->die_tag == DW_TAG_reference_type
7957 || c->die_tag == DW_TAG_const_type
7958 || c->die_tag == DW_TAG_volatile_type)
7959 {
7960 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7961
7962 return t ? is_comdat_die (t) : 0;
7963 }
7964
7965 return is_type_die (c);
7966 }
7967
7968 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7969 compilation unit. */
7970
7971 static int
7972 is_symbol_die (dw_die_ref c)
7973 {
7974 return (is_type_die (c)
7975 || (get_AT (c, DW_AT_declaration)
7976 && !get_AT (c, DW_AT_specification))
7977 || c->die_tag == DW_TAG_namespace
7978 || c->die_tag == DW_TAG_module);
7979 }
7980
7981 static char *
7982 gen_internal_sym (const char *prefix)
7983 {
7984 char buf[256];
7985
7986 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7987 return xstrdup (buf);
7988 }
7989
7990 /* Assign symbols to all worthy DIEs under DIE. */
7991
7992 static void
7993 assign_symbol_names (dw_die_ref die)
7994 {
7995 dw_die_ref c;
7996
7997 if (is_symbol_die (die))
7998 {
7999 if (comdat_symbol_id)
8000 {
8001 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8002
8003 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8004 comdat_symbol_id, comdat_symbol_number++);
8005 die->die_symbol = xstrdup (p);
8006 }
8007 else
8008 die->die_symbol = gen_internal_sym ("LDIE");
8009 }
8010
8011 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8012 }
8013
8014 struct cu_hash_table_entry
8015 {
8016 dw_die_ref cu;
8017 unsigned min_comdat_num, max_comdat_num;
8018 struct cu_hash_table_entry *next;
8019 };
8020
8021 /* Routines to manipulate hash table of CUs. */
8022 static hashval_t
8023 htab_cu_hash (const void *of)
8024 {
8025 const struct cu_hash_table_entry *const entry =
8026 (const struct cu_hash_table_entry *) of;
8027
8028 return htab_hash_string (entry->cu->die_symbol);
8029 }
8030
8031 static int
8032 htab_cu_eq (const void *of1, const void *of2)
8033 {
8034 const struct cu_hash_table_entry *const entry1 =
8035 (const struct cu_hash_table_entry *) of1;
8036 const struct die_struct *const entry2 = (const struct die_struct *) of2;
8037
8038 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8039 }
8040
8041 static void
8042 htab_cu_del (void *what)
8043 {
8044 struct cu_hash_table_entry *next,
8045 *entry = (struct cu_hash_table_entry *) what;
8046
8047 while (entry)
8048 {
8049 next = entry->next;
8050 free (entry);
8051 entry = next;
8052 }
8053 }
8054
8055 /* Check whether we have already seen this CU and set up SYM_NUM
8056 accordingly. */
8057 static int
8058 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8059 {
8060 struct cu_hash_table_entry dummy;
8061 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8062
8063 dummy.max_comdat_num = 0;
8064
8065 slot = (struct cu_hash_table_entry **)
8066 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8067 INSERT);
8068 entry = *slot;
8069
8070 for (; entry; last = entry, entry = entry->next)
8071 {
8072 if (same_die_p_wrap (cu, entry->cu))
8073 break;
8074 }
8075
8076 if (entry)
8077 {
8078 *sym_num = entry->min_comdat_num;
8079 return 1;
8080 }
8081
8082 entry = XCNEW (struct cu_hash_table_entry);
8083 entry->cu = cu;
8084 entry->min_comdat_num = *sym_num = last->max_comdat_num;
8085 entry->next = *slot;
8086 *slot = entry;
8087
8088 return 0;
8089 }
8090
8091 /* Record SYM_NUM to record of CU in HTABLE. */
8092 static void
8093 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8094 {
8095 struct cu_hash_table_entry **slot, *entry;
8096
8097 slot = (struct cu_hash_table_entry **)
8098 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8099 NO_INSERT);
8100 entry = *slot;
8101
8102 entry->max_comdat_num = sym_num;
8103 }
8104
8105 /* Traverse the DIE (which is always comp_unit_die), and set up
8106 additional compilation units for each of the include files we see
8107 bracketed by BINCL/EINCL. */
8108
8109 static void
8110 break_out_includes (dw_die_ref die)
8111 {
8112 dw_die_ref c;
8113 dw_die_ref unit = NULL;
8114 limbo_die_node *node, **pnode;
8115 htab_t cu_hash_table;
8116
8117 c = die->die_child;
8118 if (c) do {
8119 dw_die_ref prev = c;
8120 c = c->die_sib;
8121 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8122 || (unit && is_comdat_die (c)))
8123 {
8124 dw_die_ref next = c->die_sib;
8125
8126 /* This DIE is for a secondary CU; remove it from the main one. */
8127 remove_child_with_prev (c, prev);
8128
8129 if (c->die_tag == DW_TAG_GNU_BINCL)
8130 unit = push_new_compile_unit (unit, c);
8131 else if (c->die_tag == DW_TAG_GNU_EINCL)
8132 unit = pop_compile_unit (unit);
8133 else
8134 add_child_die (unit, c);
8135 c = next;
8136 if (c == die->die_child)
8137 break;
8138 }
8139 } while (c != die->die_child);
8140
8141 #if 0
8142 /* We can only use this in debugging, since the frontend doesn't check
8143 to make sure that we leave every include file we enter. */
8144 gcc_assert (!unit);
8145 #endif
8146
8147 assign_symbol_names (die);
8148 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8149 for (node = limbo_die_list, pnode = &limbo_die_list;
8150 node;
8151 node = node->next)
8152 {
8153 int is_dupl;
8154
8155 compute_section_prefix (node->die);
8156 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8157 &comdat_symbol_number);
8158 assign_symbol_names (node->die);
8159 if (is_dupl)
8160 *pnode = node->next;
8161 else
8162 {
8163 pnode = &node->next;
8164 record_comdat_symbol_number (node->die, cu_hash_table,
8165 comdat_symbol_number);
8166 }
8167 }
8168 htab_delete (cu_hash_table);
8169 }
8170
8171 /* Traverse the DIE and add a sibling attribute if it may have the
8172 effect of speeding up access to siblings. To save some space,
8173 avoid generating sibling attributes for DIE's without children. */
8174
8175 static void
8176 add_sibling_attributes (dw_die_ref die)
8177 {
8178 dw_die_ref c;
8179
8180 if (! die->die_child)
8181 return;
8182
8183 if (die->die_parent && die != die->die_parent->die_child)
8184 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8185
8186 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8187 }
8188
8189 /* Output all location lists for the DIE and its children. */
8190
8191 static void
8192 output_location_lists (dw_die_ref die)
8193 {
8194 dw_die_ref c;
8195 dw_attr_ref a;
8196 unsigned ix;
8197
8198 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8199 if (AT_class (a) == dw_val_class_loc_list)
8200 output_loc_list (AT_loc_list (a));
8201
8202 FOR_EACH_CHILD (die, c, output_location_lists (c));
8203 }
8204
8205 /* The format of each DIE (and its attribute value pairs) is encoded in an
8206 abbreviation table. This routine builds the abbreviation table and assigns
8207 a unique abbreviation id for each abbreviation entry. The children of each
8208 die are visited recursively. */
8209
8210 static void
8211 build_abbrev_table (dw_die_ref die)
8212 {
8213 unsigned long abbrev_id;
8214 unsigned int n_alloc;
8215 dw_die_ref c;
8216 dw_attr_ref a;
8217 unsigned ix;
8218
8219 /* Scan the DIE references, and mark as external any that refer to
8220 DIEs from other CUs (i.e. those which are not marked). */
8221 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8222 if (AT_class (a) == dw_val_class_die_ref
8223 && AT_ref (a)->die_mark == 0)
8224 {
8225 gcc_assert (AT_ref (a)->die_symbol);
8226 set_AT_ref_external (a, 1);
8227 }
8228
8229 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8230 {
8231 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8232 dw_attr_ref die_a, abbrev_a;
8233 unsigned ix;
8234 bool ok = true;
8235
8236 if (abbrev->die_tag != die->die_tag)
8237 continue;
8238 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8239 continue;
8240
8241 if (VEC_length (dw_attr_node, abbrev->die_attr)
8242 != VEC_length (dw_attr_node, die->die_attr))
8243 continue;
8244
8245 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8246 {
8247 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8248 if ((abbrev_a->dw_attr != die_a->dw_attr)
8249 || (value_format (abbrev_a) != value_format (die_a)))
8250 {
8251 ok = false;
8252 break;
8253 }
8254 }
8255 if (ok)
8256 break;
8257 }
8258
8259 if (abbrev_id >= abbrev_die_table_in_use)
8260 {
8261 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8262 {
8263 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8264 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8265 n_alloc);
8266
8267 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8268 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8269 abbrev_die_table_allocated = n_alloc;
8270 }
8271
8272 ++abbrev_die_table_in_use;
8273 abbrev_die_table[abbrev_id] = die;
8274 }
8275
8276 die->die_abbrev = abbrev_id;
8277 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8278 }
8279 \f
8280 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8281
8282 static int
8283 constant_size (unsigned HOST_WIDE_INT value)
8284 {
8285 int log;
8286
8287 if (value == 0)
8288 log = 0;
8289 else
8290 log = floor_log2 (value);
8291
8292 log = log / 8;
8293 log = 1 << (floor_log2 (log) + 1);
8294
8295 return log;
8296 }
8297
8298 /* Return the size of a DIE as it is represented in the
8299 .debug_info section. */
8300
8301 static unsigned long
8302 size_of_die (dw_die_ref die)
8303 {
8304 unsigned long size = 0;
8305 dw_attr_ref a;
8306 unsigned ix;
8307
8308 size += size_of_uleb128 (die->die_abbrev);
8309 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8310 {
8311 switch (AT_class (a))
8312 {
8313 case dw_val_class_addr:
8314 size += DWARF2_ADDR_SIZE;
8315 break;
8316 case dw_val_class_offset:
8317 size += DWARF_OFFSET_SIZE;
8318 break;
8319 case dw_val_class_loc:
8320 {
8321 unsigned long lsize = size_of_locs (AT_loc (a));
8322
8323 /* Block length. */
8324 size += constant_size (lsize);
8325 size += lsize;
8326 }
8327 break;
8328 case dw_val_class_loc_list:
8329 size += DWARF_OFFSET_SIZE;
8330 break;
8331 case dw_val_class_range_list:
8332 size += DWARF_OFFSET_SIZE;
8333 break;
8334 case dw_val_class_const:
8335 size += size_of_sleb128 (AT_int (a));
8336 break;
8337 case dw_val_class_unsigned_const:
8338 size += constant_size (AT_unsigned (a));
8339 break;
8340 case dw_val_class_long_long:
8341 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
8342 break;
8343 case dw_val_class_vec:
8344 size += constant_size (a->dw_attr_val.v.val_vec.length
8345 * a->dw_attr_val.v.val_vec.elt_size)
8346 + a->dw_attr_val.v.val_vec.length
8347 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8348 break;
8349 case dw_val_class_flag:
8350 size += 1;
8351 break;
8352 case dw_val_class_die_ref:
8353 /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8354 whereas in DWARF3 it's always sized as an offset. */
8355 if (AT_ref_external (a) && dwarf_version == 2)
8356 size += DWARF2_ADDR_SIZE;
8357 else
8358 size += DWARF_OFFSET_SIZE;
8359 break;
8360 case dw_val_class_fde_ref:
8361 size += DWARF_OFFSET_SIZE;
8362 break;
8363 case dw_val_class_lbl_id:
8364 size += DWARF2_ADDR_SIZE;
8365 break;
8366 case dw_val_class_lineptr:
8367 case dw_val_class_macptr:
8368 size += DWARF_OFFSET_SIZE;
8369 break;
8370 case dw_val_class_str:
8371 if (AT_string_form (a) == DW_FORM_strp)
8372 size += DWARF_OFFSET_SIZE;
8373 else
8374 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8375 break;
8376 case dw_val_class_file:
8377 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8378 break;
8379 default:
8380 gcc_unreachable ();
8381 }
8382 }
8383
8384 return size;
8385 }
8386
8387 /* Size the debugging information associated with a given DIE. Visits the
8388 DIE's children recursively. Updates the global variable next_die_offset, on
8389 each time through. Uses the current value of next_die_offset to update the
8390 die_offset field in each DIE. */
8391
8392 static void
8393 calc_die_sizes (dw_die_ref die)
8394 {
8395 dw_die_ref c;
8396
8397 die->die_offset = next_die_offset;
8398 next_die_offset += size_of_die (die);
8399
8400 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8401
8402 if (die->die_child != NULL)
8403 /* Count the null byte used to terminate sibling lists. */
8404 next_die_offset += 1;
8405 }
8406
8407 /* Set the marks for a die and its children. We do this so
8408 that we know whether or not a reference needs to use FORM_ref_addr; only
8409 DIEs in the same CU will be marked. We used to clear out the offset
8410 and use that as the flag, but ran into ordering problems. */
8411
8412 static void
8413 mark_dies (dw_die_ref die)
8414 {
8415 dw_die_ref c;
8416
8417 gcc_assert (!die->die_mark);
8418
8419 die->die_mark = 1;
8420 FOR_EACH_CHILD (die, c, mark_dies (c));
8421 }
8422
8423 /* Clear the marks for a die and its children. */
8424
8425 static void
8426 unmark_dies (dw_die_ref die)
8427 {
8428 dw_die_ref c;
8429
8430 gcc_assert (die->die_mark);
8431
8432 die->die_mark = 0;
8433 FOR_EACH_CHILD (die, c, unmark_dies (c));
8434 }
8435
8436 /* Clear the marks for a die, its children and referred dies. */
8437
8438 static void
8439 unmark_all_dies (dw_die_ref die)
8440 {
8441 dw_die_ref c;
8442 dw_attr_ref a;
8443 unsigned ix;
8444
8445 if (!die->die_mark)
8446 return;
8447 die->die_mark = 0;
8448
8449 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8450
8451 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8452 if (AT_class (a) == dw_val_class_die_ref)
8453 unmark_all_dies (AT_ref (a));
8454 }
8455
8456 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8457 generated for the compilation unit. */
8458
8459 static unsigned long
8460 size_of_pubnames (VEC (pubname_entry, gc) * names)
8461 {
8462 unsigned long size;
8463 unsigned i;
8464 pubname_ref p;
8465
8466 size = DWARF_PUBNAMES_HEADER_SIZE;
8467 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8468 if (names != pubtype_table
8469 || p->die->die_offset != 0
8470 || !flag_eliminate_unused_debug_types)
8471 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8472
8473 size += DWARF_OFFSET_SIZE;
8474 return size;
8475 }
8476
8477 /* Return the size of the information in the .debug_aranges section. */
8478
8479 static unsigned long
8480 size_of_aranges (void)
8481 {
8482 unsigned long size;
8483
8484 size = DWARF_ARANGES_HEADER_SIZE;
8485
8486 /* Count the address/length pair for this compilation unit. */
8487 if (text_section_used)
8488 size += 2 * DWARF2_ADDR_SIZE;
8489 if (cold_text_section_used)
8490 size += 2 * DWARF2_ADDR_SIZE;
8491 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8492
8493 /* Count the two zero words used to terminated the address range table. */
8494 size += 2 * DWARF2_ADDR_SIZE;
8495 return size;
8496 }
8497 \f
8498 /* Select the encoding of an attribute value. */
8499
8500 static enum dwarf_form
8501 value_format (dw_attr_ref a)
8502 {
8503 switch (a->dw_attr_val.val_class)
8504 {
8505 case dw_val_class_addr:
8506 return DW_FORM_addr;
8507 case dw_val_class_range_list:
8508 case dw_val_class_offset:
8509 case dw_val_class_loc_list:
8510 switch (DWARF_OFFSET_SIZE)
8511 {
8512 case 4:
8513 return DW_FORM_data4;
8514 case 8:
8515 return DW_FORM_data8;
8516 default:
8517 gcc_unreachable ();
8518 }
8519 case dw_val_class_loc:
8520 switch (constant_size (size_of_locs (AT_loc (a))))
8521 {
8522 case 1:
8523 return DW_FORM_block1;
8524 case 2:
8525 return DW_FORM_block2;
8526 default:
8527 gcc_unreachable ();
8528 }
8529 case dw_val_class_const:
8530 return DW_FORM_sdata;
8531 case dw_val_class_unsigned_const:
8532 switch (constant_size (AT_unsigned (a)))
8533 {
8534 case 1:
8535 return DW_FORM_data1;
8536 case 2:
8537 return DW_FORM_data2;
8538 case 4:
8539 return DW_FORM_data4;
8540 case 8:
8541 return DW_FORM_data8;
8542 default:
8543 gcc_unreachable ();
8544 }
8545 case dw_val_class_long_long:
8546 return DW_FORM_block1;
8547 case dw_val_class_vec:
8548 switch (constant_size (a->dw_attr_val.v.val_vec.length
8549 * a->dw_attr_val.v.val_vec.elt_size))
8550 {
8551 case 1:
8552 return DW_FORM_block1;
8553 case 2:
8554 return DW_FORM_block2;
8555 case 4:
8556 return DW_FORM_block4;
8557 default:
8558 gcc_unreachable ();
8559 }
8560 case dw_val_class_flag:
8561 return DW_FORM_flag;
8562 case dw_val_class_die_ref:
8563 if (AT_ref_external (a))
8564 return DW_FORM_ref_addr;
8565 else
8566 return DW_FORM_ref;
8567 case dw_val_class_fde_ref:
8568 return DW_FORM_data;
8569 case dw_val_class_lbl_id:
8570 return DW_FORM_addr;
8571 case dw_val_class_lineptr:
8572 case dw_val_class_macptr:
8573 return DW_FORM_data;
8574 case dw_val_class_str:
8575 return AT_string_form (a);
8576 case dw_val_class_file:
8577 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8578 {
8579 case 1:
8580 return DW_FORM_data1;
8581 case 2:
8582 return DW_FORM_data2;
8583 case 4:
8584 return DW_FORM_data4;
8585 default:
8586 gcc_unreachable ();
8587 }
8588
8589 default:
8590 gcc_unreachable ();
8591 }
8592 }
8593
8594 /* Output the encoding of an attribute value. */
8595
8596 static void
8597 output_value_format (dw_attr_ref a)
8598 {
8599 enum dwarf_form form = value_format (a);
8600
8601 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8602 }
8603
8604 /* Output the .debug_abbrev section which defines the DIE abbreviation
8605 table. */
8606
8607 static void
8608 output_abbrev_section (void)
8609 {
8610 unsigned long abbrev_id;
8611
8612 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8613 {
8614 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8615 unsigned ix;
8616 dw_attr_ref a_attr;
8617
8618 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8619 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8620 dwarf_tag_name (abbrev->die_tag));
8621
8622 if (abbrev->die_child != NULL)
8623 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8624 else
8625 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8626
8627 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8628 ix++)
8629 {
8630 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8631 dwarf_attr_name (a_attr->dw_attr));
8632 output_value_format (a_attr);
8633 }
8634
8635 dw2_asm_output_data (1, 0, NULL);
8636 dw2_asm_output_data (1, 0, NULL);
8637 }
8638
8639 /* Terminate the table. */
8640 dw2_asm_output_data (1, 0, NULL);
8641 }
8642
8643 /* Output a symbol we can use to refer to this DIE from another CU. */
8644
8645 static inline void
8646 output_die_symbol (dw_die_ref die)
8647 {
8648 char *sym = die->die_symbol;
8649
8650 if (sym == 0)
8651 return;
8652
8653 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8654 /* We make these global, not weak; if the target doesn't support
8655 .linkonce, it doesn't support combining the sections, so debugging
8656 will break. */
8657 targetm.asm_out.globalize_label (asm_out_file, sym);
8658
8659 ASM_OUTPUT_LABEL (asm_out_file, sym);
8660 }
8661
8662 /* Return a new location list, given the begin and end range, and the
8663 expression. gensym tells us whether to generate a new internal symbol for
8664 this location list node, which is done for the head of the list only. */
8665
8666 static inline dw_loc_list_ref
8667 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8668 const char *section, unsigned int gensym)
8669 {
8670 dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8671
8672 retlist->begin = begin;
8673 retlist->end = end;
8674 retlist->expr = expr;
8675 retlist->section = section;
8676 if (gensym)
8677 retlist->ll_symbol = gen_internal_sym ("LLST");
8678
8679 return retlist;
8680 }
8681
8682 /* Add a location description expression to a location list. */
8683
8684 static inline void
8685 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8686 const char *begin, const char *end,
8687 const char *section)
8688 {
8689 dw_loc_list_ref *d;
8690
8691 /* Find the end of the chain. */
8692 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8693 ;
8694
8695 /* Add a new location list node to the list. */
8696 *d = new_loc_list (descr, begin, end, section, 0);
8697 }
8698
8699 /* Output the location list given to us. */
8700
8701 static void
8702 output_loc_list (dw_loc_list_ref list_head)
8703 {
8704 dw_loc_list_ref curr = list_head;
8705
8706 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8707
8708 /* Walk the location list, and output each range + expression. */
8709 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8710 {
8711 unsigned long size;
8712 /* Don't output an entry that starts and ends at the same address. */
8713 if (strcmp (curr->begin, curr->end) == 0)
8714 continue;
8715 if (!have_multiple_function_sections)
8716 {
8717 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8718 "Location list begin address (%s)",
8719 list_head->ll_symbol);
8720 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8721 "Location list end address (%s)",
8722 list_head->ll_symbol);
8723 }
8724 else
8725 {
8726 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8727 "Location list begin address (%s)",
8728 list_head->ll_symbol);
8729 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8730 "Location list end address (%s)",
8731 list_head->ll_symbol);
8732 }
8733 size = size_of_locs (curr->expr);
8734
8735 /* Output the block length for this list of location operations. */
8736 gcc_assert (size <= 0xffff);
8737 dw2_asm_output_data (2, size, "%s", "Location expression size");
8738
8739 output_loc_sequence (curr->expr);
8740 }
8741
8742 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8743 "Location list terminator begin (%s)",
8744 list_head->ll_symbol);
8745 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8746 "Location list terminator end (%s)",
8747 list_head->ll_symbol);
8748 }
8749
8750 /* Output the DIE and its attributes. Called recursively to generate
8751 the definitions of each child DIE. */
8752
8753 static void
8754 output_die (dw_die_ref die)
8755 {
8756 dw_attr_ref a;
8757 dw_die_ref c;
8758 unsigned long size;
8759 unsigned ix;
8760
8761 /* If someone in another CU might refer to us, set up a symbol for
8762 them to point to. */
8763 if (die->die_symbol)
8764 output_die_symbol (die);
8765
8766 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8767 (unsigned long)die->die_offset,
8768 dwarf_tag_name (die->die_tag));
8769
8770 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8771 {
8772 const char *name = dwarf_attr_name (a->dw_attr);
8773
8774 switch (AT_class (a))
8775 {
8776 case dw_val_class_addr:
8777 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8778 break;
8779
8780 case dw_val_class_offset:
8781 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8782 "%s", name);
8783 break;
8784
8785 case dw_val_class_range_list:
8786 {
8787 char *p = strchr (ranges_section_label, '\0');
8788
8789 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8790 a->dw_attr_val.v.val_offset);
8791 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8792 debug_ranges_section, "%s", name);
8793 *p = '\0';
8794 }
8795 break;
8796
8797 case dw_val_class_loc:
8798 size = size_of_locs (AT_loc (a));
8799
8800 /* Output the block length for this list of location operations. */
8801 dw2_asm_output_data (constant_size (size), size, "%s", name);
8802
8803 output_loc_sequence (AT_loc (a));
8804 break;
8805
8806 case dw_val_class_const:
8807 /* ??? It would be slightly more efficient to use a scheme like is
8808 used for unsigned constants below, but gdb 4.x does not sign
8809 extend. Gdb 5.x does sign extend. */
8810 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8811 break;
8812
8813 case dw_val_class_unsigned_const:
8814 dw2_asm_output_data (constant_size (AT_unsigned (a)),
8815 AT_unsigned (a), "%s", name);
8816 break;
8817
8818 case dw_val_class_long_long:
8819 {
8820 unsigned HOST_WIDE_INT first, second;
8821
8822 dw2_asm_output_data (1,
8823 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8824 "%s", name);
8825
8826 if (WORDS_BIG_ENDIAN)
8827 {
8828 first = a->dw_attr_val.v.val_long_long.hi;
8829 second = a->dw_attr_val.v.val_long_long.low;
8830 }
8831 else
8832 {
8833 first = a->dw_attr_val.v.val_long_long.low;
8834 second = a->dw_attr_val.v.val_long_long.hi;
8835 }
8836
8837 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8838 first, "long long constant");
8839 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8840 second, NULL);
8841 }
8842 break;
8843
8844 case dw_val_class_vec:
8845 {
8846 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8847 unsigned int len = a->dw_attr_val.v.val_vec.length;
8848 unsigned int i;
8849 unsigned char *p;
8850
8851 dw2_asm_output_data (constant_size (len * elt_size),
8852 len * elt_size, "%s", name);
8853 if (elt_size > sizeof (HOST_WIDE_INT))
8854 {
8855 elt_size /= 2;
8856 len *= 2;
8857 }
8858 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8859 i < len;
8860 i++, p += elt_size)
8861 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8862 "fp or vector constant word %u", i);
8863 break;
8864 }
8865
8866 case dw_val_class_flag:
8867 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8868 break;
8869
8870 case dw_val_class_loc_list:
8871 {
8872 char *sym = AT_loc_list (a)->ll_symbol;
8873
8874 gcc_assert (sym);
8875 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8876 "%s", name);
8877 }
8878 break;
8879
8880 case dw_val_class_die_ref:
8881 if (AT_ref_external (a))
8882 {
8883 char *sym = AT_ref (a)->die_symbol;
8884 int size;
8885
8886 gcc_assert (sym);
8887
8888 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8889 length, whereas in DWARF3 it's always sized as an offset. */
8890 if (dwarf_version == 2)
8891 size = DWARF2_ADDR_SIZE;
8892 else
8893 size = DWARF_OFFSET_SIZE;
8894 dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
8895 }
8896 else
8897 {
8898 gcc_assert (AT_ref (a)->die_offset);
8899 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8900 "%s", name);
8901 }
8902 break;
8903
8904 case dw_val_class_fde_ref:
8905 {
8906 char l1[20];
8907
8908 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8909 a->dw_attr_val.v.val_fde_index * 2);
8910 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8911 "%s", name);
8912 }
8913 break;
8914
8915 case dw_val_class_lbl_id:
8916 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8917 break;
8918
8919 case dw_val_class_lineptr:
8920 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8921 debug_line_section, "%s", name);
8922 break;
8923
8924 case dw_val_class_macptr:
8925 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8926 debug_macinfo_section, "%s", name);
8927 break;
8928
8929 case dw_val_class_str:
8930 if (AT_string_form (a) == DW_FORM_strp)
8931 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8932 a->dw_attr_val.v.val_str->label,
8933 debug_str_section,
8934 "%s: \"%s\"", name, AT_string (a));
8935 else
8936 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8937 break;
8938
8939 case dw_val_class_file:
8940 {
8941 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8942
8943 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8944 a->dw_attr_val.v.val_file->filename);
8945 break;
8946 }
8947
8948 default:
8949 gcc_unreachable ();
8950 }
8951 }
8952
8953 FOR_EACH_CHILD (die, c, output_die (c));
8954
8955 /* Add null byte to terminate sibling list. */
8956 if (die->die_child != NULL)
8957 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8958 (unsigned long) die->die_offset);
8959 }
8960
8961 /* Output the compilation unit that appears at the beginning of the
8962 .debug_info section, and precedes the DIE descriptions. */
8963
8964 static void
8965 output_compilation_unit_header (void)
8966 {
8967 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8968 dw2_asm_output_data (4, 0xffffffff,
8969 "Initial length escape value indicating 64-bit DWARF extension");
8970 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8971 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8972 "Length of Compilation Unit Info");
8973 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8974 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8975 debug_abbrev_section,
8976 "Offset Into Abbrev. Section");
8977 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8978 }
8979
8980 /* Output the compilation unit DIE and its children. */
8981
8982 static void
8983 output_comp_unit (dw_die_ref die, int output_if_empty)
8984 {
8985 const char *secname;
8986 char *oldsym, *tmp;
8987
8988 /* Unless we are outputting main CU, we may throw away empty ones. */
8989 if (!output_if_empty && die->die_child == NULL)
8990 return;
8991
8992 /* Even if there are no children of this DIE, we must output the information
8993 about the compilation unit. Otherwise, on an empty translation unit, we
8994 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8995 will then complain when examining the file. First mark all the DIEs in
8996 this CU so we know which get local refs. */
8997 mark_dies (die);
8998
8999 build_abbrev_table (die);
9000
9001 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9002 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9003 calc_die_sizes (die);
9004
9005 oldsym = die->die_symbol;
9006 if (oldsym)
9007 {
9008 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9009
9010 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9011 secname = tmp;
9012 die->die_symbol = NULL;
9013 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9014 }
9015 else
9016 switch_to_section (debug_info_section);
9017
9018 /* Output debugging information. */
9019 output_compilation_unit_header ();
9020 output_die (die);
9021
9022 /* Leave the marks on the main CU, so we can check them in
9023 output_pubnames. */
9024 if (oldsym)
9025 {
9026 unmark_dies (die);
9027 die->die_symbol = oldsym;
9028 }
9029 }
9030
9031 /* Return the DWARF2/3 pubname associated with a decl. */
9032
9033 static const char *
9034 dwarf2_name (tree decl, int scope)
9035 {
9036 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9037 }
9038
9039 /* Add a new entry to .debug_pubnames if appropriate. */
9040
9041 static void
9042 add_pubname_string (const char *str, dw_die_ref die)
9043 {
9044 pubname_entry e;
9045
9046 e.die = die;
9047 e.name = xstrdup (str);
9048 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9049 }
9050
9051 static void
9052 add_pubname (tree decl, dw_die_ref die)
9053 {
9054 if (TREE_PUBLIC (decl))
9055 add_pubname_string (dwarf2_name (decl, 1), die);
9056 }
9057
9058 /* Add a new entry to .debug_pubtypes if appropriate. */
9059
9060 static void
9061 add_pubtype (tree decl, dw_die_ref die)
9062 {
9063 pubname_entry e;
9064
9065 e.name = NULL;
9066 if ((TREE_PUBLIC (decl)
9067 || die->die_parent == comp_unit_die)
9068 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9069 {
9070 e.die = die;
9071 if (TYPE_P (decl))
9072 {
9073 if (TYPE_NAME (decl))
9074 {
9075 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9076 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9077 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9078 && DECL_NAME (TYPE_NAME (decl)))
9079 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9080 else
9081 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9082 }
9083 }
9084 else
9085 e.name = xstrdup (dwarf2_name (decl, 1));
9086
9087 /* If we don't have a name for the type, there's no point in adding
9088 it to the table. */
9089 if (e.name && e.name[0] != '\0')
9090 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9091 }
9092 }
9093
9094 /* Output the public names table used to speed up access to externally
9095 visible names; or the public types table used to find type definitions. */
9096
9097 static void
9098 output_pubnames (VEC (pubname_entry, gc) * names)
9099 {
9100 unsigned i;
9101 unsigned long pubnames_length = size_of_pubnames (names);
9102 pubname_ref pub;
9103
9104 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9105 dw2_asm_output_data (4, 0xffffffff,
9106 "Initial length escape value indicating 64-bit DWARF extension");
9107 if (names == pubname_table)
9108 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9109 "Length of Public Names Info");
9110 else
9111 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9112 "Length of Public Type Names Info");
9113 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
9114 dw2_asm_output_data (2, 2, "DWARF Version");
9115 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9116 debug_info_section,
9117 "Offset of Compilation Unit Info");
9118 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9119 "Compilation Unit Length");
9120
9121 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9122 {
9123 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9124 if (names == pubname_table)
9125 gcc_assert (pub->die->die_mark);
9126
9127 if (names != pubtype_table
9128 || pub->die->die_offset != 0
9129 || !flag_eliminate_unused_debug_types)
9130 {
9131 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9132 "DIE offset");
9133
9134 dw2_asm_output_nstring (pub->name, -1, "external name");
9135 }
9136 }
9137
9138 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9139 }
9140
9141 /* Add a new entry to .debug_aranges if appropriate. */
9142
9143 static void
9144 add_arange (tree decl, dw_die_ref die)
9145 {
9146 if (! DECL_SECTION_NAME (decl))
9147 return;
9148
9149 if (arange_table_in_use == arange_table_allocated)
9150 {
9151 arange_table_allocated += ARANGE_TABLE_INCREMENT;
9152 arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9153 arange_table_allocated);
9154 memset (arange_table + arange_table_in_use, 0,
9155 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9156 }
9157
9158 arange_table[arange_table_in_use++] = die;
9159 }
9160
9161 /* Output the information that goes into the .debug_aranges table.
9162 Namely, define the beginning and ending address range of the
9163 text section generated for this compilation unit. */
9164
9165 static void
9166 output_aranges (void)
9167 {
9168 unsigned i;
9169 unsigned long aranges_length = size_of_aranges ();
9170
9171 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9172 dw2_asm_output_data (4, 0xffffffff,
9173 "Initial length escape value indicating 64-bit DWARF extension");
9174 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9175 "Length of Address Ranges Info");
9176 /* Version number for aranges is still 2, even in DWARF3. */
9177 dw2_asm_output_data (2, 2, "DWARF Version");
9178 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9179 debug_info_section,
9180 "Offset of Compilation Unit Info");
9181 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9182 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9183
9184 /* We need to align to twice the pointer size here. */
9185 if (DWARF_ARANGES_PAD_SIZE)
9186 {
9187 /* Pad using a 2 byte words so that padding is correct for any
9188 pointer size. */
9189 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9190 2 * DWARF2_ADDR_SIZE);
9191 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9192 dw2_asm_output_data (2, 0, NULL);
9193 }
9194
9195 /* It is necessary not to output these entries if the sections were
9196 not used; if the sections were not used, the length will be 0 and
9197 the address may end up as 0 if the section is discarded by ld
9198 --gc-sections, leaving an invalid (0, 0) entry that can be
9199 confused with the terminator. */
9200 if (text_section_used)
9201 {
9202 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9203 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9204 text_section_label, "Length");
9205 }
9206 if (cold_text_section_used)
9207 {
9208 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9209 "Address");
9210 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9211 cold_text_section_label, "Length");
9212 }
9213
9214 for (i = 0; i < arange_table_in_use; i++)
9215 {
9216 dw_die_ref die = arange_table[i];
9217
9218 /* We shouldn't see aranges for DIEs outside of the main CU. */
9219 gcc_assert (die->die_mark);
9220
9221 if (die->die_tag == DW_TAG_subprogram)
9222 {
9223 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9224 "Address");
9225 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9226 get_AT_low_pc (die), "Length");
9227 }
9228 else
9229 {
9230 /* A static variable; extract the symbol from DW_AT_location.
9231 Note that this code isn't currently hit, as we only emit
9232 aranges for functions (jason 9/23/99). */
9233 dw_attr_ref a = get_AT (die, DW_AT_location);
9234 dw_loc_descr_ref loc;
9235
9236 gcc_assert (a && AT_class (a) == dw_val_class_loc);
9237
9238 loc = AT_loc (a);
9239 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9240
9241 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9242 loc->dw_loc_oprnd1.v.val_addr, "Address");
9243 dw2_asm_output_data (DWARF2_ADDR_SIZE,
9244 get_AT_unsigned (die, DW_AT_byte_size),
9245 "Length");
9246 }
9247 }
9248
9249 /* Output the terminator words. */
9250 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9251 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9252 }
9253
9254 /* Add a new entry to .debug_ranges. Return the offset at which it
9255 was placed. */
9256
9257 static unsigned int
9258 add_ranges_num (int num)
9259 {
9260 unsigned int in_use = ranges_table_in_use;
9261
9262 if (in_use == ranges_table_allocated)
9263 {
9264 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9265 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9266 ranges_table_allocated);
9267 memset (ranges_table + ranges_table_in_use, 0,
9268 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9269 }
9270
9271 ranges_table[in_use].num = num;
9272 ranges_table_in_use = in_use + 1;
9273
9274 return in_use * 2 * DWARF2_ADDR_SIZE;
9275 }
9276
9277 /* Add a new entry to .debug_ranges corresponding to a block, or a
9278 range terminator if BLOCK is NULL. */
9279
9280 static unsigned int
9281 add_ranges (const_tree block)
9282 {
9283 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9284 }
9285
9286 /* Add a new entry to .debug_ranges corresponding to a pair of
9287 labels. */
9288
9289 static unsigned int
9290 add_ranges_by_labels (const char *begin, const char *end)
9291 {
9292 unsigned int in_use = ranges_by_label_in_use;
9293
9294 if (in_use == ranges_by_label_allocated)
9295 {
9296 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9297 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9298 ranges_by_label,
9299 ranges_by_label_allocated);
9300 memset (ranges_by_label + ranges_by_label_in_use, 0,
9301 RANGES_TABLE_INCREMENT
9302 * sizeof (struct dw_ranges_by_label_struct));
9303 }
9304
9305 ranges_by_label[in_use].begin = begin;
9306 ranges_by_label[in_use].end = end;
9307 ranges_by_label_in_use = in_use + 1;
9308
9309 return add_ranges_num (-(int)in_use - 1);
9310 }
9311
9312 static void
9313 output_ranges (void)
9314 {
9315 unsigned i;
9316 static const char *const start_fmt = "Offset 0x%x";
9317 const char *fmt = start_fmt;
9318
9319 for (i = 0; i < ranges_table_in_use; i++)
9320 {
9321 int block_num = ranges_table[i].num;
9322
9323 if (block_num > 0)
9324 {
9325 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9326 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9327
9328 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9329 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9330
9331 /* If all code is in the text section, then the compilation
9332 unit base address defaults to DW_AT_low_pc, which is the
9333 base of the text section. */
9334 if (!have_multiple_function_sections)
9335 {
9336 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9337 text_section_label,
9338 fmt, i * 2 * DWARF2_ADDR_SIZE);
9339 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9340 text_section_label, NULL);
9341 }
9342
9343 /* Otherwise, the compilation unit base address is zero,
9344 which allows us to use absolute addresses, and not worry
9345 about whether the target supports cross-section
9346 arithmetic. */
9347 else
9348 {
9349 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9350 fmt, i * 2 * DWARF2_ADDR_SIZE);
9351 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9352 }
9353
9354 fmt = NULL;
9355 }
9356
9357 /* Negative block_num stands for an index into ranges_by_label. */
9358 else if (block_num < 0)
9359 {
9360 int lab_idx = - block_num - 1;
9361
9362 if (!have_multiple_function_sections)
9363 {
9364 gcc_unreachable ();
9365 #if 0
9366 /* If we ever use add_ranges_by_labels () for a single
9367 function section, all we have to do is to take out
9368 the #if 0 above. */
9369 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9370 ranges_by_label[lab_idx].begin,
9371 text_section_label,
9372 fmt, i * 2 * DWARF2_ADDR_SIZE);
9373 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9374 ranges_by_label[lab_idx].end,
9375 text_section_label, NULL);
9376 #endif
9377 }
9378 else
9379 {
9380 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9381 ranges_by_label[lab_idx].begin,
9382 fmt, i * 2 * DWARF2_ADDR_SIZE);
9383 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9384 ranges_by_label[lab_idx].end,
9385 NULL);
9386 }
9387 }
9388 else
9389 {
9390 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9391 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9392 fmt = start_fmt;
9393 }
9394 }
9395 }
9396
9397 /* Data structure containing information about input files. */
9398 struct file_info
9399 {
9400 const char *path; /* Complete file name. */
9401 const char *fname; /* File name part. */
9402 int length; /* Length of entire string. */
9403 struct dwarf_file_data * file_idx; /* Index in input file table. */
9404 int dir_idx; /* Index in directory table. */
9405 };
9406
9407 /* Data structure containing information about directories with source
9408 files. */
9409 struct dir_info
9410 {
9411 const char *path; /* Path including directory name. */
9412 int length; /* Path length. */
9413 int prefix; /* Index of directory entry which is a prefix. */
9414 int count; /* Number of files in this directory. */
9415 int dir_idx; /* Index of directory used as base. */
9416 };
9417
9418 /* Callback function for file_info comparison. We sort by looking at
9419 the directories in the path. */
9420
9421 static int
9422 file_info_cmp (const void *p1, const void *p2)
9423 {
9424 const struct file_info *const s1 = (const struct file_info *) p1;
9425 const struct file_info *const s2 = (const struct file_info *) p2;
9426 const unsigned char *cp1;
9427 const unsigned char *cp2;
9428
9429 /* Take care of file names without directories. We need to make sure that
9430 we return consistent values to qsort since some will get confused if
9431 we return the same value when identical operands are passed in opposite
9432 orders. So if neither has a directory, return 0 and otherwise return
9433 1 or -1 depending on which one has the directory. */
9434 if ((s1->path == s1->fname || s2->path == s2->fname))
9435 return (s2->path == s2->fname) - (s1->path == s1->fname);
9436
9437 cp1 = (const unsigned char *) s1->path;
9438 cp2 = (const unsigned char *) s2->path;
9439
9440 while (1)
9441 {
9442 ++cp1;
9443 ++cp2;
9444 /* Reached the end of the first path? If so, handle like above. */
9445 if ((cp1 == (const unsigned char *) s1->fname)
9446 || (cp2 == (const unsigned char *) s2->fname))
9447 return ((cp2 == (const unsigned char *) s2->fname)
9448 - (cp1 == (const unsigned char *) s1->fname));
9449
9450 /* Character of current path component the same? */
9451 else if (*cp1 != *cp2)
9452 return *cp1 - *cp2;
9453 }
9454 }
9455
9456 struct file_name_acquire_data
9457 {
9458 struct file_info *files;
9459 int used_files;
9460 int max_files;
9461 };
9462
9463 /* Traversal function for the hash table. */
9464
9465 static int
9466 file_name_acquire (void ** slot, void *data)
9467 {
9468 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9469 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9470 struct file_info *fi;
9471 const char *f;
9472
9473 gcc_assert (fnad->max_files >= d->emitted_number);
9474
9475 if (! d->emitted_number)
9476 return 1;
9477
9478 gcc_assert (fnad->max_files != fnad->used_files);
9479
9480 fi = fnad->files + fnad->used_files++;
9481
9482 /* Skip all leading "./". */
9483 f = d->filename;
9484 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9485 f += 2;
9486
9487 /* Create a new array entry. */
9488 fi->path = f;
9489 fi->length = strlen (f);
9490 fi->file_idx = d;
9491
9492 /* Search for the file name part. */
9493 f = strrchr (f, DIR_SEPARATOR);
9494 #if defined (DIR_SEPARATOR_2)
9495 {
9496 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9497
9498 if (g != NULL)
9499 {
9500 if (f == NULL || f < g)
9501 f = g;
9502 }
9503 }
9504 #endif
9505
9506 fi->fname = f == NULL ? fi->path : f + 1;
9507 return 1;
9508 }
9509
9510 /* Output the directory table and the file name table. We try to minimize
9511 the total amount of memory needed. A heuristic is used to avoid large
9512 slowdowns with many input files. */
9513
9514 static void
9515 output_file_names (void)
9516 {
9517 struct file_name_acquire_data fnad;
9518 int numfiles;
9519 struct file_info *files;
9520 struct dir_info *dirs;
9521 int *saved;
9522 int *savehere;
9523 int *backmap;
9524 int ndirs;
9525 int idx_offset;
9526 int i;
9527 int idx;
9528
9529 if (!last_emitted_file)
9530 {
9531 dw2_asm_output_data (1, 0, "End directory table");
9532 dw2_asm_output_data (1, 0, "End file name table");
9533 return;
9534 }
9535
9536 numfiles = last_emitted_file->emitted_number;
9537
9538 /* Allocate the various arrays we need. */
9539 files = XALLOCAVEC (struct file_info, numfiles);
9540 dirs = XALLOCAVEC (struct dir_info, numfiles);
9541
9542 fnad.files = files;
9543 fnad.used_files = 0;
9544 fnad.max_files = numfiles;
9545 htab_traverse (file_table, file_name_acquire, &fnad);
9546 gcc_assert (fnad.used_files == fnad.max_files);
9547
9548 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9549
9550 /* Find all the different directories used. */
9551 dirs[0].path = files[0].path;
9552 dirs[0].length = files[0].fname - files[0].path;
9553 dirs[0].prefix = -1;
9554 dirs[0].count = 1;
9555 dirs[0].dir_idx = 0;
9556 files[0].dir_idx = 0;
9557 ndirs = 1;
9558
9559 for (i = 1; i < numfiles; i++)
9560 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9561 && memcmp (dirs[ndirs - 1].path, files[i].path,
9562 dirs[ndirs - 1].length) == 0)
9563 {
9564 /* Same directory as last entry. */
9565 files[i].dir_idx = ndirs - 1;
9566 ++dirs[ndirs - 1].count;
9567 }
9568 else
9569 {
9570 int j;
9571
9572 /* This is a new directory. */
9573 dirs[ndirs].path = files[i].path;
9574 dirs[ndirs].length = files[i].fname - files[i].path;
9575 dirs[ndirs].count = 1;
9576 dirs[ndirs].dir_idx = ndirs;
9577 files[i].dir_idx = ndirs;
9578
9579 /* Search for a prefix. */
9580 dirs[ndirs].prefix = -1;
9581 for (j = 0; j < ndirs; j++)
9582 if (dirs[j].length < dirs[ndirs].length
9583 && dirs[j].length > 1
9584 && (dirs[ndirs].prefix == -1
9585 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9586 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9587 dirs[ndirs].prefix = j;
9588
9589 ++ndirs;
9590 }
9591
9592 /* Now to the actual work. We have to find a subset of the directories which
9593 allow expressing the file name using references to the directory table
9594 with the least amount of characters. We do not do an exhaustive search
9595 where we would have to check out every combination of every single
9596 possible prefix. Instead we use a heuristic which provides nearly optimal
9597 results in most cases and never is much off. */
9598 saved = XALLOCAVEC (int, ndirs);
9599 savehere = XALLOCAVEC (int, ndirs);
9600
9601 memset (saved, '\0', ndirs * sizeof (saved[0]));
9602 for (i = 0; i < ndirs; i++)
9603 {
9604 int j;
9605 int total;
9606
9607 /* We can always save some space for the current directory. But this
9608 does not mean it will be enough to justify adding the directory. */
9609 savehere[i] = dirs[i].length;
9610 total = (savehere[i] - saved[i]) * dirs[i].count;
9611
9612 for (j = i + 1; j < ndirs; j++)
9613 {
9614 savehere[j] = 0;
9615 if (saved[j] < dirs[i].length)
9616 {
9617 /* Determine whether the dirs[i] path is a prefix of the
9618 dirs[j] path. */
9619 int k;
9620
9621 k = dirs[j].prefix;
9622 while (k != -1 && k != (int) i)
9623 k = dirs[k].prefix;
9624
9625 if (k == (int) i)
9626 {
9627 /* Yes it is. We can possibly save some memory by
9628 writing the filenames in dirs[j] relative to
9629 dirs[i]. */
9630 savehere[j] = dirs[i].length;
9631 total += (savehere[j] - saved[j]) * dirs[j].count;
9632 }
9633 }
9634 }
9635
9636 /* Check whether we can save enough to justify adding the dirs[i]
9637 directory. */
9638 if (total > dirs[i].length + 1)
9639 {
9640 /* It's worthwhile adding. */
9641 for (j = i; j < ndirs; j++)
9642 if (savehere[j] > 0)
9643 {
9644 /* Remember how much we saved for this directory so far. */
9645 saved[j] = savehere[j];
9646
9647 /* Remember the prefix directory. */
9648 dirs[j].dir_idx = i;
9649 }
9650 }
9651 }
9652
9653 /* Emit the directory name table. */
9654 idx = 1;
9655 idx_offset = dirs[0].length > 0 ? 1 : 0;
9656 for (i = 1 - idx_offset; i < ndirs; i++)
9657 dw2_asm_output_nstring (dirs[i].path,
9658 dirs[i].length
9659 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9660 "Directory Entry: 0x%x", i + idx_offset);
9661
9662 dw2_asm_output_data (1, 0, "End directory table");
9663
9664 /* We have to emit them in the order of emitted_number since that's
9665 used in the debug info generation. To do this efficiently we
9666 generate a back-mapping of the indices first. */
9667 backmap = XALLOCAVEC (int, numfiles);
9668 for (i = 0; i < numfiles; i++)
9669 backmap[files[i].file_idx->emitted_number - 1] = i;
9670
9671 /* Now write all the file names. */
9672 for (i = 0; i < numfiles; i++)
9673 {
9674 int file_idx = backmap[i];
9675 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9676
9677 #ifdef VMS_DEBUGGING_INFO
9678 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9679
9680 /* Setting these fields can lead to debugger miscomparisons,
9681 but VMS Debug requires them to be set correctly. */
9682
9683 int ver;
9684 long long cdt;
9685 long siz;
9686 int maxfilelen = strlen (files[file_idx].path)
9687 + dirs[dir_idx].length
9688 + MAX_VMS_VERSION_LEN + 1;
9689 char *filebuf = XALLOCAVEC (char, maxfilelen);
9690
9691 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9692 snprintf (filebuf, maxfilelen, "%s;%d",
9693 files[file_idx].path + dirs[dir_idx].length, ver);
9694
9695 dw2_asm_output_nstring
9696 (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9697
9698 /* Include directory index. */
9699 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9700
9701 /* Modification time. */
9702 dw2_asm_output_data_uleb128
9703 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9704 ? cdt : 0,
9705 NULL);
9706
9707 /* File length in bytes. */
9708 dw2_asm_output_data_uleb128
9709 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9710 ? siz : 0,
9711 NULL);
9712 #else
9713 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9714 "File Entry: 0x%x", (unsigned) i + 1);
9715
9716 /* Include directory index. */
9717 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9718
9719 /* Modification time. */
9720 dw2_asm_output_data_uleb128 (0, NULL);
9721
9722 /* File length in bytes. */
9723 dw2_asm_output_data_uleb128 (0, NULL);
9724 #endif
9725 }
9726
9727 dw2_asm_output_data (1, 0, "End file name table");
9728 }
9729
9730
9731 /* Output the source line number correspondence information. This
9732 information goes into the .debug_line section. */
9733
9734 static void
9735 output_line_info (void)
9736 {
9737 char l1[20], l2[20], p1[20], p2[20];
9738 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9739 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9740 unsigned opc;
9741 unsigned n_op_args;
9742 unsigned long lt_index;
9743 unsigned long current_line;
9744 long line_offset;
9745 long line_delta;
9746 unsigned long current_file;
9747 unsigned long function;
9748
9749 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9750 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9751 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9752 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9753
9754 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9755 dw2_asm_output_data (4, 0xffffffff,
9756 "Initial length escape value indicating 64-bit DWARF extension");
9757 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9758 "Length of Source Line Info");
9759 ASM_OUTPUT_LABEL (asm_out_file, l1);
9760
9761 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9762 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9763 ASM_OUTPUT_LABEL (asm_out_file, p1);
9764
9765 /* Define the architecture-dependent minimum instruction length (in
9766 bytes). In this implementation of DWARF, this field is used for
9767 information purposes only. Since GCC generates assembly language,
9768 we have no a priori knowledge of how many instruction bytes are
9769 generated for each source line, and therefore can use only the
9770 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9771 commands. Accordingly, we fix this as `1', which is "correct
9772 enough" for all architectures, and don't let the target override. */
9773 dw2_asm_output_data (1, 1,
9774 "Minimum Instruction Length");
9775
9776 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9777 "Default is_stmt_start flag");
9778 dw2_asm_output_data (1, DWARF_LINE_BASE,
9779 "Line Base Value (Special Opcodes)");
9780 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9781 "Line Range Value (Special Opcodes)");
9782 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9783 "Special Opcode Base");
9784
9785 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9786 {
9787 switch (opc)
9788 {
9789 case DW_LNS_advance_pc:
9790 case DW_LNS_advance_line:
9791 case DW_LNS_set_file:
9792 case DW_LNS_set_column:
9793 case DW_LNS_fixed_advance_pc:
9794 n_op_args = 1;
9795 break;
9796 default:
9797 n_op_args = 0;
9798 break;
9799 }
9800
9801 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9802 opc, n_op_args);
9803 }
9804
9805 /* Write out the information about the files we use. */
9806 output_file_names ();
9807 ASM_OUTPUT_LABEL (asm_out_file, p2);
9808
9809 /* We used to set the address register to the first location in the text
9810 section here, but that didn't accomplish anything since we already
9811 have a line note for the opening brace of the first function. */
9812
9813 /* Generate the line number to PC correspondence table, encoded as
9814 a series of state machine operations. */
9815 current_file = 1;
9816 current_line = 1;
9817
9818 if (cfun && in_cold_section_p)
9819 strcpy (prev_line_label, crtl->subsections.cold_section_label);
9820 else
9821 strcpy (prev_line_label, text_section_label);
9822 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9823 {
9824 dw_line_info_ref line_info = &line_info_table[lt_index];
9825
9826 #if 0
9827 /* Disable this optimization for now; GDB wants to see two line notes
9828 at the beginning of a function so it can find the end of the
9829 prologue. */
9830
9831 /* Don't emit anything for redundant notes. Just updating the
9832 address doesn't accomplish anything, because we already assume
9833 that anything after the last address is this line. */
9834 if (line_info->dw_line_num == current_line
9835 && line_info->dw_file_num == current_file)
9836 continue;
9837 #endif
9838
9839 /* Emit debug info for the address of the current line.
9840
9841 Unfortunately, we have little choice here currently, and must always
9842 use the most general form. GCC does not know the address delta
9843 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
9844 attributes which will give an upper bound on the address range. We
9845 could perhaps use length attributes to determine when it is safe to
9846 use DW_LNS_fixed_advance_pc. */
9847
9848 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9849 if (0)
9850 {
9851 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
9852 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9853 "DW_LNS_fixed_advance_pc");
9854 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9855 }
9856 else
9857 {
9858 /* This can handle any delta. This takes
9859 4+DWARF2_ADDR_SIZE bytes. */
9860 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9861 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9862 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9863 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9864 }
9865
9866 strcpy (prev_line_label, line_label);
9867
9868 /* Emit debug info for the source file of the current line, if
9869 different from the previous line. */
9870 if (line_info->dw_file_num != current_file)
9871 {
9872 current_file = line_info->dw_file_num;
9873 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9874 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9875 }
9876
9877 /* Emit debug info for the current line number, choosing the encoding
9878 that uses the least amount of space. */
9879 if (line_info->dw_line_num != current_line)
9880 {
9881 line_offset = line_info->dw_line_num - current_line;
9882 line_delta = line_offset - DWARF_LINE_BASE;
9883 current_line = line_info->dw_line_num;
9884 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9885 /* This can handle deltas from -10 to 234, using the current
9886 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
9887 takes 1 byte. */
9888 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9889 "line %lu", current_line);
9890 else
9891 {
9892 /* This can handle any delta. This takes at least 4 bytes,
9893 depending on the value being encoded. */
9894 dw2_asm_output_data (1, DW_LNS_advance_line,
9895 "advance to line %lu", current_line);
9896 dw2_asm_output_data_sleb128 (line_offset, NULL);
9897 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9898 }
9899 }
9900 else
9901 /* We still need to start a new row, so output a copy insn. */
9902 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9903 }
9904
9905 /* Emit debug info for the address of the end of the function. */
9906 if (0)
9907 {
9908 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9909 "DW_LNS_fixed_advance_pc");
9910 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9911 }
9912 else
9913 {
9914 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9915 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9916 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9917 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9918 }
9919
9920 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9921 dw2_asm_output_data_uleb128 (1, NULL);
9922 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9923
9924 function = 0;
9925 current_file = 1;
9926 current_line = 1;
9927 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9928 {
9929 dw_separate_line_info_ref line_info
9930 = &separate_line_info_table[lt_index];
9931
9932 #if 0
9933 /* Don't emit anything for redundant notes. */
9934 if (line_info->dw_line_num == current_line
9935 && line_info->dw_file_num == current_file
9936 && line_info->function == function)
9937 goto cont;
9938 #endif
9939
9940 /* Emit debug info for the address of the current line. If this is
9941 a new function, or the first line of a function, then we need
9942 to handle it differently. */
9943 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9944 lt_index);
9945 if (function != line_info->function)
9946 {
9947 function = line_info->function;
9948
9949 /* Set the address register to the first line in the function. */
9950 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9951 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9952 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9953 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9954 }
9955 else
9956 {
9957 /* ??? See the DW_LNS_advance_pc comment above. */
9958 if (0)
9959 {
9960 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9961 "DW_LNS_fixed_advance_pc");
9962 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9963 }
9964 else
9965 {
9966 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9967 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9968 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9969 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9970 }
9971 }
9972
9973 strcpy (prev_line_label, line_label);
9974
9975 /* Emit debug info for the source file of the current line, if
9976 different from the previous line. */
9977 if (line_info->dw_file_num != current_file)
9978 {
9979 current_file = line_info->dw_file_num;
9980 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9981 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9982 }
9983
9984 /* Emit debug info for the current line number, choosing the encoding
9985 that uses the least amount of space. */
9986 if (line_info->dw_line_num != current_line)
9987 {
9988 line_offset = line_info->dw_line_num - current_line;
9989 line_delta = line_offset - DWARF_LINE_BASE;
9990 current_line = line_info->dw_line_num;
9991 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9992 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9993 "line %lu", current_line);
9994 else
9995 {
9996 dw2_asm_output_data (1, DW_LNS_advance_line,
9997 "advance to line %lu", current_line);
9998 dw2_asm_output_data_sleb128 (line_offset, NULL);
9999 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10000 }
10001 }
10002 else
10003 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10004
10005 #if 0
10006 cont:
10007 #endif
10008
10009 lt_index++;
10010
10011 /* If we're done with a function, end its sequence. */
10012 if (lt_index == separate_line_info_table_in_use
10013 || separate_line_info_table[lt_index].function != function)
10014 {
10015 current_file = 1;
10016 current_line = 1;
10017
10018 /* Emit debug info for the address of the end of the function. */
10019 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
10020 if (0)
10021 {
10022 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10023 "DW_LNS_fixed_advance_pc");
10024 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10025 }
10026 else
10027 {
10028 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10029 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10030 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10031 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10032 }
10033
10034 /* Output the marker for the end of this sequence. */
10035 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10036 dw2_asm_output_data_uleb128 (1, NULL);
10037 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10038 }
10039 }
10040
10041 /* Output the marker for the end of the line number info. */
10042 ASM_OUTPUT_LABEL (asm_out_file, l2);
10043 }
10044 \f
10045 /* Given a pointer to a tree node for some base type, return a pointer to
10046 a DIE that describes the given type.
10047
10048 This routine must only be called for GCC type nodes that correspond to
10049 Dwarf base (fundamental) types. */
10050
10051 static dw_die_ref
10052 base_type_die (tree type)
10053 {
10054 dw_die_ref base_type_result;
10055 enum dwarf_type encoding;
10056
10057 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10058 return 0;
10059
10060 /* If this is a subtype that should not be emitted as a subrange type,
10061 use the base type. See subrange_type_for_debug_p. */
10062 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10063 type = TREE_TYPE (type);
10064
10065 switch (TREE_CODE (type))
10066 {
10067 case INTEGER_TYPE:
10068 if (TYPE_STRING_FLAG (type))
10069 {
10070 if (TYPE_UNSIGNED (type))
10071 encoding = DW_ATE_unsigned_char;
10072 else
10073 encoding = DW_ATE_signed_char;
10074 }
10075 else if (TYPE_UNSIGNED (type))
10076 encoding = DW_ATE_unsigned;
10077 else
10078 encoding = DW_ATE_signed;
10079 break;
10080
10081 case REAL_TYPE:
10082 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10083 encoding = DW_ATE_decimal_float;
10084 else
10085 encoding = DW_ATE_float;
10086 break;
10087
10088 case FIXED_POINT_TYPE:
10089 if (TYPE_UNSIGNED (type))
10090 encoding = DW_ATE_unsigned_fixed;
10091 else
10092 encoding = DW_ATE_signed_fixed;
10093 break;
10094
10095 /* Dwarf2 doesn't know anything about complex ints, so use
10096 a user defined type for it. */
10097 case COMPLEX_TYPE:
10098 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10099 encoding = DW_ATE_complex_float;
10100 else
10101 encoding = DW_ATE_lo_user;
10102 break;
10103
10104 case BOOLEAN_TYPE:
10105 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10106 encoding = DW_ATE_boolean;
10107 break;
10108
10109 default:
10110 /* No other TREE_CODEs are Dwarf fundamental types. */
10111 gcc_unreachable ();
10112 }
10113
10114 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10115
10116 /* This probably indicates a bug. */
10117 if (! TYPE_NAME (type))
10118 add_name_attribute (base_type_result, "__unknown__");
10119
10120 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10121 int_size_in_bytes (type));
10122 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10123
10124 return base_type_result;
10125 }
10126
10127 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10128 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10129
10130 static inline int
10131 is_base_type (tree type)
10132 {
10133 switch (TREE_CODE (type))
10134 {
10135 case ERROR_MARK:
10136 case VOID_TYPE:
10137 case INTEGER_TYPE:
10138 case REAL_TYPE:
10139 case FIXED_POINT_TYPE:
10140 case COMPLEX_TYPE:
10141 case BOOLEAN_TYPE:
10142 return 1;
10143
10144 case ARRAY_TYPE:
10145 case RECORD_TYPE:
10146 case UNION_TYPE:
10147 case QUAL_UNION_TYPE:
10148 case ENUMERAL_TYPE:
10149 case FUNCTION_TYPE:
10150 case METHOD_TYPE:
10151 case POINTER_TYPE:
10152 case REFERENCE_TYPE:
10153 case OFFSET_TYPE:
10154 case LANG_TYPE:
10155 case VECTOR_TYPE:
10156 return 0;
10157
10158 default:
10159 gcc_unreachable ();
10160 }
10161
10162 return 0;
10163 }
10164
10165 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10166 node, return the size in bits for the type if it is a constant, or else
10167 return the alignment for the type if the type's size is not constant, or
10168 else return BITS_PER_WORD if the type actually turns out to be an
10169 ERROR_MARK node. */
10170
10171 static inline unsigned HOST_WIDE_INT
10172 simple_type_size_in_bits (const_tree type)
10173 {
10174 if (TREE_CODE (type) == ERROR_MARK)
10175 return BITS_PER_WORD;
10176 else if (TYPE_SIZE (type) == NULL_TREE)
10177 return 0;
10178 else if (host_integerp (TYPE_SIZE (type), 1))
10179 return tree_low_cst (TYPE_SIZE (type), 1);
10180 else
10181 return TYPE_ALIGN (type);
10182 }
10183
10184 /* Given a pointer to a tree node for a subrange type, return a pointer
10185 to a DIE that describes the given type. */
10186
10187 static dw_die_ref
10188 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10189 {
10190 dw_die_ref subrange_die;
10191 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10192
10193 if (context_die == NULL)
10194 context_die = comp_unit_die;
10195
10196 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10197
10198 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10199 {
10200 /* The size of the subrange type and its base type do not match,
10201 so we need to generate a size attribute for the subrange type. */
10202 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10203 }
10204
10205 if (low)
10206 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10207 if (high)
10208 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10209
10210 return subrange_die;
10211 }
10212
10213 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10214 entry that chains various modifiers in front of the given type. */
10215
10216 static dw_die_ref
10217 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10218 dw_die_ref context_die)
10219 {
10220 enum tree_code code = TREE_CODE (type);
10221 dw_die_ref mod_type_die;
10222 dw_die_ref sub_die = NULL;
10223 tree item_type = NULL;
10224 tree qualified_type;
10225 tree name, low, high;
10226
10227 if (code == ERROR_MARK)
10228 return NULL;
10229
10230 /* See if we already have the appropriately qualified variant of
10231 this type. */
10232 qualified_type
10233 = get_qualified_type (type,
10234 ((is_const_type ? TYPE_QUAL_CONST : 0)
10235 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10236
10237 /* If we do, then we can just use its DIE, if it exists. */
10238 if (qualified_type)
10239 {
10240 mod_type_die = lookup_type_die (qualified_type);
10241 if (mod_type_die)
10242 return mod_type_die;
10243 }
10244
10245 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10246
10247 /* Handle C typedef types. */
10248 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10249 {
10250 tree dtype = TREE_TYPE (name);
10251
10252 if (qualified_type == dtype)
10253 {
10254 /* For a named type, use the typedef. */
10255 gen_type_die (qualified_type, context_die);
10256 return lookup_type_die (qualified_type);
10257 }
10258 else if (is_const_type < TYPE_READONLY (dtype)
10259 || is_volatile_type < TYPE_VOLATILE (dtype)
10260 || (is_const_type <= TYPE_READONLY (dtype)
10261 && is_volatile_type <= TYPE_VOLATILE (dtype)
10262 && DECL_ORIGINAL_TYPE (name) != type))
10263 /* cv-unqualified version of named type. Just use the unnamed
10264 type to which it refers. */
10265 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10266 is_const_type, is_volatile_type,
10267 context_die);
10268 /* Else cv-qualified version of named type; fall through. */
10269 }
10270
10271 if (is_const_type)
10272 {
10273 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10274 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10275 }
10276 else if (is_volatile_type)
10277 {
10278 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10279 sub_die = modified_type_die (type, 0, 0, context_die);
10280 }
10281 else if (code == POINTER_TYPE)
10282 {
10283 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10284 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10285 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10286 item_type = TREE_TYPE (type);
10287 }
10288 else if (code == REFERENCE_TYPE)
10289 {
10290 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10291 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10292 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10293 item_type = TREE_TYPE (type);
10294 }
10295 else if (code == INTEGER_TYPE
10296 && TREE_TYPE (type) != NULL_TREE
10297 && subrange_type_for_debug_p (type, &low, &high))
10298 {
10299 mod_type_die = subrange_type_die (type, low, high, context_die);
10300 item_type = TREE_TYPE (type);
10301 }
10302 else if (is_base_type (type))
10303 mod_type_die = base_type_die (type);
10304 else
10305 {
10306 gen_type_die (type, context_die);
10307
10308 /* We have to get the type_main_variant here (and pass that to the
10309 `lookup_type_die' routine) because the ..._TYPE node we have
10310 might simply be a *copy* of some original type node (where the
10311 copy was created to help us keep track of typedef names) and
10312 that copy might have a different TYPE_UID from the original
10313 ..._TYPE node. */
10314 if (TREE_CODE (type) != VECTOR_TYPE)
10315 return lookup_type_die (type_main_variant (type));
10316 else
10317 /* Vectors have the debugging information in the type,
10318 not the main variant. */
10319 return lookup_type_die (type);
10320 }
10321
10322 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10323 don't output a DW_TAG_typedef, since there isn't one in the
10324 user's program; just attach a DW_AT_name to the type. */
10325 if (name
10326 && (TREE_CODE (name) != TYPE_DECL
10327 || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10328 {
10329 if (TREE_CODE (name) == TYPE_DECL)
10330 /* Could just call add_name_and_src_coords_attributes here,
10331 but since this is a builtin type it doesn't have any
10332 useful source coordinates anyway. */
10333 name = DECL_NAME (name);
10334 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10335 }
10336
10337 if (qualified_type)
10338 equate_type_number_to_die (qualified_type, mod_type_die);
10339
10340 if (item_type)
10341 /* We must do this after the equate_type_number_to_die call, in case
10342 this is a recursive type. This ensures that the modified_type_die
10343 recursion will terminate even if the type is recursive. Recursive
10344 types are possible in Ada. */
10345 sub_die = modified_type_die (item_type,
10346 TYPE_READONLY (item_type),
10347 TYPE_VOLATILE (item_type),
10348 context_die);
10349
10350 if (sub_die != NULL)
10351 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10352
10353 return mod_type_die;
10354 }
10355
10356 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10357 an enumerated type. */
10358
10359 static inline int
10360 type_is_enum (const_tree type)
10361 {
10362 return TREE_CODE (type) == ENUMERAL_TYPE;
10363 }
10364
10365 /* Return the DBX register number described by a given RTL node. */
10366
10367 static unsigned int
10368 dbx_reg_number (const_rtx rtl)
10369 {
10370 unsigned regno = REGNO (rtl);
10371
10372 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10373
10374 #ifdef LEAF_REG_REMAP
10375 if (current_function_uses_only_leaf_regs)
10376 {
10377 int leaf_reg = LEAF_REG_REMAP (regno);
10378 if (leaf_reg != -1)
10379 regno = (unsigned) leaf_reg;
10380 }
10381 #endif
10382
10383 return DBX_REGISTER_NUMBER (regno);
10384 }
10385
10386 /* Optionally add a DW_OP_piece term to a location description expression.
10387 DW_OP_piece is only added if the location description expression already
10388 doesn't end with DW_OP_piece. */
10389
10390 static void
10391 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10392 {
10393 dw_loc_descr_ref loc;
10394
10395 if (*list_head != NULL)
10396 {
10397 /* Find the end of the chain. */
10398 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10399 ;
10400
10401 if (loc->dw_loc_opc != DW_OP_piece)
10402 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10403 }
10404 }
10405
10406 /* Return a location descriptor that designates a machine register or
10407 zero if there is none. */
10408
10409 static dw_loc_descr_ref
10410 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10411 {
10412 rtx regs;
10413
10414 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10415 return 0;
10416
10417 regs = targetm.dwarf_register_span (rtl);
10418
10419 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10420 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10421 else
10422 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10423 }
10424
10425 /* Return a location descriptor that designates a machine register for
10426 a given hard register number. */
10427
10428 static dw_loc_descr_ref
10429 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10430 {
10431 dw_loc_descr_ref reg_loc_descr;
10432
10433 if (regno <= 31)
10434 reg_loc_descr
10435 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10436 else
10437 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10438
10439 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10440 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10441
10442 return reg_loc_descr;
10443 }
10444
10445 /* Given an RTL of a register, return a location descriptor that
10446 designates a value that spans more than one register. */
10447
10448 static dw_loc_descr_ref
10449 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10450 enum var_init_status initialized)
10451 {
10452 int nregs, size, i;
10453 unsigned reg;
10454 dw_loc_descr_ref loc_result = NULL;
10455
10456 reg = REGNO (rtl);
10457 #ifdef LEAF_REG_REMAP
10458 if (current_function_uses_only_leaf_regs)
10459 {
10460 int leaf_reg = LEAF_REG_REMAP (reg);
10461 if (leaf_reg != -1)
10462 reg = (unsigned) leaf_reg;
10463 }
10464 #endif
10465 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10466 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10467
10468 /* Simple, contiguous registers. */
10469 if (regs == NULL_RTX)
10470 {
10471 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10472
10473 loc_result = NULL;
10474 while (nregs--)
10475 {
10476 dw_loc_descr_ref t;
10477
10478 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10479 VAR_INIT_STATUS_INITIALIZED);
10480 add_loc_descr (&loc_result, t);
10481 add_loc_descr_op_piece (&loc_result, size);
10482 ++reg;
10483 }
10484 return loc_result;
10485 }
10486
10487 /* Now onto stupid register sets in non contiguous locations. */
10488
10489 gcc_assert (GET_CODE (regs) == PARALLEL);
10490
10491 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10492 loc_result = NULL;
10493
10494 for (i = 0; i < XVECLEN (regs, 0); ++i)
10495 {
10496 dw_loc_descr_ref t;
10497
10498 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10499 VAR_INIT_STATUS_INITIALIZED);
10500 add_loc_descr (&loc_result, t);
10501 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10502 add_loc_descr_op_piece (&loc_result, size);
10503 }
10504
10505 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10506 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10507 return loc_result;
10508 }
10509
10510 #endif /* DWARF2_DEBUGGING_INFO */
10511
10512 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10513
10514 /* Return a location descriptor that designates a constant. */
10515
10516 static dw_loc_descr_ref
10517 int_loc_descriptor (HOST_WIDE_INT i)
10518 {
10519 enum dwarf_location_atom op;
10520
10521 /* Pick the smallest representation of a constant, rather than just
10522 defaulting to the LEB encoding. */
10523 if (i >= 0)
10524 {
10525 if (i <= 31)
10526 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10527 else if (i <= 0xff)
10528 op = DW_OP_const1u;
10529 else if (i <= 0xffff)
10530 op = DW_OP_const2u;
10531 else if (HOST_BITS_PER_WIDE_INT == 32
10532 || i <= 0xffffffff)
10533 op = DW_OP_const4u;
10534 else
10535 op = DW_OP_constu;
10536 }
10537 else
10538 {
10539 if (i >= -0x80)
10540 op = DW_OP_const1s;
10541 else if (i >= -0x8000)
10542 op = DW_OP_const2s;
10543 else if (HOST_BITS_PER_WIDE_INT == 32
10544 || i >= -0x80000000)
10545 op = DW_OP_const4s;
10546 else
10547 op = DW_OP_consts;
10548 }
10549
10550 return new_loc_descr (op, i, 0);
10551 }
10552 #endif
10553
10554 #ifdef DWARF2_DEBUGGING_INFO
10555
10556 /* Return a location descriptor that designates a base+offset location. */
10557
10558 static dw_loc_descr_ref
10559 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10560 enum var_init_status initialized)
10561 {
10562 unsigned int regno;
10563 dw_loc_descr_ref result;
10564 dw_fde_ref fde = current_fde ();
10565
10566 /* We only use "frame base" when we're sure we're talking about the
10567 post-prologue local stack frame. We do this by *not* running
10568 register elimination until this point, and recognizing the special
10569 argument pointer and soft frame pointer rtx's. */
10570 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10571 {
10572 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10573
10574 if (elim != reg)
10575 {
10576 if (GET_CODE (elim) == PLUS)
10577 {
10578 offset += INTVAL (XEXP (elim, 1));
10579 elim = XEXP (elim, 0);
10580 }
10581 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10582 && (elim == hard_frame_pointer_rtx
10583 || elim == stack_pointer_rtx))
10584 || elim == (frame_pointer_needed
10585 ? hard_frame_pointer_rtx
10586 : stack_pointer_rtx));
10587
10588 /* If drap register is used to align stack, use frame
10589 pointer + offset to access stack variables. If stack
10590 is aligned without drap, use stack pointer + offset to
10591 access stack variables. */
10592 if (crtl->stack_realign_tried
10593 && reg == frame_pointer_rtx)
10594 {
10595 int base_reg
10596 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10597 ? HARD_FRAME_POINTER_REGNUM
10598 : STACK_POINTER_REGNUM);
10599 return new_reg_loc_descr (base_reg, offset);
10600 }
10601
10602 offset += frame_pointer_fb_offset;
10603 return new_loc_descr (DW_OP_fbreg, offset, 0);
10604 }
10605 }
10606 else if (fde
10607 && fde->drap_reg != INVALID_REGNUM
10608 && (fde->drap_reg == REGNO (reg)
10609 || fde->vdrap_reg == REGNO (reg)))
10610 {
10611 /* Use cfa+offset to represent the location of arguments passed
10612 on stack when drap is used to align stack. */
10613 return new_loc_descr (DW_OP_fbreg, offset, 0);
10614 }
10615
10616 regno = dbx_reg_number (reg);
10617 if (regno <= 31)
10618 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10619 offset, 0);
10620 else
10621 result = new_loc_descr (DW_OP_bregx, regno, offset);
10622
10623 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10624 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10625
10626 return result;
10627 }
10628
10629 /* Return true if this RTL expression describes a base+offset calculation. */
10630
10631 static inline int
10632 is_based_loc (const_rtx rtl)
10633 {
10634 return (GET_CODE (rtl) == PLUS
10635 && ((REG_P (XEXP (rtl, 0))
10636 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10637 && CONST_INT_P (XEXP (rtl, 1)))));
10638 }
10639
10640 /* Return a descriptor that describes the concatenation of N locations
10641 used to form the address of a memory location. */
10642
10643 static dw_loc_descr_ref
10644 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10645 enum var_init_status initialized)
10646 {
10647 unsigned int i;
10648 dw_loc_descr_ref cc_loc_result = NULL;
10649 unsigned int n = XVECLEN (concatn, 0);
10650
10651 for (i = 0; i < n; ++i)
10652 {
10653 dw_loc_descr_ref ref;
10654 rtx x = XVECEXP (concatn, 0, i);
10655
10656 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10657 if (ref == NULL)
10658 return NULL;
10659
10660 add_loc_descr (&cc_loc_result, ref);
10661 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10662 }
10663
10664 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10665 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10666
10667 return cc_loc_result;
10668 }
10669
10670 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10671 failed. */
10672
10673 static dw_loc_descr_ref
10674 tls_mem_loc_descriptor (rtx mem)
10675 {
10676 tree base;
10677 dw_loc_descr_ref loc_result;
10678
10679 if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10680 return NULL;
10681
10682 base = get_base_address (MEM_EXPR (mem));
10683 if (base == NULL
10684 || TREE_CODE (base) != VAR_DECL
10685 || !DECL_THREAD_LOCAL_P (base))
10686 return NULL;
10687
10688 loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10689 if (loc_result == NULL)
10690 return NULL;
10691
10692 if (INTVAL (MEM_OFFSET (mem)))
10693 loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10694
10695 return loc_result;
10696 }
10697
10698 /* The following routine converts the RTL for a variable or parameter
10699 (resident in memory) into an equivalent Dwarf representation of a
10700 mechanism for getting the address of that same variable onto the top of a
10701 hypothetical "address evaluation" stack.
10702
10703 When creating memory location descriptors, we are effectively transforming
10704 the RTL for a memory-resident object into its Dwarf postfix expression
10705 equivalent. This routine recursively descends an RTL tree, turning
10706 it into Dwarf postfix code as it goes.
10707
10708 MODE is the mode of the memory reference, needed to handle some
10709 autoincrement addressing modes.
10710
10711 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10712 location list for RTL.
10713
10714 Return 0 if we can't represent the location. */
10715
10716 static dw_loc_descr_ref
10717 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10718 enum var_init_status initialized)
10719 {
10720 dw_loc_descr_ref mem_loc_result = NULL;
10721 enum dwarf_location_atom op;
10722
10723 /* Note that for a dynamically sized array, the location we will generate a
10724 description of here will be the lowest numbered location which is
10725 actually within the array. That's *not* necessarily the same as the
10726 zeroth element of the array. */
10727
10728 rtl = targetm.delegitimize_address (rtl);
10729
10730 switch (GET_CODE (rtl))
10731 {
10732 case POST_INC:
10733 case POST_DEC:
10734 case POST_MODIFY:
10735 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
10736 just fall into the SUBREG code. */
10737
10738 /* ... fall through ... */
10739
10740 case SUBREG:
10741 /* The case of a subreg may arise when we have a local (register)
10742 variable or a formal (register) parameter which doesn't quite fill
10743 up an entire register. For now, just assume that it is
10744 legitimate to make the Dwarf info refer to the whole register which
10745 contains the given subreg. */
10746 rtl = XEXP (rtl, 0);
10747
10748 /* ... fall through ... */
10749
10750 case REG:
10751 /* Whenever a register number forms a part of the description of the
10752 method for calculating the (dynamic) address of a memory resident
10753 object, DWARF rules require the register number be referred to as
10754 a "base register". This distinction is not based in any way upon
10755 what category of register the hardware believes the given register
10756 belongs to. This is strictly DWARF terminology we're dealing with
10757 here. Note that in cases where the location of a memory-resident
10758 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10759 OP_CONST (0)) the actual DWARF location descriptor that we generate
10760 may just be OP_BASEREG (basereg). This may look deceptively like
10761 the object in question was allocated to a register (rather than in
10762 memory) so DWARF consumers need to be aware of the subtle
10763 distinction between OP_REG and OP_BASEREG. */
10764 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10765 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10766 else if (stack_realign_drap
10767 && crtl->drap_reg
10768 && crtl->args.internal_arg_pointer == rtl
10769 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10770 {
10771 /* If RTL is internal_arg_pointer, which has been optimized
10772 out, use DRAP instead. */
10773 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10774 VAR_INIT_STATUS_INITIALIZED);
10775 }
10776 break;
10777
10778 case MEM:
10779 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10780 VAR_INIT_STATUS_INITIALIZED);
10781 if (mem_loc_result == NULL)
10782 mem_loc_result = tls_mem_loc_descriptor (rtl);
10783 if (mem_loc_result != 0)
10784 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10785 break;
10786
10787 case LO_SUM:
10788 rtl = XEXP (rtl, 1);
10789
10790 /* ... fall through ... */
10791
10792 case LABEL_REF:
10793 /* Some ports can transform a symbol ref into a label ref, because
10794 the symbol ref is too far away and has to be dumped into a constant
10795 pool. */
10796 case CONST:
10797 case SYMBOL_REF:
10798 /* Alternatively, the symbol in the constant pool might be referenced
10799 by a different symbol. */
10800 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10801 {
10802 bool marked;
10803 rtx tmp = get_pool_constant_mark (rtl, &marked);
10804
10805 if (GET_CODE (tmp) == SYMBOL_REF)
10806 {
10807 rtl = tmp;
10808 if (CONSTANT_POOL_ADDRESS_P (tmp))
10809 get_pool_constant_mark (tmp, &marked);
10810 else
10811 marked = true;
10812 }
10813
10814 /* If all references to this pool constant were optimized away,
10815 it was not output and thus we can't represent it.
10816 FIXME: might try to use DW_OP_const_value here, though
10817 DW_OP_piece complicates it. */
10818 if (!marked)
10819 return 0;
10820 }
10821
10822 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10823 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10824 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10825 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10826 break;
10827
10828 case PRE_MODIFY:
10829 /* Extract the PLUS expression nested inside and fall into
10830 PLUS code below. */
10831 rtl = XEXP (rtl, 1);
10832 goto plus;
10833
10834 case PRE_INC:
10835 case PRE_DEC:
10836 /* Turn these into a PLUS expression and fall into the PLUS code
10837 below. */
10838 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10839 GEN_INT (GET_CODE (rtl) == PRE_INC
10840 ? GET_MODE_UNIT_SIZE (mode)
10841 : -GET_MODE_UNIT_SIZE (mode)));
10842
10843 /* ... fall through ... */
10844
10845 case PLUS:
10846 plus:
10847 if (is_based_loc (rtl))
10848 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10849 INTVAL (XEXP (rtl, 1)),
10850 VAR_INIT_STATUS_INITIALIZED);
10851 else
10852 {
10853 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10854 VAR_INIT_STATUS_INITIALIZED);
10855 if (mem_loc_result == 0)
10856 break;
10857
10858 if (CONST_INT_P (XEXP (rtl, 1)))
10859 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10860 else
10861 {
10862 dw_loc_descr_ref mem_loc_result2
10863 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10864 VAR_INIT_STATUS_INITIALIZED);
10865 if (mem_loc_result2 == 0)
10866 break;
10867 add_loc_descr (&mem_loc_result, mem_loc_result2);
10868 add_loc_descr (&mem_loc_result,
10869 new_loc_descr (DW_OP_plus, 0, 0));
10870 }
10871 }
10872 break;
10873
10874 /* If a pseudo-reg is optimized away, it is possible for it to
10875 be replaced with a MEM containing a multiply or shift. */
10876 case MULT:
10877 op = DW_OP_mul;
10878 goto do_binop;
10879
10880 case ASHIFT:
10881 op = DW_OP_shl;
10882 goto do_binop;
10883
10884 case ASHIFTRT:
10885 op = DW_OP_shra;
10886 goto do_binop;
10887
10888 case LSHIFTRT:
10889 op = DW_OP_shr;
10890 goto do_binop;
10891
10892 do_binop:
10893 {
10894 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10895 VAR_INIT_STATUS_INITIALIZED);
10896 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10897 VAR_INIT_STATUS_INITIALIZED);
10898
10899 if (op0 == 0 || op1 == 0)
10900 break;
10901
10902 mem_loc_result = op0;
10903 add_loc_descr (&mem_loc_result, op1);
10904 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10905 break;
10906 }
10907
10908 case CONST_INT:
10909 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10910 break;
10911
10912 case CONCATN:
10913 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10914 VAR_INIT_STATUS_INITIALIZED);
10915 break;
10916
10917 case UNSPEC:
10918 /* If delegitimize_address couldn't do anything with the UNSPEC, we
10919 can't express it in the debug info. This can happen e.g. with some
10920 TLS UNSPECs. */
10921 break;
10922
10923 default:
10924 gcc_unreachable ();
10925 }
10926
10927 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10928 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10929
10930 return mem_loc_result;
10931 }
10932
10933 /* Return a descriptor that describes the concatenation of two locations.
10934 This is typically a complex variable. */
10935
10936 static dw_loc_descr_ref
10937 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10938 {
10939 dw_loc_descr_ref cc_loc_result = NULL;
10940 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10941 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10942
10943 if (x0_ref == 0 || x1_ref == 0)
10944 return 0;
10945
10946 cc_loc_result = x0_ref;
10947 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10948
10949 add_loc_descr (&cc_loc_result, x1_ref);
10950 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10951
10952 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10953 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10954
10955 return cc_loc_result;
10956 }
10957
10958 /* Return a descriptor that describes the concatenation of N
10959 locations. */
10960
10961 static dw_loc_descr_ref
10962 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10963 {
10964 unsigned int i;
10965 dw_loc_descr_ref cc_loc_result = NULL;
10966 unsigned int n = XVECLEN (concatn, 0);
10967
10968 for (i = 0; i < n; ++i)
10969 {
10970 dw_loc_descr_ref ref;
10971 rtx x = XVECEXP (concatn, 0, i);
10972
10973 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10974 if (ref == NULL)
10975 return NULL;
10976
10977 add_loc_descr (&cc_loc_result, ref);
10978 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10979 }
10980
10981 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10982 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10983
10984 return cc_loc_result;
10985 }
10986
10987 /* Output a proper Dwarf location descriptor for a variable or parameter
10988 which is either allocated in a register or in a memory location. For a
10989 register, we just generate an OP_REG and the register number. For a
10990 memory location we provide a Dwarf postfix expression describing how to
10991 generate the (dynamic) address of the object onto the address stack.
10992
10993 If we don't know how to describe it, return 0. */
10994
10995 static dw_loc_descr_ref
10996 loc_descriptor (rtx rtl, enum var_init_status initialized)
10997 {
10998 dw_loc_descr_ref loc_result = NULL;
10999
11000 switch (GET_CODE (rtl))
11001 {
11002 case SUBREG:
11003 /* The case of a subreg may arise when we have a local (register)
11004 variable or a formal (register) parameter which doesn't quite fill
11005 up an entire register. For now, just assume that it is
11006 legitimate to make the Dwarf info refer to the whole register which
11007 contains the given subreg. */
11008 rtl = SUBREG_REG (rtl);
11009
11010 /* ... fall through ... */
11011
11012 case REG:
11013 loc_result = reg_loc_descriptor (rtl, initialized);
11014 break;
11015
11016 case MEM:
11017 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11018 initialized);
11019 if (loc_result == NULL)
11020 loc_result = tls_mem_loc_descriptor (rtl);
11021 break;
11022
11023 case CONCAT:
11024 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
11025 initialized);
11026 break;
11027
11028 case CONCATN:
11029 loc_result = concatn_loc_descriptor (rtl, initialized);
11030 break;
11031
11032 case VAR_LOCATION:
11033 /* Single part. */
11034 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
11035 {
11036 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
11037 break;
11038 }
11039
11040 rtl = XEXP (rtl, 1);
11041 /* FALLTHRU */
11042
11043 case PARALLEL:
11044 {
11045 rtvec par_elems = XVEC (rtl, 0);
11046 int num_elem = GET_NUM_ELEM (par_elems);
11047 enum machine_mode mode;
11048 int i;
11049
11050 /* Create the first one, so we have something to add to. */
11051 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11052 initialized);
11053 if (loc_result == NULL)
11054 return NULL;
11055 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11056 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11057 for (i = 1; i < num_elem; i++)
11058 {
11059 dw_loc_descr_ref temp;
11060
11061 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11062 initialized);
11063 if (temp == NULL)
11064 return NULL;
11065 add_loc_descr (&loc_result, temp);
11066 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11067 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11068 }
11069 }
11070 break;
11071
11072 default:
11073 gcc_unreachable ();
11074 }
11075
11076 return loc_result;
11077 }
11078
11079 /* Similar, but generate the descriptor from trees instead of rtl. This comes
11080 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
11081 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
11082 top-level invocation, and we require the address of LOC; is 0 if we require
11083 the value of LOC. */
11084
11085 static dw_loc_descr_ref
11086 loc_descriptor_from_tree_1 (tree loc, int want_address)
11087 {
11088 dw_loc_descr_ref ret, ret1;
11089 int have_address = 0;
11090 enum dwarf_location_atom op;
11091
11092 /* ??? Most of the time we do not take proper care for sign/zero
11093 extending the values properly. Hopefully this won't be a real
11094 problem... */
11095
11096 switch (TREE_CODE (loc))
11097 {
11098 case ERROR_MARK:
11099 return 0;
11100
11101 case PLACEHOLDER_EXPR:
11102 /* This case involves extracting fields from an object to determine the
11103 position of other fields. We don't try to encode this here. The
11104 only user of this is Ada, which encodes the needed information using
11105 the names of types. */
11106 return 0;
11107
11108 case CALL_EXPR:
11109 return 0;
11110
11111 case PREINCREMENT_EXPR:
11112 case PREDECREMENT_EXPR:
11113 case POSTINCREMENT_EXPR:
11114 case POSTDECREMENT_EXPR:
11115 /* There are no opcodes for these operations. */
11116 return 0;
11117
11118 case ADDR_EXPR:
11119 /* If we already want an address, there's nothing we can do. */
11120 if (want_address)
11121 return 0;
11122
11123 /* Otherwise, process the argument and look for the address. */
11124 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
11125
11126 case VAR_DECL:
11127 if (DECL_THREAD_LOCAL_P (loc))
11128 {
11129 rtx rtl;
11130 enum dwarf_location_atom first_op;
11131 enum dwarf_location_atom second_op;
11132 bool dtprel = false;
11133
11134 if (targetm.have_tls)
11135 {
11136 /* If this is not defined, we have no way to emit the
11137 data. */
11138 if (!targetm.asm_out.output_dwarf_dtprel)
11139 return 0;
11140
11141 /* The way DW_OP_GNU_push_tls_address is specified, we
11142 can only look up addresses of objects in the current
11143 module. */
11144 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
11145 return 0;
11146 first_op = DW_OP_addr;
11147 dtprel = true;
11148 second_op = DW_OP_GNU_push_tls_address;
11149 }
11150 else
11151 {
11152 if (!targetm.emutls.debug_form_tls_address)
11153 return 0;
11154 loc = emutls_decl (loc);
11155 first_op = DW_OP_addr;
11156 second_op = DW_OP_form_tls_address;
11157 }
11158
11159 rtl = rtl_for_decl_location (loc);
11160 if (rtl == NULL_RTX)
11161 return 0;
11162
11163 if (!MEM_P (rtl))
11164 return 0;
11165 rtl = XEXP (rtl, 0);
11166 if (! CONSTANT_P (rtl))
11167 return 0;
11168
11169 ret = new_loc_descr (first_op, 0, 0);
11170 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
11171 ret->dw_loc_oprnd1.v.val_addr = rtl;
11172 ret->dtprel = dtprel;
11173
11174 ret1 = new_loc_descr (second_op, 0, 0);
11175 add_loc_descr (&ret, ret1);
11176
11177 have_address = 1;
11178 break;
11179 }
11180 /* FALLTHRU */
11181
11182 case PARM_DECL:
11183 if (DECL_HAS_VALUE_EXPR_P (loc))
11184 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
11185 want_address);
11186 /* FALLTHRU */
11187
11188 case RESULT_DECL:
11189 case FUNCTION_DECL:
11190 {
11191 rtx rtl = rtl_for_decl_location (loc);
11192
11193 if (rtl == NULL_RTX)
11194 return 0;
11195 else if (CONST_INT_P (rtl))
11196 {
11197 HOST_WIDE_INT val = INTVAL (rtl);
11198 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
11199 val &= GET_MODE_MASK (DECL_MODE (loc));
11200 ret = int_loc_descriptor (val);
11201 }
11202 else if (GET_CODE (rtl) == CONST_STRING)
11203 return 0;
11204 else if (CONSTANT_P (rtl))
11205 {
11206 ret = new_loc_descr (DW_OP_addr, 0, 0);
11207 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
11208 ret->dw_loc_oprnd1.v.val_addr = rtl;
11209 }
11210 else
11211 {
11212 enum machine_mode mode;
11213
11214 /* Certain constructs can only be represented at top-level. */
11215 if (want_address == 2)
11216 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
11217
11218 mode = GET_MODE (rtl);
11219 if (MEM_P (rtl))
11220 {
11221 rtl = XEXP (rtl, 0);
11222 have_address = 1;
11223 }
11224 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
11225 }
11226 }
11227 break;
11228
11229 case INDIRECT_REF:
11230 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11231 have_address = 1;
11232 break;
11233
11234 case COMPOUND_EXPR:
11235 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
11236
11237 CASE_CONVERT:
11238 case VIEW_CONVERT_EXPR:
11239 case SAVE_EXPR:
11240 case MODIFY_EXPR:
11241 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
11242
11243 case COMPONENT_REF:
11244 case BIT_FIELD_REF:
11245 case ARRAY_REF:
11246 case ARRAY_RANGE_REF:
11247 {
11248 tree obj, offset;
11249 HOST_WIDE_INT bitsize, bitpos, bytepos;
11250 enum machine_mode mode;
11251 int volatilep;
11252 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
11253
11254 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
11255 &unsignedp, &volatilep, false);
11256
11257 if (obj == loc)
11258 return 0;
11259
11260 ret = loc_descriptor_from_tree_1 (obj, 1);
11261 if (ret == 0
11262 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
11263 return 0;
11264
11265 if (offset != NULL_TREE)
11266 {
11267 /* Variable offset. */
11268 ret1 = loc_descriptor_from_tree_1 (offset, 0);
11269 if (ret1 == 0)
11270 return 0;
11271 add_loc_descr (&ret, ret1);
11272 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
11273 }
11274
11275 bytepos = bitpos / BITS_PER_UNIT;
11276 loc_descr_plus_const (&ret, bytepos);
11277
11278 have_address = 1;
11279 break;
11280 }
11281
11282 case INTEGER_CST:
11283 if (host_integerp (loc, 0))
11284 ret = int_loc_descriptor (tree_low_cst (loc, 0));
11285 else
11286 return 0;
11287 break;
11288
11289 case CONSTRUCTOR:
11290 {
11291 /* Get an RTL for this, if something has been emitted. */
11292 rtx rtl = lookup_constant_def (loc);
11293 enum machine_mode mode;
11294
11295 if (!rtl || !MEM_P (rtl))
11296 return 0;
11297 mode = GET_MODE (rtl);
11298 rtl = XEXP (rtl, 0);
11299 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
11300 have_address = 1;
11301 break;
11302 }
11303
11304 case TRUTH_AND_EXPR:
11305 case TRUTH_ANDIF_EXPR:
11306 case BIT_AND_EXPR:
11307 op = DW_OP_and;
11308 goto do_binop;
11309
11310 case TRUTH_XOR_EXPR:
11311 case BIT_XOR_EXPR:
11312 op = DW_OP_xor;
11313 goto do_binop;
11314
11315 case TRUTH_OR_EXPR:
11316 case TRUTH_ORIF_EXPR:
11317 case BIT_IOR_EXPR:
11318 op = DW_OP_or;
11319 goto do_binop;
11320
11321 case FLOOR_DIV_EXPR:
11322 case CEIL_DIV_EXPR:
11323 case ROUND_DIV_EXPR:
11324 case TRUNC_DIV_EXPR:
11325 op = DW_OP_div;
11326 goto do_binop;
11327
11328 case MINUS_EXPR:
11329 op = DW_OP_minus;
11330 goto do_binop;
11331
11332 case FLOOR_MOD_EXPR:
11333 case CEIL_MOD_EXPR:
11334 case ROUND_MOD_EXPR:
11335 case TRUNC_MOD_EXPR:
11336 op = DW_OP_mod;
11337 goto do_binop;
11338
11339 case MULT_EXPR:
11340 op = DW_OP_mul;
11341 goto do_binop;
11342
11343 case LSHIFT_EXPR:
11344 op = DW_OP_shl;
11345 goto do_binop;
11346
11347 case RSHIFT_EXPR:
11348 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
11349 goto do_binop;
11350
11351 case POINTER_PLUS_EXPR:
11352 case PLUS_EXPR:
11353 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
11354 && host_integerp (TREE_OPERAND (loc, 1), 0))
11355 {
11356 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11357 if (ret == 0)
11358 return 0;
11359
11360 loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
11361 break;
11362 }
11363
11364 op = DW_OP_plus;
11365 goto do_binop;
11366
11367 case LE_EXPR:
11368 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11369 return 0;
11370
11371 op = DW_OP_le;
11372 goto do_binop;
11373
11374 case GE_EXPR:
11375 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11376 return 0;
11377
11378 op = DW_OP_ge;
11379 goto do_binop;
11380
11381 case LT_EXPR:
11382 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11383 return 0;
11384
11385 op = DW_OP_lt;
11386 goto do_binop;
11387
11388 case GT_EXPR:
11389 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11390 return 0;
11391
11392 op = DW_OP_gt;
11393 goto do_binop;
11394
11395 case EQ_EXPR:
11396 op = DW_OP_eq;
11397 goto do_binop;
11398
11399 case NE_EXPR:
11400 op = DW_OP_ne;
11401 goto do_binop;
11402
11403 do_binop:
11404 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11405 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11406 if (ret == 0 || ret1 == 0)
11407 return 0;
11408
11409 add_loc_descr (&ret, ret1);
11410 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11411 break;
11412
11413 case TRUTH_NOT_EXPR:
11414 case BIT_NOT_EXPR:
11415 op = DW_OP_not;
11416 goto do_unop;
11417
11418 case ABS_EXPR:
11419 op = DW_OP_abs;
11420 goto do_unop;
11421
11422 case NEGATE_EXPR:
11423 op = DW_OP_neg;
11424 goto do_unop;
11425
11426 do_unop:
11427 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11428 if (ret == 0)
11429 return 0;
11430
11431 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11432 break;
11433
11434 case MIN_EXPR:
11435 case MAX_EXPR:
11436 {
11437 const enum tree_code code =
11438 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
11439
11440 loc = build3 (COND_EXPR, TREE_TYPE (loc),
11441 build2 (code, integer_type_node,
11442 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
11443 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
11444 }
11445
11446 /* ... fall through ... */
11447
11448 case COND_EXPR:
11449 {
11450 dw_loc_descr_ref lhs
11451 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11452 dw_loc_descr_ref rhs
11453 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
11454 dw_loc_descr_ref bra_node, jump_node, tmp;
11455
11456 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11457 if (ret == 0 || lhs == 0 || rhs == 0)
11458 return 0;
11459
11460 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11461 add_loc_descr (&ret, bra_node);
11462
11463 add_loc_descr (&ret, rhs);
11464 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11465 add_loc_descr (&ret, jump_node);
11466
11467 add_loc_descr (&ret, lhs);
11468 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11469 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11470
11471 /* ??? Need a node to point the skip at. Use a nop. */
11472 tmp = new_loc_descr (DW_OP_nop, 0, 0);
11473 add_loc_descr (&ret, tmp);
11474 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11475 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11476 }
11477 break;
11478
11479 case FIX_TRUNC_EXPR:
11480 return 0;
11481
11482 default:
11483 /* Leave front-end specific codes as simply unknown. This comes
11484 up, for instance, with the C STMT_EXPR. */
11485 if ((unsigned int) TREE_CODE (loc)
11486 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11487 return 0;
11488
11489 #ifdef ENABLE_CHECKING
11490 /* Otherwise this is a generic code; we should just lists all of
11491 these explicitly. We forgot one. */
11492 gcc_unreachable ();
11493 #else
11494 /* In a release build, we want to degrade gracefully: better to
11495 generate incomplete debugging information than to crash. */
11496 return NULL;
11497 #endif
11498 }
11499
11500 /* Show if we can't fill the request for an address. */
11501 if (want_address && !have_address)
11502 return 0;
11503
11504 /* If we've got an address and don't want one, dereference. */
11505 if (!want_address && have_address && ret)
11506 {
11507 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11508
11509 if (size > DWARF2_ADDR_SIZE || size == -1)
11510 return 0;
11511 else if (size == DWARF2_ADDR_SIZE)
11512 op = DW_OP_deref;
11513 else
11514 op = DW_OP_deref_size;
11515
11516 add_loc_descr (&ret, new_loc_descr (op, size, 0));
11517 }
11518
11519 return ret;
11520 }
11521
11522 static inline dw_loc_descr_ref
11523 loc_descriptor_from_tree (tree loc)
11524 {
11525 return loc_descriptor_from_tree_1 (loc, 2);
11526 }
11527
11528 /* Given a value, round it up to the lowest multiple of `boundary'
11529 which is not less than the value itself. */
11530
11531 static inline HOST_WIDE_INT
11532 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11533 {
11534 return (((value + boundary - 1) / boundary) * boundary);
11535 }
11536
11537 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11538 pointer to the declared type for the relevant field variable, or return
11539 `integer_type_node' if the given node turns out to be an
11540 ERROR_MARK node. */
11541
11542 static inline tree
11543 field_type (const_tree decl)
11544 {
11545 tree type;
11546
11547 if (TREE_CODE (decl) == ERROR_MARK)
11548 return integer_type_node;
11549
11550 type = DECL_BIT_FIELD_TYPE (decl);
11551 if (type == NULL_TREE)
11552 type = TREE_TYPE (decl);
11553
11554 return type;
11555 }
11556
11557 /* Given a pointer to a tree node, return the alignment in bits for
11558 it, or else return BITS_PER_WORD if the node actually turns out to
11559 be an ERROR_MARK node. */
11560
11561 static inline unsigned
11562 simple_type_align_in_bits (const_tree type)
11563 {
11564 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11565 }
11566
11567 static inline unsigned
11568 simple_decl_align_in_bits (const_tree decl)
11569 {
11570 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11571 }
11572
11573 /* Return the result of rounding T up to ALIGN. */
11574
11575 static inline HOST_WIDE_INT
11576 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11577 {
11578 /* We must be careful if T is negative because HOST_WIDE_INT can be
11579 either "above" or "below" unsigned int as per the C promotion
11580 rules, depending on the host, thus making the signedness of the
11581 direct multiplication and division unpredictable. */
11582 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11583
11584 u += align - 1;
11585 u /= align;
11586 u *= align;
11587
11588 return (HOST_WIDE_INT) u;
11589 }
11590
11591 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11592 lowest addressed byte of the "containing object" for the given FIELD_DECL,
11593 or return 0 if we are unable to determine what that offset is, either
11594 because the argument turns out to be a pointer to an ERROR_MARK node, or
11595 because the offset is actually variable. (We can't handle the latter case
11596 just yet). */
11597
11598 static HOST_WIDE_INT
11599 field_byte_offset (const_tree decl)
11600 {
11601 HOST_WIDE_INT object_offset_in_bits;
11602 HOST_WIDE_INT bitpos_int;
11603
11604 if (TREE_CODE (decl) == ERROR_MARK)
11605 return 0;
11606
11607 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11608
11609 /* We cannot yet cope with fields whose positions are variable, so
11610 for now, when we see such things, we simply return 0. Someday, we may
11611 be able to handle such cases, but it will be damn difficult. */
11612 if (! host_integerp (bit_position (decl), 0))
11613 return 0;
11614
11615 bitpos_int = int_bit_position (decl);
11616
11617 #ifdef PCC_BITFIELD_TYPE_MATTERS
11618 if (PCC_BITFIELD_TYPE_MATTERS)
11619 {
11620 tree type;
11621 tree field_size_tree;
11622 HOST_WIDE_INT deepest_bitpos;
11623 unsigned HOST_WIDE_INT field_size_in_bits;
11624 unsigned int type_align_in_bits;
11625 unsigned int decl_align_in_bits;
11626 unsigned HOST_WIDE_INT type_size_in_bits;
11627
11628 type = field_type (decl);
11629 type_size_in_bits = simple_type_size_in_bits (type);
11630 type_align_in_bits = simple_type_align_in_bits (type);
11631
11632 field_size_tree = DECL_SIZE (decl);
11633
11634 /* The size could be unspecified if there was an error, or for
11635 a flexible array member. */
11636 if (!field_size_tree)
11637 field_size_tree = bitsize_zero_node;
11638
11639 /* If the size of the field is not constant, use the type size. */
11640 if (host_integerp (field_size_tree, 1))
11641 field_size_in_bits = tree_low_cst (field_size_tree, 1);
11642 else
11643 field_size_in_bits = type_size_in_bits;
11644
11645 decl_align_in_bits = simple_decl_align_in_bits (decl);
11646
11647 /* The GCC front-end doesn't make any attempt to keep track of the
11648 starting bit offset (relative to the start of the containing
11649 structure type) of the hypothetical "containing object" for a
11650 bit-field. Thus, when computing the byte offset value for the
11651 start of the "containing object" of a bit-field, we must deduce
11652 this information on our own. This can be rather tricky to do in
11653 some cases. For example, handling the following structure type
11654 definition when compiling for an i386/i486 target (which only
11655 aligns long long's to 32-bit boundaries) can be very tricky:
11656
11657 struct S { int field1; long long field2:31; };
11658
11659 Fortunately, there is a simple rule-of-thumb which can be used
11660 in such cases. When compiling for an i386/i486, GCC will
11661 allocate 8 bytes for the structure shown above. It decides to
11662 do this based upon one simple rule for bit-field allocation.
11663 GCC allocates each "containing object" for each bit-field at
11664 the first (i.e. lowest addressed) legitimate alignment boundary
11665 (based upon the required minimum alignment for the declared
11666 type of the field) which it can possibly use, subject to the
11667 condition that there is still enough available space remaining
11668 in the containing object (when allocated at the selected point)
11669 to fully accommodate all of the bits of the bit-field itself.
11670
11671 This simple rule makes it obvious why GCC allocates 8 bytes for
11672 each object of the structure type shown above. When looking
11673 for a place to allocate the "containing object" for `field2',
11674 the compiler simply tries to allocate a 64-bit "containing
11675 object" at each successive 32-bit boundary (starting at zero)
11676 until it finds a place to allocate that 64- bit field such that
11677 at least 31 contiguous (and previously unallocated) bits remain
11678 within that selected 64 bit field. (As it turns out, for the
11679 example above, the compiler finds it is OK to allocate the
11680 "containing object" 64-bit field at bit-offset zero within the
11681 structure type.)
11682
11683 Here we attempt to work backwards from the limited set of facts
11684 we're given, and we try to deduce from those facts, where GCC
11685 must have believed that the containing object started (within
11686 the structure type). The value we deduce is then used (by the
11687 callers of this routine) to generate DW_AT_location and
11688 DW_AT_bit_offset attributes for fields (both bit-fields and, in
11689 the case of DW_AT_location, regular fields as well). */
11690
11691 /* Figure out the bit-distance from the start of the structure to
11692 the "deepest" bit of the bit-field. */
11693 deepest_bitpos = bitpos_int + field_size_in_bits;
11694
11695 /* This is the tricky part. Use some fancy footwork to deduce
11696 where the lowest addressed bit of the containing object must
11697 be. */
11698 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11699
11700 /* Round up to type_align by default. This works best for
11701 bitfields. */
11702 object_offset_in_bits
11703 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11704
11705 if (object_offset_in_bits > bitpos_int)
11706 {
11707 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11708
11709 /* Round up to decl_align instead. */
11710 object_offset_in_bits
11711 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11712 }
11713 }
11714 else
11715 #endif
11716 object_offset_in_bits = bitpos_int;
11717
11718 return object_offset_in_bits / BITS_PER_UNIT;
11719 }
11720 \f
11721 /* The following routines define various Dwarf attributes and any data
11722 associated with them. */
11723
11724 /* Add a location description attribute value to a DIE.
11725
11726 This emits location attributes suitable for whole variables and
11727 whole parameters. Note that the location attributes for struct fields are
11728 generated by the routine `data_member_location_attribute' below. */
11729
11730 static inline void
11731 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11732 dw_loc_descr_ref descr)
11733 {
11734 if (descr != 0)
11735 add_AT_loc (die, attr_kind, descr);
11736 }
11737
11738 /* Attach the specialized form of location attribute used for data members of
11739 struct and union types. In the special case of a FIELD_DECL node which
11740 represents a bit-field, the "offset" part of this special location
11741 descriptor must indicate the distance in bytes from the lowest-addressed
11742 byte of the containing struct or union type to the lowest-addressed byte of
11743 the "containing object" for the bit-field. (See the `field_byte_offset'
11744 function above).
11745
11746 For any given bit-field, the "containing object" is a hypothetical object
11747 (of some integral or enum type) within which the given bit-field lives. The
11748 type of this hypothetical "containing object" is always the same as the
11749 declared type of the individual bit-field itself (for GCC anyway... the
11750 DWARF spec doesn't actually mandate this). Note that it is the size (in
11751 bytes) of the hypothetical "containing object" which will be given in the
11752 DW_AT_byte_size attribute for this bit-field. (See the
11753 `byte_size_attribute' function below.) It is also used when calculating the
11754 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
11755 function below.) */
11756
11757 static void
11758 add_data_member_location_attribute (dw_die_ref die, tree decl)
11759 {
11760 HOST_WIDE_INT offset;
11761 dw_loc_descr_ref loc_descr = 0;
11762
11763 if (TREE_CODE (decl) == TREE_BINFO)
11764 {
11765 /* We're working on the TAG_inheritance for a base class. */
11766 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11767 {
11768 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11769 aren't at a fixed offset from all (sub)objects of the same
11770 type. We need to extract the appropriate offset from our
11771 vtable. The following dwarf expression means
11772
11773 BaseAddr = ObAddr + *((*ObAddr) - Offset)
11774
11775 This is specific to the V3 ABI, of course. */
11776
11777 dw_loc_descr_ref tmp;
11778
11779 /* Make a copy of the object address. */
11780 tmp = new_loc_descr (DW_OP_dup, 0, 0);
11781 add_loc_descr (&loc_descr, tmp);
11782
11783 /* Extract the vtable address. */
11784 tmp = new_loc_descr (DW_OP_deref, 0, 0);
11785 add_loc_descr (&loc_descr, tmp);
11786
11787 /* Calculate the address of the offset. */
11788 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11789 gcc_assert (offset < 0);
11790
11791 tmp = int_loc_descriptor (-offset);
11792 add_loc_descr (&loc_descr, tmp);
11793 tmp = new_loc_descr (DW_OP_minus, 0, 0);
11794 add_loc_descr (&loc_descr, tmp);
11795
11796 /* Extract the offset. */
11797 tmp = new_loc_descr (DW_OP_deref, 0, 0);
11798 add_loc_descr (&loc_descr, tmp);
11799
11800 /* Add it to the object address. */
11801 tmp = new_loc_descr (DW_OP_plus, 0, 0);
11802 add_loc_descr (&loc_descr, tmp);
11803 }
11804 else
11805 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11806 }
11807 else
11808 offset = field_byte_offset (decl);
11809
11810 if (! loc_descr)
11811 {
11812 if (dwarf_version > 2)
11813 {
11814 /* Don't need to output a location expression, just the constant. */
11815 add_AT_int (die, DW_AT_data_member_location, offset);
11816 return;
11817 }
11818 else
11819 {
11820 enum dwarf_location_atom op;
11821
11822 /* The DWARF2 standard says that we should assume that the structure
11823 address is already on the stack, so we can specify a structure
11824 field address by using DW_OP_plus_uconst. */
11825
11826 #ifdef MIPS_DEBUGGING_INFO
11827 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11828 operator correctly. It works only if we leave the offset on the
11829 stack. */
11830 op = DW_OP_constu;
11831 #else
11832 op = DW_OP_plus_uconst;
11833 #endif
11834
11835 loc_descr = new_loc_descr (op, offset, 0);
11836 }
11837 }
11838
11839 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11840 }
11841
11842 /* Writes integer values to dw_vec_const array. */
11843
11844 static void
11845 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11846 {
11847 while (size != 0)
11848 {
11849 *dest++ = val & 0xff;
11850 val >>= 8;
11851 --size;
11852 }
11853 }
11854
11855 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
11856
11857 static HOST_WIDE_INT
11858 extract_int (const unsigned char *src, unsigned int size)
11859 {
11860 HOST_WIDE_INT val = 0;
11861
11862 src += size;
11863 while (size != 0)
11864 {
11865 val <<= 8;
11866 val |= *--src & 0xff;
11867 --size;
11868 }
11869 return val;
11870 }
11871
11872 /* Writes floating point values to dw_vec_const array. */
11873
11874 static void
11875 insert_float (const_rtx rtl, unsigned char *array)
11876 {
11877 REAL_VALUE_TYPE rv;
11878 long val[4];
11879 int i;
11880
11881 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11882 real_to_target (val, &rv, GET_MODE (rtl));
11883
11884 /* real_to_target puts 32-bit pieces in each long. Pack them. */
11885 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11886 {
11887 insert_int (val[i], 4, array);
11888 array += 4;
11889 }
11890 }
11891
11892 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11893 does not have a "location" either in memory or in a register. These
11894 things can arise in GNU C when a constant is passed as an actual parameter
11895 to an inlined function. They can also arise in C++ where declared
11896 constants do not necessarily get memory "homes". */
11897
11898 static void
11899 add_const_value_attribute (dw_die_ref die, rtx rtl)
11900 {
11901 switch (GET_CODE (rtl))
11902 {
11903 case CONST_INT:
11904 {
11905 HOST_WIDE_INT val = INTVAL (rtl);
11906
11907 if (val < 0)
11908 add_AT_int (die, DW_AT_const_value, val);
11909 else
11910 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11911 }
11912 break;
11913
11914 case CONST_DOUBLE:
11915 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11916 floating-point constant. A CONST_DOUBLE is used whenever the
11917 constant requires more than one word in order to be adequately
11918 represented. We output CONST_DOUBLEs as blocks. */
11919 {
11920 enum machine_mode mode = GET_MODE (rtl);
11921
11922 if (SCALAR_FLOAT_MODE_P (mode))
11923 {
11924 unsigned int length = GET_MODE_SIZE (mode);
11925 unsigned char *array = GGC_NEWVEC (unsigned char, length);
11926
11927 insert_float (rtl, array);
11928 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11929 }
11930 else
11931 {
11932 /* ??? We really should be using HOST_WIDE_INT throughout. */
11933 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11934
11935 add_AT_long_long (die, DW_AT_const_value,
11936 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11937 }
11938 }
11939 break;
11940
11941 case CONST_VECTOR:
11942 {
11943 enum machine_mode mode = GET_MODE (rtl);
11944 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11945 unsigned int length = CONST_VECTOR_NUNITS (rtl);
11946 unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11947 unsigned int i;
11948 unsigned char *p;
11949
11950 switch (GET_MODE_CLASS (mode))
11951 {
11952 case MODE_VECTOR_INT:
11953 for (i = 0, p = array; i < length; i++, p += elt_size)
11954 {
11955 rtx elt = CONST_VECTOR_ELT (rtl, i);
11956 HOST_WIDE_INT lo, hi;
11957
11958 switch (GET_CODE (elt))
11959 {
11960 case CONST_INT:
11961 lo = INTVAL (elt);
11962 hi = -(lo < 0);
11963 break;
11964
11965 case CONST_DOUBLE:
11966 lo = CONST_DOUBLE_LOW (elt);
11967 hi = CONST_DOUBLE_HIGH (elt);
11968 break;
11969
11970 default:
11971 gcc_unreachable ();
11972 }
11973
11974 if (elt_size <= sizeof (HOST_WIDE_INT))
11975 insert_int (lo, elt_size, p);
11976 else
11977 {
11978 unsigned char *p0 = p;
11979 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11980
11981 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11982 if (WORDS_BIG_ENDIAN)
11983 {
11984 p0 = p1;
11985 p1 = p;
11986 }
11987 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11988 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11989 }
11990 }
11991 break;
11992
11993 case MODE_VECTOR_FLOAT:
11994 for (i = 0, p = array; i < length; i++, p += elt_size)
11995 {
11996 rtx elt = CONST_VECTOR_ELT (rtl, i);
11997 insert_float (elt, p);
11998 }
11999 break;
12000
12001 default:
12002 gcc_unreachable ();
12003 }
12004
12005 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
12006 }
12007 break;
12008
12009 case CONST_STRING:
12010 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
12011 break;
12012
12013 case SYMBOL_REF:
12014 case LABEL_REF:
12015 case CONST:
12016 add_AT_addr (die, DW_AT_const_value, rtl);
12017 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12018 break;
12019
12020 case PLUS:
12021 /* In cases where an inlined instance of an inline function is passed
12022 the address of an `auto' variable (which is local to the caller) we
12023 can get a situation where the DECL_RTL of the artificial local
12024 variable (for the inlining) which acts as a stand-in for the
12025 corresponding formal parameter (of the inline function) will look
12026 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
12027 exactly a compile-time constant expression, but it isn't the address
12028 of the (artificial) local variable either. Rather, it represents the
12029 *value* which the artificial local variable always has during its
12030 lifetime. We currently have no way to represent such quasi-constant
12031 values in Dwarf, so for now we just punt and generate nothing. */
12032 break;
12033
12034 default:
12035 /* No other kinds of rtx should be possible here. */
12036 gcc_unreachable ();
12037 }
12038
12039 }
12040
12041 /* Determine whether the evaluation of EXPR references any variables
12042 or functions which aren't otherwise used (and therefore may not be
12043 output). */
12044 static tree
12045 reference_to_unused (tree * tp, int * walk_subtrees,
12046 void * data ATTRIBUTE_UNUSED)
12047 {
12048 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
12049 *walk_subtrees = 0;
12050
12051 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
12052 && ! TREE_ASM_WRITTEN (*tp))
12053 return *tp;
12054 /* ??? The C++ FE emits debug information for using decls, so
12055 putting gcc_unreachable here falls over. See PR31899. For now
12056 be conservative. */
12057 else if (!cgraph_global_info_ready
12058 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
12059 return *tp;
12060 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
12061 {
12062 struct varpool_node *node = varpool_node (*tp);
12063 if (!node->needed)
12064 return *tp;
12065 }
12066 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
12067 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
12068 {
12069 struct cgraph_node *node = cgraph_node (*tp);
12070 if (node->process || TREE_ASM_WRITTEN (*tp))
12071 return *tp;
12072 }
12073 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
12074 return *tp;
12075
12076 return NULL_TREE;
12077 }
12078
12079 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
12080 for use in a later add_const_value_attribute call. */
12081
12082 static rtx
12083 rtl_for_decl_init (tree init, tree type)
12084 {
12085 rtx rtl = NULL_RTX;
12086
12087 /* If a variable is initialized with a string constant without embedded
12088 zeros, build CONST_STRING. */
12089 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
12090 {
12091 tree enttype = TREE_TYPE (type);
12092 tree domain = TYPE_DOMAIN (type);
12093 enum machine_mode mode = TYPE_MODE (enttype);
12094
12095 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
12096 && domain
12097 && integer_zerop (TYPE_MIN_VALUE (domain))
12098 && compare_tree_int (TYPE_MAX_VALUE (domain),
12099 TREE_STRING_LENGTH (init) - 1) == 0
12100 && ((size_t) TREE_STRING_LENGTH (init)
12101 == strlen (TREE_STRING_POINTER (init)) + 1))
12102 rtl = gen_rtx_CONST_STRING (VOIDmode,
12103 ggc_strdup (TREE_STRING_POINTER (init)));
12104 }
12105 /* Other aggregates, and complex values, could be represented using
12106 CONCAT: FIXME! */
12107 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
12108 ;
12109 /* Vectors only work if their mode is supported by the target.
12110 FIXME: generic vectors ought to work too. */
12111 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
12112 ;
12113 /* If the initializer is something that we know will expand into an
12114 immediate RTL constant, expand it now. We must be careful not to
12115 reference variables which won't be output. */
12116 else if (initializer_constant_valid_p (init, type)
12117 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
12118 {
12119 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
12120 possible. */
12121 if (TREE_CODE (type) == VECTOR_TYPE)
12122 switch (TREE_CODE (init))
12123 {
12124 case VECTOR_CST:
12125 break;
12126 case CONSTRUCTOR:
12127 if (TREE_CONSTANT (init))
12128 {
12129 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
12130 bool constant_p = true;
12131 tree value;
12132 unsigned HOST_WIDE_INT ix;
12133
12134 /* Even when ctor is constant, it might contain non-*_CST
12135 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
12136 belong into VECTOR_CST nodes. */
12137 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
12138 if (!CONSTANT_CLASS_P (value))
12139 {
12140 constant_p = false;
12141 break;
12142 }
12143
12144 if (constant_p)
12145 {
12146 init = build_vector_from_ctor (type, elts);
12147 break;
12148 }
12149 }
12150 /* FALLTHRU */
12151
12152 default:
12153 return NULL;
12154 }
12155
12156 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
12157
12158 /* If expand_expr returns a MEM, it wasn't immediate. */
12159 gcc_assert (!rtl || !MEM_P (rtl));
12160 }
12161
12162 return rtl;
12163 }
12164
12165 /* Generate RTL for the variable DECL to represent its location. */
12166
12167 static rtx
12168 rtl_for_decl_location (tree decl)
12169 {
12170 rtx rtl;
12171
12172 /* Here we have to decide where we are going to say the parameter "lives"
12173 (as far as the debugger is concerned). We only have a couple of
12174 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
12175
12176 DECL_RTL normally indicates where the parameter lives during most of the
12177 activation of the function. If optimization is enabled however, this
12178 could be either NULL or else a pseudo-reg. Both of those cases indicate
12179 that the parameter doesn't really live anywhere (as far as the code
12180 generation parts of GCC are concerned) during most of the function's
12181 activation. That will happen (for example) if the parameter is never
12182 referenced within the function.
12183
12184 We could just generate a location descriptor here for all non-NULL
12185 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
12186 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
12187 where DECL_RTL is NULL or is a pseudo-reg.
12188
12189 Note however that we can only get away with using DECL_INCOMING_RTL as
12190 a backup substitute for DECL_RTL in certain limited cases. In cases
12191 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
12192 we can be sure that the parameter was passed using the same type as it is
12193 declared to have within the function, and that its DECL_INCOMING_RTL
12194 points us to a place where a value of that type is passed.
12195
12196 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
12197 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
12198 because in these cases DECL_INCOMING_RTL points us to a value of some
12199 type which is *different* from the type of the parameter itself. Thus,
12200 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
12201 such cases, the debugger would end up (for example) trying to fetch a
12202 `float' from a place which actually contains the first part of a
12203 `double'. That would lead to really incorrect and confusing
12204 output at debug-time.
12205
12206 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
12207 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
12208 are a couple of exceptions however. On little-endian machines we can
12209 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
12210 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
12211 an integral type that is smaller than TREE_TYPE (decl). These cases arise
12212 when (on a little-endian machine) a non-prototyped function has a
12213 parameter declared to be of type `short' or `char'. In such cases,
12214 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
12215 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
12216 passed `int' value. If the debugger then uses that address to fetch
12217 a `short' or a `char' (on a little-endian machine) the result will be
12218 the correct data, so we allow for such exceptional cases below.
12219
12220 Note that our goal here is to describe the place where the given formal
12221 parameter lives during most of the function's activation (i.e. between the
12222 end of the prologue and the start of the epilogue). We'll do that as best
12223 as we can. Note however that if the given formal parameter is modified
12224 sometime during the execution of the function, then a stack backtrace (at
12225 debug-time) will show the function as having been called with the *new*
12226 value rather than the value which was originally passed in. This happens
12227 rarely enough that it is not a major problem, but it *is* a problem, and
12228 I'd like to fix it.
12229
12230 A future version of dwarf2out.c may generate two additional attributes for
12231 any given DW_TAG_formal_parameter DIE which will describe the "passed
12232 type" and the "passed location" for the given formal parameter in addition
12233 to the attributes we now generate to indicate the "declared type" and the
12234 "active location" for each parameter. This additional set of attributes
12235 could be used by debuggers for stack backtraces. Separately, note that
12236 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
12237 This happens (for example) for inlined-instances of inline function formal
12238 parameters which are never referenced. This really shouldn't be
12239 happening. All PARM_DECL nodes should get valid non-NULL
12240 DECL_INCOMING_RTL values. FIXME. */
12241
12242 /* Use DECL_RTL as the "location" unless we find something better. */
12243 rtl = DECL_RTL_IF_SET (decl);
12244
12245 /* When generating abstract instances, ignore everything except
12246 constants, symbols living in memory, and symbols living in
12247 fixed registers. */
12248 if (! reload_completed)
12249 {
12250 if (rtl
12251 && (CONSTANT_P (rtl)
12252 || (MEM_P (rtl)
12253 && CONSTANT_P (XEXP (rtl, 0)))
12254 || (REG_P (rtl)
12255 && TREE_CODE (decl) == VAR_DECL
12256 && TREE_STATIC (decl))))
12257 {
12258 rtl = targetm.delegitimize_address (rtl);
12259 return rtl;
12260 }
12261 rtl = NULL_RTX;
12262 }
12263 else if (TREE_CODE (decl) == PARM_DECL)
12264 {
12265 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
12266 {
12267 tree declared_type = TREE_TYPE (decl);
12268 tree passed_type = DECL_ARG_TYPE (decl);
12269 enum machine_mode dmode = TYPE_MODE (declared_type);
12270 enum machine_mode pmode = TYPE_MODE (passed_type);
12271
12272 /* This decl represents a formal parameter which was optimized out.
12273 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
12274 all cases where (rtl == NULL_RTX) just below. */
12275 if (dmode == pmode)
12276 rtl = DECL_INCOMING_RTL (decl);
12277 else if (SCALAR_INT_MODE_P (dmode)
12278 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
12279 && DECL_INCOMING_RTL (decl))
12280 {
12281 rtx inc = DECL_INCOMING_RTL (decl);
12282 if (REG_P (inc))
12283 rtl = inc;
12284 else if (MEM_P (inc))
12285 {
12286 if (BYTES_BIG_ENDIAN)
12287 rtl = adjust_address_nv (inc, dmode,
12288 GET_MODE_SIZE (pmode)
12289 - GET_MODE_SIZE (dmode));
12290 else
12291 rtl = inc;
12292 }
12293 }
12294 }
12295
12296 /* If the parm was passed in registers, but lives on the stack, then
12297 make a big endian correction if the mode of the type of the
12298 parameter is not the same as the mode of the rtl. */
12299 /* ??? This is the same series of checks that are made in dbxout.c before
12300 we reach the big endian correction code there. It isn't clear if all
12301 of these checks are necessary here, but keeping them all is the safe
12302 thing to do. */
12303 else if (MEM_P (rtl)
12304 && XEXP (rtl, 0) != const0_rtx
12305 && ! CONSTANT_P (XEXP (rtl, 0))
12306 /* Not passed in memory. */
12307 && !MEM_P (DECL_INCOMING_RTL (decl))
12308 /* Not passed by invisible reference. */
12309 && (!REG_P (XEXP (rtl, 0))
12310 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
12311 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
12312 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12313 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
12314 #endif
12315 )
12316 /* Big endian correction check. */
12317 && BYTES_BIG_ENDIAN
12318 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
12319 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
12320 < UNITS_PER_WORD))
12321 {
12322 int offset = (UNITS_PER_WORD
12323 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
12324
12325 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
12326 plus_constant (XEXP (rtl, 0), offset));
12327 }
12328 }
12329 else if (TREE_CODE (decl) == VAR_DECL
12330 && rtl
12331 && MEM_P (rtl)
12332 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
12333 && BYTES_BIG_ENDIAN)
12334 {
12335 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
12336 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
12337
12338 /* If a variable is declared "register" yet is smaller than
12339 a register, then if we store the variable to memory, it
12340 looks like we're storing a register-sized value, when in
12341 fact we are not. We need to adjust the offset of the
12342 storage location to reflect the actual value's bytes,
12343 else gdb will not be able to display it. */
12344 if (rsize > dsize)
12345 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
12346 plus_constant (XEXP (rtl, 0), rsize-dsize));
12347 }
12348
12349 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
12350 and will have been substituted directly into all expressions that use it.
12351 C does not have such a concept, but C++ and other languages do. */
12352 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
12353 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
12354
12355 if (rtl)
12356 rtl = targetm.delegitimize_address (rtl);
12357
12358 /* If we don't look past the constant pool, we risk emitting a
12359 reference to a constant pool entry that isn't referenced from
12360 code, and thus is not emitted. */
12361 if (rtl)
12362 rtl = avoid_constant_pool_reference (rtl);
12363
12364 return rtl;
12365 }
12366
12367 /* We need to figure out what section we should use as the base for the
12368 address ranges where a given location is valid.
12369 1. If this particular DECL has a section associated with it, use that.
12370 2. If this function has a section associated with it, use that.
12371 3. Otherwise, use the text section.
12372 XXX: If you split a variable across multiple sections, we won't notice. */
12373
12374 static const char *
12375 secname_for_decl (const_tree decl)
12376 {
12377 const char *secname;
12378
12379 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12380 {
12381 tree sectree = DECL_SECTION_NAME (decl);
12382 secname = TREE_STRING_POINTER (sectree);
12383 }
12384 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12385 {
12386 tree sectree = DECL_SECTION_NAME (current_function_decl);
12387 secname = TREE_STRING_POINTER (sectree);
12388 }
12389 else if (cfun && in_cold_section_p)
12390 secname = crtl->subsections.cold_section_label;
12391 else
12392 secname = text_section_label;
12393
12394 return secname;
12395 }
12396
12397 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
12398 returned. If so, the decl for the COMMON block is returned, and the
12399 value is the offset into the common block for the symbol. */
12400
12401 static tree
12402 fortran_common (tree decl, HOST_WIDE_INT *value)
12403 {
12404 tree val_expr, cvar;
12405 enum machine_mode mode;
12406 HOST_WIDE_INT bitsize, bitpos;
12407 tree offset;
12408 int volatilep = 0, unsignedp = 0;
12409
12410 /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
12411 it does not have a value (the offset into the common area), or if it
12412 is thread local (as opposed to global) then it isn't common, and shouldn't
12413 be handled as such. */
12414 if (TREE_CODE (decl) != VAR_DECL
12415 || !TREE_PUBLIC (decl)
12416 || !TREE_STATIC (decl)
12417 || !DECL_HAS_VALUE_EXPR_P (decl)
12418 || !is_fortran ())
12419 return NULL_TREE;
12420
12421 val_expr = DECL_VALUE_EXPR (decl);
12422 if (TREE_CODE (val_expr) != COMPONENT_REF)
12423 return NULL_TREE;
12424
12425 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
12426 &mode, &unsignedp, &volatilep, true);
12427
12428 if (cvar == NULL_TREE
12429 || TREE_CODE (cvar) != VAR_DECL
12430 || DECL_ARTIFICIAL (cvar)
12431 || !TREE_PUBLIC (cvar))
12432 return NULL_TREE;
12433
12434 *value = 0;
12435 if (offset != NULL)
12436 {
12437 if (!host_integerp (offset, 0))
12438 return NULL_TREE;
12439 *value = tree_low_cst (offset, 0);
12440 }
12441 if (bitpos != 0)
12442 *value += bitpos / BITS_PER_UNIT;
12443
12444 return cvar;
12445 }
12446
12447 /* Dereference a location expression LOC if DECL is passed by invisible
12448 reference. */
12449
12450 static dw_loc_descr_ref
12451 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12452 {
12453 HOST_WIDE_INT size;
12454 enum dwarf_location_atom op;
12455
12456 if (loc == NULL)
12457 return NULL;
12458
12459 if ((TREE_CODE (decl) != PARM_DECL
12460 && TREE_CODE (decl) != RESULT_DECL
12461 && TREE_CODE (decl) != VAR_DECL)
12462 || !DECL_BY_REFERENCE (decl))
12463 return loc;
12464
12465 /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12466 change it into corresponding DW_OP_breg{0...31,x} 0. Then the
12467 location expression is considered to be address of a memory location,
12468 rather than the register itself. */
12469 if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12470 || loc->dw_loc_opc == DW_OP_regx)
12471 && (loc->dw_loc_next == NULL
12472 || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12473 && loc->dw_loc_next->dw_loc_next == NULL)))
12474 {
12475 if (loc->dw_loc_opc == DW_OP_regx)
12476 {
12477 loc->dw_loc_opc = DW_OP_bregx;
12478 loc->dw_loc_oprnd2.v.val_int = 0;
12479 }
12480 else
12481 {
12482 loc->dw_loc_opc
12483 = (enum dwarf_location_atom)
12484 (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12485 loc->dw_loc_oprnd1.v.val_int = 0;
12486 }
12487 return loc;
12488 }
12489
12490 size = int_size_in_bytes (TREE_TYPE (decl));
12491 if (size > DWARF2_ADDR_SIZE || size == -1)
12492 return 0;
12493 else if (size == DWARF2_ADDR_SIZE)
12494 op = DW_OP_deref;
12495 else
12496 op = DW_OP_deref_size;
12497 add_loc_descr (&loc, new_loc_descr (op, size, 0));
12498 return loc;
12499 }
12500
12501 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12502 data attribute for a variable or a parameter. We generate the
12503 DW_AT_const_value attribute only in those cases where the given variable
12504 or parameter does not have a true "location" either in memory or in a
12505 register. This can happen (for example) when a constant is passed as an
12506 actual argument in a call to an inline function. (It's possible that
12507 these things can crop up in other ways also.) Note that one type of
12508 constant value which can be passed into an inlined function is a constant
12509 pointer. This can happen for example if an actual argument in an inlined
12510 function call evaluates to a compile-time constant address. */
12511
12512 static void
12513 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12514 enum dwarf_attribute attr)
12515 {
12516 rtx rtl;
12517 dw_loc_descr_ref descr;
12518 var_loc_list *loc_list;
12519 struct var_loc_node *node;
12520 if (TREE_CODE (decl) == ERROR_MARK)
12521 return;
12522
12523 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12524 || TREE_CODE (decl) == RESULT_DECL);
12525
12526 /* See if we possibly have multiple locations for this variable. */
12527 loc_list = lookup_decl_loc (decl);
12528
12529 /* If it truly has multiple locations, the first and last node will
12530 differ. */
12531 if (loc_list && loc_list->first != loc_list->last)
12532 {
12533 const char *endname, *secname;
12534 dw_loc_list_ref list;
12535 rtx varloc;
12536 enum var_init_status initialized;
12537
12538 /* Now that we know what section we are using for a base,
12539 actually construct the list of locations.
12540 The first location information is what is passed to the
12541 function that creates the location list, and the remaining
12542 locations just get added on to that list.
12543 Note that we only know the start address for a location
12544 (IE location changes), so to build the range, we use
12545 the range [current location start, next location start].
12546 This means we have to special case the last node, and generate
12547 a range of [last location start, end of function label]. */
12548
12549 node = loc_list->first;
12550 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12551 secname = secname_for_decl (decl);
12552
12553 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12554 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12555 else
12556 initialized = VAR_INIT_STATUS_INITIALIZED;
12557
12558 descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12559 list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12560 node = node->next;
12561
12562 for (; node->next; node = node->next)
12563 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12564 {
12565 /* The variable has a location between NODE->LABEL and
12566 NODE->NEXT->LABEL. */
12567 enum var_init_status initialized =
12568 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12569 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12570 descr = loc_by_reference (loc_descriptor (varloc, initialized),
12571 decl);
12572 add_loc_descr_to_loc_list (&list, descr,
12573 node->label, node->next->label, secname);
12574 }
12575
12576 /* If the variable has a location at the last label
12577 it keeps its location until the end of function. */
12578 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12579 {
12580 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12581 enum var_init_status initialized =
12582 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12583
12584 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12585 if (!current_function_decl)
12586 endname = text_end_label;
12587 else
12588 {
12589 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12590 current_function_funcdef_no);
12591 endname = ggc_strdup (label_id);
12592 }
12593 descr = loc_by_reference (loc_descriptor (varloc, initialized),
12594 decl);
12595 add_loc_descr_to_loc_list (&list, descr,
12596 node->label, endname, secname);
12597 }
12598
12599 /* Finally, add the location list to the DIE, and we are done. */
12600 add_AT_loc_list (die, attr, list);
12601 return;
12602 }
12603
12604 /* Try to get some constant RTL for this decl, and use that as the value of
12605 the location. */
12606
12607 rtl = rtl_for_decl_location (decl);
12608 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12609 {
12610 add_const_value_attribute (die, rtl);
12611 return;
12612 }
12613
12614 /* If we have tried to generate the location otherwise, and it
12615 didn't work out (we wouldn't be here if we did), and we have a one entry
12616 location list, try generating a location from that. */
12617 if (loc_list && loc_list->first)
12618 {
12619 enum var_init_status status;
12620 node = loc_list->first;
12621 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12622 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12623 if (descr)
12624 {
12625 descr = loc_by_reference (descr, decl);
12626 add_AT_location_description (die, attr, descr);
12627 return;
12628 }
12629 }
12630
12631 /* We couldn't get any rtl, so try directly generating the location
12632 description from the tree. */
12633 descr = loc_descriptor_from_tree (decl);
12634 if (descr)
12635 {
12636 descr = loc_by_reference (descr, decl);
12637 add_AT_location_description (die, attr, descr);
12638 return;
12639 }
12640 /* None of that worked, so it must not really have a location;
12641 try adding a constant value attribute from the DECL_INITIAL. */
12642 tree_add_const_value_attribute (die, decl);
12643 }
12644
12645 /* Add VARIABLE and DIE into deferred locations list. */
12646
12647 static void
12648 defer_location (tree variable, dw_die_ref die)
12649 {
12650 deferred_locations entry;
12651 entry.variable = variable;
12652 entry.die = die;
12653 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12654 }
12655
12656 /* Helper function for tree_add_const_value_attribute. Natively encode
12657 initializer INIT into an array. Return true if successful. */
12658
12659 static bool
12660 native_encode_initializer (tree init, unsigned char *array, int size)
12661 {
12662 tree type;
12663
12664 if (init == NULL_TREE)
12665 return false;
12666
12667 STRIP_NOPS (init);
12668 switch (TREE_CODE (init))
12669 {
12670 case STRING_CST:
12671 type = TREE_TYPE (init);
12672 if (TREE_CODE (type) == ARRAY_TYPE)
12673 {
12674 tree enttype = TREE_TYPE (type);
12675 enum machine_mode mode = TYPE_MODE (enttype);
12676
12677 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12678 return false;
12679 if (int_size_in_bytes (type) != size)
12680 return false;
12681 if (size > TREE_STRING_LENGTH (init))
12682 {
12683 memcpy (array, TREE_STRING_POINTER (init),
12684 TREE_STRING_LENGTH (init));
12685 memset (array + TREE_STRING_LENGTH (init),
12686 '\0', size - TREE_STRING_LENGTH (init));
12687 }
12688 else
12689 memcpy (array, TREE_STRING_POINTER (init), size);
12690 return true;
12691 }
12692 return false;
12693 case CONSTRUCTOR:
12694 type = TREE_TYPE (init);
12695 if (int_size_in_bytes (type) != size)
12696 return false;
12697 if (TREE_CODE (type) == ARRAY_TYPE)
12698 {
12699 HOST_WIDE_INT min_index;
12700 unsigned HOST_WIDE_INT cnt;
12701 int curpos = 0, fieldsize;
12702 constructor_elt *ce;
12703
12704 if (TYPE_DOMAIN (type) == NULL_TREE
12705 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12706 return false;
12707
12708 fieldsize = int_size_in_bytes (TREE_TYPE (type));
12709 if (fieldsize <= 0)
12710 return false;
12711
12712 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12713 memset (array, '\0', size);
12714 for (cnt = 0;
12715 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12716 cnt++)
12717 {
12718 tree val = ce->value;
12719 tree index = ce->index;
12720 int pos = curpos;
12721 if (index && TREE_CODE (index) == RANGE_EXPR)
12722 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12723 * fieldsize;
12724 else if (index)
12725 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12726
12727 if (val)
12728 {
12729 STRIP_NOPS (val);
12730 if (!native_encode_initializer (val, array + pos, fieldsize))
12731 return false;
12732 }
12733 curpos = pos + fieldsize;
12734 if (index && TREE_CODE (index) == RANGE_EXPR)
12735 {
12736 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12737 - tree_low_cst (TREE_OPERAND (index, 0), 0);
12738 while (count > 0)
12739 {
12740 if (val)
12741 memcpy (array + curpos, array + pos, fieldsize);
12742 curpos += fieldsize;
12743 }
12744 }
12745 gcc_assert (curpos <= size);
12746 }
12747 return true;
12748 }
12749 else if (TREE_CODE (type) == RECORD_TYPE
12750 || TREE_CODE (type) == UNION_TYPE)
12751 {
12752 tree field = NULL_TREE;
12753 unsigned HOST_WIDE_INT cnt;
12754 constructor_elt *ce;
12755
12756 if (int_size_in_bytes (type) != size)
12757 return false;
12758
12759 if (TREE_CODE (type) == RECORD_TYPE)
12760 field = TYPE_FIELDS (type);
12761
12762 for (cnt = 0;
12763 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12764 cnt++, field = field ? TREE_CHAIN (field) : 0)
12765 {
12766 tree val = ce->value;
12767 int pos, fieldsize;
12768
12769 if (ce->index != 0)
12770 field = ce->index;
12771
12772 if (val)
12773 STRIP_NOPS (val);
12774
12775 if (field == NULL_TREE || DECL_BIT_FIELD (field))
12776 return false;
12777
12778 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12779 && TYPE_DOMAIN (TREE_TYPE (field))
12780 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12781 return false;
12782 else if (DECL_SIZE_UNIT (field) == NULL_TREE
12783 || !host_integerp (DECL_SIZE_UNIT (field), 0))
12784 return false;
12785 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12786 pos = int_byte_position (field);
12787 gcc_assert (pos + fieldsize <= size);
12788 if (val
12789 && !native_encode_initializer (val, array + pos, fieldsize))
12790 return false;
12791 }
12792 return true;
12793 }
12794 return false;
12795 case VIEW_CONVERT_EXPR:
12796 case NON_LVALUE_EXPR:
12797 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12798 default:
12799 return native_encode_expr (init, array, size) == size;
12800 }
12801 }
12802
12803 /* If we don't have a copy of this variable in memory for some reason (such
12804 as a C++ member constant that doesn't have an out-of-line definition),
12805 we should tell the debugger about the constant value. */
12806
12807 static void
12808 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12809 {
12810 tree init;
12811 tree type = TREE_TYPE (decl);
12812 rtx rtl;
12813
12814 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12815 return;
12816
12817 init = DECL_INITIAL (decl);
12818 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12819 /* OK */;
12820 else
12821 return;
12822
12823 rtl = rtl_for_decl_init (init, type);
12824 if (rtl)
12825 add_const_value_attribute (var_die, rtl);
12826 /* If the host and target are sane, try harder. */
12827 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12828 && initializer_constant_valid_p (init, type))
12829 {
12830 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12831 if (size > 0 && (int) size == size)
12832 {
12833 unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12834
12835 if (native_encode_initializer (init, array, size))
12836 add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12837 }
12838 }
12839 }
12840
12841 /* Convert the CFI instructions for the current function into a
12842 location list. This is used for DW_AT_frame_base when we targeting
12843 a dwarf2 consumer that does not support the dwarf3
12844 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
12845 expressions. */
12846
12847 static dw_loc_list_ref
12848 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12849 {
12850 dw_fde_ref fde;
12851 dw_loc_list_ref list, *list_tail;
12852 dw_cfi_ref cfi;
12853 dw_cfa_location last_cfa, next_cfa;
12854 const char *start_label, *last_label, *section;
12855 dw_cfa_location remember;
12856
12857 fde = current_fde ();
12858 gcc_assert (fde != NULL);
12859
12860 section = secname_for_decl (current_function_decl);
12861 list_tail = &list;
12862 list = NULL;
12863
12864 memset (&next_cfa, 0, sizeof (next_cfa));
12865 next_cfa.reg = INVALID_REGNUM;
12866 remember = next_cfa;
12867
12868 start_label = fde->dw_fde_begin;
12869
12870 /* ??? Bald assumption that the CIE opcode list does not contain
12871 advance opcodes. */
12872 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12873 lookup_cfa_1 (cfi, &next_cfa, &remember);
12874
12875 last_cfa = next_cfa;
12876 last_label = start_label;
12877
12878 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12879 switch (cfi->dw_cfi_opc)
12880 {
12881 case DW_CFA_set_loc:
12882 case DW_CFA_advance_loc1:
12883 case DW_CFA_advance_loc2:
12884 case DW_CFA_advance_loc4:
12885 if (!cfa_equal_p (&last_cfa, &next_cfa))
12886 {
12887 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12888 start_label, last_label, section,
12889 list == NULL);
12890
12891 list_tail = &(*list_tail)->dw_loc_next;
12892 last_cfa = next_cfa;
12893 start_label = last_label;
12894 }
12895 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12896 break;
12897
12898 case DW_CFA_advance_loc:
12899 /* The encoding is complex enough that we should never emit this. */
12900 gcc_unreachable ();
12901
12902 default:
12903 lookup_cfa_1 (cfi, &next_cfa, &remember);
12904 break;
12905 }
12906
12907 if (!cfa_equal_p (&last_cfa, &next_cfa))
12908 {
12909 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12910 start_label, last_label, section,
12911 list == NULL);
12912 list_tail = &(*list_tail)->dw_loc_next;
12913 start_label = last_label;
12914 }
12915 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12916 start_label, fde->dw_fde_end, section,
12917 list == NULL);
12918
12919 return list;
12920 }
12921
12922 /* Compute a displacement from the "steady-state frame pointer" to the
12923 frame base (often the same as the CFA), and store it in
12924 frame_pointer_fb_offset. OFFSET is added to the displacement
12925 before the latter is negated. */
12926
12927 static void
12928 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12929 {
12930 rtx reg, elim;
12931
12932 #ifdef FRAME_POINTER_CFA_OFFSET
12933 reg = frame_pointer_rtx;
12934 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12935 #else
12936 reg = arg_pointer_rtx;
12937 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12938 #endif
12939
12940 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12941 if (GET_CODE (elim) == PLUS)
12942 {
12943 offset += INTVAL (XEXP (elim, 1));
12944 elim = XEXP (elim, 0);
12945 }
12946
12947 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12948 && (elim == hard_frame_pointer_rtx
12949 || elim == stack_pointer_rtx))
12950 || elim == (frame_pointer_needed
12951 ? hard_frame_pointer_rtx
12952 : stack_pointer_rtx));
12953
12954 frame_pointer_fb_offset = -offset;
12955 }
12956
12957 /* Generate a DW_AT_name attribute given some string value to be included as
12958 the value of the attribute. */
12959
12960 static void
12961 add_name_attribute (dw_die_ref die, const char *name_string)
12962 {
12963 if (name_string != NULL && *name_string != 0)
12964 {
12965 if (demangle_name_func)
12966 name_string = (*demangle_name_func) (name_string);
12967
12968 add_AT_string (die, DW_AT_name, name_string);
12969 }
12970 }
12971
12972 /* Generate a DW_AT_comp_dir attribute for DIE. */
12973
12974 static void
12975 add_comp_dir_attribute (dw_die_ref die)
12976 {
12977 const char *wd = get_src_pwd ();
12978 char *wd1;
12979
12980 if (wd == NULL)
12981 return;
12982
12983 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
12984 {
12985 int wdlen;
12986
12987 wdlen = strlen (wd);
12988 wd1 = GGC_NEWVEC (char, wdlen + 2);
12989 strcpy (wd1, wd);
12990 wd1 [wdlen] = DIR_SEPARATOR;
12991 wd1 [wdlen + 1] = 0;
12992 wd = wd1;
12993 }
12994
12995 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12996 }
12997
12998 /* Given a tree node describing an array bound (either lower or upper) output
12999 a representation for that bound. */
13000
13001 static void
13002 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
13003 {
13004 switch (TREE_CODE (bound))
13005 {
13006 case ERROR_MARK:
13007 return;
13008
13009 /* All fixed-bounds are represented by INTEGER_CST nodes. */
13010 case INTEGER_CST:
13011 if (! host_integerp (bound, 0)
13012 || (bound_attr == DW_AT_lower_bound
13013 && (((is_c_family () || is_java ()) && integer_zerop (bound))
13014 || (is_fortran () && integer_onep (bound)))))
13015 /* Use the default. */
13016 ;
13017 else
13018 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
13019 break;
13020
13021 CASE_CONVERT:
13022 case VIEW_CONVERT_EXPR:
13023 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
13024 break;
13025
13026 case SAVE_EXPR:
13027 break;
13028
13029 case VAR_DECL:
13030 case PARM_DECL:
13031 case RESULT_DECL:
13032 {
13033 dw_die_ref decl_die = lookup_decl_die (bound);
13034 dw_loc_descr_ref loc;
13035
13036 /* ??? Can this happen, or should the variable have been bound
13037 first? Probably it can, since I imagine that we try to create
13038 the types of parameters in the order in which they exist in
13039 the list, and won't have created a forward reference to a
13040 later parameter. */
13041 if (decl_die != NULL)
13042 add_AT_die_ref (subrange_die, bound_attr, decl_die);
13043 else
13044 {
13045 loc = loc_descriptor_from_tree_1 (bound, 0);
13046 add_AT_location_description (subrange_die, bound_attr, loc);
13047 }
13048 break;
13049 }
13050
13051 default:
13052 {
13053 /* Otherwise try to create a stack operation procedure to
13054 evaluate the value of the array bound. */
13055
13056 dw_die_ref ctx, decl_die;
13057 dw_loc_descr_ref loc;
13058
13059 loc = loc_descriptor_from_tree (bound);
13060 if (loc == NULL)
13061 break;
13062
13063 if (current_function_decl == 0)
13064 ctx = comp_unit_die;
13065 else
13066 ctx = lookup_decl_die (current_function_decl);
13067
13068 decl_die = new_die (DW_TAG_variable, ctx, bound);
13069 add_AT_flag (decl_die, DW_AT_artificial, 1);
13070 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
13071 add_AT_loc (decl_die, DW_AT_location, loc);
13072
13073 add_AT_die_ref (subrange_die, bound_attr, decl_die);
13074 break;
13075 }
13076 }
13077 }
13078
13079 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
13080 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
13081 Note that the block of subscript information for an array type also
13082 includes information about the element type of the given array type. */
13083
13084 static void
13085 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
13086 {
13087 unsigned dimension_number;
13088 tree lower, upper;
13089 dw_die_ref subrange_die;
13090
13091 for (dimension_number = 0;
13092 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
13093 type = TREE_TYPE (type), dimension_number++)
13094 {
13095 tree domain = TYPE_DOMAIN (type);
13096
13097 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
13098 break;
13099
13100 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
13101 and (in GNU C only) variable bounds. Handle all three forms
13102 here. */
13103 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
13104 if (domain)
13105 {
13106 /* We have an array type with specified bounds. */
13107 lower = TYPE_MIN_VALUE (domain);
13108 upper = TYPE_MAX_VALUE (domain);
13109
13110 /* Define the index type. */
13111 if (TREE_TYPE (domain))
13112 {
13113 /* ??? This is probably an Ada unnamed subrange type. Ignore the
13114 TREE_TYPE field. We can't emit debug info for this
13115 because it is an unnamed integral type. */
13116 if (TREE_CODE (domain) == INTEGER_TYPE
13117 && TYPE_NAME (domain) == NULL_TREE
13118 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
13119 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
13120 ;
13121 else
13122 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
13123 type_die);
13124 }
13125
13126 /* ??? If upper is NULL, the array has unspecified length,
13127 but it does have a lower bound. This happens with Fortran
13128 dimension arr(N:*)
13129 Since the debugger is definitely going to need to know N
13130 to produce useful results, go ahead and output the lower
13131 bound solo, and hope the debugger can cope. */
13132
13133 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
13134 if (upper)
13135 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
13136 }
13137
13138 /* Otherwise we have an array type with an unspecified length. The
13139 DWARF-2 spec does not say how to handle this; let's just leave out the
13140 bounds. */
13141 }
13142 }
13143
13144 static void
13145 add_byte_size_attribute (dw_die_ref die, tree tree_node)
13146 {
13147 unsigned size;
13148
13149 switch (TREE_CODE (tree_node))
13150 {
13151 case ERROR_MARK:
13152 size = 0;
13153 break;
13154 case ENUMERAL_TYPE:
13155 case RECORD_TYPE:
13156 case UNION_TYPE:
13157 case QUAL_UNION_TYPE:
13158 size = int_size_in_bytes (tree_node);
13159 break;
13160 case FIELD_DECL:
13161 /* For a data member of a struct or union, the DW_AT_byte_size is
13162 generally given as the number of bytes normally allocated for an
13163 object of the *declared* type of the member itself. This is true
13164 even for bit-fields. */
13165 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
13166 break;
13167 default:
13168 gcc_unreachable ();
13169 }
13170
13171 /* Note that `size' might be -1 when we get to this point. If it is, that
13172 indicates that the byte size of the entity in question is variable. We
13173 have no good way of expressing this fact in Dwarf at the present time,
13174 so just let the -1 pass on through. */
13175 add_AT_unsigned (die, DW_AT_byte_size, size);
13176 }
13177
13178 /* For a FIELD_DECL node which represents a bit-field, output an attribute
13179 which specifies the distance in bits from the highest order bit of the
13180 "containing object" for the bit-field to the highest order bit of the
13181 bit-field itself.
13182
13183 For any given bit-field, the "containing object" is a hypothetical object
13184 (of some integral or enum type) within which the given bit-field lives. The
13185 type of this hypothetical "containing object" is always the same as the
13186 declared type of the individual bit-field itself. The determination of the
13187 exact location of the "containing object" for a bit-field is rather
13188 complicated. It's handled by the `field_byte_offset' function (above).
13189
13190 Note that it is the size (in bytes) of the hypothetical "containing object"
13191 which will be given in the DW_AT_byte_size attribute for this bit-field.
13192 (See `byte_size_attribute' above). */
13193
13194 static inline void
13195 add_bit_offset_attribute (dw_die_ref die, tree decl)
13196 {
13197 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
13198 tree type = DECL_BIT_FIELD_TYPE (decl);
13199 HOST_WIDE_INT bitpos_int;
13200 HOST_WIDE_INT highest_order_object_bit_offset;
13201 HOST_WIDE_INT highest_order_field_bit_offset;
13202 HOST_WIDE_INT unsigned bit_offset;
13203
13204 /* Must be a field and a bit field. */
13205 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
13206
13207 /* We can't yet handle bit-fields whose offsets are variable, so if we
13208 encounter such things, just return without generating any attribute
13209 whatsoever. Likewise for variable or too large size. */
13210 if (! host_integerp (bit_position (decl), 0)
13211 || ! host_integerp (DECL_SIZE (decl), 1))
13212 return;
13213
13214 bitpos_int = int_bit_position (decl);
13215
13216 /* Note that the bit offset is always the distance (in bits) from the
13217 highest-order bit of the "containing object" to the highest-order bit of
13218 the bit-field itself. Since the "high-order end" of any object or field
13219 is different on big-endian and little-endian machines, the computation
13220 below must take account of these differences. */
13221 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
13222 highest_order_field_bit_offset = bitpos_int;
13223
13224 if (! BYTES_BIG_ENDIAN)
13225 {
13226 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
13227 highest_order_object_bit_offset += simple_type_size_in_bits (type);
13228 }
13229
13230 bit_offset
13231 = (! BYTES_BIG_ENDIAN
13232 ? highest_order_object_bit_offset - highest_order_field_bit_offset
13233 : highest_order_field_bit_offset - highest_order_object_bit_offset);
13234
13235 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
13236 }
13237
13238 /* For a FIELD_DECL node which represents a bit field, output an attribute
13239 which specifies the length in bits of the given field. */
13240
13241 static inline void
13242 add_bit_size_attribute (dw_die_ref die, tree decl)
13243 {
13244 /* Must be a field and a bit field. */
13245 gcc_assert (TREE_CODE (decl) == FIELD_DECL
13246 && DECL_BIT_FIELD_TYPE (decl));
13247
13248 if (host_integerp (DECL_SIZE (decl), 1))
13249 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
13250 }
13251
13252 /* If the compiled language is ANSI C, then add a 'prototyped'
13253 attribute, if arg types are given for the parameters of a function. */
13254
13255 static inline void
13256 add_prototyped_attribute (dw_die_ref die, tree func_type)
13257 {
13258 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
13259 && TYPE_ARG_TYPES (func_type) != NULL)
13260 add_AT_flag (die, DW_AT_prototyped, 1);
13261 }
13262
13263 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
13264 by looking in either the type declaration or object declaration
13265 equate table. */
13266
13267 static inline dw_die_ref
13268 add_abstract_origin_attribute (dw_die_ref die, tree origin)
13269 {
13270 dw_die_ref origin_die = NULL;
13271
13272 if (TREE_CODE (origin) != FUNCTION_DECL)
13273 {
13274 /* We may have gotten separated from the block for the inlined
13275 function, if we're in an exception handler or some such; make
13276 sure that the abstract function has been written out.
13277
13278 Doing this for nested functions is wrong, however; functions are
13279 distinct units, and our context might not even be inline. */
13280 tree fn = origin;
13281
13282 if (TYPE_P (fn))
13283 fn = TYPE_STUB_DECL (fn);
13284
13285 fn = decl_function_context (fn);
13286 if (fn)
13287 dwarf2out_abstract_function (fn);
13288 }
13289
13290 if (DECL_P (origin))
13291 origin_die = lookup_decl_die (origin);
13292 else if (TYPE_P (origin))
13293 origin_die = lookup_type_die (origin);
13294
13295 /* XXX: Functions that are never lowered don't always have correct block
13296 trees (in the case of java, they simply have no block tree, in some other
13297 languages). For these functions, there is nothing we can really do to
13298 output correct debug info for inlined functions in all cases. Rather
13299 than die, we'll just produce deficient debug info now, in that we will
13300 have variables without a proper abstract origin. In the future, when all
13301 functions are lowered, we should re-add a gcc_assert (origin_die)
13302 here. */
13303
13304 if (origin_die)
13305 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
13306 return origin_die;
13307 }
13308
13309 /* We do not currently support the pure_virtual attribute. */
13310
13311 static inline void
13312 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
13313 {
13314 if (DECL_VINDEX (func_decl))
13315 {
13316 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13317
13318 if (host_integerp (DECL_VINDEX (func_decl), 0))
13319 add_AT_loc (die, DW_AT_vtable_elem_location,
13320 new_loc_descr (DW_OP_constu,
13321 tree_low_cst (DECL_VINDEX (func_decl), 0),
13322 0));
13323
13324 /* GNU extension: Record what type this method came from originally. */
13325 if (debug_info_level > DINFO_LEVEL_TERSE)
13326 add_AT_die_ref (die, DW_AT_containing_type,
13327 lookup_type_die (DECL_CONTEXT (func_decl)));
13328 }
13329 }
13330 \f
13331 /* Add source coordinate attributes for the given decl. */
13332
13333 static void
13334 add_src_coords_attributes (dw_die_ref die, tree decl)
13335 {
13336 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13337
13338 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
13339 add_AT_unsigned (die, DW_AT_decl_line, s.line);
13340 }
13341
13342 /* Add a DW_AT_name attribute and source coordinate attribute for the
13343 given decl, but only if it actually has a name. */
13344
13345 static void
13346 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
13347 {
13348 tree decl_name;
13349
13350 decl_name = DECL_NAME (decl);
13351 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
13352 {
13353 add_name_attribute (die, dwarf2_name (decl, 0));
13354 if (! DECL_ARTIFICIAL (decl))
13355 add_src_coords_attributes (die, decl);
13356
13357 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
13358 && TREE_PUBLIC (decl)
13359 && !DECL_ABSTRACT (decl)
13360 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
13361 && !is_fortran ())
13362 {
13363 /* Defer until we have an assembler name set. */
13364 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
13365 {
13366 limbo_die_node *asm_name;
13367
13368 asm_name = GGC_CNEW (limbo_die_node);
13369 asm_name->die = die;
13370 asm_name->created_for = decl;
13371 asm_name->next = deferred_asm_name;
13372 deferred_asm_name = asm_name;
13373 }
13374 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
13375 add_AT_string (die, DW_AT_MIPS_linkage_name,
13376 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
13377 }
13378 }
13379
13380 #ifdef VMS_DEBUGGING_INFO
13381 /* Get the function's name, as described by its RTL. This may be different
13382 from the DECL_NAME name used in the source file. */
13383 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
13384 {
13385 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
13386 XEXP (DECL_RTL (decl), 0));
13387 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
13388 }
13389 #endif
13390 }
13391
13392 /* Push a new declaration scope. */
13393
13394 static void
13395 push_decl_scope (tree scope)
13396 {
13397 VEC_safe_push (tree, gc, decl_scope_table, scope);
13398 }
13399
13400 /* Pop a declaration scope. */
13401
13402 static inline void
13403 pop_decl_scope (void)
13404 {
13405 VEC_pop (tree, decl_scope_table);
13406 }
13407
13408 /* Return the DIE for the scope that immediately contains this type.
13409 Non-named types get global scope. Named types nested in other
13410 types get their containing scope if it's open, or global scope
13411 otherwise. All other types (i.e. function-local named types) get
13412 the current active scope. */
13413
13414 static dw_die_ref
13415 scope_die_for (tree t, dw_die_ref context_die)
13416 {
13417 dw_die_ref scope_die = NULL;
13418 tree containing_scope;
13419 int i;
13420
13421 /* Non-types always go in the current scope. */
13422 gcc_assert (TYPE_P (t));
13423
13424 containing_scope = TYPE_CONTEXT (t);
13425
13426 /* Use the containing namespace if it was passed in (for a declaration). */
13427 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
13428 {
13429 if (context_die == lookup_decl_die (containing_scope))
13430 /* OK */;
13431 else
13432 containing_scope = NULL_TREE;
13433 }
13434
13435 /* Ignore function type "scopes" from the C frontend. They mean that
13436 a tagged type is local to a parmlist of a function declarator, but
13437 that isn't useful to DWARF. */
13438 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
13439 containing_scope = NULL_TREE;
13440
13441 if (containing_scope == NULL_TREE)
13442 scope_die = comp_unit_die;
13443 else if (TYPE_P (containing_scope))
13444 {
13445 /* For types, we can just look up the appropriate DIE. But
13446 first we check to see if we're in the middle of emitting it
13447 so we know where the new DIE should go. */
13448 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
13449 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
13450 break;
13451
13452 if (i < 0)
13453 {
13454 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
13455 || TREE_ASM_WRITTEN (containing_scope));
13456
13457 /* If none of the current dies are suitable, we get file scope. */
13458 scope_die = comp_unit_die;
13459 }
13460 else
13461 scope_die = lookup_type_die (containing_scope);
13462 }
13463 else
13464 scope_die = context_die;
13465
13466 return scope_die;
13467 }
13468
13469 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
13470
13471 static inline int
13472 local_scope_p (dw_die_ref context_die)
13473 {
13474 for (; context_die; context_die = context_die->die_parent)
13475 if (context_die->die_tag == DW_TAG_inlined_subroutine
13476 || context_die->die_tag == DW_TAG_subprogram)
13477 return 1;
13478
13479 return 0;
13480 }
13481
13482 /* Returns nonzero if CONTEXT_DIE is a class. */
13483
13484 static inline int
13485 class_scope_p (dw_die_ref context_die)
13486 {
13487 return (context_die
13488 && (context_die->die_tag == DW_TAG_structure_type
13489 || context_die->die_tag == DW_TAG_class_type
13490 || context_die->die_tag == DW_TAG_interface_type
13491 || context_die->die_tag == DW_TAG_union_type));
13492 }
13493
13494 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
13495 whether or not to treat a DIE in this context as a declaration. */
13496
13497 static inline int
13498 class_or_namespace_scope_p (dw_die_ref context_die)
13499 {
13500 return (class_scope_p (context_die)
13501 || (context_die && context_die->die_tag == DW_TAG_namespace));
13502 }
13503
13504 /* Many forms of DIEs require a "type description" attribute. This
13505 routine locates the proper "type descriptor" die for the type given
13506 by 'type', and adds a DW_AT_type attribute below the given die. */
13507
13508 static void
13509 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13510 int decl_volatile, dw_die_ref context_die)
13511 {
13512 enum tree_code code = TREE_CODE (type);
13513 dw_die_ref type_die = NULL;
13514
13515 /* ??? If this type is an unnamed subrange type of an integral, floating-point
13516 or fixed-point type, use the inner type. This is because we have no
13517 support for unnamed types in base_type_die. This can happen if this is
13518 an Ada subrange type. Correct solution is emit a subrange type die. */
13519 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13520 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13521 type = TREE_TYPE (type), code = TREE_CODE (type);
13522
13523 if (code == ERROR_MARK
13524 /* Handle a special case. For functions whose return type is void, we
13525 generate *no* type attribute. (Note that no object may have type
13526 `void', so this only applies to function return types). */
13527 || code == VOID_TYPE)
13528 return;
13529
13530 type_die = modified_type_die (type,
13531 decl_const || TYPE_READONLY (type),
13532 decl_volatile || TYPE_VOLATILE (type),
13533 context_die);
13534
13535 if (type_die != NULL)
13536 add_AT_die_ref (object_die, DW_AT_type, type_die);
13537 }
13538
13539 /* Given an object die, add the calling convention attribute for the
13540 function call type. */
13541 static void
13542 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13543 {
13544 enum dwarf_calling_convention value = DW_CC_normal;
13545
13546 value = ((enum dwarf_calling_convention)
13547 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13548
13549 /* DWARF doesn't provide a way to identify a program's source-level
13550 entry point. DW_AT_calling_convention attributes are only meant
13551 to describe functions' calling conventions. However, lacking a
13552 better way to signal the Fortran main program, we use this for the
13553 time being, following existing custom. */
13554 if (is_fortran ()
13555 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13556 value = DW_CC_program;
13557
13558 /* Only add the attribute if the backend requests it, and
13559 is not DW_CC_normal. */
13560 if (value && (value != DW_CC_normal))
13561 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13562 }
13563
13564 /* Given a tree pointer to a struct, class, union, or enum type node, return
13565 a pointer to the (string) tag name for the given type, or zero if the type
13566 was declared without a tag. */
13567
13568 static const char *
13569 type_tag (const_tree type)
13570 {
13571 const char *name = 0;
13572
13573 if (TYPE_NAME (type) != 0)
13574 {
13575 tree t = 0;
13576
13577 /* Find the IDENTIFIER_NODE for the type name. */
13578 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13579 t = TYPE_NAME (type);
13580
13581 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13582 a TYPE_DECL node, regardless of whether or not a `typedef' was
13583 involved. */
13584 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13585 && ! DECL_IGNORED_P (TYPE_NAME (type)))
13586 {
13587 /* We want to be extra verbose. Don't call dwarf_name if
13588 DECL_NAME isn't set. The default hook for decl_printable_name
13589 doesn't like that, and in this context it's correct to return
13590 0, instead of "<anonymous>" or the like. */
13591 if (DECL_NAME (TYPE_NAME (type)))
13592 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13593 }
13594
13595 /* Now get the name as a string, or invent one. */
13596 if (!name && t != 0)
13597 name = IDENTIFIER_POINTER (t);
13598 }
13599
13600 return (name == 0 || *name == '\0') ? 0 : name;
13601 }
13602
13603 /* Return the type associated with a data member, make a special check
13604 for bit field types. */
13605
13606 static inline tree
13607 member_declared_type (const_tree member)
13608 {
13609 return (DECL_BIT_FIELD_TYPE (member)
13610 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13611 }
13612
13613 /* Get the decl's label, as described by its RTL. This may be different
13614 from the DECL_NAME name used in the source file. */
13615
13616 #if 0
13617 static const char *
13618 decl_start_label (tree decl)
13619 {
13620 rtx x;
13621 const char *fnname;
13622
13623 x = DECL_RTL (decl);
13624 gcc_assert (MEM_P (x));
13625
13626 x = XEXP (x, 0);
13627 gcc_assert (GET_CODE (x) == SYMBOL_REF);
13628
13629 fnname = XSTR (x, 0);
13630 return fnname;
13631 }
13632 #endif
13633 \f
13634 /* These routines generate the internal representation of the DIE's for
13635 the compilation unit. Debugging information is collected by walking
13636 the declaration trees passed in from dwarf2out_decl(). */
13637
13638 static void
13639 gen_array_type_die (tree type, dw_die_ref context_die)
13640 {
13641 dw_die_ref scope_die = scope_die_for (type, context_die);
13642 dw_die_ref array_die;
13643
13644 /* GNU compilers represent multidimensional array types as sequences of one
13645 dimensional array types whose element types are themselves array types.
13646 We sometimes squish that down to a single array_type DIE with multiple
13647 subscripts in the Dwarf debugging info. The draft Dwarf specification
13648 say that we are allowed to do this kind of compression in C, because
13649 there is no difference between an array of arrays and a multidimensional
13650 array. We don't do this for Ada to remain as close as possible to the
13651 actual representation, which is especially important against the language
13652 flexibilty wrt arrays of variable size. */
13653
13654 bool collapse_nested_arrays = !is_ada ();
13655 tree element_type;
13656
13657 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13658 DW_TAG_string_type doesn't have DW_AT_type attribute). */
13659 if (TYPE_STRING_FLAG (type)
13660 && TREE_CODE (type) == ARRAY_TYPE
13661 && is_fortran ()
13662 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13663 {
13664 HOST_WIDE_INT size;
13665
13666 array_die = new_die (DW_TAG_string_type, scope_die, type);
13667 add_name_attribute (array_die, type_tag (type));
13668 equate_type_number_to_die (type, array_die);
13669 size = int_size_in_bytes (type);
13670 if (size >= 0)
13671 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13672 else if (TYPE_DOMAIN (type) != NULL_TREE
13673 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13674 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13675 {
13676 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13677 dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13678
13679 size = int_size_in_bytes (TREE_TYPE (szdecl));
13680 if (loc && size > 0)
13681 {
13682 add_AT_loc (array_die, DW_AT_string_length, loc);
13683 if (size != DWARF2_ADDR_SIZE)
13684 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13685 }
13686 }
13687 return;
13688 }
13689
13690 /* ??? The SGI dwarf reader fails for array of array of enum types
13691 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13692 array type comes before the outer array type. We thus call gen_type_die
13693 before we new_die and must prevent nested array types collapsing for this
13694 target. */
13695
13696 #ifdef MIPS_DEBUGGING_INFO
13697 gen_type_die (TREE_TYPE (type), context_die);
13698 collapse_nested_arrays = false;
13699 #endif
13700
13701 array_die = new_die (DW_TAG_array_type, scope_die, type);
13702 add_name_attribute (array_die, type_tag (type));
13703 equate_type_number_to_die (type, array_die);
13704
13705 if (TREE_CODE (type) == VECTOR_TYPE)
13706 {
13707 /* The frontend feeds us a representation for the vector as a struct
13708 containing an array. Pull out the array type. */
13709 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13710 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13711 }
13712
13713 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
13714 if (is_fortran ()
13715 && TREE_CODE (type) == ARRAY_TYPE
13716 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13717 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13718 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13719
13720 #if 0
13721 /* We default the array ordering. SDB will probably do
13722 the right things even if DW_AT_ordering is not present. It's not even
13723 an issue until we start to get into multidimensional arrays anyway. If
13724 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13725 then we'll have to put the DW_AT_ordering attribute back in. (But if
13726 and when we find out that we need to put these in, we will only do so
13727 for multidimensional arrays. */
13728 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13729 #endif
13730
13731 #ifdef MIPS_DEBUGGING_INFO
13732 /* The SGI compilers handle arrays of unknown bound by setting
13733 AT_declaration and not emitting any subrange DIEs. */
13734 if (! TYPE_DOMAIN (type))
13735 add_AT_flag (array_die, DW_AT_declaration, 1);
13736 else
13737 #endif
13738 add_subscript_info (array_die, type, collapse_nested_arrays);
13739
13740 /* Add representation of the type of the elements of this array type and
13741 emit the corresponding DIE if we haven't done it already. */
13742 element_type = TREE_TYPE (type);
13743 if (collapse_nested_arrays)
13744 while (TREE_CODE (element_type) == ARRAY_TYPE)
13745 {
13746 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13747 break;
13748 element_type = TREE_TYPE (element_type);
13749 }
13750
13751 #ifndef MIPS_DEBUGGING_INFO
13752 gen_type_die (element_type, context_die);
13753 #endif
13754
13755 add_type_attribute (array_die, element_type, 0, 0, context_die);
13756
13757 if (get_AT (array_die, DW_AT_name))
13758 add_pubtype (type, array_die);
13759 }
13760
13761 static dw_loc_descr_ref
13762 descr_info_loc (tree val, tree base_decl)
13763 {
13764 HOST_WIDE_INT size;
13765 dw_loc_descr_ref loc, loc2;
13766 enum dwarf_location_atom op;
13767
13768 if (val == base_decl)
13769 return new_loc_descr (DW_OP_push_object_address, 0, 0);
13770
13771 switch (TREE_CODE (val))
13772 {
13773 CASE_CONVERT:
13774 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13775 case VAR_DECL:
13776 return loc_descriptor_from_tree_1 (val, 0);
13777 case INTEGER_CST:
13778 if (host_integerp (val, 0))
13779 return int_loc_descriptor (tree_low_cst (val, 0));
13780 break;
13781 case INDIRECT_REF:
13782 size = int_size_in_bytes (TREE_TYPE (val));
13783 if (size < 0)
13784 break;
13785 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13786 if (!loc)
13787 break;
13788 if (size == DWARF2_ADDR_SIZE)
13789 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13790 else
13791 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13792 return loc;
13793 case POINTER_PLUS_EXPR:
13794 case PLUS_EXPR:
13795 if (host_integerp (TREE_OPERAND (val, 1), 1)
13796 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13797 < 16384)
13798 {
13799 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13800 if (!loc)
13801 break;
13802 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13803 }
13804 else
13805 {
13806 op = DW_OP_plus;
13807 do_binop:
13808 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13809 if (!loc)
13810 break;
13811 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13812 if (!loc2)
13813 break;
13814 add_loc_descr (&loc, loc2);
13815 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13816 }
13817 return loc;
13818 case MINUS_EXPR:
13819 op = DW_OP_minus;
13820 goto do_binop;
13821 case MULT_EXPR:
13822 op = DW_OP_mul;
13823 goto do_binop;
13824 case EQ_EXPR:
13825 op = DW_OP_eq;
13826 goto do_binop;
13827 case NE_EXPR:
13828 op = DW_OP_ne;
13829 goto do_binop;
13830 default:
13831 break;
13832 }
13833 return NULL;
13834 }
13835
13836 static void
13837 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13838 tree val, tree base_decl)
13839 {
13840 dw_loc_descr_ref loc;
13841
13842 if (host_integerp (val, 0))
13843 {
13844 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13845 return;
13846 }
13847
13848 loc = descr_info_loc (val, base_decl);
13849 if (!loc)
13850 return;
13851
13852 add_AT_loc (die, attr, loc);
13853 }
13854
13855 /* This routine generates DIE for array with hidden descriptor, details
13856 are filled into *info by a langhook. */
13857
13858 static void
13859 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13860 dw_die_ref context_die)
13861 {
13862 dw_die_ref scope_die = scope_die_for (type, context_die);
13863 dw_die_ref array_die;
13864 int dim;
13865
13866 array_die = new_die (DW_TAG_array_type, scope_die, type);
13867 add_name_attribute (array_die, type_tag (type));
13868 equate_type_number_to_die (type, array_die);
13869
13870 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
13871 if (is_fortran ()
13872 && info->ndimensions >= 2)
13873 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13874
13875 if (info->data_location)
13876 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13877 info->base_decl);
13878 if (info->associated)
13879 add_descr_info_field (array_die, DW_AT_associated, info->associated,
13880 info->base_decl);
13881 if (info->allocated)
13882 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13883 info->base_decl);
13884
13885 for (dim = 0; dim < info->ndimensions; dim++)
13886 {
13887 dw_die_ref subrange_die
13888 = new_die (DW_TAG_subrange_type, array_die, NULL);
13889
13890 if (info->dimen[dim].lower_bound)
13891 {
13892 /* If it is the default value, omit it. */
13893 if ((is_c_family () || is_java ())
13894 && integer_zerop (info->dimen[dim].lower_bound))
13895 ;
13896 else if (is_fortran ()
13897 && integer_onep (info->dimen[dim].lower_bound))
13898 ;
13899 else
13900 add_descr_info_field (subrange_die, DW_AT_lower_bound,
13901 info->dimen[dim].lower_bound,
13902 info->base_decl);
13903 }
13904 if (info->dimen[dim].upper_bound)
13905 add_descr_info_field (subrange_die, DW_AT_upper_bound,
13906 info->dimen[dim].upper_bound,
13907 info->base_decl);
13908 if (info->dimen[dim].stride)
13909 add_descr_info_field (subrange_die, DW_AT_byte_stride,
13910 info->dimen[dim].stride,
13911 info->base_decl);
13912 }
13913
13914 gen_type_die (info->element_type, context_die);
13915 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13916
13917 if (get_AT (array_die, DW_AT_name))
13918 add_pubtype (type, array_die);
13919 }
13920
13921 #if 0
13922 static void
13923 gen_entry_point_die (tree decl, dw_die_ref context_die)
13924 {
13925 tree origin = decl_ultimate_origin (decl);
13926 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13927
13928 if (origin != NULL)
13929 add_abstract_origin_attribute (decl_die, origin);
13930 else
13931 {
13932 add_name_and_src_coords_attributes (decl_die, decl);
13933 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13934 0, 0, context_die);
13935 }
13936
13937 if (DECL_ABSTRACT (decl))
13938 equate_decl_number_to_die (decl, decl_die);
13939 else
13940 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13941 }
13942 #endif
13943
13944 /* Walk through the list of incomplete types again, trying once more to
13945 emit full debugging info for them. */
13946
13947 static void
13948 retry_incomplete_types (void)
13949 {
13950 int i;
13951
13952 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13953 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13954 }
13955
13956 /* Determine what tag to use for a record type. */
13957
13958 static enum dwarf_tag
13959 record_type_tag (tree type)
13960 {
13961 if (! lang_hooks.types.classify_record)
13962 return DW_TAG_structure_type;
13963
13964 switch (lang_hooks.types.classify_record (type))
13965 {
13966 case RECORD_IS_STRUCT:
13967 return DW_TAG_structure_type;
13968
13969 case RECORD_IS_CLASS:
13970 return DW_TAG_class_type;
13971
13972 case RECORD_IS_INTERFACE:
13973 return DW_TAG_interface_type;
13974
13975 default:
13976 gcc_unreachable ();
13977 }
13978 }
13979
13980 /* Generate a DIE to represent an enumeration type. Note that these DIEs
13981 include all of the information about the enumeration values also. Each
13982 enumerated type name/value is listed as a child of the enumerated type
13983 DIE. */
13984
13985 static dw_die_ref
13986 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13987 {
13988 dw_die_ref type_die = lookup_type_die (type);
13989
13990 if (type_die == NULL)
13991 {
13992 type_die = new_die (DW_TAG_enumeration_type,
13993 scope_die_for (type, context_die), type);
13994 equate_type_number_to_die (type, type_die);
13995 add_name_attribute (type_die, type_tag (type));
13996 }
13997 else if (! TYPE_SIZE (type))
13998 return type_die;
13999 else
14000 remove_AT (type_die, DW_AT_declaration);
14001
14002 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
14003 given enum type is incomplete, do not generate the DW_AT_byte_size
14004 attribute or the DW_AT_element_list attribute. */
14005 if (TYPE_SIZE (type))
14006 {
14007 tree link;
14008
14009 TREE_ASM_WRITTEN (type) = 1;
14010 add_byte_size_attribute (type_die, type);
14011 if (TYPE_STUB_DECL (type) != NULL_TREE)
14012 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14013
14014 /* If the first reference to this type was as the return type of an
14015 inline function, then it may not have a parent. Fix this now. */
14016 if (type_die->die_parent == NULL)
14017 add_child_die (scope_die_for (type, context_die), type_die);
14018
14019 for (link = TYPE_VALUES (type);
14020 link != NULL; link = TREE_CHAIN (link))
14021 {
14022 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
14023 tree value = TREE_VALUE (link);
14024
14025 add_name_attribute (enum_die,
14026 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
14027
14028 if (TREE_CODE (value) == CONST_DECL)
14029 value = DECL_INITIAL (value);
14030
14031 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
14032 /* DWARF2 does not provide a way of indicating whether or
14033 not enumeration constants are signed or unsigned. GDB
14034 always assumes the values are signed, so we output all
14035 values as if they were signed. That means that
14036 enumeration constants with very large unsigned values
14037 will appear to have negative values in the debugger. */
14038 add_AT_int (enum_die, DW_AT_const_value,
14039 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
14040 }
14041 }
14042 else
14043 add_AT_flag (type_die, DW_AT_declaration, 1);
14044
14045 if (get_AT (type_die, DW_AT_name))
14046 add_pubtype (type, type_die);
14047
14048 return type_die;
14049 }
14050
14051 /* Generate a DIE to represent either a real live formal parameter decl or to
14052 represent just the type of some formal parameter position in some function
14053 type.
14054
14055 Note that this routine is a bit unusual because its argument may be a
14056 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
14057 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
14058 node. If it's the former then this function is being called to output a
14059 DIE to represent a formal parameter object (or some inlining thereof). If
14060 it's the latter, then this function is only being called to output a
14061 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
14062 argument type of some subprogram type. */
14063
14064 static dw_die_ref
14065 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
14066 {
14067 tree node_or_origin = node ? node : origin;
14068 dw_die_ref parm_die
14069 = new_die (DW_TAG_formal_parameter, context_die, node);
14070
14071 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
14072 {
14073 case tcc_declaration:
14074 if (!origin)
14075 origin = decl_ultimate_origin (node);
14076 if (origin != NULL)
14077 add_abstract_origin_attribute (parm_die, origin);
14078 else
14079 {
14080 tree type = TREE_TYPE (node);
14081 add_name_and_src_coords_attributes (parm_die, node);
14082 if (DECL_BY_REFERENCE (node))
14083 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
14084 context_die);
14085 else
14086 add_type_attribute (parm_die, type,
14087 TREE_READONLY (node),
14088 TREE_THIS_VOLATILE (node),
14089 context_die);
14090 if (DECL_ARTIFICIAL (node))
14091 add_AT_flag (parm_die, DW_AT_artificial, 1);
14092 }
14093
14094 if (node && node != origin)
14095 equate_decl_number_to_die (node, parm_die);
14096 if (! DECL_ABSTRACT (node_or_origin))
14097 add_location_or_const_value_attribute (parm_die, node_or_origin,
14098 DW_AT_location);
14099
14100 break;
14101
14102 case tcc_type:
14103 /* We were called with some kind of a ..._TYPE node. */
14104 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
14105 break;
14106
14107 default:
14108 gcc_unreachable ();
14109 }
14110
14111 return parm_die;
14112 }
14113
14114 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
14115 at the end of an (ANSI prototyped) formal parameters list. */
14116
14117 static void
14118 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
14119 {
14120 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
14121 }
14122
14123 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
14124 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
14125 parameters as specified in some function type specification (except for
14126 those which appear as part of a function *definition*). */
14127
14128 static void
14129 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
14130 {
14131 tree link;
14132 tree formal_type = NULL;
14133 tree first_parm_type;
14134 tree arg;
14135
14136 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
14137 {
14138 arg = DECL_ARGUMENTS (function_or_method_type);
14139 function_or_method_type = TREE_TYPE (function_or_method_type);
14140 }
14141 else
14142 arg = NULL_TREE;
14143
14144 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
14145
14146 /* Make our first pass over the list of formal parameter types and output a
14147 DW_TAG_formal_parameter DIE for each one. */
14148 for (link = first_parm_type; link; )
14149 {
14150 dw_die_ref parm_die;
14151
14152 formal_type = TREE_VALUE (link);
14153 if (formal_type == void_type_node)
14154 break;
14155
14156 /* Output a (nameless) DIE to represent the formal parameter itself. */
14157 parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
14158 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
14159 && link == first_parm_type)
14160 || (arg && DECL_ARTIFICIAL (arg)))
14161 add_AT_flag (parm_die, DW_AT_artificial, 1);
14162
14163 link = TREE_CHAIN (link);
14164 if (arg)
14165 arg = TREE_CHAIN (arg);
14166 }
14167
14168 /* If this function type has an ellipsis, add a
14169 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
14170 if (formal_type != void_type_node)
14171 gen_unspecified_parameters_die (function_or_method_type, context_die);
14172
14173 /* Make our second (and final) pass over the list of formal parameter types
14174 and output DIEs to represent those types (as necessary). */
14175 for (link = TYPE_ARG_TYPES (function_or_method_type);
14176 link && TREE_VALUE (link);
14177 link = TREE_CHAIN (link))
14178 gen_type_die (TREE_VALUE (link), context_die);
14179 }
14180
14181 /* We want to generate the DIE for TYPE so that we can generate the
14182 die for MEMBER, which has been defined; we will need to refer back
14183 to the member declaration nested within TYPE. If we're trying to
14184 generate minimal debug info for TYPE, processing TYPE won't do the
14185 trick; we need to attach the member declaration by hand. */
14186
14187 static void
14188 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
14189 {
14190 gen_type_die (type, context_die);
14191
14192 /* If we're trying to avoid duplicate debug info, we may not have
14193 emitted the member decl for this function. Emit it now. */
14194 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
14195 && ! lookup_decl_die (member))
14196 {
14197 dw_die_ref type_die;
14198 gcc_assert (!decl_ultimate_origin (member));
14199
14200 push_decl_scope (type);
14201 type_die = lookup_type_die (type);
14202 if (TREE_CODE (member) == FUNCTION_DECL)
14203 gen_subprogram_die (member, type_die);
14204 else if (TREE_CODE (member) == FIELD_DECL)
14205 {
14206 /* Ignore the nameless fields that are used to skip bits but handle
14207 C++ anonymous unions and structs. */
14208 if (DECL_NAME (member) != NULL_TREE
14209 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
14210 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
14211 {
14212 gen_type_die (member_declared_type (member), type_die);
14213 gen_field_die (member, type_die);
14214 }
14215 }
14216 else
14217 gen_variable_die (member, NULL_TREE, type_die);
14218
14219 pop_decl_scope ();
14220 }
14221 }
14222
14223 /* Generate the DWARF2 info for the "abstract" instance of a function which we
14224 may later generate inlined and/or out-of-line instances of. */
14225
14226 static void
14227 dwarf2out_abstract_function (tree decl)
14228 {
14229 dw_die_ref old_die;
14230 tree save_fn;
14231 tree context;
14232 int was_abstract = DECL_ABSTRACT (decl);
14233
14234 /* Make sure we have the actual abstract inline, not a clone. */
14235 decl = DECL_ORIGIN (decl);
14236 htab_empty (decl_loc_table);
14237
14238 old_die = lookup_decl_die (decl);
14239 if (old_die && get_AT (old_die, DW_AT_inline))
14240 /* We've already generated the abstract instance. */
14241 return;
14242
14243 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
14244 we don't get confused by DECL_ABSTRACT. */
14245 if (debug_info_level > DINFO_LEVEL_TERSE)
14246 {
14247 context = decl_class_context (decl);
14248 if (context)
14249 gen_type_die_for_member
14250 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
14251 }
14252
14253 /* Pretend we've just finished compiling this function. */
14254 save_fn = current_function_decl;
14255 current_function_decl = decl;
14256 push_cfun (DECL_STRUCT_FUNCTION (decl));
14257
14258 set_decl_abstract_flags (decl, 1);
14259 dwarf2out_decl (decl);
14260 if (! was_abstract)
14261 set_decl_abstract_flags (decl, 0);
14262
14263 current_function_decl = save_fn;
14264 pop_cfun ();
14265 }
14266
14267 /* Helper function of premark_used_types() which gets called through
14268 htab_traverse_resize().
14269
14270 Marks the DIE of a given type in *SLOT as perennial, so it never gets
14271 marked as unused by prune_unused_types. */
14272 static int
14273 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
14274 {
14275 tree type;
14276 dw_die_ref die;
14277
14278 type = (tree) *slot;
14279 die = lookup_type_die (type);
14280 if (die != NULL)
14281 die->die_perennial_p = 1;
14282 return 1;
14283 }
14284
14285 /* Mark all members of used_types_hash as perennial. */
14286 static void
14287 premark_used_types (void)
14288 {
14289 if (cfun && cfun->used_types_hash)
14290 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
14291 }
14292
14293 /* Generate a DIE to represent a declared function (either file-scope or
14294 block-local). */
14295
14296 static void
14297 gen_subprogram_die (tree decl, dw_die_ref context_die)
14298 {
14299 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14300 tree origin = decl_ultimate_origin (decl);
14301 dw_die_ref subr_die;
14302 tree fn_arg_types;
14303 tree outer_scope;
14304 dw_die_ref old_die = lookup_decl_die (decl);
14305 int declaration = (current_function_decl != decl
14306 || class_or_namespace_scope_p (context_die));
14307
14308 premark_used_types ();
14309
14310 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
14311 started to generate the abstract instance of an inline, decided to output
14312 its containing class, and proceeded to emit the declaration of the inline
14313 from the member list for the class. If so, DECLARATION takes priority;
14314 we'll get back to the abstract instance when done with the class. */
14315
14316 /* The class-scope declaration DIE must be the primary DIE. */
14317 if (origin && declaration && class_or_namespace_scope_p (context_die))
14318 {
14319 origin = NULL;
14320 gcc_assert (!old_die);
14321 }
14322
14323 /* Now that the C++ front end lazily declares artificial member fns, we
14324 might need to retrofit the declaration into its class. */
14325 if (!declaration && !origin && !old_die
14326 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
14327 && !class_or_namespace_scope_p (context_die)
14328 && debug_info_level > DINFO_LEVEL_TERSE)
14329 old_die = force_decl_die (decl);
14330
14331 if (origin != NULL)
14332 {
14333 gcc_assert (!declaration || local_scope_p (context_die));
14334
14335 /* Fixup die_parent for the abstract instance of a nested
14336 inline function. */
14337 if (old_die && old_die->die_parent == NULL)
14338 add_child_die (context_die, old_die);
14339
14340 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14341 add_abstract_origin_attribute (subr_die, origin);
14342 }
14343 else if (old_die)
14344 {
14345 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14346 struct dwarf_file_data * file_index = lookup_filename (s.file);
14347
14348 if (!get_AT_flag (old_die, DW_AT_declaration)
14349 /* We can have a normal definition following an inline one in the
14350 case of redefinition of GNU C extern inlines.
14351 It seems reasonable to use AT_specification in this case. */
14352 && !get_AT (old_die, DW_AT_inline))
14353 {
14354 /* Detect and ignore this case, where we are trying to output
14355 something we have already output. */
14356 return;
14357 }
14358
14359 /* If the definition comes from the same place as the declaration,
14360 maybe use the old DIE. We always want the DIE for this function
14361 that has the *_pc attributes to be under comp_unit_die so the
14362 debugger can find it. We also need to do this for abstract
14363 instances of inlines, since the spec requires the out-of-line copy
14364 to have the same parent. For local class methods, this doesn't
14365 apply; we just use the old DIE. */
14366 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
14367 && (DECL_ARTIFICIAL (decl)
14368 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
14369 && (get_AT_unsigned (old_die, DW_AT_decl_line)
14370 == (unsigned) s.line))))
14371 {
14372 subr_die = old_die;
14373
14374 /* Clear out the declaration attribute and the formal parameters.
14375 Do not remove all children, because it is possible that this
14376 declaration die was forced using force_decl_die(). In such
14377 cases die that forced declaration die (e.g. TAG_imported_module)
14378 is one of the children that we do not want to remove. */
14379 remove_AT (subr_die, DW_AT_declaration);
14380 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
14381 }
14382 else
14383 {
14384 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14385 add_AT_specification (subr_die, old_die);
14386 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14387 add_AT_file (subr_die, DW_AT_decl_file, file_index);
14388 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14389 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
14390 }
14391 }
14392 else
14393 {
14394 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14395
14396 if (TREE_PUBLIC (decl))
14397 add_AT_flag (subr_die, DW_AT_external, 1);
14398
14399 add_name_and_src_coords_attributes (subr_die, decl);
14400 if (debug_info_level > DINFO_LEVEL_TERSE)
14401 {
14402 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
14403 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
14404 0, 0, context_die);
14405 }
14406
14407 add_pure_or_virtual_attribute (subr_die, decl);
14408 if (DECL_ARTIFICIAL (decl))
14409 add_AT_flag (subr_die, DW_AT_artificial, 1);
14410
14411 if (TREE_PROTECTED (decl))
14412 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
14413 else if (TREE_PRIVATE (decl))
14414 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
14415 }
14416
14417 if (declaration)
14418 {
14419 if (!old_die || !get_AT (old_die, DW_AT_inline))
14420 {
14421 add_AT_flag (subr_die, DW_AT_declaration, 1);
14422
14423 /* If this is an explicit function declaration then generate
14424 a DW_AT_explicit attribute. */
14425 if (lang_hooks.decls.function_decl_explicit_p (decl))
14426 add_AT_flag (subr_die, DW_AT_explicit, 1);
14427
14428 /* The first time we see a member function, it is in the context of
14429 the class to which it belongs. We make sure of this by emitting
14430 the class first. The next time is the definition, which is
14431 handled above. The two may come from the same source text.
14432
14433 Note that force_decl_die() forces function declaration die. It is
14434 later reused to represent definition. */
14435 equate_decl_number_to_die (decl, subr_die);
14436 }
14437 }
14438 else if (DECL_ABSTRACT (decl))
14439 {
14440 if (DECL_DECLARED_INLINE_P (decl))
14441 {
14442 if (cgraph_function_possibly_inlined_p (decl))
14443 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
14444 else
14445 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
14446 }
14447 else
14448 {
14449 if (cgraph_function_possibly_inlined_p (decl))
14450 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
14451 else
14452 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
14453 }
14454
14455 if (DECL_DECLARED_INLINE_P (decl)
14456 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
14457 add_AT_flag (subr_die, DW_AT_artificial, 1);
14458
14459 equate_decl_number_to_die (decl, subr_die);
14460 }
14461 else if (!DECL_EXTERNAL (decl))
14462 {
14463 HOST_WIDE_INT cfa_fb_offset;
14464
14465 if (!old_die || !get_AT (old_die, DW_AT_inline))
14466 equate_decl_number_to_die (decl, subr_die);
14467
14468 if (!flag_reorder_blocks_and_partition)
14469 {
14470 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
14471 current_function_funcdef_no);
14472 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
14473 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14474 current_function_funcdef_no);
14475 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
14476
14477 add_pubname (decl, subr_die);
14478 add_arange (decl, subr_die);
14479 }
14480 else
14481 { /* Do nothing for now; maybe need to duplicate die, one for
14482 hot section and one for cold section, then use the hot/cold
14483 section begin/end labels to generate the aranges... */
14484 /*
14485 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
14486 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
14487 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
14488 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
14489
14490 add_pubname (decl, subr_die);
14491 add_arange (decl, subr_die);
14492 add_arange (decl, subr_die);
14493 */
14494 }
14495
14496 #ifdef MIPS_DEBUGGING_INFO
14497 /* Add a reference to the FDE for this routine. */
14498 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
14499 #endif
14500
14501 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
14502
14503 /* We define the "frame base" as the function's CFA. This is more
14504 convenient for several reasons: (1) It's stable across the prologue
14505 and epilogue, which makes it better than just a frame pointer,
14506 (2) With dwarf3, there exists a one-byte encoding that allows us
14507 to reference the .debug_frame data by proxy, but failing that,
14508 (3) We can at least reuse the code inspection and interpretation
14509 code that determines the CFA position at various points in the
14510 function. */
14511 if (dwarf_version >= 3)
14512 {
14513 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
14514 add_AT_loc (subr_die, DW_AT_frame_base, op);
14515 }
14516 else
14517 {
14518 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14519 if (list->dw_loc_next)
14520 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14521 else
14522 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14523 }
14524
14525 /* Compute a displacement from the "steady-state frame pointer" to
14526 the CFA. The former is what all stack slots and argument slots
14527 will reference in the rtl; the later is what we've told the
14528 debugger about. We'll need to adjust all frame_base references
14529 by this displacement. */
14530 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14531
14532 if (cfun->static_chain_decl)
14533 add_AT_location_description (subr_die, DW_AT_static_link,
14534 loc_descriptor_from_tree (cfun->static_chain_decl));
14535 }
14536
14537 /* Now output descriptions of the arguments for this function. This gets
14538 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14539 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14540 `...' at the end of the formal parameter list. In order to find out if
14541 there was a trailing ellipsis or not, we must instead look at the type
14542 associated with the FUNCTION_DECL. This will be a node of type
14543 FUNCTION_TYPE. If the chain of type nodes hanging off of this
14544 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14545 an ellipsis at the end. */
14546
14547 /* In the case where we are describing a mere function declaration, all we
14548 need to do here (and all we *can* do here) is to describe the *types* of
14549 its formal parameters. */
14550 if (debug_info_level <= DINFO_LEVEL_TERSE)
14551 ;
14552 else if (declaration)
14553 gen_formal_types_die (decl, subr_die);
14554 else
14555 {
14556 /* Generate DIEs to represent all known formal parameters. */
14557 tree arg_decls = DECL_ARGUMENTS (decl);
14558 tree parm;
14559
14560 /* When generating DIEs, generate the unspecified_parameters DIE
14561 instead if we come across the arg "__builtin_va_alist" */
14562 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14563 if (TREE_CODE (parm) == PARM_DECL)
14564 {
14565 if (DECL_NAME (parm)
14566 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14567 "__builtin_va_alist"))
14568 gen_unspecified_parameters_die (parm, subr_die);
14569 else
14570 gen_decl_die (parm, NULL, subr_die);
14571 }
14572
14573 /* Decide whether we need an unspecified_parameters DIE at the end.
14574 There are 2 more cases to do this for: 1) the ansi ... declaration -
14575 this is detectable when the end of the arg list is not a
14576 void_type_node 2) an unprototyped function declaration (not a
14577 definition). This just means that we have no info about the
14578 parameters at all. */
14579 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14580 if (fn_arg_types != NULL)
14581 {
14582 /* This is the prototyped case, check for.... */
14583 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14584 gen_unspecified_parameters_die (decl, subr_die);
14585 }
14586 else if (DECL_INITIAL (decl) == NULL_TREE)
14587 gen_unspecified_parameters_die (decl, subr_die);
14588 }
14589
14590 /* Output Dwarf info for all of the stuff within the body of the function
14591 (if it has one - it may be just a declaration). */
14592 outer_scope = DECL_INITIAL (decl);
14593
14594 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14595 a function. This BLOCK actually represents the outermost binding contour
14596 for the function, i.e. the contour in which the function's formal
14597 parameters and labels get declared. Curiously, it appears that the front
14598 end doesn't actually put the PARM_DECL nodes for the current function onto
14599 the BLOCK_VARS list for this outer scope, but are strung off of the
14600 DECL_ARGUMENTS list for the function instead.
14601
14602 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14603 the LABEL_DECL nodes for the function however, and we output DWARF info
14604 for those in decls_for_scope. Just within the `outer_scope' there will be
14605 a BLOCK node representing the function's outermost pair of curly braces,
14606 and any blocks used for the base and member initializers of a C++
14607 constructor function. */
14608 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14609 {
14610 /* Emit a DW_TAG_variable DIE for a named return value. */
14611 if (DECL_NAME (DECL_RESULT (decl)))
14612 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14613
14614 current_function_has_inlines = 0;
14615 decls_for_scope (outer_scope, subr_die, 0);
14616
14617 #if 0 && defined (MIPS_DEBUGGING_INFO)
14618 if (current_function_has_inlines)
14619 {
14620 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14621 if (! comp_unit_has_inlines)
14622 {
14623 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14624 comp_unit_has_inlines = 1;
14625 }
14626 }
14627 #endif
14628 }
14629 /* Add the calling convention attribute if requested. */
14630 add_calling_convention_attribute (subr_die, decl);
14631
14632 }
14633
14634 /* Returns a hash value for X (which really is a die_struct). */
14635
14636 static hashval_t
14637 common_block_die_table_hash (const void *x)
14638 {
14639 const_dw_die_ref d = (const_dw_die_ref) x;
14640 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14641 }
14642
14643 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14644 as decl_id and die_parent of die_struct Y. */
14645
14646 static int
14647 common_block_die_table_eq (const void *x, const void *y)
14648 {
14649 const_dw_die_ref d = (const_dw_die_ref) x;
14650 const_dw_die_ref e = (const_dw_die_ref) y;
14651 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14652 }
14653
14654 /* Generate a DIE to represent a declared data object.
14655 Either DECL or ORIGIN must be non-null. */
14656
14657 static void
14658 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14659 {
14660 HOST_WIDE_INT off;
14661 tree com_decl;
14662 tree decl_or_origin = decl ? decl : origin;
14663 dw_die_ref var_die;
14664 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14665 dw_die_ref origin_die;
14666 int declaration = (DECL_EXTERNAL (decl_or_origin)
14667 /* If DECL is COMDAT and has not actually been
14668 emitted, we cannot take its address; there
14669 might end up being no definition anywhere in
14670 the program. For example, consider the C++
14671 test case:
14672
14673 template <class T>
14674 struct S { static const int i = 7; };
14675
14676 template <class T>
14677 const int S<T>::i;
14678
14679 int f() { return S<int>::i; }
14680
14681 Here, S<int>::i is not DECL_EXTERNAL, but no
14682 definition is required, so the compiler will
14683 not emit a definition. */
14684 || (TREE_CODE (decl_or_origin) == VAR_DECL
14685 && DECL_COMDAT (decl_or_origin)
14686 && !TREE_ASM_WRITTEN (decl_or_origin))
14687 || class_or_namespace_scope_p (context_die));
14688
14689 if (!origin)
14690 origin = decl_ultimate_origin (decl);
14691
14692 com_decl = fortran_common (decl_or_origin, &off);
14693
14694 /* Symbol in common gets emitted as a child of the common block, in the form
14695 of a data member. */
14696 if (com_decl)
14697 {
14698 tree field;
14699 dw_die_ref com_die;
14700 dw_loc_descr_ref loc;
14701 die_node com_die_arg;
14702
14703 var_die = lookup_decl_die (decl_or_origin);
14704 if (var_die)
14705 {
14706 if (get_AT (var_die, DW_AT_location) == NULL)
14707 {
14708 loc = loc_descriptor_from_tree (com_decl);
14709 if (loc)
14710 {
14711 if (off)
14712 {
14713 /* Optimize the common case. */
14714 if (loc->dw_loc_opc == DW_OP_addr
14715 && loc->dw_loc_next == NULL
14716 && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14717 == SYMBOL_REF)
14718 loc->dw_loc_oprnd1.v.val_addr
14719 = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14720 else
14721 loc_descr_plus_const (&loc, off);
14722 }
14723 add_AT_loc (var_die, DW_AT_location, loc);
14724 remove_AT (var_die, DW_AT_declaration);
14725 }
14726 }
14727 return;
14728 }
14729
14730 if (common_block_die_table == NULL)
14731 common_block_die_table
14732 = htab_create_ggc (10, common_block_die_table_hash,
14733 common_block_die_table_eq, NULL);
14734
14735 field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14736 com_die_arg.decl_id = DECL_UID (com_decl);
14737 com_die_arg.die_parent = context_die;
14738 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14739 loc = loc_descriptor_from_tree (com_decl);
14740 if (com_die == NULL)
14741 {
14742 const char *cnam
14743 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14744 void **slot;
14745
14746 com_die = new_die (DW_TAG_common_block, context_die, decl);
14747 add_name_and_src_coords_attributes (com_die, com_decl);
14748 if (loc)
14749 {
14750 add_AT_loc (com_die, DW_AT_location, loc);
14751 /* Avoid sharing the same loc descriptor between
14752 DW_TAG_common_block and DW_TAG_variable. */
14753 loc = loc_descriptor_from_tree (com_decl);
14754 }
14755 else if (DECL_EXTERNAL (decl))
14756 add_AT_flag (com_die, DW_AT_declaration, 1);
14757 add_pubname_string (cnam, com_die); /* ??? needed? */
14758 com_die->decl_id = DECL_UID (com_decl);
14759 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14760 *slot = (void *) com_die;
14761 }
14762 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14763 {
14764 add_AT_loc (com_die, DW_AT_location, loc);
14765 loc = loc_descriptor_from_tree (com_decl);
14766 remove_AT (com_die, DW_AT_declaration);
14767 }
14768 var_die = new_die (DW_TAG_variable, com_die, decl);
14769 add_name_and_src_coords_attributes (var_die, decl);
14770 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14771 TREE_THIS_VOLATILE (decl), context_die);
14772 add_AT_flag (var_die, DW_AT_external, 1);
14773 if (loc)
14774 {
14775 if (off)
14776 {
14777 /* Optimize the common case. */
14778 if (loc->dw_loc_opc == DW_OP_addr
14779 && loc->dw_loc_next == NULL
14780 && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14781 loc->dw_loc_oprnd1.v.val_addr
14782 = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14783 else
14784 loc_descr_plus_const (&loc, off);
14785 }
14786 add_AT_loc (var_die, DW_AT_location, loc);
14787 }
14788 else if (DECL_EXTERNAL (decl))
14789 add_AT_flag (var_die, DW_AT_declaration, 1);
14790 equate_decl_number_to_die (decl, var_die);
14791 return;
14792 }
14793
14794 /* If the compiler emitted a definition for the DECL declaration
14795 and if we already emitted a DIE for it, don't emit a second
14796 DIE for it again. */
14797 if (old_die
14798 && declaration
14799 && old_die->die_parent == context_die)
14800 return;
14801
14802 /* For static data members, the declaration in the class is supposed
14803 to have DW_TAG_member tag; the specification should still be
14804 DW_TAG_variable referencing the DW_TAG_member DIE. */
14805 if (declaration && class_scope_p (context_die))
14806 var_die = new_die (DW_TAG_member, context_die, decl);
14807 else
14808 var_die = new_die (DW_TAG_variable, context_die, decl);
14809
14810 origin_die = NULL;
14811 if (origin != NULL)
14812 origin_die = add_abstract_origin_attribute (var_die, origin);
14813
14814 /* Loop unrolling can create multiple blocks that refer to the same
14815 static variable, so we must test for the DW_AT_declaration flag.
14816
14817 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14818 copy decls and set the DECL_ABSTRACT flag on them instead of
14819 sharing them.
14820
14821 ??? Duplicated blocks have been rewritten to use .debug_ranges.
14822
14823 ??? The declare_in_namespace support causes us to get two DIEs for one
14824 variable, both of which are declarations. We want to avoid considering
14825 one to be a specification, so we must test that this DIE is not a
14826 declaration. */
14827 else if (old_die && TREE_STATIC (decl) && ! declaration
14828 && get_AT_flag (old_die, DW_AT_declaration) == 1)
14829 {
14830 /* This is a definition of a C++ class level static. */
14831 add_AT_specification (var_die, old_die);
14832 if (DECL_NAME (decl))
14833 {
14834 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14835 struct dwarf_file_data * file_index = lookup_filename (s.file);
14836
14837 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14838 add_AT_file (var_die, DW_AT_decl_file, file_index);
14839
14840 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14841 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14842 }
14843 }
14844 else
14845 {
14846 tree type = TREE_TYPE (decl);
14847
14848 add_name_and_src_coords_attributes (var_die, decl);
14849 if ((TREE_CODE (decl) == PARM_DECL
14850 || TREE_CODE (decl) == RESULT_DECL
14851 || TREE_CODE (decl) == VAR_DECL)
14852 && DECL_BY_REFERENCE (decl))
14853 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14854 else
14855 add_type_attribute (var_die, type, TREE_READONLY (decl),
14856 TREE_THIS_VOLATILE (decl), context_die);
14857
14858 if (TREE_PUBLIC (decl))
14859 add_AT_flag (var_die, DW_AT_external, 1);
14860
14861 if (DECL_ARTIFICIAL (decl))
14862 add_AT_flag (var_die, DW_AT_artificial, 1);
14863
14864 if (TREE_PROTECTED (decl))
14865 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14866 else if (TREE_PRIVATE (decl))
14867 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14868 }
14869
14870 if (declaration)
14871 add_AT_flag (var_die, DW_AT_declaration, 1);
14872
14873 if (decl && (DECL_ABSTRACT (decl) || declaration))
14874 equate_decl_number_to_die (decl, var_die);
14875
14876 if (! declaration
14877 && (! DECL_ABSTRACT (decl_or_origin)
14878 /* Local static vars are shared between all clones/inlines,
14879 so emit DW_AT_location on the abstract DIE if DECL_RTL is
14880 already set. */
14881 || (TREE_CODE (decl_or_origin) == VAR_DECL
14882 && TREE_STATIC (decl_or_origin)
14883 && DECL_RTL_SET_P (decl_or_origin)))
14884 /* When abstract origin already has DW_AT_location attribute, no need
14885 to add it again. */
14886 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14887 {
14888 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14889 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14890 defer_location (decl_or_origin, var_die);
14891 else
14892 add_location_or_const_value_attribute (var_die,
14893 decl_or_origin,
14894 DW_AT_location);
14895 add_pubname (decl_or_origin, var_die);
14896 }
14897 else
14898 tree_add_const_value_attribute (var_die, decl_or_origin);
14899 }
14900
14901 /* Generate a DIE to represent a named constant. */
14902
14903 static void
14904 gen_const_die (tree decl, dw_die_ref context_die)
14905 {
14906 dw_die_ref const_die;
14907 tree type = TREE_TYPE (decl);
14908
14909 const_die = new_die (DW_TAG_constant, context_die, decl);
14910 add_name_and_src_coords_attributes (const_die, decl);
14911 add_type_attribute (const_die, type, 1, 0, context_die);
14912 if (TREE_PUBLIC (decl))
14913 add_AT_flag (const_die, DW_AT_external, 1);
14914 if (DECL_ARTIFICIAL (decl))
14915 add_AT_flag (const_die, DW_AT_artificial, 1);
14916 tree_add_const_value_attribute (const_die, decl);
14917 }
14918
14919 /* Generate a DIE to represent a label identifier. */
14920
14921 static void
14922 gen_label_die (tree decl, dw_die_ref context_die)
14923 {
14924 tree origin = decl_ultimate_origin (decl);
14925 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14926 rtx insn;
14927 char label[MAX_ARTIFICIAL_LABEL_BYTES];
14928
14929 if (origin != NULL)
14930 add_abstract_origin_attribute (lbl_die, origin);
14931 else
14932 add_name_and_src_coords_attributes (lbl_die, decl);
14933
14934 if (DECL_ABSTRACT (decl))
14935 equate_decl_number_to_die (decl, lbl_die);
14936 else
14937 {
14938 insn = DECL_RTL_IF_SET (decl);
14939
14940 /* Deleted labels are programmer specified labels which have been
14941 eliminated because of various optimizations. We still emit them
14942 here so that it is possible to put breakpoints on them. */
14943 if (insn
14944 && (LABEL_P (insn)
14945 || ((NOTE_P (insn)
14946 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14947 {
14948 /* When optimization is enabled (via -O) some parts of the compiler
14949 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14950 represent source-level labels which were explicitly declared by
14951 the user. This really shouldn't be happening though, so catch
14952 it if it ever does happen. */
14953 gcc_assert (!INSN_DELETED_P (insn));
14954
14955 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14956 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14957 }
14958 }
14959 }
14960
14961 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
14962 attributes to the DIE for a block STMT, to describe where the inlined
14963 function was called from. This is similar to add_src_coords_attributes. */
14964
14965 static inline void
14966 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14967 {
14968 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14969
14970 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14971 add_AT_unsigned (die, DW_AT_call_line, s.line);
14972 }
14973
14974
14975 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14976 Add low_pc and high_pc attributes to the DIE for a block STMT. */
14977
14978 static inline void
14979 add_high_low_attributes (tree stmt, dw_die_ref die)
14980 {
14981 char label[MAX_ARTIFICIAL_LABEL_BYTES];
14982
14983 if (BLOCK_FRAGMENT_CHAIN (stmt))
14984 {
14985 tree chain;
14986
14987 if (inlined_function_outer_scope_p (stmt))
14988 {
14989 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14990 BLOCK_NUMBER (stmt));
14991 add_AT_lbl_id (die, DW_AT_entry_pc, label);
14992 }
14993
14994 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14995
14996 chain = BLOCK_FRAGMENT_CHAIN (stmt);
14997 do
14998 {
14999 add_ranges (chain);
15000 chain = BLOCK_FRAGMENT_CHAIN (chain);
15001 }
15002 while (chain);
15003 add_ranges (NULL);
15004 }
15005 else
15006 {
15007 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15008 BLOCK_NUMBER (stmt));
15009 add_AT_lbl_id (die, DW_AT_low_pc, label);
15010 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
15011 BLOCK_NUMBER (stmt));
15012 add_AT_lbl_id (die, DW_AT_high_pc, label);
15013 }
15014 }
15015
15016 /* Generate a DIE for a lexical block. */
15017
15018 static void
15019 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
15020 {
15021 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
15022
15023 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
15024 add_high_low_attributes (stmt, stmt_die);
15025
15026 decls_for_scope (stmt, stmt_die, depth);
15027 }
15028
15029 /* Generate a DIE for an inlined subprogram. */
15030
15031 static void
15032 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
15033 {
15034 tree decl;
15035
15036 /* The instance of function that is effectively being inlined shall not
15037 be abstract. */
15038 gcc_assert (! BLOCK_ABSTRACT (stmt));
15039
15040 decl = block_ultimate_origin (stmt);
15041
15042 /* Emit info for the abstract instance first, if we haven't yet. We
15043 must emit this even if the block is abstract, otherwise when we
15044 emit the block below (or elsewhere), we may end up trying to emit
15045 a die whose origin die hasn't been emitted, and crashing. */
15046 dwarf2out_abstract_function (decl);
15047
15048 if (! BLOCK_ABSTRACT (stmt))
15049 {
15050 dw_die_ref subr_die
15051 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
15052
15053 add_abstract_origin_attribute (subr_die, decl);
15054 if (TREE_ASM_WRITTEN (stmt))
15055 add_high_low_attributes (stmt, subr_die);
15056 add_call_src_coords_attributes (stmt, subr_die);
15057
15058 decls_for_scope (stmt, subr_die, depth);
15059 current_function_has_inlines = 1;
15060 }
15061 }
15062
15063 /* Generate a DIE for a field in a record, or structure. */
15064
15065 static void
15066 gen_field_die (tree decl, dw_die_ref context_die)
15067 {
15068 dw_die_ref decl_die;
15069
15070 if (TREE_TYPE (decl) == error_mark_node)
15071 return;
15072
15073 decl_die = new_die (DW_TAG_member, context_die, decl);
15074 add_name_and_src_coords_attributes (decl_die, decl);
15075 add_type_attribute (decl_die, member_declared_type (decl),
15076 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
15077 context_die);
15078
15079 if (DECL_BIT_FIELD_TYPE (decl))
15080 {
15081 add_byte_size_attribute (decl_die, decl);
15082 add_bit_size_attribute (decl_die, decl);
15083 add_bit_offset_attribute (decl_die, decl);
15084 }
15085
15086 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
15087 add_data_member_location_attribute (decl_die, decl);
15088
15089 if (DECL_ARTIFICIAL (decl))
15090 add_AT_flag (decl_die, DW_AT_artificial, 1);
15091
15092 if (TREE_PROTECTED (decl))
15093 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
15094 else if (TREE_PRIVATE (decl))
15095 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
15096
15097 /* Equate decl number to die, so that we can look up this decl later on. */
15098 equate_decl_number_to_die (decl, decl_die);
15099 }
15100
15101 #if 0
15102 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
15103 Use modified_type_die instead.
15104 We keep this code here just in case these types of DIEs may be needed to
15105 represent certain things in other languages (e.g. Pascal) someday. */
15106
15107 static void
15108 gen_pointer_type_die (tree type, dw_die_ref context_die)
15109 {
15110 dw_die_ref ptr_die
15111 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
15112
15113 equate_type_number_to_die (type, ptr_die);
15114 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
15115 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
15116 }
15117
15118 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
15119 Use modified_type_die instead.
15120 We keep this code here just in case these types of DIEs may be needed to
15121 represent certain things in other languages (e.g. Pascal) someday. */
15122
15123 static void
15124 gen_reference_type_die (tree type, dw_die_ref context_die)
15125 {
15126 dw_die_ref ref_die
15127 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
15128
15129 equate_type_number_to_die (type, ref_die);
15130 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
15131 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
15132 }
15133 #endif
15134
15135 /* Generate a DIE for a pointer to a member type. */
15136
15137 static void
15138 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
15139 {
15140 dw_die_ref ptr_die
15141 = new_die (DW_TAG_ptr_to_member_type,
15142 scope_die_for (type, context_die), type);
15143
15144 equate_type_number_to_die (type, ptr_die);
15145 add_AT_die_ref (ptr_die, DW_AT_containing_type,
15146 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
15147 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
15148 }
15149
15150 /* Generate the DIE for the compilation unit. */
15151
15152 static dw_die_ref
15153 gen_compile_unit_die (const char *filename)
15154 {
15155 dw_die_ref die;
15156 char producer[250];
15157 const char *language_string = lang_hooks.name;
15158 int language;
15159
15160 die = new_die (DW_TAG_compile_unit, NULL, NULL);
15161
15162 if (filename)
15163 {
15164 add_name_attribute (die, filename);
15165 /* Don't add cwd for <built-in>. */
15166 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
15167 add_comp_dir_attribute (die);
15168 }
15169
15170 sprintf (producer, "%s %s", language_string, version_string);
15171
15172 #ifdef MIPS_DEBUGGING_INFO
15173 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
15174 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
15175 not appear in the producer string, the debugger reaches the conclusion
15176 that the object file is stripped and has no debugging information.
15177 To get the MIPS/SGI debugger to believe that there is debugging
15178 information in the object file, we add a -g to the producer string. */
15179 if (debug_info_level > DINFO_LEVEL_TERSE)
15180 strcat (producer, " -g");
15181 #endif
15182
15183 add_AT_string (die, DW_AT_producer, producer);
15184
15185 if (strcmp (language_string, "GNU C++") == 0)
15186 language = DW_LANG_C_plus_plus;
15187 else if (strcmp (language_string, "GNU Ada") == 0)
15188 language = DW_LANG_Ada95;
15189 else if (strcmp (language_string, "GNU F77") == 0)
15190 language = DW_LANG_Fortran77;
15191 else if (strcmp (language_string, "GNU Fortran") == 0)
15192 language = DW_LANG_Fortran95;
15193 else if (strcmp (language_string, "GNU Pascal") == 0)
15194 language = DW_LANG_Pascal83;
15195 else if (strcmp (language_string, "GNU Java") == 0)
15196 language = DW_LANG_Java;
15197 else if (strcmp (language_string, "GNU Objective-C") == 0)
15198 language = DW_LANG_ObjC;
15199 else if (strcmp (language_string, "GNU Objective-C++") == 0)
15200 language = DW_LANG_ObjC_plus_plus;
15201 else
15202 language = DW_LANG_C89;
15203
15204 add_AT_unsigned (die, DW_AT_language, language);
15205 return die;
15206 }
15207
15208 /* Generate the DIE for a base class. */
15209
15210 static void
15211 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
15212 {
15213 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
15214
15215 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
15216 add_data_member_location_attribute (die, binfo);
15217
15218 if (BINFO_VIRTUAL_P (binfo))
15219 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15220
15221 if (access == access_public_node)
15222 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15223 else if (access == access_protected_node)
15224 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15225 }
15226
15227 /* Generate a DIE for a class member. */
15228
15229 static void
15230 gen_member_die (tree type, dw_die_ref context_die)
15231 {
15232 tree member;
15233 tree binfo = TYPE_BINFO (type);
15234 dw_die_ref child;
15235
15236 /* If this is not an incomplete type, output descriptions of each of its
15237 members. Note that as we output the DIEs necessary to represent the
15238 members of this record or union type, we will also be trying to output
15239 DIEs to represent the *types* of those members. However the `type'
15240 function (above) will specifically avoid generating type DIEs for member
15241 types *within* the list of member DIEs for this (containing) type except
15242 for those types (of members) which are explicitly marked as also being
15243 members of this (containing) type themselves. The g++ front- end can
15244 force any given type to be treated as a member of some other (containing)
15245 type by setting the TYPE_CONTEXT of the given (member) type to point to
15246 the TREE node representing the appropriate (containing) type. */
15247
15248 /* First output info about the base classes. */
15249 if (binfo)
15250 {
15251 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
15252 int i;
15253 tree base;
15254
15255 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
15256 gen_inheritance_die (base,
15257 (accesses ? VEC_index (tree, accesses, i)
15258 : access_public_node), context_die);
15259 }
15260
15261 /* Now output info about the data members and type members. */
15262 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
15263 {
15264 /* If we thought we were generating minimal debug info for TYPE
15265 and then changed our minds, some of the member declarations
15266 may have already been defined. Don't define them again, but
15267 do put them in the right order. */
15268
15269 child = lookup_decl_die (member);
15270 if (child)
15271 splice_child_die (context_die, child);
15272 else
15273 gen_decl_die (member, NULL, context_die);
15274 }
15275
15276 /* Now output info about the function members (if any). */
15277 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
15278 {
15279 /* Don't include clones in the member list. */
15280 if (DECL_ABSTRACT_ORIGIN (member))
15281 continue;
15282
15283 child = lookup_decl_die (member);
15284 if (child)
15285 splice_child_die (context_die, child);
15286 else
15287 gen_decl_die (member, NULL, context_die);
15288 }
15289 }
15290
15291 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
15292 is set, we pretend that the type was never defined, so we only get the
15293 member DIEs needed by later specification DIEs. */
15294
15295 static void
15296 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
15297 enum debug_info_usage usage)
15298 {
15299 dw_die_ref type_die = lookup_type_die (type);
15300 dw_die_ref scope_die = 0;
15301 int nested = 0;
15302 int complete = (TYPE_SIZE (type)
15303 && (! TYPE_STUB_DECL (type)
15304 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
15305 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
15306 complete = complete && should_emit_struct_debug (type, usage);
15307
15308 if (type_die && ! complete)
15309 return;
15310
15311 if (TYPE_CONTEXT (type) != NULL_TREE
15312 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15313 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
15314 nested = 1;
15315
15316 scope_die = scope_die_for (type, context_die);
15317
15318 if (! type_die || (nested && scope_die == comp_unit_die))
15319 /* First occurrence of type or toplevel definition of nested class. */
15320 {
15321 dw_die_ref old_die = type_die;
15322
15323 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
15324 ? record_type_tag (type) : DW_TAG_union_type,
15325 scope_die, type);
15326 equate_type_number_to_die (type, type_die);
15327 if (old_die)
15328 add_AT_specification (type_die, old_die);
15329 else
15330 add_name_attribute (type_die, type_tag (type));
15331 }
15332 else
15333 remove_AT (type_die, DW_AT_declaration);
15334
15335 /* If this type has been completed, then give it a byte_size attribute and
15336 then give a list of members. */
15337 if (complete && !ns_decl)
15338 {
15339 /* Prevent infinite recursion in cases where the type of some member of
15340 this type is expressed in terms of this type itself. */
15341 TREE_ASM_WRITTEN (type) = 1;
15342 add_byte_size_attribute (type_die, type);
15343 if (TYPE_STUB_DECL (type) != NULL_TREE)
15344 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
15345
15346 /* If the first reference to this type was as the return type of an
15347 inline function, then it may not have a parent. Fix this now. */
15348 if (type_die->die_parent == NULL)
15349 add_child_die (scope_die, type_die);
15350
15351 push_decl_scope (type);
15352 gen_member_die (type, type_die);
15353 pop_decl_scope ();
15354
15355 /* GNU extension: Record what type our vtable lives in. */
15356 if (TYPE_VFIELD (type))
15357 {
15358 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
15359
15360 gen_type_die (vtype, context_die);
15361 add_AT_die_ref (type_die, DW_AT_containing_type,
15362 lookup_type_die (vtype));
15363 }
15364 }
15365 else
15366 {
15367 add_AT_flag (type_die, DW_AT_declaration, 1);
15368
15369 /* We don't need to do this for function-local types. */
15370 if (TYPE_STUB_DECL (type)
15371 && ! decl_function_context (TYPE_STUB_DECL (type)))
15372 VEC_safe_push (tree, gc, incomplete_types, type);
15373 }
15374
15375 if (get_AT (type_die, DW_AT_name))
15376 add_pubtype (type, type_die);
15377 }
15378
15379 /* Generate a DIE for a subroutine _type_. */
15380
15381 static void
15382 gen_subroutine_type_die (tree type, dw_die_ref context_die)
15383 {
15384 tree return_type = TREE_TYPE (type);
15385 dw_die_ref subr_die
15386 = new_die (DW_TAG_subroutine_type,
15387 scope_die_for (type, context_die), type);
15388
15389 equate_type_number_to_die (type, subr_die);
15390 add_prototyped_attribute (subr_die, type);
15391 add_type_attribute (subr_die, return_type, 0, 0, context_die);
15392 gen_formal_types_die (type, subr_die);
15393
15394 if (get_AT (subr_die, DW_AT_name))
15395 add_pubtype (type, subr_die);
15396 }
15397
15398 /* Generate a DIE for a type definition. */
15399
15400 static void
15401 gen_typedef_die (tree decl, dw_die_ref context_die)
15402 {
15403 dw_die_ref type_die;
15404 tree origin;
15405
15406 if (TREE_ASM_WRITTEN (decl))
15407 return;
15408
15409 TREE_ASM_WRITTEN (decl) = 1;
15410 type_die = new_die (DW_TAG_typedef, context_die, decl);
15411 origin = decl_ultimate_origin (decl);
15412 if (origin != NULL)
15413 add_abstract_origin_attribute (type_die, origin);
15414 else
15415 {
15416 tree type;
15417
15418 add_name_and_src_coords_attributes (type_die, decl);
15419 if (DECL_ORIGINAL_TYPE (decl))
15420 {
15421 type = DECL_ORIGINAL_TYPE (decl);
15422
15423 gcc_assert (type != TREE_TYPE (decl));
15424 equate_type_number_to_die (TREE_TYPE (decl), type_die);
15425 }
15426 else
15427 type = TREE_TYPE (decl);
15428
15429 add_type_attribute (type_die, type, TREE_READONLY (decl),
15430 TREE_THIS_VOLATILE (decl), context_die);
15431 }
15432
15433 if (DECL_ABSTRACT (decl))
15434 equate_decl_number_to_die (decl, type_die);
15435
15436 if (get_AT (type_die, DW_AT_name))
15437 add_pubtype (decl, type_die);
15438 }
15439
15440 /* Generate a type description DIE. */
15441
15442 static void
15443 gen_type_die_with_usage (tree type, dw_die_ref context_die,
15444 enum debug_info_usage usage)
15445 {
15446 int need_pop;
15447 struct array_descr_info info;
15448
15449 if (type == NULL_TREE || type == error_mark_node)
15450 return;
15451
15452 /* If TYPE is a typedef type variant, let's generate debug info
15453 for the parent typedef which TYPE is a type of. */
15454 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15455 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
15456 {
15457 if (TREE_ASM_WRITTEN (type))
15458 return;
15459
15460 /* Prevent broken recursion; we can't hand off to the same type. */
15461 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
15462
15463 /* Use the DIE of the containing namespace as the parent DIE of
15464 the type description DIE we want to generate. */
15465 if (DECL_CONTEXT (TYPE_NAME (type))
15466 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
15467 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
15468
15469 TREE_ASM_WRITTEN (type) = 1;
15470 gen_decl_die (TYPE_NAME (type), NULL, context_die);
15471 return;
15472 }
15473
15474 /* If this is an array type with hidden descriptor, handle it first. */
15475 if (!TREE_ASM_WRITTEN (type)
15476 && lang_hooks.types.get_array_descr_info
15477 && lang_hooks.types.get_array_descr_info (type, &info))
15478 {
15479 gen_descr_array_type_die (type, &info, context_die);
15480 TREE_ASM_WRITTEN (type) = 1;
15481 return;
15482 }
15483
15484 /* We are going to output a DIE to represent the unqualified version
15485 of this type (i.e. without any const or volatile qualifiers) so
15486 get the main variant (i.e. the unqualified version) of this type
15487 now. (Vectors are special because the debugging info is in the
15488 cloned type itself). */
15489 if (TREE_CODE (type) != VECTOR_TYPE)
15490 type = type_main_variant (type);
15491
15492 if (TREE_ASM_WRITTEN (type))
15493 return;
15494
15495 switch (TREE_CODE (type))
15496 {
15497 case ERROR_MARK:
15498 break;
15499
15500 case POINTER_TYPE:
15501 case REFERENCE_TYPE:
15502 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
15503 ensures that the gen_type_die recursion will terminate even if the
15504 type is recursive. Recursive types are possible in Ada. */
15505 /* ??? We could perhaps do this for all types before the switch
15506 statement. */
15507 TREE_ASM_WRITTEN (type) = 1;
15508
15509 /* For these types, all that is required is that we output a DIE (or a
15510 set of DIEs) to represent the "basis" type. */
15511 gen_type_die_with_usage (TREE_TYPE (type), context_die,
15512 DINFO_USAGE_IND_USE);
15513 break;
15514
15515 case OFFSET_TYPE:
15516 /* This code is used for C++ pointer-to-data-member types.
15517 Output a description of the relevant class type. */
15518 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15519 DINFO_USAGE_IND_USE);
15520
15521 /* Output a description of the type of the object pointed to. */
15522 gen_type_die_with_usage (TREE_TYPE (type), context_die,
15523 DINFO_USAGE_IND_USE);
15524
15525 /* Now output a DIE to represent this pointer-to-data-member type
15526 itself. */
15527 gen_ptr_to_mbr_type_die (type, context_die);
15528 break;
15529
15530 case FUNCTION_TYPE:
15531 /* Force out return type (in case it wasn't forced out already). */
15532 gen_type_die_with_usage (TREE_TYPE (type), context_die,
15533 DINFO_USAGE_DIR_USE);
15534 gen_subroutine_type_die (type, context_die);
15535 break;
15536
15537 case METHOD_TYPE:
15538 /* Force out return type (in case it wasn't forced out already). */
15539 gen_type_die_with_usage (TREE_TYPE (type), context_die,
15540 DINFO_USAGE_DIR_USE);
15541 gen_subroutine_type_die (type, context_die);
15542 break;
15543
15544 case ARRAY_TYPE:
15545 gen_array_type_die (type, context_die);
15546 break;
15547
15548 case VECTOR_TYPE:
15549 gen_array_type_die (type, context_die);
15550 break;
15551
15552 case ENUMERAL_TYPE:
15553 case RECORD_TYPE:
15554 case UNION_TYPE:
15555 case QUAL_UNION_TYPE:
15556 /* If this is a nested type whose containing class hasn't been written
15557 out yet, writing it out will cover this one, too. This does not apply
15558 to instantiations of member class templates; they need to be added to
15559 the containing class as they are generated. FIXME: This hurts the
15560 idea of combining type decls from multiple TUs, since we can't predict
15561 what set of template instantiations we'll get. */
15562 if (TYPE_CONTEXT (type)
15563 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15564 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15565 {
15566 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15567
15568 if (TREE_ASM_WRITTEN (type))
15569 return;
15570
15571 /* If that failed, attach ourselves to the stub. */
15572 push_decl_scope (TYPE_CONTEXT (type));
15573 context_die = lookup_type_die (TYPE_CONTEXT (type));
15574 need_pop = 1;
15575 }
15576 else
15577 {
15578 context_die = declare_in_namespace (type, context_die);
15579 need_pop = 0;
15580 }
15581
15582 if (TREE_CODE (type) == ENUMERAL_TYPE)
15583 {
15584 /* This might have been written out by the call to
15585 declare_in_namespace. */
15586 if (!TREE_ASM_WRITTEN (type))
15587 gen_enumeration_type_die (type, context_die);
15588 }
15589 else
15590 gen_struct_or_union_type_die (type, context_die, usage);
15591
15592 if (need_pop)
15593 pop_decl_scope ();
15594
15595 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15596 it up if it is ever completed. gen_*_type_die will set it for us
15597 when appropriate. */
15598 return;
15599
15600 case VOID_TYPE:
15601 case INTEGER_TYPE:
15602 case REAL_TYPE:
15603 case FIXED_POINT_TYPE:
15604 case COMPLEX_TYPE:
15605 case BOOLEAN_TYPE:
15606 /* No DIEs needed for fundamental types. */
15607 break;
15608
15609 case LANG_TYPE:
15610 /* No Dwarf representation currently defined. */
15611 break;
15612
15613 default:
15614 gcc_unreachable ();
15615 }
15616
15617 TREE_ASM_WRITTEN (type) = 1;
15618 }
15619
15620 static void
15621 gen_type_die (tree type, dw_die_ref context_die)
15622 {
15623 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15624 }
15625
15626 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15627 things which are local to the given block. */
15628
15629 static void
15630 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15631 {
15632 int must_output_die = 0;
15633 bool inlined_func;
15634
15635 /* Ignore blocks that are NULL. */
15636 if (stmt == NULL_TREE)
15637 return;
15638
15639 inlined_func = inlined_function_outer_scope_p (stmt);
15640
15641 /* If the block is one fragment of a non-contiguous block, do not
15642 process the variables, since they will have been done by the
15643 origin block. Do process subblocks. */
15644 if (BLOCK_FRAGMENT_ORIGIN (stmt))
15645 {
15646 tree sub;
15647
15648 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15649 gen_block_die (sub, context_die, depth + 1);
15650
15651 return;
15652 }
15653
15654 /* Determine if we need to output any Dwarf DIEs at all to represent this
15655 block. */
15656 if (inlined_func)
15657 /* The outer scopes for inlinings *must* always be represented. We
15658 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
15659 must_output_die = 1;
15660 else
15661 {
15662 /* Determine if this block directly contains any "significant"
15663 local declarations which we will need to output DIEs for. */
15664 if (debug_info_level > DINFO_LEVEL_TERSE)
15665 /* We are not in terse mode so *any* local declaration counts
15666 as being a "significant" one. */
15667 must_output_die = ((BLOCK_VARS (stmt) != NULL
15668 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15669 && (TREE_USED (stmt)
15670 || TREE_ASM_WRITTEN (stmt)
15671 || BLOCK_ABSTRACT (stmt)));
15672 else if ((TREE_USED (stmt)
15673 || TREE_ASM_WRITTEN (stmt)
15674 || BLOCK_ABSTRACT (stmt))
15675 && !dwarf2out_ignore_block (stmt))
15676 must_output_die = 1;
15677 }
15678
15679 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15680 DIE for any block which contains no significant local declarations at
15681 all. Rather, in such cases we just call `decls_for_scope' so that any
15682 needed Dwarf info for any sub-blocks will get properly generated. Note
15683 that in terse mode, our definition of what constitutes a "significant"
15684 local declaration gets restricted to include only inlined function
15685 instances and local (nested) function definitions. */
15686 if (must_output_die)
15687 {
15688 if (inlined_func)
15689 {
15690 /* If STMT block is abstract, that means we have been called
15691 indirectly from dwarf2out_abstract_function.
15692 That function rightfully marks the descendent blocks (of
15693 the abstract function it is dealing with) as being abstract,
15694 precisely to prevent us from emitting any
15695 DW_TAG_inlined_subroutine DIE as a descendent
15696 of an abstract function instance. So in that case, we should
15697 not call gen_inlined_subroutine_die.
15698
15699 Later though, when cgraph asks dwarf2out to emit info
15700 for the concrete instance of the function decl into which
15701 the concrete instance of STMT got inlined, the later will lead
15702 to the generation of a DW_TAG_inlined_subroutine DIE. */
15703 if (! BLOCK_ABSTRACT (stmt))
15704 gen_inlined_subroutine_die (stmt, context_die, depth);
15705 }
15706 else
15707 gen_lexical_block_die (stmt, context_die, depth);
15708 }
15709 else
15710 decls_for_scope (stmt, context_die, depth);
15711 }
15712
15713 /* Process variable DECL (or variable with origin ORIGIN) within
15714 block STMT and add it to CONTEXT_DIE. */
15715 static void
15716 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15717 {
15718 dw_die_ref die;
15719 tree decl_or_origin = decl ? decl : origin;
15720 tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15721
15722 if (ultimate_origin)
15723 origin = ultimate_origin;
15724
15725 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15726 die = lookup_decl_die (decl_or_origin);
15727 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15728 && TYPE_DECL_IS_STUB (decl_or_origin))
15729 die = lookup_type_die (TREE_TYPE (decl_or_origin));
15730 else
15731 die = NULL;
15732
15733 if (die != NULL && die->die_parent == NULL)
15734 add_child_die (context_die, die);
15735 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15736 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15737 stmt, context_die);
15738 else
15739 gen_decl_die (decl, origin, context_die);
15740 }
15741
15742 /* Generate all of the decls declared within a given scope and (recursively)
15743 all of its sub-blocks. */
15744
15745 static void
15746 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15747 {
15748 tree decl;
15749 unsigned int i;
15750 tree subblocks;
15751
15752 /* Ignore NULL blocks. */
15753 if (stmt == NULL_TREE)
15754 return;
15755
15756 /* Output the DIEs to represent all of the data objects and typedefs
15757 declared directly within this block but not within any nested
15758 sub-blocks. Also, nested function and tag DIEs have been
15759 generated with a parent of NULL; fix that up now. */
15760 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15761 process_scope_var (stmt, decl, NULL_TREE, context_die);
15762 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15763 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15764 context_die);
15765
15766 /* If we're at -g1, we're not interested in subblocks. */
15767 if (debug_info_level <= DINFO_LEVEL_TERSE)
15768 return;
15769
15770 /* Output the DIEs to represent all sub-blocks (and the items declared
15771 therein) of this block. */
15772 for (subblocks = BLOCK_SUBBLOCKS (stmt);
15773 subblocks != NULL;
15774 subblocks = BLOCK_CHAIN (subblocks))
15775 gen_block_die (subblocks, context_die, depth + 1);
15776 }
15777
15778 /* Is this a typedef we can avoid emitting? */
15779
15780 static inline int
15781 is_redundant_typedef (const_tree decl)
15782 {
15783 if (TYPE_DECL_IS_STUB (decl))
15784 return 1;
15785
15786 if (DECL_ARTIFICIAL (decl)
15787 && DECL_CONTEXT (decl)
15788 && is_tagged_type (DECL_CONTEXT (decl))
15789 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15790 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15791 /* Also ignore the artificial member typedef for the class name. */
15792 return 1;
15793
15794 return 0;
15795 }
15796
15797 /* Returns the DIE for a context. */
15798
15799 static inline dw_die_ref
15800 get_context_die (tree context)
15801 {
15802 if (context)
15803 {
15804 /* Find die that represents this context. */
15805 if (TYPE_P (context))
15806 return force_type_die (context);
15807 else
15808 return force_decl_die (context);
15809 }
15810 return comp_unit_die;
15811 }
15812
15813 /* Returns the DIE for decl. A DIE will always be returned. */
15814
15815 static dw_die_ref
15816 force_decl_die (tree decl)
15817 {
15818 dw_die_ref decl_die;
15819 unsigned saved_external_flag;
15820 tree save_fn = NULL_TREE;
15821 decl_die = lookup_decl_die (decl);
15822 if (!decl_die)
15823 {
15824 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15825
15826 decl_die = lookup_decl_die (decl);
15827 if (decl_die)
15828 return decl_die;
15829
15830 switch (TREE_CODE (decl))
15831 {
15832 case FUNCTION_DECL:
15833 /* Clear current_function_decl, so that gen_subprogram_die thinks
15834 that this is a declaration. At this point, we just want to force
15835 declaration die. */
15836 save_fn = current_function_decl;
15837 current_function_decl = NULL_TREE;
15838 gen_subprogram_die (decl, context_die);
15839 current_function_decl = save_fn;
15840 break;
15841
15842 case VAR_DECL:
15843 /* Set external flag to force declaration die. Restore it after
15844 gen_decl_die() call. */
15845 saved_external_flag = DECL_EXTERNAL (decl);
15846 DECL_EXTERNAL (decl) = 1;
15847 gen_decl_die (decl, NULL, context_die);
15848 DECL_EXTERNAL (decl) = saved_external_flag;
15849 break;
15850
15851 case NAMESPACE_DECL:
15852 dwarf2out_decl (decl);
15853 break;
15854
15855 default:
15856 gcc_unreachable ();
15857 }
15858
15859 /* We should be able to find the DIE now. */
15860 if (!decl_die)
15861 decl_die = lookup_decl_die (decl);
15862 gcc_assert (decl_die);
15863 }
15864
15865 return decl_die;
15866 }
15867
15868 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
15869 always returned. */
15870
15871 static dw_die_ref
15872 force_type_die (tree type)
15873 {
15874 dw_die_ref type_die;
15875
15876 type_die = lookup_type_die (type);
15877 if (!type_die)
15878 {
15879 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15880
15881 type_die = modified_type_die (type, TYPE_READONLY (type),
15882 TYPE_VOLATILE (type), context_die);
15883 gcc_assert (type_die);
15884 }
15885 return type_die;
15886 }
15887
15888 /* Force out any required namespaces to be able to output DECL,
15889 and return the new context_die for it, if it's changed. */
15890
15891 static dw_die_ref
15892 setup_namespace_context (tree thing, dw_die_ref context_die)
15893 {
15894 tree context = (DECL_P (thing)
15895 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15896 if (context && TREE_CODE (context) == NAMESPACE_DECL)
15897 /* Force out the namespace. */
15898 context_die = force_decl_die (context);
15899
15900 return context_die;
15901 }
15902
15903 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15904 type) within its namespace, if appropriate.
15905
15906 For compatibility with older debuggers, namespace DIEs only contain
15907 declarations; all definitions are emitted at CU scope. */
15908
15909 static dw_die_ref
15910 declare_in_namespace (tree thing, dw_die_ref context_die)
15911 {
15912 dw_die_ref ns_context;
15913
15914 if (debug_info_level <= DINFO_LEVEL_TERSE)
15915 return context_die;
15916
15917 /* If this decl is from an inlined function, then don't try to emit it in its
15918 namespace, as we will get confused. It would have already been emitted
15919 when the abstract instance of the inline function was emitted anyways. */
15920 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15921 return context_die;
15922
15923 ns_context = setup_namespace_context (thing, context_die);
15924
15925 if (ns_context != context_die)
15926 {
15927 if (is_fortran ())
15928 return ns_context;
15929 if (DECL_P (thing))
15930 gen_decl_die (thing, NULL, ns_context);
15931 else
15932 gen_type_die (thing, ns_context);
15933 }
15934 return context_die;
15935 }
15936
15937 /* Generate a DIE for a namespace or namespace alias. */
15938
15939 static void
15940 gen_namespace_die (tree decl, dw_die_ref context_die)
15941 {
15942 dw_die_ref namespace_die;
15943
15944 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15945 they are an alias of. */
15946 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15947 {
15948 /* Output a real namespace or module. */
15949 context_die = setup_namespace_context (decl, comp_unit_die);
15950 namespace_die = new_die (is_fortran ()
15951 ? DW_TAG_module : DW_TAG_namespace,
15952 context_die, decl);
15953 /* For Fortran modules defined in different CU don't add src coords. */
15954 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15955 add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15956 else
15957 add_name_and_src_coords_attributes (namespace_die, decl);
15958 if (DECL_EXTERNAL (decl))
15959 add_AT_flag (namespace_die, DW_AT_declaration, 1);
15960 equate_decl_number_to_die (decl, namespace_die);
15961 }
15962 else
15963 {
15964 /* Output a namespace alias. */
15965
15966 /* Force out the namespace we are an alias of, if necessary. */
15967 dw_die_ref origin_die
15968 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15969
15970 if (DECL_CONTEXT (decl) == NULL_TREE
15971 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15972 context_die = setup_namespace_context (decl, comp_unit_die);
15973 /* Now create the namespace alias DIE. */
15974 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15975 add_name_and_src_coords_attributes (namespace_die, decl);
15976 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15977 equate_decl_number_to_die (decl, namespace_die);
15978 }
15979 }
15980
15981 /* Generate Dwarf debug information for a decl described by DECL. */
15982
15983 static void
15984 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15985 {
15986 tree decl_or_origin = decl ? decl : origin;
15987 tree class_origin = NULL;
15988
15989 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15990 return;
15991
15992 switch (TREE_CODE (decl_or_origin))
15993 {
15994 case ERROR_MARK:
15995 break;
15996
15997 case CONST_DECL:
15998 if (!is_fortran ())
15999 {
16000 /* The individual enumerators of an enum type get output when we output
16001 the Dwarf representation of the relevant enum type itself. */
16002 break;
16003 }
16004
16005 /* Emit its type. */
16006 gen_type_die (TREE_TYPE (decl), context_die);
16007
16008 /* And its containing namespace. */
16009 context_die = declare_in_namespace (decl, context_die);
16010
16011 gen_const_die (decl, context_die);
16012 break;
16013
16014 case FUNCTION_DECL:
16015 /* Don't output any DIEs to represent mere function declarations,
16016 unless they are class members or explicit block externs. */
16017 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
16018 && DECL_CONTEXT (decl_or_origin) == NULL_TREE
16019 && (current_function_decl == NULL_TREE
16020 || DECL_ARTIFICIAL (decl_or_origin)))
16021 break;
16022
16023 #if 0
16024 /* FIXME */
16025 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
16026 on local redeclarations of global functions. That seems broken. */
16027 if (current_function_decl != decl)
16028 /* This is only a declaration. */;
16029 #endif
16030
16031 /* If we're emitting a clone, emit info for the abstract instance. */
16032 if (origin || DECL_ORIGIN (decl) != decl)
16033 dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
16034
16035 /* If we're emitting an out-of-line copy of an inline function,
16036 emit info for the abstract instance and set up to refer to it. */
16037 else if (cgraph_function_possibly_inlined_p (decl)
16038 && ! DECL_ABSTRACT (decl)
16039 && ! class_or_namespace_scope_p (context_die)
16040 /* dwarf2out_abstract_function won't emit a die if this is just
16041 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
16042 that case, because that works only if we have a die. */
16043 && DECL_INITIAL (decl) != NULL_TREE)
16044 {
16045 dwarf2out_abstract_function (decl);
16046 set_decl_origin_self (decl);
16047 }
16048
16049 /* Otherwise we're emitting the primary DIE for this decl. */
16050 else if (debug_info_level > DINFO_LEVEL_TERSE)
16051 {
16052 /* Before we describe the FUNCTION_DECL itself, make sure that we
16053 have described its return type. */
16054 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
16055
16056 /* And its virtual context. */
16057 if (DECL_VINDEX (decl) != NULL_TREE)
16058 gen_type_die (DECL_CONTEXT (decl), context_die);
16059
16060 /* And its containing type. */
16061 if (!origin)
16062 origin = decl_class_context (decl);
16063 if (origin != NULL_TREE)
16064 gen_type_die_for_member (origin, decl, context_die);
16065
16066 /* And its containing namespace. */
16067 context_die = declare_in_namespace (decl, context_die);
16068 }
16069
16070 /* Now output a DIE to represent the function itself. */
16071 if (decl)
16072 gen_subprogram_die (decl, context_die);
16073 break;
16074
16075 case TYPE_DECL:
16076 /* If we are in terse mode, don't generate any DIEs to represent any
16077 actual typedefs. */
16078 if (debug_info_level <= DINFO_LEVEL_TERSE)
16079 break;
16080
16081 /* In the special case of a TYPE_DECL node representing the declaration
16082 of some type tag, if the given TYPE_DECL is marked as having been
16083 instantiated from some other (original) TYPE_DECL node (e.g. one which
16084 was generated within the original definition of an inline function) we
16085 used to generate a special (abbreviated) DW_TAG_structure_type,
16086 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
16087 should be actually referencing those DIEs, as variable DIEs with that
16088 type would be emitted already in the abstract origin, so it was always
16089 removed during unused type prunning. Don't add anything in this
16090 case. */
16091 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
16092 break;
16093
16094 if (is_redundant_typedef (decl))
16095 gen_type_die (TREE_TYPE (decl), context_die);
16096 else
16097 /* Output a DIE to represent the typedef itself. */
16098 gen_typedef_die (decl, context_die);
16099 break;
16100
16101 case LABEL_DECL:
16102 if (debug_info_level >= DINFO_LEVEL_NORMAL)
16103 gen_label_die (decl, context_die);
16104 break;
16105
16106 case VAR_DECL:
16107 case RESULT_DECL:
16108 /* If we are in terse mode, don't generate any DIEs to represent any
16109 variable declarations or definitions. */
16110 if (debug_info_level <= DINFO_LEVEL_TERSE)
16111 break;
16112
16113 /* Output any DIEs that are needed to specify the type of this data
16114 object. */
16115 if ((TREE_CODE (decl_or_origin) == RESULT_DECL
16116 || TREE_CODE (decl_or_origin) == VAR_DECL)
16117 && DECL_BY_REFERENCE (decl_or_origin))
16118 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
16119 else
16120 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
16121
16122 /* And its containing type. */
16123 class_origin = decl_class_context (decl_or_origin);
16124 if (class_origin != NULL_TREE)
16125 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
16126
16127 /* And its containing namespace. */
16128 context_die = declare_in_namespace (decl_or_origin, context_die);
16129
16130 /* Now output the DIE to represent the data object itself. This gets
16131 complicated because of the possibility that the VAR_DECL really
16132 represents an inlined instance of a formal parameter for an inline
16133 function. */
16134 if (!origin)
16135 origin = decl_ultimate_origin (decl);
16136 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
16137 gen_formal_parameter_die (decl, origin, context_die);
16138 else
16139 gen_variable_die (decl, origin, context_die);
16140 break;
16141
16142 case FIELD_DECL:
16143 /* Ignore the nameless fields that are used to skip bits but handle C++
16144 anonymous unions and structs. */
16145 if (DECL_NAME (decl) != NULL_TREE
16146 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
16147 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
16148 {
16149 gen_type_die (member_declared_type (decl), context_die);
16150 gen_field_die (decl, context_die);
16151 }
16152 break;
16153
16154 case PARM_DECL:
16155 if (DECL_BY_REFERENCE (decl_or_origin))
16156 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
16157 else
16158 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
16159 gen_formal_parameter_die (decl, origin, context_die);
16160 break;
16161
16162 case NAMESPACE_DECL:
16163 case IMPORTED_DECL:
16164 gen_namespace_die (decl, context_die);
16165 break;
16166
16167 default:
16168 /* Probably some frontend-internal decl. Assume we don't care. */
16169 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
16170 break;
16171 }
16172 }
16173 \f
16174 /* Output debug information for global decl DECL. Called from toplev.c after
16175 compilation proper has finished. */
16176
16177 static void
16178 dwarf2out_global_decl (tree decl)
16179 {
16180 /* Output DWARF2 information for file-scope tentative data object
16181 declarations, file-scope (extern) function declarations (which
16182 had no corresponding body) and file-scope tagged type declarations
16183 and definitions which have not yet been forced out. */
16184 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
16185 dwarf2out_decl (decl);
16186 }
16187
16188 /* Output debug information for type decl DECL. Called from toplev.c
16189 and from language front ends (to record built-in types). */
16190 static void
16191 dwarf2out_type_decl (tree decl, int local)
16192 {
16193 if (!local)
16194 dwarf2out_decl (decl);
16195 }
16196
16197 /* Output debug information for imported module or decl DECL.
16198 NAME is non-NULL name in the lexical block if the decl has been renamed.
16199 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
16200 that DECL belongs to.
16201 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
16202 static void
16203 dwarf2out_imported_module_or_decl_1 (tree decl,
16204 tree name,
16205 tree lexical_block,
16206 dw_die_ref lexical_block_die)
16207 {
16208 expanded_location xloc;
16209 dw_die_ref imported_die = NULL;
16210 dw_die_ref at_import_die;
16211
16212 if (TREE_CODE (decl) == IMPORTED_DECL)
16213 {
16214 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
16215 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
16216 gcc_assert (decl);
16217 }
16218 else
16219 xloc = expand_location (input_location);
16220
16221 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
16222 {
16223 if (is_base_type (TREE_TYPE (decl)))
16224 at_import_die = base_type_die (TREE_TYPE (decl));
16225 else
16226 at_import_die = force_type_die (TREE_TYPE (decl));
16227 /* For namespace N { typedef void T; } using N::T; base_type_die
16228 returns NULL, but DW_TAG_imported_declaration requires
16229 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
16230 if (!at_import_die)
16231 {
16232 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
16233 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
16234 at_import_die = lookup_type_die (TREE_TYPE (decl));
16235 gcc_assert (at_import_die);
16236 }
16237 }
16238 else
16239 {
16240 at_import_die = lookup_decl_die (decl);
16241 if (!at_import_die)
16242 {
16243 /* If we're trying to avoid duplicate debug info, we may not have
16244 emitted the member decl for this field. Emit it now. */
16245 if (TREE_CODE (decl) == FIELD_DECL)
16246 {
16247 tree type = DECL_CONTEXT (decl);
16248
16249 if (TYPE_CONTEXT (type)
16250 && TYPE_P (TYPE_CONTEXT (type))
16251 && !should_emit_struct_debug (TYPE_CONTEXT (type),
16252 DINFO_USAGE_DIR_USE))
16253 return;
16254 gen_type_die_for_member (type, decl,
16255 get_context_die (TYPE_CONTEXT (type)));
16256 }
16257 at_import_die = force_decl_die (decl);
16258 }
16259 }
16260
16261 if (TREE_CODE (decl) == NAMESPACE_DECL)
16262 imported_die = new_die (DW_TAG_imported_module,
16263 lexical_block_die,
16264 lexical_block);
16265 else
16266 imported_die = new_die (DW_TAG_imported_declaration,
16267 lexical_block_die,
16268 lexical_block);
16269
16270 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
16271 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
16272 if (name)
16273 add_AT_string (imported_die, DW_AT_name,
16274 IDENTIFIER_POINTER (name));
16275 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
16276 }
16277
16278 /* Output debug information for imported module or decl DECL.
16279 NAME is non-NULL name in context if the decl has been renamed.
16280 CHILD is true if decl is one of the renamed decls as part of
16281 importing whole module. */
16282
16283 static void
16284 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
16285 bool child)
16286 {
16287 /* dw_die_ref at_import_die; */
16288 dw_die_ref scope_die;
16289
16290 if (debug_info_level <= DINFO_LEVEL_TERSE)
16291 return;
16292
16293 gcc_assert (decl);
16294
16295 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
16296 We need decl DIE for reference and scope die. First, get DIE for the decl
16297 itself. */
16298
16299 /* Get the scope die for decl context. Use comp_unit_die for global module
16300 or decl. If die is not found for non globals, force new die. */
16301 if (context
16302 && TYPE_P (context)
16303 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
16304 return;
16305 scope_die = get_context_die (context);
16306
16307 if (child)
16308 {
16309 gcc_assert (scope_die->die_child);
16310 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
16311 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
16312 scope_die = scope_die->die_child;
16313 }
16314
16315 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
16316 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
16317
16318 }
16319
16320 /* Write the debugging output for DECL. */
16321
16322 void
16323 dwarf2out_decl (tree decl)
16324 {
16325 dw_die_ref context_die = comp_unit_die;
16326
16327 switch (TREE_CODE (decl))
16328 {
16329 case ERROR_MARK:
16330 return;
16331
16332 case FUNCTION_DECL:
16333 /* What we would really like to do here is to filter out all mere
16334 file-scope declarations of file-scope functions which are never
16335 referenced later within this translation unit (and keep all of ones
16336 that *are* referenced later on) but we aren't clairvoyant, so we have
16337 no idea which functions will be referenced in the future (i.e. later
16338 on within the current translation unit). So here we just ignore all
16339 file-scope function declarations which are not also definitions. If
16340 and when the debugger needs to know something about these functions,
16341 it will have to hunt around and find the DWARF information associated
16342 with the definition of the function.
16343
16344 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
16345 nodes represent definitions and which ones represent mere
16346 declarations. We have to check DECL_INITIAL instead. That's because
16347 the C front-end supports some weird semantics for "extern inline"
16348 function definitions. These can get inlined within the current
16349 translation unit (and thus, we need to generate Dwarf info for their
16350 abstract instances so that the Dwarf info for the concrete inlined
16351 instances can have something to refer to) but the compiler never
16352 generates any out-of-lines instances of such things (despite the fact
16353 that they *are* definitions).
16354
16355 The important point is that the C front-end marks these "extern
16356 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
16357 them anyway. Note that the C++ front-end also plays some similar games
16358 for inline function definitions appearing within include files which
16359 also contain `#pragma interface' pragmas. */
16360 if (DECL_INITIAL (decl) == NULL_TREE)
16361 return;
16362
16363 /* If we're a nested function, initially use a parent of NULL; if we're
16364 a plain function, this will be fixed up in decls_for_scope. If
16365 we're a method, it will be ignored, since we already have a DIE. */
16366 if (decl_function_context (decl)
16367 /* But if we're in terse mode, we don't care about scope. */
16368 && debug_info_level > DINFO_LEVEL_TERSE)
16369 context_die = NULL;
16370 break;
16371
16372 case VAR_DECL:
16373 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
16374 declaration and if the declaration was never even referenced from
16375 within this entire compilation unit. We suppress these DIEs in
16376 order to save space in the .debug section (by eliminating entries
16377 which are probably useless). Note that we must not suppress
16378 block-local extern declarations (whether used or not) because that
16379 would screw-up the debugger's name lookup mechanism and cause it to
16380 miss things which really ought to be in scope at a given point. */
16381 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
16382 return;
16383
16384 /* For local statics lookup proper context die. */
16385 if (TREE_STATIC (decl) && decl_function_context (decl))
16386 context_die = lookup_decl_die (DECL_CONTEXT (decl));
16387
16388 /* If we are in terse mode, don't generate any DIEs to represent any
16389 variable declarations or definitions. */
16390 if (debug_info_level <= DINFO_LEVEL_TERSE)
16391 return;
16392 break;
16393
16394 case CONST_DECL:
16395 if (debug_info_level <= DINFO_LEVEL_TERSE)
16396 return;
16397 if (!is_fortran ())
16398 return;
16399 if (TREE_STATIC (decl) && decl_function_context (decl))
16400 context_die = lookup_decl_die (DECL_CONTEXT (decl));
16401 break;
16402
16403 case NAMESPACE_DECL:
16404 case IMPORTED_DECL:
16405 if (debug_info_level <= DINFO_LEVEL_TERSE)
16406 return;
16407 if (lookup_decl_die (decl) != NULL)
16408 return;
16409 break;
16410
16411 case TYPE_DECL:
16412 /* Don't emit stubs for types unless they are needed by other DIEs. */
16413 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
16414 return;
16415
16416 /* Don't bother trying to generate any DIEs to represent any of the
16417 normal built-in types for the language we are compiling. */
16418 if (DECL_IS_BUILTIN (decl))
16419 {
16420 /* OK, we need to generate one for `bool' so GDB knows what type
16421 comparisons have. */
16422 if (is_cxx ()
16423 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
16424 && ! DECL_IGNORED_P (decl))
16425 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
16426
16427 return;
16428 }
16429
16430 /* If we are in terse mode, don't generate any DIEs for types. */
16431 if (debug_info_level <= DINFO_LEVEL_TERSE)
16432 return;
16433
16434 /* If we're a function-scope tag, initially use a parent of NULL;
16435 this will be fixed up in decls_for_scope. */
16436 if (decl_function_context (decl))
16437 context_die = NULL;
16438
16439 break;
16440
16441 default:
16442 return;
16443 }
16444
16445 gen_decl_die (decl, NULL, context_die);
16446 }
16447
16448 /* Output a marker (i.e. a label) for the beginning of the generated code for
16449 a lexical block. */
16450
16451 static void
16452 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
16453 unsigned int blocknum)
16454 {
16455 switch_to_section (current_function_section ());
16456 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
16457 }
16458
16459 /* Output a marker (i.e. a label) for the end of the generated code for a
16460 lexical block. */
16461
16462 static void
16463 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
16464 {
16465 switch_to_section (current_function_section ());
16466 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
16467 }
16468
16469 /* Returns nonzero if it is appropriate not to emit any debugging
16470 information for BLOCK, because it doesn't contain any instructions.
16471
16472 Don't allow this for blocks with nested functions or local classes
16473 as we would end up with orphans, and in the presence of scheduling
16474 we may end up calling them anyway. */
16475
16476 static bool
16477 dwarf2out_ignore_block (const_tree block)
16478 {
16479 tree decl;
16480 unsigned int i;
16481
16482 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
16483 if (TREE_CODE (decl) == FUNCTION_DECL
16484 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16485 return 0;
16486 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
16487 {
16488 decl = BLOCK_NONLOCALIZED_VAR (block, i);
16489 if (TREE_CODE (decl) == FUNCTION_DECL
16490 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16491 return 0;
16492 }
16493
16494 return 1;
16495 }
16496
16497 /* Hash table routines for file_hash. */
16498
16499 static int
16500 file_table_eq (const void *p1_p, const void *p2_p)
16501 {
16502 const struct dwarf_file_data *const p1 =
16503 (const struct dwarf_file_data *) p1_p;
16504 const char *const p2 = (const char *) p2_p;
16505 return strcmp (p1->filename, p2) == 0;
16506 }
16507
16508 static hashval_t
16509 file_table_hash (const void *p_p)
16510 {
16511 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
16512 return htab_hash_string (p->filename);
16513 }
16514
16515 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16516 dwarf2out.c) and return its "index". The index of each (known) filename is
16517 just a unique number which is associated with only that one filename. We
16518 need such numbers for the sake of generating labels (in the .debug_sfnames
16519 section) and references to those files numbers (in the .debug_srcinfo
16520 and.debug_macinfo sections). If the filename given as an argument is not
16521 found in our current list, add it to the list and assign it the next
16522 available unique index number. In order to speed up searches, we remember
16523 the index of the filename was looked up last. This handles the majority of
16524 all searches. */
16525
16526 static struct dwarf_file_data *
16527 lookup_filename (const char *file_name)
16528 {
16529 void ** slot;
16530 struct dwarf_file_data * created;
16531
16532 /* Check to see if the file name that was searched on the previous
16533 call matches this file name. If so, return the index. */
16534 if (file_table_last_lookup
16535 && (file_name == file_table_last_lookup->filename
16536 || strcmp (file_table_last_lookup->filename, file_name) == 0))
16537 return file_table_last_lookup;
16538
16539 /* Didn't match the previous lookup, search the table. */
16540 slot = htab_find_slot_with_hash (file_table, file_name,
16541 htab_hash_string (file_name), INSERT);
16542 if (*slot)
16543 return (struct dwarf_file_data *) *slot;
16544
16545 created = GGC_NEW (struct dwarf_file_data);
16546 created->filename = file_name;
16547 created->emitted_number = 0;
16548 *slot = created;
16549 return created;
16550 }
16551
16552 /* If the assembler will construct the file table, then translate the compiler
16553 internal file table number into the assembler file table number, and emit
16554 a .file directive if we haven't already emitted one yet. The file table
16555 numbers are different because we prune debug info for unused variables and
16556 types, which may include filenames. */
16557
16558 static int
16559 maybe_emit_file (struct dwarf_file_data * fd)
16560 {
16561 if (! fd->emitted_number)
16562 {
16563 if (last_emitted_file)
16564 fd->emitted_number = last_emitted_file->emitted_number + 1;
16565 else
16566 fd->emitted_number = 1;
16567 last_emitted_file = fd;
16568
16569 if (DWARF2_ASM_LINE_DEBUG_INFO)
16570 {
16571 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16572 output_quoted_string (asm_out_file,
16573 remap_debug_filename (fd->filename));
16574 fputc ('\n', asm_out_file);
16575 }
16576 }
16577
16578 return fd->emitted_number;
16579 }
16580
16581 /* Replace DW_AT_name for the decl with name. */
16582
16583 static void
16584 dwarf2out_set_name (tree decl, tree name)
16585 {
16586 dw_die_ref die;
16587 dw_attr_ref attr;
16588
16589 die = TYPE_SYMTAB_DIE (decl);
16590 if (!die)
16591 return;
16592
16593 attr = get_AT (die, DW_AT_name);
16594 if (attr)
16595 {
16596 struct indirect_string_node *node;
16597
16598 node = find_AT_string (dwarf2_name (name, 0));
16599 /* replace the string. */
16600 attr->dw_attr_val.v.val_str = node;
16601 }
16602
16603 else
16604 add_name_attribute (die, dwarf2_name (name, 0));
16605 }
16606
16607 /* Called by the final INSN scan whenever we see a var location. We
16608 use it to drop labels in the right places, and throw the location in
16609 our lookup table. */
16610
16611 static void
16612 dwarf2out_var_location (rtx loc_note)
16613 {
16614 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16615 struct var_loc_node *newloc;
16616 rtx next_real;
16617 static const char *last_label;
16618 static bool last_in_cold_section_p;
16619 tree decl;
16620
16621 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16622 return;
16623
16624 next_real = next_real_insn (loc_note);
16625 /* If there are no instructions which would be affected by this note,
16626 don't do anything. */
16627 if (next_real == NULL_RTX)
16628 return;
16629
16630 newloc = GGC_CNEW (struct var_loc_node);
16631 /* If there were no real insns between note we processed last time
16632 and this note, use the label we emitted last time. */
16633 if (last_var_location_insn != NULL_RTX
16634 && last_var_location_insn == next_real
16635 && last_in_cold_section_p == in_cold_section_p)
16636 newloc->label = last_label;
16637 else
16638 {
16639 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16640 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16641 loclabel_num++;
16642 newloc->label = ggc_strdup (loclabel);
16643 }
16644 newloc->var_loc_note = loc_note;
16645 newloc->next = NULL;
16646
16647 if (cfun && in_cold_section_p)
16648 newloc->section_label = crtl->subsections.cold_section_label;
16649 else
16650 newloc->section_label = text_section_label;
16651
16652 last_var_location_insn = next_real;
16653 last_label = newloc->label;
16654 last_in_cold_section_p = in_cold_section_p;
16655 decl = NOTE_VAR_LOCATION_DECL (loc_note);
16656 add_var_loc_to_decl (decl, newloc);
16657 }
16658
16659 /* We need to reset the locations at the beginning of each
16660 function. We can't do this in the end_function hook, because the
16661 declarations that use the locations won't have been output when
16662 that hook is called. Also compute have_multiple_function_sections here. */
16663
16664 static void
16665 dwarf2out_begin_function (tree fun)
16666 {
16667 htab_empty (decl_loc_table);
16668
16669 if (function_section (fun) != text_section)
16670 have_multiple_function_sections = true;
16671
16672 dwarf2out_note_section_used ();
16673 }
16674
16675 /* Output a label to mark the beginning of a source code line entry
16676 and record information relating to this source line, in
16677 'line_info_table' for later output of the .debug_line section. */
16678
16679 static void
16680 dwarf2out_source_line (unsigned int line, const char *filename,
16681 int discriminator, bool is_stmt)
16682 {
16683 static bool last_is_stmt = true;
16684
16685 if (debug_info_level >= DINFO_LEVEL_NORMAL
16686 && line != 0)
16687 {
16688 int file_num = maybe_emit_file (lookup_filename (filename));
16689
16690 switch_to_section (current_function_section ());
16691
16692 /* If requested, emit something human-readable. */
16693 if (flag_debug_asm)
16694 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16695 filename, line);
16696
16697 if (DWARF2_ASM_LINE_DEBUG_INFO)
16698 {
16699 /* Emit the .loc directive understood by GNU as. */
16700 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
16701 if (is_stmt != last_is_stmt)
16702 {
16703 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
16704 last_is_stmt = is_stmt;
16705 }
16706 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
16707 fprintf (asm_out_file, " discriminator %d", discriminator);
16708 fputc ('\n', asm_out_file);
16709
16710 /* Indicate that line number info exists. */
16711 line_info_table_in_use++;
16712 }
16713 else if (function_section (current_function_decl) != text_section)
16714 {
16715 dw_separate_line_info_ref line_info;
16716 targetm.asm_out.internal_label (asm_out_file,
16717 SEPARATE_LINE_CODE_LABEL,
16718 separate_line_info_table_in_use);
16719
16720 /* Expand the line info table if necessary. */
16721 if (separate_line_info_table_in_use
16722 == separate_line_info_table_allocated)
16723 {
16724 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16725 separate_line_info_table
16726 = GGC_RESIZEVEC (dw_separate_line_info_entry,
16727 separate_line_info_table,
16728 separate_line_info_table_allocated);
16729 memset (separate_line_info_table
16730 + separate_line_info_table_in_use,
16731 0,
16732 (LINE_INFO_TABLE_INCREMENT
16733 * sizeof (dw_separate_line_info_entry)));
16734 }
16735
16736 /* Add the new entry at the end of the line_info_table. */
16737 line_info
16738 = &separate_line_info_table[separate_line_info_table_in_use++];
16739 line_info->dw_file_num = file_num;
16740 line_info->dw_line_num = line;
16741 line_info->function = current_function_funcdef_no;
16742 }
16743 else
16744 {
16745 dw_line_info_ref line_info;
16746
16747 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16748 line_info_table_in_use);
16749
16750 /* Expand the line info table if necessary. */
16751 if (line_info_table_in_use == line_info_table_allocated)
16752 {
16753 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16754 line_info_table
16755 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16756 line_info_table_allocated);
16757 memset (line_info_table + line_info_table_in_use, 0,
16758 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16759 }
16760
16761 /* Add the new entry at the end of the line_info_table. */
16762 line_info = &line_info_table[line_info_table_in_use++];
16763 line_info->dw_file_num = file_num;
16764 line_info->dw_line_num = line;
16765 }
16766 }
16767 }
16768
16769 /* Record the beginning of a new source file. */
16770
16771 static void
16772 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16773 {
16774 if (flag_eliminate_dwarf2_dups)
16775 {
16776 /* Record the beginning of the file for break_out_includes. */
16777 dw_die_ref bincl_die;
16778
16779 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16780 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16781 }
16782
16783 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16784 {
16785 int file_num = maybe_emit_file (lookup_filename (filename));
16786
16787 switch_to_section (debug_macinfo_section);
16788 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16789 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16790 lineno);
16791
16792 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16793 }
16794 }
16795
16796 /* Record the end of a source file. */
16797
16798 static void
16799 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16800 {
16801 if (flag_eliminate_dwarf2_dups)
16802 /* Record the end of the file for break_out_includes. */
16803 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16804
16805 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16806 {
16807 switch_to_section (debug_macinfo_section);
16808 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16809 }
16810 }
16811
16812 /* Called from debug_define in toplev.c. The `buffer' parameter contains
16813 the tail part of the directive line, i.e. the part which is past the
16814 initial whitespace, #, whitespace, directive-name, whitespace part. */
16815
16816 static void
16817 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16818 const char *buffer ATTRIBUTE_UNUSED)
16819 {
16820 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16821 {
16822 switch_to_section (debug_macinfo_section);
16823 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16824 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16825 dw2_asm_output_nstring (buffer, -1, "The macro");
16826 }
16827 }
16828
16829 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
16830 the tail part of the directive line, i.e. the part which is past the
16831 initial whitespace, #, whitespace, directive-name, whitespace part. */
16832
16833 static void
16834 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16835 const char *buffer ATTRIBUTE_UNUSED)
16836 {
16837 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16838 {
16839 switch_to_section (debug_macinfo_section);
16840 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16841 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16842 dw2_asm_output_nstring (buffer, -1, "The macro");
16843 }
16844 }
16845
16846 /* Set up for Dwarf output at the start of compilation. */
16847
16848 static void
16849 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16850 {
16851 /* Allocate the file_table. */
16852 file_table = htab_create_ggc (50, file_table_hash,
16853 file_table_eq, NULL);
16854
16855 /* Allocate the decl_die_table. */
16856 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16857 decl_die_table_eq, NULL);
16858
16859 /* Allocate the decl_loc_table. */
16860 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16861 decl_loc_table_eq, NULL);
16862
16863 /* Allocate the initial hunk of the decl_scope_table. */
16864 decl_scope_table = VEC_alloc (tree, gc, 256);
16865
16866 /* Allocate the initial hunk of the abbrev_die_table. */
16867 abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16868 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16869 /* Zero-th entry is allocated, but unused. */
16870 abbrev_die_table_in_use = 1;
16871
16872 /* Allocate the initial hunk of the line_info_table. */
16873 line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16874 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16875
16876 /* Zero-th entry is allocated, but unused. */
16877 line_info_table_in_use = 1;
16878
16879 /* Allocate the pubtypes and pubnames vectors. */
16880 pubname_table = VEC_alloc (pubname_entry, gc, 32);
16881 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16882
16883 /* Generate the initial DIE for the .debug section. Note that the (string)
16884 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16885 will (typically) be a relative pathname and that this pathname should be
16886 taken as being relative to the directory from which the compiler was
16887 invoked when the given (base) source file was compiled. We will fill
16888 in this value in dwarf2out_finish. */
16889 comp_unit_die = gen_compile_unit_die (NULL);
16890
16891 incomplete_types = VEC_alloc (tree, gc, 64);
16892
16893 used_rtx_array = VEC_alloc (rtx, gc, 32);
16894
16895 debug_info_section = get_section (DEBUG_INFO_SECTION,
16896 SECTION_DEBUG, NULL);
16897 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16898 SECTION_DEBUG, NULL);
16899 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16900 SECTION_DEBUG, NULL);
16901 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16902 SECTION_DEBUG, NULL);
16903 debug_line_section = get_section (DEBUG_LINE_SECTION,
16904 SECTION_DEBUG, NULL);
16905 debug_loc_section = get_section (DEBUG_LOC_SECTION,
16906 SECTION_DEBUG, NULL);
16907 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16908 SECTION_DEBUG, NULL);
16909 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16910 SECTION_DEBUG, NULL);
16911 debug_str_section = get_section (DEBUG_STR_SECTION,
16912 DEBUG_STR_SECTION_FLAGS, NULL);
16913 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16914 SECTION_DEBUG, NULL);
16915 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16916 SECTION_DEBUG, NULL);
16917
16918 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16919 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16920 DEBUG_ABBREV_SECTION_LABEL, 0);
16921 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16922 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16923 COLD_TEXT_SECTION_LABEL, 0);
16924 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16925
16926 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16927 DEBUG_INFO_SECTION_LABEL, 0);
16928 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16929 DEBUG_LINE_SECTION_LABEL, 0);
16930 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16931 DEBUG_RANGES_SECTION_LABEL, 0);
16932 switch_to_section (debug_abbrev_section);
16933 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16934 switch_to_section (debug_info_section);
16935 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16936 switch_to_section (debug_line_section);
16937 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16938
16939 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16940 {
16941 switch_to_section (debug_macinfo_section);
16942 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16943 DEBUG_MACINFO_SECTION_LABEL, 0);
16944 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16945 }
16946
16947 switch_to_section (text_section);
16948 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16949 if (flag_reorder_blocks_and_partition)
16950 {
16951 cold_text_section = unlikely_text_section ();
16952 switch_to_section (cold_text_section);
16953 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16954 }
16955 }
16956
16957 /* A helper function for dwarf2out_finish called through
16958 ht_forall. Emit one queued .debug_str string. */
16959
16960 static int
16961 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16962 {
16963 struct indirect_string_node *node = (struct indirect_string_node *) *h;
16964
16965 if (node->form == DW_FORM_strp)
16966 {
16967 switch_to_section (debug_str_section);
16968 ASM_OUTPUT_LABEL (asm_out_file, node->label);
16969 assemble_string (node->str, strlen (node->str) + 1);
16970 }
16971
16972 return 1;
16973 }
16974
16975 #if ENABLE_ASSERT_CHECKING
16976 /* Verify that all marks are clear. */
16977
16978 static void
16979 verify_marks_clear (dw_die_ref die)
16980 {
16981 dw_die_ref c;
16982
16983 gcc_assert (! die->die_mark);
16984 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16985 }
16986 #endif /* ENABLE_ASSERT_CHECKING */
16987
16988 /* Clear the marks for a die and its children.
16989 Be cool if the mark isn't set. */
16990
16991 static void
16992 prune_unmark_dies (dw_die_ref die)
16993 {
16994 dw_die_ref c;
16995
16996 if (die->die_mark)
16997 die->die_mark = 0;
16998 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16999 }
17000
17001 /* Given DIE that we're marking as used, find any other dies
17002 it references as attributes and mark them as used. */
17003
17004 static void
17005 prune_unused_types_walk_attribs (dw_die_ref die)
17006 {
17007 dw_attr_ref a;
17008 unsigned ix;
17009
17010 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
17011 {
17012 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
17013 {
17014 /* A reference to another DIE.
17015 Make sure that it will get emitted. */
17016 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
17017 }
17018 /* Set the string's refcount to 0 so that prune_unused_types_mark
17019 accounts properly for it. */
17020 if (AT_class (a) == dw_val_class_str)
17021 a->dw_attr_val.v.val_str->refcount = 0;
17022 }
17023 }
17024
17025
17026 /* Mark DIE as being used. If DOKIDS is true, then walk down
17027 to DIE's children. */
17028
17029 static void
17030 prune_unused_types_mark (dw_die_ref die, int dokids)
17031 {
17032 dw_die_ref c;
17033
17034 if (die->die_mark == 0)
17035 {
17036 /* We haven't done this node yet. Mark it as used. */
17037 die->die_mark = 1;
17038
17039 /* We also have to mark its parents as used.
17040 (But we don't want to mark our parents' kids due to this.) */
17041 if (die->die_parent)
17042 prune_unused_types_mark (die->die_parent, 0);
17043
17044 /* Mark any referenced nodes. */
17045 prune_unused_types_walk_attribs (die);
17046
17047 /* If this node is a specification,
17048 also mark the definition, if it exists. */
17049 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
17050 prune_unused_types_mark (die->die_definition, 1);
17051 }
17052
17053 if (dokids && die->die_mark != 2)
17054 {
17055 /* We need to walk the children, but haven't done so yet.
17056 Remember that we've walked the kids. */
17057 die->die_mark = 2;
17058
17059 /* If this is an array type, we need to make sure our
17060 kids get marked, even if they're types. */
17061 if (die->die_tag == DW_TAG_array_type)
17062 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
17063 else
17064 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
17065 }
17066 }
17067
17068 /* For local classes, look if any static member functions were emitted
17069 and if so, mark them. */
17070
17071 static void
17072 prune_unused_types_walk_local_classes (dw_die_ref die)
17073 {
17074 dw_die_ref c;
17075
17076 if (die->die_mark == 2)
17077 return;
17078
17079 switch (die->die_tag)
17080 {
17081 case DW_TAG_structure_type:
17082 case DW_TAG_union_type:
17083 case DW_TAG_class_type:
17084 break;
17085
17086 case DW_TAG_subprogram:
17087 if (!get_AT_flag (die, DW_AT_declaration)
17088 || die->die_definition != NULL)
17089 prune_unused_types_mark (die, 1);
17090 return;
17091
17092 default:
17093 return;
17094 }
17095
17096 /* Mark children. */
17097 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
17098 }
17099
17100 /* Walk the tree DIE and mark types that we actually use. */
17101
17102 static void
17103 prune_unused_types_walk (dw_die_ref die)
17104 {
17105 dw_die_ref c;
17106
17107 /* Don't do anything if this node is already marked and
17108 children have been marked as well. */
17109 if (die->die_mark == 2)
17110 return;
17111
17112 switch (die->die_tag)
17113 {
17114 case DW_TAG_structure_type:
17115 case DW_TAG_union_type:
17116 case DW_TAG_class_type:
17117 if (die->die_perennial_p)
17118 break;
17119
17120 for (c = die->die_parent; c; c = c->die_parent)
17121 if (c->die_tag == DW_TAG_subprogram)
17122 break;
17123
17124 /* Finding used static member functions inside of classes
17125 is needed just for local classes, because for other classes
17126 static member function DIEs with DW_AT_specification
17127 are emitted outside of the DW_TAG_*_type. If we ever change
17128 it, we'd need to call this even for non-local classes. */
17129 if (c)
17130 prune_unused_types_walk_local_classes (die);
17131
17132 /* It's a type node --- don't mark it. */
17133 return;
17134
17135 case DW_TAG_const_type:
17136 case DW_TAG_packed_type:
17137 case DW_TAG_pointer_type:
17138 case DW_TAG_reference_type:
17139 case DW_TAG_volatile_type:
17140 case DW_TAG_typedef:
17141 case DW_TAG_array_type:
17142 case DW_TAG_interface_type:
17143 case DW_TAG_friend:
17144 case DW_TAG_variant_part:
17145 case DW_TAG_enumeration_type:
17146 case DW_TAG_subroutine_type:
17147 case DW_TAG_string_type:
17148 case DW_TAG_set_type:
17149 case DW_TAG_subrange_type:
17150 case DW_TAG_ptr_to_member_type:
17151 case DW_TAG_file_type:
17152 if (die->die_perennial_p)
17153 break;
17154
17155 /* It's a type node --- don't mark it. */
17156 return;
17157
17158 default:
17159 /* Mark everything else. */
17160 break;
17161 }
17162
17163 if (die->die_mark == 0)
17164 {
17165 die->die_mark = 1;
17166
17167 /* Now, mark any dies referenced from here. */
17168 prune_unused_types_walk_attribs (die);
17169 }
17170
17171 die->die_mark = 2;
17172
17173 /* Mark children. */
17174 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
17175 }
17176
17177 /* Increment the string counts on strings referred to from DIE's
17178 attributes. */
17179
17180 static void
17181 prune_unused_types_update_strings (dw_die_ref die)
17182 {
17183 dw_attr_ref a;
17184 unsigned ix;
17185
17186 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
17187 if (AT_class (a) == dw_val_class_str)
17188 {
17189 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
17190 s->refcount++;
17191 /* Avoid unnecessarily putting strings that are used less than
17192 twice in the hash table. */
17193 if (s->refcount
17194 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
17195 {
17196 void ** slot;
17197 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
17198 htab_hash_string (s->str),
17199 INSERT);
17200 gcc_assert (*slot == NULL);
17201 *slot = s;
17202 }
17203 }
17204 }
17205
17206 /* Remove from the tree DIE any dies that aren't marked. */
17207
17208 static void
17209 prune_unused_types_prune (dw_die_ref die)
17210 {
17211 dw_die_ref c;
17212
17213 gcc_assert (die->die_mark);
17214 prune_unused_types_update_strings (die);
17215
17216 if (! die->die_child)
17217 return;
17218
17219 c = die->die_child;
17220 do {
17221 dw_die_ref prev = c;
17222 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
17223 if (c == die->die_child)
17224 {
17225 /* No marked children between 'prev' and the end of the list. */
17226 if (prev == c)
17227 /* No marked children at all. */
17228 die->die_child = NULL;
17229 else
17230 {
17231 prev->die_sib = c->die_sib;
17232 die->die_child = prev;
17233 }
17234 return;
17235 }
17236
17237 if (c != prev->die_sib)
17238 prev->die_sib = c;
17239 prune_unused_types_prune (c);
17240 } while (c != die->die_child);
17241 }
17242
17243
17244 /* Remove dies representing declarations that we never use. */
17245
17246 static void
17247 prune_unused_types (void)
17248 {
17249 unsigned int i;
17250 limbo_die_node *node;
17251 pubname_ref pub;
17252
17253 #if ENABLE_ASSERT_CHECKING
17254 /* All the marks should already be clear. */
17255 verify_marks_clear (comp_unit_die);
17256 for (node = limbo_die_list; node; node = node->next)
17257 verify_marks_clear (node->die);
17258 #endif /* ENABLE_ASSERT_CHECKING */
17259
17260 /* Set the mark on nodes that are actually used. */
17261 prune_unused_types_walk (comp_unit_die);
17262 for (node = limbo_die_list; node; node = node->next)
17263 prune_unused_types_walk (node->die);
17264
17265 /* Also set the mark on nodes referenced from the
17266 pubname_table or arange_table. */
17267 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
17268 prune_unused_types_mark (pub->die, 1);
17269 for (i = 0; i < arange_table_in_use; i++)
17270 prune_unused_types_mark (arange_table[i], 1);
17271
17272 /* Get rid of nodes that aren't marked; and update the string counts. */
17273 if (debug_str_hash)
17274 htab_empty (debug_str_hash);
17275 prune_unused_types_prune (comp_unit_die);
17276 for (node = limbo_die_list; node; node = node->next)
17277 prune_unused_types_prune (node->die);
17278
17279 /* Leave the marks clear. */
17280 prune_unmark_dies (comp_unit_die);
17281 for (node = limbo_die_list; node; node = node->next)
17282 prune_unmark_dies (node->die);
17283 }
17284
17285 /* Set the parameter to true if there are any relative pathnames in
17286 the file table. */
17287 static int
17288 file_table_relative_p (void ** slot, void *param)
17289 {
17290 bool *p = (bool *) param;
17291 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
17292 if (!IS_ABSOLUTE_PATH (d->filename))
17293 {
17294 *p = true;
17295 return 0;
17296 }
17297 return 1;
17298 }
17299
17300 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
17301 to the location it would have been added, should we know its
17302 DECL_ASSEMBLER_NAME when we added other attributes. This will
17303 probably improve compactness of debug info, removing equivalent
17304 abbrevs, and hide any differences caused by deferring the
17305 computation of the assembler name, triggered by e.g. PCH. */
17306
17307 static inline void
17308 move_linkage_attr (dw_die_ref die)
17309 {
17310 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
17311 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
17312
17313 gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
17314
17315 while (--ix > 0)
17316 {
17317 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
17318
17319 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
17320 break;
17321 }
17322
17323 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
17324 {
17325 VEC_pop (dw_attr_node, die->die_attr);
17326 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
17327 }
17328 }
17329
17330 /* Output stuff that dwarf requires at the end of every file,
17331 and generate the DWARF-2 debugging info. */
17332
17333 static void
17334 dwarf2out_finish (const char *filename)
17335 {
17336 limbo_die_node *node, *next_node;
17337 dw_die_ref die = 0;
17338 unsigned int i;
17339
17340 /* Add the name for the main input file now. We delayed this from
17341 dwarf2out_init to avoid complications with PCH. */
17342 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
17343 if (!IS_ABSOLUTE_PATH (filename))
17344 add_comp_dir_attribute (comp_unit_die);
17345 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
17346 {
17347 bool p = false;
17348 htab_traverse (file_table, file_table_relative_p, &p);
17349 if (p)
17350 add_comp_dir_attribute (comp_unit_die);
17351 }
17352
17353 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
17354 {
17355 add_location_or_const_value_attribute (
17356 VEC_index (deferred_locations, deferred_locations_list, i)->die,
17357 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
17358 DW_AT_location);
17359 }
17360
17361 /* Traverse the limbo die list, and add parent/child links. The only
17362 dies without parents that should be here are concrete instances of
17363 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
17364 For concrete instances, we can get the parent die from the abstract
17365 instance. */
17366 for (node = limbo_die_list; node; node = next_node)
17367 {
17368 next_node = node->next;
17369 die = node->die;
17370
17371 if (die->die_parent == NULL)
17372 {
17373 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
17374
17375 if (origin)
17376 add_child_die (origin->die_parent, die);
17377 else if (die == comp_unit_die)
17378 ;
17379 else if (errorcount > 0 || sorrycount > 0)
17380 /* It's OK to be confused by errors in the input. */
17381 add_child_die (comp_unit_die, die);
17382 else
17383 {
17384 /* In certain situations, the lexical block containing a
17385 nested function can be optimized away, which results
17386 in the nested function die being orphaned. Likewise
17387 with the return type of that nested function. Force
17388 this to be a child of the containing function.
17389
17390 It may happen that even the containing function got fully
17391 inlined and optimized out. In that case we are lost and
17392 assign the empty child. This should not be big issue as
17393 the function is likely unreachable too. */
17394 tree context = NULL_TREE;
17395
17396 gcc_assert (node->created_for);
17397
17398 if (DECL_P (node->created_for))
17399 context = DECL_CONTEXT (node->created_for);
17400 else if (TYPE_P (node->created_for))
17401 context = TYPE_CONTEXT (node->created_for);
17402
17403 gcc_assert (context
17404 && (TREE_CODE (context) == FUNCTION_DECL
17405 || TREE_CODE (context) == NAMESPACE_DECL));
17406
17407 origin = lookup_decl_die (context);
17408 if (origin)
17409 add_child_die (origin, die);
17410 else
17411 add_child_die (comp_unit_die, die);
17412 }
17413 }
17414 }
17415
17416 limbo_die_list = NULL;
17417
17418 for (node = deferred_asm_name; node; node = node->next)
17419 {
17420 tree decl = node->created_for;
17421 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17422 {
17423 add_AT_string (node->die, DW_AT_MIPS_linkage_name,
17424 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
17425 move_linkage_attr (node->die);
17426 }
17427 }
17428
17429 deferred_asm_name = NULL;
17430
17431 /* Walk through the list of incomplete types again, trying once more to
17432 emit full debugging info for them. */
17433 retry_incomplete_types ();
17434
17435 if (flag_eliminate_unused_debug_types)
17436 prune_unused_types ();
17437
17438 /* Generate separate CUs for each of the include files we've seen.
17439 They will go into limbo_die_list. */
17440 if (flag_eliminate_dwarf2_dups)
17441 break_out_includes (comp_unit_die);
17442
17443 /* Traverse the DIE's and add add sibling attributes to those DIE's
17444 that have children. */
17445 add_sibling_attributes (comp_unit_die);
17446 for (node = limbo_die_list; node; node = node->next)
17447 add_sibling_attributes (node->die);
17448
17449 /* Output a terminator label for the .text section. */
17450 switch_to_section (text_section);
17451 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
17452 if (flag_reorder_blocks_and_partition)
17453 {
17454 switch_to_section (unlikely_text_section ());
17455 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
17456 }
17457
17458 /* We can only use the low/high_pc attributes if all of the code was
17459 in .text. */
17460 if (!have_multiple_function_sections)
17461 {
17462 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
17463 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
17464 }
17465
17466 else
17467 {
17468 unsigned fde_idx = 0;
17469
17470 /* We need to give .debug_loc and .debug_ranges an appropriate
17471 "base address". Use zero so that these addresses become
17472 absolute. Historically, we've emitted the unexpected
17473 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
17474 Emit both to give time for other tools to adapt. */
17475 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
17476 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
17477
17478 add_AT_range_list (comp_unit_die, DW_AT_ranges,
17479 add_ranges_by_labels (text_section_label,
17480 text_end_label));
17481 if (flag_reorder_blocks_and_partition)
17482 add_ranges_by_labels (cold_text_section_label,
17483 cold_end_label);
17484
17485 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
17486 {
17487 dw_fde_ref fde = &fde_table[fde_idx];
17488
17489 if (fde->dw_fde_switched_sections)
17490 {
17491 if (!fde->in_std_section)
17492 add_ranges_by_labels (fde->dw_fde_hot_section_label,
17493 fde->dw_fde_hot_section_end_label);
17494 if (!fde->cold_in_std_section)
17495 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
17496 fde->dw_fde_unlikely_section_end_label);
17497 }
17498 else if (!fde->in_std_section)
17499 add_ranges_by_labels (fde->dw_fde_begin,
17500 fde->dw_fde_end);
17501 }
17502
17503 add_ranges (NULL);
17504 }
17505
17506 /* Output location list section if necessary. */
17507 if (have_location_lists)
17508 {
17509 /* Output the location lists info. */
17510 switch_to_section (debug_loc_section);
17511 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
17512 DEBUG_LOC_SECTION_LABEL, 0);
17513 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
17514 output_location_lists (die);
17515 }
17516
17517 if (debug_info_level >= DINFO_LEVEL_NORMAL)
17518 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
17519 debug_line_section_label);
17520
17521 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17522 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
17523
17524 /* Output all of the compilation units. We put the main one last so that
17525 the offsets are available to output_pubnames. */
17526 for (node = limbo_die_list; node; node = node->next)
17527 output_comp_unit (node->die, 0);
17528
17529 /* Output the main compilation unit if non-empty or if .debug_macinfo
17530 has been emitted. */
17531 output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
17532
17533 /* Output the abbreviation table. */
17534 switch_to_section (debug_abbrev_section);
17535 output_abbrev_section ();
17536
17537 /* Output public names table if necessary. */
17538 if (!VEC_empty (pubname_entry, pubname_table))
17539 {
17540 switch_to_section (debug_pubnames_section);
17541 output_pubnames (pubname_table);
17542 }
17543
17544 /* Output public types table if necessary. */
17545 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
17546 It shouldn't hurt to emit it always, since pure DWARF2 consumers
17547 simply won't look for the section. */
17548 if (!VEC_empty (pubname_entry, pubtype_table))
17549 {
17550 switch_to_section (debug_pubtypes_section);
17551 output_pubnames (pubtype_table);
17552 }
17553
17554 /* Output the address range information. We only put functions in the arange
17555 table, so don't write it out if we don't have any. */
17556 if (fde_table_in_use)
17557 {
17558 switch_to_section (debug_aranges_section);
17559 output_aranges ();
17560 }
17561
17562 /* Output ranges section if necessary. */
17563 if (ranges_table_in_use)
17564 {
17565 switch_to_section (debug_ranges_section);
17566 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
17567 output_ranges ();
17568 }
17569
17570 /* Output the source line correspondence table. We must do this
17571 even if there is no line information. Otherwise, on an empty
17572 translation unit, we will generate a present, but empty,
17573 .debug_info section. IRIX 6.5 `nm' will then complain when
17574 examining the file. This is done late so that any filenames
17575 used by the debug_info section are marked as 'used'. */
17576 if (! DWARF2_ASM_LINE_DEBUG_INFO)
17577 {
17578 switch_to_section (debug_line_section);
17579 output_line_info ();
17580 }
17581
17582 /* Have to end the macro section. */
17583 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17584 {
17585 switch_to_section (debug_macinfo_section);
17586 dw2_asm_output_data (1, 0, "End compilation unit");
17587 }
17588
17589 /* If we emitted any DW_FORM_strp form attribute, output the string
17590 table too. */
17591 if (debug_str_hash)
17592 htab_traverse (debug_str_hash, output_indirect_string, NULL);
17593 }
17594 #else
17595
17596 /* This should never be used, but its address is needed for comparisons. */
17597 const struct gcc_debug_hooks dwarf2_debug_hooks =
17598 {
17599 0, /* init */
17600 0, /* finish */
17601 0, /* define */
17602 0, /* undef */
17603 0, /* start_source_file */
17604 0, /* end_source_file */
17605 0, /* begin_block */
17606 0, /* end_block */
17607 0, /* ignore_block */
17608 0, /* source_line */
17609 0, /* begin_prologue */
17610 0, /* end_prologue */
17611 0, /* end_epilogue */
17612 0, /* begin_function */
17613 0, /* end_function */
17614 0, /* function_decl */
17615 0, /* global_decl */
17616 0, /* type_decl */
17617 0, /* imported_module_or_decl */
17618 0, /* deferred_inline_function */
17619 0, /* outlining_inline_function */
17620 0, /* label */
17621 0, /* handle_pch */
17622 0, /* var_location */
17623 0, /* switch_text_section */
17624 0, /* set_name */
17625 0 /* start_end_main_source_file */
17626 };
17627
17628 #endif /* DWARF2_DEBUGGING_INFO */
17629
17630 #include "gt-dwarf2out.h"