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