OSDN Git Service

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