OSDN Git Service

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