OSDN Git Service

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