OSDN Git Service

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