OSDN Git Service

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