OSDN Git Service

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