OSDN Git Service

f25b18b037a2068dcfb6be0fbe499aadc26f624c
[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, 2004, 2005, 2006, 2007, 2008, 2009 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 3, 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 COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94
95 #ifdef DWARF2_DEBUGGING_INFO
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97
98 static rtx last_var_location_insn;
99 #endif
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 #ifndef DWARF2_FRAME_INFO
118 # ifdef DWARF2_DEBUGGING_INFO
119 #  define DWARF2_FRAME_INFO \
120   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
121 # else
122 #  define DWARF2_FRAME_INFO 0
123 # endif
124 #endif
125
126 /* Map register numbers held in the call frame info that gcc has
127    collected using DWARF_FRAME_REGNUM to those that should be output in
128    .debug_frame and .eh_frame.  */
129 #ifndef DWARF2_FRAME_REG_OUT
130 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
131 #endif
132
133 /* Save the result of dwarf2out_do_frame across PCH.  */
134 static GTY(()) bool saved_do_cfi_asm = 0;
135
136 /* Decide whether we want to emit frame unwind information for the current
137    translation unit.  */
138
139 int
140 dwarf2out_do_frame (void)
141 {
142   /* We want to emit correct CFA location expressions or lists, so we
143      have to return true if we're going to output debug info, even if
144      we're not going to output frame or unwind info.  */
145   return (write_symbols == DWARF2_DEBUG
146           || write_symbols == VMS_AND_DWARF2_DEBUG
147           || DWARF2_FRAME_INFO || saved_do_cfi_asm
148 #ifdef DWARF2_UNWIND_INFO
149           || (DWARF2_UNWIND_INFO
150               && (flag_unwind_tables
151                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
152 #endif
153           );
154 }
155
156 /* Decide whether to emit frame unwind via assembler directives.  */
157
158 int
159 dwarf2out_do_cfi_asm (void)
160 {
161   int enc;
162
163 #ifdef MIPS_DEBUGGING_INFO
164   return false;
165 #endif
166   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
167     return false;
168   if (saved_do_cfi_asm)
169     return true;
170   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
171     return false;
172
173   /* Make sure the personality encoding is one the assembler can support.
174      In particular, aligned addresses can't be handled.  */
175   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
176   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
177     return false;
178   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
179   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
180     return false;
181
182   saved_do_cfi_asm = true;
183   return true;
184 }
185
186 /* The size of the target's pointer type.  */
187 #ifndef PTR_SIZE
188 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
189 #endif
190
191 /* Array of RTXes referenced by the debugging information, which therefore
192    must be kept around forever.  */
193 static GTY(()) VEC(rtx,gc) *used_rtx_array;
194
195 /* A pointer to the base of a list of incomplete types which might be
196    completed at some later time.  incomplete_types_list needs to be a
197    VEC(tree,gc) because we want to tell the garbage collector about
198    it.  */
199 static GTY(()) VEC(tree,gc) *incomplete_types;
200
201 /* A pointer to the base of a table of references to declaration
202    scopes.  This table is a display which tracks the nesting
203    of declaration scopes at the current scope and containing
204    scopes.  This table is used to find the proper place to
205    define type declaration DIE's.  */
206 static GTY(()) VEC(tree,gc) *decl_scope_table;
207
208 /* Pointers to various DWARF2 sections.  */
209 static GTY(()) section *debug_info_section;
210 static GTY(()) section *debug_abbrev_section;
211 static GTY(()) section *debug_aranges_section;
212 static GTY(()) section *debug_macinfo_section;
213 static GTY(()) section *debug_line_section;
214 static GTY(()) section *debug_loc_section;
215 static GTY(()) section *debug_pubnames_section;
216 static GTY(()) section *debug_pubtypes_section;
217 static GTY(()) section *debug_str_section;
218 static GTY(()) section *debug_ranges_section;
219 static GTY(()) section *debug_frame_section;
220
221 /* Personality decl of current unit.  Used only when assembler does not support
222    personality CFI.  */
223 static GTY(()) rtx current_unit_personality;
224
225 /* How to start an assembler comment.  */
226 #ifndef ASM_COMMENT_START
227 #define ASM_COMMENT_START ";#"
228 #endif
229
230 typedef struct dw_cfi_struct *dw_cfi_ref;
231 typedef struct dw_fde_struct *dw_fde_ref;
232 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
233
234 /* Call frames are described using a sequence of Call Frame
235    Information instructions.  The register number, offset
236    and address fields are provided as possible operands;
237    their use is selected by the opcode field.  */
238
239 enum dw_cfi_oprnd_type {
240   dw_cfi_oprnd_unused,
241   dw_cfi_oprnd_reg_num,
242   dw_cfi_oprnd_offset,
243   dw_cfi_oprnd_addr,
244   dw_cfi_oprnd_loc
245 };
246
247 typedef union GTY(()) dw_cfi_oprnd_struct {
248   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
249   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
250   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
251   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
252 }
253 dw_cfi_oprnd;
254
255 typedef struct GTY(()) dw_cfi_struct {
256   dw_cfi_ref dw_cfi_next;
257   enum dwarf_call_frame_info dw_cfi_opc;
258   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
259     dw_cfi_oprnd1;
260   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
261     dw_cfi_oprnd2;
262 }
263 dw_cfi_node;
264
265 /* This is how we define the location of the CFA. We use to handle it
266    as REG + OFFSET all the time,  but now it can be more complex.
267    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
268    Instead of passing around REG and OFFSET, we pass a copy
269    of this structure.  */
270 typedef struct GTY(()) cfa_loc {
271   HOST_WIDE_INT offset;
272   HOST_WIDE_INT base_offset;
273   unsigned int reg;
274   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
275   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
276 } dw_cfa_location;
277
278 /* All call frame descriptions (FDE's) in the GCC generated DWARF
279    refer to a single Common Information Entry (CIE), defined at
280    the beginning of the .debug_frame section.  This use of a single
281    CIE obviates the need to keep track of multiple CIE's
282    in the DWARF generation routines below.  */
283
284 typedef struct GTY(()) dw_fde_struct {
285   tree decl;
286   const char *dw_fde_begin;
287   const char *dw_fde_current_label;
288   const char *dw_fde_end;
289   const char *dw_fde_hot_section_label;
290   const char *dw_fde_hot_section_end_label;
291   const char *dw_fde_unlikely_section_label;
292   const char *dw_fde_unlikely_section_end_label;
293   dw_cfi_ref dw_fde_cfi;
294   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
295   unsigned funcdef_number;
296   HOST_WIDE_INT stack_realignment;
297   /* Dynamic realign argument pointer register.  */
298   unsigned int drap_reg;
299   /* Virtual dynamic realign argument pointer register.  */
300   unsigned int vdrap_reg;
301   unsigned all_throwers_are_sibcalls : 1;
302   unsigned nothrow : 1;
303   unsigned uses_eh_lsda : 1;
304   /* Whether we did stack realign in this call frame.  */
305   unsigned stack_realign : 1;
306   /* Whether dynamic realign argument pointer register has been saved.  */
307   unsigned drap_reg_saved: 1;
308   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
309   unsigned in_std_section : 1;
310   /* True iff dw_fde_unlikely_section_label is in text_section or
311      cold_text_section.  */
312   unsigned cold_in_std_section : 1;
313   /* True iff switched sections.  */
314   unsigned dw_fde_switched_sections : 1;
315   /* True iff switching from cold to hot section.  */
316   unsigned dw_fde_switched_cold_to_hot : 1;
317 }
318 dw_fde_node;
319
320 /* Maximum size (in bytes) of an artificially generated label.  */
321 #define MAX_ARTIFICIAL_LABEL_BYTES      30
322
323 /* The size of addresses as they appear in the Dwarf 2 data.
324    Some architectures use word addresses to refer to code locations,
325    but Dwarf 2 info always uses byte addresses.  On such machines,
326    Dwarf 2 addresses need to be larger than the architecture's
327    pointers.  */
328 #ifndef DWARF2_ADDR_SIZE
329 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
330 #endif
331
332 /* The size in bytes of a DWARF field indicating an offset or length
333    relative to a debug info section, specified to be 4 bytes in the
334    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
335    as PTR_SIZE.  */
336
337 #ifndef DWARF_OFFSET_SIZE
338 #define DWARF_OFFSET_SIZE 4
339 #endif
340
341 /* According to the (draft) DWARF 3 specification, the initial length
342    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
343    bytes are 0xffffffff, followed by the length stored in the next 8
344    bytes.
345
346    However, the SGI/MIPS ABI uses an initial length which is equal to
347    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
348
349 #ifndef DWARF_INITIAL_LENGTH_SIZE
350 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
351 #endif
352
353 /* Round SIZE up to the nearest BOUNDARY.  */
354 #define DWARF_ROUND(SIZE,BOUNDARY) \
355   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
356
357 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
358 #ifndef DWARF_CIE_DATA_ALIGNMENT
359 #ifdef STACK_GROWS_DOWNWARD
360 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
361 #else
362 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
363 #endif
364 #endif
365
366 /* CIE identifier.  */
367 #if HOST_BITS_PER_WIDE_INT >= 64
368 #define DWARF_CIE_ID \
369   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
370 #else
371 #define DWARF_CIE_ID DW_CIE_ID
372 #endif
373
374 /* A pointer to the base of a table that contains frame description
375    information for each routine.  */
376 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
377
378 /* Number of elements currently allocated for fde_table.  */
379 static GTY(()) unsigned fde_table_allocated;
380
381 /* Number of elements in fde_table currently in use.  */
382 static GTY(()) unsigned fde_table_in_use;
383
384 /* Size (in elements) of increments by which we may expand the
385    fde_table.  */
386 #define FDE_TABLE_INCREMENT 256
387
388 /* Get the current fde_table entry we should use.  */
389
390 static inline dw_fde_ref
391 current_fde (void)
392 {
393   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
394 }
395
396 /* A list of call frame insns for the CIE.  */
397 static GTY(()) dw_cfi_ref cie_cfi_head;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
401    attribute that accelerates the lookup of the FDE associated
402    with the subprogram.  This variable holds the table index of the FDE
403    associated with the current function (body) definition.  */
404 static unsigned current_funcdef_fde;
405 #endif
406
407 struct GTY(()) indirect_string_node {
408   const char *str;
409   unsigned int refcount;
410   enum dwarf_form form;
411   char *label;
412 };
413
414 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
415
416 /* True if the compilation unit has location entries that reference
417    debug strings.  */
418 static GTY(()) bool debug_str_hash_forced = false;
419
420 static GTY(()) int dw2_string_counter;
421 static GTY(()) unsigned long dwarf2out_cfi_label_num;
422
423 /* True if the compilation unit places functions in more than one section.  */
424 static GTY(()) bool have_multiple_function_sections = false;
425
426 /* Whether the default text and cold text sections have been used at all.  */
427
428 static GTY(()) bool text_section_used = false;
429 static GTY(()) bool cold_text_section_used = false;
430
431 /* The default cold text section.  */
432 static GTY(()) section *cold_text_section;
433
434 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
435
436 /* Forward declarations for functions defined in this file.  */
437
438 static char *stripattributes (const char *);
439 static const char *dwarf_cfi_name (unsigned);
440 static dw_cfi_ref new_cfi (void);
441 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
442 static void add_fde_cfi (const char *, dw_cfi_ref);
443 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
444 static void lookup_cfa (dw_cfa_location *);
445 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
446 #ifdef DWARF2_UNWIND_INFO
447 static void initial_return_save (rtx);
448 #endif
449 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
450                                           HOST_WIDE_INT);
451 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
452 static void output_cfi_directive (dw_cfi_ref);
453 static void output_call_frame_info (int);
454 static void dwarf2out_note_section_used (void);
455 static void dwarf2out_stack_adjust (rtx, bool);
456 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
457 static void flush_queued_reg_saves (void);
458 static bool clobbers_queued_reg_save (const_rtx);
459 static void dwarf2out_frame_debug_expr (rtx, const char *);
460
461 /* Support for complex CFA locations.  */
462 static void output_cfa_loc (dw_cfi_ref);
463 static void output_cfa_loc_raw (dw_cfi_ref);
464 static void get_cfa_from_loc_descr (dw_cfa_location *,
465                                     struct dw_loc_descr_struct *);
466 static struct dw_loc_descr_struct *build_cfa_loc
467   (dw_cfa_location *, HOST_WIDE_INT);
468 static struct dw_loc_descr_struct *build_cfa_aligned_loc
469   (HOST_WIDE_INT, HOST_WIDE_INT);
470 static void def_cfa_1 (const char *, dw_cfa_location *);
471
472 /* How to start an assembler comment.  */
473 #ifndef ASM_COMMENT_START
474 #define ASM_COMMENT_START ";#"
475 #endif
476
477 /* Data and reference forms for relocatable data.  */
478 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
479 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
480
481 #ifndef DEBUG_FRAME_SECTION
482 #define DEBUG_FRAME_SECTION     ".debug_frame"
483 #endif
484
485 #ifndef FUNC_BEGIN_LABEL
486 #define FUNC_BEGIN_LABEL        "LFB"
487 #endif
488
489 #ifndef FUNC_END_LABEL
490 #define FUNC_END_LABEL          "LFE"
491 #endif
492
493 #ifndef FRAME_BEGIN_LABEL
494 #define FRAME_BEGIN_LABEL       "Lframe"
495 #endif
496 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
497 #define CIE_END_LABEL           "LECIE"
498 #define FDE_LABEL               "LSFDE"
499 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
500 #define FDE_END_LABEL           "LEFDE"
501 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
502 #define LINE_NUMBER_END_LABEL   "LELT"
503 #define LN_PROLOG_AS_LABEL      "LASLTP"
504 #define LN_PROLOG_END_LABEL     "LELTP"
505 #define DIE_LABEL_PREFIX        "DW"
506
507 /* The DWARF 2 CFA column which tracks the return address.  Normally this
508    is the column for PC, or the first column after all of the hard
509    registers.  */
510 #ifndef DWARF_FRAME_RETURN_COLUMN
511 #ifdef PC_REGNUM
512 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
513 #else
514 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
515 #endif
516 #endif
517
518 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
519    default, we just provide columns for all registers.  */
520 #ifndef DWARF_FRAME_REGNUM
521 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
522 #endif
523 \f
524 /* Hook used by __throw.  */
525
526 rtx
527 expand_builtin_dwarf_sp_column (void)
528 {
529   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
530   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
531 }
532
533 /* Return a pointer to a copy of the section string name S with all
534    attributes stripped off, and an asterisk prepended (for assemble_name).  */
535
536 static inline char *
537 stripattributes (const char *s)
538 {
539   char *stripped = XNEWVEC (char, strlen (s) + 2);
540   char *p = stripped;
541
542   *p++ = '*';
543
544   while (*s && *s != ',')
545     *p++ = *s++;
546
547   *p = '\0';
548   return stripped;
549 }
550
551 /* MEM is a memory reference for the register size table, each element of
552    which has mode MODE.  Initialize column C as a return address column.  */
553
554 static void
555 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
556 {
557   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
558   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
559   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
560 }
561
562 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
563
564 static inline HOST_WIDE_INT
565 div_data_align (HOST_WIDE_INT off)
566 {
567   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
568   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
569   return r;
570 }
571
572 /* Return true if we need a signed version of a given opcode
573    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
574
575 static inline bool
576 need_data_align_sf_opcode (HOST_WIDE_INT off)
577 {
578   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
579 }
580
581 /* Generate code to initialize the register size table.  */
582
583 void
584 expand_builtin_init_dwarf_reg_sizes (tree address)
585 {
586   unsigned int i;
587   enum machine_mode mode = TYPE_MODE (char_type_node);
588   rtx addr = expand_normal (address);
589   rtx mem = gen_rtx_MEM (BLKmode, addr);
590   bool wrote_return_column = false;
591
592   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
593     {
594       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
595
596       if (rnum < DWARF_FRAME_REGISTERS)
597         {
598           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
599           enum machine_mode save_mode = reg_raw_mode[i];
600           HOST_WIDE_INT size;
601
602           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
603             save_mode = choose_hard_reg_mode (i, 1, true);
604           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
605             {
606               if (save_mode == VOIDmode)
607                 continue;
608               wrote_return_column = true;
609             }
610           size = GET_MODE_SIZE (save_mode);
611           if (offset < 0)
612             continue;
613
614           emit_move_insn (adjust_address (mem, mode, offset),
615                           gen_int_mode (size, mode));
616         }
617     }
618
619   if (!wrote_return_column)
620     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
621
622 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
623   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
624 #endif
625
626   targetm.init_dwarf_reg_sizes_extra (address);
627 }
628
629 /* Convert a DWARF call frame info. operation to its string name */
630
631 static const char *
632 dwarf_cfi_name (unsigned int cfi_opc)
633 {
634   switch (cfi_opc)
635     {
636     case DW_CFA_advance_loc:
637       return "DW_CFA_advance_loc";
638     case DW_CFA_offset:
639       return "DW_CFA_offset";
640     case DW_CFA_restore:
641       return "DW_CFA_restore";
642     case DW_CFA_nop:
643       return "DW_CFA_nop";
644     case DW_CFA_set_loc:
645       return "DW_CFA_set_loc";
646     case DW_CFA_advance_loc1:
647       return "DW_CFA_advance_loc1";
648     case DW_CFA_advance_loc2:
649       return "DW_CFA_advance_loc2";
650     case DW_CFA_advance_loc4:
651       return "DW_CFA_advance_loc4";
652     case DW_CFA_offset_extended:
653       return "DW_CFA_offset_extended";
654     case DW_CFA_restore_extended:
655       return "DW_CFA_restore_extended";
656     case DW_CFA_undefined:
657       return "DW_CFA_undefined";
658     case DW_CFA_same_value:
659       return "DW_CFA_same_value";
660     case DW_CFA_register:
661       return "DW_CFA_register";
662     case DW_CFA_remember_state:
663       return "DW_CFA_remember_state";
664     case DW_CFA_restore_state:
665       return "DW_CFA_restore_state";
666     case DW_CFA_def_cfa:
667       return "DW_CFA_def_cfa";
668     case DW_CFA_def_cfa_register:
669       return "DW_CFA_def_cfa_register";
670     case DW_CFA_def_cfa_offset:
671       return "DW_CFA_def_cfa_offset";
672
673     /* DWARF 3 */
674     case DW_CFA_def_cfa_expression:
675       return "DW_CFA_def_cfa_expression";
676     case DW_CFA_expression:
677       return "DW_CFA_expression";
678     case DW_CFA_offset_extended_sf:
679       return "DW_CFA_offset_extended_sf";
680     case DW_CFA_def_cfa_sf:
681       return "DW_CFA_def_cfa_sf";
682     case DW_CFA_def_cfa_offset_sf:
683       return "DW_CFA_def_cfa_offset_sf";
684
685     /* SGI/MIPS specific */
686     case DW_CFA_MIPS_advance_loc8:
687       return "DW_CFA_MIPS_advance_loc8";
688
689     /* GNU extensions */
690     case DW_CFA_GNU_window_save:
691       return "DW_CFA_GNU_window_save";
692     case DW_CFA_GNU_args_size:
693       return "DW_CFA_GNU_args_size";
694     case DW_CFA_GNU_negative_offset_extended:
695       return "DW_CFA_GNU_negative_offset_extended";
696
697     default:
698       return "DW_CFA_<unknown>";
699     }
700 }
701
702 /* Return a pointer to a newly allocated Call Frame Instruction.  */
703
704 static inline dw_cfi_ref
705 new_cfi (void)
706 {
707   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
708
709   cfi->dw_cfi_next = NULL;
710   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
711   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
712
713   return cfi;
714 }
715
716 /* Add a Call Frame Instruction to list of instructions.  */
717
718 static inline void
719 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
720 {
721   dw_cfi_ref *p;
722   dw_fde_ref fde = current_fde ();
723
724   /* When DRAP is used, CFA is defined with an expression.  Redefine
725      CFA may lead to a different CFA value.   */
726   /* ??? Of course, this heuristic fails when we're annotating epilogues,
727      because of course we'll always want to redefine the CFA back to the
728      stack pointer on the way out.  Where should we move this check?  */
729   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
730     switch (cfi->dw_cfi_opc)
731       {
732         case DW_CFA_def_cfa_register:
733         case DW_CFA_def_cfa_offset:
734         case DW_CFA_def_cfa_offset_sf:
735         case DW_CFA_def_cfa:
736         case DW_CFA_def_cfa_sf:
737           gcc_unreachable ();
738
739         default:
740           break;
741       }
742
743   /* Find the end of the chain.  */
744   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
745     ;
746
747   *p = cfi;
748 }
749
750 /* Generate a new label for the CFI info to refer to.  FORCE is true
751    if a label needs to be output even when using .cfi_* directives.  */
752
753 char *
754 dwarf2out_cfi_label (bool force)
755 {
756   static char label[20];
757
758   if (!force && dwarf2out_do_cfi_asm ())
759     {
760       /* In this case, we will be emitting the asm directive instead of
761          the label, so just return a placeholder to keep the rest of the
762          interfaces happy.  */
763       strcpy (label, "<do not output>");
764     }
765   else
766     {
767       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
768       ASM_OUTPUT_LABEL (asm_out_file, label);
769     }
770
771   return label;
772 }
773
774 /* True if remember_state should be emitted before following CFI directive.  */
775 static bool emit_cfa_remember;
776
777 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
778    or to the CIE if LABEL is NULL.  */
779
780 static void
781 add_fde_cfi (const char *label, dw_cfi_ref cfi)
782 {
783   dw_cfi_ref *list_head;
784
785   if (emit_cfa_remember)
786     {
787       dw_cfi_ref cfi_remember;
788
789       /* Emit the state save.  */
790       emit_cfa_remember = false;
791       cfi_remember = new_cfi (); 
792       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
793       add_fde_cfi (label, cfi_remember);
794     }
795
796   list_head = &cie_cfi_head;
797
798   if (dwarf2out_do_cfi_asm ())
799     {
800       if (label)
801         {
802           dw_fde_ref fde = current_fde ();
803
804           gcc_assert (fde != NULL);
805
806           /* We still have to add the cfi to the list so that lookup_cfa
807              works later on.  When -g2 and above we even need to force
808              emitting of CFI labels and add to list a DW_CFA_set_loc for
809              convert_cfa_to_fb_loc_list purposes.  If we're generating
810              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
811              convert_cfa_to_fb_loc_list.  */
812           if (dwarf_version == 2
813               && debug_info_level > DINFO_LEVEL_TERSE
814               && (write_symbols == DWARF2_DEBUG
815                   || write_symbols == VMS_AND_DWARF2_DEBUG))
816             {
817               switch (cfi->dw_cfi_opc)
818                 {
819                 case DW_CFA_def_cfa_offset:
820                 case DW_CFA_def_cfa_offset_sf:
821                 case DW_CFA_def_cfa_register:
822                 case DW_CFA_def_cfa:
823                 case DW_CFA_def_cfa_sf:
824                 case DW_CFA_def_cfa_expression:
825                 case DW_CFA_restore_state:
826                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
827                     label = dwarf2out_cfi_label (true);
828
829                   if (fde->dw_fde_current_label == NULL
830                       || strcmp (label, fde->dw_fde_current_label) != 0)
831                     {
832                       dw_cfi_ref xcfi;
833
834                       label = xstrdup (label);
835
836                       /* Set the location counter to the new label.  */
837                       xcfi = new_cfi ();
838                       /* It doesn't metter whether DW_CFA_set_loc
839                          or DW_CFA_advance_loc4 is added here, those aren't
840                          emitted into assembly, only looked up by
841                          convert_cfa_to_fb_loc_list.  */
842                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
843                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
844                       add_cfi (&fde->dw_fde_cfi, xcfi);
845                       fde->dw_fde_current_label = label;
846                     }
847                   break;
848                 default:
849                   break;
850                 }
851             }
852
853           output_cfi_directive (cfi);
854
855           list_head = &fde->dw_fde_cfi;
856         }
857       /* ??? If this is a CFI for the CIE, we don't emit.  This
858          assumes that the standard CIE contents that the assembler
859          uses matches the standard CIE contents that the compiler
860          uses.  This is probably a bad assumption.  I'm not quite
861          sure how to address this for now.  */
862     }
863   else if (label)
864     {
865       dw_fde_ref fde = current_fde ();
866
867       gcc_assert (fde != NULL);
868
869       if (*label == 0)
870         label = dwarf2out_cfi_label (false);
871
872       if (fde->dw_fde_current_label == NULL
873           || strcmp (label, fde->dw_fde_current_label) != 0)
874         {
875           dw_cfi_ref xcfi;
876
877           label = xstrdup (label);
878
879           /* Set the location counter to the new label.  */
880           xcfi = new_cfi ();
881           /* If we have a current label, advance from there, otherwise
882              set the location directly using set_loc.  */
883           xcfi->dw_cfi_opc = fde->dw_fde_current_label
884                              ? DW_CFA_advance_loc4
885                              : DW_CFA_set_loc;
886           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
887           add_cfi (&fde->dw_fde_cfi, xcfi);
888
889           fde->dw_fde_current_label = label;
890         }
891
892       list_head = &fde->dw_fde_cfi;
893     }
894
895   add_cfi (list_head, cfi);
896 }
897
898 /* Subroutine of lookup_cfa.  */
899
900 static void
901 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
902 {
903   switch (cfi->dw_cfi_opc)
904     {
905     case DW_CFA_def_cfa_offset:
906     case DW_CFA_def_cfa_offset_sf:
907       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
908       break;
909     case DW_CFA_def_cfa_register:
910       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
911       break;
912     case DW_CFA_def_cfa:
913     case DW_CFA_def_cfa_sf:
914       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
915       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
916       break;
917     case DW_CFA_def_cfa_expression:
918       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
919       break;
920
921     case DW_CFA_remember_state:
922       gcc_assert (!remember->in_use);
923       *remember = *loc;
924       remember->in_use = 1;
925       break;
926     case DW_CFA_restore_state:
927       gcc_assert (remember->in_use);
928       *loc = *remember;
929       remember->in_use = 0;
930       break;
931
932     default:
933       break;
934     }
935 }
936
937 /* Find the previous value for the CFA.  */
938
939 static void
940 lookup_cfa (dw_cfa_location *loc)
941 {
942   dw_cfi_ref cfi;
943   dw_fde_ref fde;
944   dw_cfa_location remember;
945
946   memset (loc, 0, sizeof (*loc));
947   loc->reg = INVALID_REGNUM;
948   remember = *loc;
949
950   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
951     lookup_cfa_1 (cfi, loc, &remember);
952
953   fde = current_fde ();
954   if (fde)
955     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
956       lookup_cfa_1 (cfi, loc, &remember);
957 }
958
959 /* The current rule for calculating the DWARF2 canonical frame address.  */
960 static dw_cfa_location cfa;
961
962 /* The register used for saving registers to the stack, and its offset
963    from the CFA.  */
964 static dw_cfa_location cfa_store;
965
966 /* The current save location around an epilogue.  */
967 static dw_cfa_location cfa_remember;
968
969 /* The running total of the size of arguments pushed onto the stack.  */
970 static HOST_WIDE_INT args_size;
971
972 /* The last args_size we actually output.  */
973 static HOST_WIDE_INT old_args_size;
974
975 /* Entry point to update the canonical frame address (CFA).
976    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
977    calculated from REG+OFFSET.  */
978
979 void
980 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
981 {
982   dw_cfa_location loc;
983   loc.indirect = 0;
984   loc.base_offset = 0;
985   loc.reg = reg;
986   loc.offset = offset;
987   def_cfa_1 (label, &loc);
988 }
989
990 /* Determine if two dw_cfa_location structures define the same data.  */
991
992 static bool
993 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
994 {
995   return (loc1->reg == loc2->reg
996           && loc1->offset == loc2->offset
997           && loc1->indirect == loc2->indirect
998           && (loc1->indirect == 0
999               || loc1->base_offset == loc2->base_offset));
1000 }
1001
1002 /* This routine does the actual work.  The CFA is now calculated from
1003    the dw_cfa_location structure.  */
1004
1005 static void
1006 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1007 {
1008   dw_cfi_ref cfi;
1009   dw_cfa_location old_cfa, loc;
1010
1011   cfa = *loc_p;
1012   loc = *loc_p;
1013
1014   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1015     cfa_store.offset = loc.offset;
1016
1017   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1018   lookup_cfa (&old_cfa);
1019
1020   /* If nothing changed, no need to issue any call frame instructions.  */
1021   if (cfa_equal_p (&loc, &old_cfa))
1022     return;
1023
1024   cfi = new_cfi ();
1025
1026   if (loc.reg == old_cfa.reg && !loc.indirect)
1027     {
1028       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1029          the CFA register did not change but the offset did.  The data 
1030          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1031          in the assembler via the .cfi_def_cfa_offset directive.  */
1032       if (loc.offset < 0)
1033         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1034       else
1035         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1036       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1037     }
1038
1039 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1040   else if (loc.offset == old_cfa.offset
1041            && old_cfa.reg != INVALID_REGNUM
1042            && !loc.indirect)
1043     {
1044       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1045          indicating the CFA register has changed to <register> but the
1046          offset has not changed.  */
1047       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1048       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1049     }
1050 #endif
1051
1052   else if (loc.indirect == 0)
1053     {
1054       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1055          indicating the CFA register has changed to <register> with
1056          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1057          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1058          directive.  */
1059       if (loc.offset < 0)
1060         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1061       else
1062         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1063       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1064       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1065     }
1066   else
1067     {
1068       /* Construct a DW_CFA_def_cfa_expression instruction to
1069          calculate the CFA using a full location expression since no
1070          register-offset pair is available.  */
1071       struct dw_loc_descr_struct *loc_list;
1072
1073       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1074       loc_list = build_cfa_loc (&loc, 0);
1075       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1076     }
1077
1078   add_fde_cfi (label, cfi);
1079 }
1080
1081 /* Add the CFI for saving a register.  REG is the CFA column number.
1082    LABEL is passed to add_fde_cfi.
1083    If SREG is -1, the register is saved at OFFSET from the CFA;
1084    otherwise it is saved in SREG.  */
1085
1086 static void
1087 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1088 {
1089   dw_cfi_ref cfi = new_cfi ();
1090   dw_fde_ref fde = current_fde ();
1091
1092   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1093
1094   /* When stack is aligned, store REG using DW_CFA_expression with
1095      FP.  */
1096   if (fde
1097       && fde->stack_realign
1098       && sreg == INVALID_REGNUM)
1099     {
1100       cfi->dw_cfi_opc = DW_CFA_expression;
1101       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1102       cfi->dw_cfi_oprnd1.dw_cfi_loc
1103         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1104     }
1105   else if (sreg == INVALID_REGNUM)
1106     {
1107       if (need_data_align_sf_opcode (offset))
1108         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1109       else if (reg & ~0x3f)
1110         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1111       else
1112         cfi->dw_cfi_opc = DW_CFA_offset;
1113       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1114     }
1115   else if (sreg == reg)
1116     cfi->dw_cfi_opc = DW_CFA_same_value;
1117   else
1118     {
1119       cfi->dw_cfi_opc = DW_CFA_register;
1120       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1121     }
1122
1123   add_fde_cfi (label, cfi);
1124 }
1125
1126 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1127    This CFI tells the unwinder that it needs to restore the window registers
1128    from the previous frame's window save area.
1129
1130    ??? Perhaps we should note in the CIE where windows are saved (instead of
1131    assuming 0(cfa)) and what registers are in the window.  */
1132
1133 void
1134 dwarf2out_window_save (const char *label)
1135 {
1136   dw_cfi_ref cfi = new_cfi ();
1137
1138   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1139   add_fde_cfi (label, cfi);
1140 }
1141
1142 /* Add a CFI to update the running total of the size of arguments
1143    pushed onto the stack.  */
1144
1145 void
1146 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1147 {
1148   dw_cfi_ref cfi;
1149
1150   if (size == old_args_size)
1151     return;
1152
1153   old_args_size = size;
1154
1155   cfi = new_cfi ();
1156   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1157   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1158   add_fde_cfi (label, cfi);
1159 }
1160
1161 /* Entry point for saving a register to the stack.  REG is the GCC register
1162    number.  LABEL and OFFSET are passed to reg_save.  */
1163
1164 void
1165 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1166 {
1167   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1168 }
1169
1170 /* Entry point for saving the return address in the stack.
1171    LABEL and OFFSET are passed to reg_save.  */
1172
1173 void
1174 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1175 {
1176   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1177 }
1178
1179 /* Entry point for saving the return address in a register.
1180    LABEL and SREG are passed to reg_save.  */
1181
1182 void
1183 dwarf2out_return_reg (const char *label, unsigned int sreg)
1184 {
1185   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1186 }
1187
1188 #ifdef DWARF2_UNWIND_INFO
1189 /* Record the initial position of the return address.  RTL is
1190    INCOMING_RETURN_ADDR_RTX.  */
1191
1192 static void
1193 initial_return_save (rtx rtl)
1194 {
1195   unsigned int reg = INVALID_REGNUM;
1196   HOST_WIDE_INT offset = 0;
1197
1198   switch (GET_CODE (rtl))
1199     {
1200     case REG:
1201       /* RA is in a register.  */
1202       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1203       break;
1204
1205     case MEM:
1206       /* RA is on the stack.  */
1207       rtl = XEXP (rtl, 0);
1208       switch (GET_CODE (rtl))
1209         {
1210         case REG:
1211           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1212           offset = 0;
1213           break;
1214
1215         case PLUS:
1216           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1217           offset = INTVAL (XEXP (rtl, 1));
1218           break;
1219
1220         case MINUS:
1221           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1222           offset = -INTVAL (XEXP (rtl, 1));
1223           break;
1224
1225         default:
1226           gcc_unreachable ();
1227         }
1228
1229       break;
1230
1231     case PLUS:
1232       /* The return address is at some offset from any value we can
1233          actually load.  For instance, on the SPARC it is in %i7+8. Just
1234          ignore the offset for now; it doesn't matter for unwinding frames.  */
1235       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1236       initial_return_save (XEXP (rtl, 0));
1237       return;
1238
1239     default:
1240       gcc_unreachable ();
1241     }
1242
1243   if (reg != DWARF_FRAME_RETURN_COLUMN)
1244     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1245 }
1246 #endif
1247
1248 /* Given a SET, calculate the amount of stack adjustment it
1249    contains.  */
1250
1251 static HOST_WIDE_INT
1252 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1253                      HOST_WIDE_INT cur_offset)
1254 {
1255   const_rtx src = SET_SRC (pattern);
1256   const_rtx dest = SET_DEST (pattern);
1257   HOST_WIDE_INT offset = 0;
1258   enum rtx_code code;
1259
1260   if (dest == stack_pointer_rtx)
1261     {
1262       code = GET_CODE (src);
1263
1264       /* Assume (set (reg sp) (reg whatever)) sets args_size
1265          level to 0.  */
1266       if (code == REG && src != stack_pointer_rtx)
1267         {
1268           offset = -cur_args_size;
1269 #ifndef STACK_GROWS_DOWNWARD
1270           offset = -offset;
1271 #endif
1272           return offset - cur_offset;
1273         }
1274
1275       if (! (code == PLUS || code == MINUS)
1276           || XEXP (src, 0) != stack_pointer_rtx
1277           || !CONST_INT_P (XEXP (src, 1)))
1278         return 0;
1279
1280       /* (set (reg sp) (plus (reg sp) (const_int))) */
1281       offset = INTVAL (XEXP (src, 1));
1282       if (code == PLUS)
1283         offset = -offset;
1284       return offset;
1285     }
1286
1287   if (MEM_P (src) && !MEM_P (dest))
1288     dest = src;
1289   if (MEM_P (dest))
1290     {
1291       /* (set (mem (pre_dec (reg sp))) (foo)) */
1292       src = XEXP (dest, 0);
1293       code = GET_CODE (src);
1294
1295       switch (code)
1296         {
1297         case PRE_MODIFY:
1298         case POST_MODIFY:
1299           if (XEXP (src, 0) == stack_pointer_rtx)
1300             {
1301               rtx val = XEXP (XEXP (src, 1), 1);
1302               /* We handle only adjustments by constant amount.  */
1303               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1304                           && CONST_INT_P (val));
1305               offset = -INTVAL (val);
1306               break;
1307             }
1308           return 0;
1309
1310         case PRE_DEC:
1311         case POST_DEC:
1312           if (XEXP (src, 0) == stack_pointer_rtx)
1313             {
1314               offset = GET_MODE_SIZE (GET_MODE (dest));
1315               break;
1316             }
1317           return 0;
1318
1319         case PRE_INC:
1320         case POST_INC:
1321           if (XEXP (src, 0) == stack_pointer_rtx)
1322             {
1323               offset = -GET_MODE_SIZE (GET_MODE (dest));
1324               break;
1325             }
1326           return 0;
1327
1328         default:
1329           return 0;
1330         }
1331     }
1332   else
1333     return 0;
1334
1335   return offset;
1336 }
1337
1338 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1339    indexed by INSN_UID.  */
1340
1341 static HOST_WIDE_INT *barrier_args_size;
1342
1343 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1344
1345 static HOST_WIDE_INT
1346 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1347                              VEC (rtx, heap) **next)
1348 {
1349   HOST_WIDE_INT offset = 0;
1350   int i;
1351
1352   if (! RTX_FRAME_RELATED_P (insn))
1353     {
1354       if (prologue_epilogue_contains (insn))
1355         /* Nothing */;
1356       else if (GET_CODE (PATTERN (insn)) == SET)
1357         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1358       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1359                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1360         {
1361           /* There may be stack adjustments inside compound insns.  Search
1362              for them.  */
1363           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1364             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1365               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1366                                              cur_args_size, offset);
1367         }
1368     }
1369   else
1370     {
1371       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1372
1373       if (expr)
1374         {
1375           expr = XEXP (expr, 0);
1376           if (GET_CODE (expr) == PARALLEL
1377               || GET_CODE (expr) == SEQUENCE)
1378             for (i = 1; i < XVECLEN (expr, 0); i++)
1379               {
1380                 rtx elem = XVECEXP (expr, 0, i);
1381
1382                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1383                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1384               }
1385         }
1386     }
1387
1388 #ifndef STACK_GROWS_DOWNWARD
1389   offset = -offset;
1390 #endif
1391
1392   cur_args_size += offset;
1393   if (cur_args_size < 0)
1394     cur_args_size = 0;
1395
1396   if (JUMP_P (insn))
1397     {
1398       rtx dest = JUMP_LABEL (insn);
1399
1400       if (dest)
1401         {
1402           if (barrier_args_size [INSN_UID (dest)] < 0)
1403             {
1404               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1405               VEC_safe_push (rtx, heap, *next, dest);
1406             }
1407         }
1408     }
1409
1410   return cur_args_size;
1411 }
1412
1413 /* Walk the whole function and compute args_size on BARRIERs.  */
1414
1415 static void
1416 compute_barrier_args_size (void)
1417 {
1418   int max_uid = get_max_uid (), i;
1419   rtx insn;
1420   VEC (rtx, heap) *worklist, *next, *tmp;
1421
1422   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1423   for (i = 0; i < max_uid; i++)
1424     barrier_args_size[i] = -1;
1425
1426   worklist = VEC_alloc (rtx, heap, 20);
1427   next = VEC_alloc (rtx, heap, 20);
1428   insn = get_insns ();
1429   barrier_args_size[INSN_UID (insn)] = 0;
1430   VEC_quick_push (rtx, worklist, insn);
1431   for (;;)
1432     {
1433       while (!VEC_empty (rtx, worklist))
1434         {
1435           rtx prev, body, first_insn;
1436           HOST_WIDE_INT cur_args_size;
1437
1438           first_insn = insn = VEC_pop (rtx, worklist);
1439           cur_args_size = barrier_args_size[INSN_UID (insn)];
1440           prev = prev_nonnote_insn (insn);
1441           if (prev && BARRIER_P (prev))
1442             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1443
1444           for (; insn; insn = NEXT_INSN (insn))
1445             {
1446               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1447                 continue;
1448               if (BARRIER_P (insn))
1449                 break;
1450
1451               if (LABEL_P (insn))
1452                 {
1453                   if (insn == first_insn)
1454                     continue;
1455                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1456                     {
1457                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1458                       continue;
1459                     }
1460                   else
1461                     {
1462                       /* The insns starting with this label have been
1463                          already scanned or are in the worklist.  */
1464                       break;
1465                     }
1466                 }
1467
1468               body = PATTERN (insn);
1469               if (GET_CODE (body) == SEQUENCE)
1470                 {
1471                   HOST_WIDE_INT dest_args_size = cur_args_size;
1472                   for (i = 1; i < XVECLEN (body, 0); i++)
1473                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1474                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1475                       dest_args_size
1476                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1477                                                        dest_args_size, &next);
1478                     else
1479                       cur_args_size
1480                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1481                                                        cur_args_size, &next);
1482
1483                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1484                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1485                                                  dest_args_size, &next);
1486                   else
1487                     cur_args_size
1488                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1489                                                      cur_args_size, &next);
1490                 }
1491               else
1492                 cur_args_size
1493                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1494             }
1495         }
1496
1497       if (VEC_empty (rtx, next))
1498         break;
1499
1500       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1501       tmp = next;
1502       next = worklist;
1503       worklist = tmp;
1504       VEC_truncate (rtx, next, 0);
1505     }
1506
1507   VEC_free (rtx, heap, worklist);
1508   VEC_free (rtx, heap, next);
1509 }
1510
1511
1512 /* Check INSN to see if it looks like a push or a stack adjustment, and
1513    make a note of it if it does.  EH uses this information to find out how
1514    much extra space it needs to pop off the stack.  */
1515
1516 static void
1517 dwarf2out_stack_adjust (rtx insn, bool after_p)
1518 {
1519   HOST_WIDE_INT offset;
1520   const char *label;
1521   int i;
1522
1523   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1524      with this function.  Proper support would require all frame-related
1525      insns to be marked, and to be able to handle saving state around
1526      epilogues textually in the middle of the function.  */
1527   if (prologue_epilogue_contains (insn))
1528     return;
1529
1530   /* If INSN is an instruction from target of an annulled branch, the
1531      effects are for the target only and so current argument size
1532      shouldn't change at all.  */
1533   if (final_sequence
1534       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1535       && INSN_FROM_TARGET_P (insn))
1536     return;
1537
1538   /* If only calls can throw, and we have a frame pointer,
1539      save up adjustments until we see the CALL_INSN.  */
1540   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1541     {
1542       if (CALL_P (insn) && !after_p)
1543         {
1544           /* Extract the size of the args from the CALL rtx itself.  */
1545           insn = PATTERN (insn);
1546           if (GET_CODE (insn) == PARALLEL)
1547             insn = XVECEXP (insn, 0, 0);
1548           if (GET_CODE (insn) == SET)
1549             insn = SET_SRC (insn);
1550           gcc_assert (GET_CODE (insn) == CALL);
1551           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1552         }
1553       return;
1554     }
1555
1556   if (CALL_P (insn) && !after_p)
1557     {
1558       if (!flag_asynchronous_unwind_tables)
1559         dwarf2out_args_size ("", args_size);
1560       return;
1561     }
1562   else if (BARRIER_P (insn))
1563     {
1564       /* Don't call compute_barrier_args_size () if the only
1565          BARRIER is at the end of function.  */
1566       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1567         compute_barrier_args_size ();
1568       if (barrier_args_size == NULL)
1569         offset = 0;
1570       else
1571         {
1572           offset = barrier_args_size[INSN_UID (insn)];
1573           if (offset < 0)
1574             offset = 0;
1575         }
1576
1577       offset -= args_size;
1578 #ifndef STACK_GROWS_DOWNWARD
1579       offset = -offset;
1580 #endif
1581     }
1582   else if (GET_CODE (PATTERN (insn)) == SET)
1583     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1584   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1585            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1586     {
1587       /* There may be stack adjustments inside compound insns.  Search
1588          for them.  */
1589       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1590         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1591           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1592                                          args_size, offset);
1593     }
1594   else
1595     return;
1596
1597   if (offset == 0)
1598     return;
1599
1600   label = dwarf2out_cfi_label (false);
1601   dwarf2out_args_size_adjust (offset, label);
1602 }
1603
1604 /* Adjust args_size based on stack adjustment OFFSET.  */
1605
1606 static void
1607 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1608 {
1609   if (cfa.reg == STACK_POINTER_REGNUM)
1610     cfa.offset += offset;
1611
1612   if (cfa_store.reg == STACK_POINTER_REGNUM)
1613     cfa_store.offset += offset;
1614
1615 #ifndef STACK_GROWS_DOWNWARD
1616   offset = -offset;
1617 #endif
1618
1619   args_size += offset;
1620   if (args_size < 0)
1621     args_size = 0;
1622
1623   def_cfa_1 (label, &cfa);
1624   if (flag_asynchronous_unwind_tables)
1625     dwarf2out_args_size (label, args_size);
1626 }
1627
1628 #endif
1629
1630 /* We delay emitting a register save until either (a) we reach the end
1631    of the prologue or (b) the register is clobbered.  This clusters
1632    register saves so that there are fewer pc advances.  */
1633
1634 struct GTY(()) queued_reg_save {
1635   struct queued_reg_save *next;
1636   rtx reg;
1637   HOST_WIDE_INT cfa_offset;
1638   rtx saved_reg;
1639 };
1640
1641 static GTY(()) struct queued_reg_save *queued_reg_saves;
1642
1643 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1644 struct GTY(()) reg_saved_in_data {
1645   rtx orig_reg;
1646   rtx saved_in_reg;
1647 };
1648
1649 /* A list of registers saved in other registers.
1650    The list intentionally has a small maximum capacity of 4; if your
1651    port needs more than that, you might consider implementing a
1652    more efficient data structure.  */
1653 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1654 static GTY(()) size_t num_regs_saved_in_regs;
1655
1656 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1657 static const char *last_reg_save_label;
1658
1659 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1660    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1661
1662 static void
1663 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1664 {
1665   struct queued_reg_save *q;
1666
1667   /* Duplicates waste space, but it's also necessary to remove them
1668      for correctness, since the queue gets output in reverse
1669      order.  */
1670   for (q = queued_reg_saves; q != NULL; q = q->next)
1671     if (REGNO (q->reg) == REGNO (reg))
1672       break;
1673
1674   if (q == NULL)
1675     {
1676       q = GGC_NEW (struct queued_reg_save);
1677       q->next = queued_reg_saves;
1678       queued_reg_saves = q;
1679     }
1680
1681   q->reg = reg;
1682   q->cfa_offset = offset;
1683   q->saved_reg = sreg;
1684
1685   last_reg_save_label = label;
1686 }
1687
1688 /* Output all the entries in QUEUED_REG_SAVES.  */
1689
1690 static void
1691 flush_queued_reg_saves (void)
1692 {
1693   struct queued_reg_save *q;
1694
1695   for (q = queued_reg_saves; q; q = q->next)
1696     {
1697       size_t i;
1698       unsigned int reg, sreg;
1699
1700       for (i = 0; i < num_regs_saved_in_regs; i++)
1701         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1702           break;
1703       if (q->saved_reg && i == num_regs_saved_in_regs)
1704         {
1705           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1706           num_regs_saved_in_regs++;
1707         }
1708       if (i != num_regs_saved_in_regs)
1709         {
1710           regs_saved_in_regs[i].orig_reg = q->reg;
1711           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1712         }
1713
1714       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1715       if (q->saved_reg)
1716         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1717       else
1718         sreg = INVALID_REGNUM;
1719       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1720     }
1721
1722   queued_reg_saves = NULL;
1723   last_reg_save_label = NULL;
1724 }
1725
1726 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1727    location for?  Or, does it clobber a register which we've previously
1728    said that some other register is saved in, and for which we now
1729    have a new location for?  */
1730
1731 static bool
1732 clobbers_queued_reg_save (const_rtx insn)
1733 {
1734   struct queued_reg_save *q;
1735
1736   for (q = queued_reg_saves; q; q = q->next)
1737     {
1738       size_t i;
1739       if (modified_in_p (q->reg, insn))
1740         return true;
1741       for (i = 0; i < num_regs_saved_in_regs; i++)
1742         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1743             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1744           return true;
1745     }
1746
1747   return false;
1748 }
1749
1750 /* Entry point for saving the first register into the second.  */
1751
1752 void
1753 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1754 {
1755   size_t i;
1756   unsigned int regno, sregno;
1757
1758   for (i = 0; i < num_regs_saved_in_regs; i++)
1759     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1760       break;
1761   if (i == num_regs_saved_in_regs)
1762     {
1763       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1764       num_regs_saved_in_regs++;
1765     }
1766   regs_saved_in_regs[i].orig_reg = reg;
1767   regs_saved_in_regs[i].saved_in_reg = sreg;
1768
1769   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1770   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1771   reg_save (label, regno, sregno, 0);
1772 }
1773
1774 /* What register, if any, is currently saved in REG?  */
1775
1776 static rtx
1777 reg_saved_in (rtx reg)
1778 {
1779   unsigned int regn = REGNO (reg);
1780   size_t i;
1781   struct queued_reg_save *q;
1782
1783   for (q = queued_reg_saves; q; q = q->next)
1784     if (q->saved_reg && regn == REGNO (q->saved_reg))
1785       return q->reg;
1786
1787   for (i = 0; i < num_regs_saved_in_regs; i++)
1788     if (regs_saved_in_regs[i].saved_in_reg
1789         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1790       return regs_saved_in_regs[i].orig_reg;
1791
1792   return NULL_RTX;
1793 }
1794
1795
1796 /* A temporary register holding an integral value used in adjusting SP
1797    or setting up the store_reg.  The "offset" field holds the integer
1798    value, not an offset.  */
1799 static dw_cfa_location cfa_temp;
1800
1801 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1802
1803 static void
1804 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1805 {
1806   memset (&cfa, 0, sizeof (cfa));
1807
1808   switch (GET_CODE (pat))
1809     {
1810     case PLUS:
1811       cfa.reg = REGNO (XEXP (pat, 0));
1812       cfa.offset = INTVAL (XEXP (pat, 1));
1813       break;
1814
1815     case REG:
1816       cfa.reg = REGNO (pat);
1817       break;
1818
1819     default:
1820       /* Recurse and define an expression.  */
1821       gcc_unreachable ();
1822     }
1823
1824   def_cfa_1 (label, &cfa);
1825 }
1826
1827 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1828
1829 static void
1830 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1831 {
1832   rtx src, dest;
1833
1834   gcc_assert (GET_CODE (pat) == SET);
1835   dest = XEXP (pat, 0);
1836   src = XEXP (pat, 1);
1837
1838   switch (GET_CODE (src))
1839     {
1840     case PLUS:
1841       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1842       cfa.offset -= INTVAL (XEXP (src, 1));
1843       break;
1844
1845     case REG:
1846         break;
1847
1848     default:
1849         gcc_unreachable ();
1850     }
1851
1852   cfa.reg = REGNO (dest);
1853   gcc_assert (cfa.indirect == 0);
1854
1855   def_cfa_1 (label, &cfa);
1856 }
1857
1858 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1859
1860 static void
1861 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1862 {
1863   HOST_WIDE_INT offset;
1864   rtx src, addr, span;
1865
1866   src = XEXP (set, 1);
1867   addr = XEXP (set, 0);
1868   gcc_assert (MEM_P (addr));
1869   addr = XEXP (addr, 0);
1870   
1871   /* As documented, only consider extremely simple addresses.  */
1872   switch (GET_CODE (addr))
1873     {
1874     case REG:
1875       gcc_assert (REGNO (addr) == cfa.reg);
1876       offset = -cfa.offset;
1877       break;
1878     case PLUS:
1879       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1880       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1881       break;
1882     default:
1883       gcc_unreachable ();
1884     }
1885
1886   span = targetm.dwarf_register_span (src);
1887
1888   /* ??? We'd like to use queue_reg_save, but we need to come up with
1889      a different flushing heuristic for epilogues.  */
1890   if (!span)
1891     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1892   else
1893     {
1894       /* We have a PARALLEL describing where the contents of SRC live.
1895          Queue register saves for each piece of the PARALLEL.  */
1896       int par_index;
1897       int limit;
1898       HOST_WIDE_INT span_offset = offset;
1899
1900       gcc_assert (GET_CODE (span) == PARALLEL);
1901
1902       limit = XVECLEN (span, 0);
1903       for (par_index = 0; par_index < limit; par_index++)
1904         {
1905           rtx elem = XVECEXP (span, 0, par_index);
1906
1907           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1908                     INVALID_REGNUM, span_offset);
1909           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1910         }
1911     }
1912 }
1913
1914 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1915
1916 static void
1917 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1918 {
1919   rtx src, dest;
1920   unsigned sregno, dregno;
1921
1922   src = XEXP (set, 1);
1923   dest = XEXP (set, 0);
1924
1925   if (src == pc_rtx)
1926     sregno = DWARF_FRAME_RETURN_COLUMN;
1927   else
1928     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1929
1930   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1931
1932   /* ??? We'd like to use queue_reg_save, but we need to come up with
1933      a different flushing heuristic for epilogues.  */
1934   reg_save (label, sregno, dregno, 0);
1935 }
1936
1937 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1938
1939 static void
1940 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1941 {
1942   dw_cfi_ref cfi = new_cfi ();
1943   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1944
1945   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1946   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1947
1948   add_fde_cfi (label, cfi);
1949 }
1950
1951 /* Record call frame debugging information for an expression EXPR,
1952    which either sets SP or FP (adjusting how we calculate the frame
1953    address) or saves a register to the stack or another register.
1954    LABEL indicates the address of EXPR.
1955
1956    This function encodes a state machine mapping rtxes to actions on
1957    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1958    users need not read the source code.
1959
1960   The High-Level Picture
1961
1962   Changes in the register we use to calculate the CFA: Currently we
1963   assume that if you copy the CFA register into another register, we
1964   should take the other one as the new CFA register; this seems to
1965   work pretty well.  If it's wrong for some target, it's simple
1966   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1967
1968   Changes in the register we use for saving registers to the stack:
1969   This is usually SP, but not always.  Again, we deduce that if you
1970   copy SP into another register (and SP is not the CFA register),
1971   then the new register is the one we will be using for register
1972   saves.  This also seems to work.
1973
1974   Register saves: There's not much guesswork about this one; if
1975   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1976   register save, and the register used to calculate the destination
1977   had better be the one we think we're using for this purpose.
1978   It's also assumed that a copy from a call-saved register to another
1979   register is saving that register if RTX_FRAME_RELATED_P is set on
1980   that instruction.  If the copy is from a call-saved register to
1981   the *same* register, that means that the register is now the same
1982   value as in the caller.
1983
1984   Except: If the register being saved is the CFA register, and the
1985   offset is nonzero, we are saving the CFA, so we assume we have to
1986   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1987   the intent is to save the value of SP from the previous frame.
1988
1989   In addition, if a register has previously been saved to a different
1990   register,
1991
1992   Invariants / Summaries of Rules
1993
1994   cfa          current rule for calculating the CFA.  It usually
1995                consists of a register and an offset.
1996   cfa_store    register used by prologue code to save things to the stack
1997                cfa_store.offset is the offset from the value of
1998                cfa_store.reg to the actual CFA
1999   cfa_temp     register holding an integral value.  cfa_temp.offset
2000                stores the value, which will be used to adjust the
2001                stack pointer.  cfa_temp is also used like cfa_store,
2002                to track stores to the stack via fp or a temp reg.
2003
2004   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2005                with cfa.reg as the first operand changes the cfa.reg and its
2006                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2007                cfa_temp.offset.
2008
2009   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2010                expression yielding a constant.  This sets cfa_temp.reg
2011                and cfa_temp.offset.
2012
2013   Rule 5:      Create a new register cfa_store used to save items to the
2014                stack.
2015
2016   Rules 10-14: Save a register to the stack.  Define offset as the
2017                difference of the original location and cfa_store's
2018                location (or cfa_temp's location if cfa_temp is used).
2019
2020   Rules 16-20: If AND operation happens on sp in prologue, we assume
2021                stack is realigned.  We will use a group of DW_OP_XXX
2022                expressions to represent the location of the stored
2023                register instead of CFA+offset.
2024
2025   The Rules
2026
2027   "{a,b}" indicates a choice of a xor b.
2028   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2029
2030   Rule 1:
2031   (set <reg1> <reg2>:cfa.reg)
2032   effects: cfa.reg = <reg1>
2033            cfa.offset unchanged
2034            cfa_temp.reg = <reg1>
2035            cfa_temp.offset = cfa.offset
2036
2037   Rule 2:
2038   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2039                               {<const_int>,<reg>:cfa_temp.reg}))
2040   effects: cfa.reg = sp if fp used
2041            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2042            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2043              if cfa_store.reg==sp
2044
2045   Rule 3:
2046   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2047   effects: cfa.reg = fp
2048            cfa_offset += +/- <const_int>
2049
2050   Rule 4:
2051   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2052   constraints: <reg1> != fp
2053                <reg1> != sp
2054   effects: cfa.reg = <reg1>
2055            cfa_temp.reg = <reg1>
2056            cfa_temp.offset = cfa.offset
2057
2058   Rule 5:
2059   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2060   constraints: <reg1> != fp
2061                <reg1> != sp
2062   effects: cfa_store.reg = <reg1>
2063            cfa_store.offset = cfa.offset - cfa_temp.offset
2064
2065   Rule 6:
2066   (set <reg> <const_int>)
2067   effects: cfa_temp.reg = <reg>
2068            cfa_temp.offset = <const_int>
2069
2070   Rule 7:
2071   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2072   effects: cfa_temp.reg = <reg1>
2073            cfa_temp.offset |= <const_int>
2074
2075   Rule 8:
2076   (set <reg> (high <exp>))
2077   effects: none
2078
2079   Rule 9:
2080   (set <reg> (lo_sum <exp> <const_int>))
2081   effects: cfa_temp.reg = <reg>
2082            cfa_temp.offset = <const_int>
2083
2084   Rule 10:
2085   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2086   effects: cfa_store.offset -= <const_int>
2087            cfa.offset = cfa_store.offset if cfa.reg == sp
2088            cfa.reg = sp
2089            cfa.base_offset = -cfa_store.offset
2090
2091   Rule 11:
2092   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2093   effects: cfa_store.offset += -/+ mode_size(mem)
2094            cfa.offset = cfa_store.offset if cfa.reg == sp
2095            cfa.reg = sp
2096            cfa.base_offset = -cfa_store.offset
2097
2098   Rule 12:
2099   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2100
2101        <reg2>)
2102   effects: cfa.reg = <reg1>
2103            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2104
2105   Rule 13:
2106   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2107   effects: cfa.reg = <reg1>
2108            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2109
2110   Rule 14:
2111   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2112   effects: cfa.reg = <reg1>
2113            cfa.base_offset = -cfa_temp.offset
2114            cfa_temp.offset -= mode_size(mem)
2115
2116   Rule 15:
2117   (set <reg> {unspec, unspec_volatile})
2118   effects: target-dependent
2119
2120   Rule 16:
2121   (set sp (and: sp <const_int>))
2122   constraints: cfa_store.reg == sp
2123   effects: current_fde.stack_realign = 1
2124            cfa_store.offset = 0
2125            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2126
2127   Rule 17:
2128   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2129   effects: cfa_store.offset += -/+ mode_size(mem)
2130
2131   Rule 18:
2132   (set (mem ({pre_inc, pre_dec} sp)) fp)
2133   constraints: fde->stack_realign == 1
2134   effects: cfa_store.offset = 0
2135            cfa.reg != HARD_FRAME_POINTER_REGNUM
2136
2137   Rule 19:
2138   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2139   constraints: fde->stack_realign == 1
2140                && cfa.offset == 0
2141                && cfa.indirect == 0
2142                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2143   effects: Use DW_CFA_def_cfa_expression to define cfa
2144            cfa.reg == fde->drap_reg
2145
2146   Rule 20:
2147   (set reg fde->drap_reg)
2148   constraints: fde->vdrap_reg == INVALID_REGNUM
2149   effects: fde->vdrap_reg = reg.
2150   (set mem fde->drap_reg)
2151   constraints: fde->drap_reg_saved == 1
2152   effects: none.  */
2153
2154 static void
2155 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2156 {
2157   rtx src, dest, span;
2158   HOST_WIDE_INT offset;
2159   dw_fde_ref fde;
2160
2161   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2162      the PARALLEL independently. The first element is always processed if
2163      it is a SET. This is for backward compatibility.   Other elements
2164      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2165      flag is set in them.  */
2166   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2167     {
2168       int par_index;
2169       int limit = XVECLEN (expr, 0);
2170       rtx elem;
2171
2172       /* PARALLELs have strict read-modify-write semantics, so we
2173          ought to evaluate every rvalue before changing any lvalue.
2174          It's cumbersome to do that in general, but there's an
2175          easy approximation that is enough for all current users:
2176          handle register saves before register assignments.  */
2177       if (GET_CODE (expr) == PARALLEL)
2178         for (par_index = 0; par_index < limit; par_index++)
2179           {
2180             elem = XVECEXP (expr, 0, par_index);
2181             if (GET_CODE (elem) == SET
2182                 && MEM_P (SET_DEST (elem))
2183                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2184               dwarf2out_frame_debug_expr (elem, label);
2185           }
2186
2187       for (par_index = 0; par_index < limit; par_index++)
2188         {
2189           elem = XVECEXP (expr, 0, par_index);
2190           if (GET_CODE (elem) == SET
2191               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2192               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2193             dwarf2out_frame_debug_expr (elem, label);
2194           else if (GET_CODE (elem) == SET
2195                    && par_index != 0
2196                    && !RTX_FRAME_RELATED_P (elem))
2197             {
2198               /* Stack adjustment combining might combine some post-prologue
2199                  stack adjustment into a prologue stack adjustment.  */
2200               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2201
2202               if (offset != 0)
2203                 dwarf2out_args_size_adjust (offset, label);
2204             }
2205         }
2206       return;
2207     }
2208
2209   gcc_assert (GET_CODE (expr) == SET);
2210
2211   src = SET_SRC (expr);
2212   dest = SET_DEST (expr);
2213
2214   if (REG_P (src))
2215     {
2216       rtx rsi = reg_saved_in (src);
2217       if (rsi)
2218         src = rsi;
2219     }
2220
2221   fde = current_fde ();
2222
2223   if (REG_P (src)
2224       && fde
2225       && fde->drap_reg == REGNO (src)
2226       && (fde->drap_reg_saved
2227           || REG_P (dest)))
2228     {
2229       /* Rule 20 */
2230       /* If we are saving dynamic realign argument pointer to a
2231          register, the destination is virtual dynamic realign
2232          argument pointer.  It may be used to access argument.  */
2233       if (REG_P (dest))
2234         {
2235           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2236           fde->vdrap_reg = REGNO (dest);
2237         }
2238       return;
2239     }
2240
2241   switch (GET_CODE (dest))
2242     {
2243     case REG:
2244       switch (GET_CODE (src))
2245         {
2246           /* Setting FP from SP.  */
2247         case REG:
2248           if (cfa.reg == (unsigned) REGNO (src))
2249             {
2250               /* Rule 1 */
2251               /* Update the CFA rule wrt SP or FP.  Make sure src is
2252                  relative to the current CFA register.
2253
2254                  We used to require that dest be either SP or FP, but the
2255                  ARM copies SP to a temporary register, and from there to
2256                  FP.  So we just rely on the backends to only set
2257                  RTX_FRAME_RELATED_P on appropriate insns.  */
2258               cfa.reg = REGNO (dest);
2259               cfa_temp.reg = cfa.reg;
2260               cfa_temp.offset = cfa.offset;
2261             }
2262           else
2263             {
2264               /* Saving a register in a register.  */
2265               gcc_assert (!fixed_regs [REGNO (dest)]
2266                           /* For the SPARC and its register window.  */
2267                           || (DWARF_FRAME_REGNUM (REGNO (src))
2268                               == DWARF_FRAME_RETURN_COLUMN));
2269
2270               /* After stack is aligned, we can only save SP in FP
2271                  if drap register is used.  In this case, we have
2272                  to restore stack pointer with the CFA value and we
2273                  don't generate this DWARF information.  */
2274               if (fde
2275                   && fde->stack_realign
2276                   && REGNO (src) == STACK_POINTER_REGNUM)
2277                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2278                             && fde->drap_reg != INVALID_REGNUM
2279                             && cfa.reg != REGNO (src));
2280               else
2281                 queue_reg_save (label, src, dest, 0);
2282             }
2283           break;
2284
2285         case PLUS:
2286         case MINUS:
2287         case LO_SUM:
2288           if (dest == stack_pointer_rtx)
2289             {
2290               /* Rule 2 */
2291               /* Adjusting SP.  */
2292               switch (GET_CODE (XEXP (src, 1)))
2293                 {
2294                 case CONST_INT:
2295                   offset = INTVAL (XEXP (src, 1));
2296                   break;
2297                 case REG:
2298                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2299                               == cfa_temp.reg);
2300                   offset = cfa_temp.offset;
2301                   break;
2302                 default:
2303                   gcc_unreachable ();
2304                 }
2305
2306               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2307                 {
2308                   /* Restoring SP from FP in the epilogue.  */
2309                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2310                   cfa.reg = STACK_POINTER_REGNUM;
2311                 }
2312               else if (GET_CODE (src) == LO_SUM)
2313                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2314                 ;
2315               else
2316                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2317
2318               if (GET_CODE (src) != MINUS)
2319                 offset = -offset;
2320               if (cfa.reg == STACK_POINTER_REGNUM)
2321                 cfa.offset += offset;
2322               if (cfa_store.reg == STACK_POINTER_REGNUM)
2323                 cfa_store.offset += offset;
2324             }
2325           else if (dest == hard_frame_pointer_rtx)
2326             {
2327               /* Rule 3 */
2328               /* Either setting the FP from an offset of the SP,
2329                  or adjusting the FP */
2330               gcc_assert (frame_pointer_needed);
2331
2332               gcc_assert (REG_P (XEXP (src, 0))
2333                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2334                           && CONST_INT_P (XEXP (src, 1)));
2335               offset = INTVAL (XEXP (src, 1));
2336               if (GET_CODE (src) != MINUS)
2337                 offset = -offset;
2338               cfa.offset += offset;
2339               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2340             }
2341           else
2342             {
2343               gcc_assert (GET_CODE (src) != MINUS);
2344
2345               /* Rule 4 */
2346               if (REG_P (XEXP (src, 0))
2347                   && REGNO (XEXP (src, 0)) == cfa.reg
2348                   && CONST_INT_P (XEXP (src, 1)))
2349                 {
2350                   /* Setting a temporary CFA register that will be copied
2351                      into the FP later on.  */
2352                   offset = - INTVAL (XEXP (src, 1));
2353                   cfa.offset += offset;
2354                   cfa.reg = REGNO (dest);
2355                   /* Or used to save regs to the stack.  */
2356                   cfa_temp.reg = cfa.reg;
2357                   cfa_temp.offset = cfa.offset;
2358                 }
2359
2360               /* Rule 5 */
2361               else if (REG_P (XEXP (src, 0))
2362                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2363                        && XEXP (src, 1) == stack_pointer_rtx)
2364                 {
2365                   /* Setting a scratch register that we will use instead
2366                      of SP for saving registers to the stack.  */
2367                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2368                   cfa_store.reg = REGNO (dest);
2369                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2370                 }
2371
2372               /* Rule 9 */
2373               else if (GET_CODE (src) == LO_SUM
2374                        && CONST_INT_P (XEXP (src, 1)))
2375                 {
2376                   cfa_temp.reg = REGNO (dest);
2377                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2378                 }
2379               else
2380                 gcc_unreachable ();
2381             }
2382           break;
2383
2384           /* Rule 6 */
2385         case CONST_INT:
2386           cfa_temp.reg = REGNO (dest);
2387           cfa_temp.offset = INTVAL (src);
2388           break;
2389
2390           /* Rule 7 */
2391         case IOR:
2392           gcc_assert (REG_P (XEXP (src, 0))
2393                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2394                       && CONST_INT_P (XEXP (src, 1)));
2395
2396           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2397             cfa_temp.reg = REGNO (dest);
2398           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2399           break;
2400
2401           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2402              which will fill in all of the bits.  */
2403           /* Rule 8 */
2404         case HIGH:
2405           break;
2406
2407           /* Rule 15 */
2408         case UNSPEC:
2409         case UNSPEC_VOLATILE:
2410           gcc_assert (targetm.dwarf_handle_frame_unspec);
2411           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2412           return;
2413
2414           /* Rule 16 */
2415         case AND:
2416           /* If this AND operation happens on stack pointer in prologue,
2417              we assume the stack is realigned and we extract the
2418              alignment.  */
2419           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2420             {
2421               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2422               fde->stack_realign = 1;
2423               fde->stack_realignment = INTVAL (XEXP (src, 1));
2424               cfa_store.offset = 0;
2425
2426               if (cfa.reg != STACK_POINTER_REGNUM
2427                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2428                 fde->drap_reg = cfa.reg;
2429             }
2430           return;
2431
2432         default:
2433           gcc_unreachable ();
2434         }
2435
2436       def_cfa_1 (label, &cfa);
2437       break;
2438
2439     case MEM:
2440
2441       /* Saving a register to the stack.  Make sure dest is relative to the
2442          CFA register.  */
2443       switch (GET_CODE (XEXP (dest, 0)))
2444         {
2445           /* Rule 10 */
2446           /* With a push.  */
2447         case PRE_MODIFY:
2448           /* We can't handle variable size modifications.  */
2449           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2450                       == CONST_INT);
2451           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2452
2453           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2454                       && cfa_store.reg == STACK_POINTER_REGNUM);
2455
2456           cfa_store.offset += offset;
2457           if (cfa.reg == STACK_POINTER_REGNUM)
2458             cfa.offset = cfa_store.offset;
2459
2460           offset = -cfa_store.offset;
2461           break;
2462
2463           /* Rule 11 */
2464         case PRE_INC:
2465         case PRE_DEC:
2466           offset = GET_MODE_SIZE (GET_MODE (dest));
2467           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2468             offset = -offset;
2469
2470           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2471                        == STACK_POINTER_REGNUM)
2472                       && cfa_store.reg == STACK_POINTER_REGNUM);
2473
2474           cfa_store.offset += offset;
2475
2476           /* Rule 18: If stack is aligned, we will use FP as a
2477              reference to represent the address of the stored
2478              regiser.  */
2479           if (fde
2480               && fde->stack_realign
2481               && src == hard_frame_pointer_rtx)
2482             {
2483               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2484               cfa_store.offset = 0;
2485             }
2486
2487           if (cfa.reg == STACK_POINTER_REGNUM)
2488             cfa.offset = cfa_store.offset;
2489
2490           offset = -cfa_store.offset;
2491           break;
2492
2493           /* Rule 12 */
2494           /* With an offset.  */
2495         case PLUS:
2496         case MINUS:
2497         case LO_SUM:
2498           {
2499             int regno;
2500
2501             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2502                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2503             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2504             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2505               offset = -offset;
2506
2507             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2508
2509             if (cfa_store.reg == (unsigned) regno)
2510               offset -= cfa_store.offset;
2511             else
2512               {
2513                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2514                 offset -= cfa_temp.offset;
2515               }
2516           }
2517           break;
2518
2519           /* Rule 13 */
2520           /* Without an offset.  */
2521         case REG:
2522           {
2523             int regno = REGNO (XEXP (dest, 0));
2524
2525             if (cfa_store.reg == (unsigned) regno)
2526               offset = -cfa_store.offset;
2527             else
2528               {
2529                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2530                 offset = -cfa_temp.offset;
2531               }
2532           }
2533           break;
2534
2535           /* Rule 14 */
2536         case POST_INC:
2537           gcc_assert (cfa_temp.reg
2538                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2539           offset = -cfa_temp.offset;
2540           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2541           break;
2542
2543         default:
2544           gcc_unreachable ();
2545         }
2546
2547         /* Rule 17 */
2548         /* If the source operand of this MEM operation is not a
2549            register, basically the source is return address.  Here
2550            we only care how much stack grew and we don't save it.  */
2551       if (!REG_P (src))
2552         break;
2553
2554       if (REGNO (src) != STACK_POINTER_REGNUM
2555           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2556           && (unsigned) REGNO (src) == cfa.reg)
2557         {
2558           /* We're storing the current CFA reg into the stack.  */
2559
2560           if (cfa.offset == 0)
2561             {
2562               /* Rule 19 */
2563               /* If stack is aligned, putting CFA reg into stack means
2564                  we can no longer use reg + offset to represent CFA.
2565                  Here we use DW_CFA_def_cfa_expression instead.  The
2566                  result of this expression equals to the original CFA
2567                  value.  */
2568               if (fde
2569                   && fde->stack_realign
2570                   && cfa.indirect == 0
2571                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2572                 {
2573                   dw_cfa_location cfa_exp;
2574
2575                   gcc_assert (fde->drap_reg == cfa.reg);
2576
2577                   cfa_exp.indirect = 1;
2578                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2579                   cfa_exp.base_offset = offset;
2580                   cfa_exp.offset = 0;
2581
2582                   fde->drap_reg_saved = 1;
2583
2584                   def_cfa_1 (label, &cfa_exp);
2585                   break;
2586                 }
2587
2588               /* If the source register is exactly the CFA, assume
2589                  we're saving SP like any other register; this happens
2590                  on the ARM.  */
2591               def_cfa_1 (label, &cfa);
2592               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2593               break;
2594             }
2595           else
2596             {
2597               /* Otherwise, we'll need to look in the stack to
2598                  calculate the CFA.  */
2599               rtx x = XEXP (dest, 0);
2600
2601               if (!REG_P (x))
2602                 x = XEXP (x, 0);
2603               gcc_assert (REG_P (x));
2604
2605               cfa.reg = REGNO (x);
2606               cfa.base_offset = offset;
2607               cfa.indirect = 1;
2608               def_cfa_1 (label, &cfa);
2609               break;
2610             }
2611         }
2612
2613       def_cfa_1 (label, &cfa);
2614       {
2615         span = targetm.dwarf_register_span (src);
2616
2617         if (!span)
2618           queue_reg_save (label, src, NULL_RTX, offset);
2619         else
2620           {
2621             /* We have a PARALLEL describing where the contents of SRC
2622                live.  Queue register saves for each piece of the
2623                PARALLEL.  */
2624             int par_index;
2625             int limit;
2626             HOST_WIDE_INT span_offset = offset;
2627
2628             gcc_assert (GET_CODE (span) == PARALLEL);
2629
2630             limit = XVECLEN (span, 0);
2631             for (par_index = 0; par_index < limit; par_index++)
2632               {
2633                 rtx elem = XVECEXP (span, 0, par_index);
2634
2635                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2636                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2637               }
2638           }
2639       }
2640       break;
2641
2642     default:
2643       gcc_unreachable ();
2644     }
2645 }
2646
2647 /* Record call frame debugging information for INSN, which either
2648    sets SP or FP (adjusting how we calculate the frame address) or saves a
2649    register to the stack.  If INSN is NULL_RTX, initialize our state.
2650
2651    If AFTER_P is false, we're being called before the insn is emitted,
2652    otherwise after.  Call instructions get invoked twice.  */
2653
2654 void
2655 dwarf2out_frame_debug (rtx insn, bool after_p)
2656 {
2657   const char *label;
2658   rtx note, n;
2659   bool handled_one = false;
2660
2661   if (insn == NULL_RTX)
2662     {
2663       size_t i;
2664
2665       /* Flush any queued register saves.  */
2666       flush_queued_reg_saves ();
2667
2668       /* Set up state for generating call frame debug info.  */
2669       lookup_cfa (&cfa);
2670       gcc_assert (cfa.reg
2671                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2672
2673       cfa.reg = STACK_POINTER_REGNUM;
2674       cfa_store = cfa;
2675       cfa_temp.reg = -1;
2676       cfa_temp.offset = 0;
2677
2678       for (i = 0; i < num_regs_saved_in_regs; i++)
2679         {
2680           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2681           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2682         }
2683       num_regs_saved_in_regs = 0;
2684
2685       if (barrier_args_size)
2686         {
2687           XDELETEVEC (barrier_args_size);
2688           barrier_args_size = NULL;
2689         }
2690       return;
2691     }
2692
2693   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2694     flush_queued_reg_saves ();
2695
2696   if (! RTX_FRAME_RELATED_P (insn))
2697     {
2698       if (!ACCUMULATE_OUTGOING_ARGS)
2699         dwarf2out_stack_adjust (insn, after_p);
2700       return;
2701     }
2702
2703   label = dwarf2out_cfi_label (false);
2704
2705   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2706     switch (REG_NOTE_KIND (note))
2707       {
2708       case REG_FRAME_RELATED_EXPR:
2709         insn = XEXP (note, 0);
2710         goto found;
2711
2712       case REG_CFA_DEF_CFA:
2713         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2714         handled_one = true;
2715         break;
2716
2717       case REG_CFA_ADJUST_CFA:
2718         n = XEXP (note, 0);
2719         if (n == NULL)
2720           {
2721             n = PATTERN (insn);
2722             if (GET_CODE (n) == PARALLEL)
2723               n = XVECEXP (n, 0, 0);
2724           }
2725         dwarf2out_frame_debug_adjust_cfa (n, label);
2726         handled_one = true;
2727         break;
2728
2729       case REG_CFA_OFFSET:
2730         n = XEXP (note, 0);
2731         if (n == NULL)
2732           n = single_set (insn);
2733         dwarf2out_frame_debug_cfa_offset (n, label);
2734         handled_one = true;
2735         break;
2736
2737       case REG_CFA_REGISTER:
2738         n = XEXP (note, 0);
2739         if (n == NULL)
2740           {
2741             n = PATTERN (insn);
2742             if (GET_CODE (n) == PARALLEL)
2743               n = XVECEXP (n, 0, 0);
2744           }
2745         dwarf2out_frame_debug_cfa_register (n, label);
2746         handled_one = true;
2747         break;
2748
2749       case REG_CFA_RESTORE:
2750         n = XEXP (note, 0);
2751         if (n == NULL)
2752           {
2753             n = PATTERN (insn);
2754             if (GET_CODE (n) == PARALLEL)
2755               n = XVECEXP (n, 0, 0);
2756             n = XEXP (n, 0);
2757           }
2758         dwarf2out_frame_debug_cfa_restore (n, label);
2759         handled_one = true;
2760         break;
2761
2762       default:
2763         break;
2764       }
2765   if (handled_one)
2766     return;
2767
2768   insn = PATTERN (insn);
2769  found:
2770   dwarf2out_frame_debug_expr (insn, label);
2771 }
2772
2773 /* Determine if we need to save and restore CFI information around this
2774    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2775    we do need to save/restore, then emit the save now, and insert a
2776    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2777
2778 void
2779 dwarf2out_begin_epilogue (rtx insn)
2780 {
2781   bool saw_frp = false;
2782   rtx i;
2783
2784   /* Scan forward to the return insn, noticing if there are possible
2785      frame related insns.  */
2786   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2787     {
2788       if (!INSN_P (i))
2789         continue;
2790
2791       /* Look for both regular and sibcalls to end the block.  */
2792       if (returnjump_p (i))
2793         break;
2794       if (CALL_P (i) && SIBLING_CALL_P (i))
2795         break;
2796
2797       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2798         {
2799           int idx;
2800           rtx seq = PATTERN (i);
2801
2802           if (returnjump_p (XVECEXP (seq, 0, 0)))
2803             break;
2804           if (CALL_P (XVECEXP (seq, 0, 0))
2805               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2806             break;
2807
2808           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2809             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2810               saw_frp = true;
2811         }
2812
2813       if (RTX_FRAME_RELATED_P (i))
2814         saw_frp = true;
2815     }
2816
2817   /* If the port doesn't emit epilogue unwind info, we don't need a
2818      save/restore pair.  */
2819   if (!saw_frp)
2820     return;
2821
2822   /* Otherwise, search forward to see if the return insn was the last
2823      basic block of the function.  If so, we don't need save/restore.  */
2824   gcc_assert (i != NULL);
2825   i = next_real_insn (i);
2826   if (i == NULL)
2827     return;
2828
2829   /* Insert the restore before that next real insn in the stream, and before
2830      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2831      properly nested.  This should be after any label or alignment.  This
2832      will be pushed into the CFI stream by the function below.  */
2833   while (1)
2834     {
2835       rtx p = PREV_INSN (i);
2836       if (!NOTE_P (p))
2837         break;
2838       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2839         break;
2840       i = p;
2841     }
2842   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2843
2844   emit_cfa_remember = true;
2845
2846   /* And emulate the state save.  */
2847   gcc_assert (!cfa_remember.in_use);
2848   cfa_remember = cfa;
2849   cfa_remember.in_use = 1;
2850 }
2851
2852 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2853
2854 void
2855 dwarf2out_frame_debug_restore_state (void)
2856 {
2857   dw_cfi_ref cfi = new_cfi (); 
2858   const char *label = dwarf2out_cfi_label (false);
2859
2860   cfi->dw_cfi_opc = DW_CFA_restore_state;
2861   add_fde_cfi (label, cfi);
2862
2863   gcc_assert (cfa_remember.in_use);
2864   cfa = cfa_remember;
2865   cfa_remember.in_use = 0;
2866 }
2867
2868 #endif
2869
2870 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2871 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2872  (enum dwarf_call_frame_info cfi);
2873
2874 static enum dw_cfi_oprnd_type
2875 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2876 {
2877   switch (cfi)
2878     {
2879     case DW_CFA_nop:
2880     case DW_CFA_GNU_window_save:
2881     case DW_CFA_remember_state:
2882     case DW_CFA_restore_state:
2883       return dw_cfi_oprnd_unused;
2884
2885     case DW_CFA_set_loc:
2886     case DW_CFA_advance_loc1:
2887     case DW_CFA_advance_loc2:
2888     case DW_CFA_advance_loc4:
2889     case DW_CFA_MIPS_advance_loc8:
2890       return dw_cfi_oprnd_addr;
2891
2892     case DW_CFA_offset:
2893     case DW_CFA_offset_extended:
2894     case DW_CFA_def_cfa:
2895     case DW_CFA_offset_extended_sf:
2896     case DW_CFA_def_cfa_sf:
2897     case DW_CFA_restore:
2898     case DW_CFA_restore_extended:
2899     case DW_CFA_undefined:
2900     case DW_CFA_same_value:
2901     case DW_CFA_def_cfa_register:
2902     case DW_CFA_register:
2903       return dw_cfi_oprnd_reg_num;
2904
2905     case DW_CFA_def_cfa_offset:
2906     case DW_CFA_GNU_args_size:
2907     case DW_CFA_def_cfa_offset_sf:
2908       return dw_cfi_oprnd_offset;
2909
2910     case DW_CFA_def_cfa_expression:
2911     case DW_CFA_expression:
2912       return dw_cfi_oprnd_loc;
2913
2914     default:
2915       gcc_unreachable ();
2916     }
2917 }
2918
2919 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2920 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2921  (enum dwarf_call_frame_info cfi);
2922
2923 static enum dw_cfi_oprnd_type
2924 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2925 {
2926   switch (cfi)
2927     {
2928     case DW_CFA_def_cfa:
2929     case DW_CFA_def_cfa_sf:
2930     case DW_CFA_offset:
2931     case DW_CFA_offset_extended_sf:
2932     case DW_CFA_offset_extended:
2933       return dw_cfi_oprnd_offset;
2934
2935     case DW_CFA_register:
2936       return dw_cfi_oprnd_reg_num;
2937
2938     default:
2939       return dw_cfi_oprnd_unused;
2940     }
2941 }
2942
2943 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2944
2945 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2946    switch to the data section instead, and write out a synthetic start label
2947    for collect2 the first time around.  */
2948
2949 static void
2950 switch_to_eh_frame_section (bool back)
2951 {
2952   tree label;
2953
2954 #ifdef EH_FRAME_SECTION_NAME
2955   if (eh_frame_section == 0)
2956     {
2957       int flags;
2958
2959       if (EH_TABLES_CAN_BE_READ_ONLY)
2960         {
2961           int fde_encoding;
2962           int per_encoding;
2963           int lsda_encoding;
2964
2965           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2966                                                        /*global=*/0);
2967           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2968                                                        /*global=*/1);
2969           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2970                                                         /*global=*/0);
2971           flags = ((! flag_pic
2972                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2973                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2974                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2975                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2976                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2977                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2978                    ? 0 : SECTION_WRITE);
2979         }
2980       else
2981         flags = SECTION_WRITE;
2982       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2983     }
2984 #endif
2985
2986   if (eh_frame_section)
2987     switch_to_section (eh_frame_section);
2988   else
2989     {
2990       /* We have no special eh_frame section.  Put the information in
2991          the data section and emit special labels to guide collect2.  */
2992       switch_to_section (data_section);
2993
2994       if (!back)
2995         {
2996           label = get_file_function_name ("F");
2997           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2998           targetm.asm_out.globalize_label (asm_out_file,
2999                                            IDENTIFIER_POINTER (label));
3000           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3001         }
3002     }
3003 }
3004
3005 /* Switch [BACK] to the eh or debug frame table section, depending on
3006    FOR_EH.  */
3007
3008 static void
3009 switch_to_frame_table_section (int for_eh, bool back)
3010 {
3011   if (for_eh)
3012     switch_to_eh_frame_section (back);
3013   else
3014     {
3015       if (!debug_frame_section)
3016         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3017                                            SECTION_DEBUG, NULL);
3018       switch_to_section (debug_frame_section);
3019     }
3020 }
3021
3022 /* Output a Call Frame Information opcode and its operand(s).  */
3023
3024 static void
3025 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3026 {
3027   unsigned long r;
3028   HOST_WIDE_INT off;
3029
3030   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3031     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3032                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3033                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3034                          ((unsigned HOST_WIDE_INT)
3035                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3036   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3037     {
3038       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3039       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3040                            "DW_CFA_offset, column 0x%lx", r);
3041       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3042       dw2_asm_output_data_uleb128 (off, NULL);
3043     }
3044   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3045     {
3046       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3047       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3048                            "DW_CFA_restore, column 0x%lx", r);
3049     }
3050   else
3051     {
3052       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3053                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3054
3055       switch (cfi->dw_cfi_opc)
3056         {
3057         case DW_CFA_set_loc:
3058           if (for_eh)
3059             dw2_asm_output_encoded_addr_rtx (
3060                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3061                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3062                 false, NULL);
3063           else
3064             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3065                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3066           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3067           break;
3068
3069         case DW_CFA_advance_loc1:
3070           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3071                                 fde->dw_fde_current_label, NULL);
3072           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3073           break;
3074
3075         case DW_CFA_advance_loc2:
3076           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3077                                 fde->dw_fde_current_label, NULL);
3078           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3079           break;
3080
3081         case DW_CFA_advance_loc4:
3082           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3083                                 fde->dw_fde_current_label, NULL);
3084           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3085           break;
3086
3087         case DW_CFA_MIPS_advance_loc8:
3088           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3089                                 fde->dw_fde_current_label, NULL);
3090           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3091           break;
3092
3093         case DW_CFA_offset_extended:
3094           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3095           dw2_asm_output_data_uleb128 (r, NULL);
3096           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3097           dw2_asm_output_data_uleb128 (off, NULL);
3098           break;
3099
3100         case DW_CFA_def_cfa:
3101           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3102           dw2_asm_output_data_uleb128 (r, NULL);
3103           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3104           break;
3105
3106         case DW_CFA_offset_extended_sf:
3107           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3108           dw2_asm_output_data_uleb128 (r, NULL);
3109           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3110           dw2_asm_output_data_sleb128 (off, NULL);
3111           break;
3112
3113         case DW_CFA_def_cfa_sf:
3114           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3115           dw2_asm_output_data_uleb128 (r, NULL);
3116           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3117           dw2_asm_output_data_sleb128 (off, NULL);
3118           break;
3119
3120         case DW_CFA_restore_extended:
3121         case DW_CFA_undefined:
3122         case DW_CFA_same_value:
3123         case DW_CFA_def_cfa_register:
3124           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3125           dw2_asm_output_data_uleb128 (r, NULL);
3126           break;
3127
3128         case DW_CFA_register:
3129           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3130           dw2_asm_output_data_uleb128 (r, NULL);
3131           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3132           dw2_asm_output_data_uleb128 (r, NULL);
3133           break;
3134
3135         case DW_CFA_def_cfa_offset:
3136         case DW_CFA_GNU_args_size:
3137           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3138           break;
3139
3140         case DW_CFA_def_cfa_offset_sf:
3141           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3142           dw2_asm_output_data_sleb128 (off, NULL);
3143           break;
3144
3145         case DW_CFA_GNU_window_save:
3146           break;
3147
3148         case DW_CFA_def_cfa_expression:
3149         case DW_CFA_expression:
3150           output_cfa_loc (cfi);
3151           break;
3152
3153         case DW_CFA_GNU_negative_offset_extended:
3154           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3155           gcc_unreachable ();
3156
3157         default:
3158           break;
3159         }
3160     }
3161 }
3162
3163 /* Similar, but do it via assembler directives instead.  */
3164
3165 static void
3166 output_cfi_directive (dw_cfi_ref cfi)
3167 {
3168   unsigned long r, r2;
3169
3170   switch (cfi->dw_cfi_opc)
3171     {
3172     case DW_CFA_advance_loc:
3173     case DW_CFA_advance_loc1:
3174     case DW_CFA_advance_loc2:
3175     case DW_CFA_advance_loc4:
3176     case DW_CFA_MIPS_advance_loc8:
3177     case DW_CFA_set_loc:
3178       /* Should only be created by add_fde_cfi in a code path not
3179          followed when emitting via directives.  The assembler is
3180          going to take care of this for us.  */
3181       gcc_unreachable ();
3182
3183     case DW_CFA_offset:
3184     case DW_CFA_offset_extended:
3185     case DW_CFA_offset_extended_sf:
3186       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3187       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3188                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3189       break;
3190
3191     case DW_CFA_restore:
3192     case DW_CFA_restore_extended:
3193       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3194       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3195       break;
3196
3197     case DW_CFA_undefined:
3198       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3199       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3200       break;
3201
3202     case DW_CFA_same_value:
3203       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3204       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3205       break;
3206
3207     case DW_CFA_def_cfa:
3208     case DW_CFA_def_cfa_sf:
3209       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3210       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3211                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3212       break;
3213
3214     case DW_CFA_def_cfa_register:
3215       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3216       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3217       break;
3218
3219     case DW_CFA_register:
3220       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3221       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3222       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3223       break;
3224
3225     case DW_CFA_def_cfa_offset:
3226     case DW_CFA_def_cfa_offset_sf:
3227       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3228                HOST_WIDE_INT_PRINT_DEC"\n",
3229                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3230       break;
3231
3232     case DW_CFA_remember_state:
3233       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3234       break;
3235     case DW_CFA_restore_state:
3236       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3237       break;
3238
3239     case DW_CFA_GNU_args_size:
3240       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3241       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3242       if (flag_debug_asm)
3243         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3244                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3245       fputc ('\n', asm_out_file);
3246       break;
3247
3248     case DW_CFA_GNU_window_save:
3249       fprintf (asm_out_file, "\t.cfi_window_save\n");
3250       break;
3251
3252     case DW_CFA_def_cfa_expression:
3253     case DW_CFA_expression:
3254       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3255       output_cfa_loc_raw (cfi);
3256       fputc ('\n', asm_out_file);
3257       break;
3258
3259     default:
3260       gcc_unreachable ();
3261     }
3262 }
3263
3264 DEF_VEC_P (dw_cfi_ref);
3265 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3266
3267 /* Output CFIs to bring current FDE to the same state as after executing
3268    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3269    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3270    other arguments to pass to output_cfi.  */
3271
3272 static void
3273 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3274 {
3275   struct dw_cfi_struct cfi_buf;
3276   dw_cfi_ref cfi2;
3277   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3278   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3279   unsigned int len, idx;
3280
3281   for (;; cfi = cfi->dw_cfi_next)
3282     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3283       {
3284       case DW_CFA_advance_loc:
3285       case DW_CFA_advance_loc1:
3286       case DW_CFA_advance_loc2:
3287       case DW_CFA_advance_loc4:
3288       case DW_CFA_MIPS_advance_loc8:
3289       case DW_CFA_set_loc:
3290         /* All advances should be ignored.  */
3291         break;
3292       case DW_CFA_remember_state:
3293         {
3294           dw_cfi_ref args_size = cfi_args_size;
3295
3296           /* Skip everything between .cfi_remember_state and
3297              .cfi_restore_state.  */
3298           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3299             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3300               break;
3301             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3302               args_size = cfi2;
3303             else
3304               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3305
3306           if (cfi2 == NULL)
3307             goto flush_all;
3308           else
3309             {
3310               cfi = cfi2;
3311               cfi_args_size = args_size;
3312             }
3313           break;
3314         }
3315       case DW_CFA_GNU_args_size:
3316         cfi_args_size = cfi;
3317         break;
3318       case DW_CFA_GNU_window_save:
3319         goto flush_all;
3320       case DW_CFA_offset:
3321       case DW_CFA_offset_extended:
3322       case DW_CFA_offset_extended_sf:
3323       case DW_CFA_restore:
3324       case DW_CFA_restore_extended:
3325       case DW_CFA_undefined:
3326       case DW_CFA_same_value:
3327       case DW_CFA_register:
3328       case DW_CFA_val_offset:
3329       case DW_CFA_val_offset_sf:
3330       case DW_CFA_expression:
3331       case DW_CFA_val_expression:
3332       case DW_CFA_GNU_negative_offset_extended:
3333         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3334           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3335                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3336         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3337         break;
3338       case DW_CFA_def_cfa:
3339       case DW_CFA_def_cfa_sf:
3340       case DW_CFA_def_cfa_expression:
3341         cfi_cfa = cfi;
3342         cfi_cfa_offset = cfi;
3343         break;
3344       case DW_CFA_def_cfa_register:
3345         cfi_cfa = cfi;
3346         break;
3347       case DW_CFA_def_cfa_offset:
3348       case DW_CFA_def_cfa_offset_sf:
3349         cfi_cfa_offset = cfi;
3350         break;
3351       case DW_CFA_nop:
3352         gcc_assert (cfi == NULL);
3353       flush_all:
3354         len = VEC_length (dw_cfi_ref, regs);
3355         for (idx = 0; idx < len; idx++)
3356           {
3357             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3358             if (cfi2 != NULL
3359                 && cfi2->dw_cfi_opc != DW_CFA_restore
3360                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3361               {
3362                 if (do_cfi_asm)
3363                   output_cfi_directive (cfi2);
3364                 else
3365                   output_cfi (cfi2, fde, for_eh);
3366               }
3367           }
3368         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3369           {
3370             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3371             cfi_buf = *cfi_cfa;
3372             switch (cfi_cfa_offset->dw_cfi_opc)
3373               {
3374               case DW_CFA_def_cfa_offset:
3375                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3376                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3377                 break;
3378               case DW_CFA_def_cfa_offset_sf:
3379                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3380                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3381                 break;
3382               case DW_CFA_def_cfa:
3383               case DW_CFA_def_cfa_sf:
3384                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3385                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3386                 break;
3387               default:
3388                 gcc_unreachable ();
3389               }
3390             cfi_cfa = &cfi_buf;
3391           }
3392         else if (cfi_cfa_offset)
3393           cfi_cfa = cfi_cfa_offset;
3394         if (cfi_cfa)
3395           {
3396             if (do_cfi_asm)
3397               output_cfi_directive (cfi_cfa);
3398             else
3399               output_cfi (cfi_cfa, fde, for_eh);
3400           }
3401         cfi_cfa = NULL;
3402         cfi_cfa_offset = NULL;
3403         if (cfi_args_size
3404             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3405           {
3406             if (do_cfi_asm)
3407               output_cfi_directive (cfi_args_size);
3408             else
3409               output_cfi (cfi_args_size, fde, for_eh);
3410           }
3411         cfi_args_size = NULL;
3412         if (cfi == NULL)
3413           {
3414             VEC_free (dw_cfi_ref, heap, regs);
3415             return;
3416           }
3417         else if (do_cfi_asm)
3418           output_cfi_directive (cfi);
3419         else
3420           output_cfi (cfi, fde, for_eh);
3421         break;
3422       default:
3423         gcc_unreachable ();
3424     }
3425 }
3426
3427 /* Output one FDE.  */
3428
3429 static void
3430 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3431             char *section_start_label, int fde_encoding, char *augmentation,
3432             bool any_lsda_needed, int lsda_encoding)
3433 {
3434   const char *begin, *end;
3435   static unsigned int j;
3436   char l1[20], l2[20];
3437   dw_cfi_ref cfi;
3438
3439   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3440                                 /* empty */ 0);
3441   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3442                                   for_eh + j);
3443   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3444   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3445   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3446     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3447                          " indicating 64-bit DWARF extension");
3448   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3449                         "FDE Length");
3450   ASM_OUTPUT_LABEL (asm_out_file, l1);
3451
3452   if (for_eh)
3453     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3454   else
3455     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3456                            debug_frame_section, "FDE CIE offset");
3457
3458   if (!fde->dw_fde_switched_sections)
3459     {
3460       begin = fde->dw_fde_begin;
3461       end = fde->dw_fde_end;
3462     }
3463   else
3464     {
3465       /* For the first section, prefer dw_fde_begin over
3466          dw_fde_{hot,cold}_section_label, as the latter
3467          might be separated from the real start of the
3468          function by alignment padding.  */
3469       if (!second)
3470         begin = fde->dw_fde_begin;
3471       else if (fde->dw_fde_switched_cold_to_hot)
3472         begin = fde->dw_fde_hot_section_label;
3473       else
3474         begin = fde->dw_fde_unlikely_section_label;
3475       if (second ^ fde->dw_fde_switched_cold_to_hot)
3476         end = fde->dw_fde_unlikely_section_end_label;
3477       else
3478         end = fde->dw_fde_hot_section_end_label;
3479     }
3480
3481   if (for_eh)
3482     {
3483       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3484       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3485       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3486                                        "FDE initial location");
3487       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3488                             end, begin, "FDE address range");
3489     }
3490   else
3491     {
3492       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3493       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3494     }
3495
3496   if (augmentation[0])
3497     {
3498       if (any_lsda_needed)
3499         {
3500           int size = size_of_encoded_value (lsda_encoding);
3501
3502           if (lsda_encoding == DW_EH_PE_aligned)
3503             {
3504               int offset = (  4         /* Length */
3505                             + 4         /* CIE offset */
3506                             + 2 * size_of_encoded_value (fde_encoding)
3507                             + 1         /* Augmentation size */ );
3508               int pad = -offset & (PTR_SIZE - 1);
3509
3510               size += pad;
3511               gcc_assert (size_of_uleb128 (size) == 1);
3512             }
3513
3514           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3515
3516           if (fde->uses_eh_lsda)
3517             {
3518               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3519                                            fde->funcdef_number);
3520               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3521                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3522                                                false,
3523                                                "Language Specific Data Area");
3524             }
3525           else
3526             {
3527               if (lsda_encoding == DW_EH_PE_aligned)
3528                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3529               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3530                                    "Language Specific Data Area (none)");
3531             }
3532         }
3533       else
3534         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3535     }
3536
3537   /* Loop through the Call Frame Instructions associated with
3538      this FDE.  */
3539   fde->dw_fde_current_label = begin;
3540   if (!fde->dw_fde_switched_sections)
3541     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3542       output_cfi (cfi, fde, for_eh);
3543   else if (!second)
3544     {
3545       if (fde->dw_fde_switch_cfi)
3546         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3547           {
3548             output_cfi (cfi, fde, for_eh);
3549             if (cfi == fde->dw_fde_switch_cfi)
3550               break;
3551           }
3552     }
3553   else
3554     {
3555       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3556
3557       if (fde->dw_fde_switch_cfi)
3558         {
3559           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3560           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3561           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3562           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3563         }
3564       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3565         output_cfi (cfi, fde, for_eh);
3566     }
3567
3568   /* If we are to emit a ref/link from function bodies to their frame tables,
3569      do it now.  This is typically performed to make sure that tables
3570      associated with functions are dragged with them and not discarded in
3571      garbage collecting links. We need to do this on a per function basis to
3572      cope with -ffunction-sections.  */
3573
3574 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3575   /* Switch to the function section, emit the ref to the tables, and
3576      switch *back* into the table section.  */
3577   switch_to_section (function_section (fde->decl));
3578   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3579   switch_to_frame_table_section (for_eh, true);
3580 #endif
3581
3582   /* Pad the FDE out to an address sized boundary.  */
3583   ASM_OUTPUT_ALIGN (asm_out_file,
3584                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3585   ASM_OUTPUT_LABEL (asm_out_file, l2);
3586
3587   j += 2;
3588 }
3589
3590 /* Output the call frame information used to record information
3591    that relates to calculating the frame pointer, and records the
3592    location of saved registers.  */
3593
3594 static void
3595 output_call_frame_info (int for_eh)
3596 {
3597   unsigned int i;
3598   dw_fde_ref fde;
3599   dw_cfi_ref cfi;
3600   char l1[20], l2[20], section_start_label[20];
3601   bool any_lsda_needed = false;
3602   char augmentation[6];
3603   int augmentation_size;
3604   int fde_encoding = DW_EH_PE_absptr;
3605   int per_encoding = DW_EH_PE_absptr;
3606   int lsda_encoding = DW_EH_PE_absptr;
3607   int return_reg;
3608   rtx personality = NULL;
3609   int dw_cie_version;
3610
3611   /* Don't emit a CIE if there won't be any FDEs.  */
3612   if (fde_table_in_use == 0)
3613     return;
3614
3615   /* Nothing to do if the assembler's doing it all.  */
3616   if (dwarf2out_do_cfi_asm ())
3617     return;
3618
3619   /* If we make FDEs linkonce, we may have to emit an empty label for
3620      an FDE that wouldn't otherwise be emitted.  We want to avoid
3621      having an FDE kept around when the function it refers to is
3622      discarded.  Example where this matters: a primary function
3623      template in C++ requires EH information, but an explicit
3624      specialization doesn't.  */
3625   if (TARGET_USES_WEAK_UNWIND_INFO
3626       && ! flag_asynchronous_unwind_tables
3627       && flag_exceptions
3628       && for_eh)
3629     for (i = 0; i < fde_table_in_use; i++)
3630       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3631           && !fde_table[i].uses_eh_lsda
3632           && ! DECL_WEAK (fde_table[i].decl))
3633         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3634                                       for_eh, /* empty */ 1);
3635
3636   /* If we don't have any functions we'll want to unwind out of, don't
3637      emit any EH unwind information.  Note that if exceptions aren't
3638      enabled, we won't have collected nothrow information, and if we
3639      asked for asynchronous tables, we always want this info.  */
3640   if (for_eh)
3641     {
3642       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3643
3644       for (i = 0; i < fde_table_in_use; i++)
3645         if (fde_table[i].uses_eh_lsda)
3646           any_eh_needed = any_lsda_needed = true;
3647         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3648           any_eh_needed = true;
3649         else if (! fde_table[i].nothrow
3650                  && ! fde_table[i].all_throwers_are_sibcalls)
3651           any_eh_needed = true;
3652
3653       if (! any_eh_needed)
3654         return;
3655     }
3656
3657   /* We're going to be generating comments, so turn on app.  */
3658   if (flag_debug_asm)
3659     app_enable ();
3660
3661   /* Switch to the proper frame section, first time.  */
3662   switch_to_frame_table_section (for_eh, false);
3663
3664   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3665   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3666
3667   /* Output the CIE.  */
3668   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3669   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3670   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3671     dw2_asm_output_data (4, 0xffffffff,
3672       "Initial length escape value indicating 64-bit DWARF extension");
3673   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3674                         "Length of Common Information Entry");
3675   ASM_OUTPUT_LABEL (asm_out_file, l1);
3676
3677   /* Now that the CIE pointer is PC-relative for EH,
3678      use 0 to identify the CIE.  */
3679   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3680                        (for_eh ? 0 : DWARF_CIE_ID),
3681                        "CIE Identifier Tag");
3682
3683   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3684      use CIE version 1, unless that would produce incorrect results
3685      due to overflowing the return register column.  */
3686   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3687   dw_cie_version = 1;
3688   if (return_reg >= 256 || dwarf_version > 2)
3689     dw_cie_version = 3;
3690   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3691
3692   augmentation[0] = 0;
3693   augmentation_size = 0;
3694
3695   personality = current_unit_personality;
3696   if (for_eh)
3697     {
3698       char *p;
3699
3700       /* Augmentation:
3701          z      Indicates that a uleb128 is present to size the
3702                 augmentation section.
3703          L      Indicates the encoding (and thus presence) of
3704                 an LSDA pointer in the FDE augmentation.
3705          R      Indicates a non-default pointer encoding for
3706                 FDE code pointers.
3707          P      Indicates the presence of an encoding + language
3708                 personality routine in the CIE augmentation.  */
3709
3710       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3711       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3712       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3713
3714       p = augmentation + 1;
3715       if (personality)
3716         {
3717           *p++ = 'P';
3718           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3719           assemble_external_libcall (personality);
3720         }
3721       if (any_lsda_needed)
3722         {
3723           *p++ = 'L';
3724           augmentation_size += 1;
3725         }
3726       if (fde_encoding != DW_EH_PE_absptr)
3727         {
3728           *p++ = 'R';
3729           augmentation_size += 1;
3730         }
3731       if (p > augmentation + 1)
3732         {
3733           augmentation[0] = 'z';
3734           *p = '\0';
3735         }
3736
3737       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3738       if (personality && per_encoding == DW_EH_PE_aligned)
3739         {
3740           int offset = (  4             /* Length */
3741                         + 4             /* CIE Id */
3742                         + 1             /* CIE version */
3743                         + strlen (augmentation) + 1     /* Augmentation */
3744                         + size_of_uleb128 (1)           /* Code alignment */
3745                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3746                         + 1             /* RA column */
3747                         + 1             /* Augmentation size */
3748                         + 1             /* Personality encoding */ );
3749           int pad = -offset & (PTR_SIZE - 1);
3750
3751           augmentation_size += pad;
3752
3753           /* Augmentations should be small, so there's scarce need to
3754              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3755           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3756         }
3757     }
3758
3759   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3760   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3761   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3762                                "CIE Data Alignment Factor");
3763
3764   if (dw_cie_version == 1)
3765     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3766   else
3767     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3768
3769   if (augmentation[0])
3770     {
3771       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3772       if (personality)
3773         {
3774           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3775                                eh_data_format_name (per_encoding));
3776           dw2_asm_output_encoded_addr_rtx (per_encoding,
3777                                            personality,
3778                                            true, NULL);
3779         }
3780
3781       if (any_lsda_needed)
3782         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3783                              eh_data_format_name (lsda_encoding));
3784
3785       if (fde_encoding != DW_EH_PE_absptr)
3786         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3787                              eh_data_format_name (fde_encoding));
3788     }
3789
3790   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3791     output_cfi (cfi, NULL, for_eh);
3792
3793   /* Pad the CIE out to an address sized boundary.  */
3794   ASM_OUTPUT_ALIGN (asm_out_file,
3795                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3796   ASM_OUTPUT_LABEL (asm_out_file, l2);
3797
3798   /* Loop through all of the FDE's.  */
3799   for (i = 0; i < fde_table_in_use; i++)
3800     {
3801       unsigned int k;
3802       fde = &fde_table[i];
3803
3804       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3805       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3806           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3807           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3808           && !fde->uses_eh_lsda)
3809         continue;
3810
3811       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3812         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3813                     augmentation, any_lsda_needed, lsda_encoding);
3814     }
3815
3816   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3817     dw2_asm_output_data (4, 0, "End of Table");
3818 #ifdef MIPS_DEBUGGING_INFO
3819   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3820      get a value of 0.  Putting .align 0 after the label fixes it.  */
3821   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3822 #endif
3823
3824   /* Turn off app to make assembly quicker.  */
3825   if (flag_debug_asm)
3826     app_disable ();
3827 }
3828
3829 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3830
3831 static void
3832 dwarf2out_do_cfi_startproc (bool second)
3833 {
3834   int enc;
3835   rtx ref;
3836   rtx personality = get_personality_function (current_function_decl);
3837
3838   fprintf (asm_out_file, "\t.cfi_startproc\n");
3839
3840   if (personality)
3841     {
3842       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3843       ref = personality;
3844
3845       /* ??? The GAS support isn't entirely consistent.  We have to
3846          handle indirect support ourselves, but PC-relative is done
3847          in the assembler.  Further, the assembler can't handle any
3848          of the weirder relocation types.  */
3849       if (enc & DW_EH_PE_indirect)
3850         ref = dw2_force_const_mem (ref, true);
3851
3852       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3853       output_addr_const (asm_out_file, ref);
3854       fputc ('\n', asm_out_file);
3855     }
3856
3857   if (crtl->uses_eh_lsda)
3858     {
3859       char lab[20];
3860
3861       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3862       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3863                                    current_function_funcdef_no);
3864       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3865       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3866
3867       if (enc & DW_EH_PE_indirect)
3868         ref = dw2_force_const_mem (ref, true);
3869
3870       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3871       output_addr_const (asm_out_file, ref);
3872       fputc ('\n', asm_out_file);
3873     }
3874 }
3875
3876 /* Output a marker (i.e. a label) for the beginning of a function, before
3877    the prologue.  */
3878
3879 void
3880 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3881                           const char *file ATTRIBUTE_UNUSED)
3882 {
3883   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3884   char * dup_label;
3885   dw_fde_ref fde;
3886   section *fnsec;
3887
3888   current_function_func_begin_label = NULL;
3889
3890 #ifdef TARGET_UNWIND_INFO
3891   /* ??? current_function_func_begin_label is also used by except.c
3892      for call-site information.  We must emit this label if it might
3893      be used.  */
3894   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3895       && ! dwarf2out_do_frame ())
3896     return;
3897 #else
3898   if (! dwarf2out_do_frame ())
3899     return;
3900 #endif
3901
3902   fnsec = function_section (current_function_decl);
3903   switch_to_section (fnsec);
3904   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3905                                current_function_funcdef_no);
3906   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3907                           current_function_funcdef_no);
3908   dup_label = xstrdup (label);
3909   current_function_func_begin_label = dup_label;
3910
3911 #ifdef TARGET_UNWIND_INFO
3912   /* We can elide the fde allocation if we're not emitting debug info.  */
3913   if (! dwarf2out_do_frame ())
3914     return;
3915 #endif
3916
3917   /* Expand the fde table if necessary.  */
3918   if (fde_table_in_use == fde_table_allocated)
3919     {
3920       fde_table_allocated += FDE_TABLE_INCREMENT;
3921       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3922       memset (fde_table + fde_table_in_use, 0,
3923               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3924     }
3925
3926   /* Record the FDE associated with this function.  */
3927   current_funcdef_fde = fde_table_in_use;
3928
3929   /* Add the new FDE at the end of the fde_table.  */
3930   fde = &fde_table[fde_table_in_use++];
3931   fde->decl = current_function_decl;
3932   fde->dw_fde_begin = dup_label;
3933   fde->dw_fde_current_label = dup_label;
3934   fde->dw_fde_hot_section_label = NULL;
3935   fde->dw_fde_hot_section_end_label = NULL;
3936   fde->dw_fde_unlikely_section_label = NULL;
3937   fde->dw_fde_unlikely_section_end_label = NULL;
3938   fde->dw_fde_switched_sections = 0;
3939   fde->dw_fde_switched_cold_to_hot = 0;
3940   fde->dw_fde_end = NULL;
3941   fde->dw_fde_cfi = NULL;
3942   fde->dw_fde_switch_cfi = NULL;
3943   fde->funcdef_number = current_function_funcdef_no;
3944   fde->nothrow = crtl->nothrow;
3945   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3946   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3947   fde->drap_reg = INVALID_REGNUM;
3948   fde->vdrap_reg = INVALID_REGNUM;
3949   if (flag_reorder_blocks_and_partition)
3950     {
3951       section *unlikelysec;
3952       if (first_function_block_is_cold)
3953         fde->in_std_section = 1;
3954       else
3955         fde->in_std_section
3956           = (fnsec == text_section
3957              || (cold_text_section && fnsec == cold_text_section));
3958       unlikelysec = unlikely_text_section ();
3959       fde->cold_in_std_section
3960         = (unlikelysec == text_section
3961            || (cold_text_section && unlikelysec == cold_text_section));
3962     }
3963   else
3964     {
3965       fde->in_std_section
3966         = (fnsec == text_section
3967            || (cold_text_section && fnsec == cold_text_section));
3968       fde->cold_in_std_section = 0;
3969     }
3970
3971   args_size = old_args_size = 0;
3972
3973   /* We only want to output line number information for the genuine dwarf2
3974      prologue case, not the eh frame case.  */
3975 #ifdef DWARF2_DEBUGGING_INFO
3976   if (file)
3977     dwarf2out_source_line (line, file, 0, true);
3978 #endif
3979
3980   if (dwarf2out_do_cfi_asm ())
3981     dwarf2out_do_cfi_startproc (false);
3982   else
3983     {
3984       rtx personality = get_personality_function (current_function_decl);
3985       if (!current_unit_personality)
3986         current_unit_personality = personality;
3987
3988       /* We cannot keep a current personality per function as without CFI
3989          asm at the point where we emit the CFI data there is no current
3990          function anymore.  */
3991       if (personality
3992           && current_unit_personality != personality)
3993         sorry ("Multiple EH personalities are supported only with assemblers "
3994                "supporting .cfi.personality directive.");
3995     }
3996 }
3997
3998 /* Output a marker (i.e. a label) for the absolute end of the generated code
3999    for a function definition.  This gets called *after* the epilogue code has
4000    been generated.  */
4001
4002 void
4003 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4004                         const char *file ATTRIBUTE_UNUSED)
4005 {
4006   dw_fde_ref fde;
4007   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4008
4009 #ifdef DWARF2_DEBUGGING_INFO
4010   last_var_location_insn = NULL_RTX;
4011 #endif
4012
4013   if (dwarf2out_do_cfi_asm ())
4014     fprintf (asm_out_file, "\t.cfi_endproc\n");
4015
4016   /* Output a label to mark the endpoint of the code generated for this
4017      function.  */
4018   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4019                                current_function_funcdef_no);
4020   ASM_OUTPUT_LABEL (asm_out_file, label);
4021   fde = current_fde ();
4022   gcc_assert (fde != NULL);
4023   fde->dw_fde_end = xstrdup (label);
4024 }
4025
4026 void
4027 dwarf2out_frame_init (void)
4028 {
4029   /* Allocate the initial hunk of the fde_table.  */
4030   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4031   fde_table_allocated = FDE_TABLE_INCREMENT;
4032   fde_table_in_use = 0;
4033
4034   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4035      sake of lookup_cfa.  */
4036
4037   /* On entry, the Canonical Frame Address is at SP.  */
4038   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4039
4040 #ifdef DWARF2_UNWIND_INFO
4041   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4042     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4043 #endif
4044 }
4045
4046 void
4047 dwarf2out_frame_finish (void)
4048 {
4049   /* Output call frame information.  */
4050   if (DWARF2_FRAME_INFO)
4051     output_call_frame_info (0);
4052
4053 #ifndef TARGET_UNWIND_INFO
4054   /* Output another copy for the unwinder.  */
4055   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4056     output_call_frame_info (1);
4057 #endif
4058 }
4059
4060 /* Note that the current function section is being used for code.  */
4061
4062 static void
4063 dwarf2out_note_section_used (void)
4064 {
4065   section *sec = current_function_section ();
4066   if (sec == text_section)
4067     text_section_used = true;
4068   else if (sec == cold_text_section)
4069     cold_text_section_used = true;
4070 }
4071
4072 void
4073 dwarf2out_switch_text_section (void)
4074 {
4075   dw_fde_ref fde = current_fde ();
4076
4077   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4078
4079   fde->dw_fde_switched_sections = 1;
4080   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4081
4082   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4083   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4084   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4085   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4086   have_multiple_function_sections = true;
4087
4088   /* Reset the current label on switching text sections, so that we
4089      don't attempt to advance_loc4 between labels in different sections.  */
4090   fde->dw_fde_current_label = NULL;
4091
4092   /* There is no need to mark used sections when not debugging.  */
4093   if (cold_text_section != NULL)
4094     dwarf2out_note_section_used ();
4095
4096   if (dwarf2out_do_cfi_asm ())
4097     fprintf (asm_out_file, "\t.cfi_endproc\n");
4098
4099   /* Now do the real section switch.  */
4100   switch_to_section (current_function_section ());
4101
4102   if (dwarf2out_do_cfi_asm ())
4103     {
4104       dwarf2out_do_cfi_startproc (true);
4105       /* As this is a different FDE, insert all current CFI instructions
4106          again.  */
4107       output_cfis (fde->dw_fde_cfi, true, fde, true);
4108     }
4109   else
4110     {
4111       dw_cfi_ref cfi = fde->dw_fde_cfi;
4112
4113       cfi = fde->dw_fde_cfi;
4114       if (cfi)
4115         while (cfi->dw_cfi_next != NULL)
4116           cfi = cfi->dw_cfi_next;
4117       fde->dw_fde_switch_cfi = cfi;
4118     }
4119 }
4120 #endif
4121 \f
4122 /* And now, the subset of the debugging information support code necessary
4123    for emitting location expressions.  */
4124
4125 /* Data about a single source file.  */
4126 struct GTY(()) dwarf_file_data {
4127   const char * filename;
4128   int emitted_number;
4129 };
4130
4131 typedef struct dw_val_struct *dw_val_ref;
4132 typedef struct die_struct *dw_die_ref;
4133 typedef const struct die_struct *const_dw_die_ref;
4134 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4135 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4136
4137 typedef struct GTY(()) deferred_locations_struct
4138 {
4139   tree variable;
4140   dw_die_ref die;
4141 } deferred_locations;
4142
4143 DEF_VEC_O(deferred_locations);
4144 DEF_VEC_ALLOC_O(deferred_locations,gc);
4145
4146 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4147
4148 /* Each DIE may have a series of attribute/value pairs.  Values
4149    can take on several forms.  The forms that are used in this
4150    implementation are listed below.  */
4151
4152 enum dw_val_class
4153 {
4154   dw_val_class_addr,
4155   dw_val_class_offset,
4156   dw_val_class_loc,
4157   dw_val_class_loc_list,
4158   dw_val_class_range_list,
4159   dw_val_class_const,
4160   dw_val_class_unsigned_const,
4161   dw_val_class_long_long,
4162   dw_val_class_vec,
4163   dw_val_class_flag,
4164   dw_val_class_die_ref,
4165   dw_val_class_fde_ref,
4166   dw_val_class_lbl_id,
4167   dw_val_class_lineptr,
4168   dw_val_class_str,
4169   dw_val_class_macptr,
4170   dw_val_class_file
4171 };
4172
4173 /* Describe a floating point constant value, or a vector constant value.  */
4174
4175 typedef struct GTY(()) dw_vec_struct {
4176   unsigned char * GTY((length ("%h.length"))) array;
4177   unsigned length;
4178   unsigned elt_size;
4179 }
4180 dw_vec_const;
4181
4182 /* The dw_val_node describes an attribute's value, as it is
4183    represented internally.  */
4184
4185 typedef struct GTY(()) dw_val_struct {
4186   enum dw_val_class val_class;
4187   union dw_val_struct_union
4188     {
4189       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4190       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4191       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4192       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4193       HOST_WIDE_INT GTY ((default)) val_int;
4194       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4195       rtx GTY ((tag ("dw_val_class_long_long"))) val_long_long;
4196       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4197       struct dw_val_die_union
4198         {
4199        &n