]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/dwarf2out.c
* dbxout.c (dbxout_type, dbxout_type_name, dbxout_symbol): Use
[thirdparty/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 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 2, 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 COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA. */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
36
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
70
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
74
75 /* DWARF2 Abbreviation Glossary:
76 CFA = Canonical Frame Address
77 a fixed address on the stack which identifies a call frame.
78 We define it to be the value of SP just before the call insn.
79 The CFA register and offset, which may change during the course
80 of the function, are used to calculate its value at runtime.
81 CFI = Call Frame Instruction
82 an instruction for the DWARF2 abstract machine
83 CIE = Common Information Entry
84 information describing information common to one or more FDEs
85 DIE = Debugging Information Entry
86 FDE = Frame Description Entry
87 information describing the stack call frame, in particular,
88 how to restore registers
89
90 DW_CFA_... = DWARF2 CFA call frame instruction
91 DW_TAG_... = DWARF2 DIE tag */
92
93 /* Decide whether we want to emit frame unwind information for the current
94 translation unit. */
95
96 int
97 dwarf2out_do_frame (void)
98 {
99 return (write_symbols == DWARF2_DEBUG
100 || write_symbols == VMS_AND_DWARF2_DEBUG
101 #ifdef DWARF2_FRAME_INFO
102 || DWARF2_FRAME_INFO
103 #endif
104 #ifdef DWARF2_UNWIND_INFO
105 || flag_unwind_tables
106 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)
107 #endif
108 );
109 }
110
111 /* The size of the target's pointer type. */
112 #ifndef PTR_SIZE
113 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
114 #endif
115
116 /* Various versions of targetm.eh_frame_section. Note these must appear
117 outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro guards. */
118
119 /* Version of targetm.eh_frame_section for systems with named sections. */
120 void
121 named_section_eh_frame_section (void)
122 {
123 #ifdef EH_FRAME_SECTION_NAME
124 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
125 int fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
126 int per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
127 int lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
128 int flags;
129
130 flags = (! flag_pic
131 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
132 && (fde_encoding & 0x70) != DW_EH_PE_aligned
133 && (per_encoding & 0x70) != DW_EH_PE_absptr
134 && (per_encoding & 0x70) != DW_EH_PE_aligned
135 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
136 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
137 ? 0 : SECTION_WRITE;
138 named_section_flags (EH_FRAME_SECTION_NAME, flags);
139 #else
140 named_section_flags (EH_FRAME_SECTION_NAME, SECTION_WRITE);
141 #endif
142 #endif
143 }
144
145 /* Version of targetm.eh_frame_section for systems using collect2. */
146 void
147 collect2_eh_frame_section (void)
148 {
149 tree label = get_file_function_name ('F');
150
151 data_section ();
152 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
153 targetm.asm_out.globalize_label (asm_out_file, IDENTIFIER_POINTER (label));
154 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
155 }
156
157 /* Default version of targetm.eh_frame_section. */
158 void
159 default_eh_frame_section (void)
160 {
161 #ifdef EH_FRAME_SECTION_NAME
162 named_section_eh_frame_section ();
163 #else
164 collect2_eh_frame_section ();
165 #endif
166 }
167
168 /* Array of RTXes referenced by the debugging information, which therefore
169 must be kept around forever. */
170 static GTY(()) varray_type used_rtx_varray;
171
172 /* A pointer to the base of a list of incomplete types which might be
173 completed at some later time. incomplete_types_list needs to be a VARRAY
174 because we want to tell the garbage collector about it. */
175 static GTY(()) varray_type incomplete_types;
176
177 /* A pointer to the base of a table of references to declaration
178 scopes. This table is a display which tracks the nesting
179 of declaration scopes at the current scope and containing
180 scopes. This table is used to find the proper place to
181 define type declaration DIE's. */
182 static GTY(()) varray_type decl_scope_table;
183
184 /* How to start an assembler comment. */
185 #ifndef ASM_COMMENT_START
186 #define ASM_COMMENT_START ";#"
187 #endif
188
189 typedef struct dw_cfi_struct *dw_cfi_ref;
190 typedef struct dw_fde_struct *dw_fde_ref;
191 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
192
193 /* Call frames are described using a sequence of Call Frame
194 Information instructions. The register number, offset
195 and address fields are provided as possible operands;
196 their use is selected by the opcode field. */
197
198 enum dw_cfi_oprnd_type {
199 dw_cfi_oprnd_unused,
200 dw_cfi_oprnd_reg_num,
201 dw_cfi_oprnd_offset,
202 dw_cfi_oprnd_addr,
203 dw_cfi_oprnd_loc
204 };
205
206 typedef union dw_cfi_oprnd_struct GTY(())
207 {
208 unsigned long GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
209 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
210 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
211 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
212 }
213 dw_cfi_oprnd;
214
215 typedef struct dw_cfi_struct GTY(())
216 {
217 dw_cfi_ref dw_cfi_next;
218 enum dwarf_call_frame_info dw_cfi_opc;
219 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
220 dw_cfi_oprnd1;
221 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
222 dw_cfi_oprnd2;
223 }
224 dw_cfi_node;
225
226 /* This is how we define the location of the CFA. We use to handle it
227 as REG + OFFSET all the time, but now it can be more complex.
228 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
229 Instead of passing around REG and OFFSET, we pass a copy
230 of this structure. */
231 typedef struct cfa_loc GTY(())
232 {
233 unsigned long reg;
234 HOST_WIDE_INT offset;
235 HOST_WIDE_INT base_offset;
236 int indirect; /* 1 if CFA is accessed via a dereference. */
237 } dw_cfa_location;
238
239 /* All call frame descriptions (FDE's) in the GCC generated DWARF
240 refer to a single Common Information Entry (CIE), defined at
241 the beginning of the .debug_frame section. This use of a single
242 CIE obviates the need to keep track of multiple CIE's
243 in the DWARF generation routines below. */
244
245 typedef struct dw_fde_struct GTY(())
246 {
247 tree decl;
248 const char *dw_fde_begin;
249 const char *dw_fde_current_label;
250 const char *dw_fde_end;
251 dw_cfi_ref dw_fde_cfi;
252 unsigned funcdef_number;
253 unsigned all_throwers_are_sibcalls : 1;
254 unsigned nothrow : 1;
255 unsigned uses_eh_lsda : 1;
256 }
257 dw_fde_node;
258
259 /* Maximum size (in bytes) of an artificially generated label. */
260 #define MAX_ARTIFICIAL_LABEL_BYTES 30
261
262 /* The size of addresses as they appear in the Dwarf 2 data.
263 Some architectures use word addresses to refer to code locations,
264 but Dwarf 2 info always uses byte addresses. On such machines,
265 Dwarf 2 addresses need to be larger than the architecture's
266 pointers. */
267 #ifndef DWARF2_ADDR_SIZE
268 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
269 #endif
270
271 /* The size in bytes of a DWARF field indicating an offset or length
272 relative to a debug info section, specified to be 4 bytes in the
273 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
274 as PTR_SIZE. */
275
276 #ifndef DWARF_OFFSET_SIZE
277 #define DWARF_OFFSET_SIZE 4
278 #endif
279
280 /* According to the (draft) DWARF 3 specification, the initial length
281 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
282 bytes are 0xffffffff, followed by the length stored in the next 8
283 bytes.
284
285 However, the SGI/MIPS ABI uses an initial length which is equal to
286 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
287
288 #ifndef DWARF_INITIAL_LENGTH_SIZE
289 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
290 #endif
291
292 #define DWARF_VERSION 2
293
294 /* Round SIZE up to the nearest BOUNDARY. */
295 #define DWARF_ROUND(SIZE,BOUNDARY) \
296 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
297
298 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
299 #ifndef DWARF_CIE_DATA_ALIGNMENT
300 #ifdef STACK_GROWS_DOWNWARD
301 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
302 #else
303 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
304 #endif
305 #endif
306
307 /* A pointer to the base of a table that contains frame description
308 information for each routine. */
309 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
310
311 /* Number of elements currently allocated for fde_table. */
312 static GTY(()) unsigned fde_table_allocated;
313
314 /* Number of elements in fde_table currently in use. */
315 static GTY(()) unsigned fde_table_in_use;
316
317 /* Size (in elements) of increments by which we may expand the
318 fde_table. */
319 #define FDE_TABLE_INCREMENT 256
320
321 /* A list of call frame insns for the CIE. */
322 static GTY(()) dw_cfi_ref cie_cfi_head;
323
324 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
325 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
326 attribute that accelerates the lookup of the FDE associated
327 with the subprogram. This variable holds the table index of the FDE
328 associated with the current function (body) definition. */
329 static unsigned current_funcdef_fde;
330 #endif
331
332 struct indirect_string_node GTY(())
333 {
334 const char *str;
335 unsigned int refcount;
336 unsigned int form;
337 char *label;
338 };
339
340 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
341
342 static GTY(()) int dw2_string_counter;
343 static GTY(()) unsigned long dwarf2out_cfi_label_num;
344
345 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
346
347 /* Forward declarations for functions defined in this file. */
348
349 static char *stripattributes (const char *);
350 static const char *dwarf_cfi_name (unsigned);
351 static dw_cfi_ref new_cfi (void);
352 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
353 static void add_fde_cfi (const char *, dw_cfi_ref);
354 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
355 static void lookup_cfa (dw_cfa_location *);
356 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
357 static void initial_return_save (rtx);
358 static HOST_WIDE_INT stack_adjust_offset (rtx);
359 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
360 static void output_call_frame_info (int);
361 static void dwarf2out_stack_adjust (rtx);
362 static void flush_queued_reg_saves (void);
363 static bool clobbers_queued_reg_save (rtx);
364 static void dwarf2out_frame_debug_expr (rtx, const char *);
365
366 /* Support for complex CFA locations. */
367 static void output_cfa_loc (dw_cfi_ref);
368 static void get_cfa_from_loc_descr (dw_cfa_location *,
369 struct dw_loc_descr_struct *);
370 static struct dw_loc_descr_struct *build_cfa_loc
371 (dw_cfa_location *);
372 static void def_cfa_1 (const char *, dw_cfa_location *);
373
374 /* How to start an assembler comment. */
375 #ifndef ASM_COMMENT_START
376 #define ASM_COMMENT_START ";#"
377 #endif
378
379 /* Data and reference forms for relocatable data. */
380 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
381 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
382
383 #ifndef DEBUG_FRAME_SECTION
384 #define DEBUG_FRAME_SECTION ".debug_frame"
385 #endif
386
387 #ifndef FUNC_BEGIN_LABEL
388 #define FUNC_BEGIN_LABEL "LFB"
389 #endif
390
391 #ifndef FUNC_END_LABEL
392 #define FUNC_END_LABEL "LFE"
393 #endif
394
395 #ifndef FRAME_BEGIN_LABEL
396 #define FRAME_BEGIN_LABEL "Lframe"
397 #endif
398 #define CIE_AFTER_SIZE_LABEL "LSCIE"
399 #define CIE_END_LABEL "LECIE"
400 #define FDE_LABEL "LSFDE"
401 #define FDE_AFTER_SIZE_LABEL "LASFDE"
402 #define FDE_END_LABEL "LEFDE"
403 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
404 #define LINE_NUMBER_END_LABEL "LELT"
405 #define LN_PROLOG_AS_LABEL "LASLTP"
406 #define LN_PROLOG_END_LABEL "LELTP"
407 #define DIE_LABEL_PREFIX "DW"
408
409 /* The DWARF 2 CFA column which tracks the return address. Normally this
410 is the column for PC, or the first column after all of the hard
411 registers. */
412 #ifndef DWARF_FRAME_RETURN_COLUMN
413 #ifdef PC_REGNUM
414 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
415 #else
416 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
417 #endif
418 #endif
419
420 /* The mapping from gcc register number to DWARF 2 CFA column number. By
421 default, we just provide columns for all registers. */
422 #ifndef DWARF_FRAME_REGNUM
423 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
424 #endif
425
426 /* The offset from the incoming value of %sp to the top of the stack frame
427 for the current function. */
428 #ifndef INCOMING_FRAME_SP_OFFSET
429 #define INCOMING_FRAME_SP_OFFSET 0
430 #endif
431 \f
432 /* Hook used by __throw. */
433
434 rtx
435 expand_builtin_dwarf_sp_column (void)
436 {
437 return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
438 }
439
440 /* Return a pointer to a copy of the section string name S with all
441 attributes stripped off, and an asterisk prepended (for assemble_name). */
442
443 static inline char *
444 stripattributes (const char *s)
445 {
446 char *stripped = xmalloc (strlen (s) + 2);
447 char *p = stripped;
448
449 *p++ = '*';
450
451 while (*s && *s != ',')
452 *p++ = *s++;
453
454 *p = '\0';
455 return stripped;
456 }
457
458 /* Generate code to initialize the register size table. */
459
460 void
461 expand_builtin_init_dwarf_reg_sizes (tree address)
462 {
463 int i;
464 enum machine_mode mode = TYPE_MODE (char_type_node);
465 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
466 rtx mem = gen_rtx_MEM (BLKmode, addr);
467 bool wrote_return_column = false;
468
469 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
470 if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
471 {
472 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
473 enum machine_mode save_mode = reg_raw_mode[i];
474 HOST_WIDE_INT size;
475
476 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
477 save_mode = choose_hard_reg_mode (i, 1, true);
478 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
479 {
480 if (save_mode == VOIDmode)
481 continue;
482 wrote_return_column = true;
483 }
484 size = GET_MODE_SIZE (save_mode);
485 if (offset < 0)
486 continue;
487
488 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
489 }
490
491 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
492 gcc_assert (wrote_return_column);
493 i = DWARF_ALT_FRAME_RETURN_COLUMN;
494 wrote_return_column = false;
495 #else
496 i = DWARF_FRAME_RETURN_COLUMN;
497 #endif
498
499 if (! wrote_return_column)
500 {
501 enum machine_mode save_mode = Pmode;
502 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
503 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
504 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
505 }
506 }
507
508 /* Convert a DWARF call frame info. operation to its string name */
509
510 static const char *
511 dwarf_cfi_name (unsigned int cfi_opc)
512 {
513 switch (cfi_opc)
514 {
515 case DW_CFA_advance_loc:
516 return "DW_CFA_advance_loc";
517 case DW_CFA_offset:
518 return "DW_CFA_offset";
519 case DW_CFA_restore:
520 return "DW_CFA_restore";
521 case DW_CFA_nop:
522 return "DW_CFA_nop";
523 case DW_CFA_set_loc:
524 return "DW_CFA_set_loc";
525 case DW_CFA_advance_loc1:
526 return "DW_CFA_advance_loc1";
527 case DW_CFA_advance_loc2:
528 return "DW_CFA_advance_loc2";
529 case DW_CFA_advance_loc4:
530 return "DW_CFA_advance_loc4";
531 case DW_CFA_offset_extended:
532 return "DW_CFA_offset_extended";
533 case DW_CFA_restore_extended:
534 return "DW_CFA_restore_extended";
535 case DW_CFA_undefined:
536 return "DW_CFA_undefined";
537 case DW_CFA_same_value:
538 return "DW_CFA_same_value";
539 case DW_CFA_register:
540 return "DW_CFA_register";
541 case DW_CFA_remember_state:
542 return "DW_CFA_remember_state";
543 case DW_CFA_restore_state:
544 return "DW_CFA_restore_state";
545 case DW_CFA_def_cfa:
546 return "DW_CFA_def_cfa";
547 case DW_CFA_def_cfa_register:
548 return "DW_CFA_def_cfa_register";
549 case DW_CFA_def_cfa_offset:
550 return "DW_CFA_def_cfa_offset";
551
552 /* DWARF 3 */
553 case DW_CFA_def_cfa_expression:
554 return "DW_CFA_def_cfa_expression";
555 case DW_CFA_expression:
556 return "DW_CFA_expression";
557 case DW_CFA_offset_extended_sf:
558 return "DW_CFA_offset_extended_sf";
559 case DW_CFA_def_cfa_sf:
560 return "DW_CFA_def_cfa_sf";
561 case DW_CFA_def_cfa_offset_sf:
562 return "DW_CFA_def_cfa_offset_sf";
563
564 /* SGI/MIPS specific */
565 case DW_CFA_MIPS_advance_loc8:
566 return "DW_CFA_MIPS_advance_loc8";
567
568 /* GNU extensions */
569 case DW_CFA_GNU_window_save:
570 return "DW_CFA_GNU_window_save";
571 case DW_CFA_GNU_args_size:
572 return "DW_CFA_GNU_args_size";
573 case DW_CFA_GNU_negative_offset_extended:
574 return "DW_CFA_GNU_negative_offset_extended";
575
576 default:
577 return "DW_CFA_<unknown>";
578 }
579 }
580
581 /* Return a pointer to a newly allocated Call Frame Instruction. */
582
583 static inline dw_cfi_ref
584 new_cfi (void)
585 {
586 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
587
588 cfi->dw_cfi_next = NULL;
589 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
590 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
591
592 return cfi;
593 }
594
595 /* Add a Call Frame Instruction to list of instructions. */
596
597 static inline void
598 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
599 {
600 dw_cfi_ref *p;
601
602 /* Find the end of the chain. */
603 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
604 ;
605
606 *p = cfi;
607 }
608
609 /* Generate a new label for the CFI info to refer to. */
610
611 char *
612 dwarf2out_cfi_label (void)
613 {
614 static char label[20];
615
616 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
617 ASM_OUTPUT_LABEL (asm_out_file, label);
618 return label;
619 }
620
621 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
622 or to the CIE if LABEL is NULL. */
623
624 static void
625 add_fde_cfi (const char *label, dw_cfi_ref cfi)
626 {
627 if (label)
628 {
629 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
630
631 if (*label == 0)
632 label = dwarf2out_cfi_label ();
633
634 if (fde->dw_fde_current_label == NULL
635 || strcmp (label, fde->dw_fde_current_label) != 0)
636 {
637 dw_cfi_ref xcfi;
638
639 fde->dw_fde_current_label = label = xstrdup (label);
640
641 /* Set the location counter to the new label. */
642 xcfi = new_cfi ();
643 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
644 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
645 add_cfi (&fde->dw_fde_cfi, xcfi);
646 }
647
648 add_cfi (&fde->dw_fde_cfi, cfi);
649 }
650
651 else
652 add_cfi (&cie_cfi_head, cfi);
653 }
654
655 /* Subroutine of lookup_cfa. */
656
657 static inline void
658 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
659 {
660 switch (cfi->dw_cfi_opc)
661 {
662 case DW_CFA_def_cfa_offset:
663 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
664 break;
665 case DW_CFA_def_cfa_register:
666 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
667 break;
668 case DW_CFA_def_cfa:
669 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
670 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
671 break;
672 case DW_CFA_def_cfa_expression:
673 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
674 break;
675 default:
676 break;
677 }
678 }
679
680 /* Find the previous value for the CFA. */
681
682 static void
683 lookup_cfa (dw_cfa_location *loc)
684 {
685 dw_cfi_ref cfi;
686
687 loc->reg = (unsigned long) -1;
688 loc->offset = 0;
689 loc->indirect = 0;
690 loc->base_offset = 0;
691
692 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
693 lookup_cfa_1 (cfi, loc);
694
695 if (fde_table_in_use)
696 {
697 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
698 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
699 lookup_cfa_1 (cfi, loc);
700 }
701 }
702
703 /* The current rule for calculating the DWARF2 canonical frame address. */
704 static dw_cfa_location cfa;
705
706 /* The register used for saving registers to the stack, and its offset
707 from the CFA. */
708 static dw_cfa_location cfa_store;
709
710 /* The running total of the size of arguments pushed onto the stack. */
711 static HOST_WIDE_INT args_size;
712
713 /* The last args_size we actually output. */
714 static HOST_WIDE_INT old_args_size;
715
716 /* Entry point to update the canonical frame address (CFA).
717 LABEL is passed to add_fde_cfi. The value of CFA is now to be
718 calculated from REG+OFFSET. */
719
720 void
721 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
722 {
723 dw_cfa_location loc;
724 loc.indirect = 0;
725 loc.base_offset = 0;
726 loc.reg = reg;
727 loc.offset = offset;
728 def_cfa_1 (label, &loc);
729 }
730
731 /* This routine does the actual work. The CFA is now calculated from
732 the dw_cfa_location structure. */
733
734 static void
735 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
736 {
737 dw_cfi_ref cfi;
738 dw_cfa_location old_cfa, loc;
739
740 cfa = *loc_p;
741 loc = *loc_p;
742
743 if (cfa_store.reg == loc.reg && loc.indirect == 0)
744 cfa_store.offset = loc.offset;
745
746 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
747 lookup_cfa (&old_cfa);
748
749 /* If nothing changed, no need to issue any call frame instructions. */
750 if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
751 && loc.indirect == old_cfa.indirect
752 && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
753 return;
754
755 cfi = new_cfi ();
756
757 if (loc.reg == old_cfa.reg && !loc.indirect)
758 {
759 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
760 indicating the CFA register did not change but the offset
761 did. */
762 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
763 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
764 }
765
766 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
767 else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
768 && !loc.indirect)
769 {
770 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
771 indicating the CFA register has changed to <register> but the
772 offset has not changed. */
773 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
774 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
775 }
776 #endif
777
778 else if (loc.indirect == 0)
779 {
780 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
781 indicating the CFA register has changed to <register> with
782 the specified offset. */
783 cfi->dw_cfi_opc = DW_CFA_def_cfa;
784 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
785 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
786 }
787 else
788 {
789 /* Construct a DW_CFA_def_cfa_expression instruction to
790 calculate the CFA using a full location expression since no
791 register-offset pair is available. */
792 struct dw_loc_descr_struct *loc_list;
793
794 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
795 loc_list = build_cfa_loc (&loc);
796 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
797 }
798
799 add_fde_cfi (label, cfi);
800 }
801
802 /* Add the CFI for saving a register. REG is the CFA column number.
803 LABEL is passed to add_fde_cfi.
804 If SREG is -1, the register is saved at OFFSET from the CFA;
805 otherwise it is saved in SREG. */
806
807 static void
808 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
809 {
810 dw_cfi_ref cfi = new_cfi ();
811
812 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
813
814 if (sreg == INVALID_REGNUM)
815 {
816 if (reg & ~0x3f)
817 /* The register number won't fit in 6 bits, so we have to use
818 the long form. */
819 cfi->dw_cfi_opc = DW_CFA_offset_extended;
820 else
821 cfi->dw_cfi_opc = DW_CFA_offset;
822
823 #ifdef ENABLE_CHECKING
824 {
825 /* If we get an offset that is not a multiple of
826 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
827 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
828 description. */
829 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
830
831 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
832 }
833 #endif
834 offset /= DWARF_CIE_DATA_ALIGNMENT;
835 if (offset < 0)
836 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
837
838 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
839 }
840 else if (sreg == reg)
841 cfi->dw_cfi_opc = DW_CFA_same_value;
842 else
843 {
844 cfi->dw_cfi_opc = DW_CFA_register;
845 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
846 }
847
848 add_fde_cfi (label, cfi);
849 }
850
851 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
852 This CFI tells the unwinder that it needs to restore the window registers
853 from the previous frame's window save area.
854
855 ??? Perhaps we should note in the CIE where windows are saved (instead of
856 assuming 0(cfa)) and what registers are in the window. */
857
858 void
859 dwarf2out_window_save (const char *label)
860 {
861 dw_cfi_ref cfi = new_cfi ();
862
863 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
864 add_fde_cfi (label, cfi);
865 }
866
867 /* Add a CFI to update the running total of the size of arguments
868 pushed onto the stack. */
869
870 void
871 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
872 {
873 dw_cfi_ref cfi;
874
875 if (size == old_args_size)
876 return;
877
878 old_args_size = size;
879
880 cfi = new_cfi ();
881 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
882 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
883 add_fde_cfi (label, cfi);
884 }
885
886 /* Entry point for saving a register to the stack. REG is the GCC register
887 number. LABEL and OFFSET are passed to reg_save. */
888
889 void
890 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
891 {
892 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
893 }
894
895 /* Entry point for saving the return address in the stack.
896 LABEL and OFFSET are passed to reg_save. */
897
898 void
899 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
900 {
901 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
902 }
903
904 /* Entry point for saving the return address in a register.
905 LABEL and SREG are passed to reg_save. */
906
907 void
908 dwarf2out_return_reg (const char *label, unsigned int sreg)
909 {
910 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
911 }
912
913 /* Record the initial position of the return address. RTL is
914 INCOMING_RETURN_ADDR_RTX. */
915
916 static void
917 initial_return_save (rtx rtl)
918 {
919 unsigned int reg = INVALID_REGNUM;
920 HOST_WIDE_INT offset = 0;
921
922 switch (GET_CODE (rtl))
923 {
924 case REG:
925 /* RA is in a register. */
926 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
927 break;
928
929 case MEM:
930 /* RA is on the stack. */
931 rtl = XEXP (rtl, 0);
932 switch (GET_CODE (rtl))
933 {
934 case REG:
935 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
936 offset = 0;
937 break;
938
939 case PLUS:
940 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
941 offset = INTVAL (XEXP (rtl, 1));
942 break;
943
944 case MINUS:
945 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
946 offset = -INTVAL (XEXP (rtl, 1));
947 break;
948
949 default:
950 gcc_unreachable ();
951 }
952
953 break;
954
955 case PLUS:
956 /* The return address is at some offset from any value we can
957 actually load. For instance, on the SPARC it is in %i7+8. Just
958 ignore the offset for now; it doesn't matter for unwinding frames. */
959 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
960 initial_return_save (XEXP (rtl, 0));
961 return;
962
963 default:
964 gcc_unreachable ();
965 }
966
967 if (reg != DWARF_FRAME_RETURN_COLUMN)
968 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
969 }
970
971 /* Given a SET, calculate the amount of stack adjustment it
972 contains. */
973
974 static HOST_WIDE_INT
975 stack_adjust_offset (rtx pattern)
976 {
977 rtx src = SET_SRC (pattern);
978 rtx dest = SET_DEST (pattern);
979 HOST_WIDE_INT offset = 0;
980 enum rtx_code code;
981
982 if (dest == stack_pointer_rtx)
983 {
984 /* (set (reg sp) (plus (reg sp) (const_int))) */
985 code = GET_CODE (src);
986 if (! (code == PLUS || code == MINUS)
987 || XEXP (src, 0) != stack_pointer_rtx
988 || GET_CODE (XEXP (src, 1)) != CONST_INT)
989 return 0;
990
991 offset = INTVAL (XEXP (src, 1));
992 if (code == PLUS)
993 offset = -offset;
994 }
995 else if (MEM_P (dest))
996 {
997 /* (set (mem (pre_dec (reg sp))) (foo)) */
998 src = XEXP (dest, 0);
999 code = GET_CODE (src);
1000
1001 switch (code)
1002 {
1003 case PRE_MODIFY:
1004 case POST_MODIFY:
1005 if (XEXP (src, 0) == stack_pointer_rtx)
1006 {
1007 rtx val = XEXP (XEXP (src, 1), 1);
1008 /* We handle only adjustments by constant amount. */
1009 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1010 && GET_CODE (val) == CONST_INT);
1011 offset = -INTVAL (val);
1012 break;
1013 }
1014 return 0;
1015
1016 case PRE_DEC:
1017 case POST_DEC:
1018 if (XEXP (src, 0) == stack_pointer_rtx)
1019 {
1020 offset = GET_MODE_SIZE (GET_MODE (dest));
1021 break;
1022 }
1023 return 0;
1024
1025 case PRE_INC:
1026 case POST_INC:
1027 if (XEXP (src, 0) == stack_pointer_rtx)
1028 {
1029 offset = -GET_MODE_SIZE (GET_MODE (dest));
1030 break;
1031 }
1032 return 0;
1033
1034 default:
1035 return 0;
1036 }
1037 }
1038 else
1039 return 0;
1040
1041 return offset;
1042 }
1043
1044 /* Check INSN to see if it looks like a push or a stack adjustment, and
1045 make a note of it if it does. EH uses this information to find out how
1046 much extra space it needs to pop off the stack. */
1047
1048 static void
1049 dwarf2out_stack_adjust (rtx insn)
1050 {
1051 HOST_WIDE_INT offset;
1052 const char *label;
1053 int i;
1054
1055 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1056 with this function. Proper support would require all frame-related
1057 insns to be marked, and to be able to handle saving state around
1058 epilogues textually in the middle of the function. */
1059 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1060 return;
1061
1062 if (!flag_asynchronous_unwind_tables && CALL_P (insn))
1063 {
1064 /* Extract the size of the args from the CALL rtx itself. */
1065 insn = PATTERN (insn);
1066 if (GET_CODE (insn) == PARALLEL)
1067 insn = XVECEXP (insn, 0, 0);
1068 if (GET_CODE (insn) == SET)
1069 insn = SET_SRC (insn);
1070 gcc_assert (GET_CODE (insn) == CALL);
1071
1072 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1073 return;
1074 }
1075
1076 /* If only calls can throw, and we have a frame pointer,
1077 save up adjustments until we see the CALL_INSN. */
1078 else if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1079 return;
1080
1081 if (BARRIER_P (insn))
1082 {
1083 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1084 the compiler will have already emitted a stack adjustment, but
1085 doesn't bother for calls to noreturn functions. */
1086 #ifdef STACK_GROWS_DOWNWARD
1087 offset = -args_size;
1088 #else
1089 offset = args_size;
1090 #endif
1091 }
1092 else if (GET_CODE (PATTERN (insn)) == SET)
1093 offset = stack_adjust_offset (PATTERN (insn));
1094 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1095 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1096 {
1097 /* There may be stack adjustments inside compound insns. Search
1098 for them. */
1099 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1100 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1101 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1102 }
1103 else
1104 return;
1105
1106 if (offset == 0)
1107 return;
1108
1109 if (cfa.reg == STACK_POINTER_REGNUM)
1110 cfa.offset += offset;
1111
1112 #ifndef STACK_GROWS_DOWNWARD
1113 offset = -offset;
1114 #endif
1115
1116 args_size += offset;
1117 if (args_size < 0)
1118 args_size = 0;
1119
1120 label = dwarf2out_cfi_label ();
1121 def_cfa_1 (label, &cfa);
1122 dwarf2out_args_size (label, args_size);
1123 }
1124
1125 #endif
1126
1127 /* We delay emitting a register save until either (a) we reach the end
1128 of the prologue or (b) the register is clobbered. This clusters
1129 register saves so that there are fewer pc advances. */
1130
1131 struct queued_reg_save GTY(())
1132 {
1133 struct queued_reg_save *next;
1134 rtx reg;
1135 HOST_WIDE_INT cfa_offset;
1136 rtx saved_reg;
1137 };
1138
1139 static GTY(()) struct queued_reg_save *queued_reg_saves;
1140
1141 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1142 struct reg_saved_in_data GTY(()) {
1143 rtx orig_reg;
1144 rtx saved_in_reg;
1145 };
1146
1147 /* A list of registers saved in other registers.
1148 The list intentionally has a small maximum capacity of 4; if your
1149 port needs more than that, you might consider implementing a
1150 more efficient data structure. */
1151 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1152 static GTY(()) size_t num_regs_saved_in_regs;
1153
1154 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1155 static const char *last_reg_save_label;
1156
1157 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1158 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1159
1160 static void
1161 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1162 {
1163 struct queued_reg_save *q;
1164
1165 /* Duplicates waste space, but it's also necessary to remove them
1166 for correctness, since the queue gets output in reverse
1167 order. */
1168 for (q = queued_reg_saves; q != NULL; q = q->next)
1169 if (REGNO (q->reg) == REGNO (reg))
1170 break;
1171
1172 if (q == NULL)
1173 {
1174 q = ggc_alloc (sizeof (*q));
1175 q->next = queued_reg_saves;
1176 queued_reg_saves = q;
1177 }
1178
1179 q->reg = reg;
1180 q->cfa_offset = offset;
1181 q->saved_reg = sreg;
1182
1183 last_reg_save_label = label;
1184 }
1185
1186 /* Output all the entries in QUEUED_REG_SAVES. */
1187
1188 static void
1189 flush_queued_reg_saves (void)
1190 {
1191 struct queued_reg_save *q;
1192
1193 for (q = queued_reg_saves; q; q = q->next)
1194 {
1195 size_t i;
1196 unsigned int reg, sreg;
1197
1198 for (i = 0; i < num_regs_saved_in_regs; i++)
1199 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1200 break;
1201 if (q->saved_reg && i == num_regs_saved_in_regs)
1202 {
1203 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1204 num_regs_saved_in_regs++;
1205 }
1206 if (i != num_regs_saved_in_regs)
1207 {
1208 regs_saved_in_regs[i].orig_reg = q->reg;
1209 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1210 }
1211
1212 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1213 if (q->saved_reg)
1214 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1215 else
1216 sreg = INVALID_REGNUM;
1217 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1218 }
1219
1220 queued_reg_saves = NULL;
1221 last_reg_save_label = NULL;
1222 }
1223
1224 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1225 location for? Or, does it clobber a register which we've previously
1226 said that some other register is saved in, and for which we now
1227 have a new location for? */
1228
1229 static bool
1230 clobbers_queued_reg_save (rtx insn)
1231 {
1232 struct queued_reg_save *q;
1233
1234 for (q = queued_reg_saves; q; q = q->next)
1235 {
1236 size_t i;
1237 if (modified_in_p (q->reg, insn))
1238 return true;
1239 for (i = 0; i < num_regs_saved_in_regs; i++)
1240 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1241 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1242 return true;
1243 }
1244
1245 return false;
1246 }
1247
1248 /* What register, if any, is currently saved in REG? */
1249
1250 static rtx
1251 reg_saved_in (rtx reg)
1252 {
1253 unsigned int regn = REGNO (reg);
1254 size_t i;
1255 struct queued_reg_save *q;
1256
1257 for (q = queued_reg_saves; q; q = q->next)
1258 if (q->saved_reg && regn == REGNO (q->saved_reg))
1259 return q->reg;
1260
1261 for (i = 0; i < num_regs_saved_in_regs; i++)
1262 if (regs_saved_in_regs[i].saved_in_reg
1263 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1264 return regs_saved_in_regs[i].orig_reg;
1265
1266 return NULL_RTX;
1267 }
1268
1269
1270 /* A temporary register holding an integral value used in adjusting SP
1271 or setting up the store_reg. The "offset" field holds the integer
1272 value, not an offset. */
1273 static dw_cfa_location cfa_temp;
1274
1275 /* Record call frame debugging information for an expression EXPR,
1276 which either sets SP or FP (adjusting how we calculate the frame
1277 address) or saves a register to the stack or another register.
1278 LABEL indicates the address of EXPR.
1279
1280 This function encodes a state machine mapping rtxes to actions on
1281 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1282 users need not read the source code.
1283
1284 The High-Level Picture
1285
1286 Changes in the register we use to calculate the CFA: Currently we
1287 assume that if you copy the CFA register into another register, we
1288 should take the other one as the new CFA register; this seems to
1289 work pretty well. If it's wrong for some target, it's simple
1290 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1291
1292 Changes in the register we use for saving registers to the stack:
1293 This is usually SP, but not always. Again, we deduce that if you
1294 copy SP into another register (and SP is not the CFA register),
1295 then the new register is the one we will be using for register
1296 saves. This also seems to work.
1297
1298 Register saves: There's not much guesswork about this one; if
1299 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1300 register save, and the register used to calculate the destination
1301 had better be the one we think we're using for this purpose.
1302 It's also assumed that a copy from a call-saved register to another
1303 register is saving that register if RTX_FRAME_RELATED_P is set on
1304 that instruction. If the copy is from a call-saved register to
1305 the *same* register, that means that the register is now the same
1306 value as in the caller.
1307
1308 Except: If the register being saved is the CFA register, and the
1309 offset is nonzero, we are saving the CFA, so we assume we have to
1310 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1311 the intent is to save the value of SP from the previous frame.
1312
1313 In addition, if a register has previously been saved to a different
1314 register,
1315
1316 Invariants / Summaries of Rules
1317
1318 cfa current rule for calculating the CFA. It usually
1319 consists of a register and an offset.
1320 cfa_store register used by prologue code to save things to the stack
1321 cfa_store.offset is the offset from the value of
1322 cfa_store.reg to the actual CFA
1323 cfa_temp register holding an integral value. cfa_temp.offset
1324 stores the value, which will be used to adjust the
1325 stack pointer. cfa_temp is also used like cfa_store,
1326 to track stores to the stack via fp or a temp reg.
1327
1328 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1329 with cfa.reg as the first operand changes the cfa.reg and its
1330 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1331 cfa_temp.offset.
1332
1333 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1334 expression yielding a constant. This sets cfa_temp.reg
1335 and cfa_temp.offset.
1336
1337 Rule 5: Create a new register cfa_store used to save items to the
1338 stack.
1339
1340 Rules 10-14: Save a register to the stack. Define offset as the
1341 difference of the original location and cfa_store's
1342 location (or cfa_temp's location if cfa_temp is used).
1343
1344 The Rules
1345
1346 "{a,b}" indicates a choice of a xor b.
1347 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1348
1349 Rule 1:
1350 (set <reg1> <reg2>:cfa.reg)
1351 effects: cfa.reg = <reg1>
1352 cfa.offset unchanged
1353 cfa_temp.reg = <reg1>
1354 cfa_temp.offset = cfa.offset
1355
1356 Rule 2:
1357 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1358 {<const_int>,<reg>:cfa_temp.reg}))
1359 effects: cfa.reg = sp if fp used
1360 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1361 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1362 if cfa_store.reg==sp
1363
1364 Rule 3:
1365 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1366 effects: cfa.reg = fp
1367 cfa_offset += +/- <const_int>
1368
1369 Rule 4:
1370 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1371 constraints: <reg1> != fp
1372 <reg1> != sp
1373 effects: cfa.reg = <reg1>
1374 cfa_temp.reg = <reg1>
1375 cfa_temp.offset = cfa.offset
1376
1377 Rule 5:
1378 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1379 constraints: <reg1> != fp
1380 <reg1> != sp
1381 effects: cfa_store.reg = <reg1>
1382 cfa_store.offset = cfa.offset - cfa_temp.offset
1383
1384 Rule 6:
1385 (set <reg> <const_int>)
1386 effects: cfa_temp.reg = <reg>
1387 cfa_temp.offset = <const_int>
1388
1389 Rule 7:
1390 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1391 effects: cfa_temp.reg = <reg1>
1392 cfa_temp.offset |= <const_int>
1393
1394 Rule 8:
1395 (set <reg> (high <exp>))
1396 effects: none
1397
1398 Rule 9:
1399 (set <reg> (lo_sum <exp> <const_int>))
1400 effects: cfa_temp.reg = <reg>
1401 cfa_temp.offset = <const_int>
1402
1403 Rule 10:
1404 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1405 effects: cfa_store.offset -= <const_int>
1406 cfa.offset = cfa_store.offset if cfa.reg == sp
1407 cfa.reg = sp
1408 cfa.base_offset = -cfa_store.offset
1409
1410 Rule 11:
1411 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1412 effects: cfa_store.offset += -/+ mode_size(mem)
1413 cfa.offset = cfa_store.offset if cfa.reg == sp
1414 cfa.reg = sp
1415 cfa.base_offset = -cfa_store.offset
1416
1417 Rule 12:
1418 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1419
1420 <reg2>)
1421 effects: cfa.reg = <reg1>
1422 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1423
1424 Rule 13:
1425 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1426 effects: cfa.reg = <reg1>
1427 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1428
1429 Rule 14:
1430 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1431 effects: cfa.reg = <reg1>
1432 cfa.base_offset = -cfa_temp.offset
1433 cfa_temp.offset -= mode_size(mem) */
1434
1435 static void
1436 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1437 {
1438 rtx src, dest;
1439 HOST_WIDE_INT offset;
1440
1441 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1442 the PARALLEL independently. The first element is always processed if
1443 it is a SET. This is for backward compatibility. Other elements
1444 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1445 flag is set in them. */
1446 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1447 {
1448 int par_index;
1449 int limit = XVECLEN (expr, 0);
1450
1451 for (par_index = 0; par_index < limit; par_index++)
1452 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1453 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1454 || par_index == 0))
1455 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1456
1457 return;
1458 }
1459
1460 gcc_assert (GET_CODE (expr) == SET);
1461
1462 src = SET_SRC (expr);
1463 dest = SET_DEST (expr);
1464
1465 if (GET_CODE (src) == REG)
1466 {
1467 rtx rsi = reg_saved_in (src);
1468 if (rsi)
1469 src = rsi;
1470 }
1471
1472 switch (GET_CODE (dest))
1473 {
1474 case REG:
1475 switch (GET_CODE (src))
1476 {
1477 /* Setting FP from SP. */
1478 case REG:
1479 if (cfa.reg == (unsigned) REGNO (src))
1480 {
1481 /* Rule 1 */
1482 /* Update the CFA rule wrt SP or FP. Make sure src is
1483 relative to the current CFA register.
1484
1485 We used to require that dest be either SP or FP, but the
1486 ARM copies SP to a temporary register, and from there to
1487 FP. So we just rely on the backends to only set
1488 RTX_FRAME_RELATED_P on appropriate insns. */
1489 cfa.reg = REGNO (dest);
1490 cfa_temp.reg = cfa.reg;
1491 cfa_temp.offset = cfa.offset;
1492 }
1493 else
1494 {
1495 /* Saving a register in a register. */
1496 gcc_assert (call_used_regs [REGNO (dest)]
1497 && !fixed_regs [REGNO (dest)]);
1498 queue_reg_save (label, src, dest, 0);
1499 }
1500 break;
1501
1502 case PLUS:
1503 case MINUS:
1504 case LO_SUM:
1505 if (dest == stack_pointer_rtx)
1506 {
1507 /* Rule 2 */
1508 /* Adjusting SP. */
1509 switch (GET_CODE (XEXP (src, 1)))
1510 {
1511 case CONST_INT:
1512 offset = INTVAL (XEXP (src, 1));
1513 break;
1514 case REG:
1515 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1516 == cfa_temp.reg);
1517 offset = cfa_temp.offset;
1518 break;
1519 default:
1520 gcc_unreachable ();
1521 }
1522
1523 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1524 {
1525 /* Restoring SP from FP in the epilogue. */
1526 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1527 cfa.reg = STACK_POINTER_REGNUM;
1528 }
1529 else if (GET_CODE (src) == LO_SUM)
1530 /* Assume we've set the source reg of the LO_SUM from sp. */
1531 ;
1532 else
1533 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1534
1535 if (GET_CODE (src) != MINUS)
1536 offset = -offset;
1537 if (cfa.reg == STACK_POINTER_REGNUM)
1538 cfa.offset += offset;
1539 if (cfa_store.reg == STACK_POINTER_REGNUM)
1540 cfa_store.offset += offset;
1541 }
1542 else if (dest == hard_frame_pointer_rtx)
1543 {
1544 /* Rule 3 */
1545 /* Either setting the FP from an offset of the SP,
1546 or adjusting the FP */
1547 gcc_assert (frame_pointer_needed);
1548
1549 gcc_assert (REG_P (XEXP (src, 0))
1550 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1551 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1552 offset = INTVAL (XEXP (src, 1));
1553 if (GET_CODE (src) != MINUS)
1554 offset = -offset;
1555 cfa.offset += offset;
1556 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1557 }
1558 else
1559 {
1560 gcc_assert (GET_CODE (src) != MINUS);
1561
1562 /* Rule 4 */
1563 if (REG_P (XEXP (src, 0))
1564 && REGNO (XEXP (src, 0)) == cfa.reg
1565 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1566 {
1567 /* Setting a temporary CFA register that will be copied
1568 into the FP later on. */
1569 offset = - INTVAL (XEXP (src, 1));
1570 cfa.offset += offset;
1571 cfa.reg = REGNO (dest);
1572 /* Or used to save regs to the stack. */
1573 cfa_temp.reg = cfa.reg;
1574 cfa_temp.offset = cfa.offset;
1575 }
1576
1577 /* Rule 5 */
1578 else if (REG_P (XEXP (src, 0))
1579 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1580 && XEXP (src, 1) == stack_pointer_rtx)
1581 {
1582 /* Setting a scratch register that we will use instead
1583 of SP for saving registers to the stack. */
1584 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1585 cfa_store.reg = REGNO (dest);
1586 cfa_store.offset = cfa.offset - cfa_temp.offset;
1587 }
1588
1589 /* Rule 9 */
1590 else if (GET_CODE (src) == LO_SUM
1591 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1592 {
1593 cfa_temp.reg = REGNO (dest);
1594 cfa_temp.offset = INTVAL (XEXP (src, 1));
1595 }
1596 else
1597 gcc_unreachable ();
1598 }
1599 break;
1600
1601 /* Rule 6 */
1602 case CONST_INT:
1603 cfa_temp.reg = REGNO (dest);
1604 cfa_temp.offset = INTVAL (src);
1605 break;
1606
1607 /* Rule 7 */
1608 case IOR:
1609 gcc_assert (REG_P (XEXP (src, 0))
1610 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1611 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1612
1613 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1614 cfa_temp.reg = REGNO (dest);
1615 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1616 break;
1617
1618 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1619 which will fill in all of the bits. */
1620 /* Rule 8 */
1621 case HIGH:
1622 break;
1623
1624 default:
1625 gcc_unreachable ();
1626 }
1627
1628 def_cfa_1 (label, &cfa);
1629 break;
1630
1631 case MEM:
1632 gcc_assert (REG_P (src));
1633
1634 /* Saving a register to the stack. Make sure dest is relative to the
1635 CFA register. */
1636 switch (GET_CODE (XEXP (dest, 0)))
1637 {
1638 /* Rule 10 */
1639 /* With a push. */
1640 case PRE_MODIFY:
1641 /* We can't handle variable size modifications. */
1642 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1643 == CONST_INT);
1644 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1645
1646 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1647 && cfa_store.reg == STACK_POINTER_REGNUM);
1648
1649 cfa_store.offset += offset;
1650 if (cfa.reg == STACK_POINTER_REGNUM)
1651 cfa.offset = cfa_store.offset;
1652
1653 offset = -cfa_store.offset;
1654 break;
1655
1656 /* Rule 11 */
1657 case PRE_INC:
1658 case PRE_DEC:
1659 offset = GET_MODE_SIZE (GET_MODE (dest));
1660 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1661 offset = -offset;
1662
1663 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1664 && cfa_store.reg == STACK_POINTER_REGNUM);
1665
1666 cfa_store.offset += offset;
1667 if (cfa.reg == STACK_POINTER_REGNUM)
1668 cfa.offset = cfa_store.offset;
1669
1670 offset = -cfa_store.offset;
1671 break;
1672
1673 /* Rule 12 */
1674 /* With an offset. */
1675 case PLUS:
1676 case MINUS:
1677 case LO_SUM:
1678 {
1679 int regno;
1680
1681 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
1682 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1683 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1684 offset = -offset;
1685
1686 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1687
1688 if (cfa_store.reg == (unsigned) regno)
1689 offset -= cfa_store.offset;
1690 else
1691 {
1692 gcc_assert (cfa_temp.reg == (unsigned) regno);
1693 offset -= cfa_temp.offset;
1694 }
1695 }
1696 break;
1697
1698 /* Rule 13 */
1699 /* Without an offset. */
1700 case REG:
1701 {
1702 int regno = REGNO (XEXP (dest, 0));
1703
1704 if (cfa_store.reg == (unsigned) regno)
1705 offset = -cfa_store.offset;
1706 else
1707 {
1708 gcc_assert (cfa_temp.reg == (unsigned) regno);
1709 offset = -cfa_temp.offset;
1710 }
1711 }
1712 break;
1713
1714 /* Rule 14 */
1715 case POST_INC:
1716 gcc_assert (cfa_temp.reg
1717 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1718 offset = -cfa_temp.offset;
1719 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1720 break;
1721
1722 default:
1723 gcc_unreachable ();
1724 }
1725
1726 if (REGNO (src) != STACK_POINTER_REGNUM
1727 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1728 && (unsigned) REGNO (src) == cfa.reg)
1729 {
1730 /* We're storing the current CFA reg into the stack. */
1731
1732 if (cfa.offset == 0)
1733 {
1734 /* If the source register is exactly the CFA, assume
1735 we're saving SP like any other register; this happens
1736 on the ARM. */
1737 def_cfa_1 (label, &cfa);
1738 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1739 break;
1740 }
1741 else
1742 {
1743 /* Otherwise, we'll need to look in the stack to
1744 calculate the CFA. */
1745 rtx x = XEXP (dest, 0);
1746
1747 if (!REG_P (x))
1748 x = XEXP (x, 0);
1749 gcc_assert (REG_P (x));
1750
1751 cfa.reg = REGNO (x);
1752 cfa.base_offset = offset;
1753 cfa.indirect = 1;
1754 def_cfa_1 (label, &cfa);
1755 break;
1756 }
1757 }
1758
1759 def_cfa_1 (label, &cfa);
1760 queue_reg_save (label, src, NULL_RTX, offset);
1761 break;
1762
1763 default:
1764 gcc_unreachable ();
1765 }
1766 }
1767
1768 /* Record call frame debugging information for INSN, which either
1769 sets SP or FP (adjusting how we calculate the frame address) or saves a
1770 register to the stack. If INSN is NULL_RTX, initialize our state. */
1771
1772 void
1773 dwarf2out_frame_debug (rtx insn)
1774 {
1775 const char *label;
1776 rtx src;
1777
1778 if (insn == NULL_RTX)
1779 {
1780 size_t i;
1781
1782 /* Flush any queued register saves. */
1783 flush_queued_reg_saves ();
1784
1785 /* Set up state for generating call frame debug info. */
1786 lookup_cfa (&cfa);
1787 gcc_assert (cfa.reg
1788 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1789
1790 cfa.reg = STACK_POINTER_REGNUM;
1791 cfa_store = cfa;
1792 cfa_temp.reg = -1;
1793 cfa_temp.offset = 0;
1794
1795 for (i = 0; i < num_regs_saved_in_regs; i++)
1796 {
1797 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1798 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1799 }
1800 num_regs_saved_in_regs = 0;
1801 return;
1802 }
1803
1804 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1805 flush_queued_reg_saves ();
1806
1807 if (! RTX_FRAME_RELATED_P (insn))
1808 {
1809 if (!ACCUMULATE_OUTGOING_ARGS)
1810 dwarf2out_stack_adjust (insn);
1811
1812 return;
1813 }
1814
1815 label = dwarf2out_cfi_label ();
1816 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1817 if (src)
1818 insn = XEXP (src, 0);
1819 else
1820 insn = PATTERN (insn);
1821
1822 dwarf2out_frame_debug_expr (insn, label);
1823 }
1824
1825 #endif
1826
1827 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1828 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1829 (enum dwarf_call_frame_info cfi);
1830
1831 static enum dw_cfi_oprnd_type
1832 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1833 {
1834 switch (cfi)
1835 {
1836 case DW_CFA_nop:
1837 case DW_CFA_GNU_window_save:
1838 return dw_cfi_oprnd_unused;
1839
1840 case DW_CFA_set_loc:
1841 case DW_CFA_advance_loc1:
1842 case DW_CFA_advance_loc2:
1843 case DW_CFA_advance_loc4:
1844 case DW_CFA_MIPS_advance_loc8:
1845 return dw_cfi_oprnd_addr;
1846
1847 case DW_CFA_offset:
1848 case DW_CFA_offset_extended:
1849 case DW_CFA_def_cfa:
1850 case DW_CFA_offset_extended_sf:
1851 case DW_CFA_def_cfa_sf:
1852 case DW_CFA_restore_extended:
1853 case DW_CFA_undefined:
1854 case DW_CFA_same_value:
1855 case DW_CFA_def_cfa_register:
1856 case DW_CFA_register:
1857 return dw_cfi_oprnd_reg_num;
1858
1859 case DW_CFA_def_cfa_offset:
1860 case DW_CFA_GNU_args_size:
1861 case DW_CFA_def_cfa_offset_sf:
1862 return dw_cfi_oprnd_offset;
1863
1864 case DW_CFA_def_cfa_expression:
1865 case DW_CFA_expression:
1866 return dw_cfi_oprnd_loc;
1867
1868 default:
1869 gcc_unreachable ();
1870 }
1871 }
1872
1873 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1874 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1875 (enum dwarf_call_frame_info cfi);
1876
1877 static enum dw_cfi_oprnd_type
1878 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1879 {
1880 switch (cfi)
1881 {
1882 case DW_CFA_def_cfa:
1883 case DW_CFA_def_cfa_sf:
1884 case DW_CFA_offset:
1885 case DW_CFA_offset_extended_sf:
1886 case DW_CFA_offset_extended:
1887 return dw_cfi_oprnd_offset;
1888
1889 case DW_CFA_register:
1890 return dw_cfi_oprnd_reg_num;
1891
1892 default:
1893 return dw_cfi_oprnd_unused;
1894 }
1895 }
1896
1897 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1898
1899 /* Map register numbers held in the call frame info that gcc has
1900 collected using DWARF_FRAME_REGNUM to those that should be output in
1901 .debug_frame and .eh_frame. */
1902 #ifndef DWARF2_FRAME_REG_OUT
1903 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
1904 #endif
1905
1906 /* Output a Call Frame Information opcode and its operand(s). */
1907
1908 static void
1909 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
1910 {
1911 unsigned long r;
1912 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1913 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1914 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1915 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
1916 cfi->dw_cfi_oprnd1.dw_cfi_offset);
1917 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1918 {
1919 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1920 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1921 "DW_CFA_offset, column 0x%lx", r);
1922 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1923 }
1924 else if (cfi->dw_cfi_opc == DW_CFA_restore)
1925 {
1926 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1927 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1928 "DW_CFA_restore, column 0x%lx", r);
1929 }
1930 else
1931 {
1932 dw2_asm_output_data (1, cfi->dw_cfi_opc,
1933 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1934
1935 switch (cfi->dw_cfi_opc)
1936 {
1937 case DW_CFA_set_loc:
1938 if (for_eh)
1939 dw2_asm_output_encoded_addr_rtx (
1940 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1941 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1942 NULL);
1943 else
1944 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1945 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1946 break;
1947
1948 case DW_CFA_advance_loc1:
1949 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1950 fde->dw_fde_current_label, NULL);
1951 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1952 break;
1953
1954 case DW_CFA_advance_loc2:
1955 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1956 fde->dw_fde_current_label, NULL);
1957 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1958 break;
1959
1960 case DW_CFA_advance_loc4:
1961 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1962 fde->dw_fde_current_label, NULL);
1963 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1964 break;
1965
1966 case DW_CFA_MIPS_advance_loc8:
1967 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1968 fde->dw_fde_current_label, NULL);
1969 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1970 break;
1971
1972 case DW_CFA_offset_extended:
1973 case DW_CFA_def_cfa:
1974 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1975 dw2_asm_output_data_uleb128 (r, NULL);
1976 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1977 break;
1978
1979 case DW_CFA_offset_extended_sf:
1980 case DW_CFA_def_cfa_sf:
1981 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1982 dw2_asm_output_data_uleb128 (r, NULL);
1983 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1984 break;
1985
1986 case DW_CFA_restore_extended:
1987 case DW_CFA_undefined:
1988 case DW_CFA_same_value:
1989 case DW_CFA_def_cfa_register:
1990 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1991 dw2_asm_output_data_uleb128 (r, NULL);
1992 break;
1993
1994 case DW_CFA_register:
1995 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1996 dw2_asm_output_data_uleb128 (r, NULL);
1997 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
1998 dw2_asm_output_data_uleb128 (r, NULL);
1999 break;
2000
2001 case DW_CFA_def_cfa_offset:
2002 case DW_CFA_GNU_args_size:
2003 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2004 break;
2005
2006 case DW_CFA_def_cfa_offset_sf:
2007 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2008 break;
2009
2010 case DW_CFA_GNU_window_save:
2011 break;
2012
2013 case DW_CFA_def_cfa_expression:
2014 case DW_CFA_expression:
2015 output_cfa_loc (cfi);
2016 break;
2017
2018 case DW_CFA_GNU_negative_offset_extended:
2019 /* Obsoleted by DW_CFA_offset_extended_sf. */
2020 gcc_unreachable ();
2021
2022 default:
2023 break;
2024 }
2025 }
2026 }
2027
2028 /* Output the call frame information used to record information
2029 that relates to calculating the frame pointer, and records the
2030 location of saved registers. */
2031
2032 static void
2033 output_call_frame_info (int for_eh)
2034 {
2035 unsigned int i;
2036 dw_fde_ref fde;
2037 dw_cfi_ref cfi;
2038 char l1[20], l2[20], section_start_label[20];
2039 bool any_lsda_needed = false;
2040 char augmentation[6];
2041 int augmentation_size;
2042 int fde_encoding = DW_EH_PE_absptr;
2043 int per_encoding = DW_EH_PE_absptr;
2044 int lsda_encoding = DW_EH_PE_absptr;
2045
2046 /* Don't emit a CIE if there won't be any FDEs. */
2047 if (fde_table_in_use == 0)
2048 return;
2049
2050 /* If we make FDEs linkonce, we may have to emit an empty label for
2051 an FDE that wouldn't otherwise be emitted. We want to avoid
2052 having an FDE kept around when the function it refers to is
2053 discarded. (Example where this matters: a primary function
2054 template in C++ requires EH information, but an explicit
2055 specialization doesn't. */
2056 if (TARGET_USES_WEAK_UNWIND_INFO
2057 && ! flag_asynchronous_unwind_tables
2058 && for_eh)
2059 for (i = 0; i < fde_table_in_use; i++)
2060 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2061 && !fde_table[i].uses_eh_lsda
2062 && ! DECL_ONE_ONLY (fde_table[i].decl))
2063 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2064 for_eh, /* empty */ 1);
2065
2066 /* If we don't have any functions we'll want to unwind out of, don't
2067 emit any EH unwind information. Note that if exceptions aren't
2068 enabled, we won't have collected nothrow information, and if we
2069 asked for asynchronous tables, we always want this info. */
2070 if (for_eh)
2071 {
2072 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2073
2074 for (i = 0; i < fde_table_in_use; i++)
2075 if (fde_table[i].uses_eh_lsda)
2076 any_eh_needed = any_lsda_needed = true;
2077 else if (TARGET_USES_WEAK_UNWIND_INFO
2078 && DECL_ONE_ONLY (fde_table[i].decl))
2079 any_eh_needed = true;
2080 else if (! fde_table[i].nothrow
2081 && ! fde_table[i].all_throwers_are_sibcalls)
2082 any_eh_needed = true;
2083
2084 if (! any_eh_needed)
2085 return;
2086 }
2087
2088 /* We're going to be generating comments, so turn on app. */
2089 if (flag_debug_asm)
2090 app_enable ();
2091
2092 if (for_eh)
2093 targetm.asm_out.eh_frame_section ();
2094 else
2095 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2096
2097 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2098 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2099
2100 /* Output the CIE. */
2101 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2102 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2103 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2104 "Length of Common Information Entry");
2105 ASM_OUTPUT_LABEL (asm_out_file, l1);
2106
2107 /* Now that the CIE pointer is PC-relative for EH,
2108 use 0 to identify the CIE. */
2109 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2110 (for_eh ? 0 : DW_CIE_ID),
2111 "CIE Identifier Tag");
2112
2113 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2114
2115 augmentation[0] = 0;
2116 augmentation_size = 0;
2117 if (for_eh)
2118 {
2119 char *p;
2120
2121 /* Augmentation:
2122 z Indicates that a uleb128 is present to size the
2123 augmentation section.
2124 L Indicates the encoding (and thus presence) of
2125 an LSDA pointer in the FDE augmentation.
2126 R Indicates a non-default pointer encoding for
2127 FDE code pointers.
2128 P Indicates the presence of an encoding + language
2129 personality routine in the CIE augmentation. */
2130
2131 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2132 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2133 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2134
2135 p = augmentation + 1;
2136 if (eh_personality_libfunc)
2137 {
2138 *p++ = 'P';
2139 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2140 }
2141 if (any_lsda_needed)
2142 {
2143 *p++ = 'L';
2144 augmentation_size += 1;
2145 }
2146 if (fde_encoding != DW_EH_PE_absptr)
2147 {
2148 *p++ = 'R';
2149 augmentation_size += 1;
2150 }
2151 if (p > augmentation + 1)
2152 {
2153 augmentation[0] = 'z';
2154 *p = '\0';
2155 }
2156
2157 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2158 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2159 {
2160 int offset = ( 4 /* Length */
2161 + 4 /* CIE Id */
2162 + 1 /* CIE version */
2163 + strlen (augmentation) + 1 /* Augmentation */
2164 + size_of_uleb128 (1) /* Code alignment */
2165 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2166 + 1 /* RA column */
2167 + 1 /* Augmentation size */
2168 + 1 /* Personality encoding */ );
2169 int pad = -offset & (PTR_SIZE - 1);
2170
2171 augmentation_size += pad;
2172
2173 /* Augmentations should be small, so there's scarce need to
2174 iterate for a solution. Die if we exceed one uleb128 byte. */
2175 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2176 }
2177 }
2178
2179 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2180 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2181 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2182 "CIE Data Alignment Factor");
2183
2184 if (DW_CIE_VERSION == 1)
2185 dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2186 else
2187 dw2_asm_output_data_uleb128 (DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2188
2189 if (augmentation[0])
2190 {
2191 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2192 if (eh_personality_libfunc)
2193 {
2194 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2195 eh_data_format_name (per_encoding));
2196 dw2_asm_output_encoded_addr_rtx (per_encoding,
2197 eh_personality_libfunc, NULL);
2198 }
2199
2200 if (any_lsda_needed)
2201 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2202 eh_data_format_name (lsda_encoding));
2203
2204 if (fde_encoding != DW_EH_PE_absptr)
2205 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2206 eh_data_format_name (fde_encoding));
2207 }
2208
2209 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2210 output_cfi (cfi, NULL, for_eh);
2211
2212 /* Pad the CIE out to an address sized boundary. */
2213 ASM_OUTPUT_ALIGN (asm_out_file,
2214 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2215 ASM_OUTPUT_LABEL (asm_out_file, l2);
2216
2217 /* Loop through all of the FDE's. */
2218 for (i = 0; i < fde_table_in_use; i++)
2219 {
2220 fde = &fde_table[i];
2221
2222 /* Don't emit EH unwind info for leaf functions that don't need it. */
2223 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2224 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2225 && (! TARGET_USES_WEAK_UNWIND_INFO || ! DECL_ONE_ONLY (fde->decl))
2226 && !fde->uses_eh_lsda)
2227 continue;
2228
2229 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2230 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2231 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2232 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2233 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2234 "FDE Length");
2235 ASM_OUTPUT_LABEL (asm_out_file, l1);
2236
2237 if (for_eh)
2238 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2239 else
2240 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2241 "FDE CIE offset");
2242
2243 if (for_eh)
2244 {
2245 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2246 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2247 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2248 sym_ref,
2249 "FDE initial location");
2250 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2251 fde->dw_fde_end, fde->dw_fde_begin,
2252 "FDE address range");
2253 }
2254 else
2255 {
2256 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2257 "FDE initial location");
2258 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2259 fde->dw_fde_end, fde->dw_fde_begin,
2260 "FDE address range");
2261 }
2262
2263 if (augmentation[0])
2264 {
2265 if (any_lsda_needed)
2266 {
2267 int size = size_of_encoded_value (lsda_encoding);
2268
2269 if (lsda_encoding == DW_EH_PE_aligned)
2270 {
2271 int offset = ( 4 /* Length */
2272 + 4 /* CIE offset */
2273 + 2 * size_of_encoded_value (fde_encoding)
2274 + 1 /* Augmentation size */ );
2275 int pad = -offset & (PTR_SIZE - 1);
2276
2277 size += pad;
2278 gcc_assert (size_of_uleb128 (size) == 1);
2279 }
2280
2281 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2282
2283 if (fde->uses_eh_lsda)
2284 {
2285 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2286 fde->funcdef_number);
2287 dw2_asm_output_encoded_addr_rtx (
2288 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2289 "Language Specific Data Area");
2290 }
2291 else
2292 {
2293 if (lsda_encoding == DW_EH_PE_aligned)
2294 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2295 dw2_asm_output_data
2296 (size_of_encoded_value (lsda_encoding), 0,
2297 "Language Specific Data Area (none)");
2298 }
2299 }
2300 else
2301 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2302 }
2303
2304 /* Loop through the Call Frame Instructions associated with
2305 this FDE. */
2306 fde->dw_fde_current_label = fde->dw_fde_begin;
2307 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2308 output_cfi (cfi, fde, for_eh);
2309
2310 /* Pad the FDE out to an address sized boundary. */
2311 ASM_OUTPUT_ALIGN (asm_out_file,
2312 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2313 ASM_OUTPUT_LABEL (asm_out_file, l2);
2314 }
2315
2316 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2317 dw2_asm_output_data (4, 0, "End of Table");
2318 #ifdef MIPS_DEBUGGING_INFO
2319 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2320 get a value of 0. Putting .align 0 after the label fixes it. */
2321 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2322 #endif
2323
2324 /* Turn off app to make assembly quicker. */
2325 if (flag_debug_asm)
2326 app_disable ();
2327 }
2328
2329 /* Output a marker (i.e. a label) for the beginning of a function, before
2330 the prologue. */
2331
2332 void
2333 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2334 const char *file ATTRIBUTE_UNUSED)
2335 {
2336 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2337 dw_fde_ref fde;
2338
2339 current_function_func_begin_label = 0;
2340
2341 #ifdef TARGET_UNWIND_INFO
2342 /* ??? current_function_func_begin_label is also used by except.c
2343 for call-site information. We must emit this label if it might
2344 be used. */
2345 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2346 && ! dwarf2out_do_frame ())
2347 return;
2348 #else
2349 if (! dwarf2out_do_frame ())
2350 return;
2351 #endif
2352
2353 function_section (current_function_decl);
2354 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2355 current_function_funcdef_no);
2356 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2357 current_function_funcdef_no);
2358 current_function_func_begin_label = get_identifier (label);
2359
2360 #ifdef TARGET_UNWIND_INFO
2361 /* We can elide the fde allocation if we're not emitting debug info. */
2362 if (! dwarf2out_do_frame ())
2363 return;
2364 #endif
2365
2366 /* Expand the fde table if necessary. */
2367 if (fde_table_in_use == fde_table_allocated)
2368 {
2369 fde_table_allocated += FDE_TABLE_INCREMENT;
2370 fde_table = ggc_realloc (fde_table,
2371 fde_table_allocated * sizeof (dw_fde_node));
2372 memset (fde_table + fde_table_in_use, 0,
2373 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2374 }
2375
2376 /* Record the FDE associated with this function. */
2377 current_funcdef_fde = fde_table_in_use;
2378
2379 /* Add the new FDE at the end of the fde_table. */
2380 fde = &fde_table[fde_table_in_use++];
2381 fde->decl = current_function_decl;
2382 fde->dw_fde_begin = xstrdup (label);
2383 fde->dw_fde_current_label = NULL;
2384 fde->dw_fde_end = NULL;
2385 fde->dw_fde_cfi = NULL;
2386 fde->funcdef_number = current_function_funcdef_no;
2387 fde->nothrow = TREE_NOTHROW (current_function_decl);
2388 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2389 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2390
2391 args_size = old_args_size = 0;
2392
2393 /* We only want to output line number information for the genuine dwarf2
2394 prologue case, not the eh frame case. */
2395 #ifdef DWARF2_DEBUGGING_INFO
2396 if (file)
2397 dwarf2out_source_line (line, file);
2398 #endif
2399 }
2400
2401 /* Output a marker (i.e. a label) for the absolute end of the generated code
2402 for a function definition. This gets called *after* the epilogue code has
2403 been generated. */
2404
2405 void
2406 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2407 const char *file ATTRIBUTE_UNUSED)
2408 {
2409 dw_fde_ref fde;
2410 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2411
2412 /* Output a label to mark the endpoint of the code generated for this
2413 function. */
2414 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2415 current_function_funcdef_no);
2416 ASM_OUTPUT_LABEL (asm_out_file, label);
2417 fde = &fde_table[fde_table_in_use - 1];
2418 fde->dw_fde_end = xstrdup (label);
2419 }
2420
2421 void
2422 dwarf2out_frame_init (void)
2423 {
2424 /* Allocate the initial hunk of the fde_table. */
2425 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2426 fde_table_allocated = FDE_TABLE_INCREMENT;
2427 fde_table_in_use = 0;
2428
2429 /* Generate the CFA instructions common to all FDE's. Do it now for the
2430 sake of lookup_cfa. */
2431
2432 #ifdef DWARF2_UNWIND_INFO
2433 /* On entry, the Canonical Frame Address is at SP. */
2434 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2435 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2436 #endif
2437 }
2438
2439 void
2440 dwarf2out_frame_finish (void)
2441 {
2442 /* Output call frame information. */
2443 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2444 output_call_frame_info (0);
2445
2446 #ifndef TARGET_UNWIND_INFO
2447 /* Output another copy for the unwinder. */
2448 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2449 output_call_frame_info (1);
2450 #endif
2451 }
2452 #endif
2453 \f
2454 /* And now, the subset of the debugging information support code necessary
2455 for emitting location expressions. */
2456
2457 /* We need some way to distinguish DW_OP_addr with a direct symbol
2458 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2459 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2460
2461
2462 typedef struct dw_val_struct *dw_val_ref;
2463 typedef struct die_struct *dw_die_ref;
2464 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2465 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2466
2467 /* Each DIE may have a series of attribute/value pairs. Values
2468 can take on several forms. The forms that are used in this
2469 implementation are listed below. */
2470
2471 enum dw_val_class
2472 {
2473 dw_val_class_addr,
2474 dw_val_class_offset,
2475 dw_val_class_loc,
2476 dw_val_class_loc_list,
2477 dw_val_class_range_list,
2478 dw_val_class_const,
2479 dw_val_class_unsigned_const,
2480 dw_val_class_long_long,
2481 dw_val_class_vec,
2482 dw_val_class_flag,
2483 dw_val_class_die_ref,
2484 dw_val_class_fde_ref,
2485 dw_val_class_lbl_id,
2486 dw_val_class_lbl_offset,
2487 dw_val_class_str
2488 };
2489
2490 /* Describe a double word constant value. */
2491 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2492
2493 typedef struct dw_long_long_struct GTY(())
2494 {
2495 unsigned long hi;
2496 unsigned long low;
2497 }
2498 dw_long_long_const;
2499
2500 /* Describe a floating point constant value, or a vector constant value. */
2501
2502 typedef struct dw_vec_struct GTY(())
2503 {
2504 unsigned char * GTY((length ("%h.length"))) array;
2505 unsigned length;
2506 unsigned elt_size;
2507 }
2508 dw_vec_const;
2509
2510 /* The dw_val_node describes an attribute's value, as it is
2511 represented internally. */
2512
2513 typedef struct dw_val_struct GTY(())
2514 {
2515 enum dw_val_class val_class;
2516 union dw_val_struct_union
2517 {
2518 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2519 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2520 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2521 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2522 HOST_WIDE_INT GTY ((default)) val_int;
2523 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2524 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2525 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2526 struct dw_val_die_union
2527 {
2528 dw_die_ref die;
2529 int external;
2530 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2531 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2532 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2533 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2534 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2535 }
2536 GTY ((desc ("%1.val_class"))) v;
2537 }
2538 dw_val_node;
2539
2540 /* Locations in memory are described using a sequence of stack machine
2541 operations. */
2542
2543 typedef struct dw_loc_descr_struct GTY(())
2544 {
2545 dw_loc_descr_ref dw_loc_next;
2546 enum dwarf_location_atom dw_loc_opc;
2547 dw_val_node dw_loc_oprnd1;
2548 dw_val_node dw_loc_oprnd2;
2549 int dw_loc_addr;
2550 }
2551 dw_loc_descr_node;
2552
2553 /* Location lists are ranges + location descriptions for that range,
2554 so you can track variables that are in different places over
2555 their entire life. */
2556 typedef struct dw_loc_list_struct GTY(())
2557 {
2558 dw_loc_list_ref dw_loc_next;
2559 const char *begin; /* Label for begin address of range */
2560 const char *end; /* Label for end address of range */
2561 char *ll_symbol; /* Label for beginning of location list.
2562 Only on head of list */
2563 const char *section; /* Section this loclist is relative to */
2564 dw_loc_descr_ref expr;
2565 } dw_loc_list_node;
2566
2567 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2568
2569 static const char *dwarf_stack_op_name (unsigned);
2570 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2571 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2572 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2573 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2574 static unsigned long size_of_locs (dw_loc_descr_ref);
2575 static void output_loc_operands (dw_loc_descr_ref);
2576 static void output_loc_sequence (dw_loc_descr_ref);
2577
2578 /* Convert a DWARF stack opcode into its string name. */
2579
2580 static const char *
2581 dwarf_stack_op_name (unsigned int op)
2582 {
2583 switch (op)
2584 {
2585 case DW_OP_addr:
2586 case INTERNAL_DW_OP_tls_addr:
2587 return "DW_OP_addr";
2588 case DW_OP_deref:
2589 return "DW_OP_deref";
2590 case DW_OP_const1u:
2591 return "DW_OP_const1u";
2592 case DW_OP_const1s:
2593 return "DW_OP_const1s";
2594 case DW_OP_const2u:
2595 return "DW_OP_const2u";
2596 case DW_OP_const2s:
2597 return "DW_OP_const2s";
2598 case DW_OP_const4u:
2599 return "DW_OP_const4u";
2600 case DW_OP_const4s:
2601 return "DW_OP_const4s";
2602 case DW_OP_const8u:
2603 return "DW_OP_const8u";
2604 case DW_OP_const8s:
2605 return "DW_OP_const8s";
2606 case DW_OP_constu:
2607 return "DW_OP_constu";
2608 case DW_OP_consts:
2609 return "DW_OP_consts";
2610 case DW_OP_dup:
2611 return "DW_OP_dup";
2612 case DW_OP_drop:
2613 return "DW_OP_drop";
2614 case DW_OP_over:
2615 return "DW_OP_over";
2616 case DW_OP_pick:
2617 return "DW_OP_pick";
2618 case DW_OP_swap:
2619 return "DW_OP_swap";
2620 case DW_OP_rot:
2621 return "DW_OP_rot";
2622 case DW_OP_xderef:
2623 return "DW_OP_xderef";
2624 case DW_OP_abs:
2625 return "DW_OP_abs";
2626 case DW_OP_and:
2627 return "DW_OP_and";
2628 case DW_OP_div:
2629 return "DW_OP_div";
2630 case DW_OP_minus:
2631 return "DW_OP_minus";
2632 case DW_OP_mod:
2633 return "DW_OP_mod";
2634 case DW_OP_mul:
2635 return "DW_OP_mul";
2636 case DW_OP_neg:
2637 return "DW_OP_neg";
2638 case DW_OP_not:
2639 return "DW_OP_not";
2640 case DW_OP_or:
2641 return "DW_OP_or";
2642 case DW_OP_plus:
2643 return "DW_OP_plus";
2644 case DW_OP_plus_uconst:
2645 return "DW_OP_plus_uconst";
2646 case DW_OP_shl:
2647 return "DW_OP_shl";
2648 case DW_OP_shr:
2649 return "DW_OP_shr";
2650 case DW_OP_shra:
2651 return "DW_OP_shra";
2652 case DW_OP_xor:
2653 return "DW_OP_xor";
2654 case DW_OP_bra:
2655 return "DW_OP_bra";
2656 case DW_OP_eq:
2657 return "DW_OP_eq";
2658 case DW_OP_ge:
2659 return "DW_OP_ge";
2660 case DW_OP_gt:
2661 return "DW_OP_gt";
2662 case DW_OP_le:
2663 return "DW_OP_le";
2664 case DW_OP_lt:
2665 return "DW_OP_lt";
2666 case DW_OP_ne:
2667 return "DW_OP_ne";
2668 case DW_OP_skip:
2669 return "DW_OP_skip";
2670 case DW_OP_lit0:
2671 return "DW_OP_lit0";
2672 case DW_OP_lit1:
2673 return "DW_OP_lit1";
2674 case DW_OP_lit2:
2675 return "DW_OP_lit2";
2676 case DW_OP_lit3:
2677 return "DW_OP_lit3";
2678 case DW_OP_lit4:
2679 return "DW_OP_lit4";
2680 case DW_OP_lit5:
2681 return "DW_OP_lit5";
2682 case DW_OP_lit6:
2683 return "DW_OP_lit6";
2684 case DW_OP_lit7:
2685 return "DW_OP_lit7";
2686 case DW_OP_lit8:
2687 return "DW_OP_lit8";
2688 case DW_OP_lit9:
2689 return "DW_OP_lit9";
2690 case DW_OP_lit10:
2691 return "DW_OP_lit10";
2692 case DW_OP_lit11:
2693 return "DW_OP_lit11";
2694 case DW_OP_lit12:
2695 return "DW_OP_lit12";
2696 case DW_OP_lit13:
2697 return "DW_OP_lit13";
2698 case DW_OP_lit14:
2699 return "DW_OP_lit14";
2700 case DW_OP_lit15:
2701 return "DW_OP_lit15";
2702 case DW_OP_lit16:
2703 return "DW_OP_lit16";
2704 case DW_OP_lit17:
2705 return "DW_OP_lit17";
2706 case DW_OP_lit18:
2707 return "DW_OP_lit18";
2708 case DW_OP_lit19:
2709 return "DW_OP_lit19";
2710 case DW_OP_lit20:
2711 return "DW_OP_lit20";
2712 case DW_OP_lit21:
2713 return "DW_OP_lit21";
2714 case DW_OP_lit22:
2715 return "DW_OP_lit22";
2716 case DW_OP_lit23:
2717 return "DW_OP_lit23";
2718 case DW_OP_lit24:
2719 return "DW_OP_lit24";
2720 case DW_OP_lit25:
2721 return "DW_OP_lit25";
2722 case DW_OP_lit26:
2723 return "DW_OP_lit26";
2724 case DW_OP_lit27:
2725 return "DW_OP_lit27";
2726 case DW_OP_lit28:
2727 return "DW_OP_lit28";
2728 case DW_OP_lit29:
2729 return "DW_OP_lit29";
2730 case DW_OP_lit30:
2731 return "DW_OP_lit30";
2732 case DW_OP_lit31:
2733 return "DW_OP_lit31";
2734 case DW_OP_reg0:
2735 return "DW_OP_reg0";
2736 case DW_OP_reg1:
2737 return "DW_OP_reg1";
2738 case DW_OP_reg2:
2739 return "DW_OP_reg2";
2740 case DW_OP_reg3:
2741 return "DW_OP_reg3";
2742 case DW_OP_reg4:
2743 return "DW_OP_reg4";
2744 case DW_OP_reg5:
2745 return "DW_OP_reg5";
2746 case DW_OP_reg6:
2747 return "DW_OP_reg6";
2748 case DW_OP_reg7:
2749 return "DW_OP_reg7";
2750 case DW_OP_reg8:
2751 return "DW_OP_reg8";
2752 case DW_OP_reg9:
2753 return "DW_OP_reg9";
2754 case DW_OP_reg10:
2755 return "DW_OP_reg10";
2756 case DW_OP_reg11:
2757 return "DW_OP_reg11";
2758 case DW_OP_reg12:
2759 return "DW_OP_reg12";
2760 case DW_OP_reg13:
2761 return "DW_OP_reg13";
2762 case DW_OP_reg14:
2763 return "DW_OP_reg14";
2764 case DW_OP_reg15:
2765 return "DW_OP_reg15";
2766 case DW_OP_reg16:
2767 return "DW_OP_reg16";
2768 case DW_OP_reg17:
2769 return "DW_OP_reg17";
2770 case DW_OP_reg18:
2771 return "DW_OP_reg18";
2772 case DW_OP_reg19:
2773 return "DW_OP_reg19";
2774 case DW_OP_reg20:
2775 return "DW_OP_reg20";
2776 case DW_OP_reg21:
2777 return "DW_OP_reg21";
2778 case DW_OP_reg22:
2779 return "DW_OP_reg22";
2780 case DW_OP_reg23:
2781 return "DW_OP_reg23";
2782 case DW_OP_reg24:
2783 return "DW_OP_reg24";
2784 case DW_OP_reg25:
2785 return "DW_OP_reg25";
2786 case DW_OP_reg26:
2787 return "DW_OP_reg26";
2788 case DW_OP_reg27:
2789 return "DW_OP_reg27";
2790 case DW_OP_reg28:
2791 return "DW_OP_reg28";
2792 case DW_OP_reg29:
2793 return "DW_OP_reg29";
2794 case DW_OP_reg30:
2795 return "DW_OP_reg30";
2796 case DW_OP_reg31:
2797 return "DW_OP_reg31";
2798 case DW_OP_breg0:
2799 return "DW_OP_breg0";
2800 case DW_OP_breg1:
2801 return "DW_OP_breg1";
2802 case DW_OP_breg2:
2803 return "DW_OP_breg2";
2804 case DW_OP_breg3:
2805 return "DW_OP_breg3";
2806 case DW_OP_breg4:
2807 return "DW_OP_breg4";
2808 case DW_OP_breg5:
2809 return "DW_OP_breg5";
2810 case DW_OP_breg6:
2811 return "DW_OP_breg6";
2812 case DW_OP_breg7:
2813 return "DW_OP_breg7";
2814 case DW_OP_breg8:
2815 return "DW_OP_breg8";
2816 case DW_OP_breg9:
2817 return "DW_OP_breg9";
2818 case DW_OP_breg10:
2819 return "DW_OP_breg10";
2820 case DW_OP_breg11:
2821 return "DW_OP_breg11";
2822 case DW_OP_breg12:
2823 return "DW_OP_breg12";
2824 case DW_OP_breg13:
2825 return "DW_OP_breg13";
2826 case DW_OP_breg14:
2827 return "DW_OP_breg14";
2828 case DW_OP_breg15:
2829 return "DW_OP_breg15";
2830 case DW_OP_breg16:
2831 return "DW_OP_breg16";
2832 case DW_OP_breg17:
2833 return "DW_OP_breg17";
2834 case DW_OP_breg18:
2835 return "DW_OP_breg18";
2836 case DW_OP_breg19:
2837 return "DW_OP_breg19";
2838 case DW_OP_breg20:
2839 return "DW_OP_breg20";
2840 case DW_OP_breg21:
2841 return "DW_OP_breg21";
2842 case DW_OP_breg22:
2843 return "DW_OP_breg22";
2844 case DW_OP_breg23:
2845 return "DW_OP_breg23";
2846 case DW_OP_breg24:
2847 return "DW_OP_breg24";
2848 case DW_OP_breg25:
2849 return "DW_OP_breg25";
2850 case DW_OP_breg26:
2851 return "DW_OP_breg26";
2852 case DW_OP_breg27:
2853 return "DW_OP_breg27";
2854 case DW_OP_breg28:
2855 return "DW_OP_breg28";
2856 case DW_OP_breg29:
2857 return "DW_OP_breg29";
2858 case DW_OP_breg30:
2859 return "DW_OP_breg30";
2860 case DW_OP_breg31:
2861 return "DW_OP_breg31";
2862 case DW_OP_regx:
2863 return "DW_OP_regx";
2864 case DW_OP_fbreg:
2865 return "DW_OP_fbreg";
2866 case DW_OP_bregx:
2867 return "DW_OP_bregx";
2868 case DW_OP_piece:
2869 return "DW_OP_piece";
2870 case DW_OP_deref_size:
2871 return "DW_OP_deref_size";
2872 case DW_OP_xderef_size:
2873 return "DW_OP_xderef_size";
2874 case DW_OP_nop:
2875 return "DW_OP_nop";
2876 case DW_OP_push_object_address:
2877 return "DW_OP_push_object_address";
2878 case DW_OP_call2:
2879 return "DW_OP_call2";
2880 case DW_OP_call4:
2881 return "DW_OP_call4";
2882 case DW_OP_call_ref:
2883 return "DW_OP_call_ref";
2884 case DW_OP_GNU_push_tls_address:
2885 return "DW_OP_GNU_push_tls_address";
2886 default:
2887 return "OP_<unknown>";
2888 }
2889 }
2890
2891 /* Return a pointer to a newly allocated location description. Location
2892 descriptions are simple expression terms that can be strung
2893 together to form more complicated location (address) descriptions. */
2894
2895 static inline dw_loc_descr_ref
2896 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2897 unsigned HOST_WIDE_INT oprnd2)
2898 {
2899 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2900
2901 descr->dw_loc_opc = op;
2902 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2903 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2904 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2905 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2906
2907 return descr;
2908 }
2909
2910
2911 /* Add a location description term to a location description expression. */
2912
2913 static inline void
2914 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2915 {
2916 dw_loc_descr_ref *d;
2917
2918 /* Find the end of the chain. */
2919 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2920 ;
2921
2922 *d = descr;
2923 }
2924
2925 /* Return the size of a location descriptor. */
2926
2927 static unsigned long
2928 size_of_loc_descr (dw_loc_descr_ref loc)
2929 {
2930 unsigned long size = 1;
2931
2932 switch (loc->dw_loc_opc)
2933 {
2934 case DW_OP_addr:
2935 case INTERNAL_DW_OP_tls_addr:
2936 size += DWARF2_ADDR_SIZE;
2937 break;
2938 case DW_OP_const1u:
2939 case DW_OP_const1s:
2940 size += 1;
2941 break;
2942 case DW_OP_const2u:
2943 case DW_OP_const2s:
2944 size += 2;
2945 break;
2946 case DW_OP_const4u:
2947 case DW_OP_const4s:
2948 size += 4;
2949 break;
2950 case DW_OP_const8u:
2951 case DW_OP_const8s:
2952 size += 8;
2953 break;
2954 case DW_OP_constu:
2955 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2956 break;
2957 case DW_OP_consts:
2958 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2959 break;
2960 case DW_OP_pick:
2961 size += 1;
2962 break;
2963 case DW_OP_plus_uconst:
2964 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2965 break;
2966 case DW_OP_skip:
2967 case DW_OP_bra:
2968 size += 2;
2969 break;
2970 case DW_OP_breg0:
2971 case DW_OP_breg1:
2972 case DW_OP_breg2:
2973 case DW_OP_breg3:
2974 case DW_OP_breg4:
2975 case DW_OP_breg5:
2976 case DW_OP_breg6:
2977 case DW_OP_breg7:
2978 case DW_OP_breg8:
2979 case DW_OP_breg9:
2980 case DW_OP_breg10:
2981 case DW_OP_breg11:
2982 case DW_OP_breg12:
2983 case DW_OP_breg13:
2984 case DW_OP_breg14:
2985 case DW_OP_breg15:
2986 case DW_OP_breg16:
2987 case DW_OP_breg17:
2988 case DW_OP_breg18:
2989 case DW_OP_breg19:
2990 case DW_OP_breg20:
2991 case DW_OP_breg21:
2992 case DW_OP_breg22:
2993 case DW_OP_breg23:
2994 case DW_OP_breg24:
2995 case DW_OP_breg25:
2996 case DW_OP_breg26:
2997 case DW_OP_breg27:
2998 case DW_OP_breg28:
2999 case DW_OP_breg29:
3000 case DW_OP_breg30:
3001 case DW_OP_breg31:
3002 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3003 break;
3004 case DW_OP_regx:
3005 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3006 break;
3007 case DW_OP_fbreg:
3008 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3009 break;
3010 case DW_OP_bregx:
3011 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3012 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3013 break;
3014 case DW_OP_piece:
3015 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3016 break;
3017 case DW_OP_deref_size:
3018 case DW_OP_xderef_size:
3019 size += 1;
3020 break;
3021 case DW_OP_call2:
3022 size += 2;
3023 break;
3024 case DW_OP_call4:
3025 size += 4;
3026 break;
3027 case DW_OP_call_ref:
3028 size += DWARF2_ADDR_SIZE;
3029 break;
3030 default:
3031 break;
3032 }
3033
3034 return size;
3035 }
3036
3037 /* Return the size of a series of location descriptors. */
3038
3039 static unsigned long
3040 size_of_locs (dw_loc_descr_ref loc)
3041 {
3042 unsigned long size;
3043
3044 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3045 {
3046 loc->dw_loc_addr = size;
3047 size += size_of_loc_descr (loc);
3048 }
3049
3050 return size;
3051 }
3052
3053 /* Output location description stack opcode's operands (if any). */
3054
3055 static void
3056 output_loc_operands (dw_loc_descr_ref loc)
3057 {
3058 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3059 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3060
3061 switch (loc->dw_loc_opc)
3062 {
3063 #ifdef DWARF2_DEBUGGING_INFO
3064 case DW_OP_addr:
3065 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3066 break;
3067 case DW_OP_const2u:
3068 case DW_OP_const2s:
3069 dw2_asm_output_data (2, val1->v.val_int, NULL);
3070 break;
3071 case DW_OP_const4u:
3072 case DW_OP_const4s:
3073 dw2_asm_output_data (4, val1->v.val_int, NULL);
3074 break;
3075 case DW_OP_const8u:
3076 case DW_OP_const8s:
3077 gcc_assert (HOST_BITS_PER_LONG >= 64);
3078 dw2_asm_output_data (8, val1->v.val_int, NULL);
3079 break;
3080 case DW_OP_skip:
3081 case DW_OP_bra:
3082 {
3083 int offset;
3084
3085 gcc_assert (val1->val_class == dw_val_class_loc);
3086 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3087
3088 dw2_asm_output_data (2, offset, NULL);
3089 }
3090 break;
3091 #else
3092 case DW_OP_addr:
3093 case DW_OP_const2u:
3094 case DW_OP_const2s:
3095 case DW_OP_const4u:
3096 case DW_OP_const4s:
3097 case DW_OP_const8u:
3098 case DW_OP_const8s:
3099 case DW_OP_skip:
3100 case DW_OP_bra:
3101 /* We currently don't make any attempt to make sure these are
3102 aligned properly like we do for the main unwind info, so
3103 don't support emitting things larger than a byte if we're
3104 only doing unwinding. */
3105 gcc_unreachable ();
3106 #endif
3107 case DW_OP_const1u:
3108 case DW_OP_const1s:
3109 dw2_asm_output_data (1, val1->v.val_int, NULL);
3110 break;
3111 case DW_OP_constu:
3112 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3113 break;
3114 case DW_OP_consts:
3115 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3116 break;
3117 case DW_OP_pick:
3118 dw2_asm_output_data (1, val1->v.val_int, NULL);
3119 break;
3120 case DW_OP_plus_uconst:
3121 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3122 break;
3123 case DW_OP_breg0:
3124 case DW_OP_breg1:
3125 case DW_OP_breg2:
3126 case DW_OP_breg3:
3127 case DW_OP_breg4:
3128 case DW_OP_breg5:
3129 case DW_OP_breg6:
3130 case DW_OP_breg7:
3131 case DW_OP_breg8:
3132 case DW_OP_breg9:
3133 case DW_OP_breg10:
3134 case DW_OP_breg11:
3135 case DW_OP_breg12:
3136 case DW_OP_breg13:
3137 case DW_OP_breg14:
3138 case DW_OP_breg15:
3139 case DW_OP_breg16:
3140 case DW_OP_breg17:
3141 case DW_OP_breg18:
3142 case DW_OP_breg19:
3143 case DW_OP_breg20:
3144 case DW_OP_breg21:
3145 case DW_OP_breg22:
3146 case DW_OP_breg23:
3147 case DW_OP_breg24:
3148 case DW_OP_breg25:
3149 case DW_OP_breg26:
3150 case DW_OP_breg27:
3151 case DW_OP_breg28:
3152 case DW_OP_breg29:
3153 case DW_OP_breg30:
3154 case DW_OP_breg31:
3155 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3156 break;
3157 case DW_OP_regx:
3158 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3159 break;
3160 case DW_OP_fbreg:
3161 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3162 break;
3163 case DW_OP_bregx:
3164 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3165 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3166 break;
3167 case DW_OP_piece:
3168 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3169 break;
3170 case DW_OP_deref_size:
3171 case DW_OP_xderef_size:
3172 dw2_asm_output_data (1, val1->v.val_int, NULL);
3173 break;
3174
3175 case INTERNAL_DW_OP_tls_addr:
3176 #ifdef ASM_OUTPUT_DWARF_DTPREL
3177 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3178 val1->v.val_addr);
3179 fputc ('\n', asm_out_file);
3180 #else
3181 gcc_unreachable ();
3182 #endif
3183 break;
3184
3185 default:
3186 /* Other codes have no operands. */
3187 break;
3188 }
3189 }
3190
3191 /* Output a sequence of location operations. */
3192
3193 static void
3194 output_loc_sequence (dw_loc_descr_ref loc)
3195 {
3196 for (; loc != NULL; loc = loc->dw_loc_next)
3197 {
3198 /* Output the opcode. */
3199 dw2_asm_output_data (1, loc->dw_loc_opc,
3200 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3201
3202 /* Output the operand(s) (if any). */
3203 output_loc_operands (loc);
3204 }
3205 }
3206
3207 /* This routine will generate the correct assembly data for a location
3208 description based on a cfi entry with a complex address. */
3209
3210 static void
3211 output_cfa_loc (dw_cfi_ref cfi)
3212 {
3213 dw_loc_descr_ref loc;
3214 unsigned long size;
3215
3216 /* Output the size of the block. */
3217 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3218 size = size_of_locs (loc);
3219 dw2_asm_output_data_uleb128 (size, NULL);
3220
3221 /* Now output the operations themselves. */
3222 output_loc_sequence (loc);
3223 }
3224
3225 /* This function builds a dwarf location descriptor sequence from
3226 a dw_cfa_location. */
3227
3228 static struct dw_loc_descr_struct *
3229 build_cfa_loc (dw_cfa_location *cfa)
3230 {
3231 struct dw_loc_descr_struct *head, *tmp;
3232
3233 gcc_assert (cfa->indirect);
3234
3235 if (cfa->base_offset)
3236 {
3237 if (cfa->reg <= 31)
3238 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3239 else
3240 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3241 }
3242 else if (cfa->reg <= 31)
3243 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3244 else
3245 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3246
3247 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3248 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3249 add_loc_descr (&head, tmp);
3250 if (cfa->offset != 0)
3251 {
3252 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3253 add_loc_descr (&head, tmp);
3254 }
3255
3256 return head;
3257 }
3258
3259 /* This function fills in aa dw_cfa_location structure from a dwarf location
3260 descriptor sequence. */
3261
3262 static void
3263 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3264 {
3265 struct dw_loc_descr_struct *ptr;
3266 cfa->offset = 0;
3267 cfa->base_offset = 0;
3268 cfa->indirect = 0;
3269 cfa->reg = -1;
3270
3271 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3272 {
3273 enum dwarf_location_atom op = ptr->dw_loc_opc;
3274
3275 switch (op)
3276 {
3277 case DW_OP_reg0:
3278 case DW_OP_reg1:
3279 case DW_OP_reg2:
3280 case DW_OP_reg3:
3281 case DW_OP_reg4:
3282 case DW_OP_reg5:
3283 case DW_OP_reg6:
3284 case DW_OP_reg7:
3285 case DW_OP_reg8:
3286 case DW_OP_reg9:
3287 case DW_OP_reg10:
3288 case DW_OP_reg11:
3289 case DW_OP_reg12:
3290 case DW_OP_reg13:
3291 case DW_OP_reg14:
3292 case DW_OP_reg15:
3293 case DW_OP_reg16:
3294 case DW_OP_reg17:
3295 case DW_OP_reg18:
3296 case DW_OP_reg19:
3297 case DW_OP_reg20:
3298 case DW_OP_reg21:
3299 case DW_OP_reg22:
3300 case DW_OP_reg23:
3301 case DW_OP_reg24:
3302 case DW_OP_reg25:
3303 case DW_OP_reg26:
3304 case DW_OP_reg27:
3305 case DW_OP_reg28:
3306 case DW_OP_reg29:
3307 case DW_OP_reg30:
3308 case DW_OP_reg31:
3309 cfa->reg = op - DW_OP_reg0;
3310 break;
3311 case DW_OP_regx:
3312 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3313 break;
3314 case DW_OP_breg0:
3315 case DW_OP_breg1:
3316 case DW_OP_breg2:
3317 case DW_OP_breg3:
3318 case DW_OP_breg4:
3319 case DW_OP_breg5:
3320 case DW_OP_breg6:
3321 case DW_OP_breg7:
3322 case DW_OP_breg8:
3323 case DW_OP_breg9:
3324 case DW_OP_breg10:
3325 case DW_OP_breg11:
3326 case DW_OP_breg12:
3327 case DW_OP_breg13:
3328 case DW_OP_breg14:
3329 case DW_OP_breg15:
3330 case DW_OP_breg16:
3331 case DW_OP_breg17:
3332 case DW_OP_breg18:
3333 case DW_OP_breg19:
3334 case DW_OP_breg20:
3335 case DW_OP_breg21:
3336 case DW_OP_breg22:
3337 case DW_OP_breg23:
3338 case DW_OP_breg24:
3339 case DW_OP_breg25:
3340 case DW_OP_breg26:
3341 case DW_OP_breg27:
3342 case DW_OP_breg28:
3343 case DW_OP_breg29:
3344 case DW_OP_breg30:
3345 case DW_OP_breg31:
3346 cfa->reg = op - DW_OP_breg0;
3347 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3348 break;
3349 case DW_OP_bregx:
3350 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3351 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3352 break;
3353 case DW_OP_deref:
3354 cfa->indirect = 1;
3355 break;
3356 case DW_OP_plus_uconst:
3357 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3358 break;
3359 default:
3360 internal_error ("DW_LOC_OP %s not implemented\n",
3361 dwarf_stack_op_name (ptr->dw_loc_opc));
3362 }
3363 }
3364 }
3365 #endif /* .debug_frame support */
3366 \f
3367 /* And now, the support for symbolic debugging information. */
3368 #ifdef DWARF2_DEBUGGING_INFO
3369
3370 /* .debug_str support. */
3371 static int output_indirect_string (void **, void *);
3372
3373 static void dwarf2out_init (const char *);
3374 static void dwarf2out_finish (const char *);
3375 static void dwarf2out_define (unsigned int, const char *);
3376 static void dwarf2out_undef (unsigned int, const char *);
3377 static void dwarf2out_start_source_file (unsigned, const char *);
3378 static void dwarf2out_end_source_file (unsigned);
3379 static void dwarf2out_begin_block (unsigned, unsigned);
3380 static void dwarf2out_end_block (unsigned, unsigned);
3381 static bool dwarf2out_ignore_block (tree);
3382 static void dwarf2out_global_decl (tree);
3383 static void dwarf2out_type_decl (tree, int);
3384 static void dwarf2out_imported_module_or_decl (tree, tree);
3385 static void dwarf2out_abstract_function (tree);
3386 static void dwarf2out_var_location (rtx);
3387 static void dwarf2out_begin_function (tree);
3388
3389 /* The debug hooks structure. */
3390
3391 const struct gcc_debug_hooks dwarf2_debug_hooks =
3392 {
3393 dwarf2out_init,
3394 dwarf2out_finish,
3395 dwarf2out_define,
3396 dwarf2out_undef,
3397 dwarf2out_start_source_file,
3398 dwarf2out_end_source_file,
3399 dwarf2out_begin_block,
3400 dwarf2out_end_block,
3401 dwarf2out_ignore_block,
3402 dwarf2out_source_line,
3403 dwarf2out_begin_prologue,
3404 debug_nothing_int_charstar, /* end_prologue */
3405 dwarf2out_end_epilogue,
3406 dwarf2out_begin_function,
3407 debug_nothing_int, /* end_function */
3408 dwarf2out_decl, /* function_decl */
3409 dwarf2out_global_decl,
3410 dwarf2out_type_decl, /* type_decl */
3411 dwarf2out_imported_module_or_decl,
3412 debug_nothing_tree, /* deferred_inline_function */
3413 /* The DWARF 2 backend tries to reduce debugging bloat by not
3414 emitting the abstract description of inline functions until
3415 something tries to reference them. */
3416 dwarf2out_abstract_function, /* outlining_inline_function */
3417 debug_nothing_rtx, /* label */
3418 debug_nothing_int, /* handle_pch */
3419 dwarf2out_var_location
3420 };
3421 #endif
3422 \f
3423 /* NOTE: In the comments in this file, many references are made to
3424 "Debugging Information Entries". This term is abbreviated as `DIE'
3425 throughout the remainder of this file. */
3426
3427 /* An internal representation of the DWARF output is built, and then
3428 walked to generate the DWARF debugging info. The walk of the internal
3429 representation is done after the entire program has been compiled.
3430 The types below are used to describe the internal representation. */
3431
3432 /* Various DIE's use offsets relative to the beginning of the
3433 .debug_info section to refer to each other. */
3434
3435 typedef long int dw_offset;
3436
3437 /* Define typedefs here to avoid circular dependencies. */
3438
3439 typedef struct dw_attr_struct *dw_attr_ref;
3440 typedef struct dw_line_info_struct *dw_line_info_ref;
3441 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3442 typedef struct pubname_struct *pubname_ref;
3443 typedef struct dw_ranges_struct *dw_ranges_ref;
3444
3445 /* Each entry in the line_info_table maintains the file and
3446 line number associated with the label generated for that
3447 entry. The label gives the PC value associated with
3448 the line number entry. */
3449
3450 typedef struct dw_line_info_struct GTY(())
3451 {
3452 unsigned long dw_file_num;
3453 unsigned long dw_line_num;
3454 }
3455 dw_line_info_entry;
3456
3457 /* Line information for functions in separate sections; each one gets its
3458 own sequence. */
3459 typedef struct dw_separate_line_info_struct GTY(())
3460 {
3461 unsigned long dw_file_num;
3462 unsigned long dw_line_num;
3463 unsigned long function;
3464 }
3465 dw_separate_line_info_entry;
3466
3467 /* Each DIE attribute has a field specifying the attribute kind,
3468 a link to the next attribute in the chain, and an attribute value.
3469 Attributes are typically linked below the DIE they modify. */
3470
3471 typedef struct dw_attr_struct GTY(())
3472 {
3473 enum dwarf_attribute dw_attr;
3474 dw_attr_ref dw_attr_next;
3475 dw_val_node dw_attr_val;
3476 }
3477 dw_attr_node;
3478
3479 /* The Debugging Information Entry (DIE) structure */
3480
3481 typedef struct die_struct GTY(())
3482 {
3483 enum dwarf_tag die_tag;
3484 char *die_symbol;
3485 dw_attr_ref die_attr;
3486 dw_die_ref die_parent;
3487 dw_die_ref die_child;
3488 dw_die_ref die_sib;
3489 dw_die_ref die_definition; /* ref from a specification to its definition */
3490 dw_offset die_offset;
3491 unsigned long die_abbrev;
3492 int die_mark;
3493 unsigned int decl_id;
3494 }
3495 die_node;
3496
3497 /* The pubname structure */
3498
3499 typedef struct pubname_struct GTY(())
3500 {
3501 dw_die_ref die;
3502 char *name;
3503 }
3504 pubname_entry;
3505
3506 struct dw_ranges_struct GTY(())
3507 {
3508 int block_num;
3509 };
3510
3511 /* The limbo die list structure. */
3512 typedef struct limbo_die_struct GTY(())
3513 {
3514 dw_die_ref die;
3515 tree created_for;
3516 struct limbo_die_struct *next;
3517 }
3518 limbo_die_node;
3519
3520 /* How to start an assembler comment. */
3521 #ifndef ASM_COMMENT_START
3522 #define ASM_COMMENT_START ";#"
3523 #endif
3524
3525 /* Define a macro which returns nonzero for a TYPE_DECL which was
3526 implicitly generated for a tagged type.
3527
3528 Note that unlike the gcc front end (which generates a NULL named
3529 TYPE_DECL node for each complete tagged type, each array type, and
3530 each function type node created) the g++ front end generates a
3531 _named_ TYPE_DECL node for each tagged type node created.
3532 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3533 generate a DW_TAG_typedef DIE for them. */
3534
3535 #define TYPE_DECL_IS_STUB(decl) \
3536 (DECL_NAME (decl) == NULL_TREE \
3537 || (DECL_ARTIFICIAL (decl) \
3538 && is_tagged_type (TREE_TYPE (decl)) \
3539 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3540 /* This is necessary for stub decls that \
3541 appear in nested inline functions. */ \
3542 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3543 && (decl_ultimate_origin (decl) \
3544 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3545
3546 /* Information concerning the compilation unit's programming
3547 language, and compiler version. */
3548
3549 /* Fixed size portion of the DWARF compilation unit header. */
3550 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3551 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3552
3553 /* Fixed size portion of public names info. */
3554 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3555
3556 /* Fixed size portion of the address range info. */
3557 #define DWARF_ARANGES_HEADER_SIZE \
3558 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3559 DWARF2_ADDR_SIZE * 2) \
3560 - DWARF_INITIAL_LENGTH_SIZE)
3561
3562 /* Size of padding portion in the address range info. It must be
3563 aligned to twice the pointer size. */
3564 #define DWARF_ARANGES_PAD_SIZE \
3565 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3566 DWARF2_ADDR_SIZE * 2) \
3567 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3568
3569 /* Use assembler line directives if available. */
3570 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3571 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3572 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3573 #else
3574 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3575 #endif
3576 #endif
3577
3578 /* Minimum line offset in a special line info. opcode.
3579 This value was chosen to give a reasonable range of values. */
3580 #define DWARF_LINE_BASE -10
3581
3582 /* First special line opcode - leave room for the standard opcodes. */
3583 #define DWARF_LINE_OPCODE_BASE 10
3584
3585 /* Range of line offsets in a special line info. opcode. */
3586 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3587
3588 /* Flag that indicates the initial value of the is_stmt_start flag.
3589 In the present implementation, we do not mark any lines as
3590 the beginning of a source statement, because that information
3591 is not made available by the GCC front-end. */
3592 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3593
3594 #ifdef DWARF2_DEBUGGING_INFO
3595 /* This location is used by calc_die_sizes() to keep track
3596 the offset of each DIE within the .debug_info section. */
3597 static unsigned long next_die_offset;
3598 #endif
3599
3600 /* Record the root of the DIE's built for the current compilation unit. */
3601 static GTY(()) dw_die_ref comp_unit_die;
3602
3603 /* A list of DIEs with a NULL parent waiting to be relocated. */
3604 static GTY(()) limbo_die_node *limbo_die_list;
3605
3606 /* Filenames referenced by this compilation unit. */
3607 static GTY(()) varray_type file_table;
3608 static GTY(()) varray_type file_table_emitted;
3609 static GTY(()) size_t file_table_last_lookup_index;
3610
3611 /* A hash table of references to DIE's that describe declarations.
3612 The key is a DECL_UID() which is a unique number identifying each decl. */
3613 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3614
3615 /* Node of the variable location list. */
3616 struct var_loc_node GTY ((chain_next ("%h.next")))
3617 {
3618 rtx GTY (()) var_loc_note;
3619 const char * GTY (()) label;
3620 struct var_loc_node * GTY (()) next;
3621 };
3622
3623 /* Variable location list. */
3624 struct var_loc_list_def GTY (())
3625 {
3626 struct var_loc_node * GTY (()) first;
3627
3628 /* Do not mark the last element of the chained list because
3629 it is marked through the chain. */
3630 struct var_loc_node * GTY ((skip ("%h"))) last;
3631
3632 /* DECL_UID of the variable decl. */
3633 unsigned int decl_id;
3634 };
3635 typedef struct var_loc_list_def var_loc_list;
3636
3637
3638 /* Table of decl location linked lists. */
3639 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3640
3641 /* A pointer to the base of a list of references to DIE's that
3642 are uniquely identified by their tag, presence/absence of
3643 children DIE's, and list of attribute/value pairs. */
3644 static GTY((length ("abbrev_die_table_allocated")))
3645 dw_die_ref *abbrev_die_table;
3646
3647 /* Number of elements currently allocated for abbrev_die_table. */
3648 static GTY(()) unsigned abbrev_die_table_allocated;
3649
3650 /* Number of elements in type_die_table currently in use. */
3651 static GTY(()) unsigned abbrev_die_table_in_use;
3652
3653 /* Size (in elements) of increments by which we may expand the
3654 abbrev_die_table. */
3655 #define ABBREV_DIE_TABLE_INCREMENT 256
3656
3657 /* A pointer to the base of a table that contains line information
3658 for each source code line in .text in the compilation unit. */
3659 static GTY((length ("line_info_table_allocated")))
3660 dw_line_info_ref line_info_table;
3661
3662 /* Number of elements currently allocated for line_info_table. */
3663 static GTY(()) unsigned line_info_table_allocated;
3664
3665 /* Number of elements in line_info_table currently in use. */
3666 static GTY(()) unsigned line_info_table_in_use;
3667
3668 /* A pointer to the base of a table that contains line information
3669 for each source code line outside of .text in the compilation unit. */
3670 static GTY ((length ("separate_line_info_table_allocated")))
3671 dw_separate_line_info_ref separate_line_info_table;
3672
3673 /* Number of elements currently allocated for separate_line_info_table. */
3674 static GTY(()) unsigned separate_line_info_table_allocated;
3675
3676 /* Number of elements in separate_line_info_table currently in use. */
3677 static GTY(()) unsigned separate_line_info_table_in_use;
3678
3679 /* Size (in elements) of increments by which we may expand the
3680 line_info_table. */
3681 #define LINE_INFO_TABLE_INCREMENT 1024
3682
3683 /* A pointer to the base of a table that contains a list of publicly
3684 accessible names. */
3685 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3686
3687 /* Number of elements currently allocated for pubname_table. */
3688 static GTY(()) unsigned pubname_table_allocated;
3689
3690 /* Number of elements in pubname_table currently in use. */
3691 static GTY(()) unsigned pubname_table_in_use;
3692
3693 /* Size (in elements) of increments by which we may expand the
3694 pubname_table. */
3695 #define PUBNAME_TABLE_INCREMENT 64
3696
3697 /* Array of dies for which we should generate .debug_arange info. */
3698 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3699
3700 /* Number of elements currently allocated for arange_table. */
3701 static GTY(()) unsigned arange_table_allocated;
3702
3703 /* Number of elements in arange_table currently in use. */
3704 static GTY(()) unsigned arange_table_in_use;
3705
3706 /* Size (in elements) of increments by which we may expand the
3707 arange_table. */
3708 #define ARANGE_TABLE_INCREMENT 64
3709
3710 /* Array of dies for which we should generate .debug_ranges info. */
3711 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3712
3713 /* Number of elements currently allocated for ranges_table. */
3714 static GTY(()) unsigned ranges_table_allocated;
3715
3716 /* Number of elements in ranges_table currently in use. */
3717 static GTY(()) unsigned ranges_table_in_use;
3718
3719 /* Size (in elements) of increments by which we may expand the
3720 ranges_table. */
3721 #define RANGES_TABLE_INCREMENT 64
3722
3723 /* Whether we have location lists that need outputting */
3724 static GTY(()) unsigned have_location_lists;
3725
3726 /* Unique label counter. */
3727 static GTY(()) unsigned int loclabel_num;
3728
3729 #ifdef DWARF2_DEBUGGING_INFO
3730 /* Record whether the function being analyzed contains inlined functions. */
3731 static int current_function_has_inlines;
3732 #endif
3733 #if 0 && defined (MIPS_DEBUGGING_INFO)
3734 static int comp_unit_has_inlines;
3735 #endif
3736
3737 /* Number of file tables emitted in maybe_emit_file(). */
3738 static GTY(()) int emitcount = 0;
3739
3740 /* Number of internal labels generated by gen_internal_sym(). */
3741 static GTY(()) int label_num;
3742
3743 #ifdef DWARF2_DEBUGGING_INFO
3744
3745 /* Forward declarations for functions defined in this file. */
3746
3747 static int is_pseudo_reg (rtx);
3748 static tree type_main_variant (tree);
3749 static int is_tagged_type (tree);
3750 static const char *dwarf_tag_name (unsigned);
3751 static const char *dwarf_attr_name (unsigned);
3752 static const char *dwarf_form_name (unsigned);
3753 #if 0
3754 static const char *dwarf_type_encoding_name (unsigned);
3755 #endif
3756 static tree decl_ultimate_origin (tree);
3757 static tree block_ultimate_origin (tree);
3758 static tree decl_class_context (tree);
3759 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3760 static inline enum dw_val_class AT_class (dw_attr_ref);
3761 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3762 static inline unsigned AT_flag (dw_attr_ref);
3763 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3764 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3765 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3766 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3767 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3768 unsigned long);
3769 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3770 unsigned int, unsigned char *);
3771 static hashval_t debug_str_do_hash (const void *);
3772 static int debug_str_eq (const void *, const void *);
3773 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3774 static inline const char *AT_string (dw_attr_ref);
3775 static int AT_string_form (dw_attr_ref);
3776 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3777 static void add_AT_specification (dw_die_ref, dw_die_ref);
3778 static inline dw_die_ref AT_ref (dw_attr_ref);
3779 static inline int AT_ref_external (dw_attr_ref);
3780 static inline void set_AT_ref_external (dw_attr_ref, int);
3781 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3782 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3783 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3784 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3785 dw_loc_list_ref);
3786 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3787 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3788 static inline rtx AT_addr (dw_attr_ref);
3789 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3790 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3791 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3792 unsigned HOST_WIDE_INT);
3793 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3794 unsigned long);
3795 static inline const char *AT_lbl (dw_attr_ref);
3796 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3797 static const char *get_AT_low_pc (dw_die_ref);
3798 static const char *get_AT_hi_pc (dw_die_ref);
3799 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3800 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3801 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3802 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3803 static bool is_c_family (void);
3804 static bool is_cxx (void);
3805 static bool is_java (void);
3806 static bool is_fortran (void);
3807 static bool is_ada (void);
3808 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3809 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3810 static inline void free_die (dw_die_ref);
3811 static void remove_children (dw_die_ref);
3812 static void add_child_die (dw_die_ref, dw_die_ref);
3813 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3814 static dw_die_ref lookup_type_die (tree);
3815 static void equate_type_number_to_die (tree, dw_die_ref);
3816 static hashval_t decl_die_table_hash (const void *);
3817 static int decl_die_table_eq (const void *, const void *);
3818 static dw_die_ref lookup_decl_die (tree);
3819 static hashval_t decl_loc_table_hash (const void *);
3820 static int decl_loc_table_eq (const void *, const void *);
3821 static var_loc_list *lookup_decl_loc (tree);
3822 static void equate_decl_number_to_die (tree, dw_die_ref);
3823 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3824 static void print_spaces (FILE *);
3825 static void print_die (dw_die_ref, FILE *);
3826 static void print_dwarf_line_table (FILE *);
3827 static void reverse_die_lists (dw_die_ref);
3828 static void reverse_all_dies (dw_die_ref);
3829 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3830 static dw_die_ref pop_compile_unit (dw_die_ref);
3831 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3832 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3833 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3834 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3835 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3836 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3837 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3838 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3839 static void compute_section_prefix (dw_die_ref);
3840 static int is_type_die (dw_die_ref);
3841 static int is_comdat_die (dw_die_ref);
3842 static int is_symbol_die (dw_die_ref);
3843 static void assign_symbol_names (dw_die_ref);
3844 static void break_out_includes (dw_die_ref);
3845 static hashval_t htab_cu_hash (const void *);
3846 static int htab_cu_eq (const void *, const void *);
3847 static void htab_cu_del (void *);
3848 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3849 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3850 static void add_sibling_attributes (dw_die_ref);
3851 static void build_abbrev_table (dw_die_ref);
3852 static void output_location_lists (dw_die_ref);
3853 static int constant_size (long unsigned);
3854 static unsigned long size_of_die (dw_die_ref);
3855 static void calc_die_sizes (dw_die_ref);
3856 static void mark_dies (dw_die_ref);
3857 static void unmark_dies (dw_die_ref);
3858 static void unmark_all_dies (dw_die_ref);
3859 static unsigned long size_of_pubnames (void);
3860 static unsigned long size_of_aranges (void);
3861 static enum dwarf_form value_format (dw_attr_ref);
3862 static void output_value_format (dw_attr_ref);
3863 static void output_abbrev_section (void);
3864 static void output_die_symbol (dw_die_ref);
3865 static void output_die (dw_die_ref);
3866 static void output_compilation_unit_header (void);
3867 static void output_comp_unit (dw_die_ref, int);
3868 static const char *dwarf2_name (tree, int);
3869 static void add_pubname (tree, dw_die_ref);
3870 static void output_pubnames (void);
3871 static void add_arange (tree, dw_die_ref);
3872 static void output_aranges (void);
3873 static unsigned int add_ranges (tree);
3874 static void output_ranges (void);
3875 static void output_line_info (void);
3876 static void output_file_names (void);
3877 static dw_die_ref base_type_die (tree);
3878 static tree root_type (tree);
3879 static int is_base_type (tree);
3880 static bool is_subrange_type (tree);
3881 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3882 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3883 static int type_is_enum (tree);
3884 static unsigned int dbx_reg_number (rtx);
3885 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3886 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3887 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3888 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3889 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
3890 static int is_based_loc (rtx);
3891 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
3892 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
3893 static dw_loc_descr_ref loc_descriptor (rtx, bool);
3894 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
3895 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
3896 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3897 static tree field_type (tree);
3898 static unsigned int simple_type_align_in_bits (tree);
3899 static unsigned int simple_decl_align_in_bits (tree);
3900 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3901 static HOST_WIDE_INT field_byte_offset (tree);
3902 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3903 dw_loc_descr_ref);
3904 static void add_data_member_location_attribute (dw_die_ref, tree);
3905 static void add_const_value_attribute (dw_die_ref, rtx);
3906 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3907 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3908 static void insert_float (rtx, unsigned char *);
3909 static rtx rtl_for_decl_location (tree);
3910 static void add_location_or_const_value_attribute (dw_die_ref, tree,
3911 enum dwarf_attribute);
3912 static void tree_add_const_value_attribute (dw_die_ref, tree);
3913 static void add_name_attribute (dw_die_ref, const char *);
3914 static void add_comp_dir_attribute (dw_die_ref);
3915 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3916 static void add_subscript_info (dw_die_ref, tree);
3917 static void add_byte_size_attribute (dw_die_ref, tree);
3918 static void add_bit_offset_attribute (dw_die_ref, tree);
3919 static void add_bit_size_attribute (dw_die_ref, tree);
3920 static void add_prototyped_attribute (dw_die_ref, tree);
3921 static void add_abstract_origin_attribute (dw_die_ref, tree);
3922 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3923 static void add_src_coords_attributes (dw_die_ref, tree);
3924 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3925 static void push_decl_scope (tree);
3926 static void pop_decl_scope (void);
3927 static dw_die_ref scope_die_for (tree, dw_die_ref);
3928 static inline int local_scope_p (dw_die_ref);
3929 static inline int class_or_namespace_scope_p (dw_die_ref);
3930 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3931 static const char *type_tag (tree);
3932 static tree member_declared_type (tree);
3933 #if 0
3934 static const char *decl_start_label (tree);
3935 #endif
3936 static void gen_array_type_die (tree, dw_die_ref);
3937 static void gen_set_type_die (tree, dw_die_ref);
3938 #if 0
3939 static void gen_entry_point_die (tree, dw_die_ref);
3940 #endif
3941 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
3942 static void gen_inlined_structure_type_die (tree, dw_die_ref);
3943 static void gen_inlined_union_type_die (tree, dw_die_ref);
3944 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3945 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
3946 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3947 static void gen_formal_types_die (tree, dw_die_ref);
3948 static void gen_subprogram_die (tree, dw_die_ref);
3949 static void gen_variable_die (tree, dw_die_ref);
3950 static void gen_label_die (tree, dw_die_ref);
3951 static void gen_lexical_block_die (tree, dw_die_ref, int);
3952 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3953 static void gen_field_die (tree, dw_die_ref);
3954 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3955 static dw_die_ref gen_compile_unit_die (const char *);
3956 static void gen_string_type_die (tree, dw_die_ref);
3957 static void gen_inheritance_die (tree, tree, dw_die_ref);
3958 static void gen_member_die (tree, dw_die_ref);
3959 static void gen_struct_or_union_type_die (tree, dw_die_ref);
3960 static void gen_subroutine_type_die (tree, dw_die_ref);
3961 static void gen_typedef_die (tree, dw_die_ref);
3962 static void gen_type_die (tree, dw_die_ref);
3963 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
3964 static void gen_block_die (tree, dw_die_ref, int);
3965 static void decls_for_scope (tree, dw_die_ref, int);
3966 static int is_redundant_typedef (tree);
3967 static void gen_namespace_die (tree);
3968 static void gen_decl_die (tree, dw_die_ref);
3969 static dw_die_ref force_decl_die (tree);
3970 static dw_die_ref force_type_die (tree);
3971 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3972 static void declare_in_namespace (tree, dw_die_ref);
3973 static unsigned lookup_filename (const char *);
3974 static void init_file_table (void);
3975 static void retry_incomplete_types (void);
3976 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3977 static void splice_child_die (dw_die_ref, dw_die_ref);
3978 static int file_info_cmp (const void *, const void *);
3979 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3980 const char *, const char *, unsigned);
3981 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
3982 const char *, const char *,
3983 const char *);
3984 static void output_loc_list (dw_loc_list_ref);
3985 static char *gen_internal_sym (const char *);
3986
3987 static void prune_unmark_dies (dw_die_ref);
3988 static void prune_unused_types_mark (dw_die_ref, int);
3989 static void prune_unused_types_walk (dw_die_ref);
3990 static void prune_unused_types_walk_attribs (dw_die_ref);
3991 static void prune_unused_types_prune (dw_die_ref);
3992 static void prune_unused_types (void);
3993 static int maybe_emit_file (int);
3994
3995 /* Section names used to hold DWARF debugging information. */
3996 #ifndef DEBUG_INFO_SECTION
3997 #define DEBUG_INFO_SECTION ".debug_info"
3998 #endif
3999 #ifndef DEBUG_ABBREV_SECTION
4000 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4001 #endif
4002 #ifndef DEBUG_ARANGES_SECTION
4003 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4004 #endif
4005 #ifndef DEBUG_MACINFO_SECTION
4006 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4007 #endif
4008 #ifndef DEBUG_LINE_SECTION
4009 #define DEBUG_LINE_SECTION ".debug_line"
4010 #endif
4011 #ifndef DEBUG_LOC_SECTION
4012 #define DEBUG_LOC_SECTION ".debug_loc"
4013 #endif
4014 #ifndef DEBUG_PUBNAMES_SECTION
4015 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4016 #endif
4017 #ifndef DEBUG_STR_SECTION
4018 #define DEBUG_STR_SECTION ".debug_str"
4019 #endif
4020 #ifndef DEBUG_RANGES_SECTION
4021 #define DEBUG_RANGES_SECTION ".debug_ranges"
4022 #endif
4023
4024 /* Standard ELF section names for compiled code and data. */
4025 #ifndef TEXT_SECTION_NAME
4026 #define TEXT_SECTION_NAME ".text"
4027 #endif
4028
4029 /* Section flags for .debug_str section. */
4030 #define DEBUG_STR_SECTION_FLAGS \
4031 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4032 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4033 : SECTION_DEBUG)
4034
4035 /* Labels we insert at beginning sections we can reference instead of
4036 the section names themselves. */
4037
4038 #ifndef TEXT_SECTION_LABEL
4039 #define TEXT_SECTION_LABEL "Ltext"
4040 #endif
4041 #ifndef DEBUG_LINE_SECTION_LABEL
4042 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4043 #endif
4044 #ifndef DEBUG_INFO_SECTION_LABEL
4045 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4046 #endif
4047 #ifndef DEBUG_ABBREV_SECTION_LABEL
4048 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4049 #endif
4050 #ifndef DEBUG_LOC_SECTION_LABEL
4051 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4052 #endif
4053 #ifndef DEBUG_RANGES_SECTION_LABEL
4054 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4055 #endif
4056 #ifndef DEBUG_MACINFO_SECTION_LABEL
4057 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4058 #endif
4059
4060 /* Definitions of defaults for formats and names of various special
4061 (artificial) labels which may be generated within this file (when the -g
4062 options is used and DWARF2_DEBUGGING_INFO is in effect.
4063 If necessary, these may be overridden from within the tm.h file, but
4064 typically, overriding these defaults is unnecessary. */
4065
4066 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4067 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4068 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4069 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4070 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4071 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4072 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4073 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4074
4075 #ifndef TEXT_END_LABEL
4076 #define TEXT_END_LABEL "Letext"
4077 #endif
4078 #ifndef BLOCK_BEGIN_LABEL
4079 #define BLOCK_BEGIN_LABEL "LBB"
4080 #endif
4081 #ifndef BLOCK_END_LABEL
4082 #define BLOCK_END_LABEL "LBE"
4083 #endif
4084 #ifndef LINE_CODE_LABEL
4085 #define LINE_CODE_LABEL "LM"
4086 #endif
4087 #ifndef SEPARATE_LINE_CODE_LABEL
4088 #define SEPARATE_LINE_CODE_LABEL "LSM"
4089 #endif
4090 \f
4091 /* We allow a language front-end to designate a function that is to be
4092 called to "demangle" any name before it it put into a DIE. */
4093
4094 static const char *(*demangle_name_func) (const char *);
4095
4096 void
4097 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4098 {
4099 demangle_name_func = func;
4100 }
4101
4102 /* Test if rtl node points to a pseudo register. */
4103
4104 static inline int
4105 is_pseudo_reg (rtx rtl)
4106 {
4107 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4108 || (GET_CODE (rtl) == SUBREG
4109 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4110 }
4111
4112 /* Return a reference to a type, with its const and volatile qualifiers
4113 removed. */
4114
4115 static inline tree
4116 type_main_variant (tree type)
4117 {
4118 type = TYPE_MAIN_VARIANT (type);
4119
4120 /* ??? There really should be only one main variant among any group of
4121 variants of a given type (and all of the MAIN_VARIANT values for all
4122 members of the group should point to that one type) but sometimes the C
4123 front-end messes this up for array types, so we work around that bug
4124 here. */
4125 if (TREE_CODE (type) == ARRAY_TYPE)
4126 while (type != TYPE_MAIN_VARIANT (type))
4127 type = TYPE_MAIN_VARIANT (type);
4128
4129 return type;
4130 }
4131
4132 /* Return nonzero if the given type node represents a tagged type. */
4133
4134 static inline int
4135 is_tagged_type (tree type)
4136 {
4137 enum tree_code code = TREE_CODE (type);
4138
4139 return (code == RECORD_TYPE || code == UNION_TYPE
4140 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4141 }
4142
4143 /* Convert a DIE tag into its string name. */
4144
4145 static const char *
4146 dwarf_tag_name (unsigned int tag)
4147 {
4148 switch (tag)
4149 {
4150 case DW_TAG_padding:
4151 return "DW_TAG_padding";
4152 case DW_TAG_array_type:
4153 return "DW_TAG_array_type";
4154 case DW_TAG_class_type:
4155 return "DW_TAG_class_type";
4156 case DW_TAG_entry_point:
4157 return "DW_TAG_entry_point";
4158 case DW_TAG_enumeration_type:
4159 return "DW_TAG_enumeration_type";
4160 case DW_TAG_formal_parameter:
4161 return "DW_TAG_formal_parameter";
4162 case DW_TAG_imported_declaration:
4163 return "DW_TAG_imported_declaration";
4164 case DW_TAG_label:
4165 return "DW_TAG_label";
4166 case DW_TAG_lexical_block:
4167 return "DW_TAG_lexical_block";
4168 case DW_TAG_member:
4169 return "DW_TAG_member";
4170 case DW_TAG_pointer_type:
4171 return "DW_TAG_pointer_type";
4172 case DW_TAG_reference_type:
4173 return "DW_TAG_reference_type";
4174 case DW_TAG_compile_unit:
4175 return "DW_TAG_compile_unit";
4176 case DW_TAG_string_type:
4177 return "DW_TAG_string_type";
4178 case DW_TAG_structure_type:
4179 return "DW_TAG_structure_type";
4180 case DW_TAG_subroutine_type:
4181 return "DW_TAG_subroutine_type";
4182 case DW_TAG_typedef:
4183 return "DW_TAG_typedef";
4184 case DW_TAG_union_type:
4185 return "DW_TAG_union_type";
4186 case DW_TAG_unspecified_parameters:
4187 return "DW_TAG_unspecified_parameters";
4188 case DW_TAG_variant:
4189 return "DW_TAG_variant";
4190 case DW_TAG_common_block:
4191 return "DW_TAG_common_block";
4192 case DW_TAG_common_inclusion:
4193 return "DW_TAG_common_inclusion";
4194 case DW_TAG_inheritance:
4195 return "DW_TAG_inheritance";
4196 case DW_TAG_inlined_subroutine:
4197 return "DW_TAG_inlined_subroutine";
4198 case DW_TAG_module:
4199 return "DW_TAG_module";
4200 case DW_TAG_ptr_to_member_type:
4201 return "DW_TAG_ptr_to_member_type";
4202 case DW_TAG_set_type:
4203 return "DW_TAG_set_type";
4204 case DW_TAG_subrange_type:
4205 return "DW_TAG_subrange_type";
4206 case DW_TAG_with_stmt:
4207 return "DW_TAG_with_stmt";
4208 case DW_TAG_access_declaration:
4209 return "DW_TAG_access_declaration";
4210 case DW_TAG_base_type:
4211 return "DW_TAG_base_type";
4212 case DW_TAG_catch_block:
4213 return "DW_TAG_catch_block";
4214 case DW_TAG_const_type:
4215 return "DW_TAG_const_type";
4216 case DW_TAG_constant:
4217 return "DW_TAG_constant";
4218 case DW_TAG_enumerator:
4219 return "DW_TAG_enumerator";
4220 case DW_TAG_file_type:
4221 return "DW_TAG_file_type";
4222 case DW_TAG_friend:
4223 return "DW_TAG_friend";
4224 case DW_TAG_namelist:
4225 return "DW_TAG_namelist";
4226 case DW_TAG_namelist_item:
4227 return "DW_TAG_namelist_item";
4228 case DW_TAG_namespace:
4229 return "DW_TAG_namespace";
4230 case DW_TAG_packed_type:
4231 return "DW_TAG_packed_type";
4232 case DW_TAG_subprogram:
4233 return "DW_TAG_subprogram";
4234 case DW_TAG_template_type_param:
4235 return "DW_TAG_template_type_param";
4236 case DW_TAG_template_value_param:
4237 return "DW_TAG_template_value_param";
4238 case DW_TAG_thrown_type:
4239 return "DW_TAG_thrown_type";
4240 case DW_TAG_try_block:
4241 return "DW_TAG_try_block";
4242 case DW_TAG_variant_part:
4243 return "DW_TAG_variant_part";
4244 case DW_TAG_variable:
4245 return "DW_TAG_variable";
4246 case DW_TAG_volatile_type:
4247 return "DW_TAG_volatile_type";
4248 case DW_TAG_imported_module:
4249 return "DW_TAG_imported_module";
4250 case DW_TAG_MIPS_loop:
4251 return "DW_TAG_MIPS_loop";
4252 case DW_TAG_format_label:
4253 return "DW_TAG_format_label";
4254 case DW_TAG_function_template:
4255 return "DW_TAG_function_template";
4256 case DW_TAG_class_template:
4257 return "DW_TAG_class_template";
4258 case DW_TAG_GNU_BINCL:
4259 return "DW_TAG_GNU_BINCL";
4260 case DW_TAG_GNU_EINCL:
4261 return "DW_TAG_GNU_EINCL";
4262 default:
4263 return "DW_TAG_<unknown>";
4264 }
4265 }
4266
4267 /* Convert a DWARF attribute code into its string name. */
4268
4269 static const char *
4270 dwarf_attr_name (unsigned int attr)
4271 {
4272 switch (attr)
4273 {
4274 case DW_AT_sibling:
4275 return "DW_AT_sibling";
4276 case DW_AT_location:
4277 return "DW_AT_location";
4278 case DW_AT_name:
4279 return "DW_AT_name";
4280 case DW_AT_ordering:
4281 return "DW_AT_ordering";
4282 case DW_AT_subscr_data:
4283 return "DW_AT_subscr_data";
4284 case DW_AT_byte_size:
4285 return "DW_AT_byte_size";
4286 case DW_AT_bit_offset:
4287 return "DW_AT_bit_offset";
4288 case DW_AT_bit_size:
4289 return "DW_AT_bit_size";
4290 case DW_AT_element_list:
4291 return "DW_AT_element_list";
4292 case DW_AT_stmt_list:
4293 return "DW_AT_stmt_list";
4294 case DW_AT_low_pc:
4295 return "DW_AT_low_pc";
4296 case DW_AT_high_pc:
4297 return "DW_AT_high_pc";
4298 case DW_AT_language:
4299 return "DW_AT_language";
4300 case DW_AT_member:
4301 return "DW_AT_member";
4302 case DW_AT_discr:
4303 return "DW_AT_discr";
4304 case DW_AT_discr_value:
4305 return "DW_AT_discr_value";
4306 case DW_AT_visibility:
4307 return "DW_AT_visibility";
4308 case DW_AT_import:
4309 return "DW_AT_import";
4310 case DW_AT_string_length:
4311 return "DW_AT_string_length";
4312 case DW_AT_common_reference:
4313 return "DW_AT_common_reference";
4314 case DW_AT_comp_dir:
4315 return "DW_AT_comp_dir";
4316 case DW_AT_const_value:
4317 return "DW_AT_const_value";
4318 case DW_AT_containing_type:
4319 return "DW_AT_containing_type";
4320 case DW_AT_default_value:
4321 return "DW_AT_default_value";
4322 case DW_AT_inline:
4323 return "DW_AT_inline";
4324 case DW_AT_is_optional:
4325 return "DW_AT_is_optional";
4326 case DW_AT_lower_bound:
4327 return "DW_AT_lower_bound";
4328 case DW_AT_producer:
4329 return "DW_AT_producer";
4330 case DW_AT_prototyped:
4331 return "DW_AT_prototyped";
4332 case DW_AT_return_addr:
4333 return "DW_AT_return_addr";
4334 case DW_AT_start_scope:
4335 return "DW_AT_start_scope";
4336 case DW_AT_stride_size:
4337 return "DW_AT_stride_size";
4338 case DW_AT_upper_bound:
4339 return "DW_AT_upper_bound";
4340 case DW_AT_abstract_origin:
4341 return "DW_AT_abstract_origin";
4342 case DW_AT_accessibility:
4343 return "DW_AT_accessibility";
4344 case DW_AT_address_class:
4345 return "DW_AT_address_class";
4346 case DW_AT_artificial:
4347 return "DW_AT_artificial";
4348 case DW_AT_base_types:
4349 return "DW_AT_base_types";
4350 case DW_AT_calling_convention:
4351 return "DW_AT_calling_convention";
4352 case DW_AT_count:
4353 return "DW_AT_count";
4354 case DW_AT_data_member_location:
4355 return "DW_AT_data_member_location";
4356 case DW_AT_decl_column:
4357 return "DW_AT_decl_column";
4358 case DW_AT_decl_file:
4359 return "DW_AT_decl_file";
4360 case DW_AT_decl_line:
4361 return "DW_AT_decl_line";
4362 case DW_AT_declaration:
4363 return "DW_AT_declaration";
4364 case DW_AT_discr_list:
4365 return "DW_AT_discr_list";
4366 case DW_AT_encoding:
4367 return "DW_AT_encoding";
4368 case DW_AT_external:
4369 return "DW_AT_external";
4370 case DW_AT_frame_base:
4371 return "DW_AT_frame_base";
4372 case DW_AT_friend:
4373 return "DW_AT_friend";
4374 case DW_AT_identifier_case:
4375 return "DW_AT_identifier_case";
4376 case DW_AT_macro_info:
4377 return "DW_AT_macro_info";
4378 case DW_AT_namelist_items:
4379 return "DW_AT_namelist_items";
4380 case DW_AT_priority:
4381 return "DW_AT_priority";
4382 case DW_AT_segment:
4383 return "DW_AT_segment";
4384 case DW_AT_specification:
4385 return "DW_AT_specification";
4386 case DW_AT_static_link:
4387 return "DW_AT_static_link";
4388 case DW_AT_type:
4389 return "DW_AT_type";
4390 case DW_AT_use_location:
4391 return "DW_AT_use_location";
4392 case DW_AT_variable_parameter:
4393 return "DW_AT_variable_parameter";
4394 case DW_AT_virtuality:
4395 return "DW_AT_virtuality";
4396 case DW_AT_vtable_elem_location:
4397 return "DW_AT_vtable_elem_location";
4398
4399 case DW_AT_allocated:
4400 return "DW_AT_allocated";
4401 case DW_AT_associated:
4402 return "DW_AT_associated";
4403 case DW_AT_data_location:
4404 return "DW_AT_data_location";
4405 case DW_AT_stride:
4406 return "DW_AT_stride";
4407 case DW_AT_entry_pc:
4408 return "DW_AT_entry_pc";
4409 case DW_AT_use_UTF8:
4410 return "DW_AT_use_UTF8";
4411 case DW_AT_extension:
4412 return "DW_AT_extension";
4413 case DW_AT_ranges:
4414 return "DW_AT_ranges";
4415 case DW_AT_trampoline:
4416 return "DW_AT_trampoline";
4417 case DW_AT_call_column:
4418 return "DW_AT_call_column";
4419 case DW_AT_call_file:
4420 return "DW_AT_call_file";
4421 case DW_AT_call_line:
4422 return "DW_AT_call_line";
4423
4424 case DW_AT_MIPS_fde:
4425 return "DW_AT_MIPS_fde";
4426 case DW_AT_MIPS_loop_begin:
4427 return "DW_AT_MIPS_loop_begin";
4428 case DW_AT_MIPS_tail_loop_begin:
4429 return "DW_AT_MIPS_tail_loop_begin";
4430 case DW_AT_MIPS_epilog_begin:
4431 return "DW_AT_MIPS_epilog_begin";
4432 case DW_AT_MIPS_loop_unroll_factor:
4433 return "DW_AT_MIPS_loop_unroll_factor";
4434 case DW_AT_MIPS_software_pipeline_depth:
4435 return "DW_AT_MIPS_software_pipeline_depth";
4436 case DW_AT_MIPS_linkage_name:
4437 return "DW_AT_MIPS_linkage_name";
4438 case DW_AT_MIPS_stride:
4439 return "DW_AT_MIPS_stride";
4440 case DW_AT_MIPS_abstract_name:
4441 return "DW_AT_MIPS_abstract_name";
4442 case DW_AT_MIPS_clone_origin:
4443 return "DW_AT_MIPS_clone_origin";
4444 case DW_AT_MIPS_has_inlines:
4445 return "DW_AT_MIPS_has_inlines";
4446
4447 case DW_AT_sf_names:
4448 return "DW_AT_sf_names";
4449 case DW_AT_src_info:
4450 return "DW_AT_src_info";
4451 case DW_AT_mac_info:
4452 return "DW_AT_mac_info";
4453 case DW_AT_src_coords:
4454 return "DW_AT_src_coords";
4455 case DW_AT_body_begin:
4456 return "DW_AT_body_begin";
4457 case DW_AT_body_end:
4458 return "DW_AT_body_end";
4459 case DW_AT_GNU_vector:
4460 return "DW_AT_GNU_vector";
4461
4462 case DW_AT_VMS_rtnbeg_pd_address:
4463 return "DW_AT_VMS_rtnbeg_pd_address";
4464
4465 default:
4466 return "DW_AT_<unknown>";
4467 }
4468 }
4469
4470 /* Convert a DWARF value form code into its string name. */
4471
4472 static const char *
4473 dwarf_form_name (unsigned int form)
4474 {
4475 switch (form)
4476 {
4477 case DW_FORM_addr:
4478 return "DW_FORM_addr";
4479 case DW_FORM_block2:
4480 return "DW_FORM_block2";
4481 case DW_FORM_block4:
4482 return "DW_FORM_block4";
4483 case DW_FORM_data2:
4484 return "DW_FORM_data2";
4485 case DW_FORM_data4:
4486 return "DW_FORM_data4";
4487 case DW_FORM_data8:
4488 return "DW_FORM_data8";
4489 case DW_FORM_string:
4490 return "DW_FORM_string";
4491 case DW_FORM_block:
4492 return "DW_FORM_block";
4493 case DW_FORM_block1:
4494 return "DW_FORM_block1";
4495 case DW_FORM_data1:
4496 return "DW_FORM_data1";
4497 case DW_FORM_flag:
4498 return "DW_FORM_flag";
4499 case DW_FORM_sdata:
4500 return "DW_FORM_sdata";
4501 case DW_FORM_strp:
4502 return "DW_FORM_strp";
4503 case DW_FORM_udata:
4504 return "DW_FORM_udata";
4505 case DW_FORM_ref_addr:
4506 return "DW_FORM_ref_addr";
4507 case DW_FORM_ref1:
4508 return "DW_FORM_ref1";
4509 case DW_FORM_ref2:
4510 return "DW_FORM_ref2";
4511 case DW_FORM_ref4:
4512 return "DW_FORM_ref4";
4513 case DW_FORM_ref8:
4514 return "DW_FORM_ref8";
4515 case DW_FORM_ref_udata:
4516 return "DW_FORM_ref_udata";
4517 case DW_FORM_indirect:
4518 return "DW_FORM_indirect";
4519 default:
4520 return "DW_FORM_<unknown>";
4521 }
4522 }
4523
4524 /* Convert a DWARF type code into its string name. */
4525
4526 #if 0
4527 static const char *
4528 dwarf_type_encoding_name (unsigned enc)
4529 {
4530 switch (enc)
4531 {
4532 case DW_ATE_address:
4533 return "DW_ATE_address";
4534 case DW_ATE_boolean:
4535 return "DW_ATE_boolean";
4536 case DW_ATE_complex_float:
4537 return "DW_ATE_complex_float";
4538 case DW_ATE_float:
4539 return "DW_ATE_float";
4540 case DW_ATE_signed:
4541 return "DW_ATE_signed";
4542 case DW_ATE_signed_char:
4543 return "DW_ATE_signed_char";
4544 case DW_ATE_unsigned:
4545 return "DW_ATE_unsigned";
4546 case DW_ATE_unsigned_char:
4547 return "DW_ATE_unsigned_char";
4548 default:
4549 return "DW_ATE_<unknown>";
4550 }
4551 }
4552 #endif
4553 \f
4554 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4555 instance of an inlined instance of a decl which is local to an inline
4556 function, so we have to trace all of the way back through the origin chain
4557 to find out what sort of node actually served as the original seed for the
4558 given block. */
4559
4560 static tree
4561 decl_ultimate_origin (tree decl)
4562 {
4563 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4564 nodes in the function to point to themselves; ignore that if
4565 we're trying to output the abstract instance of this function. */
4566 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4567 return NULL_TREE;
4568
4569 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4570 most distant ancestor, this should never happen. */
4571 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4572
4573 return DECL_ABSTRACT_ORIGIN (decl);
4574 }
4575
4576 /* Determine the "ultimate origin" of a block. The block may be an inlined
4577 instance of an inlined instance of a block which is local to an inline
4578 function, so we have to trace all of the way back through the origin chain
4579 to find out what sort of node actually served as the original seed for the
4580 given block. */
4581
4582 static tree
4583 block_ultimate_origin (tree block)
4584 {
4585 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4586
4587 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4588 nodes in the function to point to themselves; ignore that if
4589 we're trying to output the abstract instance of this function. */
4590 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4591 return NULL_TREE;
4592
4593 if (immediate_origin == NULL_TREE)
4594 return NULL_TREE;
4595 else
4596 {
4597 tree ret_val;
4598 tree lookahead = immediate_origin;
4599
4600 do
4601 {
4602 ret_val = lookahead;
4603 lookahead = (TREE_CODE (ret_val) == BLOCK
4604 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4605 }
4606 while (lookahead != NULL && lookahead != ret_val);
4607
4608 return ret_val;
4609 }
4610 }
4611
4612 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4613 of a virtual function may refer to a base class, so we check the 'this'
4614 parameter. */
4615
4616 static tree
4617 decl_class_context (tree decl)
4618 {
4619 tree context = NULL_TREE;
4620
4621 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4622 context = DECL_CONTEXT (decl);
4623 else
4624 context = TYPE_MAIN_VARIANT
4625 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4626
4627 if (context && !TYPE_P (context))
4628 context = NULL_TREE;
4629
4630 return context;
4631 }
4632 \f
4633 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4634 addition order, and correct that in reverse_all_dies. */
4635
4636 static inline void
4637 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4638 {
4639 if (die != NULL && attr != NULL)
4640 {
4641 attr->dw_attr_next = die->die_attr;
4642 die->die_attr = attr;
4643 }
4644 }
4645
4646 static inline enum dw_val_class
4647 AT_class (dw_attr_ref a)
4648 {
4649 return a->dw_attr_val.val_class;
4650 }
4651
4652 /* Add a flag value attribute to a DIE. */
4653
4654 static inline void
4655 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4656 {
4657 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4658
4659 attr->dw_attr_next = NULL;
4660 attr->dw_attr = attr_kind;
4661 attr->dw_attr_val.val_class = dw_val_class_flag;
4662 attr->dw_attr_val.v.val_flag = flag;
4663 add_dwarf_attr (die, attr);
4664 }
4665
4666 static inline unsigned
4667 AT_flag (dw_attr_ref a)
4668 {
4669 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4670 return a->dw_attr_val.v.val_flag;
4671 }
4672
4673 /* Add a signed integer attribute value to a DIE. */
4674
4675 static inline void
4676 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4677 {
4678 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4679
4680 attr->dw_attr_next = NULL;
4681 attr->dw_attr = attr_kind;
4682 attr->dw_attr_val.val_class = dw_val_class_const;
4683 attr->dw_attr_val.v.val_int = int_val;
4684 add_dwarf_attr (die, attr);
4685 }
4686
4687 static inline HOST_WIDE_INT
4688 AT_int (dw_attr_ref a)
4689 {
4690 gcc_assert (a && AT_class (a) == dw_val_class_const);
4691 return a->dw_attr_val.v.val_int;
4692 }
4693
4694 /* Add an unsigned integer attribute value to a DIE. */
4695
4696 static inline void
4697 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4698 unsigned HOST_WIDE_INT unsigned_val)
4699 {
4700 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4701
4702 attr->dw_attr_next = NULL;
4703 attr->dw_attr = attr_kind;
4704 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4705 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4706 add_dwarf_attr (die, attr);
4707 }
4708
4709 static inline unsigned HOST_WIDE_INT
4710 AT_unsigned (dw_attr_ref a)
4711 {
4712 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4713 return a->dw_attr_val.v.val_unsigned;
4714 }
4715
4716 /* Add an unsigned double integer attribute value to a DIE. */
4717
4718 static inline void
4719 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4720 long unsigned int val_hi, long unsigned int val_low)
4721 {
4722 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4723
4724 attr->dw_attr_next = NULL;
4725 attr->dw_attr = attr_kind;
4726 attr->dw_attr_val.val_class = dw_val_class_long_long;
4727 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4728 attr->dw_attr_val.v.val_long_long.low = val_low;
4729 add_dwarf_attr (die, attr);
4730 }
4731
4732 /* Add a floating point attribute value to a DIE and return it. */
4733
4734 static inline void
4735 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4736 unsigned int length, unsigned int elt_size, unsigned char *array)
4737 {
4738 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4739
4740 attr->dw_attr_next = NULL;
4741 attr->dw_attr = attr_kind;
4742 attr->dw_attr_val.val_class = dw_val_class_vec;
4743 attr->dw_attr_val.v.val_vec.length = length;
4744 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4745 attr->dw_attr_val.v.val_vec.array = array;
4746 add_dwarf_attr (die, attr);
4747 }
4748
4749 /* Hash and equality functions for debug_str_hash. */
4750
4751 static hashval_t
4752 debug_str_do_hash (const void *x)
4753 {
4754 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4755 }
4756
4757 static int
4758 debug_str_eq (const void *x1, const void *x2)
4759 {
4760 return strcmp ((((const struct indirect_string_node *)x1)->str),
4761 (const char *)x2) == 0;
4762 }
4763
4764 /* Add a string attribute value to a DIE. */
4765
4766 static inline void
4767 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4768 {
4769 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4770 struct indirect_string_node *node;
4771 void **slot;
4772
4773 if (! debug_str_hash)
4774 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4775 debug_str_eq, NULL);
4776
4777 slot = htab_find_slot_with_hash (debug_str_hash, str,
4778 htab_hash_string (str), INSERT);
4779 if (*slot == NULL)
4780 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4781 node = (struct indirect_string_node *) *slot;
4782 node->str = ggc_strdup (str);
4783 node->refcount++;
4784
4785 attr->dw_attr_next = NULL;
4786 attr->dw_attr = attr_kind;
4787 attr->dw_attr_val.val_class = dw_val_class_str;
4788 attr->dw_attr_val.v.val_str = node;
4789 add_dwarf_attr (die, attr);
4790 }
4791
4792 static inline const char *
4793 AT_string (dw_attr_ref a)
4794 {
4795 gcc_assert (a && AT_class (a) == dw_val_class_str);
4796 return a->dw_attr_val.v.val_str->str;
4797 }
4798
4799 /* Find out whether a string should be output inline in DIE
4800 or out-of-line in .debug_str section. */
4801
4802 static int
4803 AT_string_form (dw_attr_ref a)
4804 {
4805 struct indirect_string_node *node;
4806 unsigned int len;
4807 char label[32];
4808
4809 gcc_assert (a && AT_class (a) == dw_val_class_str);
4810
4811 node = a->dw_attr_val.v.val_str;
4812 if (node->form)
4813 return node->form;
4814
4815 len = strlen (node->str) + 1;
4816
4817 /* If the string is shorter or equal to the size of the reference, it is
4818 always better to put it inline. */
4819 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4820 return node->form = DW_FORM_string;
4821
4822 /* If we cannot expect the linker to merge strings in .debug_str
4823 section, only put it into .debug_str if it is worth even in this
4824 single module. */
4825 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4826 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4827 return node->form = DW_FORM_string;
4828
4829 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4830 ++dw2_string_counter;
4831 node->label = xstrdup (label);
4832
4833 return node->form = DW_FORM_strp;
4834 }
4835
4836 /* Add a DIE reference attribute value to a DIE. */
4837
4838 static inline void
4839 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4840 {
4841 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4842
4843 attr->dw_attr_next = NULL;
4844 attr->dw_attr = attr_kind;
4845 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4846 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4847 attr->dw_attr_val.v.val_die_ref.external = 0;
4848 add_dwarf_attr (die, attr);
4849 }
4850
4851 /* Add an AT_specification attribute to a DIE, and also make the back
4852 pointer from the specification to the definition. */
4853
4854 static inline void
4855 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4856 {
4857 add_AT_die_ref (die, DW_AT_specification, targ_die);
4858 gcc_assert (!targ_die->die_definition);
4859 targ_die->die_definition = die;
4860 }
4861
4862 static inline dw_die_ref
4863 AT_ref (dw_attr_ref a)
4864 {
4865 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4866 return a->dw_attr_val.v.val_die_ref.die;
4867 }
4868
4869 static inline int
4870 AT_ref_external (dw_attr_ref a)
4871 {
4872 if (a && AT_class (a) == dw_val_class_die_ref)
4873 return a->dw_attr_val.v.val_die_ref.external;
4874
4875 return 0;
4876 }
4877
4878 static inline void
4879 set_AT_ref_external (dw_attr_ref a, int i)
4880 {
4881 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4882 a->dw_attr_val.v.val_die_ref.external = i;
4883 }
4884
4885 /* Add an FDE reference attribute value to a DIE. */
4886
4887 static inline void
4888 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4889 {
4890 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4891
4892 attr->dw_attr_next = NULL;
4893 attr->dw_attr = attr_kind;
4894 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4895 attr->dw_attr_val.v.val_fde_index = targ_fde;
4896 add_dwarf_attr (die, attr);
4897 }
4898
4899 /* Add a location description attribute value to a DIE. */
4900
4901 static inline void
4902 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4903 {
4904 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4905
4906 attr->dw_attr_next = NULL;
4907 attr->dw_attr = attr_kind;
4908 attr->dw_attr_val.val_class = dw_val_class_loc;
4909 attr->dw_attr_val.v.val_loc = loc;
4910 add_dwarf_attr (die, attr);
4911 }
4912
4913 static inline dw_loc_descr_ref
4914 AT_loc (dw_attr_ref a)
4915 {
4916 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4917 return a->dw_attr_val.v.val_loc;
4918 }
4919
4920 static inline void
4921 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4922 {
4923 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4924
4925 attr->dw_attr_next = NULL;
4926 attr->dw_attr = attr_kind;
4927 attr->dw_attr_val.val_class = dw_val_class_loc_list;
4928 attr->dw_attr_val.v.val_loc_list = loc_list;
4929 add_dwarf_attr (die, attr);
4930 have_location_lists = 1;
4931 }
4932
4933 static inline dw_loc_list_ref
4934 AT_loc_list (dw_attr_ref a)
4935 {
4936 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4937 return a->dw_attr_val.v.val_loc_list;
4938 }
4939
4940 /* Add an address constant attribute value to a DIE. */
4941
4942 static inline void
4943 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4944 {
4945 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4946
4947 attr->dw_attr_next = NULL;
4948 attr->dw_attr = attr_kind;
4949 attr->dw_attr_val.val_class = dw_val_class_addr;
4950 attr->dw_attr_val.v.val_addr = addr;
4951 add_dwarf_attr (die, attr);
4952 }
4953
4954 static inline rtx
4955 AT_addr (dw_attr_ref a)
4956 {
4957 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4958 return a->dw_attr_val.v.val_addr;
4959 }
4960
4961 /* Add a label identifier attribute value to a DIE. */
4962
4963 static inline void
4964 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4965 {
4966 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4967
4968 attr->dw_attr_next = NULL;
4969 attr->dw_attr = attr_kind;
4970 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4971 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4972 add_dwarf_attr (die, attr);
4973 }
4974
4975 /* Add a section offset attribute value to a DIE. */
4976
4977 static inline void
4978 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
4979 {
4980 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4981
4982 attr->dw_attr_next = NULL;
4983 attr->dw_attr = attr_kind;
4984 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4985 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4986 add_dwarf_attr (die, attr);
4987 }
4988
4989 /* Add an offset attribute value to a DIE. */
4990
4991 static inline void
4992 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4993 unsigned HOST_WIDE_INT offset)
4994 {
4995 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4996
4997 attr->dw_attr_next = NULL;
4998 attr->dw_attr = attr_kind;
4999 attr->dw_attr_val.val_class = dw_val_class_offset;
5000 attr->dw_attr_val.v.val_offset = offset;
5001 add_dwarf_attr (die, attr);
5002 }
5003
5004 /* Add an range_list attribute value to a DIE. */
5005
5006 static void
5007 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5008 long unsigned int offset)
5009 {
5010 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5011
5012 attr->dw_attr_next = NULL;
5013 attr->dw_attr = attr_kind;
5014 attr->dw_attr_val.val_class = dw_val_class_range_list;
5015 attr->dw_attr_val.v.val_offset = offset;
5016 add_dwarf_attr (die, attr);
5017 }
5018
5019 static inline const char *
5020 AT_lbl (dw_attr_ref a)
5021 {
5022 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5023 || AT_class (a) == dw_val_class_lbl_offset));
5024 return a->dw_attr_val.v.val_lbl_id;
5025 }
5026
5027 /* Get the attribute of type attr_kind. */
5028
5029 static dw_attr_ref
5030 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5031 {
5032 dw_attr_ref a;
5033 dw_die_ref spec = NULL;
5034
5035 if (die != NULL)
5036 {
5037 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5038 if (a->dw_attr == attr_kind)
5039 return a;
5040 else if (a->dw_attr == DW_AT_specification
5041 || a->dw_attr == DW_AT_abstract_origin)
5042 spec = AT_ref (a);
5043
5044 if (spec)
5045 return get_AT (spec, attr_kind);
5046 }
5047
5048 return NULL;
5049 }
5050
5051 /* Return the "low pc" attribute value, typically associated with a subprogram
5052 DIE. Return null if the "low pc" attribute is either not present, or if it
5053 cannot be represented as an assembler label identifier. */
5054
5055 static inline const char *
5056 get_AT_low_pc (dw_die_ref die)
5057 {
5058 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5059
5060 return a ? AT_lbl (a) : NULL;
5061 }
5062
5063 /* Return the "high pc" attribute value, typically associated with a subprogram
5064 DIE. Return null if the "high pc" attribute is either not present, or if it
5065 cannot be represented as an assembler label identifier. */
5066
5067 static inline const char *
5068 get_AT_hi_pc (dw_die_ref die)
5069 {
5070 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5071
5072 return a ? AT_lbl (a) : NULL;
5073 }
5074
5075 /* Return the value of the string attribute designated by ATTR_KIND, or
5076 NULL if it is not present. */
5077
5078 static inline const char *
5079 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5080 {
5081 dw_attr_ref a = get_AT (die, attr_kind);
5082
5083 return a ? AT_string (a) : NULL;
5084 }
5085
5086 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5087 if it is not present. */
5088
5089 static inline int
5090 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5091 {
5092 dw_attr_ref a = get_AT (die, attr_kind);
5093
5094 return a ? AT_flag (a) : 0;
5095 }
5096
5097 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5098 if it is not present. */
5099
5100 static inline unsigned
5101 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5102 {
5103 dw_attr_ref a = get_AT (die, attr_kind);
5104
5105 return a ? AT_unsigned (a) : 0;
5106 }
5107
5108 static inline dw_die_ref
5109 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5110 {
5111 dw_attr_ref a = get_AT (die, attr_kind);
5112
5113 return a ? AT_ref (a) : NULL;
5114 }
5115
5116 /* Return TRUE if the language is C or C++. */
5117
5118 static inline bool
5119 is_c_family (void)
5120 {
5121 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5122
5123 return (lang == DW_LANG_C || lang == DW_LANG_C89
5124 || lang == DW_LANG_C_plus_plus);
5125 }
5126
5127 /* Return TRUE if the language is C++. */
5128
5129 static inline bool
5130 is_cxx (void)
5131 {
5132 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5133 == DW_LANG_C_plus_plus);
5134 }
5135
5136 /* Return TRUE if the language is Fortran. */
5137
5138 static inline bool
5139 is_fortran (void)
5140 {
5141 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5142
5143 return (lang == DW_LANG_Fortran77
5144 || lang == DW_LANG_Fortran90
5145 || lang == DW_LANG_Fortran95);
5146 }
5147
5148 /* Return TRUE if the language is Java. */
5149
5150 static inline bool
5151 is_java (void)
5152 {
5153 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5154
5155 return lang == DW_LANG_Java;
5156 }
5157
5158 /* Return TRUE if the language is Ada. */
5159
5160 static inline bool
5161 is_ada (void)
5162 {
5163 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5164
5165 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5166 }
5167
5168 /* Free up the memory used by A. */
5169
5170 static inline void free_AT (dw_attr_ref);
5171 static inline void
5172 free_AT (dw_attr_ref a)
5173 {
5174 if (AT_class (a) == dw_val_class_str)
5175 if (a->dw_attr_val.v.val_str->refcount)
5176 a->dw_attr_val.v.val_str->refcount--;
5177 }
5178
5179 /* Remove the specified attribute if present. */
5180
5181 static void
5182 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5183 {
5184 dw_attr_ref *p;
5185 dw_attr_ref removed = NULL;
5186
5187 if (die != NULL)
5188 {
5189 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5190 if ((*p)->dw_attr == attr_kind)
5191 {
5192 removed = *p;
5193 *p = (*p)->dw_attr_next;
5194 break;
5195 }
5196
5197 if (removed != 0)
5198 free_AT (removed);
5199 }
5200 }
5201
5202 /* Remove child die whose die_tag is specified tag. */
5203
5204 static void
5205 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5206 {
5207 dw_die_ref current, prev, next;
5208 current = die->die_child;
5209 prev = NULL;
5210 while (current != NULL)
5211 {
5212 if (current->die_tag == tag)
5213 {
5214 next = current->die_sib;
5215 if (prev == NULL)
5216 die->die_child = next;
5217 else
5218 prev->die_sib = next;
5219 free_die (current);
5220 current = next;
5221 }
5222 else
5223 {
5224 prev = current;
5225 current = current->die_sib;
5226 }
5227 }
5228 }
5229
5230 /* Free up the memory used by DIE. */
5231
5232 static inline void
5233 free_die (dw_die_ref die)
5234 {
5235 remove_children (die);
5236 }
5237
5238 /* Discard the children of this DIE. */
5239
5240 static void
5241 remove_children (dw_die_ref die)
5242 {
5243 dw_die_ref child_die = die->die_child;
5244
5245 die->die_child = NULL;
5246
5247 while (child_die != NULL)
5248 {
5249 dw_die_ref tmp_die = child_die;
5250 dw_attr_ref a;
5251
5252 child_die = child_die->die_sib;
5253
5254 for (a = tmp_die->die_attr; a != NULL;)
5255 {
5256 dw_attr_ref tmp_a = a;
5257
5258 a = a->dw_attr_next;
5259 free_AT (tmp_a);
5260 }
5261
5262 free_die (tmp_die);
5263 }
5264 }
5265
5266 /* Add a child DIE below its parent. We build the lists up in reverse
5267 addition order, and correct that in reverse_all_dies. */
5268
5269 static inline void
5270 add_child_die (dw_die_ref die, dw_die_ref child_die)
5271 {
5272 if (die != NULL && child_die != NULL)
5273 {
5274 gcc_assert (die != child_die);
5275
5276 child_die->die_parent = die;
5277 child_die->die_sib = die->die_child;
5278 die->die_child = child_die;
5279 }
5280 }
5281
5282 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5283 is the specification, to the front of PARENT's list of children. */
5284
5285 static void
5286 splice_child_die (dw_die_ref parent, dw_die_ref child)
5287 {
5288 dw_die_ref *p;
5289
5290 /* We want the declaration DIE from inside the class, not the
5291 specification DIE at toplevel. */
5292 if (child->die_parent != parent)
5293 {
5294 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5295
5296 if (tmp)
5297 child = tmp;
5298 }
5299
5300 gcc_assert (child->die_parent == parent
5301 || (child->die_parent
5302 == get_AT_ref (parent, DW_AT_specification)));
5303
5304 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5305 if (*p == child)
5306 {
5307 *p = child->die_sib;
5308 break;
5309 }
5310
5311 child->die_parent = parent;
5312 child->die_sib = parent->die_child;
5313 parent->die_child = child;
5314 }
5315
5316 /* Return a pointer to a newly created DIE node. */
5317
5318 static inline dw_die_ref
5319 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5320 {
5321 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5322
5323 die->die_tag = tag_value;
5324
5325 if (parent_die != NULL)
5326 add_child_die (parent_die, die);
5327 else
5328 {
5329 limbo_die_node *limbo_node;
5330
5331 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5332 limbo_node->die = die;
5333 limbo_node->created_for = t;
5334 limbo_node->next = limbo_die_list;
5335 limbo_die_list = limbo_node;
5336 }
5337
5338 return die;
5339 }
5340
5341 /* Return the DIE associated with the given type specifier. */
5342
5343 static inline dw_die_ref
5344 lookup_type_die (tree type)
5345 {
5346 return TYPE_SYMTAB_DIE (type);
5347 }
5348
5349 /* Equate a DIE to a given type specifier. */
5350
5351 static inline void
5352 equate_type_number_to_die (tree type, dw_die_ref type_die)
5353 {
5354 TYPE_SYMTAB_DIE (type) = type_die;
5355 }
5356
5357 /* Returns a hash value for X (which really is a die_struct). */
5358
5359 static hashval_t
5360 decl_die_table_hash (const void *x)
5361 {
5362 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5363 }
5364
5365 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5366
5367 static int
5368 decl_die_table_eq (const void *x, const void *y)
5369 {
5370 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5371 }
5372
5373 /* Return the DIE associated with a given declaration. */
5374
5375 static inline dw_die_ref
5376 lookup_decl_die (tree decl)
5377 {
5378 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5379 }
5380
5381 /* Returns a hash value for X (which really is a var_loc_list). */
5382
5383 static hashval_t
5384 decl_loc_table_hash (const void *x)
5385 {
5386 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5387 }
5388
5389 /* Return nonzero if decl_id of var_loc_list X is the same as
5390 UID of decl *Y. */
5391
5392 static int
5393 decl_loc_table_eq (const void *x, const void *y)
5394 {
5395 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5396 }
5397
5398 /* Return the var_loc list associated with a given declaration. */
5399
5400 static inline var_loc_list *
5401 lookup_decl_loc (tree decl)
5402 {
5403 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5404 }
5405
5406 /* Equate a DIE to a particular declaration. */
5407
5408 static void
5409 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5410 {
5411 unsigned int decl_id = DECL_UID (decl);
5412 void **slot;
5413
5414 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5415 *slot = decl_die;
5416 decl_die->decl_id = decl_id;
5417 }
5418
5419 /* Add a variable location node to the linked list for DECL. */
5420
5421 static void
5422 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5423 {
5424 unsigned int decl_id = DECL_UID (decl);
5425 var_loc_list *temp;
5426 void **slot;
5427
5428 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5429 if (*slot == NULL)
5430 {
5431 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5432 temp->decl_id = decl_id;
5433 *slot = temp;
5434 }
5435 else
5436 temp = *slot;
5437
5438 if (temp->last)
5439 {
5440 /* If the current location is the same as the end of the list,
5441 we have nothing to do. */
5442 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5443 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5444 {
5445 /* Add LOC to the end of list and update LAST. */
5446 temp->last->next = loc;
5447 temp->last = loc;
5448 }
5449 }
5450 /* Do not add empty location to the beginning of the list. */
5451 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5452 {
5453 temp->first = loc;
5454 temp->last = loc;
5455 }
5456 }
5457 \f
5458 /* Keep track of the number of spaces used to indent the
5459 output of the debugging routines that print the structure of
5460 the DIE internal representation. */
5461 static int print_indent;
5462
5463 /* Indent the line the number of spaces given by print_indent. */
5464
5465 static inline void
5466 print_spaces (FILE *outfile)
5467 {
5468 fprintf (outfile, "%*s", print_indent, "");
5469 }
5470
5471 /* Print the information associated with a given DIE, and its children.
5472 This routine is a debugging aid only. */
5473
5474 static void
5475 print_die (dw_die_ref die, FILE *outfile)
5476 {
5477 dw_attr_ref a;
5478 dw_die_ref c;
5479
5480 print_spaces (outfile);
5481 fprintf (outfile, "DIE %4lu: %s\n",
5482 die->die_offset, dwarf_tag_name (die->die_tag));
5483 print_spaces (outfile);
5484 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5485 fprintf (outfile, " offset: %lu\n", die->die_offset);
5486
5487 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5488 {
5489 print_spaces (outfile);
5490 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5491
5492 switch (AT_class (a))
5493 {
5494 case dw_val_class_addr:
5495 fprintf (outfile, "address");
5496 break;
5497 case dw_val_class_offset:
5498 fprintf (outfile, "offset");
5499 break;
5500 case dw_val_class_loc:
5501 fprintf (outfile, "location descriptor");
5502 break;
5503 case dw_val_class_loc_list:
5504 fprintf (outfile, "location list -> label:%s",
5505 AT_loc_list (a)->ll_symbol);
5506 break;
5507 case dw_val_class_range_list:
5508 fprintf (outfile, "range list");
5509 break;
5510 case dw_val_class_const:
5511 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5512 break;
5513 case dw_val_class_unsigned_const:
5514 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5515 break;
5516 case dw_val_class_long_long:
5517 fprintf (outfile, "constant (%lu,%lu)",
5518 a->dw_attr_val.v.val_long_long.hi,
5519 a->dw_attr_val.v.val_long_long.low);
5520 break;
5521 case dw_val_class_vec:
5522 fprintf (outfile, "floating-point or vector constant");
5523 break;
5524 case dw_val_class_flag:
5525 fprintf (outfile, "%u", AT_flag (a));
5526 break;
5527 case dw_val_class_die_ref:
5528 if (AT_ref (a) != NULL)
5529 {
5530 if (AT_ref (a)->die_symbol)
5531 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5532 else
5533 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5534 }
5535 else
5536 fprintf (outfile, "die -> <null>");
5537 break;
5538 case dw_val_class_lbl_id:
5539 case dw_val_class_lbl_offset:
5540 fprintf (outfile, "label: %s", AT_lbl (a));
5541 break;
5542 case dw_val_class_str:
5543 if (AT_string (a) != NULL)
5544 fprintf (outfile, "\"%s\"", AT_string (a));
5545 else
5546 fprintf (outfile, "<null>");
5547 break;
5548 default:
5549 break;
5550 }
5551
5552 fprintf (outfile, "\n");
5553 }
5554
5555 if (die->die_child != NULL)
5556 {
5557 print_indent += 4;
5558 for (c = die->die_child; c != NULL; c = c->die_sib)
5559 print_die (c, outfile);
5560
5561 print_indent -= 4;
5562 }
5563 if (print_indent == 0)
5564 fprintf (outfile, "\n");
5565 }
5566
5567 /* Print the contents of the source code line number correspondence table.
5568 This routine is a debugging aid only. */
5569
5570 static void
5571 print_dwarf_line_table (FILE *outfile)
5572 {
5573 unsigned i;
5574 dw_line_info_ref line_info;
5575
5576 fprintf (outfile, "\n\nDWARF source line information\n");
5577 for (i = 1; i < line_info_table_in_use; i++)
5578 {
5579 line_info = &line_info_table[i];
5580 fprintf (outfile, "%5d: ", i);
5581 fprintf (outfile, "%-20s",
5582 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5583 fprintf (outfile, "%6ld", line_info->dw_line_num);
5584 fprintf (outfile, "\n");
5585 }
5586
5587 fprintf (outfile, "\n\n");
5588 }
5589
5590 /* Print the information collected for a given DIE. */
5591
5592 void
5593 debug_dwarf_die (dw_die_ref die)
5594 {
5595 print_die (die, stderr);
5596 }
5597
5598 /* Print all DWARF information collected for the compilation unit.
5599 This routine is a debugging aid only. */
5600
5601 void
5602 debug_dwarf (void)
5603 {
5604 print_indent = 0;
5605 print_die (comp_unit_die, stderr);
5606 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5607 print_dwarf_line_table (stderr);
5608 }
5609 \f
5610 /* We build up the lists of children and attributes by pushing new ones
5611 onto the beginning of the list. Reverse the lists for DIE so that
5612 they are in order of addition. */
5613
5614 static void
5615 reverse_die_lists (dw_die_ref die)
5616 {
5617 dw_die_ref c, cp, cn;
5618 dw_attr_ref a, ap, an;
5619
5620 for (a = die->die_attr, ap = 0; a; a = an)
5621 {
5622 an = a->dw_attr_next;
5623 a->dw_attr_next = ap;
5624 ap = a;
5625 }
5626
5627 die->die_attr = ap;
5628
5629 for (c = die->die_child, cp = 0; c; c = cn)
5630 {
5631 cn = c->die_sib;
5632 c->die_sib = cp;
5633 cp = c;
5634 }
5635
5636 die->die_child = cp;
5637 }
5638
5639 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5640 reverse all dies in add_sibling_attributes, which runs through all the dies,
5641 it would reverse all the dies. Now, however, since we don't call
5642 reverse_die_lists in add_sibling_attributes, we need a routine to
5643 recursively reverse all the dies. This is that routine. */
5644
5645 static void
5646 reverse_all_dies (dw_die_ref die)
5647 {
5648 dw_die_ref c;
5649
5650 reverse_die_lists (die);
5651
5652 for (c = die->die_child; c; c = c->die_sib)
5653 reverse_all_dies (c);
5654 }
5655
5656 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5657 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5658 DIE that marks the start of the DIEs for this include file. */
5659
5660 static dw_die_ref
5661 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5662 {
5663 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5664 dw_die_ref new_unit = gen_compile_unit_die (filename);
5665
5666 new_unit->die_sib = old_unit;
5667 return new_unit;
5668 }
5669
5670 /* Close an include-file CU and reopen the enclosing one. */
5671
5672 static dw_die_ref
5673 pop_compile_unit (dw_die_ref old_unit)
5674 {
5675 dw_die_ref new_unit = old_unit->die_sib;
5676
5677 old_unit->die_sib = NULL;
5678 return new_unit;
5679 }
5680
5681 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5682 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5683
5684 /* Calculate the checksum of a location expression. */
5685
5686 static inline void
5687 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5688 {
5689 CHECKSUM (loc->dw_loc_opc);
5690 CHECKSUM (loc->dw_loc_oprnd1);
5691 CHECKSUM (loc->dw_loc_oprnd2);
5692 }
5693
5694 /* Calculate the checksum of an attribute. */
5695
5696 static void
5697 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5698 {
5699 dw_loc_descr_ref loc;
5700 rtx r;
5701
5702 CHECKSUM (at->dw_attr);
5703
5704 /* We don't care about differences in file numbering. */
5705 if (at->dw_attr == DW_AT_decl_file
5706 /* Or that this was compiled with a different compiler snapshot; if
5707 the output is the same, that's what matters. */
5708 || at->dw_attr == DW_AT_producer)
5709 return;
5710
5711 switch (AT_class (at))
5712 {
5713 case dw_val_class_const:
5714 CHECKSUM (at->dw_attr_val.v.val_int);
5715 break;
5716 case dw_val_class_unsigned_const:
5717 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5718 break;
5719 case dw_val_class_long_long:
5720 CHECKSUM (at->dw_attr_val.v.val_long_long);
5721 break;
5722 case dw_val_class_vec:
5723 CHECKSUM (at->dw_attr_val.v.val_vec);
5724 break;
5725 case dw_val_class_flag:
5726 CHECKSUM (at->dw_attr_val.v.val_flag);
5727 break;
5728 case dw_val_class_str:
5729 CHECKSUM_STRING (AT_string (at));
5730 break;
5731
5732 case dw_val_class_addr:
5733 r = AT_addr (at);
5734 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5735 CHECKSUM_STRING (XSTR (r, 0));
5736 break;
5737
5738 case dw_val_class_offset:
5739 CHECKSUM (at->dw_attr_val.v.val_offset);
5740 break;
5741
5742 case dw_val_class_loc:
5743 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5744 loc_checksum (loc, ctx);
5745 break;
5746
5747 case dw_val_class_die_ref:
5748 die_checksum (AT_ref (at), ctx, mark);
5749 break;
5750
5751 case dw_val_class_fde_ref:
5752 case dw_val_class_lbl_id:
5753 case dw_val_class_lbl_offset:
5754 break;
5755
5756 default:
5757 break;
5758 }
5759 }
5760
5761 /* Calculate the checksum of a DIE. */
5762
5763 static void
5764 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5765 {
5766 dw_die_ref c;
5767 dw_attr_ref a;
5768
5769 /* To avoid infinite recursion. */
5770 if (die->die_mark)
5771 {
5772 CHECKSUM (die->die_mark);
5773 return;
5774 }
5775 die->die_mark = ++(*mark);
5776
5777 CHECKSUM (die->die_tag);
5778
5779 for (a = die->die_attr; a; a = a->dw_attr_next)
5780 attr_checksum (a, ctx, mark);
5781
5782 for (c = die->die_child; c; c = c->die_sib)
5783 die_checksum (c, ctx, mark);
5784 }
5785
5786 #undef CHECKSUM
5787 #undef CHECKSUM_STRING
5788
5789 /* Do the location expressions look same? */
5790 static inline int
5791 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5792 {
5793 return loc1->dw_loc_opc == loc2->dw_loc_opc
5794 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5795 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5796 }
5797
5798 /* Do the values look the same? */
5799 static int
5800 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5801 {
5802 dw_loc_descr_ref loc1, loc2;
5803 rtx r1, r2;
5804
5805 if (v1->val_class != v2->val_class)
5806 return 0;
5807
5808 switch (v1->val_class)
5809 {
5810 case dw_val_class_const:
5811 return v1->v.val_int == v2->v.val_int;
5812 case dw_val_class_unsigned_const:
5813 return v1->v.val_unsigned == v2->v.val_unsigned;
5814 case dw_val_class_long_long:
5815 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5816 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5817 case dw_val_class_vec:
5818 if (v1->v.val_vec.length != v2->v.val_vec.length
5819 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5820 return 0;
5821 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5822 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5823 return 0;
5824 return 1;
5825 case dw_val_class_flag:
5826 return v1->v.val_flag == v2->v.val_flag;
5827 case dw_val_class_str:
5828 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5829
5830 case dw_val_class_addr:
5831 r1 = v1->v.val_addr;
5832 r2 = v2->v.val_addr;
5833 if (GET_CODE (r1) != GET_CODE (r2))
5834 return 0;
5835 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
5836 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5837
5838 case dw_val_class_offset:
5839 return v1->v.val_offset == v2->v.val_offset;
5840
5841 case dw_val_class_loc:
5842 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5843 loc1 && loc2;
5844 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5845 if (!same_loc_p (loc1, loc2, mark))
5846 return 0;
5847 return !loc1 && !loc2;
5848
5849 case dw_val_class_die_ref:
5850 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5851
5852 case dw_val_class_fde_ref:
5853 case dw_val_class_lbl_id:
5854 case dw_val_class_lbl_offset:
5855 return 1;
5856
5857 default:
5858 return 1;
5859 }
5860 }
5861
5862 /* Do the attributes look the same? */
5863
5864 static int
5865 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5866 {
5867 if (at1->dw_attr != at2->dw_attr)
5868 return 0;
5869
5870 /* We don't care about differences in file numbering. */
5871 if (at1->dw_attr == DW_AT_decl_file
5872 /* Or that this was compiled with a different compiler snapshot; if
5873 the output is the same, that's what matters. */
5874 || at1->dw_attr == DW_AT_producer)
5875 return 1;
5876
5877 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5878 }
5879
5880 /* Do the dies look the same? */
5881
5882 static int
5883 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5884 {
5885 dw_die_ref c1, c2;
5886 dw_attr_ref a1, a2;
5887
5888 /* To avoid infinite recursion. */
5889 if (die1->die_mark)
5890 return die1->die_mark == die2->die_mark;
5891 die1->die_mark = die2->die_mark = ++(*mark);
5892
5893 if (die1->die_tag != die2->die_tag)
5894 return 0;
5895
5896 for (a1 = die1->die_attr, a2 = die2->die_attr;
5897 a1 && a2;
5898 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5899 if (!same_attr_p (a1, a2, mark))
5900 return 0;
5901 if (a1 || a2)
5902 return 0;
5903
5904 for (c1 = die1->die_child, c2 = die2->die_child;
5905 c1 && c2;
5906 c1 = c1->die_sib, c2 = c2->die_sib)
5907 if (!same_die_p (c1, c2, mark))
5908 return 0;
5909 if (c1 || c2)
5910 return 0;
5911
5912 return 1;
5913 }
5914
5915 /* Do the dies look the same? Wrapper around same_die_p. */
5916
5917 static int
5918 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5919 {
5920 int mark = 0;
5921 int ret = same_die_p (die1, die2, &mark);
5922
5923 unmark_all_dies (die1);
5924 unmark_all_dies (die2);
5925
5926 return ret;
5927 }
5928
5929 /* The prefix to attach to symbols on DIEs in the current comdat debug
5930 info section. */
5931 static char *comdat_symbol_id;
5932
5933 /* The index of the current symbol within the current comdat CU. */
5934 static unsigned int comdat_symbol_number;
5935
5936 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5937 children, and set comdat_symbol_id accordingly. */
5938
5939 static void
5940 compute_section_prefix (dw_die_ref unit_die)
5941 {
5942 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5943 const char *base = die_name ? lbasename (die_name) : "anonymous";
5944 char *name = alloca (strlen (base) + 64);
5945 char *p;
5946 int i, mark;
5947 unsigned char checksum[16];
5948 struct md5_ctx ctx;
5949
5950 /* Compute the checksum of the DIE, then append part of it as hex digits to
5951 the name filename of the unit. */
5952
5953 md5_init_ctx (&ctx);
5954 mark = 0;
5955 die_checksum (unit_die, &ctx, &mark);
5956 unmark_all_dies (unit_die);
5957 md5_finish_ctx (&ctx, checksum);
5958
5959 sprintf (name, "%s.", base);
5960 clean_symbol_name (name);
5961
5962 p = name + strlen (name);
5963 for (i = 0; i < 4; i++)
5964 {
5965 sprintf (p, "%.2x", checksum[i]);
5966 p += 2;
5967 }
5968
5969 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5970 comdat_symbol_number = 0;
5971 }
5972
5973 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5974
5975 static int
5976 is_type_die (dw_die_ref die)
5977 {
5978 switch (die->die_tag)
5979 {
5980 case DW_TAG_array_type:
5981 case DW_TAG_class_type:
5982 case DW_TAG_enumeration_type:
5983 case DW_TAG_pointer_type:
5984 case DW_TAG_reference_type:
5985 case DW_TAG_string_type:
5986 case DW_TAG_structure_type:
5987 case DW_TAG_subroutine_type:
5988 case DW_TAG_union_type:
5989 case DW_TAG_ptr_to_member_type:
5990 case DW_TAG_set_type:
5991 case DW_TAG_subrange_type:
5992 case DW_TAG_base_type:
5993 case DW_TAG_const_type:
5994 case DW_TAG_file_type:
5995 case DW_TAG_packed_type:
5996 case DW_TAG_volatile_type:
5997 case DW_TAG_typedef:
5998 return 1;
5999 default:
6000 return 0;
6001 }
6002 }
6003
6004 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6005 Basically, we want to choose the bits that are likely to be shared between
6006 compilations (types) and leave out the bits that are specific to individual
6007 compilations (functions). */
6008
6009 static int
6010 is_comdat_die (dw_die_ref c)
6011 {
6012 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6013 we do for stabs. The advantage is a greater likelihood of sharing between
6014 objects that don't include headers in the same order (and therefore would
6015 put the base types in a different comdat). jason 8/28/00 */
6016
6017 if (c->die_tag == DW_TAG_base_type)
6018 return 0;
6019
6020 if (c->die_tag == DW_TAG_pointer_type
6021 || c->die_tag == DW_TAG_reference_type
6022 || c->die_tag == DW_TAG_const_type
6023 || c->die_tag == DW_TAG_volatile_type)
6024 {
6025 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6026
6027 return t ? is_comdat_die (t) : 0;
6028 }
6029
6030 return is_type_die (c);
6031 }
6032
6033 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6034 compilation unit. */
6035
6036 static int
6037 is_symbol_die (dw_die_ref c)
6038 {
6039 return (is_type_die (c)
6040 || (get_AT (c, DW_AT_declaration)
6041 && !get_AT (c, DW_AT_specification)));
6042 }
6043
6044 static char *
6045 gen_internal_sym (const char *prefix)
6046 {
6047 char buf[256];
6048
6049 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6050 return xstrdup (buf);
6051 }
6052
6053 /* Assign symbols to all worthy DIEs under DIE. */
6054
6055 static void
6056 assign_symbol_names (dw_die_ref die)
6057 {
6058 dw_die_ref c;
6059
6060 if (is_symbol_die (die))
6061 {
6062 if (comdat_symbol_id)
6063 {
6064 char *p = alloca (strlen (comdat_symbol_id) + 64);
6065
6066 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6067 comdat_symbol_id, comdat_symbol_number++);
6068 die->die_symbol = xstrdup (p);
6069 }
6070 else
6071 die->die_symbol = gen_internal_sym ("LDIE");
6072 }
6073
6074 for (c = die->die_child; c != NULL; c = c->die_sib)
6075 assign_symbol_names (c);
6076 }
6077
6078 struct cu_hash_table_entry
6079 {
6080 dw_die_ref cu;
6081 unsigned min_comdat_num, max_comdat_num;
6082 struct cu_hash_table_entry *next;
6083 };
6084
6085 /* Routines to manipulate hash table of CUs. */
6086 static hashval_t
6087 htab_cu_hash (const void *of)
6088 {
6089 const struct cu_hash_table_entry *entry = of;
6090
6091 return htab_hash_string (entry->cu->die_symbol);
6092 }
6093
6094 static int
6095 htab_cu_eq (const void *of1, const void *of2)
6096 {
6097 const struct cu_hash_table_entry *entry1 = of1;
6098 const struct die_struct *entry2 = of2;
6099
6100 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6101 }
6102
6103 static void
6104 htab_cu_del (void *what)
6105 {
6106 struct cu_hash_table_entry *next, *entry = what;
6107
6108 while (entry)
6109 {
6110 next = entry->next;
6111 free (entry);
6112 entry = next;
6113 }
6114 }
6115
6116 /* Check whether we have already seen this CU and set up SYM_NUM
6117 accordingly. */
6118 static int
6119 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6120 {
6121 struct cu_hash_table_entry dummy;
6122 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6123
6124 dummy.max_comdat_num = 0;
6125
6126 slot = (struct cu_hash_table_entry **)
6127 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6128 INSERT);
6129 entry = *slot;
6130
6131 for (; entry; last = entry, entry = entry->next)
6132 {
6133 if (same_die_p_wrap (cu, entry->cu))
6134 break;
6135 }
6136
6137 if (entry)
6138 {
6139 *sym_num = entry->min_comdat_num;
6140 return 1;
6141 }
6142
6143 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6144 entry->cu = cu;
6145 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6146 entry->next = *slot;
6147 *slot = entry;
6148
6149 return 0;
6150 }
6151
6152 /* Record SYM_NUM to record of CU in HTABLE. */
6153 static void
6154 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6155 {
6156 struct cu_hash_table_entry **slot, *entry;
6157
6158 slot = (struct cu_hash_table_entry **)
6159 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6160 NO_INSERT);
6161 entry = *slot;
6162
6163 entry->max_comdat_num = sym_num;
6164 }
6165
6166 /* Traverse the DIE (which is always comp_unit_die), and set up
6167 additional compilation units for each of the include files we see
6168 bracketed by BINCL/EINCL. */
6169
6170 static void
6171 break_out_includes (dw_die_ref die)
6172 {
6173 dw_die_ref *ptr;
6174 dw_die_ref unit = NULL;
6175 limbo_die_node *node, **pnode;
6176 htab_t cu_hash_table;
6177
6178 for (ptr = &(die->die_child); *ptr;)
6179 {
6180 dw_die_ref c = *ptr;
6181
6182 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6183 || (unit && is_comdat_die (c)))
6184 {
6185 /* This DIE is for a secondary CU; remove it from the main one. */
6186 *ptr = c->die_sib;
6187
6188 if (c->die_tag == DW_TAG_GNU_BINCL)
6189 {
6190 unit = push_new_compile_unit (unit, c);
6191 free_die (c);
6192 }
6193 else if (c->die_tag == DW_TAG_GNU_EINCL)
6194 {
6195 unit = pop_compile_unit (unit);
6196 free_die (c);
6197 }
6198 else
6199 add_child_die (unit, c);
6200 }
6201 else
6202 {
6203 /* Leave this DIE in the main CU. */
6204 ptr = &(c->die_sib);
6205 continue;
6206 }
6207 }
6208
6209 #if 0
6210 /* We can only use this in debugging, since the frontend doesn't check
6211 to make sure that we leave every include file we enter. */
6212 gcc_assert (!unit);
6213 #endif
6214
6215 assign_symbol_names (die);
6216 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6217 for (node = limbo_die_list, pnode = &limbo_die_list;
6218 node;
6219 node = node->next)
6220 {
6221 int is_dupl;
6222
6223 compute_section_prefix (node->die);
6224 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6225 &comdat_symbol_number);
6226 assign_symbol_names (node->die);
6227 if (is_dupl)
6228 *pnode = node->next;
6229 else
6230 {
6231 pnode = &node->next;
6232 record_comdat_symbol_number (node->die, cu_hash_table,
6233 comdat_symbol_number);
6234 }
6235 }
6236 htab_delete (cu_hash_table);
6237 }
6238
6239 /* Traverse the DIE and add a sibling attribute if it may have the
6240 effect of speeding up access to siblings. To save some space,
6241 avoid generating sibling attributes for DIE's without children. */
6242
6243 static void
6244 add_sibling_attributes (dw_die_ref die)
6245 {
6246 dw_die_ref c;
6247
6248 if (die->die_tag != DW_TAG_compile_unit
6249 && die->die_sib && die->die_child != NULL)
6250 /* Add the sibling link to the front of the attribute list. */
6251 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6252
6253 for (c = die->die_child; c != NULL; c = c->die_sib)
6254 add_sibling_attributes (c);
6255 }
6256
6257 /* Output all location lists for the DIE and its children. */
6258
6259 static void
6260 output_location_lists (dw_die_ref die)
6261 {
6262 dw_die_ref c;
6263 dw_attr_ref d_attr;
6264
6265 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6266 if (AT_class (d_attr) == dw_val_class_loc_list)
6267 output_loc_list (AT_loc_list (d_attr));
6268
6269 for (c = die->die_child; c != NULL; c = c->die_sib)
6270 output_location_lists (c);
6271
6272 }
6273
6274 /* The format of each DIE (and its attribute value pairs) is encoded in an
6275 abbreviation table. This routine builds the abbreviation table and assigns
6276 a unique abbreviation id for each abbreviation entry. The children of each
6277 die are visited recursively. */
6278
6279 static void
6280 build_abbrev_table (dw_die_ref die)
6281 {
6282 unsigned long abbrev_id;
6283 unsigned int n_alloc;
6284 dw_die_ref c;
6285 dw_attr_ref d_attr, a_attr;
6286
6287 /* Scan the DIE references, and mark as external any that refer to
6288 DIEs from other CUs (i.e. those which are not marked). */
6289 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6290 if (AT_class (d_attr) == dw_val_class_die_ref
6291 && AT_ref (d_attr)->die_mark == 0)
6292 {
6293 gcc_assert (AT_ref (d_attr)->die_symbol);
6294
6295 set_AT_ref_external (d_attr, 1);
6296 }
6297
6298 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6299 {
6300 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6301
6302 if (abbrev->die_tag == die->die_tag)
6303 {
6304 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6305 {
6306 a_attr = abbrev->die_attr;
6307 d_attr = die->die_attr;
6308
6309 while (a_attr != NULL && d_attr != NULL)
6310 {
6311 if ((a_attr->dw_attr != d_attr->dw_attr)
6312 || (value_format (a_attr) != value_format (d_attr)))
6313 break;
6314
6315 a_attr = a_attr->dw_attr_next;
6316 d_attr = d_attr->dw_attr_next;
6317 }
6318
6319 if (a_attr == NULL && d_attr == NULL)
6320 break;
6321 }
6322 }
6323 }
6324
6325 if (abbrev_id >= abbrev_die_table_in_use)
6326 {
6327 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6328 {
6329 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6330 abbrev_die_table = ggc_realloc (abbrev_die_table,
6331 sizeof (dw_die_ref) * n_alloc);
6332
6333 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6334 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6335 abbrev_die_table_allocated = n_alloc;
6336 }
6337
6338 ++abbrev_die_table_in_use;
6339 abbrev_die_table[abbrev_id] = die;
6340 }
6341
6342 die->die_abbrev = abbrev_id;
6343 for (c = die->die_child; c != NULL; c = c->die_sib)
6344 build_abbrev_table (c);
6345 }
6346 \f
6347 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6348
6349 static int
6350 constant_size (long unsigned int value)
6351 {
6352 int log;
6353
6354 if (value == 0)
6355 log = 0;
6356 else
6357 log = floor_log2 (value);
6358
6359 log = log / 8;
6360 log = 1 << (floor_log2 (log) + 1);
6361
6362 return log;
6363 }
6364
6365 /* Return the size of a DIE as it is represented in the
6366 .debug_info section. */
6367
6368 static unsigned long
6369 size_of_die (dw_die_ref die)
6370 {
6371 unsigned long size = 0;
6372 dw_attr_ref a;
6373
6374 size += size_of_uleb128 (die->die_abbrev);
6375 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6376 {
6377 switch (AT_class (a))
6378 {
6379 case dw_val_class_addr:
6380 size += DWARF2_ADDR_SIZE;
6381 break;
6382 case dw_val_class_offset:
6383 size += DWARF_OFFSET_SIZE;
6384 break;
6385 case dw_val_class_loc:
6386 {
6387 unsigned long lsize = size_of_locs (AT_loc (a));
6388
6389 /* Block length. */
6390 size += constant_size (lsize);
6391 size += lsize;
6392 }
6393 break;
6394 case dw_val_class_loc_list:
6395 size += DWARF_OFFSET_SIZE;
6396 break;
6397 case dw_val_class_range_list:
6398 size += DWARF_OFFSET_SIZE;
6399 break;
6400 case dw_val_class_const:
6401 size += size_of_sleb128 (AT_int (a));
6402 break;
6403 case dw_val_class_unsigned_const:
6404 size += constant_size (AT_unsigned (a));
6405 break;
6406 case dw_val_class_long_long:
6407 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6408 break;
6409 case dw_val_class_vec:
6410 size += 1 + (a->dw_attr_val.v.val_vec.length
6411 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6412 break;
6413 case dw_val_class_flag:
6414 size += 1;
6415 break;
6416 case dw_val_class_die_ref:
6417 if (AT_ref_external (a))
6418 size += DWARF2_ADDR_SIZE;
6419 else
6420 size += DWARF_OFFSET_SIZE;
6421 break;
6422 case dw_val_class_fde_ref:
6423 size += DWARF_OFFSET_SIZE;
6424 break;
6425 case dw_val_class_lbl_id:
6426 size += DWARF2_ADDR_SIZE;
6427 break;
6428 case dw_val_class_lbl_offset:
6429 size += DWARF_OFFSET_SIZE;
6430 break;
6431 case dw_val_class_str:
6432 if (AT_string_form (a) == DW_FORM_strp)
6433 size += DWARF_OFFSET_SIZE;
6434 else
6435 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6436 break;
6437 default:
6438 gcc_unreachable ();
6439 }
6440 }
6441
6442 return size;
6443 }
6444
6445 /* Size the debugging information associated with a given DIE. Visits the
6446 DIE's children recursively. Updates the global variable next_die_offset, on
6447 each time through. Uses the current value of next_die_offset to update the
6448 die_offset field in each DIE. */
6449
6450 static void
6451 calc_die_sizes (dw_die_ref die)
6452 {
6453 dw_die_ref c;
6454
6455 die->die_offset = next_die_offset;
6456 next_die_offset += size_of_die (die);
6457
6458 for (c = die->die_child; c != NULL; c = c->die_sib)
6459 calc_die_sizes (c);
6460
6461 if (die->die_child != NULL)
6462 /* Count the null byte used to terminate sibling lists. */
6463 next_die_offset += 1;
6464 }
6465
6466 /* Set the marks for a die and its children. We do this so
6467 that we know whether or not a reference needs to use FORM_ref_addr; only
6468 DIEs in the same CU will be marked. We used to clear out the offset
6469 and use that as the flag, but ran into ordering problems. */
6470
6471 static void
6472 mark_dies (dw_die_ref die)
6473 {
6474 dw_die_ref c;
6475
6476 gcc_assert (!die->die_mark);
6477
6478 die->die_mark = 1;
6479 for (c = die->die_child; c; c = c->die_sib)
6480 mark_dies (c);
6481 }
6482
6483 /* Clear the marks for a die and its children. */
6484
6485 static void
6486 unmark_dies (dw_die_ref die)
6487 {
6488 dw_die_ref c;
6489
6490 gcc_assert (die->die_mark);
6491
6492 die->die_mark = 0;
6493 for (c = die->die_child; c; c = c->die_sib)
6494 unmark_dies (c);
6495 }
6496
6497 /* Clear the marks for a die, its children and referred dies. */
6498
6499 static void
6500 unmark_all_dies (dw_die_ref die)
6501 {
6502 dw_die_ref c;
6503 dw_attr_ref a;
6504
6505 if (!die->die_mark)
6506 return;
6507 die->die_mark = 0;
6508
6509 for (c = die->die_child; c; c = c->die_sib)
6510 unmark_all_dies (c);
6511
6512 for (a = die->die_attr; a; a = a->dw_attr_next)
6513 if (AT_class (a) == dw_val_class_die_ref)
6514 unmark_all_dies (AT_ref (a));
6515 }
6516
6517 /* Return the size of the .debug_pubnames table generated for the
6518 compilation unit. */
6519
6520 static unsigned long
6521 size_of_pubnames (void)
6522 {
6523 unsigned long size;
6524 unsigned i;
6525
6526 size = DWARF_PUBNAMES_HEADER_SIZE;
6527 for (i = 0; i < pubname_table_in_use; i++)
6528 {
6529 pubname_ref p = &pubname_table[i];
6530 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6531 }
6532
6533 size += DWARF_OFFSET_SIZE;
6534 return size;
6535 }
6536
6537 /* Return the size of the information in the .debug_aranges section. */
6538
6539 static unsigned long
6540 size_of_aranges (void)
6541 {
6542 unsigned long size;
6543
6544 size = DWARF_ARANGES_HEADER_SIZE;
6545
6546 /* Count the address/length pair for this compilation unit. */
6547 size += 2 * DWARF2_ADDR_SIZE;
6548 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6549
6550 /* Count the two zero words used to terminated the address range table. */
6551 size += 2 * DWARF2_ADDR_SIZE;
6552 return size;
6553 }
6554 \f
6555 /* Select the encoding of an attribute value. */
6556
6557 static enum dwarf_form
6558 value_format (dw_attr_ref a)
6559 {
6560 switch (a->dw_attr_val.val_class)
6561 {
6562 case dw_val_class_addr:
6563 return DW_FORM_addr;
6564 case dw_val_class_range_list:
6565 case dw_val_class_offset:
6566 switch (DWARF_OFFSET_SIZE)
6567 {
6568 case 4:
6569 return DW_FORM_data4;
6570 case 8:
6571 return DW_FORM_data8;
6572 default:
6573 gcc_unreachable ();
6574 }
6575 case dw_val_class_loc_list:
6576 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6577 .debug_loc section */
6578 return DW_FORM_data4;
6579 case dw_val_class_loc:
6580 switch (constant_size (size_of_locs (AT_loc (a))))
6581 {
6582 case 1:
6583 return DW_FORM_block1;
6584 case 2:
6585 return DW_FORM_block2;
6586 default:
6587 gcc_unreachable ();
6588 }
6589 case dw_val_class_const:
6590 return DW_FORM_sdata;
6591 case dw_val_class_unsigned_const:
6592 switch (constant_size (AT_unsigned (a)))
6593 {
6594 case 1:
6595 return DW_FORM_data1;
6596 case 2:
6597 return DW_FORM_data2;
6598 case 4:
6599 return DW_FORM_data4;
6600 case 8:
6601 return DW_FORM_data8;
6602 default:
6603 gcc_unreachable ();
6604 }
6605 case dw_val_class_long_long:
6606 return DW_FORM_block1;
6607 case dw_val_class_vec:
6608 return DW_FORM_block1;
6609 case dw_val_class_flag:
6610 return DW_FORM_flag;
6611 case dw_val_class_die_ref:
6612 if (AT_ref_external (a))
6613 return DW_FORM_ref_addr;
6614 else
6615 return DW_FORM_ref;
6616 case dw_val_class_fde_ref:
6617 return DW_FORM_data;
6618 case dw_val_class_lbl_id:
6619 return DW_FORM_addr;
6620 case dw_val_class_lbl_offset:
6621 return DW_FORM_data;
6622 case dw_val_class_str:
6623 return AT_string_form (a);
6624
6625 default:
6626 gcc_unreachable ();
6627 }
6628 }
6629
6630 /* Output the encoding of an attribute value. */
6631
6632 static void
6633 output_value_format (dw_attr_ref a)
6634 {
6635 enum dwarf_form form = value_format (a);
6636
6637 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6638 }
6639
6640 /* Output the .debug_abbrev section which defines the DIE abbreviation
6641 table. */
6642
6643 static void
6644 output_abbrev_section (void)
6645 {
6646 unsigned long abbrev_id;
6647
6648 dw_attr_ref a_attr;
6649
6650 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6651 {
6652 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6653
6654 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6655 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6656 dwarf_tag_name (abbrev->die_tag));
6657
6658 if (abbrev->die_child != NULL)
6659 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6660 else
6661 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6662
6663 for (a_attr = abbrev->die_attr; a_attr != NULL;
6664 a_attr = a_attr->dw_attr_next)
6665 {
6666 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6667 dwarf_attr_name (a_attr->dw_attr));
6668 output_value_format (a_attr);
6669 }
6670
6671 dw2_asm_output_data (1, 0, NULL);
6672 dw2_asm_output_data (1, 0, NULL);
6673 }
6674
6675 /* Terminate the table. */
6676 dw2_asm_output_data (1, 0, NULL);
6677 }
6678
6679 /* Output a symbol we can use to refer to this DIE from another CU. */
6680
6681 static inline void
6682 output_die_symbol (dw_die_ref die)
6683 {
6684 char *sym = die->die_symbol;
6685
6686 if (sym == 0)
6687 return;
6688
6689 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6690 /* We make these global, not weak; if the target doesn't support
6691 .linkonce, it doesn't support combining the sections, so debugging
6692 will break. */
6693 targetm.asm_out.globalize_label (asm_out_file, sym);
6694
6695 ASM_OUTPUT_LABEL (asm_out_file, sym);
6696 }
6697
6698 /* Return a new location list, given the begin and end range, and the
6699 expression. gensym tells us whether to generate a new internal symbol for
6700 this location list node, which is done for the head of the list only. */
6701
6702 static inline dw_loc_list_ref
6703 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6704 const char *section, unsigned int gensym)
6705 {
6706 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6707
6708 retlist->begin = begin;
6709 retlist->end = end;
6710 retlist->expr = expr;
6711 retlist->section = section;
6712 if (gensym)
6713 retlist->ll_symbol = gen_internal_sym ("LLST");
6714
6715 return retlist;
6716 }
6717
6718 /* Add a location description expression to a location list. */
6719
6720 static inline void
6721 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6722 const char *begin, const char *end,
6723 const char *section)
6724 {
6725 dw_loc_list_ref *d;
6726
6727 /* Find the end of the chain. */
6728 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6729 ;
6730
6731 /* Add a new location list node to the list. */
6732 *d = new_loc_list (descr, begin, end, section, 0);
6733 }
6734
6735 /* Output the location list given to us. */
6736
6737 static void
6738 output_loc_list (dw_loc_list_ref list_head)
6739 {
6740 dw_loc_list_ref curr = list_head;
6741
6742 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6743
6744 /* Walk the location list, and output each range + expression. */
6745 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6746 {
6747 unsigned long size;
6748 if (separate_line_info_table_in_use == 0)
6749 {
6750 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6751 "Location list begin address (%s)",
6752 list_head->ll_symbol);
6753 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6754 "Location list end address (%s)",
6755 list_head->ll_symbol);
6756 }
6757 else
6758 {
6759 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6760 "Location list begin address (%s)",
6761 list_head->ll_symbol);
6762 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6763 "Location list end address (%s)",
6764 list_head->ll_symbol);
6765 }
6766 size = size_of_locs (curr->expr);
6767
6768 /* Output the block length for this list of location operations. */
6769 gcc_assert (size <= 0xffff);
6770 dw2_asm_output_data (2, size, "%s", "Location expression size");
6771
6772 output_loc_sequence (curr->expr);
6773 }
6774
6775 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6776 "Location list terminator begin (%s)",
6777 list_head->ll_symbol);
6778 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6779 "Location list terminator end (%s)",
6780 list_head->ll_symbol);
6781 }
6782
6783 /* Output the DIE and its attributes. Called recursively to generate
6784 the definitions of each child DIE. */
6785
6786 static void
6787 output_die (dw_die_ref die)
6788 {
6789 dw_attr_ref a;
6790 dw_die_ref c;
6791 unsigned long size;
6792
6793 /* If someone in another CU might refer to us, set up a symbol for
6794 them to point to. */
6795 if (die->die_symbol)
6796 output_die_symbol (die);
6797
6798 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6799 die->die_offset, dwarf_tag_name (die->die_tag));
6800
6801 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6802 {
6803 const char *name = dwarf_attr_name (a->dw_attr);
6804
6805 switch (AT_class (a))
6806 {
6807 case dw_val_class_addr:
6808 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6809 break;
6810
6811 case dw_val_class_offset:
6812 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6813 "%s", name);
6814 break;
6815
6816 case dw_val_class_range_list:
6817 {
6818 char *p = strchr (ranges_section_label, '\0');
6819
6820 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6821 a->dw_attr_val.v.val_offset);
6822 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6823 "%s", name);
6824 *p = '\0';
6825 }
6826 break;
6827
6828 case dw_val_class_loc:
6829 size = size_of_locs (AT_loc (a));
6830
6831 /* Output the block length for this list of location operations. */
6832 dw2_asm_output_data (constant_size (size), size, "%s", name);
6833
6834 output_loc_sequence (AT_loc (a));
6835 break;
6836
6837 case dw_val_class_const:
6838 /* ??? It would be slightly more efficient to use a scheme like is
6839 used for unsigned constants below, but gdb 4.x does not sign
6840 extend. Gdb 5.x does sign extend. */
6841 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6842 break;
6843
6844 case dw_val_class_unsigned_const:
6845 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6846 AT_unsigned (a), "%s", name);
6847 break;
6848
6849 case dw_val_class_long_long:
6850 {
6851 unsigned HOST_WIDE_INT first, second;
6852
6853 dw2_asm_output_data (1,
6854 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6855 "%s", name);
6856
6857 if (WORDS_BIG_ENDIAN)
6858 {
6859 first = a->dw_attr_val.v.val_long_long.hi;
6860 second = a->dw_attr_val.v.val_long_long.low;
6861 }
6862 else
6863 {
6864 first = a->dw_attr_val.v.val_long_long.low;
6865 second = a->dw_attr_val.v.val_long_long.hi;
6866 }
6867
6868 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6869 first, "long long constant");
6870 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6871 second, NULL);
6872 }
6873 break;
6874
6875 case dw_val_class_vec:
6876 {
6877 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6878 unsigned int len = a->dw_attr_val.v.val_vec.length;
6879 unsigned int i;
6880 unsigned char *p;
6881
6882 dw2_asm_output_data (1, len * elt_size, "%s", name);
6883 if (elt_size > sizeof (HOST_WIDE_INT))
6884 {
6885 elt_size /= 2;
6886 len *= 2;
6887 }
6888 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6889 i < len;
6890 i++, p += elt_size)
6891 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6892 "fp or vector constant word %u", i);
6893 break;
6894 }
6895
6896 case dw_val_class_flag:
6897 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6898 break;
6899
6900 case dw_val_class_loc_list:
6901 {
6902 char *sym = AT_loc_list (a)->ll_symbol;
6903
6904 gcc_assert (sym);
6905 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
6906 }
6907 break;
6908
6909 case dw_val_class_die_ref:
6910 if (AT_ref_external (a))
6911 {
6912 char *sym = AT_ref (a)->die_symbol;
6913
6914 gcc_assert (sym);
6915 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6916 }
6917 else
6918 {
6919 gcc_assert (AT_ref (a)->die_offset);
6920 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6921 "%s", name);
6922 }
6923 break;
6924
6925 case dw_val_class_fde_ref:
6926 {
6927 char l1[20];
6928
6929 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6930 a->dw_attr_val.v.val_fde_index * 2);
6931 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6932 }
6933 break;
6934
6935 case dw_val_class_lbl_id:
6936 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6937 break;
6938
6939 case dw_val_class_lbl_offset:
6940 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6941 break;
6942
6943 case dw_val_class_str:
6944 if (AT_string_form (a) == DW_FORM_strp)
6945 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6946 a->dw_attr_val.v.val_str->label,
6947 "%s: \"%s\"", name, AT_string (a));
6948 else
6949 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6950 break;
6951
6952 default:
6953 gcc_unreachable ();
6954 }
6955 }
6956
6957 for (c = die->die_child; c != NULL; c = c->die_sib)
6958 output_die (c);
6959
6960 /* Add null byte to terminate sibling list. */
6961 if (die->die_child != NULL)
6962 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6963 die->die_offset);
6964 }
6965
6966 /* Output the compilation unit that appears at the beginning of the
6967 .debug_info section, and precedes the DIE descriptions. */
6968
6969 static void
6970 output_compilation_unit_header (void)
6971 {
6972 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
6973 dw2_asm_output_data (4, 0xffffffff,
6974 "Initial length escape value indicating 64-bit DWARF extension");
6975 dw2_asm_output_data (DWARF_OFFSET_SIZE,
6976 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
6977 "Length of Compilation Unit Info");
6978 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6979 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6980 "Offset Into Abbrev. Section");
6981 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6982 }
6983
6984 /* Output the compilation unit DIE and its children. */
6985
6986 static void
6987 output_comp_unit (dw_die_ref die, int output_if_empty)
6988 {
6989 const char *secname;
6990 char *oldsym, *tmp;
6991
6992 /* Unless we are outputting main CU, we may throw away empty ones. */
6993 if (!output_if_empty && die->die_child == NULL)
6994 return;
6995
6996 /* Even if there are no children of this DIE, we must output the information
6997 about the compilation unit. Otherwise, on an empty translation unit, we
6998 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
6999 will then complain when examining the file. First mark all the DIEs in
7000 this CU so we know which get local refs. */
7001 mark_dies (die);
7002
7003 build_abbrev_table (die);
7004
7005 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7006 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7007 calc_die_sizes (die);
7008
7009 oldsym = die->die_symbol;
7010 if (oldsym)
7011 {
7012 tmp = alloca (strlen (oldsym) + 24);
7013
7014 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7015 secname = tmp;
7016 die->die_symbol = NULL;
7017 }
7018 else
7019 secname = (const char *) DEBUG_INFO_SECTION;
7020
7021 /* Output debugging information. */
7022 named_section_flags (secname, SECTION_DEBUG);
7023 output_compilation_unit_header ();
7024 output_die (die);
7025
7026 /* Leave the marks on the main CU, so we can check them in
7027 output_pubnames. */
7028 if (oldsym)
7029 {
7030 unmark_dies (die);
7031 die->die_symbol = oldsym;
7032 }
7033 }
7034
7035 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7036 output of lang_hooks.decl_printable_name for C++ looks like
7037 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7038
7039 static const char *
7040 dwarf2_name (tree decl, int scope)
7041 {
7042 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7043 }
7044
7045 /* Add a new entry to .debug_pubnames if appropriate. */
7046
7047 static void
7048 add_pubname (tree decl, dw_die_ref die)
7049 {
7050 pubname_ref p;
7051
7052 if (! TREE_PUBLIC (decl))
7053 return;
7054
7055 if (pubname_table_in_use == pubname_table_allocated)
7056 {
7057 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7058 pubname_table
7059 = ggc_realloc (pubname_table,
7060 (pubname_table_allocated * sizeof (pubname_entry)));
7061 memset (pubname_table + pubname_table_in_use, 0,
7062 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7063 }
7064
7065 p = &pubname_table[pubname_table_in_use++];
7066 p->die = die;
7067 p->name = xstrdup (dwarf2_name (decl, 1));
7068 }
7069
7070 /* Output the public names table used to speed up access to externally
7071 visible names. For now, only generate entries for externally
7072 visible procedures. */
7073
7074 static void
7075 output_pubnames (void)
7076 {
7077 unsigned i;
7078 unsigned long pubnames_length = size_of_pubnames ();
7079
7080 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7081 dw2_asm_output_data (4, 0xffffffff,
7082 "Initial length escape value indicating 64-bit DWARF extension");
7083 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7084 "Length of Public Names Info");
7085 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7086 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7087 "Offset of Compilation Unit Info");
7088 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7089 "Compilation Unit Length");
7090
7091 for (i = 0; i < pubname_table_in_use; i++)
7092 {
7093 pubname_ref pub = &pubname_table[i];
7094
7095 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7096 gcc_assert (pub->die->die_mark);
7097
7098 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7099 "DIE offset");
7100
7101 dw2_asm_output_nstring (pub->name, -1, "external name");
7102 }
7103
7104 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7105 }
7106
7107 /* Add a new entry to .debug_aranges if appropriate. */
7108
7109 static void
7110 add_arange (tree decl, dw_die_ref die)
7111 {
7112 if (! DECL_SECTION_NAME (decl))
7113 return;
7114
7115 if (arange_table_in_use == arange_table_allocated)
7116 {
7117 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7118 arange_table = ggc_realloc (arange_table,
7119 (arange_table_allocated
7120 * sizeof (dw_die_ref)));
7121 memset (arange_table + arange_table_in_use, 0,
7122 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7123 }
7124
7125 arange_table[arange_table_in_use++] = die;
7126 }
7127
7128 /* Output the information that goes into the .debug_aranges table.
7129 Namely, define the beginning and ending address range of the
7130 text section generated for this compilation unit. */
7131
7132 static void
7133 output_aranges (void)
7134 {
7135 unsigned i;
7136 unsigned long aranges_length = size_of_aranges ();
7137
7138 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7139 dw2_asm_output_data (4, 0xffffffff,
7140 "Initial length escape value indicating 64-bit DWARF extension");
7141 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7142 "Length of Address Ranges Info");
7143 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7144 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7145 "Offset of Compilation Unit Info");
7146 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7147 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7148
7149 /* We need to align to twice the pointer size here. */
7150 if (DWARF_ARANGES_PAD_SIZE)
7151 {
7152 /* Pad using a 2 byte words so that padding is correct for any
7153 pointer size. */
7154 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7155 2 * DWARF2_ADDR_SIZE);
7156 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7157 dw2_asm_output_data (2, 0, NULL);
7158 }
7159
7160 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7161 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7162 text_section_label, "Length");
7163
7164 for (i = 0; i < arange_table_in_use; i++)
7165 {
7166 dw_die_ref die = arange_table[i];
7167
7168 /* We shouldn't see aranges for DIEs outside of the main CU. */
7169 gcc_assert (die->die_mark);
7170
7171 if (die->die_tag == DW_TAG_subprogram)
7172 {
7173 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7174 "Address");
7175 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7176 get_AT_low_pc (die), "Length");
7177 }
7178 else
7179 {
7180 /* A static variable; extract the symbol from DW_AT_location.
7181 Note that this code isn't currently hit, as we only emit
7182 aranges for functions (jason 9/23/99). */
7183 dw_attr_ref a = get_AT (die, DW_AT_location);
7184 dw_loc_descr_ref loc;
7185
7186 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7187
7188 loc = AT_loc (a);
7189 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7190
7191 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7192 loc->dw_loc_oprnd1.v.val_addr, "Address");
7193 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7194 get_AT_unsigned (die, DW_AT_byte_size),
7195 "Length");
7196 }
7197 }
7198
7199 /* Output the terminator words. */
7200 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7201 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7202 }
7203
7204 /* Add a new entry to .debug_ranges. Return the offset at which it
7205 was placed. */
7206
7207 static unsigned int
7208 add_ranges (tree block)
7209 {
7210 unsigned int in_use = ranges_table_in_use;
7211
7212 if (in_use == ranges_table_allocated)
7213 {
7214 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7215 ranges_table
7216 = ggc_realloc (ranges_table, (ranges_table_allocated
7217 * sizeof (struct dw_ranges_struct)));
7218 memset (ranges_table + ranges_table_in_use, 0,
7219 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7220 }
7221
7222 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7223 ranges_table_in_use = in_use + 1;
7224
7225 return in_use * 2 * DWARF2_ADDR_SIZE;
7226 }
7227
7228 static void
7229 output_ranges (void)
7230 {
7231 unsigned i;
7232 static const char *const start_fmt = "Offset 0x%x";
7233 const char *fmt = start_fmt;
7234
7235 for (i = 0; i < ranges_table_in_use; i++)
7236 {
7237 int block_num = ranges_table[i].block_num;
7238
7239 if (block_num)
7240 {
7241 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7242 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7243
7244 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7245 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7246
7247 /* If all code is in the text section, then the compilation
7248 unit base address defaults to DW_AT_low_pc, which is the
7249 base of the text section. */
7250 if (separate_line_info_table_in_use == 0)
7251 {
7252 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7253 text_section_label,
7254 fmt, i * 2 * DWARF2_ADDR_SIZE);
7255 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7256 text_section_label, NULL);
7257 }
7258
7259 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7260 compilation unit base address to zero, which allows us to
7261 use absolute addresses, and not worry about whether the
7262 target supports cross-section arithmetic. */
7263 else
7264 {
7265 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7266 fmt, i * 2 * DWARF2_ADDR_SIZE);
7267 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7268 }
7269
7270 fmt = NULL;
7271 }
7272 else
7273 {
7274 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7275 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7276 fmt = start_fmt;
7277 }
7278 }
7279 }
7280
7281 /* Data structure containing information about input files. */
7282 struct file_info
7283 {
7284 char *path; /* Complete file name. */
7285 char *fname; /* File name part. */
7286 int length; /* Length of entire string. */
7287 int file_idx; /* Index in input file table. */
7288 int dir_idx; /* Index in directory table. */
7289 };
7290
7291 /* Data structure containing information about directories with source
7292 files. */
7293 struct dir_info
7294 {
7295 char *path; /* Path including directory name. */
7296 int length; /* Path length. */
7297 int prefix; /* Index of directory entry which is a prefix. */
7298 int count; /* Number of files in this directory. */
7299 int dir_idx; /* Index of directory used as base. */
7300 int used; /* Used in the end? */
7301 };
7302
7303 /* Callback function for file_info comparison. We sort by looking at
7304 the directories in the path. */
7305
7306 static int
7307 file_info_cmp (const void *p1, const void *p2)
7308 {
7309 const struct file_info *s1 = p1;
7310 const struct file_info *s2 = p2;
7311 unsigned char *cp1;
7312 unsigned char *cp2;
7313
7314 /* Take care of file names without directories. We need to make sure that
7315 we return consistent values to qsort since some will get confused if
7316 we return the same value when identical operands are passed in opposite
7317 orders. So if neither has a directory, return 0 and otherwise return
7318 1 or -1 depending on which one has the directory. */
7319 if ((s1->path == s1->fname || s2->path == s2->fname))
7320 return (s2->path == s2->fname) - (s1->path == s1->fname);
7321
7322 cp1 = (unsigned char *) s1->path;
7323 cp2 = (unsigned char *) s2->path;
7324
7325 while (1)
7326 {
7327 ++cp1;
7328 ++cp2;
7329 /* Reached the end of the first path? If so, handle like above. */
7330 if ((cp1 == (unsigned char *) s1->fname)
7331 || (cp2 == (unsigned char *) s2->fname))
7332 return ((cp2 == (unsigned char *) s2->fname)
7333 - (cp1 == (unsigned char *) s1->fname));
7334
7335 /* Character of current path component the same? */
7336 else if (*cp1 != *cp2)
7337 return *cp1 - *cp2;
7338 }
7339 }
7340
7341 /* Output the directory table and the file name table. We try to minimize
7342 the total amount of memory needed. A heuristic is used to avoid large
7343 slowdowns with many input files. */
7344
7345 static void
7346 output_file_names (void)
7347 {
7348 struct file_info *files;
7349 struct dir_info *dirs;
7350 int *saved;
7351 int *savehere;
7352 int *backmap;
7353 size_t ndirs;
7354 int idx_offset;
7355 size_t i;
7356 int idx;
7357
7358 /* Handle the case where file_table is empty. */
7359 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7360 {
7361 dw2_asm_output_data (1, 0, "End directory table");
7362 dw2_asm_output_data (1, 0, "End file name table");
7363 return;
7364 }
7365
7366 /* Allocate the various arrays we need. */
7367 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7368 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7369
7370 /* Sort the file names. */
7371 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7372 {
7373 char *f;
7374
7375 /* Skip all leading "./". */
7376 f = VARRAY_CHAR_PTR (file_table, i);
7377 while (f[0] == '.' && f[1] == '/')
7378 f += 2;
7379
7380 /* Create a new array entry. */
7381 files[i].path = f;
7382 files[i].length = strlen (f);
7383 files[i].file_idx = i;
7384
7385 /* Search for the file name part. */
7386 f = strrchr (f, '/');
7387 files[i].fname = f == NULL ? files[i].path : f + 1;
7388 }
7389
7390 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7391 sizeof (files[0]), file_info_cmp);
7392
7393 /* Find all the different directories used. */
7394 dirs[0].path = files[1].path;
7395 dirs[0].length = files[1].fname - files[1].path;
7396 dirs[0].prefix = -1;
7397 dirs[0].count = 1;
7398 dirs[0].dir_idx = 0;
7399 dirs[0].used = 0;
7400 files[1].dir_idx = 0;
7401 ndirs = 1;
7402
7403 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7404 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7405 && memcmp (dirs[ndirs - 1].path, files[i].path,
7406 dirs[ndirs - 1].length) == 0)
7407 {
7408 /* Same directory as last entry. */
7409 files[i].dir_idx = ndirs - 1;
7410 ++dirs[ndirs - 1].count;
7411 }
7412 else
7413 {
7414 size_t j;
7415
7416 /* This is a new directory. */
7417 dirs[ndirs].path = files[i].path;
7418 dirs[ndirs].length = files[i].fname - files[i].path;
7419 dirs[ndirs].count = 1;
7420 dirs[ndirs].dir_idx = ndirs;
7421 dirs[ndirs].used = 0;
7422 files[i].dir_idx = ndirs;
7423
7424 /* Search for a prefix. */
7425 dirs[ndirs].prefix = -1;
7426 for (j = 0; j < ndirs; j++)
7427 if (dirs[j].length < dirs[ndirs].length
7428 && dirs[j].length > 1
7429 && (dirs[ndirs].prefix == -1
7430 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7431 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7432 dirs[ndirs].prefix = j;
7433
7434 ++ndirs;
7435 }
7436
7437 /* Now to the actual work. We have to find a subset of the directories which
7438 allow expressing the file name using references to the directory table
7439 with the least amount of characters. We do not do an exhaustive search
7440 where we would have to check out every combination of every single
7441 possible prefix. Instead we use a heuristic which provides nearly optimal
7442 results in most cases and never is much off. */
7443 saved = alloca (ndirs * sizeof (int));
7444 savehere = alloca (ndirs * sizeof (int));
7445
7446 memset (saved, '\0', ndirs * sizeof (saved[0]));
7447 for (i = 0; i < ndirs; i++)
7448 {
7449 size_t j;
7450 int total;
7451
7452 /* We can always save some space for the current directory. But this
7453 does not mean it will be enough to justify adding the directory. */
7454 savehere[i] = dirs[i].length;
7455 total = (savehere[i] - saved[i]) * dirs[i].count;
7456
7457 for (j = i + 1; j < ndirs; j++)
7458 {
7459 savehere[j] = 0;
7460 if (saved[j] < dirs[i].length)
7461 {
7462 /* Determine whether the dirs[i] path is a prefix of the
7463 dirs[j] path. */
7464 int k;
7465
7466 k = dirs[j].prefix;
7467 while (k != -1 && k != (int) i)
7468 k = dirs[k].prefix;
7469
7470 if (k == (int) i)
7471 {
7472 /* Yes it is. We can possibly safe some memory but
7473 writing the filenames in dirs[j] relative to
7474 dirs[i]. */
7475 savehere[j] = dirs[i].length;
7476 total += (savehere[j] - saved[j]) * dirs[j].count;
7477 }
7478 }
7479 }
7480
7481 /* Check whether we can safe enough to justify adding the dirs[i]
7482 directory. */
7483 if (total > dirs[i].length + 1)
7484 {
7485 /* It's worthwhile adding. */
7486 for (j = i; j < ndirs; j++)
7487 if (savehere[j] > 0)
7488 {
7489 /* Remember how much we saved for this directory so far. */
7490 saved[j] = savehere[j];
7491
7492 /* Remember the prefix directory. */
7493 dirs[j].dir_idx = i;
7494 }
7495 }
7496 }
7497
7498 /* We have to emit them in the order they appear in the file_table array
7499 since the index is used in the debug info generation. To do this
7500 efficiently we generate a back-mapping of the indices first. */
7501 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7502 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7503 {
7504 backmap[files[i].file_idx] = i;
7505
7506 /* Mark this directory as used. */
7507 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7508 }
7509
7510 /* That was it. We are ready to emit the information. First emit the
7511 directory name table. We have to make sure the first actually emitted
7512 directory name has index one; zero is reserved for the current working
7513 directory. Make sure we do not confuse these indices with the one for the
7514 constructed table (even though most of the time they are identical). */
7515 idx = 1;
7516 idx_offset = dirs[0].length > 0 ? 1 : 0;
7517 for (i = 1 - idx_offset; i < ndirs; i++)
7518 if (dirs[i].used != 0)
7519 {
7520 dirs[i].used = idx++;
7521 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7522 "Directory Entry: 0x%x", dirs[i].used);
7523 }
7524
7525 dw2_asm_output_data (1, 0, "End directory table");
7526
7527 /* Correct the index for the current working directory entry if it
7528 exists. */
7529 if (idx_offset == 0)
7530 dirs[0].used = 0;
7531
7532 /* Now write all the file names. */
7533 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7534 {
7535 int file_idx = backmap[i];
7536 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7537
7538 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7539 "File Entry: 0x%lx", (unsigned long) i);
7540
7541 /* Include directory index. */
7542 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7543
7544 /* Modification time. */
7545 dw2_asm_output_data_uleb128 (0, NULL);
7546
7547 /* File length in bytes. */
7548 dw2_asm_output_data_uleb128 (0, NULL);
7549 }
7550
7551 dw2_asm_output_data (1, 0, "End file name table");
7552 }
7553
7554
7555 /* Output the source line number correspondence information. This
7556 information goes into the .debug_line section. */
7557
7558 static void
7559 output_line_info (void)
7560 {
7561 char l1[20], l2[20], p1[20], p2[20];
7562 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7563 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7564 unsigned opc;
7565 unsigned n_op_args;
7566 unsigned long lt_index;
7567 unsigned long current_line;
7568 long line_offset;
7569 long line_delta;
7570 unsigned long current_file;
7571 unsigned long function;
7572
7573 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7574 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7575 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7576 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
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_delta (DWARF_OFFSET_SIZE, l2, l1,
7582 "Length of Source Line Info");
7583 ASM_OUTPUT_LABEL (asm_out_file, l1);
7584
7585 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7586 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7587 ASM_OUTPUT_LABEL (asm_out_file, p1);
7588
7589 /* Define the architecture-dependent minimum instruction length (in
7590 bytes). In this implementation of DWARF, this field is used for
7591 information purposes only. Since GCC generates assembly language,
7592 we have no a priori knowledge of how many instruction bytes are
7593 generated for each source line, and therefore can use only the
7594 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7595 commands. Accordingly, we fix this as `1', which is "correct
7596 enough" for all architectures, and don't let the target override. */
7597 dw2_asm_output_data (1, 1,
7598 "Minimum Instruction Length");
7599
7600 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7601 "Default is_stmt_start flag");
7602 dw2_asm_output_data (1, DWARF_LINE_BASE,
7603 "Line Base Value (Special Opcodes)");
7604 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7605 "Line Range Value (Special Opcodes)");
7606 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7607 "Special Opcode Base");
7608
7609 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7610 {
7611 switch (opc)
7612 {
7613 case DW_LNS_advance_pc:
7614 case DW_LNS_advance_line:
7615 case DW_LNS_set_file:
7616 case DW_LNS_set_column:
7617 case DW_LNS_fixed_advance_pc:
7618 n_op_args = 1;
7619 break;
7620 default:
7621 n_op_args = 0;
7622 break;
7623 }
7624
7625 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7626 opc, n_op_args);
7627 }
7628
7629 /* Write out the information about the files we use. */
7630 output_file_names ();
7631 ASM_OUTPUT_LABEL (asm_out_file, p2);
7632
7633 /* We used to set the address register to the first location in the text
7634 section here, but that didn't accomplish anything since we already
7635 have a line note for the opening brace of the first function. */
7636
7637 /* Generate the line number to PC correspondence table, encoded as
7638 a series of state machine operations. */
7639 current_file = 1;
7640 current_line = 1;
7641 strcpy (prev_line_label, text_section_label);
7642 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7643 {
7644 dw_line_info_ref line_info = &line_info_table[lt_index];
7645
7646 #if 0
7647 /* Disable this optimization for now; GDB wants to see two line notes
7648 at the beginning of a function so it can find the end of the
7649 prologue. */
7650
7651 /* Don't emit anything for redundant notes. Just updating the
7652 address doesn't accomplish anything, because we already assume
7653 that anything after the last address is this line. */
7654 if (line_info->dw_line_num == current_line
7655 && line_info->dw_file_num == current_file)
7656 continue;
7657 #endif
7658
7659 /* Emit debug info for the address of the current line.
7660
7661 Unfortunately, we have little choice here currently, and must always
7662 use the most general form. GCC does not know the address delta
7663 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7664 attributes which will give an upper bound on the address range. We
7665 could perhaps use length attributes to determine when it is safe to
7666 use DW_LNS_fixed_advance_pc. */
7667
7668 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7669 if (0)
7670 {
7671 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7672 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7673 "DW_LNS_fixed_advance_pc");
7674 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7675 }
7676 else
7677 {
7678 /* This can handle any delta. This takes
7679 4+DWARF2_ADDR_SIZE bytes. */
7680 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7681 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7682 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7683 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7684 }
7685
7686 strcpy (prev_line_label, line_label);
7687
7688 /* Emit debug info for the source file of the current line, if
7689 different from the previous line. */
7690 if (line_info->dw_file_num != current_file)
7691 {
7692 current_file = line_info->dw_file_num;
7693 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7694 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7695 VARRAY_CHAR_PTR (file_table,
7696 current_file));
7697 }
7698
7699 /* Emit debug info for the current line number, choosing the encoding
7700 that uses the least amount of space. */
7701 if (line_info->dw_line_num != current_line)
7702 {
7703 line_offset = line_info->dw_line_num - current_line;
7704 line_delta = line_offset - DWARF_LINE_BASE;
7705 current_line = line_info->dw_line_num;
7706 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7707 /* This can handle deltas from -10 to 234, using the current
7708 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7709 takes 1 byte. */
7710 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7711 "line %lu", current_line);
7712 else
7713 {
7714 /* This can handle any delta. This takes at least 4 bytes,
7715 depending on the value being encoded. */
7716 dw2_asm_output_data (1, DW_LNS_advance_line,
7717 "advance to line %lu", current_line);
7718 dw2_asm_output_data_sleb128 (line_offset, NULL);
7719 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7720 }
7721 }
7722 else
7723 /* We still need to start a new row, so output a copy insn. */
7724 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7725 }
7726
7727 /* Emit debug info for the address of the end of the function. */
7728 if (0)
7729 {
7730 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7731 "DW_LNS_fixed_advance_pc");
7732 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7733 }
7734 else
7735 {
7736 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7737 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7738 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7739 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7740 }
7741
7742 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7743 dw2_asm_output_data_uleb128 (1, NULL);
7744 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7745
7746 function = 0;
7747 current_file = 1;
7748 current_line = 1;
7749 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7750 {
7751 dw_separate_line_info_ref line_info
7752 = &separate_line_info_table[lt_index];
7753
7754 #if 0
7755 /* Don't emit anything for redundant notes. */
7756 if (line_info->dw_line_num == current_line
7757 && line_info->dw_file_num == current_file
7758 && line_info->function == function)
7759 goto cont;
7760 #endif
7761
7762 /* Emit debug info for the address of the current line. If this is
7763 a new function, or the first line of a function, then we need
7764 to handle it differently. */
7765 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7766 lt_index);
7767 if (function != line_info->function)
7768 {
7769 function = line_info->function;
7770
7771 /* Set the address register to the first line in the function. */
7772 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7773 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7774 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7775 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7776 }
7777 else
7778 {
7779 /* ??? See the DW_LNS_advance_pc comment above. */
7780 if (0)
7781 {
7782 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7783 "DW_LNS_fixed_advance_pc");
7784 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7785 }
7786 else
7787 {
7788 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7789 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7790 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7791 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7792 }
7793 }
7794
7795 strcpy (prev_line_label, line_label);
7796
7797 /* Emit debug info for the source file of the current line, if
7798 different from the previous line. */
7799 if (line_info->dw_file_num != current_file)
7800 {
7801 current_file = line_info->dw_file_num;
7802 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7803 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7804 VARRAY_CHAR_PTR (file_table,
7805 current_file));
7806 }
7807
7808 /* Emit debug info for the current line number, choosing the encoding
7809 that uses the least amount of space. */
7810 if (line_info->dw_line_num != current_line)
7811 {
7812 line_offset = line_info->dw_line_num - current_line;
7813 line_delta = line_offset - DWARF_LINE_BASE;
7814 current_line = line_info->dw_line_num;
7815 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7816 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7817 "line %lu", current_line);
7818 else
7819 {
7820 dw2_asm_output_data (1, DW_LNS_advance_line,
7821 "advance to line %lu", current_line);
7822 dw2_asm_output_data_sleb128 (line_offset, NULL);
7823 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7824 }
7825 }
7826 else
7827 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7828
7829 #if 0
7830 cont:
7831 #endif
7832
7833 lt_index++;
7834
7835 /* If we're done with a function, end its sequence. */
7836 if (lt_index == separate_line_info_table_in_use
7837 || separate_line_info_table[lt_index].function != function)
7838 {
7839 current_file = 1;
7840 current_line = 1;
7841
7842 /* Emit debug info for the address of the end of the function. */
7843 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7844 if (0)
7845 {
7846 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7847 "DW_LNS_fixed_advance_pc");
7848 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7849 }
7850 else
7851 {
7852 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7853 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7854 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7855 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7856 }
7857
7858 /* Output the marker for the end of this sequence. */
7859 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7860 dw2_asm_output_data_uleb128 (1, NULL);
7861 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7862 }
7863 }
7864
7865 /* Output the marker for the end of the line number info. */
7866 ASM_OUTPUT_LABEL (asm_out_file, l2);
7867 }
7868 \f
7869 /* Given a pointer to a tree node for some base type, return a pointer to
7870 a DIE that describes the given type.
7871
7872 This routine must only be called for GCC type nodes that correspond to
7873 Dwarf base (fundamental) types. */
7874
7875 static dw_die_ref
7876 base_type_die (tree type)
7877 {
7878 dw_die_ref base_type_result;
7879 const char *type_name;
7880 enum dwarf_type encoding;
7881 tree name = TYPE_NAME (type);
7882
7883 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7884 return 0;
7885
7886 if (name)
7887 {
7888 if (TREE_CODE (name) == TYPE_DECL)
7889 name = DECL_NAME (name);
7890
7891 type_name = IDENTIFIER_POINTER (name);
7892 }
7893 else
7894 type_name = "__unknown__";
7895
7896 switch (TREE_CODE (type))
7897 {
7898 case INTEGER_TYPE:
7899 /* Carefully distinguish the C character types, without messing
7900 up if the language is not C. Note that we check only for the names
7901 that contain spaces; other names might occur by coincidence in other
7902 languages. */
7903 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7904 && (type == char_type_node
7905 || ! strcmp (type_name, "signed char")
7906 || ! strcmp (type_name, "unsigned char"))))
7907 {
7908 if (TYPE_UNSIGNED (type))
7909 encoding = DW_ATE_unsigned;
7910 else
7911 encoding = DW_ATE_signed;
7912 break;
7913 }
7914 /* else fall through. */
7915
7916 case CHAR_TYPE:
7917 /* GNU Pascal/Ada CHAR type. Not used in C. */
7918 if (TYPE_UNSIGNED (type))
7919 encoding = DW_ATE_unsigned_char;
7920 else
7921 encoding = DW_ATE_signed_char;
7922 break;
7923
7924 case REAL_TYPE:
7925 encoding = DW_ATE_float;
7926 break;
7927
7928 /* Dwarf2 doesn't know anything about complex ints, so use
7929 a user defined type for it. */
7930 case COMPLEX_TYPE:
7931 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7932 encoding = DW_ATE_complex_float;
7933 else
7934 encoding = DW_ATE_lo_user;
7935 break;
7936
7937 case BOOLEAN_TYPE:
7938 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
7939 encoding = DW_ATE_boolean;
7940 break;
7941
7942 default:
7943 /* No other TREE_CODEs are Dwarf fundamental types. */
7944 gcc_unreachable ();
7945 }
7946
7947 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
7948 if (demangle_name_func)
7949 type_name = (*demangle_name_func) (type_name);
7950
7951 add_AT_string (base_type_result, DW_AT_name, type_name);
7952 add_AT_unsigned (base_type_result, DW_AT_byte_size,
7953 int_size_in_bytes (type));
7954 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7955
7956 return base_type_result;
7957 }
7958
7959 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7960 the Dwarf "root" type for the given input type. The Dwarf "root" type of
7961 a given type is generally the same as the given type, except that if the
7962 given type is a pointer or reference type, then the root type of the given
7963 type is the root type of the "basis" type for the pointer or reference
7964 type. (This definition of the "root" type is recursive.) Also, the root
7965 type of a `const' qualified type or a `volatile' qualified type is the
7966 root type of the given type without the qualifiers. */
7967
7968 static tree
7969 root_type (tree type)
7970 {
7971 if (TREE_CODE (type) == ERROR_MARK)
7972 return error_mark_node;
7973
7974 switch (TREE_CODE (type))
7975 {
7976 case ERROR_MARK:
7977 return error_mark_node;
7978
7979 case POINTER_TYPE:
7980 case REFERENCE_TYPE:
7981 return type_main_variant (root_type (TREE_TYPE (type)));
7982
7983 default:
7984 return type_main_variant (type);
7985 }
7986 }
7987
7988 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
7989 given input type is a Dwarf "fundamental" type. Otherwise return null. */
7990
7991 static inline int
7992 is_base_type (tree type)
7993 {
7994 switch (TREE_CODE (type))
7995 {
7996 case ERROR_MARK:
7997 case VOID_TYPE:
7998 case INTEGER_TYPE:
7999 case REAL_TYPE:
8000 case COMPLEX_TYPE:
8001 case BOOLEAN_TYPE:
8002 case CHAR_TYPE:
8003 return 1;
8004
8005 case SET_TYPE:
8006 case ARRAY_TYPE:
8007 case RECORD_TYPE:
8008 case UNION_TYPE:
8009 case QUAL_UNION_TYPE:
8010 case ENUMERAL_TYPE:
8011 case FUNCTION_TYPE:
8012 case METHOD_TYPE:
8013 case POINTER_TYPE:
8014 case REFERENCE_TYPE:
8015 case FILE_TYPE:
8016 case OFFSET_TYPE:
8017 case LANG_TYPE:
8018 case VECTOR_TYPE:
8019 return 0;
8020
8021 default:
8022 gcc_unreachable ();
8023 }
8024
8025 return 0;
8026 }
8027
8028 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8029 node, return the size in bits for the type if it is a constant, or else
8030 return the alignment for the type if the type's size is not constant, or
8031 else return BITS_PER_WORD if the type actually turns out to be an
8032 ERROR_MARK node. */
8033
8034 static inline unsigned HOST_WIDE_INT
8035 simple_type_size_in_bits (tree type)
8036 {
8037 if (TREE_CODE (type) == ERROR_MARK)
8038 return BITS_PER_WORD;
8039 else if (TYPE_SIZE (type) == NULL_TREE)
8040 return 0;
8041 else if (host_integerp (TYPE_SIZE (type), 1))
8042 return tree_low_cst (TYPE_SIZE (type), 1);
8043 else
8044 return TYPE_ALIGN (type);
8045 }
8046
8047 /* Return true if the debug information for the given type should be
8048 emitted as a subrange type. */
8049
8050 static inline bool
8051 is_subrange_type (tree type)
8052 {
8053 tree subtype = TREE_TYPE (type);
8054
8055 /* Subrange types are identified by the fact that they are integer
8056 types, and that they have a subtype which is either an integer type
8057 or an enumeral type. */
8058
8059 if (TREE_CODE (type) != INTEGER_TYPE
8060 || subtype == NULL_TREE)
8061 return false;
8062
8063 if (TREE_CODE (subtype) != INTEGER_TYPE
8064 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8065 return false;
8066
8067 if (TREE_CODE (type) == TREE_CODE (subtype)
8068 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8069 && TYPE_MIN_VALUE (type) != NULL
8070 && TYPE_MIN_VALUE (subtype) != NULL
8071 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8072 && TYPE_MAX_VALUE (type) != NULL
8073 && TYPE_MAX_VALUE (subtype) != NULL
8074 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8075 {
8076 /* The type and its subtype have the same representation. If in
8077 addition the two types also have the same name, then the given
8078 type is not a subrange type, but rather a plain base type. */
8079 /* FIXME: brobecker/2004-03-22:
8080 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8081 therefore be sufficient to check the TYPE_SIZE node pointers
8082 rather than checking the actual size. Unfortunately, we have
8083 found some cases, such as in the Ada "integer" type, where
8084 this is not the case. Until this problem is solved, we need to
8085 keep checking the actual size. */
8086 tree type_name = TYPE_NAME (type);
8087 tree subtype_name = TYPE_NAME (subtype);
8088
8089 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8090 type_name = DECL_NAME (type_name);
8091
8092 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8093 subtype_name = DECL_NAME (subtype_name);
8094
8095 if (type_name == subtype_name)
8096 return false;
8097 }
8098
8099 return true;
8100 }
8101
8102 /* Given a pointer to a tree node for a subrange type, return a pointer
8103 to a DIE that describes the given type. */
8104
8105 static dw_die_ref
8106 subrange_type_die (tree type, dw_die_ref context_die)
8107 {
8108 dw_die_ref subtype_die;
8109 dw_die_ref subrange_die;
8110 tree name = TYPE_NAME (type);
8111 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8112 tree subtype = TREE_TYPE (type);
8113
8114 if (context_die == NULL)
8115 context_die = comp_unit_die;
8116
8117 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8118 subtype_die = gen_enumeration_type_die (subtype, context_die);
8119 else
8120 subtype_die = base_type_die (subtype);
8121
8122 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8123
8124 if (name != NULL)
8125 {
8126 if (TREE_CODE (name) == TYPE_DECL)
8127 name = DECL_NAME (name);
8128 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8129 }
8130
8131 if (int_size_in_bytes (subtype) != size_in_bytes)
8132 {
8133 /* The size of the subrange type and its base type do not match,
8134 so we need to generate a size attribute for the subrange type. */
8135 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8136 }
8137
8138 if (TYPE_MIN_VALUE (type) != NULL)
8139 add_bound_info (subrange_die, DW_AT_lower_bound,
8140 TYPE_MIN_VALUE (type));
8141 if (TYPE_MAX_VALUE (type) != NULL)
8142 add_bound_info (subrange_die, DW_AT_upper_bound,
8143 TYPE_MAX_VALUE (type));
8144 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8145
8146 return subrange_die;
8147 }
8148
8149 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8150 entry that chains various modifiers in front of the given type. */
8151
8152 static dw_die_ref
8153 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8154 dw_die_ref context_die)
8155 {
8156 enum tree_code code = TREE_CODE (type);
8157 dw_die_ref mod_type_die = NULL;
8158 dw_die_ref sub_die = NULL;
8159 tree item_type = NULL;
8160
8161 if (code != ERROR_MARK)
8162 {
8163 tree qualified_type;
8164
8165 /* See if we already have the appropriately qualified variant of
8166 this type. */
8167 qualified_type
8168 = get_qualified_type (type,
8169 ((is_const_type ? TYPE_QUAL_CONST : 0)
8170 | (is_volatile_type
8171 ? TYPE_QUAL_VOLATILE : 0)));
8172
8173 /* If we do, then we can just use its DIE, if it exists. */
8174 if (qualified_type)
8175 {
8176 mod_type_die = lookup_type_die (qualified_type);
8177 if (mod_type_die)
8178 return mod_type_die;
8179 }
8180
8181 /* Handle C typedef types. */
8182 if (qualified_type && TYPE_NAME (qualified_type)
8183 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8184 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8185 {
8186 tree type_name = TYPE_NAME (qualified_type);
8187 tree dtype = TREE_TYPE (type_name);
8188
8189 if (qualified_type == dtype)
8190 {
8191 /* For a named type, use the typedef. */
8192 gen_type_die (qualified_type, context_die);
8193 mod_type_die = lookup_type_die (qualified_type);
8194 }
8195 else if (is_const_type < TYPE_READONLY (dtype)
8196 || is_volatile_type < TYPE_VOLATILE (dtype))
8197 /* cv-unqualified version of named type. Just use the unnamed
8198 type to which it refers. */
8199 mod_type_die
8200 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8201 is_const_type, is_volatile_type,
8202 context_die);
8203
8204 /* Else cv-qualified version of named type; fall through. */
8205 }
8206
8207 if (mod_type_die)
8208 /* OK. */
8209 ;
8210 else if (is_const_type)
8211 {
8212 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8213 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8214 }
8215 else if (is_volatile_type)
8216 {
8217 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8218 sub_die = modified_type_die (type, 0, 0, context_die);
8219 }
8220 else if (code == POINTER_TYPE)
8221 {
8222 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8223 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8224 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8225 #if 0
8226 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8227 #endif
8228 item_type = TREE_TYPE (type);
8229 }
8230 else if (code == REFERENCE_TYPE)
8231 {
8232 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8233 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8234 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8235 #if 0
8236 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8237 #endif
8238 item_type = TREE_TYPE (type);
8239 }
8240 else if (is_subrange_type (type))
8241 mod_type_die = subrange_type_die (type, context_die);
8242 else if (is_base_type (type))
8243 mod_type_die = base_type_die (type);
8244 else
8245 {
8246 gen_type_die (type, context_die);
8247
8248 /* We have to get the type_main_variant here (and pass that to the
8249 `lookup_type_die' routine) because the ..._TYPE node we have
8250 might simply be a *copy* of some original type node (where the
8251 copy was created to help us keep track of typedef names) and
8252 that copy might have a different TYPE_UID from the original
8253 ..._TYPE node. */
8254 if (TREE_CODE (type) != VECTOR_TYPE)
8255 mod_type_die = lookup_type_die (type_main_variant (type));
8256 else
8257 /* Vectors have the debugging information in the type,
8258 not the main variant. */
8259 mod_type_die = lookup_type_die (type);
8260 gcc_assert (mod_type_die);
8261 }
8262
8263 /* We want to equate the qualified type to the die below. */
8264 type = qualified_type;
8265 }
8266
8267 if (type)
8268 equate_type_number_to_die (type, mod_type_die);
8269 if (item_type)
8270 /* We must do this after the equate_type_number_to_die call, in case
8271 this is a recursive type. This ensures that the modified_type_die
8272 recursion will terminate even if the type is recursive. Recursive
8273 types are possible in Ada. */
8274 sub_die = modified_type_die (item_type,
8275 TYPE_READONLY (item_type),
8276 TYPE_VOLATILE (item_type),
8277 context_die);
8278
8279 if (sub_die != NULL)
8280 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8281
8282 return mod_type_die;
8283 }
8284
8285 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8286 an enumerated type. */
8287
8288 static inline int
8289 type_is_enum (tree type)
8290 {
8291 return TREE_CODE (type) == ENUMERAL_TYPE;
8292 }
8293
8294 /* Return the DBX register number described by a given RTL node. */
8295
8296 static unsigned int
8297 dbx_reg_number (rtx rtl)
8298 {
8299 unsigned regno = REGNO (rtl);
8300
8301 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8302
8303 return DBX_REGISTER_NUMBER (regno);
8304 }
8305
8306 /* Return a location descriptor that designates a machine register or
8307 zero if there is none. */
8308
8309 static dw_loc_descr_ref
8310 reg_loc_descriptor (rtx rtl)
8311 {
8312 unsigned reg;
8313 rtx regs;
8314
8315 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8316 return 0;
8317
8318 reg = dbx_reg_number (rtl);
8319 regs = targetm.dwarf_register_span (rtl);
8320
8321 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8322 || regs)
8323 return multiple_reg_loc_descriptor (rtl, regs);
8324 else
8325 return one_reg_loc_descriptor (reg);
8326 }
8327
8328 /* Return a location descriptor that designates a machine register for
8329 a given hard register number. */
8330
8331 static dw_loc_descr_ref
8332 one_reg_loc_descriptor (unsigned int regno)
8333 {
8334 if (regno <= 31)
8335 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8336 else
8337 return new_loc_descr (DW_OP_regx, regno, 0);
8338 }
8339
8340 /* Given an RTL of a register, return a location descriptor that
8341 designates a value that spans more than one register. */
8342
8343 static dw_loc_descr_ref
8344 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8345 {
8346 int nregs, size, i;
8347 unsigned reg;
8348 dw_loc_descr_ref loc_result = NULL;
8349
8350 reg = dbx_reg_number (rtl);
8351 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8352
8353 /* Simple, contiguous registers. */
8354 if (regs == NULL_RTX)
8355 {
8356 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8357
8358 loc_result = NULL;
8359 while (nregs--)
8360 {
8361 dw_loc_descr_ref t;
8362
8363 t = one_reg_loc_descriptor (reg);
8364 add_loc_descr (&loc_result, t);
8365 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8366 ++reg;
8367 }
8368 return loc_result;
8369 }
8370
8371 /* Now onto stupid register sets in non contiguous locations. */
8372
8373 gcc_assert (GET_CODE (regs) == PARALLEL);
8374
8375 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8376 loc_result = NULL;
8377
8378 for (i = 0; i < XVECLEN (regs, 0); ++i)
8379 {
8380 dw_loc_descr_ref t;
8381
8382 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8383 add_loc_descr (&loc_result, t);
8384 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8385 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8386 }
8387 return loc_result;
8388 }
8389
8390 /* Return a location descriptor that designates a constant. */
8391
8392 static dw_loc_descr_ref
8393 int_loc_descriptor (HOST_WIDE_INT i)
8394 {
8395 enum dwarf_location_atom op;
8396
8397 /* Pick the smallest representation of a constant, rather than just
8398 defaulting to the LEB encoding. */
8399 if (i >= 0)
8400 {
8401 if (i <= 31)
8402 op = DW_OP_lit0 + i;
8403 else if (i <= 0xff)
8404 op = DW_OP_const1u;
8405 else if (i <= 0xffff)
8406 op = DW_OP_const2u;
8407 else if (HOST_BITS_PER_WIDE_INT == 32
8408 || i <= 0xffffffff)
8409 op = DW_OP_const4u;
8410 else
8411 op = DW_OP_constu;
8412 }
8413 else
8414 {
8415 if (i >= -0x80)
8416 op = DW_OP_const1s;
8417 else if (i >= -0x8000)
8418 op = DW_OP_const2s;
8419 else if (HOST_BITS_PER_WIDE_INT == 32
8420 || i >= -0x80000000)
8421 op = DW_OP_const4s;
8422 else
8423 op = DW_OP_consts;
8424 }
8425
8426 return new_loc_descr (op, i, 0);
8427 }
8428
8429 /* Return a location descriptor that designates a base+offset location. */
8430
8431 static dw_loc_descr_ref
8432 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8433 {
8434 dw_loc_descr_ref loc_result;
8435 /* For the "frame base", we use the frame pointer or stack pointer
8436 registers, since the RTL for local variables is relative to one of
8437 them. */
8438 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8439 ? HARD_FRAME_POINTER_REGNUM
8440 : STACK_POINTER_REGNUM);
8441
8442 if (reg == fp_reg && can_use_fbreg)
8443 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8444 else if (reg <= 31)
8445 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8446 else
8447 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8448
8449 return loc_result;
8450 }
8451
8452 /* Return true if this RTL expression describes a base+offset calculation. */
8453
8454 static inline int
8455 is_based_loc (rtx rtl)
8456 {
8457 return (GET_CODE (rtl) == PLUS
8458 && ((REG_P (XEXP (rtl, 0))
8459 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8460 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8461 }
8462
8463 /* The following routine converts the RTL for a variable or parameter
8464 (resident in memory) into an equivalent Dwarf representation of a
8465 mechanism for getting the address of that same variable onto the top of a
8466 hypothetical "address evaluation" stack.
8467
8468 When creating memory location descriptors, we are effectively transforming
8469 the RTL for a memory-resident object into its Dwarf postfix expression
8470 equivalent. This routine recursively descends an RTL tree, turning
8471 it into Dwarf postfix code as it goes.
8472
8473 MODE is the mode of the memory reference, needed to handle some
8474 autoincrement addressing modes.
8475
8476 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8477 list for RTL. We can't use it when we are emitting location list for
8478 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8479 which describes how frame base changes when !frame_pointer_needed.
8480
8481 Return 0 if we can't represent the location. */
8482
8483 static dw_loc_descr_ref
8484 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8485 {
8486 dw_loc_descr_ref mem_loc_result = NULL;
8487 enum dwarf_location_atom op;
8488
8489 /* Note that for a dynamically sized array, the location we will generate a
8490 description of here will be the lowest numbered location which is
8491 actually within the array. That's *not* necessarily the same as the
8492 zeroth element of the array. */
8493
8494 rtl = targetm.delegitimize_address (rtl);
8495
8496 switch (GET_CODE (rtl))
8497 {
8498 case POST_INC:
8499 case POST_DEC:
8500 case POST_MODIFY:
8501 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8502 just fall into the SUBREG code. */
8503
8504 /* ... fall through ... */
8505
8506 case SUBREG:
8507 /* The case of a subreg may arise when we have a local (register)
8508 variable or a formal (register) parameter which doesn't quite fill
8509 up an entire register. For now, just assume that it is
8510 legitimate to make the Dwarf info refer to the whole register which
8511 contains the given subreg. */
8512 rtl = SUBREG_REG (rtl);
8513
8514 /* ... fall through ... */
8515
8516 case REG:
8517 /* Whenever a register number forms a part of the description of the
8518 method for calculating the (dynamic) address of a memory resident
8519 object, DWARF rules require the register number be referred to as
8520 a "base register". This distinction is not based in any way upon
8521 what category of register the hardware believes the given register
8522 belongs to. This is strictly DWARF terminology we're dealing with
8523 here. Note that in cases where the location of a memory-resident
8524 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8525 OP_CONST (0)) the actual DWARF location descriptor that we generate
8526 may just be OP_BASEREG (basereg). This may look deceptively like
8527 the object in question was allocated to a register (rather than in
8528 memory) so DWARF consumers need to be aware of the subtle
8529 distinction between OP_REG and OP_BASEREG. */
8530 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8531 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8532 can_use_fbreg);
8533 break;
8534
8535 case MEM:
8536 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8537 can_use_fbreg);
8538 if (mem_loc_result != 0)
8539 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8540 break;
8541
8542 case LO_SUM:
8543 rtl = XEXP (rtl, 1);
8544
8545 /* ... fall through ... */
8546
8547 case LABEL_REF:
8548 /* Some ports can transform a symbol ref into a label ref, because
8549 the symbol ref is too far away and has to be dumped into a constant
8550 pool. */
8551 case CONST:
8552 case SYMBOL_REF:
8553 /* Alternatively, the symbol in the constant pool might be referenced
8554 by a different symbol. */
8555 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8556 {
8557 bool marked;
8558 rtx tmp = get_pool_constant_mark (rtl, &marked);
8559
8560 if (GET_CODE (tmp) == SYMBOL_REF)
8561 {
8562 rtl = tmp;
8563 if (CONSTANT_POOL_ADDRESS_P (tmp))
8564 get_pool_constant_mark (tmp, &marked);
8565 else
8566 marked = true;
8567 }
8568
8569 /* If all references to this pool constant were optimized away,
8570 it was not output and thus we can't represent it.
8571 FIXME: might try to use DW_OP_const_value here, though
8572 DW_OP_piece complicates it. */
8573 if (!marked)
8574 return 0;
8575 }
8576
8577 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8578 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8579 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8580 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8581 break;
8582
8583 case PRE_MODIFY:
8584 /* Extract the PLUS expression nested inside and fall into
8585 PLUS code below. */
8586 rtl = XEXP (rtl, 1);
8587 goto plus;
8588
8589 case PRE_INC:
8590 case PRE_DEC:
8591 /* Turn these into a PLUS expression and fall into the PLUS code
8592 below. */
8593 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8594 GEN_INT (GET_CODE (rtl) == PRE_INC
8595 ? GET_MODE_UNIT_SIZE (mode)
8596 : -GET_MODE_UNIT_SIZE (mode)));
8597
8598 /* ... fall through ... */
8599
8600 case PLUS:
8601 plus:
8602 if (is_based_loc (rtl))
8603 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8604 INTVAL (XEXP (rtl, 1)),
8605 can_use_fbreg);
8606 else
8607 {
8608 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8609 can_use_fbreg);
8610 if (mem_loc_result == 0)
8611 break;
8612
8613 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8614 && INTVAL (XEXP (rtl, 1)) >= 0)
8615 add_loc_descr (&mem_loc_result,
8616 new_loc_descr (DW_OP_plus_uconst,
8617 INTVAL (XEXP (rtl, 1)), 0));
8618 else
8619 {
8620 add_loc_descr (&mem_loc_result,
8621 mem_loc_descriptor (XEXP (rtl, 1), mode,
8622 can_use_fbreg));
8623 add_loc_descr (&mem_loc_result,
8624 new_loc_descr (DW_OP_plus, 0, 0));
8625 }
8626 }
8627 break;
8628
8629 /* If a pseudo-reg is optimized away, it is possible for it to
8630 be replaced with a MEM containing a multiply or shift. */
8631 case MULT:
8632 op = DW_OP_mul;
8633 goto do_binop;
8634
8635 case ASHIFT:
8636 op = DW_OP_shl;
8637 goto do_binop;
8638
8639 case ASHIFTRT:
8640 op = DW_OP_shra;
8641 goto do_binop;
8642
8643 case LSHIFTRT:
8644 op = DW_OP_shr;
8645 goto do_binop;
8646
8647 do_binop:
8648 {
8649 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8650 can_use_fbreg);
8651 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8652 can_use_fbreg);
8653
8654 if (op0 == 0 || op1 == 0)
8655 break;
8656
8657 mem_loc_result = op0;
8658 add_loc_descr (&mem_loc_result, op1);
8659 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8660 break;
8661 }
8662
8663 case CONST_INT:
8664 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8665 break;
8666
8667 default:
8668 gcc_unreachable ();
8669 }
8670
8671 return mem_loc_result;
8672 }
8673
8674 /* Return a descriptor that describes the concatenation of two locations.
8675 This is typically a complex variable. */
8676
8677 static dw_loc_descr_ref
8678 concat_loc_descriptor (rtx x0, rtx x1)
8679 {
8680 dw_loc_descr_ref cc_loc_result = NULL;
8681 dw_loc_descr_ref x0_ref = loc_descriptor (x0, true);
8682 dw_loc_descr_ref x1_ref = loc_descriptor (x1, true);
8683
8684 if (x0_ref == 0 || x1_ref == 0)
8685 return 0;
8686
8687 cc_loc_result = x0_ref;
8688 add_loc_descr (&cc_loc_result,
8689 new_loc_descr (DW_OP_piece,
8690 GET_MODE_SIZE (GET_MODE (x0)), 0));
8691
8692 add_loc_descr (&cc_loc_result, x1_ref);
8693 add_loc_descr (&cc_loc_result,
8694 new_loc_descr (DW_OP_piece,
8695 GET_MODE_SIZE (GET_MODE (x1)), 0));
8696
8697 return cc_loc_result;
8698 }
8699
8700 /* Output a proper Dwarf location descriptor for a variable or parameter
8701 which is either allocated in a register or in a memory location. For a
8702 register, we just generate an OP_REG and the register number. For a
8703 memory location we provide a Dwarf postfix expression describing how to
8704 generate the (dynamic) address of the object onto the address stack.
8705
8706 If we don't know how to describe it, return 0. */
8707
8708 static dw_loc_descr_ref
8709 loc_descriptor (rtx rtl, bool can_use_fbreg)
8710 {
8711 dw_loc_descr_ref loc_result = NULL;
8712
8713 switch (GET_CODE (rtl))
8714 {
8715 case SUBREG:
8716 /* The case of a subreg may arise when we have a local (register)
8717 variable or a formal (register) parameter which doesn't quite fill
8718 up an entire register. For now, just assume that it is
8719 legitimate to make the Dwarf info refer to the whole register which
8720 contains the given subreg. */
8721 rtl = SUBREG_REG (rtl);
8722
8723 /* ... fall through ... */
8724
8725 case REG:
8726 loc_result = reg_loc_descriptor (rtl);
8727 break;
8728
8729 case MEM:
8730 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8731 can_use_fbreg);
8732 break;
8733
8734 case CONCAT:
8735 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8736 break;
8737
8738 case VAR_LOCATION:
8739 /* Single part. */
8740 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8741 {
8742 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8743 break;
8744 }
8745
8746 rtl = XEXP (rtl, 1);
8747 /* FALLTHRU */
8748
8749 case PARALLEL:
8750 {
8751 rtvec par_elems = XVEC (rtl, 0);
8752 int num_elem = GET_NUM_ELEM (par_elems);
8753 enum machine_mode mode;
8754 int i;
8755
8756 /* Create the first one, so we have something to add to. */
8757 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8758 can_use_fbreg);
8759 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8760 add_loc_descr (&loc_result,
8761 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8762 for (i = 1; i < num_elem; i++)
8763 {
8764 dw_loc_descr_ref temp;
8765
8766 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8767 can_use_fbreg);
8768 add_loc_descr (&loc_result, temp);
8769 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8770 add_loc_descr (&loc_result,
8771 new_loc_descr (DW_OP_piece,
8772 GET_MODE_SIZE (mode), 0));
8773 }
8774 }
8775 break;
8776
8777 default:
8778 gcc_unreachable ();
8779 }
8780
8781 return loc_result;
8782 }
8783
8784 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8785 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
8786 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
8787 top-level invocation, and we require the address of LOC; is 0 if we require
8788 the value of LOC. */
8789
8790 static dw_loc_descr_ref
8791 loc_descriptor_from_tree_1 (tree loc, int want_address)
8792 {
8793 dw_loc_descr_ref ret, ret1;
8794 int have_address = 0;
8795 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8796 enum dwarf_location_atom op;
8797
8798 /* ??? Most of the time we do not take proper care for sign/zero
8799 extending the values properly. Hopefully this won't be a real
8800 problem... */
8801
8802 switch (TREE_CODE (loc))
8803 {
8804 case ERROR_MARK:
8805 return 0;
8806
8807 case PLACEHOLDER_EXPR:
8808 /* This case involves extracting fields from an object to determine the
8809 position of other fields. We don't try to encode this here. The
8810 only user of this is Ada, which encodes the needed information using
8811 the names of types. */
8812 return 0;
8813
8814 case CALL_EXPR:
8815 return 0;
8816
8817 case PREINCREMENT_EXPR:
8818 case PREDECREMENT_EXPR:
8819 case POSTINCREMENT_EXPR:
8820 case POSTDECREMENT_EXPR:
8821 /* There are no opcodes for these operations. */
8822 return 0;
8823
8824 case ADDR_EXPR:
8825 /* If we already want an address, there's nothing we can do. */
8826 if (want_address)
8827 return 0;
8828
8829 /* Otherwise, process the argument and look for the address. */
8830 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
8831
8832 case VAR_DECL:
8833 if (DECL_THREAD_LOCAL (loc))
8834 {
8835 rtx rtl;
8836
8837 #ifndef ASM_OUTPUT_DWARF_DTPREL
8838 /* If this is not defined, we have no way to emit the data. */
8839 return 0;
8840 #endif
8841
8842 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8843 look up addresses of objects in the current module. */
8844 if (DECL_EXTERNAL (loc))
8845 return 0;
8846
8847 rtl = rtl_for_decl_location (loc);
8848 if (rtl == NULL_RTX)
8849 return 0;
8850
8851 if (!MEM_P (rtl))
8852 return 0;
8853 rtl = XEXP (rtl, 0);
8854 if (! CONSTANT_P (rtl))
8855 return 0;
8856
8857 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8858 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8859 ret->dw_loc_oprnd1.v.val_addr = rtl;
8860
8861 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8862 add_loc_descr (&ret, ret1);
8863
8864 have_address = 1;
8865 break;
8866 }
8867 /* FALLTHRU */
8868
8869 case PARM_DECL:
8870 if (DECL_VALUE_EXPR (loc))
8871 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc), want_address);
8872 /* FALLTHRU */
8873
8874 case RESULT_DECL:
8875 {
8876 rtx rtl = rtl_for_decl_location (loc);
8877
8878 if (rtl == NULL_RTX)
8879 return 0;
8880 else if (GET_CODE (rtl) == CONST_INT)
8881 {
8882 HOST_WIDE_INT val = INTVAL (rtl);
8883 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
8884 val &= GET_MODE_MASK (DECL_MODE (loc));
8885 ret = int_loc_descriptor (val);
8886 }
8887 else if (GET_CODE (rtl) == CONST_STRING)
8888 return 0;
8889 else if (CONSTANT_P (rtl))
8890 {
8891 ret = new_loc_descr (DW_OP_addr, 0, 0);
8892 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8893 ret->dw_loc_oprnd1.v.val_addr = rtl;
8894 }
8895 else
8896 {
8897 enum machine_mode mode;
8898
8899 /* Certain constructs can only be represented at top-level. */
8900 if (want_address == 2)
8901 return loc_descriptor (rtl, true);
8902
8903 mode = GET_MODE (rtl);
8904 if (MEM_P (rtl))
8905 {
8906 rtl = XEXP (rtl, 0);
8907 have_address = 1;
8908 }
8909 ret = mem_loc_descriptor (rtl, mode, true);
8910 }
8911 }
8912 break;
8913
8914 case INDIRECT_REF:
8915 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
8916 have_address = 1;
8917 break;
8918
8919 case COMPOUND_EXPR:
8920 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
8921
8922 case NOP_EXPR:
8923 case CONVERT_EXPR:
8924 case NON_LVALUE_EXPR:
8925 case VIEW_CONVERT_EXPR:
8926 case SAVE_EXPR:
8927 case MODIFY_EXPR:
8928 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
8929
8930 case COMPONENT_REF:
8931 case BIT_FIELD_REF:
8932 case ARRAY_REF:
8933 case ARRAY_RANGE_REF:
8934 {
8935 tree obj, offset;
8936 HOST_WIDE_INT bitsize, bitpos, bytepos;
8937 enum machine_mode mode;
8938 int volatilep;
8939
8940 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8941 &unsignedp, &volatilep);
8942
8943 if (obj == loc)
8944 return 0;
8945
8946 ret = loc_descriptor_from_tree_1 (obj, 1);
8947 if (ret == 0
8948 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8949 return 0;
8950
8951 if (offset != NULL_TREE)
8952 {
8953 /* Variable offset. */
8954 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
8955 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8956 }
8957
8958 bytepos = bitpos / BITS_PER_UNIT;
8959 if (bytepos > 0)
8960 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8961 else if (bytepos < 0)
8962 {
8963 add_loc_descr (&ret, int_loc_descriptor (bytepos));
8964 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8965 }
8966
8967 have_address = 1;
8968 break;
8969 }
8970
8971 case INTEGER_CST:
8972 if (host_integerp (loc, 0))
8973 ret = int_loc_descriptor (tree_low_cst (loc, 0));
8974 else
8975 return 0;
8976 break;
8977
8978 case CONSTRUCTOR:
8979 {
8980 /* Get an RTL for this, if something has been emitted. */
8981 rtx rtl = lookup_constant_def (loc);
8982 enum machine_mode mode;
8983
8984 if (!rtl || !MEM_P (rtl))
8985 return 0;
8986 mode = GET_MODE (rtl);
8987 rtl = XEXP (rtl, 0);
8988 ret = mem_loc_descriptor (rtl, mode, true);
8989 have_address = 1;
8990 break;
8991 }
8992
8993 case TRUTH_AND_EXPR:
8994 case TRUTH_ANDIF_EXPR:
8995 case BIT_AND_EXPR:
8996 op = DW_OP_and;
8997 goto do_binop;
8998
8999 case TRUTH_XOR_EXPR:
9000 case BIT_XOR_EXPR:
9001 op = DW_OP_xor;
9002 goto do_binop;
9003
9004 case TRUTH_OR_EXPR:
9005 case TRUTH_ORIF_EXPR:
9006 case BIT_IOR_EXPR:
9007 op = DW_OP_or;
9008 goto do_binop;
9009
9010 case FLOOR_DIV_EXPR:
9011 case CEIL_DIV_EXPR:
9012 case ROUND_DIV_EXPR:
9013 case TRUNC_DIV_EXPR:
9014 op = DW_OP_div;
9015 goto do_binop;
9016
9017 case MINUS_EXPR:
9018 op = DW_OP_minus;
9019 goto do_binop;
9020
9021 case FLOOR_MOD_EXPR:
9022 case CEIL_MOD_EXPR:
9023 case ROUND_MOD_EXPR:
9024 case TRUNC_MOD_EXPR:
9025 op = DW_OP_mod;
9026 goto do_binop;
9027
9028 case MULT_EXPR:
9029 op = DW_OP_mul;
9030 goto do_binop;
9031
9032 case LSHIFT_EXPR:
9033 op = DW_OP_shl;
9034 goto do_binop;
9035
9036 case RSHIFT_EXPR:
9037 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9038 goto do_binop;
9039
9040 case PLUS_EXPR:
9041 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9042 && host_integerp (TREE_OPERAND (loc, 1), 0))
9043 {
9044 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9045 if (ret == 0)
9046 return 0;
9047
9048 add_loc_descr (&ret,
9049 new_loc_descr (DW_OP_plus_uconst,
9050 tree_low_cst (TREE_OPERAND (loc, 1),
9051 0),
9052 0));
9053 break;
9054 }
9055
9056 op = DW_OP_plus;
9057 goto do_binop;
9058
9059 case LE_EXPR:
9060 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9061 return 0;
9062
9063 op = DW_OP_le;
9064 goto do_binop;
9065
9066 case GE_EXPR:
9067 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9068 return 0;
9069
9070 op = DW_OP_ge;
9071 goto do_binop;
9072
9073 case LT_EXPR:
9074 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9075 return 0;
9076
9077 op = DW_OP_lt;
9078 goto do_binop;
9079
9080 case GT_EXPR:
9081 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9082 return 0;
9083
9084 op = DW_OP_gt;
9085 goto do_binop;
9086
9087 case EQ_EXPR:
9088 op = DW_OP_eq;
9089 goto do_binop;
9090
9091 case NE_EXPR:
9092 op = DW_OP_ne;
9093 goto do_binop;
9094
9095 do_binop:
9096 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9097 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9098 if (ret == 0 || ret1 == 0)
9099 return 0;
9100
9101 add_loc_descr (&ret, ret1);
9102 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9103 break;
9104
9105 case TRUTH_NOT_EXPR:
9106 case BIT_NOT_EXPR:
9107 op = DW_OP_not;
9108 goto do_unop;
9109
9110 case ABS_EXPR:
9111 op = DW_OP_abs;
9112 goto do_unop;
9113
9114 case NEGATE_EXPR:
9115 op = DW_OP_neg;
9116 goto do_unop;
9117
9118 do_unop:
9119 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9120 if (ret == 0)
9121 return 0;
9122
9123 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9124 break;
9125
9126 case MIN_EXPR:
9127 case MAX_EXPR:
9128 {
9129 const enum tree_code code =
9130 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9131
9132 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9133 build2 (code, integer_type_node,
9134 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9135 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9136 }
9137
9138 /* ... fall through ... */
9139
9140 case COND_EXPR:
9141 {
9142 dw_loc_descr_ref lhs
9143 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9144 dw_loc_descr_ref rhs
9145 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9146 dw_loc_descr_ref bra_node, jump_node, tmp;
9147
9148 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9149 if (ret == 0 || lhs == 0 || rhs == 0)
9150 return 0;
9151
9152 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9153 add_loc_descr (&ret, bra_node);
9154
9155 add_loc_descr (&ret, rhs);
9156 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9157 add_loc_descr (&ret, jump_node);
9158
9159 add_loc_descr (&ret, lhs);
9160 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9161 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9162
9163 /* ??? Need a node to point the skip at. Use a nop. */
9164 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9165 add_loc_descr (&ret, tmp);
9166 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9167 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9168 }
9169 break;
9170
9171 default:
9172 /* Leave front-end specific codes as simply unknown. This comes
9173 up, for instance, with the C STMT_EXPR. */
9174 if ((unsigned int) TREE_CODE (loc)
9175 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9176 return 0;
9177
9178 #ifdef ENABLE_CHECKING
9179 /* Otherwise this is a generic code; we should just lists all of
9180 these explicitly. Aborting means we forgot one. */
9181 gcc_unreachable ();
9182 #else
9183 /* In a release build, we want to degrade gracefully: better to
9184 generate incomplete debugging information than to crash. */
9185 return NULL;
9186 #endif
9187 }
9188
9189 /* Show if we can't fill the request for an address. */
9190 if (want_address && !have_address)
9191 return 0;
9192
9193 /* If we've got an address and don't want one, dereference. */
9194 if (!want_address && have_address)
9195 {
9196 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9197
9198 if (size > DWARF2_ADDR_SIZE || size == -1)
9199 return 0;
9200 else if (size == DWARF2_ADDR_SIZE)
9201 op = DW_OP_deref;
9202 else
9203 op = DW_OP_deref_size;
9204
9205 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9206 }
9207
9208 return ret;
9209 }
9210
9211 static inline dw_loc_descr_ref
9212 loc_descriptor_from_tree (tree loc)
9213 {
9214 return loc_descriptor_from_tree_1 (loc, 2);
9215 }
9216
9217 /* Given a value, round it up to the lowest multiple of `boundary'
9218 which is not less than the value itself. */
9219
9220 static inline HOST_WIDE_INT
9221 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9222 {
9223 return (((value + boundary - 1) / boundary) * boundary);
9224 }
9225
9226 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9227 pointer to the declared type for the relevant field variable, or return
9228 `integer_type_node' if the given node turns out to be an
9229 ERROR_MARK node. */
9230
9231 static inline tree
9232 field_type (tree decl)
9233 {
9234 tree type;
9235
9236 if (TREE_CODE (decl) == ERROR_MARK)
9237 return integer_type_node;
9238
9239 type = DECL_BIT_FIELD_TYPE (decl);
9240 if (type == NULL_TREE)
9241 type = TREE_TYPE (decl);
9242
9243 return type;
9244 }
9245
9246 /* Given a pointer to a tree node, return the alignment in bits for
9247 it, or else return BITS_PER_WORD if the node actually turns out to
9248 be an ERROR_MARK node. */
9249
9250 static inline unsigned
9251 simple_type_align_in_bits (tree type)
9252 {
9253 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9254 }
9255
9256 static inline unsigned
9257 simple_decl_align_in_bits (tree decl)
9258 {
9259 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9260 }
9261
9262 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9263 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9264 or return 0 if we are unable to determine what that offset is, either
9265 because the argument turns out to be a pointer to an ERROR_MARK node, or
9266 because the offset is actually variable. (We can't handle the latter case
9267 just yet). */
9268
9269 static HOST_WIDE_INT
9270 field_byte_offset (tree decl)
9271 {
9272 unsigned int type_align_in_bits;
9273 unsigned int decl_align_in_bits;
9274 unsigned HOST_WIDE_INT type_size_in_bits;
9275 HOST_WIDE_INT object_offset_in_bits;
9276 tree type;
9277 tree field_size_tree;
9278 HOST_WIDE_INT bitpos_int;
9279 HOST_WIDE_INT deepest_bitpos;
9280 unsigned HOST_WIDE_INT field_size_in_bits;
9281
9282 if (TREE_CODE (decl) == ERROR_MARK)
9283 return 0;
9284
9285 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9286
9287 type = field_type (decl);
9288 field_size_tree = DECL_SIZE (decl);
9289
9290 /* The size could be unspecified if there was an error, or for
9291 a flexible array member. */
9292 if (! field_size_tree)
9293 field_size_tree = bitsize_zero_node;
9294
9295 /* We cannot yet cope with fields whose positions are variable, so
9296 for now, when we see such things, we simply return 0. Someday, we may
9297 be able to handle such cases, but it will be damn difficult. */
9298 if (! host_integerp (bit_position (decl), 0))
9299 return 0;
9300
9301 bitpos_int = int_bit_position (decl);
9302
9303 /* If we don't know the size of the field, pretend it's a full word. */
9304 if (host_integerp (field_size_tree, 1))
9305 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9306 else
9307 field_size_in_bits = BITS_PER_WORD;
9308
9309 type_size_in_bits = simple_type_size_in_bits (type);
9310 type_align_in_bits = simple_type_align_in_bits (type);
9311 decl_align_in_bits = simple_decl_align_in_bits (decl);
9312
9313 /* The GCC front-end doesn't make any attempt to keep track of the starting
9314 bit offset (relative to the start of the containing structure type) of the
9315 hypothetical "containing object" for a bit-field. Thus, when computing
9316 the byte offset value for the start of the "containing object" of a
9317 bit-field, we must deduce this information on our own. This can be rather
9318 tricky to do in some cases. For example, handling the following structure
9319 type definition when compiling for an i386/i486 target (which only aligns
9320 long long's to 32-bit boundaries) can be very tricky:
9321
9322 struct S { int field1; long long field2:31; };
9323
9324 Fortunately, there is a simple rule-of-thumb which can be used in such
9325 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9326 structure shown above. It decides to do this based upon one simple rule
9327 for bit-field allocation. GCC allocates each "containing object" for each
9328 bit-field at the first (i.e. lowest addressed) legitimate alignment
9329 boundary (based upon the required minimum alignment for the declared type
9330 of the field) which it can possibly use, subject to the condition that
9331 there is still enough available space remaining in the containing object
9332 (when allocated at the selected point) to fully accommodate all of the
9333 bits of the bit-field itself.
9334
9335 This simple rule makes it obvious why GCC allocates 8 bytes for each
9336 object of the structure type shown above. When looking for a place to
9337 allocate the "containing object" for `field2', the compiler simply tries
9338 to allocate a 64-bit "containing object" at each successive 32-bit
9339 boundary (starting at zero) until it finds a place to allocate that 64-
9340 bit field such that at least 31 contiguous (and previously unallocated)
9341 bits remain within that selected 64 bit field. (As it turns out, for the
9342 example above, the compiler finds it is OK to allocate the "containing
9343 object" 64-bit field at bit-offset zero within the structure type.)
9344
9345 Here we attempt to work backwards from the limited set of facts we're
9346 given, and we try to deduce from those facts, where GCC must have believed
9347 that the containing object started (within the structure type). The value
9348 we deduce is then used (by the callers of this routine) to generate
9349 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9350 and, in the case of DW_AT_location, regular fields as well). */
9351
9352 /* Figure out the bit-distance from the start of the structure to the
9353 "deepest" bit of the bit-field. */
9354 deepest_bitpos = bitpos_int + field_size_in_bits;
9355
9356 /* This is the tricky part. Use some fancy footwork to deduce where the
9357 lowest addressed bit of the containing object must be. */
9358 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9359
9360 /* Round up to type_align by default. This works best for bitfields. */
9361 object_offset_in_bits += type_align_in_bits - 1;
9362 object_offset_in_bits /= type_align_in_bits;
9363 object_offset_in_bits *= type_align_in_bits;
9364
9365 if (object_offset_in_bits > bitpos_int)
9366 {
9367 /* Sigh, the decl must be packed. */
9368 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9369
9370 /* Round up to decl_align instead. */
9371 object_offset_in_bits += decl_align_in_bits - 1;
9372 object_offset_in_bits /= decl_align_in_bits;
9373 object_offset_in_bits *= decl_align_in_bits;
9374 }
9375
9376 return object_offset_in_bits / BITS_PER_UNIT;
9377 }
9378 \f
9379 /* The following routines define various Dwarf attributes and any data
9380 associated with them. */
9381
9382 /* Add a location description attribute value to a DIE.
9383
9384 This emits location attributes suitable for whole variables and
9385 whole parameters. Note that the location attributes for struct fields are
9386 generated by the routine `data_member_location_attribute' below. */
9387
9388 static inline void
9389 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9390 dw_loc_descr_ref descr)
9391 {
9392 if (descr != 0)
9393 add_AT_loc (die, attr_kind, descr);
9394 }
9395
9396 /* Attach the specialized form of location attribute used for data members of
9397 struct and union types. In the special case of a FIELD_DECL node which
9398 represents a bit-field, the "offset" part of this special location
9399 descriptor must indicate the distance in bytes from the lowest-addressed
9400 byte of the containing struct or union type to the lowest-addressed byte of
9401 the "containing object" for the bit-field. (See the `field_byte_offset'
9402 function above).
9403
9404 For any given bit-field, the "containing object" is a hypothetical object
9405 (of some integral or enum type) within which the given bit-field lives. The
9406 type of this hypothetical "containing object" is always the same as the
9407 declared type of the individual bit-field itself (for GCC anyway... the
9408 DWARF spec doesn't actually mandate this). Note that it is the size (in
9409 bytes) of the hypothetical "containing object" which will be given in the
9410 DW_AT_byte_size attribute for this bit-field. (See the
9411 `byte_size_attribute' function below.) It is also used when calculating the
9412 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9413 function below.) */
9414
9415 static void
9416 add_data_member_location_attribute (dw_die_ref die, tree decl)
9417 {
9418 HOST_WIDE_INT offset;
9419 dw_loc_descr_ref loc_descr = 0;
9420
9421 if (TREE_CODE (decl) == TREE_BINFO)
9422 {
9423 /* We're working on the TAG_inheritance for a base class. */
9424 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9425 {
9426 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9427 aren't at a fixed offset from all (sub)objects of the same
9428 type. We need to extract the appropriate offset from our
9429 vtable. The following dwarf expression means
9430
9431 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9432
9433 This is specific to the V3 ABI, of course. */
9434
9435 dw_loc_descr_ref tmp;
9436
9437 /* Make a copy of the object address. */
9438 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9439 add_loc_descr (&loc_descr, tmp);
9440
9441 /* Extract the vtable address. */
9442 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9443 add_loc_descr (&loc_descr, tmp);
9444
9445 /* Calculate the address of the offset. */
9446 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9447 gcc_assert (offset < 0);
9448
9449 tmp = int_loc_descriptor (-offset);
9450 add_loc_descr (&loc_descr, tmp);
9451 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9452 add_loc_descr (&loc_descr, tmp);
9453
9454 /* Extract the offset. */
9455 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9456 add_loc_descr (&loc_descr, tmp);
9457
9458 /* Add it to the object address. */
9459 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9460 add_loc_descr (&loc_descr, tmp);
9461 }
9462 else
9463 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9464 }
9465 else
9466 offset = field_byte_offset (decl);
9467
9468 if (! loc_descr)
9469 {
9470 enum dwarf_location_atom op;
9471
9472 /* The DWARF2 standard says that we should assume that the structure
9473 address is already on the stack, so we can specify a structure field
9474 address by using DW_OP_plus_uconst. */
9475
9476 #ifdef MIPS_DEBUGGING_INFO
9477 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9478 operator correctly. It works only if we leave the offset on the
9479 stack. */
9480 op = DW_OP_constu;
9481 #else
9482 op = DW_OP_plus_uconst;
9483 #endif
9484
9485 loc_descr = new_loc_descr (op, offset, 0);
9486 }
9487
9488 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9489 }
9490
9491 /* Writes integer values to dw_vec_const array. */
9492
9493 static void
9494 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9495 {
9496 while (size != 0)
9497 {
9498 *dest++ = val & 0xff;
9499 val >>= 8;
9500 --size;
9501 }
9502 }
9503
9504 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9505
9506 static HOST_WIDE_INT
9507 extract_int (const unsigned char *src, unsigned int size)
9508 {
9509 HOST_WIDE_INT val = 0;
9510
9511 src += size;
9512 while (size != 0)
9513 {
9514 val <<= 8;
9515 val |= *--src & 0xff;
9516 --size;
9517 }
9518 return val;
9519 }
9520
9521 /* Writes floating point values to dw_vec_const array. */
9522
9523 static void
9524 insert_float (rtx rtl, unsigned char *array)
9525 {
9526 REAL_VALUE_TYPE rv;
9527 long val[4];
9528 int i;
9529
9530 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9531 real_to_target (val, &rv, GET_MODE (rtl));
9532
9533 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9534 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9535 {
9536 insert_int (val[i], 4, array);
9537 array += 4;
9538 }
9539 }
9540
9541 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9542 does not have a "location" either in memory or in a register. These
9543 things can arise in GNU C when a constant is passed as an actual parameter
9544 to an inlined function. They can also arise in C++ where declared
9545 constants do not necessarily get memory "homes". */
9546
9547 static void
9548 add_const_value_attribute (dw_die_ref die, rtx rtl)
9549 {
9550 switch (GET_CODE (rtl))
9551 {
9552 case CONST_INT:
9553 {
9554 HOST_WIDE_INT val = INTVAL (rtl);
9555
9556 if (val < 0)
9557 add_AT_int (die, DW_AT_const_value, val);
9558 else
9559 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9560 }
9561 break;
9562
9563 case CONST_DOUBLE:
9564 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9565 floating-point constant. A CONST_DOUBLE is used whenever the
9566 constant requires more than one word in order to be adequately
9567 represented. We output CONST_DOUBLEs as blocks. */
9568 {
9569 enum machine_mode mode = GET_MODE (rtl);
9570
9571 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9572 {
9573 unsigned int length = GET_MODE_SIZE (mode);
9574 unsigned char *array = ggc_alloc (length);
9575
9576 insert_float (rtl, array);
9577 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9578 }
9579 else
9580 {
9581 /* ??? We really should be using HOST_WIDE_INT throughout. */
9582 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9583
9584 add_AT_long_long (die, DW_AT_const_value,
9585 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9586 }
9587 }
9588 break;
9589
9590 case CONST_VECTOR:
9591 {
9592 enum machine_mode mode = GET_MODE (rtl);
9593 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9594 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9595 unsigned char *array = ggc_alloc (length * elt_size);
9596 unsigned int i;
9597 unsigned char *p;
9598
9599 switch (GET_MODE_CLASS (mode))
9600 {
9601 case MODE_VECTOR_INT:
9602 for (i = 0, p = array; i < length; i++, p += elt_size)
9603 {
9604 rtx elt = CONST_VECTOR_ELT (rtl, i);
9605 HOST_WIDE_INT lo, hi;
9606
9607 switch (GET_CODE (elt))
9608 {
9609 case CONST_INT:
9610 lo = INTVAL (elt);
9611 hi = -(lo < 0);
9612 break;
9613
9614 case CONST_DOUBLE:
9615 lo = CONST_DOUBLE_LOW (elt);
9616 hi = CONST_DOUBLE_HIGH (elt);
9617 break;
9618
9619 default:
9620 gcc_unreachable ();
9621 }
9622
9623 if (elt_size <= sizeof (HOST_WIDE_INT))
9624 insert_int (lo, elt_size, p);
9625 else
9626 {
9627 unsigned char *p0 = p;
9628 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9629
9630 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9631 if (WORDS_BIG_ENDIAN)
9632 {
9633 p0 = p1;
9634 p1 = p;
9635 }
9636 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9637 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9638 }
9639 }
9640 break;
9641
9642 case MODE_VECTOR_FLOAT:
9643 for (i = 0, p = array; i < length; i++, p += elt_size)
9644 {
9645 rtx elt = CONST_VECTOR_ELT (rtl, i);
9646 insert_float (elt, p);
9647 }
9648 break;
9649
9650 default:
9651 gcc_unreachable ();
9652 }
9653
9654 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9655 }
9656 break;
9657
9658 case CONST_STRING:
9659 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9660 break;
9661
9662 case SYMBOL_REF:
9663 case LABEL_REF:
9664 case CONST:
9665 add_AT_addr (die, DW_AT_const_value, rtl);
9666 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
9667 break;
9668
9669 case PLUS:
9670 /* In cases where an inlined instance of an inline function is passed
9671 the address of an `auto' variable (which is local to the caller) we
9672 can get a situation where the DECL_RTL of the artificial local
9673 variable (for the inlining) which acts as a stand-in for the
9674 corresponding formal parameter (of the inline function) will look
9675 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9676 exactly a compile-time constant expression, but it isn't the address
9677 of the (artificial) local variable either. Rather, it represents the
9678 *value* which the artificial local variable always has during its
9679 lifetime. We currently have no way to represent such quasi-constant
9680 values in Dwarf, so for now we just punt and generate nothing. */
9681 break;
9682
9683 default:
9684 /* No other kinds of rtx should be possible here. */
9685 gcc_unreachable ();
9686 }
9687
9688 }
9689
9690 static rtx
9691 rtl_for_decl_location (tree decl)
9692 {
9693 rtx rtl;
9694
9695 /* Here we have to decide where we are going to say the parameter "lives"
9696 (as far as the debugger is concerned). We only have a couple of
9697 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9698
9699 DECL_RTL normally indicates where the parameter lives during most of the
9700 activation of the function. If optimization is enabled however, this
9701 could be either NULL or else a pseudo-reg. Both of those cases indicate
9702 that the parameter doesn't really live anywhere (as far as the code
9703 generation parts of GCC are concerned) during most of the function's
9704 activation. That will happen (for example) if the parameter is never
9705 referenced within the function.
9706
9707 We could just generate a location descriptor here for all non-NULL
9708 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9709 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9710 where DECL_RTL is NULL or is a pseudo-reg.
9711
9712 Note however that we can only get away with using DECL_INCOMING_RTL as
9713 a backup substitute for DECL_RTL in certain limited cases. In cases
9714 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9715 we can be sure that the parameter was passed using the same type as it is
9716 declared to have within the function, and that its DECL_INCOMING_RTL
9717 points us to a place where a value of that type is passed.
9718
9719 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9720 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9721 because in these cases DECL_INCOMING_RTL points us to a value of some
9722 type which is *different* from the type of the parameter itself. Thus,
9723 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9724 such cases, the debugger would end up (for example) trying to fetch a
9725 `float' from a place which actually contains the first part of a
9726 `double'. That would lead to really incorrect and confusing
9727 output at debug-time.
9728
9729 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9730 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9731 are a couple of exceptions however. On little-endian machines we can
9732 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9733 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9734 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9735 when (on a little-endian machine) a non-prototyped function has a
9736 parameter declared to be of type `short' or `char'. In such cases,
9737 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9738 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9739 passed `int' value. If the debugger then uses that address to fetch
9740 a `short' or a `char' (on a little-endian machine) the result will be
9741 the correct data, so we allow for such exceptional cases below.
9742
9743 Note that our goal here is to describe the place where the given formal
9744 parameter lives during most of the function's activation (i.e. between the
9745 end of the prologue and the start of the epilogue). We'll do that as best
9746 as we can. Note however that if the given formal parameter is modified
9747 sometime during the execution of the function, then a stack backtrace (at
9748 debug-time) will show the function as having been called with the *new*
9749 value rather than the value which was originally passed in. This happens
9750 rarely enough that it is not a major problem, but it *is* a problem, and
9751 I'd like to fix it.
9752
9753 A future version of dwarf2out.c may generate two additional attributes for
9754 any given DW_TAG_formal_parameter DIE which will describe the "passed
9755 type" and the "passed location" for the given formal parameter in addition
9756 to the attributes we now generate to indicate the "declared type" and the
9757 "active location" for each parameter. This additional set of attributes
9758 could be used by debuggers for stack backtraces. Separately, note that
9759 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9760 This happens (for example) for inlined-instances of inline function formal
9761 parameters which are never referenced. This really shouldn't be
9762 happening. All PARM_DECL nodes should get valid non-NULL
9763 DECL_INCOMING_RTL values. FIXME. */
9764
9765 /* Use DECL_RTL as the "location" unless we find something better. */
9766 rtl = DECL_RTL_IF_SET (decl);
9767
9768 /* When generating abstract instances, ignore everything except
9769 constants, symbols living in memory, and symbols living in
9770 fixed registers. */
9771 if (! reload_completed)
9772 {
9773 if (rtl
9774 && (CONSTANT_P (rtl)
9775 || (MEM_P (rtl)
9776 && CONSTANT_P (XEXP (rtl, 0)))
9777 || (REG_P (rtl)
9778 && TREE_CODE (decl) == VAR_DECL
9779 && TREE_STATIC (decl))))
9780 {
9781 rtl = targetm.delegitimize_address (rtl);
9782 return rtl;
9783 }
9784 rtl = NULL_RTX;
9785 }
9786 else if (TREE_CODE (decl) == PARM_DECL)
9787 {
9788 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9789 {
9790 tree declared_type = type_main_variant (TREE_TYPE (decl));
9791 tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
9792
9793 /* This decl represents a formal parameter which was optimized out.
9794 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9795 all cases where (rtl == NULL_RTX) just below. */
9796 if (declared_type == passed_type)
9797 rtl = DECL_INCOMING_RTL (decl);
9798 else if (! BYTES_BIG_ENDIAN
9799 && TREE_CODE (declared_type) == INTEGER_TYPE
9800 && (GET_MODE_SIZE (TYPE_MODE (declared_type))
9801 <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
9802 rtl = DECL_INCOMING_RTL (decl);
9803 }
9804
9805 /* If the parm was passed in registers, but lives on the stack, then
9806 make a big endian correction if the mode of the type of the
9807 parameter is not the same as the mode of the rtl. */
9808 /* ??? This is the same series of checks that are made in dbxout.c before
9809 we reach the big endian correction code there. It isn't clear if all
9810 of these checks are necessary here, but keeping them all is the safe
9811 thing to do. */
9812 else if (MEM_P (rtl)
9813 && XEXP (rtl, 0) != const0_rtx
9814 && ! CONSTANT_P (XEXP (rtl, 0))
9815 /* Not passed in memory. */
9816 && !MEM_P (DECL_INCOMING_RTL (decl))
9817 /* Not passed by invisible reference. */
9818 && (!REG_P (XEXP (rtl, 0))
9819 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9820 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9821 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9822 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9823 #endif
9824 )
9825 /* Big endian correction check. */
9826 && BYTES_BIG_ENDIAN
9827 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9828 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9829 < UNITS_PER_WORD))
9830 {
9831 int offset = (UNITS_PER_WORD
9832 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
9833
9834 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9835 plus_constant (XEXP (rtl, 0), offset));
9836 }
9837 }
9838 else if (TREE_CODE (decl) == VAR_DECL
9839 && rtl
9840 && MEM_P (rtl)
9841 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
9842 && BYTES_BIG_ENDIAN)
9843 {
9844 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
9845 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
9846
9847 /* If a variable is declared "register" yet is smaller than
9848 a register, then if we store the variable to memory, it
9849 looks like we're storing a register-sized value, when in
9850 fact we are not. We need to adjust the offset of the
9851 storage location to reflect the actual value's bytes,
9852 else gdb will not be able to display it. */
9853 if (rsize > dsize)
9854 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9855 plus_constant (XEXP (rtl, 0), rsize-dsize));
9856 }
9857
9858 if (rtl != NULL_RTX)
9859 {
9860 rtl = eliminate_regs (rtl, 0, NULL_RTX);
9861 #ifdef LEAF_REG_REMAP
9862 if (current_function_uses_only_leaf_regs)
9863 leaf_renumber_regs_insn (rtl);
9864 #endif
9865 }
9866
9867 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9868 and will have been substituted directly into all expressions that use it.
9869 C does not have such a concept, but C++ and other languages do. */
9870 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9871 {
9872 /* If a variable is initialized with a string constant without embedded
9873 zeros, build CONST_STRING. */
9874 if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9875 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9876 {
9877 tree arrtype = TREE_TYPE (decl);
9878 tree enttype = TREE_TYPE (arrtype);
9879 tree domain = TYPE_DOMAIN (arrtype);
9880 tree init = DECL_INITIAL (decl);
9881 enum machine_mode mode = TYPE_MODE (enttype);
9882
9883 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9884 && domain
9885 && integer_zerop (TYPE_MIN_VALUE (domain))
9886 && compare_tree_int (TYPE_MAX_VALUE (domain),
9887 TREE_STRING_LENGTH (init) - 1) == 0
9888 && ((size_t) TREE_STRING_LENGTH (init)
9889 == strlen (TREE_STRING_POINTER (init)) + 1))
9890 rtl = gen_rtx_CONST_STRING (VOIDmode, TREE_STRING_POINTER (init));
9891 }
9892 /* If the initializer is something that we know will expand into an
9893 immediate RTL constant, expand it now. Expanding anything else
9894 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9895 else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9896 || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
9897 {
9898 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9899 EXPAND_INITIALIZER);
9900 /* If expand_expr returns a MEM, it wasn't immediate. */
9901 gcc_assert (!rtl || !MEM_P (rtl));
9902 }
9903 }
9904
9905 if (rtl)
9906 rtl = targetm.delegitimize_address (rtl);
9907
9908 /* If we don't look past the constant pool, we risk emitting a
9909 reference to a constant pool entry that isn't referenced from
9910 code, and thus is not emitted. */
9911 if (rtl)
9912 rtl = avoid_constant_pool_reference (rtl);
9913
9914 return rtl;
9915 }
9916
9917 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9918 data attribute for a variable or a parameter. We generate the
9919 DW_AT_const_value attribute only in those cases where the given variable
9920 or parameter does not have a true "location" either in memory or in a
9921 register. This can happen (for example) when a constant is passed as an
9922 actual argument in a call to an inline function. (It's possible that
9923 these things can crop up in other ways also.) Note that one type of
9924 constant value which can be passed into an inlined function is a constant
9925 pointer. This can happen for example if an actual argument in an inlined
9926 function call evaluates to a compile-time constant address. */
9927
9928 static void
9929 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
9930 enum dwarf_attribute attr)
9931 {
9932 rtx rtl;
9933 dw_loc_descr_ref descr;
9934 var_loc_list *loc_list;
9935
9936 if (TREE_CODE (decl) == ERROR_MARK)
9937 return;
9938
9939 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
9940 || TREE_CODE (decl) == RESULT_DECL);
9941
9942 /* See if we possibly have multiple locations for this variable. */
9943 loc_list = lookup_decl_loc (decl);
9944
9945 /* If it truly has multiple locations, the first and last node will
9946 differ. */
9947 if (loc_list && loc_list->first != loc_list->last)
9948 {
9949 const char *secname;
9950 const char *endname;
9951 dw_loc_list_ref list;
9952 rtx varloc;
9953 struct var_loc_node *node;
9954
9955 /* We need to figure out what section we should use as the base
9956 for the address ranges where a given location is valid.
9957 1. If this particular DECL has a section associated with it,
9958 use that.
9959 2. If this function has a section associated with it, use
9960 that.
9961 3. Otherwise, use the text section.
9962 XXX: If you split a variable across multiple sections, this
9963 won't notice. */
9964
9965 if (DECL_SECTION_NAME (decl))
9966 {
9967 tree sectree = DECL_SECTION_NAME (decl);
9968 secname = TREE_STRING_POINTER (sectree);
9969 }
9970 else if (current_function_decl
9971 && DECL_SECTION_NAME (current_function_decl))
9972 {
9973 tree sectree = DECL_SECTION_NAME (current_function_decl);
9974 secname = TREE_STRING_POINTER (sectree);
9975 }
9976 else
9977 secname = text_section_label;
9978
9979 /* Now that we know what section we are using for a base,
9980 actually construct the list of locations.
9981 The first location information is what is passed to the
9982 function that creates the location list, and the remaining
9983 locations just get added on to that list.
9984 Note that we only know the start address for a location
9985 (IE location changes), so to build the range, we use
9986 the range [current location start, next location start].
9987 This means we have to special case the last node, and generate
9988 a range of [last location start, end of function label]. */
9989
9990 node = loc_list->first;
9991 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9992 list = new_loc_list (loc_descriptor (varloc, attr != DW_AT_frame_base),
9993 node->label, node->next->label, secname, 1);
9994 node = node->next;
9995
9996 for (; node->next; node = node->next)
9997 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
9998 {
9999 /* The variable has a location between NODE->LABEL and
10000 NODE->NEXT->LABEL. */
10001 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10002 add_loc_descr_to_loc_list (&list,
10003 loc_descriptor (varloc,
10004 attr != DW_AT_frame_base),
10005 node->label, node->next->label, secname);
10006 }
10007
10008 /* If the variable has a location at the last label
10009 it keeps its location until the end of function. */
10010 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10011 {
10012 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10013
10014 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10015 if (!current_function_decl)
10016 endname = text_end_label;
10017 else
10018 {
10019 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10020 current_function_funcdef_no);
10021 endname = ggc_strdup (label_id);
10022 }
10023 add_loc_descr_to_loc_list (&list,
10024 loc_descriptor (varloc,
10025 attr != DW_AT_frame_base),
10026 node->label, endname, secname);
10027 }
10028
10029 /* Finally, add the location list to the DIE, and we are done. */
10030 add_AT_loc_list (die, attr, list);
10031 return;
10032 }
10033
10034 rtl = rtl_for_decl_location (decl);
10035 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10036 {
10037 add_const_value_attribute (die, rtl);
10038 return;
10039 }
10040
10041 descr = loc_descriptor_from_tree (decl);
10042 if (descr)
10043 add_AT_location_description (die, attr, descr);
10044 }
10045
10046 /* If we don't have a copy of this variable in memory for some reason (such
10047 as a C++ member constant that doesn't have an out-of-line definition),
10048 we should tell the debugger about the constant value. */
10049
10050 static void
10051 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10052 {
10053 tree init = DECL_INITIAL (decl);
10054 tree type = TREE_TYPE (decl);
10055
10056 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
10057 && initializer_constant_valid_p (init, type) == null_pointer_node)
10058 /* OK */;
10059 else
10060 return;
10061
10062 switch (TREE_CODE (type))
10063 {
10064 case INTEGER_TYPE:
10065 if (host_integerp (init, 0))
10066 add_AT_unsigned (var_die, DW_AT_const_value,
10067 tree_low_cst (init, 0));
10068 else
10069 add_AT_long_long (var_die, DW_AT_const_value,
10070 TREE_INT_CST_HIGH (init),
10071 TREE_INT_CST_LOW (init));
10072 break;
10073
10074 default:;
10075 }
10076 }
10077
10078 /* Generate a DW_AT_name attribute given some string value to be included as
10079 the value of the attribute. */
10080
10081 static void
10082 add_name_attribute (dw_die_ref die, const char *name_string)
10083 {
10084 if (name_string != NULL && *name_string != 0)
10085 {
10086 if (demangle_name_func)
10087 name_string = (*demangle_name_func) (name_string);
10088
10089 add_AT_string (die, DW_AT_name, name_string);
10090 }
10091 }
10092
10093 /* Generate a DW_AT_comp_dir attribute for DIE. */
10094
10095 static void
10096 add_comp_dir_attribute (dw_die_ref die)
10097 {
10098 const char *wd = get_src_pwd ();
10099 if (wd != NULL)
10100 add_AT_string (die, DW_AT_comp_dir, wd);
10101 }
10102
10103 /* Given a tree node describing an array bound (either lower or upper) output
10104 a representation for that bound. */
10105
10106 static void
10107 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10108 {
10109 switch (TREE_CODE (bound))
10110 {
10111 case ERROR_MARK:
10112 return;
10113
10114 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10115 case INTEGER_CST:
10116 if (! host_integerp (bound, 0)
10117 || (bound_attr == DW_AT_lower_bound
10118 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10119 || (is_fortran () && integer_onep (bound)))))
10120 /* Use the default. */
10121 ;
10122 else
10123 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10124 break;
10125
10126 case CONVERT_EXPR:
10127 case NOP_EXPR:
10128 case NON_LVALUE_EXPR:
10129 case VIEW_CONVERT_EXPR:
10130 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10131 break;
10132
10133 case SAVE_EXPR:
10134 break;
10135
10136 case VAR_DECL:
10137 case PARM_DECL:
10138 case RESULT_DECL:
10139 {
10140 dw_die_ref decl_die = lookup_decl_die (bound);
10141
10142 /* ??? Can this happen, or should the variable have been bound
10143 first? Probably it can, since I imagine that we try to create
10144 the types of parameters in the order in which they exist in
10145 the list, and won't have created a forward reference to a
10146 later parameter. */
10147 if (decl_die != NULL)
10148 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10149 break;
10150 }
10151
10152 default:
10153 {
10154 /* Otherwise try to create a stack operation procedure to
10155 evaluate the value of the array bound. */
10156
10157 dw_die_ref ctx, decl_die;
10158 dw_loc_descr_ref loc;
10159
10160 loc = loc_descriptor_from_tree (bound);
10161 if (loc == NULL)
10162 break;
10163
10164 if (current_function_decl == 0)
10165 ctx = comp_unit_die;
10166 else
10167 ctx = lookup_decl_die (current_function_decl);
10168
10169 decl_die = new_die (DW_TAG_variable, ctx, bound);
10170 add_AT_flag (decl_die, DW_AT_artificial, 1);
10171 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10172 add_AT_loc (decl_die, DW_AT_location, loc);
10173
10174 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10175 break;
10176 }
10177 }
10178 }
10179
10180 /* Note that the block of subscript information for an array type also
10181 includes information about the element type of type given array type. */
10182
10183 static void
10184 add_subscript_info (dw_die_ref type_die, tree type)
10185 {
10186 #ifndef MIPS_DEBUGGING_INFO
10187 unsigned dimension_number;
10188 #endif
10189 tree lower, upper;
10190 dw_die_ref subrange_die;
10191
10192 /* The GNU compilers represent multidimensional array types as sequences of
10193 one dimensional array types whose element types are themselves array
10194 types. Here we squish that down, so that each multidimensional array
10195 type gets only one array_type DIE in the Dwarf debugging info. The draft
10196 Dwarf specification say that we are allowed to do this kind of
10197 compression in C (because there is no difference between an array or
10198 arrays and a multidimensional array in C) but for other source languages
10199 (e.g. Ada) we probably shouldn't do this. */
10200
10201 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10202 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10203 We work around this by disabling this feature. See also
10204 gen_array_type_die. */
10205 #ifndef MIPS_DEBUGGING_INFO
10206 for (dimension_number = 0;
10207 TREE_CODE (type) == ARRAY_TYPE;
10208 type = TREE_TYPE (type), dimension_number++)
10209 #endif
10210 {
10211 tree domain = TYPE_DOMAIN (type);
10212
10213 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10214 and (in GNU C only) variable bounds. Handle all three forms
10215 here. */
10216 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10217 if (domain)
10218 {
10219 /* We have an array type with specified bounds. */
10220 lower = TYPE_MIN_VALUE (domain);
10221 upper = TYPE_MAX_VALUE (domain);
10222
10223 /* Define the index type. */
10224 if (TREE_TYPE (domain))
10225 {
10226 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10227 TREE_TYPE field. We can't emit debug info for this
10228 because it is an unnamed integral type. */
10229 if (TREE_CODE (domain) == INTEGER_TYPE
10230 && TYPE_NAME (domain) == NULL_TREE
10231 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10232 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10233 ;
10234 else
10235 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10236 type_die);
10237 }
10238
10239 /* ??? If upper is NULL, the array has unspecified length,
10240 but it does have a lower bound. This happens with Fortran
10241 dimension arr(N:*)
10242 Since the debugger is definitely going to need to know N
10243 to produce useful results, go ahead and output the lower
10244 bound solo, and hope the debugger can cope. */
10245
10246 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10247 if (upper)
10248 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10249 }
10250
10251 /* Otherwise we have an array type with an unspecified length. The
10252 DWARF-2 spec does not say how to handle this; let's just leave out the
10253 bounds. */
10254 }
10255 }
10256
10257 static void
10258 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10259 {
10260 unsigned size;
10261
10262 switch (TREE_CODE (tree_node))
10263 {
10264 case ERROR_MARK:
10265 size = 0;
10266 break;
10267 case ENUMERAL_TYPE:
10268 case RECORD_TYPE:
10269 case UNION_TYPE:
10270 case QUAL_UNION_TYPE:
10271 size = int_size_in_bytes (tree_node);
10272 break;
10273 case FIELD_DECL:
10274 /* For a data member of a struct or union, the DW_AT_byte_size is
10275 generally given as the number of bytes normally allocated for an
10276 object of the *declared* type of the member itself. This is true
10277 even for bit-fields. */
10278 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10279 break;
10280 default:
10281 gcc_unreachable ();
10282 }
10283
10284 /* Note that `size' might be -1 when we get to this point. If it is, that
10285 indicates that the byte size of the entity in question is variable. We
10286 have no good way of expressing this fact in Dwarf at the present time,
10287 so just let the -1 pass on through. */
10288 add_AT_unsigned (die, DW_AT_byte_size, size);
10289 }
10290
10291 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10292 which specifies the distance in bits from the highest order bit of the
10293 "containing object" for the bit-field to the highest order bit of the
10294 bit-field itself.
10295
10296 For any given bit-field, the "containing object" is a hypothetical object
10297 (of some integral or enum type) within which the given bit-field lives. The
10298 type of this hypothetical "containing object" is always the same as the
10299 declared type of the individual bit-field itself. The determination of the
10300 exact location of the "containing object" for a bit-field is rather
10301 complicated. It's handled by the `field_byte_offset' function (above).
10302
10303 Note that it is the size (in bytes) of the hypothetical "containing object"
10304 which will be given in the DW_AT_byte_size attribute for this bit-field.
10305 (See `byte_size_attribute' above). */
10306
10307 static inline void
10308 add_bit_offset_attribute (dw_die_ref die, tree decl)
10309 {
10310 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10311 tree type = DECL_BIT_FIELD_TYPE (decl);
10312 HOST_WIDE_INT bitpos_int;
10313 HOST_WIDE_INT highest_order_object_bit_offset;
10314 HOST_WIDE_INT highest_order_field_bit_offset;
10315 HOST_WIDE_INT unsigned bit_offset;
10316
10317 /* Must be a field and a bit field. */
10318 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10319
10320 /* We can't yet handle bit-fields whose offsets are variable, so if we
10321 encounter such things, just return without generating any attribute
10322 whatsoever. Likewise for variable or too large size. */
10323 if (! host_integerp (bit_position (decl), 0)
10324 || ! host_integerp (DECL_SIZE (decl), 1))
10325 return;
10326
10327 bitpos_int = int_bit_position (decl);
10328
10329 /* Note that the bit offset is always the distance (in bits) from the
10330 highest-order bit of the "containing object" to the highest-order bit of
10331 the bit-field itself. Since the "high-order end" of any object or field
10332 is different on big-endian and little-endian machines, the computation
10333 below must take account of these differences. */
10334 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10335 highest_order_field_bit_offset = bitpos_int;
10336
10337 if (! BYTES_BIG_ENDIAN)
10338 {
10339 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10340 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10341 }
10342
10343 bit_offset
10344 = (! BYTES_BIG_ENDIAN
10345 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10346 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10347
10348 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10349 }
10350
10351 /* For a FIELD_DECL node which represents a bit field, output an attribute
10352 which specifies the length in bits of the given field. */
10353
10354 static inline void
10355 add_bit_size_attribute (dw_die_ref die, tree decl)
10356 {
10357 /* Must be a field and a bit field. */
10358 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10359 && DECL_BIT_FIELD_TYPE (decl));
10360
10361 if (host_integerp (DECL_SIZE (decl), 1))
10362 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10363 }
10364
10365 /* If the compiled language is ANSI C, then add a 'prototyped'
10366 attribute, if arg types are given for the parameters of a function. */
10367
10368 static inline void
10369 add_prototyped_attribute (dw_die_ref die, tree func_type)
10370 {
10371 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10372 && TYPE_ARG_TYPES (func_type) != NULL)
10373 add_AT_flag (die, DW_AT_prototyped, 1);
10374 }
10375
10376 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10377 by looking in either the type declaration or object declaration
10378 equate table. */
10379
10380 static inline void
10381 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10382 {
10383 dw_die_ref origin_die = NULL;
10384
10385 if (TREE_CODE (origin) != FUNCTION_DECL)
10386 {
10387 /* We may have gotten separated from the block for the inlined
10388 function, if we're in an exception handler or some such; make
10389 sure that the abstract function has been written out.
10390
10391 Doing this for nested functions is wrong, however; functions are
10392 distinct units, and our context might not even be inline. */
10393 tree fn = origin;
10394
10395 if (TYPE_P (fn))
10396 fn = TYPE_STUB_DECL (fn);
10397
10398 fn = decl_function_context (fn);
10399 if (fn)
10400 dwarf2out_abstract_function (fn);
10401 }
10402
10403 if (DECL_P (origin))
10404 origin_die = lookup_decl_die (origin);
10405 else if (TYPE_P (origin))
10406 origin_die = lookup_type_die (origin);
10407
10408 gcc_assert (origin_die);
10409
10410 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10411 }
10412
10413 /* We do not currently support the pure_virtual attribute. */
10414
10415 static inline void
10416 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10417 {
10418 if (DECL_VINDEX (func_decl))
10419 {
10420 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10421
10422 if (host_integerp (DECL_VINDEX (func_decl), 0))
10423 add_AT_loc (die, DW_AT_vtable_elem_location,
10424 new_loc_descr (DW_OP_constu,
10425 tree_low_cst (DECL_VINDEX (func_decl), 0),
10426 0));
10427
10428 /* GNU extension: Record what type this method came from originally. */
10429 if (debug_info_level > DINFO_LEVEL_TERSE)
10430 add_AT_die_ref (die, DW_AT_containing_type,
10431 lookup_type_die (DECL_CONTEXT (func_decl)));
10432 }
10433 }
10434 \f
10435 /* Add source coordinate attributes for the given decl. */
10436
10437 static void
10438 add_src_coords_attributes (dw_die_ref die, tree decl)
10439 {
10440 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10441 unsigned file_index = lookup_filename (s.file);
10442
10443 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10444 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10445 }
10446
10447 /* Add a DW_AT_name attribute and source coordinate attribute for the
10448 given decl, but only if it actually has a name. */
10449
10450 static void
10451 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10452 {
10453 tree decl_name;
10454
10455 decl_name = DECL_NAME (decl);
10456 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10457 {
10458 add_name_attribute (die, dwarf2_name (decl, 0));
10459 if (! DECL_ARTIFICIAL (decl))
10460 add_src_coords_attributes (die, decl);
10461
10462 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10463 && TREE_PUBLIC (decl)
10464 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10465 && !DECL_ABSTRACT (decl))
10466 add_AT_string (die, DW_AT_MIPS_linkage_name,
10467 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10468 }
10469
10470 #ifdef VMS_DEBUGGING_INFO
10471 /* Get the function's name, as described by its RTL. This may be different
10472 from the DECL_NAME name used in the source file. */
10473 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10474 {
10475 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10476 XEXP (DECL_RTL (decl), 0));
10477 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
10478 }
10479 #endif
10480 }
10481
10482 /* Push a new declaration scope. */
10483
10484 static void
10485 push_decl_scope (tree scope)
10486 {
10487 VARRAY_PUSH_TREE (decl_scope_table, scope);
10488 }
10489
10490 /* Pop a declaration scope. */
10491
10492 static inline void
10493 pop_decl_scope (void)
10494 {
10495 gcc_assert (VARRAY_ACTIVE_SIZE (decl_scope_table) > 0);
10496
10497 VARRAY_POP (decl_scope_table);
10498 }
10499
10500 /* Return the DIE for the scope that immediately contains this type.
10501 Non-named types get global scope. Named types nested in other
10502 types get their containing scope if it's open, or global scope
10503 otherwise. All other types (i.e. function-local named types) get
10504 the current active scope. */
10505
10506 static dw_die_ref
10507 scope_die_for (tree t, dw_die_ref context_die)
10508 {
10509 dw_die_ref scope_die = NULL;
10510 tree containing_scope;
10511 int i;
10512
10513 /* Non-types always go in the current scope. */
10514 gcc_assert (TYPE_P (t));
10515
10516 containing_scope = TYPE_CONTEXT (t);
10517
10518 /* Use the containing namespace if it was passed in (for a declaration). */
10519 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10520 {
10521 if (context_die == lookup_decl_die (containing_scope))
10522 /* OK */;
10523 else
10524 containing_scope = NULL_TREE;
10525 }
10526
10527 /* Ignore function type "scopes" from the C frontend. They mean that
10528 a tagged type is local to a parmlist of a function declarator, but
10529 that isn't useful to DWARF. */
10530 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10531 containing_scope = NULL_TREE;
10532
10533 if (containing_scope == NULL_TREE)
10534 scope_die = comp_unit_die;
10535 else if (TYPE_P (containing_scope))
10536 {
10537 /* For types, we can just look up the appropriate DIE. But
10538 first we check to see if we're in the middle of emitting it
10539 so we know where the new DIE should go. */
10540 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10541 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
10542 break;
10543
10544 if (i < 0)
10545 {
10546 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10547 || TREE_ASM_WRITTEN (containing_scope));
10548
10549 /* If none of the current dies are suitable, we get file scope. */
10550 scope_die = comp_unit_die;
10551 }
10552 else
10553 scope_die = lookup_type_die (containing_scope);
10554 }
10555 else
10556 scope_die = context_die;
10557
10558 return scope_die;
10559 }
10560
10561 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10562
10563 static inline int
10564 local_scope_p (dw_die_ref context_die)
10565 {
10566 for (; context_die; context_die = context_die->die_parent)
10567 if (context_die->die_tag == DW_TAG_inlined_subroutine
10568 || context_die->die_tag == DW_TAG_subprogram)
10569 return 1;
10570
10571 return 0;
10572 }
10573
10574 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10575 whether or not to treat a DIE in this context as a declaration. */
10576
10577 static inline int
10578 class_or_namespace_scope_p (dw_die_ref context_die)
10579 {
10580 return (context_die
10581 && (context_die->die_tag == DW_TAG_structure_type
10582 || context_die->die_tag == DW_TAG_union_type
10583 || context_die->die_tag == DW_TAG_namespace));
10584 }
10585
10586 /* Many forms of DIEs require a "type description" attribute. This
10587 routine locates the proper "type descriptor" die for the type given
10588 by 'type', and adds a DW_AT_type attribute below the given die. */
10589
10590 static void
10591 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10592 int decl_volatile, dw_die_ref context_die)
10593 {
10594 enum tree_code code = TREE_CODE (type);
10595 dw_die_ref type_die = NULL;
10596
10597 /* ??? If this type is an unnamed subrange type of an integral or
10598 floating-point type, use the inner type. This is because we have no
10599 support for unnamed types in base_type_die. This can happen if this is
10600 an Ada subrange type. Correct solution is emit a subrange type die. */
10601 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10602 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10603 type = TREE_TYPE (type), code = TREE_CODE (type);
10604
10605 if (code == ERROR_MARK
10606 /* Handle a special case. For functions whose return type is void, we
10607 generate *no* type attribute. (Note that no object may have type
10608 `void', so this only applies to function return types). */
10609 || code == VOID_TYPE)
10610 return;
10611
10612 type_die = modified_type_die (type,
10613 decl_const || TYPE_READONLY (type),
10614 decl_volatile || TYPE_VOLATILE (type),
10615 context_die);
10616
10617 if (type_die != NULL)
10618 add_AT_die_ref (object_die, DW_AT_type, type_die);
10619 }
10620
10621 /* Given a tree pointer to a struct, class, union, or enum type node, return
10622 a pointer to the (string) tag name for the given type, or zero if the type
10623 was declared without a tag. */
10624
10625 static const char *
10626 type_tag (tree type)
10627 {
10628 const char *name = 0;
10629
10630 if (TYPE_NAME (type) != 0)
10631 {
10632 tree t = 0;
10633
10634 /* Find the IDENTIFIER_NODE for the type name. */
10635 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10636 t = TYPE_NAME (type);
10637
10638 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10639 a TYPE_DECL node, regardless of whether or not a `typedef' was
10640 involved. */
10641 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10642 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10643 t = DECL_NAME (TYPE_NAME (type));
10644
10645 /* Now get the name as a string, or invent one. */
10646 if (t != 0)
10647 name = IDENTIFIER_POINTER (t);
10648 }
10649
10650 return (name == 0 || *name == '\0') ? 0 : name;
10651 }
10652
10653 /* Return the type associated with a data member, make a special check
10654 for bit field types. */
10655
10656 static inline tree
10657 member_declared_type (tree member)
10658 {
10659 return (DECL_BIT_FIELD_TYPE (member)
10660 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10661 }
10662
10663 /* Get the decl's label, as described by its RTL. This may be different
10664 from the DECL_NAME name used in the source file. */
10665
10666 #if 0
10667 static const char *
10668 decl_start_label (tree decl)
10669 {
10670 rtx x;
10671 const char *fnname;
10672
10673 x = DECL_RTL (decl);
10674 gcc_assert (MEM_P (x));
10675
10676 x = XEXP (x, 0);
10677 gcc_assert (GET_CODE (x) == SYMBOL_REF);
10678
10679 fnname = XSTR (x, 0);
10680 return fnname;
10681 }
10682 #endif
10683 \f
10684 /* These routines generate the internal representation of the DIE's for
10685 the compilation unit. Debugging information is collected by walking
10686 the declaration trees passed in from dwarf2out_decl(). */
10687
10688 static void
10689 gen_array_type_die (tree type, dw_die_ref context_die)
10690 {
10691 dw_die_ref scope_die = scope_die_for (type, context_die);
10692 dw_die_ref array_die;
10693 tree element_type;
10694
10695 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10696 the inner array type comes before the outer array type. Thus we must
10697 call gen_type_die before we call new_die. See below also. */
10698 #ifdef MIPS_DEBUGGING_INFO
10699 gen_type_die (TREE_TYPE (type), context_die);
10700 #endif
10701
10702 array_die = new_die (DW_TAG_array_type, scope_die, type);
10703 add_name_attribute (array_die, type_tag (type));
10704 equate_type_number_to_die (type, array_die);
10705
10706 if (TREE_CODE (type) == VECTOR_TYPE)
10707 {
10708 /* The frontend feeds us a representation for the vector as a struct
10709 containing an array. Pull out the array type. */
10710 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10711 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10712 }
10713
10714 #if 0
10715 /* We default the array ordering. SDB will probably do
10716 the right things even if DW_AT_ordering is not present. It's not even
10717 an issue until we start to get into multidimensional arrays anyway. If
10718 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10719 then we'll have to put the DW_AT_ordering attribute back in. (But if
10720 and when we find out that we need to put these in, we will only do so
10721 for multidimensional arrays. */
10722 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10723 #endif
10724
10725 #ifdef MIPS_DEBUGGING_INFO
10726 /* The SGI compilers handle arrays of unknown bound by setting
10727 AT_declaration and not emitting any subrange DIEs. */
10728 if (! TYPE_DOMAIN (type))
10729 add_AT_flag (array_die, DW_AT_declaration, 1);
10730 else
10731 #endif
10732 add_subscript_info (array_die, type);
10733
10734 /* Add representation of the type of the elements of this array type. */
10735 element_type = TREE_TYPE (type);
10736
10737 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10738 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10739 We work around this by disabling this feature. See also
10740 add_subscript_info. */
10741 #ifndef MIPS_DEBUGGING_INFO
10742 while (TREE_CODE (element_type) == ARRAY_TYPE)
10743 element_type = TREE_TYPE (element_type);
10744
10745 gen_type_die (element_type, context_die);
10746 #endif
10747
10748 add_type_attribute (array_die, element_type, 0, 0, context_die);
10749 }
10750
10751 static void
10752 gen_set_type_die (tree type, dw_die_ref context_die)
10753 {
10754 dw_die_ref type_die
10755 = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
10756
10757 equate_type_number_to_die (type, type_die);
10758 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
10759 }
10760
10761 #if 0
10762 static void
10763 gen_entry_point_die (tree decl, dw_die_ref context_die)
10764 {
10765 tree origin = decl_ultimate_origin (decl);
10766 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10767
10768 if (origin != NULL)
10769 add_abstract_origin_attribute (decl_die, origin);
10770 else
10771 {
10772 add_name_and_src_coords_attributes (decl_die, decl);
10773 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10774 0, 0, context_die);
10775 }
10776
10777 if (DECL_ABSTRACT (decl))
10778 equate_decl_number_to_die (decl, decl_die);
10779 else
10780 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10781 }
10782 #endif
10783
10784 /* Walk through the list of incomplete types again, trying once more to
10785 emit full debugging info for them. */
10786
10787 static void
10788 retry_incomplete_types (void)
10789 {
10790 int i;
10791
10792 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
10793 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
10794 }
10795
10796 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10797
10798 static void
10799 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10800 {
10801 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10802
10803 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10804 be incomplete and such types are not marked. */
10805 add_abstract_origin_attribute (type_die, type);
10806 }
10807
10808 /* Generate a DIE to represent an inlined instance of a structure type. */
10809
10810 static void
10811 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
10812 {
10813 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
10814
10815 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10816 be incomplete and such types are not marked. */
10817 add_abstract_origin_attribute (type_die, type);
10818 }
10819
10820 /* Generate a DIE to represent an inlined instance of a union type. */
10821
10822 static void
10823 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
10824 {
10825 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
10826
10827 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10828 be incomplete and such types are not marked. */
10829 add_abstract_origin_attribute (type_die, type);
10830 }
10831
10832 /* Generate a DIE to represent an enumeration type. Note that these DIEs
10833 include all of the information about the enumeration values also. Each
10834 enumerated type name/value is listed as a child of the enumerated type
10835 DIE. */
10836
10837 static dw_die_ref
10838 gen_enumeration_type_die (tree type, dw_die_ref context_die)
10839 {
10840 dw_die_ref type_die = lookup_type_die (type);
10841
10842 if (type_die == NULL)
10843 {
10844 type_die = new_die (DW_TAG_enumeration_type,
10845 scope_die_for (type, context_die), type);
10846 equate_type_number_to_die (type, type_die);
10847 add_name_attribute (type_die, type_tag (type));
10848 }
10849 else if (! TYPE_SIZE (type))
10850 return type_die;
10851 else
10852 remove_AT (type_die, DW_AT_declaration);
10853
10854 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
10855 given enum type is incomplete, do not generate the DW_AT_byte_size
10856 attribute or the DW_AT_element_list attribute. */
10857 if (TYPE_SIZE (type))
10858 {
10859 tree link;
10860
10861 TREE_ASM_WRITTEN (type) = 1;
10862 add_byte_size_attribute (type_die, type);
10863 if (TYPE_STUB_DECL (type) != NULL_TREE)
10864 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10865
10866 /* If the first reference to this type was as the return type of an
10867 inline function, then it may not have a parent. Fix this now. */
10868 if (type_die->die_parent == NULL)
10869 add_child_die (scope_die_for (type, context_die), type_die);
10870
10871 for (link = TYPE_VALUES (type);
10872 link != NULL; link = TREE_CHAIN (link))
10873 {
10874 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
10875 tree value = TREE_VALUE (link);
10876
10877 add_name_attribute (enum_die,
10878 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
10879
10880 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
10881 /* DWARF2 does not provide a way of indicating whether or
10882 not enumeration constants are signed or unsigned. GDB
10883 always assumes the values are signed, so we output all
10884 values as if they were signed. That means that
10885 enumeration constants with very large unsigned values
10886 will appear to have negative values in the debugger. */
10887 add_AT_int (enum_die, DW_AT_const_value,
10888 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
10889 }
10890 }
10891 else
10892 add_AT_flag (type_die, DW_AT_declaration, 1);
10893
10894 return type_die;
10895 }
10896
10897 /* Generate a DIE to represent either a real live formal parameter decl or to
10898 represent just the type of some formal parameter position in some function
10899 type.
10900
10901 Note that this routine is a bit unusual because its argument may be a
10902 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
10903 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
10904 node. If it's the former then this function is being called to output a
10905 DIE to represent a formal parameter object (or some inlining thereof). If
10906 it's the latter, then this function is only being called to output a
10907 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
10908 argument type of some subprogram type. */
10909
10910 static dw_die_ref
10911 gen_formal_parameter_die (tree node, dw_die_ref context_die)
10912 {
10913 dw_die_ref parm_die
10914 = new_die (DW_TAG_formal_parameter, context_die, node);
10915 tree origin;
10916
10917 switch (TREE_CODE_CLASS (TREE_CODE (node)))
10918 {
10919 case 'd':
10920 origin = decl_ultimate_origin (node);
10921 if (origin != NULL)
10922 add_abstract_origin_attribute (parm_die, origin);
10923 else
10924 {
10925 add_name_and_src_coords_attributes (parm_die, node);
10926 add_type_attribute (parm_die, TREE_TYPE (node),
10927 TREE_READONLY (node),
10928 TREE_THIS_VOLATILE (node),
10929 context_die);
10930 if (DECL_ARTIFICIAL (node))
10931 add_AT_flag (parm_die, DW_AT_artificial, 1);
10932 }
10933
10934 equate_decl_number_to_die (node, parm_die);
10935 if (! DECL_ABSTRACT (node))
10936 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
10937
10938 break;
10939
10940 case 't':
10941 /* We were called with some kind of a ..._TYPE node. */
10942 add_type_attribute (parm_die, node, 0, 0, context_die);
10943 break;
10944
10945 default:
10946 gcc_unreachable ();
10947 }
10948
10949 return parm_die;
10950 }
10951
10952 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
10953 at the end of an (ANSI prototyped) formal parameters list. */
10954
10955 static void
10956 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
10957 {
10958 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
10959 }
10960
10961 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
10962 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
10963 parameters as specified in some function type specification (except for
10964 those which appear as part of a function *definition*). */
10965
10966 static void
10967 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
10968 {
10969 tree link;
10970 tree formal_type = NULL;
10971 tree first_parm_type;
10972 tree arg;
10973
10974 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
10975 {
10976 arg = DECL_ARGUMENTS (function_or_method_type);
10977 function_or_method_type = TREE_TYPE (function_or_method_type);
10978 }
10979 else
10980 arg = NULL_TREE;
10981
10982 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
10983
10984 /* Make our first pass over the list of formal parameter types and output a
10985 DW_TAG_formal_parameter DIE for each one. */
10986 for (link = first_parm_type; link; )
10987 {
10988 dw_die_ref parm_die;
10989
10990 formal_type = TREE_VALUE (link);
10991 if (formal_type == void_type_node)
10992 break;
10993
10994 /* Output a (nameless) DIE to represent the formal parameter itself. */
10995 parm_die = gen_formal_parameter_die (formal_type, context_die);
10996 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
10997 && link == first_parm_type)
10998 || (arg && DECL_ARTIFICIAL (arg)))
10999 add_AT_flag (parm_die, DW_AT_artificial, 1);
11000
11001 link = TREE_CHAIN (link);
11002 if (arg)
11003 arg = TREE_CHAIN (arg);
11004 }
11005
11006 /* If this function type has an ellipsis, add a
11007 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11008 if (formal_type != void_type_node)
11009 gen_unspecified_parameters_die (function_or_method_type, context_die);
11010
11011 /* Make our second (and final) pass over the list of formal parameter types
11012 and output DIEs to represent those types (as necessary). */
11013 for (link = TYPE_ARG_TYPES (function_or_method_type);
11014 link && TREE_VALUE (link);
11015 link = TREE_CHAIN (link))
11016 gen_type_die (TREE_VALUE (link), context_die);
11017 }
11018
11019 /* We want to generate the DIE for TYPE so that we can generate the
11020 die for MEMBER, which has been defined; we will need to refer back
11021 to the member declaration nested within TYPE. If we're trying to
11022 generate minimal debug info for TYPE, processing TYPE won't do the
11023 trick; we need to attach the member declaration by hand. */
11024
11025 static void
11026 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11027 {
11028 gen_type_die (type, context_die);
11029
11030 /* If we're trying to avoid duplicate debug info, we may not have
11031 emitted the member decl for this function. Emit it now. */
11032 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11033 && ! lookup_decl_die (member))
11034 {
11035 gcc_assert (!decl_ultimate_origin (member));
11036
11037 push_decl_scope (type);
11038 if (TREE_CODE (member) == FUNCTION_DECL)
11039 gen_subprogram_die (member, lookup_type_die (type));
11040 else
11041 gen_variable_die (member, lookup_type_die (type));
11042
11043 pop_decl_scope ();
11044 }
11045 }
11046
11047 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11048 may later generate inlined and/or out-of-line instances of. */
11049
11050 static void
11051 dwarf2out_abstract_function (tree decl)
11052 {
11053 dw_die_ref old_die;
11054 tree save_fn;
11055 tree context;
11056 int was_abstract = DECL_ABSTRACT (decl);
11057
11058 /* Make sure we have the actual abstract inline, not a clone. */
11059 decl = DECL_ORIGIN (decl);
11060
11061 old_die = lookup_decl_die (decl);
11062 if (old_die && get_AT (old_die, DW_AT_inline))
11063 /* We've already generated the abstract instance. */
11064 return;
11065
11066 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11067 we don't get confused by DECL_ABSTRACT. */
11068 if (debug_info_level > DINFO_LEVEL_TERSE)
11069 {
11070 context = decl_class_context (decl);
11071 if (context)
11072 gen_type_die_for_member
11073 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11074 }
11075
11076 /* Pretend we've just finished compiling this function. */
11077 save_fn = current_function_decl;
11078 current_function_decl = decl;
11079
11080 set_decl_abstract_flags (decl, 1);
11081 dwarf2out_decl (decl);
11082 if (! was_abstract)
11083 set_decl_abstract_flags (decl, 0);
11084
11085 current_function_decl = save_fn;
11086 }
11087
11088 /* Generate a DIE to represent a declared function (either file-scope or
11089 block-local). */
11090
11091 static void
11092 gen_subprogram_die (tree decl, dw_die_ref context_die)
11093 {
11094 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11095 tree origin = decl_ultimate_origin (decl);
11096 dw_die_ref subr_die;
11097 rtx fp_reg;
11098 tree fn_arg_types;
11099 tree outer_scope;
11100 dw_die_ref old_die = lookup_decl_die (decl);
11101 int declaration = (current_function_decl != decl
11102 || class_or_namespace_scope_p (context_die));
11103
11104 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11105 started to generate the abstract instance of an inline, decided to output
11106 its containing class, and proceeded to emit the declaration of the inline
11107 from the member list for the class. If so, DECLARATION takes priority;
11108 we'll get back to the abstract instance when done with the class. */
11109
11110 /* The class-scope declaration DIE must be the primary DIE. */
11111 if (origin && declaration && class_or_namespace_scope_p (context_die))
11112 {
11113 origin = NULL;
11114 gcc_assert (!old_die);
11115 }
11116
11117 if (origin != NULL)
11118 {
11119 gcc_assert (!declaration || local_scope_p (context_die));
11120
11121 /* Fixup die_parent for the abstract instance of a nested
11122 inline function. */
11123 if (old_die && old_die->die_parent == NULL)
11124 add_child_die (context_die, old_die);
11125
11126 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11127 add_abstract_origin_attribute (subr_die, origin);
11128 }
11129 else if (old_die)
11130 {
11131 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11132 unsigned file_index = lookup_filename (s.file);
11133
11134 if (!get_AT_flag (old_die, DW_AT_declaration)
11135 /* We can have a normal definition following an inline one in the
11136 case of redefinition of GNU C extern inlines.
11137 It seems reasonable to use AT_specification in this case. */
11138 && !get_AT (old_die, DW_AT_inline))
11139 {
11140 /* ??? This can happen if there is a bug in the program, for
11141 instance, if it has duplicate function definitions. Ideally,
11142 we should detect this case and ignore it. For now, if we have
11143 already reported an error, any error at all, then assume that
11144 we got here because of an input error, not a dwarf2 bug. */
11145 gcc_assert (errorcount);
11146 return;
11147 }
11148
11149 /* If the definition comes from the same place as the declaration,
11150 maybe use the old DIE. We always want the DIE for this function
11151 that has the *_pc attributes to be under comp_unit_die so the
11152 debugger can find it. We also need to do this for abstract
11153 instances of inlines, since the spec requires the out-of-line copy
11154 to have the same parent. For local class methods, this doesn't
11155 apply; we just use the old DIE. */
11156 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11157 && (DECL_ARTIFICIAL (decl)
11158 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11159 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11160 == (unsigned) s.line))))
11161 {
11162 subr_die = old_die;
11163
11164 /* Clear out the declaration attribute and the formal parameters.
11165 Do not remove all children, because it is possible that this
11166 declaration die was forced using force_decl_die(). In such
11167 cases die that forced declaration die (e.g. TAG_imported_module)
11168 is one of the children that we do not want to remove. */
11169 remove_AT (subr_die, DW_AT_declaration);
11170 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11171 }
11172 else
11173 {
11174 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11175 add_AT_specification (subr_die, old_die);
11176 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11177 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11178 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11179 != (unsigned) s.line)
11180 add_AT_unsigned
11181 (subr_die, DW_AT_decl_line, s.line);
11182 }
11183 }
11184 else
11185 {
11186 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11187
11188 if (TREE_PUBLIC (decl))
11189 add_AT_flag (subr_die, DW_AT_external, 1);
11190
11191 add_name_and_src_coords_attributes (subr_die, decl);
11192 if (debug_info_level > DINFO_LEVEL_TERSE)
11193 {
11194 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11195 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11196 0, 0, context_die);
11197 }
11198
11199 add_pure_or_virtual_attribute (subr_die, decl);
11200 if (DECL_ARTIFICIAL (decl))
11201 add_AT_flag (subr_die, DW_AT_artificial, 1);
11202
11203 if (TREE_PROTECTED (decl))
11204 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11205 else if (TREE_PRIVATE (decl))
11206 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11207 }
11208
11209 if (declaration)
11210 {
11211 if (!old_die || !get_AT (old_die, DW_AT_inline))
11212 {
11213 add_AT_flag (subr_die, DW_AT_declaration, 1);
11214
11215 /* The first time we see a member function, it is in the context of
11216 the class to which it belongs. We make sure of this by emitting
11217 the class first. The next time is the definition, which is
11218 handled above. The two may come from the same source text.
11219
11220 Note that force_decl_die() forces function declaration die. It is
11221 later reused to represent definition. */
11222 equate_decl_number_to_die (decl, subr_die);
11223 }
11224 }
11225 else if (DECL_ABSTRACT (decl))
11226 {
11227 if (DECL_DECLARED_INLINE_P (decl))
11228 {
11229 if (cgraph_function_possibly_inlined_p (decl))
11230 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11231 else
11232 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11233 }
11234 else
11235 {
11236 if (cgraph_function_possibly_inlined_p (decl))
11237 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11238 else
11239 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11240 }
11241
11242 equate_decl_number_to_die (decl, subr_die);
11243 }
11244 else if (!DECL_EXTERNAL (decl))
11245 {
11246 if (!old_die || !get_AT (old_die, DW_AT_inline))
11247 equate_decl_number_to_die (decl, subr_die);
11248
11249 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11250 current_function_funcdef_no);
11251 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11252 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11253 current_function_funcdef_no);
11254 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11255
11256 add_pubname (decl, subr_die);
11257 add_arange (decl, subr_die);
11258
11259 #ifdef MIPS_DEBUGGING_INFO
11260 /* Add a reference to the FDE for this routine. */
11261 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11262 #endif
11263
11264 /* Define the "frame base" location for this routine. We use the
11265 frame pointer or stack pointer registers, since the RTL for local
11266 variables is relative to one of them. */
11267 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11268 {
11269 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11270 DW_AT_frame_base);
11271 }
11272 else
11273 {
11274 fp_reg
11275 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11276 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11277 }
11278
11279 if (cfun->static_chain_decl)
11280 add_AT_location_description (subr_die, DW_AT_static_link,
11281 loc_descriptor_from_tree (cfun->static_chain_decl));
11282 }
11283
11284 /* Now output descriptions of the arguments for this function. This gets
11285 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11286 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11287 `...' at the end of the formal parameter list. In order to find out if
11288 there was a trailing ellipsis or not, we must instead look at the type
11289 associated with the FUNCTION_DECL. This will be a node of type
11290 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11291 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11292 an ellipsis at the end. */
11293
11294 /* In the case where we are describing a mere function declaration, all we
11295 need to do here (and all we *can* do here) is to describe the *types* of
11296 its formal parameters. */
11297 if (debug_info_level <= DINFO_LEVEL_TERSE)
11298 ;
11299 else if (declaration)
11300 gen_formal_types_die (decl, subr_die);
11301 else
11302 {
11303 /* Generate DIEs to represent all known formal parameters. */
11304 tree arg_decls = DECL_ARGUMENTS (decl);
11305 tree parm;
11306
11307 /* When generating DIEs, generate the unspecified_parameters DIE
11308 instead if we come across the arg "__builtin_va_alist" */
11309 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11310 if (TREE_CODE (parm) == PARM_DECL)
11311 {
11312 if (DECL_NAME (parm)
11313 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11314 "__builtin_va_alist"))
11315 gen_unspecified_parameters_die (parm, subr_die);
11316 else
11317 gen_decl_die (parm, subr_die);
11318 }
11319
11320 /* Decide whether we need an unspecified_parameters DIE at the end.
11321 There are 2 more cases to do this for: 1) the ansi ... declaration -
11322 this is detectable when the end of the arg list is not a
11323 void_type_node 2) an unprototyped function declaration (not a
11324 definition). This just means that we have no info about the
11325 parameters at all. */
11326 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11327 if (fn_arg_types != NULL)
11328 {
11329 /* This is the prototyped case, check for.... */
11330 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11331 gen_unspecified_parameters_die (decl, subr_die);
11332 }
11333 else if (DECL_INITIAL (decl) == NULL_TREE)
11334 gen_unspecified_parameters_die (decl, subr_die);
11335 }
11336
11337 /* Output Dwarf info for all of the stuff within the body of the function
11338 (if it has one - it may be just a declaration). */
11339 outer_scope = DECL_INITIAL (decl);
11340
11341 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11342 a function. This BLOCK actually represents the outermost binding contour
11343 for the function, i.e. the contour in which the function's formal
11344 parameters and labels get declared. Curiously, it appears that the front
11345 end doesn't actually put the PARM_DECL nodes for the current function onto
11346 the BLOCK_VARS list for this outer scope, but are strung off of the
11347 DECL_ARGUMENTS list for the function instead.
11348
11349 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11350 the LABEL_DECL nodes for the function however, and we output DWARF info
11351 for those in decls_for_scope. Just within the `outer_scope' there will be
11352 a BLOCK node representing the function's outermost pair of curly braces,
11353 and any blocks used for the base and member initializers of a C++
11354 constructor function. */
11355 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11356 {
11357 /* Emit a DW_TAG_variable DIE for a named return value. */
11358 if (DECL_NAME (DECL_RESULT (decl)))
11359 gen_decl_die (DECL_RESULT (decl), subr_die);
11360
11361 current_function_has_inlines = 0;
11362 decls_for_scope (outer_scope, subr_die, 0);
11363
11364 #if 0 && defined (MIPS_DEBUGGING_INFO)
11365 if (current_function_has_inlines)
11366 {
11367 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11368 if (! comp_unit_has_inlines)
11369 {
11370 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11371 comp_unit_has_inlines = 1;
11372 }
11373 }
11374 #endif
11375 }
11376 }
11377
11378 /* Generate a DIE to represent a declared data object. */
11379
11380 static void
11381 gen_variable_die (tree decl, dw_die_ref context_die)
11382 {
11383 tree origin = decl_ultimate_origin (decl);
11384 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11385
11386 dw_die_ref old_die = lookup_decl_die (decl);
11387 int declaration = (DECL_EXTERNAL (decl)
11388 || class_or_namespace_scope_p (context_die));
11389
11390 if (origin != NULL)
11391 add_abstract_origin_attribute (var_die, origin);
11392
11393 /* Loop unrolling can create multiple blocks that refer to the same
11394 static variable, so we must test for the DW_AT_declaration flag.
11395
11396 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11397 copy decls and set the DECL_ABSTRACT flag on them instead of
11398 sharing them.
11399
11400 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
11401 else if (old_die && TREE_STATIC (decl)
11402 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11403 {
11404 /* This is a definition of a C++ class level static. */
11405 add_AT_specification (var_die, old_die);
11406 if (DECL_NAME (decl))
11407 {
11408 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11409 unsigned file_index = lookup_filename (s.file);
11410
11411 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11412 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11413
11414 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11415 != (unsigned) s.line)
11416
11417 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11418 }
11419 }
11420 else
11421 {
11422 add_name_and_src_coords_attributes (var_die, decl);
11423 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11424 TREE_THIS_VOLATILE (decl), context_die);
11425
11426 if (TREE_PUBLIC (decl))
11427 add_AT_flag (var_die, DW_AT_external, 1);
11428
11429 if (DECL_ARTIFICIAL (decl))
11430 add_AT_flag (var_die, DW_AT_artificial, 1);
11431
11432 if (TREE_PROTECTED (decl))
11433 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11434 else if (TREE_PRIVATE (decl))
11435 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11436 }
11437
11438 if (declaration)
11439 add_AT_flag (var_die, DW_AT_declaration, 1);
11440
11441 if (DECL_ABSTRACT (decl) || declaration)
11442 equate_decl_number_to_die (decl, var_die);
11443
11444 if (! declaration && ! DECL_ABSTRACT (decl))
11445 {
11446 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11447 add_pubname (decl, var_die);
11448 }
11449 else
11450 tree_add_const_value_attribute (var_die, decl);
11451 }
11452
11453 /* Generate a DIE to represent a label identifier. */
11454
11455 static void
11456 gen_label_die (tree decl, dw_die_ref context_die)
11457 {
11458 tree origin = decl_ultimate_origin (decl);
11459 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11460 rtx insn;
11461 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11462
11463 if (origin != NULL)
11464 add_abstract_origin_attribute (lbl_die, origin);
11465 else
11466 add_name_and_src_coords_attributes (lbl_die, decl);
11467
11468 if (DECL_ABSTRACT (decl))
11469 equate_decl_number_to_die (decl, lbl_die);
11470 else
11471 {
11472 insn = DECL_RTL_IF_SET (decl);
11473
11474 /* Deleted labels are programmer specified labels which have been
11475 eliminated because of various optimizations. We still emit them
11476 here so that it is possible to put breakpoints on them. */
11477 if (insn
11478 && (LABEL_P (insn)
11479 || ((NOTE_P (insn)
11480 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11481 {
11482 /* When optimization is enabled (via -O) some parts of the compiler
11483 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11484 represent source-level labels which were explicitly declared by
11485 the user. This really shouldn't be happening though, so catch
11486 it if it ever does happen. */
11487 gcc_assert (!INSN_DELETED_P (insn));
11488
11489 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11490 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11491 }
11492 }
11493 }
11494
11495 /* Generate a DIE for a lexical block. */
11496
11497 static void
11498 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11499 {
11500 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11501 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11502
11503 if (! BLOCK_ABSTRACT (stmt))
11504 {
11505 if (BLOCK_FRAGMENT_CHAIN (stmt))
11506 {
11507 tree chain;
11508
11509 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
11510
11511 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11512 do
11513 {
11514 add_ranges (chain);
11515 chain = BLOCK_FRAGMENT_CHAIN (chain);
11516 }
11517 while (chain);
11518 add_ranges (NULL);
11519 }
11520 else
11521 {
11522 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11523 BLOCK_NUMBER (stmt));
11524 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11525 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11526 BLOCK_NUMBER (stmt));
11527 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11528 }
11529 }
11530
11531 decls_for_scope (stmt, stmt_die, depth);
11532 }
11533
11534 /* Generate a DIE for an inlined subprogram. */
11535
11536 static void
11537 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11538 {
11539 tree decl = block_ultimate_origin (stmt);
11540
11541 /* Emit info for the abstract instance first, if we haven't yet. We
11542 must emit this even if the block is abstract, otherwise when we
11543 emit the block below (or elsewhere), we may end up trying to emit
11544 a die whose origin die hasn't been emitted, and crashing. */
11545 dwarf2out_abstract_function (decl);
11546
11547 if (! BLOCK_ABSTRACT (stmt))
11548 {
11549 dw_die_ref subr_die
11550 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11551 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11552
11553 add_abstract_origin_attribute (subr_die, decl);
11554 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11555 BLOCK_NUMBER (stmt));
11556 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
11557 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11558 BLOCK_NUMBER (stmt));
11559 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
11560 decls_for_scope (stmt, subr_die, depth);
11561 current_function_has_inlines = 1;
11562 }
11563 else
11564 /* We may get here if we're the outer block of function A that was
11565 inlined into function B that was inlined into function C. When
11566 generating debugging info for C, dwarf2out_abstract_function(B)
11567 would mark all inlined blocks as abstract, including this one.
11568 So, we wouldn't (and shouldn't) expect labels to be generated
11569 for this one. Instead, just emit debugging info for
11570 declarations within the block. This is particularly important
11571 in the case of initializers of arguments passed from B to us:
11572 if they're statement expressions containing declarations, we
11573 wouldn't generate dies for their abstract variables, and then,
11574 when generating dies for the real variables, we'd die (pun
11575 intended :-) */
11576 gen_lexical_block_die (stmt, context_die, depth);
11577 }
11578
11579 /* Generate a DIE for a field in a record, or structure. */
11580
11581 static void
11582 gen_field_die (tree decl, dw_die_ref context_die)
11583 {
11584 dw_die_ref decl_die;
11585
11586 if (TREE_TYPE (decl) == error_mark_node)
11587 return;
11588
11589 decl_die = new_die (DW_TAG_member, context_die, decl);
11590 add_name_and_src_coords_attributes (decl_die, decl);
11591 add_type_attribute (decl_die, member_declared_type (decl),
11592 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11593 context_die);
11594
11595 if (DECL_BIT_FIELD_TYPE (decl))
11596 {
11597 add_byte_size_attribute (decl_die, decl);
11598 add_bit_size_attribute (decl_die, decl);
11599 add_bit_offset_attribute (decl_die, decl);
11600 }
11601
11602 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11603 add_data_member_location_attribute (decl_die, decl);
11604
11605 if (DECL_ARTIFICIAL (decl))
11606 add_AT_flag (decl_die, DW_AT_artificial, 1);
11607
11608 if (TREE_PROTECTED (decl))
11609 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11610 else if (TREE_PRIVATE (decl))
11611 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11612
11613 /* Equate decl number to die, so that we can look up this decl later on. */
11614 equate_decl_number_to_die (decl, decl_die);
11615 }
11616
11617 #if 0
11618 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11619 Use modified_type_die instead.
11620 We keep this code here just in case these types of DIEs may be needed to
11621 represent certain things in other languages (e.g. Pascal) someday. */
11622
11623 static void
11624 gen_pointer_type_die (tree type, dw_die_ref context_die)
11625 {
11626 dw_die_ref ptr_die
11627 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11628
11629 equate_type_number_to_die (type, ptr_die);
11630 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11631 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11632 }
11633
11634 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11635 Use modified_type_die instead.
11636 We keep this code here just in case these types of DIEs may be needed to
11637 represent certain things in other languages (e.g. Pascal) someday. */
11638
11639 static void
11640 gen_reference_type_die (tree type, dw_die_ref context_die)
11641 {
11642 dw_die_ref ref_die
11643 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11644
11645 equate_type_number_to_die (type, ref_die);
11646 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11647 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11648 }
11649 #endif
11650
11651 /* Generate a DIE for a pointer to a member type. */
11652
11653 static void
11654 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11655 {
11656 dw_die_ref ptr_die
11657 = new_die (DW_TAG_ptr_to_member_type,
11658 scope_die_for (type, context_die), type);
11659
11660 equate_type_number_to_die (type, ptr_die);
11661 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11662 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11663 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11664 }
11665
11666 /* Generate the DIE for the compilation unit. */
11667
11668 static dw_die_ref
11669 gen_compile_unit_die (const char *filename)
11670 {
11671 dw_die_ref die;
11672 char producer[250];
11673 const char *language_string = lang_hooks.name;
11674 int language;
11675
11676 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11677
11678 if (filename)
11679 {
11680 add_name_attribute (die, filename);
11681 /* Don't add cwd for <built-in>. */
11682 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11683 add_comp_dir_attribute (die);
11684 }
11685
11686 sprintf (producer, "%s %s", language_string, version_string);
11687
11688 #ifdef MIPS_DEBUGGING_INFO
11689 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11690 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11691 not appear in the producer string, the debugger reaches the conclusion
11692 that the object file is stripped and has no debugging information.
11693 To get the MIPS/SGI debugger to believe that there is debugging
11694 information in the object file, we add a -g to the producer string. */
11695 if (debug_info_level > DINFO_LEVEL_TERSE)
11696 strcat (producer, " -g");
11697 #endif
11698
11699 add_AT_string (die, DW_AT_producer, producer);
11700
11701 if (strcmp (language_string, "GNU C++") == 0)
11702 language = DW_LANG_C_plus_plus;
11703 else if (strcmp (language_string, "GNU Ada") == 0)
11704 language = DW_LANG_Ada95;
11705 else if (strcmp (language_string, "GNU F77") == 0)
11706 language = DW_LANG_Fortran77;
11707 else if (strcmp (language_string, "GNU F95") == 0)
11708 language = DW_LANG_Fortran95;
11709 else if (strcmp (language_string, "GNU Pascal") == 0)
11710 language = DW_LANG_Pascal83;
11711 else if (strcmp (language_string, "GNU Java") == 0)
11712 language = DW_LANG_Java;
11713 else
11714 language = DW_LANG_C89;
11715
11716 add_AT_unsigned (die, DW_AT_language, language);
11717 return die;
11718 }
11719
11720 /* Generate a DIE for a string type. */
11721
11722 static void
11723 gen_string_type_die (tree type, dw_die_ref context_die)
11724 {
11725 dw_die_ref type_die
11726 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11727
11728 equate_type_number_to_die (type, type_die);
11729
11730 /* ??? Fudge the string length attribute for now.
11731 TODO: add string length info. */
11732 #if 0
11733 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11734 bound_representation (upper_bound, 0, 'u');
11735 #endif
11736 }
11737
11738 /* Generate the DIE for a base class. */
11739
11740 static void
11741 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11742 {
11743 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11744
11745 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11746 add_data_member_location_attribute (die, binfo);
11747
11748 if (BINFO_VIRTUAL_P (binfo))
11749 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11750
11751 if (access == access_public_node)
11752 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11753 else if (access == access_protected_node)
11754 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11755 }
11756
11757 /* Generate a DIE for a class member. */
11758
11759 static void
11760 gen_member_die (tree type, dw_die_ref context_die)
11761 {
11762 tree member;
11763 tree binfo = TYPE_BINFO (type);
11764 dw_die_ref child;
11765
11766 /* If this is not an incomplete type, output descriptions of each of its
11767 members. Note that as we output the DIEs necessary to represent the
11768 members of this record or union type, we will also be trying to output
11769 DIEs to represent the *types* of those members. However the `type'
11770 function (above) will specifically avoid generating type DIEs for member
11771 types *within* the list of member DIEs for this (containing) type except
11772 for those types (of members) which are explicitly marked as also being
11773 members of this (containing) type themselves. The g++ front- end can
11774 force any given type to be treated as a member of some other (containing)
11775 type by setting the TYPE_CONTEXT of the given (member) type to point to
11776 the TREE node representing the appropriate (containing) type. */
11777
11778 /* First output info about the base classes. */
11779 if (binfo)
11780 {
11781 VEC (tree) *accesses = BINFO_BASE_ACCESSES (binfo);
11782 int i;
11783 tree base;
11784
11785 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
11786 gen_inheritance_die (base,
11787 (accesses ? VEC_index (tree, accesses, i)
11788 : access_public_node), context_die);
11789 }
11790
11791 /* Now output info about the data members and type members. */
11792 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
11793 {
11794 /* If we thought we were generating minimal debug info for TYPE
11795 and then changed our minds, some of the member declarations
11796 may have already been defined. Don't define them again, but
11797 do put them in the right order. */
11798
11799 child = lookup_decl_die (member);
11800 if (child)
11801 splice_child_die (context_die, child);
11802 else
11803 gen_decl_die (member, context_die);
11804 }
11805
11806 /* Now output info about the function members (if any). */
11807 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
11808 {
11809 /* Don't include clones in the member list. */
11810 if (DECL_ABSTRACT_ORIGIN (member))
11811 continue;
11812
11813 child = lookup_decl_die (member);
11814 if (child)
11815 splice_child_die (context_die, child);
11816 else
11817 gen_decl_die (member, context_die);
11818 }
11819 }
11820
11821 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
11822 is set, we pretend that the type was never defined, so we only get the
11823 member DIEs needed by later specification DIEs. */
11824
11825 static void
11826 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
11827 {
11828 dw_die_ref type_die = lookup_type_die (type);
11829 dw_die_ref scope_die = 0;
11830 int nested = 0;
11831 int complete = (TYPE_SIZE (type)
11832 && (! TYPE_STUB_DECL (type)
11833 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
11834 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
11835
11836 if (type_die && ! complete)
11837 return;
11838
11839 if (TYPE_CONTEXT (type) != NULL_TREE
11840 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11841 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
11842 nested = 1;
11843
11844 scope_die = scope_die_for (type, context_die);
11845
11846 if (! type_die || (nested && scope_die == comp_unit_die))
11847 /* First occurrence of type or toplevel definition of nested class. */
11848 {
11849 dw_die_ref old_die = type_die;
11850
11851 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11852 ? DW_TAG_structure_type : DW_TAG_union_type,
11853 scope_die, type);
11854 equate_type_number_to_die (type, type_die);
11855 if (old_die)
11856 add_AT_specification (type_die, old_die);
11857 else
11858 add_name_attribute (type_die, type_tag (type));
11859 }
11860 else
11861 remove_AT (type_die, DW_AT_declaration);
11862
11863 /* If this type has been completed, then give it a byte_size attribute and
11864 then give a list of members. */
11865 if (complete && !ns_decl)
11866 {
11867 /* Prevent infinite recursion in cases where the type of some member of
11868 this type is expressed in terms of this type itself. */
11869 TREE_ASM_WRITTEN (type) = 1;
11870 add_byte_size_attribute (type_die, type);
11871 if (TYPE_STUB_DECL (type) != NULL_TREE)
11872 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11873
11874 /* If the first reference to this type was as the return type of an
11875 inline function, then it may not have a parent. Fix this now. */
11876 if (type_die->die_parent == NULL)
11877 add_child_die (scope_die, type_die);
11878
11879 push_decl_scope (type);
11880 gen_member_die (type, type_die);
11881 pop_decl_scope ();
11882
11883 /* GNU extension: Record what type our vtable lives in. */
11884 if (TYPE_VFIELD (type))
11885 {
11886 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
11887
11888 gen_type_die (vtype, context_die);
11889 add_AT_die_ref (type_die, DW_AT_containing_type,
11890 lookup_type_die (vtype));
11891 }
11892 }
11893 else
11894 {
11895 add_AT_flag (type_die, DW_AT_declaration, 1);
11896
11897 /* We don't need to do this for function-local types. */
11898 if (TYPE_STUB_DECL (type)
11899 && ! decl_function_context (TYPE_STUB_DECL (type)))
11900 VARRAY_PUSH_TREE (incomplete_types, type);
11901 }
11902 }
11903
11904 /* Generate a DIE for a subroutine _type_. */
11905
11906 static void
11907 gen_subroutine_type_die (tree type, dw_die_ref context_die)
11908 {
11909 tree return_type = TREE_TYPE (type);
11910 dw_die_ref subr_die
11911 = new_die (DW_TAG_subroutine_type,
11912 scope_die_for (type, context_die), type);
11913
11914 equate_type_number_to_die (type, subr_die);
11915 add_prototyped_attribute (subr_die, type);
11916 add_type_attribute (subr_die, return_type, 0, 0, context_die);
11917 gen_formal_types_die (type, subr_die);
11918 }
11919
11920 /* Generate a DIE for a type definition. */
11921
11922 static void
11923 gen_typedef_die (tree decl, dw_die_ref context_die)
11924 {
11925 dw_die_ref type_die;
11926 tree origin;
11927
11928 if (TREE_ASM_WRITTEN (decl))
11929 return;
11930
11931 TREE_ASM_WRITTEN (decl) = 1;
11932 type_die = new_die (DW_TAG_typedef, context_die, decl);
11933 origin = decl_ultimate_origin (decl);
11934 if (origin != NULL)
11935 add_abstract_origin_attribute (type_die, origin);
11936 else
11937 {
11938 tree type;
11939
11940 add_name_and_src_coords_attributes (type_die, decl);
11941 if (DECL_ORIGINAL_TYPE (decl))
11942 {
11943 type = DECL_ORIGINAL_TYPE (decl);
11944
11945 gcc_assert (type != TREE_TYPE (decl));
11946 equate_type_number_to_die (TREE_TYPE (decl), type_die);
11947 }
11948 else
11949 type = TREE_TYPE (decl);
11950
11951 add_type_attribute (type_die, type, TREE_READONLY (decl),
11952 TREE_THIS_VOLATILE (decl), context_die);
11953 }
11954
11955 if (DECL_ABSTRACT (decl))
11956 equate_decl_number_to_die (decl, type_die);
11957 }
11958
11959 /* Generate a type description DIE. */
11960
11961 static void
11962 gen_type_die (tree type, dw_die_ref context_die)
11963 {
11964 int need_pop;
11965
11966 if (type == NULL_TREE || type == error_mark_node)
11967 return;
11968
11969 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11970 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
11971 {
11972 if (TREE_ASM_WRITTEN (type))
11973 return;
11974
11975 /* Prevent broken recursion; we can't hand off to the same type. */
11976 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
11977
11978 TREE_ASM_WRITTEN (type) = 1;
11979 gen_decl_die (TYPE_NAME (type), context_die);
11980 return;
11981 }
11982
11983 /* We are going to output a DIE to represent the unqualified version
11984 of this type (i.e. without any const or volatile qualifiers) so
11985 get the main variant (i.e. the unqualified version) of this type
11986 now. (Vectors are special because the debugging info is in the
11987 cloned type itself). */
11988 if (TREE_CODE (type) != VECTOR_TYPE)
11989 type = type_main_variant (type);
11990
11991 if (TREE_ASM_WRITTEN (type))
11992 return;
11993
11994 switch (TREE_CODE (type))
11995 {
11996 case ERROR_MARK:
11997 break;
11998
11999 case POINTER_TYPE:
12000 case REFERENCE_TYPE:
12001 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12002 ensures that the gen_type_die recursion will terminate even if the
12003 type is recursive. Recursive types are possible in Ada. */
12004 /* ??? We could perhaps do this for all types before the switch
12005 statement. */
12006 TREE_ASM_WRITTEN (type) = 1;
12007
12008 /* For these types, all that is required is that we output a DIE (or a
12009 set of DIEs) to represent the "basis" type. */
12010 gen_type_die (TREE_TYPE (type), context_die);
12011 break;
12012
12013 case OFFSET_TYPE:
12014 /* This code is used for C++ pointer-to-data-member types.
12015 Output a description of the relevant class type. */
12016 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12017
12018 /* Output a description of the type of the object pointed to. */
12019 gen_type_die (TREE_TYPE (type), context_die);
12020
12021 /* Now output a DIE to represent this pointer-to-data-member type
12022 itself. */
12023 gen_ptr_to_mbr_type_die (type, context_die);
12024 break;
12025
12026 case SET_TYPE:
12027 gen_type_die (TYPE_DOMAIN (type), context_die);
12028 gen_set_type_die (type, context_die);
12029 break;
12030
12031 case FILE_TYPE:
12032 gen_type_die (TREE_TYPE (type), context_die);
12033 /* No way to represent these in Dwarf yet! */
12034 gcc_unreachable ();
12035 break;
12036
12037 case FUNCTION_TYPE:
12038 /* Force out return type (in case it wasn't forced out already). */
12039 gen_type_die (TREE_TYPE (type), context_die);
12040 gen_subroutine_type_die (type, context_die);
12041 break;
12042
12043 case METHOD_TYPE:
12044 /* Force out return type (in case it wasn't forced out already). */
12045 gen_type_die (TREE_TYPE (type), context_die);
12046 gen_subroutine_type_die (type, context_die);
12047 break;
12048
12049 case ARRAY_TYPE:
12050 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12051 {
12052 gen_type_die (TREE_TYPE (type), context_die);
12053 gen_string_type_die (type, context_die);
12054 }
12055 else
12056 gen_array_type_die (type, context_die);
12057 break;
12058
12059 case VECTOR_TYPE:
12060 gen_array_type_die (type, context_die);
12061 break;
12062
12063 case ENUMERAL_TYPE:
12064 case RECORD_TYPE:
12065 case UNION_TYPE:
12066 case QUAL_UNION_TYPE:
12067 /* If this is a nested type whose containing class hasn't been written
12068 out yet, writing it out will cover this one, too. This does not apply
12069 to instantiations of member class templates; they need to be added to
12070 the containing class as they are generated. FIXME: This hurts the
12071 idea of combining type decls from multiple TUs, since we can't predict
12072 what set of template instantiations we'll get. */
12073 if (TYPE_CONTEXT (type)
12074 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12075 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12076 {
12077 gen_type_die (TYPE_CONTEXT (type), context_die);
12078
12079 if (TREE_ASM_WRITTEN (type))
12080 return;
12081
12082 /* If that failed, attach ourselves to the stub. */
12083 push_decl_scope (TYPE_CONTEXT (type));
12084 context_die = lookup_type_die (TYPE_CONTEXT (type));
12085 need_pop = 1;
12086 }
12087 else
12088 {
12089 declare_in_namespace (type, context_die);
12090 need_pop = 0;
12091 }
12092
12093 if (TREE_CODE (type) == ENUMERAL_TYPE)
12094 gen_enumeration_type_die (type, context_die);
12095 else
12096 gen_struct_or_union_type_die (type, context_die);
12097
12098 if (need_pop)
12099 pop_decl_scope ();
12100
12101 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12102 it up if it is ever completed. gen_*_type_die will set it for us
12103 when appropriate. */
12104 return;
12105
12106 case VOID_TYPE:
12107 case INTEGER_TYPE:
12108 case REAL_TYPE:
12109 case COMPLEX_TYPE:
12110 case BOOLEAN_TYPE:
12111 case CHAR_TYPE:
12112 /* No DIEs needed for fundamental types. */
12113 break;
12114
12115 case LANG_TYPE:
12116 /* No Dwarf representation currently defined. */
12117 break;
12118
12119 default:
12120 gcc_unreachable ();
12121 }
12122
12123 TREE_ASM_WRITTEN (type) = 1;
12124 }
12125
12126 /* Generate a DIE for a tagged type instantiation. */
12127
12128 static void
12129 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12130 {
12131 if (type == NULL_TREE || type == error_mark_node)
12132 return;
12133
12134 /* We are going to output a DIE to represent the unqualified version of
12135 this type (i.e. without any const or volatile qualifiers) so make sure
12136 that we have the main variant (i.e. the unqualified version) of this
12137 type now. */
12138 gcc_assert (type == type_main_variant (type));
12139
12140 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12141 an instance of an unresolved type. */
12142
12143 switch (TREE_CODE (type))
12144 {
12145 case ERROR_MARK:
12146 break;
12147
12148 case ENUMERAL_TYPE:
12149 gen_inlined_enumeration_type_die (type, context_die);
12150 break;
12151
12152 case RECORD_TYPE:
12153 gen_inlined_structure_type_die (type, context_die);
12154 break;
12155
12156 case UNION_TYPE:
12157 case QUAL_UNION_TYPE:
12158 gen_inlined_union_type_die (type, context_die);
12159 break;
12160
12161 default:
12162 gcc_unreachable ();
12163 }
12164 }
12165
12166 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12167 things which are local to the given block. */
12168
12169 static void
12170 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12171 {
12172 int must_output_die = 0;
12173 tree origin;
12174 tree decl;
12175 enum tree_code origin_code;
12176
12177 /* Ignore blocks never really used to make RTL. */
12178 if (stmt == NULL_TREE || !TREE_USED (stmt)
12179 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
12180 return;
12181
12182 /* If the block is one fragment of a non-contiguous block, do not
12183 process the variables, since they will have been done by the
12184 origin block. Do process subblocks. */
12185 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12186 {
12187 tree sub;
12188
12189 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12190 gen_block_die (sub, context_die, depth + 1);
12191
12192 return;
12193 }
12194
12195 /* Determine the "ultimate origin" of this block. This block may be an
12196 inlined instance of an inlined instance of inline function, so we have
12197 to trace all of the way back through the origin chain to find out what
12198 sort of node actually served as the original seed for the creation of
12199 the current block. */
12200 origin = block_ultimate_origin (stmt);
12201 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12202
12203 /* Determine if we need to output any Dwarf DIEs at all to represent this
12204 block. */
12205 if (origin_code == FUNCTION_DECL)
12206 /* The outer scopes for inlinings *must* always be represented. We
12207 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12208 must_output_die = 1;
12209 else
12210 {
12211 /* In the case where the current block represents an inlining of the
12212 "body block" of an inline function, we must *NOT* output any DIE for
12213 this block because we have already output a DIE to represent the whole
12214 inlined function scope and the "body block" of any function doesn't
12215 really represent a different scope according to ANSI C rules. So we
12216 check here to make sure that this block does not represent a "body
12217 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12218 if (! is_body_block (origin ? origin : stmt))
12219 {
12220 /* Determine if this block directly contains any "significant"
12221 local declarations which we will need to output DIEs for. */
12222 if (debug_info_level > DINFO_LEVEL_TERSE)
12223 /* We are not in terse mode so *any* local declaration counts
12224 as being a "significant" one. */
12225 must_output_die = (BLOCK_VARS (stmt) != NULL);
12226 else
12227 /* We are in terse mode, so only local (nested) function
12228 definitions count as "significant" local declarations. */
12229 for (decl = BLOCK_VARS (stmt);
12230 decl != NULL; decl = TREE_CHAIN (decl))
12231 if (TREE_CODE (decl) == FUNCTION_DECL
12232 && DECL_INITIAL (decl))
12233 {
12234 must_output_die = 1;
12235 break;
12236 }
12237 }
12238 }
12239
12240 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12241 DIE for any block which contains no significant local declarations at
12242 all. Rather, in such cases we just call `decls_for_scope' so that any
12243 needed Dwarf info for any sub-blocks will get properly generated. Note
12244 that in terse mode, our definition of what constitutes a "significant"
12245 local declaration gets restricted to include only inlined function
12246 instances and local (nested) function definitions. */
12247 if (must_output_die)
12248 {
12249 if (origin_code == FUNCTION_DECL)
12250 gen_inlined_subroutine_die (stmt, context_die, depth);
12251 else
12252 gen_lexical_block_die (stmt, context_die, depth);
12253 }
12254 else
12255 decls_for_scope (stmt, context_die, depth);
12256 }
12257
12258 /* Generate all of the decls declared within a given scope and (recursively)
12259 all of its sub-blocks. */
12260
12261 static void
12262 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12263 {
12264 tree decl;
12265 tree subblocks;
12266
12267 /* Ignore blocks never really used to make RTL. */
12268 if (stmt == NULL_TREE || ! TREE_USED (stmt))
12269 return;
12270
12271 /* Output the DIEs to represent all of the data objects and typedefs
12272 declared directly within this block but not within any nested
12273 sub-blocks. Also, nested function and tag DIEs have been
12274 generated with a parent of NULL; fix that up now. */
12275 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12276 {
12277 dw_die_ref die;
12278
12279 if (TREE_CODE (decl) == FUNCTION_DECL)
12280 die = lookup_decl_die (decl);
12281 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12282 die = lookup_type_die (TREE_TYPE (decl));
12283 else
12284 die = NULL;
12285
12286 if (die != NULL && die->die_parent == NULL)
12287 add_child_die (context_die, die);
12288 else
12289 gen_decl_die (decl, context_die);
12290 }
12291
12292 /* If we're at -g1, we're not interested in subblocks. */
12293 if (debug_info_level <= DINFO_LEVEL_TERSE)
12294 return;
12295
12296 /* Output the DIEs to represent all sub-blocks (and the items declared
12297 therein) of this block. */
12298 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12299 subblocks != NULL;
12300 subblocks = BLOCK_CHAIN (subblocks))
12301 gen_block_die (subblocks, context_die, depth + 1);
12302 }
12303
12304 /* Is this a typedef we can avoid emitting? */
12305
12306 static inline int
12307 is_redundant_typedef (tree decl)
12308 {
12309 if (TYPE_DECL_IS_STUB (decl))
12310 return 1;
12311
12312 if (DECL_ARTIFICIAL (decl)
12313 && DECL_CONTEXT (decl)
12314 && is_tagged_type (DECL_CONTEXT (decl))
12315 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12316 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12317 /* Also ignore the artificial member typedef for the class name. */
12318 return 1;
12319
12320 return 0;
12321 }
12322
12323 /* Returns the DIE for decl or aborts. */
12324
12325 static dw_die_ref
12326 force_decl_die (tree decl)
12327 {
12328 dw_die_ref decl_die;
12329 unsigned saved_external_flag;
12330 tree save_fn = NULL_TREE;
12331 decl_die = lookup_decl_die (decl);
12332 if (!decl_die)
12333 {
12334 dw_die_ref context_die;
12335 tree decl_context = DECL_CONTEXT (decl);
12336 if (decl_context)
12337 {
12338 /* Find die that represents this context. */
12339 if (TYPE_P (decl_context))
12340 context_die = force_type_die (decl_context);
12341 else
12342 context_die = force_decl_die (decl_context);
12343 }
12344 else
12345 context_die = comp_unit_die;
12346
12347 switch (TREE_CODE (decl))
12348 {
12349 case FUNCTION_DECL:
12350 /* Clear current_function_decl, so that gen_subprogram_die thinks
12351 that this is a declaration. At this point, we just want to force
12352 declaration die. */
12353 save_fn = current_function_decl;
12354 current_function_decl = NULL_TREE;
12355 gen_subprogram_die (decl, context_die);
12356 current_function_decl = save_fn;
12357 break;
12358
12359 case VAR_DECL:
12360 /* Set external flag to force declaration die. Restore it after
12361 gen_decl_die() call. */
12362 saved_external_flag = DECL_EXTERNAL (decl);
12363 DECL_EXTERNAL (decl) = 1;
12364 gen_decl_die (decl, context_die);
12365 DECL_EXTERNAL (decl) = saved_external_flag;
12366 break;
12367
12368 case NAMESPACE_DECL:
12369 dwarf2out_decl (decl);
12370 break;
12371
12372 default:
12373 gcc_unreachable ();
12374 }
12375
12376 /* See if we can find the die for this deci now.
12377 If not then abort. */
12378 if (!decl_die)
12379 decl_die = lookup_decl_die (decl);
12380 gcc_assert (decl_die);
12381 }
12382
12383 return decl_die;
12384 }
12385
12386 /* Returns the DIE for decl or aborts. */
12387
12388 static dw_die_ref
12389 force_type_die (tree type)
12390 {
12391 dw_die_ref type_die;
12392
12393 type_die = lookup_type_die (type);
12394 if (!type_die)
12395 {
12396 dw_die_ref context_die;
12397 if (TYPE_CONTEXT (type))
12398 if (TYPE_P (TYPE_CONTEXT (type)))
12399 context_die = force_type_die (TYPE_CONTEXT (type));
12400 else
12401 context_die = force_decl_die (TYPE_CONTEXT (type));
12402 else
12403 context_die = comp_unit_die;
12404
12405 gen_type_die (type, context_die);
12406 type_die = lookup_type_die (type);
12407 gcc_assert (type_die);
12408 }
12409 return type_die;
12410 }
12411
12412 /* Force out any required namespaces to be able to output DECL,
12413 and return the new context_die for it, if it's changed. */
12414
12415 static dw_die_ref
12416 setup_namespace_context (tree thing, dw_die_ref context_die)
12417 {
12418 tree context = DECL_P (thing) ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing);
12419 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12420 /* Force out the namespace. */
12421 context_die = force_decl_die (context);
12422
12423 return context_die;
12424 }
12425
12426 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12427 type) within its namespace, if appropriate.
12428
12429 For compatibility with older debuggers, namespace DIEs only contain
12430 declarations; all definitions are emitted at CU scope. */
12431
12432 static void
12433 declare_in_namespace (tree thing, dw_die_ref context_die)
12434 {
12435 dw_die_ref ns_context;
12436
12437 if (debug_info_level <= DINFO_LEVEL_TERSE)
12438 return;
12439
12440 ns_context = setup_namespace_context (thing, context_die);
12441
12442 if (ns_context != context_die)
12443 {
12444 if (DECL_P (thing))
12445 gen_decl_die (thing, ns_context);
12446 else
12447 gen_type_die (thing, ns_context);
12448 }
12449 }
12450
12451 /* Generate a DIE for a namespace or namespace alias. */
12452
12453 static void
12454 gen_namespace_die (tree decl)
12455 {
12456 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12457
12458 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12459 they are an alias of. */
12460 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12461 {
12462 /* Output a real namespace. */
12463 dw_die_ref namespace_die
12464 = new_die (DW_TAG_namespace, context_die, decl);
12465 add_name_and_src_coords_attributes (namespace_die, decl);
12466 equate_decl_number_to_die (decl, namespace_die);
12467 }
12468 else
12469 {
12470 /* Output a namespace alias. */
12471
12472 /* Force out the namespace we are an alias of, if necessary. */
12473 dw_die_ref origin_die
12474 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12475
12476 /* Now create the namespace alias DIE. */
12477 dw_die_ref namespace_die
12478 = new_die (DW_TAG_imported_declaration, context_die, decl);
12479 add_name_and_src_coords_attributes (namespace_die, decl);
12480 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12481 equate_decl_number_to_die (decl, namespace_die);
12482 }
12483 }
12484
12485 /* Generate Dwarf debug information for a decl described by DECL. */
12486
12487 static void
12488 gen_decl_die (tree decl, dw_die_ref context_die)
12489 {
12490 tree origin;
12491
12492 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12493 return;
12494
12495 switch (TREE_CODE (decl))
12496 {
12497 case ERROR_MARK:
12498 break;
12499
12500 case CONST_DECL:
12501 /* The individual enumerators of an enum type get output when we output
12502 the Dwarf representation of the relevant enum type itself. */
12503 break;
12504
12505 case FUNCTION_DECL:
12506 /* Don't output any DIEs to represent mere function declarations,
12507 unless they are class members or explicit block externs. */
12508 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12509 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12510 break;
12511
12512 #if 0
12513 /* FIXME */
12514 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12515 on local redeclarations of global functions. That seems broken. */
12516 if (current_function_decl != decl)
12517 /* This is only a declaration. */;
12518 #endif
12519
12520 /* If we're emitting a clone, emit info for the abstract instance. */
12521 if (DECL_ORIGIN (decl) != decl)
12522 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12523
12524 /* If we're emitting an out-of-line copy of an inline function,
12525 emit info for the abstract instance and set up to refer to it. */
12526 else if (cgraph_function_possibly_inlined_p (decl)
12527 && ! DECL_ABSTRACT (decl)
12528 && ! class_or_namespace_scope_p (context_die)
12529 /* dwarf2out_abstract_function won't emit a die if this is just
12530 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12531 that case, because that works only if we have a die. */
12532 && DECL_INITIAL (decl) != NULL_TREE)
12533 {
12534 dwarf2out_abstract_function (decl);
12535 set_decl_origin_self (decl);
12536 }
12537
12538 /* Otherwise we're emitting the primary DIE for this decl. */
12539 else if (debug_info_level > DINFO_LEVEL_TERSE)
12540 {
12541 /* Before we describe the FUNCTION_DECL itself, make sure that we
12542 have described its return type. */
12543 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12544
12545 /* And its virtual context. */
12546 if (DECL_VINDEX (decl) != NULL_TREE)
12547 gen_type_die (DECL_CONTEXT (decl), context_die);
12548
12549 /* And its containing type. */
12550 origin = decl_class_context (decl);
12551 if (origin != NULL_TREE)
12552 gen_type_die_for_member (origin, decl, context_die);
12553
12554 /* And its containing namespace. */
12555 declare_in_namespace (decl, context_die);
12556 }
12557
12558 /* Now output a DIE to represent the function itself. */
12559 gen_subprogram_die (decl, context_die);
12560 break;
12561
12562 case TYPE_DECL:
12563 /* If we are in terse mode, don't generate any DIEs to represent any
12564 actual typedefs. */
12565 if (debug_info_level <= DINFO_LEVEL_TERSE)
12566 break;
12567
12568 /* In the special case of a TYPE_DECL node representing the declaration
12569 of some type tag, if the given TYPE_DECL is marked as having been
12570 instantiated from some other (original) TYPE_DECL node (e.g. one which
12571 was generated within the original definition of an inline function) we
12572 have to generate a special (abbreviated) DW_TAG_structure_type,
12573 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12574 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12575 {
12576 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12577 break;
12578 }
12579
12580 if (is_redundant_typedef (decl))
12581 gen_type_die (TREE_TYPE (decl), context_die);
12582 else
12583 /* Output a DIE to represent the typedef itself. */
12584 gen_typedef_die (decl, context_die);
12585 break;
12586
12587 case LABEL_DECL:
12588 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12589 gen_label_die (decl, context_die);
12590 break;
12591
12592 case VAR_DECL:
12593 case RESULT_DECL:
12594 /* If we are in terse mode, don't generate any DIEs to represent any
12595 variable declarations or definitions. */
12596 if (debug_info_level <= DINFO_LEVEL_TERSE)
12597 break;
12598
12599 /* Output any DIEs that are needed to specify the type of this data
12600 object. */
12601 gen_type_die (TREE_TYPE (decl), context_die);
12602
12603 /* And its containing type. */
12604 origin = decl_class_context (decl);
12605 if (origin != NULL_TREE)
12606 gen_type_die_for_member (origin, decl, context_die);
12607
12608 /* And its containing namespace. */
12609 declare_in_namespace (decl, context_die);
12610
12611 /* Now output the DIE to represent the data object itself. This gets
12612 complicated because of the possibility that the VAR_DECL really
12613 represents an inlined instance of a formal parameter for an inline
12614 function. */
12615 origin = decl_ultimate_origin (decl);
12616 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12617 gen_formal_parameter_die (decl, context_die);
12618 else
12619 gen_variable_die (decl, context_die);
12620 break;
12621
12622 case FIELD_DECL:
12623 /* Ignore the nameless fields that are used to skip bits but handle C++
12624 anonymous unions and structs. */
12625 if (DECL_NAME (decl) != NULL_TREE
12626 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12627 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12628 {
12629 gen_type_die (member_declared_type (decl), context_die);
12630 gen_field_die (decl, context_die);
12631 }
12632 break;
12633
12634 case PARM_DECL:
12635 gen_type_die (TREE_TYPE (decl), context_die);
12636 gen_formal_parameter_die (decl, context_die);
12637 break;
12638
12639 case NAMESPACE_DECL:
12640 gen_namespace_die (decl);
12641 break;
12642
12643 default:
12644 /* Probably some frontend-internal decl. Assume we don't care. */
12645 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
12646 break;
12647 }
12648 }
12649 \f
12650 /* Add Ada "use" clause information for SGI Workshop debugger. */
12651
12652 void
12653 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12654 {
12655 unsigned int file_index;
12656
12657 if (filename != NULL)
12658 {
12659 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12660 tree context_list_decl
12661 = build_decl (LABEL_DECL, get_identifier (context_list),
12662 void_type_node);
12663
12664 TREE_PUBLIC (context_list_decl) = TRUE;
12665 add_name_attribute (unit_die, context_list);
12666 file_index = lookup_filename (filename);
12667 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12668 add_pubname (context_list_decl, unit_die);
12669 }
12670 }
12671
12672 /* Output debug information for global decl DECL. Called from toplev.c after
12673 compilation proper has finished. */
12674
12675 static void
12676 dwarf2out_global_decl (tree decl)
12677 {
12678 /* Output DWARF2 information for file-scope tentative data object
12679 declarations, file-scope (extern) function declarations (which had no
12680 corresponding body) and file-scope tagged type declarations and
12681 definitions which have not yet been forced out. */
12682 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12683 dwarf2out_decl (decl);
12684 }
12685
12686 /* Output debug information for type decl DECL. Called from toplev.c
12687 and from language front ends (to record built-in types). */
12688 static void
12689 dwarf2out_type_decl (tree decl, int local)
12690 {
12691 if (!local)
12692 dwarf2out_decl (decl);
12693 }
12694
12695 /* Output debug information for imported module or decl. */
12696
12697 static void
12698 dwarf2out_imported_module_or_decl (tree decl, tree context)
12699 {
12700 dw_die_ref imported_die, at_import_die;
12701 dw_die_ref scope_die;
12702 unsigned file_index;
12703 expanded_location xloc;
12704
12705 if (debug_info_level <= DINFO_LEVEL_TERSE)
12706 return;
12707
12708 gcc_assert (decl);
12709
12710 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12711 We need decl DIE for reference and scope die. First, get DIE for the decl
12712 itself. */
12713
12714 /* Get the scope die for decl context. Use comp_unit_die for global module
12715 or decl. If die is not found for non globals, force new die. */
12716 if (!context)
12717 scope_die = comp_unit_die;
12718 else if (TYPE_P (context))
12719 scope_die = force_type_die (context);
12720 else
12721 scope_die = force_decl_die (context);
12722
12723 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12724 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12725 at_import_die = force_type_die (TREE_TYPE (decl));
12726 else
12727 at_import_die = force_decl_die (decl);
12728
12729 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12730 if (TREE_CODE (decl) == NAMESPACE_DECL)
12731 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12732 else
12733 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12734
12735 xloc = expand_location (input_location);
12736 file_index = lookup_filename (xloc.file);
12737 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12738 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
12739 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12740 }
12741
12742 /* Write the debugging output for DECL. */
12743
12744 void
12745 dwarf2out_decl (tree decl)
12746 {
12747 dw_die_ref context_die = comp_unit_die;
12748
12749 switch (TREE_CODE (decl))
12750 {
12751 case ERROR_MARK:
12752 return;
12753
12754 case FUNCTION_DECL:
12755 /* What we would really like to do here is to filter out all mere
12756 file-scope declarations of file-scope functions which are never
12757 referenced later within this translation unit (and keep all of ones
12758 that *are* referenced later on) but we aren't clairvoyant, so we have
12759 no idea which functions will be referenced in the future (i.e. later
12760 on within the current translation unit). So here we just ignore all
12761 file-scope function declarations which are not also definitions. If
12762 and when the debugger needs to know something about these functions,
12763 it will have to hunt around and find the DWARF information associated
12764 with the definition of the function.
12765
12766 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
12767 nodes represent definitions and which ones represent mere
12768 declarations. We have to check DECL_INITIAL instead. That's because
12769 the C front-end supports some weird semantics for "extern inline"
12770 function definitions. These can get inlined within the current
12771 translation unit (an thus, we need to generate Dwarf info for their
12772 abstract instances so that the Dwarf info for the concrete inlined
12773 instances can have something to refer to) but the compiler never
12774 generates any out-of-lines instances of such things (despite the fact
12775 that they *are* definitions).
12776
12777 The important point is that the C front-end marks these "extern
12778 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12779 them anyway. Note that the C++ front-end also plays some similar games
12780 for inline function definitions appearing within include files which
12781 also contain `#pragma interface' pragmas. */
12782 if (DECL_INITIAL (decl) == NULL_TREE)
12783 return;
12784
12785 /* If we're a nested function, initially use a parent of NULL; if we're
12786 a plain function, this will be fixed up in decls_for_scope. If
12787 we're a method, it will be ignored, since we already have a DIE. */
12788 if (decl_function_context (decl)
12789 /* But if we're in terse mode, we don't care about scope. */
12790 && debug_info_level > DINFO_LEVEL_TERSE)
12791 context_die = NULL;
12792 break;
12793
12794 case VAR_DECL:
12795 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
12796 declaration and if the declaration was never even referenced from
12797 within this entire compilation unit. We suppress these DIEs in
12798 order to save space in the .debug section (by eliminating entries
12799 which are probably useless). Note that we must not suppress
12800 block-local extern declarations (whether used or not) because that
12801 would screw-up the debugger's name lookup mechanism and cause it to
12802 miss things which really ought to be in scope at a given point. */
12803 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
12804 return;
12805
12806 /* If we are in terse mode, don't generate any DIEs to represent any
12807 variable declarations or definitions. */
12808 if (debug_info_level <= DINFO_LEVEL_TERSE)
12809 return;
12810 break;
12811
12812 case NAMESPACE_DECL:
12813 if (debug_info_level <= DINFO_LEVEL_TERSE)
12814 return;
12815 if (lookup_decl_die (decl) != NULL)
12816 return;
12817 break;
12818
12819 case TYPE_DECL:
12820 /* Don't emit stubs for types unless they are needed by other DIEs. */
12821 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12822 return;
12823
12824 /* Don't bother trying to generate any DIEs to represent any of the
12825 normal built-in types for the language we are compiling. */
12826 if (DECL_IS_BUILTIN (decl))
12827 {
12828 /* OK, we need to generate one for `bool' so GDB knows what type
12829 comparisons have. */
12830 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12831 == DW_LANG_C_plus_plus)
12832 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12833 && ! DECL_IGNORED_P (decl))
12834 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
12835
12836 return;
12837 }
12838
12839 /* If we are in terse mode, don't generate any DIEs for types. */
12840 if (debug_info_level <= DINFO_LEVEL_TERSE)
12841 return;
12842
12843 /* If we're a function-scope tag, initially use a parent of NULL;
12844 this will be fixed up in decls_for_scope. */
12845 if (decl_function_context (decl))
12846 context_die = NULL;
12847
12848 break;
12849
12850 default:
12851 return;
12852 }
12853
12854 gen_decl_die (decl, context_die);
12855 }
12856
12857 /* Output a marker (i.e. a label) for the beginning of the generated code for
12858 a lexical block. */
12859
12860 static void
12861 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
12862 unsigned int blocknum)
12863 {
12864 function_section (current_function_decl);
12865 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
12866 }
12867
12868 /* Output a marker (i.e. a label) for the end of the generated code for a
12869 lexical block. */
12870
12871 static void
12872 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
12873 {
12874 function_section (current_function_decl);
12875 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
12876 }
12877
12878 /* Returns nonzero if it is appropriate not to emit any debugging
12879 information for BLOCK, because it doesn't contain any instructions.
12880
12881 Don't allow this for blocks with nested functions or local classes
12882 as we would end up with orphans, and in the presence of scheduling
12883 we may end up calling them anyway. */
12884
12885 static bool
12886 dwarf2out_ignore_block (tree block)
12887 {
12888 tree decl;
12889
12890 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
12891 if (TREE_CODE (decl) == FUNCTION_DECL
12892 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
12893 return 0;
12894
12895 return 1;
12896 }
12897
12898 /* Lookup FILE_NAME (in the list of filenames that we know about here in
12899 dwarf2out.c) and return its "index". The index of each (known) filename is
12900 just a unique number which is associated with only that one filename. We
12901 need such numbers for the sake of generating labels (in the .debug_sfnames
12902 section) and references to those files numbers (in the .debug_srcinfo
12903 and.debug_macinfo sections). If the filename given as an argument is not
12904 found in our current list, add it to the list and assign it the next
12905 available unique index number. In order to speed up searches, we remember
12906 the index of the filename was looked up last. This handles the majority of
12907 all searches. */
12908
12909 static unsigned
12910 lookup_filename (const char *file_name)
12911 {
12912 size_t i, n;
12913 char *save_file_name;
12914
12915 /* Check to see if the file name that was searched on the previous
12916 call matches this file name. If so, return the index. */
12917 if (file_table_last_lookup_index != 0)
12918 {
12919 const char *last
12920 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
12921 if (strcmp (file_name, last) == 0)
12922 return file_table_last_lookup_index;
12923 }
12924
12925 /* Didn't match the previous lookup, search the table */
12926 n = VARRAY_ACTIVE_SIZE (file_table);
12927 for (i = 1; i < n; i++)
12928 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
12929 {
12930 file_table_last_lookup_index = i;
12931 return i;
12932 }
12933
12934 /* Add the new entry to the end of the filename table. */
12935 file_table_last_lookup_index = n;
12936 save_file_name = (char *) ggc_strdup (file_name);
12937 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
12938 VARRAY_PUSH_UINT (file_table_emitted, 0);
12939
12940 return i;
12941 }
12942
12943 static int
12944 maybe_emit_file (int fileno)
12945 {
12946 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
12947 {
12948 if (!VARRAY_UINT (file_table_emitted, fileno))
12949 {
12950 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
12951 fprintf (asm_out_file, "\t.file %u ",
12952 VARRAY_UINT (file_table_emitted, fileno));
12953 output_quoted_string (asm_out_file,
12954 VARRAY_CHAR_PTR (file_table, fileno));
12955 fputc ('\n', asm_out_file);
12956 }
12957 return VARRAY_UINT (file_table_emitted, fileno);
12958 }
12959 else
12960 return fileno;
12961 }
12962
12963 static void
12964 init_file_table (void)
12965 {
12966 /* Allocate the initial hunk of the file_table. */
12967 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
12968 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
12969
12970 /* Skip the first entry - file numbers begin at 1. */
12971 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
12972 VARRAY_PUSH_UINT (file_table_emitted, 0);
12973 file_table_last_lookup_index = 0;
12974 }
12975
12976 /* Called by the final INSN scan whenever we see a var location. We
12977 use it to drop labels in the right places, and throw the location in
12978 our lookup table. */
12979
12980 static void
12981 dwarf2out_var_location (rtx loc_note)
12982 {
12983 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
12984 struct var_loc_node *newloc;
12985 rtx prev_insn;
12986 static rtx last_insn;
12987 static const char *last_label;
12988
12989 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
12990 return;
12991 prev_insn = PREV_INSN (loc_note);
12992
12993 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
12994 /* If the insn we processed last time is the previous insn
12995 and it is also a var location note, use the label we emitted
12996 last time. */
12997 if (last_insn != NULL_RTX
12998 && last_insn == prev_insn
12999 && NOTE_P (prev_insn)
13000 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13001 {
13002 newloc->label = last_label;
13003 }
13004 else
13005 {
13006 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13007 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13008 loclabel_num++;
13009 newloc->label = ggc_strdup (loclabel);
13010 }
13011 newloc->var_loc_note = loc_note;
13012 newloc->next = NULL;
13013
13014 last_insn = loc_note;
13015 last_label = newloc->label;
13016
13017 add_var_loc_to_decl (NOTE_VAR_LOCATION_DECL (loc_note), newloc);
13018 }
13019
13020 /* We need to reset the locations at the beginning of each
13021 function. We can't do this in the end_function hook, because the
13022 declarations that use the locations won't have been outputted when
13023 that hook is called. */
13024
13025 static void
13026 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13027 {
13028 htab_empty (decl_loc_table);
13029 }
13030
13031 /* Output a label to mark the beginning of a source code line entry
13032 and record information relating to this source line, in
13033 'line_info_table' for later output of the .debug_line section. */
13034
13035 static void
13036 dwarf2out_source_line (unsigned int line, const char *filename)
13037 {
13038 if (debug_info_level >= DINFO_LEVEL_NORMAL
13039 && line != 0)
13040 {
13041 function_section (current_function_decl);
13042
13043 /* If requested, emit something human-readable. */
13044 if (flag_debug_asm)
13045 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13046 filename, line);
13047
13048 if (DWARF2_ASM_LINE_DEBUG_INFO)
13049 {
13050 unsigned file_num = lookup_filename (filename);
13051
13052 file_num = maybe_emit_file (file_num);
13053
13054 /* Emit the .loc directive understood by GNU as. */
13055 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13056
13057 /* Indicate that line number info exists. */
13058 line_info_table_in_use++;
13059
13060 /* Indicate that multiple line number tables exist. */
13061 if (DECL_SECTION_NAME (current_function_decl))
13062 separate_line_info_table_in_use++;
13063 }
13064 else if (DECL_SECTION_NAME (current_function_decl))
13065 {
13066 dw_separate_line_info_ref line_info;
13067 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13068 separate_line_info_table_in_use);
13069
13070 /* Expand the line info table if necessary. */
13071 if (separate_line_info_table_in_use
13072 == separate_line_info_table_allocated)
13073 {
13074 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13075 separate_line_info_table
13076 = ggc_realloc (separate_line_info_table,
13077 separate_line_info_table_allocated
13078 * sizeof (dw_separate_line_info_entry));
13079 memset (separate_line_info_table
13080 + separate_line_info_table_in_use,
13081 0,
13082 (LINE_INFO_TABLE_INCREMENT
13083 * sizeof (dw_separate_line_info_entry)));
13084 }
13085
13086 /* Add the new entry at the end of the line_info_table. */
13087 line_info
13088 = &separate_line_info_table[separate_line_info_table_in_use++];
13089 line_info->dw_file_num = lookup_filename (filename);
13090 line_info->dw_line_num = line;
13091 line_info->function = current_function_funcdef_no;
13092 }
13093 else
13094 {
13095 dw_line_info_ref line_info;
13096
13097 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13098 line_info_table_in_use);
13099
13100 /* Expand the line info table if necessary. */
13101 if (line_info_table_in_use == line_info_table_allocated)
13102 {
13103 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13104 line_info_table
13105 = ggc_realloc (line_info_table,
13106 (line_info_table_allocated
13107 * sizeof (dw_line_info_entry)));
13108 memset (line_info_table + line_info_table_in_use, 0,
13109 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13110 }
13111
13112 /* Add the new entry at the end of the line_info_table. */
13113 line_info = &line_info_table[line_info_table_in_use++];
13114 line_info->dw_file_num = lookup_filename (filename);
13115 line_info->dw_line_num = line;
13116 }
13117 }
13118 }
13119
13120 /* Record the beginning of a new source file. */
13121
13122 static void
13123 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13124 {
13125 if (flag_eliminate_dwarf2_dups)
13126 {
13127 /* Record the beginning of the file for break_out_includes. */
13128 dw_die_ref bincl_die;
13129
13130 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13131 add_AT_string (bincl_die, DW_AT_name, filename);
13132 }
13133
13134 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13135 {
13136 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13137 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13138 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13139 lineno);
13140 maybe_emit_file (lookup_filename (filename));
13141 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13142 "Filename we just started");
13143 }
13144 }
13145
13146 /* Record the end of a source file. */
13147
13148 static void
13149 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13150 {
13151 if (flag_eliminate_dwarf2_dups)
13152 /* Record the end of the file for break_out_includes. */
13153 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13154
13155 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13156 {
13157 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13158 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13159 }
13160 }
13161
13162 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13163 the tail part of the directive line, i.e. the part which is past the
13164 initial whitespace, #, whitespace, directive-name, whitespace part. */
13165
13166 static void
13167 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13168 const char *buffer ATTRIBUTE_UNUSED)
13169 {
13170 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13171 {
13172 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13173 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13174 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13175 dw2_asm_output_nstring (buffer, -1, "The macro");
13176 }
13177 }
13178
13179 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13180 the tail part of the directive line, i.e. the part which is past the
13181 initial whitespace, #, whitespace, directive-name, whitespace part. */
13182
13183 static void
13184 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13185 const char *buffer ATTRIBUTE_UNUSED)
13186 {
13187 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13188 {
13189 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13190 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13191 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13192 dw2_asm_output_nstring (buffer, -1, "The macro");
13193 }
13194 }
13195
13196 /* Set up for Dwarf output at the start of compilation. */
13197
13198 static void
13199 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13200 {
13201 init_file_table ();
13202
13203 /* Allocate the decl_die_table. */
13204 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13205 decl_die_table_eq, NULL);
13206
13207 /* Allocate the decl_loc_table. */
13208 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13209 decl_loc_table_eq, NULL);
13210
13211 /* Allocate the initial hunk of the decl_scope_table. */
13212 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
13213
13214 /* Allocate the initial hunk of the abbrev_die_table. */
13215 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13216 * sizeof (dw_die_ref));
13217 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13218 /* Zero-th entry is allocated, but unused */
13219 abbrev_die_table_in_use = 1;
13220
13221 /* Allocate the initial hunk of the line_info_table. */
13222 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13223 * sizeof (dw_line_info_entry));
13224 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13225
13226 /* Zero-th entry is allocated, but unused */
13227 line_info_table_in_use = 1;
13228
13229 /* Generate the initial DIE for the .debug section. Note that the (string)
13230 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13231 will (typically) be a relative pathname and that this pathname should be
13232 taken as being relative to the directory from which the compiler was
13233 invoked when the given (base) source file was compiled. We will fill
13234 in this value in dwarf2out_finish. */
13235 comp_unit_die = gen_compile_unit_die (NULL);
13236
13237 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
13238
13239 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
13240
13241 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13242 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13243 DEBUG_ABBREV_SECTION_LABEL, 0);
13244 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13245 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13246 else
13247 strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
13248
13249 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13250 DEBUG_INFO_SECTION_LABEL, 0);
13251 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13252 DEBUG_LINE_SECTION_LABEL, 0);
13253 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13254 DEBUG_RANGES_SECTION_LABEL, 0);
13255 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13256 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13257 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13258 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13259 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13260 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13261
13262 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13263 {
13264 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13265 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13266 DEBUG_MACINFO_SECTION_LABEL, 0);
13267 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13268 }
13269
13270 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13271 {
13272 text_section ();
13273 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13274 }
13275 }
13276
13277 /* A helper function for dwarf2out_finish called through
13278 ht_forall. Emit one queued .debug_str string. */
13279
13280 static int
13281 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13282 {
13283 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13284
13285 if (node->form == DW_FORM_strp)
13286 {
13287 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13288 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13289 assemble_string (node->str, strlen (node->str) + 1);
13290 }
13291
13292 return 1;
13293 }
13294
13295
13296
13297 /* Clear the marks for a die and its children.
13298 Be cool if the mark isn't set. */
13299
13300 static void
13301 prune_unmark_dies (dw_die_ref die)
13302 {
13303 dw_die_ref c;
13304 die->die_mark = 0;
13305 for (c = die->die_child; c; c = c->die_sib)
13306 prune_unmark_dies (c);
13307 }
13308
13309
13310 /* Given DIE that we're marking as used, find any other dies
13311 it references as attributes and mark them as used. */
13312
13313 static void
13314 prune_unused_types_walk_attribs (dw_die_ref die)
13315 {
13316 dw_attr_ref a;
13317
13318 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13319 {
13320 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13321 {
13322 /* A reference to another DIE.
13323 Make sure that it will get emitted. */
13324 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13325 }
13326 else if (a->dw_attr == DW_AT_decl_file)
13327 {
13328 /* A reference to a file. Make sure the file name is emitted. */
13329 a->dw_attr_val.v.val_unsigned =
13330 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13331 }
13332 }
13333 }
13334
13335
13336 /* Mark DIE as being used. If DOKIDS is true, then walk down
13337 to DIE's children. */
13338
13339 static void
13340 prune_unused_types_mark (dw_die_ref die, int dokids)
13341 {
13342 dw_die_ref c;
13343
13344 if (die->die_mark == 0)
13345 {
13346 /* We haven't done this node yet. Mark it as used. */
13347 die->die_mark = 1;
13348
13349 /* We also have to mark its parents as used.
13350 (But we don't want to mark our parents' kids due to this.) */
13351 if (die->die_parent)
13352 prune_unused_types_mark (die->die_parent, 0);
13353
13354 /* Mark any referenced nodes. */
13355 prune_unused_types_walk_attribs (die);
13356
13357 /* If this node is a specification,
13358 also mark the definition, if it exists. */
13359 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13360 prune_unused_types_mark (die->die_definition, 1);
13361 }
13362
13363 if (dokids && die->die_mark != 2)
13364 {
13365 /* We need to walk the children, but haven't done so yet.
13366 Remember that we've walked the kids. */
13367 die->die_mark = 2;
13368
13369 /* Walk them. */
13370 for (c = die->die_child; c; c = c->die_sib)
13371 {
13372 /* If this is an array type, we need to make sure our
13373 kids get marked, even if they're types. */
13374 if (die->die_tag == DW_TAG_array_type)
13375 prune_unused_types_mark (c, 1);
13376 else
13377 prune_unused_types_walk (c);
13378 }
13379 }
13380 }
13381
13382
13383 /* Walk the tree DIE and mark types that we actually use. */
13384
13385 static void
13386 prune_unused_types_walk (dw_die_ref die)
13387 {
13388 dw_die_ref c;
13389
13390 /* Don't do anything if this node is already marked. */
13391 if (die->die_mark)
13392 return;
13393
13394 switch (die->die_tag) {
13395 case DW_TAG_const_type:
13396 case DW_TAG_packed_type:
13397 case DW_TAG_pointer_type:
13398 case DW_TAG_reference_type:
13399 case DW_TAG_volatile_type:
13400 case DW_TAG_typedef:
13401 case DW_TAG_array_type:
13402 case DW_TAG_structure_type:
13403 case DW_TAG_union_type:
13404 case DW_TAG_class_type:
13405 case DW_TAG_friend:
13406 case DW_TAG_variant_part:
13407 case DW_TAG_enumeration_type:
13408 case DW_TAG_subroutine_type:
13409 case DW_TAG_string_type:
13410 case DW_TAG_set_type:
13411 case DW_TAG_subrange_type:
13412 case DW_TAG_ptr_to_member_type:
13413 case DW_TAG_file_type:
13414 /* It's a type node --- don't mark it. */
13415 return;
13416
13417 default:
13418 /* Mark everything else. */
13419 break;
13420 }
13421
13422 die->die_mark = 1;
13423
13424 /* Now, mark any dies referenced from here. */
13425 prune_unused_types_walk_attribs (die);
13426
13427 /* Mark children. */
13428 for (c = die->die_child; c; c = c->die_sib)
13429 prune_unused_types_walk (c);
13430 }
13431
13432
13433 /* Remove from the tree DIE any dies that aren't marked. */
13434
13435 static void
13436 prune_unused_types_prune (dw_die_ref die)
13437 {
13438 dw_die_ref c, p, n;
13439
13440 gcc_assert (die->die_mark);
13441
13442 p = NULL;
13443 for (c = die->die_child; c; c = n)
13444 {
13445 n = c->die_sib;
13446 if (c->die_mark)
13447 {
13448 prune_unused_types_prune (c);
13449 p = c;
13450 }
13451 else
13452 {
13453 if (p)
13454 p->die_sib = n;
13455 else
13456 die->die_child = n;
13457 free_die (c);
13458 }
13459 }
13460 }
13461
13462
13463 /* Remove dies representing declarations that we never use. */
13464
13465 static void
13466 prune_unused_types (void)
13467 {
13468 unsigned int i;
13469 limbo_die_node *node;
13470
13471 /* Clear all the marks. */
13472 prune_unmark_dies (comp_unit_die);
13473 for (node = limbo_die_list; node; node = node->next)
13474 prune_unmark_dies (node->die);
13475
13476 /* Set the mark on nodes that are actually used. */
13477 prune_unused_types_walk (comp_unit_die);
13478 for (node = limbo_die_list; node; node = node->next)
13479 prune_unused_types_walk (node->die);
13480
13481 /* Also set the mark on nodes referenced from the
13482 pubname_table or arange_table. */
13483 for (i = 0; i < pubname_table_in_use; i++)
13484 prune_unused_types_mark (pubname_table[i].die, 1);
13485 for (i = 0; i < arange_table_in_use; i++)
13486 prune_unused_types_mark (arange_table[i], 1);
13487
13488 /* Get rid of nodes that aren't marked. */
13489 prune_unused_types_prune (comp_unit_die);
13490 for (node = limbo_die_list; node; node = node->next)
13491 prune_unused_types_prune (node->die);
13492
13493 /* Leave the marks clear. */
13494 prune_unmark_dies (comp_unit_die);
13495 for (node = limbo_die_list; node; node = node->next)
13496 prune_unmark_dies (node->die);
13497 }
13498
13499 /* Output stuff that dwarf requires at the end of every file,
13500 and generate the DWARF-2 debugging info. */
13501
13502 static void
13503 dwarf2out_finish (const char *filename)
13504 {
13505 limbo_die_node *node, *next_node;
13506 dw_die_ref die = 0;
13507
13508 /* Add the name for the main input file now. We delayed this from
13509 dwarf2out_init to avoid complications with PCH. */
13510 add_name_attribute (comp_unit_die, filename);
13511 if (filename[0] != DIR_SEPARATOR)
13512 add_comp_dir_attribute (comp_unit_die);
13513 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13514 {
13515 size_t i;
13516 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13517 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13518 /* Don't add cwd for <built-in>. */
13519 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13520 {
13521 add_comp_dir_attribute (comp_unit_die);
13522 break;
13523 }
13524 }
13525
13526 /* Traverse the limbo die list, and add parent/child links. The only
13527 dies without parents that should be here are concrete instances of
13528 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13529 For concrete instances, we can get the parent die from the abstract
13530 instance. */
13531 for (node = limbo_die_list; node; node = next_node)
13532 {
13533 next_node = node->next;
13534 die = node->die;
13535
13536 if (die->die_parent == NULL)
13537 {
13538 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13539
13540 if (origin)
13541 add_child_die (origin->die_parent, die);
13542 else if (die == comp_unit_die)
13543 ;
13544 else if (errorcount > 0 || sorrycount > 0)
13545 /* It's OK to be confused by errors in the input. */
13546 add_child_die (comp_unit_die, die);
13547 else
13548 {
13549 /* In certain situations, the lexical block containing a
13550 nested function can be optimized away, which results
13551 in the nested function die being orphaned. Likewise
13552 with the return type of that nested function. Force
13553 this to be a child of the containing function. */
13554 tree context = NULL_TREE;
13555
13556 gcc_assert (node->created_for);
13557
13558 if (DECL_P (node->created_for))
13559 context = DECL_CONTEXT (node->created_for);
13560 else if (TYPE_P (node->created_for))
13561 context = TYPE_CONTEXT (node->created_for);
13562
13563 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
13564
13565 origin = lookup_decl_die (context);
13566 gcc_assert (origin);
13567 add_child_die (origin, die);
13568 }
13569 }
13570 }
13571
13572 limbo_die_list = NULL;
13573
13574 /* Walk through the list of incomplete types again, trying once more to
13575 emit full debugging info for them. */
13576 retry_incomplete_types ();
13577
13578 /* We need to reverse all the dies before break_out_includes, or
13579 we'll see the end of an include file before the beginning. */
13580 reverse_all_dies (comp_unit_die);
13581
13582 if (flag_eliminate_unused_debug_types)
13583 prune_unused_types ();
13584
13585 /* Generate separate CUs for each of the include files we've seen.
13586 They will go into limbo_die_list. */
13587 if (flag_eliminate_dwarf2_dups)
13588 break_out_includes (comp_unit_die);
13589
13590 /* Traverse the DIE's and add add sibling attributes to those DIE's
13591 that have children. */
13592 add_sibling_attributes (comp_unit_die);
13593 for (node = limbo_die_list; node; node = node->next)
13594 add_sibling_attributes (node->die);
13595
13596 /* Output a terminator label for the .text section. */
13597 text_section ();
13598 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13599
13600 /* Output the source line correspondence table. We must do this
13601 even if there is no line information. Otherwise, on an empty
13602 translation unit, we will generate a present, but empty,
13603 .debug_info section. IRIX 6.5 `nm' will then complain when
13604 examining the file. */
13605 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13606 {
13607 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13608 output_line_info ();
13609 }
13610
13611 /* Output location list section if necessary. */
13612 if (have_location_lists)
13613 {
13614 /* Output the location lists info. */
13615 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13616 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13617 DEBUG_LOC_SECTION_LABEL, 0);
13618 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13619 output_location_lists (die);
13620 have_location_lists = 0;
13621 }
13622
13623 /* We can only use the low/high_pc attributes if all of the code was
13624 in .text. */
13625 if (separate_line_info_table_in_use == 0)
13626 {
13627 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13628 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13629 }
13630
13631 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13632 "base address". Use zero so that these addresses become absolute. */
13633 else if (have_location_lists || ranges_table_in_use)
13634 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13635
13636 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13637 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13638 debug_line_section_label);
13639
13640 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13641 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13642
13643 /* Output all of the compilation units. We put the main one last so that
13644 the offsets are available to output_pubnames. */
13645 for (node = limbo_die_list; node; node = node->next)
13646 output_comp_unit (node->die, 0);
13647
13648 output_comp_unit (comp_unit_die, 0);
13649
13650 /* Output the abbreviation table. */
13651 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13652 output_abbrev_section ();
13653
13654 /* Output public names table if necessary. */
13655 if (pubname_table_in_use)
13656 {
13657 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13658 output_pubnames ();
13659 }
13660
13661 /* Output the address range information. We only put functions in the arange
13662 table, so don't write it out if we don't have any. */
13663 if (fde_table_in_use)
13664 {
13665 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13666 output_aranges ();
13667 }
13668
13669 /* Output ranges section if necessary. */
13670 if (ranges_table_in_use)
13671 {
13672 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13673 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13674 output_ranges ();
13675 }
13676
13677 /* Have to end the primary source file. */
13678 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13679 {
13680 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13681 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13682 dw2_asm_output_data (1, 0, "End compilation unit");
13683 }
13684
13685 /* If we emitted any DW_FORM_strp form attribute, output the string
13686 table too. */
13687 if (debug_str_hash)
13688 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13689 }
13690 #else
13691
13692 /* This should never be used, but its address is needed for comparisons. */
13693 const struct gcc_debug_hooks dwarf2_debug_hooks;
13694
13695 #endif /* DWARF2_DEBUGGING_INFO */
13696
13697 #include "gt-dwarf2out.h"