OSDN Git Service

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