OSDN Git Service

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