OSDN Git Service

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