OSDN Git Service

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