OSDN Git Service

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