OSDN Git Service

* cse.c: Fix formatting.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from the GNU C compiler.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 #include "config.h"
38 #include "system.h"
39 #include "tree.h"
40 #include "flags.h"
41 #include "rtl.h"
42 #include "hard-reg-set.h"
43 #include "regs.h"
44 #include "insn-config.h"
45 #include "reload.h"
46 #include "function.h"
47 #include "output.h"
48 #include "expr.h"
49 #include "libfuncs.h"
50 #include "except.h"
51 #include "dwarf2.h"
52 #include "dwarf2out.h"
53 #include "dwarf2asm.h"
54 #include "toplev.h"
55 #include "varray.h"
56 #include "ggc.h"
57 #include "md5.h"
58 #include "tm_p.h"
59 #include "diagnostic.h"
60 #include "debug.h"
61 #include "target.h"
62 #include "langhooks.h"
63 #include "hashtable.h"
64
65 #ifdef DWARF2_DEBUGGING_INFO
66 static void dwarf2out_source_line       PARAMS ((unsigned int, const char *));
67 #endif
68
69 /* DWARF2 Abbreviation Glossary:
70    CFA = Canonical Frame Address
71            a fixed address on the stack which identifies a call frame.
72            We define it to be the value of SP just before the call insn.
73            The CFA register and offset, which may change during the course
74            of the function, are used to calculate its value at runtime.
75    CFI = Call Frame Instruction
76            an instruction for the DWARF2 abstract machine
77    CIE = Common Information Entry
78            information describing information common to one or more FDEs
79    DIE = Debugging Information Entry
80    FDE = Frame Description Entry
81            information describing the stack call frame, in particular,
82            how to restore registers
83
84    DW_CFA_... = DWARF2 CFA call frame instruction
85    DW_TAG_... = DWARF2 DIE tag */
86
87 /* Decide whether we want to emit frame unwind information for the current
88    translation unit.  */
89
90 int
91 dwarf2out_do_frame ()
92 {
93   return (write_symbols == DWARF2_DEBUG
94           || write_symbols == VMS_AND_DWARF2_DEBUG
95 #ifdef DWARF2_FRAME_INFO
96           || DWARF2_FRAME_INFO
97 #endif
98 #ifdef DWARF2_UNWIND_INFO
99           || flag_unwind_tables
100           || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)
101 #endif
102           );
103 }
104
105 /* The number of the current function definition for which debugging
106    information is being generated.  These numbers range from 1 up to the
107    maximum number of function definitions contained within the current
108    compilation unit.  These numbers are used to create unique label id's
109    unique to each function definition.  */
110 unsigned current_funcdef_number = 0;
111
112 /* The size of the target's pointer type.  */
113 #ifndef PTR_SIZE
114 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
115 #endif
116
117 /* Default version of targetm.eh_frame_section.  Note this must appear
118    outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro
119    guards.  */
120
121 void
122 default_eh_frame_section ()
123 {
124 #ifdef EH_FRAME_SECTION_NAME
125   named_section_flags (EH_FRAME_SECTION_NAME, SECTION_WRITE);
126 #else
127   tree label = get_file_function_name ('F');
128
129   data_section ();
130   ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
131   ASM_GLOBALIZE_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
132   ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
133 #endif
134 }
135
136 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
137
138 /* How to start an assembler comment.  */
139 #ifndef ASM_COMMENT_START
140 #define ASM_COMMENT_START ";#"
141 #endif
142
143 typedef struct dw_cfi_struct *dw_cfi_ref;
144 typedef struct dw_fde_struct *dw_fde_ref;
145 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
146
147 /* Call frames are described using a sequence of Call Frame
148    Information instructions.  The register number, offset
149    and address fields are provided as possible operands;
150    their use is selected by the opcode field.  */
151
152 typedef union dw_cfi_oprnd_struct
153 {
154   unsigned long dw_cfi_reg_num;
155   long int dw_cfi_offset;
156   const char *dw_cfi_addr;
157   struct dw_loc_descr_struct *dw_cfi_loc;
158 }
159 dw_cfi_oprnd;
160
161 typedef struct dw_cfi_struct
162 {
163   dw_cfi_ref dw_cfi_next;
164   enum dwarf_call_frame_info dw_cfi_opc;
165   dw_cfi_oprnd dw_cfi_oprnd1;
166   dw_cfi_oprnd dw_cfi_oprnd2;
167 }
168 dw_cfi_node;
169
170 /* This is how we define the location of the CFA. We use to handle it
171    as REG + OFFSET all the time,  but now it can be more complex.
172    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
173    Instead of passing around REG and OFFSET, we pass a copy
174    of this structure.  */
175 typedef struct cfa_loc
176 {
177   unsigned long reg;
178   long offset;
179   long base_offset;
180   int indirect;            /* 1 if CFA is accessed via a dereference.  */
181 } dw_cfa_location;
182
183 /* All call frame descriptions (FDE's) in the GCC generated DWARF
184    refer to a single Common Information Entry (CIE), defined at
185    the beginning of the .debug_frame section.  This use of a single
186    CIE obviates the need to keep track of multiple CIE's
187    in the DWARF generation routines below.  */
188
189 typedef struct dw_fde_struct
190 {
191   const char *dw_fde_begin;
192   const char *dw_fde_current_label;
193   const char *dw_fde_end;
194   dw_cfi_ref dw_fde_cfi;
195   unsigned funcdef_number;
196   unsigned nothrow : 1;
197   unsigned uses_eh_lsda : 1;
198 }
199 dw_fde_node;
200
201 /* Maximum size (in bytes) of an artificially generated label.  */
202 #define MAX_ARTIFICIAL_LABEL_BYTES      30
203
204 /* The size of addresses as they appear in the Dwarf 2 data.
205    Some architectures use word addresses to refer to code locations,
206    but Dwarf 2 info always uses byte addresses.  On such machines,
207    Dwarf 2 addresses need to be larger than the architecture's
208    pointers.  */
209 #ifndef DWARF2_ADDR_SIZE
210 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
211 #endif
212
213 /* The size in bytes of a DWARF field indicating an offset or length
214    relative to a debug info section, specified to be 4 bytes in the
215    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
216    as PTR_SIZE.  */
217
218 #ifndef DWARF_OFFSET_SIZE
219 #define DWARF_OFFSET_SIZE 4
220 #endif
221
222 #define DWARF_VERSION 2
223
224 /* Round SIZE up to the nearest BOUNDARY.  */
225 #define DWARF_ROUND(SIZE,BOUNDARY) \
226   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
227
228 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
229 #ifndef DWARF_CIE_DATA_ALIGNMENT
230 #ifdef STACK_GROWS_DOWNWARD
231 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
232 #else
233 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
234 #endif
235 #endif
236
237 /* A pointer to the base of a table that contains frame description
238    information for each routine.  */
239 static dw_fde_ref fde_table;
240
241 /* Number of elements currently allocated for fde_table.  */
242 static unsigned fde_table_allocated;
243
244 /* Number of elements in fde_table currently in use.  */
245 static unsigned fde_table_in_use;
246
247 /* Size (in elements) of increments by which we may expand the
248    fde_table.  */
249 #define FDE_TABLE_INCREMENT 256
250
251 /* A list of call frame insns for the CIE.  */
252 static dw_cfi_ref cie_cfi_head;
253
254 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
255    attribute that accelerates the lookup of the FDE associated
256    with the subprogram.  This variable holds the table index of the FDE
257    associated with the current function (body) definition.  */
258 static unsigned current_funcdef_fde;
259
260 struct ht *debug_str_hash;
261
262 struct indirect_string_node
263 {
264   struct ht_identifier id;
265   unsigned int refcount;
266   unsigned int form;
267   char *label;
268 };
269
270 /* Forward declarations for functions defined in this file.  */
271
272 static char *stripattributes            PARAMS ((const char *));
273 static const char *dwarf_cfi_name       PARAMS ((unsigned));
274 static dw_cfi_ref new_cfi               PARAMS ((void));
275 static void add_cfi                     PARAMS ((dw_cfi_ref *, dw_cfi_ref));
276 static void add_fde_cfi                 PARAMS ((const char *, dw_cfi_ref));
277 static void lookup_cfa_1                PARAMS ((dw_cfi_ref,
278                                                  dw_cfa_location *));
279 static void lookup_cfa                  PARAMS ((dw_cfa_location *));
280 static void reg_save                    PARAMS ((const char *, unsigned,
281                                                  unsigned, long));
282 static void initial_return_save         PARAMS ((rtx));
283 static long stack_adjust_offset         PARAMS ((rtx));
284 static void output_cfi                  PARAMS ((dw_cfi_ref, dw_fde_ref, int));
285 static void output_call_frame_info      PARAMS ((int));
286 static void dwarf2out_stack_adjust      PARAMS ((rtx));
287 static void queue_reg_save              PARAMS ((const char *, rtx, long));
288 static void flush_queued_reg_saves      PARAMS ((void));
289 static bool clobbers_queued_reg_save    PARAMS ((rtx));
290 static void dwarf2out_frame_debug_expr  PARAMS ((rtx, const char *));
291
292 /* Support for complex CFA locations.  */
293 static void output_cfa_loc              PARAMS ((dw_cfi_ref));
294 static void get_cfa_from_loc_descr      PARAMS ((dw_cfa_location *,
295                                                 struct dw_loc_descr_struct *));
296 static struct dw_loc_descr_struct *build_cfa_loc
297                                         PARAMS ((dw_cfa_location *));
298 static void def_cfa_1                   PARAMS ((const char *,
299                                                  dw_cfa_location *));
300
301 /* .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   tree created_for;
3181   struct limbo_die_struct *next;
3182 }
3183 limbo_die_node;
3184
3185 /* How to start an assembler comment.  */
3186 #ifndef ASM_COMMENT_START
3187 #define ASM_COMMENT_START ";#"
3188 #endif
3189
3190 /* Define a macro which returns non-zero for a TYPE_DECL which was
3191    implicitly generated for a tagged type.
3192
3193    Note that unlike the gcc front end (which generates a NULL named
3194    TYPE_DECL node for each complete tagged type, each array type, and
3195    each function type node created) the g++ front end generates a
3196    _named_ TYPE_DECL node for each tagged type node created.
3197    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3198    generate a DW_TAG_typedef DIE for them.  */
3199
3200 #define TYPE_DECL_IS_STUB(decl)                         \
3201   (DECL_NAME (decl) == NULL_TREE                        \
3202    || (DECL_ARTIFICIAL (decl)                           \
3203        && is_tagged_type (TREE_TYPE (decl))             \
3204        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3205            /* This is necessary for stub decls that     \
3206               appear in nested inline functions.  */    \
3207            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3208                && (decl_ultimate_origin (decl)          \
3209                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3210
3211 /* Information concerning the compilation unit's programming
3212    language, and compiler version.  */
3213
3214 extern int flag_traditional;
3215
3216 /* Fixed size portion of the DWARF compilation unit header.  */
3217 #define DWARF_COMPILE_UNIT_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 3)
3218
3219 /* Fixed size portion of debugging line information prolog.  */
3220 #define DWARF_LINE_PROLOG_HEADER_SIZE 5
3221
3222 /* Fixed size portion of public names info.  */
3223 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3224
3225 /* Fixed size portion of the address range info.  */
3226 #define DWARF_ARANGES_HEADER_SIZE                                       \
3227   (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2)        \
3228    - DWARF_OFFSET_SIZE)
3229
3230 /* Size of padding portion in the address range info.  It must be
3231    aligned to twice the pointer size.  */
3232 #define DWARF_ARANGES_PAD_SIZE \
3233   (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2) \
3234    - (2 * DWARF_OFFSET_SIZE + 4))
3235
3236 /* Use assembler line directives if available.  */
3237 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3238 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3239 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3240 #else
3241 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3242 #endif
3243 #endif
3244
3245 /* Define the architecture-dependent minimum instruction length (in bytes).
3246    In this implementation of DWARF, this field is used for information
3247    purposes only.  Since GCC generates assembly language, we have
3248    no a priori knowledge of how many instruction bytes are generated
3249    for each source line, and therefore can use only the  DW_LNE_set_address
3250    and DW_LNS_fixed_advance_pc line information commands.  */
3251 #ifndef DWARF_LINE_MIN_INSTR_LENGTH
3252 #define DWARF_LINE_MIN_INSTR_LENGTH 4
3253 #endif
3254
3255 /* Minimum line offset in a special line info. opcode.
3256    This value was chosen to give a reasonable range of values.  */
3257 #define DWARF_LINE_BASE  -10
3258
3259 /* First special line opcode - leave room for the standard opcodes.  */
3260 #define DWARF_LINE_OPCODE_BASE  10
3261
3262 /* Range of line offsets in a special line info. opcode.  */
3263 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3264
3265 /* Flag that indicates the initial value of the is_stmt_start flag.
3266    In the present implementation, we do not mark any lines as
3267    the beginning of a source statement, because that information
3268    is not made available by the GCC front-end.  */
3269 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3270
3271 /* This location is used by calc_die_sizes() to keep track
3272    the offset of each DIE within the .debug_info section.  */
3273 static unsigned long next_die_offset;
3274
3275 /* Record the root of the DIE's built for the current compilation unit.  */
3276 static dw_die_ref comp_unit_die;
3277
3278 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3279 static limbo_die_node *limbo_die_list = 0;
3280
3281 /* Structure used by lookup_filename to manage sets of filenames.  */
3282 struct file_table
3283 {
3284   char **table;
3285   unsigned allocated;
3286   unsigned in_use;
3287   unsigned last_lookup_index;
3288 };
3289
3290 /* Size (in elements) of increments by which we may expand the filename
3291    table.  */
3292 #define FILE_TABLE_INCREMENT 64
3293
3294 /* Filenames referenced by this compilation unit.  */
3295 static struct file_table file_table;
3296
3297 /* Local pointer to the name of the main input file.  Initialized in
3298    dwarf2out_init.  */
3299 static const char *primary_filename;
3300
3301 /* A pointer to the base of a table of references to DIE's that describe
3302    declarations.  The table is indexed by DECL_UID() which is a unique
3303    number identifying each decl.  */
3304 static dw_die_ref *decl_die_table;
3305
3306 /* Number of elements currently allocated for the decl_die_table.  */
3307 static unsigned decl_die_table_allocated;
3308
3309 /* Number of elements in decl_die_table currently in use.  */
3310 static unsigned decl_die_table_in_use;
3311
3312 /* Size (in elements) of increments by which we may expand the
3313    decl_die_table.  */
3314 #define DECL_DIE_TABLE_INCREMENT 256
3315
3316 /* A pointer to the base of a table of references to declaration
3317    scopes.  This table is a display which tracks the nesting
3318    of declaration scopes at the current scope and containing
3319    scopes.  This table is used to find the proper place to
3320    define type declaration DIE's.  */
3321 varray_type decl_scope_table;
3322
3323 /* A pointer to the base of a list of references to DIE's that
3324    are uniquely identified by their tag, presence/absence of
3325    children DIE's, and list of attribute/value pairs.  */
3326 static dw_die_ref *abbrev_die_table;
3327
3328 /* Number of elements currently allocated for abbrev_die_table.  */
3329 static unsigned abbrev_die_table_allocated;
3330
3331 /* Number of elements in type_die_table currently in use.  */
3332 static unsigned abbrev_die_table_in_use;
3333
3334 /* Size (in elements) of increments by which we may expand the
3335    abbrev_die_table.  */
3336 #define ABBREV_DIE_TABLE_INCREMENT 256
3337
3338 /* A pointer to the base of a table that contains line information
3339    for each source code line in .text in the compilation unit.  */
3340 static dw_line_info_ref line_info_table;
3341
3342 /* Number of elements currently allocated for line_info_table.  */
3343 static unsigned line_info_table_allocated;
3344
3345 /* Number of elements in separate_line_info_table currently in use.  */
3346 static unsigned separate_line_info_table_in_use;
3347
3348 /* A pointer to the base of a table that contains line information
3349    for each source code line outside of .text in the compilation unit.  */
3350 static dw_separate_line_info_ref separate_line_info_table;
3351
3352 /* Number of elements currently allocated for separate_line_info_table.  */
3353 static unsigned separate_line_info_table_allocated;
3354
3355 /* Number of elements in line_info_table currently in use.  */
3356 static unsigned line_info_table_in_use;
3357
3358 /* Size (in elements) of increments by which we may expand the
3359    line_info_table.  */
3360 #define LINE_INFO_TABLE_INCREMENT 1024
3361
3362 /* A pointer to the base of a table that contains a list of publicly
3363    accessible names.  */
3364 static pubname_ref pubname_table;
3365
3366 /* Number of elements currently allocated for pubname_table.  */
3367 static unsigned pubname_table_allocated;
3368
3369 /* Number of elements in pubname_table currently in use.  */
3370 static unsigned pubname_table_in_use;
3371
3372 /* Size (in elements) of increments by which we may expand the
3373    pubname_table.  */
3374 #define PUBNAME_TABLE_INCREMENT 64
3375
3376 /* Array of dies for which we should generate .debug_arange info.  */
3377 static dw_die_ref *arange_table;
3378
3379 /* Number of elements currently allocated for arange_table.  */
3380 static unsigned arange_table_allocated;
3381
3382 /* Number of elements in arange_table currently in use.  */
3383 static unsigned arange_table_in_use;
3384
3385 /* Size (in elements) of increments by which we may expand the
3386    arange_table.  */
3387 #define ARANGE_TABLE_INCREMENT 64
3388
3389 /* Array of dies for which we should generate .debug_ranges info.  */
3390 static dw_ranges_ref ranges_table;
3391
3392 /* Number of elements currently allocated for ranges_table.  */
3393 static unsigned ranges_table_allocated;
3394
3395 /* Number of elements in ranges_table currently in use.  */
3396 static unsigned ranges_table_in_use;
3397
3398 /* Size (in elements) of increments by which we may expand the
3399    ranges_table.  */
3400 #define RANGES_TABLE_INCREMENT 64
3401
3402 /* Whether we have location lists that need outputting */
3403 static unsigned have_location_lists;
3404
3405 /* A pointer to the base of a list of incomplete types which might be
3406    completed at some later time.  incomplete_types_list needs to be a VARRAY
3407    because we want to tell the garbage collector about it.  */
3408 varray_type incomplete_types;
3409
3410 /* Record whether the function being analyzed contains inlined functions.  */
3411 static int current_function_has_inlines;
3412 #if 0 && defined (MIPS_DEBUGGING_INFO)
3413 static int comp_unit_has_inlines;
3414 #endif
3415
3416 /* Array of RTXes referenced by the debugging information, which therefore
3417    must be kept around forever.  This is a GC root.  */
3418 static varray_type used_rtx_varray;
3419
3420 /* Forward declarations for functions defined in this file.  */
3421
3422 static int is_pseudo_reg                PARAMS ((rtx));
3423 static tree type_main_variant           PARAMS ((tree));
3424 static int is_tagged_type               PARAMS ((tree));
3425 static const char *dwarf_tag_name       PARAMS ((unsigned));
3426 static const char *dwarf_attr_name      PARAMS ((unsigned));
3427 static const char *dwarf_form_name      PARAMS ((unsigned));
3428 #if 0
3429 static const char *dwarf_type_encoding_name PARAMS ((unsigned));
3430 #endif
3431 static tree decl_ultimate_origin        PARAMS ((tree));
3432 static tree block_ultimate_origin       PARAMS ((tree));
3433 static tree decl_class_context          PARAMS ((tree));
3434 static void add_dwarf_attr              PARAMS ((dw_die_ref, dw_attr_ref));
3435 static inline dw_val_class AT_class     PARAMS ((dw_attr_ref));
3436 static void add_AT_flag                 PARAMS ((dw_die_ref,
3437                                                  enum dwarf_attribute,
3438                                                  unsigned));
3439 static inline unsigned AT_flag          PARAMS ((dw_attr_ref));
3440 static void add_AT_int                  PARAMS ((dw_die_ref,
3441                                                  enum dwarf_attribute, long));
3442 static inline long int AT_int           PARAMS ((dw_attr_ref));
3443 static void add_AT_unsigned             PARAMS ((dw_die_ref,
3444                                                  enum dwarf_attribute,
3445                                                  unsigned long));
3446 static inline unsigned long             AT_unsigned PARAMS ((dw_attr_ref));
3447 static void add_AT_long_long            PARAMS ((dw_die_ref,
3448                                                  enum dwarf_attribute,
3449                                                  unsigned long,
3450                                                  unsigned long));
3451 static void add_AT_float                PARAMS ((dw_die_ref,
3452                                                  enum dwarf_attribute,
3453                                                  unsigned, long *));
3454 static void add_AT_string               PARAMS ((dw_die_ref,
3455                                                  enum dwarf_attribute,
3456                                                  const char *));
3457 static inline const char *AT_string     PARAMS ((dw_attr_ref));
3458 static int AT_string_form               PARAMS ((dw_attr_ref));
3459 static void add_AT_die_ref              PARAMS ((dw_die_ref,
3460                                                  enum dwarf_attribute,
3461                                                  dw_die_ref));
3462 static inline dw_die_ref AT_ref         PARAMS ((dw_attr_ref));
3463 static inline int AT_ref_external       PARAMS ((dw_attr_ref));
3464 static inline void set_AT_ref_external  PARAMS ((dw_attr_ref, int));
3465 static void add_AT_fde_ref              PARAMS ((dw_die_ref,
3466                                                  enum dwarf_attribute,
3467                                                  unsigned));
3468 static void add_AT_loc                  PARAMS ((dw_die_ref,
3469                                                  enum dwarf_attribute,
3470                                                  dw_loc_descr_ref));
3471 static inline dw_loc_descr_ref AT_loc   PARAMS ((dw_attr_ref));
3472 static void add_AT_loc_list             PARAMS ((dw_die_ref,
3473                                                  enum dwarf_attribute,
3474                                                  dw_loc_list_ref));
3475 static inline dw_loc_list_ref AT_loc_list PARAMS ((dw_attr_ref));
3476 static void add_AT_addr                 PARAMS ((dw_die_ref,
3477                                                  enum dwarf_attribute,
3478                                                  rtx));
3479 static inline rtx AT_addr               PARAMS ((dw_attr_ref));
3480 static void add_AT_lbl_id               PARAMS ((dw_die_ref,
3481                                                  enum dwarf_attribute,
3482                                                  const char *));
3483 static void add_AT_lbl_offset           PARAMS ((dw_die_ref,
3484                                                  enum dwarf_attribute,
3485                                                  const char *));
3486 static void add_AT_offset               PARAMS ((dw_die_ref,
3487                                                  enum dwarf_attribute,
3488                                                  unsigned long));
3489 static void add_AT_range_list           PARAMS ((dw_die_ref,
3490                                                  enum dwarf_attribute,
3491                                                  unsigned long));
3492 static inline const char *AT_lbl        PARAMS ((dw_attr_ref));
3493 static dw_attr_ref get_AT               PARAMS ((dw_die_ref,
3494                                                  enum dwarf_attribute));
3495 static const char *get_AT_low_pc        PARAMS ((dw_die_ref));
3496 static const char *get_AT_hi_pc         PARAMS ((dw_die_ref));
3497 static const char *get_AT_string        PARAMS ((dw_die_ref,
3498                                                  enum dwarf_attribute));
3499 static int get_AT_flag                  PARAMS ((dw_die_ref,
3500                                                  enum dwarf_attribute));
3501 static unsigned get_AT_unsigned         PARAMS ((dw_die_ref,
3502                                                  enum dwarf_attribute));
3503 static inline dw_die_ref get_AT_ref     PARAMS ((dw_die_ref,
3504                                                  enum dwarf_attribute));
3505 static int is_c_family                  PARAMS ((void));
3506 static int is_cxx                       PARAMS ((void));
3507 static int is_java                      PARAMS ((void));
3508 static int is_fortran                   PARAMS ((void));
3509 static void remove_AT                   PARAMS ((dw_die_ref,
3510                                                  enum dwarf_attribute));
3511 static inline void free_die             PARAMS ((dw_die_ref));
3512 static void remove_children             PARAMS ((dw_die_ref));
3513 static void add_child_die               PARAMS ((dw_die_ref, dw_die_ref));
3514 static dw_die_ref new_die               PARAMS ((enum dwarf_tag, dw_die_ref,
3515                                                  tree));
3516 static dw_die_ref lookup_type_die       PARAMS ((tree));
3517 static void equate_type_number_to_die   PARAMS ((tree, dw_die_ref));
3518 static dw_die_ref lookup_decl_die       PARAMS ((tree));
3519 static void equate_decl_number_to_die   PARAMS ((tree, dw_die_ref));
3520 static void print_spaces                PARAMS ((FILE *));
3521 static void print_die                   PARAMS ((dw_die_ref, FILE *));
3522 static void print_dwarf_line_table      PARAMS ((FILE *));
3523 static void reverse_die_lists           PARAMS ((dw_die_ref));
3524 static void reverse_all_dies            PARAMS ((dw_die_ref));
3525 static dw_die_ref push_new_compile_unit PARAMS ((dw_die_ref, dw_die_ref));
3526 static dw_die_ref pop_compile_unit      PARAMS ((dw_die_ref));
3527 static void loc_checksum                PARAMS ((dw_loc_descr_ref,
3528                                                  struct md5_ctx *));
3529 static void attr_checksum               PARAMS ((dw_attr_ref,
3530                                                  struct md5_ctx *));
3531 static void die_checksum                PARAMS ((dw_die_ref,
3532                                                  struct md5_ctx *));
3533 static void compute_section_prefix      PARAMS ((dw_die_ref));
3534 static int is_type_die                  PARAMS ((dw_die_ref));
3535 static int is_comdat_die                PARAMS ((dw_die_ref));
3536 static int is_symbol_die                PARAMS ((dw_die_ref));
3537 static void assign_symbol_names         PARAMS ((dw_die_ref));
3538 static void break_out_includes          PARAMS ((dw_die_ref));
3539 static void add_sibling_attributes      PARAMS ((dw_die_ref));
3540 static void build_abbrev_table          PARAMS ((dw_die_ref));
3541 static void output_location_lists       PARAMS ((dw_die_ref));
3542 static int constant_size                PARAMS ((long unsigned));
3543 static unsigned long size_of_die        PARAMS ((dw_die_ref));
3544 static void calc_die_sizes              PARAMS ((dw_die_ref));
3545 static void mark_dies                   PARAMS ((dw_die_ref));
3546 static void unmark_dies                 PARAMS ((dw_die_ref));
3547 static unsigned long size_of_pubnames   PARAMS ((void));
3548 static unsigned long size_of_aranges    PARAMS ((void));
3549 static enum dwarf_form value_format     PARAMS ((dw_attr_ref));
3550 static void output_value_format         PARAMS ((dw_attr_ref));
3551 static void output_abbrev_section       PARAMS ((void));
3552 static void output_die_symbol           PARAMS ((dw_die_ref));
3553 static void output_die                  PARAMS ((dw_die_ref));
3554 static void output_compilation_unit_header PARAMS ((void));
3555 static void output_comp_unit            PARAMS ((dw_die_ref));
3556 static const char *dwarf2_name          PARAMS ((tree, int));
3557 static void add_pubname                 PARAMS ((tree, dw_die_ref));
3558 static void output_pubnames             PARAMS ((void));
3559 static void add_arange                  PARAMS ((tree, dw_die_ref));
3560 static void output_aranges              PARAMS ((void));
3561 static unsigned int add_ranges          PARAMS ((tree));
3562 static void output_ranges               PARAMS ((void));
3563 static void output_line_info            PARAMS ((void));
3564 static void output_file_names           PARAMS ((void));
3565 static dw_die_ref base_type_die         PARAMS ((tree));
3566 static tree root_type                   PARAMS ((tree));
3567 static int is_base_type                 PARAMS ((tree));
3568 static dw_die_ref modified_type_die     PARAMS ((tree, int, int, dw_die_ref));
3569 static int type_is_enum                 PARAMS ((tree));
3570 static unsigned int reg_number          PARAMS ((rtx));
3571 static dw_loc_descr_ref reg_loc_descriptor PARAMS ((rtx));
3572 static dw_loc_descr_ref int_loc_descriptor PARAMS ((HOST_WIDE_INT));
3573 static dw_loc_descr_ref based_loc_descr PARAMS ((unsigned, long));
3574 static int is_based_loc                 PARAMS ((rtx));
3575 static dw_loc_descr_ref mem_loc_descriptor PARAMS ((rtx, enum machine_mode mode));
3576 static dw_loc_descr_ref concat_loc_descriptor PARAMS ((rtx, rtx));
3577 static dw_loc_descr_ref loc_descriptor  PARAMS ((rtx));
3578 static dw_loc_descr_ref loc_descriptor_from_tree PARAMS ((tree, int));
3579 static HOST_WIDE_INT ceiling            PARAMS ((HOST_WIDE_INT, unsigned int));
3580 static tree field_type                  PARAMS ((tree));
3581 static unsigned int simple_type_align_in_bits PARAMS ((tree));
3582 static unsigned int simple_decl_align_in_bits PARAMS ((tree));
3583 static unsigned HOST_WIDE_INT simple_type_size_in_bits PARAMS ((tree));
3584 static HOST_WIDE_INT field_byte_offset  PARAMS ((tree));
3585 static void add_AT_location_description PARAMS ((dw_die_ref,
3586                                                  enum dwarf_attribute, rtx));
3587 static void add_data_member_location_attribute PARAMS ((dw_die_ref, tree));
3588 static void add_const_value_attribute   PARAMS ((dw_die_ref, rtx));
3589 static rtx rtl_for_decl_location        PARAMS ((tree));
3590 static void add_location_or_const_value_attribute PARAMS ((dw_die_ref, tree));
3591 static void tree_add_const_value_attribute PARAMS ((dw_die_ref, tree));
3592 static void add_name_attribute          PARAMS ((dw_die_ref, const char *));
3593 static void add_bound_info              PARAMS ((dw_die_ref,
3594                                                  enum dwarf_attribute, tree));
3595 static void add_subscript_info          PARAMS ((dw_die_ref, tree));
3596 static void add_byte_size_attribute     PARAMS ((dw_die_ref, tree));
3597 static void add_bit_offset_attribute    PARAMS ((dw_die_ref, tree));
3598 static void add_bit_size_attribute      PARAMS ((dw_die_ref, tree));
3599 static void add_prototyped_attribute    PARAMS ((dw_die_ref, tree));
3600 static void add_abstract_origin_attribute PARAMS ((dw_die_ref, tree));
3601 static void add_pure_or_virtual_attribute PARAMS ((dw_die_ref, tree));
3602 static void add_src_coords_attributes   PARAMS ((dw_die_ref, tree));
3603 static void add_name_and_src_coords_attributes PARAMS ((dw_die_ref, tree));
3604 static void push_decl_scope             PARAMS ((tree));
3605 static void pop_decl_scope              PARAMS ((void));
3606 static dw_die_ref scope_die_for         PARAMS ((tree, dw_die_ref));
3607 static inline int local_scope_p         PARAMS ((dw_die_ref));
3608 static inline int class_scope_p         PARAMS ((dw_die_ref));
3609 static void add_type_attribute          PARAMS ((dw_die_ref, tree, int, int,
3610                                                  dw_die_ref));
3611 static const char *type_tag             PARAMS ((tree));
3612 static tree member_declared_type        PARAMS ((tree));
3613 #if 0
3614 static const char *decl_start_label     PARAMS ((tree));
3615 #endif
3616 static void gen_array_type_die          PARAMS ((tree, dw_die_ref));
3617 static void gen_set_type_die            PARAMS ((tree, dw_die_ref));
3618 #if 0
3619 static void gen_entry_point_die         PARAMS ((tree, dw_die_ref));
3620 #endif
3621 static void gen_inlined_enumeration_type_die PARAMS ((tree, dw_die_ref));
3622 static void gen_inlined_structure_type_die PARAMS ((tree, dw_die_ref));
3623 static void gen_inlined_union_type_die  PARAMS ((tree, dw_die_ref));
3624 static void gen_enumeration_type_die    PARAMS ((tree, dw_die_ref));
3625 static dw_die_ref gen_formal_parameter_die PARAMS ((tree, dw_die_ref));
3626 static void gen_unspecified_parameters_die PARAMS ((tree, dw_die_ref));
3627 static void gen_formal_types_die        PARAMS ((tree, dw_die_ref));
3628 static void gen_subprogram_die          PARAMS ((tree, dw_die_ref));
3629 static void gen_variable_die            PARAMS ((tree, dw_die_ref));
3630 static void gen_label_die               PARAMS ((tree, dw_die_ref));
3631 static void gen_lexical_block_die       PARAMS ((tree, dw_die_ref, int));
3632 static void gen_inlined_subroutine_die  PARAMS ((tree, dw_die_ref, int));
3633 static void gen_field_die               PARAMS ((tree, dw_die_ref));
3634 static void gen_ptr_to_mbr_type_die     PARAMS ((tree, dw_die_ref));
3635 static dw_die_ref gen_compile_unit_die  PARAMS ((const char *));
3636 static void gen_string_type_die         PARAMS ((tree, dw_die_ref));
3637 static void gen_inheritance_die         PARAMS ((tree, dw_die_ref));
3638 static void gen_member_die              PARAMS ((tree, dw_die_ref));
3639 static void gen_struct_or_union_type_die PARAMS ((tree, dw_die_ref));
3640 static void gen_subroutine_type_die     PARAMS ((tree, dw_die_ref));
3641 static void gen_typedef_die             PARAMS ((tree, dw_die_ref));
3642 static void gen_type_die                PARAMS ((tree, dw_die_ref));
3643 static void gen_tagged_type_instantiation_die PARAMS ((tree, dw_die_ref));
3644 static void gen_block_die               PARAMS ((tree, dw_die_ref, int));
3645 static void decls_for_scope             PARAMS ((tree, dw_die_ref, int));
3646 static int is_redundant_typedef         PARAMS ((tree));
3647 static void gen_decl_die                PARAMS ((tree, dw_die_ref));
3648 static unsigned lookup_filename         PARAMS ((const char *));
3649 static void init_file_table             PARAMS ((void));
3650 static void retry_incomplete_types      PARAMS ((void));
3651 static void gen_type_die_for_member     PARAMS ((tree, tree, dw_die_ref));
3652 static void splice_child_die            PARAMS ((dw_die_ref, dw_die_ref));
3653 static int file_info_cmp                PARAMS ((const void *, const void *));
3654 static dw_loc_list_ref new_loc_list     PARAMS ((dw_loc_descr_ref, 
3655                                                  const char *, const char *,
3656                                                  const char *, unsigned));
3657 static void add_loc_descr_to_loc_list   PARAMS ((dw_loc_list_ref *,
3658                                                  dw_loc_descr_ref,
3659                                                  const char *, const char *, const char *));
3660 static void output_loc_list             PARAMS ((dw_loc_list_ref));
3661 static char *gen_internal_sym           PARAMS ((const char *));
3662 static void mark_limbo_die_list         PARAMS ((void *));
3663
3664 /* Section names used to hold DWARF debugging information.  */
3665 #ifndef DEBUG_INFO_SECTION
3666 #define DEBUG_INFO_SECTION      ".debug_info"
3667 #endif
3668 #ifndef DEBUG_ABBREV_SECTION
3669 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3670 #endif
3671 #ifndef DEBUG_ARANGES_SECTION
3672 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3673 #endif
3674 #ifndef DEBUG_MACINFO_SECTION
3675 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3676 #endif
3677 #ifndef DEBUG_LINE_SECTION
3678 #define DEBUG_LINE_SECTION      ".debug_line"
3679 #endif
3680 #ifndef DEBUG_LOC_SECTION
3681 #define DEBUG_LOC_SECTION       ".debug_loc"
3682 #endif
3683 #ifndef DEBUG_PUBNAMES_SECTION
3684 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3685 #endif
3686 #ifndef DEBUG_STR_SECTION
3687 #define DEBUG_STR_SECTION       ".debug_str"
3688 #endif
3689 #ifndef DEBUG_RANGES_SECTION
3690 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3691 #endif
3692
3693 /* Standard ELF section names for compiled code and data.  */
3694 #ifndef TEXT_SECTION_NAME
3695 #define TEXT_SECTION_NAME       ".text"
3696 #endif
3697
3698 /* Section flags for .debug_str section.  */
3699 #ifdef HAVE_GAS_SHF_MERGE
3700 #define DEBUG_STR_SECTION_FLAGS \
3701   (SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1)
3702 #else
3703 #define DEBUG_STR_SECTION_FLAGS SECTION_DEBUG
3704 #endif
3705
3706 /* Labels we insert at beginning sections we can reference instead of
3707    the section names themselves.  */
3708
3709 #ifndef TEXT_SECTION_LABEL
3710 #define TEXT_SECTION_LABEL              "Ltext"
3711 #endif
3712 #ifndef DEBUG_LINE_SECTION_LABEL
3713 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3714 #endif
3715 #ifndef DEBUG_INFO_SECTION_LABEL
3716 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3717 #endif
3718 #ifndef DEBUG_ABBREV_SECTION_LABEL
3719 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3720 #endif
3721 #ifndef DEBUG_LOC_SECTION_LABEL
3722 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3723 #endif
3724 #ifndef DEBUG_RANGES_SECTION_LABEL
3725 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3726 #endif
3727 #ifndef DEBUG_MACINFO_SECTION_LABEL
3728 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3729 #endif
3730
3731 /* Definitions of defaults for formats and names of various special
3732    (artificial) labels which may be generated within this file (when the -g
3733    options is used and DWARF_DEBUGGING_INFO is in effect.
3734    If necessary, these may be overridden from within the tm.h file, but
3735    typically, overriding these defaults is unnecessary.  */
3736
3737 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3738 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3739 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3740 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3741 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3742 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3743 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3744 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3745
3746 #ifndef TEXT_END_LABEL
3747 #define TEXT_END_LABEL          "Letext"
3748 #endif
3749 #ifndef DATA_END_LABEL
3750 #define DATA_END_LABEL          "Ledata"
3751 #endif
3752 #ifndef BSS_END_LABEL
3753 #define BSS_END_LABEL           "Lebss"
3754 #endif
3755 #ifndef BLOCK_BEGIN_LABEL
3756 #define BLOCK_BEGIN_LABEL       "LBB"
3757 #endif
3758 #ifndef BLOCK_END_LABEL
3759 #define BLOCK_END_LABEL         "LBE"
3760 #endif
3761 #ifndef BODY_BEGIN_LABEL
3762 #define BODY_BEGIN_LABEL        "Lbb"
3763 #endif
3764 #ifndef BODY_END_LABEL
3765 #define BODY_END_LABEL          "Lbe"
3766 #endif
3767 #ifndef LINE_CODE_LABEL
3768 #define LINE_CODE_LABEL         "LM"
3769 #endif
3770 #ifndef SEPARATE_LINE_CODE_LABEL
3771 #define SEPARATE_LINE_CODE_LABEL        "LSM"
3772 #endif
3773 \f
3774 /* We allow a language front-end to designate a function that is to be
3775    called to "demangle" any name before it it put into a DIE.  */
3776
3777 static const char *(*demangle_name_func) PARAMS ((const char *));
3778
3779 void
3780 dwarf2out_set_demangle_name_func (func)
3781      const char *(*func) PARAMS ((const char *));
3782 {
3783   demangle_name_func = func;
3784 }
3785
3786 /* Test if rtl node points to a pseudo register.  */
3787
3788 static inline int
3789 is_pseudo_reg (rtl)
3790      rtx rtl;
3791 {
3792   return ((GET_CODE (rtl) == REG && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3793           || (GET_CODE (rtl) == SUBREG
3794               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3795 }
3796
3797 /* Return a reference to a type, with its const and volatile qualifiers
3798    removed.  */
3799
3800 static inline tree
3801 type_main_variant (type)
3802      tree type;
3803 {
3804   type = TYPE_MAIN_VARIANT (type);
3805
3806   /* ??? There really should be only one main variant among any group of
3807      variants of a given type (and all of the MAIN_VARIANT values for all
3808      members of the group should point to that one type) but sometimes the C
3809      front-end messes this up for array types, so we work around that bug
3810      here.  */
3811   if (TREE_CODE (type) == ARRAY_TYPE)
3812     while (type != TYPE_MAIN_VARIANT (type))
3813       type = TYPE_MAIN_VARIANT (type);
3814
3815   return type;
3816 }
3817
3818 /* Return non-zero if the given type node represents a tagged type.  */
3819
3820 static inline int
3821 is_tagged_type (type)
3822      tree type;
3823 {
3824   enum tree_code code = TREE_CODE (type);
3825
3826   return (code == RECORD_TYPE || code == UNION_TYPE
3827           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3828 }
3829
3830 /* Convert a DIE tag into its string name.  */
3831
3832 static const char *
3833 dwarf_tag_name (tag)
3834      unsigned tag;
3835 {
3836   switch (tag)
3837     {
3838     case DW_TAG_padding:
3839       return "DW_TAG_padding";
3840     case DW_TAG_array_type:
3841       return "DW_TAG_array_type";
3842     case DW_TAG_class_type:
3843       return "DW_TAG_class_type";
3844     case DW_TAG_entry_point:
3845       return "DW_TAG_entry_point";
3846     case DW_TAG_enumeration_type:
3847       return "DW_TAG_enumeration_type";
3848     case DW_TAG_formal_parameter:
3849       return "DW_TAG_formal_parameter";
3850     case DW_TAG_imported_declaration:
3851       return "DW_TAG_imported_declaration";
3852     case DW_TAG_label:
3853       return "DW_TAG_label";
3854     case DW_TAG_lexical_block:
3855       return "DW_TAG_lexical_block";
3856     case DW_TAG_member:
3857       return "DW_TAG_member";
3858     case DW_TAG_pointer_type:
3859       return "DW_TAG_pointer_type";
3860     case DW_TAG_reference_type:
3861       return "DW_TAG_reference_type";
3862     case DW_TAG_compile_unit:
3863       return "DW_TAG_compile_unit";
3864     case DW_TAG_string_type:
3865       return "DW_TAG_string_type";
3866     case DW_TAG_structure_type:
3867       return "DW_TAG_structure_type";
3868     case DW_TAG_subroutine_type:
3869       return "DW_TAG_subroutine_type";
3870     case DW_TAG_typedef:
3871       return "DW_TAG_typedef";
3872     case DW_TAG_union_type:
3873       return "DW_TAG_union_type";
3874     case DW_TAG_unspecified_parameters:
3875       return "DW_TAG_unspecified_parameters";
3876     case DW_TAG_variant:
3877       return "DW_TAG_variant";
3878     case DW_TAG_common_block:
3879       return "DW_TAG_common_block";
3880     case DW_TAG_common_inclusion:
3881       return "DW_TAG_common_inclusion";
3882     case DW_TAG_inheritance:
3883       return "DW_TAG_inheritance";
3884     case DW_TAG_inlined_subroutine:
3885       return "DW_TAG_inlined_subroutine";
3886     case DW_TAG_module:
3887       return "DW_TAG_module";
3888     case DW_TAG_ptr_to_member_type:
3889       return "DW_TAG_ptr_to_member_type";
3890     case DW_TAG_set_type:
3891       return "DW_TAG_set_type";
3892     case DW_TAG_subrange_type:
3893       return "DW_TAG_subrange_type";
3894     case DW_TAG_with_stmt:
3895       return "DW_TAG_with_stmt";
3896     case DW_TAG_access_declaration:
3897       return "DW_TAG_access_declaration";
3898     case DW_TAG_base_type:
3899       return "DW_TAG_base_type";
3900     case DW_TAG_catch_block:
3901       return "DW_TAG_catch_block";
3902     case DW_TAG_const_type:
3903       return "DW_TAG_const_type";
3904     case DW_TAG_constant:
3905       return "DW_TAG_constant";
3906     case DW_TAG_enumerator:
3907       return "DW_TAG_enumerator";
3908     case DW_TAG_file_type:
3909       return "DW_TAG_file_type";
3910     case DW_TAG_friend:
3911       return "DW_TAG_friend";
3912     case DW_TAG_namelist:
3913       return "DW_TAG_namelist";
3914     case DW_TAG_namelist_item:
3915       return "DW_TAG_namelist_item";
3916     case DW_TAG_packed_type:
3917       return "DW_TAG_packed_type";
3918     case DW_TAG_subprogram:
3919       return "DW_TAG_subprogram";
3920     case DW_TAG_template_type_param:
3921       return "DW_TAG_template_type_param";
3922     case DW_TAG_template_value_param:
3923       return "DW_TAG_template_value_param";
3924     case DW_TAG_thrown_type:
3925       return "DW_TAG_thrown_type";
3926     case DW_TAG_try_block:
3927       return "DW_TAG_try_block";
3928     case DW_TAG_variant_part:
3929       return "DW_TAG_variant_part";
3930     case DW_TAG_variable:
3931       return "DW_TAG_variable";
3932     case DW_TAG_volatile_type:
3933       return "DW_TAG_volatile_type";
3934     case DW_TAG_MIPS_loop:
3935       return "DW_TAG_MIPS_loop";
3936     case DW_TAG_format_label:
3937       return "DW_TAG_format_label";
3938     case DW_TAG_function_template:
3939       return "DW_TAG_function_template";
3940     case DW_TAG_class_template:
3941       return "DW_TAG_class_template";
3942     case DW_TAG_GNU_BINCL:
3943       return "DW_TAG_GNU_BINCL";
3944     case DW_TAG_GNU_EINCL:
3945       return "DW_TAG_GNU_EINCL";
3946     default:
3947       return "DW_TAG_<unknown>";
3948     }
3949 }
3950
3951 /* Convert a DWARF attribute code into its string name.  */
3952
3953 static const char *
3954 dwarf_attr_name (attr)
3955      unsigned attr;
3956 {
3957   switch (attr)
3958     {
3959     case DW_AT_sibling:
3960       return "DW_AT_sibling";
3961     case DW_AT_location:
3962       return "DW_AT_location";
3963     case DW_AT_name:
3964       return "DW_AT_name";
3965     case DW_AT_ordering:
3966       return "DW_AT_ordering";
3967     case DW_AT_subscr_data:
3968       return "DW_AT_subscr_data";
3969     case DW_AT_byte_size:
3970       return "DW_AT_byte_size";
3971     case DW_AT_bit_offset:
3972       return "DW_AT_bit_offset";
3973     case DW_AT_bit_size:
3974       return "DW_AT_bit_size";
3975     case DW_AT_element_list:
3976       return "DW_AT_element_list";
3977     case DW_AT_stmt_list:
3978       return "DW_AT_stmt_list";
3979     case DW_AT_low_pc:
3980       return "DW_AT_low_pc";
3981     case DW_AT_high_pc:
3982       return "DW_AT_high_pc";
3983     case DW_AT_language:
3984       return "DW_AT_language";
3985     case DW_AT_member:
3986       return "DW_AT_member";
3987     case DW_AT_discr:
3988       return "DW_AT_discr";
3989     case DW_AT_discr_value:
3990       return "DW_AT_discr_value";
3991     case DW_AT_visibility:
3992       return "DW_AT_visibility";
3993     case DW_AT_import:
3994       return "DW_AT_import";
3995     case DW_AT_string_length:
3996       return "DW_AT_string_length";
3997     case DW_AT_common_reference:
3998       return "DW_AT_common_reference";
3999     case DW_AT_comp_dir:
4000       return "DW_AT_comp_dir";
4001     case DW_AT_const_value:
4002       return "DW_AT_const_value";
4003     case DW_AT_containing_type:
4004       return "DW_AT_containing_type";
4005     case DW_AT_default_value:
4006       return "DW_AT_default_value";
4007     case DW_AT_inline:
4008       return "DW_AT_inline";
4009     case DW_AT_is_optional:
4010       return "DW_AT_is_optional";
4011     case DW_AT_lower_bound:
4012       return "DW_AT_lower_bound";
4013     case DW_AT_producer:
4014       return "DW_AT_producer";
4015     case DW_AT_prototyped:
4016       return "DW_AT_prototyped";
4017     case DW_AT_return_addr:
4018       return "DW_AT_return_addr";
4019     case DW_AT_start_scope:
4020       return "DW_AT_start_scope";
4021     case DW_AT_stride_size:
4022       return "DW_AT_stride_size";
4023     case DW_AT_upper_bound:
4024       return "DW_AT_upper_bound";
4025     case DW_AT_abstract_origin:
4026       return "DW_AT_abstract_origin";
4027     case DW_AT_accessibility:
4028       return "DW_AT_accessibility";
4029     case DW_AT_address_class:
4030       return "DW_AT_address_class";
4031     case DW_AT_artificial:
4032       return "DW_AT_artificial";
4033     case DW_AT_base_types:
4034       return "DW_AT_base_types";
4035     case DW_AT_calling_convention:
4036       return "DW_AT_calling_convention";
4037     case DW_AT_count:
4038       return "DW_AT_count";
4039     case DW_AT_data_member_location:
4040       return "DW_AT_data_member_location";
4041     case DW_AT_decl_column:
4042       return "DW_AT_decl_column";
4043     case DW_AT_decl_file:
4044       return "DW_AT_decl_file";
4045     case DW_AT_decl_line:
4046       return "DW_AT_decl_line";
4047     case DW_AT_declaration:
4048       return "DW_AT_declaration";
4049     case DW_AT_discr_list:
4050       return "DW_AT_discr_list";
4051     case DW_AT_encoding:
4052       return "DW_AT_encoding";
4053     case DW_AT_external:
4054       return "DW_AT_external";
4055     case DW_AT_frame_base:
4056       return "DW_AT_frame_base";
4057     case DW_AT_friend:
4058       return "DW_AT_friend";
4059     case DW_AT_identifier_case:
4060       return "DW_AT_identifier_case";
4061     case DW_AT_macro_info:
4062       return "DW_AT_macro_info";
4063     case DW_AT_namelist_items:
4064       return "DW_AT_namelist_items";
4065     case DW_AT_priority:
4066       return "DW_AT_priority";
4067     case DW_AT_segment:
4068       return "DW_AT_segment";
4069     case DW_AT_specification:
4070       return "DW_AT_specification";
4071     case DW_AT_static_link:
4072       return "DW_AT_static_link";
4073     case DW_AT_type:
4074       return "DW_AT_type";
4075     case DW_AT_use_location:
4076       return "DW_AT_use_location";
4077     case DW_AT_variable_parameter:
4078       return "DW_AT_variable_parameter";
4079     case DW_AT_virtuality:
4080       return "DW_AT_virtuality";
4081     case DW_AT_vtable_elem_location:
4082       return "DW_AT_vtable_elem_location";
4083
4084     case DW_AT_allocated:
4085       return "DW_AT_allocated";
4086     case DW_AT_associated:
4087       return "DW_AT_associated";
4088     case DW_AT_data_location:
4089       return "DW_AT_data_location";
4090     case DW_AT_stride:
4091       return "DW_AT_stride";
4092     case DW_AT_entry_pc:
4093       return "DW_AT_entry_pc";
4094     case DW_AT_use_UTF8:
4095       return "DW_AT_use_UTF8";
4096     case DW_AT_extension:
4097       return "DW_AT_extension";
4098     case DW_AT_ranges:
4099       return "DW_AT_ranges";
4100     case DW_AT_trampoline:
4101       return "DW_AT_trampoline";
4102     case DW_AT_call_column:
4103       return "DW_AT_call_column";
4104     case DW_AT_call_file:
4105       return "DW_AT_call_file";
4106     case DW_AT_call_line:
4107       return "DW_AT_call_line";
4108
4109     case DW_AT_MIPS_fde:
4110       return "DW_AT_MIPS_fde";
4111     case DW_AT_MIPS_loop_begin:
4112       return "DW_AT_MIPS_loop_begin";
4113     case DW_AT_MIPS_tail_loop_begin:
4114       return "DW_AT_MIPS_tail_loop_begin";
4115     case DW_AT_MIPS_epilog_begin:
4116       return "DW_AT_MIPS_epilog_begin";
4117     case DW_AT_MIPS_loop_unroll_factor:
4118       return "DW_AT_MIPS_loop_unroll_factor";
4119     case DW_AT_MIPS_software_pipeline_depth:
4120       return "DW_AT_MIPS_software_pipeline_depth";
4121     case DW_AT_MIPS_linkage_name:
4122       return "DW_AT_MIPS_linkage_name";
4123     case DW_AT_MIPS_stride:
4124       return "DW_AT_MIPS_stride";
4125     case DW_AT_MIPS_abstract_name:
4126       return "DW_AT_MIPS_abstract_name";
4127     case DW_AT_MIPS_clone_origin:
4128       return "DW_AT_MIPS_clone_origin";
4129     case DW_AT_MIPS_has_inlines:
4130       return "DW_AT_MIPS_has_inlines";
4131
4132     case DW_AT_sf_names:
4133       return "DW_AT_sf_names";
4134     case DW_AT_src_info:
4135       return "DW_AT_src_info";
4136     case DW_AT_mac_info:
4137       return "DW_AT_mac_info";
4138     case DW_AT_src_coords:
4139       return "DW_AT_src_coords";
4140     case DW_AT_body_begin:
4141       return "DW_AT_body_begin";
4142     case DW_AT_body_end:
4143       return "DW_AT_body_end";
4144     case DW_AT_VMS_rtnbeg_pd_address:
4145       return "DW_AT_VMS_rtnbeg_pd_address";
4146
4147     default:
4148       return "DW_AT_<unknown>";
4149     }
4150 }
4151
4152 /* Convert a DWARF value form code into its string name.  */
4153
4154 static const char *
4155 dwarf_form_name (form)
4156      unsigned form;
4157 {
4158   switch (form)
4159     {
4160     case DW_FORM_addr:
4161       return "DW_FORM_addr";
4162     case DW_FORM_block2:
4163       return "DW_FORM_block2";
4164     case DW_FORM_block4:
4165       return "DW_FORM_block4";
4166     case DW_FORM_data2:
4167       return "DW_FORM_data2";
4168     case DW_FORM_data4:
4169       return "DW_FORM_data4";
4170     case DW_FORM_data8:
4171       return "DW_FORM_data8";
4172     case DW_FORM_string:
4173       return "DW_FORM_string";
4174     case DW_FORM_block:
4175       return "DW_FORM_block";
4176     case DW_FORM_block1:
4177       return "DW_FORM_block1";
4178     case DW_FORM_data1:
4179       return "DW_FORM_data1";
4180     case DW_FORM_flag:
4181       return "DW_FORM_flag";
4182     case DW_FORM_sdata:
4183       return "DW_FORM_sdata";
4184     case DW_FORM_strp:
4185       return "DW_FORM_strp";
4186     case DW_FORM_udata:
4187       return "DW_FORM_udata";
4188     case DW_FORM_ref_addr:
4189       return "DW_FORM_ref_addr";
4190     case DW_FORM_ref1:
4191       return "DW_FORM_ref1";
4192     case DW_FORM_ref2:
4193       return "DW_FORM_ref2";
4194     case DW_FORM_ref4:
4195       return "DW_FORM_ref4";
4196     case DW_FORM_ref8:
4197       return "DW_FORM_ref8";
4198     case DW_FORM_ref_udata:
4199       return "DW_FORM_ref_udata";
4200     case DW_FORM_indirect:
4201       return "DW_FORM_indirect";
4202     default:
4203       return "DW_FORM_<unknown>";
4204     }
4205 }
4206
4207 /* Convert a DWARF type code into its string name.  */
4208
4209 #if 0
4210 static const char *
4211 dwarf_type_encoding_name (enc)
4212      unsigned enc;
4213 {
4214   switch (enc)
4215     {
4216     case DW_ATE_address:
4217       return "DW_ATE_address";
4218     case DW_ATE_boolean:
4219       return "DW_ATE_boolean";
4220     case DW_ATE_complex_float:
4221       return "DW_ATE_complex_float";
4222     case DW_ATE_float:
4223       return "DW_ATE_float";
4224     case DW_ATE_signed:
4225       return "DW_ATE_signed";
4226     case DW_ATE_signed_char:
4227       return "DW_ATE_signed_char";
4228     case DW_ATE_unsigned:
4229       return "DW_ATE_unsigned";
4230     case DW_ATE_unsigned_char:
4231       return "DW_ATE_unsigned_char";
4232     default:
4233       return "DW_ATE_<unknown>";
4234     }
4235 }
4236 #endif
4237 \f
4238 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4239    instance of an inlined instance of a decl which is local to an inline
4240    function, so we have to trace all of the way back through the origin chain
4241    to find out what sort of node actually served as the original seed for the
4242    given block.  */
4243
4244 static tree
4245 decl_ultimate_origin (decl)
4246      tree decl;
4247 {
4248   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4249      nodes in the function to point to themselves; ignore that if
4250      we're trying to output the abstract instance of this function.  */
4251   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4252     return NULL_TREE;
4253
4254 #ifdef ENABLE_CHECKING
4255   if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4256     /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4257        most distant ancestor, this should never happen.  */
4258     abort ();
4259 #endif
4260
4261   return DECL_ABSTRACT_ORIGIN (decl);
4262 }
4263
4264 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4265    instance of an inlined instance of a block which is local to an inline
4266    function, so we have to trace all of the way back through the origin chain
4267    to find out what sort of node actually served as the original seed for the
4268    given block.  */
4269
4270 static tree
4271 block_ultimate_origin (block)
4272      tree block;
4273 {
4274   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4275
4276   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4277      nodes in the function to point to themselves; ignore that if
4278      we're trying to output the abstract instance of this function.  */
4279   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4280     return NULL_TREE;
4281
4282   if (immediate_origin == NULL_TREE)
4283     return NULL_TREE;
4284   else
4285     {
4286       tree ret_val;
4287       tree lookahead = immediate_origin;
4288
4289       do
4290         {
4291           ret_val = lookahead;
4292           lookahead = (TREE_CODE (ret_val) == BLOCK
4293                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4294         }
4295       while (lookahead != NULL && lookahead != ret_val);
4296
4297       return ret_val;
4298     }
4299 }
4300
4301 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4302    of a virtual function may refer to a base class, so we check the 'this'
4303    parameter.  */
4304
4305 static tree
4306 decl_class_context (decl)
4307      tree decl;
4308 {
4309   tree context = NULL_TREE;
4310
4311   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4312     context = DECL_CONTEXT (decl);
4313   else
4314     context = TYPE_MAIN_VARIANT
4315       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4316
4317   if (context && !TYPE_P (context))
4318     context = NULL_TREE;
4319
4320   return context;
4321 }
4322 \f
4323 /* Add an attribute/value pair to a DIE.  We build the lists up in reverse
4324    addition order, and correct that in reverse_all_dies.  */
4325
4326 static inline void
4327 add_dwarf_attr (die, attr)
4328      dw_die_ref die;
4329      dw_attr_ref attr;
4330 {
4331   if (die != NULL && attr != NULL)
4332     {
4333       attr->dw_attr_next = die->die_attr;
4334       die->die_attr = attr;
4335     }
4336 }
4337
4338 static inline dw_val_class
4339 AT_class (a)
4340      dw_attr_ref a;
4341 {
4342   return a->dw_attr_val.val_class;
4343 }
4344
4345 /* Add a flag value attribute to a DIE.  */
4346
4347 static inline void
4348 add_AT_flag (die, attr_kind, flag)
4349      dw_die_ref die;
4350      enum dwarf_attribute attr_kind;
4351      unsigned flag;
4352 {
4353   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4354
4355   attr->dw_attr_next = NULL;
4356   attr->dw_attr = attr_kind;
4357   attr->dw_attr_val.val_class = dw_val_class_flag;
4358   attr->dw_attr_val.v.val_flag = flag;
4359   add_dwarf_attr (die, attr);
4360 }
4361
4362 static inline unsigned
4363 AT_flag (a)
4364      dw_attr_ref a;
4365 {
4366   if (a && AT_class (a) == dw_val_class_flag)
4367     return a->dw_attr_val.v.val_flag;
4368
4369   abort ();
4370 }
4371
4372 /* Add a signed integer attribute value to a DIE.  */
4373
4374 static inline void
4375 add_AT_int (die, attr_kind, int_val)
4376      dw_die_ref die;
4377      enum dwarf_attribute attr_kind;
4378      long int int_val;
4379 {
4380   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4381
4382   attr->dw_attr_next = NULL;
4383   attr->dw_attr = attr_kind;
4384   attr->dw_attr_val.val_class = dw_val_class_const;
4385   attr->dw_attr_val.v.val_int = int_val;
4386   add_dwarf_attr (die, attr);
4387 }
4388
4389 static inline long int
4390 AT_int (a)
4391      dw_attr_ref a;
4392 {
4393   if (a && AT_class (a) == dw_val_class_const)
4394     return a->dw_attr_val.v.val_int;
4395
4396   abort ();
4397 }
4398
4399 /* Add an unsigned integer attribute value to a DIE.  */
4400
4401 static inline void
4402 add_AT_unsigned (die, attr_kind, unsigned_val)
4403      dw_die_ref die;
4404      enum dwarf_attribute attr_kind;
4405      unsigned long unsigned_val;
4406 {
4407   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4408
4409   attr->dw_attr_next = NULL;
4410   attr->dw_attr = attr_kind;
4411   attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4412   attr->dw_attr_val.v.val_unsigned = unsigned_val;
4413   add_dwarf_attr (die, attr);
4414 }
4415
4416 static inline unsigned long
4417 AT_unsigned (a)
4418      dw_attr_ref a;
4419 {
4420   if (a && AT_class (a) == dw_val_class_unsigned_const)
4421     return a->dw_attr_val.v.val_unsigned;
4422
4423   abort ();
4424 }
4425
4426 /* Add an unsigned double integer attribute value to a DIE.  */
4427
4428 static inline void
4429 add_AT_long_long (die, attr_kind, val_hi, val_low)
4430      dw_die_ref die;
4431      enum dwarf_attribute attr_kind;
4432      unsigned long val_hi;
4433      unsigned long val_low;
4434 {
4435   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4436
4437   attr->dw_attr_next = NULL;
4438   attr->dw_attr = attr_kind;
4439   attr->dw_attr_val.val_class = dw_val_class_long_long;
4440   attr->dw_attr_val.v.val_long_long.hi = val_hi;
4441   attr->dw_attr_val.v.val_long_long.low = val_low;
4442   add_dwarf_attr (die, attr);
4443 }
4444
4445 /* Add a floating point attribute value to a DIE and return it.  */
4446
4447 static inline void
4448 add_AT_float (die, attr_kind, length, array)
4449      dw_die_ref die;
4450      enum dwarf_attribute attr_kind;
4451      unsigned length;
4452      long *array;
4453 {
4454   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4455
4456   attr->dw_attr_next = NULL;
4457   attr->dw_attr = attr_kind;
4458   attr->dw_attr_val.val_class = dw_val_class_float;
4459   attr->dw_attr_val.v.val_float.length = length;
4460   attr->dw_attr_val.v.val_float.array = array;
4461   add_dwarf_attr (die, attr);
4462 }
4463
4464 /* Add a string attribute value to a DIE.  */
4465
4466 static inline void
4467 add_AT_string (die, attr_kind, str)
4468      dw_die_ref die;
4469      enum dwarf_attribute attr_kind;
4470      const char *str;
4471 {
4472   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4473   struct indirect_string_node *node;
4474   
4475   if (! debug_str_hash)
4476     {
4477       debug_str_hash = ht_create (10);
4478       debug_str_hash->alloc_node = indirect_string_alloc;
4479     }
4480
4481   node = (struct indirect_string_node *)
4482          ht_lookup (debug_str_hash, (const unsigned char *) str,
4483                     strlen (str), HT_ALLOC);
4484   node->refcount++;
4485
4486   attr->dw_attr_next = NULL;
4487   attr->dw_attr = attr_kind;
4488   attr->dw_attr_val.val_class = dw_val_class_str;
4489   attr->dw_attr_val.v.val_str = node;
4490   add_dwarf_attr (die, attr);
4491 }
4492
4493 static inline const char *
4494 AT_string (a)
4495      dw_attr_ref a;
4496 {
4497   if (a && AT_class (a) == dw_val_class_str)
4498     return (const char *) HT_STR (&a->dw_attr_val.v.val_str->id);
4499
4500   abort ();
4501 }
4502
4503 /* Find out whether a string should be output inline in DIE
4504    or out-of-line in .debug_str section.  */
4505
4506 static int
4507 AT_string_form (a)
4508      dw_attr_ref a;
4509 {
4510   if (a && AT_class (a) == dw_val_class_str)
4511     {
4512       struct indirect_string_node *node;
4513       unsigned int len;
4514       extern int const_labelno;
4515       char label[32];
4516
4517       node = a->dw_attr_val.v.val_str;
4518       if (node->form)
4519         return node->form;
4520
4521       len = HT_LEN (&node->id) + 1;
4522
4523       /* If the string is shorter or equal to the size of the reference, it is
4524          always better to put it inline.  */
4525       if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4526         return node->form = DW_FORM_string;
4527
4528       /* If we cannot expect the linker to merge strings in .debug_str
4529          section, only put it into .debug_str if it is worth even in this
4530          single module.  */
4531       if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4532           && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4533         return node->form = DW_FORM_string;
4534
4535       ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
4536       ++const_labelno;
4537       node->label = xstrdup (label);
4538
4539       return node->form = DW_FORM_strp;
4540     }
4541
4542   abort ();
4543 }
4544
4545 /* Add a DIE reference attribute value to a DIE.  */
4546
4547 static inline void
4548 add_AT_die_ref (die, attr_kind, targ_die)
4549      dw_die_ref die;
4550      enum dwarf_attribute attr_kind;
4551      dw_die_ref targ_die;
4552 {
4553   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4554
4555   attr->dw_attr_next = NULL;
4556   attr->dw_attr = attr_kind;
4557   attr->dw_attr_val.val_class = dw_val_class_die_ref;
4558   attr->dw_attr_val.v.val_die_ref.die = targ_die;
4559   attr->dw_attr_val.v.val_die_ref.external = 0;
4560   add_dwarf_attr (die, attr);
4561 }
4562
4563 static inline dw_die_ref
4564 AT_ref (a)
4565      dw_attr_ref a;
4566 {
4567   if (a && AT_class (a) == dw_val_class_die_ref)
4568     return a->dw_attr_val.v.val_die_ref.die;
4569
4570   abort ();
4571 }
4572
4573 static inline int
4574 AT_ref_external (a)
4575      dw_attr_ref a;
4576 {
4577   if (a && AT_class (a) == dw_val_class_die_ref)
4578     return a->dw_attr_val.v.val_die_ref.external;
4579
4580   return 0;
4581 }
4582
4583 static inline void
4584 set_AT_ref_external (a, i)
4585      dw_attr_ref a;
4586      int i;
4587 {
4588   if (a && AT_class (a) == dw_val_class_die_ref)
4589     a->dw_attr_val.v.val_die_ref.external = i;
4590   else
4591     abort ();
4592 }
4593
4594 /* Add an FDE reference attribute value to a DIE.  */
4595
4596 static inline void
4597 add_AT_fde_ref (die, attr_kind, targ_fde)
4598      dw_die_ref die;
4599      enum dwarf_attribute attr_kind;
4600      unsigned targ_fde;
4601 {
4602   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4603
4604   attr->dw_attr_next = NULL;
4605   attr->dw_attr = attr_kind;
4606   attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4607   attr->dw_attr_val.v.val_fde_index = targ_fde;
4608   add_dwarf_attr (die, attr);
4609 }
4610
4611 /* Add a location description attribute value to a DIE.  */
4612
4613 static inline void
4614 add_AT_loc (die, attr_kind, loc)
4615      dw_die_ref die;
4616      enum dwarf_attribute attr_kind;
4617      dw_loc_descr_ref loc;
4618 {
4619   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4620
4621   attr->dw_attr_next = NULL;
4622   attr->dw_attr = attr_kind;
4623   attr->dw_attr_val.val_class = dw_val_class_loc;
4624   attr->dw_attr_val.v.val_loc = loc;
4625   add_dwarf_attr (die, attr);
4626 }
4627
4628 static inline dw_loc_descr_ref
4629 AT_loc (a)
4630      dw_attr_ref a;
4631 {
4632   if (a && AT_class (a) == dw_val_class_loc)
4633     return a->dw_attr_val.v.val_loc;
4634
4635   abort ();
4636 }
4637
4638 static inline void
4639 add_AT_loc_list (die, attr_kind, loc_list)
4640      dw_die_ref die;
4641      enum dwarf_attribute attr_kind;
4642      dw_loc_list_ref loc_list;
4643 {
4644   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4645
4646   attr->dw_attr_next = NULL;
4647   attr->dw_attr = attr_kind;
4648   attr->dw_attr_val.val_class = dw_val_class_loc_list;
4649   attr->dw_attr_val.v.val_loc_list = loc_list;
4650   add_dwarf_attr (die, attr);
4651   have_location_lists = 1;
4652 }
4653
4654 static inline dw_loc_list_ref
4655 AT_loc_list (a)
4656      dw_attr_ref a;
4657 {
4658   if (a && AT_class (a) == dw_val_class_loc_list)
4659     return a->dw_attr_val.v.val_loc_list;
4660
4661   abort ();
4662 }
4663
4664 /* Add an address constant attribute value to a DIE.  */
4665
4666 static inline void
4667 add_AT_addr (die, attr_kind, addr)
4668      dw_die_ref die;
4669      enum dwarf_attribute attr_kind;
4670      rtx addr;
4671 {
4672   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4673
4674   attr->dw_attr_next = NULL;
4675   attr->dw_attr = attr_kind;
4676   attr->dw_attr_val.val_class = dw_val_class_addr;
4677   attr->dw_attr_val.v.val_addr = addr;
4678   add_dwarf_attr (die, attr);
4679 }
4680
4681 static inline rtx
4682 AT_addr (a)
4683      dw_attr_ref a;
4684 {
4685   if (a && AT_class (a) == dw_val_class_addr)
4686     return a->dw_attr_val.v.val_addr;
4687
4688   abort ();
4689 }
4690
4691 /* Add a label identifier attribute value to a DIE.  */
4692
4693 static inline void
4694 add_AT_lbl_id (die, attr_kind, lbl_id)
4695      dw_die_ref die;
4696      enum dwarf_attribute attr_kind;
4697      const char *lbl_id;
4698 {
4699   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4700
4701   attr->dw_attr_next = NULL;
4702   attr->dw_attr = attr_kind;
4703   attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4704   attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4705   add_dwarf_attr (die, attr);
4706 }
4707
4708 /* Add a section offset attribute value to a DIE.  */
4709
4710 static inline void
4711 add_AT_lbl_offset (die, attr_kind, label)
4712      dw_die_ref die;
4713      enum dwarf_attribute attr_kind;
4714      const char *label;
4715 {
4716   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4717
4718   attr->dw_attr_next = NULL;
4719   attr->dw_attr = attr_kind;
4720   attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4721   attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4722   add_dwarf_attr (die, attr);
4723 }
4724
4725 /* Add an offset attribute value to a DIE.  */
4726
4727 static inline void
4728 add_AT_offset (die, attr_kind, offset)
4729      dw_die_ref die;
4730      enum dwarf_attribute attr_kind;
4731      unsigned long offset;
4732 {
4733   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4734
4735   attr->dw_attr_next = NULL;
4736   attr->dw_attr = attr_kind;
4737   attr->dw_attr_val.val_class = dw_val_class_offset;
4738   attr->dw_attr_val.v.val_offset = offset;
4739   add_dwarf_attr (die, attr);
4740 }
4741
4742 /* Add an range_list attribute value to a DIE.  */
4743
4744 static void
4745 add_AT_range_list (die, attr_kind, offset)
4746      dw_die_ref die;
4747      enum dwarf_attribute attr_kind;
4748      unsigned long offset;
4749 {
4750   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4751
4752   attr->dw_attr_next = NULL;
4753   attr->dw_attr = attr_kind;
4754   attr->dw_attr_val.val_class = dw_val_class_range_list;
4755   attr->dw_attr_val.v.val_offset = offset;
4756   add_dwarf_attr (die, attr);
4757 }
4758
4759 static inline const char *
4760 AT_lbl (a)
4761      dw_attr_ref a;
4762 {
4763   if (a && (AT_class (a) == dw_val_class_lbl_id
4764             || AT_class (a) == dw_val_class_lbl_offset))
4765     return a->dw_attr_val.v.val_lbl_id;
4766
4767   abort ();
4768 }
4769
4770 /* Get the attribute of type attr_kind.  */
4771
4772 static inline dw_attr_ref
4773 get_AT (die, attr_kind)
4774      dw_die_ref die;
4775      enum dwarf_attribute attr_kind;
4776 {
4777   dw_attr_ref a;
4778   dw_die_ref spec = NULL;
4779
4780   if (die != NULL)
4781     {
4782       for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4783         if (a->dw_attr == attr_kind)
4784           return a;
4785         else if (a->dw_attr == DW_AT_specification
4786                  || a->dw_attr == DW_AT_abstract_origin)
4787           spec = AT_ref (a);
4788
4789       if (spec)
4790         return get_AT (spec, attr_kind);
4791     }
4792
4793   return NULL;
4794 }
4795
4796 /* Return the "low pc" attribute value, typically associated with a subprogram
4797    DIE.  Return null if the "low pc" attribute is either not present, or if it
4798    cannot be represented as an assembler label identifier.  */
4799
4800 static inline const char *
4801 get_AT_low_pc (die)
4802      dw_die_ref die;
4803 {
4804   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4805
4806   return a ? AT_lbl (a) : NULL;
4807 }
4808
4809 /* Return the "high pc" attribute value, typically associated with a subprogram
4810    DIE.  Return null if the "high pc" attribute is either not present, or if it
4811    cannot be represented as an assembler label identifier.  */
4812
4813 static inline const char *
4814 get_AT_hi_pc (die)
4815      dw_die_ref die;
4816 {
4817   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4818
4819   return a ? AT_lbl (a) : NULL;
4820 }
4821
4822 /* Return the value of the string attribute designated by ATTR_KIND, or
4823    NULL if it is not present.  */
4824
4825 static inline const char *
4826 get_AT_string (die, attr_kind)
4827      dw_die_ref die;
4828      enum dwarf_attribute attr_kind;
4829 {
4830   dw_attr_ref a = get_AT (die, attr_kind);
4831
4832   return a ? AT_string (a) : NULL;
4833 }
4834
4835 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4836    if it is not present.  */
4837
4838 static inline int
4839 get_AT_flag (die, attr_kind)
4840      dw_die_ref die;
4841      enum dwarf_attribute attr_kind;
4842 {
4843   dw_attr_ref a = get_AT (die, attr_kind);
4844
4845   return a ? AT_flag (a) : 0;
4846 }
4847
4848 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4849    if it is not present.  */
4850
4851 static inline unsigned
4852 get_AT_unsigned (die, attr_kind)
4853      dw_die_ref die;
4854      enum dwarf_attribute attr_kind;
4855 {
4856   dw_attr_ref a = get_AT (die, attr_kind);
4857
4858   return a ? AT_unsigned (a) : 0;
4859 }
4860
4861 static inline dw_die_ref
4862 get_AT_ref (die, attr_kind)
4863      dw_die_ref die;
4864      enum dwarf_attribute attr_kind;
4865 {
4866   dw_attr_ref a = get_AT (die, attr_kind);
4867
4868   return a ? AT_ref (a) : NULL;
4869 }
4870
4871 static inline int
4872 is_c_family ()
4873 {
4874   unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4875
4876   return (lang == DW_LANG_C || lang == DW_LANG_C89
4877           || lang == DW_LANG_C_plus_plus);
4878 }
4879
4880 static inline int
4881 is_cxx ()
4882 {
4883   return (get_AT_unsigned (comp_unit_die, DW_AT_language)
4884           == DW_LANG_C_plus_plus);
4885 }  
4886
4887 static inline int
4888 is_fortran ()
4889 {
4890   unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4891
4892   return (lang == DW_LANG_Fortran77 || lang == DW_LANG_Fortran90);
4893 }
4894
4895 static inline int
4896 is_java ()
4897 {
4898   unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4899
4900   return (lang == DW_LANG_Java);
4901 }
4902
4903 /* Free up the memory used by A.  */
4904
4905 static inline void free_AT PARAMS ((dw_attr_ref));
4906 static inline void
4907 free_AT (a)
4908      dw_attr_ref a;
4909 {
4910   switch (AT_class (a))
4911     {
4912     case dw_val_class_str:
4913       if (a->dw_attr_val.v.val_str->refcount)
4914         a->dw_attr_val.v.val_str->refcount--;
4915       break;
4916
4917     case dw_val_class_lbl_id:
4918     case dw_val_class_lbl_offset:
4919       free (a->dw_attr_val.v.val_lbl_id);
4920       break;
4921
4922     case dw_val_class_float:
4923       free (a->dw_attr_val.v.val_float.array);
4924       break;
4925
4926     default:
4927       break;
4928     }
4929
4930   free (a);
4931 }
4932
4933 /* Remove the specified attribute if present.  */
4934
4935 static void
4936 remove_AT (die, attr_kind)
4937      dw_die_ref die;
4938      enum dwarf_attribute attr_kind;
4939 {
4940   dw_attr_ref *p;
4941   dw_attr_ref removed = NULL;
4942
4943   if (die != NULL)
4944     {
4945       for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
4946         if ((*p)->dw_attr == attr_kind)
4947           {
4948             removed = *p;
4949             *p = (*p)->dw_attr_next;
4950             break;
4951           }
4952
4953       if (removed != 0)
4954         free_AT (removed);
4955     }
4956 }
4957
4958 /* Free up the memory used by DIE.  */
4959
4960 static inline void
4961 free_die (die)
4962      dw_die_ref die;
4963 {
4964   remove_children (die);
4965   free (die);
4966 }
4967
4968 /* Discard the children of this DIE.  */
4969
4970 static void
4971 remove_children (die)
4972      dw_die_ref die;
4973 {
4974   dw_die_ref child_die = die->die_child;
4975
4976   die->die_child = NULL;
4977
4978   while (child_die != NULL)
4979     {
4980       dw_die_ref tmp_die = child_die;
4981       dw_attr_ref a;
4982
4983       child_die = child_die->die_sib;
4984
4985       for (a = tmp_die->die_attr; a != NULL;)
4986         {
4987           dw_attr_ref tmp_a = a;
4988
4989           a = a->dw_attr_next;
4990           free_AT (tmp_a);
4991         }
4992
4993       free_die (tmp_die);
4994     }
4995 }
4996
4997 /* Add a child DIE below its parent.  We build the lists up in reverse
4998    addition order, and correct that in reverse_all_dies.  */
4999
5000 static inline void
5001 add_child_die (die, child_die)
5002      dw_die_ref die;
5003      dw_die_ref child_die;
5004 {
5005   if (die != NULL && child_die != NULL)
5006     {
5007       if (die == child_die)
5008         abort ();
5009
5010       child_die->die_parent = die;
5011       child_die->die_sib = die->die_child;
5012       die->die_child = child_die;
5013     }
5014 }
5015
5016 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5017    is the specification, to the front of PARENT's list of children.  */
5018
5019 static void
5020 splice_child_die (parent, child)
5021      dw_die_ref parent, child;
5022 {
5023   dw_die_ref *p;
5024
5025   /* We want the declaration DIE from inside the class, not the
5026      specification DIE at toplevel.  */
5027   if (child->die_parent != parent)
5028     {
5029       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5030
5031       if (tmp)
5032         child = tmp;
5033     }
5034
5035   if (child->die_parent != parent
5036       && child->die_parent != get_AT_ref (parent, DW_AT_specification))
5037     abort ();
5038
5039   for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5040     if (*p == child)
5041       {
5042         *p = child->die_sib;
5043         break;
5044       }
5045
5046   child->die_sib = parent->die_child;
5047   parent->die_child = child;
5048 }
5049
5050 /* Return a pointer to a newly created DIE node.  */
5051
5052 static inline dw_die_ref
5053 new_die (tag_value, parent_die, t)
5054      enum dwarf_tag tag_value;
5055      dw_die_ref parent_die;
5056      tree t;
5057 {
5058   dw_die_ref die = (dw_die_ref) xcalloc (1, sizeof (die_node));
5059
5060   die->die_tag = tag_value;
5061
5062   if (parent_die != NULL)
5063     add_child_die (parent_die, die);
5064   else
5065     {
5066       limbo_die_node *limbo_node;
5067
5068       limbo_node = (limbo_die_node *) xmalloc (sizeof (limbo_die_node));
5069       limbo_node->die = die;
5070       limbo_node->created_for = t;
5071       limbo_node->next = limbo_die_list;
5072       limbo_die_list = limbo_node;
5073     }
5074
5075   return die;
5076 }
5077
5078 /* Return the DIE associated with the given type specifier.  */
5079
5080 static inline dw_die_ref
5081 lookup_type_die (type)
5082      tree type;
5083 {
5084   if (TREE_CODE (type) == VECTOR_TYPE)
5085     type = TYPE_DEBUG_REPRESENTATION_TYPE (type);
5086
5087   return (dw_die_ref) TYPE_SYMTAB_POINTER (type);
5088 }
5089
5090 /* Equate a DIE to a given type specifier.  */
5091
5092 static inline void
5093 equate_type_number_to_die (type, type_die)
5094      tree type;
5095      dw_die_ref type_die;
5096 {
5097   TYPE_SYMTAB_POINTER (type) = (char *) type_die;
5098 }
5099
5100 /* Return the DIE associated with a given declaration.  */
5101
5102 static inline dw_die_ref
5103 lookup_decl_die (decl)
5104      tree decl;
5105 {
5106   unsigned decl_id = DECL_UID (decl);
5107
5108   return (decl_id < decl_die_table_in_use ? decl_die_table[decl_id] : NULL);
5109 }
5110
5111 /* Equate a DIE to a particular declaration.  */
5112
5113 static void
5114 equate_decl_number_to_die (decl, decl_die)
5115      tree decl;
5116      dw_die_ref decl_die;
5117 {
5118   unsigned decl_id = DECL_UID (decl);
5119   unsigned num_allocated;
5120
5121   if (decl_id >= decl_die_table_allocated)
5122     {
5123       num_allocated
5124         = ((decl_id + 1 + DECL_DIE_TABLE_INCREMENT - 1)
5125            / DECL_DIE_TABLE_INCREMENT)
5126           * DECL_DIE_TABLE_INCREMENT;
5127
5128       decl_die_table
5129         = (dw_die_ref *) xrealloc (decl_die_table,
5130                                    sizeof (dw_die_ref) * num_allocated);
5131
5132       memset ((char *) &decl_die_table[decl_die_table_allocated], 0,
5133              (num_allocated - decl_die_table_allocated) * sizeof (dw_die_ref));
5134       decl_die_table_allocated = num_allocated;
5135     }
5136
5137   if (decl_id >= decl_die_table_in_use)
5138     decl_die_table_in_use = (decl_id + 1);
5139
5140   decl_die_table[decl_id] = decl_die;
5141 }
5142 \f
5143 /* Keep track of the number of spaces used to indent the
5144    output of the debugging routines that print the structure of
5145    the DIE internal representation.  */
5146 static int print_indent;
5147
5148 /* Indent the line the number of spaces given by print_indent.  */
5149
5150 static inline void
5151 print_spaces (outfile)
5152      FILE *outfile;
5153 {
5154   fprintf (outfile, "%*s", print_indent, "");
5155 }
5156
5157 /* Print the information associated with a given DIE, and its children.
5158    This routine is a debugging aid only.  */
5159
5160 static void
5161 print_die (die, outfile)
5162      dw_die_ref die;
5163      FILE *outfile;
5164 {
5165   dw_attr_ref a;
5166   dw_die_ref c;
5167
5168   print_spaces (outfile);
5169   fprintf (outfile, "DIE %4lu: %s\n",
5170            die->die_offset, dwarf_tag_name (die->die_tag));
5171   print_spaces (outfile);
5172   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5173   fprintf (outfile, " offset: %lu\n", die->die_offset);
5174
5175   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5176     {
5177       print_spaces (outfile);
5178       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5179
5180       switch (AT_class (a))
5181         {
5182         case dw_val_class_addr:
5183           fprintf (outfile, "address");
5184           break;
5185         case dw_val_class_offset:
5186           fprintf (outfile, "offset");
5187           break;
5188         case dw_val_class_loc:
5189           fprintf (outfile, "location descriptor");
5190           break;
5191         case dw_val_class_loc_list:
5192           fprintf (outfile, "location list -> label:%s",
5193                    AT_loc_list (a)->ll_symbol);
5194           break;
5195         case dw_val_class_range_list:
5196           fprintf (outfile, "range list");
5197           break;
5198         case dw_val_class_const:
5199           fprintf (outfile, "%ld", AT_int (a));
5200           break;
5201         case dw_val_class_unsigned_const:
5202           fprintf (outfile, "%lu", AT_unsigned (a));
5203           break;
5204         case dw_val_class_long_long:
5205           fprintf (outfile, "constant (%lu,%lu)",
5206                    a->dw_attr_val.v.val_long_long.hi,
5207                    a->dw_attr_val.v.val_long_long.low);
5208           break;
5209         case dw_val_class_float:
5210           fprintf (outfile, "floating-point constant");
5211           break;
5212         case dw_val_class_flag:
5213           fprintf (outfile, "%u", AT_flag (a));
5214           break;
5215         case dw_val_class_die_ref:
5216           if (AT_ref (a) != NULL)
5217             {
5218               if (AT_ref (a)->die_symbol)
5219                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5220               else
5221                 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5222             }
5223           else
5224             fprintf (outfile, "die -> <null>");
5225           break;
5226         case dw_val_class_lbl_id:
5227         case dw_val_class_lbl_offset:
5228           fprintf (outfile, "label: %s", AT_lbl (a));
5229           break;
5230         case dw_val_class_str:
5231           if (AT_string (a) != NULL)
5232             fprintf (outfile, "\"%s\"", AT_string (a));
5233           else
5234             fprintf (outfile, "<null>");
5235           break;
5236         default:
5237           break;
5238         }
5239
5240       fprintf (outfile, "\n");
5241     }
5242
5243   if (die->die_child != NULL)
5244     {
5245       print_indent += 4;
5246       for (c = die->die_child; c != NULL; c = c->die_sib)
5247         print_die (c, outfile);
5248
5249       print_indent -= 4;
5250     }
5251   if (print_indent == 0)
5252     fprintf (outfile, "\n");
5253 }
5254
5255 /* Print the contents of the source code line number correspondence table.
5256    This routine is a debugging aid only.  */
5257
5258 static void
5259 print_dwarf_line_table (outfile)
5260      FILE *outfile;
5261 {
5262   unsigned i;
5263   dw_line_info_ref line_info;
5264
5265   fprintf (outfile, "\n\nDWARF source line information\n");
5266   for (i = 1; i < line_info_table_in_use; i++)
5267     {
5268       line_info = &line_info_table[i];
5269       fprintf (outfile, "%5d: ", i);
5270       fprintf (outfile, "%-20s", file_table.table[line_info->dw_file_num]);
5271       fprintf (outfile, "%6ld", line_info->dw_line_num);
5272       fprintf (outfile, "\n");
5273     }
5274
5275   fprintf (outfile, "\n\n");
5276 }
5277
5278 /* Print the information collected for a given DIE.  */
5279
5280 void
5281 debug_dwarf_die (die)
5282      dw_die_ref die;
5283 {
5284   print_die (die, stderr);
5285 }
5286
5287 /* Print all DWARF information collected for the compilation unit.
5288    This routine is a debugging aid only.  */
5289
5290 void
5291 debug_dwarf ()
5292 {
5293   print_indent = 0;
5294   print_die (comp_unit_die, stderr);
5295   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5296     print_dwarf_line_table (stderr);
5297 }
5298 \f
5299 /* We build up the lists of children and attributes by pushing new ones
5300    onto the beginning of the list.  Reverse the lists for DIE so that
5301    they are in order of addition.  */
5302
5303 static void
5304 reverse_die_lists (die)
5305      dw_die_ref die;
5306 {
5307   dw_die_ref c, cp, cn;
5308   dw_attr_ref a, ap, an;
5309
5310   for (a = die->die_attr, ap = 0; a; a = an)
5311     {
5312       an = a->dw_attr_next;
5313       a->dw_attr_next = ap;
5314       ap = a;
5315     }
5316
5317   die->die_attr = ap;
5318
5319   for (c = die->die_child, cp = 0; c; c = cn)
5320     {
5321       cn = c->die_sib;
5322       c->die_sib = cp;
5323       cp = c;
5324     }
5325
5326   die->die_child = cp;
5327 }
5328
5329 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5330    reverse all dies in add_sibling_attributes, which runs through all the dies,
5331    it would reverse all the dies.  Now, however, since we don't call
5332    reverse_die_lists in add_sibling_attributes, we need a routine to
5333    recursively reverse all the dies. This is that routine.  */
5334
5335 static void
5336 reverse_all_dies (die)
5337      dw_die_ref die;
5338 {
5339   dw_die_ref c;
5340
5341   reverse_die_lists (die);
5342
5343   for (c = die->die_child; c; c = c->die_sib)
5344     reverse_all_dies (c);
5345 }
5346
5347 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5348    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5349    DIE that marks the start of the DIEs for this include file.  */
5350
5351 static dw_die_ref
5352 push_new_compile_unit (old_unit, bincl_die)
5353      dw_die_ref old_unit, bincl_die;
5354 {
5355   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5356   dw_die_ref new_unit = gen_compile_unit_die (filename);
5357
5358   new_unit->die_sib = old_unit;
5359   return new_unit;
5360 }
5361
5362 /* Close an include-file CU and reopen the enclosing one.  */
5363
5364 static dw_die_ref
5365 pop_compile_unit (old_unit)
5366      dw_die_ref old_unit;
5367 {
5368   dw_die_ref new_unit = old_unit->die_sib;
5369
5370   old_unit->die_sib = NULL;
5371   return new_unit;
5372 }
5373
5374 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5375 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5376
5377 /* Calculate the checksum of a location expression.  */
5378
5379 static inline void
5380 loc_checksum (loc, ctx)
5381      dw_loc_descr_ref loc;
5382      struct md5_ctx *ctx;
5383 {
5384   CHECKSUM (loc->dw_loc_opc);
5385   CHECKSUM (loc->dw_loc_oprnd1);
5386   CHECKSUM (loc->dw_loc_oprnd2);
5387 }
5388
5389 /* Calculate the checksum of an attribute.  */
5390
5391 static void
5392 attr_checksum (at, ctx)
5393      dw_attr_ref at;
5394      struct md5_ctx *ctx;
5395 {
5396   dw_loc_descr_ref loc;
5397   rtx r;
5398
5399   CHECKSUM (at->dw_attr);
5400
5401   /* We don't care about differences in file numbering.  */
5402   if (at->dw_attr == DW_AT_decl_file
5403       /* Or that this was compiled with a different compiler snapshot; if
5404          the output is the same, that's what matters.  */
5405       || at->dw_attr == DW_AT_producer)
5406     return;
5407
5408   switch (AT_class (at))
5409     {
5410     case dw_val_class_const:
5411       CHECKSUM (at->dw_attr_val.v.val_int);
5412       break;
5413     case dw_val_class_unsigned_const:
5414       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5415       break;
5416     case dw_val_class_long_long:
5417       CHECKSUM (at->dw_attr_val.v.val_long_long);
5418       break;
5419     case dw_val_class_float:
5420       CHECKSUM (at->dw_attr_val.v.val_float);
5421       break;
5422     case dw_val_class_flag:
5423       CHECKSUM (at->dw_attr_val.v.val_flag);
5424       break;
5425     case dw_val_class_str:
5426       CHECKSUM_STRING (AT_string (at));
5427       break;
5428
5429     case dw_val_class_addr:
5430       r = AT_addr (at);
5431       switch (GET_CODE (r))
5432         {
5433         case SYMBOL_REF:
5434           CHECKSUM_STRING (XSTR (r, 0));
5435           break;
5436
5437         default:
5438           abort ();
5439         }
5440       break;
5441
5442     case dw_val_class_offset:
5443       CHECKSUM (at->dw_attr_val.v.val_offset);
5444       break;
5445
5446     case dw_val_class_loc:
5447       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5448         loc_checksum (loc, ctx);
5449       break;
5450
5451     case dw_val_class_die_ref:
5452       if (AT_ref (at)->die_offset)
5453         CHECKSUM (AT_ref (at)->die_offset);
5454       /* FIXME else use target die name or something.  */
5455
5456     case dw_val_class_fde_ref:
5457     case dw_val_class_lbl_id:
5458     case dw_val_class_lbl_offset:
5459       break;
5460
5461     default:
5462       break;
5463     }
5464 }
5465
5466 /* Calculate the checksum of a DIE.  */
5467
5468 static void
5469 die_checksum (die, ctx)
5470      dw_die_ref die;
5471      struct md5_ctx *ctx;
5472 {
5473   dw_die_ref c;
5474   dw_attr_ref a;
5475
5476   CHECKSUM (die->die_tag);
5477
5478   for (a = die->die_attr; a; a = a->dw_attr_next)
5479     attr_checksum (a, ctx);
5480
5481   for (c = die->die_child; c; c = c->die_sib)
5482     die_checksum (c, ctx);
5483 }
5484
5485 #undef CHECKSUM
5486 #undef CHECKSUM_STRING
5487
5488 /* The prefix to attach to symbols on DIEs in the current comdat debug
5489    info section.  */
5490 static char *comdat_symbol_id;
5491
5492 /* The index of the current symbol within the current comdat CU.  */
5493 static unsigned int comdat_symbol_number;
5494
5495 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5496    children, and set comdat_symbol_id accordingly.  */
5497
5498 static void
5499 compute_section_prefix (unit_die)
5500      dw_die_ref unit_die;
5501 {
5502   const char *base = lbasename (get_AT_string (unit_die, DW_AT_name));
5503   char *name = (char *) alloca (strlen (base) + 64);
5504   char *p;
5505   int i;
5506   unsigned char checksum[16];
5507   struct md5_ctx ctx;
5508
5509   /* Compute the checksum of the DIE, then append part of it as hex digits to
5510      the name filename of the unit.  */
5511
5512   md5_init_ctx (&ctx);
5513   die_checksum (unit_die, &ctx);
5514   md5_finish_ctx (&ctx, checksum);
5515
5516   sprintf (name, "%s.", base);
5517   clean_symbol_name (name);
5518
5519   p = name + strlen (name);
5520   for (i = 0; i < 4; i++)
5521     {
5522       sprintf (p, "%.2x", checksum[i]);
5523       p += 2;
5524     }
5525
5526   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5527   comdat_symbol_number = 0;
5528 }
5529
5530 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
5531
5532 static int
5533 is_type_die (die)
5534      dw_die_ref die;
5535 {
5536   switch (die->die_tag)
5537     {
5538     case DW_TAG_array_type:
5539     case DW_TAG_class_type:
5540     case DW_TAG_enumeration_type:
5541     case DW_TAG_pointer_type:
5542     case DW_TAG_reference_type:
5543     case DW_TAG_string_type:
5544     case DW_TAG_structure_type:
5545     case DW_TAG_subroutine_type:
5546     case DW_TAG_union_type:
5547     case DW_TAG_ptr_to_member_type:
5548     case DW_TAG_set_type:
5549     case DW_TAG_subrange_type:
5550     case DW_TAG_base_type:
5551     case DW_TAG_const_type:
5552     case DW_TAG_file_type:
5553     case DW_TAG_packed_type:
5554     case DW_TAG_volatile_type:
5555       return 1;
5556     default:
5557       return 0;
5558     }
5559 }
5560
5561 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5562    Basically, we want to choose the bits that are likely to be shared between
5563    compilations (types) and leave out the bits that are specific to individual
5564    compilations (functions).  */
5565
5566 static int
5567 is_comdat_die (c)
5568      dw_die_ref c;
5569 {
5570   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5571      we do for stabs.  The advantage is a greater likelihood of sharing between
5572      objects that don't include headers in the same order (and therefore would
5573      put the base types in a different comdat).  jason 8/28/00 */
5574
5575   if (c->die_tag == DW_TAG_base_type)
5576     return 0;
5577
5578   if (c->die_tag == DW_TAG_pointer_type
5579       || c->die_tag == DW_TAG_reference_type
5580       || c->die_tag == DW_TAG_const_type
5581       || c->die_tag == DW_TAG_volatile_type)
5582     {
5583       dw_die_ref t = get_AT_ref (c, DW_AT_type);
5584
5585       return t ? is_comdat_die (t) : 0;
5586     }
5587
5588   return is_type_die (c);
5589 }
5590
5591 /* Returns 1 iff C is the sort of DIE that might be referred to from another
5592    compilation unit.  */
5593
5594 static int
5595 is_symbol_die (c)
5596      dw_die_ref c;
5597 {
5598   return (is_type_die (c)
5599           || (get_AT (c, DW_AT_declaration) 
5600               && !get_AT (c, DW_AT_specification)));
5601 }
5602
5603 static char *
5604 gen_internal_sym (prefix)
5605      const char *prefix;
5606 {
5607   char buf[256];
5608   static int label_num;
5609
5610   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
5611   return xstrdup (buf);
5612 }
5613
5614 /* Assign symbols to all worthy DIEs under DIE.  */
5615
5616 static void
5617 assign_symbol_names (die)
5618      dw_die_ref die;
5619 {
5620   dw_die_ref c;
5621
5622   if (is_symbol_die (die))
5623     {
5624       if (comdat_symbol_id)
5625         {
5626           char *p = alloca (strlen (comdat_symbol_id) + 64);
5627
5628           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
5629                    comdat_symbol_id, comdat_symbol_number++);
5630           die->die_symbol = xstrdup (p);
5631         }
5632       else
5633         die->die_symbol = gen_internal_sym ("LDIE");
5634     }
5635
5636   for (c = die->die_child; c != NULL; c = c->die_sib)
5637     assign_symbol_names (c);
5638 }
5639
5640 /* Traverse the DIE (which is always comp_unit_die), and set up
5641    additional compilation units for each of the include files we see
5642    bracketed by BINCL/EINCL.  */
5643
5644 static void
5645 break_out_includes (die)
5646      dw_die_ref die;
5647 {
5648   dw_die_ref *ptr;
5649   dw_die_ref unit = NULL;
5650   limbo_die_node *node;
5651
5652   for (ptr = &(die->die_child); *ptr; )
5653     {
5654       dw_die_ref c = *ptr;
5655
5656       if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
5657           || (unit && is_comdat_die (c)))
5658         {
5659           /* This DIE is for a secondary CU; remove it from the main one.  */
5660           *ptr = c->die_sib;
5661
5662           if (c->die_tag == DW_TAG_GNU_BINCL)
5663             {
5664               unit = push_new_compile_unit (unit, c);
5665               free_die (c);
5666             }
5667           else if (c->die_tag == DW_TAG_GNU_EINCL)
5668             {
5669               unit = pop_compile_unit (unit);
5670               free_die (c);
5671             }
5672           else
5673             add_child_die (unit, c);
5674         }
5675       else
5676         {
5677           /* Leave this DIE in the main CU.  */
5678           ptr = &(c->die_sib);
5679           continue;
5680         }
5681     }
5682
5683 #if 0
5684   /* We can only use this in debugging, since the frontend doesn't check
5685      to make sure that we leave every include file we enter.  */
5686   if (unit != NULL)
5687     abort ();
5688 #endif
5689
5690   assign_symbol_names (die);
5691   for (node = limbo_die_list; node; node = node->next)
5692     {
5693       compute_section_prefix (node->die);
5694       assign_symbol_names (node->die);
5695     }
5696 }
5697
5698 /* Traverse the DIE and add a sibling attribute if it may have the
5699    effect of speeding up access to siblings.  To save some space,
5700    avoid generating sibling attributes for DIE's without children.  */
5701
5702 static void
5703 add_sibling_attributes (die)
5704      dw_die_ref die;
5705 {
5706   dw_die_ref c;
5707
5708   if (die->die_tag != DW_TAG_compile_unit
5709       && die->die_sib && die->die_child != NULL)
5710     /* Add the sibling link to the front of the attribute list.  */
5711     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
5712
5713   for (c = die->die_child; c != NULL; c = c->die_sib)
5714     add_sibling_attributes (c);
5715 }
5716
5717 /* Output all location lists for the DIE and its children.  */
5718
5719 static void
5720 output_location_lists (die)
5721      dw_die_ref die;
5722 {
5723   dw_die_ref c;
5724   dw_attr_ref d_attr;
5725
5726   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
5727     if (AT_class (d_attr) == dw_val_class_loc_list)
5728       output_loc_list (AT_loc_list (d_attr));
5729
5730   for (c = die->die_child; c != NULL; c = c->die_sib)
5731     output_location_lists (c);
5732
5733 }
5734 /* The format of each DIE (and its attribute value pairs) is encoded in an
5735    abbreviation table.  This routine builds the abbreviation table and assigns
5736    a unique abbreviation id for each abbreviation entry.  The children of each
5737    die are visited recursively.  */
5738
5739 static void
5740 build_abbrev_table (die)
5741      dw_die_ref die;
5742 {
5743   unsigned long abbrev_id;
5744   unsigned int n_alloc;
5745   dw_die_ref c;
5746   dw_attr_ref d_attr, a_attr;
5747
5748   /* Scan the DIE references, and mark as external any that refer to
5749      DIEs from other CUs (i.e. those which are not marked).  */
5750   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
5751     if (AT_class (d_attr) == dw_val_class_die_ref
5752         && AT_ref (d_attr)->die_mark == 0)
5753       {
5754         if (AT_ref (d_attr)->die_symbol == 0)
5755           abort ();
5756
5757         set_AT_ref_external (d_attr, 1);
5758       }
5759
5760   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
5761     {
5762       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
5763
5764       if (abbrev->die_tag == die->die_tag)
5765         {
5766           if ((abbrev->die_child != NULL) == (die->die_child != NULL))
5767             {
5768               a_attr = abbrev->die_attr;
5769               d_attr = die->die_attr;
5770
5771               while (a_attr != NULL && d_attr != NULL)
5772                 {
5773                   if ((a_attr->dw_attr != d_attr->dw_attr)
5774                       || (value_format (a_attr) != value_format (d_attr)))
5775                     break;
5776
5777                   a_attr = a_attr->dw_attr_next;
5778                   d_attr = d_attr->dw_attr_next;
5779                 }
5780
5781               if (a_attr == NULL && d_attr == NULL)
5782                 break;
5783             }
5784         }
5785     }
5786
5787   if (abbrev_id >= abbrev_die_table_in_use)
5788     {
5789       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
5790         {
5791           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
5792           abbrev_die_table
5793             = (dw_die_ref *) xrealloc (abbrev_die_table,
5794                                        sizeof (dw_die_ref) * n_alloc);
5795
5796           memset ((char *) &abbrev_die_table[abbrev_die_table_allocated], 0,
5797                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
5798           abbrev_die_table_allocated = n_alloc;
5799         }
5800
5801       ++abbrev_die_table_in_use;
5802       abbrev_die_table[abbrev_id] = die;
5803     }
5804
5805   die->die_abbrev = abbrev_id;
5806   for (c = die->die_child; c != NULL; c = c->die_sib)
5807     build_abbrev_table (c);
5808 }
5809 \f
5810 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
5811
5812 static int
5813 constant_size (value)
5814      long unsigned value;
5815 {
5816   int log;
5817
5818   if (value == 0)
5819     log = 0;
5820   else
5821     log = floor_log2 (value);
5822
5823   log = log / 8;
5824   log = 1 << (floor_log2 (log) + 1);
5825
5826   return log;
5827 }
5828
5829 /* Return the size of a DIE as it is represented in the
5830    .debug_info section.  */
5831
5832 static unsigned long
5833 size_of_die (die)
5834      dw_die_ref die;
5835 {
5836   unsigned long size = 0;
5837   dw_attr_ref a;
5838
5839   size += size_of_uleb128 (die->die_abbrev);
5840   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5841     {
5842       switch (AT_class (a))
5843         {
5844         case dw_val_class_addr:
5845           size += DWARF2_ADDR_SIZE;
5846           break;
5847         case dw_val_class_offset:
5848           size += DWARF_OFFSET_SIZE;
5849           break;
5850         case dw_val_class_loc:
5851           {
5852             unsigned long lsize = size_of_locs (AT_loc (a));
5853
5854             /* Block length.  */
5855             size += constant_size (lsize);
5856             size += lsize;
5857           }
5858           break;
5859         case dw_val_class_loc_list:
5860           size += DWARF_OFFSET_SIZE;
5861           break;
5862         case dw_val_class_range_list:
5863           size += DWARF_OFFSET_SIZE;
5864           break;
5865         case dw_val_class_const:
5866           size += size_of_sleb128 (AT_int (a));
5867           break;
5868         case dw_val_class_unsigned_const:
5869           size += constant_size (AT_unsigned (a));
5870           break;
5871         case dw_val_class_long_long:
5872           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
5873           break;
5874         case dw_val_class_float:
5875           size += 1 + a->dw_attr_val.v.val_float.length * 4; /* block */
5876           break;
5877         case dw_val_class_flag:
5878           size += 1;
5879           break;
5880         case dw_val_class_die_ref:
5881           size += DWARF_OFFSET_SIZE;
5882           break;
5883         case dw_val_class_fde_ref:
5884           size += DWARF_OFFSET_SIZE;
5885           break;
5886         case dw_val_class_lbl_id:
5887           size += DWARF2_ADDR_SIZE;
5888           break;
5889         case dw_val_class_lbl_offset:
5890           size += DWARF_OFFSET_SIZE;
5891           break;
5892         case dw_val_class_str:
5893           if (AT_string_form (a) == DW_FORM_strp)
5894             size += DWARF_OFFSET_SIZE;
5895           else
5896             size += HT_LEN (&a->dw_attr_val.v.val_str->id) + 1;
5897           break;
5898         default:
5899           abort ();
5900         }
5901     }
5902
5903   return size;
5904 }
5905
5906 /* Size the debugging information associated with a given DIE.  Visits the
5907    DIE's children recursively.  Updates the global variable next_die_offset, on
5908    each time through.  Uses the current value of next_die_offset to update the
5909    die_offset field in each DIE.  */
5910
5911 static void
5912 calc_die_sizes (die)
5913      dw_die_ref die;
5914 {
5915   dw_die_ref c;
5916
5917   die->die_offset = next_die_offset;
5918   next_die_offset += size_of_die (die);
5919
5920   for (c = die->die_child; c != NULL; c = c->die_sib)
5921     calc_die_sizes (c);
5922
5923   if (die->die_child != NULL)
5924     /* Count the null byte used to terminate sibling lists.  */
5925     next_die_offset += 1;
5926 }
5927
5928 /* Set the marks for a die and its children.  We do this so
5929    that we know whether or not a reference needs to use FORM_ref_addr; only
5930    DIEs in the same CU will be marked.  We used to clear out the offset
5931    and use that as the flag, but ran into ordering problems.  */
5932
5933 static void
5934 mark_dies (die)
5935      dw_die_ref die;
5936 {
5937   dw_die_ref c;
5938
5939   die->die_mark = 1;
5940   for (c = die->die_child; c; c = c->die_sib)
5941     mark_dies (c);
5942 }
5943
5944 /* Clear the marks for a die and its children.  */
5945
5946 static void
5947 unmark_dies (die)
5948      dw_die_ref die;
5949 {
5950   dw_die_ref c;
5951
5952   die->die_mark = 0;
5953   for (c = die->die_child; c; c = c->die_sib)
5954     unmark_dies (c);
5955 }
5956
5957 /* Return the size of the .debug_pubnames table  generated for the
5958    compilation unit.  */
5959
5960 static unsigned long
5961 size_of_pubnames ()
5962 {
5963   unsigned long size;
5964   unsigned i;
5965
5966   size = DWARF_PUBNAMES_HEADER_SIZE;
5967   for (i = 0; i < pubname_table_in_use; i++)
5968     {
5969       pubname_ref p = &pubname_table[i];
5970       size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
5971     }
5972
5973   size += DWARF_OFFSET_SIZE;
5974   return size;
5975 }
5976
5977 /* Return the size of the information in the .debug_aranges section.  */
5978
5979 static unsigned long
5980 size_of_aranges ()
5981 {
5982   unsigned long size;
5983
5984   size = DWARF_ARANGES_HEADER_SIZE;
5985
5986   /* Count the address/length pair for this compilation unit.  */
5987   size += 2 * DWARF2_ADDR_SIZE;
5988   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
5989
5990   /* Count the two zero words used to terminated the address range table.  */
5991   size += 2 * DWARF2_ADDR_SIZE;
5992   return size;
5993 }
5994 \f
5995 /* Select the encoding of an attribute value.  */
5996
5997 static enum dwarf_form
5998 value_format (a)
5999      dw_attr_ref a;
6000 {
6001   switch (a->dw_attr_val.val_class)
6002     {
6003     case dw_val_class_addr:
6004       return DW_FORM_addr;
6005     case dw_val_class_range_list:
6006     case dw_val_class_offset:
6007       if (DWARF_OFFSET_SIZE == 4)
6008         return DW_FORM_data4;
6009       if (DWARF_OFFSET_SIZE == 8)
6010         return DW_FORM_data8;
6011       abort ();
6012     case dw_val_class_loc_list:
6013       /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6014          .debug_loc section */
6015       return DW_FORM_data4;
6016     case dw_val_class_loc:
6017       switch (constant_size (size_of_locs (AT_loc (a))))
6018         {
6019         case 1:
6020           return DW_FORM_block1;
6021         case 2:
6022           return DW_FORM_block2;
6023         default:
6024           abort ();
6025         }
6026     case dw_val_class_const:
6027       return DW_FORM_sdata;
6028     case dw_val_class_unsigned_const:
6029       switch (constant_size (AT_unsigned (a)))
6030         {
6031         case 1:
6032           return DW_FORM_data1;
6033         case 2:
6034           return DW_FORM_data2;
6035         case 4:
6036           return DW_FORM_data4;
6037         case 8:
6038           return DW_FORM_data8;
6039         default:
6040           abort ();
6041         }
6042     case dw_val_class_long_long:
6043       return DW_FORM_block1;
6044     case dw_val_class_float:
6045       return DW_FORM_block1;
6046     case dw_val_class_flag:
6047       return DW_FORM_flag;
6048     case dw_val_class_die_ref:
6049       if (AT_ref_external (a))
6050         return DW_FORM_ref_addr;
6051       else
6052         return DW_FORM_ref;
6053     case dw_val_class_fde_ref:
6054       return DW_FORM_data;
6055     case dw_val_class_lbl_id:
6056       return DW_FORM_addr;
6057     case dw_val_class_lbl_offset:
6058       return DW_FORM_data;
6059     case dw_val_class_str:
6060       return AT_string_form (a);
6061
6062     default:
6063       abort ();
6064     }
6065 }
6066
6067 /* Output the encoding of an attribute value.  */
6068
6069 static void
6070 output_value_format (a)
6071      dw_attr_ref a;
6072 {
6073   enum dwarf_form form = value_format (a);
6074
6075   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6076 }
6077
6078 /* Output the .debug_abbrev section which defines the DIE abbreviation
6079    table.  */
6080
6081 static void
6082 output_abbrev_section ()
6083 {
6084   unsigned long abbrev_id;
6085
6086   dw_attr_ref a_attr;
6087
6088   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6089     {
6090       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6091
6092       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6093       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6094                                    dwarf_tag_name (abbrev->die_tag));
6095
6096       if (abbrev->die_child != NULL)
6097         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6098       else
6099         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6100
6101       for (a_attr = abbrev->die_attr; a_attr != NULL;
6102            a_attr = a_attr->dw_attr_next)
6103         {
6104           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6105                                        dwarf_attr_name (a_attr->dw_attr));
6106           output_value_format (a_attr);
6107         }
6108
6109       dw2_asm_output_data (1, 0, NULL);
6110       dw2_asm_output_data (1, 0, NULL);
6111     }
6112
6113   /* Terminate the table.  */
6114   dw2_asm_output_data (1, 0, NULL);
6115 }
6116
6117 /* Output a symbol we can use to refer to this DIE from another CU.  */
6118
6119 static inline void
6120 output_die_symbol (die)
6121      dw_die_ref die;
6122 {
6123   char *sym = die->die_symbol;
6124
6125   if (sym == 0)
6126     return;
6127
6128   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6129     /* We make these global, not weak; if the target doesn't support
6130        .linkonce, it doesn't support combining the sections, so debugging
6131        will break.  */
6132     ASM_GLOBALIZE_LABEL (asm_out_file, sym);
6133
6134   ASM_OUTPUT_LABEL (asm_out_file, sym);
6135 }
6136
6137 /* Return a new location list, given the begin and end range, and the
6138    expression. gensym tells us whether to generate a new internal symbol for
6139    this location list node, which is done for the head of the list only.  */
6140
6141 static inline dw_loc_list_ref
6142 new_loc_list (expr, begin, end, section, gensym)
6143      dw_loc_descr_ref expr;
6144      const char *begin;
6145      const char *end;
6146      const char *section;
6147      unsigned gensym;
6148 {
6149   dw_loc_list_ref retlist
6150     = (dw_loc_list_ref) xcalloc (1, sizeof (dw_loc_list_node));
6151
6152   retlist->begin = begin;
6153   retlist->end = end;
6154   retlist->expr = expr;
6155   retlist->section = section;
6156   if (gensym) 
6157     retlist->ll_symbol = gen_internal_sym ("LLST");
6158
6159   return retlist;
6160 }
6161
6162 /* Add a location description expression to a location list */
6163
6164 static inline void
6165 add_loc_descr_to_loc_list (list_head, descr, begin, end, section)
6166      dw_loc_list_ref *list_head;
6167      dw_loc_descr_ref descr;
6168      const char *begin;
6169      const char *end;
6170      const char *section;
6171 {
6172   dw_loc_list_ref *d;
6173   
6174   /* Find the end of the chain.  */
6175   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6176     ;
6177
6178   /* Add a new location list node to the list */
6179   *d = new_loc_list (descr, begin, end, section, 0);
6180 }
6181
6182 /* Output the location list given to us */
6183
6184 static void
6185 output_loc_list (list_head)
6186      dw_loc_list_ref list_head;
6187 {
6188   dw_loc_list_ref curr = list_head;
6189
6190   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6191
6192   /* ??? This shouldn't be needed now that we've forced the
6193      compilation unit base address to zero when there is code
6194      in more than one section.  */
6195   if (strcmp (curr->section, ".text") == 0)
6196     {
6197       /* dw2_asm_output_data will mask off any extra bits in the ~0.  */
6198       dw2_asm_output_data (DWARF2_ADDR_SIZE, ~(unsigned HOST_WIDE_INT) 0,
6199                            "Location list base address specifier fake entry");
6200       dw2_asm_output_offset (DWARF2_ADDR_SIZE, curr->section,
6201                              "Location list base address specifier base");
6202     }
6203
6204   for (curr = list_head; curr != NULL; curr=curr->dw_loc_next)
6205     {
6206       unsigned long size;
6207
6208       dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6209                             "Location list begin address (%s)",
6210                             list_head->ll_symbol);
6211       dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6212                             "Location list end address (%s)",
6213                             list_head->ll_symbol);
6214       size = size_of_locs (curr->expr);
6215       
6216       /* Output the block length for this list of location operations.  */
6217       if (size > 0xffff)
6218         abort ();
6219       dw2_asm_output_data (2, size, "%s", "Location expression size");
6220
6221       output_loc_sequence (curr->expr);
6222     }
6223
6224   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0,
6225                        "Location list terminator begin (%s)",
6226                        list_head->ll_symbol);
6227   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0,
6228                        "Location list terminator end (%s)",
6229                        list_head->ll_symbol);
6230 }
6231
6232 /* Output the DIE and its attributes.  Called recursively to generate
6233    the definitions of each child DIE.  */
6234
6235 static void
6236 output_die (die)
6237      dw_die_ref die;
6238 {
6239   dw_attr_ref a;
6240   dw_die_ref c;
6241   unsigned long size;
6242
6243   /* If someone in another CU might refer to us, set up a symbol for
6244      them to point to.  */
6245   if (die->die_symbol)
6246     output_die_symbol (die);
6247
6248   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6249                                die->die_offset, dwarf_tag_name (die->die_tag));
6250
6251   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6252     {
6253       const char *name = dwarf_attr_name (a->dw_attr);
6254
6255       switch (AT_class (a))
6256         {
6257         case dw_val_class_addr:
6258           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6259           break;
6260
6261         case dw_val_class_offset:
6262           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6263                                "%s", name);
6264           break;
6265
6266         case dw_val_class_range_list:
6267           {
6268             char *p = strchr (ranges_section_label, '\0');
6269
6270             sprintf (p, "+0x%lx", a->dw_attr_val.v.val_offset);
6271             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6272                                    "%s", name);
6273             *p = '\0';
6274           }
6275           break;
6276
6277         case dw_val_class_loc:
6278           size = size_of_locs (AT_loc (a));
6279
6280           /* Output the block length for this list of location operations.  */
6281           dw2_asm_output_data (constant_size (size), size, "%s", name);
6282
6283           output_loc_sequence (AT_loc (a));
6284           break;
6285
6286         case dw_val_class_const:
6287           /* ??? It would be slightly more efficient to use a scheme like is
6288              used for unsigned constants below, but gdb 4.x does not sign
6289              extend.  Gdb 5.x does sign extend.  */
6290           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6291           break;
6292
6293         case dw_val_class_unsigned_const:
6294           dw2_asm_output_data (constant_size (AT_unsigned (a)),
6295                                AT_unsigned (a), "%s", name);
6296           break;
6297
6298         case dw_val_class_long_long:
6299           {
6300             unsigned HOST_WIDE_INT first, second;
6301
6302             dw2_asm_output_data (1,
6303                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6304                                  "%s", name);
6305
6306             if (WORDS_BIG_ENDIAN)
6307               {
6308                 first = a->dw_attr_val.v.val_long_long.hi;
6309                 second = a->dw_attr_val.v.val_long_long.low;
6310               }
6311             else
6312               {
6313                 first = a->dw_attr_val.v.val_long_long.low;
6314                 second = a->dw_attr_val.v.val_long_long.hi;
6315               }
6316
6317             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6318                                  first, "long long constant");
6319             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6320                                  second, NULL);
6321           }
6322           break;
6323
6324         case dw_val_class_float:
6325           {
6326             unsigned int i;
6327
6328             dw2_asm_output_data (1, a->dw_attr_val.v.val_float.length * 4,
6329                                  "%s", name);
6330
6331             for (i = 0; i < a->dw_attr_val.v.val_float.length; i++)
6332               dw2_asm_output_data (4, a->dw_attr_val.v.val_float.array[i],
6333                                    "fp constant word %u", i);
6334             break;
6335           }
6336
6337         case dw_val_class_flag:
6338           dw2_asm_output_data (1, AT_flag (a), "%s", name);
6339           break;
6340
6341         case dw_val_class_loc_list:
6342           {
6343             char *sym = AT_loc_list (a)->ll_symbol;
6344
6345             if (sym == 0)
6346               abort ();
6347             dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym,
6348                                   loc_section_label, "%s", name);
6349           }
6350           break;
6351
6352         case dw_val_class_die_ref:
6353           if (AT_ref_external (a))
6354             {
6355               char *sym = AT_ref (a)->die_symbol;
6356
6357               if (sym == 0)
6358                 abort ();
6359               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6360             }
6361           else if (AT_ref (a)->die_offset == 0)
6362             abort ();
6363           else
6364             dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6365                                  "%s", name);
6366           break;
6367
6368         case dw_val_class_fde_ref:
6369           {
6370             char l1[20];
6371
6372             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6373                                          a->dw_attr_val.v.val_fde_index * 2);
6374             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6375           }
6376           break;
6377
6378         case dw_val_class_lbl_id:
6379           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6380           break;
6381
6382         case dw_val_class_lbl_offset:
6383           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6384           break;
6385
6386         case dw_val_class_str:
6387           if (AT_string_form (a) == DW_FORM_strp)
6388             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6389                                    a->dw_attr_val.v.val_str->label,
6390                                    "%s: \"%s\"", name, AT_string (a));
6391           else
6392             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6393           break;
6394
6395         default:
6396           abort ();
6397         }
6398     }
6399
6400   for (c = die->die_child; c != NULL; c = c->die_sib)
6401     output_die (c);
6402
6403   /* Add null byte to terminate sibling list.  */
6404   if (die->die_child != NULL)
6405     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6406                          die->die_offset);
6407 }
6408
6409 /* Output the compilation unit that appears at the beginning of the
6410    .debug_info section, and precedes the DIE descriptions.  */
6411
6412 static void
6413 output_compilation_unit_header ()
6414 {
6415   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset - DWARF_OFFSET_SIZE,
6416                        "Length of Compilation Unit Info");
6417   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6418   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6419                          "Offset Into Abbrev. Section");
6420   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6421 }
6422
6423 /* Output the compilation unit DIE and its children.  */
6424
6425 static void
6426 output_comp_unit (die)
6427      dw_die_ref die;
6428 {
6429   const char *secname;
6430
6431   /* Even if there are no children of this DIE, we must output the information
6432      about the compilation unit.  Otherwise, on an empty translation unit, we
6433      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
6434      will then complain when examining the file.  First mark all the DIEs in
6435      this CU so we know which get local refs.  */
6436   mark_dies (die);
6437
6438   build_abbrev_table (die);
6439
6440   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
6441   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6442   calc_die_sizes (die);
6443
6444   if (die->die_symbol)
6445     {
6446       char *tmp = (char *) alloca (strlen (die->die_symbol) + 24);
6447
6448       sprintf (tmp, ".gnu.linkonce.wi.%s", die->die_symbol);
6449       secname = tmp;
6450       die->die_symbol = NULL;
6451     }
6452   else
6453     secname = (const char *) DEBUG_INFO_SECTION;
6454
6455   /* Output debugging information.  */
6456   named_section_flags (secname, SECTION_DEBUG);
6457   output_compilation_unit_header ();
6458   output_die (die);
6459
6460   /* Leave the marks on the main CU, so we can check them in
6461      output_pubnames.  */
6462   if (die->die_symbol)
6463     unmark_dies (die);
6464 }
6465
6466 /* The DWARF2 pubname for a nested thingy looks like "A::f".  The output
6467    of decl_printable_name for C++ looks like "A::f(int)".  Let's drop the
6468    argument list, and maybe the scope.  */
6469
6470 static const char *
6471 dwarf2_name (decl, scope)
6472      tree decl;
6473      int scope;
6474 {
6475   return (*decl_printable_name) (decl, scope ? 1 : 0);
6476 }
6477
6478 /* Add a new entry to .debug_pubnames if appropriate.  */
6479
6480 static void
6481 add_pubname (decl, die)
6482      tree decl;
6483      dw_die_ref die;
6484 {
6485   pubname_ref p;
6486
6487   if (! TREE_PUBLIC (decl))
6488     return;
6489
6490   if (pubname_table_in_use == pubname_table_allocated)
6491     {
6492       pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
6493       pubname_table
6494         = (pubname_ref) xrealloc (pubname_table,
6495                                   (pubname_table_allocated
6496                                    * sizeof (pubname_entry)));
6497     }
6498
6499   p = &pubname_table[pubname_table_in_use++];
6500   p->die = die;
6501   p->name = xstrdup (dwarf2_name (decl, 1));
6502 }
6503
6504 /* Output the public names table used to speed up access to externally
6505    visible names.  For now, only generate entries for externally
6506    visible procedures.  */
6507
6508 static void
6509 output_pubnames ()
6510 {
6511   unsigned i;
6512   unsigned long pubnames_length = size_of_pubnames ();
6513
6514   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
6515                        "Length of Public Names Info");
6516   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6517   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
6518                          "Offset of Compilation Unit Info");
6519   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
6520                        "Compilation Unit Length");
6521
6522   for (i = 0; i < pubname_table_in_use; i++)
6523     {
6524       pubname_ref pub = &pubname_table[i];
6525
6526       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
6527       if (pub->die->die_mark == 0)
6528         abort ();
6529
6530       dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
6531                            "DIE offset");
6532
6533       dw2_asm_output_nstring (pub->name, -1, "external name");
6534     }
6535
6536   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
6537 }
6538
6539 /* Add a new entry to .debug_aranges if appropriate.  */
6540
6541 static void
6542 add_arange (decl, die)
6543      tree decl;
6544      dw_die_ref die;
6545 {
6546   if (! DECL_SECTION_NAME (decl))
6547     return;
6548
6549   if (arange_table_in_use == arange_table_allocated)
6550     {
6551       arange_table_allocated += ARANGE_TABLE_INCREMENT;
6552       arange_table = (dw_die_ref *)
6553         xrealloc (arange_table, arange_table_allocated * sizeof (dw_die_ref));
6554     }
6555
6556   arange_table[arange_table_in_use++] = die;
6557 }
6558
6559 /* Output the information that goes into the .debug_aranges table.
6560    Namely, define the beginning and ending address range of the
6561    text section generated for this compilation unit.  */
6562
6563 static void
6564 output_aranges ()
6565 {
6566   unsigned i;
6567   unsigned long aranges_length = size_of_aranges ();
6568
6569   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
6570                        "Length of Address Ranges Info");
6571   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6572   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
6573                          "Offset of Compilation Unit Info");
6574   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
6575   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
6576
6577   /* We need to align to twice the pointer size here.  */
6578   if (DWARF_ARANGES_PAD_SIZE)
6579     {
6580       /* Pad using a 2 byte words so that padding is correct for any
6581          pointer size.  */
6582       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
6583                            2 * DWARF2_ADDR_SIZE);
6584       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
6585         dw2_asm_output_data (2, 0, NULL);
6586     }
6587
6588   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
6589   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
6590                         text_section_label, "Length");
6591
6592   for (i = 0; i < arange_table_in_use; i++)
6593     {
6594       dw_die_ref die = arange_table[i];
6595
6596       /* We shouldn't see aranges for DIEs outside of the main CU.  */
6597       if (die->die_mark == 0)
6598         abort ();
6599
6600       if (die->die_tag == DW_TAG_subprogram)
6601         {
6602           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
6603                                "Address");
6604           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
6605                                 get_AT_low_pc (die), "Length");
6606         }
6607       else
6608         {
6609           /* A static variable; extract the symbol from DW_AT_location.
6610              Note that this code isn't currently hit, as we only emit
6611              aranges for functions (jason 9/23/99).  */
6612           dw_attr_ref a = get_AT (die, DW_AT_location);
6613           dw_loc_descr_ref loc;
6614
6615           if (! a || AT_class (a) != dw_val_class_loc)
6616             abort ();
6617
6618           loc = AT_loc (a);
6619           if (loc->dw_loc_opc != DW_OP_addr)
6620             abort ();
6621
6622           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
6623                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
6624           dw2_asm_output_data (DWARF2_ADDR_SIZE,
6625                                get_AT_unsigned (die, DW_AT_byte_size),
6626                                "Length");
6627         }
6628     }
6629
6630   /* Output the terminator words.  */
6631   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6632   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6633 }
6634
6635 /* Add a new entry to .debug_ranges.  Return the offset at which it
6636    was placed.  */
6637
6638 static unsigned int
6639 add_ranges (block)
6640      tree block;
6641 {
6642   unsigned int in_use = ranges_table_in_use;
6643
6644   if (in_use == ranges_table_allocated)
6645     {
6646       ranges_table_allocated += RANGES_TABLE_INCREMENT;
6647       ranges_table = (dw_ranges_ref)
6648         xrealloc (ranges_table, (ranges_table_allocated
6649                                  * sizeof (struct dw_ranges_struct)));
6650     }
6651
6652   ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
6653   ranges_table_in_use = in_use + 1;
6654
6655   return in_use * 2 * DWARF2_ADDR_SIZE;
6656 }
6657
6658 static void
6659 output_ranges ()
6660 {
6661   unsigned i;
6662   static const char *const start_fmt = "Offset 0x%x";
6663   const char *fmt = start_fmt;
6664
6665   for (i = 0; i < ranges_table_in_use; i++)
6666     {
6667       int block_num = ranges_table[i].block_num;
6668
6669       if (block_num)
6670         {
6671           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
6672           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
6673
6674           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
6675           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
6676
6677           /* If all code is in the text section, then the compilation
6678              unit base address defaults to DW_AT_low_pc, which is the
6679              base of the text section.  */
6680           if (separate_line_info_table_in_use == 0)
6681             {
6682               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
6683                                     text_section_label,
6684                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
6685               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
6686                                     text_section_label, NULL);
6687             }
6688
6689           /* Otherwise, we add a DW_AT_entry_pc attribute to force the
6690              compilation unit base address to zero, which allows us to
6691              use absolute addresses, and not worry about whether the
6692              target supports cross-section arithmetic.  */
6693           else
6694             {
6695               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
6696                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
6697               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
6698             }
6699
6700           fmt = NULL;
6701         }
6702       else
6703         {
6704           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6705           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6706           fmt = start_fmt;
6707         }
6708     }
6709 }
6710
6711 /* Data structure containing information about input files.  */
6712 struct file_info
6713 {
6714   char *path;           /* Complete file name.  */
6715   char *fname;          /* File name part.  */
6716   int length;           /* Length of entire string.  */
6717   int file_idx;         /* Index in input file table.  */
6718   int dir_idx;          /* Index in directory table.  */
6719 };
6720
6721 /* Data structure containing information about directories with source
6722    files.  */
6723 struct dir_info
6724 {
6725   char *path;           /* Path including directory name.  */
6726   int length;           /* Path length.  */
6727   int prefix;           /* Index of directory entry which is a prefix.  */
6728   int count;            /* Number of files in this directory.  */
6729   int dir_idx;          /* Index of directory used as base.  */
6730   int used;             /* Used in the end?  */
6731 };
6732
6733 /* Callback function for file_info comparison.  We sort by looking at
6734    the directories in the path.  */
6735
6736 static int
6737 file_info_cmp (p1, p2)
6738      const void *p1;
6739      const void *p2;
6740 {
6741   const struct file_info *s1 = p1;
6742   const struct file_info *s2 = p2;
6743   unsigned char *cp1;
6744   unsigned char *cp2;
6745
6746   /* Take care of file names without directories.  We need to make sure that
6747      we return consistent values to qsort since some will get confused if
6748      we return the same value when identical operands are passed in opposite
6749      orders.  So if neither has a directory, return 0 and otherwise return
6750      1 or -1 depending on which one has the directory.  */
6751   if ((s1->path == s1->fname || s2->path == s2->fname))
6752     return (s2->path == s2->fname) - (s1->path == s1->fname);
6753
6754   cp1 = (unsigned char *) s1->path;
6755   cp2 = (unsigned char *) s2->path;
6756
6757   while (1)
6758     {
6759       ++cp1;
6760       ++cp2;
6761       /* Reached the end of the first path?  If so, handle like above.  */
6762       if ((cp1 == (unsigned char *) s1->fname)
6763           || (cp2 == (unsigned char *) s2->fname))
6764         return ((cp2 == (unsigned char *) s2->fname)
6765                 - (cp1 == (unsigned char *) s1->fname));
6766
6767       /* Character of current path component the same?  */
6768       else if (*cp1 != *cp2)
6769         return *cp1 - *cp2;
6770     }
6771 }
6772
6773 /* Output the directory table and the file name table.  We try to minimize
6774    the total amount of memory needed.  A heuristic is used to avoid large
6775    slowdowns with many input files.  */
6776
6777 static void
6778 output_file_names ()
6779 {
6780   struct file_info *files;
6781   struct dir_info *dirs;
6782   int *saved;
6783   int *savehere;
6784   int *backmap;
6785   int ndirs;
6786   int idx_offset;
6787   int i;
6788   int idx;
6789
6790   /* Allocate the various arrays we need.  */
6791   files = (struct file_info *) alloca (file_table.in_use
6792                                        * sizeof (struct file_info));
6793   dirs = (struct dir_info *) alloca (file_table.in_use
6794                                      * sizeof (struct dir_info));
6795
6796   /* Sort the file names.  */
6797   for (i = 1; i < (int) file_table.in_use; i++)
6798     {
6799       char *f;
6800
6801       /* Skip all leading "./".  */
6802       f = file_table.table[i];
6803       while (f[0] == '.' && f[1] == '/')
6804         f += 2;
6805
6806       /* Create a new array entry.  */
6807       files[i].path = f;
6808       files[i].length = strlen (f);
6809       files[i].file_idx = i;
6810
6811       /* Search for the file name part.  */
6812       f = strrchr (f, '/');
6813       files[i].fname = f == NULL ? files[i].path : f + 1;
6814     }
6815
6816   qsort (files + 1, file_table.in_use - 1, sizeof (files[0]), file_info_cmp);
6817
6818   /* Find all the different directories used.  */
6819   dirs[0].path = files[1].path;
6820   dirs[0].length = files[1].fname - files[1].path;
6821   dirs[0].prefix = -1;
6822   dirs[0].count = 1;
6823   dirs[0].dir_idx = 0;
6824   dirs[0].used = 0;
6825   files[1].dir_idx = 0;
6826   ndirs = 1;
6827
6828   for (i = 2; i < (int) file_table.in_use; i++)
6829     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
6830         && memcmp (dirs[ndirs - 1].path, files[i].path,
6831                    dirs[ndirs - 1].length) == 0)
6832       {
6833         /* Same directory as last entry.  */
6834         files[i].dir_idx = ndirs - 1;
6835         ++dirs[ndirs - 1].count;
6836       }
6837     else
6838       {
6839         int j;
6840
6841         /* This is a new directory.  */
6842         dirs[ndirs].path = files[i].path;
6843         dirs[ndirs].length = files[i].fname - files[i].path;
6844         dirs[ndirs].count = 1;
6845         dirs[ndirs].dir_idx = ndirs;
6846         dirs[ndirs].used = 0;
6847         files[i].dir_idx = ndirs;
6848
6849         /* Search for a prefix.  */
6850         dirs[ndirs].prefix = -1;
6851         for (j = 0; j < ndirs; j++)
6852           if (dirs[j].length < dirs[ndirs].length
6853               && dirs[j].length > 1
6854               && (dirs[ndirs].prefix == -1
6855                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
6856               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
6857             dirs[ndirs].prefix = j;
6858
6859         ++ndirs;
6860       }
6861
6862   /* Now to the actual work.  We have to find a subset of the directories which
6863      allow expressing the file name using references to the directory table
6864      with the least amount of characters.  We do not do an exhaustive search
6865      where we would have to check out every combination of every single
6866      possible prefix.  Instead we use a heuristic which provides nearly optimal
6867      results in most cases and never is much off.  */
6868   saved = (int *) alloca (ndirs * sizeof (int));
6869   savehere = (int *) alloca (ndirs * sizeof (int));
6870
6871   memset (saved, '\0', ndirs * sizeof (saved[0]));
6872   for (i = 0; i < ndirs; i++)
6873     {
6874       int j;
6875       int total;
6876
6877       /* We can always save some space for the current directory.  But this
6878          does not mean it will be enough to justify adding the directory.  */
6879       savehere[i] = dirs[i].length;
6880       total = (savehere[i] - saved[i]) * dirs[i].count;
6881
6882       for (j = i + 1; j < ndirs; j++)
6883         {
6884           savehere[j] = 0;
6885           if (saved[j] < dirs[i].length)
6886             {
6887               /* Determine whether the dirs[i] path is a prefix of the
6888                  dirs[j] path.  */
6889               int k;
6890
6891               k = dirs[j].prefix;
6892               while (k != -1 && k != i)
6893                 k = dirs[k].prefix;
6894
6895               if (k == i)
6896                 {
6897                   /* Yes it is.  We can possibly safe some memory but
6898                      writing the filenames in dirs[j] relative to
6899                      dirs[i].  */
6900                   savehere[j] = dirs[i].length;
6901                   total += (savehere[j] - saved[j]) * dirs[j].count;
6902                 }
6903             }
6904         }
6905
6906       /* Check whether we can safe enough to justify adding the dirs[i]
6907          directory.  */
6908       if (total > dirs[i].length + 1)
6909         {
6910           /* It's worthwhile adding.  */
6911           for (j = i; j < ndirs; j++)
6912             if (savehere[j] > 0)
6913               {
6914                 /* Remember how much we saved for this directory so far.  */
6915                 saved[j] = savehere[j];
6916
6917                 /* Remember the prefix directory.  */
6918                 dirs[j].dir_idx = i;
6919               }
6920         }
6921     }
6922
6923   /* We have to emit them in the order they appear in the file_table array
6924      since the index is used in the debug info generation.  To do this
6925      efficiently we generate a back-mapping of the indices first.  */
6926   backmap = (int *) alloca (file_table.in_use * sizeof (int));
6927   for (i = 1; i < (int) file_table.in_use; i++)
6928     {
6929       backmap[files[i].file_idx] = i;
6930
6931       /* Mark this directory as used.  */
6932       dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
6933     }
6934
6935   /* That was it.  We are ready to emit the information.  First emit the
6936      directory name table.  We have to make sure the first actually emitted
6937      directory name has index one; zero is reserved for the current working
6938      directory.  Make sure we do not confuse these indices with the one for the
6939      constructed table (even though most of the time they are identical).  */
6940   idx = 1;
6941   idx_offset = dirs[0].length > 0 ? 1 : 0;
6942   for (i = 1 - idx_offset; i < ndirs; i++)
6943     if (dirs[i].used != 0)
6944       {
6945         dirs[i].used = idx++;
6946         dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
6947                                 "Directory Entry: 0x%x", dirs[i].used);
6948       }
6949
6950   dw2_asm_output_data (1, 0, "End directory table");
6951
6952   /* Correct the index for the current working directory entry if it
6953      exists.  */
6954   if (idx_offset == 0)
6955     dirs[0].used = 0;
6956
6957   /* Now write all the file names.  */
6958   for (i = 1; i < (int) file_table.in_use; i++)
6959     {
6960       int file_idx = backmap[i];
6961       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
6962
6963       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
6964                               "File Entry: 0x%x", i);
6965
6966       /* Include directory index.  */
6967       dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
6968
6969       /* Modification time.  */
6970       dw2_asm_output_data_uleb128 (0, NULL);
6971
6972       /* File length in bytes.  */
6973       dw2_asm_output_data_uleb128 (0, NULL);
6974     }
6975
6976   dw2_asm_output_data (1, 0, "End file name table");
6977 }
6978
6979
6980 /* Output the source line number correspondence information.  This
6981    information goes into the .debug_line section.  */
6982
6983 static void
6984 output_line_info ()
6985 {
6986   char l1[20], l2[20], p1[20], p2[20];
6987   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
6988   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
6989   unsigned opc;
6990   unsigned n_op_args;
6991   unsigned long lt_index;
6992   unsigned long current_line;
6993   long line_offset;
6994   long line_delta;
6995   unsigned long current_file;
6996   unsigned long function;
6997
6998   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
6999   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7000   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7001   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7002
7003   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7004                         "Length of Source Line Info");
7005   ASM_OUTPUT_LABEL (asm_out_file, l1);
7006
7007   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7008   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7009   ASM_OUTPUT_LABEL (asm_out_file, p1);
7010
7011   dw2_asm_output_data (1, DWARF_LINE_MIN_INSTR_LENGTH,
7012                        "Minimum Instruction Length");
7013   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7014                        "Default is_stmt_start flag");
7015   dw2_asm_output_data (1, DWARF_LINE_BASE,
7016                        "Line Base Value (Special Opcodes)");
7017   dw2_asm_output_data (1, DWARF_LINE_RANGE,
7018                        "Line Range Value (Special Opcodes)");
7019   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7020                        "Special Opcode Base");
7021
7022   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7023     {
7024       switch (opc)
7025         {
7026         case DW_LNS_advance_pc:
7027         case DW_LNS_advance_line:
7028         case DW_LNS_set_file:
7029         case DW_LNS_set_column:
7030         case DW_LNS_fixed_advance_pc:
7031           n_op_args = 1;
7032           break;
7033         default:
7034           n_op_args = 0;
7035           break;
7036         }
7037
7038       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7039                            opc, n_op_args);
7040     }
7041
7042   /* Write out the information about the files we use.  */
7043   output_file_names ();
7044   ASM_OUTPUT_LABEL (asm_out_file, p2);
7045
7046   /* We used to set the address register to the first location in the text
7047      section here, but that didn't accomplish anything since we already
7048      have a line note for the opening brace of the first function.  */
7049
7050   /* Generate the line number to PC correspondence table, encoded as
7051      a series of state machine operations.  */
7052   current_file = 1;
7053   current_line = 1;
7054   strcpy (prev_line_label, text_section_label);
7055   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7056     {
7057       dw_line_info_ref line_info = &line_info_table[lt_index];
7058
7059 #if 0
7060       /* Disable this optimization for now; GDB wants to see two line notes
7061          at the beginning of a function so it can find the end of the
7062          prologue.  */
7063
7064       /* Don't emit anything for redundant notes.  Just updating the
7065          address doesn't accomplish anything, because we already assume
7066          that anything after the last address is this line.  */
7067       if (line_info->dw_line_num == current_line
7068           && line_info->dw_file_num == current_file)
7069         continue;
7070 #endif
7071
7072       /* Emit debug info for the address of the current line.
7073
7074          Unfortunately, we have little choice here currently, and must always
7075          use the most general form.  GCC does not know the address delta
7076          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
7077          attributes which will give an upper bound on the address range.  We
7078          could perhaps use length attributes to determine when it is safe to
7079          use DW_LNS_fixed_advance_pc.  */
7080
7081       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7082       if (0)
7083         {
7084           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
7085           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7086                                "DW_LNS_fixed_advance_pc");
7087           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7088         }
7089       else
7090         {
7091           /* This can handle any delta.  This takes
7092              4+DWARF2_ADDR_SIZE bytes.  */
7093           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7094           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7095           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7096           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7097         }
7098
7099       strcpy (prev_line_label, line_label);
7100
7101       /* Emit debug info for the source file of the current line, if
7102          different from the previous line.  */
7103       if (line_info->dw_file_num != current_file)
7104         {
7105           current_file = line_info->dw_file_num;
7106           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7107           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7108                                        file_table.table[current_file]);
7109         }
7110
7111       /* Emit debug info for the current line number, choosing the encoding
7112          that uses the least amount of space.  */
7113       if (line_info->dw_line_num != current_line)
7114         {
7115           line_offset = line_info->dw_line_num - current_line;
7116           line_delta = line_offset - DWARF_LINE_BASE;
7117           current_line = line_info->dw_line_num;
7118           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7119             /* This can handle deltas from -10 to 234, using the current
7120                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
7121                takes 1 byte.  */
7122             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7123                                  "line %lu", current_line);
7124           else
7125             {
7126               /* This can handle any delta.  This takes at least 4 bytes,
7127                  depending on the value being encoded.  */
7128               dw2_asm_output_data (1, DW_LNS_advance_line,
7129                                    "advance to line %lu", current_line);
7130               dw2_asm_output_data_sleb128 (line_offset, NULL);
7131               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7132             }
7133         }
7134       else
7135         /* We still need to start a new row, so output a copy insn.  */
7136         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7137     }
7138
7139   /* Emit debug info for the address of the end of the function.  */
7140   if (0)
7141     {
7142       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7143                            "DW_LNS_fixed_advance_pc");
7144       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7145     }
7146   else
7147     {
7148       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7149       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7150       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7151       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7152     }
7153
7154   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7155   dw2_asm_output_data_uleb128 (1, NULL);
7156   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7157
7158   function = 0;
7159   current_file = 1;
7160   current_line = 1;
7161   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7162     {
7163       dw_separate_line_info_ref line_info
7164         = &separate_line_info_table[lt_index];
7165
7166 #if 0
7167       /* Don't emit anything for redundant notes.  */
7168       if (line_info->dw_line_num == current_line
7169           && line_info->dw_file_num == current_file
7170           && line_info->function == function)
7171         goto cont;
7172 #endif
7173
7174       /* Emit debug info for the address of the current line.  If this is
7175          a new function, or the first line of a function, then we need
7176          to handle it differently.  */
7177       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7178                                    lt_index);
7179       if (function != line_info->function)
7180         {
7181           function = line_info->function;
7182
7183           /* Set the address register to the first line in the function */
7184           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7185           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7186           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7187           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7188         }
7189       else
7190         {
7191           /* ??? See the DW_LNS_advance_pc comment above.  */
7192           if (0)
7193             {
7194               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7195                                    "DW_LNS_fixed_advance_pc");
7196               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7197             }
7198           else
7199             {
7200               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7201               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7202               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7203               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7204             }
7205         }
7206
7207       strcpy (prev_line_label, line_label);
7208
7209       /* Emit debug info for the source file of the current line, if
7210          different from the previous line.  */
7211       if (line_info->dw_file_num != current_file)
7212         {
7213           current_file = line_info->dw_file_num;
7214           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7215           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7216                                        file_table.table[current_file]);
7217         }
7218
7219       /* Emit debug info for the current line number, choosing the encoding
7220          that uses the least amount of space.  */
7221       if (line_info->dw_line_num != current_line)
7222         {
7223           line_offset = line_info->dw_line_num - current_line;
7224           line_delta = line_offset - DWARF_LINE_BASE;
7225           current_line = line_info->dw_line_num;
7226           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7227             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7228                                  "line %lu", current_line);
7229           else
7230             {
7231               dw2_asm_output_data (1, DW_LNS_advance_line,
7232                                    "advance to line %lu", current_line);
7233               dw2_asm_output_data_sleb128 (line_offset, NULL);
7234               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7235             }
7236         }
7237       else
7238         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7239
7240 #if 0
7241     cont:
7242 #endif
7243
7244       lt_index++;
7245
7246       /* If we're done with a function, end its sequence.  */
7247       if (lt_index == separate_line_info_table_in_use
7248           || separate_line_info_table[lt_index].function != function)
7249         {
7250           current_file = 1;
7251           current_line = 1;
7252
7253           /* Emit debug info for the address of the end of the function.  */
7254           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7255           if (0)
7256             {
7257               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7258                                    "DW_LNS_fixed_advance_pc");
7259               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7260             }
7261           else
7262             {
7263               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7264               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7265               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7266               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7267             }
7268
7269           /* Output the marker for the end of this sequence.  */
7270           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7271           dw2_asm_output_data_uleb128 (1, NULL);
7272           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7273         }
7274     }
7275
7276   /* Output the marker for the end of the line number info.  */
7277   ASM_OUTPUT_LABEL (asm_out_file, l2);
7278 }
7279 \f
7280 /* Given a pointer to a tree node for some base type, return a pointer to
7281    a DIE that describes the given type.
7282
7283    This routine must only be called for GCC type nodes that correspond to
7284    Dwarf base (fundamental) types.  */
7285
7286 static dw_die_ref
7287 base_type_die (type)
7288      tree type;
7289 {
7290   dw_die_ref base_type_result;
7291   const char *type_name;
7292   enum dwarf_type encoding;
7293   tree name = TYPE_NAME (type);
7294
7295   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7296     return 0;
7297
7298   if (name)
7299     {
7300       if (TREE_CODE (name) == TYPE_DECL)
7301         name = DECL_NAME (name);
7302
7303       type_name = IDENTIFIER_POINTER (name);
7304     }
7305   else
7306     type_name = "__unknown__";
7307
7308   switch (TREE_CODE (type))
7309     {
7310     case INTEGER_TYPE:
7311       /* Carefully distinguish the C character types, without messing
7312          up if the language is not C. Note that we check only for the names
7313          that contain spaces; other names might occur by coincidence in other
7314          languages.  */
7315       if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7316              && (type == char_type_node
7317                  || ! strcmp (type_name, "signed char")
7318                  || ! strcmp (type_name, "unsigned char"))))
7319         {
7320           if (TREE_UNSIGNED (type))
7321             encoding = DW_ATE_unsigned;
7322           else
7323             encoding = DW_ATE_signed;
7324           break;
7325         }
7326       /* else fall through.  */
7327
7328     case CHAR_TYPE:
7329       /* GNU Pascal/Ada CHAR type.  Not used in C.  */
7330       if (TREE_UNSIGNED (type))
7331         encoding = DW_ATE_unsigned_char;
7332       else
7333         encoding = DW_ATE_signed_char;
7334       break;
7335
7336     case REAL_TYPE:
7337       encoding = DW_ATE_float;
7338       break;
7339
7340       /* Dwarf2 doesn't know anything about complex ints, so use
7341          a user defined type for it.  */
7342     case COMPLEX_TYPE:
7343       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7344         encoding = DW_ATE_complex_float;
7345       else
7346         encoding = DW_ATE_lo_user;
7347       break;
7348
7349     case BOOLEAN_TYPE:
7350       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
7351       encoding = DW_ATE_boolean;
7352       break;
7353
7354     default:
7355       /* No other TREE_CODEs are Dwarf fundamental types.  */
7356       abort ();
7357     }
7358
7359   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
7360   if (demangle_name_func)
7361     type_name = (*demangle_name_func) (type_name);
7362
7363   add_AT_string (base_type_result, DW_AT_name, type_name);
7364   add_AT_unsigned (base_type_result, DW_AT_byte_size,
7365                    int_size_in_bytes (type));
7366   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7367
7368   return base_type_result;
7369 }
7370
7371 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7372    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
7373    a given type is generally the same as the given type, except that if the
7374    given type is a pointer or reference type, then the root type of the given
7375    type is the root type of the "basis" type for the pointer or reference
7376    type.  (This definition of the "root" type is recursive.) Also, the root
7377    type of a `const' qualified type or a `volatile' qualified type is the
7378    root type of the given type without the qualifiers.  */
7379
7380 static tree
7381 root_type (type)
7382      tree type;
7383 {
7384   if (TREE_CODE (type) == ERROR_MARK)
7385     return error_mark_node;
7386
7387   switch (TREE_CODE (type))
7388     {
7389     case ERROR_MARK:
7390       return error_mark_node;
7391
7392     case POINTER_TYPE:
7393     case REFERENCE_TYPE:
7394       return type_main_variant (root_type (TREE_TYPE (type)));
7395
7396     default:
7397       return type_main_variant (type);
7398     }
7399 }
7400
7401 /* Given a pointer to an arbitrary ..._TYPE tree node, return non-zero if the
7402    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
7403
7404 static inline int
7405 is_base_type (type)
7406      tree type;
7407 {
7408   switch (TREE_CODE (type))
7409     {
7410     case ERROR_MARK:
7411     case VOID_TYPE:
7412     case INTEGER_TYPE:
7413     case REAL_TYPE:
7414     case COMPLEX_TYPE:
7415     case BOOLEAN_TYPE:
7416     case CHAR_TYPE:
7417       return 1;
7418
7419     case SET_TYPE:
7420     case ARRAY_TYPE:
7421     case RECORD_TYPE:
7422     case UNION_TYPE:
7423     case QUAL_UNION_TYPE:
7424     case ENUMERAL_TYPE:
7425     case FUNCTION_TYPE:
7426     case METHOD_TYPE:
7427     case POINTER_TYPE:
7428     case REFERENCE_TYPE:
7429     case FILE_TYPE:
7430     case OFFSET_TYPE:
7431     case LANG_TYPE:
7432     case VECTOR_TYPE:
7433       return 0;
7434
7435     default:
7436       abort ();
7437     }
7438
7439   return 0;
7440 }
7441
7442 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
7443    entry that chains various modifiers in front of the given type.  */
7444
7445 static dw_die_ref
7446 modified_type_die (type, is_const_type, is_volatile_type, context_die)
7447      tree type;
7448      int is_const_type;
7449      int is_volatile_type;
7450      dw_die_ref context_die;
7451 {
7452   enum tree_code code = TREE_CODE (type);
7453   dw_die_ref mod_type_die = NULL;
7454   dw_die_ref sub_die = NULL;
7455   tree item_type = NULL;
7456
7457   if (code != ERROR_MARK)
7458     {
7459       tree qualified_type;
7460
7461       /* See if we already have the appropriately qualified variant of
7462          this type.  */
7463       qualified_type 
7464         = get_qualified_type (type,
7465                               ((is_const_type ? TYPE_QUAL_CONST : 0)
7466                                | (is_volatile_type 
7467                                   ? TYPE_QUAL_VOLATILE : 0)));
7468
7469       /* If we do, then we can just use its DIE, if it exists.  */
7470       if (qualified_type)
7471         {
7472           mod_type_die = lookup_type_die (qualified_type);
7473           if (mod_type_die)
7474             return mod_type_die;
7475         }
7476
7477       /* Handle C typedef types.  */
7478       if (qualified_type && TYPE_NAME (qualified_type) 
7479           && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
7480           && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
7481         {
7482           tree type_name = TYPE_NAME (qualified_type);
7483           tree dtype = TREE_TYPE (type_name);
7484
7485           if (qualified_type == dtype)
7486             {
7487               /* For a named type, use the typedef.  */
7488               gen_type_die (qualified_type, context_die);
7489               mod_type_die = lookup_type_die (qualified_type);
7490             }
7491           else if (is_const_type < TYPE_READONLY (dtype)
7492                    || is_volatile_type < TYPE_VOLATILE (dtype))
7493             /* cv-unqualified version of named type.  Just use the unnamed
7494                type to which it refers.  */
7495             mod_type_die
7496               = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
7497                                    is_const_type, is_volatile_type,
7498                                    context_die);
7499
7500           /* Else cv-qualified version of named type; fall through.  */
7501         }
7502
7503       if (mod_type_die)
7504         /* OK.  */
7505         ;
7506       else if (is_const_type)
7507         {
7508           mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
7509           sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
7510         }
7511       else if (is_volatile_type)
7512         {
7513           mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
7514           sub_die = modified_type_die (type, 0, 0, context_die);
7515         }
7516       else if (code == POINTER_TYPE)
7517         {
7518           mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
7519           add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7520 #if 0
7521           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7522 #endif
7523           item_type = TREE_TYPE (type);
7524         }
7525       else if (code == REFERENCE_TYPE)
7526         {
7527           mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
7528           add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7529 #if 0
7530           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7531 #endif
7532           item_type = TREE_TYPE (type);
7533         }
7534       else if (is_base_type (type))
7535         mod_type_die = base_type_die (type);
7536       else
7537         {
7538           gen_type_die (type, context_die);
7539
7540           /* We have to get the type_main_variant here (and pass that to the
7541              `lookup_type_die' routine) because the ..._TYPE node we have
7542              might simply be a *copy* of some original type node (where the
7543              copy was created to help us keep track of typedef names) and
7544              that copy might have a different TYPE_UID from the original
7545              ..._TYPE node.  */
7546           mod_type_die = lookup_type_die (type_main_variant (type));
7547           if (mod_type_die == NULL)
7548             abort ();
7549         }
7550
7551       /* We want to equate the qualified type to the die below.  */
7552       if (qualified_type)
7553         type = qualified_type;
7554     }
7555
7556   equate_type_number_to_die (type, mod_type_die);
7557   if (item_type)
7558     /* We must do this after the equate_type_number_to_die call, in case
7559        this is a recursive type.  This ensures that the modified_type_die
7560        recursion will terminate even if the type is recursive.  Recursive
7561        types are possible in Ada.  */
7562     sub_die = modified_type_die (item_type,
7563                                  TYPE_READONLY (item_type),
7564                                  TYPE_VOLATILE (item_type),
7565                                  context_die);
7566
7567   if (sub_die != NULL)
7568     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
7569
7570   return mod_type_die;
7571 }
7572
7573 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
7574    an enumerated type.  */
7575
7576 static inline int
7577 type_is_enum (type)
7578      tree type;
7579 {
7580   return TREE_CODE (type) == ENUMERAL_TYPE;
7581 }
7582
7583 /* Return the register number described by a given RTL node.  */
7584
7585 static unsigned int
7586 reg_number (rtl)
7587      rtx rtl;
7588 {
7589   unsigned regno = REGNO (rtl);
7590
7591   if (regno >= FIRST_PSEUDO_REGISTER)
7592     abort ();
7593
7594   return DBX_REGISTER_NUMBER (regno);
7595 }
7596
7597 /* Return a location descriptor that designates a machine register or
7598    zero if there is no such.  */
7599
7600 static dw_loc_descr_ref
7601 reg_loc_descriptor (rtl)
7602      rtx rtl;
7603 {
7604   dw_loc_descr_ref loc_result = NULL;
7605   unsigned reg;
7606
7607   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
7608     return 0;
7609
7610   reg = reg_number (rtl);
7611   if (reg <= 31)
7612     loc_result = new_loc_descr (DW_OP_reg0 + reg, 0, 0);
7613   else
7614     loc_result = new_loc_descr (DW_OP_regx, reg, 0);
7615
7616   return loc_result;
7617 }
7618
7619 /* Return a location descriptor that designates a constant.  */
7620
7621 static dw_loc_descr_ref
7622 int_loc_descriptor (i)
7623      HOST_WIDE_INT i;
7624 {
7625   enum dwarf_location_atom op;
7626
7627   /* Pick the smallest representation of a constant, rather than just
7628      defaulting to the LEB encoding.  */
7629   if (i >= 0)
7630     {
7631       if (i <= 31)
7632         op = DW_OP_lit0 + i;
7633       else if (i <= 0xff)
7634         op = DW_OP_const1u;
7635       else if (i <= 0xffff)
7636         op = DW_OP_const2u;
7637       else if (HOST_BITS_PER_WIDE_INT == 32
7638                || i <= 0xffffffff)
7639         op = DW_OP_const4u;
7640       else
7641         op = DW_OP_constu;
7642     }
7643   else
7644     {
7645       if (i >= -0x80)
7646         op = DW_OP_const1s;
7647       else if (i >= -0x8000)
7648         op = DW_OP_const2s;
7649       else if (HOST_BITS_PER_WIDE_INT == 32
7650                || i >= -0x80000000)
7651         op = DW_OP_const4s;
7652       else
7653         op = DW_OP_consts;
7654     }
7655
7656   return new_loc_descr (op, i, 0);
7657 }
7658
7659 /* Return a location descriptor that designates a base+offset location.  */
7660
7661 static dw_loc_descr_ref
7662 based_loc_descr (reg, offset)
7663      unsigned reg;
7664      long int offset;
7665 {
7666   dw_loc_descr_ref loc_result;
7667   /* For the "frame base", we use the frame pointer or stack pointer
7668      registers, since the RTL for local variables is relative to one of
7669      them.  */
7670   unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
7671                                          ? HARD_FRAME_POINTER_REGNUM
7672                                          : STACK_POINTER_REGNUM);
7673
7674   if (reg == fp_reg)
7675     loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
7676   else if (reg <= 31)
7677     loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
7678   else
7679     loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
7680
7681   return loc_result;
7682 }
7683
7684 /* Return true if this RTL expression describes a base+offset calculation.  */
7685
7686 static inline int
7687 is_based_loc (rtl)
7688      rtx rtl;
7689 {
7690   return (GET_CODE (rtl) == PLUS
7691           && ((GET_CODE (XEXP (rtl, 0)) == REG
7692                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
7693                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
7694 }
7695
7696 /* The following routine converts the RTL for a variable or parameter
7697    (resident in memory) into an equivalent Dwarf representation of a
7698    mechanism for getting the address of that same variable onto the top of a
7699    hypothetical "address evaluation" stack.
7700
7701    When creating memory location descriptors, we are effectively transforming
7702    the RTL for a memory-resident object into its Dwarf postfix expression
7703    equivalent.  This routine recursively descends an RTL tree, turning
7704    it into Dwarf postfix code as it goes.
7705
7706    MODE is the mode of the memory reference, needed to handle some
7707    autoincrement addressing modes.
7708
7709    Return 0 if we can't represent the location.  */
7710
7711 static dw_loc_descr_ref
7712 mem_loc_descriptor (rtl, mode)
7713      rtx rtl;
7714      enum machine_mode mode;
7715 {
7716   dw_loc_descr_ref mem_loc_result = NULL;
7717
7718   /* Note that for a dynamically sized array, the location we will generate a
7719      description of here will be the lowest numbered location which is
7720      actually within the array.  That's *not* necessarily the same as the
7721      zeroth element of the array.  */
7722
7723 #ifdef ASM_SIMPLIFY_DWARF_ADDR
7724   rtl = ASM_SIMPLIFY_DWARF_ADDR (rtl);
7725 #endif
7726
7727   switch (GET_CODE (rtl))
7728     {
7729     case POST_INC:
7730     case POST_DEC:
7731     case POST_MODIFY:
7732       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
7733          just fall into the SUBREG code.  */
7734
7735       /* ... fall through ...  */
7736
7737     case SUBREG:
7738       /* The case of a subreg may arise when we have a local (register)
7739          variable or a formal (register) parameter which doesn't quite fill
7740          up an entire register.  For now, just assume that it is
7741          legitimate to make the Dwarf info refer to the whole register which
7742          contains the given subreg.  */
7743       rtl = SUBREG_REG (rtl);
7744
7745       /* ... fall through ...  */
7746
7747     case REG:
7748       /* Whenever a register number forms a part of the description of the
7749          method for calculating the (dynamic) address of a memory resident
7750          object, DWARF rules require the register number be referred to as
7751          a "base register".  This distinction is not based in any way upon
7752          what category of register the hardware believes the given register
7753          belongs to.  This is strictly DWARF terminology we're dealing with
7754          here. Note that in cases where the location of a memory-resident
7755          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
7756          OP_CONST (0)) the actual DWARF location descriptor that we generate
7757          may just be OP_BASEREG (basereg).  This may look deceptively like
7758          the object in question was allocated to a register (rather than in
7759          memory) so DWARF consumers need to be aware of the subtle
7760          distinction between OP_REG and OP_BASEREG.  */
7761       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
7762         mem_loc_result = based_loc_descr (reg_number (rtl), 0);
7763       break;
7764
7765     case MEM:
7766       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7767       if (mem_loc_result != 0)
7768         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
7769       break;
7770
7771     case LABEL_REF:
7772       /* Some ports can transform a symbol ref into a label ref, because
7773          the symbol ref is too far away and has to be dumped into a constant
7774          pool.  */
7775     case CONST:
7776     case SYMBOL_REF:
7777       /* Alternatively, the symbol in the constant pool might be referenced
7778          by a different symbol.  */
7779       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
7780         {
7781           rtx tmp = get_pool_constant (rtl);
7782
7783           if (GET_CODE (tmp) == SYMBOL_REF)
7784             rtl = tmp;
7785         }
7786
7787       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
7788       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
7789       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
7790       VARRAY_PUSH_RTX (used_rtx_varray, rtl);
7791       break;
7792
7793     case PRE_MODIFY:
7794       /* Extract the PLUS expression nested inside and fall into
7795          PLUS code below.  */
7796       rtl = XEXP (rtl, 1);
7797       goto plus;
7798
7799     case PRE_INC:
7800     case PRE_DEC:
7801       /* Turn these into a PLUS expression and fall into the PLUS code
7802          below.  */
7803       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
7804                           GEN_INT (GET_CODE (rtl) == PRE_INC
7805                                    ? GET_MODE_UNIT_SIZE (mode)
7806                                    : -GET_MODE_UNIT_SIZE (mode)));
7807
7808       /* ... fall through ...  */
7809
7810     case PLUS:
7811     plus:
7812       if (is_based_loc (rtl))
7813         mem_loc_result = based_loc_descr (reg_number (XEXP (rtl, 0)),
7814                                           INTVAL (XEXP (rtl, 1)));
7815       else
7816         {
7817           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
7818           if (mem_loc_result == 0)
7819             break;
7820
7821           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
7822               && INTVAL (XEXP (rtl, 1)) >= 0)
7823             add_loc_descr (&mem_loc_result,
7824                            new_loc_descr (DW_OP_plus_uconst,
7825                                           INTVAL (XEXP (rtl, 1)), 0));
7826           else
7827             {
7828               add_loc_descr (&mem_loc_result,
7829                              mem_loc_descriptor (XEXP (rtl, 1), mode));
7830               add_loc_descr (&mem_loc_result,
7831                              new_loc_descr (DW_OP_plus, 0, 0));
7832             }
7833         }
7834       break;
7835
7836     case MULT:
7837       {
7838         /* If a pseudo-reg is optimized away, it is possible for it to
7839            be replaced with a MEM containing a multiply.  */
7840         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
7841         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
7842
7843         if (op0 == 0 || op1 == 0)
7844           break;
7845
7846         mem_loc_result = op0;
7847         add_loc_descr (&mem_loc_result, op1);
7848         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
7849         break;
7850       }
7851
7852     case CONST_INT:
7853       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
7854       break;
7855
7856     default:
7857       abort ();
7858     }
7859
7860   return mem_loc_result;
7861 }
7862
7863 /* Return a descriptor that describes the concatenation of two locations.
7864    This is typically a complex variable.  */
7865
7866 static dw_loc_descr_ref
7867 concat_loc_descriptor (x0, x1)
7868      rtx x0, x1;
7869 {
7870   dw_loc_descr_ref cc_loc_result = NULL;
7871   dw_loc_descr_ref x0_ref = loc_descriptor (x0);
7872   dw_loc_descr_ref x1_ref = loc_descriptor (x1);
7873
7874   if (x0_ref == 0 || x1_ref == 0)
7875     return 0;
7876
7877   cc_loc_result = x0_ref;
7878   add_loc_descr (&cc_loc_result,
7879                  new_loc_descr (DW_OP_piece,
7880                                 GET_MODE_SIZE (GET_MODE (x0)), 0));
7881
7882   add_loc_descr (&cc_loc_result, x1_ref);
7883   add_loc_descr (&cc_loc_result,
7884                  new_loc_descr (DW_OP_piece,
7885                                 GET_MODE_SIZE (GET_MODE (x1)), 0));
7886
7887   return cc_loc_result;
7888 }
7889
7890 /* Output a proper Dwarf location descriptor for a variable or parameter
7891    which is either allocated in a register or in a memory location.  For a
7892    register, we just generate an OP_REG and the register number.  For a
7893    memory location we provide a Dwarf postfix expression describing how to
7894    generate the (dynamic) address of the object onto the address stack.
7895
7896    If we don't know how to describe it, return 0.  */
7897
7898 static dw_loc_descr_ref
7899 loc_descriptor (rtl)
7900      rtx rtl;
7901 {
7902   dw_loc_descr_ref loc_result = NULL;
7903
7904   switch (GET_CODE (rtl))
7905     {
7906     case SUBREG:
7907       /* The case of a subreg may arise when we have a local (register)
7908          variable or a formal (register) parameter which doesn't quite fill
7909          up an entire register.  For now, just assume that it is
7910          legitimate to make the Dwarf info refer to the whole register which
7911          contains the given subreg.  */
7912       rtl = SUBREG_REG (rtl);
7913
7914       /* ... fall through ...  */
7915
7916     case REG:
7917       loc_result = reg_loc_descriptor (rtl);
7918       break;
7919
7920     case MEM:
7921       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7922       break;
7923
7924     case CONCAT:
7925       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
7926       break;
7927
7928     default:
7929       abort ();
7930     }
7931
7932   return loc_result;
7933 }
7934
7935 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
7936    up particularly with variable length arrays.  If ADDRESSP is nonzero, we are
7937    looking for an address.  Otherwise, we return a value.  If we can't make a
7938    descriptor, return 0.  */
7939
7940 static dw_loc_descr_ref
7941 loc_descriptor_from_tree (loc, addressp)
7942      tree loc;
7943      int addressp;
7944 {
7945   dw_loc_descr_ref ret, ret1;
7946   int indirect_p = 0;
7947   int unsignedp = TREE_UNSIGNED (TREE_TYPE (loc));
7948   enum dwarf_location_atom op;
7949
7950   /* ??? Most of the time we do not take proper care for sign/zero
7951      extending the values properly.  Hopefully this won't be a real
7952      problem...  */
7953
7954   switch (TREE_CODE (loc))
7955     {
7956     case ERROR_MARK:
7957       return 0;
7958
7959     case WITH_RECORD_EXPR:
7960     case PLACEHOLDER_EXPR:
7961       /* This case involves extracting fields from an object to determine the
7962          position of other fields.  We don't try to encode this here.  The
7963          only user of this is Ada, which encodes the needed information using
7964          the names of types.  */
7965       return 0;
7966
7967     case VAR_DECL:
7968     case PARM_DECL:
7969       {
7970         rtx rtl = rtl_for_decl_location (loc);
7971         enum machine_mode mode = GET_MODE (rtl);
7972
7973         if (rtl == NULL_RTX)
7974           return 0;
7975         else if (CONSTANT_P (rtl))
7976           {
7977             ret = new_loc_descr (DW_OP_addr, 0, 0);
7978             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
7979             ret->dw_loc_oprnd1.v.val_addr = rtl;
7980             indirect_p = 1;
7981           }
7982         else
7983           {
7984             if (GET_CODE (rtl) == MEM)
7985               {
7986                 indirect_p = 1;
7987                 rtl = XEXP (rtl, 0);
7988               }
7989
7990             ret = mem_loc_descriptor (rtl, mode);
7991           }
7992       }
7993       break;
7994
7995     case INDIRECT_REF:
7996       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7997       indirect_p = 1;
7998       break;
7999
8000     case COMPOUND_EXPR:
8001       return loc_descriptor_from_tree (TREE_OPERAND (loc, 1), addressp);
8002
8003     case NOP_EXPR:
8004     case CONVERT_EXPR:
8005     case NON_LVALUE_EXPR:
8006     case VIEW_CONVERT_EXPR:
8007     case SAVE_EXPR:
8008       return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
8009
8010     case COMPONENT_REF:
8011     case BIT_FIELD_REF:
8012     case ARRAY_REF:
8013     case ARRAY_RANGE_REF:
8014       {
8015         tree obj, offset;
8016         HOST_WIDE_INT bitsize, bitpos, bytepos;
8017         enum machine_mode mode;
8018         int volatilep;
8019
8020         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8021                                    &unsignedp, &volatilep);
8022
8023         if (obj == loc)
8024           return 0;
8025
8026         ret = loc_descriptor_from_tree (obj, 1);
8027         if (ret == 0
8028             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8029           return 0;
8030
8031         if (offset != NULL_TREE)
8032           {
8033             /* Variable offset.  */
8034             add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
8035             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8036           }
8037
8038         if (!addressp)
8039           indirect_p = 1;
8040
8041         bytepos = bitpos / BITS_PER_UNIT;
8042         if (bytepos > 0)
8043           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8044         else if (bytepos < 0)
8045           {
8046             add_loc_descr (&ret, int_loc_descriptor (bytepos));
8047             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8048           }
8049         break;
8050       }
8051
8052     case INTEGER_CST:
8053       if (host_integerp (loc, 0))
8054         ret = int_loc_descriptor (tree_low_cst (loc, 0));
8055       else
8056         return 0;
8057       break;
8058
8059     case BIT_AND_EXPR:
8060       op = DW_OP_and;
8061       goto do_binop;
8062
8063     case BIT_XOR_EXPR:
8064       op = DW_OP_xor;
8065       goto do_binop;
8066
8067     case BIT_IOR_EXPR:
8068       op = DW_OP_or;
8069       goto do_binop;
8070
8071     case TRUNC_DIV_EXPR:
8072       op = DW_OP_div;
8073       goto do_binop;
8074
8075     case MINUS_EXPR:
8076       op = DW_OP_minus;
8077       goto do_binop;
8078
8079     case TRUNC_MOD_EXPR:
8080       op = DW_OP_mod;
8081       goto do_binop;
8082
8083     case MULT_EXPR:
8084       op = DW_OP_mul;
8085       goto do_binop;
8086
8087     case LSHIFT_EXPR:
8088       op = DW_OP_shl;
8089       goto do_binop;
8090
8091     case RSHIFT_EXPR:
8092       op = (unsignedp ? DW_OP_shr : DW_OP_shra);
8093       goto do_binop;
8094
8095     case PLUS_EXPR:
8096       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
8097           && host_integerp (TREE_OPERAND (loc, 1), 0))
8098         {
8099           ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8100           if (ret == 0)
8101             return 0;
8102
8103           add_loc_descr (&ret,
8104                          new_loc_descr (DW_OP_plus_uconst,
8105                                         tree_low_cst (TREE_OPERAND (loc, 1),
8106                                                       0),
8107                                         0));
8108           break;
8109         }
8110
8111       op = DW_OP_plus;
8112       goto do_binop;
8113
8114     case LE_EXPR:
8115       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8116         return 0;
8117
8118       op = DW_OP_le;
8119       goto do_binop;
8120
8121     case GE_EXPR:
8122       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8123         return 0;
8124
8125       op = DW_OP_ge;
8126       goto do_binop;
8127
8128     case LT_EXPR:
8129       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8130         return 0;
8131
8132       op = DW_OP_lt;
8133       goto do_binop;
8134
8135     case GT_EXPR:
8136       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8137         return 0;
8138
8139       op = DW_OP_gt;
8140       goto do_binop;
8141
8142     case EQ_EXPR:
8143       op = DW_OP_eq;
8144       goto do_binop;
8145
8146     case NE_EXPR:
8147       op = DW_OP_ne;
8148       goto do_binop;
8149
8150     do_binop:
8151       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8152       ret1 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8153       if (ret == 0 || ret1 == 0)
8154         return 0;
8155
8156       add_loc_descr (&ret, ret1);
8157       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8158       break;
8159
8160     case BIT_NOT_EXPR:
8161       op = DW_OP_not;
8162       goto do_unop;
8163
8164     case ABS_EXPR:
8165       op = DW_OP_abs;
8166       goto do_unop;
8167
8168     case NEGATE_EXPR:
8169       op = DW_OP_neg;
8170       goto do_unop;
8171
8172     do_unop:
8173       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8174       if (ret == 0)
8175         return 0;
8176
8177       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8178       break;
8179
8180     case MAX_EXPR:
8181       loc = build (COND_EXPR, TREE_TYPE (loc),
8182                    build (LT_EXPR, integer_type_node,
8183                           TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
8184                    TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
8185
8186       /* ... fall through ... */
8187
8188     case COND_EXPR:
8189       {
8190         dw_loc_descr_ref lhs
8191           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8192         dw_loc_descr_ref rhs
8193           = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
8194         dw_loc_descr_ref bra_node, jump_node, tmp;
8195
8196         ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8197         if (ret == 0 || lhs == 0 || rhs == 0)
8198           return 0;
8199
8200         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
8201         add_loc_descr (&ret, bra_node);
8202
8203         add_loc_descr (&ret, rhs);
8204         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
8205         add_loc_descr (&ret, jump_node);
8206
8207         add_loc_descr (&ret, lhs);
8208         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8209         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
8210
8211         /* ??? Need a node to point the skip at.  Use a nop.  */
8212         tmp = new_loc_descr (DW_OP_nop, 0, 0);
8213         add_loc_descr (&ret, tmp);
8214         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8215         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
8216       }
8217       break;
8218
8219     default:
8220       abort ();
8221     }
8222
8223   /* Show if we can't fill the request for an address.  */
8224   if (addressp && indirect_p == 0)
8225     return 0;
8226
8227   /* If we've got an address and don't want one, dereference.  */
8228   if (!addressp && indirect_p > 0)
8229     {
8230       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
8231
8232       if (size > DWARF2_ADDR_SIZE || size == -1)
8233         return 0;
8234       else if (size == DWARF2_ADDR_SIZE)
8235         op = DW_OP_deref;
8236       else
8237         op = DW_OP_deref_size;
8238
8239       add_loc_descr (&ret, new_loc_descr (op, size, 0));
8240     }
8241
8242   return ret;
8243 }
8244
8245 /* Given a value, round it up to the lowest multiple of `boundary'
8246    which is not less than the value itself.  */
8247
8248 static inline HOST_WIDE_INT
8249 ceiling (value, boundary)
8250      HOST_WIDE_INT value;
8251      unsigned int boundary;
8252 {
8253   return (((value + boundary - 1) / boundary) * boundary);
8254 }
8255
8256 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
8257    pointer to the declared type for the relevant field variable, or return
8258    `integer_type_node' if the given node turns out to be an
8259    ERROR_MARK node.  */
8260
8261 static inline tree
8262 field_type (decl)
8263      tree decl;
8264 {
8265   tree type;
8266
8267   if (TREE_CODE (decl) == ERROR_MARK)
8268     return integer_type_node;
8269
8270   type = DECL_BIT_FIELD_TYPE (decl);
8271   if (type == NULL_TREE)
8272     type = TREE_TYPE (decl);
8273
8274   return type;
8275 }
8276
8277 /* Given a pointer to a tree node, return the alignment in bits for
8278    it, or else return BITS_PER_WORD if the node actually turns out to
8279    be an ERROR_MARK node.  */
8280
8281 static inline unsigned
8282 simple_type_align_in_bits (type)
8283      tree type;
8284 {
8285   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
8286 }
8287
8288 static inline unsigned
8289 simple_decl_align_in_bits (decl)
8290      tree decl;
8291 {
8292   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
8293 }
8294
8295 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8296    node, return the size in bits for the type if it is a constant, or else
8297    return the alignment for the type if the type's size is not constant, or
8298    else return BITS_PER_WORD if the type actually turns out to be an
8299    ERROR_MARK node.  */
8300
8301 static inline unsigned HOST_WIDE_INT
8302 simple_type_size_in_bits (type)
8303      tree type;
8304 {
8305
8306   if (TREE_CODE (type) == ERROR_MARK)
8307     return BITS_PER_WORD;
8308   else if (TYPE_SIZE (type) == NULL_TREE)
8309     return 0;
8310   else if (host_integerp (TYPE_SIZE (type), 1))
8311     return tree_low_cst (TYPE_SIZE (type), 1);
8312   else
8313     return TYPE_ALIGN (type);
8314 }
8315
8316 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
8317    lowest addressed byte of the "containing object" for the given FIELD_DECL,
8318    or return 0 if we are unable to determine what that offset is, either
8319    because the argument turns out to be a pointer to an ERROR_MARK node, or
8320    because the offset is actually variable.  (We can't handle the latter case
8321    just yet).  */
8322
8323 static HOST_WIDE_INT
8324 field_byte_offset (decl)
8325      tree decl;
8326 {
8327   unsigned int type_align_in_bits;
8328   unsigned int decl_align_in_bits;
8329   unsigned HOST_WIDE_INT type_size_in_bits;
8330   HOST_WIDE_INT object_offset_in_bits;
8331   tree type;
8332   tree field_size_tree;
8333   HOST_WIDE_INT bitpos_int;
8334   HOST_WIDE_INT deepest_bitpos;
8335   unsigned HOST_WIDE_INT field_size_in_bits;
8336
8337   if (TREE_CODE (decl) == ERROR_MARK)
8338     return 0;
8339   else if (TREE_CODE (decl) != FIELD_DECL)
8340     abort ();
8341
8342   type = field_type (decl);
8343   field_size_tree = DECL_SIZE (decl);
8344
8345   /* The size could be unspecified if there was an error, or for
8346      a flexible array member.  */
8347   if (! field_size_tree)
8348     field_size_tree = bitsize_zero_node;
8349
8350   /* We cannot yet cope with fields whose positions are variable, so
8351      for now, when we see such things, we simply return 0.  Someday, we may
8352      be able to handle such cases, but it will be damn difficult.  */
8353   if (! host_integerp (bit_position (decl), 0))
8354     return 0;
8355
8356   bitpos_int = int_bit_position (decl);
8357
8358   /* If we don't know the size of the field, pretend it's a full word.  */
8359   if (host_integerp (field_size_tree, 1))
8360     field_size_in_bits = tree_low_cst (field_size_tree, 1);
8361   else
8362     field_size_in_bits = BITS_PER_WORD;
8363
8364   type_size_in_bits = simple_type_size_in_bits (type);
8365   type_align_in_bits = simple_type_align_in_bits (type);
8366   decl_align_in_bits = simple_decl_align_in_bits (decl);
8367
8368   /* The GCC front-end doesn't make any attempt to keep track of the starting
8369      bit offset (relative to the start of the containing structure type) of the
8370      hypothetical "containing object" for a bit-field.  Thus, when computing
8371      the byte offset value for the start of the "containing object" of a
8372      bit-field, we must deduce this information on our own. This can be rather
8373      tricky to do in some cases.  For example, handling the following structure
8374      type definition when compiling for an i386/i486 target (which only aligns
8375      long long's to 32-bit boundaries) can be very tricky:
8376
8377          struct S { int field1; long long field2:31; };
8378
8379      Fortunately, there is a simple rule-of-thumb which can be used in such
8380      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
8381      structure shown above.  It decides to do this based upon one simple rule
8382      for bit-field allocation.  GCC allocates each "containing object" for each
8383      bit-field at the first (i.e. lowest addressed) legitimate alignment
8384      boundary (based upon the required minimum alignment for the declared type
8385      of the field) which it can possibly use, subject to the condition that
8386      there is still enough available space remaining in the containing object
8387      (when allocated at the selected point) to fully accommodate all of the
8388      bits of the bit-field itself.
8389
8390      This simple rule makes it obvious why GCC allocates 8 bytes for each
8391      object of the structure type shown above.  When looking for a place to
8392      allocate the "containing object" for `field2', the compiler simply tries
8393      to allocate a 64-bit "containing object" at each successive 32-bit
8394      boundary (starting at zero) until it finds a place to allocate that 64-
8395      bit field such that at least 31 contiguous (and previously unallocated)
8396      bits remain within that selected 64 bit field.  (As it turns out, for the
8397      example above, the compiler finds it is OK to allocate the "containing
8398      object" 64-bit field at bit-offset zero within the structure type.)
8399
8400      Here we attempt to work backwards from the limited set of facts we're
8401      given, and we try to deduce from those facts, where GCC must have believed
8402      that the containing object started (within the structure type). The value
8403      we deduce is then used (by the callers of this routine) to generate
8404      DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
8405      and, in the case of DW_AT_location, regular fields as well).  */
8406
8407   /* Figure out the bit-distance from the start of the structure to the
8408      "deepest" bit of the bit-field.  */
8409   deepest_bitpos = bitpos_int + field_size_in_bits;
8410
8411   /* This is the tricky part.  Use some fancy footwork to deduce where the
8412      lowest addressed bit of the containing object must be.  */
8413   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8414
8415   /* Round up to type_align by default.  This works best for bitfields.  */
8416   object_offset_in_bits += type_align_in_bits - 1;
8417   object_offset_in_bits /= type_align_in_bits;
8418   object_offset_in_bits *= type_align_in_bits;
8419
8420   if (object_offset_in_bits > bitpos_int)
8421     {
8422       /* Sigh, the decl must be packed.  */
8423       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8424
8425       /* Round up to decl_align instead.  */
8426       object_offset_in_bits += decl_align_in_bits - 1;
8427       object_offset_in_bits /= decl_align_in_bits;
8428       object_offset_in_bits *= decl_align_in_bits;
8429     }
8430
8431   return object_offset_in_bits / BITS_PER_UNIT;
8432 }
8433 \f
8434 /* The following routines define various Dwarf attributes and any data
8435    associated with them.  */
8436
8437 /* Add a location description attribute value to a DIE.
8438
8439    This emits location attributes suitable for whole variables and
8440    whole parameters.  Note that the location attributes for struct fields are
8441    generated by the routine `data_member_location_attribute' below.  */
8442
8443 static void
8444 add_AT_location_description (die, attr_kind, rtl)
8445      dw_die_ref die;
8446      enum dwarf_attribute attr_kind;
8447      rtx rtl;
8448 {
8449   dw_loc_descr_ref descr = loc_descriptor (rtl);
8450
8451   if (descr != 0)
8452     add_AT_loc (die, attr_kind, descr);
8453 }
8454
8455 /* Attach the specialized form of location attribute used for data members of
8456    struct and union types.  In the special case of a FIELD_DECL node which
8457    represents a bit-field, the "offset" part of this special location
8458    descriptor must indicate the distance in bytes from the lowest-addressed
8459    byte of the containing struct or union type to the lowest-addressed byte of
8460    the "containing object" for the bit-field.  (See the `field_byte_offset'
8461    function above).
8462
8463    For any given bit-field, the "containing object" is a hypothetical object
8464    (of some integral or enum type) within which the given bit-field lives.  The
8465    type of this hypothetical "containing object" is always the same as the
8466    declared type of the individual bit-field itself (for GCC anyway... the
8467    DWARF spec doesn't actually mandate this).  Note that it is the size (in
8468    bytes) of the hypothetical "containing object" which will be given in the
8469    DW_AT_byte_size attribute for this bit-field.  (See the
8470    `byte_size_attribute' function below.)  It is also used when calculating the
8471    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
8472    function below.)  */
8473
8474 static void
8475 add_data_member_location_attribute (die, decl)
8476      dw_die_ref die;
8477      tree decl;
8478 {
8479   long offset;
8480   dw_loc_descr_ref loc_descr = 0;
8481
8482   if (TREE_CODE (decl) == TREE_VEC)
8483     {
8484       /* We're working on the TAG_inheritance for a base class.  */
8485       if (TREE_VIA_VIRTUAL (decl) && is_cxx ())
8486         {
8487           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
8488              aren't at a fixed offset from all (sub)objects of the same
8489              type.  We need to extract the appropriate offset from our
8490              vtable.  The following dwarf expression means
8491
8492                BaseAddr = ObAddr + *((*ObAddr) - Offset)
8493
8494              This is specific to the V3 ABI, of course.  */
8495
8496           dw_loc_descr_ref tmp;
8497
8498           /* Make a copy of the object address.  */
8499           tmp = new_loc_descr (DW_OP_dup, 0, 0);
8500           add_loc_descr (&loc_descr, tmp);
8501
8502           /* Extract the vtable address.  */
8503           tmp = new_loc_descr (DW_OP_deref, 0, 0);
8504           add_loc_descr (&loc_descr, tmp);
8505
8506           /* Calculate the address of the offset.  */
8507           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
8508           if (offset >= 0)
8509             abort ();
8510
8511           tmp = int_loc_descriptor (-offset);
8512           add_loc_descr (&loc_descr, tmp);
8513           tmp = new_loc_descr (DW_OP_minus, 0, 0);
8514           add_loc_descr (&loc_descr, tmp);
8515
8516           /* Extract the offset.  */
8517           tmp = new_loc_descr (DW_OP_deref, 0, 0);
8518           add_loc_descr (&loc_descr, tmp);
8519
8520           /* Add it to the object address.  */
8521           tmp = new_loc_descr (DW_OP_plus, 0, 0);
8522           add_loc_descr (&loc_descr, tmp);
8523         }
8524       else
8525         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
8526     }
8527   else
8528     offset = field_byte_offset (decl);
8529
8530   if (! loc_descr)
8531     {
8532       enum dwarf_location_atom op;
8533
8534       /* The DWARF2 standard says that we should assume that the structure
8535          address is already on the stack, so we can specify a structure field
8536          address by using DW_OP_plus_uconst.  */
8537
8538 #ifdef MIPS_DEBUGGING_INFO
8539       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
8540          operator correctly.  It works only if we leave the offset on the
8541          stack.  */
8542       op = DW_OP_constu;
8543 #else
8544       op = DW_OP_plus_uconst;
8545 #endif
8546
8547       loc_descr = new_loc_descr (op, offset, 0);
8548     }
8549
8550   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
8551 }
8552
8553 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
8554    does not have a "location" either in memory or in a register.  These
8555    things can arise in GNU C when a constant is passed as an actual parameter
8556    to an inlined function.  They can also arise in C++ where declared
8557    constants do not necessarily get memory "homes".  */
8558
8559 static void
8560 add_const_value_attribute (die, rtl)
8561      dw_die_ref die;
8562      rtx rtl;
8563 {
8564   switch (GET_CODE (rtl))
8565     {
8566     case CONST_INT:
8567       /* Note that a CONST_INT rtx could represent either an integer
8568          or a floating-point constant.  A CONST_INT is used whenever
8569          the constant will fit into a single word.  In all such
8570          cases, the original mode of the constant value is wiped
8571          out, and the CONST_INT rtx is assigned VOIDmode.  */
8572       {
8573         HOST_WIDE_INT val = INTVAL (rtl);
8574         
8575         /* ??? We really should be using HOST_WIDE_INT throughout.  */
8576         if (val < 0 && (long) val == val)
8577           add_AT_int (die, DW_AT_const_value, (long) val);
8578         else if ((unsigned long) val == (unsigned HOST_WIDE_INT) val)
8579           add_AT_unsigned (die, DW_AT_const_value, (unsigned long) val);
8580         else
8581           {
8582 #if HOST_BITS_PER_LONG * 2 == HOST_BITS_PER_WIDE_INT
8583             add_AT_long_long (die, DW_AT_const_value,
8584                               val >> HOST_BITS_PER_LONG, val);
8585 #else
8586             abort ();
8587 #endif
8588           }
8589       }
8590       break;
8591
8592     case CONST_DOUBLE:
8593       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
8594          floating-point constant.  A CONST_DOUBLE is used whenever the
8595          constant requires more than one word in order to be adequately
8596          represented.  We output CONST_DOUBLEs as blocks.  */
8597       {
8598         enum machine_mode mode = GET_MODE (rtl);
8599
8600         if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8601           {
8602             unsigned length = GET_MODE_SIZE (mode) / 4;
8603             long *array = (long *) xmalloc (sizeof (long) * length);
8604             REAL_VALUE_TYPE rv;
8605
8606             REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
8607             switch (mode)
8608               {
8609               case SFmode:
8610                 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
8611                 break;
8612
8613               case DFmode:
8614                 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
8615                 break;
8616
8617               case XFmode:
8618               case TFmode:
8619                 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
8620                 break;
8621
8622               default:
8623                 abort ();
8624               }
8625
8626             add_AT_float (die, DW_AT_const_value, length, array);
8627           }
8628         else
8629           {
8630             /* ??? We really should be using HOST_WIDE_INT throughout.  */
8631             if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
8632               abort ();
8633
8634             add_AT_long_long (die, DW_AT_const_value,
8635                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
8636           }
8637       }
8638       break;
8639
8640     case CONST_STRING:
8641       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
8642       break;
8643
8644     case SYMBOL_REF:
8645     case LABEL_REF:
8646     case CONST:
8647       add_AT_addr (die, DW_AT_const_value, rtl);
8648       VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8649       break;
8650
8651     case PLUS:
8652       /* In cases where an inlined instance of an inline function is passed
8653          the address of an `auto' variable (which is local to the caller) we
8654          can get a situation where the DECL_RTL of the artificial local
8655          variable (for the inlining) which acts as a stand-in for the
8656          corresponding formal parameter (of the inline function) will look
8657          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
8658          exactly a compile-time constant expression, but it isn't the address
8659          of the (artificial) local variable either.  Rather, it represents the
8660          *value* which the artificial local variable always has during its
8661          lifetime.  We currently have no way to represent such quasi-constant
8662          values in Dwarf, so for now we just punt and generate nothing.  */
8663       break;
8664
8665     default:
8666       /* No other kinds of rtx should be possible here.  */
8667       abort ();
8668     }
8669
8670 }
8671
8672 static rtx
8673 rtl_for_decl_location (decl)
8674      tree decl;
8675 {
8676   rtx rtl;
8677
8678   /* Here we have to decide where we are going to say the parameter "lives"
8679      (as far as the debugger is concerned).  We only have a couple of
8680      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
8681
8682      DECL_RTL normally indicates where the parameter lives during most of the
8683      activation of the function.  If optimization is enabled however, this
8684      could be either NULL or else a pseudo-reg.  Both of those cases indicate
8685      that the parameter doesn't really live anywhere (as far as the code
8686      generation parts of GCC are concerned) during most of the function's
8687      activation.  That will happen (for example) if the parameter is never
8688      referenced within the function.
8689
8690      We could just generate a location descriptor here for all non-NULL
8691      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
8692      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
8693      where DECL_RTL is NULL or is a pseudo-reg.
8694
8695      Note however that we can only get away with using DECL_INCOMING_RTL as
8696      a backup substitute for DECL_RTL in certain limited cases.  In cases
8697      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
8698      we can be sure that the parameter was passed using the same type as it is
8699      declared to have within the function, and that its DECL_INCOMING_RTL
8700      points us to a place where a value of that type is passed.
8701
8702      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
8703      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
8704      because in these cases DECL_INCOMING_RTL points us to a value of some
8705      type which is *different* from the type of the parameter itself.  Thus,
8706      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
8707      such cases, the debugger would end up (for example) trying to fetch a
8708      `float' from a place which actually contains the first part of a
8709      `double'.  That would lead to really incorrect and confusing
8710      output at debug-time.
8711
8712      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
8713      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
8714      are a couple of exceptions however.  On little-endian machines we can
8715      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
8716      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
8717      an integral type that is smaller than TREE_TYPE (decl). These cases arise
8718      when (on a little-endian machine) a non-prototyped function has a
8719      parameter declared to be of type `short' or `char'.  In such cases,
8720      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
8721      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
8722      passed `int' value.  If the debugger then uses that address to fetch
8723      a `short' or a `char' (on a little-endian machine) the result will be
8724      the correct data, so we allow for such exceptional cases below.
8725
8726      Note that our goal here is to describe the place where the given formal
8727      parameter lives during most of the function's activation (i.e. between the
8728      end of the prologue and the start of the epilogue).  We'll do that as best
8729      as we can. Note however that if the given formal parameter is modified
8730      sometime during the execution of the function, then a stack backtrace (at
8731      debug-time) will show the function as having been called with the *new*
8732      value rather than the value which was originally passed in.  This happens
8733      rarely enough that it is not a major problem, but it *is* a problem, and
8734      I'd like to fix it.
8735
8736      A future version of dwarf2out.c may generate two additional attributes for
8737      any given DW_TAG_formal_parameter DIE which will describe the "passed
8738      type" and the "passed location" for the given formal parameter in addition
8739      to the attributes we now generate to indicate the "declared type" and the
8740      "active location" for each parameter.  This additional set of attributes
8741      could be used by debuggers for stack backtraces. Separately, note that
8742      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
8743      This happens (for example) for inlined-instances of inline function formal
8744      parameters which are never referenced.  This really shouldn't be
8745      happening.  All PARM_DECL nodes should get valid non-NULL
8746      DECL_INCOMING_RTL values, but integrate.c doesn't currently generate these
8747      values for inlined instances of inline function parameters, so when we see
8748      such cases, we are just out-of-luck for the time being (until integrate.c
8749      gets fixed).  */
8750
8751   /* Use DECL_RTL as the "location" unless we find something better.  */
8752   rtl = DECL_RTL_IF_SET (decl);
8753
8754   if (TREE_CODE (decl) == PARM_DECL)
8755     {
8756       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
8757         {
8758           tree declared_type = type_main_variant (TREE_TYPE (decl));
8759           tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
8760
8761           /* This decl represents a formal parameter which was optimized out.
8762              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
8763              all cases where (rtl == NULL_RTX) just below.  */
8764           if (declared_type == passed_type)
8765             rtl = DECL_INCOMING_RTL (decl);
8766           else if (! BYTES_BIG_ENDIAN
8767                    && TREE_CODE (declared_type) == INTEGER_TYPE
8768                    && (GET_MODE_SIZE (TYPE_MODE (declared_type))
8769                        <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
8770             rtl = DECL_INCOMING_RTL (decl);
8771         }
8772
8773       /* If the parm was passed in registers, but lives on the stack, then
8774          make a big endian correction if the mode of the type of the
8775          parameter is not the same as the mode of the rtl.  */
8776       /* ??? This is the same series of checks that are made in dbxout.c before
8777          we reach the big endian correction code there.  It isn't clear if all
8778          of these checks are necessary here, but keeping them all is the safe
8779          thing to do.  */
8780       else if (GET_CODE (rtl) == MEM
8781                && XEXP (rtl, 0) != const0_rtx
8782                && ! CONSTANT_P (XEXP (rtl, 0))
8783                /* Not passed in memory.  */
8784                && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
8785                /* Not passed by invisible reference.  */
8786                && (GET_CODE (XEXP (rtl, 0)) != REG
8787                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
8788                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
8789 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
8790                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
8791 #endif
8792                      )
8793                /* Big endian correction check.  */
8794                && BYTES_BIG_ENDIAN
8795                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
8796                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
8797                    < UNITS_PER_WORD))
8798         {
8799           int offset = (UNITS_PER_WORD
8800                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
8801
8802           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
8803                              plus_constant (XEXP (rtl, 0), offset));
8804         }
8805     }
8806
8807   if (rtl != NULL_RTX)
8808     {
8809       rtl = eliminate_regs (rtl, 0, NULL_RTX);
8810 #ifdef LEAF_REG_REMAP
8811       if (current_function_uses_only_leaf_regs)
8812         leaf_renumber_regs_insn (rtl);
8813 #endif
8814     }
8815
8816   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
8817      and will have been substituted directly into all expressions that use it.
8818      C does not have such a concept, but C++ and other languages do.  */
8819   else if (DECL_INITIAL (decl))
8820     rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
8821                        EXPAND_INITIALIZER);
8822
8823   return rtl;
8824 }
8825
8826 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
8827    data attribute for a variable or a parameter.  We generate the
8828    DW_AT_const_value attribute only in those cases where the given variable
8829    or parameter does not have a true "location" either in memory or in a
8830    register.  This can happen (for example) when a constant is passed as an
8831    actual argument in a call to an inline function.  (It's possible that
8832    these things can crop up in other ways also.)  Note that one type of
8833    constant value which can be passed into an inlined function is a constant
8834    pointer.  This can happen for example if an actual argument in an inlined
8835    function call evaluates to a compile-time constant address.  */
8836
8837 static void
8838 add_location_or_const_value_attribute (die, decl)
8839      dw_die_ref die;
8840      tree decl;
8841 {
8842   rtx rtl;
8843
8844   if (TREE_CODE (decl) == ERROR_MARK)
8845     return;
8846   else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
8847     abort ();
8848
8849   rtl = rtl_for_decl_location (decl);
8850   if (rtl == NULL_RTX)
8851     return;
8852
8853   /* If we don't look past the constant pool, we risk emitting a
8854      reference to a constant pool entry that isn't referenced from
8855      code, and thus is not emitted.  */
8856   rtl = avoid_constant_pool_reference (rtl);
8857
8858   switch (GET_CODE (rtl))
8859     {
8860     case ADDRESSOF:
8861       /* The address of a variable that was optimized away; don't emit
8862          anything.  */
8863       break;
8864
8865     case CONST_INT:
8866     case CONST_DOUBLE:
8867     case CONST_STRING:
8868     case SYMBOL_REF:
8869     case LABEL_REF:
8870     case CONST:
8871     case PLUS:
8872       /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
8873       add_const_value_attribute (die, rtl);
8874       break;
8875
8876     case MEM:
8877     case REG:
8878     case SUBREG:
8879     case CONCAT:
8880       add_AT_location_description (die, DW_AT_location, rtl);
8881       break;
8882
8883     default:
8884       abort ();
8885     }
8886 }
8887
8888 /* If we don't have a copy of this variable in memory for some reason (such
8889    as a C++ member constant that doesn't have an out-of-line definition),
8890    we should tell the debugger about the constant value.  */
8891
8892 static void
8893 tree_add_const_value_attribute (var_die, decl)
8894      dw_die_ref var_die;
8895      tree decl;
8896 {
8897   tree init = DECL_INITIAL (decl);
8898   tree type = TREE_TYPE (decl);
8899
8900   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
8901       && initializer_constant_valid_p (init, type) == null_pointer_node)
8902     /* OK */;
8903   else
8904     return;
8905
8906   switch (TREE_CODE (type))
8907     {
8908     case INTEGER_TYPE:
8909       if (host_integerp (init, 0))
8910         add_AT_unsigned (var_die, DW_AT_const_value,
8911                          tree_low_cst (init, 0));
8912       else
8913         add_AT_long_long (var_die, DW_AT_const_value,
8914                           TREE_INT_CST_HIGH (init),
8915                           TREE_INT_CST_LOW (init));
8916       break;
8917
8918     default:;
8919     }
8920 }
8921
8922 /* Generate an DW_AT_name attribute given some string value to be included as
8923    the value of the attribute.  */
8924
8925 static inline void
8926 add_name_attribute (die, name_string)
8927      dw_die_ref die;
8928      const char *name_string;
8929 {
8930   if (name_string != NULL && *name_string != 0)
8931     {
8932       if (demangle_name_func)
8933         name_string = (*demangle_name_func) (name_string);
8934
8935       add_AT_string (die, DW_AT_name, name_string);
8936     }
8937 }
8938
8939 /* Given a tree node describing an array bound (either lower or upper) output
8940    a representation for that bound.  */
8941
8942 static void
8943 add_bound_info (subrange_die, bound_attr, bound)
8944      dw_die_ref subrange_die;
8945      enum dwarf_attribute bound_attr;
8946      tree bound;
8947 {
8948   switch (TREE_CODE (bound))
8949     {
8950     case ERROR_MARK:
8951       return;
8952
8953     /* All fixed-bounds are represented by INTEGER_CST nodes.        */
8954     case INTEGER_CST:
8955       if (! host_integerp (bound, 0)
8956           || (bound_attr == DW_AT_lower_bound
8957               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
8958                   || (is_fortran () && integer_onep (bound)))))
8959         /* use the default */
8960         ;
8961       else
8962         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
8963       break;
8964
8965     case CONVERT_EXPR:
8966     case NOP_EXPR:
8967     case NON_LVALUE_EXPR:
8968     case VIEW_CONVERT_EXPR:
8969       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
8970       break;
8971
8972     case SAVE_EXPR:
8973       /* If optimization is turned on, the SAVE_EXPRs that describe how to
8974          access the upper bound values may be bogus.  If they refer to a
8975          register, they may only describe how to get at these values at the
8976          points in the generated code right after they have just been
8977          computed.  Worse yet, in the typical case, the upper bound values
8978          will not even *be* computed in the optimized code (though the
8979          number of elements will), so these SAVE_EXPRs are entirely
8980          bogus. In order to compensate for this fact, we check here to see
8981          if optimization is enabled, and if so, we don't add an attribute
8982          for the (unknown and unknowable) upper bound.  This should not
8983          cause too much trouble for existing (stupid?)  debuggers because
8984          they have to deal with empty upper bounds location descriptions
8985          anyway in order to be able to deal with incomplete array types.
8986          Of course an intelligent debugger (GDB?)  should be able to
8987          comprehend that a missing upper bound specification in an array
8988          type used for a storage class `auto' local array variable
8989          indicates that the upper bound is both unknown (at compile- time)
8990          and unknowable (at run-time) due to optimization.
8991
8992          We assume that a MEM rtx is safe because gcc wouldn't put the
8993          value there unless it was going to be used repeatedly in the
8994          function, i.e. for cleanups.  */
8995       if (SAVE_EXPR_RTL (bound)
8996           && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
8997         {
8998           dw_die_ref ctx = lookup_decl_die (current_function_decl);
8999           dw_die_ref decl_die = new_die (DW_TAG_variable, ctx, bound);
9000           rtx loc = SAVE_EXPR_RTL (bound);
9001
9002           /* If the RTL for the SAVE_EXPR is memory, handle the case where
9003              it references an outer function's frame.  */
9004           if (GET_CODE (loc) == MEM)
9005             {
9006               rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
9007
9008               if (XEXP (loc, 0) != new_addr)
9009                 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
9010             }
9011
9012           add_AT_flag (decl_die, DW_AT_artificial, 1);
9013           add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9014           add_AT_location_description (decl_die, DW_AT_location, loc);
9015           add_AT_die_ref (subrange_die, bound_attr, decl_die);
9016         }
9017
9018       /* Else leave out the attribute.  */
9019       break;
9020
9021     case VAR_DECL:
9022     case PARM_DECL:
9023       {
9024         dw_die_ref decl_die = lookup_decl_die (bound);
9025
9026         /* ??? Can this happen, or should the variable have been bound
9027            first?  Probably it can, since I imagine that we try to create
9028            the types of parameters in the order in which they exist in
9029            the list, and won't have created a forward reference to a
9030            later parameter.  */
9031         if (decl_die != NULL)
9032           add_AT_die_ref (subrange_die, bound_attr, decl_die);
9033         break;
9034       }
9035
9036     default:
9037       {
9038         /* Otherwise try to create a stack operation procedure to
9039            evaluate the value of the array bound.  */
9040
9041         dw_die_ref ctx, decl_die;
9042         dw_loc_descr_ref loc;
9043
9044         loc = loc_descriptor_from_tree (bound, 0);
9045         if (loc == NULL)
9046           break;
9047
9048         if (current_function_decl == 0)
9049           ctx = comp_unit_die;
9050         else
9051           ctx = lookup_decl_die (current_function_decl);
9052
9053         decl_die = new_die (DW_TAG_variable, ctx, bound);
9054         add_AT_flag (decl_die, DW_AT_artificial, 1);
9055         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9056         add_AT_loc (decl_die, DW_AT_location, loc);
9057
9058         add_AT_die_ref (subrange_die, bound_attr, decl_die);
9059         break;
9060       }
9061     }
9062 }
9063
9064 /* Note that the block of subscript information for an array type also
9065    includes information about the element type of type given array type.  */
9066
9067 static void
9068 add_subscript_info (type_die, type)
9069      dw_die_ref type_die;
9070      tree type;
9071 {
9072 #ifndef MIPS_DEBUGGING_INFO
9073   unsigned dimension_number;
9074 #endif
9075   tree lower, upper;
9076   dw_die_ref subrange_die;
9077
9078   /* The GNU compilers represent multidimensional array types as sequences of
9079      one dimensional array types whose element types are themselves array
9080      types.  Here we squish that down, so that each multidimensional array
9081      type gets only one array_type DIE in the Dwarf debugging info. The draft
9082      Dwarf specification say that we are allowed to do this kind of
9083      compression in C (because there is no difference between an array or
9084      arrays and a multidimensional array in C) but for other source languages
9085      (e.g. Ada) we probably shouldn't do this.  */
9086
9087   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9088      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
9089      We work around this by disabling this feature.  See also
9090      gen_array_type_die.  */
9091 #ifndef MIPS_DEBUGGING_INFO
9092   for (dimension_number = 0;
9093        TREE_CODE (type) == ARRAY_TYPE;
9094        type = TREE_TYPE (type), dimension_number++)
9095 #endif
9096     {
9097       tree domain = TYPE_DOMAIN (type);
9098
9099       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
9100          and (in GNU C only) variable bounds.  Handle all three forms
9101          here.  */
9102       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
9103       if (domain)
9104         {
9105           /* We have an array type with specified bounds.  */
9106           lower = TYPE_MIN_VALUE (domain);
9107           upper = TYPE_MAX_VALUE (domain);
9108
9109           /* define the index type.  */
9110           if (TREE_TYPE (domain))
9111             {
9112               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
9113                  TREE_TYPE field.  We can't emit debug info for this
9114                  because it is an unnamed integral type.  */
9115               if (TREE_CODE (domain) == INTEGER_TYPE
9116                   && TYPE_NAME (domain) == NULL_TREE
9117                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
9118                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
9119                 ;
9120               else
9121                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
9122                                     type_die);
9123             }
9124
9125           /* ??? If upper is NULL, the array has unspecified length,
9126              but it does have a lower bound.  This happens with Fortran
9127                dimension arr(N:*)
9128              Since the debugger is definitely going to need to know N
9129              to produce useful results, go ahead and output the lower
9130              bound solo, and hope the debugger can cope.  */
9131
9132           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
9133           if (upper)
9134             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
9135         }
9136
9137       /* Otherwise we have an array type with an unspecified length.  The
9138          DWARF-2 spec does not say how to handle this; let's just leave out the
9139          bounds.  */
9140     }
9141 }
9142
9143 static void
9144 add_byte_size_attribute (die, tree_node)
9145      dw_die_ref die;
9146      tree tree_node;
9147 {
9148   unsigned size;
9149
9150   switch (TREE_CODE (tree_node))
9151     {
9152     case ERROR_MARK:
9153       size = 0;
9154       break;
9155     case ENUMERAL_TYPE:
9156     case RECORD_TYPE:
9157     case UNION_TYPE:
9158     case QUAL_UNION_TYPE:
9159       size = int_size_in_bytes (tree_node);
9160       break;
9161     case FIELD_DECL:
9162       /* For a data member of a struct or union, the DW_AT_byte_size is
9163          generally given as the number of bytes normally allocated for an
9164          object of the *declared* type of the member itself.  This is true
9165          even for bit-fields.  */
9166       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
9167       break;
9168     default:
9169       abort ();
9170     }
9171
9172   /* Note that `size' might be -1 when we get to this point.  If it is, that
9173      indicates that the byte size of the entity in question is variable.  We
9174      have no good way of expressing this fact in Dwarf at the present time,
9175      so just let the -1 pass on through.  */
9176   add_AT_unsigned (die, DW_AT_byte_size, size);
9177 }
9178
9179 /* For a FIELD_DECL node which represents a bit-field, output an attribute
9180    which specifies the distance in bits from the highest order bit of the
9181    "containing object" for the bit-field to the highest order bit of the
9182    bit-field itself.
9183
9184    For any given bit-field, the "containing object" is a hypothetical object
9185    (of some integral or enum type) within which the given bit-field lives.  The
9186    type of this hypothetical "containing object" is always the same as the
9187    declared type of the individual bit-field itself.  The determination of the
9188    exact location of the "containing object" for a bit-field is rather
9189    complicated.  It's handled by the `field_byte_offset' function (above).
9190
9191    Note that it is the size (in bytes) of the hypothetical "containing object"
9192    which will be given in the DW_AT_byte_size attribute for this bit-field.
9193    (See `byte_size_attribute' above).  */
9194
9195 static inline void
9196 add_bit_offset_attribute (die, decl)
9197      dw_die_ref die;
9198      tree decl;
9199 {
9200   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
9201   tree type = DECL_BIT_FIELD_TYPE (decl);
9202   HOST_WIDE_INT bitpos_int;
9203   HOST_WIDE_INT highest_order_object_bit_offset;
9204   HOST_WIDE_INT highest_order_field_bit_offset;
9205   HOST_WIDE_INT unsigned bit_offset;
9206
9207   /* Must be a field and a bit field.  */
9208   if (!type
9209       || TREE_CODE (decl) != FIELD_DECL)
9210     abort ();
9211
9212   /* We can't yet handle bit-fields whose offsets are variable, so if we
9213      encounter such things, just return without generating any attribute
9214      whatsoever.  Likewise for variable or too large size.  */
9215   if (! host_integerp (bit_position (decl), 0)
9216       || ! host_integerp (DECL_SIZE (decl), 1))
9217     return;
9218
9219   bitpos_int = int_bit_position (decl);
9220
9221   /* Note that the bit offset is always the distance (in bits) from the
9222      highest-order bit of the "containing object" to the highest-order bit of
9223      the bit-field itself.  Since the "high-order end" of any object or field
9224      is different on big-endian and little-endian machines, the computation
9225      below must take account of these differences.  */
9226   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
9227   highest_order_field_bit_offset = bitpos_int;
9228
9229   if (! BYTES_BIG_ENDIAN)
9230     {
9231       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
9232       highest_order_object_bit_offset += simple_type_size_in_bits (type);
9233     }
9234
9235   bit_offset
9236     = (! BYTES_BIG_ENDIAN
9237        ? highest_order_object_bit_offset - highest_order_field_bit_offset
9238        : highest_order_field_bit_offset - highest_order_object_bit_offset);
9239
9240   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
9241 }
9242
9243 /* For a FIELD_DECL node which represents a bit field, output an attribute
9244    which specifies the length in bits of the given field.  */
9245
9246 static inline void
9247 add_bit_size_attribute (die, decl)
9248      dw_die_ref die;
9249      tree decl;
9250 {
9251   /* Must be a field and a bit field.  */
9252   if (TREE_CODE (decl) != FIELD_DECL
9253       || ! DECL_BIT_FIELD_TYPE (decl))
9254     abort ();
9255
9256   if (host_integerp (DECL_SIZE (decl), 1))
9257     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
9258 }
9259
9260 /* If the compiled language is ANSI C, then add a 'prototyped'
9261    attribute, if arg types are given for the parameters of a function.  */
9262
9263 static inline void
9264 add_prototyped_attribute (die, func_type)
9265      dw_die_ref die;
9266      tree func_type;
9267 {
9268   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
9269       && TYPE_ARG_TYPES (func_type) != NULL)
9270     add_AT_flag (die, DW_AT_prototyped, 1);
9271 }
9272
9273 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
9274    by looking in either the type declaration or object declaration
9275    equate table.  */
9276
9277 static inline void
9278 add_abstract_origin_attribute (die, origin)
9279      dw_die_ref die;
9280      tree origin;
9281 {
9282   dw_die_ref origin_die = NULL;
9283
9284   if (TREE_CODE (origin) != FUNCTION_DECL)
9285     {
9286       /* We may have gotten separated from the block for the inlined
9287          function, if we're in an exception handler or some such; make
9288          sure that the abstract function has been written out.
9289
9290          Doing this for nested functions is wrong, however; functions are
9291          distinct units, and our context might not even be inline.  */
9292       tree fn = origin;
9293
9294       if (TYPE_P (fn))
9295         fn = TYPE_STUB_DECL (fn);
9296
9297       fn = decl_function_context (fn);
9298       if (fn)
9299         dwarf2out_abstract_function (fn);
9300     }
9301
9302   if (DECL_P (origin))
9303     origin_die = lookup_decl_die (origin);
9304   else if (TYPE_P (origin))
9305     origin_die = lookup_type_die (origin);
9306
9307   if (origin_die == NULL)
9308     abort ();
9309
9310   add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
9311 }
9312
9313 /* We do not currently support the pure_virtual attribute.  */
9314
9315 static inline void
9316 add_pure_or_virtual_attribute (die, func_decl)
9317      dw_die_ref die;
9318      tree func_decl;
9319 {
9320   if (DECL_VINDEX (func_decl))
9321     {
9322       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
9323
9324       if (host_integerp (DECL_VINDEX (func_decl), 0))
9325         add_AT_loc (die, DW_AT_vtable_elem_location,
9326                     new_loc_descr (DW_OP_constu,
9327                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
9328                                    0));
9329
9330       /* GNU extension: Record what type this method came from originally.  */
9331       if (debug_info_level > DINFO_LEVEL_TERSE)
9332         add_AT_die_ref (die, DW_AT_containing_type,
9333                         lookup_type_die (DECL_CONTEXT (func_decl)));
9334     }
9335 }
9336 \f
9337 /* Add source coordinate attributes for the given decl.  */
9338
9339 static void
9340 add_src_coords_attributes (die, decl)
9341      dw_die_ref die;
9342      tree decl;
9343 {
9344   unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
9345
9346   add_AT_unsigned (die, DW_AT_decl_file, file_index);
9347   add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
9348 }
9349
9350 /* Add an DW_AT_name attribute and source coordinate attribute for the
9351    given decl, but only if it actually has a name.  */
9352
9353 static void
9354 add_name_and_src_coords_attributes (die, decl)
9355      dw_die_ref die;
9356      tree decl;
9357 {
9358   tree decl_name;
9359
9360   decl_name = DECL_NAME (decl);
9361   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
9362     {
9363       add_name_attribute (die, dwarf2_name (decl, 0));
9364       if (! DECL_ARTIFICIAL (decl))
9365         add_src_coords_attributes (die, decl);
9366
9367       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
9368           && TREE_PUBLIC (decl)
9369           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
9370           && !DECL_ABSTRACT (decl))
9371         add_AT_string (die, DW_AT_MIPS_linkage_name,
9372                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
9373     }
9374
9375 #ifdef VMS_DEBUGGING_INFO
9376   /* Get the function's name, as described by its RTL.  This may be different
9377      from the DECL_NAME name used in the source file.  */
9378   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
9379     {
9380       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
9381                    XEXP (DECL_RTL (decl), 0));
9382       VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
9383     }
9384 #endif
9385 }
9386
9387 /* Push a new declaration scope.  */
9388
9389 static void
9390 push_decl_scope (scope)
9391      tree scope;
9392 {
9393   VARRAY_PUSH_TREE (decl_scope_table, scope);
9394 }
9395
9396 /* Pop a declaration scope.  */
9397
9398 static inline void
9399 pop_decl_scope ()
9400 {
9401   if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
9402     abort ();
9403
9404   VARRAY_POP (decl_scope_table);
9405 }
9406
9407 /* Return the DIE for the scope that immediately contains this type.
9408    Non-named types get global scope.  Named types nested in other
9409    types get their containing scope if it's open, or global scope
9410    otherwise.  All other types (i.e. function-local named types) get
9411    the current active scope.  */
9412
9413 static dw_die_ref
9414 scope_die_for (t, context_die)
9415      tree t;
9416      dw_die_ref context_die;
9417 {
9418   dw_die_ref scope_die = NULL;
9419   tree containing_scope;
9420   int i;
9421
9422   /* Non-types always go in the current scope.  */
9423   if (! TYPE_P (t))
9424     abort ();
9425
9426   containing_scope = TYPE_CONTEXT (t);
9427
9428   /* Ignore namespaces for the moment.  */
9429   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
9430     containing_scope = NULL_TREE;
9431
9432   /* Ignore function type "scopes" from the C frontend.  They mean that
9433      a tagged type is local to a parmlist of a function declarator, but
9434      that isn't useful to DWARF.  */
9435   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
9436     containing_scope = NULL_TREE;
9437
9438   if (containing_scope == NULL_TREE)
9439     scope_die = comp_unit_die;
9440   else if (TYPE_P (containing_scope))
9441     {
9442       /* For types, we can just look up the appropriate DIE.  But
9443          first we check to see if we're in the middle of emitting it
9444          so we know where the new DIE should go.  */
9445       for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
9446         if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
9447           break;
9448
9449       if (i < 0)
9450         {
9451           if (debug_info_level > DINFO_LEVEL_TERSE
9452               && !TREE_ASM_WRITTEN (containing_scope))
9453             abort ();
9454
9455           /* If none of the current dies are suitable, we get file scope.  */
9456           scope_die = comp_unit_die;
9457         }
9458       else
9459         scope_die = lookup_type_die (containing_scope);
9460     }
9461   else
9462     scope_die = context_die;
9463
9464   return scope_die;
9465 }
9466
9467 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
9468
9469 static inline int
9470 local_scope_p (context_die)
9471      dw_die_ref context_die;
9472 {
9473   for (; context_die; context_die = context_die->die_parent)
9474     if (context_die->die_tag == DW_TAG_inlined_subroutine
9475         || context_die->die_tag == DW_TAG_subprogram)
9476       return 1;
9477
9478   return 0;
9479 }
9480
9481 /* Returns nonzero if CONTEXT_DIE is a class.  */
9482
9483 static inline int
9484 class_scope_p (context_die)
9485      dw_die_ref context_die;
9486 {
9487   return (context_die
9488           && (context_die->die_tag == DW_TAG_structure_type
9489               || context_die->die_tag == DW_TAG_union_type));
9490 }
9491
9492 /* Many forms of DIEs require a "type description" attribute.  This
9493    routine locates the proper "type descriptor" die for the type given
9494    by 'type', and adds an DW_AT_type attribute below the given die.  */
9495
9496 static void
9497 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
9498      dw_die_ref object_die;
9499      tree type;
9500      int decl_const;
9501      int decl_volatile;
9502      dw_die_ref context_die;
9503 {
9504   enum tree_code code  = TREE_CODE (type);
9505   dw_die_ref type_die  = NULL;
9506
9507   /* ??? If this type is an unnamed subrange type of an integral or
9508      floating-point type, use the inner type.  This is because we have no
9509      support for unnamed types in base_type_die.  This can happen if this is
9510      an Ada subrange type.  Correct solution is emit a subrange type die.  */
9511   if ((code == INTEGER_TYPE || code == REAL_TYPE)
9512       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
9513     type = TREE_TYPE (type), code = TREE_CODE (type);
9514
9515   if (code == ERROR_MARK
9516       /* Handle a special case.  For functions whose return type is void, we
9517          generate *no* type attribute.  (Note that no object may have type
9518          `void', so this only applies to function return types).  */
9519       || code == VOID_TYPE)
9520     return;
9521
9522   type_die = modified_type_die (type,
9523                                 decl_const || TYPE_READONLY (type),
9524                                 decl_volatile || TYPE_VOLATILE (type),
9525                                 context_die);
9526
9527   if (type_die != NULL)
9528     add_AT_die_ref (object_die, DW_AT_type, type_die);
9529 }
9530
9531 /* Given a tree pointer to a struct, class, union, or enum type node, return
9532    a pointer to the (string) tag name for the given type, or zero if the type
9533    was declared without a tag.  */
9534
9535 static const char *
9536 type_tag (type)
9537      tree type;
9538 {
9539   const char *name = 0;
9540
9541   if (TYPE_NAME (type) != 0)
9542     {
9543       tree t = 0;
9544
9545       /* Find the IDENTIFIER_NODE for the type name.  */
9546       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9547         t = TYPE_NAME (type);
9548
9549       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
9550          a TYPE_DECL node, regardless of whether or not a `typedef' was
9551          involved.  */
9552       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9553                && ! DECL_IGNORED_P (TYPE_NAME (type)))
9554         t = DECL_NAME (TYPE_NAME (type));
9555
9556       /* Now get the name as a string, or invent one.  */
9557       if (t != 0)
9558         name = IDENTIFIER_POINTER (t);
9559     }
9560
9561   return (name == 0 || *name == '\0') ? 0 : name;
9562 }
9563
9564 /* Return the type associated with a data member, make a special check
9565    for bit field types.  */
9566
9567 static inline tree
9568 member_declared_type (member)
9569      tree member;
9570 {
9571   return (DECL_BIT_FIELD_TYPE (member)
9572           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
9573 }
9574
9575 /* Get the decl's label, as described by its RTL. This may be different
9576    from the DECL_NAME name used in the source file.  */
9577
9578 #if 0
9579 static const char *
9580 decl_start_label (decl)
9581      tree decl;
9582 {
9583   rtx x;
9584   const char *fnname;
9585
9586   x = DECL_RTL (decl);
9587   if (GET_CODE (x) != MEM)
9588     abort ();
9589
9590   x = XEXP (x, 0);
9591   if (GET_CODE (x) != SYMBOL_REF)
9592     abort ();
9593
9594   fnname = XSTR (x, 0);
9595   return fnname;
9596 }
9597 #endif
9598 \f
9599 /* These routines generate the internal representation of the DIE's for
9600    the compilation unit.  Debugging information is collected by walking
9601    the declaration trees passed in from dwarf2out_decl().  */
9602
9603 static void
9604 gen_array_type_die (type, context_die)
9605      tree type;
9606      dw_die_ref context_die;
9607 {
9608   dw_die_ref scope_die = scope_die_for (type, context_die);
9609   dw_die_ref array_die;
9610   tree element_type;
9611
9612   /* ??? The SGI dwarf reader fails for array of array of enum types unless
9613      the inner array type comes before the outer array type.  Thus we must
9614      call gen_type_die before we call new_die.  See below also.  */
9615 #ifdef MIPS_DEBUGGING_INFO
9616   gen_type_die (TREE_TYPE (type), context_die);
9617 #endif
9618
9619   array_die = new_die (DW_TAG_array_type, scope_die, type);
9620
9621 #if 0
9622   /* We default the array ordering.  SDB will probably do
9623      the right things even if DW_AT_ordering is not present.  It's not even
9624      an issue until we start to get into multidimensional arrays anyway.  If
9625      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
9626      then we'll have to put the DW_AT_ordering attribute back in.  (But if
9627      and when we find out that we need to put these in, we will only do so
9628      for multidimensional arrays.  */
9629   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
9630 #endif
9631
9632 #ifdef MIPS_DEBUGGING_INFO
9633   /* The SGI compilers handle arrays of unknown bound by setting
9634      AT_declaration and not emitting any subrange DIEs.  */
9635   if (! TYPE_DOMAIN (type))
9636     add_AT_unsigned (array_die, DW_AT_declaration, 1);
9637   else
9638 #endif
9639     add_subscript_info (array_die, type);
9640
9641   add_name_attribute (array_die, type_tag (type));
9642   equate_type_number_to_die (type, array_die);
9643
9644   /* Add representation of the type of the elements of this array type.  */
9645   element_type = TREE_TYPE (type);
9646
9647   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9648      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
9649      We work around this by disabling this feature.  See also
9650      add_subscript_info.  */
9651 #ifndef MIPS_DEBUGGING_INFO
9652   while (TREE_CODE (element_type) == ARRAY_TYPE)
9653     element_type = TREE_TYPE (element_type);
9654
9655   gen_type_die (element_type, context_die);
9656 #endif
9657
9658   add_type_attribute (array_die, element_type, 0, 0, context_die);
9659 }
9660
9661 static void
9662 gen_set_type_die (type, context_die)
9663      tree type;
9664      dw_die_ref context_die;
9665 {
9666   dw_die_ref type_die
9667     = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
9668
9669   equate_type_number_to_die (type, type_die);
9670   add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
9671 }
9672
9673 #if 0
9674 static void
9675 gen_entry_point_die (decl, context_die)
9676      tree decl;
9677      dw_die_ref context_die;
9678 {
9679   tree origin = decl_ultimate_origin (decl);
9680   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
9681
9682   if (origin != NULL)
9683     add_abstract_origin_attribute (decl_die, origin);
9684   else
9685     {
9686       add_name_and_src_coords_attributes (decl_die, decl);
9687       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
9688                           0, 0, context_die);
9689     }
9690
9691   if (DECL_ABSTRACT (decl))
9692     equate_decl_number_to_die (decl, decl_die);
9693   else
9694     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
9695 }
9696 #endif
9697
9698 /* Walk through the list of incomplete types again, trying once more to
9699    emit full debugging info for them.  */
9700
9701 static void
9702 retry_incomplete_types ()
9703 {
9704   int i;
9705
9706   for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
9707     gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
9708 }
9709
9710 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
9711
9712 static void
9713 gen_inlined_enumeration_type_die (type, context_die)
9714      tree type;
9715      dw_die_ref context_die;
9716 {
9717   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
9718
9719   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9720      be incomplete and such types are not marked.  */
9721   add_abstract_origin_attribute (type_die, type);
9722 }
9723
9724 /* Generate a DIE to represent an inlined instance of a structure type.  */
9725
9726 static void
9727 gen_inlined_structure_type_die (type, context_die)
9728      tree type;
9729      dw_die_ref context_die;
9730 {
9731   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
9732
9733   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9734      be incomplete and such types are not marked.  */
9735   add_abstract_origin_attribute (type_die, type);
9736 }
9737
9738 /* Generate a DIE to represent an inlined instance of a union type.  */
9739
9740 static void
9741 gen_inlined_union_type_die (type, context_die)
9742      tree type;
9743      dw_die_ref context_die;
9744 {
9745   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
9746
9747   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9748      be incomplete and such types are not marked.  */
9749   add_abstract_origin_attribute (type_die, type);
9750 }
9751
9752 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
9753    include all of the information about the enumeration values also. Each
9754    enumerated type name/value is listed as a child of the enumerated type
9755    DIE.  */
9756
9757 static void
9758 gen_enumeration_type_die (type, context_die)
9759      tree type;
9760      dw_die_ref context_die;
9761 {
9762   dw_die_ref type_die = lookup_type_die (type);
9763
9764   if (type_die == NULL)
9765     {
9766       type_die = new_die (DW_TAG_enumeration_type,
9767                           scope_die_for (type, context_die), type);
9768       equate_type_number_to_die (type, type_die);
9769       add_name_attribute (type_die, type_tag (type));
9770     }
9771   else if (! TYPE_SIZE (type))
9772     return;
9773   else
9774     remove_AT (type_die, DW_AT_declaration);
9775
9776   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
9777      given enum type is incomplete, do not generate the DW_AT_byte_size
9778      attribute or the DW_AT_element_list attribute.  */
9779   if (TYPE_SIZE (type))
9780     {
9781       tree link;
9782
9783       TREE_ASM_WRITTEN (type) = 1;
9784       add_byte_size_attribute (type_die, type);
9785       if (TYPE_STUB_DECL (type) != NULL_TREE)
9786         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
9787
9788       /* If the first reference to this type was as the return type of an
9789          inline function, then it may not have a parent.  Fix this now.  */
9790       if (type_die->die_parent == NULL)
9791         add_child_die (scope_die_for (type, context_die), type_die);
9792
9793       for (link = TYPE_FIELDS (type);
9794            link != NULL; link = TREE_CHAIN (link))
9795         {
9796           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
9797
9798           add_name_attribute (enum_die,
9799                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
9800
9801           if (host_integerp (TREE_VALUE (link), 0))
9802             {
9803               if (tree_int_cst_sgn (TREE_VALUE (link)) < 0)
9804                 add_AT_int (enum_die, DW_AT_const_value,
9805                             tree_low_cst (TREE_VALUE (link), 0));
9806               else
9807                 add_AT_unsigned (enum_die, DW_AT_const_value,
9808                                  tree_low_cst (TREE_VALUE (link), 0));
9809             }
9810         }
9811     }
9812   else
9813     add_AT_flag (type_die, DW_AT_declaration, 1);
9814 }
9815
9816 /* Generate a DIE to represent either a real live formal parameter decl or to
9817    represent just the type of some formal parameter position in some function
9818    type.
9819
9820    Note that this routine is a bit unusual because its argument may be a
9821    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
9822    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
9823    node.  If it's the former then this function is being called to output a
9824    DIE to represent a formal parameter object (or some inlining thereof).  If
9825    it's the latter, then this function is only being called to output a
9826    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
9827    argument type of some subprogram type.  */
9828
9829 static dw_die_ref
9830 gen_formal_parameter_die (node, context_die)
9831      tree node;
9832      dw_die_ref context_die;
9833 {
9834   dw_die_ref parm_die
9835     = new_die (DW_TAG_formal_parameter, context_die, node);
9836   tree origin;
9837
9838   switch (TREE_CODE_CLASS (TREE_CODE (node)))
9839     {
9840     case 'd':
9841       origin = decl_ultimate_origin (node);
9842       if (origin != NULL)
9843         add_abstract_origin_attribute (parm_die, origin);
9844       else
9845         {
9846           add_name_and_src_coords_attributes (parm_die, node);
9847           add_type_attribute (parm_die, TREE_TYPE (node),
9848                               TREE_READONLY (node),
9849                               TREE_THIS_VOLATILE (node),
9850                               context_die);
9851           if (DECL_ARTIFICIAL (node))
9852             add_AT_flag (parm_die, DW_AT_artificial, 1);
9853         }
9854
9855       equate_decl_number_to_die (node, parm_die);
9856       if (! DECL_ABSTRACT (node))
9857         add_location_or_const_value_attribute (parm_die, node);
9858
9859       break;
9860
9861     case 't':
9862       /* We were called with some kind of a ..._TYPE node.  */
9863       add_type_attribute (parm_die, node, 0, 0, context_die);
9864       break;
9865
9866     default:
9867       abort ();
9868     }
9869
9870   return parm_die;
9871 }
9872
9873 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
9874    at the end of an (ANSI prototyped) formal parameters list.  */
9875
9876 static void
9877 gen_unspecified_parameters_die (decl_or_type, context_die)
9878      tree decl_or_type;
9879      dw_die_ref context_die;
9880 {
9881   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
9882 }
9883
9884 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
9885    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
9886    parameters as specified in some function type specification (except for
9887    those which appear as part of a function *definition*).  */
9888
9889 static void
9890 gen_formal_types_die (function_or_method_type, context_die)
9891      tree function_or_method_type;
9892      dw_die_ref context_die;
9893 {
9894   tree link;
9895   tree formal_type = NULL;
9896   tree first_parm_type;
9897   tree arg;
9898
9899   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
9900     {
9901       arg = DECL_ARGUMENTS (function_or_method_type);
9902       function_or_method_type = TREE_TYPE (function_or_method_type);
9903     }
9904   else
9905     arg = NULL_TREE;
9906   
9907   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
9908
9909   /* Make our first pass over the list of formal parameter types and output a
9910      DW_TAG_formal_parameter DIE for each one.  */
9911   for (link = first_parm_type; link; )
9912     {
9913       dw_die_ref parm_die;
9914
9915       formal_type = TREE_VALUE (link);
9916       if (formal_type == void_type_node)
9917         break;
9918
9919       /* Output a (nameless) DIE to represent the formal parameter itself.  */
9920       parm_die = gen_formal_parameter_die (formal_type, context_die);
9921       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
9922            && link == first_parm_type)
9923           || (arg && DECL_ARTIFICIAL (arg)))
9924         add_AT_flag (parm_die, DW_AT_artificial, 1);
9925
9926       link = TREE_CHAIN (link);
9927       if (arg)
9928         arg = TREE_CHAIN (arg);
9929     }
9930
9931   /* If this function type has an ellipsis, add a
9932      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
9933   if (formal_type != void_type_node)
9934     gen_unspecified_parameters_die (function_or_method_type, context_die);
9935
9936   /* Make our second (and final) pass over the list of formal parameter types
9937      and output DIEs to represent those types (as necessary).  */
9938   for (link = TYPE_ARG_TYPES (function_or_method_type);
9939        link && TREE_VALUE (link);
9940        link = TREE_CHAIN (link))
9941     gen_type_die (TREE_VALUE (link), context_die);
9942 }
9943
9944 /* We want to generate the DIE for TYPE so that we can generate the
9945    die for MEMBER, which has been defined; we will need to refer back
9946    to the member declaration nested within TYPE.  If we're trying to
9947    generate minimal debug info for TYPE, processing TYPE won't do the
9948    trick; we need to attach the member declaration by hand.  */
9949
9950 static void
9951 gen_type_die_for_member (type, member, context_die)
9952      tree type, member;
9953      dw_die_ref context_die;
9954 {
9955   gen_type_die (type, context_die);
9956
9957   /* If we're trying to avoid duplicate debug info, we may not have
9958      emitted the member decl for this function.  Emit it now.  */
9959   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
9960       && ! lookup_decl_die (member))
9961     {
9962       if (decl_ultimate_origin (member))
9963         abort ();
9964
9965       push_decl_scope (type);
9966       if (TREE_CODE (member) == FUNCTION_DECL)
9967         gen_subprogram_die (member, lookup_type_die (type));
9968       else
9969         gen_variable_die (member, lookup_type_die (type));
9970
9971       pop_decl_scope ();
9972     }
9973 }
9974
9975 /* Generate the DWARF2 info for the "abstract" instance of a function which we
9976    may later generate inlined and/or out-of-line instances of.  */
9977
9978 static void
9979 dwarf2out_abstract_function (decl)
9980      tree decl;
9981 {
9982   dw_die_ref old_die;
9983   tree save_fn;
9984   tree context;
9985   int was_abstract = DECL_ABSTRACT (decl);
9986
9987   /* Make sure we have the actual abstract inline, not a clone.  */
9988   decl = DECL_ORIGIN (decl);
9989
9990   old_die = lookup_decl_die (decl);  
9991   if (old_die && get_AT_unsigned (old_die, DW_AT_inline))
9992     /* We've already generated the abstract instance.  */
9993     return;
9994
9995   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
9996      we don't get confused by DECL_ABSTRACT.  */
9997   if (debug_info_level > DINFO_LEVEL_TERSE)
9998     {
9999       context = decl_class_context (decl);
10000       if (context)
10001         gen_type_die_for_member
10002           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
10003     }
10004  
10005   /* Pretend we've just finished compiling this function.  */
10006   save_fn = current_function_decl;
10007   current_function_decl = decl;
10008
10009   set_decl_abstract_flags (decl, 1);
10010   dwarf2out_decl (decl);
10011   if (! was_abstract)
10012     set_decl_abstract_flags (decl, 0);
10013
10014   current_function_decl = save_fn;
10015 }
10016
10017 /* Generate a DIE to represent a declared function (either file-scope or
10018    block-local).  */
10019
10020 static void
10021 gen_subprogram_die (decl, context_die)
10022      tree decl;
10023      dw_die_ref context_die;
10024 {
10025   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10026   tree origin = decl_ultimate_origin (decl);
10027   dw_die_ref subr_die;
10028   rtx fp_reg;
10029   tree fn_arg_types;
10030   tree outer_scope;
10031   dw_die_ref old_die = lookup_decl_die (decl);
10032   int declaration = (current_function_decl != decl
10033                      || class_scope_p (context_die));
10034
10035   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
10036      started to generate the abstract instance of an inline, decided to output
10037      its containing class, and proceeded to emit the declaration of the inline
10038      from the member list for the class.  If so, DECLARATION takes priority;
10039      we'll get back to the abstract instance when done with the class.  */
10040
10041   /* The class-scope declaration DIE must be the primary DIE.  */
10042   if (origin && declaration && class_scope_p (context_die))
10043     {
10044       origin = NULL;
10045       if (old_die)
10046         abort ();
10047     }
10048
10049   if (origin != NULL)
10050     {
10051       if (declaration && ! local_scope_p (context_die))
10052         abort ();
10053
10054       /* Fixup die_parent for the abstract instance of a nested
10055          inline function.  */
10056       if (old_die && old_die->die_parent == NULL)
10057         add_child_die (context_die, old_die);
10058
10059       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10060       add_abstract_origin_attribute (subr_die, origin);
10061     }
10062   else if (old_die)
10063     {
10064       unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10065
10066       if (!get_AT_flag (old_die, DW_AT_declaration)
10067           /* We can have a normal definition following an inline one in the
10068              case of redefinition of GNU C extern inlines.
10069              It seems reasonable to use AT_specification in this case.  */
10070           && !get_AT_unsigned (old_die, DW_AT_inline))
10071         {
10072           /* ??? This can happen if there is a bug in the program, for
10073              instance, if it has duplicate function definitions.  Ideally,
10074              we should detect this case and ignore it.  For now, if we have
10075              already reported an error, any error at all, then assume that
10076              we got here because of an input error, not a dwarf2 bug.  */
10077           if (errorcount)
10078             return;
10079           abort ();
10080         }
10081
10082       /* If the definition comes from the same place as the declaration,
10083          maybe use the old DIE.  We always want the DIE for this function
10084          that has the *_pc attributes to be under comp_unit_die so the
10085          debugger can find it.  We also need to do this for abstract
10086          instances of inlines, since the spec requires the out-of-line copy
10087          to have the same parent.  For local class methods, this doesn't
10088          apply; we just use the old DIE.  */
10089       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
10090           && (DECL_ARTIFICIAL (decl)
10091               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
10092                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
10093                       == (unsigned) DECL_SOURCE_LINE (decl)))))
10094         {
10095           subr_die = old_die;
10096
10097           /* Clear out the declaration attribute and the parm types.  */
10098           remove_AT (subr_die, DW_AT_declaration);
10099           remove_children (subr_die);
10100         }
10101       else
10102         {
10103           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10104           add_AT_die_ref (subr_die, DW_AT_specification, old_die);
10105           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10106             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
10107           if (get_AT_unsigned (old_die, DW_AT_decl_line)
10108               != (unsigned) DECL_SOURCE_LINE (decl))
10109             add_AT_unsigned
10110               (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
10111         }
10112     }
10113   else
10114     {
10115       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10116
10117       if (TREE_PUBLIC (decl))
10118         add_AT_flag (subr_die, DW_AT_external, 1);
10119
10120       add_name_and_src_coords_attributes (subr_die, decl);
10121       if (debug_info_level > DINFO_LEVEL_TERSE)
10122         {
10123           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
10124           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
10125                               0, 0, context_die);
10126         }
10127
10128       add_pure_or_virtual_attribute (subr_die, decl);
10129       if (DECL_ARTIFICIAL (decl))
10130         add_AT_flag (subr_die, DW_AT_artificial, 1);
10131
10132       if (TREE_PROTECTED (decl))
10133         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
10134       else if (TREE_PRIVATE (decl))
10135         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
10136     }
10137
10138   if (declaration)
10139     {
10140       if (!old_die || !get_AT_unsigned (old_die, DW_AT_inline))
10141         {
10142           add_AT_flag (subr_die, DW_AT_declaration, 1);
10143
10144           /* The first time we see a member function, it is in the context of
10145              the class to which it belongs.  We make sure of this by emitting
10146              the class first.  The next time is the definition, which is
10147              handled above.  The two may come from the same source text.  */
10148           if (DECL_CONTEXT (decl) || DECL_ABSTRACT (decl))
10149             equate_decl_number_to_die (decl, subr_die);
10150         }
10151     }
10152   else if (DECL_ABSTRACT (decl))
10153     {
10154       if (DECL_INLINE (decl) && !flag_no_inline)
10155         {
10156           /* ??? Checking DECL_DEFER_OUTPUT is correct for static
10157              inline functions, but not for extern inline functions.
10158              We can't get this completely correct because information
10159              about whether the function was declared inline is not
10160              saved anywhere.  */
10161           if (DECL_DEFER_OUTPUT (decl))
10162             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
10163           else
10164             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
10165         }
10166       else
10167         add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
10168
10169       equate_decl_number_to_die (decl, subr_die);
10170     }
10171   else if (!DECL_EXTERNAL (decl))
10172     {
10173       if (!old_die || !get_AT_unsigned (old_die, DW_AT_inline))
10174         equate_decl_number_to_die (decl, subr_die);
10175
10176       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
10177                                    current_funcdef_number);
10178       add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
10179       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10180                                    current_funcdef_number);
10181       add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
10182
10183       add_pubname (decl, subr_die);
10184       add_arange (decl, subr_die);
10185
10186 #ifdef MIPS_DEBUGGING_INFO
10187       /* Add a reference to the FDE for this routine.  */
10188       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
10189 #endif
10190
10191       /* Define the "frame base" location for this routine.  We use the
10192          frame pointer or stack pointer registers, since the RTL for local
10193          variables is relative to one of them.  */
10194       fp_reg
10195         = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
10196       add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
10197
10198 #if 0
10199       /* ??? This fails for nested inline functions, because context_display
10200          is not part of the state saved/restored for inline functions.  */
10201       if (current_function_needs_context)
10202         add_AT_location_description (subr_die, DW_AT_static_link,
10203                                      lookup_static_chain (decl));
10204 #endif
10205     }
10206
10207   /* Now output descriptions of the arguments for this function. This gets
10208      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
10209      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
10210      `...' at the end of the formal parameter list.  In order to find out if
10211      there was a trailing ellipsis or not, we must instead look at the type
10212      associated with the FUNCTION_DECL.  This will be a node of type
10213      FUNCTION_TYPE. If the chain of type nodes hanging off of this
10214      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
10215      an ellipsis at the end.  */
10216
10217   /* In the case where we are describing a mere function declaration, all we
10218      need to do here (and all we *can* do here) is to describe the *types* of
10219      its formal parameters.  */
10220   if (debug_info_level <= DINFO_LEVEL_TERSE)
10221     ;
10222   else if (declaration)
10223     gen_formal_types_die (decl, subr_die);
10224   else
10225     {
10226       /* Generate DIEs to represent all known formal parameters */
10227       tree arg_decls = DECL_ARGUMENTS (decl);
10228       tree parm;
10229
10230       /* When generating DIEs, generate the unspecified_parameters DIE
10231          instead if we come across the arg "__builtin_va_alist" */
10232       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
10233         if (TREE_CODE (parm) == PARM_DECL)
10234           {
10235             if (DECL_NAME (parm)
10236                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
10237                             "__builtin_va_alist"))
10238               gen_unspecified_parameters_die (parm, subr_die);
10239             else
10240               gen_decl_die (parm, subr_die);
10241           }
10242
10243       /* Decide whether we need an unspecified_parameters DIE at the end.
10244          There are 2 more cases to do this for: 1) the ansi ... declaration -
10245          this is detectable when the end of the arg list is not a
10246          void_type_node 2) an unprototyped function declaration (not a
10247          definition).  This just means that we have no info about the
10248          parameters at all.  */
10249       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
10250       if (fn_arg_types != NULL)
10251         {
10252           /* this is the prototyped case, check for ...  */
10253           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
10254             gen_unspecified_parameters_die (decl, subr_die);
10255         }
10256       else if (DECL_INITIAL (decl) == NULL_TREE)
10257         gen_unspecified_parameters_die (decl, subr_die);
10258     }
10259
10260   /* Output Dwarf info for all of the stuff within the body of the function
10261      (if it has one - it may be just a declaration).  */
10262   outer_scope = DECL_INITIAL (decl);
10263
10264   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
10265      a function.  This BLOCK actually represents the outermost binding contour
10266      for the function, i.e. the contour in which the function's formal
10267      parameters and labels get declared. Curiously, it appears that the front
10268      end doesn't actually put the PARM_DECL nodes for the current function onto
10269      the BLOCK_VARS list for this outer scope, but are strung off of the
10270      DECL_ARGUMENTS list for the function instead.
10271
10272      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
10273      the LABEL_DECL nodes for the function however, and we output DWARF info
10274      for those in decls_for_scope.  Just within the `outer_scope' there will be
10275      a BLOCK node representing the function's outermost pair of curly braces,
10276      and any blocks used for the base and member initializers of a C++
10277      constructor function.  */
10278   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
10279     {
10280       current_function_has_inlines = 0;
10281       decls_for_scope (outer_scope, subr_die, 0);
10282
10283 #if 0 && defined (MIPS_DEBUGGING_INFO)
10284       if (current_function_has_inlines)
10285         {
10286           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
10287           if (! comp_unit_has_inlines)
10288             {
10289               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
10290               comp_unit_has_inlines = 1;
10291             }
10292         }
10293 #endif
10294     }
10295 }
10296
10297 /* Generate a DIE to represent a declared data object.  */
10298
10299 static void
10300 gen_variable_die (decl, context_die)
10301      tree decl;
10302      dw_die_ref context_die;
10303 {
10304   tree origin = decl_ultimate_origin (decl);
10305   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
10306
10307   dw_die_ref old_die = lookup_decl_die (decl);
10308   int declaration = (DECL_EXTERNAL (decl)
10309                      || class_scope_p (context_die));
10310
10311   if (origin != NULL)
10312     add_abstract_origin_attribute (var_die, origin);
10313
10314   /* Loop unrolling can create multiple blocks that refer to the same
10315      static variable, so we must test for the DW_AT_declaration flag.
10316
10317      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
10318      copy decls and set the DECL_ABSTRACT flag on them instead of
10319      sharing them.
10320
10321      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
10322   else if (old_die && TREE_STATIC (decl)
10323            && get_AT_flag (old_die, DW_AT_declaration) == 1)
10324     {
10325       /* This is a definition of a C++ class level static.  */
10326       add_AT_die_ref (var_die, DW_AT_specification, old_die);
10327       if (DECL_NAME (decl))
10328         {
10329           unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10330
10331           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10332             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
10333
10334           if (get_AT_unsigned (old_die, DW_AT_decl_line)
10335               != (unsigned) DECL_SOURCE_LINE (decl))
10336
10337             add_AT_unsigned (var_die, DW_AT_decl_line,
10338                              DECL_SOURCE_LINE (decl));
10339         }
10340     }
10341   else
10342     {
10343       add_name_and_src_coords_attributes (var_die, decl);
10344       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
10345                           TREE_THIS_VOLATILE (decl), context_die);
10346
10347       if (TREE_PUBLIC (decl))
10348         add_AT_flag (var_die, DW_AT_external, 1);
10349
10350       if (DECL_ARTIFICIAL (decl))
10351         add_AT_flag (var_die, DW_AT_artificial, 1);
10352
10353       if (TREE_PROTECTED (decl))
10354         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
10355       else if (TREE_PRIVATE (decl))
10356         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
10357     }
10358
10359   if (declaration)
10360     add_AT_flag (var_die, DW_AT_declaration, 1);
10361
10362   if (class_scope_p (context_die) || DECL_ABSTRACT (decl))
10363     equate_decl_number_to_die (decl, var_die);
10364
10365   if (! declaration && ! DECL_ABSTRACT (decl))
10366     {
10367       add_location_or_const_value_attribute (var_die, decl);
10368       add_pubname (decl, var_die);
10369     }
10370   else
10371     tree_add_const_value_attribute (var_die, decl);
10372 }
10373
10374 /* Generate a DIE to represent a label identifier.  */
10375
10376 static void
10377 gen_label_die (decl, context_die)
10378      tree decl;
10379      dw_die_ref context_die;
10380 {
10381   tree origin = decl_ultimate_origin (decl);
10382   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
10383   rtx insn;
10384   char label[MAX_ARTIFICIAL_LABEL_BYTES];
10385
10386   if (origin != NULL)
10387     add_abstract_origin_attribute (lbl_die, origin);
10388   else
10389     add_name_and_src_coords_attributes (lbl_die, decl);
10390
10391   if (DECL_ABSTRACT (decl))
10392     equate_decl_number_to_die (decl, lbl_die);
10393   else
10394     {
10395       insn = DECL_RTL (decl);
10396
10397       /* Deleted labels are programmer specified labels which have been
10398          eliminated because of various optimisations.  We still emit them
10399          here so that it is possible to put breakpoints on them.  */
10400       if (GET_CODE (insn) == CODE_LABEL
10401           || ((GET_CODE (insn) == NOTE
10402                && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
10403         {
10404           /* When optimization is enabled (via -O) some parts of the compiler
10405              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
10406              represent source-level labels which were explicitly declared by
10407              the user.  This really shouldn't be happening though, so catch
10408              it if it ever does happen.  */
10409           if (INSN_DELETED_P (insn))
10410             abort ();
10411
10412           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
10413           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
10414         }
10415     }
10416 }
10417
10418 /* Generate a DIE for a lexical block.  */
10419
10420 static void
10421 gen_lexical_block_die (stmt, context_die, depth)
10422      tree stmt;
10423      dw_die_ref context_die;
10424      int depth;
10425 {
10426   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
10427   char label[MAX_ARTIFICIAL_LABEL_BYTES];
10428
10429   if (! BLOCK_ABSTRACT (stmt))
10430     {
10431       if (BLOCK_FRAGMENT_CHAIN (stmt))
10432         {
10433           tree chain;
10434
10435           add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
10436
10437           chain = BLOCK_FRAGMENT_CHAIN (stmt);
10438           do
10439             {
10440               add_ranges (chain);
10441               chain = BLOCK_FRAGMENT_CHAIN (chain);
10442             }
10443           while (chain);
10444           add_ranges (NULL);
10445         }
10446       else
10447         {
10448           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10449                                        BLOCK_NUMBER (stmt));
10450           add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
10451           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10452                                        BLOCK_NUMBER (stmt));
10453           add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
10454         }
10455     }
10456
10457   decls_for_scope (stmt, stmt_die, depth);
10458 }
10459
10460 /* Generate a DIE for an inlined subprogram.  */
10461
10462 static void
10463 gen_inlined_subroutine_die (stmt, context_die, depth)
10464      tree stmt;
10465      dw_die_ref context_die;
10466      int depth;
10467 {
10468   if (! BLOCK_ABSTRACT (stmt))
10469     {
10470       dw_die_ref subr_die
10471         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
10472       tree decl = block_ultimate_origin (stmt);
10473       char label[MAX_ARTIFICIAL_LABEL_BYTES];
10474
10475       /* Emit info for the abstract instance first, if we haven't yet.  */
10476       dwarf2out_abstract_function (decl);
10477
10478       add_abstract_origin_attribute (subr_die, decl);
10479       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10480                                    BLOCK_NUMBER (stmt));
10481       add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
10482       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10483                                    BLOCK_NUMBER (stmt));
10484       add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
10485       decls_for_scope (stmt, subr_die, depth);
10486       current_function_has_inlines = 1;
10487     }
10488 }
10489
10490 /* Generate a DIE for a field in a record, or structure.  */
10491
10492 static void
10493 gen_field_die (decl, context_die)
10494      tree decl;
10495      dw_die_ref context_die;
10496 {
10497   dw_die_ref decl_die = new_die (DW_TAG_member, context_die, decl);
10498
10499   add_name_and_src_coords_attributes (decl_die, decl);
10500   add_type_attribute (decl_die, member_declared_type (decl),
10501                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
10502                       context_die);
10503
10504   if (DECL_BIT_FIELD_TYPE (decl))
10505     {
10506       add_byte_size_attribute (decl_die, decl);
10507       add_bit_size_attribute (decl_die, decl);
10508       add_bit_offset_attribute (decl_die, decl);
10509     }
10510
10511   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
10512     add_data_member_location_attribute (decl_die, decl);
10513
10514   if (DECL_ARTIFICIAL (decl))
10515     add_AT_flag (decl_die, DW_AT_artificial, 1);
10516
10517   if (TREE_PROTECTED (decl))
10518     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
10519   else if (TREE_PRIVATE (decl))
10520     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
10521 }
10522
10523 #if 0
10524 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10525    Use modified_type_die instead.
10526    We keep this code here just in case these types of DIEs may be needed to
10527    represent certain things in other languages (e.g. Pascal) someday.  */
10528
10529 static void
10530 gen_pointer_type_die (type, context_die)
10531      tree type;
10532      dw_die_ref context_die;
10533 {
10534   dw_die_ref ptr_die
10535     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
10536
10537   equate_type_number_to_die (type, ptr_die);
10538   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10539   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10540 }
10541
10542 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10543    Use modified_type_die instead.
10544    We keep this code here just in case these types of DIEs may be needed to
10545    represent certain things in other languages (e.g. Pascal) someday.  */
10546
10547 static void
10548 gen_reference_type_die (type, context_die)
10549      tree type;
10550      dw_die_ref context_die;
10551 {
10552   dw_die_ref ref_die
10553     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
10554
10555   equate_type_number_to_die (type, ref_die);
10556   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
10557   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10558 }
10559 #endif
10560
10561 /* Generate a DIE for a pointer to a member type.  */
10562
10563 static void
10564 gen_ptr_to_mbr_type_die (type, context_die)
10565      tree type;
10566      dw_die_ref context_die;
10567 {
10568   dw_die_ref ptr_die
10569     = new_die (DW_TAG_ptr_to_member_type,
10570                scope_die_for (type, context_die), type);
10571
10572   equate_type_number_to_die (type, ptr_die);
10573   add_AT_die_ref (ptr_die, DW_AT_containing_type,
10574                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
10575   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10576 }
10577
10578 /* Generate the DIE for the compilation unit.  */
10579
10580 static dw_die_ref
10581 gen_compile_unit_die (filename)
10582      const char *filename;
10583 {
10584   dw_die_ref die;
10585   char producer[250];
10586   const char *wd = getpwd ();
10587   const char *language_string = lang_hooks.name;
10588   int language;
10589
10590   die = new_die (DW_TAG_compile_unit, NULL, NULL);
10591   add_name_attribute (die, filename);
10592
10593   if (wd != NULL && filename[0] != DIR_SEPARATOR)
10594     add_AT_string (die, DW_AT_comp_dir, wd);
10595
10596   sprintf (producer, "%s %s", language_string, version_string);
10597
10598 #ifdef MIPS_DEBUGGING_INFO
10599   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
10600      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
10601      not appear in the producer string, the debugger reaches the conclusion
10602      that the object file is stripped and has no debugging information.
10603      To get the MIPS/SGI debugger to believe that there is debugging
10604      information in the object file, we add a -g to the producer string.  */
10605   if (debug_info_level > DINFO_LEVEL_TERSE)
10606     strcat (producer, " -g");
10607 #endif
10608
10609   add_AT_string (die, DW_AT_producer, producer);
10610
10611   if (strcmp (language_string, "GNU C++") == 0)
10612     language = DW_LANG_C_plus_plus;
10613   else if (strcmp (language_string, "GNU Ada") == 0)
10614     language = DW_LANG_Ada83;
10615   else if (strcmp (language_string, "GNU F77") == 0)
10616     language = DW_LANG_Fortran77;
10617   else if (strcmp (language_string, "GNU Pascal") == 0)
10618     language = DW_LANG_Pascal83;
10619   else if (strcmp (language_string, "GNU Java") == 0)
10620     language = DW_LANG_Java;
10621   else if (flag_traditional)
10622     language = DW_LANG_C;
10623   else
10624     language = DW_LANG_C89;
10625
10626   add_AT_unsigned (die, DW_AT_language, language);
10627   return die;
10628 }
10629
10630 /* Generate a DIE for a string type.  */
10631
10632 static void
10633 gen_string_type_die (type, context_die)
10634      tree type;
10635      dw_die_ref context_die;
10636 {
10637   dw_die_ref type_die
10638     = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
10639
10640   equate_type_number_to_die (type, type_die);
10641
10642   /* ??? Fudge the string length attribute for now.
10643      TODO: add string length info.  */
10644 #if 0
10645   string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
10646   bound_representation (upper_bound, 0, 'u');
10647 #endif
10648 }
10649
10650 /* Generate the DIE for a base class.  */
10651
10652 static void
10653 gen_inheritance_die (binfo, context_die)
10654      tree binfo;
10655      dw_die_ref context_die;
10656 {
10657   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
10658
10659   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
10660   add_data_member_location_attribute (die, binfo);
10661
10662   if (TREE_VIA_VIRTUAL (binfo))
10663     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10664
10665   if (TREE_VIA_PUBLIC (binfo))
10666     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
10667   else if (TREE_VIA_PROTECTED (binfo))
10668     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
10669 }
10670
10671 /* Generate a DIE for a class member.  */
10672
10673 static void
10674 gen_member_die (type, context_die)
10675      tree type;
10676      dw_die_ref context_die;
10677 {
10678   tree member;
10679   dw_die_ref child;
10680
10681   /* If this is not an incomplete type, output descriptions of each of its
10682      members. Note that as we output the DIEs necessary to represent the
10683      members of this record or union type, we will also be trying to output
10684      DIEs to represent the *types* of those members. However the `type'
10685      function (above) will specifically avoid generating type DIEs for member
10686      types *within* the list of member DIEs for this (containing) type except
10687      for those types (of members) which are explicitly marked as also being
10688      members of this (containing) type themselves.  The g++ front- end can
10689      force any given type to be treated as a member of some other (containing)
10690      type by setting the TYPE_CONTEXT of the given (member) type to point to
10691      the TREE node representing the appropriate (containing) type.  */
10692
10693   /* First output info about the base classes.  */
10694   if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
10695     {
10696       tree bases = TYPE_BINFO_BASETYPES (type);
10697       int n_bases = TREE_VEC_LENGTH (bases);
10698       int i;
10699
10700       for (i = 0; i < n_bases; i++)
10701         gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
10702     }
10703
10704   /* Now output info about the data members and type members.  */
10705   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
10706     {
10707       /* If we thought we were generating minimal debug info for TYPE
10708          and then changed our minds, some of the member declarations
10709          may have already been defined.  Don't define them again, but
10710          do put them in the right order.  */
10711
10712       child = lookup_decl_die (member);
10713       if (child)
10714         splice_child_die (context_die, child);
10715       else
10716         gen_decl_die (member, context_die);
10717     }
10718
10719   /* Now output info about the function members (if any).  */
10720   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
10721     {
10722       /* Don't include clones in the member list.  */
10723       if (DECL_ABSTRACT_ORIGIN (member))
10724         continue;
10725
10726       child = lookup_decl_die (member);
10727       if (child)
10728         splice_child_die (context_die, child);
10729       else
10730         gen_decl_die (member, context_die);
10731     }
10732 }
10733
10734 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
10735    is set, we pretend that the type was never defined, so we only get the
10736    member DIEs needed by later specification DIEs.  */
10737
10738 static void
10739 gen_struct_or_union_type_die (type, context_die)
10740      tree type;
10741      dw_die_ref context_die;
10742 {
10743   dw_die_ref type_die = lookup_type_die (type);
10744   dw_die_ref scope_die = 0;
10745   int nested = 0;
10746   int complete = (TYPE_SIZE (type)
10747                   && (! TYPE_STUB_DECL (type)
10748                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
10749
10750   if (type_die && ! complete)
10751     return;
10752
10753   if (TYPE_CONTEXT (type) != NULL_TREE
10754       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type)))
10755     nested = 1;
10756
10757   scope_die = scope_die_for (type, context_die);
10758
10759   if (! type_die || (nested && scope_die == comp_unit_die))
10760     /* First occurrence of type or toplevel definition of nested class.  */
10761     {
10762       dw_die_ref old_die = type_die;
10763
10764       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
10765                           ? DW_TAG_structure_type : DW_TAG_union_type,
10766                           scope_die, type);
10767       equate_type_number_to_die (type, type_die);
10768       if (old_die)
10769         add_AT_die_ref (type_die, DW_AT_specification, old_die);
10770       else
10771         add_name_attribute (type_die, type_tag (type));
10772     }
10773   else
10774     remove_AT (type_die, DW_AT_declaration);
10775
10776   /* If this type has been completed, then give it a byte_size attribute and
10777      then give a list of members.  */
10778   if (complete)
10779     {
10780       /* Prevent infinite recursion in cases where the type of some member of
10781          this type is expressed in terms of this type itself.  */
10782       TREE_ASM_WRITTEN (type) = 1;
10783       add_byte_size_attribute (type_die, type);
10784       if (TYPE_STUB_DECL (type) != NULL_TREE)
10785         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10786
10787       /* If the first reference to this type was as the return type of an
10788          inline function, then it may not have a parent.  Fix this now.  */
10789       if (type_die->die_parent == NULL)
10790         add_child_die (scope_die, type_die);
10791
10792       push_decl_scope (type);
10793       gen_member_die (type, type_die);
10794       pop_decl_scope ();
10795
10796       /* GNU extension: Record what type our vtable lives in.  */
10797       if (TYPE_VFIELD (type))
10798         {
10799           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
10800
10801           gen_type_die (vtype, context_die);
10802           add_AT_die_ref (type_die, DW_AT_containing_type,
10803                           lookup_type_die (vtype));
10804         }
10805     }
10806   else
10807     {
10808       add_AT_flag (type_die, DW_AT_declaration, 1);
10809
10810       /* We don't need to do this for function-local types.  */
10811       if (! decl_function_context (TYPE_STUB_DECL (type)))
10812         VARRAY_PUSH_TREE (incomplete_types, type);
10813     }
10814 }
10815
10816 /* Generate a DIE for a subroutine _type_.  */
10817
10818 static void
10819 gen_subroutine_type_die (type, context_die)
10820      tree type;
10821      dw_die_ref context_die;
10822 {
10823   tree return_type = TREE_TYPE (type);
10824   dw_die_ref subr_die
10825     = new_die (DW_TAG_subroutine_type,
10826                scope_die_for (type, context_die), type);
10827
10828   equate_type_number_to_die (type, subr_die);
10829   add_prototyped_attribute (subr_die, type);
10830   add_type_attribute (subr_die, return_type, 0, 0, context_die);
10831   gen_formal_types_die (type, subr_die);
10832 }
10833
10834 /* Generate a DIE for a type definition */
10835
10836 static void
10837 gen_typedef_die (decl, context_die)
10838      tree decl;
10839      dw_die_ref context_die;
10840 {
10841   dw_die_ref type_die;
10842   tree origin;
10843
10844   if (TREE_ASM_WRITTEN (decl))
10845     return;
10846
10847   TREE_ASM_WRITTEN (decl) = 1;
10848   type_die = new_die (DW_TAG_typedef, context_die, decl);
10849   origin = decl_ultimate_origin (decl);
10850   if (origin != NULL)
10851     add_abstract_origin_attribute (type_die, origin);
10852   else
10853     {
10854       tree type;
10855
10856       add_name_and_src_coords_attributes (type_die, decl);
10857       if (DECL_ORIGINAL_TYPE (decl))
10858         {
10859           type = DECL_ORIGINAL_TYPE (decl);
10860
10861           if (type == TREE_TYPE (decl))
10862             abort ();
10863           else
10864             equate_type_number_to_die (TREE_TYPE (decl), type_die);
10865         }
10866       else
10867         type = TREE_TYPE (decl);
10868
10869       add_type_attribute (type_die, type, TREE_READONLY (decl),
10870                           TREE_THIS_VOLATILE (decl), context_die);
10871     }
10872
10873   if (DECL_ABSTRACT (decl))
10874     equate_decl_number_to_die (decl, type_die);
10875 }
10876
10877 /* Generate a type description DIE.  */
10878
10879 static void
10880 gen_type_die (type, context_die)
10881      tree type;
10882      dw_die_ref context_die;
10883 {
10884   int need_pop;
10885
10886   if (type == NULL_TREE || type == error_mark_node)
10887     return;
10888
10889   /* We are going to output a DIE to represent the unqualified version of
10890      this type (i.e. without any const or volatile qualifiers) so get the
10891      main variant (i.e. the unqualified version) of this type now.  */
10892   type = type_main_variant (type);
10893
10894   if (TREE_ASM_WRITTEN (type))
10895     return;
10896
10897   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10898       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
10899     {
10900       TREE_ASM_WRITTEN (type) = 1;
10901       gen_decl_die (TYPE_NAME (type), context_die);
10902       return;
10903     }
10904
10905   switch (TREE_CODE (type))
10906     {
10907     case ERROR_MARK:
10908       break;
10909
10910     case POINTER_TYPE:
10911     case REFERENCE_TYPE:
10912       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
10913          ensures that the gen_type_die recursion will terminate even if the
10914          type is recursive.  Recursive types are possible in Ada.  */
10915       /* ??? We could perhaps do this for all types before the switch
10916          statement.  */
10917       TREE_ASM_WRITTEN (type) = 1;
10918
10919       /* For these types, all that is required is that we output a DIE (or a
10920          set of DIEs) to represent the "basis" type.  */
10921       gen_type_die (TREE_TYPE (type), context_die);
10922       break;
10923
10924     case OFFSET_TYPE:
10925       /* This code is used for C++ pointer-to-data-member types.
10926          Output a description of the relevant class type.  */
10927       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
10928
10929       /* Output a description of the type of the object pointed to.  */
10930       gen_type_die (TREE_TYPE (type), context_die);
10931
10932       /* Now output a DIE to represent this pointer-to-data-member type
10933          itself.  */
10934       gen_ptr_to_mbr_type_die (type, context_die);
10935       break;
10936
10937     case SET_TYPE:
10938       gen_type_die (TYPE_DOMAIN (type), context_die);
10939       gen_set_type_die (type, context_die);
10940       break;
10941
10942     case FILE_TYPE:
10943       gen_type_die (TREE_TYPE (type), context_die);
10944       abort ();                 /* No way to represent these in Dwarf yet!  */
10945       break;
10946
10947     case FUNCTION_TYPE:
10948       /* Force out return type (in case it wasn't forced out already).  */
10949       gen_type_die (TREE_TYPE (type), context_die);
10950       gen_subroutine_type_die (type, context_die);
10951       break;
10952
10953     case METHOD_TYPE:
10954       /* Force out return type (in case it wasn't forced out already).  */
10955       gen_type_die (TREE_TYPE (type), context_die);
10956       gen_subroutine_type_die (type, context_die);
10957       break;
10958
10959     case ARRAY_TYPE:
10960       if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
10961         {
10962           gen_type_die (TREE_TYPE (type), context_die);
10963           gen_string_type_die (type, context_die);
10964         }
10965       else
10966         gen_array_type_die (type, context_die);
10967       break;
10968
10969     case VECTOR_TYPE:
10970       gen_type_die (TYPE_DEBUG_REPRESENTATION_TYPE (type), context_die);
10971       break;
10972
10973     case ENUMERAL_TYPE:
10974     case RECORD_TYPE:
10975     case UNION_TYPE:
10976     case QUAL_UNION_TYPE:
10977       /* If this is a nested type whose containing class hasn't been written
10978          out yet, writing it out will cover this one, too.  This does not apply
10979          to instantiations of member class templates; they need to be added to
10980          the containing class as they are generated.  FIXME: This hurts the
10981          idea of combining type decls from multiple TUs, since we can't predict
10982          what set of template instantiations we'll get.  */
10983       if (TYPE_CONTEXT (type)
10984           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
10985           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
10986         {
10987           gen_type_die (TYPE_CONTEXT (type), context_die);
10988
10989           if (TREE_ASM_WRITTEN (type))
10990             return;
10991
10992           /* If that failed, attach ourselves to the stub.  */
10993           push_decl_scope (TYPE_CONTEXT (type));
10994           context_die = lookup_type_die (TYPE_CONTEXT (type));
10995           need_pop = 1;
10996         }
10997       else
10998         need_pop = 0;
10999
11000       if (TREE_CODE (type) == ENUMERAL_TYPE)
11001         gen_enumeration_type_die (type, context_die);
11002       else
11003         gen_struct_or_union_type_die (type, context_die);
11004
11005       if (need_pop)
11006         pop_decl_scope ();
11007
11008       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
11009          it up if it is ever completed.  gen_*_type_die will set it for us
11010          when appropriate.  */
11011       return;
11012
11013     case VOID_TYPE:
11014     case INTEGER_TYPE:
11015     case REAL_TYPE:
11016     case COMPLEX_TYPE:
11017     case BOOLEAN_TYPE:
11018     case CHAR_TYPE:
11019       /* No DIEs needed for fundamental types.  */
11020       break;
11021
11022     case LANG_TYPE:
11023       /* No Dwarf representation currently defined.  */
11024       break;
11025
11026     default:
11027       abort ();
11028     }
11029
11030   TREE_ASM_WRITTEN (type) = 1;
11031 }
11032
11033 /* Generate a DIE for a tagged type instantiation.  */
11034
11035 static void
11036 gen_tagged_type_instantiation_die (type, context_die)
11037      tree type;
11038      dw_die_ref context_die;
11039 {
11040   if (type == NULL_TREE || type == error_mark_node)
11041     return;
11042
11043   /* We are going to output a DIE to represent the unqualified version of
11044      this type (i.e. without any const or volatile qualifiers) so make sure
11045      that we have the main variant (i.e. the unqualified version) of this
11046      type now.  */
11047   if (type != type_main_variant (type))
11048     abort ();
11049
11050   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
11051      an instance of an unresolved type.  */
11052
11053   switch (TREE_CODE (type))
11054     {
11055     case ERROR_MARK:
11056       break;
11057
11058     case ENUMERAL_TYPE:
11059       gen_inlined_enumeration_type_die (type, context_die);
11060       break;
11061
11062     case RECORD_TYPE:
11063       gen_inlined_structure_type_die (type, context_die);
11064       break;
11065
11066     case UNION_TYPE:
11067     case QUAL_UNION_TYPE:
11068       gen_inlined_union_type_die (type, context_die);
11069       break;
11070
11071     default:
11072       abort ();
11073     }
11074 }
11075
11076 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
11077    things which are local to the given block.  */
11078
11079 static void
11080 gen_block_die (stmt, context_die, depth)
11081      tree stmt;
11082      dw_die_ref context_die;
11083      int depth;
11084 {
11085   int must_output_die = 0;
11086   tree origin;
11087   tree decl;
11088   enum tree_code origin_code;
11089
11090   /* Ignore blocks never really used to make RTL.  */
11091   if (stmt == NULL_TREE || !TREE_USED (stmt)
11092       || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
11093     return;
11094
11095   /* If the block is one fragment of a non-contiguous block, do not
11096      process the variables, since they will have been done by the
11097      origin block.  Do process subblocks.  */
11098   if (BLOCK_FRAGMENT_ORIGIN (stmt))
11099     {
11100       tree sub;
11101
11102       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
11103         gen_block_die (sub, context_die, depth + 1);
11104
11105       return;
11106     }
11107
11108   /* Determine the "ultimate origin" of this block.  This block may be an
11109      inlined instance of an inlined instance of inline function, so we have
11110      to trace all of the way back through the origin chain to find out what
11111      sort of node actually served as the original seed for the creation of
11112      the current block.  */
11113   origin = block_ultimate_origin (stmt);
11114   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
11115
11116   /* Determine if we need to output any Dwarf DIEs at all to represent this
11117      block.  */
11118   if (origin_code == FUNCTION_DECL)
11119     /* The outer scopes for inlinings *must* always be represented.  We
11120        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
11121     must_output_die = 1;
11122   else
11123     {
11124       /* In the case where the current block represents an inlining of the
11125          "body block" of an inline function, we must *NOT* output any DIE for
11126          this block because we have already output a DIE to represent the whole
11127          inlined function scope and the "body block" of any function doesn't
11128          really represent a different scope according to ANSI C rules.  So we
11129          check here to make sure that this block does not represent a "body
11130          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
11131       if (! is_body_block (origin ? origin : stmt))
11132         {
11133           /* Determine if this block directly contains any "significant"
11134              local declarations which we will need to output DIEs for.  */
11135           if (debug_info_level > DINFO_LEVEL_TERSE)
11136             /* We are not in terse mode so *any* local declaration counts
11137                as being a "significant" one.  */
11138             must_output_die = (BLOCK_VARS (stmt) != NULL);
11139           else
11140             /* We are in terse mode, so only local (nested) function
11141                definitions count as "significant" local declarations.  */
11142             for (decl = BLOCK_VARS (stmt);
11143                  decl != NULL; decl = TREE_CHAIN (decl))
11144               if (TREE_CODE (decl) == FUNCTION_DECL
11145                   && DECL_INITIAL (decl))
11146                 {
11147                   must_output_die = 1;
11148                   break;
11149                 }
11150         }
11151     }
11152
11153   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
11154      DIE for any block which contains no significant local declarations at
11155      all.  Rather, in such cases we just call `decls_for_scope' so that any
11156      needed Dwarf info for any sub-blocks will get properly generated. Note
11157      that in terse mode, our definition of what constitutes a "significant"
11158      local declaration gets restricted to include only inlined function
11159      instances and local (nested) function definitions.  */
11160   if (must_output_die)
11161     {
11162       if (origin_code == FUNCTION_DECL)
11163         gen_inlined_subroutine_die (stmt, context_die, depth);
11164       else
11165         gen_lexical_block_die (stmt, context_die, depth);
11166     }
11167   else
11168     decls_for_scope (stmt, context_die, depth);
11169 }
11170
11171 /* Generate all of the decls declared within a given scope and (recursively)
11172    all of its sub-blocks.  */
11173
11174 static void
11175 decls_for_scope (stmt, context_die, depth)
11176      tree stmt;
11177      dw_die_ref context_die;
11178      int depth;
11179 {
11180   tree decl;
11181   tree subblocks;
11182
11183   /* Ignore blocks never really used to make RTL.  */
11184   if (stmt == NULL_TREE || ! TREE_USED (stmt))
11185     return;
11186
11187   /* Output the DIEs to represent all of the data objects and typedefs
11188      declared directly within this block but not within any nested
11189      sub-blocks.  Also, nested function and tag DIEs have been
11190      generated with a parent of NULL; fix that up now.  */
11191   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
11192     {
11193       dw_die_ref die;
11194
11195       if (TREE_CODE (decl) == FUNCTION_DECL)
11196         die = lookup_decl_die (decl);
11197       else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
11198         die = lookup_type_die (TREE_TYPE (decl));
11199       else
11200         die = NULL;
11201
11202       if (die != NULL && die->die_parent == NULL)
11203         add_child_die (context_die, die);
11204       else
11205         gen_decl_die (decl, context_die);
11206     }
11207
11208   /* Output the DIEs to represent all sub-blocks (and the items declared
11209      therein) of this block.  */
11210   for (subblocks = BLOCK_SUBBLOCKS (stmt);
11211        subblocks != NULL;
11212        subblocks = BLOCK_CHAIN (subblocks))
11213     gen_block_die (subblocks, context_die, depth + 1);
11214 }
11215
11216 /* Is this a typedef we can avoid emitting?  */
11217
11218 static inline int
11219 is_redundant_typedef (decl)
11220      tree decl;
11221 {
11222   if (TYPE_DECL_IS_STUB (decl))
11223     return 1;
11224
11225   if (DECL_ARTIFICIAL (decl)
11226       && DECL_CONTEXT (decl)
11227       && is_tagged_type (DECL_CONTEXT (decl))
11228       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
11229       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
11230     /* Also ignore the artificial member typedef for the class name.  */
11231     return 1;
11232
11233   return 0;
11234 }
11235
11236 /* Generate Dwarf debug information for a decl described by DECL.  */
11237
11238 static void
11239 gen_decl_die (decl, context_die)
11240      tree decl;
11241      dw_die_ref context_die;
11242 {
11243   tree origin;
11244
11245   if (DECL_P (decl) && DECL_IGNORED_P (decl))
11246     return;
11247
11248   switch (TREE_CODE (decl))
11249     {
11250     case ERROR_MARK:
11251       break;
11252
11253     case CONST_DECL:
11254       /* The individual enumerators of an enum type get output when we output
11255          the Dwarf representation of the relevant enum type itself.  */
11256       break;
11257
11258     case FUNCTION_DECL:
11259       /* Don't output any DIEs to represent mere function declarations,
11260          unless they are class members or explicit block externs.  */
11261       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
11262           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
11263         break;
11264
11265       /* If we're emitting a clone, emit info for the abstract instance.  */
11266       if (DECL_ORIGIN (decl) != decl)
11267         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
11268
11269       /* If we're emitting an out-of-line copy of an inline function,
11270          emit info for the abstract instance and set up to refer to it.  */
11271       else if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
11272                && ! class_scope_p (context_die)
11273                /* dwarf2out_abstract_function won't emit a die if this is just
11274                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
11275                   that case, because that works only if we have a die.  */
11276                && DECL_INITIAL (decl) != NULL_TREE)
11277         {
11278           dwarf2out_abstract_function (decl);
11279           set_decl_origin_self (decl);
11280         }
11281
11282       /* Otherwise we're emitting the primary DIE for this decl.  */
11283       else if (debug_info_level > DINFO_LEVEL_TERSE)
11284         {
11285           /* Before we describe the FUNCTION_DECL itself, make sure that we
11286              have described its return type.  */
11287           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
11288
11289           /* And its virtual context.  */
11290           if (DECL_VINDEX (decl) != NULL_TREE)
11291             gen_type_die (DECL_CONTEXT (decl), context_die);
11292
11293           /* And its containing type.  */
11294           origin = decl_class_context (decl);
11295           if (origin != NULL_TREE)
11296             gen_type_die_for_member (origin, decl, context_die);
11297         }
11298
11299       /* Now output a DIE to represent the function itself.  */
11300       gen_subprogram_die (decl, context_die);
11301       break;
11302
11303     case TYPE_DECL:
11304       /* If we are in terse mode, don't generate any DIEs to represent any
11305          actual typedefs.  */
11306       if (debug_info_level <= DINFO_LEVEL_TERSE)
11307         break;
11308
11309       /* In the special case of a TYPE_DECL node representing the declaration
11310          of some type tag, if the given TYPE_DECL is marked as having been
11311          instantiated from some other (original) TYPE_DECL node (e.g. one which
11312          was generated within the original definition of an inline function) we
11313          have to generate a special (abbreviated) DW_TAG_structure_type,
11314          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
11315       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
11316         {
11317           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
11318           break;
11319         }
11320
11321       if (is_redundant_typedef (decl))
11322         gen_type_die (TREE_TYPE (decl), context_die);
11323       else
11324         /* Output a DIE to represent the typedef itself.  */
11325         gen_typedef_die (decl, context_die);
11326       break;
11327
11328     case LABEL_DECL:
11329       if (debug_info_level >= DINFO_LEVEL_NORMAL)
11330         gen_label_die (decl, context_die);
11331       break;
11332
11333     case VAR_DECL:
11334       /* If we are in terse mode, don't generate any DIEs to represent any
11335          variable declarations or definitions.  */
11336       if (debug_info_level <= DINFO_LEVEL_TERSE)
11337         break;
11338
11339       /* Output any DIEs that are needed to specify the type of this data
11340          object.  */
11341       gen_type_die (TREE_TYPE (decl), context_die);
11342
11343       /* And its containing type.  */
11344       origin = decl_class_context (decl);
11345       if (origin != NULL_TREE)
11346         gen_type_die_for_member (origin, decl, context_die);
11347
11348       /* Now output the DIE to represent the data object itself.  This gets
11349          complicated because of the possibility that the VAR_DECL really
11350          represents an inlined instance of a formal parameter for an inline
11351          function.  */
11352       origin = decl_ultimate_origin (decl);
11353       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
11354         gen_formal_parameter_die (decl, context_die);
11355       else
11356         gen_variable_die (decl, context_die);
11357       break;
11358
11359     case FIELD_DECL:
11360       /* Ignore the nameless fields that are used to skip bits but handle C++
11361          anonymous unions.  */
11362       if (DECL_NAME (decl) != NULL_TREE
11363           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
11364         {
11365           gen_type_die (member_declared_type (decl), context_die);
11366           gen_field_die (decl, context_die);
11367         }
11368       break;
11369
11370     case PARM_DECL:
11371       gen_type_die (TREE_TYPE (decl), context_die);
11372       gen_formal_parameter_die (decl, context_die);
11373       break;
11374
11375     case NAMESPACE_DECL:
11376       /* Ignore for now.  */
11377       break;
11378
11379     default:
11380       abort ();
11381     }
11382 }
11383
11384 static void
11385 mark_limbo_die_list (ptr)
11386      void *ptr ATTRIBUTE_UNUSED;
11387 {
11388   limbo_die_node *node;
11389   for (node = limbo_die_list; node ; node = node->next)
11390     ggc_mark_tree (node->created_for);
11391 }
11392 \f
11393 /* Add Ada "use" clause information for SGI Workshop debugger.  */
11394
11395 void
11396 dwarf2out_add_library_unit_info (filename, context_list)
11397      const char *filename;
11398      const char *context_list;
11399 {
11400   unsigned int file_index;
11401
11402   if (filename != NULL)
11403     {
11404       dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
11405       tree context_list_decl
11406         = build_decl (LABEL_DECL, get_identifier (context_list),
11407                       void_type_node);
11408
11409       TREE_PUBLIC (context_list_decl) = TRUE;
11410       add_name_attribute (unit_die, context_list);
11411       file_index = lookup_filename (filename);
11412       add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
11413       add_pubname (context_list_decl, unit_die);
11414     }
11415 }
11416
11417 /* Output debug information for global decl DECL.  Called from toplev.c after
11418    compilation proper has finished.  */
11419
11420 static void
11421 dwarf2out_global_decl (decl)
11422      tree decl;
11423 {
11424   /* Output DWARF2 information for file-scope tentative data object
11425      declarations, file-scope (extern) function declarations (which had no
11426      corresponding body) and file-scope tagged type declarations and
11427      definitions which have not yet been forced out.  */
11428   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
11429     dwarf2out_decl (decl);
11430 }
11431
11432 /* Write the debugging output for DECL.  */
11433
11434 void
11435 dwarf2out_decl (decl)
11436      tree decl;
11437 {
11438   dw_die_ref context_die = comp_unit_die;
11439
11440   switch (TREE_CODE (decl))
11441     {
11442     case ERROR_MARK:
11443       return;
11444
11445     case FUNCTION_DECL:
11446       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a
11447          builtin function.  Explicit programmer-supplied declarations of
11448          these same functions should NOT be ignored however.  */
11449       if (DECL_EXTERNAL (decl) && DECL_BUILT_IN (decl))
11450         return;
11451
11452       /* What we would really like to do here is to filter out all mere
11453          file-scope declarations of file-scope functions which are never
11454          referenced later within this translation unit (and keep all of ones
11455          that *are* referenced later on) but we aren't clairvoyant, so we have
11456          no idea which functions will be referenced in the future (i.e. later
11457          on within the current translation unit). So here we just ignore all
11458          file-scope function declarations which are not also definitions.  If
11459          and when the debugger needs to know something about these functions,
11460          it will have to hunt around and find the DWARF information associated
11461          with the definition of the function.
11462
11463          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
11464          nodes represent definitions and which ones represent mere
11465          declarations.  We have to check DECL_INITIAL instead. That's because
11466          the C front-end supports some weird semantics for "extern inline"
11467          function definitions.  These can get inlined within the current
11468          translation unit (an thus, we need to generate Dwarf info for their
11469          abstract instances so that the Dwarf info for the concrete inlined
11470          instances can have something to refer to) but the compiler never
11471          generates any out-of-lines instances of such things (despite the fact
11472          that they *are* definitions).
11473
11474          The important point is that the C front-end marks these "extern
11475          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
11476          them anyway. Note that the C++ front-end also plays some similar games
11477          for inline function definitions appearing within include files which
11478          also contain `#pragma interface' pragmas.  */
11479       if (DECL_INITIAL (decl) == NULL_TREE)
11480         return;
11481
11482       /* If we're a nested function, initially use a parent of NULL; if we're
11483          a plain function, this will be fixed up in decls_for_scope.  If
11484          we're a method, it will be ignored, since we already have a DIE.  */
11485       if (decl_function_context (decl))
11486         context_die = NULL;
11487       break;
11488
11489     case VAR_DECL:
11490       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
11491          declaration and if the declaration was never even referenced from
11492          within this entire compilation unit.  We suppress these DIEs in
11493          order to save space in the .debug section (by eliminating entries
11494          which are probably useless).  Note that we must not suppress
11495          block-local extern declarations (whether used or not) because that
11496          would screw-up the debugger's name lookup mechanism and cause it to
11497          miss things which really ought to be in scope at a given point.  */
11498       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
11499         return;
11500
11501       /* If we are in terse mode, don't generate any DIEs to represent any
11502          variable declarations or definitions.  */
11503       if (debug_info_level <= DINFO_LEVEL_TERSE)
11504         return;
11505       break;
11506
11507     case TYPE_DECL:
11508       /* Don't emit stubs for types unless they are needed by other DIEs.  */
11509       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
11510         return;
11511
11512       /* Don't bother trying to generate any DIEs to represent any of the
11513          normal built-in types for the language we are compiling.  */
11514       if (DECL_SOURCE_LINE (decl) == 0)
11515         {
11516           /* OK, we need to generate one for `bool' so GDB knows what type
11517              comparisons have.  */
11518           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
11519                == DW_LANG_C_plus_plus)
11520               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
11521               && ! DECL_IGNORED_P (decl))
11522             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
11523
11524           return;
11525         }
11526
11527       /* If we are in terse mode, don't generate any DIEs for types.  */
11528       if (debug_info_level <= DINFO_LEVEL_TERSE)
11529         return;
11530
11531       /* If we're a function-scope tag, initially use a parent of NULL;
11532          this will be fixed up in decls_for_scope.  */
11533       if (decl_function_context (decl))
11534         context_die = NULL;
11535
11536       break;
11537
11538     default:
11539       return;
11540     }
11541
11542   gen_decl_die (decl, context_die);
11543 }
11544
11545 /* Output a marker (i.e. a label) for the beginning of the generated code for
11546    a lexical block.  */
11547
11548 static void
11549 dwarf2out_begin_block (line, blocknum)
11550      unsigned int line ATTRIBUTE_UNUSED;
11551      unsigned int blocknum;
11552 {
11553   function_section (current_function_decl);
11554   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
11555 }
11556
11557 /* Output a marker (i.e. a label) for the end of the generated code for a
11558    lexical block.  */
11559
11560 static void
11561 dwarf2out_end_block (line, blocknum)
11562      unsigned int line ATTRIBUTE_UNUSED;
11563      unsigned int blocknum;
11564 {
11565   function_section (current_function_decl);
11566   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
11567 }
11568
11569 /* Returns nonzero if it is appropriate not to emit any debugging
11570    information for BLOCK, because it doesn't contain any instructions.
11571
11572    Don't allow this for blocks with nested functions or local classes
11573    as we would end up with orphans, and in the presence of scheduling
11574    we may end up calling them anyway.  */
11575
11576 static bool
11577 dwarf2out_ignore_block (block)
11578      tree block;
11579 {
11580   tree decl;
11581
11582   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
11583     if (TREE_CODE (decl) == FUNCTION_DECL
11584         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
11585       return 0;
11586
11587   return 1;
11588 }
11589
11590 /* Lookup FILE_NAME (in the list of filenames that we know about here in
11591    dwarf2out.c) and return its "index".  The index of each (known) filename is
11592    just a unique number which is associated with only that one filename.  We
11593    need such numbers for the sake of generating labels (in the .debug_sfnames
11594    section) and references to those files numbers (in the .debug_srcinfo
11595    and.debug_macinfo sections).  If the filename given as an argument is not
11596    found in our current list, add it to the list and assign it the next
11597    available unique index number.  In order to speed up searches, we remember
11598    the index of the filename was looked up last.  This handles the majority of
11599    all searches.  */
11600
11601 static unsigned
11602 lookup_filename (file_name)
11603      const char *file_name;
11604 {
11605   unsigned i;
11606
11607   /* ??? Why isn't DECL_SOURCE_FILE left null instead.  */
11608   if (strcmp (file_name, "<internal>") == 0
11609       || strcmp (file_name, "<built-in>") == 0)
11610     return 0;
11611
11612   /* Check to see if the file name that was searched on the previous
11613      call matches this file name.  If so, return the index.  */
11614   if (file_table.last_lookup_index != 0)
11615     if (0 == strcmp (file_name,
11616                      file_table.table[file_table.last_lookup_index]))
11617       return file_table.last_lookup_index;
11618
11619   /* Didn't match the previous lookup, search the table */
11620   for (i = 1; i < file_table.in_use; i++)
11621     if (strcmp (file_name, file_table.table[i]) == 0)
11622       {
11623         file_table.last_lookup_index = i;
11624         return i;
11625       }
11626
11627   /* Prepare to add a new table entry by making sure there is enough space in
11628      the table to do so.  If not, expand the current table.  */
11629   if (i == file_table.allocated)
11630     {
11631       file_table.allocated = i + FILE_TABLE_INCREMENT;
11632       file_table.table = (char **)
11633         xrealloc (file_table.table, file_table.allocated * sizeof (char *));
11634     }
11635
11636   /* Add the new entry to the end of the filename table.  */
11637   file_table.table[i] = xstrdup (file_name);
11638   file_table.in_use = i + 1;
11639   file_table.last_lookup_index = i;
11640
11641   if (DWARF2_ASM_LINE_DEBUG_INFO)
11642     fprintf (asm_out_file, "\t.file %u \"%s\"\n", i, file_name);
11643
11644   return i;
11645 }
11646
11647 static void
11648 init_file_table ()
11649 {
11650   /* Allocate the initial hunk of the file_table.  */
11651   file_table.table = (char **) xcalloc (FILE_TABLE_INCREMENT, sizeof (char *));
11652   file_table.allocated = FILE_TABLE_INCREMENT;
11653
11654   /* Skip the first entry - file numbers begin at 1.  */
11655   file_table.in_use = 1;
11656   file_table.last_lookup_index = 0;
11657 }
11658
11659 /* Output a label to mark the beginning of a source code line entry
11660    and record information relating to this source line, in
11661    'line_info_table' for later output of the .debug_line section.  */
11662
11663 static void
11664 dwarf2out_source_line (line, filename)
11665      unsigned int line;
11666      const char *filename;
11667 {
11668   if (debug_info_level >= DINFO_LEVEL_NORMAL)
11669     {
11670       function_section (current_function_decl);
11671
11672       /* If requested, emit something human-readable.  */
11673       if (flag_debug_asm)
11674         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
11675                  filename, line);
11676
11677       if (DWARF2_ASM_LINE_DEBUG_INFO)
11678         {
11679           unsigned file_num = lookup_filename (filename);
11680
11681           /* Emit the .loc directive understood by GNU as.  */
11682           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
11683
11684           /* Indicate that line number info exists.  */
11685           line_info_table_in_use++;
11686
11687           /* Indicate that multiple line number tables exist.  */
11688           if (DECL_SECTION_NAME (current_function_decl))
11689             separate_line_info_table_in_use++;
11690         }
11691       else if (DECL_SECTION_NAME (current_function_decl))
11692         {
11693           dw_separate_line_info_ref line_info;
11694           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, SEPARATE_LINE_CODE_LABEL,
11695                                      separate_line_info_table_in_use);
11696
11697           /* expand the line info table if necessary */
11698           if (separate_line_info_table_in_use
11699               == separate_line_info_table_allocated)
11700             {
11701               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11702               separate_line_info_table
11703                 = (dw_separate_line_info_ref)
11704                   xrealloc (separate_line_info_table,
11705                             separate_line_info_table_allocated
11706                             * sizeof (dw_separate_line_info_entry));
11707             }
11708
11709           /* Add the new entry at the end of the line_info_table.  */
11710           line_info
11711             = &separate_line_info_table[separate_line_info_table_in_use++];
11712           line_info->dw_file_num = lookup_filename (filename);
11713           line_info->dw_line_num = line;
11714           line_info->function = current_funcdef_number;
11715         }
11716       else
11717         {
11718           dw_line_info_ref line_info;
11719
11720           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, LINE_CODE_LABEL,
11721                                      line_info_table_in_use);
11722
11723           /* Expand the line info table if necessary.  */
11724           if (line_info_table_in_use == line_info_table_allocated)
11725             {
11726               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11727               line_info_table
11728                 = (dw_line_info_ref)
11729                   xrealloc (line_info_table,
11730                             (line_info_table_allocated
11731                              * sizeof (dw_line_info_entry)));
11732             }
11733
11734           /* Add the new entry at the end of the line_info_table.  */
11735           line_info = &line_info_table[line_info_table_in_use++];
11736           line_info->dw_file_num = lookup_filename (filename);
11737           line_info->dw_line_num = line;
11738         }
11739     }
11740 }
11741
11742 /* Record the beginning of a new source file.  */
11743
11744 static void
11745 dwarf2out_start_source_file (lineno, filename)
11746      unsigned int lineno;
11747      const char *filename;
11748 {
11749   if (flag_eliminate_dwarf2_dups)
11750     {
11751       /* Record the beginning of the file for break_out_includes.  */
11752       dw_die_ref bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
11753       add_AT_string (bincl_die, DW_AT_name, filename);
11754     }
11755
11756   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11757     {
11758       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11759       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
11760       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
11761                                    lineno);
11762       dw2_asm_output_data_uleb128 (lookup_filename (filename),
11763                                    "Filename we just started");
11764     }
11765 }
11766
11767 /* Record the end of a source file.  */
11768
11769 static void
11770 dwarf2out_end_source_file (lineno)
11771      unsigned int lineno ATTRIBUTE_UNUSED;
11772 {
11773   if (flag_eliminate_dwarf2_dups)
11774     /* Record the end of the file for break_out_includes.  */
11775     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
11776
11777   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11778     {
11779       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11780       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
11781     }
11782 }
11783
11784 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
11785    the tail part of the directive line, i.e. the part which is past the
11786    initial whitespace, #, whitespace, directive-name, whitespace part.  */
11787
11788 static void
11789 dwarf2out_define (lineno, buffer)
11790      unsigned lineno ATTRIBUTE_UNUSED;
11791      const char *buffer ATTRIBUTE_UNUSED;
11792 {
11793   static int initialized = 0;
11794   if (!initialized)
11795     {
11796       dwarf2out_start_source_file (0, primary_filename);
11797       initialized = 1;
11798     }
11799
11800   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11801     {
11802       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11803       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
11804       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
11805       dw2_asm_output_nstring (buffer, -1, "The macro");
11806     }
11807 }
11808
11809 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
11810    the tail part of the directive line, i.e. the part which is past the
11811    initial whitespace, #, whitespace, directive-name, whitespace part.  */
11812
11813 static void
11814 dwarf2out_undef (lineno, buffer)
11815      unsigned lineno ATTRIBUTE_UNUSED;
11816      const char *buffer ATTRIBUTE_UNUSED;
11817 {
11818   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11819     {
11820       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11821       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
11822       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
11823       dw2_asm_output_nstring (buffer, -1, "The macro");
11824     }
11825 }
11826
11827 /* Set up for Dwarf output at the start of compilation.  */
11828
11829 static void
11830 dwarf2out_init (main_input_filename)
11831      const char *main_input_filename;
11832 {
11833   init_file_table ();
11834
11835   /* Remember the name of the primary input file.  */
11836   primary_filename = main_input_filename;
11837
11838   /* Add it to the file table first, under the assumption that we'll
11839      be emitting line number data for it first, which avoids having
11840      to add an initial DW_LNS_set_file.  */
11841   lookup_filename (main_input_filename);
11842
11843   /* Allocate the initial hunk of the decl_die_table.  */
11844   decl_die_table
11845     = (dw_die_ref *) xcalloc (DECL_DIE_TABLE_INCREMENT, sizeof (dw_die_ref));
11846   decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
11847   decl_die_table_in_use = 0;
11848
11849   /* Allocate the initial hunk of the decl_scope_table.  */
11850   VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
11851   ggc_add_tree_varray_root (&decl_scope_table, 1);
11852
11853   /* Allocate the initial hunk of the abbrev_die_table.  */
11854   abbrev_die_table
11855     = (dw_die_ref *) xcalloc (ABBREV_DIE_TABLE_INCREMENT,
11856                               sizeof (dw_die_ref));
11857   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
11858   /* Zero-th entry is allocated, but unused */
11859   abbrev_die_table_in_use = 1;
11860
11861   /* Allocate the initial hunk of the line_info_table.  */
11862   line_info_table
11863     = (dw_line_info_ref) xcalloc (LINE_INFO_TABLE_INCREMENT,
11864                                   sizeof (dw_line_info_entry));
11865   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
11866
11867   /* Zero-th entry is allocated, but unused */
11868   line_info_table_in_use = 1;
11869
11870   /* Generate the initial DIE for the .debug section.  Note that the (string)
11871      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
11872      will (typically) be a relative pathname and that this pathname should be
11873      taken as being relative to the directory from which the compiler was
11874      invoked when the given (base) source file was compiled.  */
11875   comp_unit_die = gen_compile_unit_die (main_input_filename);
11876
11877   VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
11878   ggc_add_tree_varray_root (&incomplete_types, 1);
11879
11880   VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
11881   ggc_add_rtx_varray_root (&used_rtx_varray, 1);
11882
11883   ggc_add_root (&limbo_die_list, 1, 1, mark_limbo_die_list);
11884
11885   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
11886   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
11887                                DEBUG_ABBREV_SECTION_LABEL, 0);
11888   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11889     ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
11890   else
11891     strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
11892
11893   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
11894                                DEBUG_INFO_SECTION_LABEL, 0);
11895   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
11896                                DEBUG_LINE_SECTION_LABEL, 0);
11897   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
11898                                DEBUG_RANGES_SECTION_LABEL, 0);
11899   named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
11900   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
11901   named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
11902   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11903   named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
11904   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
11905
11906   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11907     {
11908       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11909       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
11910                                    DEBUG_MACINFO_SECTION_LABEL, 0);
11911       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
11912     }
11913
11914   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11915     {
11916       text_section ();
11917       ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
11918     }
11919 }
11920
11921 /* Allocate a string in .debug_str hash table.  */
11922
11923 static hashnode
11924 indirect_string_alloc (tab)
11925      hash_table *tab ATTRIBUTE_UNUSED;
11926 {
11927   struct indirect_string_node *node;
11928
11929   node = xmalloc (sizeof (struct indirect_string_node));
11930   node->refcount = 0;
11931   node->form = 0;
11932   node->label = NULL;
11933
11934   return (hashnode) node;
11935 }
11936
11937 /* A helper function for dwarf2out_finish called through
11938    ht_forall.  Emit one queued .debug_str string.  */
11939
11940 static int
11941 output_indirect_string (pfile, h, v)
11942      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
11943      hashnode h;
11944      const PTR v ATTRIBUTE_UNUSED;
11945 {
11946   struct indirect_string_node *node = (struct indirect_string_node *) h;
11947
11948   if (node->form == DW_FORM_strp)
11949     {
11950       named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
11951       ASM_OUTPUT_LABEL (asm_out_file, node->label);
11952       assemble_string ((const char *) HT_STR (&node->id),
11953                        HT_LEN (&node->id) + 1);
11954     }
11955
11956   return 1;
11957 }
11958
11959 /* Output stuff that dwarf requires at the end of every file,
11960    and generate the DWARF-2 debugging info.  */
11961
11962 static void
11963 dwarf2out_finish (input_filename)
11964      const char *input_filename ATTRIBUTE_UNUSED;
11965 {
11966   limbo_die_node *node, *next_node;
11967   dw_die_ref die = 0;
11968
11969   /* Traverse the limbo die list, and add parent/child links.  The only
11970      dies without parents that should be here are concrete instances of
11971      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
11972      For concrete instances, we can get the parent die from the abstract
11973      instance.  */
11974   for (node = limbo_die_list; node; node = next_node)
11975     {
11976       next_node = node->next;
11977       die = node->die;
11978
11979       if (die->die_parent == NULL)
11980         {
11981           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
11982           tree context;
11983
11984           if (origin)
11985             add_child_die (origin->die_parent, die);
11986           else if (die == comp_unit_die)
11987             ;
11988           else if (node->created_for
11989                    && ((DECL_P (node->created_for)
11990                         && (context = DECL_CONTEXT (node->created_for)))
11991                        || (TYPE_P (node->created_for)
11992                            && (context = TYPE_CONTEXT (node->created_for))))
11993                    && TREE_CODE (context) == FUNCTION_DECL)
11994             {
11995               /* In certain situations, the lexical block containing a
11996                  nested function can be optimized away, which results
11997                  in the nested function die being orphaned.  Likewise
11998                  with the return type of that nested function.  Force
11999                  this to be a child of the containing function.  */
12000               origin = lookup_decl_die (context);
12001               if (! origin)
12002                 abort ();
12003               add_child_die (origin, die);
12004             }
12005           else if (errorcount > 0 || sorrycount > 0)
12006             /* It's OK to be confused by errors in the input.  */
12007             add_child_die (comp_unit_die, die);
12008           else
12009             abort ();
12010         }
12011
12012       free (node);
12013     }
12014
12015   limbo_die_list = NULL;
12016
12017   /* Walk through the list of incomplete types again, trying once more to
12018      emit full debugging info for them.  */
12019   retry_incomplete_types ();
12020
12021   /* We need to reverse all the dies before break_out_includes, or
12022      we'll see the end of an include file before the beginning.  */
12023   reverse_all_dies (comp_unit_die);
12024
12025   /* Generate separate CUs for each of the include files we've seen.
12026      They will go into limbo_die_list.  */
12027   if (flag_eliminate_dwarf2_dups)
12028     break_out_includes (comp_unit_die);
12029
12030   /* Traverse the DIE's and add add sibling attributes to those DIE's
12031      that have children.  */
12032   add_sibling_attributes (comp_unit_die);
12033   for (node = limbo_die_list; node; node = node->next)
12034     add_sibling_attributes (node->die);
12035
12036   /* Output a terminator label for the .text section.  */
12037   text_section ();
12038   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, TEXT_END_LABEL, 0);
12039
12040   /* Output the source line correspondence table.  We must do this
12041      even if there is no line information.  Otherwise, on an empty
12042      translation unit, we will generate a present, but empty,
12043      .debug_info section.  IRIX 6.5 `nm' will then complain when
12044      examining the file.  */
12045   if (! DWARF2_ASM_LINE_DEBUG_INFO)
12046     {
12047       named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
12048       output_line_info ();
12049     }
12050
12051   /* Output location list section if necessary.  */
12052   if (have_location_lists)
12053     {
12054       /* Output the location lists info.  */
12055       named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
12056       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
12057                                    DEBUG_LOC_SECTION_LABEL, 0);
12058       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
12059       output_location_lists (die);
12060       have_location_lists = 0;
12061     }
12062
12063   /* We can only use the low/high_pc attributes if all of the code was
12064      in .text.  */
12065   if (separate_line_info_table_in_use == 0)
12066     {
12067       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
12068       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
12069     }
12070
12071   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
12072      "base address".  Use zero so that these addresses become absolute.  */
12073   else if (have_location_lists || ranges_table_in_use)
12074     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
12075
12076   if (debug_info_level >= DINFO_LEVEL_NORMAL)
12077     add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
12078                        debug_line_section_label);
12079
12080   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12081     add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
12082
12083   /* Output all of the compilation units.  We put the main one last so that
12084      the offsets are available to output_pubnames.  */
12085   for (node = limbo_die_list; node; node = node->next)
12086     output_comp_unit (node->die);
12087
12088   output_comp_unit (comp_unit_die);
12089
12090   /* Output the abbreviation table.  */
12091   named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
12092   output_abbrev_section ();
12093
12094   /* Output public names table if necessary.  */
12095   if (pubname_table_in_use)
12096     {
12097       named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
12098       output_pubnames ();
12099     }
12100
12101   /* Output the address range information.  We only put functions in the arange
12102      table, so don't write it out if we don't have any.  */
12103   if (fde_table_in_use)
12104     {
12105       named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
12106       output_aranges ();
12107     }
12108
12109   /* Output ranges section if necessary.  */
12110   if (ranges_table_in_use)
12111     {
12112       named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
12113       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12114       output_ranges ();
12115     }
12116
12117   /* Have to end the primary source file.  */
12118   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12119     { 
12120       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12121       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
12122     }
12123
12124   /* If we emitted any DW_FORM_strp form attribute, output the string
12125      table too.  */
12126   if (debug_str_hash)
12127     ht_forall (debug_str_hash, output_indirect_string, NULL);
12128 }
12129 #endif /* DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO */