OSDN Git Service

* gcc.c-torture/execute/strct-stdarg-1.x: New file. Expect
[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
7972         if (rtl == NULL_RTX)
7973           return 0;
7974         else if (CONSTANT_P (rtl))
7975           {
7976             ret = new_loc_descr (DW_OP_addr, 0, 0);
7977             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
7978             ret->dw_loc_oprnd1.v.val_addr = rtl;
7979             indirect_p = 1;
7980           }
7981         else
7982           {
7983             enum machine_mode mode = GET_MODE (rtl);
7984
7985             if (GET_CODE (rtl) == MEM)
7986               {
7987                 indirect_p = 1;
7988                 rtl = XEXP (rtl, 0);
7989               }
7990
7991             ret = mem_loc_descriptor (rtl, mode);
7992           }
7993       }
7994       break;
7995
7996     case INDIRECT_REF:
7997       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7998       indirect_p = 1;
7999       break;
8000
8001     case COMPOUND_EXPR:
8002       return loc_descriptor_from_tree (TREE_OPERAND (loc, 1), addressp);
8003
8004     case NOP_EXPR:
8005     case CONVERT_EXPR:
8006     case NON_LVALUE_EXPR:
8007     case VIEW_CONVERT_EXPR:
8008     case SAVE_EXPR:
8009       return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
8010
8011     case COMPONENT_REF:
8012     case BIT_FIELD_REF:
8013     case ARRAY_REF:
8014     case ARRAY_RANGE_REF:
8015       {
8016         tree obj, offset;
8017         HOST_WIDE_INT bitsize, bitpos, bytepos;
8018         enum machine_mode mode;
8019         int volatilep;
8020
8021         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8022                                    &unsignedp, &volatilep);
8023
8024         if (obj == loc)
8025           return 0;
8026
8027         ret = loc_descriptor_from_tree (obj, 1);
8028         if (ret == 0
8029             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8030           return 0;
8031
8032         if (offset != NULL_TREE)
8033           {
8034             /* Variable offset.  */
8035             add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
8036             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8037           }
8038
8039         if (!addressp)
8040           indirect_p = 1;
8041
8042         bytepos = bitpos / BITS_PER_UNIT;
8043         if (bytepos > 0)
8044           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8045         else if (bytepos < 0)
8046           {
8047             add_loc_descr (&ret, int_loc_descriptor (bytepos));
8048             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8049           }
8050         break;
8051       }
8052
8053     case INTEGER_CST:
8054       if (host_integerp (loc, 0))
8055         ret = int_loc_descriptor (tree_low_cst (loc, 0));
8056       else
8057         return 0;
8058       break;
8059
8060     case BIT_AND_EXPR:
8061       op = DW_OP_and;
8062       goto do_binop;
8063
8064     case BIT_XOR_EXPR:
8065       op = DW_OP_xor;
8066       goto do_binop;
8067
8068     case BIT_IOR_EXPR:
8069       op = DW_OP_or;
8070       goto do_binop;
8071
8072     case TRUNC_DIV_EXPR:
8073       op = DW_OP_div;
8074       goto do_binop;
8075
8076     case MINUS_EXPR:
8077       op = DW_OP_minus;
8078       goto do_binop;
8079
8080     case TRUNC_MOD_EXPR:
8081       op = DW_OP_mod;
8082       goto do_binop;
8083
8084     case MULT_EXPR:
8085       op = DW_OP_mul;
8086       goto do_binop;
8087
8088     case LSHIFT_EXPR:
8089       op = DW_OP_shl;
8090       goto do_binop;
8091
8092     case RSHIFT_EXPR:
8093       op = (unsignedp ? DW_OP_shr : DW_OP_shra);
8094       goto do_binop;
8095
8096     case PLUS_EXPR:
8097       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
8098           && host_integerp (TREE_OPERAND (loc, 1), 0))
8099         {
8100           ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8101           if (ret == 0)
8102             return 0;
8103
8104           add_loc_descr (&ret,
8105                          new_loc_descr (DW_OP_plus_uconst,
8106                                         tree_low_cst (TREE_OPERAND (loc, 1),
8107                                                       0),
8108                                         0));
8109           break;
8110         }
8111
8112       op = DW_OP_plus;
8113       goto do_binop;
8114
8115     case LE_EXPR:
8116       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8117         return 0;
8118
8119       op = DW_OP_le;
8120       goto do_binop;
8121
8122     case GE_EXPR:
8123       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8124         return 0;
8125
8126       op = DW_OP_ge;
8127       goto do_binop;
8128
8129     case LT_EXPR:
8130       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8131         return 0;
8132
8133       op = DW_OP_lt;
8134       goto do_binop;
8135
8136     case GT_EXPR:
8137       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8138         return 0;
8139
8140       op = DW_OP_gt;
8141       goto do_binop;
8142
8143     case EQ_EXPR:
8144       op = DW_OP_eq;
8145       goto do_binop;
8146
8147     case NE_EXPR:
8148       op = DW_OP_ne;
8149       goto do_binop;
8150
8151     do_binop:
8152       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8153       ret1 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8154       if (ret == 0 || ret1 == 0)
8155         return 0;
8156
8157       add_loc_descr (&ret, ret1);
8158       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8159       break;
8160
8161     case BIT_NOT_EXPR:
8162       op = DW_OP_not;
8163       goto do_unop;
8164
8165     case ABS_EXPR:
8166       op = DW_OP_abs;
8167       goto do_unop;
8168
8169     case NEGATE_EXPR:
8170       op = DW_OP_neg;
8171       goto do_unop;
8172
8173     do_unop:
8174       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8175       if (ret == 0)
8176         return 0;
8177
8178       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8179       break;
8180
8181     case MAX_EXPR:
8182       loc = build (COND_EXPR, TREE_TYPE (loc),
8183                    build (LT_EXPR, integer_type_node,
8184                           TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
8185                    TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
8186
8187       /* ... fall through ... */
8188
8189     case COND_EXPR:
8190       {
8191         dw_loc_descr_ref lhs
8192           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8193         dw_loc_descr_ref rhs
8194           = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
8195         dw_loc_descr_ref bra_node, jump_node, tmp;
8196
8197         ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8198         if (ret == 0 || lhs == 0 || rhs == 0)
8199           return 0;
8200
8201         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
8202         add_loc_descr (&ret, bra_node);
8203
8204         add_loc_descr (&ret, rhs);
8205         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
8206         add_loc_descr (&ret, jump_node);
8207
8208         add_loc_descr (&ret, lhs);
8209         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8210         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
8211
8212         /* ??? Need a node to point the skip at.  Use a nop.  */
8213         tmp = new_loc_descr (DW_OP_nop, 0, 0);
8214         add_loc_descr (&ret, tmp);
8215         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8216         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
8217       }
8218       break;
8219
8220     default:
8221       abort ();
8222     }
8223
8224   /* Show if we can't fill the request for an address.  */
8225   if (addressp && indirect_p == 0)
8226     return 0;
8227
8228   /* If we've got an address and don't want one, dereference.  */
8229   if (!addressp && indirect_p > 0)
8230     {
8231       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
8232
8233       if (size > DWARF2_ADDR_SIZE || size == -1)
8234         return 0;
8235       else if (size == DWARF2_ADDR_SIZE)
8236         op = DW_OP_deref;
8237       else
8238         op = DW_OP_deref_size;
8239
8240       add_loc_descr (&ret, new_loc_descr (op, size, 0));
8241     }
8242
8243   return ret;
8244 }
8245
8246 /* Given a value, round it up to the lowest multiple of `boundary'
8247    which is not less than the value itself.  */
8248
8249 static inline HOST_WIDE_INT
8250 ceiling (value, boundary)
8251      HOST_WIDE_INT value;
8252      unsigned int boundary;
8253 {
8254   return (((value + boundary - 1) / boundary) * boundary);
8255 }
8256
8257 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
8258    pointer to the declared type for the relevant field variable, or return
8259    `integer_type_node' if the given node turns out to be an
8260    ERROR_MARK node.  */
8261
8262 static inline tree
8263 field_type (decl)
8264      tree decl;
8265 {
8266   tree type;
8267
8268   if (TREE_CODE (decl) == ERROR_MARK)
8269     return integer_type_node;
8270
8271   type = DECL_BIT_FIELD_TYPE (decl);
8272   if (type == NULL_TREE)
8273     type = TREE_TYPE (decl);
8274
8275   return type;
8276 }
8277
8278 /* Given a pointer to a tree node, return the alignment in bits for
8279    it, or else return BITS_PER_WORD if the node actually turns out to
8280    be an ERROR_MARK node.  */
8281
8282 static inline unsigned
8283 simple_type_align_in_bits (type)
8284      tree type;
8285 {
8286   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
8287 }
8288
8289 static inline unsigned
8290 simple_decl_align_in_bits (decl)
8291      tree decl;
8292 {
8293   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
8294 }
8295
8296 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8297    node, return the size in bits for the type if it is a constant, or else
8298    return the alignment for the type if the type's size is not constant, or
8299    else return BITS_PER_WORD if the type actually turns out to be an
8300    ERROR_MARK node.  */
8301
8302 static inline unsigned HOST_WIDE_INT
8303 simple_type_size_in_bits (type)
8304      tree type;
8305 {
8306
8307   if (TREE_CODE (type) == ERROR_MARK)
8308     return BITS_PER_WORD;
8309   else if (TYPE_SIZE (type) == NULL_TREE)
8310     return 0;
8311   else if (host_integerp (TYPE_SIZE (type), 1))
8312     return tree_low_cst (TYPE_SIZE (type), 1);
8313   else
8314     return TYPE_ALIGN (type);
8315 }
8316
8317 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
8318    lowest addressed byte of the "containing object" for the given FIELD_DECL,
8319    or return 0 if we are unable to determine what that offset is, either
8320    because the argument turns out to be a pointer to an ERROR_MARK node, or
8321    because the offset is actually variable.  (We can't handle the latter case
8322    just yet).  */
8323
8324 static HOST_WIDE_INT
8325 field_byte_offset (decl)
8326      tree decl;
8327 {
8328   unsigned int type_align_in_bits;
8329   unsigned int decl_align_in_bits;
8330   unsigned HOST_WIDE_INT type_size_in_bits;
8331   HOST_WIDE_INT object_offset_in_bits;
8332   tree type;
8333   tree field_size_tree;
8334   HOST_WIDE_INT bitpos_int;
8335   HOST_WIDE_INT deepest_bitpos;
8336   unsigned HOST_WIDE_INT field_size_in_bits;
8337
8338   if (TREE_CODE (decl) == ERROR_MARK)
8339     return 0;
8340   else if (TREE_CODE (decl) != FIELD_DECL)
8341     abort ();
8342
8343   type = field_type (decl);
8344   field_size_tree = DECL_SIZE (decl);
8345
8346   /* The size could be unspecified if there was an error, or for
8347      a flexible array member.  */
8348   if (! field_size_tree)
8349     field_size_tree = bitsize_zero_node;
8350
8351   /* We cannot yet cope with fields whose positions are variable, so
8352      for now, when we see such things, we simply return 0.  Someday, we may
8353      be able to handle such cases, but it will be damn difficult.  */
8354   if (! host_integerp (bit_position (decl), 0))
8355     return 0;
8356
8357   bitpos_int = int_bit_position (decl);
8358
8359   /* If we don't know the size of the field, pretend it's a full word.  */
8360   if (host_integerp (field_size_tree, 1))
8361     field_size_in_bits = tree_low_cst (field_size_tree, 1);
8362   else
8363     field_size_in_bits = BITS_PER_WORD;
8364
8365   type_size_in_bits = simple_type_size_in_bits (type);
8366   type_align_in_bits = simple_type_align_in_bits (type);
8367   decl_align_in_bits = simple_decl_align_in_bits (decl);
8368
8369   /* The GCC front-end doesn't make any attempt to keep track of the starting
8370      bit offset (relative to the start of the containing structure type) of the
8371      hypothetical "containing object" for a bit-field.  Thus, when computing
8372      the byte offset value for the start of the "containing object" of a
8373      bit-field, we must deduce this information on our own. This can be rather
8374      tricky to do in some cases.  For example, handling the following structure
8375      type definition when compiling for an i386/i486 target (which only aligns
8376      long long's to 32-bit boundaries) can be very tricky:
8377
8378          struct S { int field1; long long field2:31; };
8379
8380      Fortunately, there is a simple rule-of-thumb which can be used in such
8381      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
8382      structure shown above.  It decides to do this based upon one simple rule
8383      for bit-field allocation.  GCC allocates each "containing object" for each
8384      bit-field at the first (i.e. lowest addressed) legitimate alignment
8385      boundary (based upon the required minimum alignment for the declared type
8386      of the field) which it can possibly use, subject to the condition that
8387      there is still enough available space remaining in the containing object
8388      (when allocated at the selected point) to fully accommodate all of the
8389      bits of the bit-field itself.
8390
8391      This simple rule makes it obvious why GCC allocates 8 bytes for each
8392      object of the structure type shown above.  When looking for a place to
8393      allocate the "containing object" for `field2', the compiler simply tries
8394      to allocate a 64-bit "containing object" at each successive 32-bit
8395      boundary (starting at zero) until it finds a place to allocate that 64-
8396      bit field such that at least 31 contiguous (and previously unallocated)
8397      bits remain within that selected 64 bit field.  (As it turns out, for the
8398      example above, the compiler finds it is OK to allocate the "containing
8399      object" 64-bit field at bit-offset zero within the structure type.)
8400
8401      Here we attempt to work backwards from the limited set of facts we're
8402      given, and we try to deduce from those facts, where GCC must have believed
8403      that the containing object started (within the structure type). The value
8404      we deduce is then used (by the callers of this routine) to generate
8405      DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
8406      and, in the case of DW_AT_location, regular fields as well).  */
8407
8408   /* Figure out the bit-distance from the start of the structure to the
8409      "deepest" bit of the bit-field.  */
8410   deepest_bitpos = bitpos_int + field_size_in_bits;
8411
8412   /* This is the tricky part.  Use some fancy footwork to deduce where the
8413      lowest addressed bit of the containing object must be.  */
8414   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8415
8416   /* Round up to type_align by default.  This works best for bitfields.  */
8417   object_offset_in_bits += type_align_in_bits - 1;
8418   object_offset_in_bits /= type_align_in_bits;
8419   object_offset_in_bits *= type_align_in_bits;
8420
8421   if (object_offset_in_bits > bitpos_int)
8422     {
8423       /* Sigh, the decl must be packed.  */
8424       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8425
8426       /* Round up to decl_align instead.  */
8427       object_offset_in_bits += decl_align_in_bits - 1;
8428       object_offset_in_bits /= decl_align_in_bits;
8429       object_offset_in_bits *= decl_align_in_bits;
8430     }
8431
8432   return object_offset_in_bits / BITS_PER_UNIT;
8433 }
8434 \f
8435 /* The following routines define various Dwarf attributes and any data
8436    associated with them.  */
8437
8438 /* Add a location description attribute value to a DIE.
8439
8440    This emits location attributes suitable for whole variables and
8441    whole parameters.  Note that the location attributes for struct fields are
8442    generated by the routine `data_member_location_attribute' below.  */
8443
8444 static void
8445 add_AT_location_description (die, attr_kind, rtl)
8446      dw_die_ref die;
8447      enum dwarf_attribute attr_kind;
8448      rtx rtl;
8449 {
8450   dw_loc_descr_ref descr = loc_descriptor (rtl);
8451
8452   if (descr != 0)
8453     add_AT_loc (die, attr_kind, descr);
8454 }
8455
8456 /* Attach the specialized form of location attribute used for data members of
8457    struct and union types.  In the special case of a FIELD_DECL node which
8458    represents a bit-field, the "offset" part of this special location
8459    descriptor must indicate the distance in bytes from the lowest-addressed
8460    byte of the containing struct or union type to the lowest-addressed byte of
8461    the "containing object" for the bit-field.  (See the `field_byte_offset'
8462    function above).
8463
8464    For any given bit-field, the "containing object" is a hypothetical object
8465    (of some integral or enum type) within which the given bit-field lives.  The
8466    type of this hypothetical "containing object" is always the same as the
8467    declared type of the individual bit-field itself (for GCC anyway... the
8468    DWARF spec doesn't actually mandate this).  Note that it is the size (in
8469    bytes) of the hypothetical "containing object" which will be given in the
8470    DW_AT_byte_size attribute for this bit-field.  (See the
8471    `byte_size_attribute' function below.)  It is also used when calculating the
8472    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
8473    function below.)  */
8474
8475 static void
8476 add_data_member_location_attribute (die, decl)
8477      dw_die_ref die;
8478      tree decl;
8479 {
8480   long offset;
8481   dw_loc_descr_ref loc_descr = 0;
8482
8483   if (TREE_CODE (decl) == TREE_VEC)
8484     {
8485       /* We're working on the TAG_inheritance for a base class.  */
8486       if (TREE_VIA_VIRTUAL (decl) && is_cxx ())
8487         {
8488           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
8489              aren't at a fixed offset from all (sub)objects of the same
8490              type.  We need to extract the appropriate offset from our
8491              vtable.  The following dwarf expression means
8492
8493                BaseAddr = ObAddr + *((*ObAddr) - Offset)
8494
8495              This is specific to the V3 ABI, of course.  */
8496
8497           dw_loc_descr_ref tmp;
8498
8499           /* Make a copy of the object address.  */
8500           tmp = new_loc_descr (DW_OP_dup, 0, 0);
8501           add_loc_descr (&loc_descr, tmp);
8502
8503           /* Extract the vtable address.  */
8504           tmp = new_loc_descr (DW_OP_deref, 0, 0);
8505           add_loc_descr (&loc_descr, tmp);
8506
8507           /* Calculate the address of the offset.  */
8508           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
8509           if (offset >= 0)
8510             abort ();
8511
8512           tmp = int_loc_descriptor (-offset);
8513           add_loc_descr (&loc_descr, tmp);
8514           tmp = new_loc_descr (DW_OP_minus, 0, 0);
8515           add_loc_descr (&loc_descr, tmp);
8516
8517           /* Extract the offset.  */
8518           tmp = new_loc_descr (DW_OP_deref, 0, 0);
8519           add_loc_descr (&loc_descr, tmp);
8520
8521           /* Add it to the object address.  */
8522           tmp = new_loc_descr (DW_OP_plus, 0, 0);
8523           add_loc_descr (&loc_descr, tmp);
8524         }
8525       else
8526         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
8527     }
8528   else
8529     offset = field_byte_offset (decl);
8530
8531   if (! loc_descr)
8532     {
8533       enum dwarf_location_atom op;
8534
8535       /* The DWARF2 standard says that we should assume that the structure
8536          address is already on the stack, so we can specify a structure field
8537          address by using DW_OP_plus_uconst.  */
8538
8539 #ifdef MIPS_DEBUGGING_INFO
8540       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
8541          operator correctly.  It works only if we leave the offset on the
8542          stack.  */
8543       op = DW_OP_constu;
8544 #else
8545       op = DW_OP_plus_uconst;
8546 #endif
8547
8548       loc_descr = new_loc_descr (op, offset, 0);
8549     }
8550
8551   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
8552 }
8553
8554 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
8555    does not have a "location" either in memory or in a register.  These
8556    things can arise in GNU C when a constant is passed as an actual parameter
8557    to an inlined function.  They can also arise in C++ where declared
8558    constants do not necessarily get memory "homes".  */
8559
8560 static void
8561 add_const_value_attribute (die, rtl)
8562      dw_die_ref die;
8563      rtx rtl;
8564 {
8565   switch (GET_CODE (rtl))
8566     {
8567     case CONST_INT:
8568       /* Note that a CONST_INT rtx could represent either an integer
8569          or a floating-point constant.  A CONST_INT is used whenever
8570          the constant will fit into a single word.  In all such
8571          cases, the original mode of the constant value is wiped
8572          out, and the CONST_INT rtx is assigned VOIDmode.  */
8573       {
8574         HOST_WIDE_INT val = INTVAL (rtl);
8575         
8576         /* ??? We really should be using HOST_WIDE_INT throughout.  */
8577         if (val < 0 && (long) val == val)
8578           add_AT_int (die, DW_AT_const_value, (long) val);
8579         else if ((unsigned long) val == (unsigned HOST_WIDE_INT) val)
8580           add_AT_unsigned (die, DW_AT_const_value, (unsigned long) val);
8581         else
8582           {
8583 #if HOST_BITS_PER_LONG * 2 == HOST_BITS_PER_WIDE_INT
8584             add_AT_long_long (die, DW_AT_const_value,
8585                               val >> HOST_BITS_PER_LONG, val);
8586 #else
8587             abort ();
8588 #endif
8589           }
8590       }
8591       break;
8592
8593     case CONST_DOUBLE:
8594       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
8595          floating-point constant.  A CONST_DOUBLE is used whenever the
8596          constant requires more than one word in order to be adequately
8597          represented.  We output CONST_DOUBLEs as blocks.  */
8598       {
8599         enum machine_mode mode = GET_MODE (rtl);
8600
8601         if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8602           {
8603             unsigned length = GET_MODE_SIZE (mode) / 4;
8604             long *array = (long *) xmalloc (sizeof (long) * length);
8605             REAL_VALUE_TYPE rv;
8606
8607             REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
8608             switch (mode)
8609               {
8610               case SFmode:
8611                 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
8612                 break;
8613
8614               case DFmode:
8615                 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
8616                 break;
8617
8618               case XFmode:
8619               case TFmode:
8620                 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
8621                 break;
8622
8623               default:
8624                 abort ();
8625               }
8626
8627             add_AT_float (die, DW_AT_const_value, length, array);
8628           }
8629         else
8630           {
8631             /* ??? We really should be using HOST_WIDE_INT throughout.  */
8632             if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
8633               abort ();
8634
8635             add_AT_long_long (die, DW_AT_const_value,
8636                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
8637           }
8638       }
8639       break;
8640
8641     case CONST_STRING:
8642       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
8643       break;
8644
8645     case SYMBOL_REF:
8646     case LABEL_REF:
8647     case CONST:
8648       add_AT_addr (die, DW_AT_const_value, rtl);
8649       VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8650       break;
8651
8652     case PLUS:
8653       /* In cases where an inlined instance of an inline function is passed
8654          the address of an `auto' variable (which is local to the caller) we
8655          can get a situation where the DECL_RTL of the artificial local
8656          variable (for the inlining) which acts as a stand-in for the
8657          corresponding formal parameter (of the inline function) will look
8658          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
8659          exactly a compile-time constant expression, but it isn't the address
8660          of the (artificial) local variable either.  Rather, it represents the
8661          *value* which the artificial local variable always has during its
8662          lifetime.  We currently have no way to represent such quasi-constant
8663          values in Dwarf, so for now we just punt and generate nothing.  */
8664       break;
8665
8666     default:
8667       /* No other kinds of rtx should be possible here.  */
8668       abort ();
8669     }
8670
8671 }
8672
8673 static rtx
8674 rtl_for_decl_location (decl)
8675      tree decl;
8676 {
8677   rtx rtl;
8678
8679   /* Here we have to decide where we are going to say the parameter "lives"
8680      (as far as the debugger is concerned).  We only have a couple of
8681      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
8682
8683      DECL_RTL normally indicates where the parameter lives during most of the
8684      activation of the function.  If optimization is enabled however, this
8685      could be either NULL or else a pseudo-reg.  Both of those cases indicate
8686      that the parameter doesn't really live anywhere (as far as the code
8687      generation parts of GCC are concerned) during most of the function's
8688      activation.  That will happen (for example) if the parameter is never
8689      referenced within the function.
8690
8691      We could just generate a location descriptor here for all non-NULL
8692      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
8693      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
8694      where DECL_RTL is NULL or is a pseudo-reg.
8695
8696      Note however that we can only get away with using DECL_INCOMING_RTL as
8697      a backup substitute for DECL_RTL in certain limited cases.  In cases
8698      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
8699      we can be sure that the parameter was passed using the same type as it is
8700      declared to have within the function, and that its DECL_INCOMING_RTL
8701      points us to a place where a value of that type is passed.
8702
8703      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
8704      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
8705      because in these cases DECL_INCOMING_RTL points us to a value of some
8706      type which is *different* from the type of the parameter itself.  Thus,
8707      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
8708      such cases, the debugger would end up (for example) trying to fetch a
8709      `float' from a place which actually contains the first part of a
8710      `double'.  That would lead to really incorrect and confusing
8711      output at debug-time.
8712
8713      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
8714      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
8715      are a couple of exceptions however.  On little-endian machines we can
8716      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
8717      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
8718      an integral type that is smaller than TREE_TYPE (decl). These cases arise
8719      when (on a little-endian machine) a non-prototyped function has a
8720      parameter declared to be of type `short' or `char'.  In such cases,
8721      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
8722      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
8723      passed `int' value.  If the debugger then uses that address to fetch
8724      a `short' or a `char' (on a little-endian machine) the result will be
8725      the correct data, so we allow for such exceptional cases below.
8726
8727      Note that our goal here is to describe the place where the given formal
8728      parameter lives during most of the function's activation (i.e. between the
8729      end of the prologue and the start of the epilogue).  We'll do that as best
8730      as we can. Note however that if the given formal parameter is modified
8731      sometime during the execution of the function, then a stack backtrace (at
8732      debug-time) will show the function as having been called with the *new*
8733      value rather than the value which was originally passed in.  This happens
8734      rarely enough that it is not a major problem, but it *is* a problem, and
8735      I'd like to fix it.
8736
8737      A future version of dwarf2out.c may generate two additional attributes for
8738      any given DW_TAG_formal_parameter DIE which will describe the "passed
8739      type" and the "passed location" for the given formal parameter in addition
8740      to the attributes we now generate to indicate the "declared type" and the
8741      "active location" for each parameter.  This additional set of attributes
8742      could be used by debuggers for stack backtraces. Separately, note that
8743      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
8744      This happens (for example) for inlined-instances of inline function formal
8745      parameters which are never referenced.  This really shouldn't be
8746      happening.  All PARM_DECL nodes should get valid non-NULL
8747      DECL_INCOMING_RTL values, but integrate.c doesn't currently generate these
8748      values for inlined instances of inline function parameters, so when we see
8749      such cases, we are just out-of-luck for the time being (until integrate.c
8750      gets fixed).  */
8751
8752   /* Use DECL_RTL as the "location" unless we find something better.  */
8753   rtl = DECL_RTL_IF_SET (decl);
8754
8755   /* When generating abstract instances, ignore everything except
8756      constants and symbols living in memory.  */
8757   if (! reload_completed)
8758     {
8759       if (rtl
8760           && (CONSTANT_P (rtl)
8761               || (GET_CODE (rtl) == MEM
8762                   && CONSTANT_P (XEXP (rtl, 0)))))
8763         return rtl;
8764       rtl = NULL_RTX;
8765     }
8766   else if (TREE_CODE (decl) == PARM_DECL)
8767     {
8768       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
8769         {
8770           tree declared_type = type_main_variant (TREE_TYPE (decl));
8771           tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
8772
8773           /* This decl represents a formal parameter which was optimized out.
8774              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
8775              all cases where (rtl == NULL_RTX) just below.  */
8776           if (declared_type == passed_type)
8777             rtl = DECL_INCOMING_RTL (decl);
8778           else if (! BYTES_BIG_ENDIAN
8779                    && TREE_CODE (declared_type) == INTEGER_TYPE
8780                    && (GET_MODE_SIZE (TYPE_MODE (declared_type))
8781                        <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
8782             rtl = DECL_INCOMING_RTL (decl);
8783         }
8784
8785       /* If the parm was passed in registers, but lives on the stack, then
8786          make a big endian correction if the mode of the type of the
8787          parameter is not the same as the mode of the rtl.  */
8788       /* ??? This is the same series of checks that are made in dbxout.c before
8789          we reach the big endian correction code there.  It isn't clear if all
8790          of these checks are necessary here, but keeping them all is the safe
8791          thing to do.  */
8792       else if (GET_CODE (rtl) == MEM
8793                && XEXP (rtl, 0) != const0_rtx
8794                && ! CONSTANT_P (XEXP (rtl, 0))
8795                /* Not passed in memory.  */
8796                && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
8797                /* Not passed by invisible reference.  */
8798                && (GET_CODE (XEXP (rtl, 0)) != REG
8799                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
8800                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
8801 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
8802                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
8803 #endif
8804                      )
8805                /* Big endian correction check.  */
8806                && BYTES_BIG_ENDIAN
8807                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
8808                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
8809                    < UNITS_PER_WORD))
8810         {
8811           int offset = (UNITS_PER_WORD
8812                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
8813
8814           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
8815                              plus_constant (XEXP (rtl, 0), offset));
8816         }
8817     }
8818
8819   if (rtl != NULL_RTX)
8820     {
8821       rtl = eliminate_regs (rtl, 0, NULL_RTX);
8822 #ifdef LEAF_REG_REMAP
8823       if (current_function_uses_only_leaf_regs)
8824         leaf_renumber_regs_insn (rtl);
8825 #endif
8826     }
8827
8828   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
8829      and will have been substituted directly into all expressions that use it.
8830      C does not have such a concept, but C++ and other languages do.  */
8831   else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
8832     rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
8833                        EXPAND_INITIALIZER);
8834
8835   return rtl;
8836 }
8837
8838 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
8839    data attribute for a variable or a parameter.  We generate the
8840    DW_AT_const_value attribute only in those cases where the given variable
8841    or parameter does not have a true "location" either in memory or in a
8842    register.  This can happen (for example) when a constant is passed as an
8843    actual argument in a call to an inline function.  (It's possible that
8844    these things can crop up in other ways also.)  Note that one type of
8845    constant value which can be passed into an inlined function is a constant
8846    pointer.  This can happen for example if an actual argument in an inlined
8847    function call evaluates to a compile-time constant address.  */
8848
8849 static void
8850 add_location_or_const_value_attribute (die, decl)
8851      dw_die_ref die;
8852      tree decl;
8853 {
8854   rtx rtl;
8855
8856   if (TREE_CODE (decl) == ERROR_MARK)
8857     return;
8858   else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
8859     abort ();
8860
8861   rtl = rtl_for_decl_location (decl);
8862   if (rtl == NULL_RTX)
8863     return;
8864
8865   /* If we don't look past the constant pool, we risk emitting a
8866      reference to a constant pool entry that isn't referenced from
8867      code, and thus is not emitted.  */
8868   rtl = avoid_constant_pool_reference (rtl);
8869
8870   switch (GET_CODE (rtl))
8871     {
8872     case ADDRESSOF:
8873       /* The address of a variable that was optimized away; don't emit
8874          anything.  */
8875       break;
8876
8877     case CONST_INT:
8878     case CONST_DOUBLE:
8879     case CONST_STRING:
8880     case SYMBOL_REF:
8881     case LABEL_REF:
8882     case CONST:
8883     case PLUS:
8884       /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
8885       add_const_value_attribute (die, rtl);
8886       break;
8887
8888     case MEM:
8889     case REG:
8890     case SUBREG:
8891     case CONCAT:
8892       add_AT_location_description (die, DW_AT_location, rtl);
8893       break;
8894
8895     default:
8896       abort ();
8897     }
8898 }
8899
8900 /* If we don't have a copy of this variable in memory for some reason (such
8901    as a C++ member constant that doesn't have an out-of-line definition),
8902    we should tell the debugger about the constant value.  */
8903
8904 static void
8905 tree_add_const_value_attribute (var_die, decl)
8906      dw_die_ref var_die;
8907      tree decl;
8908 {
8909   tree init = DECL_INITIAL (decl);
8910   tree type = TREE_TYPE (decl);
8911
8912   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
8913       && initializer_constant_valid_p (init, type) == null_pointer_node)
8914     /* OK */;
8915   else
8916     return;
8917
8918   switch (TREE_CODE (type))
8919     {
8920     case INTEGER_TYPE:
8921       if (host_integerp (init, 0))
8922         add_AT_unsigned (var_die, DW_AT_const_value,
8923                          tree_low_cst (init, 0));
8924       else
8925         add_AT_long_long (var_die, DW_AT_const_value,
8926                           TREE_INT_CST_HIGH (init),
8927                           TREE_INT_CST_LOW (init));
8928       break;
8929
8930     default:;
8931     }
8932 }
8933
8934 /* Generate an DW_AT_name attribute given some string value to be included as
8935    the value of the attribute.  */
8936
8937 static inline void
8938 add_name_attribute (die, name_string)
8939      dw_die_ref die;
8940      const char *name_string;
8941 {
8942   if (name_string != NULL && *name_string != 0)
8943     {
8944       if (demangle_name_func)
8945         name_string = (*demangle_name_func) (name_string);
8946
8947       add_AT_string (die, DW_AT_name, name_string);
8948     }
8949 }
8950
8951 /* Given a tree node describing an array bound (either lower or upper) output
8952    a representation for that bound.  */
8953
8954 static void
8955 add_bound_info (subrange_die, bound_attr, bound)
8956      dw_die_ref subrange_die;
8957      enum dwarf_attribute bound_attr;
8958      tree bound;
8959 {
8960   switch (TREE_CODE (bound))
8961     {
8962     case ERROR_MARK:
8963       return;
8964
8965     /* All fixed-bounds are represented by INTEGER_CST nodes.        */
8966     case INTEGER_CST:
8967       if (! host_integerp (bound, 0)
8968           || (bound_attr == DW_AT_lower_bound
8969               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
8970                   || (is_fortran () && integer_onep (bound)))))
8971         /* use the default */
8972         ;
8973       else
8974         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
8975       break;
8976
8977     case CONVERT_EXPR:
8978     case NOP_EXPR:
8979     case NON_LVALUE_EXPR:
8980     case VIEW_CONVERT_EXPR:
8981       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
8982       break;
8983
8984     case SAVE_EXPR:
8985       /* If optimization is turned on, the SAVE_EXPRs that describe how to
8986          access the upper bound values may be bogus.  If they refer to a
8987          register, they may only describe how to get at these values at the
8988          points in the generated code right after they have just been
8989          computed.  Worse yet, in the typical case, the upper bound values
8990          will not even *be* computed in the optimized code (though the
8991          number of elements will), so these SAVE_EXPRs are entirely
8992          bogus. In order to compensate for this fact, we check here to see
8993          if optimization is enabled, and if so, we don't add an attribute
8994          for the (unknown and unknowable) upper bound.  This should not
8995          cause too much trouble for existing (stupid?)  debuggers because
8996          they have to deal with empty upper bounds location descriptions
8997          anyway in order to be able to deal with incomplete array types.
8998          Of course an intelligent debugger (GDB?)  should be able to
8999          comprehend that a missing upper bound specification in an array
9000          type used for a storage class `auto' local array variable
9001          indicates that the upper bound is both unknown (at compile- time)
9002          and unknowable (at run-time) due to optimization.
9003
9004          We assume that a MEM rtx is safe because gcc wouldn't put the
9005          value there unless it was going to be used repeatedly in the
9006          function, i.e. for cleanups.  */
9007       if (SAVE_EXPR_RTL (bound)
9008           && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
9009         {
9010           dw_die_ref ctx = lookup_decl_die (current_function_decl);
9011           dw_die_ref decl_die = new_die (DW_TAG_variable, ctx, bound);
9012           rtx loc = SAVE_EXPR_RTL (bound);
9013
9014           /* If the RTL for the SAVE_EXPR is memory, handle the case where
9015              it references an outer function's frame.  */
9016           if (GET_CODE (loc) == MEM)
9017             {
9018               rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
9019
9020               if (XEXP (loc, 0) != new_addr)
9021                 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
9022             }
9023
9024           add_AT_flag (decl_die, DW_AT_artificial, 1);
9025           add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9026           add_AT_location_description (decl_die, DW_AT_location, loc);
9027           add_AT_die_ref (subrange_die, bound_attr, decl_die);
9028         }
9029
9030       /* Else leave out the attribute.  */
9031       break;
9032
9033     case VAR_DECL:
9034     case PARM_DECL:
9035       {
9036         dw_die_ref decl_die = lookup_decl_die (bound);
9037
9038         /* ??? Can this happen, or should the variable have been bound
9039            first?  Probably it can, since I imagine that we try to create
9040            the types of parameters in the order in which they exist in
9041            the list, and won't have created a forward reference to a
9042            later parameter.  */
9043         if (decl_die != NULL)
9044           add_AT_die_ref (subrange_die, bound_attr, decl_die);
9045         break;
9046       }
9047
9048     default:
9049       {
9050         /* Otherwise try to create a stack operation procedure to
9051            evaluate the value of the array bound.  */
9052
9053         dw_die_ref ctx, decl_die;
9054         dw_loc_descr_ref loc;
9055
9056         loc = loc_descriptor_from_tree (bound, 0);
9057         if (loc == NULL)
9058           break;
9059
9060         if (current_function_decl == 0)
9061           ctx = comp_unit_die;
9062         else
9063           ctx = lookup_decl_die (current_function_decl);
9064
9065         decl_die = new_die (DW_TAG_variable, ctx, bound);
9066         add_AT_flag (decl_die, DW_AT_artificial, 1);
9067         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9068         add_AT_loc (decl_die, DW_AT_location, loc);
9069
9070         add_AT_die_ref (subrange_die, bound_attr, decl_die);
9071         break;
9072       }
9073     }
9074 }
9075
9076 /* Note that the block of subscript information for an array type also
9077    includes information about the element type of type given array type.  */
9078
9079 static void
9080 add_subscript_info (type_die, type)
9081      dw_die_ref type_die;
9082      tree type;
9083 {
9084 #ifndef MIPS_DEBUGGING_INFO
9085   unsigned dimension_number;
9086 #endif
9087   tree lower, upper;
9088   dw_die_ref subrange_die;
9089
9090   /* The GNU compilers represent multidimensional array types as sequences of
9091      one dimensional array types whose element types are themselves array
9092      types.  Here we squish that down, so that each multidimensional array
9093      type gets only one array_type DIE in the Dwarf debugging info. The draft
9094      Dwarf specification say that we are allowed to do this kind of
9095      compression in C (because there is no difference between an array or
9096      arrays and a multidimensional array in C) but for other source languages
9097      (e.g. Ada) we probably shouldn't do this.  */
9098
9099   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9100      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
9101      We work around this by disabling this feature.  See also
9102      gen_array_type_die.  */
9103 #ifndef MIPS_DEBUGGING_INFO
9104   for (dimension_number = 0;
9105        TREE_CODE (type) == ARRAY_TYPE;
9106        type = TREE_TYPE (type), dimension_number++)
9107 #endif
9108     {
9109       tree domain = TYPE_DOMAIN (type);
9110
9111       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
9112          and (in GNU C only) variable bounds.  Handle all three forms
9113          here.  */
9114       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
9115       if (domain)
9116         {
9117           /* We have an array type with specified bounds.  */
9118           lower = TYPE_MIN_VALUE (domain);
9119           upper = TYPE_MAX_VALUE (domain);
9120
9121           /* define the index type.  */
9122           if (TREE_TYPE (domain))
9123             {
9124               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
9125                  TREE_TYPE field.  We can't emit debug info for this
9126                  because it is an unnamed integral type.  */
9127               if (TREE_CODE (domain) == INTEGER_TYPE
9128                   && TYPE_NAME (domain) == NULL_TREE
9129                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
9130                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
9131                 ;
9132               else
9133                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
9134                                     type_die);
9135             }
9136
9137           /* ??? If upper is NULL, the array has unspecified length,
9138              but it does have a lower bound.  This happens with Fortran
9139                dimension arr(N:*)
9140              Since the debugger is definitely going to need to know N
9141              to produce useful results, go ahead and output the lower
9142              bound solo, and hope the debugger can cope.  */
9143
9144           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
9145           if (upper)
9146             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
9147         }
9148
9149       /* Otherwise we have an array type with an unspecified length.  The
9150          DWARF-2 spec does not say how to handle this; let's just leave out the
9151          bounds.  */
9152     }
9153 }
9154
9155 static void
9156 add_byte_size_attribute (die, tree_node)
9157      dw_die_ref die;
9158      tree tree_node;
9159 {
9160   unsigned size;
9161
9162   switch (TREE_CODE (tree_node))
9163     {
9164     case ERROR_MARK:
9165       size = 0;
9166       break;
9167     case ENUMERAL_TYPE:
9168     case RECORD_TYPE:
9169     case UNION_TYPE:
9170     case QUAL_UNION_TYPE:
9171       size = int_size_in_bytes (tree_node);
9172       break;
9173     case FIELD_DECL:
9174       /* For a data member of a struct or union, the DW_AT_byte_size is
9175          generally given as the number of bytes normally allocated for an
9176          object of the *declared* type of the member itself.  This is true
9177          even for bit-fields.  */
9178       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
9179       break;
9180     default:
9181       abort ();
9182     }
9183
9184   /* Note that `size' might be -1 when we get to this point.  If it is, that
9185      indicates that the byte size of the entity in question is variable.  We
9186      have no good way of expressing this fact in Dwarf at the present time,
9187      so just let the -1 pass on through.  */
9188   add_AT_unsigned (die, DW_AT_byte_size, size);
9189 }
9190
9191 /* For a FIELD_DECL node which represents a bit-field, output an attribute
9192    which specifies the distance in bits from the highest order bit of the
9193    "containing object" for the bit-field to the highest order bit of the
9194    bit-field itself.
9195
9196    For any given bit-field, the "containing object" is a hypothetical object
9197    (of some integral or enum type) within which the given bit-field lives.  The
9198    type of this hypothetical "containing object" is always the same as the
9199    declared type of the individual bit-field itself.  The determination of the
9200    exact location of the "containing object" for a bit-field is rather
9201    complicated.  It's handled by the `field_byte_offset' function (above).
9202
9203    Note that it is the size (in bytes) of the hypothetical "containing object"
9204    which will be given in the DW_AT_byte_size attribute for this bit-field.
9205    (See `byte_size_attribute' above).  */
9206
9207 static inline void
9208 add_bit_offset_attribute (die, decl)
9209      dw_die_ref die;
9210      tree decl;
9211 {
9212   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
9213   tree type = DECL_BIT_FIELD_TYPE (decl);
9214   HOST_WIDE_INT bitpos_int;
9215   HOST_WIDE_INT highest_order_object_bit_offset;
9216   HOST_WIDE_INT highest_order_field_bit_offset;
9217   HOST_WIDE_INT unsigned bit_offset;
9218
9219   /* Must be a field and a bit field.  */
9220   if (!type
9221       || TREE_CODE (decl) != FIELD_DECL)
9222     abort ();
9223
9224   /* We can't yet handle bit-fields whose offsets are variable, so if we
9225      encounter such things, just return without generating any attribute
9226      whatsoever.  Likewise for variable or too large size.  */
9227   if (! host_integerp (bit_position (decl), 0)
9228       || ! host_integerp (DECL_SIZE (decl), 1))
9229     return;
9230
9231   bitpos_int = int_bit_position (decl);
9232
9233   /* Note that the bit offset is always the distance (in bits) from the
9234      highest-order bit of the "containing object" to the highest-order bit of
9235      the bit-field itself.  Since the "high-order end" of any object or field
9236      is different on big-endian and little-endian machines, the computation
9237      below must take account of these differences.  */
9238   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
9239   highest_order_field_bit_offset = bitpos_int;
9240
9241   if (! BYTES_BIG_ENDIAN)
9242     {
9243       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
9244       highest_order_object_bit_offset += simple_type_size_in_bits (type);
9245     }
9246
9247   bit_offset
9248     = (! BYTES_BIG_ENDIAN
9249        ? highest_order_object_bit_offset - highest_order_field_bit_offset
9250        : highest_order_field_bit_offset - highest_order_object_bit_offset);
9251
9252   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
9253 }
9254
9255 /* For a FIELD_DECL node which represents a bit field, output an attribute
9256    which specifies the length in bits of the given field.  */
9257
9258 static inline void
9259 add_bit_size_attribute (die, decl)
9260      dw_die_ref die;
9261      tree decl;
9262 {
9263   /* Must be a field and a bit field.  */
9264   if (TREE_CODE (decl) != FIELD_DECL
9265       || ! DECL_BIT_FIELD_TYPE (decl))
9266     abort ();
9267
9268   if (host_integerp (DECL_SIZE (decl), 1))
9269     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
9270 }
9271
9272 /* If the compiled language is ANSI C, then add a 'prototyped'
9273    attribute, if arg types are given for the parameters of a function.  */
9274
9275 static inline void
9276 add_prototyped_attribute (die, func_type)
9277      dw_die_ref die;
9278      tree func_type;
9279 {
9280   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
9281       && TYPE_ARG_TYPES (func_type) != NULL)
9282     add_AT_flag (die, DW_AT_prototyped, 1);
9283 }
9284
9285 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
9286    by looking in either the type declaration or object declaration
9287    equate table.  */
9288
9289 static inline void
9290 add_abstract_origin_attribute (die, origin)
9291      dw_die_ref die;
9292      tree origin;
9293 {
9294   dw_die_ref origin_die = NULL;
9295
9296   if (TREE_CODE (origin) != FUNCTION_DECL)
9297     {
9298       /* We may have gotten separated from the block for the inlined
9299          function, if we're in an exception handler or some such; make
9300          sure that the abstract function has been written out.
9301
9302          Doing this for nested functions is wrong, however; functions are
9303          distinct units, and our context might not even be inline.  */
9304       tree fn = origin;
9305
9306       if (TYPE_P (fn))
9307         fn = TYPE_STUB_DECL (fn);
9308
9309       fn = decl_function_context (fn);
9310       if (fn)
9311         dwarf2out_abstract_function (fn);
9312     }
9313
9314   if (DECL_P (origin))
9315     origin_die = lookup_decl_die (origin);
9316   else if (TYPE_P (origin))
9317     origin_die = lookup_type_die (origin);
9318
9319   if (origin_die == NULL)
9320     abort ();
9321
9322   add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
9323 }
9324
9325 /* We do not currently support the pure_virtual attribute.  */
9326
9327 static inline void
9328 add_pure_or_virtual_attribute (die, func_decl)
9329      dw_die_ref die;
9330      tree func_decl;
9331 {
9332   if (DECL_VINDEX (func_decl))
9333     {
9334       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
9335
9336       if (host_integerp (DECL_VINDEX (func_decl), 0))
9337         add_AT_loc (die, DW_AT_vtable_elem_location,
9338                     new_loc_descr (DW_OP_constu,
9339                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
9340                                    0));
9341
9342       /* GNU extension: Record what type this method came from originally.  */
9343       if (debug_info_level > DINFO_LEVEL_TERSE)
9344         add_AT_die_ref (die, DW_AT_containing_type,
9345                         lookup_type_die (DECL_CONTEXT (func_decl)));
9346     }
9347 }
9348 \f
9349 /* Add source coordinate attributes for the given decl.  */
9350
9351 static void
9352 add_src_coords_attributes (die, decl)
9353      dw_die_ref die;
9354      tree decl;
9355 {
9356   unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
9357
9358   add_AT_unsigned (die, DW_AT_decl_file, file_index);
9359   add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
9360 }
9361
9362 /* Add an DW_AT_name attribute and source coordinate attribute for the
9363    given decl, but only if it actually has a name.  */
9364
9365 static void
9366 add_name_and_src_coords_attributes (die, decl)
9367      dw_die_ref die;
9368      tree decl;
9369 {
9370   tree decl_name;
9371
9372   decl_name = DECL_NAME (decl);
9373   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
9374     {
9375       add_name_attribute (die, dwarf2_name (decl, 0));
9376       if (! DECL_ARTIFICIAL (decl))
9377         add_src_coords_attributes (die, decl);
9378
9379       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
9380           && TREE_PUBLIC (decl)
9381           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
9382           && !DECL_ABSTRACT (decl))
9383         add_AT_string (die, DW_AT_MIPS_linkage_name,
9384                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
9385     }
9386
9387 #ifdef VMS_DEBUGGING_INFO
9388   /* Get the function's name, as described by its RTL.  This may be different
9389      from the DECL_NAME name used in the source file.  */
9390   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
9391     {
9392       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
9393                    XEXP (DECL_RTL (decl), 0));
9394       VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
9395     }
9396 #endif
9397 }
9398
9399 /* Push a new declaration scope.  */
9400
9401 static void
9402 push_decl_scope (scope)
9403      tree scope;
9404 {
9405   VARRAY_PUSH_TREE (decl_scope_table, scope);
9406 }
9407
9408 /* Pop a declaration scope.  */
9409
9410 static inline void
9411 pop_decl_scope ()
9412 {
9413   if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
9414     abort ();
9415
9416   VARRAY_POP (decl_scope_table);
9417 }
9418
9419 /* Return the DIE for the scope that immediately contains this type.
9420    Non-named types get global scope.  Named types nested in other
9421    types get their containing scope if it's open, or global scope
9422    otherwise.  All other types (i.e. function-local named types) get
9423    the current active scope.  */
9424
9425 static dw_die_ref
9426 scope_die_for (t, context_die)
9427      tree t;
9428      dw_die_ref context_die;
9429 {
9430   dw_die_ref scope_die = NULL;
9431   tree containing_scope;
9432   int i;
9433
9434   /* Non-types always go in the current scope.  */
9435   if (! TYPE_P (t))
9436     abort ();
9437
9438   containing_scope = TYPE_CONTEXT (t);
9439
9440   /* Ignore namespaces for the moment.  */
9441   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
9442     containing_scope = NULL_TREE;
9443
9444   /* Ignore function type "scopes" from the C frontend.  They mean that
9445      a tagged type is local to a parmlist of a function declarator, but
9446      that isn't useful to DWARF.  */
9447   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
9448     containing_scope = NULL_TREE;
9449
9450   if (containing_scope == NULL_TREE)
9451     scope_die = comp_unit_die;
9452   else if (TYPE_P (containing_scope))
9453     {
9454       /* For types, we can just look up the appropriate DIE.  But
9455          first we check to see if we're in the middle of emitting it
9456          so we know where the new DIE should go.  */
9457       for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
9458         if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
9459           break;
9460
9461       if (i < 0)
9462         {
9463           if (debug_info_level > DINFO_LEVEL_TERSE
9464               && !TREE_ASM_WRITTEN (containing_scope))
9465             abort ();
9466
9467           /* If none of the current dies are suitable, we get file scope.  */
9468           scope_die = comp_unit_die;
9469         }
9470       else
9471         scope_die = lookup_type_die (containing_scope);
9472     }
9473   else
9474     scope_die = context_die;
9475
9476   return scope_die;
9477 }
9478
9479 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
9480
9481 static inline int
9482 local_scope_p (context_die)
9483      dw_die_ref context_die;
9484 {
9485   for (; context_die; context_die = context_die->die_parent)
9486     if (context_die->die_tag == DW_TAG_inlined_subroutine
9487         || context_die->die_tag == DW_TAG_subprogram)
9488       return 1;
9489
9490   return 0;
9491 }
9492
9493 /* Returns nonzero if CONTEXT_DIE is a class.  */
9494
9495 static inline int
9496 class_scope_p (context_die)
9497      dw_die_ref context_die;
9498 {
9499   return (context_die
9500           && (context_die->die_tag == DW_TAG_structure_type
9501               || context_die->die_tag == DW_TAG_union_type));
9502 }
9503
9504 /* Many forms of DIEs require a "type description" attribute.  This
9505    routine locates the proper "type descriptor" die for the type given
9506    by 'type', and adds an DW_AT_type attribute below the given die.  */
9507
9508 static void
9509 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
9510      dw_die_ref object_die;
9511      tree type;
9512      int decl_const;
9513      int decl_volatile;
9514      dw_die_ref context_die;
9515 {
9516   enum tree_code code  = TREE_CODE (type);
9517   dw_die_ref type_die  = NULL;
9518
9519   /* ??? If this type is an unnamed subrange type of an integral or
9520      floating-point type, use the inner type.  This is because we have no
9521      support for unnamed types in base_type_die.  This can happen if this is
9522      an Ada subrange type.  Correct solution is emit a subrange type die.  */
9523   if ((code == INTEGER_TYPE || code == REAL_TYPE)
9524       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
9525     type = TREE_TYPE (type), code = TREE_CODE (type);
9526
9527   if (code == ERROR_MARK
9528       /* Handle a special case.  For functions whose return type is void, we
9529          generate *no* type attribute.  (Note that no object may have type
9530          `void', so this only applies to function return types).  */
9531       || code == VOID_TYPE)
9532     return;
9533
9534   type_die = modified_type_die (type,
9535                                 decl_const || TYPE_READONLY (type),
9536                                 decl_volatile || TYPE_VOLATILE (type),
9537                                 context_die);
9538
9539   if (type_die != NULL)
9540     add_AT_die_ref (object_die, DW_AT_type, type_die);
9541 }
9542
9543 /* Given a tree pointer to a struct, class, union, or enum type node, return
9544    a pointer to the (string) tag name for the given type, or zero if the type
9545    was declared without a tag.  */
9546
9547 static const char *
9548 type_tag (type)
9549      tree type;
9550 {
9551   const char *name = 0;
9552
9553   if (TYPE_NAME (type) != 0)
9554     {
9555       tree t = 0;
9556
9557       /* Find the IDENTIFIER_NODE for the type name.  */
9558       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9559         t = TYPE_NAME (type);
9560
9561       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
9562          a TYPE_DECL node, regardless of whether or not a `typedef' was
9563          involved.  */
9564       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9565                && ! DECL_IGNORED_P (TYPE_NAME (type)))
9566         t = DECL_NAME (TYPE_NAME (type));
9567
9568       /* Now get the name as a string, or invent one.  */
9569       if (t != 0)
9570         name = IDENTIFIER_POINTER (t);
9571     }
9572
9573   return (name == 0 || *name == '\0') ? 0 : name;
9574 }
9575
9576 /* Return the type associated with a data member, make a special check
9577    for bit field types.  */
9578
9579 static inline tree
9580 member_declared_type (member)
9581      tree member;
9582 {
9583   return (DECL_BIT_FIELD_TYPE (member)
9584           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
9585 }
9586
9587 /* Get the decl's label, as described by its RTL. This may be different
9588    from the DECL_NAME name used in the source file.  */
9589
9590 #if 0
9591 static const char *
9592 decl_start_label (decl)
9593      tree decl;
9594 {
9595   rtx x;
9596   const char *fnname;
9597
9598   x = DECL_RTL (decl);
9599   if (GET_CODE (x) != MEM)
9600     abort ();
9601
9602   x = XEXP (x, 0);
9603   if (GET_CODE (x) != SYMBOL_REF)
9604     abort ();
9605
9606   fnname = XSTR (x, 0);
9607   return fnname;
9608 }
9609 #endif
9610 \f
9611 /* These routines generate the internal representation of the DIE's for
9612    the compilation unit.  Debugging information is collected by walking
9613    the declaration trees passed in from dwarf2out_decl().  */
9614
9615 static void
9616 gen_array_type_die (type, context_die)
9617      tree type;
9618      dw_die_ref context_die;
9619 {
9620   dw_die_ref scope_die = scope_die_for (type, context_die);
9621   dw_die_ref array_die;
9622   tree element_type;
9623
9624   /* ??? The SGI dwarf reader fails for array of array of enum types unless
9625      the inner array type comes before the outer array type.  Thus we must
9626      call gen_type_die before we call new_die.  See below also.  */
9627 #ifdef MIPS_DEBUGGING_INFO
9628   gen_type_die (TREE_TYPE (type), context_die);
9629 #endif
9630
9631   array_die = new_die (DW_TAG_array_type, scope_die, type);
9632
9633 #if 0
9634   /* We default the array ordering.  SDB will probably do
9635      the right things even if DW_AT_ordering is not present.  It's not even
9636      an issue until we start to get into multidimensional arrays anyway.  If
9637      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
9638      then we'll have to put the DW_AT_ordering attribute back in.  (But if
9639      and when we find out that we need to put these in, we will only do so
9640      for multidimensional arrays.  */
9641   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
9642 #endif
9643
9644 #ifdef MIPS_DEBUGGING_INFO
9645   /* The SGI compilers handle arrays of unknown bound by setting
9646      AT_declaration and not emitting any subrange DIEs.  */
9647   if (! TYPE_DOMAIN (type))
9648     add_AT_unsigned (array_die, DW_AT_declaration, 1);
9649   else
9650 #endif
9651     add_subscript_info (array_die, type);
9652
9653   add_name_attribute (array_die, type_tag (type));
9654   equate_type_number_to_die (type, array_die);
9655
9656   /* Add representation of the type of the elements of this array type.  */
9657   element_type = TREE_TYPE (type);
9658
9659   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9660      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
9661      We work around this by disabling this feature.  See also
9662      add_subscript_info.  */
9663 #ifndef MIPS_DEBUGGING_INFO
9664   while (TREE_CODE (element_type) == ARRAY_TYPE)
9665     element_type = TREE_TYPE (element_type);
9666
9667   gen_type_die (element_type, context_die);
9668 #endif
9669
9670   add_type_attribute (array_die, element_type, 0, 0, context_die);
9671 }
9672
9673 static void
9674 gen_set_type_die (type, context_die)
9675      tree type;
9676      dw_die_ref context_die;
9677 {
9678   dw_die_ref type_die
9679     = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
9680
9681   equate_type_number_to_die (type, type_die);
9682   add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
9683 }
9684
9685 #if 0
9686 static void
9687 gen_entry_point_die (decl, context_die)
9688      tree decl;
9689      dw_die_ref context_die;
9690 {
9691   tree origin = decl_ultimate_origin (decl);
9692   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
9693
9694   if (origin != NULL)
9695     add_abstract_origin_attribute (decl_die, origin);
9696   else
9697     {
9698       add_name_and_src_coords_attributes (decl_die, decl);
9699       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
9700                           0, 0, context_die);
9701     }
9702
9703   if (DECL_ABSTRACT (decl))
9704     equate_decl_number_to_die (decl, decl_die);
9705   else
9706     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
9707 }
9708 #endif
9709
9710 /* Walk through the list of incomplete types again, trying once more to
9711    emit full debugging info for them.  */
9712
9713 static void
9714 retry_incomplete_types ()
9715 {
9716   int i;
9717
9718   for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
9719     gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
9720 }
9721
9722 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
9723
9724 static void
9725 gen_inlined_enumeration_type_die (type, context_die)
9726      tree type;
9727      dw_die_ref context_die;
9728 {
9729   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
9730
9731   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9732      be incomplete and such types are not marked.  */
9733   add_abstract_origin_attribute (type_die, type);
9734 }
9735
9736 /* Generate a DIE to represent an inlined instance of a structure type.  */
9737
9738 static void
9739 gen_inlined_structure_type_die (type, context_die)
9740      tree type;
9741      dw_die_ref context_die;
9742 {
9743   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
9744
9745   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9746      be incomplete and such types are not marked.  */
9747   add_abstract_origin_attribute (type_die, type);
9748 }
9749
9750 /* Generate a DIE to represent an inlined instance of a union type.  */
9751
9752 static void
9753 gen_inlined_union_type_die (type, context_die)
9754      tree type;
9755      dw_die_ref context_die;
9756 {
9757   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
9758
9759   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9760      be incomplete and such types are not marked.  */
9761   add_abstract_origin_attribute (type_die, type);
9762 }
9763
9764 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
9765    include all of the information about the enumeration values also. Each
9766    enumerated type name/value is listed as a child of the enumerated type
9767    DIE.  */
9768
9769 static void
9770 gen_enumeration_type_die (type, context_die)
9771      tree type;
9772      dw_die_ref context_die;
9773 {
9774   dw_die_ref type_die = lookup_type_die (type);
9775
9776   if (type_die == NULL)
9777     {
9778       type_die = new_die (DW_TAG_enumeration_type,
9779                           scope_die_for (type, context_die), type);
9780       equate_type_number_to_die (type, type_die);
9781       add_name_attribute (type_die, type_tag (type));
9782     }
9783   else if (! TYPE_SIZE (type))
9784     return;
9785   else
9786     remove_AT (type_die, DW_AT_declaration);
9787
9788   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
9789      given enum type is incomplete, do not generate the DW_AT_byte_size
9790      attribute or the DW_AT_element_list attribute.  */
9791   if (TYPE_SIZE (type))
9792     {
9793       tree link;
9794
9795       TREE_ASM_WRITTEN (type) = 1;
9796       add_byte_size_attribute (type_die, type);
9797       if (TYPE_STUB_DECL (type) != NULL_TREE)
9798         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
9799
9800       /* If the first reference to this type was as the return type of an
9801          inline function, then it may not have a parent.  Fix this now.  */
9802       if (type_die->die_parent == NULL)
9803         add_child_die (scope_die_for (type, context_die), type_die);
9804
9805       for (link = TYPE_FIELDS (type);
9806            link != NULL; link = TREE_CHAIN (link))
9807         {
9808           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
9809
9810           add_name_attribute (enum_die,
9811                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
9812
9813           if (host_integerp (TREE_VALUE (link), 0))
9814             {
9815               if (tree_int_cst_sgn (TREE_VALUE (link)) < 0)
9816                 add_AT_int (enum_die, DW_AT_const_value,
9817                             tree_low_cst (TREE_VALUE (link), 0));
9818               else
9819                 add_AT_unsigned (enum_die, DW_AT_const_value,
9820                                  tree_low_cst (TREE_VALUE (link), 0));
9821             }
9822         }
9823     }
9824   else
9825     add_AT_flag (type_die, DW_AT_declaration, 1);
9826 }
9827
9828 /* Generate a DIE to represent either a real live formal parameter decl or to
9829    represent just the type of some formal parameter position in some function
9830    type.
9831
9832    Note that this routine is a bit unusual because its argument may be a
9833    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
9834    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
9835    node.  If it's the former then this function is being called to output a
9836    DIE to represent a formal parameter object (or some inlining thereof).  If
9837    it's the latter, then this function is only being called to output a
9838    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
9839    argument type of some subprogram type.  */
9840
9841 static dw_die_ref
9842 gen_formal_parameter_die (node, context_die)
9843      tree node;
9844      dw_die_ref context_die;
9845 {
9846   dw_die_ref parm_die
9847     = new_die (DW_TAG_formal_parameter, context_die, node);
9848   tree origin;
9849
9850   switch (TREE_CODE_CLASS (TREE_CODE (node)))
9851     {
9852     case 'd':
9853       origin = decl_ultimate_origin (node);
9854       if (origin != NULL)
9855         add_abstract_origin_attribute (parm_die, origin);
9856       else
9857         {
9858           add_name_and_src_coords_attributes (parm_die, node);
9859           add_type_attribute (parm_die, TREE_TYPE (node),
9860                               TREE_READONLY (node),
9861                               TREE_THIS_VOLATILE (node),
9862                               context_die);
9863           if (DECL_ARTIFICIAL (node))
9864             add_AT_flag (parm_die, DW_AT_artificial, 1);
9865         }
9866
9867       equate_decl_number_to_die (node, parm_die);
9868       if (! DECL_ABSTRACT (node))
9869         add_location_or_const_value_attribute (parm_die, node);
9870
9871       break;
9872
9873     case 't':
9874       /* We were called with some kind of a ..._TYPE node.  */
9875       add_type_attribute (parm_die, node, 0, 0, context_die);
9876       break;
9877
9878     default:
9879       abort ();
9880     }
9881
9882   return parm_die;
9883 }
9884
9885 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
9886    at the end of an (ANSI prototyped) formal parameters list.  */
9887
9888 static void
9889 gen_unspecified_parameters_die (decl_or_type, context_die)
9890      tree decl_or_type;
9891      dw_die_ref context_die;
9892 {
9893   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
9894 }
9895
9896 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
9897    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
9898    parameters as specified in some function type specification (except for
9899    those which appear as part of a function *definition*).  */
9900
9901 static void
9902 gen_formal_types_die (function_or_method_type, context_die)
9903      tree function_or_method_type;
9904      dw_die_ref context_die;
9905 {
9906   tree link;
9907   tree formal_type = NULL;
9908   tree first_parm_type;
9909   tree arg;
9910
9911   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
9912     {
9913       arg = DECL_ARGUMENTS (function_or_method_type);
9914       function_or_method_type = TREE_TYPE (function_or_method_type);
9915     }
9916   else
9917     arg = NULL_TREE;
9918   
9919   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
9920
9921   /* Make our first pass over the list of formal parameter types and output a
9922      DW_TAG_formal_parameter DIE for each one.  */
9923   for (link = first_parm_type; link; )
9924     {
9925       dw_die_ref parm_die;
9926
9927       formal_type = TREE_VALUE (link);
9928       if (formal_type == void_type_node)
9929         break;
9930
9931       /* Output a (nameless) DIE to represent the formal parameter itself.  */
9932       parm_die = gen_formal_parameter_die (formal_type, context_die);
9933       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
9934            && link == first_parm_type)
9935           || (arg && DECL_ARTIFICIAL (arg)))
9936         add_AT_flag (parm_die, DW_AT_artificial, 1);
9937
9938       link = TREE_CHAIN (link);
9939       if (arg)
9940         arg = TREE_CHAIN (arg);
9941     }
9942
9943   /* If this function type has an ellipsis, add a
9944      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
9945   if (formal_type != void_type_node)
9946     gen_unspecified_parameters_die (function_or_method_type, context_die);
9947
9948   /* Make our second (and final) pass over the list of formal parameter types
9949      and output DIEs to represent those types (as necessary).  */
9950   for (link = TYPE_ARG_TYPES (function_or_method_type);
9951        link && TREE_VALUE (link);
9952        link = TREE_CHAIN (link))
9953     gen_type_die (TREE_VALUE (link), context_die);
9954 }
9955
9956 /* We want to generate the DIE for TYPE so that we can generate the
9957    die for MEMBER, which has been defined; we will need to refer back
9958    to the member declaration nested within TYPE.  If we're trying to
9959    generate minimal debug info for TYPE, processing TYPE won't do the
9960    trick; we need to attach the member declaration by hand.  */
9961
9962 static void
9963 gen_type_die_for_member (type, member, context_die)
9964      tree type, member;
9965      dw_die_ref context_die;
9966 {
9967   gen_type_die (type, context_die);
9968
9969   /* If we're trying to avoid duplicate debug info, we may not have
9970      emitted the member decl for this function.  Emit it now.  */
9971   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
9972       && ! lookup_decl_die (member))
9973     {
9974       if (decl_ultimate_origin (member))
9975         abort ();
9976
9977       push_decl_scope (type);
9978       if (TREE_CODE (member) == FUNCTION_DECL)
9979         gen_subprogram_die (member, lookup_type_die (type));
9980       else
9981         gen_variable_die (member, lookup_type_die (type));
9982
9983       pop_decl_scope ();
9984     }
9985 }
9986
9987 /* Generate the DWARF2 info for the "abstract" instance of a function which we
9988    may later generate inlined and/or out-of-line instances of.  */
9989
9990 static void
9991 dwarf2out_abstract_function (decl)
9992      tree decl;
9993 {
9994   dw_die_ref old_die;
9995   tree save_fn;
9996   tree context;
9997   int was_abstract = DECL_ABSTRACT (decl);
9998
9999   /* Make sure we have the actual abstract inline, not a clone.  */
10000   decl = DECL_ORIGIN (decl);
10001
10002   old_die = lookup_decl_die (decl);  
10003   if (old_die && get_AT_unsigned (old_die, DW_AT_inline))
10004     /* We've already generated the abstract instance.  */
10005     return;
10006
10007   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
10008      we don't get confused by DECL_ABSTRACT.  */
10009   if (debug_info_level > DINFO_LEVEL_TERSE)
10010     {
10011       context = decl_class_context (decl);
10012       if (context)
10013         gen_type_die_for_member
10014           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
10015     }
10016  
10017   /* Pretend we've just finished compiling this function.  */
10018   save_fn = current_function_decl;
10019   current_function_decl = decl;
10020
10021   set_decl_abstract_flags (decl, 1);
10022   dwarf2out_decl (decl);
10023   if (! was_abstract)
10024     set_decl_abstract_flags (decl, 0);
10025
10026   current_function_decl = save_fn;
10027 }
10028
10029 /* Generate a DIE to represent a declared function (either file-scope or
10030    block-local).  */
10031
10032 static void
10033 gen_subprogram_die (decl, context_die)
10034      tree decl;
10035      dw_die_ref context_die;
10036 {
10037   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10038   tree origin = decl_ultimate_origin (decl);
10039   dw_die_ref subr_die;
10040   rtx fp_reg;
10041   tree fn_arg_types;
10042   tree outer_scope;
10043   dw_die_ref old_die = lookup_decl_die (decl);
10044   int declaration = (current_function_decl != decl
10045                      || class_scope_p (context_die));
10046
10047   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
10048      started to generate the abstract instance of an inline, decided to output
10049      its containing class, and proceeded to emit the declaration of the inline
10050      from the member list for the class.  If so, DECLARATION takes priority;
10051      we'll get back to the abstract instance when done with the class.  */
10052
10053   /* The class-scope declaration DIE must be the primary DIE.  */
10054   if (origin && declaration && class_scope_p (context_die))
10055     {
10056       origin = NULL;
10057       if (old_die)
10058         abort ();
10059     }
10060
10061   if (origin != NULL)
10062     {
10063       if (declaration && ! local_scope_p (context_die))
10064         abort ();
10065
10066       /* Fixup die_parent for the abstract instance of a nested
10067          inline function.  */
10068       if (old_die && old_die->die_parent == NULL)
10069         add_child_die (context_die, old_die);
10070
10071       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10072       add_abstract_origin_attribute (subr_die, origin);
10073     }
10074   else if (old_die)
10075     {
10076       unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10077
10078       if (!get_AT_flag (old_die, DW_AT_declaration)
10079           /* We can have a normal definition following an inline one in the
10080              case of redefinition of GNU C extern inlines.
10081              It seems reasonable to use AT_specification in this case.  */
10082           && !get_AT_unsigned (old_die, DW_AT_inline))
10083         {
10084           /* ??? This can happen if there is a bug in the program, for
10085              instance, if it has duplicate function definitions.  Ideally,
10086              we should detect this case and ignore it.  For now, if we have
10087              already reported an error, any error at all, then assume that
10088              we got here because of an input error, not a dwarf2 bug.  */
10089           if (errorcount)
10090             return;
10091           abort ();
10092         }
10093
10094       /* If the definition comes from the same place as the declaration,
10095          maybe use the old DIE.  We always want the DIE for this function
10096          that has the *_pc attributes to be under comp_unit_die so the
10097          debugger can find it.  We also need to do this for abstract
10098          instances of inlines, since the spec requires the out-of-line copy
10099          to have the same parent.  For local class methods, this doesn't
10100          apply; we just use the old DIE.  */
10101       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
10102           && (DECL_ARTIFICIAL (decl)
10103               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
10104                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
10105                       == (unsigned) DECL_SOURCE_LINE (decl)))))
10106         {
10107           subr_die = old_die;
10108
10109           /* Clear out the declaration attribute and the parm types.  */
10110           remove_AT (subr_die, DW_AT_declaration);
10111           remove_children (subr_die);
10112         }
10113       else
10114         {
10115           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10116           add_AT_die_ref (subr_die, DW_AT_specification, old_die);
10117           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10118             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
10119           if (get_AT_unsigned (old_die, DW_AT_decl_line)
10120               != (unsigned) DECL_SOURCE_LINE (decl))
10121             add_AT_unsigned
10122               (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
10123         }
10124     }
10125   else
10126     {
10127       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10128
10129       if (TREE_PUBLIC (decl))
10130         add_AT_flag (subr_die, DW_AT_external, 1);
10131
10132       add_name_and_src_coords_attributes (subr_die, decl);
10133       if (debug_info_level > DINFO_LEVEL_TERSE)
10134         {
10135           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
10136           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
10137                               0, 0, context_die);
10138         }
10139
10140       add_pure_or_virtual_attribute (subr_die, decl);
10141       if (DECL_ARTIFICIAL (decl))
10142         add_AT_flag (subr_die, DW_AT_artificial, 1);
10143
10144       if (TREE_PROTECTED (decl))
10145         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
10146       else if (TREE_PRIVATE (decl))
10147         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
10148     }
10149
10150   if (declaration)
10151     {
10152       if (!old_die || !get_AT_unsigned (old_die, DW_AT_inline))
10153         {
10154           add_AT_flag (subr_die, DW_AT_declaration, 1);
10155
10156           /* The first time we see a member function, it is in the context of
10157              the class to which it belongs.  We make sure of this by emitting
10158              the class first.  The next time is the definition, which is
10159              handled above.  The two may come from the same source text.  */
10160           if (DECL_CONTEXT (decl) || DECL_ABSTRACT (decl))
10161             equate_decl_number_to_die (decl, subr_die);
10162         }
10163     }
10164   else if (DECL_ABSTRACT (decl))
10165     {
10166       if (DECL_INLINE (decl) && !flag_no_inline)
10167         {
10168           /* ??? Checking DECL_DEFER_OUTPUT is correct for static
10169              inline functions, but not for extern inline functions.
10170              We can't get this completely correct because information
10171              about whether the function was declared inline is not
10172              saved anywhere.  */
10173           if (DECL_DEFER_OUTPUT (decl))
10174             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
10175           else
10176             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
10177         }
10178       else
10179         add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
10180
10181       equate_decl_number_to_die (decl, subr_die);
10182     }
10183   else if (!DECL_EXTERNAL (decl))
10184     {
10185       if (!old_die || !get_AT_unsigned (old_die, DW_AT_inline))
10186         equate_decl_number_to_die (decl, subr_die);
10187
10188       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
10189                                    current_funcdef_number);
10190       add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
10191       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10192                                    current_funcdef_number);
10193       add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
10194
10195       add_pubname (decl, subr_die);
10196       add_arange (decl, subr_die);
10197
10198 #ifdef MIPS_DEBUGGING_INFO
10199       /* Add a reference to the FDE for this routine.  */
10200       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
10201 #endif
10202
10203       /* Define the "frame base" location for this routine.  We use the
10204          frame pointer or stack pointer registers, since the RTL for local
10205          variables is relative to one of them.  */
10206       fp_reg
10207         = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
10208       add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
10209
10210 #if 0
10211       /* ??? This fails for nested inline functions, because context_display
10212          is not part of the state saved/restored for inline functions.  */
10213       if (current_function_needs_context)
10214         add_AT_location_description (subr_die, DW_AT_static_link,
10215                                      lookup_static_chain (decl));
10216 #endif
10217     }
10218
10219   /* Now output descriptions of the arguments for this function. This gets
10220      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
10221      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
10222      `...' at the end of the formal parameter list.  In order to find out if
10223      there was a trailing ellipsis or not, we must instead look at the type
10224      associated with the FUNCTION_DECL.  This will be a node of type
10225      FUNCTION_TYPE. If the chain of type nodes hanging off of this
10226      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
10227      an ellipsis at the end.  */
10228
10229   /* In the case where we are describing a mere function declaration, all we
10230      need to do here (and all we *can* do here) is to describe the *types* of
10231      its formal parameters.  */
10232   if (debug_info_level <= DINFO_LEVEL_TERSE)
10233     ;
10234   else if (declaration)
10235     gen_formal_types_die (decl, subr_die);
10236   else
10237     {
10238       /* Generate DIEs to represent all known formal parameters */
10239       tree arg_decls = DECL_ARGUMENTS (decl);
10240       tree parm;
10241
10242       /* When generating DIEs, generate the unspecified_parameters DIE
10243          instead if we come across the arg "__builtin_va_alist" */
10244       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
10245         if (TREE_CODE (parm) == PARM_DECL)
10246           {
10247             if (DECL_NAME (parm)
10248                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
10249                             "__builtin_va_alist"))
10250               gen_unspecified_parameters_die (parm, subr_die);
10251             else
10252               gen_decl_die (parm, subr_die);
10253           }
10254
10255       /* Decide whether we need an unspecified_parameters DIE at the end.
10256          There are 2 more cases to do this for: 1) the ansi ... declaration -
10257          this is detectable when the end of the arg list is not a
10258          void_type_node 2) an unprototyped function declaration (not a
10259          definition).  This just means that we have no info about the
10260          parameters at all.  */
10261       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
10262       if (fn_arg_types != NULL)
10263         {
10264           /* this is the prototyped case, check for ...  */
10265           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
10266             gen_unspecified_parameters_die (decl, subr_die);
10267         }
10268       else if (DECL_INITIAL (decl) == NULL_TREE)
10269         gen_unspecified_parameters_die (decl, subr_die);
10270     }
10271
10272   /* Output Dwarf info for all of the stuff within the body of the function
10273      (if it has one - it may be just a declaration).  */
10274   outer_scope = DECL_INITIAL (decl);
10275
10276   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
10277      a function.  This BLOCK actually represents the outermost binding contour
10278      for the function, i.e. the contour in which the function's formal
10279      parameters and labels get declared. Curiously, it appears that the front
10280      end doesn't actually put the PARM_DECL nodes for the current function onto
10281      the BLOCK_VARS list for this outer scope, but are strung off of the
10282      DECL_ARGUMENTS list for the function instead.
10283
10284      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
10285      the LABEL_DECL nodes for the function however, and we output DWARF info
10286      for those in decls_for_scope.  Just within the `outer_scope' there will be
10287      a BLOCK node representing the function's outermost pair of curly braces,
10288      and any blocks used for the base and member initializers of a C++
10289      constructor function.  */
10290   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
10291     {
10292       current_function_has_inlines = 0;
10293       decls_for_scope (outer_scope, subr_die, 0);
10294
10295 #if 0 && defined (MIPS_DEBUGGING_INFO)
10296       if (current_function_has_inlines)
10297         {
10298           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
10299           if (! comp_unit_has_inlines)
10300             {
10301               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
10302               comp_unit_has_inlines = 1;
10303             }
10304         }
10305 #endif
10306     }
10307 }
10308
10309 /* Generate a DIE to represent a declared data object.  */
10310
10311 static void
10312 gen_variable_die (decl, context_die)
10313      tree decl;
10314      dw_die_ref context_die;
10315 {
10316   tree origin = decl_ultimate_origin (decl);
10317   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
10318
10319   dw_die_ref old_die = lookup_decl_die (decl);
10320   int declaration = (DECL_EXTERNAL (decl)
10321                      || class_scope_p (context_die));
10322
10323   if (origin != NULL)
10324     add_abstract_origin_attribute (var_die, origin);
10325
10326   /* Loop unrolling can create multiple blocks that refer to the same
10327      static variable, so we must test for the DW_AT_declaration flag.
10328
10329      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
10330      copy decls and set the DECL_ABSTRACT flag on them instead of
10331      sharing them.
10332
10333      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
10334   else if (old_die && TREE_STATIC (decl)
10335            && get_AT_flag (old_die, DW_AT_declaration) == 1)
10336     {
10337       /* This is a definition of a C++ class level static.  */
10338       add_AT_die_ref (var_die, DW_AT_specification, old_die);
10339       if (DECL_NAME (decl))
10340         {
10341           unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10342
10343           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10344             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
10345
10346           if (get_AT_unsigned (old_die, DW_AT_decl_line)
10347               != (unsigned) DECL_SOURCE_LINE (decl))
10348
10349             add_AT_unsigned (var_die, DW_AT_decl_line,
10350                              DECL_SOURCE_LINE (decl));
10351         }
10352     }
10353   else
10354     {
10355       add_name_and_src_coords_attributes (var_die, decl);
10356       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
10357                           TREE_THIS_VOLATILE (decl), context_die);
10358
10359       if (TREE_PUBLIC (decl))
10360         add_AT_flag (var_die, DW_AT_external, 1);
10361
10362       if (DECL_ARTIFICIAL (decl))
10363         add_AT_flag (var_die, DW_AT_artificial, 1);
10364
10365       if (TREE_PROTECTED (decl))
10366         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
10367       else if (TREE_PRIVATE (decl))
10368         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
10369     }
10370
10371   if (declaration)
10372     add_AT_flag (var_die, DW_AT_declaration, 1);
10373
10374   if (class_scope_p (context_die) || DECL_ABSTRACT (decl))
10375     equate_decl_number_to_die (decl, var_die);
10376
10377   if (! declaration && ! DECL_ABSTRACT (decl))
10378     {
10379       add_location_or_const_value_attribute (var_die, decl);
10380       add_pubname (decl, var_die);
10381     }
10382   else
10383     tree_add_const_value_attribute (var_die, decl);
10384 }
10385
10386 /* Generate a DIE to represent a label identifier.  */
10387
10388 static void
10389 gen_label_die (decl, context_die)
10390      tree decl;
10391      dw_die_ref context_die;
10392 {
10393   tree origin = decl_ultimate_origin (decl);
10394   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
10395   rtx insn;
10396   char label[MAX_ARTIFICIAL_LABEL_BYTES];
10397
10398   if (origin != NULL)
10399     add_abstract_origin_attribute (lbl_die, origin);
10400   else
10401     add_name_and_src_coords_attributes (lbl_die, decl);
10402
10403   if (DECL_ABSTRACT (decl))
10404     equate_decl_number_to_die (decl, lbl_die);
10405   else
10406     {
10407       insn = DECL_RTL (decl);
10408
10409       /* Deleted labels are programmer specified labels which have been
10410          eliminated because of various optimisations.  We still emit them
10411          here so that it is possible to put breakpoints on them.  */
10412       if (GET_CODE (insn) == CODE_LABEL
10413           || ((GET_CODE (insn) == NOTE
10414                && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
10415         {
10416           /* When optimization is enabled (via -O) some parts of the compiler
10417              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
10418              represent source-level labels which were explicitly declared by
10419              the user.  This really shouldn't be happening though, so catch
10420              it if it ever does happen.  */
10421           if (INSN_DELETED_P (insn))
10422             abort ();
10423
10424           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
10425           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
10426         }
10427     }
10428 }
10429
10430 /* Generate a DIE for a lexical block.  */
10431
10432 static void
10433 gen_lexical_block_die (stmt, context_die, depth)
10434      tree stmt;
10435      dw_die_ref context_die;
10436      int depth;
10437 {
10438   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
10439   char label[MAX_ARTIFICIAL_LABEL_BYTES];
10440
10441   if (! BLOCK_ABSTRACT (stmt))
10442     {
10443       if (BLOCK_FRAGMENT_CHAIN (stmt))
10444         {
10445           tree chain;
10446
10447           add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
10448
10449           chain = BLOCK_FRAGMENT_CHAIN (stmt);
10450           do
10451             {
10452               add_ranges (chain);
10453               chain = BLOCK_FRAGMENT_CHAIN (chain);
10454             }
10455           while (chain);
10456           add_ranges (NULL);
10457         }
10458       else
10459         {
10460           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10461                                        BLOCK_NUMBER (stmt));
10462           add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
10463           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10464                                        BLOCK_NUMBER (stmt));
10465           add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
10466         }
10467     }
10468
10469   decls_for_scope (stmt, stmt_die, depth);
10470 }
10471
10472 /* Generate a DIE for an inlined subprogram.  */
10473
10474 static void
10475 gen_inlined_subroutine_die (stmt, context_die, depth)
10476      tree stmt;
10477      dw_die_ref context_die;
10478      int depth;
10479 {
10480   if (! BLOCK_ABSTRACT (stmt))
10481     {
10482       dw_die_ref subr_die
10483         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
10484       tree decl = block_ultimate_origin (stmt);
10485       char label[MAX_ARTIFICIAL_LABEL_BYTES];
10486
10487       /* Emit info for the abstract instance first, if we haven't yet.  */
10488       dwarf2out_abstract_function (decl);
10489
10490       add_abstract_origin_attribute (subr_die, decl);
10491       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10492                                    BLOCK_NUMBER (stmt));
10493       add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
10494       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10495                                    BLOCK_NUMBER (stmt));
10496       add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
10497       decls_for_scope (stmt, subr_die, depth);
10498       current_function_has_inlines = 1;
10499     }
10500 }
10501
10502 /* Generate a DIE for a field in a record, or structure.  */
10503
10504 static void
10505 gen_field_die (decl, context_die)
10506      tree decl;
10507      dw_die_ref context_die;
10508 {
10509   dw_die_ref decl_die = new_die (DW_TAG_member, context_die, decl);
10510
10511   add_name_and_src_coords_attributes (decl_die, decl);
10512   add_type_attribute (decl_die, member_declared_type (decl),
10513                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
10514                       context_die);
10515
10516   if (DECL_BIT_FIELD_TYPE (decl))
10517     {
10518       add_byte_size_attribute (decl_die, decl);
10519       add_bit_size_attribute (decl_die, decl);
10520       add_bit_offset_attribute (decl_die, decl);
10521     }
10522
10523   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
10524     add_data_member_location_attribute (decl_die, decl);
10525
10526   if (DECL_ARTIFICIAL (decl))
10527     add_AT_flag (decl_die, DW_AT_artificial, 1);
10528
10529   if (TREE_PROTECTED (decl))
10530     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
10531   else if (TREE_PRIVATE (decl))
10532     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
10533 }
10534
10535 #if 0
10536 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10537    Use modified_type_die instead.
10538    We keep this code here just in case these types of DIEs may be needed to
10539    represent certain things in other languages (e.g. Pascal) someday.  */
10540
10541 static void
10542 gen_pointer_type_die (type, context_die)
10543      tree type;
10544      dw_die_ref context_die;
10545 {
10546   dw_die_ref ptr_die
10547     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
10548
10549   equate_type_number_to_die (type, ptr_die);
10550   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10551   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10552 }
10553
10554 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10555    Use modified_type_die instead.
10556    We keep this code here just in case these types of DIEs may be needed to
10557    represent certain things in other languages (e.g. Pascal) someday.  */
10558
10559 static void
10560 gen_reference_type_die (type, context_die)
10561      tree type;
10562      dw_die_ref context_die;
10563 {
10564   dw_die_ref ref_die
10565     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
10566
10567   equate_type_number_to_die (type, ref_die);
10568   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
10569   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10570 }
10571 #endif
10572
10573 /* Generate a DIE for a pointer to a member type.  */
10574
10575 static void
10576 gen_ptr_to_mbr_type_die (type, context_die)
10577      tree type;
10578      dw_die_ref context_die;
10579 {
10580   dw_die_ref ptr_die
10581     = new_die (DW_TAG_ptr_to_member_type,
10582                scope_die_for (type, context_die), type);
10583
10584   equate_type_number_to_die (type, ptr_die);
10585   add_AT_die_ref (ptr_die, DW_AT_containing_type,
10586                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
10587   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10588 }
10589
10590 /* Generate the DIE for the compilation unit.  */
10591
10592 static dw_die_ref
10593 gen_compile_unit_die (filename)
10594      const char *filename;
10595 {
10596   dw_die_ref die;
10597   char producer[250];
10598   const char *wd = getpwd ();
10599   const char *language_string = lang_hooks.name;
10600   int language;
10601
10602   die = new_die (DW_TAG_compile_unit, NULL, NULL);
10603   add_name_attribute (die, filename);
10604
10605   if (wd != NULL && filename[0] != DIR_SEPARATOR)
10606     add_AT_string (die, DW_AT_comp_dir, wd);
10607
10608   sprintf (producer, "%s %s", language_string, version_string);
10609
10610 #ifdef MIPS_DEBUGGING_INFO
10611   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
10612      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
10613      not appear in the producer string, the debugger reaches the conclusion
10614      that the object file is stripped and has no debugging information.
10615      To get the MIPS/SGI debugger to believe that there is debugging
10616      information in the object file, we add a -g to the producer string.  */
10617   if (debug_info_level > DINFO_LEVEL_TERSE)
10618     strcat (producer, " -g");
10619 #endif
10620
10621   add_AT_string (die, DW_AT_producer, producer);
10622
10623   if (strcmp (language_string, "GNU C++") == 0)
10624     language = DW_LANG_C_plus_plus;
10625   else if (strcmp (language_string, "GNU Ada") == 0)
10626     language = DW_LANG_Ada83;
10627   else if (strcmp (language_string, "GNU F77") == 0)
10628     language = DW_LANG_Fortran77;
10629   else if (strcmp (language_string, "GNU Pascal") == 0)
10630     language = DW_LANG_Pascal83;
10631   else if (strcmp (language_string, "GNU Java") == 0)
10632     language = DW_LANG_Java;
10633   else if (flag_traditional)
10634     language = DW_LANG_C;
10635   else
10636     language = DW_LANG_C89;
10637
10638   add_AT_unsigned (die, DW_AT_language, language);
10639   return die;
10640 }
10641
10642 /* Generate a DIE for a string type.  */
10643
10644 static void
10645 gen_string_type_die (type, context_die)
10646      tree type;
10647      dw_die_ref context_die;
10648 {
10649   dw_die_ref type_die
10650     = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
10651
10652   equate_type_number_to_die (type, type_die);
10653
10654   /* ??? Fudge the string length attribute for now.
10655      TODO: add string length info.  */
10656 #if 0
10657   string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
10658   bound_representation (upper_bound, 0, 'u');
10659 #endif
10660 }
10661
10662 /* Generate the DIE for a base class.  */
10663
10664 static void
10665 gen_inheritance_die (binfo, context_die)
10666      tree binfo;
10667      dw_die_ref context_die;
10668 {
10669   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
10670
10671   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
10672   add_data_member_location_attribute (die, binfo);
10673
10674   if (TREE_VIA_VIRTUAL (binfo))
10675     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10676
10677   if (TREE_VIA_PUBLIC (binfo))
10678     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
10679   else if (TREE_VIA_PROTECTED (binfo))
10680     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
10681 }
10682
10683 /* Generate a DIE for a class member.  */
10684
10685 static void
10686 gen_member_die (type, context_die)
10687      tree type;
10688      dw_die_ref context_die;
10689 {
10690   tree member;
10691   dw_die_ref child;
10692
10693   /* If this is not an incomplete type, output descriptions of each of its
10694      members. Note that as we output the DIEs necessary to represent the
10695      members of this record or union type, we will also be trying to output
10696      DIEs to represent the *types* of those members. However the `type'
10697      function (above) will specifically avoid generating type DIEs for member
10698      types *within* the list of member DIEs for this (containing) type except
10699      for those types (of members) which are explicitly marked as also being
10700      members of this (containing) type themselves.  The g++ front- end can
10701      force any given type to be treated as a member of some other (containing)
10702      type by setting the TYPE_CONTEXT of the given (member) type to point to
10703      the TREE node representing the appropriate (containing) type.  */
10704
10705   /* First output info about the base classes.  */
10706   if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
10707     {
10708       tree bases = TYPE_BINFO_BASETYPES (type);
10709       int n_bases = TREE_VEC_LENGTH (bases);
10710       int i;
10711
10712       for (i = 0; i < n_bases; i++)
10713         gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
10714     }
10715
10716   /* Now output info about the data members and type members.  */
10717   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
10718     {
10719       /* If we thought we were generating minimal debug info for TYPE
10720          and then changed our minds, some of the member declarations
10721          may have already been defined.  Don't define them again, but
10722          do put them in the right order.  */
10723
10724       child = lookup_decl_die (member);
10725       if (child)
10726         splice_child_die (context_die, child);
10727       else
10728         gen_decl_die (member, context_die);
10729     }
10730
10731   /* Now output info about the function members (if any).  */
10732   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
10733     {
10734       /* Don't include clones in the member list.  */
10735       if (DECL_ABSTRACT_ORIGIN (member))
10736         continue;
10737
10738       child = lookup_decl_die (member);
10739       if (child)
10740         splice_child_die (context_die, child);
10741       else
10742         gen_decl_die (member, context_die);
10743     }
10744 }
10745
10746 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
10747    is set, we pretend that the type was never defined, so we only get the
10748    member DIEs needed by later specification DIEs.  */
10749
10750 static void
10751 gen_struct_or_union_type_die (type, context_die)
10752      tree type;
10753      dw_die_ref context_die;
10754 {
10755   dw_die_ref type_die = lookup_type_die (type);
10756   dw_die_ref scope_die = 0;
10757   int nested = 0;
10758   int complete = (TYPE_SIZE (type)
10759                   && (! TYPE_STUB_DECL (type)
10760                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
10761
10762   if (type_die && ! complete)
10763     return;
10764
10765   if (TYPE_CONTEXT (type) != NULL_TREE
10766       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type)))
10767     nested = 1;
10768
10769   scope_die = scope_die_for (type, context_die);
10770
10771   if (! type_die || (nested && scope_die == comp_unit_die))
10772     /* First occurrence of type or toplevel definition of nested class.  */
10773     {
10774       dw_die_ref old_die = type_die;
10775
10776       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
10777                           ? DW_TAG_structure_type : DW_TAG_union_type,
10778                           scope_die, type);
10779       equate_type_number_to_die (type, type_die);
10780       if (old_die)
10781         add_AT_die_ref (type_die, DW_AT_specification, old_die);
10782       else
10783         add_name_attribute (type_die, type_tag (type));
10784     }
10785   else
10786     remove_AT (type_die, DW_AT_declaration);
10787
10788   /* If this type has been completed, then give it a byte_size attribute and
10789      then give a list of members.  */
10790   if (complete)
10791     {
10792       /* Prevent infinite recursion in cases where the type of some member of
10793          this type is expressed in terms of this type itself.  */
10794       TREE_ASM_WRITTEN (type) = 1;
10795       add_byte_size_attribute (type_die, type);
10796       if (TYPE_STUB_DECL (type) != NULL_TREE)
10797         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10798
10799       /* If the first reference to this type was as the return type of an
10800          inline function, then it may not have a parent.  Fix this now.  */
10801       if (type_die->die_parent == NULL)
10802         add_child_die (scope_die, type_die);
10803
10804       push_decl_scope (type);
10805       gen_member_die (type, type_die);
10806       pop_decl_scope ();
10807
10808       /* GNU extension: Record what type our vtable lives in.  */
10809       if (TYPE_VFIELD (type))
10810         {
10811           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
10812
10813           gen_type_die (vtype, context_die);
10814           add_AT_die_ref (type_die, DW_AT_containing_type,
10815                           lookup_type_die (vtype));
10816         }
10817     }
10818   else
10819     {
10820       add_AT_flag (type_die, DW_AT_declaration, 1);
10821
10822       /* We don't need to do this for function-local types.  */
10823       if (! decl_function_context (TYPE_STUB_DECL (type)))
10824         VARRAY_PUSH_TREE (incomplete_types, type);
10825     }
10826 }
10827
10828 /* Generate a DIE for a subroutine _type_.  */
10829
10830 static void
10831 gen_subroutine_type_die (type, context_die)
10832      tree type;
10833      dw_die_ref context_die;
10834 {
10835   tree return_type = TREE_TYPE (type);
10836   dw_die_ref subr_die
10837     = new_die (DW_TAG_subroutine_type,
10838                scope_die_for (type, context_die), type);
10839
10840   equate_type_number_to_die (type, subr_die);
10841   add_prototyped_attribute (subr_die, type);
10842   add_type_attribute (subr_die, return_type, 0, 0, context_die);
10843   gen_formal_types_die (type, subr_die);
10844 }
10845
10846 /* Generate a DIE for a type definition */
10847
10848 static void
10849 gen_typedef_die (decl, context_die)
10850      tree decl;
10851      dw_die_ref context_die;
10852 {
10853   dw_die_ref type_die;
10854   tree origin;
10855
10856   if (TREE_ASM_WRITTEN (decl))
10857     return;
10858
10859   TREE_ASM_WRITTEN (decl) = 1;
10860   type_die = new_die (DW_TAG_typedef, context_die, decl);
10861   origin = decl_ultimate_origin (decl);
10862   if (origin != NULL)
10863     add_abstract_origin_attribute (type_die, origin);
10864   else
10865     {
10866       tree type;
10867
10868       add_name_and_src_coords_attributes (type_die, decl);
10869       if (DECL_ORIGINAL_TYPE (decl))
10870         {
10871           type = DECL_ORIGINAL_TYPE (decl);
10872
10873           if (type == TREE_TYPE (decl))
10874             abort ();
10875           else
10876             equate_type_number_to_die (TREE_TYPE (decl), type_die);
10877         }
10878       else
10879         type = TREE_TYPE (decl);
10880
10881       add_type_attribute (type_die, type, TREE_READONLY (decl),
10882                           TREE_THIS_VOLATILE (decl), context_die);
10883     }
10884
10885   if (DECL_ABSTRACT (decl))
10886     equate_decl_number_to_die (decl, type_die);
10887 }
10888
10889 /* Generate a type description DIE.  */
10890
10891 static void
10892 gen_type_die (type, context_die)
10893      tree type;
10894      dw_die_ref context_die;
10895 {
10896   int need_pop;
10897
10898   if (type == NULL_TREE || type == error_mark_node)
10899     return;
10900
10901   /* We are going to output a DIE to represent the unqualified version of
10902      this type (i.e. without any const or volatile qualifiers) so get the
10903      main variant (i.e. the unqualified version) of this type now.  */
10904   type = type_main_variant (type);
10905
10906   if (TREE_ASM_WRITTEN (type))
10907     return;
10908
10909   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10910       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
10911     {
10912       TREE_ASM_WRITTEN (type) = 1;
10913       gen_decl_die (TYPE_NAME (type), context_die);
10914       return;
10915     }
10916
10917   switch (TREE_CODE (type))
10918     {
10919     case ERROR_MARK:
10920       break;
10921
10922     case POINTER_TYPE:
10923     case REFERENCE_TYPE:
10924       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
10925          ensures that the gen_type_die recursion will terminate even if the
10926          type is recursive.  Recursive types are possible in Ada.  */
10927       /* ??? We could perhaps do this for all types before the switch
10928          statement.  */
10929       TREE_ASM_WRITTEN (type) = 1;
10930
10931       /* For these types, all that is required is that we output a DIE (or a
10932          set of DIEs) to represent the "basis" type.  */
10933       gen_type_die (TREE_TYPE (type), context_die);
10934       break;
10935
10936     case OFFSET_TYPE:
10937       /* This code is used for C++ pointer-to-data-member types.
10938          Output a description of the relevant class type.  */
10939       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
10940
10941       /* Output a description of the type of the object pointed to.  */
10942       gen_type_die (TREE_TYPE (type), context_die);
10943
10944       /* Now output a DIE to represent this pointer-to-data-member type
10945          itself.  */
10946       gen_ptr_to_mbr_type_die (type, context_die);
10947       break;
10948
10949     case SET_TYPE:
10950       gen_type_die (TYPE_DOMAIN (type), context_die);
10951       gen_set_type_die (type, context_die);
10952       break;
10953
10954     case FILE_TYPE:
10955       gen_type_die (TREE_TYPE (type), context_die);
10956       abort ();                 /* No way to represent these in Dwarf yet!  */
10957       break;
10958
10959     case FUNCTION_TYPE:
10960       /* Force out return type (in case it wasn't forced out already).  */
10961       gen_type_die (TREE_TYPE (type), context_die);
10962       gen_subroutine_type_die (type, context_die);
10963       break;
10964
10965     case METHOD_TYPE:
10966       /* Force out return type (in case it wasn't forced out already).  */
10967       gen_type_die (TREE_TYPE (type), context_die);
10968       gen_subroutine_type_die (type, context_die);
10969       break;
10970
10971     case ARRAY_TYPE:
10972       if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
10973         {
10974           gen_type_die (TREE_TYPE (type), context_die);
10975           gen_string_type_die (type, context_die);
10976         }
10977       else
10978         gen_array_type_die (type, context_die);
10979       break;
10980
10981     case VECTOR_TYPE:
10982       gen_type_die (TYPE_DEBUG_REPRESENTATION_TYPE (type), context_die);
10983       break;
10984
10985     case ENUMERAL_TYPE:
10986     case RECORD_TYPE:
10987     case UNION_TYPE:
10988     case QUAL_UNION_TYPE:
10989       /* If this is a nested type whose containing class hasn't been written
10990          out yet, writing it out will cover this one, too.  This does not apply
10991          to instantiations of member class templates; they need to be added to
10992          the containing class as they are generated.  FIXME: This hurts the
10993          idea of combining type decls from multiple TUs, since we can't predict
10994          what set of template instantiations we'll get.  */
10995       if (TYPE_CONTEXT (type)
10996           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
10997           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
10998         {
10999           gen_type_die (TYPE_CONTEXT (type), context_die);
11000
11001           if (TREE_ASM_WRITTEN (type))
11002             return;
11003
11004           /* If that failed, attach ourselves to the stub.  */
11005           push_decl_scope (TYPE_CONTEXT (type));
11006           context_die = lookup_type_die (TYPE_CONTEXT (type));
11007           need_pop = 1;
11008         }
11009       else
11010         need_pop = 0;
11011
11012       if (TREE_CODE (type) == ENUMERAL_TYPE)
11013         gen_enumeration_type_die (type, context_die);
11014       else
11015         gen_struct_or_union_type_die (type, context_die);
11016
11017       if (need_pop)
11018         pop_decl_scope ();
11019
11020       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
11021          it up if it is ever completed.  gen_*_type_die will set it for us
11022          when appropriate.  */
11023       return;
11024
11025     case VOID_TYPE:
11026     case INTEGER_TYPE:
11027     case REAL_TYPE:
11028     case COMPLEX_TYPE:
11029     case BOOLEAN_TYPE:
11030     case CHAR_TYPE:
11031       /* No DIEs needed for fundamental types.  */
11032       break;
11033
11034     case LANG_TYPE:
11035       /* No Dwarf representation currently defined.  */
11036       break;
11037
11038     default:
11039       abort ();
11040     }
11041
11042   TREE_ASM_WRITTEN (type) = 1;
11043 }
11044
11045 /* Generate a DIE for a tagged type instantiation.  */
11046
11047 static void
11048 gen_tagged_type_instantiation_die (type, context_die)
11049      tree type;
11050      dw_die_ref context_die;
11051 {
11052   if (type == NULL_TREE || type == error_mark_node)
11053     return;
11054
11055   /* We are going to output a DIE to represent the unqualified version of
11056      this type (i.e. without any const or volatile qualifiers) so make sure
11057      that we have the main variant (i.e. the unqualified version) of this
11058      type now.  */
11059   if (type != type_main_variant (type))
11060     abort ();
11061
11062   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
11063      an instance of an unresolved type.  */
11064
11065   switch (TREE_CODE (type))
11066     {
11067     case ERROR_MARK:
11068       break;
11069
11070     case ENUMERAL_TYPE:
11071       gen_inlined_enumeration_type_die (type, context_die);
11072       break;
11073
11074     case RECORD_TYPE:
11075       gen_inlined_structure_type_die (type, context_die);
11076       break;
11077
11078     case UNION_TYPE:
11079     case QUAL_UNION_TYPE:
11080       gen_inlined_union_type_die (type, context_die);
11081       break;
11082
11083     default:
11084       abort ();
11085     }
11086 }
11087
11088 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
11089    things which are local to the given block.  */
11090
11091 static void
11092 gen_block_die (stmt, context_die, depth)
11093      tree stmt;
11094      dw_die_ref context_die;
11095      int depth;
11096 {
11097   int must_output_die = 0;
11098   tree origin;
11099   tree decl;
11100   enum tree_code origin_code;
11101
11102   /* Ignore blocks never really used to make RTL.  */
11103   if (stmt == NULL_TREE || !TREE_USED (stmt)
11104       || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
11105     return;
11106
11107   /* If the block is one fragment of a non-contiguous block, do not
11108      process the variables, since they will have been done by the
11109      origin block.  Do process subblocks.  */
11110   if (BLOCK_FRAGMENT_ORIGIN (stmt))
11111     {
11112       tree sub;
11113
11114       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
11115         gen_block_die (sub, context_die, depth + 1);
11116
11117       return;
11118     }
11119
11120   /* Determine the "ultimate origin" of this block.  This block may be an
11121      inlined instance of an inlined instance of inline function, so we have
11122      to trace all of the way back through the origin chain to find out what
11123      sort of node actually served as the original seed for the creation of
11124      the current block.  */
11125   origin = block_ultimate_origin (stmt);
11126   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
11127
11128   /* Determine if we need to output any Dwarf DIEs at all to represent this
11129      block.  */
11130   if (origin_code == FUNCTION_DECL)
11131     /* The outer scopes for inlinings *must* always be represented.  We
11132        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
11133     must_output_die = 1;
11134   else
11135     {
11136       /* In the case where the current block represents an inlining of the
11137          "body block" of an inline function, we must *NOT* output any DIE for
11138          this block because we have already output a DIE to represent the whole
11139          inlined function scope and the "body block" of any function doesn't
11140          really represent a different scope according to ANSI C rules.  So we
11141          check here to make sure that this block does not represent a "body
11142          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
11143       if (! is_body_block (origin ? origin : stmt))
11144         {
11145           /* Determine if this block directly contains any "significant"
11146              local declarations which we will need to output DIEs for.  */
11147           if (debug_info_level > DINFO_LEVEL_TERSE)
11148             /* We are not in terse mode so *any* local declaration counts
11149                as being a "significant" one.  */
11150             must_output_die = (BLOCK_VARS (stmt) != NULL);
11151           else
11152             /* We are in terse mode, so only local (nested) function
11153                definitions count as "significant" local declarations.  */
11154             for (decl = BLOCK_VARS (stmt);
11155                  decl != NULL; decl = TREE_CHAIN (decl))
11156               if (TREE_CODE (decl) == FUNCTION_DECL
11157                   && DECL_INITIAL (decl))
11158                 {
11159                   must_output_die = 1;
11160                   break;
11161                 }
11162         }
11163     }
11164
11165   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
11166      DIE for any block which contains no significant local declarations at
11167      all.  Rather, in such cases we just call `decls_for_scope' so that any
11168      needed Dwarf info for any sub-blocks will get properly generated. Note
11169      that in terse mode, our definition of what constitutes a "significant"
11170      local declaration gets restricted to include only inlined function
11171      instances and local (nested) function definitions.  */
11172   if (must_output_die)
11173     {
11174       if (origin_code == FUNCTION_DECL)
11175         gen_inlined_subroutine_die (stmt, context_die, depth);
11176       else
11177         gen_lexical_block_die (stmt, context_die, depth);
11178     }
11179   else
11180     decls_for_scope (stmt, context_die, depth);
11181 }
11182
11183 /* Generate all of the decls declared within a given scope and (recursively)
11184    all of its sub-blocks.  */
11185
11186 static void
11187 decls_for_scope (stmt, context_die, depth)
11188      tree stmt;
11189      dw_die_ref context_die;
11190      int depth;
11191 {
11192   tree decl;
11193   tree subblocks;
11194
11195   /* Ignore blocks never really used to make RTL.  */
11196   if (stmt == NULL_TREE || ! TREE_USED (stmt))
11197     return;
11198
11199   /* Output the DIEs to represent all of the data objects and typedefs
11200      declared directly within this block but not within any nested
11201      sub-blocks.  Also, nested function and tag DIEs have been
11202      generated with a parent of NULL; fix that up now.  */
11203   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
11204     {
11205       dw_die_ref die;
11206
11207       if (TREE_CODE (decl) == FUNCTION_DECL)
11208         die = lookup_decl_die (decl);
11209       else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
11210         die = lookup_type_die (TREE_TYPE (decl));
11211       else
11212         die = NULL;
11213
11214       if (die != NULL && die->die_parent == NULL)
11215         add_child_die (context_die, die);
11216       else
11217         gen_decl_die (decl, context_die);
11218     }
11219
11220   /* Output the DIEs to represent all sub-blocks (and the items declared
11221      therein) of this block.  */
11222   for (subblocks = BLOCK_SUBBLOCKS (stmt);
11223        subblocks != NULL;
11224        subblocks = BLOCK_CHAIN (subblocks))
11225     gen_block_die (subblocks, context_die, depth + 1);
11226 }
11227
11228 /* Is this a typedef we can avoid emitting?  */
11229
11230 static inline int
11231 is_redundant_typedef (decl)
11232      tree decl;
11233 {
11234   if (TYPE_DECL_IS_STUB (decl))
11235     return 1;
11236
11237   if (DECL_ARTIFICIAL (decl)
11238       && DECL_CONTEXT (decl)
11239       && is_tagged_type (DECL_CONTEXT (decl))
11240       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
11241       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
11242     /* Also ignore the artificial member typedef for the class name.  */
11243     return 1;
11244
11245   return 0;
11246 }
11247
11248 /* Generate Dwarf debug information for a decl described by DECL.  */
11249
11250 static void
11251 gen_decl_die (decl, context_die)
11252      tree decl;
11253      dw_die_ref context_die;
11254 {
11255   tree origin;
11256
11257   if (DECL_P (decl) && DECL_IGNORED_P (decl))
11258     return;
11259
11260   switch (TREE_CODE (decl))
11261     {
11262     case ERROR_MARK:
11263       break;
11264
11265     case CONST_DECL:
11266       /* The individual enumerators of an enum type get output when we output
11267          the Dwarf representation of the relevant enum type itself.  */
11268       break;
11269
11270     case FUNCTION_DECL:
11271       /* Don't output any DIEs to represent mere function declarations,
11272          unless they are class members or explicit block externs.  */
11273       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
11274           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
11275         break;
11276
11277       /* If we're emitting a clone, emit info for the abstract instance.  */
11278       if (DECL_ORIGIN (decl) != decl)
11279         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
11280
11281       /* If we're emitting an out-of-line copy of an inline function,
11282          emit info for the abstract instance and set up to refer to it.  */
11283       else if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
11284                && ! class_scope_p (context_die)
11285                /* dwarf2out_abstract_function won't emit a die if this is just
11286                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
11287                   that case, because that works only if we have a die.  */
11288                && DECL_INITIAL (decl) != NULL_TREE)
11289         {
11290           dwarf2out_abstract_function (decl);
11291           set_decl_origin_self (decl);
11292         }
11293
11294       /* Otherwise we're emitting the primary DIE for this decl.  */
11295       else if (debug_info_level > DINFO_LEVEL_TERSE)
11296         {
11297           /* Before we describe the FUNCTION_DECL itself, make sure that we
11298              have described its return type.  */
11299           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
11300
11301           /* And its virtual context.  */
11302           if (DECL_VINDEX (decl) != NULL_TREE)
11303             gen_type_die (DECL_CONTEXT (decl), context_die);
11304
11305           /* And its containing type.  */
11306           origin = decl_class_context (decl);
11307           if (origin != NULL_TREE)
11308             gen_type_die_for_member (origin, decl, context_die);
11309         }
11310
11311       /* Now output a DIE to represent the function itself.  */
11312       gen_subprogram_die (decl, context_die);
11313       break;
11314
11315     case TYPE_DECL:
11316       /* If we are in terse mode, don't generate any DIEs to represent any
11317          actual typedefs.  */
11318       if (debug_info_level <= DINFO_LEVEL_TERSE)
11319         break;
11320
11321       /* In the special case of a TYPE_DECL node representing the declaration
11322          of some type tag, if the given TYPE_DECL is marked as having been
11323          instantiated from some other (original) TYPE_DECL node (e.g. one which
11324          was generated within the original definition of an inline function) we
11325          have to generate a special (abbreviated) DW_TAG_structure_type,
11326          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
11327       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
11328         {
11329           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
11330           break;
11331         }
11332
11333       if (is_redundant_typedef (decl))
11334         gen_type_die (TREE_TYPE (decl), context_die);
11335       else
11336         /* Output a DIE to represent the typedef itself.  */
11337         gen_typedef_die (decl, context_die);
11338       break;
11339
11340     case LABEL_DECL:
11341       if (debug_info_level >= DINFO_LEVEL_NORMAL)
11342         gen_label_die (decl, context_die);
11343       break;
11344
11345     case VAR_DECL:
11346       /* If we are in terse mode, don't generate any DIEs to represent any
11347          variable declarations or definitions.  */
11348       if (debug_info_level <= DINFO_LEVEL_TERSE)
11349         break;
11350
11351       /* Output any DIEs that are needed to specify the type of this data
11352          object.  */
11353       gen_type_die (TREE_TYPE (decl), context_die);
11354
11355       /* And its containing type.  */
11356       origin = decl_class_context (decl);
11357       if (origin != NULL_TREE)
11358         gen_type_die_for_member (origin, decl, context_die);
11359
11360       /* Now output the DIE to represent the data object itself.  This gets
11361          complicated because of the possibility that the VAR_DECL really
11362          represents an inlined instance of a formal parameter for an inline
11363          function.  */
11364       origin = decl_ultimate_origin (decl);
11365       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
11366         gen_formal_parameter_die (decl, context_die);
11367       else
11368         gen_variable_die (decl, context_die);
11369       break;
11370
11371     case FIELD_DECL:
11372       /* Ignore the nameless fields that are used to skip bits but handle C++
11373          anonymous unions.  */
11374       if (DECL_NAME (decl) != NULL_TREE
11375           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
11376         {
11377           gen_type_die (member_declared_type (decl), context_die);
11378           gen_field_die (decl, context_die);
11379         }
11380       break;
11381
11382     case PARM_DECL:
11383       gen_type_die (TREE_TYPE (decl), context_die);
11384       gen_formal_parameter_die (decl, context_die);
11385       break;
11386
11387     case NAMESPACE_DECL:
11388       /* Ignore for now.  */
11389       break;
11390
11391     default:
11392       abort ();
11393     }
11394 }
11395
11396 static void
11397 mark_limbo_die_list (ptr)
11398      void *ptr ATTRIBUTE_UNUSED;
11399 {
11400   limbo_die_node *node;
11401   for (node = limbo_die_list; node ; node = node->next)
11402     ggc_mark_tree (node->created_for);
11403 }
11404 \f
11405 /* Add Ada "use" clause information for SGI Workshop debugger.  */
11406
11407 void
11408 dwarf2out_add_library_unit_info (filename, context_list)
11409      const char *filename;
11410      const char *context_list;
11411 {
11412   unsigned int file_index;
11413
11414   if (filename != NULL)
11415     {
11416       dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
11417       tree context_list_decl
11418         = build_decl (LABEL_DECL, get_identifier (context_list),
11419                       void_type_node);
11420
11421       TREE_PUBLIC (context_list_decl) = TRUE;
11422       add_name_attribute (unit_die, context_list);
11423       file_index = lookup_filename (filename);
11424       add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
11425       add_pubname (context_list_decl, unit_die);
11426     }
11427 }
11428
11429 /* Output debug information for global decl DECL.  Called from toplev.c after
11430    compilation proper has finished.  */
11431
11432 static void
11433 dwarf2out_global_decl (decl)
11434      tree decl;
11435 {
11436   /* Output DWARF2 information for file-scope tentative data object
11437      declarations, file-scope (extern) function declarations (which had no
11438      corresponding body) and file-scope tagged type declarations and
11439      definitions which have not yet been forced out.  */
11440   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
11441     dwarf2out_decl (decl);
11442 }
11443
11444 /* Write the debugging output for DECL.  */
11445
11446 void
11447 dwarf2out_decl (decl)
11448      tree decl;
11449 {
11450   dw_die_ref context_die = comp_unit_die;
11451
11452   switch (TREE_CODE (decl))
11453     {
11454     case ERROR_MARK:
11455       return;
11456
11457     case FUNCTION_DECL:
11458       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a
11459          builtin function.  Explicit programmer-supplied declarations of
11460          these same functions should NOT be ignored however.  */
11461       if (DECL_EXTERNAL (decl) && DECL_BUILT_IN (decl))
11462         return;
11463
11464       /* What we would really like to do here is to filter out all mere
11465          file-scope declarations of file-scope functions which are never
11466          referenced later within this translation unit (and keep all of ones
11467          that *are* referenced later on) but we aren't clairvoyant, so we have
11468          no idea which functions will be referenced in the future (i.e. later
11469          on within the current translation unit). So here we just ignore all
11470          file-scope function declarations which are not also definitions.  If
11471          and when the debugger needs to know something about these functions,
11472          it will have to hunt around and find the DWARF information associated
11473          with the definition of the function.
11474
11475          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
11476          nodes represent definitions and which ones represent mere
11477          declarations.  We have to check DECL_INITIAL instead. That's because
11478          the C front-end supports some weird semantics for "extern inline"
11479          function definitions.  These can get inlined within the current
11480          translation unit (an thus, we need to generate Dwarf info for their
11481          abstract instances so that the Dwarf info for the concrete inlined
11482          instances can have something to refer to) but the compiler never
11483          generates any out-of-lines instances of such things (despite the fact
11484          that they *are* definitions).
11485
11486          The important point is that the C front-end marks these "extern
11487          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
11488          them anyway. Note that the C++ front-end also plays some similar games
11489          for inline function definitions appearing within include files which
11490          also contain `#pragma interface' pragmas.  */
11491       if (DECL_INITIAL (decl) == NULL_TREE)
11492         return;
11493
11494       /* If we're a nested function, initially use a parent of NULL; if we're
11495          a plain function, this will be fixed up in decls_for_scope.  If
11496          we're a method, it will be ignored, since we already have a DIE.  */
11497       if (decl_function_context (decl))
11498         context_die = NULL;
11499       break;
11500
11501     case VAR_DECL:
11502       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
11503          declaration and if the declaration was never even referenced from
11504          within this entire compilation unit.  We suppress these DIEs in
11505          order to save space in the .debug section (by eliminating entries
11506          which are probably useless).  Note that we must not suppress
11507          block-local extern declarations (whether used or not) because that
11508          would screw-up the debugger's name lookup mechanism and cause it to
11509          miss things which really ought to be in scope at a given point.  */
11510       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
11511         return;
11512
11513       /* If we are in terse mode, don't generate any DIEs to represent any
11514          variable declarations or definitions.  */
11515       if (debug_info_level <= DINFO_LEVEL_TERSE)
11516         return;
11517       break;
11518
11519     case TYPE_DECL:
11520       /* Don't emit stubs for types unless they are needed by other DIEs.  */
11521       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
11522         return;
11523
11524       /* Don't bother trying to generate any DIEs to represent any of the
11525          normal built-in types for the language we are compiling.  */
11526       if (DECL_SOURCE_LINE (decl) == 0)
11527         {
11528           /* OK, we need to generate one for `bool' so GDB knows what type
11529              comparisons have.  */
11530           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
11531                == DW_LANG_C_plus_plus)
11532               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
11533               && ! DECL_IGNORED_P (decl))
11534             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
11535
11536           return;
11537         }
11538
11539       /* If we are in terse mode, don't generate any DIEs for types.  */
11540       if (debug_info_level <= DINFO_LEVEL_TERSE)
11541         return;
11542
11543       /* If we're a function-scope tag, initially use a parent of NULL;
11544          this will be fixed up in decls_for_scope.  */
11545       if (decl_function_context (decl))
11546         context_die = NULL;
11547
11548       break;
11549
11550     default:
11551       return;
11552     }
11553
11554   gen_decl_die (decl, context_die);
11555 }
11556
11557 /* Output a marker (i.e. a label) for the beginning of the generated code for
11558    a lexical block.  */
11559
11560 static void
11561 dwarf2out_begin_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_BEGIN_LABEL, blocknum);
11567 }
11568
11569 /* Output a marker (i.e. a label) for the end of the generated code for a
11570    lexical block.  */
11571
11572 static void
11573 dwarf2out_end_block (line, blocknum)
11574      unsigned int line ATTRIBUTE_UNUSED;
11575      unsigned int blocknum;
11576 {
11577   function_section (current_function_decl);
11578   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
11579 }
11580
11581 /* Returns nonzero if it is appropriate not to emit any debugging
11582    information for BLOCK, because it doesn't contain any instructions.
11583
11584    Don't allow this for blocks with nested functions or local classes
11585    as we would end up with orphans, and in the presence of scheduling
11586    we may end up calling them anyway.  */
11587
11588 static bool
11589 dwarf2out_ignore_block (block)
11590      tree block;
11591 {
11592   tree decl;
11593
11594   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
11595     if (TREE_CODE (decl) == FUNCTION_DECL
11596         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
11597       return 0;
11598
11599   return 1;
11600 }
11601
11602 /* Lookup FILE_NAME (in the list of filenames that we know about here in
11603    dwarf2out.c) and return its "index".  The index of each (known) filename is
11604    just a unique number which is associated with only that one filename.  We
11605    need such numbers for the sake of generating labels (in the .debug_sfnames
11606    section) and references to those files numbers (in the .debug_srcinfo
11607    and.debug_macinfo sections).  If the filename given as an argument is not
11608    found in our current list, add it to the list and assign it the next
11609    available unique index number.  In order to speed up searches, we remember
11610    the index of the filename was looked up last.  This handles the majority of
11611    all searches.  */
11612
11613 static unsigned
11614 lookup_filename (file_name)
11615      const char *file_name;
11616 {
11617   unsigned i;
11618
11619   /* ??? Why isn't DECL_SOURCE_FILE left null instead.  */
11620   if (strcmp (file_name, "<internal>") == 0
11621       || strcmp (file_name, "<built-in>") == 0)
11622     return 0;
11623
11624   /* Check to see if the file name that was searched on the previous
11625      call matches this file name.  If so, return the index.  */
11626   if (file_table.last_lookup_index != 0)
11627     if (0 == strcmp (file_name,
11628                      file_table.table[file_table.last_lookup_index]))
11629       return file_table.last_lookup_index;
11630
11631   /* Didn't match the previous lookup, search the table */
11632   for (i = 1; i < file_table.in_use; i++)
11633     if (strcmp (file_name, file_table.table[i]) == 0)
11634       {
11635         file_table.last_lookup_index = i;
11636         return i;
11637       }
11638
11639   /* Prepare to add a new table entry by making sure there is enough space in
11640      the table to do so.  If not, expand the current table.  */
11641   if (i == file_table.allocated)
11642     {
11643       file_table.allocated = i + FILE_TABLE_INCREMENT;
11644       file_table.table = (char **)
11645         xrealloc (file_table.table, file_table.allocated * sizeof (char *));
11646     }
11647
11648   /* Add the new entry to the end of the filename table.  */
11649   file_table.table[i] = xstrdup (file_name);
11650   file_table.in_use = i + 1;
11651   file_table.last_lookup_index = i;
11652
11653   if (DWARF2_ASM_LINE_DEBUG_INFO)
11654     fprintf (asm_out_file, "\t.file %u \"%s\"\n", i, file_name);
11655
11656   return i;
11657 }
11658
11659 static void
11660 init_file_table ()
11661 {
11662   /* Allocate the initial hunk of the file_table.  */
11663   file_table.table = (char **) xcalloc (FILE_TABLE_INCREMENT, sizeof (char *));
11664   file_table.allocated = FILE_TABLE_INCREMENT;
11665
11666   /* Skip the first entry - file numbers begin at 1.  */
11667   file_table.in_use = 1;
11668   file_table.last_lookup_index = 0;
11669 }
11670
11671 /* Output a label to mark the beginning of a source code line entry
11672    and record information relating to this source line, in
11673    'line_info_table' for later output of the .debug_line section.  */
11674
11675 static void
11676 dwarf2out_source_line (line, filename)
11677      unsigned int line;
11678      const char *filename;
11679 {
11680   if (debug_info_level >= DINFO_LEVEL_NORMAL)
11681     {
11682       function_section (current_function_decl);
11683
11684       /* If requested, emit something human-readable.  */
11685       if (flag_debug_asm)
11686         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
11687                  filename, line);
11688
11689       if (DWARF2_ASM_LINE_DEBUG_INFO)
11690         {
11691           unsigned file_num = lookup_filename (filename);
11692
11693           /* Emit the .loc directive understood by GNU as.  */
11694           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
11695
11696           /* Indicate that line number info exists.  */
11697           line_info_table_in_use++;
11698
11699           /* Indicate that multiple line number tables exist.  */
11700           if (DECL_SECTION_NAME (current_function_decl))
11701             separate_line_info_table_in_use++;
11702         }
11703       else if (DECL_SECTION_NAME (current_function_decl))
11704         {
11705           dw_separate_line_info_ref line_info;
11706           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, SEPARATE_LINE_CODE_LABEL,
11707                                      separate_line_info_table_in_use);
11708
11709           /* expand the line info table if necessary */
11710           if (separate_line_info_table_in_use
11711               == separate_line_info_table_allocated)
11712             {
11713               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11714               separate_line_info_table
11715                 = (dw_separate_line_info_ref)
11716                   xrealloc (separate_line_info_table,
11717                             separate_line_info_table_allocated
11718                             * sizeof (dw_separate_line_info_entry));
11719             }
11720
11721           /* Add the new entry at the end of the line_info_table.  */
11722           line_info
11723             = &separate_line_info_table[separate_line_info_table_in_use++];
11724           line_info->dw_file_num = lookup_filename (filename);
11725           line_info->dw_line_num = line;
11726           line_info->function = current_funcdef_number;
11727         }
11728       else
11729         {
11730           dw_line_info_ref line_info;
11731
11732           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, LINE_CODE_LABEL,
11733                                      line_info_table_in_use);
11734
11735           /* Expand the line info table if necessary.  */
11736           if (line_info_table_in_use == line_info_table_allocated)
11737             {
11738               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11739               line_info_table
11740                 = (dw_line_info_ref)
11741                   xrealloc (line_info_table,
11742                             (line_info_table_allocated
11743                              * sizeof (dw_line_info_entry)));
11744             }
11745
11746           /* Add the new entry at the end of the line_info_table.  */
11747           line_info = &line_info_table[line_info_table_in_use++];
11748           line_info->dw_file_num = lookup_filename (filename);
11749           line_info->dw_line_num = line;
11750         }
11751     }
11752 }
11753
11754 /* Record the beginning of a new source file.  */
11755
11756 static void
11757 dwarf2out_start_source_file (lineno, filename)
11758      unsigned int lineno;
11759      const char *filename;
11760 {
11761   if (flag_eliminate_dwarf2_dups)
11762     {
11763       /* Record the beginning of the file for break_out_includes.  */
11764       dw_die_ref bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
11765       add_AT_string (bincl_die, DW_AT_name, filename);
11766     }
11767
11768   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11769     {
11770       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11771       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
11772       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
11773                                    lineno);
11774       dw2_asm_output_data_uleb128 (lookup_filename (filename),
11775                                    "Filename we just started");
11776     }
11777 }
11778
11779 /* Record the end of a source file.  */
11780
11781 static void
11782 dwarf2out_end_source_file (lineno)
11783      unsigned int lineno ATTRIBUTE_UNUSED;
11784 {
11785   if (flag_eliminate_dwarf2_dups)
11786     /* Record the end of the file for break_out_includes.  */
11787     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
11788
11789   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11790     {
11791       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11792       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
11793     }
11794 }
11795
11796 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
11797    the tail part of the directive line, i.e. the part which is past the
11798    initial whitespace, #, whitespace, directive-name, whitespace part.  */
11799
11800 static void
11801 dwarf2out_define (lineno, buffer)
11802      unsigned lineno ATTRIBUTE_UNUSED;
11803      const char *buffer ATTRIBUTE_UNUSED;
11804 {
11805   static int initialized = 0;
11806   if (!initialized)
11807     {
11808       dwarf2out_start_source_file (0, primary_filename);
11809       initialized = 1;
11810     }
11811
11812   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11813     {
11814       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11815       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
11816       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
11817       dw2_asm_output_nstring (buffer, -1, "The macro");
11818     }
11819 }
11820
11821 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
11822    the tail part of the directive line, i.e. the part which is past the
11823    initial whitespace, #, whitespace, directive-name, whitespace part.  */
11824
11825 static void
11826 dwarf2out_undef (lineno, buffer)
11827      unsigned lineno ATTRIBUTE_UNUSED;
11828      const char *buffer ATTRIBUTE_UNUSED;
11829 {
11830   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11831     {
11832       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11833       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
11834       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
11835       dw2_asm_output_nstring (buffer, -1, "The macro");
11836     }
11837 }
11838
11839 /* Set up for Dwarf output at the start of compilation.  */
11840
11841 static void
11842 dwarf2out_init (main_input_filename)
11843      const char *main_input_filename;
11844 {
11845   init_file_table ();
11846
11847   /* Remember the name of the primary input file.  */
11848   primary_filename = main_input_filename;
11849
11850   /* Add it to the file table first, under the assumption that we'll
11851      be emitting line number data for it first, which avoids having
11852      to add an initial DW_LNS_set_file.  */
11853   lookup_filename (main_input_filename);
11854
11855   /* Allocate the initial hunk of the decl_die_table.  */
11856   decl_die_table
11857     = (dw_die_ref *) xcalloc (DECL_DIE_TABLE_INCREMENT, sizeof (dw_die_ref));
11858   decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
11859   decl_die_table_in_use = 0;
11860
11861   /* Allocate the initial hunk of the decl_scope_table.  */
11862   VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
11863   ggc_add_tree_varray_root (&decl_scope_table, 1);
11864
11865   /* Allocate the initial hunk of the abbrev_die_table.  */
11866   abbrev_die_table
11867     = (dw_die_ref *) xcalloc (ABBREV_DIE_TABLE_INCREMENT,
11868                               sizeof (dw_die_ref));
11869   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
11870   /* Zero-th entry is allocated, but unused */
11871   abbrev_die_table_in_use = 1;
11872
11873   /* Allocate the initial hunk of the line_info_table.  */
11874   line_info_table
11875     = (dw_line_info_ref) xcalloc (LINE_INFO_TABLE_INCREMENT,
11876                                   sizeof (dw_line_info_entry));
11877   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
11878
11879   /* Zero-th entry is allocated, but unused */
11880   line_info_table_in_use = 1;
11881
11882   /* Generate the initial DIE for the .debug section.  Note that the (string)
11883      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
11884      will (typically) be a relative pathname and that this pathname should be
11885      taken as being relative to the directory from which the compiler was
11886      invoked when the given (base) source file was compiled.  */
11887   comp_unit_die = gen_compile_unit_die (main_input_filename);
11888
11889   VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
11890   ggc_add_tree_varray_root (&incomplete_types, 1);
11891
11892   VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
11893   ggc_add_rtx_varray_root (&used_rtx_varray, 1);
11894
11895   ggc_add_root (&limbo_die_list, 1, 1, mark_limbo_die_list);
11896
11897   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
11898   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
11899                                DEBUG_ABBREV_SECTION_LABEL, 0);
11900   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11901     ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
11902   else
11903     strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
11904
11905   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
11906                                DEBUG_INFO_SECTION_LABEL, 0);
11907   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
11908                                DEBUG_LINE_SECTION_LABEL, 0);
11909   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
11910                                DEBUG_RANGES_SECTION_LABEL, 0);
11911   named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
11912   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
11913   named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
11914   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11915   named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
11916   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
11917
11918   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11919     {
11920       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11921       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
11922                                    DEBUG_MACINFO_SECTION_LABEL, 0);
11923       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
11924     }
11925
11926   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11927     {
11928       text_section ();
11929       ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
11930     }
11931 }
11932
11933 /* Allocate a string in .debug_str hash table.  */
11934
11935 static hashnode
11936 indirect_string_alloc (tab)
11937      hash_table *tab ATTRIBUTE_UNUSED;
11938 {
11939   struct indirect_string_node *node;
11940
11941   node = xmalloc (sizeof (struct indirect_string_node));
11942   node->refcount = 0;
11943   node->form = 0;
11944   node->label = NULL;
11945
11946   return (hashnode) node;
11947 }
11948
11949 /* A helper function for dwarf2out_finish called through
11950    ht_forall.  Emit one queued .debug_str string.  */
11951
11952 static int
11953 output_indirect_string (pfile, h, v)
11954      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
11955      hashnode h;
11956      const PTR v ATTRIBUTE_UNUSED;
11957 {
11958   struct indirect_string_node *node = (struct indirect_string_node *) h;
11959
11960   if (node->form == DW_FORM_strp)
11961     {
11962       named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
11963       ASM_OUTPUT_LABEL (asm_out_file, node->label);
11964       assemble_string ((const char *) HT_STR (&node->id),
11965                        HT_LEN (&node->id) + 1);
11966     }
11967
11968   return 1;
11969 }
11970
11971 /* Output stuff that dwarf requires at the end of every file,
11972    and generate the DWARF-2 debugging info.  */
11973
11974 static void
11975 dwarf2out_finish (input_filename)
11976      const char *input_filename ATTRIBUTE_UNUSED;
11977 {
11978   limbo_die_node *node, *next_node;
11979   dw_die_ref die = 0;
11980
11981   /* Traverse the limbo die list, and add parent/child links.  The only
11982      dies without parents that should be here are concrete instances of
11983      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
11984      For concrete instances, we can get the parent die from the abstract
11985      instance.  */
11986   for (node = limbo_die_list; node; node = next_node)
11987     {
11988       next_node = node->next;
11989       die = node->die;
11990
11991       if (die->die_parent == NULL)
11992         {
11993           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
11994           tree context;
11995
11996           if (origin)
11997             add_child_die (origin->die_parent, die);
11998           else if (die == comp_unit_die)
11999             ;
12000           else if (node->created_for
12001                    && ((DECL_P (node->created_for)
12002                         && (context = DECL_CONTEXT (node->created_for)))
12003                        || (TYPE_P (node->created_for)
12004                            && (context = TYPE_CONTEXT (node->created_for))))
12005                    && TREE_CODE (context) == FUNCTION_DECL)
12006             {
12007               /* In certain situations, the lexical block containing a
12008                  nested function can be optimized away, which results
12009                  in the nested function die being orphaned.  Likewise
12010                  with the return type of that nested function.  Force
12011                  this to be a child of the containing function.  */
12012               origin = lookup_decl_die (context);
12013               if (! origin)
12014                 abort ();
12015               add_child_die (origin, die);
12016             }
12017           else if (errorcount > 0 || sorrycount > 0)
12018             /* It's OK to be confused by errors in the input.  */
12019             add_child_die (comp_unit_die, die);
12020           else
12021             abort ();
12022         }
12023
12024       free (node);
12025     }
12026
12027   limbo_die_list = NULL;
12028
12029   /* Walk through the list of incomplete types again, trying once more to
12030      emit full debugging info for them.  */
12031   retry_incomplete_types ();
12032
12033   /* We need to reverse all the dies before break_out_includes, or
12034      we'll see the end of an include file before the beginning.  */
12035   reverse_all_dies (comp_unit_die);
12036
12037   /* Generate separate CUs for each of the include files we've seen.
12038      They will go into limbo_die_list.  */
12039   if (flag_eliminate_dwarf2_dups)
12040     break_out_includes (comp_unit_die);
12041
12042   /* Traverse the DIE's and add add sibling attributes to those DIE's
12043      that have children.  */
12044   add_sibling_attributes (comp_unit_die);
12045   for (node = limbo_die_list; node; node = node->next)
12046     add_sibling_attributes (node->die);
12047
12048   /* Output a terminator label for the .text section.  */
12049   text_section ();
12050   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, TEXT_END_LABEL, 0);
12051
12052   /* Output the source line correspondence table.  We must do this
12053      even if there is no line information.  Otherwise, on an empty
12054      translation unit, we will generate a present, but empty,
12055      .debug_info section.  IRIX 6.5 `nm' will then complain when
12056      examining the file.  */
12057   if (! DWARF2_ASM_LINE_DEBUG_INFO)
12058     {
12059       named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
12060       output_line_info ();
12061     }
12062
12063   /* Output location list section if necessary.  */
12064   if (have_location_lists)
12065     {
12066       /* Output the location lists info.  */
12067       named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
12068       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
12069                                    DEBUG_LOC_SECTION_LABEL, 0);
12070       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
12071       output_location_lists (die);
12072       have_location_lists = 0;
12073     }
12074
12075   /* We can only use the low/high_pc attributes if all of the code was
12076      in .text.  */
12077   if (separate_line_info_table_in_use == 0)
12078     {
12079       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
12080       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
12081     }
12082
12083   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
12084      "base address".  Use zero so that these addresses become absolute.  */
12085   else if (have_location_lists || ranges_table_in_use)
12086     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
12087
12088   if (debug_info_level >= DINFO_LEVEL_NORMAL)
12089     add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
12090                        debug_line_section_label);
12091
12092   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12093     add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
12094
12095   /* Output all of the compilation units.  We put the main one last so that
12096      the offsets are available to output_pubnames.  */
12097   for (node = limbo_die_list; node; node = node->next)
12098     output_comp_unit (node->die);
12099
12100   output_comp_unit (comp_unit_die);
12101
12102   /* Output the abbreviation table.  */
12103   named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
12104   output_abbrev_section ();
12105
12106   /* Output public names table if necessary.  */
12107   if (pubname_table_in_use)
12108     {
12109       named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
12110       output_pubnames ();
12111     }
12112
12113   /* Output the address range information.  We only put functions in the arange
12114      table, so don't write it out if we don't have any.  */
12115   if (fde_table_in_use)
12116     {
12117       named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
12118       output_aranges ();
12119     }
12120
12121   /* Output ranges section if necessary.  */
12122   if (ranges_table_in_use)
12123     {
12124       named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
12125       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12126       output_ranges ();
12127     }
12128
12129   /* Have to end the primary source file.  */
12130   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12131     { 
12132       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12133       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
12134     }
12135
12136   /* If we emitted any DW_FORM_strp form attribute, output the string
12137      table too.  */
12138   if (debug_str_hash)
12139     ht_forall (debug_str_hash, output_indirect_string, NULL);
12140 }
12141 #endif /* DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO */