OSDN Git Service

2001-07-04 Daniel Berlin <dan@cgsoftware.com>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from the GNU C compiler.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of 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 long 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 long 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       if (DWARF2_ADDR_SIZE == 4)
5950         dw2_asm_output_data (DWARF2_ADDR_SIZE, 0xffffffff, "Location list base address specifier fake entry");
5951       else if (DWARF2_ADDR_SIZE == 8)
5952         dw2_asm_output_data (DWARF2_ADDR_SIZE, 0xffffffffffffffffLL, "Location list base address specifier fake entry");
5953       else
5954         abort();
5955       dw2_asm_output_offset (DWARF2_ADDR_SIZE, curr->section, "Location list base address specifier base");
5956     }
5957   for (curr = list_head; curr != NULL; curr=curr->dw_loc_next)
5958     {
5959       int size;
5960       dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section, "Location list begin address (%s)", list_head->ll_symbol);
5961       dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section, "Location list end address (%s)", list_head->ll_symbol);
5962       size = size_of_locs (curr->expr);
5963       
5964       /* Output the block length for this list of location operations.  */
5965       dw2_asm_output_data (constant_size (size), size, "%s", "Location expression size");
5966       
5967       output_loc_sequence (curr->expr);
5968     }
5969   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Location list terminator begin (%s)", list_head->ll_symbol);
5970   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Location list terminator end (%s)", list_head->ll_symbol);
5971 }
5972 /* Output the DIE and its attributes.  Called recursively to generate
5973    the definitions of each child DIE.  */
5974
5975 static void
5976 output_die (die)
5977      register dw_die_ref die;
5978 {
5979   register dw_attr_ref a;
5980   register dw_die_ref c;
5981   register unsigned long size;
5982
5983   /* If someone in another CU might refer to us, set up a symbol for
5984      them to point to.  */
5985   if (die->die_symbol)
5986     output_die_symbol (die);
5987
5988   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
5989                                die->die_offset, dwarf_tag_name (die->die_tag));
5990
5991   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5992     {
5993       const char *name = dwarf_attr_name (a->dw_attr);
5994
5995       switch (AT_class (a))
5996         {
5997         case dw_val_class_addr:
5998           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
5999           break;
6000
6001         case dw_val_class_loc:
6002           size = size_of_locs (AT_loc (a));
6003
6004           /* Output the block length for this list of location operations.  */
6005           dw2_asm_output_data (constant_size (size), size, "%s", name);
6006
6007           output_loc_sequence (AT_loc (a));
6008           break;
6009
6010         case dw_val_class_const:
6011           /* ??? It would be slightly more efficient to use a scheme like is
6012              used for unsigned constants below, but gdb 4.x does not sign
6013              extend.  Gdb 5.x does sign extend.  */
6014           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6015           break;
6016
6017         case dw_val_class_unsigned_const:
6018           dw2_asm_output_data (constant_size (AT_unsigned (a)),
6019                                AT_unsigned (a), "%s", name);
6020           break;
6021
6022         case dw_val_class_long_long:
6023           {
6024             unsigned HOST_WIDE_INT first, second;
6025
6026             dw2_asm_output_data (1, 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR,
6027                                  "%s", name);
6028
6029             if (WORDS_BIG_ENDIAN)
6030               {
6031                 first = a->dw_attr_val.v.val_long_long.hi;
6032                 second = a->dw_attr_val.v.val_long_long.low;
6033               }
6034             else
6035               {
6036                 first = a->dw_attr_val.v.val_long_long.low;
6037                 second = a->dw_attr_val.v.val_long_long.hi;
6038               }
6039             dw2_asm_output_data (HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR,
6040                                  first, "long long constant");
6041             dw2_asm_output_data (HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR,
6042                                  second, NULL);
6043           }
6044           break;
6045
6046         case dw_val_class_float:
6047           {
6048             register unsigned int i;
6049
6050             dw2_asm_output_data (1, a->dw_attr_val.v.val_float.length * 4,
6051                                  "%s", name);
6052
6053             for (i = 0; i < a->dw_attr_val.v.val_float.length; ++i)
6054               dw2_asm_output_data (4, a->dw_attr_val.v.val_float.array[i],
6055                                    "fp constant word %u", i);
6056             break;
6057           }
6058
6059         case dw_val_class_flag:
6060           dw2_asm_output_data (1, AT_flag (a), "%s", name);
6061           break;
6062         case dw_val_class_loc_list:
6063           {
6064             char *sym = AT_loc_list (a)->ll_symbol;
6065             if (sym == 0)
6066               abort();
6067             dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label, name);
6068           }
6069           break;
6070         case dw_val_class_die_ref:
6071           if (AT_ref_external (a))
6072             {
6073               char *sym = AT_ref (a)->die_symbol;
6074               if (sym == 0)
6075                 abort ();
6076               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6077             }
6078           else if (AT_ref (a)->die_offset == 0)
6079             abort ();
6080           else
6081             dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6082                                  "%s", name);
6083           break;
6084
6085         case dw_val_class_fde_ref:
6086           {
6087             char l1[20];
6088             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6089                                          a->dw_attr_val.v.val_fde_index * 2);
6090             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6091           }
6092           break;
6093
6094         case dw_val_class_lbl_id:
6095           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6096           break;
6097
6098         case dw_val_class_lbl_offset:
6099           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6100           break;
6101
6102         case dw_val_class_str:
6103           dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6104           break;
6105
6106         default:
6107           abort ();
6108         }
6109     }
6110
6111   for (c = die->die_child; c != NULL; c = c->die_sib)
6112     output_die (c);
6113
6114   if (die->die_child != NULL)
6115     {
6116       /* Add null byte to terminate sibling list.  */
6117       dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6118                            die->die_offset);
6119     }
6120 }
6121
6122 /* Output the compilation unit that appears at the beginning of the
6123    .debug_info section, and precedes the DIE descriptions.  */
6124
6125 static void
6126 output_compilation_unit_header ()
6127 {
6128   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset - DWARF_OFFSET_SIZE,
6129                        "Length of Compilation Unit Info");
6130
6131   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6132
6133   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6134                          "Offset Into Abbrev. Section");
6135
6136   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6137 }
6138
6139 /* Output the compilation unit DIE and its children.  */
6140
6141 static void
6142 output_comp_unit (die)
6143      dw_die_ref die;
6144 {
6145   const char *secname;
6146
6147   /* Even if there are no children of this DIE, we must output the
6148      information about the compilation unit.  Otherwise, on an empty
6149      translation unit, we will generate a present, but empty,
6150      .debug_info section.  IRIX 6.5 `nm' will then complain when
6151      examining the file.
6152      
6153      Mark all the DIEs in this CU so we know which get local refs.  */
6154   mark_dies (die);
6155
6156   build_abbrev_table (die);
6157
6158   /* Initialize the beginning DIE offset - and calculate sizes/offsets.   */
6159   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6160   calc_die_sizes (die);
6161
6162   if (die->die_symbol)
6163     {
6164       char *tmp = (char *) alloca (strlen (die->die_symbol) + 24);
6165       sprintf (tmp, ".gnu.linkonce.wi.%s", die->die_symbol);
6166       secname = tmp;
6167       die->die_symbol = NULL;
6168     }
6169   else
6170     secname = (const char *) DEBUG_INFO_SECTION;
6171
6172   /* Output debugging information.  */
6173   ASM_OUTPUT_SECTION (asm_out_file, secname);
6174   output_compilation_unit_header ();
6175   output_die (die);
6176
6177   /* Leave the marks on the main CU, so we can check them in
6178      output_pubnames.  */
6179   if (die->die_symbol)
6180     unmark_dies (die);
6181 }
6182
6183 /* The DWARF2 pubname for a nested thingy looks like "A::f".  The output
6184    of decl_printable_name for C++ looks like "A::f(int)".  Let's drop the
6185    argument list, and maybe the scope.  */
6186
6187 static const char *
6188 dwarf2_name (decl, scope)
6189      tree decl;
6190      int scope;
6191 {
6192   return (*decl_printable_name) (decl, scope ? 1 : 0);
6193 }
6194
6195 /* Add a new entry to .debug_pubnames if appropriate.  */
6196
6197 static void
6198 add_pubname (decl, die)
6199      tree decl;
6200      dw_die_ref die;
6201 {
6202   pubname_ref p;
6203
6204   if (! TREE_PUBLIC (decl))
6205     return;
6206
6207   if (pubname_table_in_use == pubname_table_allocated)
6208     {
6209       pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
6210       pubname_table = (pubname_ref) xrealloc
6211         (pubname_table, pubname_table_allocated * sizeof (pubname_entry));
6212     }
6213
6214   p = &pubname_table[pubname_table_in_use++];
6215   p->die = die;
6216
6217   p->name = xstrdup (dwarf2_name (decl, 1));
6218 }
6219
6220 /* Output the public names table used to speed up access to externally
6221    visible names.  For now, only generate entries for externally
6222    visible procedures.  */
6223
6224 static void
6225 output_pubnames ()
6226 {
6227   register unsigned i;
6228   register unsigned long pubnames_length = size_of_pubnames ();
6229
6230   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
6231                        "Length of Public Names Info");
6232
6233   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6234
6235   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
6236                          "Offset of Compilation Unit Info");
6237
6238   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
6239                        "Compilation Unit Length");
6240
6241   for (i = 0; i < pubname_table_in_use; ++i)
6242     {
6243       register pubname_ref pub = &pubname_table[i];
6244
6245       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
6246       if (pub->die->die_mark == 0)
6247         abort ();
6248
6249       dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
6250                            "DIE offset");
6251
6252       dw2_asm_output_nstring (pub->name, -1, "external name");
6253     }
6254
6255   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
6256 }
6257
6258 /* Add a new entry to .debug_aranges if appropriate.  */
6259
6260 static void
6261 add_arange (decl, die)
6262      tree decl;
6263      dw_die_ref die;
6264 {
6265   if (! DECL_SECTION_NAME (decl))
6266     return;
6267
6268   if (arange_table_in_use == arange_table_allocated)
6269     {
6270       arange_table_allocated += ARANGE_TABLE_INCREMENT;
6271       arange_table
6272         = (arange_ref) xrealloc (arange_table,
6273                                  arange_table_allocated * sizeof (dw_die_ref));
6274     }
6275
6276   arange_table[arange_table_in_use++] = die;
6277 }
6278
6279 /* Output the information that goes into the .debug_aranges table.
6280    Namely, define the beginning and ending address range of the
6281    text section generated for this compilation unit.  */
6282
6283 static void
6284 output_aranges ()
6285 {
6286   register unsigned i;
6287   register unsigned long aranges_length = size_of_aranges ();
6288
6289   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
6290                        "Length of Address Ranges Info");
6291
6292   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6293
6294   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
6295                          "Offset of Compilation Unit Info");
6296
6297   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
6298
6299   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
6300
6301   /* We need to align to twice the pointer size here.  */
6302   if (DWARF_ARANGES_PAD_SIZE)
6303     {
6304       /* Pad using a 2 byte words so that padding is correct for any
6305          pointer size.  */
6306       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
6307                            2 * DWARF2_ADDR_SIZE);
6308       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
6309         dw2_asm_output_data (2, 0, NULL);
6310     }
6311
6312   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
6313   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
6314                         text_section_label, "Length");
6315
6316   for (i = 0; i < arange_table_in_use; ++i)
6317     {
6318       dw_die_ref die = arange_table[i];
6319
6320       /* We shouldn't see aranges for DIEs outside of the main CU.  */
6321       if (die->die_mark == 0)
6322         abort ();
6323
6324       if (die->die_tag == DW_TAG_subprogram)
6325         {
6326           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
6327                                  "Address");
6328           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
6329                                 get_AT_low_pc (die), "Length");
6330         }
6331       else
6332         {
6333           /* A static variable; extract the symbol from DW_AT_location.
6334              Note that this code isn't currently hit, as we only emit
6335              aranges for functions (jason 9/23/99).  */
6336
6337           dw_attr_ref a = get_AT (die, DW_AT_location);
6338           dw_loc_descr_ref loc;
6339           if (! a || AT_class (a) != dw_val_class_loc)
6340             abort ();
6341
6342           loc = AT_loc (a);
6343           if (loc->dw_loc_opc != DW_OP_addr)
6344             abort ();
6345
6346           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
6347                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
6348           dw2_asm_output_data (DWARF2_ADDR_SIZE,
6349                                get_AT_unsigned (die, DW_AT_byte_size),
6350                                "Length");
6351         }
6352     }
6353
6354   /* Output the terminator words.  */
6355   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6356   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6357 }
6358
6359
6360 /* Data structure containing information about input files.  */
6361 struct file_info
6362 {
6363   char *path;           /* Complete file name.  */
6364   char *fname;          /* File name part.  */
6365   int length;           /* Length of entire string.  */
6366   int file_idx;         /* Index in input file table.  */
6367   int dir_idx;          /* Index in directory table.  */
6368 };
6369
6370 /* Data structure containing information about directories with source
6371    files.  */
6372 struct dir_info
6373 {
6374   char *path;           /* Path including directory name.  */
6375   int length;           /* Path length.  */
6376   int prefix;           /* Index of directory entry which is a prefix.  */
6377   int count;            /* Number of files in this directory.  */
6378   int dir_idx;          /* Index of directory used as base.  */
6379   int used;             /* Used in the end?  */
6380 };
6381
6382 /* Callback function for file_info comparison.  We sort by looking at
6383    the directories in the path.  */
6384 static int
6385 file_info_cmp (p1, p2)
6386      const void *p1;
6387      const void *p2;
6388 {
6389   const struct file_info *s1 = p1;
6390   const struct file_info *s2 = p2;
6391   unsigned char *cp1;
6392   unsigned char *cp2;
6393
6394   /* Take care of file names without directories.  */
6395   if (s1->path == s1->fname)
6396     return -1;
6397   else if (s2->path == s2->fname)
6398     return 1;
6399
6400   cp1 = (unsigned char *) s1->path;
6401   cp2 = (unsigned char *) s2->path;
6402
6403   while (1)
6404     {
6405       ++cp1;
6406       ++cp2;
6407       /* Reached the end of the first path?  */
6408       if (cp1 == (unsigned char *) s1->fname)
6409         /* It doesn't really matter in which order files from the
6410            same directory are sorted in.  Therefore don't test for
6411            the second path reaching the end.  */
6412         return -1;
6413       else if (cp2 == (unsigned char *) s2->fname)
6414         return 1;
6415
6416       /* Character of current path component the same?  */
6417       if (*cp1 != *cp2)
6418         return *cp1 - *cp2;
6419     }
6420 }
6421
6422 /* Output the directory table and the file name table.  We try to minimize
6423    the total amount of memory needed.  A heuristic is used to avoid large
6424    slowdowns with many input files.  */
6425 static void
6426 output_file_names ()
6427 {
6428   struct file_info *files;
6429   struct dir_info *dirs;
6430   int *saved;
6431   int *savehere;
6432   int *backmap;
6433   int ndirs;
6434   int idx_offset;
6435   int i;
6436   int idx;
6437
6438   /* Allocate the various arrays we need.  */
6439   files = (struct file_info *) alloca (file_table.in_use
6440                                        * sizeof (struct file_info));
6441   dirs = (struct dir_info *) alloca (file_table.in_use
6442                                      * sizeof (struct dir_info));
6443
6444   /* Sort the file names.  */
6445   for (i = 1; i < (int) file_table.in_use; ++i)
6446     {
6447       char *f;
6448
6449       /* Skip all leading "./".  */
6450       f = file_table.table[i];
6451       while (f[0] == '.' && f[1] == '/')
6452         f += 2;
6453
6454       /* Create a new array entry.  */
6455       files[i].path = f;
6456       files[i].length = strlen (f);
6457       files[i].file_idx = i;
6458
6459       /* Search for the file name part.  */
6460       f = strrchr (f, '/');
6461       files[i].fname = f == NULL ? files[i].path : f + 1;
6462     }
6463   qsort (files + 1, file_table.in_use - 1, sizeof (files[0]), file_info_cmp);
6464
6465   /* Find all the different directories used.  */
6466   dirs[0].path = files[1].path;
6467   dirs[0].length = files[1].fname - files[1].path;
6468   dirs[0].prefix = -1;
6469   dirs[0].count = 1;
6470   dirs[0].dir_idx = 0;
6471   dirs[0].used = 0;
6472   files[1].dir_idx = 0;
6473   ndirs = 1;
6474
6475   for (i = 2; i < (int) file_table.in_use; ++i)
6476     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
6477         && memcmp (dirs[ndirs - 1].path, files[i].path,
6478                    dirs[ndirs - 1].length) == 0)
6479       {
6480         /* Same directory as last entry.  */
6481         files[i].dir_idx = ndirs - 1;
6482         ++dirs[ndirs - 1].count;
6483       }
6484     else
6485       {
6486         int j;
6487
6488         /* This is a new directory.  */
6489         dirs[ndirs].path = files[i].path;
6490         dirs[ndirs].length = files[i].fname - files[i].path;
6491         dirs[ndirs].count = 1;
6492         dirs[ndirs].dir_idx = ndirs;
6493         dirs[ndirs].used = 0;
6494         files[i].dir_idx = ndirs;
6495
6496         /* Search for a prefix.  */
6497         dirs[ndirs].prefix = -1;
6498         for (j = 0; j < ndirs; ++j)
6499           if (dirs[j].length < dirs[ndirs].length
6500               && dirs[j].length > 1
6501               && (dirs[ndirs].prefix == -1
6502                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
6503               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
6504             dirs[ndirs].prefix = j;
6505
6506         ++ndirs;
6507       }
6508
6509   /* Now to the actual work.  We have to find a subset of the
6510      directories which allow expressing the file name using references
6511      to the directory table with the least amount of characters.  We
6512      do not do an exhaustive search where we would have to check out
6513      every combination of every single possible prefix.  Instead we
6514      use a heuristic which provides nearly optimal results in most
6515      cases and never is much off.  */
6516   saved = (int *) alloca (ndirs * sizeof (int));
6517   savehere = (int *) alloca (ndirs * sizeof (int));
6518
6519   memset (saved, '\0', ndirs * sizeof (saved[0]));
6520   for (i = 0; i < ndirs; ++i)
6521     {
6522       int j;
6523       int total;
6524
6525       /* We can always save some space for the current directory.  But
6526          this does not mean it will be enough to justify adding the
6527          directory.  */
6528       savehere[i] = dirs[i].length;
6529       total = (savehere[i] - saved[i]) * dirs[i].count;
6530
6531       for (j = i + 1; j < ndirs; ++j)
6532         {
6533           savehere[j] = 0;
6534
6535           if (saved[j] < dirs[i].length)
6536             {
6537               /* Determine whether the dirs[i] path is a prefix of the
6538                  dirs[j] path.  */
6539               int k;
6540
6541               k = dirs[j].prefix;
6542               while (k != -1 && k != i)
6543                 k = dirs[k].prefix;
6544
6545               if (k == i)
6546                 {
6547                   /* Yes it is.  We can possibly safe some memory but
6548                      writing the filenames in dirs[j] relative to
6549                      dirs[i].  */
6550                   savehere[j] = dirs[i].length;
6551                   total += (savehere[j] - saved[j]) * dirs[j].count;
6552                 }
6553             }
6554         }
6555
6556       /* Check whether we can safe enough to justify adding the dirs[i]
6557          directory.  */
6558       if (total > dirs[i].length + 1)
6559         {
6560           /* It's worthwhile adding.  */
6561           for (j = i; j < ndirs; ++j)
6562             if (savehere[j] > 0)
6563               {
6564                 /* Remember how much we saved for this directory so far.  */
6565                 saved[j] = savehere[j];
6566
6567                 /* Remember the prefix directory.  */
6568                 dirs[j].dir_idx = i;
6569               }
6570         }
6571     }
6572
6573   /* We have to emit them in the order they appear in the file_table
6574      array since the index is used in the debug info generation.  To
6575      do this efficiently we generate a back-mapping of the indices
6576      first.  */
6577   backmap = (int *) alloca (file_table.in_use * sizeof (int));
6578   for (i = 1; i < (int) file_table.in_use; ++i)
6579     {
6580       backmap[files[i].file_idx] = i;
6581       /* Mark this directory as used.  */
6582       dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
6583     }
6584
6585   /* That was it.  We are ready to emit the information.  First the
6586      directory name table.  Here we have to make sure that the first
6587      actually emitted directory name has the index one.  Zero is
6588      reserved for the current working directory.  Make sure we do not
6589      confuse these indices with the one for the constructed table
6590      (even though most of the time they are identical).  */
6591   idx = 1;
6592   idx_offset = dirs[0].length > 0 ? 1 : 0;
6593   for (i = 1 - idx_offset; i < ndirs; ++i)
6594     if (dirs[i].used != 0)
6595       {
6596         dirs[i].used = idx++;
6597         dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
6598                                 "Directory Entry: 0x%x", dirs[i].used);
6599       }
6600   dw2_asm_output_data (1, 0, "End directory table");
6601
6602   /* Correct the index for the current working directory entry if it
6603      exists.  */
6604   if (idx_offset == 0)
6605     dirs[0].used = 0;
6606
6607   /* Now write all the file names.  */
6608   for (i = 1; i < (int) file_table.in_use; ++i)
6609     {
6610       int file_idx = backmap[i];
6611       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
6612
6613       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
6614                               "File Entry: 0x%x", i);
6615
6616       /* Include directory index.  */
6617       dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
6618
6619       /* Modification time.  */
6620       dw2_asm_output_data_uleb128 (0, NULL);
6621
6622       /* File length in bytes.  */
6623       dw2_asm_output_data_uleb128 (0, NULL);
6624     }
6625   dw2_asm_output_data (1, 0, "End file name table");
6626 }
6627
6628
6629 /* Output the source line number correspondence information.  This
6630    information goes into the .debug_line section.  */
6631
6632 static void
6633 output_line_info ()
6634 {
6635   char l1[20], l2[20], p1[20], p2[20];
6636   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
6637   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
6638   register unsigned opc;
6639   register unsigned n_op_args;
6640   register unsigned long lt_index;
6641   register unsigned long current_line;
6642   register long line_offset;
6643   register long line_delta;
6644   register unsigned long current_file;
6645   register unsigned long function;
6646
6647   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
6648   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
6649   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
6650   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
6651
6652   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
6653                         "Length of Source Line Info");
6654   ASM_OUTPUT_LABEL (asm_out_file, l1);
6655
6656   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6657
6658   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
6659   ASM_OUTPUT_LABEL (asm_out_file, p1);
6660
6661   dw2_asm_output_data (1, DWARF_LINE_MIN_INSTR_LENGTH,
6662                        "Minimum Instruction Length");
6663
6664   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
6665                        "Default is_stmt_start flag");
6666
6667   dw2_asm_output_data (1, DWARF_LINE_BASE,
6668                        "Line Base Value (Special Opcodes)");
6669
6670   dw2_asm_output_data (1, DWARF_LINE_RANGE,
6671                        "Line Range Value (Special Opcodes)");
6672
6673   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
6674                        "Special Opcode Base");
6675
6676   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; ++opc)
6677     {
6678       switch (opc)
6679         {
6680         case DW_LNS_advance_pc:
6681         case DW_LNS_advance_line:
6682         case DW_LNS_set_file:
6683         case DW_LNS_set_column:
6684         case DW_LNS_fixed_advance_pc:
6685           n_op_args = 1;
6686           break;
6687         default:
6688           n_op_args = 0;
6689           break;
6690         }
6691
6692       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
6693                            opc, n_op_args);
6694     }
6695
6696   /* Write out the information about the files we use.  */
6697   output_file_names ();
6698   ASM_OUTPUT_LABEL (asm_out_file, p2);
6699
6700   /* We used to set the address register to the first location in the text
6701      section here, but that didn't accomplish anything since we already
6702      have a line note for the opening brace of the first function.  */
6703
6704   /* Generate the line number to PC correspondence table, encoded as
6705      a series of state machine operations.  */
6706   current_file = 1;
6707   current_line = 1;
6708   strcpy (prev_line_label, text_section_label);
6709   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
6710     {
6711       register dw_line_info_ref line_info = &line_info_table[lt_index];
6712
6713 #if 0
6714       /* Disable this optimization for now; GDB wants to see two line notes
6715          at the beginning of a function so it can find the end of the
6716          prologue.  */
6717
6718       /* Don't emit anything for redundant notes.  Just updating the
6719          address doesn't accomplish anything, because we already assume
6720          that anything after the last address is this line.  */
6721       if (line_info->dw_line_num == current_line
6722           && line_info->dw_file_num == current_file)
6723         continue;
6724 #endif
6725
6726       /* Emit debug info for the address of the current line.
6727
6728          Unfortunately, we have little choice here currently, and must always
6729          use the most general form.  Gcc does not know the address delta
6730          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
6731          attributes which will give an upper bound on the address range.  We
6732          could perhaps use length attributes to determine when it is safe to
6733          use DW_LNS_fixed_advance_pc.  */
6734
6735       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
6736       if (0)
6737         {
6738           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
6739           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
6740                                "DW_LNS_fixed_advance_pc");
6741           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
6742         }
6743       else
6744         {
6745           /* This can handle any delta.  This takes
6746              4+DWARF2_ADDR_SIZE bytes.  */
6747           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
6748           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
6749           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
6750           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
6751         }
6752       strcpy (prev_line_label, line_label);
6753
6754       /* Emit debug info for the source file of the current line, if
6755          different from the previous line.  */
6756       if (line_info->dw_file_num != current_file)
6757         {
6758           current_file = line_info->dw_file_num;
6759           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
6760           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
6761                                        file_table.table[current_file]);
6762         }
6763
6764       /* Emit debug info for the current line number, choosing the encoding
6765          that uses the least amount of space.  */
6766       if (line_info->dw_line_num != current_line)
6767         {
6768           line_offset = line_info->dw_line_num - current_line;
6769           line_delta = line_offset - DWARF_LINE_BASE;
6770           current_line = line_info->dw_line_num;
6771           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
6772             {
6773               /* This can handle deltas from -10 to 234, using the current
6774                  definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
6775                  takes 1 byte.  */
6776               dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
6777                                    "line %lu", current_line);
6778             }
6779           else
6780             {
6781               /* This can handle any delta.  This takes at least 4 bytes,
6782                  depending on the value being encoded.  */
6783               dw2_asm_output_data (1, DW_LNS_advance_line,
6784                                    "advance to line %lu", current_line);
6785               dw2_asm_output_data_sleb128 (line_offset, NULL);
6786               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
6787             }
6788         }
6789       else
6790         {
6791           /* We still need to start a new row, so output a copy insn.  */
6792           dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
6793         }
6794     }
6795
6796   /* Emit debug info for the address of the end of the function.  */
6797   if (0)
6798     {
6799       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
6800                            "DW_LNS_fixed_advance_pc");
6801       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
6802     }
6803   else
6804     {
6805       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
6806       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
6807       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
6808       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
6809     }
6810
6811   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
6812   dw2_asm_output_data_uleb128 (1, NULL);
6813   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
6814
6815   function = 0;
6816   current_file = 1;
6817   current_line = 1;
6818   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
6819     {
6820       register dw_separate_line_info_ref line_info
6821         = &separate_line_info_table[lt_index];
6822
6823 #if 0
6824       /* Don't emit anything for redundant notes.  */
6825       if (line_info->dw_line_num == current_line
6826           && line_info->dw_file_num == current_file
6827           && line_info->function == function)
6828         goto cont;
6829 #endif
6830
6831       /* Emit debug info for the address of the current line.  If this is
6832          a new function, or the first line of a function, then we need
6833          to handle it differently.  */
6834       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
6835                                    lt_index);
6836       if (function != line_info->function)
6837         {
6838           function = line_info->function;
6839
6840           /* Set the address register to the first line in the function */
6841           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
6842           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
6843           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
6844           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
6845         }
6846       else
6847         {
6848           /* ??? See the DW_LNS_advance_pc comment above.  */
6849           if (0)
6850             {
6851               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
6852                                    "DW_LNS_fixed_advance_pc");
6853               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
6854             }
6855           else
6856             {
6857               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
6858               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
6859               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
6860               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
6861             }
6862         }
6863       strcpy (prev_line_label, line_label);
6864
6865       /* Emit debug info for the source file of the current line, if
6866          different from the previous line.  */
6867       if (line_info->dw_file_num != current_file)
6868         {
6869           current_file = line_info->dw_file_num;
6870           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
6871           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
6872                                        file_table.table[current_file]);
6873         }
6874
6875       /* Emit debug info for the current line number, choosing the encoding
6876          that uses the least amount of space.  */
6877       if (line_info->dw_line_num != current_line)
6878         {
6879           line_offset = line_info->dw_line_num - current_line;
6880           line_delta = line_offset - DWARF_LINE_BASE;
6881           current_line = line_info->dw_line_num;
6882           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
6883             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
6884                                  "line %lu", current_line);
6885           else
6886             {
6887               dw2_asm_output_data (1, DW_LNS_advance_line,
6888                                    "advance to line %lu", current_line);
6889               dw2_asm_output_data_sleb128 (line_offset, NULL);
6890               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
6891             }
6892         }
6893       else
6894         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
6895
6896 #if 0
6897     cont:
6898 #endif
6899       ++lt_index;
6900
6901       /* If we're done with a function, end its sequence.  */
6902       if (lt_index == separate_line_info_table_in_use
6903           || separate_line_info_table[lt_index].function != function)
6904         {
6905           current_file = 1;
6906           current_line = 1;
6907
6908           /* Emit debug info for the address of the end of the function.  */
6909           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
6910           if (0)
6911             {
6912               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
6913                                    "DW_LNS_fixed_advance_pc");
6914               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
6915             }
6916           else
6917             {
6918               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
6919               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
6920               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
6921               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
6922             }
6923
6924           /* Output the marker for the end of this sequence.  */
6925           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
6926           dw2_asm_output_data_uleb128 (1, NULL);
6927           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
6928         }
6929     }
6930
6931   /* Output the marker for the end of the line number info.  */
6932   ASM_OUTPUT_LABEL (asm_out_file, l2);
6933 }
6934 \f
6935 /* Given a pointer to a tree node for some base type, return a pointer to
6936    a DIE that describes the given type.
6937
6938    This routine must only be called for GCC type nodes that correspond to
6939    Dwarf base (fundamental) types.  */
6940
6941 static dw_die_ref
6942 base_type_die (type)
6943      register tree type;
6944 {
6945   register dw_die_ref base_type_result;
6946   register const char *type_name;
6947   register enum dwarf_type encoding;
6948   register tree name = TYPE_NAME (type);
6949
6950   if (TREE_CODE (type) == ERROR_MARK
6951       || TREE_CODE (type) == VOID_TYPE)
6952     return 0;
6953
6954   if (name)
6955     {
6956       if (TREE_CODE (name) == TYPE_DECL)
6957         name = DECL_NAME (name);
6958
6959       type_name = IDENTIFIER_POINTER (name);
6960     }
6961   else
6962     type_name = "__unknown__";
6963
6964   switch (TREE_CODE (type))
6965     {
6966     case INTEGER_TYPE:
6967       /* Carefully distinguish the C character types, without messing
6968          up if the language is not C. Note that we check only for the names
6969          that contain spaces; other names might occur by coincidence in other
6970          languages.  */
6971       if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
6972              && (type == char_type_node
6973                  || ! strcmp (type_name, "signed char")
6974                  || ! strcmp (type_name, "unsigned char"))))
6975         {
6976           if (TREE_UNSIGNED (type))
6977             encoding = DW_ATE_unsigned;
6978           else
6979             encoding = DW_ATE_signed;
6980           break;
6981         }
6982       /* else fall through.  */
6983
6984     case CHAR_TYPE:
6985       /* GNU Pascal/Ada CHAR type.  Not used in C.  */
6986       if (TREE_UNSIGNED (type))
6987         encoding = DW_ATE_unsigned_char;
6988       else
6989         encoding = DW_ATE_signed_char;
6990       break;
6991
6992     case REAL_TYPE:
6993       encoding = DW_ATE_float;
6994       break;
6995
6996       /* Dwarf2 doesn't know anything about complex ints, so use
6997          a user defined type for it.  */
6998     case COMPLEX_TYPE:
6999       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7000         encoding = DW_ATE_complex_float;
7001       else
7002         encoding = DW_ATE_lo_user;
7003       break;
7004
7005     case BOOLEAN_TYPE:
7006       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
7007       encoding = DW_ATE_boolean;
7008       break;
7009
7010     default:
7011       abort (); /* No other TREE_CODEs are Dwarf fundamental types.  */
7012     }
7013
7014   base_type_result = new_die (DW_TAG_base_type, comp_unit_die);
7015   if (demangle_name_func)
7016     type_name = (*demangle_name_func) (type_name);
7017
7018   add_AT_string (base_type_result, DW_AT_name, type_name);
7019   add_AT_unsigned (base_type_result, DW_AT_byte_size,
7020                    int_size_in_bytes (type));
7021   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7022
7023   return base_type_result;
7024 }
7025
7026 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7027    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
7028    a given type is generally the same as the given type, except that if the
7029    given type is a pointer or reference type, then the root type of the given
7030    type is the root type of the "basis" type for the pointer or reference
7031    type.  (This definition of the "root" type is recursive.) Also, the root
7032    type of a `const' qualified type or a `volatile' qualified type is the
7033    root type of the given type without the qualifiers.  */
7034
7035 static tree
7036 root_type (type)
7037      register tree type;
7038 {
7039   if (TREE_CODE (type) == ERROR_MARK)
7040     return error_mark_node;
7041
7042   switch (TREE_CODE (type))
7043     {
7044     case ERROR_MARK:
7045       return error_mark_node;
7046
7047     case POINTER_TYPE:
7048     case REFERENCE_TYPE:
7049       return type_main_variant (root_type (TREE_TYPE (type)));
7050
7051     default:
7052       return type_main_variant (type);
7053     }
7054 }
7055
7056 /* Given a pointer to an arbitrary ..._TYPE tree node, return non-zero if the
7057    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
7058
7059 static inline int
7060 is_base_type (type)
7061      register tree type;
7062 {
7063   switch (TREE_CODE (type))
7064     {
7065     case ERROR_MARK:
7066     case VOID_TYPE:
7067     case INTEGER_TYPE:
7068     case REAL_TYPE:
7069     case COMPLEX_TYPE:
7070     case BOOLEAN_TYPE:
7071     case CHAR_TYPE:
7072       return 1;
7073
7074     case SET_TYPE:
7075     case ARRAY_TYPE:
7076     case RECORD_TYPE:
7077     case UNION_TYPE:
7078     case QUAL_UNION_TYPE:
7079     case ENUMERAL_TYPE:
7080     case FUNCTION_TYPE:
7081     case METHOD_TYPE:
7082     case POINTER_TYPE:
7083     case REFERENCE_TYPE:
7084     case FILE_TYPE:
7085     case OFFSET_TYPE:
7086     case LANG_TYPE:
7087     case VECTOR_TYPE:
7088       return 0;
7089
7090     default:
7091       abort ();
7092     }
7093
7094   return 0;
7095 }
7096
7097 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
7098    entry that chains various modifiers in front of the given type.  */
7099
7100 static dw_die_ref
7101 modified_type_die (type, is_const_type, is_volatile_type, context_die)
7102      register tree type;
7103      register int is_const_type;
7104      register int is_volatile_type;
7105      register dw_die_ref context_die;
7106 {
7107   register enum tree_code code = TREE_CODE (type);
7108   register dw_die_ref mod_type_die = NULL;
7109   register dw_die_ref sub_die = NULL;
7110   register tree item_type = NULL;
7111
7112   if (code != ERROR_MARK)
7113     {
7114       tree qualified_type;
7115
7116       /* See if we already have the appropriately qualified variant of
7117          this type.  */
7118       qualified_type 
7119         = get_qualified_type (type,
7120                               ((is_const_type ? TYPE_QUAL_CONST : 0)
7121                                | (is_volatile_type 
7122                                   ? TYPE_QUAL_VOLATILE : 0)));
7123       /* If we do, then we can just use its DIE, if it exists.  */
7124       if (qualified_type)
7125         {
7126           mod_type_die = lookup_type_die (qualified_type);
7127           if (mod_type_die)
7128             return mod_type_die;
7129         }
7130
7131       /* Handle C typedef types.  */
7132       if (qualified_type && TYPE_NAME (qualified_type) 
7133           && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
7134           && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
7135         {
7136           tree type_name = TYPE_NAME (qualified_type);
7137           tree dtype = TREE_TYPE (type_name);
7138           if (qualified_type == dtype)
7139             {
7140               /* For a named type, use the typedef.  */
7141               gen_type_die (qualified_type, context_die);
7142               mod_type_die = lookup_type_die (qualified_type);
7143             }
7144
7145           else if (is_const_type < TYPE_READONLY (dtype)
7146                    || is_volatile_type < TYPE_VOLATILE (dtype))
7147             /* cv-unqualified version of named type.  Just use the unnamed
7148                type to which it refers.  */
7149             mod_type_die
7150               = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
7151                                    is_const_type, is_volatile_type,
7152                                    context_die);
7153           /* Else cv-qualified version of named type; fall through.  */
7154         }
7155
7156       if (mod_type_die)
7157         /* OK.  */
7158         ;
7159       else if (is_const_type)
7160         {
7161           mod_type_die = new_die (DW_TAG_const_type, comp_unit_die);
7162           sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
7163         }
7164       else if (is_volatile_type)
7165         {
7166           mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die);
7167           sub_die = modified_type_die (type, 0, 0, context_die);
7168         }
7169       else if (code == POINTER_TYPE)
7170         {
7171           mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die);
7172           add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7173 #if 0
7174           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7175 #endif
7176           item_type = TREE_TYPE (type);
7177         }
7178       else if (code == REFERENCE_TYPE)
7179         {
7180           mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die);
7181           add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7182 #if 0
7183           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7184 #endif
7185           item_type = TREE_TYPE (type);
7186         }
7187       else if (is_base_type (type))
7188         mod_type_die = base_type_die (type);
7189       else
7190         {
7191           gen_type_die (type, context_die);
7192
7193           /* We have to get the type_main_variant here (and pass that to the
7194              `lookup_type_die' routine) because the ..._TYPE node we have
7195              might simply be a *copy* of some original type node (where the
7196              copy was created to help us keep track of typedef names) and
7197              that copy might have a different TYPE_UID from the original
7198              ..._TYPE node.  */
7199           mod_type_die = lookup_type_die (type_main_variant (type));
7200           if (mod_type_die == NULL)
7201             abort ();
7202         }
7203
7204       /* We want to equate the qualified type to the die below.  */
7205       if (qualified_type)
7206         type = qualified_type;
7207     }
7208
7209   equate_type_number_to_die (type, mod_type_die);
7210   if (item_type)
7211     /* We must do this after the equate_type_number_to_die call, in case
7212        this is a recursive type.  This ensures that the modified_type_die
7213        recursion will terminate even if the type is recursive.  Recursive
7214        types are possible in Ada.  */
7215     sub_die = modified_type_die (item_type,
7216                                  TYPE_READONLY (item_type),
7217                                  TYPE_VOLATILE (item_type),
7218                                  context_die);
7219
7220   if (sub_die != NULL)
7221     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
7222
7223   return mod_type_die;
7224 }
7225
7226 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
7227    an enumerated type.   */
7228
7229 static inline int
7230 type_is_enum (type)
7231      register tree type;
7232 {
7233   return TREE_CODE (type) == ENUMERAL_TYPE;
7234 }
7235
7236 /* Return the register number described by a given RTL node.  */
7237
7238 static unsigned int
7239 reg_number (rtl)
7240      register rtx rtl;
7241 {
7242   register unsigned regno = REGNO (rtl);
7243
7244   if (regno >= FIRST_PSEUDO_REGISTER)
7245     {
7246       warning ("internal regno botch: regno = %d\n", regno);
7247       regno = 0;
7248     }
7249
7250   regno = DBX_REGISTER_NUMBER (regno);
7251   return regno;
7252 }
7253
7254 /* Return a location descriptor that designates a machine register.  */
7255
7256 static dw_loc_descr_ref
7257 reg_loc_descriptor (rtl)
7258      register rtx rtl;
7259 {
7260   register dw_loc_descr_ref loc_result = NULL;
7261   register unsigned reg = reg_number (rtl);
7262
7263   if (reg <= 31)
7264     loc_result = new_loc_descr (DW_OP_reg0 + reg, 0, 0);
7265   else
7266     loc_result = new_loc_descr (DW_OP_regx, reg, 0);
7267
7268   return loc_result;
7269 }
7270
7271 /* Return a location descriptor that designates a constant.  */
7272
7273 static dw_loc_descr_ref
7274 int_loc_descriptor (i)
7275      HOST_WIDE_INT i;
7276 {
7277   enum dwarf_location_atom op;
7278
7279   /* Pick the smallest representation of a constant, rather than just
7280      defaulting to the LEB encoding.  */
7281   if (i >= 0)
7282     {
7283       if (i <= 31)
7284         op = DW_OP_lit0 + i;
7285       else if (i <= 0xff)
7286         op = DW_OP_const1u;
7287       else if (i <= 0xffff)
7288         op = DW_OP_const2u;
7289       else if (HOST_BITS_PER_WIDE_INT == 32
7290                || i <= 0xffffffff)
7291         op = DW_OP_const4u;
7292       else
7293         op = DW_OP_constu;
7294     }
7295   else
7296     {
7297       if (i >= -0x80)
7298         op = DW_OP_const1s;
7299       else if (i >= -0x8000)
7300         op = DW_OP_const2s;
7301       else if (HOST_BITS_PER_WIDE_INT == 32
7302                || i >= -0x80000000)
7303         op = DW_OP_const4s;
7304       else
7305         op = DW_OP_consts;
7306     }
7307
7308   return new_loc_descr (op, i, 0);
7309 }
7310
7311 /* Return a location descriptor that designates a base+offset location.  */
7312
7313 static dw_loc_descr_ref
7314 based_loc_descr (reg, offset)
7315      unsigned reg;
7316      long int offset;
7317 {
7318   register dw_loc_descr_ref loc_result;
7319   /* For the "frame base", we use the frame pointer or stack pointer
7320      registers, since the RTL for local variables is relative to one of
7321      them.  */
7322   register unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
7323                                                   ? HARD_FRAME_POINTER_REGNUM
7324                                                   : STACK_POINTER_REGNUM);
7325
7326   if (reg == fp_reg)
7327     loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
7328   else if (reg <= 31)
7329     loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
7330   else
7331     loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
7332
7333   return loc_result;
7334 }
7335
7336 /* Return true if this RTL expression describes a base+offset calculation.  */
7337
7338 static inline int
7339 is_based_loc (rtl)
7340      register rtx rtl;
7341 {
7342     return (GET_CODE (rtl) == PLUS
7343             && ((GET_CODE (XEXP (rtl, 0)) == REG
7344                  && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
7345 }
7346
7347 /* The following routine converts the RTL for a variable or parameter
7348    (resident in memory) into an equivalent Dwarf representation of a
7349    mechanism for getting the address of that same variable onto the top of a
7350    hypothetical "address evaluation" stack.
7351
7352    When creating memory location descriptors, we are effectively transforming
7353    the RTL for a memory-resident object into its Dwarf postfix expression
7354    equivalent.  This routine recursively descends an RTL tree, turning
7355    it into Dwarf postfix code as it goes.
7356
7357    MODE is the mode of the memory reference, needed to handle some
7358    autoincrement addressing modes.  */
7359
7360 static dw_loc_descr_ref
7361 mem_loc_descriptor (rtl, mode)
7362      register rtx rtl;
7363      enum machine_mode mode;
7364 {
7365   dw_loc_descr_ref mem_loc_result = NULL;
7366   /* Note that for a dynamically sized array, the location we will generate a
7367      description of here will be the lowest numbered location which is
7368      actually within the array.  That's *not* necessarily the same as the
7369      zeroth element of the array.  */
7370
7371 #ifdef ASM_SIMPLIFY_DWARF_ADDR
7372   rtl = ASM_SIMPLIFY_DWARF_ADDR (rtl);
7373 #endif
7374
7375   switch (GET_CODE (rtl))
7376     {
7377     case POST_INC:
7378     case POST_DEC:
7379     case POST_MODIFY:
7380       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
7381          just fall into the SUBREG code.  */
7382
7383       /* Fall through.  */
7384
7385     case SUBREG:
7386       /* The case of a subreg may arise when we have a local (register)
7387          variable or a formal (register) parameter which doesn't quite fill
7388          up an entire register.  For now, just assume that it is
7389          legitimate to make the Dwarf info refer to the whole register which
7390          contains the given subreg.  */
7391       rtl = SUBREG_REG (rtl);
7392
7393       /* Fall through.  */
7394
7395     case REG:
7396       /* Whenever a register number forms a part of the description of the
7397          method for calculating the (dynamic) address of a memory resident
7398          object, DWARF rules require the register number be referred to as
7399          a "base register".  This distinction is not based in any way upon
7400          what category of register the hardware believes the given register
7401          belongs to.  This is strictly DWARF terminology we're dealing with
7402          here. Note that in cases where the location of a memory-resident
7403          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
7404          OP_CONST (0)) the actual DWARF location descriptor that we generate
7405          may just be OP_BASEREG (basereg).  This may look deceptively like
7406          the object in question was allocated to a register (rather than in
7407          memory) so DWARF consumers need to be aware of the subtle
7408          distinction between OP_REG and OP_BASEREG.  */
7409       mem_loc_result = based_loc_descr (reg_number (rtl), 0);
7410       break;
7411
7412     case MEM:
7413       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7414       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
7415       break;
7416
7417     case LABEL_REF:
7418       /* Some ports can transform a symbol ref into a label ref, because
7419          the symbol ref is too far away and has to be dumped into a constant
7420          pool.  */
7421     case CONST:
7422     case SYMBOL_REF:
7423       /* Alternatively, the symbol in the constant pool can be referenced
7424          by a different symbol.  */
7425       if (GET_CODE (rtl) == SYMBOL_REF
7426           && CONSTANT_POOL_ADDRESS_P (rtl))
7427         {
7428           rtx tmp = get_pool_constant (rtl);
7429           /* Doesn't work for floating point constants.  */
7430           if (! (GET_CODE (tmp) == CONST_DOUBLE && GET_MODE (tmp) != VOIDmode))
7431             rtl = tmp;
7432         }
7433
7434
7435       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
7436       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
7437       mem_loc_result->dw_loc_oprnd1.v.val_addr = save_rtx (rtl);
7438       break;
7439
7440     case PRE_MODIFY:
7441       /* Extract the PLUS expression nested inside and fall into
7442          PLUS code bellow.  */
7443       rtl = XEXP (rtl, 1);
7444       goto plus;
7445
7446     case PRE_INC:
7447     case PRE_DEC:
7448       /* Turn these into a PLUS expression and fall into the PLUS code
7449          below.  */
7450       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
7451                           GEN_INT (GET_CODE (rtl) == PRE_INC
7452                                    ? GET_MODE_UNIT_SIZE (mode)
7453                                    : -GET_MODE_UNIT_SIZE (mode)));
7454
7455       /* Fall through.  */
7456
7457     case PLUS:
7458     plus:
7459       if (is_based_loc (rtl))
7460         mem_loc_result = based_loc_descr (reg_number (XEXP (rtl, 0)),
7461                                           INTVAL (XEXP (rtl, 1)));
7462       else
7463         {
7464           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
7465
7466           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
7467               && INTVAL (XEXP (rtl, 1)) >= 0)
7468             {
7469               add_loc_descr (&mem_loc_result,
7470                              new_loc_descr (DW_OP_plus_uconst,
7471                                             INTVAL (XEXP (rtl, 1)), 0));
7472             }
7473           else
7474             {
7475               add_loc_descr (&mem_loc_result,
7476                              mem_loc_descriptor (XEXP (rtl, 1), mode));
7477               add_loc_descr (&mem_loc_result,
7478                              new_loc_descr (DW_OP_plus, 0, 0));
7479             }
7480         }
7481       break;
7482
7483     case MULT:
7484       /* If a pseudo-reg is optimized away, it is possible for it to
7485          be replaced with a MEM containing a multiply.  */
7486       add_loc_descr (&mem_loc_result,
7487                      mem_loc_descriptor (XEXP (rtl, 0), mode));
7488       add_loc_descr (&mem_loc_result,
7489                      mem_loc_descriptor (XEXP (rtl, 1), mode));
7490       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
7491       break;
7492
7493     case CONST_INT:
7494       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
7495       break;
7496
7497     default:
7498       abort ();
7499     }
7500
7501   return mem_loc_result;
7502 }
7503
7504 /* Return a descriptor that describes the concatenation of two locations.
7505    This is typically a complex variable.  */
7506
7507 static dw_loc_descr_ref
7508 concat_loc_descriptor (x0, x1)
7509      register rtx x0, x1;
7510 {
7511   dw_loc_descr_ref cc_loc_result = NULL;
7512
7513   if (!is_pseudo_reg (x0)
7514       && (GET_CODE (x0) != MEM || !is_pseudo_reg (XEXP (x0, 0))))
7515     add_loc_descr (&cc_loc_result, loc_descriptor (x0));
7516   add_loc_descr (&cc_loc_result,
7517                  new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x0)), 0));
7518
7519   if (!is_pseudo_reg (x1)
7520       && (GET_CODE (x1) != MEM || !is_pseudo_reg (XEXP (x1, 0))))
7521     add_loc_descr (&cc_loc_result, loc_descriptor (x1));
7522   add_loc_descr (&cc_loc_result,
7523                  new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x1)), 0));
7524
7525   return cc_loc_result;
7526 }
7527
7528 /* Output a proper Dwarf location descriptor for a variable or parameter
7529    which is either allocated in a register or in a memory location.  For a
7530    register, we just generate an OP_REG and the register number.  For a
7531    memory location we provide a Dwarf postfix expression describing how to
7532    generate the (dynamic) address of the object onto the address stack.  */
7533
7534 static dw_loc_descr_ref
7535 loc_descriptor (rtl)
7536      register rtx rtl;
7537 {
7538   dw_loc_descr_ref loc_result = NULL;
7539   switch (GET_CODE (rtl))
7540     {
7541     case SUBREG:
7542       /* The case of a subreg may arise when we have a local (register)
7543          variable or a formal (register) parameter which doesn't quite fill
7544          up an entire register.  For now, just assume that it is
7545          legitimate to make the Dwarf info refer to the whole register which
7546          contains the given subreg.  */
7547       rtl = SUBREG_REG (rtl);
7548
7549       /* Fall through.  */
7550
7551     case REG:
7552       loc_result = reg_loc_descriptor (rtl);
7553       break;
7554
7555     case MEM:
7556       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7557       break;
7558
7559     case CONCAT:
7560       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
7561       break;
7562
7563     default:
7564       abort ();
7565     }
7566
7567   return loc_result;
7568 }
7569
7570 /* Similar, but generate the descriptor from trees instead of rtl.
7571    This comes up particularly with variable length arrays.  */
7572
7573 static dw_loc_descr_ref
7574 loc_descriptor_from_tree (loc, addressp)
7575      tree loc;
7576      int addressp;
7577 {
7578   dw_loc_descr_ref ret = NULL;
7579   int indirect_size = 0;
7580   int unsignedp = TREE_UNSIGNED (TREE_TYPE (loc));
7581   enum dwarf_location_atom op;
7582
7583   /* ??? Most of the time we do not take proper care for sign/zero
7584      extending the values properly.  Hopefully this won't be a real
7585      problem...  */
7586
7587   switch (TREE_CODE (loc))
7588     {
7589     case ERROR_MARK:
7590       break;
7591
7592     case WITH_RECORD_EXPR:
7593       /* This case involves extracting fields from an object to determine the
7594          position of other fields.  We don't try to encode this here.  The
7595          only user of this is Ada, which encodes the needed information using
7596          the names of types.  */
7597       return ret;
7598
7599     case VAR_DECL:
7600     case PARM_DECL:
7601       {
7602         rtx rtl = rtl_for_decl_location (loc);
7603         enum machine_mode mode = DECL_MODE (loc);
7604
7605         if (rtl == NULL_RTX)
7606           break;
7607         else if (CONSTANT_P (rtl))
7608           {
7609             ret = new_loc_descr (DW_OP_addr, 0, 0);
7610             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
7611             ret->dw_loc_oprnd1.v.val_addr = rtl;
7612             indirect_size = GET_MODE_SIZE (mode);
7613           }
7614         else
7615           {
7616             if (GET_CODE (rtl) == MEM)
7617               {
7618                 indirect_size = GET_MODE_SIZE (mode);
7619                 rtl = XEXP (rtl, 0);
7620               }
7621             ret = mem_loc_descriptor (rtl, mode);
7622           }
7623       }
7624       break;
7625
7626     case INDIRECT_REF:
7627       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7628       indirect_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)));
7629       break;
7630
7631     case NOP_EXPR:
7632     case CONVERT_EXPR:
7633     case NON_LVALUE_EXPR:
7634     case SAVE_EXPR:
7635       return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
7636
7637     case COMPONENT_REF:
7638     case BIT_FIELD_REF:
7639     case ARRAY_REF:
7640     case ARRAY_RANGE_REF:
7641       {
7642         tree obj, offset;
7643         HOST_WIDE_INT bitsize, bitpos, bytepos;
7644         enum machine_mode mode;
7645         int volatilep;
7646         unsigned int alignment;
7647
7648         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
7649                                    &unsignedp, &volatilep, &alignment);
7650         ret = loc_descriptor_from_tree (obj, 1);
7651
7652         if (offset != NULL_TREE)
7653           {
7654             /* Variable offset.  */
7655             add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
7656             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
7657           }
7658
7659         if (addressp)
7660           {
7661             /* We cannot address anything not on a unit boundary.  */
7662             if (bitpos % BITS_PER_UNIT != 0)
7663               abort ();
7664           }
7665         else
7666           {
7667             if (bitpos % BITS_PER_UNIT != 0
7668                 || bitsize % BITS_PER_UNIT != 0)
7669               {
7670                 /* ??? We could handle this by loading and shifting etc.
7671                    Wait until someone needs it before expending the effort.  */
7672                 abort ();
7673               }
7674
7675             indirect_size = bitsize / BITS_PER_UNIT;
7676           }
7677
7678         bytepos = bitpos / BITS_PER_UNIT;
7679         if (bytepos > 0)
7680           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
7681         else if (bytepos < 0)
7682           {
7683             add_loc_descr (&ret, int_loc_descriptor (bytepos));
7684             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
7685           }
7686         break;
7687       }
7688
7689     case INTEGER_CST:
7690       if (host_integerp (loc, 0))
7691         ret = int_loc_descriptor (tree_low_cst (loc, 0));
7692       break;
7693
7694     case BIT_AND_EXPR:
7695       op = DW_OP_and;
7696       goto do_binop;
7697     case BIT_XOR_EXPR:
7698       op = DW_OP_xor;
7699       goto do_binop;
7700     case BIT_IOR_EXPR:
7701       op = DW_OP_or;
7702       goto do_binop;
7703     case TRUNC_DIV_EXPR:
7704       op = DW_OP_div;
7705       goto do_binop;
7706     case MINUS_EXPR:
7707       op = DW_OP_minus;
7708       goto do_binop;
7709     case TRUNC_MOD_EXPR:
7710       op = DW_OP_mod;
7711       goto do_binop;
7712     case MULT_EXPR:
7713       op = DW_OP_mul;
7714       goto do_binop;
7715     case LSHIFT_EXPR:
7716       op = DW_OP_shl;
7717       goto do_binop;
7718     case RSHIFT_EXPR:
7719       op = (unsignedp ? DW_OP_shr : DW_OP_shra);
7720       goto do_binop;
7721     case PLUS_EXPR:
7722       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
7723           && host_integerp (TREE_OPERAND (loc, 1), 0))
7724         {
7725           ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7726           add_loc_descr (&ret,
7727                          new_loc_descr (DW_OP_plus_uconst,
7728                                         tree_low_cst (TREE_OPERAND (loc, 1),
7729                                                       0),
7730                                         0));
7731           break;
7732         }
7733       op = DW_OP_plus;
7734       goto do_binop;
7735     case LE_EXPR:
7736       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
7737         break;
7738       op = DW_OP_le;
7739       goto do_binop;
7740     case GE_EXPR:
7741       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
7742         break;
7743       op = DW_OP_ge;
7744       goto do_binop;
7745     case LT_EXPR:
7746       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
7747         break;
7748       op = DW_OP_lt;
7749       goto do_binop;
7750     case GT_EXPR:
7751       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
7752         break;
7753       op = DW_OP_gt;
7754       goto do_binop;
7755     case EQ_EXPR:
7756       op = DW_OP_eq;
7757       goto do_binop;
7758     case NE_EXPR:
7759       op = DW_OP_ne;
7760       goto do_binop;
7761
7762     do_binop:
7763       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7764       add_loc_descr (&ret, loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0));
7765       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
7766       break;
7767
7768     case BIT_NOT_EXPR:
7769       op = DW_OP_not;
7770       goto do_unop;
7771     case ABS_EXPR:
7772       op = DW_OP_abs;
7773       goto do_unop;
7774     case NEGATE_EXPR:
7775       op = DW_OP_neg;
7776       goto do_unop;
7777
7778     do_unop:
7779       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7780       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
7781       break;
7782
7783     case MAX_EXPR:
7784       loc = build (COND_EXPR, TREE_TYPE (loc),
7785                    build (LT_EXPR, integer_type_node,
7786                           TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
7787                    TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
7788       /* FALLTHRU */
7789
7790     case COND_EXPR:
7791       {
7792         dw_loc_descr_ref bra_node, jump_node, tmp;
7793
7794         ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7795         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
7796         add_loc_descr (&ret, bra_node);
7797
7798         tmp = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
7799         add_loc_descr (&ret, tmp);
7800         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
7801         add_loc_descr (&ret, jump_node);
7802
7803         tmp = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
7804         add_loc_descr (&ret, tmp);
7805         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
7806         bra_node->dw_loc_oprnd1.v.val_loc = tmp;
7807
7808         /* ??? Need a node to point the skip at.  Use a nop.  */
7809         tmp = new_loc_descr (DW_OP_nop, 0, 0);
7810         add_loc_descr (&ret, tmp);
7811         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
7812         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
7813       }
7814       break;
7815
7816     default:
7817       abort ();
7818     }
7819
7820   /* If we can't fill the request for an address, die.  */
7821   if (addressp && indirect_size == 0)
7822     abort ();
7823
7824   /* If we've got an address and don't want one, dereference.  */
7825   if (!addressp && indirect_size > 0)
7826     {
7827       if (indirect_size > DWARF2_ADDR_SIZE)
7828         abort ();
7829       if (indirect_size == DWARF2_ADDR_SIZE)
7830         op = DW_OP_deref;
7831       else
7832         op = DW_OP_deref_size;
7833       add_loc_descr (&ret, new_loc_descr (op, indirect_size, 0));
7834     }
7835
7836   return ret;
7837 }
7838
7839 /* Given a value, round it up to the lowest multiple of `boundary'
7840    which is not less than the value itself.  */
7841
7842 static inline HOST_WIDE_INT
7843 ceiling (value, boundary)
7844      HOST_WIDE_INT value;
7845      unsigned int boundary;
7846 {
7847   return (((value + boundary - 1) / boundary) * boundary);
7848 }
7849
7850 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
7851    pointer to the declared type for the relevant field variable, or return
7852    `integer_type_node' if the given node turns out to be an
7853    ERROR_MARK node.  */
7854
7855 static inline tree
7856 field_type (decl)
7857      register tree decl;
7858 {
7859   register tree type;
7860
7861   if (TREE_CODE (decl) == ERROR_MARK)
7862     return integer_type_node;
7863
7864   type = DECL_BIT_FIELD_TYPE (decl);
7865   if (type == NULL_TREE)
7866     type = TREE_TYPE (decl);
7867
7868   return type;
7869 }
7870
7871 /* Given a pointer to a tree node, return the alignment in bits for
7872    it, or else return BITS_PER_WORD if the node actually turns out to
7873    be an ERROR_MARK node.  */
7874
7875 static inline unsigned
7876 simple_type_align_in_bits (type)
7877      register tree type;
7878 {
7879   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
7880 }
7881
7882 static inline unsigned
7883 simple_decl_align_in_bits (decl)
7884      register tree decl;
7885 {
7886   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
7887 }
7888
7889 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
7890    node, return the size in bits for the type if it is a constant, or else
7891    return the alignment for the type if the type's size is not constant, or
7892    else return BITS_PER_WORD if the type actually turns out to be an
7893    ERROR_MARK node.  */
7894
7895 static inline unsigned HOST_WIDE_INT
7896 simple_type_size_in_bits (type)
7897      register tree type;
7898 {
7899   tree type_size_tree;
7900
7901   if (TREE_CODE (type) == ERROR_MARK)
7902     return BITS_PER_WORD;
7903   type_size_tree = TYPE_SIZE (type);
7904
7905   if (type_size_tree == NULL_TREE)
7906     return 0;
7907   if (! host_integerp (type_size_tree, 1))
7908     return TYPE_ALIGN (type);
7909   return tree_low_cst (type_size_tree, 1);
7910 }
7911
7912 /* Given a pointer to what is assumed to be a FIELD_DECL node, compute and
7913    return the byte offset of the lowest addressed byte of the "containing
7914    object" for the given FIELD_DECL, or return 0 if we are unable to
7915    determine what that offset is, either because the argument turns out to
7916    be a pointer to an ERROR_MARK node, or because the offset is actually
7917    variable.  (We can't handle the latter case just yet).  */
7918
7919 static HOST_WIDE_INT
7920 field_byte_offset (decl)
7921      register tree decl;
7922 {
7923   unsigned int type_align_in_bits;
7924   unsigned int decl_align_in_bits;
7925   unsigned HOST_WIDE_INT type_size_in_bits;
7926   HOST_WIDE_INT object_offset_in_bits;
7927   HOST_WIDE_INT object_offset_in_bytes;
7928   tree type;
7929   tree field_size_tree;
7930   HOST_WIDE_INT bitpos_int;
7931   HOST_WIDE_INT deepest_bitpos;
7932   unsigned HOST_WIDE_INT field_size_in_bits;
7933
7934   if (TREE_CODE (decl) == ERROR_MARK)
7935     return 0;
7936
7937   if (TREE_CODE (decl) != FIELD_DECL)
7938     abort ();
7939
7940   type = field_type (decl);
7941   field_size_tree = DECL_SIZE (decl);
7942
7943   /* The size could be unspecified if there was an error, or for
7944      a flexible array member.  */
7945   if (! field_size_tree)
7946     field_size_tree = bitsize_zero_node;
7947
7948   /* We cannot yet cope with fields whose positions are variable, so
7949      for now, when we see such things, we simply return 0.  Someday, we may
7950      be able to handle such cases, but it will be damn difficult.  */
7951   if (! host_integerp (bit_position (decl), 0))
7952     return 0;
7953
7954   bitpos_int = int_bit_position (decl);
7955
7956   /* If we don't know the size of the field, pretend it's a full word.  */
7957   if (host_integerp (field_size_tree, 1))
7958     field_size_in_bits = tree_low_cst (field_size_tree, 1);
7959   else
7960     field_size_in_bits = BITS_PER_WORD;
7961
7962   type_size_in_bits = simple_type_size_in_bits (type);
7963   type_align_in_bits = simple_type_align_in_bits (type);
7964   decl_align_in_bits = simple_decl_align_in_bits (decl);
7965
7966   /* Note that the GCC front-end doesn't make any attempt to keep track of
7967      the starting bit offset (relative to the start of the containing
7968      structure type) of the hypothetical "containing object" for a bit-
7969      field.  Thus, when computing the byte offset value for the start of the
7970      "containing object" of a bit-field, we must deduce this information on
7971      our own. This can be rather tricky to do in some cases.  For example,
7972      handling the following structure type definition when compiling for an
7973      i386/i486 target (which only aligns long long's to 32-bit boundaries)
7974      can be very tricky:
7975
7976          struct S { int field1; long long field2:31; };
7977
7978      Fortunately, there is a simple rule-of-thumb which can be
7979      used in such cases.  When compiling for an i386/i486, GCC will allocate
7980      8 bytes for the structure shown above.  It decides to do this based upon
7981      one simple rule for bit-field allocation.  Quite simply, GCC allocates
7982      each "containing object" for each bit-field at the first (i.e. lowest
7983      addressed) legitimate alignment boundary (based upon the required
7984      minimum alignment for the declared type of the field) which it can
7985      possibly use, subject to the condition that there is still enough
7986      available space remaining in the containing object (when allocated at
7987      the selected point) to fully accommodate all of the bits of the
7988      bit-field itself.  This simple rule makes it obvious why GCC allocates
7989      8 bytes for each object of the structure type shown above.  When looking
7990      for a place to allocate the "containing object" for `field2', the
7991      compiler simply tries to allocate a 64-bit "containing object" at each
7992      successive 32-bit boundary (starting at zero) until it finds a place to
7993      allocate that 64- bit field such that at least 31 contiguous (and
7994      previously unallocated) bits remain within that selected 64 bit field.
7995      (As it turns out, for the example above, the compiler finds that it is
7996      OK to allocate the "containing object" 64-bit field at bit-offset zero
7997      within the structure type.) Here we attempt to work backwards from the
7998      limited set of facts we're given, and we try to deduce from those facts,
7999      where GCC must have believed that the containing object started (within
8000      the structure type). The value we deduce is then used (by the callers of
8001      this routine) to generate DW_AT_location and DW_AT_bit_offset attributes
8002      for fields (both bit-fields and, in the case of DW_AT_location, regular
8003      fields as well).  */
8004
8005   /* Figure out the bit-distance from the start of the structure to the
8006      "deepest" bit of the bit-field.  */
8007   deepest_bitpos = bitpos_int + field_size_in_bits;
8008
8009   /* This is the tricky part.  Use some fancy footwork to deduce where the
8010      lowest addressed bit of the containing object must be.  */
8011   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8012
8013   /* Round up to type_align by default.  This works best for bitfields.  */
8014   object_offset_in_bits += type_align_in_bits - 1;
8015   object_offset_in_bits /= type_align_in_bits;
8016   object_offset_in_bits *= type_align_in_bits;
8017
8018   if (object_offset_in_bits > bitpos_int)
8019     {
8020       /* Sigh, the decl must be packed.  */
8021       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8022
8023       /* Round up to decl_align instead.  */
8024       object_offset_in_bits += decl_align_in_bits - 1;
8025       object_offset_in_bits /= decl_align_in_bits;
8026       object_offset_in_bits *= decl_align_in_bits;
8027     }
8028
8029   object_offset_in_bytes = object_offset_in_bits / BITS_PER_UNIT;
8030
8031   return object_offset_in_bytes;
8032 }
8033 \f
8034 /* The following routines define various Dwarf attributes and any data
8035    associated with them.  */
8036
8037 /* Add a location description attribute value to a DIE.
8038
8039    This emits location attributes suitable for whole variables and
8040    whole parameters.  Note that the location attributes for struct fields are
8041    generated by the routine `data_member_location_attribute' below.  */
8042
8043 static void
8044 add_AT_location_description (die, attr_kind, rtl)
8045      dw_die_ref die;
8046      enum dwarf_attribute attr_kind;
8047      register rtx rtl;
8048 {
8049   /* Handle a special case.  If we are about to output a location descriptor
8050      for a variable or parameter which has been optimized out of existence,
8051      don't do that.  A variable which has been optimized out
8052      of existence will have a DECL_RTL value which denotes a pseudo-reg.
8053      Currently, in some rare cases, variables can have DECL_RTL values which
8054      look like (MEM (REG pseudo-reg#)).  These cases are due to bugs
8055      elsewhere in the compiler.  We treat such cases as if the variable(s) in
8056      question had been optimized out of existence.  */
8057
8058   if (is_pseudo_reg (rtl)
8059       || (GET_CODE (rtl) == MEM
8060           && is_pseudo_reg (XEXP (rtl, 0)))
8061       /* This can happen for a PARM_DECL with a DECL_INCOMING_RTL which
8062          references the internal argument pointer (a pseudo) in a function
8063          where all references to the internal argument pointer were
8064          eliminated via the optimizers.  */
8065       || (GET_CODE (rtl) == MEM
8066           && GET_CODE (XEXP (rtl, 0)) == PLUS
8067           && is_pseudo_reg (XEXP (XEXP (rtl, 0), 0)))
8068       || (GET_CODE (rtl) == CONCAT
8069           && is_pseudo_reg (XEXP (rtl, 0))
8070           && is_pseudo_reg (XEXP (rtl, 1))))
8071     return;
8072
8073   add_AT_loc (die, attr_kind, loc_descriptor (rtl));
8074 }
8075
8076 /* Attach the specialized form of location attribute used for data
8077    members of struct and union types.  In the special case of a
8078    FIELD_DECL node which represents a bit-field, the "offset" part
8079    of this special location descriptor must indicate the distance
8080    in bytes from the lowest-addressed byte of the containing struct
8081    or union type to the lowest-addressed byte of the "containing
8082    object" for the bit-field.  (See the `field_byte_offset' function
8083    above).. For any given bit-field, the "containing object" is a
8084    hypothetical object (of some integral or enum type) within which
8085    the given bit-field lives.  The type of this hypothetical
8086    "containing object" is always the same as the declared type of
8087    the individual bit-field itself (for GCC anyway... the DWARF
8088    spec doesn't actually mandate this).  Note that it is the size
8089    (in bytes) of the hypothetical "containing object" which will
8090    be given in the DW_AT_byte_size attribute for this bit-field.
8091    (See the `byte_size_attribute' function below.)  It is also used
8092    when calculating the value of the DW_AT_bit_offset attribute.
8093    (See the `bit_offset_attribute' function below).  */
8094
8095 static void
8096 add_data_member_location_attribute (die, decl)
8097      register dw_die_ref die;
8098      register tree decl;
8099 {
8100   register unsigned long offset;
8101   register dw_loc_descr_ref loc_descr;
8102   register enum dwarf_location_atom op;
8103
8104   if (TREE_CODE (decl) == TREE_VEC)
8105     offset = tree_low_cst (BINFO_OFFSET (decl), 0);
8106   else
8107     offset = field_byte_offset (decl);
8108
8109   /* The DWARF2 standard says that we should assume that the structure address
8110      is already on the stack, so we can specify a structure field address
8111      by using DW_OP_plus_uconst.  */
8112
8113 #ifdef MIPS_DEBUGGING_INFO
8114   /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst operator
8115      correctly.  It works only if we leave the offset on the stack.  */
8116   op = DW_OP_constu;
8117 #else
8118   op = DW_OP_plus_uconst;
8119 #endif
8120
8121   loc_descr = new_loc_descr (op, offset, 0);
8122   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
8123 }
8124
8125 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
8126    does not have a "location" either in memory or in a register.  These
8127    things can arise in GNU C when a constant is passed as an actual parameter
8128    to an inlined function.  They can also arise in C++ where declared
8129    constants do not necessarily get memory "homes".  */
8130
8131 static void
8132 add_const_value_attribute (die, rtl)
8133      register dw_die_ref die;
8134      register rtx rtl;
8135 {
8136   switch (GET_CODE (rtl))
8137     {
8138     case CONST_INT:
8139       /* Note that a CONST_INT rtx could represent either an integer
8140          or a floating-point constant.  A CONST_INT is used whenever
8141          the constant will fit into a single word.  In all such
8142          cases, the original mode of the constant value is wiped
8143          out, and the CONST_INT rtx is assigned VOIDmode.  */
8144       {
8145         HOST_WIDE_INT val = INTVAL (rtl);
8146         
8147         /* ??? We really should be using HOST_WIDE_INT throughout.  */
8148         if (val < 0)
8149           {
8150             if ((long) val != val)
8151               abort ();
8152             add_AT_int (die, DW_AT_const_value, (long) val);
8153           }
8154         else
8155           {
8156             if ((unsigned long) val != (unsigned HOST_WIDE_INT) val)
8157               abort ();
8158             add_AT_int (die, DW_AT_const_value, (unsigned long) val);
8159           }
8160       }
8161       break;
8162
8163     case CONST_DOUBLE:
8164       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
8165          floating-point constant.  A CONST_DOUBLE is used whenever the
8166          constant requires more than one word in order to be adequately
8167          represented.  We output CONST_DOUBLEs as blocks.  */
8168       {
8169         register enum machine_mode mode = GET_MODE (rtl);
8170
8171         if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8172           {
8173             register unsigned length = GET_MODE_SIZE (mode) / 4;
8174             long *array = (long *) xmalloc (sizeof (long) * length);
8175             REAL_VALUE_TYPE rv;
8176
8177             REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
8178             switch (mode)
8179               {
8180               case SFmode:
8181                 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
8182                 break;
8183
8184               case DFmode:
8185                 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
8186                 break;
8187
8188               case XFmode:
8189               case TFmode:
8190                 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
8191                 break;
8192
8193               default:
8194                 abort ();
8195               }
8196
8197             add_AT_float (die, DW_AT_const_value, length, array);
8198           }
8199         else
8200           {
8201             /* ??? We really should be using HOST_WIDE_INT throughout.  */
8202             if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
8203               abort ();
8204             add_AT_long_long (die, DW_AT_const_value,
8205                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
8206           }
8207       }
8208       break;
8209
8210     case CONST_STRING:
8211       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
8212       break;
8213
8214     case SYMBOL_REF:
8215     case LABEL_REF:
8216     case CONST:
8217       add_AT_addr (die, DW_AT_const_value, save_rtx (rtl));
8218       break;
8219
8220     case PLUS:
8221       /* In cases where an inlined instance of an inline function is passed
8222          the address of an `auto' variable (which is local to the caller) we
8223          can get a situation where the DECL_RTL of the artificial local
8224          variable (for the inlining) which acts as a stand-in for the
8225          corresponding formal parameter (of the inline function) will look
8226          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
8227          exactly a compile-time constant expression, but it isn't the address
8228          of the (artificial) local variable either.  Rather, it represents the
8229          *value* which the artificial local variable always has during its
8230          lifetime.  We currently have no way to represent such quasi-constant
8231          values in Dwarf, so for now we just punt and generate nothing.  */
8232       break;
8233
8234     default:
8235       /* No other kinds of rtx should be possible here.  */
8236       abort ();
8237     }
8238
8239 }
8240
8241 static rtx
8242 rtl_for_decl_location (decl)
8243      tree decl;
8244 {
8245   register rtx rtl;
8246
8247   /* Here we have to decide where we are going to say the parameter "lives"
8248      (as far as the debugger is concerned).  We only have a couple of
8249      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
8250
8251      DECL_RTL normally indicates where the parameter lives during most of the
8252      activation of the function.  If optimization is enabled however, this
8253      could be either NULL or else a pseudo-reg.  Both of those cases indicate
8254      that the parameter doesn't really live anywhere (as far as the code
8255      generation parts of GCC are concerned) during most of the function's
8256      activation.  That will happen (for example) if the parameter is never
8257      referenced within the function.
8258
8259      We could just generate a location descriptor here for all non-NULL
8260      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
8261      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
8262      where DECL_RTL is NULL or is a pseudo-reg.
8263
8264      Note however that we can only get away with using DECL_INCOMING_RTL as
8265      a backup substitute for DECL_RTL in certain limited cases.  In cases
8266      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
8267      we can be sure that the parameter was passed using the same type as it is
8268      declared to have within the function, and that its DECL_INCOMING_RTL
8269      points us to a place where a value of that type is passed.
8270
8271      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
8272      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
8273      because in these cases DECL_INCOMING_RTL points us to a value of some
8274      type which is *different* from the type of the parameter itself.  Thus,
8275      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
8276      such cases, the debugger would end up (for example) trying to fetch a
8277      `float' from a place which actually contains the first part of a
8278      `double'.  That would lead to really incorrect and confusing
8279      output at debug-time.
8280
8281      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
8282      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
8283      are a couple of exceptions however.  On little-endian machines we can
8284      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
8285      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
8286      an integral type that is smaller than TREE_TYPE (decl). These cases arise
8287      when (on a little-endian machine) a non-prototyped function has a
8288      parameter declared to be of type `short' or `char'.  In such cases,
8289      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
8290      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
8291      passed `int' value.  If the debugger then uses that address to fetch
8292      a `short' or a `char' (on a little-endian machine) the result will be
8293      the correct data, so we allow for such exceptional cases below.
8294
8295      Note that our goal here is to describe the place where the given formal
8296      parameter lives during most of the function's activation (i.e. between
8297      the end of the prologue and the start of the epilogue).  We'll do that
8298      as best as we can. Note however that if the given formal parameter is
8299      modified sometime during the execution of the function, then a stack
8300      backtrace (at debug-time) will show the function as having been
8301      called with the *new* value rather than the value which was
8302      originally passed in.  This happens rarely enough that it is not
8303      a major problem, but it *is* a problem, and I'd like to fix it.
8304
8305      A future version of dwarf2out.c may generate two additional
8306      attributes for any given DW_TAG_formal_parameter DIE which will
8307      describe the "passed type" and the "passed location" for the
8308      given formal parameter in addition to the attributes we now
8309      generate to indicate the "declared type" and the "active
8310      location" for each parameter.  This additional set of attributes
8311      could be used by debuggers for stack backtraces. Separately, note
8312      that sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be
8313      NULL also.  This happens (for example) for inlined-instances of
8314      inline function formal parameters which are never referenced.
8315      This really shouldn't be happening.  All PARM_DECL nodes should
8316      get valid non-NULL DECL_INCOMING_RTL values, but integrate.c
8317      doesn't currently generate these values for inlined instances of
8318      inline function parameters, so when we see such cases, we are
8319      just out-of-luck for the time being (until integrate.c
8320      gets fixed).  */
8321
8322   /* Use DECL_RTL as the "location" unless we find something better.  */
8323   rtl = DECL_RTL_IF_SET (decl);
8324
8325   if (TREE_CODE (decl) == PARM_DECL)
8326     {
8327       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
8328         {
8329           tree declared_type = type_main_variant (TREE_TYPE (decl));
8330           tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
8331
8332           /* This decl represents a formal parameter which was optimized out.
8333              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
8334              all* cases where (rtl == NULL_RTX) just below.  */
8335           if (declared_type == passed_type)
8336             rtl = DECL_INCOMING_RTL (decl);
8337           else if (! BYTES_BIG_ENDIAN
8338                    && TREE_CODE (declared_type) == INTEGER_TYPE
8339                    && (GET_MODE_SIZE (TYPE_MODE (declared_type))
8340                        <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
8341             rtl = DECL_INCOMING_RTL (decl);
8342         }
8343
8344       /* If the parm was passed in registers, but lives on the stack, then
8345          make a big endian correction if the mode of the type of the
8346          parameter is not the same as the mode of the rtl.  */
8347       /* ??? This is the same series of checks that are made in dbxout.c before
8348          we reach the big endian correction code there.  It isn't clear if all
8349          of these checks are necessary here, but keeping them all is the safe
8350          thing to do.  */
8351       else if (GET_CODE (rtl) == MEM
8352                && XEXP (rtl, 0) != const0_rtx
8353                && ! CONSTANT_P (XEXP (rtl, 0))
8354                /* Not passed in memory.  */
8355                && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
8356                /* Not passed by invisible reference.  */
8357                && (GET_CODE (XEXP (rtl, 0)) != REG
8358                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
8359                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
8360 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
8361                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
8362 #endif
8363                      )
8364                /* Big endian correction check.  */
8365                && BYTES_BIG_ENDIAN
8366                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
8367                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
8368                    < UNITS_PER_WORD))
8369         {
8370           int offset = (UNITS_PER_WORD
8371                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
8372           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
8373                              plus_constant (XEXP (rtl, 0), offset));
8374         }
8375     }
8376
8377   if (rtl != NULL_RTX)
8378     {
8379       rtl = eliminate_regs (rtl, 0, NULL_RTX);
8380 #ifdef LEAF_REG_REMAP
8381       if (current_function_uses_only_leaf_regs)
8382         leaf_renumber_regs_insn (rtl);
8383 #endif
8384     }
8385
8386   return rtl;
8387 }
8388
8389 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
8390    data attribute for a variable or a parameter.  We generate the
8391    DW_AT_const_value attribute only in those cases where the given variable
8392    or parameter does not have a true "location" either in memory or in a
8393    register.  This can happen (for example) when a constant is passed as an
8394    actual argument in a call to an inline function.  (It's possible that
8395    these things can crop up in other ways also.)  Note that one type of
8396    constant value which can be passed into an inlined function is a constant
8397    pointer.  This can happen for example if an actual argument in an inlined
8398    function call evaluates to a compile-time constant address.  */
8399
8400 static void
8401 add_location_or_const_value_attribute (die, decl)
8402      register dw_die_ref die;
8403      register tree decl;
8404 {
8405   register rtx rtl;
8406
8407   if (TREE_CODE (decl) == ERROR_MARK)
8408     return;
8409
8410   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
8411     abort ();
8412
8413   rtl = rtl_for_decl_location (decl);
8414   if (rtl == NULL_RTX)
8415     return;
8416
8417   switch (GET_CODE (rtl))
8418     {
8419     case ADDRESSOF:
8420       /* The address of a variable that was optimized away; don't emit
8421          anything.  */
8422       break;
8423
8424     case CONST_INT:
8425     case CONST_DOUBLE:
8426     case CONST_STRING:
8427     case SYMBOL_REF:
8428     case LABEL_REF:
8429     case CONST:
8430     case PLUS:
8431       /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
8432       add_const_value_attribute (die, rtl);
8433       break;
8434
8435     case MEM:
8436     case REG:
8437     case SUBREG:
8438     case CONCAT:
8439       add_AT_location_description (die, DW_AT_location, rtl);
8440       break;
8441
8442     default:
8443       abort ();
8444     }
8445 }
8446
8447 /* If we don't have a copy of this variable in memory for some reason (such
8448    as a C++ member constant that doesn't have an out-of-line definition),
8449    we should tell the debugger about the constant value.  */
8450
8451 static void
8452 tree_add_const_value_attribute (var_die, decl)
8453      dw_die_ref var_die;
8454      tree decl;
8455 {
8456   tree init = DECL_INITIAL (decl);
8457   tree type = TREE_TYPE (decl);
8458
8459   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
8460       && initializer_constant_valid_p (init, type) == null_pointer_node)
8461     /* OK */;
8462   else
8463     return;
8464
8465   switch (TREE_CODE (type))
8466     {
8467     case INTEGER_TYPE:
8468       if (host_integerp (init, 0))
8469         add_AT_unsigned (var_die, DW_AT_const_value,
8470                          TREE_INT_CST_LOW (init));
8471       else
8472         add_AT_long_long (var_die, DW_AT_const_value,
8473                           TREE_INT_CST_HIGH (init),
8474                           TREE_INT_CST_LOW (init));
8475       break;
8476
8477     default:;
8478     }
8479 }
8480
8481 /* Generate an DW_AT_name attribute given some string value to be included as
8482    the value of the attribute.  */
8483
8484 static inline void
8485 add_name_attribute (die, name_string)
8486      register dw_die_ref die;
8487      register const char *name_string;
8488 {
8489   if (name_string != NULL && *name_string != 0)
8490     {
8491       if (demangle_name_func)
8492         name_string = (*demangle_name_func) (name_string);
8493
8494       add_AT_string (die, DW_AT_name, name_string);
8495     }
8496 }
8497
8498 /* Given a tree node describing an array bound (either lower or upper) output
8499    a representation for that bound.  */
8500
8501 static void
8502 add_bound_info (subrange_die, bound_attr, bound)
8503      register dw_die_ref subrange_die;
8504      register enum dwarf_attribute bound_attr;
8505      register tree bound;
8506 {
8507   /* If this is an Ada unconstrained array type, then don't emit any debug
8508      info because the array bounds are unknown.  They are parameterized when
8509      the type is instantiated.  */
8510   if (contains_placeholder_p (bound))
8511     return;
8512
8513   switch (TREE_CODE (bound))
8514     {
8515     case ERROR_MARK:
8516       return;
8517
8518     /* All fixed-bounds are represented by INTEGER_CST nodes.        */
8519     case INTEGER_CST:
8520       if (! host_integerp (bound, 0)
8521           || (bound_attr == DW_AT_lower_bound
8522               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
8523                   || (is_fortran () && integer_onep (bound)))))
8524         /* use the default */
8525         ;
8526       else
8527         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
8528       break;
8529
8530     case CONVERT_EXPR:
8531     case NOP_EXPR:
8532     case NON_LVALUE_EXPR:
8533       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
8534       break;
8535
8536     case SAVE_EXPR:
8537       /* If optimization is turned on, the SAVE_EXPRs that describe how to
8538          access the upper bound values may be bogus.  If they refer to a
8539          register, they may only describe how to get at these values at the
8540          points in the generated code right after they have just been
8541          computed.  Worse yet, in the typical case, the upper bound values
8542          will not even *be* computed in the optimized code (though the
8543          number of elements will), so these SAVE_EXPRs are entirely
8544          bogus. In order to compensate for this fact, we check here to see
8545          if optimization is enabled, and if so, we don't add an attribute
8546          for the (unknown and unknowable) upper bound.  This should not
8547          cause too much trouble for existing (stupid?)  debuggers because
8548          they have to deal with empty upper bounds location descriptions
8549          anyway in order to be able to deal with incomplete array types.
8550          Of course an intelligent debugger (GDB?)  should be able to
8551          comprehend that a missing upper bound specification in a array
8552          type used for a storage class `auto' local array variable
8553          indicates that the upper bound is both unknown (at compile- time)
8554          and unknowable (at run-time) due to optimization.
8555
8556          We assume that a MEM rtx is safe because gcc wouldn't put the
8557          value there unless it was going to be used repeatedly in the
8558          function, i.e. for cleanups.  */
8559       if (SAVE_EXPR_RTL (bound)
8560           && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
8561         {
8562           register dw_die_ref ctx = lookup_decl_die (current_function_decl);
8563           register dw_die_ref decl_die = new_die (DW_TAG_variable, ctx);
8564           register rtx loc = SAVE_EXPR_RTL (bound);
8565
8566           /* If the RTL for the SAVE_EXPR is memory, handle the case where
8567              it references an outer function's frame.  */
8568
8569           if (GET_CODE (loc) == MEM)
8570             {
8571               rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
8572
8573               if (XEXP (loc, 0) != new_addr)
8574                 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
8575             }
8576
8577           add_AT_flag (decl_die, DW_AT_artificial, 1);
8578           add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
8579           add_AT_location_description (decl_die, DW_AT_location, loc);
8580           add_AT_die_ref (subrange_die, bound_attr, decl_die);
8581         }
8582
8583       /* Else leave out the attribute.  */
8584       break;
8585
8586     case VAR_DECL:
8587     case PARM_DECL:
8588       {
8589         dw_die_ref decl_die = lookup_decl_die (bound);
8590
8591         /* ??? Can this happen, or should the variable have been bound
8592            first?  Probably it can, since I imagine that we try to create
8593            the types of parameters in the order in which they exist in
8594            the list, and won't have created a forward reference to a
8595            later parameter.  */
8596         if (decl_die != NULL)
8597           add_AT_die_ref (subrange_die, bound_attr, decl_die);
8598         break;
8599       }
8600
8601     default:
8602       {
8603         /* Otherwise try to create a stack operation procedure to
8604            evaluate the value of the array bound.  */
8605
8606         dw_die_ref ctx, decl_die;
8607         dw_loc_descr_ref loc;
8608
8609         loc = loc_descriptor_from_tree (bound, 0);
8610         if (loc == NULL)
8611           break;
8612
8613         ctx = lookup_decl_die (current_function_decl);
8614
8615         decl_die = new_die (DW_TAG_variable, ctx);
8616         add_AT_flag (decl_die, DW_AT_artificial, 1);
8617         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
8618         add_AT_loc (decl_die, DW_AT_location, loc);
8619
8620         add_AT_die_ref (subrange_die, bound_attr, decl_die);
8621         break;
8622       }
8623     }
8624 }
8625
8626 /* Note that the block of subscript information for an array type also
8627    includes information about the element type of type given array type.  */
8628
8629 static void
8630 add_subscript_info (type_die, type)
8631      register dw_die_ref type_die;
8632      register tree type;
8633 {
8634 #ifndef MIPS_DEBUGGING_INFO
8635   register unsigned dimension_number;
8636 #endif
8637   register tree lower, upper;
8638   register dw_die_ref subrange_die;
8639
8640   /* The GNU compilers represent multidimensional array types as sequences of
8641      one dimensional array types whose element types are themselves array
8642      types.  Here we squish that down, so that each multidimensional array
8643      type gets only one array_type DIE in the Dwarf debugging info. The draft
8644      Dwarf specification say that we are allowed to do this kind of
8645      compression in C (because there is no difference between an array or
8646      arrays and a multidimensional array in C) but for other source languages
8647      (e.g. Ada) we probably shouldn't do this.  */
8648
8649   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
8650      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
8651      We work around this by disabling this feature.  See also
8652      gen_array_type_die.  */
8653 #ifndef MIPS_DEBUGGING_INFO
8654   for (dimension_number = 0;
8655        TREE_CODE (type) == ARRAY_TYPE;
8656        type = TREE_TYPE (type), dimension_number++)
8657     {
8658 #endif
8659       register tree domain = TYPE_DOMAIN (type);
8660
8661       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
8662          and (in GNU C only) variable bounds.  Handle all three forms
8663          here.  */
8664       subrange_die = new_die (DW_TAG_subrange_type, type_die);
8665       if (domain)
8666         {
8667           /* We have an array type with specified bounds.  */
8668           lower = TYPE_MIN_VALUE (domain);
8669           upper = TYPE_MAX_VALUE (domain);
8670
8671           /* define the index type.  */
8672           if (TREE_TYPE (domain))
8673             {
8674               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
8675                  TREE_TYPE field.  We can't emit debug info for this
8676                  because it is an unnamed integral type.  */
8677               if (TREE_CODE (domain) == INTEGER_TYPE
8678                   && TYPE_NAME (domain) == NULL_TREE
8679                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
8680                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
8681                 ;
8682               else
8683                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
8684                                     type_die);
8685             }
8686
8687           /* ??? If upper is NULL, the array has unspecified length,
8688              but it does have a lower bound.  This happens with Fortran
8689                dimension arr(N:*)
8690              Since the debugger is definitely going to need to know N
8691              to produce useful results, go ahead and output the lower
8692              bound solo, and hope the debugger can cope.  */
8693
8694           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
8695           if (upper)
8696             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
8697         }
8698       else
8699         /* We have an array type with an unspecified length.  The DWARF-2
8700              spec does not say how to handle this; let's just leave out the
8701              bounds.  */
8702         {;}
8703
8704 #ifndef MIPS_DEBUGGING_INFO
8705     }
8706 #endif
8707 }
8708
8709 static void
8710 add_byte_size_attribute (die, tree_node)
8711      dw_die_ref die;
8712      register tree tree_node;
8713 {
8714   register unsigned size;
8715
8716   switch (TREE_CODE (tree_node))
8717     {
8718     case ERROR_MARK:
8719       size = 0;
8720       break;
8721     case ENUMERAL_TYPE:
8722     case RECORD_TYPE:
8723     case UNION_TYPE:
8724     case QUAL_UNION_TYPE:
8725       size = int_size_in_bytes (tree_node);
8726       break;
8727     case FIELD_DECL:
8728       /* For a data member of a struct or union, the DW_AT_byte_size is
8729          generally given as the number of bytes normally allocated for an
8730          object of the *declared* type of the member itself.  This is true
8731          even for bit-fields.  */
8732       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
8733       break;
8734     default:
8735       abort ();
8736     }
8737
8738   /* Note that `size' might be -1 when we get to this point.  If it is, that
8739      indicates that the byte size of the entity in question is variable.  We
8740      have no good way of expressing this fact in Dwarf at the present time,
8741      so just let the -1 pass on through.  */
8742
8743   add_AT_unsigned (die, DW_AT_byte_size, size);
8744 }
8745
8746 /* For a FIELD_DECL node which represents a bit-field, output an attribute
8747    which specifies the distance in bits from the highest order bit of the
8748    "containing object" for the bit-field to the highest order bit of the
8749    bit-field itself.
8750
8751    For any given bit-field, the "containing object" is a hypothetical
8752    object (of some integral or enum type) within which the given bit-field
8753    lives.  The type of this hypothetical "containing object" is always the
8754    same as the declared type of the individual bit-field itself.  The
8755    determination of the exact location of the "containing object" for a
8756    bit-field is rather complicated.  It's handled by the
8757    `field_byte_offset' function (above).
8758
8759    Note that it is the size (in bytes) of the hypothetical "containing object"
8760    which will be given in the DW_AT_byte_size attribute for this bit-field.
8761    (See `byte_size_attribute' above).  */
8762
8763 static inline void
8764 add_bit_offset_attribute (die, decl)
8765      register dw_die_ref die;
8766      register tree decl;
8767 {
8768   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
8769   tree type = DECL_BIT_FIELD_TYPE (decl);
8770   HOST_WIDE_INT bitpos_int;
8771   HOST_WIDE_INT highest_order_object_bit_offset;
8772   HOST_WIDE_INT highest_order_field_bit_offset;
8773   HOST_WIDE_INT unsigned bit_offset;
8774
8775   /* Must be a field and a bit field.  */
8776   if (!type
8777       || TREE_CODE (decl) != FIELD_DECL)
8778     abort ();
8779
8780   /* We can't yet handle bit-fields whose offsets are variable, so if we
8781      encounter such things, just return without generating any attribute
8782      whatsoever.  Likewise for variable or too large size.  */
8783   if (! host_integerp (bit_position (decl), 0)
8784       || ! host_integerp (DECL_SIZE (decl), 1))
8785     return;
8786
8787   bitpos_int = int_bit_position (decl);
8788
8789   /* Note that the bit offset is always the distance (in bits) from the
8790      highest-order bit of the "containing object" to the highest-order bit of
8791      the bit-field itself.  Since the "high-order end" of any object or field
8792      is different on big-endian and little-endian machines, the computation
8793      below must take account of these differences.  */
8794   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
8795   highest_order_field_bit_offset = bitpos_int;
8796
8797   if (! BYTES_BIG_ENDIAN)
8798     {
8799       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
8800       highest_order_object_bit_offset += simple_type_size_in_bits (type);
8801     }
8802
8803   bit_offset
8804     = (! BYTES_BIG_ENDIAN
8805        ? highest_order_object_bit_offset - highest_order_field_bit_offset
8806        : highest_order_field_bit_offset - highest_order_object_bit_offset);
8807
8808   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
8809 }
8810
8811 /* For a FIELD_DECL node which represents a bit field, output an attribute
8812    which specifies the length in bits of the given field.  */
8813
8814 static inline void
8815 add_bit_size_attribute (die, decl)
8816      register dw_die_ref die;
8817      register tree decl;
8818 {
8819   /* Must be a field and a bit field.  */
8820   if (TREE_CODE (decl) != FIELD_DECL
8821       || ! DECL_BIT_FIELD_TYPE (decl))
8822     abort ();
8823
8824   if (host_integerp (DECL_SIZE (decl), 1))
8825     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
8826 }
8827
8828 /* If the compiled language is ANSI C, then add a 'prototyped'
8829    attribute, if arg types are given for the parameters of a function.  */
8830
8831 static inline void
8832 add_prototyped_attribute (die, func_type)
8833      register dw_die_ref die;
8834      register tree func_type;
8835 {
8836   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
8837       && TYPE_ARG_TYPES (func_type) != NULL)
8838     add_AT_flag (die, DW_AT_prototyped, 1);
8839 }
8840
8841 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
8842    by looking in either the type declaration or object declaration
8843    equate table.  */
8844
8845 static inline void
8846 add_abstract_origin_attribute (die, origin)
8847      register dw_die_ref die;
8848      register tree origin;
8849 {
8850   dw_die_ref origin_die = NULL;
8851
8852   if (TREE_CODE (origin) != FUNCTION_DECL)
8853     {
8854       /* We may have gotten separated from the block for the inlined
8855          function, if we're in an exception handler or some such; make
8856          sure that the abstract function has been written out.
8857
8858          Doing this for nested functions is wrong, however; functions are
8859          distinct units, and our context might not even be inline.  */
8860       tree fn = origin;
8861       if (TYPE_P (fn))
8862         fn = TYPE_STUB_DECL (fn);
8863       fn = decl_function_context (fn);
8864       if (fn)
8865         dwarf2out_abstract_function (fn);
8866     }
8867
8868   if (DECL_P (origin))
8869     origin_die = lookup_decl_die (origin);
8870   else if (TYPE_P (origin))
8871     origin_die = lookup_type_die (origin);
8872
8873   if (origin_die == NULL)
8874     abort ();
8875
8876   add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
8877 }
8878
8879 /* We do not currently support the pure_virtual attribute.  */
8880
8881 static inline void
8882 add_pure_or_virtual_attribute (die, func_decl)
8883      register dw_die_ref die;
8884      register tree func_decl;
8885 {
8886   if (DECL_VINDEX (func_decl))
8887     {
8888       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
8889
8890       if (host_integerp (DECL_VINDEX (func_decl), 0))
8891         add_AT_loc (die, DW_AT_vtable_elem_location,
8892                     new_loc_descr (DW_OP_constu,
8893                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
8894                                    0));
8895
8896       /* GNU extension: Record what type this method came from originally.  */
8897       if (debug_info_level > DINFO_LEVEL_TERSE)
8898         add_AT_die_ref (die, DW_AT_containing_type,
8899                         lookup_type_die (DECL_CONTEXT (func_decl)));
8900     }
8901 }
8902 \f
8903 /* Add source coordinate attributes for the given decl.  */
8904
8905 static void
8906 add_src_coords_attributes (die, decl)
8907      register dw_die_ref die;
8908      register tree decl;
8909 {
8910   register unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
8911
8912   add_AT_unsigned (die, DW_AT_decl_file, file_index);
8913   add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
8914 }
8915
8916 /* Add an DW_AT_name attribute and source coordinate attribute for the
8917    given decl, but only if it actually has a name.  */
8918
8919 static void
8920 add_name_and_src_coords_attributes (die, decl)
8921      register dw_die_ref die;
8922      register tree decl;
8923 {
8924   register tree decl_name;
8925
8926   decl_name = DECL_NAME (decl);
8927   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
8928     {
8929       add_name_attribute (die, dwarf2_name (decl, 0));
8930       if (! DECL_ARTIFICIAL (decl))
8931         add_src_coords_attributes (die, decl);
8932
8933       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
8934           && TREE_PUBLIC (decl)
8935           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
8936           && !DECL_ABSTRACT (decl))
8937         add_AT_string (die, DW_AT_MIPS_linkage_name,
8938                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
8939     }
8940 }
8941
8942 /* Push a new declaration scope.  */
8943
8944 static void
8945 push_decl_scope (scope)
8946      tree scope;
8947 {
8948   /* Make room in the decl_scope_table, if necessary.  */
8949   if (decl_scope_table_allocated == decl_scope_depth)
8950     {
8951       decl_scope_table_allocated += DECL_SCOPE_TABLE_INCREMENT;
8952       decl_scope_table
8953         = (tree *) xrealloc (decl_scope_table,
8954                              decl_scope_table_allocated * sizeof (tree));
8955     }
8956
8957   decl_scope_table[decl_scope_depth] = scope;
8958   decl_scope_depth++;
8959 }
8960
8961 /* Pop a declaration scope.  */
8962 static inline void
8963 pop_decl_scope ()
8964 {
8965   if (decl_scope_depth <= 0)
8966     abort ();
8967   --decl_scope_depth;
8968 }
8969
8970 /* Return the DIE for the scope that immediately contains this type.
8971    Non-named types get global scope.  Named types nested in other
8972    types get their containing scope if it's open, or global scope
8973    otherwise.  All other types (i.e. function-local named types) get
8974    the current active scope.  */
8975
8976 static dw_die_ref
8977 scope_die_for (t, context_die)
8978      register tree t;
8979      register dw_die_ref context_die;
8980 {
8981   register dw_die_ref scope_die = NULL;
8982   register tree containing_scope;
8983   register int i;
8984
8985   /* Non-types always go in the current scope.  */
8986   if (! TYPE_P (t))
8987     abort ();
8988
8989   containing_scope = TYPE_CONTEXT (t);
8990
8991   /* Ignore namespaces for the moment.  */
8992   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
8993     containing_scope = NULL_TREE;
8994
8995   /* Ignore function type "scopes" from the C frontend.  They mean that
8996      a tagged type is local to a parmlist of a function declarator, but
8997      that isn't useful to DWARF.  */
8998   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
8999     containing_scope = NULL_TREE;
9000
9001   if (containing_scope == NULL_TREE)
9002     scope_die = comp_unit_die;
9003   else if (TYPE_P (containing_scope))
9004     {
9005       /* For types, we can just look up the appropriate DIE.  But
9006          first we check to see if we're in the middle of emitting it
9007          so we know where the new DIE should go.  */
9008
9009       for (i = decl_scope_depth - 1; i >= 0; --i)
9010         if (decl_scope_table[i] == containing_scope)
9011           break;
9012
9013       if (i < 0)
9014         {
9015           if (debug_info_level > DINFO_LEVEL_TERSE
9016               && !TREE_ASM_WRITTEN (containing_scope))
9017             abort ();
9018
9019           /* If none of the current dies are suitable, we get file scope.  */
9020           scope_die = comp_unit_die;
9021         }
9022       else
9023         scope_die = lookup_type_die (containing_scope);
9024     }
9025   else
9026     scope_die = context_die;
9027
9028   return scope_die;
9029 }
9030
9031 /* Returns nonzero iff CONTEXT_DIE is internal to a function.  */
9032
9033 static inline int local_scope_p PARAMS ((dw_die_ref));
9034 static inline int
9035 local_scope_p (context_die)
9036      dw_die_ref context_die;
9037 {
9038   for (; context_die; context_die = context_die->die_parent)
9039     if (context_die->die_tag == DW_TAG_inlined_subroutine
9040         || context_die->die_tag == DW_TAG_subprogram)
9041       return 1;
9042   return 0;
9043 }
9044
9045 /* Returns nonzero iff CONTEXT_DIE is a class.  */
9046
9047 static inline int class_scope_p PARAMS ((dw_die_ref));
9048 static inline int
9049 class_scope_p (context_die)
9050      dw_die_ref context_die;
9051 {
9052   return (context_die
9053           && (context_die->die_tag == DW_TAG_structure_type
9054               || context_die->die_tag == DW_TAG_union_type));
9055 }
9056
9057 /* Many forms of DIEs require a "type description" attribute.  This
9058    routine locates the proper "type descriptor" die for the type given
9059    by 'type', and adds an DW_AT_type attribute below the given die.  */
9060
9061 static void
9062 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
9063      register dw_die_ref object_die;
9064      register tree type;
9065      register int decl_const;
9066      register int decl_volatile;
9067      register dw_die_ref context_die;
9068 {
9069   register enum tree_code code  = TREE_CODE (type);
9070   register dw_die_ref type_die  = NULL;
9071
9072   /* ??? If this type is an unnamed subrange type of an integral or
9073      floating-point type, use the inner type.  This is because we have no
9074      support for unnamed types in base_type_die.  This can happen if this is
9075      an Ada subrange type.  Correct solution is emit a subrange type die.  */
9076   if ((code == INTEGER_TYPE || code == REAL_TYPE)
9077       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
9078     type = TREE_TYPE (type), code = TREE_CODE (type);
9079
9080   if (code == ERROR_MARK)
9081     return;
9082
9083   /* Handle a special case.  For functions whose return type is void, we
9084      generate *no* type attribute.  (Note that no object may have type
9085      `void', so this only applies to function return types).  */
9086   if (code == VOID_TYPE)
9087     return;
9088
9089   type_die = modified_type_die (type,
9090                                 decl_const || TYPE_READONLY (type),
9091                                 decl_volatile || TYPE_VOLATILE (type),
9092                                 context_die);
9093   if (type_die != NULL)
9094     add_AT_die_ref (object_die, DW_AT_type, type_die);
9095 }
9096
9097 /* Given a tree pointer to a struct, class, union, or enum type node, return
9098    a pointer to the (string) tag name for the given type, or zero if the type
9099    was declared without a tag.  */
9100
9101 static const char *
9102 type_tag (type)
9103      register tree type;
9104 {
9105   register const char *name = 0;
9106
9107   if (TYPE_NAME (type) != 0)
9108     {
9109       register tree t = 0;
9110
9111       /* Find the IDENTIFIER_NODE for the type name.  */
9112       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9113         t = TYPE_NAME (type);
9114
9115       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
9116          a TYPE_DECL node, regardless of whether or not a `typedef' was
9117          involved.  */
9118       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9119                && ! DECL_IGNORED_P (TYPE_NAME (type)))
9120         t = DECL_NAME (TYPE_NAME (type));
9121
9122       /* Now get the name as a string, or invent one.  */
9123       if (t != 0)
9124         name = IDENTIFIER_POINTER (t);
9125     }
9126
9127   return (name == 0 || *name == '\0') ? 0 : name;
9128 }
9129
9130 /* Return the type associated with a data member, make a special check
9131    for bit field types.  */
9132
9133 static inline tree
9134 member_declared_type (member)
9135      register tree member;
9136 {
9137   return (DECL_BIT_FIELD_TYPE (member)
9138           ? DECL_BIT_FIELD_TYPE (member)
9139           : TREE_TYPE (member));
9140 }
9141
9142 /* Get the decl's label, as described by its RTL. This may be different
9143    from the DECL_NAME name used in the source file.  */
9144
9145 #if 0
9146 static const char *
9147 decl_start_label (decl)
9148      register tree decl;
9149 {
9150   rtx x;
9151   const char *fnname;
9152   x = DECL_RTL (decl);
9153   if (GET_CODE (x) != MEM)
9154     abort ();
9155
9156   x = XEXP (x, 0);
9157   if (GET_CODE (x) != SYMBOL_REF)
9158     abort ();
9159
9160   fnname = XSTR (x, 0);
9161   return fnname;
9162 }
9163 #endif
9164 \f
9165 /* These routines generate the internal representation of the DIE's for
9166    the compilation unit.  Debugging information is collected by walking
9167    the declaration trees passed in from dwarf2out_decl().  */
9168
9169 static void
9170 gen_array_type_die (type, context_die)
9171      register tree type;
9172      register dw_die_ref context_die;
9173 {
9174   register dw_die_ref scope_die = scope_die_for (type, context_die);
9175   register dw_die_ref array_die;
9176   register tree element_type;
9177
9178   /* ??? The SGI dwarf reader fails for array of array of enum types unless
9179      the inner array type comes before the outer array type.  Thus we must
9180      call gen_type_die before we call new_die.  See below also.  */
9181 #ifdef MIPS_DEBUGGING_INFO
9182   gen_type_die (TREE_TYPE (type), context_die);
9183 #endif
9184
9185   array_die = new_die (DW_TAG_array_type, scope_die);
9186
9187 #if 0
9188   /* We default the array ordering.  SDB will probably do
9189      the right things even if DW_AT_ordering is not present.  It's not even
9190      an issue until we start to get into multidimensional arrays anyway.  If
9191      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
9192      then we'll have to put the DW_AT_ordering attribute back in.  (But if
9193      and when we find out that we need to put these in, we will only do so
9194      for multidimensional arrays.  */
9195   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
9196 #endif
9197
9198 #ifdef MIPS_DEBUGGING_INFO
9199   /* The SGI compilers handle arrays of unknown bound by setting
9200      AT_declaration and not emitting any subrange DIEs.  */
9201   if (! TYPE_DOMAIN (type))
9202     add_AT_unsigned (array_die, DW_AT_declaration, 1);
9203   else
9204 #endif
9205     add_subscript_info (array_die, type);
9206
9207   add_name_attribute (array_die, type_tag (type));
9208   equate_type_number_to_die (type, array_die);
9209
9210   /* Add representation of the type of the elements of this array type.  */
9211   element_type = TREE_TYPE (type);
9212
9213   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9214      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
9215      We work around this by disabling this feature.  See also
9216      add_subscript_info.  */
9217 #ifndef MIPS_DEBUGGING_INFO
9218   while (TREE_CODE (element_type) == ARRAY_TYPE)
9219     element_type = TREE_TYPE (element_type);
9220
9221   gen_type_die (element_type, context_die);
9222 #endif
9223
9224   add_type_attribute (array_die, element_type, 0, 0, context_die);
9225 }
9226
9227 static void
9228 gen_set_type_die (type, context_die)
9229      register tree type;
9230      register dw_die_ref context_die;
9231 {
9232   register dw_die_ref type_die
9233     = new_die (DW_TAG_set_type, scope_die_for (type, context_die));
9234
9235   equate_type_number_to_die (type, type_die);
9236   add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
9237 }
9238
9239 #if 0
9240 static void
9241 gen_entry_point_die (decl, context_die)
9242      register tree decl;
9243      register dw_die_ref context_die;
9244 {
9245   register tree origin = decl_ultimate_origin (decl);
9246   register dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die);
9247   if (origin != NULL)
9248     add_abstract_origin_attribute (decl_die, origin);
9249   else
9250     {
9251       add_name_and_src_coords_attributes (decl_die, decl);
9252       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
9253                           0, 0, context_die);
9254     }
9255
9256   if (DECL_ABSTRACT (decl))
9257     equate_decl_number_to_die (decl, decl_die);
9258   else
9259     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
9260 }
9261 #endif
9262
9263 /* Remember a type in the incomplete_types_list.  */
9264
9265 static void
9266 add_incomplete_type (type)
9267      tree type;
9268 {
9269   if (incomplete_types == incomplete_types_allocated)
9270     {
9271       incomplete_types_allocated += INCOMPLETE_TYPES_INCREMENT;
9272       incomplete_types_list
9273         = (tree *) xrealloc (incomplete_types_list,
9274                              sizeof (tree) * incomplete_types_allocated);
9275     }
9276
9277   incomplete_types_list[incomplete_types++] = type;
9278 }
9279
9280 /* Walk through the list of incomplete types again, trying once more to
9281    emit full debugging info for them.  */
9282
9283 static void
9284 retry_incomplete_types ()
9285 {
9286   register tree type;
9287
9288   while (incomplete_types)
9289     {
9290       --incomplete_types;
9291       type = incomplete_types_list[incomplete_types];
9292       gen_type_die (type, comp_unit_die);
9293     }
9294 }
9295
9296 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
9297
9298 static void
9299 gen_inlined_enumeration_type_die (type, context_die)
9300      register tree type;
9301      register dw_die_ref context_die;
9302 {
9303   register dw_die_ref type_die = new_die (DW_TAG_enumeration_type,
9304                                           context_die);
9305   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9306      be incomplete and such types are not marked.  */
9307   add_abstract_origin_attribute (type_die, type);
9308 }
9309
9310 /* Generate a DIE to represent an inlined instance of a structure type.  */
9311
9312 static void
9313 gen_inlined_structure_type_die (type, context_die)
9314      register tree type;
9315      register dw_die_ref context_die;
9316 {
9317   register dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die);
9318
9319   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9320      be incomplete and such types are not marked.  */
9321   add_abstract_origin_attribute (type_die, type);
9322 }
9323
9324 /* Generate a DIE to represent an inlined instance of a union type.  */
9325
9326 static void
9327 gen_inlined_union_type_die (type, context_die)
9328      register tree type;
9329      register dw_die_ref context_die;
9330 {
9331   register dw_die_ref type_die = new_die (DW_TAG_union_type, context_die);
9332
9333   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9334      be incomplete and such types are not marked.  */
9335   add_abstract_origin_attribute (type_die, type);
9336 }
9337
9338 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
9339    include all of the information about the enumeration values also. Each
9340    enumerated type name/value is listed as a child of the enumerated type
9341    DIE.  */
9342
9343 static void
9344 gen_enumeration_type_die (type, context_die)
9345      register tree type;
9346      register dw_die_ref context_die;
9347 {
9348   register dw_die_ref type_die = lookup_type_die (type);
9349
9350   if (type_die == NULL)
9351     {
9352       type_die = new_die (DW_TAG_enumeration_type,
9353                           scope_die_for (type, context_die));
9354       equate_type_number_to_die (type, type_die);
9355       add_name_attribute (type_die, type_tag (type));
9356     }
9357   else if (! TYPE_SIZE (type))
9358     return;
9359   else
9360     remove_AT (type_die, DW_AT_declaration);
9361
9362   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
9363      given enum type is incomplete, do not generate the DW_AT_byte_size
9364      attribute or the DW_AT_element_list attribute.  */
9365   if (TYPE_SIZE (type))
9366     {
9367       register tree link;
9368
9369       TREE_ASM_WRITTEN (type) = 1;
9370       add_byte_size_attribute (type_die, type);
9371       if (TYPE_STUB_DECL (type) != NULL_TREE)
9372         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
9373
9374       /* If the first reference to this type was as the return type of an
9375          inline function, then it may not have a parent.  Fix this now.  */
9376       if (type_die->die_parent == NULL)
9377         add_child_die (scope_die_for (type, context_die), type_die);
9378
9379       for (link = TYPE_FIELDS (type);
9380            link != NULL; link = TREE_CHAIN (link))
9381         {
9382           register dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die);
9383
9384           add_name_attribute (enum_die,
9385                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
9386
9387           if (host_integerp (TREE_VALUE (link), 0))
9388             {
9389               if (tree_int_cst_sgn (TREE_VALUE (link)) < 0)
9390                 add_AT_int (enum_die, DW_AT_const_value,
9391                             tree_low_cst (TREE_VALUE (link), 0));
9392               else
9393                 add_AT_unsigned (enum_die, DW_AT_const_value,
9394                                  tree_low_cst (TREE_VALUE (link), 0));
9395             }
9396         }
9397     }
9398   else
9399     add_AT_flag (type_die, DW_AT_declaration, 1);
9400 }
9401
9402 /* Generate a DIE to represent either a real live formal parameter decl or to
9403    represent just the type of some formal parameter position in some function
9404    type.
9405
9406    Note that this routine is a bit unusual because its argument may be a
9407    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
9408    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
9409    node.  If it's the former then this function is being called to output a
9410    DIE to represent a formal parameter object (or some inlining thereof).  If
9411    it's the latter, then this function is only being called to output a
9412    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
9413    argument type of some subprogram type.  */
9414
9415 static dw_die_ref
9416 gen_formal_parameter_die (node, context_die)
9417      register tree node;
9418      register dw_die_ref context_die;
9419 {
9420   register dw_die_ref parm_die
9421     = new_die (DW_TAG_formal_parameter, context_die);
9422   register tree origin;
9423
9424   switch (TREE_CODE_CLASS (TREE_CODE (node)))
9425     {
9426     case 'd':
9427       origin = decl_ultimate_origin (node);
9428       if (origin != NULL)
9429         add_abstract_origin_attribute (parm_die, origin);
9430       else
9431         {
9432           add_name_and_src_coords_attributes (parm_die, node);
9433           add_type_attribute (parm_die, TREE_TYPE (node),
9434                               TREE_READONLY (node),
9435                               TREE_THIS_VOLATILE (node),
9436                               context_die);
9437           if (DECL_ARTIFICIAL (node))
9438             add_AT_flag (parm_die, DW_AT_artificial, 1);
9439         }
9440
9441       equate_decl_number_to_die (node, parm_die);
9442       if (! DECL_ABSTRACT (node))
9443         add_location_or_const_value_attribute (parm_die, node);
9444
9445       break;
9446
9447     case 't':
9448       /* We were called with some kind of a ..._TYPE node.  */
9449       add_type_attribute (parm_die, node, 0, 0, context_die);
9450       break;
9451
9452     default:
9453       abort ();
9454     }
9455
9456   return parm_die;
9457 }
9458
9459 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
9460    at the end of an (ANSI prototyped) formal parameters list.  */
9461
9462 static void
9463 gen_unspecified_parameters_die (decl_or_type, context_die)
9464      register tree decl_or_type ATTRIBUTE_UNUSED;
9465      register dw_die_ref context_die;
9466 {
9467   new_die (DW_TAG_unspecified_parameters, context_die);
9468 }
9469
9470 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
9471    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
9472    parameters as specified in some function type specification (except for
9473    those which appear as part of a function *definition*).  */
9474
9475 static void
9476 gen_formal_types_die (function_or_method_type, context_die)
9477      register tree function_or_method_type;
9478      register dw_die_ref context_die;
9479 {
9480   register tree link;
9481   register tree formal_type = NULL;
9482   register tree first_parm_type;
9483   tree arg;
9484
9485   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
9486     {
9487       arg = DECL_ARGUMENTS (function_or_method_type);
9488       function_or_method_type = TREE_TYPE (function_or_method_type);
9489     }
9490   else
9491     arg = NULL_TREE;
9492   
9493   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
9494
9495   /* Make our first pass over the list of formal parameter types and output a
9496      DW_TAG_formal_parameter DIE for each one.  */
9497   for (link = first_parm_type; link; )
9498     {
9499       register dw_die_ref parm_die;
9500
9501       formal_type = TREE_VALUE (link);
9502       if (formal_type == void_type_node)
9503         break;
9504
9505       /* Output a (nameless) DIE to represent the formal parameter itself.  */
9506       parm_die = gen_formal_parameter_die (formal_type, context_die);
9507       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
9508            && link == first_parm_type)
9509           || (arg && DECL_ARTIFICIAL (arg)))
9510         add_AT_flag (parm_die, DW_AT_artificial, 1);
9511
9512       link = TREE_CHAIN (link);
9513       if (arg)
9514         arg = TREE_CHAIN (arg);
9515     }
9516
9517   /* If this function type has an ellipsis, add a
9518      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
9519   if (formal_type != void_type_node)
9520     gen_unspecified_parameters_die (function_or_method_type, context_die);
9521
9522   /* Make our second (and final) pass over the list of formal parameter types
9523      and output DIEs to represent those types (as necessary).  */
9524   for (link = TYPE_ARG_TYPES (function_or_method_type);
9525        link;
9526        link = TREE_CHAIN (link))
9527     {
9528       formal_type = TREE_VALUE (link);
9529       if (formal_type == void_type_node)
9530         break;
9531
9532       gen_type_die (formal_type, context_die);
9533     }
9534 }
9535
9536 /* We want to generate the DIE for TYPE so that we can generate the
9537    die for MEMBER, which has been defined; we will need to refer back
9538    to the member declaration nested within TYPE.  If we're trying to
9539    generate minimal debug info for TYPE, processing TYPE won't do the
9540    trick; we need to attach the member declaration by hand.  */
9541
9542 static void
9543 gen_type_die_for_member (type, member, context_die)
9544      tree type, member;
9545      dw_die_ref context_die;
9546 {
9547   gen_type_die (type, context_die);
9548
9549   /* If we're trying to avoid duplicate debug info, we may not have
9550      emitted the member decl for this function.  Emit it now.  */
9551   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
9552       && ! lookup_decl_die (member))
9553     {
9554       if (decl_ultimate_origin (member))
9555         abort ();
9556
9557       push_decl_scope (type);
9558       if (TREE_CODE (member) == FUNCTION_DECL)
9559         gen_subprogram_die (member, lookup_type_die (type));
9560       else
9561         gen_variable_die (member, lookup_type_die (type));
9562       pop_decl_scope ();
9563     }
9564 }
9565
9566 /* Generate the DWARF2 info for the "abstract" instance
9567    of a function which we may later generate inlined and/or
9568    out-of-line instances of.  */
9569
9570 void
9571 dwarf2out_abstract_function (decl)
9572      tree decl;
9573 {
9574   register dw_die_ref old_die;
9575   tree save_fn;
9576   tree context;
9577   int was_abstract = DECL_ABSTRACT (decl);
9578
9579   /* Make sure we have the actual abstract inline, not a clone.  */
9580   decl = DECL_ORIGIN (decl);
9581
9582   old_die = lookup_decl_die (decl);  
9583   if (old_die && get_AT_unsigned (old_die, DW_AT_inline))
9584     /* We've already generated the abstract instance.  */
9585     return;
9586
9587   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
9588      we don't get confused by DECL_ABSTRACT.  */
9589   context = decl_class_context (decl);
9590   if (context)
9591     gen_type_die_for_member
9592       (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
9593  
9594   /* Pretend we've just finished compiling this function.  */
9595   save_fn = current_function_decl;
9596   current_function_decl = decl;
9597
9598   set_decl_abstract_flags (decl, 1);
9599   dwarf2out_decl (decl);
9600   if (! was_abstract)
9601     set_decl_abstract_flags (decl, 0);
9602
9603   current_function_decl = save_fn;
9604 }
9605
9606 /* Generate a DIE to represent a declared function (either file-scope or
9607    block-local).  */
9608
9609 static void
9610 gen_subprogram_die (decl, context_die)
9611      register tree decl;
9612      register dw_die_ref context_die;
9613 {
9614   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
9615   register tree origin = decl_ultimate_origin (decl);
9616   register dw_die_ref subr_die;
9617   register rtx fp_reg;
9618   register tree fn_arg_types;
9619   register tree outer_scope;
9620   register dw_die_ref old_die = lookup_decl_die (decl);
9621   register int declaration = (current_function_decl != decl
9622                               || class_scope_p (context_die));
9623
9624   /* Note that it is possible to have both DECL_ABSTRACT and `declaration'
9625      be true, if we started to generate the abstract instance of an inline,
9626      decided to output its containing class, and proceeded to emit the
9627      declaration of the inline from the member list for the class.  In that
9628      case, `declaration' takes priority; we'll get back to the abstract
9629      instance when we're done with the class.  */
9630
9631   /* The class-scope declaration DIE must be the primary DIE.  */
9632   if (origin && declaration && class_scope_p (context_die))
9633     {
9634       origin = NULL;
9635       if (old_die)
9636         abort ();
9637     }
9638
9639   if (origin != NULL)
9640     {
9641       if (declaration && ! local_scope_p (context_die))
9642         abort ();
9643
9644       /* Fixup die_parent for the abstract instance of a nested
9645          inline function.  */
9646       if (old_die && old_die->die_parent == NULL)
9647         add_child_die (context_die, old_die);
9648
9649       subr_die = new_die (DW_TAG_subprogram, context_die);
9650       add_abstract_origin_attribute (subr_die, origin);
9651     }
9652   else if (old_die)
9653     {
9654       unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
9655
9656       if (!get_AT_flag (old_die, DW_AT_declaration)
9657           /* We can have a normal definition following an inline one in the
9658              case of redefinition of GNU C extern inlines.
9659              It seems reasonable to use AT_specification in this case.  */
9660           && !get_AT_unsigned (old_die, DW_AT_inline))
9661         {
9662           /* ??? This can happen if there is a bug in the program, for
9663              instance, if it has duplicate function definitions.  Ideally,
9664              we should detect this case and ignore it.  For now, if we have
9665              already reported an error, any error at all, then assume that
9666              we got here because of a input error, not a dwarf2 bug.  */
9667           if (errorcount)
9668             return;
9669           abort ();
9670         }
9671
9672       /* If the definition comes from the same place as the declaration,
9673          maybe use the old DIE.  We always want the DIE for this function
9674          that has the *_pc attributes to be under comp_unit_die so the
9675          debugger can find it.  We also need to do this for abstract
9676          instances of inlines, since the spec requires the out-of-line copy
9677          to have the same parent.  For local class methods, this doesn't
9678          apply; we just use the old DIE.  */
9679       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
9680           && (DECL_ARTIFICIAL (decl)
9681               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
9682                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
9683                       == (unsigned) DECL_SOURCE_LINE (decl)))))
9684         {
9685           subr_die = old_die;
9686
9687           /* Clear out the declaration attribute and the parm types.  */
9688           remove_AT (subr_die, DW_AT_declaration);
9689           remove_children (subr_die);
9690         }
9691       else
9692         {
9693           subr_die = new_die (DW_TAG_subprogram, context_die);
9694           add_AT_die_ref (subr_die, DW_AT_specification, old_die);
9695           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
9696             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
9697           if (get_AT_unsigned (old_die, DW_AT_decl_line)
9698               != (unsigned) DECL_SOURCE_LINE (decl))
9699             add_AT_unsigned
9700               (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
9701         }
9702     }
9703   else
9704     {
9705       subr_die = new_die (DW_TAG_subprogram, context_die);
9706
9707       if (TREE_PUBLIC (decl))
9708         add_AT_flag (subr_die, DW_AT_external, 1);
9709
9710       add_name_and_src_coords_attributes (subr_die, decl);
9711       if (debug_info_level > DINFO_LEVEL_TERSE)
9712         {
9713           register tree type = TREE_TYPE (decl);
9714
9715           add_prototyped_attribute (subr_die, type);
9716           add_type_attribute (subr_die, TREE_TYPE (type), 0, 0, context_die);
9717         }
9718
9719       add_pure_or_virtual_attribute (subr_die, decl);
9720       if (DECL_ARTIFICIAL (decl))
9721         add_AT_flag (subr_die, DW_AT_artificial, 1);
9722       if (TREE_PROTECTED (decl))
9723         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
9724       else if (TREE_PRIVATE (decl))
9725         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
9726     }
9727
9728   if (declaration)
9729     {
9730       if (!(old_die && get_AT_unsigned (old_die, DW_AT_inline)))
9731         {
9732           add_AT_flag (subr_die, DW_AT_declaration, 1);
9733
9734           /* The first time we see a member function, it is in the context of
9735              the class to which it belongs.  We make sure of this by emitting
9736              the class first.  The next time is the definition, which is
9737              handled above.  The two may come from the same source text.  */
9738           if (DECL_CONTEXT (decl) || DECL_ABSTRACT (decl))
9739             equate_decl_number_to_die (decl, subr_die);
9740         }
9741     }
9742   else if (DECL_ABSTRACT (decl))
9743     {
9744       if (DECL_INLINE (decl) && !flag_no_inline)
9745         {
9746           /* ??? Checking DECL_DEFER_OUTPUT is correct for static
9747              inline functions, but not for extern inline functions.
9748              We can't get this completely correct because information
9749              about whether the function was declared inline is not
9750              saved anywhere.  */
9751           if (DECL_DEFER_OUTPUT (decl))
9752             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
9753           else
9754             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
9755         }
9756       else
9757         add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
9758
9759       equate_decl_number_to_die (decl, subr_die);
9760     }
9761   else if (!DECL_EXTERNAL (decl))
9762     {
9763       if (!(old_die && get_AT_unsigned (old_die, DW_AT_inline)))
9764         equate_decl_number_to_die (decl, subr_die);
9765
9766       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
9767                                    current_funcdef_number);
9768       add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
9769       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
9770                                    current_funcdef_number);
9771       add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
9772
9773       add_pubname (decl, subr_die);
9774       add_arange (decl, subr_die);
9775
9776 #ifdef MIPS_DEBUGGING_INFO
9777       /* Add a reference to the FDE for this routine.  */
9778       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
9779 #endif
9780
9781       /* Define the "frame base" location for this routine.  We use the
9782          frame pointer or stack pointer registers, since the RTL for local
9783          variables is relative to one of them.  */
9784       fp_reg
9785         = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
9786       add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
9787
9788 #if 0
9789       /* ??? This fails for nested inline functions, because context_display
9790          is not part of the state saved/restored for inline functions.  */
9791       if (current_function_needs_context)
9792         add_AT_location_description (subr_die, DW_AT_static_link,
9793                                      lookup_static_chain (decl));
9794 #endif
9795     }
9796
9797   /* Now output descriptions of the arguments for this function. This gets
9798      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
9799      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
9800      `...' at the end of the formal parameter list.  In order to find out if
9801      there was a trailing ellipsis or not, we must instead look at the type
9802      associated with the FUNCTION_DECL.  This will be a node of type
9803      FUNCTION_TYPE. If the chain of type nodes hanging off of this
9804      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
9805      an ellipsis at the end.  */
9806
9807   /* In the case where we are describing a mere function declaration, all we
9808      need to do here (and all we *can* do here) is to describe the *types* of
9809      its formal parameters.  */
9810   if (debug_info_level <= DINFO_LEVEL_TERSE)
9811     ;
9812   else if (declaration)
9813     gen_formal_types_die (decl, subr_die);
9814   else
9815     {
9816       /* Generate DIEs to represent all known formal parameters */
9817       register tree arg_decls = DECL_ARGUMENTS (decl);
9818       register tree parm;
9819
9820       /* When generating DIEs, generate the unspecified_parameters DIE
9821          instead if we come across the arg "__builtin_va_alist" */
9822       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
9823         if (TREE_CODE (parm) == PARM_DECL)
9824           {
9825             if (DECL_NAME (parm)
9826                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
9827                             "__builtin_va_alist"))
9828               gen_unspecified_parameters_die (parm, subr_die);
9829             else
9830               gen_decl_die (parm, subr_die);
9831           }
9832
9833       /* Decide whether we need a unspecified_parameters DIE at the end.
9834          There are 2 more cases to do this for: 1) the ansi ... declaration -
9835          this is detectable when the end of the arg list is not a
9836          void_type_node 2) an unprototyped function declaration (not a
9837          definition).  This just means that we have no info about the
9838          parameters at all.  */
9839       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
9840       if (fn_arg_types != NULL)
9841         {
9842           /* this is the prototyped case, check for ...  */
9843           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
9844             gen_unspecified_parameters_die (decl, subr_die);
9845         }
9846       else if (DECL_INITIAL (decl) == NULL_TREE)
9847         gen_unspecified_parameters_die (decl, subr_die);
9848     }
9849
9850   /* Output Dwarf info for all of the stuff within the body of the function
9851      (if it has one - it may be just a declaration).  */
9852   outer_scope = DECL_INITIAL (decl);
9853
9854   /* Note that here, `outer_scope' is a pointer to the outermost BLOCK
9855      node created to represent a function. This outermost BLOCK actually
9856      represents the outermost binding contour for the function, i.e. the
9857      contour in which the function's formal parameters and labels get
9858      declared. Curiously, it appears that the front end doesn't actually
9859      put the PARM_DECL nodes for the current function onto the BLOCK_VARS
9860      list for this outer scope.  (They are strung off of the DECL_ARGUMENTS
9861      list for the function instead.) The BLOCK_VARS list for the
9862      `outer_scope' does provide us with a list of the LABEL_DECL nodes for
9863      the function however, and we output DWARF info for those in
9864      decls_for_scope.  Just within the `outer_scope' there will be a BLOCK
9865      node representing the function's outermost pair of curly braces, and
9866      any blocks used for the base and member initializers of a C++
9867      constructor function.  */
9868   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
9869     {
9870       current_function_has_inlines = 0;
9871       decls_for_scope (outer_scope, subr_die, 0);
9872
9873 #if 0 && defined (MIPS_DEBUGGING_INFO)
9874       if (current_function_has_inlines)
9875         {
9876           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
9877           if (! comp_unit_has_inlines)
9878             {
9879               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
9880               comp_unit_has_inlines = 1;
9881             }
9882         }
9883 #endif
9884     }
9885 }
9886
9887 /* Generate a DIE to represent a declared data object.  */
9888
9889 static void
9890 gen_variable_die (decl, context_die)
9891      register tree decl;
9892      register dw_die_ref context_die;
9893 {
9894   register tree origin = decl_ultimate_origin (decl);
9895   register dw_die_ref var_die = new_die (DW_TAG_variable, context_die);
9896
9897   dw_die_ref old_die = lookup_decl_die (decl);
9898   int declaration = (DECL_EXTERNAL (decl)
9899                      || class_scope_p (context_die));
9900
9901   if (origin != NULL)
9902     add_abstract_origin_attribute (var_die, origin);
9903   /* Loop unrolling can create multiple blocks that refer to the same
9904      static variable, so we must test for the DW_AT_declaration flag.  */
9905   /* ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
9906      copy decls and set the DECL_ABSTRACT flag on them instead of
9907      sharing them.  */
9908   else if (old_die && TREE_STATIC (decl)
9909            && get_AT_flag (old_die, DW_AT_declaration) == 1)
9910     {
9911       /* This is a definition of a C++ class level static.  */
9912       add_AT_die_ref (var_die, DW_AT_specification, old_die);
9913       if (DECL_NAME (decl))
9914         {
9915           unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
9916
9917           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
9918             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
9919
9920           if (get_AT_unsigned (old_die, DW_AT_decl_line)
9921               != (unsigned) DECL_SOURCE_LINE (decl))
9922
9923             add_AT_unsigned (var_die, DW_AT_decl_line,
9924                              DECL_SOURCE_LINE (decl));
9925         }
9926     }
9927   else
9928     {
9929       add_name_and_src_coords_attributes (var_die, decl);
9930       add_type_attribute (var_die, TREE_TYPE (decl),
9931                           TREE_READONLY (decl),
9932                           TREE_THIS_VOLATILE (decl), context_die);
9933
9934       if (TREE_PUBLIC (decl))
9935         add_AT_flag (var_die, DW_AT_external, 1);
9936
9937       if (DECL_ARTIFICIAL (decl))
9938         add_AT_flag (var_die, DW_AT_artificial, 1);
9939
9940       if (TREE_PROTECTED (decl))
9941         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
9942
9943       else if (TREE_PRIVATE (decl))
9944         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
9945     }
9946
9947   if (declaration)
9948     add_AT_flag (var_die, DW_AT_declaration, 1);
9949
9950   if (class_scope_p (context_die) || DECL_ABSTRACT (decl))
9951     equate_decl_number_to_die (decl, var_die);
9952
9953   if (! declaration && ! DECL_ABSTRACT (decl))
9954     {
9955       add_location_or_const_value_attribute (var_die, decl);
9956       add_pubname (decl, var_die);
9957     }
9958   else
9959     tree_add_const_value_attribute (var_die, decl);
9960 }
9961
9962 /* Generate a DIE to represent a label identifier.  */
9963
9964 static void
9965 gen_label_die (decl, context_die)
9966      register tree decl;
9967      register dw_die_ref context_die;
9968 {
9969   register tree origin = decl_ultimate_origin (decl);
9970   register dw_die_ref lbl_die = new_die (DW_TAG_label, context_die);
9971   register rtx insn;
9972   char label[MAX_ARTIFICIAL_LABEL_BYTES];
9973
9974   if (origin != NULL)
9975     add_abstract_origin_attribute (lbl_die, origin);
9976   else
9977     add_name_and_src_coords_attributes (lbl_die, decl);
9978
9979   if (DECL_ABSTRACT (decl))
9980     equate_decl_number_to_die (decl, lbl_die);
9981   else
9982     {
9983       insn = DECL_RTL (decl);
9984
9985       /* Deleted labels are programmer specified labels which have been
9986          eliminated because of various optimisations.  We still emit them
9987          here so that it is possible to put breakpoints on them.  */
9988       if (GET_CODE (insn) == CODE_LABEL
9989           || ((GET_CODE (insn) == NOTE
9990                && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
9991         {
9992           /* When optimization is enabled (via -O) some parts of the compiler
9993              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
9994              represent source-level labels which were explicitly declared by
9995              the user.  This really shouldn't be happening though, so catch
9996              it if it ever does happen.  */
9997           if (INSN_DELETED_P (insn))
9998             abort ();
9999
10000           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
10001           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
10002         }
10003     }
10004 }
10005
10006 /* Generate a DIE for a lexical block.  */
10007
10008 static void
10009 gen_lexical_block_die (stmt, context_die, depth)
10010      register tree stmt;
10011      register dw_die_ref context_die;
10012      int depth;
10013 {
10014   register dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die);
10015   char label[MAX_ARTIFICIAL_LABEL_BYTES];
10016
10017   if (! BLOCK_ABSTRACT (stmt))
10018     {
10019       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10020                                    BLOCK_NUMBER (stmt));
10021       add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
10022       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10023                                    BLOCK_NUMBER (stmt));
10024       add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
10025     }
10026
10027   decls_for_scope (stmt, stmt_die, depth);
10028 }
10029
10030 /* Generate a DIE for an inlined subprogram.  */
10031
10032 static void
10033 gen_inlined_subroutine_die (stmt, context_die, depth)
10034      register tree stmt;
10035      register dw_die_ref context_die;
10036      int depth;
10037 {
10038   if (! BLOCK_ABSTRACT (stmt))
10039     {
10040       register dw_die_ref subr_die
10041         = new_die (DW_TAG_inlined_subroutine, context_die);
10042       register tree decl = block_ultimate_origin (stmt);
10043       char label[MAX_ARTIFICIAL_LABEL_BYTES];
10044
10045       /* Emit info for the abstract instance first, if we haven't yet.  */
10046       dwarf2out_abstract_function (decl);
10047
10048       add_abstract_origin_attribute (subr_die, decl);
10049       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10050                                    BLOCK_NUMBER (stmt));
10051       add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
10052       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10053                                    BLOCK_NUMBER (stmt));
10054       add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
10055       decls_for_scope (stmt, subr_die, depth);
10056       current_function_has_inlines = 1;
10057     }
10058 }
10059
10060 /* Generate a DIE for a field in a record, or structure.  */
10061
10062 static void
10063 gen_field_die (decl, context_die)
10064      register tree decl;
10065      register dw_die_ref context_die;
10066 {
10067   register dw_die_ref decl_die = new_die (DW_TAG_member, context_die);
10068
10069   add_name_and_src_coords_attributes (decl_die, decl);
10070   add_type_attribute (decl_die, member_declared_type (decl),
10071                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
10072                       context_die);
10073
10074   /* If this is a bit field...  */
10075   if (DECL_BIT_FIELD_TYPE (decl))
10076     {
10077       add_byte_size_attribute (decl_die, decl);
10078       add_bit_size_attribute (decl_die, decl);
10079       add_bit_offset_attribute (decl_die, decl);
10080     }
10081
10082   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
10083     add_data_member_location_attribute (decl_die, decl);
10084
10085   if (DECL_ARTIFICIAL (decl))
10086     add_AT_flag (decl_die, DW_AT_artificial, 1);
10087
10088   if (TREE_PROTECTED (decl))
10089     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
10090
10091   else if (TREE_PRIVATE (decl))
10092     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
10093 }
10094
10095 #if 0
10096 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10097    Use modified_type_die instead.
10098    We keep this code here just in case these types of DIEs may be needed to
10099    represent certain things in other languages (e.g. Pascal) someday.  */
10100 static void
10101 gen_pointer_type_die (type, context_die)
10102      register tree type;
10103      register dw_die_ref context_die;
10104 {
10105   register dw_die_ref ptr_die
10106     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die));
10107
10108   equate_type_number_to_die (type, ptr_die);
10109   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10110   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10111 }
10112
10113 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10114    Use modified_type_die instead.
10115    We keep this code here just in case these types of DIEs may be needed to
10116    represent certain things in other languages (e.g. Pascal) someday.  */
10117 static void
10118 gen_reference_type_die (type, context_die)
10119      register tree type;
10120      register dw_die_ref context_die;
10121 {
10122   register dw_die_ref ref_die
10123     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die));
10124
10125   equate_type_number_to_die (type, ref_die);
10126   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
10127   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10128 }
10129 #endif
10130
10131 /* Generate a DIE for a pointer to a member type.  */
10132 static void
10133 gen_ptr_to_mbr_type_die (type, context_die)
10134      register tree type;
10135      register dw_die_ref context_die;
10136 {
10137   register dw_die_ref ptr_die
10138     = new_die (DW_TAG_ptr_to_member_type, scope_die_for (type, context_die));
10139
10140   equate_type_number_to_die (type, ptr_die);
10141   add_AT_die_ref (ptr_die, DW_AT_containing_type,
10142                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
10143   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10144 }
10145
10146 /* Generate the DIE for the compilation unit.  */
10147
10148 static dw_die_ref
10149 gen_compile_unit_die (filename)
10150      register const char *filename;
10151 {
10152   register dw_die_ref die;
10153   char producer[250];
10154   const char *wd = getpwd ();
10155   int language;
10156
10157   die = new_die (DW_TAG_compile_unit, NULL);
10158   add_name_attribute (die, filename);
10159
10160   if (wd != NULL && filename[0] != DIR_SEPARATOR)
10161     add_AT_string (die, DW_AT_comp_dir, wd);
10162
10163   sprintf (producer, "%s %s", language_string, version_string);
10164
10165 #ifdef MIPS_DEBUGGING_INFO
10166   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
10167      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
10168      not appear in the producer string, the debugger reaches the conclusion
10169      that the object file is stripped and has no debugging information.
10170      To get the MIPS/SGI debugger to believe that there is debugging
10171      information in the object file, we add a -g to the producer string.  */
10172   if (debug_info_level > DINFO_LEVEL_TERSE)
10173     strcat (producer, " -g");
10174 #endif
10175
10176   add_AT_string (die, DW_AT_producer, producer);
10177
10178   if (strcmp (language_string, "GNU C++") == 0)
10179     language = DW_LANG_C_plus_plus;
10180   else if (strcmp (language_string, "GNU Ada") == 0)
10181     language = DW_LANG_Ada83;
10182   else if (strcmp (language_string, "GNU F77") == 0)
10183     language = DW_LANG_Fortran77;
10184   else if (strcmp (language_string, "GNU Pascal") == 0)
10185     language = DW_LANG_Pascal83;
10186   else if (strcmp (language_string, "GNU Java") == 0)
10187     language = DW_LANG_Java;
10188   else if (flag_traditional)
10189     language = DW_LANG_C;
10190   else
10191     language = DW_LANG_C89;
10192
10193   add_AT_unsigned (die, DW_AT_language, language);
10194
10195   return die;
10196 }
10197
10198 /* Generate a DIE for a string type.  */
10199
10200 static void
10201 gen_string_type_die (type, context_die)
10202      register tree type;
10203      register dw_die_ref context_die;
10204 {
10205   register dw_die_ref type_die
10206     = new_die (DW_TAG_string_type, scope_die_for (type, context_die));
10207
10208   equate_type_number_to_die (type, type_die);
10209
10210   /* Fudge the string length attribute for now.  */
10211
10212   /* TODO: add string length info.
10213    string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
10214                               bound_representation (upper_bound, 0, 'u'); */
10215 }
10216
10217 /* Generate the DIE for a base class.  */
10218
10219 static void
10220 gen_inheritance_die (binfo, context_die)
10221      register tree binfo;
10222      register dw_die_ref context_die;
10223 {
10224   dw_die_ref die = new_die (DW_TAG_inheritance, context_die);
10225
10226   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
10227   add_data_member_location_attribute (die, binfo);
10228
10229   if (TREE_VIA_VIRTUAL (binfo))
10230     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10231   if (TREE_VIA_PUBLIC (binfo))
10232     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
10233   else if (TREE_VIA_PROTECTED (binfo))
10234     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
10235 }
10236
10237 /* Generate a DIE for a class member.  */
10238
10239 static void
10240 gen_member_die (type, context_die)
10241      register tree type;
10242      register dw_die_ref context_die;
10243 {
10244   register tree member;
10245   dw_die_ref child;
10246
10247   /* If this is not an incomplete type, output descriptions of each of its
10248      members. Note that as we output the DIEs necessary to represent the
10249      members of this record or union type, we will also be trying to output
10250      DIEs to represent the *types* of those members. However the `type'
10251      function (above) will specifically avoid generating type DIEs for member
10252      types *within* the list of member DIEs for this (containing) type execpt
10253      for those types (of members) which are explicitly marked as also being
10254      members of this (containing) type themselves.  The g++ front- end can
10255      force any given type to be treated as a member of some other
10256      (containing) type by setting the TYPE_CONTEXT of the given (member) type
10257      to point to the TREE node representing the appropriate (containing)
10258      type.  */
10259
10260   /* First output info about the base classes.  */
10261   if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
10262     {
10263       register tree bases = TYPE_BINFO_BASETYPES (type);
10264       register int n_bases = TREE_VEC_LENGTH (bases);
10265       register int i;
10266
10267       for (i = 0; i < n_bases; i++)
10268         gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
10269     }
10270
10271   /* Now output info about the data members and type members.  */
10272   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
10273     {
10274       /* If we thought we were generating minimal debug info for TYPE
10275          and then changed our minds, some of the member declarations
10276          may have already been defined.  Don't define them again, but
10277          do put them in the right order.  */
10278
10279       child = lookup_decl_die (member);
10280       if (child)
10281         splice_child_die (context_die, child);
10282       else
10283         gen_decl_die (member, context_die);
10284     }
10285
10286   /* Now output info about the function members (if any).  */
10287   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
10288     {
10289       /* Don't include clones in the member list.  */
10290       if (DECL_ABSTRACT_ORIGIN (member))
10291         continue;
10292
10293       child = lookup_decl_die (member);
10294       if (child)
10295         splice_child_die (context_die, child);
10296       else
10297         gen_decl_die (member, context_die);
10298     }
10299 }
10300
10301 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
10302    is set, we pretend that the type was never defined, so we only get the
10303    member DIEs needed by later specification DIEs.  */
10304
10305 static void
10306 gen_struct_or_union_type_die (type, context_die)
10307      register tree type;
10308      register dw_die_ref context_die;
10309 {
10310   register dw_die_ref type_die = lookup_type_die (type);
10311   register dw_die_ref scope_die = 0;
10312   register int nested = 0;
10313   int complete = (TYPE_SIZE (type)
10314                   && (! TYPE_STUB_DECL (type)
10315                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
10316
10317   if (type_die && ! complete)
10318     return;
10319
10320   if (TYPE_CONTEXT (type) != NULL_TREE
10321       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type)))
10322     nested = 1;
10323
10324   scope_die = scope_die_for (type, context_die);
10325
10326   if (! type_die || (nested && scope_die == comp_unit_die))
10327     /* First occurrence of type or toplevel definition of nested class.  */
10328     {
10329       register dw_die_ref old_die = type_die;
10330
10331       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
10332                           ? DW_TAG_structure_type : DW_TAG_union_type,
10333                           scope_die);
10334       equate_type_number_to_die (type, type_die);
10335       if (old_die)
10336         add_AT_die_ref (type_die, DW_AT_specification, old_die);
10337       else
10338         add_name_attribute (type_die, type_tag (type));
10339     }
10340   else
10341     remove_AT (type_die, DW_AT_declaration);
10342
10343   /* If this type has been completed, then give it a byte_size attribute and
10344      then give a list of members.  */
10345   if (complete)
10346     {
10347       /* Prevent infinite recursion in cases where the type of some member of
10348          this type is expressed in terms of this type itself.  */
10349       TREE_ASM_WRITTEN (type) = 1;
10350       add_byte_size_attribute (type_die, type);
10351       if (TYPE_STUB_DECL (type) != NULL_TREE)
10352         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10353
10354       /* If the first reference to this type was as the return type of an
10355          inline function, then it may not have a parent.  Fix this now.  */
10356       if (type_die->die_parent == NULL)
10357         add_child_die (scope_die, type_die);
10358
10359       push_decl_scope (type);
10360       gen_member_die (type, type_die);
10361       pop_decl_scope ();
10362
10363       /* GNU extension: Record what type our vtable lives in.  */
10364       if (TYPE_VFIELD (type))
10365         {
10366           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
10367
10368           gen_type_die (vtype, context_die);
10369           add_AT_die_ref (type_die, DW_AT_containing_type,
10370                           lookup_type_die (vtype));
10371         }
10372     }
10373   else
10374     {
10375       add_AT_flag (type_die, DW_AT_declaration, 1);
10376
10377       /* We don't need to do this for function-local types.  */
10378       if (! decl_function_context (TYPE_STUB_DECL (type)))
10379         add_incomplete_type (type);
10380     }
10381 }
10382
10383 /* Generate a DIE for a subroutine _type_.  */
10384
10385 static void
10386 gen_subroutine_type_die (type, context_die)
10387      register tree type;
10388      register dw_die_ref context_die;
10389 {
10390   register tree return_type = TREE_TYPE (type);
10391   register dw_die_ref subr_die
10392     = new_die (DW_TAG_subroutine_type, scope_die_for (type, context_die));
10393
10394   equate_type_number_to_die (type, subr_die);
10395   add_prototyped_attribute (subr_die, type);
10396   add_type_attribute (subr_die, return_type, 0, 0, context_die);
10397   gen_formal_types_die (type, subr_die);
10398 }
10399
10400 /* Generate a DIE for a type definition */
10401
10402 static void
10403 gen_typedef_die (decl, context_die)
10404      register tree decl;
10405      register dw_die_ref context_die;
10406 {
10407   register dw_die_ref type_die;
10408   register tree origin;
10409
10410   if (TREE_ASM_WRITTEN (decl))
10411     return;
10412   TREE_ASM_WRITTEN (decl) = 1;
10413
10414   type_die = new_die (DW_TAG_typedef, context_die);
10415   origin = decl_ultimate_origin (decl);
10416   if (origin != NULL)
10417     add_abstract_origin_attribute (type_die, origin);
10418   else
10419     {
10420       register tree type;
10421       add_name_and_src_coords_attributes (type_die, decl);
10422       if (DECL_ORIGINAL_TYPE (decl))
10423         {
10424           type = DECL_ORIGINAL_TYPE (decl);
10425
10426           if (type == TREE_TYPE (decl))
10427             abort ();
10428           else
10429             equate_type_number_to_die (TREE_TYPE (decl), type_die);
10430         }
10431       else
10432         type = TREE_TYPE (decl);
10433       add_type_attribute (type_die, type, TREE_READONLY (decl),
10434                           TREE_THIS_VOLATILE (decl), context_die);
10435     }
10436
10437   if (DECL_ABSTRACT (decl))
10438     equate_decl_number_to_die (decl, type_die);
10439 }
10440
10441 /* Generate a type description DIE.  */
10442
10443 static void
10444 gen_type_die (type, context_die)
10445      register tree type;
10446      register dw_die_ref context_die;
10447 {
10448   int need_pop;
10449
10450   if (type == NULL_TREE || type == error_mark_node)
10451     return;
10452
10453   /* We are going to output a DIE to represent the unqualified version of
10454      this type (i.e. without any const or volatile qualifiers) so get the
10455      main variant (i.e. the unqualified version) of this type now.  */
10456   type = type_main_variant (type);
10457
10458   if (TREE_ASM_WRITTEN (type))
10459     return;
10460
10461   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10462       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
10463     {
10464       TREE_ASM_WRITTEN (type) = 1;
10465       gen_decl_die (TYPE_NAME (type), context_die);
10466       return;
10467     }
10468
10469   switch (TREE_CODE (type))
10470     {
10471     case ERROR_MARK:
10472       break;
10473
10474     case POINTER_TYPE:
10475     case REFERENCE_TYPE:
10476       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
10477          ensures that the gen_type_die recursion will terminate even if the
10478          type is recursive.  Recursive types are possible in Ada.  */
10479       /* ??? We could perhaps do this for all types before the switch
10480          statement.  */
10481       TREE_ASM_WRITTEN (type) = 1;
10482
10483       /* For these types, all that is required is that we output a DIE (or a
10484          set of DIEs) to represent the "basis" type.  */
10485       gen_type_die (TREE_TYPE (type), context_die);
10486       break;
10487
10488     case OFFSET_TYPE:
10489       /* This code is used for C++ pointer-to-data-member types.
10490          Output a description of the relevant class type.  */
10491       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
10492
10493       /* Output a description of the type of the object pointed to.  */
10494       gen_type_die (TREE_TYPE (type), context_die);
10495
10496       /* Now output a DIE to represent this pointer-to-data-member type
10497          itself.  */
10498       gen_ptr_to_mbr_type_die (type, context_die);
10499       break;
10500
10501     case SET_TYPE:
10502       gen_type_die (TYPE_DOMAIN (type), context_die);
10503       gen_set_type_die (type, context_die);
10504       break;
10505
10506     case FILE_TYPE:
10507       gen_type_die (TREE_TYPE (type), context_die);
10508       abort ();                 /* No way to represent these in Dwarf yet!  */
10509       break;
10510
10511     case FUNCTION_TYPE:
10512       /* Force out return type (in case it wasn't forced out already).  */
10513       gen_type_die (TREE_TYPE (type), context_die);
10514       gen_subroutine_type_die (type, context_die);
10515       break;
10516
10517     case METHOD_TYPE:
10518       /* Force out return type (in case it wasn't forced out already).  */
10519       gen_type_die (TREE_TYPE (type), context_die);
10520       gen_subroutine_type_die (type, context_die);
10521       break;
10522
10523     case ARRAY_TYPE:
10524       if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
10525         {
10526           gen_type_die (TREE_TYPE (type), context_die);
10527           gen_string_type_die (type, context_die);
10528         }
10529       else
10530         gen_array_type_die (type, context_die);
10531       break;
10532
10533     case VECTOR_TYPE:
10534       gen_type_die (TYPE_DEBUG_REPRESENTATION_TYPE (type), context_die);
10535       break;
10536
10537     case ENUMERAL_TYPE:
10538     case RECORD_TYPE:
10539     case UNION_TYPE:
10540     case QUAL_UNION_TYPE:
10541       /* If this is a nested type whose containing class hasn't been
10542          written out yet, writing it out will cover this one, too.
10543          This does not apply to instantiations of member class templates;
10544          they need to be added to the containing class as they are
10545          generated.  FIXME: This hurts the idea of combining type decls
10546          from multiple TUs, since we can't predict what set of template
10547          instantiations we'll get.  */
10548       if (TYPE_CONTEXT (type)
10549           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
10550           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
10551         {
10552           gen_type_die (TYPE_CONTEXT (type), context_die);
10553
10554           if (TREE_ASM_WRITTEN (type))
10555             return;
10556
10557           /* If that failed, attach ourselves to the stub.  */
10558           push_decl_scope (TYPE_CONTEXT (type));
10559           context_die = lookup_type_die (TYPE_CONTEXT (type));
10560           need_pop = 1;
10561         }
10562       else
10563         need_pop = 0;
10564
10565       if (TREE_CODE (type) == ENUMERAL_TYPE)
10566         gen_enumeration_type_die (type, context_die);
10567       else
10568         gen_struct_or_union_type_die (type, context_die);
10569
10570       if (need_pop)
10571         pop_decl_scope ();
10572
10573       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
10574          it up if it is ever completed.  gen_*_type_die will set it for us
10575          when appropriate.  */
10576       return;
10577
10578     case VOID_TYPE:
10579     case INTEGER_TYPE:
10580     case REAL_TYPE:
10581     case COMPLEX_TYPE:
10582     case BOOLEAN_TYPE:
10583     case CHAR_TYPE:
10584       /* No DIEs needed for fundamental types.  */
10585       break;
10586
10587     case LANG_TYPE:
10588       /* No Dwarf representation currently defined.  */
10589       break;
10590
10591     default:
10592       abort ();
10593     }
10594
10595   TREE_ASM_WRITTEN (type) = 1;
10596 }
10597
10598 /* Generate a DIE for a tagged type instantiation.  */
10599
10600 static void
10601 gen_tagged_type_instantiation_die (type, context_die)
10602      register tree type;
10603      register dw_die_ref context_die;
10604 {
10605   if (type == NULL_TREE || type == error_mark_node)
10606     return;
10607
10608   /* We are going to output a DIE to represent the unqualified version of
10609      this type (i.e. without any const or volatile qualifiers) so make sure
10610      that we have the main variant (i.e. the unqualified version) of this
10611      type now.  */
10612   if (type != type_main_variant (type))
10613     abort ();
10614
10615   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
10616      an instance of an unresolved type.  */
10617
10618   switch (TREE_CODE (type))
10619     {
10620     case ERROR_MARK:
10621       break;
10622
10623     case ENUMERAL_TYPE:
10624       gen_inlined_enumeration_type_die (type, context_die);
10625       break;
10626
10627     case RECORD_TYPE:
10628       gen_inlined_structure_type_die (type, context_die);
10629       break;
10630
10631     case UNION_TYPE:
10632     case QUAL_UNION_TYPE:
10633       gen_inlined_union_type_die (type, context_die);
10634       break;
10635
10636     default:
10637       abort ();
10638     }
10639 }
10640
10641 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
10642    things which are local to the given block.  */
10643
10644 static void
10645 gen_block_die (stmt, context_die, depth)
10646      register tree stmt;
10647      register dw_die_ref context_die;
10648      int depth;
10649 {
10650   register int must_output_die = 0;
10651   register tree origin;
10652   register tree decl;
10653   register enum tree_code origin_code;
10654
10655   /* Ignore blocks never really used to make RTL.  */
10656
10657   if (stmt == NULL_TREE || !TREE_USED (stmt)
10658       || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
10659     return;
10660
10661   /* Determine the "ultimate origin" of this block.  This block may be an
10662      inlined instance of an inlined instance of inline function, so we have
10663      to trace all of the way back through the origin chain to find out what
10664      sort of node actually served as the original seed for the creation of
10665      the current block.  */
10666   origin = block_ultimate_origin (stmt);
10667   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
10668
10669   /* Determine if we need to output any Dwarf DIEs at all to represent this
10670      block.  */
10671   if (origin_code == FUNCTION_DECL)
10672     /* The outer scopes for inlinings *must* always be represented.  We
10673        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
10674     must_output_die = 1;
10675   else
10676     {
10677       /* In the case where the current block represents an inlining of the
10678          "body block" of an inline function, we must *NOT* output any DIE for
10679          this block because we have already output a DIE to represent the
10680          whole inlined function scope and the "body block" of any function
10681          doesn't really represent a different scope according to ANSI C
10682          rules.  So we check here to make sure that this block does not
10683          represent a "body block inlining" before trying to set the
10684          `must_output_die' flag.  */
10685       if (! is_body_block (origin ? origin : stmt))
10686         {
10687           /* Determine if this block directly contains any "significant"
10688              local declarations which we will need to output DIEs for.  */
10689           if (debug_info_level > DINFO_LEVEL_TERSE)
10690             /* We are not in terse mode so *any* local declaration counts
10691                as being a "significant" one.  */
10692             must_output_die = (BLOCK_VARS (stmt) != NULL);
10693           else
10694             /* We are in terse mode, so only local (nested) function
10695                definitions count as "significant" local declarations.  */
10696             for (decl = BLOCK_VARS (stmt);
10697                  decl != NULL; decl = TREE_CHAIN (decl))
10698               if (TREE_CODE (decl) == FUNCTION_DECL
10699                   && DECL_INITIAL (decl))
10700                 {
10701                   must_output_die = 1;
10702                   break;
10703                 }
10704         }
10705     }
10706
10707   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
10708      DIE for any block which contains no significant local declarations at
10709      all.  Rather, in such cases we just call `decls_for_scope' so that any
10710      needed Dwarf info for any sub-blocks will get properly generated. Note
10711      that in terse mode, our definition of what constitutes a "significant"
10712      local declaration gets restricted to include only inlined function
10713      instances and local (nested) function definitions.  */
10714   if (must_output_die)
10715     {
10716       if (origin_code == FUNCTION_DECL)
10717         gen_inlined_subroutine_die (stmt, context_die, depth);
10718       else
10719         gen_lexical_block_die (stmt, context_die, depth);
10720     }
10721   else
10722     decls_for_scope (stmt, context_die, depth);
10723 }
10724
10725 /* Generate all of the decls declared within a given scope and (recursively)
10726    all of its sub-blocks.  */
10727
10728 static void
10729 decls_for_scope (stmt, context_die, depth)
10730      register tree stmt;
10731      register dw_die_ref context_die;
10732      int depth;
10733 {
10734   register tree decl;
10735   register tree subblocks;
10736
10737   /* Ignore blocks never really used to make RTL.  */
10738   if (stmt == NULL_TREE || ! TREE_USED (stmt))
10739     return;
10740
10741   /* Output the DIEs to represent all of the data objects and typedefs
10742      declared directly within this block but not within any nested
10743      sub-blocks.  Also, nested function and tag DIEs have been
10744      generated with a parent of NULL; fix that up now.  */
10745   for (decl = BLOCK_VARS (stmt);
10746        decl != NULL; decl = TREE_CHAIN (decl))
10747     {
10748       register dw_die_ref die;
10749
10750       if (TREE_CODE (decl) == FUNCTION_DECL)
10751         die = lookup_decl_die (decl);
10752       else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
10753         die = lookup_type_die (TREE_TYPE (decl));
10754       else
10755         die = NULL;
10756
10757       if (die != NULL && die->die_parent == NULL)
10758         add_child_die (context_die, die);
10759       else
10760         gen_decl_die (decl, context_die);
10761     }
10762
10763   /* Output the DIEs to represent all sub-blocks (and the items declared
10764      therein) of this block.  */
10765   for (subblocks = BLOCK_SUBBLOCKS (stmt);
10766        subblocks != NULL;
10767        subblocks = BLOCK_CHAIN (subblocks))
10768     gen_block_die (subblocks, context_die, depth + 1);
10769 }
10770
10771 /* Is this a typedef we can avoid emitting?  */
10772
10773 static inline int
10774 is_redundant_typedef (decl)
10775      register tree decl;
10776 {
10777   if (TYPE_DECL_IS_STUB (decl))
10778     return 1;
10779
10780   if (DECL_ARTIFICIAL (decl)
10781       && DECL_CONTEXT (decl)
10782       && is_tagged_type (DECL_CONTEXT (decl))
10783       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
10784       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
10785     /* Also ignore the artificial member typedef for the class name.  */
10786     return 1;
10787
10788   return 0;
10789 }
10790
10791 /* Generate Dwarf debug information for a decl described by DECL.  */
10792
10793 static void
10794 gen_decl_die (decl, context_die)
10795      register tree decl;
10796      register dw_die_ref context_die;
10797 {
10798   register tree origin;
10799
10800   if (TREE_CODE (decl) == ERROR_MARK)
10801     return;
10802
10803   /* If this ..._DECL node is marked to be ignored, then ignore it.  */
10804   if (DECL_IGNORED_P (decl))
10805     return;
10806
10807   switch (TREE_CODE (decl))
10808     {
10809     case CONST_DECL:
10810       /* The individual enumerators of an enum type get output when we output
10811          the Dwarf representation of the relevant enum type itself.  */
10812       break;
10813
10814     case FUNCTION_DECL:
10815       /* Don't output any DIEs to represent mere function declarations,
10816          unless they are class members or explicit block externs.  */
10817       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
10818           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
10819         break;
10820
10821       /* If we're emitting a clone, emit info for the abstract instance.  */
10822       if (DECL_ORIGIN (decl) != decl)
10823         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
10824       /* If we're emitting an out-of-line copy of an inline function,
10825          emit info for the abstract instance and set up to refer to it.  */
10826       else if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
10827                && ! class_scope_p (context_die)
10828                /* dwarf2out_abstract_function won't emit a die if this is just
10829                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
10830                   that case, because that works only if we have a die.  */
10831                && DECL_INITIAL (decl) != NULL_TREE)
10832         {
10833           dwarf2out_abstract_function (decl);
10834           set_decl_origin_self (decl);
10835         }
10836       /* Otherwise we're emitting the primary DIE for this decl.  */
10837       else if (debug_info_level > DINFO_LEVEL_TERSE)
10838         {
10839           /* Before we describe the FUNCTION_DECL itself, make sure that we
10840              have described its return type.  */
10841           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
10842
10843           /* And its virtual context.  */
10844           if (DECL_VINDEX (decl) != NULL_TREE)
10845             gen_type_die (DECL_CONTEXT (decl), context_die);
10846
10847           /* And its containing type.  */
10848           origin = decl_class_context (decl);
10849           if (origin != NULL_TREE)
10850             gen_type_die_for_member (origin, decl, context_die);
10851         }
10852
10853       /* Now output a DIE to represent the function itself.  */
10854       gen_subprogram_die (decl, context_die);
10855       break;
10856
10857     case TYPE_DECL:
10858       /* If we are in terse mode, don't generate any DIEs to represent any
10859          actual typedefs.  */
10860       if (debug_info_level <= DINFO_LEVEL_TERSE)
10861         break;
10862
10863       /* In the special case of a TYPE_DECL node representing the
10864          declaration of some type tag, if the given TYPE_DECL is marked as
10865          having been instantiated from some other (original) TYPE_DECL node
10866          (e.g. one which was generated within the original definition of an
10867          inline function) we have to generate a special (abbreviated)
10868          DW_TAG_structure_type, DW_TAG_union_type, or DW_TAG_enumeration_type
10869          DIE here.  */
10870       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
10871         {
10872           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
10873           break;
10874         }
10875
10876       if (is_redundant_typedef (decl))
10877         gen_type_die (TREE_TYPE (decl), context_die);
10878       else
10879         /* Output a DIE to represent the typedef itself.  */
10880         gen_typedef_die (decl, context_die);
10881       break;
10882
10883     case LABEL_DECL:
10884       if (debug_info_level >= DINFO_LEVEL_NORMAL)
10885         gen_label_die (decl, context_die);
10886       break;
10887
10888     case VAR_DECL:
10889       /* If we are in terse mode, don't generate any DIEs to represent any
10890          variable declarations or definitions.  */
10891       if (debug_info_level <= DINFO_LEVEL_TERSE)
10892         break;
10893
10894       /* Output any DIEs that are needed to specify the type of this data
10895          object.  */
10896       gen_type_die (TREE_TYPE (decl), context_die);
10897
10898       /* And its containing type.  */
10899       origin = decl_class_context (decl);
10900       if (origin != NULL_TREE)
10901         gen_type_die_for_member (origin, decl, context_die);
10902
10903       /* Now output the DIE to represent the data object itself.  This gets
10904          complicated because of the possibility that the VAR_DECL really
10905          represents an inlined instance of a formal parameter for an inline
10906          function.  */
10907       origin = decl_ultimate_origin (decl);
10908       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
10909         gen_formal_parameter_die (decl, context_die);
10910       else
10911         gen_variable_die (decl, context_die);
10912       break;
10913
10914     case FIELD_DECL:
10915       /* Ignore the nameless fields that are used to skip bits, but
10916          handle C++ anonymous unions.  */
10917       if (DECL_NAME (decl) != NULL_TREE
10918           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
10919         {
10920           gen_type_die (member_declared_type (decl), context_die);
10921           gen_field_die (decl, context_die);
10922         }
10923       break;
10924
10925     case PARM_DECL:
10926       gen_type_die (TREE_TYPE (decl), context_die);
10927       gen_formal_parameter_die (decl, context_die);
10928       break;
10929
10930     case NAMESPACE_DECL:
10931       /* Ignore for now.  */
10932       break;
10933
10934     default:
10935       abort ();
10936     }
10937 }
10938 \f
10939 /* Add Ada "use" clause information for SGI Workshop debugger.  */
10940
10941 void
10942 dwarf2out_add_library_unit_info (filename, context_list)
10943      const char *filename;
10944      const char *context_list;
10945 {
10946   unsigned int file_index;
10947
10948   if (filename != NULL)
10949     {
10950       dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die);
10951       tree context_list_decl
10952         = build_decl (LABEL_DECL, get_identifier (context_list),
10953                       void_type_node);
10954
10955       TREE_PUBLIC (context_list_decl) = TRUE;
10956       add_name_attribute (unit_die, context_list);
10957       file_index = lookup_filename (filename);
10958       add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
10959       add_pubname (context_list_decl, unit_die);
10960     }
10961 }
10962
10963 /* Write the debugging output for DECL.  */
10964
10965 void
10966 dwarf2out_decl (decl)
10967      register tree decl;
10968 {
10969   register dw_die_ref context_die = comp_unit_die;
10970
10971   if (TREE_CODE (decl) == ERROR_MARK)
10972     return;
10973
10974   /* If this ..._DECL node is marked to be ignored, then ignore it.  */
10975   if (DECL_IGNORED_P (decl))
10976     return;
10977
10978   switch (TREE_CODE (decl))
10979     {
10980     case FUNCTION_DECL:
10981       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a
10982          builtin function.  Explicit programmer-supplied declarations of
10983          these same functions should NOT be ignored however.  */
10984       if (DECL_EXTERNAL (decl) && DECL_BUILT_IN (decl))
10985         return;
10986
10987       /* What we would really like to do here is to filter out all mere
10988          file-scope declarations of file-scope functions which are never
10989          referenced later within this translation unit (and keep all of ones
10990          that *are* referenced later on) but we aren't clairvoyant, so we have
10991          no idea which functions will be referenced in the future (i.e. later
10992          on within the current translation unit). So here we just ignore all
10993          file-scope function declarations which are not also definitions.  If
10994          and when the debugger needs to know something about these functions,
10995          it will have to hunt around and find the DWARF information associated
10996          with the definition of the function.  Note that we can't just check
10997          `DECL_EXTERNAL' to find out which FUNCTION_DECL nodes represent
10998          definitions and which ones represent mere declarations.  We have to
10999          check `DECL_INITIAL' instead. That's because the C front-end
11000          supports some weird semantics for "extern inline" function
11001          definitions.  These can get inlined within the current translation
11002          unit (an thus, we need to generate DWARF info for their abstract
11003          instances so that the DWARF info for the concrete inlined instances
11004          can have something to refer to) but the compiler never generates any
11005          out-of-lines instances of such things (despite the fact that they
11006          *are* definitions).  The important point is that the C front-end
11007          marks these "extern inline" functions as DECL_EXTERNAL, but we need
11008          to generate DWARF for them anyway. Note that the C++ front-end also
11009          plays some similar games for inline function definitions appearing
11010          within include files which also contain
11011          `#pragma interface' pragmas.  */
11012       if (DECL_INITIAL (decl) == NULL_TREE)
11013         return;
11014
11015       /* If we're a nested function, initially use a parent of NULL; if we're
11016          a plain function, this will be fixed up in decls_for_scope.  If
11017          we're a method, it will be ignored, since we already have a DIE.  */
11018       if (decl_function_context (decl))
11019         context_die = NULL;
11020
11021       break;
11022
11023     case VAR_DECL:
11024       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
11025          declaration and if the declaration was never even referenced from
11026          within this entire compilation unit.  We suppress these DIEs in
11027          order to save space in the .debug section (by eliminating entries
11028          which are probably useless).  Note that we must not suppress
11029          block-local extern declarations (whether used or not) because that
11030          would screw-up the debugger's name lookup mechanism and cause it to
11031          miss things which really ought to be in scope at a given point.  */
11032       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
11033         return;
11034
11035       /* If we are in terse mode, don't generate any DIEs to represent any
11036          variable declarations or definitions.  */
11037       if (debug_info_level <= DINFO_LEVEL_TERSE)
11038         return;
11039       break;
11040
11041     case TYPE_DECL:
11042       /* Don't emit stubs for types unless they are needed by other DIEs.  */
11043       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
11044         return;
11045
11046       /* Don't bother trying to generate any DIEs to represent any of the
11047          normal built-in types for the language we are compiling.  */
11048       if (DECL_SOURCE_LINE (decl) == 0)
11049         {
11050           /* OK, we need to generate one for `bool' so GDB knows what type
11051              comparisons have.  */
11052           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
11053                == DW_LANG_C_plus_plus)
11054               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE)
11055             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
11056
11057           return;
11058         }
11059
11060       /* If we are in terse mode, don't generate any DIEs for types.  */
11061       if (debug_info_level <= DINFO_LEVEL_TERSE)
11062         return;
11063
11064       /* If we're a function-scope tag, initially use a parent of NULL;
11065          this will be fixed up in decls_for_scope.  */
11066       if (decl_function_context (decl))
11067         context_die = NULL;
11068
11069       break;
11070
11071     default:
11072       return;
11073     }
11074
11075   gen_decl_die (decl, context_die);
11076 }
11077
11078 /* Output a marker (i.e. a label) for the beginning of the generated code for
11079    a lexical block.  */
11080
11081 void
11082 dwarf2out_begin_block (blocknum)
11083      register unsigned blocknum;
11084 {
11085   function_section (current_function_decl);
11086   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
11087 }
11088
11089 /* Output a marker (i.e. a label) for the end of the generated code for a
11090    lexical block.  */
11091
11092 void
11093 dwarf2out_end_block (blocknum)
11094      register unsigned blocknum;
11095 {
11096   function_section (current_function_decl);
11097   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
11098 }
11099
11100 /* Returns nonzero if it is appropriate not to emit any debugging
11101    information for BLOCK, because it doesn't contain any instructions.
11102
11103    Don't allow this for blocks with nested functions or local classes
11104    as we would end up with orphans, and in the presence of scheduling
11105    we may end up calling them anyway.  */
11106
11107 int
11108 dwarf2out_ignore_block (block)
11109      tree block;
11110 {
11111   tree decl;
11112   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
11113     if (TREE_CODE (decl) == FUNCTION_DECL
11114         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
11115       return 0;
11116   return 1;
11117 }
11118
11119 /* Lookup a filename (in the list of filenames that we know about here in
11120    dwarf2out.c) and return its "index".  The index of each (known) filename is
11121    just a unique number which is associated with only that one filename.
11122    We need such numbers for the sake of generating labels
11123    (in the .debug_sfnames section) and references to those
11124    files  numbers (in the .debug_srcinfo and.debug_macinfo sections).
11125    If the filename given as an argument is not found in our current list,
11126    add it to the list and assign it the next available unique index number.
11127    In order to speed up searches, we remember the index of the filename
11128    was looked up last.  This handles the majority of all searches.  */
11129
11130 static unsigned
11131 lookup_filename (file_name)
11132      const char *file_name;
11133 {
11134   register unsigned i;
11135
11136   /* ??? Why isn't DECL_SOURCE_FILE left null instead.  */
11137   if (strcmp (file_name, "<internal>") == 0
11138       || strcmp (file_name, "<built-in>") == 0)
11139     return 0;
11140
11141   /* Check to see if the file name that was searched on the previous
11142      call matches this file name.  If so, return the index.  */
11143   if (file_table.last_lookup_index != 0)
11144     if (strcmp (file_name, file_table.table[file_table.last_lookup_index]) == 0)
11145       return file_table.last_lookup_index;
11146
11147   /* Didn't match the previous lookup, search the table */
11148   for (i = 1; i < file_table.in_use; ++i)
11149     if (strcmp (file_name, file_table.table[i]) == 0)
11150       {
11151         file_table.last_lookup_index = i;
11152         return i;
11153       }
11154
11155   /* Prepare to add a new table entry by making sure there is enough space in
11156      the table to do so.  If not, expand the current table.  */
11157   if (i == file_table.allocated)
11158     {
11159       file_table.allocated = i + FILE_TABLE_INCREMENT;
11160       file_table.table = (char **)
11161         xrealloc (file_table.table, file_table.allocated * sizeof (char *));
11162     }
11163
11164   /* Add the new entry to the end of the filename table.  */
11165   file_table.table[i] = xstrdup (file_name);
11166   file_table.in_use = i + 1;
11167   file_table.last_lookup_index = i;
11168
11169   if (DWARF2_ASM_LINE_DEBUG_INFO)
11170     fprintf (asm_out_file, "\t.file %u \"%s\"\n", i, file_name);
11171
11172   return i;
11173 }
11174
11175 static void
11176 init_file_table ()
11177 {
11178   /* Allocate the initial hunk of the file_table.  */
11179   file_table.table = (char **) xcalloc (FILE_TABLE_INCREMENT, sizeof (char *));
11180   file_table.allocated = FILE_TABLE_INCREMENT;
11181
11182   /* Skip the first entry - file numbers begin at 1.  */
11183   file_table.in_use = 1;
11184   file_table.last_lookup_index = 0;
11185 }
11186
11187 /* Output a label to mark the beginning of a source code line entry
11188    and record information relating to this source line, in
11189    'line_info_table' for later output of the .debug_line section.  */
11190
11191 void
11192 dwarf2out_line (filename, line)
11193      register const char *filename;
11194      register unsigned line;
11195 {
11196   if (debug_info_level >= DINFO_LEVEL_NORMAL)
11197     {
11198       function_section (current_function_decl);
11199
11200       if (DWARF2_ASM_LINE_DEBUG_INFO)
11201         {
11202           unsigned file_num = lookup_filename (filename);
11203
11204           /* Emit the .loc directive understood by GNU as.  */
11205           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
11206
11207           /* Indicate that line number info exists.  */
11208           ++line_info_table_in_use;
11209
11210           /* Indicate that multiple line number tables exist.  */
11211           if (DECL_SECTION_NAME (current_function_decl))
11212             ++separate_line_info_table_in_use;
11213         }
11214       else if (DECL_SECTION_NAME (current_function_decl))
11215         {
11216           register dw_separate_line_info_ref line_info;
11217           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, SEPARATE_LINE_CODE_LABEL,
11218                                      separate_line_info_table_in_use);
11219           if (flag_debug_asm)
11220             fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
11221                      filename, line);
11222
11223           /* expand the line info table if necessary */
11224           if (separate_line_info_table_in_use
11225               == separate_line_info_table_allocated)
11226             {
11227               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11228               separate_line_info_table
11229                 = (dw_separate_line_info_ref)
11230                   xrealloc (separate_line_info_table,
11231                             separate_line_info_table_allocated
11232                             * sizeof (dw_separate_line_info_entry));
11233             }
11234
11235           /* Add the new entry at the end of the line_info_table.  */
11236           line_info
11237             = &separate_line_info_table[separate_line_info_table_in_use++];
11238           line_info->dw_file_num = lookup_filename (filename);
11239           line_info->dw_line_num = line;
11240           line_info->function = current_funcdef_number;
11241         }
11242       else
11243         {
11244           register dw_line_info_ref line_info;
11245
11246           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, LINE_CODE_LABEL,
11247                                      line_info_table_in_use);
11248           if (flag_debug_asm)
11249             fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
11250                      filename, line);
11251
11252           /* Expand the line info table if necessary.  */
11253           if (line_info_table_in_use == line_info_table_allocated)
11254             {
11255               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11256               line_info_table
11257                 = (dw_line_info_ref)
11258                   xrealloc (line_info_table,
11259                             (line_info_table_allocated
11260                              * sizeof (dw_line_info_entry)));
11261             }
11262
11263           /* Add the new entry at the end of the line_info_table.  */
11264           line_info = &line_info_table[line_info_table_in_use++];
11265           line_info->dw_file_num = lookup_filename (filename);
11266           line_info->dw_line_num = line;
11267         }
11268     }
11269 }
11270
11271 /* Record the beginning of a new source file. */
11272
11273 void
11274 dwarf2out_start_source_file (lineno, filename)
11275      register unsigned int lineno ATTRIBUTE_UNUSED;
11276      register const char *filename ATTRIBUTE_UNUSED;
11277 {
11278   if (flag_eliminate_dwarf2_dups)
11279     {
11280       /* Record the beginning of the file for break_out_includes.  */
11281       dw_die_ref bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die);
11282       add_AT_string (bincl_die, DW_AT_name, filename);
11283     }
11284   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11285     {
11286       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
11287       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
11288       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d", lineno);
11289       dw2_asm_output_data_uleb128 (lookup_filename (filename), "Filename we just started");
11290     }
11291 }
11292
11293 /* Record the end of a source file.  */
11294
11295 void
11296 dwarf2out_end_source_file ()
11297 {
11298   if (flag_eliminate_dwarf2_dups)
11299     {
11300       /* Record the end of the file for break_out_includes.  */
11301       new_die (DW_TAG_GNU_EINCL, comp_unit_die);
11302     }
11303   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11304     {
11305       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
11306       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
11307     }
11308 }
11309
11310 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
11311    the tail part of the directive line, i.e. the part which is past the
11312    initial whitespace, #, whitespace, directive-name, whitespace part.  */
11313
11314 void
11315 dwarf2out_define (lineno, buffer)
11316      register unsigned lineno ATTRIBUTE_UNUSED;
11317      register const char *buffer ATTRIBUTE_UNUSED;
11318 {
11319   static int initialized = 0;
11320   if (!initialized)
11321     {
11322       dwarf2out_start_source_file (0, primary_filename);
11323       initialized = 1;
11324     }
11325   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11326     {
11327       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
11328       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
11329       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
11330       dw2_asm_output_nstring (buffer, -1, "The macro");
11331     }
11332 }
11333
11334 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
11335    the tail part of the directive line, i.e. the part which is past the
11336    initial whitespace, #, whitespace, directive-name, whitespace part.  */
11337
11338 void
11339 dwarf2out_undef (lineno, buffer)
11340      register unsigned lineno ATTRIBUTE_UNUSED;
11341      register const char *buffer ATTRIBUTE_UNUSED;
11342 {
11343   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11344     {
11345       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
11346       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
11347       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
11348       dw2_asm_output_nstring (buffer, -1, "The macro");
11349     }
11350 }
11351
11352 /* Set up for Dwarf output at the start of compilation.  */
11353
11354 void
11355 dwarf2out_init (asm_out_file, main_input_filename)
11356      register FILE *asm_out_file;
11357      register const char *main_input_filename;
11358 {
11359   init_file_table ();
11360
11361   /* Remember the name of the primary input file.  */
11362   primary_filename = main_input_filename;
11363
11364   /* Add it to the file table first, under the assumption that we'll
11365      be emitting line number data for it first, which avoids having
11366      to add an initial DW_LNS_set_file.  */
11367   lookup_filename (main_input_filename);
11368
11369   /* Allocate the initial hunk of the decl_die_table.  */
11370   decl_die_table
11371     = (dw_die_ref *) xcalloc (DECL_DIE_TABLE_INCREMENT, sizeof (dw_die_ref));
11372   decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
11373   decl_die_table_in_use = 0;
11374
11375   /* Allocate the initial hunk of the decl_scope_table.  */
11376   decl_scope_table
11377     = (tree *) xcalloc (DECL_SCOPE_TABLE_INCREMENT, sizeof (tree));
11378   decl_scope_table_allocated = DECL_SCOPE_TABLE_INCREMENT;
11379   decl_scope_depth = 0;
11380
11381   /* Allocate the initial hunk of the abbrev_die_table.  */
11382   abbrev_die_table
11383     = (dw_die_ref *) xcalloc (ABBREV_DIE_TABLE_INCREMENT,
11384                               sizeof (dw_die_ref));
11385   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
11386   /* Zero-th entry is allocated, but unused */
11387   abbrev_die_table_in_use = 1;
11388
11389   /* Allocate the initial hunk of the line_info_table.  */
11390   line_info_table
11391     = (dw_line_info_ref) xcalloc (LINE_INFO_TABLE_INCREMENT,
11392                                   sizeof (dw_line_info_entry));
11393   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
11394   /* Zero-th entry is allocated, but unused */
11395   line_info_table_in_use = 1;
11396
11397   /* Generate the initial DIE for the .debug section.  Note that the (string)
11398      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
11399      will (typically) be a relative pathname and that this pathname should be
11400      taken as being relative to the directory from which the compiler was
11401      invoked when the given (base) source file was compiled.  */
11402   comp_unit_die = gen_compile_unit_die (main_input_filename);
11403
11404   VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
11405   ggc_add_rtx_varray_root (&used_rtx_varray, 1);
11406
11407   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
11408   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
11409                                DEBUG_ABBREV_SECTION_LABEL, 0);
11410   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11411     ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
11412   else
11413     strcpy (text_section_label, stripattributes (TEXT_SECTION));
11414   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
11415                                DEBUG_INFO_SECTION_LABEL, 0);
11416   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
11417                                DEBUG_LINE_SECTION_LABEL, 0);
11418   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
11419   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LOC_SECTION);
11420   ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
11421   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_ABBREV_SECTION);
11422   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
11423   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11424     {
11425       ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
11426       ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
11427     }
11428   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_INFO_SECTION);
11429   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11430   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
11431   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
11432   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11433     {
11434       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
11435       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
11436                                    DEBUG_MACINFO_SECTION_LABEL, 0);
11437       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
11438     }
11439 }
11440
11441 /* Output stuff that dwarf requires at the end of every file,
11442    and generate the DWARF-2 debugging info.  */
11443
11444 void
11445 dwarf2out_finish ()
11446 {
11447   limbo_die_node *node, *next_node;
11448   dw_die_ref die;
11449
11450   /* Traverse the limbo die list, and add parent/child links.  The only
11451      dies without parents that should be here are concrete instances of
11452      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
11453      For concrete instances, we can get the parent die from the abstract
11454      instance.  */
11455   for (node = limbo_die_list; node; node = next_node)
11456     {
11457       next_node = node->next;
11458       die = node->die;
11459
11460       if (die->die_parent == NULL)
11461         {
11462           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
11463           if (origin)
11464             add_child_die (origin->die_parent, die);
11465           else if (die == comp_unit_die)
11466             ;
11467           else
11468             abort ();
11469         }
11470       free (node);
11471     }
11472   limbo_die_list = NULL;
11473
11474   /* Walk through the list of incomplete types again, trying once more to
11475      emit full debugging info for them.  */
11476   retry_incomplete_types ();
11477
11478   /* We need to reverse all the dies before break_out_includes, or
11479      we'll see the end of an include file before the beginning.  */
11480   reverse_all_dies (comp_unit_die);
11481
11482   /* Generate separate CUs for each of the include files we've seen.
11483      They will go into limbo_die_list.  */
11484   if (flag_eliminate_dwarf2_dups)
11485     break_out_includes (comp_unit_die);
11486
11487   /* Traverse the DIE's and add add sibling attributes to those DIE's
11488      that have children.  */
11489   add_sibling_attributes (comp_unit_die);
11490   for (node = limbo_die_list; node; node = node->next)
11491     add_sibling_attributes (node->die);
11492
11493   /* Output a terminator label for the .text section.  */
11494   ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
11495   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, TEXT_END_LABEL, 0);
11496
11497 #if 0
11498   /* Output a terminator label for the .data section.  */
11499   ASM_OUTPUT_SECTION (asm_out_file, DATA_SECTION);
11500   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, DATA_END_LABEL, 0);
11501
11502   /* Output a terminator label for the .bss section.  */
11503   ASM_OUTPUT_SECTION (asm_out_file, BSS_SECTION);
11504   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BSS_END_LABEL, 0);
11505 #endif
11506
11507   /* Output the source line correspondence table.  We must do this
11508      even if there is no line information.  Otherwise, on an empty
11509      translation unit, we will generate a present, but empty,
11510      .debug_info section.  IRIX 6.5 `nm' will then complain when
11511      examining the file.  */
11512   if (! DWARF2_ASM_LINE_DEBUG_INFO)
11513     {
11514       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
11515       output_line_info ();
11516     }
11517
11518   /* We can only use the low/high_pc attributes if all of the code was
11519      in .text.  */
11520   if (separate_line_info_table_in_use == 0)
11521     {
11522       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
11523       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
11524     }
11525
11526   if (debug_info_level >= DINFO_LEVEL_NORMAL)
11527     add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
11528                        debug_line_section_label);
11529
11530   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11531     add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
11532
11533   /* Output all of the compilation units.  We put the main one last so that
11534      the offsets are available to output_pubnames.  */
11535   for (node = limbo_die_list; node; node = node->next)
11536     output_comp_unit (node->die);
11537   output_comp_unit (comp_unit_die);
11538
11539   /* Output the abbreviation table.  */
11540   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_ABBREV_SECTION);
11541   output_abbrev_section ();
11542
11543   if (pubname_table_in_use)
11544     {
11545       /* Output public names table.  */
11546       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_PUBNAMES_SECTION);
11547       output_pubnames ();
11548     }
11549
11550   /* We only put functions in the arange table, so don't write it out if
11551      we don't have any.  */
11552   if (fde_table_in_use)
11553     {
11554       /* Output the address range information.  */
11555       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_ARANGES_SECTION);
11556       output_aranges ();
11557     }
11558   /* Output location list section if necessary */
11559   if (have_location_lists)
11560     {
11561       /* Output the location lists info. */
11562       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LOC_SECTION);
11563       output_location_lists (die);
11564       have_location_lists = 0;
11565     }
11566
11567   /* Have to end the primary source file. */
11568   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11569     { 
11570       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
11571       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
11572     }
11573   
11574 }
11575 #endif /* DWARF2_DEBUGGING_INFO */