OSDN Git Service

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