OSDN Git Service

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