OSDN Git Service

* regclass.c (choose_hard_reg_mode): Add third argument.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003 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 GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "flags.h"
43 #include "real.h"
44 #include "rtl.h"
45 #include "hard-reg-set.h"
46 #include "regs.h"
47 #include "insn-config.h"
48 #include "reload.h"
49 #include "function.h"
50 #include "output.h"
51 #include "expr.h"
52 #include "libfuncs.h"
53 #include "except.h"
54 #include "dwarf2.h"
55 #include "dwarf2out.h"
56 #include "dwarf2asm.h"
57 #include "toplev.h"
58 #include "varray.h"
59 #include "ggc.h"
60 #include "md5.h"
61 #include "tm_p.h"
62 #include "diagnostic.h"
63 #include "debug.h"
64 #include "target.h"
65 #include "langhooks.h"
66 #include "hashtab.h"
67
68 #ifdef DWARF2_DEBUGGING_INFO
69 static void dwarf2out_source_line (unsigned int, const char *);
70 #endif
71
72 /* DWARF2 Abbreviation Glossary:
73    CFA = Canonical Frame Address
74            a fixed address on the stack which identifies a call frame.
75            We define it to be the value of SP just before the call insn.
76            The CFA register and offset, which may change during the course
77            of the function, are used to calculate its value at runtime.
78    CFI = Call Frame Instruction
79            an instruction for the DWARF2 abstract machine
80    CIE = Common Information Entry
81            information describing information common to one or more FDEs
82    DIE = Debugging Information Entry
83    FDE = Frame Description Entry
84            information describing the stack call frame, in particular,
85            how to restore registers
86
87    DW_CFA_... = DWARF2 CFA call frame instruction
88    DW_TAG_... = DWARF2 DIE tag */
89
90 /* Decide whether we want to emit frame unwind information for the current
91    translation unit.  */
92
93 int
94 dwarf2out_do_frame (void)
95 {
96   return (write_symbols == DWARF2_DEBUG
97           || write_symbols == VMS_AND_DWARF2_DEBUG
98 #ifdef DWARF2_FRAME_INFO
99           || DWARF2_FRAME_INFO
100 #endif
101 #ifdef DWARF2_UNWIND_INFO
102           || flag_unwind_tables
103           || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)
104 #endif
105           );
106 }
107
108 /* The size of the target's pointer type.  */
109 #ifndef PTR_SIZE
110 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
111 #endif
112
113 /* Default version of targetm.eh_frame_section.  Note this must appear
114    outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro
115    guards.  */
116
117 void
118 default_eh_frame_section (void)
119 {
120 #ifdef EH_FRAME_SECTION_NAME
121 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
122   int fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
123   int per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
124   int lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
125   int flags;
126
127   flags = (! flag_pic
128            || ((fde_encoding & 0x70) != DW_EH_PE_absptr
129                && (fde_encoding & 0x70) != DW_EH_PE_aligned
130                && (per_encoding & 0x70) != DW_EH_PE_absptr
131                && (per_encoding & 0x70) != DW_EH_PE_aligned
132                && (lsda_encoding & 0x70) != DW_EH_PE_absptr
133                && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
134           ? 0 : SECTION_WRITE;
135   named_section_flags (EH_FRAME_SECTION_NAME, flags);
136 #else
137   named_section_flags (EH_FRAME_SECTION_NAME, SECTION_WRITE);
138 #endif
139 #else
140   tree label = get_file_function_name ('F');
141
142   data_section ();
143   ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
144   (*targetm.asm_out.globalize_label) (asm_out_file, IDENTIFIER_POINTER (label));
145   ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
146 #endif
147 }
148
149 /* Array of RTXes referenced by the debugging information, which therefore
150    must be kept around forever.  */
151 static GTY(()) varray_type used_rtx_varray;
152
153 /* A pointer to the base of a list of incomplete types which might be
154    completed at some later time.  incomplete_types_list needs to be a VARRAY
155    because we want to tell the garbage collector about it.  */
156 static GTY(()) varray_type incomplete_types;
157
158 /* A pointer to the base of a table of references to declaration
159    scopes.  This table is a display which tracks the nesting
160    of declaration scopes at the current scope and containing
161    scopes.  This table is used to find the proper place to
162    define type declaration DIE's.  */
163 static GTY(()) varray_type decl_scope_table;
164
165 /* How to start an assembler comment.  */
166 #ifndef ASM_COMMENT_START
167 #define ASM_COMMENT_START ";#"
168 #endif
169
170 typedef struct dw_cfi_struct *dw_cfi_ref;
171 typedef struct dw_fde_struct *dw_fde_ref;
172 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
173
174 /* Call frames are described using a sequence of Call Frame
175    Information instructions.  The register number, offset
176    and address fields are provided as possible operands;
177    their use is selected by the opcode field.  */
178
179 enum dw_cfi_oprnd_type {
180   dw_cfi_oprnd_unused,
181   dw_cfi_oprnd_reg_num,
182   dw_cfi_oprnd_offset,
183   dw_cfi_oprnd_addr,
184   dw_cfi_oprnd_loc
185 };
186
187 typedef union dw_cfi_oprnd_struct GTY(())
188 {
189   unsigned long GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
190   long int GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
191   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
192   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
193 }
194 dw_cfi_oprnd;
195
196 typedef struct dw_cfi_struct GTY(())
197 {
198   dw_cfi_ref dw_cfi_next;
199   enum dwarf_call_frame_info dw_cfi_opc;
200   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
201     dw_cfi_oprnd1;
202   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
203     dw_cfi_oprnd2;
204 }
205 dw_cfi_node;
206
207 /* This is how we define the location of the CFA. We use to handle it
208    as REG + OFFSET all the time,  but now it can be more complex.
209    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
210    Instead of passing around REG and OFFSET, we pass a copy
211    of this structure.  */
212 typedef struct cfa_loc GTY(())
213 {
214   unsigned long reg;
215   long offset;
216   long base_offset;
217   int indirect;            /* 1 if CFA is accessed via a dereference.  */
218 } dw_cfa_location;
219
220 /* All call frame descriptions (FDE's) in the GCC generated DWARF
221    refer to a single Common Information Entry (CIE), defined at
222    the beginning of the .debug_frame section.  This use of a single
223    CIE obviates the need to keep track of multiple CIE's
224    in the DWARF generation routines below.  */
225
226 typedef struct dw_fde_struct GTY(())
227 {
228   const char *dw_fde_begin;
229   const char *dw_fde_current_label;
230   const char *dw_fde_end;
231   dw_cfi_ref dw_fde_cfi;
232   unsigned funcdef_number;
233   unsigned all_throwers_are_sibcalls : 1;
234   unsigned nothrow : 1;
235   unsigned uses_eh_lsda : 1;
236 }
237 dw_fde_node;
238
239 /* Maximum size (in bytes) of an artificially generated label.  */
240 #define MAX_ARTIFICIAL_LABEL_BYTES      30
241
242 /* The size of addresses as they appear in the Dwarf 2 data.
243    Some architectures use word addresses to refer to code locations,
244    but Dwarf 2 info always uses byte addresses.  On such machines,
245    Dwarf 2 addresses need to be larger than the architecture's
246    pointers.  */
247 #ifndef DWARF2_ADDR_SIZE
248 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
249 #endif
250
251 /* The size in bytes of a DWARF field indicating an offset or length
252    relative to a debug info section, specified to be 4 bytes in the
253    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
254    as PTR_SIZE.  */
255
256 #ifndef DWARF_OFFSET_SIZE
257 #define DWARF_OFFSET_SIZE 4
258 #endif
259
260 /* According to the (draft) DWARF 3 specification, the initial length
261    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
262    bytes are 0xffffffff, followed by the length stored in the next 8
263    bytes.
264
265    However, the SGI/MIPS ABI uses an initial length which is equal to
266    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
267
268 #ifndef DWARF_INITIAL_LENGTH_SIZE
269 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
270 #endif
271
272 #define DWARF_VERSION 2
273
274 /* Round SIZE up to the nearest BOUNDARY.  */
275 #define DWARF_ROUND(SIZE,BOUNDARY) \
276   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
277
278 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
279 #ifndef DWARF_CIE_DATA_ALIGNMENT
280 #ifdef STACK_GROWS_DOWNWARD
281 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
282 #else
283 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
284 #endif
285 #endif
286
287 /* A pointer to the base of a table that contains frame description
288    information for each routine.  */
289 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
290
291 /* Number of elements currently allocated for fde_table.  */
292 static GTY(()) unsigned fde_table_allocated;
293
294 /* Number of elements in fde_table currently in use.  */
295 static GTY(()) unsigned fde_table_in_use;
296
297 /* Size (in elements) of increments by which we may expand the
298    fde_table.  */
299 #define FDE_TABLE_INCREMENT 256
300
301 /* A list of call frame insns for the CIE.  */
302 static GTY(()) dw_cfi_ref cie_cfi_head;
303
304 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
305 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
306    attribute that accelerates the lookup of the FDE associated
307    with the subprogram.  This variable holds the table index of the FDE
308    associated with the current function (body) definition.  */
309 static unsigned current_funcdef_fde;
310 #endif
311
312 struct indirect_string_node GTY(())
313 {
314   const char *str;
315   unsigned int refcount;
316   unsigned int form;
317   char *label;
318 };
319
320 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
321
322 static GTY(()) int dw2_string_counter;
323 static GTY(()) unsigned long dwarf2out_cfi_label_num;
324
325 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
326
327 /* Forward declarations for functions defined in this file.  */
328
329 static char *stripattributes (const char *);
330 static const char *dwarf_cfi_name (unsigned);
331 static dw_cfi_ref new_cfi (void);
332 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
333 static void add_fde_cfi (const char *, dw_cfi_ref);
334 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
335 static void lookup_cfa (dw_cfa_location *);
336 static void reg_save (const char *, unsigned, unsigned, long);
337 static void initial_return_save (rtx);
338 static long stack_adjust_offset (rtx);
339 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
340 static void output_call_frame_info (int);
341 static void dwarf2out_stack_adjust (rtx);
342 static void queue_reg_save (const char *, rtx, long);
343 static void flush_queued_reg_saves (void);
344 static bool clobbers_queued_reg_save (rtx);
345 static void dwarf2out_frame_debug_expr (rtx, const char *);
346
347 /* Support for complex CFA locations.  */
348 static void output_cfa_loc (dw_cfi_ref);
349 static void get_cfa_from_loc_descr (dw_cfa_location *,
350                                     struct dw_loc_descr_struct *);
351 static struct dw_loc_descr_struct *build_cfa_loc
352  (dw_cfa_location *);
353 static void def_cfa_1 (const char *, dw_cfa_location *);
354
355 /* How to start an assembler comment.  */
356 #ifndef ASM_COMMENT_START
357 #define ASM_COMMENT_START ";#"
358 #endif
359
360 /* Data and reference forms for relocatable data.  */
361 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
362 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
363
364 #ifndef DEBUG_FRAME_SECTION
365 #define DEBUG_FRAME_SECTION     ".debug_frame"
366 #endif
367
368 #ifndef FUNC_BEGIN_LABEL
369 #define FUNC_BEGIN_LABEL        "LFB"
370 #endif
371
372 #ifndef FUNC_END_LABEL
373 #define FUNC_END_LABEL          "LFE"
374 #endif
375
376 #define FRAME_BEGIN_LABEL       "Lframe"
377 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
378 #define CIE_END_LABEL           "LECIE"
379 #define FDE_LABEL               "LSFDE"
380 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
381 #define FDE_END_LABEL           "LEFDE"
382 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
383 #define LINE_NUMBER_END_LABEL   "LELT"
384 #define LN_PROLOG_AS_LABEL      "LASLTP"
385 #define LN_PROLOG_END_LABEL     "LELTP"
386 #define DIE_LABEL_PREFIX        "DW"
387
388 /* The DWARF 2 CFA column which tracks the return address.  Normally this
389    is the column for PC, or the first column after all of the hard
390    registers.  */
391 #ifndef DWARF_FRAME_RETURN_COLUMN
392 #ifdef PC_REGNUM
393 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
394 #else
395 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
396 #endif
397 #endif
398
399 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
400    default, we just provide columns for all registers.  */
401 #ifndef DWARF_FRAME_REGNUM
402 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
403 #endif
404
405 /* The offset from the incoming value of %sp to the top of the stack frame
406    for the current function.  */
407 #ifndef INCOMING_FRAME_SP_OFFSET
408 #define INCOMING_FRAME_SP_OFFSET 0
409 #endif
410 \f
411 /* Hook used by __throw.  */
412
413 rtx
414 expand_builtin_dwarf_sp_column (void)
415 {
416   return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
417 }
418
419 /* Return a pointer to a copy of the section string name S with all
420    attributes stripped off, and an asterisk prepended (for assemble_name).  */
421
422 static inline char *
423 stripattributes (const char *s)
424 {
425   char *stripped = xmalloc (strlen (s) + 2);
426   char *p = stripped;
427
428   *p++ = '*';
429
430   while (*s && *s != ',')
431     *p++ = *s++;
432
433   *p = '\0';
434   return stripped;
435 }
436
437 /* Generate code to initialize the register size table.  */
438
439 void
440 expand_builtin_init_dwarf_reg_sizes (tree address)
441 {
442   int i;
443   enum machine_mode mode = TYPE_MODE (char_type_node);
444   rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
445   rtx mem = gen_rtx_MEM (BLKmode, addr);
446
447   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
448     if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
449       {
450         HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
451         enum machine_mode save_mode = reg_raw_mode[i];
452         HOST_WIDE_INT size;
453
454         if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
455           save_mode = choose_hard_reg_mode (i, 1, true);
456         size = GET_MODE_SIZE (save_mode);
457         if (offset < 0)
458           continue;
459
460         emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
461       }
462 }
463
464 /* Convert a DWARF call frame info. operation to its string name */
465
466 static const char *
467 dwarf_cfi_name (unsigned int cfi_opc)
468 {
469   switch (cfi_opc)
470     {
471     case DW_CFA_advance_loc:
472       return "DW_CFA_advance_loc";
473     case DW_CFA_offset:
474       return "DW_CFA_offset";
475     case DW_CFA_restore:
476       return "DW_CFA_restore";
477     case DW_CFA_nop:
478       return "DW_CFA_nop";
479     case DW_CFA_set_loc:
480       return "DW_CFA_set_loc";
481     case DW_CFA_advance_loc1:
482       return "DW_CFA_advance_loc1";
483     case DW_CFA_advance_loc2:
484       return "DW_CFA_advance_loc2";
485     case DW_CFA_advance_loc4:
486       return "DW_CFA_advance_loc4";
487     case DW_CFA_offset_extended:
488       return "DW_CFA_offset_extended";
489     case DW_CFA_restore_extended:
490       return "DW_CFA_restore_extended";
491     case DW_CFA_undefined:
492       return "DW_CFA_undefined";
493     case DW_CFA_same_value:
494       return "DW_CFA_same_value";
495     case DW_CFA_register:
496       return "DW_CFA_register";
497     case DW_CFA_remember_state:
498       return "DW_CFA_remember_state";
499     case DW_CFA_restore_state:
500       return "DW_CFA_restore_state";
501     case DW_CFA_def_cfa:
502       return "DW_CFA_def_cfa";
503     case DW_CFA_def_cfa_register:
504       return "DW_CFA_def_cfa_register";
505     case DW_CFA_def_cfa_offset:
506       return "DW_CFA_def_cfa_offset";
507
508     /* DWARF 3 */
509     case DW_CFA_def_cfa_expression:
510       return "DW_CFA_def_cfa_expression";
511     case DW_CFA_expression:
512       return "DW_CFA_expression";
513     case DW_CFA_offset_extended_sf:
514       return "DW_CFA_offset_extended_sf";
515     case DW_CFA_def_cfa_sf:
516       return "DW_CFA_def_cfa_sf";
517     case DW_CFA_def_cfa_offset_sf:
518       return "DW_CFA_def_cfa_offset_sf";
519
520     /* SGI/MIPS specific */
521     case DW_CFA_MIPS_advance_loc8:
522       return "DW_CFA_MIPS_advance_loc8";
523
524     /* GNU extensions */
525     case DW_CFA_GNU_window_save:
526       return "DW_CFA_GNU_window_save";
527     case DW_CFA_GNU_args_size:
528       return "DW_CFA_GNU_args_size";
529     case DW_CFA_GNU_negative_offset_extended:
530       return "DW_CFA_GNU_negative_offset_extended";
531
532     default:
533       return "DW_CFA_<unknown>";
534     }
535 }
536
537 /* Return a pointer to a newly allocated Call Frame Instruction.  */
538
539 static inline dw_cfi_ref
540 new_cfi (void)
541 {
542   dw_cfi_ref cfi = (dw_cfi_ref) ggc_alloc (sizeof (dw_cfi_node));
543
544   cfi->dw_cfi_next = NULL;
545   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
546   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
547
548   return cfi;
549 }
550
551 /* Add a Call Frame Instruction to list of instructions.  */
552
553 static inline void
554 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
555 {
556   dw_cfi_ref *p;
557
558   /* Find the end of the chain.  */
559   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
560     ;
561
562   *p = cfi;
563 }
564
565 /* Generate a new label for the CFI info to refer to.  */
566
567 char *
568 dwarf2out_cfi_label (void)
569 {
570   static char label[20];
571
572   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
573   ASM_OUTPUT_LABEL (asm_out_file, label);
574   return label;
575 }
576
577 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
578    or to the CIE if LABEL is NULL.  */
579
580 static void
581 add_fde_cfi (const char *label, dw_cfi_ref cfi)
582 {
583   if (label)
584     {
585       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
586
587       if (*label == 0)
588         label = dwarf2out_cfi_label ();
589
590       if (fde->dw_fde_current_label == NULL
591           || strcmp (label, fde->dw_fde_current_label) != 0)
592         {
593           dw_cfi_ref xcfi;
594
595           fde->dw_fde_current_label = label = xstrdup (label);
596
597           /* Set the location counter to the new label.  */
598           xcfi = new_cfi ();
599           xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
600           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
601           add_cfi (&fde->dw_fde_cfi, xcfi);
602         }
603
604       add_cfi (&fde->dw_fde_cfi, cfi);
605     }
606
607   else
608     add_cfi (&cie_cfi_head, cfi);
609 }
610
611 /* Subroutine of lookup_cfa.  */
612
613 static inline void
614 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
615 {
616   switch (cfi->dw_cfi_opc)
617     {
618     case DW_CFA_def_cfa_offset:
619       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
620       break;
621     case DW_CFA_def_cfa_register:
622       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
623       break;
624     case DW_CFA_def_cfa:
625       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
626       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
627       break;
628     case DW_CFA_def_cfa_expression:
629       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
630       break;
631     default:
632       break;
633     }
634 }
635
636 /* Find the previous value for the CFA.  */
637
638 static void
639 lookup_cfa (dw_cfa_location *loc)
640 {
641   dw_cfi_ref cfi;
642
643   loc->reg = (unsigned long) -1;
644   loc->offset = 0;
645   loc->indirect = 0;
646   loc->base_offset = 0;
647
648   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
649     lookup_cfa_1 (cfi, loc);
650
651   if (fde_table_in_use)
652     {
653       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
654       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
655         lookup_cfa_1 (cfi, loc);
656     }
657 }
658
659 /* The current rule for calculating the DWARF2 canonical frame address.  */
660 static dw_cfa_location cfa;
661
662 /* The register used for saving registers to the stack, and its offset
663    from the CFA.  */
664 static dw_cfa_location cfa_store;
665
666 /* The running total of the size of arguments pushed onto the stack.  */
667 static long args_size;
668
669 /* The last args_size we actually output.  */
670 static long old_args_size;
671
672 /* Entry point to update the canonical frame address (CFA).
673    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
674    calculated from REG+OFFSET.  */
675
676 void
677 dwarf2out_def_cfa (const char *label, unsigned int reg, long int offset)
678 {
679   dw_cfa_location loc;
680   loc.indirect = 0;
681   loc.base_offset = 0;
682   loc.reg = reg;
683   loc.offset = offset;
684   def_cfa_1 (label, &loc);
685 }
686
687 /* This routine does the actual work.  The CFA is now calculated from
688    the dw_cfa_location structure.  */
689
690 static void
691 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
692 {
693   dw_cfi_ref cfi;
694   dw_cfa_location old_cfa, loc;
695
696   cfa = *loc_p;
697   loc = *loc_p;
698
699   if (cfa_store.reg == loc.reg && loc.indirect == 0)
700     cfa_store.offset = loc.offset;
701
702   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
703   lookup_cfa (&old_cfa);
704
705   /* If nothing changed, no need to issue any call frame instructions.  */
706   if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
707       && loc.indirect == old_cfa.indirect
708       && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
709     return;
710
711   cfi = new_cfi ();
712
713   if (loc.reg == old_cfa.reg && !loc.indirect)
714     {
715       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
716          indicating the CFA register did not change but the offset
717          did.  */
718       cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
719       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
720     }
721
722 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
723   else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
724            && !loc.indirect)
725     {
726       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
727          indicating the CFA register has changed to <register> but the
728          offset has not changed.  */
729       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
730       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
731     }
732 #endif
733
734   else if (loc.indirect == 0)
735     {
736       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
737          indicating the CFA register has changed to <register> with
738          the specified offset.  */
739       cfi->dw_cfi_opc = DW_CFA_def_cfa;
740       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
741       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
742     }
743   else
744     {
745       /* Construct a DW_CFA_def_cfa_expression instruction to
746          calculate the CFA using a full location expression since no
747          register-offset pair is available.  */
748       struct dw_loc_descr_struct *loc_list;
749
750       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
751       loc_list = build_cfa_loc (&loc);
752       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
753     }
754
755   add_fde_cfi (label, cfi);
756 }
757
758 /* Add the CFI for saving a register.  REG is the CFA column number.
759    LABEL is passed to add_fde_cfi.
760    If SREG is -1, the register is saved at OFFSET from the CFA;
761    otherwise it is saved in SREG.  */
762
763 static void
764 reg_save (const char *label, unsigned int reg, unsigned int sreg, long int offset)
765 {
766   dw_cfi_ref cfi = new_cfi ();
767
768   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
769
770   /* The following comparison is correct. -1 is used to indicate that
771      the value isn't a register number.  */
772   if (sreg == (unsigned int) -1)
773     {
774       if (reg & ~0x3f)
775         /* The register number won't fit in 6 bits, so we have to use
776            the long form.  */
777         cfi->dw_cfi_opc = DW_CFA_offset_extended;
778       else
779         cfi->dw_cfi_opc = DW_CFA_offset;
780
781 #ifdef ENABLE_CHECKING
782       {
783         /* If we get an offset that is not a multiple of
784            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
785            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
786            description.  */
787         long check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
788
789         if (check_offset * DWARF_CIE_DATA_ALIGNMENT != offset)
790           abort ();
791       }
792 #endif
793       offset /= DWARF_CIE_DATA_ALIGNMENT;
794       if (offset < 0)
795         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
796
797       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
798     }
799   else if (sreg == reg)
800     /* We could emit a DW_CFA_same_value in this case, but don't bother.  */
801     return;
802   else
803     {
804       cfi->dw_cfi_opc = DW_CFA_register;
805       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
806     }
807
808   add_fde_cfi (label, cfi);
809 }
810
811 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
812    This CFI tells the unwinder that it needs to restore the window registers
813    from the previous frame's window save area.
814
815    ??? Perhaps we should note in the CIE where windows are saved (instead of
816    assuming 0(cfa)) and what registers are in the window.  */
817
818 void
819 dwarf2out_window_save (const char *label)
820 {
821   dw_cfi_ref cfi = new_cfi ();
822
823   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
824   add_fde_cfi (label, cfi);
825 }
826
827 /* Add a CFI to update the running total of the size of arguments
828    pushed onto the stack.  */
829
830 void
831 dwarf2out_args_size (const char *label, long int size)
832 {
833   dw_cfi_ref cfi;
834
835   if (size == old_args_size)
836     return;
837
838   old_args_size = size;
839
840   cfi = new_cfi ();
841   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
842   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
843   add_fde_cfi (label, cfi);
844 }
845
846 /* Entry point for saving a register to the stack.  REG is the GCC register
847    number.  LABEL and OFFSET are passed to reg_save.  */
848
849 void
850 dwarf2out_reg_save (const char *label, unsigned int reg, long int offset)
851 {
852   reg_save (label, DWARF_FRAME_REGNUM (reg), -1, offset);
853 }
854
855 /* Entry point for saving the return address in the stack.
856    LABEL and OFFSET are passed to reg_save.  */
857
858 void
859 dwarf2out_return_save (const char *label, long int offset)
860 {
861   reg_save (label, DWARF_FRAME_RETURN_COLUMN, -1, offset);
862 }
863
864 /* Entry point for saving the return address in a register.
865    LABEL and SREG are passed to reg_save.  */
866
867 void
868 dwarf2out_return_reg (const char *label, unsigned int sreg)
869 {
870   reg_save (label, DWARF_FRAME_RETURN_COLUMN, sreg, 0);
871 }
872
873 /* Record the initial position of the return address.  RTL is
874    INCOMING_RETURN_ADDR_RTX.  */
875
876 static void
877 initial_return_save (rtx rtl)
878 {
879   unsigned int reg = (unsigned int) -1;
880   HOST_WIDE_INT offset = 0;
881
882   switch (GET_CODE (rtl))
883     {
884     case REG:
885       /* RA is in a register.  */
886       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
887       break;
888
889     case MEM:
890       /* RA is on the stack.  */
891       rtl = XEXP (rtl, 0);
892       switch (GET_CODE (rtl))
893         {
894         case REG:
895           if (REGNO (rtl) != STACK_POINTER_REGNUM)
896             abort ();
897           offset = 0;
898           break;
899
900         case PLUS:
901           if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
902             abort ();
903           offset = INTVAL (XEXP (rtl, 1));
904           break;
905
906         case MINUS:
907           if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
908             abort ();
909           offset = -INTVAL (XEXP (rtl, 1));
910           break;
911
912         default:
913           abort ();
914         }
915
916       break;
917
918     case PLUS:
919       /* The return address is at some offset from any value we can
920          actually load.  For instance, on the SPARC it is in %i7+8. Just
921          ignore the offset for now; it doesn't matter for unwinding frames.  */
922       if (GET_CODE (XEXP (rtl, 1)) != CONST_INT)
923         abort ();
924       initial_return_save (XEXP (rtl, 0));
925       return;
926
927     default:
928       abort ();
929     }
930
931   reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
932 }
933
934 /* Given a SET, calculate the amount of stack adjustment it
935    contains.  */
936
937 static long
938 stack_adjust_offset (rtx pattern)
939 {
940   rtx src = SET_SRC (pattern);
941   rtx dest = SET_DEST (pattern);
942   HOST_WIDE_INT offset = 0;
943   enum rtx_code code;
944
945   if (dest == stack_pointer_rtx)
946     {
947       /* (set (reg sp) (plus (reg sp) (const_int))) */
948       code = GET_CODE (src);
949       if (! (code == PLUS || code == MINUS)
950           || XEXP (src, 0) != stack_pointer_rtx
951           || GET_CODE (XEXP (src, 1)) != CONST_INT)
952         return 0;
953
954       offset = INTVAL (XEXP (src, 1));
955       if (code == PLUS)
956         offset = -offset;
957     }
958   else if (GET_CODE (dest) == MEM)
959     {
960       /* (set (mem (pre_dec (reg sp))) (foo)) */
961       src = XEXP (dest, 0);
962       code = GET_CODE (src);
963
964       switch (code)
965         {
966         case PRE_MODIFY:
967         case POST_MODIFY:
968           if (XEXP (src, 0) == stack_pointer_rtx)
969             {
970               rtx val = XEXP (XEXP (src, 1), 1);
971               /* We handle only adjustments by constant amount.  */
972               if (GET_CODE (XEXP (src, 1)) != PLUS ||
973                   GET_CODE (val) != CONST_INT)
974                 abort ();
975               offset = -INTVAL (val);
976               break;
977             }
978           return 0;
979
980         case PRE_DEC:
981         case POST_DEC:
982           if (XEXP (src, 0) == stack_pointer_rtx)
983             {
984               offset = GET_MODE_SIZE (GET_MODE (dest));
985               break;
986             }
987           return 0;
988
989         case PRE_INC:
990         case POST_INC:
991           if (XEXP (src, 0) == stack_pointer_rtx)
992             {
993               offset = -GET_MODE_SIZE (GET_MODE (dest));
994               break;
995             }
996           return 0;
997
998         default:
999           return 0;
1000         }
1001     }
1002   else
1003     return 0;
1004
1005   return offset;
1006 }
1007
1008 /* Check INSN to see if it looks like a push or a stack adjustment, and
1009    make a note of it if it does.  EH uses this information to find out how
1010    much extra space it needs to pop off the stack.  */
1011
1012 static void
1013 dwarf2out_stack_adjust (rtx insn)
1014 {
1015   HOST_WIDE_INT offset;
1016   const char *label;
1017   int i;
1018
1019   if (!flag_asynchronous_unwind_tables && GET_CODE (insn) == CALL_INSN)
1020     {
1021       /* Extract the size of the args from the CALL rtx itself.  */
1022       insn = PATTERN (insn);
1023       if (GET_CODE (insn) == PARALLEL)
1024         insn = XVECEXP (insn, 0, 0);
1025       if (GET_CODE (insn) == SET)
1026         insn = SET_SRC (insn);
1027       if (GET_CODE (insn) != CALL)
1028         abort ();
1029
1030       dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1031       return;
1032     }
1033
1034   /* If only calls can throw, and we have a frame pointer,
1035      save up adjustments until we see the CALL_INSN.  */
1036   else if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1037     return;
1038
1039   if (GET_CODE (insn) == BARRIER)
1040     {
1041       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1042          the compiler will have already emitted a stack adjustment, but
1043          doesn't bother for calls to noreturn functions.  */
1044 #ifdef STACK_GROWS_DOWNWARD
1045       offset = -args_size;
1046 #else
1047       offset = args_size;
1048 #endif
1049     }
1050   else if (GET_CODE (PATTERN (insn)) == SET)
1051     offset = stack_adjust_offset (PATTERN (insn));
1052   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1053            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1054     {
1055       /* There may be stack adjustments inside compound insns.  Search
1056          for them.  */
1057       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1058         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1059           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1060     }
1061   else
1062     return;
1063
1064   if (offset == 0)
1065     return;
1066
1067   if (cfa.reg == STACK_POINTER_REGNUM)
1068     cfa.offset += offset;
1069
1070 #ifndef STACK_GROWS_DOWNWARD
1071   offset = -offset;
1072 #endif
1073
1074   args_size += offset;
1075   if (args_size < 0)
1076     args_size = 0;
1077
1078   label = dwarf2out_cfi_label ();
1079   def_cfa_1 (label, &cfa);
1080   dwarf2out_args_size (label, args_size);
1081 }
1082
1083 #endif
1084
1085 /* We delay emitting a register save until either (a) we reach the end
1086    of the prologue or (b) the register is clobbered.  This clusters
1087    register saves so that there are fewer pc advances.  */
1088
1089 struct queued_reg_save GTY(())
1090 {
1091   struct queued_reg_save *next;
1092   rtx reg;
1093   long cfa_offset;
1094 };
1095
1096 static GTY(()) struct queued_reg_save *queued_reg_saves;
1097
1098 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1099 static const char *last_reg_save_label;
1100
1101 static void
1102 queue_reg_save (const char *label, rtx reg, long int offset)
1103 {
1104   struct queued_reg_save *q = ggc_alloc (sizeof (*q));
1105
1106   q->next = queued_reg_saves;
1107   q->reg = reg;
1108   q->cfa_offset = offset;
1109   queued_reg_saves = q;
1110
1111   last_reg_save_label = label;
1112 }
1113
1114 static void
1115 flush_queued_reg_saves (void)
1116 {
1117   struct queued_reg_save *q, *next;
1118
1119   for (q = queued_reg_saves; q; q = next)
1120     {
1121       dwarf2out_reg_save (last_reg_save_label, REGNO (q->reg), q->cfa_offset);
1122       next = q->next;
1123     }
1124
1125   queued_reg_saves = NULL;
1126   last_reg_save_label = NULL;
1127 }
1128
1129 static bool
1130 clobbers_queued_reg_save (rtx insn)
1131 {
1132   struct queued_reg_save *q;
1133
1134   for (q = queued_reg_saves; q; q = q->next)
1135     if (modified_in_p (q->reg, insn))
1136       return true;
1137
1138   return false;
1139 }
1140
1141
1142 /* A temporary register holding an integral value used in adjusting SP
1143    or setting up the store_reg.  The "offset" field holds the integer
1144    value, not an offset.  */
1145 static dw_cfa_location cfa_temp;
1146
1147 /* Record call frame debugging information for an expression EXPR,
1148    which either sets SP or FP (adjusting how we calculate the frame
1149    address) or saves a register to the stack.  LABEL indicates the
1150    address of EXPR.
1151
1152    This function encodes a state machine mapping rtxes to actions on
1153    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1154    users need not read the source code.
1155
1156   The High-Level Picture
1157
1158   Changes in the register we use to calculate the CFA: Currently we
1159   assume that if you copy the CFA register into another register, we
1160   should take the other one as the new CFA register; this seems to
1161   work pretty well.  If it's wrong for some target, it's simple
1162   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1163
1164   Changes in the register we use for saving registers to the stack:
1165   This is usually SP, but not always.  Again, we deduce that if you
1166   copy SP into another register (and SP is not the CFA register),
1167   then the new register is the one we will be using for register
1168   saves.  This also seems to work.
1169
1170   Register saves: There's not much guesswork about this one; if
1171   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1172   register save, and the register used to calculate the destination
1173   had better be the one we think we're using for this purpose.
1174
1175   Except: If the register being saved is the CFA register, and the
1176   offset is nonzero, we are saving the CFA, so we assume we have to
1177   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1178   the intent is to save the value of SP from the previous frame.
1179
1180   Invariants / Summaries of Rules
1181
1182   cfa          current rule for calculating the CFA.  It usually
1183                consists of a register and an offset.
1184   cfa_store    register used by prologue code to save things to the stack
1185                cfa_store.offset is the offset from the value of
1186                cfa_store.reg to the actual CFA
1187   cfa_temp     register holding an integral value.  cfa_temp.offset
1188                stores the value, which will be used to adjust the
1189                stack pointer.  cfa_temp is also used like cfa_store,
1190                to track stores to the stack via fp or a temp reg.
1191
1192   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1193                with cfa.reg as the first operand changes the cfa.reg and its
1194                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1195                cfa_temp.offset.
1196
1197   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1198                expression yielding a constant.  This sets cfa_temp.reg
1199                and cfa_temp.offset.
1200
1201   Rule 5:      Create a new register cfa_store used to save items to the
1202                stack.
1203
1204   Rules 10-14: Save a register to the stack.  Define offset as the
1205                difference of the original location and cfa_store's
1206                location (or cfa_temp's location if cfa_temp is used).
1207
1208   The Rules
1209
1210   "{a,b}" indicates a choice of a xor b.
1211   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1212
1213   Rule 1:
1214   (set <reg1> <reg2>:cfa.reg)
1215   effects: cfa.reg = <reg1>
1216            cfa.offset unchanged
1217            cfa_temp.reg = <reg1>
1218            cfa_temp.offset = cfa.offset
1219
1220   Rule 2:
1221   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1222                               {<const_int>,<reg>:cfa_temp.reg}))
1223   effects: cfa.reg = sp if fp used
1224            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1225            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1226              if cfa_store.reg==sp
1227
1228   Rule 3:
1229   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1230   effects: cfa.reg = fp
1231            cfa_offset += +/- <const_int>
1232
1233   Rule 4:
1234   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1235   constraints: <reg1> != fp
1236                <reg1> != sp
1237   effects: cfa.reg = <reg1>
1238            cfa_temp.reg = <reg1>
1239            cfa_temp.offset = cfa.offset
1240
1241   Rule 5:
1242   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1243   constraints: <reg1> != fp
1244                <reg1> != sp
1245   effects: cfa_store.reg = <reg1>
1246            cfa_store.offset = cfa.offset - cfa_temp.offset
1247
1248   Rule 6:
1249   (set <reg> <const_int>)
1250   effects: cfa_temp.reg = <reg>
1251            cfa_temp.offset = <const_int>
1252
1253   Rule 7:
1254   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1255   effects: cfa_temp.reg = <reg1>
1256            cfa_temp.offset |= <const_int>
1257
1258   Rule 8:
1259   (set <reg> (high <exp>))
1260   effects: none
1261
1262   Rule 9:
1263   (set <reg> (lo_sum <exp> <const_int>))
1264   effects: cfa_temp.reg = <reg>
1265            cfa_temp.offset = <const_int>
1266
1267   Rule 10:
1268   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1269   effects: cfa_store.offset -= <const_int>
1270            cfa.offset = cfa_store.offset if cfa.reg == sp
1271            cfa.reg = sp
1272            cfa.base_offset = -cfa_store.offset
1273
1274   Rule 11:
1275   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1276   effects: cfa_store.offset += -/+ mode_size(mem)
1277            cfa.offset = cfa_store.offset if cfa.reg == sp
1278            cfa.reg = sp
1279            cfa.base_offset = -cfa_store.offset
1280
1281   Rule 12:
1282   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1283
1284        <reg2>)
1285   effects: cfa.reg = <reg1>
1286            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1287
1288   Rule 13:
1289   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1290   effects: cfa.reg = <reg1>
1291            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1292
1293   Rule 14:
1294   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1295   effects: cfa.reg = <reg1>
1296            cfa.base_offset = -cfa_temp.offset
1297            cfa_temp.offset -= mode_size(mem)  */
1298
1299 static void
1300 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1301 {
1302   rtx src, dest;
1303   HOST_WIDE_INT offset;
1304
1305   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1306      the PARALLEL independently. The first element is always processed if
1307      it is a SET. This is for backward compatibility.   Other elements
1308      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1309      flag is set in them.  */
1310   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1311     {
1312       int par_index;
1313       int limit = XVECLEN (expr, 0);
1314
1315       for (par_index = 0; par_index < limit; par_index++)
1316         if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1317             && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1318                 || par_index == 0))
1319           dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1320
1321       return;
1322     }
1323
1324   if (GET_CODE (expr) != SET)
1325     abort ();
1326
1327   src = SET_SRC (expr);
1328   dest = SET_DEST (expr);
1329
1330   switch (GET_CODE (dest))
1331     {
1332     case REG:
1333       /* Rule 1 */
1334       /* Update the CFA rule wrt SP or FP.  Make sure src is
1335          relative to the current CFA register.  */
1336       switch (GET_CODE (src))
1337         {
1338           /* Setting FP from SP.  */
1339         case REG:
1340           if (cfa.reg == (unsigned) REGNO (src))
1341             /* OK.  */
1342             ;
1343           else
1344             abort ();
1345
1346           /* We used to require that dest be either SP or FP, but the
1347              ARM copies SP to a temporary register, and from there to
1348              FP.  So we just rely on the backends to only set
1349              RTX_FRAME_RELATED_P on appropriate insns.  */
1350           cfa.reg = REGNO (dest);
1351           cfa_temp.reg = cfa.reg;
1352           cfa_temp.offset = cfa.offset;
1353           break;
1354
1355         case PLUS:
1356         case MINUS:
1357         case LO_SUM:
1358           if (dest == stack_pointer_rtx)
1359             {
1360               /* Rule 2 */
1361               /* Adjusting SP.  */
1362               switch (GET_CODE (XEXP (src, 1)))
1363                 {
1364                 case CONST_INT:
1365                   offset = INTVAL (XEXP (src, 1));
1366                   break;
1367                 case REG:
1368                   if ((unsigned) REGNO (XEXP (src, 1)) != cfa_temp.reg)
1369                     abort ();
1370                   offset = cfa_temp.offset;
1371                   break;
1372                 default:
1373                   abort ();
1374                 }
1375
1376               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1377                 {
1378                   /* Restoring SP from FP in the epilogue.  */
1379                   if (cfa.reg != (unsigned) HARD_FRAME_POINTER_REGNUM)
1380                     abort ();
1381                   cfa.reg = STACK_POINTER_REGNUM;
1382                 }
1383               else if (GET_CODE (src) == LO_SUM)
1384                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1385                 ;
1386               else if (XEXP (src, 0) != stack_pointer_rtx)
1387                 abort ();
1388
1389               if (GET_CODE (src) != MINUS)
1390                 offset = -offset;
1391               if (cfa.reg == STACK_POINTER_REGNUM)
1392                 cfa.offset += offset;
1393               if (cfa_store.reg == STACK_POINTER_REGNUM)
1394                 cfa_store.offset += offset;
1395             }
1396           else if (dest == hard_frame_pointer_rtx)
1397             {
1398               /* Rule 3 */
1399               /* Either setting the FP from an offset of the SP,
1400                  or adjusting the FP */
1401               if (! frame_pointer_needed)
1402                 abort ();
1403
1404               if (GET_CODE (XEXP (src, 0)) == REG
1405                   && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1406                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1407                 {
1408                   offset = INTVAL (XEXP (src, 1));
1409                   if (GET_CODE (src) != MINUS)
1410                     offset = -offset;
1411                   cfa.offset += offset;
1412                   cfa.reg = HARD_FRAME_POINTER_REGNUM;
1413                 }
1414               else
1415                 abort ();
1416             }
1417           else
1418             {
1419               if (GET_CODE (src) == MINUS)
1420                 abort ();
1421
1422               /* Rule 4 */
1423               if (GET_CODE (XEXP (src, 0)) == REG
1424                   && REGNO (XEXP (src, 0)) == cfa.reg
1425                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1426                 {
1427                   /* Setting a temporary CFA register that will be copied
1428                      into the FP later on.  */
1429                   offset = - INTVAL (XEXP (src, 1));
1430                   cfa.offset += offset;
1431                   cfa.reg = REGNO (dest);
1432                   /* Or used to save regs to the stack.  */
1433                   cfa_temp.reg = cfa.reg;
1434                   cfa_temp.offset = cfa.offset;
1435                 }
1436
1437               /* Rule 5 */
1438               else if (GET_CODE (XEXP (src, 0)) == REG
1439                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1440                        && XEXP (src, 1) == stack_pointer_rtx)
1441                 {
1442                   /* Setting a scratch register that we will use instead
1443                      of SP for saving registers to the stack.  */
1444                   if (cfa.reg != STACK_POINTER_REGNUM)
1445                     abort ();
1446                   cfa_store.reg = REGNO (dest);
1447                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1448                 }
1449
1450               /* Rule 9 */
1451               else if (GET_CODE (src) == LO_SUM
1452                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1453                 {
1454                   cfa_temp.reg = REGNO (dest);
1455                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1456                 }
1457               else
1458                 abort ();
1459             }
1460           break;
1461
1462           /* Rule 6 */
1463         case CONST_INT:
1464           cfa_temp.reg = REGNO (dest);
1465           cfa_temp.offset = INTVAL (src);
1466           break;
1467
1468           /* Rule 7 */
1469         case IOR:
1470           if (GET_CODE (XEXP (src, 0)) != REG
1471               || (unsigned) REGNO (XEXP (src, 0)) != cfa_temp.reg
1472               || GET_CODE (XEXP (src, 1)) != CONST_INT)
1473             abort ();
1474
1475           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1476             cfa_temp.reg = REGNO (dest);
1477           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1478           break;
1479
1480           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1481              which will fill in all of the bits.  */
1482           /* Rule 8 */
1483         case HIGH:
1484           break;
1485
1486         default:
1487           abort ();
1488         }
1489
1490       def_cfa_1 (label, &cfa);
1491       break;
1492
1493     case MEM:
1494       if (GET_CODE (src) != REG)
1495         abort ();
1496
1497       /* Saving a register to the stack.  Make sure dest is relative to the
1498          CFA register.  */
1499       switch (GET_CODE (XEXP (dest, 0)))
1500         {
1501           /* Rule 10 */
1502           /* With a push.  */
1503         case PRE_MODIFY:
1504           /* We can't handle variable size modifications.  */
1505           if (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1)) != CONST_INT)
1506             abort ();
1507           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1508
1509           if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1510               || cfa_store.reg != STACK_POINTER_REGNUM)
1511             abort ();
1512
1513           cfa_store.offset += offset;
1514           if (cfa.reg == STACK_POINTER_REGNUM)
1515             cfa.offset = cfa_store.offset;
1516
1517           offset = -cfa_store.offset;
1518           break;
1519
1520           /* Rule 11 */
1521         case PRE_INC:
1522         case PRE_DEC:
1523           offset = GET_MODE_SIZE (GET_MODE (dest));
1524           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1525             offset = -offset;
1526
1527           if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1528               || cfa_store.reg != STACK_POINTER_REGNUM)
1529             abort ();
1530
1531           cfa_store.offset += offset;
1532           if (cfa.reg == STACK_POINTER_REGNUM)
1533             cfa.offset = cfa_store.offset;
1534
1535           offset = -cfa_store.offset;
1536           break;
1537
1538           /* Rule 12 */
1539           /* With an offset.  */
1540         case PLUS:
1541         case MINUS:
1542         case LO_SUM:
1543           if (GET_CODE (XEXP (XEXP (dest, 0), 1)) != CONST_INT)
1544             abort ();
1545           offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1546           if (GET_CODE (XEXP (dest, 0)) == MINUS)
1547             offset = -offset;
1548
1549           if (cfa_store.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1550             offset -= cfa_store.offset;
1551           else if (cfa_temp.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1552             offset -= cfa_temp.offset;
1553           else
1554             abort ();
1555           break;
1556
1557           /* Rule 13 */
1558           /* Without an offset.  */
1559         case REG:
1560           if (cfa_store.reg == (unsigned) REGNO (XEXP (dest, 0)))
1561             offset = -cfa_store.offset;
1562           else if (cfa_temp.reg == (unsigned) REGNO (XEXP (dest, 0)))
1563             offset = -cfa_temp.offset;
1564           else
1565             abort ();
1566           break;
1567
1568           /* Rule 14 */
1569         case POST_INC:
1570           if (cfa_temp.reg != (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1571             abort ();
1572           offset = -cfa_temp.offset;
1573           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1574           break;
1575
1576         default:
1577           abort ();
1578         }
1579
1580       if (REGNO (src) != STACK_POINTER_REGNUM
1581           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1582           && (unsigned) REGNO (src) == cfa.reg)
1583         {
1584           /* We're storing the current CFA reg into the stack.  */
1585
1586           if (cfa.offset == 0)
1587             {
1588               /* If the source register is exactly the CFA, assume
1589                  we're saving SP like any other register; this happens
1590                  on the ARM.  */
1591               def_cfa_1 (label, &cfa);
1592               queue_reg_save (label, stack_pointer_rtx, offset);
1593               break;
1594             }
1595           else
1596             {
1597               /* Otherwise, we'll need to look in the stack to
1598                  calculate the CFA.  */
1599               rtx x = XEXP (dest, 0);
1600
1601               if (GET_CODE (x) != REG)
1602                 x = XEXP (x, 0);
1603               if (GET_CODE (x) != REG)
1604                 abort ();
1605
1606               cfa.reg = REGNO (x);
1607               cfa.base_offset = offset;
1608               cfa.indirect = 1;
1609               def_cfa_1 (label, &cfa);
1610               break;
1611             }
1612         }
1613
1614       def_cfa_1 (label, &cfa);
1615       queue_reg_save (label, src, offset);
1616       break;
1617
1618     default:
1619       abort ();
1620     }
1621 }
1622
1623 /* Record call frame debugging information for INSN, which either
1624    sets SP or FP (adjusting how we calculate the frame address) or saves a
1625    register to the stack.  If INSN is NULL_RTX, initialize our state.  */
1626
1627 void
1628 dwarf2out_frame_debug (rtx insn)
1629 {
1630   const char *label;
1631   rtx src;
1632
1633   if (insn == NULL_RTX)
1634     {
1635       /* Flush any queued register saves.  */
1636       flush_queued_reg_saves ();
1637
1638       /* Set up state for generating call frame debug info.  */
1639       lookup_cfa (&cfa);
1640       if (cfa.reg != (unsigned long) DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM))
1641         abort ();
1642
1643       cfa.reg = STACK_POINTER_REGNUM;
1644       cfa_store = cfa;
1645       cfa_temp.reg = -1;
1646       cfa_temp.offset = 0;
1647       return;
1648     }
1649
1650   if (GET_CODE (insn) != INSN || clobbers_queued_reg_save (insn))
1651     flush_queued_reg_saves ();
1652
1653   if (! RTX_FRAME_RELATED_P (insn))
1654     {
1655       if (!ACCUMULATE_OUTGOING_ARGS)
1656         dwarf2out_stack_adjust (insn);
1657
1658       return;
1659     }
1660
1661   label = dwarf2out_cfi_label ();
1662   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1663   if (src)
1664     insn = XEXP (src, 0);
1665   else
1666     insn = PATTERN (insn);
1667
1668   dwarf2out_frame_debug_expr (insn, label);
1669 }
1670
1671 #endif
1672
1673 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1674 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1675  (enum dwarf_call_frame_info cfi);
1676
1677 static enum dw_cfi_oprnd_type
1678 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1679 {
1680   switch (cfi)
1681     {
1682     case DW_CFA_nop:
1683     case DW_CFA_GNU_window_save:
1684       return dw_cfi_oprnd_unused;
1685
1686     case DW_CFA_set_loc:
1687     case DW_CFA_advance_loc1:
1688     case DW_CFA_advance_loc2:
1689     case DW_CFA_advance_loc4:
1690     case DW_CFA_MIPS_advance_loc8:
1691       return dw_cfi_oprnd_addr;
1692
1693     case DW_CFA_offset:
1694     case DW_CFA_offset_extended:
1695     case DW_CFA_def_cfa:
1696     case DW_CFA_offset_extended_sf:
1697     case DW_CFA_def_cfa_sf:
1698     case DW_CFA_restore_extended:
1699     case DW_CFA_undefined:
1700     case DW_CFA_same_value:
1701     case DW_CFA_def_cfa_register:
1702     case DW_CFA_register:
1703       return dw_cfi_oprnd_reg_num;
1704
1705     case DW_CFA_def_cfa_offset:
1706     case DW_CFA_GNU_args_size:
1707     case DW_CFA_def_cfa_offset_sf:
1708       return dw_cfi_oprnd_offset;
1709
1710     case DW_CFA_def_cfa_expression:
1711     case DW_CFA_expression:
1712       return dw_cfi_oprnd_loc;
1713
1714     default:
1715       abort ();
1716     }
1717 }
1718
1719 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1720 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1721  (enum dwarf_call_frame_info cfi);
1722
1723 static enum dw_cfi_oprnd_type
1724 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1725 {
1726   switch (cfi)
1727     {
1728     case DW_CFA_def_cfa:
1729     case DW_CFA_def_cfa_sf:
1730     case DW_CFA_offset:
1731     case DW_CFA_offset_extended_sf:
1732     case DW_CFA_offset_extended:
1733       return dw_cfi_oprnd_offset;
1734
1735     case DW_CFA_register:
1736       return dw_cfi_oprnd_reg_num;
1737
1738     default:
1739       return dw_cfi_oprnd_unused;
1740     }
1741 }
1742
1743 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1744
1745 /* Output a Call Frame Information opcode and its operand(s).  */
1746
1747 static void
1748 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
1749 {
1750   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1751     dw2_asm_output_data (1, (cfi->dw_cfi_opc
1752                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1753                          "DW_CFA_advance_loc 0x%lx",
1754                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
1755   else if (cfi->dw_cfi_opc == DW_CFA_offset)
1756     {
1757       dw2_asm_output_data (1, (cfi->dw_cfi_opc
1758                                | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f)),
1759                            "DW_CFA_offset, column 0x%lx",
1760                            cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1761       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1762     }
1763   else if (cfi->dw_cfi_opc == DW_CFA_restore)
1764     dw2_asm_output_data (1, (cfi->dw_cfi_opc
1765                              | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f)),
1766                          "DW_CFA_restore, column 0x%lx",
1767                          cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1768   else
1769     {
1770       dw2_asm_output_data (1, cfi->dw_cfi_opc,
1771                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1772
1773       switch (cfi->dw_cfi_opc)
1774         {
1775         case DW_CFA_set_loc:
1776           if (for_eh)
1777             dw2_asm_output_encoded_addr_rtx (
1778                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1779                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1780                 NULL);
1781           else
1782             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1783                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1784           break;
1785
1786         case DW_CFA_advance_loc1:
1787           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1788                                 fde->dw_fde_current_label, NULL);
1789           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1790           break;
1791
1792         case DW_CFA_advance_loc2:
1793           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1794                                 fde->dw_fde_current_label, NULL);
1795           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1796           break;
1797
1798         case DW_CFA_advance_loc4:
1799           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1800                                 fde->dw_fde_current_label, NULL);
1801           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1802           break;
1803
1804         case DW_CFA_MIPS_advance_loc8:
1805           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1806                                 fde->dw_fde_current_label, NULL);
1807           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1808           break;
1809
1810         case DW_CFA_offset_extended:
1811         case DW_CFA_def_cfa:
1812           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1813                                        NULL);
1814           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1815           break;
1816
1817         case DW_CFA_offset_extended_sf:
1818         case DW_CFA_def_cfa_sf:
1819           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1820                                        NULL);
1821           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1822           break;
1823
1824         case DW_CFA_restore_extended:
1825         case DW_CFA_undefined:
1826         case DW_CFA_same_value:
1827         case DW_CFA_def_cfa_register:
1828           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1829                                        NULL);
1830           break;
1831
1832         case DW_CFA_register:
1833           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1834                                        NULL);
1835           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_reg_num,
1836                                        NULL);
1837           break;
1838
1839         case DW_CFA_def_cfa_offset:
1840         case DW_CFA_GNU_args_size:
1841           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1842           break;
1843
1844         case DW_CFA_def_cfa_offset_sf:
1845           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1846           break;
1847
1848         case DW_CFA_GNU_window_save:
1849           break;
1850
1851         case DW_CFA_def_cfa_expression:
1852         case DW_CFA_expression:
1853           output_cfa_loc (cfi);
1854           break;
1855
1856         case DW_CFA_GNU_negative_offset_extended:
1857           /* Obsoleted by DW_CFA_offset_extended_sf.  */
1858           abort ();
1859
1860         default:
1861           break;
1862         }
1863     }
1864 }
1865
1866 /* Output the call frame information used to used to record information
1867    that relates to calculating the frame pointer, and records the
1868    location of saved registers.  */
1869
1870 static void
1871 output_call_frame_info (int for_eh)
1872 {
1873   unsigned int i;
1874   dw_fde_ref fde;
1875   dw_cfi_ref cfi;
1876   char l1[20], l2[20], section_start_label[20];
1877   bool any_lsda_needed = false;
1878   char augmentation[6];
1879   int augmentation_size;
1880   int fde_encoding = DW_EH_PE_absptr;
1881   int per_encoding = DW_EH_PE_absptr;
1882   int lsda_encoding = DW_EH_PE_absptr;
1883
1884   /* Don't emit a CIE if there won't be any FDEs.  */
1885   if (fde_table_in_use == 0)
1886     return;
1887
1888   /* If we don't have any functions we'll want to unwind out of, don't
1889      emit any EH unwind information.  Note that if exceptions aren't
1890      enabled, we won't have collected nothrow information, and if we
1891      asked for asynchronous tables, we always want this info.  */
1892   if (for_eh)
1893     {
1894       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
1895
1896       for (i = 0; i < fde_table_in_use; i++)
1897         if (fde_table[i].uses_eh_lsda)
1898           any_eh_needed = any_lsda_needed = true;
1899         else if (! fde_table[i].nothrow
1900                  && ! fde_table[i].all_throwers_are_sibcalls)
1901           any_eh_needed = true;
1902
1903       if (! any_eh_needed)
1904         return;
1905     }
1906
1907   /* We're going to be generating comments, so turn on app.  */
1908   if (flag_debug_asm)
1909     app_enable ();
1910
1911   if (for_eh)
1912     (*targetm.asm_out.eh_frame_section) ();
1913   else
1914     named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
1915
1916   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
1917   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
1918
1919   /* Output the CIE.  */
1920   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
1921   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
1922   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1923                         "Length of Common Information Entry");
1924   ASM_OUTPUT_LABEL (asm_out_file, l1);
1925
1926   /* Now that the CIE pointer is PC-relative for EH,
1927      use 0 to identify the CIE.  */
1928   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
1929                        (for_eh ? 0 : DW_CIE_ID),
1930                        "CIE Identifier Tag");
1931
1932   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
1933
1934   augmentation[0] = 0;
1935   augmentation_size = 0;
1936   if (for_eh)
1937     {
1938       char *p;
1939
1940       /* Augmentation:
1941          z      Indicates that a uleb128 is present to size the
1942                 augmentation section.
1943          L      Indicates the encoding (and thus presence) of
1944                 an LSDA pointer in the FDE augmentation.
1945          R      Indicates a non-default pointer encoding for
1946                 FDE code pointers.
1947          P      Indicates the presence of an encoding + language
1948                 personality routine in the CIE augmentation.  */
1949
1950       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
1951       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
1952       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1953
1954       p = augmentation + 1;
1955       if (eh_personality_libfunc)
1956         {
1957           *p++ = 'P';
1958           augmentation_size += 1 + size_of_encoded_value (per_encoding);
1959         }
1960       if (any_lsda_needed)
1961         {
1962           *p++ = 'L';
1963           augmentation_size += 1;
1964         }
1965       if (fde_encoding != DW_EH_PE_absptr)
1966         {
1967           *p++ = 'R';
1968           augmentation_size += 1;
1969         }
1970       if (p > augmentation + 1)
1971         {
1972           augmentation[0] = 'z';
1973           *p = '\0';
1974         }
1975
1976       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
1977       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
1978         {
1979           int offset = (  4             /* Length */
1980                         + 4             /* CIE Id */
1981                         + 1             /* CIE version */
1982                         + strlen (augmentation) + 1     /* Augmentation */
1983                         + size_of_uleb128 (1)           /* Code alignment */
1984                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
1985                         + 1             /* RA column */
1986                         + 1             /* Augmentation size */
1987                         + 1             /* Personality encoding */ );
1988           int pad = -offset & (PTR_SIZE - 1);
1989
1990           augmentation_size += pad;
1991
1992           /* Augmentations should be small, so there's scarce need to
1993              iterate for a solution.  Die if we exceed one uleb128 byte.  */
1994           if (size_of_uleb128 (augmentation_size) != 1)
1995             abort ();
1996         }
1997     }
1998
1999   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2000   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2001   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2002                                "CIE Data Alignment Factor");
2003   dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2004
2005   if (augmentation[0])
2006     {
2007       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2008       if (eh_personality_libfunc)
2009         {
2010           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2011                                eh_data_format_name (per_encoding));
2012           dw2_asm_output_encoded_addr_rtx (per_encoding,
2013                                            eh_personality_libfunc, NULL);
2014         }
2015
2016       if (any_lsda_needed)
2017         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2018                              eh_data_format_name (lsda_encoding));
2019
2020       if (fde_encoding != DW_EH_PE_absptr)
2021         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2022                              eh_data_format_name (fde_encoding));
2023     }
2024
2025   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2026     output_cfi (cfi, NULL, for_eh);
2027
2028   /* Pad the CIE out to an address sized boundary.  */
2029   ASM_OUTPUT_ALIGN (asm_out_file,
2030                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2031   ASM_OUTPUT_LABEL (asm_out_file, l2);
2032
2033   /* Loop through all of the FDE's.  */
2034   for (i = 0; i < fde_table_in_use; i++)
2035     {
2036       fde = &fde_table[i];
2037
2038       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2039       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2040           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2041           && !fde->uses_eh_lsda)
2042         continue;
2043
2044       (*targetm.asm_out.internal_label) (asm_out_file, FDE_LABEL, for_eh + i * 2);
2045       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2046       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2047       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2048                             "FDE Length");
2049       ASM_OUTPUT_LABEL (asm_out_file, l1);
2050
2051       if (for_eh)
2052         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2053       else
2054         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2055                                "FDE CIE offset");
2056
2057       if (for_eh)
2058         {
2059           dw2_asm_output_encoded_addr_rtx (fde_encoding,
2060                    gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin),
2061                    "FDE initial location");
2062           dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2063                                 fde->dw_fde_end, fde->dw_fde_begin,
2064                                 "FDE address range");
2065         }
2066       else
2067         {
2068           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2069                                "FDE initial location");
2070           dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2071                                 fde->dw_fde_end, fde->dw_fde_begin,
2072                                 "FDE address range");
2073         }
2074
2075       if (augmentation[0])
2076         {
2077           if (any_lsda_needed)
2078             {
2079               int size = size_of_encoded_value (lsda_encoding);
2080
2081               if (lsda_encoding == DW_EH_PE_aligned)
2082                 {
2083                   int offset = (  4             /* Length */
2084                                 + 4             /* CIE offset */
2085                                 + 2 * size_of_encoded_value (fde_encoding)
2086                                 + 1             /* Augmentation size */ );
2087                   int pad = -offset & (PTR_SIZE - 1);
2088
2089                   size += pad;
2090                   if (size_of_uleb128 (size) != 1)
2091                     abort ();
2092                 }
2093
2094               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2095
2096               if (fde->uses_eh_lsda)
2097                 {
2098                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2099                                                fde->funcdef_number);
2100                   dw2_asm_output_encoded_addr_rtx (
2101                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2102                         "Language Specific Data Area");
2103                 }
2104               else
2105                 {
2106                   if (lsda_encoding == DW_EH_PE_aligned)
2107                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2108                   dw2_asm_output_data
2109                     (size_of_encoded_value (lsda_encoding), 0,
2110                      "Language Specific Data Area (none)");
2111                 }
2112             }
2113           else
2114             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2115         }
2116
2117       /* Loop through the Call Frame Instructions associated with
2118          this FDE.  */
2119       fde->dw_fde_current_label = fde->dw_fde_begin;
2120       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2121         output_cfi (cfi, fde, for_eh);
2122
2123       /* Pad the FDE out to an address sized boundary.  */
2124       ASM_OUTPUT_ALIGN (asm_out_file,
2125                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2126       ASM_OUTPUT_LABEL (asm_out_file, l2);
2127     }
2128
2129   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2130     dw2_asm_output_data (4, 0, "End of Table");
2131 #ifdef MIPS_DEBUGGING_INFO
2132   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2133      get a value of 0.  Putting .align 0 after the label fixes it.  */
2134   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2135 #endif
2136
2137   /* Turn off app to make assembly quicker.  */
2138   if (flag_debug_asm)
2139     app_disable ();
2140 }
2141
2142 /* Output a marker (i.e. a label) for the beginning of a function, before
2143    the prologue.  */
2144
2145 void
2146 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2147                           const char *file ATTRIBUTE_UNUSED)
2148 {
2149   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2150   dw_fde_ref fde;
2151
2152   current_function_func_begin_label = 0;
2153
2154 #ifdef IA64_UNWIND_INFO
2155   /* ??? current_function_func_begin_label is also used by except.c
2156      for call-site information.  We must emit this label if it might
2157      be used.  */
2158   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2159       && ! dwarf2out_do_frame ())
2160     return;
2161 #else
2162   if (! dwarf2out_do_frame ())
2163     return;
2164 #endif
2165
2166   function_section (current_function_decl);
2167   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2168                                current_function_funcdef_no);
2169   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2170                           current_function_funcdef_no);
2171   current_function_func_begin_label = get_identifier (label);
2172
2173 #ifdef IA64_UNWIND_INFO
2174   /* We can elide the fde allocation if we're not emitting debug info.  */
2175   if (! dwarf2out_do_frame ())
2176     return;
2177 #endif
2178
2179   /* Expand the fde table if necessary.  */
2180   if (fde_table_in_use == fde_table_allocated)
2181     {
2182       fde_table_allocated += FDE_TABLE_INCREMENT;
2183       fde_table = ggc_realloc (fde_table,
2184                                fde_table_allocated * sizeof (dw_fde_node));
2185       memset (fde_table + fde_table_in_use, 0,
2186               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2187     }
2188
2189   /* Record the FDE associated with this function.  */
2190   current_funcdef_fde = fde_table_in_use;
2191
2192   /* Add the new FDE at the end of the fde_table.  */
2193   fde = &fde_table[fde_table_in_use++];
2194   fde->dw_fde_begin = xstrdup (label);
2195   fde->dw_fde_current_label = NULL;
2196   fde->dw_fde_end = NULL;
2197   fde->dw_fde_cfi = NULL;
2198   fde->funcdef_number = current_function_funcdef_no;
2199   fde->nothrow = current_function_nothrow;
2200   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2201   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2202
2203   args_size = old_args_size = 0;
2204
2205   /* We only want to output line number information for the genuine dwarf2
2206      prologue case, not the eh frame case.  */
2207 #ifdef DWARF2_DEBUGGING_INFO
2208   if (file)
2209     dwarf2out_source_line (line, file);
2210 #endif
2211 }
2212
2213 /* Output a marker (i.e. a label) for the absolute end of the generated code
2214    for a function definition.  This gets called *after* the epilogue code has
2215    been generated.  */
2216
2217 void
2218 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2219                         const char *file ATTRIBUTE_UNUSED)
2220 {
2221   dw_fde_ref fde;
2222   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2223
2224   /* Output a label to mark the endpoint of the code generated for this
2225      function.  */
2226   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2227                                current_function_funcdef_no);
2228   ASM_OUTPUT_LABEL (asm_out_file, label);
2229   fde = &fde_table[fde_table_in_use - 1];
2230   fde->dw_fde_end = xstrdup (label);
2231 }
2232
2233 void
2234 dwarf2out_frame_init (void)
2235 {
2236   /* Allocate the initial hunk of the fde_table.  */
2237   fde_table = (dw_fde_ref) ggc_alloc_cleared (FDE_TABLE_INCREMENT
2238                                               * sizeof (dw_fde_node));
2239   fde_table_allocated = FDE_TABLE_INCREMENT;
2240   fde_table_in_use = 0;
2241
2242   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2243      sake of lookup_cfa.  */
2244
2245 #ifdef DWARF2_UNWIND_INFO
2246   /* On entry, the Canonical Frame Address is at SP.  */
2247   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2248   initial_return_save (INCOMING_RETURN_ADDR_RTX);
2249 #endif
2250 }
2251
2252 void
2253 dwarf2out_frame_finish (void)
2254 {
2255   /* Output call frame information.  */
2256   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2257     output_call_frame_info (0);
2258
2259   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2260     output_call_frame_info (1);
2261 }
2262 #endif
2263 \f
2264 /* And now, the subset of the debugging information support code necessary
2265    for emitting location expressions.  */
2266
2267 /* We need some way to distinguish DW_OP_addr with a direct symbol
2268    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2269 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2270
2271
2272 typedef struct dw_val_struct *dw_val_ref;
2273 typedef struct die_struct *dw_die_ref;
2274 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2275 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2276
2277 /* Each DIE may have a series of attribute/value pairs.  Values
2278    can take on several forms.  The forms that are used in this
2279    implementation are listed below.  */
2280
2281 enum dw_val_class
2282 {
2283   dw_val_class_addr,
2284   dw_val_class_offset,
2285   dw_val_class_loc,
2286   dw_val_class_loc_list,
2287   dw_val_class_range_list,
2288   dw_val_class_const,
2289   dw_val_class_unsigned_const,
2290   dw_val_class_long_long,
2291   dw_val_class_float,
2292   dw_val_class_flag,
2293   dw_val_class_die_ref,
2294   dw_val_class_fde_ref,
2295   dw_val_class_lbl_id,
2296   dw_val_class_lbl_offset,
2297   dw_val_class_str
2298 };
2299
2300 /* Describe a double word constant value.  */
2301 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2302
2303 typedef struct dw_long_long_struct GTY(())
2304 {
2305   unsigned long hi;
2306   unsigned long low;
2307 }
2308 dw_long_long_const;
2309
2310 /* Describe a floating point constant value.  */
2311
2312 typedef struct dw_fp_struct GTY(())
2313 {
2314   long * GTY((length ("%h.length"))) array;
2315   unsigned length;
2316 }
2317 dw_float_const;
2318
2319 /* The dw_val_node describes an attribute's value, as it is
2320    represented internally.  */
2321
2322 typedef struct dw_val_struct GTY(())
2323 {
2324   enum dw_val_class val_class;
2325   union dw_val_struct_union
2326     {
2327       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2328       long unsigned GTY ((tag ("dw_val_class_offset"))) val_offset;
2329       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2330       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2331       long int GTY ((default (""))) val_int;
2332       long unsigned GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2333       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2334       dw_float_const GTY ((tag ("dw_val_class_float"))) val_float;
2335       struct dw_val_die_union
2336         {
2337           dw_die_ref die;
2338           int external;
2339         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2340       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2341       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2342       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2343       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2344     }
2345   GTY ((desc ("%1.val_class"))) v;
2346 }
2347 dw_val_node;
2348
2349 /* Locations in memory are described using a sequence of stack machine
2350    operations.  */
2351
2352 typedef struct dw_loc_descr_struct GTY(())
2353 {
2354   dw_loc_descr_ref dw_loc_next;
2355   enum dwarf_location_atom dw_loc_opc;
2356   dw_val_node dw_loc_oprnd1;
2357   dw_val_node dw_loc_oprnd2;
2358   int dw_loc_addr;
2359 }
2360 dw_loc_descr_node;
2361
2362 /* Location lists are ranges + location descriptions for that range,
2363    so you can track variables that are in different places over
2364    their entire life.  */
2365 typedef struct dw_loc_list_struct GTY(())
2366 {
2367   dw_loc_list_ref dw_loc_next;
2368   const char *begin; /* Label for begin address of range */
2369   const char *end;  /* Label for end address of range */
2370   char *ll_symbol; /* Label for beginning of location list.
2371                       Only on head of list */
2372   const char *section; /* Section this loclist is relative to */
2373   dw_loc_descr_ref expr;
2374 } dw_loc_list_node;
2375
2376 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2377
2378 static const char *dwarf_stack_op_name (unsigned);
2379 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2380                                        unsigned long, unsigned long);
2381 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2382 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2383 static unsigned long size_of_locs (dw_loc_descr_ref);
2384 static void output_loc_operands (dw_loc_descr_ref);
2385 static void output_loc_sequence (dw_loc_descr_ref);
2386
2387 /* Convert a DWARF stack opcode into its string name.  */
2388
2389 static const char *
2390 dwarf_stack_op_name (unsigned int op)
2391 {
2392   switch (op)
2393     {
2394     case DW_OP_addr:
2395     case INTERNAL_DW_OP_tls_addr:
2396       return "DW_OP_addr";
2397     case DW_OP_deref:
2398       return "DW_OP_deref";
2399     case DW_OP_const1u:
2400       return "DW_OP_const1u";
2401     case DW_OP_const1s:
2402       return "DW_OP_const1s";
2403     case DW_OP_const2u:
2404       return "DW_OP_const2u";
2405     case DW_OP_const2s:
2406       return "DW_OP_const2s";
2407     case DW_OP_const4u:
2408       return "DW_OP_const4u";
2409     case DW_OP_const4s:
2410       return "DW_OP_const4s";
2411     case DW_OP_const8u:
2412       return "DW_OP_const8u";
2413     case DW_OP_const8s:
2414       return "DW_OP_const8s";
2415     case DW_OP_constu:
2416       return "DW_OP_constu";
2417     case DW_OP_consts:
2418       return "DW_OP_consts";
2419     case DW_OP_dup:
2420       return "DW_OP_dup";
2421     case DW_OP_drop:
2422       return "DW_OP_drop";
2423     case DW_OP_over:
2424       return "DW_OP_over";
2425     case DW_OP_pick:
2426       return "DW_OP_pick";
2427     case DW_OP_swap:
2428       return "DW_OP_swap";
2429     case DW_OP_rot:
2430       return "DW_OP_rot";
2431     case DW_OP_xderef:
2432       return "DW_OP_xderef";
2433     case DW_OP_abs:
2434       return "DW_OP_abs";
2435     case DW_OP_and:
2436       return "DW_OP_and";
2437     case DW_OP_div:
2438       return "DW_OP_div";
2439     case DW_OP_minus:
2440       return "DW_OP_minus";
2441     case DW_OP_mod:
2442       return "DW_OP_mod";
2443     case DW_OP_mul:
2444       return "DW_OP_mul";
2445     case DW_OP_neg:
2446       return "DW_OP_neg";
2447     case DW_OP_not:
2448       return "DW_OP_not";
2449     case DW_OP_or:
2450       return "DW_OP_or";
2451     case DW_OP_plus:
2452       return "DW_OP_plus";
2453     case DW_OP_plus_uconst:
2454       return "DW_OP_plus_uconst";
2455     case DW_OP_shl:
2456       return "DW_OP_shl";
2457     case DW_OP_shr:
2458       return "DW_OP_shr";
2459     case DW_OP_shra:
2460       return "DW_OP_shra";
2461     case DW_OP_xor:
2462       return "DW_OP_xor";
2463     case DW_OP_bra:
2464       return "DW_OP_bra";
2465     case DW_OP_eq:
2466       return "DW_OP_eq";
2467     case DW_OP_ge:
2468       return "DW_OP_ge";
2469     case DW_OP_gt:
2470       return "DW_OP_gt";
2471     case DW_OP_le:
2472       return "DW_OP_le";
2473     case DW_OP_lt:
2474       return "DW_OP_lt";
2475     case DW_OP_ne:
2476       return "DW_OP_ne";
2477     case DW_OP_skip:
2478       return "DW_OP_skip";
2479     case DW_OP_lit0:
2480       return "DW_OP_lit0";
2481     case DW_OP_lit1:
2482       return "DW_OP_lit1";
2483     case DW_OP_lit2:
2484       return "DW_OP_lit2";
2485     case DW_OP_lit3:
2486       return "DW_OP_lit3";
2487     case DW_OP_lit4:
2488       return "DW_OP_lit4";
2489     case DW_OP_lit5:
2490       return "DW_OP_lit5";
2491     case DW_OP_lit6:
2492       return "DW_OP_lit6";
2493     case DW_OP_lit7:
2494       return "DW_OP_lit7";
2495     case DW_OP_lit8:
2496       return "DW_OP_lit8";
2497     case DW_OP_lit9:
2498       return "DW_OP_lit9";
2499     case DW_OP_lit10:
2500       return "DW_OP_lit10";
2501     case DW_OP_lit11:
2502       return "DW_OP_lit11";
2503     case DW_OP_lit12:
2504       return "DW_OP_lit12";
2505     case DW_OP_lit13:
2506       return "DW_OP_lit13";
2507     case DW_OP_lit14:
2508       return "DW_OP_lit14";
2509     case DW_OP_lit15:
2510       return "DW_OP_lit15";
2511     case DW_OP_lit16:
2512       return "DW_OP_lit16";
2513     case DW_OP_lit17:
2514       return "DW_OP_lit17";
2515     case DW_OP_lit18:
2516       return "DW_OP_lit18";
2517     case DW_OP_lit19:
2518       return "DW_OP_lit19";
2519     case DW_OP_lit20:
2520       return "DW_OP_lit20";
2521     case DW_OP_lit21:
2522       return "DW_OP_lit21";
2523     case DW_OP_lit22:
2524       return "DW_OP_lit22";
2525     case DW_OP_lit23:
2526       return "DW_OP_lit23";
2527     case DW_OP_lit24:
2528       return "DW_OP_lit24";
2529     case DW_OP_lit25:
2530       return "DW_OP_lit25";
2531     case DW_OP_lit26:
2532       return "DW_OP_lit26";
2533     case DW_OP_lit27:
2534       return "DW_OP_lit27";
2535     case DW_OP_lit28:
2536       return "DW_OP_lit28";
2537     case DW_OP_lit29:
2538       return "DW_OP_lit29";
2539     case DW_OP_lit30:
2540       return "DW_OP_lit30";
2541     case DW_OP_lit31:
2542       return "DW_OP_lit31";
2543     case DW_OP_reg0:
2544       return "DW_OP_reg0";
2545     case DW_OP_reg1:
2546       return "DW_OP_reg1";
2547     case DW_OP_reg2:
2548       return "DW_OP_reg2";
2549     case DW_OP_reg3:
2550       return "DW_OP_reg3";
2551     case DW_OP_reg4:
2552       return "DW_OP_reg4";
2553     case DW_OP_reg5:
2554       return "DW_OP_reg5";
2555     case DW_OP_reg6:
2556       return "DW_OP_reg6";
2557     case DW_OP_reg7:
2558       return "DW_OP_reg7";
2559     case DW_OP_reg8:
2560       return "DW_OP_reg8";
2561     case DW_OP_reg9:
2562       return "DW_OP_reg9";
2563     case DW_OP_reg10:
2564       return "DW_OP_reg10";
2565     case DW_OP_reg11:
2566       return "DW_OP_reg11";
2567     case DW_OP_reg12:
2568       return "DW_OP_reg12";
2569     case DW_OP_reg13:
2570       return "DW_OP_reg13";
2571     case DW_OP_reg14:
2572       return "DW_OP_reg14";
2573     case DW_OP_reg15:
2574       return "DW_OP_reg15";
2575     case DW_OP_reg16:
2576       return "DW_OP_reg16";
2577     case DW_OP_reg17:
2578       return "DW_OP_reg17";
2579     case DW_OP_reg18:
2580       return "DW_OP_reg18";
2581     case DW_OP_reg19:
2582       return "DW_OP_reg19";
2583     case DW_OP_reg20:
2584       return "DW_OP_reg20";
2585     case DW_OP_reg21:
2586       return "DW_OP_reg21";
2587     case DW_OP_reg22:
2588       return "DW_OP_reg22";
2589     case DW_OP_reg23:
2590       return "DW_OP_reg23";
2591     case DW_OP_reg24:
2592       return "DW_OP_reg24";
2593     case DW_OP_reg25:
2594       return "DW_OP_reg25";
2595     case DW_OP_reg26:
2596       return "DW_OP_reg26";
2597     case DW_OP_reg27:
2598       return "DW_OP_reg27";
2599     case DW_OP_reg28:
2600       return "DW_OP_reg28";
2601     case DW_OP_reg29:
2602       return "DW_OP_reg29";
2603     case DW_OP_reg30:
2604       return "DW_OP_reg30";
2605     case DW_OP_reg31:
2606       return "DW_OP_reg31";
2607     case DW_OP_breg0:
2608       return "DW_OP_breg0";
2609     case DW_OP_breg1:
2610       return "DW_OP_breg1";
2611     case DW_OP_breg2:
2612       return "DW_OP_breg2";
2613     case DW_OP_breg3:
2614       return "DW_OP_breg3";
2615     case DW_OP_breg4:
2616       return "DW_OP_breg4";
2617     case DW_OP_breg5:
2618       return "DW_OP_breg5";
2619     case DW_OP_breg6:
2620       return "DW_OP_breg6";
2621     case DW_OP_breg7:
2622       return "DW_OP_breg7";
2623     case DW_OP_breg8:
2624       return "DW_OP_breg8";
2625     case DW_OP_breg9:
2626       return "DW_OP_breg9";
2627     case DW_OP_breg10:
2628       return "DW_OP_breg10";
2629     case DW_OP_breg11:
2630       return "DW_OP_breg11";
2631     case DW_OP_breg12:
2632       return "DW_OP_breg12";
2633     case DW_OP_breg13:
2634       return "DW_OP_breg13";
2635     case DW_OP_breg14:
2636       return "DW_OP_breg14";
2637     case DW_OP_breg15:
2638       return "DW_OP_breg15";
2639     case DW_OP_breg16:
2640       return "DW_OP_breg16";
2641     case DW_OP_breg17:
2642       return "DW_OP_breg17";
2643     case DW_OP_breg18:
2644       return "DW_OP_breg18";
2645     case DW_OP_breg19:
2646       return "DW_OP_breg19";
2647     case DW_OP_breg20:
2648       return "DW_OP_breg20";
2649     case DW_OP_breg21:
2650       return "DW_OP_breg21";
2651     case DW_OP_breg22:
2652       return "DW_OP_breg22";
2653     case DW_OP_breg23:
2654       return "DW_OP_breg23";
2655     case DW_OP_breg24:
2656       return "DW_OP_breg24";
2657     case DW_OP_breg25:
2658       return "DW_OP_breg25";
2659     case DW_OP_breg26:
2660       return "DW_OP_breg26";
2661     case DW_OP_breg27:
2662       return "DW_OP_breg27";
2663     case DW_OP_breg28:
2664       return "DW_OP_breg28";
2665     case DW_OP_breg29:
2666       return "DW_OP_breg29";
2667     case DW_OP_breg30:
2668       return "DW_OP_breg30";
2669     case DW_OP_breg31:
2670       return "DW_OP_breg31";
2671     case DW_OP_regx:
2672       return "DW_OP_regx";
2673     case DW_OP_fbreg:
2674       return "DW_OP_fbreg";
2675     case DW_OP_bregx:
2676       return "DW_OP_bregx";
2677     case DW_OP_piece:
2678       return "DW_OP_piece";
2679     case DW_OP_deref_size:
2680       return "DW_OP_deref_size";
2681     case DW_OP_xderef_size:
2682       return "DW_OP_xderef_size";
2683     case DW_OP_nop:
2684       return "DW_OP_nop";
2685     case DW_OP_push_object_address:
2686       return "DW_OP_push_object_address";
2687     case DW_OP_call2:
2688       return "DW_OP_call2";
2689     case DW_OP_call4:
2690       return "DW_OP_call4";
2691     case DW_OP_call_ref:
2692       return "DW_OP_call_ref";
2693     case DW_OP_GNU_push_tls_address:
2694       return "DW_OP_GNU_push_tls_address";
2695     default:
2696       return "OP_<unknown>";
2697     }
2698 }
2699
2700 /* Return a pointer to a newly allocated location description.  Location
2701    descriptions are simple expression terms that can be strung
2702    together to form more complicated location (address) descriptions.  */
2703
2704 static inline dw_loc_descr_ref
2705 new_loc_descr (enum dwarf_location_atom op, long unsigned int oprnd1,
2706                long unsigned int oprnd2)
2707 {
2708   dw_loc_descr_ref descr
2709     = (dw_loc_descr_ref) ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2710
2711   descr->dw_loc_opc = op;
2712   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2713   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2714   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2715   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2716
2717   return descr;
2718 }
2719
2720
2721 /* Add a location description term to a location description expression.  */
2722
2723 static inline void
2724 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2725 {
2726   dw_loc_descr_ref *d;
2727
2728   /* Find the end of the chain.  */
2729   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2730     ;
2731
2732   *d = descr;
2733 }
2734
2735 /* Return the size of a location descriptor.  */
2736
2737 static unsigned long
2738 size_of_loc_descr (dw_loc_descr_ref loc)
2739 {
2740   unsigned long size = 1;
2741
2742   switch (loc->dw_loc_opc)
2743     {
2744     case DW_OP_addr:
2745     case INTERNAL_DW_OP_tls_addr:
2746       size += DWARF2_ADDR_SIZE;
2747       break;
2748     case DW_OP_const1u:
2749     case DW_OP_const1s:
2750       size += 1;
2751       break;
2752     case DW_OP_const2u:
2753     case DW_OP_const2s:
2754       size += 2;
2755       break;
2756     case DW_OP_const4u:
2757     case DW_OP_const4s:
2758       size += 4;
2759       break;
2760     case DW_OP_const8u:
2761     case DW_OP_const8s:
2762       size += 8;
2763       break;
2764     case DW_OP_constu:
2765       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2766       break;
2767     case DW_OP_consts:
2768       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2769       break;
2770     case DW_OP_pick:
2771       size += 1;
2772       break;
2773     case DW_OP_plus_uconst:
2774       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2775       break;
2776     case DW_OP_skip:
2777     case DW_OP_bra:
2778       size += 2;
2779       break;
2780     case DW_OP_breg0:
2781     case DW_OP_breg1:
2782     case DW_OP_breg2:
2783     case DW_OP_breg3:
2784     case DW_OP_breg4:
2785     case DW_OP_breg5:
2786     case DW_OP_breg6:
2787     case DW_OP_breg7:
2788     case DW_OP_breg8:
2789     case DW_OP_breg9:
2790     case DW_OP_breg10:
2791     case DW_OP_breg11:
2792     case DW_OP_breg12:
2793     case DW_OP_breg13:
2794     case DW_OP_breg14:
2795     case DW_OP_breg15:
2796     case DW_OP_breg16:
2797     case DW_OP_breg17:
2798     case DW_OP_breg18:
2799     case DW_OP_breg19:
2800     case DW_OP_breg20:
2801     case DW_OP_breg21:
2802     case DW_OP_breg22:
2803     case DW_OP_breg23:
2804     case DW_OP_breg24:
2805     case DW_OP_breg25:
2806     case DW_OP_breg26:
2807     case DW_OP_breg27:
2808     case DW_OP_breg28:
2809     case DW_OP_breg29:
2810     case DW_OP_breg30:
2811     case DW_OP_breg31:
2812       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2813       break;
2814     case DW_OP_regx:
2815       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2816       break;
2817     case DW_OP_fbreg:
2818       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2819       break;
2820     case DW_OP_bregx:
2821       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2822       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2823       break;
2824     case DW_OP_piece:
2825       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2826       break;
2827     case DW_OP_deref_size:
2828     case DW_OP_xderef_size:
2829       size += 1;
2830       break;
2831     case DW_OP_call2:
2832       size += 2;
2833       break;
2834     case DW_OP_call4:
2835       size += 4;
2836       break;
2837     case DW_OP_call_ref:
2838       size += DWARF2_ADDR_SIZE;
2839       break;
2840     default:
2841       break;
2842     }
2843
2844   return size;
2845 }
2846
2847 /* Return the size of a series of location descriptors.  */
2848
2849 static unsigned long
2850 size_of_locs (dw_loc_descr_ref loc)
2851 {
2852   unsigned long size;
2853
2854   for (size = 0; loc != NULL; loc = loc->dw_loc_next)
2855     {
2856       loc->dw_loc_addr = size;
2857       size += size_of_loc_descr (loc);
2858     }
2859
2860   return size;
2861 }
2862
2863 /* Output location description stack opcode's operands (if any).  */
2864
2865 static void
2866 output_loc_operands (dw_loc_descr_ref loc)
2867 {
2868   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2869   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2870
2871   switch (loc->dw_loc_opc)
2872     {
2873 #ifdef DWARF2_DEBUGGING_INFO
2874     case DW_OP_addr:
2875       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2876       break;
2877     case DW_OP_const2u:
2878     case DW_OP_const2s:
2879       dw2_asm_output_data (2, val1->v.val_int, NULL);
2880       break;
2881     case DW_OP_const4u:
2882     case DW_OP_const4s:
2883       dw2_asm_output_data (4, val1->v.val_int, NULL);
2884       break;
2885     case DW_OP_const8u:
2886     case DW_OP_const8s:
2887       if (HOST_BITS_PER_LONG < 64)
2888         abort ();
2889       dw2_asm_output_data (8, val1->v.val_int, NULL);
2890       break;
2891     case DW_OP_skip:
2892     case DW_OP_bra:
2893       {
2894         int offset;
2895
2896         if (val1->val_class == dw_val_class_loc)
2897           offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2898         else
2899           abort ();
2900
2901         dw2_asm_output_data (2, offset, NULL);
2902       }
2903       break;
2904 #else
2905     case DW_OP_addr:
2906     case DW_OP_const2u:
2907     case DW_OP_const2s:
2908     case DW_OP_const4u:
2909     case DW_OP_const4s:
2910     case DW_OP_const8u:
2911     case DW_OP_const8s:
2912     case DW_OP_skip:
2913     case DW_OP_bra:
2914       /* We currently don't make any attempt to make sure these are
2915          aligned properly like we do for the main unwind info, so
2916          don't support emitting things larger than a byte if we're
2917          only doing unwinding.  */
2918       abort ();
2919 #endif
2920     case DW_OP_const1u:
2921     case DW_OP_const1s:
2922       dw2_asm_output_data (1, val1->v.val_int, NULL);
2923       break;
2924     case DW_OP_constu:
2925       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2926       break;
2927     case DW_OP_consts:
2928       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2929       break;
2930     case DW_OP_pick:
2931       dw2_asm_output_data (1, val1->v.val_int, NULL);
2932       break;
2933     case DW_OP_plus_uconst:
2934       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2935       break;
2936     case DW_OP_breg0:
2937     case DW_OP_breg1:
2938     case DW_OP_breg2:
2939     case DW_OP_breg3:
2940     case DW_OP_breg4:
2941     case DW_OP_breg5:
2942     case DW_OP_breg6:
2943     case DW_OP_breg7:
2944     case DW_OP_breg8:
2945     case DW_OP_breg9:
2946     case DW_OP_breg10:
2947     case DW_OP_breg11:
2948     case DW_OP_breg12:
2949     case DW_OP_breg13:
2950     case DW_OP_breg14:
2951     case DW_OP_breg15:
2952     case DW_OP_breg16:
2953     case DW_OP_breg17:
2954     case DW_OP_breg18:
2955     case DW_OP_breg19:
2956     case DW_OP_breg20:
2957     case DW_OP_breg21:
2958     case DW_OP_breg22:
2959     case DW_OP_breg23:
2960     case DW_OP_breg24:
2961     case DW_OP_breg25:
2962     case DW_OP_breg26:
2963     case DW_OP_breg27:
2964     case DW_OP_breg28:
2965     case DW_OP_breg29:
2966     case DW_OP_breg30:
2967     case DW_OP_breg31:
2968       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2969       break;
2970     case DW_OP_regx:
2971       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2972       break;
2973     case DW_OP_fbreg:
2974       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2975       break;
2976     case DW_OP_bregx:
2977       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2978       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2979       break;
2980     case DW_OP_piece:
2981       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2982       break;
2983     case DW_OP_deref_size:
2984     case DW_OP_xderef_size:
2985       dw2_asm_output_data (1, val1->v.val_int, NULL);
2986       break;
2987
2988     case INTERNAL_DW_OP_tls_addr:
2989 #ifdef ASM_OUTPUT_DWARF_DTPREL
2990       ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
2991                                val1->v.val_addr);
2992       fputc ('\n', asm_out_file);
2993 #else
2994       abort ();
2995 #endif
2996       break;
2997
2998     default:
2999       /* Other codes have no operands.  */
3000       break;
3001     }
3002 }
3003
3004 /* Output a sequence of location operations.  */
3005
3006 static void
3007 output_loc_sequence (dw_loc_descr_ref loc)
3008 {
3009   for (; loc != NULL; loc = loc->dw_loc_next)
3010     {
3011       /* Output the opcode.  */
3012       dw2_asm_output_data (1, loc->dw_loc_opc,
3013                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3014
3015       /* Output the operand(s) (if any).  */
3016       output_loc_operands (loc);
3017     }
3018 }
3019
3020 /* This routine will generate the correct assembly data for a location
3021    description based on a cfi entry with a complex address.  */
3022
3023 static void
3024 output_cfa_loc (dw_cfi_ref cfi)
3025 {
3026   dw_loc_descr_ref loc;
3027   unsigned long size;
3028
3029   /* Output the size of the block.  */
3030   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3031   size = size_of_locs (loc);
3032   dw2_asm_output_data_uleb128 (size, NULL);
3033
3034   /* Now output the operations themselves.  */
3035   output_loc_sequence (loc);
3036 }
3037
3038 /* This function builds a dwarf location descriptor sequence from
3039    a dw_cfa_location.  */
3040
3041 static struct dw_loc_descr_struct *
3042 build_cfa_loc (dw_cfa_location *cfa)
3043 {
3044   struct dw_loc_descr_struct *head, *tmp;
3045
3046   if (cfa->indirect == 0)
3047     abort ();
3048
3049   if (cfa->base_offset)
3050     {
3051       if (cfa->reg <= 31)
3052         head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3053       else
3054         head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3055     }
3056   else if (cfa->reg <= 31)
3057     head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3058   else
3059     head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3060
3061   head->dw_loc_oprnd1.val_class = dw_val_class_const;
3062   tmp = new_loc_descr (DW_OP_deref, 0, 0);
3063   add_loc_descr (&head, tmp);
3064   if (cfa->offset != 0)
3065     {
3066       tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3067       add_loc_descr (&head, tmp);
3068     }
3069
3070   return head;
3071 }
3072
3073 /* This function fills in aa dw_cfa_location structure from a dwarf location
3074    descriptor sequence.  */
3075
3076 static void
3077 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3078 {
3079   struct dw_loc_descr_struct *ptr;
3080   cfa->offset = 0;
3081   cfa->base_offset = 0;
3082   cfa->indirect = 0;
3083   cfa->reg = -1;
3084
3085   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3086     {
3087       enum dwarf_location_atom op = ptr->dw_loc_opc;
3088
3089       switch (op)
3090         {
3091         case DW_OP_reg0:
3092         case DW_OP_reg1:
3093         case DW_OP_reg2:
3094         case DW_OP_reg3:
3095         case DW_OP_reg4:
3096         case DW_OP_reg5:
3097         case DW_OP_reg6:
3098         case DW_OP_reg7:
3099         case DW_OP_reg8:
3100         case DW_OP_reg9:
3101         case DW_OP_reg10:
3102         case DW_OP_reg11:
3103         case DW_OP_reg12:
3104         case DW_OP_reg13:
3105         case DW_OP_reg14:
3106         case DW_OP_reg15:
3107         case DW_OP_reg16:
3108         case DW_OP_reg17:
3109         case DW_OP_reg18:
3110         case DW_OP_reg19:
3111         case DW_OP_reg20:
3112         case DW_OP_reg21:
3113         case DW_OP_reg22:
3114         case DW_OP_reg23:
3115         case DW_OP_reg24:
3116         case DW_OP_reg25:
3117         case DW_OP_reg26:
3118         case DW_OP_reg27:
3119         case DW_OP_reg28:
3120         case DW_OP_reg29:
3121         case DW_OP_reg30:
3122         case DW_OP_reg31:
3123           cfa->reg = op - DW_OP_reg0;
3124           break;
3125         case DW_OP_regx:
3126           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3127           break;
3128         case DW_OP_breg0:
3129         case DW_OP_breg1:
3130         case DW_OP_breg2:
3131         case DW_OP_breg3:
3132         case DW_OP_breg4:
3133         case DW_OP_breg5:
3134         case DW_OP_breg6:
3135         case DW_OP_breg7:
3136         case DW_OP_breg8:
3137         case DW_OP_breg9:
3138         case DW_OP_breg10:
3139         case DW_OP_breg11:
3140         case DW_OP_breg12:
3141         case DW_OP_breg13:
3142         case DW_OP_breg14:
3143         case DW_OP_breg15:
3144         case DW_OP_breg16:
3145         case DW_OP_breg17:
3146         case DW_OP_breg18:
3147         case DW_OP_breg19:
3148         case DW_OP_breg20:
3149         case DW_OP_breg21:
3150         case DW_OP_breg22:
3151         case DW_OP_breg23:
3152         case DW_OP_breg24:
3153         case DW_OP_breg25:
3154         case DW_OP_breg26:
3155         case DW_OP_breg27:
3156         case DW_OP_breg28:
3157         case DW_OP_breg29:
3158         case DW_OP_breg30:
3159         case DW_OP_breg31:
3160           cfa->reg = op - DW_OP_breg0;
3161           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3162           break;
3163         case DW_OP_bregx:
3164           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3165           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3166           break;
3167         case DW_OP_deref:
3168           cfa->indirect = 1;
3169           break;
3170         case DW_OP_plus_uconst:
3171           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3172           break;
3173         default:
3174           internal_error ("DW_LOC_OP %s not implemented\n",
3175                           dwarf_stack_op_name (ptr->dw_loc_opc));
3176         }
3177     }
3178 }
3179 #endif /* .debug_frame support */
3180 \f
3181 /* And now, the support for symbolic debugging information.  */
3182 #ifdef DWARF2_DEBUGGING_INFO
3183
3184 /* .debug_str support.  */
3185 static int output_indirect_string (void **, void *);
3186
3187 static void dwarf2out_init (const char *);
3188 static void dwarf2out_finish (const char *);
3189 static void dwarf2out_define (unsigned int, const char *);
3190 static void dwarf2out_undef (unsigned int, const char *);
3191 static void dwarf2out_start_source_file (unsigned, const char *);
3192 static void dwarf2out_end_source_file (unsigned);
3193 static void dwarf2out_begin_block (unsigned, unsigned);
3194 static void dwarf2out_end_block (unsigned, unsigned);
3195 static bool dwarf2out_ignore_block (tree);
3196 static void dwarf2out_global_decl (tree);
3197 static void dwarf2out_abstract_function (tree);
3198
3199 /* The debug hooks structure.  */
3200
3201 const struct gcc_debug_hooks dwarf2_debug_hooks =
3202 {
3203   dwarf2out_init,
3204   dwarf2out_finish,
3205   dwarf2out_define,
3206   dwarf2out_undef,
3207   dwarf2out_start_source_file,
3208   dwarf2out_end_source_file,
3209   dwarf2out_begin_block,
3210   dwarf2out_end_block,
3211   dwarf2out_ignore_block,
3212   dwarf2out_source_line,
3213   dwarf2out_begin_prologue,
3214   debug_nothing_int_charstar,   /* end_prologue */
3215   dwarf2out_end_epilogue,
3216   debug_nothing_tree,           /* begin_function */
3217   debug_nothing_int,            /* end_function */
3218   dwarf2out_decl,               /* function_decl */
3219   dwarf2out_global_decl,
3220   debug_nothing_tree,           /* deferred_inline_function */
3221   /* The DWARF 2 backend tries to reduce debugging bloat by not
3222      emitting the abstract description of inline functions until
3223      something tries to reference them.  */
3224   dwarf2out_abstract_function,  /* outlining_inline_function */
3225   debug_nothing_rtx,            /* label */
3226   debug_nothing_int             /* handle_pch */
3227 };
3228 #endif
3229 \f
3230 /* NOTE: In the comments in this file, many references are made to
3231    "Debugging Information Entries".  This term is abbreviated as `DIE'
3232    throughout the remainder of this file.  */
3233
3234 /* An internal representation of the DWARF output is built, and then
3235    walked to generate the DWARF debugging info.  The walk of the internal
3236    representation is done after the entire program has been compiled.
3237    The types below are used to describe the internal representation.  */
3238
3239 /* Various DIE's use offsets relative to the beginning of the
3240    .debug_info section to refer to each other.  */
3241
3242 typedef long int dw_offset;
3243
3244 /* Define typedefs here to avoid circular dependencies.  */
3245
3246 typedef struct dw_attr_struct *dw_attr_ref;
3247 typedef struct dw_line_info_struct *dw_line_info_ref;
3248 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3249 typedef struct pubname_struct *pubname_ref;
3250 typedef struct dw_ranges_struct *dw_ranges_ref;
3251
3252 /* Each entry in the line_info_table maintains the file and
3253    line number associated with the label generated for that
3254    entry.  The label gives the PC value associated with
3255    the line number entry.  */
3256
3257 typedef struct dw_line_info_struct GTY(())
3258 {
3259   unsigned long dw_file_num;
3260   unsigned long dw_line_num;
3261 }
3262 dw_line_info_entry;
3263
3264 /* Line information for functions in separate sections; each one gets its
3265    own sequence.  */
3266 typedef struct dw_separate_line_info_struct GTY(())
3267 {
3268   unsigned long dw_file_num;
3269   unsigned long dw_line_num;
3270   unsigned long function;
3271 }
3272 dw_separate_line_info_entry;
3273
3274 /* Each DIE attribute has a field specifying the attribute kind,
3275    a link to the next attribute in the chain, and an attribute value.
3276    Attributes are typically linked below the DIE they modify.  */
3277
3278 typedef struct dw_attr_struct GTY(())
3279 {
3280   enum dwarf_attribute dw_attr;
3281   dw_attr_ref dw_attr_next;
3282   dw_val_node dw_attr_val;
3283 }
3284 dw_attr_node;
3285
3286 /* The Debugging Information Entry (DIE) structure */
3287
3288 typedef struct die_struct GTY(())
3289 {
3290   enum dwarf_tag die_tag;
3291   char *die_symbol;
3292   dw_attr_ref die_attr;
3293   dw_die_ref die_parent;
3294   dw_die_ref die_child;
3295   dw_die_ref die_sib;
3296   dw_offset die_offset;
3297   unsigned long die_abbrev;
3298   int die_mark;
3299 }
3300 die_node;
3301
3302 /* The pubname structure */
3303
3304 typedef struct pubname_struct GTY(())
3305 {
3306   dw_die_ref die;
3307   char *name;
3308 }
3309 pubname_entry;
3310
3311 struct dw_ranges_struct GTY(())
3312 {
3313   int block_num;
3314 };
3315
3316 /* The limbo die list structure.  */
3317 typedef struct limbo_die_struct GTY(())
3318 {
3319   dw_die_ref die;
3320   tree created_for;
3321   struct limbo_die_struct *next;
3322 }
3323 limbo_die_node;
3324
3325 /* How to start an assembler comment.  */
3326 #ifndef ASM_COMMENT_START
3327 #define ASM_COMMENT_START ";#"
3328 #endif
3329
3330 /* Define a macro which returns nonzero for a TYPE_DECL which was
3331    implicitly generated for a tagged type.
3332
3333    Note that unlike the gcc front end (which generates a NULL named
3334    TYPE_DECL node for each complete tagged type, each array type, and
3335    each function type node created) the g++ front end generates a
3336    _named_ TYPE_DECL node for each tagged type node created.
3337    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3338    generate a DW_TAG_typedef DIE for them.  */
3339
3340 #define TYPE_DECL_IS_STUB(decl)                         \
3341   (DECL_NAME (decl) == NULL_TREE                        \
3342    || (DECL_ARTIFICIAL (decl)                           \
3343        && is_tagged_type (TREE_TYPE (decl))             \
3344        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3345            /* This is necessary for stub decls that     \
3346               appear in nested inline functions.  */    \
3347            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3348                && (decl_ultimate_origin (decl)          \
3349                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3350
3351 /* Information concerning the compilation unit's programming
3352    language, and compiler version.  */
3353
3354 /* Fixed size portion of the DWARF compilation unit header.  */
3355 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3356   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3357
3358 /* Fixed size portion of public names info.  */
3359 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3360
3361 /* Fixed size portion of the address range info.  */
3362 #define DWARF_ARANGES_HEADER_SIZE                                       \
3363   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3364                 DWARF2_ADDR_SIZE * 2)                                   \
3365    - DWARF_INITIAL_LENGTH_SIZE)
3366
3367 /* Size of padding portion in the address range info.  It must be
3368    aligned to twice the pointer size.  */
3369 #define DWARF_ARANGES_PAD_SIZE \
3370   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3371                 DWARF2_ADDR_SIZE * 2) \
3372    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3373
3374 /* Use assembler line directives if available.  */
3375 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3376 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3377 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3378 #else
3379 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3380 #endif
3381 #endif
3382
3383 /* Minimum line offset in a special line info. opcode.
3384    This value was chosen to give a reasonable range of values.  */
3385 #define DWARF_LINE_BASE  -10
3386
3387 /* First special line opcode - leave room for the standard opcodes.  */
3388 #define DWARF_LINE_OPCODE_BASE  10
3389
3390 /* Range of line offsets in a special line info. opcode.  */
3391 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3392
3393 /* Flag that indicates the initial value of the is_stmt_start flag.
3394    In the present implementation, we do not mark any lines as
3395    the beginning of a source statement, because that information
3396    is not made available by the GCC front-end.  */
3397 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3398
3399 #ifdef DWARF2_DEBUGGING_INFO
3400 /* This location is used by calc_die_sizes() to keep track
3401    the offset of each DIE within the .debug_info section.  */
3402 static unsigned long next_die_offset;
3403 #endif
3404
3405 /* Record the root of the DIE's built for the current compilation unit.  */
3406 static GTY(()) dw_die_ref comp_unit_die;
3407
3408 #ifdef DWARF2_DEBUGGING_INFO
3409 /* We need special handling in dwarf2out_start_source_file if it is
3410    first one.  */
3411 static int is_main_source;
3412 #endif
3413
3414 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3415 static GTY(()) limbo_die_node *limbo_die_list;
3416
3417 /* Filenames referenced by this compilation unit.  */
3418 static GTY(()) varray_type file_table;
3419 static GTY(()) varray_type file_table_emitted;
3420 static GTY(()) size_t file_table_last_lookup_index;
3421
3422 /* A pointer to the base of a table of references to DIE's that describe
3423    declarations.  The table is indexed by DECL_UID() which is a unique
3424    number identifying each decl.  */
3425 static GTY((length ("decl_die_table_allocated"))) dw_die_ref *decl_die_table;
3426
3427 /* Number of elements currently allocated for the decl_die_table.  */
3428 static GTY(()) unsigned decl_die_table_allocated;
3429
3430 /* Number of elements in decl_die_table currently in use.  */
3431 static GTY(()) unsigned decl_die_table_in_use;
3432
3433 /* Size (in elements) of increments by which we may expand the
3434    decl_die_table.  */
3435 #define DECL_DIE_TABLE_INCREMENT 256
3436
3437 /* A pointer to the base of a list of references to DIE's that
3438    are uniquely identified by their tag, presence/absence of
3439    children DIE's, and list of attribute/value pairs.  */
3440 static GTY((length ("abbrev_die_table_allocated")))
3441   dw_die_ref *abbrev_die_table;
3442
3443 /* Number of elements currently allocated for abbrev_die_table.  */
3444 static GTY(()) unsigned abbrev_die_table_allocated;
3445
3446 /* Number of elements in type_die_table currently in use.  */
3447 static GTY(()) unsigned abbrev_die_table_in_use;
3448
3449 /* Size (in elements) of increments by which we may expand the
3450    abbrev_die_table.  */
3451 #define ABBREV_DIE_TABLE_INCREMENT 256
3452
3453 /* A pointer to the base of a table that contains line information
3454    for each source code line in .text in the compilation unit.  */
3455 static GTY((length ("line_info_table_allocated")))
3456      dw_line_info_ref line_info_table;
3457
3458 /* Number of elements currently allocated for line_info_table.  */
3459 static GTY(()) unsigned line_info_table_allocated;
3460
3461 /* Number of elements in line_info_table currently in use.  */
3462 static GTY(()) unsigned line_info_table_in_use;
3463
3464 /* A pointer to the base of a table that contains line information
3465    for each source code line outside of .text in the compilation unit.  */
3466 static GTY ((length ("separate_line_info_table_allocated")))
3467      dw_separate_line_info_ref separate_line_info_table;
3468
3469 /* Number of elements currently allocated for separate_line_info_table.  */
3470 static GTY(()) unsigned separate_line_info_table_allocated;
3471
3472 /* Number of elements in separate_line_info_table currently in use.  */
3473 static GTY(()) unsigned separate_line_info_table_in_use;
3474
3475 /* Size (in elements) of increments by which we may expand the
3476    line_info_table.  */
3477 #define LINE_INFO_TABLE_INCREMENT 1024
3478
3479 /* A pointer to the base of a table that contains a list of publicly
3480    accessible names.  */
3481 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3482
3483 /* Number of elements currently allocated for pubname_table.  */
3484 static GTY(()) unsigned pubname_table_allocated;
3485
3486 /* Number of elements in pubname_table currently in use.  */
3487 static GTY(()) unsigned pubname_table_in_use;
3488
3489 /* Size (in elements) of increments by which we may expand the
3490    pubname_table.  */
3491 #define PUBNAME_TABLE_INCREMENT 64
3492
3493 /* Array of dies for which we should generate .debug_arange info.  */
3494 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3495
3496 /* Number of elements currently allocated for arange_table.  */
3497 static GTY(()) unsigned arange_table_allocated;
3498
3499 /* Number of elements in arange_table currently in use.  */
3500 static GTY(()) unsigned arange_table_in_use;
3501
3502 /* Size (in elements) of increments by which we may expand the
3503    arange_table.  */
3504 #define ARANGE_TABLE_INCREMENT 64
3505
3506 /* Array of dies for which we should generate .debug_ranges info.  */
3507 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3508
3509 /* Number of elements currently allocated for ranges_table.  */
3510 static GTY(()) unsigned ranges_table_allocated;
3511
3512 /* Number of elements in ranges_table currently in use.  */
3513 static GTY(()) unsigned ranges_table_in_use;
3514
3515 /* Size (in elements) of increments by which we may expand the
3516    ranges_table.  */
3517 #define RANGES_TABLE_INCREMENT 64
3518
3519 /* Whether we have location lists that need outputting */
3520 static GTY(()) unsigned have_location_lists;
3521
3522 #ifdef DWARF2_DEBUGGING_INFO
3523 /* Record whether the function being analyzed contains inlined functions.  */
3524 static int current_function_has_inlines;
3525 #endif
3526 #if 0 && defined (MIPS_DEBUGGING_INFO)
3527 static int comp_unit_has_inlines;
3528 #endif
3529
3530 /* Number of file tables emitted in maybe_emit_file().  */
3531 static GTY(()) int emitcount = 0;
3532
3533 /* Number of internal labels generated by gen_internal_sym().  */
3534 static GTY(()) int label_num;
3535
3536 #ifdef DWARF2_DEBUGGING_INFO
3537
3538 /* Forward declarations for functions defined in this file.  */
3539
3540 static int is_pseudo_reg (rtx);
3541 static tree type_main_variant (tree);
3542 static int is_tagged_type (tree);
3543 static const char *dwarf_tag_name (unsigned);
3544 static const char *dwarf_attr_name (unsigned);
3545 static const char *dwarf_form_name (unsigned);
3546 #if 0
3547 static const char *dwarf_type_encoding_name (unsigned);
3548 #endif
3549 static tree decl_ultimate_origin (tree);
3550 static tree block_ultimate_origin (tree);
3551 static tree decl_class_context (tree);
3552 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3553 static inline enum dw_val_class AT_class (dw_attr_ref);
3554 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3555 static inline unsigned AT_flag (dw_attr_ref);
3556 static void add_AT_int (dw_die_ref, enum dwarf_attribute, long);
3557 static inline long int AT_int (dw_attr_ref);
3558 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned long);
3559 static inline unsigned long AT_unsigned (dw_attr_ref);
3560 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3561                               unsigned long);
3562 static void add_AT_float (dw_die_ref, enum dwarf_attribute, unsigned, long *);
3563 static hashval_t debug_str_do_hash (const void *);
3564 static int debug_str_eq (const void *, const void *);
3565 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3566 static inline const char *AT_string (dw_attr_ref);
3567 static int AT_string_form (dw_attr_ref);
3568 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3569 static inline dw_die_ref AT_ref (dw_attr_ref);
3570 static inline int AT_ref_external (dw_attr_ref);
3571 static inline void set_AT_ref_external (dw_attr_ref, int);
3572 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3573 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3574 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3575 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3576                              dw_loc_list_ref);
3577 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3578 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3579 static inline rtx AT_addr (dw_attr_ref);
3580 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3581 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3582 static void add_AT_offset (dw_die_ref, enum dwarf_attribute, unsigned long);
3583 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3584                                unsigned long);
3585 static inline const char *AT_lbl (dw_attr_ref);
3586 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3587 static const char *get_AT_low_pc (dw_die_ref);
3588 static const char *get_AT_hi_pc (dw_die_ref);
3589 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3590 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3591 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3592 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3593 static bool is_c_family (void);
3594 static bool is_cxx (void);
3595 static bool is_java (void);
3596 static bool is_fortran (void);
3597 static bool is_ada (void);
3598 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3599 static inline void free_die (dw_die_ref);
3600 static void remove_children (dw_die_ref);
3601 static void add_child_die (dw_die_ref, dw_die_ref);
3602 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3603 static dw_die_ref lookup_type_die (tree);
3604 static void equate_type_number_to_die (tree, dw_die_ref);
3605 static dw_die_ref lookup_decl_die (tree);
3606 static void equate_decl_number_to_die (tree, dw_die_ref);
3607 static void print_spaces (FILE *);
3608 static void print_die (dw_die_ref, FILE *);
3609 static void print_dwarf_line_table (FILE *);
3610 static void reverse_die_lists (dw_die_ref);
3611 static void reverse_all_dies (dw_die_ref);
3612 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3613 static dw_die_ref pop_compile_unit (dw_die_ref);
3614 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3615 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3616 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3617 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3618 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3619 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3620 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3621 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3622 static void compute_section_prefix (dw_die_ref);
3623 static int is_type_die (dw_die_ref);
3624 static int is_comdat_die (dw_die_ref);
3625 static int is_symbol_die (dw_die_ref);
3626 static void assign_symbol_names (dw_die_ref);
3627 static void break_out_includes (dw_die_ref);
3628 static hashval_t htab_cu_hash (const void *);
3629 static int htab_cu_eq (const void *, const void *);
3630 static void htab_cu_del (void *);
3631 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3632 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3633 static void add_sibling_attributes (dw_die_ref);
3634 static void build_abbrev_table (dw_di