1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2014 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6 
7 This file is part of GCC.
8 
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13 
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22 
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 	   the file numbers are used by .debug_info.  Alternately, leave
25 	   out locations for types and decls.
26 	 Avoid talking about ctors and op= for PODs.
27 	 Factor out common prologue sequences into multiple CIEs.  */
28 
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34 
35 /* DWARF2 Abbreviation Glossary:
36 
37    CFA = Canonical Frame Address
38 	   a fixed address on the stack which identifies a call frame.
39 	   We define it to be the value of SP just before the call insn.
40 	   The CFA register and offset, which may change during the course
41 	   of the function, are used to calculate its value at runtime.
42 
43    CFI = Call Frame Instruction
44 	   an instruction for the DWARF2 abstract machine
45 
46    CIE = Common Information Entry
47 	   information describing information common to one or more FDEs
48 
49    DIE = Debugging Information Entry
50 
51    FDE = Frame Description Entry
52 	   information describing the stack call frame, in particular,
53 	   how to restore registers
54 
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57 
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "tree-dfa.h"
99 #include "gdb/gdb-index.h"
100 
101 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
102 static rtx last_var_location_insn;
103 static rtx cached_next_real_insn;
104 
105 #ifdef VMS_DEBUGGING_INFO
106 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
107 
108 /* Define this macro to be a nonzero value if the directory specifications
109     which are output in the debug info should end with a separator.  */
110 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
111 /* Define this macro to evaluate to a nonzero value if GCC should refrain
112    from generating indirect strings in DWARF2 debug information, for instance
113    if your target is stuck with an old version of GDB that is unable to
114    process them properly or uses VMS Debug.  */
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
116 #else
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
119 #endif
120 
121 /* ??? Poison these here until it can be done generically.  They've been
122    totally replaced in this file; make sure it stays that way.  */
123 #undef DWARF2_UNWIND_INFO
124 #undef DWARF2_FRAME_INFO
125 #if (GCC_VERSION >= 3000)
126  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
127 #endif
128 
129 /* The size of the target's pointer type.  */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
133 
134 /* Array of RTXes referenced by the debugging information, which therefore
135    must be kept around forever.  */
136 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
137 
138 /* A pointer to the base of a list of incomplete types which might be
139    completed at some later time.  incomplete_types_list needs to be a
140    vec<tree, va_gc> *because we want to tell the garbage collector about
141    it.  */
142 static GTY(()) vec<tree, va_gc> *incomplete_types;
143 
144 /* A pointer to the base of a table of references to declaration
145    scopes.  This table is a display which tracks the nesting
146    of declaration scopes at the current scope and containing
147    scopes.  This table is used to find the proper place to
148    define type declaration DIE's.  */
149 static GTY(()) vec<tree, va_gc> *decl_scope_table;
150 
151 /* Pointers to various DWARF2 sections.  */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_skeleton_info_section;
154 static GTY(()) section *debug_abbrev_section;
155 static GTY(()) section *debug_skeleton_abbrev_section;
156 static GTY(()) section *debug_aranges_section;
157 static GTY(()) section *debug_addr_section;
158 static GTY(()) section *debug_macinfo_section;
159 static GTY(()) section *debug_line_section;
160 static GTY(()) section *debug_skeleton_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_str_dwo_section;
166 static GTY(()) section *debug_str_offsets_section;
167 static GTY(()) section *debug_ranges_section;
168 static GTY(()) section *debug_frame_section;
169 
170 /* Maximum size (in bytes) of an artificially generated label.  */
171 #define MAX_ARTIFICIAL_LABEL_BYTES	30
172 
173 /* According to the (draft) DWARF 3 specification, the initial length
174    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
175    bytes are 0xffffffff, followed by the length stored in the next 8
176    bytes.
177 
178    However, the SGI/MIPS ABI uses an initial length which is equal to
179    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
180 
181 #ifndef DWARF_INITIAL_LENGTH_SIZE
182 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
183 #endif
184 
185 /* Round SIZE up to the nearest BOUNDARY.  */
186 #define DWARF_ROUND(SIZE,BOUNDARY) \
187   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
188 
189 /* CIE identifier.  */
190 #if HOST_BITS_PER_WIDE_INT >= 64
191 #define DWARF_CIE_ID \
192   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
193 #else
194 #define DWARF_CIE_ID DW_CIE_ID
195 #endif
196 
197 
198 /* A vector for a table that contains frame description
199    information for each routine.  */
200 #define NOT_INDEXED (-1U)
201 #define NO_INDEX_ASSIGNED (-2U)
202 
203 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
204 
205 struct GTY(()) indirect_string_node {
206   const char *str;
207   unsigned int refcount;
208   enum dwarf_form form;
209   char *label;
210   unsigned int index;
211 };
212 
213 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
214 
215 /* With split_debug_info, both the comp_dir and dwo_name go in the
216    main object file, rather than the dwo, similar to the force_direct
217    parameter elsewhere but with additional complications:
218 
219    1) The string is needed in both the main object file and the dwo.
220    That is, the comp_dir and dwo_name will appear in both places.
221 
222    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
223    DW_FORM_GNU_str_index.
224 
225    3) GCC chooses the form to use late, depending on the size and
226    reference count.
227 
228    Rather than forcing the all debug string handling functions and
229    callers to deal with these complications, simply use a separate,
230    special-cased string table for any attribute that should go in the
231    main object file.  This limits the complexity to just the places
232    that need it.  */
233 
234 static GTY ((param_is (struct indirect_string_node)))
235   htab_t skeleton_debug_str_hash;
236 
237 static GTY(()) int dw2_string_counter;
238 
239 /* True if the compilation unit places functions in more than one section.  */
240 static GTY(()) bool have_multiple_function_sections = false;
241 
242 /* Whether the default text and cold text sections have been used at all.  */
243 
244 static GTY(()) bool text_section_used = false;
245 static GTY(()) bool cold_text_section_used = false;
246 
247 /* The default cold text section.  */
248 static GTY(()) section *cold_text_section;
249 
250 /* The DIE for C++1y 'auto' in a function return type.  */
251 static GTY(()) dw_die_ref auto_die;
252 
253 /* The DIE for C++1y 'decltype(auto)' in a function return type.  */
254 static GTY(()) dw_die_ref decltype_auto_die;
255 
256 /* Forward declarations for functions defined in this file.  */
257 
258 static char *stripattributes (const char *);
259 static void output_call_frame_info (int);
260 static void dwarf2out_note_section_used (void);
261 
262 /* Personality decl of current unit.  Used only when assembler does not support
263    personality CFI.  */
264 static GTY(()) rtx current_unit_personality;
265 
266 /* Data and reference forms for relocatable data.  */
267 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
268 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
269 
270 #ifndef DEBUG_FRAME_SECTION
271 #define DEBUG_FRAME_SECTION	".debug_frame"
272 #endif
273 
274 #ifndef FUNC_BEGIN_LABEL
275 #define FUNC_BEGIN_LABEL	"LFB"
276 #endif
277 
278 #ifndef FUNC_END_LABEL
279 #define FUNC_END_LABEL		"LFE"
280 #endif
281 
282 #ifndef PROLOGUE_END_LABEL
283 #define PROLOGUE_END_LABEL	"LPE"
284 #endif
285 
286 #ifndef EPILOGUE_BEGIN_LABEL
287 #define EPILOGUE_BEGIN_LABEL	"LEB"
288 #endif
289 
290 #ifndef FRAME_BEGIN_LABEL
291 #define FRAME_BEGIN_LABEL	"Lframe"
292 #endif
293 #define CIE_AFTER_SIZE_LABEL	"LSCIE"
294 #define CIE_END_LABEL		"LECIE"
295 #define FDE_LABEL		"LSFDE"
296 #define FDE_AFTER_SIZE_LABEL	"LASFDE"
297 #define FDE_END_LABEL		"LEFDE"
298 #define LINE_NUMBER_BEGIN_LABEL	"LSLT"
299 #define LINE_NUMBER_END_LABEL	"LELT"
300 #define LN_PROLOG_AS_LABEL	"LASLTP"
301 #define LN_PROLOG_END_LABEL	"LELTP"
302 #define DIE_LABEL_PREFIX	"DW"
303 
304 /* Match the base name of a file to the base name of a compilation unit. */
305 
306 static int
matches_main_base(const char * path)307 matches_main_base (const char *path)
308 {
309   /* Cache the last query. */
310   static const char *last_path = NULL;
311   static int last_match = 0;
312   if (path != last_path)
313     {
314       const char *base;
315       int length = base_of_path (path, &base);
316       last_path = path;
317       last_match = (length == main_input_baselength
318                     && memcmp (base, main_input_basename, length) == 0);
319     }
320   return last_match;
321 }
322 
323 #ifdef DEBUG_DEBUG_STRUCT
324 
325 static int
dump_struct_debug(tree type,enum debug_info_usage usage,enum debug_struct_file criterion,int generic,int matches,int result)326 dump_struct_debug (tree type, enum debug_info_usage usage,
327 		   enum debug_struct_file criterion, int generic,
328 		   int matches, int result)
329 {
330   /* Find the type name. */
331   tree type_decl = TYPE_STUB_DECL (type);
332   tree t = type_decl;
333   const char *name = 0;
334   if (TREE_CODE (t) == TYPE_DECL)
335     t = DECL_NAME (t);
336   if (t)
337     name = IDENTIFIER_POINTER (t);
338 
339   fprintf (stderr, "	struct %d %s %s %s %s %d %p %s\n",
340 	   criterion,
341            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
342            matches ? "bas" : "hdr",
343            generic ? "gen" : "ord",
344            usage == DINFO_USAGE_DFN ? ";" :
345              usage == DINFO_USAGE_DIR_USE ? "." : "*",
346            result,
347            (void*) type_decl, name);
348   return result;
349 }
350 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
351   dump_struct_debug (type, usage, criterion, generic, matches, result)
352 
353 #else
354 
355 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
356   (result)
357 
358 #endif
359 
360 static bool
should_emit_struct_debug(tree type,enum debug_info_usage usage)361 should_emit_struct_debug (tree type, enum debug_info_usage usage)
362 {
363   enum debug_struct_file criterion;
364   tree type_decl;
365   bool generic = lang_hooks.types.generic_p (type);
366 
367   if (generic)
368     criterion = debug_struct_generic[usage];
369   else
370     criterion = debug_struct_ordinary[usage];
371 
372   if (criterion == DINFO_STRUCT_FILE_NONE)
373     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
374   if (criterion == DINFO_STRUCT_FILE_ANY)
375     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
376 
377   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
378 
379   if (type_decl != NULL)
380     {
381      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
382         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
383 
384       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
385         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
386     }
387 
388   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
389 }
390 
391 /* Return a pointer to a copy of the section string name S with all
392    attributes stripped off, and an asterisk prepended (for assemble_name).  */
393 
394 static inline char *
stripattributes(const char * s)395 stripattributes (const char *s)
396 {
397   char *stripped = XNEWVEC (char, strlen (s) + 2);
398   char *p = stripped;
399 
400   *p++ = '*';
401 
402   while (*s && *s != ',')
403     *p++ = *s++;
404 
405   *p = '\0';
406   return stripped;
407 }
408 
409 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
410    switch to the data section instead, and write out a synthetic start label
411    for collect2 the first time around.  */
412 
413 static void
switch_to_eh_frame_section(bool back)414 switch_to_eh_frame_section (bool back)
415 {
416   tree label;
417 
418 #ifdef EH_FRAME_SECTION_NAME
419   if (eh_frame_section == 0)
420     {
421       int flags;
422 
423       if (EH_TABLES_CAN_BE_READ_ONLY)
424 	{
425 	  int fde_encoding;
426 	  int per_encoding;
427 	  int lsda_encoding;
428 
429 	  fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
430 						       /*global=*/0);
431 	  per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
432 						       /*global=*/1);
433 	  lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
434 							/*global=*/0);
435 	  flags = ((! flag_pic
436 		    || ((fde_encoding & 0x70) != DW_EH_PE_absptr
437 			&& (fde_encoding & 0x70) != DW_EH_PE_aligned
438 			&& (per_encoding & 0x70) != DW_EH_PE_absptr
439 			&& (per_encoding & 0x70) != DW_EH_PE_aligned
440 			&& (lsda_encoding & 0x70) != DW_EH_PE_absptr
441 			&& (lsda_encoding & 0x70) != DW_EH_PE_aligned))
442 		   ? 0 : SECTION_WRITE);
443 	}
444       else
445 	flags = SECTION_WRITE;
446       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
447     }
448 #endif /* EH_FRAME_SECTION_NAME */
449 
450   if (eh_frame_section)
451     switch_to_section (eh_frame_section);
452   else
453     {
454       /* We have no special eh_frame section.  Put the information in
455 	 the data section and emit special labels to guide collect2.  */
456       switch_to_section (data_section);
457 
458       if (!back)
459 	{
460 	  label = get_file_function_name ("F");
461 	  ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
462 	  targetm.asm_out.globalize_label (asm_out_file,
463 					   IDENTIFIER_POINTER (label));
464 	  ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
465 	}
466     }
467 }
468 
469 /* Switch [BACK] to the eh or debug frame table section, depending on
470    FOR_EH.  */
471 
472 static void
switch_to_frame_table_section(int for_eh,bool back)473 switch_to_frame_table_section (int for_eh, bool back)
474 {
475   if (for_eh)
476     switch_to_eh_frame_section (back);
477   else
478     {
479       if (!debug_frame_section)
480 	debug_frame_section = get_section (DEBUG_FRAME_SECTION,
481 					   SECTION_DEBUG, NULL);
482       switch_to_section (debug_frame_section);
483     }
484 }
485 
486 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
487 
488 enum dw_cfi_oprnd_type
dw_cfi_oprnd1_desc(enum dwarf_call_frame_info cfi)489 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
490 {
491   switch (cfi)
492     {
493     case DW_CFA_nop:
494     case DW_CFA_GNU_window_save:
495     case DW_CFA_remember_state:
496     case DW_CFA_restore_state:
497       return dw_cfi_oprnd_unused;
498 
499     case DW_CFA_set_loc:
500     case DW_CFA_advance_loc1:
501     case DW_CFA_advance_loc2:
502     case DW_CFA_advance_loc4:
503     case DW_CFA_MIPS_advance_loc8:
504       return dw_cfi_oprnd_addr;
505 
506     case DW_CFA_offset:
507     case DW_CFA_offset_extended:
508     case DW_CFA_def_cfa:
509     case DW_CFA_offset_extended_sf:
510     case DW_CFA_def_cfa_sf:
511     case DW_CFA_restore:
512     case DW_CFA_restore_extended:
513     case DW_CFA_undefined:
514     case DW_CFA_same_value:
515     case DW_CFA_def_cfa_register:
516     case DW_CFA_register:
517     case DW_CFA_expression:
518       return dw_cfi_oprnd_reg_num;
519 
520     case DW_CFA_def_cfa_offset:
521     case DW_CFA_GNU_args_size:
522     case DW_CFA_def_cfa_offset_sf:
523       return dw_cfi_oprnd_offset;
524 
525     case DW_CFA_def_cfa_expression:
526       return dw_cfi_oprnd_loc;
527 
528     default:
529       gcc_unreachable ();
530     }
531 }
532 
533 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
534 
535 enum dw_cfi_oprnd_type
dw_cfi_oprnd2_desc(enum dwarf_call_frame_info cfi)536 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
537 {
538   switch (cfi)
539     {
540     case DW_CFA_def_cfa:
541     case DW_CFA_def_cfa_sf:
542     case DW_CFA_offset:
543     case DW_CFA_offset_extended_sf:
544     case DW_CFA_offset_extended:
545       return dw_cfi_oprnd_offset;
546 
547     case DW_CFA_register:
548       return dw_cfi_oprnd_reg_num;
549 
550     case DW_CFA_expression:
551       return dw_cfi_oprnd_loc;
552 
553     default:
554       return dw_cfi_oprnd_unused;
555     }
556 }
557 
558 /* Output one FDE.  */
559 
560 static void
output_fde(dw_fde_ref fde,bool for_eh,bool second,char * section_start_label,int fde_encoding,char * augmentation,bool any_lsda_needed,int lsda_encoding)561 output_fde (dw_fde_ref fde, bool for_eh, bool second,
562 	    char *section_start_label, int fde_encoding, char *augmentation,
563 	    bool any_lsda_needed, int lsda_encoding)
564 {
565   const char *begin, *end;
566   static unsigned int j;
567   char l1[20], l2[20];
568 
569   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
570 				     /* empty */ 0);
571   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
572 				  for_eh + j);
573   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
574   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
575   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
576     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
577 			 " indicating 64-bit DWARF extension");
578   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
579 			"FDE Length");
580   ASM_OUTPUT_LABEL (asm_out_file, l1);
581 
582   if (for_eh)
583     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
584   else
585     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
586 			   debug_frame_section, "FDE CIE offset");
587 
588   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
589   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
590 
591   if (for_eh)
592     {
593       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
594       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
595       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
596 				       "FDE initial location");
597       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
598 			    end, begin, "FDE address range");
599     }
600   else
601     {
602       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
603       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
604     }
605 
606   if (augmentation[0])
607     {
608       if (any_lsda_needed)
609 	{
610 	  int size = size_of_encoded_value (lsda_encoding);
611 
612 	  if (lsda_encoding == DW_EH_PE_aligned)
613 	    {
614 	      int offset = (  4		/* Length */
615 			    + 4		/* CIE offset */
616 			    + 2 * size_of_encoded_value (fde_encoding)
617 			    + 1		/* Augmentation size */ );
618 	      int pad = -offset & (PTR_SIZE - 1);
619 
620 	      size += pad;
621 	      gcc_assert (size_of_uleb128 (size) == 1);
622 	    }
623 
624 	  dw2_asm_output_data_uleb128 (size, "Augmentation size");
625 
626 	  if (fde->uses_eh_lsda)
627 	    {
628 	      ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
629 					   fde->funcdef_number);
630 	      dw2_asm_output_encoded_addr_rtx (lsda_encoding,
631 					       gen_rtx_SYMBOL_REF (Pmode, l1),
632 					       false,
633 					       "Language Specific Data Area");
634 	    }
635 	  else
636 	    {
637 	      if (lsda_encoding == DW_EH_PE_aligned)
638 		ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
639 	      dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
640 				   "Language Specific Data Area (none)");
641 	    }
642 	}
643       else
644 	dw2_asm_output_data_uleb128 (0, "Augmentation size");
645     }
646 
647   /* Loop through the Call Frame Instructions associated with this FDE.  */
648   fde->dw_fde_current_label = begin;
649   {
650     size_t from, until, i;
651 
652     from = 0;
653     until = vec_safe_length (fde->dw_fde_cfi);
654 
655     if (fde->dw_fde_second_begin == NULL)
656       ;
657     else if (!second)
658       until = fde->dw_fde_switch_cfi_index;
659     else
660       from = fde->dw_fde_switch_cfi_index;
661 
662     for (i = from; i < until; i++)
663       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
664   }
665 
666   /* If we are to emit a ref/link from function bodies to their frame tables,
667      do it now.  This is typically performed to make sure that tables
668      associated with functions are dragged with them and not discarded in
669      garbage collecting links. We need to do this on a per function basis to
670      cope with -ffunction-sections.  */
671 
672 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
673   /* Switch to the function section, emit the ref to the tables, and
674      switch *back* into the table section.  */
675   switch_to_section (function_section (fde->decl));
676   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
677   switch_to_frame_table_section (for_eh, true);
678 #endif
679 
680   /* Pad the FDE out to an address sized boundary.  */
681   ASM_OUTPUT_ALIGN (asm_out_file,
682 		    floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
683   ASM_OUTPUT_LABEL (asm_out_file, l2);
684 
685   j += 2;
686 }
687 
688 /* Return true if frame description entry FDE is needed for EH.  */
689 
690 static bool
fde_needed_for_eh_p(dw_fde_ref fde)691 fde_needed_for_eh_p (dw_fde_ref fde)
692 {
693   if (flag_asynchronous_unwind_tables)
694     return true;
695 
696   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
697     return true;
698 
699   if (fde->uses_eh_lsda)
700     return true;
701 
702   /* If exceptions are enabled, we have collected nothrow info.  */
703   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
704     return false;
705 
706   return true;
707 }
708 
709 /* Output the call frame information used to record information
710    that relates to calculating the frame pointer, and records the
711    location of saved registers.  */
712 
713 static void
output_call_frame_info(int for_eh)714 output_call_frame_info (int for_eh)
715 {
716   unsigned int i;
717   dw_fde_ref fde;
718   dw_cfi_ref cfi;
719   char l1[20], l2[20], section_start_label[20];
720   bool any_lsda_needed = false;
721   char augmentation[6];
722   int augmentation_size;
723   int fde_encoding = DW_EH_PE_absptr;
724   int per_encoding = DW_EH_PE_absptr;
725   int lsda_encoding = DW_EH_PE_absptr;
726   int return_reg;
727   rtx personality = NULL;
728   int dw_cie_version;
729 
730   /* Don't emit a CIE if there won't be any FDEs.  */
731   if (!fde_vec)
732     return;
733 
734   /* Nothing to do if the assembler's doing it all.  */
735   if (dwarf2out_do_cfi_asm ())
736     return;
737 
738   /* If we don't have any functions we'll want to unwind out of, don't emit
739      any EH unwind information.  If we make FDEs linkonce, we may have to
740      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
741      want to avoid having an FDE kept around when the function it refers to
742      is discarded.  Example where this matters: a primary function template
743      in C++ requires EH information, an explicit specialization doesn't.  */
744   if (for_eh)
745     {
746       bool any_eh_needed = false;
747 
748       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
749 	{
750 	  if (fde->uses_eh_lsda)
751 	    any_eh_needed = any_lsda_needed = true;
752 	  else if (fde_needed_for_eh_p (fde))
753 	    any_eh_needed = true;
754 	  else if (TARGET_USES_WEAK_UNWIND_INFO)
755 	    targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
756 	}
757 
758       if (!any_eh_needed)
759 	return;
760     }
761 
762   /* We're going to be generating comments, so turn on app.  */
763   if (flag_debug_asm)
764     app_enable ();
765 
766   /* Switch to the proper frame section, first time.  */
767   switch_to_frame_table_section (for_eh, false);
768 
769   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
770   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
771 
772   /* Output the CIE.  */
773   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
774   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
775   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
776     dw2_asm_output_data (4, 0xffffffff,
777       "Initial length escape value indicating 64-bit DWARF extension");
778   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
779 			"Length of Common Information Entry");
780   ASM_OUTPUT_LABEL (asm_out_file, l1);
781 
782   /* Now that the CIE pointer is PC-relative for EH,
783      use 0 to identify the CIE.  */
784   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
785 		       (for_eh ? 0 : DWARF_CIE_ID),
786 		       "CIE Identifier Tag");
787 
788   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
789      use CIE version 1, unless that would produce incorrect results
790      due to overflowing the return register column.  */
791   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
792   dw_cie_version = 1;
793   if (return_reg >= 256 || dwarf_version > 2)
794     dw_cie_version = 3;
795   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
796 
797   augmentation[0] = 0;
798   augmentation_size = 0;
799 
800   personality = current_unit_personality;
801   if (for_eh)
802     {
803       char *p;
804 
805       /* Augmentation:
806 	 z	Indicates that a uleb128 is present to size the
807 		augmentation section.
808 	 L	Indicates the encoding (and thus presence) of
809 		an LSDA pointer in the FDE augmentation.
810 	 R	Indicates a non-default pointer encoding for
811 		FDE code pointers.
812 	 P	Indicates the presence of an encoding + language
813 		personality routine in the CIE augmentation.  */
814 
815       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
816       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
817       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
818 
819       p = augmentation + 1;
820       if (personality)
821 	{
822 	  *p++ = 'P';
823 	  augmentation_size += 1 + size_of_encoded_value (per_encoding);
824 	  assemble_external_libcall (personality);
825 	}
826       if (any_lsda_needed)
827 	{
828 	  *p++ = 'L';
829 	  augmentation_size += 1;
830 	}
831       if (fde_encoding != DW_EH_PE_absptr)
832 	{
833 	  *p++ = 'R';
834 	  augmentation_size += 1;
835 	}
836       if (p > augmentation + 1)
837 	{
838 	  augmentation[0] = 'z';
839 	  *p = '\0';
840 	}
841 
842       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
843       if (personality && per_encoding == DW_EH_PE_aligned)
844 	{
845 	  int offset = (  4		/* Length */
846 			+ 4		/* CIE Id */
847 			+ 1		/* CIE version */
848 			+ strlen (augmentation) + 1	/* Augmentation */
849 			+ size_of_uleb128 (1)		/* Code alignment */
850 			+ size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
851 			+ 1		/* RA column */
852 			+ 1		/* Augmentation size */
853 			+ 1		/* Personality encoding */ );
854 	  int pad = -offset & (PTR_SIZE - 1);
855 
856 	  augmentation_size += pad;
857 
858 	  /* Augmentations should be small, so there's scarce need to
859 	     iterate for a solution.  Die if we exceed one uleb128 byte.  */
860 	  gcc_assert (size_of_uleb128 (augmentation_size) == 1);
861 	}
862     }
863 
864   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
865   if (dw_cie_version >= 4)
866     {
867       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
868       dw2_asm_output_data (1, 0, "CIE Segment Size");
869     }
870   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
871   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
872 			       "CIE Data Alignment Factor");
873 
874   if (dw_cie_version == 1)
875     dw2_asm_output_data (1, return_reg, "CIE RA Column");
876   else
877     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
878 
879   if (augmentation[0])
880     {
881       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
882       if (personality)
883 	{
884 	  dw2_asm_output_data (1, per_encoding, "Personality (%s)",
885 			       eh_data_format_name (per_encoding));
886 	  dw2_asm_output_encoded_addr_rtx (per_encoding,
887 					   personality,
888 					   true, NULL);
889 	}
890 
891       if (any_lsda_needed)
892 	dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
893 			     eh_data_format_name (lsda_encoding));
894 
895       if (fde_encoding != DW_EH_PE_absptr)
896 	dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
897 			     eh_data_format_name (fde_encoding));
898     }
899 
900   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
901     output_cfi (cfi, NULL, for_eh);
902 
903   /* Pad the CIE out to an address sized boundary.  */
904   ASM_OUTPUT_ALIGN (asm_out_file,
905 		    floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
906   ASM_OUTPUT_LABEL (asm_out_file, l2);
907 
908   /* Loop through all of the FDE's.  */
909   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
910     {
911       unsigned int k;
912 
913       /* Don't emit EH unwind info for leaf functions that don't need it.  */
914       if (for_eh && !fde_needed_for_eh_p (fde))
915 	continue;
916 
917       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
918 	output_fde (fde, for_eh, k, section_start_label, fde_encoding,
919 		    augmentation, any_lsda_needed, lsda_encoding);
920     }
921 
922   if (for_eh && targetm.terminate_dw2_eh_frame_info)
923     dw2_asm_output_data (4, 0, "End of Table");
924 
925   /* Turn off app to make assembly quicker.  */
926   if (flag_debug_asm)
927     app_disable ();
928 }
929 
930 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
931 
932 static void
dwarf2out_do_cfi_startproc(bool second)933 dwarf2out_do_cfi_startproc (bool second)
934 {
935   int enc;
936   rtx ref;
937   rtx personality = get_personality_function (current_function_decl);
938 
939   fprintf (asm_out_file, "\t.cfi_startproc\n");
940 
941   if (personality)
942     {
943       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
944       ref = personality;
945 
946       /* ??? The GAS support isn't entirely consistent.  We have to
947 	 handle indirect support ourselves, but PC-relative is done
948 	 in the assembler.  Further, the assembler can't handle any
949 	 of the weirder relocation types.  */
950       if (enc & DW_EH_PE_indirect)
951 	ref = dw2_force_const_mem (ref, true);
952 
953       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
954       output_addr_const (asm_out_file, ref);
955       fputc ('\n', asm_out_file);
956     }
957 
958   if (crtl->uses_eh_lsda)
959     {
960       char lab[20];
961 
962       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
963       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
964 				   current_function_funcdef_no);
965       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
966       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
967 
968       if (enc & DW_EH_PE_indirect)
969 	ref = dw2_force_const_mem (ref, true);
970 
971       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
972       output_addr_const (asm_out_file, ref);
973       fputc ('\n', asm_out_file);
974     }
975 }
976 
977 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
978    this allocation may be done before pass_final.  */
979 
980 dw_fde_ref
dwarf2out_alloc_current_fde(void)981 dwarf2out_alloc_current_fde (void)
982 {
983   dw_fde_ref fde;
984 
985   fde = ggc_alloc_cleared_dw_fde_node ();
986   fde->decl = current_function_decl;
987   fde->funcdef_number = current_function_funcdef_no;
988   fde->fde_index = vec_safe_length (fde_vec);
989   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
990   fde->uses_eh_lsda = crtl->uses_eh_lsda;
991   fde->nothrow = crtl->nothrow;
992   fde->drap_reg = INVALID_REGNUM;
993   fde->vdrap_reg = INVALID_REGNUM;
994 
995   /* Record the FDE associated with this function.  */
996   cfun->fde = fde;
997   vec_safe_push (fde_vec, fde);
998 
999   return fde;
1000 }
1001 
1002 /* Output a marker (i.e. a label) for the beginning of a function, before
1003    the prologue.  */
1004 
1005 void
dwarf2out_begin_prologue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1006 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1007 			  const char *file ATTRIBUTE_UNUSED)
1008 {
1009   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1010   char * dup_label;
1011   dw_fde_ref fde;
1012   section *fnsec;
1013   bool do_frame;
1014 
1015   current_function_func_begin_label = NULL;
1016 
1017   do_frame = dwarf2out_do_frame ();
1018 
1019   /* ??? current_function_func_begin_label is also used by except.c for
1020      call-site information.  We must emit this label if it might be used.  */
1021   if (!do_frame
1022       && (!flag_exceptions
1023 	  || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1024     return;
1025 
1026   fnsec = function_section (current_function_decl);
1027   switch_to_section (fnsec);
1028   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1029 			       current_function_funcdef_no);
1030   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1031 			  current_function_funcdef_no);
1032   dup_label = xstrdup (label);
1033   current_function_func_begin_label = dup_label;
1034 
1035   /* We can elide the fde allocation if we're not emitting debug info.  */
1036   if (!do_frame)
1037     return;
1038 
1039   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1040      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1041      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1042      do so now.  */
1043   fde = cfun->fde;
1044   if (fde == NULL)
1045     fde = dwarf2out_alloc_current_fde ();
1046 
1047   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1048   fde->dw_fde_begin = dup_label;
1049   fde->dw_fde_current_label = dup_label;
1050   fde->in_std_section = (fnsec == text_section
1051 			 || (cold_text_section && fnsec == cold_text_section));
1052 
1053   /* We only want to output line number information for the genuine dwarf2
1054      prologue case, not the eh frame case.  */
1055 #ifdef DWARF2_DEBUGGING_INFO
1056   if (file)
1057     dwarf2out_source_line (line, file, 0, true);
1058 #endif
1059 
1060   if (dwarf2out_do_cfi_asm ())
1061     dwarf2out_do_cfi_startproc (false);
1062   else
1063     {
1064       rtx personality = get_personality_function (current_function_decl);
1065       if (!current_unit_personality)
1066         current_unit_personality = personality;
1067 
1068       /* We cannot keep a current personality per function as without CFI
1069 	 asm, at the point where we emit the CFI data, there is no current
1070 	 function anymore.  */
1071       if (personality && current_unit_personality != personality)
1072 	sorry ("multiple EH personalities are supported only with assemblers "
1073 	       "supporting .cfi_personality directive");
1074     }
1075 }
1076 
1077 /* Output a marker (i.e. a label) for the end of the generated code
1078    for a function prologue.  This gets called *after* the prologue code has
1079    been generated.  */
1080 
1081 void
dwarf2out_vms_end_prologue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1082 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1083 			const char *file ATTRIBUTE_UNUSED)
1084 {
1085   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1086 
1087   /* Output a label to mark the endpoint of the code generated for this
1088      function.  */
1089   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1090 			       current_function_funcdef_no);
1091   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1092 			  current_function_funcdef_no);
1093   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1094 }
1095 
1096 /* Output a marker (i.e. a label) for the beginning of the generated code
1097    for a function epilogue.  This gets called *before* the prologue code has
1098    been generated.  */
1099 
1100 void
dwarf2out_vms_begin_epilogue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1101 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1102 			  const char *file ATTRIBUTE_UNUSED)
1103 {
1104   dw_fde_ref fde = cfun->fde;
1105   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1106 
1107   if (fde->dw_fde_vms_begin_epilogue)
1108     return;
1109 
1110   /* Output a label to mark the endpoint of the code generated for this
1111      function.  */
1112   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1113 			       current_function_funcdef_no);
1114   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1115 			  current_function_funcdef_no);
1116   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1117 }
1118 
1119 /* Output a marker (i.e. a label) for the absolute end of the generated code
1120    for a function definition.  This gets called *after* the epilogue code has
1121    been generated.  */
1122 
1123 void
dwarf2out_end_epilogue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1124 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1125 			const char *file ATTRIBUTE_UNUSED)
1126 {
1127   dw_fde_ref fde;
1128   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1129 
1130   last_var_location_insn = NULL_RTX;
1131   cached_next_real_insn = NULL_RTX;
1132 
1133   if (dwarf2out_do_cfi_asm ())
1134     fprintf (asm_out_file, "\t.cfi_endproc\n");
1135 
1136   /* Output a label to mark the endpoint of the code generated for this
1137      function.  */
1138   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1139 			       current_function_funcdef_no);
1140   ASM_OUTPUT_LABEL (asm_out_file, label);
1141   fde = cfun->fde;
1142   gcc_assert (fde != NULL);
1143   if (fde->dw_fde_second_begin == NULL)
1144     fde->dw_fde_end = xstrdup (label);
1145 }
1146 
1147 void
dwarf2out_frame_finish(void)1148 dwarf2out_frame_finish (void)
1149 {
1150   /* Output call frame information.  */
1151   if (targetm.debug_unwind_info () == UI_DWARF2)
1152     output_call_frame_info (0);
1153 
1154   /* Output another copy for the unwinder.  */
1155   if ((flag_unwind_tables || flag_exceptions)
1156       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1157     output_call_frame_info (1);
1158 }
1159 
1160 /* Note that the current function section is being used for code.  */
1161 
1162 static void
dwarf2out_note_section_used(void)1163 dwarf2out_note_section_used (void)
1164 {
1165   section *sec = current_function_section ();
1166   if (sec == text_section)
1167     text_section_used = true;
1168   else if (sec == cold_text_section)
1169     cold_text_section_used = true;
1170 }
1171 
1172 static void var_location_switch_text_section (void);
1173 static void set_cur_line_info_table (section *);
1174 
1175 void
dwarf2out_switch_text_section(void)1176 dwarf2out_switch_text_section (void)
1177 {
1178   section *sect;
1179   dw_fde_ref fde = cfun->fde;
1180 
1181   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1182 
1183   if (!in_cold_section_p)
1184     {
1185       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1186       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1187       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1188     }
1189   else
1190     {
1191       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1192       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1193       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1194     }
1195   have_multiple_function_sections = true;
1196 
1197   /* There is no need to mark used sections when not debugging.  */
1198   if (cold_text_section != NULL)
1199     dwarf2out_note_section_used ();
1200 
1201   if (dwarf2out_do_cfi_asm ())
1202     fprintf (asm_out_file, "\t.cfi_endproc\n");
1203 
1204   /* Now do the real section switch.  */
1205   sect = current_function_section ();
1206   switch_to_section (sect);
1207 
1208   fde->second_in_std_section
1209     = (sect == text_section
1210        || (cold_text_section && sect == cold_text_section));
1211 
1212   if (dwarf2out_do_cfi_asm ())
1213     dwarf2out_do_cfi_startproc (true);
1214 
1215   var_location_switch_text_section ();
1216 
1217   if (cold_text_section != NULL)
1218     set_cur_line_info_table (sect);
1219 }
1220 
1221 /* And now, the subset of the debugging information support code necessary
1222    for emitting location expressions.  */
1223 
1224 /* Data about a single source file.  */
1225 struct GTY(()) dwarf_file_data {
1226   const char * filename;
1227   int emitted_number;
1228 };
1229 
1230 typedef struct GTY(()) deferred_locations_struct
1231 {
1232   tree variable;
1233   dw_die_ref die;
1234 } deferred_locations;
1235 
1236 
1237 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1238 
1239 
1240 /* Describe an entry into the .debug_addr section.  */
1241 
1242 enum ate_kind {
1243   ate_kind_rtx,
1244   ate_kind_rtx_dtprel,
1245   ate_kind_label
1246 };
1247 
1248 typedef struct GTY(()) addr_table_entry_struct {
1249   enum ate_kind kind;
1250   unsigned int refcount;
1251   unsigned int index;
1252   union addr_table_entry_struct_union
1253     {
1254       rtx GTY ((tag ("0"))) rtl;
1255       char * GTY ((tag ("1"))) label;
1256     }
1257   GTY ((desc ("%1.kind"))) addr;
1258 }
1259 addr_table_entry;
1260 
1261 /* Location lists are ranges + location descriptions for that range,
1262    so you can track variables that are in different places over
1263    their entire life.  */
1264 typedef struct GTY(()) dw_loc_list_struct {
1265   dw_loc_list_ref dw_loc_next;
1266   const char *begin; /* Label and addr_entry for start of range */
1267   addr_table_entry *begin_entry;
1268   const char *end;  /* Label for end of range */
1269   char *ll_symbol; /* Label for beginning of location list.
1270 		      Only on head of list */
1271   const char *section; /* Section this loclist is relative to */
1272   dw_loc_descr_ref expr;
1273   hashval_t hash;
1274   /* True if all addresses in this and subsequent lists are known to be
1275      resolved.  */
1276   bool resolved_addr;
1277   /* True if this list has been replaced by dw_loc_next.  */
1278   bool replaced;
1279   bool emitted;
1280   /* True if the range should be emitted even if begin and end
1281      are the same.  */
1282   bool force;
1283 } dw_loc_list_node;
1284 
1285 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1286 
1287 /* Convert a DWARF stack opcode into its string name.  */
1288 
1289 static const char *
dwarf_stack_op_name(unsigned int op)1290 dwarf_stack_op_name (unsigned int op)
1291 {
1292   const char *name = get_DW_OP_name (op);
1293 
1294   if (name != NULL)
1295     return name;
1296 
1297   return "OP_<unknown>";
1298 }
1299 
1300 /* Return a pointer to a newly allocated location description.  Location
1301    descriptions are simple expression terms that can be strung
1302    together to form more complicated location (address) descriptions.  */
1303 
1304 static inline dw_loc_descr_ref
new_loc_descr(enum dwarf_location_atom op,unsigned HOST_WIDE_INT oprnd1,unsigned HOST_WIDE_INT oprnd2)1305 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1306 	       unsigned HOST_WIDE_INT oprnd2)
1307 {
1308   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1309 
1310   descr->dw_loc_opc = op;
1311   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1312   descr->dw_loc_oprnd1.val_entry = NULL;
1313   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1314   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1315   descr->dw_loc_oprnd2.val_entry = NULL;
1316   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1317 
1318   return descr;
1319 }
1320 
1321 /* Return a pointer to a newly allocated location description for
1322    REG and OFFSET.  */
1323 
1324 static inline dw_loc_descr_ref
new_reg_loc_descr(unsigned int reg,unsigned HOST_WIDE_INT offset)1325 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1326 {
1327   if (reg <= 31)
1328     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1329 			  offset, 0);
1330   else
1331     return new_loc_descr (DW_OP_bregx, reg, offset);
1332 }
1333 
1334 /* Add a location description term to a location description expression.  */
1335 
1336 static inline void
add_loc_descr(dw_loc_descr_ref * list_head,dw_loc_descr_ref descr)1337 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1338 {
1339   dw_loc_descr_ref *d;
1340 
1341   /* Find the end of the chain.  */
1342   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1343     ;
1344 
1345   *d = descr;
1346 }
1347 
1348 /* Compare two location operands for exact equality.  */
1349 
1350 static bool
dw_val_equal_p(dw_val_node * a,dw_val_node * b)1351 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1352 {
1353   if (a->val_class != b->val_class)
1354     return false;
1355   switch (a->val_class)
1356     {
1357     case dw_val_class_none:
1358       return true;
1359     case dw_val_class_addr:
1360       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1361 
1362     case dw_val_class_offset:
1363     case dw_val_class_unsigned_const:
1364     case dw_val_class_const:
1365     case dw_val_class_range_list:
1366     case dw_val_class_lineptr:
1367     case dw_val_class_macptr:
1368       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1369       return a->v.val_unsigned == b->v.val_unsigned;
1370 
1371     case dw_val_class_loc:
1372       return a->v.val_loc == b->v.val_loc;
1373     case dw_val_class_loc_list:
1374       return a->v.val_loc_list == b->v.val_loc_list;
1375     case dw_val_class_die_ref:
1376       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1377     case dw_val_class_fde_ref:
1378       return a->v.val_fde_index == b->v.val_fde_index;
1379     case dw_val_class_lbl_id:
1380     case dw_val_class_high_pc:
1381       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1382     case dw_val_class_str:
1383       return a->v.val_str == b->v.val_str;
1384     case dw_val_class_flag:
1385       return a->v.val_flag == b->v.val_flag;
1386     case dw_val_class_file:
1387       return a->v.val_file == b->v.val_file;
1388     case dw_val_class_decl_ref:
1389       return a->v.val_decl_ref == b->v.val_decl_ref;
1390 
1391     case dw_val_class_const_double:
1392       return (a->v.val_double.high == b->v.val_double.high
1393 	      && a->v.val_double.low == b->v.val_double.low);
1394 
1395     case dw_val_class_vec:
1396       {
1397 	size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1398 	size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1399 
1400 	return (a_len == b_len
1401 		&& !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1402       }
1403 
1404     case dw_val_class_data8:
1405       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1406 
1407     case dw_val_class_vms_delta:
1408       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1409               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1410     }
1411   gcc_unreachable ();
1412 }
1413 
1414 /* Compare two location atoms for exact equality.  */
1415 
1416 static bool
loc_descr_equal_p_1(dw_loc_descr_ref a,dw_loc_descr_ref b)1417 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1418 {
1419   if (a->dw_loc_opc != b->dw_loc_opc)
1420     return false;
1421 
1422   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1423      address size, but since we always allocate cleared storage it
1424      should be zero for other types of locations.  */
1425   if (a->dtprel != b->dtprel)
1426     return false;
1427 
1428   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1429 	  && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1430 }
1431 
1432 /* Compare two complete location expressions for exact equality.  */
1433 
1434 bool
loc_descr_equal_p(dw_loc_descr_ref a,dw_loc_descr_ref b)1435 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1436 {
1437   while (1)
1438     {
1439       if (a == b)
1440 	return true;
1441       if (a == NULL || b == NULL)
1442 	return false;
1443       if (!loc_descr_equal_p_1 (a, b))
1444 	return false;
1445 
1446       a = a->dw_loc_next;
1447       b = b->dw_loc_next;
1448     }
1449 }
1450 
1451 
1452 /* Add a constant OFFSET to a location expression.  */
1453 
1454 static void
loc_descr_plus_const(dw_loc_descr_ref * list_head,HOST_WIDE_INT offset)1455 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1456 {
1457   dw_loc_descr_ref loc;
1458   HOST_WIDE_INT *p;
1459 
1460   gcc_assert (*list_head != NULL);
1461 
1462   if (!offset)
1463     return;
1464 
1465   /* Find the end of the chain.  */
1466   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1467     ;
1468 
1469   p = NULL;
1470   if (loc->dw_loc_opc == DW_OP_fbreg
1471       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1472     p = &loc->dw_loc_oprnd1.v.val_int;
1473   else if (loc->dw_loc_opc == DW_OP_bregx)
1474     p = &loc->dw_loc_oprnd2.v.val_int;
1475 
1476   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1477      offset.  Don't optimize if an signed integer overflow would happen.  */
1478   if (p != NULL
1479       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1480 	  || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1481     *p += offset;
1482 
1483   else if (offset > 0)
1484     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1485 
1486   else
1487     {
1488       loc->dw_loc_next = int_loc_descriptor (-offset);
1489       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1490     }
1491 }
1492 
1493 /* Add a constant OFFSET to a location list.  */
1494 
1495 static void
loc_list_plus_const(dw_loc_list_ref list_head,HOST_WIDE_INT offset)1496 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1497 {
1498   dw_loc_list_ref d;
1499   for (d = list_head; d != NULL; d = d->dw_loc_next)
1500     loc_descr_plus_const (&d->expr, offset);
1501 }
1502 
1503 #define DWARF_REF_SIZE	\
1504   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1505 
1506 static unsigned long int get_base_type_offset (dw_die_ref);
1507 
1508 /* Return the size of a location descriptor.  */
1509 
1510 static unsigned long
size_of_loc_descr(dw_loc_descr_ref loc)1511 size_of_loc_descr (dw_loc_descr_ref loc)
1512 {
1513   unsigned long size = 1;
1514 
1515   switch (loc->dw_loc_opc)
1516     {
1517     case DW_OP_addr:
1518       size += DWARF2_ADDR_SIZE;
1519       break;
1520     case DW_OP_GNU_addr_index:
1521     case DW_OP_GNU_const_index:
1522       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1523       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1524       break;
1525     case DW_OP_const1u:
1526     case DW_OP_const1s:
1527       size += 1;
1528       break;
1529     case DW_OP_const2u:
1530     case DW_OP_const2s:
1531       size += 2;
1532       break;
1533     case DW_OP_const4u:
1534     case DW_OP_const4s:
1535       size += 4;
1536       break;
1537     case DW_OP_const8u:
1538     case DW_OP_const8s:
1539       size += 8;
1540       break;
1541     case DW_OP_constu:
1542       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1543       break;
1544     case DW_OP_consts:
1545       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1546       break;
1547     case DW_OP_pick:
1548       size += 1;
1549       break;
1550     case DW_OP_plus_uconst:
1551       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1552       break;
1553     case DW_OP_skip:
1554     case DW_OP_bra:
1555       size += 2;
1556       break;
1557     case DW_OP_breg0:
1558     case DW_OP_breg1:
1559     case DW_OP_breg2:
1560     case DW_OP_breg3:
1561     case DW_OP_breg4:
1562     case DW_OP_breg5:
1563     case DW_OP_breg6:
1564     case DW_OP_breg7:
1565     case DW_OP_breg8:
1566     case DW_OP_breg9:
1567     case DW_OP_breg10:
1568     case DW_OP_breg11:
1569     case DW_OP_breg12:
1570     case DW_OP_breg13:
1571     case DW_OP_breg14:
1572     case DW_OP_breg15:
1573     case DW_OP_breg16:
1574     case DW_OP_breg17:
1575     case DW_OP_breg18:
1576     case DW_OP_breg19:
1577     case DW_OP_breg20:
1578     case DW_OP_breg21:
1579     case DW_OP_breg22:
1580     case DW_OP_breg23:
1581     case DW_OP_breg24:
1582     case DW_OP_breg25:
1583     case DW_OP_breg26:
1584     case DW_OP_breg27:
1585     case DW_OP_breg28:
1586     case DW_OP_breg29:
1587     case DW_OP_breg30:
1588     case DW_OP_breg31:
1589       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590       break;
1591     case DW_OP_regx:
1592       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1593       break;
1594     case DW_OP_fbreg:
1595       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1596       break;
1597     case DW_OP_bregx:
1598       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1599       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1600       break;
1601     case DW_OP_piece:
1602       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1603       break;
1604     case DW_OP_bit_piece:
1605       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1606       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1607       break;
1608     case DW_OP_deref_size:
1609     case DW_OP_xderef_size:
1610       size += 1;
1611       break;
1612     case DW_OP_call2:
1613       size += 2;
1614       break;
1615     case DW_OP_call4:
1616       size += 4;
1617       break;
1618     case DW_OP_call_ref:
1619       size += DWARF_REF_SIZE;
1620       break;
1621     case DW_OP_implicit_value:
1622       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1623 	      + loc->dw_loc_oprnd1.v.val_unsigned;
1624       break;
1625     case DW_OP_GNU_implicit_pointer:
1626       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1627       break;
1628     case DW_OP_GNU_entry_value:
1629       {
1630 	unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1631 	size += size_of_uleb128 (op_size) + op_size;
1632 	break;
1633       }
1634     case DW_OP_GNU_const_type:
1635       {
1636 	unsigned long o
1637 	  = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1638 	size += size_of_uleb128 (o) + 1;
1639 	switch (loc->dw_loc_oprnd2.val_class)
1640 	  {
1641 	  case dw_val_class_vec:
1642 	    size += loc->dw_loc_oprnd2.v.val_vec.length
1643 		    * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1644 	    break;
1645 	  case dw_val_class_const:
1646 	    size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1647 	    break;
1648 	  case dw_val_class_const_double:
1649 	    size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1650 	    break;
1651 	  default:
1652 	    gcc_unreachable ();
1653 	  }
1654 	break;
1655       }
1656     case DW_OP_GNU_regval_type:
1657       {
1658 	unsigned long o
1659 	  = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1660 	size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1661 		+ size_of_uleb128 (o);
1662       }
1663       break;
1664     case DW_OP_GNU_deref_type:
1665       {
1666 	unsigned long o
1667 	  = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1668 	size += 1 + size_of_uleb128 (o);
1669       }
1670       break;
1671     case DW_OP_GNU_convert:
1672     case DW_OP_GNU_reinterpret:
1673       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1674 	size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1675       else
1676 	{
1677 	  unsigned long o
1678 	    = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1679 	  size += size_of_uleb128 (o);
1680 	}
1681       break;
1682     case DW_OP_GNU_parameter_ref:
1683       size += 4;
1684       break;
1685     default:
1686       break;
1687     }
1688 
1689   return size;
1690 }
1691 
1692 /* Return the size of a series of location descriptors.  */
1693 
1694 unsigned long
size_of_locs(dw_loc_descr_ref loc)1695 size_of_locs (dw_loc_descr_ref loc)
1696 {
1697   dw_loc_descr_ref l;
1698   unsigned long size;
1699 
1700   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1701      field, to avoid writing to a PCH file.  */
1702   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1703     {
1704       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1705 	break;
1706       size += size_of_loc_descr (l);
1707     }
1708   if (! l)
1709     return size;
1710 
1711   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1712     {
1713       l->dw_loc_addr = size;
1714       size += size_of_loc_descr (l);
1715     }
1716 
1717   return size;
1718 }
1719 
1720 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1721 static void get_ref_die_offset_label (char *, dw_die_ref);
1722 static unsigned long int get_ref_die_offset (dw_die_ref);
1723 
1724 /* Output location description stack opcode's operands (if any).
1725    The for_eh_or_skip parameter controls whether register numbers are
1726    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1727    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1728    info).  This should be suppressed for the cases that have not been converted
1729    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1730 
1731 static void
output_loc_operands(dw_loc_descr_ref loc,int for_eh_or_skip)1732 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1733 {
1734   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1735   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1736 
1737   switch (loc->dw_loc_opc)
1738     {
1739 #ifdef DWARF2_DEBUGGING_INFO
1740     case DW_OP_const2u:
1741     case DW_OP_const2s:
1742       dw2_asm_output_data (2, val1->v.val_int, NULL);
1743       break;
1744     case DW_OP_const4u:
1745       if (loc->dtprel)
1746 	{
1747 	  gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1748 	  targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1749 					       val1->v.val_addr);
1750 	  fputc ('\n', asm_out_file);
1751 	  break;
1752 	}
1753       /* FALLTHRU */
1754     case DW_OP_const4s:
1755       dw2_asm_output_data (4, val1->v.val_int, NULL);
1756       break;
1757     case DW_OP_const8u:
1758       if (loc->dtprel)
1759 	{
1760 	  gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1761 	  targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1762 					       val1->v.val_addr);
1763 	  fputc ('\n', asm_out_file);
1764 	  break;
1765 	}
1766       /* FALLTHRU */
1767     case DW_OP_const8s:
1768       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1769       dw2_asm_output_data (8, val1->v.val_int, NULL);
1770       break;
1771     case DW_OP_skip:
1772     case DW_OP_bra:
1773       {
1774 	int offset;
1775 
1776 	gcc_assert (val1->val_class == dw_val_class_loc);
1777 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1778 
1779 	dw2_asm_output_data (2, offset, NULL);
1780       }
1781       break;
1782     case DW_OP_implicit_value:
1783       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1784       switch (val2->val_class)
1785 	{
1786 	case dw_val_class_const:
1787 	  dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1788 	  break;
1789 	case dw_val_class_vec:
1790 	  {
1791 	    unsigned int elt_size = val2->v.val_vec.elt_size;
1792 	    unsigned int len = val2->v.val_vec.length;
1793 	    unsigned int i;
1794 	    unsigned char *p;
1795 
1796 	    if (elt_size > sizeof (HOST_WIDE_INT))
1797 	      {
1798 		elt_size /= 2;
1799 		len *= 2;
1800 	      }
1801 	    for (i = 0, p = val2->v.val_vec.array;
1802 		 i < len;
1803 		 i++, p += elt_size)
1804 	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1805 				   "fp or vector constant word %u", i);
1806 	  }
1807 	  break;
1808 	case dw_val_class_const_double:
1809 	  {
1810 	    unsigned HOST_WIDE_INT first, second;
1811 
1812 	    if (WORDS_BIG_ENDIAN)
1813 	      {
1814 		first = val2->v.val_double.high;
1815 		second = val2->v.val_double.low;
1816 	      }
1817 	    else
1818 	      {
1819 		first = val2->v.val_double.low;
1820 		second = val2->v.val_double.high;
1821 	      }
1822 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1823 				 first, NULL);
1824 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1825 				 second, NULL);
1826 	  }
1827 	  break;
1828 	case dw_val_class_addr:
1829 	  gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1830 	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1831 	  break;
1832 	default:
1833 	  gcc_unreachable ();
1834 	}
1835       break;
1836 #else
1837     case DW_OP_const2u:
1838     case DW_OP_const2s:
1839     case DW_OP_const4u:
1840     case DW_OP_const4s:
1841     case DW_OP_const8u:
1842     case DW_OP_const8s:
1843     case DW_OP_skip:
1844     case DW_OP_bra:
1845     case DW_OP_implicit_value:
1846       /* We currently don't make any attempt to make sure these are
1847 	 aligned properly like we do for the main unwind info, so
1848 	 don't support emitting things larger than a byte if we're
1849 	 only doing unwinding.  */
1850       gcc_unreachable ();
1851 #endif
1852     case DW_OP_const1u:
1853     case DW_OP_const1s:
1854       dw2_asm_output_data (1, val1->v.val_int, NULL);
1855       break;
1856     case DW_OP_constu:
1857       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1858       break;
1859     case DW_OP_consts:
1860       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1861       break;
1862     case DW_OP_pick:
1863       dw2_asm_output_data (1, val1->v.val_int, NULL);
1864       break;
1865     case DW_OP_plus_uconst:
1866       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1867       break;
1868     case DW_OP_breg0:
1869     case DW_OP_breg1:
1870     case DW_OP_breg2:
1871     case DW_OP_breg3:
1872     case DW_OP_breg4:
1873     case DW_OP_breg5:
1874     case DW_OP_breg6:
1875     case DW_OP_breg7:
1876     case DW_OP_breg8:
1877     case DW_OP_breg9:
1878     case DW_OP_breg10:
1879     case DW_OP_breg11:
1880     case DW_OP_breg12:
1881     case DW_OP_breg13:
1882     case DW_OP_breg14:
1883     case DW_OP_breg15:
1884     case DW_OP_breg16:
1885     case DW_OP_breg17:
1886     case DW_OP_breg18:
1887     case DW_OP_breg19:
1888     case DW_OP_breg20:
1889     case DW_OP_breg21:
1890     case DW_OP_breg22:
1891     case DW_OP_breg23:
1892     case DW_OP_breg24:
1893     case DW_OP_breg25:
1894     case DW_OP_breg26:
1895     case DW_OP_breg27:
1896     case DW_OP_breg28:
1897     case DW_OP_breg29:
1898     case DW_OP_breg30:
1899     case DW_OP_breg31:
1900       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1901       break;
1902     case DW_OP_regx:
1903       {
1904 	unsigned r = val1->v.val_unsigned;
1905 	if (for_eh_or_skip >= 0)
1906 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1907 	gcc_assert (size_of_uleb128 (r)
1908 		    == size_of_uleb128 (val1->v.val_unsigned));
1909 	dw2_asm_output_data_uleb128 (r, NULL);
1910       }
1911       break;
1912     case DW_OP_fbreg:
1913       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1914       break;
1915     case DW_OP_bregx:
1916       {
1917 	unsigned r = val1->v.val_unsigned;
1918 	if (for_eh_or_skip >= 0)
1919 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1920 	gcc_assert (size_of_uleb128 (r)
1921 		    == size_of_uleb128 (val1->v.val_unsigned));
1922 	dw2_asm_output_data_uleb128 (r, NULL);
1923 	dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1924       }
1925       break;
1926     case DW_OP_piece:
1927       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1928       break;
1929     case DW_OP_bit_piece:
1930       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1931       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1932       break;
1933     case DW_OP_deref_size:
1934     case DW_OP_xderef_size:
1935       dw2_asm_output_data (1, val1->v.val_int, NULL);
1936       break;
1937 
1938     case DW_OP_addr:
1939       if (loc->dtprel)
1940 	{
1941 	  if (targetm.asm_out.output_dwarf_dtprel)
1942 	    {
1943 	      targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1944 						   DWARF2_ADDR_SIZE,
1945 						   val1->v.val_addr);
1946 	      fputc ('\n', asm_out_file);
1947 	    }
1948 	  else
1949 	    gcc_unreachable ();
1950 	}
1951       else
1952 	{
1953 #ifdef DWARF2_DEBUGGING_INFO
1954 	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1955 #else
1956 	  gcc_unreachable ();
1957 #endif
1958 	}
1959       break;
1960 
1961     case DW_OP_GNU_addr_index:
1962     case DW_OP_GNU_const_index:
1963       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1964       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1965                                    "(index into .debug_addr)");
1966       break;
1967 
1968     case DW_OP_GNU_implicit_pointer:
1969       {
1970 	char label[MAX_ARTIFICIAL_LABEL_BYTES
1971 		   + HOST_BITS_PER_WIDE_INT / 2 + 2];
1972 	gcc_assert (val1->val_class == dw_val_class_die_ref);
1973 	get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1974 	dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1975 	dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1976       }
1977       break;
1978 
1979     case DW_OP_GNU_entry_value:
1980       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1981       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1982       break;
1983 
1984     case DW_OP_GNU_const_type:
1985       {
1986 	unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1987 	gcc_assert (o);
1988 	dw2_asm_output_data_uleb128 (o, NULL);
1989 	switch (val2->val_class)
1990 	  {
1991 	  case dw_val_class_const:
1992 	    l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1993 	    dw2_asm_output_data (1, l, NULL);
1994 	    dw2_asm_output_data (l, val2->v.val_int, NULL);
1995 	    break;
1996 	  case dw_val_class_vec:
1997 	    {
1998 	      unsigned int elt_size = val2->v.val_vec.elt_size;
1999 	      unsigned int len = val2->v.val_vec.length;
2000 	      unsigned int i;
2001 	      unsigned char *p;
2002 
2003 	      l = len * elt_size;
2004 	      dw2_asm_output_data (1, l, NULL);
2005 	      if (elt_size > sizeof (HOST_WIDE_INT))
2006 		{
2007 		  elt_size /= 2;
2008 		  len *= 2;
2009 		}
2010 	      for (i = 0, p = val2->v.val_vec.array;
2011 		   i < len;
2012 		   i++, p += elt_size)
2013 		dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2014 				     "fp or vector constant word %u", i);
2015 	    }
2016 	    break;
2017 	  case dw_val_class_const_double:
2018 	    {
2019 	      unsigned HOST_WIDE_INT first, second;
2020 	      l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2021 
2022 	      dw2_asm_output_data (1, 2 * l, NULL);
2023 	      if (WORDS_BIG_ENDIAN)
2024 		{
2025 		  first = val2->v.val_double.high;
2026 		  second = val2->v.val_double.low;
2027 		}
2028 	      else
2029 		{
2030 		  first = val2->v.val_double.low;
2031 		  second = val2->v.val_double.high;
2032 		}
2033 	      dw2_asm_output_data (l, first, NULL);
2034 	      dw2_asm_output_data (l, second, NULL);
2035 	    }
2036 	    break;
2037 	  default:
2038 	    gcc_unreachable ();
2039 	  }
2040       }
2041       break;
2042     case DW_OP_GNU_regval_type:
2043       {
2044 	unsigned r = val1->v.val_unsigned;
2045 	unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2046 	gcc_assert (o);
2047 	if (for_eh_or_skip >= 0)
2048 	  {
2049 	    r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2050 	    gcc_assert (size_of_uleb128 (r)
2051 			== size_of_uleb128 (val1->v.val_unsigned));
2052 	  }
2053 	dw2_asm_output_data_uleb128 (r, NULL);
2054 	dw2_asm_output_data_uleb128 (o, NULL);
2055       }
2056       break;
2057     case DW_OP_GNU_deref_type:
2058       {
2059 	unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2060 	gcc_assert (o);
2061 	dw2_asm_output_data (1, val1->v.val_int, NULL);
2062 	dw2_asm_output_data_uleb128 (o, NULL);
2063       }
2064       break;
2065     case DW_OP_GNU_convert:
2066     case DW_OP_GNU_reinterpret:
2067       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2068 	dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2069       else
2070 	{
2071 	  unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2072 	  gcc_assert (o);
2073 	  dw2_asm_output_data_uleb128 (o, NULL);
2074 	}
2075       break;
2076 
2077     case DW_OP_GNU_parameter_ref:
2078       {
2079 	unsigned long o;
2080 	gcc_assert (val1->val_class == dw_val_class_die_ref);
2081 	o = get_ref_die_offset (val1->v.val_die_ref.die);
2082 	dw2_asm_output_data (4, o, NULL);
2083       }
2084       break;
2085 
2086     default:
2087       /* Other codes have no operands.  */
2088       break;
2089     }
2090 }
2091 
2092 /* Output a sequence of location operations.
2093    The for_eh_or_skip parameter controls whether register numbers are
2094    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2095    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2096    info).  This should be suppressed for the cases that have not been converted
2097    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2098 
2099 void
output_loc_sequence(dw_loc_descr_ref loc,int for_eh_or_skip)2100 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2101 {
2102   for (; loc != NULL; loc = loc->dw_loc_next)
2103     {
2104       enum dwarf_location_atom opc = loc->dw_loc_opc;
2105       /* Output the opcode.  */
2106       if (for_eh_or_skip >= 0
2107           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2108 	{
2109 	  unsigned r = (opc - DW_OP_breg0);
2110 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2111 	  gcc_assert (r <= 31);
2112 	  opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2113 	}
2114       else if (for_eh_or_skip >= 0
2115 	       && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2116 	{
2117 	  unsigned r = (opc - DW_OP_reg0);
2118 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2119 	  gcc_assert (r <= 31);
2120 	  opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2121 	}
2122 
2123       dw2_asm_output_data (1, opc,
2124 			     "%s", dwarf_stack_op_name (opc));
2125 
2126       /* Output the operand(s) (if any).  */
2127       output_loc_operands (loc, for_eh_or_skip);
2128     }
2129 }
2130 
2131 /* Output location description stack opcode's operands (if any).
2132    The output is single bytes on a line, suitable for .cfi_escape.  */
2133 
2134 static void
output_loc_operands_raw(dw_loc_descr_ref loc)2135 output_loc_operands_raw (dw_loc_descr_ref loc)
2136 {
2137   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2138   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2139 
2140   switch (loc->dw_loc_opc)
2141     {
2142     case DW_OP_addr:
2143     case DW_OP_GNU_addr_index:
2144     case DW_OP_GNU_const_index:
2145     case DW_OP_implicit_value:
2146       /* We cannot output addresses in .cfi_escape, only bytes.  */
2147       gcc_unreachable ();
2148 
2149     case DW_OP_const1u:
2150     case DW_OP_const1s:
2151     case DW_OP_pick:
2152     case DW_OP_deref_size:
2153     case DW_OP_xderef_size:
2154       fputc (',', asm_out_file);
2155       dw2_asm_output_data_raw (1, val1->v.val_int);
2156       break;
2157 
2158     case DW_OP_const2u:
2159     case DW_OP_const2s:
2160       fputc (',', asm_out_file);
2161       dw2_asm_output_data_raw (2, val1->v.val_int);
2162       break;
2163 
2164     case DW_OP_const4u:
2165     case DW_OP_const4s:
2166       fputc (',', asm_out_file);
2167       dw2_asm_output_data_raw (4, val1->v.val_int);
2168       break;
2169 
2170     case DW_OP_const8u:
2171     case DW_OP_const8s:
2172       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2173       fputc (',', asm_out_file);
2174       dw2_asm_output_data_raw (8, val1->v.val_int);
2175       break;
2176 
2177     case DW_OP_skip:
2178     case DW_OP_bra:
2179       {
2180 	int offset;
2181 
2182 	gcc_assert (val1->val_class == dw_val_class_loc);
2183 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2184 
2185         fputc (',', asm_out_file);
2186 	dw2_asm_output_data_raw (2, offset);
2187       }
2188       break;
2189 
2190     case DW_OP_regx:
2191       {
2192 	unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2193 	gcc_assert (size_of_uleb128 (r)
2194 		    == size_of_uleb128 (val1->v.val_unsigned));
2195 	fputc (',', asm_out_file);
2196 	dw2_asm_output_data_uleb128_raw (r);
2197       }
2198       break;
2199 
2200     case DW_OP_constu:
2201     case DW_OP_plus_uconst:
2202     case DW_OP_piece:
2203       fputc (',', asm_out_file);
2204       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2205       break;
2206 
2207     case DW_OP_bit_piece:
2208       fputc (',', asm_out_file);
2209       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2210       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2211       break;
2212 
2213     case DW_OP_consts:
2214     case DW_OP_breg0:
2215     case DW_OP_breg1:
2216     case DW_OP_breg2:
2217     case DW_OP_breg3:
2218     case DW_OP_breg4:
2219     case DW_OP_breg5:
2220     case DW_OP_breg6:
2221     case DW_OP_breg7:
2222     case DW_OP_breg8:
2223     case DW_OP_breg9:
2224     case DW_OP_breg10:
2225     case DW_OP_breg11:
2226     case DW_OP_breg12:
2227     case DW_OP_breg13:
2228     case DW_OP_breg14:
2229     case DW_OP_breg15:
2230     case DW_OP_breg16:
2231     case DW_OP_breg17:
2232     case DW_OP_breg18:
2233     case DW_OP_breg19:
2234     case DW_OP_breg20:
2235     case DW_OP_breg21:
2236     case DW_OP_breg22:
2237     case DW_OP_breg23:
2238     case DW_OP_breg24:
2239     case DW_OP_breg25:
2240     case DW_OP_breg26:
2241     case DW_OP_breg27:
2242     case DW_OP_breg28:
2243     case DW_OP_breg29:
2244     case DW_OP_breg30:
2245     case DW_OP_breg31:
2246     case DW_OP_fbreg:
2247       fputc (',', asm_out_file);
2248       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2249       break;
2250 
2251     case DW_OP_bregx:
2252       {
2253 	unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2254 	gcc_assert (size_of_uleb128 (r)
2255 		    == size_of_uleb128 (val1->v.val_unsigned));
2256 	fputc (',', asm_out_file);
2257 	dw2_asm_output_data_uleb128_raw (r);
2258 	fputc (',', asm_out_file);
2259 	dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2260       }
2261       break;
2262 
2263     case DW_OP_GNU_implicit_pointer:
2264     case DW_OP_GNU_entry_value:
2265     case DW_OP_GNU_const_type:
2266     case DW_OP_GNU_regval_type:
2267     case DW_OP_GNU_deref_type:
2268     case DW_OP_GNU_convert:
2269     case DW_OP_GNU_reinterpret:
2270     case DW_OP_GNU_parameter_ref:
2271       gcc_unreachable ();
2272       break;
2273 
2274     default:
2275       /* Other codes have no operands.  */
2276       break;
2277     }
2278 }
2279 
2280 void
output_loc_sequence_raw(dw_loc_descr_ref loc)2281 output_loc_sequence_raw (dw_loc_descr_ref loc)
2282 {
2283   while (1)
2284     {
2285       enum dwarf_location_atom opc = loc->dw_loc_opc;
2286       /* Output the opcode.  */
2287       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2288 	{
2289 	  unsigned r = (opc - DW_OP_breg0);
2290 	  r = DWARF2_FRAME_REG_OUT (r, 1);
2291 	  gcc_assert (r <= 31);
2292 	  opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2293 	}
2294       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2295 	{
2296 	  unsigned r = (opc - DW_OP_reg0);
2297 	  r = DWARF2_FRAME_REG_OUT (r, 1);
2298 	  gcc_assert (r <= 31);
2299 	  opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2300 	}
2301       /* Output the opcode.  */
2302       fprintf (asm_out_file, "%#x", opc);
2303       output_loc_operands_raw (loc);
2304 
2305       if (!loc->dw_loc_next)
2306 	break;
2307       loc = loc->dw_loc_next;
2308 
2309       fputc (',', asm_out_file);
2310     }
2311 }
2312 
2313 /* This function builds a dwarf location descriptor sequence from a
2314    dw_cfa_location, adding the given OFFSET to the result of the
2315    expression.  */
2316 
2317 struct dw_loc_descr_node *
build_cfa_loc(dw_cfa_location * cfa,HOST_WIDE_INT offset)2318 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2319 {
2320   struct dw_loc_descr_node *head, *tmp;
2321 
2322   offset += cfa->offset;
2323 
2324   if (cfa->indirect)
2325     {
2326       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2327       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2328       head->dw_loc_oprnd1.val_entry = NULL;
2329       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2330       add_loc_descr (&head, tmp);
2331       if (offset != 0)
2332 	{
2333 	  tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2334 	  add_loc_descr (&head, tmp);
2335 	}
2336     }
2337   else
2338     head = new_reg_loc_descr (cfa->reg, offset);
2339 
2340   return head;
2341 }
2342 
2343 /* This function builds a dwarf location descriptor sequence for
2344    the address at OFFSET from the CFA when stack is aligned to
2345    ALIGNMENT byte.  */
2346 
2347 struct dw_loc_descr_node *
build_cfa_aligned_loc(dw_cfa_location * cfa,HOST_WIDE_INT offset,HOST_WIDE_INT alignment)2348 build_cfa_aligned_loc (dw_cfa_location *cfa,
2349 		       HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2350 {
2351   struct dw_loc_descr_node *head;
2352   unsigned int dwarf_fp
2353     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2354 
2355   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2356   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2357     {
2358       head = new_reg_loc_descr (dwarf_fp, 0);
2359       add_loc_descr (&head, int_loc_descriptor (alignment));
2360       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2361       loc_descr_plus_const (&head, offset);
2362     }
2363   else
2364     head = new_reg_loc_descr (dwarf_fp, offset);
2365   return head;
2366 }
2367 
2368 /* And now, the support for symbolic debugging information.  */
2369 
2370 /* .debug_str support.  */
2371 static int output_indirect_string (void **, void *);
2372 
2373 static void dwarf2out_init (const char *);
2374 static void dwarf2out_finish (const char *);
2375 static void dwarf2out_assembly_start (void);
2376 static void dwarf2out_define (unsigned int, const char *);
2377 static void dwarf2out_undef (unsigned int, const char *);
2378 static void dwarf2out_start_source_file (unsigned, const char *);
2379 static void dwarf2out_end_source_file (unsigned);
2380 static void dwarf2out_function_decl (tree);
2381 static void dwarf2out_begin_block (unsigned, unsigned);
2382 static void dwarf2out_end_block (unsigned, unsigned);
2383 static bool dwarf2out_ignore_block (const_tree);
2384 static void dwarf2out_global_decl (tree);
2385 static void dwarf2out_type_decl (tree, int);
2386 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2387 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2388 						 dw_die_ref);
2389 static void dwarf2out_abstract_function (tree);
2390 static void dwarf2out_var_location (rtx);
2391 static void dwarf2out_begin_function (tree);
2392 static void dwarf2out_end_function (unsigned int);
2393 static void dwarf2out_set_name (tree, tree);
2394 
2395 /* The debug hooks structure.  */
2396 
2397 const struct gcc_debug_hooks dwarf2_debug_hooks =
2398 {
2399   dwarf2out_init,
2400   dwarf2out_finish,
2401   dwarf2out_assembly_start,
2402   dwarf2out_define,
2403   dwarf2out_undef,
2404   dwarf2out_start_source_file,
2405   dwarf2out_end_source_file,
2406   dwarf2out_begin_block,
2407   dwarf2out_end_block,
2408   dwarf2out_ignore_block,
2409   dwarf2out_source_line,
2410   dwarf2out_begin_prologue,
2411 #if VMS_DEBUGGING_INFO
2412   dwarf2out_vms_end_prologue,
2413   dwarf2out_vms_begin_epilogue,
2414 #else
2415   debug_nothing_int_charstar,
2416   debug_nothing_int_charstar,
2417 #endif
2418   dwarf2out_end_epilogue,
2419   dwarf2out_begin_function,
2420   dwarf2out_end_function,	/* end_function */
2421   dwarf2out_function_decl,	/* function_decl */
2422   dwarf2out_global_decl,
2423   dwarf2out_type_decl,		/* type_decl */
2424   dwarf2out_imported_module_or_decl,
2425   debug_nothing_tree,		/* deferred_inline_function */
2426   /* The DWARF 2 backend tries to reduce debugging bloat by not
2427      emitting the abstract description of inline functions until
2428      something tries to reference them.  */
2429   dwarf2out_abstract_function,	/* outlining_inline_function */
2430   debug_nothing_rtx,		/* label */
2431   debug_nothing_int,		/* handle_pch */
2432   dwarf2out_var_location,
2433   dwarf2out_switch_text_section,
2434   dwarf2out_set_name,
2435   1,                            /* start_end_main_source_file */
2436   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2437 };
2438 
2439 /* NOTE: In the comments in this file, many references are made to
2440    "Debugging Information Entries".  This term is abbreviated as `DIE'
2441    throughout the remainder of this file.  */
2442 
2443 /* An internal representation of the DWARF output is built, and then
2444    walked to generate the DWARF debugging info.  The walk of the internal
2445    representation is done after the entire program has been compiled.
2446    The types below are used to describe the internal representation.  */
2447 
2448 /* Whether to put type DIEs into their own section .debug_types instead
2449    of making them part of the .debug_info section.  Only supported for
2450    Dwarf V4 or higher and the user didn't disable them through
2451    -fno-debug-types-section.  It is more efficient to put them in a
2452    separate comdat sections since the linker will then be able to
2453    remove duplicates.  But not all tools support .debug_types sections
2454    yet.  */
2455 
2456 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2457 
2458 /* Various DIE's use offsets relative to the beginning of the
2459    .debug_info section to refer to each other.  */
2460 
2461 typedef long int dw_offset;
2462 
2463 /* Define typedefs here to avoid circular dependencies.  */
2464 
2465 typedef struct dw_attr_struct *dw_attr_ref;
2466 typedef struct dw_line_info_struct *dw_line_info_ref;
2467 typedef struct pubname_struct *pubname_ref;
2468 typedef struct dw_ranges_struct *dw_ranges_ref;
2469 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2470 typedef struct comdat_type_struct *comdat_type_node_ref;
2471 
2472 /* The entries in the line_info table more-or-less mirror the opcodes
2473    that are used in the real dwarf line table.  Arrays of these entries
2474    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2475    supported.  */
2476 
2477 enum dw_line_info_opcode {
2478   /* Emit DW_LNE_set_address; the operand is the label index.  */
2479   LI_set_address,
2480 
2481   /* Emit a row to the matrix with the given line.  This may be done
2482      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2483      special opcodes.  */
2484   LI_set_line,
2485 
2486   /* Emit a DW_LNS_set_file.  */
2487   LI_set_file,
2488 
2489   /* Emit a DW_LNS_set_column.  */
2490   LI_set_column,
2491 
2492   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2493   LI_negate_stmt,
2494 
2495   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2496   LI_set_prologue_end,
2497   LI_set_epilogue_begin,
2498 
2499   /* Emit a DW_LNE_set_discriminator.  */
2500   LI_set_discriminator
2501 };
2502 
2503 typedef struct GTY(()) dw_line_info_struct {
2504   enum dw_line_info_opcode opcode;
2505   unsigned int val;
2506 } dw_line_info_entry;
2507 
2508 
2509 typedef struct GTY(()) dw_line_info_table_struct {
2510   /* The label that marks the end of this section.  */
2511   const char *end_label;
2512 
2513   /* The values for the last row of the matrix, as collected in the table.
2514      These are used to minimize the changes to the next row.  */
2515   unsigned int file_num;
2516   unsigned int line_num;
2517   unsigned int column_num;
2518   int discrim_num;
2519   bool is_stmt;
2520   bool in_use;
2521 
2522   vec<dw_line_info_entry, va_gc> *entries;
2523 } dw_line_info_table;
2524 
2525 typedef dw_line_info_table *dw_line_info_table_p;
2526 
2527 
2528 /* Each DIE attribute has a field specifying the attribute kind,
2529    a link to the next attribute in the chain, and an attribute value.
2530    Attributes are typically linked below the DIE they modify.  */
2531 
2532 typedef struct GTY(()) dw_attr_struct {
2533   enum dwarf_attribute dw_attr;
2534   dw_val_node dw_attr_val;
2535 }
2536 dw_attr_node;
2537 
2538 
2539 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2540    The children of each node form a circular list linked by
2541    die_sib.  die_child points to the node *before* the "first" child node.  */
2542 
2543 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2544   union die_symbol_or_type_node
2545     {
2546       const char * GTY ((tag ("0"))) die_symbol;
2547       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2548     }
2549   GTY ((desc ("%0.comdat_type_p"))) die_id;
2550   vec<dw_attr_node, va_gc> *die_attr;
2551   dw_die_ref die_parent;
2552   dw_die_ref die_child;
2553   dw_die_ref die_sib;
2554   dw_die_ref die_definition; /* ref from a specification to its definition */
2555   dw_offset die_offset;
2556   unsigned long die_abbrev;
2557   int die_mark;
2558   unsigned int decl_id;
2559   enum dwarf_tag die_tag;
2560   /* Die is used and must not be pruned as unused.  */
2561   BOOL_BITFIELD die_perennial_p : 1;
2562   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2563   /* Lots of spare bits.  */
2564 }
2565 die_node;
2566 
2567 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2568 #define FOR_EACH_CHILD(die, c, expr) do {	\
2569   c = die->die_child;				\
2570   if (c) do {					\
2571     c = c->die_sib;				\
2572     expr;					\
2573   } while (c != die->die_child);		\
2574 } while (0)
2575 
2576 /* The pubname structure */
2577 
2578 typedef struct GTY(()) pubname_struct {
2579   dw_die_ref die;
2580   const char *name;
2581 }
2582 pubname_entry;
2583 
2584 
2585 struct GTY(()) dw_ranges_struct {
2586   /* If this is positive, it's a block number, otherwise it's a
2587      bitwise-negated index into dw_ranges_by_label.  */
2588   int num;
2589 };
2590 
2591 /* A structure to hold a macinfo entry.  */
2592 
2593 typedef struct GTY(()) macinfo_struct {
2594   unsigned char code;
2595   unsigned HOST_WIDE_INT lineno;
2596   const char *info;
2597 }
2598 macinfo_entry;
2599 
2600 
2601 struct GTY(()) dw_ranges_by_label_struct {
2602   const char *begin;
2603   const char *end;
2604 };
2605 
2606 /* The comdat type node structure.  */
2607 typedef struct GTY(()) comdat_type_struct
2608 {
2609   dw_die_ref root_die;
2610   dw_die_ref type_die;
2611   dw_die_ref skeleton_die;
2612   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2613   struct comdat_type_struct *next;
2614 }
2615 comdat_type_node;
2616 
2617 /* The limbo die list structure.  */
2618 typedef struct GTY(()) limbo_die_struct {
2619   dw_die_ref die;
2620   tree created_for;
2621   struct limbo_die_struct *next;
2622 }
2623 limbo_die_node;
2624 
2625 typedef struct skeleton_chain_struct
2626 {
2627   dw_die_ref old_die;
2628   dw_die_ref new_die;
2629   struct skeleton_chain_struct *parent;
2630 }
2631 skeleton_chain_node;
2632 
2633 /* Define a macro which returns nonzero for a TYPE_DECL which was
2634    implicitly generated for a type.
2635 
2636    Note that, unlike the C front-end (which generates a NULL named
2637    TYPE_DECL node for each complete tagged type, each array type,
2638    and each function type node created) the C++ front-end generates
2639    a _named_ TYPE_DECL node for each tagged type node created.
2640    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2641    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2642    front-end, but for each type, tagged or not.  */
2643 
2644 #define TYPE_DECL_IS_STUB(decl)				\
2645   (DECL_NAME (decl) == NULL_TREE			\
2646    || (DECL_ARTIFICIAL (decl)				\
2647        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))	\
2648 	   /* This is necessary for stub decls that	\
2649 	      appear in nested inline functions.  */	\
2650 	   || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE	\
2651 	       && (decl_ultimate_origin (decl)		\
2652 		   == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2653 
2654 /* Information concerning the compilation unit's programming
2655    language, and compiler version.  */
2656 
2657 /* Fixed size portion of the DWARF compilation unit header.  */
2658 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2659   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2660 
2661 /* Fixed size portion of the DWARF comdat type unit header.  */
2662 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2663   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2664    + DWARF_OFFSET_SIZE)
2665 
2666 /* Fixed size portion of public names info.  */
2667 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2668 
2669 /* Fixed size portion of the address range info.  */
2670 #define DWARF_ARANGES_HEADER_SIZE					\
2671   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,	\
2672 		DWARF2_ADDR_SIZE * 2)					\
2673    - DWARF_INITIAL_LENGTH_SIZE)
2674 
2675 /* Size of padding portion in the address range info.  It must be
2676    aligned to twice the pointer size.  */
2677 #define DWARF_ARANGES_PAD_SIZE \
2678   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2679 		DWARF2_ADDR_SIZE * 2)				   \
2680    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2681 
2682 /* Use assembler line directives if available.  */
2683 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2684 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2685 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2686 #else
2687 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2688 #endif
2689 #endif
2690 
2691 /* Minimum line offset in a special line info. opcode.
2692    This value was chosen to give a reasonable range of values.  */
2693 #define DWARF_LINE_BASE  -10
2694 
2695 /* First special line opcode - leave room for the standard opcodes.  */
2696 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2697 
2698 /* Range of line offsets in a special line info. opcode.  */
2699 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2700 
2701 /* Flag that indicates the initial value of the is_stmt_start flag.
2702    In the present implementation, we do not mark any lines as
2703    the beginning of a source statement, because that information
2704    is not made available by the GCC front-end.  */
2705 #define	DWARF_LINE_DEFAULT_IS_STMT_START 1
2706 
2707 /* Maximum number of operations per instruction bundle.  */
2708 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2709 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2710 #endif
2711 
2712 /* This location is used by calc_die_sizes() to keep track
2713    the offset of each DIE within the .debug_info section.  */
2714 static unsigned long next_die_offset;
2715 
2716 /* Record the root of the DIE's built for the current compilation unit.  */
2717 static GTY(()) dw_die_ref single_comp_unit_die;
2718 
2719 /* A list of type DIEs that have been separated into comdat sections.  */
2720 static GTY(()) comdat_type_node *comdat_type_list;
2721 
2722 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2723 static GTY(()) limbo_die_node *limbo_die_list;
2724 
2725 /* A list of DIEs for which we may have to generate
2726    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2727 static GTY(()) limbo_die_node *deferred_asm_name;
2728 
2729 /* Filenames referenced by this compilation unit.  */
2730 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2731 
2732 /* A hash table of references to DIE's that describe declarations.
2733    The key is a DECL_UID() which is a unique number identifying each decl.  */
2734 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2735 
2736 /* A hash table of references to DIE's that describe COMMON blocks.
2737    The key is DECL_UID() ^ die_parent.  */
2738 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2739 
2740 typedef struct GTY(()) die_arg_entry_struct {
2741     dw_die_ref die;
2742     tree arg;
2743 } die_arg_entry;
2744 
2745 
2746 /* Node of the variable location list.  */
2747 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2748   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2749      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2750      in mode of the EXPR_LIST node and first EXPR_LIST operand
2751      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2752      location or NULL for padding.  For larger bitsizes,
2753      mode is 0 and first operand is a CONCAT with bitsize
2754      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2755      NULL as second operand.  */
2756   rtx GTY (()) loc;
2757   const char * GTY (()) label;
2758   struct var_loc_node * GTY (()) next;
2759 };
2760 
2761 /* Variable location list.  */
2762 struct GTY (()) var_loc_list_def {
2763   struct var_loc_node * GTY (()) first;
2764 
2765   /* Pointer to the last but one or last element of the
2766      chained list.  If the list is empty, both first and
2767      last are NULL, if the list contains just one node
2768      or the last node certainly is not redundant, it points
2769      to the last node, otherwise points to the last but one.
2770      Do not mark it for GC because it is marked through the chain.  */
2771   struct var_loc_node * GTY ((skip ("%h"))) last;
2772 
2773   /* Pointer to the last element before section switch,
2774      if NULL, either sections weren't switched or first
2775      is after section switch.  */
2776   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2777 
2778   /* DECL_UID of the variable decl.  */
2779   unsigned int decl_id;
2780 };
2781 typedef struct var_loc_list_def var_loc_list;
2782 
2783 /* Call argument location list.  */
2784 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2785   rtx GTY (()) call_arg_loc_note;
2786   const char * GTY (()) label;
2787   tree GTY (()) block;
2788   bool tail_call_p;
2789   rtx GTY (()) symbol_ref;
2790   struct call_arg_loc_node * GTY (()) next;
2791 };
2792 
2793 
2794 /* Table of decl location linked lists.  */
2795 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2796 
2797 /* Head and tail of call_arg_loc chain.  */
2798 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2799 static struct call_arg_loc_node *call_arg_loc_last;
2800 
2801 /* Number of call sites in the current function.  */
2802 static int call_site_count = -1;
2803 /* Number of tail call sites in the current function.  */
2804 static int tail_call_site_count = -1;
2805 
2806 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2807    DIEs.  */
2808 static vec<dw_die_ref> block_map;
2809 
2810 /* A cached location list.  */
2811 struct GTY (()) cached_dw_loc_list_def {
2812   /* The DECL_UID of the decl that this entry describes.  */
2813   unsigned int decl_id;
2814 
2815   /* The cached location list.  */
2816   dw_loc_list_ref loc_list;
2817 };
2818 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2819 
2820 /* Table of cached location lists.  */
2821 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2822 
2823 /* A pointer to the base of a list of references to DIE's that
2824    are uniquely identified by their tag, presence/absence of
2825    children DIE's, and list of attribute/value pairs.  */
2826 static GTY((length ("abbrev_die_table_allocated")))
2827   dw_die_ref *abbrev_die_table;
2828 
2829 /* Number of elements currently allocated for abbrev_die_table.  */
2830 static GTY(()) unsigned abbrev_die_table_allocated;
2831 
2832 /* Number of elements in type_die_table currently in use.  */
2833 static GTY(()) unsigned abbrev_die_table_in_use;
2834 
2835 /* Size (in elements) of increments by which we may expand the
2836    abbrev_die_table.  */
2837 #define ABBREV_DIE_TABLE_INCREMENT 256
2838 
2839 /* A global counter for generating labels for line number data.  */
2840 static unsigned int line_info_label_num;
2841 
2842 /* The current table to which we should emit line number information
2843    for the current function.  This will be set up at the beginning of
2844    assembly for the function.  */
2845 static dw_line_info_table *cur_line_info_table;
2846 
2847 /* The two default tables of line number info.  */
2848 static GTY(()) dw_line_info_table *text_section_line_info;
2849 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2850 
2851 /* The set of all non-default tables of line number info.  */
2852 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2853 
2854 /* A flag to tell pubnames/types export if there is an info section to
2855    refer to.  */
2856 static bool info_section_emitted;
2857 
2858 /* A pointer to the base of a table that contains a list of publicly
2859    accessible names.  */
2860 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2861 
2862 /* A pointer to the base of a table that contains a list of publicly
2863    accessible types.  */
2864 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2865 
2866 /* A pointer to the base of a table that contains a list of macro
2867    defines/undefines (and file start/end markers).  */
2868 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2869 
2870 /* True if .debug_macinfo or .debug_macros section is going to be
2871    emitted.  */
2872 #define have_macinfo \
2873   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2874    && !macinfo_table->is_empty ())
2875 
2876 /* Array of dies for which we should generate .debug_ranges info.  */
2877 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2878 
2879 /* Number of elements currently allocated for ranges_table.  */
2880 static GTY(()) unsigned ranges_table_allocated;
2881 
2882 /* Number of elements in ranges_table currently in use.  */
2883 static GTY(()) unsigned ranges_table_in_use;
2884 
2885 /* Array of pairs of labels referenced in ranges_table.  */
2886 static GTY ((length ("ranges_by_label_allocated")))
2887      dw_ranges_by_label_ref ranges_by_label;
2888 
2889 /* Number of elements currently allocated for ranges_by_label.  */
2890 static GTY(()) unsigned ranges_by_label_allocated;
2891 
2892 /* Number of elements in ranges_by_label currently in use.  */
2893 static GTY(()) unsigned ranges_by_label_in_use;
2894 
2895 /* Size (in elements) of increments by which we may expand the
2896    ranges_table.  */
2897 #define RANGES_TABLE_INCREMENT 64
2898 
2899 /* Whether we have location lists that need outputting */
2900 static GTY(()) bool have_location_lists;
2901 
2902 /* Unique label counter.  */
2903 static GTY(()) unsigned int loclabel_num;
2904 
2905 /* Unique label counter for point-of-call tables.  */
2906 static GTY(()) unsigned int poc_label_num;
2907 
2908 /* Record whether the function being analyzed contains inlined functions.  */
2909 static int current_function_has_inlines;
2910 
2911 /* The last file entry emitted by maybe_emit_file().  */
2912 static GTY(()) struct dwarf_file_data * last_emitted_file;
2913 
2914 /* Number of internal labels generated by gen_internal_sym().  */
2915 static GTY(()) int label_num;
2916 
2917 /* Cached result of previous call to lookup_filename.  */
2918 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2919 
2920 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2921 
2922 /* Instances of generic types for which we need to generate debug
2923    info that describe their generic parameters and arguments. That
2924    generation needs to happen once all types are properly laid out so
2925    we do it at the end of compilation.  */
2926 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2927 
2928 /* Offset from the "steady-state frame pointer" to the frame base,
2929    within the current function.  */
2930 static HOST_WIDE_INT frame_pointer_fb_offset;
2931 static bool frame_pointer_fb_offset_valid;
2932 
2933 static vec<dw_die_ref> base_types;
2934 
2935 /* Forward declarations for functions defined in this file.  */
2936 
2937 static int is_pseudo_reg (const_rtx);
2938 static tree type_main_variant (tree);
2939 static int is_tagged_type (const_tree);
2940 static const char *dwarf_tag_name (unsigned);
2941 static const char *dwarf_attr_name (unsigned);
2942 static const char *dwarf_form_name (unsigned);
2943 static tree decl_ultimate_origin (const_tree);
2944 static tree decl_class_context (tree);
2945 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2946 static inline enum dw_val_class AT_class (dw_attr_ref);
2947 static inline unsigned int AT_index (dw_attr_ref);
2948 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2949 static inline unsigned AT_flag (dw_attr_ref);
2950 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2951 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2952 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2953 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2954 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2955 			   HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2956 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2957 			       unsigned int, unsigned char *);
2958 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2959 static hashval_t debug_str_do_hash (const void *);
2960 static int debug_str_eq (const void *, const void *);
2961 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2962 static inline const char *AT_string (dw_attr_ref);
2963 static enum dwarf_form AT_string_form (dw_attr_ref);
2964 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2965 static void add_AT_specification (dw_die_ref, dw_die_ref);
2966 static inline dw_die_ref AT_ref (dw_attr_ref);
2967 static inline int AT_ref_external (dw_attr_ref);
2968 static inline void set_AT_ref_external (dw_attr_ref, int);
2969 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2970 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2971 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2972 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2973 			     dw_loc_list_ref);
2974 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2975 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2976 static void remove_addr_table_entry (addr_table_entry *);
2977 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2978 static inline rtx AT_addr (dw_attr_ref);
2979 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2980 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2981 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2982 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2983 			   unsigned HOST_WIDE_INT);
2984 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2985                                unsigned long, bool);
2986 static inline const char *AT_lbl (dw_attr_ref);
2987 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2988 static const char *get_AT_low_pc (dw_die_ref);
2989 static const char *get_AT_hi_pc (dw_die_ref);
2990 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2991 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2992 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2993 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2994 static bool is_cxx (void);
2995 static bool is_fortran (void);
2996 static bool is_ada (void);
2997 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2998 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2999 static void add_child_die (dw_die_ref, dw_die_ref);
3000 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3001 static dw_die_ref lookup_type_die (tree);
3002 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3003 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3004 static void equate_type_number_to_die (tree, dw_die_ref);
3005 static hashval_t decl_die_table_hash (const void *);
3006 static int decl_die_table_eq (const void *, const void *);
3007 static dw_die_ref lookup_decl_die (tree);
3008 static hashval_t common_block_die_table_hash (const void *);
3009 static int common_block_die_table_eq (const void *, const void *);
3010 static hashval_t decl_loc_table_hash (const void *);
3011 static int decl_loc_table_eq (const void *, const void *);
3012 static var_loc_list *lookup_decl_loc (const_tree);
3013 static void equate_decl_number_to_die (tree, dw_die_ref);
3014 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3015 static void print_spaces (FILE *);
3016 static void print_die (dw_die_ref, FILE *);
3017 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3018 static dw_die_ref pop_compile_unit (dw_die_ref);
3019 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3020 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3021 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3022 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3023 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3024 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3025 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3026 				   struct md5_ctx *, int *);
3027 struct checksum_attributes;
3028 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3029 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3030 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3031 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3032 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3033 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3034 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3035 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3036 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3037 static void compute_section_prefix (dw_die_ref);
3038 static int is_type_die (dw_die_ref);
3039 static int is_comdat_die (dw_die_ref);
3040 static int is_symbol_die (dw_die_ref);
3041 static inline bool is_template_instantiation (dw_die_ref);
3042 static void assign_symbol_names (dw_die_ref);
3043 static void break_out_includes (dw_die_ref);
3044 static int is_declaration_die (dw_die_ref);
3045 static int should_move_die_to_comdat (dw_die_ref);
3046 static dw_die_ref clone_as_declaration (dw_die_ref);
3047 static dw_die_ref clone_die (dw_die_ref);
3048 static dw_die_ref clone_tree (dw_die_ref);
3049 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3050 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3051 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3052 static dw_die_ref generate_skeleton (dw_die_ref);
3053 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3054                                                          dw_die_ref,
3055                                                          dw_die_ref);
3056 static void break_out_comdat_types (dw_die_ref);
3057 static void copy_decls_for_unworthy_types (dw_die_ref);
3058 
3059 static void add_sibling_attributes (dw_die_ref);
3060 static void output_location_lists (dw_die_ref);
3061 static int constant_size (unsigned HOST_WIDE_INT);
3062 static unsigned long size_of_die (dw_die_ref);
3063 static void calc_die_sizes (dw_die_ref);
3064 static void calc_base_type_die_sizes (void);
3065 static void mark_dies (dw_die_ref);
3066 static void unmark_dies (dw_die_ref);
3067 static void unmark_all_dies (dw_die_ref);
3068 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3069 static unsigned long size_of_aranges (void);
3070 static enum dwarf_form value_format (dw_attr_ref);
3071 static void output_value_format (dw_attr_ref);
3072 static void output_abbrev_section (void);
3073 static void output_die_abbrevs (unsigned long, dw_die_ref);
3074 static void output_die_symbol (dw_die_ref);
3075 static void output_die (dw_die_ref);
3076 static void output_compilation_unit_header (void);
3077 static void output_comp_unit (dw_die_ref, int);
3078 static void output_comdat_type_unit (comdat_type_node *);
3079 static const char *dwarf2_name (tree, int);
3080 static void add_pubname (tree, dw_die_ref);
3081 static void add_enumerator_pubname (const char *, dw_die_ref);
3082 static void add_pubname_string (const char *, dw_die_ref);
3083 static void add_pubtype (tree, dw_die_ref);
3084 static void output_pubnames (vec<pubname_entry, va_gc> *);
3085 static void output_aranges (unsigned long);
3086 static unsigned int add_ranges_num (int);
3087 static unsigned int add_ranges (const_tree);
3088 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3089                                   bool *, bool);
3090 static void output_ranges (void);
3091 static dw_line_info_table *new_line_info_table (void);
3092 static void output_line_info (bool);
3093 static void output_file_names (void);
3094 static dw_die_ref base_type_die (tree);
3095 static int is_base_type (tree);
3096 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3097 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3098 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3099 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3100 static int type_is_enum (const_tree);
3101 static unsigned int dbx_reg_number (const_rtx);
3102 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3103 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3104 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3105 						enum var_init_status);
3106 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3107 						     enum var_init_status);
3108 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3109 					 enum var_init_status);
3110 static int is_based_loc (const_rtx);
3111 static int resolve_one_addr (rtx *, void *);
3112 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3113 					       enum var_init_status);
3114 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3115 					enum var_init_status);
3116 static dw_loc_list_ref loc_list_from_tree (tree, int);
3117 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3118 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3119 static tree field_type (const_tree);
3120 static unsigned int simple_type_align_in_bits (const_tree);
3121 static unsigned int simple_decl_align_in_bits (const_tree);
3122 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3123 static HOST_WIDE_INT field_byte_offset (const_tree);
3124 static void add_AT_location_description	(dw_die_ref, enum dwarf_attribute,
3125 					 dw_loc_list_ref);
3126 static void add_data_member_location_attribute (dw_die_ref, tree);
3127 static bool add_const_value_attribute (dw_die_ref, rtx);
3128 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3129 static void insert_double (double_int, unsigned char *);
3130 static void insert_float (const_rtx, unsigned char *);
3131 static rtx rtl_for_decl_location (tree);
3132 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3133 						   enum dwarf_attribute);
3134 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3135 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3136 static void add_name_attribute (dw_die_ref, const char *);
3137 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3138 static void add_comp_dir_attribute (dw_die_ref);
3139 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3140 static void add_subscript_info (dw_die_ref, tree, bool);
3141 static void add_byte_size_attribute (dw_die_ref, tree);
3142 static void add_bit_offset_attribute (dw_die_ref, tree);
3143 static void add_bit_size_attribute (dw_die_ref, tree);
3144 static void add_prototyped_attribute (dw_die_ref, tree);
3145 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3146 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3147 static void add_src_coords_attributes (dw_die_ref, tree);
3148 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3149 static void push_decl_scope (tree);
3150 static void pop_decl_scope (void);
3151 static dw_die_ref scope_die_for (tree, dw_die_ref);
3152 static inline int local_scope_p (dw_die_ref);
3153 static inline int class_scope_p (dw_die_ref);
3154 static inline int class_or_namespace_scope_p (dw_die_ref);
3155 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3156 static void add_calling_convention_attribute (dw_die_ref, tree);
3157 static const char *type_tag (const_tree);
3158 static tree member_declared_type (const_tree);
3159 #if 0
3160 static const char *decl_start_label (tree);
3161 #endif
3162 static void gen_array_type_die (tree, dw_die_ref);
3163 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3164 #if 0
3165 static void gen_entry_point_die (tree, dw_die_ref);
3166 #endif
3167 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3168 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3169 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3170 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3171 static void gen_formal_types_die (tree, dw_die_ref);
3172 static void gen_subprogram_die (tree, dw_die_ref);
3173 static void gen_variable_die (tree, tree, dw_die_ref);
3174 static void gen_const_die (tree, dw_die_ref);
3175 static void gen_label_die (tree, dw_die_ref);
3176 static void gen_lexical_block_die (tree, dw_die_ref, int);
3177 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3178 static void gen_field_die (tree, dw_die_ref);
3179 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3180 static dw_die_ref gen_compile_unit_die (const char *);
3181 static void gen_inheritance_die (tree, tree, dw_die_ref);
3182 static void gen_member_die (tree, dw_die_ref);
3183 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3184 						enum debug_info_usage);
3185 static void gen_subroutine_type_die (tree, dw_die_ref);
3186 static void gen_typedef_die (tree, dw_die_ref);
3187 static void gen_type_die (tree, dw_die_ref);
3188 static void gen_block_die (tree, dw_die_ref, int);
3189 static void decls_for_scope (tree, dw_die_ref, int);
3190 static inline int is_redundant_typedef (const_tree);
3191 static bool is_naming_typedef_decl (const_tree);
3192 static inline dw_die_ref get_context_die (tree);
3193 static void gen_namespace_die (tree, dw_die_ref);
3194 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3195 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3196 static dw_die_ref force_decl_die (tree);
3197 static dw_die_ref force_type_die (tree);
3198 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3199 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3200 static struct dwarf_file_data * lookup_filename (const char *);
3201 static void retry_incomplete_types (void);
3202 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3203 static void gen_generic_params_dies (tree);
3204 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3205 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3206 static void splice_child_die (dw_die_ref, dw_die_ref);
3207 static int file_info_cmp (const void *, const void *);
3208 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3209 				     const char *, const char *);
3210 static void output_loc_list (dw_loc_list_ref);
3211 static char *gen_internal_sym (const char *);
3212 static bool want_pubnames (void);
3213 
3214 static void prune_unmark_dies (dw_die_ref);
3215 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3216 static void prune_unused_types_mark (dw_die_ref, int);
3217 static void prune_unused_types_walk (dw_die_ref);
3218 static void prune_unused_types_walk_attribs (dw_die_ref);
3219 static void prune_unused_types_prune (dw_die_ref);
3220 static void prune_unused_types (void);
3221 static int maybe_emit_file (struct dwarf_file_data *fd);
3222 static inline const char *AT_vms_delta1 (dw_attr_ref);
3223 static inline const char *AT_vms_delta2 (dw_attr_ref);
3224 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3225 				     const char *, const char *);
3226 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3227 static void gen_remaining_tmpl_value_param_die_attribute (void);
3228 static bool generic_type_p (tree);
3229 static void schedule_generic_params_dies_gen (tree t);
3230 static void gen_scheduled_generic_parms_dies (void);
3231 
3232 static const char *comp_dir_string (void);
3233 
3234 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3235 
3236 /* enum for tracking thread-local variables whose address is really an offset
3237    relative to the TLS pointer, which will need link-time relocation, but will
3238    not need relocation by the DWARF consumer.  */
3239 
3240 enum dtprel_bool
3241 {
3242   dtprel_false = 0,
3243   dtprel_true = 1
3244 };
3245 
3246 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3247    use DW_OP_const*.  For regular variables, which need both link-time
3248    relocation and consumer-level relocation (e.g., to account for shared objects
3249    loaded at a random address), we use DW_OP_addr*.  */
3250 
3251 static inline enum dwarf_location_atom
dw_addr_op(enum dtprel_bool dtprel)3252 dw_addr_op (enum dtprel_bool dtprel)
3253 {
3254   if (dtprel == dtprel_true)
3255     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3256             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3257   else
3258     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3259 }
3260 
3261 /* Return a pointer to a newly allocated address location description.  If
3262    dwarf_split_debug_info is true, then record the address with the appropriate
3263    relocation.  */
3264 static inline dw_loc_descr_ref
new_addr_loc_descr(rtx addr,enum dtprel_bool dtprel)3265 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3266 {
3267   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3268 
3269   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3270   ref->dw_loc_oprnd1.v.val_addr = addr;
3271   ref->dtprel = dtprel;
3272   if (dwarf_split_debug_info)
3273     ref->dw_loc_oprnd1.val_entry
3274         = add_addr_table_entry (addr,
3275                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3276   else
3277     ref->dw_loc_oprnd1.val_entry = NULL;
3278 
3279   return ref;
3280 }
3281 
3282 /* Section names used to hold DWARF debugging information.  */
3283 
3284 #ifndef DEBUG_INFO_SECTION
3285 #define DEBUG_INFO_SECTION	".debug_info"
3286 #endif
3287 #ifndef DEBUG_DWO_INFO_SECTION
3288 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3289 #endif
3290 #ifndef DEBUG_ABBREV_SECTION
3291 #define DEBUG_ABBREV_SECTION	".debug_abbrev"
3292 #endif
3293 #ifndef DEBUG_DWO_ABBREV_SECTION
3294 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3295 #endif
3296 #ifndef DEBUG_ARANGES_SECTION
3297 #define DEBUG_ARANGES_SECTION	".debug_aranges"
3298 #endif
3299 #ifndef DEBUG_ADDR_SECTION
3300 #define DEBUG_ADDR_SECTION     ".debug_addr"
3301 #endif
3302 #ifndef DEBUG_NORM_MACINFO_SECTION
3303 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3304 #endif
3305 #ifndef DEBUG_DWO_MACINFO_SECTION
3306 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3307 #endif
3308 #ifndef DEBUG_MACINFO_SECTION
3309 #define DEBUG_MACINFO_SECTION                                           \
3310   (!dwarf_split_debug_info                                              \
3311    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3312 #endif
3313 #ifndef DEBUG_NORM_MACRO_SECTION
3314 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3315 #endif
3316 #ifndef DEBUG_DWO_MACRO_SECTION
3317 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3318 #endif
3319 #ifndef DEBUG_MACRO_SECTION
3320 #define DEBUG_MACRO_SECTION                                             \
3321   (!dwarf_split_debug_info                                              \
3322    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3323 #endif
3324 #ifndef DEBUG_LINE_SECTION
3325 #define DEBUG_LINE_SECTION	".debug_line"
3326 #endif
3327 #ifndef DEBUG_DWO_LINE_SECTION
3328 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3329 #endif
3330 #ifndef DEBUG_LOC_SECTION
3331 #define DEBUG_LOC_SECTION	".debug_loc"
3332 #endif
3333 #ifndef DEBUG_DWO_LOC_SECTION
3334 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3335 #endif
3336 #ifndef DEBUG_PUBNAMES_SECTION
3337 #define DEBUG_PUBNAMES_SECTION	\
3338   ((debug_generate_pub_sections == 2) \
3339    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3340 #endif
3341 #ifndef DEBUG_PUBTYPES_SECTION
3342 #define DEBUG_PUBTYPES_SECTION	\
3343   ((debug_generate_pub_sections == 2) \
3344    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3345 #endif
3346 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3347 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3348 #ifndef DEBUG_STR_OFFSETS_SECTION
3349 #define DEBUG_STR_OFFSETS_SECTION                                       \
3350   (!dwarf_split_debug_info                                              \
3351    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3352 #endif
3353 #ifndef DEBUG_STR_DWO_SECTION
3354 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3355 #endif
3356 #ifndef DEBUG_STR_SECTION
3357 #define DEBUG_STR_SECTION  ".debug_str"
3358 #endif
3359 #ifndef DEBUG_RANGES_SECTION
3360 #define DEBUG_RANGES_SECTION	".debug_ranges"
3361 #endif
3362 
3363 /* Standard ELF section names for compiled code and data.  */
3364 #ifndef TEXT_SECTION_NAME
3365 #define TEXT_SECTION_NAME	".text"
3366 #endif
3367 
3368 /* Section flags for .debug_macinfo/.debug_macro section.  */
3369 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3370   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3371 
3372 /* Section flags for .debug_str section.  */
3373 #define DEBUG_STR_SECTION_FLAGS                                 \
3374   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3375    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3376    : SECTION_DEBUG)
3377 
3378 /* Section flags for .debug_str.dwo section.  */
3379 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3380 
3381 /* Labels we insert at beginning sections we can reference instead of
3382    the section names themselves.  */
3383 
3384 #ifndef TEXT_SECTION_LABEL
3385 #define TEXT_SECTION_LABEL                 "Ltext"
3386 #endif
3387 #ifndef COLD_TEXT_SECTION_LABEL
3388 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3389 #endif
3390 #ifndef DEBUG_LINE_SECTION_LABEL
3391 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3392 #endif
3393 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3394 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3395 #endif
3396 #ifndef DEBUG_INFO_SECTION_LABEL
3397 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3398 #endif
3399 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3400 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3401 #endif
3402 #ifndef DEBUG_ABBREV_SECTION_LABEL
3403 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3404 #endif
3405 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3406 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3407 #endif
3408 #ifndef DEBUG_ADDR_SECTION_LABEL
3409 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3410 #endif
3411 #ifndef DEBUG_LOC_SECTION_LABEL
3412 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3413 #endif
3414 #ifndef DEBUG_RANGES_SECTION_LABEL
3415 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3416 #endif
3417 #ifndef DEBUG_MACINFO_SECTION_LABEL
3418 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3419 #endif
3420 #ifndef DEBUG_MACRO_SECTION_LABEL
3421 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3422 #endif
3423 #define SKELETON_COMP_DIE_ABBREV 1
3424 #define SKELETON_TYPE_DIE_ABBREV 2
3425 
3426 /* Definitions of defaults for formats and names of various special
3427    (artificial) labels which may be generated within this file (when the -g
3428    options is used and DWARF2_DEBUGGING_INFO is in effect.
3429    If necessary, these may be overridden from within the tm.h file, but
3430    typically, overriding these defaults is unnecessary.  */
3431 
3432 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3440 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3441 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3442 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3443 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3444 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3445 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3446 
3447 #ifndef TEXT_END_LABEL
3448 #define TEXT_END_LABEL		"Letext"
3449 #endif
3450 #ifndef COLD_END_LABEL
3451 #define COLD_END_LABEL          "Letext_cold"
3452 #endif
3453 #ifndef BLOCK_BEGIN_LABEL
3454 #define BLOCK_BEGIN_LABEL	"LBB"
3455 #endif
3456 #ifndef BLOCK_END_LABEL
3457 #define BLOCK_END_LABEL		"LBE"
3458 #endif
3459 #ifndef LINE_CODE_LABEL
3460 #define LINE_CODE_LABEL		"LM"
3461 #endif
3462 
3463 
3464 /* Return the root of the DIE's built for the current compilation unit.  */
3465 static dw_die_ref
comp_unit_die(void)3466 comp_unit_die (void)
3467 {
3468   if (!single_comp_unit_die)
3469     single_comp_unit_die = gen_compile_unit_die (NULL);
3470   return single_comp_unit_die;
3471 }
3472 
3473 /* We allow a language front-end to designate a function that is to be
3474    called to "demangle" any name before it is put into a DIE.  */
3475 
3476 static const char *(*demangle_name_func) (const char *);
3477 
3478 void
dwarf2out_set_demangle_name_func(const char * (* func)(const char *))3479 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3480 {
3481   demangle_name_func = func;
3482 }
3483 
3484 /* Test if rtl node points to a pseudo register.  */
3485 
3486 static inline int
is_pseudo_reg(const_rtx rtl)3487 is_pseudo_reg (const_rtx rtl)
3488 {
3489   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3490 	  || (GET_CODE (rtl) == SUBREG
3491 	      && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3492 }
3493 
3494 /* Return a reference to a type, with its const and volatile qualifiers
3495    removed.  */
3496 
3497 static inline tree
type_main_variant(tree type)3498 type_main_variant (tree type)
3499 {
3500   type = TYPE_MAIN_VARIANT (type);
3501 
3502   /* ??? There really should be only one main variant among any group of
3503      variants of a given type (and all of the MAIN_VARIANT values for all
3504      members of the group should point to that one type) but sometimes the C
3505      front-end messes this up for array types, so we work around that bug
3506      here.  */
3507   if (TREE_CODE (type) == ARRAY_TYPE)
3508     while (type != TYPE_MAIN_VARIANT (type))
3509       type = TYPE_MAIN_VARIANT (type);
3510 
3511   return type;
3512 }
3513 
3514 /* Return nonzero if the given type node represents a tagged type.  */
3515 
3516 static inline int
is_tagged_type(const_tree type)3517 is_tagged_type (const_tree type)
3518 {
3519   enum tree_code code = TREE_CODE (type);
3520 
3521   return (code == RECORD_TYPE || code == UNION_TYPE
3522 	  || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3523 }
3524 
3525 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3526 
3527 static void
get_ref_die_offset_label(char * label,dw_die_ref ref)3528 get_ref_die_offset_label (char *label, dw_die_ref ref)
3529 {
3530   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3531 }
3532 
3533 /* Return die_offset of a DIE reference to a base type.  */
3534 
3535 static unsigned long int
get_base_type_offset(dw_die_ref ref)3536 get_base_type_offset (dw_die_ref ref)
3537 {
3538   if (ref->die_offset)
3539     return ref->die_offset;
3540   if (comp_unit_die ()->die_abbrev)
3541     {
3542       calc_base_type_die_sizes ();
3543       gcc_assert (ref->die_offset);
3544     }
3545   return ref->die_offset;
3546 }
3547 
3548 /* Return die_offset of a DIE reference other than base type.  */
3549 
3550 static unsigned long int
get_ref_die_offset(dw_die_ref ref)3551 get_ref_die_offset (dw_die_ref ref)
3552 {
3553   gcc_assert (ref->die_offset);
3554   return ref->die_offset;
3555 }
3556 
3557 /* Convert a DIE tag into its string name.  */
3558 
3559 static const char *
dwarf_tag_name(unsigned int tag)3560 dwarf_tag_name (unsigned int tag)
3561 {
3562   const char *name = get_DW_TAG_name (tag);
3563 
3564   if (name != NULL)
3565     return name;
3566 
3567   return "DW_TAG_<unknown>";
3568 }
3569 
3570 /* Convert a DWARF attribute code into its string name.  */
3571 
3572 static const char *
dwarf_attr_name(unsigned int attr)3573 dwarf_attr_name (unsigned int attr)
3574 {
3575   const char *name;
3576 
3577   switch (attr)
3578     {
3579 #if VMS_DEBUGGING_INFO
3580     case DW_AT_HP_prologue:
3581       return "DW_AT_HP_prologue";
3582 #else
3583     case DW_AT_MIPS_loop_unroll_factor:
3584       return "DW_AT_MIPS_loop_unroll_factor";
3585 #endif
3586 
3587 #if VMS_DEBUGGING_INFO
3588     case DW_AT_HP_epilogue:
3589       return "DW_AT_HP_epilogue";
3590 #else
3591     case DW_AT_MIPS_stride:
3592       return "DW_AT_MIPS_stride";
3593 #endif
3594     }
3595 
3596   name = get_DW_AT_name (attr);
3597 
3598   if (name != NULL)
3599     return name;
3600 
3601   return "DW_AT_<unknown>";
3602 }
3603 
3604 /* Convert a DWARF value form code into its string name.  */
3605 
3606 static const char *
dwarf_form_name(unsigned int form)3607 dwarf_form_name (unsigned int form)
3608 {
3609   const char *name = get_DW_FORM_name (form);
3610 
3611   if (name != NULL)
3612     return name;
3613 
3614   return "DW_FORM_<unknown>";
3615 }
3616 
3617 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3618    instance of an inlined instance of a decl which is local to an inline
3619    function, so we have to trace all of the way back through the origin chain
3620    to find out what sort of node actually served as the original seed for the
3621    given block.  */
3622 
3623 static tree
decl_ultimate_origin(const_tree decl)3624 decl_ultimate_origin (const_tree decl)
3625 {
3626   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3627     return NULL_TREE;
3628 
3629   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3630      nodes in the function to point to themselves; ignore that if
3631      we're trying to output the abstract instance of this function.  */
3632   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3633     return NULL_TREE;
3634 
3635   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3636      most distant ancestor, this should never happen.  */
3637   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3638 
3639   return DECL_ABSTRACT_ORIGIN (decl);
3640 }
3641 
3642 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3643    of a virtual function may refer to a base class, so we check the 'this'
3644    parameter.  */
3645 
3646 static tree
decl_class_context(tree decl)3647 decl_class_context (tree decl)
3648 {
3649   tree context = NULL_TREE;
3650 
3651   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3652     context = DECL_CONTEXT (decl);
3653   else
3654     context = TYPE_MAIN_VARIANT
3655       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3656 
3657   if (context && !TYPE_P (context))
3658     context = NULL_TREE;
3659 
3660   return context;
3661 }
3662 
3663 /* Add an attribute/value pair to a DIE.  */
3664 
3665 static inline void
add_dwarf_attr(dw_die_ref die,dw_attr_ref attr)3666 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3667 {
3668   /* Maybe this should be an assert?  */
3669   if (die == NULL)
3670     return;
3671 
3672   vec_safe_reserve (die->die_attr, 1);
3673   vec_safe_push (die->die_attr, *attr);
3674 }
3675 
3676 static inline enum dw_val_class
AT_class(dw_attr_ref a)3677 AT_class (dw_attr_ref a)
3678 {
3679   return a->dw_attr_val.val_class;
3680 }
3681 
3682 /* Return the index for any attribute that will be referenced with a
3683    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3684    are stored in dw_attr_val.v.val_str for reference counting
3685    pruning.  */
3686 
3687 static inline unsigned int
AT_index(dw_attr_ref a)3688 AT_index (dw_attr_ref a)
3689 {
3690   if (AT_class (a) == dw_val_class_str)
3691     return a->dw_attr_val.v.val_str->index;
3692   else if (a->dw_attr_val.val_entry != NULL)
3693     return a->dw_attr_val.val_entry->index;
3694   return NOT_INDEXED;
3695 }
3696 
3697 /* Add a flag value attribute to a DIE.  */
3698 
3699 static inline void
add_AT_flag(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned int flag)3700 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3701 {
3702   dw_attr_node attr;
3703 
3704   attr.dw_attr = attr_kind;
3705   attr.dw_attr_val.val_class = dw_val_class_flag;
3706   attr.dw_attr_val.val_entry = NULL;
3707   attr.dw_attr_val.v.val_flag = flag;
3708   add_dwarf_attr (die, &attr);
3709 }
3710 
3711 static inline unsigned
AT_flag(dw_attr_ref a)3712 AT_flag (dw_attr_ref a)
3713 {
3714   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3715   return a->dw_attr_val.v.val_flag;
3716 }
3717 
3718 /* Add a signed integer attribute value to a DIE.  */
3719 
3720 static inline void
add_AT_int(dw_die_ref die,enum dwarf_attribute attr_kind,HOST_WIDE_INT int_val)3721 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3722 {
3723   dw_attr_node attr;
3724 
3725   attr.dw_attr = attr_kind;
3726   attr.dw_attr_val.val_class = dw_val_class_const;
3727   attr.dw_attr_val.val_entry = NULL;
3728   attr.dw_attr_val.v.val_int = int_val;
3729   add_dwarf_attr (die, &attr);
3730 }
3731 
3732 static inline HOST_WIDE_INT
AT_int(dw_attr_ref a)3733 AT_int (dw_attr_ref a)
3734 {
3735   gcc_assert (a && AT_class (a) == dw_val_class_const);
3736   return a->dw_attr_val.v.val_int;
3737 }
3738 
3739 /* Add an unsigned integer attribute value to a DIE.  */
3740 
3741 static inline void
add_AT_unsigned(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned HOST_WIDE_INT unsigned_val)3742 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3743 		 unsigned HOST_WIDE_INT unsigned_val)
3744 {
3745   dw_attr_node attr;
3746 
3747   attr.dw_attr = attr_kind;
3748   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3749   attr.dw_attr_val.val_entry = NULL;
3750   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3751   add_dwarf_attr (die, &attr);
3752 }
3753 
3754 static inline unsigned HOST_WIDE_INT
AT_unsigned(dw_attr_ref a)3755 AT_unsigned (dw_attr_ref a)
3756 {
3757   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3758   return a->dw_attr_val.v.val_unsigned;
3759 }
3760 
3761 /* Add an unsigned double integer attribute value to a DIE.  */
3762 
3763 static inline void
add_AT_double(dw_die_ref die,enum dwarf_attribute attr_kind,HOST_WIDE_INT high,unsigned HOST_WIDE_INT low)3764 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3765 	       HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3766 {
3767   dw_attr_node attr;
3768 
3769   attr.dw_attr = attr_kind;
3770   attr.dw_attr_val.val_class = dw_val_class_const_double;
3771   attr.dw_attr_val.val_entry = NULL;
3772   attr.dw_attr_val.v.val_double.high = high;
3773   attr.dw_attr_val.v.val_double.low = low;
3774   add_dwarf_attr (die, &attr);
3775 }
3776 
3777 /* Add a floating point attribute value to a DIE and return it.  */
3778 
3779 static inline void
add_AT_vec(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned int length,unsigned int elt_size,unsigned char * array)3780 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3781 	    unsigned int length, unsigned int elt_size, unsigned char *array)
3782 {
3783   dw_attr_node attr;
3784 
3785   attr.dw_attr = attr_kind;
3786   attr.dw_attr_val.val_class = dw_val_class_vec;
3787   attr.dw_attr_val.val_entry = NULL;
3788   attr.dw_attr_val.v.val_vec.length = length;
3789   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3790   attr.dw_attr_val.v.val_vec.array = array;
3791   add_dwarf_attr (die, &attr);
3792 }
3793 
3794 /* Add an 8-byte data attribute value to a DIE.  */
3795 
3796 static inline void
add_AT_data8(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned char data8[8])3797 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3798               unsigned char data8[8])
3799 {
3800   dw_attr_node attr;
3801 
3802   attr.dw_attr = attr_kind;
3803   attr.dw_attr_val.val_class = dw_val_class_data8;
3804   attr.dw_attr_val.val_entry = NULL;
3805   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3806   add_dwarf_attr (die, &attr);
3807 }
3808 
3809 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3810    dwarf_split_debug_info, address attributes in dies destined for the
3811    final executable have force_direct set to avoid using indexed
3812    references.  */
3813 
3814 static inline void
add_AT_low_high_pc(dw_die_ref die,const char * lbl_low,const char * lbl_high,bool force_direct)3815 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3816                     bool force_direct)
3817 {
3818   dw_attr_node attr;
3819   char * lbl_id;
3820 
3821   lbl_id = xstrdup (lbl_low);
3822   attr.dw_attr = DW_AT_low_pc;
3823   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3824   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3825   if (dwarf_split_debug_info && !force_direct)
3826     attr.dw_attr_val.val_entry
3827         = add_addr_table_entry (lbl_id, ate_kind_label);
3828   else
3829     attr.dw_attr_val.val_entry = NULL;
3830   add_dwarf_attr (die, &attr);
3831 
3832   attr.dw_attr = DW_AT_high_pc;
3833   if (dwarf_version < 4)
3834     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3835   else
3836     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3837   lbl_id = xstrdup (lbl_high);
3838   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3839   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3840       && dwarf_split_debug_info && !force_direct)
3841     attr.dw_attr_val.val_entry
3842         = add_addr_table_entry (lbl_id, ate_kind_label);
3843   else
3844     attr.dw_attr_val.val_entry = NULL;
3845   add_dwarf_attr (die, &attr);
3846 }
3847 
3848 /* Hash and equality functions for debug_str_hash.  */
3849 
3850 static hashval_t
debug_str_do_hash(const void * x)3851 debug_str_do_hash (const void *x)
3852 {
3853   return htab_hash_string (((const struct indirect_string_node *)x)->str);
3854 }
3855 
3856 static int
debug_str_eq(const void * x1,const void * x2)3857 debug_str_eq (const void *x1, const void *x2)
3858 {
3859   return strcmp ((((const struct indirect_string_node *)x1)->str),
3860 		 (const char *)x2) == 0;
3861 }
3862 
3863 /* Add STR to the given string hash table.  */
3864 
3865 static struct indirect_string_node *
find_AT_string_in_table(const char * str,htab_t table)3866 find_AT_string_in_table (const char *str, htab_t table)
3867 {
3868   struct indirect_string_node *node;
3869   void **slot;
3870 
3871   slot = htab_find_slot_with_hash (table, str,
3872 				   htab_hash_string (str), INSERT);
3873   if (*slot == NULL)
3874     {
3875       node = ggc_alloc_cleared_indirect_string_node ();
3876       node->str = ggc_strdup (str);
3877       *slot = node;
3878     }
3879   else
3880     node = (struct indirect_string_node *) *slot;
3881 
3882   node->refcount++;
3883   return node;
3884 }
3885 
3886 /* Add STR to the indirect string hash table.  */
3887 
3888 static struct indirect_string_node *
find_AT_string(const char * str)3889 find_AT_string (const char *str)
3890 {
3891   if (! debug_str_hash)
3892     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3893 				      debug_str_eq, NULL);
3894 
3895   return find_AT_string_in_table (str, debug_str_hash);
3896 }
3897 
3898 /* Add a string attribute value to a DIE.  */
3899 
3900 static inline void
add_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind,const char * str)3901 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3902 {
3903   dw_attr_node attr;
3904   struct indirect_string_node *node;
3905 
3906   node = find_AT_string (str);
3907 
3908   attr.dw_attr = attr_kind;
3909   attr.dw_attr_val.val_class = dw_val_class_str;
3910   attr.dw_attr_val.val_entry = NULL;
3911   attr.dw_attr_val.v.val_str = node;
3912   add_dwarf_attr (die, &attr);
3913 }
3914 
3915 static inline const char *
AT_string(dw_attr_ref a)3916 AT_string (dw_attr_ref a)
3917 {
3918   gcc_assert (a && AT_class (a) == dw_val_class_str);
3919   return a->dw_attr_val.v.val_str->str;
3920 }
3921 
3922 /* Call this function directly to bypass AT_string_form's logic to put
3923    the string inline in the die. */
3924 
3925 static void
set_indirect_string(struct indirect_string_node * node)3926 set_indirect_string (struct indirect_string_node *node)
3927 {
3928   char label[32];
3929   /* Already indirect is a no op.  */
3930   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3931     {
3932       gcc_assert (node->label);
3933       return;
3934     }
3935   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3936   ++dw2_string_counter;
3937   node->label = xstrdup (label);
3938 
3939   if (!dwarf_split_debug_info)
3940     {
3941       node->form = DW_FORM_strp;
3942       node->index = NOT_INDEXED;
3943     }
3944   else
3945     {
3946       node->form = DW_FORM_GNU_str_index;
3947       node->index = NO_INDEX_ASSIGNED;
3948     }
3949 }
3950 
3951 /* Find out whether a string should be output inline in DIE
3952    or out-of-line in .debug_str section.  */
3953 
3954 static enum dwarf_form
find_string_form(struct indirect_string_node * node)3955 find_string_form (struct indirect_string_node *node)
3956 {
3957   unsigned int len;
3958 
3959   if (node->form)
3960     return node->form;
3961 
3962   len = strlen (node->str) + 1;
3963 
3964   /* If the string is shorter or equal to the size of the reference, it is
3965      always better to put it inline.  */
3966   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3967     return node->form = DW_FORM_string;
3968 
3969   /* If we cannot expect the linker to merge strings in .debug_str
3970      section, only put it into .debug_str if it is worth even in this
3971      single module.  */
3972   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3973       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3974       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3975     return node->form = DW_FORM_string;
3976 
3977   set_indirect_string (node);
3978 
3979   return node->form;
3980 }
3981 
3982 /* Find out whether the string referenced from the attribute should be
3983    output inline in DIE or out-of-line in .debug_str section.  */
3984 
3985 static enum dwarf_form
AT_string_form(dw_attr_ref a)3986 AT_string_form (dw_attr_ref a)
3987 {
3988   gcc_assert (a && AT_class (a) == dw_val_class_str);
3989   return find_string_form (a->dw_attr_val.v.val_str);
3990 }
3991 
3992 /* Add a DIE reference attribute value to a DIE.  */
3993 
3994 static inline void
add_AT_die_ref(dw_die_ref die,enum dwarf_attribute attr_kind,dw_die_ref targ_die)3995 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3996 {
3997   dw_attr_node attr;
3998 
3999 #ifdef ENABLE_CHECKING
4000   gcc_assert (targ_die != NULL);
4001 #else
4002   /* With LTO we can end up trying to reference something we didn't create
4003      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4004   if (targ_die == NULL)
4005     return;
4006 #endif
4007 
4008   attr.dw_attr = attr_kind;
4009   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4010   attr.dw_attr_val.val_entry = NULL;
4011   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4012   attr.dw_attr_val.v.val_die_ref.external = 0;
4013   add_dwarf_attr (die, &attr);
4014 }
4015 
4016 /* Change DIE reference REF to point to NEW_DIE instead.  */
4017 
4018 static inline void
change_AT_die_ref(dw_attr_ref ref,dw_die_ref new_die)4019 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4020 {
4021   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4022   ref->dw_attr_val.v.val_die_ref.die = new_die;
4023   ref->dw_attr_val.v.val_die_ref.external = 0;
4024 }
4025 
4026 /* Add an AT_specification attribute to a DIE, and also make the back
4027    pointer from the specification to the definition.  */
4028 
4029 static inline void
add_AT_specification(dw_die_ref die,dw_die_ref targ_die)4030 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4031 {
4032   add_AT_die_ref (die, DW_AT_specification, targ_die);
4033   gcc_assert (!targ_die->die_definition);
4034   targ_die->die_definition = die;
4035 }
4036 
4037 static inline dw_die_ref
AT_ref(dw_attr_ref a)4038 AT_ref (dw_attr_ref a)
4039 {
4040   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4041   return a->dw_attr_val.v.val_die_ref.die;
4042 }
4043 
4044 static inline int
AT_ref_external(dw_attr_ref a)4045 AT_ref_external (dw_attr_ref a)
4046 {
4047   if (a && AT_class (a) == dw_val_class_die_ref)
4048     return a->dw_attr_val.v.val_die_ref.external;
4049 
4050   return 0;
4051 }
4052 
4053 static inline void
set_AT_ref_external(dw_attr_ref a,int i)4054 set_AT_ref_external (dw_attr_ref a, int i)
4055 {
4056   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4057   a->dw_attr_val.v.val_die_ref.external = i;
4058 }
4059 
4060 /* Add an FDE reference attribute value to a DIE.  */
4061 
4062 static inline void
add_AT_fde_ref(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned int targ_fde)4063 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4064 {
4065   dw_attr_node attr;
4066 
4067   attr.dw_attr = attr_kind;
4068   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4069   attr.dw_attr_val.val_entry = NULL;
4070   attr.dw_attr_val.v.val_fde_index = targ_fde;
4071   add_dwarf_attr (die, &attr);
4072 }
4073 
4074 /* Add a location description attribute value to a DIE.  */
4075 
4076 static inline void
add_AT_loc(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_descr_ref loc)4077 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4078 {
4079   dw_attr_node attr;
4080 
4081   attr.dw_attr = attr_kind;
4082   attr.dw_attr_val.val_class = dw_val_class_loc;
4083   attr.dw_attr_val.val_entry = NULL;
4084   attr.dw_attr_val.v.val_loc = loc;
4085   add_dwarf_attr (die, &attr);
4086 }
4087 
4088 static inline dw_loc_descr_ref
AT_loc(dw_attr_ref a)4089 AT_loc (dw_attr_ref a)
4090 {
4091   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4092   return a->dw_attr_val.v.val_loc;
4093 }
4094 
4095 static inline void
add_AT_loc_list(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_list_ref loc_list)4096 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4097 {
4098   dw_attr_node attr;
4099 
4100   attr.dw_attr = attr_kind;
4101   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4102   attr.dw_attr_val.val_entry = NULL;
4103   attr.dw_attr_val.v.val_loc_list = loc_list;
4104   add_dwarf_attr (die, &attr);
4105   have_location_lists = true;
4106 }
4107 
4108 static inline dw_loc_list_ref
AT_loc_list(dw_attr_ref a)4109 AT_loc_list (dw_attr_ref a)
4110 {
4111   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4112   return a->dw_attr_val.v.val_loc_list;
4113 }
4114 
4115 static inline dw_loc_list_ref *
AT_loc_list_ptr(dw_attr_ref a)4116 AT_loc_list_ptr (dw_attr_ref a)
4117 {
4118   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4119   return &a->dw_attr_val.v.val_loc_list;
4120 }
4121 
4122 /* Table of entries into the .debug_addr section.  */
4123 
4124 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4125 
4126 /* Hash an address_table_entry.  */
4127 
4128 static hashval_t
addr_table_entry_do_hash(const void * x)4129 addr_table_entry_do_hash (const void *x)
4130 {
4131   const addr_table_entry *a = (const addr_table_entry *) x;
4132   switch (a->kind)
4133     {
4134       case ate_kind_rtx:
4135         return iterative_hash_rtx (a->addr.rtl, 0);
4136       case ate_kind_rtx_dtprel:
4137         return iterative_hash_rtx (a->addr.rtl, 1);
4138       case ate_kind_label:
4139         return htab_hash_string (a->addr.label);
4140       default:
4141         gcc_unreachable ();
4142     }
4143 }
4144 
4145 /* Determine equality for two address_table_entries.  */
4146 
4147 static int
addr_table_entry_eq(const void * x1,const void * x2)4148 addr_table_entry_eq (const void *x1, const void *x2)
4149 {
4150   const addr_table_entry *a1 = (const addr_table_entry *) x1;
4151   const addr_table_entry *a2 = (const addr_table_entry *) x2;
4152 
4153   if (a1->kind != a2->kind)
4154     return 0;
4155   switch (a1->kind)
4156     {
4157       case ate_kind_rtx:
4158       case ate_kind_rtx_dtprel:
4159         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4160       case ate_kind_label:
4161         return strcmp (a1->addr.label, a2->addr.label) == 0;
4162       default:
4163         gcc_unreachable ();
4164     }
4165 }
4166 
4167 /* Initialize an addr_table_entry.  */
4168 
4169 void
init_addr_table_entry(addr_table_entry * e,enum ate_kind kind,void * addr)4170 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4171 {
4172   e->kind = kind;
4173   switch (kind)
4174     {
4175       case ate_kind_rtx:
4176       case ate_kind_rtx_dtprel:
4177         e->addr.rtl = (rtx) addr;
4178         break;
4179       case ate_kind_label:
4180         e->addr.label = (char *) addr;
4181         break;
4182     }
4183   e->refcount = 0;
4184   e->index = NO_INDEX_ASSIGNED;
4185 }
4186 
4187 /* Add attr to the address table entry to the table.  Defer setting an
4188    index until output time.  */
4189 
4190 static addr_table_entry *
add_addr_table_entry(void * addr,enum ate_kind kind)4191 add_addr_table_entry (void *addr, enum ate_kind kind)
4192 {
4193   addr_table_entry *node;
4194   addr_table_entry finder;
4195   void **slot;
4196 
4197   gcc_assert (dwarf_split_debug_info);
4198   if (! addr_index_table)
4199     addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4200                                         addr_table_entry_eq, NULL);
4201   init_addr_table_entry (&finder, kind, addr);
4202   slot = htab_find_slot (addr_index_table, &finder, INSERT);
4203 
4204   if (*slot == HTAB_EMPTY_ENTRY)
4205     {
4206       node = ggc_alloc_cleared_addr_table_entry ();
4207       init_addr_table_entry (node, kind, addr);
4208       *slot = node;
4209     }
4210   else
4211     node = (addr_table_entry *) *slot;
4212 
4213   node->refcount++;
4214   return node;
4215 }
4216 
4217 /* Remove an entry from the addr table by decrementing its refcount.
4218    Strictly, decrementing the refcount would be enough, but the
4219    assertion that the entry is actually in the table has found
4220    bugs.  */
4221 
4222 static void
remove_addr_table_entry(addr_table_entry * entry)4223 remove_addr_table_entry (addr_table_entry *entry)
4224 {
4225   gcc_assert (dwarf_split_debug_info && addr_index_table);
4226   /* After an index is assigned, the table is frozen.  */
4227   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4228   entry->refcount--;
4229 }
4230 
4231 /* Given a location list, remove all addresses it refers to from the
4232    address_table.  */
4233 
4234 static void
remove_loc_list_addr_table_entries(dw_loc_descr_ref descr)4235 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4236 {
4237   for (; descr; descr = descr->dw_loc_next)
4238     if (descr->dw_loc_oprnd1.val_entry != NULL)
4239       {
4240         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4241         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4242       }
4243 }
4244 
4245 /* A helper function for dwarf2out_finish called through
4246    htab_traverse.  Assign an addr_table_entry its index.  All entries
4247    must be collected into the table when this function is called,
4248    because the indexing code relies on htab_traverse to traverse nodes
4249    in the same order for each run. */
4250 
4251 static int
index_addr_table_entry(void ** h,void * v)4252 index_addr_table_entry (void **h, void *v)
4253 {
4254   addr_table_entry *node = (addr_table_entry *) *h;
4255   unsigned int *index = (unsigned int *) v;
4256 
4257   /* Don't index unreferenced nodes.  */
4258   if (node->refcount == 0)
4259     return 1;
4260 
4261   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4262   node->index = *index;
4263   *index += 1;
4264 
4265   return 1;
4266 }
4267 
4268 /* Add an address constant attribute value to a DIE.  When using
4269    dwarf_split_debug_info, address attributes in dies destined for the
4270    final executable should be direct references--setting the parameter
4271    force_direct ensures this behavior.  */
4272 
4273 static inline void
add_AT_addr(dw_die_ref die,enum dwarf_attribute attr_kind,rtx addr,bool force_direct)4274 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4275              bool force_direct)
4276 {
4277   dw_attr_node attr;
4278 
4279   attr.dw_attr = attr_kind;
4280   attr.dw_attr_val.val_class = dw_val_class_addr;
4281   attr.dw_attr_val.v.val_addr = addr;
4282   if (dwarf_split_debug_info && !force_direct)
4283     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4284   else
4285     attr.dw_attr_val.val_entry = NULL;
4286   add_dwarf_attr (die, &attr);
4287 }
4288 
4289 /* Get the RTX from to an address DIE attribute.  */
4290 
4291 static inline rtx
AT_addr(dw_attr_ref a)4292 AT_addr (dw_attr_ref a)
4293 {
4294   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4295   return a->dw_attr_val.v.val_addr;
4296 }
4297 
4298 /* Add a file attribute value to a DIE.  */
4299 
4300 static inline void
add_AT_file(dw_die_ref die,enum dwarf_attribute attr_kind,struct dwarf_file_data * fd)4301 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4302 	     struct dwarf_file_data *fd)
4303 {
4304   dw_attr_node attr;
4305 
4306   attr.dw_attr = attr_kind;
4307   attr.dw_attr_val.val_class = dw_val_class_file;
4308   attr.dw_attr_val.val_entry = NULL;
4309   attr.dw_attr_val.v.val_file = fd;
4310   add_dwarf_attr (die, &attr);
4311 }
4312 
4313 /* Get the dwarf_file_data from a file DIE attribute.  */
4314 
4315 static inline struct dwarf_file_data *
AT_file(dw_attr_ref a)4316 AT_file (dw_attr_ref a)
4317 {
4318   gcc_assert (a && AT_class (a) == dw_val_class_file);
4319   return a->dw_attr_val.v.val_file;
4320 }
4321 
4322 /* Add a vms delta attribute value to a DIE.  */
4323 
4324 static inline void
add_AT_vms_delta(dw_die_ref die,enum dwarf_attribute attr_kind,const char * lbl1,const char * lbl2)4325 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4326 		  const char *lbl1, const char *lbl2)
4327 {
4328   dw_attr_node attr;
4329 
4330   attr.dw_attr = attr_kind;
4331   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4332   attr.dw_attr_val.val_entry = NULL;
4333   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4334   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4335   add_dwarf_attr (die, &attr);
4336 }
4337 
4338 /* Add a label identifier attribute value to a DIE.  */
4339 
4340 static inline void
add_AT_lbl_id(dw_die_ref die,enum dwarf_attribute attr_kind,const char * lbl_id)4341 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4342                const char *lbl_id)
4343 {
4344   dw_attr_node attr;
4345 
4346   attr.dw_attr = attr_kind;
4347   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4348   attr.dw_attr_val.val_entry = NULL;
4349   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4350   if (dwarf_split_debug_info)
4351     attr.dw_attr_val.val_entry
4352         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4353                                 ate_kind_label);
4354   add_dwarf_attr (die, &attr);
4355 }
4356 
4357 /* Add a section offset attribute value to a DIE, an offset into the
4358    debug_line section.  */
4359 
4360 static inline void
add_AT_lineptr(dw_die_ref die,enum dwarf_attribute attr_kind,const char * label)4361 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4362 		const char *label)
4363 {
4364   dw_attr_node attr;
4365 
4366   attr.dw_attr = attr_kind;
4367   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4368   attr.dw_attr_val.val_entry = NULL;
4369   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4370   add_dwarf_attr (die, &attr);
4371 }
4372 
4373 /* Add a section offset attribute value to a DIE, an offset into the
4374    debug_macinfo section.  */
4375 
4376 static inline void
add_AT_macptr(dw_die_ref die,enum dwarf_attribute attr_kind,const char * label)4377 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4378 	       const char *label)
4379 {
4380   dw_attr_node attr;
4381 
4382   attr.dw_attr = attr_kind;
4383   attr.dw_attr_val.val_class = dw_val_class_macptr;
4384   attr.dw_attr_val.val_entry = NULL;
4385   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4386   add_dwarf_attr (die, &attr);
4387 }
4388 
4389 /* Add an offset attribute value to a DIE.  */
4390 
4391 static inline void
add_AT_offset(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned HOST_WIDE_INT offset)4392 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4393 	       unsigned HOST_WIDE_INT offset)
4394 {
4395   dw_attr_node attr;
4396 
4397   attr.dw_attr = attr_kind;
4398   attr.dw_attr_val.val_class = dw_val_class_offset;
4399   attr.dw_attr_val.val_entry = NULL;
4400   attr.dw_attr_val.v.val_offset = offset;
4401   add_dwarf_attr (die, &attr);
4402 }
4403 
4404 /* Add a range_list attribute value to a DIE.  When using
4405    dwarf_split_debug_info, address attributes in dies destined for the
4406    final executable should be direct references--setting the parameter
4407    force_direct ensures this behavior.  */
4408 
4409 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4410 #define RELOCATED_OFFSET (NULL)
4411 
4412 static void
add_AT_range_list(dw_die_ref die,enum dwarf_attribute attr_kind,long unsigned int offset,bool force_direct)4413 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4414                    long unsigned int offset, bool force_direct)
4415 {
4416   dw_attr_node attr;
4417 
4418   attr.dw_attr = attr_kind;
4419   attr.dw_attr_val.val_class = dw_val_class_range_list;
4420   /* For the range_list attribute, use val_entry to store whether the
4421      offset should follow split-debug-info or normal semantics.  This
4422      value is read in output_range_list_offset.  */
4423   if (dwarf_split_debug_info && !force_direct)
4424     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4425   else
4426     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4427   attr.dw_attr_val.v.val_offset = offset;
4428   add_dwarf_attr (die, &attr);
4429 }
4430 
4431 /* Return the start label of a delta attribute.  */
4432 
4433 static inline const char *
AT_vms_delta1(dw_attr_ref a)4434 AT_vms_delta1 (dw_attr_ref a)
4435 {
4436   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4437   return a->dw_attr_val.v.val_vms_delta.lbl1;
4438 }
4439 
4440 /* Return the end label of a delta attribute.  */
4441 
4442 static inline const char *
AT_vms_delta2(dw_attr_ref a)4443 AT_vms_delta2 (dw_attr_ref a)
4444 {
4445   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4446   return a->dw_attr_val.v.val_vms_delta.lbl2;
4447 }
4448 
4449 static inline const char *
AT_lbl(dw_attr_ref a)4450 AT_lbl (dw_attr_ref a)
4451 {
4452   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4453 		    || AT_class (a) == dw_val_class_lineptr
4454 		    || AT_class (a) == dw_val_class_macptr
4455 		    || AT_class (a) == dw_val_class_high_pc));
4456   return a->dw_attr_val.v.val_lbl_id;
4457 }
4458 
4459 /* Get the attribute of type attr_kind.  */
4460 
4461 static dw_attr_ref
get_AT(dw_die_ref die,enum dwarf_attribute attr_kind)4462 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4463 {
4464   dw_attr_ref a;
4465   unsigned ix;
4466   dw_die_ref spec = NULL;
4467 
4468   if (! die)
4469     return NULL;
4470 
4471   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4472     if (a->dw_attr == attr_kind)
4473       return a;
4474     else if (a->dw_attr == DW_AT_specification
4475 	     || a->dw_attr == DW_AT_abstract_origin)
4476       spec = AT_ref (a);
4477 
4478   if (spec)
4479     return get_AT (spec, attr_kind);
4480 
4481   return NULL;
4482 }
4483 
4484 /* Returns the parent of the declaration of DIE.  */
4485 
4486 static dw_die_ref
get_die_parent(dw_die_ref die)4487 get_die_parent (dw_die_ref die)
4488 {
4489   dw_die_ref t;
4490 
4491   if (!die)
4492     return NULL;
4493 
4494   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4495       || (t = get_AT_ref (die, DW_AT_specification)))
4496     die = t;
4497 
4498   return die->die_parent;
4499 }
4500 
4501 /* Return the "low pc" attribute value, typically associated with a subprogram
4502    DIE.  Return null if the "low pc" attribute is either not present, or if it
4503    cannot be represented as an assembler label identifier.  */
4504 
4505 static inline const char *
get_AT_low_pc(dw_die_ref die)4506 get_AT_low_pc (dw_die_ref die)
4507 {
4508   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4509 
4510   return a ? AT_lbl (a) : NULL;
4511 }
4512 
4513 /* Return the "high pc" attribute value, typically associated with a subprogram
4514    DIE.  Return null if the "high pc" attribute is either not present, or if it
4515    cannot be represented as an assembler label identifier.  */
4516 
4517 static inline const char *
get_AT_hi_pc(dw_die_ref die)4518 get_AT_hi_pc (dw_die_ref die)
4519 {
4520   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4521 
4522   return a ? AT_lbl (a) : NULL;
4523 }
4524 
4525 /* Return the value of the string attribute designated by ATTR_KIND, or
4526    NULL if it is not present.  */
4527 
4528 static inline const char *
get_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind)4529 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4530 {
4531   dw_attr_ref a = get_AT (die, attr_kind);
4532 
4533   return a ? AT_string (a) : NULL;
4534 }
4535 
4536 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4537    if it is not present.  */
4538 
4539 static inline int
get_AT_flag(dw_die_ref die,enum dwarf_attribute attr_kind)4540 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4541 {
4542   dw_attr_ref a = get_AT (die, attr_kind);
4543 
4544   return a ? AT_flag (a) : 0;
4545 }
4546 
4547 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4548    if it is not present.  */
4549 
4550 static inline unsigned
get_AT_unsigned(dw_die_ref die,enum dwarf_attribute attr_kind)4551 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4552 {
4553   dw_attr_ref a = get_AT (die, attr_kind);
4554 
4555   return a ? AT_unsigned (a) : 0;
4556 }
4557 
4558 static inline dw_die_ref
get_AT_ref(dw_die_ref die,enum dwarf_attribute attr_kind)4559 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4560 {
4561   dw_attr_ref a = get_AT (die, attr_kind);
4562 
4563   return a ? AT_ref (a) : NULL;
4564 }
4565 
4566 static inline struct dwarf_file_data *
get_AT_file(dw_die_ref die,enum dwarf_attribute attr_kind)4567 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4568 {
4569   dw_attr_ref a = get_AT (die, attr_kind);
4570 
4571   return a ? AT_file (a) : NULL;
4572 }
4573 
4574 /* Return TRUE if the language is C++.  */
4575 
4576 static inline bool
is_cxx(void)4577 is_cxx (void)
4578 {
4579   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4580 
4581   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4582 }
4583 
4584 /* Return TRUE if the language is Java.  */
4585 
4586 static inline bool
is_java(void)4587 is_java (void)
4588 {
4589   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4590 
4591   return lang == DW_LANG_Java;
4592 }
4593 
4594 /* Return TRUE if the language is Fortran.  */
4595 
4596 static inline bool
is_fortran(void)4597 is_fortran (void)
4598 {
4599   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4600 
4601   return (lang == DW_LANG_Fortran77
4602 	  || lang == DW_LANG_Fortran90
4603 	  || lang == DW_LANG_Fortran95);
4604 }
4605 
4606 /* Return TRUE if the language is Ada.  */
4607 
4608 static inline bool
is_ada(void)4609 is_ada (void)
4610 {
4611   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4612 
4613   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4614 }
4615 
4616 /* Remove the specified attribute if present.  */
4617 
4618 static void
remove_AT(dw_die_ref die,enum dwarf_attribute attr_kind)4619 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4620 {
4621   dw_attr_ref a;
4622   unsigned ix;
4623 
4624   if (! die)
4625     return;
4626 
4627   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4628     if (a->dw_attr == attr_kind)
4629       {
4630 	if (AT_class (a) == dw_val_class_str)
4631 	  if (a->dw_attr_val.v.val_str->refcount)
4632 	    a->dw_attr_val.v.val_str->refcount--;
4633 
4634 	/* vec::ordered_remove should help reduce the number of abbrevs
4635 	   that are needed.  */
4636 	die->die_attr->ordered_remove (ix);
4637 	return;
4638       }
4639 }
4640 
4641 /* Remove CHILD from its parent.  PREV must have the property that
4642    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4643 
4644 static void
remove_child_with_prev(dw_die_ref child,dw_die_ref prev)4645 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4646 {
4647   gcc_assert (child->die_parent == prev->die_parent);
4648   gcc_assert (prev->die_sib == child);
4649   if (prev == child)
4650     {
4651       gcc_assert (child->die_parent->die_child == child);
4652       prev = NULL;
4653     }
4654   else
4655     prev->die_sib = child->die_sib;
4656   if (child->die_parent->die_child == child)
4657     child->die_parent->die_child = prev;
4658 }
4659 
4660 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4661    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4662 
4663 static void
replace_child(dw_die_ref old_child,dw_die_ref new_child,dw_die_ref prev)4664 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4665 {
4666   dw_die_ref parent = old_child->die_parent;
4667 
4668   gcc_assert (parent == prev->die_parent);
4669   gcc_assert (prev->die_sib == old_child);
4670 
4671   new_child->die_parent = parent;
4672   if (prev == old_child)
4673     {
4674       gcc_assert (parent->die_child == old_child);
4675       new_child->die_sib = new_child;
4676     }
4677   else
4678     {
4679       prev->die_sib = new_child;
4680       new_child->die_sib = old_child->die_sib;
4681     }
4682   if (old_child->die_parent->die_child == old_child)
4683     old_child->die_parent->die_child = new_child;
4684 }
4685 
4686 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4687 
4688 static void
move_all_children(dw_die_ref old_parent,dw_die_ref new_parent)4689 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4690 {
4691   dw_die_ref c;
4692   new_parent->die_child = old_parent->die_child;
4693   old_parent->die_child = NULL;
4694   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4695 }
4696 
4697 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4698    matches TAG.  */
4699 
4700 static void
remove_child_TAG(dw_die_ref die,enum dwarf_tag tag)4701 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4702 {
4703   dw_die_ref c;
4704 
4705   c = die->die_child;
4706   if (c) do {
4707     dw_die_ref prev = c;
4708     c = c->die_sib;
4709     while (c->die_tag == tag)
4710       {
4711 	remove_child_with_prev (c, prev);
4712 	/* Might have removed every child.  */
4713 	if (c == c->die_sib)
4714 	  return;
4715 	c = c->die_sib;
4716       }
4717   } while (c != die->die_child);
4718 }
4719 
4720 /* Add a CHILD_DIE as the last child of DIE.  */
4721 
4722 static void
add_child_die(dw_die_ref die,dw_die_ref child_die)4723 add_child_die (dw_die_ref die, dw_die_ref child_die)
4724 {
4725   /* FIXME this should probably be an assert.  */
4726   if (! die || ! child_die)
4727     return;
4728   gcc_assert (die != child_die);
4729 
4730   child_die->die_parent = die;
4731   if (die->die_child)
4732     {
4733       child_die->die_sib = die->die_child->die_sib;
4734       die->die_child->die_sib = child_die;
4735     }
4736   else
4737     child_die->die_sib = child_die;
4738   die->die_child = child_die;
4739 }
4740 
4741 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4742    is the specification, to the end of PARENT's list of children.
4743    This is done by removing and re-adding it.  */
4744 
4745 static void
splice_child_die(dw_die_ref parent,dw_die_ref child)4746 splice_child_die (dw_die_ref parent, dw_die_ref child)
4747 {
4748   dw_die_ref p;
4749 
4750   /* We want the declaration DIE from inside the class, not the
4751      specification DIE at toplevel.  */
4752   if (child->die_parent != parent)
4753     {
4754       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4755 
4756       if (tmp)
4757 	child = tmp;
4758     }
4759 
4760   gcc_assert (child->die_parent == parent
4761 	      || (child->die_parent
4762 		  == get_AT_ref (parent, DW_AT_specification)));
4763 
4764   for (p = child->die_parent->die_child; ; p = p->die_sib)
4765     if (p->die_sib == child)
4766       {
4767 	remove_child_with_prev (child, p);
4768 	break;
4769       }
4770 
4771   add_child_die (parent, child);
4772 }
4773 
4774 /* Return a pointer to a newly created DIE node.  */
4775 
4776 static inline dw_die_ref
new_die(enum dwarf_tag tag_value,dw_die_ref parent_die,tree t)4777 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4778 {
4779   dw_die_ref die = ggc_alloc_cleared_die_node ();
4780 
4781   die->die_tag = tag_value;
4782 
4783   if (parent_die != NULL)
4784     add_child_die (parent_die, die);
4785   else
4786     {
4787       limbo_die_node *limbo_node;
4788 
4789       limbo_node = ggc_alloc_cleared_limbo_die_node ();
4790       limbo_node->die = die;
4791       limbo_node->created_for = t;
4792       limbo_node->next = limbo_die_list;
4793       limbo_die_list = limbo_node;
4794     }
4795 
4796   return die;
4797 }
4798 
4799 /* Return the DIE associated with the given type specifier.  */
4800 
4801 static inline dw_die_ref
lookup_type_die(tree type)4802 lookup_type_die (tree type)
4803 {
4804   return TYPE_SYMTAB_DIE (type);
4805 }
4806 
4807 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4808    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4809    anonymous type instead the one of the naming typedef.  */
4810 
4811 static inline dw_die_ref
strip_naming_typedef(tree type,dw_die_ref type_die)4812 strip_naming_typedef (tree type, dw_die_ref type_die)
4813 {
4814   if (type
4815       && TREE_CODE (type) == RECORD_TYPE
4816       && type_die
4817       && type_die->die_tag == DW_TAG_typedef
4818       && is_naming_typedef_decl (TYPE_NAME (type)))
4819     type_die = get_AT_ref (type_die, DW_AT_type);
4820   return type_die;
4821 }
4822 
4823 /* Like lookup_type_die, but if type is an anonymous type named by a
4824    typedef[1], return the DIE of the anonymous type instead the one of
4825    the naming typedef.  This is because in gen_typedef_die, we did
4826    equate the anonymous struct named by the typedef with the DIE of
4827    the naming typedef. So by default, lookup_type_die on an anonymous
4828    struct yields the DIE of the naming typedef.
4829 
4830    [1]: Read the comment of is_naming_typedef_decl to learn about what
4831    a naming typedef is.  */
4832 
4833 static inline dw_die_ref
lookup_type_die_strip_naming_typedef(tree type)4834 lookup_type_die_strip_naming_typedef (tree type)
4835 {
4836   dw_die_ref die = lookup_type_die (type);
4837   return strip_naming_typedef (type, die);
4838 }
4839 
4840 /* Equate a DIE to a given type specifier.  */
4841 
4842 static inline void
equate_type_number_to_die(tree type,dw_die_ref type_die)4843 equate_type_number_to_die (tree type, dw_die_ref type_die)
4844 {
4845   TYPE_SYMTAB_DIE (type) = type_die;
4846 }
4847 
4848 /* Returns a hash value for X (which really is a die_struct).  */
4849 
4850 static hashval_t
decl_die_table_hash(const void * x)4851 decl_die_table_hash (const void *x)
4852 {
4853   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4854 }
4855 
4856 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
4857 
4858 static int
decl_die_table_eq(const void * x,const void * y)4859 decl_die_table_eq (const void *x, const void *y)
4860 {
4861   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4862 }
4863 
4864 /* Return the DIE associated with a given declaration.  */
4865 
4866 static inline dw_die_ref
lookup_decl_die(tree decl)4867 lookup_decl_die (tree decl)
4868 {
4869   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4870 }
4871 
4872 /* Returns a hash value for X (which really is a var_loc_list).  */
4873 
4874 static hashval_t
decl_loc_table_hash(const void * x)4875 decl_loc_table_hash (const void *x)
4876 {
4877   return (hashval_t) ((const var_loc_list *) x)->decl_id;
4878 }
4879 
4880 /* Return nonzero if decl_id of var_loc_list X is the same as
4881    UID of decl *Y.  */
4882 
4883 static int
decl_loc_table_eq(const void * x,const void * y)4884 decl_loc_table_eq (const void *x, const void *y)
4885 {
4886   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4887 }
4888 
4889 /* Return the var_loc list associated with a given declaration.  */
4890 
4891 static inline var_loc_list *
lookup_decl_loc(const_tree decl)4892 lookup_decl_loc (const_tree decl)
4893 {
4894   if (!decl_loc_table)
4895     return NULL;
4896   return (var_loc_list *)
4897     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4898 }
4899 
4900 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
4901 
4902 static hashval_t
cached_dw_loc_list_table_hash(const void * x)4903 cached_dw_loc_list_table_hash (const void *x)
4904 {
4905   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4906 }
4907 
4908 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4909    UID of decl *Y.  */
4910 
4911 static int
cached_dw_loc_list_table_eq(const void * x,const void * y)4912 cached_dw_loc_list_table_eq (const void *x, const void *y)
4913 {
4914   return (((const cached_dw_loc_list *) x)->decl_id
4915 	  == DECL_UID ((const_tree) y));
4916 }
4917 
4918 /* Equate a DIE to a particular declaration.  */
4919 
4920 static void
equate_decl_number_to_die(tree decl,dw_die_ref decl_die)4921 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4922 {
4923   unsigned int decl_id = DECL_UID (decl);
4924   void **slot;
4925 
4926   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4927   *slot = decl_die;
4928   decl_die->decl_id = decl_id;
4929 }
4930 
4931 /* Return how many bits covers PIECE EXPR_LIST.  */
4932 
4933 static int
decl_piece_bitsize(rtx piece)4934 decl_piece_bitsize (rtx piece)
4935 {
4936   int ret = (int) GET_MODE (piece);
4937   if (ret)
4938     return ret;
4939   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4940 	      && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4941   return INTVAL (XEXP (XEXP (piece, 0), 0));
4942 }
4943 
4944 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
4945 
4946 static rtx *
decl_piece_varloc_ptr(rtx piece)4947 decl_piece_varloc_ptr (rtx piece)
4948 {
4949   if ((int) GET_MODE (piece))
4950     return &XEXP (piece, 0);
4951   else
4952     return &XEXP (XEXP (piece, 0), 1);
4953 }
4954 
4955 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4956    Next is the chain of following piece nodes.  */
4957 
4958 static rtx
decl_piece_node(rtx loc_note,HOST_WIDE_INT bitsize,rtx next)4959 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4960 {
4961   if (bitsize <= (int) MAX_MACHINE_MODE)
4962     return alloc_EXPR_LIST (bitsize, loc_note, next);
4963   else
4964     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4965 					       GEN_INT (bitsize),
4966 					       loc_note), next);
4967 }
4968 
4969 /* Return rtx that should be stored into loc field for
4970    LOC_NOTE and BITPOS/BITSIZE.  */
4971 
4972 static rtx
construct_piece_list(rtx loc_note,HOST_WIDE_INT bitpos,HOST_WIDE_INT bitsize)4973 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4974 		      HOST_WIDE_INT bitsize)
4975 {
4976   if (bitsize != -1)
4977     {
4978       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4979       if (bitpos != 0)
4980 	loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4981     }
4982   return loc_note;
4983 }
4984 
4985 /* This function either modifies location piece list *DEST in
4986    place (if SRC and INNER is NULL), or copies location piece list
4987    *SRC to *DEST while modifying it.  Location BITPOS is modified
4988    to contain LOC_NOTE, any pieces overlapping it are removed resp.
4989    not copied and if needed some padding around it is added.
4990    When modifying in place, DEST should point to EXPR_LIST where
4991    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4992    to the start of the whole list and INNER points to the EXPR_LIST
4993    where earlier pieces cover PIECE_BITPOS bits.  */
4994 
4995 static void
adjust_piece_list(rtx * dest,rtx * src,rtx * inner,HOST_WIDE_INT bitpos,HOST_WIDE_INT piece_bitpos,HOST_WIDE_INT bitsize,rtx loc_note)4996 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4997 		   HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4998 		   HOST_WIDE_INT bitsize, rtx loc_note)
4999 {
5000   int diff;
5001   bool copy = inner != NULL;
5002 
5003   if (copy)
5004     {
5005       /* First copy all nodes preceding the current bitpos.  */
5006       while (src != inner)
5007 	{
5008 	  *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5009 				   decl_piece_bitsize (*src), NULL_RTX);
5010 	  dest = &XEXP (*dest, 1);
5011 	  src = &XEXP (*src, 1);
5012 	}
5013     }
5014   /* Add padding if needed.  */
5015   if (bitpos != piece_bitpos)
5016     {
5017       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5018 			       copy ? NULL_RTX : *dest);
5019       dest = &XEXP (*dest, 1);
5020     }
5021   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5022     {
5023       gcc_assert (!copy);
5024       /* A piece with correct bitpos and bitsize already exist,
5025 	 just update the location for it and return.  */
5026       *decl_piece_varloc_ptr (*dest) = loc_note;
5027       return;
5028     }
5029   /* Add the piece that changed.  */
5030   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5031   dest = &XEXP (*dest, 1);
5032   /* Skip over pieces that overlap it.  */
5033   diff = bitpos - piece_bitpos + bitsize;
5034   if (!copy)
5035     src = dest;
5036   while (diff > 0 && *src)
5037     {
5038       rtx piece = *src;
5039       diff -= decl_piece_bitsize (piece);
5040       if (copy)
5041 	src = &XEXP (piece, 1);
5042       else
5043 	{
5044 	  *src = XEXP (piece, 1);
5045 	  free_EXPR_LIST_node (piece);
5046 	}
5047     }
5048   /* Add padding if needed.  */
5049   if (diff < 0 && *src)
5050     {
5051       if (!copy)
5052 	dest = src;
5053       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5054       dest = &XEXP (*dest, 1);
5055     }
5056   if (!copy)
5057     return;
5058   /* Finally copy all nodes following it.  */
5059   while (*src)
5060     {
5061       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5062 			       decl_piece_bitsize (*src), NULL_RTX);
5063       dest = &XEXP (*dest, 1);
5064       src = &XEXP (*src, 1);
5065     }
5066 }
5067 
5068 /* Add a variable location node to the linked list for DECL.  */
5069 
5070 static struct var_loc_node *
add_var_loc_to_decl(tree decl,rtx loc_note,const char * label)5071 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5072 {
5073   unsigned int decl_id;
5074   var_loc_list *temp;
5075   void **slot;
5076   struct var_loc_node *loc = NULL;
5077   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5078 
5079   if (TREE_CODE (decl) == VAR_DECL
5080       && DECL_HAS_DEBUG_EXPR_P (decl))
5081     {
5082       tree realdecl = DECL_DEBUG_EXPR (decl);
5083       if (handled_component_p (realdecl)
5084 	  || (TREE_CODE (realdecl) == MEM_REF
5085 	      && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5086 	{
5087 	  HOST_WIDE_INT maxsize;
5088 	  tree innerdecl;
5089 	  innerdecl
5090 	    = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5091 	  if (!DECL_P (innerdecl)
5092 	      || DECL_IGNORED_P (innerdecl)
5093 	      || TREE_STATIC (innerdecl)
5094 	      || bitsize <= 0
5095 	      || bitpos + bitsize > 256
5096 	      || bitsize != maxsize)
5097 	    return NULL;
5098 	  decl = innerdecl;
5099 	}
5100     }
5101 
5102   decl_id = DECL_UID (decl);
5103   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5104   if (*slot == NULL)
5105     {
5106       temp = ggc_alloc_cleared_var_loc_list ();
5107       temp->decl_id = decl_id;
5108       *slot = temp;
5109     }
5110   else
5111     temp = (var_loc_list *) *slot;
5112 
5113   /* For PARM_DECLs try to keep around the original incoming value,
5114      even if that means we'll emit a zero-range .debug_loc entry.  */
5115   if (temp->last
5116       && temp->first == temp->last
5117       && TREE_CODE (decl) == PARM_DECL
5118       && NOTE_P (temp->first->loc)
5119       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5120       && DECL_INCOMING_RTL (decl)
5121       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5122       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5123 	 == GET_CODE (DECL_INCOMING_RTL (decl))
5124       && prev_real_insn (temp->first->loc) == NULL_RTX
5125       && (bitsize != -1
5126 	  || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5127 			   NOTE_VAR_LOCATION_LOC (loc_note))
5128 	  || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5129 	      != NOTE_VAR_LOCATION_STATUS (loc_note))))
5130     {
5131       loc = ggc_alloc_cleared_var_loc_node ();
5132       temp->first->next = loc;
5133       temp->last = loc;
5134       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5135     }
5136   else if (temp->last)
5137     {
5138       struct var_loc_node *last = temp->last, *unused = NULL;
5139       rtx *piece_loc = NULL, last_loc_note;
5140       int piece_bitpos = 0;
5141       if (last->next)
5142 	{
5143 	  last = last->next;
5144 	  gcc_assert (last->next == NULL);
5145 	}
5146       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5147 	{
5148 	  piece_loc = &last->loc;
5149 	  do
5150 	    {
5151 	      int cur_bitsize = decl_piece_bitsize (*piece_loc);
5152 	      if (piece_bitpos + cur_bitsize > bitpos)
5153 		break;
5154 	      piece_bitpos += cur_bitsize;
5155 	      piece_loc = &XEXP (*piece_loc, 1);
5156 	    }
5157 	  while (*piece_loc);
5158 	}
5159       /* TEMP->LAST here is either pointer to the last but one or
5160 	 last element in the chained list, LAST is pointer to the
5161 	 last element.  */
5162       if (label && strcmp (last->label, label) == 0)
5163 	{
5164 	  /* For SRA optimized variables if there weren't any real
5165 	     insns since last note, just modify the last node.  */
5166 	  if (piece_loc != NULL)
5167 	    {
5168 	      adjust_piece_list (piece_loc, NULL, NULL,
5169 				 bitpos, piece_bitpos, bitsize, loc_note);
5170 	      return NULL;
5171 	    }
5172 	  /* If the last note doesn't cover any instructions, remove it.  */
5173 	  if (temp->last != last)
5174 	    {
5175 	      temp->last->next = NULL;
5176 	      unused = last;
5177 	      last = temp->last;
5178 	      gcc_assert (strcmp (last->label, label) != 0);
5179 	    }
5180 	  else
5181 	    {
5182 	      gcc_assert (temp->first == temp->last
5183 			  || (temp->first->next == temp->last
5184 			      && TREE_CODE (decl) == PARM_DECL));
5185 	      memset (temp->last, '\0', sizeof (*temp->last));
5186 	      temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5187 	      return temp->last;
5188 	    }
5189 	}
5190       if (bitsize == -1 && NOTE_P (last->loc))
5191 	last_loc_note = last->loc;
5192       else if (piece_loc != NULL
5193 	       && *piece_loc != NULL_RTX
5194 	       && piece_bitpos == bitpos
5195 	       && decl_piece_bitsize (*piece_loc) == bitsize)
5196 	last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5197       else
5198 	last_loc_note = NULL_RTX;
5199       /* If the current location is the same as the end of the list,
5200 	 and either both or neither of the locations is uninitialized,
5201 	 we have nothing to do.  */
5202       if (last_loc_note == NULL_RTX
5203 	  || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5204 			    NOTE_VAR_LOCATION_LOC (loc_note)))
5205 	  || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5206 	       != NOTE_VAR_LOCATION_STATUS (loc_note))
5207 	      && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5208 		   == VAR_INIT_STATUS_UNINITIALIZED)
5209 		  || (NOTE_VAR_LOCATION_STATUS (loc_note)
5210 		      == VAR_INIT_STATUS_UNINITIALIZED))))
5211 	{
5212 	  /* Add LOC to the end of list and update LAST.  If the last
5213 	     element of the list has been removed above, reuse its
5214 	     memory for the new node, otherwise allocate a new one.  */
5215 	  if (unused)
5216 	    {
5217 	      loc = unused;
5218 	      memset (loc, '\0', sizeof (*loc));
5219 	    }
5220 	  else
5221 	    loc = ggc_alloc_cleared_var_loc_node ();
5222 	  if (bitsize == -1 || piece_loc == NULL)
5223 	    loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5224 	  else
5225 	    adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5226 			       bitpos, piece_bitpos, bitsize, loc_note);
5227 	  last->next = loc;
5228 	  /* Ensure TEMP->LAST will point either to the new last but one
5229 	     element of the chain, or to the last element in it.  */
5230 	  if (last != temp->last)
5231 	    temp->last = last;
5232 	}
5233       else if (unused)
5234 	ggc_free (unused);
5235     }
5236   else
5237     {
5238       loc = ggc_alloc_cleared_var_loc_node ();
5239       temp->first = loc;
5240       temp->last = loc;
5241       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5242     }
5243   return loc;
5244 }
5245 
5246 /* Keep track of the number of spaces used to indent the
5247    output of the debugging routines that print the structure of
5248    the DIE internal representation.  */
5249 static int print_indent;
5250 
5251 /* Indent the line the number of spaces given by print_indent.  */
5252 
5253 static inline void
print_spaces(FILE * outfile)5254 print_spaces (FILE *outfile)
5255 {
5256   fprintf (outfile, "%*s", print_indent, "");
5257 }
5258 
5259 /* Print a type signature in hex.  */
5260 
5261 static inline void
print_signature(FILE * outfile,char * sig)5262 print_signature (FILE *outfile, char *sig)
5263 {
5264   int i;
5265 
5266   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5267     fprintf (outfile, "%02x", sig[i] & 0xff);
5268 }
5269 
5270 /* Print the information associated with a given DIE, and its children.
5271    This routine is a debugging aid only.  */
5272 
5273 static void
print_die(dw_die_ref die,FILE * outfile)5274 print_die (dw_die_ref die, FILE *outfile)
5275 {
5276   dw_attr_ref a;
5277   dw_die_ref c;
5278   unsigned ix;
5279 
5280   print_spaces (outfile);
5281   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5282 	   die->die_offset, dwarf_tag_name (die->die_tag),
5283 	   (void*) die);
5284   print_spaces (outfile);
5285   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5286   fprintf (outfile, " offset: %ld", die->die_offset);
5287   fprintf (outfile, " mark: %d\n", die->die_mark);
5288 
5289   if (die->comdat_type_p)
5290     {
5291       print_spaces (outfile);
5292       fprintf (outfile, "  signature: ");
5293       print_signature (outfile, die->die_id.die_type_node->signature);
5294       fprintf (outfile, "\n");
5295     }
5296 
5297   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5298     {
5299       print_spaces (outfile);
5300       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5301 
5302       switch (AT_class (a))
5303 	{
5304 	case dw_val_class_addr:
5305 	  fprintf (outfile, "address");
5306 	  break;
5307 	case dw_val_class_offset:
5308 	  fprintf (outfile, "offset");
5309 	  break;
5310 	case dw_val_class_loc:
5311 	  fprintf (outfile, "location descriptor");
5312 	  break;
5313 	case dw_val_class_loc_list:
5314 	  fprintf (outfile, "location list -> label:%s",
5315 		   AT_loc_list (a)->ll_symbol);
5316 	  break;
5317 	case dw_val_class_range_list:
5318 	  fprintf (outfile, "range list");
5319 	  break;
5320 	case dw_val_class_const:
5321 	  fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5322 	  break;
5323 	case dw_val_class_unsigned_const:
5324 	  fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5325 	  break;
5326 	case dw_val_class_const_double:
5327 	  fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC ","\
5328 			    HOST_WIDE_INT_PRINT_UNSIGNED ")",
5329 		   a->dw_attr_val.v.val_double.high,
5330 		   a->dw_attr_val.v.val_double.low);
5331 	  break;
5332 	case dw_val_class_vec:
5333 	  fprintf (outfile, "floating-point or vector constant");
5334 	  break;
5335 	case dw_val_class_flag:
5336 	  fprintf (outfile, "%u", AT_flag (a));
5337 	  break;
5338 	case dw_val_class_die_ref:
5339 	  if (AT_ref (a) != NULL)
5340 	    {
5341 	      if (AT_ref (a)->comdat_type_p)
5342 	        {
5343 		  fprintf (outfile, "die -> signature: ");
5344 		  print_signature (outfile,
5345 		  		   AT_ref (a)->die_id.die_type_node->signature);
5346                 }
5347 	      else if (AT_ref (a)->die_id.die_symbol)
5348 		fprintf (outfile, "die -> label: %s",
5349 		         AT_ref (a)->die_id.die_symbol);
5350 	      else
5351 		fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5352 	      fprintf (outfile, " (%p)", (void *) AT_ref (a));
5353 	    }
5354 	  else
5355 	    fprintf (outfile, "die -> <null>");
5356 	  break;
5357 	case dw_val_class_vms_delta:
5358 	  fprintf (outfile, "delta: @slotcount(%s-%s)",
5359 		   AT_vms_delta2 (a), AT_vms_delta1 (a));
5360 	  break;
5361 	case dw_val_class_lbl_id:
5362 	case dw_val_class_lineptr:
5363 	case dw_val_class_macptr:
5364 	case dw_val_class_high_pc:
5365 	  fprintf (outfile, "label: %s", AT_lbl (a));
5366 	  break;
5367 	case dw_val_class_str:
5368 	  if (AT_string (a) != NULL)
5369 	    fprintf (outfile, "\"%s\"", AT_string (a));
5370 	  else
5371 	    fprintf (outfile, "<null>");
5372 	  break;
5373 	case dw_val_class_file:
5374 	  fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5375 		   AT_file (a)->emitted_number);
5376 	  break;
5377 	case dw_val_class_data8:
5378 	  {
5379 	    int i;
5380 
5381             for (i = 0; i < 8; i++)
5382               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5383 	    break;
5384           }
5385 	default:
5386 	  break;
5387 	}
5388 
5389       fprintf (outfile, "\n");
5390     }
5391 
5392   if (die->die_child != NULL)
5393     {
5394       print_indent += 4;
5395       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5396       print_indent -= 4;
5397     }
5398   if (print_indent == 0)
5399     fprintf (outfile, "\n");
5400 }
5401 
5402 /* Print the information collected for a given DIE.  */
5403 
5404 DEBUG_FUNCTION void
debug_dwarf_die(dw_die_ref die)5405 debug_dwarf_die (dw_die_ref die)
5406 {
5407   print_die (die, stderr);
5408 }
5409 
5410 DEBUG_FUNCTION void
debug(die_struct & ref)5411 debug (die_struct &ref)
5412 {
5413   print_die (&ref, stderr);
5414 }
5415 
5416 DEBUG_FUNCTION void
debug(die_struct * ptr)5417 debug (die_struct *ptr)
5418 {
5419   if (ptr)
5420     debug (*ptr);
5421   else
5422     fprintf (stderr, "<nil>\n");
5423 }
5424 
5425 
5426 /* Print all DWARF information collected for the compilation unit.
5427    This routine is a debugging aid only.  */
5428 
5429 DEBUG_FUNCTION void
debug_dwarf(void)5430 debug_dwarf (void)
5431 {
5432   print_indent = 0;
5433   print_die (comp_unit_die (), stderr);
5434 }
5435 
5436 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5437    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5438    DIE that marks the start of the DIEs for this include file.  */
5439 
5440 static dw_die_ref
push_new_compile_unit(dw_die_ref old_unit,dw_die_ref bincl_die)5441 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5442 {
5443   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5444   dw_die_ref new_unit = gen_compile_unit_die (filename);
5445 
5446   new_unit->die_sib = old_unit;
5447   return new_unit;
5448 }
5449 
5450 /* Close an include-file CU and reopen the enclosing one.  */
5451 
5452 static dw_die_ref
pop_compile_unit(dw_die_ref old_unit)5453 pop_compile_unit (dw_die_ref old_unit)
5454 {
5455   dw_die_ref new_unit = old_unit->die_sib;
5456 
5457   old_unit->die_sib = NULL;
5458   return new_unit;
5459 }
5460 
5461 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5462 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5463 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5464 
5465 /* Calculate the checksum of a location expression.  */
5466 
5467 static inline void
loc_checksum(dw_loc_descr_ref loc,struct md5_ctx * ctx)5468 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5469 {
5470   int tem;
5471   hashval_t hash = 0;
5472 
5473   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5474   CHECKSUM (tem);
5475   hash = hash_loc_operands (loc, hash);
5476   CHECKSUM (hash);
5477 }
5478 
5479 /* Calculate the checksum of an attribute.  */
5480 
5481 static void
attr_checksum(dw_attr_ref at,struct md5_ctx * ctx,int * mark)5482 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5483 {
5484   dw_loc_descr_ref loc;
5485   rtx r;
5486 
5487   CHECKSUM (at->dw_attr);
5488 
5489   /* We don't care that this was compiled with a different compiler
5490      snapshot; if the output is the same, that's what matters.  */
5491   if (at->dw_attr == DW_AT_producer)
5492     return;
5493 
5494   switch (AT_class (at))
5495     {
5496     case dw_val_class_const:
5497       CHECKSUM (at->dw_attr_val.v.val_int);
5498       break;
5499     case dw_val_class_unsigned_const:
5500       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5501       break;
5502     case dw_val_class_const_double:
5503       CHECKSUM (at->dw_attr_val.v.val_double);
5504       break;
5505     case dw_val_class_vec:
5506       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5507 		      (at->dw_attr_val.v.val_vec.length
5508 		       * at->dw_attr_val.v.val_vec.elt_size));
5509       break;
5510     case dw_val_class_flag:
5511       CHECKSUM (at->dw_attr_val.v.val_flag);
5512       break;
5513     case dw_val_class_str:
5514       CHECKSUM_STRING (AT_string (at));
5515       break;
5516 
5517     case dw_val_class_addr:
5518       r = AT_addr (at);
5519       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5520       CHECKSUM_STRING (XSTR (r, 0));
5521       break;
5522 
5523     case dw_val_class_offset:
5524       CHECKSUM (at->dw_attr_val.v.val_offset);
5525       break;
5526 
5527     case dw_val_class_loc:
5528       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5529 	loc_checksum (loc, ctx);
5530       break;
5531 
5532     case dw_val_class_die_ref:
5533       die_checksum (AT_ref (at), ctx, mark);
5534       break;
5535 
5536     case dw_val_class_fde_ref:
5537     case dw_val_class_vms_delta:
5538     case dw_val_class_lbl_id:
5539     case dw_val_class_lineptr:
5540     case dw_val_class_macptr:
5541     case dw_val_class_high_pc:
5542       break;
5543 
5544     case dw_val_class_file:
5545       CHECKSUM_STRING (AT_file (at)->filename);
5546       break;
5547 
5548     case dw_val_class_data8:
5549       CHECKSUM (at->dw_attr_val.v.val_data8);
5550       break;
5551 
5552     default:
5553       break;
5554     }
5555 }
5556 
5557 /* Calculate the checksum of a DIE.  */
5558 
5559 static void
die_checksum(dw_die_ref die,struct md5_ctx * ctx,int * mark)5560 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5561 {
5562   dw_die_ref c;
5563   dw_attr_ref a;
5564   unsigned ix;
5565 
5566   /* To avoid infinite recursion.  */
5567   if (die->die_mark)
5568     {
5569       CHECKSUM (die->die_mark);
5570       return;
5571     }
5572   die->die_mark = ++(*mark);
5573 
5574   CHECKSUM (die->die_tag);
5575 
5576   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5577     attr_checksum (a, ctx, mark);
5578 
5579   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5580 }
5581 
5582 #undef CHECKSUM
5583 #undef CHECKSUM_BLOCK
5584 #undef CHECKSUM_STRING
5585 
5586 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5587 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5588 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5589 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5590 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5591 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5592 #define CHECKSUM_ATTR(FOO) \
5593   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5594 
5595 /* Calculate the checksum of a number in signed LEB128 format.  */
5596 
5597 static void
checksum_sleb128(HOST_WIDE_INT value,struct md5_ctx * ctx)5598 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5599 {
5600   unsigned char byte;
5601   bool more;
5602 
5603   while (1)
5604     {
5605       byte = (value & 0x7f);
5606       value >>= 7;
5607       more = !((value == 0 && (byte & 0x40) == 0)
5608 		|| (value == -1 && (byte & 0x40) != 0));
5609       if (more)
5610 	byte |= 0x80;
5611       CHECKSUM (byte);
5612       if (!more)
5613 	break;
5614     }
5615 }
5616 
5617 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5618 
5619 static void
checksum_uleb128(unsigned HOST_WIDE_INT value,struct md5_ctx * ctx)5620 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5621 {
5622   while (1)
5623     {
5624       unsigned char byte = (value & 0x7f);
5625       value >>= 7;
5626       if (value != 0)
5627 	/* More bytes to follow.  */
5628 	byte |= 0x80;
5629       CHECKSUM (byte);
5630       if (value == 0)
5631 	break;
5632     }
5633 }
5634 
5635 /* Checksum the context of the DIE.  This adds the names of any
5636    surrounding namespaces or structures to the checksum.  */
5637 
5638 static void
checksum_die_context(dw_die_ref die,struct md5_ctx * ctx)5639 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5640 {
5641   const char *name;
5642   dw_die_ref spec;
5643   int tag = die->die_tag;
5644 
5645   if (tag != DW_TAG_namespace
5646       && tag != DW_TAG_structure_type
5647       && tag != DW_TAG_class_type)
5648     return;
5649 
5650   name = get_AT_string (die, DW_AT_name);
5651 
5652   spec = get_AT_ref (die, DW_AT_specification);
5653   if (spec != NULL)
5654     die = spec;
5655 
5656   if (die->die_parent != NULL)
5657     checksum_die_context (die->die_parent, ctx);
5658 
5659   CHECKSUM_ULEB128 ('C');
5660   CHECKSUM_ULEB128 (tag);
5661   if (name != NULL)
5662     CHECKSUM_STRING (name);
5663 }
5664 
5665 /* Calculate the checksum of a location expression.  */
5666 
5667 static inline void
loc_checksum_ordered(dw_loc_descr_ref loc,struct md5_ctx * ctx)5668 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5669 {
5670   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5671      were emitted as a DW_FORM_sdata instead of a location expression.  */
5672   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5673     {
5674       CHECKSUM_ULEB128 (DW_FORM_sdata);
5675       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5676       return;
5677     }
5678 
5679   /* Otherwise, just checksum the raw location expression.  */
5680   while (loc != NULL)
5681     {
5682       hashval_t hash = 0;
5683 
5684       CHECKSUM_ULEB128 (loc->dtprel);
5685       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5686       hash = hash_loc_operands (loc, hash);
5687       CHECKSUM (hash);
5688       loc = loc->dw_loc_next;
5689     }
5690 }
5691 
5692 /* Calculate the checksum of an attribute.  */
5693 
5694 static void
attr_checksum_ordered(enum dwarf_tag tag,dw_attr_ref at,struct md5_ctx * ctx,int * mark)5695 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5696 		       struct md5_ctx *ctx, int *mark)
5697 {
5698   dw_loc_descr_ref loc;
5699   rtx r;
5700 
5701   if (AT_class (at) == dw_val_class_die_ref)
5702     {
5703       dw_die_ref target_die = AT_ref (at);
5704 
5705       /* For pointer and reference types, we checksum only the (qualified)
5706 	 name of the target type (if there is a name).  For friend entries,
5707 	 we checksum only the (qualified) name of the target type or function.
5708 	 This allows the checksum to remain the same whether the target type
5709 	 is complete or not.  */
5710       if ((at->dw_attr == DW_AT_type
5711 	   && (tag == DW_TAG_pointer_type
5712 	       || tag == DW_TAG_reference_type
5713 	       || tag == DW_TAG_rvalue_reference_type
5714 	       || tag == DW_TAG_ptr_to_member_type))
5715 	  || (at->dw_attr == DW_AT_friend
5716 	      && tag == DW_TAG_friend))
5717 	{
5718 	  dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5719 
5720 	  if (name_attr != NULL)
5721 	    {
5722 	      dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5723 
5724 	      if (decl == NULL)
5725 		decl = target_die;
5726 	      CHECKSUM_ULEB128 ('N');
5727 	      CHECKSUM_ULEB128 (at->dw_attr);
5728 	      if (decl->die_parent != NULL)
5729 		checksum_die_context (decl->die_parent, ctx);
5730 	      CHECKSUM_ULEB128 ('E');
5731 	      CHECKSUM_STRING (AT_string (name_attr));
5732 	      return;
5733 	    }
5734 	}
5735 
5736       /* For all other references to another DIE, we check to see if the
5737          target DIE has already been visited.  If it has, we emit a
5738          backward reference; if not, we descend recursively.  */
5739       if (target_die->die_mark > 0)
5740         {
5741 	  CHECKSUM_ULEB128 ('R');
5742 	  CHECKSUM_ULEB128 (at->dw_attr);
5743 	  CHECKSUM_ULEB128 (target_die->die_mark);
5744         }
5745       else
5746         {
5747 	  dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5748 
5749 	  if (decl == NULL)
5750 	    decl = target_die;
5751 	  target_die->die_mark = ++(*mark);
5752 	  CHECKSUM_ULEB128 ('T');
5753 	  CHECKSUM_ULEB128 (at->dw_attr);
5754 	  if (decl->die_parent != NULL)
5755 	    checksum_die_context (decl->die_parent, ctx);
5756 	  die_checksum_ordered (target_die, ctx, mark);
5757         }
5758       return;
5759     }
5760 
5761   CHECKSUM_ULEB128 ('A');
5762   CHECKSUM_ULEB128 (at->dw_attr);
5763 
5764   switch (AT_class (at))
5765     {
5766     case dw_val_class_const:
5767       CHECKSUM_ULEB128 (DW_FORM_sdata);
5768       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5769       break;
5770 
5771     case dw_val_class_unsigned_const:
5772       CHECKSUM_ULEB128 (DW_FORM_sdata);
5773       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5774       break;
5775 
5776     case dw_val_class_const_double:
5777       CHECKSUM_ULEB128 (DW_FORM_block);
5778       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5779       CHECKSUM (at->dw_attr_val.v.val_double);
5780       break;
5781 
5782     case dw_val_class_vec:
5783       CHECKSUM_ULEB128 (DW_FORM_block);
5784       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5785 			* at->dw_attr_val.v.val_vec.elt_size);
5786       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5787 		      (at->dw_attr_val.v.val_vec.length
5788 		       * at->dw_attr_val.v.val_vec.elt_size));
5789       break;
5790 
5791     case dw_val_class_flag:
5792       CHECKSUM_ULEB128 (DW_FORM_flag);
5793       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5794       break;
5795 
5796     case dw_val_class_str:
5797       CHECKSUM_ULEB128 (DW_FORM_string);
5798       CHECKSUM_STRING (AT_string (at));
5799       break;
5800 
5801     case dw_val_class_addr:
5802       r = AT_addr (at);
5803       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5804       CHECKSUM_ULEB128 (DW_FORM_string);
5805       CHECKSUM_STRING (XSTR (r, 0));
5806       break;
5807 
5808     case dw_val_class_offset:
5809       CHECKSUM_ULEB128 (DW_FORM_sdata);
5810       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5811       break;
5812 
5813     case dw_val_class_loc:
5814       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5815 	loc_checksum_ordered (loc, ctx);
5816       break;
5817 
5818     case dw_val_class_fde_ref:
5819     case dw_val_class_lbl_id:
5820     case dw_val_class_lineptr:
5821     case dw_val_class_macptr:
5822     case dw_val_class_high_pc:
5823       break;
5824 
5825     case dw_val_class_file:
5826       CHECKSUM_ULEB128 (DW_FORM_string);
5827       CHECKSUM_STRING (AT_file (at)->filename);
5828       break;
5829 
5830     case dw_val_class_data8:
5831       CHECKSUM (at->dw_attr_val.v.val_data8);
5832       break;
5833 
5834     default:
5835       break;
5836     }
5837 }
5838 
5839 struct checksum_attributes
5840 {
5841   dw_attr_ref at_name;
5842   dw_attr_ref at_type;
5843   dw_attr_ref at_friend;
5844   dw_attr_ref at_accessibility;
5845   dw_attr_ref at_address_class;
5846   dw_attr_ref at_allocated;
5847   dw_attr_ref at_artificial;
5848   dw_attr_ref at_associated;
5849   dw_attr_ref at_binary_scale;
5850   dw_attr_ref at_bit_offset;
5851   dw_attr_ref at_bit_size;
5852   dw_attr_ref at_bit_stride;
5853   dw_attr_ref at_byte_size;
5854   dw_attr_ref at_byte_stride;
5855   dw_attr_ref at_const_value;
5856   dw_attr_ref at_containing_type;
5857   dw_attr_ref at_count;
5858   dw_attr_ref at_data_location;
5859   dw_attr_ref at_data_member_location;
5860   dw_attr_ref at_decimal_scale;
5861   dw_attr_ref at_decimal_sign;
5862   dw_attr_ref at_default_value;
5863   dw_attr_ref at_digit_count;
5864   dw_attr_ref at_discr;
5865   dw_attr_ref at_discr_list;
5866   dw_attr_ref at_discr_value;
5867   dw_attr_ref at_encoding;
5868   dw_attr_ref at_endianity;
5869   dw_attr_ref at_explicit;
5870   dw_attr_ref at_is_optional;
5871   dw_attr_ref at_location;
5872   dw_attr_ref at_lower_bound;
5873   dw_attr_ref at_mutable;
5874   dw_attr_ref at_ordering;
5875   dw_attr_ref at_picture_string;
5876   dw_attr_ref at_prototyped;
5877   dw_attr_ref at_small;
5878   dw_attr_ref at_segment;
5879   dw_attr_ref at_string_length;
5880   dw_attr_ref at_threads_scaled;
5881   dw_attr_ref at_upper_bound;
5882   dw_attr_ref at_use_location;
5883   dw_attr_ref at_use_UTF8;
5884   dw_attr_ref at_variable_parameter;
5885   dw_attr_ref at_virtuality;
5886   dw_attr_ref at_visibility;
5887   dw_attr_ref at_vtable_elem_location;
5888 };
5889 
5890 /* Collect the attributes that we will want to use for the checksum.  */
5891 
5892 static void
collect_checksum_attributes(struct checksum_attributes * attrs,dw_die_ref die)5893 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5894 {
5895   dw_attr_ref a;
5896   unsigned ix;
5897 
5898   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5899     {
5900       switch (a->dw_attr)
5901         {
5902         case DW_AT_name:
5903           attrs->at_name = a;
5904           break;
5905         case DW_AT_type:
5906           attrs->at_type = a;
5907           break;
5908         case DW_AT_friend:
5909           attrs->at_friend = a;
5910           break;
5911         case DW_AT_accessibility:
5912           attrs->at_accessibility = a;
5913           break;
5914         case DW_AT_address_class:
5915           attrs->at_address_class = a;
5916           break;
5917         case DW_AT_allocated:
5918           attrs->at_allocated = a;
5919           break;
5920         case DW_AT_artificial:
5921           attrs->at_artificial = a;
5922           break;
5923         case DW_AT_associated:
5924           attrs->at_associated = a;
5925           break;
5926         case DW_AT_binary_scale:
5927           attrs->at_binary_scale = a;
5928           break;
5929         case DW_AT_bit_offset:
5930           attrs->at_bit_offset = a;
5931           break;
5932         case DW_AT_bit_size:
5933           attrs->at_bit_size = a;
5934           break;
5935         case DW_AT_bit_stride:
5936           attrs->at_bit_stride = a;
5937           break;
5938         case DW_AT_byte_size:
5939           attrs->at_byte_size = a;
5940           break;
5941         case DW_AT_byte_stride:
5942           attrs->at_byte_stride = a;
5943           break;
5944         case DW_AT_const_value:
5945           attrs->at_const_value = a;
5946           break;
5947         case DW_AT_containing_type:
5948           attrs->at_containing_type = a;
5949           break;
5950         case DW_AT_count:
5951           attrs->at_count = a;
5952           break;
5953         case DW_AT_data_location:
5954           attrs->at_data_location = a;
5955           break;
5956         case DW_AT_data_member_location:
5957           attrs->at_data_member_location = a;
5958           break;
5959         case DW_AT_decimal_scale:
5960           attrs->at_decimal_scale = a;
5961           break;
5962         case DW_AT_decimal_sign:
5963           attrs->at_decimal_sign = a;
5964           break;
5965         case DW_AT_default_value:
5966           attrs->at_default_value = a;
5967           break;
5968         case DW_AT_digit_count:
5969           attrs->at_digit_count = a;
5970           break;
5971         case DW_AT_discr:
5972           attrs->at_discr = a;
5973           break;
5974         case DW_AT_discr_list:
5975           attrs->at_discr_list = a;
5976           break;
5977         case DW_AT_discr_value:
5978           attrs->at_discr_value = a;
5979           break;
5980         case DW_AT_encoding:
5981           attrs->at_encoding = a;
5982           break;
5983         case DW_AT_endianity:
5984           attrs->at_endianity = a;
5985           break;
5986         case DW_AT_explicit:
5987           attrs->at_explicit = a;
5988           break;
5989         case DW_AT_is_optional:
5990           attrs->at_is_optional = a;
5991           break;
5992         case DW_AT_location:
5993           attrs->at_location = a;
5994           break;
5995         case DW_AT_lower_bound:
5996           attrs->at_lower_bound = a;
5997           break;
5998         case DW_AT_mutable:
5999           attrs->at_mutable = a;
6000           break;
6001         case DW_AT_ordering:
6002           attrs->at_ordering = a;
6003           break;
6004         case DW_AT_picture_string:
6005           attrs->at_picture_string = a;
6006           break;
6007         case DW_AT_prototyped:
6008           attrs->at_prototyped = a;
6009           break;
6010         case DW_AT_small:
6011           attrs->at_small = a;
6012           break;
6013         case DW_AT_segment:
6014           attrs->at_segment = a;
6015           break;
6016         case DW_AT_string_length:
6017           attrs->at_string_length = a;
6018           break;
6019         case DW_AT_threads_scaled:
6020           attrs->at_threads_scaled = a;
6021           break;
6022         case DW_AT_upper_bound:
6023           attrs->at_upper_bound = a;
6024           break;
6025         case DW_AT_use_location:
6026           attrs->at_use_location = a;
6027           break;
6028         case DW_AT_use_UTF8:
6029           attrs->at_use_UTF8 = a;
6030           break;
6031         case DW_AT_variable_parameter:
6032           attrs->at_variable_parameter = a;
6033           break;
6034         case DW_AT_virtuality:
6035           attrs->at_virtuality = a;
6036           break;
6037         case DW_AT_visibility:
6038           attrs->at_visibility = a;
6039           break;
6040         case DW_AT_vtable_elem_location:
6041           attrs->at_vtable_elem_location = a;
6042           break;
6043         default:
6044           break;
6045         }
6046     }
6047 }
6048 
6049 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6050 
6051 static void
die_checksum_ordered(dw_die_ref die,struct md5_ctx * ctx,int * mark)6052 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6053 {
6054   dw_die_ref c;
6055   dw_die_ref decl;
6056   struct checksum_attributes attrs;
6057 
6058   CHECKSUM_ULEB128 ('D');
6059   CHECKSUM_ULEB128 (die->die_tag);
6060 
6061   memset (&attrs, 0, sizeof (attrs));
6062 
6063   decl = get_AT_ref (die, DW_AT_specification);
6064   if (decl != NULL)
6065     collect_checksum_attributes (&attrs, decl);
6066   collect_checksum_attributes (&attrs, die);
6067 
6068   CHECKSUM_ATTR (attrs.at_name);
6069   CHECKSUM_ATTR (attrs.at_accessibility);
6070   CHECKSUM_ATTR (attrs.at_address_class);
6071   CHECKSUM_ATTR (attrs.at_allocated);
6072   CHECKSUM_ATTR (attrs.at_artificial);
6073   CHECKSUM_ATTR (attrs.at_associated);
6074   CHECKSUM_ATTR (attrs.at_binary_scale);
6075   CHECKSUM_ATTR (attrs.at_bit_offset);
6076   CHECKSUM_ATTR (attrs.at_bit_size);
6077   CHECKSUM_ATTR (attrs.at_bit_stride);
6078   CHECKSUM_ATTR (attrs.at_byte_size);
6079   CHECKSUM_ATTR (attrs.at_byte_stride);
6080   CHECKSUM_ATTR (attrs.at_const_value);
6081   CHECKSUM_ATTR (attrs.at_containing_type);
6082   CHECKSUM_ATTR (attrs.at_count);
6083   CHECKSUM_ATTR (attrs.at_data_location);
6084   CHECKSUM_ATTR (attrs.at_data_member_location);
6085   CHECKSUM_ATTR (attrs.at_decimal_scale);
6086   CHECKSUM_ATTR (attrs.at_decimal_sign);
6087   CHECKSUM_ATTR (attrs.at_default_value);
6088   CHECKSUM_ATTR (attrs.at_digit_count);
6089   CHECKSUM_ATTR (attrs.at_discr);
6090   CHECKSUM_ATTR (attrs.at_discr_list);
6091   CHECKSUM_ATTR (attrs.at_discr_value);
6092   CHECKSUM_ATTR (attrs.at_encoding);
6093   CHECKSUM_ATTR (attrs.at_endianity);
6094   CHECKSUM_ATTR (attrs.at_explicit);
6095   CHECKSUM_ATTR (attrs.at_is_optional);
6096   CHECKSUM_ATTR (attrs.at_location);
6097   CHECKSUM_ATTR (attrs.at_lower_bound);
6098   CHECKSUM_ATTR (attrs.at_mutable);
6099   CHECKSUM_ATTR (attrs.at_ordering);
6100   CHECKSUM_ATTR (attrs.at_picture_string);
6101   CHECKSUM_ATTR (attrs.at_prototyped);
6102   CHECKSUM_ATTR (attrs.at_small);
6103   CHECKSUM_ATTR (attrs.at_segment);
6104   CHECKSUM_ATTR (attrs.at_string_length);
6105   CHECKSUM_ATTR (attrs.at_threads_scaled);
6106   CHECKSUM_ATTR (attrs.at_upper_bound);
6107   CHECKSUM_ATTR (attrs.at_use_location);
6108   CHECKSUM_ATTR (attrs.at_use_UTF8);
6109   CHECKSUM_ATTR (attrs.at_variable_parameter);
6110   CHECKSUM_ATTR (attrs.at_virtuality);
6111   CHECKSUM_ATTR (attrs.at_visibility);
6112   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6113   CHECKSUM_ATTR (attrs.at_type);
6114   CHECKSUM_ATTR (attrs.at_friend);
6115 
6116   /* Checksum the child DIEs.  */
6117   c = die->die_child;
6118   if (c) do {
6119     dw_attr_ref name_attr;
6120 
6121     c = c->die_sib;
6122     name_attr = get_AT (c, DW_AT_name);
6123     if (is_template_instantiation (c))
6124       {
6125 	/* Ignore instantiations of member type and function templates.  */
6126       }
6127     else if (name_attr != NULL
6128 	     && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6129       {
6130 	/* Use a shallow checksum for named nested types and member
6131 	   functions.  */
6132         CHECKSUM_ULEB128 ('S');
6133         CHECKSUM_ULEB128 (c->die_tag);
6134         CHECKSUM_STRING (AT_string (name_attr));
6135       }
6136     else
6137       {
6138 	/* Use a deep checksum for other children.  */
6139         /* Mark this DIE so it gets processed when unmarking.  */
6140         if (c->die_mark == 0)
6141           c->die_mark = -1;
6142         die_checksum_ordered (c, ctx, mark);
6143       }
6144   } while (c != die->die_child);
6145 
6146   CHECKSUM_ULEB128 (0);
6147 }
6148 
6149 /* Add a type name and tag to a hash.  */
6150 static void
die_odr_checksum(int tag,const char * name,md5_ctx * ctx)6151 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6152 {
6153   CHECKSUM_ULEB128 (tag);
6154   CHECKSUM_STRING (name);
6155 }
6156 
6157 #undef CHECKSUM
6158 #undef CHECKSUM_STRING
6159 #undef CHECKSUM_ATTR
6160 #undef CHECKSUM_LEB128
6161 #undef CHECKSUM_ULEB128
6162 
6163 /* Generate the type signature for DIE.  This is computed by generating an
6164    MD5 checksum over the DIE's tag, its relevant attributes, and its
6165    children.  Attributes that are references to other DIEs are processed
6166    by recursion, using the MARK field to prevent infinite recursion.
6167    If the DIE is nested inside a namespace or another type, we also
6168    need to include that context in the signature.  The lower 64 bits
6169    of the resulting MD5 checksum comprise the signature.  */
6170 
6171 static void
generate_type_signature(dw_die_ref die,comdat_type_node * type_node)6172 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6173 {
6174   int mark;
6175   const char *name;
6176   unsigned char checksum[16];
6177   struct md5_ctx ctx;
6178   dw_die_ref decl;
6179   dw_die_ref parent;
6180 
6181   name = get_AT_string (die, DW_AT_name);
6182   decl = get_AT_ref (die, DW_AT_specification);
6183   parent = get_die_parent (die);
6184 
6185   /* First, compute a signature for just the type name (and its surrounding
6186      context, if any.  This is stored in the type unit DIE for link-time
6187      ODR (one-definition rule) checking.  */
6188 
6189   if (is_cxx () && name != NULL)
6190     {
6191       md5_init_ctx (&ctx);
6192 
6193       /* Checksum the names of surrounding namespaces and structures.  */
6194       if (parent != NULL)
6195         checksum_die_context (parent, &ctx);
6196 
6197       /* Checksum the current DIE. */
6198       die_odr_checksum (die->die_tag, name, &ctx);
6199       md5_finish_ctx (&ctx, checksum);
6200 
6201       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6202     }
6203 
6204   /* Next, compute the complete type signature.  */
6205 
6206   md5_init_ctx (&ctx);
6207   mark = 1;
6208   die->die_mark = mark;
6209 
6210   /* Checksum the names of surrounding namespaces and structures.  */
6211   if (parent != NULL)
6212     checksum_die_context (parent, &ctx);
6213 
6214   /* Checksum the DIE and its children.  */
6215   die_checksum_ordered (die, &ctx, &mark);
6216   unmark_all_dies (die);
6217   md5_finish_ctx (&ctx, checksum);
6218 
6219   /* Store the signature in the type node and link the type DIE and the
6220      type node together.  */
6221   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6222           DWARF_TYPE_SIGNATURE_SIZE);
6223   die->comdat_type_p = true;
6224   die->die_id.die_type_node = type_node;
6225   type_node->type_die = die;
6226 
6227   /* If the DIE is a specification, link its declaration to the type node
6228      as well.  */
6229   if (decl != NULL)
6230     {
6231       decl->comdat_type_p = true;
6232       decl->die_id.die_type_node = type_node;
6233     }
6234 }
6235 
6236 /* Do the location expressions look same?  */
6237 static inline int
same_loc_p(dw_loc_descr_ref loc1,dw_loc_descr_ref loc2,int * mark)6238 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6239 {
6240   return loc1->dw_loc_opc == loc2->dw_loc_opc
6241 	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6242 	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6243 }
6244 
6245 /* Do the values look the same?  */
6246 static int
same_dw_val_p(const dw_val_node * v1,const dw_val_node * v2,int * mark)6247 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6248 {
6249   dw_loc_descr_ref loc1, loc2;
6250   rtx r1, r2;
6251 
6252   if (v1->val_class != v2->val_class)
6253     return 0;
6254 
6255   switch (v1->val_class)
6256     {
6257     case dw_val_class_const:
6258       return v1->v.val_int == v2->v.val_int;
6259     case dw_val_class_unsigned_const:
6260       return v1->v.val_unsigned == v2->v.val_unsigned;
6261     case dw_val_class_const_double:
6262       return v1->v.val_double.high == v2->v.val_double.high
6263 	     && v1->v.val_double.low == v2->v.val_double.low;
6264     case dw_val_class_vec:
6265       if (v1->v.val_vec.length != v2->v.val_vec.length
6266 	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6267 	return 0;
6268       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6269 		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
6270 	return 0;
6271       return 1;
6272     case dw_val_class_flag:
6273       return v1->v.val_flag == v2->v.val_flag;
6274     case dw_val_class_str:
6275       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6276 
6277     case dw_val_class_addr:
6278       r1 = v1->v.val_addr;
6279       r2 = v2->v.val_addr;
6280       if (GET_CODE (r1) != GET_CODE (r2))
6281 	return 0;
6282       return !rtx_equal_p (r1, r2);
6283 
6284     case dw_val_class_offset:
6285       return v1->v.val_offset == v2->v.val_offset;
6286 
6287     case dw_val_class_loc:
6288       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6289 	   loc1 && loc2;
6290 	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6291 	if (!same_loc_p (loc1, loc2, mark))
6292 	  return 0;
6293       return !loc1 && !loc2;
6294 
6295     case dw_val_class_die_ref:
6296       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6297 
6298     case dw_val_class_fde_ref:
6299     case dw_val_class_vms_delta:
6300     case dw_val_class_lbl_id:
6301     case dw_val_class_lineptr:
6302     case dw_val_class_macptr:
6303     case dw_val_class_high_pc:
6304       return 1;
6305 
6306     case dw_val_class_file:
6307       return v1->v.val_file == v2->v.val_file;
6308 
6309     case dw_val_class_data8:
6310       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6311 
6312     default:
6313       return 1;
6314     }
6315 }
6316 
6317 /* Do the attributes look the same?  */
6318 
6319 static int
same_attr_p(dw_attr_ref at1,dw_attr_ref at2,int * mark)6320 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6321 {
6322   if (at1->dw_attr != at2->dw_attr)
6323     return 0;
6324 
6325   /* We don't care that this was compiled with a different compiler
6326      snapshot; if the output is the same, that's what matters. */
6327   if (at1->dw_attr == DW_AT_producer)
6328     return 1;
6329 
6330   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6331 }
6332 
6333 /* Do the dies look the same?  */
6334 
6335 static int
same_die_p(dw_die_ref die1,dw_die_ref die2,int * mark)6336 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6337 {
6338   dw_die_ref c1, c2;
6339   dw_attr_ref a1;
6340   unsigned ix;
6341 
6342   /* To avoid infinite recursion.  */
6343   if (die1->die_mark)
6344     return die1->die_mark == die2->die_mark;
6345   die1->die_mark = die2->die_mark = ++(*mark);
6346 
6347   if (die1->die_tag != die2->die_tag)
6348     return 0;
6349 
6350   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6351     return 0;
6352 
6353   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6354     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6355       return 0;
6356 
6357   c1 = die1->die_child;
6358   c2 = die2->die_child;
6359   if (! c1)
6360     {
6361       if (c2)
6362 	return 0;
6363     }
6364   else
6365     for (;;)
6366       {
6367 	if (!same_die_p (c1, c2, mark))
6368 	  return 0;
6369 	c1 = c1->die_sib;
6370 	c2 = c2->die_sib;
6371 	if (c1 == die1->die_child)
6372 	  {
6373 	    if (c2 == die2->die_child)
6374 	      break;
6375 	    else
6376 	      return 0;
6377 	  }
6378     }
6379 
6380   return 1;
6381 }
6382 
6383 /* Do the dies look the same?  Wrapper around same_die_p.  */
6384 
6385 static int
same_die_p_wrap(dw_die_ref die1,dw_die_ref die2)6386 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6387 {
6388   int mark = 0;
6389   int ret = same_die_p (die1, die2, &mark);
6390 
6391   unmark_all_dies (die1);
6392   unmark_all_dies (die2);
6393 
6394   return ret;
6395 }
6396 
6397 /* The prefix to attach to symbols on DIEs in the current comdat debug
6398    info section.  */
6399 static const char *comdat_symbol_id;
6400 
6401 /* The index of the current symbol within the current comdat CU.  */
6402 static unsigned int comdat_symbol_number;
6403 
6404 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6405    children, and set comdat_symbol_id accordingly.  */
6406 
6407 static void
compute_section_prefix(dw_die_ref unit_die)6408 compute_section_prefix (dw_die_ref unit_die)
6409 {
6410   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6411   const char *base = die_name ? lbasename (die_name) : "anonymous";
6412   char *name = XALLOCAVEC (char, strlen (base) + 64);
6413   char *p;
6414   int i, mark;
6415   unsigned char checksum[16];
6416   struct md5_ctx ctx;
6417 
6418   /* Compute the checksum of the DIE, then append part of it as hex digits to
6419      the name filename of the unit.  */
6420 
6421   md5_init_ctx (&ctx);
6422   mark = 0;
6423   die_checksum (unit_die, &ctx, &mark);
6424   unmark_all_dies (unit_die);
6425   md5_finish_ctx (&ctx, checksum);
6426 
6427   sprintf (name, "%s.", base);
6428   clean_symbol_name (name);
6429 
6430   p = name + strlen (name);
6431   for (i = 0; i < 4; i++)
6432     {
6433       sprintf (p, "%.2x", checksum[i]);
6434       p += 2;
6435     }
6436 
6437   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6438   comdat_symbol_number = 0;
6439 }
6440 
6441 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6442 
6443 static int
is_type_die(dw_die_ref die)6444 is_type_die (dw_die_ref die)
6445 {
6446   switch (die->die_tag)
6447     {
6448     case DW_TAG_array_type:
6449     case DW_TAG_class_type:
6450     case DW_TAG_interface_type:
6451     case DW_TAG_enumeration_type:
6452     case DW_TAG_pointer_type:
6453     case DW_TAG_reference_type:
6454     case DW_TAG_rvalue_reference_type:
6455     case DW_TAG_string_type:
6456     case DW_TAG_structure_type:
6457     case DW_TAG_subroutine_type:
6458     case DW_TAG_union_type:
6459     case DW_TAG_ptr_to_member_type:
6460     case DW_TAG_set_type:
6461     case DW_TAG_subrange_type:
6462     case DW_TAG_base_type:
6463     case DW_TAG_const_type:
6464     case DW_TAG_file_type:
6465     case DW_TAG_packed_type:
6466     case DW_TAG_volatile_type:
6467     case DW_TAG_typedef:
6468       return 1;
6469     default:
6470       return 0;
6471     }
6472 }
6473 
6474 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6475    Basically, we want to choose the bits that are likely to be shared between
6476    compilations (types) and leave out the bits that are specific to individual
6477    compilations (functions).  */
6478 
6479 static int
is_comdat_die(dw_die_ref c)6480 is_comdat_die (dw_die_ref c)
6481 {
6482   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6483      we do for stabs.  The advantage is a greater likelihood of sharing between
6484      objects that don't include headers in the same order (and therefore would
6485      put the base types in a different comdat).  jason 8/28/00 */
6486 
6487   if (c->die_tag == DW_TAG_base_type)
6488     return 0;
6489 
6490   if (c->die_tag == DW_TAG_pointer_type
6491       || c->die_tag == DW_TAG_reference_type
6492       || c->die_tag == DW_TAG_rvalue_reference_type
6493       || c->die_tag == DW_TAG_const_type
6494       || c->die_tag == DW_TAG_volatile_type)
6495     {
6496       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6497 
6498       return t ? is_comdat_die (t) : 0;
6499     }
6500 
6501   return is_type_die (c);
6502 }
6503 
6504 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6505    compilation unit.  */
6506 
6507 static int
is_symbol_die(dw_die_ref c)6508 is_symbol_die (dw_die_ref c)
6509 {
6510   return (is_type_die (c)
6511 	  || is_declaration_die (c)
6512 	  || c->die_tag == DW_TAG_namespace
6513 	  || c->die_tag == DW_TAG_module);
6514 }
6515 
6516 /* Returns true iff C is a compile-unit DIE.  */
6517 
6518 static inline bool
is_cu_die(dw_die_ref c)6519 is_cu_die (dw_die_ref c)
6520 {
6521   return c && c->die_tag == DW_TAG_compile_unit;
6522 }
6523 
6524 /* Returns true iff C is a unit DIE of some sort.  */
6525 
6526 static inline bool
is_unit_die(dw_die_ref c)6527 is_unit_die (dw_die_ref c)
6528 {
6529   return c && (c->die_tag == DW_TAG_compile_unit
6530 	       || c->die_tag == DW_TAG_partial_unit
6531 	       || c->die_tag == DW_TAG_type_unit);
6532 }
6533 
6534 /* Returns true iff C is a namespace DIE.  */
6535 
6536 static inline bool
is_namespace_die(dw_die_ref c)6537 is_namespace_die (dw_die_ref c)
6538 {
6539   return c && c->die_tag == DW_TAG_namespace;
6540 }
6541 
6542 /* Returns true iff C is a class or structure DIE.  */
6543 
6544 static inline bool
is_class_die(dw_die_ref c)6545 is_class_die (dw_die_ref c)
6546 {
6547   return c && (c->die_tag == DW_TAG_class_type
6548                || c->die_tag == DW_TAG_structure_type);
6549 }
6550 
6551 /* Return non-zero if this DIE is a template parameter.  */
6552 
6553 static inline bool
is_template_parameter(dw_die_ref die)6554 is_template_parameter (dw_die_ref die)
6555 {
6556   switch (die->die_tag)
6557     {
6558     case DW_TAG_template_type_param:
6559     case DW_TAG_template_value_param:
6560     case DW_TAG_GNU_template_template_param:
6561     case DW_TAG_GNU_template_parameter_pack:
6562       return true;
6563     default:
6564       return false;
6565     }
6566 }
6567 
6568 /* Return non-zero if this DIE represents a template instantiation.  */
6569 
6570 static inline bool
is_template_instantiation(dw_die_ref die)6571 is_template_instantiation (dw_die_ref die)
6572 {
6573   dw_die_ref c;
6574 
6575   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6576     return false;
6577   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6578   return false;
6579 }
6580 
6581 static char *
gen_internal_sym(const char * prefix)6582 gen_internal_sym (const char *prefix)
6583 {
6584   char buf[256];
6585 
6586   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6587   return xstrdup (buf);
6588 }
6589 
6590 /* Assign symbols to all worthy DIEs under DIE.  */
6591 
6592 static void
assign_symbol_names(dw_die_ref die)6593 assign_symbol_names (dw_die_ref die)
6594 {
6595   dw_die_ref c;
6596 
6597   if (is_symbol_die (die) && !die->comdat_type_p)
6598     {
6599       if (comdat_symbol_id)
6600 	{
6601 	  char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6602 
6603 	  sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6604 		   comdat_symbol_id, comdat_symbol_number++);
6605 	  die->die_id.die_symbol = xstrdup (p);
6606 	}
6607       else
6608 	die->die_id.die_symbol = gen_internal_sym ("LDIE");
6609     }
6610 
6611   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6612 }
6613 
6614 struct cu_hash_table_entry
6615 {
6616   dw_die_ref cu;
6617   unsigned min_comdat_num, max_comdat_num;
6618   struct cu_hash_table_entry *next;
6619 };
6620 
6621 /* Helpers to manipulate hash table of CUs.  */
6622 
6623 struct cu_hash_table_entry_hasher
6624 {
6625   typedef cu_hash_table_entry value_type;
6626   typedef die_struct compare_type;
6627   static inline hashval_t hash (const value_type *);
6628   static inline bool equal (const value_type *, const compare_type *);
6629   static inline void remove (value_type *);
6630 };
6631 
6632 inline hashval_t
hash(const value_type * entry)6633 cu_hash_table_entry_hasher::hash (const value_type *entry)
6634 {
6635   return htab_hash_string (entry->cu->die_id.die_symbol);
6636 }
6637 
6638 inline bool
equal(const value_type * entry1,const compare_type * entry2)6639 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6640 				   const compare_type *entry2)
6641 {
6642   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6643 }
6644 
6645 inline void
remove(value_type * entry)6646 cu_hash_table_entry_hasher::remove (value_type *entry)
6647 {
6648   struct cu_hash_table_entry *next;
6649 
6650   while (entry)
6651     {
6652       next = entry->next;
6653       free (entry);
6654       entry = next;
6655     }
6656 }
6657 
6658 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6659 
6660 /* Check whether we have already seen this CU and set up SYM_NUM
6661    accordingly.  */
6662 static int
check_duplicate_cu(dw_die_ref cu,cu_hash_type htable,unsigned int * sym_num)6663 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6664 {
6665   struct cu_hash_table_entry dummy;
6666   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6667 
6668   dummy.max_comdat_num = 0;
6669 
6670   slot = htable.find_slot_with_hash (cu,
6671 				     htab_hash_string (cu->die_id.die_symbol),
6672 				     INSERT);
6673   entry = *slot;
6674 
6675   for (; entry; last = entry, entry = entry->next)
6676     {
6677       if (same_die_p_wrap (cu, entry->cu))
6678 	break;
6679     }
6680 
6681   if (entry)
6682     {
6683       *sym_num = entry->min_comdat_num;
6684       return 1;
6685     }
6686 
6687   entry = XCNEW (struct cu_hash_table_entry);
6688   entry->cu = cu;
6689   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6690   entry->next = *slot;
6691   *slot = entry;
6692 
6693   return 0;
6694 }
6695 
6696 /* Record SYM_NUM to record of CU in HTABLE.  */
6697 static void
record_comdat_symbol_number(dw_die_ref cu,cu_hash_type htable,unsigned int sym_num)6698 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6699 			     unsigned int sym_num)
6700 {
6701   struct cu_hash_table_entry **slot, *entry;
6702 
6703   slot = htable.find_slot_with_hash (cu,
6704 				     htab_hash_string (cu->die_id.die_symbol),
6705 				     NO_INSERT);
6706   entry = *slot;
6707 
6708   entry->max_comdat_num = sym_num;
6709 }
6710 
6711 /* Traverse the DIE (which is always comp_unit_die), and set up
6712    additional compilation units for each of the include files we see
6713    bracketed by BINCL/EINCL.  */
6714 
6715 static void
break_out_includes(dw_die_ref die)6716 break_out_includes (dw_die_ref die)
6717 {
6718   dw_die_ref c;
6719   dw_die_ref unit = NULL;
6720   limbo_die_node *node, **pnode;
6721   cu_hash_type cu_hash_table;
6722 
6723   c = die->die_child;
6724   if (c) do {
6725     dw_die_ref prev = c;
6726     c = c->die_sib;
6727     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6728 	   || (unit && is_comdat_die (c)))
6729       {
6730 	dw_die_ref next = c->die_sib;
6731 
6732 	/* This DIE is for a secondary CU; remove it from the main one.  */
6733 	remove_child_with_prev (c, prev);
6734 
6735 	if (c->die_tag == DW_TAG_GNU_BINCL)
6736 	  unit = push_new_compile_unit (unit, c);
6737 	else if (c->die_tag == DW_TAG_GNU_EINCL)
6738 	  unit = pop_compile_unit (unit);
6739 	else
6740 	  add_child_die (unit, c);
6741 	c = next;
6742 	if (c == die->die_child)
6743 	  break;
6744       }
6745   } while (c != die->die_child);
6746 
6747 #if 0
6748   /* We can only use this in debugging, since the frontend doesn't check
6749      to make sure that we leave every include file we enter.  */
6750   gcc_assert (!unit);
6751 #endif
6752 
6753   assign_symbol_names (die);
6754   cu_hash_table.create (10);
6755   for (node = limbo_die_list, pnode = &limbo_die_list;
6756        node;
6757        node = node->next)
6758     {
6759       int is_dupl;
6760 
6761       compute_section_prefix (node->die);
6762       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6763 			&comdat_symbol_number);
6764       assign_symbol_names (node->die);
6765       if (is_dupl)
6766 	*pnode = node->next;
6767       else
6768 	{
6769 	  pnode = &node->next;
6770 	  record_comdat_symbol_number (node->die, cu_hash_table,
6771 		comdat_symbol_number);
6772 	}
6773     }
6774   cu_hash_table.dispose ();
6775 }
6776 
6777 /* Return non-zero if this DIE is a declaration.  */
6778 
6779 static int
is_declaration_die(dw_die_ref die)6780 is_declaration_die (dw_die_ref die)
6781 {
6782   dw_attr_ref a;
6783   unsigned ix;
6784 
6785   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6786     if (a->dw_attr == DW_AT_declaration)
6787       return 1;
6788 
6789   return 0;
6790 }
6791 
6792 /* Return non-zero if this DIE is nested inside a subprogram.  */
6793 
6794 static int
is_nested_in_subprogram(dw_die_ref die)6795 is_nested_in_subprogram (dw_die_ref die)
6796 {
6797   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6798 
6799   if (decl == NULL)
6800     decl = die;
6801   return local_scope_p (decl);
6802 }
6803 
6804 /* Return non-zero if this DIE contains a defining declaration of a
6805    subprogram.  */
6806 
6807 static int
contains_subprogram_definition(dw_die_ref die)6808 contains_subprogram_definition (dw_die_ref die)
6809 {
6810   dw_die_ref c;
6811 
6812   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6813     return 1;
6814   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6815   return 0;
6816 }
6817 
6818 /* Return non-zero if this is a type DIE that should be moved to a
6819    COMDAT .debug_types section.  */
6820 
6821 static int
should_move_die_to_comdat(dw_die_ref die)6822 should_move_die_to_comdat (dw_die_ref die)
6823 {
6824   switch (die->die_tag)
6825     {
6826     case DW_TAG_class_type:
6827     case DW_TAG_structure_type:
6828     case DW_TAG_enumeration_type:
6829     case DW_TAG_union_type:
6830       /* Don't move declarations, inlined instances, or types nested in a
6831 	 subprogram.  */
6832       if (is_declaration_die (die)
6833           || get_AT (die, DW_AT_abstract_origin)
6834           || is_nested_in_subprogram (die))
6835         return 0;
6836       /* A type definition should never contain a subprogram definition.  */
6837       gcc_assert (!contains_subprogram_definition (die));
6838       return 1;
6839     case DW_TAG_array_type:
6840     case DW_TAG_interface_type:
6841     case DW_TAG_pointer_type:
6842     case DW_TAG_reference_type:
6843     case DW_TAG_rvalue_reference_type:
6844     case DW_TAG_string_type:
6845     case DW_TAG_subroutine_type:
6846     case DW_TAG_ptr_to_member_type:
6847     case DW_TAG_set_type:
6848     case DW_TAG_subrange_type:
6849     case DW_TAG_base_type:
6850     case DW_TAG_const_type:
6851     case DW_TAG_file_type:
6852     case DW_TAG_packed_type:
6853     case DW_TAG_volatile_type:
6854     case DW_TAG_typedef:
6855     default:
6856       return 0;
6857     }
6858 }
6859 
6860 /* Make a clone of DIE.  */
6861 
6862 static dw_die_ref
clone_die(dw_die_ref die)6863 clone_die (dw_die_ref die)
6864 {
6865   dw_die_ref clone;
6866   dw_attr_ref a;
6867   unsigned ix;
6868 
6869   clone = ggc_alloc_cleared_die_node ();
6870   clone->die_tag = die->die_tag;
6871 
6872   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6873     add_dwarf_attr (clone, a);
6874 
6875   return clone;
6876 }
6877 
6878 /* Make a clone of the tree rooted at DIE.  */
6879 
6880 static dw_die_ref
clone_tree(dw_die_ref die)6881 clone_tree (dw_die_ref die)
6882 {
6883   dw_die_ref c;
6884   dw_die_ref clone = clone_die (die);
6885 
6886   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6887 
6888   return clone;
6889 }
6890 
6891 /* Make a clone of DIE as a declaration.  */
6892 
6893 static dw_die_ref
clone_as_declaration(dw_die_ref die)6894 clone_as_declaration (dw_die_ref die)
6895 {
6896   dw_die_ref clone;
6897   dw_die_ref decl;
6898   dw_attr_ref a;
6899   unsigned ix;
6900 
6901   /* If the DIE is already a declaration, just clone it.  */
6902   if (is_declaration_die (die))
6903     return clone_die (die);
6904 
6905   /* If the DIE is a specification, just clone its declaration DIE.  */
6906   decl = get_AT_ref (die, DW_AT_specification);
6907   if (decl != NULL)
6908     {
6909       clone = clone_die (decl);
6910       if (die->comdat_type_p)
6911 	add_AT_die_ref (clone, DW_AT_signature, die);
6912       return clone;
6913     }
6914 
6915   clone = ggc_alloc_cleared_die_node ();
6916   clone->die_tag = die->die_tag;
6917 
6918   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6919     {
6920       /* We don't want to copy over all attributes.
6921          For example we don't want DW_AT_byte_size because otherwise we will no
6922          longer have a declaration and GDB will treat it as a definition.  */
6923 
6924       switch (a->dw_attr)
6925         {
6926         case DW_AT_artificial:
6927         case DW_AT_containing_type:
6928         case DW_AT_external:
6929         case DW_AT_name:
6930         case DW_AT_type:
6931         case DW_AT_virtuality:
6932         case DW_AT_linkage_name:
6933         case DW_AT_MIPS_linkage_name:
6934           add_dwarf_attr (clone, a);
6935           break;
6936         case DW_AT_byte_size:
6937         default:
6938           break;
6939         }
6940     }
6941 
6942   if (die->comdat_type_p)
6943     add_AT_die_ref (clone, DW_AT_signature, die);
6944 
6945   add_AT_flag (clone, DW_AT_declaration, 1);
6946   return clone;
6947 }
6948 
6949 
6950 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
6951 
6952 struct decl_table_entry
6953 {
6954   dw_die_ref orig;
6955   dw_die_ref copy;
6956 };
6957 
6958 /* Helpers to manipulate hash table of copied declarations.  */
6959 
6960 /* Hashtable helpers.  */
6961 
6962 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6963 {
6964   typedef decl_table_entry value_type;
6965   typedef die_struct compare_type;
6966   static inline hashval_t hash (const value_type *);
6967   static inline bool equal (const value_type *, const compare_type *);
6968 };
6969 
6970 inline hashval_t
hash(const value_type * entry)6971 decl_table_entry_hasher::hash (const value_type *entry)
6972 {
6973   return htab_hash_pointer (entry->orig);
6974 }
6975 
6976 inline bool
equal(const value_type * entry1,const compare_type * entry2)6977 decl_table_entry_hasher::equal (const value_type *entry1,
6978 				const compare_type *entry2)
6979 {
6980   return entry1->orig == entry2;
6981 }
6982 
6983 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6984 
6985 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6986    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
6987    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
6988    to check if the ancestor has already been copied into UNIT.  */
6989 
6990 static dw_die_ref
copy_ancestor_tree(dw_die_ref unit,dw_die_ref die,decl_hash_type decl_table)6991 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6992 {
6993   dw_die_ref parent = die->die_parent;
6994   dw_die_ref new_parent = unit;
6995   dw_die_ref copy;
6996   decl_table_entry **slot = NULL;
6997   struct decl_table_entry *entry = NULL;
6998 
6999   if (decl_table.is_created ())
7000     {
7001       /* Check if the entry has already been copied to UNIT.  */
7002       slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7003 					     INSERT);
7004       if (*slot != HTAB_EMPTY_ENTRY)
7005         {
7006           entry = *slot;
7007           return entry->copy;
7008         }
7009 
7010       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7011       entry = XCNEW (struct decl_table_entry);
7012       entry->orig = die;
7013       entry->copy = NULL;
7014       *slot = entry;
7015     }
7016 
7017   if (parent != NULL)
7018     {
7019       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7020       if (spec != NULL)
7021         parent = spec;
7022       if (!is_unit_die (parent))
7023         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7024     }
7025 
7026   copy = clone_as_declaration (die);
7027   add_child_die (new_parent, copy);
7028 
7029   if (decl_table.is_created ())
7030     {
7031       /* Record the pointer to the copy.  */
7032       entry->copy = copy;
7033     }
7034 
7035   return copy;
7036 }
7037 /* Copy the declaration context to the new type unit DIE.  This includes
7038    any surrounding namespace or type declarations.  If the DIE has an
7039    AT_specification attribute, it also includes attributes and children
7040    attached to the specification, and returns a pointer to the original
7041    parent of the declaration DIE.  Returns NULL otherwise.  */
7042 
7043 static dw_die_ref
copy_declaration_context(dw_die_ref unit,dw_die_ref die)7044 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7045 {
7046   dw_die_ref decl;
7047   dw_die_ref new_decl;
7048   dw_die_ref orig_parent = NULL;
7049 
7050   decl = get_AT_ref (die, DW_AT_specification);
7051   if (decl == NULL)
7052     decl = die;
7053   else
7054     {
7055       unsigned ix;
7056       dw_die_ref c;
7057       dw_attr_ref a;
7058 
7059       /* The original DIE will be changed to a declaration, and must
7060          be moved to be a child of the original declaration DIE.  */
7061       orig_parent = decl->die_parent;
7062 
7063       /* Copy the type node pointer from the new DIE to the original
7064          declaration DIE so we can forward references later.  */
7065       decl->comdat_type_p = true;
7066       decl->die_id.die_type_node = die->die_id.die_type_node;
7067 
7068       remove_AT (die, DW_AT_specification);
7069 
7070       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7071         {
7072           if (a->dw_attr != DW_AT_name
7073               && a->dw_attr != DW_AT_declaration
7074               && a->dw_attr != DW_AT_external)
7075             add_dwarf_attr (die, a);
7076         }
7077 
7078       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7079     }
7080 
7081   if (decl->die_parent != NULL
7082       && !is_unit_die (decl->die_parent))
7083     {
7084       new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7085       if (new_decl != NULL)
7086         {
7087           remove_AT (new_decl, DW_AT_signature);
7088           add_AT_specification (die, new_decl);
7089         }
7090     }
7091 
7092   return orig_parent;
7093 }
7094 
7095 /* Generate the skeleton ancestor tree for the given NODE, then clone
7096    the DIE and add the clone into the tree.  */
7097 
7098 static void
generate_skeleton_ancestor_tree(skeleton_chain_node * node)7099 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7100 {
7101   if (node->new_die != NULL)
7102     return;
7103 
7104   node->new_die = clone_as_declaration (node->old_die);
7105 
7106   if (node->parent != NULL)
7107     {
7108       generate_skeleton_ancestor_tree (node->parent);
7109       add_child_die (node->parent->new_die, node->new_die);
7110     }
7111 }
7112 
7113 /* Generate a skeleton tree of DIEs containing any declarations that are
7114    found in the original tree.  We traverse the tree looking for declaration
7115    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7116 
7117 static void
generate_skeleton_bottom_up(skeleton_chain_node * parent)7118 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7119 {
7120   skeleton_chain_node node;
7121   dw_die_ref c;
7122   dw_die_ref first;
7123   dw_die_ref prev = NULL;
7124   dw_die_ref next = NULL;
7125 
7126   node.parent = parent;
7127 
7128   first = c = parent->old_die->die_child;
7129   if (c)
7130     next = c->die_sib;
7131   if (c) do {
7132     if (prev == NULL || prev->die_sib == c)
7133       prev = c;
7134     c = next;
7135     next = (c == first ? NULL : c->die_sib);
7136     node.old_die = c;
7137     node.new_die = NULL;
7138     if (is_declaration_die (c))
7139       {
7140 	if (is_template_instantiation (c))
7141 	  {
7142 	    /* Instantiated templates do not need to be cloned into the
7143 	       type unit.  Just move the DIE and its children back to
7144 	       the skeleton tree (in the main CU).  */
7145 	    remove_child_with_prev (c, prev);
7146 	    add_child_die (parent->new_die, c);
7147 	    c = prev;
7148 	  }
7149 	else
7150 	  {
7151 	    /* Clone the existing DIE, move the original to the skeleton
7152 	       tree (which is in the main CU), and put the clone, with
7153 	       all the original's children, where the original came from
7154 	       (which is about to be moved to the type unit).  */
7155 	    dw_die_ref clone = clone_die (c);
7156 	    move_all_children (c, clone);
7157 
7158 	    replace_child (c, clone, prev);
7159 	    generate_skeleton_ancestor_tree (parent);
7160 	    add_child_die (parent->new_die, c);
7161 	    node.new_die = c;
7162 	    c = clone;
7163 	  }
7164       }
7165     generate_skeleton_bottom_up (&node);
7166   } while (next != NULL);
7167 }
7168 
7169 /* Wrapper function for generate_skeleton_bottom_up.  */
7170 
7171 static dw_die_ref
generate_skeleton(dw_die_ref die)7172 generate_skeleton (dw_die_ref die)
7173 {
7174   skeleton_chain_node node;
7175 
7176   node.old_die = die;
7177   node.new_die = NULL;
7178   node.parent = NULL;
7179 
7180   /* If this type definition is nested inside another type,
7181      and is not an instantiation of a template, always leave
7182      at least a declaration in its place.  */
7183   if (die->die_parent != NULL
7184       && is_type_die (die->die_parent)
7185       && !is_template_instantiation (die))
7186     node.new_die = clone_as_declaration (die);
7187 
7188   generate_skeleton_bottom_up (&node);
7189   return node.new_die;
7190 }
7191 
7192 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7193    declaration.  The original DIE is moved to a new compile unit so that
7194    existing references to it follow it to the new location.  If any of the
7195    original DIE's descendants is a declaration, we need to replace the
7196    original DIE with a skeleton tree and move the declarations back into the
7197    skeleton tree.  */
7198 
7199 static dw_die_ref
remove_child_or_replace_with_skeleton(dw_die_ref unit,dw_die_ref child,dw_die_ref prev)7200 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7201 				       dw_die_ref prev)
7202 {
7203   dw_die_ref skeleton, orig_parent;
7204 
7205   /* Copy the declaration context to the type unit DIE.  If the returned
7206      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7207      that DIE.  */
7208   orig_parent = copy_declaration_context (unit, child);
7209 
7210   skeleton = generate_skeleton (child);
7211   if (skeleton == NULL)
7212     remove_child_with_prev (child, prev);
7213   else
7214     {
7215       skeleton->comdat_type_p = true;
7216       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7217 
7218       /* If the original DIE was a specification, we need to put
7219          the skeleton under the parent DIE of the declaration.
7220 	 This leaves the original declaration in the tree, but
7221 	 it will be pruned later since there are no longer any
7222 	 references to it.  */
7223       if (orig_parent != NULL)
7224 	{
7225 	  remove_child_with_prev (child, prev);
7226 	  add_child_die (orig_parent, skeleton);
7227 	}
7228       else
7229 	replace_child (child, skeleton, prev);
7230     }
7231 
7232   return skeleton;
7233 }
7234 
7235 /* Traverse the DIE and set up additional .debug_types sections for each
7236    type worthy of being placed in a COMDAT section.  */
7237 
7238 static void
break_out_comdat_types(dw_die_ref die)7239 break_out_comdat_types (dw_die_ref die)
7240 {
7241   dw_die_ref c;
7242   dw_die_ref first;
7243   dw_die_ref prev = NULL;
7244   dw_die_ref next = NULL;
7245   dw_die_ref unit = NULL;
7246 
7247   first = c = die->die_child;
7248   if (c)
7249     next = c->die_sib;
7250   if (c) do {
7251     if (prev == NULL || prev->die_sib == c)
7252       prev = c;
7253     c = next;
7254     next = (c == first ? NULL : c->die_sib);
7255     if (should_move_die_to_comdat (c))
7256       {
7257         dw_die_ref replacement;
7258 	comdat_type_node_ref type_node;
7259 
7260         /* Break out nested types into their own type units.  */
7261         break_out_comdat_types (c);
7262 
7263         /* Create a new type unit DIE as the root for the new tree, and
7264            add it to the list of comdat types.  */
7265         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7266         add_AT_unsigned (unit, DW_AT_language,
7267                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7268         type_node = ggc_alloc_cleared_comdat_type_node ();
7269         type_node->root_die = unit;
7270         type_node->next = comdat_type_list;
7271         comdat_type_list = type_node;
7272 
7273         /* Generate the type signature.  */
7274         generate_type_signature (c, type_node);
7275 
7276         /* Copy the declaration context, attributes, and children of the
7277            declaration into the new type unit DIE, then remove this DIE
7278 	   from the main CU (or replace it with a skeleton if necessary).  */
7279 	replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7280 	type_node->skeleton_die = replacement;
7281 
7282         /* Add the DIE to the new compunit.  */
7283 	add_child_die (unit, c);
7284 
7285         if (replacement != NULL)
7286           c = replacement;
7287       }
7288     else if (c->die_tag == DW_TAG_namespace
7289              || c->die_tag == DW_TAG_class_type
7290              || c->die_tag == DW_TAG_structure_type
7291              || c->die_tag == DW_TAG_union_type)
7292       {
7293         /* Look for nested types that can be broken out.  */
7294         break_out_comdat_types (c);
7295       }
7296   } while (next != NULL);
7297 }
7298 
7299 /* Like clone_tree, but additionally enter all the children into
7300    the hash table decl_table.  */
7301 
7302 static dw_die_ref
clone_tree_hash(dw_die_ref die,decl_hash_type decl_table)7303 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7304 {
7305   dw_die_ref c;
7306   dw_die_ref clone = clone_die (die);
7307   struct decl_table_entry *entry;
7308   decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7309 					  htab_hash_pointer (die), INSERT);
7310   /* Assert that DIE isn't in the hash table yet.  If it would be there
7311      before, the ancestors would be necessarily there as well, therefore
7312      clone_tree_hash wouldn't be called.  */
7313   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7314   entry = XCNEW (struct decl_table_entry);
7315   entry->orig = die;
7316   entry->copy = clone;
7317   *slot = entry;
7318 
7319   FOR_EACH_CHILD (die, c,
7320 		  add_child_die (clone, clone_tree_hash (c, decl_table)));
7321 
7322   return clone;
7323 }
7324 
7325 /* Walk the DIE and its children, looking for references to incomplete
7326    or trivial types that are unmarked (i.e., that are not in the current
7327    type_unit).  */
7328 
7329 static void
copy_decls_walk(dw_die_ref unit,dw_die_ref die,decl_hash_type decl_table)7330 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7331 {
7332   dw_die_ref c;
7333   dw_attr_ref a;
7334   unsigned ix;
7335 
7336   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7337     {
7338       if (AT_class (a) == dw_val_class_die_ref)
7339         {
7340           dw_die_ref targ = AT_ref (a);
7341           decl_table_entry **slot;
7342           struct decl_table_entry *entry;
7343 
7344           if (targ->die_mark != 0 || targ->comdat_type_p)
7345             continue;
7346 
7347           slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7348 						 INSERT);
7349 
7350           if (*slot != HTAB_EMPTY_ENTRY)
7351             {
7352               /* TARG has already been copied, so we just need to
7353                  modify the reference to point to the copy.  */
7354               entry = *slot;
7355               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7356             }
7357           else
7358             {
7359               dw_die_ref parent = unit;
7360 	      dw_die_ref copy = clone_die (targ);
7361 
7362               /* Record in DECL_TABLE that TARG has been copied.
7363                  Need to do this now, before the recursive call,
7364                  because DECL_TABLE may be expanded and SLOT
7365                  would no longer be a valid pointer.  */
7366               entry = XCNEW (struct decl_table_entry);
7367               entry->orig = targ;
7368               entry->copy = copy;
7369               *slot = entry;
7370 
7371 	      FOR_EACH_CHILD (targ, c,
7372 			      add_child_die (copy,
7373 					     clone_tree_hash (c, decl_table)));
7374 
7375               /* Make sure the cloned tree is marked as part of the
7376                  type unit.  */
7377               mark_dies (copy);
7378 
7379               /* If TARG has surrounding context, copy its ancestor tree
7380                  into the new type unit.  */
7381               if (targ->die_parent != NULL
7382 		  && !is_unit_die (targ->die_parent))
7383                 parent = copy_ancestor_tree (unit, targ->die_parent,
7384                                              decl_table);
7385 
7386               add_child_die (parent, copy);
7387               a->dw_attr_val.v.val_die_ref.die = copy;
7388 
7389               /* Make sure the newly-copied DIE is walked.  If it was
7390                  installed in a previously-added context, it won't
7391                  get visited otherwise.  */
7392               if (parent != unit)
7393 		{
7394 		  /* Find the highest point of the newly-added tree,
7395 		     mark each node along the way, and walk from there.  */
7396 		  parent->die_mark = 1;
7397 		  while (parent->die_parent
7398 		  	 && parent->die_parent->die_mark == 0)
7399 		    {
7400 		      parent = parent->die_parent;
7401 		      parent->die_mark = 1;
7402 		    }
7403 		  copy_decls_walk (unit, parent, decl_table);
7404 		}
7405             }
7406         }
7407     }
7408 
7409   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7410 }
7411 
7412 /* Copy declarations for "unworthy" types into the new comdat section.
7413    Incomplete types, modified types, and certain other types aren't broken
7414    out into comdat sections of their own, so they don't have a signature,
7415    and we need to copy the declaration into the same section so that we
7416    don't have an external reference.  */
7417 
7418 static void
copy_decls_for_unworthy_types(dw_die_ref unit)7419 copy_decls_for_unworthy_types (dw_die_ref unit)
7420 {
7421   decl_hash_type decl_table;
7422 
7423   mark_dies (unit);
7424   decl_table.create (10);
7425   copy_decls_walk (unit, unit, decl_table);
7426   decl_table.dispose ();
7427   unmark_dies (unit);
7428 }
7429 
7430 /* Traverse the DIE and add a sibling attribute if it may have the
7431    effect of speeding up access to siblings.  To save some space,
7432    avoid generating sibling attributes for DIE's without children.  */
7433 
7434 static void
add_sibling_attributes(dw_die_ref die)7435 add_sibling_attributes (dw_die_ref die)
7436 {
7437   dw_die_ref c;
7438 
7439   if (! die->die_child)
7440     return;
7441 
7442   if (die->die_parent && die != die->die_parent->die_child)
7443     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7444 
7445   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7446 }
7447 
7448 /* Output all location lists for the DIE and its children.  */
7449 
7450 static void
output_location_lists(dw_die_ref die)7451 output_location_lists (dw_die_ref die)
7452 {
7453   dw_die_ref c;
7454   dw_attr_ref a;
7455   unsigned ix;
7456 
7457   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7458     if (AT_class (a) == dw_val_class_loc_list)
7459       output_loc_list (AT_loc_list (a));
7460 
7461   FOR_EACH_CHILD (die, c, output_location_lists (c));
7462 }
7463 
7464 /* We want to limit the number of external references, because they are
7465    larger than local references: a relocation takes multiple words, and
7466    even a sig8 reference is always eight bytes, whereas a local reference
7467    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7468    So if we encounter multiple external references to the same type DIE, we
7469    make a local typedef stub for it and redirect all references there.
7470 
7471    This is the element of the hash table for keeping track of these
7472    references.  */
7473 
7474 struct external_ref
7475 {
7476   dw_die_ref type;
7477   dw_die_ref stub;
7478   unsigned n_refs;
7479 };
7480 
7481 /* Hashtable helpers.  */
7482 
7483 struct external_ref_hasher : typed_free_remove <external_ref>
7484 {
7485   typedef external_ref value_type;
7486   typedef external_ref compare_type;
7487   static inline hashval_t hash (const value_type *);
7488   static inline bool equal (const value_type *, const compare_type *);
7489 };
7490 
7491 inline hashval_t
hash(const value_type * r)7492 external_ref_hasher::hash (const value_type *r)
7493 {
7494   dw_die_ref die = r->type;
7495   hashval_t h = 0;
7496 
7497   /* We can't use the address of the DIE for hashing, because
7498      that will make the order of the stub DIEs non-deterministic.  */
7499   if (! die->comdat_type_p)
7500     /* We have a symbol; use it to compute a hash.  */
7501     h = htab_hash_string (die->die_id.die_symbol);
7502   else
7503     {
7504       /* We have a type signature; use a subset of the bits as the hash.
7505 	 The 8-byte signature is at least as large as hashval_t.  */
7506       comdat_type_node_ref type_node = die->die_id.die_type_node;
7507       memcpy (&h, type_node->signature, sizeof (h));
7508     }
7509   return h;
7510 }
7511 
7512 inline bool
equal(const value_type * r1,const compare_type * r2)7513 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7514 {
7515   return r1->type == r2->type;
7516 }
7517 
7518 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7519 
7520 /* Return a pointer to the external_ref for references to DIE.  */
7521 
7522 static struct external_ref *
lookup_external_ref(external_ref_hash_type map,dw_die_ref die)7523 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7524 {
7525   struct external_ref ref, *ref_p;
7526   external_ref **slot;
7527 
7528   ref.type = die;
7529   slot = map.find_slot (&ref, INSERT);
7530   if (*slot != HTAB_EMPTY_ENTRY)
7531     return *slot;
7532 
7533   ref_p = XCNEW (struct external_ref);
7534   ref_p->type = die;
7535   *slot = ref_p;
7536   return ref_p;
7537 }
7538 
7539 /* Subroutine of optimize_external_refs, below.
7540 
7541    If we see a type skeleton, record it as our stub.  If we see external
7542    references, remember how many we've seen.  */
7543 
7544 static void
optimize_external_refs_1(dw_die_ref die,external_ref_hash_type map)7545 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7546 {
7547   dw_die_ref c;
7548   dw_attr_ref a;
7549   unsigned ix;
7550   struct external_ref *ref_p;
7551 
7552   if (is_type_die (die)
7553       && (c = get_AT_ref (die, DW_AT_signature)))
7554     {
7555       /* This is a local skeleton; use it for local references.  */
7556       ref_p = lookup_external_ref (map, c);
7557       ref_p->stub = die;
7558     }
7559 
7560   /* Scan the DIE references, and remember any that refer to DIEs from
7561      other CUs (i.e. those which are not marked).  */
7562   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7563     if (AT_class (a) == dw_val_class_die_ref
7564 	&& (c = AT_ref (a))->die_mark == 0
7565 	&& is_type_die (c))
7566       {
7567 	ref_p = lookup_external_ref (map, c);
7568 	ref_p->n_refs++;
7569       }
7570 
7571   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7572 }
7573 
7574 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7575    points to an external_ref, DATA is the CU we're processing.  If we don't
7576    already have a local stub, and we have multiple refs, build a stub.  */
7577 
7578 int
dwarf2_build_local_stub(external_ref ** slot,dw_die_ref data)7579 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7580 {
7581   struct external_ref *ref_p = *slot;
7582 
7583   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7584     {
7585       /* We have multiple references to this type, so build a small stub.
7586 	 Both of these forms are a bit dodgy from the perspective of the
7587 	 DWARF standard, since technically they should have names.  */
7588       dw_die_ref cu = data;
7589       dw_die_ref type = ref_p->type;
7590       dw_die_ref stub = NULL;
7591 
7592       if (type->comdat_type_p)
7593 	{
7594 	  /* If we refer to this type via sig8, use AT_signature.  */
7595 	  stub = new_die (type->die_tag, cu, NULL_TREE);
7596 	  add_AT_die_ref (stub, DW_AT_signature, type);
7597 	}
7598       else
7599 	{
7600 	  /* Otherwise, use a typedef with no name.  */
7601 	  stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7602 	  add_AT_die_ref (stub, DW_AT_type, type);
7603 	}
7604 
7605       stub->die_mark++;
7606       ref_p->stub = stub;
7607     }
7608   return 1;
7609 }
7610 
7611 /* DIE is a unit; look through all the DIE references to see if there are
7612    any external references to types, and if so, create local stubs for
7613    them which will be applied in build_abbrev_table.  This is useful because
7614    references to local DIEs are smaller.  */
7615 
7616 static external_ref_hash_type
optimize_external_refs(dw_die_ref die)7617 optimize_external_refs (dw_die_ref die)
7618 {
7619   external_ref_hash_type map;
7620   map.create (10);
7621   optimize_external_refs_1 (die, map);
7622   map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7623   return map;
7624 }
7625 
7626 /* The format of each DIE (and its attribute value pairs) is encoded in an
7627    abbreviation table.  This routine builds the abbreviation table and assigns
7628    a unique abbreviation id for each abbreviation entry.  The children of each
7629    die are visited recursively.  */
7630 
7631 static void
build_abbrev_table(dw_die_ref die,external_ref_hash_type extern_map)7632 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7633 {
7634   unsigned long abbrev_id;
7635   unsigned int n_alloc;
7636   dw_die_ref c;
7637   dw_attr_ref a;
7638   unsigned ix;
7639 
7640   /* Scan the DIE references, and replace any that refer to
7641      DIEs from other CUs (i.e. those which are not marked) with
7642      the local stubs we built in optimize_external_refs.  */
7643   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7644     if (AT_class (a) == dw_val_class_die_ref
7645 	&& (c = AT_ref (a))->die_mark == 0)
7646       {
7647 	struct external_ref *ref_p;
7648 	gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7649 
7650 	ref_p = lookup_external_ref (extern_map, c);
7651 	if (ref_p->stub && ref_p->stub != die)
7652 	  change_AT_die_ref (a, ref_p->stub);
7653 	else
7654 	  /* We aren't changing this reference, so mark it external.  */
7655 	  set_AT_ref_external (a, 1);
7656       }
7657 
7658   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7659     {
7660       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7661       dw_attr_ref die_a, abbrev_a;
7662       unsigned ix;
7663       bool ok = true;
7664 
7665       if (abbrev->die_tag != die->die_tag)
7666 	continue;
7667       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7668 	continue;
7669 
7670       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7671 	continue;
7672 
7673       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7674 	{
7675 	  abbrev_a = &(*abbrev->die_attr)[ix];
7676 	  if ((abbrev_a->dw_attr != die_a->dw_attr)
7677 	      || (value_format (abbrev_a) != value_format (die_a)))
7678 	    {
7679 	      ok = false;
7680 	      break;
7681 	    }
7682 	}
7683       if (ok)
7684 	break;
7685     }
7686 
7687   if (abbrev_id >= abbrev_die_table_in_use)
7688     {
7689       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7690 	{
7691 	  n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7692 	  abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7693 					    n_alloc);
7694 
7695 	  memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7696 		 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7697 	  abbrev_die_table_allocated = n_alloc;
7698 	}
7699 
7700       ++abbrev_die_table_in_use;
7701       abbrev_die_table[abbrev_id] = die;
7702     }
7703 
7704   die->die_abbrev = abbrev_id;
7705   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7706 }
7707 
7708 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7709 
7710 static int
constant_size(unsigned HOST_WIDE_INT value)7711 constant_size (unsigned HOST_WIDE_INT value)
7712 {
7713   int log;
7714 
7715   if (value == 0)
7716     log = 0;
7717   else
7718     log = floor_log2 (value);
7719 
7720   log = log / 8;
7721   log = 1 << (floor_log2 (log) + 1);
7722 
7723   return log;
7724 }
7725 
7726 /* Return the size of a DIE as it is represented in the
7727    .debug_info section.  */
7728 
7729 static unsigned long
size_of_die(dw_die_ref die)7730 size_of_die (dw_die_ref die)
7731 {
7732   unsigned long size = 0;
7733   dw_attr_ref a;
7734   unsigned ix;
7735   enum dwarf_form form;
7736 
7737   size += size_of_uleb128 (die->die_abbrev);
7738   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7739     {
7740       switch (AT_class (a))
7741 	{
7742 	case dw_val_class_addr:
7743           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7744             {
7745               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7746               size += size_of_uleb128 (AT_index (a));
7747             }
7748           else
7749             size += DWARF2_ADDR_SIZE;
7750 	  break;
7751 	case dw_val_class_offset:
7752 	  size += DWARF_OFFSET_SIZE;
7753 	  break;
7754 	case dw_val_class_loc:
7755 	  {
7756 	    unsigned long lsize = size_of_locs (AT_loc (a));
7757 
7758 	    /* Block length.  */
7759 	    if (dwarf_version >= 4)
7760 	      size += size_of_uleb128 (lsize);
7761 	    else
7762 	      size += constant_size (lsize);
7763 	    size += lsize;
7764 	  }
7765 	  break;
7766 	case dw_val_class_loc_list:
7767           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7768             {
7769               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7770               size += size_of_uleb128 (AT_index (a));
7771             }
7772           else
7773             size += DWARF_OFFSET_SIZE;
7774 	  break;
7775 	case dw_val_class_range_list:
7776           size += DWARF_OFFSET_SIZE;
7777 	  break;
7778 	case dw_val_class_const:
7779 	  size += size_of_sleb128 (AT_int (a));
7780 	  break;
7781 	case dw_val_class_unsigned_const:
7782 	  {
7783 	    int csize = constant_size (AT_unsigned (a));
7784 	    if (dwarf_version == 3
7785 		&& a->dw_attr == DW_AT_data_member_location
7786 		&& csize >= 4)
7787 	      size += size_of_uleb128 (AT_unsigned (a));
7788 	    else
7789 	      size += csize;
7790 	  }
7791 	  break;
7792 	case dw_val_class_const_double:
7793 	  size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7794 	  if (HOST_BITS_PER_WIDE_INT >= 64)
7795 	    size++; /* block */
7796 	  break;
7797 	case dw_val_class_vec:
7798 	  size += constant_size (a->dw_attr_val.v.val_vec.length
7799 				 * a->dw_attr_val.v.val_vec.elt_size)
7800 		  + a->dw_attr_val.v.val_vec.length
7801 		    * a->dw_attr_val.v.val_vec.elt_size; /* block */
7802 	  break;
7803 	case dw_val_class_flag:
7804 	  if (dwarf_version >= 4)
7805 	    /* Currently all add_AT_flag calls pass in 1 as last argument,
7806 	       so DW_FORM_flag_present can be used.  If that ever changes,
7807 	       we'll need to use DW_FORM_flag and have some optimization
7808 	       in build_abbrev_table that will change those to
7809 	       DW_FORM_flag_present if it is set to 1 in all DIEs using
7810 	       the same abbrev entry.  */
7811 	    gcc_assert (a->dw_attr_val.v.val_flag == 1);
7812 	  else
7813 	    size += 1;
7814 	  break;
7815 	case dw_val_class_die_ref:
7816 	  if (AT_ref_external (a))
7817 	    {
7818 	      /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7819 		 we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7820 		 is sized by target address length, whereas in DWARF3
7821 		 it's always sized as an offset.  */
7822 	      if (use_debug_types)
7823 		size += DWARF_TYPE_SIGNATURE_SIZE;
7824 	      else if (dwarf_version == 2)
7825 		size += DWARF2_ADDR_SIZE;
7826 	      else
7827 		size += DWARF_OFFSET_SIZE;
7828 	    }
7829 	  else
7830 	    size += DWARF_OFFSET_SIZE;
7831 	  break;
7832 	case dw_val_class_fde_ref:
7833 	  size += DWARF_OFFSET_SIZE;
7834 	  break;
7835 	case dw_val_class_lbl_id:
7836           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7837             {
7838               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7839               size += size_of_uleb128 (AT_index (a));
7840             }
7841           else
7842             size += DWARF2_ADDR_SIZE;
7843 	  break;
7844 	case dw_val_class_lineptr:
7845 	case dw_val_class_macptr:
7846           size += DWARF_OFFSET_SIZE;
7847 	  break;
7848 	case dw_val_class_str:
7849           form = AT_string_form (a);
7850           if (form == DW_FORM_strp)
7851 	    size += DWARF_OFFSET_SIZE;
7852          else if (form == DW_FORM_GNU_str_index)
7853             size += size_of_uleb128 (AT_index (a));
7854 	  else
7855 	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7856 	  break;
7857 	case dw_val_class_file:
7858 	  size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7859 	  break;
7860 	case dw_val_class_data8:
7861 	  size += 8;
7862 	  break;
7863 	case dw_val_class_vms_delta:
7864 	  size += DWARF_OFFSET_SIZE;
7865 	  break;
7866 	case dw_val_class_high_pc:
7867 	  size += DWARF2_ADDR_SIZE;
7868 	  break;
7869 	default:
7870 	  gcc_unreachable ();
7871 	}
7872     }
7873 
7874   return size;
7875 }
7876 
7877 /* Size the debugging information associated with a given DIE.  Visits the
7878    DIE's children recursively.  Updates the global variable next_die_offset, on
7879    each time through.  Uses the current value of next_die_offset to update the
7880    die_offset field in each DIE.  */
7881 
7882 static void
calc_die_sizes(dw_die_ref die)7883 calc_die_sizes (dw_die_ref die)
7884 {
7885   dw_die_ref c;
7886 
7887   gcc_assert (die->die_offset == 0
7888 	      || (unsigned long int) die->die_offset == next_die_offset);
7889   die->die_offset = next_die_offset;
7890   next_die_offset += size_of_die (die);
7891 
7892   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7893 
7894   if (die->die_child != NULL)
7895     /* Count the null byte used to terminate sibling lists.  */
7896     next_die_offset += 1;
7897 }
7898 
7899 /* Size just the base type children at the start of the CU.
7900    This is needed because build_abbrev needs to size locs
7901    and sizing of type based stack ops needs to know die_offset
7902    values for the base types.  */
7903 
7904 static void
calc_base_type_die_sizes(void)7905 calc_base_type_die_sizes (void)
7906 {
7907   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7908   unsigned int i;
7909   dw_die_ref base_type;
7910 #if ENABLE_ASSERT_CHECKING
7911   dw_die_ref prev = comp_unit_die ()->die_child;
7912 #endif
7913 
7914   die_offset += size_of_die (comp_unit_die ());
7915   for (i = 0; base_types.iterate (i, &base_type); i++)
7916     {
7917 #if ENABLE_ASSERT_CHECKING
7918       gcc_assert (base_type->die_offset == 0
7919 		  && prev->die_sib == base_type
7920 		  && base_type->die_child == NULL
7921 		  && base_type->die_abbrev);
7922       prev = base_type;
7923 #endif
7924       base_type->die_offset = die_offset;
7925       die_offset += size_of_die (base_type);
7926     }
7927 }
7928 
7929 /* Set the marks for a die and its children.  We do this so
7930    that we know whether or not a reference needs to use FORM_ref_addr; only
7931    DIEs in the same CU will be marked.  We used to clear out the offset
7932    and use that as the flag, but ran into ordering problems.  */
7933 
7934 static void
mark_dies(dw_die_ref die)7935 mark_dies (dw_die_ref die)
7936 {
7937   dw_die_ref c;
7938 
7939   gcc_assert (!die->die_mark);
7940 
7941   die->die_mark = 1;
7942   FOR_EACH_CHILD (die, c, mark_dies (c));
7943 }
7944 
7945 /* Clear the marks for a die and its children.  */
7946 
7947 static void
unmark_dies(dw_die_ref die)7948 unmark_dies (dw_die_ref die)
7949 {
7950   dw_die_ref c;
7951 
7952   if (! use_debug_types)
7953     gcc_assert (die->die_mark);
7954 
7955   die->die_mark = 0;
7956   FOR_EACH_CHILD (die, c, unmark_dies (c));
7957 }
7958 
7959 /* Clear the marks for a die, its children and referred dies.  */
7960 
7961 static void
unmark_all_dies(dw_die_ref die)7962 unmark_all_dies (dw_die_ref die)
7963 {
7964   dw_die_ref c;
7965   dw_attr_ref a;
7966   unsigned ix;
7967 
7968   if (!die->die_mark)
7969     return;
7970   die->die_mark = 0;
7971 
7972   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7973 
7974   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7975     if (AT_class (a) == dw_val_class_die_ref)
7976       unmark_all_dies (AT_ref (a));
7977 }
7978 
7979 /* Calculate if the entry should appear in the final output file.  It may be
7980    from a pruned a type.  */
7981 
7982 static bool
include_pubname_in_output(vec<pubname_entry,va_gc> * table,pubname_entry * p)7983 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7984 {
7985   /* By limiting gnu pubnames to definitions only, gold can generate a
7986      gdb index without entries for declarations, which don't include
7987      enough information to be useful.  */
7988   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7989     return false;
7990 
7991   if (table == pubname_table)
7992     {
7993       /* Enumerator names are part of the pubname table, but the
7994          parent DW_TAG_enumeration_type die may have been pruned.
7995          Don't output them if that is the case.  */
7996       if (p->die->die_tag == DW_TAG_enumerator &&
7997           (p->die->die_parent == NULL
7998            || !p->die->die_parent->die_perennial_p))
7999         return false;
8000 
8001       /* Everything else in the pubname table is included.  */
8002       return true;
8003     }
8004 
8005   /* The pubtypes table shouldn't include types that have been
8006      pruned.  */
8007   return (p->die->die_offset != 0
8008           || !flag_eliminate_unused_debug_types);
8009 }
8010 
8011 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8012    generated for the compilation unit.  */
8013 
8014 static unsigned long
size_of_pubnames(vec<pubname_entry,va_gc> * names)8015 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8016 {
8017   unsigned long size;
8018   unsigned i;
8019   pubname_ref p;
8020   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8021 
8022   size = DWARF_PUBNAMES_HEADER_SIZE;
8023   FOR_EACH_VEC_ELT (*names, i, p)
8024     if (include_pubname_in_output (names, p))
8025       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8026 
8027   size += DWARF_OFFSET_SIZE;
8028   return size;
8029 }
8030 
8031 /* Return the size of the information in the .debug_aranges section.  */
8032 
8033 static unsigned long
size_of_aranges(void)8034 size_of_aranges (void)
8035 {
8036   unsigned long size;
8037 
8038   size = DWARF_ARANGES_HEADER_SIZE;
8039 
8040   /* Count the address/length pair for this compilation unit.  */
8041   if (text_section_used)
8042     size += 2 * DWARF2_ADDR_SIZE;
8043   if (cold_text_section_used)
8044     size += 2 * DWARF2_ADDR_SIZE;
8045   if (have_multiple_function_sections)
8046     {
8047       unsigned fde_idx;
8048       dw_fde_ref fde;
8049 
8050       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8051 	{
8052 	  if (DECL_IGNORED_P (fde->decl))
8053 	    continue;
8054 	  if (!fde->in_std_section)
8055 	    size += 2 * DWARF2_ADDR_SIZE;
8056 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8057 	    size += 2 * DWARF2_ADDR_SIZE;
8058 	}
8059     }
8060 
8061   /* Count the two zero words used to terminated the address range table.  */
8062   size += 2 * DWARF2_ADDR_SIZE;
8063   return size;
8064 }
8065 
8066 /* Select the encoding of an attribute value.  */
8067 
8068 static enum dwarf_form
value_format(dw_attr_ref a)8069 value_format (dw_attr_ref a)
8070 {
8071   switch (AT_class (a))
8072     {
8073     case dw_val_class_addr:
8074       /* Only very few attributes allow DW_FORM_addr.  */
8075       switch (a->dw_attr)
8076 	{
8077 	case DW_AT_low_pc:
8078 	case DW_AT_high_pc:
8079 	case DW_AT_entry_pc:
8080 	case DW_AT_trampoline:
8081           return (AT_index (a) == NOT_INDEXED
8082                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8083 	default:
8084 	  break;
8085 	}
8086       switch (DWARF2_ADDR_SIZE)
8087 	{
8088 	case 1:
8089 	  return DW_FORM_data1;
8090 	case 2:
8091 	  return DW_FORM_data2;
8092 	case 4:
8093 	  return DW_FORM_data4;
8094 	case 8:
8095 	  return DW_FORM_data8;
8096 	default:
8097 	  gcc_unreachable ();
8098 	}
8099     case dw_val_class_range_list:
8100     case dw_val_class_loc_list:
8101       if (dwarf_version >= 4)
8102 	return DW_FORM_sec_offset;
8103       /* FALLTHRU */
8104     case dw_val_class_vms_delta:
8105     case dw_val_class_offset:
8106       switch (DWARF_OFFSET_SIZE)
8107 	{
8108 	case 4:
8109 	  return DW_FORM_data4;
8110 	case 8:
8111 	  return DW_FORM_data8;
8112 	default:
8113 	  gcc_unreachable ();
8114 	}
8115     case dw_val_class_loc:
8116       if (dwarf_version >= 4)
8117 	return DW_FORM_exprloc;
8118       switch (constant_size (size_of_locs (AT_loc (a))))
8119 	{
8120 	case 1:
8121 	  return DW_FORM_block1;
8122 	case 2:
8123 	  return DW_FORM_block2;
8124 	case 4:
8125 	  return DW_FORM_block4;
8126 	default:
8127 	  gcc_unreachable ();
8128 	}
8129     case dw_val_class_const:
8130       return DW_FORM_sdata;
8131     case dw_val_class_unsigned_const:
8132       switch (constant_size (AT_unsigned (a)))
8133 	{
8134 	case 1:
8135 	  return DW_FORM_data1;
8136 	case 2:
8137 	  return DW_FORM_data2;
8138 	case 4:
8139 	  /* In DWARF3 DW_AT_data_member_location with
8140 	     DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8141 	     constant, so we need to use DW_FORM_udata if we need
8142 	     a large constant.  */
8143 	  if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8144 	    return DW_FORM_udata;
8145 	  return DW_FORM_data4;
8146 	case 8:
8147 	  if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8148 	    return DW_FORM_udata;
8149 	  return DW_FORM_data8;
8150 	default:
8151 	  gcc_unreachable ();
8152 	}
8153     case dw_val_class_const_double:
8154       switch (HOST_BITS_PER_WIDE_INT)
8155 	{
8156 	case 8:
8157 	  return DW_FORM_data2;
8158 	case 16:
8159 	  return DW_FORM_data4;
8160 	case 32:
8161 	  return DW_FORM_data8;
8162 	case 64:
8163 	default:
8164 	  return DW_FORM_block1;
8165 	}
8166     case dw_val_class_vec:
8167       switch (constant_size (a->dw_attr_val.v.val_vec.length
8168 			     * a->dw_attr_val.v.val_vec.elt_size))
8169 	{
8170 	case 1:
8171 	  return DW_FORM_block1;
8172 	case 2:
8173 	  return DW_FORM_block2;
8174 	case 4:
8175 	  return DW_FORM_block4;
8176 	default:
8177 	  gcc_unreachable ();
8178 	}
8179     case dw_val_class_flag:
8180       if (dwarf_version >= 4)
8181 	{
8182 	  /* Currently all add_AT_flag calls pass in 1 as last argument,
8183 	     so DW_FORM_flag_present can be used.  If that ever changes,
8184 	     we'll need to use DW_FORM_flag and have some optimization
8185 	     in build_abbrev_table that will change those to
8186 	     DW_FORM_flag_present if it is set to 1 in all DIEs using
8187 	     the same abbrev entry.  */
8188 	  gcc_assert (a->dw_attr_val.v.val_flag == 1);
8189 	  return DW_FORM_flag_present;
8190 	}
8191       return DW_FORM_flag;
8192     case dw_val_class_die_ref:
8193       if (AT_ref_external (a))
8194 	return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8195       else
8196 	return DW_FORM_ref;
8197     case dw_val_class_fde_ref:
8198       return DW_FORM_data;
8199     case dw_val_class_lbl_id:
8200       return (AT_index (a) == NOT_INDEXED
8201               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8202     case dw_val_class_lineptr:
8203     case dw_val_class_macptr:
8204       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8205     case dw_val_class_str:
8206       return AT_string_form (a);
8207     case dw_val_class_file:
8208       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8209 	{
8210 	case 1:
8211 	  return DW_FORM_data1;
8212 	case 2:
8213 	  return DW_FORM_data2;
8214 	case 4:
8215 	  return DW_FORM_data4;
8216 	default:
8217 	  gcc_unreachable ();
8218 	}
8219 
8220     case dw_val_class_data8:
8221       return DW_FORM_data8;
8222 
8223     case dw_val_class_high_pc:
8224       switch (DWARF2_ADDR_SIZE)
8225 	{
8226 	case 1:
8227 	  return DW_FORM_data1;
8228 	case 2:
8229 	  return DW_FORM_data2;
8230 	case 4:
8231 	  return DW_FORM_data4;
8232 	case 8:
8233 	  return DW_FORM_data8;
8234 	default:
8235 	  gcc_unreachable ();
8236 	}
8237 
8238     default:
8239       gcc_unreachable ();
8240     }
8241 }
8242 
8243 /* Output the encoding of an attribute value.  */
8244 
8245 static void
output_value_format(dw_attr_ref a)8246 output_value_format (dw_attr_ref a)
8247 {
8248   enum dwarf_form form = value_format (a);
8249 
8250   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8251 }
8252 
8253 /* Given a die and id, produce the appropriate abbreviations.  */
8254 
8255 static void
output_die_abbrevs(unsigned long abbrev_id,dw_die_ref abbrev)8256 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8257 {
8258   unsigned ix;
8259   dw_attr_ref a_attr;
8260 
8261   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8262   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8263                                dwarf_tag_name (abbrev->die_tag));
8264 
8265   if (abbrev->die_child != NULL)
8266     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8267   else
8268     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8269 
8270   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8271     {
8272       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8273                                    dwarf_attr_name (a_attr->dw_attr));
8274       output_value_format (a_attr);
8275     }
8276 
8277   dw2_asm_output_data (1, 0, NULL);
8278   dw2_asm_output_data (1, 0, NULL);
8279 }
8280 
8281 
8282 /* Output the .debug_abbrev section which defines the DIE abbreviation
8283    table.  */
8284 
8285 static void
output_abbrev_section(void)8286 output_abbrev_section (void)
8287 {
8288   unsigned long abbrev_id;
8289 
8290   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8291     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8292 
8293   /* Terminate the table.  */
8294   dw2_asm_output_data (1, 0, NULL);
8295 }
8296 
8297 /* Output a symbol we can use to refer to this DIE from another CU.  */
8298 
8299 static inline void
output_die_symbol(dw_die_ref die)8300 output_die_symbol (dw_die_ref die)
8301 {
8302   const char *sym = die->die_id.die_symbol;
8303 
8304   gcc_assert (!die->comdat_type_p);
8305 
8306   if (sym == 0)
8307     return;
8308 
8309   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8310     /* We make these global, not weak; if the target doesn't support
8311        .linkonce, it doesn't support combining the sections, so debugging
8312        will break.  */
8313     targetm.asm_out.globalize_label (asm_out_file, sym);
8314 
8315   ASM_OUTPUT_LABEL (asm_out_file, sym);
8316 }
8317 
8318 /* Return a new location list, given the begin and end range, and the
8319    expression.  */
8320 
8321 static inline dw_loc_list_ref
new_loc_list(dw_loc_descr_ref expr,const char * begin,const char * end,const char * section)8322 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8323 	      const char *section)
8324 {
8325   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8326 
8327   retlist->begin = begin;
8328   retlist->begin_entry = NULL;
8329   retlist->end = end;
8330   retlist->expr = expr;
8331   retlist->section = section;
8332 
8333   return retlist;
8334 }
8335 
8336 /* Generate a new internal symbol for this location list node, if it
8337    hasn't got one yet.  */
8338 
8339 static inline void
gen_llsym(dw_loc_list_ref list)8340 gen_llsym (dw_loc_list_ref list)
8341 {
8342   gcc_assert (!list->ll_symbol);
8343   list->ll_symbol = gen_internal_sym ("LLST");
8344 }
8345 
8346 /* Output the location list given to us.  */
8347 
8348 static void
output_loc_list(dw_loc_list_ref list_head)8349 output_loc_list (dw_loc_list_ref list_head)
8350 {
8351   dw_loc_list_ref curr = list_head;
8352 
8353   if (list_head->emitted)
8354     return;
8355   list_head->emitted = true;
8356 
8357   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8358 
8359   /* Walk the location list, and output each range + expression.  */
8360   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8361     {
8362       unsigned long size;
8363       /* Don't output an entry that starts and ends at the same address.  */
8364       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8365 	continue;
8366       size = size_of_locs (curr->expr);
8367       /* If the expression is too large, drop it on the floor.  We could
8368 	 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8369 	 in the expression, but >= 64KB expressions for a single value
8370 	 in a single range are unlikely very useful.  */
8371       if (size > 0xffff)
8372 	continue;
8373       if (dwarf_split_debug_info)
8374         {
8375           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8376                                "Location list start/length entry (%s)",
8377                                list_head->ll_symbol);
8378           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8379                                        "Location list range start index (%s)",
8380                                        curr->begin);
8381           /* The length field is 4 bytes.  If we ever need to support
8382             an 8-byte length, we can add a new DW_LLE code or fall back
8383             to DW_LLE_GNU_start_end_entry.  */
8384           dw2_asm_output_delta (4, curr->end, curr->begin,
8385                                 "Location list range length (%s)",
8386                                 list_head->ll_symbol);
8387         }
8388       else if (!have_multiple_function_sections)
8389 	{
8390 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8391 				"Location list begin address (%s)",
8392 				list_head->ll_symbol);
8393 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8394 				"Location list end address (%s)",
8395 				list_head->ll_symbol);
8396 	}
8397       else
8398 	{
8399 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8400 			       "Location list begin address (%s)",
8401 			       list_head->ll_symbol);
8402 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8403 			       "Location list end address (%s)",
8404 			       list_head->ll_symbol);
8405 	}
8406 
8407       /* Output the block length for this list of location operations.  */
8408       gcc_assert (size <= 0xffff);
8409       dw2_asm_output_data (2, size, "%s", "Location expression size");
8410 
8411       output_loc_sequence (curr->expr, -1);
8412     }
8413 
8414   if (dwarf_split_debug_info)
8415     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8416                          "Location list terminator (%s)",
8417                          list_head->ll_symbol);
8418   else
8419     {
8420       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8421                            "Location list terminator begin (%s)",
8422                            list_head->ll_symbol);
8423       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8424                            "Location list terminator end (%s)",
8425                            list_head->ll_symbol);
8426     }
8427 }
8428 
8429 /* Output a range_list offset into the debug_range section.  Emit a
8430    relocated reference if val_entry is NULL, otherwise, emit an
8431    indirect reference.  */
8432 
8433 static void
output_range_list_offset(dw_attr_ref a)8434 output_range_list_offset (dw_attr_ref a)
8435 {
8436   const char *name = dwarf_attr_name (a->dw_attr);
8437 
8438   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8439     {
8440       char *p = strchr (ranges_section_label, '\0');
8441       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8442       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8443                              debug_ranges_section, "%s", name);
8444       *p = '\0';
8445     }
8446   else
8447     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8448                          "%s (offset from %s)", name, ranges_section_label);
8449 }
8450 
8451 /* Output the offset into the debug_loc section.  */
8452 
8453 static void
output_loc_list_offset(dw_attr_ref a)8454 output_loc_list_offset (dw_attr_ref a)
8455 {
8456   char *sym = AT_loc_list (a)->ll_symbol;
8457 
8458   gcc_assert (sym);
8459   if (dwarf_split_debug_info)
8460     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8461                           "%s", dwarf_attr_name (a->dw_attr));
8462   else
8463     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8464                            "%s", dwarf_attr_name (a->dw_attr));
8465 }
8466 
8467 /* Output an attribute's index or value appropriately.  */
8468 
8469 static void
output_attr_index_or_value(dw_attr_ref a)8470 output_attr_index_or_value (dw_attr_ref a)
8471 {
8472   const char *name = dwarf_attr_name (a->dw_attr);
8473 
8474   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8475     {
8476       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8477       return;
8478     }
8479   switch (AT_class (a))
8480     {
8481       case dw_val_class_addr:
8482         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8483         break;
8484       case dw_val_class_high_pc:
8485       case dw_val_class_lbl_id:
8486         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8487         break;
8488       case dw_val_class_loc_list:
8489         output_loc_list_offset (a);
8490         break;
8491       default:
8492         gcc_unreachable ();
8493     }
8494 }
8495 
8496 /* Output a type signature.  */
8497 
8498 static inline void
output_signature(const char * sig,const char * name)8499 output_signature (const char *sig, const char *name)
8500 {
8501   int i;
8502 
8503   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8504     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8505 }
8506 
8507 /* Output the DIE and its attributes.  Called recursively to generate
8508    the definitions of each child DIE.  */
8509 
8510 static void
output_die(dw_die_ref die)8511 output_die (dw_die_ref die)
8512 {
8513   dw_attr_ref a;
8514   dw_die_ref c;
8515   unsigned long size;
8516   unsigned ix;
8517 
8518   /* If someone in another CU might refer to us, set up a symbol for
8519      them to point to.  */
8520   if (! die->comdat_type_p && die->die_id.die_symbol)
8521     output_die_symbol (die);
8522 
8523   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8524 			       (unsigned long)die->die_offset,
8525 			       dwarf_tag_name (die->die_tag));
8526 
8527   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8528     {
8529       const char *name = dwarf_attr_name (a->dw_attr);
8530 
8531       switch (AT_class (a))
8532 	{
8533 	case dw_val_class_addr:
8534           output_attr_index_or_value (a);
8535 	  break;
8536 
8537 	case dw_val_class_offset:
8538 	  dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8539 			       "%s", name);
8540 	  break;
8541 
8542 	case dw_val_class_range_list:
8543           output_range_list_offset (a);
8544 	  break;
8545 
8546 	case dw_val_class_loc:
8547 	  size = size_of_locs (AT_loc (a));
8548 
8549 	  /* Output the block length for this list of location operations.  */
8550 	  if (dwarf_version >= 4)
8551 	    dw2_asm_output_data_uleb128 (size, "%s", name);
8552 	  else
8553 	    dw2_asm_output_data (constant_size (size), size, "%s", name);
8554 
8555 	  output_loc_sequence (AT_loc (a), -1);
8556 	  break;
8557 
8558 	case dw_val_class_const:
8559 	  /* ??? It would be slightly more efficient to use a scheme like is
8560 	     used for unsigned constants below, but gdb 4.x does not sign
8561 	     extend.  Gdb 5.x does sign extend.  */
8562 	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8563 	  break;
8564 
8565 	case dw_val_class_unsigned_const:
8566 	  {
8567 	    int csize = constant_size (AT_unsigned (a));
8568 	    if (dwarf_version == 3
8569 		&& a->dw_attr == DW_AT_data_member_location
8570 		&& csize >= 4)
8571 	      dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8572 	    else
8573 	      dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8574 	  }
8575 	  break;
8576 
8577 	case dw_val_class_const_double:
8578 	  {
8579 	    unsigned HOST_WIDE_INT first, second;
8580 
8581 	    if (HOST_BITS_PER_WIDE_INT >= 64)
8582 	      dw2_asm_output_data (1,
8583 				   HOST_BITS_PER_DOUBLE_INT
8584 				   / HOST_BITS_PER_CHAR,
8585 				   NULL);
8586 
8587 	    if (WORDS_BIG_ENDIAN)
8588 	      {
8589 		first = a->dw_attr_val.v.val_double.high;
8590 		second = a->dw_attr_val.v.val_double.low;
8591 	      }
8592 	    else
8593 	      {
8594 		first = a->dw_attr_val.v.val_double.low;
8595 		second = a->dw_attr_val.v.val_double.high;
8596 	      }
8597 
8598 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8599                                  first, "%s", name);
8600 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8601 				 second, NULL);
8602 	  }
8603 	  break;
8604 
8605 	case dw_val_class_vec:
8606 	  {
8607 	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8608 	    unsigned int len = a->dw_attr_val.v.val_vec.length;
8609 	    unsigned int i;
8610 	    unsigned char *p;
8611 
8612 	    dw2_asm_output_data (constant_size (len * elt_size),
8613 				 len * elt_size, "%s", name);
8614 	    if (elt_size > sizeof (HOST_WIDE_INT))
8615 	      {
8616 		elt_size /= 2;
8617 		len *= 2;
8618 	      }
8619 	    for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8620 		 i < len;
8621 		 i++, p += elt_size)
8622 	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8623 				   "fp or vector constant word %u", i);
8624 	    break;
8625 	  }
8626 
8627 	case dw_val_class_flag:
8628 	  if (dwarf_version >= 4)
8629 	    {
8630 	      /* Currently all add_AT_flag calls pass in 1 as last argument,
8631 		 so DW_FORM_flag_present can be used.  If that ever changes,
8632 		 we'll need to use DW_FORM_flag and have some optimization
8633 		 in build_abbrev_table that will change those to
8634 		 DW_FORM_flag_present if it is set to 1 in all DIEs using
8635 		 the same abbrev entry.  */
8636 	      gcc_assert (AT_flag (a) == 1);
8637 	      if (flag_debug_asm)
8638 		fprintf (asm_out_file, "\t\t\t%s %s\n",
8639 			 ASM_COMMENT_START, name);
8640 	      break;
8641 	    }
8642 	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
8643 	  break;
8644 
8645 	case dw_val_class_loc_list:
8646           output_attr_index_or_value (a);
8647 	  break;
8648 
8649 	case dw_val_class_die_ref:
8650 	  if (AT_ref_external (a))
8651 	    {
8652 	      if (AT_ref (a)->comdat_type_p)
8653 	        {
8654 	          comdat_type_node_ref type_node =
8655 	            AT_ref (a)->die_id.die_type_node;
8656 
8657 	          gcc_assert (type_node);
8658 	          output_signature (type_node->signature, name);
8659 	        }
8660 	      else
8661 	        {
8662 		  const char *sym = AT_ref (a)->die_id.die_symbol;
8663 		  int size;
8664 
8665 		  gcc_assert (sym);
8666 		  /* In DWARF2, DW_FORM_ref_addr is sized by target address
8667 		     length, whereas in DWARF3 it's always sized as an
8668 		     offset.  */
8669 		  if (dwarf_version == 2)
8670 		    size = DWARF2_ADDR_SIZE;
8671 		  else
8672 		    size = DWARF_OFFSET_SIZE;
8673 		  dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8674 					 name);
8675 		}
8676 	    }
8677 	  else
8678 	    {
8679 	      gcc_assert (AT_ref (a)->die_offset);
8680 	      dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8681 				   "%s", name);
8682 	    }
8683 	  break;
8684 
8685 	case dw_val_class_fde_ref:
8686 	  {
8687 	    char l1[20];
8688 
8689 	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8690 					 a->dw_attr_val.v.val_fde_index * 2);
8691 	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8692 				   "%s", name);
8693 	  }
8694 	  break;
8695 
8696 	case dw_val_class_vms_delta:
8697 	  dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8698 				    AT_vms_delta2 (a), AT_vms_delta1 (a),
8699 				    "%s", name);
8700 	  break;
8701 
8702 	case dw_val_class_lbl_id:
8703           output_attr_index_or_value (a);
8704 	  break;
8705 
8706 	case dw_val_class_lineptr:
8707 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8708 				 debug_line_section, "%s", name);
8709 	  break;
8710 
8711 	case dw_val_class_macptr:
8712 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8713 				 debug_macinfo_section, "%s", name);
8714 	  break;
8715 
8716 	case dw_val_class_str:
8717           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8718             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8719                                    a->dw_attr_val.v.val_str->label,
8720                                    debug_str_section,
8721                                    "%s: \"%s\"", name, AT_string (a));
8722           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8723             dw2_asm_output_data_uleb128 (AT_index (a),
8724                                          "%s: \"%s\"", name, AT_string (a));
8725           else
8726 	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8727 	  break;
8728 
8729 	case dw_val_class_file:
8730 	  {
8731 	    int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8732 
8733 	    dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8734 				 a->dw_attr_val.v.val_file->filename);
8735 	    break;
8736 	  }
8737 
8738 	case dw_val_class_data8:
8739 	  {
8740 	    int i;
8741 
8742 	    for (i = 0; i < 8; i++)
8743 	      dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8744 				   i == 0 ? "%s" : NULL, name);
8745 	    break;
8746 	  }
8747 
8748 	case dw_val_class_high_pc:
8749 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8750 				get_AT_low_pc (die), "DW_AT_high_pc");
8751 	  break;
8752 
8753 	default:
8754 	  gcc_unreachable ();
8755 	}
8756     }
8757 
8758   FOR_EACH_CHILD (die, c, output_die (c));
8759 
8760   /* Add null byte to terminate sibling list.  */
8761   if (die->die_child != NULL)
8762     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8763 			 (unsigned long) die->die_offset);
8764 }
8765 
8766 /* Output the compilation unit that appears at the beginning of the
8767    .debug_info section, and precedes the DIE descriptions.  */
8768 
8769 static void
output_compilation_unit_header(void)8770 output_compilation_unit_header (void)
8771 {
8772   int ver = dwarf_version;
8773 
8774   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8775     dw2_asm_output_data (4, 0xffffffff,
8776       "Initial length escape value indicating 64-bit DWARF extension");
8777   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8778 		       next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8779 		       "Length of Compilation Unit Info");
8780   dw2_asm_output_data (2, ver, "DWARF version number");
8781   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8782 			 debug_abbrev_section,
8783 			 "Offset Into Abbrev. Section");
8784   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8785 }
8786 
8787 /* Output the compilation unit DIE and its children.  */
8788 
8789 static void
output_comp_unit(dw_die_ref die,int output_if_empty)8790 output_comp_unit (dw_die_ref die, int output_if_empty)
8791 {
8792   const char *secname, *oldsym;
8793   char *tmp;
8794   external_ref_hash_type extern_map;
8795 
8796   /* Unless we are outputting main CU, we may throw away empty ones.  */
8797   if (!output_if_empty && die->die_child == NULL)
8798     return;
8799 
8800   /* Even if there are no children of this DIE, we must output the information
8801      about the compilation unit.  Otherwise, on an empty translation unit, we
8802      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8803      will then complain when examining the file.  First mark all the DIEs in
8804      this CU so we know which get local refs.  */
8805   mark_dies (die);
8806 
8807   extern_map = optimize_external_refs (die);
8808 
8809   build_abbrev_table (die, extern_map);
8810 
8811   extern_map.dispose ();
8812 
8813   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8814   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8815   calc_die_sizes (die);
8816 
8817   oldsym = die->die_id.die_symbol;
8818   if (oldsym)
8819     {
8820       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8821 
8822       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8823       secname = tmp;
8824       die->die_id.die_symbol = NULL;
8825       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8826     }
8827   else
8828     {
8829       switch_to_section (debug_info_section);
8830       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8831       info_section_emitted = true;
8832     }
8833 
8834   /* Output debugging information.  */
8835   output_compilation_unit_header ();
8836   output_die (die);
8837 
8838   /* Leave the marks on the main CU, so we can check them in
8839      output_pubnames.  */
8840   if (oldsym)
8841     {
8842       unmark_dies (die);
8843       die->die_id.die_symbol = oldsym;
8844     }
8845 }
8846 
8847 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8848    and .debug_pubtypes.  This is configured per-target, but can be
8849    overridden by the -gpubnames or -gno-pubnames options.  */
8850 
8851 static inline bool
want_pubnames(void)8852 want_pubnames (void)
8853 {
8854   if (debug_info_level <= DINFO_LEVEL_TERSE)
8855     return false;
8856   if (debug_generate_pub_sections != -1)
8857     return debug_generate_pub_sections;
8858   return targetm.want_debug_pub_sections;
8859 }
8860 
8861 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
8862 
8863 static void
add_AT_pubnames(dw_die_ref die)8864 add_AT_pubnames (dw_die_ref die)
8865 {
8866   if (want_pubnames ())
8867     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8868 }
8869 
8870 /* Add a string attribute value to a skeleton DIE.  */
8871 
8872 static inline void
add_skeleton_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind,const char * str)8873 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8874                         const char *str)
8875 {
8876   dw_attr_node attr;
8877   struct indirect_string_node *node;
8878 
8879   if (! skeleton_debug_str_hash)
8880     skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8881                                                debug_str_eq, NULL);
8882 
8883   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8884   find_string_form (node);
8885   if (node->form == DW_FORM_GNU_str_index)
8886     node->form = DW_FORM_strp;
8887 
8888   attr.dw_attr = attr_kind;
8889   attr.dw_attr_val.val_class = dw_val_class_str;
8890   attr.dw_attr_val.val_entry = NULL;
8891   attr.dw_attr_val.v.val_str = node;
8892   add_dwarf_attr (die, &attr);
8893 }
8894 
8895 /* Helper function to generate top-level dies for skeleton debug_info and
8896    debug_types.  */
8897 
8898 static void
add_top_level_skeleton_die_attrs(dw_die_ref die)8899 add_top_level_skeleton_die_attrs (dw_die_ref die)
8900 {
8901   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8902   const char *comp_dir = comp_dir_string ();
8903 
8904   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8905   if (comp_dir != NULL)
8906     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8907   add_AT_pubnames (die);
8908   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8909 }
8910 
8911 /* Return the single type-unit die for skeleton type units.  */
8912 
8913 static dw_die_ref
get_skeleton_type_unit(void)8914 get_skeleton_type_unit (void)
8915 {
8916   /* For dwarf_split_debug_sections with use_type info, all type units in the
8917      skeleton sections have identical dies (but different headers).  This
8918      single die will be output many times.  */
8919 
8920   static dw_die_ref skeleton_type_unit = NULL;
8921 
8922   if (skeleton_type_unit == NULL)
8923     {
8924       skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8925       add_top_level_skeleton_die_attrs (skeleton_type_unit);
8926       skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8927     }
8928   return skeleton_type_unit;
8929 }
8930 
8931 /* Output skeleton debug sections that point to the dwo file.  */
8932 
8933 static void
output_skeleton_debug_sections(dw_die_ref comp_unit)8934 output_skeleton_debug_sections (dw_die_ref comp_unit)
8935 {
8936   /* These attributes will be found in the full debug_info section.  */
8937   remove_AT (comp_unit, DW_AT_producer);
8938   remove_AT (comp_unit, DW_AT_language);
8939 
8940   switch_to_section (debug_skeleton_info_section);
8941   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8942 
8943   /* Produce the skeleton compilation-unit header.  This one differs enough from
8944      a normal CU header that it's better not to call output_compilation_unit
8945      header.  */
8946   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8947     dw2_asm_output_data (4, 0xffffffff,
8948       "Initial length escape value indicating 64-bit DWARF extension");
8949 
8950   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8951                        DWARF_COMPILE_UNIT_HEADER_SIZE
8952                        - DWARF_INITIAL_LENGTH_SIZE
8953                        + size_of_die (comp_unit),
8954                       "Length of Compilation Unit Info");
8955   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8956   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8957                          debug_abbrev_section,
8958                          "Offset Into Abbrev. Section");
8959   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8960 
8961   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8962   output_die (comp_unit);
8963 
8964   /* Build the skeleton debug_abbrev section.  */
8965   switch_to_section (debug_skeleton_abbrev_section);
8966   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8967 
8968   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8969   if (use_debug_types)
8970     output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8971 
8972   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8973 }
8974 
8975 /* Output a comdat type unit DIE and its children.  */
8976 
8977 static void
output_comdat_type_unit(comdat_type_node * node)8978 output_comdat_type_unit (comdat_type_node *node)
8979 {
8980   const char *secname;
8981   char *tmp;
8982   int i;
8983 #if defined (OBJECT_FORMAT_ELF)
8984   tree comdat_key;
8985 #endif
8986   external_ref_hash_type extern_map;
8987 
8988   /* First mark all the DIEs in this CU so we know which get local refs.  */
8989   mark_dies (node->root_die);
8990 
8991   extern_map = optimize_external_refs (node->root_die);
8992 
8993   build_abbrev_table (node->root_die, extern_map);
8994 
8995   extern_map.dispose ();
8996 
8997   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8998   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8999   calc_die_sizes (node->root_die);
9000 
9001 #if defined (OBJECT_FORMAT_ELF)
9002   if (!dwarf_split_debug_info)
9003     secname = ".debug_types";
9004   else
9005     secname = ".debug_types.dwo";
9006 
9007   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9008   sprintf (tmp, "wt.");
9009   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9010     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9011   comdat_key = get_identifier (tmp);
9012   targetm.asm_out.named_section (secname,
9013                                  SECTION_DEBUG | SECTION_LINKONCE,
9014                                  comdat_key);
9015 #else
9016   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9017   sprintf (tmp, ".gnu.linkonce.wt.");
9018   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9019     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9020   secname = tmp;
9021   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9022 #endif
9023 
9024   /* Output debugging information.  */
9025   output_compilation_unit_header ();
9026   output_signature (node->signature, "Type Signature");
9027   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9028 		       "Offset to Type DIE");
9029   output_die (node->root_die);
9030 
9031   unmark_dies (node->root_die);
9032 
9033 #if defined (OBJECT_FORMAT_ELF)
9034   if (dwarf_split_debug_info)
9035     {
9036       /* Produce the skeleton type-unit header.  */
9037       const char *secname = ".debug_types";
9038 
9039       targetm.asm_out.named_section (secname,
9040                                      SECTION_DEBUG | SECTION_LINKONCE,
9041                                      comdat_key);
9042       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9043         dw2_asm_output_data (4, 0xffffffff,
9044           "Initial length escape value indicating 64-bit DWARF extension");
9045 
9046       dw2_asm_output_data (DWARF_OFFSET_SIZE,
9047                            DWARF_COMPILE_UNIT_HEADER_SIZE
9048                            - DWARF_INITIAL_LENGTH_SIZE
9049                            + size_of_die (get_skeleton_type_unit ())
9050                            + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9051                            "Length of Type Unit Info");
9052       dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9053       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9054                              debug_skeleton_abbrev_section_label,
9055                              debug_abbrev_section,
9056                              "Offset Into Abbrev. Section");
9057       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9058       output_signature (node->signature, "Type Signature");
9059       dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9060 
9061       output_die (get_skeleton_type_unit ());
9062     }
9063 #endif
9064 }
9065 
9066 /* Return the DWARF2/3 pubname associated with a decl.  */
9067 
9068 static const char *
dwarf2_name(tree decl,int scope)9069 dwarf2_name (tree decl, int scope)
9070 {
9071   if (DECL_NAMELESS (decl))
9072     return NULL;
9073   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9074 }
9075 
9076 /* Add a new entry to .debug_pubnames if appropriate.  */
9077 
9078 static void
add_pubname_string(const char * str,dw_die_ref die)9079 add_pubname_string (const char *str, dw_die_ref die)
9080 {
9081   pubname_entry e;
9082 
9083   e.die = die;
9084   e.name = xstrdup (str);
9085   vec_safe_push (pubname_table, e);
9086 }
9087 
9088 static void
add_pubname(tree decl,dw_die_ref die)9089 add_pubname (tree decl, dw_die_ref die)
9090 {
9091   if (!want_pubnames ())
9092     return;
9093 
9094   /* Don't add items to the table when we expect that the consumer will have
9095      just read the enclosing die.  For example, if the consumer is looking at a
9096      class_member, it will either be inside the class already, or will have just
9097      looked up the class to find the member.  Either way, searching the class is
9098      faster than searching the index.  */
9099   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9100       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9101     {
9102       const char *name = dwarf2_name (decl, 1);
9103 
9104       if (name)
9105 	add_pubname_string (name, die);
9106     }
9107 }
9108 
9109 /* Add an enumerator to the pubnames section.  */
9110 
9111 static void
add_enumerator_pubname(const char * scope_name,dw_die_ref die)9112 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9113 {
9114   pubname_entry e;
9115 
9116   gcc_assert (scope_name);
9117   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9118   e.die = die;
9119   vec_safe_push (pubname_table, e);
9120 }
9121 
9122 /* Add a new entry to .debug_pubtypes if appropriate.  */
9123 
9124 static void
add_pubtype(tree decl,dw_die_ref die)9125 add_pubtype (tree decl, dw_die_ref die)
9126 {
9127   pubname_entry e;
9128 
9129   if (!want_pubnames ())
9130     return;
9131 
9132   if ((TREE_PUBLIC (decl)
9133        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9134       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9135     {
9136       tree scope = NULL;
9137       const char *scope_name = "";
9138       const char *sep = is_cxx () ? "::" : ".";
9139       const char *name;
9140 
9141       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9142       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9143         {
9144           scope_name = lang_hooks.dwarf_name (scope, 1);
9145           if (scope_name != NULL && scope_name[0] != '\0')
9146             scope_name = concat (scope_name, sep, NULL);
9147           else
9148             scope_name = "";
9149 	}
9150 
9151       if (TYPE_P (decl))
9152         name = type_tag (decl);
9153       else
9154         name = lang_hooks.dwarf_name (decl, 1);
9155 
9156       /* If we don't have a name for the type, there's no point in adding
9157 	 it to the table.  */
9158       if (name != NULL && name[0] != '\0')
9159         {
9160           e.die = die;
9161           e.name = concat (scope_name, name, NULL);
9162           vec_safe_push (pubtype_table, e);
9163         }
9164 
9165       /* Although it might be more consistent to add the pubinfo for the
9166          enumerators as their dies are created, they should only be added if the
9167          enum type meets the criteria above.  So rather than re-check the parent
9168          enum type whenever an enumerator die is created, just output them all
9169          here.  This isn't protected by the name conditional because anonymous
9170          enums don't have names.  */
9171       if (die->die_tag == DW_TAG_enumeration_type)
9172         {
9173           dw_die_ref c;
9174 
9175           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9176         }
9177     }
9178 }
9179 
9180 /* Output a single entry in the pubnames table.  */
9181 
9182 static void
output_pubname(dw_offset die_offset,pubname_entry * entry)9183 output_pubname (dw_offset die_offset, pubname_entry *entry)
9184 {
9185   dw_die_ref die = entry->die;
9186   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9187 
9188   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9189 
9190   if (debug_generate_pub_sections == 2)
9191     {
9192       /* This logic follows gdb's method for determining the value of the flag
9193          byte.  */
9194       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9195       switch (die->die_tag)
9196       {
9197         case DW_TAG_typedef:
9198         case DW_TAG_base_type:
9199         case DW_TAG_subrange_type:
9200           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9201           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9202           break;
9203         case DW_TAG_enumerator:
9204           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9205                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9206           if (!is_cxx () && !is_java ())
9207             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9208           break;
9209         case DW_TAG_subprogram:
9210           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9211                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9212           if (!is_ada ())
9213             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9214           break;
9215         case DW_TAG_constant:
9216           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9217                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9218           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9219           break;
9220         case DW_TAG_variable:
9221           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9222                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9223           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9224           break;
9225         case DW_TAG_namespace:
9226         case DW_TAG_imported_declaration:
9227           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9228           break;
9229         case DW_TAG_class_type:
9230         case DW_TAG_interface_type:
9231         case DW_TAG_structure_type:
9232         case DW_TAG_union_type:
9233         case DW_TAG_enumeration_type:
9234           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9235           if (!is_cxx () && !is_java ())
9236 	    GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9237           break;
9238         default:
9239           /* An unusual tag.  Leave the flag-byte empty.  */
9240           break;
9241       }
9242       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9243                            "GDB-index flags");
9244     }
9245 
9246   dw2_asm_output_nstring (entry->name, -1, "external name");
9247 }
9248 
9249 
9250 /* Output the public names table used to speed up access to externally
9251    visible names; or the public types table used to find type definitions.  */
9252 
9253 static void
output_pubnames(vec<pubname_entry,va_gc> * names)9254 output_pubnames (vec<pubname_entry, va_gc> *names)
9255 {
9256   unsigned i;
9257   unsigned long pubnames_length = size_of_pubnames (names);
9258   pubname_ref pub;
9259 
9260   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9261     dw2_asm_output_data (4, 0xffffffff,
9262       "Initial length escape value indicating 64-bit DWARF extension");
9263   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9264 
9265   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9266   dw2_asm_output_data (2, 2, "DWARF Version");
9267 
9268   if (dwarf_split_debug_info)
9269     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9270                            debug_skeleton_info_section,
9271                            "Offset of Compilation Unit Info");
9272   else
9273     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9274                            debug_info_section,
9275                            "Offset of Compilation Unit Info");
9276   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9277 		       "Compilation Unit Length");
9278 
9279   FOR_EACH_VEC_ELT (*names, i, pub)
9280     {
9281       if (include_pubname_in_output (names, pub))
9282 	{
9283 	  dw_offset die_offset = pub->die->die_offset;
9284 
9285           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9286           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9287             gcc_assert (pub->die->die_mark);
9288 
9289 	  /* If we're putting types in their own .debug_types sections,
9290 	     the .debug_pubtypes table will still point to the compile
9291 	     unit (not the type unit), so we want to use the offset of
9292 	     the skeleton DIE (if there is one).  */
9293 	  if (pub->die->comdat_type_p && names == pubtype_table)
9294 	    {
9295 	      comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9296 
9297 	      if (type_node != NULL)
9298 	        die_offset = (type_node->skeleton_die != NULL
9299 			      ? type_node->skeleton_die->die_offset
9300 			      : comp_unit_die ()->die_offset);
9301 	    }
9302 
9303           output_pubname (die_offset, pub);
9304 	}
9305     }
9306 
9307   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9308 }
9309 
9310 /* Output public names and types tables if necessary.  */
9311 
9312 static void
output_pubtables(void)9313 output_pubtables (void)
9314 {
9315   if (!want_pubnames () || !info_section_emitted)
9316     return;
9317 
9318   switch_to_section (debug_pubnames_section);
9319   output_pubnames (pubname_table);
9320   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9321      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9322      simply won't look for the section.  */
9323   switch_to_section (debug_pubtypes_section);
9324   output_pubnames (pubtype_table);
9325 }
9326 
9327 
9328 /* Output the information that goes into the .debug_aranges table.
9329    Namely, define the beginning and ending address range of the
9330    text section generated for this compilation unit.  */
9331 
9332 static void
output_aranges(unsigned long aranges_length)9333 output_aranges (unsigned long aranges_length)
9334 {
9335   unsigned i;
9336 
9337   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9338     dw2_asm_output_data (4, 0xffffffff,
9339       "Initial length escape value indicating 64-bit DWARF extension");
9340   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9341 		       "Length of Address Ranges Info");
9342   /* Version number for aranges is still 2, even in DWARF3.  */
9343   dw2_asm_output_data (2, 2, "DWARF Version");
9344   if (dwarf_split_debug_info)
9345     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9346                            debug_skeleton_info_section,
9347                            "Offset of Compilation Unit Info");
9348   else
9349     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9350                            debug_info_section,
9351                            "Offset of Compilation Unit Info");
9352   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9353   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9354 
9355   /* We need to align to twice the pointer size here.  */
9356   if (DWARF_ARANGES_PAD_SIZE)
9357     {
9358       /* Pad using a 2 byte words so that padding is correct for any
9359 	 pointer size.  */
9360       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9361 			   2 * DWARF2_ADDR_SIZE);
9362       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9363 	dw2_asm_output_data (2, 0, NULL);
9364     }
9365 
9366   /* It is necessary not to output these entries if the sections were
9367      not used; if the sections were not used, the length will be 0 and
9368      the address may end up as 0 if the section is discarded by ld
9369      --gc-sections, leaving an invalid (0, 0) entry that can be
9370      confused with the terminator.  */
9371   if (text_section_used)
9372     {
9373       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9374       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9375 			    text_section_label, "Length");
9376     }
9377   if (cold_text_section_used)
9378     {
9379       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9380 			   "Address");
9381       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9382 			    cold_text_section_label, "Length");
9383     }
9384 
9385   if (have_multiple_function_sections)
9386     {
9387       unsigned fde_idx;
9388       dw_fde_ref fde;
9389 
9390       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9391 	{
9392 	  if (DECL_IGNORED_P (fde->decl))
9393 	    continue;
9394 	  if (!fde->in_std_section)
9395 	    {
9396 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9397 				   "Address");
9398 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9399 				    fde->dw_fde_begin, "Length");
9400 	    }
9401 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9402 	    {
9403 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9404 				   "Address");
9405 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9406 				    fde->dw_fde_second_begin, "Length");
9407 	    }
9408 	}
9409     }
9410 
9411   /* Output the terminator words.  */
9412   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9413   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9414 }
9415 
9416 /* Add a new entry to .debug_ranges.  Return the offset at which it
9417    was placed.  */
9418 
9419 static unsigned int
add_ranges_num(int num)9420 add_ranges_num (int num)
9421 {
9422   unsigned int in_use = ranges_table_in_use;
9423 
9424   if (in_use == ranges_table_allocated)
9425     {
9426       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9427       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9428 				    ranges_table_allocated);
9429       memset (ranges_table + ranges_table_in_use, 0,
9430 	      RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9431     }
9432 
9433   ranges_table[in_use].num = num;
9434   ranges_table_in_use = in_use + 1;
9435 
9436   return in_use * 2 * DWARF2_ADDR_SIZE;
9437 }
9438 
9439 /* Add a new entry to .debug_ranges corresponding to a block, or a
9440    range terminator if BLOCK is NULL.  */
9441 
9442 static unsigned int
add_ranges(const_tree block)9443 add_ranges (const_tree block)
9444 {
9445   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9446 }
9447 
9448 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9449    When using dwarf_split_debug_info, address attributes in dies destined
9450    for the final executable should be direct references--setting the
9451    parameter force_direct ensures this behavior.  */
9452 
9453 static void
add_ranges_by_labels(dw_die_ref die,const char * begin,const char * end,bool * added,bool force_direct)9454 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9455                       bool *added, bool force_direct)
9456 {
9457   unsigned int in_use = ranges_by_label_in_use;
9458   unsigned int offset;
9459 
9460   if (in_use == ranges_by_label_allocated)
9461     {
9462       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9463       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9464 				       ranges_by_label,
9465 				       ranges_by_label_allocated);
9466       memset (ranges_by_label + ranges_by_label_in_use, 0,
9467 	      RANGES_TABLE_INCREMENT
9468 	      * sizeof (struct dw_ranges_by_label_struct));
9469     }
9470 
9471   ranges_by_label[in_use].begin = begin;
9472   ranges_by_label[in_use].end = end;
9473   ranges_by_label_in_use = in_use + 1;
9474 
9475   offset = add_ranges_num (-(int)in_use - 1);
9476   if (!*added)
9477     {
9478       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9479       *added = true;
9480     }
9481 }
9482 
9483 static void
output_ranges(void)9484 output_ranges (void)
9485 {
9486   unsigned i;
9487   static const char *const start_fmt = "Offset %#x";
9488   const char *fmt = start_fmt;
9489 
9490   for (i = 0; i < ranges_table_in_use; i++)
9491     {
9492       int block_num = ranges_table[i].num;
9493 
9494       if (block_num > 0)
9495 	{
9496 	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9497 	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9498 
9499 	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9500 	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9501 
9502 	  /* If all code is in the text section, then the compilation
9503 	     unit base address defaults to DW_AT_low_pc, which is the
9504 	     base of the text section.  */
9505 	  if (!have_multiple_function_sections)
9506 	    {
9507 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9508 				    text_section_label,
9509 				    fmt, i * 2 * DWARF2_ADDR_SIZE);
9510 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9511 				    text_section_label, NULL);
9512 	    }
9513 
9514 	  /* Otherwise, the compilation unit base address is zero,
9515 	     which allows us to use absolute addresses, and not worry
9516 	     about whether the target supports cross-section
9517 	     arithmetic.  */
9518 	  else
9519 	    {
9520 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9521 				   fmt, i * 2 * DWARF2_ADDR_SIZE);
9522 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9523 	    }
9524 
9525 	  fmt = NULL;
9526 	}
9527 
9528       /* Negative block_num stands for an index into ranges_by_label.  */
9529       else if (block_num < 0)
9530 	{
9531 	  int lab_idx = - block_num - 1;
9532 
9533 	  if (!have_multiple_function_sections)
9534 	    {
9535 	      gcc_unreachable ();
9536 #if 0
9537 	      /* If we ever use add_ranges_by_labels () for a single
9538 		 function section, all we have to do is to take out
9539 		 the #if 0 above.  */
9540 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9541 				    ranges_by_label[lab_idx].begin,
9542 				    text_section_label,
9543 				    fmt, i * 2 * DWARF2_ADDR_SIZE);
9544 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9545 				    ranges_by_label[lab_idx].end,
9546 				    text_section_label, NULL);
9547 #endif
9548 	    }
9549 	  else
9550 	    {
9551 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9552 				   ranges_by_label[lab_idx].begin,
9553 				   fmt, i * 2 * DWARF2_ADDR_SIZE);
9554 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9555 				   ranges_by_label[lab_idx].end,
9556 				   NULL);
9557 	    }
9558 	}
9559       else
9560 	{
9561 	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9562 	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9563 	  fmt = start_fmt;
9564 	}
9565     }
9566 }
9567 
9568 /* Data structure containing information about input files.  */
9569 struct file_info
9570 {
9571   const char *path;	/* Complete file name.  */
9572   const char *fname;	/* File name part.  */
9573   int length;		/* Length of entire string.  */
9574   struct dwarf_file_data * file_idx;	/* Index in input file table.  */
9575   int dir_idx;		/* Index in directory table.  */
9576 };
9577 
9578 /* Data structure containing information about directories with source
9579    files.  */
9580 struct dir_info
9581 {
9582   const char *path;	/* Path including directory name.  */
9583   int length;		/* Path length.  */
9584   int prefix;		/* Index of directory entry which is a prefix.  */
9585   int count;		/* Number of files in this directory.  */
9586   int dir_idx;		/* Index of directory used as base.  */
9587 };
9588 
9589 /* Callback function for file_info comparison.  We sort by looking at
9590    the directories in the path.  */
9591 
9592 static int
file_info_cmp(const void * p1,const void * p2)9593 file_info_cmp (const void *p1, const void *p2)
9594 {
9595   const struct file_info *const s1 = (const struct file_info *) p1;
9596   const struct file_info *const s2 = (const struct file_info *) p2;
9597   const unsigned char *cp1;
9598   const unsigned char *cp2;
9599 
9600   /* Take care of file names without directories.  We need to make sure that
9601      we return consistent values to qsort since some will get confused if
9602      we return the same value when identical operands are passed in opposite
9603      orders.  So if neither has a directory, return 0 and otherwise return
9604      1 or -1 depending on which one has the directory.  */
9605   if ((s1->path == s1->fname || s2->path == s2->fname))
9606     return (s2->path == s2->fname) - (s1->path == s1->fname);
9607 
9608   cp1 = (const unsigned char *) s1->path;
9609   cp2 = (const unsigned char *) s2->path;
9610 
9611   while (1)
9612     {
9613       ++cp1;
9614       ++cp2;
9615       /* Reached the end of the first path?  If so, handle like above.  */
9616       if ((cp1 == (const unsigned char *) s1->fname)
9617 	  || (cp2 == (const unsigned char *) s2->fname))
9618 	return ((cp2 == (const unsigned char *) s2->fname)
9619 		- (cp1 == (const unsigned char *) s1->fname));
9620 
9621       /* Character of current path component the same?  */
9622       else if (*cp1 != *cp2)
9623 	return *cp1 - *cp2;
9624     }
9625 }
9626 
9627 struct file_name_acquire_data
9628 {
9629   struct file_info *files;
9630   int used_files;
9631   int max_files;
9632 };
9633 
9634 /* Traversal function for the hash table.  */
9635 
9636 static int
file_name_acquire(void ** slot,void * data)9637 file_name_acquire (void ** slot, void *data)
9638 {
9639   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9640   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9641   struct file_info *fi;
9642   const char *f;
9643 
9644   gcc_assert (fnad->max_files >= d->emitted_number);
9645 
9646   if (! d->emitted_number)
9647     return 1;
9648 
9649   gcc_assert (fnad->max_files != fnad->used_files);
9650 
9651   fi = fnad->files + fnad->used_files++;
9652 
9653   /* Skip all leading "./".  */
9654   f = d->filename;
9655   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9656     f += 2;
9657 
9658   /* Create a new array entry.  */
9659   fi->path = f;
9660   fi->length = strlen (f);
9661   fi->file_idx = d;
9662 
9663   /* Search for the file name part.  */
9664   f = strrchr (f, DIR_SEPARATOR);
9665 #if defined (DIR_SEPARATOR_2)
9666   {
9667     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9668 
9669     if (g != NULL)
9670       {
9671 	if (f == NULL || f < g)
9672 	  f = g;
9673       }
9674   }
9675 #endif
9676 
9677   fi->fname = f == NULL ? fi->path : f + 1;
9678   return 1;
9679 }
9680 
9681 /* Output the directory table and the file name table.  We try to minimize
9682    the total amount of memory needed.  A heuristic is used to avoid large
9683    slowdowns with many input files.  */
9684 
9685 static void
output_file_names(void)9686 output_file_names (void)
9687 {
9688   struct file_name_acquire_data fnad;
9689   int numfiles;
9690   struct file_info *files;
9691   struct dir_info *dirs;
9692   int *saved;
9693   int *savehere;
9694   int *backmap;
9695   int ndirs;
9696   int idx_offset;
9697   int i;
9698 
9699   if (!last_emitted_file)
9700     {
9701       dw2_asm_output_data (1, 0, "End directory table");
9702       dw2_asm_output_data (1, 0, "End file name table");
9703       return;
9704     }
9705 
9706   numfiles = last_emitted_file->emitted_number;
9707 
9708   /* Allocate the various arrays we need.  */
9709   files = XALLOCAVEC (struct file_info, numfiles);
9710   dirs = XALLOCAVEC (struct dir_info, numfiles);
9711 
9712   fnad.files = files;
9713   fnad.used_files = 0;
9714   fnad.max_files = numfiles;
9715   htab_traverse (file_table, file_name_acquire, &fnad);
9716   gcc_assert (fnad.used_files == fnad.max_files);
9717 
9718   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9719 
9720   /* Find all the different directories used.  */
9721   dirs[0].path = files[0].path;
9722   dirs[0].length = files[0].fname - files[0].path;
9723   dirs[0].prefix = -1;
9724   dirs[0].count = 1;
9725   dirs[0].dir_idx = 0;
9726   files[0].dir_idx = 0;
9727   ndirs = 1;
9728 
9729   for (i = 1; i < numfiles; i++)
9730     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9731 	&& memcmp (dirs[ndirs - 1].path, files[i].path,
9732 		   dirs[ndirs - 1].length) == 0)
9733       {
9734 	/* Same directory as last entry.  */
9735 	files[i].dir_idx = ndirs - 1;
9736 	++dirs[ndirs - 1].count;
9737       }
9738     else
9739       {
9740 	int j;
9741 
9742 	/* This is a new directory.  */
9743 	dirs[ndirs].path = files[i].path;
9744 	dirs[ndirs].length = files[i].fname - files[i].path;
9745 	dirs[ndirs].count = 1;
9746 	dirs[ndirs].dir_idx = ndirs;
9747 	files[i].dir_idx = ndirs;
9748 
9749 	/* Search for a prefix.  */
9750 	dirs[ndirs].prefix = -1;
9751 	for (j = 0; j < ndirs; j++)
9752 	  if (dirs[j].length < dirs[ndirs].length
9753 	      && dirs[j].length > 1
9754 	      && (dirs[ndirs].prefix == -1
9755 		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9756 	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9757 	    dirs[ndirs].prefix = j;
9758 
9759 	++ndirs;
9760       }
9761 
9762   /* Now to the actual work.  We have to find a subset of the directories which
9763      allow expressing the file name using references to the directory table
9764      with the least amount of characters.  We do not do an exhaustive search
9765      where we would have to check out every combination of every single
9766      possible prefix.  Instead we use a heuristic which provides nearly optimal
9767      results in most cases and never is much off.  */
9768   saved = XALLOCAVEC (int, ndirs);
9769   savehere = XALLOCAVEC (int, ndirs);
9770 
9771   memset (saved, '\0', ndirs * sizeof (saved[0]));
9772   for (i = 0; i < ndirs; i++)
9773     {
9774       int j;
9775       int total;
9776 
9777       /* We can always save some space for the current directory.  But this
9778 	 does not mean it will be enough to justify adding the directory.  */
9779       savehere[i] = dirs[i].length;
9780       total = (savehere[i] - saved[i]) * dirs[i].count;
9781 
9782       for (j = i + 1; j < ndirs; j++)
9783 	{
9784 	  savehere[j] = 0;
9785 	  if (saved[j] < dirs[i].length)
9786 	    {
9787 	      /* Determine whether the dirs[i] path is a prefix of the
9788 		 dirs[j] path.  */
9789 	      int k;
9790 
9791 	      k = dirs[j].prefix;
9792 	      while (k != -1 && k != (int) i)
9793 		k = dirs[k].prefix;
9794 
9795 	      if (k == (int) i)
9796 		{
9797 		  /* Yes it is.  We can possibly save some memory by
9798 		     writing the filenames in dirs[j] relative to
9799 		     dirs[i].  */
9800 		  savehere[j] = dirs[i].length;
9801 		  total += (savehere[j] - saved[j]) * dirs[j].count;
9802 		}
9803 	    }
9804 	}
9805 
9806       /* Check whether we can save enough to justify adding the dirs[i]
9807 	 directory.  */
9808       if (total > dirs[i].length + 1)
9809 	{
9810 	  /* It's worthwhile adding.  */
9811 	  for (j = i; j < ndirs; j++)
9812 	    if (savehere[j] > 0)
9813 	      {
9814 		/* Remember how much we saved for this directory so far.  */
9815 		saved[j] = savehere[j];
9816 
9817 		/* Remember the prefix directory.  */
9818 		dirs[j].dir_idx = i;
9819 	      }
9820 	}
9821     }
9822 
9823   /* Emit the directory name table.  */
9824   idx_offset = dirs[0].length > 0 ? 1 : 0;
9825   for (i = 1 - idx_offset; i < ndirs; i++)
9826     dw2_asm_output_nstring (dirs[i].path,
9827 			    dirs[i].length
9828 			     - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9829 			    "Directory Entry: %#x", i + idx_offset);
9830 
9831   dw2_asm_output_data (1, 0, "End directory table");
9832 
9833   /* We have to emit them in the order of emitted_number since that's
9834      used in the debug info generation.  To do this efficiently we
9835      generate a back-mapping of the indices first.  */
9836   backmap = XALLOCAVEC (int, numfiles);
9837   for (i = 0; i < numfiles; i++)
9838     backmap[files[i].file_idx->emitted_number - 1] = i;
9839 
9840   /* Now write all the file names.  */
9841   for (i = 0; i < numfiles; i++)
9842     {
9843       int file_idx = backmap[i];
9844       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9845 
9846 #ifdef VMS_DEBUGGING_INFO
9847 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9848 
9849       /* Setting these fields can lead to debugger miscomparisons,
9850          but VMS Debug requires them to be set correctly.  */
9851 
9852       int ver;
9853       long long cdt;
9854       long siz;
9855       int maxfilelen = strlen (files[file_idx].path)
9856 			       + dirs[dir_idx].length
9857 			       + MAX_VMS_VERSION_LEN + 1;
9858       char *filebuf = XALLOCAVEC (char, maxfilelen);
9859 
9860       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9861       snprintf (filebuf, maxfilelen, "%s;%d",
9862 	        files[file_idx].path + dirs[dir_idx].length, ver);
9863 
9864       dw2_asm_output_nstring
9865 	(filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9866 
9867       /* Include directory index.  */
9868       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9869 
9870       /* Modification time.  */
9871       dw2_asm_output_data_uleb128
9872         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9873 	  ? cdt : 0,
9874 	 NULL);
9875 
9876       /* File length in bytes.  */
9877       dw2_asm_output_data_uleb128
9878         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9879       	  ? siz : 0,
9880 	 NULL);
9881 #else
9882       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9883 			      "File Entry: %#x", (unsigned) i + 1);
9884 
9885       /* Include directory index.  */
9886       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9887 
9888       /* Modification time.  */
9889       dw2_asm_output_data_uleb128 (0, NULL);
9890 
9891       /* File length in bytes.  */
9892       dw2_asm_output_data_uleb128 (0, NULL);
9893 #endif /* VMS_DEBUGGING_INFO */
9894     }
9895 
9896   dw2_asm_output_data (1, 0, "End file name table");
9897 }
9898 
9899 
9900 /* Output one line number table into the .debug_line section.  */
9901 
9902 static void
output_one_line_info_table(dw_line_info_table * table)9903 output_one_line_info_table (dw_line_info_table *table)
9904 {
9905   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9906   unsigned int current_line = 1;
9907   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9908   dw_line_info_entry *ent;
9909   size_t i;
9910 
9911   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9912     {
9913       switch (ent->opcode)
9914 	{
9915 	case LI_set_address:
9916 	  /* ??? Unfortunately, we have little choice here currently, and
9917 	     must always use the most general form.  GCC does not know the
9918 	     address delta itself, so we can't use DW_LNS_advance_pc.  Many
9919 	     ports do have length attributes which will give an upper bound
9920 	     on the address range.  We could perhaps use length attributes
9921 	     to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9922 	  ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9923 
9924 	  /* This can handle any delta.  This takes
9925 	     4+DWARF2_ADDR_SIZE bytes.  */
9926 	  dw2_asm_output_data (1, 0, "set address %s", line_label);
9927 	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9928 	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9929 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9930 	  break;
9931 
9932 	case LI_set_line:
9933 	  if (ent->val == current_line)
9934 	    {
9935 	      /* We still need to start a new row, so output a copy insn.  */
9936 	      dw2_asm_output_data (1, DW_LNS_copy,
9937 				   "copy line %u", current_line);
9938 	    }
9939 	  else
9940 	    {
9941 	      int line_offset = ent->val - current_line;
9942 	      int line_delta = line_offset - DWARF_LINE_BASE;
9943 
9944 	      current_line = ent->val;
9945 	      if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9946 		{
9947 		  /* This can handle deltas from -10 to 234, using the current
9948 		     definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9949 		     This takes 1 byte.  */
9950 		  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9951 				       "line %u", current_line);
9952 		}
9953 	      else
9954 		{
9955 		  /* This can handle any delta.  This takes at least 4 bytes,
9956 		     depending on the value being encoded.  */
9957 		  dw2_asm_output_data (1, DW_LNS_advance_line,
9958 				       "advance to line %u", current_line);
9959 		  dw2_asm_output_data_sleb128 (line_offset, NULL);
9960 		  dw2_asm_output_data (1, DW_LNS_copy, NULL);
9961 		}
9962 	    }
9963 	  break;
9964 
9965 	case LI_set_file:
9966 	  dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9967 	  dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9968 	  break;
9969 
9970 	case LI_set_column:
9971 	  dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9972 	  dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9973 	  break;
9974 
9975 	case LI_negate_stmt:
9976 	  current_is_stmt = !current_is_stmt;
9977 	  dw2_asm_output_data (1, DW_LNS_negate_stmt,
9978 			       "is_stmt %d", current_is_stmt);
9979 	  break;
9980 
9981 	case LI_set_prologue_end:
9982 	  dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9983 			       "set prologue end");
9984 	  break;
9985 
9986 	case LI_set_epilogue_begin:
9987 	  dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9988 			       "set epilogue begin");
9989 	  break;
9990 
9991 	case LI_set_discriminator:
9992 	  dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9993 	  dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9994 	  dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9995 	  dw2_asm_output_data_uleb128 (ent->val, NULL);
9996 	  break;
9997 	}
9998     }
9999 
10000   /* Emit debug info for the address of the end of the table.  */
10001   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10002   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10003   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10004   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10005 
10006   dw2_asm_output_data (1, 0, "end sequence");
10007   dw2_asm_output_data_uleb128 (1, NULL);
10008   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10009 }
10010 
10011 /* Output the source line number correspondence information.  This
10012    information goes into the .debug_line section.  */
10013 
10014 static void
output_line_info(bool prologue_only)10015 output_line_info (bool prologue_only)
10016 {
10017   char l1[20], l2[20], p1[20], p2[20];
10018   int ver = dwarf_version;
10019   bool saw_one = false;
10020   int opc;
10021 
10022   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10023   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10024   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10025   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10026 
10027   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10028     dw2_asm_output_data (4, 0xffffffff,
10029       "Initial length escape value indicating 64-bit DWARF extension");
10030   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10031 			"Length of Source Line Info");
10032   ASM_OUTPUT_LABEL (asm_out_file, l1);
10033 
10034   dw2_asm_output_data (2, ver, "DWARF Version");
10035   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10036   ASM_OUTPUT_LABEL (asm_out_file, p1);
10037 
10038   /* Define the architecture-dependent minimum instruction length (in bytes).
10039      In this implementation of DWARF, this field is used for information
10040      purposes only.  Since GCC generates assembly language, we have no
10041      a priori knowledge of how many instruction bytes are generated for each
10042      source line, and therefore can use only the DW_LNE_set_address and
10043      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10044      this as '1', which is "correct enough" for all architectures,
10045      and don't let the target override.  */
10046   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10047 
10048   if (ver >= 4)
10049     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10050 			 "Maximum Operations Per Instruction");
10051   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10052 		       "Default is_stmt_start flag");
10053   dw2_asm_output_data (1, DWARF_LINE_BASE,
10054 		       "Line Base Value (Special Opcodes)");
10055   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10056 		       "Line Range Value (Special Opcodes)");
10057   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10058 		       "Special Opcode Base");
10059 
10060   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10061     {
10062       int n_op_args;
10063       switch (opc)
10064 	{
10065 	case DW_LNS_advance_pc:
10066 	case DW_LNS_advance_line:
10067 	case DW_LNS_set_file:
10068 	case DW_LNS_set_column:
10069 	case DW_LNS_fixed_advance_pc:
10070 	case DW_LNS_set_isa:
10071 	  n_op_args = 1;
10072 	  break;
10073 	default:
10074 	  n_op_args = 0;
10075 	  break;
10076 	}
10077 
10078       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10079 			   opc, n_op_args);
10080     }
10081 
10082   /* Write out the information about the files we use.  */
10083   output_file_names ();
10084   ASM_OUTPUT_LABEL (asm_out_file, p2);
10085   if (prologue_only)
10086     {
10087       /* Output the marker for the end of the line number info.  */
10088       ASM_OUTPUT_LABEL (asm_out_file, l2);
10089       return;
10090     }
10091 
10092   if (separate_line_info)
10093     {
10094       dw_line_info_table *table;
10095       size_t i;
10096 
10097       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10098 	if (table->in_use)
10099 	  {
10100 	    output_one_line_info_table (table);
10101 	    saw_one = true;
10102 	  }
10103     }
10104   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10105     {
10106       output_one_line_info_table (cold_text_section_line_info);
10107       saw_one = true;
10108     }
10109 
10110   /* ??? Some Darwin linkers crash on a .debug_line section with no
10111      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10112      sufficient -- the address column must also be initialized.
10113      Make sure to output at least one set_address/end_sequence pair,
10114      choosing .text since that section is always present.  */
10115   if (text_section_line_info->in_use || !saw_one)
10116     output_one_line_info_table (text_section_line_info);
10117 
10118   /* Output the marker for the end of the line number info.  */
10119   ASM_OUTPUT_LABEL (asm_out_file, l2);
10120 }
10121 
10122 /* Given a pointer to a tree node for some base type, return a pointer to
10123    a DIE that describes the given type.
10124 
10125    This routine must only be called for GCC type nodes that correspond to
10126    Dwarf base (fundamental) types.  */
10127 
10128 static dw_die_ref
base_type_die(tree type)10129 base_type_die (tree type)
10130 {
10131   dw_die_ref base_type_result;
10132   enum dwarf_type encoding;
10133 
10134   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10135     return 0;
10136 
10137   /* If this is a subtype that should not be emitted as a subrange type,
10138      use the base type.  See subrange_type_for_debug_p.  */
10139   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10140     type = TREE_TYPE (type);
10141 
10142   switch (TREE_CODE (type))
10143     {
10144     case INTEGER_TYPE:
10145       if ((dwarf_version >= 4 || !dwarf_strict)
10146 	  && TYPE_NAME (type)
10147 	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10148 	  && DECL_IS_BUILTIN (TYPE_NAME (type))
10149 	  && DECL_NAME (TYPE_NAME (type)))
10150 	{
10151 	  const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10152 	  if (strcmp (name, "char16_t") == 0
10153 	      || strcmp (name, "char32_t") == 0)
10154 	    {
10155 	      encoding = DW_ATE_UTF;
10156 	      break;
10157 	    }
10158 	}
10159       if (TYPE_STRING_FLAG (type))
10160 	{
10161 	  if (TYPE_UNSIGNED (type))
10162 	    encoding = DW_ATE_unsigned_char;
10163 	  else
10164 	    encoding = DW_ATE_signed_char;
10165 	}
10166       else if (TYPE_UNSIGNED (type))
10167 	encoding = DW_ATE_unsigned;
10168       else
10169 	encoding = DW_ATE_signed;
10170       break;
10171 
10172     case REAL_TYPE:
10173       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10174 	{
10175 	  if (dwarf_version >= 3 || !dwarf_strict)
10176 	    encoding = DW_ATE_decimal_float;
10177 	  else
10178 	    encoding = DW_ATE_lo_user;
10179 	}
10180       else
10181 	encoding = DW_ATE_float;
10182       break;
10183 
10184     case FIXED_POINT_TYPE:
10185       if (!(dwarf_version >= 3 || !dwarf_strict))
10186 	encoding = DW_ATE_lo_user;
10187       else if (TYPE_UNSIGNED (type))
10188 	encoding = DW_ATE_unsigned_fixed;
10189       else
10190 	encoding = DW_ATE_signed_fixed;
10191       break;
10192 
10193       /* Dwarf2 doesn't know anything about complex ints, so use
10194 	 a user defined type for it.  */
10195     case COMPLEX_TYPE:
10196       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10197 	encoding = DW_ATE_complex_float;
10198       else
10199 	encoding = DW_ATE_lo_user;
10200       break;
10201 
10202     case BOOLEAN_TYPE:
10203       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10204       encoding = DW_ATE_boolean;
10205       break;
10206 
10207     default:
10208       /* No other TREE_CODEs are Dwarf fundamental types.  */
10209       gcc_unreachable ();
10210     }
10211 
10212   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10213 
10214   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10215 		   int_size_in_bytes (type));
10216   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10217   add_pubtype (type, base_type_result);
10218 
10219   return base_type_result;
10220 }
10221 
10222 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10223    named 'auto' in its type: return true for it, false otherwise.  */
10224 
10225 static inline bool
is_cxx_auto(tree type)10226 is_cxx_auto (tree type)
10227 {
10228   if (is_cxx ())
10229     {
10230       tree name = TYPE_NAME (type);
10231       if (TREE_CODE (name) == TYPE_DECL)
10232 	name = DECL_NAME (name);
10233       if (name == get_identifier ("auto")
10234 	  || name == get_identifier ("decltype(auto)"))
10235 	return true;
10236     }
10237   return false;
10238 }
10239 
10240 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10241    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10242 
10243 static inline int
is_base_type(tree type)10244 is_base_type (tree type)
10245 {
10246   switch (TREE_CODE (type))
10247     {
10248     case ERROR_MARK:
10249     case VOID_TYPE:
10250     case INTEGER_TYPE:
10251     case REAL_TYPE:
10252     case FIXED_POINT_TYPE:
10253     case COMPLEX_TYPE:
10254     case BOOLEAN_TYPE:
10255       return 1;
10256 
10257     case ARRAY_TYPE:
10258     case RECORD_TYPE:
10259     case UNION_TYPE:
10260     case QUAL_UNION_TYPE:
10261     case ENUMERAL_TYPE:
10262     case FUNCTION_TYPE:
10263     case METHOD_TYPE:
10264     case POINTER_TYPE:
10265     case REFERENCE_TYPE:
10266     case NULLPTR_TYPE:
10267     case OFFSET_TYPE:
10268     case LANG_TYPE:
10269     case VECTOR_TYPE:
10270       return 0;
10271 
10272     default:
10273       if (is_cxx_auto (type))
10274 	return 0;
10275       gcc_unreachable ();
10276     }
10277 
10278   return 0;
10279 }
10280 
10281 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10282    node, return the size in bits for the type if it is a constant, or else
10283    return the alignment for the type if the type's size is not constant, or
10284    else return BITS_PER_WORD if the type actually turns out to be an
10285    ERROR_MARK node.  */
10286 
10287 static inline unsigned HOST_WIDE_INT
simple_type_size_in_bits(const_tree type)10288 simple_type_size_in_bits (const_tree type)
10289 {
10290   if (TREE_CODE (type) == ERROR_MARK)
10291     return BITS_PER_WORD;
10292   else if (TYPE_SIZE (type) == NULL_TREE)
10293     return 0;
10294   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10295     return tree_to_uhwi (TYPE_SIZE (type));
10296   else
10297     return TYPE_ALIGN (type);
10298 }
10299 
10300 /* Similarly, but return a double_int instead of UHWI.  */
10301 
10302 static inline double_int
double_int_type_size_in_bits(const_tree type)10303 double_int_type_size_in_bits (const_tree type)
10304 {
10305   if (TREE_CODE (type) == ERROR_MARK)
10306     return double_int::from_uhwi (BITS_PER_WORD);
10307   else if (TYPE_SIZE (type) == NULL_TREE)
10308     return double_int_zero;
10309   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10310     return tree_to_double_int (TYPE_SIZE (type));
10311   else
10312     return double_int::from_uhwi (TYPE_ALIGN (type));
10313 }
10314 
10315 /*  Given a pointer to a tree node for a subrange type, return a pointer
10316     to a DIE that describes the given type.  */
10317 
10318 static dw_die_ref
subrange_type_die(tree type,tree low,tree high,dw_die_ref context_die)10319 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10320 {
10321   dw_die_ref subrange_die;
10322   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10323 
10324   if (context_die == NULL)
10325     context_die = comp_unit_die ();
10326 
10327   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10328 
10329   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10330     {
10331       /* The size of the subrange type and its base type do not match,
10332 	 so we need to generate a size attribute for the subrange type.  */
10333       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10334     }
10335 
10336   if (low)
10337     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10338   if (high)
10339     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10340 
10341   return subrange_die;
10342 }
10343 
10344 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10345    entry that chains various modifiers in front of the given type.  */
10346 
10347 static dw_die_ref
modified_type_die(tree type,int is_const_type,int is_volatile_type,dw_die_ref context_die)10348 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10349 		   dw_die_ref context_die)
10350 {
10351   enum tree_code code = TREE_CODE (type);
10352   dw_die_ref mod_type_die;
10353   dw_die_ref sub_die = NULL;
10354   tree item_type = NULL;
10355   tree qualified_type;
10356   tree name, low, high;
10357   dw_die_ref mod_scope;
10358 
10359   if (code == ERROR_MARK)
10360     return NULL;
10361 
10362   /* See if we already have the appropriately qualified variant of
10363      this type.  */
10364   qualified_type
10365     = get_qualified_type (type,
10366 			  ((is_const_type ? TYPE_QUAL_CONST : 0)
10367 			   | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10368 
10369   if (qualified_type == sizetype
10370       && TYPE_NAME (qualified_type)
10371       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10372     {
10373       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10374 
10375       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10376 			   && TYPE_PRECISION (t)
10377 			   == TYPE_PRECISION (qualified_type)
10378 			   && TYPE_UNSIGNED (t)
10379 			   == TYPE_UNSIGNED (qualified_type));
10380       qualified_type = t;
10381     }
10382 
10383   /* If we do, then we can just use its DIE, if it exists.  */
10384   if (qualified_type)
10385     {
10386       mod_type_die = lookup_type_die (qualified_type);
10387       if (mod_type_die)
10388 	return mod_type_die;
10389     }
10390 
10391   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10392 
10393   /* Handle C typedef types.  */
10394   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10395       && !DECL_ARTIFICIAL (name))
10396     {
10397       tree dtype = TREE_TYPE (name);
10398 
10399       if (qualified_type == dtype)
10400 	{
10401 	  /* For a named type, use the typedef.  */
10402 	  gen_type_die (qualified_type, context_die);
10403 	  return lookup_type_die (qualified_type);
10404 	}
10405       else if (is_const_type < TYPE_READONLY (dtype)
10406 	       || is_volatile_type < TYPE_VOLATILE (dtype)
10407 	       || (is_const_type <= TYPE_READONLY (dtype)
10408 		   && is_volatile_type <= TYPE_VOLATILE (dtype)
10409 		   && DECL_ORIGINAL_TYPE (name) != type))
10410 	/* cv-unqualified version of named type.  Just use the unnamed
10411 	   type to which it refers.  */
10412 	return modified_type_die (DECL_ORIGINAL_TYPE (name),
10413 				  is_const_type, is_volatile_type,
10414 				  context_die);
10415       /* Else cv-qualified version of named type; fall through.  */
10416     }
10417 
10418   mod_scope = scope_die_for (type, context_die);
10419 
10420   if (is_const_type
10421       /* If both is_const_type and is_volatile_type, prefer the path
10422 	 which leads to a qualified type.  */
10423       && (!is_volatile_type
10424 	  || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10425 	  || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10426     {
10427       mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10428       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10429     }
10430   else if (is_volatile_type)
10431     {
10432       mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10433       sub_die = modified_type_die (type, is_const_type, 0, context_die);
10434     }
10435   else if (code == POINTER_TYPE)
10436     {
10437       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10438       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10439 		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
10440       item_type = TREE_TYPE (type);
10441       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10442 	add_AT_unsigned (mod_type_die, DW_AT_address_class,
10443 			 TYPE_ADDR_SPACE (item_type));
10444     }
10445   else if (code == REFERENCE_TYPE)
10446     {
10447       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10448 	mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10449 				type);
10450       else
10451 	mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10452       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10453 		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
10454       item_type = TREE_TYPE (type);
10455       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10456 	add_AT_unsigned (mod_type_die, DW_AT_address_class,
10457 			 TYPE_ADDR_SPACE (item_type));
10458     }
10459   else if (code == INTEGER_TYPE
10460 	   && TREE_TYPE (type) != NULL_TREE
10461 	   && subrange_type_for_debug_p (type, &low, &high))
10462     {
10463       mod_type_die = subrange_type_die (type, low, high, context_die);
10464       item_type = TREE_TYPE (type);
10465     }
10466   else if (is_base_type (type))
10467     mod_type_die = base_type_die (type);
10468   else
10469     {
10470       gen_type_die (type, context_die);
10471 
10472       /* We have to get the type_main_variant here (and pass that to the
10473 	 `lookup_type_die' routine) because the ..._TYPE node we have
10474 	 might simply be a *copy* of some original type node (where the
10475 	 copy was created to help us keep track of typedef names) and
10476 	 that copy might have a different TYPE_UID from the original
10477 	 ..._TYPE node.  */
10478       if (TREE_CODE (type) != VECTOR_TYPE)
10479 	return lookup_type_die (type_main_variant (type));
10480       else
10481 	/* Vectors have the debugging information in the type,
10482 	   not the main variant.  */
10483 	return lookup_type_die (type);
10484     }
10485 
10486   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10487      don't output a DW_TAG_typedef, since there isn't one in the
10488      user's program; just attach a DW_AT_name to the type.
10489      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10490      if the base type already has the same name.  */
10491   if (name
10492       && ((TREE_CODE (name) != TYPE_DECL
10493 	   && (qualified_type == TYPE_MAIN_VARIANT (type)
10494 	       || (!is_const_type && !is_volatile_type)))
10495 	  || (TREE_CODE (name) == TYPE_DECL
10496 	      && TREE_TYPE (name) == qualified_type
10497 	      && DECL_NAME (name))))
10498     {
10499       if (TREE_CODE (name) == TYPE_DECL)
10500 	/* Could just call add_name_and_src_coords_attributes here,
10501 	   but since this is a builtin type it doesn't have any
10502 	   useful source coordinates anyway.  */
10503 	name = DECL_NAME (name);
10504       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10505     }
10506   /* This probably indicates a bug.  */
10507   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10508     {
10509       name = TYPE_NAME (type);
10510       if (name
10511 	  && TREE_CODE (name) == TYPE_DECL)
10512 	name = DECL_NAME (name);
10513       add_name_attribute (mod_type_die,
10514 			  name ? IDENTIFIER_POINTER (name) : "__unknown__");
10515     }
10516 
10517   if (qualified_type)
10518     equate_type_number_to_die (qualified_type, mod_type_die);
10519 
10520   if (item_type)
10521     /* We must do this after the equate_type_number_to_die call, in case
10522        this is a recursive type.  This ensures that the modified_type_die
10523        recursion will terminate even if the type is recursive.  Recursive
10524        types are possible in Ada.  */
10525     sub_die = modified_type_die (item_type,
10526 				 TYPE_READONLY (item_type),
10527 				 TYPE_VOLATILE (item_type),
10528 				 context_die);
10529 
10530   if (sub_die != NULL)
10531     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10532 
10533   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10534   if (TYPE_ARTIFICIAL (type))
10535     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10536 
10537   return mod_type_die;
10538 }
10539 
10540 /* Generate DIEs for the generic parameters of T.
10541    T must be either a generic type or a generic function.
10542    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10543 
10544 static void
gen_generic_params_dies(tree t)10545 gen_generic_params_dies (tree t)
10546 {
10547   tree parms, args;
10548   int parms_num, i;
10549   dw_die_ref die = NULL;
10550   int non_default;
10551 
10552   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10553     return;
10554 
10555   if (TYPE_P (t))
10556     die = lookup_type_die (t);
10557   else if (DECL_P (t))
10558     die = lookup_decl_die (t);
10559 
10560   gcc_assert (die);
10561 
10562   parms = lang_hooks.get_innermost_generic_parms (t);
10563   if (!parms)
10564     /* T has no generic parameter. It means T is neither a generic type
10565        or function. End of story.  */
10566     return;
10567 
10568   parms_num = TREE_VEC_LENGTH (parms);
10569   args = lang_hooks.get_innermost_generic_args (t);
10570   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10571     non_default = int_cst_value (TREE_CHAIN (args));
10572   else
10573     non_default = TREE_VEC_LENGTH (args);
10574   for (i = 0; i < parms_num; i++)
10575     {
10576       tree parm, arg, arg_pack_elems;
10577       dw_die_ref parm_die;
10578 
10579       parm = TREE_VEC_ELT (parms, i);
10580       arg = TREE_VEC_ELT (args, i);
10581       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10582       gcc_assert (parm && TREE_VALUE (parm) && arg);
10583 
10584       if (parm && TREE_VALUE (parm) && arg)
10585 	{
10586 	  /* If PARM represents a template parameter pack,
10587 	     emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10588 	     by DW_TAG_template_*_parameter DIEs for the argument
10589 	     pack elements of ARG. Note that ARG would then be
10590 	     an argument pack.  */
10591 	  if (arg_pack_elems)
10592 	    parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10593 						    arg_pack_elems,
10594 						    die);
10595 	  else
10596 	    parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10597 					      true /* emit name */, die);
10598 	  if (i >= non_default)
10599 	    add_AT_flag (parm_die, DW_AT_default_value, 1);
10600 	}
10601     }
10602 }
10603 
10604 /* Create and return a DIE for PARM which should be
10605    the representation of a generic type parameter.
10606    For instance, in the C++ front end, PARM would be a template parameter.
10607    ARG is the argument to PARM.
10608    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10609    name of the PARM.
10610    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10611    as a child node.  */
10612 
10613 static dw_die_ref
generic_parameter_die(tree parm,tree arg,bool emit_name_p,dw_die_ref parent_die)10614 generic_parameter_die (tree parm, tree arg,
10615 		       bool emit_name_p,
10616 		       dw_die_ref parent_die)
10617 {
10618   dw_die_ref tmpl_die = NULL;
10619   const char *name = NULL;
10620 
10621   if (!parm || !DECL_NAME (parm) || !arg)
10622     return NULL;
10623 
10624   /* We support non-type generic parameters and arguments,
10625      type generic parameters and arguments, as well as
10626      generic generic parameters (a.k.a. template template parameters in C++)
10627      and arguments.  */
10628   if (TREE_CODE (parm) == PARM_DECL)
10629     /* PARM is a nontype generic parameter  */
10630     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10631   else if (TREE_CODE (parm) == TYPE_DECL)
10632     /* PARM is a type generic parameter.  */
10633     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10634   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10635     /* PARM is a generic generic parameter.
10636        Its DIE is a GNU extension. It shall have a
10637        DW_AT_name attribute to represent the name of the template template
10638        parameter, and a DW_AT_GNU_template_name attribute to represent the
10639        name of the template template argument.  */
10640     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10641 			parent_die, parm);
10642   else
10643     gcc_unreachable ();
10644 
10645   if (tmpl_die)
10646     {
10647       tree tmpl_type;
10648 
10649       /* If PARM is a generic parameter pack, it means we are
10650          emitting debug info for a template argument pack element.
10651 	 In other terms, ARG is a template argument pack element.
10652 	 In that case, we don't emit any DW_AT_name attribute for
10653 	 the die.  */
10654       if (emit_name_p)
10655 	{
10656 	  name = IDENTIFIER_POINTER (DECL_NAME (parm));
10657 	  gcc_assert (name);
10658 	  add_AT_string (tmpl_die, DW_AT_name, name);
10659 	}
10660 
10661       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10662 	{
10663 	  /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10664 	     TMPL_DIE should have a child DW_AT_type attribute that is set
10665 	     to the type of the argument to PARM, which is ARG.
10666 	     If PARM is a type generic parameter, TMPL_DIE should have a
10667 	     child DW_AT_type that is set to ARG.  */
10668 	  tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10669 	  add_type_attribute (tmpl_die, tmpl_type, 0,
10670 			      TREE_THIS_VOLATILE (tmpl_type),
10671 			      parent_die);
10672 	}
10673       else
10674 	{
10675 	  /* So TMPL_DIE is a DIE representing a
10676 	     a generic generic template parameter, a.k.a template template
10677 	     parameter in C++ and arg is a template.  */
10678 
10679 	  /* The DW_AT_GNU_template_name attribute of the DIE must be set
10680 	     to the name of the argument.  */
10681 	  name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10682 	  if (name)
10683 	    add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10684 	}
10685 
10686       if (TREE_CODE (parm) == PARM_DECL)
10687 	/* So PARM is a non-type generic parameter.
10688 	   DWARF3 5.6.8 says we must set a DW_AT_const_value child
10689 	   attribute of TMPL_DIE which value represents the value
10690 	   of ARG.
10691 	   We must be careful here:
10692 	   The value of ARG might reference some function decls.
10693 	   We might currently be emitting debug info for a generic
10694 	   type and types are emitted before function decls, we don't
10695 	   know if the function decls referenced by ARG will actually be
10696 	   emitted after cgraph computations.
10697 	   So must defer the generation of the DW_AT_const_value to
10698 	   after cgraph is ready.  */
10699 	append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10700     }
10701 
10702   return tmpl_die;
10703 }
10704 
10705 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10706    PARM_PACK must be a template parameter pack. The returned DIE
10707    will be child DIE of PARENT_DIE.  */
10708 
10709 static dw_die_ref
template_parameter_pack_die(tree parm_pack,tree parm_pack_args,dw_die_ref parent_die)10710 template_parameter_pack_die (tree parm_pack,
10711 			     tree parm_pack_args,
10712 			     dw_die_ref parent_die)
10713 {
10714   dw_die_ref die;
10715   int j;
10716 
10717   gcc_assert (parent_die && parm_pack);
10718 
10719   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10720   add_name_and_src_coords_attributes (die, parm_pack);
10721   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10722     generic_parameter_die (parm_pack,
10723 			   TREE_VEC_ELT (parm_pack_args, j),
10724 			   false /* Don't emit DW_AT_name */,
10725 			   die);
10726   return die;
10727 }
10728 
10729 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10730    an enumerated type.  */
10731 
10732 static inline int
type_is_enum(const_tree type)10733 type_is_enum (const_tree type)
10734 {
10735   return TREE_CODE (type) == ENUMERAL_TYPE;
10736 }
10737 
10738 /* Return the DBX register number described by a given RTL node.  */
10739 
10740 static unsigned int
dbx_reg_number(const_rtx rtl)10741 dbx_reg_number (const_rtx rtl)
10742 {
10743   unsigned regno = REGNO (rtl);
10744 
10745   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10746 
10747 #ifdef LEAF_REG_REMAP
10748   if (crtl->uses_only_leaf_regs)
10749     {
10750       int leaf_reg = LEAF_REG_REMAP (regno);
10751       if (leaf_reg != -1)
10752 	regno = (unsigned) leaf_reg;
10753     }
10754 #endif
10755 
10756   regno = DBX_REGISTER_NUMBER (regno);
10757   gcc_assert (regno != INVALID_REGNUM);
10758   return regno;
10759 }
10760 
10761 /* Optionally add a DW_OP_piece term to a location description expression.
10762    DW_OP_piece is only added if the location description expression already
10763    doesn't end with DW_OP_piece.  */
10764 
10765 static void
add_loc_descr_op_piece(dw_loc_descr_ref * list_head,int size)10766 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10767 {
10768   dw_loc_descr_ref loc;
10769 
10770   if (*list_head != NULL)
10771     {
10772       /* Find the end of the chain.  */
10773       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10774 	;
10775 
10776       if (loc->dw_loc_opc != DW_OP_piece)
10777 	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10778     }
10779 }
10780 
10781 /* Return a location descriptor that designates a machine register or
10782    zero if there is none.  */
10783 
10784 static dw_loc_descr_ref
reg_loc_descriptor(rtx rtl,enum var_init_status initialized)10785 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10786 {
10787   rtx regs;
10788 
10789   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10790     return 0;
10791 
10792   /* We only use "frame base" when we're sure we're talking about the
10793      post-prologue local stack frame.  We do this by *not* running
10794      register elimination until this point, and recognizing the special
10795      argument pointer and soft frame pointer rtx's.
10796      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10797   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10798       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10799     {
10800       dw_loc_descr_ref result = NULL;
10801 
10802       if (dwarf_version >= 4 || !dwarf_strict)
10803 	{
10804 	  result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10805 				       initialized);
10806 	  if (result)
10807 	    add_loc_descr (&result,
10808 			   new_loc_descr (DW_OP_stack_value, 0, 0));
10809 	}
10810       return result;
10811     }
10812 
10813   regs = targetm.dwarf_register_span (rtl);
10814 
10815   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10816     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10817   else
10818     {
10819       unsigned int dbx_regnum = dbx_reg_number (rtl);
10820       if (dbx_regnum == IGNORED_DWARF_REGNUM)
10821 	return 0;
10822       return one_reg_loc_descriptor (dbx_regnum, initialized);
10823     }
10824 }
10825 
10826 /* Return a location descriptor that designates a machine register for
10827    a given hard register number.  */
10828 
10829 static dw_loc_descr_ref
one_reg_loc_descriptor(unsigned int regno,enum var_init_status initialized)10830 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10831 {
10832   dw_loc_descr_ref reg_loc_descr;
10833 
10834   if (regno <= 31)
10835     reg_loc_descr
10836       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10837   else
10838     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10839 
10840   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10841     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10842 
10843   return reg_loc_descr;
10844 }
10845 
10846 /* Given an RTL of a register, return a location descriptor that
10847    designates a value that spans more than one register.  */
10848 
10849 static dw_loc_descr_ref
multiple_reg_loc_descriptor(rtx rtl,rtx regs,enum var_init_status initialized)10850 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10851 			     enum var_init_status initialized)
10852 {
10853   int size, i;
10854   dw_loc_descr_ref loc_result = NULL;
10855 
10856   /* Simple, contiguous registers.  */
10857   if (regs == NULL_RTX)
10858     {
10859       unsigned reg = REGNO (rtl);
10860       int nregs;
10861 
10862 #ifdef LEAF_REG_REMAP
10863       if (crtl->uses_only_leaf_regs)
10864 	{
10865 	  int leaf_reg = LEAF_REG_REMAP (reg);
10866 	  if (leaf_reg != -1)
10867 	    reg = (unsigned) leaf_reg;
10868 	}
10869 #endif
10870 
10871       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10872       nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10873 
10874       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10875 
10876       loc_result = NULL;
10877       while (nregs--)
10878 	{
10879 	  dw_loc_descr_ref t;
10880 
10881 	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10882 				      VAR_INIT_STATUS_INITIALIZED);
10883 	  add_loc_descr (&loc_result, t);
10884 	  add_loc_descr_op_piece (&loc_result, size);
10885 	  ++reg;
10886 	}
10887       return loc_result;
10888     }
10889 
10890   /* Now onto stupid register sets in non contiguous locations.  */
10891 
10892   gcc_assert (GET_CODE (regs) == PARALLEL);
10893 
10894   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10895   loc_result = NULL;
10896 
10897   for (i = 0; i < XVECLEN (regs, 0); ++i)
10898     {
10899       dw_loc_descr_ref t;
10900 
10901       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10902 				  VAR_INIT_STATUS_INITIALIZED);
10903       add_loc_descr (&loc_result, t);
10904       add_loc_descr_op_piece (&loc_result, size);
10905     }
10906 
10907   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10908     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10909   return loc_result;
10910 }
10911 
10912 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10913 
10914 /* Return a location descriptor that designates a constant i,
10915    as a compound operation from constant (i >> shift), constant shift
10916    and DW_OP_shl.  */
10917 
10918 static dw_loc_descr_ref
int_shift_loc_descriptor(HOST_WIDE_INT i,int shift)10919 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10920 {
10921   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10922   add_loc_descr (&ret, int_loc_descriptor (shift));
10923   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10924   return ret;
10925 }
10926 
10927 /* Return a location descriptor that designates a constant.  */
10928 
10929 static dw_loc_descr_ref
int_loc_descriptor(HOST_WIDE_INT i)10930 int_loc_descriptor (HOST_WIDE_INT i)
10931 {
10932   enum dwarf_location_atom op;
10933 
10934   /* Pick the smallest representation of a constant, rather than just
10935      defaulting to the LEB encoding.  */
10936   if (i >= 0)
10937     {
10938       int clz = clz_hwi (i);
10939       int ctz = ctz_hwi (i);
10940       if (i <= 31)
10941 	op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10942       else if (i <= 0xff)
10943 	op = DW_OP_const1u;
10944       else if (i <= 0xffff)
10945 	op = DW_OP_const2u;
10946       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10947 	       && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10948 	/* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10949 	   DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10950 	   while DW_OP_const4u is 5 bytes.  */
10951 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10952       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10953 	       && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10954 	/* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10955 	   while DW_OP_const4u is 5 bytes.  */
10956 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10957       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10958 	op = DW_OP_const4u;
10959       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10960 	       && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10961 	/* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10962 	   while DW_OP_constu of constant >= 0x100000000 takes at least
10963 	   6 bytes.  */
10964 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10965       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10966 	       && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10967 		  >= HOST_BITS_PER_WIDE_INT)
10968 	/* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10969 	   DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10970 	   while DW_OP_constu takes in this case at least 6 bytes.  */
10971 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10972       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10973 	       && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10974 	       && size_of_uleb128 (i) > 6)
10975 	/* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10976 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10977       else
10978 	op = DW_OP_constu;
10979     }
10980   else
10981     {
10982       if (i >= -0x80)
10983 	op = DW_OP_const1s;
10984       else if (i >= -0x8000)
10985 	op = DW_OP_const2s;
10986       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10987 	{
10988 	  if (size_of_int_loc_descriptor (i) < 5)
10989 	    {
10990 	      dw_loc_descr_ref ret = int_loc_descriptor (-i);
10991 	      add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10992 	      return ret;
10993 	    }
10994 	  op = DW_OP_const4s;
10995 	}
10996       else
10997 	{
10998 	  if (size_of_int_loc_descriptor (i)
10999 	      < (unsigned long) 1 + size_of_sleb128 (i))
11000 	    {
11001 	      dw_loc_descr_ref ret = int_loc_descriptor (-i);
11002 	      add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11003 	      return ret;
11004 	    }
11005 	  op = DW_OP_consts;
11006 	}
11007     }
11008 
11009   return new_loc_descr (op, i, 0);
11010 }
11011 
11012 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11013    without actually allocating it.  */
11014 
11015 static unsigned long
size_of_int_shift_loc_descriptor(HOST_WIDE_INT i,int shift)11016 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11017 {
11018   return size_of_int_loc_descriptor (i >> shift)
11019 	 + size_of_int_loc_descriptor (shift)
11020 	 + 1;
11021 }
11022 
11023 /* Return size_of_locs (int_loc_descriptor (i)) without
11024    actually allocating it.  */
11025 
11026 static unsigned long
size_of_int_loc_descriptor(HOST_WIDE_INT i)11027 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11028 {
11029   unsigned long s;
11030 
11031   if (i >= 0)
11032     {
11033       int clz, ctz;
11034       if (i <= 31)
11035 	return 1;
11036       else if (i <= 0xff)
11037 	return 2;
11038       else if (i <= 0xffff)
11039 	return 3;
11040       clz = clz_hwi (i);
11041       ctz = ctz_hwi (i);
11042       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11043 	  && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11044 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11045 						    - clz - 5);
11046       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11047 	       && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11048 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11049 						    - clz - 8);
11050       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11051 	return 5;
11052       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11053       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11054 	  && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11055 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11056 						    - clz - 8);
11057       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11058 	       && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11059 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11060 						    - clz - 16);
11061       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11062 	       && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11063 	       && s > 6)
11064 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11065 						    - clz - 32);
11066       else
11067 	return 1 + s;
11068     }
11069   else
11070     {
11071       if (i >= -0x80)
11072 	return 2;
11073       else if (i >= -0x8000)
11074 	return 3;
11075       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11076 	{
11077 	  if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11078 	    {
11079 	      s = size_of_int_loc_descriptor (-i) + 1;
11080 	      if (s < 5)
11081 		return s;
11082 	    }
11083 	  return 5;
11084 	}
11085       else
11086 	{
11087 	  unsigned long r = 1 + size_of_sleb128 (i);
11088 	  if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11089 	    {
11090 	      s = size_of_int_loc_descriptor (-i) + 1;
11091 	      if (s < r)
11092 		return s;
11093 	    }
11094 	  return r;
11095 	}
11096     }
11097 }
11098 
11099 /* Return loc description representing "address" of integer value.
11100    This can appear only as toplevel expression.  */
11101 
11102 static dw_loc_descr_ref
address_of_int_loc_descriptor(int size,HOST_WIDE_INT i)11103 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11104 {
11105   int litsize;
11106   dw_loc_descr_ref loc_result = NULL;
11107 
11108   if (!(dwarf_version >= 4 || !dwarf_strict))
11109     return NULL;
11110 
11111   litsize = size_of_int_loc_descriptor (i);
11112   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11113      is more compact.  For DW_OP_stack_value we need:
11114      litsize + 1 (DW_OP_stack_value)
11115      and for DW_OP_implicit_value:
11116      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11117   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11118     {
11119       loc_result = int_loc_descriptor (i);
11120       add_loc_descr (&loc_result,
11121 		     new_loc_descr (DW_OP_stack_value, 0, 0));
11122       return loc_result;
11123     }
11124 
11125   loc_result = new_loc_descr (DW_OP_implicit_value,
11126 			      size, 0);
11127   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11128   loc_result->dw_loc_oprnd2.v.val_int = i;
11129   return loc_result;
11130 }
11131 
11132 /* Return a location descriptor that designates a base+offset location.  */
11133 
11134 static dw_loc_descr_ref
based_loc_descr(rtx reg,HOST_WIDE_INT offset,enum var_init_status initialized)11135 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11136 		 enum var_init_status initialized)
11137 {
11138   unsigned int regno;
11139   dw_loc_descr_ref result;
11140   dw_fde_ref fde = cfun->fde;
11141 
11142   /* We only use "frame base" when we're sure we're talking about the
11143      post-prologue local stack frame.  We do this by *not* running
11144      register elimination until this point, and recognizing the special
11145      argument pointer and soft frame pointer rtx's.  */
11146   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11147     {
11148       rtx elim = (ira_use_lra_p
11149 		  ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11150 		  : eliminate_regs (reg, VOIDmode, NULL_RTX));
11151 
11152       if (elim != reg)
11153 	{
11154 	  if (GET_CODE (elim) == PLUS)
11155 	    {
11156 	      offset += INTVAL (XEXP (elim, 1));
11157 	      elim = XEXP (elim, 0);
11158 	    }
11159 	  gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11160 		       && (elim == hard_frame_pointer_rtx
11161 			   || elim == stack_pointer_rtx))
11162 	              || elim == (frame_pointer_needed
11163 				  ? hard_frame_pointer_rtx
11164 				  : stack_pointer_rtx));
11165 
11166 	  /* If drap register is used to align stack, use frame
11167 	     pointer + offset to access stack variables.  If stack
11168 	     is aligned without drap, use stack pointer + offset to
11169 	     access stack variables.  */
11170 	  if (crtl->stack_realign_tried
11171 	      && reg == frame_pointer_rtx)
11172 	    {
11173 	      int base_reg
11174 		= DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11175 				      ? HARD_FRAME_POINTER_REGNUM
11176 				      : REGNO (elim));
11177 	      return new_reg_loc_descr (base_reg, offset);
11178 	    }
11179 
11180 	  gcc_assert (frame_pointer_fb_offset_valid);
11181 	  offset += frame_pointer_fb_offset;
11182 	  return new_loc_descr (DW_OP_fbreg, offset, 0);
11183 	}
11184     }
11185 
11186   regno = REGNO (reg);
11187 #ifdef LEAF_REG_REMAP
11188   if (crtl->uses_only_leaf_regs)
11189     {
11190       int leaf_reg = LEAF_REG_REMAP (regno);
11191       if (leaf_reg != -1)
11192 	regno = (unsigned) leaf_reg;
11193     }
11194 #endif
11195   regno = DWARF_FRAME_REGNUM (regno);
11196 
11197   if (!optimize && fde
11198       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11199     {
11200       /* Use cfa+offset to represent the location of arguments passed
11201 	 on the stack when drap is used to align stack.
11202 	 Only do this when not optimizing, for optimized code var-tracking
11203 	 is supposed to track where the arguments live and the register
11204 	 used as vdrap or drap in some spot might be used for something
11205 	 else in other part of the routine.  */
11206       return new_loc_descr (DW_OP_fbreg, offset, 0);
11207     }
11208 
11209   if (regno <= 31)
11210     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11211 			    offset, 0);
11212   else
11213     result = new_loc_descr (DW_OP_bregx, regno, offset);
11214 
11215   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11216     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11217 
11218   return result;
11219 }
11220 
11221 /* Return true if this RTL expression describes a base+offset calculation.  */
11222 
11223 static inline int
is_based_loc(const_rtx rtl)11224 is_based_loc (const_rtx rtl)
11225 {
11226   return (GET_CODE (rtl) == PLUS
11227 	  && ((REG_P (XEXP (rtl, 0))
11228 	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11229 	       && CONST_INT_P (XEXP (rtl, 1)))));
11230 }
11231 
11232 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11233    failed.  */
11234 
11235 static dw_loc_descr_ref
tls_mem_loc_descriptor(rtx mem)11236 tls_mem_loc_descriptor (rtx mem)
11237 {
11238   tree base;
11239   dw_loc_descr_ref loc_result;
11240 
11241   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11242     return NULL;
11243 
11244   base = get_base_address (MEM_EXPR (mem));
11245   if (base == NULL
11246       || TREE_CODE (base) != VAR_DECL
11247       || !DECL_THREAD_LOCAL_P (base))
11248     return NULL;
11249 
11250   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11251   if (loc_result == NULL)
11252     return NULL;
11253 
11254   if (MEM_OFFSET (mem))
11255     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11256 
11257   return loc_result;
11258 }
11259 
11260 /* Output debug info about reason why we failed to expand expression as dwarf
11261    expression.  */
11262 
11263 static void
expansion_failed(tree expr,rtx rtl,char const * reason)11264 expansion_failed (tree expr, rtx rtl, char const *reason)
11265 {
11266   if (dump_file && (dump_flags & TDF_DETAILS))
11267     {
11268       fprintf (dump_file, "Failed to expand as dwarf: ");
11269       if (expr)
11270 	print_generic_expr (dump_file, expr, dump_flags);
11271       if (rtl)
11272 	{
11273 	  fprintf (dump_file, "\n");
11274 	  print_rtl (dump_file, rtl);
11275 	}
11276       fprintf (dump_file, "\nReason: %s\n", reason);
11277     }
11278 }
11279 
11280 /* Helper function for const_ok_for_output, called either directly
11281    or via for_each_rtx.  */
11282 
11283 static int
const_ok_for_output_1(rtx * rtlp,void * data ATTRIBUTE_UNUSED)11284 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11285 {
11286   rtx rtl = *rtlp;
11287 
11288   if (GET_CODE (rtl) == UNSPEC)
11289     {
11290       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11291 	 we can't express it in the debug info.  */
11292 #ifdef ENABLE_CHECKING
11293       /* Don't complain about TLS UNSPECs, those are just too hard to
11294 	 delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11295 	 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11296 	 rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11297       if (XVECLEN (rtl, 0) == 0
11298 	  || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11299 	  || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11300 	inform (current_function_decl
11301 		? DECL_SOURCE_LOCATION (current_function_decl)
11302 		: UNKNOWN_LOCATION,
11303 #if NUM_UNSPEC_VALUES > 0
11304 		"non-delegitimized UNSPEC %s (%d) found in variable location",
11305 		((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11306 		 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11307 		XINT (rtl, 1));
11308 #else
11309 		"non-delegitimized UNSPEC %d found in variable location",
11310 		XINT (rtl, 1));
11311 #endif
11312 #endif
11313       expansion_failed (NULL_TREE, rtl,
11314 			"UNSPEC hasn't been delegitimized.\n");
11315       return 1;
11316     }
11317 
11318   if (targetm.const_not_ok_for_debug_p (rtl))
11319     {
11320       expansion_failed (NULL_TREE, rtl,
11321 			"Expression rejected for debug by the backend.\n");
11322       return 1;
11323     }
11324 
11325   /* FIXME: Refer to PR60655. It is possible for simplification
11326      of rtl expressions in var tracking to produce such expressions.
11327      We should really identify / validate expressions
11328      enclosed in CONST that can be handled by assemblers on various
11329      targets and only handle legitimate cases here.  */
11330   if (GET_CODE (rtl) != SYMBOL_REF)
11331     {
11332       if (GET_CODE (rtl) == NOT)
11333 	  return 1;
11334 
11335       return 0;
11336     }
11337 
11338   if (CONSTANT_POOL_ADDRESS_P (rtl))
11339     {
11340       bool marked;
11341       get_pool_constant_mark (rtl, &marked);
11342       /* If all references to this pool constant were optimized away,
11343 	 it was not output and thus we can't represent it.  */
11344       if (!marked)
11345 	{
11346 	  expansion_failed (NULL_TREE, rtl,
11347 			    "Constant was removed from constant pool.\n");
11348 	  return 1;
11349 	}
11350     }
11351 
11352   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11353     return 1;
11354 
11355   /* Avoid references to external symbols in debug info, on several targets
11356      the linker might even refuse to link when linking a shared library,
11357      and in many other cases the relocations for .debug_info/.debug_loc are
11358      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11359      to be defined within the same shared library or executable are fine.  */
11360   if (SYMBOL_REF_EXTERNAL_P (rtl))
11361     {
11362       tree decl = SYMBOL_REF_DECL (rtl);
11363 
11364       if (decl == NULL || !targetm.binds_local_p (decl))
11365 	{
11366 	  expansion_failed (NULL_TREE, rtl,
11367 			    "Symbol not defined in current TU.\n");
11368 	  return 1;
11369 	}
11370     }
11371 
11372   return 0;
11373 }
11374 
11375 /* Return true if constant RTL can be emitted in DW_OP_addr or
11376    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11377    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11378 
11379 static bool
const_ok_for_output(rtx rtl)11380 const_ok_for_output (rtx rtl)
11381 {
11382   if (GET_CODE (rtl) == SYMBOL_REF)
11383     return const_ok_for_output_1 (&rtl, NULL) == 0;
11384 
11385   if (GET_CODE (rtl) == CONST)
11386     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11387 
11388   return true;
11389 }
11390 
11391 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11392    if possible, NULL otherwise.  */
11393 
11394 static dw_die_ref
base_type_for_mode(enum machine_mode mode,bool unsignedp)11395 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11396 {
11397   dw_die_ref type_die;
11398   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11399 
11400   if (type == NULL)
11401     return NULL;
11402   switch (TREE_CODE (type))
11403     {
11404     case INTEGER_TYPE:
11405     case REAL_TYPE:
11406       break;
11407     default:
11408       return NULL;
11409     }
11410   type_die = lookup_type_die (type);
11411   if (!type_die)
11412     type_die = modified_type_die (type, false, false, comp_unit_die ());
11413   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11414     return NULL;
11415   return type_die;
11416 }
11417 
11418 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11419    type matching MODE, or, if MODE is narrower than or as wide as
11420    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11421    possible.  */
11422 
11423 static dw_loc_descr_ref
convert_descriptor_to_mode(enum machine_mode mode,dw_loc_descr_ref op)11424 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11425 {
11426   enum machine_mode outer_mode = mode;
11427   dw_die_ref type_die;
11428   dw_loc_descr_ref cvt;
11429 
11430   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11431     {
11432       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11433       return op;
11434     }
11435   type_die = base_type_for_mode (outer_mode, 1);
11436   if (type_die == NULL)
11437     return NULL;
11438   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11439   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11440   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11441   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11442   add_loc_descr (&op, cvt);
11443   return op;
11444 }
11445 
11446 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11447 
11448 static dw_loc_descr_ref
compare_loc_descriptor(enum dwarf_location_atom op,dw_loc_descr_ref op0,dw_loc_descr_ref op1)11449 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11450 			dw_loc_descr_ref op1)
11451 {
11452   dw_loc_descr_ref ret = op0;
11453   add_loc_descr (&ret, op1);
11454   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11455   if (STORE_FLAG_VALUE != 1)
11456     {
11457       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11458       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11459     }
11460   return ret;
11461 }
11462 
11463 /* Return location descriptor for signed comparison OP RTL.  */
11464 
11465 static dw_loc_descr_ref
scompare_loc_descriptor(enum dwarf_location_atom op,rtx rtl,enum machine_mode mem_mode)11466 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11467 			 enum machine_mode mem_mode)
11468 {
11469   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11470   dw_loc_descr_ref op0, op1;
11471   int shift;
11472 
11473   if (op_mode == VOIDmode)
11474     op_mode = GET_MODE (XEXP (rtl, 1));
11475   if (op_mode == VOIDmode)
11476     return NULL;
11477 
11478   if (dwarf_strict
11479       && (GET_MODE_CLASS (op_mode) != MODE_INT
11480 	  || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11481     return NULL;
11482 
11483   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11484 			    VAR_INIT_STATUS_INITIALIZED);
11485   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11486 			    VAR_INIT_STATUS_INITIALIZED);
11487 
11488   if (op0 == NULL || op1 == NULL)
11489     return NULL;
11490 
11491   if (GET_MODE_CLASS (op_mode) != MODE_INT
11492       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11493     return compare_loc_descriptor (op, op0, op1);
11494 
11495   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11496     {
11497       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11498       dw_loc_descr_ref cvt;
11499 
11500       if (type_die == NULL)
11501 	return NULL;
11502       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11503       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11504       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11505       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11506       add_loc_descr (&op0, cvt);
11507       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11508       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11509       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11510       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11511       add_loc_descr (&op1, cvt);
11512       return compare_loc_descriptor (op, op0, op1);
11513     }
11514 
11515   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11516   /* For eq/ne, if the operands are known to be zero-extended,
11517      there is no need to do the fancy shifting up.  */
11518   if (op == DW_OP_eq || op == DW_OP_ne)
11519     {
11520       dw_loc_descr_ref last0, last1;
11521       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11522 	;
11523       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11524 	;
11525       /* deref_size zero extends, and for constants we can check
11526 	 whether they are zero extended or not.  */
11527       if (((last0->dw_loc_opc == DW_OP_deref_size
11528 	    && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11529 	   || (CONST_INT_P (XEXP (rtl, 0))
11530 	       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11531 		  == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11532 	  && ((last1->dw_loc_opc == DW_OP_deref_size
11533 	       && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11534 	      || (CONST_INT_P (XEXP (rtl, 1))
11535 		  && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11536 		     == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11537 	return compare_loc_descriptor (op, op0, op1);
11538 
11539       /* EQ/NE comparison against constant in narrower type than
11540 	 DWARF2_ADDR_SIZE can be performed either as
11541 	 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11542 	 DW_OP_{eq,ne}
11543 	 or
11544 	 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11545 	 DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11546       if (CONST_INT_P (XEXP (rtl, 1))
11547 	  && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11548 	  && (size_of_int_loc_descriptor (shift) + 1
11549 	      + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11550 	      >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11551 		 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11552 					       & GET_MODE_MASK (op_mode))))
11553 	{
11554 	  add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11555 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11556 	  op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11557 				    & GET_MODE_MASK (op_mode));
11558 	  return compare_loc_descriptor (op, op0, op1);
11559 	}
11560     }
11561   add_loc_descr (&op0, int_loc_descriptor (shift));
11562   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11563   if (CONST_INT_P (XEXP (rtl, 1)))
11564     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11565   else
11566     {
11567       add_loc_descr (&op1, int_loc_descriptor (shift));
11568       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11569     }
11570   return compare_loc_descriptor (op, op0, op1);
11571 }
11572 
11573 /* Return location descriptor for unsigned comparison OP RTL.  */
11574 
11575 static dw_loc_descr_ref
ucompare_loc_descriptor(enum dwarf_location_atom op,rtx rtl,enum machine_mode mem_mode)11576 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11577 			 enum machine_mode mem_mode)
11578 {
11579   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11580   dw_loc_descr_ref op0, op1;
11581 
11582   if (op_mode == VOIDmode)
11583     op_mode = GET_MODE (XEXP (rtl, 1));
11584   if (op_mode == VOIDmode)
11585     return NULL;
11586   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11587     return NULL;
11588 
11589   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11590     return NULL;
11591 
11592   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11593 			    VAR_INIT_STATUS_INITIALIZED);
11594   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11595 			    VAR_INIT_STATUS_INITIALIZED);
11596 
11597   if (op0 == NULL || op1 == NULL)
11598     return NULL;
11599 
11600   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11601     {
11602       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11603       dw_loc_descr_ref last0, last1;
11604       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11605 	;
11606       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11607 	;
11608       if (CONST_INT_P (XEXP (rtl, 0)))
11609 	op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11610       /* deref_size zero extends, so no need to mask it again.  */
11611       else if (last0->dw_loc_opc != DW_OP_deref_size
11612 	       || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11613 	{
11614 	  add_loc_descr (&op0, int_loc_descriptor (mask));
11615 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11616 	}
11617       if (CONST_INT_P (XEXP (rtl, 1)))
11618 	op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11619       /* deref_size zero extends, so no need to mask it again.  */
11620       else if (last1->dw_loc_opc != DW_OP_deref_size
11621 	       || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11622 	{
11623 	  add_loc_descr (&op1, int_loc_descriptor (mask));
11624 	  add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11625 	}
11626     }
11627   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11628     {
11629       HOST_WIDE_INT bias = 1;
11630       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11631       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11632       if (CONST_INT_P (XEXP (rtl, 1)))
11633 	op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11634 				  + INTVAL (XEXP (rtl, 1)));
11635       else
11636 	add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11637 					    bias, 0));
11638     }
11639   return compare_loc_descriptor (op, op0, op1);
11640 }
11641 
11642 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11643 
11644 static dw_loc_descr_ref
minmax_loc_descriptor(rtx rtl,enum machine_mode mode,enum machine_mode mem_mode)11645 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11646 		       enum machine_mode mem_mode)
11647 {
11648   enum dwarf_location_atom op;
11649   dw_loc_descr_ref op0, op1, ret;
11650   dw_loc_descr_ref bra_node, drop_node;
11651 
11652   if (dwarf_strict
11653       && (GET_MODE_CLASS (mode) != MODE_INT
11654 	  || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11655     return NULL;
11656 
11657   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11658 			    VAR_INIT_STATUS_INITIALIZED);
11659   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11660 			    VAR_INIT_STATUS_INITIALIZED);
11661 
11662   if (op0 == NULL || op1 == NULL)
11663     return NULL;
11664 
11665   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11666   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11667   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11668   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11669     {
11670       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11671 	{
11672 	  HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11673 	  add_loc_descr (&op0, int_loc_descriptor (mask));
11674 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11675 	  add_loc_descr (&op1, int_loc_descriptor (mask));
11676 	  add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11677 	}
11678       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11679 	{
11680 	  HOST_WIDE_INT bias = 1;
11681 	  bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11682 	  add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11683 	  add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11684 	}
11685     }
11686   else if (GET_MODE_CLASS (mode) == MODE_INT
11687 	   && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11688     {
11689       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11690       add_loc_descr (&op0, int_loc_descriptor (shift));
11691       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11692       add_loc_descr (&op1, int_loc_descriptor (shift));
11693       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11694     }
11695   else if (GET_MODE_CLASS (mode) == MODE_INT
11696 	   && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11697     {
11698       dw_die_ref type_die = base_type_for_mode (mode, 0);
11699       dw_loc_descr_ref cvt;
11700       if (type_die == NULL)
11701 	return NULL;
11702       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11703       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11704       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11705       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11706       add_loc_descr (&op0, cvt);
11707       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11708       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11709       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11710       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11711       add_loc_descr (&op1, cvt);
11712     }
11713 
11714   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11715     op = DW_OP_lt;
11716   else
11717     op = DW_OP_gt;
11718   ret = op0;
11719   add_loc_descr (&ret, op1);
11720   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11721   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11722   add_loc_descr (&ret, bra_node);
11723   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11724   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11725   add_loc_descr (&ret, drop_node);
11726   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11727   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11728   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11729       && GET_MODE_CLASS (mode) == MODE_INT
11730       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11731     ret = convert_descriptor_to_mode (mode, ret);
11732   return ret;
11733 }
11734 
11735 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11736    but after converting arguments to type_die, afterwards
11737    convert back to unsigned.  */
11738 
11739 static dw_loc_descr_ref
typed_binop(enum dwarf_location_atom op,rtx rtl,dw_die_ref type_die,enum machine_mode mode,enum machine_mode mem_mode)11740 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11741 	     enum machine_mode mode, enum machine_mode mem_mode)
11742 {
11743   dw_loc_descr_ref cvt, op0, op1;
11744 
11745   if (type_die == NULL)
11746     return NULL;
11747   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11748 			    VAR_INIT_STATUS_INITIALIZED);
11749   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11750 			    VAR_INIT_STATUS_INITIALIZED);
11751   if (op0 == NULL || op1 == NULL)
11752     return NULL;
11753   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11754   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11755   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11756   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11757   add_loc_descr (&op0, cvt);
11758   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11759   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11760   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11761   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11762   add_loc_descr (&op1, cvt);
11763   add_loc_descr (&op0, op1);
11764   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11765   return convert_descriptor_to_mode (mode, op0);
11766 }
11767 
11768 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11769    const0 is DW_OP_lit0 or corresponding typed constant,
11770    const1 is DW_OP_lit1 or corresponding typed constant
11771    and constMSB is constant with just the MSB bit set
11772    for the mode):
11773        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11774    L1: const0 DW_OP_swap
11775    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11776        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11777    L3: DW_OP_drop
11778    L4: DW_OP_nop
11779 
11780    CTZ is similar:
11781        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11782    L1: const0 DW_OP_swap
11783    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11784        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11785    L3: DW_OP_drop
11786    L4: DW_OP_nop
11787 
11788    FFS is similar:
11789        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11790    L1: const1 DW_OP_swap
11791    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11792        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11793    L3: DW_OP_drop
11794    L4: DW_OP_nop  */
11795 
11796 static dw_loc_descr_ref
clz_loc_descriptor(rtx rtl,enum machine_mode mode,enum machine_mode mem_mode)11797 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11798 		    enum machine_mode mem_mode)
11799 {
11800   dw_loc_descr_ref op0, ret, tmp;
11801   HOST_WIDE_INT valv;
11802   dw_loc_descr_ref l1jump, l1label;
11803   dw_loc_descr_ref l2jump, l2label;
11804   dw_loc_descr_ref l3jump, l3label;
11805   dw_loc_descr_ref l4jump, l4label;
11806   rtx msb;
11807 
11808   if (GET_MODE_CLASS (mode) != MODE_INT
11809       || GET_MODE (XEXP (rtl, 0)) != mode
11810       || (GET_CODE (rtl) == CLZ
11811 	  && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11812     return NULL;
11813 
11814   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11815 			    VAR_INIT_STATUS_INITIALIZED);
11816   if (op0 == NULL)
11817     return NULL;
11818   ret = op0;
11819   if (GET_CODE (rtl) == CLZ)
11820     {
11821       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11822 	valv = GET_MODE_BITSIZE (mode);
11823     }
11824   else if (GET_CODE (rtl) == FFS)
11825     valv = 0;
11826   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11827     valv = GET_MODE_BITSIZE (mode);
11828   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11829   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11830   add_loc_descr (&ret, l1jump);
11831   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11832   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11833 			    VAR_INIT_STATUS_INITIALIZED);
11834   if (tmp == NULL)
11835     return NULL;
11836   add_loc_descr (&ret, tmp);
11837   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11838   add_loc_descr (&ret, l4jump);
11839   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11840 				? const1_rtx : const0_rtx,
11841 				mode, mem_mode,
11842 				VAR_INIT_STATUS_INITIALIZED);
11843   if (l1label == NULL)
11844     return NULL;
11845   add_loc_descr (&ret, l1label);
11846   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11847   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11848   add_loc_descr (&ret, l2label);
11849   if (GET_CODE (rtl) != CLZ)
11850     msb = const1_rtx;
11851   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11852     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11853 		   << (GET_MODE_BITSIZE (mode) - 1));
11854   else
11855     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11856 				  << (GET_MODE_BITSIZE (mode)
11857 				      - HOST_BITS_PER_WIDE_INT - 1), mode);
11858   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11859     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11860 			 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11861 			 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11862   else
11863     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11864 			      VAR_INIT_STATUS_INITIALIZED);
11865   if (tmp == NULL)
11866     return NULL;
11867   add_loc_descr (&ret, tmp);
11868   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11869   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11870   add_loc_descr (&ret, l3jump);
11871   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11872 			    VAR_INIT_STATUS_INITIALIZED);
11873   if (tmp == NULL)
11874     return NULL;
11875   add_loc_descr (&ret, tmp);
11876   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11877 				      ? DW_OP_shl : DW_OP_shr, 0, 0));
11878   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11879   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11880   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11881   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11882   add_loc_descr (&ret, l2jump);
11883   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11884   add_loc_descr (&ret, l3label);
11885   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11886   add_loc_descr (&ret, l4label);
11887   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11888   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11889   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11890   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11891   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11892   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11893   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11894   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11895   return ret;
11896 }
11897 
11898 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11899    const1 is DW_OP_lit1 or corresponding typed constant):
11900        const0 DW_OP_swap
11901    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11902        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11903    L2: DW_OP_drop
11904 
11905    PARITY is similar:
11906    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11907        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11908    L2: DW_OP_drop  */
11909 
11910 static dw_loc_descr_ref
popcount_loc_descriptor(rtx rtl,enum machine_mode mode,enum machine_mode mem_mode)11911 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11912 			 enum machine_mode mem_mode)
11913 {
11914   dw_loc_descr_ref op0, ret, tmp;
11915   dw_loc_descr_ref l1jump, l1label;
11916   dw_loc_descr_ref l2jump, l2label;
11917 
11918   if (GET_MODE_CLASS (mode) != MODE_INT
11919       || GET_MODE (XEXP (rtl, 0)) != mode)
11920     return NULL;
11921 
11922   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11923 			    VAR_INIT_STATUS_INITIALIZED);
11924   if (op0 == NULL)
11925     return NULL;
11926   ret = op0;
11927   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11928 			    VAR_INIT_STATUS_INITIALIZED);
11929   if (tmp == NULL)
11930     return NULL;
11931   add_loc_descr (&ret, tmp);
11932   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11933   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11934   add_loc_descr (&ret, l1label);
11935   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11936   add_loc_descr (&ret, l2jump);
11937   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11938   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11939   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11940 			    VAR_INIT_STATUS_INITIALIZED);
11941   if (tmp == NULL)
11942     return NULL;
11943   add_loc_descr (&ret, tmp);
11944   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11945   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11946 				      ? DW_OP_plus : DW_OP_xor, 0, 0));
11947   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11948   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11949 			    VAR_INIT_STATUS_INITIALIZED);
11950   add_loc_descr (&ret, tmp);
11951   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11952   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11953   add_loc_descr (&ret, l1jump);
11954   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11955   add_loc_descr (&ret, l2label);
11956   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11957   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11958   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11959   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11960   return ret;
11961 }
11962 
11963 /* BSWAP (constS is initial shift count, either 56 or 24):
11964        constS const0
11965    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11966        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11967        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11968        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11969    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11970 
11971 static dw_loc_descr_ref
bswap_loc_descriptor(rtx rtl,enum machine_mode mode,enum machine_mode mem_mode)11972 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11973 		      enum machine_mode mem_mode)
11974 {
11975   dw_loc_descr_ref op0, ret, tmp;
11976   dw_loc_descr_ref l1jump, l1label;
11977   dw_loc_descr_ref l2jump, l2label;
11978 
11979   if (GET_MODE_CLASS (mode) != MODE_INT
11980       || BITS_PER_UNIT != 8
11981       || (GET_MODE_BITSIZE (mode) != 32
11982 	  &&  GET_MODE_BITSIZE (mode) != 64))
11983     return NULL;
11984 
11985   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11986 			    VAR_INIT_STATUS_INITIALIZED);
11987   if (op0 == NULL)
11988     return NULL;
11989 
11990   ret = op0;
11991   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11992 			    mode, mem_mode,
11993 			    VAR_INIT_STATUS_INITIALIZED);
11994   if (tmp == NULL)
11995     return NULL;
11996   add_loc_descr (&ret, tmp);
11997   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11998 			    VAR_INIT_STATUS_INITIALIZED);
11999   if (tmp == NULL)
12000     return NULL;
12001   add_loc_descr (&ret, tmp);
12002   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12003   add_loc_descr (&ret, l1label);
12004   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12005 			    mode, mem_mode,
12006 			    VAR_INIT_STATUS_INITIALIZED);
12007   add_loc_descr (&ret, tmp);
12008   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12009   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12010   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12011   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12012 			    VAR_INIT_STATUS_INITIALIZED);
12013   if (tmp == NULL)
12014     return NULL;
12015   add_loc_descr (&ret, tmp);
12016   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12017   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12018   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12019   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12020   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12021   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12022   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12023 			    VAR_INIT_STATUS_INITIALIZED);
12024   add_loc_descr (&ret, tmp);
12025   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12026   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12027   add_loc_descr (&ret, l2jump);
12028   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12029 			    VAR_INIT_STATUS_INITIALIZED);
12030   add_loc_descr (&ret, tmp);
12031   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12032   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12033   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12034   add_loc_descr (&ret, l1jump);
12035   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12036   add_loc_descr (&ret, l2label);
12037   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12038   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12039   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12040   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12041   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12042   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12043   return ret;
12044 }
12045 
12046 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12047    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12048    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12049    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12050 
12051    ROTATERT is similar:
12052    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12053    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12054    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12055 
12056 static dw_loc_descr_ref
rotate_loc_descriptor(rtx rtl,enum machine_mode mode,enum machine_mode mem_mode)12057 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12058 		       enum machine_mode mem_mode)
12059 {
12060   rtx rtlop1 = XEXP (rtl, 1);
12061   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12062   int i;
12063 
12064   if (GET_MODE_CLASS (mode) != MODE_INT)
12065     return NULL;
12066 
12067   if (GET_MODE (rtlop1) != VOIDmode
12068       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12069     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12070   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12071 			    VAR_INIT_STATUS_INITIALIZED);
12072   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12073 			    VAR_INIT_STATUS_INITIALIZED);
12074   if (op0 == NULL || op1 == NULL)
12075     return NULL;
12076   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12077     for (i = 0; i < 2; i++)
12078       {
12079 	if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12080 	  mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12081 					mode, mem_mode,
12082 					VAR_INIT_STATUS_INITIALIZED);
12083 	else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12084 	  mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12085 				   ? DW_OP_const4u
12086 				   : HOST_BITS_PER_WIDE_INT == 64
12087 				   ? DW_OP_const8u : DW_OP_constu,
12088 				   GET_MODE_MASK (mode), 0);
12089 	else
12090 	  mask[i] = NULL;
12091 	if (mask[i] == NULL)
12092 	  return NULL;
12093 	add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12094       }
12095   ret = op0;
12096   add_loc_descr (&ret, op1);
12097   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12098   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12099   if (GET_CODE (rtl) == ROTATERT)
12100     {
12101       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12102       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12103 					  GET_MODE_BITSIZE (mode), 0));
12104     }
12105   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12106   if (mask[0] != NULL)
12107     add_loc_descr (&ret, mask[0]);
12108   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12109   if (mask[1] != NULL)
12110     {
12111       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12112       add_loc_descr (&ret, mask[1]);
12113       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12114     }
12115   if (GET_CODE (rtl) == ROTATE)
12116     {
12117       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12118       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12119 					  GET_MODE_BITSIZE (mode), 0));
12120     }
12121   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12122   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12123   return ret;
12124 }
12125 
12126 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12127    for DEBUG_PARAMETER_REF RTL.  */
12128 
12129 static dw_loc_descr_ref
parameter_ref_descriptor(rtx rtl)12130 parameter_ref_descriptor (rtx rtl)
12131 {
12132   dw_loc_descr_ref ret;
12133   dw_die_ref ref;
12134 
12135   if (dwarf_strict)
12136     return NULL;
12137   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12138   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12139   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12140   if (ref)
12141     {
12142       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12143       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12144       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12145     }
12146   else
12147     {
12148       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12149       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12150     }
12151   return ret;
12152 }
12153 
12154 /* The following routine converts the RTL for a variable or parameter
12155    (resident in memory) into an equivalent Dwarf representation of a
12156    mechanism for getting the address of that same variable onto the top of a
12157    hypothetical "address evaluation" stack.
12158 
12159    When creating memory location descriptors, we are effectively transforming
12160    the RTL for a memory-resident object into its Dwarf postfix expression
12161    equivalent.  This routine recursively descends an RTL tree, turning
12162    it into Dwarf postfix code as it goes.
12163 
12164    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12165 
12166    MEM_MODE is the mode of the memory reference, needed to handle some
12167    autoincrement addressing modes.
12168 
12169    Return 0 if we can't represent the location.  */
12170 
12171 dw_loc_descr_ref
mem_loc_descriptor(rtx rtl,enum machine_mode mode,enum machine_mode mem_mode,enum var_init_status initialized)12172 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12173 		    enum machine_mode mem_mode,
12174 		    enum var_init_status initialized)
12175 {
12176   dw_loc_descr_ref mem_loc_result = NULL;
12177   enum dwarf_location_atom op;
12178   dw_loc_descr_ref op0, op1;
12179   rtx inner = NULL_RTX;
12180 
12181   if (mode == VOIDmode)
12182     mode = GET_MODE (rtl);
12183 
12184   /* Note that for a dynamically sized array, the location we will generate a
12185      description of here will be the lowest numbered location which is
12186      actually within the array.  That's *not* necessarily the same as the
12187      zeroth element of the array.  */
12188 
12189   rtl = targetm.delegitimize_address (rtl);
12190 
12191   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12192     return NULL;
12193 
12194   switch (GET_CODE (rtl))
12195     {
12196     case POST_INC:
12197     case POST_DEC:
12198     case POST_MODIFY:
12199       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12200 
12201     case SUBREG:
12202       /* The case of a subreg may arise when we have a local (register)
12203 	 variable or a formal (register) parameter which doesn't quite fill
12204 	 up an entire register.  For now, just assume that it is
12205 	 legitimate to make the Dwarf info refer to the whole register which
12206 	 contains the given subreg.  */
12207       if (!subreg_lowpart_p (rtl))
12208 	break;
12209       inner = SUBREG_REG (rtl);
12210     case TRUNCATE:
12211       if (inner == NULL_RTX)
12212         inner = XEXP (rtl, 0);
12213       if (GET_MODE_CLASS (mode) == MODE_INT
12214 	  && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12215 	  && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12216 #ifdef POINTERS_EXTEND_UNSIGNED
12217 	      || (mode == Pmode && mem_mode != VOIDmode)
12218 #endif
12219 	     )
12220 	  && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12221 	{
12222 	  mem_loc_result = mem_loc_descriptor (inner,
12223 					       GET_MODE (inner),
12224 					       mem_mode, initialized);
12225 	  break;
12226 	}
12227       if (dwarf_strict)
12228 	break;
12229       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12230 	break;
12231       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12232 	  && (GET_MODE_CLASS (mode) != MODE_INT
12233 	      || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12234 	break;
12235       else
12236 	{
12237 	  dw_die_ref type_die;
12238 	  dw_loc_descr_ref cvt;
12239 
12240 	  mem_loc_result = mem_loc_descriptor (inner,
12241 					       GET_MODE (inner),
12242 					       mem_mode, initialized);
12243 	  if (mem_loc_result == NULL)
12244 	    break;
12245 	  type_die = base_type_for_mode (mode,
12246 					 GET_MODE_CLASS (mode) == MODE_INT);
12247 	  if (type_die == NULL)
12248 	    {
12249 	      mem_loc_result = NULL;
12250 	      break;
12251 	    }
12252 	  if (GET_MODE_SIZE (mode)
12253 	      != GET_MODE_SIZE (GET_MODE (inner)))
12254 	    cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12255 	  else
12256 	    cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12257 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12258 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12259 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12260 	  add_loc_descr (&mem_loc_result, cvt);
12261 	}
12262       break;
12263 
12264     case REG:
12265       if (GET_MODE_CLASS (mode) != MODE_INT
12266 	  || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12267 	      && rtl != arg_pointer_rtx
12268 	      && rtl != frame_pointer_rtx
12269 #ifdef POINTERS_EXTEND_UNSIGNED
12270 	      && (mode != Pmode || mem_mode == VOIDmode)
12271 #endif
12272 	      ))
12273 	{
12274 	  dw_die_ref type_die;
12275 	  unsigned int dbx_regnum;
12276 
12277 	  if (dwarf_strict)
12278 	    break;
12279 	  if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12280 	    break;
12281 	  type_die = base_type_for_mode (mode,
12282 					 GET_MODE_CLASS (mode) == MODE_INT);
12283 	  if (type_die == NULL)
12284 	    break;
12285 
12286 	  dbx_regnum = dbx_reg_number (rtl);
12287 	  if (dbx_regnum == IGNORED_DWARF_REGNUM)
12288 	    break;
12289 	  mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12290 					  dbx_regnum, 0);
12291 	  mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12292 	  mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12293 	  mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12294 	  break;
12295 	}
12296       /* Whenever a register number forms a part of the description of the
12297 	 method for calculating the (dynamic) address of a memory resident
12298 	 object, DWARF rules require the register number be referred to as
12299 	 a "base register".  This distinction is not based in any way upon
12300 	 what category of register the hardware believes the given register
12301 	 belongs to.  This is strictly DWARF terminology we're dealing with
12302 	 here. Note that in cases where the location of a memory-resident
12303 	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12304 	 OP_CONST (0)) the actual DWARF location descriptor that we generate
12305 	 may just be OP_BASEREG (basereg).  This may look deceptively like
12306 	 the object in question was allocated to a register (rather than in
12307 	 memory) so DWARF consumers need to be aware of the subtle
12308 	 distinction between OP_REG and OP_BASEREG.  */
12309       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12310 	mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12311       else if (stack_realign_drap
12312 	       && crtl->drap_reg
12313 	       && crtl->args.internal_arg_pointer == rtl
12314 	       && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12315 	{
12316 	  /* If RTL is internal_arg_pointer, which has been optimized
12317 	     out, use DRAP instead.  */
12318 	  mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12319 					    VAR_INIT_STATUS_INITIALIZED);
12320 	}
12321       break;
12322 
12323     case SIGN_EXTEND:
12324     case ZERO_EXTEND:
12325       if (GET_MODE_CLASS (mode) != MODE_INT)
12326 	break;
12327       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12328 				mem_mode, VAR_INIT_STATUS_INITIALIZED);
12329       if (op0 == 0)
12330 	break;
12331       else if (GET_CODE (rtl) == ZERO_EXTEND
12332 	       && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12333 	       && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12334 		  < HOST_BITS_PER_WIDE_INT
12335 	       /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12336 		  to expand zero extend as two shifts instead of
12337 		  masking.  */
12338 	       && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12339 	{
12340 	  enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12341 	  mem_loc_result = op0;
12342 	  add_loc_descr (&mem_loc_result,
12343 			 int_loc_descriptor (GET_MODE_MASK (imode)));
12344 	  add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12345 	}
12346       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12347 	{
12348 	  int shift = DWARF2_ADDR_SIZE
12349 		      - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12350 	  shift *= BITS_PER_UNIT;
12351 	  if (GET_CODE (rtl) == SIGN_EXTEND)
12352 	    op = DW_OP_shra;
12353 	  else
12354 	    op = DW_OP_shr;
12355 	  mem_loc_result = op0;
12356 	  add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12357 	  add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12358 	  add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12359 	  add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12360 	}
12361       else if (!dwarf_strict)
12362 	{
12363 	  dw_die_ref type_die1, type_die2;
12364 	  dw_loc_descr_ref cvt;
12365 
12366 	  type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12367 					  GET_CODE (rtl) == ZERO_EXTEND);
12368 	  if (type_die1 == NULL)
12369 	    break;
12370 	  type_die2 = base_type_for_mode (mode, 1);
12371 	  if (type_die2 == NULL)
12372 	    break;
12373 	  mem_loc_result = op0;
12374 	  cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12375 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12376 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12377 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12378 	  add_loc_descr (&mem_loc_result, cvt);
12379 	  cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12380 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12381 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12382 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12383 	  add_loc_descr (&mem_loc_result, cvt);
12384 	}
12385       break;
12386 
12387     case MEM:
12388       {
12389 	rtx new_rtl = avoid_constant_pool_reference (rtl);
12390 	if (new_rtl != rtl)
12391 	  {
12392 	    mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12393 						 initialized);
12394 	    if (mem_loc_result != NULL)
12395 	      return mem_loc_result;
12396 	  }
12397       }
12398       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12399 					   get_address_mode (rtl), mode,
12400 					   VAR_INIT_STATUS_INITIALIZED);
12401       if (mem_loc_result == NULL)
12402 	mem_loc_result = tls_mem_loc_descriptor (rtl);
12403       if (mem_loc_result != NULL)
12404 	{
12405 	  if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12406 	      || GET_MODE_CLASS (mode) != MODE_INT)
12407 	    {
12408 	      dw_die_ref type_die;
12409 	      dw_loc_descr_ref deref;
12410 
12411 	      if (dwarf_strict)
12412 		return NULL;
12413 	      type_die
12414 		= base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12415 	      if (type_die == NULL)
12416 		return NULL;
12417 	      deref = new_loc_descr (DW_OP_GNU_deref_type,
12418 				     GET_MODE_SIZE (mode), 0);
12419 	      deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12420 	      deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12421 	      deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12422 	      add_loc_descr (&mem_loc_result, deref);
12423 	    }
12424 	  else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12425 	    add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12426 	  else
12427 	    add_loc_descr (&mem_loc_result,
12428 			   new_loc_descr (DW_OP_deref_size,
12429 					  GET_MODE_SIZE (mode), 0));
12430 	}
12431       break;
12432 
12433     case LO_SUM:
12434       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12435 
12436     case LABEL_REF:
12437       /* Some ports can transform a symbol ref into a label ref, because
12438 	 the symbol ref is too far away and has to be dumped into a constant
12439 	 pool.  */
12440     case CONST:
12441     case SYMBOL_REF:
12442       if (GET_MODE_CLASS (mode) != MODE_INT
12443 	  || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12444 #ifdef POINTERS_EXTEND_UNSIGNED
12445 	      && (mode != Pmode || mem_mode == VOIDmode)
12446 #endif
12447 	      ))
12448 	break;
12449       if (GET_CODE (rtl) == SYMBOL_REF
12450 	  && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12451 	{
12452 	  dw_loc_descr_ref temp;
12453 
12454 	  /* If this is not defined, we have no way to emit the data.  */
12455 	  if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12456 	    break;
12457 
12458           temp = new_addr_loc_descr (rtl, dtprel_true);
12459 
12460 	  mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12461 	  add_loc_descr (&mem_loc_result, temp);
12462 
12463 	  break;
12464 	}
12465 
12466       if (!const_ok_for_output (rtl))
12467 	break;
12468 
12469     symref:
12470       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12471       vec_safe_push (used_rtx_array, rtl);
12472       break;
12473 
12474     case CONCAT:
12475     case CONCATN:
12476     case VAR_LOCATION:
12477     case DEBUG_IMPLICIT_PTR:
12478       expansion_failed (NULL_TREE, rtl,
12479 			"CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12480       return 0;
12481 
12482     case ENTRY_VALUE:
12483       if (dwarf_strict)
12484 	return NULL;
12485       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12486 	{
12487 	  if (GET_MODE_CLASS (mode) != MODE_INT
12488 	      || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12489 	    op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12490 				      VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12491 	  else
12492 	    {
12493               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12494 	      if (dbx_regnum == IGNORED_DWARF_REGNUM)
12495 		return NULL;
12496 	      op0 = one_reg_loc_descriptor (dbx_regnum,
12497 					    VAR_INIT_STATUS_INITIALIZED);
12498 	    }
12499 	}
12500       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12501 	       && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12502 	{
12503 	  op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12504 				    VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12505 	  if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12506 	    return NULL;
12507 	}
12508       else
12509 	gcc_unreachable ();
12510       if (op0 == NULL)
12511 	return NULL;
12512       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12513       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12514       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12515       break;
12516 
12517     case DEBUG_PARAMETER_REF:
12518       mem_loc_result = parameter_ref_descriptor (rtl);
12519       break;
12520 
12521     case PRE_MODIFY:
12522       /* Extract the PLUS expression nested inside and fall into
12523 	 PLUS code below.  */
12524       rtl = XEXP (rtl, 1);
12525       goto plus;
12526 
12527     case PRE_INC:
12528     case PRE_DEC:
12529       /* Turn these into a PLUS expression and fall into the PLUS code
12530 	 below.  */
12531       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12532 			  gen_int_mode (GET_CODE (rtl) == PRE_INC
12533 					? GET_MODE_UNIT_SIZE (mem_mode)
12534 					: -GET_MODE_UNIT_SIZE (mem_mode),
12535 					mode));
12536 
12537       /* ... fall through ...  */
12538 
12539     case PLUS:
12540     plus:
12541       if (is_based_loc (rtl)
12542 	  && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12543 	      || XEXP (rtl, 0) == arg_pointer_rtx
12544 	      || XEXP (rtl, 0) == frame_pointer_rtx)
12545 	  && GET_MODE_CLASS (mode) == MODE_INT)
12546 	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12547 					  INTVAL (XEXP (rtl, 1)),
12548 					  VAR_INIT_STATUS_INITIALIZED);
12549       else
12550 	{
12551 	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12552 					       VAR_INIT_STATUS_INITIALIZED);
12553 	  if (mem_loc_result == 0)
12554 	    break;
12555 
12556 	  if (CONST_INT_P (XEXP (rtl, 1))
12557 	      && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12558 	    loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12559 	  else
12560 	    {
12561 	      op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12562 					VAR_INIT_STATUS_INITIALIZED);
12563 	      if (op1 == 0)
12564 		return NULL;
12565 	      add_loc_descr (&mem_loc_result, op1);
12566 	      add_loc_descr (&mem_loc_result,
12567 			     new_loc_descr (DW_OP_plus, 0, 0));
12568 	    }
12569 	}
12570       break;
12571 
12572     /* If a pseudo-reg is optimized away, it is possible for it to
12573        be replaced with a MEM containing a multiply or shift.  */
12574     case MINUS:
12575       op = DW_OP_minus;
12576       goto do_binop;
12577 
12578     case MULT:
12579       op = DW_OP_mul;
12580       goto do_binop;
12581 
12582     case DIV:
12583       if (!dwarf_strict
12584 	  && GET_MODE_CLASS (mode) == MODE_INT
12585 	  && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12586 	{
12587 	  mem_loc_result = typed_binop (DW_OP_div, rtl,
12588 					base_type_for_mode (mode, 0),
12589 					mode, mem_mode);
12590 	  break;
12591 	}
12592       op = DW_OP_div;
12593       goto do_binop;
12594 
12595     case UMOD:
12596       op = DW_OP_mod;
12597       goto do_binop;
12598 
12599     case ASHIFT:
12600       op = DW_OP_shl;
12601       goto do_shift;
12602 
12603     case ASHIFTRT:
12604       op = DW_OP_shra;
12605       goto do_shift;
12606 
12607     case LSHIFTRT:
12608       op = DW_OP_shr;
12609       goto do_shift;
12610 
12611     do_shift:
12612       if (GET_MODE_CLASS (mode) != MODE_INT)
12613 	break;
12614       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12615 				VAR_INIT_STATUS_INITIALIZED);
12616       {
12617 	rtx rtlop1 = XEXP (rtl, 1);
12618 	if (GET_MODE (rtlop1) != VOIDmode
12619 	    && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12620 	       < GET_MODE_BITSIZE (mode))
12621 	  rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12622 	op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12623 				  VAR_INIT_STATUS_INITIALIZED);
12624       }
12625 
12626       if (op0 == 0 || op1 == 0)
12627 	break;
12628 
12629       mem_loc_result = op0;
12630       add_loc_descr (&mem_loc_result, op1);
12631       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12632       break;
12633 
12634     case AND:
12635       op = DW_OP_and;
12636       goto do_binop;
12637 
12638     case IOR:
12639       op = DW_OP_or;
12640       goto do_binop;
12641 
12642     case XOR:
12643       op = DW_OP_xor;
12644       goto do_binop;
12645 
12646     do_binop:
12647       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12648 				VAR_INIT_STATUS_INITIALIZED);
12649       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12650 				VAR_INIT_STATUS_INITIALIZED);
12651 
12652       if (op0 == 0 || op1 == 0)
12653 	break;
12654 
12655       mem_loc_result = op0;
12656       add_loc_descr (&mem_loc_result, op1);
12657       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12658       break;
12659 
12660     case MOD:
12661       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12662 	{
12663 	  mem_loc_result = typed_binop (DW_OP_mod, rtl,
12664 					base_type_for_mode (mode, 0),
12665 					mode, mem_mode);
12666 	  break;
12667 	}
12668 
12669       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12670 				VAR_INIT_STATUS_INITIALIZED);
12671       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12672 				VAR_INIT_STATUS_INITIALIZED);
12673 
12674       if (op0 == 0 || op1 == 0)
12675 	break;
12676 
12677       mem_loc_result = op0;
12678       add_loc_descr (&mem_loc_result, op1);
12679       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12680       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12681       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12682       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12683       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12684       break;
12685 
12686     case UDIV:
12687       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12688 	{
12689 	  if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12690 	    {
12691 	      op = DW_OP_div;
12692 	      goto do_binop;
12693 	    }
12694 	  mem_loc_result = typed_binop (DW_OP_div, rtl,
12695 					base_type_for_mode (mode, 1),
12696 					mode, mem_mode);
12697 	}
12698       break;
12699 
12700     case NOT:
12701       op = DW_OP_not;
12702       goto do_unop;
12703 
12704     case ABS:
12705       op = DW_OP_abs;
12706       goto do_unop;
12707 
12708     case NEG:
12709       op = DW_OP_neg;
12710       goto do_unop;
12711 
12712     do_unop:
12713       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12714 				VAR_INIT_STATUS_INITIALIZED);
12715 
12716       if (op0 == 0)
12717 	break;
12718 
12719       mem_loc_result = op0;
12720       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12721       break;
12722 
12723     case CONST_INT:
12724       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12725 #ifdef POINTERS_EXTEND_UNSIGNED
12726 	  || (mode == Pmode
12727 	      && mem_mode != VOIDmode
12728 	      && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12729 #endif
12730 	  )
12731 	{
12732 	  mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12733 	  break;
12734 	}
12735       if (!dwarf_strict
12736 	  && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12737 	      || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12738 	{
12739 	  dw_die_ref type_die = base_type_for_mode (mode, 1);
12740 	  enum machine_mode amode;
12741 	  if (type_die == NULL)
12742 	    return NULL;
12743 	  amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12744 				 MODE_INT, 0);
12745 	  if (INTVAL (rtl) >= 0
12746 	      && amode != BLKmode
12747 	      && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12748 	      /* const DW_OP_GNU_convert <XXX> vs.
12749 		 DW_OP_GNU_const_type <XXX, 1, const>.  */
12750 	      && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12751 		 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12752 	    {
12753 	      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12754 	      op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12755 	      op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12756 	      op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12757 	      op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12758 	      add_loc_descr (&mem_loc_result, op0);
12759 	      return mem_loc_result;
12760 	    }
12761 	  mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12762 					  INTVAL (rtl));
12763 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12764 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12765 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12766 	  if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12767 	    mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12768 	  else
12769 	    {
12770 	      mem_loc_result->dw_loc_oprnd2.val_class
12771 		= dw_val_class_const_double;
12772 	      mem_loc_result->dw_loc_oprnd2.v.val_double
12773 		= double_int::from_shwi (INTVAL (rtl));
12774 	    }
12775 	}
12776       break;
12777 
12778     case CONST_DOUBLE:
12779       if (!dwarf_strict)
12780 	{
12781 	  dw_die_ref type_die;
12782 
12783 	  /* Note that a CONST_DOUBLE rtx could represent either an integer
12784 	     or a floating-point constant.  A CONST_DOUBLE is used whenever
12785 	     the constant requires more than one word in order to be
12786 	     adequately represented.  We output CONST_DOUBLEs as blocks.  */
12787 	  if (mode == VOIDmode
12788 	      || (GET_MODE (rtl) == VOIDmode
12789 		  && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12790 	    break;
12791 	  type_die = base_type_for_mode (mode,
12792 					 GET_MODE_CLASS (mode) == MODE_INT);
12793 	  if (type_die == NULL)
12794 	    return NULL;
12795 	  mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12796 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12797 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12798 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12799 	  if (SCALAR_FLOAT_MODE_P (mode))
12800 	    {
12801 	      unsigned int length = GET_MODE_SIZE (mode);
12802 	      unsigned char *array
12803 		  = (unsigned char*) ggc_alloc_atomic (length);
12804 
12805 	      insert_float (rtl, array);
12806 	      mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12807 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12808 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12809 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12810 	    }
12811 	  else
12812 	    {
12813 	      mem_loc_result->dw_loc_oprnd2.val_class
12814 		= dw_val_class_const_double;
12815 	      mem_loc_result->dw_loc_oprnd2.v.val_double
12816 		= rtx_to_double_int (rtl);
12817 	    }
12818 	}
12819       break;
12820 
12821     case EQ:
12822       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12823       break;
12824 
12825     case GE:
12826       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12827       break;
12828 
12829     case GT:
12830       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12831       break;
12832 
12833     case LE:
12834       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12835       break;
12836 
12837     case LT:
12838       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12839       break;
12840 
12841     case NE:
12842       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12843       break;
12844 
12845     case GEU:
12846       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12847       break;
12848 
12849     case GTU:
12850       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12851       break;
12852 
12853     case LEU:
12854       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12855       break;
12856 
12857     case LTU:
12858       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12859       break;
12860 
12861     case UMIN:
12862     case UMAX:
12863       if (GET_MODE_CLASS (mode) != MODE_INT)
12864 	break;
12865       /* FALLTHRU */
12866     case SMIN:
12867     case SMAX:
12868       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12869       break;
12870 
12871     case ZERO_EXTRACT:
12872     case SIGN_EXTRACT:
12873       if (CONST_INT_P (XEXP (rtl, 1))
12874 	  && CONST_INT_P (XEXP (rtl, 2))
12875 	  && ((unsigned) INTVAL (XEXP (rtl, 1))
12876 	      + (unsigned) INTVAL (XEXP (rtl, 2))
12877 	      <= GET_MODE_BITSIZE (mode))
12878 	  && GET_MODE_CLASS (mode) == MODE_INT
12879 	  && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12880 	  && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12881 	{
12882 	  int shift, size;
12883 	  op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12884 				    mem_mode, VAR_INIT_STATUS_INITIALIZED);
12885 	  if (op0 == 0)
12886 	    break;
12887 	  if (GET_CODE (rtl) == SIGN_EXTRACT)
12888 	    op = DW_OP_shra;
12889 	  else
12890 	    op = DW_OP_shr;
12891 	  mem_loc_result = op0;
12892 	  size = INTVAL (XEXP (rtl, 1));
12893 	  shift = INTVAL (XEXP (rtl, 2));
12894 	  if (BITS_BIG_ENDIAN)
12895 	    shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12896 		    - shift - size;
12897 	  if (shift + size != (int) DWARF2_ADDR_SIZE)
12898 	    {
12899 	      add_loc_descr (&mem_loc_result,
12900 			     int_loc_descriptor (DWARF2_ADDR_SIZE
12901 						 - shift - size));
12902 	      add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12903 	    }
12904 	  if (size != (int) DWARF2_ADDR_SIZE)
12905 	    {
12906 	      add_loc_descr (&mem_loc_result,
12907 			     int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12908 	      add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12909 	    }
12910 	}
12911       break;
12912 
12913     case IF_THEN_ELSE:
12914       {
12915 	dw_loc_descr_ref op2, bra_node, drop_node;
12916 	op0 = mem_loc_descriptor (XEXP (rtl, 0),
12917 				  GET_MODE (XEXP (rtl, 0)) == VOIDmode
12918 				  ? word_mode : GET_MODE (XEXP (rtl, 0)),
12919 				  mem_mode, VAR_INIT_STATUS_INITIALIZED);
12920 	op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12921 				  VAR_INIT_STATUS_INITIALIZED);
12922 	op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12923 				  VAR_INIT_STATUS_INITIALIZED);
12924 	if (op0 == NULL || op1 == NULL || op2 == NULL)
12925 	  break;
12926 
12927 	mem_loc_result = op1;
12928 	add_loc_descr (&mem_loc_result, op2);
12929 	add_loc_descr (&mem_loc_result, op0);
12930 	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12931 	add_loc_descr (&mem_loc_result, bra_node);
12932 	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12933 	drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12934 	add_loc_descr (&mem_loc_result, drop_node);
12935 	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12936 	bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12937       }
12938       break;
12939 
12940     case FLOAT_EXTEND:
12941     case FLOAT_TRUNCATE:
12942     case FLOAT:
12943     case UNSIGNED_FLOAT:
12944     case FIX:
12945     case UNSIGNED_FIX:
12946       if (!dwarf_strict)
12947 	{
12948 	  dw_die_ref type_die;
12949 	  dw_loc_descr_ref cvt;
12950 
12951 	  op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12952 				    mem_mode, VAR_INIT_STATUS_INITIALIZED);
12953 	  if (op0 == NULL)
12954 	    break;
12955 	  if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12956 	      && (GET_CODE (rtl) == FLOAT
12957 		  || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12958 		     <= DWARF2_ADDR_SIZE))
12959 	    {
12960 	      type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12961 					     GET_CODE (rtl) == UNSIGNED_FLOAT);
12962 	      if (type_die == NULL)
12963 		break;
12964 	      cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12965 	      cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12966 	      cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12967 	      cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12968 	      add_loc_descr (&op0, cvt);
12969 	    }
12970 	  type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12971 	  if (type_die == NULL)
12972 	    break;
12973 	  cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12974 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12975 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12976 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12977 	  add_loc_descr (&op0, cvt);
12978 	  if (GET_MODE_CLASS (mode) == MODE_INT
12979 	      && (GET_CODE (rtl) == FIX
12980 		  || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12981 	    {
12982 	      op0 = convert_descriptor_to_mode (mode, op0);
12983 	      if (op0 == NULL)
12984 		break;
12985 	    }
12986 	  mem_loc_result = op0;
12987 	}
12988       break;
12989 
12990     case CLZ:
12991     case CTZ:
12992     case FFS:
12993       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12994       break;
12995 
12996     case POPCOUNT:
12997     case PARITY:
12998       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12999       break;
13000 
13001     case BSWAP:
13002       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13003       break;
13004 
13005     case ROTATE:
13006     case ROTATERT:
13007       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13008       break;
13009 
13010     case COMPARE:
13011       /* In theory, we could implement the above.  */
13012       /* DWARF cannot represent the unsigned compare operations
13013 	 natively.  */
13014     case SS_MULT:
13015     case US_MULT:
13016     case SS_DIV:
13017     case US_DIV:
13018     case SS_PLUS:
13019     case US_PLUS:
13020     case SS_MINUS:
13021     case US_MINUS:
13022     case SS_NEG:
13023     case US_NEG:
13024     case SS_ABS:
13025     case SS_ASHIFT:
13026     case US_ASHIFT:
13027     case SS_TRUNCATE:
13028     case US_TRUNCATE:
13029     case UNORDERED:
13030     case ORDERED:
13031     case UNEQ:
13032     case UNGE:
13033     case UNGT:
13034     case UNLE:
13035     case UNLT:
13036     case LTGT:
13037     case FRACT_CONVERT:
13038     case UNSIGNED_FRACT_CONVERT:
13039     case SAT_FRACT:
13040     case UNSIGNED_SAT_FRACT:
13041     case SQRT:
13042     case ASM_OPERANDS:
13043     case VEC_MERGE:
13044     case VEC_SELECT:
13045     case VEC_CONCAT:
13046     case VEC_DUPLICATE:
13047     case UNSPEC:
13048     case HIGH:
13049     case FMA:
13050     case STRICT_LOW_PART:
13051     case CONST_VECTOR:
13052     case CONST_FIXED:
13053     case CLRSB:
13054     case CLOBBER:
13055       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13056 	 can't express it in the debug info.  This can happen e.g. with some
13057 	 TLS UNSPECs.  */
13058       break;
13059 
13060     case CONST_STRING:
13061       resolve_one_addr (&rtl, NULL);
13062       goto symref;
13063 
13064     default:
13065 #ifdef ENABLE_CHECKING
13066       print_rtl (stderr, rtl);
13067       gcc_unreachable ();
13068 #else
13069       break;
13070 #endif
13071     }
13072 
13073   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13074     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13075 
13076   return mem_loc_result;
13077 }
13078 
13079 /* Return a descriptor that describes the concatenation of two locations.
13080    This is typically a complex variable.  */
13081 
13082 static dw_loc_descr_ref
concat_loc_descriptor(rtx x0,rtx x1,enum var_init_status initialized)13083 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13084 {
13085   dw_loc_descr_ref cc_loc_result = NULL;
13086   dw_loc_descr_ref x0_ref
13087     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13088   dw_loc_descr_ref x1_ref
13089     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13090 
13091   if (x0_ref == 0 || x1_ref == 0)
13092     return 0;
13093 
13094   cc_loc_result = x0_ref;
13095   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13096 
13097   add_loc_descr (&cc_loc_result, x1_ref);
13098   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13099 
13100   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13101     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13102 
13103   return cc_loc_result;
13104 }
13105 
13106 /* Return a descriptor that describes the concatenation of N
13107    locations.  */
13108 
13109 static dw_loc_descr_ref
concatn_loc_descriptor(rtx concatn,enum var_init_status initialized)13110 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13111 {
13112   unsigned int i;
13113   dw_loc_descr_ref cc_loc_result = NULL;
13114   unsigned int n = XVECLEN (concatn, 0);
13115 
13116   for (i = 0; i < n; ++i)
13117     {
13118       dw_loc_descr_ref ref;
13119       rtx x = XVECEXP (concatn, 0, i);
13120 
13121       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13122       if (ref == NULL)
13123 	return NULL;
13124 
13125       add_loc_descr (&cc_loc_result, ref);
13126       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13127     }
13128 
13129   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13130     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13131 
13132   return cc_loc_result;
13133 }
13134 
13135 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13136    for DEBUG_IMPLICIT_PTR RTL.  */
13137 
13138 static dw_loc_descr_ref
implicit_ptr_descriptor(rtx rtl,HOST_WIDE_INT offset)13139 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13140 {
13141   dw_loc_descr_ref ret;
13142   dw_die_ref ref;
13143 
13144   if (dwarf_strict)
13145     return NULL;
13146   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13147 	      || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13148 	      || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13149   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13150   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13151   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13152   if (ref)
13153     {
13154       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13155       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13156       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13157     }
13158   else
13159     {
13160       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13161       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13162     }
13163   return ret;
13164 }
13165 
13166 /* Output a proper Dwarf location descriptor for a variable or parameter
13167    which is either allocated in a register or in a memory location.  For a
13168    register, we just generate an OP_REG and the register number.  For a
13169    memory location we provide a Dwarf postfix expression describing how to
13170    generate the (dynamic) address of the object onto the address stack.
13171 
13172    MODE is mode of the decl if this loc_descriptor is going to be used in
13173    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13174    allowed, VOIDmode otherwise.
13175 
13176    If we don't know how to describe it, return 0.  */
13177 
13178 static dw_loc_descr_ref
loc_descriptor(rtx rtl,enum machine_mode mode,enum var_init_status initialized)13179 loc_descriptor (rtx rtl, enum machine_mode mode,
13180 		enum var_init_status initialized)
13181 {
13182   dw_loc_descr_ref loc_result = NULL;
13183 
13184   switch (GET_CODE (rtl))
13185     {
13186     case SUBREG:
13187       /* The case of a subreg may arise when we have a local (register)
13188 	 variable or a formal (register) parameter which doesn't quite fill
13189 	 up an entire register.  For now, just assume that it is
13190 	 legitimate to make the Dwarf info refer to the whole register which
13191 	 contains the given subreg.  */
13192       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13193 	loc_result = loc_descriptor (SUBREG_REG (rtl),
13194 				     GET_MODE (SUBREG_REG (rtl)), initialized);
13195       else
13196 	goto do_default;
13197       break;
13198 
13199     case REG:
13200       loc_result = reg_loc_descriptor (rtl, initialized);
13201       break;
13202 
13203     case MEM:
13204       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13205 				       GET_MODE (rtl), initialized);
13206       if (loc_result == NULL)
13207 	loc_result = tls_mem_loc_descriptor (rtl);
13208       if (loc_result == NULL)
13209 	{
13210 	  rtx new_rtl = avoid_constant_pool_reference (rtl);
13211 	  if (new_rtl != rtl)
13212 	    loc_result = loc_descriptor (new_rtl, mode, initialized);
13213 	}
13214       break;
13215 
13216     case CONCAT:
13217       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13218 					  initialized);
13219       break;
13220 
13221     case CONCATN:
13222       loc_result = concatn_loc_descriptor (rtl, initialized);
13223       break;
13224 
13225     case VAR_LOCATION:
13226       /* Single part.  */
13227       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13228 	{
13229 	  rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13230 	  if (GET_CODE (loc) == EXPR_LIST)
13231 	    loc = XEXP (loc, 0);
13232 	  loc_result = loc_descriptor (loc, mode, initialized);
13233 	  break;
13234 	}
13235 
13236       rtl = XEXP (rtl, 1);
13237       /* FALLTHRU */
13238 
13239     case PARALLEL:
13240       {
13241 	rtvec par_elems = XVEC (rtl, 0);
13242 	int num_elem = GET_NUM_ELEM (par_elems);
13243 	enum machine_mode mode;
13244 	int i;
13245 
13246 	/* Create the first one, so we have something to add to.  */
13247 	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13248 				     VOIDmode, initialized);
13249 	if (loc_result == NULL)
13250 	  return NULL;
13251 	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13252 	add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13253 	for (i = 1; i < num_elem; i++)
13254 	  {
13255 	    dw_loc_descr_ref temp;
13256 
13257 	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13258 				   VOIDmode, initialized);
13259 	    if (temp == NULL)
13260 	      return NULL;
13261 	    add_loc_descr (&loc_result, temp);
13262 	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13263 	    add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13264 	  }
13265       }
13266       break;
13267 
13268     case CONST_INT:
13269       if (mode != VOIDmode && mode != BLKmode)
13270 	loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13271 						    INTVAL (rtl));
13272       break;
13273 
13274     case CONST_DOUBLE:
13275       if (mode == VOIDmode)
13276 	mode = GET_MODE (rtl);
13277 
13278       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13279 	{
13280 	  gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13281 
13282 	  /* Note that a CONST_DOUBLE rtx could represent either an integer
13283 	     or a floating-point constant.  A CONST_DOUBLE is used whenever
13284 	     the constant requires more than one word in order to be
13285 	     adequately represented.  We output CONST_DOUBLEs as blocks.  */
13286 	  loc_result = new_loc_descr (DW_OP_implicit_value,
13287 				      GET_MODE_SIZE (mode), 0);
13288 	  if (SCALAR_FLOAT_MODE_P (mode))
13289 	    {
13290 	      unsigned int length = GET_MODE_SIZE (mode);
13291 	      unsigned char *array
13292                   = (unsigned char*) ggc_alloc_atomic (length);
13293 
13294 	      insert_float (rtl, array);
13295 	      loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13296 	      loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13297 	      loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13298 	      loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13299 	    }
13300 	  else
13301 	    {
13302 	      loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13303 	      loc_result->dw_loc_oprnd2.v.val_double
13304 	        = rtx_to_double_int (rtl);
13305 	    }
13306 	}
13307       break;
13308 
13309     case CONST_VECTOR:
13310       if (mode == VOIDmode)
13311 	mode = GET_MODE (rtl);
13312 
13313       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13314 	{
13315 	  unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13316 	  unsigned int length = CONST_VECTOR_NUNITS (rtl);
13317 	  unsigned char *array = (unsigned char *)
13318 	    ggc_alloc_atomic (length * elt_size);
13319 	  unsigned int i;
13320 	  unsigned char *p;
13321 
13322 	  gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13323 	  switch (GET_MODE_CLASS (mode))
13324 	    {
13325 	    case MODE_VECTOR_INT:
13326 	      for (i = 0, p = array; i < length; i++, p += elt_size)
13327 		{
13328 		  rtx elt = CONST_VECTOR_ELT (rtl, i);
13329 		  double_int val = rtx_to_double_int (elt);
13330 
13331 		  if (elt_size <= sizeof (HOST_WIDE_INT))
13332 		    insert_int (val.to_shwi (), elt_size, p);
13333 		  else
13334 		    {
13335 		      gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13336 		      insert_double (val, p);
13337 		    }
13338 		}
13339 	      break;
13340 
13341 	    case MODE_VECTOR_FLOAT:
13342 	      for (i = 0, p = array; i < length; i++, p += elt_size)
13343 		{
13344 		  rtx elt = CONST_VECTOR_ELT (rtl, i);
13345 		  insert_float (elt, p);
13346 		}
13347 	      break;
13348 
13349 	    default:
13350 	      gcc_unreachable ();
13351 	    }
13352 
13353 	  loc_result = new_loc_descr (DW_OP_implicit_value,
13354 				      length * elt_size, 0);
13355 	  loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13356 	  loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13357 	  loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13358 	  loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13359 	}
13360       break;
13361 
13362     case CONST:
13363       if (mode == VOIDmode
13364 	  || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13365 	  || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13366 	  || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13367 	{
13368 	  loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13369 	  break;
13370 	}
13371       /* FALLTHROUGH */
13372     case SYMBOL_REF:
13373       if (!const_ok_for_output (rtl))
13374 	break;
13375     case LABEL_REF:
13376       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13377 	  && (dwarf_version >= 4 || !dwarf_strict))
13378 	{
13379          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13380 	  add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13381 	  vec_safe_push (used_rtx_array, rtl);
13382 	}
13383       break;
13384 
13385     case DEBUG_IMPLICIT_PTR:
13386       loc_result = implicit_ptr_descriptor (rtl, 0);
13387       break;
13388 
13389     case PLUS:
13390       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13391 	  && CONST_INT_P (XEXP (rtl, 1)))
13392 	{
13393 	  loc_result
13394 	    = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13395 	  break;
13396 	}
13397       /* FALLTHRU */
13398     do_default:
13399     default:
13400       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13401 	   && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13402 	   && dwarf_version >= 4)
13403 	  || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13404 	{
13405 	  /* Value expression.  */
13406 	  loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13407 	  if (loc_result)
13408 	    add_loc_descr (&loc_result,
13409 			   new_loc_descr (DW_OP_stack_value, 0, 0));
13410 	}
13411       break;
13412     }
13413 
13414   return loc_result;
13415 }
13416 
13417 /* We need to figure out what section we should use as the base for the
13418    address ranges where a given location is valid.
13419    1. If this particular DECL has a section associated with it, use that.
13420    2. If this function has a section associated with it, use that.
13421    3. Otherwise, use the text section.
13422    XXX: If you split a variable across multiple sections, we won't notice.  */
13423 
13424 static const char *
secname_for_decl(const_tree decl)13425 secname_for_decl (const_tree decl)
13426 {
13427   const char *secname;
13428 
13429   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13430     {
13431       tree sectree = DECL_SECTION_NAME (decl);
13432       secname = TREE_STRING_POINTER (sectree);
13433     }
13434   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13435     {
13436       tree sectree = DECL_SECTION_NAME (current_function_decl);
13437       secname = TREE_STRING_POINTER (sectree);
13438     }
13439   else if (cfun && in_cold_section_p)
13440     secname = crtl->subsections.cold_section_label;
13441   else
13442     secname = text_section_label;
13443 
13444   return secname;
13445 }
13446 
13447 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13448 
13449 static bool
decl_by_reference_p(tree decl)13450 decl_by_reference_p (tree decl)
13451 {
13452   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13453   	   || TREE_CODE (decl) == VAR_DECL)
13454 	  && DECL_BY_REFERENCE (decl));
13455 }
13456 
13457 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13458    for VARLOC.  */
13459 
13460 static dw_loc_descr_ref
dw_loc_list_1(tree loc,rtx varloc,int want_address,enum var_init_status initialized)13461 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13462 	       enum var_init_status initialized)
13463 {
13464   int have_address = 0;
13465   dw_loc_descr_ref descr;
13466   enum machine_mode mode;
13467 
13468   if (want_address != 2)
13469     {
13470       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13471       /* Single part.  */
13472       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13473 	{
13474 	  varloc = PAT_VAR_LOCATION_LOC (varloc);
13475 	  if (GET_CODE (varloc) == EXPR_LIST)
13476 	    varloc = XEXP (varloc, 0);
13477 	  mode = GET_MODE (varloc);
13478 	  if (MEM_P (varloc))
13479 	    {
13480 	      rtx addr = XEXP (varloc, 0);
13481 	      descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13482 					  mode, initialized);
13483 	      if (descr)
13484 		have_address = 1;
13485 	      else
13486 		{
13487 		  rtx x = avoid_constant_pool_reference (varloc);
13488 		  if (x != varloc)
13489 		    descr = mem_loc_descriptor (x, mode, VOIDmode,
13490 						initialized);
13491 		}
13492 	    }
13493 	  else
13494 	    descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13495 	}
13496       else
13497 	return 0;
13498     }
13499   else
13500     {
13501       if (GET_CODE (varloc) == VAR_LOCATION)
13502 	mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13503       else
13504 	mode = DECL_MODE (loc);
13505       descr = loc_descriptor (varloc, mode, initialized);
13506       have_address = 1;
13507     }
13508 
13509   if (!descr)
13510     return 0;
13511 
13512   if (want_address == 2 && !have_address
13513       && (dwarf_version >= 4 || !dwarf_strict))
13514     {
13515       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13516 	{
13517 	  expansion_failed (loc, NULL_RTX,
13518 			    "DWARF address size mismatch");
13519 	  return 0;
13520 	}
13521       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13522       have_address = 1;
13523     }
13524   /* Show if we can't fill the request for an address.  */
13525   if (want_address && !have_address)
13526     {
13527       expansion_failed (loc, NULL_RTX,
13528 			"Want address and only have value");
13529       return 0;
13530     }
13531 
13532   /* If we've got an address and don't want one, dereference.  */
13533   if (!want_address && have_address)
13534     {
13535       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13536       enum dwarf_location_atom op;
13537 
13538       if (size > DWARF2_ADDR_SIZE || size == -1)
13539 	{
13540 	  expansion_failed (loc, NULL_RTX,
13541 			    "DWARF address size mismatch");
13542 	  return 0;
13543 	}
13544       else if (size == DWARF2_ADDR_SIZE)
13545 	op = DW_OP_deref;
13546       else
13547 	op = DW_OP_deref_size;
13548 
13549       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13550     }
13551 
13552   return descr;
13553 }
13554 
13555 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13556    if it is not possible.  */
13557 
13558 static dw_loc_descr_ref
new_loc_descr_op_bit_piece(HOST_WIDE_INT bitsize,HOST_WIDE_INT offset)13559 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13560 {
13561   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13562     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13563   else if (dwarf_version >= 3 || !dwarf_strict)
13564     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13565   else
13566     return NULL;
13567 }
13568 
13569 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13570    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13571 
13572 static dw_loc_descr_ref
dw_sra_loc_expr(tree decl,rtx loc)13573 dw_sra_loc_expr (tree decl, rtx loc)
13574 {
13575   rtx p;
13576   unsigned int padsize = 0;
13577   dw_loc_descr_ref descr, *descr_tail;
13578   unsigned HOST_WIDE_INT decl_size;
13579   rtx varloc;
13580   enum var_init_status initialized;
13581 
13582   if (DECL_SIZE (decl) == NULL
13583       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13584     return NULL;
13585 
13586   decl_size = tree_to_uhwi (DECL_SIZE (decl));
13587   descr = NULL;
13588   descr_tail = &descr;
13589 
13590   for (p = loc; p; p = XEXP (p, 1))
13591     {
13592       unsigned int bitsize = decl_piece_bitsize (p);
13593       rtx loc_note = *decl_piece_varloc_ptr (p);
13594       dw_loc_descr_ref cur_descr;
13595       dw_loc_descr_ref *tail, last = NULL;
13596       unsigned int opsize = 0;
13597 
13598       if (loc_note == NULL_RTX
13599 	  || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13600 	{
13601 	  padsize += bitsize;
13602 	  continue;
13603 	}
13604       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13605       varloc = NOTE_VAR_LOCATION (loc_note);
13606       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13607       if (cur_descr == NULL)
13608 	{
13609 	  padsize += bitsize;
13610 	  continue;
13611 	}
13612 
13613       /* Check that cur_descr either doesn't use
13614 	 DW_OP_*piece operations, or their sum is equal
13615 	 to bitsize.  Otherwise we can't embed it.  */
13616       for (tail = &cur_descr; *tail != NULL;
13617 	   tail = &(*tail)->dw_loc_next)
13618 	if ((*tail)->dw_loc_opc == DW_OP_piece)
13619 	  {
13620 	    opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13621 		      * BITS_PER_UNIT;
13622 	    last = *tail;
13623 	  }
13624 	else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13625 	  {
13626 	    opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13627 	    last = *tail;
13628 	  }
13629 
13630       if (last != NULL && opsize != bitsize)
13631 	{
13632 	  padsize += bitsize;
13633 	  /* Discard the current piece of the descriptor and release any
13634 	     addr_table entries it uses.  */
13635 	  remove_loc_list_addr_table_entries (cur_descr);
13636 	  continue;
13637 	}
13638 
13639       /* If there is a hole, add DW_OP_*piece after empty DWARF
13640 	 expression, which means that those bits are optimized out.  */
13641       if (padsize)
13642 	{
13643 	  if (padsize > decl_size)
13644 	    {
13645 	      remove_loc_list_addr_table_entries (cur_descr);
13646 	      goto discard_descr;
13647 	    }
13648 	  decl_size -= padsize;
13649 	  *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13650 	  if (*descr_tail == NULL)
13651 	    {
13652 	      remove_loc_list_addr_table_entries (cur_descr);
13653 	      goto discard_descr;
13654 	    }
13655 	  descr_tail = &(*descr_tail)->dw_loc_next;
13656 	  padsize = 0;
13657 	}
13658       *descr_tail = cur_descr;
13659       descr_tail = tail;
13660       if (bitsize > decl_size)
13661 	goto discard_descr;
13662       decl_size -= bitsize;
13663       if (last == NULL)
13664 	{
13665 	  HOST_WIDE_INT offset = 0;
13666 	  if (GET_CODE (varloc) == VAR_LOCATION
13667 	      && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13668 	    {
13669 	      varloc = PAT_VAR_LOCATION_LOC (varloc);
13670 	      if (GET_CODE (varloc) == EXPR_LIST)
13671 		varloc = XEXP (varloc, 0);
13672 	    }
13673 	  do
13674 	    {
13675 	      if (GET_CODE (varloc) == CONST
13676 		  || GET_CODE (varloc) == SIGN_EXTEND
13677 		  || GET_CODE (varloc) == ZERO_EXTEND)
13678 		varloc = XEXP (varloc, 0);
13679 	      else if (GET_CODE (varloc) == SUBREG)
13680 		varloc = SUBREG_REG (varloc);
13681 	      else
13682 		break;
13683 	    }
13684 	  while (1);
13685 	  /* DW_OP_bit_size offset should be zero for register
13686 	     or implicit location descriptions and empty location
13687 	     descriptions, but for memory addresses needs big endian
13688 	     adjustment.  */
13689 	  if (MEM_P (varloc))
13690 	    {
13691 	      unsigned HOST_WIDE_INT memsize
13692 		= MEM_SIZE (varloc) * BITS_PER_UNIT;
13693 	      if (memsize != bitsize)
13694 		{
13695 		  if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13696 		      && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13697 		    goto discard_descr;
13698 		  if (memsize < bitsize)
13699 		    goto discard_descr;
13700 		  if (BITS_BIG_ENDIAN)
13701 		    offset = memsize - bitsize;
13702 		}
13703 	    }
13704 
13705 	  *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13706 	  if (*descr_tail == NULL)
13707 	    goto discard_descr;
13708 	  descr_tail = &(*descr_tail)->dw_loc_next;
13709 	}
13710     }
13711 
13712   /* If there were any non-empty expressions, add padding till the end of
13713      the decl.  */
13714   if (descr != NULL && decl_size != 0)
13715     {
13716       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13717       if (*descr_tail == NULL)
13718 	goto discard_descr;
13719     }
13720   return descr;
13721 
13722 discard_descr:
13723   /* Discard the descriptor and release any addr_table entries it uses.  */
13724   remove_loc_list_addr_table_entries (descr);
13725   return NULL;
13726 }
13727 
13728 /* Return the dwarf representation of the location list LOC_LIST of
13729    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13730    function.  */
13731 
13732 static dw_loc_list_ref
dw_loc_list(var_loc_list * loc_list,tree decl,int want_address)13733 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13734 {
13735   const char *endname, *secname;
13736   rtx varloc;
13737   enum var_init_status initialized;
13738   struct var_loc_node *node;
13739   dw_loc_descr_ref descr;
13740   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13741   dw_loc_list_ref list = NULL;
13742   dw_loc_list_ref *listp = &list;
13743 
13744   /* Now that we know what section we are using for a base,
13745      actually construct the list of locations.
13746      The first location information is what is passed to the
13747      function that creates the location list, and the remaining
13748      locations just get added on to that list.
13749      Note that we only know the start address for a location
13750      (IE location changes), so to build the range, we use
13751      the range [current location start, next location start].
13752      This means we have to special case the last node, and generate
13753      a range of [last location start, end of function label].  */
13754 
13755   secname = secname_for_decl (decl);
13756 
13757   for (node = loc_list->first; node; node = node->next)
13758     if (GET_CODE (node->loc) == EXPR_LIST
13759 	|| NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13760       {
13761 	if (GET_CODE (node->loc) == EXPR_LIST)
13762 	  {
13763 	    /* This requires DW_OP_{,bit_}piece, which is not usable
13764 	       inside DWARF expressions.  */
13765 	    if (want_address != 2)
13766 	      continue;
13767 	    descr = dw_sra_loc_expr (decl, node->loc);
13768 	    if (descr == NULL)
13769 	      continue;
13770 	  }
13771 	else
13772 	  {
13773 	    initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13774 	    varloc = NOTE_VAR_LOCATION (node->loc);
13775 	    descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13776 	  }
13777 	if (descr)
13778 	  {
13779 	    bool range_across_switch = false;
13780 	    /* If section switch happens in between node->label
13781 	       and node->next->label (or end of function) and
13782 	       we can't emit it as a single entry list,
13783 	       emit two ranges, first one ending at the end
13784 	       of first partition and second one starting at the
13785 	       beginning of second partition.  */
13786 	    if (node == loc_list->last_before_switch
13787 		&& (node != loc_list->first || loc_list->first->next)
13788 		&& current_function_decl)
13789 	      {
13790 		endname = cfun->fde->dw_fde_end;
13791 		range_across_switch = true;
13792 	      }
13793 	    /* The variable has a location between NODE->LABEL and
13794 	       NODE->NEXT->LABEL.  */
13795 	    else if (node->next)
13796 	      endname = node->next->label;
13797 	    /* If the variable has a location at the last label
13798 	       it keeps its location until the end of function.  */
13799 	    else if (!current_function_decl)
13800 	      endname = text_end_label;
13801 	    else
13802 	      {
13803 		ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13804 					     current_function_funcdef_no);
13805 		endname = ggc_strdup (label_id);
13806 	      }
13807 
13808 	    *listp = new_loc_list (descr, node->label, endname, secname);
13809 	    if (TREE_CODE (decl) == PARM_DECL
13810 		&& node == loc_list->first
13811 		&& NOTE_P (node->loc)
13812 		&& strcmp (node->label, endname) == 0)
13813 	      (*listp)->force = true;
13814 	    listp = &(*listp)->dw_loc_next;
13815 
13816 	    if (range_across_switch)
13817 	      {
13818 		if (GET_CODE (node->loc) == EXPR_LIST)
13819 		  descr = dw_sra_loc_expr (decl, node->loc);
13820 		else
13821 		  {
13822 		    initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13823 		    varloc = NOTE_VAR_LOCATION (node->loc);
13824 		    descr = dw_loc_list_1 (decl, varloc, want_address,
13825 					   initialized);
13826 		  }
13827 		gcc_assert (descr);
13828 		/* The variable has a location between NODE->LABEL and
13829 		   NODE->NEXT->LABEL.  */
13830 		if (node->next)
13831 		  endname = node->next->label;
13832 		else
13833 		  endname = cfun->fde->dw_fde_second_end;
13834 		*listp = new_loc_list (descr,
13835 				       cfun->fde->dw_fde_second_begin,
13836 				       endname, secname);
13837 		listp = &(*listp)->dw_loc_next;
13838 	      }
13839 	  }
13840       }
13841 
13842   /* Try to avoid the overhead of a location list emitting a location
13843      expression instead, but only if we didn't have more than one
13844      location entry in the first place.  If some entries were not
13845      representable, we don't want to pretend a single entry that was
13846      applies to the entire scope in which the variable is
13847      available.  */
13848   if (list && loc_list->first->next)
13849     gen_llsym (list);
13850 
13851   return list;
13852 }
13853 
13854 /* Return if the loc_list has only single element and thus can be represented
13855    as location description.   */
13856 
13857 static bool
single_element_loc_list_p(dw_loc_list_ref list)13858 single_element_loc_list_p (dw_loc_list_ref list)
13859 {
13860   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13861   return !list->ll_symbol;
13862 }
13863 
13864 /* To each location in list LIST add loc descr REF.  */
13865 
13866 static void
add_loc_descr_to_each(dw_loc_list_ref list,dw_loc_descr_ref ref)13867 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13868 {
13869   dw_loc_descr_ref copy;
13870   add_loc_descr (&list->expr, ref);
13871   list = list->dw_loc_next;
13872   while (list)
13873     {
13874       copy = ggc_alloc_dw_loc_descr_node ();
13875       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13876       add_loc_descr (&list->expr, copy);
13877       while (copy->dw_loc_next)
13878 	{
13879 	  dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13880 	  memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13881 	  copy->dw_loc_next = new_copy;
13882 	  copy = new_copy;
13883 	}
13884       list = list->dw_loc_next;
13885     }
13886 }
13887 
13888 /* Given two lists RET and LIST
13889    produce location list that is result of adding expression in LIST
13890    to expression in RET on each position in program.
13891    Might be destructive on both RET and LIST.
13892 
13893    TODO: We handle only simple cases of RET or LIST having at most one
13894    element. General case would inolve sorting the lists in program order
13895    and merging them that will need some additional work.
13896    Adding that will improve quality of debug info especially for SRA-ed
13897    structures.  */
13898 
13899 static void
add_loc_list(dw_loc_list_ref * ret,dw_loc_list_ref list)13900 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13901 {
13902   if (!list)
13903     return;
13904   if (!*ret)
13905     {
13906       *ret = list;
13907       return;
13908     }
13909   if (!list->dw_loc_next)
13910     {
13911       add_loc_descr_to_each (*ret, list->expr);
13912       return;
13913     }
13914   if (!(*ret)->dw_loc_next)
13915     {
13916       add_loc_descr_to_each (list, (*ret)->expr);
13917       *ret = list;
13918       return;
13919     }
13920   expansion_failed (NULL_TREE, NULL_RTX,
13921 		    "Don't know how to merge two non-trivial"
13922 		    " location lists.\n");
13923   *ret = NULL;
13924   return;
13925 }
13926 
13927 /* LOC is constant expression.  Try a luck, look it up in constant
13928    pool and return its loc_descr of its address.  */
13929 
13930 static dw_loc_descr_ref
cst_pool_loc_descr(tree loc)13931 cst_pool_loc_descr (tree loc)
13932 {
13933   /* Get an RTL for this, if something has been emitted.  */
13934   rtx rtl = lookup_constant_def (loc);
13935 
13936   if (!rtl || !MEM_P (rtl))
13937     {
13938       gcc_assert (!rtl);
13939       return 0;
13940     }
13941   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13942 
13943   /* TODO: We might get more coverage if we was actually delaying expansion
13944      of all expressions till end of compilation when constant pools are fully
13945      populated.  */
13946   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13947     {
13948       expansion_failed (loc, NULL_RTX,
13949 			"CST value in contant pool but not marked.");
13950       return 0;
13951     }
13952   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13953 			     GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13954 }
13955 
13956 /* Return dw_loc_list representing address of addr_expr LOC
13957    by looking for inner INDIRECT_REF expression and turning
13958    it into simple arithmetics.  */
13959 
13960 static dw_loc_list_ref
loc_list_for_address_of_addr_expr_of_indirect_ref(tree loc,bool toplev)13961 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13962 {
13963   tree obj, offset;
13964   HOST_WIDE_INT bitsize, bitpos, bytepos;
13965   enum machine_mode mode;
13966   int unsignedp, volatilep = 0;
13967   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13968 
13969   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13970 			     &bitsize, &bitpos, &offset, &mode,
13971 			     &unsignedp, &volatilep, false);
13972   STRIP_NOPS (obj);
13973   if (bitpos % BITS_PER_UNIT)
13974     {
13975       expansion_failed (loc, NULL_RTX, "bitfield access");
13976       return 0;
13977     }
13978   if (!INDIRECT_REF_P (obj))
13979     {
13980       expansion_failed (obj,
13981 			NULL_RTX, "no indirect ref in inner refrence");
13982       return 0;
13983     }
13984   if (!offset && !bitpos)
13985     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13986   else if (toplev
13987 	   && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13988 	   && (dwarf_version >= 4 || !dwarf_strict))
13989     {
13990       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13991       if (!list_ret)
13992 	return 0;
13993       if (offset)
13994 	{
13995 	  /* Variable offset.  */
13996 	  list_ret1 = loc_list_from_tree (offset, 0);
13997 	  if (list_ret1 == 0)
13998 	    return 0;
13999 	  add_loc_list (&list_ret, list_ret1);
14000 	  if (!list_ret)
14001 	    return 0;
14002 	  add_loc_descr_to_each (list_ret,
14003 				 new_loc_descr (DW_OP_plus, 0, 0));
14004 	}
14005       bytepos = bitpos / BITS_PER_UNIT;
14006       if (bytepos > 0)
14007 	add_loc_descr_to_each (list_ret,
14008 			       new_loc_descr (DW_OP_plus_uconst,
14009 					      bytepos, 0));
14010       else if (bytepos < 0)
14011 	loc_list_plus_const (list_ret, bytepos);
14012       add_loc_descr_to_each (list_ret,
14013 			     new_loc_descr (DW_OP_stack_value, 0, 0));
14014     }
14015   return list_ret;
14016 }
14017 
14018 
14019 /* Generate Dwarf location list representing LOC.
14020    If WANT_ADDRESS is false, expression computing LOC will be computed
14021    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14022    if WANT_ADDRESS is 2, expression computing address useable in location
14023      will be returned (i.e. DW_OP_reg can be used
14024      to refer to register values).  */
14025 
14026 static dw_loc_list_ref
loc_list_from_tree(tree loc,int want_address)14027 loc_list_from_tree (tree loc, int want_address)
14028 {
14029   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14030   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14031   int have_address = 0;
14032   enum dwarf_location_atom op;
14033 
14034   /* ??? Most of the time we do not take proper care for sign/zero
14035      extending the values properly.  Hopefully this won't be a real
14036      problem...  */
14037 
14038   switch (TREE_CODE (loc))
14039     {
14040     case ERROR_MARK:
14041       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14042       return 0;
14043 
14044     case PLACEHOLDER_EXPR:
14045       /* This case involves extracting fields from an object to determine the
14046 	 position of other fields.  We don't try to encode this here.  The
14047 	 only user of this is Ada, which encodes the needed information using
14048 	 the names of types.  */
14049       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14050       return 0;
14051 
14052     case CALL_EXPR:
14053       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14054       /* There are no opcodes for these operations.  */
14055       return 0;
14056 
14057     case PREINCREMENT_EXPR:
14058     case PREDECREMENT_EXPR:
14059     case POSTINCREMENT_EXPR:
14060     case POSTDECREMENT_EXPR:
14061       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14062       /* There are no opcodes for these operations.  */
14063       return 0;
14064 
14065     case ADDR_EXPR:
14066       /* If we already want an address, see if there is INDIRECT_REF inside
14067          e.g. for &this->field.  */
14068       if (want_address)
14069 	{
14070 	  list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14071 		       (loc, want_address == 2);
14072 	  if (list_ret)
14073 	    have_address = 1;
14074 	  else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14075 	  	   && (ret = cst_pool_loc_descr (loc)))
14076 	    have_address = 1;
14077 	}
14078         /* Otherwise, process the argument and look for the address.  */
14079       if (!list_ret && !ret)
14080         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14081       else
14082 	{
14083 	  if (want_address)
14084 	    expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14085 	  return NULL;
14086 	}
14087       break;
14088 
14089     case VAR_DECL:
14090       if (DECL_THREAD_LOCAL_P (loc))
14091 	{
14092 	  rtx rtl;
14093          enum dwarf_location_atom tls_op;
14094          enum dtprel_bool dtprel = dtprel_false;
14095 
14096 	  if (targetm.have_tls)
14097 	    {
14098 	      /* If this is not defined, we have no way to emit the
14099 		 data.  */
14100 	      if (!targetm.asm_out.output_dwarf_dtprel)
14101 		return 0;
14102 
14103 	       /* The way DW_OP_GNU_push_tls_address is specified, we
14104 	     	  can only look up addresses of objects in the current
14105 	     	  module.  We used DW_OP_addr as first op, but that's
14106 		  wrong, because DW_OP_addr is relocated by the debug
14107 		  info consumer, while DW_OP_GNU_push_tls_address
14108 		  operand shouldn't be.  */
14109 	      if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14110 		return 0;
14111              dtprel = dtprel_true;
14112              tls_op = DW_OP_GNU_push_tls_address;
14113 	    }
14114 	  else
14115 	    {
14116 	      if (!targetm.emutls.debug_form_tls_address
14117 		  || !(dwarf_version >= 3 || !dwarf_strict))
14118 		return 0;
14119 	      /* We stuffed the control variable into the DECL_VALUE_EXPR
14120 		 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14121 		 no longer appear in gimple code.  We used the control
14122 		 variable in specific so that we could pick it up here.  */
14123 	      loc = DECL_VALUE_EXPR (loc);
14124               tls_op = DW_OP_form_tls_address;
14125 	    }
14126 
14127 	  rtl = rtl_for_decl_location (loc);
14128 	  if (rtl == NULL_RTX)
14129 	    return 0;
14130 
14131 	  if (!MEM_P (rtl))
14132 	    return 0;
14133 	  rtl = XEXP (rtl, 0);
14134 	  if (! CONSTANT_P (rtl))
14135 	    return 0;
14136 
14137           ret = new_addr_loc_descr (rtl, dtprel);
14138           ret1 = new_loc_descr (tls_op, 0, 0);
14139 	  add_loc_descr (&ret, ret1);
14140 
14141 	  have_address = 1;
14142 	  break;
14143 	}
14144       /* FALLTHRU */
14145 
14146     case PARM_DECL:
14147     case RESULT_DECL:
14148       if (DECL_HAS_VALUE_EXPR_P (loc))
14149 	return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14150 				   want_address);
14151       /* FALLTHRU */
14152 
14153     case FUNCTION_DECL:
14154       {
14155 	rtx rtl;
14156 	var_loc_list *loc_list = lookup_decl_loc (loc);
14157 
14158 	if (loc_list && loc_list->first)
14159 	  {
14160 	    list_ret = dw_loc_list (loc_list, loc, want_address);
14161 	    have_address = want_address != 0;
14162 	    break;
14163 	  }
14164 	rtl = rtl_for_decl_location (loc);
14165 	if (rtl == NULL_RTX)
14166 	  {
14167 	    expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14168 	    return 0;
14169 	  }
14170 	else if (CONST_INT_P (rtl))
14171 	  {
14172 	    HOST_WIDE_INT val = INTVAL (rtl);
14173 	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14174 	      val &= GET_MODE_MASK (DECL_MODE (loc));
14175 	    ret = int_loc_descriptor (val);
14176 	  }
14177 	else if (GET_CODE (rtl) == CONST_STRING)
14178 	  {
14179 	    expansion_failed (loc, NULL_RTX, "CONST_STRING");
14180 	    return 0;
14181 	  }
14182 	else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14183           ret = new_addr_loc_descr (rtl, dtprel_false);
14184 	else
14185 	  {
14186 	    enum machine_mode mode, mem_mode;
14187 
14188 	    /* Certain constructs can only be represented at top-level.  */
14189 	    if (want_address == 2)
14190 	      {
14191 		ret = loc_descriptor (rtl, VOIDmode,
14192 				      VAR_INIT_STATUS_INITIALIZED);
14193 		have_address = 1;
14194 	      }
14195 	    else
14196 	      {
14197 		mode = GET_MODE (rtl);
14198 		mem_mode = VOIDmode;
14199 		if (MEM_P (rtl))
14200 		  {
14201 		    mem_mode = mode;
14202 		    mode = get_address_mode (rtl);
14203 		    rtl = XEXP (rtl, 0);
14204 		    have_address = 1;
14205 		  }
14206 		ret = mem_loc_descriptor (rtl, mode, mem_mode,
14207 					  VAR_INIT_STATUS_INITIALIZED);
14208 	      }
14209 	    if (!ret)
14210 	      expansion_failed (loc, rtl,
14211 				"failed to produce loc descriptor for rtl");
14212 	  }
14213       }
14214       break;
14215 
14216     case MEM_REF:
14217       /* ??? FIXME.  */
14218       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14219 	return 0;
14220       /* Fallthru.  */
14221     case INDIRECT_REF:
14222       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14223       have_address = 1;
14224       break;
14225 
14226     case TARGET_MEM_REF:
14227     case SSA_NAME:
14228       return NULL;
14229 
14230     case COMPOUND_EXPR:
14231       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14232 
14233     CASE_CONVERT:
14234     case VIEW_CONVERT_EXPR:
14235     case SAVE_EXPR:
14236     case MODIFY_EXPR:
14237       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14238 
14239     case COMPONENT_REF:
14240     case BIT_FIELD_REF:
14241     case ARRAY_REF:
14242     case ARRAY_RANGE_REF:
14243     case REALPART_EXPR:
14244     case IMAGPART_EXPR:
14245       {
14246 	tree obj, offset;
14247 	HOST_WIDE_INT bitsize, bitpos, bytepos;
14248 	enum machine_mode mode;
14249 	int unsignedp, volatilep = 0;
14250 
14251 	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14252 				   &unsignedp, &volatilep, false);
14253 
14254 	gcc_assert (obj != loc);
14255 
14256 	list_ret = loc_list_from_tree (obj,
14257 				       want_address == 2
14258 				       && !bitpos && !offset ? 2 : 1);
14259 	/* TODO: We can extract value of the small expression via shifting even
14260 	   for nonzero bitpos.  */
14261 	if (list_ret == 0)
14262 	  return 0;
14263 	if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14264 	  {
14265 	    expansion_failed (loc, NULL_RTX,
14266 			      "bitfield access");
14267 	    return 0;
14268 	  }
14269 
14270 	if (offset != NULL_TREE)
14271 	  {
14272 	    /* Variable offset.  */
14273 	    list_ret1 = loc_list_from_tree (offset, 0);
14274 	    if (list_ret1 == 0)
14275 	      return 0;
14276 	    add_loc_list (&list_ret, list_ret1);
14277 	    if (!list_ret)
14278 	      return 0;
14279 	    add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14280 	  }
14281 
14282 	bytepos = bitpos / BITS_PER_UNIT;
14283 	if (bytepos > 0)
14284 	  add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14285 	else if (bytepos < 0)
14286 	  loc_list_plus_const (list_ret, bytepos);
14287 
14288 	have_address = 1;
14289 	break;
14290       }
14291 
14292     case INTEGER_CST:
14293       if ((want_address || !tree_fits_shwi_p (loc))
14294 	  && (ret = cst_pool_loc_descr (loc)))
14295 	have_address = 1;
14296       else if (want_address == 2
14297 	       && tree_fits_shwi_p (loc)
14298 	       && (ret = address_of_int_loc_descriptor
14299 	       		   (int_size_in_bytes (TREE_TYPE (loc)),
14300 	       		    tree_to_shwi (loc))))
14301 	have_address = 1;
14302       else if (tree_fits_shwi_p (loc))
14303 	ret = int_loc_descriptor (tree_to_shwi (loc));
14304       else
14305 	{
14306 	  expansion_failed (loc, NULL_RTX,
14307 			    "Integer operand is not host integer");
14308 	  return 0;
14309 	}
14310       break;
14311 
14312     case CONSTRUCTOR:
14313     case REAL_CST:
14314     case STRING_CST:
14315     case COMPLEX_CST:
14316       if ((ret = cst_pool_loc_descr (loc)))
14317 	have_address = 1;
14318       else
14319       /* We can construct small constants here using int_loc_descriptor.  */
14320 	expansion_failed (loc, NULL_RTX,
14321 			  "constructor or constant not in constant pool");
14322       break;
14323 
14324     case TRUTH_AND_EXPR:
14325     case TRUTH_ANDIF_EXPR:
14326     case BIT_AND_EXPR:
14327       op = DW_OP_and;
14328       goto do_binop;
14329 
14330     case TRUTH_XOR_EXPR:
14331     case BIT_XOR_EXPR:
14332       op = DW_OP_xor;
14333       goto do_binop;
14334 
14335     case TRUTH_OR_EXPR:
14336     case TRUTH_ORIF_EXPR:
14337     case BIT_IOR_EXPR:
14338       op = DW_OP_or;
14339       goto do_binop;
14340 
14341     case FLOOR_DIV_EXPR:
14342     case CEIL_DIV_EXPR:
14343     case ROUND_DIV_EXPR:
14344     case TRUNC_DIV_EXPR:
14345       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14346 	return 0;
14347       op = DW_OP_div;
14348       goto do_binop;
14349 
14350     case MINUS_EXPR:
14351       op = DW_OP_minus;
14352       goto do_binop;
14353 
14354     case FLOOR_MOD_EXPR:
14355     case CEIL_MOD_EXPR:
14356     case ROUND_MOD_EXPR:
14357     case TRUNC_MOD_EXPR:
14358       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14359 	{
14360 	  op = DW_OP_mod;
14361 	  goto do_binop;
14362 	}
14363       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14364       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14365       if (list_ret == 0 || list_ret1 == 0)
14366 	return 0;
14367 
14368       add_loc_list (&list_ret, list_ret1);
14369       if (list_ret == 0)
14370 	return 0;
14371       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14372       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14373       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14374       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14375       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14376       break;
14377 
14378     case MULT_EXPR:
14379       op = DW_OP_mul;
14380       goto do_binop;
14381 
14382     case LSHIFT_EXPR:
14383       op = DW_OP_shl;
14384       goto do_binop;
14385 
14386     case RSHIFT_EXPR:
14387       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14388       goto do_binop;
14389 
14390     case POINTER_PLUS_EXPR:
14391     case PLUS_EXPR:
14392       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14393 	{
14394 	  list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14395 	  if (list_ret == 0)
14396 	    return 0;
14397 
14398 	  loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14399 	  break;
14400 	}
14401 
14402       op = DW_OP_plus;
14403       goto do_binop;
14404 
14405     case LE_EXPR:
14406       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14407 	return 0;
14408 
14409       op = DW_OP_le;
14410       goto do_binop;
14411 
14412     case GE_EXPR:
14413       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14414 	return 0;
14415 
14416       op = DW_OP_ge;
14417       goto do_binop;
14418 
14419     case LT_EXPR:
14420       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14421 	return 0;
14422 
14423       op = DW_OP_lt;
14424       goto do_binop;
14425 
14426     case GT_EXPR:
14427       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14428 	return 0;
14429 
14430       op = DW_OP_gt;
14431       goto do_binop;
14432 
14433     case EQ_EXPR:
14434       op = DW_OP_eq;
14435       goto do_binop;
14436 
14437     case NE_EXPR:
14438       op = DW_OP_ne;
14439       goto do_binop;
14440 
14441     do_binop:
14442       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14443       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14444       if (list_ret == 0 || list_ret1 == 0)
14445 	return 0;
14446 
14447       add_loc_list (&list_ret, list_ret1);
14448       if (list_ret == 0)
14449 	return 0;
14450       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14451       break;
14452 
14453     case TRUTH_NOT_EXPR:
14454     case BIT_NOT_EXPR:
14455       op = DW_OP_not;
14456       goto do_unop;
14457 
14458     case ABS_EXPR:
14459       op = DW_OP_abs;
14460       goto do_unop;
14461 
14462     case NEGATE_EXPR:
14463       op = DW_OP_neg;
14464       goto do_unop;
14465 
14466     do_unop:
14467       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14468       if (list_ret == 0)
14469 	return 0;
14470 
14471       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14472       break;
14473 
14474     case MIN_EXPR:
14475     case MAX_EXPR:
14476       {
14477 	const enum tree_code code =
14478 	  TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14479 
14480 	loc = build3 (COND_EXPR, TREE_TYPE (loc),
14481 		      build2 (code, integer_type_node,
14482 			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14483 		      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14484       }
14485 
14486       /* ... fall through ...  */
14487 
14488     case COND_EXPR:
14489       {
14490 	dw_loc_descr_ref lhs
14491 	  = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14492 	dw_loc_list_ref rhs
14493 	  = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14494 	dw_loc_descr_ref bra_node, jump_node, tmp;
14495 
14496 	list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14497 	if (list_ret == 0 || lhs == 0 || rhs == 0)
14498 	  return 0;
14499 
14500 	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14501 	add_loc_descr_to_each (list_ret, bra_node);
14502 
14503 	add_loc_list (&list_ret, rhs);
14504 	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14505 	add_loc_descr_to_each (list_ret, jump_node);
14506 
14507 	add_loc_descr_to_each (list_ret, lhs);
14508 	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14509 	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14510 
14511 	/* ??? Need a node to point the skip at.  Use a nop.  */
14512 	tmp = new_loc_descr (DW_OP_nop, 0, 0);
14513 	add_loc_descr_to_each (list_ret, tmp);
14514 	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14515 	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14516       }
14517       break;
14518 
14519     case FIX_TRUNC_EXPR:
14520       return 0;
14521 
14522     default:
14523       /* Leave front-end specific codes as simply unknown.  This comes
14524 	 up, for instance, with the C STMT_EXPR.  */
14525       if ((unsigned int) TREE_CODE (loc)
14526 	  >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14527 	{
14528 	  expansion_failed (loc, NULL_RTX,
14529 			    "language specific tree node");
14530 	  return 0;
14531 	}
14532 
14533 #ifdef ENABLE_CHECKING
14534       /* Otherwise this is a generic code; we should just lists all of
14535 	 these explicitly.  We forgot one.  */
14536       gcc_unreachable ();
14537 #else
14538       /* In a release build, we want to degrade gracefully: better to
14539 	 generate incomplete debugging information than to crash.  */
14540       return NULL;
14541 #endif
14542     }
14543 
14544   if (!ret && !list_ret)
14545     return 0;
14546 
14547   if (want_address == 2 && !have_address
14548       && (dwarf_version >= 4 || !dwarf_strict))
14549     {
14550       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14551 	{
14552 	  expansion_failed (loc, NULL_RTX,
14553 			    "DWARF address size mismatch");
14554 	  return 0;
14555 	}
14556       if (ret)
14557 	add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14558       else
14559 	add_loc_descr_to_each (list_ret,
14560 			       new_loc_descr (DW_OP_stack_value, 0, 0));
14561       have_address = 1;
14562     }
14563   /* Show if we can't fill the request for an address.  */
14564   if (want_address && !have_address)
14565     {
14566       expansion_failed (loc, NULL_RTX,
14567 			"Want address and only have value");
14568       return 0;
14569     }
14570 
14571   gcc_assert (!ret || !list_ret);
14572 
14573   /* If we've got an address and don't want one, dereference.  */
14574   if (!want_address && have_address)
14575     {
14576       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14577 
14578       if (size > DWARF2_ADDR_SIZE || size == -1)
14579 	{
14580 	  expansion_failed (loc, NULL_RTX,
14581 			    "DWARF address size mismatch");
14582 	  return 0;
14583 	}
14584       else if (size == DWARF2_ADDR_SIZE)
14585 	op = DW_OP_deref;
14586       else
14587 	op = DW_OP_deref_size;
14588 
14589       if (ret)
14590 	add_loc_descr (&ret, new_loc_descr (op, size, 0));
14591       else
14592 	add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14593     }
14594   if (ret)
14595     list_ret = new_loc_list (ret, NULL, NULL, NULL);
14596 
14597   return list_ret;
14598 }
14599 
14600 /* Same as above but return only single location expression.  */
14601 static dw_loc_descr_ref
loc_descriptor_from_tree(tree loc,int want_address)14602 loc_descriptor_from_tree (tree loc, int want_address)
14603 {
14604   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14605   if (!ret)
14606     return NULL;
14607   if (ret->dw_loc_next)
14608     {
14609       expansion_failed (loc, NULL_RTX,
14610 			"Location list where only loc descriptor needed");
14611       return NULL;
14612     }
14613   return ret->expr;
14614 }
14615 
14616 /* Given a value, round it up to the lowest multiple of `boundary'
14617    which is not less than the value itself.  */
14618 
14619 static inline HOST_WIDE_INT
ceiling(HOST_WIDE_INT value,unsigned int boundary)14620 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14621 {
14622   return (((value + boundary - 1) / boundary) * boundary);
14623 }
14624 
14625 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14626    pointer to the declared type for the relevant field variable, or return
14627    `integer_type_node' if the given node turns out to be an
14628    ERROR_MARK node.  */
14629 
14630 static inline tree
field_type(const_tree decl)14631 field_type (const_tree decl)
14632 {
14633   tree type;
14634 
14635   if (TREE_CODE (decl) == ERROR_MARK)
14636     return integer_type_node;
14637 
14638   type = DECL_BIT_FIELD_TYPE (decl);
14639   if (type == NULL_TREE)
14640     type = TREE_TYPE (decl);
14641 
14642   return type;
14643 }
14644 
14645 /* Given a pointer to a tree node, return the alignment in bits for
14646    it, or else return BITS_PER_WORD if the node actually turns out to
14647    be an ERROR_MARK node.  */
14648 
14649 static inline unsigned
simple_type_align_in_bits(const_tree type)14650 simple_type_align_in_bits (const_tree type)
14651 {
14652   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14653 }
14654 
14655 static inline unsigned
simple_decl_align_in_bits(const_tree decl)14656 simple_decl_align_in_bits (const_tree decl)
14657 {
14658   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14659 }
14660 
14661 /* Return the result of rounding T up to ALIGN.  */
14662 
14663 static inline double_int
round_up_to_align(double_int t,unsigned int align)14664 round_up_to_align (double_int t, unsigned int align)
14665 {
14666   double_int alignd = double_int::from_uhwi (align);
14667   t += alignd;
14668   t += double_int_minus_one;
14669   t = t.div (alignd, true, TRUNC_DIV_EXPR);
14670   t *= alignd;
14671   return t;
14672 }
14673 
14674 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14675    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14676    or return 0 if we are unable to determine what that offset is, either
14677    because the argument turns out to be a pointer to an ERROR_MARK node, or
14678    because the offset is actually variable.  (We can't handle the latter case
14679    just yet).  */
14680 
14681 static HOST_WIDE_INT
field_byte_offset(const_tree decl)14682 field_byte_offset (const_tree decl)
14683 {
14684   double_int object_offset_in_bits;
14685   double_int object_offset_in_bytes;
14686   double_int bitpos_int;
14687 
14688   if (TREE_CODE (decl) == ERROR_MARK)
14689     return 0;
14690 
14691   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14692 
14693   /* We cannot yet cope with fields whose positions are variable, so
14694      for now, when we see such things, we simply return 0.  Someday, we may
14695      be able to handle such cases, but it will be damn difficult.  */
14696   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14697     return 0;
14698 
14699   bitpos_int = tree_to_double_int (bit_position (decl));
14700 
14701 #ifdef PCC_BITFIELD_TYPE_MATTERS
14702   if (PCC_BITFIELD_TYPE_MATTERS)
14703     {
14704       tree type;
14705       tree field_size_tree;
14706       double_int deepest_bitpos;
14707       double_int field_size_in_bits;
14708       unsigned int type_align_in_bits;
14709       unsigned int decl_align_in_bits;
14710       double_int type_size_in_bits;
14711 
14712       type = field_type (decl);
14713       type_size_in_bits = double_int_type_size_in_bits (type);
14714       type_align_in_bits = simple_type_align_in_bits (type);
14715 
14716       field_size_tree = DECL_SIZE (decl);
14717 
14718       /* The size could be unspecified if there was an error, or for
14719 	 a flexible array member.  */
14720       if (!field_size_tree)
14721 	field_size_tree = bitsize_zero_node;
14722 
14723       /* If the size of the field is not constant, use the type size.  */
14724       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14725 	field_size_in_bits = tree_to_double_int (field_size_tree);
14726       else
14727 	field_size_in_bits = type_size_in_bits;
14728 
14729       decl_align_in_bits = simple_decl_align_in_bits (decl);
14730 
14731       /* The GCC front-end doesn't make any attempt to keep track of the
14732 	 starting bit offset (relative to the start of the containing
14733 	 structure type) of the hypothetical "containing object" for a
14734 	 bit-field.  Thus, when computing the byte offset value for the
14735 	 start of the "containing object" of a bit-field, we must deduce
14736 	 this information on our own. This can be rather tricky to do in
14737 	 some cases.  For example, handling the following structure type
14738 	 definition when compiling for an i386/i486 target (which only
14739 	 aligns long long's to 32-bit boundaries) can be very tricky:
14740 
14741 	 struct S { int field1; long long field2:31; };
14742 
14743 	 Fortunately, there is a simple rule-of-thumb which can be used
14744 	 in such cases.  When compiling for an i386/i486, GCC will
14745 	 allocate 8 bytes for the structure shown above.  It decides to
14746 	 do this based upon one simple rule for bit-field allocation.
14747 	 GCC allocates each "containing object" for each bit-field at
14748 	 the first (i.e. lowest addressed) legitimate alignment boundary
14749 	 (based upon the required minimum alignment for the declared
14750 	 type of the field) which it can possibly use, subject to the
14751 	 condition that there is still enough available space remaining
14752 	 in the containing object (when allocated at the selected point)
14753 	 to fully accommodate all of the bits of the bit-field itself.
14754 
14755 	 This simple rule makes it obvious why GCC allocates 8 bytes for
14756 	 each object of the structure type shown above.  When looking
14757 	 for a place to allocate the "containing object" for `field2',
14758 	 the compiler simply tries to allocate a 64-bit "containing
14759 	 object" at each successive 32-bit boundary (starting at zero)
14760 	 until it finds a place to allocate that 64- bit field such that
14761 	 at least 31 contiguous (and previously unallocated) bits remain
14762 	 within that selected 64 bit field.  (As it turns out, for the
14763 	 example above, the compiler finds it is OK to allocate the
14764 	 "containing object" 64-bit field at bit-offset zero within the
14765 	 structure type.)
14766 
14767 	 Here we attempt to work backwards from the limited set of facts
14768 	 we're given, and we try to deduce from those facts, where GCC
14769 	 must have believed that the containing object started (within
14770 	 the structure type). The value we deduce is then used (by the
14771 	 callers of this routine) to generate DW_AT_location and
14772 	 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14773 	 the case of DW_AT_location, regular fields as well).  */
14774 
14775       /* Figure out the bit-distance from the start of the structure to
14776 	 the "deepest" bit of the bit-field.  */
14777       deepest_bitpos = bitpos_int + field_size_in_bits;
14778 
14779       /* This is the tricky part.  Use some fancy footwork to deduce
14780 	 where the lowest addressed bit of the containing object must
14781 	 be.  */
14782       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14783 
14784       /* Round up to type_align by default.  This works best for
14785 	 bitfields.  */
14786       object_offset_in_bits
14787 	= round_up_to_align (object_offset_in_bits, type_align_in_bits);
14788 
14789       if (object_offset_in_bits.ugt (bitpos_int))
14790 	{
14791 	  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14792 
14793 	  /* Round up to decl_align instead.  */
14794 	  object_offset_in_bits
14795 	    = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14796 	}
14797     }
14798   else
14799 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14800     object_offset_in_bits = bitpos_int;
14801 
14802   object_offset_in_bytes
14803     = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14804 				 true, TRUNC_DIV_EXPR);
14805   return object_offset_in_bytes.to_shwi ();
14806 }
14807 
14808 /* The following routines define various Dwarf attributes and any data
14809    associated with them.  */
14810 
14811 /* Add a location description attribute value to a DIE.
14812 
14813    This emits location attributes suitable for whole variables and
14814    whole parameters.  Note that the location attributes for struct fields are
14815    generated by the routine `data_member_location_attribute' below.  */
14816 
14817 static inline void
add_AT_location_description(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_list_ref descr)14818 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14819 			     dw_loc_list_ref descr)
14820 {
14821   if (descr == 0)
14822     return;
14823   if (single_element_loc_list_p (descr))
14824     add_AT_loc (die, attr_kind, descr->expr);
14825   else
14826     add_AT_loc_list (die, attr_kind, descr);
14827 }
14828 
14829 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14830 
14831 static void
add_accessibility_attribute(dw_die_ref die,tree decl)14832 add_accessibility_attribute (dw_die_ref die, tree decl)
14833 {
14834   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14835      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14836      the default has always been DW_ACCESS_public.  */
14837   if (TREE_PROTECTED (decl))
14838     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14839   else if (TREE_PRIVATE (decl))
14840     {
14841       if (dwarf_version == 2
14842 	  || die->die_parent == NULL
14843 	  || die->die_parent->die_tag != DW_TAG_class_type)
14844 	add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14845     }
14846   else if (dwarf_version > 2
14847 	   && die->die_parent
14848 	   && die->die_parent->die_tag == DW_TAG_class_type)
14849     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14850 }
14851 
14852 /* Attach the specialized form of location attribute used for data members of
14853    struct and union types.  In the special case of a FIELD_DECL node which
14854    represents a bit-field, the "offset" part of this special location
14855    descriptor must indicate the distance in bytes from the lowest-addressed
14856    byte of the containing struct or union type to the lowest-addressed byte of
14857    the "containing object" for the bit-field.  (See the `field_byte_offset'
14858    function above).
14859 
14860    For any given bit-field, the "containing object" is a hypothetical object
14861    (of some integral or enum type) within which the given bit-field lives.  The
14862    type of this hypothetical "containing object" is always the same as the
14863    declared type of the individual bit-field itself (for GCC anyway... the
14864    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14865    bytes) of the hypothetical "containing object" which will be given in the
14866    DW_AT_byte_size attribute for this bit-field.  (See the
14867    `byte_size_attribute' function below.)  It is also used when calculating the
14868    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14869    function below.)  */
14870 
14871 static void
add_data_member_location_attribute(dw_die_ref die,tree decl)14872 add_data_member_location_attribute (dw_die_ref die, tree decl)
14873 {
14874   HOST_WIDE_INT offset;
14875   dw_loc_descr_ref loc_descr = 0;
14876 
14877   if (TREE_CODE (decl) == TREE_BINFO)
14878     {
14879       /* We're working on the TAG_inheritance for a base class.  */
14880       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14881 	{
14882 	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14883 	     aren't at a fixed offset from all (sub)objects of the same
14884 	     type.  We need to extract the appropriate offset from our
14885 	     vtable.  The following dwarf expression means
14886 
14887 	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
14888 
14889 	     This is specific to the V3 ABI, of course.  */
14890 
14891 	  dw_loc_descr_ref tmp;
14892 
14893 	  /* Make a copy of the object address.  */
14894 	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
14895 	  add_loc_descr (&loc_descr, tmp);
14896 
14897 	  /* Extract the vtable address.  */
14898 	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
14899 	  add_loc_descr (&loc_descr, tmp);
14900 
14901 	  /* Calculate the address of the offset.  */
14902 	  offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14903 	  gcc_assert (offset < 0);
14904 
14905 	  tmp = int_loc_descriptor (-offset);
14906 	  add_loc_descr (&loc_descr, tmp);
14907 	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
14908 	  add_loc_descr (&loc_descr, tmp);
14909 
14910 	  /* Extract the offset.  */
14911 	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
14912 	  add_loc_descr (&loc_descr, tmp);
14913 
14914 	  /* Add it to the object address.  */
14915 	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
14916 	  add_loc_descr (&loc_descr, tmp);
14917 	}
14918       else
14919 	offset = tree_to_shwi (BINFO_OFFSET (decl));
14920     }
14921   else
14922     offset = field_byte_offset (decl);
14923 
14924   if (! loc_descr)
14925     {
14926       if (dwarf_version > 2)
14927 	{
14928 	  /* Don't need to output a location expression, just the constant. */
14929 	  if (offset < 0)
14930 	    add_AT_int (die, DW_AT_data_member_location, offset);
14931 	  else
14932 	    add_AT_unsigned (die, DW_AT_data_member_location, offset);
14933 	  return;
14934 	}
14935       else
14936 	{
14937 	  enum dwarf_location_atom op;
14938 
14939 	  /* The DWARF2 standard says that we should assume that the structure
14940 	     address is already on the stack, so we can specify a structure
14941 	     field address by using DW_OP_plus_uconst.  */
14942 	  op = DW_OP_plus_uconst;
14943 	  loc_descr = new_loc_descr (op, offset, 0);
14944 	}
14945     }
14946 
14947   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14948 }
14949 
14950 /* Writes integer values to dw_vec_const array.  */
14951 
14952 static void
insert_int(HOST_WIDE_INT val,unsigned int size,unsigned char * dest)14953 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14954 {
14955   while (size != 0)
14956     {
14957       *dest++ = val & 0xff;
14958       val >>= 8;
14959       --size;
14960     }
14961 }
14962 
14963 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14964 
14965 static HOST_WIDE_INT
extract_int(const unsigned char * src,unsigned int size)14966 extract_int (const unsigned char *src, unsigned int size)
14967 {
14968   HOST_WIDE_INT val = 0;
14969 
14970   src += size;
14971   while (size != 0)
14972     {
14973       val <<= 8;
14974       val |= *--src & 0xff;
14975       --size;
14976     }
14977   return val;
14978 }
14979 
14980 /* Writes double_int values to dw_vec_const array.  */
14981 
14982 static void
insert_double(double_int val,unsigned char * dest)14983 insert_double (double_int val, unsigned char *dest)
14984 {
14985   unsigned char *p0 = dest;
14986   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14987 
14988   if (WORDS_BIG_ENDIAN)
14989     {
14990       p0 = p1;
14991       p1 = dest;
14992     }
14993 
14994   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14995   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14996 }
14997 
14998 /* Writes floating point values to dw_vec_const array.  */
14999 
15000 static void
insert_float(const_rtx rtl,unsigned char * array)15001 insert_float (const_rtx rtl, unsigned char *array)
15002 {
15003   REAL_VALUE_TYPE rv;
15004   long val[4];
15005   int i;
15006 
15007   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15008   real_to_target (val, &rv, GET_MODE (rtl));
15009 
15010   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15011   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15012     {
15013       insert_int (val[i], 4, array);
15014       array += 4;
15015     }
15016 }
15017 
15018 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15019    does not have a "location" either in memory or in a register.  These
15020    things can arise in GNU C when a constant is passed as an actual parameter
15021    to an inlined function.  They can also arise in C++ where declared
15022    constants do not necessarily get memory "homes".  */
15023 
15024 static bool
add_const_value_attribute(dw_die_ref die,rtx rtl)15025 add_const_value_attribute (dw_die_ref die, rtx rtl)
15026 {
15027   switch (GET_CODE (rtl))
15028     {
15029     case CONST_INT:
15030       {
15031 	HOST_WIDE_INT val = INTVAL (rtl);
15032 
15033 	if (val < 0)
15034 	  add_AT_int (die, DW_AT_const_value, val);
15035 	else
15036 	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15037       }
15038       return true;
15039 
15040     case CONST_DOUBLE:
15041       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15042 	 floating-point constant.  A CONST_DOUBLE is used whenever the
15043 	 constant requires more than one word in order to be adequately
15044 	 represented.  */
15045       {
15046 	enum machine_mode mode = GET_MODE (rtl);
15047 
15048 	if (SCALAR_FLOAT_MODE_P (mode))
15049 	  {
15050 	    unsigned int length = GET_MODE_SIZE (mode);
15051 	    unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15052 
15053 	    insert_float (rtl, array);
15054 	    add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15055 	  }
15056 	else
15057 	  add_AT_double (die, DW_AT_const_value,
15058 			 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15059       }
15060       return true;
15061 
15062     case CONST_VECTOR:
15063       {
15064 	enum machine_mode mode = GET_MODE (rtl);
15065 	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15066 	unsigned int length = CONST_VECTOR_NUNITS (rtl);
15067 	unsigned char *array = (unsigned char *) ggc_alloc_atomic
15068 	  (length * elt_size);
15069 	unsigned int i;
15070 	unsigned char *p;
15071 
15072 	switch (GET_MODE_CLASS (mode))
15073 	  {
15074 	  case MODE_VECTOR_INT:
15075 	    for (i = 0, p = array; i < length; i++, p += elt_size)
15076 	      {
15077 		rtx elt = CONST_VECTOR_ELT (rtl, i);
15078 		double_int val = rtx_to_double_int (elt);
15079 
15080 		if (elt_size <= sizeof (HOST_WIDE_INT))
15081 		  insert_int (val.to_shwi (), elt_size, p);
15082 		else
15083 		  {
15084 		    gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15085 		    insert_double (val, p);
15086 		  }
15087 	      }
15088 	    break;
15089 
15090 	  case MODE_VECTOR_FLOAT:
15091 	    for (i = 0, p = array; i < length; i++, p += elt_size)
15092 	      {
15093 		rtx elt = CONST_VECTOR_ELT (rtl, i);
15094 		insert_float (elt, p);
15095 	      }
15096 	    break;
15097 
15098 	  default:
15099 	    gcc_unreachable ();
15100 	  }
15101 
15102 	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15103       }
15104       return true;
15105 
15106     case CONST_STRING:
15107       if (dwarf_version >= 4 || !dwarf_strict)
15108 	{
15109 	  dw_loc_descr_ref loc_result;
15110 	  resolve_one_addr (&rtl, NULL);
15111 	rtl_addr:
15112           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15113 	  add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15114 	  add_AT_loc (die, DW_AT_location, loc_result);
15115 	  vec_safe_push (used_rtx_array, rtl);
15116 	  return true;
15117 	}
15118       return false;
15119 
15120     case CONST:
15121       if (CONSTANT_P (XEXP (rtl, 0)))
15122 	return add_const_value_attribute (die, XEXP (rtl, 0));
15123       /* FALLTHROUGH */
15124     case SYMBOL_REF:
15125       if (!const_ok_for_output (rtl))
15126 	return false;
15127     case LABEL_REF:
15128       if (dwarf_version >= 4 || !dwarf_strict)
15129 	goto rtl_addr;
15130       return false;
15131 
15132     case PLUS:
15133       /* In cases where an inlined instance of an inline function is passed
15134 	 the address of an `auto' variable (which is local to the caller) we
15135 	 can get a situation where the DECL_RTL of the artificial local
15136 	 variable (for the inlining) which acts as a stand-in for the
15137 	 corresponding formal parameter (of the inline function) will look
15138 	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15139 	 exactly a compile-time constant expression, but it isn't the address
15140 	 of the (artificial) local variable either.  Rather, it represents the
15141 	 *value* which the artificial local variable always has during its
15142 	 lifetime.  We currently have no way to represent such quasi-constant
15143 	 values in Dwarf, so for now we just punt and generate nothing.  */
15144       return false;
15145 
15146     case HIGH:
15147     case CONST_FIXED:
15148       return false;
15149 
15150     case MEM:
15151       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15152 	  && MEM_READONLY_P (rtl)
15153 	  && GET_MODE (rtl) == BLKmode)
15154 	{
15155 	  add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15156 	  return true;
15157 	}
15158       return false;
15159 
15160     default:
15161       /* No other kinds of rtx should be possible here.  */
15162       gcc_unreachable ();
15163     }
15164   return false;
15165 }
15166 
15167 /* Determine whether the evaluation of EXPR references any variables
15168    or functions which aren't otherwise used (and therefore may not be
15169    output).  */
15170 static tree
reference_to_unused(tree * tp,int * walk_subtrees,void * data ATTRIBUTE_UNUSED)15171 reference_to_unused (tree * tp, int * walk_subtrees,
15172 		     void * data ATTRIBUTE_UNUSED)
15173 {
15174   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15175     *walk_subtrees = 0;
15176 
15177   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15178       && ! TREE_ASM_WRITTEN (*tp))
15179     return *tp;
15180   /* ???  The C++ FE emits debug information for using decls, so
15181      putting gcc_unreachable here falls over.  See PR31899.  For now
15182      be conservative.  */
15183   else if (!cgraph_global_info_ready
15184 	   && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15185     return *tp;
15186   else if (TREE_CODE (*tp) == VAR_DECL)
15187     {
15188       varpool_node *node = varpool_get_node (*tp);
15189       if (!node || !node->definition)
15190 	return *tp;
15191     }
15192   else if (TREE_CODE (*tp) == FUNCTION_DECL
15193 	   && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15194     {
15195       /* The call graph machinery must have finished analyzing,
15196          optimizing and gimplifying the CU by now.
15197 	 So if *TP has no call graph node associated
15198 	 to it, it means *TP will not be emitted.  */
15199       if (!cgraph_get_node (*tp))
15200 	return *tp;
15201     }
15202   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15203     return *tp;
15204 
15205   return NULL_TREE;
15206 }
15207 
15208 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15209    for use in a later add_const_value_attribute call.  */
15210 
15211 static rtx
rtl_for_decl_init(tree init,tree type)15212 rtl_for_decl_init (tree init, tree type)
15213 {
15214   rtx rtl = NULL_RTX;
15215 
15216   STRIP_NOPS (init);
15217 
15218   /* If a variable is initialized with a string constant without embedded
15219      zeros, build CONST_STRING.  */
15220   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15221     {
15222       tree enttype = TREE_TYPE (type);
15223       tree domain = TYPE_DOMAIN (type);
15224       enum machine_mode mode = TYPE_MODE (enttype);
15225 
15226       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15227 	  && domain
15228 	  && integer_zerop (TYPE_MIN_VALUE (domain))
15229 	  && compare_tree_int (TYPE_MAX_VALUE (domain),
15230 			       TREE_STRING_LENGTH (init) - 1) == 0
15231 	  && ((size_t) TREE_STRING_LENGTH (init)
15232 	      == strlen (TREE_STRING_POINTER (init)) + 1))
15233 	{
15234 	  rtl = gen_rtx_CONST_STRING (VOIDmode,
15235 				      ggc_strdup (TREE_STRING_POINTER (init)));
15236 	  rtl = gen_rtx_MEM (BLKmode, rtl);
15237 	  MEM_READONLY_P (rtl) = 1;
15238 	}
15239     }
15240   /* Other aggregates, and complex values, could be represented using
15241      CONCAT: FIXME!  */
15242   else if (AGGREGATE_TYPE_P (type)
15243 	   || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15244 	       && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15245 	   || TREE_CODE (type) == COMPLEX_TYPE)
15246     ;
15247   /* Vectors only work if their mode is supported by the target.
15248      FIXME: generic vectors ought to work too.  */
15249   else if (TREE_CODE (type) == VECTOR_TYPE
15250 	   && !VECTOR_MODE_P (TYPE_MODE (type)))
15251     ;
15252   /* If the initializer is something that we know will expand into an
15253      immediate RTL constant, expand it now.  We must be careful not to
15254      reference variables which won't be output.  */
15255   else if (initializer_constant_valid_p (init, type)
15256 	   && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15257     {
15258       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15259 	 possible.  */
15260       if (TREE_CODE (type) == VECTOR_TYPE)
15261 	switch (TREE_CODE (init))
15262 	  {
15263 	  case VECTOR_CST:
15264 	    break;
15265 	  case CONSTRUCTOR:
15266 	    if (TREE_CONSTANT (init))
15267 	      {
15268 		vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15269 		bool constant_p = true;
15270 		tree value;
15271 		unsigned HOST_WIDE_INT ix;
15272 
15273 		/* Even when ctor is constant, it might contain non-*_CST
15274 		   elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15275 		   belong into VECTOR_CST nodes.  */
15276 		FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15277 		  if (!CONSTANT_CLASS_P (value))
15278 		    {
15279 		      constant_p = false;
15280 		      break;
15281 		    }
15282 
15283 		if (constant_p)
15284 		  {
15285 		    init = build_vector_from_ctor (type, elts);
15286 		    break;
15287 		  }
15288 	      }
15289 	    /* FALLTHRU */
15290 
15291 	  default:
15292 	    return NULL;
15293 	  }
15294 
15295       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15296 
15297       /* If expand_expr returns a MEM, it wasn't immediate.  */
15298       gcc_assert (!rtl || !MEM_P (rtl));
15299     }
15300 
15301   return rtl;
15302 }
15303 
15304 /* Generate RTL for the variable DECL to represent its location.  */
15305 
15306 static rtx
rtl_for_decl_location(tree decl)15307 rtl_for_decl_location (tree decl)
15308 {
15309   rtx rtl;
15310 
15311   /* Here we have to decide where we are going to say the parameter "lives"
15312      (as far as the debugger is concerned).  We only have a couple of
15313      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15314 
15315      DECL_RTL normally indicates where the parameter lives during most of the
15316      activation of the function.  If optimization is enabled however, this
15317      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15318      that the parameter doesn't really live anywhere (as far as the code
15319      generation parts of GCC are concerned) during most of the function's
15320      activation.  That will happen (for example) if the parameter is never
15321      referenced within the function.
15322 
15323      We could just generate a location descriptor here for all non-NULL
15324      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15325      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15326      where DECL_RTL is NULL or is a pseudo-reg.
15327 
15328      Note however that we can only get away with using DECL_INCOMING_RTL as
15329      a backup substitute for DECL_RTL in certain limited cases.  In cases
15330      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15331      we can be sure that the parameter was passed using the same type as it is
15332      declared to have within the function, and that its DECL_INCOMING_RTL
15333      points us to a place where a value of that type is passed.
15334 
15335      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15336      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15337      because in these cases DECL_INCOMING_RTL points us to a value of some
15338      type which is *different* from the type of the parameter itself.  Thus,
15339      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15340      such cases, the debugger would end up (for example) trying to fetch a
15341      `float' from a place which actually contains the first part of a
15342      `double'.  That would lead to really incorrect and confusing
15343      output at debug-time.
15344 
15345      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15346      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15347      are a couple of exceptions however.  On little-endian machines we can
15348      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15349      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15350      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15351      when (on a little-endian machine) a non-prototyped function has a
15352      parameter declared to be of type `short' or `char'.  In such cases,
15353      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15354      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15355      passed `int' value.  If the debugger then uses that address to fetch
15356      a `short' or a `char' (on a little-endian machine) the result will be
15357      the correct data, so we allow for such exceptional cases below.
15358 
15359      Note that our goal here is to describe the place where the given formal
15360      parameter lives during most of the function's activation (i.e. between the
15361      end of the prologue and the start of the epilogue).  We'll do that as best
15362      as we can. Note however that if the given formal parameter is modified
15363      sometime during the execution of the function, then a stack backtrace (at
15364      debug-time) will show the function as having been called with the *new*
15365      value rather than the value which was originally passed in.  This happens
15366      rarely enough that it is not a major problem, but it *is* a problem, and
15367      I'd like to fix it.
15368 
15369      A future version of dwarf2out.c may generate two additional attributes for
15370      any given DW_TAG_formal_parameter DIE which will describe the "passed
15371      type" and the "passed location" for the given formal parameter in addition
15372      to the attributes we now generate to indicate the "declared type" and the
15373      "active location" for each parameter.  This additional set of attributes
15374      could be used by debuggers for stack backtraces. Separately, note that
15375      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15376      This happens (for example) for inlined-instances of inline function formal
15377      parameters which are never referenced.  This really shouldn't be
15378      happening.  All PARM_DECL nodes should get valid non-NULL
15379      DECL_INCOMING_RTL values.  FIXME.  */
15380 
15381   /* Use DECL_RTL as the "location" unless we find something better.  */
15382   rtl = DECL_RTL_IF_SET (decl);
15383 
15384   /* When generating abstract instances, ignore everything except
15385      constants, symbols living in memory, and symbols living in
15386      fixed registers.  */
15387   if (! reload_completed)
15388     {
15389       if (rtl
15390 	  && (CONSTANT_P (rtl)
15391 	      || (MEM_P (rtl)
15392 	          && CONSTANT_P (XEXP (rtl, 0)))
15393 	      || (REG_P (rtl)
15394 	          && TREE_CODE (decl) == VAR_DECL
15395 		  && TREE_STATIC (decl))))
15396 	{
15397 	  rtl = targetm.delegitimize_address (rtl);
15398 	  return rtl;
15399 	}
15400       rtl = NULL_RTX;
15401     }
15402   else if (TREE_CODE (decl) == PARM_DECL)
15403     {
15404       if (rtl == NULL_RTX
15405 	  || is_pseudo_reg (rtl)
15406 	  || (MEM_P (rtl)
15407 	      && is_pseudo_reg (XEXP (rtl, 0))
15408 	      && DECL_INCOMING_RTL (decl)
15409 	      && MEM_P (DECL_INCOMING_RTL (decl))
15410 	      && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15411 	{
15412 	  tree declared_type = TREE_TYPE (decl);
15413 	  tree passed_type = DECL_ARG_TYPE (decl);
15414 	  enum machine_mode dmode = TYPE_MODE (declared_type);
15415 	  enum machine_mode pmode = TYPE_MODE (passed_type);
15416 
15417 	  /* This decl represents a formal parameter which was optimized out.
15418 	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15419 	     all cases where (rtl == NULL_RTX) just below.  */
15420 	  if (dmode == pmode)
15421 	    rtl = DECL_INCOMING_RTL (decl);
15422 	  else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15423 		   && SCALAR_INT_MODE_P (dmode)
15424 		   && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15425 		   && DECL_INCOMING_RTL (decl))
15426 	    {
15427 	      rtx inc = DECL_INCOMING_RTL (decl);
15428 	      if (REG_P (inc))
15429 		rtl = inc;
15430 	      else if (MEM_P (inc))
15431 		{
15432 		  if (BYTES_BIG_ENDIAN)
15433 		    rtl = adjust_address_nv (inc, dmode,
15434 					     GET_MODE_SIZE (pmode)
15435 					     - GET_MODE_SIZE (dmode));
15436 		  else
15437 		    rtl = inc;
15438 		}
15439 	    }
15440 	}
15441 
15442       /* If the parm was passed in registers, but lives on the stack, then
15443 	 make a big endian correction if the mode of the type of the
15444 	 parameter is not the same as the mode of the rtl.  */
15445       /* ??? This is the same series of checks that are made in dbxout.c before
15446 	 we reach the big endian correction code there.  It isn't clear if all
15447 	 of these checks are necessary here, but keeping them all is the safe
15448 	 thing to do.  */
15449       else if (MEM_P (rtl)
15450 	       && XEXP (rtl, 0) != const0_rtx
15451 	       && ! CONSTANT_P (XEXP (rtl, 0))
15452 	       /* Not passed in memory.  */
15453 	       && !MEM_P (DECL_INCOMING_RTL (decl))
15454 	       /* Not passed by invisible reference.  */
15455 	       && (!REG_P (XEXP (rtl, 0))
15456 		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15457 		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15458 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15459 		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15460 #endif
15461 		     )
15462 	       /* Big endian correction check.  */
15463 	       && BYTES_BIG_ENDIAN
15464 	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15465 	       && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15466 		   < UNITS_PER_WORD))
15467 	{
15468 	  enum machine_mode addr_mode = get_address_mode (rtl);
15469 	  int offset = (UNITS_PER_WORD
15470 			- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15471 
15472 	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15473 			     plus_constant (addr_mode, XEXP (rtl, 0), offset));
15474 	}
15475     }
15476   else if (TREE_CODE (decl) == VAR_DECL
15477 	   && rtl
15478 	   && MEM_P (rtl)
15479 	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15480 	   && BYTES_BIG_ENDIAN)
15481     {
15482       enum machine_mode addr_mode = get_address_mode (rtl);
15483       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15484       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15485 
15486       /* If a variable is declared "register" yet is smaller than
15487 	 a register, then if we store the variable to memory, it
15488 	 looks like we're storing a register-sized value, when in
15489 	 fact we are not.  We need to adjust the offset of the
15490 	 storage location to reflect the actual value's bytes,
15491 	 else gdb will not be able to display it.  */
15492       if (rsize > dsize)
15493 	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15494 			   plus_constant (addr_mode, XEXP (rtl, 0),
15495 					  rsize - dsize));
15496     }
15497 
15498   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15499      and will have been substituted directly into all expressions that use it.
15500      C does not have such a concept, but C++ and other languages do.  */
15501   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15502     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15503 
15504   if (rtl)
15505     rtl = targetm.delegitimize_address (rtl);
15506 
15507   /* If we don't look past the constant pool, we risk emitting a
15508      reference to a constant pool entry that isn't referenced from
15509      code, and thus is not emitted.  */
15510   if (rtl)
15511     rtl = avoid_constant_pool_reference (rtl);
15512 
15513   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15514      in the current CU, resolve_addr will remove the expression referencing
15515      it.  */
15516   if (rtl == NULL_RTX
15517       && TREE_CODE (decl) == VAR_DECL
15518       && !DECL_EXTERNAL (decl)
15519       && TREE_STATIC (decl)
15520       && DECL_NAME (decl)
15521       && !DECL_HARD_REGISTER (decl)
15522       && DECL_MODE (decl) != VOIDmode)
15523     {
15524       rtl = make_decl_rtl_for_debug (decl);
15525       if (!MEM_P (rtl)
15526 	  || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15527 	  || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15528 	rtl = NULL_RTX;
15529     }
15530 
15531   return rtl;
15532 }
15533 
15534 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15535    returned.  If so, the decl for the COMMON block is returned, and the
15536    value is the offset into the common block for the symbol.  */
15537 
15538 static tree
fortran_common(tree decl,HOST_WIDE_INT * value)15539 fortran_common (tree decl, HOST_WIDE_INT *value)
15540 {
15541   tree val_expr, cvar;
15542   enum machine_mode mode;
15543   HOST_WIDE_INT bitsize, bitpos;
15544   tree offset;
15545   int unsignedp, volatilep = 0;
15546 
15547   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15548      it does not have a value (the offset into the common area), or if it
15549      is thread local (as opposed to global) then it isn't common, and shouldn't
15550      be handled as such.  */
15551   if (TREE_CODE (decl) != VAR_DECL
15552       || !TREE_STATIC (decl)
15553       || !DECL_HAS_VALUE_EXPR_P (decl)
15554       || !is_fortran ())
15555     return NULL_TREE;
15556 
15557   val_expr = DECL_VALUE_EXPR (decl);
15558   if (TREE_CODE (val_expr) != COMPONENT_REF)
15559     return NULL_TREE;
15560 
15561   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15562 			      &mode, &unsignedp, &volatilep, true);
15563 
15564   if (cvar == NULL_TREE
15565       || TREE_CODE (cvar) != VAR_DECL
15566       || DECL_ARTIFICIAL (cvar)
15567       || !TREE_PUBLIC (cvar))
15568     return NULL_TREE;
15569 
15570   *value = 0;
15571   if (offset != NULL)
15572     {
15573       if (!tree_fits_shwi_p (offset))
15574 	return NULL_TREE;
15575       *value = tree_to_shwi (offset);
15576     }
15577   if (bitpos != 0)
15578     *value += bitpos / BITS_PER_UNIT;
15579 
15580   return cvar;
15581 }
15582 
15583 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15584    data attribute for a variable or a parameter.  We generate the
15585    DW_AT_const_value attribute only in those cases where the given variable
15586    or parameter does not have a true "location" either in memory or in a
15587    register.  This can happen (for example) when a constant is passed as an
15588    actual argument in a call to an inline function.  (It's possible that
15589    these things can crop up in other ways also.)  Note that one type of
15590    constant value which can be passed into an inlined function is a constant
15591    pointer.  This can happen for example if an actual argument in an inlined
15592    function call evaluates to a compile-time constant address.
15593 
15594    CACHE_P is true if it is worth caching the location list for DECL,
15595    so that future calls can reuse it rather than regenerate it from scratch.
15596    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15597    since we will need to refer to them each time the function is inlined.  */
15598 
15599 static bool
add_location_or_const_value_attribute(dw_die_ref die,tree decl,bool cache_p,enum dwarf_attribute attr)15600 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15601 				       enum dwarf_attribute attr)
15602 {
15603   rtx rtl;
15604   dw_loc_list_ref list;
15605   var_loc_list *loc_list;
15606   cached_dw_loc_list *cache;
15607   void **slot;
15608 
15609   if (TREE_CODE (decl) == ERROR_MARK)
15610     return false;
15611 
15612   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15613 	      || TREE_CODE (decl) == RESULT_DECL);
15614 
15615   /* Try to get some constant RTL for this decl, and use that as the value of
15616      the location.  */
15617 
15618   rtl = rtl_for_decl_location (decl);
15619   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15620       && add_const_value_attribute (die, rtl))
15621     return true;
15622 
15623   /* See if we have single element location list that is equivalent to
15624      a constant value.  That way we are better to use add_const_value_attribute
15625      rather than expanding constant value equivalent.  */
15626   loc_list = lookup_decl_loc (decl);
15627   if (loc_list
15628       && loc_list->first
15629       && loc_list->first->next == NULL
15630       && NOTE_P (loc_list->first->loc)
15631       && NOTE_VAR_LOCATION (loc_list->first->loc)
15632       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15633     {
15634       struct var_loc_node *node;
15635 
15636       node = loc_list->first;
15637       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15638       if (GET_CODE (rtl) == EXPR_LIST)
15639 	rtl = XEXP (rtl, 0);
15640       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15641 	  && add_const_value_attribute (die, rtl))
15642 	 return true;
15643     }
15644   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15645      list several times.  See if we've already cached the contents.  */
15646   list = NULL;
15647   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15648     cache_p = false;
15649   if (cache_p)
15650     {
15651       cache = (cached_dw_loc_list *)
15652 	htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15653       if (cache)
15654 	list = cache->loc_list;
15655     }
15656   if (list == NULL)
15657     {
15658       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15659       /* It is usually worth caching this result if the decl is from
15660 	 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
15661       if (cache_p && list && list->dw_loc_next)
15662 	{
15663 	  slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15664 					   DECL_UID (decl), INSERT);
15665 	  cache = ggc_alloc_cleared_cached_dw_loc_list ();
15666 	  cache->decl_id = DECL_UID (decl);
15667 	  cache->loc_list = list;
15668 	  *slot = cache;
15669 	}
15670     }
15671   if (list)
15672     {
15673       add_AT_location_description (die, attr, list);
15674       return true;
15675     }
15676   /* None of that worked, so it must not really have a location;
15677      try adding a constant value attribute from the DECL_INITIAL.  */
15678   return tree_add_const_value_attribute_for_decl (die, decl);
15679 }
15680 
15681 /* Add VARIABLE and DIE into deferred locations list.  */
15682 
15683 static void
defer_location(tree variable,dw_die_ref die)15684 defer_location (tree variable, dw_die_ref die)
15685 {
15686   deferred_locations entry;
15687   entry.variable = variable;
15688   entry.die = die;
15689   vec_safe_push (deferred_locations_list, entry);
15690 }
15691 
15692 /* Helper function for tree_add_const_value_attribute.  Natively encode
15693    initializer INIT into an array.  Return true if successful.  */
15694 
15695 static bool
native_encode_initializer(tree init,unsigned char * array,int size)15696 native_encode_initializer (tree init, unsigned char *array, int size)
15697 {
15698   tree type;
15699 
15700   if (init == NULL_TREE)
15701     return false;
15702 
15703   STRIP_NOPS (init);
15704   switch (TREE_CODE (init))
15705     {
15706     case STRING_CST:
15707       type = TREE_TYPE (init);
15708       if (TREE_CODE (type) == ARRAY_TYPE)
15709 	{
15710 	  tree enttype = TREE_TYPE (type);
15711 	  enum machine_mode mode = TYPE_MODE (enttype);
15712 
15713 	  if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15714 	    return false;
15715 	  if (int_size_in_bytes (type) != size)
15716 	    return false;
15717 	  if (size > TREE_STRING_LENGTH (init))
15718 	    {
15719 	      memcpy (array, TREE_STRING_POINTER (init),
15720 		      TREE_STRING_LENGTH (init));
15721 	      memset (array + TREE_STRING_LENGTH (init),
15722 		      '\0', size - TREE_STRING_LENGTH (init));
15723 	    }
15724 	  else
15725 	    memcpy (array, TREE_STRING_POINTER (init), size);
15726 	  return true;
15727 	}
15728       return false;
15729     case CONSTRUCTOR:
15730       type = TREE_TYPE (init);
15731       if (int_size_in_bytes (type) != size)
15732 	return false;
15733       if (TREE_CODE (type) == ARRAY_TYPE)
15734 	{
15735 	  HOST_WIDE_INT min_index;
15736 	  unsigned HOST_WIDE_INT cnt;
15737 	  int curpos = 0, fieldsize;
15738 	  constructor_elt *ce;
15739 
15740 	  if (TYPE_DOMAIN (type) == NULL_TREE
15741 	      || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15742 	    return false;
15743 
15744 	  fieldsize = int_size_in_bytes (TREE_TYPE (type));
15745 	  if (fieldsize <= 0)
15746 	    return false;
15747 
15748 	  min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15749 	  memset (array, '\0', size);
15750 	  FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15751 	    {
15752 	      tree val = ce->value;
15753 	      tree index = ce->index;
15754 	      int pos = curpos;
15755 	      if (index && TREE_CODE (index) == RANGE_EXPR)
15756 		pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15757 		      * fieldsize;
15758 	      else if (index)
15759 		pos = (tree_to_shwi (index) - min_index) * fieldsize;
15760 
15761 	      if (val)
15762 		{
15763 		  STRIP_NOPS (val);
15764 		  if (!native_encode_initializer (val, array + pos, fieldsize))
15765 		    return false;
15766 		}
15767 	      curpos = pos + fieldsize;
15768 	      if (index && TREE_CODE (index) == RANGE_EXPR)
15769 		{
15770 		  int count = tree_to_shwi (TREE_OPERAND (index, 1))
15771 			      - tree_to_shwi (TREE_OPERAND (index, 0));
15772 		  while (count-- > 0)
15773 		    {
15774 		      if (val)
15775 			memcpy (array + curpos, array + pos, fieldsize);
15776 		      curpos += fieldsize;
15777 		    }
15778 		}
15779 	      gcc_assert (curpos <= size);
15780 	    }
15781 	  return true;
15782 	}
15783       else if (TREE_CODE (type) == RECORD_TYPE
15784 	       || TREE_CODE (type) == UNION_TYPE)
15785 	{
15786 	  tree field = NULL_TREE;
15787 	  unsigned HOST_WIDE_INT cnt;
15788 	  constructor_elt *ce;
15789 
15790 	  if (int_size_in_bytes (type) != size)
15791 	    return false;
15792 
15793 	  if (TREE_CODE (type) == RECORD_TYPE)
15794 	    field = TYPE_FIELDS (type);
15795 
15796 	  FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15797 	    {
15798 	      tree val = ce->value;
15799 	      int pos, fieldsize;
15800 
15801 	      if (ce->index != 0)
15802 		field = ce->index;
15803 
15804 	      if (val)
15805 		STRIP_NOPS (val);
15806 
15807 	      if (field == NULL_TREE || DECL_BIT_FIELD (field))
15808 		return false;
15809 
15810 	      if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15811 		  && TYPE_DOMAIN (TREE_TYPE (field))
15812 		  && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15813 		return false;
15814 	      else if (DECL_SIZE_UNIT (field) == NULL_TREE
15815 		       || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15816 		return false;
15817 	      fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15818 	      pos = int_byte_position (field);
15819 	      gcc_assert (pos + fieldsize <= size);
15820 	      if (val
15821 		  && !native_encode_initializer (val, array + pos, fieldsize))
15822 		return false;
15823 	    }
15824 	  return true;
15825 	}
15826       return false;
15827     case VIEW_CONVERT_EXPR:
15828     case NON_LVALUE_EXPR:
15829       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15830     default:
15831       return native_encode_expr (init, array, size) == size;
15832     }
15833 }
15834 
15835 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15836    attribute is the const value T.  */
15837 
15838 static bool
tree_add_const_value_attribute(dw_die_ref die,tree t)15839 tree_add_const_value_attribute (dw_die_ref die, tree t)
15840 {
15841   tree init;
15842   tree type = TREE_TYPE (t);
15843   rtx rtl;
15844 
15845   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15846     return false;
15847 
15848   init = t;
15849   gcc_assert (!DECL_P (init));
15850 
15851   rtl = rtl_for_decl_init (init, type);
15852   if (rtl)
15853     return add_const_value_attribute (die, rtl);
15854   /* If the host and target are sane, try harder.  */
15855   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15856 	   && initializer_constant_valid_p (init, type))
15857     {
15858       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15859       if (size > 0 && (int) size == size)
15860 	{
15861 	  unsigned char *array = (unsigned char *)
15862 	    ggc_alloc_cleared_atomic (size);
15863 
15864 	  if (native_encode_initializer (init, array, size))
15865 	    {
15866 	      add_AT_vec (die, DW_AT_const_value, size, 1, array);
15867 	      return true;
15868 	    }
15869 	  ggc_free (array);
15870 	}
15871     }
15872   return false;
15873 }
15874 
15875 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15876    attribute is the const value of T, where T is an integral constant
15877    variable with static storage duration
15878    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15879 
15880 static bool
tree_add_const_value_attribute_for_decl(dw_die_ref var_die,tree decl)15881 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15882 {
15883 
15884   if (!decl
15885       || (TREE_CODE (decl) != VAR_DECL
15886 	  && TREE_CODE (decl) != CONST_DECL)
15887       || (TREE_CODE (decl) == VAR_DECL
15888 	  && !TREE_STATIC (decl)))
15889     return false;
15890 
15891     if (TREE_READONLY (decl)
15892 	&& ! TREE_THIS_VOLATILE (decl)
15893 	&& DECL_INITIAL (decl))
15894       /* OK */;
15895     else
15896       return false;
15897 
15898   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15899   if (get_AT (var_die, DW_AT_const_value))
15900     return false;
15901 
15902   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15903 }
15904 
15905 /* Convert the CFI instructions for the current function into a
15906    location list.  This is used for DW_AT_frame_base when we targeting
15907    a dwarf2 consumer that does not support the dwarf3
15908    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15909    expressions.  */
15910 
15911 static dw_loc_list_ref
convert_cfa_to_fb_loc_list(HOST_WIDE_INT offset)15912 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15913 {
15914   int ix;
15915   dw_fde_ref fde;
15916   dw_loc_list_ref list, *list_tail;
15917   dw_cfi_ref cfi;
15918   dw_cfa_location last_cfa, next_cfa;
15919   const char *start_label, *last_label, *section;
15920   dw_cfa_location remember;
15921 
15922   fde = cfun->fde;
15923   gcc_assert (fde != NULL);
15924 
15925   section = secname_for_decl (current_function_decl);
15926   list_tail = &list;
15927   list = NULL;
15928 
15929   memset (&next_cfa, 0, sizeof (next_cfa));
15930   next_cfa.reg = INVALID_REGNUM;
15931   remember = next_cfa;
15932 
15933   start_label = fde->dw_fde_begin;
15934 
15935   /* ??? Bald assumption that the CIE opcode list does not contain
15936      advance opcodes.  */
15937   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15938     lookup_cfa_1 (cfi, &next_cfa, &remember);
15939 
15940   last_cfa = next_cfa;
15941   last_label = start_label;
15942 
15943   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15944     {
15945       /* If the first partition contained no CFI adjustments, the
15946 	 CIE opcodes apply to the whole first partition.  */
15947       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15948 				 fde->dw_fde_begin, fde->dw_fde_end, section);
15949       list_tail =&(*list_tail)->dw_loc_next;
15950       start_label = last_label = fde->dw_fde_second_begin;
15951     }
15952 
15953   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15954     {
15955       switch (cfi->dw_cfi_opc)
15956 	{
15957 	case DW_CFA_set_loc:
15958 	case DW_CFA_advance_loc1:
15959 	case DW_CFA_advance_loc2:
15960 	case DW_CFA_advance_loc4:
15961 	  if (!cfa_equal_p (&last_cfa, &next_cfa))
15962 	    {
15963 	      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15964 					 start_label, last_label, section);
15965 
15966 	      list_tail = &(*list_tail)->dw_loc_next;
15967 	      last_cfa = next_cfa;
15968 	      start_label = last_label;
15969 	    }
15970 	  last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15971 	  break;
15972 
15973 	case DW_CFA_advance_loc:
15974 	  /* The encoding is complex enough that we should never emit this.  */
15975 	  gcc_unreachable ();
15976 
15977 	default:
15978 	  lookup_cfa_1 (cfi, &next_cfa, &remember);
15979 	  break;
15980 	}
15981       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15982 	{
15983 	  if (!cfa_equal_p (&last_cfa, &next_cfa))
15984 	    {
15985 	      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15986 					 start_label, last_label, section);
15987 
15988 	      list_tail = &(*list_tail)->dw_loc_next;
15989 	      last_cfa = next_cfa;
15990 	      start_label = last_label;
15991 	    }
15992 	  *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15993 				     start_label, fde->dw_fde_end, section);
15994 	  list_tail = &(*list_tail)->dw_loc_next;
15995 	  start_label = last_label = fde->dw_fde_second_begin;
15996 	}
15997     }
15998 
15999   if (!cfa_equal_p (&last_cfa, &next_cfa))
16000     {
16001       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16002 				 start_label, last_label, section);
16003       list_tail = &(*list_tail)->dw_loc_next;
16004       start_label = last_label;
16005     }
16006 
16007   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16008 			     start_label,
16009 			     fde->dw_fde_second_begin
16010 			     ? fde->dw_fde_second_end : fde->dw_fde_end,
16011 			     section);
16012 
16013   if (list && list->dw_loc_next)
16014     gen_llsym (list);
16015 
16016   return list;
16017 }
16018 
16019 /* Compute a displacement from the "steady-state frame pointer" to the
16020    frame base (often the same as the CFA), and store it in
16021    frame_pointer_fb_offset.  OFFSET is added to the displacement
16022    before the latter is negated.  */
16023 
16024 static void
compute_frame_pointer_to_fb_displacement(HOST_WIDE_INT offset)16025 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16026 {
16027   rtx reg, elim;
16028 
16029 #ifdef FRAME_POINTER_CFA_OFFSET
16030   reg = frame_pointer_rtx;
16031   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16032 #else
16033   reg = arg_pointer_rtx;
16034   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16035 #endif
16036 
16037   elim = (ira_use_lra_p
16038 	  ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16039 	  : eliminate_regs (reg, VOIDmode, NULL_RTX));
16040   if (GET_CODE (elim) == PLUS)
16041     {
16042       offset += INTVAL (XEXP (elim, 1));
16043       elim = XEXP (elim, 0);
16044     }
16045 
16046   frame_pointer_fb_offset = -offset;
16047 
16048   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16049      in which to eliminate.  This is because it's stack pointer isn't
16050      directly accessible as a register within the ISA.  To work around
16051      this, assume that while we cannot provide a proper value for
16052      frame_pointer_fb_offset, we won't need one either.  */
16053   frame_pointer_fb_offset_valid
16054     = ((SUPPORTS_STACK_ALIGNMENT
16055 	&& (elim == hard_frame_pointer_rtx
16056 	    || elim == stack_pointer_rtx))
16057        || elim == (frame_pointer_needed
16058 		   ? hard_frame_pointer_rtx
16059 		   : stack_pointer_rtx));
16060 }
16061 
16062 /* Generate a DW_AT_name attribute given some string value to be included as
16063    the value of the attribute.  */
16064 
16065 static void
add_name_attribute(dw_die_ref die,const char * name_string)16066 add_name_attribute (dw_die_ref die, const char *name_string)
16067 {
16068   if (name_string != NULL && *name_string != 0)
16069     {
16070       if (demangle_name_func)
16071 	name_string = (*demangle_name_func) (name_string);
16072 
16073       add_AT_string (die, DW_AT_name, name_string);
16074     }
16075 }
16076 
16077 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16078    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16079    of TYPE accordingly.
16080 
16081    ??? This is a temporary measure until after we're able to generate
16082    regular DWARF for the complex Ada type system.  */
16083 
16084 static void
add_gnat_descriptive_type_attribute(dw_die_ref die,tree type,dw_die_ref context_die)16085 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16086 				     dw_die_ref context_die)
16087 {
16088   tree dtype;
16089   dw_die_ref dtype_die;
16090 
16091   if (!lang_hooks.types.descriptive_type)
16092     return;
16093 
16094   dtype = lang_hooks.types.descriptive_type (type);
16095   if (!dtype)
16096     return;
16097 
16098   dtype_die = lookup_type_die (dtype);
16099   if (!dtype_die)
16100     {
16101       gen_type_die (dtype, context_die);
16102       dtype_die = lookup_type_die (dtype);
16103       gcc_assert (dtype_die);
16104     }
16105 
16106   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16107 }
16108 
16109 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16110 
16111 static const char *
comp_dir_string(void)16112 comp_dir_string (void)
16113 {
16114   const char *wd;
16115   char *wd1;
16116   static const char *cached_wd = NULL;
16117 
16118   if (cached_wd != NULL)
16119     return cached_wd;
16120 
16121   wd = get_src_pwd ();
16122   if (wd == NULL)
16123     return NULL;
16124 
16125   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16126     {
16127       int wdlen;
16128 
16129       wdlen = strlen (wd);
16130       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16131       strcpy (wd1, wd);
16132       wd1 [wdlen] = DIR_SEPARATOR;
16133       wd1 [wdlen + 1] = 0;
16134       wd = wd1;
16135     }
16136 
16137   cached_wd = remap_debug_filename (wd);
16138   return cached_wd;
16139 }
16140 
16141 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16142 
16143 static void
add_comp_dir_attribute(dw_die_ref die)16144 add_comp_dir_attribute (dw_die_ref die)
16145 {
16146   const char * wd = comp_dir_string ();
16147   if (wd != NULL)
16148     add_AT_string (die, DW_AT_comp_dir, wd);
16149 }
16150 
16151 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16152    default.  */
16153 
16154 static int
lower_bound_default(void)16155 lower_bound_default (void)
16156 {
16157   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16158     {
16159     case DW_LANG_C:
16160     case DW_LANG_C89:
16161     case DW_LANG_C99:
16162     case DW_LANG_C_plus_plus:
16163     case DW_LANG_ObjC:
16164     case DW_LANG_ObjC_plus_plus:
16165     case DW_LANG_Java:
16166       return 0;
16167     case DW_LANG_Fortran77:
16168     case DW_LANG_Fortran90:
16169     case DW_LANG_Fortran95:
16170       return 1;
16171     case DW_LANG_UPC:
16172     case DW_LANG_D:
16173     case DW_LANG_Python:
16174       return dwarf_version >= 4 ? 0 : -1;
16175     case DW_LANG_Ada95:
16176     case DW_LANG_Ada83:
16177     case DW_LANG_Cobol74:
16178     case DW_LANG_Cobol85:
16179     case DW_LANG_Pascal83:
16180     case DW_LANG_Modula2:
16181     case DW_LANG_PLI:
16182       return dwarf_version >= 4 ? 1 : -1;
16183     default:
16184       return -1;
16185     }
16186 }
16187 
16188 /* Given a tree node describing an array bound (either lower or upper) output
16189    a representation for that bound.  */
16190 
16191 static void
add_bound_info(dw_die_ref subrange_die,enum dwarf_attribute bound_attr,tree bound)16192 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16193 {
16194   switch (TREE_CODE (bound))
16195     {
16196     case ERROR_MARK:
16197       return;
16198 
16199     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
16200     case INTEGER_CST:
16201       {
16202 	unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16203 	int dflt;
16204 
16205 	/* Use the default if possible.  */
16206 	if (bound_attr == DW_AT_lower_bound
16207 	    && tree_fits_shwi_p (bound)
16208 	    && (dflt = lower_bound_default ()) != -1
16209 	    && tree_to_shwi (bound) == dflt)
16210 	  ;
16211 
16212 	/* Otherwise represent the bound as an unsigned value with the
16213 	   precision of its type.  The precision and signedness of the
16214 	   type will be necessary to re-interpret it unambiguously.  */
16215 	else if (prec < HOST_BITS_PER_WIDE_INT)
16216 	  {
16217 	    unsigned HOST_WIDE_INT mask
16218 	      = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16219 	    add_AT_unsigned (subrange_die, bound_attr,
16220 		  	     TREE_INT_CST_LOW (bound) & mask);
16221 	  }
16222 	else if (prec == HOST_BITS_PER_WIDE_INT
16223 		 || TREE_INT_CST_HIGH (bound) == 0)
16224 	  add_AT_unsigned (subrange_die, bound_attr,
16225 		  	   TREE_INT_CST_LOW (bound));
16226 	else
16227 	  add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16228 		         TREE_INT_CST_LOW (bound));
16229       }
16230       break;
16231 
16232     CASE_CONVERT:
16233     case VIEW_CONVERT_EXPR:
16234       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16235       break;
16236 
16237     case SAVE_EXPR:
16238       break;
16239 
16240     case VAR_DECL:
16241     case PARM_DECL:
16242     case RESULT_DECL:
16243       {
16244 	dw_die_ref decl_die = lookup_decl_die (bound);
16245 
16246 	/* ??? Can this happen, or should the variable have been bound
16247 	   first?  Probably it can, since I imagine that we try to create
16248 	   the types of parameters in the order in which they exist in
16249 	   the list, and won't have created a forward reference to a
16250 	   later parameter.  */
16251 	if (decl_die != NULL)
16252 	  {
16253 	    add_AT_die_ref (subrange_die, bound_attr, decl_die);
16254 	    break;
16255 	  }
16256       }
16257       /* FALLTHRU */
16258 
16259     default:
16260       {
16261 	/* Otherwise try to create a stack operation procedure to
16262 	   evaluate the value of the array bound.  */
16263 
16264 	dw_die_ref ctx, decl_die;
16265 	dw_loc_list_ref list;
16266 
16267 	list = loc_list_from_tree (bound, 2);
16268 	if (list == NULL || single_element_loc_list_p (list))
16269 	  {
16270 	    /* If DW_AT_*bound is not a reference nor constant, it is
16271 	       a DWARF expression rather than location description.
16272 	       For that loc_list_from_tree (bound, 0) is needed.
16273 	       If that fails to give a single element list,
16274 	       fall back to outputting this as a reference anyway.  */
16275 	    dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16276 	    if (list2 && single_element_loc_list_p (list2))
16277 	      {
16278 		add_AT_loc (subrange_die, bound_attr, list2->expr);
16279 		break;
16280 	      }
16281 	  }
16282 	if (list == NULL)
16283 	  break;
16284 
16285 	if (current_function_decl == 0)
16286 	  ctx = comp_unit_die ();
16287 	else
16288 	  ctx = lookup_decl_die (current_function_decl);
16289 
16290 	decl_die = new_die (DW_TAG_variable, ctx, bound);
16291 	add_AT_flag (decl_die, DW_AT_artificial, 1);
16292 	add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16293 	add_AT_location_description (decl_die, DW_AT_location, list);
16294 	add_AT_die_ref (subrange_die, bound_attr, decl_die);
16295 	break;
16296       }
16297     }
16298 }
16299 
16300 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16301    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16302    Note that the block of subscript information for an array type also
16303    includes information about the element type of the given array type.  */
16304 
16305 static void
add_subscript_info(dw_die_ref type_die,tree type,bool collapse_p)16306 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16307 {
16308   unsigned dimension_number;
16309   tree lower, upper;
16310   dw_die_ref subrange_die;
16311 
16312   for (dimension_number = 0;
16313        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16314        type = TREE_TYPE (type), dimension_number++)
16315     {
16316       tree domain = TYPE_DOMAIN (type);
16317 
16318       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16319 	break;
16320 
16321       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16322 	 and (in GNU C only) variable bounds.  Handle all three forms
16323 	 here.  */
16324       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16325       if (domain)
16326 	{
16327 	  /* We have an array type with specified bounds.  */
16328 	  lower = TYPE_MIN_VALUE (domain);
16329 	  upper = TYPE_MAX_VALUE (domain);
16330 
16331 	  /* Define the index type.  */
16332 	  if (TREE_TYPE (domain))
16333 	    {
16334 	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16335 		 TREE_TYPE field.  We can't emit debug info for this
16336 		 because it is an unnamed integral type.  */
16337 	      if (TREE_CODE (domain) == INTEGER_TYPE
16338 		  && TYPE_NAME (domain) == NULL_TREE
16339 		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16340 		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16341 		;
16342 	      else
16343 		add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16344 				    type_die);
16345 	    }
16346 
16347 	  /* ??? If upper is NULL, the array has unspecified length,
16348 	     but it does have a lower bound.  This happens with Fortran
16349 	       dimension arr(N:*)
16350 	     Since the debugger is definitely going to need to know N
16351 	     to produce useful results, go ahead and output the lower
16352 	     bound solo, and hope the debugger can cope.  */
16353 
16354 	  add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16355 	  if (upper)
16356 	    add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16357 	}
16358 
16359       /* Otherwise we have an array type with an unspecified length.  The
16360 	 DWARF-2 spec does not say how to handle this; let's just leave out the
16361 	 bounds.  */
16362     }
16363 }
16364 
16365 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
16366 
16367 static void
add_byte_size_attribute(dw_die_ref die,tree tree_node)16368 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16369 {
16370   dw_die_ref decl_die;
16371   HOST_WIDE_INT size;
16372 
16373   switch (TREE_CODE (tree_node))
16374     {
16375     case ERROR_MARK:
16376       size = 0;
16377       break;
16378     case ENUMERAL_TYPE:
16379     case RECORD_TYPE:
16380     case UNION_TYPE:
16381     case QUAL_UNION_TYPE:
16382       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16383 	  && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16384 	{
16385 	  add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16386 	  return;
16387 	}
16388       size = int_size_in_bytes (tree_node);
16389       break;
16390     case FIELD_DECL:
16391       /* For a data member of a struct or union, the DW_AT_byte_size is
16392 	 generally given as the number of bytes normally allocated for an
16393 	 object of the *declared* type of the member itself.  This is true
16394 	 even for bit-fields.  */
16395       size = int_size_in_bytes (field_type (tree_node));
16396       break;
16397     default:
16398       gcc_unreachable ();
16399     }
16400 
16401   /* Note that `size' might be -1 when we get to this point.  If it is, that
16402      indicates that the byte size of the entity in question is variable.  We
16403      have no good way of expressing this fact in Dwarf at the present time,
16404      when location description was not used by the caller code instead.  */
16405   if (size >= 0)
16406     add_AT_unsigned (die, DW_AT_byte_size, size);
16407 }
16408 
16409 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16410    which specifies the distance in bits from the highest order bit of the
16411    "containing object" for the bit-field to the highest order bit of the
16412    bit-field itself.
16413 
16414    For any given bit-field, the "containing object" is a hypothetical object
16415    (of some integral or enum type) within which the given bit-field lives.  The
16416    type of this hypothetical "containing object" is always the same as the
16417    declared type of the individual bit-field itself.  The determination of the
16418    exact location of the "containing object" for a bit-field is rather
16419    complicated.  It's handled by the `field_byte_offset' function (above).
16420 
16421    Note that it is the size (in bytes) of the hypothetical "containing object"
16422    which will be given in the DW_AT_byte_size attribute for this bit-field.
16423    (See `byte_size_attribute' above).  */
16424 
16425 static inline void
add_bit_offset_attribute(dw_die_ref die,tree decl)16426 add_bit_offset_attribute (dw_die_ref die, tree decl)
16427 {
16428   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16429   tree type = DECL_BIT_FIELD_TYPE (decl);
16430   HOST_WIDE_INT bitpos_int;
16431   HOST_WIDE_INT highest_order_object_bit_offset;
16432   HOST_WIDE_INT highest_order_field_bit_offset;
16433   HOST_WIDE_INT bit_offset;
16434 
16435   /* Must be a field and a bit field.  */
16436   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16437 
16438   /* We can't yet handle bit-fields whose offsets are variable, so if we
16439      encounter such things, just return without generating any attribute
16440      whatsoever.  Likewise for variable or too large size.  */
16441   if (! tree_fits_shwi_p (bit_position (decl))
16442       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16443     return;
16444 
16445   bitpos_int = int_bit_position (decl);
16446 
16447   /* Note that the bit offset is always the distance (in bits) from the
16448      highest-order bit of the "containing object" to the highest-order bit of
16449      the bit-field itself.  Since the "high-order end" of any object or field
16450      is different on big-endian and little-endian machines, the computation
16451      below must take account of these differences.  */
16452   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16453   highest_order_field_bit_offset = bitpos_int;
16454 
16455   if (! BYTES_BIG_ENDIAN)
16456     {
16457       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16458       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16459     }
16460 
16461   bit_offset
16462     = (! BYTES_BIG_ENDIAN
16463        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16464        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16465 
16466   if (bit_offset < 0)
16467     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16468   else
16469     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16470 }
16471 
16472 /* For a FIELD_DECL node which represents a bit field, output an attribute
16473    which specifies the length in bits of the given field.  */
16474 
16475 static inline void
add_bit_size_attribute(dw_die_ref die,tree decl)16476 add_bit_size_attribute (dw_die_ref die, tree decl)
16477 {
16478   /* Must be a field and a bit field.  */
16479   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16480 	      && DECL_BIT_FIELD_TYPE (decl));
16481 
16482   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16483     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16484 }
16485 
16486 /* If the compiled language is ANSI C, then add a 'prototyped'
16487    attribute, if arg types are given for the parameters of a function.  */
16488 
16489 static inline void
add_prototyped_attribute(dw_die_ref die,tree func_type)16490 add_prototyped_attribute (dw_die_ref die, tree func_type)
16491 {
16492   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16493       && prototype_p (func_type))
16494     add_AT_flag (die, DW_AT_prototyped, 1);
16495 }
16496 
16497 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16498    by looking in either the type declaration or object declaration
16499    equate table.  */
16500 
16501 static inline dw_die_ref
add_abstract_origin_attribute(dw_die_ref die,tree origin)16502 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16503 {
16504   dw_die_ref origin_die = NULL;
16505 
16506   if (TREE_CODE (origin) != FUNCTION_DECL)
16507     {
16508       /* We may have gotten separated from the block for the inlined
16509 	 function, if we're in an exception handler or some such; make
16510 	 sure that the abstract function has been written out.
16511 
16512 	 Doing this for nested functions is wrong, however; functions are
16513 	 distinct units, and our context might not even be inline.  */
16514       tree fn = origin;
16515 
16516       if (TYPE_P (fn))
16517 	fn = TYPE_STUB_DECL (fn);
16518 
16519       fn = decl_function_context (fn);
16520       if (fn)
16521 	dwarf2out_abstract_function (fn);
16522     }
16523 
16524   if (DECL_P (origin))
16525     origin_die = lookup_decl_die (origin);
16526   else if (TYPE_P (origin))
16527     origin_die = lookup_type_die (origin);
16528 
16529   /* XXX: Functions that are never lowered don't always have correct block
16530      trees (in the case of java, they simply have no block tree, in some other
16531      languages).  For these functions, there is nothing we can really do to
16532      output correct debug info for inlined functions in all cases.  Rather
16533      than die, we'll just produce deficient debug info now, in that we will
16534      have variables without a proper abstract origin.  In the future, when all
16535      functions are lowered, we should re-add a gcc_assert (origin_die)
16536      here.  */
16537 
16538   if (origin_die)
16539     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16540   return origin_die;
16541 }
16542 
16543 /* We do not currently support the pure_virtual attribute.  */
16544 
16545 static inline void
add_pure_or_virtual_attribute(dw_die_ref die,tree func_decl)16546 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16547 {
16548   if (DECL_VINDEX (func_decl))
16549     {
16550       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16551 
16552       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16553 	add_AT_loc (die, DW_AT_vtable_elem_location,
16554 		    new_loc_descr (DW_OP_constu,
16555 				   tree_to_shwi (DECL_VINDEX (func_decl)),
16556 				   0));
16557 
16558       /* GNU extension: Record what type this method came from originally.  */
16559       if (debug_info_level > DINFO_LEVEL_TERSE
16560 	  && DECL_CONTEXT (func_decl))
16561 	add_AT_die_ref (die, DW_AT_containing_type,
16562 			lookup_type_die (DECL_CONTEXT (func_decl)));
16563     }
16564 }
16565 
16566 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16567    given decl.  This used to be a vendor extension until after DWARF 4
16568    standardized it.  */
16569 
16570 static void
add_linkage_attr(dw_die_ref die,tree decl)16571 add_linkage_attr (dw_die_ref die, tree decl)
16572 {
16573   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16574 
16575   /* Mimic what assemble_name_raw does with a leading '*'.  */
16576   if (name[0] == '*')
16577     name = &name[1];
16578 
16579   if (dwarf_version >= 4)
16580     add_AT_string (die, DW_AT_linkage_name, name);
16581   else
16582     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16583 }
16584 
16585 /* Add source coordinate attributes for the given decl.  */
16586 
16587 static void
add_src_coords_attributes(dw_die_ref die,tree decl)16588 add_src_coords_attributes (dw_die_ref die, tree decl)
16589 {
16590   expanded_location s;
16591 
16592   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16593     return;
16594   s = expand_location (DECL_SOURCE_LOCATION (decl));
16595   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16596   add_AT_unsigned (die, DW_AT_decl_line, s.line);
16597 }
16598 
16599 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
16600 
16601 static void
add_linkage_name(dw_die_ref die,tree decl)16602 add_linkage_name (dw_die_ref die, tree decl)
16603 {
16604   if (debug_info_level > DINFO_LEVEL_TERSE
16605       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16606       && TREE_PUBLIC (decl)
16607       && !DECL_ABSTRACT (decl)
16608       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16609       && die->die_tag != DW_TAG_member)
16610     {
16611       /* Defer until we have an assembler name set.  */
16612       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16613 	{
16614 	  limbo_die_node *asm_name;
16615 
16616 	  asm_name = ggc_alloc_cleared_limbo_die_node ();
16617 	  asm_name->die = die;
16618 	  asm_name->created_for = decl;
16619 	  asm_name->next = deferred_asm_name;
16620 	  deferred_asm_name = asm_name;
16621 	}
16622       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16623 	add_linkage_attr (die, decl);
16624     }
16625 }
16626 
16627 /* Add a DW_AT_name attribute and source coordinate attribute for the
16628    given decl, but only if it actually has a name.  */
16629 
16630 static void
add_name_and_src_coords_attributes(dw_die_ref die,tree decl)16631 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16632 {
16633   tree decl_name;
16634 
16635   decl_name = DECL_NAME (decl);
16636   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16637     {
16638       const char *name = dwarf2_name (decl, 0);
16639       if (name)
16640 	add_name_attribute (die, name);
16641       if (! DECL_ARTIFICIAL (decl))
16642 	add_src_coords_attributes (die, decl);
16643 
16644       add_linkage_name (die, decl);
16645     }
16646 
16647 #ifdef VMS_DEBUGGING_INFO
16648   /* Get the function's name, as described by its RTL.  This may be different
16649      from the DECL_NAME name used in the source file.  */
16650   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16651     {
16652       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16653                   XEXP (DECL_RTL (decl), 0), false);
16654       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16655     }
16656 #endif /* VMS_DEBUGGING_INFO */
16657 }
16658 
16659 #ifdef VMS_DEBUGGING_INFO
16660 /* Output the debug main pointer die for VMS */
16661 
16662 void
dwarf2out_vms_debug_main_pointer(void)16663 dwarf2out_vms_debug_main_pointer (void)
16664 {
16665   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16666   dw_die_ref die;
16667 
16668   /* Allocate the VMS debug main subprogram die.  */
16669   die = ggc_alloc_cleared_die_node ();
16670   die->die_tag = DW_TAG_subprogram;
16671   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16672   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16673 			       current_function_funcdef_no);
16674   add_AT_lbl_id (die, DW_AT_entry_pc, label);
16675 
16676   /* Make it the first child of comp_unit_die ().  */
16677   die->die_parent = comp_unit_die ();
16678   if (comp_unit_die ()->die_child)
16679     {
16680       die->die_sib = comp_unit_die ()->die_child->die_sib;
16681       comp_unit_die ()->die_child->die_sib = die;
16682     }
16683   else
16684     {
16685       die->die_sib = die;
16686       comp_unit_die ()->die_child = die;
16687     }
16688 }
16689 #endif /* VMS_DEBUGGING_INFO */
16690 
16691 /* Push a new declaration scope.  */
16692 
16693 static void
push_decl_scope(tree scope)16694 push_decl_scope (tree scope)
16695 {
16696   vec_safe_push (decl_scope_table, scope);
16697 }
16698 
16699 /* Pop a declaration scope.  */
16700 
16701 static inline void
pop_decl_scope(void)16702 pop_decl_scope (void)
16703 {
16704   decl_scope_table->pop ();
16705 }
16706 
16707 /* walk_tree helper function for uses_local_type, below.  */
16708 
16709 static tree
uses_local_type_r(tree * tp,int * walk_subtrees,void * data ATTRIBUTE_UNUSED)16710 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16711 {
16712   if (!TYPE_P (*tp))
16713     *walk_subtrees = 0;
16714   else
16715     {
16716       tree name = TYPE_NAME (*tp);
16717       if (name && DECL_P (name) && decl_function_context (name))
16718 	return *tp;
16719     }
16720   return NULL_TREE;
16721 }
16722 
16723 /* If TYPE involves a function-local type (including a local typedef to a
16724    non-local type), returns that type; otherwise returns NULL_TREE.  */
16725 
16726 static tree
uses_local_type(tree type)16727 uses_local_type (tree type)
16728 {
16729   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16730   return used;
16731 }
16732 
16733 /* Return the DIE for the scope that immediately contains this type.
16734    Non-named types that do not involve a function-local type get global
16735    scope.  Named types nested in namespaces or other types get their
16736    containing scope.  All other types (i.e. function-local named types) get
16737    the current active scope.  */
16738 
16739 static dw_die_ref
scope_die_for(tree t,dw_die_ref context_die)16740 scope_die_for (tree t, dw_die_ref context_die)
16741 {
16742   dw_die_ref scope_die = NULL;
16743   tree containing_scope;
16744 
16745   /* Non-types always go in the current scope.  */
16746   gcc_assert (TYPE_P (t));
16747 
16748   /* Use the scope of the typedef, rather than the scope of the type
16749      it refers to.  */
16750   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16751     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16752   else
16753     containing_scope = TYPE_CONTEXT (t);
16754 
16755   /* Use the containing namespace if there is one.  */
16756   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16757     {
16758       if (context_die == lookup_decl_die (containing_scope))
16759 	/* OK */;
16760       else if (debug_info_level > DINFO_LEVEL_TERSE)
16761 	context_die = get_context_die (containing_scope);
16762       else
16763 	containing_scope = NULL_TREE;
16764     }
16765 
16766   /* Ignore function type "scopes" from the C frontend.  They mean that
16767      a tagged type is local to a parmlist of a function declarator, but
16768      that isn't useful to DWARF.  */
16769   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16770     containing_scope = NULL_TREE;
16771 
16772   if (SCOPE_FILE_SCOPE_P (containing_scope))
16773     {
16774       /* If T uses a local type keep it local as well, to avoid references
16775 	 to function-local DIEs from outside the function.  */
16776       if (current_function_decl && uses_local_type (t))
16777 	scope_die = context_die;
16778       else
16779 	scope_die = comp_unit_die ();
16780     }
16781   else if (TYPE_P (containing_scope))
16782     {
16783       /* For types, we can just look up the appropriate DIE.  */
16784       if (debug_info_level > DINFO_LEVEL_TERSE)
16785 	scope_die = get_context_die (containing_scope);
16786       else
16787 	{
16788 	  scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16789 	  if (scope_die == NULL)
16790 	    scope_die = comp_unit_die ();
16791 	}
16792     }
16793   else
16794     scope_die = context_die;
16795 
16796   return scope_die;
16797 }
16798 
16799 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16800 
16801 static inline int
local_scope_p(dw_die_ref context_die)16802 local_scope_p (dw_die_ref context_die)
16803 {
16804   for (; context_die; context_die = context_die->die_parent)
16805     if (context_die->die_tag == DW_TAG_inlined_subroutine
16806 	|| context_die->die_tag == DW_TAG_subprogram)
16807       return 1;
16808 
16809   return 0;
16810 }
16811 
16812 /* Returns nonzero if CONTEXT_DIE is a class.  */
16813 
16814 static inline int
class_scope_p(dw_die_ref context_die)16815 class_scope_p (dw_die_ref context_die)
16816 {
16817   return (context_die
16818 	  && (context_die->die_tag == DW_TAG_structure_type
16819 	      || context_die->die_tag == DW_TAG_class_type
16820 	      || context_die->die_tag == DW_TAG_interface_type
16821 	      || context_die->die_tag == DW_TAG_union_type));
16822 }
16823 
16824 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16825    whether or not to treat a DIE in this context as a declaration.  */
16826 
16827 static inline int
class_or_namespace_scope_p(dw_die_ref context_die)16828 class_or_namespace_scope_p (dw_die_ref context_die)
16829 {
16830   return (class_scope_p (context_die)
16831 	  || (context_die && context_die->die_tag == DW_TAG_namespace));
16832 }
16833 
16834 /* Many forms of DIEs require a "type description" attribute.  This
16835    routine locates the proper "type descriptor" die for the type given
16836    by 'type', and adds a DW_AT_type attribute below the given die.  */
16837 
16838 static void
add_type_attribute(dw_die_ref object_die,tree type,int decl_const,int decl_volatile,dw_die_ref context_die)16839 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16840 		    int decl_volatile, dw_die_ref context_die)
16841 {
16842   enum tree_code code  = TREE_CODE (type);
16843   dw_die_ref type_die  = NULL;
16844 
16845   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16846      or fixed-point type, use the inner type.  This is because we have no
16847      support for unnamed types in base_type_die.  This can happen if this is
16848      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16849   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16850       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16851     type = TREE_TYPE (type), code = TREE_CODE (type);
16852 
16853   if (code == ERROR_MARK
16854       /* Handle a special case.  For functions whose return type is void, we
16855 	 generate *no* type attribute.  (Note that no object may have type
16856 	 `void', so this only applies to function return types).  */
16857       || code == VOID_TYPE)
16858     return;
16859 
16860   type_die = modified_type_die (type,
16861 				decl_const || TYPE_READONLY (type),
16862 				decl_volatile || TYPE_VOLATILE (type),
16863 				context_die);
16864 
16865   if (type_die != NULL)
16866     add_AT_die_ref (object_die, DW_AT_type, type_die);
16867 }
16868 
16869 /* Given an object die, add the calling convention attribute for the
16870    function call type.  */
16871 static void
add_calling_convention_attribute(dw_die_ref subr_die,tree decl)16872 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16873 {
16874   enum dwarf_calling_convention value = DW_CC_normal;
16875 
16876   value = ((enum dwarf_calling_convention)
16877 	   targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16878 
16879   if (is_fortran ()
16880       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16881     {
16882       /* DWARF 2 doesn't provide a way to identify a program's source-level
16883 	entry point.  DW_AT_calling_convention attributes are only meant
16884 	to describe functions' calling conventions.  However, lacking a
16885 	better way to signal the Fortran main program, we used this for
16886 	a long time, following existing custom.  Now, DWARF 4 has
16887 	DW_AT_main_subprogram, which we add below, but some tools still
16888 	rely on the old way, which we thus keep.  */
16889       value = DW_CC_program;
16890 
16891       if (dwarf_version >= 4 || !dwarf_strict)
16892 	add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16893     }
16894 
16895   /* Only add the attribute if the backend requests it, and
16896      is not DW_CC_normal.  */
16897   if (value && (value != DW_CC_normal))
16898     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16899 }
16900 
16901 /* Given a tree pointer to a struct, class, union, or enum type node, return
16902    a pointer to the (string) tag name for the given type, or zero if the type
16903    was declared without a tag.  */
16904 
16905 static const char *
type_tag(const_tree type)16906 type_tag (const_tree type)
16907 {
16908   const char *name = 0;
16909 
16910   if (TYPE_NAME (type) != 0)
16911     {
16912       tree t = 0;
16913 
16914       /* Find the IDENTIFIER_NODE for the type name.  */
16915       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16916 	  && !TYPE_NAMELESS (type))
16917 	t = TYPE_NAME (type);
16918 
16919       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16920 	 a TYPE_DECL node, regardless of whether or not a `typedef' was
16921 	 involved.  */
16922       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16923 	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
16924 	{
16925 	  /* We want to be extra verbose.  Don't call dwarf_name if
16926 	     DECL_NAME isn't set.  The default hook for decl_printable_name
16927 	     doesn't like that, and in this context it's correct to return
16928 	     0, instead of "<anonymous>" or the like.  */
16929 	  if (DECL_NAME (TYPE_NAME (type))
16930 	      && !DECL_NAMELESS (TYPE_NAME (type)))
16931 	    name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16932 	}
16933 
16934       /* Now get the name as a string, or invent one.  */
16935       if (!name && t != 0)
16936 	name = IDENTIFIER_POINTER (t);
16937     }
16938 
16939   return (name == 0 || *name == '\0') ? 0 : name;
16940 }
16941 
16942 /* Return the type associated with a data member, make a special check
16943    for bit field types.  */
16944 
16945 static inline tree
member_declared_type(const_tree member)16946 member_declared_type (const_tree member)
16947 {
16948   return (DECL_BIT_FIELD_TYPE (member)
16949 	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16950 }
16951 
16952 /* Get the decl's label, as described by its RTL. This may be different
16953    from the DECL_NAME name used in the source file.  */
16954 
16955 #if 0
16956 static const char *
16957 decl_start_label (tree decl)
16958 {
16959   rtx x;
16960   const char *fnname;
16961 
16962   x = DECL_RTL (decl);
16963   gcc_assert (MEM_P (x));
16964 
16965   x = XEXP (x, 0);
16966   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16967 
16968   fnname = XSTR (x, 0);
16969   return fnname;
16970 }
16971 #endif
16972 
16973 /* These routines generate the internal representation of the DIE's for
16974    the compilation unit.  Debugging information is collected by walking
16975    the declaration trees passed in from dwarf2out_decl().  */
16976 
16977 static void
gen_array_type_die(tree type,dw_die_ref context_die)16978 gen_array_type_die (tree type, dw_die_ref context_die)
16979 {
16980   dw_die_ref scope_die = scope_die_for (type, context_die);
16981   dw_die_ref array_die;
16982 
16983   /* GNU compilers represent multidimensional array types as sequences of one
16984      dimensional array types whose element types are themselves array types.
16985      We sometimes squish that down to a single array_type DIE with multiple
16986      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16987      say that we are allowed to do this kind of compression in C, because
16988      there is no difference between an array of arrays and a multidimensional
16989      array.  We don't do this for Ada to remain as close as possible to the
16990      actual representation, which is especially important against the language
16991      flexibilty wrt arrays of variable size.  */
16992 
16993   bool collapse_nested_arrays = !is_ada ();
16994   tree element_type;
16995 
16996   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16997      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16998   if (TYPE_STRING_FLAG (type)
16999       && TREE_CODE (type) == ARRAY_TYPE
17000       && is_fortran ()
17001       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17002     {
17003       HOST_WIDE_INT size;
17004 
17005       array_die = new_die (DW_TAG_string_type, scope_die, type);
17006       add_name_attribute (array_die, type_tag (type));
17007       equate_type_number_to_die (type, array_die);
17008       size = int_size_in_bytes (type);
17009       if (size >= 0)
17010 	add_AT_unsigned (array_die, DW_AT_byte_size, size);
17011       else if (TYPE_DOMAIN (type) != NULL_TREE
17012 	       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17013 	       && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17014 	{
17015 	  tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17016 	  dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17017 
17018 	  size = int_size_in_bytes (TREE_TYPE (szdecl));
17019 	  if (loc && size > 0)
17020 	    {
17021 	      add_AT_location_description (array_die, DW_AT_string_length, loc);
17022 	      if (size != DWARF2_ADDR_SIZE)
17023 		add_AT_unsigned (array_die, DW_AT_byte_size, size);
17024 	    }
17025 	}
17026       return;
17027     }
17028 
17029   array_die = new_die (DW_TAG_array_type, scope_die, type);
17030   add_name_attribute (array_die, type_tag (type));
17031   equate_type_number_to_die (type, array_die);
17032 
17033   if (TREE_CODE (type) == VECTOR_TYPE)
17034     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17035 
17036   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17037   if (is_fortran ()
17038       && TREE_CODE (type) == ARRAY_TYPE
17039       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17040       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17041     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17042 
17043 #if 0
17044   /* We default the array ordering.  SDB will probably do
17045      the right things even if DW_AT_ordering is not present.  It's not even
17046      an issue until we start to get into multidimensional arrays anyway.  If
17047      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17048      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17049      and when we find out that we need to put these in, we will only do so
17050      for multidimensional arrays.  */
17051   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17052 #endif
17053 
17054   if (TREE_CODE (type) == VECTOR_TYPE)
17055     {
17056       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17057       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17058       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17059       add_bound_info (subrange_die, DW_AT_upper_bound,
17060 		      size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17061     }
17062   else
17063     add_subscript_info (array_die, type, collapse_nested_arrays);
17064 
17065   /* Add representation of the type of the elements of this array type and
17066      emit the corresponding DIE if we haven't done it already.  */
17067   element_type = TREE_TYPE (type);
17068   if (collapse_nested_arrays)
17069     while (TREE_CODE (element_type) == ARRAY_TYPE)
17070       {
17071 	if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17072 	  break;
17073 	element_type = TREE_TYPE (element_type);
17074       }
17075 
17076   add_type_attribute (array_die, element_type, 0, 0, context_die);
17077 
17078   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17079   if (TYPE_ARTIFICIAL (type))
17080     add_AT_flag (array_die, DW_AT_artificial, 1);
17081 
17082   if (get_AT (array_die, DW_AT_name))
17083     add_pubtype (type, array_die);
17084 }
17085 
17086 static dw_loc_descr_ref
descr_info_loc(tree val,tree base_decl)17087 descr_info_loc (tree val, tree base_decl)
17088 {
17089   HOST_WIDE_INT size;
17090   dw_loc_descr_ref loc, loc2;
17091   enum dwarf_location_atom op;
17092 
17093   if (val == base_decl)
17094     return new_loc_descr (DW_OP_push_object_address, 0, 0);
17095 
17096   switch (TREE_CODE (val))
17097     {
17098     CASE_CONVERT:
17099       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17100     case VAR_DECL:
17101       return loc_descriptor_from_tree (val, 0);
17102     case INTEGER_CST:
17103       if (tree_fits_shwi_p (val))
17104 	return int_loc_descriptor (tree_to_shwi (val));
17105       break;
17106     case INDIRECT_REF:
17107       size = int_size_in_bytes (TREE_TYPE (val));
17108       if (size < 0)
17109 	break;
17110       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17111       if (!loc)
17112 	break;
17113       if (size == DWARF2_ADDR_SIZE)
17114 	add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17115       else
17116 	add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17117       return loc;
17118     case POINTER_PLUS_EXPR:
17119     case PLUS_EXPR:
17120       if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17121 	  && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17122 	{
17123 	  loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17124 	  if (!loc)
17125 	    break;
17126 	  loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17127 	}
17128       else
17129 	{
17130 	  op = DW_OP_plus;
17131 	do_binop:
17132 	  loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17133 	  if (!loc)
17134 	    break;
17135 	  loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17136 	  if (!loc2)
17137 	    break;
17138 	  add_loc_descr (&loc, loc2);
17139 	  add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17140 	}
17141       return loc;
17142     case MINUS_EXPR:
17143       op = DW_OP_minus;
17144       goto do_binop;
17145     case MULT_EXPR:
17146       op = DW_OP_mul;
17147       goto do_binop;
17148     case EQ_EXPR:
17149       op = DW_OP_eq;
17150       goto do_binop;
17151     case NE_EXPR:
17152       op = DW_OP_ne;
17153       goto do_binop;
17154     default:
17155       break;
17156     }
17157   return NULL;
17158 }
17159 
17160 static void
add_descr_info_field(dw_die_ref die,enum dwarf_attribute attr,tree val,tree base_decl)17161 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17162 		      tree val, tree base_decl)
17163 {
17164   dw_loc_descr_ref loc;
17165 
17166   if (tree_fits_shwi_p (val))
17167     {
17168       add_AT_unsigned (die, attr, tree_to_shwi (val));
17169       return;
17170     }
17171 
17172   loc = descr_info_loc (val, base_decl);
17173   if (!loc)
17174     return;
17175 
17176   add_AT_loc (die, attr, loc);
17177 }
17178 
17179 /* This routine generates DIE for array with hidden descriptor, details
17180    are filled into *info by a langhook.  */
17181 
17182 static void
gen_descr_array_type_die(tree type,struct array_descr_info * info,dw_die_ref context_die)17183 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17184 			  dw_die_ref context_die)
17185 {
17186   dw_die_ref scope_die = scope_die_for (type, context_die);
17187   dw_die_ref array_die;
17188   int dim;
17189 
17190   array_die = new_die (DW_TAG_array_type, scope_die, type);
17191   add_name_attribute (array_die, type_tag (type));
17192   equate_type_number_to_die (type, array_die);
17193 
17194   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17195   if (is_fortran ()
17196       && info->ndimensions >= 2)
17197     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17198 
17199   if (info->data_location)
17200     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17201 			  info->base_decl);
17202   if (info->associated)
17203     add_descr_info_field (array_die, DW_AT_associated, info->associated,
17204 			  info->base_decl);
17205   if (info->allocated)
17206     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17207 			  info->base_decl);
17208 
17209   for (dim = 0; dim < info->ndimensions; dim++)
17210     {
17211       dw_die_ref subrange_die
17212 	= new_die (DW_TAG_subrange_type, array_die, NULL);
17213 
17214       if (info->dimen[dim].lower_bound)
17215 	{
17216 	  /* If it is the default value, omit it.  */
17217 	  int dflt;
17218 
17219 	  if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17220 	      && (dflt = lower_bound_default ()) != -1
17221 	      && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17222 	    ;
17223 	  else
17224 	    add_descr_info_field (subrange_die, DW_AT_lower_bound,
17225 				  info->dimen[dim].lower_bound,
17226 				  info->base_decl);
17227 	}
17228       if (info->dimen[dim].upper_bound)
17229 	add_descr_info_field (subrange_die, DW_AT_upper_bound,
17230 			      info->dimen[dim].upper_bound,
17231 			      info->base_decl);
17232       if (info->dimen[dim].stride)
17233 	add_descr_info_field (subrange_die, DW_AT_byte_stride,
17234 			      info->dimen[dim].stride,
17235 			      info->base_decl);
17236     }
17237 
17238   gen_type_die (info->element_type, context_die);
17239   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17240 
17241   if (get_AT (array_die, DW_AT_name))
17242     add_pubtype (type, array_die);
17243 }
17244 
17245 #if 0
17246 static void
17247 gen_entry_point_die (tree decl, dw_die_ref context_die)
17248 {
17249   tree origin = decl_ultimate_origin (decl);
17250   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17251 
17252   if (origin != NULL)
17253     add_abstract_origin_attribute (decl_die, origin);
17254   else
17255     {
17256       add_name_and_src_coords_attributes (decl_die, decl);
17257       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17258 			  0, 0, context_die);
17259     }
17260 
17261   if (DECL_ABSTRACT (decl))
17262     equate_decl_number_to_die (decl, decl_die);
17263   else
17264     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17265 }
17266 #endif
17267 
17268 /* Walk through the list of incomplete types again, trying once more to
17269    emit full debugging info for them.  */
17270 
17271 static void
retry_incomplete_types(void)17272 retry_incomplete_types (void)
17273 {
17274   int i;
17275 
17276   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17277     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17278       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17279 }
17280 
17281 /* Determine what tag to use for a record type.  */
17282 
17283 static enum dwarf_tag
record_type_tag(tree type)17284 record_type_tag (tree type)
17285 {
17286   if (! lang_hooks.types.classify_record)
17287     return DW_TAG_structure_type;
17288 
17289   switch (lang_hooks.types.classify_record (type))
17290     {
17291     case RECORD_IS_STRUCT:
17292       return DW_TAG_structure_type;
17293 
17294     case RECORD_IS_CLASS:
17295       return DW_TAG_class_type;
17296 
17297     case RECORD_IS_INTERFACE:
17298       if (dwarf_version >= 3 || !dwarf_strict)
17299 	return DW_TAG_interface_type;
17300       return DW_TAG_structure_type;
17301 
17302     default:
17303       gcc_unreachable ();
17304     }
17305 }
17306 
17307 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17308    include all of the information about the enumeration values also. Each
17309    enumerated type name/value is listed as a child of the enumerated type
17310    DIE.  */
17311 
17312 static dw_die_ref
gen_enumeration_type_die(tree type,dw_die_ref context_die)17313 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17314 {
17315   dw_die_ref type_die = lookup_type_die (type);
17316 
17317   if (type_die == NULL)
17318     {
17319       type_die = new_die (DW_TAG_enumeration_type,
17320 			  scope_die_for (type, context_die), type);
17321       equate_type_number_to_die (type, type_die);
17322       add_name_attribute (type_die, type_tag (type));
17323       if (dwarf_version >= 4 || !dwarf_strict)
17324 	{
17325 	  if (ENUM_IS_SCOPED (type))
17326 	    add_AT_flag (type_die, DW_AT_enum_class, 1);
17327 	  if (ENUM_IS_OPAQUE (type))
17328 	    add_AT_flag (type_die, DW_AT_declaration, 1);
17329 	}
17330     }
17331   else if (! TYPE_SIZE (type))
17332     return type_die;
17333   else
17334     remove_AT (type_die, DW_AT_declaration);
17335 
17336   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17337      given enum type is incomplete, do not generate the DW_AT_byte_size
17338      attribute or the DW_AT_element_list attribute.  */
17339   if (TYPE_SIZE (type))
17340     {
17341       tree link;
17342 
17343       TREE_ASM_WRITTEN (type) = 1;
17344       add_byte_size_attribute (type_die, type);
17345       if (TYPE_STUB_DECL (type) != NULL_TREE)
17346 	{
17347 	  add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17348 	  add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17349 	}
17350 
17351       /* If the first reference to this type was as the return type of an
17352 	 inline function, then it may not have a parent.  Fix this now.  */
17353       if (type_die->die_parent == NULL)
17354 	add_child_die (scope_die_for (type, context_die), type_die);
17355 
17356       for (link = TYPE_VALUES (type);
17357 	   link != NULL; link = TREE_CHAIN (link))
17358 	{
17359 	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17360 	  tree value = TREE_VALUE (link);
17361 
17362 	  add_name_attribute (enum_die,
17363 			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17364 
17365 	  if (TREE_CODE (value) == CONST_DECL)
17366 	    value = DECL_INITIAL (value);
17367 
17368 	  if (simple_type_size_in_bits (TREE_TYPE (value))
17369 	      <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17370 	    /* DWARF2 does not provide a way of indicating whether or
17371 	       not enumeration constants are signed or unsigned.  GDB
17372 	       always assumes the values are signed, so we output all
17373 	       values as if they were signed.  That means that
17374 	       enumeration constants with very large unsigned values
17375 	       will appear to have negative values in the debugger.
17376 
17377 	       TODO: the above comment is wrong, DWARF2 does provide
17378 	       DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17379 	       This should be re-worked to use correct signed/unsigned
17380 	       int/double tags for all cases, instead of always treating as
17381 	       signed.  */
17382 	    add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17383 	  else
17384 	    /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17385 	       that here.  */
17386 	    add_AT_double (enum_die, DW_AT_const_value,
17387 			   TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17388 	}
17389 
17390       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17391       if (TYPE_ARTIFICIAL (type))
17392 	add_AT_flag (type_die, DW_AT_artificial, 1);
17393     }
17394   else
17395     add_AT_flag (type_die, DW_AT_declaration, 1);
17396 
17397   add_pubtype (type, type_die);
17398 
17399   return type_die;
17400 }
17401 
17402 /* Generate a DIE to represent either a real live formal parameter decl or to
17403    represent just the type of some formal parameter position in some function
17404    type.
17405 
17406    Note that this routine is a bit unusual because its argument may be a
17407    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17408    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17409    node.  If it's the former then this function is being called to output a
17410    DIE to represent a formal parameter object (or some inlining thereof).  If
17411    it's the latter, then this function is only being called to output a
17412    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17413    argument type of some subprogram type.
17414    If EMIT_NAME_P is true, name and source coordinate attributes
17415    are emitted.  */
17416 
17417 static dw_die_ref
gen_formal_parameter_die(tree node,tree origin,bool emit_name_p,dw_die_ref context_die)17418 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17419 			  dw_die_ref context_die)
17420 {
17421   tree node_or_origin = node ? node : origin;
17422   tree ultimate_origin;
17423   dw_die_ref parm_die
17424     = new_die (DW_TAG_formal_parameter, context_die, node);
17425 
17426   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17427     {
17428     case tcc_declaration:
17429       ultimate_origin = decl_ultimate_origin (node_or_origin);
17430       if (node || ultimate_origin)
17431 	origin = ultimate_origin;
17432       if (origin != NULL)
17433 	add_abstract_origin_attribute (parm_die, origin);
17434       else if (emit_name_p)
17435 	add_name_and_src_coords_attributes (parm_die, node);
17436       if (origin == NULL
17437 	  || (! DECL_ABSTRACT (node_or_origin)
17438 	      && variably_modified_type_p (TREE_TYPE (node_or_origin),
17439 					   decl_function_context
17440 							    (node_or_origin))))
17441 	{
17442 	  tree type = TREE_TYPE (node_or_origin);
17443 	  if (decl_by_reference_p (node_or_origin))
17444 	    add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17445 				context_die);
17446 	  else
17447 	    add_type_attribute (parm_die, type,
17448 				TREE_READONLY (node_or_origin),
17449 				TREE_THIS_VOLATILE (node_or_origin),
17450 				context_die);
17451 	}
17452       if (origin == NULL && DECL_ARTIFICIAL (node))
17453 	add_AT_flag (parm_die, DW_AT_artificial, 1);
17454 
17455       if (node && node != origin)
17456         equate_decl_number_to_die (node, parm_die);
17457       if (! DECL_ABSTRACT (node_or_origin))
17458 	add_location_or_const_value_attribute (parm_die, node_or_origin,
17459 					       node == NULL, DW_AT_location);
17460 
17461       break;
17462 
17463     case tcc_type:
17464       /* We were called with some kind of a ..._TYPE node.  */
17465       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17466       break;
17467 
17468     default:
17469       gcc_unreachable ();
17470     }
17471 
17472   return parm_die;
17473 }
17474 
17475 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17476    children DW_TAG_formal_parameter DIEs representing the arguments of the
17477    parameter pack.
17478 
17479    PARM_PACK must be a function parameter pack.
17480    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17481    must point to the subsequent arguments of the function PACK_ARG belongs to.
17482    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17483    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17484    following the last one for which a DIE was generated.  */
17485 
17486 static dw_die_ref
gen_formal_parameter_pack_die(tree parm_pack,tree pack_arg,dw_die_ref subr_die,tree * next_arg)17487 gen_formal_parameter_pack_die  (tree parm_pack,
17488 				tree pack_arg,
17489 				dw_die_ref subr_die,
17490 				tree *next_arg)
17491 {
17492   tree arg;
17493   dw_die_ref parm_pack_die;
17494 
17495   gcc_assert (parm_pack
17496 	      && lang_hooks.function_parameter_pack_p (parm_pack)
17497 	      && subr_die);
17498 
17499   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17500   add_src_coords_attributes (parm_pack_die, parm_pack);
17501 
17502   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17503     {
17504       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17505 								 parm_pack))
17506 	break;
17507       gen_formal_parameter_die (arg, NULL,
17508 				false /* Don't emit name attribute.  */,
17509 				parm_pack_die);
17510     }
17511   if (next_arg)
17512     *next_arg = arg;
17513   return parm_pack_die;
17514 }
17515 
17516 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17517    at the end of an (ANSI prototyped) formal parameters list.  */
17518 
17519 static void
gen_unspecified_parameters_die(tree decl_or_type,dw_die_ref context_die)17520 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17521 {
17522   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17523 }
17524 
17525 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17526    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17527    parameters as specified in some function type specification (except for
17528    those which appear as part of a function *definition*).  */
17529 
17530 static void
gen_formal_types_die(tree function_or_method_type,dw_die_ref context_die)17531 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17532 {
17533   tree link;
17534   tree formal_type = NULL;
17535   tree first_parm_type;
17536   tree arg;
17537 
17538   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17539     {
17540       arg = DECL_ARGUMENTS (function_or_method_type);
17541       function_or_method_type = TREE_TYPE (function_or_method_type);
17542     }
17543   else
17544     arg = NULL_TREE;
17545 
17546   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17547 
17548   /* Make our first pass over the list of formal parameter types and output a
17549      DW_TAG_formal_parameter DIE for each one.  */
17550   for (link = first_parm_type; link; )
17551     {
17552       dw_die_ref parm_die;
17553 
17554       formal_type = TREE_VALUE (link);
17555       if (formal_type == void_type_node)
17556 	break;
17557 
17558       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17559       parm_die = gen_formal_parameter_die (formal_type, NULL,
17560 					   true /* Emit name attribute.  */,
17561 					   context_die);
17562       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17563 	  && link == first_parm_type)
17564 	{
17565 	  add_AT_flag (parm_die, DW_AT_artificial, 1);
17566 	  if (dwarf_version >= 3 || !dwarf_strict)
17567 	    add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17568 	}
17569       else if (arg && DECL_ARTIFICIAL (arg))
17570 	add_AT_flag (parm_die, DW_AT_artificial, 1);
17571 
17572       link = TREE_CHAIN (link);
17573       if (arg)
17574 	arg = DECL_CHAIN (arg);
17575     }
17576 
17577   /* If this function type has an ellipsis, add a
17578      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17579   if (formal_type != void_type_node)
17580     gen_unspecified_parameters_die (function_or_method_type, context_die);
17581 
17582   /* Make our second (and final) pass over the list of formal parameter types
17583      and output DIEs to represent those types (as necessary).  */
17584   for (link = TYPE_ARG_TYPES (function_or_method_type);
17585        link && TREE_VALUE (link);
17586        link = TREE_CHAIN (link))
17587     gen_type_die (TREE_VALUE (link), context_die);
17588 }
17589 
17590 /* We want to generate the DIE for TYPE so that we can generate the
17591    die for MEMBER, which has been defined; we will need to refer back
17592    to the member declaration nested within TYPE.  If we're trying to
17593    generate minimal debug info for TYPE, processing TYPE won't do the
17594    trick; we need to attach the member declaration by hand.  */
17595 
17596 static void
gen_type_die_for_member(tree type,tree member,dw_die_ref context_die)17597 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17598 {
17599   gen_type_die (type, context_die);
17600 
17601   /* If we're trying to avoid duplicate debug info, we may not have
17602      emitted the member decl for this function.  Emit it now.  */
17603   if (TYPE_STUB_DECL (type)
17604       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17605       && ! lookup_decl_die (member))
17606     {
17607       dw_die_ref type_die;
17608       gcc_assert (!decl_ultimate_origin (member));
17609 
17610       push_decl_scope (type);
17611       type_die = lookup_type_die_strip_naming_typedef (type);
17612       if (TREE_CODE (member) == FUNCTION_DECL)
17613 	gen_subprogram_die (member, type_die);
17614       else if (TREE_CODE (member) == FIELD_DECL)
17615 	{
17616 	  /* Ignore the nameless fields that are used to skip bits but handle
17617 	     C++ anonymous unions and structs.  */
17618 	  if (DECL_NAME (member) != NULL_TREE
17619 	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17620 	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17621 	    {
17622 	      gen_type_die (member_declared_type (member), type_die);
17623 	      gen_field_die (member, type_die);
17624 	    }
17625 	}
17626       else
17627 	gen_variable_die (member, NULL_TREE, type_die);
17628 
17629       pop_decl_scope ();
17630     }
17631 }
17632 
17633 /* Forward declare these functions, because they are mutually recursive
17634   with their set_block_* pairing functions.  */
17635 static void set_decl_origin_self (tree);
17636 static void set_decl_abstract_flags (tree, int);
17637 
17638 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17639    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17640    that it points to the node itself, thus indicating that the node is its
17641    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17642    the given node is NULL, recursively descend the decl/block tree which
17643    it is the root of, and for each other ..._DECL or BLOCK node contained
17644    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17645    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17646    values to point to themselves.  */
17647 
17648 static void
set_block_origin_self(tree stmt)17649 set_block_origin_self (tree stmt)
17650 {
17651   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17652     {
17653       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17654 
17655       {
17656 	tree local_decl;
17657 
17658 	for (local_decl = BLOCK_VARS (stmt);
17659 	     local_decl != NULL_TREE;
17660 	     local_decl = DECL_CHAIN (local_decl))
17661 	  if (! DECL_EXTERNAL (local_decl))
17662 	    set_decl_origin_self (local_decl);	/* Potential recursion.  */
17663       }
17664 
17665       {
17666 	tree subblock;
17667 
17668 	for (subblock = BLOCK_SUBBLOCKS (stmt);
17669 	     subblock != NULL_TREE;
17670 	     subblock = BLOCK_CHAIN (subblock))
17671 	  set_block_origin_self (subblock);	/* Recurse.  */
17672       }
17673     }
17674 }
17675 
17676 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17677    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17678    node to so that it points to the node itself, thus indicating that the
17679    node represents its own (abstract) origin.  Additionally, if the
17680    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17681    the decl/block tree of which the given node is the root of, and for
17682    each other ..._DECL or BLOCK node contained therein whose
17683    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17684    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17685    point to themselves.  */
17686 
17687 static void
set_decl_origin_self(tree decl)17688 set_decl_origin_self (tree decl)
17689 {
17690   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17691     {
17692       DECL_ABSTRACT_ORIGIN (decl) = decl;
17693       if (TREE_CODE (decl) == FUNCTION_DECL)
17694 	{
17695 	  tree arg;
17696 
17697 	  for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17698 	    DECL_ABSTRACT_ORIGIN (arg) = arg;
17699 	  if (DECL_INITIAL (decl) != NULL_TREE
17700 	      && DECL_INITIAL (decl) != error_mark_node)
17701 	    set_block_origin_self (DECL_INITIAL (decl));
17702 	}
17703     }
17704 }
17705 
17706 /* Given a pointer to some BLOCK node, and a boolean value to set the
17707    "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17708    the given block, and for all local decls and all local sub-blocks
17709    (recursively) which are contained therein.  */
17710 
17711 static void
set_block_abstract_flags(tree stmt,int setting)17712 set_block_abstract_flags (tree stmt, int setting)
17713 {
17714   tree local_decl;
17715   tree subblock;
17716   unsigned int i;
17717 
17718   BLOCK_ABSTRACT (stmt) = setting;
17719 
17720   for (local_decl = BLOCK_VARS (stmt);
17721        local_decl != NULL_TREE;
17722        local_decl = DECL_CHAIN (local_decl))
17723     if (! DECL_EXTERNAL (local_decl))
17724       set_decl_abstract_flags (local_decl, setting);
17725 
17726   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17727     {
17728       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17729       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17730 	  || TREE_CODE (local_decl) == PARM_DECL)
17731 	set_decl_abstract_flags (local_decl, setting);
17732     }
17733 
17734   for (subblock = BLOCK_SUBBLOCKS (stmt);
17735        subblock != NULL_TREE;
17736        subblock = BLOCK_CHAIN (subblock))
17737     set_block_abstract_flags (subblock, setting);
17738 }
17739 
17740 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17741    "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17742    given decl, and (in the case where the decl is a FUNCTION_DECL) also
17743    set the abstract flags for all of the parameters, local vars, local
17744    blocks and sub-blocks (recursively) to the same setting.  */
17745 
17746 static void
set_decl_abstract_flags(tree decl,int setting)17747 set_decl_abstract_flags (tree decl, int setting)
17748 {
17749   DECL_ABSTRACT (decl) = setting;
17750   if (TREE_CODE (decl) == FUNCTION_DECL)
17751     {
17752       tree arg;
17753 
17754       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17755 	DECL_ABSTRACT (arg) = setting;
17756       if (DECL_INITIAL (decl) != NULL_TREE
17757 	  && DECL_INITIAL (decl) != error_mark_node)
17758 	set_block_abstract_flags (DECL_INITIAL (decl), setting);
17759     }
17760 }
17761 
17762 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17763    may later generate inlined and/or out-of-line instances of.  */
17764 
17765 static void
dwarf2out_abstract_function(tree decl)17766 dwarf2out_abstract_function (tree decl)
17767 {
17768   dw_die_ref old_die;
17769   tree save_fn;
17770   tree context;
17771   int was_abstract;
17772   htab_t old_decl_loc_table;
17773   htab_t old_cached_dw_loc_list_table;
17774   int old_call_site_count, old_tail_call_site_count;
17775   struct call_arg_loc_node *old_call_arg_locations;
17776 
17777   /* Make sure we have the actual abstract inline, not a clone.  */
17778   decl = DECL_ORIGIN (decl);
17779 
17780   old_die = lookup_decl_die (decl);
17781   if (old_die && get_AT (old_die, DW_AT_inline))
17782     /* We've already generated the abstract instance.  */
17783     return;
17784 
17785   /* We can be called while recursively when seeing block defining inlined subroutine
17786      DIE.  Be sure to not clobber the outer location table nor use it or we would
17787      get locations in abstract instantces.  */
17788   old_decl_loc_table = decl_loc_table;
17789   decl_loc_table = NULL;
17790   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17791   cached_dw_loc_list_table = NULL;
17792   old_call_arg_locations = call_arg_locations;
17793   call_arg_locations = NULL;
17794   old_call_site_count = call_site_count;
17795   call_site_count = -1;
17796   old_tail_call_site_count = tail_call_site_count;
17797   tail_call_site_count = -1;
17798 
17799   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17800      we don't get confused by DECL_ABSTRACT.  */
17801   if (debug_info_level > DINFO_LEVEL_TERSE)
17802     {
17803       context = decl_class_context (decl);
17804       if (context)
17805 	gen_type_die_for_member
17806 	  (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17807     }
17808 
17809   /* Pretend we've just finished compiling this function.  */
17810   save_fn = current_function_decl;
17811   current_function_decl = decl;
17812 
17813   was_abstract = DECL_ABSTRACT (decl);
17814   set_decl_abstract_flags (decl, 1);
17815   dwarf2out_decl (decl);
17816   if (! was_abstract)
17817     set_decl_abstract_flags (decl, 0);
17818 
17819   current_function_decl = save_fn;
17820   decl_loc_table = old_decl_loc_table;
17821   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17822   call_arg_locations = old_call_arg_locations;
17823   call_site_count = old_call_site_count;
17824   tail_call_site_count = old_tail_call_site_count;
17825 }
17826 
17827 /* Helper function of premark_used_types() which gets called through
17828    htab_traverse.
17829 
17830    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17831    marked as unused by prune_unused_types.  */
17832 
17833 static int
premark_used_types_helper(void ** slot,void * data ATTRIBUTE_UNUSED)17834 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17835 {
17836   tree type;
17837   dw_die_ref die;
17838 
17839   type = (tree) *slot;
17840   die = lookup_type_die (type);
17841   if (die != NULL)
17842     die->die_perennial_p = 1;
17843   return 1;
17844 }
17845 
17846 /* Helper function of premark_types_used_by_global_vars which gets called
17847    through htab_traverse.
17848 
17849    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17850    marked as unused by prune_unused_types. The DIE of the type is marked
17851    only if the global variable using the type will actually be emitted.  */
17852 
17853 static int
premark_types_used_by_global_vars_helper(void ** slot,void * data ATTRIBUTE_UNUSED)17854 premark_types_used_by_global_vars_helper (void **slot,
17855 					  void *data ATTRIBUTE_UNUSED)
17856 {
17857   struct types_used_by_vars_entry *entry;
17858   dw_die_ref die;
17859 
17860   entry = (struct types_used_by_vars_entry *) *slot;
17861   gcc_assert (entry->type != NULL
17862 	      && entry->var_decl != NULL);
17863   die = lookup_type_die (entry->type);
17864   if (die)
17865     {
17866       /* Ask cgraph if the global variable really is to be emitted.
17867          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17868       varpool_node *node = varpool_get_node (entry->var_decl);
17869       if (node && node->definition)
17870 	{
17871 	  die->die_perennial_p = 1;
17872 	  /* Keep the parent DIEs as well.  */
17873 	  while ((die = die->die_parent) && die->die_perennial_p == 0)
17874 	    die->die_perennial_p = 1;
17875 	}
17876     }
17877   return 1;
17878 }
17879 
17880 /* Mark all members of used_types_hash as perennial.  */
17881 
17882 static void
premark_used_types(struct function * fun)17883 premark_used_types (struct function *fun)
17884 {
17885   if (fun && fun->used_types_hash)
17886     htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17887 }
17888 
17889 /* Mark all members of types_used_by_vars_entry as perennial.  */
17890 
17891 static void
premark_types_used_by_global_vars(void)17892 premark_types_used_by_global_vars (void)
17893 {
17894   if (types_used_by_vars_hash)
17895     htab_traverse (types_used_by_vars_hash,
17896 		   premark_types_used_by_global_vars_helper, NULL);
17897 }
17898 
17899 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17900    for CA_LOC call arg loc node.  */
17901 
17902 static dw_die_ref
gen_call_site_die(tree decl,dw_die_ref subr_die,struct call_arg_loc_node * ca_loc)17903 gen_call_site_die (tree decl, dw_die_ref subr_die,
17904 		   struct call_arg_loc_node *ca_loc)
17905 {
17906   dw_die_ref stmt_die = NULL, die;
17907   tree block = ca_loc->block;
17908 
17909   while (block
17910 	 && block != DECL_INITIAL (decl)
17911 	 && TREE_CODE (block) == BLOCK)
17912     {
17913       if (block_map.length () > BLOCK_NUMBER (block))
17914 	stmt_die = block_map[BLOCK_NUMBER (block)];
17915       if (stmt_die)
17916 	break;
17917       block = BLOCK_SUPERCONTEXT (block);
17918     }
17919   if (stmt_die == NULL)
17920     stmt_die = subr_die;
17921   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17922   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17923   if (ca_loc->tail_call_p)
17924     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17925   if (ca_loc->symbol_ref)
17926     {
17927       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17928       if (tdie)
17929 	add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17930       else
17931         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17932     }
17933   return die;
17934 }
17935 
17936 /* Generate a DIE to represent a declared function (either file-scope or
17937    block-local).  */
17938 
17939 static void
gen_subprogram_die(tree decl,dw_die_ref context_die)17940 gen_subprogram_die (tree decl, dw_die_ref context_die)
17941 {
17942   tree origin = decl_ultimate_origin (decl);
17943   dw_die_ref subr_die;
17944   tree outer_scope;
17945   dw_die_ref old_die = lookup_decl_die (decl);
17946   int declaration = (current_function_decl != decl
17947 		     || class_or_namespace_scope_p (context_die));
17948 
17949   premark_used_types (DECL_STRUCT_FUNCTION (decl));
17950 
17951   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17952      started to generate the abstract instance of an inline, decided to output
17953      its containing class, and proceeded to emit the declaration of the inline
17954      from the member list for the class.  If so, DECLARATION takes priority;
17955      we'll get back to the abstract instance when done with the class.  */
17956 
17957   /* The class-scope declaration DIE must be the primary DIE.  */
17958   if (origin && declaration && class_or_namespace_scope_p (context_die))
17959     {
17960       origin = NULL;
17961       gcc_assert (!old_die);
17962     }
17963 
17964   /* Now that the C++ front end lazily declares artificial member fns, we
17965      might need to retrofit the declaration into its class.  */
17966   if (!declaration && !origin && !old_die
17967       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17968       && !class_or_namespace_scope_p (context_die)
17969       && debug_info_level > DINFO_LEVEL_TERSE)
17970     old_die = force_decl_die (decl);
17971 
17972   if (origin != NULL)
17973     {
17974       gcc_assert (!declaration || local_scope_p (context_die));
17975 
17976       /* Fixup die_parent for the abstract instance of a nested
17977 	 inline function.  */
17978       if (old_die && old_die->die_parent == NULL)
17979 	add_child_die (context_die, old_die);
17980 
17981       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17982       add_abstract_origin_attribute (subr_die, origin);
17983       /*  This is where the actual code for a cloned function is.
17984 	  Let's emit linkage name attribute for it.  This helps
17985 	  debuggers to e.g, set breakpoints into
17986 	  constructors/destructors when the user asks "break
17987 	  K::K".  */
17988       add_linkage_name (subr_die, decl);
17989     }
17990   else if (old_die)
17991     {
17992       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17993       struct dwarf_file_data * file_index = lookup_filename (s.file);
17994 
17995       if (!get_AT_flag (old_die, DW_AT_declaration)
17996 	  /* We can have a normal definition following an inline one in the
17997 	     case of redefinition of GNU C extern inlines.
17998 	     It seems reasonable to use AT_specification in this case.  */
17999 	  && !get_AT (old_die, DW_AT_inline))
18000 	{
18001 	  /* Detect and ignore this case, where we are trying to output
18002 	     something we have already output.  */
18003 	  return;
18004 	}
18005 
18006       /* If the definition comes from the same place as the declaration,
18007 	 maybe use the old DIE.  We always want the DIE for this function
18008 	 that has the *_pc attributes to be under comp_unit_die so the
18009 	 debugger can find it.  We also need to do this for abstract
18010 	 instances of inlines, since the spec requires the out-of-line copy
18011 	 to have the same parent.  For local class methods, this doesn't
18012 	 apply; we just use the old DIE.  */
18013       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18014 	  && (DECL_ARTIFICIAL (decl)
18015 	      || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18016 		  && (get_AT_unsigned (old_die, DW_AT_decl_line)
18017 		      == (unsigned) s.line))))
18018 	{
18019 	  subr_die = old_die;
18020 
18021 	  /* Clear out the declaration attribute and the formal parameters.
18022 	     Do not remove all children, because it is possible that this
18023 	     declaration die was forced using force_decl_die(). In such
18024 	     cases die that forced declaration die (e.g. TAG_imported_module)
18025 	     is one of the children that we do not want to remove.  */
18026 	  remove_AT (subr_die, DW_AT_declaration);
18027 	  remove_AT (subr_die, DW_AT_object_pointer);
18028 	  remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18029 	}
18030       else
18031 	{
18032 	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18033 	  add_AT_specification (subr_die, old_die);
18034           add_pubname (decl, subr_die);
18035 	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18036 	    add_AT_file (subr_die, DW_AT_decl_file, file_index);
18037 	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18038 	    add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18039 
18040 	  /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18041 	     emit the real type on the definition die.  */
18042 	  if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18043 	    {
18044 	      dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18045 	      if (die == auto_die || die == decltype_auto_die)
18046 		add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18047 				    0, 0, context_die);
18048 	    }
18049 	}
18050     }
18051   else
18052     {
18053       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18054 
18055       if (TREE_PUBLIC (decl))
18056 	add_AT_flag (subr_die, DW_AT_external, 1);
18057 
18058       add_name_and_src_coords_attributes (subr_die, decl);
18059       add_pubname (decl, subr_die);
18060       if (debug_info_level > DINFO_LEVEL_TERSE)
18061 	{
18062 	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18063 	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18064 			      0, 0, context_die);
18065 	}
18066 
18067       add_pure_or_virtual_attribute (subr_die, decl);
18068       if (DECL_ARTIFICIAL (decl))
18069 	add_AT_flag (subr_die, DW_AT_artificial, 1);
18070 
18071       add_accessibility_attribute (subr_die, decl);
18072     }
18073 
18074   if (declaration)
18075     {
18076       if (!old_die || !get_AT (old_die, DW_AT_inline))
18077 	{
18078 	  add_AT_flag (subr_die, DW_AT_declaration, 1);
18079 
18080 	  /* If this is an explicit function declaration then generate
18081 	     a DW_AT_explicit attribute.  */
18082 	  if (lang_hooks.decls.function_decl_explicit_p (decl)
18083 	      && (dwarf_version >= 3 || !dwarf_strict))
18084 	    add_AT_flag (subr_die, DW_AT_explicit, 1);
18085 
18086 	  /* The first time we see a member function, it is in the context of
18087 	     the class to which it belongs.  We make sure of this by emitting
18088 	     the class first.  The next time is the definition, which is
18089 	     handled above.  The two may come from the same source text.
18090 
18091 	     Note that force_decl_die() forces function declaration die. It is
18092 	     later reused to represent definition.  */
18093 	  equate_decl_number_to_die (decl, subr_die);
18094 	}
18095     }
18096   else if (DECL_ABSTRACT (decl))
18097     {
18098       if (DECL_DECLARED_INLINE_P (decl))
18099 	{
18100 	  if (cgraph_function_possibly_inlined_p (decl))
18101 	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18102 	  else
18103 	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18104 	}
18105       else
18106 	{
18107 	  if (cgraph_function_possibly_inlined_p (decl))
18108 	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18109 	  else
18110 	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18111 	}
18112 
18113       if (DECL_DECLARED_INLINE_P (decl)
18114 	  && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18115 	add_AT_flag (subr_die, DW_AT_artificial, 1);
18116 
18117       equate_decl_number_to_die (decl, subr_die);
18118     }
18119   else if (!DECL_EXTERNAL (decl))
18120     {
18121       HOST_WIDE_INT cfa_fb_offset;
18122       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18123 
18124       if (!old_die || !get_AT (old_die, DW_AT_inline))
18125 	equate_decl_number_to_die (decl, subr_die);
18126 
18127       gcc_checking_assert (fun);
18128       if (!flag_reorder_blocks_and_partition)
18129 	{
18130 	  dw_fde_ref fde = fun->fde;
18131 	  if (fde->dw_fde_begin)
18132 	    {
18133 	      /* We have already generated the labels.  */
18134              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18135                                  fde->dw_fde_end, false);
18136 	    }
18137 	  else
18138 	    {
18139 	      /* Create start/end labels and add the range.  */
18140 	      char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18141 	      char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18142 	      ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18143 					   current_function_funcdef_no);
18144 	      ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18145 					   current_function_funcdef_no);
18146              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18147                                  false);
18148 	    }
18149 
18150 #if VMS_DEBUGGING_INFO
18151       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18152 	 Section 2.3 Prologue and Epilogue Attributes:
18153 	 When a breakpoint is set on entry to a function, it is generally
18154 	 desirable for execution to be suspended, not on the very first
18155 	 instruction of the function, but rather at a point after the
18156 	 function's frame has been set up, after any language defined local
18157 	 declaration processing has been completed, and before execution of
18158 	 the first statement of the function begins. Debuggers generally
18159 	 cannot properly determine where this point is.  Similarly for a
18160 	 breakpoint set on exit from a function. The prologue and epilogue
18161 	 attributes allow a compiler to communicate the location(s) to use.  */
18162 
18163       {
18164         if (fde->dw_fde_vms_end_prologue)
18165           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18166 	    fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18167 
18168         if (fde->dw_fde_vms_begin_epilogue)
18169           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18170 	    fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18171       }
18172 #endif
18173 
18174 	}
18175       else
18176 	{
18177 	  /* Generate pubnames entries for the split function code ranges.  */
18178 	  dw_fde_ref fde = fun->fde;
18179 
18180 	  if (fde->dw_fde_second_begin)
18181 	    {
18182 	      if (dwarf_version >= 3 || !dwarf_strict)
18183 		{
18184 		  /* We should use ranges for non-contiguous code section
18185 		     addresses.  Use the actual code range for the initial
18186 		     section, since the HOT/COLD labels might precede an
18187 		     alignment offset.  */
18188 		  bool range_list_added = false;
18189 		  add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18190                                         fde->dw_fde_end, &range_list_added,
18191                                         false);
18192 		  add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18193 					fde->dw_fde_second_end,
18194                                        &range_list_added, false);
18195 		  if (range_list_added)
18196 		    add_ranges (NULL);
18197 		}
18198 	      else
18199 		{
18200 		  /* There is no real support in DW2 for this .. so we make
18201 		     a work-around.  First, emit the pub name for the segment
18202 		     containing the function label.  Then make and emit a
18203 		     simplified subprogram DIE for the second segment with the
18204 		     name pre-fixed by __hot/cold_sect_of_.  We use the same
18205 		     linkage name for the second die so that gdb will find both
18206 		     sections when given "b foo".  */
18207 		  const char *name = NULL;
18208 		  tree decl_name = DECL_NAME (decl);
18209 		  dw_die_ref seg_die;
18210 
18211 		  /* Do the 'primary' section.   */
18212 		  add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18213                                       fde->dw_fde_end, false);
18214 
18215 		  /* Build a minimal DIE for the secondary section.  */
18216 		  seg_die = new_die (DW_TAG_subprogram,
18217 				     subr_die->die_parent, decl);
18218 
18219 		  if (TREE_PUBLIC (decl))
18220 		    add_AT_flag (seg_die, DW_AT_external, 1);
18221 
18222 		  if (decl_name != NULL
18223 		      && IDENTIFIER_POINTER (decl_name) != NULL)
18224 		    {
18225 		      name = dwarf2_name (decl, 1);
18226 		      if (! DECL_ARTIFICIAL (decl))
18227 			add_src_coords_attributes (seg_die, decl);
18228 
18229 		      add_linkage_name (seg_die, decl);
18230 		    }
18231 		  gcc_assert (name != NULL);
18232 		  add_pure_or_virtual_attribute (seg_die, decl);
18233 		  if (DECL_ARTIFICIAL (decl))
18234 		    add_AT_flag (seg_die, DW_AT_artificial, 1);
18235 
18236 		  name = concat ("__second_sect_of_", name, NULL);
18237 		  add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18238                                       fde->dw_fde_second_end, false);
18239 		  add_name_attribute (seg_die, name);
18240 		  if (want_pubnames ())
18241 		    add_pubname_string (name, seg_die);
18242 		}
18243 	    }
18244 	  else
18245            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18246                                false);
18247 	}
18248 
18249       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18250 
18251       /* We define the "frame base" as the function's CFA.  This is more
18252 	 convenient for several reasons: (1) It's stable across the prologue
18253 	 and epilogue, which makes it better than just a frame pointer,
18254 	 (2) With dwarf3, there exists a one-byte encoding that allows us
18255 	 to reference the .debug_frame data by proxy, but failing that,
18256 	 (3) We can at least reuse the code inspection and interpretation
18257 	 code that determines the CFA position at various points in the
18258 	 function.  */
18259       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18260 	{
18261 	  dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18262 	  add_AT_loc (subr_die, DW_AT_frame_base, op);
18263 	}
18264       else
18265 	{
18266 	  dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18267 	  if (list->dw_loc_next)
18268 	    add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18269 	  else
18270 	    add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18271 	}
18272 
18273       /* Compute a displacement from the "steady-state frame pointer" to
18274 	 the CFA.  The former is what all stack slots and argument slots
18275 	 will reference in the rtl; the latter is what we've told the
18276 	 debugger about.  We'll need to adjust all frame_base references
18277 	 by this displacement.  */
18278       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18279 
18280       if (fun->static_chain_decl)
18281 	add_AT_location_description (subr_die, DW_AT_static_link,
18282 		 loc_list_from_tree (fun->static_chain_decl, 2));
18283     }
18284 
18285   /* Generate child dies for template paramaters.  */
18286   if (debug_info_level > DINFO_LEVEL_TERSE)
18287     gen_generic_params_dies (decl);
18288 
18289   /* Now output descriptions of the arguments for this function. This gets
18290      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18291      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18292      `...' at the end of the formal parameter list.  In order to find out if
18293      there was a trailing ellipsis or not, we must instead look at the type
18294      associated with the FUNCTION_DECL.  This will be a node of type
18295      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18296      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18297      an ellipsis at the end.  */
18298 
18299   /* In the case where we are describing a mere function declaration, all we
18300      need to do here (and all we *can* do here) is to describe the *types* of
18301      its formal parameters.  */
18302   if (debug_info_level <= DINFO_LEVEL_TERSE)
18303     ;
18304   else if (declaration)
18305     gen_formal_types_die (decl, subr_die);
18306   else
18307     {
18308       /* Generate DIEs to represent all known formal parameters.  */
18309       tree parm = DECL_ARGUMENTS (decl);
18310       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18311       tree generic_decl_parm = generic_decl
18312 				? DECL_ARGUMENTS (generic_decl)
18313 				: NULL;
18314 
18315       /* Now we want to walk the list of parameters of the function and
18316 	 emit their relevant DIEs.
18317 
18318 	 We consider the case of DECL being an instance of a generic function
18319 	 as well as it being a normal function.
18320 
18321 	 If DECL is an instance of a generic function we walk the
18322 	 parameters of the generic function declaration _and_ the parameters of
18323 	 DECL itself. This is useful because we want to emit specific DIEs for
18324 	 function parameter packs and those are declared as part of the
18325 	 generic function declaration. In that particular case,
18326 	 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18327 	 That DIE has children DIEs representing the set of arguments
18328 	 of the pack. Note that the set of pack arguments can be empty.
18329 	 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18330 	 children DIE.
18331 
18332 	 Otherwise, we just consider the parameters of DECL.  */
18333       while (generic_decl_parm || parm)
18334 	{
18335 	  if (generic_decl_parm
18336 	      && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18337 	    gen_formal_parameter_pack_die (generic_decl_parm,
18338 					   parm, subr_die,
18339 					   &parm);
18340 	  else if (parm)
18341 	    {
18342 	      dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18343 
18344 	      if (parm == DECL_ARGUMENTS (decl)
18345 		  && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18346 		  && parm_die
18347 		  && (dwarf_version >= 3 || !dwarf_strict))
18348 		add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18349 
18350 	      parm = DECL_CHAIN (parm);
18351 	    }
18352 
18353 	  if (generic_decl_parm)
18354 	    generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18355 	}
18356 
18357       /* Decide whether we need an unspecified_parameters DIE at the end.
18358 	 There are 2 more cases to do this for: 1) the ansi ... declaration -
18359 	 this is detectable when the end of the arg list is not a
18360 	 void_type_node 2) an unprototyped function declaration (not a
18361 	 definition).  This just means that we have no info about the
18362 	 parameters at all.  */
18363       if (prototype_p (TREE_TYPE (decl)))
18364 	{
18365 	  /* This is the prototyped case, check for....  */
18366 	  if (stdarg_p (TREE_TYPE (decl)))
18367 	    gen_unspecified_parameters_die (decl, subr_die);
18368 	}
18369       else if (DECL_INITIAL (decl) == NULL_TREE)
18370 	gen_unspecified_parameters_die (decl, subr_die);
18371     }
18372 
18373   /* Output Dwarf info for all of the stuff within the body of the function
18374      (if it has one - it may be just a declaration).  */
18375   outer_scope = DECL_INITIAL (decl);
18376 
18377   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18378      a function.  This BLOCK actually represents the outermost binding contour
18379      for the function, i.e. the contour in which the function's formal
18380      parameters and labels get declared. Curiously, it appears that the front
18381      end doesn't actually put the PARM_DECL nodes for the current function onto
18382      the BLOCK_VARS list for this outer scope, but are strung off of the
18383      DECL_ARGUMENTS list for the function instead.
18384 
18385      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18386      the LABEL_DECL nodes for the function however, and we output DWARF info
18387      for those in decls_for_scope.  Just within the `outer_scope' there will be
18388      a BLOCK node representing the function's outermost pair of curly braces,
18389      and any blocks used for the base and member initializers of a C++
18390      constructor function.  */
18391   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18392     {
18393       int call_site_note_count = 0;
18394       int tail_call_site_note_count = 0;
18395 
18396       /* Emit a DW_TAG_variable DIE for a named return value.  */
18397       if (DECL_NAME (DECL_RESULT (decl)))
18398 	gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18399 
18400       current_function_has_inlines = 0;
18401       decls_for_scope (outer_scope, subr_die, 0);
18402 
18403       if (call_arg_locations && !dwarf_strict)
18404 	{
18405 	  struct call_arg_loc_node *ca_loc;
18406 	  for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18407 	    {
18408 	      dw_die_ref die = NULL;
18409 	      rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18410 	      rtx arg, next_arg;
18411 
18412 	      for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18413 		   arg; arg = next_arg)
18414 		{
18415 		  dw_loc_descr_ref reg, val;
18416 		  enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18417 		  dw_die_ref cdie, tdie = NULL;
18418 
18419 		  next_arg = XEXP (arg, 1);
18420 		  if (REG_P (XEXP (XEXP (arg, 0), 0))
18421 		      && next_arg
18422 		      && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18423 		      && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18424 		      && REGNO (XEXP (XEXP (arg, 0), 0))
18425 			 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18426 		    next_arg = XEXP (next_arg, 1);
18427 		  if (mode == VOIDmode)
18428 		    {
18429 		      mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18430 		      if (mode == VOIDmode)
18431 			mode = GET_MODE (XEXP (arg, 0));
18432 		    }
18433 		  if (mode == VOIDmode || mode == BLKmode)
18434 		    continue;
18435 		  if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18436 		    {
18437 		      gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18438 		      tloc = XEXP (XEXP (arg, 0), 1);
18439 		      continue;
18440 		    }
18441 		  else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18442 			   && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18443 		    {
18444 		      gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18445 		      tlocc = XEXP (XEXP (arg, 0), 1);
18446 		      continue;
18447 		    }
18448 		  reg = NULL;
18449 		  if (REG_P (XEXP (XEXP (arg, 0), 0)))
18450 		    reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18451 					      VAR_INIT_STATUS_INITIALIZED);
18452 		  else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18453 		    {
18454 		      rtx mem = XEXP (XEXP (arg, 0), 0);
18455 		      reg = mem_loc_descriptor (XEXP (mem, 0),
18456 						get_address_mode (mem),
18457 						GET_MODE (mem),
18458 						VAR_INIT_STATUS_INITIALIZED);
18459 		    }
18460 		  else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18461 			   == DEBUG_PARAMETER_REF)
18462 		    {
18463 		      tree tdecl
18464 			= DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18465 		      tdie = lookup_decl_die (tdecl);
18466 		      if (tdie == NULL)
18467 			continue;
18468 		    }
18469 		  else
18470 		    continue;
18471 		  if (reg == NULL
18472 		      && GET_CODE (XEXP (XEXP (arg, 0), 0))
18473 			 != DEBUG_PARAMETER_REF)
18474 		    continue;
18475 		  val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18476 					    VOIDmode,
18477 					    VAR_INIT_STATUS_INITIALIZED);
18478 		  if (val == NULL)
18479 		    continue;
18480 		  if (die == NULL)
18481 		    die = gen_call_site_die (decl, subr_die, ca_loc);
18482 		  cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18483 				  NULL_TREE);
18484 		  if (reg != NULL)
18485 		    add_AT_loc (cdie, DW_AT_location, reg);
18486 		  else if (tdie != NULL)
18487 		    add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18488 		  add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18489 		  if (next_arg != XEXP (arg, 1))
18490 		    {
18491 		      mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18492 		      if (mode == VOIDmode)
18493 			mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18494 		      val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18495 							    0), 1),
18496 						mode, VOIDmode,
18497 						VAR_INIT_STATUS_INITIALIZED);
18498 		      if (val != NULL)
18499 			add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18500 		    }
18501 		}
18502 	      if (die == NULL
18503 		  && (ca_loc->symbol_ref || tloc))
18504 		die = gen_call_site_die (decl, subr_die, ca_loc);
18505 	      if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18506 		{
18507 		  dw_loc_descr_ref tval = NULL;
18508 
18509 		  if (tloc != NULL_RTX)
18510 		    tval = mem_loc_descriptor (tloc,
18511 					       GET_MODE (tloc) == VOIDmode
18512 					       ? Pmode : GET_MODE (tloc),
18513 					       VOIDmode,
18514 					       VAR_INIT_STATUS_INITIALIZED);
18515 		  if (tval)
18516 		    add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18517 		  else if (tlocc != NULL_RTX)
18518 		    {
18519 		      tval = mem_loc_descriptor (tlocc,
18520 						 GET_MODE (tlocc) == VOIDmode
18521 						 ? Pmode : GET_MODE (tlocc),
18522 						 VOIDmode,
18523 						 VAR_INIT_STATUS_INITIALIZED);
18524 		      if (tval)
18525 			add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18526 				    tval);
18527 		    }
18528 		}
18529 	      if (die != NULL)
18530 		{
18531 		  call_site_note_count++;
18532 		  if (ca_loc->tail_call_p)
18533 		    tail_call_site_note_count++;
18534 		}
18535 	    }
18536 	}
18537       call_arg_locations = NULL;
18538       call_arg_loc_last = NULL;
18539       if (tail_call_site_count >= 0
18540 	  && tail_call_site_count == tail_call_site_note_count
18541 	  && !dwarf_strict)
18542 	{
18543 	  if (call_site_count >= 0
18544 	      && call_site_count == call_site_note_count)
18545 	    add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18546 	  else
18547 	    add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18548 	}
18549       call_site_count = -1;
18550       tail_call_site_count = -1;
18551     }
18552 
18553   if (subr_die != old_die)
18554     /* Add the calling convention attribute if requested.  */
18555     add_calling_convention_attribute (subr_die, decl);
18556 }
18557 
18558 /* Returns a hash value for X (which really is a die_struct).  */
18559 
18560 static hashval_t
common_block_die_table_hash(const void * x)18561 common_block_die_table_hash (const void *x)
18562 {
18563   const_dw_die_ref d = (const_dw_die_ref) x;
18564   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18565 }
18566 
18567 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18568    as decl_id and die_parent of die_struct Y.  */
18569 
18570 static int
common_block_die_table_eq(const void * x,const void * y)18571 common_block_die_table_eq (const void *x, const void *y)
18572 {
18573   const_dw_die_ref d = (const_dw_die_ref) x;
18574   const_dw_die_ref e = (const_dw_die_ref) y;
18575   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18576 }
18577 
18578 /* Generate a DIE to represent a declared data object.
18579    Either DECL or ORIGIN must be non-null.  */
18580 
18581 static void
gen_variable_die(tree decl,tree origin,dw_die_ref context_die)18582 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18583 {
18584   HOST_WIDE_INT off = 0;
18585   tree com_decl;
18586   tree decl_or_origin = decl ? decl : origin;
18587   tree ultimate_origin;
18588   dw_die_ref var_die;
18589   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18590   dw_die_ref origin_die;
18591   bool declaration = (DECL_EXTERNAL (decl_or_origin)
18592 		      || class_or_namespace_scope_p (context_die));
18593   bool specialization_p = false;
18594 
18595   ultimate_origin = decl_ultimate_origin (decl_or_origin);
18596   if (decl || ultimate_origin)
18597     origin = ultimate_origin;
18598   com_decl = fortran_common (decl_or_origin, &off);
18599 
18600   /* Symbol in common gets emitted as a child of the common block, in the form
18601      of a data member.  */
18602   if (com_decl)
18603     {
18604       dw_die_ref com_die;
18605       dw_loc_list_ref loc;
18606       die_node com_die_arg;
18607 
18608       var_die = lookup_decl_die (decl_or_origin);
18609       if (var_die)
18610 	{
18611 	  if (get_AT (var_die, DW_AT_location) == NULL)
18612 	    {
18613 	      loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18614 	      if (loc)
18615 		{
18616 		  if (off)
18617 		    {
18618 		      /* Optimize the common case.  */
18619 		      if (single_element_loc_list_p (loc)
18620 			  && loc->expr->dw_loc_opc == DW_OP_addr
18621 			  && loc->expr->dw_loc_next == NULL
18622 			  && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18623 			     == SYMBOL_REF)
18624 			{
18625 			  rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18626 			  loc->expr->dw_loc_oprnd1.v.val_addr
18627 			    = plus_constant (GET_MODE (x), x , off);
18628 			}
18629 		      else
18630 			loc_list_plus_const (loc, off);
18631 		    }
18632 		  add_AT_location_description (var_die, DW_AT_location, loc);
18633 		  remove_AT (var_die, DW_AT_declaration);
18634 		}
18635 	    }
18636 	  return;
18637 	}
18638 
18639       if (common_block_die_table == NULL)
18640 	common_block_die_table
18641 	  = htab_create_ggc (10, common_block_die_table_hash,
18642 			     common_block_die_table_eq, NULL);
18643 
18644       com_die_arg.decl_id = DECL_UID (com_decl);
18645       com_die_arg.die_parent = context_die;
18646       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18647       loc = loc_list_from_tree (com_decl, 2);
18648       if (com_die == NULL)
18649 	{
18650 	  const char *cnam
18651 	    = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18652 	  void **slot;
18653 
18654 	  com_die = new_die (DW_TAG_common_block, context_die, decl);
18655 	  add_name_and_src_coords_attributes (com_die, com_decl);
18656 	  if (loc)
18657 	    {
18658 	      add_AT_location_description (com_die, DW_AT_location, loc);
18659 	      /* Avoid sharing the same loc descriptor between
18660 		 DW_TAG_common_block and DW_TAG_variable.  */
18661 	      loc = loc_list_from_tree (com_decl, 2);
18662 	    }
18663           else if (DECL_EXTERNAL (decl))
18664 	    add_AT_flag (com_die, DW_AT_declaration, 1);
18665 	  if (want_pubnames ())
18666 	    add_pubname_string (cnam, com_die); /* ??? needed? */
18667 	  com_die->decl_id = DECL_UID (com_decl);
18668 	  slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18669 	  *slot = (void *) com_die;
18670 	}
18671       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18672 	{
18673 	  add_AT_location_description (com_die, DW_AT_location, loc);
18674 	  loc = loc_list_from_tree (com_decl, 2);
18675 	  remove_AT (com_die, DW_AT_declaration);
18676 	}
18677       var_die = new_die (DW_TAG_variable, com_die, decl);
18678       add_name_and_src_coords_attributes (var_die, decl);
18679       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18680 			  TREE_THIS_VOLATILE (decl), context_die);
18681       add_AT_flag (var_die, DW_AT_external, 1);
18682       if (loc)
18683 	{
18684 	  if (off)
18685 	    {
18686 	      /* Optimize the common case.  */
18687 	      if (single_element_loc_list_p (loc)
18688                   && loc->expr->dw_loc_opc == DW_OP_addr
18689 		  && loc->expr->dw_loc_next == NULL
18690 		  && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18691 		{
18692 		  rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18693 		  loc->expr->dw_loc_oprnd1.v.val_addr
18694 		    = plus_constant (GET_MODE (x), x, off);
18695 		}
18696 	      else
18697 		loc_list_plus_const (loc, off);
18698 	    }
18699 	  add_AT_location_description (var_die, DW_AT_location, loc);
18700 	}
18701       else if (DECL_EXTERNAL (decl))
18702 	add_AT_flag (var_die, DW_AT_declaration, 1);
18703       equate_decl_number_to_die (decl, var_die);
18704       return;
18705     }
18706 
18707   /* If the compiler emitted a definition for the DECL declaration
18708      and if we already emitted a DIE for it, don't emit a second
18709      DIE for it again. Allow re-declarations of DECLs that are
18710      inside functions, though.  */
18711   if (old_die && declaration && !local_scope_p (context_die))
18712     return;
18713 
18714   /* For static data members, the declaration in the class is supposed
18715      to have DW_TAG_member tag; the specification should still be
18716      DW_TAG_variable referencing the DW_TAG_member DIE.  */
18717   if (declaration && class_scope_p (context_die))
18718     var_die = new_die (DW_TAG_member, context_die, decl);
18719   else
18720     var_die = new_die (DW_TAG_variable, context_die, decl);
18721 
18722   origin_die = NULL;
18723   if (origin != NULL)
18724     origin_die = add_abstract_origin_attribute (var_die, origin);
18725 
18726   /* Loop unrolling can create multiple blocks that refer to the same
18727      static variable, so we must test for the DW_AT_declaration flag.
18728 
18729      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18730      copy decls and set the DECL_ABSTRACT flag on them instead of
18731      sharing them.
18732 
18733      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18734 
18735      ??? The declare_in_namespace support causes us to get two DIEs for one
18736      variable, both of which are declarations.  We want to avoid considering
18737      one to be a specification, so we must test that this DIE is not a
18738      declaration.  */
18739   else if (old_die && TREE_STATIC (decl) && ! declaration
18740 	   && get_AT_flag (old_die, DW_AT_declaration) == 1)
18741     {
18742       /* This is a definition of a C++ class level static.  */
18743       add_AT_specification (var_die, old_die);
18744       specialization_p = true;
18745       if (DECL_NAME (decl))
18746 	{
18747 	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18748 	  struct dwarf_file_data * file_index = lookup_filename (s.file);
18749 
18750 	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18751 	    add_AT_file (var_die, DW_AT_decl_file, file_index);
18752 
18753 	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18754 	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18755 
18756 	  if (old_die->die_tag == DW_TAG_member)
18757 	    add_linkage_name (var_die, decl);
18758 	}
18759     }
18760   else
18761     add_name_and_src_coords_attributes (var_die, decl);
18762 
18763   if ((origin == NULL && !specialization_p)
18764       || (origin != NULL
18765 	  && !DECL_ABSTRACT (decl_or_origin)
18766 	  && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18767 				       decl_function_context
18768 							(decl_or_origin))))
18769     {
18770       tree type = TREE_TYPE (decl_or_origin);
18771 
18772       if (decl_by_reference_p (decl_or_origin))
18773 	add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18774       else
18775 	add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18776 			    TREE_THIS_VOLATILE (decl_or_origin), context_die);
18777     }
18778 
18779   if (origin == NULL && !specialization_p)
18780     {
18781       if (TREE_PUBLIC (decl))
18782 	add_AT_flag (var_die, DW_AT_external, 1);
18783 
18784       if (DECL_ARTIFICIAL (decl))
18785 	add_AT_flag (var_die, DW_AT_artificial, 1);
18786 
18787       add_accessibility_attribute (var_die, decl);
18788     }
18789 
18790   if (declaration)
18791     add_AT_flag (var_die, DW_AT_declaration, 1);
18792 
18793   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18794     equate_decl_number_to_die (decl, var_die);
18795 
18796   if (! declaration
18797       && (! DECL_ABSTRACT (decl_or_origin)
18798 	  /* Local static vars are shared between all clones/inlines,
18799 	     so emit DW_AT_location on the abstract DIE if DECL_RTL is
18800 	     already set.  */
18801 	  || (TREE_CODE (decl_or_origin) == VAR_DECL
18802 	      && TREE_STATIC (decl_or_origin)
18803 	      && DECL_RTL_SET_P (decl_or_origin)))
18804       /* When abstract origin already has DW_AT_location attribute, no need
18805 	 to add it again.  */
18806       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18807     {
18808       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18809           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18810 	defer_location (decl_or_origin, var_die);
18811       else
18812         add_location_or_const_value_attribute (var_die, decl_or_origin,
18813 					       decl == NULL, DW_AT_location);
18814       add_pubname (decl_or_origin, var_die);
18815     }
18816   else
18817     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18818 }
18819 
18820 /* Generate a DIE to represent a named constant.  */
18821 
18822 static void
gen_const_die(tree decl,dw_die_ref context_die)18823 gen_const_die (tree decl, dw_die_ref context_die)
18824 {
18825   dw_die_ref const_die;
18826   tree type = TREE_TYPE (decl);
18827 
18828   const_die = new_die (DW_TAG_constant, context_die, decl);
18829   add_name_and_src_coords_attributes (const_die, decl);
18830   add_type_attribute (const_die, type, 1, 0, context_die);
18831   if (TREE_PUBLIC (decl))
18832     add_AT_flag (const_die, DW_AT_external, 1);
18833   if (DECL_ARTIFICIAL (decl))
18834     add_AT_flag (const_die, DW_AT_artificial, 1);
18835   tree_add_const_value_attribute_for_decl (const_die, decl);
18836 }
18837 
18838 /* Generate a DIE to represent a label identifier.  */
18839 
18840 static void
gen_label_die(tree decl,dw_die_ref context_die)18841 gen_label_die (tree decl, dw_die_ref context_die)
18842 {
18843   tree origin = decl_ultimate_origin (decl);
18844   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18845   rtx insn;
18846   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18847 
18848   if (origin != NULL)
18849     add_abstract_origin_attribute (lbl_die, origin);
18850   else
18851     add_name_and_src_coords_attributes (lbl_die, decl);
18852 
18853   if (DECL_ABSTRACT (decl))
18854     equate_decl_number_to_die (decl, lbl_die);
18855   else
18856     {
18857       insn = DECL_RTL_IF_SET (decl);
18858 
18859       /* Deleted labels are programmer specified labels which have been
18860 	 eliminated because of various optimizations.  We still emit them
18861 	 here so that it is possible to put breakpoints on them.  */
18862       if (insn
18863 	  && (LABEL_P (insn)
18864 	      || ((NOTE_P (insn)
18865 	           && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18866 	{
18867 	  /* When optimization is enabled (via -O) some parts of the compiler
18868 	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18869 	     represent source-level labels which were explicitly declared by
18870 	     the user.  This really shouldn't be happening though, so catch
18871 	     it if it ever does happen.  */
18872 	  gcc_assert (!INSN_DELETED_P (insn));
18873 
18874 	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18875           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18876 	}
18877       else if (insn
18878 	       && NOTE_P (insn)
18879 	       && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18880 	       && CODE_LABEL_NUMBER (insn) != -1)
18881 	{
18882 	  ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18883           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18884 	}
18885     }
18886 }
18887 
18888 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18889    attributes to the DIE for a block STMT, to describe where the inlined
18890    function was called from.  This is similar to add_src_coords_attributes.  */
18891 
18892 static inline void
add_call_src_coords_attributes(tree stmt,dw_die_ref die)18893 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18894 {
18895   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18896 
18897   if (dwarf_version >= 3 || !dwarf_strict)
18898     {
18899       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18900       add_AT_unsigned (die, DW_AT_call_line, s.line);
18901     }
18902 }
18903 
18904 
18905 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18906    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18907 
18908 static inline void
add_high_low_attributes(tree stmt,dw_die_ref die)18909 add_high_low_attributes (tree stmt, dw_die_ref die)
18910 {
18911   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18912 
18913   if (BLOCK_FRAGMENT_CHAIN (stmt)
18914       && (dwarf_version >= 3 || !dwarf_strict))
18915     {
18916       tree chain, superblock = NULL_TREE;
18917       dw_die_ref pdie;
18918       dw_attr_ref attr = NULL;
18919 
18920       if (inlined_function_outer_scope_p (stmt))
18921 	{
18922 	  ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18923 				       BLOCK_NUMBER (stmt));
18924           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18925 	}
18926 
18927       /* Optimize duplicate .debug_ranges lists or even tails of
18928 	 lists.  If this BLOCK has same ranges as its supercontext,
18929 	 lookup DW_AT_ranges attribute in the supercontext (and
18930 	 recursively so), verify that the ranges_table contains the
18931 	 right values and use it instead of adding a new .debug_range.  */
18932       for (chain = stmt, pdie = die;
18933 	   BLOCK_SAME_RANGE (chain);
18934 	   chain = BLOCK_SUPERCONTEXT (chain))
18935 	{
18936 	  dw_attr_ref new_attr;
18937 
18938 	  pdie = pdie->die_parent;
18939 	  if (pdie == NULL)
18940 	    break;
18941 	  if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18942 	    break;
18943 	  new_attr = get_AT (pdie, DW_AT_ranges);
18944 	  if (new_attr == NULL
18945 	      || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18946 	    break;
18947 	  attr = new_attr;
18948 	  superblock = BLOCK_SUPERCONTEXT (chain);
18949 	}
18950       if (attr != NULL
18951 	  && (ranges_table[attr->dw_attr_val.v.val_offset
18952 			   / 2 / DWARF2_ADDR_SIZE].num
18953 	      == BLOCK_NUMBER (superblock))
18954 	  && BLOCK_FRAGMENT_CHAIN (superblock))
18955 	{
18956 	  unsigned long off = attr->dw_attr_val.v.val_offset
18957 			      / 2 / DWARF2_ADDR_SIZE;
18958 	  unsigned long supercnt = 0, thiscnt = 0;
18959 	  for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18960 	       chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18961 	    {
18962 	      ++supercnt;
18963 	      gcc_checking_assert (ranges_table[off + supercnt].num
18964 				   == BLOCK_NUMBER (chain));
18965 	    }
18966 	  gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18967 	  for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18968 	       chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18969 	    ++thiscnt;
18970 	  gcc_assert (supercnt >= thiscnt);
18971 	  add_AT_range_list (die, DW_AT_ranges,
18972                              ((off + supercnt - thiscnt)
18973                               * 2 * DWARF2_ADDR_SIZE),
18974                              false);
18975 	  return;
18976 	}
18977 
18978       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18979 
18980       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18981       do
18982 	{
18983 	  add_ranges (chain);
18984 	  chain = BLOCK_FRAGMENT_CHAIN (chain);
18985 	}
18986       while (chain);
18987       add_ranges (NULL);
18988     }
18989   else
18990     {
18991       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18992       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18993 				   BLOCK_NUMBER (stmt));
18994       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18995 				   BLOCK_NUMBER (stmt));
18996       add_AT_low_high_pc (die, label, label_high, false);
18997     }
18998 }
18999 
19000 /* Generate a DIE for a lexical block.  */
19001 
19002 static void
gen_lexical_block_die(tree stmt,dw_die_ref context_die,int depth)19003 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19004 {
19005   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19006 
19007   if (call_arg_locations)
19008     {
19009       if (block_map.length () <= BLOCK_NUMBER (stmt))
19010 	block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19011       block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19012     }
19013 
19014   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19015     add_high_low_attributes (stmt, stmt_die);
19016 
19017   decls_for_scope (stmt, stmt_die, depth);
19018 }
19019 
19020 /* Generate a DIE for an inlined subprogram.  */
19021 
19022 static void
gen_inlined_subroutine_die(tree stmt,dw_die_ref context_die,int depth)19023 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19024 {
19025   tree decl;
19026 
19027   /* The instance of function that is effectively being inlined shall not
19028      be abstract.  */
19029   gcc_assert (! BLOCK_ABSTRACT (stmt));
19030 
19031   decl = block_ultimate_origin (stmt);
19032 
19033   /* Emit info for the abstract instance first, if we haven't yet.  We
19034      must emit this even if the block is abstract, otherwise when we
19035      emit the block below (or elsewhere), we may end up trying to emit
19036      a die whose origin die hasn't been emitted, and crashing.  */
19037   dwarf2out_abstract_function (decl);
19038 
19039   if (! BLOCK_ABSTRACT (stmt))
19040     {
19041       dw_die_ref subr_die
19042 	= new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19043 
19044       if (call_arg_locations)
19045 	{
19046 	  if (block_map.length () <= BLOCK_NUMBER (stmt))
19047 	    block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19048 	  block_map[BLOCK_NUMBER (stmt)] = subr_die;
19049 	}
19050       add_abstract_origin_attribute (subr_die, decl);
19051       if (TREE_ASM_WRITTEN (stmt))
19052         add_high_low_attributes (stmt, subr_die);
19053       add_call_src_coords_attributes (stmt, subr_die);
19054 
19055       decls_for_scope (stmt, subr_die, depth);
19056       current_function_has_inlines = 1;
19057     }
19058 }
19059 
19060 /* Generate a DIE for a field in a record, or structure.  */
19061 
19062 static void
gen_field_die(tree decl,dw_die_ref context_die)19063 gen_field_die (tree decl, dw_die_ref context_die)
19064 {
19065   dw_die_ref decl_die;
19066 
19067   if (TREE_TYPE (decl) == error_mark_node)
19068     return;
19069 
19070   decl_die = new_die (DW_TAG_member, context_die, decl);
19071   add_name_and_src_coords_attributes (decl_die, decl);
19072   add_type_attribute (decl_die, member_declared_type (decl),
19073 		      TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19074 		      context_die);
19075 
19076   if (DECL_BIT_FIELD_TYPE (decl))
19077     {
19078       add_byte_size_attribute (decl_die, decl);
19079       add_bit_size_attribute (decl_die, decl);
19080       add_bit_offset_attribute (decl_die, decl);
19081     }
19082 
19083   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19084     add_data_member_location_attribute (decl_die, decl);
19085 
19086   if (DECL_ARTIFICIAL (decl))
19087     add_AT_flag (decl_die, DW_AT_artificial, 1);
19088 
19089   add_accessibility_attribute (decl_die, decl);
19090 
19091   /* Equate decl number to die, so that we can look up this decl later on.  */
19092   equate_decl_number_to_die (decl, decl_die);
19093 }
19094 
19095 #if 0
19096 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19097    Use modified_type_die instead.
19098    We keep this code here just in case these types of DIEs may be needed to
19099    represent certain things in other languages (e.g. Pascal) someday.  */
19100 
19101 static void
19102 gen_pointer_type_die (tree type, dw_die_ref context_die)
19103 {
19104   dw_die_ref ptr_die
19105     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19106 
19107   equate_type_number_to_die (type, ptr_die);
19108   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19109   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19110 }
19111 
19112 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19113    Use modified_type_die instead.
19114    We keep this code here just in case these types of DIEs may be needed to
19115    represent certain things in other languages (e.g. Pascal) someday.  */
19116 
19117 static void
19118 gen_reference_type_die (tree type, dw_die_ref context_die)
19119 {
19120   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19121 
19122   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19123     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19124   else
19125     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19126 
19127   equate_type_number_to_die (type, ref_die);
19128   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19129   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19130 }
19131 #endif
19132 
19133 /* Generate a DIE for a pointer to a member type.  */
19134 
19135 static void
gen_ptr_to_mbr_type_die(tree type,dw_die_ref context_die)19136 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19137 {
19138   dw_die_ref ptr_die
19139     = new_die (DW_TAG_ptr_to_member_type,
19140 	       scope_die_for (type, context_die), type);
19141 
19142   equate_type_number_to_die (type, ptr_die);
19143   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19144 		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19145   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19146 }
19147 
19148 typedef const char *dchar_p; /* For DEF_VEC_P.  */
19149 
19150 static char *producer_string;
19151 
19152 /* Return a heap allocated producer string including command line options
19153    if -grecord-gcc-switches.  */
19154 
19155 static char *
gen_producer_string(void)19156 gen_producer_string (void)
19157 {
19158   size_t j;
19159   auto_vec<dchar_p> switches;
19160   const char *language_string = lang_hooks.name;
19161   char *producer, *tail;
19162   const char *p;
19163   size_t len = dwarf_record_gcc_switches ? 0 : 3;
19164   size_t plen = strlen (language_string) + 1 + strlen (version_string);
19165 
19166   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19167     switch (save_decoded_options[j].opt_index)
19168       {
19169       case OPT_o:
19170       case OPT_d:
19171       case OPT_dumpbase:
19172       case OPT_dumpdir:
19173       case OPT_auxbase:
19174       case OPT_auxbase_strip:
19175       case OPT_quiet:
19176       case OPT_version:
19177       case OPT_v:
19178       case OPT_w:
19179       case OPT_L:
19180       case OPT_D:
19181       case OPT_I:
19182       case OPT_U:
19183       case OPT_SPECIAL_unknown:
19184       case OPT_SPECIAL_ignore:
19185       case OPT_SPECIAL_program_name:
19186       case OPT_SPECIAL_input_file:
19187       case OPT_grecord_gcc_switches:
19188       case OPT_gno_record_gcc_switches:
19189       case OPT__output_pch_:
19190       case OPT_fdiagnostics_show_location_:
19191       case OPT_fdiagnostics_show_option:
19192       case OPT_fdiagnostics_show_caret:
19193       case OPT_fdiagnostics_color_:
19194       case OPT_fverbose_asm:
19195       case OPT____:
19196       case OPT__sysroot_:
19197       case OPT_nostdinc:
19198       case OPT_nostdinc__:
19199 	/* Ignore these.  */
19200 	continue;
19201       default:
19202         if (cl_options[save_decoded_options[j].opt_index].flags
19203 	    & CL_NO_DWARF_RECORD)
19204 	  continue;
19205         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19206 			     == '-');
19207         switch (save_decoded_options[j].canonical_option[0][1])
19208 	  {
19209 	  case 'M':
19210 	  case 'i':
19211 	  case 'W':
19212 	    continue;
19213 	  case 'f':
19214 	    if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19215 			 "dump", 4) == 0)
19216 	      continue;
19217 	    break;
19218 	  default:
19219 	    break;
19220 	  }
19221 	switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19222 	len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19223 	break;
19224       }
19225 
19226   producer = XNEWVEC (char, plen + 1 + len + 1);
19227   tail = producer;
19228   sprintf (tail, "%s %s", language_string, version_string);
19229   tail += plen;
19230 
19231   FOR_EACH_VEC_ELT (switches, j, p)
19232     {
19233       len = strlen (p);
19234       *tail = ' ';
19235       memcpy (tail + 1, p, len);
19236       tail += len + 1;
19237     }
19238 
19239   *tail = '\0';
19240   return producer;
19241 }
19242 
19243 /* Generate the DIE for the compilation unit.  */
19244 
19245 static dw_die_ref
gen_compile_unit_die(const char * filename)19246 gen_compile_unit_die (const char *filename)
19247 {
19248   dw_die_ref die;
19249   const char *language_string = lang_hooks.name;
19250   int language;
19251 
19252   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19253 
19254   if (filename)
19255     {
19256       add_name_attribute (die, filename);
19257       /* Don't add cwd for <built-in>.  */
19258       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19259 	add_comp_dir_attribute (die);
19260     }
19261 
19262   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19263 
19264   /* If our producer is LTO try to figure out a common language to use
19265      from the global list of translation units.  */
19266   if (strcmp (language_string, "GNU GIMPLE") == 0)
19267     {
19268       unsigned i;
19269       tree t;
19270       const char *common_lang = NULL;
19271 
19272       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19273 	{
19274 	  if (!TRANSLATION_UNIT_LANGUAGE (t))
19275 	    continue;
19276 	  if (!common_lang)
19277 	    common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19278 	  else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19279 	    ;
19280 	  else if (strncmp (common_lang, "GNU C", 5) == 0
19281 		    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19282 	    /* Mixing C and C++ is ok, use C++ in that case.  */
19283 	    common_lang = "GNU C++";
19284 	  else
19285 	    {
19286 	      /* Fall back to C.  */
19287 	      common_lang = NULL;
19288 	      break;
19289 	    }
19290 	}
19291 
19292       if (common_lang)
19293 	language_string = common_lang;
19294     }
19295 
19296   language = DW_LANG_C89;
19297   if (strcmp (language_string, "GNU C++") == 0)
19298     language = DW_LANG_C_plus_plus;
19299   else if (strcmp (language_string, "GNU F77") == 0)
19300     language = DW_LANG_Fortran77;
19301   else if (strcmp (language_string, "GNU Pascal") == 0)
19302     language = DW_LANG_Pascal83;
19303   else if (dwarf_version >= 3 || !dwarf_strict)
19304     {
19305       if (strcmp (language_string, "GNU Ada") == 0)
19306 	language = DW_LANG_Ada95;
19307       else if (strcmp (language_string, "GNU Fortran") == 0)
19308 	language = DW_LANG_Fortran95;
19309       else if (strcmp (language_string, "GNU Java") == 0)
19310 	language = DW_LANG_Java;
19311       else if (strcmp (language_string, "GNU Objective-C") == 0)
19312 	language = DW_LANG_ObjC;
19313       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19314 	language = DW_LANG_ObjC_plus_plus;
19315       else if (dwarf_version >= 5 || !dwarf_strict)
19316 	{
19317 	  if (strcmp (language_string, "GNU Go") == 0)
19318 	    language = DW_LANG_Go;
19319 	}
19320     }
19321   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
19322   else if (strcmp (language_string, "GNU Fortran") == 0)
19323     language = DW_LANG_Fortran90;
19324 
19325   add_AT_unsigned (die, DW_AT_language, language);
19326 
19327   switch (language)
19328     {
19329     case DW_LANG_Fortran77:
19330     case DW_LANG_Fortran90:
19331     case DW_LANG_Fortran95:
19332       /* Fortran has case insensitive identifiers and the front-end
19333 	 lowercases everything.  */
19334       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19335       break;
19336     default:
19337       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19338       break;
19339     }
19340   return die;
19341 }
19342 
19343 /* Generate the DIE for a base class.  */
19344 
19345 static void
gen_inheritance_die(tree binfo,tree access,dw_die_ref context_die)19346 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19347 {
19348   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19349 
19350   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19351   add_data_member_location_attribute (die, binfo);
19352 
19353   if (BINFO_VIRTUAL_P (binfo))
19354     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19355 
19356   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19357      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19358      the default has always been DW_ACCESS_private.  */
19359   if (access == access_public_node)
19360     {
19361       if (dwarf_version == 2
19362 	  || context_die->die_tag == DW_TAG_class_type)
19363       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19364     }
19365   else if (access == access_protected_node)
19366     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19367   else if (dwarf_version > 2
19368 	   && context_die->die_tag != DW_TAG_class_type)
19369     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19370 }
19371 
19372 /* Generate a DIE for a class member.  */
19373 
19374 static void
gen_member_die(tree type,dw_die_ref context_die)19375 gen_member_die (tree type, dw_die_ref context_die)
19376 {
19377   tree member;
19378   tree binfo = TYPE_BINFO (type);
19379   dw_die_ref child;
19380 
19381   /* If this is not an incomplete type, output descriptions of each of its
19382      members. Note that as we output the DIEs necessary to represent the
19383      members of this record or union type, we will also be trying to output
19384      DIEs to represent the *types* of those members. However the `type'
19385      function (above) will specifically avoid generating type DIEs for member
19386      types *within* the list of member DIEs for this (containing) type except
19387      for those types (of members) which are explicitly marked as also being
19388      members of this (containing) type themselves.  The g++ front- end can
19389      force any given type to be treated as a member of some other (containing)
19390      type by setting the TYPE_CONTEXT of the given (member) type to point to
19391      the TREE node representing the appropriate (containing) type.  */
19392 
19393   /* First output info about the base classes.  */
19394   if (binfo)
19395     {
19396       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19397       int i;
19398       tree base;
19399 
19400       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19401 	gen_inheritance_die (base,
19402 			     (accesses ? (*accesses)[i] : access_public_node),
19403 			     context_die);
19404     }
19405 
19406   /* Now output info about the data members and type members.  */
19407   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19408     {
19409       /* If we thought we were generating minimal debug info for TYPE
19410 	 and then changed our minds, some of the member declarations
19411 	 may have already been defined.  Don't define them again, but
19412 	 do put them in the right order.  */
19413 
19414       child = lookup_decl_die (member);
19415       if (child)
19416 	splice_child_die (context_die, child);
19417       else
19418 	gen_decl_die (member, NULL, context_die);
19419     }
19420 
19421   /* Now output info about the function members (if any).  */
19422   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19423     {
19424       /* Don't include clones in the member list.  */
19425       if (DECL_ABSTRACT_ORIGIN (member))
19426 	continue;
19427 
19428       child = lookup_decl_die (member);
19429       if (child)
19430 	splice_child_die (context_die, child);
19431       else
19432 	gen_decl_die (member, NULL, context_die);
19433     }
19434 }
19435 
19436 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19437    is set, we pretend that the type was never defined, so we only get the
19438    member DIEs needed by later specification DIEs.  */
19439 
19440 static void
gen_struct_or_union_type_die(tree type,dw_die_ref context_die,enum debug_info_usage usage)19441 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19442 				enum debug_info_usage usage)
19443 {
19444   dw_die_ref type_die = lookup_type_die (type);
19445   dw_die_ref scope_die = 0;
19446   int nested = 0;
19447   int complete = (TYPE_SIZE (type)
19448 		  && (! TYPE_STUB_DECL (type)
19449 		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19450   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19451   complete = complete && should_emit_struct_debug (type, usage);
19452 
19453   if (type_die && ! complete)
19454     return;
19455 
19456   if (TYPE_CONTEXT (type) != NULL_TREE
19457       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19458 	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19459     nested = 1;
19460 
19461   scope_die = scope_die_for (type, context_die);
19462 
19463   /* Generate child dies for template paramaters.  */
19464   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19465     schedule_generic_params_dies_gen (type);
19466 
19467   if (! type_die || (nested && is_cu_die (scope_die)))
19468     /* First occurrence of type or toplevel definition of nested class.  */
19469     {
19470       dw_die_ref old_die = type_die;
19471 
19472       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19473 			  ? record_type_tag (type) : DW_TAG_union_type,
19474 			  scope_die, type);
19475       equate_type_number_to_die (type, type_die);
19476       if (old_die)
19477 	add_AT_specification (type_die, old_die);
19478       else
19479 	add_name_attribute (type_die, type_tag (type));
19480     }
19481   else
19482     remove_AT (type_die, DW_AT_declaration);
19483 
19484   /* If this type has been completed, then give it a byte_size attribute and
19485      then give a list of members.  */
19486   if (complete && !ns_decl)
19487     {
19488       /* Prevent infinite recursion in cases where the type of some member of
19489 	 this type is expressed in terms of this type itself.  */
19490       TREE_ASM_WRITTEN (type) = 1;
19491       add_byte_size_attribute (type_die, type);
19492       if (TYPE_STUB_DECL (type) != NULL_TREE)
19493 	{
19494 	  add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19495 	  add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19496 	}
19497 
19498       /* If the first reference to this type was as the return type of an
19499 	 inline function, then it may not have a parent.  Fix this now.  */
19500       if (type_die->die_parent == NULL)
19501 	add_child_die (scope_die, type_die);
19502 
19503       push_decl_scope (type);
19504       gen_member_die (type, type_die);
19505       pop_decl_scope ();
19506 
19507       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19508       if (TYPE_ARTIFICIAL (type))
19509 	add_AT_flag (type_die, DW_AT_artificial, 1);
19510 
19511       /* GNU extension: Record what type our vtable lives in.  */
19512       if (TYPE_VFIELD (type))
19513 	{
19514 	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19515 
19516 	  gen_type_die (vtype, context_die);
19517 	  add_AT_die_ref (type_die, DW_AT_containing_type,
19518 			  lookup_type_die (vtype));
19519 	}
19520     }
19521   else
19522     {
19523       add_AT_flag (type_die, DW_AT_declaration, 1);
19524 
19525       /* We don't need to do this for function-local types.  */
19526       if (TYPE_STUB_DECL (type)
19527 	  && ! decl_function_context (TYPE_STUB_DECL (type)))
19528 	vec_safe_push (incomplete_types, type);
19529     }
19530 
19531   if (get_AT (type_die, DW_AT_name))
19532     add_pubtype (type, type_die);
19533 }
19534 
19535 /* Generate a DIE for a subroutine _type_.  */
19536 
19537 static void
gen_subroutine_type_die(tree type,dw_die_ref context_die)19538 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19539 {
19540   tree return_type = TREE_TYPE (type);
19541   dw_die_ref subr_die
19542     = new_die (DW_TAG_subroutine_type,
19543 	       scope_die_for (type, context_die), type);
19544 
19545   equate_type_number_to_die (type, subr_die);
19546   add_prototyped_attribute (subr_die, type);
19547   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19548   gen_formal_types_die (type, subr_die);
19549 
19550   if (get_AT (subr_die, DW_AT_name))
19551     add_pubtype (type, subr_die);
19552 }
19553 
19554 /* Generate a DIE for a type definition.  */
19555 
19556 static void
gen_typedef_die(tree decl,dw_die_ref context_die)19557 gen_typedef_die (tree decl, dw_die_ref context_die)
19558 {
19559   dw_die_ref type_die;
19560   tree origin;
19561 
19562   if (TREE_ASM_WRITTEN (decl))
19563     return;
19564 
19565   TREE_ASM_WRITTEN (decl) = 1;
19566   type_die = new_die (DW_TAG_typedef, context_die, decl);
19567   origin = decl_ultimate_origin (decl);
19568   if (origin != NULL)
19569     add_abstract_origin_attribute (type_die, origin);
19570   else
19571     {
19572       tree type;
19573 
19574       add_name_and_src_coords_attributes (type_die, decl);
19575       if (DECL_ORIGINAL_TYPE (decl))
19576 	{
19577 	  type = DECL_ORIGINAL_TYPE (decl);
19578 
19579 	  gcc_assert (type != TREE_TYPE (decl));
19580 	  equate_type_number_to_die (TREE_TYPE (decl), type_die);
19581 	}
19582       else
19583 	{
19584 	  type = TREE_TYPE (decl);
19585 
19586 	  if (is_naming_typedef_decl (TYPE_NAME (type)))
19587 	    {
19588 	      /* Here, we are in the case of decl being a typedef naming
19589 	         an anonymous type, e.g:
19590 	             typedef struct {...} foo;
19591 	         In that case TREE_TYPE (decl) is not a typedef variant
19592 	         type and TYPE_NAME of the anonymous type is set to the
19593 	         TYPE_DECL of the typedef. This construct is emitted by
19594 	         the C++ FE.
19595 
19596 	         TYPE is the anonymous struct named by the typedef
19597 	         DECL. As we need the DW_AT_type attribute of the
19598 	         DW_TAG_typedef to point to the DIE of TYPE, let's
19599 	         generate that DIE right away. add_type_attribute
19600 	         called below will then pick (via lookup_type_die) that
19601 	         anonymous struct DIE.  */
19602 	      if (!TREE_ASM_WRITTEN (type))
19603 	        gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19604 
19605 	      /* This is a GNU Extension.  We are adding a
19606 		 DW_AT_linkage_name attribute to the DIE of the
19607 		 anonymous struct TYPE.  The value of that attribute
19608 		 is the name of the typedef decl naming the anonymous
19609 		 struct.  This greatly eases the work of consumers of
19610 		 this debug info.  */
19611 	      add_linkage_attr (lookup_type_die (type), decl);
19612 	    }
19613 	}
19614 
19615       add_type_attribute (type_die, type, TREE_READONLY (decl),
19616 			  TREE_THIS_VOLATILE (decl), context_die);
19617 
19618       if (is_naming_typedef_decl (decl))
19619 	/* We want that all subsequent calls to lookup_type_die with
19620 	   TYPE in argument yield the DW_TAG_typedef we have just
19621 	   created.  */
19622 	equate_type_number_to_die (type, type_die);
19623 
19624       add_accessibility_attribute (type_die, decl);
19625     }
19626 
19627   if (DECL_ABSTRACT (decl))
19628     equate_decl_number_to_die (decl, type_die);
19629 
19630   if (get_AT (type_die, DW_AT_name))
19631     add_pubtype (decl, type_die);
19632 }
19633 
19634 /* Generate a DIE for a struct, class, enum or union type.  */
19635 
19636 static void
gen_tagged_type_die(tree type,dw_die_ref context_die,enum debug_info_usage usage)19637 gen_tagged_type_die (tree type,
19638 		     dw_die_ref context_die,
19639 		     enum debug_info_usage usage)
19640 {
19641   int need_pop;
19642 
19643   if (type == NULL_TREE
19644       || !is_tagged_type (type))
19645     return;
19646 
19647   /* If this is a nested type whose containing class hasn't been written
19648      out yet, writing it out will cover this one, too.  This does not apply
19649      to instantiations of member class templates; they need to be added to
19650      the containing class as they are generated.  FIXME: This hurts the
19651      idea of combining type decls from multiple TUs, since we can't predict
19652      what set of template instantiations we'll get.  */
19653   if (TYPE_CONTEXT (type)
19654       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19655       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19656     {
19657       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19658 
19659       if (TREE_ASM_WRITTEN (type))
19660 	return;
19661 
19662       /* If that failed, attach ourselves to the stub.  */
19663       push_decl_scope (TYPE_CONTEXT (type));
19664       context_die = lookup_type_die (TYPE_CONTEXT (type));
19665       need_pop = 1;
19666     }
19667   else if (TYPE_CONTEXT (type) != NULL_TREE
19668 	   && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19669     {
19670       /* If this type is local to a function that hasn't been written
19671 	 out yet, use a NULL context for now; it will be fixed up in
19672 	 decls_for_scope.  */
19673       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19674       /* A declaration DIE doesn't count; nested types need to go in the
19675 	 specification.  */
19676       if (context_die && is_declaration_die (context_die))
19677 	context_die = NULL;
19678       need_pop = 0;
19679     }
19680   else
19681     {
19682       context_die = declare_in_namespace (type, context_die);
19683       need_pop = 0;
19684     }
19685 
19686   if (TREE_CODE (type) == ENUMERAL_TYPE)
19687     {
19688       /* This might have been written out by the call to
19689 	 declare_in_namespace.  */
19690       if (!TREE_ASM_WRITTEN (type))
19691 	gen_enumeration_type_die (type, context_die);
19692     }
19693   else
19694     gen_struct_or_union_type_die (type, context_die, usage);
19695 
19696   if (need_pop)
19697     pop_decl_scope ();
19698 
19699   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19700      it up if it is ever completed.  gen_*_type_die will set it for us
19701      when appropriate.  */
19702 }
19703 
19704 /* Generate a type description DIE.  */
19705 
19706 static void
gen_type_die_with_usage(tree type,dw_die_ref context_die,enum debug_info_usage usage)19707 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19708 			 enum debug_info_usage usage)
19709 {
19710   struct array_descr_info info;
19711 
19712   if (type == NULL_TREE || type == error_mark_node)
19713     return;
19714 
19715   if (TYPE_NAME (type) != NULL_TREE
19716       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19717       && is_redundant_typedef (TYPE_NAME (type))
19718       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19719     /* The DECL of this type is a typedef we don't want to emit debug
19720        info for but we want debug info for its underlying typedef.
19721        This can happen for e.g, the injected-class-name of a C++
19722        type.  */
19723     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19724 
19725   /* If TYPE is a typedef type variant, let's generate debug info
19726      for the parent typedef which TYPE is a type of.  */
19727   if (typedef_variant_p (type))
19728     {
19729       if (TREE_ASM_WRITTEN (type))
19730 	return;
19731 
19732       /* Prevent broken recursion; we can't hand off to the same type.  */
19733       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19734 
19735       /* Give typedefs the right scope.  */
19736       context_die = scope_die_for (type, context_die);
19737 
19738       TREE_ASM_WRITTEN (type) = 1;
19739 
19740       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19741       return;
19742     }
19743 
19744   /* If type is an anonymous tagged type named by a typedef, let's
19745      generate debug info for the typedef.  */
19746   if (is_naming_typedef_decl (TYPE_NAME (type)))
19747     {
19748       /* Use the DIE of the containing namespace as the parent DIE of
19749          the type description DIE we want to generate.  */
19750       if (DECL_CONTEXT (TYPE_NAME (type))
19751 	  && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19752 	context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19753 
19754       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19755       return;
19756     }
19757 
19758   /* If this is an array type with hidden descriptor, handle it first.  */
19759   if (!TREE_ASM_WRITTEN (type)
19760       && lang_hooks.types.get_array_descr_info
19761       && lang_hooks.types.get_array_descr_info (type, &info)
19762       && (dwarf_version >= 3 || !dwarf_strict))
19763     {
19764       gen_descr_array_type_die (type, &info, context_die);
19765       TREE_ASM_WRITTEN (type) = 1;
19766       return;
19767     }
19768 
19769   /* We are going to output a DIE to represent the unqualified version
19770      of this type (i.e. without any const or volatile qualifiers) so
19771      get the main variant (i.e. the unqualified version) of this type
19772      now.  (Vectors are special because the debugging info is in the
19773      cloned type itself).  */
19774   if (TREE_CODE (type) != VECTOR_TYPE)
19775     type = type_main_variant (type);
19776 
19777   if (TREE_ASM_WRITTEN (type))
19778     return;
19779 
19780   switch (TREE_CODE (type))
19781     {
19782     case ERROR_MARK:
19783       break;
19784 
19785     case POINTER_TYPE:
19786     case REFERENCE_TYPE:
19787       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19788 	 ensures that the gen_type_die recursion will terminate even if the
19789 	 type is recursive.  Recursive types are possible in Ada.  */
19790       /* ??? We could perhaps do this for all types before the switch
19791 	 statement.  */
19792       TREE_ASM_WRITTEN (type) = 1;
19793 
19794       /* For these types, all that is required is that we output a DIE (or a
19795 	 set of DIEs) to represent the "basis" type.  */
19796       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19797 				DINFO_USAGE_IND_USE);
19798       break;
19799 
19800     case OFFSET_TYPE:
19801       /* This code is used for C++ pointer-to-data-member types.
19802 	 Output a description of the relevant class type.  */
19803       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19804 					DINFO_USAGE_IND_USE);
19805 
19806       /* Output a description of the type of the object pointed to.  */
19807       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19808 					DINFO_USAGE_IND_USE);
19809 
19810       /* Now output a DIE to represent this pointer-to-data-member type
19811 	 itself.  */
19812       gen_ptr_to_mbr_type_die (type, context_die);
19813       break;
19814 
19815     case FUNCTION_TYPE:
19816       /* Force out return type (in case it wasn't forced out already).  */
19817       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19818 					DINFO_USAGE_DIR_USE);
19819       gen_subroutine_type_die (type, context_die);
19820       break;
19821 
19822     case METHOD_TYPE:
19823       /* Force out return type (in case it wasn't forced out already).  */
19824       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19825 					DINFO_USAGE_DIR_USE);
19826       gen_subroutine_type_die (type, context_die);
19827       break;
19828 
19829     case ARRAY_TYPE:
19830       gen_array_type_die (type, context_die);
19831       break;
19832 
19833     case VECTOR_TYPE:
19834       gen_array_type_die (type, context_die);
19835       break;
19836 
19837     case ENUMERAL_TYPE:
19838     case RECORD_TYPE:
19839     case UNION_TYPE:
19840     case QUAL_UNION_TYPE:
19841       gen_tagged_type_die (type, context_die, usage);
19842       return;
19843 
19844     case VOID_TYPE:
19845     case INTEGER_TYPE:
19846     case REAL_TYPE:
19847     case FIXED_POINT_TYPE:
19848     case COMPLEX_TYPE:
19849     case BOOLEAN_TYPE:
19850       /* No DIEs needed for fundamental types.  */
19851       break;
19852 
19853     case NULLPTR_TYPE:
19854     case LANG_TYPE:
19855       /* Just use DW_TAG_unspecified_type.  */
19856       {
19857         dw_die_ref type_die = lookup_type_die (type);
19858         if (type_die == NULL)
19859           {
19860 	    tree name = TYPE_NAME (type);
19861 	    if (TREE_CODE (name) == TYPE_DECL)
19862 	      name = DECL_NAME (name);
19863             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19864             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19865             equate_type_number_to_die (type, type_die);
19866           }
19867       }
19868       break;
19869 
19870     default:
19871       if (is_cxx_auto (type))
19872 	{
19873 	  tree name = TYPE_NAME (type);
19874 	  if (TREE_CODE (name) == TYPE_DECL)
19875 	    name = DECL_NAME (name);
19876 	  dw_die_ref *die = (name == get_identifier ("auto")
19877 			     ? &auto_die : &decltype_auto_die);
19878 	  if (!*die)
19879 	    {
19880 	      *die = new_die (DW_TAG_unspecified_type,
19881 			      comp_unit_die (), NULL_TREE);
19882 	      add_name_attribute (*die, IDENTIFIER_POINTER (name));
19883 	    }
19884 	  equate_type_number_to_die (type, *die);
19885 	  break;
19886 	}
19887       gcc_unreachable ();
19888     }
19889 
19890   TREE_ASM_WRITTEN (type) = 1;
19891 }
19892 
19893 static void
gen_type_die(tree type,dw_die_ref context_die)19894 gen_type_die (tree type, dw_die_ref context_die)
19895 {
19896   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19897 }
19898 
19899 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19900    things which are local to the given block.  */
19901 
19902 static void
gen_block_die(tree stmt,dw_die_ref context_die,int depth)19903 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19904 {
19905   int must_output_die = 0;
19906   bool inlined_func;
19907 
19908   /* Ignore blocks that are NULL.  */
19909   if (stmt == NULL_TREE)
19910     return;
19911 
19912   inlined_func = inlined_function_outer_scope_p (stmt);
19913 
19914   /* If the block is one fragment of a non-contiguous block, do not
19915      process the variables, since they will have been done by the
19916      origin block.  Do process subblocks.  */
19917   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19918     {
19919       tree sub;
19920 
19921       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19922 	gen_block_die (sub, context_die, depth + 1);
19923 
19924       return;
19925     }
19926 
19927   /* Determine if we need to output any Dwarf DIEs at all to represent this
19928      block.  */
19929   if (inlined_func)
19930     /* The outer scopes for inlinings *must* always be represented.  We
19931        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19932     must_output_die = 1;
19933   else
19934     {
19935       /* Determine if this block directly contains any "significant"
19936 	 local declarations which we will need to output DIEs for.  */
19937       if (debug_info_level > DINFO_LEVEL_TERSE)
19938 	/* We are not in terse mode so *any* local declaration counts
19939 	   as being a "significant" one.  */
19940 	must_output_die = ((BLOCK_VARS (stmt) != NULL
19941 			    || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19942 			   && (TREE_USED (stmt)
19943 			       || TREE_ASM_WRITTEN (stmt)
19944 			       || BLOCK_ABSTRACT (stmt)));
19945       else if ((TREE_USED (stmt)
19946 		|| TREE_ASM_WRITTEN (stmt)
19947 		|| BLOCK_ABSTRACT (stmt))
19948       	       && !dwarf2out_ignore_block (stmt))
19949 	must_output_die = 1;
19950     }
19951 
19952   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19953      DIE for any block which contains no significant local declarations at
19954      all.  Rather, in such cases we just call `decls_for_scope' so that any
19955      needed Dwarf info for any sub-blocks will get properly generated. Note
19956      that in terse mode, our definition of what constitutes a "significant"
19957      local declaration gets restricted to include only inlined function
19958      instances and local (nested) function definitions.  */
19959   if (must_output_die)
19960     {
19961       if (inlined_func)
19962 	{
19963 	  /* If STMT block is abstract, that means we have been called
19964 	     indirectly from dwarf2out_abstract_function.
19965 	     That function rightfully marks the descendent blocks (of
19966 	     the abstract function it is dealing with) as being abstract,
19967 	     precisely to prevent us from emitting any
19968 	     DW_TAG_inlined_subroutine DIE as a descendent
19969 	     of an abstract function instance. So in that case, we should
19970 	     not call gen_inlined_subroutine_die.
19971 
19972 	     Later though, when cgraph asks dwarf2out to emit info
19973 	     for the concrete instance of the function decl into which
19974 	     the concrete instance of STMT got inlined, the later will lead
19975 	     to the generation of a DW_TAG_inlined_subroutine DIE.  */
19976 	  if (! BLOCK_ABSTRACT (stmt))
19977 	    gen_inlined_subroutine_die (stmt, context_die, depth);
19978 	}
19979       else
19980 	gen_lexical_block_die (stmt, context_die, depth);
19981     }
19982   else
19983     decls_for_scope (stmt, context_die, depth);
19984 }
19985 
19986 /* Process variable DECL (or variable with origin ORIGIN) within
19987    block STMT and add it to CONTEXT_DIE.  */
19988 static void
process_scope_var(tree stmt,tree decl,tree origin,dw_die_ref context_die)19989 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19990 {
19991   dw_die_ref die;
19992   tree decl_or_origin = decl ? decl : origin;
19993 
19994   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19995     die = lookup_decl_die (decl_or_origin);
19996   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19997            && TYPE_DECL_IS_STUB (decl_or_origin))
19998     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19999   else
20000     die = NULL;
20001 
20002   if (die != NULL && die->die_parent == NULL)
20003     add_child_die (context_die, die);
20004   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20005     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20006 					 stmt, context_die);
20007   else
20008     gen_decl_die (decl, origin, context_die);
20009 }
20010 
20011 /* Generate all of the decls declared within a given scope and (recursively)
20012    all of its sub-blocks.  */
20013 
20014 static void
decls_for_scope(tree stmt,dw_die_ref context_die,int depth)20015 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20016 {
20017   tree decl;
20018   unsigned int i;
20019   tree subblocks;
20020 
20021   /* Ignore NULL blocks.  */
20022   if (stmt == NULL_TREE)
20023     return;
20024 
20025   /* Output the DIEs to represent all of the data objects and typedefs
20026      declared directly within this block but not within any nested
20027      sub-blocks.  Also, nested function and tag DIEs have been
20028      generated with a parent of NULL; fix that up now.  We don't
20029      have to do this if we're at -g1.  */
20030   if (debug_info_level > DINFO_LEVEL_TERSE)
20031     {
20032       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20033 	process_scope_var (stmt, decl, NULL_TREE, context_die);
20034       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20035 	process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20036 			   context_die);
20037     }
20038 
20039   /* Even if we're at -g1, we need to process the subblocks in order to get
20040      inlined call information.  */
20041 
20042   /* Output the DIEs to represent all sub-blocks (and the items declared
20043      therein) of this block.  */
20044   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20045        subblocks != NULL;
20046        subblocks = BLOCK_CHAIN (subblocks))
20047     gen_block_die (subblocks, context_die, depth + 1);
20048 }
20049 
20050 /* Is this a typedef we can avoid emitting?  */
20051 
20052 static inline int
is_redundant_typedef(const_tree decl)20053 is_redundant_typedef (const_tree decl)
20054 {
20055   if (TYPE_DECL_IS_STUB (decl))
20056     return 1;
20057 
20058   if (DECL_ARTIFICIAL (decl)
20059       && DECL_CONTEXT (decl)
20060       && is_tagged_type (DECL_CONTEXT (decl))
20061       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20062       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20063     /* Also ignore the artificial member typedef for the class name.  */
20064     return 1;
20065 
20066   return 0;
20067 }
20068 
20069 /* Return TRUE if TYPE is a typedef that names a type for linkage
20070    purposes. This kind of typedefs is produced by the C++ FE for
20071    constructs like:
20072 
20073    typedef struct {...} foo;
20074 
20075    In that case, there is no typedef variant type produced for foo.
20076    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20077    struct type.  */
20078 
20079 static bool
is_naming_typedef_decl(const_tree decl)20080 is_naming_typedef_decl (const_tree decl)
20081 {
20082   if (decl == NULL_TREE
20083       || TREE_CODE (decl) != TYPE_DECL
20084       || !is_tagged_type (TREE_TYPE (decl))
20085       || DECL_IS_BUILTIN (decl)
20086       || is_redundant_typedef (decl)
20087       /* It looks like Ada produces TYPE_DECLs that are very similar
20088          to C++ naming typedefs but that have different
20089          semantics. Let's be specific to c++ for now.  */
20090       || !is_cxx ())
20091     return FALSE;
20092 
20093   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20094 	  && TYPE_NAME (TREE_TYPE (decl)) == decl
20095 	  && (TYPE_STUB_DECL (TREE_TYPE (decl))
20096 	      != TYPE_NAME (TREE_TYPE (decl))));
20097 }
20098 
20099 /* Returns the DIE for a context.  */
20100 
20101 static inline dw_die_ref
get_context_die(tree context)20102 get_context_die (tree context)
20103 {
20104   if (context)
20105     {
20106       /* Find die that represents this context.  */
20107       if (TYPE_P (context))
20108 	{
20109 	  context = TYPE_MAIN_VARIANT (context);
20110 	  return strip_naming_typedef (context, force_type_die (context));
20111 	}
20112       else
20113 	return force_decl_die (context);
20114     }
20115   return comp_unit_die ();
20116 }
20117 
20118 /* Returns the DIE for decl.  A DIE will always be returned.  */
20119 
20120 static dw_die_ref
force_decl_die(tree decl)20121 force_decl_die (tree decl)
20122 {
20123   dw_die_ref decl_die;
20124   unsigned saved_external_flag;
20125   tree save_fn = NULL_TREE;
20126   decl_die = lookup_decl_die (decl);
20127   if (!decl_die)
20128     {
20129       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20130 
20131       decl_die = lookup_decl_die (decl);
20132       if (decl_die)
20133 	return decl_die;
20134 
20135       switch (TREE_CODE (decl))
20136 	{
20137 	case FUNCTION_DECL:
20138 	  /* Clear current_function_decl, so that gen_subprogram_die thinks
20139 	     that this is a declaration. At this point, we just want to force
20140 	     declaration die.  */
20141 	  save_fn = current_function_decl;
20142 	  current_function_decl = NULL_TREE;
20143 	  gen_subprogram_die (decl, context_die);
20144 	  current_function_decl = save_fn;
20145 	  break;
20146 
20147 	case VAR_DECL:
20148 	  /* Set external flag to force declaration die. Restore it after
20149 	   gen_decl_die() call.  */
20150 	  saved_external_flag = DECL_EXTERNAL (decl);
20151 	  DECL_EXTERNAL (decl) = 1;
20152 	  gen_decl_die (decl, NULL, context_die);
20153 	  DECL_EXTERNAL (decl) = saved_external_flag;
20154 	  break;
20155 
20156 	case NAMESPACE_DECL:
20157 	  if (dwarf_version >= 3 || !dwarf_strict)
20158 	    dwarf2out_decl (decl);
20159 	  else
20160 	    /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20161 	    decl_die = comp_unit_die ();
20162 	  break;
20163 
20164 	case TRANSLATION_UNIT_DECL:
20165 	  decl_die = comp_unit_die ();
20166 	  break;
20167 
20168 	default:
20169 	  gcc_unreachable ();
20170 	}
20171 
20172       /* We should be able to find the DIE now.  */
20173       if (!decl_die)
20174 	decl_die = lookup_decl_die (decl);
20175       gcc_assert (decl_die);
20176     }
20177 
20178   return decl_die;
20179 }
20180 
20181 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20182    always returned.  */
20183 
20184 static dw_die_ref
force_type_die(tree type)20185 force_type_die (tree type)
20186 {
20187   dw_die_ref type_die;
20188 
20189   type_die = lookup_type_die (type);
20190   if (!type_die)
20191     {
20192       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20193 
20194       type_die = modified_type_die (type, TYPE_READONLY (type),
20195 				    TYPE_VOLATILE (type), context_die);
20196       gcc_assert (type_die);
20197     }
20198   return type_die;
20199 }
20200 
20201 /* Force out any required namespaces to be able to output DECL,
20202    and return the new context_die for it, if it's changed.  */
20203 
20204 static dw_die_ref
setup_namespace_context(tree thing,dw_die_ref context_die)20205 setup_namespace_context (tree thing, dw_die_ref context_die)
20206 {
20207   tree context = (DECL_P (thing)
20208 		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20209   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20210     /* Force out the namespace.  */
20211     context_die = force_decl_die (context);
20212 
20213   return context_die;
20214 }
20215 
20216 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20217    type) within its namespace, if appropriate.
20218 
20219    For compatibility with older debuggers, namespace DIEs only contain
20220    declarations; all definitions are emitted at CU scope.  */
20221 
20222 static dw_die_ref
declare_in_namespace(tree thing,dw_die_ref context_die)20223 declare_in_namespace (tree thing, dw_die_ref context_die)
20224 {
20225   dw_die_ref ns_context;
20226 
20227   if (debug_info_level <= DINFO_LEVEL_TERSE)
20228     return context_die;
20229 
20230   /* If this decl is from an inlined function, then don't try to emit it in its
20231      namespace, as we will get confused.  It would have already been emitted
20232      when the abstract instance of the inline function was emitted anyways.  */
20233   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20234     return context_die;
20235 
20236   ns_context = setup_namespace_context (thing, context_die);
20237 
20238   if (ns_context != context_die)
20239     {
20240       if (is_fortran ())
20241 	return ns_context;
20242       if (DECL_P (thing))
20243 	gen_decl_die (thing, NULL, ns_context);
20244       else
20245 	gen_type_die (thing, ns_context);
20246     }
20247   return context_die;
20248 }
20249 
20250 /* Generate a DIE for a namespace or namespace alias.  */
20251 
20252 static void
gen_namespace_die(tree decl,dw_die_ref context_die)20253 gen_namespace_die (tree decl, dw_die_ref context_die)
20254 {
20255   dw_die_ref namespace_die;
20256 
20257   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20258      they are an alias of.  */
20259   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20260     {
20261       /* Output a real namespace or module.  */
20262       context_die = setup_namespace_context (decl, comp_unit_die ());
20263       namespace_die = new_die (is_fortran ()
20264 			       ? DW_TAG_module : DW_TAG_namespace,
20265 			       context_die, decl);
20266       /* For Fortran modules defined in different CU don't add src coords.  */
20267       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20268 	{
20269 	  const char *name = dwarf2_name (decl, 0);
20270 	  if (name)
20271 	    add_name_attribute (namespace_die, name);
20272 	}
20273       else
20274 	add_name_and_src_coords_attributes (namespace_die, decl);
20275       if (DECL_EXTERNAL (decl))
20276 	add_AT_flag (namespace_die, DW_AT_declaration, 1);
20277       equate_decl_number_to_die (decl, namespace_die);
20278     }
20279   else
20280     {
20281       /* Output a namespace alias.  */
20282 
20283       /* Force out the namespace we are an alias of, if necessary.  */
20284       dw_die_ref origin_die
20285 	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20286 
20287       if (DECL_FILE_SCOPE_P (decl)
20288 	  || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20289 	context_die = setup_namespace_context (decl, comp_unit_die ());
20290       /* Now create the namespace alias DIE.  */
20291       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20292       add_name_and_src_coords_attributes (namespace_die, decl);
20293       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20294       equate_decl_number_to_die (decl, namespace_die);
20295     }
20296   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
20297   if (want_pubnames ())
20298     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20299 }
20300 
20301 /* Generate Dwarf debug information for a decl described by DECL.
20302    The return value is currently only meaningful for PARM_DECLs,
20303    for all other decls it returns NULL.  */
20304 
20305 static dw_die_ref
gen_decl_die(tree decl,tree origin,dw_die_ref context_die)20306 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20307 {
20308   tree decl_or_origin = decl ? decl : origin;
20309   tree class_origin = NULL, ultimate_origin;
20310 
20311   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20312     return NULL;
20313 
20314   switch (TREE_CODE (decl_or_origin))
20315     {
20316     case ERROR_MARK:
20317       break;
20318 
20319     case CONST_DECL:
20320       if (!is_fortran () && !is_ada ())
20321 	{
20322 	  /* The individual enumerators of an enum type get output when we output
20323 	     the Dwarf representation of the relevant enum type itself.  */
20324 	  break;
20325 	}
20326 
20327       /* Emit its type.  */
20328       gen_type_die (TREE_TYPE (decl), context_die);
20329 
20330       /* And its containing namespace.  */
20331       context_die = declare_in_namespace (decl, context_die);
20332 
20333       gen_const_die (decl, context_die);
20334       break;
20335 
20336     case FUNCTION_DECL:
20337       /* Don't output any DIEs to represent mere function declarations,
20338 	 unless they are class members or explicit block externs.  */
20339       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20340           && DECL_FILE_SCOPE_P (decl_or_origin)
20341 	  && (current_function_decl == NULL_TREE
20342 	      || DECL_ARTIFICIAL (decl_or_origin)))
20343 	break;
20344 
20345 #if 0
20346       /* FIXME */
20347       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20348 	 on local redeclarations of global functions.  That seems broken.  */
20349       if (current_function_decl != decl)
20350 	/* This is only a declaration.  */;
20351 #endif
20352 
20353       /* If we're emitting a clone, emit info for the abstract instance.  */
20354       if (origin || DECL_ORIGIN (decl) != decl)
20355 	dwarf2out_abstract_function (origin
20356 				     ? DECL_ORIGIN (origin)
20357 				     : DECL_ABSTRACT_ORIGIN (decl));
20358 
20359       /* If we're emitting an out-of-line copy of an inline function,
20360 	 emit info for the abstract instance and set up to refer to it.  */
20361       else if (cgraph_function_possibly_inlined_p (decl)
20362 	       && ! DECL_ABSTRACT (decl)
20363 	       && ! class_or_namespace_scope_p (context_die)
20364 	       /* dwarf2out_abstract_function won't emit a die if this is just
20365 		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20366 		  that case, because that works only if we have a die.  */
20367 	       && DECL_INITIAL (decl) != NULL_TREE)
20368 	{
20369 	  dwarf2out_abstract_function (decl);
20370 	  set_decl_origin_self (decl);
20371 	}
20372 
20373       /* Otherwise we're emitting the primary DIE for this decl.  */
20374       else if (debug_info_level > DINFO_LEVEL_TERSE)
20375 	{
20376 	  /* Before we describe the FUNCTION_DECL itself, make sure that we
20377 	     have its containing type.  */
20378 	  if (!origin)
20379 	    origin = decl_class_context (decl);
20380 	  if (origin != NULL_TREE)
20381 	    gen_type_die (origin, context_die);
20382 
20383 	  /* And its return type.  */
20384 	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20385 
20386 	  /* And its virtual context.  */
20387 	  if (DECL_VINDEX (decl) != NULL_TREE)
20388 	    gen_type_die (DECL_CONTEXT (decl), context_die);
20389 
20390 	  /* Make sure we have a member DIE for decl.  */
20391 	  if (origin != NULL_TREE)
20392 	    gen_type_die_for_member (origin, decl, context_die);
20393 
20394 	  /* And its containing namespace.  */
20395 	  context_die = declare_in_namespace (decl, context_die);
20396 	}
20397 
20398       /* Now output a DIE to represent the function itself.  */
20399       if (decl)
20400         gen_subprogram_die (decl, context_die);
20401       break;
20402 
20403     case TYPE_DECL:
20404       /* If we are in terse mode, don't generate any DIEs to represent any
20405 	 actual typedefs.  */
20406       if (debug_info_level <= DINFO_LEVEL_TERSE)
20407 	break;
20408 
20409       /* In the special case of a TYPE_DECL node representing the declaration
20410 	 of some type tag, if the given TYPE_DECL is marked as having been
20411 	 instantiated from some other (original) TYPE_DECL node (e.g. one which
20412 	 was generated within the original definition of an inline function) we
20413 	 used to generate a special (abbreviated) DW_TAG_structure_type,
20414 	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20415 	 should be actually referencing those DIEs, as variable DIEs with that
20416 	 type would be emitted already in the abstract origin, so it was always
20417 	 removed during unused type prunning.  Don't add anything in this
20418 	 case.  */
20419       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20420 	break;
20421 
20422       if (is_redundant_typedef (decl))
20423 	gen_type_die (TREE_TYPE (decl), context_die);
20424       else
20425 	/* Output a DIE to represent the typedef itself.  */
20426 	gen_typedef_die (decl, context_die);
20427       break;
20428 
20429     case LABEL_DECL:
20430       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20431 	gen_label_die (decl, context_die);
20432       break;
20433 
20434     case VAR_DECL:
20435     case RESULT_DECL:
20436       /* If we are in terse mode, don't generate any DIEs to represent any
20437 	 variable declarations or definitions.  */
20438       if (debug_info_level <= DINFO_LEVEL_TERSE)
20439 	break;
20440 
20441       /* Output any DIEs that are needed to specify the type of this data
20442 	 object.  */
20443       if (decl_by_reference_p (decl_or_origin))
20444 	gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20445       else
20446 	gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20447 
20448       /* And its containing type.  */
20449       class_origin = decl_class_context (decl_or_origin);
20450       if (class_origin != NULL_TREE)
20451 	gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20452 
20453       /* And its containing namespace.  */
20454       context_die = declare_in_namespace (decl_or_origin, context_die);
20455 
20456       /* Now output the DIE to represent the data object itself.  This gets
20457 	 complicated because of the possibility that the VAR_DECL really
20458 	 represents an inlined instance of a formal parameter for an inline
20459 	 function.  */
20460       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20461       if (ultimate_origin != NULL_TREE
20462 	  && TREE_CODE (ultimate_origin) == PARM_DECL)
20463 	gen_formal_parameter_die (decl, origin,
20464 				  true /* Emit name attribute.  */,
20465 				  context_die);
20466       else
20467 	gen_variable_die (decl, origin, context_die);
20468       break;
20469 
20470     case FIELD_DECL:
20471       /* Ignore the nameless fields that are used to skip bits but handle C++
20472 	 anonymous unions and structs.  */
20473       if (DECL_NAME (decl) != NULL_TREE
20474 	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20475 	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20476 	{
20477 	  gen_type_die (member_declared_type (decl), context_die);
20478 	  gen_field_die (decl, context_die);
20479 	}
20480       break;
20481 
20482     case PARM_DECL:
20483       if (DECL_BY_REFERENCE (decl_or_origin))
20484 	gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20485       else
20486 	gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20487       return gen_formal_parameter_die (decl, origin,
20488 				       true /* Emit name attribute.  */,
20489 				       context_die);
20490 
20491     case NAMESPACE_DECL:
20492     case IMPORTED_DECL:
20493       if (dwarf_version >= 3 || !dwarf_strict)
20494 	gen_namespace_die (decl, context_die);
20495       break;
20496 
20497     case NAMELIST_DECL:
20498       gen_namelist_decl (DECL_NAME (decl), context_die,
20499 			 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20500       break;
20501 
20502     default:
20503       /* Probably some frontend-internal decl.  Assume we don't care.  */
20504       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20505       break;
20506     }
20507 
20508   return NULL;
20509 }
20510 
20511 /* Output debug information for global decl DECL.  Called from toplev.c after
20512    compilation proper has finished.  */
20513 
20514 static void
dwarf2out_global_decl(tree decl)20515 dwarf2out_global_decl (tree decl)
20516 {
20517   /* Output DWARF2 information for file-scope tentative data object
20518      declarations, file-scope (extern) function declarations (which
20519      had no corresponding body) and file-scope tagged type declarations
20520      and definitions which have not yet been forced out.  */
20521   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20522     dwarf2out_decl (decl);
20523 }
20524 
20525 /* Output debug information for type decl DECL.  Called from toplev.c
20526    and from language front ends (to record built-in types).  */
20527 static void
dwarf2out_type_decl(tree decl,int local)20528 dwarf2out_type_decl (tree decl, int local)
20529 {
20530   if (!local)
20531     dwarf2out_decl (decl);
20532 }
20533 
20534 /* Output debug information for imported module or decl DECL.
20535    NAME is non-NULL name in the lexical block if the decl has been renamed.
20536    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20537    that DECL belongs to.
20538    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20539 static void
dwarf2out_imported_module_or_decl_1(tree decl,tree name,tree lexical_block,dw_die_ref lexical_block_die)20540 dwarf2out_imported_module_or_decl_1 (tree decl,
20541 				     tree name,
20542 				     tree lexical_block,
20543 				     dw_die_ref lexical_block_die)
20544 {
20545   expanded_location xloc;
20546   dw_die_ref imported_die = NULL;
20547   dw_die_ref at_import_die;
20548 
20549   if (TREE_CODE (decl) == IMPORTED_DECL)
20550     {
20551       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20552       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20553       gcc_assert (decl);
20554     }
20555   else
20556     xloc = expand_location (input_location);
20557 
20558   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20559     {
20560       at_import_die = force_type_die (TREE_TYPE (decl));
20561       /* For namespace N { typedef void T; } using N::T; base_type_die
20562 	 returns NULL, but DW_TAG_imported_declaration requires
20563 	 the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20564       if (!at_import_die)
20565 	{
20566 	  gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20567 	  gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20568 	  at_import_die = lookup_type_die (TREE_TYPE (decl));
20569 	  gcc_assert (at_import_die);
20570 	}
20571     }
20572   else
20573     {
20574       at_import_die = lookup_decl_die (decl);
20575       if (!at_import_die)
20576 	{
20577 	  /* If we're trying to avoid duplicate debug info, we may not have
20578 	     emitted the member decl for this field.  Emit it now.  */
20579 	  if (TREE_CODE (decl) == FIELD_DECL)
20580 	    {
20581 	      tree type = DECL_CONTEXT (decl);
20582 
20583 	      if (TYPE_CONTEXT (type)
20584 		  && TYPE_P (TYPE_CONTEXT (type))
20585 		  && !should_emit_struct_debug (TYPE_CONTEXT (type),
20586 						DINFO_USAGE_DIR_USE))
20587 		return;
20588 	      gen_type_die_for_member (type, decl,
20589 				       get_context_die (TYPE_CONTEXT (type)));
20590 	    }
20591 	  if (TREE_CODE (decl) == NAMELIST_DECL)
20592 	    at_import_die = gen_namelist_decl (DECL_NAME (decl),
20593 					 get_context_die (DECL_CONTEXT (decl)),
20594 					 NULL_TREE);
20595 	  else
20596 	    at_import_die = force_decl_die (decl);
20597 	}
20598     }
20599 
20600   if (TREE_CODE (decl) == NAMESPACE_DECL)
20601     {
20602       if (dwarf_version >= 3 || !dwarf_strict)
20603 	imported_die = new_die (DW_TAG_imported_module,
20604 				lexical_block_die,
20605 				lexical_block);
20606       else
20607 	return;
20608     }
20609   else
20610     imported_die = new_die (DW_TAG_imported_declaration,
20611 			    lexical_block_die,
20612 			    lexical_block);
20613 
20614   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20615   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20616   if (name)
20617     add_AT_string (imported_die, DW_AT_name,
20618 		   IDENTIFIER_POINTER (name));
20619   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20620 }
20621 
20622 /* Output debug information for imported module or decl DECL.
20623    NAME is non-NULL name in context if the decl has been renamed.
20624    CHILD is true if decl is one of the renamed decls as part of
20625    importing whole module.  */
20626 
20627 static void
dwarf2out_imported_module_or_decl(tree decl,tree name,tree context,bool child)20628 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20629 				   bool child)
20630 {
20631   /* dw_die_ref at_import_die;  */
20632   dw_die_ref scope_die;
20633 
20634   if (debug_info_level <= DINFO_LEVEL_TERSE)
20635     return;
20636 
20637   gcc_assert (decl);
20638 
20639   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20640      We need decl DIE for reference and scope die. First, get DIE for the decl
20641      itself.  */
20642 
20643   /* Get the scope die for decl context. Use comp_unit_die for global module
20644      or decl. If die is not found for non globals, force new die.  */
20645   if (context
20646       && TYPE_P (context)
20647       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20648     return;
20649 
20650   if (!(dwarf_version >= 3 || !dwarf_strict))
20651     return;
20652 
20653   scope_die = get_context_die (context);
20654 
20655   if (child)
20656     {
20657       gcc_assert (scope_die->die_child);
20658       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20659       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20660       scope_die = scope_die->die_child;
20661     }
20662 
20663   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20664   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20665 
20666 }
20667 
20668 /* Output debug information for namelists.   */
20669 
20670 static dw_die_ref
gen_namelist_decl(tree name,dw_die_ref scope_die,tree item_decls)20671 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20672 {
20673   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20674   tree value;
20675   unsigned i;
20676 
20677   if (debug_info_level <= DINFO_LEVEL_TERSE)
20678     return NULL;
20679 
20680   gcc_assert (scope_die != NULL);
20681   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20682   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20683 
20684   /* If there are no item_decls, we have a nondefining namelist, e.g.
20685      with USE association; hence, set DW_AT_declaration.  */
20686   if (item_decls == NULL_TREE)
20687     {
20688       add_AT_flag (nml_die, DW_AT_declaration, 1);
20689       return nml_die;
20690     }
20691 
20692   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20693     {
20694       nml_item_ref_die = lookup_decl_die (value);
20695       if (!nml_item_ref_die)
20696 	nml_item_ref_die = force_decl_die (value);
20697 
20698       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20699       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20700     }
20701   return nml_die;
20702 }
20703 
20704 
20705 /* Write the debugging output for DECL.  */
20706 
20707 void
dwarf2out_decl(tree decl)20708 dwarf2out_decl (tree decl)
20709 {
20710   dw_die_ref context_die = comp_unit_die ();
20711 
20712   switch (TREE_CODE (decl))
20713     {
20714     case ERROR_MARK:
20715       return;
20716 
20717     case FUNCTION_DECL:
20718       /* What we would really like to do here is to filter out all mere
20719 	 file-scope declarations of file-scope functions which are never
20720 	 referenced later within this translation unit (and keep all of ones
20721 	 that *are* referenced later on) but we aren't clairvoyant, so we have
20722 	 no idea which functions will be referenced in the future (i.e. later
20723 	 on within the current translation unit). So here we just ignore all
20724 	 file-scope function declarations which are not also definitions.  If
20725 	 and when the debugger needs to know something about these functions,
20726 	 it will have to hunt around and find the DWARF information associated
20727 	 with the definition of the function.
20728 
20729 	 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20730 	 nodes represent definitions and which ones represent mere
20731 	 declarations.  We have to check DECL_INITIAL instead. That's because
20732 	 the C front-end supports some weird semantics for "extern inline"
20733 	 function definitions.  These can get inlined within the current
20734 	 translation unit (and thus, we need to generate Dwarf info for their
20735 	 abstract instances so that the Dwarf info for the concrete inlined
20736 	 instances can have something to refer to) but the compiler never
20737 	 generates any out-of-lines instances of such things (despite the fact
20738 	 that they *are* definitions).
20739 
20740 	 The important point is that the C front-end marks these "extern
20741 	 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20742 	 them anyway. Note that the C++ front-end also plays some similar games
20743 	 for inline function definitions appearing within include files which
20744 	 also contain `#pragma interface' pragmas.
20745 
20746 	 If we are called from dwarf2out_abstract_function output a DIE
20747 	 anyway.  We can end up here this way with early inlining and LTO
20748 	 where the inlined function is output in a different LTRANS unit
20749 	 or not at all.  */
20750       if (DECL_INITIAL (decl) == NULL_TREE
20751 	  && ! DECL_ABSTRACT (decl))
20752 	return;
20753 
20754       /* If we're a nested function, initially use a parent of NULL; if we're
20755 	 a plain function, this will be fixed up in decls_for_scope.  If
20756 	 we're a method, it will be ignored, since we already have a DIE.  */
20757       if (decl_function_context (decl)
20758 	  /* But if we're in terse mode, we don't care about scope.  */
20759 	  && debug_info_level > DINFO_LEVEL_TERSE)
20760 	context_die = NULL;
20761       break;
20762 
20763     case VAR_DECL:
20764       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20765 	 declaration and if the declaration was never even referenced from
20766 	 within this entire compilation unit.  We suppress these DIEs in
20767 	 order to save space in the .debug section (by eliminating entries
20768 	 which are probably useless).  Note that we must not suppress
20769 	 block-local extern declarations (whether used or not) because that
20770 	 would screw-up the debugger's name lookup mechanism and cause it to
20771 	 miss things which really ought to be in scope at a given point.  */
20772       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20773 	return;
20774 
20775       /* For local statics lookup proper context die.  */
20776       if (TREE_STATIC (decl)
20777 	  && DECL_CONTEXT (decl)
20778 	  && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20779 	context_die = lookup_decl_die (DECL_CONTEXT (decl));
20780 
20781       /* If we are in terse mode, don't generate any DIEs to represent any
20782 	 variable declarations or definitions.  */
20783       if (debug_info_level <= DINFO_LEVEL_TERSE)
20784 	return;
20785       break;
20786 
20787     case CONST_DECL:
20788       if (debug_info_level <= DINFO_LEVEL_TERSE)
20789 	return;
20790       if (!is_fortran () && !is_ada ())
20791 	return;
20792       if (TREE_STATIC (decl) && decl_function_context (decl))
20793 	context_die = lookup_decl_die (DECL_CONTEXT (decl));
20794       break;
20795 
20796     case NAMESPACE_DECL:
20797     case IMPORTED_DECL:
20798       if (debug_info_level <= DINFO_LEVEL_TERSE)
20799 	return;
20800       if (lookup_decl_die (decl) != NULL)
20801 	return;
20802       break;
20803 
20804     case TYPE_DECL:
20805       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20806       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20807 	return;
20808 
20809       /* Don't bother trying to generate any DIEs to represent any of the
20810 	 normal built-in types for the language we are compiling.  */
20811       if (DECL_IS_BUILTIN (decl))
20812 	return;
20813 
20814       /* If we are in terse mode, don't generate any DIEs for types.  */
20815       if (debug_info_level <= DINFO_LEVEL_TERSE)
20816 	return;
20817 
20818       /* If we're a function-scope tag, initially use a parent of NULL;
20819 	 this will be fixed up in decls_for_scope.  */
20820       if (decl_function_context (decl))
20821 	context_die = NULL;
20822 
20823       break;
20824 
20825     case NAMELIST_DECL:
20826       break;
20827 
20828     default:
20829       return;
20830     }
20831 
20832   gen_decl_die (decl, NULL, context_die);
20833 }
20834 
20835 /* Write the debugging output for DECL.  */
20836 
20837 static void
dwarf2out_function_decl(tree decl)20838 dwarf2out_function_decl (tree decl)
20839 {
20840   dwarf2out_decl (decl);
20841   call_arg_locations = NULL;
20842   call_arg_loc_last = NULL;
20843   call_site_count = -1;
20844   tail_call_site_count = -1;
20845   block_map.release ();
20846   htab_empty (decl_loc_table);
20847   htab_empty (cached_dw_loc_list_table);
20848 }
20849 
20850 /* Output a marker (i.e. a label) for the beginning of the generated code for
20851    a lexical block.  */
20852 
20853 static void
dwarf2out_begin_block(unsigned int line ATTRIBUTE_UNUSED,unsigned int blocknum)20854 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20855 		       unsigned int blocknum)
20856 {
20857   switch_to_section (current_function_section ());
20858   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20859 }
20860 
20861 /* Output a marker (i.e. a label) for the end of the generated code for a
20862    lexical block.  */
20863 
20864 static void
dwarf2out_end_block(unsigned int line ATTRIBUTE_UNUSED,unsigned int blocknum)20865 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20866 {
20867   switch_to_section (current_function_section ());
20868   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20869 }
20870 
20871 /* Returns nonzero if it is appropriate not to emit any debugging
20872    information for BLOCK, because it doesn't contain any instructions.
20873 
20874    Don't allow this for blocks with nested functions or local classes
20875    as we would end up with orphans, and in the presence of scheduling
20876    we may end up calling them anyway.  */
20877 
20878 static bool
dwarf2out_ignore_block(const_tree block)20879 dwarf2out_ignore_block (const_tree block)
20880 {
20881   tree decl;
20882   unsigned int i;
20883 
20884   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20885     if (TREE_CODE (decl) == FUNCTION_DECL
20886 	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20887       return 0;
20888   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20889     {
20890       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20891       if (TREE_CODE (decl) == FUNCTION_DECL
20892 	  || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20893       return 0;
20894     }
20895 
20896   return 1;
20897 }
20898 
20899 /* Hash table routines for file_hash.  */
20900 
20901 static int
file_table_eq(const void * p1_p,const void * p2_p)20902 file_table_eq (const void *p1_p, const void *p2_p)
20903 {
20904   const struct dwarf_file_data *const p1 =
20905     (const struct dwarf_file_data *) p1_p;
20906   const char *const p2 = (const char *) p2_p;
20907   return filename_cmp (p1->filename, p2) == 0;
20908 }
20909 
20910 static hashval_t
file_table_hash(const void * p_p)20911 file_table_hash (const void *p_p)
20912 {
20913   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20914   return htab_hash_string (p->filename);
20915 }
20916 
20917 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20918    dwarf2out.c) and return its "index".  The index of each (known) filename is
20919    just a unique number which is associated with only that one filename.  We
20920    need such numbers for the sake of generating labels (in the .debug_sfnames
20921    section) and references to those files numbers (in the .debug_srcinfo
20922    and.debug_macinfo sections).  If the filename given as an argument is not
20923    found in our current list, add it to the list and assign it the next
20924    available unique index number.  In order to speed up searches, we remember
20925    the index of the filename was looked up last.  This handles the majority of
20926    all searches.  */
20927 
20928 static struct dwarf_file_data *
lookup_filename(const char * file_name)20929 lookup_filename (const char *file_name)
20930 {
20931   void ** slot;
20932   struct dwarf_file_data * created;
20933 
20934   /* Check to see if the file name that was searched on the previous
20935      call matches this file name.  If so, return the index.  */
20936   if (file_table_last_lookup
20937       && (file_name == file_table_last_lookup->filename
20938 	  || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20939     return file_table_last_lookup;
20940 
20941   /* Didn't match the previous lookup, search the table.  */
20942   slot = htab_find_slot_with_hash (file_table, file_name,
20943 				   htab_hash_string (file_name), INSERT);
20944   if (*slot)
20945     return (struct dwarf_file_data *) *slot;
20946 
20947   created = ggc_alloc_dwarf_file_data ();
20948   created->filename = file_name;
20949   created->emitted_number = 0;
20950   *slot = created;
20951   return created;
20952 }
20953 
20954 /* If the assembler will construct the file table, then translate the compiler
20955    internal file table number into the assembler file table number, and emit
20956    a .file directive if we haven't already emitted one yet.  The file table
20957    numbers are different because we prune debug info for unused variables and
20958    types, which may include filenames.  */
20959 
20960 static int
maybe_emit_file(struct dwarf_file_data * fd)20961 maybe_emit_file (struct dwarf_file_data * fd)
20962 {
20963   if (! fd->emitted_number)
20964     {
20965       if (last_emitted_file)
20966 	fd->emitted_number = last_emitted_file->emitted_number + 1;
20967       else
20968 	fd->emitted_number = 1;
20969       last_emitted_file = fd;
20970 
20971       if (DWARF2_ASM_LINE_DEBUG_INFO)
20972 	{
20973 	  fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20974 	  output_quoted_string (asm_out_file,
20975 				remap_debug_filename (fd->filename));
20976 	  fputc ('\n', asm_out_file);
20977 	}
20978     }
20979 
20980   return fd->emitted_number;
20981 }
20982 
20983 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20984    That generation should happen after function debug info has been
20985    generated. The value of the attribute is the constant value of ARG.  */
20986 
20987 static void
append_entry_to_tmpl_value_parm_die_table(dw_die_ref die,tree arg)20988 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20989 {
20990   die_arg_entry entry;
20991 
20992   if (!die || !arg)
20993     return;
20994 
20995   if (!tmpl_value_parm_die_table)
20996     vec_alloc (tmpl_value_parm_die_table, 32);
20997 
20998   entry.die = die;
20999   entry.arg = arg;
21000   vec_safe_push (tmpl_value_parm_die_table, entry);
21001 }
21002 
21003 /* Return TRUE if T is an instance of generic type, FALSE
21004    otherwise.  */
21005 
21006 static bool
generic_type_p(tree t)21007 generic_type_p (tree t)
21008 {
21009   if (t == NULL_TREE || !TYPE_P (t))
21010     return false;
21011   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21012 }
21013 
21014 /* Schedule the generation of the generic parameter dies for the
21015   instance of generic type T. The proper generation itself is later
21016   done by gen_scheduled_generic_parms_dies. */
21017 
21018 static void
schedule_generic_params_dies_gen(tree t)21019 schedule_generic_params_dies_gen (tree t)
21020 {
21021   if (!generic_type_p (t))
21022     return;
21023 
21024   if (!generic_type_instances)
21025     vec_alloc (generic_type_instances, 256);
21026 
21027   vec_safe_push (generic_type_instances, t);
21028 }
21029 
21030 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21031    by append_entry_to_tmpl_value_parm_die_table. This function must
21032    be called after function DIEs have been generated.  */
21033 
21034 static void
gen_remaining_tmpl_value_param_die_attribute(void)21035 gen_remaining_tmpl_value_param_die_attribute (void)
21036 {
21037   if (tmpl_value_parm_die_table)
21038     {
21039       unsigned i;
21040       die_arg_entry *e;
21041 
21042       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21043 	tree_add_const_value_attribute (e->die, e->arg);
21044     }
21045 }
21046 
21047 /* Generate generic parameters DIEs for instances of generic types
21048    that have been previously scheduled by
21049    schedule_generic_params_dies_gen. This function must be called
21050    after all the types of the CU have been laid out.  */
21051 
21052 static void
gen_scheduled_generic_parms_dies(void)21053 gen_scheduled_generic_parms_dies (void)
21054 {
21055   unsigned i;
21056   tree t;
21057 
21058   if (!generic_type_instances)
21059     return;
21060 
21061   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21062     if (COMPLETE_TYPE_P (t))
21063       gen_generic_params_dies (t);
21064 }
21065 
21066 
21067 /* Replace DW_AT_name for the decl with name.  */
21068 
21069 static void
dwarf2out_set_name(tree decl,tree name)21070 dwarf2out_set_name (tree decl, tree name)
21071 {
21072   dw_die_ref die;
21073   dw_attr_ref attr;
21074   const char *dname;
21075 
21076   die = TYPE_SYMTAB_DIE (decl);
21077   if (!die)
21078     return;
21079 
21080   dname = dwarf2_name (name, 0);
21081   if (!dname)
21082     return;
21083 
21084   attr = get_AT (die, DW_AT_name);
21085   if (attr)
21086     {
21087       struct indirect_string_node *node;
21088 
21089       node = find_AT_string (dname);
21090       /* replace the string.  */
21091       attr->dw_attr_val.v.val_str = node;
21092     }
21093 
21094   else
21095     add_name_attribute (die, dname);
21096 }
21097 
21098 /* True if before or during processing of the first function being emitted.  */
21099 static bool in_first_function_p = true;
21100 /* True if loc_note during dwarf2out_var_location call might still be
21101    before first real instruction at address equal to .Ltext0.  */
21102 static bool maybe_at_text_label_p = true;
21103 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
21104 static unsigned int first_loclabel_num_not_at_text_label;
21105 
21106 /* Called by the final INSN scan whenever we see a var location.  We
21107    use it to drop labels in the right places, and throw the location in
21108    our lookup table.  */
21109 
21110 static void
dwarf2out_var_location(rtx loc_note)21111 dwarf2out_var_location (rtx loc_note)
21112 {
21113   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21114   struct var_loc_node *newloc;
21115   rtx next_real, next_note;
21116   static const char *last_label;
21117   static const char *last_postcall_label;
21118   static bool last_in_cold_section_p;
21119   static rtx expected_next_loc_note;
21120   tree decl;
21121   bool var_loc_p;
21122 
21123   if (!NOTE_P (loc_note))
21124     {
21125       if (CALL_P (loc_note))
21126 	{
21127 	  call_site_count++;
21128 	  if (SIBLING_CALL_P (loc_note))
21129 	    tail_call_site_count++;
21130 	}
21131       return;
21132     }
21133 
21134   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21135   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21136     return;
21137 
21138   /* Optimize processing a large consecutive sequence of location
21139      notes so we don't spend too much time in next_real_insn.  If the
21140      next insn is another location note, remember the next_real_insn
21141      calculation for next time.  */
21142   next_real = cached_next_real_insn;
21143   if (next_real)
21144     {
21145       if (expected_next_loc_note != loc_note)
21146 	next_real = NULL_RTX;
21147     }
21148 
21149   next_note = NEXT_INSN (loc_note);
21150   if (! next_note
21151       || INSN_DELETED_P (next_note)
21152       || ! NOTE_P (next_note)
21153       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21154 	  && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21155     next_note = NULL_RTX;
21156 
21157   if (! next_real)
21158     next_real = next_real_insn (loc_note);
21159 
21160   if (next_note)
21161     {
21162       expected_next_loc_note = next_note;
21163       cached_next_real_insn = next_real;
21164     }
21165   else
21166     cached_next_real_insn = NULL_RTX;
21167 
21168   /* If there are no instructions which would be affected by this note,
21169      don't do anything.  */
21170   if (var_loc_p
21171       && next_real == NULL_RTX
21172       && !NOTE_DURING_CALL_P (loc_note))
21173     return;
21174 
21175   if (next_real == NULL_RTX)
21176     next_real = get_last_insn ();
21177 
21178   /* If there were any real insns between note we processed last time
21179      and this note (or if it is the first note), clear
21180      last_{,postcall_}label so that they are not reused this time.  */
21181   if (last_var_location_insn == NULL_RTX
21182       || last_var_location_insn != next_real
21183       || last_in_cold_section_p != in_cold_section_p)
21184     {
21185       last_label = NULL;
21186       last_postcall_label = NULL;
21187     }
21188 
21189   if (var_loc_p)
21190     {
21191       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21192       newloc = add_var_loc_to_decl (decl, loc_note,
21193 				    NOTE_DURING_CALL_P (loc_note)
21194 				    ? last_postcall_label : last_label);
21195       if (newloc == NULL)
21196 	return;
21197     }
21198   else
21199     {
21200       decl = NULL_TREE;
21201       newloc = NULL;
21202     }
21203 
21204   /* If there were no real insns between note we processed last time
21205      and this note, use the label we emitted last time.  Otherwise
21206      create a new label and emit it.  */
21207   if (last_label == NULL)
21208     {
21209       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21210       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21211       loclabel_num++;
21212       last_label = ggc_strdup (loclabel);
21213       /* See if loclabel might be equal to .Ltext0.  If yes,
21214 	 bump first_loclabel_num_not_at_text_label.  */
21215       if (!have_multiple_function_sections
21216 	  && in_first_function_p
21217 	  && maybe_at_text_label_p)
21218 	{
21219 	  static rtx last_start;
21220 	  rtx insn;
21221 	  for (insn = loc_note; insn; insn = previous_insn (insn))
21222 	    if (insn == last_start)
21223 	      break;
21224 	    else if (!NONDEBUG_INSN_P (insn))
21225 	      continue;
21226 	    else
21227 	      {
21228 		rtx body = PATTERN (insn);
21229 		if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21230 		  continue;
21231 		/* Inline asm could occupy zero bytes.  */
21232 		else if (GET_CODE (body) == ASM_INPUT
21233 			 || asm_noperands (body) >= 0)
21234 		  continue;
21235 #ifdef HAVE_attr_length
21236 		else if (get_attr_min_length (insn) == 0)
21237 		  continue;
21238 #endif
21239 		else
21240 		  {
21241 		    /* Assume insn has non-zero length.  */
21242 		    maybe_at_text_label_p = false;
21243 		    break;
21244 		  }
21245 	      }
21246 	  if (maybe_at_text_label_p)
21247 	    {
21248 	      last_start = loc_note;
21249 	      first_loclabel_num_not_at_text_label = loclabel_num;
21250 	    }
21251 	}
21252     }
21253 
21254   if (!var_loc_p)
21255     {
21256       struct call_arg_loc_node *ca_loc
21257 	= ggc_alloc_cleared_call_arg_loc_node ();
21258       rtx prev = prev_real_insn (loc_note), x;
21259       ca_loc->call_arg_loc_note = loc_note;
21260       ca_loc->next = NULL;
21261       ca_loc->label = last_label;
21262       gcc_assert (prev
21263 		  && (CALL_P (prev)
21264 		      || (NONJUMP_INSN_P (prev)
21265 			  && GET_CODE (PATTERN (prev)) == SEQUENCE
21266 			  && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21267       if (!CALL_P (prev))
21268 	prev = XVECEXP (PATTERN (prev), 0, 0);
21269       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21270       x = get_call_rtx_from (PATTERN (prev));
21271       if (x)
21272 	{
21273 	  x = XEXP (XEXP (x, 0), 0);
21274 	  if (GET_CODE (x) == SYMBOL_REF
21275 	      && SYMBOL_REF_DECL (x)
21276 	      && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21277 	    ca_loc->symbol_ref = x;
21278 	}
21279       ca_loc->block = insn_scope (prev);
21280       if (call_arg_locations)
21281 	call_arg_loc_last->next = ca_loc;
21282       else
21283 	call_arg_locations = ca_loc;
21284       call_arg_loc_last = ca_loc;
21285     }
21286   else if (!NOTE_DURING_CALL_P (loc_note))
21287     newloc->label = last_label;
21288   else
21289     {
21290       if (!last_postcall_label)
21291 	{
21292 	  sprintf (loclabel, "%s-1", last_label);
21293 	  last_postcall_label = ggc_strdup (loclabel);
21294 	}
21295       newloc->label = last_postcall_label;
21296     }
21297 
21298   last_var_location_insn = next_real;
21299   last_in_cold_section_p = in_cold_section_p;
21300 }
21301 
21302 /* Note in one location list that text section has changed.  */
21303 
21304 static int
var_location_switch_text_section_1(void ** slot,void * data ATTRIBUTE_UNUSED)21305 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21306 {
21307   var_loc_list *list = (var_loc_list *) *slot;
21308   if (list->first)
21309     list->last_before_switch
21310       = list->last->next ? list->last->next : list->last;
21311   return 1;
21312 }
21313 
21314 /* Note in all location lists that text section has changed.  */
21315 
21316 static void
var_location_switch_text_section(void)21317 var_location_switch_text_section (void)
21318 {
21319   if (decl_loc_table == NULL)
21320     return;
21321 
21322   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21323 }
21324 
21325 /* Create a new line number table.  */
21326 
21327 static dw_line_info_table *
new_line_info_table(void)21328 new_line_info_table (void)
21329 {
21330   dw_line_info_table *table;
21331 
21332   table = ggc_alloc_cleared_dw_line_info_table_struct ();
21333   table->file_num = 1;
21334   table->line_num = 1;
21335   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21336 
21337   return table;
21338 }
21339 
21340 /* Lookup the "current" table into which we emit line info, so
21341    that we don't have to do it for every source line.  */
21342 
21343 static void
set_cur_line_info_table(section * sec)21344 set_cur_line_info_table (section *sec)
21345 {
21346   dw_line_info_table *table;
21347 
21348   if (sec == text_section)
21349     table = text_section_line_info;
21350   else if (sec == cold_text_section)
21351     {
21352       table = cold_text_section_line_info;
21353       if (!table)
21354 	{
21355 	  cold_text_section_line_info = table = new_line_info_table ();
21356 	  table->end_label = cold_end_label;
21357 	}
21358     }
21359   else
21360     {
21361       const char *end_label;
21362 
21363       if (flag_reorder_blocks_and_partition)
21364 	{
21365 	  if (in_cold_section_p)
21366 	    end_label = crtl->subsections.cold_section_end_label;
21367 	  else
21368 	    end_label = crtl->subsections.hot_section_end_label;
21369 	}
21370       else
21371 	{
21372 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
21373 	  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21374 				       current_function_funcdef_no);
21375 	  end_label = ggc_strdup (label);
21376 	}
21377 
21378       table = new_line_info_table ();
21379       table->end_label = end_label;
21380 
21381       vec_safe_push (separate_line_info, table);
21382     }
21383 
21384   if (DWARF2_ASM_LINE_DEBUG_INFO)
21385     table->is_stmt = (cur_line_info_table
21386 		      ? cur_line_info_table->is_stmt
21387 		      : DWARF_LINE_DEFAULT_IS_STMT_START);
21388   cur_line_info_table = table;
21389 }
21390 
21391 
21392 /* We need to reset the locations at the beginning of each
21393    function. We can't do this in the end_function hook, because the
21394    declarations that use the locations won't have been output when
21395    that hook is called.  Also compute have_multiple_function_sections here.  */
21396 
21397 static void
dwarf2out_begin_function(tree fun)21398 dwarf2out_begin_function (tree fun)
21399 {
21400   section *sec = function_section (fun);
21401 
21402   if (sec != text_section)
21403     have_multiple_function_sections = true;
21404 
21405   if (flag_reorder_blocks_and_partition && !cold_text_section)
21406     {
21407       gcc_assert (current_function_decl == fun);
21408       cold_text_section = unlikely_text_section ();
21409       switch_to_section (cold_text_section);
21410       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21411       switch_to_section (sec);
21412     }
21413 
21414   dwarf2out_note_section_used ();
21415   call_site_count = 0;
21416   tail_call_site_count = 0;
21417 
21418   set_cur_line_info_table (sec);
21419 }
21420 
21421 /* Helper function of dwarf2out_end_function, called only after emitting
21422    the very first function into assembly.  Check if some .debug_loc range
21423    might end with a .LVL* label that could be equal to .Ltext0.
21424    In that case we must force using absolute addresses in .debug_loc ranges,
21425    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21426    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21427    list terminator.
21428    Set have_multiple_function_sections to true in that case and
21429    terminate htab traversal.  */
21430 
21431 static int
find_empty_loc_ranges_at_text_label(void ** slot,void *)21432 find_empty_loc_ranges_at_text_label (void **slot, void *)
21433 {
21434   var_loc_list *entry;
21435   struct var_loc_node *node;
21436 
21437   entry = (var_loc_list *) *slot;
21438   node = entry->first;
21439   if (node && node->next && node->next->label)
21440     {
21441       unsigned int i;
21442       const char *label = node->next->label;
21443       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21444 
21445       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21446 	{
21447 	  ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21448 	  if (strcmp (label, loclabel) == 0)
21449 	    {
21450 	      have_multiple_function_sections = true;
21451 	      return 0;
21452 	    }
21453 	}
21454     }
21455   return 1;
21456 }
21457 
21458 /* Hook called after emitting a function into assembly.
21459    This does something only for the very first function emitted.  */
21460 
21461 static void
dwarf2out_end_function(unsigned int)21462 dwarf2out_end_function (unsigned int)
21463 {
21464   if (in_first_function_p
21465       && !have_multiple_function_sections
21466       && first_loclabel_num_not_at_text_label
21467       && decl_loc_table)
21468     htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21469 		   NULL);
21470   in_first_function_p = false;
21471   maybe_at_text_label_p = false;
21472 }
21473 
21474 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
21475 
21476 static void
push_dw_line_info_entry(dw_line_info_table * table,enum dw_line_info_opcode opcode,unsigned int val)21477 push_dw_line_info_entry (dw_line_info_table *table,
21478 			 enum dw_line_info_opcode opcode, unsigned int val)
21479 {
21480   dw_line_info_entry e;
21481   e.opcode = opcode;
21482   e.val = val;
21483   vec_safe_push (table->entries, e);
21484 }
21485 
21486 /* Output a label to mark the beginning of a source code line entry
21487    and record information relating to this source line, in
21488    'line_info_table' for later output of the .debug_line section.  */
21489 /* ??? The discriminator parameter ought to be unsigned.  */
21490 
21491 static void
dwarf2out_source_line(unsigned int line,const char * filename,int discriminator,bool is_stmt)21492 dwarf2out_source_line (unsigned int line, const char *filename,
21493                        int discriminator, bool is_stmt)
21494 {
21495   unsigned int file_num;
21496   dw_line_info_table *table;
21497 
21498   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21499     return;
21500 
21501   /* The discriminator column was added in dwarf4.  Simplify the below
21502      by simply removing it if we're not supposed to output it.  */
21503   if (dwarf_version < 4 && dwarf_strict)
21504     discriminator = 0;
21505 
21506   table = cur_line_info_table;
21507   file_num = maybe_emit_file (lookup_filename (filename));
21508 
21509   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
21510      the debugger has used the second (possibly duplicate) line number
21511      at the beginning of the function to mark the end of the prologue.
21512      We could eliminate any other duplicates within the function.  For
21513      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21514      that second line number entry.  */
21515   /* Recall that this end-of-prologue indication is *not* the same thing
21516      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
21517      to which the hook corresponds, follows the last insn that was
21518      emitted by gen_prologue.  What we need is to precede the first insn
21519      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21520      insn that corresponds to something the user wrote.  These may be
21521      very different locations once scheduling is enabled.  */
21522 
21523   if (0 && file_num == table->file_num
21524       && line == table->line_num
21525       && discriminator == table->discrim_num
21526       && is_stmt == table->is_stmt)
21527     return;
21528 
21529   switch_to_section (current_function_section ());
21530 
21531   /* If requested, emit something human-readable.  */
21532   if (flag_debug_asm)
21533     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21534 
21535   if (DWARF2_ASM_LINE_DEBUG_INFO)
21536     {
21537       /* Emit the .loc directive understood by GNU as.  */
21538       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21539 	 file_num, line, is_stmt, discriminator */
21540       fputs ("\t.loc ", asm_out_file);
21541       fprint_ul (asm_out_file, file_num);
21542       putc (' ', asm_out_file);
21543       fprint_ul (asm_out_file, line);
21544       putc (' ', asm_out_file);
21545       putc ('0', asm_out_file);
21546 
21547       if (is_stmt != table->is_stmt)
21548 	{
21549 	  fputs (" is_stmt ", asm_out_file);
21550 	  putc (is_stmt ? '1' : '0', asm_out_file);
21551 	}
21552       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21553 	{
21554 	  gcc_assert (discriminator > 0);
21555 	  fputs (" discriminator ", asm_out_file);
21556 	  fprint_ul (asm_out_file, (unsigned long) discriminator);
21557 	}
21558       putc ('\n', asm_out_file);
21559     }
21560   else
21561     {
21562       unsigned int label_num = ++line_info_label_num;
21563 
21564       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21565 
21566       push_dw_line_info_entry (table, LI_set_address, label_num);
21567       if (file_num != table->file_num)
21568 	push_dw_line_info_entry (table, LI_set_file, file_num);
21569       if (discriminator != table->discrim_num)
21570 	push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21571       if (is_stmt != table->is_stmt)
21572 	push_dw_line_info_entry (table, LI_negate_stmt, 0);
21573       push_dw_line_info_entry (table, LI_set_line, line);
21574     }
21575 
21576   table->file_num = file_num;
21577   table->line_num = line;
21578   table->discrim_num = discriminator;
21579   table->is_stmt = is_stmt;
21580   table->in_use = true;
21581 }
21582 
21583 /* Record the beginning of a new source file.  */
21584 
21585 static void
dwarf2out_start_source_file(unsigned int lineno,const char * filename)21586 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21587 {
21588   if (flag_eliminate_dwarf2_dups)
21589     {
21590       /* Record the beginning of the file for break_out_includes.  */
21591       dw_die_ref bincl_die;
21592 
21593       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21594       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21595     }
21596 
21597   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21598     {
21599       macinfo_entry e;
21600       e.code = DW_MACINFO_start_file;
21601       e.lineno = lineno;
21602       e.info = ggc_strdup (filename);
21603       vec_safe_push (macinfo_table, e);
21604     }
21605 }
21606 
21607 /* Record the end of a source file.  */
21608 
21609 static void
dwarf2out_end_source_file(unsigned int lineno ATTRIBUTE_UNUSED)21610 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21611 {
21612   if (flag_eliminate_dwarf2_dups)
21613     /* Record the end of the file for break_out_includes.  */
21614     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21615 
21616   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21617     {
21618       macinfo_entry e;
21619       e.code = DW_MACINFO_end_file;
21620       e.lineno = lineno;
21621       e.info = NULL;
21622       vec_safe_push (macinfo_table, e);
21623     }
21624 }
21625 
21626 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21627    the tail part of the directive line, i.e. the part which is past the
21628    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21629 
21630 static void
dwarf2out_define(unsigned int lineno ATTRIBUTE_UNUSED,const char * buffer ATTRIBUTE_UNUSED)21631 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21632 		  const char *buffer ATTRIBUTE_UNUSED)
21633 {
21634   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21635     {
21636       macinfo_entry e;
21637       /* Insert a dummy first entry to be able to optimize the whole
21638 	 predefined macro block using DW_MACRO_GNU_transparent_include.  */
21639       if (macinfo_table->is_empty () && lineno <= 1)
21640 	{
21641 	  e.code = 0;
21642 	  e.lineno = 0;
21643 	  e.info = NULL;
21644 	  vec_safe_push (macinfo_table, e);
21645 	}
21646       e.code = DW_MACINFO_define;
21647       e.lineno = lineno;
21648       e.info = ggc_strdup (buffer);
21649       vec_safe_push (macinfo_table, e);
21650     }
21651 }
21652 
21653 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21654    the tail part of the directive line, i.e. the part which is past the
21655    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21656 
21657 static void
dwarf2out_undef(unsigned int lineno ATTRIBUTE_UNUSED,const char * buffer ATTRIBUTE_UNUSED)21658 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21659 		 const char *buffer ATTRIBUTE_UNUSED)
21660 {
21661   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21662     {
21663       macinfo_entry e;
21664       /* Insert a dummy first entry to be able to optimize the whole
21665 	 predefined macro block using DW_MACRO_GNU_transparent_include.  */
21666       if (macinfo_table->is_empty () && lineno <= 1)
21667 	{
21668 	  e.code = 0;
21669 	  e.lineno = 0;
21670 	  e.info = NULL;
21671 	  vec_safe_push (macinfo_table, e);
21672 	}
21673       e.code = DW_MACINFO_undef;
21674       e.lineno = lineno;
21675       e.info = ggc_strdup (buffer);
21676       vec_safe_push (macinfo_table, e);
21677     }
21678 }
21679 
21680 /* Helpers to manipulate hash table of CUs.  */
21681 
21682 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21683 {
21684   typedef macinfo_entry value_type;
21685   typedef macinfo_entry compare_type;
21686   static inline hashval_t hash (const value_type *);
21687   static inline bool equal (const value_type *, const compare_type *);
21688 };
21689 
21690 inline hashval_t
hash(const value_type * entry)21691 macinfo_entry_hasher::hash (const value_type *entry)
21692 {
21693   return htab_hash_string (entry->info);
21694 }
21695 
21696 inline bool
equal(const value_type * entry1,const compare_type * entry2)21697 macinfo_entry_hasher::equal (const value_type *entry1,
21698 			     const compare_type *entry2)
21699 {
21700   return !strcmp (entry1->info, entry2->info);
21701 }
21702 
21703 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21704 
21705 /* Output a single .debug_macinfo entry.  */
21706 
21707 static void
output_macinfo_op(macinfo_entry * ref)21708 output_macinfo_op (macinfo_entry *ref)
21709 {
21710   int file_num;
21711   size_t len;
21712   struct indirect_string_node *node;
21713   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21714   struct dwarf_file_data *fd;
21715 
21716   switch (ref->code)
21717     {
21718     case DW_MACINFO_start_file:
21719       fd = lookup_filename (ref->info);
21720       file_num = maybe_emit_file (fd);
21721       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21722       dw2_asm_output_data_uleb128 (ref->lineno,
21723 				   "Included from line number %lu",
21724 				   (unsigned long) ref->lineno);
21725       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21726       break;
21727     case DW_MACINFO_end_file:
21728       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21729       break;
21730     case DW_MACINFO_define:
21731     case DW_MACINFO_undef:
21732       len = strlen (ref->info) + 1;
21733       if (!dwarf_strict
21734 	  && len > DWARF_OFFSET_SIZE
21735 	  && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21736 	  && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21737 	{
21738 	  ref->code = ref->code == DW_MACINFO_define
21739 		      ? DW_MACRO_GNU_define_indirect
21740 		      : DW_MACRO_GNU_undef_indirect;
21741 	  output_macinfo_op (ref);
21742 	  return;
21743 	}
21744       dw2_asm_output_data (1, ref->code,
21745 			   ref->code == DW_MACINFO_define
21746 			   ? "Define macro" : "Undefine macro");
21747       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21748 				   (unsigned long) ref->lineno);
21749       dw2_asm_output_nstring (ref->info, -1, "The macro");
21750       break;
21751     case DW_MACRO_GNU_define_indirect:
21752     case DW_MACRO_GNU_undef_indirect:
21753       node = find_AT_string (ref->info);
21754       gcc_assert (node
21755                   && ((node->form == DW_FORM_strp)
21756                       || (node->form == DW_FORM_GNU_str_index)));
21757       dw2_asm_output_data (1, ref->code,
21758 			   ref->code == DW_MACRO_GNU_define_indirect
21759 			   ? "Define macro indirect"
21760 			   : "Undefine macro indirect");
21761       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21762 				   (unsigned long) ref->lineno);
21763       if (node->form == DW_FORM_strp)
21764         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21765                                debug_str_section, "The macro: \"%s\"",
21766                                ref->info);
21767       else
21768         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21769                                      ref->info);
21770       break;
21771     case DW_MACRO_GNU_transparent_include:
21772       dw2_asm_output_data (1, ref->code, "Transparent include");
21773       ASM_GENERATE_INTERNAL_LABEL (label,
21774 				   DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21775       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21776       break;
21777     default:
21778       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21779 	       ASM_COMMENT_START, (unsigned long) ref->code);
21780       break;
21781     }
21782 }
21783 
21784 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21785    other compilation unit .debug_macinfo sections.  IDX is the first
21786    index of a define/undef, return the number of ops that should be
21787    emitted in a comdat .debug_macinfo section and emit
21788    a DW_MACRO_GNU_transparent_include entry referencing it.
21789    If the define/undef entry should be emitted normally, return 0.  */
21790 
21791 static unsigned
optimize_macinfo_range(unsigned int idx,vec<macinfo_entry,va_gc> * files,macinfo_hash_type * macinfo_htab)21792 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21793 			macinfo_hash_type *macinfo_htab)
21794 {
21795   macinfo_entry *first, *second, *cur, *inc;
21796   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21797   unsigned char checksum[16];
21798   struct md5_ctx ctx;
21799   char *grp_name, *tail;
21800   const char *base;
21801   unsigned int i, count, encoded_filename_len, linebuf_len;
21802   macinfo_entry **slot;
21803 
21804   first = &(*macinfo_table)[idx];
21805   second = &(*macinfo_table)[idx + 1];
21806 
21807   /* Optimize only if there are at least two consecutive define/undef ops,
21808      and either all of them are before first DW_MACINFO_start_file
21809      with lineno {0,1} (i.e. predefined macro block), or all of them are
21810      in some included header file.  */
21811   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21812     return 0;
21813   if (vec_safe_is_empty (files))
21814     {
21815       if (first->lineno > 1 || second->lineno > 1)
21816 	return 0;
21817     }
21818   else if (first->lineno == 0)
21819     return 0;
21820 
21821   /* Find the last define/undef entry that can be grouped together
21822      with first and at the same time compute md5 checksum of their
21823      codes, linenumbers and strings.  */
21824   md5_init_ctx (&ctx);
21825   for (i = idx; macinfo_table->iterate (i, &cur); i++)
21826     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21827       break;
21828     else if (vec_safe_is_empty (files) && cur->lineno > 1)
21829       break;
21830     else
21831       {
21832 	unsigned char code = cur->code;
21833 	md5_process_bytes (&code, 1, &ctx);
21834 	checksum_uleb128 (cur->lineno, &ctx);
21835 	md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21836       }
21837   md5_finish_ctx (&ctx, checksum);
21838   count = i - idx;
21839 
21840   /* From the containing include filename (if any) pick up just
21841      usable characters from its basename.  */
21842   if (vec_safe_is_empty (files))
21843     base = "";
21844   else
21845     base = lbasename (files->last ().info);
21846   for (encoded_filename_len = 0, i = 0; base[i]; i++)
21847     if (ISIDNUM (base[i]) || base[i] == '.')
21848       encoded_filename_len++;
21849   /* Count . at the end.  */
21850   if (encoded_filename_len)
21851     encoded_filename_len++;
21852 
21853   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21854   linebuf_len = strlen (linebuf);
21855 
21856   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
21857   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21858 			 + 16 * 2 + 1);
21859   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21860   tail = grp_name + 4;
21861   if (encoded_filename_len)
21862     {
21863       for (i = 0; base[i]; i++)
21864 	if (ISIDNUM (base[i]) || base[i] == '.')
21865 	  *tail++ = base[i];
21866       *tail++ = '.';
21867     }
21868   memcpy (tail, linebuf, linebuf_len);
21869   tail += linebuf_len;
21870   *tail++ = '.';
21871   for (i = 0; i < 16; i++)
21872     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21873 
21874   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21875      in the empty vector entry before the first define/undef.  */
21876   inc = &(*macinfo_table)[idx - 1];
21877   inc->code = DW_MACRO_GNU_transparent_include;
21878   inc->lineno = 0;
21879   inc->info = ggc_strdup (grp_name);
21880   if (!macinfo_htab->is_created ())
21881     macinfo_htab->create (10);
21882   /* Avoid emitting duplicates.  */
21883   slot = macinfo_htab->find_slot (inc, INSERT);
21884   if (*slot != NULL)
21885     {
21886       inc->code = 0;
21887       inc->info = NULL;
21888       /* If such an entry has been used before, just emit
21889 	 a DW_MACRO_GNU_transparent_include op.  */
21890       inc = *slot;
21891       output_macinfo_op (inc);
21892       /* And clear all macinfo_entry in the range to avoid emitting them
21893 	 in the second pass.  */
21894       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21895 	{
21896 	  cur->code = 0;
21897 	  cur->info = NULL;
21898 	}
21899     }
21900   else
21901     {
21902       *slot = inc;
21903       inc->lineno = macinfo_htab->elements ();
21904       output_macinfo_op (inc);
21905     }
21906   return count;
21907 }
21908 
21909 /* Save any strings needed by the macinfo table in the debug str
21910    table.  All strings must be collected into the table by the time
21911    index_string is called.  */
21912 
21913 static void
save_macinfo_strings(void)21914 save_macinfo_strings (void)
21915 {
21916   unsigned len;
21917   unsigned i;
21918   macinfo_entry *ref;
21919 
21920   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21921     {
21922       switch (ref->code)
21923         {
21924           /* Match the logic in output_macinfo_op to decide on
21925              indirect strings.  */
21926           case DW_MACINFO_define:
21927           case DW_MACINFO_undef:
21928             len = strlen (ref->info) + 1;
21929             if (!dwarf_strict
21930                 && len > DWARF_OFFSET_SIZE
21931                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21932                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21933               set_indirect_string (find_AT_string (ref->info));
21934             break;
21935           case DW_MACRO_GNU_define_indirect:
21936           case DW_MACRO_GNU_undef_indirect:
21937             set_indirect_string (find_AT_string (ref->info));
21938             break;
21939           default:
21940             break;
21941         }
21942     }
21943 }
21944 
21945 /* Output macinfo section(s).  */
21946 
21947 static void
output_macinfo(void)21948 output_macinfo (void)
21949 {
21950   unsigned i;
21951   unsigned long length = vec_safe_length (macinfo_table);
21952   macinfo_entry *ref;
21953   vec<macinfo_entry, va_gc> *files = NULL;
21954   macinfo_hash_type macinfo_htab;
21955 
21956   if (! length)
21957     return;
21958 
21959   /* output_macinfo* uses these interchangeably.  */
21960   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21961 	      && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21962 	      && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21963 	      && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21964 
21965   /* For .debug_macro emit the section header.  */
21966   if (!dwarf_strict)
21967     {
21968       dw2_asm_output_data (2, 4, "DWARF macro version number");
21969       if (DWARF_OFFSET_SIZE == 8)
21970 	dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21971       else
21972 	dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21973       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21974                              (!dwarf_split_debug_info ? debug_line_section_label
21975                               : debug_skeleton_line_section_label),
21976                              debug_line_section, NULL);
21977     }
21978 
21979   /* In the first loop, it emits the primary .debug_macinfo section
21980      and after each emitted op the macinfo_entry is cleared.
21981      If a longer range of define/undef ops can be optimized using
21982      DW_MACRO_GNU_transparent_include, the
21983      DW_MACRO_GNU_transparent_include op is emitted and kept in
21984      the vector before the first define/undef in the range and the
21985      whole range of define/undef ops is not emitted and kept.  */
21986   for (i = 0; macinfo_table->iterate (i, &ref); i++)
21987     {
21988       switch (ref->code)
21989 	{
21990 	case DW_MACINFO_start_file:
21991 	  vec_safe_push (files, *ref);
21992 	  break;
21993 	case DW_MACINFO_end_file:
21994 	  if (!vec_safe_is_empty (files))
21995 	    files->pop ();
21996 	  break;
21997 	case DW_MACINFO_define:
21998 	case DW_MACINFO_undef:
21999 	  if (!dwarf_strict
22000 	      && HAVE_COMDAT_GROUP
22001 	      && vec_safe_length (files) != 1
22002 	      && i > 0
22003 	      && i + 1 < length
22004 	      && (*macinfo_table)[i - 1].code == 0)
22005 	    {
22006 	      unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22007 	      if (count)
22008 		{
22009 		  i += count - 1;
22010 		  continue;
22011 		}
22012 	    }
22013 	  break;
22014 	case 0:
22015 	  /* A dummy entry may be inserted at the beginning to be able
22016 	     to optimize the whole block of predefined macros.  */
22017 	  if (i == 0)
22018 	    continue;
22019 	default:
22020 	  break;
22021 	}
22022       output_macinfo_op (ref);
22023       ref->info = NULL;
22024       ref->code = 0;
22025     }
22026 
22027   if (!macinfo_htab.is_created ())
22028     return;
22029 
22030   macinfo_htab.dispose ();
22031 
22032   /* If any DW_MACRO_GNU_transparent_include were used, on those
22033      DW_MACRO_GNU_transparent_include entries terminate the
22034      current chain and switch to a new comdat .debug_macinfo
22035      section and emit the define/undef entries within it.  */
22036   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22037     switch (ref->code)
22038       {
22039       case 0:
22040 	continue;
22041       case DW_MACRO_GNU_transparent_include:
22042 	{
22043 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
22044 	  tree comdat_key = get_identifier (ref->info);
22045 	  /* Terminate the previous .debug_macinfo section.  */
22046 	  dw2_asm_output_data (1, 0, "End compilation unit");
22047 	  targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22048 					 SECTION_DEBUG
22049 					 | SECTION_LINKONCE,
22050 					 comdat_key);
22051 	  ASM_GENERATE_INTERNAL_LABEL (label,
22052 				       DEBUG_MACRO_SECTION_LABEL,
22053 				       ref->lineno);
22054 	  ASM_OUTPUT_LABEL (asm_out_file, label);
22055 	  ref->code = 0;
22056 	  ref->info = NULL;
22057 	  dw2_asm_output_data (2, 4, "DWARF macro version number");
22058 	  if (DWARF_OFFSET_SIZE == 8)
22059 	    dw2_asm_output_data (1, 1, "Flags: 64-bit");
22060 	  else
22061 	    dw2_asm_output_data (1, 0, "Flags: 32-bit");
22062 	}
22063 	break;
22064       case DW_MACINFO_define:
22065       case DW_MACINFO_undef:
22066 	output_macinfo_op (ref);
22067 	ref->code = 0;
22068 	ref->info = NULL;
22069 	break;
22070       default:
22071 	gcc_unreachable ();
22072       }
22073 }
22074 
22075 /* Set up for Dwarf output at the start of compilation.  */
22076 
22077 static void
dwarf2out_init(const char * filename ATTRIBUTE_UNUSED)22078 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22079 {
22080   /* Allocate the file_table.  */
22081   file_table = htab_create_ggc (50, file_table_hash,
22082 				file_table_eq, NULL);
22083 
22084   /* Allocate the decl_die_table.  */
22085   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22086 				    decl_die_table_eq, NULL);
22087 
22088   /* Allocate the decl_loc_table.  */
22089   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22090 				    decl_loc_table_eq, NULL);
22091 
22092   /* Allocate the cached_dw_loc_list_table.  */
22093   cached_dw_loc_list_table
22094     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22095 		       cached_dw_loc_list_table_eq, NULL);
22096 
22097   /* Allocate the initial hunk of the decl_scope_table.  */
22098   vec_alloc (decl_scope_table, 256);
22099 
22100   /* Allocate the initial hunk of the abbrev_die_table.  */
22101   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22102     (ABBREV_DIE_TABLE_INCREMENT);
22103   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22104   /* Zero-th entry is allocated, but unused.  */
22105   abbrev_die_table_in_use = 1;
22106 
22107   /* Allocate the pubtypes and pubnames vectors.  */
22108   vec_alloc (pubname_table, 32);
22109   vec_alloc (pubtype_table, 32);
22110 
22111   vec_alloc (incomplete_types, 64);
22112 
22113   vec_alloc (used_rtx_array, 32);
22114 
22115   if (!dwarf_split_debug_info)
22116     {
22117       debug_info_section = get_section (DEBUG_INFO_SECTION,
22118                                         SECTION_DEBUG, NULL);
22119       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22120                                           SECTION_DEBUG, NULL);
22121       debug_loc_section = get_section (DEBUG_LOC_SECTION,
22122                                        SECTION_DEBUG, NULL);
22123     }
22124   else
22125     {
22126       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22127                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22128       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22129                                           SECTION_DEBUG | SECTION_EXCLUDE,
22130                                           NULL);
22131       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22132                                         SECTION_DEBUG, NULL);
22133       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22134                                                  SECTION_DEBUG, NULL);
22135       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22136                                                    SECTION_DEBUG, NULL);
22137       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22138                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22139 
22140       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22141          the main .o, but the skeleton_line goes into the split off dwo.  */
22142       debug_skeleton_line_section
22143           = get_section (DEBUG_DWO_LINE_SECTION,
22144                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22145       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22146                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22147       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22148                                                SECTION_DEBUG | SECTION_EXCLUDE,
22149                                                NULL);
22150       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22151                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22152       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22153                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22154       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22155                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22156     }
22157   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22158 				       SECTION_DEBUG, NULL);
22159   debug_macinfo_section = get_section (dwarf_strict
22160 				       ? DEBUG_MACINFO_SECTION
22161 				       : DEBUG_MACRO_SECTION,
22162                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
22163   debug_line_section = get_section (DEBUG_LINE_SECTION,
22164 				    SECTION_DEBUG, NULL);
22165   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22166 					SECTION_DEBUG, NULL);
22167   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22168 					SECTION_DEBUG, NULL);
22169   debug_str_section = get_section (DEBUG_STR_SECTION,
22170 				   DEBUG_STR_SECTION_FLAGS, NULL);
22171   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22172 				      SECTION_DEBUG, NULL);
22173   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22174 				     SECTION_DEBUG, NULL);
22175 
22176   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22177   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22178 			       DEBUG_ABBREV_SECTION_LABEL, 0);
22179   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22180   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22181 			       COLD_TEXT_SECTION_LABEL, 0);
22182   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22183 
22184   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22185 			       DEBUG_INFO_SECTION_LABEL, 0);
22186   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22187 			       DEBUG_LINE_SECTION_LABEL, 0);
22188   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22189 			       DEBUG_RANGES_SECTION_LABEL, 0);
22190   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22191                                DEBUG_ADDR_SECTION_LABEL, 0);
22192   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22193 			       dwarf_strict
22194 			       ? DEBUG_MACINFO_SECTION_LABEL
22195 			       : DEBUG_MACRO_SECTION_LABEL, 0);
22196   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22197 
22198   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22199     vec_alloc (macinfo_table, 64);
22200 
22201   switch_to_section (text_section);
22202   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22203 
22204   /* Make sure the line number table for .text always exists.  */
22205   text_section_line_info = new_line_info_table ();
22206   text_section_line_info->end_label = text_end_label;
22207 }
22208 
22209 /* Called before compile () starts outputtting functions, variables
22210    and toplevel asms into assembly.  */
22211 
22212 static void
dwarf2out_assembly_start(void)22213 dwarf2out_assembly_start (void)
22214 {
22215   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22216       && dwarf2out_do_cfi_asm ()
22217       && (!(flag_unwind_tables || flag_exceptions)
22218 	  || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22219     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22220 }
22221 
22222 /* A helper function for dwarf2out_finish called through
22223    htab_traverse.  Assign a string its index.  All strings must be
22224    collected into the table by the time index_string is called,
22225    because the indexing code relies on htab_traverse to traverse nodes
22226    in the same order for each run. */
22227 
22228 static int
index_string(void ** h,void * v)22229 index_string (void **h, void *v)
22230 {
22231   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22232   unsigned int *index = (unsigned int *) v;
22233 
22234   find_string_form (node);
22235   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22236     {
22237       gcc_assert (node->index == NO_INDEX_ASSIGNED);
22238       node->index = *index;
22239       *index += 1;
22240     }
22241   return 1;
22242 }
22243 
22244 /* A helper function for output_indirect_strings called through
22245    htab_traverse.  Output the offset to a string and update the
22246    current offset.  */
22247 
22248 static int
output_index_string_offset(void ** h,void * v)22249 output_index_string_offset (void **h, void *v)
22250 {
22251   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22252   unsigned int *offset = (unsigned int *) v;
22253 
22254   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22255     {
22256       /* Assert that this node has been assigned an index.  */
22257       gcc_assert (node->index != NO_INDEX_ASSIGNED
22258                   && node->index != NOT_INDEXED);
22259       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22260                            "indexed string 0x%x: %s", node->index, node->str);
22261       *offset += strlen (node->str) + 1;
22262     }
22263   return 1;
22264 }
22265 
22266 /* A helper function for dwarf2out_finish called through
22267    htab_traverse.  Output the indexed string.  */
22268 
22269 static int
output_index_string(void ** h,void * v)22270 output_index_string (void **h, void *v)
22271 {
22272   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22273   unsigned int *cur_idx = (unsigned int *) v;
22274 
22275   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22276     {
22277       /* Assert that the strings are output in the same order as their
22278          indexes were assigned.  */
22279       gcc_assert (*cur_idx == node->index);
22280       assemble_string (node->str, strlen (node->str) + 1);
22281       *cur_idx += 1;
22282     }
22283   return 1;
22284 }
22285 
22286 /* A helper function for dwarf2out_finish called through
22287    htab_traverse.  Emit one queued .debug_str string.  */
22288 
22289 static int
output_indirect_string(void ** h,void * v ATTRIBUTE_UNUSED)22290 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22291 {
22292   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22293 
22294   node->form = find_string_form (node);
22295   if (node->form == DW_FORM_strp && node->refcount > 0)
22296     {
22297       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22298       assemble_string (node->str, strlen (node->str) + 1);
22299     }
22300 
22301   return 1;
22302 }
22303 
22304 /* Output the indexed string table.  */
22305 
22306 static void
output_indirect_strings(void)22307 output_indirect_strings (void)
22308 {
22309   switch_to_section (debug_str_section);
22310   if (!dwarf_split_debug_info)
22311     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22312   else
22313     {
22314       unsigned int offset = 0;
22315       unsigned int cur_idx = 0;
22316 
22317       htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22318 
22319       switch_to_section (debug_str_offsets_section);
22320       htab_traverse_noresize (debug_str_hash,
22321                               output_index_string_offset,
22322                               &offset);
22323       switch_to_section (debug_str_dwo_section);
22324       htab_traverse_noresize (debug_str_hash,
22325                               output_index_string,
22326                               &cur_idx);
22327     }
22328 }
22329 
22330 /* Callback for htab_traverse to assign an index to an entry in the
22331    table, and to write that entry to the .debug_addr section.  */
22332 
22333 static int
output_addr_table_entry(void ** slot,void * data)22334 output_addr_table_entry (void **slot, void *data)
22335 {
22336   addr_table_entry *entry = (addr_table_entry *) *slot;
22337   unsigned int *cur_index = (unsigned int *)data;
22338 
22339   if (entry->refcount == 0)
22340     {
22341       gcc_assert (entry->index == NO_INDEX_ASSIGNED
22342                   || entry->index == NOT_INDEXED);
22343       return 1;
22344     }
22345 
22346   gcc_assert (entry->index == *cur_index);
22347   (*cur_index)++;
22348 
22349   switch (entry->kind)
22350     {
22351       case ate_kind_rtx:
22352         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22353                                  "0x%x", entry->index);
22354         break;
22355       case ate_kind_rtx_dtprel:
22356         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22357         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22358                                              DWARF2_ADDR_SIZE,
22359                                              entry->addr.rtl);
22360         fputc ('\n', asm_out_file);
22361         break;
22362       case ate_kind_label:
22363         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22364                                  "0x%x", entry->index);
22365         break;
22366       default:
22367         gcc_unreachable ();
22368     }
22369   return 1;
22370 }
22371 
22372 /* Produce the .debug_addr section.  */
22373 
22374 static void
output_addr_table(void)22375 output_addr_table (void)
22376 {
22377   unsigned int index = 0;
22378   if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22379     return;
22380 
22381   switch_to_section (debug_addr_section);
22382   htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22383 }
22384 
22385 #if ENABLE_ASSERT_CHECKING
22386 /* Verify that all marks are clear.  */
22387 
22388 static void
verify_marks_clear(dw_die_ref die)22389 verify_marks_clear (dw_die_ref die)
22390 {
22391   dw_die_ref c;
22392 
22393   gcc_assert (! die->die_mark);
22394   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22395 }
22396 #endif /* ENABLE_ASSERT_CHECKING */
22397 
22398 /* Clear the marks for a die and its children.
22399    Be cool if the mark isn't set.  */
22400 
22401 static void
prune_unmark_dies(dw_die_ref die)22402 prune_unmark_dies (dw_die_ref die)
22403 {
22404   dw_die_ref c;
22405 
22406   if (die->die_mark)
22407     die->die_mark = 0;
22408   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22409 }
22410 
22411 /* Given DIE that we're marking as used, find any other dies
22412    it references as attributes and mark them as used.  */
22413 
22414 static void
prune_unused_types_walk_attribs(dw_die_ref die)22415 prune_unused_types_walk_attribs (dw_die_ref die)
22416 {
22417   dw_attr_ref a;
22418   unsigned ix;
22419 
22420   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22421     {
22422       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22423 	{
22424 	  /* A reference to another DIE.
22425 	     Make sure that it will get emitted.
22426 	     If it was broken out into a comdat group, don't follow it.  */
22427           if (! AT_ref (a)->comdat_type_p
22428               || a->dw_attr == DW_AT_specification)
22429 	    prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22430 	}
22431       /* Set the string's refcount to 0 so that prune_unused_types_mark
22432 	 accounts properly for it.  */
22433       if (AT_class (a) == dw_val_class_str)
22434 	a->dw_attr_val.v.val_str->refcount = 0;
22435     }
22436 }
22437 
22438 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22439 
22440 static void
prune_unused_types_mark_generic_parms_dies(dw_die_ref die)22441 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22442 {
22443   dw_die_ref c;
22444 
22445   if (die == NULL || die->die_child == NULL)
22446     return;
22447   c = die->die_child;
22448   do
22449     {
22450       if (is_template_parameter (c))
22451 	prune_unused_types_mark (c, 1);
22452       c = c->die_sib;
22453     } while (c && c != die->die_child);
22454 }
22455 
22456 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22457    to DIE's children.  */
22458 
22459 static void
prune_unused_types_mark(dw_die_ref die,int dokids)22460 prune_unused_types_mark (dw_die_ref die, int dokids)
22461 {
22462   dw_die_ref c;
22463 
22464   if (die->die_mark == 0)
22465     {
22466       /* We haven't done this node yet.  Mark it as used.  */
22467       die->die_mark = 1;
22468       /* If this is the DIE of a generic type instantiation,
22469 	 mark the children DIEs that describe its generic parms and
22470 	 args.  */
22471       prune_unused_types_mark_generic_parms_dies (die);
22472 
22473       /* We also have to mark its parents as used.
22474 	 (But we don't want to mark our parent's kids due to this,
22475 	 unless it is a class.)  */
22476       if (die->die_parent)
22477 	prune_unused_types_mark (die->die_parent,
22478 				 class_scope_p (die->die_parent));
22479 
22480       /* Mark any referenced nodes.  */
22481       prune_unused_types_walk_attribs (die);
22482 
22483       /* If this node is a specification,
22484 	 also mark the definition, if it exists.  */
22485       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22486 	prune_unused_types_mark (die->die_definition, 1);
22487     }
22488 
22489   if (dokids && die->die_mark != 2)
22490     {
22491       /* We need to walk the children, but haven't done so yet.
22492 	 Remember that we've walked the kids.  */
22493       die->die_mark = 2;
22494 
22495       /* If this is an array type, we need to make sure our
22496 	 kids get marked, even if they're types.  If we're
22497 	 breaking out types into comdat sections, do this
22498 	 for all type definitions.  */
22499       if (die->die_tag == DW_TAG_array_type
22500           || (use_debug_types
22501               && is_type_die (die) && ! is_declaration_die (die)))
22502 	FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22503       else
22504 	FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22505     }
22506 }
22507 
22508 /* For local classes, look if any static member functions were emitted
22509    and if so, mark them.  */
22510 
22511 static void
prune_unused_types_walk_local_classes(dw_die_ref die)22512 prune_unused_types_walk_local_classes (dw_die_ref die)
22513 {
22514   dw_die_ref c;
22515 
22516   if (die->die_mark == 2)
22517     return;
22518 
22519   switch (die->die_tag)
22520     {
22521     case DW_TAG_structure_type:
22522     case DW_TAG_union_type:
22523     case DW_TAG_class_type:
22524       break;
22525 
22526     case DW_TAG_subprogram:
22527       if (!get_AT_flag (die, DW_AT_declaration)
22528 	  || die->die_definition != NULL)
22529 	prune_unused_types_mark (die, 1);
22530       return;
22531 
22532     default:
22533       return;
22534     }
22535 
22536   /* Mark children.  */
22537   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22538 }
22539 
22540 /* Walk the tree DIE and mark types that we actually use.  */
22541 
22542 static void
prune_unused_types_walk(dw_die_ref die)22543 prune_unused_types_walk (dw_die_ref die)
22544 {
22545   dw_die_ref c;
22546 
22547   /* Don't do anything if this node is already marked and
22548      children have been marked as well.  */
22549   if (die->die_mark == 2)
22550     return;
22551 
22552   switch (die->die_tag)
22553     {
22554     case DW_TAG_structure_type:
22555     case DW_TAG_union_type:
22556     case DW_TAG_class_type:
22557       if (die->die_perennial_p)
22558 	break;
22559 
22560       for (c = die->die_parent; c; c = c->die_parent)
22561 	if (c->die_tag == DW_TAG_subprogram)
22562 	  break;
22563 
22564       /* Finding used static member functions inside of classes
22565 	 is needed just for local classes, because for other classes
22566 	 static member function DIEs with DW_AT_specification
22567 	 are emitted outside of the DW_TAG_*_type.  If we ever change
22568 	 it, we'd need to call this even for non-local classes.  */
22569       if (c)
22570 	prune_unused_types_walk_local_classes (die);
22571 
22572       /* It's a type node --- don't mark it.  */
22573       return;
22574 
22575     case DW_TAG_const_type:
22576     case DW_TAG_packed_type:
22577     case DW_TAG_pointer_type:
22578     case DW_TAG_reference_type:
22579     case DW_TAG_rvalue_reference_type:
22580     case DW_TAG_volatile_type:
22581     case DW_TAG_typedef:
22582     case DW_TAG_array_type:
22583     case DW_TAG_interface_type:
22584     case DW_TAG_friend:
22585     case DW_TAG_variant_part:
22586     case DW_TAG_enumeration_type:
22587     case DW_TAG_subroutine_type:
22588     case DW_TAG_string_type:
22589     case DW_TAG_set_type:
22590     case DW_TAG_subrange_type:
22591     case DW_TAG_ptr_to_member_type:
22592     case DW_TAG_file_type:
22593       if (die->die_perennial_p)
22594 	break;
22595 
22596       /* It's a type node --- don't mark it.  */
22597       return;
22598 
22599     default:
22600       /* Mark everything else.  */
22601       break;
22602   }
22603 
22604   if (die->die_mark == 0)
22605     {
22606       die->die_mark = 1;
22607 
22608       /* Now, mark any dies referenced from here.  */
22609       prune_unused_types_walk_attribs (die);
22610     }
22611 
22612   die->die_mark = 2;
22613 
22614   /* Mark children.  */
22615   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22616 }
22617 
22618 /* Increment the string counts on strings referred to from DIE's
22619    attributes.  */
22620 
22621 static void
prune_unused_types_update_strings(dw_die_ref die)22622 prune_unused_types_update_strings (dw_die_ref die)
22623 {
22624   dw_attr_ref a;
22625   unsigned ix;
22626 
22627   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22628     if (AT_class (a) == dw_val_class_str)
22629       {
22630 	struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22631 	s->refcount++;
22632 	/* Avoid unnecessarily putting strings that are used less than
22633 	   twice in the hash table.  */
22634 	if (s->refcount
22635 	    == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22636 	  {
22637 	    void ** slot;
22638 	    slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22639 					     htab_hash_string (s->str),
22640 					     INSERT);
22641 	    gcc_assert (*slot == NULL);
22642 	    *slot = s;
22643 	  }
22644       }
22645 }
22646 
22647 /* Remove from the tree DIE any dies that aren't marked.  */
22648 
22649 static void
prune_unused_types_prune(dw_die_ref die)22650 prune_unused_types_prune (dw_die_ref die)
22651 {
22652   dw_die_ref c;
22653 
22654   gcc_assert (die->die_mark);
22655   prune_unused_types_update_strings (die);
22656 
22657   if (! die->die_child)
22658     return;
22659 
22660   c = die->die_child;
22661   do {
22662     dw_die_ref prev = c;
22663     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22664       if (c == die->die_child)
22665 	{
22666 	  /* No marked children between 'prev' and the end of the list.  */
22667 	  if (prev == c)
22668 	    /* No marked children at all.  */
22669 	    die->die_child = NULL;
22670 	  else
22671 	    {
22672 	      prev->die_sib = c->die_sib;
22673 	      die->die_child = prev;
22674 	    }
22675 	  return;
22676 	}
22677 
22678     if (c != prev->die_sib)
22679       prev->die_sib = c;
22680     prune_unused_types_prune (c);
22681   } while (c != die->die_child);
22682 }
22683 
22684 /* Remove dies representing declarations that we never use.  */
22685 
22686 static void
prune_unused_types(void)22687 prune_unused_types (void)
22688 {
22689   unsigned int i;
22690   limbo_die_node *node;
22691   comdat_type_node *ctnode;
22692   pubname_ref pub;
22693   dw_die_ref base_type;
22694 
22695 #if ENABLE_ASSERT_CHECKING
22696   /* All the marks should already be clear.  */
22697   verify_marks_clear (comp_unit_die ());
22698   for (node = limbo_die_list; node; node = node->next)
22699     verify_marks_clear (node->die);
22700   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22701     verify_marks_clear (ctnode->root_die);
22702 #endif /* ENABLE_ASSERT_CHECKING */
22703 
22704   /* Mark types that are used in global variables.  */
22705   premark_types_used_by_global_vars ();
22706 
22707   /* Set the mark on nodes that are actually used.  */
22708   prune_unused_types_walk (comp_unit_die ());
22709   for (node = limbo_die_list; node; node = node->next)
22710     prune_unused_types_walk (node->die);
22711   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22712     {
22713       prune_unused_types_walk (ctnode->root_die);
22714       prune_unused_types_mark (ctnode->type_die, 1);
22715     }
22716 
22717   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
22718      are unusual in that they are pubnames that are the children of pubtypes.
22719      They should only be marked via their parent DW_TAG_enumeration_type die,
22720      not as roots in themselves.  */
22721   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22722     if (pub->die->die_tag != DW_TAG_enumerator)
22723       prune_unused_types_mark (pub->die, 1);
22724   for (i = 0; base_types.iterate (i, &base_type); i++)
22725     prune_unused_types_mark (base_type, 1);
22726 
22727   if (debug_str_hash)
22728     htab_empty (debug_str_hash);
22729   if (skeleton_debug_str_hash)
22730     htab_empty (skeleton_debug_str_hash);
22731   prune_unused_types_prune (comp_unit_die ());
22732   for (node = limbo_die_list; node; node = node->next)
22733     prune_unused_types_prune (node->die);
22734   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22735     prune_unused_types_prune (ctnode->root_die);
22736 
22737   /* Leave the marks clear.  */
22738   prune_unmark_dies (comp_unit_die ());
22739   for (node = limbo_die_list; node; node = node->next)
22740     prune_unmark_dies (node->die);
22741   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22742     prune_unmark_dies (ctnode->root_die);
22743 }
22744 
22745 /* Set the parameter to true if there are any relative pathnames in
22746    the file table.  */
22747 static int
file_table_relative_p(void ** slot,void * param)22748 file_table_relative_p (void ** slot, void *param)
22749 {
22750   bool *p = (bool *) param;
22751   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22752   if (!IS_ABSOLUTE_PATH (d->filename))
22753     {
22754       *p = true;
22755       return 0;
22756     }
22757   return 1;
22758 }
22759 
22760 /* Helpers to manipulate hash table of comdat type units.  */
22761 
22762 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22763 {
22764   typedef comdat_type_node value_type;
22765   typedef comdat_type_node compare_type;
22766   static inline hashval_t hash (const value_type *);
22767   static inline bool equal (const value_type *, const compare_type *);
22768 };
22769 
22770 inline hashval_t
hash(const value_type * type_node)22771 comdat_type_hasher::hash (const value_type *type_node)
22772 {
22773   hashval_t h;
22774   memcpy (&h, type_node->signature, sizeof (h));
22775   return h;
22776 }
22777 
22778 inline bool
equal(const value_type * type_node_1,const compare_type * type_node_2)22779 comdat_type_hasher::equal (const value_type *type_node_1,
22780 			   const compare_type *type_node_2)
22781 {
22782   return (! memcmp (type_node_1->signature, type_node_2->signature,
22783                     DWARF_TYPE_SIGNATURE_SIZE));
22784 }
22785 
22786 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22787    to the location it would have been added, should we know its
22788    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22789    probably improve compactness of debug info, removing equivalent
22790    abbrevs, and hide any differences caused by deferring the
22791    computation of the assembler name, triggered by e.g. PCH.  */
22792 
22793 static inline void
move_linkage_attr(dw_die_ref die)22794 move_linkage_attr (dw_die_ref die)
22795 {
22796   unsigned ix = vec_safe_length (die->die_attr);
22797   dw_attr_node linkage = (*die->die_attr)[ix - 1];
22798 
22799   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22800 	      || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22801 
22802   while (--ix > 0)
22803     {
22804       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22805 
22806       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22807 	break;
22808     }
22809 
22810   if (ix != vec_safe_length (die->die_attr) - 1)
22811     {
22812       die->die_attr->pop ();
22813       die->die_attr->quick_insert (ix, linkage);
22814     }
22815 }
22816 
22817 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22818    referenced from typed stack ops and count how often they are used.  */
22819 
22820 static void
mark_base_types(dw_loc_descr_ref loc)22821 mark_base_types (dw_loc_descr_ref loc)
22822 {
22823   dw_die_ref base_type = NULL;
22824 
22825   for (; loc; loc = loc->dw_loc_next)
22826     {
22827       switch (loc->dw_loc_opc)
22828 	{
22829 	case DW_OP_GNU_regval_type:
22830 	case DW_OP_GNU_deref_type:
22831 	  base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22832 	  break;
22833 	case DW_OP_GNU_convert:
22834 	case DW_OP_GNU_reinterpret:
22835 	  if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22836 	    continue;
22837 	  /* FALLTHRU */
22838 	case DW_OP_GNU_const_type:
22839 	  base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22840 	  break;
22841 	case DW_OP_GNU_entry_value:
22842 	  mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22843 	  continue;
22844 	default:
22845 	  continue;
22846 	}
22847       gcc_assert (base_type->die_parent == comp_unit_die ());
22848       if (base_type->die_mark)
22849 	base_type->die_mark++;
22850       else
22851 	{
22852 	  base_types.safe_push (base_type);
22853 	  base_type->die_mark = 1;
22854 	}
22855     }
22856 }
22857 
22858 /* Comparison function for sorting marked base types.  */
22859 
22860 static int
base_type_cmp(const void * x,const void * y)22861 base_type_cmp (const void *x, const void *y)
22862 {
22863   dw_die_ref dx = *(const dw_die_ref *) x;
22864   dw_die_ref dy = *(const dw_die_ref *) y;
22865   unsigned int byte_size1, byte_size2;
22866   unsigned int encoding1, encoding2;
22867   if (dx->die_mark > dy->die_mark)
22868     return -1;
22869   if (dx->die_mark < dy->die_mark)
22870     return 1;
22871   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22872   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22873   if (byte_size1 < byte_size2)
22874     return 1;
22875   if (byte_size1 > byte_size2)
22876     return -1;
22877   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22878   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22879   if (encoding1 < encoding2)
22880     return 1;
22881   if (encoding1 > encoding2)
22882     return -1;
22883   return 0;
22884 }
22885 
22886 /* Move base types marked by mark_base_types as early as possible
22887    in the CU, sorted by decreasing usage count both to make the
22888    uleb128 references as small as possible and to make sure they
22889    will have die_offset already computed by calc_die_sizes when
22890    sizes of typed stack loc ops is computed.  */
22891 
22892 static void
move_marked_base_types(void)22893 move_marked_base_types (void)
22894 {
22895   unsigned int i;
22896   dw_die_ref base_type, die, c;
22897 
22898   if (base_types.is_empty ())
22899     return;
22900 
22901   /* Sort by decreasing usage count, they will be added again in that
22902      order later on.  */
22903   base_types.qsort (base_type_cmp);
22904   die = comp_unit_die ();
22905   c = die->die_child;
22906   do
22907     {
22908       dw_die_ref prev = c;
22909       c = c->die_sib;
22910       while (c->die_mark)
22911 	{
22912 	  remove_child_with_prev (c, prev);
22913 	  /* As base types got marked, there must be at least
22914 	     one node other than DW_TAG_base_type.  */
22915 	  gcc_assert (c != c->die_sib);
22916 	  c = c->die_sib;
22917 	}
22918     }
22919   while (c != die->die_child);
22920   gcc_assert (die->die_child);
22921   c = die->die_child;
22922   for (i = 0; base_types.iterate (i, &base_type); i++)
22923     {
22924       base_type->die_mark = 0;
22925       base_type->die_sib = c->die_sib;
22926       c->die_sib = base_type;
22927       c = base_type;
22928     }
22929 }
22930 
22931 /* Helper function for resolve_addr, attempt to resolve
22932    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22933    SYMBOL_REFs refer to variables emitted in the current CU.  */
22934 
22935 static int
resolve_one_addr(rtx * addr,void * data ATTRIBUTE_UNUSED)22936 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22937 {
22938   rtx rtl = *addr;
22939 
22940   if (GET_CODE (rtl) == CONST_STRING)
22941     {
22942       size_t len = strlen (XSTR (rtl, 0)) + 1;
22943       tree t = build_string (len, XSTR (rtl, 0));
22944       tree tlen = size_int (len - 1);
22945       TREE_TYPE (t)
22946 	= build_array_type (char_type_node, build_index_type (tlen));
22947       rtl = lookup_constant_def (t);
22948       if (!rtl || !MEM_P (rtl))
22949 	return 1;
22950       rtl = XEXP (rtl, 0);
22951       if (GET_CODE (rtl) == SYMBOL_REF
22952 	  && SYMBOL_REF_DECL (rtl)
22953 	  && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22954 	return 1;
22955       vec_safe_push (used_rtx_array, rtl);
22956       *addr = rtl;
22957       return 0;
22958     }
22959 
22960   if (GET_CODE (rtl) == SYMBOL_REF
22961       && SYMBOL_REF_DECL (rtl))
22962     {
22963       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22964 	{
22965 	  if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22966 	    return 1;
22967 	}
22968       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22969 	return 1;
22970     }
22971 
22972   if (GET_CODE (rtl) == CONST
22973       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22974     return 1;
22975 
22976   return 0;
22977 }
22978 
22979 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22980    if possible, and create DW_TAG_dwarf_procedure that can be referenced
22981    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
22982 
22983 static rtx
string_cst_pool_decl(tree t)22984 string_cst_pool_decl (tree t)
22985 {
22986   rtx rtl = output_constant_def (t, 1);
22987   unsigned char *array;
22988   dw_loc_descr_ref l;
22989   tree decl;
22990   size_t len;
22991   dw_die_ref ref;
22992 
22993   if (!rtl || !MEM_P (rtl))
22994     return NULL_RTX;
22995   rtl = XEXP (rtl, 0);
22996   if (GET_CODE (rtl) != SYMBOL_REF
22997       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22998     return NULL_RTX;
22999 
23000   decl = SYMBOL_REF_DECL (rtl);
23001   if (!lookup_decl_die (decl))
23002     {
23003       len = TREE_STRING_LENGTH (t);
23004       vec_safe_push (used_rtx_array, rtl);
23005       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23006       array = (unsigned char *) ggc_alloc_atomic (len);
23007       memcpy (array, TREE_STRING_POINTER (t), len);
23008       l = new_loc_descr (DW_OP_implicit_value, len, 0);
23009       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23010       l->dw_loc_oprnd2.v.val_vec.length = len;
23011       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23012       l->dw_loc_oprnd2.v.val_vec.array = array;
23013       add_AT_loc (ref, DW_AT_location, l);
23014       equate_decl_number_to_die (decl, ref);
23015     }
23016   return rtl;
23017 }
23018 
23019 /* Helper function of resolve_addr_in_expr.  LOC is
23020    a DW_OP_addr followed by DW_OP_stack_value, either at the start
23021    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23022    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
23023    with DW_OP_GNU_implicit_pointer if possible
23024    and return true, if unsuccessful, return false.  */
23025 
23026 static bool
optimize_one_addr_into_implicit_ptr(dw_loc_descr_ref loc)23027 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23028 {
23029   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23030   HOST_WIDE_INT offset = 0;
23031   dw_die_ref ref = NULL;
23032   tree decl;
23033 
23034   if (GET_CODE (rtl) == CONST
23035       && GET_CODE (XEXP (rtl, 0)) == PLUS
23036       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23037     {
23038       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23039       rtl = XEXP (XEXP (rtl, 0), 0);
23040     }
23041   if (GET_CODE (rtl) == CONST_STRING)
23042     {
23043       size_t len = strlen (XSTR (rtl, 0)) + 1;
23044       tree t = build_string (len, XSTR (rtl, 0));
23045       tree tlen = size_int (len - 1);
23046 
23047       TREE_TYPE (t)
23048 	= build_array_type (char_type_node, build_index_type (tlen));
23049       rtl = string_cst_pool_decl (t);
23050       if (!rtl)
23051 	return false;
23052     }
23053   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23054     {
23055       decl = SYMBOL_REF_DECL (rtl);
23056       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23057 	{
23058 	  ref = lookup_decl_die (decl);
23059 	  if (ref && (get_AT (ref, DW_AT_location)
23060 		      || get_AT (ref, DW_AT_const_value)))
23061 	    {
23062 	      loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23063 	      loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23064 	      loc->dw_loc_oprnd1.val_entry = NULL;
23065 	      loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23066 	      loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23067 	      loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23068 	      loc->dw_loc_oprnd2.v.val_int = offset;
23069 	      return true;
23070 	    }
23071 	}
23072     }
23073   return false;
23074 }
23075 
23076 /* Helper function for resolve_addr, handle one location
23077    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23078    the location list couldn't be resolved.  */
23079 
23080 static bool
resolve_addr_in_expr(dw_loc_descr_ref loc)23081 resolve_addr_in_expr (dw_loc_descr_ref loc)
23082 {
23083   dw_loc_descr_ref keep = NULL;
23084   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23085     switch (loc->dw_loc_opc)
23086       {
23087       case DW_OP_addr:
23088 	if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23089 	  {
23090 	    if ((prev == NULL
23091 		 || prev->dw_loc_opc == DW_OP_piece
23092 		 || prev->dw_loc_opc == DW_OP_bit_piece)
23093 		&& loc->dw_loc_next
23094 		&& loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23095 		&& !dwarf_strict
23096 		&& optimize_one_addr_into_implicit_ptr (loc))
23097 	      break;
23098 	    return false;
23099 	  }
23100 	break;
23101       case DW_OP_GNU_addr_index:
23102       case DW_OP_GNU_const_index:
23103 	if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23104             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23105           {
23106             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23107             if (resolve_one_addr (&rtl, NULL))
23108               return false;
23109             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23110             loc->dw_loc_oprnd1.val_entry =
23111                 add_addr_table_entry (rtl, ate_kind_rtx);
23112           }
23113 	break;
23114       case DW_OP_const4u:
23115       case DW_OP_const8u:
23116 	if (loc->dtprel
23117 	    && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23118 	  return false;
23119 	break;
23120       case DW_OP_plus_uconst:
23121 	if (size_of_loc_descr (loc)
23122 	    > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23123 	      + 1
23124 	    && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23125 	  {
23126 	    dw_loc_descr_ref repl
23127 	      = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23128 	    add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23129 	    add_loc_descr (&repl, loc->dw_loc_next);
23130 	    *loc = *repl;
23131 	  }
23132 	break;
23133       case DW_OP_implicit_value:
23134 	if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23135 	    && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23136 	  return false;
23137 	break;
23138       case DW_OP_GNU_implicit_pointer:
23139       case DW_OP_GNU_parameter_ref:
23140 	if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23141 	  {
23142 	    dw_die_ref ref
23143 	      = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23144 	    if (ref == NULL)
23145 	      return false;
23146 	    loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23147 	    loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23148 	    loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23149 	  }
23150 	break;
23151       case DW_OP_GNU_const_type:
23152       case DW_OP_GNU_regval_type:
23153       case DW_OP_GNU_deref_type:
23154       case DW_OP_GNU_convert:
23155       case DW_OP_GNU_reinterpret:
23156 	while (loc->dw_loc_next
23157 	       && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23158 	  {
23159 	    dw_die_ref base1, base2;
23160 	    unsigned enc1, enc2, size1, size2;
23161 	    if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23162 		|| loc->dw_loc_opc == DW_OP_GNU_deref_type)
23163 	      base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23164 	    else if (loc->dw_loc_oprnd1.val_class
23165 		     == dw_val_class_unsigned_const)
23166 	      break;
23167 	    else
23168 	      base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23169 	    if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23170 		== dw_val_class_unsigned_const)
23171 	      break;
23172 	    base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23173 	    gcc_assert (base1->die_tag == DW_TAG_base_type
23174 			&& base2->die_tag == DW_TAG_base_type);
23175 	    enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23176 	    enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23177 	    size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23178 	    size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23179 	    if (size1 == size2
23180 		&& (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23181 		     && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23182 		     && loc != keep)
23183 		    || enc1 == enc2))
23184 	      {
23185 		/* Optimize away next DW_OP_GNU_convert after
23186 		   adjusting LOC's base type die reference.  */
23187 		if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23188 		    || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23189 		  loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23190 		else
23191 		  loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23192 		loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23193 		continue;
23194 	      }
23195 	    /* Don't change integer DW_OP_GNU_convert after e.g. floating
23196 	       point typed stack entry.  */
23197 	    else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23198 	      keep = loc->dw_loc_next;
23199 	    break;
23200 	  }
23201 	break;
23202       default:
23203 	break;
23204       }
23205   return true;
23206 }
23207 
23208 /* Helper function of resolve_addr.  DIE had DW_AT_location of
23209    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23210    and DW_OP_addr couldn't be resolved.  resolve_addr has already
23211    removed the DW_AT_location attribute.  This function attempts to
23212    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23213    to it or DW_AT_const_value attribute, if possible.  */
23214 
23215 static void
optimize_location_into_implicit_ptr(dw_die_ref die,tree decl)23216 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23217 {
23218   if (TREE_CODE (decl) != VAR_DECL
23219       || lookup_decl_die (decl) != die
23220       || DECL_EXTERNAL (decl)
23221       || !TREE_STATIC (decl)
23222       || DECL_INITIAL (decl) == NULL_TREE
23223       || DECL_P (DECL_INITIAL (decl))
23224       || get_AT (die, DW_AT_const_value))
23225     return;
23226 
23227   tree init = DECL_INITIAL (decl);
23228   HOST_WIDE_INT offset = 0;
23229   /* For variables that have been optimized away and thus
23230      don't have a memory location, see if we can emit
23231      DW_AT_const_value instead.  */
23232   if (tree_add_const_value_attribute (die, init))
23233     return;
23234   if (dwarf_strict)
23235     return;
23236   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23237      and ADDR_EXPR refers to a decl that has DW_AT_location or
23238      DW_AT_const_value (but isn't addressable, otherwise
23239      resolving the original DW_OP_addr wouldn't fail), see if
23240      we can add DW_OP_GNU_implicit_pointer.  */
23241   STRIP_NOPS (init);
23242   if (TREE_CODE (init) == POINTER_PLUS_EXPR
23243       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23244     {
23245       offset = tree_to_shwi (TREE_OPERAND (init, 1));
23246       init = TREE_OPERAND (init, 0);
23247       STRIP_NOPS (init);
23248     }
23249   if (TREE_CODE (init) != ADDR_EXPR)
23250     return;
23251   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23252        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23253       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23254 	  && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23255 	  && TREE_OPERAND (init, 0) != decl))
23256     {
23257       dw_die_ref ref;
23258       dw_loc_descr_ref l;
23259 
23260       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23261 	{
23262 	  rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23263 	  if (!rtl)
23264 	    return;
23265 	  decl = SYMBOL_REF_DECL (rtl);
23266 	}
23267       else
23268 	decl = TREE_OPERAND (init, 0);
23269       ref = lookup_decl_die (decl);
23270       if (ref == NULL
23271 	  || (!get_AT (ref, DW_AT_location)
23272 	      && !get_AT (ref, DW_AT_const_value)))
23273 	return;
23274       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23275       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23276       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23277       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23278       add_AT_loc (die, DW_AT_location, l);
23279     }
23280 }
23281 
23282 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23283    an address in .rodata section if the string literal is emitted there,
23284    or remove the containing location list or replace DW_AT_const_value
23285    with DW_AT_location and empty location expression, if it isn't found
23286    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23287    to something that has been emitted in the current CU.  */
23288 
23289 static void
resolve_addr(dw_die_ref die)23290 resolve_addr (dw_die_ref die)
23291 {
23292   dw_die_ref c;
23293   dw_attr_ref a;
23294   dw_loc_list_ref *curr, *start, loc;
23295   unsigned ix;
23296 
23297   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23298     switch (AT_class (a))
23299       {
23300       case dw_val_class_loc_list:
23301 	start = curr = AT_loc_list_ptr (a);
23302 	loc = *curr;
23303 	gcc_assert (loc);
23304 	/* The same list can be referenced more than once.  See if we have
23305 	   already recorded the result from a previous pass.  */
23306 	if (loc->replaced)
23307 	  *curr = loc->dw_loc_next;
23308 	else if (!loc->resolved_addr)
23309 	  {
23310 	    /* As things stand, we do not expect or allow one die to
23311 	       reference a suffix of another die's location list chain.
23312 	       References must be identical or completely separate.
23313 	       There is therefore no need to cache the result of this
23314 	       pass on any list other than the first; doing so
23315 	       would lead to unnecessary writes.  */
23316 	    while (*curr)
23317 	      {
23318 		gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23319 		if (!resolve_addr_in_expr ((*curr)->expr))
23320 		  {
23321 		    dw_loc_list_ref next = (*curr)->dw_loc_next;
23322                     dw_loc_descr_ref l = (*curr)->expr;
23323 
23324 		    if (next && (*curr)->ll_symbol)
23325 		      {
23326 			gcc_assert (!next->ll_symbol);
23327 			next->ll_symbol = (*curr)->ll_symbol;
23328 		      }
23329                     if (dwarf_split_debug_info)
23330                       remove_loc_list_addr_table_entries (l);
23331 		    *curr = next;
23332 		  }
23333 		else
23334 		  {
23335 		    mark_base_types ((*curr)->expr);
23336 		    curr = &(*curr)->dw_loc_next;
23337 		  }
23338 	      }
23339 	    if (loc == *start)
23340 	      loc->resolved_addr = 1;
23341 	    else
23342 	      {
23343 		loc->replaced = 1;
23344 		loc->dw_loc_next = *start;
23345 	      }
23346 	  }
23347 	if (!*start)
23348 	  {
23349 	    remove_AT (die, a->dw_attr);
23350 	    ix--;
23351 	  }
23352 	break;
23353       case dw_val_class_loc:
23354 	{
23355 	  dw_loc_descr_ref l = AT_loc (a);
23356 	  /* For -gdwarf-2 don't attempt to optimize
23357 	     DW_AT_data_member_location containing
23358 	     DW_OP_plus_uconst - older consumers might
23359 	     rely on it being that op instead of a more complex,
23360 	     but shorter, location description.  */
23361 	  if ((dwarf_version > 2
23362 	       || a->dw_attr != DW_AT_data_member_location
23363 	       || l == NULL
23364 	       || l->dw_loc_opc != DW_OP_plus_uconst
23365 	       || l->dw_loc_next != NULL)
23366 	      && !resolve_addr_in_expr (l))
23367 	    {
23368 	      if (dwarf_split_debug_info)
23369 		remove_loc_list_addr_table_entries (l);
23370 	      if (l != NULL
23371 		  && l->dw_loc_next == NULL
23372 		  && l->dw_loc_opc == DW_OP_addr
23373 		  && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23374 		  && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23375 		  && a->dw_attr == DW_AT_location)
23376 		{
23377 		  tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23378 		  remove_AT (die, a->dw_attr);
23379 		  ix--;
23380 		  optimize_location_into_implicit_ptr (die, decl);
23381 		  break;
23382 		}
23383 	      remove_AT (die, a->dw_attr);
23384 	      ix--;
23385 	    }
23386 	  else
23387 	    mark_base_types (l);
23388 	}
23389 	break;
23390       case dw_val_class_addr:
23391 	if (a->dw_attr == DW_AT_const_value
23392 	    && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23393 	  {
23394             if (AT_index (a) != NOT_INDEXED)
23395               remove_addr_table_entry (a->dw_attr_val.val_entry);
23396 	    remove_AT (die, a->dw_attr);
23397 	    ix--;
23398 	  }
23399 	if (die->die_tag == DW_TAG_GNU_call_site
23400 	    && a->dw_attr == DW_AT_abstract_origin)
23401 	  {
23402 	    tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23403 	    dw_die_ref tdie = lookup_decl_die (tdecl);
23404 	    if (tdie == NULL
23405 		&& DECL_EXTERNAL (tdecl)
23406 		&& DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23407 	      {
23408 		force_decl_die (tdecl);
23409 		tdie = lookup_decl_die (tdecl);
23410 	      }
23411 	    if (tdie)
23412 	      {
23413 		a->dw_attr_val.val_class = dw_val_class_die_ref;
23414 		a->dw_attr_val.v.val_die_ref.die = tdie;
23415 		a->dw_attr_val.v.val_die_ref.external = 0;
23416 	      }
23417 	    else
23418 	      {
23419                 if (AT_index (a) != NOT_INDEXED)
23420                   remove_addr_table_entry (a->dw_attr_val.val_entry);
23421 		remove_AT (die, a->dw_attr);
23422 		ix--;
23423 	      }
23424 	  }
23425 	break;
23426       default:
23427 	break;
23428       }
23429 
23430   FOR_EACH_CHILD (die, c, resolve_addr (c));
23431 }
23432 
23433 /* Helper routines for optimize_location_lists.
23434    This pass tries to share identical local lists in .debug_loc
23435    section.  */
23436 
23437 /* Iteratively hash operands of LOC opcode.  */
23438 
23439 static hashval_t
hash_loc_operands(dw_loc_descr_ref loc,hashval_t hash)23440 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23441 {
23442   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23443   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23444 
23445   switch (loc->dw_loc_opc)
23446     {
23447     case DW_OP_const4u:
23448     case DW_OP_const8u:
23449       if (loc->dtprel)
23450 	goto hash_addr;
23451       /* FALLTHRU */
23452     case DW_OP_const1u:
23453     case DW_OP_const1s:
23454     case DW_OP_const2u:
23455     case DW_OP_const2s:
23456     case DW_OP_const4s:
23457     case DW_OP_const8s:
23458     case DW_OP_constu:
23459     case DW_OP_consts:
23460     case DW_OP_pick:
23461     case DW_OP_plus_uconst:
23462     case DW_OP_breg0:
23463     case DW_OP_breg1:
23464     case DW_OP_breg2:
23465     case DW_OP_breg3:
23466     case DW_OP_breg4:
23467     case DW_OP_breg5:
23468     case DW_OP_breg6:
23469     case DW_OP_breg7:
23470     case DW_OP_breg8:
23471     case DW_OP_breg9:
23472     case DW_OP_breg10:
23473     case DW_OP_breg11:
23474     case DW_OP_breg12:
23475     case DW_OP_breg13:
23476     case DW_OP_breg14:
23477     case DW_OP_breg15:
23478     case DW_OP_breg16:
23479     case DW_OP_breg17:
23480     case DW_OP_breg18:
23481     case DW_OP_breg19:
23482     case DW_OP_breg20:
23483     case DW_OP_breg21:
23484     case DW_OP_breg22:
23485     case DW_OP_breg23:
23486     case DW_OP_breg24:
23487     case DW_OP_breg25:
23488     case DW_OP_breg26:
23489     case DW_OP_breg27:
23490     case DW_OP_breg28:
23491     case DW_OP_breg29:
23492     case DW_OP_breg30:
23493     case DW_OP_breg31:
23494     case DW_OP_regx:
23495     case DW_OP_fbreg:
23496     case DW_OP_piece:
23497     case DW_OP_deref_size:
23498     case DW_OP_xderef_size:
23499       hash = iterative_hash_object (val1->v.val_int, hash);
23500       break;
23501     case DW_OP_skip:
23502     case DW_OP_bra:
23503       {
23504 	int offset;
23505 
23506 	gcc_assert (val1->val_class == dw_val_class_loc);
23507 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23508 	hash = iterative_hash_object (offset, hash);
23509       }
23510       break;
23511     case DW_OP_implicit_value:
23512       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23513       switch (val2->val_class)
23514 	{
23515 	case dw_val_class_const:
23516 	  hash = iterative_hash_object (val2->v.val_int, hash);
23517 	  break;
23518 	case dw_val_class_vec:
23519 	  {
23520 	    unsigned int elt_size = val2->v.val_vec.elt_size;
23521 	    unsigned int len = val2->v.val_vec.length;
23522 
23523 	    hash = iterative_hash_object (elt_size, hash);
23524 	    hash = iterative_hash_object (len, hash);
23525 	    hash = iterative_hash (val2->v.val_vec.array,
23526 				   len * elt_size, hash);
23527 	  }
23528 	  break;
23529 	case dw_val_class_const_double:
23530 	  hash = iterative_hash_object (val2->v.val_double.low, hash);
23531 	  hash = iterative_hash_object (val2->v.val_double.high, hash);
23532 	  break;
23533 	case dw_val_class_addr:
23534 	  hash = iterative_hash_rtx (val2->v.val_addr, hash);
23535 	  break;
23536 	default:
23537 	  gcc_unreachable ();
23538 	}
23539       break;
23540     case DW_OP_bregx:
23541     case DW_OP_bit_piece:
23542       hash = iterative_hash_object (val1->v.val_int, hash);
23543       hash = iterative_hash_object (val2->v.val_int, hash);
23544       break;
23545     case DW_OP_addr:
23546     hash_addr:
23547       if (loc->dtprel)
23548 	{
23549 	  unsigned char dtprel = 0xd1;
23550 	  hash = iterative_hash_object (dtprel, hash);
23551 	}
23552       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23553       break;
23554     case DW_OP_GNU_addr_index:
23555     case DW_OP_GNU_const_index:
23556       {
23557         if (loc->dtprel)
23558           {
23559             unsigned char dtprel = 0xd1;
23560             hash = iterative_hash_object (dtprel, hash);
23561           }
23562         hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23563       }
23564       break;
23565     case DW_OP_GNU_implicit_pointer:
23566       hash = iterative_hash_object (val2->v.val_int, hash);
23567       break;
23568     case DW_OP_GNU_entry_value:
23569       hash = hash_loc_operands (val1->v.val_loc, hash);
23570       break;
23571     case DW_OP_GNU_regval_type:
23572     case DW_OP_GNU_deref_type:
23573       {
23574 	unsigned int byte_size
23575 	  = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23576 	unsigned int encoding
23577 	  = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23578 	hash = iterative_hash_object (val1->v.val_int, hash);
23579 	hash = iterative_hash_object (byte_size, hash);
23580 	hash = iterative_hash_object (encoding, hash);
23581       }
23582       break;
23583     case DW_OP_GNU_convert:
23584     case DW_OP_GNU_reinterpret:
23585       if (val1->val_class == dw_val_class_unsigned_const)
23586 	{
23587 	  hash = iterative_hash_object (val1->v.val_unsigned, hash);
23588 	  break;
23589 	}
23590       /* FALLTHRU */
23591     case DW_OP_GNU_const_type:
23592       {
23593 	unsigned int byte_size
23594 	  = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23595 	unsigned int encoding
23596 	  = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23597 	hash = iterative_hash_object (byte_size, hash);
23598 	hash = iterative_hash_object (encoding, hash);
23599 	if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23600 	  break;
23601 	hash = iterative_hash_object (val2->val_class, hash);
23602 	switch (val2->val_class)
23603 	  {
23604 	  case dw_val_class_const:
23605 	    hash = iterative_hash_object (val2->v.val_int, hash);
23606 	    break;
23607 	  case dw_val_class_vec:
23608 	    {
23609 	      unsigned int elt_size = val2->v.val_vec.elt_size;
23610 	      unsigned int len = val2->v.val_vec.length;
23611 
23612 	      hash = iterative_hash_object (elt_size, hash);
23613 	      hash = iterative_hash_object (len, hash);
23614 	      hash = iterative_hash (val2->v.val_vec.array,
23615 				     len * elt_size, hash);
23616 	    }
23617 	    break;
23618 	  case dw_val_class_const_double:
23619 	    hash = iterative_hash_object (val2->v.val_double.low, hash);
23620 	    hash = iterative_hash_object (val2->v.val_double.high, hash);
23621 	    break;
23622 	  default:
23623 	    gcc_unreachable ();
23624 	  }
23625       }
23626       break;
23627 
23628     default:
23629       /* Other codes have no operands.  */
23630       break;
23631     }
23632   return hash;
23633 }
23634 
23635 /* Iteratively hash the whole DWARF location expression LOC.  */
23636 
23637 static inline hashval_t
hash_locs(dw_loc_descr_ref loc,hashval_t hash)23638 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23639 {
23640   dw_loc_descr_ref l;
23641   bool sizes_computed = false;
23642   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23643   size_of_locs (loc);
23644 
23645   for (l = loc; l != NULL; l = l->dw_loc_next)
23646     {
23647       enum dwarf_location_atom opc = l->dw_loc_opc;
23648       hash = iterative_hash_object (opc, hash);
23649       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23650 	{
23651 	  size_of_locs (loc);
23652 	  sizes_computed = true;
23653 	}
23654       hash = hash_loc_operands (l, hash);
23655     }
23656   return hash;
23657 }
23658 
23659 /* Compute hash of the whole location list LIST_HEAD.  */
23660 
23661 static inline void
hash_loc_list(dw_loc_list_ref list_head)23662 hash_loc_list (dw_loc_list_ref list_head)
23663 {
23664   dw_loc_list_ref curr = list_head;
23665   hashval_t hash = 0;
23666 
23667   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23668     {
23669       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23670       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23671       if (curr->section)
23672 	hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23673 			       hash);
23674       hash = hash_locs (curr->expr, hash);
23675     }
23676   list_head->hash = hash;
23677 }
23678 
23679 /* Return true if X and Y opcodes have the same operands.  */
23680 
23681 static inline bool
compare_loc_operands(dw_loc_descr_ref x,dw_loc_descr_ref y)23682 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23683 {
23684   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23685   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23686   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23687   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23688 
23689   switch (x->dw_loc_opc)
23690     {
23691     case DW_OP_const4u:
23692     case DW_OP_const8u:
23693       if (x->dtprel)
23694 	goto hash_addr;
23695       /* FALLTHRU */
23696     case DW_OP_const1u:
23697     case DW_OP_const1s:
23698     case DW_OP_const2u:
23699     case DW_OP_const2s:
23700     case DW_OP_const4s:
23701     case DW_OP_const8s:
23702     case DW_OP_constu:
23703     case DW_OP_consts:
23704     case DW_OP_pick:
23705     case DW_OP_plus_uconst:
23706     case DW_OP_breg0:
23707     case DW_OP_breg1:
23708     case DW_OP_breg2:
23709     case DW_OP_breg3:
23710     case DW_OP_breg4:
23711     case DW_OP_breg5:
23712     case DW_OP_breg6:
23713     case DW_OP_breg7:
23714     case DW_OP_breg8:
23715     case DW_OP_breg9:
23716     case DW_OP_breg10:
23717     case DW_OP_breg11:
23718     case DW_OP_breg12:
23719     case DW_OP_breg13:
23720     case DW_OP_breg14:
23721     case DW_OP_breg15:
23722     case DW_OP_breg16:
23723     case DW_OP_breg17:
23724     case DW_OP_breg18:
23725     case DW_OP_breg19:
23726     case DW_OP_breg20:
23727     case DW_OP_breg21:
23728     case DW_OP_breg22:
23729     case DW_OP_breg23:
23730     case DW_OP_breg24:
23731     case DW_OP_breg25:
23732     case DW_OP_breg26:
23733     case DW_OP_breg27:
23734     case DW_OP_breg28:
23735     case DW_OP_breg29:
23736     case DW_OP_breg30:
23737     case DW_OP_breg31:
23738     case DW_OP_regx:
23739     case DW_OP_fbreg:
23740     case DW_OP_piece:
23741     case DW_OP_deref_size:
23742     case DW_OP_xderef_size:
23743       return valx1->v.val_int == valy1->v.val_int;
23744     case DW_OP_skip:
23745     case DW_OP_bra:
23746       /* If splitting debug info, the use of DW_OP_GNU_addr_index
23747         can cause irrelevant differences in dw_loc_addr.  */
23748       gcc_assert (valx1->val_class == dw_val_class_loc
23749 		  && valy1->val_class == dw_val_class_loc
23750                   && (dwarf_split_debug_info
23751                       || x->dw_loc_addr == y->dw_loc_addr));
23752       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23753     case DW_OP_implicit_value:
23754       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23755 	  || valx2->val_class != valy2->val_class)
23756 	return false;
23757       switch (valx2->val_class)
23758 	{
23759 	case dw_val_class_const:
23760 	  return valx2->v.val_int == valy2->v.val_int;
23761 	case dw_val_class_vec:
23762 	  return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23763 		 && valx2->v.val_vec.length == valy2->v.val_vec.length
23764 		 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23765 			    valx2->v.val_vec.elt_size
23766 			    * valx2->v.val_vec.length) == 0;
23767 	case dw_val_class_const_double:
23768 	  return valx2->v.val_double.low == valy2->v.val_double.low
23769 		 && valx2->v.val_double.high == valy2->v.val_double.high;
23770 	case dw_val_class_addr:
23771 	  return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23772 	default:
23773 	  gcc_unreachable ();
23774 	}
23775     case DW_OP_bregx:
23776     case DW_OP_bit_piece:
23777       return valx1->v.val_int == valy1->v.val_int
23778 	     && valx2->v.val_int == valy2->v.val_int;
23779     case DW_OP_addr:
23780     hash_addr:
23781       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23782     case DW_OP_GNU_addr_index:
23783     case DW_OP_GNU_const_index:
23784       {
23785         rtx ax1 = valx1->val_entry->addr.rtl;
23786         rtx ay1 = valy1->val_entry->addr.rtl;
23787         return rtx_equal_p (ax1, ay1);
23788       }
23789     case DW_OP_GNU_implicit_pointer:
23790       return valx1->val_class == dw_val_class_die_ref
23791 	     && valx1->val_class == valy1->val_class
23792 	     && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23793 	     && valx2->v.val_int == valy2->v.val_int;
23794     case DW_OP_GNU_entry_value:
23795       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23796     case DW_OP_GNU_const_type:
23797       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23798 	  || valx2->val_class != valy2->val_class)
23799 	return false;
23800       switch (valx2->val_class)
23801 	{
23802 	case dw_val_class_const:
23803 	  return valx2->v.val_int == valy2->v.val_int;
23804 	case dw_val_class_vec:
23805 	  return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23806 		 && valx2->v.val_vec.length == valy2->v.val_vec.length
23807 		 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23808 			    valx2->v.val_vec.elt_size
23809 			    * valx2->v.val_vec.length) == 0;
23810 	case dw_val_class_const_double:
23811 	  return valx2->v.val_double.low == valy2->v.val_double.low
23812 		 && valx2->v.val_double.high == valy2->v.val_double.high;
23813 	default:
23814 	  gcc_unreachable ();
23815 	}
23816     case DW_OP_GNU_regval_type:
23817     case DW_OP_GNU_deref_type:
23818       return valx1->v.val_int == valy1->v.val_int
23819 	     && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23820     case DW_OP_GNU_convert:
23821     case DW_OP_GNU_reinterpret:
23822       if (valx1->val_class != valy1->val_class)
23823 	return false;
23824       if (valx1->val_class == dw_val_class_unsigned_const)
23825 	return valx1->v.val_unsigned == valy1->v.val_unsigned;
23826       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23827     case DW_OP_GNU_parameter_ref:
23828       return valx1->val_class == dw_val_class_die_ref
23829 	     && valx1->val_class == valy1->val_class
23830 	     && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23831     default:
23832       /* Other codes have no operands.  */
23833       return true;
23834     }
23835 }
23836 
23837 /* Return true if DWARF location expressions X and Y are the same.  */
23838 
23839 static inline bool
compare_locs(dw_loc_descr_ref x,dw_loc_descr_ref y)23840 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23841 {
23842   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23843     if (x->dw_loc_opc != y->dw_loc_opc
23844 	|| x->dtprel != y->dtprel
23845 	|| !compare_loc_operands (x, y))
23846       break;
23847   return x == NULL && y == NULL;
23848 }
23849 
23850 /* Hashtable helpers.  */
23851 
23852 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23853 {
23854   typedef dw_loc_list_struct value_type;
23855   typedef dw_loc_list_struct compare_type;
23856   static inline hashval_t hash (const value_type *);
23857   static inline bool equal (const value_type *, const compare_type *);
23858 };
23859 
23860 /* Return precomputed hash of location list X.  */
23861 
23862 inline hashval_t
hash(const value_type * x)23863 loc_list_hasher::hash (const value_type *x)
23864 {
23865   return x->hash;
23866 }
23867 
23868 /* Return true if location lists A and B are the same.  */
23869 
23870 inline bool
equal(const value_type * a,const compare_type * b)23871 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23872 {
23873   if (a == b)
23874     return 1;
23875   if (a->hash != b->hash)
23876     return 0;
23877   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23878     if (strcmp (a->begin, b->begin) != 0
23879 	|| strcmp (a->end, b->end) != 0
23880 	|| (a->section == NULL) != (b->section == NULL)
23881 	|| (a->section && strcmp (a->section, b->section) != 0)
23882 	|| !compare_locs (a->expr, b->expr))
23883       break;
23884   return a == NULL && b == NULL;
23885 }
23886 
23887 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23888 
23889 
23890 /* Recursively optimize location lists referenced from DIE
23891    children and share them whenever possible.  */
23892 
23893 static void
optimize_location_lists_1(dw_die_ref die,loc_list_hash_type htab)23894 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23895 {
23896   dw_die_ref c;
23897   dw_attr_ref a;
23898   unsigned ix;
23899   dw_loc_list_struct **slot;
23900 
23901   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23902     if (AT_class (a) == dw_val_class_loc_list)
23903       {
23904 	dw_loc_list_ref list = AT_loc_list (a);
23905 	/* TODO: perform some optimizations here, before hashing
23906 	   it and storing into the hash table.  */
23907 	hash_loc_list (list);
23908 	slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23909 	if (*slot == NULL)
23910 	  *slot = list;
23911 	else
23912           a->dw_attr_val.v.val_loc_list = *slot;
23913       }
23914 
23915   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23916 }
23917 
23918 
23919 /* Recursively assign each location list a unique index into the debug_addr
23920    section.  */
23921 
23922 static void
index_location_lists(dw_die_ref die)23923 index_location_lists (dw_die_ref die)
23924 {
23925   dw_die_ref c;
23926   dw_attr_ref a;
23927   unsigned ix;
23928 
23929   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23930     if (AT_class (a) == dw_val_class_loc_list)
23931       {
23932         dw_loc_list_ref list = AT_loc_list (a);
23933         dw_loc_list_ref curr;
23934         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23935           {
23936             /* Don't index an entry that has already been indexed
23937                or won't be output.  */
23938             if (curr->begin_entry != NULL
23939                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23940               continue;
23941 
23942             curr->begin_entry
23943                 = add_addr_table_entry (xstrdup (curr->begin),
23944                                         ate_kind_label);
23945           }
23946       }
23947 
23948   FOR_EACH_CHILD (die, c, index_location_lists (c));
23949 }
23950 
23951 /* Optimize location lists referenced from DIE
23952    children and share them whenever possible.  */
23953 
23954 static void
optimize_location_lists(dw_die_ref die)23955 optimize_location_lists (dw_die_ref die)
23956 {
23957   loc_list_hash_type htab;
23958   htab.create (500);
23959   optimize_location_lists_1 (die, htab);
23960   htab.dispose ();
23961 }
23962 
23963 /* Output stuff that dwarf requires at the end of every file,
23964    and generate the DWARF-2 debugging info.  */
23965 
23966 static void
dwarf2out_finish(const char * filename)23967 dwarf2out_finish (const char *filename)
23968 {
23969   limbo_die_node *node, *next_node;
23970   comdat_type_node *ctnode;
23971   hash_table <comdat_type_hasher> comdat_type_table;
23972   unsigned int i;
23973   dw_die_ref main_comp_unit_die;
23974 
23975   /* PCH might result in DW_AT_producer string being restored from the
23976      header compilation, so always fill it with empty string initially
23977      and overwrite only here.  */
23978   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23979   producer_string = gen_producer_string ();
23980   producer->dw_attr_val.v.val_str->refcount--;
23981   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23982 
23983   gen_scheduled_generic_parms_dies ();
23984   gen_remaining_tmpl_value_param_die_attribute ();
23985 
23986   /* Add the name for the main input file now.  We delayed this from
23987      dwarf2out_init to avoid complications with PCH.  */
23988   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23989   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23990     add_comp_dir_attribute (comp_unit_die ());
23991   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23992     {
23993       bool p = false;
23994       htab_traverse (file_table, file_table_relative_p, &p);
23995       if (p)
23996 	add_comp_dir_attribute (comp_unit_die ());
23997     }
23998 
23999   if (deferred_locations_list)
24000     for (i = 0; i < deferred_locations_list->length (); i++)
24001       {
24002 	add_location_or_const_value_attribute (
24003 	    (*deferred_locations_list)[i].die,
24004 	    (*deferred_locations_list)[i].variable,
24005 	    false,
24006 	    DW_AT_location);
24007       }
24008 
24009   /* Traverse the limbo die list, and add parent/child links.  The only
24010      dies without parents that should be here are concrete instances of
24011      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24012      For concrete instances, we can get the parent die from the abstract
24013      instance.  */
24014   for (node = limbo_die_list; node; node = next_node)
24015     {
24016       dw_die_ref die = node->die;
24017       next_node = node->next;
24018 
24019       if (die->die_parent == NULL)
24020 	{
24021 	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24022 
24023 	  if (origin && origin->die_parent)
24024 	    add_child_die (origin->die_parent, die);
24025 	  else if (is_cu_die (die))
24026 	    ;
24027 	  else if (seen_error ())
24028 	    /* It's OK to be confused by errors in the input.  */
24029 	    add_child_die (comp_unit_die (), die);
24030 	  else
24031 	    {
24032 	      /* In certain situations, the lexical block containing a
24033 		 nested function can be optimized away, which results
24034 		 in the nested function die being orphaned.  Likewise
24035 		 with the return type of that nested function.  Force
24036 		 this to be a child of the containing function.
24037 
24038 		 It may happen that even the containing function got fully
24039 		 inlined and optimized out.  In that case we are lost and
24040 		 assign the empty child.  This should not be big issue as
24041 		 the function is likely unreachable too.  */
24042 	      gcc_assert (node->created_for);
24043 
24044 	      if (DECL_P (node->created_for))
24045 		origin = get_context_die (DECL_CONTEXT (node->created_for));
24046 	      else if (TYPE_P (node->created_for))
24047 		origin = scope_die_for (node->created_for, comp_unit_die ());
24048 	      else
24049 		origin = comp_unit_die ();
24050 
24051 	      add_child_die (origin, die);
24052 	    }
24053 	}
24054     }
24055 
24056   limbo_die_list = NULL;
24057 
24058 #if ENABLE_ASSERT_CHECKING
24059   {
24060     dw_die_ref die = comp_unit_die (), c;
24061     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24062   }
24063 #endif
24064   resolve_addr (comp_unit_die ());
24065   move_marked_base_types ();
24066 
24067   for (node = deferred_asm_name; node; node = node->next)
24068     {
24069       tree decl = node->created_for;
24070       /* When generating LTO bytecode we can not generate new assembler
24071          names at this point and all important decls got theirs via
24072 	 free-lang-data.  */
24073       if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24074 	  && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24075 	{
24076 	  add_linkage_attr (node->die, decl);
24077 	  move_linkage_attr (node->die);
24078 	}
24079     }
24080 
24081   deferred_asm_name = NULL;
24082 
24083   /* Walk through the list of incomplete types again, trying once more to
24084      emit full debugging info for them.  */
24085   retry_incomplete_types ();
24086 
24087   if (flag_eliminate_unused_debug_types)
24088     prune_unused_types ();
24089 
24090   /* Generate separate COMDAT sections for type DIEs. */
24091   if (use_debug_types)
24092     {
24093       break_out_comdat_types (comp_unit_die ());
24094 
24095       /* Each new type_unit DIE was added to the limbo die list when created.
24096          Since these have all been added to comdat_type_list, clear the
24097          limbo die list.  */
24098       limbo_die_list = NULL;
24099 
24100       /* For each new comdat type unit, copy declarations for incomplete
24101          types to make the new unit self-contained (i.e., no direct
24102          references to the main compile unit).  */
24103       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24104         copy_decls_for_unworthy_types (ctnode->root_die);
24105       copy_decls_for_unworthy_types (comp_unit_die ());
24106 
24107       /* In the process of copying declarations from one unit to another,
24108          we may have left some declarations behind that are no longer
24109          referenced.  Prune them.  */
24110       prune_unused_types ();
24111     }
24112 
24113   /* Generate separate CUs for each of the include files we've seen.
24114      They will go into limbo_die_list.  */
24115   if (flag_eliminate_dwarf2_dups)
24116     break_out_includes (comp_unit_die ());
24117 
24118   /* Traverse the DIE's and add add sibling attributes to those DIE's
24119      that have children.  */
24120   add_sibling_attributes (comp_unit_die ());
24121   for (node = limbo_die_list; node; node = node->next)
24122     add_sibling_attributes (node->die);
24123   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24124     add_sibling_attributes (ctnode->root_die);
24125 
24126   /* When splitting DWARF info, we put some attributes in the
24127      skeleton compile_unit DIE that remains in the .o, while
24128      most attributes go in the DWO compile_unit_die.  */
24129   if (dwarf_split_debug_info)
24130     main_comp_unit_die = gen_compile_unit_die (NULL);
24131   else
24132     main_comp_unit_die = comp_unit_die ();
24133 
24134   /* Output a terminator label for the .text section.  */
24135   switch_to_section (text_section);
24136   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24137   if (cold_text_section)
24138     {
24139       switch_to_section (cold_text_section);
24140       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24141     }
24142 
24143   /* We can only use the low/high_pc attributes if all of the code was
24144      in .text.  */
24145   if (!have_multiple_function_sections
24146       || (dwarf_version < 3 && dwarf_strict))
24147     {
24148       /* Don't add if the CU has no associated code.  */
24149       if (text_section_used)
24150         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24151                             text_end_label, true);
24152     }
24153   else
24154     {
24155       unsigned fde_idx;
24156       dw_fde_ref fde;
24157       bool range_list_added = false;
24158 
24159       if (text_section_used)
24160         add_ranges_by_labels (main_comp_unit_die, text_section_label,
24161                               text_end_label, &range_list_added, true);
24162       if (cold_text_section_used)
24163         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24164                               cold_end_label, &range_list_added, true);
24165 
24166       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24167 	{
24168 	  if (DECL_IGNORED_P (fde->decl))
24169 	    continue;
24170 	  if (!fde->in_std_section)
24171             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24172                                   fde->dw_fde_end, &range_list_added,
24173                                   true);
24174 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24175             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24176                                   fde->dw_fde_second_end, &range_list_added,
24177                                   true);
24178 	}
24179 
24180       if (range_list_added)
24181 	{
24182 	  /* We need to give .debug_loc and .debug_ranges an appropriate
24183 	     "base address".  Use zero so that these addresses become
24184 	     absolute.  Historically, we've emitted the unexpected
24185 	     DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24186 	     Emit both to give time for other tools to adapt.  */
24187           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24188 	  if (! dwarf_strict && dwarf_version < 4)
24189             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24190 
24191 	  add_ranges (NULL);
24192 	}
24193     }
24194 
24195   if (debug_info_level >= DINFO_LEVEL_TERSE)
24196     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24197 		    debug_line_section_label);
24198 
24199   if (have_macinfo)
24200     add_AT_macptr (comp_unit_die (),
24201 		   dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24202 		   macinfo_section_label);
24203 
24204   if (dwarf_split_debug_info)
24205     {
24206       /* optimize_location_lists calculates the size of the lists,
24207          so index them first, and assign indices to the entries.
24208          Although optimize_location_lists will remove entries from
24209          the table, it only does so for duplicates, and therefore
24210          only reduces ref_counts to 1.  */
24211       index_location_lists (comp_unit_die ());
24212 
24213       if (addr_index_table != NULL)
24214         {
24215           unsigned int index = 0;
24216           htab_traverse_noresize (addr_index_table,
24217                                   index_addr_table_entry, &index);
24218         }
24219     }
24220 
24221   if (have_location_lists)
24222     optimize_location_lists (comp_unit_die ());
24223 
24224   save_macinfo_strings ();
24225 
24226   if (dwarf_split_debug_info)
24227     {
24228       unsigned int index = 0;
24229 
24230       /* Add attributes common to skeleton compile_units and
24231          type_units.  Because these attributes include strings, it
24232          must be done before freezing the string table.  Top-level
24233          skeleton die attrs are added when the skeleton type unit is
24234          created, so ensure it is created by this point.  */
24235       add_top_level_skeleton_die_attrs (main_comp_unit_die);
24236       (void) get_skeleton_type_unit ();
24237       htab_traverse_noresize (debug_str_hash, index_string, &index);
24238     }
24239 
24240   /* Output all of the compilation units.  We put the main one last so that
24241      the offsets are available to output_pubnames.  */
24242   for (node = limbo_die_list; node; node = node->next)
24243     output_comp_unit (node->die, 0);
24244 
24245   comdat_type_table.create (100);
24246   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24247     {
24248       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24249 
24250       /* Don't output duplicate types.  */
24251       if (*slot != HTAB_EMPTY_ENTRY)
24252         continue;
24253 
24254       /* Add a pointer to the line table for the main compilation unit
24255          so that the debugger can make sense of DW_AT_decl_file
24256          attributes.  */
24257       if (debug_info_level >= DINFO_LEVEL_TERSE)
24258         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24259                         (!dwarf_split_debug_info
24260                          ? debug_line_section_label
24261                          : debug_skeleton_line_section_label));
24262 
24263       output_comdat_type_unit (ctnode);
24264       *slot = ctnode;
24265     }
24266   comdat_type_table.dispose ();
24267 
24268   /* The AT_pubnames attribute needs to go in all skeleton dies, including
24269      both the main_cu and all skeleton TUs.  Making this call unconditional
24270      would end up either adding a second copy of the AT_pubnames attribute, or
24271      requiring a special case in add_top_level_skeleton_die_attrs.  */
24272   if (!dwarf_split_debug_info)
24273     add_AT_pubnames (comp_unit_die ());
24274 
24275   if (dwarf_split_debug_info)
24276     {
24277       int mark;
24278       unsigned char checksum[16];
24279       struct md5_ctx ctx;
24280 
24281       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
24282       md5_init_ctx (&ctx);
24283       mark = 0;
24284       die_checksum (comp_unit_die (), &ctx, &mark);
24285       unmark_all_dies (comp_unit_die ());
24286       md5_finish_ctx (&ctx, checksum);
24287 
24288       /* Use the first 8 bytes of the checksum as the dwo_id,
24289         and add it to both comp-unit DIEs.  */
24290       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24291       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24292 
24293       /* Add the base offset of the ranges table to the skeleton
24294         comp-unit DIE.  */
24295       if (ranges_table_in_use)
24296         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24297                         ranges_section_label);
24298 
24299       switch_to_section (debug_addr_section);
24300       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24301       output_addr_table ();
24302     }
24303 
24304   /* Output the main compilation unit if non-empty or if .debug_macinfo
24305      or .debug_macro will be emitted.  */
24306   output_comp_unit (comp_unit_die (), have_macinfo);
24307 
24308   if (dwarf_split_debug_info && info_section_emitted)
24309     output_skeleton_debug_sections (main_comp_unit_die);
24310 
24311   /* Output the abbreviation table.  */
24312   if (abbrev_die_table_in_use != 1)
24313     {
24314       switch_to_section (debug_abbrev_section);
24315       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24316       output_abbrev_section ();
24317     }
24318 
24319   /* Output location list section if necessary.  */
24320   if (have_location_lists)
24321     {
24322       /* Output the location lists info.  */
24323       switch_to_section (debug_loc_section);
24324       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24325       output_location_lists (comp_unit_die ());
24326     }
24327 
24328   output_pubtables ();
24329 
24330   /* Output the address range information if a CU (.debug_info section)
24331      was emitted.  We output an empty table even if we had no functions
24332      to put in it.  This because the consumer has no way to tell the
24333      difference between an empty table that we omitted and failure to
24334      generate a table that would have contained data.  */
24335   if (info_section_emitted)
24336     {
24337       unsigned long aranges_length = size_of_aranges ();
24338 
24339       switch_to_section (debug_aranges_section);
24340       output_aranges (aranges_length);
24341     }
24342 
24343   /* Output ranges section if necessary.  */
24344   if (ranges_table_in_use)
24345     {
24346       switch_to_section (debug_ranges_section);
24347       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24348       output_ranges ();
24349     }
24350 
24351   /* Have to end the macro section.  */
24352   if (have_macinfo)
24353     {
24354       switch_to_section (debug_macinfo_section);
24355       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24356       output_macinfo ();
24357       dw2_asm_output_data (1, 0, "End compilation unit");
24358     }
24359 
24360   /* Output the source line correspondence table.  We must do this
24361      even if there is no line information.  Otherwise, on an empty
24362      translation unit, we will generate a present, but empty,
24363      .debug_info section.  IRIX 6.5 `nm' will then complain when
24364      examining the file.  This is done late so that any filenames
24365      used by the debug_info section are marked as 'used'.  */
24366   switch_to_section (debug_line_section);
24367   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24368   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24369     output_line_info (false);
24370 
24371   if (dwarf_split_debug_info && info_section_emitted)
24372     {
24373       switch_to_section (debug_skeleton_line_section);
24374       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24375       output_line_info (true);
24376     }
24377 
24378   /* If we emitted any indirect strings, output the string table too.  */
24379   if (debug_str_hash || skeleton_debug_str_hash)
24380     output_indirect_strings ();
24381 }
24382 
24383 #include "gt-dwarf2out.h"
24384