tree-data-ref.c (subscript_dependence_tester_1): Call free_conflict_function.
[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 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 *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 # define DWARF2_FRAME_INFO \
100 (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 # define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107 collected using DWARF_FRAME_REGNUM to those that should be output in
108 .debug_frame and .eh_frame. */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Decide whether we want to emit frame unwind information for the current
114 translation unit. */
115
116 int
117 dwarf2out_do_frame (void)
118 {
119 /* We want to emit correct CFA location expressions or lists, so we
120 have to return true if we're going to output debug info, even if
121 we're not going to output frame or unwind info. */
122 return (write_symbols == DWARF2_DEBUG
123 || write_symbols == VMS_AND_DWARF2_DEBUG
124 || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126 || (DWARF2_UNWIND_INFO
127 && (flag_unwind_tables
128 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
130 );
131 }
132
133 /* The size of the target's pointer type. */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
137
138 /* Array of RTXes referenced by the debugging information, which therefore
139 must be kept around forever. */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
141
142 /* A pointer to the base of a list of incomplete types which might be
143 completed at some later time. incomplete_types_list needs to be a
144 VEC(tree,gc) because we want to tell the garbage collector about
145 it. */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
147
148 /* A pointer to the base of a table of references to declaration
149 scopes. This table is a display which tracks the nesting
150 of declaration scopes at the current scope and containing
151 scopes. This table is used to find the proper place to
152 define type declaration DIE's. */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
154
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
167
168 /* How to start an assembler comment. */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
172
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
176
177 /* Call frames are described using a sequence of Call Frame
178 Information instructions. The register number, offset
179 and address fields are provided as possible operands;
180 their use is selected by the opcode field. */
181
182 enum dw_cfi_oprnd_type {
183 dw_cfi_oprnd_unused,
184 dw_cfi_oprnd_reg_num,
185 dw_cfi_oprnd_offset,
186 dw_cfi_oprnd_addr,
187 dw_cfi_oprnd_loc
188 };
189
190 typedef union dw_cfi_oprnd_struct GTY(())
191 {
192 unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
196 }
197 dw_cfi_oprnd;
198
199 typedef struct dw_cfi_struct GTY(())
200 {
201 dw_cfi_ref dw_cfi_next;
202 enum dwarf_call_frame_info dw_cfi_opc;
203 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204 dw_cfi_oprnd1;
205 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206 dw_cfi_oprnd2;
207 }
208 dw_cfi_node;
209
210 /* This is how we define the location of the CFA. We use to handle it
211 as REG + OFFSET all the time, but now it can be more complex.
212 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213 Instead of passing around REG and OFFSET, we pass a copy
214 of this structure. */
215 typedef struct cfa_loc GTY(())
216 {
217 HOST_WIDE_INT offset;
218 HOST_WIDE_INT base_offset;
219 unsigned int reg;
220 int indirect; /* 1 if CFA is accessed via a dereference. */
221 } dw_cfa_location;
222
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224 refer to a single Common Information Entry (CIE), defined at
225 the beginning of the .debug_frame section. This use of a single
226 CIE obviates the need to keep track of multiple CIE's
227 in the DWARF generation routines below. */
228
229 typedef struct dw_fde_struct GTY(())
230 {
231 tree decl;
232 const char *dw_fde_begin;
233 const char *dw_fde_current_label;
234 const char *dw_fde_end;
235 const char *dw_fde_hot_section_label;
236 const char *dw_fde_hot_section_end_label;
237 const char *dw_fde_unlikely_section_label;
238 const char *dw_fde_unlikely_section_end_label;
239 bool dw_fde_switched_sections;
240 dw_cfi_ref dw_fde_cfi;
241 unsigned funcdef_number;
242 unsigned all_throwers_are_sibcalls : 1;
243 unsigned nothrow : 1;
244 unsigned uses_eh_lsda : 1;
245 }
246 dw_fde_node;
247
248 /* Maximum size (in bytes) of an artificially generated label. */
249 #define MAX_ARTIFICIAL_LABEL_BYTES 30
250
251 /* The size of addresses as they appear in the Dwarf 2 data.
252 Some architectures use word addresses to refer to code locations,
253 but Dwarf 2 info always uses byte addresses. On such machines,
254 Dwarf 2 addresses need to be larger than the architecture's
255 pointers. */
256 #ifndef DWARF2_ADDR_SIZE
257 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258 #endif
259
260 /* The size in bytes of a DWARF field indicating an offset or length
261 relative to a debug info section, specified to be 4 bytes in the
262 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
263 as PTR_SIZE. */
264
265 #ifndef DWARF_OFFSET_SIZE
266 #define DWARF_OFFSET_SIZE 4
267 #endif
268
269 /* According to the (draft) DWARF 3 specification, the initial length
270 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
271 bytes are 0xffffffff, followed by the length stored in the next 8
272 bytes.
273
274 However, the SGI/MIPS ABI uses an initial length which is equal to
275 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
276
277 #ifndef DWARF_INITIAL_LENGTH_SIZE
278 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279 #endif
280
281 #define DWARF_VERSION 2
282
283 /* Round SIZE up to the nearest BOUNDARY. */
284 #define DWARF_ROUND(SIZE,BOUNDARY) \
285 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
286
287 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
288 #ifndef DWARF_CIE_DATA_ALIGNMENT
289 #ifdef STACK_GROWS_DOWNWARD
290 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
291 #else
292 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
293 #endif
294 #endif
295
296 /* CIE identifier. */
297 #if HOST_BITS_PER_WIDE_INT >= 64
298 #define DWARF_CIE_ID \
299 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300 #else
301 #define DWARF_CIE_ID DW_CIE_ID
302 #endif
303
304 /* A pointer to the base of a table that contains frame description
305 information for each routine. */
306 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
307
308 /* Number of elements currently allocated for fde_table. */
309 static GTY(()) unsigned fde_table_allocated;
310
311 /* Number of elements in fde_table currently in use. */
312 static GTY(()) unsigned fde_table_in_use;
313
314 /* Size (in elements) of increments by which we may expand the
315 fde_table. */
316 #define FDE_TABLE_INCREMENT 256
317
318 /* A list of call frame insns for the CIE. */
319 static GTY(()) dw_cfi_ref cie_cfi_head;
320
321 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
322 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
323 attribute that accelerates the lookup of the FDE associated
324 with the subprogram. This variable holds the table index of the FDE
325 associated with the current function (body) definition. */
326 static unsigned current_funcdef_fde;
327 #endif
328
329 struct indirect_string_node GTY(())
330 {
331 const char *str;
332 unsigned int refcount;
333 unsigned int form;
334 char *label;
335 };
336
337 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
338
339 static GTY(()) int dw2_string_counter;
340 static GTY(()) unsigned long dwarf2out_cfi_label_num;
341
342 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
343
344 /* Forward declarations for functions defined in this file. */
345
346 static char *stripattributes (const char *);
347 static const char *dwarf_cfi_name (unsigned);
348 static dw_cfi_ref new_cfi (void);
349 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
350 static void add_fde_cfi (const char *, dw_cfi_ref);
351 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
352 static void lookup_cfa (dw_cfa_location *);
353 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
354 #ifdef DWARF2_UNWIND_INFO
355 static void initial_return_save (rtx);
356 #endif
357 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
358 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
359 static void output_call_frame_info (int);
360 static void dwarf2out_stack_adjust (rtx, bool);
361 static void flush_queued_reg_saves (void);
362 static bool clobbers_queued_reg_save (const_rtx);
363 static void dwarf2out_frame_debug_expr (rtx, const char *);
364
365 /* Support for complex CFA locations. */
366 static void output_cfa_loc (dw_cfi_ref);
367 static void get_cfa_from_loc_descr (dw_cfa_location *,
368 struct dw_loc_descr_struct *);
369 static struct dw_loc_descr_struct *build_cfa_loc
370 (dw_cfa_location *, HOST_WIDE_INT);
371 static void def_cfa_1 (const char *, dw_cfa_location *);
372
373 /* How to start an assembler comment. */
374 #ifndef ASM_COMMENT_START
375 #define ASM_COMMENT_START ";#"
376 #endif
377
378 /* Data and reference forms for relocatable data. */
379 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
380 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
381
382 #ifndef DEBUG_FRAME_SECTION
383 #define DEBUG_FRAME_SECTION ".debug_frame"
384 #endif
385
386 #ifndef FUNC_BEGIN_LABEL
387 #define FUNC_BEGIN_LABEL "LFB"
388 #endif
389
390 #ifndef FUNC_END_LABEL
391 #define FUNC_END_LABEL "LFE"
392 #endif
393
394 #ifndef FRAME_BEGIN_LABEL
395 #define FRAME_BEGIN_LABEL "Lframe"
396 #endif
397 #define CIE_AFTER_SIZE_LABEL "LSCIE"
398 #define CIE_END_LABEL "LECIE"
399 #define FDE_LABEL "LSFDE"
400 #define FDE_AFTER_SIZE_LABEL "LASFDE"
401 #define FDE_END_LABEL "LEFDE"
402 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
403 #define LINE_NUMBER_END_LABEL "LELT"
404 #define LN_PROLOG_AS_LABEL "LASLTP"
405 #define LN_PROLOG_END_LABEL "LELTP"
406 #define DIE_LABEL_PREFIX "DW"
407
408 /* The DWARF 2 CFA column which tracks the return address. Normally this
409 is the column for PC, or the first column after all of the hard
410 registers. */
411 #ifndef DWARF_FRAME_RETURN_COLUMN
412 #ifdef PC_REGNUM
413 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
414 #else
415 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
416 #endif
417 #endif
418
419 /* The mapping from gcc register number to DWARF 2 CFA column number. By
420 default, we just provide columns for all registers. */
421 #ifndef DWARF_FRAME_REGNUM
422 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
423 #endif
424 \f
425 /* Hook used by __throw. */
426
427 rtx
428 expand_builtin_dwarf_sp_column (void)
429 {
430 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
431 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
432 }
433
434 /* Return a pointer to a copy of the section string name S with all
435 attributes stripped off, and an asterisk prepended (for assemble_name). */
436
437 static inline char *
438 stripattributes (const char *s)
439 {
440 char *stripped = XNEWVEC (char, strlen (s) + 2);
441 char *p = stripped;
442
443 *p++ = '*';
444
445 while (*s && *s != ',')
446 *p++ = *s++;
447
448 *p = '\0';
449 return stripped;
450 }
451
452 /* MEM is a memory reference for the register size table, each element of
453 which has mode MODE. Initialize column C as a return address column. */
454
455 static void
456 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
457 {
458 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
459 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
460 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
461 }
462
463 /* Generate code to initialize the register size table. */
464
465 void
466 expand_builtin_init_dwarf_reg_sizes (tree address)
467 {
468 unsigned int i;
469 enum machine_mode mode = TYPE_MODE (char_type_node);
470 rtx addr = expand_normal (address);
471 rtx mem = gen_rtx_MEM (BLKmode, addr);
472 bool wrote_return_column = false;
473
474 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
475 {
476 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
477
478 if (rnum < DWARF_FRAME_REGISTERS)
479 {
480 HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
481 enum machine_mode save_mode = reg_raw_mode[i];
482 HOST_WIDE_INT size;
483
484 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
485 save_mode = choose_hard_reg_mode (i, 1, true);
486 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
487 {
488 if (save_mode == VOIDmode)
489 continue;
490 wrote_return_column = true;
491 }
492 size = GET_MODE_SIZE (save_mode);
493 if (offset < 0)
494 continue;
495
496 emit_move_insn (adjust_address (mem, mode, offset),
497 gen_int_mode (size, mode));
498 }
499 }
500
501 if (!wrote_return_column)
502 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
503
504 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
505 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
506 #endif
507
508 targetm.init_dwarf_reg_sizes_extra (address);
509 }
510
511 /* Convert a DWARF call frame info. operation to its string name */
512
513 static const char *
514 dwarf_cfi_name (unsigned int cfi_opc)
515 {
516 switch (cfi_opc)
517 {
518 case DW_CFA_advance_loc:
519 return "DW_CFA_advance_loc";
520 case DW_CFA_offset:
521 return "DW_CFA_offset";
522 case DW_CFA_restore:
523 return "DW_CFA_restore";
524 case DW_CFA_nop:
525 return "DW_CFA_nop";
526 case DW_CFA_set_loc:
527 return "DW_CFA_set_loc";
528 case DW_CFA_advance_loc1:
529 return "DW_CFA_advance_loc1";
530 case DW_CFA_advance_loc2:
531 return "DW_CFA_advance_loc2";
532 case DW_CFA_advance_loc4:
533 return "DW_CFA_advance_loc4";
534 case DW_CFA_offset_extended:
535 return "DW_CFA_offset_extended";
536 case DW_CFA_restore_extended:
537 return "DW_CFA_restore_extended";
538 case DW_CFA_undefined:
539 return "DW_CFA_undefined";
540 case DW_CFA_same_value:
541 return "DW_CFA_same_value";
542 case DW_CFA_register:
543 return "DW_CFA_register";
544 case DW_CFA_remember_state:
545 return "DW_CFA_remember_state";
546 case DW_CFA_restore_state:
547 return "DW_CFA_restore_state";
548 case DW_CFA_def_cfa:
549 return "DW_CFA_def_cfa";
550 case DW_CFA_def_cfa_register:
551 return "DW_CFA_def_cfa_register";
552 case DW_CFA_def_cfa_offset:
553 return "DW_CFA_def_cfa_offset";
554
555 /* DWARF 3 */
556 case DW_CFA_def_cfa_expression:
557 return "DW_CFA_def_cfa_expression";
558 case DW_CFA_expression:
559 return "DW_CFA_expression";
560 case DW_CFA_offset_extended_sf:
561 return "DW_CFA_offset_extended_sf";
562 case DW_CFA_def_cfa_sf:
563 return "DW_CFA_def_cfa_sf";
564 case DW_CFA_def_cfa_offset_sf:
565 return "DW_CFA_def_cfa_offset_sf";
566
567 /* SGI/MIPS specific */
568 case DW_CFA_MIPS_advance_loc8:
569 return "DW_CFA_MIPS_advance_loc8";
570
571 /* GNU extensions */
572 case DW_CFA_GNU_window_save:
573 return "DW_CFA_GNU_window_save";
574 case DW_CFA_GNU_args_size:
575 return "DW_CFA_GNU_args_size";
576 case DW_CFA_GNU_negative_offset_extended:
577 return "DW_CFA_GNU_negative_offset_extended";
578
579 default:
580 return "DW_CFA_<unknown>";
581 }
582 }
583
584 /* Return a pointer to a newly allocated Call Frame Instruction. */
585
586 static inline dw_cfi_ref
587 new_cfi (void)
588 {
589 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
590
591 cfi->dw_cfi_next = NULL;
592 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
593 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
594
595 return cfi;
596 }
597
598 /* Add a Call Frame Instruction to list of instructions. */
599
600 static inline void
601 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
602 {
603 dw_cfi_ref *p;
604
605 /* Find the end of the chain. */
606 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
607 ;
608
609 *p = cfi;
610 }
611
612 /* Generate a new label for the CFI info to refer to. */
613
614 char *
615 dwarf2out_cfi_label (void)
616 {
617 static char label[20];
618
619 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
620 ASM_OUTPUT_LABEL (asm_out_file, label);
621 return label;
622 }
623
624 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
625 or to the CIE if LABEL is NULL. */
626
627 static void
628 add_fde_cfi (const char *label, dw_cfi_ref cfi)
629 {
630 if (label)
631 {
632 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
633
634 if (*label == 0)
635 label = dwarf2out_cfi_label ();
636
637 if (fde->dw_fde_current_label == NULL
638 || strcmp (label, fde->dw_fde_current_label) != 0)
639 {
640 dw_cfi_ref xcfi;
641
642 label = xstrdup (label);
643
644 /* Set the location counter to the new label. */
645 xcfi = new_cfi ();
646 /* If we have a current label, advance from there, otherwise
647 set the location directly using set_loc. */
648 xcfi->dw_cfi_opc = fde->dw_fde_current_label
649 ? DW_CFA_advance_loc4
650 : DW_CFA_set_loc;
651 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
652 add_cfi (&fde->dw_fde_cfi, xcfi);
653
654 fde->dw_fde_current_label = label;
655 }
656
657 add_cfi (&fde->dw_fde_cfi, cfi);
658 }
659
660 else
661 add_cfi (&cie_cfi_head, cfi);
662 }
663
664 /* Subroutine of lookup_cfa. */
665
666 static void
667 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
668 {
669 switch (cfi->dw_cfi_opc)
670 {
671 case DW_CFA_def_cfa_offset:
672 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
673 break;
674 case DW_CFA_def_cfa_offset_sf:
675 loc->offset
676 = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
677 break;
678 case DW_CFA_def_cfa_register:
679 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
680 break;
681 case DW_CFA_def_cfa:
682 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
683 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
684 break;
685 case DW_CFA_def_cfa_sf:
686 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
687 loc->offset
688 = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689 break;
690 case DW_CFA_def_cfa_expression:
691 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
692 break;
693 default:
694 break;
695 }
696 }
697
698 /* Find the previous value for the CFA. */
699
700 static void
701 lookup_cfa (dw_cfa_location *loc)
702 {
703 dw_cfi_ref cfi;
704
705 loc->reg = INVALID_REGNUM;
706 loc->offset = 0;
707 loc->indirect = 0;
708 loc->base_offset = 0;
709
710 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
711 lookup_cfa_1 (cfi, loc);
712
713 if (fde_table_in_use)
714 {
715 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
716 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
717 lookup_cfa_1 (cfi, loc);
718 }
719 }
720
721 /* The current rule for calculating the DWARF2 canonical frame address. */
722 static dw_cfa_location cfa;
723
724 /* The register used for saving registers to the stack, and its offset
725 from the CFA. */
726 static dw_cfa_location cfa_store;
727
728 /* The running total of the size of arguments pushed onto the stack. */
729 static HOST_WIDE_INT args_size;
730
731 /* The last args_size we actually output. */
732 static HOST_WIDE_INT old_args_size;
733
734 /* Entry point to update the canonical frame address (CFA).
735 LABEL is passed to add_fde_cfi. The value of CFA is now to be
736 calculated from REG+OFFSET. */
737
738 void
739 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
740 {
741 dw_cfa_location loc;
742 loc.indirect = 0;
743 loc.base_offset = 0;
744 loc.reg = reg;
745 loc.offset = offset;
746 def_cfa_1 (label, &loc);
747 }
748
749 /* Determine if two dw_cfa_location structures define the same data. */
750
751 static bool
752 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
753 {
754 return (loc1->reg == loc2->reg
755 && loc1->offset == loc2->offset
756 && loc1->indirect == loc2->indirect
757 && (loc1->indirect == 0
758 || loc1->base_offset == loc2->base_offset));
759 }
760
761 /* This routine does the actual work. The CFA is now calculated from
762 the dw_cfa_location structure. */
763
764 static void
765 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
766 {
767 dw_cfi_ref cfi;
768 dw_cfa_location old_cfa, loc;
769
770 cfa = *loc_p;
771 loc = *loc_p;
772
773 if (cfa_store.reg == loc.reg && loc.indirect == 0)
774 cfa_store.offset = loc.offset;
775
776 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
777 lookup_cfa (&old_cfa);
778
779 /* If nothing changed, no need to issue any call frame instructions. */
780 if (cfa_equal_p (&loc, &old_cfa))
781 return;
782
783 cfi = new_cfi ();
784
785 if (loc.reg == old_cfa.reg && !loc.indirect)
786 {
787 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
788 the CFA register did not change but the offset did. */
789 if (loc.offset < 0)
790 {
791 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
792 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
793
794 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
795 cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
796 }
797 else
798 {
799 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
800 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
801 }
802 }
803
804 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
805 else if (loc.offset == old_cfa.offset
806 && old_cfa.reg != INVALID_REGNUM
807 && !loc.indirect)
808 {
809 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
810 indicating the CFA register has changed to <register> but the
811 offset has not changed. */
812 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
813 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
814 }
815 #endif
816
817 else if (loc.indirect == 0)
818 {
819 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
820 indicating the CFA register has changed to <register> with
821 the specified offset. */
822 if (loc.offset < 0)
823 {
824 HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
825 gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
826
827 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
828 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829 cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
830 }
831 else
832 {
833 cfi->dw_cfi_opc = DW_CFA_def_cfa;
834 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
836 }
837 }
838 else
839 {
840 /* Construct a DW_CFA_def_cfa_expression instruction to
841 calculate the CFA using a full location expression since no
842 register-offset pair is available. */
843 struct dw_loc_descr_struct *loc_list;
844
845 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
846 loc_list = build_cfa_loc (&loc, 0);
847 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
848 }
849
850 add_fde_cfi (label, cfi);
851 }
852
853 /* Add the CFI for saving a register. REG is the CFA column number.
854 LABEL is passed to add_fde_cfi.
855 If SREG is -1, the register is saved at OFFSET from the CFA;
856 otherwise it is saved in SREG. */
857
858 static void
859 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
860 {
861 dw_cfi_ref cfi = new_cfi ();
862
863 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
864
865 if (sreg == INVALID_REGNUM)
866 {
867 if (reg & ~0x3f)
868 /* The register number won't fit in 6 bits, so we have to use
869 the long form. */
870 cfi->dw_cfi_opc = DW_CFA_offset_extended;
871 else
872 cfi->dw_cfi_opc = DW_CFA_offset;
873
874 #ifdef ENABLE_CHECKING
875 {
876 /* If we get an offset that is not a multiple of
877 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
878 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
879 description. */
880 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
881
882 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
883 }
884 #endif
885 offset /= DWARF_CIE_DATA_ALIGNMENT;
886 if (offset < 0)
887 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
888
889 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
890 }
891 else if (sreg == reg)
892 cfi->dw_cfi_opc = DW_CFA_same_value;
893 else
894 {
895 cfi->dw_cfi_opc = DW_CFA_register;
896 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
897 }
898
899 add_fde_cfi (label, cfi);
900 }
901
902 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
903 This CFI tells the unwinder that it needs to restore the window registers
904 from the previous frame's window save area.
905
906 ??? Perhaps we should note in the CIE where windows are saved (instead of
907 assuming 0(cfa)) and what registers are in the window. */
908
909 void
910 dwarf2out_window_save (const char *label)
911 {
912 dw_cfi_ref cfi = new_cfi ();
913
914 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
915 add_fde_cfi (label, cfi);
916 }
917
918 /* Add a CFI to update the running total of the size of arguments
919 pushed onto the stack. */
920
921 void
922 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
923 {
924 dw_cfi_ref cfi;
925
926 if (size == old_args_size)
927 return;
928
929 old_args_size = size;
930
931 cfi = new_cfi ();
932 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
933 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
934 add_fde_cfi (label, cfi);
935 }
936
937 /* Entry point for saving a register to the stack. REG is the GCC register
938 number. LABEL and OFFSET are passed to reg_save. */
939
940 void
941 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
942 {
943 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
944 }
945
946 /* Entry point for saving the return address in the stack.
947 LABEL and OFFSET are passed to reg_save. */
948
949 void
950 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
951 {
952 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
953 }
954
955 /* Entry point for saving the return address in a register.
956 LABEL and SREG are passed to reg_save. */
957
958 void
959 dwarf2out_return_reg (const char *label, unsigned int sreg)
960 {
961 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
962 }
963
964 #ifdef DWARF2_UNWIND_INFO
965 /* Record the initial position of the return address. RTL is
966 INCOMING_RETURN_ADDR_RTX. */
967
968 static void
969 initial_return_save (rtx rtl)
970 {
971 unsigned int reg = INVALID_REGNUM;
972 HOST_WIDE_INT offset = 0;
973
974 switch (GET_CODE (rtl))
975 {
976 case REG:
977 /* RA is in a register. */
978 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
979 break;
980
981 case MEM:
982 /* RA is on the stack. */
983 rtl = XEXP (rtl, 0);
984 switch (GET_CODE (rtl))
985 {
986 case REG:
987 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
988 offset = 0;
989 break;
990
991 case PLUS:
992 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
993 offset = INTVAL (XEXP (rtl, 1));
994 break;
995
996 case MINUS:
997 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
998 offset = -INTVAL (XEXP (rtl, 1));
999 break;
1000
1001 default:
1002 gcc_unreachable ();
1003 }
1004
1005 break;
1006
1007 case PLUS:
1008 /* The return address is at some offset from any value we can
1009 actually load. For instance, on the SPARC it is in %i7+8. Just
1010 ignore the offset for now; it doesn't matter for unwinding frames. */
1011 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1012 initial_return_save (XEXP (rtl, 0));
1013 return;
1014
1015 default:
1016 gcc_unreachable ();
1017 }
1018
1019 if (reg != DWARF_FRAME_RETURN_COLUMN)
1020 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1021 }
1022 #endif
1023
1024 /* Given a SET, calculate the amount of stack adjustment it
1025 contains. */
1026
1027 static HOST_WIDE_INT
1028 stack_adjust_offset (const_rtx pattern)
1029 {
1030 const_rtx src = SET_SRC (pattern);
1031 const_rtx dest = SET_DEST (pattern);
1032 HOST_WIDE_INT offset = 0;
1033 enum rtx_code code;
1034
1035 if (dest == stack_pointer_rtx)
1036 {
1037 /* (set (reg sp) (plus (reg sp) (const_int))) */
1038 code = GET_CODE (src);
1039 if (! (code == PLUS || code == MINUS)
1040 || XEXP (src, 0) != stack_pointer_rtx
1041 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1042 return 0;
1043
1044 offset = INTVAL (XEXP (src, 1));
1045 if (code == PLUS)
1046 offset = -offset;
1047 }
1048 else if (MEM_P (dest))
1049 {
1050 /* (set (mem (pre_dec (reg sp))) (foo)) */
1051 src = XEXP (dest, 0);
1052 code = GET_CODE (src);
1053
1054 switch (code)
1055 {
1056 case PRE_MODIFY:
1057 case POST_MODIFY:
1058 if (XEXP (src, 0) == stack_pointer_rtx)
1059 {
1060 rtx val = XEXP (XEXP (src, 1), 1);
1061 /* We handle only adjustments by constant amount. */
1062 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1063 && GET_CODE (val) == CONST_INT);
1064 offset = -INTVAL (val);
1065 break;
1066 }
1067 return 0;
1068
1069 case PRE_DEC:
1070 case POST_DEC:
1071 if (XEXP (src, 0) == stack_pointer_rtx)
1072 {
1073 offset = GET_MODE_SIZE (GET_MODE (dest));
1074 break;
1075 }
1076 return 0;
1077
1078 case PRE_INC:
1079 case POST_INC:
1080 if (XEXP (src, 0) == stack_pointer_rtx)
1081 {
1082 offset = -GET_MODE_SIZE (GET_MODE (dest));
1083 break;
1084 }
1085 return 0;
1086
1087 default:
1088 return 0;
1089 }
1090 }
1091 else
1092 return 0;
1093
1094 return offset;
1095 }
1096
1097 /* Check INSN to see if it looks like a push or a stack adjustment, and
1098 make a note of it if it does. EH uses this information to find out how
1099 much extra space it needs to pop off the stack. */
1100
1101 static void
1102 dwarf2out_stack_adjust (rtx insn, bool after_p)
1103 {
1104 HOST_WIDE_INT offset;
1105 const char *label;
1106 int i;
1107
1108 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1109 with this function. Proper support would require all frame-related
1110 insns to be marked, and to be able to handle saving state around
1111 epilogues textually in the middle of the function. */
1112 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1113 return;
1114
1115 /* If only calls can throw, and we have a frame pointer,
1116 save up adjustments until we see the CALL_INSN. */
1117 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1118 {
1119 if (CALL_P (insn) && !after_p)
1120 {
1121 /* Extract the size of the args from the CALL rtx itself. */
1122 insn = PATTERN (insn);
1123 if (GET_CODE (insn) == PARALLEL)
1124 insn = XVECEXP (insn, 0, 0);
1125 if (GET_CODE (insn) == SET)
1126 insn = SET_SRC (insn);
1127 gcc_assert (GET_CODE (insn) == CALL);
1128 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1129 }
1130 return;
1131 }
1132
1133 if (CALL_P (insn) && !after_p)
1134 {
1135 if (!flag_asynchronous_unwind_tables)
1136 dwarf2out_args_size ("", args_size);
1137 return;
1138 }
1139 else if (BARRIER_P (insn))
1140 {
1141 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1142 the compiler will have already emitted a stack adjustment, but
1143 doesn't bother for calls to noreturn functions. */
1144 #ifdef STACK_GROWS_DOWNWARD
1145 offset = -args_size;
1146 #else
1147 offset = args_size;
1148 #endif
1149 }
1150 else if (GET_CODE (PATTERN (insn)) == SET)
1151 offset = stack_adjust_offset (PATTERN (insn));
1152 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1153 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1154 {
1155 /* There may be stack adjustments inside compound insns. Search
1156 for them. */
1157 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1158 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1159 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1160 }
1161 else
1162 return;
1163
1164 if (offset == 0)
1165 return;
1166
1167 if (cfa.reg == STACK_POINTER_REGNUM)
1168 cfa.offset += offset;
1169
1170 #ifndef STACK_GROWS_DOWNWARD
1171 offset = -offset;
1172 #endif
1173
1174 args_size += offset;
1175 if (args_size < 0)
1176 args_size = 0;
1177
1178 label = dwarf2out_cfi_label ();
1179 def_cfa_1 (label, &cfa);
1180 if (flag_asynchronous_unwind_tables)
1181 dwarf2out_args_size (label, args_size);
1182 }
1183
1184 #endif
1185
1186 /* We delay emitting a register save until either (a) we reach the end
1187 of the prologue or (b) the register is clobbered. This clusters
1188 register saves so that there are fewer pc advances. */
1189
1190 struct queued_reg_save GTY(())
1191 {
1192 struct queued_reg_save *next;
1193 rtx reg;
1194 HOST_WIDE_INT cfa_offset;
1195 rtx saved_reg;
1196 };
1197
1198 static GTY(()) struct queued_reg_save *queued_reg_saves;
1199
1200 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1201 struct reg_saved_in_data GTY(()) {
1202 rtx orig_reg;
1203 rtx saved_in_reg;
1204 };
1205
1206 /* A list of registers saved in other registers.
1207 The list intentionally has a small maximum capacity of 4; if your
1208 port needs more than that, you might consider implementing a
1209 more efficient data structure. */
1210 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1211 static GTY(()) size_t num_regs_saved_in_regs;
1212
1213 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1214 static const char *last_reg_save_label;
1215
1216 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1217 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1218
1219 static void
1220 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1221 {
1222 struct queued_reg_save *q;
1223
1224 /* Duplicates waste space, but it's also necessary to remove them
1225 for correctness, since the queue gets output in reverse
1226 order. */
1227 for (q = queued_reg_saves; q != NULL; q = q->next)
1228 if (REGNO (q->reg) == REGNO (reg))
1229 break;
1230
1231 if (q == NULL)
1232 {
1233 q = ggc_alloc (sizeof (*q));
1234 q->next = queued_reg_saves;
1235 queued_reg_saves = q;
1236 }
1237
1238 q->reg = reg;
1239 q->cfa_offset = offset;
1240 q->saved_reg = sreg;
1241
1242 last_reg_save_label = label;
1243 }
1244
1245 /* Output all the entries in QUEUED_REG_SAVES. */
1246
1247 static void
1248 flush_queued_reg_saves (void)
1249 {
1250 struct queued_reg_save *q;
1251
1252 for (q = queued_reg_saves; q; q = q->next)
1253 {
1254 size_t i;
1255 unsigned int reg, sreg;
1256
1257 for (i = 0; i < num_regs_saved_in_regs; i++)
1258 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1259 break;
1260 if (q->saved_reg && i == num_regs_saved_in_regs)
1261 {
1262 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1263 num_regs_saved_in_regs++;
1264 }
1265 if (i != num_regs_saved_in_regs)
1266 {
1267 regs_saved_in_regs[i].orig_reg = q->reg;
1268 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1269 }
1270
1271 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1272 if (q->saved_reg)
1273 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1274 else
1275 sreg = INVALID_REGNUM;
1276 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1277 }
1278
1279 queued_reg_saves = NULL;
1280 last_reg_save_label = NULL;
1281 }
1282
1283 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1284 location for? Or, does it clobber a register which we've previously
1285 said that some other register is saved in, and for which we now
1286 have a new location for? */
1287
1288 static bool
1289 clobbers_queued_reg_save (const_rtx insn)
1290 {
1291 struct queued_reg_save *q;
1292
1293 for (q = queued_reg_saves; q; q = q->next)
1294 {
1295 size_t i;
1296 if (modified_in_p (q->reg, insn))
1297 return true;
1298 for (i = 0; i < num_regs_saved_in_regs; i++)
1299 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1300 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1301 return true;
1302 }
1303
1304 return false;
1305 }
1306
1307 /* Entry point for saving the first register into the second. */
1308
1309 void
1310 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1311 {
1312 size_t i;
1313 unsigned int regno, sregno;
1314
1315 for (i = 0; i < num_regs_saved_in_regs; i++)
1316 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1317 break;
1318 if (i == num_regs_saved_in_regs)
1319 {
1320 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1321 num_regs_saved_in_regs++;
1322 }
1323 regs_saved_in_regs[i].orig_reg = reg;
1324 regs_saved_in_regs[i].saved_in_reg = sreg;
1325
1326 regno = DWARF_FRAME_REGNUM (REGNO (reg));
1327 sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1328 reg_save (label, regno, sregno, 0);
1329 }
1330
1331 /* What register, if any, is currently saved in REG? */
1332
1333 static rtx
1334 reg_saved_in (rtx reg)
1335 {
1336 unsigned int regn = REGNO (reg);
1337 size_t i;
1338 struct queued_reg_save *q;
1339
1340 for (q = queued_reg_saves; q; q = q->next)
1341 if (q->saved_reg && regn == REGNO (q->saved_reg))
1342 return q->reg;
1343
1344 for (i = 0; i < num_regs_saved_in_regs; i++)
1345 if (regs_saved_in_regs[i].saved_in_reg
1346 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1347 return regs_saved_in_regs[i].orig_reg;
1348
1349 return NULL_RTX;
1350 }
1351
1352
1353 /* A temporary register holding an integral value used in adjusting SP
1354 or setting up the store_reg. The "offset" field holds the integer
1355 value, not an offset. */
1356 static dw_cfa_location cfa_temp;
1357
1358 /* Record call frame debugging information for an expression EXPR,
1359 which either sets SP or FP (adjusting how we calculate the frame
1360 address) or saves a register to the stack or another register.
1361 LABEL indicates the address of EXPR.
1362
1363 This function encodes a state machine mapping rtxes to actions on
1364 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1365 users need not read the source code.
1366
1367 The High-Level Picture
1368
1369 Changes in the register we use to calculate the CFA: Currently we
1370 assume that if you copy the CFA register into another register, we
1371 should take the other one as the new CFA register; this seems to
1372 work pretty well. If it's wrong for some target, it's simple
1373 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1374
1375 Changes in the register we use for saving registers to the stack:
1376 This is usually SP, but not always. Again, we deduce that if you
1377 copy SP into another register (and SP is not the CFA register),
1378 then the new register is the one we will be using for register
1379 saves. This also seems to work.
1380
1381 Register saves: There's not much guesswork about this one; if
1382 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1383 register save, and the register used to calculate the destination
1384 had better be the one we think we're using for this purpose.
1385 It's also assumed that a copy from a call-saved register to another
1386 register is saving that register if RTX_FRAME_RELATED_P is set on
1387 that instruction. If the copy is from a call-saved register to
1388 the *same* register, that means that the register is now the same
1389 value as in the caller.
1390
1391 Except: If the register being saved is the CFA register, and the
1392 offset is nonzero, we are saving the CFA, so we assume we have to
1393 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1394 the intent is to save the value of SP from the previous frame.
1395
1396 In addition, if a register has previously been saved to a different
1397 register,
1398
1399 Invariants / Summaries of Rules
1400
1401 cfa current rule for calculating the CFA. It usually
1402 consists of a register and an offset.
1403 cfa_store register used by prologue code to save things to the stack
1404 cfa_store.offset is the offset from the value of
1405 cfa_store.reg to the actual CFA
1406 cfa_temp register holding an integral value. cfa_temp.offset
1407 stores the value, which will be used to adjust the
1408 stack pointer. cfa_temp is also used like cfa_store,
1409 to track stores to the stack via fp or a temp reg.
1410
1411 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1412 with cfa.reg as the first operand changes the cfa.reg and its
1413 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1414 cfa_temp.offset.
1415
1416 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1417 expression yielding a constant. This sets cfa_temp.reg
1418 and cfa_temp.offset.
1419
1420 Rule 5: Create a new register cfa_store used to save items to the
1421 stack.
1422
1423 Rules 10-14: Save a register to the stack. Define offset as the
1424 difference of the original location and cfa_store's
1425 location (or cfa_temp's location if cfa_temp is used).
1426
1427 The Rules
1428
1429 "{a,b}" indicates a choice of a xor b.
1430 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1431
1432 Rule 1:
1433 (set <reg1> <reg2>:cfa.reg)
1434 effects: cfa.reg = <reg1>
1435 cfa.offset unchanged
1436 cfa_temp.reg = <reg1>
1437 cfa_temp.offset = cfa.offset
1438
1439 Rule 2:
1440 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1441 {<const_int>,<reg>:cfa_temp.reg}))
1442 effects: cfa.reg = sp if fp used
1443 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1444 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1445 if cfa_store.reg==sp
1446
1447 Rule 3:
1448 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1449 effects: cfa.reg = fp
1450 cfa_offset += +/- <const_int>
1451
1452 Rule 4:
1453 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1454 constraints: <reg1> != fp
1455 <reg1> != sp
1456 effects: cfa.reg = <reg1>
1457 cfa_temp.reg = <reg1>
1458 cfa_temp.offset = cfa.offset
1459
1460 Rule 5:
1461 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1462 constraints: <reg1> != fp
1463 <reg1> != sp
1464 effects: cfa_store.reg = <reg1>
1465 cfa_store.offset = cfa.offset - cfa_temp.offset
1466
1467 Rule 6:
1468 (set <reg> <const_int>)
1469 effects: cfa_temp.reg = <reg>
1470 cfa_temp.offset = <const_int>
1471
1472 Rule 7:
1473 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1474 effects: cfa_temp.reg = <reg1>
1475 cfa_temp.offset |= <const_int>
1476
1477 Rule 8:
1478 (set <reg> (high <exp>))
1479 effects: none
1480
1481 Rule 9:
1482 (set <reg> (lo_sum <exp> <const_int>))
1483 effects: cfa_temp.reg = <reg>
1484 cfa_temp.offset = <const_int>
1485
1486 Rule 10:
1487 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1488 effects: cfa_store.offset -= <const_int>
1489 cfa.offset = cfa_store.offset if cfa.reg == sp
1490 cfa.reg = sp
1491 cfa.base_offset = -cfa_store.offset
1492
1493 Rule 11:
1494 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1495 effects: cfa_store.offset += -/+ mode_size(mem)
1496 cfa.offset = cfa_store.offset if cfa.reg == sp
1497 cfa.reg = sp
1498 cfa.base_offset = -cfa_store.offset
1499
1500 Rule 12:
1501 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1502
1503 <reg2>)
1504 effects: cfa.reg = <reg1>
1505 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1506
1507 Rule 13:
1508 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1509 effects: cfa.reg = <reg1>
1510 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1511
1512 Rule 14:
1513 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1514 effects: cfa.reg = <reg1>
1515 cfa.base_offset = -cfa_temp.offset
1516 cfa_temp.offset -= mode_size(mem)
1517
1518 Rule 15:
1519 (set <reg> {unspec, unspec_volatile})
1520 effects: target-dependent */
1521
1522 static void
1523 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1524 {
1525 rtx src, dest;
1526 HOST_WIDE_INT offset;
1527
1528 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1529 the PARALLEL independently. The first element is always processed if
1530 it is a SET. This is for backward compatibility. Other elements
1531 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1532 flag is set in them. */
1533 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1534 {
1535 int par_index;
1536 int limit = XVECLEN (expr, 0);
1537 rtx elem;
1538
1539 /* PARALLELs have strict read-modify-write semantics, so we
1540 ought to evaluate every rvalue before changing any lvalue.
1541 It's cumbersome to do that in general, but there's an
1542 easy approximation that is enough for all current users:
1543 handle register saves before register assignments. */
1544 if (GET_CODE (expr) == PARALLEL)
1545 for (par_index = 0; par_index < limit; par_index++)
1546 {
1547 elem = XVECEXP (expr, 0, par_index);
1548 if (GET_CODE (elem) == SET
1549 && MEM_P (SET_DEST (elem))
1550 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1551 dwarf2out_frame_debug_expr (elem, label);
1552 }
1553
1554 for (par_index = 0; par_index < limit; par_index++)
1555 {
1556 elem = XVECEXP (expr, 0, par_index);
1557 if (GET_CODE (elem) == SET
1558 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1559 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1560 dwarf2out_frame_debug_expr (elem, label);
1561 }
1562 return;
1563 }
1564
1565 gcc_assert (GET_CODE (expr) == SET);
1566
1567 src = SET_SRC (expr);
1568 dest = SET_DEST (expr);
1569
1570 if (REG_P (src))
1571 {
1572 rtx rsi = reg_saved_in (src);
1573 if (rsi)
1574 src = rsi;
1575 }
1576
1577 switch (GET_CODE (dest))
1578 {
1579 case REG:
1580 switch (GET_CODE (src))
1581 {
1582 /* Setting FP from SP. */
1583 case REG:
1584 if (cfa.reg == (unsigned) REGNO (src))
1585 {
1586 /* Rule 1 */
1587 /* Update the CFA rule wrt SP or FP. Make sure src is
1588 relative to the current CFA register.
1589
1590 We used to require that dest be either SP or FP, but the
1591 ARM copies SP to a temporary register, and from there to
1592 FP. So we just rely on the backends to only set
1593 RTX_FRAME_RELATED_P on appropriate insns. */
1594 cfa.reg = REGNO (dest);
1595 cfa_temp.reg = cfa.reg;
1596 cfa_temp.offset = cfa.offset;
1597 }
1598 else
1599 {
1600 /* Saving a register in a register. */
1601 gcc_assert (!fixed_regs [REGNO (dest)]
1602 /* For the SPARC and its register window. */
1603 || (DWARF_FRAME_REGNUM (REGNO (src))
1604 == DWARF_FRAME_RETURN_COLUMN));
1605 queue_reg_save (label, src, dest, 0);
1606 }
1607 break;
1608
1609 case PLUS:
1610 case MINUS:
1611 case LO_SUM:
1612 if (dest == stack_pointer_rtx)
1613 {
1614 /* Rule 2 */
1615 /* Adjusting SP. */
1616 switch (GET_CODE (XEXP (src, 1)))
1617 {
1618 case CONST_INT:
1619 offset = INTVAL (XEXP (src, 1));
1620 break;
1621 case REG:
1622 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1623 == cfa_temp.reg);
1624 offset = cfa_temp.offset;
1625 break;
1626 default:
1627 gcc_unreachable ();
1628 }
1629
1630 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1631 {
1632 /* Restoring SP from FP in the epilogue. */
1633 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1634 cfa.reg = STACK_POINTER_REGNUM;
1635 }
1636 else if (GET_CODE (src) == LO_SUM)
1637 /* Assume we've set the source reg of the LO_SUM from sp. */
1638 ;
1639 else
1640 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1641
1642 if (GET_CODE (src) != MINUS)
1643 offset = -offset;
1644 if (cfa.reg == STACK_POINTER_REGNUM)
1645 cfa.offset += offset;
1646 if (cfa_store.reg == STACK_POINTER_REGNUM)
1647 cfa_store.offset += offset;
1648 }
1649 else if (dest == hard_frame_pointer_rtx)
1650 {
1651 /* Rule 3 */
1652 /* Either setting the FP from an offset of the SP,
1653 or adjusting the FP */
1654 gcc_assert (frame_pointer_needed);
1655
1656 gcc_assert (REG_P (XEXP (src, 0))
1657 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1658 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1659 offset = INTVAL (XEXP (src, 1));
1660 if (GET_CODE (src) != MINUS)
1661 offset = -offset;
1662 cfa.offset += offset;
1663 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1664 }
1665 else
1666 {
1667 gcc_assert (GET_CODE (src) != MINUS);
1668
1669 /* Rule 4 */
1670 if (REG_P (XEXP (src, 0))
1671 && REGNO (XEXP (src, 0)) == cfa.reg
1672 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1673 {
1674 /* Setting a temporary CFA register that will be copied
1675 into the FP later on. */
1676 offset = - INTVAL (XEXP (src, 1));
1677 cfa.offset += offset;
1678 cfa.reg = REGNO (dest);
1679 /* Or used to save regs to the stack. */
1680 cfa_temp.reg = cfa.reg;
1681 cfa_temp.offset = cfa.offset;
1682 }
1683
1684 /* Rule 5 */
1685 else if (REG_P (XEXP (src, 0))
1686 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1687 && XEXP (src, 1) == stack_pointer_rtx)
1688 {
1689 /* Setting a scratch register that we will use instead
1690 of SP for saving registers to the stack. */
1691 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1692 cfa_store.reg = REGNO (dest);
1693 cfa_store.offset = cfa.offset - cfa_temp.offset;
1694 }
1695
1696 /* Rule 9 */
1697 else if (GET_CODE (src) == LO_SUM
1698 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1699 {
1700 cfa_temp.reg = REGNO (dest);
1701 cfa_temp.offset = INTVAL (XEXP (src, 1));
1702 }
1703 else
1704 gcc_unreachable ();
1705 }
1706 break;
1707
1708 /* Rule 6 */
1709 case CONST_INT:
1710 cfa_temp.reg = REGNO (dest);
1711 cfa_temp.offset = INTVAL (src);
1712 break;
1713
1714 /* Rule 7 */
1715 case IOR:
1716 gcc_assert (REG_P (XEXP (src, 0))
1717 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1718 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1719
1720 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1721 cfa_temp.reg = REGNO (dest);
1722 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1723 break;
1724
1725 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1726 which will fill in all of the bits. */
1727 /* Rule 8 */
1728 case HIGH:
1729 break;
1730
1731 /* Rule 15 */
1732 case UNSPEC:
1733 case UNSPEC_VOLATILE:
1734 gcc_assert (targetm.dwarf_handle_frame_unspec);
1735 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1736 return;
1737
1738 default:
1739 gcc_unreachable ();
1740 }
1741
1742 def_cfa_1 (label, &cfa);
1743 break;
1744
1745 case MEM:
1746 gcc_assert (REG_P (src));
1747
1748 /* Saving a register to the stack. Make sure dest is relative to the
1749 CFA register. */
1750 switch (GET_CODE (XEXP (dest, 0)))
1751 {
1752 /* Rule 10 */
1753 /* With a push. */
1754 case PRE_MODIFY:
1755 /* We can't handle variable size modifications. */
1756 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1757 == CONST_INT);
1758 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1759
1760 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1761 && cfa_store.reg == STACK_POINTER_REGNUM);
1762
1763 cfa_store.offset += offset;
1764 if (cfa.reg == STACK_POINTER_REGNUM)
1765 cfa.offset = cfa_store.offset;
1766
1767 offset = -cfa_store.offset;
1768 break;
1769
1770 /* Rule 11 */
1771 case PRE_INC:
1772 case PRE_DEC:
1773 offset = GET_MODE_SIZE (GET_MODE (dest));
1774 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1775 offset = -offset;
1776
1777 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1778 && cfa_store.reg == STACK_POINTER_REGNUM);
1779
1780 cfa_store.offset += offset;
1781 if (cfa.reg == STACK_POINTER_REGNUM)
1782 cfa.offset = cfa_store.offset;
1783
1784 offset = -cfa_store.offset;
1785 break;
1786
1787 /* Rule 12 */
1788 /* With an offset. */
1789 case PLUS:
1790 case MINUS:
1791 case LO_SUM:
1792 {
1793 int regno;
1794
1795 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1796 && REG_P (XEXP (XEXP (dest, 0), 0)));
1797 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1798 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1799 offset = -offset;
1800
1801 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1802
1803 if (cfa_store.reg == (unsigned) regno)
1804 offset -= cfa_store.offset;
1805 else
1806 {
1807 gcc_assert (cfa_temp.reg == (unsigned) regno);
1808 offset -= cfa_temp.offset;
1809 }
1810 }
1811 break;
1812
1813 /* Rule 13 */
1814 /* Without an offset. */
1815 case REG:
1816 {
1817 int regno = REGNO (XEXP (dest, 0));
1818
1819 if (cfa_store.reg == (unsigned) regno)
1820 offset = -cfa_store.offset;
1821 else
1822 {
1823 gcc_assert (cfa_temp.reg == (unsigned) regno);
1824 offset = -cfa_temp.offset;
1825 }
1826 }
1827 break;
1828
1829 /* Rule 14 */
1830 case POST_INC:
1831 gcc_assert (cfa_temp.reg
1832 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1833 offset = -cfa_temp.offset;
1834 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1835 break;
1836
1837 default:
1838 gcc_unreachable ();
1839 }
1840
1841 if (REGNO (src) != STACK_POINTER_REGNUM
1842 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1843 && (unsigned) REGNO (src) == cfa.reg)
1844 {
1845 /* We're storing the current CFA reg into the stack. */
1846
1847 if (cfa.offset == 0)
1848 {
1849 /* If the source register is exactly the CFA, assume
1850 we're saving SP like any other register; this happens
1851 on the ARM. */
1852 def_cfa_1 (label, &cfa);
1853 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1854 break;
1855 }
1856 else
1857 {
1858 /* Otherwise, we'll need to look in the stack to
1859 calculate the CFA. */
1860 rtx x = XEXP (dest, 0);
1861
1862 if (!REG_P (x))
1863 x = XEXP (x, 0);
1864 gcc_assert (REG_P (x));
1865
1866 cfa.reg = REGNO (x);
1867 cfa.base_offset = offset;
1868 cfa.indirect = 1;
1869 def_cfa_1 (label, &cfa);
1870 break;
1871 }
1872 }
1873
1874 def_cfa_1 (label, &cfa);
1875 queue_reg_save (label, src, NULL_RTX, offset);
1876 break;
1877
1878 default:
1879 gcc_unreachable ();
1880 }
1881 }
1882
1883 /* Record call frame debugging information for INSN, which either
1884 sets SP or FP (adjusting how we calculate the frame address) or saves a
1885 register to the stack. If INSN is NULL_RTX, initialize our state.
1886
1887 If AFTER_P is false, we're being called before the insn is emitted,
1888 otherwise after. Call instructions get invoked twice. */
1889
1890 void
1891 dwarf2out_frame_debug (rtx insn, bool after_p)
1892 {
1893 const char *label;
1894 rtx src;
1895
1896 if (insn == NULL_RTX)
1897 {
1898 size_t i;
1899
1900 /* Flush any queued register saves. */
1901 flush_queued_reg_saves ();
1902
1903 /* Set up state for generating call frame debug info. */
1904 lookup_cfa (&cfa);
1905 gcc_assert (cfa.reg
1906 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1907
1908 cfa.reg = STACK_POINTER_REGNUM;
1909 cfa_store = cfa;
1910 cfa_temp.reg = -1;
1911 cfa_temp.offset = 0;
1912
1913 for (i = 0; i < num_regs_saved_in_regs; i++)
1914 {
1915 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1916 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1917 }
1918 num_regs_saved_in_regs = 0;
1919 return;
1920 }
1921
1922 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1923 flush_queued_reg_saves ();
1924
1925 if (! RTX_FRAME_RELATED_P (insn))
1926 {
1927 if (!ACCUMULATE_OUTGOING_ARGS)
1928 dwarf2out_stack_adjust (insn, after_p);
1929 return;
1930 }
1931
1932 label = dwarf2out_cfi_label ();
1933 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1934 if (src)
1935 insn = XEXP (src, 0);
1936 else
1937 insn = PATTERN (insn);
1938
1939 dwarf2out_frame_debug_expr (insn, label);
1940 }
1941
1942 #endif
1943
1944 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1945 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1946 (enum dwarf_call_frame_info cfi);
1947
1948 static enum dw_cfi_oprnd_type
1949 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1950 {
1951 switch (cfi)
1952 {
1953 case DW_CFA_nop:
1954 case DW_CFA_GNU_window_save:
1955 return dw_cfi_oprnd_unused;
1956
1957 case DW_CFA_set_loc:
1958 case DW_CFA_advance_loc1:
1959 case DW_CFA_advance_loc2:
1960 case DW_CFA_advance_loc4:
1961 case DW_CFA_MIPS_advance_loc8:
1962 return dw_cfi_oprnd_addr;
1963
1964 case DW_CFA_offset:
1965 case DW_CFA_offset_extended:
1966 case DW_CFA_def_cfa:
1967 case DW_CFA_offset_extended_sf:
1968 case DW_CFA_def_cfa_sf:
1969 case DW_CFA_restore_extended:
1970 case DW_CFA_undefined:
1971 case DW_CFA_same_value:
1972 case DW_CFA_def_cfa_register:
1973 case DW_CFA_register:
1974 return dw_cfi_oprnd_reg_num;
1975
1976 case DW_CFA_def_cfa_offset:
1977 case DW_CFA_GNU_args_size:
1978 case DW_CFA_def_cfa_offset_sf:
1979 return dw_cfi_oprnd_offset;
1980
1981 case DW_CFA_def_cfa_expression:
1982 case DW_CFA_expression:
1983 return dw_cfi_oprnd_loc;
1984
1985 default:
1986 gcc_unreachable ();
1987 }
1988 }
1989
1990 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1991 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1992 (enum dwarf_call_frame_info cfi);
1993
1994 static enum dw_cfi_oprnd_type
1995 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1996 {
1997 switch (cfi)
1998 {
1999 case DW_CFA_def_cfa:
2000 case DW_CFA_def_cfa_sf:
2001 case DW_CFA_offset:
2002 case DW_CFA_offset_extended_sf:
2003 case DW_CFA_offset_extended:
2004 return dw_cfi_oprnd_offset;
2005
2006 case DW_CFA_register:
2007 return dw_cfi_oprnd_reg_num;
2008
2009 default:
2010 return dw_cfi_oprnd_unused;
2011 }
2012 }
2013
2014 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2015
2016 /* Switch to eh_frame_section. If we don't have an eh_frame_section,
2017 switch to the data section instead, and write out a synthetic label
2018 for collect2. */
2019
2020 static void
2021 switch_to_eh_frame_section (void)
2022 {
2023 tree label;
2024
2025 #ifdef EH_FRAME_SECTION_NAME
2026 if (eh_frame_section == 0)
2027 {
2028 int flags;
2029
2030 if (EH_TABLES_CAN_BE_READ_ONLY)
2031 {
2032 int fde_encoding;
2033 int per_encoding;
2034 int lsda_encoding;
2035
2036 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2037 /*global=*/0);
2038 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2039 /*global=*/1);
2040 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2041 /*global=*/0);
2042 flags = ((! flag_pic
2043 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2044 && (fde_encoding & 0x70) != DW_EH_PE_aligned
2045 && (per_encoding & 0x70) != DW_EH_PE_absptr
2046 && (per_encoding & 0x70) != DW_EH_PE_aligned
2047 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2048 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2049 ? 0 : SECTION_WRITE);
2050 }
2051 else
2052 flags = SECTION_WRITE;
2053 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2054 }
2055 #endif
2056
2057 if (eh_frame_section)
2058 switch_to_section (eh_frame_section);
2059 else
2060 {
2061 /* We have no special eh_frame section. Put the information in
2062 the data section and emit special labels to guide collect2. */
2063 switch_to_section (data_section);
2064 label = get_file_function_name ("F");
2065 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2066 targetm.asm_out.globalize_label (asm_out_file,
2067 IDENTIFIER_POINTER (label));
2068 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2069 }
2070 }
2071
2072 /* Output a Call Frame Information opcode and its operand(s). */
2073
2074 static void
2075 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2076 {
2077 unsigned long r;
2078 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2079 dw2_asm_output_data (1, (cfi->dw_cfi_opc
2080 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2081 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2082 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2083 else if (cfi->dw_cfi_opc == DW_CFA_offset)
2084 {
2085 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2086 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2087 "DW_CFA_offset, column 0x%lx", r);
2088 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2089 }
2090 else if (cfi->dw_cfi_opc == DW_CFA_restore)
2091 {
2092 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2093 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2094 "DW_CFA_restore, column 0x%lx", r);
2095 }
2096 else
2097 {
2098 dw2_asm_output_data (1, cfi->dw_cfi_opc,
2099 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2100
2101 switch (cfi->dw_cfi_opc)
2102 {
2103 case DW_CFA_set_loc:
2104 if (for_eh)
2105 dw2_asm_output_encoded_addr_rtx (
2106 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2107 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2108 false, NULL);
2109 else
2110 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2111 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2112 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2113 break;
2114
2115 case DW_CFA_advance_loc1:
2116 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2117 fde->dw_fde_current_label, NULL);
2118 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2119 break;
2120
2121 case DW_CFA_advance_loc2:
2122 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2123 fde->dw_fde_current_label, NULL);
2124 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2125 break;
2126
2127 case DW_CFA_advance_loc4:
2128 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2129 fde->dw_fde_current_label, NULL);
2130 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2131 break;
2132
2133 case DW_CFA_MIPS_advance_loc8:
2134 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2135 fde->dw_fde_current_label, NULL);
2136 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2137 break;
2138
2139 case DW_CFA_offset_extended:
2140 case DW_CFA_def_cfa:
2141 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2142 dw2_asm_output_data_uleb128 (r, NULL);
2143 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2144 break;
2145
2146 case DW_CFA_offset_extended_sf:
2147 case DW_CFA_def_cfa_sf:
2148 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2149 dw2_asm_output_data_uleb128 (r, NULL);
2150 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2151 break;
2152
2153 case DW_CFA_restore_extended:
2154 case DW_CFA_undefined:
2155 case DW_CFA_same_value:
2156 case DW_CFA_def_cfa_register:
2157 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2158 dw2_asm_output_data_uleb128 (r, NULL);
2159 break;
2160
2161 case DW_CFA_register:
2162 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2163 dw2_asm_output_data_uleb128 (r, NULL);
2164 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2165 dw2_asm_output_data_uleb128 (r, NULL);
2166 break;
2167
2168 case DW_CFA_def_cfa_offset:
2169 case DW_CFA_GNU_args_size:
2170 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2171 break;
2172
2173 case DW_CFA_def_cfa_offset_sf:
2174 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2175 break;
2176
2177 case DW_CFA_GNU_window_save:
2178 break;
2179
2180 case DW_CFA_def_cfa_expression:
2181 case DW_CFA_expression:
2182 output_cfa_loc (cfi);
2183 break;
2184
2185 case DW_CFA_GNU_negative_offset_extended:
2186 /* Obsoleted by DW_CFA_offset_extended_sf. */
2187 gcc_unreachable ();
2188
2189 default:
2190 break;
2191 }
2192 }
2193 }
2194
2195 /* Output the call frame information used to record information
2196 that relates to calculating the frame pointer, and records the
2197 location of saved registers. */
2198
2199 static void
2200 output_call_frame_info (int for_eh)
2201 {
2202 unsigned int i;
2203 dw_fde_ref fde;
2204 dw_cfi_ref cfi;
2205 char l1[20], l2[20], section_start_label[20];
2206 bool any_lsda_needed = false;
2207 char augmentation[6];
2208 int augmentation_size;
2209 int fde_encoding = DW_EH_PE_absptr;
2210 int per_encoding = DW_EH_PE_absptr;
2211 int lsda_encoding = DW_EH_PE_absptr;
2212 int return_reg;
2213
2214 /* Don't emit a CIE if there won't be any FDEs. */
2215 if (fde_table_in_use == 0)
2216 return;
2217
2218 /* If we make FDEs linkonce, we may have to emit an empty label for
2219 an FDE that wouldn't otherwise be emitted. We want to avoid
2220 having an FDE kept around when the function it refers to is
2221 discarded. Example where this matters: a primary function
2222 template in C++ requires EH information, but an explicit
2223 specialization doesn't. */
2224 if (TARGET_USES_WEAK_UNWIND_INFO
2225 && ! flag_asynchronous_unwind_tables
2226 && flag_exceptions
2227 && for_eh)
2228 for (i = 0; i < fde_table_in_use; i++)
2229 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2230 && !fde_table[i].uses_eh_lsda
2231 && ! DECL_WEAK (fde_table[i].decl))
2232 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2233 for_eh, /* empty */ 1);
2234
2235 /* If we don't have any functions we'll want to unwind out of, don't
2236 emit any EH unwind information. Note that if exceptions aren't
2237 enabled, we won't have collected nothrow information, and if we
2238 asked for asynchronous tables, we always want this info. */
2239 if (for_eh)
2240 {
2241 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2242
2243 for (i = 0; i < fde_table_in_use; i++)
2244 if (fde_table[i].uses_eh_lsda)
2245 any_eh_needed = any_lsda_needed = true;
2246 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2247 any_eh_needed = true;
2248 else if (! fde_table[i].nothrow
2249 && ! fde_table[i].all_throwers_are_sibcalls)
2250 any_eh_needed = true;
2251
2252 if (! any_eh_needed)
2253 return;
2254 }
2255
2256 /* We're going to be generating comments, so turn on app. */
2257 if (flag_debug_asm)
2258 app_enable ();
2259
2260 if (for_eh)
2261 switch_to_eh_frame_section ();
2262 else
2263 {
2264 if (!debug_frame_section)
2265 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2266 SECTION_DEBUG, NULL);
2267 switch_to_section (debug_frame_section);
2268 }
2269
2270 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2271 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2272
2273 /* Output the CIE. */
2274 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2275 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2276 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2277 dw2_asm_output_data (4, 0xffffffff,
2278 "Initial length escape value indicating 64-bit DWARF extension");
2279 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2280 "Length of Common Information Entry");
2281 ASM_OUTPUT_LABEL (asm_out_file, l1);
2282
2283 /* Now that the CIE pointer is PC-relative for EH,
2284 use 0 to identify the CIE. */
2285 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2286 (for_eh ? 0 : DWARF_CIE_ID),
2287 "CIE Identifier Tag");
2288
2289 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2290
2291 augmentation[0] = 0;
2292 augmentation_size = 0;
2293 if (for_eh)
2294 {
2295 char *p;
2296
2297 /* Augmentation:
2298 z Indicates that a uleb128 is present to size the
2299 augmentation section.
2300 L Indicates the encoding (and thus presence) of
2301 an LSDA pointer in the FDE augmentation.
2302 R Indicates a non-default pointer encoding for
2303 FDE code pointers.
2304 P Indicates the presence of an encoding + language
2305 personality routine in the CIE augmentation. */
2306
2307 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2308 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2309 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2310
2311 p = augmentation + 1;
2312 if (eh_personality_libfunc)
2313 {
2314 *p++ = 'P';
2315 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2316 assemble_external_libcall (eh_personality_libfunc);
2317 }
2318 if (any_lsda_needed)
2319 {
2320 *p++ = 'L';
2321 augmentation_size += 1;
2322 }
2323 if (fde_encoding != DW_EH_PE_absptr)
2324 {
2325 *p++ = 'R';
2326 augmentation_size += 1;
2327 }
2328 if (p > augmentation + 1)
2329 {
2330 augmentation[0] = 'z';
2331 *p = '\0';
2332 }
2333
2334 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2335 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2336 {
2337 int offset = ( 4 /* Length */
2338 + 4 /* CIE Id */
2339 + 1 /* CIE version */
2340 + strlen (augmentation) + 1 /* Augmentation */
2341 + size_of_uleb128 (1) /* Code alignment */
2342 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2343 + 1 /* RA column */
2344 + 1 /* Augmentation size */
2345 + 1 /* Personality encoding */ );
2346 int pad = -offset & (PTR_SIZE - 1);
2347
2348 augmentation_size += pad;
2349
2350 /* Augmentations should be small, so there's scarce need to
2351 iterate for a solution. Die if we exceed one uleb128 byte. */
2352 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2353 }
2354 }
2355
2356 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2357 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2358 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2359 "CIE Data Alignment Factor");
2360
2361 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2362 if (DW_CIE_VERSION == 1)
2363 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2364 else
2365 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2366
2367 if (augmentation[0])
2368 {
2369 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2370 if (eh_personality_libfunc)
2371 {
2372 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2373 eh_data_format_name (per_encoding));
2374 dw2_asm_output_encoded_addr_rtx (per_encoding,
2375 eh_personality_libfunc,
2376 true, NULL);
2377 }
2378
2379 if (any_lsda_needed)
2380 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2381 eh_data_format_name (lsda_encoding));
2382
2383 if (fde_encoding != DW_EH_PE_absptr)
2384 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2385 eh_data_format_name (fde_encoding));
2386 }
2387
2388 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2389 output_cfi (cfi, NULL, for_eh);
2390
2391 /* Pad the CIE out to an address sized boundary. */
2392 ASM_OUTPUT_ALIGN (asm_out_file,
2393 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2394 ASM_OUTPUT_LABEL (asm_out_file, l2);
2395
2396 /* Loop through all of the FDE's. */
2397 for (i = 0; i < fde_table_in_use; i++)
2398 {
2399 fde = &fde_table[i];
2400
2401 /* Don't emit EH unwind info for leaf functions that don't need it. */
2402 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2403 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2404 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2405 && !fde->uses_eh_lsda)
2406 continue;
2407
2408 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2409 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2410 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2411 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2412 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2413 dw2_asm_output_data (4, 0xffffffff,
2414 "Initial length escape value indicating 64-bit DWARF extension");
2415 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2416 "FDE Length");
2417 ASM_OUTPUT_LABEL (asm_out_file, l1);
2418
2419 if (for_eh)
2420 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2421 else
2422 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2423 debug_frame_section, "FDE CIE offset");
2424
2425 if (for_eh)
2426 {
2427 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2428 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2429 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2430 sym_ref,
2431 false,
2432 "FDE initial location");
2433 if (fde->dw_fde_switched_sections)
2434 {
2435 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2436 fde->dw_fde_unlikely_section_label);
2437 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2438 fde->dw_fde_hot_section_label);
2439 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2440 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2441 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2442 "FDE initial location");
2443 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2444 fde->dw_fde_hot_section_end_label,
2445 fde->dw_fde_hot_section_label,
2446 "FDE address range");
2447 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2448 "FDE initial location");
2449 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2450 fde->dw_fde_unlikely_section_end_label,
2451 fde->dw_fde_unlikely_section_label,
2452 "FDE address range");
2453 }
2454 else
2455 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2456 fde->dw_fde_end, fde->dw_fde_begin,
2457 "FDE address range");
2458 }
2459 else
2460 {
2461 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2462 "FDE initial location");
2463 if (fde->dw_fde_switched_sections)
2464 {
2465 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2466 fde->dw_fde_hot_section_label,
2467 "FDE initial location");
2468 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2469 fde->dw_fde_hot_section_end_label,
2470 fde->dw_fde_hot_section_label,
2471 "FDE address range");
2472 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2473 fde->dw_fde_unlikely_section_label,
2474 "FDE initial location");
2475 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2476 fde->dw_fde_unlikely_section_end_label,
2477 fde->dw_fde_unlikely_section_label,
2478 "FDE address range");
2479 }
2480 else
2481 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2482 fde->dw_fde_end, fde->dw_fde_begin,
2483 "FDE address range");
2484 }
2485
2486 if (augmentation[0])
2487 {
2488 if (any_lsda_needed)
2489 {
2490 int size = size_of_encoded_value (lsda_encoding);
2491
2492 if (lsda_encoding == DW_EH_PE_aligned)
2493 {
2494 int offset = ( 4 /* Length */
2495 + 4 /* CIE offset */
2496 + 2 * size_of_encoded_value (fde_encoding)
2497 + 1 /* Augmentation size */ );
2498 int pad = -offset & (PTR_SIZE - 1);
2499
2500 size += pad;
2501 gcc_assert (size_of_uleb128 (size) == 1);
2502 }
2503
2504 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2505
2506 if (fde->uses_eh_lsda)
2507 {
2508 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2509 fde->funcdef_number);
2510 dw2_asm_output_encoded_addr_rtx (
2511 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2512 false, "Language Specific Data Area");
2513 }
2514 else
2515 {
2516 if (lsda_encoding == DW_EH_PE_aligned)
2517 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2518 dw2_asm_output_data
2519 (size_of_encoded_value (lsda_encoding), 0,
2520 "Language Specific Data Area (none)");
2521 }
2522 }
2523 else
2524 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2525 }
2526
2527 /* Loop through the Call Frame Instructions associated with
2528 this FDE. */
2529 fde->dw_fde_current_label = fde->dw_fde_begin;
2530 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2531 output_cfi (cfi, fde, for_eh);
2532
2533 /* Pad the FDE out to an address sized boundary. */
2534 ASM_OUTPUT_ALIGN (asm_out_file,
2535 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2536 ASM_OUTPUT_LABEL (asm_out_file, l2);
2537 }
2538
2539 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2540 dw2_asm_output_data (4, 0, "End of Table");
2541 #ifdef MIPS_DEBUGGING_INFO
2542 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2543 get a value of 0. Putting .align 0 after the label fixes it. */
2544 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2545 #endif
2546
2547 /* Turn off app to make assembly quicker. */
2548 if (flag_debug_asm)
2549 app_disable ();
2550 }
2551
2552 /* Output a marker (i.e. a label) for the beginning of a function, before
2553 the prologue. */
2554
2555 void
2556 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2557 const char *file ATTRIBUTE_UNUSED)
2558 {
2559 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2560 char * dup_label;
2561 dw_fde_ref fde;
2562
2563 current_function_func_begin_label = NULL;
2564
2565 #ifdef TARGET_UNWIND_INFO
2566 /* ??? current_function_func_begin_label is also used by except.c
2567 for call-site information. We must emit this label if it might
2568 be used. */
2569 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2570 && ! dwarf2out_do_frame ())
2571 return;
2572 #else
2573 if (! dwarf2out_do_frame ())
2574 return;
2575 #endif
2576
2577 switch_to_section (function_section (current_function_decl));
2578 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2579 current_function_funcdef_no);
2580 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2581 current_function_funcdef_no);
2582 dup_label = xstrdup (label);
2583 current_function_func_begin_label = dup_label;
2584
2585 #ifdef TARGET_UNWIND_INFO
2586 /* We can elide the fde allocation if we're not emitting debug info. */
2587 if (! dwarf2out_do_frame ())
2588 return;
2589 #endif
2590
2591 /* Expand the fde table if necessary. */
2592 if (fde_table_in_use == fde_table_allocated)
2593 {
2594 fde_table_allocated += FDE_TABLE_INCREMENT;
2595 fde_table = ggc_realloc (fde_table,
2596 fde_table_allocated * sizeof (dw_fde_node));
2597 memset (fde_table + fde_table_in_use, 0,
2598 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2599 }
2600
2601 /* Record the FDE associated with this function. */
2602 current_funcdef_fde = fde_table_in_use;
2603
2604 /* Add the new FDE at the end of the fde_table. */
2605 fde = &fde_table[fde_table_in_use++];
2606 fde->decl = current_function_decl;
2607 fde->dw_fde_begin = dup_label;
2608 fde->dw_fde_current_label = dup_label;
2609 fde->dw_fde_hot_section_label = NULL;
2610 fde->dw_fde_hot_section_end_label = NULL;
2611 fde->dw_fde_unlikely_section_label = NULL;
2612 fde->dw_fde_unlikely_section_end_label = NULL;
2613 fde->dw_fde_switched_sections = false;
2614 fde->dw_fde_end = NULL;
2615 fde->dw_fde_cfi = NULL;
2616 fde->funcdef_number = current_function_funcdef_no;
2617 fde->nothrow = TREE_NOTHROW (current_function_decl);
2618 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2619 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2620
2621 args_size = old_args_size = 0;
2622
2623 /* We only want to output line number information for the genuine dwarf2
2624 prologue case, not the eh frame case. */
2625 #ifdef DWARF2_DEBUGGING_INFO
2626 if (file)
2627 dwarf2out_source_line (line, file);
2628 #endif
2629 }
2630
2631 /* Output a marker (i.e. a label) for the absolute end of the generated code
2632 for a function definition. This gets called *after* the epilogue code has
2633 been generated. */
2634
2635 void
2636 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2637 const char *file ATTRIBUTE_UNUSED)
2638 {
2639 dw_fde_ref fde;
2640 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2641
2642 /* Output a label to mark the endpoint of the code generated for this
2643 function. */
2644 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2645 current_function_funcdef_no);
2646 ASM_OUTPUT_LABEL (asm_out_file, label);
2647 fde = &fde_table[fde_table_in_use - 1];
2648 fde->dw_fde_end = xstrdup (label);
2649 }
2650
2651 void
2652 dwarf2out_frame_init (void)
2653 {
2654 /* Allocate the initial hunk of the fde_table. */
2655 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2656 fde_table_allocated = FDE_TABLE_INCREMENT;
2657 fde_table_in_use = 0;
2658
2659 /* Generate the CFA instructions common to all FDE's. Do it now for the
2660 sake of lookup_cfa. */
2661
2662 /* On entry, the Canonical Frame Address is at SP. */
2663 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2664
2665 #ifdef DWARF2_UNWIND_INFO
2666 if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2667 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2668 #endif
2669 }
2670
2671 void
2672 dwarf2out_frame_finish (void)
2673 {
2674 /* Output call frame information. */
2675 if (DWARF2_FRAME_INFO)
2676 output_call_frame_info (0);
2677
2678 #ifndef TARGET_UNWIND_INFO
2679 /* Output another copy for the unwinder. */
2680 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2681 output_call_frame_info (1);
2682 #endif
2683 }
2684 #endif
2685 \f
2686 /* And now, the subset of the debugging information support code necessary
2687 for emitting location expressions. */
2688
2689 /* Data about a single source file. */
2690 struct dwarf_file_data GTY(())
2691 {
2692 const char * filename;
2693 int emitted_number;
2694 };
2695
2696 /* We need some way to distinguish DW_OP_addr with a direct symbol
2697 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2698 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2699
2700
2701 typedef struct dw_val_struct *dw_val_ref;
2702 typedef struct die_struct *dw_die_ref;
2703 typedef const struct die_struct *const_dw_die_ref;
2704 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2705 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2706
2707 /* Each DIE may have a series of attribute/value pairs. Values
2708 can take on several forms. The forms that are used in this
2709 implementation are listed below. */
2710
2711 enum dw_val_class
2712 {
2713 dw_val_class_addr,
2714 dw_val_class_offset,
2715 dw_val_class_loc,
2716 dw_val_class_loc_list,
2717 dw_val_class_range_list,
2718 dw_val_class_const,
2719 dw_val_class_unsigned_const,
2720 dw_val_class_long_long,
2721 dw_val_class_vec,
2722 dw_val_class_flag,
2723 dw_val_class_die_ref,
2724 dw_val_class_fde_ref,
2725 dw_val_class_lbl_id,
2726 dw_val_class_lineptr,
2727 dw_val_class_str,
2728 dw_val_class_macptr,
2729 dw_val_class_file
2730 };
2731
2732 /* Describe a double word constant value. */
2733 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2734
2735 typedef struct dw_long_long_struct GTY(())
2736 {
2737 unsigned long hi;
2738 unsigned long low;
2739 }
2740 dw_long_long_const;
2741
2742 /* Describe a floating point constant value, or a vector constant value. */
2743
2744 typedef struct dw_vec_struct GTY(())
2745 {
2746 unsigned char * GTY((length ("%h.length"))) array;
2747 unsigned length;
2748 unsigned elt_size;
2749 }
2750 dw_vec_const;
2751
2752 /* The dw_val_node describes an attribute's value, as it is
2753 represented internally. */
2754
2755 typedef struct dw_val_struct GTY(())
2756 {
2757 enum dw_val_class val_class;
2758 union dw_val_struct_union
2759 {
2760 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2761 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2762 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2763 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2764 HOST_WIDE_INT GTY ((default)) val_int;
2765 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2766 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2767 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2768 struct dw_val_die_union
2769 {
2770 dw_die_ref die;
2771 int external;
2772 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2773 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2774 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2775 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2776 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2777 struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2778 }
2779 GTY ((desc ("%1.val_class"))) v;
2780 }
2781 dw_val_node;
2782
2783 /* Locations in memory are described using a sequence of stack machine
2784 operations. */
2785
2786 typedef struct dw_loc_descr_struct GTY(())
2787 {
2788 dw_loc_descr_ref dw_loc_next;
2789 enum dwarf_location_atom dw_loc_opc;
2790 dw_val_node dw_loc_oprnd1;
2791 dw_val_node dw_loc_oprnd2;
2792 int dw_loc_addr;
2793 }
2794 dw_loc_descr_node;
2795
2796 /* Location lists are ranges + location descriptions for that range,
2797 so you can track variables that are in different places over
2798 their entire life. */
2799 typedef struct dw_loc_list_struct GTY(())
2800 {
2801 dw_loc_list_ref dw_loc_next;
2802 const char *begin; /* Label for begin address of range */
2803 const char *end; /* Label for end address of range */
2804 char *ll_symbol; /* Label for beginning of location list.
2805 Only on head of list */
2806 const char *section; /* Section this loclist is relative to */
2807 dw_loc_descr_ref expr;
2808 } dw_loc_list_node;
2809
2810 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2811
2812 static const char *dwarf_stack_op_name (unsigned);
2813 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2814 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2815 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2816 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2817 static unsigned long size_of_locs (dw_loc_descr_ref);
2818 static void output_loc_operands (dw_loc_descr_ref);
2819 static void output_loc_sequence (dw_loc_descr_ref);
2820
2821 /* Convert a DWARF stack opcode into its string name. */
2822
2823 static const char *
2824 dwarf_stack_op_name (unsigned int op)
2825 {
2826 switch (op)
2827 {
2828 case DW_OP_addr:
2829 case INTERNAL_DW_OP_tls_addr:
2830 return "DW_OP_addr";
2831 case DW_OP_deref:
2832 return "DW_OP_deref";
2833 case DW_OP_const1u:
2834 return "DW_OP_const1u";
2835 case DW_OP_const1s:
2836 return "DW_OP_const1s";
2837 case DW_OP_const2u:
2838 return "DW_OP_const2u";
2839 case DW_OP_const2s:
2840 return "DW_OP_const2s";
2841 case DW_OP_const4u:
2842 return "DW_OP_const4u";
2843 case DW_OP_const4s:
2844 return "DW_OP_const4s";
2845 case DW_OP_const8u:
2846 return "DW_OP_const8u";
2847 case DW_OP_const8s:
2848 return "DW_OP_const8s";
2849 case DW_OP_constu:
2850 return "DW_OP_constu";
2851 case DW_OP_consts:
2852 return "DW_OP_consts";
2853 case DW_OP_dup:
2854 return "DW_OP_dup";
2855 case DW_OP_drop:
2856 return "DW_OP_drop";
2857 case DW_OP_over:
2858 return "DW_OP_over";
2859 case DW_OP_pick:
2860 return "DW_OP_pick";
2861 case DW_OP_swap:
2862 return "DW_OP_swap";
2863 case DW_OP_rot:
2864 return "DW_OP_rot";
2865 case DW_OP_xderef:
2866 return "DW_OP_xderef";
2867 case DW_OP_abs:
2868 return "DW_OP_abs";
2869 case DW_OP_and:
2870 return "DW_OP_and";
2871 case DW_OP_div:
2872 return "DW_OP_div";
2873 case DW_OP_minus:
2874 return "DW_OP_minus";
2875 case DW_OP_mod:
2876 return "DW_OP_mod";
2877 case DW_OP_mul:
2878 return "DW_OP_mul";
2879 case DW_OP_neg:
2880 return "DW_OP_neg";
2881 case DW_OP_not:
2882 return "DW_OP_not";
2883 case DW_OP_or:
2884 return "DW_OP_or";
2885 case DW_OP_plus:
2886 return "DW_OP_plus";
2887 case DW_OP_plus_uconst:
2888 return "DW_OP_plus_uconst";
2889 case DW_OP_shl:
2890 return "DW_OP_shl";
2891 case DW_OP_shr:
2892 return "DW_OP_shr";
2893 case DW_OP_shra:
2894 return "DW_OP_shra";
2895 case DW_OP_xor:
2896 return "DW_OP_xor";
2897 case DW_OP_bra:
2898 return "DW_OP_bra";
2899 case DW_OP_eq:
2900 return "DW_OP_eq";
2901 case DW_OP_ge:
2902 return "DW_OP_ge";
2903 case DW_OP_gt:
2904 return "DW_OP_gt";
2905 case DW_OP_le:
2906 return "DW_OP_le";
2907 case DW_OP_lt:
2908 return "DW_OP_lt";
2909 case DW_OP_ne:
2910 return "DW_OP_ne";
2911 case DW_OP_skip:
2912 return "DW_OP_skip";
2913 case DW_OP_lit0:
2914 return "DW_OP_lit0";
2915 case DW_OP_lit1:
2916 return "DW_OP_lit1";
2917 case DW_OP_lit2:
2918 return "DW_OP_lit2";
2919 case DW_OP_lit3:
2920 return "DW_OP_lit3";
2921 case DW_OP_lit4:
2922 return "DW_OP_lit4";
2923 case DW_OP_lit5:
2924 return "DW_OP_lit5";
2925 case DW_OP_lit6:
2926 return "DW_OP_lit6";
2927 case DW_OP_lit7:
2928 return "DW_OP_lit7";
2929 case DW_OP_lit8:
2930 return "DW_OP_lit8";
2931 case DW_OP_lit9:
2932 return "DW_OP_lit9";
2933 case DW_OP_lit10:
2934 return "DW_OP_lit10";
2935 case DW_OP_lit11:
2936 return "DW_OP_lit11";
2937 case DW_OP_lit12:
2938 return "DW_OP_lit12";
2939 case DW_OP_lit13:
2940 return "DW_OP_lit13";
2941 case DW_OP_lit14:
2942 return "DW_OP_lit14";
2943 case DW_OP_lit15:
2944 return "DW_OP_lit15";
2945 case DW_OP_lit16:
2946 return "DW_OP_lit16";
2947 case DW_OP_lit17:
2948 return "DW_OP_lit17";
2949 case DW_OP_lit18:
2950 return "DW_OP_lit18";
2951 case DW_OP_lit19:
2952 return "DW_OP_lit19";
2953 case DW_OP_lit20:
2954 return "DW_OP_lit20";
2955 case DW_OP_lit21:
2956 return "DW_OP_lit21";
2957 case DW_OP_lit22:
2958 return "DW_OP_lit22";
2959 case DW_OP_lit23:
2960 return "DW_OP_lit23";
2961 case DW_OP_lit24:
2962 return "DW_OP_lit24";
2963 case DW_OP_lit25:
2964 return "DW_OP_lit25";
2965 case DW_OP_lit26:
2966 return "DW_OP_lit26";
2967 case DW_OP_lit27:
2968 return "DW_OP_lit27";
2969 case DW_OP_lit28:
2970 return "DW_OP_lit28";
2971 case DW_OP_lit29:
2972 return "DW_OP_lit29";
2973 case DW_OP_lit30:
2974 return "DW_OP_lit30";
2975 case DW_OP_lit31:
2976 return "DW_OP_lit31";
2977 case DW_OP_reg0:
2978 return "DW_OP_reg0";
2979 case DW_OP_reg1:
2980 return "DW_OP_reg1";
2981 case DW_OP_reg2:
2982 return "DW_OP_reg2";
2983 case DW_OP_reg3:
2984 return "DW_OP_reg3";
2985 case DW_OP_reg4:
2986 return "DW_OP_reg4";
2987 case DW_OP_reg5:
2988 return "DW_OP_reg5";
2989 case DW_OP_reg6:
2990 return "DW_OP_reg6";
2991 case DW_OP_reg7:
2992 return "DW_OP_reg7";
2993 case DW_OP_reg8:
2994 return "DW_OP_reg8";
2995 case DW_OP_reg9:
2996 return "DW_OP_reg9";
2997 case DW_OP_reg10:
2998 return "DW_OP_reg10";
2999 case DW_OP_reg11:
3000 return "DW_OP_reg11";
3001 case DW_OP_reg12:
3002 return "DW_OP_reg12";
3003 case DW_OP_reg13:
3004 return "DW_OP_reg13";
3005 case DW_OP_reg14:
3006 return "DW_OP_reg14";
3007 case DW_OP_reg15:
3008 return "DW_OP_reg15";
3009 case DW_OP_reg16:
3010 return "DW_OP_reg16";
3011 case DW_OP_reg17:
3012 return "DW_OP_reg17";
3013 case DW_OP_reg18:
3014 return "DW_OP_reg18";
3015 case DW_OP_reg19:
3016 return "DW_OP_reg19";
3017 case DW_OP_reg20:
3018 return "DW_OP_reg20";
3019 case DW_OP_reg21:
3020 return "DW_OP_reg21";
3021 case DW_OP_reg22:
3022 return "DW_OP_reg22";
3023 case DW_OP_reg23:
3024 return "DW_OP_reg23";
3025 case DW_OP_reg24:
3026 return "DW_OP_reg24";
3027 case DW_OP_reg25:
3028 return "DW_OP_reg25";
3029 case DW_OP_reg26:
3030 return "DW_OP_reg26";
3031 case DW_OP_reg27:
3032 return "DW_OP_reg27";
3033 case DW_OP_reg28:
3034 return "DW_OP_reg28";
3035 case DW_OP_reg29:
3036 return "DW_OP_reg29";
3037 case DW_OP_reg30:
3038 return "DW_OP_reg30";
3039 case DW_OP_reg31:
3040 return "DW_OP_reg31";
3041 case DW_OP_breg0:
3042 return "DW_OP_breg0";
3043 case DW_OP_breg1:
3044 return "DW_OP_breg1";
3045 case DW_OP_breg2:
3046 return "DW_OP_breg2";
3047 case DW_OP_breg3:
3048 return "DW_OP_breg3";
3049 case DW_OP_breg4:
3050 return "DW_OP_breg4";
3051 case DW_OP_breg5:
3052 return "DW_OP_breg5";
3053 case DW_OP_breg6:
3054 return "DW_OP_breg6";
3055 case DW_OP_breg7:
3056 return "DW_OP_breg7";
3057 case DW_OP_breg8:
3058 return "DW_OP_breg8";
3059 case DW_OP_breg9:
3060 return "DW_OP_breg9";
3061 case DW_OP_breg10:
3062 return "DW_OP_breg10";
3063 case DW_OP_breg11:
3064 return "DW_OP_breg11";
3065 case DW_OP_breg12:
3066 return "DW_OP_breg12";
3067 case DW_OP_breg13:
3068 return "DW_OP_breg13";
3069 case DW_OP_breg14:
3070 return "DW_OP_breg14";
3071 case DW_OP_breg15:
3072 return "DW_OP_breg15";
3073 case DW_OP_breg16:
3074 return "DW_OP_breg16";
3075 case DW_OP_breg17:
3076 return "DW_OP_breg17";
3077 case DW_OP_breg18:
3078 return "DW_OP_breg18";
3079 case DW_OP_breg19:
3080 return "DW_OP_breg19";
3081 case DW_OP_breg20:
3082 return "DW_OP_breg20";
3083 case DW_OP_breg21:
3084 return "DW_OP_breg21";
3085 case DW_OP_breg22:
3086 return "DW_OP_breg22";
3087 case DW_OP_breg23:
3088 return "DW_OP_breg23";
3089 case DW_OP_breg24:
3090 return "DW_OP_breg24";
3091 case DW_OP_breg25:
3092 return "DW_OP_breg25";
3093 case DW_OP_breg26:
3094 return "DW_OP_breg26";
3095 case DW_OP_breg27:
3096 return "DW_OP_breg27";
3097 case DW_OP_breg28:
3098 return "DW_OP_breg28";
3099 case DW_OP_breg29:
3100 return "DW_OP_breg29";
3101 case DW_OP_breg30:
3102 return "DW_OP_breg30";
3103 case DW_OP_breg31:
3104 return "DW_OP_breg31";
3105 case DW_OP_regx:
3106 return "DW_OP_regx";
3107 case DW_OP_fbreg:
3108 return "DW_OP_fbreg";
3109 case DW_OP_bregx:
3110 return "DW_OP_bregx";
3111 case DW_OP_piece:
3112 return "DW_OP_piece";
3113 case DW_OP_deref_size:
3114 return "DW_OP_deref_size";
3115 case DW_OP_xderef_size:
3116 return "DW_OP_xderef_size";
3117 case DW_OP_nop:
3118 return "DW_OP_nop";
3119 case DW_OP_push_object_address:
3120 return "DW_OP_push_object_address";
3121 case DW_OP_call2:
3122 return "DW_OP_call2";
3123 case DW_OP_call4:
3124 return "DW_OP_call4";
3125 case DW_OP_call_ref:
3126 return "DW_OP_call_ref";
3127 case DW_OP_GNU_push_tls_address:
3128 return "DW_OP_GNU_push_tls_address";
3129 case DW_OP_GNU_uninit:
3130 return "DW_OP_GNU_uninit";
3131 default:
3132 return "OP_<unknown>";
3133 }
3134 }
3135
3136 /* Return a pointer to a newly allocated location description. Location
3137 descriptions are simple expression terms that can be strung
3138 together to form more complicated location (address) descriptions. */
3139
3140 static inline dw_loc_descr_ref
3141 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3142 unsigned HOST_WIDE_INT oprnd2)
3143 {
3144 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3145
3146 descr->dw_loc_opc = op;
3147 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3148 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3149 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3150 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3151
3152 return descr;
3153 }
3154
3155 /* Add a location description term to a location description expression. */
3156
3157 static inline void
3158 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3159 {
3160 dw_loc_descr_ref *d;
3161
3162 /* Find the end of the chain. */
3163 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3164 ;
3165
3166 *d = descr;
3167 }
3168
3169 /* Return the size of a location descriptor. */
3170
3171 static unsigned long
3172 size_of_loc_descr (dw_loc_descr_ref loc)
3173 {
3174 unsigned long size = 1;
3175
3176 switch (loc->dw_loc_opc)
3177 {
3178 case DW_OP_addr:
3179 case INTERNAL_DW_OP_tls_addr:
3180 size += DWARF2_ADDR_SIZE;
3181 break;
3182 case DW_OP_const1u:
3183 case DW_OP_const1s:
3184 size += 1;
3185 break;
3186 case DW_OP_const2u:
3187 case DW_OP_const2s:
3188 size += 2;
3189 break;
3190 case DW_OP_const4u:
3191 case DW_OP_const4s:
3192 size += 4;
3193 break;
3194 case DW_OP_const8u:
3195 case DW_OP_const8s:
3196 size += 8;
3197 break;
3198 case DW_OP_constu:
3199 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3200 break;
3201 case DW_OP_consts:
3202 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3203 break;
3204 case DW_OP_pick:
3205 size += 1;
3206 break;
3207 case DW_OP_plus_uconst:
3208 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3209 break;
3210 case DW_OP_skip:
3211 case DW_OP_bra:
3212 size += 2;
3213 break;
3214 case DW_OP_breg0:
3215 case DW_OP_breg1:
3216 case DW_OP_breg2:
3217 case DW_OP_breg3:
3218 case DW_OP_breg4:
3219 case DW_OP_breg5:
3220 case DW_OP_breg6:
3221 case DW_OP_breg7:
3222 case DW_OP_breg8:
3223 case DW_OP_breg9:
3224 case DW_OP_breg10:
3225 case DW_OP_breg11:
3226 case DW_OP_breg12:
3227 case DW_OP_breg13:
3228 case DW_OP_breg14:
3229 case DW_OP_breg15:
3230 case DW_OP_breg16:
3231 case DW_OP_breg17:
3232 case DW_OP_breg18:
3233 case DW_OP_breg19:
3234 case DW_OP_breg20:
3235 case DW_OP_breg21:
3236 case DW_OP_breg22:
3237 case DW_OP_breg23:
3238 case DW_OP_breg24:
3239 case DW_OP_breg25:
3240 case DW_OP_breg26:
3241 case DW_OP_breg27:
3242 case DW_OP_breg28:
3243 case DW_OP_breg29:
3244 case DW_OP_breg30:
3245 case DW_OP_breg31:
3246 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3247 break;
3248 case DW_OP_regx:
3249 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3250 break;
3251 case DW_OP_fbreg:
3252 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3253 break;
3254 case DW_OP_bregx:
3255 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3256 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3257 break;
3258 case DW_OP_piece:
3259 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3260 break;
3261 case DW_OP_deref_size:
3262 case DW_OP_xderef_size:
3263 size += 1;
3264 break;
3265 case DW_OP_call2:
3266 size += 2;
3267 break;
3268 case DW_OP_call4:
3269 size += 4;
3270 break;
3271 case DW_OP_call_ref:
3272 size += DWARF2_ADDR_SIZE;
3273 break;
3274 default:
3275 break;
3276 }
3277
3278 return size;
3279 }
3280
3281 /* Return the size of a series of location descriptors. */
3282
3283 static unsigned long
3284 size_of_locs (dw_loc_descr_ref loc)
3285 {
3286 dw_loc_descr_ref l;
3287 unsigned long size;
3288
3289 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3290 field, to avoid writing to a PCH file. */
3291 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3292 {
3293 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3294 break;
3295 size += size_of_loc_descr (l);
3296 }
3297 if (! l)
3298 return size;
3299
3300 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3301 {
3302 l->dw_loc_addr = size;
3303 size += size_of_loc_descr (l);
3304 }
3305
3306 return size;
3307 }
3308
3309 /* Output location description stack opcode's operands (if any). */
3310
3311 static void
3312 output_loc_operands (dw_loc_descr_ref loc)
3313 {
3314 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3315 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3316
3317 switch (loc->dw_loc_opc)
3318 {
3319 #ifdef DWARF2_DEBUGGING_INFO
3320 case DW_OP_addr:
3321 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3322 break;
3323 case DW_OP_const2u:
3324 case DW_OP_const2s:
3325 dw2_asm_output_data (2, val1->v.val_int, NULL);
3326 break;
3327 case DW_OP_const4u:
3328 case DW_OP_const4s:
3329 dw2_asm_output_data (4, val1->v.val_int, NULL);
3330 break;
3331 case DW_OP_const8u:
3332 case DW_OP_const8s:
3333 gcc_assert (HOST_BITS_PER_LONG >= 64);
3334 dw2_asm_output_data (8, val1->v.val_int, NULL);
3335 break;
3336 case DW_OP_skip:
3337 case DW_OP_bra:
3338 {
3339 int offset;
3340
3341 gcc_assert (val1->val_class == dw_val_class_loc);
3342 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3343
3344 dw2_asm_output_data (2, offset, NULL);
3345 }
3346 break;
3347 #else
3348 case DW_OP_addr:
3349 case DW_OP_const2u:
3350 case DW_OP_const2s:
3351 case DW_OP_const4u:
3352 case DW_OP_const4s:
3353 case DW_OP_const8u:
3354 case DW_OP_const8s:
3355 case DW_OP_skip:
3356 case DW_OP_bra:
3357 /* We currently don't make any attempt to make sure these are
3358 aligned properly like we do for the main unwind info, so
3359 don't support emitting things larger than a byte if we're
3360 only doing unwinding. */
3361 gcc_unreachable ();
3362 #endif
3363 case DW_OP_const1u:
3364 case DW_OP_const1s:
3365 dw2_asm_output_data (1, val1->v.val_int, NULL);
3366 break;
3367 case DW_OP_constu:
3368 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3369 break;
3370 case DW_OP_consts:
3371 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3372 break;
3373 case DW_OP_pick:
3374 dw2_asm_output_data (1, val1->v.val_int, NULL);
3375 break;
3376 case DW_OP_plus_uconst:
3377 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3378 break;
3379 case DW_OP_breg0:
3380 case DW_OP_breg1:
3381 case DW_OP_breg2:
3382 case DW_OP_breg3:
3383 case DW_OP_breg4:
3384 case DW_OP_breg5:
3385 case DW_OP_breg6:
3386 case DW_OP_breg7:
3387 case DW_OP_breg8:
3388 case DW_OP_breg9:
3389 case DW_OP_breg10:
3390 case DW_OP_breg11:
3391 case DW_OP_breg12:
3392 case DW_OP_breg13:
3393 case DW_OP_breg14:
3394 case DW_OP_breg15:
3395 case DW_OP_breg16:
3396 case DW_OP_breg17:
3397 case DW_OP_breg18:
3398 case DW_OP_breg19:
3399 case DW_OP_breg20:
3400 case DW_OP_breg21:
3401 case DW_OP_breg22:
3402 case DW_OP_breg23:
3403 case DW_OP_breg24:
3404 case DW_OP_breg25:
3405 case DW_OP_breg26:
3406 case DW_OP_breg27:
3407 case DW_OP_breg28:
3408 case DW_OP_breg29:
3409 case DW_OP_breg30:
3410 case DW_OP_breg31:
3411 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3412 break;
3413 case DW_OP_regx:
3414 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3415 break;
3416 case DW_OP_fbreg:
3417 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3418 break;
3419 case DW_OP_bregx:
3420 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3421 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3422 break;
3423 case DW_OP_piece:
3424 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3425 break;
3426 case DW_OP_deref_size:
3427 case DW_OP_xderef_size:
3428 dw2_asm_output_data (1, val1->v.val_int, NULL);
3429 break;
3430
3431 case INTERNAL_DW_OP_tls_addr:
3432 if (targetm.asm_out.output_dwarf_dtprel)
3433 {
3434 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3435 DWARF2_ADDR_SIZE,
3436 val1->v.val_addr);
3437 fputc ('\n', asm_out_file);
3438 }
3439 else
3440 gcc_unreachable ();
3441 break;
3442
3443 default:
3444 /* Other codes have no operands. */
3445 break;
3446 }
3447 }
3448
3449 /* Output a sequence of location operations. */
3450
3451 static void
3452 output_loc_sequence (dw_loc_descr_ref loc)
3453 {
3454 for (; loc != NULL; loc = loc->dw_loc_next)
3455 {
3456 /* Output the opcode. */
3457 dw2_asm_output_data (1, loc->dw_loc_opc,
3458 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3459
3460 /* Output the operand(s) (if any). */
3461 output_loc_operands (loc);
3462 }
3463 }
3464
3465 /* This routine will generate the correct assembly data for a location
3466 description based on a cfi entry with a complex address. */
3467
3468 static void
3469 output_cfa_loc (dw_cfi_ref cfi)
3470 {
3471 dw_loc_descr_ref loc;
3472 unsigned long size;
3473
3474 /* Output the size of the block. */
3475 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3476 size = size_of_locs (loc);
3477 dw2_asm_output_data_uleb128 (size, NULL);
3478
3479 /* Now output the operations themselves. */
3480 output_loc_sequence (loc);
3481 }
3482
3483 /* This function builds a dwarf location descriptor sequence from a
3484 dw_cfa_location, adding the given OFFSET to the result of the
3485 expression. */
3486
3487 static struct dw_loc_descr_struct *
3488 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3489 {
3490 struct dw_loc_descr_struct *head, *tmp;
3491
3492 offset += cfa->offset;
3493
3494 if (cfa->indirect)
3495 {
3496 if (cfa->base_offset)
3497 {
3498 if (cfa->reg <= 31)
3499 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3500 else
3501 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3502 }
3503 else if (cfa->reg <= 31)
3504 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3505 else
3506 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3507
3508 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3509 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3510 add_loc_descr (&head, tmp);
3511 if (offset != 0)
3512 {
3513 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3514 add_loc_descr (&head, tmp);
3515 }
3516 }
3517 else
3518 {
3519 if (offset == 0)
3520 if (cfa->reg <= 31)
3521 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3522 else
3523 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3524 else if (cfa->reg <= 31)
3525 head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3526 else
3527 head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3528 }
3529
3530 return head;
3531 }
3532
3533 /* This function fills in aa dw_cfa_location structure from a dwarf location
3534 descriptor sequence. */
3535
3536 static void
3537 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3538 {
3539 struct dw_loc_descr_struct *ptr;
3540 cfa->offset = 0;
3541 cfa->base_offset = 0;
3542 cfa->indirect = 0;
3543 cfa->reg = -1;
3544
3545 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3546 {
3547 enum dwarf_location_atom op = ptr->dw_loc_opc;
3548
3549 switch (op)
3550 {
3551 case DW_OP_reg0:
3552 case DW_OP_reg1:
3553 case DW_OP_reg2:
3554 case DW_OP_reg3:
3555 case DW_OP_reg4:
3556 case DW_OP_reg5:
3557 case DW_OP_reg6:
3558 case DW_OP_reg7:
3559 case DW_OP_reg8:
3560 case DW_OP_reg9:
3561 case DW_OP_reg10:
3562 case DW_OP_reg11:
3563 case DW_OP_reg12:
3564 case DW_OP_reg13:
3565 case DW_OP_reg14:
3566 case DW_OP_reg15:
3567 case DW_OP_reg16:
3568 case DW_OP_reg17:
3569 case DW_OP_reg18:
3570 case DW_OP_reg19:
3571 case DW_OP_reg20:
3572 case DW_OP_reg21:
3573 case DW_OP_reg22:
3574 case DW_OP_reg23:
3575 case DW_OP_reg24:
3576 case DW_OP_reg25:
3577 case DW_OP_reg26:
3578 case DW_OP_reg27:
3579 case DW_OP_reg28:
3580 case DW_OP_reg29:
3581 case DW_OP_reg30:
3582 case DW_OP_reg31:
3583 cfa->reg = op - DW_OP_reg0;
3584 break;
3585 case DW_OP_regx:
3586 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3587 break;
3588 case DW_OP_breg0:
3589 case DW_OP_breg1:
3590 case DW_OP_breg2:
3591 case DW_OP_breg3:
3592 case DW_OP_breg4:
3593 case DW_OP_breg5:
3594 case DW_OP_breg6:
3595 case DW_OP_breg7:
3596 case DW_OP_breg8:
3597 case DW_OP_breg9:
3598 case DW_OP_breg10:
3599 case DW_OP_breg11:
3600 case DW_OP_breg12:
3601 case DW_OP_breg13:
3602 case DW_OP_breg14:
3603 case DW_OP_breg15:
3604 case DW_OP_breg16:
3605 case DW_OP_breg17:
3606 case DW_OP_breg18:
3607 case DW_OP_breg19:
3608 case DW_OP_breg20:
3609 case DW_OP_breg21:
3610 case DW_OP_breg22:
3611 case DW_OP_breg23:
3612 case DW_OP_breg24:
3613 case DW_OP_breg25:
3614 case DW_OP_breg26:
3615 case DW_OP_breg27:
3616 case DW_OP_breg28:
3617 case DW_OP_breg29:
3618 case DW_OP_breg30:
3619 case DW_OP_breg31:
3620 cfa->reg = op - DW_OP_breg0;
3621 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3622 break;
3623 case DW_OP_bregx:
3624 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3625 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3626 break;
3627 case DW_OP_deref:
3628 cfa->indirect = 1;
3629 break;
3630 case DW_OP_plus_uconst:
3631 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3632 break;
3633 default:
3634 internal_error ("DW_LOC_OP %s not implemented",
3635 dwarf_stack_op_name (ptr->dw_loc_opc));
3636 }
3637 }
3638 }
3639 #endif /* .debug_frame support */
3640 \f
3641 /* And now, the support for symbolic debugging information. */
3642 #ifdef DWARF2_DEBUGGING_INFO
3643
3644 /* .debug_str support. */
3645 static int output_indirect_string (void **, void *);
3646
3647 static void dwarf2out_init (const char *);
3648 static void dwarf2out_finish (const char *);
3649 static void dwarf2out_define (unsigned int, const char *);
3650 static void dwarf2out_undef (unsigned int, const char *);
3651 static void dwarf2out_start_source_file (unsigned, const char *);
3652 static void dwarf2out_end_source_file (unsigned);
3653 static void dwarf2out_begin_block (unsigned, unsigned);
3654 static void dwarf2out_end_block (unsigned, unsigned);
3655 static bool dwarf2out_ignore_block (const_tree);
3656 static void dwarf2out_global_decl (tree);
3657 static void dwarf2out_type_decl (tree, int);
3658 static void dwarf2out_imported_module_or_decl (tree, tree);
3659 static void dwarf2out_abstract_function (tree);
3660 static void dwarf2out_var_location (rtx);
3661 static void dwarf2out_begin_function (tree);
3662
3663 /* The debug hooks structure. */
3664
3665 const struct gcc_debug_hooks dwarf2_debug_hooks =
3666 {
3667 dwarf2out_init,
3668 dwarf2out_finish,
3669 dwarf2out_define,
3670 dwarf2out_undef,
3671 dwarf2out_start_source_file,
3672 dwarf2out_end_source_file,
3673 dwarf2out_begin_block,
3674 dwarf2out_end_block,
3675 dwarf2out_ignore_block,
3676 dwarf2out_source_line,
3677 dwarf2out_begin_prologue,
3678 debug_nothing_int_charstar, /* end_prologue */
3679 dwarf2out_end_epilogue,
3680 dwarf2out_begin_function,
3681 debug_nothing_int, /* end_function */
3682 dwarf2out_decl, /* function_decl */
3683 dwarf2out_global_decl,
3684 dwarf2out_type_decl, /* type_decl */
3685 dwarf2out_imported_module_or_decl,
3686 debug_nothing_tree, /* deferred_inline_function */
3687 /* The DWARF 2 backend tries to reduce debugging bloat by not
3688 emitting the abstract description of inline functions until
3689 something tries to reference them. */
3690 dwarf2out_abstract_function, /* outlining_inline_function */
3691 debug_nothing_rtx, /* label */
3692 debug_nothing_int, /* handle_pch */
3693 dwarf2out_var_location,
3694 dwarf2out_switch_text_section,
3695 1 /* start_end_main_source_file */
3696 };
3697 #endif
3698 \f
3699 /* NOTE: In the comments in this file, many references are made to
3700 "Debugging Information Entries". This term is abbreviated as `DIE'
3701 throughout the remainder of this file. */
3702
3703 /* An internal representation of the DWARF output is built, and then
3704 walked to generate the DWARF debugging info. The walk of the internal
3705 representation is done after the entire program has been compiled.
3706 The types below are used to describe the internal representation. */
3707
3708 /* Various DIE's use offsets relative to the beginning of the
3709 .debug_info section to refer to each other. */
3710
3711 typedef long int dw_offset;
3712
3713 /* Define typedefs here to avoid circular dependencies. */
3714
3715 typedef struct dw_attr_struct *dw_attr_ref;
3716 typedef struct dw_line_info_struct *dw_line_info_ref;
3717 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3718 typedef struct pubname_struct *pubname_ref;
3719 typedef struct dw_ranges_struct *dw_ranges_ref;
3720 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3721
3722 /* Each entry in the line_info_table maintains the file and
3723 line number associated with the label generated for that
3724 entry. The label gives the PC value associated with
3725 the line number entry. */
3726
3727 typedef struct dw_line_info_struct GTY(())
3728 {
3729 unsigned long dw_file_num;
3730 unsigned long dw_line_num;
3731 }
3732 dw_line_info_entry;
3733
3734 /* Line information for functions in separate sections; each one gets its
3735 own sequence. */
3736 typedef struct dw_separate_line_info_struct GTY(())
3737 {
3738 unsigned long dw_file_num;
3739 unsigned long dw_line_num;
3740 unsigned long function;
3741 }
3742 dw_separate_line_info_entry;
3743
3744 /* Each DIE attribute has a field specifying the attribute kind,
3745 a link to the next attribute in the chain, and an attribute value.
3746 Attributes are typically linked below the DIE they modify. */
3747
3748 typedef struct dw_attr_struct GTY(())
3749 {
3750 enum dwarf_attribute dw_attr;
3751 dw_val_node dw_attr_val;
3752 }
3753 dw_attr_node;
3754
3755 DEF_VEC_O(dw_attr_node);
3756 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3757
3758 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3759 The children of each node form a circular list linked by
3760 die_sib. die_child points to the node *before* the "first" child node. */
3761
3762 typedef struct die_struct GTY(())
3763 {
3764 enum dwarf_tag die_tag;
3765 char *die_symbol;
3766 VEC(dw_attr_node,gc) * die_attr;
3767 dw_die_ref die_parent;
3768 dw_die_ref die_child;
3769 dw_die_ref die_sib;
3770 dw_die_ref die_definition; /* ref from a specification to its definition */
3771 dw_offset die_offset;
3772 unsigned long die_abbrev;
3773 int die_mark;
3774 /* Die is used and must not be pruned as unused. */
3775 int die_perennial_p;
3776 unsigned int decl_id;
3777 }
3778 die_node;
3779
3780 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3781 #define FOR_EACH_CHILD(die, c, expr) do { \
3782 c = die->die_child; \
3783 if (c) do { \
3784 c = c->die_sib; \
3785 expr; \
3786 } while (c != die->die_child); \
3787 } while (0)
3788
3789 /* The pubname structure */
3790
3791 typedef struct pubname_struct GTY(())
3792 {
3793 dw_die_ref die;
3794 const char *name;
3795 }
3796 pubname_entry;
3797
3798 DEF_VEC_O(pubname_entry);
3799 DEF_VEC_ALLOC_O(pubname_entry, gc);
3800
3801 struct dw_ranges_struct GTY(())
3802 {
3803 /* If this is positive, it's a block number, otherwise it's a
3804 bitwise-negated index into dw_ranges_by_label. */
3805 int num;
3806 };
3807
3808 struct dw_ranges_by_label_struct GTY(())
3809 {
3810 const char *begin;
3811 const char *end;
3812 };
3813
3814 /* The limbo die list structure. */
3815 typedef struct limbo_die_struct GTY(())
3816 {
3817 dw_die_ref die;
3818 tree created_for;
3819 struct limbo_die_struct *next;
3820 }
3821 limbo_die_node;
3822
3823 /* How to start an assembler comment. */
3824 #ifndef ASM_COMMENT_START
3825 #define ASM_COMMENT_START ";#"
3826 #endif
3827
3828 /* Define a macro which returns nonzero for a TYPE_DECL which was
3829 implicitly generated for a tagged type.
3830
3831 Note that unlike the gcc front end (which generates a NULL named
3832 TYPE_DECL node for each complete tagged type, each array type, and
3833 each function type node created) the g++ front end generates a
3834 _named_ TYPE_DECL node for each tagged type node created.
3835 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3836 generate a DW_TAG_typedef DIE for them. */
3837
3838 #define TYPE_DECL_IS_STUB(decl) \
3839 (DECL_NAME (decl) == NULL_TREE \
3840 || (DECL_ARTIFICIAL (decl) \
3841 && is_tagged_type (TREE_TYPE (decl)) \
3842 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3843 /* This is necessary for stub decls that \
3844 appear in nested inline functions. */ \
3845 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3846 && (decl_ultimate_origin (decl) \
3847 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3848
3849 /* Information concerning the compilation unit's programming
3850 language, and compiler version. */
3851
3852 /* Fixed size portion of the DWARF compilation unit header. */
3853 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3854 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3855
3856 /* Fixed size portion of public names info. */
3857 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3858
3859 /* Fixed size portion of the address range info. */
3860 #define DWARF_ARANGES_HEADER_SIZE \
3861 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3862 DWARF2_ADDR_SIZE * 2) \
3863 - DWARF_INITIAL_LENGTH_SIZE)
3864
3865 /* Size of padding portion in the address range info. It must be
3866 aligned to twice the pointer size. */
3867 #define DWARF_ARANGES_PAD_SIZE \
3868 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3869 DWARF2_ADDR_SIZE * 2) \
3870 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3871
3872 /* Use assembler line directives if available. */
3873 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3874 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3875 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3876 #else
3877 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3878 #endif
3879 #endif
3880
3881 /* Minimum line offset in a special line info. opcode.
3882 This value was chosen to give a reasonable range of values. */
3883 #define DWARF_LINE_BASE -10
3884
3885 /* First special line opcode - leave room for the standard opcodes. */
3886 #define DWARF_LINE_OPCODE_BASE 10
3887
3888 /* Range of line offsets in a special line info. opcode. */
3889 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3890
3891 /* Flag that indicates the initial value of the is_stmt_start flag.
3892 In the present implementation, we do not mark any lines as
3893 the beginning of a source statement, because that information
3894 is not made available by the GCC front-end. */
3895 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3896
3897 #ifdef DWARF2_DEBUGGING_INFO
3898 /* This location is used by calc_die_sizes() to keep track
3899 the offset of each DIE within the .debug_info section. */
3900 static unsigned long next_die_offset;
3901 #endif
3902
3903 /* Record the root of the DIE's built for the current compilation unit. */
3904 static GTY(()) dw_die_ref comp_unit_die;
3905
3906 /* A list of DIEs with a NULL parent waiting to be relocated. */
3907 static GTY(()) limbo_die_node *limbo_die_list;
3908
3909 /* Filenames referenced by this compilation unit. */
3910 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3911
3912 /* A hash table of references to DIE's that describe declarations.
3913 The key is a DECL_UID() which is a unique number identifying each decl. */
3914 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3915
3916 /* Node of the variable location list. */
3917 struct var_loc_node GTY ((chain_next ("%h.next")))
3918 {
3919 rtx GTY (()) var_loc_note;
3920 const char * GTY (()) label;
3921 const char * GTY (()) section_label;
3922 struct var_loc_node * GTY (()) next;
3923 };
3924
3925 /* Variable location list. */
3926 struct var_loc_list_def GTY (())
3927 {
3928 struct var_loc_node * GTY (()) first;
3929
3930 /* Do not mark the last element of the chained list because
3931 it is marked through the chain. */
3932 struct var_loc_node * GTY ((skip ("%h"))) last;
3933
3934 /* DECL_UID of the variable decl. */
3935 unsigned int decl_id;
3936 };
3937 typedef struct var_loc_list_def var_loc_list;
3938
3939
3940 /* Table of decl location linked lists. */
3941 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3942
3943 /* A pointer to the base of a list of references to DIE's that
3944 are uniquely identified by their tag, presence/absence of
3945 children DIE's, and list of attribute/value pairs. */
3946 static GTY((length ("abbrev_die_table_allocated")))
3947 dw_die_ref *abbrev_die_table;
3948
3949 /* Number of elements currently allocated for abbrev_die_table. */
3950 static GTY(()) unsigned abbrev_die_table_allocated;
3951
3952 /* Number of elements in type_die_table currently in use. */
3953 static GTY(()) unsigned abbrev_die_table_in_use;
3954
3955 /* Size (in elements) of increments by which we may expand the
3956 abbrev_die_table. */
3957 #define ABBREV_DIE_TABLE_INCREMENT 256
3958
3959 /* A pointer to the base of a table that contains line information
3960 for each source code line in .text in the compilation unit. */
3961 static GTY((length ("line_info_table_allocated")))
3962 dw_line_info_ref line_info_table;
3963
3964 /* Number of elements currently allocated for line_info_table. */
3965 static GTY(()) unsigned line_info_table_allocated;
3966
3967 /* Number of elements in line_info_table currently in use. */
3968 static GTY(()) unsigned line_info_table_in_use;
3969
3970 /* True if the compilation unit places functions in more than one section. */
3971 static GTY(()) bool have_multiple_function_sections = false;
3972
3973 /* A pointer to the base of a table that contains line information
3974 for each source code line outside of .text in the compilation unit. */
3975 static GTY ((length ("separate_line_info_table_allocated")))
3976 dw_separate_line_info_ref separate_line_info_table;
3977
3978 /* Number of elements currently allocated for separate_line_info_table. */
3979 static GTY(()) unsigned separate_line_info_table_allocated;
3980
3981 /* Number of elements in separate_line_info_table currently in use. */
3982 static GTY(()) unsigned separate_line_info_table_in_use;
3983
3984 /* Size (in elements) of increments by which we may expand the
3985 line_info_table. */
3986 #define LINE_INFO_TABLE_INCREMENT 1024
3987
3988 /* A pointer to the base of a table that contains a list of publicly
3989 accessible names. */
3990 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
3991
3992 /* A pointer to the base of a table that contains a list of publicly
3993 accessible types. */
3994 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3995
3996 /* Array of dies for which we should generate .debug_arange info. */
3997 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3998
3999 /* Number of elements currently allocated for arange_table. */
4000 static GTY(()) unsigned arange_table_allocated;
4001
4002 /* Number of elements in arange_table currently in use. */
4003 static GTY(()) unsigned arange_table_in_use;
4004
4005 /* Size (in elements) of increments by which we may expand the
4006 arange_table. */
4007 #define ARANGE_TABLE_INCREMENT 64
4008
4009 /* Array of dies for which we should generate .debug_ranges info. */
4010 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4011
4012 /* Number of elements currently allocated for ranges_table. */
4013 static GTY(()) unsigned ranges_table_allocated;
4014
4015 /* Number of elements in ranges_table currently in use. */
4016 static GTY(()) unsigned ranges_table_in_use;
4017
4018 /* Array of pairs of labels referenced in ranges_table. */
4019 static GTY ((length ("ranges_by_label_allocated")))
4020 dw_ranges_by_label_ref ranges_by_label;
4021
4022 /* Number of elements currently allocated for ranges_by_label. */
4023 static GTY(()) unsigned ranges_by_label_allocated;
4024
4025 /* Number of elements in ranges_by_label currently in use. */
4026 static GTY(()) unsigned ranges_by_label_in_use;
4027
4028 /* Size (in elements) of increments by which we may expand the
4029 ranges_table. */
4030 #define RANGES_TABLE_INCREMENT 64
4031
4032 /* Whether we have location lists that need outputting */
4033 static GTY(()) bool have_location_lists;
4034
4035 /* Unique label counter. */
4036 static GTY(()) unsigned int loclabel_num;
4037
4038 #ifdef DWARF2_DEBUGGING_INFO
4039 /* Record whether the function being analyzed contains inlined functions. */
4040 static int current_function_has_inlines;
4041 #endif
4042 #if 0 && defined (MIPS_DEBUGGING_INFO)
4043 static int comp_unit_has_inlines;
4044 #endif
4045
4046 /* The last file entry emitted by maybe_emit_file(). */
4047 static GTY(()) struct dwarf_file_data * last_emitted_file;
4048
4049 /* Number of internal labels generated by gen_internal_sym(). */
4050 static GTY(()) int label_num;
4051
4052 /* Cached result of previous call to lookup_filename. */
4053 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4054
4055 /* Whether the default text and cold text sections have been used at
4056 all. */
4057
4058 static GTY(()) bool text_section_used = false;
4059 static GTY(()) bool cold_text_section_used = false;
4060
4061 /* The default cold text section. */
4062 static GTY(()) section *cold_text_section;
4063
4064 #ifdef DWARF2_DEBUGGING_INFO
4065
4066 /* Offset from the "steady-state frame pointer" to the frame base,
4067 within the current function. */
4068 static HOST_WIDE_INT frame_pointer_fb_offset;
4069
4070 /* Forward declarations for functions defined in this file. */
4071
4072 static int is_pseudo_reg (const_rtx);
4073 static tree type_main_variant (tree);
4074 static int is_tagged_type (const_tree);
4075 static const char *dwarf_tag_name (unsigned);
4076 static const char *dwarf_attr_name (unsigned);
4077 static const char *dwarf_form_name (unsigned);
4078 static tree decl_ultimate_origin (const_tree);
4079 static tree block_ultimate_origin (const_tree);
4080 static tree decl_class_context (tree);
4081 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4082 static inline enum dw_val_class AT_class (dw_attr_ref);
4083 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4084 static inline unsigned AT_flag (dw_attr_ref);
4085 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4086 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4087 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4088 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4089 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4090 unsigned long);
4091 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4092 unsigned int, unsigned char *);
4093 static hashval_t debug_str_do_hash (const void *);
4094 static int debug_str_eq (const void *, const void *);
4095 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4096 static inline const char *AT_string (dw_attr_ref);
4097 static int AT_string_form (dw_attr_ref);
4098 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4099 static void add_AT_specification (dw_die_ref, dw_die_ref);
4100 static inline dw_die_ref AT_ref (dw_attr_ref);
4101 static inline int AT_ref_external (dw_attr_ref);
4102 static inline void set_AT_ref_external (dw_attr_ref, int);
4103 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4104 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4105 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4106 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4107 dw_loc_list_ref);
4108 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4109 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4110 static inline rtx AT_addr (dw_attr_ref);
4111 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4112 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4113 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4114 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4115 unsigned HOST_WIDE_INT);
4116 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4117 unsigned long);
4118 static inline const char *AT_lbl (dw_attr_ref);
4119 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4120 static const char *get_AT_low_pc (dw_die_ref);
4121 static const char *get_AT_hi_pc (dw_die_ref);
4122 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4123 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4124 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4125 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4126 static bool is_c_family (void);
4127 static bool is_cxx (void);
4128 static bool is_java (void);
4129 static bool is_fortran (void);
4130 static bool is_ada (void);
4131 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4132 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4133 static void add_child_die (dw_die_ref, dw_die_ref);
4134 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4135 static dw_die_ref lookup_type_die (tree);
4136 static void equate_type_number_to_die (tree, dw_die_ref);
4137 static hashval_t decl_die_table_hash (const void *);
4138 static int decl_die_table_eq (const void *, const void *);
4139 static dw_die_ref lookup_decl_die (tree);
4140 static hashval_t decl_loc_table_hash (const void *);
4141 static int decl_loc_table_eq (const void *, const void *);
4142 static var_loc_list *lookup_decl_loc (const_tree);
4143 static void equate_decl_number_to_die (tree, dw_die_ref);
4144 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4145 static void print_spaces (FILE *);
4146 static void print_die (dw_die_ref, FILE *);
4147 static void print_dwarf_line_table (FILE *);
4148 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4149 static dw_die_ref pop_compile_unit (dw_die_ref);
4150 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4151 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4152 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4153 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4154 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4155 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4156 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4157 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4158 static void compute_section_prefix (dw_die_ref);
4159 static int is_type_die (dw_die_ref);
4160 static int is_comdat_die (dw_die_ref);
4161 static int is_symbol_die (dw_die_ref);
4162 static void assign_symbol_names (dw_die_ref);
4163 static void break_out_includes (dw_die_ref);
4164 static hashval_t htab_cu_hash (const void *);
4165 static int htab_cu_eq (const void *, const void *);
4166 static void htab_cu_del (void *);
4167 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4168 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4169 static void add_sibling_attributes (dw_die_ref);
4170 static void build_abbrev_table (dw_die_ref);
4171 static void output_location_lists (dw_die_ref);
4172 static int constant_size (long unsigned);
4173 static unsigned long size_of_die (dw_die_ref);
4174 static void calc_die_sizes (dw_die_ref);
4175 static void mark_dies (dw_die_ref);
4176 static void unmark_dies (dw_die_ref);
4177 static void unmark_all_dies (dw_die_ref);
4178 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4179 static unsigned long size_of_aranges (void);
4180 static enum dwarf_form value_format (dw_attr_ref);
4181 static void output_value_format (dw_attr_ref);
4182 static void output_abbrev_section (void);
4183 static void output_die_symbol (dw_die_ref);
4184 static void output_die (dw_die_ref);
4185 static void output_compilation_unit_header (void);
4186 static void output_comp_unit (dw_die_ref, int);
4187 static const char *dwarf2_name (tree, int);
4188 static void add_pubname (tree, dw_die_ref);
4189 static void add_pubtype (tree, dw_die_ref);
4190 static void output_pubnames (VEC (pubname_entry,gc) *);
4191 static void add_arange (tree, dw_die_ref);
4192 static void output_aranges (void);
4193 static unsigned int add_ranges_num (int);
4194 static unsigned int add_ranges (const_tree);
4195 static unsigned int add_ranges_by_labels (const char *, const char *);
4196 static void output_ranges (void);
4197 static void output_line_info (void);
4198 static void output_file_names (void);
4199 static dw_die_ref base_type_die (tree);
4200 static int is_base_type (tree);
4201 static bool is_subrange_type (const_tree);
4202 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4203 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4204 static int type_is_enum (const_tree);
4205 static unsigned int dbx_reg_number (const_rtx);
4206 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4207 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4208 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4209 enum var_init_status);
4210 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4211 enum var_init_status);
4212 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4213 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4214 enum var_init_status);
4215 static int is_based_loc (const_rtx);
4216 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4217 enum var_init_status);
4218 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4219 enum var_init_status);
4220 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4221 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4222 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4223 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4224 static tree field_type (const_tree);
4225 static unsigned int simple_type_align_in_bits (const_tree);
4226 static unsigned int simple_decl_align_in_bits (const_tree);
4227 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4228 static HOST_WIDE_INT field_byte_offset (const_tree);
4229 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4230 dw_loc_descr_ref);
4231 static void add_data_member_location_attribute (dw_die_ref, tree);
4232 static void add_const_value_attribute (dw_die_ref, rtx);
4233 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4234 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4235 static void insert_float (const_rtx, unsigned char *);
4236 static rtx rtl_for_decl_location (tree);
4237 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4238 enum dwarf_attribute);
4239 static void tree_add_const_value_attribute (dw_die_ref, tree);
4240 static void add_name_attribute (dw_die_ref, const char *);
4241 static void add_comp_dir_attribute (dw_die_ref);
4242 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4243 static void add_subscript_info (dw_die_ref, tree);
4244 static void add_byte_size_attribute (dw_die_ref, tree);
4245 static void add_bit_offset_attribute (dw_die_ref, tree);
4246 static void add_bit_size_attribute (dw_die_ref, tree);
4247 static void add_prototyped_attribute (dw_die_ref, tree);
4248 static void add_abstract_origin_attribute (dw_die_ref, tree);
4249 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4250 static void add_src_coords_attributes (dw_die_ref, tree);
4251 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4252 static void push_decl_scope (tree);
4253 static void pop_decl_scope (void);
4254 static dw_die_ref scope_die_for (tree, dw_die_ref);
4255 static inline int local_scope_p (dw_die_ref);
4256 static inline int class_or_namespace_scope_p (dw_die_ref);
4257 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4258 static void add_calling_convention_attribute (dw_die_ref, tree);
4259 static const char *type_tag (const_tree);
4260 static tree member_declared_type (const_tree);
4261 #if 0
4262 static const char *decl_start_label (tree);
4263 #endif
4264 static void gen_array_type_die (tree, dw_die_ref);
4265 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4266 #if 0
4267 static void gen_entry_point_die (tree, dw_die_ref);
4268 #endif
4269 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4270 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4271 static void gen_inlined_union_type_die (tree, dw_die_ref);
4272 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4273 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4274 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4275 static void gen_formal_types_die (tree, dw_die_ref);
4276 static void gen_subprogram_die (tree, dw_die_ref);
4277 static void gen_variable_die (tree, dw_die_ref);
4278 static void gen_label_die (tree, dw_die_ref);
4279 static void gen_lexical_block_die (tree, dw_die_ref, int);
4280 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4281 static void gen_field_die (tree, dw_die_ref);
4282 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4283 static dw_die_ref gen_compile_unit_die (const char *);
4284 static void gen_inheritance_die (tree, tree, dw_die_ref);
4285 static void gen_member_die (tree, dw_die_ref);
4286 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4287 enum debug_info_usage);
4288 static void gen_subroutine_type_die (tree, dw_die_ref);
4289 static void gen_typedef_die (tree, dw_die_ref);
4290 static void gen_type_die (tree, dw_die_ref);
4291 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4292 static void gen_block_die (tree, dw_die_ref, int);
4293 static void decls_for_scope (tree, dw_die_ref, int);
4294 static int is_redundant_typedef (const_tree);
4295 static void gen_namespace_die (tree);
4296 static void gen_decl_die (tree, dw_die_ref);
4297 static dw_die_ref force_decl_die (tree);
4298 static dw_die_ref force_type_die (tree);
4299 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4300 static void declare_in_namespace (tree, dw_die_ref);
4301 static struct dwarf_file_data * lookup_filename (const char *);
4302 static void retry_incomplete_types (void);
4303 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4304 static void splice_child_die (dw_die_ref, dw_die_ref);
4305 static int file_info_cmp (const void *, const void *);
4306 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4307 const char *, const char *, unsigned);
4308 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4309 const char *, const char *,
4310 const char *);
4311 static void output_loc_list (dw_loc_list_ref);
4312 static char *gen_internal_sym (const char *);
4313
4314 static void prune_unmark_dies (dw_die_ref);
4315 static void prune_unused_types_mark (dw_die_ref, int);
4316 static void prune_unused_types_walk (dw_die_ref);
4317 static void prune_unused_types_walk_attribs (dw_die_ref);
4318 static void prune_unused_types_prune (dw_die_ref);
4319 static void prune_unused_types (void);
4320 static int maybe_emit_file (struct dwarf_file_data *fd);
4321
4322 /* Section names used to hold DWARF debugging information. */
4323 #ifndef DEBUG_INFO_SECTION
4324 #define DEBUG_INFO_SECTION ".debug_info"
4325 #endif
4326 #ifndef DEBUG_ABBREV_SECTION
4327 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4328 #endif
4329 #ifndef DEBUG_ARANGES_SECTION
4330 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4331 #endif
4332 #ifndef DEBUG_MACINFO_SECTION
4333 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4334 #endif
4335 #ifndef DEBUG_LINE_SECTION
4336 #define DEBUG_LINE_SECTION ".debug_line"
4337 #endif
4338 #ifndef DEBUG_LOC_SECTION
4339 #define DEBUG_LOC_SECTION ".debug_loc"
4340 #endif
4341 #ifndef DEBUG_PUBNAMES_SECTION
4342 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4343 #endif
4344 #ifndef DEBUG_STR_SECTION
4345 #define DEBUG_STR_SECTION ".debug_str"
4346 #endif
4347 #ifndef DEBUG_RANGES_SECTION
4348 #define DEBUG_RANGES_SECTION ".debug_ranges"
4349 #endif
4350
4351 /* Standard ELF section names for compiled code and data. */
4352 #ifndef TEXT_SECTION_NAME
4353 #define TEXT_SECTION_NAME ".text"
4354 #endif
4355
4356 /* Section flags for .debug_str section. */
4357 #define DEBUG_STR_SECTION_FLAGS \
4358 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4359 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4360 : SECTION_DEBUG)
4361
4362 /* Labels we insert at beginning sections we can reference instead of
4363 the section names themselves. */
4364
4365 #ifndef TEXT_SECTION_LABEL
4366 #define TEXT_SECTION_LABEL "Ltext"
4367 #endif
4368 #ifndef COLD_TEXT_SECTION_LABEL
4369 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4370 #endif
4371 #ifndef DEBUG_LINE_SECTION_LABEL
4372 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4373 #endif
4374 #ifndef DEBUG_INFO_SECTION_LABEL
4375 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4376 #endif
4377 #ifndef DEBUG_ABBREV_SECTION_LABEL
4378 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4379 #endif
4380 #ifndef DEBUG_LOC_SECTION_LABEL
4381 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4382 #endif
4383 #ifndef DEBUG_RANGES_SECTION_LABEL
4384 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4385 #endif
4386 #ifndef DEBUG_MACINFO_SECTION_LABEL
4387 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4388 #endif
4389
4390 /* Definitions of defaults for formats and names of various special
4391 (artificial) labels which may be generated within this file (when the -g
4392 options is used and DWARF2_DEBUGGING_INFO is in effect.
4393 If necessary, these may be overridden from within the tm.h file, but
4394 typically, overriding these defaults is unnecessary. */
4395
4396 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4397 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4398 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4399 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4400 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4401 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4402 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4403 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4404 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4405 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4406
4407 #ifndef TEXT_END_LABEL
4408 #define TEXT_END_LABEL "Letext"
4409 #endif
4410 #ifndef COLD_END_LABEL
4411 #define COLD_END_LABEL "Letext_cold"
4412 #endif
4413 #ifndef BLOCK_BEGIN_LABEL
4414 #define BLOCK_BEGIN_LABEL "LBB"
4415 #endif
4416 #ifndef BLOCK_END_LABEL
4417 #define BLOCK_END_LABEL "LBE"
4418 #endif
4419 #ifndef LINE_CODE_LABEL
4420 #define LINE_CODE_LABEL "LM"
4421 #endif
4422 #ifndef SEPARATE_LINE_CODE_LABEL
4423 #define SEPARATE_LINE_CODE_LABEL "LSM"
4424 #endif
4425
4426 \f
4427 /* We allow a language front-end to designate a function that is to be
4428 called to "demangle" any name before it is put into a DIE. */
4429
4430 static const char *(*demangle_name_func) (const char *);
4431
4432 void
4433 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4434 {
4435 demangle_name_func = func;
4436 }
4437
4438 /* Test if rtl node points to a pseudo register. */
4439
4440 static inline int
4441 is_pseudo_reg (const_rtx rtl)
4442 {
4443 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4444 || (GET_CODE (rtl) == SUBREG
4445 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4446 }
4447
4448 /* Return a reference to a type, with its const and volatile qualifiers
4449 removed. */
4450
4451 static inline tree
4452 type_main_variant (tree type)
4453 {
4454 type = TYPE_MAIN_VARIANT (type);
4455
4456 /* ??? There really should be only one main variant among any group of
4457 variants of a given type (and all of the MAIN_VARIANT values for all
4458 members of the group should point to that one type) but sometimes the C
4459 front-end messes this up for array types, so we work around that bug
4460 here. */
4461 if (TREE_CODE (type) == ARRAY_TYPE)
4462 while (type != TYPE_MAIN_VARIANT (type))
4463 type = TYPE_MAIN_VARIANT (type);
4464
4465 return type;
4466 }
4467
4468 /* Return nonzero if the given type node represents a tagged type. */
4469
4470 static inline int
4471 is_tagged_type (const_tree type)
4472 {
4473 enum tree_code code = TREE_CODE (type);
4474
4475 return (code == RECORD_TYPE || code == UNION_TYPE
4476 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4477 }
4478
4479 /* Convert a DIE tag into its string name. */
4480
4481 static const char *
4482 dwarf_tag_name (unsigned int tag)
4483 {
4484 switch (tag)
4485 {
4486 case DW_TAG_padding:
4487 return "DW_TAG_padding";
4488 case DW_TAG_array_type:
4489 return "DW_TAG_array_type";
4490 case DW_TAG_class_type:
4491 return "DW_TAG_class_type";
4492 case DW_TAG_entry_point:
4493 return "DW_TAG_entry_point";
4494 case DW_TAG_enumeration_type:
4495 return "DW_TAG_enumeration_type";
4496 case DW_TAG_formal_parameter:
4497 return "DW_TAG_formal_parameter";
4498 case DW_TAG_imported_declaration:
4499 return "DW_TAG_imported_declaration";
4500 case DW_TAG_label:
4501 return "DW_TAG_label";
4502 case DW_TAG_lexical_block:
4503 return "DW_TAG_lexical_block";
4504 case DW_TAG_member:
4505 return "DW_TAG_member";
4506 case DW_TAG_pointer_type:
4507 return "DW_TAG_pointer_type";
4508 case DW_TAG_reference_type:
4509 return "DW_TAG_reference_type";
4510 case DW_TAG_compile_unit:
4511 return "DW_TAG_compile_unit";
4512 case DW_TAG_string_type:
4513 return "DW_TAG_string_type";
4514 case DW_TAG_structure_type:
4515 return "DW_TAG_structure_type";
4516 case DW_TAG_subroutine_type:
4517 return "DW_TAG_subroutine_type";
4518 case DW_TAG_typedef:
4519 return "DW_TAG_typedef";
4520 case DW_TAG_union_type:
4521 return "DW_TAG_union_type";
4522 case DW_TAG_unspecified_parameters:
4523 return "DW_TAG_unspecified_parameters";
4524 case DW_TAG_variant:
4525 return "DW_TAG_variant";
4526 case DW_TAG_common_block:
4527 return "DW_TAG_common_block";
4528 case DW_TAG_common_inclusion:
4529 return "DW_TAG_common_inclusion";
4530 case DW_TAG_inheritance:
4531 return "DW_TAG_inheritance";
4532 case DW_TAG_inlined_subroutine:
4533 return "DW_TAG_inlined_subroutine";
4534 case DW_TAG_module:
4535 return "DW_TAG_module";
4536 case DW_TAG_ptr_to_member_type:
4537 return "DW_TAG_ptr_to_member_type";
4538 case DW_TAG_set_type:
4539 return "DW_TAG_set_type";
4540 case DW_TAG_subrange_type:
4541 return "DW_TAG_subrange_type";
4542 case DW_TAG_with_stmt:
4543 return "DW_TAG_with_stmt";
4544 case DW_TAG_access_declaration:
4545 return "DW_TAG_access_declaration";
4546 case DW_TAG_base_type:
4547 return "DW_TAG_base_type";
4548 case DW_TAG_catch_block:
4549 return "DW_TAG_catch_block";
4550 case DW_TAG_const_type:
4551 return "DW_TAG_const_type";
4552 case DW_TAG_constant:
4553 return "DW_TAG_constant";
4554 case DW_TAG_enumerator:
4555 return "DW_TAG_enumerator";
4556 case DW_TAG_file_type:
4557 return "DW_TAG_file_type";
4558 case DW_TAG_friend:
4559 return "DW_TAG_friend";
4560 case DW_TAG_namelist:
4561 return "DW_TAG_namelist";
4562 case DW_TAG_namelist_item:
4563 return "DW_TAG_namelist_item";
4564 case DW_TAG_packed_type:
4565 return "DW_TAG_packed_type";
4566 case DW_TAG_subprogram:
4567 return "DW_TAG_subprogram";
4568 case DW_TAG_template_type_param:
4569 return "DW_TAG_template_type_param";
4570 case DW_TAG_template_value_param:
4571 return "DW_TAG_template_value_param";
4572 case DW_TAG_thrown_type:
4573 return "DW_TAG_thrown_type";
4574 case DW_TAG_try_block:
4575 return "DW_TAG_try_block";
4576 case DW_TAG_variant_part:
4577 return "DW_TAG_variant_part";
4578 case DW_TAG_variable:
4579 return "DW_TAG_variable";
4580 case DW_TAG_volatile_type:
4581 return "DW_TAG_volatile_type";
4582 case DW_TAG_dwarf_procedure:
4583 return "DW_TAG_dwarf_procedure";
4584 case DW_TAG_restrict_type:
4585 return "DW_TAG_restrict_type";
4586 case DW_TAG_interface_type:
4587 return "DW_TAG_interface_type";
4588 case DW_TAG_namespace:
4589 return "DW_TAG_namespace";
4590 case DW_TAG_imported_module:
4591 return "DW_TAG_imported_module";
4592 case DW_TAG_unspecified_type:
4593 return "DW_TAG_unspecified_type";
4594 case DW_TAG_partial_unit:
4595 return "DW_TAG_partial_unit";
4596 case DW_TAG_imported_unit:
4597 return "DW_TAG_imported_unit";
4598 case DW_TAG_condition:
4599 return "DW_TAG_condition";
4600 case DW_TAG_shared_type:
4601 return "DW_TAG_shared_type";
4602 case DW_TAG_MIPS_loop:
4603 return "DW_TAG_MIPS_loop";
4604 case DW_TAG_format_label:
4605 return "DW_TAG_format_label";
4606 case DW_TAG_function_template:
4607 return "DW_TAG_function_template";
4608 case DW_TAG_class_template:
4609 return "DW_TAG_class_template";
4610 case DW_TAG_GNU_BINCL:
4611 return "DW_TAG_GNU_BINCL";
4612 case DW_TAG_GNU_EINCL:
4613 return "DW_TAG_GNU_EINCL";
4614 default:
4615 return "DW_TAG_<unknown>";
4616 }
4617 }
4618
4619 /* Convert a DWARF attribute code into its string name. */
4620
4621 static const char *
4622 dwarf_attr_name (unsigned int attr)
4623 {
4624 switch (attr)
4625 {
4626 case DW_AT_sibling:
4627 return "DW_AT_sibling";
4628 case DW_AT_location:
4629 return "DW_AT_location";
4630 case DW_AT_name:
4631 return "DW_AT_name";
4632 case DW_AT_ordering:
4633 return "DW_AT_ordering";
4634 case DW_AT_subscr_data:
4635 return "DW_AT_subscr_data";
4636 case DW_AT_byte_size:
4637 return "DW_AT_byte_size";
4638 case DW_AT_bit_offset:
4639 return "DW_AT_bit_offset";
4640 case DW_AT_bit_size:
4641 return "DW_AT_bit_size";
4642 case DW_AT_element_list:
4643 return "DW_AT_element_list";
4644 case DW_AT_stmt_list:
4645 return "DW_AT_stmt_list";
4646 case DW_AT_low_pc:
4647 return "DW_AT_low_pc";
4648 case DW_AT_high_pc:
4649 return "DW_AT_high_pc";
4650 case DW_AT_language:
4651 return "DW_AT_language";
4652 case DW_AT_member:
4653 return "DW_AT_member";
4654 case DW_AT_discr:
4655 return "DW_AT_discr";
4656 case DW_AT_discr_value:
4657 return "DW_AT_discr_value";
4658 case DW_AT_visibility:
4659 return "DW_AT_visibility";
4660 case DW_AT_import:
4661 return "DW_AT_import";
4662 case DW_AT_string_length:
4663 return "DW_AT_string_length";
4664 case DW_AT_common_reference:
4665 return "DW_AT_common_reference";
4666 case DW_AT_comp_dir:
4667 return "DW_AT_comp_dir";
4668 case DW_AT_const_value:
4669 return "DW_AT_const_value";
4670 case DW_AT_containing_type:
4671 return "DW_AT_containing_type";
4672 case DW_AT_default_value:
4673 return "DW_AT_default_value";
4674 case DW_AT_inline:
4675 return "DW_AT_inline";
4676 case DW_AT_is_optional:
4677 return "DW_AT_is_optional";
4678 case DW_AT_lower_bound:
4679 return "DW_AT_lower_bound";
4680 case DW_AT_producer:
4681 return "DW_AT_producer";
4682 case DW_AT_prototyped:
4683 return "DW_AT_prototyped";
4684 case DW_AT_return_addr:
4685 return "DW_AT_return_addr";
4686 case DW_AT_start_scope:
4687 return "DW_AT_start_scope";
4688 case DW_AT_bit_stride:
4689 return "DW_AT_bit_stride";
4690 case DW_AT_upper_bound:
4691 return "DW_AT_upper_bound";
4692 case DW_AT_abstract_origin:
4693 return "DW_AT_abstract_origin";
4694 case DW_AT_accessibility:
4695 return "DW_AT_accessibility";
4696 case DW_AT_address_class:
4697 return "DW_AT_address_class";
4698 case DW_AT_artificial:
4699 return "DW_AT_artificial";
4700 case DW_AT_base_types:
4701 return "DW_AT_base_types";
4702 case DW_AT_calling_convention:
4703 return "DW_AT_calling_convention";
4704 case DW_AT_count:
4705 return "DW_AT_count";
4706 case DW_AT_data_member_location:
4707 return "DW_AT_data_member_location";
4708 case DW_AT_decl_column:
4709 return "DW_AT_decl_column";
4710 case DW_AT_decl_file:
4711 return "DW_AT_decl_file";
4712 case DW_AT_decl_line:
4713 return "DW_AT_decl_line";
4714 case DW_AT_declaration:
4715 return "DW_AT_declaration";
4716 case DW_AT_discr_list:
4717 return "DW_AT_discr_list";
4718 case DW_AT_encoding:
4719 return "DW_AT_encoding";
4720 case DW_AT_external:
4721 return "DW_AT_external";
4722 case DW_AT_frame_base:
4723 return "DW_AT_frame_base";
4724 case DW_AT_friend:
4725 return "DW_AT_friend";
4726 case DW_AT_identifier_case:
4727 return "DW_AT_identifier_case";
4728 case DW_AT_macro_info:
4729 return "DW_AT_macro_info";
4730 case DW_AT_namelist_items:
4731 return "DW_AT_namelist_items";
4732 case DW_AT_priority:
4733 return "DW_AT_priority";
4734 case DW_AT_segment:
4735 return "DW_AT_segment";
4736 case DW_AT_specification:
4737 return "DW_AT_specification";
4738 case DW_AT_static_link:
4739 return "DW_AT_static_link";
4740 case DW_AT_type:
4741 return "DW_AT_type";
4742 case DW_AT_use_location:
4743 return "DW_AT_use_location";
4744 case DW_AT_variable_parameter:
4745 return "DW_AT_variable_parameter";
4746 case DW_AT_virtuality:
4747 return "DW_AT_virtuality";
4748 case DW_AT_vtable_elem_location:
4749 return "DW_AT_vtable_elem_location";
4750
4751 case DW_AT_allocated:
4752 return "DW_AT_allocated";
4753 case DW_AT_associated:
4754 return "DW_AT_associated";
4755 case DW_AT_data_location:
4756 return "DW_AT_data_location";
4757 case DW_AT_byte_stride:
4758 return "DW_AT_byte_stride";
4759 case DW_AT_entry_pc:
4760 return "DW_AT_entry_pc";
4761 case DW_AT_use_UTF8:
4762 return "DW_AT_use_UTF8";
4763 case DW_AT_extension:
4764 return "DW_AT_extension";
4765 case DW_AT_ranges:
4766 return "DW_AT_ranges";
4767 case DW_AT_trampoline:
4768 return "DW_AT_trampoline";
4769 case DW_AT_call_column:
4770 return "DW_AT_call_column";
4771 case DW_AT_call_file:
4772 return "DW_AT_call_file";
4773 case DW_AT_call_line:
4774 return "DW_AT_call_line";
4775
4776 case DW_AT_MIPS_fde:
4777 return "DW_AT_MIPS_fde";
4778 case DW_AT_MIPS_loop_begin:
4779 return "DW_AT_MIPS_loop_begin";
4780 case DW_AT_MIPS_tail_loop_begin:
4781 return "DW_AT_MIPS_tail_loop_begin";
4782 case DW_AT_MIPS_epilog_begin:
4783 return "DW_AT_MIPS_epilog_begin";
4784 case DW_AT_MIPS_loop_unroll_factor:
4785 return "DW_AT_MIPS_loop_unroll_factor";
4786 case DW_AT_MIPS_software_pipeline_depth:
4787 return "DW_AT_MIPS_software_pipeline_depth";
4788 case DW_AT_MIPS_linkage_name:
4789 return "DW_AT_MIPS_linkage_name";
4790 case DW_AT_MIPS_stride:
4791 return "DW_AT_MIPS_stride";
4792 case DW_AT_MIPS_abstract_name:
4793 return "DW_AT_MIPS_abstract_name";
4794 case DW_AT_MIPS_clone_origin:
4795 return "DW_AT_MIPS_clone_origin";
4796 case DW_AT_MIPS_has_inlines:
4797 return "DW_AT_MIPS_has_inlines";
4798
4799 case DW_AT_sf_names:
4800 return "DW_AT_sf_names";
4801 case DW_AT_src_info:
4802 return "DW_AT_src_info";
4803 case DW_AT_mac_info:
4804 return "DW_AT_mac_info";
4805 case DW_AT_src_coords:
4806 return "DW_AT_src_coords";
4807 case DW_AT_body_begin:
4808 return "DW_AT_body_begin";
4809 case DW_AT_body_end:
4810 return "DW_AT_body_end";
4811 case DW_AT_GNU_vector:
4812 return "DW_AT_GNU_vector";
4813
4814 case DW_AT_VMS_rtnbeg_pd_address:
4815 return "DW_AT_VMS_rtnbeg_pd_address";
4816
4817 default:
4818 return "DW_AT_<unknown>";
4819 }
4820 }
4821
4822 /* Convert a DWARF value form code into its string name. */
4823
4824 static const char *
4825 dwarf_form_name (unsigned int form)
4826 {
4827 switch (form)
4828 {
4829 case DW_FORM_addr:
4830 return "DW_FORM_addr";
4831 case DW_FORM_block2:
4832 return "DW_FORM_block2";
4833 case DW_FORM_block4:
4834 return "DW_FORM_block4";
4835 case DW_FORM_data2:
4836 return "DW_FORM_data2";
4837 case DW_FORM_data4:
4838 return "DW_FORM_data4";
4839 case DW_FORM_data8:
4840 return "DW_FORM_data8";
4841 case DW_FORM_string:
4842 return "DW_FORM_string";
4843 case DW_FORM_block:
4844 return "DW_FORM_block";
4845 case DW_FORM_block1:
4846 return "DW_FORM_block1";
4847 case DW_FORM_data1:
4848 return "DW_FORM_data1";
4849 case DW_FORM_flag:
4850 return "DW_FORM_flag";
4851 case DW_FORM_sdata:
4852 return "DW_FORM_sdata";
4853 case DW_FORM_strp:
4854 return "DW_FORM_strp";
4855 case DW_FORM_udata:
4856 return "DW_FORM_udata";
4857 case DW_FORM_ref_addr:
4858 return "DW_FORM_ref_addr";
4859 case DW_FORM_ref1:
4860 return "DW_FORM_ref1";
4861 case DW_FORM_ref2:
4862 return "DW_FORM_ref2";
4863 case DW_FORM_ref4:
4864 return "DW_FORM_ref4";
4865 case DW_FORM_ref8:
4866 return "DW_FORM_ref8";
4867 case DW_FORM_ref_udata:
4868 return "DW_FORM_ref_udata";
4869 case DW_FORM_indirect:
4870 return "DW_FORM_indirect";
4871 default:
4872 return "DW_FORM_<unknown>";
4873 }
4874 }
4875 \f
4876 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4877 instance of an inlined instance of a decl which is local to an inline
4878 function, so we have to trace all of the way back through the origin chain
4879 to find out what sort of node actually served as the original seed for the
4880 given block. */
4881
4882 static tree
4883 decl_ultimate_origin (const_tree decl)
4884 {
4885 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4886 return NULL_TREE;
4887
4888 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4889 nodes in the function to point to themselves; ignore that if
4890 we're trying to output the abstract instance of this function. */
4891 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4892 return NULL_TREE;
4893
4894 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4895 most distant ancestor, this should never happen. */
4896 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4897
4898 return DECL_ABSTRACT_ORIGIN (decl);
4899 }
4900
4901 /* Determine the "ultimate origin" of a block. The block may be an inlined
4902 instance of an inlined instance of a block which is local to an inline
4903 function, so we have to trace all of the way back through the origin chain
4904 to find out what sort of node actually served as the original seed for the
4905 given block. */
4906
4907 static tree
4908 block_ultimate_origin (const_tree block)
4909 {
4910 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4911
4912 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4913 nodes in the function to point to themselves; ignore that if
4914 we're trying to output the abstract instance of this function. */
4915 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4916 return NULL_TREE;
4917
4918 if (immediate_origin == NULL_TREE)
4919 return NULL_TREE;
4920 else
4921 {
4922 tree ret_val;
4923 tree lookahead = immediate_origin;
4924
4925 do
4926 {
4927 ret_val = lookahead;
4928 lookahead = (TREE_CODE (ret_val) == BLOCK
4929 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4930 }
4931 while (lookahead != NULL && lookahead != ret_val);
4932
4933 /* The block's abstract origin chain may not be the *ultimate* origin of
4934 the block. It could lead to a DECL that has an abstract origin set.
4935 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4936 will give us if it has one). Note that DECL's abstract origins are
4937 supposed to be the most distant ancestor (or so decl_ultimate_origin
4938 claims), so we don't need to loop following the DECL origins. */
4939 if (DECL_P (ret_val))
4940 return DECL_ORIGIN (ret_val);
4941
4942 return ret_val;
4943 }
4944 }
4945
4946 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4947 of a virtual function may refer to a base class, so we check the 'this'
4948 parameter. */
4949
4950 static tree
4951 decl_class_context (tree decl)
4952 {
4953 tree context = NULL_TREE;
4954
4955 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4956 context = DECL_CONTEXT (decl);
4957 else
4958 context = TYPE_MAIN_VARIANT
4959 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4960
4961 if (context && !TYPE_P (context))
4962 context = NULL_TREE;
4963
4964 return context;
4965 }
4966 \f
4967 /* Add an attribute/value pair to a DIE. */
4968
4969 static inline void
4970 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4971 {
4972 /* Maybe this should be an assert? */
4973 if (die == NULL)
4974 return;
4975
4976 if (die->die_attr == NULL)
4977 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4978 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4979 }
4980
4981 static inline enum dw_val_class
4982 AT_class (dw_attr_ref a)
4983 {
4984 return a->dw_attr_val.val_class;
4985 }
4986
4987 /* Add a flag value attribute to a DIE. */
4988
4989 static inline void
4990 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4991 {
4992 dw_attr_node attr;
4993
4994 attr.dw_attr = attr_kind;
4995 attr.dw_attr_val.val_class = dw_val_class_flag;
4996 attr.dw_attr_val.v.val_flag = flag;
4997 add_dwarf_attr (die, &attr);
4998 }
4999
5000 static inline unsigned
5001 AT_flag (dw_attr_ref a)
5002 {
5003 gcc_assert (a && AT_class (a) == dw_val_class_flag);
5004 return a->dw_attr_val.v.val_flag;
5005 }
5006
5007 /* Add a signed integer attribute value to a DIE. */
5008
5009 static inline void
5010 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5011 {
5012 dw_attr_node attr;
5013
5014 attr.dw_attr = attr_kind;
5015 attr.dw_attr_val.val_class = dw_val_class_const;
5016 attr.dw_attr_val.v.val_int = int_val;
5017 add_dwarf_attr (die, &attr);
5018 }
5019
5020 static inline HOST_WIDE_INT
5021 AT_int (dw_attr_ref a)
5022 {
5023 gcc_assert (a && AT_class (a) == dw_val_class_const);
5024 return a->dw_attr_val.v.val_int;
5025 }
5026
5027 /* Add an unsigned integer attribute value to a DIE. */
5028
5029 static inline void
5030 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5031 unsigned HOST_WIDE_INT unsigned_val)
5032 {
5033 dw_attr_node attr;
5034
5035 attr.dw_attr = attr_kind;
5036 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5037 attr.dw_attr_val.v.val_unsigned = unsigned_val;
5038 add_dwarf_attr (die, &attr);
5039 }
5040
5041 static inline unsigned HOST_WIDE_INT
5042 AT_unsigned (dw_attr_ref a)
5043 {
5044 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5045 return a->dw_attr_val.v.val_unsigned;
5046 }
5047
5048 /* Add an unsigned double integer attribute value to a DIE. */
5049
5050 static inline void
5051 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5052 long unsigned int val_hi, long unsigned int val_low)
5053 {
5054 dw_attr_node attr;
5055
5056 attr.dw_attr = attr_kind;
5057 attr.dw_attr_val.val_class = dw_val_class_long_long;
5058 attr.dw_attr_val.v.val_long_long.hi = val_hi;
5059 attr.dw_attr_val.v.val_long_long.low = val_low;
5060 add_dwarf_attr (die, &attr);
5061 }
5062
5063 /* Add a floating point attribute value to a DIE and return it. */
5064
5065 static inline void
5066 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5067 unsigned int length, unsigned int elt_size, unsigned char *array)
5068 {
5069 dw_attr_node attr;
5070
5071 attr.dw_attr = attr_kind;
5072 attr.dw_attr_val.val_class = dw_val_class_vec;
5073 attr.dw_attr_val.v.val_vec.length = length;
5074 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5075 attr.dw_attr_val.v.val_vec.array = array;
5076 add_dwarf_attr (die, &attr);
5077 }
5078
5079 /* Hash and equality functions for debug_str_hash. */
5080
5081 static hashval_t
5082 debug_str_do_hash (const void *x)
5083 {
5084 return htab_hash_string (((const struct indirect_string_node *)x)->str);
5085 }
5086
5087 static int
5088 debug_str_eq (const void *x1, const void *x2)
5089 {
5090 return strcmp ((((const struct indirect_string_node *)x1)->str),
5091 (const char *)x2) == 0;
5092 }
5093
5094 /* Add a string attribute value to a DIE. */
5095
5096 static inline void
5097 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5098 {
5099 dw_attr_node attr;
5100 struct indirect_string_node *node;
5101 void **slot;
5102
5103 if (! debug_str_hash)
5104 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5105 debug_str_eq, NULL);
5106
5107 slot = htab_find_slot_with_hash (debug_str_hash, str,
5108 htab_hash_string (str), INSERT);
5109 if (*slot == NULL)
5110 {
5111 node = (struct indirect_string_node *)
5112 ggc_alloc_cleared (sizeof (struct indirect_string_node));
5113 node->str = ggc_strdup (str);
5114 *slot = node;
5115 }
5116 else
5117 node = (struct indirect_string_node *) *slot;
5118
5119 node->refcount++;
5120
5121 attr.dw_attr = attr_kind;
5122 attr.dw_attr_val.val_class = dw_val_class_str;
5123 attr.dw_attr_val.v.val_str = node;
5124 add_dwarf_attr (die, &attr);
5125 }
5126
5127 static inline const char *
5128 AT_string (dw_attr_ref a)
5129 {
5130 gcc_assert (a && AT_class (a) == dw_val_class_str);
5131 return a->dw_attr_val.v.val_str->str;
5132 }
5133
5134 /* Find out whether a string should be output inline in DIE
5135 or out-of-line in .debug_str section. */
5136
5137 static int
5138 AT_string_form (dw_attr_ref a)
5139 {
5140 struct indirect_string_node *node;
5141 unsigned int len;
5142 char label[32];
5143
5144 gcc_assert (a && AT_class (a) == dw_val_class_str);
5145
5146 node = a->dw_attr_val.v.val_str;
5147 if (node->form)
5148 return node->form;
5149
5150 len = strlen (node->str) + 1;
5151
5152 /* If the string is shorter or equal to the size of the reference, it is
5153 always better to put it inline. */
5154 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5155 return node->form = DW_FORM_string;
5156
5157 /* If we cannot expect the linker to merge strings in .debug_str
5158 section, only put it into .debug_str if it is worth even in this
5159 single module. */
5160 if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5161 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5162 return node->form = DW_FORM_string;
5163
5164 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5165 ++dw2_string_counter;
5166 node->label = xstrdup (label);
5167
5168 return node->form = DW_FORM_strp;
5169 }
5170
5171 /* Add a DIE reference attribute value to a DIE. */
5172
5173 static inline void
5174 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5175 {
5176 dw_attr_node attr;
5177
5178 attr.dw_attr = attr_kind;
5179 attr.dw_attr_val.val_class = dw_val_class_die_ref;
5180 attr.dw_attr_val.v.val_die_ref.die = targ_die;
5181 attr.dw_attr_val.v.val_die_ref.external = 0;
5182 add_dwarf_attr (die, &attr);
5183 }
5184
5185 /* Add an AT_specification attribute to a DIE, and also make the back
5186 pointer from the specification to the definition. */
5187
5188 static inline void
5189 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5190 {
5191 add_AT_die_ref (die, DW_AT_specification, targ_die);
5192 gcc_assert (!targ_die->die_definition);
5193 targ_die->die_definition = die;
5194 }
5195
5196 static inline dw_die_ref
5197 AT_ref (dw_attr_ref a)
5198 {
5199 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5200 return a->dw_attr_val.v.val_die_ref.die;
5201 }
5202
5203 static inline int
5204 AT_ref_external (dw_attr_ref a)
5205 {
5206 if (a && AT_class (a) == dw_val_class_die_ref)
5207 return a->dw_attr_val.v.val_die_ref.external;
5208
5209 return 0;
5210 }
5211
5212 static inline void
5213 set_AT_ref_external (dw_attr_ref a, int i)
5214 {
5215 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5216 a->dw_attr_val.v.val_die_ref.external = i;
5217 }
5218
5219 /* Add an FDE reference attribute value to a DIE. */
5220
5221 static inline void
5222 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5223 {
5224 dw_attr_node attr;
5225
5226 attr.dw_attr = attr_kind;
5227 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5228 attr.dw_attr_val.v.val_fde_index = targ_fde;
5229 add_dwarf_attr (die, &attr);
5230 }
5231
5232 /* Add a location description attribute value to a DIE. */
5233
5234 static inline void
5235 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5236 {
5237 dw_attr_node attr;
5238
5239 attr.dw_attr = attr_kind;
5240 attr.dw_attr_val.val_class = dw_val_class_loc;
5241 attr.dw_attr_val.v.val_loc = loc;
5242 add_dwarf_attr (die, &attr);
5243 }
5244
5245 static inline dw_loc_descr_ref
5246 AT_loc (dw_attr_ref a)
5247 {
5248 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5249 return a->dw_attr_val.v.val_loc;
5250 }
5251
5252 static inline void
5253 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5254 {
5255 dw_attr_node attr;
5256
5257 attr.dw_attr = attr_kind;
5258 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5259 attr.dw_attr_val.v.val_loc_list = loc_list;
5260 add_dwarf_attr (die, &attr);
5261 have_location_lists = true;
5262 }
5263
5264 static inline dw_loc_list_ref
5265 AT_loc_list (dw_attr_ref a)
5266 {
5267 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5268 return a->dw_attr_val.v.val_loc_list;
5269 }
5270
5271 /* Add an address constant attribute value to a DIE. */
5272
5273 static inline void
5274 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5275 {
5276 dw_attr_node attr;
5277
5278 attr.dw_attr = attr_kind;
5279 attr.dw_attr_val.val_class = dw_val_class_addr;
5280 attr.dw_attr_val.v.val_addr = addr;
5281 add_dwarf_attr (die, &attr);
5282 }
5283
5284 /* Get the RTX from to an address DIE attribute. */
5285
5286 static inline rtx
5287 AT_addr (dw_attr_ref a)
5288 {
5289 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5290 return a->dw_attr_val.v.val_addr;
5291 }
5292
5293 /* Add a file attribute value to a DIE. */
5294
5295 static inline void
5296 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5297 struct dwarf_file_data *fd)
5298 {
5299 dw_attr_node attr;
5300
5301 attr.dw_attr = attr_kind;
5302 attr.dw_attr_val.val_class = dw_val_class_file;
5303 attr.dw_attr_val.v.val_file = fd;
5304 add_dwarf_attr (die, &attr);
5305 }
5306
5307 /* Get the dwarf_file_data from a file DIE attribute. */
5308
5309 static inline struct dwarf_file_data *
5310 AT_file (dw_attr_ref a)
5311 {
5312 gcc_assert (a && AT_class (a) == dw_val_class_file);
5313 return a->dw_attr_val.v.val_file;
5314 }
5315
5316 /* Add a label identifier attribute value to a DIE. */
5317
5318 static inline void
5319 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5320 {
5321 dw_attr_node attr;
5322
5323 attr.dw_attr = attr_kind;
5324 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5325 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5326 add_dwarf_attr (die, &attr);
5327 }
5328
5329 /* Add a section offset attribute value to a DIE, an offset into the
5330 debug_line section. */
5331
5332 static inline void
5333 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5334 const char *label)
5335 {
5336 dw_attr_node attr;
5337
5338 attr.dw_attr = attr_kind;
5339 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5340 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5341 add_dwarf_attr (die, &attr);
5342 }
5343
5344 /* Add a section offset attribute value to a DIE, an offset into the
5345 debug_macinfo section. */
5346
5347 static inline void
5348 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5349 const char *label)
5350 {
5351 dw_attr_node attr;
5352
5353 attr.dw_attr = attr_kind;
5354 attr.dw_attr_val.val_class = dw_val_class_macptr;
5355 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5356 add_dwarf_attr (die, &attr);
5357 }
5358
5359 /* Add an offset attribute value to a DIE. */
5360
5361 static inline void
5362 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5363 unsigned HOST_WIDE_INT offset)
5364 {
5365 dw_attr_node attr;
5366
5367 attr.dw_attr = attr_kind;
5368 attr.dw_attr_val.val_class = dw_val_class_offset;
5369 attr.dw_attr_val.v.val_offset = offset;
5370 add_dwarf_attr (die, &attr);
5371 }
5372
5373 /* Add an range_list attribute value to a DIE. */
5374
5375 static void
5376 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5377 long unsigned int offset)
5378 {
5379 dw_attr_node attr;
5380
5381 attr.dw_attr = attr_kind;
5382 attr.dw_attr_val.val_class = dw_val_class_range_list;
5383 attr.dw_attr_val.v.val_offset = offset;
5384 add_dwarf_attr (die, &attr);
5385 }
5386
5387 static inline const char *
5388 AT_lbl (dw_attr_ref a)
5389 {
5390 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5391 || AT_class (a) == dw_val_class_lineptr
5392 || AT_class (a) == dw_val_class_macptr));
5393 return a->dw_attr_val.v.val_lbl_id;
5394 }
5395
5396 /* Get the attribute of type attr_kind. */
5397
5398 static dw_attr_ref
5399 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5400 {
5401 dw_attr_ref a;
5402 unsigned ix;
5403 dw_die_ref spec = NULL;
5404
5405 if (! die)
5406 return NULL;
5407
5408 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5409 if (a->dw_attr == attr_kind)
5410 return a;
5411 else if (a->dw_attr == DW_AT_specification
5412 || a->dw_attr == DW_AT_abstract_origin)
5413 spec = AT_ref (a);
5414
5415 if (spec)
5416 return get_AT (spec, attr_kind);
5417
5418 return NULL;
5419 }
5420
5421 /* Return the "low pc" attribute value, typically associated with a subprogram
5422 DIE. Return null if the "low pc" attribute is either not present, or if it
5423 cannot be represented as an assembler label identifier. */
5424
5425 static inline const char *
5426 get_AT_low_pc (dw_die_ref die)
5427 {
5428 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5429
5430 return a ? AT_lbl (a) : NULL;
5431 }
5432
5433 /* Return the "high pc" attribute value, typically associated with a subprogram
5434 DIE. Return null if the "high pc" attribute is either not present, or if it
5435 cannot be represented as an assembler label identifier. */
5436
5437 static inline const char *
5438 get_AT_hi_pc (dw_die_ref die)
5439 {
5440 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5441
5442 return a ? AT_lbl (a) : NULL;
5443 }
5444
5445 /* Return the value of the string attribute designated by ATTR_KIND, or
5446 NULL if it is not present. */
5447
5448 static inline const char *
5449 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5450 {
5451 dw_attr_ref a = get_AT (die, attr_kind);
5452
5453 return a ? AT_string (a) : NULL;
5454 }
5455
5456 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5457 if it is not present. */
5458
5459 static inline int
5460 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5461 {
5462 dw_attr_ref a = get_AT (die, attr_kind);
5463
5464 return a ? AT_flag (a) : 0;
5465 }
5466
5467 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5468 if it is not present. */
5469
5470 static inline unsigned
5471 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5472 {
5473 dw_attr_ref a = get_AT (die, attr_kind);
5474
5475 return a ? AT_unsigned (a) : 0;
5476 }
5477
5478 static inline dw_die_ref
5479 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5480 {
5481 dw_attr_ref a = get_AT (die, attr_kind);
5482
5483 return a ? AT_ref (a) : NULL;
5484 }
5485
5486 static inline struct dwarf_file_data *
5487 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5488 {
5489 dw_attr_ref a = get_AT (die, attr_kind);
5490
5491 return a ? AT_file (a) : NULL;
5492 }
5493
5494 /* Return TRUE if the language is C or C++. */
5495
5496 static inline bool
5497 is_c_family (void)
5498 {
5499 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5500
5501 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5502 || lang == DW_LANG_C99
5503 || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5504 }
5505
5506 /* Return TRUE if the language is C++. */
5507
5508 static inline bool
5509 is_cxx (void)
5510 {
5511 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5512
5513 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5514 }
5515
5516 /* Return TRUE if the language is Fortran. */
5517
5518 static inline bool
5519 is_fortran (void)
5520 {
5521 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5522
5523 return (lang == DW_LANG_Fortran77
5524 || lang == DW_LANG_Fortran90
5525 || lang == DW_LANG_Fortran95);
5526 }
5527
5528 /* Return TRUE if the language is Java. */
5529
5530 static inline bool
5531 is_java (void)
5532 {
5533 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5534
5535 return lang == DW_LANG_Java;
5536 }
5537
5538 /* Return TRUE if the language is Ada. */
5539
5540 static inline bool
5541 is_ada (void)
5542 {
5543 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5544
5545 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5546 }
5547
5548 /* Remove the specified attribute if present. */
5549
5550 static void
5551 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5552 {
5553 dw_attr_ref a;
5554 unsigned ix;
5555
5556 if (! die)
5557 return;
5558
5559 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5560 if (a->dw_attr == attr_kind)
5561 {
5562 if (AT_class (a) == dw_val_class_str)
5563 if (a->dw_attr_val.v.val_str->refcount)
5564 a->dw_attr_val.v.val_str->refcount--;
5565
5566 /* VEC_ordered_remove should help reduce the number of abbrevs
5567 that are needed. */
5568 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5569 return;
5570 }
5571 }
5572
5573 /* Remove CHILD from its parent. PREV must have the property that
5574 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5575
5576 static void
5577 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5578 {
5579 gcc_assert (child->die_parent == prev->die_parent);
5580 gcc_assert (prev->die_sib == child);
5581 if (prev == child)
5582 {
5583 gcc_assert (child->die_parent->die_child == child);
5584 prev = NULL;
5585 }
5586 else
5587 prev->die_sib = child->die_sib;
5588 if (child->die_parent->die_child == child)
5589 child->die_parent->die_child = prev;
5590 }
5591
5592 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5593 matches TAG. */
5594
5595 static void
5596 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5597 {
5598 dw_die_ref c;
5599
5600 c = die->die_child;
5601 if (c) do {
5602 dw_die_ref prev = c;
5603 c = c->die_sib;
5604 while (c->die_tag == tag)
5605 {
5606 remove_child_with_prev (c, prev);
5607 /* Might have removed every child. */
5608 if (c == c->die_sib)
5609 return;
5610 c = c->die_sib;
5611 }
5612 } while (c != die->die_child);
5613 }
5614
5615 /* Add a CHILD_DIE as the last child of DIE. */
5616
5617 static void
5618 add_child_die (dw_die_ref die, dw_die_ref child_die)
5619 {
5620 /* FIXME this should probably be an assert. */
5621 if (! die || ! child_die)
5622 return;
5623 gcc_assert (die != child_die);
5624
5625 child_die->die_parent = die;
5626 if (die->die_child)
5627 {
5628 child_die->die_sib = die->die_child->die_sib;
5629 die->die_child->die_sib = child_die;
5630 }
5631 else
5632 child_die->die_sib = child_die;
5633 die->die_child = child_die;
5634 }
5635
5636 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5637 is the specification, to the end of PARENT's list of children.
5638 This is done by removing and re-adding it. */
5639
5640 static void
5641 splice_child_die (dw_die_ref parent, dw_die_ref child)
5642 {
5643 dw_die_ref p;
5644
5645 /* We want the declaration DIE from inside the class, not the
5646 specification DIE at toplevel. */
5647 if (child->die_parent != parent)
5648 {
5649 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5650
5651 if (tmp)
5652 child = tmp;
5653 }
5654
5655 gcc_assert (child->die_parent == parent
5656 || (child->die_parent
5657 == get_AT_ref (parent, DW_AT_specification)));
5658
5659 for (p = child->die_parent->die_child; ; p = p->die_sib)
5660 if (p->die_sib == child)
5661 {
5662 remove_child_with_prev (child, p);
5663 break;
5664 }
5665
5666 add_child_die (parent, child);
5667 }
5668
5669 /* Return a pointer to a newly created DIE node. */
5670
5671 static inline dw_die_ref
5672 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5673 {
5674 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5675
5676 die->die_tag = tag_value;
5677
5678 if (parent_die != NULL)
5679 add_child_die (parent_die, die);
5680 else
5681 {
5682 limbo_die_node *limbo_node;
5683
5684 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5685 limbo_node->die = die;
5686 limbo_node->created_for = t;
5687 limbo_node->next = limbo_die_list;
5688 limbo_die_list = limbo_node;
5689 }
5690
5691 return die;
5692 }
5693
5694 /* Return the DIE associated with the given type specifier. */
5695
5696 static inline dw_die_ref
5697 lookup_type_die (tree type)
5698 {
5699 return TYPE_SYMTAB_DIE (type);
5700 }
5701
5702 /* Equate a DIE to a given type specifier. */
5703
5704 static inline void
5705 equate_type_number_to_die (tree type, dw_die_ref type_die)
5706 {
5707 TYPE_SYMTAB_DIE (type) = type_die;
5708 }
5709
5710 /* Returns a hash value for X (which really is a die_struct). */
5711
5712 static hashval_t
5713 decl_die_table_hash (const void *x)
5714 {
5715 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5716 }
5717
5718 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5719
5720 static int
5721 decl_die_table_eq (const void *x, const void *y)
5722 {
5723 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5724 }
5725
5726 /* Return the DIE associated with a given declaration. */
5727
5728 static inline dw_die_ref
5729 lookup_decl_die (tree decl)
5730 {
5731 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5732 }
5733
5734 /* Returns a hash value for X (which really is a var_loc_list). */
5735
5736 static hashval_t
5737 decl_loc_table_hash (const void *x)
5738 {
5739 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5740 }
5741
5742 /* Return nonzero if decl_id of var_loc_list X is the same as
5743 UID of decl *Y. */
5744
5745 static int
5746 decl_loc_table_eq (const void *x, const void *y)
5747 {
5748 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5749 }
5750
5751 /* Return the var_loc list associated with a given declaration. */
5752
5753 static inline var_loc_list *
5754 lookup_decl_loc (const_tree decl)
5755 {
5756 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5757 }
5758
5759 /* Equate a DIE to a particular declaration. */
5760
5761 static void
5762 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5763 {
5764 unsigned int decl_id = DECL_UID (decl);
5765 void **slot;
5766
5767 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5768 *slot = decl_die;
5769 decl_die->decl_id = decl_id;
5770 }
5771
5772 /* Add a variable location node to the linked list for DECL. */
5773
5774 static void
5775 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5776 {
5777 unsigned int decl_id = DECL_UID (decl);
5778 var_loc_list *temp;
5779 void **slot;
5780
5781 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5782 if (*slot == NULL)
5783 {
5784 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5785 temp->decl_id = decl_id;
5786 *slot = temp;
5787 }
5788 else
5789 temp = *slot;
5790
5791 if (temp->last)
5792 {
5793 /* If the current location is the same as the end of the list,
5794 and either both or neither of the locations is uninitialized,
5795 we have nothing to do. */
5796 if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5797 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5798 || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5799 != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5800 && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5801 == VAR_INIT_STATUS_UNINITIALIZED)
5802 || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5803 == VAR_INIT_STATUS_UNINITIALIZED))))
5804 {
5805 /* Add LOC to the end of list and update LAST. */
5806 temp->last->next = loc;
5807 temp->last = loc;
5808 }
5809 }
5810 /* Do not add empty location to the beginning of the list. */
5811 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5812 {
5813 temp->first = loc;
5814 temp->last = loc;
5815 }
5816 }
5817 \f
5818 /* Keep track of the number of spaces used to indent the
5819 output of the debugging routines that print the structure of
5820 the DIE internal representation. */
5821 static int print_indent;
5822
5823 /* Indent the line the number of spaces given by print_indent. */
5824
5825 static inline void
5826 print_spaces (FILE *outfile)
5827 {
5828 fprintf (outfile, "%*s", print_indent, "");
5829 }
5830
5831 /* Print the information associated with a given DIE, and its children.
5832 This routine is a debugging aid only. */
5833
5834 static void
5835 print_die (dw_die_ref die, FILE *outfile)
5836 {
5837 dw_attr_ref a;
5838 dw_die_ref c;
5839 unsigned ix;
5840
5841 print_spaces (outfile);
5842 fprintf (outfile, "DIE %4ld: %s\n",
5843 die->die_offset, dwarf_tag_name (die->die_tag));
5844 print_spaces (outfile);
5845 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5846 fprintf (outfile, " offset: %ld\n", die->die_offset);
5847
5848 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5849 {
5850 print_spaces (outfile);
5851 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5852
5853 switch (AT_class (a))
5854 {
5855 case dw_val_class_addr:
5856 fprintf (outfile, "address");
5857 break;
5858 case dw_val_class_offset:
5859 fprintf (outfile, "offset");
5860 break;
5861 case dw_val_class_loc:
5862 fprintf (outfile, "location descriptor");
5863 break;
5864 case dw_val_class_loc_list:
5865 fprintf (outfile, "location list -> label:%s",
5866 AT_loc_list (a)->ll_symbol);
5867 break;
5868 case dw_val_class_range_list:
5869 fprintf (outfile, "range list");
5870 break;
5871 case dw_val_class_const:
5872 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5873 break;
5874 case dw_val_class_unsigned_const:
5875 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5876 break;
5877 case dw_val_class_long_long:
5878 fprintf (outfile, "constant (%lu,%lu)",
5879 a->dw_attr_val.v.val_long_long.hi,
5880 a->dw_attr_val.v.val_long_long.low);
5881 break;
5882 case dw_val_class_vec:
5883 fprintf (outfile, "floating-point or vector constant");
5884 break;
5885 case dw_val_class_flag:
5886 fprintf (outfile, "%u", AT_flag (a));
5887 break;
5888 case dw_val_class_die_ref:
5889 if (AT_ref (a) != NULL)
5890 {
5891 if (AT_ref (a)->die_symbol)
5892 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5893 else
5894 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5895 }
5896 else
5897 fprintf (outfile, "die -> <null>");
5898 break;
5899 case dw_val_class_lbl_id:
5900 case dw_val_class_lineptr:
5901 case dw_val_class_macptr:
5902 fprintf (outfile, "label: %s", AT_lbl (a));
5903 break;
5904 case dw_val_class_str:
5905 if (AT_string (a) != NULL)
5906 fprintf (outfile, "\"%s\"", AT_string (a));
5907 else
5908 fprintf (outfile, "<null>");
5909 break;
5910 case dw_val_class_file:
5911 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5912 AT_file (a)->emitted_number);
5913 break;
5914 default:
5915 break;
5916 }
5917
5918 fprintf (outfile, "\n");
5919 }
5920
5921 if (die->die_child != NULL)
5922 {
5923 print_indent += 4;
5924 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5925 print_indent -= 4;
5926 }
5927 if (print_indent == 0)
5928 fprintf (outfile, "\n");
5929 }
5930
5931 /* Print the contents of the source code line number correspondence table.
5932 This routine is a debugging aid only. */
5933
5934 static void
5935 print_dwarf_line_table (FILE *outfile)
5936 {
5937 unsigned i;
5938 dw_line_info_ref line_info;
5939
5940 fprintf (outfile, "\n\nDWARF source line information\n");
5941 for (i = 1; i < line_info_table_in_use; i++)
5942 {
5943 line_info = &line_info_table[i];
5944 fprintf (outfile, "%5d: %4ld %6ld\n", i,
5945 line_info->dw_file_num,
5946 line_info->dw_line_num);
5947 }
5948
5949 fprintf (outfile, "\n\n");
5950 }
5951
5952 /* Print the information collected for a given DIE. */
5953
5954 void
5955 debug_dwarf_die (dw_die_ref die)
5956 {
5957 print_die (die, stderr);
5958 }
5959
5960 /* Print all DWARF information collected for the compilation unit.
5961 This routine is a debugging aid only. */
5962
5963 void
5964 debug_dwarf (void)
5965 {
5966 print_indent = 0;
5967 print_die (comp_unit_die, stderr);
5968 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5969 print_dwarf_line_table (stderr);
5970 }
5971 \f
5972 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5973 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5974 DIE that marks the start of the DIEs for this include file. */
5975
5976 static dw_die_ref
5977 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5978 {
5979 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5980 dw_die_ref new_unit = gen_compile_unit_die (filename);
5981
5982 new_unit->die_sib = old_unit;
5983 return new_unit;
5984 }
5985
5986 /* Close an include-file CU and reopen the enclosing one. */
5987
5988 static dw_die_ref
5989 pop_compile_unit (dw_die_ref old_unit)
5990 {
5991 dw_die_ref new_unit = old_unit->die_sib;
5992
5993 old_unit->die_sib = NULL;
5994 return new_unit;
5995 }
5996
5997 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5998 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5999
6000 /* Calculate the checksum of a location expression. */
6001
6002 static inline void
6003 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6004 {
6005 CHECKSUM (loc->dw_loc_opc);
6006 CHECKSUM (loc->dw_loc_oprnd1);
6007 CHECKSUM (loc->dw_loc_oprnd2);
6008 }
6009
6010 /* Calculate the checksum of an attribute. */
6011
6012 static void
6013 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6014 {
6015 dw_loc_descr_ref loc;
6016 rtx r;
6017
6018 CHECKSUM (at->dw_attr);
6019
6020 /* We don't care that this was compiled with a different compiler
6021 snapshot; if the output is the same, that's what matters. */
6022 if (at->dw_attr == DW_AT_producer)
6023 return;
6024
6025 switch (AT_class (at))
6026 {
6027 case dw_val_class_const:
6028 CHECKSUM (at->dw_attr_val.v.val_int);
6029 break;
6030 case dw_val_class_unsigned_const:
6031 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6032 break;
6033 case dw_val_class_long_long:
6034 CHECKSUM (at->dw_attr_val.v.val_long_long);
6035 break;
6036 case dw_val_class_vec:
6037 CHECKSUM (at->dw_attr_val.v.val_vec);
6038 break;
6039 case dw_val_class_flag:
6040 CHECKSUM (at->dw_attr_val.v.val_flag);
6041 break;
6042 case dw_val_class_str:
6043 CHECKSUM_STRING (AT_string (at));
6044 break;
6045
6046 case dw_val_class_addr:
6047 r = AT_addr (at);
6048 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6049 CHECKSUM_STRING (XSTR (r, 0));
6050 break;
6051
6052 case dw_val_class_offset:
6053 CHECKSUM (at->dw_attr_val.v.val_offset);
6054 break;
6055
6056 case dw_val_class_loc:
6057 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6058 loc_checksum (loc, ctx);
6059 break;
6060
6061 case dw_val_class_die_ref:
6062 die_checksum (AT_ref (at), ctx, mark);
6063 break;
6064
6065 case dw_val_class_fde_ref:
6066 case dw_val_class_lbl_id:
6067 case dw_val_class_lineptr:
6068 case dw_val_class_macptr:
6069 break;
6070
6071 case dw_val_class_file:
6072 CHECKSUM_STRING (AT_file (at)->filename);
6073 break;
6074
6075 default:
6076 break;
6077 }
6078 }
6079
6080 /* Calculate the checksum of a DIE. */
6081
6082 static void
6083 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6084 {
6085 dw_die_ref c;
6086 dw_attr_ref a;
6087 unsigned ix;
6088
6089 /* To avoid infinite recursion. */
6090 if (die->die_mark)
6091 {
6092 CHECKSUM (die->die_mark);
6093 return;
6094 }
6095 die->die_mark = ++(*mark);
6096
6097 CHECKSUM (die->die_tag);
6098
6099 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6100 attr_checksum (a, ctx, mark);
6101
6102 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6103 }
6104
6105 #undef CHECKSUM
6106 #undef CHECKSUM_STRING
6107
6108 /* Do the location expressions look same? */
6109 static inline int
6110 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6111 {
6112 return loc1->dw_loc_opc == loc2->dw_loc_opc
6113 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6114 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6115 }
6116
6117 /* Do the values look the same? */
6118 static int
6119 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6120 {
6121 dw_loc_descr_ref loc1, loc2;
6122 rtx r1, r2;
6123
6124 if (v1->val_class != v2->val_class)
6125 return 0;
6126
6127 switch (v1->val_class)
6128 {
6129 case dw_val_class_const:
6130 return v1->v.val_int == v2->v.val_int;
6131 case dw_val_class_unsigned_const:
6132 return v1->v.val_unsigned == v2->v.val_unsigned;
6133 case dw_val_class_long_long:
6134 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6135 && v1->v.val_long_long.low == v2->v.val_long_long.low;
6136 case dw_val_class_vec:
6137 if (v1->v.val_vec.length != v2->v.val_vec.length
6138 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6139 return 0;
6140 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6141 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6142 return 0;
6143 return 1;
6144 case dw_val_class_flag:
6145 return v1->v.val_flag == v2->v.val_flag;
6146 case dw_val_class_str:
6147 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6148
6149 case dw_val_class_addr:
6150 r1 = v1->v.val_addr;
6151 r2 = v2->v.val_addr;
6152 if (GET_CODE (r1) != GET_CODE (r2))
6153 return 0;
6154 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6155 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6156
6157 case dw_val_class_offset:
6158 return v1->v.val_offset == v2->v.val_offset;
6159
6160 case dw_val_class_loc:
6161 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6162 loc1 && loc2;
6163 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6164 if (!same_loc_p (loc1, loc2, mark))
6165 return 0;
6166 return !loc1 && !loc2;
6167
6168 case dw_val_class_die_ref:
6169 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6170
6171 case dw_val_class_fde_ref:
6172 case dw_val_class_lbl_id:
6173 case dw_val_class_lineptr:
6174 case dw_val_class_macptr:
6175 return 1;
6176
6177 case dw_val_class_file:
6178 return v1->v.val_file == v2->v.val_file;
6179
6180 default:
6181 return 1;
6182 }
6183 }
6184
6185 /* Do the attributes look the same? */
6186
6187 static int
6188 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6189 {
6190 if (at1->dw_attr != at2->dw_attr)
6191 return 0;
6192
6193 /* We don't care that this was compiled with a different compiler
6194 snapshot; if the output is the same, that's what matters. */
6195 if (at1->dw_attr == DW_AT_producer)
6196 return 1;
6197
6198 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6199 }
6200
6201 /* Do the dies look the same? */
6202
6203 static int
6204 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6205 {
6206 dw_die_ref c1, c2;
6207 dw_attr_ref a1;
6208 unsigned ix;
6209
6210 /* To avoid infinite recursion. */
6211 if (die1->die_mark)
6212 return die1->die_mark == die2->die_mark;
6213 die1->die_mark = die2->die_mark = ++(*mark);
6214
6215 if (die1->die_tag != die2->die_tag)
6216 return 0;
6217
6218 if (VEC_length (dw_attr_node, die1->die_attr)
6219 != VEC_length (dw_attr_node, die2->die_attr))
6220 return 0;
6221
6222 for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6223 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6224 return 0;
6225
6226 c1 = die1->die_child;
6227 c2 = die2->die_child;
6228 if (! c1)
6229 {
6230 if (c2)
6231 return 0;
6232 }
6233 else
6234 for (;;)
6235 {
6236 if (!same_die_p (c1, c2, mark))
6237 return 0;
6238 c1 = c1->die_sib;
6239 c2 = c2->die_sib;
6240 if (c1 == die1->die_child)
6241 {
6242 if (c2 == die2->die_child)
6243 break;
6244 else
6245 return 0;
6246 }
6247 }
6248
6249 return 1;
6250 }
6251
6252 /* Do the dies look the same? Wrapper around same_die_p. */
6253
6254 static int
6255 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6256 {
6257 int mark = 0;
6258 int ret = same_die_p (die1, die2, &mark);
6259
6260 unmark_all_dies (die1);
6261 unmark_all_dies (die2);
6262
6263 return ret;
6264 }
6265
6266 /* The prefix to attach to symbols on DIEs in the current comdat debug
6267 info section. */
6268 static char *comdat_symbol_id;
6269
6270 /* The index of the current symbol within the current comdat CU. */
6271 static unsigned int comdat_symbol_number;
6272
6273 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6274 children, and set comdat_symbol_id accordingly. */
6275
6276 static void
6277 compute_section_prefix (dw_die_ref unit_die)
6278 {
6279 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6280 const char *base = die_name ? lbasename (die_name) : "anonymous";
6281 char *name = alloca (strlen (base) + 64);
6282 char *p;
6283 int i, mark;
6284 unsigned char checksum[16];
6285 struct md5_ctx ctx;
6286
6287 /* Compute the checksum of the DIE, then append part of it as hex digits to
6288 the name filename of the unit. */
6289
6290 md5_init_ctx (&ctx);
6291 mark = 0;
6292 die_checksum (unit_die, &ctx, &mark);
6293 unmark_all_dies (unit_die);
6294 md5_finish_ctx (&ctx, checksum);
6295
6296 sprintf (name, "%s.", base);
6297 clean_symbol_name (name);
6298
6299 p = name + strlen (name);
6300 for (i = 0; i < 4; i++)
6301 {
6302 sprintf (p, "%.2x", checksum[i]);
6303 p += 2;
6304 }
6305
6306 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6307 comdat_symbol_number = 0;
6308 }
6309
6310 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6311
6312 static int
6313 is_type_die (dw_die_ref die)
6314 {
6315 switch (die->die_tag)
6316 {
6317 case DW_TAG_array_type:
6318 case DW_TAG_class_type:
6319 case DW_TAG_interface_type:
6320 case DW_TAG_enumeration_type:
6321 case DW_TAG_pointer_type:
6322 case DW_TAG_reference_type:
6323 case DW_TAG_string_type:
6324 case DW_TAG_structure_type:
6325 case DW_TAG_subroutine_type:
6326 case DW_TAG_union_type:
6327 case DW_TAG_ptr_to_member_type:
6328 case DW_TAG_set_type:
6329 case DW_TAG_subrange_type:
6330 case DW_TAG_base_type:
6331 case DW_TAG_const_type:
6332 case DW_TAG_file_type:
6333 case DW_TAG_packed_type:
6334 case DW_TAG_volatile_type:
6335 case DW_TAG_typedef:
6336 return 1;
6337 default:
6338 return 0;
6339 }
6340 }
6341
6342 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6343 Basically, we want to choose the bits that are likely to be shared between
6344 compilations (types) and leave out the bits that are specific to individual
6345 compilations (functions). */
6346
6347 static int
6348 is_comdat_die (dw_die_ref c)
6349 {
6350 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6351 we do for stabs. The advantage is a greater likelihood of sharing between
6352 objects that don't include headers in the same order (and therefore would
6353 put the base types in a different comdat). jason 8/28/00 */
6354
6355 if (c->die_tag == DW_TAG_base_type)
6356 return 0;
6357
6358 if (c->die_tag == DW_TAG_pointer_type
6359 || c->die_tag == DW_TAG_reference_type
6360 || c->die_tag == DW_TAG_const_type
6361 || c->die_tag == DW_TAG_volatile_type)
6362 {
6363 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6364
6365 return t ? is_comdat_die (t) : 0;
6366 }
6367
6368 return is_type_die (c);
6369 }
6370
6371 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6372 compilation unit. */
6373
6374 static int
6375 is_symbol_die (dw_die_ref c)
6376 {
6377 return (is_type_die (c)
6378 || (get_AT (c, DW_AT_declaration)
6379 && !get_AT (c, DW_AT_specification))
6380 || c->die_tag == DW_TAG_namespace);
6381 }
6382
6383 static char *
6384 gen_internal_sym (const char *prefix)
6385 {
6386 char buf[256];
6387
6388 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6389 return xstrdup (buf);
6390 }
6391
6392 /* Assign symbols to all worthy DIEs under DIE. */
6393
6394 static void
6395 assign_symbol_names (dw_die_ref die)
6396 {
6397 dw_die_ref c;
6398
6399 if (is_symbol_die (die))
6400 {
6401 if (comdat_symbol_id)
6402 {
6403 char *p = alloca (strlen (comdat_symbol_id) + 64);
6404
6405 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6406 comdat_symbol_id, comdat_symbol_number++);
6407 die->die_symbol = xstrdup (p);
6408 }
6409 else
6410 die->die_symbol = gen_internal_sym ("LDIE");
6411 }
6412
6413 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6414 }
6415
6416 struct cu_hash_table_entry
6417 {
6418 dw_die_ref cu;
6419 unsigned min_comdat_num, max_comdat_num;
6420 struct cu_hash_table_entry *next;
6421 };
6422
6423 /* Routines to manipulate hash table of CUs. */
6424 static hashval_t
6425 htab_cu_hash (const void *of)
6426 {
6427 const struct cu_hash_table_entry *entry = of;
6428
6429 return htab_hash_string (entry->cu->die_symbol);
6430 }
6431
6432 static int
6433 htab_cu_eq (const void *of1, const void *of2)
6434 {
6435 const struct cu_hash_table_entry *entry1 = of1;
6436 const struct die_struct *entry2 = of2;
6437
6438 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6439 }
6440
6441 static void
6442 htab_cu_del (void *what)
6443 {
6444 struct cu_hash_table_entry *next, *entry = what;
6445
6446 while (entry)
6447 {
6448 next = entry->next;
6449 free (entry);
6450 entry = next;
6451 }
6452 }
6453
6454 /* Check whether we have already seen this CU and set up SYM_NUM
6455 accordingly. */
6456 static int
6457 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6458 {
6459 struct cu_hash_table_entry dummy;
6460 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6461
6462 dummy.max_comdat_num = 0;
6463
6464 slot = (struct cu_hash_table_entry **)
6465 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6466 INSERT);
6467 entry = *slot;
6468
6469 for (; entry; last = entry, entry = entry->next)
6470 {
6471 if (same_die_p_wrap (cu, entry->cu))
6472 break;
6473 }
6474
6475 if (entry)
6476 {
6477 *sym_num = entry->min_comdat_num;
6478 return 1;
6479 }
6480
6481 entry = XCNEW (struct cu_hash_table_entry);
6482 entry->cu = cu;
6483 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6484 entry->next = *slot;
6485 *slot = entry;
6486
6487 return 0;
6488 }
6489
6490 /* Record SYM_NUM to record of CU in HTABLE. */
6491 static void
6492 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6493 {
6494 struct cu_hash_table_entry **slot, *entry;
6495
6496 slot = (struct cu_hash_table_entry **)
6497 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6498 NO_INSERT);
6499 entry = *slot;
6500
6501 entry->max_comdat_num = sym_num;
6502 }
6503
6504 /* Traverse the DIE (which is always comp_unit_die), and set up
6505 additional compilation units for each of the include files we see
6506 bracketed by BINCL/EINCL. */
6507
6508 static void
6509 break_out_includes (dw_die_ref die)
6510 {
6511 dw_die_ref c;
6512 dw_die_ref unit = NULL;
6513 limbo_die_node *node, **pnode;
6514 htab_t cu_hash_table;
6515
6516 c = die->die_child;
6517 if (c) do {
6518 dw_die_ref prev = c;
6519 c = c->die_sib;
6520 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6521 || (unit && is_comdat_die (c)))
6522 {
6523 dw_die_ref next = c->die_sib;
6524
6525 /* This DIE is for a secondary CU; remove it from the main one. */
6526 remove_child_with_prev (c, prev);
6527
6528 if (c->die_tag == DW_TAG_GNU_BINCL)
6529 unit = push_new_compile_unit (unit, c);
6530 else if (c->die_tag == DW_TAG_GNU_EINCL)
6531 unit = pop_compile_unit (unit);
6532 else
6533 add_child_die (unit, c);
6534 c = next;
6535 if (c == die->die_child)
6536 break;
6537 }
6538 } while (c != die->die_child);
6539
6540 #if 0
6541 /* We can only use this in debugging, since the frontend doesn't check
6542 to make sure that we leave every include file we enter. */
6543 gcc_assert (!unit);
6544 #endif
6545
6546 assign_symbol_names (die);
6547 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6548 for (node = limbo_die_list, pnode = &limbo_die_list;
6549 node;
6550 node = node->next)
6551 {
6552 int is_dupl;
6553
6554 compute_section_prefix (node->die);
6555 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6556 &comdat_symbol_number);
6557 assign_symbol_names (node->die);
6558 if (is_dupl)
6559 *pnode = node->next;
6560 else
6561 {
6562 pnode = &node->next;
6563 record_comdat_symbol_number (node->die, cu_hash_table,
6564 comdat_symbol_number);
6565 }
6566 }
6567 htab_delete (cu_hash_table);
6568 }
6569
6570 /* Traverse the DIE and add a sibling attribute if it may have the
6571 effect of speeding up access to siblings. To save some space,
6572 avoid generating sibling attributes for DIE's without children. */
6573
6574 static void
6575 add_sibling_attributes (dw_die_ref die)
6576 {
6577 dw_die_ref c;
6578
6579 if (! die->die_child)
6580 return;
6581
6582 if (die->die_parent && die != die->die_parent->die_child)
6583 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6584
6585 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6586 }
6587
6588 /* Output all location lists for the DIE and its children. */
6589
6590 static void
6591 output_location_lists (dw_die_ref die)
6592 {
6593 dw_die_ref c;
6594 dw_attr_ref a;
6595 unsigned ix;
6596
6597 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6598 if (AT_class (a) == dw_val_class_loc_list)
6599 output_loc_list (AT_loc_list (a));
6600
6601 FOR_EACH_CHILD (die, c, output_location_lists (c));
6602 }
6603
6604 /* The format of each DIE (and its attribute value pairs) is encoded in an
6605 abbreviation table. This routine builds the abbreviation table and assigns
6606 a unique abbreviation id for each abbreviation entry. The children of each
6607 die are visited recursively. */
6608
6609 static void
6610 build_abbrev_table (dw_die_ref die)
6611 {
6612 unsigned long abbrev_id;
6613 unsigned int n_alloc;
6614 dw_die_ref c;
6615 dw_attr_ref a;
6616 unsigned ix;
6617
6618 /* Scan the DIE references, and mark as external any that refer to
6619 DIEs from other CUs (i.e. those which are not marked). */
6620 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6621 if (AT_class (a) == dw_val_class_die_ref
6622 && AT_ref (a)->die_mark == 0)
6623 {
6624 gcc_assert (AT_ref (a)->die_symbol);
6625
6626 set_AT_ref_external (a, 1);
6627 }
6628
6629 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6630 {
6631 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6632 dw_attr_ref die_a, abbrev_a;
6633 unsigned ix;
6634 bool ok = true;
6635
6636 if (abbrev->die_tag != die->die_tag)
6637 continue;
6638 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6639 continue;
6640
6641 if (VEC_length (dw_attr_node, abbrev->die_attr)
6642 != VEC_length (dw_attr_node, die->die_attr))
6643 continue;
6644
6645 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6646 {
6647 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6648 if ((abbrev_a->dw_attr != die_a->dw_attr)
6649 || (value_format (abbrev_a) != value_format (die_a)))
6650 {
6651 ok = false;
6652 break;
6653 }
6654 }
6655 if (ok)
6656 break;
6657 }
6658
6659 if (abbrev_id >= abbrev_die_table_in_use)
6660 {
6661 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6662 {
6663 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6664 abbrev_die_table = ggc_realloc (abbrev_die_table,
6665 sizeof (dw_die_ref) * n_alloc);
6666
6667 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6668 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6669 abbrev_die_table_allocated = n_alloc;
6670 }
6671
6672 ++abbrev_die_table_in_use;
6673 abbrev_die_table[abbrev_id] = die;
6674 }
6675
6676 die->die_abbrev = abbrev_id;
6677 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6678 }
6679 \f
6680 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6681
6682 static int
6683 constant_size (long unsigned int value)
6684 {
6685 int log;
6686
6687 if (value == 0)
6688 log = 0;
6689 else
6690 log = floor_log2 (value);
6691
6692 log = log / 8;
6693 log = 1 << (floor_log2 (log) + 1);
6694
6695 return log;
6696 }
6697
6698 /* Return the size of a DIE as it is represented in the
6699 .debug_info section. */
6700
6701 static unsigned long
6702 size_of_die (dw_die_ref die)
6703 {
6704 unsigned long size = 0;
6705 dw_attr_ref a;
6706 unsigned ix;
6707
6708 size += size_of_uleb128 (die->die_abbrev);
6709 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6710 {
6711 switch (AT_class (a))
6712 {
6713 case dw_val_class_addr:
6714 size += DWARF2_ADDR_SIZE;
6715 break;
6716 case dw_val_class_offset:
6717 size += DWARF_OFFSET_SIZE;
6718 break;
6719 case dw_val_class_loc:
6720 {
6721 unsigned long lsize = size_of_locs (AT_loc (a));
6722
6723 /* Block length. */
6724 size += constant_size (lsize);
6725 size += lsize;
6726 }
6727 break;
6728 case dw_val_class_loc_list:
6729 size += DWARF_OFFSET_SIZE;
6730 break;
6731 case dw_val_class_range_list:
6732 size += DWARF_OFFSET_SIZE;
6733 break;
6734 case dw_val_class_const:
6735 size += size_of_sleb128 (AT_int (a));
6736 break;
6737 case dw_val_class_unsigned_const:
6738 size += constant_size (AT_unsigned (a));
6739 break;
6740 case dw_val_class_long_long:
6741 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6742 break;
6743 case dw_val_class_vec:
6744 size += 1 + (a->dw_attr_val.v.val_vec.length
6745 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6746 break;
6747 case dw_val_class_flag:
6748 size += 1;
6749 break;
6750 case dw_val_class_die_ref:
6751 if (AT_ref_external (a))
6752 size += DWARF2_ADDR_SIZE;
6753 else
6754 size += DWARF_OFFSET_SIZE;
6755 break;
6756 case dw_val_class_fde_ref:
6757 size += DWARF_OFFSET_SIZE;
6758 break;
6759 case dw_val_class_lbl_id:
6760 size += DWARF2_ADDR_SIZE;
6761 break;
6762 case dw_val_class_lineptr:
6763 case dw_val_class_macptr:
6764 size += DWARF_OFFSET_SIZE;
6765 break;
6766 case dw_val_class_str:
6767 if (AT_string_form (a) == DW_FORM_strp)
6768 size += DWARF_OFFSET_SIZE;
6769 else
6770 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6771 break;
6772 case dw_val_class_file:
6773 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6774 break;
6775 default:
6776 gcc_unreachable ();
6777 }
6778 }
6779
6780 return size;
6781 }
6782
6783 /* Size the debugging information associated with a given DIE. Visits the
6784 DIE's children recursively. Updates the global variable next_die_offset, on
6785 each time through. Uses the current value of next_die_offset to update the
6786 die_offset field in each DIE. */
6787
6788 static void
6789 calc_die_sizes (dw_die_ref die)
6790 {
6791 dw_die_ref c;
6792
6793 die->die_offset = next_die_offset;
6794 next_die_offset += size_of_die (die);
6795
6796 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6797
6798 if (die->die_child != NULL)
6799 /* Count the null byte used to terminate sibling lists. */
6800 next_die_offset += 1;
6801 }
6802
6803 /* Set the marks for a die and its children. We do this so
6804 that we know whether or not a reference needs to use FORM_ref_addr; only
6805 DIEs in the same CU will be marked. We used to clear out the offset
6806 and use that as the flag, but ran into ordering problems. */
6807
6808 static void
6809 mark_dies (dw_die_ref die)
6810 {
6811 dw_die_ref c;
6812
6813 gcc_assert (!die->die_mark);
6814
6815 die->die_mark = 1;
6816 FOR_EACH_CHILD (die, c, mark_dies (c));
6817 }
6818
6819 /* Clear the marks for a die and its children. */
6820
6821 static void
6822 unmark_dies (dw_die_ref die)
6823 {
6824 dw_die_ref c;
6825
6826 gcc_assert (die->die_mark);
6827
6828 die->die_mark = 0;
6829 FOR_EACH_CHILD (die, c, unmark_dies (c));
6830 }
6831
6832 /* Clear the marks for a die, its children and referred dies. */
6833
6834 static void
6835 unmark_all_dies (dw_die_ref die)
6836 {
6837 dw_die_ref c;
6838 dw_attr_ref a;
6839 unsigned ix;
6840
6841 if (!die->die_mark)
6842 return;
6843 die->die_mark = 0;
6844
6845 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6846
6847 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6848 if (AT_class (a) == dw_val_class_die_ref)
6849 unmark_all_dies (AT_ref (a));
6850 }
6851
6852 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6853 generated for the compilation unit. */
6854
6855 static unsigned long
6856 size_of_pubnames (VEC (pubname_entry, gc) * names)
6857 {
6858 unsigned long size;
6859 unsigned i;
6860 pubname_ref p;
6861
6862 size = DWARF_PUBNAMES_HEADER_SIZE;
6863 for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6864 if (names != pubtype_table
6865 || p->die->die_offset != 0
6866 || !flag_eliminate_unused_debug_types)
6867 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6868
6869 size += DWARF_OFFSET_SIZE;
6870 return size;
6871 }
6872
6873 /* Return the size of the information in the .debug_aranges section. */
6874
6875 static unsigned long
6876 size_of_aranges (void)
6877 {
6878 unsigned long size;
6879
6880 size = DWARF_ARANGES_HEADER_SIZE;
6881
6882 /* Count the address/length pair for this compilation unit. */
6883 if (text_section_used)
6884 size += 2 * DWARF2_ADDR_SIZE;
6885 if (cold_text_section_used)
6886 size += 2 * DWARF2_ADDR_SIZE;
6887 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6888
6889 /* Count the two zero words used to terminated the address range table. */
6890 size += 2 * DWARF2_ADDR_SIZE;
6891 return size;
6892 }
6893 \f
6894 /* Select the encoding of an attribute value. */
6895
6896 static enum dwarf_form
6897 value_format (dw_attr_ref a)
6898 {
6899 switch (a->dw_attr_val.val_class)
6900 {
6901 case dw_val_class_addr:
6902 return DW_FORM_addr;
6903 case dw_val_class_range_list:
6904 case dw_val_class_offset:
6905 case dw_val_class_loc_list:
6906 switch (DWARF_OFFSET_SIZE)
6907 {
6908 case 4:
6909 return DW_FORM_data4;
6910 case 8:
6911 return DW_FORM_data8;
6912 default:
6913 gcc_unreachable ();
6914 }
6915 case dw_val_class_loc:
6916 switch (constant_size (size_of_locs (AT_loc (a))))
6917 {
6918 case 1:
6919 return DW_FORM_block1;
6920 case 2:
6921 return DW_FORM_block2;
6922 default:
6923 gcc_unreachable ();
6924 }
6925 case dw_val_class_const:
6926 return DW_FORM_sdata;
6927 case dw_val_class_unsigned_const:
6928 switch (constant_size (AT_unsigned (a)))
6929 {
6930 case 1:
6931 return DW_FORM_data1;
6932 case 2:
6933 return DW_FORM_data2;
6934 case 4:
6935 return DW_FORM_data4;
6936 case 8:
6937 return DW_FORM_data8;
6938 default:
6939 gcc_unreachable ();
6940 }
6941 case dw_val_class_long_long:
6942 return DW_FORM_block1;
6943 case dw_val_class_vec:
6944 return DW_FORM_block1;
6945 case dw_val_class_flag:
6946 return DW_FORM_flag;
6947 case dw_val_class_die_ref:
6948 if (AT_ref_external (a))
6949 return DW_FORM_ref_addr;
6950 else
6951 return DW_FORM_ref;
6952 case dw_val_class_fde_ref:
6953 return DW_FORM_data;
6954 case dw_val_class_lbl_id:
6955 return DW_FORM_addr;
6956 case dw_val_class_lineptr:
6957 case dw_val_class_macptr:
6958 return DW_FORM_data;
6959 case dw_val_class_str:
6960 return AT_string_form (a);
6961 case dw_val_class_file:
6962 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6963 {
6964 case 1:
6965 return DW_FORM_data1;
6966 case 2:
6967 return DW_FORM_data2;
6968 case 4:
6969 return DW_FORM_data4;
6970 default:
6971 gcc_unreachable ();
6972 }
6973
6974 default:
6975 gcc_unreachable ();
6976 }
6977 }
6978
6979 /* Output the encoding of an attribute value. */
6980
6981 static void
6982 output_value_format (dw_attr_ref a)
6983 {
6984 enum dwarf_form form = value_format (a);
6985
6986 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6987 }
6988
6989 /* Output the .debug_abbrev section which defines the DIE abbreviation
6990 table. */
6991
6992 static void
6993 output_abbrev_section (void)
6994 {
6995 unsigned long abbrev_id;
6996
6997 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6998 {
6999 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7000 unsigned ix;
7001 dw_attr_ref a_attr;
7002
7003 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7004 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7005 dwarf_tag_name (abbrev->die_tag));
7006
7007 if (abbrev->die_child != NULL)
7008 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7009 else
7010 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7011
7012 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7013 ix++)
7014 {
7015 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7016 dwarf_attr_name (a_attr->dw_attr));
7017 output_value_format (a_attr);
7018 }
7019
7020 dw2_asm_output_data (1, 0, NULL);
7021 dw2_asm_output_data (1, 0, NULL);
7022 }
7023
7024 /* Terminate the table. */
7025 dw2_asm_output_data (1, 0, NULL);
7026 }
7027
7028 /* Output a symbol we can use to refer to this DIE from another CU. */
7029
7030 static inline void
7031 output_die_symbol (dw_die_ref die)
7032 {
7033 char *sym = die->die_symbol;
7034
7035 if (sym == 0)
7036 return;
7037
7038 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7039 /* We make these global, not weak; if the target doesn't support
7040 .linkonce, it doesn't support combining the sections, so debugging
7041 will break. */
7042 targetm.asm_out.globalize_label (asm_out_file, sym);
7043
7044 ASM_OUTPUT_LABEL (asm_out_file, sym);
7045 }
7046
7047 /* Return a new location list, given the begin and end range, and the
7048 expression. gensym tells us whether to generate a new internal symbol for
7049 this location list node, which is done for the head of the list only. */
7050
7051 static inline dw_loc_list_ref
7052 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7053 const char *section, unsigned int gensym)
7054 {
7055 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7056
7057 retlist->begin = begin;
7058 retlist->end = end;
7059 retlist->expr = expr;
7060 retlist->section = section;
7061 if (gensym)
7062 retlist->ll_symbol = gen_internal_sym ("LLST");
7063
7064 return retlist;
7065 }
7066
7067 /* Add a location description expression to a location list. */
7068
7069 static inline void
7070 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7071 const char *begin, const char *end,
7072 const char *section)
7073 {
7074 dw_loc_list_ref *d;
7075
7076 /* Find the end of the chain. */
7077 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7078 ;
7079
7080 /* Add a new location list node to the list. */
7081 *d = new_loc_list (descr, begin, end, section, 0);
7082 }
7083
7084 /* Note that the current function section is being used for code. */
7085
7086 static void
7087 dwarf2out_note_section_used (void)
7088 {
7089 section *sec = current_function_section ();
7090 if (sec == text_section)
7091 text_section_used = true;
7092 else if (sec == cold_text_section)
7093 cold_text_section_used = true;
7094 }
7095
7096 void
7097 dwarf2out_switch_text_section (void)
7098 {
7099 dw_fde_ref fde;
7100
7101 gcc_assert (cfun);
7102
7103 fde = &fde_table[fde_table_in_use - 1];
7104 fde->dw_fde_switched_sections = true;
7105 fde->dw_fde_hot_section_label = cfun->hot_section_label;
7106 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7107 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7108 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7109 have_multiple_function_sections = true;
7110
7111 /* Reset the current label on switching text sections, so that we
7112 don't attempt to advance_loc4 between labels in different sections. */
7113 fde->dw_fde_current_label = NULL;
7114
7115 dwarf2out_note_section_used ();
7116 }
7117
7118 /* Output the location list given to us. */
7119
7120 static void
7121 output_loc_list (dw_loc_list_ref list_head)
7122 {
7123 dw_loc_list_ref curr = list_head;
7124
7125 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7126
7127 /* Walk the location list, and output each range + expression. */
7128 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7129 {
7130 unsigned long size;
7131 /* Don't output an entry that starts and ends at the same address. */
7132 if (strcmp (curr->begin, curr->end) == 0)
7133 continue;
7134 if (!have_multiple_function_sections)
7135 {
7136 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7137 "Location list begin address (%s)",
7138 list_head->ll_symbol);
7139 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7140 "Location list end address (%s)",
7141 list_head->ll_symbol);
7142 }
7143 else
7144 {
7145 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7146 "Location list begin address (%s)",
7147 list_head->ll_symbol);
7148 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7149 "Location list end address (%s)",
7150 list_head->ll_symbol);
7151 }
7152 size = size_of_locs (curr->expr);
7153
7154 /* Output the block length for this list of location operations. */
7155 gcc_assert (size <= 0xffff);
7156 dw2_asm_output_data (2, size, "%s", "Location expression size");
7157
7158 output_loc_sequence (curr->expr);
7159 }
7160
7161 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7162 "Location list terminator begin (%s)",
7163 list_head->ll_symbol);
7164 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7165 "Location list terminator end (%s)",
7166 list_head->ll_symbol);
7167 }
7168
7169 /* Output the DIE and its attributes. Called recursively to generate
7170 the definitions of each child DIE. */
7171
7172 static void
7173 output_die (dw_die_ref die)
7174 {
7175 dw_attr_ref a;
7176 dw_die_ref c;
7177 unsigned long size;
7178 unsigned ix;
7179
7180 /* If someone in another CU might refer to us, set up a symbol for
7181 them to point to. */
7182 if (die->die_symbol)
7183 output_die_symbol (die);
7184
7185 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7186 (unsigned long)die->die_offset,
7187 dwarf_tag_name (die->die_tag));
7188
7189 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7190 {
7191 const char *name = dwarf_attr_name (a->dw_attr);
7192
7193 switch (AT_class (a))
7194 {
7195 case dw_val_class_addr:
7196 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7197 break;
7198
7199 case dw_val_class_offset:
7200 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7201 "%s", name);
7202 break;
7203
7204 case dw_val_class_range_list:
7205 {
7206 char *p = strchr (ranges_section_label, '\0');
7207
7208 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7209 a->dw_attr_val.v.val_offset);
7210 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7211 debug_ranges_section, "%s", name);
7212 *p = '\0';
7213 }
7214 break;
7215
7216 case dw_val_class_loc:
7217 size = size_of_locs (AT_loc (a));
7218
7219 /* Output the block length for this list of location operations. */
7220 dw2_asm_output_data (constant_size (size), size, "%s", name);
7221
7222 output_loc_sequence (AT_loc (a));
7223 break;
7224
7225 case dw_val_class_const:
7226 /* ??? It would be slightly more efficient to use a scheme like is
7227 used for unsigned constants below, but gdb 4.x does not sign
7228 extend. Gdb 5.x does sign extend. */
7229 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7230 break;
7231
7232 case dw_val_class_unsigned_const:
7233 dw2_asm_output_data (constant_size (AT_unsigned (a)),
7234 AT_unsigned (a), "%s", name);
7235 break;
7236
7237 case dw_val_class_long_long:
7238 {
7239 unsigned HOST_WIDE_INT first, second;
7240
7241 dw2_asm_output_data (1,
7242 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7243 "%s", name);
7244
7245 if (WORDS_BIG_ENDIAN)
7246 {
7247 first = a->dw_attr_val.v.val_long_long.hi;
7248 second = a->dw_attr_val.v.val_long_long.low;
7249 }
7250 else
7251 {
7252 first = a->dw_attr_val.v.val_long_long.low;
7253 second = a->dw_attr_val.v.val_long_long.hi;
7254 }
7255
7256 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7257 first, "long long constant");
7258 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7259 second, NULL);
7260 }
7261 break;
7262
7263 case dw_val_class_vec:
7264 {
7265 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7266 unsigned int len = a->dw_attr_val.v.val_vec.length;
7267 unsigned int i;
7268 unsigned char *p;
7269
7270 dw2_asm_output_data (1, len * elt_size, "%s", name);
7271 if (elt_size > sizeof (HOST_WIDE_INT))
7272 {
7273 elt_size /= 2;
7274 len *= 2;
7275 }
7276 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7277 i < len;
7278 i++, p += elt_size)
7279 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7280 "fp or vector constant word %u", i);
7281 break;
7282 }
7283
7284 case dw_val_class_flag:
7285 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7286 break;
7287
7288 case dw_val_class_loc_list:
7289 {
7290 char *sym = AT_loc_list (a)->ll_symbol;
7291
7292 gcc_assert (sym);
7293 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7294 "%s", name);
7295 }
7296 break;
7297
7298 case dw_val_class_die_ref:
7299 if (AT_ref_external (a))
7300 {
7301 char *sym = AT_ref (a)->die_symbol;
7302
7303 gcc_assert (sym);
7304 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7305 "%s", name);
7306 }
7307 else
7308 {
7309 gcc_assert (AT_ref (a)->die_offset);
7310 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7311 "%s", name);
7312 }
7313 break;
7314
7315 case dw_val_class_fde_ref:
7316 {
7317 char l1[20];
7318
7319 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7320 a->dw_attr_val.v.val_fde_index * 2);
7321 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7322 "%s", name);
7323 }
7324 break;
7325
7326 case dw_val_class_lbl_id:
7327 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7328 break;
7329
7330 case dw_val_class_lineptr:
7331 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7332 debug_line_section, "%s", name);
7333 break;
7334
7335 case dw_val_class_macptr:
7336 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7337 debug_macinfo_section, "%s", name);
7338 break;
7339
7340 case dw_val_class_str:
7341 if (AT_string_form (a) == DW_FORM_strp)
7342 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7343 a->dw_attr_val.v.val_str->label,
7344 debug_str_section,
7345 "%s: \"%s\"", name, AT_string (a));
7346 else
7347 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7348 break;
7349
7350 case dw_val_class_file:
7351 {
7352 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7353
7354 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7355 a->dw_attr_val.v.val_file->filename);
7356 break;
7357 }
7358
7359 default:
7360 gcc_unreachable ();
7361 }
7362 }
7363
7364 FOR_EACH_CHILD (die, c, output_die (c));
7365
7366 /* Add null byte to terminate sibling list. */
7367 if (die->die_child != NULL)
7368 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7369 (unsigned long) die->die_offset);
7370 }
7371
7372 /* Output the compilation unit that appears at the beginning of the
7373 .debug_info section, and precedes the DIE descriptions. */
7374
7375 static void
7376 output_compilation_unit_header (void)
7377 {
7378 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7379 dw2_asm_output_data (4, 0xffffffff,
7380 "Initial length escape value indicating 64-bit DWARF extension");
7381 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7382 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7383 "Length of Compilation Unit Info");
7384 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7385 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7386 debug_abbrev_section,
7387 "Offset Into Abbrev. Section");
7388 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7389 }
7390
7391 /* Output the compilation unit DIE and its children. */
7392
7393 static void
7394 output_comp_unit (dw_die_ref die, int output_if_empty)
7395 {
7396 const char *secname;
7397 char *oldsym, *tmp;
7398
7399 /* Unless we are outputting main CU, we may throw away empty ones. */
7400 if (!output_if_empty && die->die_child == NULL)
7401 return;
7402
7403 /* Even if there are no children of this DIE, we must output the information
7404 about the compilation unit. Otherwise, on an empty translation unit, we
7405 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7406 will then complain when examining the file. First mark all the DIEs in
7407 this CU so we know which get local refs. */
7408 mark_dies (die);
7409
7410 build_abbrev_table (die);
7411
7412 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7413 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7414 calc_die_sizes (die);
7415
7416 oldsym = die->die_symbol;
7417 if (oldsym)
7418 {
7419 tmp = alloca (strlen (oldsym) + 24);
7420
7421 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7422 secname = tmp;
7423 die->die_symbol = NULL;
7424 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7425 }
7426 else
7427 switch_to_section (debug_info_section);
7428
7429 /* Output debugging information. */
7430 output_compilation_unit_header ();
7431 output_die (die);
7432
7433 /* Leave the marks on the main CU, so we can check them in
7434 output_pubnames. */
7435 if (oldsym)
7436 {
7437 unmark_dies (die);
7438 die->die_symbol = oldsym;
7439 }
7440 }
7441
7442 /* Return the DWARF2/3 pubname associated with a decl. */
7443
7444 static const char *
7445 dwarf2_name (tree decl, int scope)
7446 {
7447 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7448 }
7449
7450 /* Add a new entry to .debug_pubnames if appropriate. */
7451
7452 static void
7453 add_pubname (tree decl, dw_die_ref die)
7454 {
7455 pubname_entry e;
7456
7457 if (! TREE_PUBLIC (decl))
7458 return;
7459
7460 e.die = die;
7461 e.name = xstrdup (dwarf2_name (decl, 1));
7462 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7463 }
7464
7465 /* Add a new entry to .debug_pubtypes if appropriate. */
7466
7467 static void
7468 add_pubtype (tree decl, dw_die_ref die)
7469 {
7470 pubname_entry e;
7471
7472 e.name = NULL;
7473 if ((TREE_PUBLIC (decl)
7474 || die->die_parent == comp_unit_die)
7475 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7476 {
7477 e.die = die;
7478 if (TYPE_P (decl))
7479 {
7480 if (TYPE_NAME (decl))
7481 {
7482 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7483 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7484 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7485 && DECL_NAME (TYPE_NAME (decl)))
7486 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7487 else
7488 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7489 }
7490 }
7491 else
7492 e.name = xstrdup (dwarf2_name (decl, 1));
7493
7494 /* If we don't have a name for the type, there's no point in adding
7495 it to the table. */
7496 if (e.name && e.name[0] != '\0')
7497 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7498 }
7499 }
7500
7501 /* Output the public names table used to speed up access to externally
7502 visible names; or the public types table used to find type definitions. */
7503
7504 static void
7505 output_pubnames (VEC (pubname_entry, gc) * names)
7506 {
7507 unsigned i;
7508 unsigned long pubnames_length = size_of_pubnames (names);
7509 pubname_ref pub;
7510
7511 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7512 dw2_asm_output_data (4, 0xffffffff,
7513 "Initial length escape value indicating 64-bit DWARF extension");
7514 if (names == pubname_table)
7515 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7516 "Length of Public Names Info");
7517 else
7518 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7519 "Length of Public Type Names Info");
7520 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7521 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7522 debug_info_section,
7523 "Offset of Compilation Unit Info");
7524 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7525 "Compilation Unit Length");
7526
7527 for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7528 {
7529 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7530 if (names == pubname_table)
7531 gcc_assert (pub->die->die_mark);
7532
7533 if (names != pubtype_table
7534 || pub->die->die_offset != 0
7535 || !flag_eliminate_unused_debug_types)
7536 {
7537 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7538 "DIE offset");
7539
7540 dw2_asm_output_nstring (pub->name, -1, "external name");
7541 }
7542 }
7543
7544 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7545 }
7546
7547 /* Add a new entry to .debug_aranges if appropriate. */
7548
7549 static void
7550 add_arange (tree decl, dw_die_ref die)
7551 {
7552 if (! DECL_SECTION_NAME (decl))
7553 return;
7554
7555 if (arange_table_in_use == arange_table_allocated)
7556 {
7557 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7558 arange_table = ggc_realloc (arange_table,
7559 (arange_table_allocated
7560 * sizeof (dw_die_ref)));
7561 memset (arange_table + arange_table_in_use, 0,
7562 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7563 }
7564
7565 arange_table[arange_table_in_use++] = die;
7566 }
7567
7568 /* Output the information that goes into the .debug_aranges table.
7569 Namely, define the beginning and ending address range of the
7570 text section generated for this compilation unit. */
7571
7572 static void
7573 output_aranges (void)
7574 {
7575 unsigned i;
7576 unsigned long aranges_length = size_of_aranges ();
7577
7578 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7579 dw2_asm_output_data (4, 0xffffffff,
7580 "Initial length escape value indicating 64-bit DWARF extension");
7581 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7582 "Length of Address Ranges Info");
7583 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7584 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7585 debug_info_section,
7586 "Offset of Compilation Unit Info");
7587 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7588 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7589
7590 /* We need to align to twice the pointer size here. */
7591 if (DWARF_ARANGES_PAD_SIZE)
7592 {
7593 /* Pad using a 2 byte words so that padding is correct for any
7594 pointer size. */
7595 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7596 2 * DWARF2_ADDR_SIZE);
7597 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7598 dw2_asm_output_data (2, 0, NULL);
7599 }
7600
7601 /* It is necessary not to output these entries if the sections were
7602 not used; if the sections were not used, the length will be 0 and
7603 the address may end up as 0 if the section is discarded by ld
7604 --gc-sections, leaving an invalid (0, 0) entry that can be
7605 confused with the terminator. */
7606 if (text_section_used)
7607 {
7608 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7609 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7610 text_section_label, "Length");
7611 }
7612 if (cold_text_section_used)
7613 {
7614 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7615 "Address");
7616 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7617 cold_text_section_label, "Length");
7618 }
7619
7620 for (i = 0; i < arange_table_in_use; i++)
7621 {
7622 dw_die_ref die = arange_table[i];
7623
7624 /* We shouldn't see aranges for DIEs outside of the main CU. */
7625 gcc_assert (die->die_mark);
7626
7627 if (die->die_tag == DW_TAG_subprogram)
7628 {
7629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7630 "Address");
7631 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7632 get_AT_low_pc (die), "Length");
7633 }
7634 else
7635 {
7636 /* A static variable; extract the symbol from DW_AT_location.
7637 Note that this code isn't currently hit, as we only emit
7638 aranges for functions (jason 9/23/99). */
7639 dw_attr_ref a = get_AT (die, DW_AT_location);
7640 dw_loc_descr_ref loc;
7641
7642 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7643
7644 loc = AT_loc (a);
7645 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7646
7647 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7648 loc->dw_loc_oprnd1.v.val_addr, "Address");
7649 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7650 get_AT_unsigned (die, DW_AT_byte_size),
7651 "Length");
7652 }
7653 }
7654
7655 /* Output the terminator words. */
7656 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7657 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7658 }
7659
7660 /* Add a new entry to .debug_ranges. Return the offset at which it
7661 was placed. */
7662
7663 static unsigned int
7664 add_ranges_num (int num)
7665 {
7666 unsigned int in_use = ranges_table_in_use;
7667
7668 if (in_use == ranges_table_allocated)
7669 {
7670 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7671 ranges_table
7672 = ggc_realloc (ranges_table, (ranges_table_allocated
7673 * sizeof (struct dw_ranges_struct)));
7674 memset (ranges_table + ranges_table_in_use, 0,
7675 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7676 }
7677
7678 ranges_table[in_use].num = num;
7679 ranges_table_in_use = in_use + 1;
7680
7681 return in_use * 2 * DWARF2_ADDR_SIZE;
7682 }
7683
7684 /* Add a new entry to .debug_ranges corresponding to a block, or a
7685 range terminator if BLOCK is NULL. */
7686
7687 static unsigned int
7688 add_ranges (const_tree block)
7689 {
7690 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7691 }
7692
7693 /* Add a new entry to .debug_ranges corresponding to a pair of
7694 labels. */
7695
7696 static unsigned int
7697 add_ranges_by_labels (const char *begin, const char *end)
7698 {
7699 unsigned int in_use = ranges_by_label_in_use;
7700
7701 if (in_use == ranges_by_label_allocated)
7702 {
7703 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7704 ranges_by_label
7705 = ggc_realloc (ranges_by_label,
7706 (ranges_by_label_allocated
7707 * sizeof (struct dw_ranges_by_label_struct)));
7708 memset (ranges_by_label + ranges_by_label_in_use, 0,
7709 RANGES_TABLE_INCREMENT
7710 * sizeof (struct dw_ranges_by_label_struct));
7711 }
7712
7713 ranges_by_label[in_use].begin = begin;
7714 ranges_by_label[in_use].end = end;
7715 ranges_by_label_in_use = in_use + 1;
7716
7717 return add_ranges_num (-(int)in_use - 1);
7718 }
7719
7720 static void
7721 output_ranges (void)
7722 {
7723 unsigned i;
7724 static const char *const start_fmt = "Offset 0x%x";
7725 const char *fmt = start_fmt;
7726
7727 for (i = 0; i < ranges_table_in_use; i++)
7728 {
7729 int block_num = ranges_table[i].num;
7730
7731 if (block_num > 0)
7732 {
7733 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7734 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7735
7736 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7737 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7738
7739 /* If all code is in the text section, then the compilation
7740 unit base address defaults to DW_AT_low_pc, which is the
7741 base of the text section. */
7742 if (!have_multiple_function_sections)
7743 {
7744 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7745 text_section_label,
7746 fmt, i * 2 * DWARF2_ADDR_SIZE);
7747 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7748 text_section_label, NULL);
7749 }
7750
7751 /* Otherwise, the compilation unit base address is zero,
7752 which allows us to use absolute addresses, and not worry
7753 about whether the target supports cross-section
7754 arithmetic. */
7755 else
7756 {
7757 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7758 fmt, i * 2 * DWARF2_ADDR_SIZE);
7759 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7760 }
7761
7762 fmt = NULL;
7763 }
7764
7765 /* Negative block_num stands for an index into ranges_by_label. */
7766 else if (block_num < 0)
7767 {
7768 int lab_idx = - block_num - 1;
7769
7770 if (!have_multiple_function_sections)
7771 {
7772 gcc_unreachable ();
7773 #if 0
7774 /* If we ever use add_ranges_by_labels () for a single
7775 function section, all we have to do is to take out
7776 the #if 0 above. */
7777 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7778 ranges_by_label[lab_idx].begin,
7779 text_section_label,
7780 fmt, i * 2 * DWARF2_ADDR_SIZE);
7781 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7782 ranges_by_label[lab_idx].end,
7783 text_section_label, NULL);
7784 #endif
7785 }
7786 else
7787 {
7788 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7789 ranges_by_label[lab_idx].begin,
7790 fmt, i * 2 * DWARF2_ADDR_SIZE);
7791 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7792 ranges_by_label[lab_idx].end,
7793 NULL);
7794 }
7795 }
7796 else
7797 {
7798 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7799 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7800 fmt = start_fmt;
7801 }
7802 }
7803 }
7804
7805 /* Data structure containing information about input files. */
7806 struct file_info
7807 {
7808 const char *path; /* Complete file name. */
7809 const char *fname; /* File name part. */
7810 int length; /* Length of entire string. */
7811 struct dwarf_file_data * file_idx; /* Index in input file table. */
7812 int dir_idx; /* Index in directory table. */
7813 };
7814
7815 /* Data structure containing information about directories with source
7816 files. */
7817 struct dir_info
7818 {
7819 const char *path; /* Path including directory name. */
7820 int length; /* Path length. */
7821 int prefix; /* Index of directory entry which is a prefix. */
7822 int count; /* Number of files in this directory. */
7823 int dir_idx; /* Index of directory used as base. */
7824 };
7825
7826 /* Callback function for file_info comparison. We sort by looking at
7827 the directories in the path. */
7828
7829 static int
7830 file_info_cmp (const void *p1, const void *p2)
7831 {
7832 const struct file_info *s1 = p1;
7833 const struct file_info *s2 = p2;
7834 const unsigned char *cp1;
7835 const unsigned char *cp2;
7836
7837 /* Take care of file names without directories. We need to make sure that
7838 we return consistent values to qsort since some will get confused if
7839 we return the same value when identical operands are passed in opposite
7840 orders. So if neither has a directory, return 0 and otherwise return
7841 1 or -1 depending on which one has the directory. */
7842 if ((s1->path == s1->fname || s2->path == s2->fname))
7843 return (s2->path == s2->fname) - (s1->path == s1->fname);
7844
7845 cp1 = (const unsigned char *) s1->path;
7846 cp2 = (const unsigned char *) s2->path;
7847
7848 while (1)
7849 {
7850 ++cp1;
7851 ++cp2;
7852 /* Reached the end of the first path? If so, handle like above. */
7853 if ((cp1 == (const unsigned char *) s1->fname)
7854 || (cp2 == (const unsigned char *) s2->fname))
7855 return ((cp2 == (const unsigned char *) s2->fname)
7856 - (cp1 == (const unsigned char *) s1->fname));
7857
7858 /* Character of current path component the same? */
7859 else if (*cp1 != *cp2)
7860 return *cp1 - *cp2;
7861 }
7862 }
7863
7864 struct file_name_acquire_data
7865 {
7866 struct file_info *files;
7867 int used_files;
7868 int max_files;
7869 };
7870
7871 /* Traversal function for the hash table. */
7872
7873 static int
7874 file_name_acquire (void ** slot, void *data)
7875 {
7876 struct file_name_acquire_data *fnad = data;
7877 struct dwarf_file_data *d = *slot;
7878 struct file_info *fi;
7879 const char *f;
7880
7881 gcc_assert (fnad->max_files >= d->emitted_number);
7882
7883 if (! d->emitted_number)
7884 return 1;
7885
7886 gcc_assert (fnad->max_files != fnad->used_files);
7887
7888 fi = fnad->files + fnad->used_files++;
7889
7890 /* Skip all leading "./". */
7891 f = d->filename;
7892 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7893 f += 2;
7894
7895 /* Create a new array entry. */
7896 fi->path = f;
7897 fi->length = strlen (f);
7898 fi->file_idx = d;
7899
7900 /* Search for the file name part. */
7901 f = strrchr (f, DIR_SEPARATOR);
7902 #if defined (DIR_SEPARATOR_2)
7903 {
7904 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7905
7906 if (g != NULL)
7907 {
7908 if (f == NULL || f < g)
7909 f = g;
7910 }
7911 }
7912 #endif
7913
7914 fi->fname = f == NULL ? fi->path : f + 1;
7915 return 1;
7916 }
7917
7918 /* Output the directory table and the file name table. We try to minimize
7919 the total amount of memory needed. A heuristic is used to avoid large
7920 slowdowns with many input files. */
7921
7922 static void
7923 output_file_names (void)
7924 {
7925 struct file_name_acquire_data fnad;
7926 int numfiles;
7927 struct file_info *files;
7928 struct dir_info *dirs;
7929 int *saved;
7930 int *savehere;
7931 int *backmap;
7932 int ndirs;
7933 int idx_offset;
7934 int i;
7935 int idx;
7936
7937 if (!last_emitted_file)
7938 {
7939 dw2_asm_output_data (1, 0, "End directory table");
7940 dw2_asm_output_data (1, 0, "End file name table");
7941 return;
7942 }
7943
7944 numfiles = last_emitted_file->emitted_number;
7945
7946 /* Allocate the various arrays we need. */
7947 files = alloca (numfiles * sizeof (struct file_info));
7948 dirs = alloca (numfiles * sizeof (struct dir_info));
7949
7950 fnad.files = files;
7951 fnad.used_files = 0;
7952 fnad.max_files = numfiles;
7953 htab_traverse (file_table, file_name_acquire, &fnad);
7954 gcc_assert (fnad.used_files == fnad.max_files);
7955
7956 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7957
7958 /* Find all the different directories used. */
7959 dirs[0].path = files[0].path;
7960 dirs[0].length = files[0].fname - files[0].path;
7961 dirs[0].prefix = -1;
7962 dirs[0].count = 1;
7963 dirs[0].dir_idx = 0;
7964 files[0].dir_idx = 0;
7965 ndirs = 1;
7966
7967 for (i = 1; i < numfiles; i++)
7968 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7969 && memcmp (dirs[ndirs - 1].path, files[i].path,
7970 dirs[ndirs - 1].length) == 0)
7971 {
7972 /* Same directory as last entry. */
7973 files[i].dir_idx = ndirs - 1;
7974 ++dirs[ndirs - 1].count;
7975 }
7976 else
7977 {
7978 int j;
7979
7980 /* This is a new directory. */
7981 dirs[ndirs].path = files[i].path;
7982 dirs[ndirs].length = files[i].fname - files[i].path;
7983 dirs[ndirs].count = 1;
7984 dirs[ndirs].dir_idx = ndirs;
7985 files[i].dir_idx = ndirs;
7986
7987 /* Search for a prefix. */
7988 dirs[ndirs].prefix = -1;
7989 for (j = 0; j < ndirs; j++)
7990 if (dirs[j].length < dirs[ndirs].length
7991 && dirs[j].length > 1
7992 && (dirs[ndirs].prefix == -1
7993 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7994 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7995 dirs[ndirs].prefix = j;
7996
7997 ++ndirs;
7998 }
7999
8000 /* Now to the actual work. We have to find a subset of the directories which
8001 allow expressing the file name using references to the directory table
8002 with the least amount of characters. We do not do an exhaustive search
8003 where we would have to check out every combination of every single
8004 possible prefix. Instead we use a heuristic which provides nearly optimal
8005 results in most cases and never is much off. */
8006 saved = alloca (ndirs * sizeof (int));
8007 savehere = alloca (ndirs * sizeof (int));
8008
8009 memset (saved, '\0', ndirs * sizeof (saved[0]));
8010 for (i = 0; i < ndirs; i++)
8011 {
8012 int j;
8013 int total;
8014
8015 /* We can always save some space for the current directory. But this
8016 does not mean it will be enough to justify adding the directory. */
8017 savehere[i] = dirs[i].length;
8018 total = (savehere[i] - saved[i]) * dirs[i].count;
8019
8020 for (j = i + 1; j < ndirs; j++)
8021 {
8022 savehere[j] = 0;
8023 if (saved[j] < dirs[i].length)
8024 {
8025 /* Determine whether the dirs[i] path is a prefix of the
8026 dirs[j] path. */
8027 int k;
8028
8029 k = dirs[j].prefix;
8030 while (k != -1 && k != (int) i)
8031 k = dirs[k].prefix;
8032
8033 if (k == (int) i)
8034 {
8035 /* Yes it is. We can possibly save some memory by
8036 writing the filenames in dirs[j] relative to
8037 dirs[i]. */
8038 savehere[j] = dirs[i].length;
8039 total += (savehere[j] - saved[j]) * dirs[j].count;
8040 }
8041 }
8042 }
8043
8044 /* Check whether we can save enough to justify adding the dirs[i]
8045 directory. */
8046 if (total > dirs[i].length + 1)
8047 {
8048 /* It's worthwhile adding. */
8049 for (j = i; j < ndirs; j++)
8050 if (savehere[j] > 0)
8051 {
8052 /* Remember how much we saved for this directory so far. */
8053 saved[j] = savehere[j];
8054
8055 /* Remember the prefix directory. */
8056 dirs[j].dir_idx = i;
8057 }
8058 }
8059 }
8060
8061 /* Emit the directory name table. */
8062 idx = 1;
8063 idx_offset = dirs[0].length > 0 ? 1 : 0;
8064 for (i = 1 - idx_offset; i < ndirs; i++)
8065 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8066 "Directory Entry: 0x%x", i + idx_offset);
8067
8068 dw2_asm_output_data (1, 0, "End directory table");
8069
8070 /* We have to emit them in the order of emitted_number since that's
8071 used in the debug info generation. To do this efficiently we
8072 generate a back-mapping of the indices first. */
8073 backmap = alloca (numfiles * sizeof (int));
8074 for (i = 0; i < numfiles; i++)
8075 backmap[files[i].file_idx->emitted_number - 1] = i;
8076
8077 /* Now write all the file names. */
8078 for (i = 0; i < numfiles; i++)
8079 {
8080 int file_idx = backmap[i];
8081 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8082
8083 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8084 "File Entry: 0x%x", (unsigned) i + 1);
8085
8086 /* Include directory index. */
8087 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8088
8089 /* Modification time. */
8090 dw2_asm_output_data_uleb128 (0, NULL);
8091
8092 /* File length in bytes. */
8093 dw2_asm_output_data_uleb128 (0, NULL);
8094 }
8095
8096 dw2_asm_output_data (1, 0, "End file name table");
8097 }
8098
8099
8100 /* Output the source line number correspondence information. This
8101 information goes into the .debug_line section. */
8102
8103 static void
8104 output_line_info (void)
8105 {
8106 char l1[20], l2[20], p1[20], p2[20];
8107 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8108 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8109 unsigned opc;
8110 unsigned n_op_args;
8111 unsigned long lt_index;
8112 unsigned long current_line;
8113 long line_offset;
8114 long line_delta;
8115 unsigned long current_file;
8116 unsigned long function;
8117
8118 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8119 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8120 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8121 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8122
8123 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8124 dw2_asm_output_data (4, 0xffffffff,
8125 "Initial length escape value indicating 64-bit DWARF extension");
8126 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8127 "Length of Source Line Info");
8128 ASM_OUTPUT_LABEL (asm_out_file, l1);
8129
8130 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8131 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8132 ASM_OUTPUT_LABEL (asm_out_file, p1);
8133
8134 /* Define the architecture-dependent minimum instruction length (in
8135 bytes). In this implementation of DWARF, this field is used for
8136 information purposes only. Since GCC generates assembly language,
8137 we have no a priori knowledge of how many instruction bytes are
8138 generated for each source line, and therefore can use only the
8139 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8140 commands. Accordingly, we fix this as `1', which is "correct
8141 enough" for all architectures, and don't let the target override. */
8142 dw2_asm_output_data (1, 1,
8143 "Minimum Instruction Length");
8144
8145 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8146 "Default is_stmt_start flag");
8147 dw2_asm_output_data (1, DWARF_LINE_BASE,
8148 "Line Base Value (Special Opcodes)");
8149 dw2_asm_output_data (1, DWARF_LINE_RANGE,
8150 "Line Range Value (Special Opcodes)");
8151 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8152 "Special Opcode Base");
8153
8154 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8155 {
8156 switch (opc)
8157 {
8158 case DW_LNS_advance_pc:
8159 case DW_LNS_advance_line:
8160 case DW_LNS_set_file:
8161 case DW_LNS_set_column:
8162 case DW_LNS_fixed_advance_pc:
8163 n_op_args = 1;
8164 break;
8165 default:
8166 n_op_args = 0;
8167 break;
8168 }
8169
8170 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8171 opc, n_op_args);
8172 }
8173
8174 /* Write out the information about the files we use. */
8175 output_file_names ();
8176 ASM_OUTPUT_LABEL (asm_out_file, p2);
8177
8178 /* We used to set the address register to the first location in the text
8179 section here, but that didn't accomplish anything since we already
8180 have a line note for the opening brace of the first function. */
8181
8182 /* Generate the line number to PC correspondence table, encoded as
8183 a series of state machine operations. */
8184 current_file = 1;
8185 current_line = 1;
8186
8187 if (cfun && in_cold_section_p)
8188 strcpy (prev_line_label, cfun->cold_section_label);
8189 else
8190 strcpy (prev_line_label, text_section_label);
8191 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8192 {
8193 dw_line_info_ref line_info = &line_info_table[lt_index];
8194
8195 #if 0
8196 /* Disable this optimization for now; GDB wants to see two line notes
8197 at the beginning of a function so it can find the end of the
8198 prologue. */
8199
8200 /* Don't emit anything for redundant notes. Just updating the
8201 address doesn't accomplish anything, because we already assume
8202 that anything after the last address is this line. */
8203 if (line_info->dw_line_num == current_line
8204 && line_info->dw_file_num == current_file)
8205 continue;
8206 #endif
8207
8208 /* Emit debug info for the address of the current line.
8209
8210 Unfortunately, we have little choice here currently, and must always
8211 use the most general form. GCC does not know the address delta
8212 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
8213 attributes which will give an upper bound on the address range. We
8214 could perhaps use length attributes to determine when it is safe to
8215 use DW_LNS_fixed_advance_pc. */
8216
8217 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8218 if (0)
8219 {
8220 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
8221 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8222 "DW_LNS_fixed_advance_pc");
8223 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8224 }
8225 else
8226 {
8227 /* This can handle any delta. This takes
8228 4+DWARF2_ADDR_SIZE bytes. */
8229 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8230 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8231 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8232 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8233 }
8234
8235 strcpy (prev_line_label, line_label);
8236
8237 /* Emit debug info for the source file of the current line, if
8238 different from the previous line. */
8239 if (line_info->dw_file_num != current_file)
8240 {
8241 current_file = line_info->dw_file_num;
8242 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8243 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8244 }
8245
8246 /* Emit debug info for the current line number, choosing the encoding
8247 that uses the least amount of space. */
8248 if (line_info->dw_line_num != current_line)
8249 {
8250 line_offset = line_info->dw_line_num - current_line;
8251 line_delta = line_offset - DWARF_LINE_BASE;
8252 current_line = line_info->dw_line_num;
8253 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8254 /* This can handle deltas from -10 to 234, using the current
8255 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
8256 takes 1 byte. */
8257 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8258 "line %lu", current_line);
8259 else
8260 {
8261 /* This can handle any delta. This takes at least 4 bytes,
8262 depending on the value being encoded. */
8263 dw2_asm_output_data (1, DW_LNS_advance_line,
8264 "advance to line %lu", current_line);
8265 dw2_asm_output_data_sleb128 (line_offset, NULL);
8266 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8267 }
8268 }
8269 else
8270 /* We still need to start a new row, so output a copy insn. */
8271 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8272 }
8273
8274 /* Emit debug info for the address of the end of the function. */
8275 if (0)
8276 {
8277 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8278 "DW_LNS_fixed_advance_pc");
8279 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8280 }
8281 else
8282 {
8283 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8284 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8285 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8286 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8287 }
8288
8289 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8290 dw2_asm_output_data_uleb128 (1, NULL);
8291 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8292
8293 function = 0;
8294 current_file = 1;
8295 current_line = 1;
8296 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8297 {
8298 dw_separate_line_info_ref line_info
8299 = &separate_line_info_table[lt_index];
8300
8301 #if 0
8302 /* Don't emit anything for redundant notes. */
8303 if (line_info->dw_line_num == current_line
8304 && line_info->dw_file_num == current_file
8305 && line_info->function == function)
8306 goto cont;
8307 #endif
8308
8309 /* Emit debug info for the address of the current line. If this is
8310 a new function, or the first line of a function, then we need
8311 to handle it differently. */
8312 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8313 lt_index);
8314 if (function != line_info->function)
8315 {
8316 function = line_info->function;
8317
8318 /* Set the address register to the first line in the function. */
8319 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8320 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8321 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8322 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8323 }
8324 else
8325 {
8326 /* ??? See the DW_LNS_advance_pc comment above. */
8327 if (0)
8328 {
8329 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8330 "DW_LNS_fixed_advance_pc");
8331 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8332 }
8333 else
8334 {
8335 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8336 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8337 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8338 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8339 }
8340 }
8341
8342 strcpy (prev_line_label, line_label);
8343
8344 /* Emit debug info for the source file of the current line, if
8345 different from the previous line. */
8346 if (line_info->dw_file_num != current_file)
8347 {
8348 current_file = line_info->dw_file_num;
8349 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8350 dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8351 }
8352
8353 /* Emit debug info for the current line number, choosing the encoding
8354 that uses the least amount of space. */
8355 if (line_info->dw_line_num != current_line)
8356 {
8357 line_offset = line_info->dw_line_num - current_line;
8358 line_delta = line_offset - DWARF_LINE_BASE;
8359 current_line = line_info->dw_line_num;
8360 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8361 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8362 "line %lu", current_line);
8363 else
8364 {
8365 dw2_asm_output_data (1, DW_LNS_advance_line,
8366 "advance to line %lu", current_line);
8367 dw2_asm_output_data_sleb128 (line_offset, NULL);
8368 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8369 }
8370 }
8371 else
8372 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8373
8374 #if 0
8375 cont:
8376 #endif
8377
8378 lt_index++;
8379
8380 /* If we're done with a function, end its sequence. */
8381 if (lt_index == separate_line_info_table_in_use
8382 || separate_line_info_table[lt_index].function != function)
8383 {
8384 current_file = 1;
8385 current_line = 1;
8386
8387 /* Emit debug info for the address of the end of the function. */
8388 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8389 if (0)
8390 {
8391 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8392 "DW_LNS_fixed_advance_pc");
8393 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8394 }
8395 else
8396 {
8397 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8398 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8399 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8400 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8401 }
8402
8403 /* Output the marker for the end of this sequence. */
8404 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8405 dw2_asm_output_data_uleb128 (1, NULL);
8406 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8407 }
8408 }
8409
8410 /* Output the marker for the end of the line number info. */
8411 ASM_OUTPUT_LABEL (asm_out_file, l2);
8412 }
8413 \f
8414 /* Given a pointer to a tree node for some base type, return a pointer to
8415 a DIE that describes the given type.
8416
8417 This routine must only be called for GCC type nodes that correspond to
8418 Dwarf base (fundamental) types. */
8419
8420 static dw_die_ref
8421 base_type_die (tree type)
8422 {
8423 dw_die_ref base_type_result;
8424 enum dwarf_type encoding;
8425
8426 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8427 return 0;
8428
8429 switch (TREE_CODE (type))
8430 {
8431 case INTEGER_TYPE:
8432 if (TYPE_STRING_FLAG (type))
8433 {
8434 if (TYPE_UNSIGNED (type))
8435 encoding = DW_ATE_unsigned_char;
8436 else
8437 encoding = DW_ATE_signed_char;
8438 }
8439 else if (TYPE_UNSIGNED (type))
8440 encoding = DW_ATE_unsigned;
8441 else
8442 encoding = DW_ATE_signed;
8443 break;
8444
8445 case REAL_TYPE:
8446 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8447 encoding = DW_ATE_decimal_float;
8448 else
8449 encoding = DW_ATE_float;
8450 break;
8451
8452 case FIXED_POINT_TYPE:
8453 if (TYPE_UNSIGNED (type))
8454 encoding = DW_ATE_unsigned_fixed;
8455 else
8456 encoding = DW_ATE_signed_fixed;
8457 break;
8458
8459 /* Dwarf2 doesn't know anything about complex ints, so use
8460 a user defined type for it. */
8461 case COMPLEX_TYPE:
8462 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8463 encoding = DW_ATE_complex_float;
8464 else
8465 encoding = DW_ATE_lo_user;
8466 break;
8467
8468 case BOOLEAN_TYPE:
8469 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8470 encoding = DW_ATE_boolean;
8471 break;
8472
8473 default:
8474 /* No other TREE_CODEs are Dwarf fundamental types. */
8475 gcc_unreachable ();
8476 }
8477
8478 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8479
8480 /* This probably indicates a bug. */
8481 if (! TYPE_NAME (type))
8482 add_name_attribute (base_type_result, "__unknown__");
8483
8484 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8485 int_size_in_bytes (type));
8486 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8487
8488 return base_type_result;
8489 }
8490
8491 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8492 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8493
8494 static inline int
8495 is_base_type (tree type)
8496 {
8497 switch (TREE_CODE (type))
8498 {
8499 case ERROR_MARK:
8500 case VOID_TYPE:
8501 case INTEGER_TYPE:
8502 case REAL_TYPE:
8503 case FIXED_POINT_TYPE:
8504 case COMPLEX_TYPE:
8505 case BOOLEAN_TYPE:
8506 return 1;
8507
8508 case ARRAY_TYPE:
8509 case RECORD_TYPE:
8510 case UNION_TYPE:
8511 case QUAL_UNION_TYPE:
8512 case ENUMERAL_TYPE:
8513 case FUNCTION_TYPE:
8514 case METHOD_TYPE:
8515 case POINTER_TYPE:
8516 case REFERENCE_TYPE:
8517 case OFFSET_TYPE:
8518 case LANG_TYPE:
8519 case VECTOR_TYPE:
8520 return 0;
8521
8522 default:
8523 gcc_unreachable ();
8524 }
8525
8526 return 0;
8527 }
8528
8529 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8530 node, return the size in bits for the type if it is a constant, or else
8531 return the alignment for the type if the type's size is not constant, or
8532 else return BITS_PER_WORD if the type actually turns out to be an
8533 ERROR_MARK node. */
8534
8535 static inline unsigned HOST_WIDE_INT
8536 simple_type_size_in_bits (const_tree type)
8537 {
8538 if (TREE_CODE (type) == ERROR_MARK)
8539 return BITS_PER_WORD;
8540 else if (TYPE_SIZE (type) == NULL_TREE)
8541 return 0;
8542 else if (host_integerp (TYPE_SIZE (type), 1))
8543 return tree_low_cst (TYPE_SIZE (type), 1);
8544 else
8545 return TYPE_ALIGN (type);
8546 }
8547
8548 /* Return true if the debug information for the given type should be
8549 emitted as a subrange type. */
8550
8551 static inline bool
8552 is_subrange_type (const_tree type)
8553 {
8554 tree subtype = TREE_TYPE (type);
8555
8556 /* Subrange types are identified by the fact that they are integer
8557 types, and that they have a subtype which is either an integer type
8558 or an enumeral type. */
8559
8560 if (TREE_CODE (type) != INTEGER_TYPE
8561 || subtype == NULL_TREE)
8562 return false;
8563
8564 if (TREE_CODE (subtype) != INTEGER_TYPE
8565 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8566 return false;
8567
8568 if (TREE_CODE (type) == TREE_CODE (subtype)
8569 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8570 && TYPE_MIN_VALUE (type) != NULL
8571 && TYPE_MIN_VALUE (subtype) != NULL
8572 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8573 && TYPE_MAX_VALUE (type) != NULL
8574 && TYPE_MAX_VALUE (subtype) != NULL
8575 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8576 {
8577 /* The type and its subtype have the same representation. If in
8578 addition the two types also have the same name, then the given
8579 type is not a subrange type, but rather a plain base type. */
8580 /* FIXME: brobecker/2004-03-22:
8581 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8582 therefore be sufficient to check the TYPE_SIZE node pointers
8583 rather than checking the actual size. Unfortunately, we have
8584 found some cases, such as in the Ada "integer" type, where
8585 this is not the case. Until this problem is solved, we need to
8586 keep checking the actual size. */
8587 tree type_name = TYPE_NAME (type);
8588 tree subtype_name = TYPE_NAME (subtype);
8589
8590 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8591 type_name = DECL_NAME (type_name);
8592
8593 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8594 subtype_name = DECL_NAME (subtype_name);
8595
8596 if (type_name == subtype_name)
8597 return false;
8598 }
8599
8600 return true;
8601 }
8602
8603 /* Given a pointer to a tree node for a subrange type, return a pointer
8604 to a DIE that describes the given type. */
8605
8606 static dw_die_ref
8607 subrange_type_die (tree type, dw_die_ref context_die)
8608 {
8609 dw_die_ref subrange_die;
8610 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8611
8612 if (context_die == NULL)
8613 context_die = comp_unit_die;
8614
8615 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8616
8617 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8618 {
8619 /* The size of the subrange type and its base type do not match,
8620 so we need to generate a size attribute for the subrange type. */
8621 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8622 }
8623
8624 if (TYPE_MIN_VALUE (type) != NULL)
8625 add_bound_info (subrange_die, DW_AT_lower_bound,
8626 TYPE_MIN_VALUE (type));
8627 if (TYPE_MAX_VALUE (type) != NULL)
8628 add_bound_info (subrange_die, DW_AT_upper_bound,
8629 TYPE_MAX_VALUE (type));
8630
8631 return subrange_die;
8632 }
8633
8634 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8635 entry that chains various modifiers in front of the given type. */
8636
8637 static dw_die_ref
8638 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8639 dw_die_ref context_die)
8640 {
8641 enum tree_code code = TREE_CODE (type);
8642 dw_die_ref mod_type_die;
8643 dw_die_ref sub_die = NULL;
8644 tree item_type = NULL;
8645 tree qualified_type;
8646 tree name;
8647
8648 if (code == ERROR_MARK)
8649 return NULL;
8650
8651 /* See if we already have the appropriately qualified variant of
8652 this type. */
8653 qualified_type
8654 = get_qualified_type (type,
8655 ((is_const_type ? TYPE_QUAL_CONST : 0)
8656 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8657
8658 /* If we do, then we can just use its DIE, if it exists. */
8659 if (qualified_type)
8660 {
8661 mod_type_die = lookup_type_die (qualified_type);
8662 if (mod_type_die)
8663 return mod_type_die;
8664 }
8665
8666 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8667
8668 /* Handle C typedef types. */
8669 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8670 {
8671 tree dtype = TREE_TYPE (name);
8672
8673 if (qualified_type == dtype)
8674 {
8675 /* For a named type, use the typedef. */
8676 gen_type_die (qualified_type, context_die);
8677 return lookup_type_die (qualified_type);
8678 }
8679 else if (is_const_type < TYPE_READONLY (dtype)
8680 || is_volatile_type < TYPE_VOLATILE (dtype)
8681 || (is_const_type <= TYPE_READONLY (dtype)
8682 && is_volatile_type <= TYPE_VOLATILE (dtype)
8683 && DECL_ORIGINAL_TYPE (name) != type))
8684 /* cv-unqualified version of named type. Just use the unnamed
8685 type to which it refers. */
8686 return modified_type_die (DECL_ORIGINAL_TYPE (name),
8687 is_const_type, is_volatile_type,
8688 context_die);
8689 /* Else cv-qualified version of named type; fall through. */
8690 }
8691
8692 if (is_const_type)
8693 {
8694 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8695 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8696 }
8697 else if (is_volatile_type)
8698 {
8699 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8700 sub_die = modified_type_die (type, 0, 0, context_die);
8701 }
8702 else if (code == POINTER_TYPE)
8703 {
8704 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8705 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8706 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8707 item_type = TREE_TYPE (type);
8708 }
8709 else if (code == REFERENCE_TYPE)
8710 {
8711 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8712 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8713 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8714 item_type = TREE_TYPE (type);
8715 }
8716 else if (is_subrange_type (type))
8717 {
8718 mod_type_die = subrange_type_die (type, context_die);
8719 item_type = TREE_TYPE (type);
8720 }
8721 else if (is_base_type (type))
8722 mod_type_die = base_type_die (type);
8723 else
8724 {
8725 gen_type_die (type, context_die);
8726
8727 /* We have to get the type_main_variant here (and pass that to the
8728 `lookup_type_die' routine) because the ..._TYPE node we have
8729 might simply be a *copy* of some original type node (where the
8730 copy was created to help us keep track of typedef names) and
8731 that copy might have a different TYPE_UID from the original
8732 ..._TYPE node. */
8733 if (TREE_CODE (type) != VECTOR_TYPE)
8734 return lookup_type_die (type_main_variant (type));
8735 else
8736 /* Vectors have the debugging information in the type,
8737 not the main variant. */
8738 return lookup_type_die (type);
8739 }
8740
8741 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
8742 don't output a DW_TAG_typedef, since there isn't one in the
8743 user's program; just attach a DW_AT_name to the type. */
8744 if (name
8745 && (TREE_CODE (name) != TYPE_DECL
8746 || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8747 {
8748 if (TREE_CODE (name) == TYPE_DECL)
8749 /* Could just call add_name_and_src_coords_attributes here,
8750 but since this is a builtin type it doesn't have any
8751 useful source coordinates anyway. */
8752 name = DECL_NAME (name);
8753 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8754 }
8755
8756 if (qualified_type)
8757 equate_type_number_to_die (qualified_type, mod_type_die);
8758
8759 if (item_type)
8760 /* We must do this after the equate_type_number_to_die call, in case
8761 this is a recursive type. This ensures that the modified_type_die
8762 recursion will terminate even if the type is recursive. Recursive
8763 types are possible in Ada. */
8764 sub_die = modified_type_die (item_type,
8765 TYPE_READONLY (item_type),
8766 TYPE_VOLATILE (item_type),
8767 context_die);
8768
8769 if (sub_die != NULL)
8770 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8771
8772 return mod_type_die;
8773 }
8774
8775 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8776 an enumerated type. */
8777
8778 static inline int
8779 type_is_enum (const_tree type)
8780 {
8781 return TREE_CODE (type) == ENUMERAL_TYPE;
8782 }
8783
8784 /* Return the DBX register number described by a given RTL node. */
8785
8786 static unsigned int
8787 dbx_reg_number (const_rtx rtl)
8788 {
8789 unsigned regno = REGNO (rtl);
8790
8791 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8792
8793 #ifdef LEAF_REG_REMAP
8794 if (current_function_uses_only_leaf_regs)
8795 {
8796 int leaf_reg = LEAF_REG_REMAP (regno);
8797 if (leaf_reg != -1)
8798 regno = (unsigned) leaf_reg;
8799 }
8800 #endif
8801
8802 return DBX_REGISTER_NUMBER (regno);
8803 }
8804
8805 /* Optionally add a DW_OP_piece term to a location description expression.
8806 DW_OP_piece is only added if the location description expression already
8807 doesn't end with DW_OP_piece. */
8808
8809 static void
8810 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8811 {
8812 dw_loc_descr_ref loc;
8813
8814 if (*list_head != NULL)
8815 {
8816 /* Find the end of the chain. */
8817 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8818 ;
8819
8820 if (loc->dw_loc_opc != DW_OP_piece)
8821 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8822 }
8823 }
8824
8825 /* Return a location descriptor that designates a machine register or
8826 zero if there is none. */
8827
8828 static dw_loc_descr_ref
8829 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8830 {
8831 rtx regs;
8832
8833 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8834 return 0;
8835
8836 regs = targetm.dwarf_register_span (rtl);
8837
8838 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8839 return multiple_reg_loc_descriptor (rtl, regs, initialized);
8840 else
8841 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8842 }
8843
8844 /* Return a location descriptor that designates a machine register for
8845 a given hard register number. */
8846
8847 static dw_loc_descr_ref
8848 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8849 {
8850 dw_loc_descr_ref reg_loc_descr;
8851 if (regno <= 31)
8852 reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8853 else
8854 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8855
8856 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8857 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8858
8859 return reg_loc_descr;
8860 }
8861
8862 /* Given an RTL of a register, return a location descriptor that
8863 designates a value that spans more than one register. */
8864
8865 static dw_loc_descr_ref
8866 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8867 enum var_init_status initialized)
8868 {
8869 int nregs, size, i;
8870 unsigned reg;
8871 dw_loc_descr_ref loc_result = NULL;
8872
8873 reg = REGNO (rtl);
8874 #ifdef LEAF_REG_REMAP
8875 if (current_function_uses_only_leaf_regs)
8876 {
8877 int leaf_reg = LEAF_REG_REMAP (reg);
8878 if (leaf_reg != -1)
8879 reg = (unsigned) leaf_reg;
8880 }
8881 #endif
8882 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8883 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8884
8885 /* Simple, contiguous registers. */
8886 if (regs == NULL_RTX)
8887 {
8888 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8889
8890 loc_result = NULL;
8891 while (nregs--)
8892 {
8893 dw_loc_descr_ref t;
8894
8895 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8896 VAR_INIT_STATUS_INITIALIZED);
8897 add_loc_descr (&loc_result, t);
8898 add_loc_descr_op_piece (&loc_result, size);
8899 ++reg;
8900 }
8901 return loc_result;
8902 }
8903
8904 /* Now onto stupid register sets in non contiguous locations. */
8905
8906 gcc_assert (GET_CODE (regs) == PARALLEL);
8907
8908 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8909 loc_result = NULL;
8910
8911 for (i = 0; i < XVECLEN (regs, 0); ++i)
8912 {
8913 dw_loc_descr_ref t;
8914
8915 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8916 VAR_INIT_STATUS_INITIALIZED);
8917 add_loc_descr (&loc_result, t);
8918 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8919 add_loc_descr_op_piece (&loc_result, size);
8920 }
8921
8922 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8923 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8924 return loc_result;
8925 }
8926
8927 /* Return a location descriptor that designates a constant. */
8928
8929 static dw_loc_descr_ref
8930 int_loc_descriptor (HOST_WIDE_INT i)
8931 {
8932 enum dwarf_location_atom op;
8933
8934 /* Pick the smallest representation of a constant, rather than just
8935 defaulting to the LEB encoding. */
8936 if (i >= 0)
8937 {
8938 if (i <= 31)
8939 op = DW_OP_lit0 + i;
8940 else if (i <= 0xff)
8941 op = DW_OP_const1u;
8942 else if (i <= 0xffff)
8943 op = DW_OP_const2u;
8944 else if (HOST_BITS_PER_WIDE_INT == 32
8945 || i <= 0xffffffff)
8946 op = DW_OP_const4u;
8947 else
8948 op = DW_OP_constu;
8949 }
8950 else
8951 {
8952 if (i >= -0x80)
8953 op = DW_OP_const1s;
8954 else if (i >= -0x8000)
8955 op = DW_OP_const2s;
8956 else if (HOST_BITS_PER_WIDE_INT == 32
8957 || i >= -0x80000000)
8958 op = DW_OP_const4s;
8959 else
8960 op = DW_OP_consts;
8961 }
8962
8963 return new_loc_descr (op, i, 0);
8964 }
8965
8966 /* Return a location descriptor that designates a base+offset location. */
8967
8968 static dw_loc_descr_ref
8969 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
8970 enum var_init_status initialized)
8971 {
8972 unsigned int regno;
8973 dw_loc_descr_ref result;
8974
8975 /* We only use "frame base" when we're sure we're talking about the
8976 post-prologue local stack frame. We do this by *not* running
8977 register elimination until this point, and recognizing the special
8978 argument pointer and soft frame pointer rtx's. */
8979 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8980 {
8981 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8982
8983 if (elim != reg)
8984 {
8985 if (GET_CODE (elim) == PLUS)
8986 {
8987 offset += INTVAL (XEXP (elim, 1));
8988 elim = XEXP (elim, 0);
8989 }
8990 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8991 : stack_pointer_rtx));
8992 offset += frame_pointer_fb_offset;
8993
8994 return new_loc_descr (DW_OP_fbreg, offset, 0);
8995 }
8996 }
8997
8998 regno = dbx_reg_number (reg);
8999 if (regno <= 31)
9000 result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9001 else
9002 result = new_loc_descr (DW_OP_bregx, regno, offset);
9003
9004 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9005 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9006
9007 return result;
9008 }
9009
9010 /* Return true if this RTL expression describes a base+offset calculation. */
9011
9012 static inline int
9013 is_based_loc (const_rtx rtl)
9014 {
9015 return (GET_CODE (rtl) == PLUS
9016 && ((REG_P (XEXP (rtl, 0))
9017 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9018 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9019 }
9020
9021 /* Return a descriptor that describes the concatenation of N locations
9022 used to form the address of a memory location. */
9023
9024 static dw_loc_descr_ref
9025 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9026 enum var_init_status initialized)
9027 {
9028 unsigned int i;
9029 dw_loc_descr_ref cc_loc_result = NULL;
9030 unsigned int n = XVECLEN (concatn, 0);
9031
9032 for (i = 0; i < n; ++i)
9033 {
9034 dw_loc_descr_ref ref;
9035 rtx x = XVECEXP (concatn, 0, i);
9036
9037 ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9038 if (ref == NULL)
9039 return NULL;
9040
9041 add_loc_descr (&cc_loc_result, ref);
9042 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9043 }
9044
9045 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9046 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9047
9048 return cc_loc_result;
9049 }
9050
9051 /* The following routine converts the RTL for a variable or parameter
9052 (resident in memory) into an equivalent Dwarf representation of a
9053 mechanism for getting the address of that same variable onto the top of a
9054 hypothetical "address evaluation" stack.
9055
9056 When creating memory location descriptors, we are effectively transforming
9057 the RTL for a memory-resident object into its Dwarf postfix expression
9058 equivalent. This routine recursively descends an RTL tree, turning
9059 it into Dwarf postfix code as it goes.
9060
9061 MODE is the mode of the memory reference, needed to handle some
9062 autoincrement addressing modes.
9063
9064 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9065 location list for RTL.
9066
9067 Return 0 if we can't represent the location. */
9068
9069 static dw_loc_descr_ref
9070 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9071 enum var_init_status initialized)
9072 {
9073 dw_loc_descr_ref mem_loc_result = NULL;
9074 enum dwarf_location_atom op;
9075
9076 /* Note that for a dynamically sized array, the location we will generate a
9077 description of here will be the lowest numbered location which is
9078 actually within the array. That's *not* necessarily the same as the
9079 zeroth element of the array. */
9080
9081 rtl = targetm.delegitimize_address (rtl);
9082
9083 switch (GET_CODE (rtl))
9084 {
9085 case POST_INC:
9086 case POST_DEC:
9087 case POST_MODIFY:
9088 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
9089 just fall into the SUBREG code. */
9090
9091 /* ... fall through ... */
9092
9093 case SUBREG:
9094 /* The case of a subreg may arise when we have a local (register)
9095 variable or a formal (register) parameter which doesn't quite fill
9096 up an entire register. For now, just assume that it is
9097 legitimate to make the Dwarf info refer to the whole register which
9098 contains the given subreg. */
9099 rtl = XEXP (rtl, 0);
9100
9101 /* ... fall through ... */
9102
9103 case REG:
9104 /* Whenever a register number forms a part of the description of the
9105 method for calculating the (dynamic) address of a memory resident
9106 object, DWARF rules require the register number be referred to as
9107 a "base register". This distinction is not based in any way upon
9108 what category of register the hardware believes the given register
9109 belongs to. This is strictly DWARF terminology we're dealing with
9110 here. Note that in cases where the location of a memory-resident
9111 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9112 OP_CONST (0)) the actual DWARF location descriptor that we generate
9113 may just be OP_BASEREG (basereg). This may look deceptively like
9114 the object in question was allocated to a register (rather than in
9115 memory) so DWARF consumers need to be aware of the subtle
9116 distinction between OP_REG and OP_BASEREG. */
9117 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9118 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9119 break;
9120
9121 case MEM:
9122 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9123 VAR_INIT_STATUS_INITIALIZED);
9124 if (mem_loc_result != 0)
9125 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9126 break;
9127
9128 case LO_SUM:
9129 rtl = XEXP (rtl, 1);
9130
9131 /* ... fall through ... */
9132
9133 case LABEL_REF:
9134 /* Some ports can transform a symbol ref into a label ref, because
9135 the symbol ref is too far away and has to be dumped into a constant
9136 pool. */
9137 case CONST:
9138 case SYMBOL_REF:
9139 /* Alternatively, the symbol in the constant pool might be referenced
9140 by a different symbol. */
9141 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9142 {
9143 bool marked;
9144 rtx tmp = get_pool_constant_mark (rtl, &marked);
9145
9146 if (GET_CODE (tmp) == SYMBOL_REF)
9147 {
9148 rtl = tmp;
9149 if (CONSTANT_POOL_ADDRESS_P (tmp))
9150 get_pool_constant_mark (tmp, &marked);
9151 else
9152 marked = true;
9153 }
9154
9155 /* If all references to this pool constant were optimized away,
9156 it was not output and thus we can't represent it.
9157 FIXME: might try to use DW_OP_const_value here, though
9158 DW_OP_piece complicates it. */
9159 if (!marked)
9160 return 0;
9161 }
9162
9163 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9164 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9165 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9166 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9167 break;
9168
9169 case PRE_MODIFY:
9170 /* Extract the PLUS expression nested inside and fall into
9171 PLUS code below. */
9172 rtl = XEXP (rtl, 1);
9173 goto plus;
9174
9175 case PRE_INC:
9176 case PRE_DEC:
9177 /* Turn these into a PLUS expression and fall into the PLUS code
9178 below. */
9179 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9180 GEN_INT (GET_CODE (rtl) == PRE_INC
9181 ? GET_MODE_UNIT_SIZE (mode)
9182 : -GET_MODE_UNIT_SIZE (mode)));
9183
9184 /* ... fall through ... */
9185
9186 case PLUS:
9187 plus:
9188 if (is_based_loc (rtl))
9189 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9190 INTVAL (XEXP (rtl, 1)),
9191 VAR_INIT_STATUS_INITIALIZED);
9192 else
9193 {
9194 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9195 VAR_INIT_STATUS_INITIALIZED);
9196 if (mem_loc_result == 0)
9197 break;
9198
9199 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9200 && INTVAL (XEXP (rtl, 1)) >= 0)
9201 add_loc_descr (&mem_loc_result,
9202 new_loc_descr (DW_OP_plus_uconst,
9203 INTVAL (XEXP (rtl, 1)), 0));
9204 else
9205 {
9206 add_loc_descr (&mem_loc_result,
9207 mem_loc_descriptor (XEXP (rtl, 1), mode,
9208 VAR_INIT_STATUS_INITIALIZED));
9209 add_loc_descr (&mem_loc_result,
9210 new_loc_descr (DW_OP_plus, 0, 0));
9211 }
9212 }
9213 break;
9214
9215 /* If a pseudo-reg is optimized away, it is possible for it to
9216 be replaced with a MEM containing a multiply or shift. */
9217 case MULT:
9218 op = DW_OP_mul;
9219 goto do_binop;
9220
9221 case ASHIFT:
9222 op = DW_OP_shl;
9223 goto do_binop;
9224
9225 case ASHIFTRT:
9226 op = DW_OP_shra;
9227 goto do_binop;
9228
9229 case LSHIFTRT:
9230 op = DW_OP_shr;
9231 goto do_binop;
9232
9233 do_binop:
9234 {
9235 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9236 VAR_INIT_STATUS_INITIALIZED);
9237 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9238 VAR_INIT_STATUS_INITIALIZED);
9239
9240 if (op0 == 0 || op1 == 0)
9241 break;
9242
9243 mem_loc_result = op0;
9244 add_loc_descr (&mem_loc_result, op1);
9245 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9246 break;
9247 }
9248
9249 case CONST_INT:
9250 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9251 break;
9252
9253 case CONCATN:
9254 mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9255 VAR_INIT_STATUS_INITIALIZED);
9256 break;
9257
9258 default:
9259 gcc_unreachable ();
9260 }
9261
9262 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9263 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9264
9265 return mem_loc_result;
9266 }
9267
9268 /* Return a descriptor that describes the concatenation of two locations.
9269 This is typically a complex variable. */
9270
9271 static dw_loc_descr_ref
9272 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9273 {
9274 dw_loc_descr_ref cc_loc_result = NULL;
9275 dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9276 dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9277
9278 if (x0_ref == 0 || x1_ref == 0)
9279 return 0;
9280
9281 cc_loc_result = x0_ref;
9282 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9283
9284 add_loc_descr (&cc_loc_result, x1_ref);
9285 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9286
9287 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9288 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9289
9290 return cc_loc_result;
9291 }
9292
9293 /* Return a descriptor that describes the concatenation of N
9294 locations. */
9295
9296 static dw_loc_descr_ref
9297 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9298 {
9299 unsigned int i;
9300 dw_loc_descr_ref cc_loc_result = NULL;
9301 unsigned int n = XVECLEN (concatn, 0);
9302
9303 for (i = 0; i < n; ++i)
9304 {
9305 dw_loc_descr_ref ref;
9306 rtx x = XVECEXP (concatn, 0, i);
9307
9308 ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9309 if (ref == NULL)
9310 return NULL;
9311
9312 add_loc_descr (&cc_loc_result, ref);
9313 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9314 }
9315
9316 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9317 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9318
9319 return cc_loc_result;
9320 }
9321
9322 /* Output a proper Dwarf location descriptor for a variable or parameter
9323 which is either allocated in a register or in a memory location. For a
9324 register, we just generate an OP_REG and the register number. For a
9325 memory location we provide a Dwarf postfix expression describing how to
9326 generate the (dynamic) address of the object onto the address stack.
9327
9328 If we don't know how to describe it, return 0. */
9329
9330 static dw_loc_descr_ref
9331 loc_descriptor (rtx rtl, enum var_init_status initialized)
9332 {
9333 dw_loc_descr_ref loc_result = NULL;
9334
9335 switch (GET_CODE (rtl))
9336 {
9337 case SUBREG:
9338 /* The case of a subreg may arise when we have a local (register)
9339 variable or a formal (register) parameter which doesn't quite fill
9340 up an entire register. For now, just assume that it is
9341 legitimate to make the Dwarf info refer to the whole register which
9342 contains the given subreg. */
9343 rtl = SUBREG_REG (rtl);
9344
9345 /* ... fall through ... */
9346
9347 case REG:
9348 loc_result = reg_loc_descriptor (rtl, initialized);
9349 break;
9350
9351 case MEM:
9352 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9353 initialized);
9354 break;
9355
9356 case CONCAT:
9357 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9358 initialized);
9359 break;
9360
9361 case CONCATN:
9362 loc_result = concatn_loc_descriptor (rtl, initialized);
9363 break;
9364
9365 case VAR_LOCATION:
9366 /* Single part. */
9367 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9368 {
9369 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9370 break;
9371 }
9372
9373 rtl = XEXP (rtl, 1);
9374 /* FALLTHRU */
9375
9376 case PARALLEL:
9377 {
9378 rtvec par_elems = XVEC (rtl, 0);
9379 int num_elem = GET_NUM_ELEM (par_elems);
9380 enum machine_mode mode;
9381 int i;
9382
9383 /* Create the first one, so we have something to add to. */
9384 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9385 initialized);
9386 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9387 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9388 for (i = 1; i < num_elem; i++)
9389 {
9390 dw_loc_descr_ref temp;
9391
9392 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9393 initialized);
9394 add_loc_descr (&loc_result, temp);
9395 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9396 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9397 }
9398 }
9399 break;
9400
9401 default:
9402 gcc_unreachable ();
9403 }
9404
9405 return loc_result;
9406 }
9407
9408 /* Similar, but generate the descriptor from trees instead of rtl. This comes
9409 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
9410 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9411 top-level invocation, and we require the address of LOC; is 0 if we require
9412 the value of LOC. */
9413
9414 static dw_loc_descr_ref
9415 loc_descriptor_from_tree_1 (tree loc, int want_address)
9416 {
9417 dw_loc_descr_ref ret, ret1;
9418 int have_address = 0;
9419 enum dwarf_location_atom op;
9420
9421 /* ??? Most of the time we do not take proper care for sign/zero
9422 extending the values properly. Hopefully this won't be a real
9423 problem... */
9424
9425 switch (TREE_CODE (loc))
9426 {
9427 case ERROR_MARK:
9428 return 0;
9429
9430 case PLACEHOLDER_EXPR:
9431 /* This case involves extracting fields from an object to determine the
9432 position of other fields. We don't try to encode this here. The
9433 only user of this is Ada, which encodes the needed information using
9434 the names of types. */
9435 return 0;
9436
9437 case CALL_EXPR:
9438 return 0;
9439
9440 case PREINCREMENT_EXPR:
9441 case PREDECREMENT_EXPR:
9442 case POSTINCREMENT_EXPR:
9443 case POSTDECREMENT_EXPR:
9444 /* There are no opcodes for these operations. */
9445 return 0;
9446
9447 case ADDR_EXPR:
9448 /* If we already want an address, there's nothing we can do. */
9449 if (want_address)
9450 return 0;
9451
9452 /* Otherwise, process the argument and look for the address. */
9453 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9454
9455 case VAR_DECL:
9456 if (DECL_THREAD_LOCAL_P (loc))
9457 {
9458 rtx rtl;
9459
9460 /* If this is not defined, we have no way to emit the data. */
9461 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9462 return 0;
9463
9464 /* The way DW_OP_GNU_push_tls_address is specified, we can only
9465 look up addresses of objects in the current module. */
9466 if (DECL_EXTERNAL (loc))
9467 return 0;
9468
9469 rtl = rtl_for_decl_location (loc);
9470 if (rtl == NULL_RTX)
9471 return 0;
9472
9473 if (!MEM_P (rtl))
9474 return 0;
9475 rtl = XEXP (rtl, 0);
9476 if (! CONSTANT_P (rtl))
9477 return 0;
9478
9479 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9480 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9481 ret->dw_loc_oprnd1.v.val_addr = rtl;
9482
9483 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9484 add_loc_descr (&ret, ret1);
9485
9486 have_address = 1;
9487 break;
9488 }
9489 /* FALLTHRU */
9490
9491 case PARM_DECL:
9492 if (DECL_HAS_VALUE_EXPR_P (loc))
9493 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9494 want_address);
9495 /* FALLTHRU */
9496
9497 case RESULT_DECL:
9498 case FUNCTION_DECL:
9499 {
9500 rtx rtl = rtl_for_decl_location (loc);
9501
9502 if (rtl == NULL_RTX)
9503 return 0;
9504 else if (GET_CODE (rtl) == CONST_INT)
9505 {
9506 HOST_WIDE_INT val = INTVAL (rtl);
9507 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9508 val &= GET_MODE_MASK (DECL_MODE (loc));
9509 ret = int_loc_descriptor (val);
9510 }
9511 else if (GET_CODE (rtl) == CONST_STRING)
9512 return 0;
9513 else if (CONSTANT_P (rtl))
9514 {
9515 ret = new_loc_descr (DW_OP_addr, 0, 0);
9516 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9517 ret->dw_loc_oprnd1.v.val_addr = rtl;
9518 }
9519 else
9520 {
9521 enum machine_mode mode;
9522
9523 /* Certain constructs can only be represented at top-level. */
9524 if (want_address == 2)
9525 return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9526
9527 mode = GET_MODE (rtl);
9528 if (MEM_P (rtl))
9529 {
9530 rtl = XEXP (rtl, 0);
9531 have_address = 1;
9532 }
9533 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9534 }
9535 }
9536 break;
9537
9538 case INDIRECT_REF:
9539 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9540 have_address = 1;
9541 break;
9542
9543 case COMPOUND_EXPR:
9544 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9545
9546 case NOP_EXPR:
9547 case CONVERT_EXPR:
9548 case NON_LVALUE_EXPR:
9549 case VIEW_CONVERT_EXPR:
9550 case SAVE_EXPR:
9551 case GIMPLE_MODIFY_STMT:
9552 return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9553 want_address);
9554
9555 case COMPONENT_REF:
9556 case BIT_FIELD_REF:
9557 case ARRAY_REF:
9558 case ARRAY_RANGE_REF:
9559 {
9560 tree obj, offset;
9561 HOST_WIDE_INT bitsize, bitpos, bytepos;
9562 enum machine_mode mode;
9563 int volatilep;
9564 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9565
9566 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9567 &unsignedp, &volatilep, false);
9568
9569 if (obj == loc)
9570 return 0;
9571
9572 ret = loc_descriptor_from_tree_1 (obj, 1);
9573 if (ret == 0
9574 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9575 return 0;
9576
9577 if (offset != NULL_TREE)
9578 {
9579 /* Variable offset. */
9580 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9581 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9582 }
9583
9584 bytepos = bitpos / BITS_PER_UNIT;
9585 if (bytepos > 0)
9586 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9587 else if (bytepos < 0)
9588 {
9589 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9590 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9591 }
9592
9593 have_address = 1;
9594 break;
9595 }
9596
9597 case INTEGER_CST:
9598 if (host_integerp (loc, 0))
9599 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9600 else
9601 return 0;
9602 break;
9603
9604 case CONSTRUCTOR:
9605 {
9606 /* Get an RTL for this, if something has been emitted. */
9607 rtx rtl = lookup_constant_def (loc);
9608 enum machine_mode mode;
9609
9610 if (!rtl || !MEM_P (rtl))
9611 return 0;
9612 mode = GET_MODE (rtl);
9613 rtl = XEXP (rtl, 0);
9614 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9615 have_address = 1;
9616 break;
9617 }
9618
9619 case TRUTH_AND_EXPR:
9620 case TRUTH_ANDIF_EXPR:
9621 case BIT_AND_EXPR:
9622 op = DW_OP_and;
9623 goto do_binop;
9624
9625 case TRUTH_XOR_EXPR:
9626 case BIT_XOR_EXPR:
9627 op = DW_OP_xor;
9628 goto do_binop;
9629
9630 case TRUTH_OR_EXPR:
9631 case TRUTH_ORIF_EXPR:
9632 case BIT_IOR_EXPR:
9633 op = DW_OP_or;
9634 goto do_binop;
9635
9636 case FLOOR_DIV_EXPR:
9637 case CEIL_DIV_EXPR:
9638 case ROUND_DIV_EXPR:
9639 case TRUNC_DIV_EXPR:
9640 op = DW_OP_div;
9641 goto do_binop;
9642
9643 case MINUS_EXPR:
9644 op = DW_OP_minus;
9645 goto do_binop;
9646
9647 case FLOOR_MOD_EXPR:
9648 case CEIL_MOD_EXPR:
9649 case ROUND_MOD_EXPR:
9650 case TRUNC_MOD_EXPR:
9651 op = DW_OP_mod;
9652 goto do_binop;
9653
9654 case MULT_EXPR:
9655 op = DW_OP_mul;
9656 goto do_binop;
9657
9658 case LSHIFT_EXPR:
9659 op = DW_OP_shl;
9660 goto do_binop;
9661
9662 case RSHIFT_EXPR:
9663 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9664 goto do_binop;
9665
9666 case POINTER_PLUS_EXPR:
9667 case PLUS_EXPR:
9668 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9669 && host_integerp (TREE_OPERAND (loc, 1), 0))
9670 {
9671 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9672 if (ret == 0)
9673 return 0;
9674
9675 add_loc_descr (&ret,
9676 new_loc_descr (DW_OP_plus_uconst,
9677 tree_low_cst (TREE_OPERAND (loc, 1),
9678 0),
9679 0));
9680 break;
9681 }
9682
9683 op = DW_OP_plus;
9684 goto do_binop;
9685
9686 case LE_EXPR:
9687 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9688 return 0;
9689
9690 op = DW_OP_le;
9691 goto do_binop;
9692
9693 case GE_EXPR:
9694 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9695 return 0;
9696
9697 op = DW_OP_ge;
9698 goto do_binop;
9699
9700 case LT_EXPR:
9701 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9702 return 0;
9703
9704 op = DW_OP_lt;
9705 goto do_binop;
9706
9707 case GT_EXPR:
9708 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9709 return 0;
9710
9711 op = DW_OP_gt;
9712 goto do_binop;
9713
9714 case EQ_EXPR:
9715 op = DW_OP_eq;
9716 goto do_binop;
9717
9718 case NE_EXPR:
9719 op = DW_OP_ne;
9720 goto do_binop;
9721
9722 do_binop:
9723 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9724 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9725 if (ret == 0 || ret1 == 0)
9726 return 0;
9727
9728 add_loc_descr (&ret, ret1);
9729 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9730 break;
9731
9732 case TRUTH_NOT_EXPR:
9733 case BIT_NOT_EXPR:
9734 op = DW_OP_not;
9735 goto do_unop;
9736
9737 case ABS_EXPR:
9738 op = DW_OP_abs;
9739 goto do_unop;
9740
9741 case NEGATE_EXPR:
9742 op = DW_OP_neg;
9743 goto do_unop;
9744
9745 do_unop:
9746 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9747 if (ret == 0)
9748 return 0;
9749
9750 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9751 break;
9752
9753 case MIN_EXPR:
9754 case MAX_EXPR:
9755 {
9756 const enum tree_code code =
9757 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9758
9759 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9760 build2 (code, integer_type_node,
9761 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9762 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9763 }
9764
9765 /* ... fall through ... */
9766
9767 case COND_EXPR:
9768 {
9769 dw_loc_descr_ref lhs
9770 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9771 dw_loc_descr_ref rhs
9772 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9773 dw_loc_descr_ref bra_node, jump_node, tmp;
9774
9775 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9776 if (ret == 0 || lhs == 0 || rhs == 0)
9777 return 0;
9778
9779 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9780 add_loc_descr (&ret, bra_node);
9781
9782 add_loc_descr (&ret, rhs);
9783 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9784 add_loc_descr (&ret, jump_node);
9785
9786 add_loc_descr (&ret, lhs);
9787 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9788 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9789
9790 /* ??? Need a node to point the skip at. Use a nop. */
9791 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9792 add_loc_descr (&ret, tmp);
9793 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9794 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9795 }
9796 break;
9797
9798 case FIX_TRUNC_EXPR:
9799 return 0;
9800
9801 default:
9802 /* Leave front-end specific codes as simply unknown. This comes
9803 up, for instance, with the C STMT_EXPR. */
9804 if ((unsigned int) TREE_CODE (loc)
9805 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9806 return 0;
9807
9808 #ifdef ENABLE_CHECKING
9809 /* Otherwise this is a generic code; we should just lists all of
9810 these explicitly. We forgot one. */
9811 gcc_unreachable ();
9812 #else
9813 /* In a release build, we want to degrade gracefully: better to
9814 generate incomplete debugging information than to crash. */
9815 return NULL;
9816 #endif
9817 }
9818
9819 /* Show if we can't fill the request for an address. */
9820 if (want_address && !have_address)
9821 return 0;
9822
9823 /* If we've got an address and don't want one, dereference. */
9824 if (!want_address && have_address && ret)
9825 {
9826 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9827
9828 if (size > DWARF2_ADDR_SIZE || size == -1)
9829 return 0;
9830 else if (size == DWARF2_ADDR_SIZE)
9831 op = DW_OP_deref;
9832 else
9833 op = DW_OP_deref_size;
9834
9835 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9836 }
9837
9838 return ret;
9839 }
9840
9841 static inline dw_loc_descr_ref
9842 loc_descriptor_from_tree (tree loc)
9843 {
9844 return loc_descriptor_from_tree_1 (loc, 2);
9845 }
9846
9847 /* Given a value, round it up to the lowest multiple of `boundary'
9848 which is not less than the value itself. */
9849
9850 static inline HOST_WIDE_INT
9851 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9852 {
9853 return (((value + boundary - 1) / boundary) * boundary);
9854 }
9855
9856 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9857 pointer to the declared type for the relevant field variable, or return
9858 `integer_type_node' if the given node turns out to be an
9859 ERROR_MARK node. */
9860
9861 static inline tree
9862 field_type (const_tree decl)
9863 {
9864 tree type;
9865
9866 if (TREE_CODE (decl) == ERROR_MARK)
9867 return integer_type_node;
9868
9869 type = DECL_BIT_FIELD_TYPE (decl);
9870 if (type == NULL_TREE)
9871 type = TREE_TYPE (decl);
9872
9873 return type;
9874 }
9875
9876 /* Given a pointer to a tree node, return the alignment in bits for
9877 it, or else return BITS_PER_WORD if the node actually turns out to
9878 be an ERROR_MARK node. */
9879
9880 static inline unsigned
9881 simple_type_align_in_bits (const_tree type)
9882 {
9883 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9884 }
9885
9886 static inline unsigned
9887 simple_decl_align_in_bits (const_tree decl)
9888 {
9889 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9890 }
9891
9892 /* Return the result of rounding T up to ALIGN. */
9893
9894 static inline HOST_WIDE_INT
9895 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9896 {
9897 /* We must be careful if T is negative because HOST_WIDE_INT can be
9898 either "above" or "below" unsigned int as per the C promotion
9899 rules, depending on the host, thus making the signedness of the
9900 direct multiplication and division unpredictable. */
9901 unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9902
9903 u += align - 1;
9904 u /= align;
9905 u *= align;
9906
9907 return (HOST_WIDE_INT) u;
9908 }
9909
9910 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9911 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9912 or return 0 if we are unable to determine what that offset is, either
9913 because the argument turns out to be a pointer to an ERROR_MARK node, or
9914 because the offset is actually variable. (We can't handle the latter case
9915 just yet). */
9916
9917 static HOST_WIDE_INT
9918 field_byte_offset (const_tree decl)
9919 {
9920 HOST_WIDE_INT object_offset_in_bits;
9921 HOST_WIDE_INT bitpos_int;
9922
9923 if (TREE_CODE (decl) == ERROR_MARK)
9924 return 0;
9925
9926 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9927
9928 /* We cannot yet cope with fields whose positions are variable, so
9929 for now, when we see such things, we simply return 0. Someday, we may
9930 be able to handle such cases, but it will be damn difficult. */
9931 if (! host_integerp (bit_position (decl), 0))
9932 return 0;
9933
9934 bitpos_int = int_bit_position (decl);
9935
9936 #ifdef PCC_BITFIELD_TYPE_MATTERS
9937 if (PCC_BITFIELD_TYPE_MATTERS)
9938 {
9939 tree type;
9940 tree field_size_tree;
9941 HOST_WIDE_INT deepest_bitpos;
9942 unsigned HOST_WIDE_INT field_size_in_bits;
9943 unsigned int type_align_in_bits;
9944 unsigned int decl_align_in_bits;
9945 unsigned HOST_WIDE_INT type_size_in_bits;
9946
9947 type = field_type (decl);
9948 field_size_tree = DECL_SIZE (decl);
9949
9950 /* The size could be unspecified if there was an error, or for
9951 a flexible array member. */
9952 if (! field_size_tree)
9953 field_size_tree = bitsize_zero_node;
9954
9955 /* If we don't know the size of the field, pretend it's a full word. */
9956 if (host_integerp (field_size_tree, 1))
9957 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9958 else
9959 field_size_in_bits = BITS_PER_WORD;
9960
9961 type_size_in_bits = simple_type_size_in_bits (type);
9962 type_align_in_bits = simple_type_align_in_bits (type);
9963 decl_align_in_bits = simple_decl_align_in_bits (decl);
9964
9965 /* The GCC front-end doesn't make any attempt to keep track of the
9966 starting bit offset (relative to the start of the containing
9967 structure type) of the hypothetical "containing object" for a
9968 bit-field. Thus, when computing the byte offset value for the
9969 start of the "containing object" of a bit-field, we must deduce
9970 this information on our own. This can be rather tricky to do in
9971 some cases. For example, handling the following structure type
9972 definition when compiling for an i386/i486 target (which only
9973 aligns long long's to 32-bit boundaries) can be very tricky:
9974
9975 struct S { int field1; long long field2:31; };
9976
9977 Fortunately, there is a simple rule-of-thumb which can be used
9978 in such cases. When compiling for an i386/i486, GCC will
9979 allocate 8 bytes for the structure shown above. It decides to
9980 do this based upon one simple rule for bit-field allocation.
9981 GCC allocates each "containing object" for each bit-field at
9982 the first (i.e. lowest addressed) legitimate alignment boundary
9983 (based upon the required minimum alignment for the declared
9984 type of the field) which it can possibly use, subject to the
9985 condition that there is still enough available space remaining
9986 in the containing object (when allocated at the selected point)
9987 to fully accommodate all of the bits of the bit-field itself.
9988
9989 This simple rule makes it obvious why GCC allocates 8 bytes for
9990 each object of the structure type shown above. When looking
9991 for a place to allocate the "containing object" for `field2',
9992 the compiler simply tries to allocate a 64-bit "containing
9993 object" at each successive 32-bit boundary (starting at zero)
9994 until it finds a place to allocate that 64- bit field such that
9995 at least 31 contiguous (and previously unallocated) bits remain
9996 within that selected 64 bit field. (As it turns out, for the
9997 example above, the compiler finds it is OK to allocate the
9998 "containing object" 64-bit field at bit-offset zero within the
9999 structure type.)
10000
10001 Here we attempt to work backwards from the limited set of facts
10002 we're given, and we try to deduce from those facts, where GCC
10003 must have believed that the containing object started (within
10004 the structure type). The value we deduce is then used (by the
10005 callers of this routine) to generate DW_AT_location and
10006 DW_AT_bit_offset attributes for fields (both bit-fields and, in
10007 the case of DW_AT_location, regular fields as well). */
10008
10009 /* Figure out the bit-distance from the start of the structure to
10010 the "deepest" bit of the bit-field. */
10011 deepest_bitpos = bitpos_int + field_size_in_bits;
10012
10013 /* This is the tricky part. Use some fancy footwork to deduce
10014 where the lowest addressed bit of the containing object must
10015 be. */
10016 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10017
10018 /* Round up to type_align by default. This works best for
10019 bitfields. */
10020 object_offset_in_bits
10021 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10022
10023 if (object_offset_in_bits > bitpos_int)
10024 {
10025 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10026
10027 /* Round up to decl_align instead. */
10028 object_offset_in_bits
10029 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10030 }
10031 }
10032 else
10033 #endif
10034 object_offset_in_bits = bitpos_int;
10035
10036 return object_offset_in_bits / BITS_PER_UNIT;
10037 }
10038 \f
10039 /* The following routines define various Dwarf attributes and any data
10040 associated with them. */
10041
10042 /* Add a location description attribute value to a DIE.
10043
10044 This emits location attributes suitable for whole variables and
10045 whole parameters. Note that the location attributes for struct fields are
10046 generated by the routine `data_member_location_attribute' below. */
10047
10048 static inline void
10049 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10050 dw_loc_descr_ref descr)
10051 {
10052 if (descr != 0)
10053 add_AT_loc (die, attr_kind, descr);
10054 }
10055
10056 /* Attach the specialized form of location attribute used for data members of
10057 struct and union types. In the special case of a FIELD_DECL node which
10058 represents a bit-field, the "offset" part of this special location
10059 descriptor must indicate the distance in bytes from the lowest-addressed
10060 byte of the containing struct or union type to the lowest-addressed byte of
10061 the "containing object" for the bit-field. (See the `field_byte_offset'
10062 function above).
10063
10064 For any given bit-field, the "containing object" is a hypothetical object
10065 (of some integral or enum type) within which the given bit-field lives. The
10066 type of this hypothetical "containing object" is always the same as the
10067 declared type of the individual bit-field itself (for GCC anyway... the
10068 DWARF spec doesn't actually mandate this). Note that it is the size (in
10069 bytes) of the hypothetical "containing object" which will be given in the
10070 DW_AT_byte_size attribute for this bit-field. (See the
10071 `byte_size_attribute' function below.) It is also used when calculating the
10072 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
10073 function below.) */
10074
10075 static void
10076 add_data_member_location_attribute (dw_die_ref die, tree decl)
10077 {
10078 HOST_WIDE_INT offset;
10079 dw_loc_descr_ref loc_descr = 0;
10080
10081 if (TREE_CODE (decl) == TREE_BINFO)
10082 {
10083 /* We're working on the TAG_inheritance for a base class. */
10084 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10085 {
10086 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10087 aren't at a fixed offset from all (sub)objects of the same
10088 type. We need to extract the appropriate offset from our
10089 vtable. The following dwarf expression means
10090
10091 BaseAddr = ObAddr + *((*ObAddr) - Offset)
10092
10093 This is specific to the V3 ABI, of course. */
10094
10095 dw_loc_descr_ref tmp;
10096
10097 /* Make a copy of the object address. */
10098 tmp = new_loc_descr (DW_OP_dup, 0, 0);
10099 add_loc_descr (&loc_descr, tmp);
10100
10101 /* Extract the vtable address. */
10102 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10103 add_loc_descr (&loc_descr, tmp);
10104
10105 /* Calculate the address of the offset. */
10106 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10107 gcc_assert (offset < 0);
10108
10109 tmp = int_loc_descriptor (-offset);
10110 add_loc_descr (&loc_descr, tmp);
10111 tmp = new_loc_descr (DW_OP_minus, 0, 0);
10112 add_loc_descr (&loc_descr, tmp);
10113
10114 /* Extract the offset. */
10115 tmp = new_loc_descr (DW_OP_deref, 0, 0);
10116 add_loc_descr (&loc_descr, tmp);
10117
10118 /* Add it to the object address. */
10119 tmp = new_loc_descr (DW_OP_plus, 0, 0);
10120 add_loc_descr (&loc_descr, tmp);
10121 }
10122 else
10123 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10124 }
10125 else
10126 offset = field_byte_offset (decl);
10127
10128 if (! loc_descr)
10129 {
10130 enum dwarf_location_atom op;
10131
10132 /* The DWARF2 standard says that we should assume that the structure
10133 address is already on the stack, so we can specify a structure field
10134 address by using DW_OP_plus_uconst. */
10135
10136 #ifdef MIPS_DEBUGGING_INFO
10137 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10138 operator correctly. It works only if we leave the offset on the
10139 stack. */
10140 op = DW_OP_constu;
10141 #else
10142 op = DW_OP_plus_uconst;
10143 #endif
10144
10145 loc_descr = new_loc_descr (op, offset, 0);
10146 }
10147
10148 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10149 }
10150
10151 /* Writes integer values to dw_vec_const array. */
10152
10153 static void
10154 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10155 {
10156 while (size != 0)
10157 {
10158 *dest++ = val & 0xff;
10159 val >>= 8;
10160 --size;
10161 }
10162 }
10163
10164 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
10165
10166 static HOST_WIDE_INT
10167 extract_int (const unsigned char *src, unsigned int size)
10168 {
10169 HOST_WIDE_INT val = 0;
10170
10171 src += size;
10172 while (size != 0)
10173 {
10174 val <<= 8;
10175 val |= *--src & 0xff;
10176 --size;
10177 }
10178 return val;
10179 }
10180
10181 /* Writes floating point values to dw_vec_const array. */
10182
10183 static void
10184 insert_float (const_rtx rtl, unsigned char *array)
10185 {
10186 REAL_VALUE_TYPE rv;
10187 long val[4];
10188 int i;
10189
10190 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10191 real_to_target (val, &rv, GET_MODE (rtl));
10192
10193 /* real_to_target puts 32-bit pieces in each long. Pack them. */
10194 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10195 {
10196 insert_int (val[i], 4, array);
10197 array += 4;
10198 }
10199 }
10200
10201 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10202 does not have a "location" either in memory or in a register. These
10203 things can arise in GNU C when a constant is passed as an actual parameter
10204 to an inlined function. They can also arise in C++ where declared
10205 constants do not necessarily get memory "homes". */
10206
10207 static void
10208 add_const_value_attribute (dw_die_ref die, rtx rtl)
10209 {
10210 switch (GET_CODE (rtl))
10211 {
10212 case CONST_INT:
10213 {
10214 HOST_WIDE_INT val = INTVAL (rtl);
10215
10216 if (val < 0)
10217 add_AT_int (die, DW_AT_const_value, val);
10218 else
10219 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10220 }
10221 break;
10222
10223 case CONST_DOUBLE:
10224 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10225 floating-point constant. A CONST_DOUBLE is used whenever the
10226 constant requires more than one word in order to be adequately
10227 represented. We output CONST_DOUBLEs as blocks. */
10228 {
10229 enum machine_mode mode = GET_MODE (rtl);
10230
10231 if (SCALAR_FLOAT_MODE_P (mode))
10232 {
10233 unsigned int length = GET_MODE_SIZE (mode);
10234 unsigned char *array = ggc_alloc (length);
10235
10236 insert_float (rtl, array);
10237 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10238 }
10239 else
10240 {
10241 /* ??? We really should be using HOST_WIDE_INT throughout. */
10242 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10243
10244 add_AT_long_long (die, DW_AT_const_value,
10245 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10246 }
10247 }
10248 break;
10249
10250 case CONST_VECTOR:
10251 {
10252 enum machine_mode mode = GET_MODE (rtl);
10253 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10254 unsigned int length = CONST_VECTOR_NUNITS (rtl);
10255 unsigned char *array = ggc_alloc (length * elt_size);
10256 unsigned int i;
10257 unsigned char *p;
10258
10259 switch (GET_MODE_CLASS (mode))
10260 {
10261 case MODE_VECTOR_INT:
10262 for (i = 0, p = array; i < length; i++, p += elt_size)
10263 {
10264 rtx elt = CONST_VECTOR_ELT (rtl, i);
10265 HOST_WIDE_INT lo, hi;
10266
10267 switch (GET_CODE (elt))
10268 {
10269 case CONST_INT:
10270 lo = INTVAL (elt);
10271 hi = -(lo < 0);
10272 break;
10273
10274 case CONST_DOUBLE:
10275 lo = CONST_DOUBLE_LOW (elt);
10276 hi = CONST_DOUBLE_HIGH (elt);
10277 break;
10278
10279 default:
10280 gcc_unreachable ();
10281 }
10282
10283 if (elt_size <= sizeof (HOST_WIDE_INT))
10284 insert_int (lo, elt_size, p);
10285 else
10286 {
10287 unsigned char *p0 = p;
10288 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10289
10290 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10291 if (WORDS_BIG_ENDIAN)
10292 {
10293 p0 = p1;
10294 p1 = p;
10295 }
10296 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10297 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10298 }
10299 }
10300 break;
10301
10302 case MODE_VECTOR_FLOAT:
10303 for (i = 0, p = array; i < length; i++, p += elt_size)
10304 {
10305 rtx elt = CONST_VECTOR_ELT (rtl, i);
10306 insert_float (elt, p);
10307 }
10308 break;
10309
10310 default:
10311 gcc_unreachable ();
10312 }
10313
10314 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10315 }
10316 break;
10317
10318 case CONST_STRING:
10319 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10320 break;
10321
10322 case SYMBOL_REF:
10323 case LABEL_REF:
10324 case CONST:
10325 add_AT_addr (die, DW_AT_const_value, rtl);
10326 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10327 break;
10328
10329 case PLUS:
10330 /* In cases where an inlined instance of an inline function is passed
10331 the address of an `auto' variable (which is local to the caller) we
10332 can get a situation where the DECL_RTL of the artificial local
10333 variable (for the inlining) which acts as a stand-in for the
10334 corresponding formal parameter (of the inline function) will look
10335 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
10336 exactly a compile-time constant expression, but it isn't the address
10337 of the (artificial) local variable either. Rather, it represents the
10338 *value* which the artificial local variable always has during its
10339 lifetime. We currently have no way to represent such quasi-constant
10340 values in Dwarf, so for now we just punt and generate nothing. */
10341 break;
10342
10343 default:
10344 /* No other kinds of rtx should be possible here. */
10345 gcc_unreachable ();
10346 }
10347
10348 }
10349
10350 /* Determine whether the evaluation of EXPR references any variables
10351 or functions which aren't otherwise used (and therefore may not be
10352 output). */
10353 static tree
10354 reference_to_unused (tree * tp, int * walk_subtrees,
10355 void * data ATTRIBUTE_UNUSED)
10356 {
10357 if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10358 *walk_subtrees = 0;
10359
10360 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10361 && ! TREE_ASM_WRITTEN (*tp))
10362 return *tp;
10363 else if (!flag_unit_at_a_time)
10364 return NULL_TREE;
10365 /* ??? The C++ FE emits debug information for using decls, so
10366 putting gcc_unreachable here falls over. See PR31899. For now
10367 be conservative. */
10368 else if (!cgraph_global_info_ready
10369 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10370 return *tp;
10371 else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10372 {
10373 struct varpool_node *node = varpool_node (*tp);
10374 if (!node->needed)
10375 return *tp;
10376 }
10377 else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10378 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10379 {
10380 struct cgraph_node *node = cgraph_node (*tp);
10381 if (!node->output)
10382 return *tp;
10383 }
10384 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10385 return *tp;
10386
10387 return NULL_TREE;
10388 }
10389
10390 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10391 for use in a later add_const_value_attribute call. */
10392
10393 static rtx
10394 rtl_for_decl_init (tree init, tree type)
10395 {
10396 rtx rtl = NULL_RTX;
10397
10398 /* If a variable is initialized with a string constant without embedded
10399 zeros, build CONST_STRING. */
10400 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10401 {
10402 tree enttype = TREE_TYPE (type);
10403 tree domain = TYPE_DOMAIN (type);
10404 enum machine_mode mode = TYPE_MODE (enttype);
10405
10406 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10407 && domain
10408 && integer_zerop (TYPE_MIN_VALUE (domain))
10409 && compare_tree_int (TYPE_MAX_VALUE (domain),
10410 TREE_STRING_LENGTH (init) - 1) == 0
10411 && ((size_t) TREE_STRING_LENGTH (init)
10412 == strlen (TREE_STRING_POINTER (init)) + 1))
10413 rtl = gen_rtx_CONST_STRING (VOIDmode,
10414 ggc_strdup (TREE_STRING_POINTER (init)));
10415 }
10416 /* Other aggregates, and complex values, could be represented using
10417 CONCAT: FIXME! */
10418 else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10419 ;
10420 /* Vectors only work if their mode is supported by the target.
10421 FIXME: generic vectors ought to work too. */
10422 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10423 ;
10424 /* If the initializer is something that we know will expand into an
10425 immediate RTL constant, expand it now. We must be careful not to
10426 reference variables which won't be output. */
10427 else if (initializer_constant_valid_p (init, type)
10428 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10429 {
10430 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10431 possible. */
10432 if (TREE_CODE (type) == VECTOR_TYPE)
10433 switch (TREE_CODE (init))
10434 {
10435 case VECTOR_CST:
10436 break;
10437 case CONSTRUCTOR:
10438 if (TREE_CONSTANT (init))
10439 {
10440 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10441 bool constant_p = true;
10442 tree value;
10443 unsigned HOST_WIDE_INT ix;
10444
10445 /* Even when ctor is constant, it might contain non-*_CST
10446 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10447 belong into VECTOR_CST nodes. */
10448 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10449 if (!CONSTANT_CLASS_P (value))
10450 {
10451 constant_p = false;
10452 break;
10453 }
10454
10455 if (constant_p)
10456 {
10457 init = build_vector_from_ctor (type, elts);
10458 break;
10459 }
10460 }
10461 /* FALLTHRU */
10462
10463 default:
10464 return NULL;
10465 }
10466
10467 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10468
10469 /* If expand_expr returns a MEM, it wasn't immediate. */
10470 gcc_assert (!rtl || !MEM_P (rtl));
10471 }
10472
10473 return rtl;
10474 }
10475
10476 /* Generate RTL for the variable DECL to represent its location. */
10477
10478 static rtx
10479 rtl_for_decl_location (tree decl)
10480 {
10481 rtx rtl;
10482
10483 /* Here we have to decide where we are going to say the parameter "lives"
10484 (as far as the debugger is concerned). We only have a couple of
10485 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10486
10487 DECL_RTL normally indicates where the parameter lives during most of the
10488 activation of the function. If optimization is enabled however, this
10489 could be either NULL or else a pseudo-reg. Both of those cases indicate
10490 that the parameter doesn't really live anywhere (as far as the code
10491 generation parts of GCC are concerned) during most of the function's
10492 activation. That will happen (for example) if the parameter is never
10493 referenced within the function.
10494
10495 We could just generate a location descriptor here for all non-NULL
10496 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10497 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10498 where DECL_RTL is NULL or is a pseudo-reg.
10499
10500 Note however that we can only get away with using DECL_INCOMING_RTL as
10501 a backup substitute for DECL_RTL in certain limited cases. In cases
10502 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10503 we can be sure that the parameter was passed using the same type as it is
10504 declared to have within the function, and that its DECL_INCOMING_RTL
10505 points us to a place where a value of that type is passed.
10506
10507 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10508 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10509 because in these cases DECL_INCOMING_RTL points us to a value of some
10510 type which is *different* from the type of the parameter itself. Thus,
10511 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10512 such cases, the debugger would end up (for example) trying to fetch a
10513 `float' from a place which actually contains the first part of a
10514 `double'. That would lead to really incorrect and confusing
10515 output at debug-time.
10516
10517 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10518 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
10519 are a couple of exceptions however. On little-endian machines we can
10520 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10521 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10522 an integral type that is smaller than TREE_TYPE (decl). These cases arise
10523 when (on a little-endian machine) a non-prototyped function has a
10524 parameter declared to be of type `short' or `char'. In such cases,
10525 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10526 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10527 passed `int' value. If the debugger then uses that address to fetch
10528 a `short' or a `char' (on a little-endian machine) the result will be
10529 the correct data, so we allow for such exceptional cases below.
10530
10531 Note that our goal here is to describe the place where the given formal
10532 parameter lives during most of the function's activation (i.e. between the
10533 end of the prologue and the start of the epilogue). We'll do that as best
10534 as we can. Note however that if the given formal parameter is modified
10535 sometime during the execution of the function, then a stack backtrace (at
10536 debug-time) will show the function as having been called with the *new*
10537 value rather than the value which was originally passed in. This happens
10538 rarely enough that it is not a major problem, but it *is* a problem, and
10539 I'd like to fix it.
10540
10541 A future version of dwarf2out.c may generate two additional attributes for
10542 any given DW_TAG_formal_parameter DIE which will describe the "passed
10543 type" and the "passed location" for the given formal parameter in addition
10544 to the attributes we now generate to indicate the "declared type" and the
10545 "active location" for each parameter. This additional set of attributes
10546 could be used by debuggers for stack backtraces. Separately, note that
10547 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10548 This happens (for example) for inlined-instances of inline function formal
10549 parameters which are never referenced. This really shouldn't be
10550 happening. All PARM_DECL nodes should get valid non-NULL
10551 DECL_INCOMING_RTL values. FIXME. */
10552
10553 /* Use DECL_RTL as the "location" unless we find something better. */
10554 rtl = DECL_RTL_IF_SET (decl);
10555
10556 /* When generating abstract instances, ignore everything except
10557 constants, symbols living in memory, and symbols living in
10558 fixed registers. */
10559 if (! reload_completed)
10560 {
10561 if (rtl
10562 && (CONSTANT_P (rtl)
10563 || (MEM_P (rtl)
10564 && CONSTANT_P (XEXP (rtl, 0)))
10565 || (REG_P (rtl)
10566 && TREE_CODE (decl) == VAR_DECL
10567 && TREE_STATIC (decl))))
10568 {
10569 rtl = targetm.delegitimize_address (rtl);
10570 return rtl;
10571 }
10572 rtl = NULL_RTX;
10573 }
10574 else if (TREE_CODE (decl) == PARM_DECL)
10575 {
10576 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10577 {
10578 tree declared_type = TREE_TYPE (decl);
10579 tree passed_type = DECL_ARG_TYPE (decl);
10580 enum machine_mode dmode = TYPE_MODE (declared_type);
10581 enum machine_mode pmode = TYPE_MODE (passed_type);
10582
10583 /* This decl represents a formal parameter which was optimized out.
10584 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10585 all cases where (rtl == NULL_RTX) just below. */
10586 if (dmode == pmode)
10587 rtl = DECL_INCOMING_RTL (decl);
10588 else if (SCALAR_INT_MODE_P (dmode)
10589 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10590 && DECL_INCOMING_RTL (decl))
10591 {
10592 rtx inc = DECL_INCOMING_RTL (decl);
10593 if (REG_P (inc))
10594 rtl = inc;
10595 else if (MEM_P (inc))
10596 {
10597 if (BYTES_BIG_ENDIAN)
10598 rtl = adjust_address_nv (inc, dmode,
10599 GET_MODE_SIZE (pmode)
10600 - GET_MODE_SIZE (dmode));
10601 else
10602 rtl = inc;
10603 }
10604 }
10605 }
10606
10607 /* If the parm was passed in registers, but lives on the stack, then
10608 make a big endian correction if the mode of the type of the
10609 parameter is not the same as the mode of the rtl. */
10610 /* ??? This is the same series of checks that are made in dbxout.c before
10611 we reach the big endian correction code there. It isn't clear if all
10612 of these checks are necessary here, but keeping them all is the safe
10613 thing to do. */
10614 else if (MEM_P (rtl)
10615 && XEXP (rtl, 0) != const0_rtx
10616 && ! CONSTANT_P (XEXP (rtl, 0))
10617 /* Not passed in memory. */
10618 && !MEM_P (DECL_INCOMING_RTL (decl))
10619 /* Not passed by invisible reference. */
10620 && (!REG_P (XEXP (rtl, 0))
10621 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10622 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10623 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10624 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10625 #endif
10626 )
10627 /* Big endian correction check. */
10628 && BYTES_BIG_ENDIAN
10629 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10630 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10631 < UNITS_PER_WORD))
10632 {
10633 int offset = (UNITS_PER_WORD
10634 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10635
10636 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10637 plus_constant (XEXP (rtl, 0), offset));
10638 }
10639 }
10640 else if (TREE_CODE (decl) == VAR_DECL
10641 && rtl
10642 && MEM_P (rtl)
10643 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10644 && BYTES_BIG_ENDIAN)
10645 {
10646 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10647 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10648
10649 /* If a variable is declared "register" yet is smaller than
10650 a register, then if we store the variable to memory, it
10651 looks like we're storing a register-sized value, when in
10652 fact we are not. We need to adjust the offset of the
10653 storage location to reflect the actual value's bytes,
10654 else gdb will not be able to display it. */
10655 if (rsize > dsize)
10656 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10657 plus_constant (XEXP (rtl, 0), rsize-dsize));
10658 }
10659
10660 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10661 and will have been substituted directly into all expressions that use it.
10662 C does not have such a concept, but C++ and other languages do. */
10663 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10664 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10665
10666 if (rtl)
10667 rtl = targetm.delegitimize_address (rtl);
10668
10669 /* If we don't look past the constant pool, we risk emitting a
10670 reference to a constant pool entry that isn't referenced from
10671 code, and thus is not emitted. */
10672 if (rtl)
10673 rtl = avoid_constant_pool_reference (rtl);
10674
10675 return rtl;
10676 }
10677
10678 /* We need to figure out what section we should use as the base for the
10679 address ranges where a given location is valid.
10680 1. If this particular DECL has a section associated with it, use that.
10681 2. If this function has a section associated with it, use that.
10682 3. Otherwise, use the text section.
10683 XXX: If you split a variable across multiple sections, we won't notice. */
10684
10685 static const char *
10686 secname_for_decl (const_tree decl)
10687 {
10688 const char *secname;
10689
10690 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10691 {
10692 tree sectree = DECL_SECTION_NAME (decl);
10693 secname = TREE_STRING_POINTER (sectree);
10694 }
10695 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10696 {
10697 tree sectree = DECL_SECTION_NAME (current_function_decl);
10698 secname = TREE_STRING_POINTER (sectree);
10699 }
10700 else if (cfun && in_cold_section_p)
10701 secname = cfun->cold_section_label;
10702 else
10703 secname = text_section_label;
10704
10705 return secname;
10706 }
10707
10708 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10709 data attribute for a variable or a parameter. We generate the
10710 DW_AT_const_value attribute only in those cases where the given variable
10711 or parameter does not have a true "location" either in memory or in a
10712 register. This can happen (for example) when a constant is passed as an
10713 actual argument in a call to an inline function. (It's possible that
10714 these things can crop up in other ways also.) Note that one type of
10715 constant value which can be passed into an inlined function is a constant
10716 pointer. This can happen for example if an actual argument in an inlined
10717 function call evaluates to a compile-time constant address. */
10718
10719 static void
10720 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10721 enum dwarf_attribute attr)
10722 {
10723 rtx rtl;
10724 dw_loc_descr_ref descr;
10725 var_loc_list *loc_list;
10726 struct var_loc_node *node;
10727 if (TREE_CODE (decl) == ERROR_MARK)
10728 return;
10729
10730 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10731 || TREE_CODE (decl) == RESULT_DECL);
10732
10733 /* See if we possibly have multiple locations for this variable. */
10734 loc_list = lookup_decl_loc (decl);
10735
10736 /* If it truly has multiple locations, the first and last node will
10737 differ. */
10738 if (loc_list && loc_list->first != loc_list->last)
10739 {
10740 const char *endname, *secname;
10741 dw_loc_list_ref list;
10742 rtx varloc;
10743 enum var_init_status initialized;
10744
10745 /* Now that we know what section we are using for a base,
10746 actually construct the list of locations.
10747 The first location information is what is passed to the
10748 function that creates the location list, and the remaining
10749 locations just get added on to that list.
10750 Note that we only know the start address for a location
10751 (IE location changes), so to build the range, we use
10752 the range [current location start, next location start].
10753 This means we have to special case the last node, and generate
10754 a range of [last location start, end of function label]. */
10755
10756 node = loc_list->first;
10757 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10758 secname = secname_for_decl (decl);
10759
10760 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10761 initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10762 else
10763 initialized = VAR_INIT_STATUS_INITIALIZED;
10764
10765 list = new_loc_list (loc_descriptor (varloc, initialized),
10766 node->label, node->next->label, secname, 1);
10767 node = node->next;
10768
10769 for (; node->next; node = node->next)
10770 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10771 {
10772 /* The variable has a location between NODE->LABEL and
10773 NODE->NEXT->LABEL. */
10774 enum var_init_status initialized =
10775 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10776 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10777 add_loc_descr_to_loc_list (&list,
10778 loc_descriptor (varloc, initialized),
10779 node->label, node->next->label, secname);
10780 }
10781
10782 /* If the variable has a location at the last label
10783 it keeps its location until the end of function. */
10784 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10785 {
10786 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10787 enum var_init_status initialized =
10788 NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10789
10790 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10791 if (!current_function_decl)
10792 endname = text_end_label;
10793 else
10794 {
10795 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10796 current_function_funcdef_no);
10797 endname = ggc_strdup (label_id);
10798 }
10799 add_loc_descr_to_loc_list (&list,
10800 loc_descriptor (varloc, initialized),
10801 node->label, endname, secname);
10802 }
10803
10804 /* Finally, add the location list to the DIE, and we are done. */
10805 add_AT_loc_list (die, attr, list);
10806 return;
10807 }
10808
10809 /* Try to get some constant RTL for this decl, and use that as the value of
10810 the location. */
10811
10812 rtl = rtl_for_decl_location (decl);
10813 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10814 {
10815 add_const_value_attribute (die, rtl);
10816 return;
10817 }
10818
10819 /* If we have tried to generate the location otherwise, and it
10820 didn't work out (we wouldn't be here if we did), and we have a one entry
10821 location list, try generating a location from that. */
10822 if (loc_list && loc_list->first)
10823 {
10824 enum var_init_status status;
10825 node = loc_list->first;
10826 status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10827 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10828 if (descr)
10829 {
10830 add_AT_location_description (die, attr, descr);
10831 return;
10832 }
10833 }
10834
10835 /* We couldn't get any rtl, so try directly generating the location
10836 description from the tree. */
10837 descr = loc_descriptor_from_tree (decl);
10838 if (descr)
10839 {
10840 add_AT_location_description (die, attr, descr);
10841 return;
10842 }
10843 /* None of that worked, so it must not really have a location;
10844 try adding a constant value attribute from the DECL_INITIAL. */
10845 tree_add_const_value_attribute (die, decl);
10846 }
10847
10848 /* If we don't have a copy of this variable in memory for some reason (such
10849 as a C++ member constant that doesn't have an out-of-line definition),
10850 we should tell the debugger about the constant value. */
10851
10852 static void
10853 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10854 {
10855 tree init = DECL_INITIAL (decl);
10856 tree type = TREE_TYPE (decl);
10857 rtx rtl;
10858
10859 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10860 /* OK */;
10861 else
10862 return;
10863
10864 rtl = rtl_for_decl_init (init, type);
10865 if (rtl)
10866 add_const_value_attribute (var_die, rtl);
10867 }
10868
10869 /* Convert the CFI instructions for the current function into a
10870 location list. This is used for DW_AT_frame_base when we targeting
10871 a dwarf2 consumer that does not support the dwarf3
10872 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
10873 expressions. */
10874
10875 static dw_loc_list_ref
10876 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10877 {
10878 dw_fde_ref fde;
10879 dw_loc_list_ref list, *list_tail;
10880 dw_cfi_ref cfi;
10881 dw_cfa_location last_cfa, next_cfa;
10882 const char *start_label, *last_label, *section;
10883
10884 fde = &fde_table[fde_table_in_use - 1];
10885
10886 section = secname_for_decl (current_function_decl);
10887 list_tail = &list;
10888 list = NULL;
10889
10890 next_cfa.reg = INVALID_REGNUM;
10891 next_cfa.offset = 0;
10892 next_cfa.indirect = 0;
10893 next_cfa.base_offset = 0;
10894
10895 start_label = fde->dw_fde_begin;
10896
10897 /* ??? Bald assumption that the CIE opcode list does not contain
10898 advance opcodes. */
10899 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10900 lookup_cfa_1 (cfi, &next_cfa);
10901
10902 last_cfa = next_cfa;
10903 last_label = start_label;
10904
10905 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10906 switch (cfi->dw_cfi_opc)
10907 {
10908 case DW_CFA_set_loc:
10909 case DW_CFA_advance_loc1:
10910 case DW_CFA_advance_loc2:
10911 case DW_CFA_advance_loc4:
10912 if (!cfa_equal_p (&last_cfa, &next_cfa))
10913 {
10914 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10915 start_label, last_label, section,
10916 list == NULL);
10917
10918 list_tail = &(*list_tail)->dw_loc_next;
10919 last_cfa = next_cfa;
10920 start_label = last_label;
10921 }
10922 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10923 break;
10924
10925 case DW_CFA_advance_loc:
10926 /* The encoding is complex enough that we should never emit this. */
10927 case DW_CFA_remember_state:
10928 case DW_CFA_restore_state:
10929 /* We don't handle these two in this function. It would be possible
10930 if it were to be required. */
10931 gcc_unreachable ();
10932
10933 default:
10934 lookup_cfa_1 (cfi, &next_cfa);
10935 break;
10936 }
10937
10938 if (!cfa_equal_p (&last_cfa, &next_cfa))
10939 {
10940 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10941 start_label, last_label, section,
10942 list == NULL);
10943 list_tail = &(*list_tail)->dw_loc_next;
10944 start_label = last_label;
10945 }
10946 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10947 start_label, fde->dw_fde_end, section,
10948 list == NULL);
10949
10950 return list;
10951 }
10952
10953 /* Compute a displacement from the "steady-state frame pointer" to the
10954 frame base (often the same as the CFA), and store it in
10955 frame_pointer_fb_offset. OFFSET is added to the displacement
10956 before the latter is negated. */
10957
10958 static void
10959 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10960 {
10961 rtx reg, elim;
10962
10963 #ifdef FRAME_POINTER_CFA_OFFSET
10964 reg = frame_pointer_rtx;
10965 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10966 #else
10967 reg = arg_pointer_rtx;
10968 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10969 #endif
10970
10971 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10972 if (GET_CODE (elim) == PLUS)
10973 {
10974 offset += INTVAL (XEXP (elim, 1));
10975 elim = XEXP (elim, 0);
10976 }
10977 gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10978 : stack_pointer_rtx));
10979
10980 frame_pointer_fb_offset = -offset;
10981 }
10982
10983 /* Generate a DW_AT_name attribute given some string value to be included as
10984 the value of the attribute. */
10985
10986 static void
10987 add_name_attribute (dw_die_ref die, const char *name_string)
10988 {
10989 if (name_string != NULL && *name_string != 0)
10990 {
10991 if (demangle_name_func)
10992 name_string = (*demangle_name_func) (name_string);
10993
10994 add_AT_string (die, DW_AT_name, name_string);
10995 }
10996 }
10997
10998 /* Generate a DW_AT_comp_dir attribute for DIE. */
10999
11000 static void
11001 add_comp_dir_attribute (dw_die_ref die)
11002 {
11003 const char *wd = get_src_pwd ();
11004 if (wd != NULL)
11005 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11006 }
11007
11008 /* Given a tree node describing an array bound (either lower or upper) output
11009 a representation for that bound. */
11010
11011 static void
11012 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11013 {
11014 switch (TREE_CODE (bound))
11015 {
11016 case ERROR_MARK:
11017 return;
11018
11019 /* All fixed-bounds are represented by INTEGER_CST nodes. */
11020 case INTEGER_CST:
11021 if (! host_integerp (bound, 0)
11022 || (bound_attr == DW_AT_lower_bound
11023 && (((is_c_family () || is_java ()) && integer_zerop (bound))
11024 || (is_fortran () && integer_onep (bound)))))
11025 /* Use the default. */
11026 ;
11027 else
11028 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11029 break;
11030
11031 case CONVERT_EXPR:
11032 case NOP_EXPR:
11033 case NON_LVALUE_EXPR:
11034 case VIEW_CONVERT_EXPR:
11035 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11036 break;
11037
11038 case SAVE_EXPR:
11039 break;
11040
11041 case VAR_DECL:
11042 case PARM_DECL:
11043 case RESULT_DECL:
11044 {
11045 dw_die_ref decl_die = lookup_decl_die (bound);
11046
11047 /* ??? Can this happen, or should the variable have been bound
11048 first? Probably it can, since I imagine that we try to create
11049 the types of parameters in the order in which they exist in
11050 the list, and won't have created a forward reference to a
11051 later parameter. */
11052 if (decl_die != NULL)
11053 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11054 break;
11055 }
11056
11057 default:
11058 {
11059 /* Otherwise try to create a stack operation procedure to
11060 evaluate the value of the array bound. */
11061
11062 dw_die_ref ctx, decl_die;
11063 dw_loc_descr_ref loc;
11064
11065 loc = loc_descriptor_from_tree (bound);
11066 if (loc == NULL)
11067 break;
11068
11069 if (current_function_decl == 0)
11070 ctx = comp_unit_die;
11071 else
11072 ctx = lookup_decl_die (current_function_decl);
11073
11074 decl_die = new_die (DW_TAG_variable, ctx, bound);
11075 add_AT_flag (decl_die, DW_AT_artificial, 1);
11076 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11077 add_AT_loc (decl_die, DW_AT_location, loc);
11078
11079 add_AT_die_ref (subrange_die, bound_attr, decl_die);
11080 break;
11081 }
11082 }
11083 }
11084
11085 /* Note that the block of subscript information for an array type also
11086 includes information about the element type of type given array type. */
11087
11088 static void
11089 add_subscript_info (dw_die_ref type_die, tree type)
11090 {
11091 #ifndef MIPS_DEBUGGING_INFO
11092 unsigned dimension_number;
11093 #endif
11094 tree lower, upper;
11095 dw_die_ref subrange_die;
11096
11097 /* The GNU compilers represent multidimensional array types as sequences of
11098 one dimensional array types whose element types are themselves array
11099 types. Here we squish that down, so that each multidimensional array
11100 type gets only one array_type DIE in the Dwarf debugging info. The draft
11101 Dwarf specification say that we are allowed to do this kind of
11102 compression in C (because there is no difference between an array or
11103 arrays and a multidimensional array in C) but for other source languages
11104 (e.g. Ada) we probably shouldn't do this. */
11105
11106 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11107 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11108 We work around this by disabling this feature. See also
11109 gen_array_type_die. */
11110 #ifndef MIPS_DEBUGGING_INFO
11111 for (dimension_number = 0;
11112 TREE_CODE (type) == ARRAY_TYPE;
11113 type = TREE_TYPE (type), dimension_number++)
11114 #endif
11115 {
11116 tree domain = TYPE_DOMAIN (type);
11117
11118 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11119 and (in GNU C only) variable bounds. Handle all three forms
11120 here. */
11121 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11122 if (domain)
11123 {
11124 /* We have an array type with specified bounds. */
11125 lower = TYPE_MIN_VALUE (domain);
11126 upper = TYPE_MAX_VALUE (domain);
11127
11128 /* Define the index type. */
11129 if (TREE_TYPE (domain))
11130 {
11131 /* ??? This is probably an Ada unnamed subrange type. Ignore the
11132 TREE_TYPE field. We can't emit debug info for this
11133 because it is an unnamed integral type. */
11134 if (TREE_CODE (domain) == INTEGER_TYPE
11135 && TYPE_NAME (domain) == NULL_TREE
11136 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11137 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11138 ;
11139 else
11140 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11141 type_die);
11142 }
11143
11144 /* ??? If upper is NULL, the array has unspecified length,
11145 but it does have a lower bound. This happens with Fortran
11146 dimension arr(N:*)
11147 Since the debugger is definitely going to need to know N
11148 to produce useful results, go ahead and output the lower
11149 bound solo, and hope the debugger can cope. */
11150
11151 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11152 if (upper)
11153 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11154 }
11155
11156 /* Otherwise we have an array type with an unspecified length. The
11157 DWARF-2 spec does not say how to handle this; let's just leave out the
11158 bounds. */
11159 }
11160 }
11161
11162 static void
11163 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11164 {
11165 unsigned size;
11166
11167 switch (TREE_CODE (tree_node))
11168 {
11169 case ERROR_MARK:
11170 size = 0;
11171 break;
11172 case ENUMERAL_TYPE:
11173 case RECORD_TYPE:
11174 case UNION_TYPE:
11175 case QUAL_UNION_TYPE:
11176 size = int_size_in_bytes (tree_node);
11177 break;
11178 case FIELD_DECL:
11179 /* For a data member of a struct or union, the DW_AT_byte_size is
11180 generally given as the number of bytes normally allocated for an
11181 object of the *declared* type of the member itself. This is true
11182 even for bit-fields. */
11183 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11184 break;
11185 default:
11186 gcc_unreachable ();
11187 }
11188
11189 /* Note that `size' might be -1 when we get to this point. If it is, that
11190 indicates that the byte size of the entity in question is variable. We
11191 have no good way of expressing this fact in Dwarf at the present time,
11192 so just let the -1 pass on through. */
11193 add_AT_unsigned (die, DW_AT_byte_size, size);
11194 }
11195
11196 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11197 which specifies the distance in bits from the highest order bit of the
11198 "containing object" for the bit-field to the highest order bit of the
11199 bit-field itself.
11200
11201 For any given bit-field, the "containing object" is a hypothetical object
11202 (of some integral or enum type) within which the given bit-field lives. The
11203 type of this hypothetical "containing object" is always the same as the
11204 declared type of the individual bit-field itself. The determination of the
11205 exact location of the "containing object" for a bit-field is rather
11206 complicated. It's handled by the `field_byte_offset' function (above).
11207
11208 Note that it is the size (in bytes) of the hypothetical "containing object"
11209 which will be given in the DW_AT_byte_size attribute for this bit-field.
11210 (See `byte_size_attribute' above). */
11211
11212 static inline void
11213 add_bit_offset_attribute (dw_die_ref die, tree decl)
11214 {
11215 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11216 tree type = DECL_BIT_FIELD_TYPE (decl);
11217 HOST_WIDE_INT bitpos_int;
11218 HOST_WIDE_INT highest_order_object_bit_offset;
11219 HOST_WIDE_INT highest_order_field_bit_offset;
11220 HOST_WIDE_INT unsigned bit_offset;
11221
11222 /* Must be a field and a bit field. */
11223 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11224
11225 /* We can't yet handle bit-fields whose offsets are variable, so if we
11226 encounter such things, just return without generating any attribute
11227 whatsoever. Likewise for variable or too large size. */
11228 if (! host_integerp (bit_position (decl), 0)
11229 || ! host_integerp (DECL_SIZE (decl), 1))
11230 return;
11231
11232 bitpos_int = int_bit_position (decl);
11233
11234 /* Note that the bit offset is always the distance (in bits) from the
11235 highest-order bit of the "containing object" to the highest-order bit of
11236 the bit-field itself. Since the "high-order end" of any object or field
11237 is different on big-endian and little-endian machines, the computation
11238 below must take account of these differences. */
11239 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11240 highest_order_field_bit_offset = bitpos_int;
11241
11242 if (! BYTES_BIG_ENDIAN)
11243 {
11244 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11245 highest_order_object_bit_offset += simple_type_size_in_bits (type);
11246 }
11247
11248 bit_offset
11249 = (! BYTES_BIG_ENDIAN
11250 ? highest_order_object_bit_offset - highest_order_field_bit_offset
11251 : highest_order_field_bit_offset - highest_order_object_bit_offset);
11252
11253 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11254 }
11255
11256 /* For a FIELD_DECL node which represents a bit field, output an attribute
11257 which specifies the length in bits of the given field. */
11258
11259 static inline void
11260 add_bit_size_attribute (dw_die_ref die, tree decl)
11261 {
11262 /* Must be a field and a bit field. */
11263 gcc_assert (TREE_CODE (decl) == FIELD_DECL
11264 && DECL_BIT_FIELD_TYPE (decl));
11265
11266 if (host_integerp (DECL_SIZE (decl), 1))
11267 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11268 }
11269
11270 /* If the compiled language is ANSI C, then add a 'prototyped'
11271 attribute, if arg types are given for the parameters of a function. */
11272
11273 static inline void
11274 add_prototyped_attribute (dw_die_ref die, tree func_type)
11275 {
11276 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11277 && TYPE_ARG_TYPES (func_type) != NULL)
11278 add_AT_flag (die, DW_AT_prototyped, 1);
11279 }
11280
11281 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
11282 by looking in either the type declaration or object declaration
11283 equate table. */
11284
11285 static inline void
11286 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11287 {
11288 dw_die_ref origin_die = NULL;
11289
11290 if (TREE_CODE (origin) != FUNCTION_DECL)
11291 {
11292 /* We may have gotten separated from the block for the inlined
11293 function, if we're in an exception handler or some such; make
11294 sure that the abstract function has been written out.
11295
11296 Doing this for nested functions is wrong, however; functions are
11297 distinct units, and our context might not even be inline. */
11298 tree fn = origin;
11299
11300 if (TYPE_P (fn))
11301 fn = TYPE_STUB_DECL (fn);
11302
11303 fn = decl_function_context (fn);
11304 if (fn)
11305 dwarf2out_abstract_function (fn);
11306 }
11307
11308 if (DECL_P (origin))
11309 origin_die = lookup_decl_die (origin);
11310 else if (TYPE_P (origin))
11311 origin_die = lookup_type_die (origin);
11312
11313 /* XXX: Functions that are never lowered don't always have correct block
11314 trees (in the case of java, they simply have no block tree, in some other
11315 languages). For these functions, there is nothing we can really do to
11316 output correct debug info for inlined functions in all cases. Rather
11317 than die, we'll just produce deficient debug info now, in that we will
11318 have variables without a proper abstract origin. In the future, when all
11319 functions are lowered, we should re-add a gcc_assert (origin_die)
11320 here. */
11321
11322 if (origin_die)
11323 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11324 }
11325
11326 /* We do not currently support the pure_virtual attribute. */
11327
11328 static inline void
11329 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11330 {
11331 if (DECL_VINDEX (func_decl))
11332 {
11333 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11334
11335 if (host_integerp (DECL_VINDEX (func_decl), 0))
11336 add_AT_loc (die, DW_AT_vtable_elem_location,
11337 new_loc_descr (DW_OP_constu,
11338 tree_low_cst (DECL_VINDEX (func_decl), 0),
11339 0));
11340
11341 /* GNU extension: Record what type this method came from originally. */
11342 if (debug_info_level > DINFO_LEVEL_TERSE)
11343 add_AT_die_ref (die, DW_AT_containing_type,
11344 lookup_type_die (DECL_CONTEXT (func_decl)));
11345 }
11346 }
11347 \f
11348 /* Add source coordinate attributes for the given decl. */
11349
11350 static void
11351 add_src_coords_attributes (dw_die_ref die, tree decl)
11352 {
11353 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11354
11355 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11356 add_AT_unsigned (die, DW_AT_decl_line, s.line);
11357 }
11358
11359 /* Add a DW_AT_name attribute and source coordinate attribute for the
11360 given decl, but only if it actually has a name. */
11361
11362 static void
11363 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11364 {
11365 tree decl_name;
11366
11367 decl_name = DECL_NAME (decl);
11368 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11369 {
11370 add_name_attribute (die, dwarf2_name (decl, 0));
11371 if (! DECL_ARTIFICIAL (decl))
11372 add_src_coords_attributes (die, decl);
11373
11374 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11375 && TREE_PUBLIC (decl)
11376 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11377 && !DECL_ABSTRACT (decl)
11378 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11379 && !is_fortran ())
11380 add_AT_string (die, DW_AT_MIPS_linkage_name,
11381 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11382 }
11383
11384 #ifdef VMS_DEBUGGING_INFO
11385 /* Get the function's name, as described by its RTL. This may be different
11386 from the DECL_NAME name used in the source file. */
11387 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11388 {
11389 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11390 XEXP (DECL_RTL (decl), 0));
11391 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11392 }
11393 #endif
11394 }
11395
11396 /* Push a new declaration scope. */
11397
11398 static void
11399 push_decl_scope (tree scope)
11400 {
11401 VEC_safe_push (tree, gc, decl_scope_table, scope);
11402 }
11403
11404 /* Pop a declaration scope. */
11405
11406 static inline void
11407 pop_decl_scope (void)
11408 {
11409 VEC_pop (tree, decl_scope_table);
11410 }
11411
11412 /* Return the DIE for the scope that immediately contains this type.
11413 Non-named types get global scope. Named types nested in other
11414 types get their containing scope if it's open, or global scope
11415 otherwise. All other types (i.e. function-local named types) get
11416 the current active scope. */
11417
11418 static dw_die_ref
11419 scope_die_for (tree t, dw_die_ref context_die)
11420 {
11421 dw_die_ref scope_die = NULL;
11422 tree containing_scope;
11423 int i;
11424
11425 /* Non-types always go in the current scope. */
11426 gcc_assert (TYPE_P (t));
11427
11428 containing_scope = TYPE_CONTEXT (t);
11429
11430 /* Use the containing namespace if it was passed in (for a declaration). */
11431 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11432 {
11433 if (context_die == lookup_decl_die (containing_scope))
11434 /* OK */;
11435 else
11436 containing_scope = NULL_TREE;
11437 }
11438
11439 /* Ignore function type "scopes" from the C frontend. They mean that
11440 a tagged type is local to a parmlist of a function declarator, but
11441 that isn't useful to DWARF. */
11442 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11443 containing_scope = NULL_TREE;
11444
11445 if (containing_scope == NULL_TREE)
11446 scope_die = comp_unit_die;
11447 else if (TYPE_P (containing_scope))
11448 {
11449 /* For types, we can just look up the appropriate DIE. But
11450 first we check to see if we're in the middle of emitting it
11451 so we know where the new DIE should go. */
11452 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11453 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11454 break;
11455
11456 if (i < 0)
11457 {
11458 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11459 || TREE_ASM_WRITTEN (containing_scope));
11460
11461 /* If none of the current dies are suitable, we get file scope. */
11462 scope_die = comp_unit_die;
11463 }
11464 else
11465 scope_die = lookup_type_die (containing_scope);
11466 }
11467 else
11468 scope_die = context_die;
11469
11470 return scope_die;
11471 }
11472
11473 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
11474
11475 static inline int
11476 local_scope_p (dw_die_ref context_die)
11477 {
11478 for (; context_die; context_die = context_die->die_parent)
11479 if (context_die->die_tag == DW_TAG_inlined_subroutine
11480 || context_die->die_tag == DW_TAG_subprogram)
11481 return 1;
11482
11483 return 0;
11484 }
11485
11486 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11487 whether or not to treat a DIE in this context as a declaration. */
11488
11489 static inline int
11490 class_or_namespace_scope_p (dw_die_ref context_die)
11491 {
11492 return (context_die
11493 && (context_die->die_tag == DW_TAG_structure_type
11494 || context_die->die_tag == DW_TAG_class_type
11495 || context_die->die_tag == DW_TAG_interface_type
11496 || context_die->die_tag == DW_TAG_union_type
11497 || context_die->die_tag == DW_TAG_namespace));
11498 }
11499
11500 /* Many forms of DIEs require a "type description" attribute. This
11501 routine locates the proper "type descriptor" die for the type given
11502 by 'type', and adds a DW_AT_type attribute below the given die. */
11503
11504 static void
11505 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11506 int decl_volatile, dw_die_ref context_die)
11507 {
11508 enum tree_code code = TREE_CODE (type);
11509 dw_die_ref type_die = NULL;
11510
11511 /* ??? If this type is an unnamed subrange type of an integral, floating-point
11512 or fixed-point type, use the inner type. This is because we have no
11513 support for unnamed types in base_type_die. This can happen if this is
11514 an Ada subrange type. Correct solution is emit a subrange type die. */
11515 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11516 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11517 type = TREE_TYPE (type), code = TREE_CODE (type);
11518
11519 if (code == ERROR_MARK
11520 /* Handle a special case. For functions whose return type is void, we
11521 generate *no* type attribute. (Note that no object may have type
11522 `void', so this only applies to function return types). */
11523 || code == VOID_TYPE)
11524 return;
11525
11526 type_die = modified_type_die (type,
11527 decl_const || TYPE_READONLY (type),
11528 decl_volatile || TYPE_VOLATILE (type),
11529 context_die);
11530
11531 if (type_die != NULL)
11532 add_AT_die_ref (object_die, DW_AT_type, type_die);
11533 }
11534
11535 /* Given an object die, add the calling convention attribute for the
11536 function call type. */
11537 static void
11538 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11539 {
11540 enum dwarf_calling_convention value = DW_CC_normal;
11541
11542 value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11543
11544 /* DWARF doesn't provide a way to identify a program's source-level
11545 entry point. DW_AT_calling_convention attributes are only meant
11546 to describe functions' calling conventions. However, lacking a
11547 better way to signal the Fortran main program, we use this for the
11548 time being, following existing custom. */
11549 if (is_fortran ()
11550 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11551 value = DW_CC_program;
11552
11553 /* Only add the attribute if the backend requests it, and
11554 is not DW_CC_normal. */
11555 if (value && (value != DW_CC_normal))
11556 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11557 }
11558
11559 /* Given a tree pointer to a struct, class, union, or enum type node, return
11560 a pointer to the (string) tag name for the given type, or zero if the type
11561 was declared without a tag. */
11562
11563 static const char *
11564 type_tag (const_tree type)
11565 {
11566 const char *name = 0;
11567
11568 if (TYPE_NAME (type) != 0)
11569 {
11570 tree t = 0;
11571
11572 /* Find the IDENTIFIER_NODE for the type name. */
11573 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11574 t = TYPE_NAME (type);
11575
11576 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11577 a TYPE_DECL node, regardless of whether or not a `typedef' was
11578 involved. */
11579 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11580 && ! DECL_IGNORED_P (TYPE_NAME (type)))
11581 {
11582 /* We want to be extra verbose. Don't call dwarf_name if
11583 DECL_NAME isn't set. The default hook for decl_printable_name
11584 doesn't like that, and in this context it's correct to return
11585 0, instead of "<anonymous>" or the like. */
11586 if (DECL_NAME (TYPE_NAME (type)))
11587 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11588 }
11589
11590 /* Now get the name as a string, or invent one. */
11591 if (!name && t != 0)
11592 name = IDENTIFIER_POINTER (t);
11593 }
11594
11595 return (name == 0 || *name == '\0') ? 0 : name;
11596 }
11597
11598 /* Return the type associated with a data member, make a special check
11599 for bit field types. */
11600
11601 static inline tree
11602 member_declared_type (const_tree member)
11603 {
11604 return (DECL_BIT_FIELD_TYPE (member)
11605 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11606 }
11607
11608 /* Get the decl's label, as described by its RTL. This may be different
11609 from the DECL_NAME name used in the source file. */
11610
11611 #if 0
11612 static const char *
11613 decl_start_label (tree decl)
11614 {
11615 rtx x;
11616 const char *fnname;
11617
11618 x = DECL_RTL (decl);
11619 gcc_assert (MEM_P (x));
11620
11621 x = XEXP (x, 0);
11622 gcc_assert (GET_CODE (x) == SYMBOL_REF);
11623
11624 fnname = XSTR (x, 0);
11625 return fnname;
11626 }
11627 #endif
11628 \f
11629 /* These routines generate the internal representation of the DIE's for
11630 the compilation unit. Debugging information is collected by walking
11631 the declaration trees passed in from dwarf2out_decl(). */
11632
11633 static void
11634 gen_array_type_die (tree type, dw_die_ref context_die)
11635 {
11636 dw_die_ref scope_die = scope_die_for (type, context_die);
11637 dw_die_ref array_die;
11638 tree element_type;
11639
11640 /* ??? The SGI dwarf reader fails for array of array of enum types unless
11641 the inner array type comes before the outer array type. Thus we must
11642 call gen_type_die before we call new_die. See below also. */
11643 #ifdef MIPS_DEBUGGING_INFO
11644 gen_type_die (TREE_TYPE (type), context_die);
11645 #endif
11646
11647 array_die = new_die (DW_TAG_array_type, scope_die, type);
11648 add_name_attribute (array_die, type_tag (type));
11649 equate_type_number_to_die (type, array_die);
11650
11651 if (TREE_CODE (type) == VECTOR_TYPE)
11652 {
11653 /* The frontend feeds us a representation for the vector as a struct
11654 containing an array. Pull out the array type. */
11655 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11656 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11657 }
11658
11659 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
11660 if (is_fortran ()
11661 && TREE_CODE (type) == ARRAY_TYPE
11662 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11663 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11664
11665 #if 0
11666 /* We default the array ordering. SDB will probably do
11667 the right things even if DW_AT_ordering is not present. It's not even
11668 an issue until we start to get into multidimensional arrays anyway. If
11669 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11670 then we'll have to put the DW_AT_ordering attribute back in. (But if
11671 and when we find out that we need to put these in, we will only do so
11672 for multidimensional arrays. */
11673 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11674 #endif
11675
11676 #ifdef MIPS_DEBUGGING_INFO
11677 /* The SGI compilers handle arrays of unknown bound by setting
11678 AT_declaration and not emitting any subrange DIEs. */
11679 if (! TYPE_DOMAIN (type))
11680 add_AT_flag (array_die, DW_AT_declaration, 1);
11681 else
11682 #endif
11683 add_subscript_info (array_die, type);
11684
11685 /* Add representation of the type of the elements of this array type. */
11686 element_type = TREE_TYPE (type);
11687
11688 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11689 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
11690 We work around this by disabling this feature. See also
11691 add_subscript_info. */
11692 #ifndef MIPS_DEBUGGING_INFO
11693 while (TREE_CODE (element_type) == ARRAY_TYPE)
11694 element_type = TREE_TYPE (element_type);
11695
11696 gen_type_die (element_type, context_die);
11697 #endif
11698
11699 add_type_attribute (array_die, element_type, 0, 0, context_die);
11700
11701 if (get_AT (array_die, DW_AT_name))
11702 add_pubtype (type, array_die);
11703 }
11704
11705 static dw_loc_descr_ref
11706 descr_info_loc (tree val, tree base_decl)
11707 {
11708 HOST_WIDE_INT size;
11709 dw_loc_descr_ref loc, loc2;
11710 enum dwarf_location_atom op;
11711
11712 if (val == base_decl)
11713 return new_loc_descr (DW_OP_push_object_address, 0, 0);
11714
11715 switch (TREE_CODE (val))
11716 {
11717 case NOP_EXPR:
11718 case CONVERT_EXPR:
11719 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11720 case INTEGER_CST:
11721 if (host_integerp (val, 0))
11722 return int_loc_descriptor (tree_low_cst (val, 0));
11723 break;
11724 case INDIRECT_REF:
11725 size = int_size_in_bytes (TREE_TYPE (val));
11726 if (size < 0)
11727 break;
11728 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11729 if (!loc)
11730 break;
11731 if (size == DWARF2_ADDR_SIZE)
11732 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11733 else
11734 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11735 return loc;
11736 case POINTER_PLUS_EXPR:
11737 case PLUS_EXPR:
11738 if (host_integerp (TREE_OPERAND (val, 1), 1)
11739 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11740 < 16384)
11741 {
11742 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11743 if (!loc)
11744 break;
11745 add_loc_descr (&loc,
11746 new_loc_descr (DW_OP_plus_uconst,
11747 tree_low_cst (TREE_OPERAND (val, 1),
11748 1), 0));
11749 }
11750 else
11751 {
11752 op = DW_OP_plus;
11753 do_binop:
11754 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11755 if (!loc)
11756 break;
11757 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11758 if (!loc2)
11759 break;
11760 add_loc_descr (&loc, loc2);
11761 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11762 }
11763 return loc;
11764 case MINUS_EXPR:
11765 op = DW_OP_minus;
11766 goto do_binop;
11767 case MULT_EXPR:
11768 op = DW_OP_mul;
11769 goto do_binop;
11770 case EQ_EXPR:
11771 op = DW_OP_eq;
11772 goto do_binop;
11773 case NE_EXPR:
11774 op = DW_OP_ne;
11775 goto do_binop;
11776 default:
11777 break;
11778 }
11779 return NULL;
11780 }
11781
11782 static void
11783 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11784 tree val, tree base_decl)
11785 {
11786 dw_loc_descr_ref loc;
11787
11788 if (host_integerp (val, 0))
11789 {
11790 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11791 return;
11792 }
11793
11794 loc = descr_info_loc (val, base_decl);
11795 if (!loc)
11796 return;
11797
11798 add_AT_loc (die, attr, loc);
11799 }
11800
11801 /* This routine generates DIE for array with hidden descriptor, details
11802 are filled into *info by a langhook. */
11803
11804 static void
11805 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11806 dw_die_ref context_die)
11807 {
11808 dw_die_ref scope_die = scope_die_for (type, context_die);
11809 dw_die_ref array_die;
11810 int dim;
11811
11812 array_die = new_die (DW_TAG_array_type, scope_die, type);
11813 add_name_attribute (array_die, type_tag (type));
11814 equate_type_number_to_die (type, array_die);
11815
11816 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
11817 if (is_fortran ()
11818 && info->ndimensions >= 2)
11819 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11820
11821 if (info->data_location)
11822 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11823 info->base_decl);
11824 if (info->associated)
11825 add_descr_info_field (array_die, DW_AT_associated, info->associated,
11826 info->base_decl);
11827 if (info->allocated)
11828 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11829 info->base_decl);
11830
11831 for (dim = 0; dim < info->ndimensions; dim++)
11832 {
11833 dw_die_ref subrange_die
11834 = new_die (DW_TAG_subrange_type, array_die, NULL);
11835
11836 if (info->dimen[dim].lower_bound)
11837 {
11838 /* If it is the default value, omit it. */
11839 if ((is_c_family () || is_java ())
11840 && integer_zerop (info->dimen[dim].lower_bound))
11841 ;
11842 else if (is_fortran ()
11843 && integer_onep (info->dimen[dim].lower_bound))
11844 ;
11845 else
11846 add_descr_info_field (subrange_die, DW_AT_lower_bound,
11847 info->dimen[dim].lower_bound,
11848 info->base_decl);
11849 }
11850 if (info->dimen[dim].upper_bound)
11851 add_descr_info_field (subrange_die, DW_AT_upper_bound,
11852 info->dimen[dim].upper_bound,
11853 info->base_decl);
11854 if (info->dimen[dim].stride)
11855 add_descr_info_field (subrange_die, DW_AT_byte_stride,
11856 info->dimen[dim].stride,
11857 info->base_decl);
11858 }
11859
11860 gen_type_die (info->element_type, context_die);
11861 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11862
11863 if (get_AT (array_die, DW_AT_name))
11864 add_pubtype (type, array_die);
11865 }
11866
11867 #if 0
11868 static void
11869 gen_entry_point_die (tree decl, dw_die_ref context_die)
11870 {
11871 tree origin = decl_ultimate_origin (decl);
11872 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11873
11874 if (origin != NULL)
11875 add_abstract_origin_attribute (decl_die, origin);
11876 else
11877 {
11878 add_name_and_src_coords_attributes (decl_die, decl);
11879 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11880 0, 0, context_die);
11881 }
11882
11883 if (DECL_ABSTRACT (decl))
11884 equate_decl_number_to_die (decl, decl_die);
11885 else
11886 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11887 }
11888 #endif
11889
11890 /* Walk through the list of incomplete types again, trying once more to
11891 emit full debugging info for them. */
11892
11893 static void
11894 retry_incomplete_types (void)
11895 {
11896 int i;
11897
11898 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11899 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11900 }
11901
11902 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11903
11904 static void
11905 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11906 {
11907 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11908
11909 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11910 be incomplete and such types are not marked. */
11911 add_abstract_origin_attribute (type_die, type);
11912 }
11913
11914 /* Determine what tag to use for a record type. */
11915
11916 static enum dwarf_tag
11917 record_type_tag (tree type)
11918 {
11919 if (! lang_hooks.types.classify_record)
11920 return DW_TAG_structure_type;
11921
11922 switch (lang_hooks.types.classify_record (type))
11923 {
11924 case RECORD_IS_STRUCT:
11925 return DW_TAG_structure_type;
11926
11927 case RECORD_IS_CLASS:
11928 return DW_TAG_class_type;
11929
11930 case RECORD_IS_INTERFACE:
11931 return DW_TAG_interface_type;
11932
11933 default:
11934 gcc_unreachable ();
11935 }
11936 }
11937
11938 /* Generate a DIE to represent an inlined instance of a structure type. */
11939
11940 static void
11941 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11942 {
11943 dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
11944
11945 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11946 be incomplete and such types are not marked. */
11947 add_abstract_origin_attribute (type_die, type);
11948 }
11949
11950 /* Generate a DIE to represent an inlined instance of a union type. */
11951
11952 static void
11953 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11954 {
11955 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11956
11957 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11958 be incomplete and such types are not marked. */
11959 add_abstract_origin_attribute (type_die, type);
11960 }
11961
11962 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11963 include all of the information about the enumeration values also. Each
11964 enumerated type name/value is listed as a child of the enumerated type
11965 DIE. */
11966
11967 static dw_die_ref
11968 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11969 {
11970 dw_die_ref type_die = lookup_type_die (type);
11971
11972 if (type_die == NULL)
11973 {
11974 type_die = new_die (DW_TAG_enumeration_type,
11975 scope_die_for (type, context_die), type);
11976 equate_type_number_to_die (type, type_die);
11977 add_name_attribute (type_die, type_tag (type));
11978 }
11979 else if (! TYPE_SIZE (type))
11980 return type_die;
11981 else
11982 remove_AT (type_die, DW_AT_declaration);
11983
11984 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11985 given enum type is incomplete, do not generate the DW_AT_byte_size
11986 attribute or the DW_AT_element_list attribute. */
11987 if (TYPE_SIZE (type))
11988 {
11989 tree link;
11990
11991 TREE_ASM_WRITTEN (type) = 1;
11992 add_byte_size_attribute (type_die, type);
11993 if (TYPE_STUB_DECL (type) != NULL_TREE)
11994 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11995
11996 /* If the first reference to this type was as the return type of an
11997 inline function, then it may not have a parent. Fix this now. */
11998 if (type_die->die_parent == NULL)
11999 add_child_die (scope_die_for (type, context_die), type_die);
12000
12001 for (link = TYPE_VALUES (type);
12002 link != NULL; link = TREE_CHAIN (link))
12003 {
12004 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12005 tree value = TREE_VALUE (link);
12006
12007 add_name_attribute (enum_die,
12008 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12009
12010 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12011 /* DWARF2 does not provide a way of indicating whether or
12012 not enumeration constants are signed or unsigned. GDB
12013 always assumes the values are signed, so we output all
12014 values as if they were signed. That means that
12015 enumeration constants with very large unsigned values
12016 will appear to have negative values in the debugger. */
12017 add_AT_int (enum_die, DW_AT_const_value,
12018 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12019 }
12020 }
12021 else
12022 add_AT_flag (type_die, DW_AT_declaration, 1);
12023
12024 if (get_AT (type_die, DW_AT_name))
12025 add_pubtype (type, type_die);
12026
12027 return type_die;
12028 }
12029
12030 /* Generate a DIE to represent either a real live formal parameter decl or to
12031 represent just the type of some formal parameter position in some function
12032 type.
12033
12034 Note that this routine is a bit unusual because its argument may be a
12035 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12036 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12037 node. If it's the former then this function is being called to output a
12038 DIE to represent a formal parameter object (or some inlining thereof). If
12039 it's the latter, then this function is only being called to output a
12040 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12041 argument type of some subprogram type. */
12042
12043 static dw_die_ref
12044 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12045 {
12046 dw_die_ref parm_die
12047 = new_die (DW_TAG_formal_parameter, context_die, node);
12048 tree origin;
12049
12050 switch (TREE_CODE_CLASS (TREE_CODE (node)))
12051 {
12052 case tcc_declaration:
12053 origin = decl_ultimate_origin (node);
12054 if (origin != NULL)
12055 add_abstract_origin_attribute (parm_die, origin);
12056 else
12057 {
12058 tree type = TREE_TYPE (node);
12059 add_name_and_src_coords_attributes (parm_die, node);
12060 if (DECL_BY_REFERENCE (node))
12061 type = TREE_TYPE (type);
12062 add_type_attribute (parm_die, type,
12063 TREE_READONLY (node),
12064 TREE_THIS_VOLATILE (node),
12065 context_die);
12066 if (DECL_ARTIFICIAL (node))
12067 add_AT_flag (parm_die, DW_AT_artificial, 1);
12068 }
12069
12070 equate_decl_number_to_die (node, parm_die);
12071 if (! DECL_ABSTRACT (node))
12072 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12073
12074 break;
12075
12076 case tcc_type:
12077 /* We were called with some kind of a ..._TYPE node. */
12078 add_type_attribute (parm_die, node, 0, 0, context_die);
12079 break;
12080
12081 default:
12082 gcc_unreachable ();
12083 }
12084
12085 return parm_die;
12086 }
12087
12088 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12089 at the end of an (ANSI prototyped) formal parameters list. */
12090
12091 static void
12092 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12093 {
12094 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12095 }
12096
12097 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12098 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12099 parameters as specified in some function type specification (except for
12100 those which appear as part of a function *definition*). */
12101
12102 static void
12103 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12104 {
12105 tree link;
12106 tree formal_type = NULL;
12107 tree first_parm_type;
12108 tree arg;
12109
12110 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12111 {
12112 arg = DECL_ARGUMENTS (function_or_method_type);
12113 function_or_method_type = TREE_TYPE (function_or_method_type);
12114 }
12115 else
12116 arg = NULL_TREE;
12117
12118 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12119
12120 /* Make our first pass over the list of formal parameter types and output a
12121 DW_TAG_formal_parameter DIE for each one. */
12122 for (link = first_parm_type; link; )
12123 {
12124 dw_die_ref parm_die;
12125
12126 formal_type = TREE_VALUE (link);
12127 if (formal_type == void_type_node)
12128 break;
12129
12130 /* Output a (nameless) DIE to represent the formal parameter itself. */
12131 parm_die = gen_formal_parameter_die (formal_type, context_die);
12132 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12133 && link == first_parm_type)
12134 || (arg && DECL_ARTIFICIAL (arg)))
12135 add_AT_flag (parm_die, DW_AT_artificial, 1);
12136
12137 link = TREE_CHAIN (link);
12138 if (arg)
12139 arg = TREE_CHAIN (arg);
12140 }
12141
12142 /* If this function type has an ellipsis, add a
12143 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
12144 if (formal_type != void_type_node)
12145 gen_unspecified_parameters_die (function_or_method_type, context_die);
12146
12147 /* Make our second (and final) pass over the list of formal parameter types
12148 and output DIEs to represent those types (as necessary). */
12149 for (link = TYPE_ARG_TYPES (function_or_method_type);
12150 link && TREE_VALUE (link);
12151 link = TREE_CHAIN (link))
12152 gen_type_die (TREE_VALUE (link), context_die);
12153 }
12154
12155 /* We want to generate the DIE for TYPE so that we can generate the
12156 die for MEMBER, which has been defined; we will need to refer back
12157 to the member declaration nested within TYPE. If we're trying to
12158 generate minimal debug info for TYPE, processing TYPE won't do the
12159 trick; we need to attach the member declaration by hand. */
12160
12161 static void
12162 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12163 {
12164 gen_type_die (type, context_die);
12165
12166 /* If we're trying to avoid duplicate debug info, we may not have
12167 emitted the member decl for this function. Emit it now. */
12168 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12169 && ! lookup_decl_die (member))
12170 {
12171 dw_die_ref type_die;
12172 gcc_assert (!decl_ultimate_origin (member));
12173
12174 push_decl_scope (type);
12175 type_die = lookup_type_die (type);
12176 if (TREE_CODE (member) == FUNCTION_DECL)
12177 gen_subprogram_die (member, type_die);
12178 else if (TREE_CODE (member) == FIELD_DECL)
12179 {
12180 /* Ignore the nameless fields that are used to skip bits but handle
12181 C++ anonymous unions and structs. */
12182 if (DECL_NAME (member) != NULL_TREE
12183 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12184 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12185 {
12186 gen_type_die (member_declared_type (member), type_die);
12187 gen_field_die (member, type_die);
12188 }
12189 }
12190 else
12191 gen_variable_die (member, type_die);
12192
12193 pop_decl_scope ();
12194 }
12195 }
12196
12197 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12198 may later generate inlined and/or out-of-line instances of. */
12199
12200 static void
12201 dwarf2out_abstract_function (tree decl)
12202 {
12203 dw_die_ref old_die;
12204 tree save_fn;
12205 tree context;
12206 int was_abstract = DECL_ABSTRACT (decl);
12207
12208 /* Make sure we have the actual abstract inline, not a clone. */
12209 decl = DECL_ORIGIN (decl);
12210
12211 old_die = lookup_decl_die (decl);
12212 if (old_die && get_AT (old_die, DW_AT_inline))
12213 /* We've already generated the abstract instance. */
12214 return;
12215
12216 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12217 we don't get confused by DECL_ABSTRACT. */
12218 if (debug_info_level > DINFO_LEVEL_TERSE)
12219 {
12220 context = decl_class_context (decl);
12221 if (context)
12222 gen_type_die_for_member
12223 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12224 }
12225
12226 /* Pretend we've just finished compiling this function. */
12227 save_fn = current_function_decl;
12228 current_function_decl = decl;
12229 push_cfun (DECL_STRUCT_FUNCTION (decl));
12230
12231 set_decl_abstract_flags (decl, 1);
12232 dwarf2out_decl (decl);
12233 if (! was_abstract)
12234 set_decl_abstract_flags (decl, 0);
12235
12236 current_function_decl = save_fn;
12237 pop_cfun ();
12238 }
12239
12240 /* Helper function of premark_used_types() which gets called through
12241 htab_traverse_resize().
12242
12243 Marks the DIE of a given type in *SLOT as perennial, so it never gets
12244 marked as unused by prune_unused_types. */
12245 static int
12246 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12247 {
12248 tree type;
12249 dw_die_ref die;
12250
12251 type = *slot;
12252 die = lookup_type_die (type);
12253 if (die != NULL)
12254 die->die_perennial_p = 1;
12255 return 1;
12256 }
12257
12258 /* Mark all members of used_types_hash as perennial. */
12259 static void
12260 premark_used_types (void)
12261 {
12262 if (cfun && cfun->used_types_hash)
12263 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12264 }
12265
12266 /* Generate a DIE to represent a declared function (either file-scope or
12267 block-local). */
12268
12269 static void
12270 gen_subprogram_die (tree decl, dw_die_ref context_die)
12271 {
12272 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12273 tree origin = decl_ultimate_origin (decl);
12274 dw_die_ref subr_die;
12275 tree fn_arg_types;
12276 tree outer_scope;
12277 dw_die_ref old_die = lookup_decl_die (decl);
12278 int declaration = (current_function_decl != decl
12279 || class_or_namespace_scope_p (context_die));
12280
12281 premark_used_types ();
12282
12283 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12284 started to generate the abstract instance of an inline, decided to output
12285 its containing class, and proceeded to emit the declaration of the inline
12286 from the member list for the class. If so, DECLARATION takes priority;
12287 we'll get back to the abstract instance when done with the class. */
12288
12289 /* The class-scope declaration DIE must be the primary DIE. */
12290 if (origin && declaration && class_or_namespace_scope_p (context_die))
12291 {
12292 origin = NULL;
12293 gcc_assert (!old_die);
12294 }
12295
12296 /* Now that the C++ front end lazily declares artificial member fns, we
12297 might need to retrofit the declaration into its class. */
12298 if (!declaration && !origin && !old_die
12299 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12300 && !class_or_namespace_scope_p (context_die)
12301 && debug_info_level > DINFO_LEVEL_TERSE)
12302 old_die = force_decl_die (decl);
12303
12304 if (origin != NULL)
12305 {
12306 gcc_assert (!declaration || local_scope_p (context_die));
12307
12308 /* Fixup die_parent for the abstract instance of a nested
12309 inline function. */
12310 if (old_die && old_die->die_parent == NULL)
12311 add_child_die (context_die, old_die);
12312
12313 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12314 add_abstract_origin_attribute (subr_die, origin);
12315 }
12316 else if (old_die)
12317 {
12318 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12319 struct dwarf_file_data * file_index = lookup_filename (s.file);
12320
12321 if (!get_AT_flag (old_die, DW_AT_declaration)
12322 /* We can have a normal definition following an inline one in the
12323 case of redefinition of GNU C extern inlines.
12324 It seems reasonable to use AT_specification in this case. */
12325 && !get_AT (old_die, DW_AT_inline))
12326 {
12327 /* Detect and ignore this case, where we are trying to output
12328 something we have already output. */
12329 return;
12330 }
12331
12332 /* If the definition comes from the same place as the declaration,
12333 maybe use the old DIE. We always want the DIE for this function
12334 that has the *_pc attributes to be under comp_unit_die so the
12335 debugger can find it. We also need to do this for abstract
12336 instances of inlines, since the spec requires the out-of-line copy
12337 to have the same parent. For local class methods, this doesn't
12338 apply; we just use the old DIE. */
12339 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12340 && (DECL_ARTIFICIAL (decl)
12341 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12342 && (get_AT_unsigned (old_die, DW_AT_decl_line)
12343 == (unsigned) s.line))))
12344 {
12345 subr_die = old_die;
12346
12347 /* Clear out the declaration attribute and the formal parameters.
12348 Do not remove all children, because it is possible that this
12349 declaration die was forced using force_decl_die(). In such
12350 cases die that forced declaration die (e.g. TAG_imported_module)
12351 is one of the children that we do not want to remove. */
12352 remove_AT (subr_die, DW_AT_declaration);
12353 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12354 }
12355 else
12356 {
12357 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12358 add_AT_specification (subr_die, old_die);
12359 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12360 add_AT_file (subr_die, DW_AT_decl_file, file_index);
12361 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12362 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12363 }
12364 }
12365 else
12366 {
12367 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12368
12369 if (TREE_PUBLIC (decl))
12370 add_AT_flag (subr_die, DW_AT_external, 1);
12371
12372 add_name_and_src_coords_attributes (subr_die, decl);
12373 if (debug_info_level > DINFO_LEVEL_TERSE)
12374 {
12375 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12376 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12377 0, 0, context_die);
12378 }
12379
12380 add_pure_or_virtual_attribute (subr_die, decl);
12381 if (DECL_ARTIFICIAL (decl))
12382 add_AT_flag (subr_die, DW_AT_artificial, 1);
12383
12384 if (TREE_PROTECTED (decl))
12385 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12386 else if (TREE_PRIVATE (decl))
12387 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12388 }
12389
12390 if (declaration)
12391 {
12392 if (!old_die || !get_AT (old_die, DW_AT_inline))
12393 {
12394 add_AT_flag (subr_die, DW_AT_declaration, 1);
12395
12396 /* The first time we see a member function, it is in the context of
12397 the class to which it belongs. We make sure of this by emitting
12398 the class first. The next time is the definition, which is
12399 handled above. The two may come from the same source text.
12400
12401 Note that force_decl_die() forces function declaration die. It is
12402 later reused to represent definition. */
12403 equate_decl_number_to_die (decl, subr_die);
12404 }
12405 }
12406 else if (DECL_ABSTRACT (decl))
12407 {
12408 if (DECL_DECLARED_INLINE_P (decl))
12409 {
12410 if (cgraph_function_possibly_inlined_p (decl))
12411 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12412 else
12413 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12414 }
12415 else
12416 {
12417 if (cgraph_function_possibly_inlined_p (decl))
12418 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12419 else
12420 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12421 }
12422
12423 if (DECL_DECLARED_INLINE_P (decl)
12424 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12425 add_AT_flag (subr_die, DW_AT_artificial, 1);
12426
12427 equate_decl_number_to_die (decl, subr_die);
12428 }
12429 else if (!DECL_EXTERNAL (decl))
12430 {
12431 HOST_WIDE_INT cfa_fb_offset;
12432
12433 if (!old_die || !get_AT (old_die, DW_AT_inline))
12434 equate_decl_number_to_die (decl, subr_die);
12435
12436 if (!flag_reorder_blocks_and_partition)
12437 {
12438 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12439 current_function_funcdef_no);
12440 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12441 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12442 current_function_funcdef_no);
12443 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12444
12445 add_pubname (decl, subr_die);
12446 add_arange (decl, subr_die);
12447 }
12448 else
12449 { /* Do nothing for now; maybe need to duplicate die, one for
12450 hot section and ond for cold section, then use the hot/cold
12451 section begin/end labels to generate the aranges... */
12452 /*
12453 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12454 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12455 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12456 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12457
12458 add_pubname (decl, subr_die);
12459 add_arange (decl, subr_die);
12460 add_arange (decl, subr_die);
12461 */
12462 }
12463
12464 #ifdef MIPS_DEBUGGING_INFO
12465 /* Add a reference to the FDE for this routine. */
12466 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12467 #endif
12468
12469 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12470
12471 /* We define the "frame base" as the function's CFA. This is more
12472 convenient for several reasons: (1) It's stable across the prologue
12473 and epilogue, which makes it better than just a frame pointer,
12474 (2) With dwarf3, there exists a one-byte encoding that allows us
12475 to reference the .debug_frame data by proxy, but failing that,
12476 (3) We can at least reuse the code inspection and interpretation
12477 code that determines the CFA position at various points in the
12478 function. */
12479 /* ??? Use some command-line or configury switch to enable the use
12480 of dwarf3 DW_OP_call_frame_cfa. At present there are no dwarf
12481 consumers that understand it; fall back to "pure" dwarf2 and
12482 convert the CFA data into a location list. */
12483 {
12484 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12485 if (list->dw_loc_next)
12486 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12487 else
12488 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12489 }
12490
12491 /* Compute a displacement from the "steady-state frame pointer" to
12492 the CFA. The former is what all stack slots and argument slots
12493 will reference in the rtl; the later is what we've told the
12494 debugger about. We'll need to adjust all frame_base references
12495 by this displacement. */
12496 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12497
12498 if (cfun->static_chain_decl)
12499 add_AT_location_description (subr_die, DW_AT_static_link,
12500 loc_descriptor_from_tree (cfun->static_chain_decl));
12501 }
12502
12503 /* Now output descriptions of the arguments for this function. This gets
12504 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12505 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12506 `...' at the end of the formal parameter list. In order to find out if
12507 there was a trailing ellipsis or not, we must instead look at the type
12508 associated with the FUNCTION_DECL. This will be a node of type
12509 FUNCTION_TYPE. If the chain of type nodes hanging off of this
12510 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12511 an ellipsis at the end. */
12512
12513 /* In the case where we are describing a mere function declaration, all we
12514 need to do here (and all we *can* do here) is to describe the *types* of
12515 its formal parameters. */
12516 if (debug_info_level <= DINFO_LEVEL_TERSE)
12517 ;
12518 else if (declaration)
12519 gen_formal_types_die (decl, subr_die);
12520 else
12521 {
12522 /* Generate DIEs to represent all known formal parameters. */
12523 tree arg_decls = DECL_ARGUMENTS (decl);
12524 tree parm;
12525
12526 /* When generating DIEs, generate the unspecified_parameters DIE
12527 instead if we come across the arg "__builtin_va_alist" */
12528 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12529 if (TREE_CODE (parm) == PARM_DECL)
12530 {
12531 if (DECL_NAME (parm)
12532 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12533 "__builtin_va_alist"))
12534 gen_unspecified_parameters_die (parm, subr_die);
12535 else
12536 gen_decl_die (parm, subr_die);
12537 }
12538
12539 /* Decide whether we need an unspecified_parameters DIE at the end.
12540 There are 2 more cases to do this for: 1) the ansi ... declaration -
12541 this is detectable when the end of the arg list is not a
12542 void_type_node 2) an unprototyped function declaration (not a
12543 definition). This just means that we have no info about the
12544 parameters at all. */
12545 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12546 if (fn_arg_types != NULL)
12547 {
12548 /* This is the prototyped case, check for.... */
12549 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12550 gen_unspecified_parameters_die (decl, subr_die);
12551 }
12552 else if (DECL_INITIAL (decl) == NULL_TREE)
12553 gen_unspecified_parameters_die (decl, subr_die);
12554 }
12555
12556 /* Output Dwarf info for all of the stuff within the body of the function
12557 (if it has one - it may be just a declaration). */
12558 outer_scope = DECL_INITIAL (decl);
12559
12560 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12561 a function. This BLOCK actually represents the outermost binding contour
12562 for the function, i.e. the contour in which the function's formal
12563 parameters and labels get declared. Curiously, it appears that the front
12564 end doesn't actually put the PARM_DECL nodes for the current function onto
12565 the BLOCK_VARS list for this outer scope, but are strung off of the
12566 DECL_ARGUMENTS list for the function instead.
12567
12568 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12569 the LABEL_DECL nodes for the function however, and we output DWARF info
12570 for those in decls_for_scope. Just within the `outer_scope' there will be
12571 a BLOCK node representing the function's outermost pair of curly braces,
12572 and any blocks used for the base and member initializers of a C++
12573 constructor function. */
12574 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12575 {
12576 /* Emit a DW_TAG_variable DIE for a named return value. */
12577 if (DECL_NAME (DECL_RESULT (decl)))
12578 gen_decl_die (DECL_RESULT (decl), subr_die);
12579
12580 current_function_has_inlines = 0;
12581 decls_for_scope (outer_scope, subr_die, 0);
12582
12583 #if 0 && defined (MIPS_DEBUGGING_INFO)
12584 if (current_function_has_inlines)
12585 {
12586 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12587 if (! comp_unit_has_inlines)
12588 {
12589 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12590 comp_unit_has_inlines = 1;
12591 }
12592 }
12593 #endif
12594 }
12595 /* Add the calling convention attribute if requested. */
12596 add_calling_convention_attribute (subr_die, decl);
12597
12598 }
12599
12600 /* Generate a DIE to represent a declared data object. */
12601
12602 static void
12603 gen_variable_die (tree decl, dw_die_ref context_die)
12604 {
12605 tree origin = decl_ultimate_origin (decl);
12606 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12607
12608 dw_die_ref old_die = lookup_decl_die (decl);
12609 int declaration = (DECL_EXTERNAL (decl)
12610 /* If DECL is COMDAT and has not actually been
12611 emitted, we cannot take its address; there
12612 might end up being no definition anywhere in
12613 the program. For example, consider the C++
12614 test case:
12615
12616 template <class T>
12617 struct S { static const int i = 7; };
12618
12619 template <class T>
12620 const int S<T>::i;
12621
12622 int f() { return S<int>::i; }
12623
12624 Here, S<int>::i is not DECL_EXTERNAL, but no
12625 definition is required, so the compiler will
12626 not emit a definition. */
12627 || (TREE_CODE (decl) == VAR_DECL
12628 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12629 || class_or_namespace_scope_p (context_die));
12630
12631 if (origin != NULL)
12632 add_abstract_origin_attribute (var_die, origin);
12633
12634 /* Loop unrolling can create multiple blocks that refer to the same
12635 static variable, so we must test for the DW_AT_declaration flag.
12636
12637 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12638 copy decls and set the DECL_ABSTRACT flag on them instead of
12639 sharing them.
12640
12641 ??? Duplicated blocks have been rewritten to use .debug_ranges.
12642
12643 ??? The declare_in_namespace support causes us to get two DIEs for one
12644 variable, both of which are declarations. We want to avoid considering
12645 one to be a specification, so we must test that this DIE is not a
12646 declaration. */
12647 else if (old_die && TREE_STATIC (decl) && ! declaration
12648 && get_AT_flag (old_die, DW_AT_declaration) == 1)
12649 {
12650 /* This is a definition of a C++ class level static. */
12651 add_AT_specification (var_die, old_die);
12652 if (DECL_NAME (decl))
12653 {
12654 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12655 struct dwarf_file_data * file_index = lookup_filename (s.file);
12656
12657 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12658 add_AT_file (var_die, DW_AT_decl_file, file_index);
12659
12660 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12661 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12662 }
12663 }
12664 else
12665 {
12666 tree type = TREE_TYPE (decl);
12667 if ((TREE_CODE (decl) == PARM_DECL
12668 || TREE_CODE (decl) == RESULT_DECL)
12669 && DECL_BY_REFERENCE (decl))
12670 type = TREE_TYPE (type);
12671
12672 add_name_and_src_coords_attributes (var_die, decl);
12673 add_type_attribute (var_die, type, TREE_READONLY (decl),
12674 TREE_THIS_VOLATILE (decl), context_die);
12675
12676 if (TREE_PUBLIC (decl))
12677 add_AT_flag (var_die, DW_AT_external, 1);
12678
12679 if (DECL_ARTIFICIAL (decl))
12680 add_AT_flag (var_die, DW_AT_artificial, 1);
12681
12682 if (TREE_PROTECTED (decl))
12683 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12684 else if (TREE_PRIVATE (decl))
12685 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12686 }
12687
12688 if (declaration)
12689 add_AT_flag (var_die, DW_AT_declaration, 1);
12690
12691 if (DECL_ABSTRACT (decl) || declaration)
12692 equate_decl_number_to_die (decl, var_die);
12693
12694 if (! declaration && ! DECL_ABSTRACT (decl))
12695 {
12696 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12697 add_pubname (decl, var_die);
12698 }
12699 else
12700 tree_add_const_value_attribute (var_die, decl);
12701 }
12702
12703 /* Generate a DIE to represent a label identifier. */
12704
12705 static void
12706 gen_label_die (tree decl, dw_die_ref context_die)
12707 {
12708 tree origin = decl_ultimate_origin (decl);
12709 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12710 rtx insn;
12711 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12712
12713 if (origin != NULL)
12714 add_abstract_origin_attribute (lbl_die, origin);
12715 else
12716 add_name_and_src_coords_attributes (lbl_die, decl);
12717
12718 if (DECL_ABSTRACT (decl))
12719 equate_decl_number_to_die (decl, lbl_die);
12720 else
12721 {
12722 insn = DECL_RTL_IF_SET (decl);
12723
12724 /* Deleted labels are programmer specified labels which have been
12725 eliminated because of various optimizations. We still emit them
12726 here so that it is possible to put breakpoints on them. */
12727 if (insn
12728 && (LABEL_P (insn)
12729 || ((NOTE_P (insn)
12730 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12731 {
12732 /* When optimization is enabled (via -O) some parts of the compiler
12733 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12734 represent source-level labels which were explicitly declared by
12735 the user. This really shouldn't be happening though, so catch
12736 it if it ever does happen. */
12737 gcc_assert (!INSN_DELETED_P (insn));
12738
12739 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12740 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12741 }
12742 }
12743 }
12744
12745 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
12746 attributes to the DIE for a block STMT, to describe where the inlined
12747 function was called from. This is similar to add_src_coords_attributes. */
12748
12749 static inline void
12750 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12751 {
12752 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12753
12754 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12755 add_AT_unsigned (die, DW_AT_call_line, s.line);
12756 }
12757
12758
12759 /* If STMT's abstract origin is a function declaration and STMT's
12760 first subblock's abstract origin is the function's outermost block,
12761 then we're looking at the main entry point. */
12762 static bool
12763 is_inlined_entry_point (const_tree stmt)
12764 {
12765 tree decl, block;
12766
12767 if (!stmt || TREE_CODE (stmt) != BLOCK)
12768 return false;
12769
12770 decl = block_ultimate_origin (stmt);
12771
12772 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12773 return false;
12774
12775 block = BLOCK_SUBBLOCKS (stmt);
12776
12777 if (block)
12778 {
12779 if (TREE_CODE (block) != BLOCK)
12780 return false;
12781
12782 block = block_ultimate_origin (block);
12783 }
12784
12785 return block == DECL_INITIAL (decl);
12786 }
12787
12788 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12789 Add low_pc and high_pc attributes to the DIE for a block STMT. */
12790
12791 static inline void
12792 add_high_low_attributes (tree stmt, dw_die_ref die)
12793 {
12794 char label[MAX_ARTIFICIAL_LABEL_BYTES];
12795
12796 if (BLOCK_FRAGMENT_CHAIN (stmt))
12797 {
12798 tree chain;
12799
12800 if (is_inlined_entry_point (stmt))
12801 {
12802 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12803 BLOCK_NUMBER (stmt));
12804 add_AT_lbl_id (die, DW_AT_entry_pc, label);
12805 }
12806
12807 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12808
12809 chain = BLOCK_FRAGMENT_CHAIN (stmt);
12810 do
12811 {
12812 add_ranges (chain);
12813 chain = BLOCK_FRAGMENT_CHAIN (chain);
12814 }
12815 while (chain);
12816 add_ranges (NULL);
12817 }
12818 else
12819 {
12820 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12821 BLOCK_NUMBER (stmt));
12822 add_AT_lbl_id (die, DW_AT_low_pc, label);
12823 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12824 BLOCK_NUMBER (stmt));
12825 add_AT_lbl_id (die, DW_AT_high_pc, label);
12826 }
12827 }
12828
12829 /* Generate a DIE for a lexical block. */
12830
12831 static void
12832 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12833 {
12834 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12835
12836 if (! BLOCK_ABSTRACT (stmt))
12837 add_high_low_attributes (stmt, stmt_die);
12838
12839 decls_for_scope (stmt, stmt_die, depth);
12840 }
12841
12842 /* Generate a DIE for an inlined subprogram. */
12843
12844 static void
12845 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12846 {
12847 tree decl = block_ultimate_origin (stmt);
12848
12849 /* Emit info for the abstract instance first, if we haven't yet. We
12850 must emit this even if the block is abstract, otherwise when we
12851 emit the block below (or elsewhere), we may end up trying to emit
12852 a die whose origin die hasn't been emitted, and crashing. */
12853 dwarf2out_abstract_function (decl);
12854
12855 if (! BLOCK_ABSTRACT (stmt))
12856 {
12857 dw_die_ref subr_die
12858 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12859
12860 add_abstract_origin_attribute (subr_die, decl);
12861 add_high_low_attributes (stmt, subr_die);
12862 add_call_src_coords_attributes (stmt, subr_die);
12863
12864 decls_for_scope (stmt, subr_die, depth);
12865 current_function_has_inlines = 1;
12866 }
12867 else
12868 /* We may get here if we're the outer block of function A that was
12869 inlined into function B that was inlined into function C. When
12870 generating debugging info for C, dwarf2out_abstract_function(B)
12871 would mark all inlined blocks as abstract, including this one.
12872 So, we wouldn't (and shouldn't) expect labels to be generated
12873 for this one. Instead, just emit debugging info for
12874 declarations within the block. This is particularly important
12875 in the case of initializers of arguments passed from B to us:
12876 if they're statement expressions containing declarations, we
12877 wouldn't generate dies for their abstract variables, and then,
12878 when generating dies for the real variables, we'd die (pun
12879 intended :-) */
12880 gen_lexical_block_die (stmt, context_die, depth);
12881 }
12882
12883 /* Generate a DIE for a field in a record, or structure. */
12884
12885 static void
12886 gen_field_die (tree decl, dw_die_ref context_die)
12887 {
12888 dw_die_ref decl_die;
12889
12890 if (TREE_TYPE (decl) == error_mark_node)
12891 return;
12892
12893 decl_die = new_die (DW_TAG_member, context_die, decl);
12894 add_name_and_src_coords_attributes (decl_die, decl);
12895 add_type_attribute (decl_die, member_declared_type (decl),
12896 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12897 context_die);
12898
12899 if (DECL_BIT_FIELD_TYPE (decl))
12900 {
12901 add_byte_size_attribute (decl_die, decl);
12902 add_bit_size_attribute (decl_die, decl);
12903 add_bit_offset_attribute (decl_die, decl);
12904 }
12905
12906 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12907 add_data_member_location_attribute (decl_die, decl);
12908
12909 if (DECL_ARTIFICIAL (decl))
12910 add_AT_flag (decl_die, DW_AT_artificial, 1);
12911
12912 if (TREE_PROTECTED (decl))
12913 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12914 else if (TREE_PRIVATE (decl))
12915 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12916
12917 /* Equate decl number to die, so that we can look up this decl later on. */
12918 equate_decl_number_to_die (decl, decl_die);
12919 }
12920
12921 #if 0
12922 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12923 Use modified_type_die instead.
12924 We keep this code here just in case these types of DIEs may be needed to
12925 represent certain things in other languages (e.g. Pascal) someday. */
12926
12927 static void
12928 gen_pointer_type_die (tree type, dw_die_ref context_die)
12929 {
12930 dw_die_ref ptr_die
12931 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12932
12933 equate_type_number_to_die (type, ptr_die);
12934 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12935 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12936 }
12937
12938 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12939 Use modified_type_die instead.
12940 We keep this code here just in case these types of DIEs may be needed to
12941 represent certain things in other languages (e.g. Pascal) someday. */
12942
12943 static void
12944 gen_reference_type_die (tree type, dw_die_ref context_die)
12945 {
12946 dw_die_ref ref_die
12947 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12948
12949 equate_type_number_to_die (type, ref_die);
12950 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12951 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12952 }
12953 #endif
12954
12955 /* Generate a DIE for a pointer to a member type. */
12956
12957 static void
12958 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12959 {
12960 dw_die_ref ptr_die
12961 = new_die (DW_TAG_ptr_to_member_type,
12962 scope_die_for (type, context_die), type);
12963
12964 equate_type_number_to_die (type, ptr_die);
12965 add_AT_die_ref (ptr_die, DW_AT_containing_type,
12966 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12967 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12968 }
12969
12970 /* Generate the DIE for the compilation unit. */
12971
12972 static dw_die_ref
12973 gen_compile_unit_die (const char *filename)
12974 {
12975 dw_die_ref die;
12976 char producer[250];
12977 const char *language_string = lang_hooks.name;
12978 int language;
12979
12980 die = new_die (DW_TAG_compile_unit, NULL, NULL);
12981
12982 if (filename)
12983 {
12984 add_name_attribute (die, filename);
12985 /* Don't add cwd for <built-in>. */
12986 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12987 add_comp_dir_attribute (die);
12988 }
12989
12990 sprintf (producer, "%s %s", language_string, version_string);
12991
12992 #ifdef MIPS_DEBUGGING_INFO
12993 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12994 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12995 not appear in the producer string, the debugger reaches the conclusion
12996 that the object file is stripped and has no debugging information.
12997 To get the MIPS/SGI debugger to believe that there is debugging
12998 information in the object file, we add a -g to the producer string. */
12999 if (debug_info_level > DINFO_LEVEL_TERSE)
13000 strcat (producer, " -g");
13001 #endif
13002
13003 add_AT_string (die, DW_AT_producer, producer);
13004
13005 if (strcmp (language_string, "GNU C++") == 0)
13006 language = DW_LANG_C_plus_plus;
13007 else if (strcmp (language_string, "GNU Ada") == 0)
13008 language = DW_LANG_Ada95;
13009 else if (strcmp (language_string, "GNU F77") == 0)
13010 language = DW_LANG_Fortran77;
13011 else if (strcmp (language_string, "GNU F95") == 0)
13012 language = DW_LANG_Fortran95;
13013 else if (strcmp (language_string, "GNU Pascal") == 0)
13014 language = DW_LANG_Pascal83;
13015 else if (strcmp (language_string, "GNU Java") == 0)
13016 language = DW_LANG_Java;
13017 else if (strcmp (language_string, "GNU Objective-C") == 0)
13018 language = DW_LANG_ObjC;
13019 else if (strcmp (language_string, "GNU Objective-C++") == 0)
13020 language = DW_LANG_ObjC_plus_plus;
13021 else
13022 language = DW_LANG_C89;
13023
13024 add_AT_unsigned (die, DW_AT_language, language);
13025 return die;
13026 }
13027
13028 /* Generate the DIE for a base class. */
13029
13030 static void
13031 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13032 {
13033 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13034
13035 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13036 add_data_member_location_attribute (die, binfo);
13037
13038 if (BINFO_VIRTUAL_P (binfo))
13039 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13040
13041 if (access == access_public_node)
13042 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13043 else if (access == access_protected_node)
13044 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13045 }
13046
13047 /* Generate a DIE for a class member. */
13048
13049 static void
13050 gen_member_die (tree type, dw_die_ref context_die)
13051 {
13052 tree member;
13053 tree binfo = TYPE_BINFO (type);
13054 dw_die_ref child;
13055
13056 /* If this is not an incomplete type, output descriptions of each of its
13057 members. Note that as we output the DIEs necessary to represent the
13058 members of this record or union type, we will also be trying to output
13059 DIEs to represent the *types* of those members. However the `type'
13060 function (above) will specifically avoid generating type DIEs for member
13061 types *within* the list of member DIEs for this (containing) type except
13062 for those types (of members) which are explicitly marked as also being
13063 members of this (containing) type themselves. The g++ front- end can
13064 force any given type to be treated as a member of some other (containing)
13065 type by setting the TYPE_CONTEXT of the given (member) type to point to
13066 the TREE node representing the appropriate (containing) type. */
13067
13068 /* First output info about the base classes. */
13069 if (binfo)
13070 {
13071 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13072 int i;
13073 tree base;
13074
13075 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13076 gen_inheritance_die (base,
13077 (accesses ? VEC_index (tree, accesses, i)
13078 : access_public_node), context_die);
13079 }
13080
13081 /* Now output info about the data members and type members. */
13082 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13083 {
13084 /* If we thought we were generating minimal debug info for TYPE
13085 and then changed our minds, some of the member declarations
13086 may have already been defined. Don't define them again, but
13087 do put them in the right order. */
13088
13089 child = lookup_decl_die (member);
13090 if (child)
13091 splice_child_die (context_die, child);
13092 else
13093 gen_decl_die (member, context_die);
13094 }
13095
13096 /* Now output info about the function members (if any). */
13097 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13098 {
13099 /* Don't include clones in the member list. */
13100 if (DECL_ABSTRACT_ORIGIN (member))
13101 continue;
13102
13103 child = lookup_decl_die (member);
13104 if (child)
13105 splice_child_die (context_die, child);
13106 else
13107 gen_decl_die (member, context_die);
13108 }
13109 }
13110
13111 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
13112 is set, we pretend that the type was never defined, so we only get the
13113 member DIEs needed by later specification DIEs. */
13114
13115 static void
13116 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13117 enum debug_info_usage usage)
13118 {
13119 dw_die_ref type_die = lookup_type_die (type);
13120 dw_die_ref scope_die = 0;
13121 int nested = 0;
13122 int complete = (TYPE_SIZE (type)
13123 && (! TYPE_STUB_DECL (type)
13124 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13125 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13126 complete = complete && should_emit_struct_debug (type, usage);
13127
13128 if (type_die && ! complete)
13129 return;
13130
13131 if (TYPE_CONTEXT (type) != NULL_TREE
13132 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13133 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13134 nested = 1;
13135
13136 scope_die = scope_die_for (type, context_die);
13137
13138 if (! type_die || (nested && scope_die == comp_unit_die))
13139 /* First occurrence of type or toplevel definition of nested class. */
13140 {
13141 dw_die_ref old_die = type_die;
13142
13143 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13144 ? record_type_tag (type) : DW_TAG_union_type,
13145 scope_die, type);
13146 equate_type_number_to_die (type, type_die);
13147 if (old_die)
13148 add_AT_specification (type_die, old_die);
13149 else
13150 add_name_attribute (type_die, type_tag (type));
13151 }
13152 else
13153 remove_AT (type_die, DW_AT_declaration);
13154
13155 /* If this type has been completed, then give it a byte_size attribute and
13156 then give a list of members. */
13157 if (complete && !ns_decl)
13158 {
13159 /* Prevent infinite recursion in cases where the type of some member of
13160 this type is expressed in terms of this type itself. */
13161 TREE_ASM_WRITTEN (type) = 1;
13162 add_byte_size_attribute (type_die, type);
13163 if (TYPE_STUB_DECL (type) != NULL_TREE)
13164 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13165
13166 /* If the first reference to this type was as the return type of an
13167 inline function, then it may not have a parent. Fix this now. */
13168 if (type_die->die_parent == NULL)
13169 add_child_die (scope_die, type_die);
13170
13171 push_decl_scope (type);
13172 gen_member_die (type, type_die);
13173 pop_decl_scope ();
13174
13175 /* GNU extension: Record what type our vtable lives in. */
13176 if (TYPE_VFIELD (type))
13177 {
13178 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13179
13180 gen_type_die (vtype, context_die);
13181 add_AT_die_ref (type_die, DW_AT_containing_type,
13182 lookup_type_die (vtype));
13183 }
13184 }
13185 else
13186 {
13187 add_AT_flag (type_die, DW_AT_declaration, 1);
13188
13189 /* We don't need to do this for function-local types. */
13190 if (TYPE_STUB_DECL (type)
13191 && ! decl_function_context (TYPE_STUB_DECL (type)))
13192 VEC_safe_push (tree, gc, incomplete_types, type);
13193 }
13194
13195 if (get_AT (type_die, DW_AT_name))
13196 add_pubtype (type, type_die);
13197 }
13198
13199 /* Generate a DIE for a subroutine _type_. */
13200
13201 static void
13202 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13203 {
13204 tree return_type = TREE_TYPE (type);
13205 dw_die_ref subr_die
13206 = new_die (DW_TAG_subroutine_type,
13207 scope_die_for (type, context_die), type);
13208
13209 equate_type_number_to_die (type, subr_die);
13210 add_prototyped_attribute (subr_die, type);
13211 add_type_attribute (subr_die, return_type, 0, 0, context_die);
13212 gen_formal_types_die (type, subr_die);
13213
13214 if (get_AT (subr_die, DW_AT_name))
13215 add_pubtype (type, subr_die);
13216 }
13217
13218 /* Generate a DIE for a type definition. */
13219
13220 static void
13221 gen_typedef_die (tree decl, dw_die_ref context_die)
13222 {
13223 dw_die_ref type_die;
13224 tree origin;
13225
13226 if (TREE_ASM_WRITTEN (decl))
13227 return;
13228
13229 TREE_ASM_WRITTEN (decl) = 1;
13230 type_die = new_die (DW_TAG_typedef, context_die, decl);
13231 origin = decl_ultimate_origin (decl);
13232 if (origin != NULL)
13233 add_abstract_origin_attribute (type_die, origin);
13234 else
13235 {
13236 tree type;
13237
13238 add_name_and_src_coords_attributes (type_die, decl);
13239 if (DECL_ORIGINAL_TYPE (decl))
13240 {
13241 type = DECL_ORIGINAL_TYPE (decl);
13242
13243 gcc_assert (type != TREE_TYPE (decl));
13244 equate_type_number_to_die (TREE_TYPE (decl), type_die);
13245 }
13246 else
13247 type = TREE_TYPE (decl);
13248
13249 add_type_attribute (type_die, type, TREE_READONLY (decl),
13250 TREE_THIS_VOLATILE (decl), context_die);
13251 }
13252
13253 if (DECL_ABSTRACT (decl))
13254 equate_decl_number_to_die (decl, type_die);
13255
13256 if (get_AT (type_die, DW_AT_name))
13257 add_pubtype (decl, type_die);
13258 }
13259
13260 /* Generate a type description DIE. */
13261
13262 static void
13263 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13264 enum debug_info_usage usage)
13265 {
13266 int need_pop;
13267 struct array_descr_info info;
13268
13269 if (type == NULL_TREE || type == error_mark_node)
13270 return;
13271
13272 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13273 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13274 {
13275 if (TREE_ASM_WRITTEN (type))
13276 return;
13277
13278 /* Prevent broken recursion; we can't hand off to the same type. */
13279 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13280
13281 TREE_ASM_WRITTEN (type) = 1;
13282 gen_decl_die (TYPE_NAME (type), context_die);
13283 return;
13284 }
13285
13286 /* If this is an array type with hidden descriptor, handle it first. */
13287 if (!TREE_ASM_WRITTEN (type)
13288 && lang_hooks.types.get_array_descr_info
13289 && lang_hooks.types.get_array_descr_info (type, &info))
13290 {
13291 gen_descr_array_type_die (type, &info, context_die);
13292 TREE_ASM_WRITTEN (type) = 1;
13293 return;
13294 }
13295
13296 /* We are going to output a DIE to represent the unqualified version
13297 of this type (i.e. without any const or volatile qualifiers) so
13298 get the main variant (i.e. the unqualified version) of this type
13299 now. (Vectors are special because the debugging info is in the
13300 cloned type itself). */
13301 if (TREE_CODE (type) != VECTOR_TYPE)
13302 type = type_main_variant (type);
13303
13304 if (TREE_ASM_WRITTEN (type))
13305 return;
13306
13307 switch (TREE_CODE (type))
13308 {
13309 case ERROR_MARK:
13310 break;
13311
13312 case POINTER_TYPE:
13313 case REFERENCE_TYPE:
13314 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
13315 ensures that the gen_type_die recursion will terminate even if the
13316 type is recursive. Recursive types are possible in Ada. */
13317 /* ??? We could perhaps do this for all types before the switch
13318 statement. */
13319 TREE_ASM_WRITTEN (type) = 1;
13320
13321 /* For these types, all that is required is that we output a DIE (or a
13322 set of DIEs) to represent the "basis" type. */
13323 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13324 DINFO_USAGE_IND_USE);
13325 break;
13326
13327 case OFFSET_TYPE:
13328 /* This code is used for C++ pointer-to-data-member types.
13329 Output a description of the relevant class type. */
13330 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13331 DINFO_USAGE_IND_USE);
13332
13333 /* Output a description of the type of the object pointed to. */
13334 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13335 DINFO_USAGE_IND_USE);
13336
13337 /* Now output a DIE to represent this pointer-to-data-member type
13338 itself. */
13339 gen_ptr_to_mbr_type_die (type, context_die);
13340 break;
13341
13342 case FUNCTION_TYPE:
13343 /* Force out return type (in case it wasn't forced out already). */
13344 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13345 DINFO_USAGE_DIR_USE);
13346 gen_subroutine_type_die (type, context_die);
13347 break;
13348
13349 case METHOD_TYPE:
13350 /* Force out return type (in case it wasn't forced out already). */
13351 gen_type_die_with_usage (TREE_TYPE (type), context_die,
13352 DINFO_USAGE_DIR_USE);
13353 gen_subroutine_type_die (type, context_die);
13354 break;
13355
13356 case ARRAY_TYPE:
13357 gen_array_type_die (type, context_die);
13358 break;
13359
13360 case VECTOR_TYPE:
13361 gen_array_type_die (type, context_die);
13362 break;
13363
13364 case ENUMERAL_TYPE:
13365 case RECORD_TYPE:
13366 case UNION_TYPE:
13367 case QUAL_UNION_TYPE:
13368 /* If this is a nested type whose containing class hasn't been written
13369 out yet, writing it out will cover this one, too. This does not apply
13370 to instantiations of member class templates; they need to be added to
13371 the containing class as they are generated. FIXME: This hurts the
13372 idea of combining type decls from multiple TUs, since we can't predict
13373 what set of template instantiations we'll get. */
13374 if (TYPE_CONTEXT (type)
13375 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13376 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13377 {
13378 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13379
13380 if (TREE_ASM_WRITTEN (type))
13381 return;
13382
13383 /* If that failed, attach ourselves to the stub. */
13384 push_decl_scope (TYPE_CONTEXT (type));
13385 context_die = lookup_type_die (TYPE_CONTEXT (type));
13386 need_pop = 1;
13387 }
13388 else
13389 {
13390 declare_in_namespace (type, context_die);
13391 need_pop = 0;
13392 }
13393
13394 if (TREE_CODE (type) == ENUMERAL_TYPE)
13395 {
13396 /* This might have been written out by the call to
13397 declare_in_namespace. */
13398 if (!TREE_ASM_WRITTEN (type))
13399 gen_enumeration_type_die (type, context_die);
13400 }
13401 else
13402 gen_struct_or_union_type_die (type, context_die, usage);
13403
13404 if (need_pop)
13405 pop_decl_scope ();
13406
13407 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13408 it up if it is ever completed. gen_*_type_die will set it for us
13409 when appropriate. */
13410 return;
13411
13412 case VOID_TYPE:
13413 case INTEGER_TYPE:
13414 case REAL_TYPE:
13415 case FIXED_POINT_TYPE:
13416 case COMPLEX_TYPE:
13417 case BOOLEAN_TYPE:
13418 /* No DIEs needed for fundamental types. */
13419 break;
13420
13421 case LANG_TYPE:
13422 /* No Dwarf representation currently defined. */
13423 break;
13424
13425 default:
13426 gcc_unreachable ();
13427 }
13428
13429 TREE_ASM_WRITTEN (type) = 1;
13430 }
13431
13432 static void
13433 gen_type_die (tree type, dw_die_ref context_die)
13434 {
13435 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13436 }
13437
13438 /* Generate a DIE for a tagged type instantiation. */
13439
13440 static void
13441 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13442 {
13443 if (type == NULL_TREE || type == error_mark_node)
13444 return;
13445
13446 /* We are going to output a DIE to represent the unqualified version of
13447 this type (i.e. without any const or volatile qualifiers) so make sure
13448 that we have the main variant (i.e. the unqualified version) of this
13449 type now. */
13450 gcc_assert (type == type_main_variant (type));
13451
13452 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13453 an instance of an unresolved type. */
13454
13455 switch (TREE_CODE (type))
13456 {
13457 case ERROR_MARK:
13458 break;
13459
13460 case ENUMERAL_TYPE:
13461 gen_inlined_enumeration_type_die (type, context_die);
13462 break;
13463
13464 case RECORD_TYPE:
13465 gen_inlined_structure_type_die (type, context_die);
13466 break;
13467
13468 case UNION_TYPE:
13469 case QUAL_UNION_TYPE:
13470 gen_inlined_union_type_die (type, context_die);
13471 break;
13472
13473 default:
13474 gcc_unreachable ();
13475 }
13476 }
13477
13478 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13479 things which are local to the given block. */
13480
13481 static void
13482 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13483 {
13484 int must_output_die = 0;
13485 tree origin;
13486 tree decl;
13487 enum tree_code origin_code;
13488
13489 /* Ignore blocks that are NULL. */
13490 if (stmt == NULL_TREE)
13491 return;
13492
13493 /* If the block is one fragment of a non-contiguous block, do not
13494 process the variables, since they will have been done by the
13495 origin block. Do process subblocks. */
13496 if (BLOCK_FRAGMENT_ORIGIN (stmt))
13497 {
13498 tree sub;
13499
13500 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13501 gen_block_die (sub, context_die, depth + 1);
13502
13503 return;
13504 }
13505
13506 /* Determine the "ultimate origin" of this block. This block may be an
13507 inlined instance of an inlined instance of inline function, so we have
13508 to trace all of the way back through the origin chain to find out what
13509 sort of node actually served as the original seed for the creation of
13510 the current block. */
13511 origin = block_ultimate_origin (stmt);
13512 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13513
13514 /* Determine if we need to output any Dwarf DIEs at all to represent this
13515 block. */
13516 if (origin_code == FUNCTION_DECL)
13517 /* The outer scopes for inlinings *must* always be represented. We
13518 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
13519 must_output_die = 1;
13520 else
13521 {
13522 /* In the case where the current block represents an inlining of the
13523 "body block" of an inline function, we must *NOT* output any DIE for
13524 this block because we have already output a DIE to represent the whole
13525 inlined function scope and the "body block" of any function doesn't
13526 really represent a different scope according to ANSI C rules. So we
13527 check here to make sure that this block does not represent a "body
13528 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
13529 if (! is_body_block (origin ? origin : stmt))
13530 {
13531 /* Determine if this block directly contains any "significant"
13532 local declarations which we will need to output DIEs for. */
13533 if (debug_info_level > DINFO_LEVEL_TERSE)
13534 /* We are not in terse mode so *any* local declaration counts
13535 as being a "significant" one. */
13536 must_output_die = (BLOCK_VARS (stmt) != NULL
13537 && (TREE_USED (stmt)
13538 || TREE_ASM_WRITTEN (stmt)
13539 || BLOCK_ABSTRACT (stmt)));
13540 else
13541 /* We are in terse mode, so only local (nested) function
13542 definitions count as "significant" local declarations. */
13543 for (decl = BLOCK_VARS (stmt);
13544 decl != NULL; decl = TREE_CHAIN (decl))
13545 if (TREE_CODE (decl) == FUNCTION_DECL
13546 && DECL_INITIAL (decl))
13547 {
13548 must_output_die = 1;
13549 break;
13550 }
13551 }
13552 }
13553
13554 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13555 DIE for any block which contains no significant local declarations at
13556 all. Rather, in such cases we just call `decls_for_scope' so that any
13557 needed Dwarf info for any sub-blocks will get properly generated. Note
13558 that in terse mode, our definition of what constitutes a "significant"
13559 local declaration gets restricted to include only inlined function
13560 instances and local (nested) function definitions. */
13561 if (must_output_die)
13562 {
13563 if (origin_code == FUNCTION_DECL)
13564 gen_inlined_subroutine_die (stmt, context_die, depth);
13565 else
13566 gen_lexical_block_die (stmt, context_die, depth);
13567 }
13568 else
13569 decls_for_scope (stmt, context_die, depth);
13570 }
13571
13572 /* Generate all of the decls declared within a given scope and (recursively)
13573 all of its sub-blocks. */
13574
13575 static void
13576 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13577 {
13578 tree decl;
13579 tree subblocks;
13580
13581 /* Ignore NULL blocks. */
13582 if (stmt == NULL_TREE)
13583 return;
13584
13585 if (TREE_USED (stmt))
13586 {
13587 /* Output the DIEs to represent all of the data objects and typedefs
13588 declared directly within this block but not within any nested
13589 sub-blocks. Also, nested function and tag DIEs have been
13590 generated with a parent of NULL; fix that up now. */
13591 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13592 {
13593 dw_die_ref die;
13594
13595 if (TREE_CODE (decl) == FUNCTION_DECL)
13596 die = lookup_decl_die (decl);
13597 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13598 die = lookup_type_die (TREE_TYPE (decl));
13599 else
13600 die = NULL;
13601
13602 if (die != NULL && die->die_parent == NULL)
13603 add_child_die (context_die, die);
13604 /* Do not produce debug information for static variables since
13605 these might be optimized out. We are called for these later
13606 in varpool_analyze_pending_decls. */
13607 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13608 ;
13609 else
13610 gen_decl_die (decl, context_die);
13611 }
13612 }
13613
13614 /* If we're at -g1, we're not interested in subblocks. */
13615 if (debug_info_level <= DINFO_LEVEL_TERSE)
13616 return;
13617
13618 /* Output the DIEs to represent all sub-blocks (and the items declared
13619 therein) of this block. */
13620 for (subblocks = BLOCK_SUBBLOCKS (stmt);
13621 subblocks != NULL;
13622 subblocks = BLOCK_CHAIN (subblocks))
13623 gen_block_die (subblocks, context_die, depth + 1);
13624 }
13625
13626 /* Is this a typedef we can avoid emitting? */
13627
13628 static inline int
13629 is_redundant_typedef (const_tree decl)
13630 {
13631 if (TYPE_DECL_IS_STUB (decl))
13632 return 1;
13633
13634 if (DECL_ARTIFICIAL (decl)
13635 && DECL_CONTEXT (decl)
13636 && is_tagged_type (DECL_CONTEXT (decl))
13637 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13638 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13639 /* Also ignore the artificial member typedef for the class name. */
13640 return 1;
13641
13642 return 0;
13643 }
13644
13645 /* Returns the DIE for decl. A DIE will always be returned. */
13646
13647 static dw_die_ref
13648 force_decl_die (tree decl)
13649 {
13650 dw_die_ref decl_die;
13651 unsigned saved_external_flag;
13652 tree save_fn = NULL_TREE;
13653 decl_die = lookup_decl_die (decl);
13654 if (!decl_die)
13655 {
13656 dw_die_ref context_die;
13657 tree decl_context = DECL_CONTEXT (decl);
13658 if (decl_context)
13659 {
13660 /* Find die that represents this context. */
13661 if (TYPE_P (decl_context))
13662 context_die = force_type_die (decl_context);
13663 else
13664 context_die = force_decl_die (decl_context);
13665 }
13666 else
13667 context_die = comp_unit_die;
13668
13669 decl_die = lookup_decl_die (decl);
13670 if (decl_die)
13671 return decl_die;
13672
13673 switch (TREE_CODE (decl))
13674 {
13675 case FUNCTION_DECL:
13676 /* Clear current_function_decl, so that gen_subprogram_die thinks
13677 that this is a declaration. At this point, we just want to force
13678 declaration die. */
13679 save_fn = current_function_decl;
13680 current_function_decl = NULL_TREE;
13681 gen_subprogram_die (decl, context_die);
13682 current_function_decl = save_fn;
13683 break;
13684
13685 case VAR_DECL:
13686 /* Set external flag to force declaration die. Restore it after
13687 gen_decl_die() call. */
13688 saved_external_flag = DECL_EXTERNAL (decl);
13689 DECL_EXTERNAL (decl) = 1;
13690 gen_decl_die (decl, context_die);
13691 DECL_EXTERNAL (decl) = saved_external_flag;
13692 break;
13693
13694 case NAMESPACE_DECL:
13695 dwarf2out_decl (decl);
13696 break;
13697
13698 default:
13699 gcc_unreachable ();
13700 }
13701
13702 /* We should be able to find the DIE now. */
13703 if (!decl_die)
13704 decl_die = lookup_decl_die (decl);
13705 gcc_assert (decl_die);
13706 }
13707
13708 return decl_die;
13709 }
13710
13711 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
13712 always returned. */
13713
13714 static dw_die_ref
13715 force_type_die (tree type)
13716 {
13717 dw_die_ref type_die;
13718
13719 type_die = lookup_type_die (type);
13720 if (!type_die)
13721 {
13722 dw_die_ref context_die;
13723 if (TYPE_CONTEXT (type))
13724 {
13725 if (TYPE_P (TYPE_CONTEXT (type)))
13726 context_die = force_type_die (TYPE_CONTEXT (type));
13727 else
13728 context_die = force_decl_die (TYPE_CONTEXT (type));
13729 }
13730 else
13731 context_die = comp_unit_die;
13732
13733 type_die = lookup_type_die (type);
13734 if (type_die)
13735 return type_die;
13736 gen_type_die (type, context_die);
13737 type_die = lookup_type_die (type);
13738 gcc_assert (type_die);
13739 }
13740 return type_die;
13741 }
13742
13743 /* Force out any required namespaces to be able to output DECL,
13744 and return the new context_die for it, if it's changed. */
13745
13746 static dw_die_ref
13747 setup_namespace_context (tree thing, dw_die_ref context_die)
13748 {
13749 tree context = (DECL_P (thing)
13750 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13751 if (context && TREE_CODE (context) == NAMESPACE_DECL)
13752 /* Force out the namespace. */
13753 context_die = force_decl_die (context);
13754
13755 return context_die;
13756 }
13757
13758 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13759 type) within its namespace, if appropriate.
13760
13761 For compatibility with older debuggers, namespace DIEs only contain
13762 declarations; all definitions are emitted at CU scope. */
13763
13764 static void
13765 declare_in_namespace (tree thing, dw_die_ref context_die)
13766 {
13767 dw_die_ref ns_context;
13768
13769 if (debug_info_level <= DINFO_LEVEL_TERSE)
13770 return;
13771
13772 /* If this decl is from an inlined function, then don't try to emit it in its
13773 namespace, as we will get confused. It would have already been emitted
13774 when the abstract instance of the inline function was emitted anyways. */
13775 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13776 return;
13777
13778 ns_context = setup_namespace_context (thing, context_die);
13779
13780 if (ns_context != context_die)
13781 {
13782 if (DECL_P (thing))
13783 gen_decl_die (thing, ns_context);
13784 else
13785 gen_type_die (thing, ns_context);
13786 }
13787 }
13788
13789 /* Generate a DIE for a namespace or namespace alias. */
13790
13791 static void
13792 gen_namespace_die (tree decl)
13793 {
13794 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13795
13796 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13797 they are an alias of. */
13798 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13799 {
13800 /* Output a real namespace. */
13801 dw_die_ref namespace_die
13802 = new_die (DW_TAG_namespace, context_die, decl);
13803 add_name_and_src_coords_attributes (namespace_die, decl);
13804 equate_decl_number_to_die (decl, namespace_die);
13805 }
13806 else
13807 {
13808 /* Output a namespace alias. */
13809
13810 /* Force out the namespace we are an alias of, if necessary. */
13811 dw_die_ref origin_die
13812 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13813
13814 /* Now create the namespace alias DIE. */
13815 dw_die_ref namespace_die
13816 = new_die (DW_TAG_imported_declaration, context_die, decl);
13817 add_name_and_src_coords_attributes (namespace_die, decl);
13818 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13819 equate_decl_number_to_die (decl, namespace_die);
13820 }
13821 }
13822
13823 /* Generate Dwarf debug information for a decl described by DECL. */
13824
13825 static void
13826 gen_decl_die (tree decl, dw_die_ref context_die)
13827 {
13828 tree origin;
13829
13830 if (DECL_P (decl) && DECL_IGNORED_P (decl))
13831 return;
13832
13833 switch (TREE_CODE (decl))
13834 {
13835 case ERROR_MARK:
13836 break;
13837
13838 case CONST_DECL:
13839 /* The individual enumerators of an enum type get output when we output
13840 the Dwarf representation of the relevant enum type itself. */
13841 break;
13842
13843 case FUNCTION_DECL:
13844 /* Don't output any DIEs to represent mere function declarations,
13845 unless they are class members or explicit block externs. */
13846 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13847 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13848 break;
13849
13850 #if 0
13851 /* FIXME */
13852 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13853 on local redeclarations of global functions. That seems broken. */
13854 if (current_function_decl != decl)
13855 /* This is only a declaration. */;
13856 #endif
13857
13858 /* If we're emitting a clone, emit info for the abstract instance. */
13859 if (DECL_ORIGIN (decl) != decl)
13860 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13861
13862 /* If we're emitting an out-of-line copy of an inline function,
13863 emit info for the abstract instance and set up to refer to it. */
13864 else if (cgraph_function_possibly_inlined_p (decl)
13865 && ! DECL_ABSTRACT (decl)
13866 && ! class_or_namespace_scope_p (context_die)
13867 /* dwarf2out_abstract_function won't emit a die if this is just
13868 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
13869 that case, because that works only if we have a die. */
13870 && DECL_INITIAL (decl) != NULL_TREE)
13871 {
13872 dwarf2out_abstract_function (decl);
13873 set_decl_origin_self (decl);
13874 }
13875
13876 /* Otherwise we're emitting the primary DIE for this decl. */
13877 else if (debug_info_level > DINFO_LEVEL_TERSE)
13878 {
13879 /* Before we describe the FUNCTION_DECL itself, make sure that we
13880 have described its return type. */
13881 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13882
13883 /* And its virtual context. */
13884 if (DECL_VINDEX (decl) != NULL_TREE)
13885 gen_type_die (DECL_CONTEXT (decl), context_die);
13886
13887 /* And its containing type. */
13888 origin = decl_class_context (decl);
13889 if (origin != NULL_TREE)
13890 gen_type_die_for_member (origin, decl, context_die);
13891
13892 /* And its containing namespace. */
13893 declare_in_namespace (decl, context_die);
13894 }
13895
13896 /* Now output a DIE to represent the function itself. */
13897 gen_subprogram_die (decl, context_die);
13898 break;
13899
13900 case TYPE_DECL:
13901 /* If we are in terse mode, don't generate any DIEs to represent any
13902 actual typedefs. */
13903 if (debug_info_level <= DINFO_LEVEL_TERSE)
13904 break;
13905
13906 /* In the special case of a TYPE_DECL node representing the declaration
13907 of some type tag, if the given TYPE_DECL is marked as having been
13908 instantiated from some other (original) TYPE_DECL node (e.g. one which
13909 was generated within the original definition of an inline function) we
13910 have to generate a special (abbreviated) DW_TAG_structure_type,
13911 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
13912 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13913 && is_tagged_type (TREE_TYPE (decl)))
13914 {
13915 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13916 break;
13917 }
13918
13919 if (is_redundant_typedef (decl))
13920 gen_type_die (TREE_TYPE (decl), context_die);
13921 else
13922 /* Output a DIE to represent the typedef itself. */
13923 gen_typedef_die (decl, context_die);
13924 break;
13925
13926 case LABEL_DECL:
13927 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13928 gen_label_die (decl, context_die);
13929 break;
13930
13931 case VAR_DECL:
13932 case RESULT_DECL:
13933 /* If we are in terse mode, don't generate any DIEs to represent any
13934 variable declarations or definitions. */
13935 if (debug_info_level <= DINFO_LEVEL_TERSE)
13936 break;
13937
13938 /* Output any DIEs that are needed to specify the type of this data
13939 object. */
13940 if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
13941 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13942 else
13943 gen_type_die (TREE_TYPE (decl), context_die);
13944
13945 /* And its containing type. */
13946 origin = decl_class_context (decl);
13947 if (origin != NULL_TREE)
13948 gen_type_die_for_member (origin, decl, context_die);
13949
13950 /* And its containing namespace. */
13951 declare_in_namespace (decl, context_die);
13952
13953 /* Now output the DIE to represent the data object itself. This gets
13954 complicated because of the possibility that the VAR_DECL really
13955 represents an inlined instance of a formal parameter for an inline
13956 function. */
13957 origin = decl_ultimate_origin (decl);
13958 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13959 gen_formal_parameter_die (decl, context_die);
13960 else
13961 gen_variable_die (decl, context_die);
13962 break;
13963
13964 case FIELD_DECL:
13965 /* Ignore the nameless fields that are used to skip bits but handle C++
13966 anonymous unions and structs. */
13967 if (DECL_NAME (decl) != NULL_TREE
13968 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13969 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13970 {
13971 gen_type_die (member_declared_type (decl), context_die);
13972 gen_field_die (decl, context_die);
13973 }
13974 break;
13975
13976 case PARM_DECL:
13977 if (DECL_BY_REFERENCE (decl))
13978 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13979 else
13980 gen_type_die (TREE_TYPE (decl), context_die);
13981 gen_formal_parameter_die (decl, context_die);
13982 break;
13983
13984 case NAMESPACE_DECL:
13985 gen_namespace_die (decl);
13986 break;
13987
13988 default:
13989 /* Probably some frontend-internal decl. Assume we don't care. */
13990 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13991 break;
13992 }
13993 }
13994 \f
13995 /* Output debug information for global decl DECL. Called from toplev.c after
13996 compilation proper has finished. */
13997
13998 static void
13999 dwarf2out_global_decl (tree decl)
14000 {
14001 /* Output DWARF2 information for file-scope tentative data object
14002 declarations, file-scope (extern) function declarations (which had no
14003 corresponding body) and file-scope tagged type declarations and
14004 definitions which have not yet been forced out. */
14005 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14006 dwarf2out_decl (decl);
14007 }
14008
14009 /* Output debug information for type decl DECL. Called from toplev.c
14010 and from language front ends (to record built-in types). */
14011 static void
14012 dwarf2out_type_decl (tree decl, int local)
14013 {
14014 if (!local)
14015 dwarf2out_decl (decl);
14016 }
14017
14018 /* Output debug information for imported module or decl. */
14019
14020 static void
14021 dwarf2out_imported_module_or_decl (tree decl, tree context)
14022 {
14023 dw_die_ref imported_die, at_import_die;
14024 dw_die_ref scope_die;
14025 expanded_location xloc;
14026
14027 if (debug_info_level <= DINFO_LEVEL_TERSE)
14028 return;
14029
14030 gcc_assert (decl);
14031
14032 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14033 We need decl DIE for reference and scope die. First, get DIE for the decl
14034 itself. */
14035
14036 /* Get the scope die for decl context. Use comp_unit_die for global module
14037 or decl. If die is not found for non globals, force new die. */
14038 if (!context)
14039 scope_die = comp_unit_die;
14040 else if (TYPE_P (context))
14041 {
14042 if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14043 return;
14044 scope_die = force_type_die (context);
14045 }
14046 else
14047 scope_die = force_decl_die (context);
14048
14049 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
14050 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14051 {
14052 if (is_base_type (TREE_TYPE (decl)))
14053 at_import_die = base_type_die (TREE_TYPE (decl));
14054 else
14055 at_import_die = force_type_die (TREE_TYPE (decl));
14056 }
14057 else
14058 {
14059 at_import_die = lookup_decl_die (decl);
14060 if (!at_import_die)
14061 {
14062 /* If we're trying to avoid duplicate debug info, we may not have
14063 emitted the member decl for this field. Emit it now. */
14064 if (TREE_CODE (decl) == FIELD_DECL)
14065 {
14066 tree type = DECL_CONTEXT (decl);
14067 dw_die_ref type_context_die;
14068
14069 if (TYPE_CONTEXT (type))
14070 if (TYPE_P (TYPE_CONTEXT (type)))
14071 {
14072 if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14073 DINFO_USAGE_DIR_USE))
14074 return;
14075 type_context_die = force_type_die (TYPE_CONTEXT (type));
14076 }
14077 else
14078 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14079 else
14080 type_context_die = comp_unit_die;
14081 gen_type_die_for_member (type, decl, type_context_die);
14082 }
14083 at_import_die = force_decl_die (decl);
14084 }
14085 }
14086
14087 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
14088 if (TREE_CODE (decl) == NAMESPACE_DECL)
14089 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14090 else
14091 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14092
14093 xloc = expand_location (input_location);
14094 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14095 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14096 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14097 }
14098
14099 /* Write the debugging output for DECL. */
14100
14101 void
14102 dwarf2out_decl (tree decl)
14103 {
14104 dw_die_ref context_die = comp_unit_die;
14105
14106 switch (TREE_CODE (decl))
14107 {
14108 case ERROR_MARK:
14109 return;
14110
14111 case FUNCTION_DECL:
14112 /* What we would really like to do here is to filter out all mere
14113 file-scope declarations of file-scope functions which are never
14114 referenced later within this translation unit (and keep all of ones
14115 that *are* referenced later on) but we aren't clairvoyant, so we have
14116 no idea which functions will be referenced in the future (i.e. later
14117 on within the current translation unit). So here we just ignore all
14118 file-scope function declarations which are not also definitions. If
14119 and when the debugger needs to know something about these functions,
14120 it will have to hunt around and find the DWARF information associated
14121 with the definition of the function.
14122
14123 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14124 nodes represent definitions and which ones represent mere
14125 declarations. We have to check DECL_INITIAL instead. That's because
14126 the C front-end supports some weird semantics for "extern inline"
14127 function definitions. These can get inlined within the current
14128 translation unit (and thus, we need to generate Dwarf info for their
14129 abstract instances so that the Dwarf info for the concrete inlined
14130 instances can have something to refer to) but the compiler never
14131 generates any out-of-lines instances of such things (despite the fact
14132 that they *are* definitions).
14133
14134 The important point is that the C front-end marks these "extern
14135 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14136 them anyway. Note that the C++ front-end also plays some similar games
14137 for inline function definitions appearing within include files which
14138 also contain `#pragma interface' pragmas. */
14139 if (DECL_INITIAL (decl) == NULL_TREE)
14140 return;
14141
14142 /* If we're a nested function, initially use a parent of NULL; if we're
14143 a plain function, this will be fixed up in decls_for_scope. If
14144 we're a method, it will be ignored, since we already have a DIE. */
14145 if (decl_function_context (decl)
14146 /* But if we're in terse mode, we don't care about scope. */
14147 && debug_info_level > DINFO_LEVEL_TERSE)
14148 context_die = NULL;
14149 break;
14150
14151 case VAR_DECL:
14152 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14153 declaration and if the declaration was never even referenced from
14154 within this entire compilation unit. We suppress these DIEs in
14155 order to save space in the .debug section (by eliminating entries
14156 which are probably useless). Note that we must not suppress
14157 block-local extern declarations (whether used or not) because that
14158 would screw-up the debugger's name lookup mechanism and cause it to
14159 miss things which really ought to be in scope at a given point. */
14160 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14161 return;
14162
14163 /* For local statics lookup proper context die. */
14164 if (TREE_STATIC (decl) && decl_function_context (decl))
14165 context_die = lookup_decl_die (DECL_CONTEXT (decl));
14166
14167 /* If we are in terse mode, don't generate any DIEs to represent any
14168 variable declarations or definitions. */
14169 if (debug_info_level <= DINFO_LEVEL_TERSE)
14170 return;
14171 break;
14172
14173 case NAMESPACE_DECL:
14174 if (debug_info_level <= DINFO_LEVEL_TERSE)
14175 return;
14176 if (lookup_decl_die (decl) != NULL)
14177 return;
14178 break;
14179
14180 case TYPE_DECL:
14181 /* Don't emit stubs for types unless they are needed by other DIEs. */
14182 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14183 return;
14184
14185 /* Don't bother trying to generate any DIEs to represent any of the
14186 normal built-in types for the language we are compiling. */
14187 if (DECL_IS_BUILTIN (decl))
14188 {
14189 /* OK, we need to generate one for `bool' so GDB knows what type
14190 comparisons have. */
14191 if (is_cxx ()
14192 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14193 && ! DECL_IGNORED_P (decl))
14194 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14195
14196 return;
14197 }
14198
14199 /* If we are in terse mode, don't generate any DIEs for types. */
14200 if (debug_info_level <= DINFO_LEVEL_TERSE)
14201 return;
14202
14203 /* If we're a function-scope tag, initially use a parent of NULL;
14204 this will be fixed up in decls_for_scope. */
14205 if (decl_function_context (decl))
14206 context_die = NULL;
14207
14208 break;
14209
14210 default:
14211 return;
14212 }
14213
14214 gen_decl_die (decl, context_die);
14215 }
14216
14217 /* Output a marker (i.e. a label) for the beginning of the generated code for
14218 a lexical block. */
14219
14220 static void
14221 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14222 unsigned int blocknum)
14223 {
14224 switch_to_section (current_function_section ());
14225 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14226 }
14227
14228 /* Output a marker (i.e. a label) for the end of the generated code for a
14229 lexical block. */
14230
14231 static void
14232 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14233 {
14234 switch_to_section (current_function_section ());
14235 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14236 }
14237
14238 /* Returns nonzero if it is appropriate not to emit any debugging
14239 information for BLOCK, because it doesn't contain any instructions.
14240
14241 Don't allow this for blocks with nested functions or local classes
14242 as we would end up with orphans, and in the presence of scheduling
14243 we may end up calling them anyway. */
14244
14245 static bool
14246 dwarf2out_ignore_block (const_tree block)
14247 {
14248 tree decl;
14249
14250 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14251 if (TREE_CODE (decl) == FUNCTION_DECL
14252 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14253 return 0;
14254
14255 return 1;
14256 }
14257
14258 /* Hash table routines for file_hash. */
14259
14260 static int
14261 file_table_eq (const void *p1_p, const void *p2_p)
14262 {
14263 const struct dwarf_file_data * p1 = p1_p;
14264 const char * p2 = p2_p;
14265 return strcmp (p1->filename, p2) == 0;
14266 }
14267
14268 static hashval_t
14269 file_table_hash (const void *p_p)
14270 {
14271 const struct dwarf_file_data * p = p_p;
14272 return htab_hash_string (p->filename);
14273 }
14274
14275 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14276 dwarf2out.c) and return its "index". The index of each (known) filename is
14277 just a unique number which is associated with only that one filename. We
14278 need such numbers for the sake of generating labels (in the .debug_sfnames
14279 section) and references to those files numbers (in the .debug_srcinfo
14280 and.debug_macinfo sections). If the filename given as an argument is not
14281 found in our current list, add it to the list and assign it the next
14282 available unique index number. In order to speed up searches, we remember
14283 the index of the filename was looked up last. This handles the majority of
14284 all searches. */
14285
14286 static struct dwarf_file_data *
14287 lookup_filename (const char *file_name)
14288 {
14289 void ** slot;
14290 struct dwarf_file_data * created;
14291
14292 /* Check to see if the file name that was searched on the previous
14293 call matches this file name. If so, return the index. */
14294 if (file_table_last_lookup
14295 && (file_name == file_table_last_lookup->filename
14296 || strcmp (file_table_last_lookup->filename, file_name) == 0))
14297 return file_table_last_lookup;
14298
14299 /* Didn't match the previous lookup, search the table. */
14300 slot = htab_find_slot_with_hash (file_table, file_name,
14301 htab_hash_string (file_name), INSERT);
14302 if (*slot)
14303 return *slot;
14304
14305 created = ggc_alloc (sizeof (struct dwarf_file_data));
14306 created->filename = file_name;
14307 created->emitted_number = 0;
14308 *slot = created;
14309 return created;
14310 }
14311
14312 /* If the assembler will construct the file table, then translate the compiler
14313 internal file table number into the assembler file table number, and emit
14314 a .file directive if we haven't already emitted one yet. The file table
14315 numbers are different because we prune debug info for unused variables and
14316 types, which may include filenames. */
14317
14318 static int
14319 maybe_emit_file (struct dwarf_file_data * fd)
14320 {
14321 if (! fd->emitted_number)
14322 {
14323 if (last_emitted_file)
14324 fd->emitted_number = last_emitted_file->emitted_number + 1;
14325 else
14326 fd->emitted_number = 1;
14327 last_emitted_file = fd;
14328
14329 if (DWARF2_ASM_LINE_DEBUG_INFO)
14330 {
14331 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14332 output_quoted_string (asm_out_file,
14333 remap_debug_filename (fd->filename));
14334 fputc ('\n', asm_out_file);
14335 }
14336 }
14337
14338 return fd->emitted_number;
14339 }
14340
14341 /* Called by the final INSN scan whenever we see a var location. We
14342 use it to drop labels in the right places, and throw the location in
14343 our lookup table. */
14344
14345 static void
14346 dwarf2out_var_location (rtx loc_note)
14347 {
14348 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14349 struct var_loc_node *newloc;
14350 rtx prev_insn;
14351 static rtx last_insn;
14352 static const char *last_label;
14353 tree decl;
14354
14355 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14356 return;
14357 prev_insn = PREV_INSN (loc_note);
14358
14359 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14360 /* If the insn we processed last time is the previous insn
14361 and it is also a var location note, use the label we emitted
14362 last time. */
14363 if (last_insn != NULL_RTX
14364 && last_insn == prev_insn
14365 && NOTE_P (prev_insn)
14366 && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14367 {
14368 newloc->label = last_label;
14369 }
14370 else
14371 {
14372 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14373 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14374 loclabel_num++;
14375 newloc->label = ggc_strdup (loclabel);
14376 }
14377 newloc->var_loc_note = loc_note;
14378 newloc->next = NULL;
14379
14380 if (cfun && in_cold_section_p)
14381 newloc->section_label = cfun->cold_section_label;
14382 else
14383 newloc->section_label = text_section_label;
14384
14385 last_insn = loc_note;
14386 last_label = newloc->label;
14387 decl = NOTE_VAR_LOCATION_DECL (loc_note);
14388 add_var_loc_to_decl (decl, newloc);
14389 }
14390
14391 /* We need to reset the locations at the beginning of each
14392 function. We can't do this in the end_function hook, because the
14393 declarations that use the locations won't have been output when
14394 that hook is called. Also compute have_multiple_function_sections here. */
14395
14396 static void
14397 dwarf2out_begin_function (tree fun)
14398 {
14399 htab_empty (decl_loc_table);
14400
14401 if (function_section (fun) != text_section)
14402 have_multiple_function_sections = true;
14403
14404 dwarf2out_note_section_used ();
14405 }
14406
14407 /* Output a label to mark the beginning of a source code line entry
14408 and record information relating to this source line, in
14409 'line_info_table' for later output of the .debug_line section. */
14410
14411 static void
14412 dwarf2out_source_line (unsigned int line, const char *filename)
14413 {
14414 if (debug_info_level >= DINFO_LEVEL_NORMAL
14415 && line != 0)
14416 {
14417 int file_num = maybe_emit_file (lookup_filename (filename));
14418
14419 switch_to_section (current_function_section ());
14420
14421 /* If requested, emit something human-readable. */
14422 if (flag_debug_asm)
14423 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14424 filename, line);
14425
14426 if (DWARF2_ASM_LINE_DEBUG_INFO)
14427 {
14428 /* Emit the .loc directive understood by GNU as. */
14429 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14430
14431 /* Indicate that line number info exists. */
14432 line_info_table_in_use++;
14433 }
14434 else if (function_section (current_function_decl) != text_section)
14435 {
14436 dw_separate_line_info_ref line_info;
14437 targetm.asm_out.internal_label (asm_out_file,
14438 SEPARATE_LINE_CODE_LABEL,
14439 separate_line_info_table_in_use);
14440
14441 /* Expand the line info table if necessary. */
14442 if (separate_line_info_table_in_use
14443 == separate_line_info_table_allocated)
14444 {
14445 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14446 separate_line_info_table
14447 = ggc_realloc (separate_line_info_table,
14448 separate_line_info_table_allocated
14449 * sizeof (dw_separate_line_info_entry));
14450 memset (separate_line_info_table
14451 + separate_line_info_table_in_use,
14452 0,
14453 (LINE_INFO_TABLE_INCREMENT
14454 * sizeof (dw_separate_line_info_entry)));
14455 }
14456
14457 /* Add the new entry at the end of the line_info_table. */
14458 line_info
14459 = &separate_line_info_table[separate_line_info_table_in_use++];
14460 line_info->dw_file_num = file_num;
14461 line_info->dw_line_num = line;
14462 line_info->function = current_function_funcdef_no;
14463 }
14464 else
14465 {
14466 dw_line_info_ref line_info;
14467
14468 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14469 line_info_table_in_use);
14470
14471 /* Expand the line info table if necessary. */
14472 if (line_info_table_in_use == line_info_table_allocated)
14473 {
14474 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14475 line_info_table
14476 = ggc_realloc (line_info_table,
14477 (line_info_table_allocated
14478 * sizeof (dw_line_info_entry)));
14479 memset (line_info_table + line_info_table_in_use, 0,
14480 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14481 }
14482
14483 /* Add the new entry at the end of the line_info_table. */
14484 line_info = &line_info_table[line_info_table_in_use++];
14485 line_info->dw_file_num = file_num;
14486 line_info->dw_line_num = line;
14487 }
14488 }
14489 }
14490
14491 /* Record the beginning of a new source file. */
14492
14493 static void
14494 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14495 {
14496 if (flag_eliminate_dwarf2_dups)
14497 {
14498 /* Record the beginning of the file for break_out_includes. */
14499 dw_die_ref bincl_die;
14500
14501 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14502 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14503 }
14504
14505 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14506 {
14507 int file_num = maybe_emit_file (lookup_filename (filename));
14508
14509 switch_to_section (debug_macinfo_section);
14510 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14511 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14512 lineno);
14513
14514 dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14515 }
14516 }
14517
14518 /* Record the end of a source file. */
14519
14520 static void
14521 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14522 {
14523 if (flag_eliminate_dwarf2_dups)
14524 /* Record the end of the file for break_out_includes. */
14525 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14526
14527 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14528 {
14529 switch_to_section (debug_macinfo_section);
14530 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14531 }
14532 }
14533
14534 /* Called from debug_define in toplev.c. The `buffer' parameter contains
14535 the tail part of the directive line, i.e. the part which is past the
14536 initial whitespace, #, whitespace, directive-name, whitespace part. */
14537
14538 static void
14539 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14540 const char *buffer ATTRIBUTE_UNUSED)
14541 {
14542 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14543 {
14544 switch_to_section (debug_macinfo_section);
14545 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14546 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14547 dw2_asm_output_nstring (buffer, -1, "The macro");
14548 }
14549 }
14550
14551 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
14552 the tail part of the directive line, i.e. the part which is past the
14553 initial whitespace, #, whitespace, directive-name, whitespace part. */
14554
14555 static void
14556 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14557 const char *buffer ATTRIBUTE_UNUSED)
14558 {
14559 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14560 {
14561 switch_to_section (debug_macinfo_section);
14562 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14563 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14564 dw2_asm_output_nstring (buffer, -1, "The macro");
14565 }
14566 }
14567
14568 /* Set up for Dwarf output at the start of compilation. */
14569
14570 static void
14571 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14572 {
14573 /* Allocate the file_table. */
14574 file_table = htab_create_ggc (50, file_table_hash,
14575 file_table_eq, NULL);
14576
14577 /* Allocate the decl_die_table. */
14578 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14579 decl_die_table_eq, NULL);
14580
14581 /* Allocate the decl_loc_table. */
14582 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14583 decl_loc_table_eq, NULL);
14584
14585 /* Allocate the initial hunk of the decl_scope_table. */
14586 decl_scope_table = VEC_alloc (tree, gc, 256);
14587
14588 /* Allocate the initial hunk of the abbrev_die_table. */
14589 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14590 * sizeof (dw_die_ref));
14591 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14592 /* Zero-th entry is allocated, but unused. */
14593 abbrev_die_table_in_use = 1;
14594
14595 /* Allocate the initial hunk of the line_info_table. */
14596 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14597 * sizeof (dw_line_info_entry));
14598 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14599
14600 /* Zero-th entry is allocated, but unused. */
14601 line_info_table_in_use = 1;
14602
14603 /* Allocate the pubtypes and pubnames vectors. */
14604 pubname_table = VEC_alloc (pubname_entry, gc, 32);
14605 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14606
14607 /* Generate the initial DIE for the .debug section. Note that the (string)
14608 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14609 will (typically) be a relative pathname and that this pathname should be
14610 taken as being relative to the directory from which the compiler was
14611 invoked when the given (base) source file was compiled. We will fill
14612 in this value in dwarf2out_finish. */
14613 comp_unit_die = gen_compile_unit_die (NULL);
14614
14615 incomplete_types = VEC_alloc (tree, gc, 64);
14616
14617 used_rtx_array = VEC_alloc (rtx, gc, 32);
14618
14619 debug_info_section = get_section (DEBUG_INFO_SECTION,
14620 SECTION_DEBUG, NULL);
14621 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14622 SECTION_DEBUG, NULL);
14623 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14624 SECTION_DEBUG, NULL);
14625 debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14626 SECTION_DEBUG, NULL);
14627 debug_line_section = get_section (DEBUG_LINE_SECTION,
14628 SECTION_DEBUG, NULL);
14629 debug_loc_section = get_section (DEBUG_LOC_SECTION,
14630 SECTION_DEBUG, NULL);
14631 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14632 SECTION_DEBUG, NULL);
14633 #ifdef DEBUG_PUBTYPES_SECTION
14634 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14635 SECTION_DEBUG, NULL);
14636 #endif
14637 debug_str_section = get_section (DEBUG_STR_SECTION,
14638 DEBUG_STR_SECTION_FLAGS, NULL);
14639 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14640 SECTION_DEBUG, NULL);
14641 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14642 SECTION_DEBUG, NULL);
14643
14644 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14645 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14646 DEBUG_ABBREV_SECTION_LABEL, 0);
14647 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14648 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14649 COLD_TEXT_SECTION_LABEL, 0);
14650 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14651
14652 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14653 DEBUG_INFO_SECTION_LABEL, 0);
14654 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14655 DEBUG_LINE_SECTION_LABEL, 0);
14656 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14657 DEBUG_RANGES_SECTION_LABEL, 0);
14658 switch_to_section (debug_abbrev_section);
14659 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14660 switch_to_section (debug_info_section);
14661 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14662 switch_to_section (debug_line_section);
14663 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14664
14665 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14666 {
14667 switch_to_section (debug_macinfo_section);
14668 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14669 DEBUG_MACINFO_SECTION_LABEL, 0);
14670 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14671 }
14672
14673 switch_to_section (text_section);
14674 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14675 if (flag_reorder_blocks_and_partition)
14676 {
14677 cold_text_section = unlikely_text_section ();
14678 switch_to_section (cold_text_section);
14679 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14680 }
14681 }
14682
14683 /* A helper function for dwarf2out_finish called through
14684 ht_forall. Emit one queued .debug_str string. */
14685
14686 static int
14687 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14688 {
14689 struct indirect_string_node *node = (struct indirect_string_node *) *h;
14690
14691 if (node->form == DW_FORM_strp)
14692 {
14693 switch_to_section (debug_str_section);
14694 ASM_OUTPUT_LABEL (asm_out_file, node->label);
14695 assemble_string (node->str, strlen (node->str) + 1);
14696 }
14697
14698 return 1;
14699 }
14700
14701 #if ENABLE_ASSERT_CHECKING
14702 /* Verify that all marks are clear. */
14703
14704 static void
14705 verify_marks_clear (dw_die_ref die)
14706 {
14707 dw_die_ref c;
14708
14709 gcc_assert (! die->die_mark);
14710 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14711 }
14712 #endif /* ENABLE_ASSERT_CHECKING */
14713
14714 /* Clear the marks for a die and its children.
14715 Be cool if the mark isn't set. */
14716
14717 static void
14718 prune_unmark_dies (dw_die_ref die)
14719 {
14720 dw_die_ref c;
14721
14722 if (die->die_mark)
14723 die->die_mark = 0;
14724 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14725 }
14726
14727 /* Given DIE that we're marking as used, find any other dies
14728 it references as attributes and mark them as used. */
14729
14730 static void
14731 prune_unused_types_walk_attribs (dw_die_ref die)
14732 {
14733 dw_attr_ref a;
14734 unsigned ix;
14735
14736 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14737 {
14738 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14739 {
14740 /* A reference to another DIE.
14741 Make sure that it will get emitted. */
14742 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14743 }
14744 /* Set the string's refcount to 0 so that prune_unused_types_mark
14745 accounts properly for it. */
14746 if (AT_class (a) == dw_val_class_str)
14747 a->dw_attr_val.v.val_str->refcount = 0;
14748 }
14749 }
14750
14751
14752 /* Mark DIE as being used. If DOKIDS is true, then walk down
14753 to DIE's children. */
14754
14755 static void
14756 prune_unused_types_mark (dw_die_ref die, int dokids)
14757 {
14758 dw_die_ref c;
14759
14760 if (die->die_mark == 0)
14761 {
14762 /* We haven't done this node yet. Mark it as used. */
14763 die->die_mark = 1;
14764
14765 /* We also have to mark its parents as used.
14766 (But we don't want to mark our parents' kids due to this.) */
14767 if (die->die_parent)
14768 prune_unused_types_mark (die->die_parent, 0);
14769
14770 /* Mark any referenced nodes. */
14771 prune_unused_types_walk_attribs (die);
14772
14773 /* If this node is a specification,
14774 also mark the definition, if it exists. */
14775 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14776 prune_unused_types_mark (die->die_definition, 1);
14777 }
14778
14779 if (dokids && die->die_mark != 2)
14780 {
14781 /* We need to walk the children, but haven't done so yet.
14782 Remember that we've walked the kids. */
14783 die->die_mark = 2;
14784
14785 /* If this is an array type, we need to make sure our
14786 kids get marked, even if they're types. */
14787 if (die->die_tag == DW_TAG_array_type)
14788 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14789 else
14790 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14791 }
14792 }
14793
14794
14795 /* Walk the tree DIE and mark types that we actually use. */
14796
14797 static void
14798 prune_unused_types_walk (dw_die_ref die)
14799 {
14800 dw_die_ref c;
14801
14802 /* Don't do anything if this node is already marked. */
14803 if (die->die_mark)
14804 return;
14805
14806 switch (die->die_tag)
14807 {
14808 case DW_TAG_const_type:
14809 case DW_TAG_packed_type:
14810 case DW_TAG_pointer_type:
14811 case DW_TAG_reference_type:
14812 case DW_TAG_volatile_type:
14813 case DW_TAG_typedef:
14814 case DW_TAG_array_type:
14815 case DW_TAG_structure_type:
14816 case DW_TAG_union_type:
14817 case DW_TAG_class_type:
14818 case DW_TAG_interface_type:
14819 case DW_TAG_friend:
14820 case DW_TAG_variant_part:
14821 case DW_TAG_enumeration_type:
14822 case DW_TAG_subroutine_type:
14823 case DW_TAG_string_type:
14824 case DW_TAG_set_type:
14825 case DW_TAG_subrange_type:
14826 case DW_TAG_ptr_to_member_type:
14827 case DW_TAG_file_type:
14828 if (die->die_perennial_p)
14829 break;
14830
14831 /* It's a type node --- don't mark it. */
14832 return;
14833
14834 default:
14835 /* Mark everything else. */
14836 break;
14837 }
14838
14839 die->die_mark = 1;
14840
14841 /* Now, mark any dies referenced from here. */
14842 prune_unused_types_walk_attribs (die);
14843
14844 /* Mark children. */
14845 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14846 }
14847
14848 /* Increment the string counts on strings referred to from DIE's
14849 attributes. */
14850
14851 static void
14852 prune_unused_types_update_strings (dw_die_ref die)
14853 {
14854 dw_attr_ref a;
14855 unsigned ix;
14856
14857 for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14858 if (AT_class (a) == dw_val_class_str)
14859 {
14860 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14861 s->refcount++;
14862 /* Avoid unnecessarily putting strings that are used less than
14863 twice in the hash table. */
14864 if (s->refcount
14865 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14866 {
14867 void ** slot;
14868 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14869 htab_hash_string (s->str),
14870 INSERT);
14871 gcc_assert (*slot == NULL);
14872 *slot = s;
14873 }
14874 }
14875 }
14876
14877 /* Remove from the tree DIE any dies that aren't marked. */
14878
14879 static void
14880 prune_unused_types_prune (dw_die_ref die)
14881 {
14882 dw_die_ref c;
14883
14884 gcc_assert (die->die_mark);
14885 prune_unused_types_update_strings (die);
14886
14887 if (! die->die_child)
14888 return;
14889
14890 c = die->die_child;
14891 do {
14892 dw_die_ref prev = c;
14893 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14894 if (c == die->die_child)
14895 {
14896 /* No marked children between 'prev' and the end of the list. */
14897 if (prev == c)
14898 /* No marked children at all. */
14899 die->die_child = NULL;
14900 else
14901 {
14902 prev->die_sib = c->die_sib;
14903 die->die_child = prev;
14904 }
14905 return;
14906 }
14907
14908 if (c != prev->die_sib)
14909 prev->die_sib = c;
14910 prune_unused_types_prune (c);
14911 } while (c != die->die_child);
14912 }
14913
14914
14915 /* Remove dies representing declarations that we never use. */
14916
14917 static void
14918 prune_unused_types (void)
14919 {
14920 unsigned int i;
14921 limbo_die_node *node;
14922 pubname_ref pub;
14923
14924 #if ENABLE_ASSERT_CHECKING
14925 /* All the marks should already be clear. */
14926 verify_marks_clear (comp_unit_die);
14927 for (node = limbo_die_list; node; node = node->next)
14928 verify_marks_clear (node->die);
14929 #endif /* ENABLE_ASSERT_CHECKING */
14930
14931 /* Set the mark on nodes that are actually used. */
14932 prune_unused_types_walk (comp_unit_die);
14933 for (node = limbo_die_list; node; node = node->next)
14934 prune_unused_types_walk (node->die);
14935
14936 /* Also set the mark on nodes referenced from the
14937 pubname_table or arange_table. */
14938 for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14939 prune_unused_types_mark (pub->die, 1);
14940 for (i = 0; i < arange_table_in_use; i++)
14941 prune_unused_types_mark (arange_table[i], 1);
14942
14943 /* Get rid of nodes that aren't marked; and update the string counts. */
14944 if (debug_str_hash)
14945 htab_empty (debug_str_hash);
14946 prune_unused_types_prune (comp_unit_die);
14947 for (node = limbo_die_list; node; node = node->next)
14948 prune_unused_types_prune (node->die);
14949
14950 /* Leave the marks clear. */
14951 prune_unmark_dies (comp_unit_die);
14952 for (node = limbo_die_list; node; node = node->next)
14953 prune_unmark_dies (node->die);
14954 }
14955
14956 /* Set the parameter to true if there are any relative pathnames in
14957 the file table. */
14958 static int
14959 file_table_relative_p (void ** slot, void *param)
14960 {
14961 bool *p = param;
14962 struct dwarf_file_data *d = *slot;
14963 if (!IS_ABSOLUTE_PATH (d->filename))
14964 {
14965 *p = true;
14966 return 0;
14967 }
14968 return 1;
14969 }
14970
14971 /* Output stuff that dwarf requires at the end of every file,
14972 and generate the DWARF-2 debugging info. */
14973
14974 static void
14975 dwarf2out_finish (const char *filename)
14976 {
14977 limbo_die_node *node, *next_node;
14978 dw_die_ref die = 0;
14979
14980 /* Add the name for the main input file now. We delayed this from
14981 dwarf2out_init to avoid complications with PCH. */
14982 add_name_attribute (comp_unit_die, remap_debug_filename (filename));
14983 if (!IS_ABSOLUTE_PATH (filename))
14984 add_comp_dir_attribute (comp_unit_die);
14985 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14986 {
14987 bool p = false;
14988 htab_traverse (file_table, file_table_relative_p, &p);
14989 if (p)
14990 add_comp_dir_attribute (comp_unit_die);
14991 }
14992
14993 /* Traverse the limbo die list, and add parent/child links. The only
14994 dies without parents that should be here are concrete instances of
14995 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
14996 For concrete instances, we can get the parent die from the abstract
14997 instance. */
14998 for (node = limbo_die_list; node; node = next_node)
14999 {
15000 next_node = node->next;
15001 die = node->die;
15002
15003 if (die->die_parent == NULL)
15004 {
15005 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15006
15007 if (origin)
15008 add_child_die (origin->die_parent, die);
15009 else if (die == comp_unit_die)
15010 ;
15011 else if (errorcount > 0 || sorrycount > 0)
15012 /* It's OK to be confused by errors in the input. */
15013 add_child_die (comp_unit_die, die);
15014 else
15015 {
15016 /* In certain situations, the lexical block containing a
15017 nested function can be optimized away, which results
15018 in the nested function die being orphaned. Likewise
15019 with the return type of that nested function. Force
15020 this to be a child of the containing function.
15021
15022 It may happen that even the containing function got fully
15023 inlined and optimized out. In that case we are lost and
15024 assign the empty child. This should not be big issue as
15025 the function is likely unreachable too. */
15026 tree context = NULL_TREE;
15027
15028 gcc_assert (node->created_for);
15029
15030 if (DECL_P (node->created_for))
15031 context = DECL_CONTEXT (node->created_for);
15032 else if (TYPE_P (node->created_for))
15033 context = TYPE_CONTEXT (node->created_for);
15034
15035 gcc_assert (context
15036 && (TREE_CODE (context) == FUNCTION_DECL
15037 || TREE_CODE (context) == NAMESPACE_DECL));
15038
15039 origin = lookup_decl_die (context);
15040 if (origin)
15041 add_child_die (origin, die);
15042 else
15043 add_child_die (comp_unit_die, die);
15044 }
15045 }
15046 }
15047
15048 limbo_die_list = NULL;
15049
15050 /* Walk through the list of incomplete types again, trying once more to
15051 emit full debugging info for them. */
15052 retry_incomplete_types ();
15053
15054 if (flag_eliminate_unused_debug_types)
15055 prune_unused_types ();
15056
15057 /* Generate separate CUs for each of the include files we've seen.
15058 They will go into limbo_die_list. */
15059 if (flag_eliminate_dwarf2_dups)
15060 break_out_includes (comp_unit_die);
15061
15062 /* Traverse the DIE's and add add sibling attributes to those DIE's
15063 that have children. */
15064 add_sibling_attributes (comp_unit_die);
15065 for (node = limbo_die_list; node; node = node->next)
15066 add_sibling_attributes (node->die);
15067
15068 /* Output a terminator label for the .text section. */
15069 switch_to_section (text_section);
15070 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15071 if (flag_reorder_blocks_and_partition)
15072 {
15073 switch_to_section (unlikely_text_section ());
15074 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15075 }
15076
15077 /* We can only use the low/high_pc attributes if all of the code was
15078 in .text. */
15079 if (!have_multiple_function_sections)
15080 {
15081 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15082 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15083 }
15084
15085 else
15086 {
15087 unsigned fde_idx = 0;
15088
15089 /* We need to give .debug_loc and .debug_ranges an appropriate
15090 "base address". Use zero so that these addresses become
15091 absolute. Historically, we've emitted the unexpected
15092 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15093 Emit both to give time for other tools to adapt. */
15094 add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15095 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15096
15097 add_AT_range_list (comp_unit_die, DW_AT_ranges,
15098 add_ranges_by_labels (text_section_label,
15099 text_end_label));
15100 if (flag_reorder_blocks_and_partition)
15101 add_ranges_by_labels (cold_text_section_label,
15102 cold_end_label);
15103
15104 for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15105 {
15106 dw_fde_ref fde = &fde_table[fde_idx];
15107
15108 if (fde->dw_fde_switched_sections)
15109 {
15110 add_ranges_by_labels (fde->dw_fde_hot_section_label,
15111 fde->dw_fde_hot_section_end_label);
15112 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15113 fde->dw_fde_unlikely_section_end_label);
15114 }
15115 else
15116 add_ranges_by_labels (fde->dw_fde_begin,
15117 fde->dw_fde_end);
15118 }
15119
15120 add_ranges (NULL);
15121 }
15122
15123 /* Output location list section if necessary. */
15124 if (have_location_lists)
15125 {
15126 /* Output the location lists info. */
15127 switch_to_section (debug_loc_section);
15128 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15129 DEBUG_LOC_SECTION_LABEL, 0);
15130 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15131 output_location_lists (die);
15132 }
15133
15134 if (debug_info_level >= DINFO_LEVEL_NORMAL)
15135 add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15136 debug_line_section_label);
15137
15138 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15139 add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15140
15141 /* Output all of the compilation units. We put the main one last so that
15142 the offsets are available to output_pubnames. */
15143 for (node = limbo_die_list; node; node = node->next)
15144 output_comp_unit (node->die, 0);
15145
15146 output_comp_unit (comp_unit_die, 0);
15147
15148 /* Output the abbreviation table. */
15149 switch_to_section (debug_abbrev_section);
15150 output_abbrev_section ();
15151
15152 /* Output public names table if necessary. */
15153 if (!VEC_empty (pubname_entry, pubname_table))
15154 {
15155 switch_to_section (debug_pubnames_section);
15156 output_pubnames (pubname_table);
15157 }
15158
15159 #ifdef DEBUG_PUBTYPES_SECTION
15160 /* Output public types table if necessary. */
15161 if (!VEC_empty (pubname_entry, pubtype_table))
15162 {
15163 switch_to_section (debug_pubtypes_section);
15164 output_pubnames (pubtype_table);
15165 }
15166 #endif
15167
15168 /* Output the address range information. We only put functions in the arange
15169 table, so don't write it out if we don't have any. */
15170 if (fde_table_in_use)
15171 {
15172 switch_to_section (debug_aranges_section);
15173 output_aranges ();
15174 }
15175
15176 /* Output ranges section if necessary. */
15177 if (ranges_table_in_use)
15178 {
15179 switch_to_section (debug_ranges_section);
15180 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15181 output_ranges ();
15182 }
15183
15184 /* Output the source line correspondence table. We must do this
15185 even if there is no line information. Otherwise, on an empty
15186 translation unit, we will generate a present, but empty,
15187 .debug_info section. IRIX 6.5 `nm' will then complain when
15188 examining the file. This is done late so that any filenames
15189 used by the debug_info section are marked as 'used'. */
15190 if (! DWARF2_ASM_LINE_DEBUG_INFO)
15191 {
15192 switch_to_section (debug_line_section);
15193 output_line_info ();
15194 }
15195
15196 /* Have to end the macro section. */
15197 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15198 {
15199 switch_to_section (debug_macinfo_section);
15200 dw2_asm_output_data (1, 0, "End compilation unit");
15201 }
15202
15203 /* If we emitted any DW_FORM_strp form attribute, output the string
15204 table too. */
15205 if (debug_str_hash)
15206 htab_traverse (debug_str_hash, output_indirect_string, NULL);
15207 }
15208 #else
15209
15210 /* This should never be used, but its address is needed for comparisons. */
15211 const struct gcc_debug_hooks dwarf2_debug_hooks;
15212
15213 #endif /* DWARF2_DEBUGGING_INFO */
15214
15215 #include "gt-dwarf2out.h"