OSDN Git Service

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