OSDN Git Service

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