OSDN Git Service

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